1
2
3
4
5
6 #include <sngcm/ast/Statement.hpp>
7 #include <sngcm/ast/Identifier.hpp>
8 #include <sngcm/ast/Visitor.hpp>
9
10 namespace sngcm { namespace ast {
11
12 LabelNode::LabelNode(const Span& span_) : Node(NodeType::labelNode, span_)
13 {
14 }
15
16 LabelNode::LabelNode(const Span& span_, const std::u32string& label_) : Node(NodeType::labelNode, span_), label(label_)
17 {
18 }
19
20 Node* LabelNode::Clone(CloneContext& cloneContext) const
21 {
22 return new LabelNode(GetSpan(), label);
23 }
24
25 void LabelNode::Accept(Visitor& visitor)
26 {
27 visitor.Visit(*this);
28 }
29
30 void LabelNode::Write(AstWriter& writer)
31 {
32 Node::Write(writer);
33 writer.GetBinaryWriter().Write(label);
34 }
35
36 void LabelNode::Read(AstReader& reader)
37 {
38 Node::Read(reader);
39 label = reader.GetBinaryReader().ReadUtf32String();
40 }
41
42 StatementNode::StatementNode(NodeType nodeType_, const Span& span_) : Node(nodeType_, span_)
43 {
44 }
45
46 void StatementNode::Write(AstWriter& writer)
47 {
48 Node::Write(writer);
49 }
50
51 void StatementNode::Read(AstReader& reader)
52 {
53 Node::Read(reader);
54 }
55
56 LabeledStatementNode::LabeledStatementNode(const Span& span_) : StatementNode(NodeType::labeledStatementNode, span_)
57 {
58 }
59
60 LabeledStatementNode::LabeledStatementNode(const Span& span_, StatementNode* stmt_) : StatementNode(NodeType::labeledStatementNode, span_), stmt(stmt_)
61 {
62 stmt->SetParent(this);
63 }
64
65 Node* LabeledStatementNode::Clone(CloneContext& cloneContext) const
66 {
67 LabeledStatementNode* clone = new LabeledStatementNode(GetSpan(), static_cast<StatementNode*>(stmt->Clone(cloneContext)));
68 clone->SetLabelNode(static_cast<LabelNode*>(labelNode->Clone(cloneContext)));
69 return clone;
70 }
71
72 void LabeledStatementNode::Accept(Visitor& visitor)
73 {
74 visitor.Visit(*this);
75 }
76
77 void LabeledStatementNode::Write(AstWriter& writer)
78 {
79 StatementNode::Write(writer);
80 writer.Write(labelNode.get());
81 writer.Write(stmt.get());
82 }
83
84 void LabeledStatementNode::Read(AstReader& reader)
85 {
86 StatementNode::Read(reader);
87 labelNode.reset(reader.ReadLabelNode());
88 labelNode->SetParent(this);
89 stmt.reset(reader.ReadStatementNode());
90 stmt->SetParent(this);
91 }
92
93 void LabeledStatementNode::SetLabelNode(LabelNode* labelNode_)
94 {
95 labelNode.reset(labelNode_);
96 labelNode->SetParent(this);
97 }
98
99 CompoundStatementNode::CompoundStatementNode(const Span& span_) : StatementNode(NodeType::compoundStatementNode, span_), statements(), beginBraceSpan(), endBraceSpan()
100 {
101 }
102
103 Node* CompoundStatementNode::Clone(CloneContext& cloneContext) const
104 {
105 CompoundStatementNode* clone = new CompoundStatementNode(GetSpan());
106 int n = statements.Count();
107 for (int i = 0; i < n; ++i)
108 {
109 StatementNode* statement = statements[i];
110 clone->AddStatement(static_cast<StatementNode*>(statement->Clone(cloneContext)));
111 }
112 clone->beginBraceSpan = beginBraceSpan;
113 clone->endBraceSpan = endBraceSpan;
114 return clone;
115 }
116
117 void CompoundStatementNode::Accept(Visitor& visitor)
118 {
119 visitor.Visit(*this);
120 }
121
122 void CompoundStatementNode::Write(AstWriter& writer)
123 {
124 StatementNode::Write(writer);
125 statements.Write(writer);
126 writer.Write(beginBraceSpan);
127 writer.Write(endBraceSpan);
128 }
129
130 void CompoundStatementNode::Read(AstReader& reader)
131 {
132 StatementNode::Read(reader);
133 statements.Read(reader);
134 statements.SetParent(this);
135 beginBraceSpan = reader.ReadSpan();
136 endBraceSpan = reader.ReadSpan();
137 }
138
139 void CompoundStatementNode::AddStatement(StatementNode* statement)
140 {
141 statement->SetParent(this);
142 statements.Add(statement);
143 }
144
145 ReturnStatementNode::ReturnStatementNode(const Span& span_) : StatementNode(NodeType::returnStatementNode, span_), expression()
146 {
147 }
148
149 ReturnStatementNode::ReturnStatementNode(const Span& span_, Node* expression_) : StatementNode(NodeType::returnStatementNode, span_), expression(expression_)
150 {
151 if (expression)
152 {
153 expression->SetParent(this);
154 }
155 }
156
157 Node* ReturnStatementNode::Clone(CloneContext& cloneContext) const
158 {
159 Node* clonedExpression = nullptr;
160 if (expression)
161 {
162 clonedExpression = expression->Clone(cloneContext);
163 }
164 ReturnStatementNode* clone = new ReturnStatementNode(GetSpan(), clonedExpression);
165 return clone;
166 }
167
168 void ReturnStatementNode::Accept(Visitor& visitor)
169 {
170 visitor.Visit(*this);
171 }
172
173 void ReturnStatementNode::Write(AstWriter& writer)
174 {
175 StatementNode::Write(writer);
176 bool hasExpression = expression != nullptr;
177 writer.GetBinaryWriter().Write(hasExpression);
178 if (hasExpression)
179 {
180 writer.Write(expression.get());
181 }
182 }
183
184 void ReturnStatementNode::Read(AstReader& reader)
185 {
186 StatementNode::Read(reader);
187 bool hasExpression = reader.GetBinaryReader().ReadBool();
188 if (hasExpression)
189 {
190 expression.reset(reader.ReadNode());
191 expression->SetParent(this);
192 }
193 }
194
195 IfStatementNode::IfStatementNode(const Span& span_) : StatementNode(NodeType::ifStatementNode, span_), condition(), thenS(), elseS()
196 {
197 }
198
199 IfStatementNode::IfStatementNode(const Span& span_, Node* condition_, StatementNode* thenS_, StatementNode* elseS_) :
200 StatementNode(NodeType::ifStatementNode, span_), condition(condition_), thenS(thenS_), elseS(elseS_)
201 {
202 condition->SetParent(this);
203 thenS->SetParent(this);
204 if (elseS)
205 {
206 elseS->SetParent(this);
207 }
208 }
209
210 Node* IfStatementNode::Clone(CloneContext& cloneContext) const
211 {
212 StatementNode* clonedElseS = nullptr;
213 if (elseS)
214 {
215 clonedElseS = static_cast<StatementNode*>(elseS->Clone(cloneContext));
216 }
217 IfStatementNode* clone = new IfStatementNode(GetSpan(), condition->Clone(cloneContext), static_cast<StatementNode*>(thenS->Clone(cloneContext)), clonedElseS);
218 clone->SetLeftParenSpan(leftParenSpan);
219 clone->SetRightParenSpan(rightParenSpan);
220 clone->SetElseSpan(elseSpan);
221 return clone;
222 }
223
224 void IfStatementNode::Accept(Visitor& visitor)
225 {
226 visitor.Visit(*this);
227 }
228
229 void IfStatementNode::Write(AstWriter& writer)
230 {
231 StatementNode::Write(writer);
232 writer.Write(condition.get());
233 writer.Write(thenS.get());
234 bool hasElseS = elseS != nullptr;
235 writer.GetBinaryWriter().Write(hasElseS);
236 if (hasElseS)
237 {
238 writer.Write(elseS.get());
239 }
240 writer.Write(leftParenSpan);
241 writer.Write(rightParenSpan);
242 writer.Write(elseSpan);
243 }
244
245 void IfStatementNode::Read(AstReader& reader)
246 {
247 StatementNode::Read(reader);
248 condition.reset(reader.ReadNode());
249 condition->SetParent(this);
250 thenS.reset(reader.ReadStatementNode());
251 thenS->SetParent(this);
252 bool hasElseS = reader.GetBinaryReader().ReadBool();
253 if (hasElseS)
254 {
255 elseS.reset(reader.ReadStatementNode());
256 elseS->SetParent(this);
257 }
258 leftParenSpan = reader.ReadSpan();
259 rightParenSpan = reader.ReadSpan();
260 elseSpan = reader.ReadSpan();
261 }
262
263 WhileStatementNode::WhileStatementNode(const Span& span_) : StatementNode(NodeType::whileStatementNode, span_), condition(), statement()
264 {
265 }
266
267 WhileStatementNode::WhileStatementNode(const Span& span_, Node* condition_, StatementNode* statement_) :
268 StatementNode(NodeType::whileStatementNode, span_), condition(condition_), statement(statement_)
269 {
270 condition->SetParent(this);
271 statement->SetParent(this);
272 }
273
274 Node* WhileStatementNode::Clone(CloneContext& cloneContext) const
275 {
276 WhileStatementNode* clone = new WhileStatementNode(GetSpan(), condition->Clone(cloneContext), static_cast<StatementNode*>(statement->Clone(cloneContext)));
277 clone->SetLeftParenSpan(leftParenSpan);
278 clone->SetRightParenSpan(rightParenSpan);
279 return clone;
280 }
281
282 void WhileStatementNode::Accept(Visitor& visitor)
283 {
284 visitor.Visit(*this);
285 }
286
287 void WhileStatementNode::Write(AstWriter& writer)
288 {
289 StatementNode::Write(writer);
290 writer.Write(condition.get());
291 writer.Write(statement.get());
292 writer.Write(leftParenSpan);
293 writer.Write(rightParenSpan);
294 }
295
296 void WhileStatementNode::Read(AstReader& reader)
297 {
298 StatementNode::Read(reader);
299 condition.reset(reader.ReadNode());
300 condition->SetParent(this);
301 statement.reset(reader.ReadStatementNode());
302 statement->SetParent(this);
303 leftParenSpan = reader.ReadSpan();
304 rightParenSpan = reader.ReadSpan();
305 }
306
307 DoStatementNode::DoStatementNode(const Span& span_) : StatementNode(NodeType::doStatementNode, span_), statement(), condition()
308 {
309 }
310
311 DoStatementNode::DoStatementNode(const Span& span_, StatementNode* statement_, Node* condition_) : StatementNode(NodeType::doStatementNode, span_), statement(statement_), condition(condition_)
312 {
313 statement->SetParent(this);
314 condition->SetParent(this);
315 }
316
317 Node* DoStatementNode::Clone(CloneContext& cloneContext) const
318 {
319 DoStatementNode* clone = new DoStatementNode(GetSpan(), static_cast<StatementNode*>(statement->Clone(cloneContext)), condition->Clone(cloneContext));
320 clone->SetWhileSpan(whileSpan);
321 clone->SetLeftParenSpan(leftParenSpan);
322 clone->SetRightParenSpan(rightParenSpan);
323 return clone;
324 }
325
326 void DoStatementNode::Accept(Visitor& visitor)
327 {
328 visitor.Visit(*this);
329 }
330
331 void DoStatementNode::Write(AstWriter& writer)
332 {
333 StatementNode::Write(writer);
334 writer.Write(statement.get());
335 writer.Write(condition.get());
336 writer.Write(whileSpan);
337 writer.Write(leftParenSpan);
338 writer.Write(rightParenSpan);
339 }
340
341 void DoStatementNode::Read(AstReader& reader)
342 {
343 StatementNode::Read(reader);
344 statement.reset(reader.ReadStatementNode());
345 statement->SetParent(this);
346 condition.reset(reader.ReadNode());
347 condition->SetParent(this);
348 whileSpan = reader.ReadSpan();
349 leftParenSpan = reader.ReadSpan();
350 rightParenSpan = reader.ReadSpan();
351 }
352
353 ForStatementNode::ForStatementNode(const Span& span_) : StatementNode(NodeType::forStatementNode, span_), initS(), condition(), loopS(), actionS()
354 {
355 }
356
357 ForStatementNode::ForStatementNode(const Span& span_, StatementNode* initS_, Node* condition_, StatementNode* loopS_, StatementNode* actionS_) :
358 StatementNode(NodeType::forStatementNode, span_), initS(initS_), condition(condition_), loopS(loopS_), actionS(actionS_)
359 {
360 initS->SetParent(this);
361 if (condition)
362 {
363 condition->SetParent(this);
364 }
365 loopS->SetParent(this);
366 actionS->SetParent(this);
367 }
368
369 Node* ForStatementNode::Clone(CloneContext& cloneContext) const
370 {
371 Node* clonedCondition = nullptr;
372 if (condition)
373 {
374 clonedCondition = condition->Clone(cloneContext);
375 }
376 ForStatementNode* clone = new ForStatementNode(GetSpan(), static_cast<StatementNode*>(initS->Clone(cloneContext)), clonedCondition, static_cast<StatementNode*>(loopS->Clone(cloneContext)),
377 static_cast<StatementNode*>(actionS->Clone(cloneContext)));
378 clone->SetLeftParenSpan(leftParenSpan);
379 clone->SetRightParenSpan(rightParenSpan);
380 return clone;
381 }
382
383 void ForStatementNode::Accept(Visitor& visitor)
384 {
385 visitor.Visit(*this);
386 }
387
388 void ForStatementNode::Write(AstWriter& writer)
389 {
390 StatementNode::Write(writer);
391 writer.Write(initS.get());
392 bool hasCondition = condition != nullptr;
393 writer.GetBinaryWriter().Write(hasCondition);
394 if (hasCondition)
395 {
396 writer.Write(condition.get());
397 }
398 writer.Write(loopS.get());
399 writer.Write(actionS.get());
400 writer.Write(leftParenSpan);
401 writer.Write(rightParenSpan);
402 }
403
404 void ForStatementNode::Read(AstReader& reader)
405 {
406 StatementNode::Read(reader);
407 initS.reset(reader.ReadStatementNode());
408 initS->SetParent(this);
409 bool hasCondition = reader.GetBinaryReader().ReadBool();
410 if (hasCondition)
411 {
412 condition.reset(reader.ReadNode());
413 condition->SetParent(this);
414 }
415 loopS.reset(reader.ReadStatementNode());
416 loopS->SetParent(this);
417 actionS.reset(reader.ReadStatementNode());
418 actionS->SetParent(this);
419 leftParenSpan = reader.ReadSpan();
420 rightParenSpan = reader.ReadSpan();
421 }
422
423 BreakStatementNode::BreakStatementNode(const Span& span_) : StatementNode(NodeType::breakStatementNode, span_)
424 {
425 }
426
427 Node* BreakStatementNode::Clone(CloneContext& cloneContext) const
428 {
429 BreakStatementNode* clone = new BreakStatementNode(GetSpan());
430 return clone;
431 }
432
433 void BreakStatementNode::Accept(Visitor& visitor)
434 {
435 visitor.Visit(*this);
436 }
437
438 ContinueStatementNode::ContinueStatementNode(const Span& span_) : StatementNode(NodeType::continueStatementNode, span_)
439 {
440 }
441
442 Node* ContinueStatementNode::Clone(CloneContext& cloneContext) const
443 {
444 ContinueStatementNode* clone = new ContinueStatementNode(GetSpan());
445 return clone;
446 }
447
448 void ContinueStatementNode::Accept(Visitor& visitor)
449 {
450 visitor.Visit(*this);
451 }
452
453 GotoStatementNode::GotoStatementNode(const Span& span_) : StatementNode(NodeType::gotoStatementNode, span_)
454 {
455 }
456
457 GotoStatementNode::GotoStatementNode(const Span& span_, const std::u32string& target_) : StatementNode(NodeType::gotoStatementNode, span_), target(target_)
458 {
459 }
460
461 Node* GotoStatementNode::Clone(CloneContext& cloneContext) const
462 {
463 GotoStatementNode* clone = new GotoStatementNode(GetSpan(), target);
464 return clone;
465 }
466
467 void GotoStatementNode::Accept(Visitor& visitor)
468 {
469 visitor.Visit(*this);
470 }
471
472 void GotoStatementNode::Write(AstWriter& writer)
473 {
474 StatementNode::Write(writer);
475 writer.GetBinaryWriter().Write(target);
476 }
477
478 void GotoStatementNode::Read(AstReader& reader)
479 {
480 StatementNode::Read(reader);
481 target = reader.GetBinaryReader().ReadUtf32String();
482 }
483
484 ConstructionStatementNode::ConstructionStatementNode(const Span& span_) : StatementNode(NodeType::constructionStatementNode, span_), typeExpr(), id(), arguments(), assignment(false), empty(false)
485 {
486 }
487
488 ConstructionStatementNode::ConstructionStatementNode(const Span& span_, Node* typeExpr_, IdentifierNode* id_) :
489 StatementNode(NodeType::constructionStatementNode, span_), typeExpr(typeExpr_), id(id_), arguments(), assignment(false), empty(false)
490 {
491 typeExpr->SetParent(this);
492 id->SetParent(this);
493 }
494
495 Node* ConstructionStatementNode::Clone(CloneContext& cloneContext) const
496 {
497 ConstructionStatementNode* clone = new ConstructionStatementNode(GetSpan(), typeExpr->Clone(cloneContext), static_cast<IdentifierNode*>(id->Clone(cloneContext)));
498 int n = arguments.Count();
499 for (int i = 0; i < n; ++i)
500 {
501 clone->AddArgument(arguments[i]->Clone(cloneContext));
502 }
503 if (assignment) clone->SetAssignment();
504 if (empty) clone->SetEmpty();
505 return clone;
506 }
507
508 void ConstructionStatementNode::Accept(Visitor& visitor)
509 {
510 visitor.Visit(*this);
511 }
512
513 void ConstructionStatementNode::Write(AstWriter& writer)
514 {
515 StatementNode::Write(writer);
516 writer.Write(typeExpr.get());
517 writer.Write(id.get());
518 arguments.Write(writer);
519 writer.GetBinaryWriter().Write(assignment);
520 writer.GetBinaryWriter().Write(empty);
521 }
522
523 void ConstructionStatementNode::Read(AstReader& reader)
524 {
525 StatementNode::Read(reader);
526 typeExpr.reset(reader.ReadNode());
527 typeExpr->SetParent(this);
528 id.reset(reader.ReadIdentifierNode());
529 id->SetParent(this);
530 arguments.Read(reader);
531 arguments.SetParent(this);
532 assignment = reader.GetBinaryReader().ReadBool();
533 empty = reader.GetBinaryReader().ReadBool();
534 }
535
536 void ConstructionStatementNode::AddArgument(Node* argument)
537 {
538 argument->SetParent(this);
539 arguments.Add(argument);
540 }
541
542 DeleteStatementNode::DeleteStatementNode(const Span& span_) : StatementNode(NodeType::deleteStatementNode, span_), expression()
543 {
544 }
545
546 DeleteStatementNode::DeleteStatementNode(const Span& span_, Node* expression_) : StatementNode(NodeType::deleteStatementNode, span_), expression(expression_)
547 {
548 expression->SetParent(this);
549 }
550
551 Node* DeleteStatementNode::Clone(CloneContext& cloneContext) const
552 {
553 DeleteStatementNode* clone = new DeleteStatementNode(GetSpan(), expression->Clone(cloneContext));
554 return clone;
555 }
556
557 void DeleteStatementNode::Accept(Visitor& visitor)
558 {
559 visitor.Visit(*this);
560 }
561
562 void DeleteStatementNode::Write(AstWriter& writer)
563 {
564 StatementNode::Write(writer);
565 writer.Write(expression.get());
566 }
567
568 void DeleteStatementNode::Read(AstReader& reader)
569 {
570 StatementNode::Read(reader);
571 expression.reset(reader.ReadNode());
572 expression->SetParent(this);
573 }
574
575 DestroyStatementNode::DestroyStatementNode(const Span& span_) : StatementNode(NodeType::destroyStatementNode, span_), expression()
576 {
577 }
578
579 DestroyStatementNode::DestroyStatementNode(const Span& span_, Node* expression_) : StatementNode(NodeType::destroyStatementNode, span_), expression(expression_)
580 {
581 expression->SetParent(this);
582 }
583
584 Node* DestroyStatementNode::Clone(CloneContext& cloneContext) const
585 {
586 DestroyStatementNode* clone = new DestroyStatementNode(GetSpan(), expression->Clone(cloneContext));
587 return clone;
588 }
589
590 void DestroyStatementNode::Accept(Visitor& visitor)
591 {
592 visitor.Visit(*this);
593 }
594
595 void DestroyStatementNode::Write(AstWriter& writer)
596 {
597 StatementNode::Write(writer);
598 writer.Write(expression.get());
599 }
600
601 void DestroyStatementNode::Read(AstReader& reader)
602 {
603 StatementNode::Read(reader);
604 expression.reset(reader.ReadNode());
605 expression->SetParent(this);
606 }
607
608 AssignmentStatementNode::AssignmentStatementNode(const Span& span_) : StatementNode(NodeType::assignmentStatementNode, span_), targetExpr(), sourceExpr()
609 {
610 }
611
612 AssignmentStatementNode::AssignmentStatementNode(const Span& span_, Node* targetExpr_, Node* sourceExpr_) :
613 StatementNode(NodeType::assignmentStatementNode, span_), targetExpr(targetExpr_), sourceExpr(sourceExpr_)
614 {
615 targetExpr->SetParent(this);
616 sourceExpr->SetParent(this);
617 }
618
619 Node* AssignmentStatementNode::Clone(CloneContext& cloneContext) const
620 {
621 AssignmentStatementNode* clone = new AssignmentStatementNode(GetSpan(), targetExpr->Clone(cloneContext), sourceExpr->Clone(cloneContext));
622 return clone;
623 }
624
625 void AssignmentStatementNode::Accept(Visitor& visitor)
626 {
627 visitor.Visit(*this);
628 }
629
630 void AssignmentStatementNode::Write(AstWriter& writer)
631 {
632 StatementNode::Write(writer);
633 writer.Write(targetExpr.get());
634 writer.Write(sourceExpr.get());
635 }
636
637 void AssignmentStatementNode::Read(AstReader& reader)
638 {
639 StatementNode::Read(reader);
640 targetExpr.reset(reader.ReadNode());
641 targetExpr->SetParent(this);
642 sourceExpr.reset(reader.ReadNode());
643 sourceExpr->SetParent(this);
644 }
645
646 ExpressionStatementNode::ExpressionStatementNode(const Span& span_) : StatementNode(NodeType::expressionStatementNode, span_), expression()
647 {
648 }
649
650 ExpressionStatementNode::ExpressionStatementNode(const Span& span_, Node* expression_) : StatementNode(NodeType::expressionStatementNode, span_), expression(expression_)
651 {
652 expression->SetParent(this);
653 }
654
655 Node* ExpressionStatementNode::Clone(CloneContext& cloneContext) const
656 {
657 ExpressionStatementNode* clone = new ExpressionStatementNode(GetSpan(), expression->Clone(cloneContext));
658 return clone;
659 }
660
661 void ExpressionStatementNode::Accept(Visitor& visitor)
662 {
663 visitor.Visit(*this);
664 }
665
666 void ExpressionStatementNode::Write(AstWriter& writer)
667 {
668 StatementNode::Write(writer);
669 writer.Write(expression.get());
670 }
671
672 void ExpressionStatementNode::Read(AstReader& reader)
673 {
674 StatementNode::Read(reader);
675 expression.reset(reader.ReadNode());
676 expression->SetParent(this);
677 }
678
679 EmptyStatementNode::EmptyStatementNode(const Span& span_) : StatementNode(NodeType::emptyStatementNode, span_)
680 {
681 }
682
683 Node* EmptyStatementNode::Clone(CloneContext& cloneContext) const
684 {
685 EmptyStatementNode* clone = new EmptyStatementNode(GetSpan());
686 return clone;
687 }
688
689 void EmptyStatementNode::Accept(Visitor& visitor)
690 {
691 visitor.Visit(*this);
692 }
693
694 RangeForStatementNode::RangeForStatementNode(const Span& span_) : StatementNode(NodeType::rangeForStatementNode, span_), typeExpr(), id(), container(), action()
695 {
696 }
697
698 RangeForStatementNode::RangeForStatementNode(const Span& span_, Node* typeExpr_, IdentifierNode* id_, Node* container_, StatementNode* action_) :
699 StatementNode(NodeType::rangeForStatementNode, span_), typeExpr(typeExpr_), id(id_), container(container_), action(action_)
700 {
701 typeExpr->SetParent(this);
702 id->SetParent(this);
703 container->SetParent(this);
704 action->SetParent(this);
705 }
706
707 Node* RangeForStatementNode::Clone(CloneContext& cloneContext) const
708 {
709 RangeForStatementNode* clone = new RangeForStatementNode(GetSpan(), typeExpr->Clone(cloneContext), static_cast<IdentifierNode*>(id->Clone(cloneContext)), container->Clone(cloneContext),
710 static_cast<StatementNode*>(action->Clone(cloneContext)));
711 clone->SetLeftParenSpan(leftParenSpan);
712 clone->SetRightParenSpan(rightParenSpan);
713 clone->SetColonSpan(colonSpan);
714 return clone;
715 }
716
717 void RangeForStatementNode::Accept(Visitor& visitor)
718 {
719 visitor.Visit(*this);
720 }
721
722 void RangeForStatementNode::Write(AstWriter& writer)
723 {
724 StatementNode::Write(writer);
725 writer.Write(typeExpr.get());
726 writer.Write(id.get());
727 writer.Write(container.get());
728 writer.Write(action.get());
729 writer.Write(leftParenSpan);
730 writer.Write(rightParenSpan);
731 writer.Write(colonSpan);
732 }
733
734 void RangeForStatementNode::Read(AstReader& reader)
735 {
736 StatementNode::Read(reader);
737 typeExpr.reset(reader.ReadNode());
738 typeExpr->SetParent(this);
739 id.reset(reader.ReadIdentifierNode());
740 id->SetParent(this);
741 container.reset(reader.ReadNode());
742 container->SetParent(this);
743 action.reset(reader.ReadStatementNode());
744 action->SetParent(this);
745 leftParenSpan = reader.ReadSpan();
746 rightParenSpan = reader.ReadSpan();
747 colonSpan = reader.ReadSpan();
748 }
749
750 SwitchStatementNode::SwitchStatementNode(const Span& span_) : StatementNode(NodeType::switchStatementNode, span_), condition(), cases(), defaultS()
751 {
752 }
753
754 SwitchStatementNode::SwitchStatementNode(const Span& span_, Node* condition_) : StatementNode(NodeType::switchStatementNode, span_), condition(condition_), cases(), defaultS()
755 {
756 condition->SetParent(this);
757 }
758
759 Node* SwitchStatementNode::Clone(CloneContext& cloneContext) const
760 {
761 SwitchStatementNode* clone = new SwitchStatementNode(GetSpan(), condition->Clone(cloneContext));
762 int n = cases.Count();
763 for (int i = 0; i < n; ++i)
764 {
765 clone->AddCase(static_cast<CaseStatementNode*>(cases[i]->Clone(cloneContext)));
766 }
767 if (defaultS)
768 {
769 clone->SetDefault(static_cast<DefaultStatementNode*>(defaultS->Clone(cloneContext)));
770 }
771 clone->SetLeftParenSpan(leftParenSpan);
772 clone->SetRightParenSpan(rightParenSpan);
773 clone->SetBeginBraceSpan(beginBraceSpan);
774 clone->SetEndBraceSpan(endBraceSpan);
775 return clone;
776 }
777
778 void SwitchStatementNode::Accept(Visitor& visitor)
779 {
780 visitor.Visit(*this);
781 }
782
783 void SwitchStatementNode::Write(AstWriter& writer)
784 {
785 StatementNode::Write(writer);
786 writer.Write(condition.get());
787 cases.Write(writer);
788 bool hasDefault = defaultS != nullptr;
789 writer.GetBinaryWriter().Write(hasDefault);
790 if (hasDefault)
791 {
792 writer.Write(defaultS.get());
793 }
794 writer.Write(leftParenSpan);
795 writer.Write(rightParenSpan);
796 writer.Write(beginBraceSpan);
797 writer.Write(endBraceSpan);
798 }
799
800 void SwitchStatementNode::Read(AstReader& reader)
801 {
802 StatementNode::Read(reader);
803 condition.reset(reader.ReadNode());
804 condition->SetParent(this);
805 cases.Read(reader);
806 cases.SetParent(this);
807 bool hasDefault = reader.GetBinaryReader().ReadBool();
808 if (hasDefault)
809 {
810 defaultS.reset(reader.ReadDefaultStatementNode());
811 defaultS->SetParent(this);
812 }
813 leftParenSpan = reader.ReadSpan();
814 rightParenSpan = reader.ReadSpan();
815 beginBraceSpan = reader.ReadSpan();
816 endBraceSpan = reader.ReadSpan();
817 }
818
819 void SwitchStatementNode::AddCase(CaseStatementNode* caseS)
820 {
821 caseS->SetParent(this);
822 cases.Add(caseS);
823 }
824
825 void SwitchStatementNode::SetDefault(DefaultStatementNode* defaultS_)
826 {
827 defaultS.reset(defaultS_);
828 defaultS->SetParent(this);
829 }
830
831 CaseStatementNode::CaseStatementNode(const Span& span_) : StatementNode(NodeType::caseStatementNode, span_), caseExprs(), statements()
832 {
833 }
834
835 Node* CaseStatementNode::Clone(CloneContext& cloneContext) const
836 {
837 CaseStatementNode* clone = new CaseStatementNode(GetSpan());
838 int ne = caseExprs.Count();
839 for (int i = 0; i < ne; ++i)
840 {
841 clone->AddCaseExpr(caseExprs[i]->Clone(cloneContext));
842 }
843 int ns = statements.Count();
844 for (int i = 0; i < ns; ++i)
845 {
846 clone->AddStatement(static_cast<StatementNode*>(statements[i]->Clone(cloneContext)));
847 }
848 clone->caseSpans = caseSpans;
849 return clone;
850 }
851
852 void CaseStatementNode::Accept(Visitor& visitor)
853 {
854 visitor.Visit(*this);
855 }
856
857 void CaseStatementNode::Write(AstWriter& writer)
858 {
859 StatementNode::Write(writer);
860 caseExprs.Write(writer);
861 statements.Write(writer);
862 uint32_t n = static_cast<uint32_t>(caseSpans.size());
863 writer.GetBinaryWriter().WriteULEB128UInt(n);
864 for (uint32_t i = 0u; i < n; ++i)
865 {
866 writer.Write(caseSpans[i]);
867 }
868 }
869
870 void CaseStatementNode::Read(AstReader& reader)
871 {
872 StatementNode::Read(reader);
873 caseExprs.Read(reader);
874 caseExprs.SetParent(this);
875 statements.Read(reader);
876 statements.SetParent(this);
877 uint32_t n = reader.GetBinaryReader().ReadULEB128UInt();
878 for (uint32_t i = 0u; i < n; ++i)
879 {
880 caseSpans.push_back(reader.ReadSpan());
881 }
882 }
883
884 void CaseStatementNode::AddCaseExpr(Node* caseExpr)
885 {
886 caseExpr->SetParent(this);
887 caseExprs.Add(caseExpr);
888 }
889
890 void CaseStatementNode::AddCaseSpan(const Span& caseSpan)
891 {
892 caseSpans.push_back(caseSpan);
893 }
894
895 void CaseStatementNode::AddStatement(StatementNode* statement)
896 {
897 statement->SetParent(this);
898 statements.Add(statement);
899 }
900
901 DefaultStatementNode::DefaultStatementNode(const Span& span_) : StatementNode(NodeType::defaultStatementNode, span_), statements()
902 {
903 }
904
905 Node* DefaultStatementNode::Clone(CloneContext& cloneContext) const
906 {
907 DefaultStatementNode* clone = new DefaultStatementNode(GetSpan());
908 int n = statements.Count();
909 for (int i = 0; i < n; ++i)
910 {
911 clone->AddStatement(static_cast<StatementNode*>(statements[i]->Clone(cloneContext)));
912 }
913 return clone;
914 }
915
916 void DefaultStatementNode::Accept(Visitor& visitor)
917 {
918 visitor.Visit(*this);
919 }
920
921 void DefaultStatementNode::Write(AstWriter& writer)
922 {
923 StatementNode::Write(writer);
924 statements.Write(writer);
925 }
926
927 void DefaultStatementNode::Read(AstReader& reader)
928 {
929 StatementNode::Read(reader);
930 statements.Read(reader);
931 statements.SetParent(this);
932 }
933
934 void DefaultStatementNode::AddStatement(StatementNode* statement)
935 {
936 statement->SetParent(this);
937 statements.Add(statement);
938 }
939
940 GotoCaseStatementNode::GotoCaseStatementNode(const Span& span_) : StatementNode(NodeType::gotoCaseStatementNode, span_), caseExpr()
941 {
942 }
943
944 GotoCaseStatementNode::GotoCaseStatementNode(const Span& span_, Node* caseExpr_) : StatementNode(NodeType::gotoCaseStatementNode, span_), caseExpr(caseExpr_)
945 {
946 caseExpr->SetParent(this);
947 }
948
949 Node* GotoCaseStatementNode::Clone(CloneContext& cloneContext) const
950 {
951 GotoCaseStatementNode* clone = new GotoCaseStatementNode(GetSpan(), caseExpr->Clone(cloneContext));
952 return clone;
953 }
954
955 void GotoCaseStatementNode::Accept(Visitor& visitor)
956 {
957 visitor.Visit(*this);
958 }
959
960 void GotoCaseStatementNode::Write(AstWriter& writer)
961 {
962 StatementNode::Write(writer);
963 writer.Write(caseExpr.get());
964 }
965
966 void GotoCaseStatementNode::Read(AstReader& reader)
967 {
968 StatementNode::Read(reader);
969 caseExpr.reset(reader.ReadNode());
970 }
971
972 GotoDefaultStatementNode::GotoDefaultStatementNode(const Span& span_) : StatementNode(NodeType::gotoDefaultStatementNode, span_)
973 {
974 }
975
976 Node* GotoDefaultStatementNode::Clone(CloneContext& cloneContext) const
977 {
978 GotoDefaultStatementNode* clone = new GotoDefaultStatementNode(GetSpan());
979 return clone;
980 }
981
982 void GotoDefaultStatementNode::Accept(Visitor& visitor)
983 {
984 visitor.Visit(*this);
985 }
986
987 ThrowStatementNode::ThrowStatementNode(const Span& span_) : StatementNode(NodeType::throwStatementNode, span_), expression()
988 {
989 }
990
991 ThrowStatementNode::ThrowStatementNode(const Span& span_, Node* expression_) : StatementNode(NodeType::throwStatementNode, span_), expression(expression_)
992 {
993 if (expression)
994 {
995 expression->SetParent(this);
996 }
997 }
998
999 Node* ThrowStatementNode::Clone(CloneContext& cloneContext) const
1000 {
1001 Node* clonedExpression = nullptr;
1002 if (expression)
1003 {
1004 clonedExpression = expression->Clone(cloneContext);
1005 }
1006 ThrowStatementNode* clone = new ThrowStatementNode(GetSpan(), clonedExpression);
1007 return clone;
1008 }
1009
1010 void ThrowStatementNode::Accept(Visitor& visitor)
1011 {
1012 visitor.Visit(*this);
1013 }
1014
1015 void ThrowStatementNode::Write(AstWriter& writer)
1016 {
1017 StatementNode::Write(writer);
1018 bool hasExpression = expression != nullptr;
1019 writer.GetBinaryWriter().Write(hasExpression);
1020 if (hasExpression)
1021 {
1022 writer.Write(expression.get());
1023 }
1024 }
1025
1026 void ThrowStatementNode::Read(AstReader& reader)
1027 {
1028 StatementNode::Read(reader);
1029 bool hasExpression = reader.GetBinaryReader().ReadBool();
1030 if (hasExpression)
1031 {
1032 expression.reset(reader.ReadNode());
1033 expression->SetParent(this);
1034 }
1035 }
1036
1037 CatchNode::CatchNode(const Span& span_) : Node(NodeType::catchNode, span_), typeExpr(), id(), catchBlock()
1038 {
1039 }
1040
1041 CatchNode::CatchNode(const Span& span_, Node* typeExpr_, IdentifierNode* id_, CompoundStatementNode* catchBlock_) :
1042 Node(NodeType::catchNode, span_), typeExpr(typeExpr_), id(id_), catchBlock(catchBlock_)
1043 {
1044 typeExpr->SetParent(this);
1045 if (id)
1046 {
1047 id->SetParent(this);
1048 }
1049 catchBlock->SetParent(this);
1050 }
1051
1052 Node* CatchNode::Clone(CloneContext& cloneContext) const
1053 {
1054 IdentifierNode* clonedId = nullptr;
1055 if (id)
1056 {
1057 clonedId = static_cast<IdentifierNode*>(id->Clone(cloneContext));
1058 }
1059 CatchNode* clone = new CatchNode(GetSpan(), typeExpr->Clone(cloneContext), clonedId, static_cast<CompoundStatementNode*>(catchBlock->Clone(cloneContext)));
1060 clone->SetLeftParenSpan(leftParenSpan);
1061 clone->SetRightParenSpan(rightParenSpan);
1062 return clone;
1063 }
1064
1065 void CatchNode::Accept(Visitor& visitor)
1066 {
1067 visitor.Visit(*this);
1068 }
1069
1070 void CatchNode::Write(AstWriter& writer)
1071 {
1072 Node::Write(writer);
1073 writer.Write(typeExpr.get());
1074 bool hasId = id != nullptr;
1075 writer.GetBinaryWriter().Write(hasId);
1076 if (hasId)
1077 {
1078 writer.Write(id.get());
1079 }
1080 writer.Write(catchBlock.get());
1081 writer.Write(leftParenSpan);
1082 writer.Write(rightParenSpan);
1083 }
1084
1085 void CatchNode::Read(AstReader& reader)
1086 {
1087 Node::Read(reader);
1088 typeExpr.reset(reader.ReadNode());
1089 typeExpr->SetParent(this);
1090 bool hasId = reader.GetBinaryReader().ReadBool();
1091 if (hasId)
1092 {
1093 id.reset(reader.ReadIdentifierNode());
1094 id->SetParent(this);
1095 }
1096 catchBlock.reset(reader.ReadCompoundStatementNode());
1097 catchBlock->SetParent(this);
1098 leftParenSpan = reader.ReadSpan();
1099 rightParenSpan = reader.ReadSpan();
1100 }
1101
1102 TryStatementNode::TryStatementNode(const Span& span_) : StatementNode(NodeType::tryStatementNode, span_), tryBlock(), catches()
1103 {
1104 }
1105
1106 TryStatementNode::TryStatementNode(const Span& span_, CompoundStatementNode* tryBlock_) : StatementNode(NodeType::tryStatementNode, span_), tryBlock(tryBlock_), catches()
1107 {
1108 tryBlock->SetParent(this);
1109 }
1110
1111 Node* TryStatementNode::Clone(CloneContext& cloneContext) const
1112 {
1113 TryStatementNode* clone = new TryStatementNode(GetSpan(), static_cast<CompoundStatementNode*>(tryBlock->Clone(cloneContext)));
1114 int n = catches.Count();
1115 for (int i = 0; i < n; ++i)
1116 {
1117 clone->AddCatch(static_cast<CatchNode*>(catches[i]->Clone(cloneContext)));
1118 }
1119 return clone;
1120 }
1121
1122 void TryStatementNode::Accept(Visitor& visitor)
1123 {
1124 visitor.Visit(*this);
1125 }
1126
1127 void TryStatementNode::Write(AstWriter& writer)
1128 {
1129 StatementNode::Write(writer);
1130 writer.Write(tryBlock.get());
1131 catches.Write(writer);
1132 }
1133
1134 void TryStatementNode::Read(AstReader& reader)
1135 {
1136 StatementNode::Read(reader);
1137 tryBlock.reset(reader.ReadCompoundStatementNode());
1138 tryBlock->SetParent(this);
1139 catches.Read(reader);
1140 catches.SetParent(this);
1141 }
1142
1143 void TryStatementNode::AddCatch(CatchNode* catch_)
1144 {
1145 catch_->SetParent(this);
1146 catches.Add(catch_);
1147 }
1148
1149 AssertStatementNode::AssertStatementNode(const Span& span_) : StatementNode(NodeType::assertStatementNode, span_), assertExpr()
1150 {
1151 }
1152
1153 AssertStatementNode::AssertStatementNode(const Span& span_, Node* assertExpr_) : StatementNode(NodeType::assertStatementNode, span_), assertExpr(assertExpr_)
1154 {
1155 assertExpr->SetParent(this);
1156 }
1157
1158 Node* AssertStatementNode::Clone(CloneContext& cloneContext) const
1159 {
1160 AssertStatementNode* clone = new AssertStatementNode(GetSpan(), assertExpr->Clone(cloneContext));
1161 return clone;
1162 }
1163
1164 void AssertStatementNode::Accept(Visitor& visitor)
1165 {
1166 visitor.Visit(*this);
1167 }
1168
1169 void AssertStatementNode::Write(AstWriter& writer)
1170 {
1171 StatementNode::Write(writer);
1172 writer.Write(assertExpr.get());
1173 }
1174
1175 void AssertStatementNode::Read(AstReader& reader)
1176 {
1177 StatementNode::Read(reader);
1178 assertExpr.reset(reader.ReadNode());
1179 assertExpr->SetParent(this);
1180 }
1181
1182 ConditionalCompilationExpressionNode::ConditionalCompilationExpressionNode(NodeType nodeType_, const Span& span_) : Node(nodeType_, span_)
1183 {
1184 }
1185
1186 ConditionalCompilationBinaryExpressionNode::ConditionalCompilationBinaryExpressionNode(NodeType nodeType_, const Span& span_) : ConditionalCompilationExpressionNode(nodeType_, span_)
1187 {
1188 }
1189
1190 ConditionalCompilationBinaryExpressionNode::ConditionalCompilationBinaryExpressionNode(NodeType nodeType_, const Span& span_, ConditionalCompilationExpressionNode* left_, ConditionalCompilationExpressionNode* right_) :
1191 ConditionalCompilationExpressionNode(nodeType_, span_), left(left_), right(right_)
1192 {
1193 left->SetParent(this);
1194 right->SetParent(this);
1195 }
1196
1197 void ConditionalCompilationBinaryExpressionNode::Write(AstWriter& writer)
1198 {
1199 ConditionalCompilationExpressionNode::Write(writer);
1200 writer.Write(left.get());
1201 writer.Write(right.get());
1202 }
1203
1204 void ConditionalCompilationBinaryExpressionNode::Read(AstReader& reader)
1205 {
1206 ConditionalCompilationExpressionNode::Read(reader);
1207 left.reset(reader.ReadConditionalCompilationExpressionNode());
1208 left->SetParent(this);
1209 right.reset(reader.ReadConditionalCompilationExpressionNode());
1210 right->SetParent(this);
1211 }
1212
1213 ConditionalCompilationDisjunctionNode::ConditionalCompilationDisjunctionNode(const Span& span_) : ConditionalCompilationBinaryExpressionNode(NodeType::conditionalCompilationDisjunctionNode, span_)
1214 {
1215 }
1216
1217 ConditionalCompilationDisjunctionNode::ConditionalCompilationDisjunctionNode(const Span& span_, ConditionalCompilationExpressionNode* left_, ConditionalCompilationExpressionNode* right_) :
1218 ConditionalCompilationBinaryExpressionNode(NodeType::conditionalCompilationDisjunctionNode, span_, left_, right_)
1219 {
1220 }
1221
1222 Node* ConditionalCompilationDisjunctionNode::Clone(CloneContext& cloneContext) const
1223 {
1224 return new ConditionalCompilationDisjunctionNode(GetSpan(), static_cast<ConditionalCompilationExpressionNode*>(Left()->Clone(cloneContext)), static_cast<ConditionalCompilationExpressionNode*>(Right()->Clone(cloneContext)));
1225 }
1226
1227 void ConditionalCompilationDisjunctionNode::Accept(Visitor& visitor)
1228 {
1229 visitor.Visit(*this);
1230 }
1231
1232 ConditionalCompilationConjunctionNode::ConditionalCompilationConjunctionNode(const Span& span_) : ConditionalCompilationBinaryExpressionNode(NodeType::conditionalCompilationConjunctionNode, span_)
1233 {
1234 }
1235
1236 ConditionalCompilationConjunctionNode::ConditionalCompilationConjunctionNode(const Span& span_, ConditionalCompilationExpressionNode* left_, ConditionalCompilationExpressionNode* right_) :
1237 ConditionalCompilationBinaryExpressionNode(NodeType::conditionalCompilationConjunctionNode, span_, left_, right_)
1238 {
1239 }
1240
1241 Node* ConditionalCompilationConjunctionNode::Clone(CloneContext& cloneContext) const
1242 {
1243 return new ConditionalCompilationConjunctionNode(GetSpan(), static_cast<ConditionalCompilationExpressionNode*>(Left()->Clone(cloneContext)), static_cast<ConditionalCompilationExpressionNode*>(Right()->Clone(cloneContext)));
1244 }
1245
1246 void ConditionalCompilationConjunctionNode::Accept(Visitor& visitor)
1247 {
1248 visitor.Visit(*this);
1249 }
1250
1251 ConditionalCompilationNotNode::ConditionalCompilationNotNode(const Span& span_) : ConditionalCompilationExpressionNode(NodeType::conditionalCompilationNotNode, span_)
1252 {
1253 }
1254
1255 ConditionalCompilationNotNode::ConditionalCompilationNotNode(const Span& span_, ConditionalCompilationExpressionNode* expr_) :
1256 ConditionalCompilationExpressionNode(NodeType::conditionalCompilationNotNode, span_), expr(expr_)
1257 {
1258 expr->SetParent(this);
1259 }
1260
1261 Node* ConditionalCompilationNotNode::Clone(CloneContext& cloneContext) const
1262 {
1263 return new ConditionalCompilationNotNode(GetSpan(), static_cast<ConditionalCompilationExpressionNode*>(expr->Clone(cloneContext)));
1264 }
1265
1266 void ConditionalCompilationNotNode::Accept(Visitor& visitor)
1267 {
1268 visitor.Visit(*this);
1269 }
1270
1271 void ConditionalCompilationNotNode::Write(AstWriter& writer)
1272 {
1273 ConditionalCompilationExpressionNode::Write(writer);
1274 writer.Write(expr.get());
1275 }
1276
1277 void ConditionalCompilationNotNode::Read(AstReader& reader)
1278 {
1279 ConditionalCompilationExpressionNode::Read(reader);
1280 expr.reset(reader.ReadConditionalCompilationExpressionNode());
1281 expr->SetParent(this);
1282 }
1283
1284 ConditionalCompilationPrimaryNode::ConditionalCompilationPrimaryNode(const Span& span_) : ConditionalCompilationExpressionNode(NodeType::conditionalCompilationPrimaryNode, span_)
1285 {
1286 }
1287
1288 ConditionalCompilationPrimaryNode::ConditionalCompilationPrimaryNode(const Span& span_, const std::u32string& symbol_) :
1289 ConditionalCompilationExpressionNode(NodeType::conditionalCompilationPrimaryNode, span_), symbol(symbol_)
1290 {
1291 }
1292
1293 Node* ConditionalCompilationPrimaryNode::Clone(CloneContext& cloneContext) const
1294 {
1295 return new ConditionalCompilationPrimaryNode(GetSpan(), symbol);
1296 }
1297
1298 void ConditionalCompilationPrimaryNode::Accept(Visitor& visitor)
1299 {
1300 visitor.Visit(*this);
1301 }
1302
1303 void ConditionalCompilationPrimaryNode::Write(AstWriter& writer)
1304 {
1305 ConditionalCompilationExpressionNode::Write(writer);
1306 writer.GetBinaryWriter().Write(symbol);
1307 }
1308
1309 void ConditionalCompilationPrimaryNode::Read(AstReader& reader)
1310 {
1311 ConditionalCompilationExpressionNode::Read(reader);
1312 symbol = reader.GetBinaryReader().ReadUtf32String();
1313 }
1314
1315 ParenthesizedConditionalCompilationExpressionNode::ParenthesizedConditionalCompilationExpressionNode(const Span& span_) :
1316 ConditionalCompilationExpressionNode(NodeType::parenthesizedCondCompExpressionNode, span_)
1317 {
1318 }
1319
1320 ParenthesizedConditionalCompilationExpressionNode::ParenthesizedConditionalCompilationExpressionNode(const Span& span_, ConditionalCompilationExpressionNode* expr_) :
1321 ConditionalCompilationExpressionNode(NodeType::parenthesizedCondCompExpressionNode, span_), expr(expr_)
1322 {
1323 }
1324
1325 Node* ParenthesizedConditionalCompilationExpressionNode::Clone(CloneContext& cloneContext) const
1326 {
1327 return new ParenthesizedConditionalCompilationExpressionNode(GetSpan(), static_cast<ConditionalCompilationExpressionNode*>(expr->Clone(cloneContext)));
1328 }
1329
1330 void ParenthesizedConditionalCompilationExpressionNode::Accept(Visitor& visitor)
1331 {
1332 visitor.Visit(*this);
1333 }
1334
1335 void ParenthesizedConditionalCompilationExpressionNode::Write(AstWriter& writer)
1336 {
1337 ConditionalCompilationExpressionNode::Write(writer);
1338 writer.Write(expr.get());
1339 }
1340
1341 void ParenthesizedConditionalCompilationExpressionNode::Read(AstReader& reader)
1342 {
1343 ConditionalCompilationExpressionNode::Read(reader);
1344 expr.reset(reader.ReadConditionalCompilationExpressionNode());
1345 expr->SetParent(this);
1346 }
1347
1348 ConditionalCompilationPartNode::ConditionalCompilationPartNode(const Span& span_) : Node(NodeType::conditionalCompilationPartNode, span_)
1349 {
1350 }
1351
1352 ConditionalCompilationPartNode::ConditionalCompilationPartNode(const Span& span_, ConditionalCompilationExpressionNode* expr_) : Node(NodeType::conditionalCompilationPartNode, span_), expr(expr_)
1353 {
1354 if (expr)
1355 {
1356 expr->SetParent(this);
1357 }
1358 }
1359
1360 void ConditionalCompilationPartNode::AddStatement(StatementNode* statement)
1361 {
1362 statement->SetParent(this);
1363 statements.Add(statement);
1364 }
1365
1366 Node* ConditionalCompilationPartNode::Clone(CloneContext& cloneContext) const
1367 {
1368 ConditionalCompilationExpressionNode* clonedIfExpr = nullptr;
1369 if (expr)
1370 {
1371 clonedIfExpr = static_cast<ConditionalCompilationExpressionNode*>(expr->Clone(cloneContext));
1372 }
1373 ConditionalCompilationPartNode* clone = new ConditionalCompilationPartNode(GetSpan(), clonedIfExpr);
1374 int n = statements.Count();
1375 for (int i = 0; i < n; ++i)
1376 {
1377 clone->AddStatement(static_cast<StatementNode*>(statements[i]->Clone(cloneContext)));
1378 }
1379 clone->SetKeywordSpan(keywordSpan);
1380 clone->SetLeftParenSpan(leftParenSpan);
1381 clone->SetRightParenSpan(rightParenSpan);
1382 return clone;
1383 }
1384
1385 void ConditionalCompilationPartNode::Accept(Visitor& visitor)
1386 {
1387 visitor.Visit(*this);
1388 }
1389
1390 void ConditionalCompilationPartNode::Write(AstWriter& writer)
1391 {
1392 Node::Write(writer);
1393 bool hasExpr = expr != nullptr;
1394 writer.GetBinaryWriter().Write(hasExpr);
1395 if (hasExpr)
1396 {
1397 writer.Write(expr.get());
1398 }
1399 statements.Write(writer);
1400 writer.Write(keywordSpan);
1401 writer.Write(leftParenSpan);
1402 writer.Write(rightParenSpan);
1403 }
1404
1405 void ConditionalCompilationPartNode::Read(AstReader& reader)
1406 {
1407 Node::Read(reader);
1408 bool hasExpr = reader.GetBinaryReader().ReadBool();
1409 if (hasExpr)
1410 {
1411 expr.reset(reader.ReadConditionalCompilationExpressionNode());
1412 expr->SetParent(this);
1413 }
1414 statements.Read(reader);
1415 statements.SetParent(this);
1416 keywordSpan = reader.ReadSpan();
1417 leftParenSpan = reader.ReadSpan();
1418 rightParenSpan = reader.ReadSpan();
1419 }
1420
1421 ConditionalCompilationStatementNode::ConditionalCompilationStatementNode(const Span& span_) : StatementNode(NodeType::conditionalCompilationStatementNode, span_), ifPart(nullptr)
1422 {
1423 }
1424
1425 ConditionalCompilationStatementNode::ConditionalCompilationStatementNode(const Span& span_, ConditionalCompilationExpressionNode* ifExpr_) :
1426 StatementNode(NodeType::conditionalCompilationStatementNode, span_), ifPart(new ConditionalCompilationPartNode(span_, ifExpr_))
1427 {
1428 }
1429
1430 void ConditionalCompilationStatementNode::AddIfStatement(StatementNode* statement)
1431 {
1432 ifPart->AddStatement(statement);
1433 }
1434
1435 void ConditionalCompilationStatementNode::AddElifExpr(const Span& span, ConditionalCompilationExpressionNode* expr)
1436 {
1437 elifParts.Add(new ConditionalCompilationPartNode(span, expr));
1438 }
1439
1440 void ConditionalCompilationStatementNode::AddElifStatement(StatementNode* statement)
1441 {
1442 elifParts[elifParts.Count() - 1]->AddStatement(statement);
1443 }
1444
1445 void ConditionalCompilationStatementNode::SetElifLeftParenSpan(const Span& span)
1446 {
1447 elifParts[elifParts.Count() - 1]->SetLeftParenSpan(span);
1448 }
1449
1450 void ConditionalCompilationStatementNode::SetElifRightParenSpan(const Span& span)
1451 {
1452 elifParts[elifParts.Count() - 1]->SetRightParenSpan(span);
1453 }
1454
1455 void ConditionalCompilationStatementNode::SetElifKeywordSpan(const Span& span)
1456 {
1457 elifParts[elifParts.Count() - 1]->SetKeywordSpan(span);
1458 }
1459
1460 void ConditionalCompilationStatementNode::AddElseStatement(const Span& span, StatementNode* statement)
1461 {
1462 if (!elsePart)
1463 {
1464 elsePart.reset(new ConditionalCompilationPartNode(span));
1465 }
1466 elsePart->AddStatement(statement);
1467 }
1468
1469 Node* ConditionalCompilationStatementNode::Clone(CloneContext& cloneContext) const
1470 {
1471 ConditionalCompilationStatementNode* clone = new ConditionalCompilationStatementNode(GetSpan());
1472 ConditionalCompilationPartNode* clonedIfPart = static_cast<ConditionalCompilationPartNode*>(ifPart->Clone(cloneContext));
1473 clone->ifPart.reset(clonedIfPart);
1474 int n = elifParts.Count();
1475 for (int i = 0; i < n; ++i)
1476 {
1477 ConditionalCompilationPartNode* elifPart = elifParts[i];
1478 ConditionalCompilationPartNode* clonedElifPart = static_cast<ConditionalCompilationPartNode*>(elifPart->Clone(cloneContext));
1479 clone->elifParts.Add(clonedElifPart);
1480 }
1481 if (elsePart)
1482 {
1483 ConditionalCompilationPartNode* clonedElsePart = static_cast<ConditionalCompilationPartNode*>(elsePart->Clone(cloneContext));
1484 clone->elsePart.reset(clonedElsePart);
1485 }
1486 clone->SetEndIfSpan(endifSpan);
1487 return clone;
1488 }
1489
1490 void ConditionalCompilationStatementNode::Accept(Visitor& visitor)
1491 {
1492 visitor.Visit(*this);
1493 }
1494
1495 void ConditionalCompilationStatementNode::Write(AstWriter& writer)
1496 {
1497 StatementNode::Write(writer);
1498 writer.Write(ifPart.get());
1499 elifParts.Write(writer);
1500 bool hasElsePart = elsePart != nullptr;
1501 writer.GetBinaryWriter().Write(hasElsePart);
1502 if (hasElsePart)
1503 {
1504 writer.Write(elsePart.get());
1505 }
1506 writer.Write(endifSpan);
1507 }
1508
1509 void ConditionalCompilationStatementNode::Read(AstReader& reader)
1510 {
1511 StatementNode::Read(reader);
1512 ifPart.reset(reader.ReadConditionalCompilationPartNode());
1513 ifPart->SetParent(this);
1514 elifParts.Read(reader);
1515 elifParts.SetParent(this);
1516 bool hasElsePart = reader.GetBinaryReader().ReadBool();
1517 if (hasElsePart)
1518 {
1519 elsePart.reset(reader.ReadConditionalCompilationPartNode());
1520 elsePart->SetParent(this);
1521 }
1522 endifSpan = reader.ReadSpan();
1523 }
1524
1525 void ConditionalCompilationStatementNode::SetIfPart(ConditionalCompilationPartNode* ifPart_)
1526 {
1527 ifPart.reset(ifPart_);
1528 }
1529
1530 void ConditionalCompilationStatementNode::AddElifPart(ConditionalCompilationPartNode* elifPart)
1531 {
1532 elifParts.Add(elifPart);
1533 }
1534
1535 void ConditionalCompilationStatementNode::SetElsePart(ConditionalCompilationPartNode* elsePart_)
1536 {
1537 elsePart.reset(elsePart_);
1538 }
1539
1540 } }