mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:11:08 +01:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user