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