Switch ParseNodeKind to EnumBase (#2510)

This shouldn't have any behavior change, it's just using #2504
This commit is contained in:
Jon Ross-Perkins
2023-01-05 13:33:32 -08:00
committed by GitHub
parent 0af5c55f55
commit 97634a5e91
6 changed files with 128 additions and 211 deletions
+1 -14
View File
@@ -13,20 +13,7 @@ cc_library(
textual_hdrs = ["parse_node_kind.def"],
deps = [
"//common:check",
"//common:ostream",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "parse_node_kind_test",
size = "small",
srcs = ["parse_node_kind_test.cpp"],
deps = [
":parse_node_kind",
"//common:gtest_main",
"@com_google_googletest//:gtest",
"@llvm-project//llvm:Support",
"//toolchain/common:enum_base",
],
)
+9 -13
View File
@@ -5,17 +5,13 @@
#include "toolchain/parser/parse_node_kind.h"
#include "common/check.h"
#include "llvm/ADT/StringRef.h"
namespace Carbon {
auto ParseNodeKind::name() const -> llvm::StringRef {
static constexpr llvm::StringLiteral Names[] = {
#define CARBON_PARSE_NODE_KIND(Name) #Name,
CARBON_DEFINE_ENUM_CLASS_NAMES(ParseNodeKind) = {
#define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
#include "toolchain/parser/parse_node_kind.def"
};
return Names[static_cast<int>(kind_)];
}
};
auto ParseNodeKind::has_bracket() const -> bool {
static constexpr bool HasBracket[] = {
@@ -23,7 +19,7 @@ auto ParseNodeKind::has_bracket() const -> bool {
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(...) false,
#include "toolchain/parser/parse_node_kind.def"
};
return HasBracket[static_cast<int>(kind_)];
return HasBracket[AsInt()];
}
auto ParseNodeKind::bracket() const -> ParseNodeKind {
@@ -31,12 +27,12 @@ auto ParseNodeKind::bracket() const -> ParseNodeKind {
// child_count.
static constexpr ParseNodeKind Bracket[] = {
#define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName) \
ParseNodeKind::BracketName(),
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) ParseNodeKind::Name(),
ParseNodeKind::BracketName,
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) ParseNodeKind::Name,
#include "toolchain/parser/parse_node_kind.def"
};
auto bracket = Bracket[static_cast<int>(kind_)];
CARBON_CHECK(bracket != kind_) << *this;
auto bracket = Bracket[AsInt()];
CARBON_CHECK(bracket != *this) << *this;
return bracket;
}
@@ -46,7 +42,7 @@ auto ParseNodeKind::child_count() const -> int32_t {
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size) Size,
#include "toolchain/parser/parse_node_kind.def"
};
auto child_count = ChildCount[static_cast<int>(kind_)];
auto child_count = ChildCount[AsInt()];
CARBON_CHECK(child_count >= 0) << *this;
return child_count;
}
+12 -49
View File
@@ -5,58 +5,22 @@
#ifndef CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_
#define CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_
#include <cstdint>
#include <iterator>
#include "common/ostream.h"
#include "llvm/ADT/StringRef.h"
#include "toolchain/common/enum_base.h"
namespace Carbon {
CARBON_DEFINE_RAW_ENUM_CLASS(ParseNodeKind, uint8_t){
#define CARBON_PARSE_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
#include "toolchain/parser/parse_node_kind.def"
};
// A class wrapping an enumeration of the different kinds of nodes in the parse
// tree.
//
// Rather than using a raw enumerator for each distinct kind of node produced by
// the parser, we wrap the enumerator in a class to expose a more rich API
// including bidirectional mappings to string spellings of the different kinds
// and any relevant classification.
//
// Instances of this type should always be created using the `constexpr` static
// member functions. These instances are designed specifically to be usable in
// `case` labels of `switch` statements just like an enumerator would.
class ParseNodeKind {
// Note that this must be declared earlier in the class so that its type can
// be used, for example in the conversion operator.
enum class KindEnum : uint8_t {
#define CARBON_PARSE_NODE_KIND(Name) Name,
#include "toolchain/parser/parse_node_kind.def"
};
class ParseNodeKind : public CARBON_ENUM_BASE(ParseNodeKind) {
public:
// `clang-format` has a bug with spacing around `->` returns in macros. See
// https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
#define CARBON_PARSE_NODE_KIND(Name) \
static constexpr auto Name()->ParseNodeKind { \
return ParseNodeKind(KindEnum::Name); \
}
#define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECLARATION(Name)
#include "toolchain/parser/parse_node_kind.def"
// The default constructor is deleted because objects of this type should
// always be constructed using the above factory functions for each unique
// kind.
ParseNodeKind() = delete;
// Gets a friendly name for the token for logging or debugging.
[[nodiscard]] auto name() const -> llvm::StringRef;
// Enable conversion to our private enum, including in a `constexpr` context,
// to enable usage in `switch` and `case`. The enum remains private and
// nothing else should be using this function.
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator KindEnum() const { return kind_; }
void Print(llvm::raw_ostream& out) const { out << name(); }
// Returns true if the node is bracketed; otherwise, child_count is used.
auto has_bracket() const -> bool;
@@ -67,13 +31,12 @@ class ParseNodeKind {
// Returns the number of children that the node must have, often 0. Requires
// that has_bracket is false.
auto child_count() const -> int32_t;
private:
constexpr explicit ParseNodeKind(KindEnum k) : kind_(k) {}
KindEnum kind_;
};
#define CARBON_PARSE_NODE_KIND(Name) \
CARBON_ENUM_CONSTANT_DEFINITION(ParseNodeKind, Name)
#include "toolchain/parser/parse_node_kind.def"
// We expect the parse node kind to fit compactly into 8 bits.
static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
-25
View File
@@ -1,25 +0,0 @@
// 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
#include "toolchain/parser/parse_node_kind.h"
#include <gtest/gtest.h>
#include <cstring>
#include "llvm/ADT/StringRef.h"
namespace Carbon::Testing {
namespace {
// Not much to test here, so just verify that the API compiles and returns the
// data in the `.def` file.
#define CARBON_PARSE_NODE_KIND(Name) \
TEST(ParseNodeKindTest, Name) { \
EXPECT_EQ(#Name, ParseNodeKind::Name().name()); \
}
#include "toolchain/parser/parse_node_kind.def"
} // namespace
} // namespace Carbon::Testing
+89 -92
View File
@@ -270,10 +270,10 @@ auto Parser::HandleCodeBlockState() -> void {
PushState(ParserState::CodeBlockFinish);
if (ConsumeAndAddLeafNodeIf(TokenKind::OpenCurlyBrace(),
ParseNodeKind::CodeBlockStart())) {
ParseNodeKind::CodeBlockStart)) {
PushState(ParserState::StatementScopeLoop);
} else {
AddLeafNode(ParseNodeKind::CodeBlockStart(), *position_,
AddLeafNode(ParseNodeKind::CodeBlockStart, *position_,
/*has_error=*/true);
// Recover by parsing a single statement.
@@ -448,7 +448,7 @@ auto Parser::Parse() -> void {
}
}
AddLeafNode(ParseNodeKind::FileEnd(), *position_);
AddLeafNode(ParseNodeKind::FileEnd, *position_);
}
auto Parser::HandleBraceExpressionState() -> void {
@@ -459,7 +459,7 @@ auto Parser::HandleBraceExpressionState() -> void {
CARBON_CHECK(ConsumeAndAddLeafNodeIf(
TokenKind::OpenCurlyBrace(),
ParseNodeKind::StructLiteralOrStructTypeLiteralStart()));
ParseNodeKind::StructLiteralOrStructTypeLiteralStart));
if (!PositionIs(TokenKind::CloseCurlyBrace())) {
PushState(ParserState::BraceExpressionParameterAsUnknown);
}
@@ -610,17 +610,15 @@ auto Parser::HandleBraceExpressionParameterFinish(BraceExpressionKind kind)
auto state = PopState();
if (state.has_error) {
AddLeafNode(ParseNodeKind::StructFieldUnknown(), state.token,
AddLeafNode(ParseNodeKind::StructFieldUnknown, state.token,
/*has_error=*/true);
} else {
AddNode(kind == BraceExpressionKind::Type
? ParseNodeKind::StructFieldType()
: ParseNodeKind::StructFieldValue(),
AddNode(kind == BraceExpressionKind::Type ? ParseNodeKind::StructFieldType
: ParseNodeKind::StructFieldValue,
state.token, state.subtree_start, /*has_error=*/false);
}
if (ConsumeListToken(ParseNodeKind::StructComma(),
TokenKind::CloseCurlyBrace(),
if (ConsumeListToken(ParseNodeKind::StructComma, TokenKind::CloseCurlyBrace(),
state.has_error) == ListTokenKind::Comma) {
PushState(BraceExpressionKindToParserState(
kind, ParserState::BraceExpressionParameterAsType,
@@ -644,8 +642,8 @@ auto Parser::HandleBraceExpressionParameterFinishAsUnknownState() -> void {
auto Parser::HandleBraceExpressionFinish(BraceExpressionKind kind) -> void {
auto state = PopState();
AddNode(kind == BraceExpressionKind::Type ? ParseNodeKind::StructTypeLiteral()
: ParseNodeKind::StructLiteral(),
AddNode(kind == BraceExpressionKind::Type ? ParseNodeKind::StructTypeLiteral
: ParseNodeKind::StructLiteral,
Consume(), state.subtree_start, state.has_error);
}
@@ -667,7 +665,7 @@ auto Parser::HandleCallExpressionState() -> void {
state.state = ParserState::CallExpressionFinish;
PushState(state);
AddNode(ParseNodeKind::CallExpressionStart(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::CallExpressionStart, Consume(), state.subtree_start,
state.has_error);
if (!PositionIs(TokenKind::CloseParen())) {
PushState(ParserState::CallExpressionParameterFinish);
@@ -682,7 +680,7 @@ auto Parser::HandleCallExpressionParameterFinishState() -> void {
ReturnErrorOnState();
}
if (ConsumeListToken(ParseNodeKind::CallExpressionComma(),
if (ConsumeListToken(ParseNodeKind::CallExpressionComma,
TokenKind::CloseParen(),
state.has_error) == ListTokenKind::Comma) {
PushState(ParserState::CallExpressionParameterFinish);
@@ -693,7 +691,7 @@ auto Parser::HandleCallExpressionParameterFinishState() -> void {
auto Parser::HandleCallExpressionFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::CallExpression(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::CallExpression, Consume(), state.subtree_start,
state.has_error);
}
@@ -702,10 +700,10 @@ auto Parser::HandleCodeBlockFinishState() -> void {
// If the block started with an open curly, this is a close curly.
if (tokens_->GetKind(state.token) == TokenKind::OpenCurlyBrace()) {
AddNode(ParseNodeKind::CodeBlock(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::CodeBlock, Consume(), state.subtree_start,
state.has_error);
} else {
AddNode(ParseNodeKind::CodeBlock(), state.token, state.subtree_start,
AddNode(ParseNodeKind::CodeBlock, state.token, state.subtree_start,
/*has_error=*/true);
}
}
@@ -720,7 +718,7 @@ auto Parser::HandleDeclarationLoopState() -> void {
}
case TokenKind::Fn(): {
PushState(ParserState::FunctionIntroducer);
AddLeafNode(ParseNodeKind::FunctionIntroducer(), Consume());
AddLeafNode(ParseNodeKind::FunctionIntroducer, Consume());
break;
}
case TokenKind::Package(): {
@@ -728,7 +726,7 @@ auto Parser::HandleDeclarationLoopState() -> void {
break;
}
case TokenKind::Semi(): {
AddLeafNode(ParseNodeKind::EmptyDeclaration(), Consume());
AddLeafNode(ParseNodeKind::EmptyDeclaration, Consume());
break;
}
case TokenKind::Var(): {
@@ -746,7 +744,7 @@ auto Parser::HandleDeclarationLoopState() -> void {
auto semi = SkipPastLikelyEnd(cursor);
// Locate the EmptyDeclaration at the semi when found, but use the
// original cursor location for an error when not.
AddLeafNode(ParseNodeKind::EmptyDeclaration(), semi ? *semi : cursor,
AddLeafNode(ParseNodeKind::EmptyDeclaration, semi ? *semi : cursor,
/*has_error=*/true);
break;
}
@@ -759,7 +757,7 @@ auto Parser::HandleDeducedParameterListFinishState() -> void {
CARBON_CHECK(tokens_->GetKind(*position_) == TokenKind::CloseSquareBracket())
<< "Expected current token to be: `]`, found: "
<< tokens_->GetKind(state.token);
AddNode(ParseNodeKind::DeducedParameterList(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::DeducedParameterList, Consume(), state.subtree_start,
state.has_error);
}
@@ -770,17 +768,17 @@ auto Parser::HandleDesignator(bool as_struct) -> void {
auto dot = ConsumeChecked(TokenKind::Period());
if (!ConsumeAndAddLeafNodeIf(TokenKind::Identifier(),
ParseNodeKind::DesignatedName())) {
ParseNodeKind::DesignatedName)) {
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
"Expected identifier after `.`.");
emitter_->Emit(*position_, ExpectedIdentifierAfterDot);
// If we see a keyword, assume it was intended to be the designated name.
// TODO: Should keywords be valid in designators?
if (PositionKind().is_keyword()) {
AddLeafNode(ParseNodeKind::DesignatedName(), Consume(),
AddLeafNode(ParseNodeKind::DesignatedName, Consume(),
/*has_error=*/true);
} else {
AddLeafNode(ParseNodeKind::DesignatedName(), *position_,
AddLeafNode(ParseNodeKind::DesignatedName, *position_,
/*has_error=*/true);
// Indicate the error to the parent state so that it can avoid producing
// more errors.
@@ -788,8 +786,8 @@ auto Parser::HandleDesignator(bool as_struct) -> void {
}
}
AddNode(as_struct ? ParseNodeKind::StructFieldDesignator()
: ParseNodeKind::DesignatorExpression(),
AddNode(as_struct ? ParseNodeKind::StructFieldDesignator
: ParseNodeKind::DesignatorExpression,
dot, state.subtree_start, state.has_error);
}
@@ -840,7 +838,7 @@ auto Parser::HandleExpressionInPostfixState() -> void {
// expression.
switch (PositionKind()) {
case TokenKind::Identifier(): {
AddLeafNode(ParseNodeKind::NameReference(), Consume());
AddLeafNode(ParseNodeKind::NameReference, Consume());
PushState(state);
break;
}
@@ -850,7 +848,7 @@ auto Parser::HandleExpressionInPostfixState() -> void {
case TokenKind::IntegerTypeLiteral():
case TokenKind::UnsignedIntegerTypeLiteral():
case TokenKind::FloatingPointTypeLiteral(): {
AddLeafNode(ParseNodeKind::Literal(), Consume());
AddLeafNode(ParseNodeKind::Literal, Consume());
PushState(state);
break;
}
@@ -865,7 +863,7 @@ auto Parser::HandleExpressionInPostfixState() -> void {
break;
}
case TokenKind::SelfType(): {
AddLeafNode(ParseNodeKind::SelfType(), Consume());
AddLeafNode(ParseNodeKind::SelfType, Consume());
PushState(state);
break;
}
@@ -954,7 +952,7 @@ auto Parser::HandleExpressionLoopState() -> void {
PushState(state);
PushStateForExpression(operator_precedence);
} else {
AddNode(ParseNodeKind::PostfixOperator(), state.token, state.subtree_start,
AddNode(ParseNodeKind::PostfixOperator, state.token, state.subtree_start,
state.has_error);
state.has_error = false;
PushState(state);
@@ -964,7 +962,7 @@ auto Parser::HandleExpressionLoopState() -> void {
auto Parser::HandleExpressionLoopForBinaryState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::InfixOperator(), state.token, state.subtree_start,
AddNode(ParseNodeKind::InfixOperator, state.token, state.subtree_start,
state.has_error);
state.state = ParserState::ExpressionLoop;
state.has_error = false;
@@ -974,7 +972,7 @@ auto Parser::HandleExpressionLoopForBinaryState() -> void {
auto Parser::HandleExpressionLoopForPrefixState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::PrefixOperator(), state.token, state.subtree_start,
AddNode(ParseNodeKind::PrefixOperator, state.token, state.subtree_start,
state.has_error);
state.state = ParserState::ExpressionLoop;
state.has_error = false;
@@ -985,7 +983,7 @@ auto Parser::HandleExpressionStatementFinishState() -> void {
auto state = PopState();
if (auto semi = ConsumeIf(TokenKind::Semi())) {
AddNode(ParseNodeKind::ExpressionStatement(), *semi, state.subtree_start,
AddNode(ParseNodeKind::ExpressionStatement, *semi, state.subtree_start,
state.has_error);
return;
}
@@ -995,7 +993,7 @@ auto Parser::HandleExpressionStatementFinishState() -> void {
}
if (auto semi_token = SkipPastLikelyEnd(state.token)) {
AddNode(ParseNodeKind::ExpressionStatement(), *semi_token,
AddNode(ParseNodeKind::ExpressionStatement, *semi_token,
state.subtree_start,
/*has_error=*/true);
return;
@@ -1013,7 +1011,7 @@ auto Parser::HandleFunctionError(StateStackEntry state,
token = *semi;
}
}
AddNode(ParseNodeKind::FunctionDeclaration(), token, state.subtree_start,
AddNode(ParseNodeKind::FunctionDeclaration, token, state.subtree_start,
/*has_error=*/true);
}
@@ -1021,7 +1019,7 @@ auto Parser::HandleFunctionIntroducerState() -> void {
auto state = PopState();
if (!ConsumeAndAddLeafNodeIf(TokenKind::Identifier(),
ParseNodeKind::DeclaredName())) {
ParseNodeKind::DeclaredName)) {
CARBON_DIAGNOSTIC(ExpectedFunctionName, Error,
"Expected function name after `fn` keyword.");
emitter_->Emit(*position_, ExpectedFunctionName);
@@ -1039,7 +1037,7 @@ auto Parser::HandleFunctionIntroducerState() -> void {
if (PositionIs(TokenKind::OpenSquareBracket())) {
PushState(ParserState::DeducedParameterListFinish);
// This is for sure a `[`, we can safely create the corresponding node.
AddLeafNode(ParseNodeKind::DeducedParameterListStart(), Consume());
AddLeafNode(ParseNodeKind::DeducedParameterListStart, Consume());
if (PositionIs(TokenKind::CloseSquareBracket())) {
return;
@@ -1070,7 +1068,7 @@ auto Parser::HandleFunctionAfterDeducedParameterListState() -> void {
state.state = ParserState::FunctionAfterParameterList;
PushState(state);
PushState(ParserState::FunctionParameterListFinish);
AddLeafNode(ParseNodeKind::ParameterListStart(), Consume());
AddLeafNode(ParseNodeKind::ParameterListStart, Consume());
if (!PositionIs(TokenKind::CloseParen())) {
PushState(ParserState::FunctionParameter);
@@ -1091,7 +1089,7 @@ auto Parser::HandleFunctionParameterFinishState() -> void {
ReturnErrorOnState();
}
if (ConsumeListToken(ParseNodeKind::ParameterListComma(),
if (ConsumeListToken(ParseNodeKind::ParameterListComma,
TokenKind::CloseParen(),
state.has_error) == ListTokenKind::Comma) {
PushState(ParserState::PatternAsFunctionParameter);
@@ -1101,9 +1099,8 @@ auto Parser::HandleFunctionParameterFinishState() -> void {
auto Parser::HandleFunctionParameterListFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::ParameterList(),
ConsumeChecked(TokenKind::CloseParen()), state.subtree_start,
state.has_error);
AddNode(ParseNodeKind::ParameterList, ConsumeChecked(TokenKind::CloseParen()),
state.subtree_start, state.has_error);
}
auto Parser::HandleFunctionAfterParameterListState() -> void {
@@ -1125,7 +1122,7 @@ auto Parser::HandleFunctionAfterParameterListState() -> void {
auto Parser::HandleFunctionReturnTypeFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::ReturnType(), state.token, state.subtree_start,
AddNode(ParseNodeKind::ReturnType, state.token, state.subtree_start,
state.has_error);
}
@@ -1134,7 +1131,7 @@ auto Parser::HandleFunctionSignatureFinishState() -> void {
switch (PositionKind()) {
case TokenKind::Semi(): {
AddNode(ParseNodeKind::FunctionDeclaration(), Consume(),
AddNode(ParseNodeKind::FunctionDeclaration, Consume(),
state.subtree_start, state.has_error);
break;
}
@@ -1148,7 +1145,7 @@ auto Parser::HandleFunctionSignatureFinishState() -> void {
break;
}
AddNode(ParseNodeKind::FunctionDefinitionStart(), Consume(),
AddNode(ParseNodeKind::FunctionDefinitionStart, Consume(),
state.subtree_start, state.has_error);
// Any error is recorded on the FunctionDefinitionStart.
state.has_error = false;
@@ -1173,7 +1170,7 @@ auto Parser::HandleFunctionSignatureFinishState() -> void {
auto Parser::HandleFunctionDefinitionFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::FunctionDefinition(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::FunctionDefinition, Consume(), state.subtree_start,
state.has_error);
}
@@ -1184,7 +1181,7 @@ auto Parser::HandleInterfaceIntroducerState() -> void {
stack_context_ = ParseContext::Interface;
if (!ConsumeAndAddLeafNodeIf(TokenKind::Identifier(),
ParseNodeKind::DeclaredName())) {
ParseNodeKind::DeclaredName)) {
CARBON_DIAGNOSTIC(ExpectedInterfaceName, Error,
"Expected interface name after `interface` keyword.");
emitter_->Emit(*position_, ExpectedInterfaceName);
@@ -1194,7 +1191,7 @@ auto Parser::HandleInterfaceIntroducerState() -> void {
// bracketing on interfaces.
// TODO: Either fix this or normalize it, still deciding on the right
// approach.
AddLeafNode(ParseNodeKind::DeclaredName(), state.token, /*has_error=*/true);
AddLeafNode(ParseNodeKind::DeclaredName, state.token, /*has_error=*/true);
}
bool parse_body = true;
@@ -1214,7 +1211,7 @@ auto Parser::HandleInterfaceIntroducerState() -> void {
if (parse_body) {
PushState(ParserState::InterfaceDefinitionLoop);
AddLeafNode(ParseNodeKind::InterfaceBodyStart(), Consume());
AddLeafNode(ParseNodeKind::InterfaceBodyStart, Consume());
}
}
@@ -1226,20 +1223,20 @@ auto Parser::HandleInterfaceDefinitionLoopState() -> void {
case TokenKind::CloseCurlyBrace(): {
auto state = PopState();
AddNode(ParseNodeKind::InterfaceBody(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::InterfaceBody, Consume(), state.subtree_start,
state.has_error);
break;
}
case TokenKind::Fn(): {
PushState(ParserState::FunctionIntroducer);
AddLeafNode(ParseNodeKind::FunctionIntroducer(), Consume());
AddLeafNode(ParseNodeKind::FunctionIntroducer, Consume());
break;
}
default: {
emitter_->Emit(*position_, UnrecognizedDeclaration);
if (auto semi = SkipPastLikelyEnd(*position_)) {
AddLeafNode(ParseNodeKind::EmptyDeclaration(), *semi,
AddLeafNode(ParseNodeKind::EmptyDeclaration, *semi,
/*has_error=*/true);
} else {
ReturnErrorOnState();
@@ -1251,25 +1248,25 @@ auto Parser::HandleInterfaceDefinitionLoopState() -> void {
auto Parser::HandleInterfaceDefinitionFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::InterfaceDefinition(), state.token,
state.subtree_start, state.has_error);
AddNode(ParseNodeKind::InterfaceDefinition, state.token, state.subtree_start,
state.has_error);
stack_context_ = ParseContext::File;
}
auto Parser::HandlePackageState() -> void {
auto state = PopState();
AddLeafNode(ParseNodeKind::PackageIntroducer(), Consume());
AddLeafNode(ParseNodeKind::PackageIntroducer, Consume());
auto exit_on_parse_error = [&]() {
auto semi_token = SkipPastLikelyEnd(state.token);
return AddNode(ParseNodeKind::PackageDirective(),
return AddNode(ParseNodeKind::PackageDirective,
semi_token ? *semi_token : state.token, state.subtree_start,
/*has_error=*/true);
};
if (!ConsumeAndAddLeafNodeIf(TokenKind::Identifier(),
ParseNodeKind::DeclaredName())) {
ParseNodeKind::DeclaredName)) {
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
"Expected identifier after `package`.");
emitter_->Emit(*position_, ExpectedIdentifierAfterPackage);
@@ -1282,7 +1279,7 @@ auto Parser::HandlePackageState() -> void {
auto library_start = tree_->size();
if (!ConsumeAndAddLeafNodeIf(TokenKind::StringLiteral(),
ParseNodeKind::Literal())) {
ParseNodeKind::Literal)) {
CARBON_DIAGNOSTIC(
ExpectedLibraryName, Error,
"Expected a string literal to specify the library name.");
@@ -1291,18 +1288,18 @@ auto Parser::HandlePackageState() -> void {
return;
}
AddNode(ParseNodeKind::PackageLibrary(), *library_token, library_start,
AddNode(ParseNodeKind::PackageLibrary, *library_token, library_start,
/*has_error=*/false);
library_parsed = true;
}
switch (auto api_or_impl_token = tokens_->GetKind(*(position_))) {
case TokenKind::Api(): {
AddLeafNode(ParseNodeKind::PackageApi(), Consume());
AddLeafNode(ParseNodeKind::PackageApi, Consume());
break;
}
case TokenKind::Impl(): {
AddLeafNode(ParseNodeKind::PackageImpl(), Consume());
AddLeafNode(ParseNodeKind::PackageImpl, Consume());
break;
}
default: {
@@ -1331,7 +1328,7 @@ auto Parser::HandlePackageState() -> void {
return;
}
AddNode(ParseNodeKind::PackageDirective(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::PackageDirective, Consume(), state.subtree_start,
/*has_error=*/false);
}
@@ -1347,32 +1344,32 @@ auto Parser::HandleParenCondition(ParseNodeKind start_kind,
}
auto Parser::HandleParenConditionAsIfState() -> void {
HandleParenCondition(ParseNodeKind::IfConditionStart(),
HandleParenCondition(ParseNodeKind::IfConditionStart,
ParserState::ParenConditionFinishAsIf);
}
auto Parser::HandleParenConditionAsWhileState() -> void {
HandleParenCondition(ParseNodeKind::WhileConditionStart(),
HandleParenCondition(ParseNodeKind::WhileConditionStart,
ParserState::ParenConditionFinishAsWhile);
}
auto Parser::HandleParenConditionFinishAsIfState() -> void {
auto state = PopState();
ConsumeAndAddCloseParen(state, ParseNodeKind::IfCondition());
ConsumeAndAddCloseParen(state, ParseNodeKind::IfCondition);
}
auto Parser::HandleParenConditionFinishAsWhileState() -> void {
auto state = PopState();
ConsumeAndAddCloseParen(state, ParseNodeKind::WhileCondition());
ConsumeAndAddCloseParen(state, ParseNodeKind::WhileCondition);
}
auto Parser::HandleParenExpressionState() -> void {
auto state = PopState();
// Advance past the open paren.
AddLeafNode(ParseNodeKind::ParenExpressionOrTupleLiteralStart(),
AddLeafNode(ParseNodeKind::ParenExpressionOrTupleLiteralStart,
ConsumeChecked(TokenKind::OpenParen()));
if (PositionIs(TokenKind::CloseParen())) {
@@ -1390,7 +1387,7 @@ auto Parser::HandleParenExpressionParameterFinish(bool as_tuple) -> void {
auto state = PopState();
auto list_token_kind =
ConsumeListToken(ParseNodeKind::TupleLiteralComma(),
ConsumeListToken(ParseNodeKind::TupleLiteralComma,
TokenKind::CloseParen(), state.has_error);
if (list_token_kind == ListTokenKind::Close) {
return;
@@ -1427,14 +1424,14 @@ auto Parser::HandleParenExpressionParameterFinishAsTupleState() -> void {
auto Parser::HandleParenExpressionFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::ParenExpression(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::ParenExpression, Consume(), state.subtree_start,
state.has_error);
}
auto Parser::HandleParenExpressionFinishAsTupleState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::TupleLiteral(), Consume(), state.subtree_start,
AddNode(ParseNodeKind::TupleLiteral, Consume(), state.subtree_start,
state.has_error);
}
@@ -1471,7 +1468,7 @@ auto Parser::HandlePattern(PatternKind pattern_kind) -> void {
state.token = *(position_ + 1);
PushState(state);
PushStateForExpression(PrecedenceGroup::ForType());
AddLeafNode(ParseNodeKind::DeclaredName(), *position_);
AddLeafNode(ParseNodeKind::DeclaredName, *position_);
position_ += 2;
}
@@ -1493,7 +1490,7 @@ auto Parser::HandlePatternFinishState() -> void {
}
// TODO: may need to mark has_error if !type.
AddNode(ParseNodeKind::PatternBinding(), state.token, state.subtree_start,
AddNode(ParseNodeKind::PatternBinding, state.token, state.subtree_start,
/*has_error=*/false);
}
@@ -1506,7 +1503,7 @@ auto Parser::HandlePatternAddressState() -> void {
return;
}
AddNode(ParseNodeKind::Address(), state.token, state.subtree_start,
AddNode(ParseNodeKind::Address, state.token, state.subtree_start,
/*has_error=*/false);
}
@@ -1529,7 +1526,7 @@ auto Parser::HandleSelfPatternState() -> void {
state.token = *(position_ + 1);
PushState(state);
PushStateForExpression(PrecedenceGroup::ForType());
AddLeafNode(ParseNodeKind::SelfDeducedParameter(), *position_);
AddLeafNode(ParseNodeKind::SelfDeducedParameter, *position_);
position_ += 2;
return;
}
@@ -1549,7 +1546,7 @@ auto Parser::HandleSelfPatternState() -> void {
PushState(ParserState::PatternFinish);
PushStateForExpression(PrecedenceGroup::ForType());
AddLeafNode(ParseNodeKind::SelfDeducedParameter(), *(position_ + 1));
AddLeafNode(ParseNodeKind::SelfDeducedParameter, *(position_ + 1));
position_ += 2;
return;
}
@@ -1577,12 +1574,12 @@ auto Parser::HandleStatementState() -> void {
switch (PositionKind()) {
case TokenKind::Break(): {
PushState(ParserState::StatementBreakFinish);
AddLeafNode(ParseNodeKind::BreakStatementStart(), Consume());
AddLeafNode(ParseNodeKind::BreakStatementStart, Consume());
break;
}
case TokenKind::Continue(): {
PushState(ParserState::StatementContinueFinish);
AddLeafNode(ParseNodeKind::ContinueStatementStart(), Consume());
AddLeafNode(ParseNodeKind::ContinueStatementStart, Consume());
break;
}
case TokenKind::For(): {
@@ -1616,17 +1613,17 @@ auto Parser::HandleStatementState() -> void {
}
auto Parser::HandleStatementBreakFinishState() -> void {
HandleStatementKeywordFinish(ParseNodeKind::BreakStatement());
HandleStatementKeywordFinish(ParseNodeKind::BreakStatement);
}
auto Parser::HandleStatementContinueFinishState() -> void {
HandleStatementKeywordFinish(ParseNodeKind::ContinueStatement());
HandleStatementKeywordFinish(ParseNodeKind::ContinueStatement);
}
auto Parser::HandleStatementForHeaderState() -> void {
auto state = PopState();
ConsumeAndAddOpenParen(state.token, ParseNodeKind::ForHeaderStart());
ConsumeAndAddOpenParen(state.token, ParseNodeKind::ForHeaderStart);
state.state = ParserState::StatementForHeaderIn;
@@ -1658,7 +1655,7 @@ auto Parser::HandleStatementForHeaderInState() -> void {
auto Parser::HandleStatementForHeaderFinishState() -> void {
auto state = PopState();
ConsumeAndAddCloseParen(state, ParseNodeKind::ForHeader());
ConsumeAndAddCloseParen(state, ParseNodeKind::ForHeader);
PushState(ParserState::CodeBlock);
}
@@ -1666,7 +1663,7 @@ auto Parser::HandleStatementForHeaderFinishState() -> void {
auto Parser::HandleStatementForFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::ForStatement(), state.token, state.subtree_start,
AddNode(ParseNodeKind::ForStatement, state.token, state.subtree_start,
state.has_error);
}
@@ -1690,21 +1687,21 @@ auto Parser::HandleStatementIfThenBlockFinishState() -> void {
auto state = PopState();
if (ConsumeAndAddLeafNodeIf(TokenKind::Else(),
ParseNodeKind::IfStatementElse())) {
ParseNodeKind::IfStatementElse)) {
state.state = ParserState::StatementIfElseBlockFinish;
PushState(state);
// `else if` is permitted as a special case.
PushState(PositionIs(TokenKind::If()) ? ParserState::StatementIf
: ParserState::CodeBlock);
} else {
AddNode(ParseNodeKind::IfStatement(), state.token, state.subtree_start,
AddNode(ParseNodeKind::IfStatement, state.token, state.subtree_start,
state.has_error);
}
}
auto Parser::HandleStatementIfElseBlockFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::IfStatement(), state.token, state.subtree_start,
AddNode(ParseNodeKind::IfStatement, state.token, state.subtree_start,
state.has_error);
}
@@ -1733,14 +1730,14 @@ auto Parser::HandleStatementReturnState() -> void {
state.state = ParserState::StatementReturnFinish;
PushState(state);
AddLeafNode(ParseNodeKind::ReturnStatementStart(), Consume());
AddLeafNode(ParseNodeKind::ReturnStatementStart, Consume());
if (!PositionIs(TokenKind::Semi())) {
PushState(ParserState::Expression);
}
}
auto Parser::HandleStatementReturnFinishState() -> void {
HandleStatementKeywordFinish(ParseNodeKind::ReturnStatement());
HandleStatementKeywordFinish(ParseNodeKind::ReturnStatement);
}
auto Parser::HandleStatementScopeLoopState() -> void {
@@ -1776,7 +1773,7 @@ auto Parser::HandleStatementWhileConditionFinishState() -> void {
auto Parser::HandleStatementWhileBlockFinishState() -> void {
auto state = PopState();
AddNode(ParseNodeKind::WhileStatement(), state.token, state.subtree_start,
AddNode(ParseNodeKind::WhileStatement, state.token, state.subtree_start,
state.has_error);
}
@@ -1787,7 +1784,7 @@ auto Parser::HandleVar(ParserState finish_state) -> void {
PushState(finish_state);
PushState(ParserState::VarAfterPattern);
AddLeafNode(ParseNodeKind::VariableIntroducer(), Consume());
AddLeafNode(ParseNodeKind::VariableIntroducer, Consume());
// This will start at the pattern.
PushState(ParserState::PatternAsVariable);
@@ -1812,7 +1809,7 @@ auto Parser::HandleVarAfterPatternState() -> void {
}
if (auto equals = ConsumeIf(TokenKind::Equal())) {
AddLeafNode(ParseNodeKind::VariableInitializer(), *equals);
AddLeafNode(ParseNodeKind::VariableInitializer, *equals);
PushState(ParserState::Expression);
}
}
@@ -1830,7 +1827,7 @@ auto Parser::HandleVarFinishAsSemicolonState() -> void {
end_token = *semi_token;
}
}
AddNode(ParseNodeKind::VariableDeclaration(), end_token, state.subtree_start,
AddNode(ParseNodeKind::VariableDeclaration, end_token, state.subtree_start,
state.has_error);
}
@@ -1853,7 +1850,7 @@ auto Parser::HandleVarFinishAsForState() -> void {
state.has_error = true;
}
AddNode(ParseNodeKind::ForIn(), end_token, state.subtree_start,
AddNode(ParseNodeKind::ForIn, end_token, state.subtree_start,
state.has_error);
}
@@ -27,7 +27,7 @@ class SemanticsParseTreeHandler::PrettyStackTraceNodeStack
const auto& entry = handler_->node_stack_[i];
auto parse_node_kind = handler_->parse_tree_->node_kind(entry.parse_node);
output << "\t" << i << ".\t" << parse_node_kind;
if (parse_node_kind == ParseNodeKind::PatternBinding()) {
if (parse_node_kind == ParseNodeKind::PatternBinding) {
output << " -> " << entry.name_id;
} else {
if (entry.result_id.is_valid()) {
@@ -74,7 +74,7 @@ auto SemanticsParseTreeHandler::Build() -> void {
for (auto parse_node : parse_tree_->postorder()) {
switch (auto parse_kind = parse_tree_->node_kind(parse_node)) {
#define CARBON_PARSE_NODE_KIND(Name) \
case ParseNodeKind::Name(): { \
case ParseNodeKind::Name: { \
Handle##Name(parse_node); \
break; \
}
@@ -99,8 +99,7 @@ auto SemanticsParseTreeHandler::BindName(ParseTree::Node name_node,
SemanticsNodeId type_id,
SemanticsNodeId target_id)
-> SemanticsStringId {
CARBON_CHECK(parse_tree_->node_kind(name_node) ==
ParseNodeKind::DeclaredName())
CARBON_CHECK(parse_tree_->node_kind(name_node) == ParseNodeKind::DeclaredName)
<< parse_tree_->node_kind(name_node);
auto name_str = parse_tree_->GetNodeText(name_node);
auto name_id = semantics_->AddString(name_str);
@@ -156,7 +155,7 @@ auto SemanticsParseTreeHandler::Push(ParseTree::Node parse_node,
auto SemanticsParseTreeHandler::Push(ParseTree::Node parse_node,
SemanticsStringId name_id) -> void {
CARBON_CHECK(parse_tree_->node_kind(parse_node) ==
ParseNodeKind::PatternBinding());
ParseNodeKind::PatternBinding);
CARBON_VLOG() << "Push " << node_stack_.size() << ": "
<< parse_tree_->node_kind(parse_node) << " -> " << name_id
<< "\n";
@@ -385,10 +384,10 @@ auto SemanticsParseTreeHandler::HandleFunctionDefinition(
ParseTree::Node parse_node) -> void {
// Merges code block children up under the FunctionDefinitionStart.
while (parse_tree_->node_kind(node_stack_.back().parse_node) !=
ParseNodeKind::FunctionDefinitionStart()) {
ParseNodeKind::FunctionDefinitionStart) {
node_stack_.pop_back();
}
Pop(ParseNodeKind::FunctionDefinitionStart());
Pop(ParseNodeKind::FunctionDefinitionStart);
PopScope();
node_block_stack_.pop_back();
Push(parse_node);
@@ -396,11 +395,11 @@ auto SemanticsParseTreeHandler::HandleFunctionDefinition(
auto SemanticsParseTreeHandler::HandleFunctionDefinitionStart(
ParseTree::Node parse_node) -> void {
Pop(ParseNodeKind::ParameterList());
Pop(ParseNodeKind::ParameterList);
auto name_node = node_stack_.back().parse_node;
node_stack_.pop_back();
auto fn_node = node_stack_.back().parse_node;
Pop(ParseNodeKind::FunctionIntroducer());
Pop(ParseNodeKind::FunctionIntroducer);
auto decl_id = AddNode(SemanticsNode::MakeFunctionDeclaration(fn_node));
// TODO: Propagate the type of the function.
@@ -557,7 +556,7 @@ auto SemanticsParseTreeHandler::HandleParameterList(ParseTree::Node parse_node)
-> void {
// TODO: This should transform into a usable parameter list. For now
// it's unused and only stored so that node counts match.
Pop(ParseNodeKind::ParameterListStart());
Pop(ParseNodeKind::ParameterListStart);
Push(parse_node);
}
@@ -616,13 +615,13 @@ auto SemanticsParseTreeHandler::HandlePrefixOperator(
auto SemanticsParseTreeHandler::HandleReturnStatement(
ParseTree::Node parse_node) -> void {
if (parse_tree_->node_kind(node_stack_.back().parse_node) ==
ParseNodeKind::ReturnStatementStart()) {
Pop(ParseNodeKind::ReturnStatementStart());
ParseNodeKind::ReturnStatementStart) {
Pop(ParseNodeKind::ReturnStatementStart);
Push(parse_node, SemanticsNode::MakeReturn(parse_node));
} else {
auto arg = PopWithResult();
auto arg_type = semantics_->GetType(arg);
Pop(ParseNodeKind::ReturnStatementStart());
Pop(ParseNodeKind::ReturnStatementStart);
Push(parse_node,
SemanticsNode::MakeReturnExpression(parse_node, arg_type, arg));
}
@@ -705,13 +704,13 @@ auto SemanticsParseTreeHandler::HandleVariableDeclaration(
CARBON_CHECK(last_child.result_id.is_valid());
if (parse_tree_->node_kind(last_child.parse_node) !=
ParseNodeKind::PatternBinding()) {
ParseNodeKind::PatternBinding) {
SemanticsNodeId init_id = last_child.result_id;
auto storage_id = PopWithResult(ParseNodeKind::VariableInitializer());
auto storage_id = PopWithResult(ParseNodeKind::VariableInitializer);
auto binding = node_stack_.pop_back_val();
CARBON_CHECK(parse_tree_->node_kind(binding.parse_node) ==
ParseNodeKind::PatternBinding());
ParseNodeKind::PatternBinding);
CARBON_CHECK(binding.name_id.is_valid());
// Restore the name now that the initializer is complete.
@@ -723,7 +722,7 @@ auto SemanticsParseTreeHandler::HandleVariableDeclaration(
init_id));
}
Pop(ParseNodeKind::VariableIntroducer());
Pop(ParseNodeKind::VariableIntroducer);
Push(parse_node);
}
@@ -739,7 +738,7 @@ auto SemanticsParseTreeHandler::HandleVariableInitializer(
// restored by `VariableDeclaration`.
auto back = node_stack_.back();
CARBON_CHECK(parse_tree_->node_kind(back.parse_node) ==
ParseNodeKind::PatternBinding())
ParseNodeKind::PatternBinding)
<< parse_tree_->node_kind(back.parse_node);
// Save the storage ID.