Commit Graph
139 Commits
Author SHA1 Message Date
Geoff RomerandJon Meow c311c8849c Use static name resolution in Interpreter (#1022)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2022-01-21 09:48:11 -08:00
Geoff Romer 461e178273 Store named constant values in the AST (#1011)
This enables us to stop using `Env` in the typechecker. As a byproduct, this commit also restructures the interpreter to handle run-time global initialization as part of ordinary execution, using the Action stack.
2022-01-13 11:07:28 -08:00
Geoff Romer 50263483d8 Add name accessor to NamedEntityView (#994)
This required changing BindingPattern::name() to return `"_"` instead of nullopt when representing a `"_"` binding.

Semi-related drive-by fixes:
- Update BindingPlaceholderValue to expose a NamedEntity instead of a string name, and stop exposing its type.
- Drop the unused SourceLocation parameter of ValueEqual
2022-01-04 15:53:11 -08:00
Geoff Romer fd45b089e9 Add typechecking of type declarations (#984)
Also explicitly model the types of expressions whose values are class or choice types, and make those be the static_type of the type declaration. This ensures that the static_type of a NamedEntity consistently corresponds to the static type of an IdentifierExpression using that name.
2021-12-13 16:19:52 -08:00
Geoff Romer e65e85b15d Use AST nodes as generic arg deduction keys (#982) 2021-12-10 12:19:06 -08:00
Geoff RomerandJon Meow 17e0a1afb9 Implement static name resolution (#958)
This doesn't actually use the results of name resolution, but it does verify that they are present.
Also ensures that name resolution and type checking are applied to deduced function parameters and the implicit call to `Main()`.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-30 13:33:54 -08:00
Geoff RomerandJon Meow dc5e62fc7a Support parsing and testing unimplemented expressions (#957)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-30 12:29:15 -08:00
Geoff Romer be0c1e9da6 Move value-category checking to compile time (#960) 2021-11-29 17:07:50 -08:00
Geoff Romer 3e188c8562 Express prelude using Carbon syntax (#955)
This should be clearer, more maintainable, and more scalable than building the prelude AST by hand.
2021-11-29 13:56:55 -08:00
Geoff Romer 3f7e1cd3fb Clarify and simplify handling of lvalues (#956)
- Rename `PointerValue` to `LValue` to reflect how it's actually used. We can introduce a `PointerValue` type when we add support for actual pointer values.
- Remove support for pattern assignment. It's unclear if Carbon will support this, and even if we do, it raises questions that should first be addressed in a language proposal, like "is the left-hand side of `(x, y) = (1, 2)` an lvalue, or a pattern, or both, or something else entirely?"
2021-11-23 14:13:08 -08:00
Geoff Romer 9642d7ad05 Factor out ActionStack from Interpreter (#951) 2021-11-17 12:59:16 -08:00
Geoff Romer 7a5b8434c8 Define a base class for all AST nodes. (#947)
Also implement code-generation to manage the resulting boilerplate.
2021-11-16 11:54:47 -08:00
Geoff Romer d854fb93cb Factor out AST node for function return types. (#912)
This enables us to stop treating the return type as a Pattern (which is really isn't), treat return types more consistently with other static types in the typechecker, and drop ReturnTypeContext.

Additional changes:
- Merge TypeCheckFunDef with TypeOfFunDef.
- Handle implicit conversions in `return` statements.
- Require function type literals to have an explicit `->`.
- Move consistency check for omitted returns from TypeChecker to ResolveControlFlow.
2021-11-12 11:30:31 -08:00
Geoff RomerandJon Meow a4aff26821 Stop allocating Actions on the Arena (#934)
This enables us to manage local variables in Carbon using C++ RAII.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-11-10 16:35:06 -08:00
Geoff Romer a9bb06ca14 Make NamedEntityInterface non-movable (#946) 2021-11-10 09:33:00 -08:00
Jon Meow e8d9b7675b Merge ClassDefinition and ClassDeclaration (#940) 2021-11-09 15:24:22 -08:00
Jon Meow 73eea8e47a Merge UnwindTo and UnwindPast logic (#926)
Also shifts the DeallocateScope concept into UnwindTodoTop since that's how it's used.
2021-11-08 09:22:38 -08:00
Jon MeowandGeoff Romer 27e084d37a Start populating named entities in relevant locations. (#919)
This starts detecting naming collisions as a consequence of being able to determine when the name is declared twice in a given scope.



Co-authored-by: Geoff Romer <gromer@google.com>
2021-11-03 08:04:13 -07:00
Jon Meow 904774fbb8 Collapse Sequence into Block. (#927) 2021-11-01 14:59:39 -07:00
Jon Meow bbd4940e6d Be more explicit about AST types (#921)
This was born out of wanting FunctionDeclaration to explicitly have a Block for a body, and became a bit more of specifying types around. Note this forces exec_program to generate a Block for print()'s body, which is probably more correct as now we can expect a standard FunctionDeclaration AST structure, even for the built-ins.

There is a syntactic change here: a continuation's body is now a Block, not just a Statement. I've added an example disallowed test case. I think this is more reasonable syntax.

Other than that, note that optional_else now generates a valid Block. This has me thinking about whether we can eliminate Sequence, but that seemed well out of scope for this.
2021-10-28 13:54:45 -07:00
Geoff Romer 70a7839a31 Fix bug from #909 (#924) 2021-10-27 17:09:03 -07:00
Geoff Romer 0df9e8666c Factor AllocationId out of Address (#916)
This lets us statically distinguish between code that works with arbitrary `Address`es and code that can only work with pointers to separately-allocated storage, and so we no longer need to worry about the latter code crashing at run-time (as `Heap::Deallocate` did) or silently doing the wrong thing (as `Heap::PrintAddress` did) if it's given the wrong kind of `Address`.
2021-10-27 16:40:17 -07:00
Geoff Romer 0da907fd2a Unify StructElement and VarValues (#909) 2021-10-27 16:33:38 -07:00
Geoff RomerandJon Meow 3bec7f8dc0 Unify Action and Scope stacks, and eliminate Frame (#880)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-21 14:55:41 -07:00
Geoff Romer 3677b76cb8 Perform implicit conversions at run time (#903)
This resolves the "cheating" in #870.
2021-10-21 12:47:49 -07:00
Jon MeowandGeoff Romer 057b9fe253 Clean up remaining executable_semantics clang-tidy warnings (#906)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-10-20 10:25:22 -07:00
Geoff RomerandJon Meow 79e3d284b4 Move pattern interpretation to compile time (#904)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2021-10-20 10:05:17 -07:00
Jon Meow ffa4e76ede Add underscores to private data members (#898)
This is the last PR I plan to have focused on #720
2021-10-19 14:27:10 -07:00
Jon Meow a9eed3dbf1 Pass the flag instead of using a global. (#893)
Arguably missed in #769 

Note, this is reminding me we have more class members to rename for `_`, but I felt it's best to use the new naming instead of adding more to clean up.
2021-10-19 09:16:57 -07:00
Geoff Romer a3eac75a5b Unify function declarations and definitions. (#896) 2021-10-18 16:21:39 -07:00
Geoff Romer c4d7adde9a Drop CopyVal (#895)
Values are immutable, so they should never need to be copied.
2021-10-18 15:58:35 -07:00
Jon Meow 5b67a08d97 Refactor Action accessors (#891) 2021-10-18 11:02:09 -07:00
Jon Meow eeed6301d6 Refactor Value accessors (#892) 2021-10-18 10:50:56 -07:00
Jon Meow 8bda2ca432 Refactor Pattern and Member accessors. (#889) 2021-10-18 10:25:52 -07:00
Jon Meow b278e4edcf Refactor Statement accessors (#890) 2021-10-18 10:25:35 -07:00
Geoff Romer bb28d37eed Drop support for named tuple fields (#886)
Rationale: Based on the status of #478 and #505, Carbon won't have this feature for a while, and it will be simpler not to support it on spec in the meantime.
2021-10-15 13:19:57 -07:00
Jon MeowandGeoff Romer 9007bfcb88 Run clang-tidy on the interpreter directory (#885)
`// NOLINT` is added on type aliases in stack.h and dictionary.h to allow lower_snake_case naming -- this didn't feel like a check worth disabling in spite of false positives.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-10-15 08:54:53 -07:00
Jon Meow 55ecb62ce5 Refactor Expression accessor/mutator style (#883) 2021-10-13 16:56:27 -07:00
Geoff Romer a99d882223 Perform type-checking in place (#867) 2021-10-11 16:37:39 -07:00
Geoff Romer d177a08e01 Make continuations consistently "shallow" (#874)
Prior to this change, `__await` would make a deep copy of the continuation stack, but shallow-copy the individual stack frames within it. As a result, continuations appeared to have shallow semantics so long as the continuation stack had only a single frame.

This change also removes an obsolete test from the brief period when we intended continuations to have deep-copy semantics, which has been passing basically by accident.
2021-10-11 14:57:09 -07:00
Geoff Romer 6068d2306b Address feedback from #856 (#864)
* Make tuple/struct fields mutable.
* Avoid ExpectType when we know it will fail.
2021-10-08 16:52:17 -07:00
Jon Meow 70797e8bf8 Mass rename SourceLoc and Tag (#860)
This does a mass rename of:

-   `SourceLoc()` -> `source_loc()` for property naming
    - `loc` -> `source_loc_` for underscore+consistency
    - Generally changing function args to `source_loc` for consistency
-   `Tag()` -> `kind()` for property naming and `Kind` parity
    - `tag` -> `kind_` for underscore

Also renames `Pos` and `Results` on `Action`. These are a bit of an exception in that most base classes only have `Tag` and maybe `SourceLoc`, whereas `Action` has a little more. I felt okay having `source_loc()` and `kind()` on the base class where children do `Exp()` and the like, but it felt weird to me to mix it on the same class.

The reason for doing this cross-class in one PR is so that I can do it efficiently with a global replace in the codebase, rather than e.g. changing `Expression` but having to read through compiler errors to determine where it's calling `Expression`'s `Tag` versus a different `Tag`. The end result should be equivalent.
2021-09-29 16:52:12 -07:00
Geoff Romer c4e40aaa86 Add support for struct types, following p0561. (#856) 2021-09-29 16:14:27 -07:00
Jon Meow d157d96338 Migrate Declaration to newer property style, class-ify ClassDefinition (#859) 2021-09-29 14:38:18 -07:00
Jon Meow 25dce9fbcf Change Match clauses from pairs to classes (#858)
Updates style of the Match class at the same time.
2021-09-29 13:36:54 -07:00
Geoff Romer 532948d9a5 Miscellaneous tuple simplifications (#857)
- Drop unnecessary special-casing of 0-tuples.
- Drop unused constructor.
2021-09-27 12:10:16 -07:00
Jon Meow 04ab30f231 Make the AST mutable (#849)
The code is pretty intertwined: having the AST be truly mutable means (to me) changing parser.ypp to return non-const values, but then the way things are passed around between objects should be non-const (particularly an issue with lists), which then creates issues with construction of lists in the TypeChecker, which then TypeChecker needs to mostly be non-const.

Due to the difficulties in breaking this apart, whereas I'd previously considering refactoring accessor naming in the same PR, I've largely avoided doing so. The intent is then that this PR focuses mainly on const -> non-const AST behavior.

call_main moves out of interpreter.cpp so that interpreter.cpp can receive a fully const AST.
2021-09-27 10:57:31 -07:00
Jon Meow 721743bc58 Change Alternative to a class (#853)
Revives BisonWrap because this seems a reasonable use of it (avoiding the need to have an std::optional or pointer for Alternative, both of which I thought could be unclear about the intent).
2021-09-27 09:05:22 -07:00
Jon Meow 6ab2bff69c Switch FunctionDefinition to a class (#852)
Splitting out the task from #849
2021-09-27 09:05:04 -07:00
Jon Meow 56dc4ae375 Rename Ptr<T> to Nonnull<T*> (#832)
Note that ptr.h also includes an enable_if change, to help avoid https://bugs.llvm.org/show_bug.cgi?id=51881
2021-09-16 19:22:57 -07:00