Refactor SemanticsNode factory functions into factory templates. (#2711)

I'm trying to reduce the amount of per-SemanticsNodeKind boilerplate, and make mistakes (e.g., SemanticsNodeKind not matching the Make name, misplacing the type, or Get/Make type mismatches) easier to see.

I could've done this with (more) macros, but felt that the template approach was reasonable enough and likely easier to understand/debug. I'm not sure whether there's more that I could be doing with variadics to reduce the amount of factory code, but this feels good right now.
This commit is contained in:
Jon Ross-Perkins
2023-03-27 11:07:49 -07:00
committed by GitHub
parent 4fa11af13f
commit 7fc203c536
7 changed files with 201 additions and 185 deletions
+5 -5
View File
@@ -16,8 +16,8 @@ auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
SemanticsIR semantics(/*builtin_ir=*/nullptr);
semantics.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
#define CARBON_SEMANTICS_BUILTIN_KIND(Name, Type, ...) \
semantics.nodes_.push_back(SemanticsNode::MakeBuiltin( \
#define CARBON_SEMANTICS_BUILTIN_KIND(Name, Type, ...) \
semantics.nodes_.push_back(SemanticsNode::Builtin::Make( \
SemanticsBuiltinKind::Name, SemanticsNodeId::Builtin##Type));
#include "toolchain/semantics/semantics_builtin_kind.def"
@@ -44,9 +44,9 @@ auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
// We can reuse the type node ID because the offsets of cross-references
// will be the same in this IR.
auto type = builtin_ir.nodes_[i].type();
semantics.nodes_[i] =
SemanticsNode::MakeCrossReference(type, BuiltinIR, SemanticsNodeId(i));
auto type = builtin_ir.nodes_[i].type_id();
semantics.nodes_[i] = SemanticsNode::CrossReference::Make(
type, BuiltinIR, SemanticsNodeId(i));
}
ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);