Files
carbon-lang/toolchain/check/node_stack.cpp
T
Richard Smith c35e0fea64 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.
2024-02-05 22:07:38 +00:00

39 lines
1.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
#include "toolchain/check/node_stack.h"
#include "llvm/ADT/STLExtras.h"
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;
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";
}
}
} // namespace Carbon::Check