diff --git a/toolchain/docs/parse.md b/toolchain/docs/parse.md index 821cb3577387..c6dd7ad31a1a 100644 --- a/toolchain/docs/parse.md +++ b/toolchain/docs/parse.md @@ -462,9 +462,9 @@ switches to the expression-statement state. Depending on the introducer, different actions can be taken. The most common case is to: -- Call `context.PushState(State::___);` to mark the beginning of the statement - or declaration and indicate the state that will handle the tokens after the - introducer. +- Call `context.PushState(StateKind::___);` to mark the beginning of the + statement or declaration and indicate the state that will handle the tokens + after the introducer. - Call `context.AddLeafNode(NodeKind::___, context.Consume());` to output a bracketing node for this introducer. @@ -513,8 +513,8 @@ declaration or statement. - **Step 1**: Save the current value of `context.tree().size()`. This could be accomplished by calling `context.PushState()`, which saves that value in the - `subtree_start` field of `Context::StateStackEntry`; or by constructing a - `Context::StateStackEntry` value directly, as is done in + `subtree_start` field of `Context::State`; or by constructing a + `Context::State` value directly, as is done in [parse/handle_decl_scope_loop.cpp](/toolchain/parse/handle_decl_scope_loop.cpp). This marks the position of the placeholder node we are going to replace, as well as the beginning of the subtree we are eventually going to emit for @@ -701,10 +701,10 @@ This is handled by the `ExpectAsOrTypeExpression` code from if (context.PositionIs(Lex::TokenKind::As)) { // as ... context.AddLeafNode(NodeKind::DefaultSelfImplAs, context.Consume()); - context.PushState(State::Expr); + context.PushState(StateKind::Expr); } else { // as ... - context.PushState(State::ImplBeforeAs); + context.PushState(StateKind::ImplBeforeAs); context.PushStateForExpr(PrecedenceGroup::ForImplAs()); } ``` @@ -716,7 +716,7 @@ auto state = context.PopState(); if (auto as = context.ConsumeIf(Lex::TokenKind::As)) { context.AddNode(NodeKind::TypeImplAs, *as, state.subtree_start, state.has_error); - context.PushState(State::Expr); + context.PushState(StateKind::Expr); } else { if (!state.has_error) { CARBON_DIAGNOSTIC(ImplExpectedAs, Error, @@ -728,9 +728,9 @@ if (auto as = context.ConsumeIf(Lex::TokenKind::As)) { ``` Note (1) that the `state.subtree_start` value comes from the -`context.PushState(State::ImplBeforeAs);` before parsing the type expression, -and that is how that type expression ends up as the child of the created -`TypeImplAs` node. Unlike +`context.PushState(StateKind::ImplBeforeAs);` before parsing the type +expression, and that is how that type expression ends up as the child of the +created `TypeImplAs` node. Unlike [the previous case 1](#case-1-introducer-to-optional-clause-is-used-as-parent-node), though, the parent node uses the token after the optional expression, rather than an introducer token for the optional clause. @@ -784,10 +784,10 @@ This is handled by the `ExpectAsOrTypeExpression` code from if (context.PositionIs(Lex::TokenKind::As)) { // as ... context.AddLeafNode(NodeKind::ImplAs, context.Consume()); - context.PushState(State::Expr); + context.PushState(StateKind::Expr); } else { // as ... - context.PushState(State::ImplBeforeAs); + context.PushState(StateKind::ImplBeforeAs); context.PushStateForExpr(PrecedenceGroup::ForImplAs()); } ``` diff --git a/toolchain/parse/context.cpp b/toolchain/parse/context.cpp index 27b679a7e0a0..56dabbfede38 100644 --- a/toolchain/parse/context.cpp +++ b/toolchain/parse/context.cpp @@ -68,8 +68,8 @@ auto Context::ConsumeAndAddOpenParen(Lex::TokenIndex default_token, } auto Context::ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, - StateStackEntry state, - NodeKind close_kind) -> void { + State state, NodeKind close_kind) + -> void { Lex::TokenKind open_token_kind = tokens().GetKind(expected_open); if (!open_token_kind.is_opening_symbol()) { @@ -346,8 +346,7 @@ auto Context::ConsumeListToken(NodeKind comma_kind, Lex::TokenKind close_kind, } } -auto Context::AddNodeExpectingDeclSemi(StateStackEntry state, - NodeKind node_kind, +auto Context::AddNodeExpectingDeclSemi(State state, NodeKind node_kind, Lex::TokenKind decl_kind, bool is_def_allowed) -> void { // TODO: This could better handle things like: @@ -373,7 +372,7 @@ auto Context::AddNodeExpectingDeclSemi(StateStackEntry state, } } -auto Context::RecoverFromDeclError(StateStackEntry state, NodeKind node_kind, +auto Context::RecoverFromDeclError(State state, NodeKind node_kind, bool skip_past_likely_end) -> void { auto token = state.token; if (skip_past_likely_end) { diff --git a/toolchain/parse/context.h b/toolchain/parse/context.h index afed1df5ec0f..fd18321d24a4 100644 --- a/toolchain/parse/context.h +++ b/toolchain/parse/context.h @@ -75,8 +75,7 @@ class Context { }; // Used to track state on state_stack_. - // TODO: Rename to `State`. - struct StateStackEntry : public Printable { + struct State : public Printable { // Prints state information for verbose output. auto Print(llvm::raw_ostream& output) const -> void { output << kind << " @" << token << " subtree_start=" << subtree_start @@ -111,7 +110,7 @@ class Context { int32_t subtree_start; }; - // We expect StateStackEntry to fit into 12 bytes: + // We expect State to fit into 12 bytes: // state = 1 byte // has_error and in_var_pattern = 1 byte // ambient_precedence = 1 byte @@ -120,8 +119,7 @@ class Context { // subtree_start = 4 bytes // If it becomes bigger, it'd be worth examining better packing; it should be // feasible to pack the 1-byte entries more tightly. - static_assert(sizeof(StateStackEntry) == 12, - "StateStackEntry has unexpected size!"); + static_assert(sizeof(State) == 12, "State has unexpected size!"); explicit Context(Tree* tree, Lex::TokenizedBuffer* tokens, Diagnostics::Consumer* consumer, @@ -192,9 +190,8 @@ class Context { // Creates a parse node of the specified close kind. If `expected_open` is not // an opening symbol, the parse node will be associated with `state.token`, // no input will be consumed, and no diagnostic will be emitted. - auto ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, - StateStackEntry state, NodeKind close_kind) - -> void; + auto ConsumeAndAddCloseSymbol(Lex::TokenIndex expected_open, State state, + NodeKind close_kind) -> void; // Composes `ConsumeIf` and `AddLeafNode`, returning false when ConsumeIf // fails. @@ -281,7 +278,7 @@ class Context { } // Pops the state and keeps the value for inspection. - auto PopState() -> StateStackEntry { + auto PopState() -> State { auto back = state_stack_.pop_back_val(); CARBON_VLOG("Pop {0}: {1}\n", state_stack_.size(), back); return back; @@ -331,7 +328,7 @@ class Context { } // Pushes a constructed state onto the stack. - auto PushState(StateStackEntry state) -> void { + auto PushState(State state) -> void { CARBON_VLOG("Push {0}: {1}\n", state_stack_.size(), state); state_stack_.push_back(state); CARBON_CHECK(state_stack_.size() < (1 << 20), @@ -339,9 +336,9 @@ class Context { } // Pushes a constructed state onto the stack, with a different kind. - auto PushState(StateStackEntry state_entry, StateKind kind) -> void { - state_entry.kind = kind; - PushState(state_entry); + auto PushState(State state, StateKind kind) -> void { + state.kind = kind; + PushState(state); } // Propagates an error up the state stack, to the parent state. @@ -350,7 +347,7 @@ class Context { // Adds a node for a declaration's semicolon. Includes error recovery when the // token is not a semicolon, using `decl_kind` and `is_def_allowed` to inform // diagnostics. - auto AddNodeExpectingDeclSemi(StateStackEntry state, NodeKind node_kind, + auto AddNodeExpectingDeclSemi(State state, NodeKind node_kind, Lex::TokenKind decl_kind, bool is_def_allowed) -> void; @@ -365,7 +362,7 @@ class Context { // definition has started (although one could be present). Recover to a // semicolon when it makes sense as a possible end, otherwise use the // introducer token for the error. - auto RecoverFromDeclError(StateStackEntry state, NodeKind node_kind, + auto RecoverFromDeclError(State state, NodeKind node_kind, bool skip_past_likely_end) -> void; // Handles parsing of the library name. Returns the name's ID on success, @@ -414,11 +411,9 @@ class Context { auto position() -> Lex::TokenIterator& { return position_; } auto position() const -> Lex::TokenIterator { return position_; } - auto state_stack() -> llvm::SmallVector& { - return state_stack_; - } + auto state_stack() -> llvm::SmallVector& { return state_stack_; } - auto state_stack() const -> const llvm::SmallVector& { + auto state_stack() const -> const llvm::SmallVector& { return state_stack_; } @@ -453,7 +448,7 @@ class Context { // The FileEnd token. Lex::TokenIterator end_; - llvm::SmallVector state_stack_; + llvm::SmallVector state_stack_; // The deferred definition indexes of functions whose definitions have begun // but not yet finished. diff --git a/toolchain/parse/handle_brace_expr.cpp b/toolchain/parse/handle_brace_expr.cpp index 0fcc2307b2f9..205b74ca893a 100644 --- a/toolchain/parse/handle_brace_expr.cpp +++ b/toolchain/parse/handle_brace_expr.cpp @@ -20,8 +20,7 @@ auto HandleBraceExpr(Context& context) -> void { } // Prints a diagnostic for brace expression syntax errors. -static auto HandleBraceExprParamError(Context& context, - Context::StateStackEntry state, +static auto HandleBraceExprParamError(Context& context, Context::State state, StateKind param_finish_state_kind) -> void { Diagnostics::IntAsSelect mode(0); diff --git a/toolchain/parse/handle_decl_name_and_params.cpp b/toolchain/parse/handle_decl_name_and_params.cpp index 824fb52d2f91..6547d6a0c60e 100644 --- a/toolchain/parse/handle_decl_name_and_params.cpp +++ b/toolchain/parse/handle_decl_name_and_params.cpp @@ -10,7 +10,7 @@ namespace Carbon::Parse { // Adds a leaf node for the name, and updates the state stack for parameter // handling. -static auto HandleName(Context& context, Context::StateStackEntry state, +static auto HandleName(Context& context, Context::State state, Lex::TokenIndex name_token, NodeKind not_before_params_kind, NodeKind not_before_params_qualifier_kind, diff --git a/toolchain/parse/handle_decl_scope_loop.cpp b/toolchain/parse/handle_decl_scope_loop.cpp index d36e0e941a27..cced086f2230 100644 --- a/toolchain/parse/handle_decl_scope_loop.cpp +++ b/toolchain/parse/handle_decl_scope_loop.cpp @@ -32,7 +32,7 @@ static auto HandleUnrecognizedDecl(Context& context, int32_t subtree_start) // Replaces the introducer placeholder node, and pushes the introducer state for // processing. -static auto ApplyIntroducer(Context& context, Context::StateStackEntry state, +static auto ApplyIntroducer(Context& context, Context::State state, NodeKind introducer_kind, StateKind next_state_kind) -> void { context.ReplacePlaceholderNode(state.subtree_start, introducer_kind, @@ -118,7 +118,7 @@ static constexpr auto DeclIntroducers = [] { // Returns true if the current position is a declaration. If we see a // declaration introducer keyword token, replace the placeholder node and switch // to a state to parse the rest of the declaration. -static auto TryHandleAsDecl(Context& context, Context::StateStackEntry state, +static auto TryHandleAsDecl(Context& context, Context::State state, bool saw_modifier) -> bool { const auto& info = DeclIntroducers[context.PositionKind().AsInt()]; diff --git a/toolchain/parse/handle_expr.cpp b/toolchain/parse/handle_expr.cpp index 9360c4558534..656e345021fc 100644 --- a/toolchain/parse/handle_expr.cpp +++ b/toolchain/parse/handle_expr.cpp @@ -374,8 +374,7 @@ auto HandleExprLoop(Context& context) -> void { } // Adds the operator node and returns the main expression loop. -static auto HandleExprLoopForOperator(Context& context, - Context::StateStackEntry state, +static auto HandleExprLoopForOperator(Context& context, Context::State state, NodeKind node_kind) -> void { context.AddNode(node_kind, state.token, state.has_error); state.has_error = false; diff --git a/toolchain/parse/handle_import_and_package.cpp b/toolchain/parse/handle_import_and_package.cpp index de1f45bb0321..a97c895b3114 100644 --- a/toolchain/parse/handle_import_and_package.cpp +++ b/toolchain/parse/handle_import_and_package.cpp @@ -13,7 +13,7 @@ namespace Carbon::Parse { // Provides common error exiting logic that skips to the semi, if present. -static auto OnParseError(Context& context, Context::StateStackEntry state, +static auto OnParseError(Context& context, Context::State state, NodeKind declaration) -> void { context.AddNode(declaration, context.SkipPastLikelyEnd(state.token), /*has_error=*/true); @@ -23,7 +23,7 @@ static auto OnParseError(Context& context, Context::StateStackEntry state, // the given declaration. // TODO: Restructure how we handle packaging declarations to avoid the need to // do this. -static auto HasModifier(Context& context, Context::StateStackEntry state, +static auto HasModifier(Context& context, Context::State state, Lex::TokenKind modifier) -> bool { for (Lex::TokenIterator it(state.token); it != context.position(); ++it) { if (context.tokens().GetKind(*it) == modifier) { @@ -35,7 +35,7 @@ static auto HasModifier(Context& context, Context::StateStackEntry state, // Handles everything after the declaration's introducer. template -static auto HandleDeclContent(Context& context, Context::StateStackEntry state, +static auto HandleDeclContent(Context& context, Context::State state, bool is_export, bool is_impl, llvm::function_refvoid> on_parse_error) -> void { @@ -163,8 +163,8 @@ static auto VerifyInImports(Context& context, Lex::TokenIndex intro_token) } // Diagnoses if `export` is used in an `impl` file. -static auto RestrictExportToApi(Context& context, - Context::StateStackEntry& state) -> void { +static auto RestrictExportToApi(Context& context, Context::State& state) + -> void { // Error for both Main//default and every implementation file. auto packaging = context.tree().packaging_decl(); if (!packaging || packaging->is_impl) { diff --git a/toolchain/parse/handle_paren_expr.cpp b/toolchain/parse/handle_paren_expr.cpp index ad3472964f90..dfa5f8e6e196 100644 --- a/toolchain/parse/handle_paren_expr.cpp +++ b/toolchain/parse/handle_paren_expr.cpp @@ -19,8 +19,8 @@ auto HandleOnlyParenExpr(Context& context) -> void { context.PushState(StateKind::Expr); } -static auto FinishParenExpr(Context& context, - const Context::StateStackEntry& state) -> void { +static auto FinishParenExpr(Context& context, const Context::State& state) + -> void { context.AddNode(NodeKind::ParenExpr, context.Consume(), state.has_error); } diff --git a/toolchain/parse/state.def b/toolchain/parse/state.def index 832a6739347e..c494938097e8 100644 --- a/toolchain/parse/state.def +++ b/toolchain/parse/state.def @@ -54,7 +54,7 @@ CARBON_PARSE_STATE_VARIANT(Kind, Variant1) \ CARBON_PARSE_STATE_VARIANTS3(Kind, Variant2, Variant3, Variant4) -// Used as a default for StateStackEntry initialization in some cases. Should +// Used as a default for State initialization in some cases. Should // not be put on the state stack. CARBON_PARSE_STATE(Invalid)