mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:31:15 +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:
@@ -38,8 +38,12 @@ auto HandleStatement(Context& context) -> void {
|
||||
context.PushState(State::StatementReturn);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Returned: {
|
||||
context.PushState(State::VarAsReturned);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Var: {
|
||||
context.PushState(State::VarAsSemicolon);
|
||||
context.PushState(State::VarAsDecl);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::While: {
|
||||
@@ -179,8 +183,15 @@ auto HandleStatementReturn(Context& context) -> void {
|
||||
context.PushState(state);
|
||||
|
||||
context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
|
||||
if (!context.PositionIs(Lex::TokenKind::Semi)) {
|
||||
|
||||
if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
|
||||
// `return var;`
|
||||
context.AddLeafNode(NodeKind::ReturnVarSpecifier, *var_token);
|
||||
} else if (!context.PositionIs(Lex::TokenKind::Semi)) {
|
||||
// `return <expression>;`
|
||||
context.PushState(State::Expr);
|
||||
} else {
|
||||
// `return;`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user