Support for let declarations. (#3257)

A `let` declaration is represented by a `bind_name` node in SemIR:

```carbon-semir
  %b: i32 = bind_name "b", %a
```

Because `Check` encounters the pattern before it sees the value, we
first create the `bind_name` node with an unset value and don't add it
to the block. Then, once we've seen and converted the initializer, we
update the `bind_name` to have the value and add it to the current
block, after the initializer code.
This commit is contained in:
Richard Smith
2023-10-06 00:06:21 +00:00
committed by GitHub
parent c7e6238fa8
commit 74d52738ff
28 changed files with 579 additions and 13 deletions
+11 -1
View File
@@ -6,7 +6,7 @@
namespace Carbon::Parse {
// Handles PatternAs(DeducedParameter|FunctionParameter|Variable).
// Handles PatternAs(DeducedParameter|FunctionParameter|Variable|Let).
static auto HandlePattern(Context& context, Context::PatternKind pattern_kind)
-> void {
auto state = context.PopState();
@@ -36,6 +36,12 @@ static auto HandlePattern(Context& context, Context::PatternKind pattern_kind)
context.emitter().Emit(*context.position(), ExpectedVariableName);
break;
}
case Context::PatternKind::Let: {
CARBON_DIAGNOSTIC(ExpectedLetBindingName, Error,
"Expected pattern in `let` declaration.");
context.emitter().Emit(*context.position(), ExpectedLetBindingName);
break;
}
}
// Add a placeholder for the type.
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
@@ -90,6 +96,10 @@ auto HandlePatternAsVariable(Context& context) -> void {
HandlePattern(context, Context::PatternKind::Variable);
}
auto HandlePatternAsLet(Context& context) -> void {
HandlePattern(context, Context::PatternKind::Let);
}
// Handles PatternFinishAs(Generic|Regular).
static auto HandlePatternFinish(Context& context, NodeKind node_kind) -> void {
auto state = context.PopState();