Rename parse_node -> node_id (#3760)

This was previously discussed at
https://discord.com/channels/655572317891461132/655578254970716160/1209975051588210729.
I'm initiating this mainly because we typically use "id" suffixes to
indicate an `IdBase` being passed around and the non-id suffix of
`parse_node` suggests at it carrying more data than it actually does.
There used to be more reason for avoiding `node_id` because
`SemIR::InstId` used to be named `NodeId`, but that's no longer
necessary. As a consequence, I'd like to rename `parse_node` to more
precisely reflect its type.

In full, this is doing:

```
parse_node_kind -> node_kind
parse_node -> node_id
ParseNodeCategory -> NodeCategory
ParseNodeKind -> NodeKind
ParseNode -> NodeId
```

This is primarily in check and sem_ir, but with some `parse_node_kind`
references in parse too.

Pluralization is consistent with name forms on both sides, so that
wasn't part of my replacements.
This commit is contained in:
Jon Ross-Perkins
2024-03-09 00:21:29 +00:00
committed by GitHub
parent a1317e61e0
commit 86a7c9ff45
57 changed files with 1050 additions and 1115 deletions
+88 -96
View File
@@ -83,11 +83,11 @@ class IdUnion {
//
// Pop APIs will run basic verification:
//
// - If receiving a Parse::NodeKind, verify that the parse_node being popped has
// - If receiving a Parse::NodeKind, verify that the node_id being popped has
// that kind. Similarly, if receiving a Parse::NodeCategory, make sure the
// of the popped parse_node overlaps that category.
// of the popped node_id overlaps that category.
// - Validates the kind of id data in the node based on the kind or category of
// the parse_node.
// the node_id.
//
// These should be assumed API constraints unless otherwise mentioned on a
// method. The main exception is PopAndIgnore, which doesn't do verification.
@@ -99,36 +99,36 @@ class NodeStack {
// Pushes a solo parse tree node onto the stack. Used when there is no
// IR generated by the node.
auto Push(Parse::NodeId parse_node) -> void {
auto kind = parse_tree_->node_kind(parse_node);
CARBON_CHECK(ParseNodeKindToIdKind(kind) == Id::Kind::None)
auto Push(Parse::NodeId node_id) -> void {
auto kind = parse_tree_->node_kind(node_id);
CARBON_CHECK(NodeKindToIdKind(kind) == Id::Kind::None)
<< "Parse kind expects an Id: " << kind;
CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind
<< " -> <none>\n";
CARBON_CHECK(stack_.size() < (1 << 20))
<< "Excessive stack size: likely infinite loop";
stack_.push_back(Entry{parse_node, Id()});
stack_.push_back(Entry{node_id, Id()});
}
// Pushes a parse tree node onto the stack with an ID.
template <typename IdT>
auto Push(Parse::NodeId parse_node, IdT id) -> void {
auto kind = parse_tree_->node_kind(parse_node);
CARBON_CHECK(ParseNodeKindToIdKind(kind) == Id::KindFor<IdT>())
auto Push(Parse::NodeId node_id, IdT id) -> void {
auto kind = parse_tree_->node_kind(node_id);
CARBON_CHECK(NodeKindToIdKind(kind) == Id::KindFor<IdT>())
<< "Parse kind expected a different IdT: " << kind << " -> " << id
<< "\n";
CARBON_CHECK(id.is_valid()) << "Push called with invalid id: "
<< parse_tree_->node_kind(parse_node);
CARBON_CHECK(id.is_valid())
<< "Push called with invalid id: " << parse_tree_->node_kind(node_id);
CARBON_VLOG() << "Node Push " << stack_.size() << ": " << kind << " -> "
<< id << "\n";
CARBON_CHECK(stack_.size() < (1 << 20))
<< "Excessive stack size: likely infinite loop";
stack_.push_back(Entry{parse_node, Id(id)});
stack_.push_back(Entry{node_id, Id(id)});
}
// Returns whether there is a node of the specified kind on top of the stack.
auto PeekIs(Parse::NodeKind kind) const -> bool {
return !stack_.empty() && PeekParseNodeKind() == kind;
return !stack_.empty() && PeekNodeKind() == kind;
}
// Returns whether there is a node of the specified kind on top of the stack.
@@ -141,7 +141,7 @@ class NodeStack {
// Returns whether the node on the top of the stack has an overlapping
// category.
auto PeekIs(Parse::NodeCategory category) const -> bool {
return !stack_.empty() && !!(PeekParseNodeKind().category() & category);
return !stack_.empty() && !!(PeekNodeKind().category() & category);
}
// Returns whether the node on the top of the stack has an overlapping
@@ -154,8 +154,8 @@ class NodeStack {
// Returns whether there is a name on top of the stack.
auto PeekIsName() const -> bool {
return !stack_.empty() && ParseNodeKindToIdKind(PeekParseNodeKind()) ==
Id::KindFor<SemIR::NameId>();
return !stack_.empty() &&
NodeKindToIdKind(PeekNodeKind()) == Id::KindFor<SemIR::NameId>();
}
// Returns whether the *next* node on the stack is a given kind. This doesn't
@@ -165,7 +165,7 @@ class NodeStack {
template <const Parse::NodeKind& RequiredParseKind>
auto PeekNextIs() const -> bool {
CARBON_CHECK(stack_.size() >= 2);
return parse_tree_->node_kind(stack_[stack_.size() - 2].parse_node) ==
return parse_tree_->node_kind(stack_[stack_.size() - 2].node_id) ==
RequiredParseKind;
}
@@ -173,110 +173,107 @@ class NodeStack {
auto PopAndIgnore() -> void {
Entry back = stack_.pop_back_val();
CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
<< parse_tree_->node_kind(back.parse_node)
<< " -> <ignored>\n";
<< parse_tree_->node_kind(back.node_id) << " -> <ignored>\n";
}
// Pops the top of the stack and returns the parse_node.
// Pops the top of the stack and returns the node_id.
template <const Parse::NodeKind& RequiredParseKind>
auto PopForSoloParseNode() -> Parse::NodeIdForKind<RequiredParseKind> {
auto PopForSoloNodeId() -> Parse::NodeIdForKind<RequiredParseKind> {
Entry back = PopEntry<SemIR::InstId>();
RequireIdKind(RequiredParseKind, Id::Kind::None);
RequireParseKind<RequiredParseKind>(back.parse_node);
return Parse::NodeIdForKind<RequiredParseKind>(back.parse_node);
RequireParseKind<RequiredParseKind>(back.node_id);
return Parse::NodeIdForKind<RequiredParseKind>(back.node_id);
}
// Pops the top of the stack if it is the given kind, and returns the
// parse_node. Otherwise, returns std::nullopt.
// node_id. Otherwise, returns std::nullopt.
template <const Parse::NodeKind& RequiredParseKind>
auto PopForSoloParseNodeIf()
auto PopForSoloNodeIdIf()
-> std::optional<Parse::NodeIdForKind<RequiredParseKind>> {
if (PeekIs<RequiredParseKind>()) {
return PopForSoloParseNode<RequiredParseKind>();
return PopForSoloNodeId<RequiredParseKind>();
}
return std::nullopt;
}
// Pops the top of the stack.
template <const Parse::NodeKind& RequiredParseKind>
auto PopAndDiscardSoloParseNode() -> void {
PopForSoloParseNode<RequiredParseKind>();
auto PopAndDiscardSoloNodeId() -> void {
PopForSoloNodeId<RequiredParseKind>();
}
// Pops the top of the stack if it is the given kind. Returns `true` if a node
// was popped.
template <const Parse::NodeKind& RequiredParseKind>
auto PopAndDiscardSoloParseNodeIf() -> bool {
auto PopAndDiscardSoloNodeIdIf() -> bool {
if (!PeekIs<RequiredParseKind>()) {
return false;
}
PopForSoloParseNode<RequiredParseKind>();
PopForSoloNodeId<RequiredParseKind>();
return true;
}
// Pops an expression from the top of the stack and returns the parse_node and
// Pops an expression from the top of the stack and returns the node_id and
// the ID.
auto PopExprWithParseNode() -> std::pair<Parse::AnyExprId, SemIR::InstId>;
auto PopExprWithNodeId() -> std::pair<Parse::AnyExprId, SemIR::InstId>;
// Pops a pattern from the top of the stack and returns the parse_node and
// Pops a pattern from the top of the stack and returns the node_id and
// the ID.
auto PopPatternWithParseNode() -> std::pair<Parse::NodeId, SemIR::InstId> {
return PopWithParseNode<SemIR::InstId>();
auto PopPatternWithNodeId() -> std::pair<Parse::NodeId, SemIR::InstId> {
return PopWithNodeId<SemIR::InstId>();
}
// Pops a name from the top of the stack and returns the parse_node and
// Pops a name from the top of the stack and returns the node_id and
// the ID.
auto PopNameWithParseNode() -> std::pair<Parse::NodeId, SemIR::NameId> {
return PopWithParseNode<SemIR::NameId>();
auto PopNameWithNodeId() -> std::pair<Parse::NodeId, SemIR::NameId> {
return PopWithNodeId<SemIR::NameId>();
}
// Pops the top of the stack and returns the parse_node and the ID.
// Pops the top of the stack and returns the node_id and the ID.
template <const Parse::NodeKind& RequiredParseKind>
auto PopWithParseNode() -> auto {
auto PopWithNodeId() -> auto {
auto id = Peek<RequiredParseKind>();
Parse::NodeIdForKind<RequiredParseKind> parse_node(
stack_.pop_back_val().parse_node);
return std::make_pair(parse_node, id);
Parse::NodeIdForKind<RequiredParseKind> node_id(
stack_.pop_back_val().node_id);
return std::make_pair(node_id, id);
}
// Pops the top of the stack and returns the parse_node and the ID.
// Pops the top of the stack and returns the node_id and the ID.
template <Parse::NodeCategory RequiredParseCategory>
auto PopWithParseNode() -> auto {
auto PopWithNodeId() -> auto {
auto id = Peek<RequiredParseCategory>();
Parse::NodeIdInCategory<RequiredParseCategory> parse_node(
stack_.pop_back_val().parse_node);
return std::make_pair(parse_node, id);
Parse::NodeIdInCategory<RequiredParseCategory> node_id(
stack_.pop_back_val().node_id);
return std::make_pair(node_id, id);
}
// Pops an expression from the top of the stack and returns the ID.
// Expressions always map Parse::NodeCategory::Expr nodes to SemIR::InstId.
auto PopExpr() -> SemIR::InstId { return PopExprWithParseNode().second; }
auto PopExpr() -> SemIR::InstId { return PopExprWithNodeId().second; }
// Pops a pattern from the top of the stack and returns the ID.
// Patterns map multiple Parse::NodeKinds to SemIR::InstId always.
auto PopPattern() -> SemIR::InstId {
return PopPatternWithParseNode().second;
}
auto PopPattern() -> SemIR::InstId { return PopPatternWithNodeId().second; }
// Pops a name from the top of the stack and returns the ID.
auto PopName() -> SemIR::NameId { return PopNameWithParseNode().second; }
auto PopName() -> SemIR::NameId { return PopNameWithNodeId().second; }
// Pops the top of the stack and returns the ID.
template <const Parse::NodeKind& RequiredParseKind>
auto Pop() -> auto {
return PopWithParseNode<RequiredParseKind>().second;
return PopWithNodeId<RequiredParseKind>().second;
}
// Pops the top of the stack and returns the ID.
template <Parse::NodeCategory RequiredParseCategory>
auto Pop() -> auto {
return PopWithParseNode<RequiredParseCategory>().second;
return PopWithNodeId<RequiredParseCategory>().second;
}
// Pops the top of the stack and returns the ID.
template <typename IdT>
auto Pop() -> IdT {
return PopWithParseNode<IdT>().second;
return PopWithNodeId<IdT>().second;
}
// Pops the top of the stack if it has the given kind, and returns the ID.
@@ -299,47 +296,43 @@ class NodeStack {
return std::nullopt;
}
// Pops the top of the stack and returns the parse_node and the ID if it is
// Pops the top of the stack and returns the node_id and the ID if it is
// of the specified kind.
template <const Parse::NodeKind& RequiredParseKind>
auto PopWithParseNodeIf()
-> std::pair<Parse::NodeIdForKind<RequiredParseKind>,
decltype(PopIf<RequiredParseKind>())> {
auto PopWithNodeIdIf() -> std::pair<Parse::NodeIdForKind<RequiredParseKind>,
decltype(PopIf<RequiredParseKind>())> {
if (!PeekIs<RequiredParseKind>()) {
return {Parse::NodeId::Invalid, std::nullopt};
}
return PopWithParseNode<RequiredParseKind>();
return PopWithNodeId<RequiredParseKind>();
}
// Pops the top of the stack and returns the parse_node and the ID if it is
// Pops the top of the stack and returns the node_id and the ID if it is
// of the specified category.
template <Parse::NodeCategory RequiredParseCategory>
auto PopWithParseNodeIf()
auto PopWithNodeIdIf()
-> std::pair<Parse::NodeIdInCategory<RequiredParseCategory>,
decltype(PopIf<RequiredParseCategory>())> {
if (!PeekIs<RequiredParseCategory>()) {
return {Parse::NodeId::Invalid, std::nullopt};
}
return PopWithParseNode<RequiredParseCategory>();
return PopWithNodeId<RequiredParseCategory>();
}
// Peeks at the parse node of the top of the node stack.
auto PeekParseNode() const -> Parse::NodeId {
return stack_.back().parse_node;
}
auto PeekNodeId() const -> Parse::NodeId { return stack_.back().node_id; }
// Peeks at the kind of the parse node of the top of the node stack.
auto PeekParseNodeKind() const -> Parse::NodeKind {
return parse_tree_->node_kind(PeekParseNode());
auto PeekNodeKind() const -> Parse::NodeKind {
return parse_tree_->node_kind(PeekNodeId());
}
// Peeks at the ID associated with the top of the name stack.
template <const Parse::NodeKind& RequiredParseKind>
auto Peek() const -> auto {
Entry back = stack_.back();
RequireParseKind<RequiredParseKind>(back.parse_node);
constexpr Id::Kind RequiredIdKind =
ParseNodeKindToIdKind(RequiredParseKind);
RequireParseKind<RequiredParseKind>(back.node_id);
constexpr Id::Kind RequiredIdKind = NodeKindToIdKind(RequiredParseKind);
return Peek<RequiredIdKind>();
}
@@ -347,9 +340,9 @@ class NodeStack {
template <Parse::NodeCategory RequiredParseCategory>
auto Peek() const -> auto {
Entry back = stack_.back();
RequireParseCategory<RequiredParseCategory>(back.parse_node);
RequireParseCategory<RequiredParseCategory>(back.node_id);
constexpr std::optional<Id::Kind> RequiredIdKind =
ParseNodeCategoryToIdKind(RequiredParseCategory, false);
NodeCategoryToIdKind(RequiredParseCategory, false);
static_assert(RequiredIdKind.has_value());
return Peek<*RequiredIdKind>();
}
@@ -364,8 +357,8 @@ class NodeStack {
// An ID that can be associated with a parse node.
//
// Each parse node kind has a corresponding Id::Kind indicating which kind of
// ID is stored, computed by ParseNodeKindToIdKind. Id::Kind::None indicates
// that the parse node has no associated ID, in which case the *SoloParseNode
// ID is stored, computed by NodeKindToIdKind. Id::Kind::None indicates
// that the parse node has no associated ID, in which case the *SoloNodeId
// functions should be used to push and pop it. Id::Kind::Invalid indicates
// that the parse node should not appear in the node stack at all.
using Id = IdUnion<SemIR::InstId, SemIR::InstBlockId, SemIR::FunctionId,
@@ -375,7 +368,7 @@ class NodeStack {
// An entry in stack_.
struct Entry {
// The parse node associated with the stack entry.
Parse::NodeId parse_node;
Parse::NodeId node_id;
// The ID associated with this parse node. The kind of ID is determined by
// the kind of the parse node, so a separate discriminiator is not needed.
@@ -385,8 +378,8 @@ class NodeStack {
// Translate a parse node category to the enum ID kind it should always
// provide, if it is consistent.
static constexpr auto ParseNodeCategoryToIdKind(Parse::NodeCategory category,
bool for_node_kind)
static constexpr auto NodeCategoryToIdKind(Parse::NodeCategory category,
bool for_node_kind)
-> std::optional<Id::Kind> {
std::optional<Id::Kind> result;
auto set_id_if_category_is = [&](Parse::NodeCategory cat, Id::Kind kind) {
@@ -422,7 +415,7 @@ class NodeStack {
using IdKindTableType = std::array<Id::Kind, Parse::NodeKind::ValidCount>;
// Lookup table to implement `ParseNodeKindToIdKind`. Initialized to the
// Lookup table to implement `NodeKindToIdKind`. Initialized to the
// return value of `ComputeIdKindTable()`.
static const IdKindTableType IdKindTable;
@@ -432,7 +425,7 @@ class NodeStack {
auto to_id_kind =
[](const Parse::NodeKind::Definition& node_kind) -> Id::Kind {
if (auto from_category =
ParseNodeCategoryToIdKind(node_kind.category(), true)) {
NodeCategoryToIdKind(node_kind.category(), true)) {
return *from_category;
}
switch (node_kind) {
@@ -498,8 +491,7 @@ class NodeStack {
}
// Translate a parse node kind to the enum ID kind it should always provide.
static constexpr auto ParseNodeKindToIdKind(Parse::NodeKind kind)
-> Id::Kind {
static constexpr auto NodeKindToIdKind(Parse::NodeKind kind) -> Id::Kind {
return IdKindTable[kind.AsInt()];
}
@@ -515,38 +507,38 @@ class NodeStack {
auto PopEntry() -> Entry {
Entry back = stack_.pop_back_val();
CARBON_VLOG() << "Node Pop " << stack_.size() << ": "
<< parse_tree_->node_kind(back.parse_node) << " -> "
<< parse_tree_->node_kind(back.node_id) << " -> "
<< back.id.template As<IdT>() << "\n";
return back;
}
// Pops the top of the stack and returns the parse_node and the ID.
// Pops the top of the stack and returns the node_id and the ID.
template <typename IdT>
auto PopWithParseNode() -> std::pair<Parse::NodeId, IdT> {
auto PopWithNodeId() -> std::pair<Parse::NodeId, IdT> {
Entry back = PopEntry<IdT>();
RequireIdKind(parse_tree_->node_kind(back.parse_node), Id::KindFor<IdT>());
return {back.parse_node, back.id.template As<IdT>()};
RequireIdKind(parse_tree_->node_kind(back.node_id), Id::KindFor<IdT>());
return {back.node_id, back.id.template As<IdT>()};
}
// Require a Parse::NodeKind be mapped to a particular Id::Kind.
auto RequireIdKind(Parse::NodeKind parse_kind, Id::Kind id_kind) const
-> void {
CARBON_CHECK(ParseNodeKindToIdKind(parse_kind) == id_kind)
CARBON_CHECK(NodeKindToIdKind(parse_kind) == id_kind)
<< "Unexpected Id::Kind mapping for " << parse_kind;
}
// Require an entry to have the given Parse::NodeKind.
template <const Parse::NodeKind& RequiredParseKind>
auto RequireParseKind(Parse::NodeId parse_node) const -> void {
auto actual_kind = parse_tree_->node_kind(parse_node);
auto RequireParseKind(Parse::NodeId node_id) const -> void {
auto actual_kind = parse_tree_->node_kind(node_id);
CARBON_CHECK(RequiredParseKind == actual_kind)
<< "Expected " << RequiredParseKind << ", found " << actual_kind;
}
// Require an entry to have the given Parse::NodeCategory.
template <Parse::NodeCategory RequiredParseCategory>
auto RequireParseCategory(Parse::NodeId parse_node) const -> void {
auto kind = parse_tree_->node_kind(parse_node);
auto RequireParseCategory(Parse::NodeId node_id) const -> void {
auto kind = parse_tree_->node_kind(node_id);
CARBON_CHECK(!!(RequiredParseCategory & kind.category()))
<< "Expected " << RequiredParseCategory << ", found " << kind
<< " with category " << kind.category();
@@ -567,9 +559,9 @@ class NodeStack {
constexpr NodeStack::IdKindTableType NodeStack::IdKindTable =
ComputeIdKindTable();
inline auto NodeStack::PopExprWithParseNode()
inline auto NodeStack::PopExprWithNodeId()
-> std::pair<Parse::AnyExprId, SemIR::InstId> {
return PopWithParseNode<Parse::NodeCategory::Expr>();
return PopWithNodeId<Parse::NodeCategory::Expr>();
}
} // namespace Carbon::Check