source: src/AST/Convert.cpp@ 933f32f

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 933f32f was f6964ef, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 34.2 KB
RevLine 
[6d51bd7]1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[74dbbf6]7// Convert.cpp -- Convert between the new and old syntax trees.
[6d51bd7]8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
[6f8e87d]11// Last Modified By : Andrew Beach
[675d816]12// Last Modified On : Fri May 17 16:01:00 2019
13// Update Count : 4
[6d51bd7]14//
15
16#include "Convert.hpp"
17
18#include "AST/Attribute.hpp"
19#include "AST/Decl.hpp"
20#include "AST/Expr.hpp"
21#include "AST/Init.hpp"
22#include "AST/Stmt.hpp"
[172d9342]23#include "AST/TypeSubstitution.hpp"
[6d51bd7]24
25#include "SynTree/Attribute.h"
26#include "SynTree/Declaration.h"
[172d9342]27#include "SynTree/TypeSubstitution.h"
[6d51bd7]28
29//================================================================================================
30// Utilities
31template<template <class...> class C>
32struct to {
33 template<typename T>
34 static auto from( T && v ) -> C< typename T::value_type > {
35 C< typename T::value_type > l;
36 std::move(std::begin(v), std::end(v), std::back_inserter(l));
37 return l;
38 }
39};
40
41//================================================================================================
[74dbbf6]42class ConverterNewToOld : public ast::Visitor {
[675d816]43 BaseSyntaxNode * node = nullptr;
44
[74dbbf6]45 template<typename T>
[675d816]46 struct Getter {
47 ConverterNewToOld & visitor;
48
49 template<typename U>
50 T * accept1( const ast::ptr<U> & ptr ) {
51 ptr->accept( visitor );
52 T * ret = strict_dynamic_cast< T * >( visitor.node );
53 visitor.node = nullptr;
54 return ret;
55 }
56
57 template<typename U>
58 std::list< T * > acceptL( const U & container ) {
59 std::list< T * > ret;
60 for (auto ptr : container ) {
61 ret.emplace_back( accept1( ptr ) );
62 }
63 return ret;
64 }
65 };
66
67 template<typename T>
68 Getter<T> get() {
69 return Getter<T>{ *this };
70 }
71
72 Label makeLabel(Statement * labelled, const ast::Label& label) {
73 return Label(
74 label.name,
75 labelled,
76 get<Attribute>().acceptL(label.attributes)
77 );
78 }
79
80 template<template <class...> class C>
81 std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
82 std::list<Label> ret;
83 for (auto label : labels) {
84 ret.push_back( makeLabel(labelled, label) );
85 }
[74dbbf6]86 return ret;
87 }
88
[675d816]89public:
90 Declaration * decl( const ast::Decl * declNode ) {
91 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
92 }
[74dbbf6]93
[675d816]94private:
[74dbbf6]95 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
96 (void)node;
97 return nullptr;
98 }
99
100 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
101 (void)node;
102 return nullptr;
103 }
104
105 const ast::Decl * visit( const ast::StructDecl * node ) override final {
106 (void)node;
107 return nullptr;
108 }
109
110 const ast::Decl * visit( const ast::UnionDecl * node ) override final {
111 (void)node;
112 return nullptr;
113 }
114
115 const ast::Decl * visit( const ast::EnumDecl * node ) override final {
116 (void)node;
117 return nullptr;
118 }
119
120 const ast::Decl * visit( const ast::TraitDecl * node ) override final {
121 (void)node;
122 return nullptr;
123 }
124
125 const ast::Decl * visit( const ast::TypeDecl * node ) override final {
126 (void)node;
127 return nullptr;
128 }
129
130 const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
131 (void)node;
132 return nullptr;
133 }
134
135 const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
136 (void)node;
137 return nullptr;
138 }
139
140 const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
141 (void)node;
142 return nullptr;
143 }
144
145 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
[675d816]146 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
147 stmt->location = node->location;
148 stmt->labels = makeLabelL( stmt, node->labels );
149 this->node = stmt;
[74dbbf6]150 return nullptr;
151 }
152
153 const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
[675d816]154 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
155 stmt->location = node->location;
156 stmt->labels = makeLabelL( stmt, node->labels );
157 this->node = stmt;
[74dbbf6]158 return nullptr;
159 }
160
161 const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
[675d816]162 auto stmt = new AsmStmt(
163 node->isVolatile,
164 get<Expression>().accept1( node->instruction ),
165 get<Expression>().acceptL( node->output ),
166 get<Expression>().acceptL( node->input ),
167 get<ConstantExpr>().acceptL( node->clobber ),
168 makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
169 );
170 stmt->location = node->location;
171 stmt->labels = makeLabelL( stmt, node->labels );
172 this->node = stmt;
[74dbbf6]173 return nullptr;
174 }
175
176 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
[675d816]177 auto stmt = new DirectiveStmt( node->directive );
178 stmt->location = node->location;
179 stmt->labels = makeLabelL( stmt, node->labels );
180 this->node = stmt;
[74dbbf6]181 return nullptr;
182 }
183
184 const ast::Stmt * visit( const ast::IfStmt * node ) override final {
[675d816]185 auto stmt = new IfStmt(
186 get<Expression>().accept1( node->cond ),
187 get<Statement>().accept1( node->thenPart ),
188 get<Statement>().accept1( node->elsePart ),
189 get<Statement>().acceptL( node->inits )
190 );
191 stmt->location = node->location;
192 stmt->labels = makeLabelL( stmt, node->labels );
193 this->node = stmt;
[74dbbf6]194 return nullptr;
195 }
196
[675d816]197 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
198 auto stmt = new SwitchStmt(
199 get<Expression>().accept1( node->cond ),
200 get<Statement>().acceptL( node->stmts )
201 );
202 stmt->location = node->location;
203 stmt->labels = makeLabelL( stmt, node->labels );
204 this->node = stmt;
[74dbbf6]205 return nullptr;
206 }
207
[675d816]208 const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
209 auto stmt = new CaseStmt(
210 get<Expression>().accept1( node->cond ),
211 get<Statement>().acceptL( node->stmts ),
212 node->isDefault()
213 );
214 stmt->location = node->location;
215 stmt->labels = makeLabelL( stmt, node->labels );
216 this->node = stmt;
[74dbbf6]217 return nullptr;
218 }
219
[675d816]220 const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
221 auto inits = get<Statement>().acceptL( node->inits );
222 auto stmt = new WhileStmt(
223 get<Expression>().accept1( node->cond ),
224 get<Statement>().accept1( node->body ),
225 inits,
226 node->isDoWhile
227 );
228 stmt->location = node->location;
229 stmt->labels = makeLabelL( stmt, node->labels );
230 this->node = stmt;
[74dbbf6]231 return nullptr;
232 }
233
[675d816]234 const ast::Stmt * visit( const ast::ForStmt * node ) override final {
235 auto stmt = new ForStmt(
236 get<Statement>().acceptL( node->inits ),
237 get<Expression>().accept1( node->cond ),
238 get<Expression>().accept1( node->inc ),
239 get<Statement>().accept1( node->body )
240 );
241 stmt->location = node->location;
242 stmt->labels = makeLabelL( stmt, node->labels );
243 this->node = stmt;
[74dbbf6]244 return nullptr;
245 }
246
247 const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
[675d816]248 BranchStmt * stmt;
249 if (node->computedTarget) {
250 stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
251 BranchStmt::Goto );
252 } else {
253 BranchStmt::Type type;
254 switch (node->kind) {
255 #define CASE(n) \
256 case ast::BranchStmt::n: \
257 type = BranchStmt::n; \
258 break
259 CASE(Goto);
260 CASE(Break);
261 CASE(Continue);
262 CASE(FallThrough);
263 CASE(FallThroughDefault);
264 #undef CASE
265 default:
266 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
267 }
268
269 // The labels here are also weird.
270 stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
271 stmt->target = makeLabel( stmt, node->target );
272 }
273 stmt->location = node->location;
274 stmt->labels = makeLabelL( stmt, node->labels );
275 this->node = stmt;
[74dbbf6]276 return nullptr;
277 }
278
279 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
[675d816]280 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
281 stmt->location = node->location;
282 stmt->labels = makeLabelL( stmt, node->labels );
283 this->node = stmt;
[74dbbf6]284 return nullptr;
285 }
286
287 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
[675d816]288 ThrowStmt::Kind kind;
289 switch (node->kind) {
290 case ast::ThrowStmt::Terminate:
291 kind = ThrowStmt::Terminate;
292 break;
293 case ast::ThrowStmt::Resume:
294 kind = ThrowStmt::Resume;
295 break;
296 default:
297 assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
298 }
299 auto stmt = new ThrowStmt(
300 kind,
301 get<Expression>().accept1( node->expr ),
302 get<Expression>().accept1( node->target )
303 );
304 stmt->location = node->location;
305 stmt->labels = makeLabelL( stmt, node->labels );
306 this->node = stmt;
[74dbbf6]307 return nullptr;
308 }
309
310 const ast::Stmt * visit( const ast::TryStmt * node ) override final {
[675d816]311 auto handlers = get<CatchStmt>().acceptL( node->handlers );
312 auto stmt = new TryStmt(
313 get<CompoundStmt>().accept1( node->body ),
314 handlers,
315 get<FinallyStmt>().accept1( node->finally )
316 );
317 stmt->location = node->location;
318 stmt->labels = makeLabelL( stmt, node->labels );
319 this->node = stmt;
[74dbbf6]320 return nullptr;
321 }
322
323 const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
[675d816]324 CatchStmt::Kind kind;
325 switch (node->kind) {
326 case ast::CatchStmt::Terminate:
327 kind = CatchStmt::Terminate;
328 break;
329 case ast::CatchStmt::Resume:
330 kind = CatchStmt::Resume;
331 break;
332 default:
333 assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
334 }
335 auto stmt = new CatchStmt(
336 kind,
337 get<Declaration>().accept1( node->decl ),
338 get<Expression>().accept1( node->cond ),
339 get<Statement>().accept1( node->body )
340 );
341 stmt->location = node->location;
342 stmt->labels = makeLabelL( stmt, node->labels );
343 this->node = stmt;
[74dbbf6]344 return nullptr;
345 }
346
347 const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
[675d816]348 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
349 stmt->location = node->location;
350 stmt->labels = makeLabelL( stmt, node->labels );
351 this->node = stmt;
[74dbbf6]352 return nullptr;
353 }
354
355 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
[675d816]356 auto stmt = new WaitForStmt;
357 stmt->clauses.reserve( node->clauses.size() );
358 for ( auto clause : node->clauses ) {
359 stmt->clauses.push_back({{
360 get<Expression>().accept1( clause.target.function ),
361 get<Expression>().acceptL( clause.target.arguments ),
362 },
363 get<Statement>().accept1( clause.stmt ),
364 get<Expression>().accept1( clause.cond ),
365 });
366 }
367 stmt->timeout = {
368 get<Expression>().accept1( node->timeout.time ),
369 get<Statement>().accept1( node->timeout.stmt ),
370 get<Expression>().accept1( node->timeout.cond ),
371 };
372 stmt->orelse = {
373 get<Statement>().accept1( node->orElse.stmt ),
374 get<Expression>().accept1( node->orElse.cond ),
375 };
376 stmt->location = node->location;
377 stmt->labels = makeLabelL( stmt, node->labels );
378 this->node = stmt;
[74dbbf6]379 return nullptr;
380 }
381
382 const ast::Stmt * visit( const ast::WithStmt * node ) override final {
[675d816]383 auto stmt = new WithStmt(
384 get<Expression>().acceptL( node->exprs ),
385 get<Statement>().accept1( node->stmt )
386 );
387 stmt->location = node->location;
388 stmt->labels = makeLabelL( stmt, node->labels );
389 this->node = stmt;
[74dbbf6]390 return nullptr;
391 }
392
393 const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
[675d816]394 auto stmt = new NullStmt();
395 stmt->location = node->location;
396 stmt->labels = makeLabelL( stmt, node->labels );
397 this->node = stmt;
[74dbbf6]398 return nullptr;
399 }
400
401 const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
[675d816]402 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
403 stmt->location = node->location;
404 stmt->labels = makeLabelL( stmt, node->labels );
405 this->node = stmt;
[74dbbf6]406 return nullptr;
407 }
408
409 const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
410 (void)node;
411 return nullptr;
412 }
413
414 const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
415 (void)node;
416 return nullptr;
417 }
418
419 const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
420 (void)node;
421 return nullptr;
422 }
423
424 const ast::Expr * visit( const ast::NameExpr * node ) override final {
425 (void)node;
426 return nullptr;
427 }
428
429 const ast::Expr * visit( const ast::AddressExpr * node ) override final {
430 (void)node;
431 return nullptr;
432 }
433
434 const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
435 (void)node;
436 return nullptr;
437 }
438
439 const ast::Expr * visit( const ast::CastExpr * node ) override final {
440 (void)node;
441 return nullptr;
442 }
443
444 const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
445 (void)node;
446 return nullptr;
447 }
448
449 const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
450 (void)node;
451 return nullptr;
452 }
453
454 const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
455 (void)node;
456 return nullptr;
457 }
458
459 const ast::Expr * visit( const ast::MemberExpr * node ) override final {
460 (void)node;
461 return nullptr;
462 }
463
464 const ast::Expr * visit( const ast::VariableExpr * node ) override final {
465 (void)node;
466 return nullptr;
467 }
468
469 const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
470 (void)node;
471 return nullptr;
472 }
473
474 const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
475 (void)node;
476 return nullptr;
477 }
478
479 const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
480 (void)node;
481 return nullptr;
482 }
483
484 const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
485 (void)node;
486 return nullptr;
487 }
488
489 const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
490 (void)node;
491 return nullptr;
492 }
493
494 const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
495 (void)node;
496 return nullptr;
497 }
498
499 const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
500 (void)node;
501 return nullptr;
502 }
503
504 const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
505 (void)node;
506 return nullptr;
507 }
508
509 const ast::Expr * visit( const ast::CommaExpr * node ) override final {
510 (void)node;
511 return nullptr;
512 }
513
514 const ast::Expr * visit( const ast::TypeExpr * node ) override final {
515 (void)node;
516 return nullptr;
517 }
518
519 const ast::Expr * visit( const ast::AsmExpr * node ) override final {
520 (void)node;
521 return nullptr;
522 }
523
524 const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
525 (void)node;
526 return nullptr;
527 }
528
529 const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
530 (void)node;
531 return nullptr;
532 }
533
534 const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
535 (void)node;
536 return nullptr;
537 }
538
539 const ast::Expr * visit( const ast::RangeExpr * node ) override final {
540 (void)node;
541 return nullptr;
542 }
543
544 const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
545 (void)node;
546 return nullptr;
547 }
548
549 const ast::Expr * visit( const ast::TupleExpr * node ) override final {
550 (void)node;
551 return nullptr;
552 }
553
554 const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
555 (void)node;
556 return nullptr;
557 }
558
559 const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
560 (void)node;
561 return nullptr;
562 }
563
564 const ast::Expr * visit( const ast::StmtExpr * node ) override final {
565 (void)node;
566 return nullptr;
567 }
568
569 const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
570 (void)node;
571 return nullptr;
572 }
573
574 const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
575 (void)node;
576 return nullptr;
577 }
578
579 const ast::Expr * visit( const ast::InitExpr * node ) override final {
580 (void)node;
581 return nullptr;
582 }
583
584 const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
585 (void)node;
586 return nullptr;
587 }
588
589 const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
590 (void)node;
591 return nullptr;
592 }
593
594 const ast::Expr * visit( const ast::GenericExpr * node ) override final {
595 (void)node;
596 return nullptr;
597 }
598
599 const ast::Type * visit( const ast::VoidType * node ) override final {
600 (void)node;
601 return nullptr;
602 }
603
604 const ast::Type * visit( const ast::BasicType * node ) override final {
605 (void)node;
606 return nullptr;
607 }
608
609 const ast::Type * visit( const ast::PointerType * node ) override final {
610 (void)node;
611 return nullptr;
612 }
613
614 const ast::Type * visit( const ast::ArrayType * node ) override final {
615 (void)node;
616 return nullptr;
617 }
618
619 const ast::Type * visit( const ast::ReferenceType * node ) override final {
620 (void)node;
621 return nullptr;
622 }
623
624 const ast::Type * visit( const ast::QualifiedType * node ) override final {
625 (void)node;
626 return nullptr;
627 }
628
629 const ast::Type * visit( const ast::FunctionType * node ) override final {
630 (void)node;
631 return nullptr;
632 }
633
634 const ast::Type * visit( const ast::StructInstType * node ) override final {
635 (void)node;
636 return nullptr;
637 }
638
639 const ast::Type * visit( const ast::UnionInstType * node ) override final {
640 (void)node;
641 return nullptr;
642 }
643
644 const ast::Type * visit( const ast::EnumInstType * node ) override final {
645 (void)node;
646 return nullptr;
647 }
648
649 const ast::Type * visit( const ast::TraitInstType * node ) override final {
650 (void)node;
651 return nullptr;
652 }
653
654 const ast::Type * visit( const ast::TypeInstType * node ) override final {
655 (void)node;
656 return nullptr;
657 }
658
659 const ast::Type * visit( const ast::TupleType * node ) override final {
660 (void)node;
661 return nullptr;
662 }
663
664 const ast::Type * visit( const ast::TypeofType * node ) override final {
665 (void)node;
666 return nullptr;
667 }
668
669 const ast::Type * visit( const ast::VarArgsType * node ) override final {
670 (void)node;
671 return nullptr;
672 }
673
674 const ast::Type * visit( const ast::ZeroType * node ) override final {
675 (void)node;
676 return nullptr;
677 }
678
679 const ast::Type * visit( const ast::OneType * node ) override final {
680 (void)node;
681 return nullptr;
682 }
683
684 const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
685 (void)node;
686 return nullptr;
687 }
688
689 const ast::Designation * visit( const ast::Designation * node ) override final {
690 (void)node;
691 return nullptr;
692 }
693
694 const ast::Init * visit( const ast::SingleInit * node ) override final {
695 (void)node;
696 return nullptr;
697 }
698
699 const ast::Init * visit( const ast::ListInit * node ) override final {
700 (void)node;
701 return nullptr;
702 }
703
704 const ast::Init * visit( const ast::ConstructorInit * node ) override final {
705 (void)node;
706 return nullptr;
707 }
708
709 const ast::Attribute * visit( const ast::Attribute * node ) override final {
710 (void)node;
711 return nullptr;
712 }
713
714 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
715 (void)node;
716 return nullptr;
717 }
[6d51bd7]718};
719
[74dbbf6]720std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > && translationUnit ) {
721 ConverterNewToOld c;
722 std::list< Declaration * > decls;
723 for(auto d : translationUnit) {
[675d816]724 decls.emplace_back( c.decl( d ) );
[74dbbf6]725 delete d;
726 }
727 return decls;
[6d51bd7]728}
729
730//================================================================================================
731
732class ConverterOldToNew : public Visitor {
733public:
734 ast::Decl * decl() {
735 return strict_dynamic_cast< ast::Decl * >( node );
736 }
737private:
738 ast::Node * node;
739
[6f8e87d]740 // Local Utilities:
741
742 template<typename NewT, typename OldT>
743 NewT * getAccept1( OldT old ) {
744 old->accept(*this);
745 return strict_dynamic_cast< NewT * >( node );
746 }
747
748# define GET_ACCEPT_1(child, type) \
749 getAccept1< ast::type, decltype( old->child ) >( old->child )
750
751 template<typename NewT, typename OldC>
752 std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
753 std::vector< ast::ptr<NewT> > ret;
754 ret.reserve( old.size() );
755 for ( auto a : old ) {
756 a->accept( *this );
757 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
758 }
759 return ret;
760 }
761
762# define GET_ACCEPT_V(child, type) \
763 getAcceptV< ast::type, decltype( old->child ) >( old->child )
764
765 ast::Label make_label(Label* old) {
766 return ast::Label(
767 old->labelled->location,
768 old->name,
769 GET_ACCEPT_V(attributes, Attribute)
770 );
771 }
772
[6d51bd7]773 template<template <class...> class C>
774 C<ast::Label> make_labels(C<Label> olds) {
775 C<ast::Label> ret;
[6f8e87d]776 for (auto oldn : olds) {
777 ret.push_back( make_label( &oldn ) );
[6d51bd7]778 }
779 return ret;
780 }
781
[6f8e87d]782# define GET_LABELS_V(labels) \
783 to<std::vector>::from( make_labels( std::move( labels ) ) )
784
785 // Now all the visit functions:
786
[6d51bd7]787 virtual void visit( ObjectDecl * old ) override final {
788 auto decl = new ast::ObjectDecl(
789 old->location,
790 old->name,
[d66e7b7]791 GET_ACCEPT_1(type, Type),
792 GET_ACCEPT_1(init, Init),
[6d51bd7]793 { old->get_storageClasses().val },
794 { old->linkage.val },
[d66e7b7]795 GET_ACCEPT_1(bitfieldWidth, Expr),
796 GET_ACCEPT_V(attributes, Attribute),
[6d51bd7]797 { old->get_funcSpec().val }
798 );
799 decl->scopeLevel = old->scopeLevel;
800 decl->mangleName = old->mangleName;
801 decl->isDeleted = old->isDeleted;
802 decl->uniqueId = old->uniqueId;
803 decl->extension = old->extension;
804
805 this->node = decl;
806 }
807
808 virtual void visit( FunctionDecl * ) override final {
809
810 }
811
812 virtual void visit( StructDecl * old ) override final {
813 auto decl = new ast::StructDecl(
814 old->location,
815 old->name,
816 old->kind,
[d66e7b7]817 GET_ACCEPT_V(attributes, Attribute),
[6d51bd7]818 { old->linkage.val }
819 );
[d66e7b7]820 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
[6d51bd7]821 decl->body = old->body;
[d66e7b7]822 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
823 decl->members = GET_ACCEPT_V(members, Decl);
[6d51bd7]824 decl->extension = old->extension;
825 decl->uniqueId = old->uniqueId;
826 decl->storage = { old->storageClasses.val };
827
828 this->node = decl;
829 }
830
831 virtual void visit( UnionDecl * old ) override final {
832 auto decl = new ast::UnionDecl(
833 old->location,
834 old->name,
[d66e7b7]835 GET_ACCEPT_V(attributes, Attribute),
[6d51bd7]836 { old->linkage.val }
837 );
[d66e7b7]838 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
[6d51bd7]839 decl->body = old->body;
[d66e7b7]840 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
841 decl->members = GET_ACCEPT_V(members, Decl);
[6d51bd7]842 decl->extension = old->extension;
843 decl->uniqueId = old->uniqueId;
844 decl->storage = { old->storageClasses.val };
845
846 this->node = decl;
847 }
848
849 virtual void visit( EnumDecl * old ) override final {
850 auto decl = new ast::UnionDecl(
851 old->location,
852 old->name,
[d66e7b7]853 GET_ACCEPT_V(attributes, Attribute),
[6d51bd7]854 { old->linkage.val }
855 );
[d66e7b7]856 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
[6d51bd7]857 decl->body = old->body;
[d66e7b7]858 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
859 decl->members = GET_ACCEPT_V(members, Decl);
[6d51bd7]860 decl->extension = old->extension;
861 decl->uniqueId = old->uniqueId;
862 decl->storage = { old->storageClasses.val };
863
864 this->node = decl;
865 }
866
867 virtual void visit( TraitDecl * old ) override final {
868 auto decl = new ast::UnionDecl(
869 old->location,
870 old->name,
[d66e7b7]871 GET_ACCEPT_V(attributes, Attribute),
[6d51bd7]872 { old->linkage.val }
873 );
[d66e7b7]874 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
[6d51bd7]875 decl->body = old->body;
[d66e7b7]876 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
877 decl->members = GET_ACCEPT_V(members, Decl);
[6d51bd7]878 decl->extension = old->extension;
879 decl->uniqueId = old->uniqueId;
880 decl->storage = { old->storageClasses.val };
881
882 this->node = decl;
883 }
884
885 virtual void visit( TypeDecl * ) override final {
886
887 }
888
889 virtual void visit( TypedefDecl * old ) override final {
890 auto decl = new ast::TypedefDecl(
891 old->location,
892 old->name,
893 { old->storageClasses.val },
[d66e7b7]894 GET_ACCEPT_1(base, Type),
[6d51bd7]895 { old->linkage.val }
896 );
[d66e7b7]897 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
898 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
[6d51bd7]899 decl->extension = old->extension;
900 decl->uniqueId = old->uniqueId;
901 decl->storage = { old->storageClasses.val };
902
903 this->node = decl;
904 }
905
906 virtual void visit( AsmDecl * ) override final {
907
908 }
909
910 virtual void visit( StaticAssertDecl * ) override final {
911
912 }
913
914 virtual void visit( CompoundStmt * old ) override final {
915 auto stmt = new ast::CompoundStmt(
916 old->location,
[d66e7b7]917 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
918 GET_LABELS_V(old->labels)
[6d51bd7]919 );
920
921 this->node = stmt;
922 }
923
924 virtual void visit( ExprStmt * old ) override final {
[6f8e87d]925 this->node = new ast::ExprStmt(
[6d51bd7]926 old->location,
[6f8e87d]927 GET_ACCEPT_1(expr, Expr),
928 GET_LABELS_V(old->labels)
[6d51bd7]929 );
930 }
931
[6f8e87d]932 virtual void visit( AsmStmt * old ) override final {
933 this->node = new ast::AsmStmt(
934 old->location,
935 old->voltile,
936 GET_ACCEPT_1(instruction, Expr),
937 GET_ACCEPT_V(output, Expr),
938 GET_ACCEPT_V(input, Expr),
939 GET_ACCEPT_V(clobber, ConstantExpr),
940 GET_LABELS_V(old->gotolabels),
941 GET_LABELS_V(old->labels)
942 );
[6d51bd7]943 }
944
[6f8e87d]945 virtual void visit( DirectiveStmt * old ) override final {
946 this->node = new ast::DirectiveStmt(
947 old->location,
948 old->directive,
949 GET_LABELS_V(old->labels)
950 );
[6d51bd7]951 }
952
[6f8e87d]953 virtual void visit( IfStmt * old ) override final {
954 this->node = new ast::IfStmt(
955 old->location,
956 GET_ACCEPT_1(condition, Expr),
957 GET_ACCEPT_1(thenPart, Stmt),
958 GET_ACCEPT_1(elsePart, Stmt),
959 GET_ACCEPT_V(initialization, Stmt),
960 GET_LABELS_V(old->labels)
961 );
[6d51bd7]962 }
963
[6f8e87d]964 virtual void visit( SwitchStmt * old ) override final {
965 this->node = new ast::SwitchStmt(
966 old->location,
967 GET_ACCEPT_1(condition, Expr),
968 GET_ACCEPT_V(statements, Stmt),
969 GET_LABELS_V(old->labels)
970 );
[6d51bd7]971 }
972
[6f8e87d]973 virtual void visit( CaseStmt * old ) override final {
974 this->node = new ast::CaseStmt(
975 old->location,
976 GET_ACCEPT_1(condition, Expr),
977 GET_ACCEPT_V(stmts, Stmt),
978 GET_LABELS_V(old->labels)
979 );
[6d51bd7]980 }
981
[6f8e87d]982 virtual void visit( WhileStmt * old ) override final {
983 this->node = new ast::WhileStmt(
984 old->location,
985 GET_ACCEPT_1(condition, Expr),
986 GET_ACCEPT_1(body, Stmt),
987 GET_ACCEPT_V(initialization, Stmt),
988 old->isDoWhile,
989 GET_LABELS_V(old->labels)
990 );
[6d51bd7]991 }
992
[6f8e87d]993 virtual void visit( ForStmt * old ) override final {
994 this->node = new ast::ForStmt(
995 old->location,
996 GET_ACCEPT_V(initialization, Stmt),
997 GET_ACCEPT_1(condition, Expr),
998 GET_ACCEPT_1(increment, Expr),
999 GET_ACCEPT_1(body, Stmt),
1000 GET_LABELS_V(old->labels)
1001 );
[6d51bd7]1002 }
1003
[6f8e87d]1004 virtual void visit( BranchStmt * old ) override final {
1005 if (old->computedTarget) {
1006 this->node = new ast::BranchStmt(
1007 old->location,
1008 GET_ACCEPT_1(computedTarget, Expr),
1009 GET_LABELS_V(old->labels)
1010 );
1011 } else {
1012 ast::BranchStmt::Kind kind;
1013 switch (old->type) {
1014 #define CASE(n) \
1015 case BranchStmt::n: \
1016 kind = ast::BranchStmt::n; \
1017 break
1018 CASE(Goto);
1019 CASE(Break);
1020 CASE(Continue);
1021 CASE(FallThrough);
1022 CASE(FallThroughDefault);
1023 #undef CASE
[d66e7b7]1024 default:
1025 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
[6f8e87d]1026 }
1027
1028 Label label = old->originalTarget;
1029 auto stmt = new ast::BranchStmt(
1030 old->location,
1031 kind,
1032 make_label(&label),
1033 GET_LABELS_V(old->labels)
1034 );
1035 stmt->target = make_label(&old->target);
1036 this->node = stmt;
1037 }
[6d51bd7]1038 }
1039
[6f8e87d]1040 virtual void visit( ReturnStmt * old ) override final {
1041 this->node = new ast::ReturnStmt(
1042 old->location,
1043 GET_ACCEPT_1(expr, Expr),
1044 GET_LABELS_V(old->labels)
1045 );
[6d51bd7]1046 }
1047
[6f8e87d]1048 virtual void visit( ThrowStmt * old ) override final {
1049 ast::ThrowStmt::Kind kind;
1050 switch (old->kind) {
1051 case ThrowStmt::Terminate:
1052 kind = ast::ThrowStmt::Terminate;
1053 break;
1054 case ThrowStmt::Resume:
1055 kind = ast::ThrowStmt::Resume;
1056 break;
[d66e7b7]1057 default:
1058 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
[6f8e87d]1059 }
[6d51bd7]1060
[6f8e87d]1061 this->node = new ast::ThrowStmt(
1062 old->location,
1063 kind,
1064 GET_ACCEPT_1(expr, Expr),
1065 GET_ACCEPT_1(target, Expr),
1066 GET_LABELS_V(old->labels)
1067 );
[6d51bd7]1068 }
1069
[6f8e87d]1070 virtual void visit( TryStmt * old ) override final {
1071 this->node = new ast::TryStmt(
1072 old->location,
1073 GET_ACCEPT_1(block, CompoundStmt),
1074 GET_ACCEPT_V(handlers, CatchStmt),
1075 GET_ACCEPT_1(finallyBlock, FinallyStmt),
1076 GET_LABELS_V(old->labels)
1077 );
[6d51bd7]1078 }
1079
[6f8e87d]1080 virtual void visit( CatchStmt * old ) override final {
1081 ast::CatchStmt::Kind kind;
1082 switch (old->kind) {
1083 case CatchStmt::Terminate:
1084 kind = ast::CatchStmt::Terminate;
1085 break;
1086 case CatchStmt::Resume:
1087 kind = ast::CatchStmt::Resume;
1088 break;
[d66e7b7]1089 default:
1090 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
[6f8e87d]1091 }
[6d51bd7]1092
[6f8e87d]1093 this->node = new ast::CatchStmt(
1094 old->location,
1095 kind,
1096 GET_ACCEPT_1(decl, Decl),
1097 GET_ACCEPT_1(cond, Expr),
1098 GET_ACCEPT_1(body, Stmt),
1099 GET_LABELS_V(old->labels)
1100 );
[6d51bd7]1101 }
1102
[6f8e87d]1103 virtual void visit( FinallyStmt * old ) override final {
1104 this->node = new ast::FinallyStmt(
1105 old->location,
1106 GET_ACCEPT_1(block, CompoundStmt),
1107 GET_LABELS_V(old->labels)
1108 );
[6d51bd7]1109 }
1110
[6f8e87d]1111 virtual void visit( WaitForStmt * old ) override final {
1112 ast::WaitForStmt * stmt = new ast::WaitForStmt(
1113 old->location,
1114 GET_LABELS_V(old->labels)
1115 );
[6d51bd7]1116
[6f8e87d]1117 stmt->clauses.reserve( old->clauses.size() );
1118 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
1119 stmt->clauses.push_back({
1120 ast::WaitForStmt::Target{
1121 GET_ACCEPT_1(clauses[i].target.function, Expr),
1122 GET_ACCEPT_V(clauses[i].target.arguments, Expr)
1123 },
1124 GET_ACCEPT_1(clauses[i].statement, Stmt),
1125 GET_ACCEPT_1(clauses[i].condition, Expr)
1126 });
1127 }
1128 stmt->timeout = {
1129 GET_ACCEPT_1(timeout.time, Expr),
1130 GET_ACCEPT_1(timeout.statement, Stmt),
1131 GET_ACCEPT_1(timeout.condition, Expr),
1132 };
1133 stmt->orElse = {
1134 GET_ACCEPT_1(timeout.statement, Stmt),
1135 GET_ACCEPT_1(timeout.condition, Expr),
1136 };
[6d51bd7]1137
[6f8e87d]1138 this->node = stmt;
1139 }
[6d51bd7]1140
[6f8e87d]1141 virtual void visit( WithStmt * old ) override final {
1142 this->node = new ast::WithStmt(
1143 old->location,
1144 GET_ACCEPT_V(exprs, Expr),
1145 GET_ACCEPT_1(stmt, Stmt),
1146 GET_LABELS_V(old->labels)
1147 );
[6d51bd7]1148 }
1149
1150 virtual void visit( NullStmt * old ) override final {
[6f8e87d]1151 this->node = new ast::NullStmt(
[6d51bd7]1152 old->location,
[6f8e87d]1153 GET_LABELS_V(old->labels)
[6d51bd7]1154 );
1155 }
1156
[6f8e87d]1157 virtual void visit( DeclStmt * old ) override final {
1158 this->node = new ast::DeclStmt(
1159 old->location,
1160 GET_ACCEPT_1(decl, Decl),
1161 GET_LABELS_V(old->labels)
1162 );
[6d51bd7]1163 }
1164
[6f8e87d]1165 virtual void visit( ImplicitCtorDtorStmt * old ) override final {
1166 this->node = new ast::ImplicitCtorDtorStmt(
1167 old->location,
1168 GET_ACCEPT_1(callStmt, Stmt),
1169 GET_LABELS_V(old->labels)
1170 );
[6d51bd7]1171 }
1172
[172d9342]1173 ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
1174
1175 ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
1176
1177 for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
1178 rslt->add( old_i->first,
1179 getAccept1<ast::Type>(old_i->second) );
1180 }
[6d51bd7]1181
[172d9342]1182 for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
1183 rslt->addVar( old_i->first,
1184 getAccept1<ast::Expr>(old_i->second) );
1185 }
[6d51bd7]1186 }
1187
[172d9342]1188 void convertInferUnion(ast::Expr::InferUnion &nwInferred, InferredParams oldInferParams, const std::vector<UniqueId> &oldResnSlots) {
1189
1190 (void) nwInferred;
1191 (void) oldInferParams;
1192 (void) oldResnSlots;
1193
1194 // TODO
1195 }
1196
1197 ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
[6d51bd7]1198
[172d9342]1199 nw->result = GET_ACCEPT_1(result, Type);
1200 nw->env = convertTypeSubstitution(old->env);
1201
1202 nw->extension = old->extension;
1203 convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
1204
1205 return nw;
1206 }
[6d51bd7]1207
[172d9342]1208 virtual void visit( ApplicationExpr * ) override final {
1209 // TODO
[6d51bd7]1210 }
1211
[172d9342]1212 virtual void visit( UntypedExpr * ) override final {
1213 // TODO
1214 }
[6d51bd7]1215
[172d9342]1216 virtual void visit( NameExpr * old ) override final {
1217 this->node = visitBaseExpr( old,
1218 new ast::NameExpr(
1219 old->location,
1220 old->get_name()
1221 )
1222 );
[6d51bd7]1223 }
1224
1225 virtual void visit( CastExpr * ) override final {
[172d9342]1226 // TODO ... (rest)
[6d51bd7]1227 }
1228
1229 virtual void visit( KeywordCastExpr * ) override final {
1230
1231 }
1232
1233 virtual void visit( VirtualCastExpr * ) override final {
1234
1235 }
1236
1237 virtual void visit( AddressExpr * ) override final {
1238
1239 }
1240
1241 virtual void visit( LabelAddressExpr * ) override final {
1242
1243 }
1244
1245 virtual void visit( UntypedMemberExpr * ) override final {
1246
1247 }
1248
1249 virtual void visit( MemberExpr * ) override final {
1250
1251 }
1252
1253 virtual void visit( VariableExpr * ) override final {
1254
1255 }
1256
1257 virtual void visit( ConstantExpr * ) override final {
1258
1259 }
1260
1261 virtual void visit( SizeofExpr * ) override final {
1262
1263 }
1264
1265 virtual void visit( AlignofExpr * ) override final {
1266
1267 }
1268
1269 virtual void visit( UntypedOffsetofExpr * ) override final {
1270
1271 }
1272
1273 virtual void visit( OffsetofExpr * ) override final {
1274
1275 }
1276
1277 virtual void visit( OffsetPackExpr * ) override final {
1278
1279 }
1280
1281 virtual void visit( LogicalExpr * ) override final {
1282
1283 }
1284
1285 virtual void visit( ConditionalExpr * ) override final {
1286
1287 }
1288
1289 virtual void visit( CommaExpr * ) override final {
1290
1291 }
1292
1293 virtual void visit( TypeExpr * ) override final {
1294
1295 }
1296
1297 virtual void visit( AsmExpr * ) override final {
1298
1299 }
1300
1301 virtual void visit( ImplicitCopyCtorExpr * ) override final {
1302
1303 }
1304
1305 virtual void visit( ConstructorExpr * ) override final {
1306
1307 }
1308
1309 virtual void visit( CompoundLiteralExpr * ) override final {
1310
1311 }
1312
1313 virtual void visit( RangeExpr * ) override final {
1314
1315 }
1316
1317 virtual void visit( UntypedTupleExpr * ) override final {
1318
1319 }
1320
1321 virtual void visit( TupleExpr * ) override final {
1322
1323 }
1324
1325 virtual void visit( TupleIndexExpr * ) override final {
1326
1327 }
1328
1329 virtual void visit( TupleAssignExpr * ) override final {
1330
1331 }
1332
1333 virtual void visit( StmtExpr * ) override final {
1334
1335 }
1336
1337 virtual void visit( UniqueExpr * ) override final {
1338
1339 }
1340
1341 virtual void visit( UntypedInitExpr * ) override final {
1342
1343 }
1344
1345 virtual void visit( InitExpr * ) override final {
1346
1347 }
1348
1349 virtual void visit( DeletedExpr * ) override final {
1350
1351 }
1352
1353 virtual void visit( DefaultArgExpr * ) override final {
1354
1355 }
1356
1357 virtual void visit( GenericExpr * ) override final {
1358
1359 }
1360
1361 virtual void visit( VoidType * ) override final {
1362
1363 }
1364
1365 virtual void visit( BasicType * ) override final {
1366
1367 }
1368
1369 virtual void visit( PointerType * ) override final {
1370
1371 }
1372
1373 virtual void visit( ArrayType * ) override final {
1374
1375 }
1376
1377 virtual void visit( ReferenceType * ) override final {
1378
1379 }
1380
1381 virtual void visit( QualifiedType * ) override final {
1382
1383 }
1384
1385 virtual void visit( FunctionType * ) override final {
1386
1387 }
1388
1389 virtual void visit( StructInstType * ) override final {
1390
1391 }
1392
1393 virtual void visit( UnionInstType * ) override final {
1394
1395 }
1396
1397 virtual void visit( EnumInstType * ) override final {
1398
1399 }
1400
1401 virtual void visit( TraitInstType * ) override final {
1402
1403 }
1404
1405 virtual void visit( TypeInstType * ) override final {
1406
1407 }
1408
1409 virtual void visit( TupleType * ) override final {
1410
1411 }
1412
1413 virtual void visit( TypeofType * ) override final {
1414
1415 }
1416
1417 virtual void visit( AttrType * ) override final {
1418
1419 }
1420
1421 virtual void visit( VarArgsType * ) override final {
1422
1423 }
1424
1425 virtual void visit( ZeroType * ) override final {
1426
1427 }
1428
1429 virtual void visit( OneType * ) override final {
1430
1431 }
1432
1433 virtual void visit( GlobalScopeType * ) override final {
1434
1435 }
1436
1437 virtual void visit( Designation * ) override final {
1438
1439 }
1440
1441 virtual void visit( SingleInit * ) override final {
1442
1443 }
1444
1445 virtual void visit( ListInit * ) override final {
1446
1447 }
1448
1449 virtual void visit( ConstructorInit * ) override final {
1450
1451 }
1452
1453 virtual void visit( Constant * ) override final {
1454
1455 }
1456
1457 virtual void visit( Attribute * ) override final {
1458
1459 }
[b336af9]1460
1461 virtual void visit( AttrExpr * ) override final {
1462
1463 assert( 0 );
1464 }
[6d51bd7]1465};
1466
[6f8e87d]1467#undef GET_LABELS_V
1468#undef GET_ACCEPT_V
1469#undef GET_ACCEPT_1
1470
[74dbbf6]1471std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
[6d51bd7]1472 ConverterOldToNew c;
1473 std::list< ast::ptr< ast::Decl > > decls;
1474 for(auto d : translationUnit) {
1475 d->accept( c );
1476 decls.emplace_back( c.decl() );
1477 delete d;
1478 }
1479 return decls;
[6f8e87d]1480}
Note: See TracBrowser for help on using the repository browser.