1 // =================================
  2 // Copyright (c) 2020 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 #include <sngcm/ast/Node.hpp>
  7 #include <sngcm/ast/AstWriter.hpp>
  8 #include <sngcm/ast/AstReader.hpp>
  9 #include <sngcm/ast/BasicType.hpp>
 10 #include <sngcm/ast/Literal.hpp>
 11 #include <sngcm/ast/CompileUnit.hpp>
 12 #include <sngcm/ast/Identifier.hpp>
 13 #include <sngcm/ast/Typedef.hpp>
 14 #include <sngcm/ast/Constant.hpp>
 15 #include <sngcm/ast/Enumeration.hpp>
 16 #include <sngcm/ast/TypeExpr.hpp>
 17 #include <sngcm/ast/Expression.hpp>
 18 #include <sngcm/ast/Parameter.hpp>
 19 #include <sngcm/ast/Template.hpp>
 20 #include <sngcm/ast/Delegate.hpp>
 21 #include <sngcm/ast/Statement.hpp>
 22 #include <sngcm/ast/Function.hpp>
 23 #include <sngcm/ast/Class.hpp>
 24 #include <sngcm/ast/Interface.hpp>
 25 #include <sngcm/ast/Concept.hpp>
 26 #include <sngcm/ast/GlobalVariable.hpp>
 27 #include <sngcm/ast/Comment.hpp>
 28 #include <algorithm>
 29 
 30 namespace sngcm { namespace ast {
 31 
 32 const char* nodeTypeStr[] =
 33 {
 34     "boolNode""sbyteNode""byteNode""shortNode""ushortNode""intNode""uintNode""longNode""ulongNode""floatNode""doubleNode""charNode""wcharNode""ucharNode""voidNode"
 35     "booleanLiteralNode""sbyteLiteralNode""byteLiteralNode""shortLiteralNode""ushortLiteralNode""intLiteralNode""uintLiteralNode""longLiteralNode""ulongLiteralNode"
 36     "floatLiteralNode""doubleLiteralNode""charLiteralNode""wcharLiteralNode""ucharLiteralNode""stringLiteralNode""wstringLiteralNode""ustringLiteralNode""nullLiteralNode""arrayLiteralNode"
 37     "structuredLiteralNode"
 38     "compileUnitNode""namespaceNode""aliasNode""namespaceImportNode""identifierNode""templateIdNode""functionNode"
 39     "classNode""thisInitializerNode""baseInitializerNode""memberInitializerNode""staticConstructorNode""constructorNode""destructorNode""memberFunctionNode""conversionFunctionNode"
 40     "memberVariableNode"
 41     "interfaceNode""delegateNode""classDelegateNode"
 42     "parenthesizedConstraintNode""disjunctiveConstraintNode""conjunctiveConstraintNode""whereConstraintNode""predicateConstraintNode""isConstraintNode""multiParamConstraintNode""typeNameConstraintNode"
 43     "constructorConstraintNode""destructorConstraintNode""memberFunctionConstraintNode""functionConstraintNode"
 44     "sameConstraintNode""derivedConstraintNode""convertibleConstraintNode""explicitlyConvertibleConstraintNode""commonConstraintNode""nonreferenceTypeConstraintNode"
 45     "axiomStatementNode""axiomNode""conceptIdNode""conceptNode"
 46     "sameConceptNode""derivedConceptNode""convertibleConceptNode""ecxplicitlyConvertibleConceptNode""commonConceptNode""nonreferenceTypeConceptNode"
 47     "labelNode""compoundStatementNode""returnStatementNode""ifStatementNode""whileStatementNode""doStatementNode""forStatementNode""breakStatementNode""continueStatementNode"
 48     "gotoStatementNode""constructionStatementNode""deleteStatementNode""destroyStatementNode""assignmentStatementNode""expressionStatementNode""emptyStatementNode"
 49     "rangeForStatementNode""switchStatementNode""caseStatementNode""defaultStatementNode""gotoCaseStatementNode"
 50     "gotoDefaultStatementNode""throwStatementNode""catchNode""tryStatementNode""assertStatementNode"
 51     "typedefNode""constantNode""enumTypeNode""enumConstantNode""parameterNode""templateParameterNode"
 52     "constNode""refNode""arrayNode"
 53     "dotNode""arrowNode""equivalenceNode""implicationNode""disjunctionNode""conjunctionNode""bitOrNode""bitXorNode""bitAndNode""equalNode""notEqualNode""lessNode""greaterNode"
 54     "lessOrEqualNode""greaterOrEqualNode""shiftLeftNode""shiftRightNode"
 55     "addNode""subNode""mulNode""divNode""remNode""notNode""unaryPlusNode""unaryMinusNode""prefixIncrementNode""prefixDecrementNode""complementNode""derefNode""addrOfNode"
 56     "isNode""asNode""indexingNode""invokeNode""postfixIncrementNode""postfixDecrementNode""sizeOfNode""typeNameNode""typeIdNode""castNode""constructNode""newNode""thisNode""baseNode"
 57     "conditionalCompilationDisjunctionNode""conditionalCompilationConjunctionNode""conditionalCompilationNotNode""conditionalCompilationPrimaryNode""conditionalCompilationPartNode"
 58     "conditionalCompilationStatementNode""uuidLiteralNode""cursorIdNode""parenthesizedExpressionNode""globalVariableNode""parenthesizedCondCompExpressionNode""labeledStatementNode"
 59     "commentNode"
 60     "maxNode"
 61 };
 62 
 63 std::string NodeTypeStr(NodeType nodeType)
 64 {
 65     return nodeTypeStr[static_cast<size_t>(nodeType)];
 66 }
 67 
 68 Node::Node(NodeType nodeType_const Span& span_) : nodeType(nodeType_)span(span_)parent(nullptr)
 69 {
 70 }
 71 
 72 Node::~Node()
 73 {
 74 }
 75 
 76 void Node::SetParent(Node* parent_)
 77 {
 78     parent = parent_;
 79 }
 80 
 81 void Node::Write(AstWriter& writer)
 82 {
 83 }
 84 
 85 void Node::Read(AstReader& reader)
 86 {
 87 }
 88 
 89 void Node::SetFullSpan()
 90 {
 91 }
 92 
 93 UnaryNode::UnaryNode(NodeType nodeType_const Span& span_) : Node(nodeType_span_)subject()
 94 {
 95 }
 96 
 97 UnaryNode::UnaryNode(NodeType nodeType_const Span& span_Node* subject_) : Node(nodeType_span_)subject(subject_)
 98 {
 99 }
100 
101 void UnaryNode::Write(AstWriter& writer)
102 {
103     Node::Write(writer);
104     writer.Write(subject.get());
105 }
106 
107 void UnaryNode::Read(AstReader& reader)
108 {
109     Node::Read(reader);
110     subject.reset(reader.ReadNode());
111     subject->SetParent(this);
112 }
113 
114 void UnaryNode::SetFullSpan()
115 {
116     if (subject)
117     {
118         const Span& thisSpan = GetSpan();
119         subject->SetFullSpan();
120         const Span& childSpan = subject->GetSpan();
121         SetSpanStart(std::min(thisSpan.startchildSpan.start));
122         SetSpanEnd(std::max(thisSpan.endchildSpan.end));
123     }
124 }
125 
126 BinaryNode::BinaryNode(NodeType nodeTypeconst Span& span_) : Node(nodeTypespan_)left()right()
127 {
128 }
129 
130 BinaryNode::BinaryNode(NodeType nodeTypeconst Span& span_Node* left_Node* right_) : Node(nodeTypespan_)left(left_)right(right_)
131 {
132     left->SetParent(this);
133     right->SetParent(this);
134 }
135 
136 void BinaryNode::Write(AstWriter& writer)
137 {
138     Node::Write(writer);
139     writer.Write(left.get());
140     writer.Write(right.get());
141 }
142 
143 void BinaryNode::Read(AstReader& reader)
144 {
145     Node::Read(reader);
146     left.reset(reader.ReadNode());
147     left->SetParent(this);
148     right.reset(reader.ReadNode());
149     right->SetParent(this);
150 }
151 
152 void BinaryNode::SetFullSpan()
153 {
154     const Span& thisSpan = GetSpan();
155     left->SetFullSpan();
156     right->SetFullSpan();
157     const Span& leftSpan = left->GetSpan();
158     const Span& rightSpan = right->GetSpan();
159     SetSpanStart(std::min(thisSpan.startstd::min(leftSpan.startrightSpan.start)));
160     SetSpanEnd(std::max(thisSpan.endstd::max(leftSpan.endrightSpan.end)));
161 }
162 
163 NodeCreator::NodeCreator()
164 {
165 }
166 
167 NodeCreator::~NodeCreator()
168 {
169 }
170 
171 template<typename T>
172 class ConcreteNodeCreator public NodeCreator
173 {
174 public:
175     ConcreteNodeCreator() : NodeCreator() {}
176     ConcreteNodeCreator(const ConcreteNodeCreator&) = delete;
177     ConcreteNodeCreator& operator=(const ConcreteNodeCreator&) = delete;
178     Node* CreateNode(const Span& span) override
179     {
180         return new T(span);
181     }
182 };
183 
184 std::unique_ptr<NodeFactory> NodeFactory::instance;
185 
186 void NodeFactory::Init()
187 {
188     instance.reset(new NodeFactory());
189 }
190 
191 void NodeFactory::Done()
192 {
193     instance.reset();
194 }
195 
196 NodeFactory::NodeFactory()
197 {
198     creators.resize(static_cast<size_t>(NodeType::maxNode));
199 
200     Register(NodeType::boolNodenew ConcreteNodeCreator<BoolNode>());
201     Register(NodeType::sbyteNodenew ConcreteNodeCreator<SByteNode>());
202     Register(NodeType::byteNodenew ConcreteNodeCreator<ByteNode>());
203     Register(NodeType::shortNodenew ConcreteNodeCreator<ShortNode>());
204     Register(NodeType::ushortNodenew ConcreteNodeCreator<UShortNode>());
205     Register(NodeType::intNodenew ConcreteNodeCreator<IntNode>());
206     Register(NodeType::uintNodenew ConcreteNodeCreator<UIntNode>());
207     Register(NodeType::longNodenew ConcreteNodeCreator<LongNode>());
208     Register(NodeType::ulongNodenew ConcreteNodeCreator<ULongNode>());
209     Register(NodeType::floatNodenew ConcreteNodeCreator<FloatNode>());
210     Register(NodeType::doubleNodenew ConcreteNodeCreator<DoubleNode>());
211     Register(NodeType::charNodenew ConcreteNodeCreator<CharNode>());
212     Register(NodeType::wcharNodenew ConcreteNodeCreator<WCharNode>());
213     Register(NodeType::ucharNodenew ConcreteNodeCreator<UCharNode>());
214     Register(NodeType::voidNodenew ConcreteNodeCreator<VoidNode>());
215 
216     Register(NodeType::booleanLiteralNodenew ConcreteNodeCreator<BooleanLiteralNode>());
217     Register(NodeType::sbyteLiteralNodenew ConcreteNodeCreator<SByteLiteralNode>());
218     Register(NodeType::byteLiteralNodenew ConcreteNodeCreator<ByteLiteralNode>());
219     Register(NodeType::shortLiteralNodenew ConcreteNodeCreator<ShortLiteralNode>());
220     Register(NodeType::ushortLiteralNodenew ConcreteNodeCreator<UShortLiteralNode>());
221     Register(NodeType::intLiteralNodenew ConcreteNodeCreator<IntLiteralNode>());
222     Register(NodeType::uintLiteralNodenew ConcreteNodeCreator<UIntLiteralNode>());
223     Register(NodeType::longLiteralNodenew ConcreteNodeCreator<LongLiteralNode>());
224     Register(NodeType::ulongLiteralNodenew ConcreteNodeCreator<ULongLiteralNode>());
225     Register(NodeType::floatLiteralNodenew ConcreteNodeCreator<FloatLiteralNode>());
226     Register(NodeType::doubleLiteralNodenew ConcreteNodeCreator<DoubleLiteralNode>());
227     Register(NodeType::charLiteralNodenew ConcreteNodeCreator<CharLiteralNode>());
228     Register(NodeType::wcharLiteralNodenew ConcreteNodeCreator<WCharLiteralNode>());
229     Register(NodeType::ucharLiteralNodenew ConcreteNodeCreator<UCharLiteralNode>());
230     Register(NodeType::stringLiteralNodenew ConcreteNodeCreator<StringLiteralNode>());
231     Register(NodeType::wstringLiteralNodenew ConcreteNodeCreator<WStringLiteralNode>());
232     Register(NodeType::ustringLiteralNodenew ConcreteNodeCreator<UStringLiteralNode>());
233     Register(NodeType::nullLiteralNodenew ConcreteNodeCreator<NullLiteralNode>());
234     Register(NodeType::arrayLiteralNodenew ConcreteNodeCreator<ArrayLiteralNode>());
235     Register(NodeType::structuredLiteralNodenew ConcreteNodeCreator<StructuredLiteralNode>());
236 
237     Register(NodeType::compileUnitNodenew ConcreteNodeCreator<CompileUnitNode>());
238     Register(NodeType::namespaceNodenew ConcreteNodeCreator<NamespaceNode>());
239     Register(NodeType::aliasNodenew ConcreteNodeCreator<AliasNode>());
240     Register(NodeType::namespaceImportNodenew ConcreteNodeCreator<NamespaceImportNode>());
241     Register(NodeType::identifierNodenew ConcreteNodeCreator<IdentifierNode>());
242     Register(NodeType::templateIdNodenew ConcreteNodeCreator<TemplateIdNode>());
243     Register(NodeType::functionNodenew ConcreteNodeCreator<FunctionNode>());
244     Register(NodeType::classNodenew ConcreteNodeCreator<ClassNode>());
245     Register(NodeType::thisInitializerNodenew ConcreteNodeCreator<ThisInitializerNode>());
246     Register(NodeType::baseInitializerNodenew ConcreteNodeCreator<BaseInitializerNode>());
247     Register(NodeType::memberInitializerNodenew ConcreteNodeCreator<MemberInitializerNode>());
248     Register(NodeType::staticConstructorNodenew ConcreteNodeCreator<StaticConstructorNode>());
249     Register(NodeType::constructorNodenew ConcreteNodeCreator<ConstructorNode>());
250     Register(NodeType::destructorNodenew ConcreteNodeCreator<DestructorNode>());
251     Register(NodeType::memberFunctionNodenew ConcreteNodeCreator<MemberFunctionNode>());
252     Register(NodeType::conversionFunctionNodenew ConcreteNodeCreator<ConversionFunctionNode>());
253     Register(NodeType::memberVariableNodenew ConcreteNodeCreator<MemberVariableNode>());
254     Register(NodeType::interfaceNodenew ConcreteNodeCreator<InterfaceNode>());
255     Register(NodeType::delegateNodenew ConcreteNodeCreator<DelegateNode>());
256     Register(NodeType::classDelegateNodenew ConcreteNodeCreator<ClassDelegateNode>());
257 
258     Register(NodeType::parenthesizedConstraintNodenew ConcreteNodeCreator<ParenthesizedConstraintNode>());
259     Register(NodeType::disjunctiveConstraintNodenew ConcreteNodeCreator<DisjunctiveConstraintNode>());
260     Register(NodeType::conjunctiveConstraintNodenew ConcreteNodeCreator<ConjunctiveConstraintNode>());
261     Register(NodeType::whereConstraintNodenew ConcreteNodeCreator<WhereConstraintNode>());
262     Register(NodeType::predicateConstraintNodenew ConcreteNodeCreator<PredicateConstraintNode>());
263     Register(NodeType::isConstraintNodenew ConcreteNodeCreator<IsConstraintNode>());
264     Register(NodeType::multiParamConstraintNodenew ConcreteNodeCreator<MultiParamConstraintNode>());
265     Register(NodeType::typeNameConstraintNodenew ConcreteNodeCreator<TypeNameConstraintNode>());
266     Register(NodeType::constructorConstraintNodenew ConcreteNodeCreator<ConstructorConstraintNode>());
267     Register(NodeType::destructorConstraintNodenew ConcreteNodeCreator<DestructorConstraintNode>());
268     Register(NodeType::memberFunctionConstraintNodenew ConcreteNodeCreator<MemberFunctionConstraintNode>());
269     Register(NodeType::functionConstraintNodenew ConcreteNodeCreator<FunctionConstraintNode>());
270     Register(NodeType::sameConstraintNodenew ConcreteNodeCreator<SameConstraintNode>());
271     Register(NodeType::derivedConstraintNodenew ConcreteNodeCreator<DerivedConstraintNode>());
272     Register(NodeType::convertibleConstraintNodenew ConcreteNodeCreator<ConvertibleConstraintNode>());
273     Register(NodeType::explicitlyConvertibleConstraintNodenew ConcreteNodeCreator<ExplicitlyConvertibleConstraintNode>());
274     Register(NodeType::commonConstraintNodenew ConcreteNodeCreator<CommonConstraintNode>());
275     Register(NodeType::nonreferenceTypeConstraintNodenew ConcreteNodeCreator<NonreferenceTypeConstraintNode>());
276     Register(NodeType::axiomStatementNodenew ConcreteNodeCreator<AxiomStatementNode>());
277     Register(NodeType::axiomNodenew ConcreteNodeCreator<AxiomNode>());
278     Register(NodeType::conceptIdNodenew ConcreteNodeCreator<ConceptIdNode>());
279     Register(NodeType::conceptNodenew ConcreteNodeCreator<ConceptNode>());
280     Register(NodeType::sameConceptNodenew ConcreteNodeCreator<SameConceptNode>());
281     Register(NodeType::derivedConceptNodenew ConcreteNodeCreator<DerivedConceptNode>());
282     Register(NodeType::convertibleConceptNodenew ConcreteNodeCreator<ConvertibleConceptNode>());
283     Register(NodeType::explicitlyConvertibleConceptNodenew ConcreteNodeCreator<ExplicitlyConvertibleConceptNode>());
284     Register(NodeType::commonConceptNodenew ConcreteNodeCreator<CommonConceptNode>());
285     Register(NodeType::nonreferenceTypeConceptNodenew ConcreteNodeCreator<NonreferenceTypeConceptNode>());
286 
287     Register(NodeType::labelNodenew ConcreteNodeCreator<LabelNode>());
288     Register(NodeType::labeledStatementNodenew ConcreteNodeCreator<LabeledStatementNode>());
289     Register(NodeType::compoundStatementNodenew ConcreteNodeCreator<CompoundStatementNode>());
290     Register(NodeType::returnStatementNodenew ConcreteNodeCreator<ReturnStatementNode>());
291     Register(NodeType::ifStatementNodenew ConcreteNodeCreator<IfStatementNode>());
292     Register(NodeType::whileStatementNodenew ConcreteNodeCreator<WhileStatementNode>());
293     Register(NodeType::doStatementNodenew ConcreteNodeCreator<DoStatementNode>());
294     Register(NodeType::forStatementNodenew ConcreteNodeCreator<ForStatementNode>());
295     Register(NodeType::breakStatementNodenew ConcreteNodeCreator<BreakStatementNode>());
296     Register(NodeType::continueStatementNodenew ConcreteNodeCreator<ContinueStatementNode>());
297     Register(NodeType::gotoStatementNodenew ConcreteNodeCreator<GotoStatementNode>());
298     Register(NodeType::constructionStatementNodenew ConcreteNodeCreator<ConstructionStatementNode>());
299     Register(NodeType::deleteStatementNodenew ConcreteNodeCreator<DeleteStatementNode>());
300     Register(NodeType::destroyStatementNodenew ConcreteNodeCreator<DestroyStatementNode>());
301     Register(NodeType::assignmentStatementNodenew ConcreteNodeCreator<AssignmentStatementNode>());
302     Register(NodeType::expressionStatementNodenew ConcreteNodeCreator<ExpressionStatementNode>());
303     Register(NodeType::emptyStatementNodenew ConcreteNodeCreator<EmptyStatementNode>());
304     Register(NodeType::rangeForStatementNodenew ConcreteNodeCreator<RangeForStatementNode>());
305     Register(NodeType::switchStatementNodenew ConcreteNodeCreator<SwitchStatementNode>());
306     Register(NodeType::caseStatementNodenew ConcreteNodeCreator<CaseStatementNode>());
307     Register(NodeType::defaultStatementNodenew ConcreteNodeCreator<DefaultStatementNode>());
308     Register(NodeType::gotoCaseStatementNodenew ConcreteNodeCreator<GotoCaseStatementNode>());
309     Register(NodeType::gotoDefaultStatementNodenew ConcreteNodeCreator<GotoDefaultStatementNode>());
310     Register(NodeType::throwStatementNodenew ConcreteNodeCreator<ThrowStatementNode>());
311     Register(NodeType::catchNodenew ConcreteNodeCreator<CatchNode>());
312     Register(NodeType::tryStatementNodenew ConcreteNodeCreator<TryStatementNode>());
313     Register(NodeType::assertStatementNodenew ConcreteNodeCreator<AssertStatementNode>());
314 
315     Register(NodeType::typedefNodenew ConcreteNodeCreator<TypedefNode>());
316     Register(NodeType::constantNodenew ConcreteNodeCreator<ConstantNode>());
317     Register(NodeType::enumTypeNodenew ConcreteNodeCreator<EnumTypeNode>());
318     Register(NodeType::enumConstantNodenew ConcreteNodeCreator<EnumConstantNode>());
319     Register(NodeType::parameterNodenew ConcreteNodeCreator<ParameterNode>());
320     Register(NodeType::templateParameterNodenew ConcreteNodeCreator<TemplateParameterNode>());
321 
322     Register(NodeType::constNodenew ConcreteNodeCreator<ConstNode>());
323     Register(NodeType::lvalueRefNodenew ConcreteNodeCreator<LValueRefNode>());
324     Register(NodeType::rvalueRefNodenew ConcreteNodeCreator<RValueRefNode>());
325     Register(NodeType::pointerNodenew ConcreteNodeCreator<PointerNode>());
326     Register(NodeType::dotNodenew ConcreteNodeCreator<DotNode>());
327     Register(NodeType::arrowNodenew ConcreteNodeCreator<ArrowNode>());
328     Register(NodeType::arrayNodenew ConcreteNodeCreator<ArrayNode>());
329 
330     Register(NodeType::equivalenceNodenew ConcreteNodeCreator<EquivalenceNode>());
331     Register(NodeType::implicationNodenew ConcreteNodeCreator<ImplicationNode>());
332     Register(NodeType::disjunctionNodenew ConcreteNodeCreator<DisjunctionNode>());
333     Register(NodeType::conjunctionNodenew ConcreteNodeCreator<ConjunctionNode>());
334     Register(NodeType::bitOrNodenew ConcreteNodeCreator<BitOrNode>());
335     Register(NodeType::bitXorNodenew ConcreteNodeCreator<BitXorNode>());
336     Register(NodeType::bitAndNodenew ConcreteNodeCreator<BitAndNode>());
337     Register(NodeType::equalNodenew ConcreteNodeCreator<EqualNode>());
338     Register(NodeType::notEqualNodenew ConcreteNodeCreator<NotEqualNode>());
339     Register(NodeType::lessNodenew ConcreteNodeCreator<LessNode>());
340     Register(NodeType::greaterNodenew ConcreteNodeCreator<GreaterNode>());
341     Register(NodeType::lessOrEqualNodenew ConcreteNodeCreator<LessOrEqualNode>());
342     Register(NodeType::greaterOrEqualNodenew ConcreteNodeCreator<GreaterOrEqualNode>());
343     Register(NodeType::shiftLeftNodenew ConcreteNodeCreator<ShiftLeftNode>());
344     Register(NodeType::shiftRightNodenew ConcreteNodeCreator<ShiftRightNode>());
345     Register(NodeType::addNodenew ConcreteNodeCreator<AddNode>());
346     Register(NodeType::subNodenew ConcreteNodeCreator<SubNode>());
347     Register(NodeType::mulNodenew ConcreteNodeCreator<MulNode>());
348     Register(NodeType::divNodenew ConcreteNodeCreator<DivNode>());
349     Register(NodeType::remNodenew ConcreteNodeCreator<RemNode>());
350     Register(NodeType::notNodenew ConcreteNodeCreator<NotNode>());
351     Register(NodeType::unaryPlusNodenew ConcreteNodeCreator<UnaryPlusNode>());
352     Register(NodeType::unaryMinusNodenew ConcreteNodeCreator<UnaryMinusNode>());
353     Register(NodeType::prefixIncrementNodenew ConcreteNodeCreator<PrefixIncrementNode>());
354     Register(NodeType::prefixDecrementNodenew ConcreteNodeCreator<PrefixDecrementNode>());
355     Register(NodeType::complementNodenew ConcreteNodeCreator<ComplementNode>());
356     Register(NodeType::derefNodenew ConcreteNodeCreator<DerefNode>());
357     Register(NodeType::addrOfNodenew ConcreteNodeCreator<AddrOfNode>());
358     Register(NodeType::isNodenew ConcreteNodeCreator<IsNode>());
359     Register(NodeType::asNodenew ConcreteNodeCreator<AsNode>());
360     Register(NodeType::indexingNodenew ConcreteNodeCreator<IndexingNode>());
361     Register(NodeType::invokeNodenew ConcreteNodeCreator<InvokeNode>());
362     Register(NodeType::postfixIncrementNodenew ConcreteNodeCreator<PostfixIncrementNode>());
363     Register(NodeType::postfixDecrementNodenew ConcreteNodeCreator<PostfixDecrementNode>());
364     Register(NodeType::sizeOfNodenew ConcreteNodeCreator<SizeOfNode>());
365     Register(NodeType::typeNameNodenew ConcreteNodeCreator<TypeNameNode>());
366     Register(NodeType::typeIdNodenew ConcreteNodeCreator<TypeIdNode>());
367     Register(NodeType::castNodenew ConcreteNodeCreator<CastNode>());
368     Register(NodeType::constructNodenew ConcreteNodeCreator<ConstructNode>());
369     Register(NodeType::newNodenew ConcreteNodeCreator<NewNode>());
370     Register(NodeType::thisNodenew ConcreteNodeCreator<ThisNode>());
371     Register(NodeType::baseNodenew ConcreteNodeCreator<BaseNode>());
372     Register(NodeType::conditionalCompilationDisjunctionNodenew ConcreteNodeCreator<ConditionalCompilationDisjunctionNode>());
373     Register(NodeType::conditionalCompilationConjunctionNodenew ConcreteNodeCreator<ConditionalCompilationConjunctionNode>());
374     Register(NodeType::conditionalCompilationNotNodenew ConcreteNodeCreator<ConditionalCompilationNotNode>());
375     Register(NodeType::conditionalCompilationPrimaryNodenew ConcreteNodeCreator<ConditionalCompilationPrimaryNode>());
376     Register(NodeType::conditionalCompilationPartNodenew ConcreteNodeCreator<ConditionalCompilationPartNode>());
377     Register(NodeType::parenthesizedCondCompExpressionNodenew ConcreteNodeCreator<ParenthesizedConditionalCompilationExpressionNode>());
378     Register(NodeType::conditionalCompilationStatementNodenew ConcreteNodeCreator<ConditionalCompilationStatementNode>());
379     Register(NodeType::uuidLiteralNodenew ConcreteNodeCreator<UuidLiteralNode>());
380     Register(NodeType::cursorIdNodenew ConcreteNodeCreator<CursorIdNode>());
381     Register(NodeType::parenthesizedExpressionNodenew ConcreteNodeCreator<ParenthesizedExpressionNode>());
382     Register(NodeType::globalVariableNodenew ConcreteNodeCreator<GlobalVariableNode>());
383     Register(NodeType::commentNodenew ConcreteNodeCreator<CommentNode>());
384 }
385 
386 void NodeFactory::Register(NodeType nodeTypeNodeCreator* creator)
387 {
388     creators[static_cast<size_t>(nodeType)] = std::unique_ptr<NodeCreator>(creator);
389 }
390 
391 Node* NodeFactory::CreateNode(NodeType nodeTypeconst Span& span)
392 {
393     const std::std::unique_ptr<NodeCreator>&creator=creators[static_cast<size_t>(nodeType)];
394     if (creator)
395     {
396         Node* value = creator->CreateNode(span);
397         if (value)
398         {
399             return value;
400         }
401         else
402         {
403             throw std::runtime_error("could not create node");
404         }
405     }
406     else
407     {
408         throw std::runtime_error("no creator for node type '" + NodeTypeStr(nodeType) + "'");
409     }
410 }
411 
412 void NodeInit()
413 {
414     NodeFactory::Init();
415 }
416 
417 void NodeDone()
418 {
419     NodeFactory::Done();
420 }
421 
422 } } // namespace sngcm::ast