mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 23:10:28 +00:00
Instead of tracking the token associated with a parse node in the `.def` file macro, track it on the typed node instead. List the token as a field inside the node structure to show the order of the token relative to the other components of the grammar production, and to allow the token index to be accessed when the node is extracted. Remove the corresponding information from the `.def` file, leaving behind just a list of parse node kinds in the majority of cases. This also removes the checking of the token kind associated with a parse node in the case where the parse node has errors. Previously we had a flag on the node kind to indicate whether we should check this, but per [discord discussion](https://discord.com/channels/655572317891461132/655578254970716160/1246214418979881052), we have decided to remove this. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
146 lines
5.6 KiB
C++
146 lines
5.6 KiB
C++
// 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_
|
|
#define CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_
|
|
|
|
#include "toolchain/base/index_base.h"
|
|
#include "toolchain/lex/token_index.h"
|
|
#include "toolchain/parse/node_kind.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
// Represents an invalid node id of any type
|
|
struct InvalidNodeId {};
|
|
|
|
// A lightweight handle representing a node in the tree.
|
|
//
|
|
// Objects of this type are small and cheap to copy and store. They don't
|
|
// contain any of the information about the node, and serve as a handle that
|
|
// can be used with the underlying tree to query for detailed information.
|
|
struct NodeId : public IdBase {
|
|
// An explicitly invalid instance.
|
|
static constexpr InvalidNodeId Invalid;
|
|
|
|
using IdBase::IdBase;
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr NodeId(InvalidNodeId /*invalid*/) : IdBase(NodeId::InvalidIndex) {}
|
|
};
|
|
|
|
// For looking up the type associated with a given id type.
|
|
template <typename T>
|
|
struct NodeForId;
|
|
|
|
// `<KindName>Id` is a typed version of `NodeId` that references a node of kind
|
|
// `<KindName>`:
|
|
template <const NodeKind& K>
|
|
struct NodeIdForKind : public NodeId {
|
|
// NOLINTNEXTLINE(readability-identifier-naming)
|
|
static const NodeKind& Kind;
|
|
constexpr explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr NodeIdForKind(InvalidNodeId /*invalid*/)
|
|
: NodeId(NodeId::InvalidIndex) {}
|
|
};
|
|
template <const NodeKind& K>
|
|
const NodeKind& NodeIdForKind<K>::Kind = K;
|
|
|
|
#define CARBON_PARSE_NODE_KIND(KindName) \
|
|
using KindName##Id = NodeIdForKind<NodeKind::KindName>;
|
|
#include "toolchain/parse/node_kind.def"
|
|
|
|
// NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
|
|
template <NodeCategory::RawEnumType Category>
|
|
struct NodeIdInCategory : public NodeId {
|
|
// Support conversion from `NodeIdForKind<Kind>` if Kind's category
|
|
// overlaps with `Category`.
|
|
template <const NodeKind& Kind>
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
NodeIdInCategory(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
|
|
CARBON_CHECK(Kind.category().HasAnyOf(Category));
|
|
}
|
|
|
|
constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr NodeIdInCategory(InvalidNodeId /*invalid*/)
|
|
: NodeId(NodeId::InvalidIndex) {}
|
|
};
|
|
|
|
// Aliases for `NodeIdInCategory` to describe particular categories of nodes.
|
|
using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
|
|
using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
|
|
using AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
|
|
using AnyMemberNameOrMemberExprId =
|
|
NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr>;
|
|
using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
|
|
using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
|
|
using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
|
|
|
|
// NodeId with kind that matches one of the `T::Kind`s.
|
|
template <typename... T>
|
|
struct NodeIdOneOf : public NodeId {
|
|
static_assert(sizeof...(T) >= 2, "Expected at least two types.");
|
|
constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
|
|
template <const NodeKind& Kind>
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
|
|
static_assert(((T::Kind == Kind) || ...));
|
|
}
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr NodeIdOneOf(InvalidNodeId /*invalid*/)
|
|
: NodeId(NodeId::InvalidIndex) {}
|
|
};
|
|
|
|
using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
|
|
using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
|
|
BuiltinFunctionDefinitionStartId>;
|
|
using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
|
|
using AnyInterfaceDeclId =
|
|
NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
|
|
using AnyNamespaceId = NodeIdOneOf<NamespaceId, ImportDeclId>;
|
|
using AnyPointerDeferenceExprId =
|
|
NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
|
|
|
|
// NodeId with kind that is anything but T::Kind.
|
|
template <typename T>
|
|
struct NodeIdNot : public NodeId {
|
|
constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
constexpr NodeIdNot(InvalidNodeId /*invalid*/)
|
|
: NodeId(NodeId::InvalidIndex) {}
|
|
};
|
|
|
|
// This class holds the token corresponding to a parse node, and defines the
|
|
// expected token kind. The specified token index will always have kind K if the
|
|
// enclosing node doesn't have errors. Note that we never try to extract a node
|
|
// that has errors, and there are no restrictions on the token kind in that
|
|
// case.
|
|
template <const Lex::TokenKind& K>
|
|
struct Token {
|
|
static constexpr const Lex::TokenKind& Kind = K;
|
|
Lex::TokenIndex index;
|
|
};
|
|
|
|
// This class holds the token corresponding to a parse node in the case where
|
|
// the parse node can correspond to any token. This should only be used when the
|
|
// node kind is either not used in a finished tree, such as `Placeholder`, or is
|
|
// always invalid, such as `InvalidParse`.
|
|
struct AnyToken {
|
|
Lex::TokenIndex index;
|
|
};
|
|
|
|
// This class holds the token corresponding to a parse node in the case of a
|
|
// virtual token. The parse node doesn't actually own the token in this case.
|
|
template <const Lex::TokenKind& K>
|
|
struct VirtualToken {
|
|
Token<K> token;
|
|
};
|
|
|
|
// Note that the support for extracting these types using the `Tree::Extract*`
|
|
// functions is defined in `extract.cpp`.
|
|
|
|
} // namespace Carbon::Parse
|
|
|
|
#endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_
|