mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 05:52:37 +00:00
These are intended to allow the structure of a parse tree node to be
described more precisely in code, to support these use cases:
- Automated checking that the parse tree conforms to the expected
structure. (Added to `Tree::Verify`.)
- Easier reading and understanding of the structure of the parse tree by
toolchain developers. (See `parse/typed_nodes.h`.)
- Easier navigation of the parse tree, for example for tooling uses and
for use when forming diagnostics.
On this last point, an object representing the file may be inspecting
using `Tree::ExtractFile`, as in:
```
auto file = tree->ExtractFile();
for (AnyDeclId decl_id : file.decls) {
// `decl_id` is convertible to a `NodeId`.
if (std::optional<FunctionDecl> fn_decl =
tree->ExtractAs<FunctionDecl>(decl_id)) {
// fn_decl->params is a `TuplePatternId` (which extends `NodeId`)
// that is guaranteed to reference a `TuplePattern`.
std::optional<TuplePattern> params = tree->Extract(fn_decl->params);
// `params` has a value unless there was an error in that node.
} else if (auto class_def = tree->ExtractAs<ClassDefinition>(decl_id)) {
// ...
}
}
```
The `Extract...` functions collect the child nodes into the typed parse
node's fields (internally using a `Tree::SiblingIterator`) for easy
access. However, this is not as fast as directly observing the tree
structure using the postorder strategy being used by the check stage.
These functions rely on using struct reflection on the typed parse node
definitions from `parse/typed_nodes.h` to get the expected structure of
child nodes and then populate them.
Note that validating these in `Tree::Verify` adds significant cost to
it, and is currently included in the parsing stage. Without this change,
a 10 mloc test case of lex & parse takes 4.129 s ± 0.041 s. With this
change, it takes 5.768 s ± 0.036 s.
This builds upon and completes #3393.
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
---------
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
100 lines
3.3 KiB
C++
100 lines
3.3 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/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 <typename T>
|
|
struct NodeForId;
|
|
|
|
// `<KindName>Id` is a typed version of `NodeId` that references a node of kind
|
|
// `<KindName>`:
|
|
template <const NodeKind&>
|
|
struct NodeIdForKind : public NodeId {
|
|
static const NodeIdForKind Invalid;
|
|
|
|
explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
|
|
};
|
|
template <const NodeKind& Kind>
|
|
constexpr NodeIdForKind<Kind> NodeIdForKind<Kind>::Invalid =
|
|
NodeIdForKind(NodeId::Invalid.index);
|
|
|
|
#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 Category>
|
|
struct NodeIdInCategory : public NodeId {
|
|
// An explicitly invalid instance.
|
|
static const NodeIdInCategory<Category> Invalid;
|
|
|
|
explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
|
|
};
|
|
|
|
template <NodeCategory Category>
|
|
constexpr NodeIdInCategory<Category> NodeIdInCategory<Category>::Invalid =
|
|
NodeIdInCategory<Category>(NodeId::InvalidIndex);
|
|
|
|
// Aliases for `NodeIdInCategory` to describe particular categories of nodes.
|
|
using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
|
|
using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
|
|
using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
|
|
using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
|
|
using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
|
|
using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
|
|
|
|
// NodeId with kind that matches either T::Kind or U::Kind.
|
|
template <typename T, typename U>
|
|
struct NodeIdOneOf : public NodeId {
|
|
// An explicitly invalid instance.
|
|
static const NodeIdOneOf<T, U> Invalid;
|
|
|
|
explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
|
|
};
|
|
|
|
template <typename T, typename U>
|
|
constexpr NodeIdOneOf<T, U> NodeIdOneOf<T, U>::Invalid =
|
|
NodeIdOneOf<T, U>(NodeId::InvalidIndex);
|
|
|
|
// NodeId with kind that is anything but T::Kind.
|
|
template <typename T>
|
|
struct NodeIdNot : public NodeId {
|
|
// An explicitly invalid instance.
|
|
static const NodeIdNot<T> Invalid;
|
|
|
|
explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
|
|
};
|
|
|
|
template <typename T>
|
|
constexpr NodeIdNot<T> NodeIdNot<T>::Invalid =
|
|
NodeIdNot<T>(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_
|