Reduce macro use for node kinds to get flexibility (#2354)

As I'm looking at adding builtins, it feels a bit like the types of args will end up scaling close to the number of semantic kinds. Rather than having this lead to a bunch of macros, this approach drops the macros for plain code.

Note, this adds Get functions too -- I'd planned those regardless for type safety, this is just getting ahead of the issue so that the Print function is pretty clean.
This commit is contained in:
Jon Ross-Perkins
2022-10-26 14:19:35 -07:00
committed by GitHub
parent 0755598fa8
commit 722beb8334
3 changed files with 100 additions and 148 deletions
+10 -27
View File
@@ -7,41 +7,24 @@
namespace Carbon {
static auto PrintArgs(llvm::raw_ostream& /*out*/,
const SemanticsNodeArgs::None /*no_args*/) {}
const SemanticsNode::NoArgs /*no_args*/) {}
static auto PrintArgs(llvm::raw_ostream& out, SemanticsNodeId one_node) {
out << one_node;
template <typename T>
static auto PrintArgs(llvm::raw_ostream& out, T arg) {
out << arg;
}
static auto PrintArgs(llvm::raw_ostream& out, SemanticsTwoNodeIds two_nodes) {
out << two_nodes.nodes[0] << ", " << two_nodes.nodes[1];
}
static auto PrintArgs(llvm::raw_ostream& out,
SemanticsIdentifierId identifier) {
out << identifier;
}
static auto PrintArgs(llvm::raw_ostream& out,
SemanticsIntegerLiteralId integer_literal) {
out << integer_literal;
}
static auto PrintArgs(llvm::raw_ostream& out, SemanticsNodeBlockId node_block) {
out << node_block;
}
static auto PrintArgs(llvm::raw_ostream& out,
SemanticsNodeIdAndNodeBlockId node_and_node_block) {
out << node_and_node_block.node << ", " << node_and_node_block.node_block;
template <typename T0, typename T1>
static auto PrintArgs(llvm::raw_ostream& out, std::pair<T0, T1> args) {
out << args.first << ", " << args.second;
}
void SemanticsNode::Print(llvm::raw_ostream& out) const {
out << kind_ << "(";
switch (kind_) {
#define CARBON_SEMANTICS_NODE_KIND(Name, Args) \
case SemanticsNodeKind::Name(): \
PrintArgs(out, one_of_args_.Args); \
#define CARBON_SEMANTICS_NODE_KIND(Name) \
case SemanticsNodeKind::Name(): \
PrintArgs(out, GetAs##Name()); \
break;
#include "toolchain/semantics/semantics_node_kind.def"
}