diff --git a/toolchain/check/handle_literal.cpp b/toolchain/check/handle_literal.cpp index 5309d37bc2b0..43dc76564152 100644 --- a/toolchain/check/handle_literal.cpp +++ b/toolchain/check/handle_literal.cpp @@ -100,4 +100,9 @@ auto HandleTypeTypeLiteral(Context& context, return true; } +auto HandleAutoTypeLiteral(Context& context, + Parse::AutoTypeLiteralId parse_node) -> bool { + return context.TODO(parse_node, "HandleAutoTypeLiteral"); +} + } // namespace Carbon::Check diff --git a/toolchain/parse/handle_expr.cpp b/toolchain/parse/handle_expr.cpp index 81d72a697de5..9680a51d4fff 100644 --- a/toolchain/parse/handle_expr.cpp +++ b/toolchain/parse/handle_expr.cpp @@ -133,6 +133,11 @@ auto HandleExprInPostfix(Context& context) -> void { context.PushState(state); break; } + case Lex::TokenKind::Auto: { + context.AddLeafNode(NodeKind::AutoTypeLiteral, context.Consume()); + context.PushState(state); + break; + } case Lex::TokenKind::OpenCurlyBrace: { context.PushState(state); context.PushState(State::BraceExpr); diff --git a/toolchain/parse/node_kind.def b/toolchain/parse/node_kind.def index ea7690bae333..7080b551d3ae 100644 --- a/toolchain/parse/node_kind.def +++ b/toolchain/parse/node_kind.def @@ -488,6 +488,7 @@ CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(UnsignedIntTypeLiteral, CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatTypeLiteral, FloatTypeLiteral) CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral, StringTypeLiteral) CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, Type) +CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(AutoTypeLiteral, Auto) // A prefix operator, such as `not`: // _external_: expression diff --git a/toolchain/parse/testdata/auto/let.carbon b/toolchain/parse/testdata/auto/let.carbon new file mode 100644 index 000000000000..b20662bde226 --- /dev/null +++ b/toolchain/parse/testdata/auto/let.carbon @@ -0,0 +1,20 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +let x: auto = 1; + +// CHECK:STDOUT: - filename: let.carbon +// CHECK:STDOUT: parse_tree: [ +// CHECK:STDOUT: {kind: 'FileStart', text: ''}, +// CHECK:STDOUT: {kind: 'LetIntroducer', text: 'let'}, +// CHECK:STDOUT: {kind: 'IdentifierName', text: 'x'}, +// CHECK:STDOUT: {kind: 'AutoTypeLiteral', text: 'auto'}, +// CHECK:STDOUT: {kind: 'BindingPattern', text: ':', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'LetInitializer', text: '='}, +// CHECK:STDOUT: {kind: 'IntLiteral', text: '1'}, +// CHECK:STDOUT: {kind: 'LetDecl', text: ';', subtree_size: 7}, +// CHECK:STDOUT: {kind: 'FileEnd', text: ''}, +// CHECK:STDOUT: ] diff --git a/toolchain/parse/testdata/auto/match_case.carbon b/toolchain/parse/testdata/auto/match_case.carbon new file mode 100644 index 000000000000..257b32dae5e1 --- /dev/null +++ b/toolchain/parse/testdata/auto/match_case.carbon @@ -0,0 +1,55 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +fn f() -> bool { + match (true) { + case x: auto if (x == true) => { return true; } + default => { return false; } + } +} + +// CHECK:STDOUT: - filename: match_case.carbon +// CHECK:STDOUT: parse_tree: [ +// CHECK:STDOUT: {kind: 'FileStart', text: ''}, +// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'}, +// CHECK:STDOUT: {kind: 'IdentifierName', text: 'f'}, +// CHECK:STDOUT: {kind: 'TuplePatternStart', text: '('}, +// CHECK:STDOUT: {kind: 'TuplePattern', text: ')', subtree_size: 2}, +// CHECK:STDOUT: {kind: 'BoolTypeLiteral', text: 'bool'}, +// CHECK:STDOUT: {kind: 'ReturnType', text: '->', subtree_size: 2}, +// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 7}, +// CHECK:STDOUT: {kind: 'MatchIntroducer', text: 'match'}, +// CHECK:STDOUT: {kind: 'MatchConditionStart', text: '('}, +// CHECK:STDOUT: {kind: 'BoolLiteralTrue', text: 'true'}, +// CHECK:STDOUT: {kind: 'MatchCondition', text: ')', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'MatchStatementStart', text: '{', subtree_size: 5}, +// CHECK:STDOUT: {kind: 'MatchCaseIntroducer', text: 'case'}, +// CHECK:STDOUT: {kind: 'IdentifierName', text: 'x'}, +// CHECK:STDOUT: {kind: 'AutoTypeLiteral', text: 'auto'}, +// CHECK:STDOUT: {kind: 'BindingPattern', text: ':', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'MatchCaseGuardIntroducer', text: 'if'}, +// CHECK:STDOUT: {kind: 'MatchCaseGuardStart', text: '('}, +// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'x'}, +// CHECK:STDOUT: {kind: 'BoolLiteralTrue', text: 'true'}, +// CHECK:STDOUT: {kind: 'InfixOperatorEqualEqual', text: '==', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'MatchCaseGuard', text: ')', subtree_size: 6}, +// CHECK:STDOUT: {kind: 'MatchCaseEqualGreater', text: '=>'}, +// CHECK:STDOUT: {kind: 'MatchCaseStart', text: '{', subtree_size: 12}, +// CHECK:STDOUT: {kind: 'ReturnStatementStart', text: 'return'}, +// CHECK:STDOUT: {kind: 'BoolLiteralTrue', text: 'true'}, +// CHECK:STDOUT: {kind: 'ReturnStatement', text: ';', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'MatchCase', text: '}', subtree_size: 16}, +// CHECK:STDOUT: {kind: 'MatchDefaultIntroducer', text: 'default'}, +// CHECK:STDOUT: {kind: 'MatchDefaultEqualGreater', text: '=>'}, +// CHECK:STDOUT: {kind: 'MatchDefaultStart', text: '{', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'ReturnStatementStart', text: 'return'}, +// CHECK:STDOUT: {kind: 'BoolLiteralFalse', text: 'false'}, +// CHECK:STDOUT: {kind: 'ReturnStatement', text: ';', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'MatchDefault', text: '}', subtree_size: 7}, +// CHECK:STDOUT: {kind: 'MatchStatement', text: '}', subtree_size: 29}, +// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 37}, +// CHECK:STDOUT: {kind: 'FileEnd', text: ''}, +// CHECK:STDOUT: ] diff --git a/toolchain/parse/testdata/auto/return.carbon b/toolchain/parse/testdata/auto/return.carbon new file mode 100644 index 000000000000..cc2128ce7619 --- /dev/null +++ b/toolchain/parse/testdata/auto/return.carbon @@ -0,0 +1,26 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +fn f() -> auto { + return true; +} + +// CHECK:STDOUT: - filename: return.carbon +// CHECK:STDOUT: parse_tree: [ +// CHECK:STDOUT: {kind: 'FileStart', text: ''}, +// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'}, +// CHECK:STDOUT: {kind: 'IdentifierName', text: 'f'}, +// CHECK:STDOUT: {kind: 'TuplePatternStart', text: '('}, +// CHECK:STDOUT: {kind: 'TuplePattern', text: ')', subtree_size: 2}, +// CHECK:STDOUT: {kind: 'AutoTypeLiteral', text: 'auto'}, +// CHECK:STDOUT: {kind: 'ReturnType', text: '->', subtree_size: 2}, +// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 7}, +// CHECK:STDOUT: {kind: 'ReturnStatementStart', text: 'return'}, +// CHECK:STDOUT: {kind: 'BoolLiteralTrue', text: 'true'}, +// CHECK:STDOUT: {kind: 'ReturnStatement', text: ';', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 11}, +// CHECK:STDOUT: {kind: 'FileEnd', text: ''}, +// CHECK:STDOUT: ] diff --git a/toolchain/parse/testdata/auto/var.carbon b/toolchain/parse/testdata/auto/var.carbon new file mode 100644 index 000000000000..b4c7fde31c30 --- /dev/null +++ b/toolchain/parse/testdata/auto/var.carbon @@ -0,0 +1,20 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +var y: auto = false; + +// CHECK:STDOUT: - filename: var.carbon +// CHECK:STDOUT: parse_tree: [ +// CHECK:STDOUT: {kind: 'FileStart', text: ''}, +// CHECK:STDOUT: {kind: 'VariableIntroducer', text: 'var'}, +// CHECK:STDOUT: {kind: 'IdentifierName', text: 'y'}, +// CHECK:STDOUT: {kind: 'AutoTypeLiteral', text: 'auto'}, +// CHECK:STDOUT: {kind: 'BindingPattern', text: ':', subtree_size: 3}, +// CHECK:STDOUT: {kind: 'BoolLiteralFalse', text: 'false'}, +// CHECK:STDOUT: {kind: 'VariableInitializer', text: '=', subtree_size: 2}, +// CHECK:STDOUT: {kind: 'VariableDecl', text: ';', subtree_size: 7}, +// CHECK:STDOUT: {kind: 'FileEnd', text: ''}, +// CHECK:STDOUT: ]