Tidy up and refactor the node stack. (#3685)

Add a type representing a non-discriminated union of IDs. Refactor the
node stack to use it. Plus a few other refactorings aiming to clean up
and simplify the code. The overall goal here is that adding a new kind
of ID, node category, or instruction should only require changing one
place in the node stack rather than a bunch of different changes.

One minor functionality change: crash backtraces now use the correct
type for IDs when dumping the node stack rather than using `InstID`
printing for all but one case.
This commit is contained in:
Richard Smith
2024-02-05 22:07:38 +00:00
committed by GitHub
parent cb849cf165
commit c35e0fea64
2 changed files with 172 additions and 243 deletions
+17 -6
View File
@@ -9,16 +9,27 @@
namespace Carbon::Check {
auto NodeStack::PrintForStackDump(llvm::raw_ostream& output) const -> void {
auto print_id = [&]<Id::Kind Kind>(Id id) {
if constexpr (Kind == Id::Kind::None) {
output << " -> no value";
} else if constexpr (Kind == Id::Kind::Invalid) {
CARBON_FATAL() << "Should not be in node stack";
} else {
output << " -> " << id.As<Kind>();
}
};
output << "NodeStack:\n";
for (auto [i, entry] : llvm::enumerate(stack_)) {
auto parse_node_kind = parse_tree_->node_kind(entry.parse_node);
output << "\t" << i << ".\t" << parse_node_kind;
if (parse_node_kind == Parse::NodeKind::BindingPattern) {
output << " -> " << entry.name_id;
} else {
if (entry.inst_id.is_valid()) {
output << " -> " << entry.inst_id;
}
switch (parse_node_kind) {
#define CARBON_PARSE_NODE_KIND(Kind) \
case Parse::NodeKind::Kind: \
print_id.operator()<ParseNodeKindToIdKind(Parse::NodeKind::Kind)>( \
entry.id); \
break;
#include "toolchain/parse/node_kind.def"
}
output << "\n";
}