Commit Graph
68 Commits
Author SHA1 Message Date
Adrien Leravat 1d0bb85d0c Explorer: basic abstract class support (#2441)
Relates to #1881 

Features:
* Add basic support for `abstract` class
    * Allow extending an abstract class
    * Prevent direct instantiation of an abstract class
Changes:
* Check that class extensibility for `VariableDefinition` is not `Abstract`
* Add corresponding set of lit tests
2022-12-06 09:35:55 -08:00
Adrien LeravatandRichard Smith 46f4887cf7 Explorer: support .base to initialize parent class from struct (#2361)
Relates to https://github.com/carbon-language/carbon-lang/issues/1881

- Add support for `.base` field in structs for [parent class initialization](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#constructors)
    - Disabling base class initialization without `.base`
- Support class constructors (`Create() -> Self`) for base classes
- Direct access to base class(es) attributes with `object.var` remains unaffected

Changes:
- Add `TypeChecker::FieldTypesWithBase` to help assessing if a struct with `base` fields can be converted to a class
- Add a new `base_type()` attribute+getter to `NominalClassDeclaration` to as a first step to allow resolving parametrized classes
- Add a new `base` attribute+getter to `NominalClassValue` that contains the base class `NominalClassValue`. It is currently used mainly to get and set members of a class object.
- Add `Interpreter::ConvertClassWithBase` to build `NominalClassValue` from a init struct, that contains `.base` fields with either `NominalClassValue` or `StructValue`
- Add `FindClassField` to find a field in a class or its base classes
- Remove superfluous `ClassDeclaration::base()` in favor of `ClassDeclaration::base_type()`

Limitations;
- Though some work is done in that direction, parametrized base class where time is not know at the declaration site are not supported. Namely the example below does not compile
```
base class A(T:! Type) {}
class B(T:! Type) extends A(T) {}
```
But this one is functional already
```
base class A(T:! Type) {}
class B extends A(i32) {}
```

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-11-29 13:14:30 -08:00
Richard Smith dfbe35bae5 Remove pattern evaluation from the interpreter. (#2400)
Patterns all compute their values when type-checked, so we never
actually need to do any multi-step evaluation to compute the value of a
pattern. Doing so was leading to quadratic runtime and excess noise in
the trace file.
2022-11-15 16:36:18 -08:00
Richard Smith e557d4af3b Make addr me: Self* work for interface methods. (#2374)
Fixes #2223.
2022-11-07 08:11:19 -08:00
Richard Smith 9e8cacae00 Implement support for named constraints (#2359)
Basic support for named constraints as described in [the generics design](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#named-constraints). Support is provided for `extends` and `impl` declarations in `constraint`, but not yet for member aliases.

The missing prelude named constraints `Add`, `Mul`, `Ordered`, etc. are also added.
2022-11-04 16:23:02 -07:00
cef93fba5e Feature call destructor for tuples and bug fixes for the destructor process (#2255)
This PR includes the following changes:
*  Added destruction process for tuples
*  Fix: In the current version only the last member of a object can be destroyed
*  Fix: In the current version, the destructor of the object is called after each method call 

I hope it is useful

Co-authored-by: m new <michael.burzan@outlook.de>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-11-04 12:13:24 -07:00
Richard Smith 0219218b01 Make type contexts expect a value of type Type. (#2357)
In particular, this means that a type can implement `ImplicitAs(Type)` and have values of that type behave like types.

This implies that `()` and `{}` are no longer types. They are now values whose type is the result of converting `()` or `{}` to type `Type`, as has been discussed recently and seems to be the supported direction. This fixes various cases where these types were previously mishandled.
2022-10-28 08:20:49 -07:00
Richard Smith f46eaf49a2 Compute a fixed point of rewrite constraints. (#2350)
When resolving a constraint type, compute the fixed point of the specified rewrite constraints, as requested by @josh11b in review of #2173 and tentatively agreed as our direction. If no such fixed point exists, detect that situation and diagnose the problem.

The algorithm used here is to iteratively apply all rewrites to each rewrite constraint until either one of them refers to itself, which indicates there's a cycle in the rewrite graph, or the set of rewrites converges.

This algorithm has some pathological inputs in which the runtime can grow exponentially in the size of the input (indeed, the fully-rewritten set of rewrites can grow exponentially in the size of the input, so this is unavoidable), so an iteration limit is also provided.
2022-10-26 14:49:17 -07:00
Richard Smith bc7bf325d6 Set the value of .Self in a where to that of the outer .Self. (#2344)
Prior to this change, declarations like `let N:! X(.Self) where .(X(.Self).Y) == 5;` have the surprising behavior of the two `.Self` expressions resolving to two different symbolic values. Fix this by forcing the inner one to have the same symbolic value as the outer one, albeit with a different type.
2022-10-25 11:59:42 -07:00
Richard Smith 374bf9f853 Require convertibility to the type of the associated constant when checking a rewrite constraint. (#2321)
Per recent discussion, in `... where .A = B`, require that `B` is implicitly convertible to the type of `A` immediately, rather than treating that as part of the criteria that a type must satisfy to satisfy the resulting constraint. Extend the implementation of implicit conversion so that conversion of a type to a constraint checks that the type satisfies the constraint.

As part of implementing this, stop duplicating rewrite constraints as equality constraints. Instead, when checking that a constraint is satisfied, check both its equality constraints and its rewrite constraints. This fixes an infinite recursion that would otherwise be caused by this change, and is also a necessary prerequisite for applying rewrite constraints to equality constraints, where we would otherwise collapse the implied equality constraints to a tautological `V == V` constraint.

In passing, make ErrorBuilder support building the error message more incrementally and use that to improve diagnostics for mismatched values with equality constraints.
2022-10-20 16:18:26 -07:00
Jon Ross-Perkins 8e5dcc2588 Enable readability-qualified-auto (#2314)
As suggested on #2310
2022-10-18 19:21:49 -07:00
Jon Ross-Perkins 4d522c8e90 Finish making clang-tidy (mostly) work (again) and run -fix (#2312)
This does some more work to the run_clang_tidy.py wrapper script, and runs an example pass.

"again" because it's really the proto fuzzer changes that broke it, it had been working before.

"mostly" because there's still an issue within the proto fuzzer that it can't find "port/protobuf.h", i.e. https://github.com/google/libprotobuf-mutator/tree/master/port, but I'm still hesitant to add an include path there.
2022-10-18 15:48:17 -07:00
Richard Smith 561e45033e Remove TypeOf*Type for types whose type-of-type can just be Type (#2302)
We don't seem to have any need for `TypeOf*Type` types, and having them introduces the temptation to use them during type-checking, which would lead to types having different behavior when their type-of-type is `Type` versus when it's a more precise type. Remove these types for now.

If we later decide that we want each type literal to have a unique type, as we do for value literals, we can introduce a single value kind for that rather than one for each kind of type.

No changes to `TypeOfMemberName`, `TypeOfParameterizedEntityName`, and `TypeOfMixinPseudoType`, which are placeholders, not real types.
2022-10-18 14:08:40 -07:00
Richard Smith 4b679510e7 Check equality constraints when checking whether a constraint is satisfied (#2294)
Add checking that a type only satisfies a constraint if it satisfies all of that constraint's equality and rewrite constraints.

Enforce the rule that `=` must be used in impls when specifying associated constant values rather than `==`.

Turn off the pre-#2173 single-step equality behavior. This is getting somewhat ahead of the approved design, but it's a one-line change to restore the old behavior.

Fix `CARBON_CHECK` to handle top-level `,`s in its argument, such as may happen in template argument lists, as this change introduces such a check.
2022-10-17 22:07:19 -07:00
a3329cf004 Diagnose uses of declarations that are too early. (#2288)
There are lots of ways a declaration can be used before we have the information necessary to handle that use. Issue diagnostics for these.

Interleave declaration and type-checking of global declarations so that declaring a later declaration can depend on the results of type-checking an earlier one.

Incorporates tests added in #2266.

Fixes #1394, fixes #1395, fixes #1396.

Co-authored-by: pmqtt <51272730+pmqtt@users.noreply.github.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-10-13 15:30:29 -07:00
Richard Smith b1c50bd6a7 Revert "Fix: Issue #1394 (#2266)" (#2285)
This change left a test broken and removed a fair amount of test coverage for unrelated features, and after further discussion it's not clear that this is the direction we want to go in.

This reverts commit 6cf2272bbe.
2022-10-13 12:15:56 -07:00
6cf2272bbe Fix: Issue #1394 (#2266)
* start correct

* Fix #1394

* remove llvm::outs()

* Fix #1394

* ---

* fix issue-1394

* fix issue-1394

* Update explorer/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* fix issue-1394

* remove and rename test files

* add changes from j.m

Co-authored-by: m new <michael.burzan@outlook.de>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-10-13 19:44:53 +02:00
Richard Smith 7f7b9a4086 Implement support for extends and impl as declarations in interfaces (#2279) 2022-10-12 17:13:25 -07:00
Richard Smith 9f534ae004 Implement support for rewrite constraints (#2276)
Implements basic support for rewrite constraints as proposed in #2173.

Support for associated constants and complex constraints in general is also made more robust: argument deduction now properly computes and substitutes witnesses, and interface declarations track a more correct description of the constraint that they introduce than the one we previously built.
2022-10-12 12:55:55 -07:00
Richard Smith 9feff92f22 Track witnesses wherever possible (#2263)
Change explorer's handling of witnesses to track them wherever possible. Associated constant support needs witnesses to be available much more pervasively.

The main changes here are:

-   `Substitute` now takes a set of bindings covering both `Value`s for `GenericBinding`s and `Witness`es for `ImplBinding`s, and substitutes both.
-   Witnesses are now tracked within `ConstraintType`s. The `.Self` type has an `ImplBinding` that self-references from parts of the constraint to other parts of the constraint can use to access the witness for the constraint.
-   The constraint type for an `impl` and for a `GenericBinding` are now stored in pre-substituted form, with references to the actual constrained type rather than symbolic references to `.Self`. This results in minor changes in diagnostic text.

Argument deduction still performs substitutions without remapping witnesses.
2022-10-11 15:20:23 -07:00
Richard Smith 8f0f69b65f Remove RuntimeError / CompilationError. (#2258)
Instead, work out the prefix for an error based on whether it was produced during parsing, semantic analysis, or when running the program.
2022-10-04 15:39:04 -07:00
Richard Smith 2340cb6703 Build value representations for all kinds of witness (#2250)
Instead of forming a `SymbolicWitness` that contains an `Expression` when we can't directly resolve a witness to an `impl`, form different kinds of `Witness` values for the various situations:

- A `BindingWitness` witnesses that a type implements a constraint by reference to an `ImplBinding`.
- A `ConstraintWitness` witnesses that a type implements a constraint by reference to witnesses for each of the impl constraints within the constraint.
- A `ConstraintImplWitness` witnesses that a type implements a constraint by reference to a larger constraint which contains that constraint as an impl constraint.

Remove `SymbolicWitness` values and `InstantiateImpl` expressions, which are now unused. In order to remove the final usage of `SymbolicWitness`, I fixed a TODO to give `Self` the proper type within an interface declaration. I don't think this is an observable change.

No functional change intended.
2022-10-04 12:14:52 -07:00
Richard Smith 04d49cebd8 Consistently use a Witness rather than an expression to represent a possibly-symbolic witness. (#2245)
Previously we used an expression in some places and a `Witness` values in others. The eventual goal is to make `Witness` values behave like other symbolic values such as `NominalClassType`, but the first step is to consistently treat them like values rather than expressions.

No functionality change intended.
2022-10-03 14:00:41 -07:00
pmqtt cfa295c0af Feature destructor (#2116) 2022-09-26 10:11:05 -07:00
Junhee Cho 69d4363ea5 Adds division to multiplicative expression. (#2091)
* Multiplication and division have the same priority.
* A new builtin interface DivWith is added.
* In some tests expecting a compilation error (syntax error), the error
  message now says it is expecting SLASH or binary *.
2022-09-19 15:02:29 -07:00
pmqttandm new 82f8556890 Implementation of __intrinsic_assert (#2155)
Co-authored-by: m new <michael.burzan@outlook.de>
2022-09-09 13:05:42 -07:00
pmqttandm new 5ef6e9b6a0 Feature not equal (#2146)
Implement != operator.

Co-authored-by: m new <michael.burzan@outlook.de>
2022-09-09 12:25:20 -07:00
Darshal Shettyandjosh11b 037c99e2fb Explorer: Mixin phase 1 (#2069)
* Replay changes from pre-force-push mixin branch

* Base MixinPseudoType off of the new InterfaceType

* Add more test cases.

Also removed an unnecessary check that would have already been
handled by the parser.

* WIP detect member clashes during mixing mixins

* Implement fuzzer changes

* Implement member name clash check when mixing mixins

* Modify parser and lexer for experimental mixin feature

* Add comments

* Update explorer/testdata/mixin/simple-mix-in-mixin.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* Update explorer/testdata/mixin/use-mixin-method-in-class-method.carbon

Co-authored-by: josh11b <josh11b@users.noreply.github.com>

* Make code review changes

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-09-06 17:08:05 -04:00
pmqtt f957d4d87d Feature generic choice (#2042)
Implementation of generic choices!

I hope it is useful!
2022-08-24 11:30:04 -07:00
Zenong Zhang e6651836df Fix source code location for dynamic unformed use in ReturnVar. (#2035)
The error message should point to the source code location of `ReturnVar`, instead of the declaration of the `returned var`.
2022-08-15 12:50:28 -07:00
f754373786 For loop for arrays (#1753)
Co-authored-by: m new <michael.burzan@outlook.de>
Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-08-09 15:34:24 -07:00
Geoff Romer 4ed8180bc7 Make Rand deterministic (#1910)
Also re-enable auto-updating of Rand test (disabled in #1894)
2022-08-09 14:17:26 -07:00
pmqtt 63da070671 add operator to comparsion (#1883)
Implement operator <, <=, > and >=
2022-08-04 09:56:16 -07:00
Richard Smith e26bc32343 Change remaining uses of Bool to bool, following #750. (#1901)
The most significant change here is that explorer now uses the chosen spelling
rather than the old `Bool` spelling. Also update a few documentation examples
and some skeletal design docs to use the chosen spelling.
2022-08-03 17:55:34 -07:00
Richard SmithandJon Ross-Perkins ef96f60425 Add support for bitwise operators. (#1809)
Following #1191, add initial support for bitwise operators. Support for both
integer operations and for operator overloading via the interfaces specified in
#1191 is provided.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-07-31 15:25:10 -07:00
Thejaswi Kadur 5665f32bf7 Support for user-defined Eq impls (#1730)
Related to #1549; user-defined equality is a prerequisite for user-defined comparison.
2022-07-30 11:51:39 -07:00
Adrien Leravat cbd4b8d82d Add explorer support for short-circuit evaluation for 'and' and 'or' (#1789)
Implements short-circuit evaluation for `and` and `or` binary
operators.

Fixes #1651
2022-07-29 13:10:40 -07:00
pmqttandm new df93623741 Extends carbon to generate random number to make samples more interesting (#1643)
Implements the Rand function to carbon

Co-authored-by: m new <michael.burzan@outlook.de>
2022-07-29 10:39:30 -07:00
9f89c63503 Add operator modulo (#1681)
Co-authored-by: m new <michael.burzan@outlook.de>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-07-28 12:51:35 -07:00
Thejaswi Kadur 790e075986 Convert first Print argument to String before evaluating (#1548)
Fixes #1439.
2022-07-27 10:29:09 -07:00
Zenong Zhang 075b3aba1b Implementation of unformed state for global variables. (#1571)
- Allocate UninitializedValue on heap when a VariableDeclaration does not have initialization.
- Added test cases.
2022-07-24 15:32:38 -07:00
Richard Smith 59ae6e31b6 Rename PrimitiveOperatorExpression to OperatorExpression. (#1530)
This isn't modeling only primitive operators any more: it also models operators
that are rewritten as calls to an interface function.
2022-07-22 11:36:36 -07:00
Richard Smith 93e19649aa Support for as operator and user-defined As impls. (#1476) 2022-07-21 13:16:18 -07:00
Zenong ZhangandJon Ross-Perkins 52ee050019 Initial implementation of unformed state for local variables. (#1387)
Allows unformed state for local variables. Reports a run-time error when an unformed local variable is used.
- Added declaration without initialization for local variables in the parser.
- Made the init expression of VariableDefinition optional.
- Expanded (alive, dead) to (uninitialized, alive, dead) in the Heap.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-07-20 16:03:52 -04:00
Jon Ross-Perkins a41915b5af Support basic int printing (#1405)
We don't have overload resolution, so I've simply added PrintInt for now. But it's really tempting to turn __intrinsic_print into a total kludge function.
2022-07-17 23:35:44 -04:00
Richard SmithandJon Ross-Perkins 663ed32b1b Initial support for associated constants (#1376)
Basic support for declaring, specifying the values of, and using associated constants.

This is incomplete in various ways. For example, when checking whether a type satisfies a constraint, there is no check that its associated constants match those in the constraint, and name lookup into a value whose type is an associated constant is not supported yet.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-07-14 18:18:27 -07:00
SlaterLatiao 9106e9239e Initial implementation of returned var. (#1348)
This PR implements the `returned var` for the explorer.
- Enabled `returned var` and `return var` syntax in lexer and parser.
- Split `Return` statement into `ReturnVar` and `ReturnExpression` to distinguish the two types of returns.
- Added logic in name resolution, type checking and interpretation to process `returned var` definition and `ReturnVar` statement.
2022-07-06 13:35:41 -07:00
Richard Smith a1be2a8a38 Track the arguments and witnesses on values indirectly. (#1335)
This avoids the need to make copies of potentially large maps when
the same bindings are used in multiple values, such as when forming
a bound method value.
2022-06-21 08:48:48 -07:00
Richard Smith 17ee3ed9b7 Track a resolved Member on each member access expression. (#1333)
Type checking now stores information on a member access identifying the member that was accessed, not only its name. This parallels what we do for other similar constructs whose meaning is resolved by name lookup or type-checking, and will allow us to avoid redoing lookups in some cases during interpretation.
2022-06-16 17:22:31 -07:00
Richard Smith 2fef76e58c Basic support for .Self within :! bindings and where expressions. (#1311)
`.Self` is modeled as a new kind of expression, `DotSelfExpression`. Name resolution associates each occurrence of a `DotSelfExpression` with a generic binding, much like for an `IdentifierExpression`.

Both `:!` bindings and `where` expressions bring `.Self` into scope. The tentative intent is that if there are multiple `.Self`s in scope and they refer to different bindings, the result of using a `.Self` expression is an ambiguity error, but that is not implemented in this patch. Instead, like for `IdentifierExpression`s, we find the innermost enclosing definition of `.Self`.

`.Foo` expressions are rewritten to `.Self.Foo`, but this isn't enough to make them do anything useful, because associated constants aren't supported in general yet.
2022-06-14 21:49:01 -07:00