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
@@ -28,6 +28,7 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
// Allocate a node of the appropriate kind, linked to the name for error
// locations.
// TODO: Each of these cases should create a `BindName` node.
switch (auto context_parse_node_kind = context.parse_tree().node_kind(
context.node_stack().PeekParseNode())) {
case Parse::NodeKind::VariableIntroducer:
@@ -38,6 +39,16 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
context.AddNodeAndPush(parse_node, SemIR::Node::Parameter::Make(
name_node, cast_type_id, name_id));
break;
case Parse::NodeKind::LetIntroducer:
// Create the node, but don't add it to a block until after we've formed
// its initializer.
// TODO: For general pattern parsing, we'll need to create a block to hold
// the `let` pattern before we see the initializer.
context.node_stack().Push(
parse_node,
context.semantics_ir().AddNodeInNoBlock(SemIR::Node::BindName::Make(
name_node, cast_type_id, name_id, SemIR::NodeId::Invalid)));
break;
default:
CARBON_FATAL() << "Found a pattern binding in unexpected context "
<< context_parse_node_kind;