mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 14:21:09 +01:00
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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user