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:
Richard Smith authored and GitHub committed 2023-11-10 19:52:30 +00:00
1 parent 5020fdb3be
commit afd6d85610
34 files changed
+1077 -146

No files matched your search

+14 -3
View File
@@ -4,6 +4,7 @@
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/check/return.h"
#include "toolchain/sem_ir/inst.h"
namespace Carbon::Check {
@@ -62,6 +63,7 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
// error locations.
switch (auto context_parse_node_kind = context.parse_tree().node_kind(
context.node_stack().PeekParseNode())) {
case Parse::NodeKind::ReturnedSpecifier:
case Parse::NodeKind::VariableIntroducer: {
// A `var` declaration at class scope introduces a field.
auto enclosing_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
@@ -78,7 +80,12 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
}
SemIR::InstId value_id = SemIR::InstId::Invalid;
SemIR::TypeId value_type_id = cast_type_id;
if (enclosing_class_decl) {
if (context_parse_node_kind == Parse::NodeKind::ReturnedSpecifier) {
CARBON_CHECK(!enclosing_class_decl) << "`returned var` at class scope";
value_id =
CheckReturnedVar(context, context.node_stack().PeekParseNode(),
name_node, name_id, type_node, cast_type_id);
} else if (enclosing_class_decl) {
auto& class_info =
context.classes().Get(enclosing_class_decl->class_id);
auto field_type_inst_id = context.AddInst(SemIR::UnboundFieldType{
@@ -98,9 +105,13 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
value_id = context.AddInst(
SemIR::VarStorage{name_node, value_type_id, name_id});
}
context.AddInstAndPush(
parse_node,
auto bind_id = context.AddInst(
SemIR::BindName{name_node, value_type_id, name_id, value_id});
context.node_stack().Push(parse_node, bind_id);
if (context_parse_node_kind == Parse::NodeKind::ReturnedSpecifier) {
RegisterReturnedVar(context, bind_id);
}
break;
}