mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:44:41 +01:00
Support for returned var and return var. (#3374)
Implement toolchain support for `returned var` and `return var`.
- Modeled `returned` in the parse tree as a `ReturnedSpecifier`
appearing after the `VariableIntroducer`.
- Modeled `return var` in the parse tree as a `ReturnVarSpecifier`
appearing after the `ReturnStatementStart`.
- Factored out the implementation of `return` statement and `returned
var` handling in check into a new `return.{h,cpp}`. The parse nodes
themselves are still handled in `handle_*.cpp`. This allows easy code
reuse between `return` and `returned var`.
This commit is contained in:
@@ -8,6 +8,26 @@
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
auto HandleVariableIntroducer(Context& context, Parse::Node parse_node)
|
||||
-> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleReturnedSpecifier(Context& context, Parse::Node parse_node) -> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleVariableInitializer(Context& context, Parse::Node parse_node)
|
||||
-> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleVariableDecl(Context& context, Parse::Node parse_node) -> bool {
|
||||
// Handle the optional initializer.
|
||||
auto init_id = SemIR::InstId::Invalid;
|
||||
@@ -32,17 +52,21 @@ auto HandleVariableDecl(Context& context, Parse::Node parse_node) -> bool {
|
||||
value_id = bind_name->value_id;
|
||||
}
|
||||
|
||||
// Pop the `returned` specifier if present.
|
||||
context.node_stack()
|
||||
.PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ReturnedSpecifier>();
|
||||
|
||||
// If there was an initializer, assign it to the storage.
|
||||
if (has_init) {
|
||||
if (context.insts().Get(value_id).Is<SemIR::VarStorage>()) {
|
||||
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);
|
||||
// TODO: Consider using different instruction kinds for assignment versus
|
||||
// initialization.
|
||||
context.AddInst(SemIR::Assign{parse_node, value_id, init_id});
|
||||
} else {
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,18 +76,4 @@ auto HandleVariableDecl(Context& context, Parse::Node parse_node) -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleVariableIntroducer(Context& context, Parse::Node parse_node)
|
||||
-> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleVariableInitializer(Context& context, Parse::Node parse_node)
|
||||
-> bool {
|
||||
// No action, just a bracketing node.
|
||||
context.node_stack().Push(parse_node);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
|
||||
Reference in New Issue
Block a user