Commit Graph
1420 Commits
Author SHA1 Message Date
dd2f7d732c Design updates for #2015 numeric type literal syntax (#2410)
Edit numeric literal design for the literal type proposal and add reference where #2015 was mentioned.

Closes #2159 

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-12-09 11:01:21 -08:00
Jon Ross-Perkins d96ede7144 Remove equality on enum types (#2452)
A quick check suggests this isn't necessary -- probably because the implicit enum cast is used for comparisons. I think this adds a lot to the boilerplate feel of these types, so if we can remove it there's a lot less sharing to do.
2022-12-09 09:36:35 -08:00
Jon Ross-Perkins d42d864e82 Make TokenKind's API closer to toolchain's typical API setup. (#2456)
This is somewhat based on the name vs Name difference, but I figured I'd split it out and just sweep up the API on the whole while looking at a different approach to #2453
2022-12-08 15:31:04 -08:00
Richard Smith 0ef7fa3a1d Support p->member, rewriting it to (*p).member in the parser. (#2455)
* Support `p->member`, rewriting it to `(*p).member` in the parser.

This behavior is as described in
https://github.com/carbon-language/carbon-lang/tree/trunk/docs/design#pointer-types:

> `p->m` is syntactic sugar for `(*p).m`.
2022-12-08 13:42:36 -08:00
Jon Ross-Perkins f10531bcb3 Use macros to enforce handling parse nodes in SemanticsIR. (#2448)
This creates a bit of extra cost in adding parse nodes in that a TODO must be added to semantics, but I think the link is going to last this way long-term. In semantics, it reduces the boilerplate of the main for loop and makes it more obvious what's missing, leaving stub functions to be filled in.
2022-12-08 08:59:28 -08:00
Jon Ross-Perkins 34b5349df9 Change result_id to use is_valid instead of an optional. (#2449)
Incrementally shrinks size, but should also make the errors a bit clearer with better context.
2022-12-07 16:30:23 -08:00
josh11b ee998c99ff Clean up some small mistakes (#2451) 2022-12-07 15:12:08 -08:00
josh11b 9165b084f4 Can't override virtual methods in an adapter (#2450)
This is just a consequence of casting to an adapter type doesn't change the data, including the vtable pointer.
2022-12-07 15:11:31 -08:00
Jon Ross-Perkins 30960d11a9 Start adding var decls to semantics. (#2447)
This is just the declaration, without initialization. Partly breaking it out because I'm changing the placeholder builtin types.

Might also need to separate out storage of the var from the name bind.
2022-12-07 15:10:42 -08:00
Jon Ross-Perkins 04f0288cd2 Bracket the tokenized buffer output. (#2446)
This makes TokenizedBuffer more consistent with ParseTree and SemanticsIR, which also wrap with [] to produce a sequence value.

It also makes it possible in driver.cpp to just prefix the line with a name, so it ends up with:

var_name: [
  (content)
]
Noticed this due to bracketing comments on #2443 and trying to think of better answers. With this, we can also say that the [] bracket a variable.
2022-12-07 11:22:33 -08:00
Chandler Carruth 5f85822caa Add -march=... to our compile flags. (#2439)
This makes more modern CPU instructions available. I noticed that we weren't already doing this when working on another bit of code where its actually relevant. This doesn't make a big difference for any of the few benchmarks we have at the moment, but it seems like a good idea.

Modern Clang/LLVM support this exact spelling both on x86 and ARM CPUs, so its surprisingly portable. I've tested it on my ARM mac just in case.

I've picked specific arch flags here because using detection with `native` seems to run into issues in the GitHub actions. Sadly, the x86 macOS runners force a somewhat minimal set of features for x86, but it will still give us consistent results.
2022-12-07 09:50:30 -08:00
Jon Ross-Perkins 60eb06ce94 Move trace banners to driver and trace intermediate state. (#2443)
Printing intermediate state should be helpful to be able to examine the input when debugging later steps.

Intermediate state is hard to trace from the individual libraries since they don't know whether `dump` is going to print the state, so this moves some trace logic into the driver which is better equipped to make the decision.
2022-12-07 09:17:54 -08:00
josh11b 9c8fd6864e Implement rename me -> self (#2444)
Implements change proposed in #1382

Replaces #1624
2022-12-06 20:17:00 -08:00
josh11bandRichard Smith d1210c4a50 Rename me -> self (#1382)
Carbon has been using a `me: Self` or `addr me: Self*` deduced parameter to mark a function as a method as decided in #494 and implemented in #722 . This does not match existing languages, and so this proposal switches `me` to `self` to match Python, Rust, and Swift.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-12-06 15:58:48 -08:00
Adrien Leravat 0cb2c92c6c Explorer: support index-based fields for FieldPath and Members (#2417)
Features
* Support addressing positional members (i.e. tuple fields) using an index.
* Addresses a couple of `TODO`s relative to positional members

Changes:
* Add a new `IndexedValue` mirroring `NamedValue`
* Adds `FieldPath::index() -> int`
* Adds a `Member` variant for `IndexedValue`
2022-12-06 13:31:18 -08:00
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
Avi levy 94c207158e llvm-ar no such file or directory fix instruction (#2423)
Add link to llvm-ar error fix instruction.
2022-12-05 14:02:58 -08:00
Tobias Feldballe Petersen 47f28b361f Updated docker tagging to match correct ubuntu version (#2440)
The docker Readme seams to tag the built docker images with version 2004, but is built using version 2204. This PR simply starts tagging the images correctly to avoid confusion.
2022-12-05 08:42:25 -08:00
Jon Ross-Perkins 991dadcbed Move parse tree tests to files. (#2437)
I think it'd be helpful to examine what the parse tree looks like in failure cases.

Note, I'm not sure that the behavior of the recovery situations is correct; the test had been asserting that the parse tree should indicate it's error free. However, this means that the only signal the the driver that the input is invalid is that the diagnostic emitter was used. I think it may be important to have it return a non-zero error to prevent compile, or we can turn these into warnings but then "requiring a space" is wrong.

Either way, that's a concern I have with the pre-existing recovery behavior: I'm just trying to highlight it as I make this change, because now the test is really that nodes aren't tagged with has_error.
2022-12-05 08:30:53 -08:00
Chandler Carruth 814ae3d419 Fix some broken regexes for files in pre-commit configs. (#2438)
The result was we weren't actually running buildifier on files for
example, so this also runs it on a bunch of files to clean things up.
2022-12-05 08:16:06 -08:00
Adrien Leravat 054537270b README.md: add a section with past talks (#2434)
Suggesting to add a section with past conference talks, to include content that has been created around carbon. This feel to me like an accessible type of content to have in the main README for those wanting to dig a bit deeper without going into /docs.

This does not show the speaker name to keep it "Carbon/community" oriented, vs naming, but there's an argument both ways I think.
2022-12-02 17:41:20 -08:00
josh11bandRichard Smith 571b869f1c Constraints must use Self (#2376)
Require `impl as` constraints in an `interface` or `constraint` definition to mention `Self` implicitly or explicitly. Require `where` clauses to refer to `.Self` directly, or through a designator like `.Foo`.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-12-02 16:11:23 -08:00
Jon Ross-PerkinsandRichard Smith d50fef1736 Expand use of IndexBase (#2436)
Initially I'd added this to lexer, this includes parser and semantics. Also adds ComparableIndexBase to unify a few common cases where <> comparisons are supported.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-12-01 14:09:18 -08:00
Chandler CarruthandJon Ross-Perkins 94cf343b05 Update LLVM and switch to std::optional. (#2424)
LLVM's bazel build has changed a bit, so this updates the tree for that.

LLVM is also moving `llvm::Optional` to match the standard API, but it seemed simpler to just switch to `std::optional`.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-12-01 09:22:43 -08:00
Jon Ross-Perkins 16bbdbbdb8 Add vlog output to the parser. (#2435)
Might eventually want to change this further, but I'm just adding the quick framework for it.
2022-11-30 17:28:49 -08:00
Jon Ross-Perkins f9979e3cb1 Switch WORKSPACE from %s to .format (#2433)
Per discussion on #2424
2022-11-30 16:42:06 -08:00
Jon Ross-Perkins 8480a0bbd6 Fix a couple debug output issues (#2415)
Make `.run` targets include symbol information, and include token names on CHECK output in more places.
2022-11-30 10:24:09 -08:00
Jon Ross-Perkins 6bfd202f4e Finish bracketing of parse nodes. (#2430)
- Finishes remaining "todo" parse nodes.
- Improving error recovery for invalid designators and structs, so that the parse tree still looks similar to a valid parse tree.
- Call expressions now have the thing being called as a child (of the start) instead of a sibling.
- Use of Start is replacing use of End in several parse nodes, like structs and call expressions.
- Adjusting documentation of parse node structures in an attempt to make it more consistent and understandable.
- The current state for interfaces and if/else is mostly being documented, not altered.
2022-11-30 09:14:07 -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
Jon Ross-PerkinsandChandler Carruth 84deb62aef Work on ParseTree structure to use more bracketed structures. (#2416)
This works on multiple statements to make them better for the bracketing model. Stub nodes are added in more cases of invalid syntax, simply so that the semantics has reliably structured input. Comments in parse_node_kind.def now try to show the expected parse tree structure in postorder form.

This labels If, While, and For a little differently in parse nodes so that at the start of the postorder traversal, it'll already be available to semantics which structure is being processed. I need to do a little more with If in particular, but this felt like a reasonable stopping point.

While this makes significant parser changes, the changes to parser_state.def are minimal, mostly naming-related. The actual flow isn't substantively changed, just a couple minor names and the new As(If|While) state which allows distinguishing IfCondition and WhileCondition.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-11-28 15:02:59 -08:00
Kareem Ergawyandergawy a508390423 [parser] Start re-implementing interfaces using the stack parser. (#2412)
First change towards re-implementing interfaces using the new parser. I kept it small to make sure we are on the same page regarding stack states and how the parse tree should look like.

Co-authored-by: ergawy <kareem.ergawy@guardsquare.com>
2022-11-28 12:58:41 -08:00
Jon Ross-PerkinsandChandler Carruth 5cd05efc62 Introduce verification checks on subtree_size (#2414)
This switches the Verify method to walk postorder so that we can see how much subtree_size is really used, and shift towards removing it. It also starts calling Verify.

Also, I think I'd lost the reserve/size check during Parser refactoring, so I'm putting that back in as part of Verify.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-11-18 16:45:59 -08:00
Jon Ross-Perkins 97ed697386 For flyweights, shift from llvm::SmallVector to a type that enforces index types. (#2398)
The intent here is to reduce use of vanilla `int32_t` without a clear indicator of what it's referencing, and to more tightly link references with the underlying types they reference into.

The type name DataIndex doesn't feel great, but I was kind of floundering for a better name.
2022-11-18 14:07:01 -08:00
Jon Ross-Perkins d9ce1827c8 Adjust how invalid declarations pass errors. (#2413)
When there's no semicolon for an invalid EmptyDeclaration, rather than producing nothing, produce an EmptyDeclaration with the original location that led to the error.

Note this removes a direct edit (the only one) of the parse tree's error state. Elsewhere it's an indirection from adding an error node.
2022-11-18 08:02:07 -08:00
Jon Ross-Perkins 4c8fdf5124 Start drafting out semantic type checking. (#2406)
This is a first pass at what semantic type checking might look like. Types propagate along nodes, we use an InvalidType object when there's an error, and once there's an InvalidType we stop doing so much type checking.

This adds some RealLiteral handling in order to get type mismatches. I'm cautious about creating some real value for SemanticsIR (since the tokenized buffer version is a bit constrained), so I'm not doing that yet. But I will probably need to in order to maintain SemanticsIR having hermetic copies of its data, without a parse tree dependency.
2022-11-17 13:36:40 -08:00
c9a1e351ba Template generics (#2200)
Add template generics, with optional constraints but no [SFINAE](https://en.cppreference.com/w/cpp/language/sfinae), to Carbon. Template generics allows the compiler postpone type checking of expressions dependent on a template parameter until the function is called and the value of that parameter is known.

Example usage:

```carbon
fn Identity[template T:! Type](x: T) -> T {
  return x;
}
```

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-11-17 10:23:52 -08:00
Jon Ross-Perkins adac572430 Replace some reference members with pointer members. (#2408)
I'd noted this while rewriting the parser and, while I kept it there during conversion, I think switching is consistent with the higher-level desire and the use of references therein was just an oversight.

Discussed at:
https://discord.com/channels/655572317891461132/655578254970716160/1042551242813083840
2022-11-17 08:58:43 -08:00
Jon Ross-Perkins 37dd968709 Track the ParseTree::Node with SemanticsNode. (#2407)
Longer term, I think this is going to be important as part of associating errors with code after the particular node has been processed, even though it isn't used here.
2022-11-17 08:58:13 -08:00
Jon Ross-Perkins 242b191317 Add notes to comment on #access-requests as a notification substitute. (#2405)
I've been meaning to do this -- just a couple days ago I processed some that we'd missed for a few weeks...
2022-11-17 08:57:47 -08:00
Jon Ross-Perkins fbed1398fe Change ParameterList to use a Start that brackets params (#2403)
The code change is small here. I'm breaking this out because it has a lot of test churn, and semantics doesn't have significant logic around it yet.
2022-11-17 08:57:22 -08:00
Jon Ross-Perkins 5da32bd560 Rename to StructLiteralOrStructTypeLiteralStart (#2404)
As requested
2022-11-17 08:56:48 -08:00
Jon Ross-Perkins e45ab50e5b Switch testing to pre-installed versions of clang-14 (#2402)
We keep seeing fragility installing software, with both brew (e.g., the recent python issues) and apt.llvm.org (currently flaky).

At this point, images for both ubuntu and macos have versions of llvm-14 that seem to successfully compile:
https://github.com/carbon-language/carbon-lang/actions/runs/3474670746/jobs/5808098087

Although we may want to figure out a way to resume running llvm-15 so that we can see compatibility issues, this seems preferable for baseline testing in order to reduce maintenance churn.

In addition to the above changes, this also configures cancellation more precisely, and stops installing bazel/bazelisk (it should already be preinstalled).
2022-11-17 08:54:44 -08:00
Jon Ross-Perkins dc23f6305d Use Consume() more where possible. (#2397)
Just trying to standardize the use in a few spots; no behavioral change.
2022-11-15 22:45:47 -08:00
Jon Ross-Perkins c165ab6c2b Change StructLiteral and StructTypeLiteral parsing to use an ambiguous start. (#2396)
This is necessary in order to use a bracketing approach; parsing doesn't know the contained format until it parses the first element, which we don't want to do look-ahead for. I think the bracketing is higher value than knowing the format before adding the node.
2022-11-15 22:44:57 -08:00
Jon Ross-Perkins b914f46ec5 Change keyword statements to bracket arguments. (#2394)
This changes `return`, `break`, and `continue` to treat the keyword as the "start" and semicolon as the "parent", essentially bracketing the keyword.

Pragmatically this is focusing on making `return` work with only one ParseNodeKind: because `return` and `;` now bracket the expression, we can tightly determine whether the `return` has arguments without looking at subtree size. However, it's possible that `break` and `continue` may in the future take some kind of label as an argument, so the consistency seems beneficial there too.

Note this eliminates the StatementEnd ParseNodeKind, as it's obsolete with this change.
2022-11-15 22:42:54 -08:00
Jon Ross-Perkins ed654c1aa8 Clean up Parser stack construction (#2386)
Just some unnecessary functions.
2022-11-15 22:37:41 -08:00
Jon Ross-Perkins 310cf0d2f9 Refactor Pattern and FunctionParameter handling for Parser consistency (#2385)
While the Parser has similar divergent states, lists of Expressions tend to be handled more like this. I'm keeping the divergent start state in order to continue support of a distinct error, but I think this organization of FunctionParameter/FunctionParameterFinish will be less surprising.
2022-11-15 22:36:52 -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
Jon Ross-Perkins 3b4bb985fb Explicitly install python with --overwrite (#2393)
```
==> Pouring python@3.11--3.11.0.monterey.bottle.tar.gz
Error: The brew link step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/2to3-3.11
Target /usr/local/bin/2to3-3.11
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3-3.11'
```

Installing llvm with --overwrite didn't seem to pass it on dependencies, particularly python (https://github.com/carbon-language/carbon-lang/actions/runs/3464174572/jobs/5785378508). So this instead installs python separately, and then --overwrite works.

See https://github.com/carbon-language/carbon-lang/actions/runs/3464364789/jobs/5785802783 for the example passing run. The actions on this PR will probably still use the trunk version.
2022-11-14 14:33:03 -08:00
Jon Ross-Perkins e6de3ed495 Fix a message to say the tool instead of 'explorer' (#2395) 2022-11-14 14:24:57 -08:00