mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 06:04:56 +01:00
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:
@@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user