// 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/parse/node_kind.h" namespace Carbon::Parse { // 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 const NodeId Invalid; using IdBase::IdBase; }; constexpr NodeId NodeId::Invalid = NodeId(NodeId::InvalidIndex); // For looking up the type associated with a given id type. template struct NodeForId; // `Id` is a typed version of `NodeId` that references a node of kind // ``: template struct NodeIdForKind : public NodeId { static const NodeIdForKind Invalid; explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {} }; template constexpr NodeIdForKind NodeIdForKind::Invalid = NodeIdForKind(NodeId::Invalid.index); #define CARBON_PARSE_NODE_KIND(KindName) \ using KindName##Id = NodeIdForKind; #include "toolchain/parse/node_kind.def" // NodeId that matches any NodeKind whose `category()` overlaps with `Category`. template struct NodeIdInCategory : public NodeId { // An explicitly invalid instance. static const NodeIdInCategory Invalid; // TODO: Support conversion from `NodeIdForKind` if `Kind::category()` // overlaps with `Category`. explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {} }; template constexpr NodeIdInCategory NodeIdInCategory::Invalid = NodeIdInCategory(NodeId::InvalidIndex); // Aliases for `NodeIdInCategory` to describe particular categories of nodes. using AnyDeclId = NodeIdInCategory; using AnyExprId = NodeIdInCategory; using AnyMemberNameId = NodeIdInCategory; using AnyModifierId = NodeIdInCategory; using AnyNameComponentId = NodeIdInCategory; using AnyPatternId = NodeIdInCategory; using AnyStatementId = NodeIdInCategory; // NodeId with kind that matches either T::Kind or U::Kind. template struct NodeIdOneOf : public NodeId { // An explicitly invalid instance. static const NodeIdOneOf Invalid; // TODO: Support conversion from `NodeIdForKind` if `Kind` is // `T::Kind` or `U::Kind`. explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {} }; template constexpr NodeIdOneOf NodeIdOneOf::Invalid = NodeIdOneOf(NodeId::InvalidIndex); // NodeId with kind that is anything but T::Kind. template struct NodeIdNot : public NodeId { // An explicitly invalid instance. static const NodeIdNot Invalid; explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {} }; template constexpr NodeIdNot NodeIdNot::Invalid = NodeIdNot(NodeId::InvalidIndex); // 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_