mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Simplify handling of VariableInitializer (#3515)
Also stop supporting `var` with initializer inside `for`. Resolves TODO in `handle_variable.cpp` --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
a6508fcf05
commit
927d633762
@@ -27,26 +27,16 @@ auto HandleReturnedModifier(Context& context,
|
||||
auto HandleVariableInitializer(Context& context,
|
||||
Parse::VariableInitializerId parse_node)
|
||||
-> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
SemIR::InstId init_id = context.node_stack().PopExpr();
|
||||
context.node_stack().Push(parse_node, init_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleVariableDecl(Context& context, Parse::VariableDeclId parse_node)
|
||||
-> bool {
|
||||
// Handle the optional initializer.
|
||||
auto init_id = SemIR::InstId::Invalid;
|
||||
Parse::NodeKind next_kind = context.node_stack().PeekParseNodeKind();
|
||||
if (next_kind == Parse::NodeKind::TuplePattern) {
|
||||
return context.TODO(parse_node, "tuple pattern in var");
|
||||
}
|
||||
// TODO: find a more robust way to determine if there was an initializer.
|
||||
bool has_init = next_kind != Parse::NodeKind::BindingPattern;
|
||||
if (has_init) {
|
||||
init_id = context.node_stack().PopExpr();
|
||||
context.node_stack()
|
||||
.PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
|
||||
}
|
||||
std::optional<SemIR::InstId> init_id =
|
||||
context.node_stack().PopIf<Parse::NodeKind::VariableInitializer>();
|
||||
|
||||
if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
|
||||
return context.TODO(parse_node, "tuple pattern in var");
|
||||
@@ -71,16 +61,16 @@ auto HandleVariableDecl(Context& context, Parse::VariableDeclId parse_node)
|
||||
.PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedModifier>();
|
||||
|
||||
// If there was an initializer, assign it to the storage.
|
||||
if (has_init) {
|
||||
if (init_id.has_value()) {
|
||||
if (context.GetCurrentScopeAs<SemIR::ClassDecl>()) {
|
||||
// TODO: In a class scope, we should instead save the initializer
|
||||
// somewhere so that we can use it as a default.
|
||||
context.TODO(parse_node, "Field initializer");
|
||||
} else {
|
||||
init_id = Initialize(context, parse_node, value_id, init_id);
|
||||
init_id = Initialize(context, parse_node, value_id, *init_id);
|
||||
// TODO: Consider using different instruction kinds for assignment versus
|
||||
// initialization.
|
||||
context.AddInst(SemIR::Assign{parse_node, value_id, init_id});
|
||||
context.AddInst(SemIR::Assign{parse_node, value_id, *init_id});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user