Commit Graph
110 Commits
Author SHA1 Message Date
Geoff Romer ec2f8e64e2 Support parsing Carbon files from strings. (#954)
Also, pass file names as string_view for greater convenience.
2021-11-23 10:44:39 -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 Romer a9bb06ca14 Make NamedEntityInterface non-movable (#946) 2021-11-10 09:33:00 -08:00
Chandler Carruth 65ac59eb2a Add a script to compute minimal roots for check_deps. (#932)
This avoids needing to have nearly as many rules here which should
reduce its churn.

I've tested that this reaches the exact same set of transitive
dependencies.

Note, only the last commit here is new.
2021-11-03 12:53:54 -07: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
Chandler Carruth 5f67029479 Use upstream GoogleTest and add related test utils. (#876)
This moves over to the vanilla upstream GoogleTest pulled in the more
expected manner with Bazel. It also adds Abseil and Google Benchmark
libraries in the same fashion (there are cross dependencies here).

As part of this, also introduce a dependency check test that can enforce
basic layering of dependencies. For example, this lets us ensure that
non-test Carbon code only depends on LLVM and Clang despite having other
libraries available. There remains some cleanup to improve the way these
dependency tests work, but this at least ensures we don't regress.

I've also provided workarounds to allow both Carbon code and LLVM code
to freely be used with GoogleTest (and other `std::ostream` based
output code). This is done by extending the code in
`//common/ostream.h`. One downside is that it requires opening the
`llvm` namespace and adding an ADL_found overload there. I think on
balance this is still a win and doesn't make me too nervous.

The new version of GoogleTest requires printing more often from matchers
and so I've also added several printing routines to types that
previously didn't require them. Otherwise, most of the updates are just
using the more conventional upstream style of including the headers and
adding `ostream.h` where it is needed.

I did consider moving code over to use `std::ostream` instead of LLVM's
`raw_ostream`, but the advantages of not doing virtual dispatch still
seem significant, and it also seems good to retain access to LLVM's
formatting utilities built around `raw_ostream` given that we can't pull
arbitrary dependencies into Carbon code outside of test code.

All of this was slightly motivated by requests for newer features in
GoogleTest, but much more-so by my desire to have access to Google
Benchmark and Abseil when writing benchmarks. For example, using
Abseil's random number generator seems extremely helpful when generating
inputs for benchmarks. The growing dependencies between these packages
further motivated me to just pull them all in and ensure they worked
well.
2021-11-02 20:14:12 -07:00
Jon Meow 904774fbb8 Collapse Sequence into Block. (#927) 2021-11-01 14:59:39 -07:00
Jon Meow eeff5dcdae Mark fnty as __Fn to reflect experimental state (#928)
The `__Fn` naming is intended to mirror things like `__Continuation`.

The reason for this path is because it's not clear this is the form we'll want, and I think experimental naming will help reflect that.
2021-11-01 10:56:04 -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 d79387479c Revert formatting suggestion from #906 (#910)
Clang-format insists on this, which seems like a bug to me.
2021-10-21 12:46:09 -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
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 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 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 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
Jon MeowandGeoff Romer 5aa958345b Implement auto return types, removing => returns (#850)
This implements #826, I think covering everything important there.

Regarding ReturnTypeContext, I broke that out because it started feeling like a significant number of args to be passing around, and I think this makes the association inside type checking clearer.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-09-27 11:17:24 -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 b1af08fed0 Rename ptr.h to nonnull.h (#839) 2021-09-20 15:15:30 -07:00
Jon Meow 9edb2459fd Remove BisonWrap, which was a Ptr-specific feature (#836) 2021-09-20 13:51:20 -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
Jon Meow 3351443c8f Switch to a mypy fork that handles imports (#823)
I'm seeing if I can upstream thundergolfer/bazel-mypy-integration#43, but we can also point at my fork for the time being.

This should resolve conflicts with mypy treating imports as non-hermetic, creating inconsistent behavior if packages are/aren't installed locally.
2021-09-13 13:08:54 -07:00
Jon Meow 90f04700e2 Remove global_arena (#814)
With this, only main.cpp instantiates an arena. Maybe we'll want to split that up more later (e.g., so that the runtime interpreter uses its own arena), but given the intent to have type-checking update the AST, I thought this was a reasonable approach for now in order to avoid ownership complexities.

Fixes #769
2021-09-09 11:00:57 -07:00
Geoff Romer f931a8cead Replace all lists with vectors (#815) 2021-09-07 09:20:10 -07:00
Jon MeowandGeoff Romer 820081b8c1 Add table sorting functionality (#796)
Also apply it to the list of tokens. This way there's no need for "sort order" comments.

This was a side-effect of me trying to merge in api/impl/library/package, and thinking "why am I doing this manually?"



Co-authored-by: Geoff Romer <gromer@google.com>
2021-09-02 17:28:37 -07:00
Jon Meow a03536a196 Add syntax for package and library (#792)
Doesn't add much logic, only takes advantage of parser structure for the ordering enforcement.

Note import_nonexistent tests should probably fail, but writing import tests needs a chain of functionality, and I figured I'd just start adding some to validate the syntax (not adding existent imports because that'd require multi-file structure).
2021-09-02 16:01:15 -07:00
Jon Meow 31b4f1e7ac Move ExecProgram to interpreter/ (#802)
Seems to make sense since it's the only interpreter/ dependency in syntax/ right now, so this feels like untangling deps.

Fixes #371
2021-09-01 15:22:10 -07:00
Jon Meow 5f8b231322 Switch Clause to Ptr (#800)
This is consistent with other lists, doesn't seem to benefit from being a Ptr.
2021-09-01 15:19:58 -07:00
Jon MeowandGeoff Romer 9f67c4b9a8 Run clang-format on parser.ypp and lexer.lpp (#738)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-31 12:41:08 -07:00
Jon Meow 973759f0ff Add parser.output to outs (#795)
Fixes #416
2021-08-31 10:46:42 -07:00
Jon Meow 49013ae1cc Reject invalid string literal whitespace on unescape (#793)
This is based on discussion on #732: that we should probably parse the invalid whitespace, then reject it as part of string validation, rather than having different parses. I worry the question of "how is this parsed" may lead to subtly unexpected results if we aren't consistent, so I'm switching the logic from the lexer to the unescape library (and also adjusting the list of rejected whitespace).
2021-08-30 15:22:03 -07:00
Jon Meow 32f5845e7b Refactor Interpreter/TypeChecker to classes to remove interpreter globals (#790)
Along with #789 this addresses most of #769 although global_arena is still a TODO (that's widespread and overlaps with other changes so I wanted to do it after these are in).
2021-08-30 15:21:40 -07:00
Jon Meow 00779b60a8 Reformat lexer/parser tokens. (#772)
Related to discussion on #738, but also trying to standardize handling of everything and make placement of token spellings (`AND "and"`) consistently in lexer.lpp.

I could have gone the other direction, removing the list of things in lexer.lpp, but this feels like it does more to use the compiler to detect skew between lexer.lpp and parser.ypp.
2021-08-30 14:59:30 -07:00
Jon MeowandGeoff Romer 36ed79dc25 Convert Statement to use Ptr (#788)
Note this makes a few cases where the Statement was optional explicit (Block, If, Sequence). I do add a few CHECKs around where statements were optional and assumed but unverified.

I switch TypeCheckStmt to not take an optional Statement because I think it makes the call sites clearer in behavior. It's also a smaller change than the converse, because taking an optional Statement means the returned statement would also need to be optional. Arguably a wrapper for optional statements could be added, but this still seems cleaner to me, and there aren't that many cases of an optional statement.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-30 14:41:14 -07:00
Jon Meow ed2d171703 Configure reentrant bison/flex (#789)
Make the parser reentrant, working towards #769 and the ability to load other packages cleanly.
2021-08-30 14:23:16 -07:00
Jon Meow fd89bcb4aa Convert Pattern and Expression to Ptr (#787)
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding.

That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution).

I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution).

I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
2021-08-27 09:16:20 -07:00
Jon Meow e93a361032 Use Ptr for Member (#786) 2021-08-26 16:32:22 -07:00
Jon Meow 3ec550a85f Replace int lines with file-associated SourceLocations (#779)
Most interesting changes should be ast/source_location.h and syntax/parse_and_lex_context.h
2021-08-26 10:07:16 -07:00
Jon Meow 5b18f24db0 Switch allocation of FunctionDefinition to use Ptr (#785) 2021-08-26 09:33:29 -07:00
Jon Meow d7b2c9b580 Remove uses of rules_cc (#776)
Per https://github.com/bazelbuild/rules_cc this still isn't necessary. There's no build-time enforcement, so usage is inconsistent/incorrect. Rather than letting this linger, remove it pending Bazel tooling enforcing it.
2021-08-24 08:55:17 -07:00
Jon MeowandGeoff Romer 0619b4ce6a Provide a BisonWrap for default construction (#761)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-19 12:31:25 -07:00
Jon Meow 367f9e4e94 Rename struct to class per #651 (#765) 2021-08-19 12:15:20 -07:00
Jon Meow 925c60b669 Change Declaration passing to use Ptr (#758) 2021-08-19 11:33:32 -07:00
Jon MeowandGeoff Romer c6ebe0db67 Add Ptr and RawNew for migration (#751)
Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-17 13:37:29 -07:00
Jon Meow 1eeaa208f2 Move paren_contents under ast and restrict syntax BUILD visibility (#756)
Trying to straighten out the BUILD graph; the dependency of ast pieces on paren_contents makes me think this is the right direction.
2021-08-17 12:52:53 -07:00
Jon Meow 9a9ccd8b7d Exclude tab in string literals (#734)
Noticed this while working on #732, seemed easiest to split changes though.
2021-08-12 09:53:04 -07:00