source: src/AST/Convert.cpp@ d66e7b7

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 d66e7b7 was d66e7b7, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Removed ACCEPT_1 and ACCEPT_N from Convert.cpp. Use GET_ACCEPT_? instead.

  • Property mode set to 100644
File size: 16.1 KB
Line 
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//
7// Convert.cpp --
8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
11// Last Modified By : Andrew Beach
12// Last Modified On : Fri May 17 11:14:00 2019
13// Update Count : 2
14//
15
16#include "Convert.hpp"
17
18#include "AST/Pass.hpp"
19
20#include "AST/Attribute.hpp"
21#include "AST/Decl.hpp"
22#include "AST/Expr.hpp"
23#include "AST/Init.hpp"
24#include "AST/Stmt.hpp"
25
26
27#include "SynTree/Attribute.h"
28#include "SynTree/Declaration.h"
29
30//================================================================================================
31// Utilities
32template<template <class...> class C>
33struct to {
34 template<typename T>
35 static auto from( T && v ) -> C< typename T::value_type > {
36 C< typename T::value_type > l;
37 std::move(std::begin(v), std::end(v), std::back_inserter(l));
38 return l;
39 }
40};
41
42//================================================================================================
43class ConverterNewToOld {
44public:
45 std::list< Declaration * > translationUnit;
46};
47
48std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > & translationUnit ) {
49 ast::Pass<ConverterNewToOld> converter;
50 ast::accept_all(translationUnit, converter);
51 return converter.pass.translationUnit;
52}
53
54//================================================================================================
55
56class ConverterOldToNew : public Visitor {
57public:
58 ast::Decl * decl() {
59 return strict_dynamic_cast< ast::Decl * >( node );
60 }
61private:
62 ast::Node * node;
63
64 // Local Utilities:
65
66 template<typename NewT, typename OldT>
67 NewT * getAccept1( OldT old ) {
68 old->accept(*this);
69 return strict_dynamic_cast< NewT * >( node );
70 }
71
72# define GET_ACCEPT_1(child, type) \
73 getAccept1< ast::type, decltype( old->child ) >( old->child )
74
75 template<typename NewT, typename OldC>
76 std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
77 std::vector< ast::ptr<NewT> > ret;
78 ret.reserve( old.size() );
79 for ( auto a : old ) {
80 a->accept( *this );
81 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
82 }
83 return ret;
84 }
85
86# define GET_ACCEPT_V(child, type) \
87 getAcceptV< ast::type, decltype( old->child ) >( old->child )
88
89 ast::Label make_label(Label* old) {
90 return ast::Label(
91 old->labelled->location,
92 old->name,
93 GET_ACCEPT_V(attributes, Attribute)
94 );
95 }
96
97 template<template <class...> class C>
98 C<ast::Label> make_labels(C<Label> olds) {
99 C<ast::Label> ret;
100 for (auto oldn : olds) {
101 ret.push_back( make_label( &oldn ) );
102 }
103 return ret;
104 }
105
106# define GET_LABELS_V(labels) \
107 to<std::vector>::from( make_labels( std::move( labels ) ) )
108
109 // Now all the visit functions:
110
111 virtual void visit( ObjectDecl * old ) override final {
112 auto decl = new ast::ObjectDecl(
113 old->location,
114 old->name,
115 GET_ACCEPT_1(type, Type),
116 GET_ACCEPT_1(init, Init),
117 { old->get_storageClasses().val },
118 { old->linkage.val },
119 GET_ACCEPT_1(bitfieldWidth, Expr),
120 GET_ACCEPT_V(attributes, Attribute),
121 { old->get_funcSpec().val }
122 );
123 decl->scopeLevel = old->scopeLevel;
124 decl->mangleName = old->mangleName;
125 decl->isDeleted = old->isDeleted;
126 decl->uniqueId = old->uniqueId;
127 decl->extension = old->extension;
128
129 this->node = decl;
130 }
131
132 virtual void visit( FunctionDecl * ) override final {
133
134 }
135
136 virtual void visit( StructDecl * old ) override final {
137 auto decl = new ast::StructDecl(
138 old->location,
139 old->name,
140 old->kind,
141 GET_ACCEPT_V(attributes, Attribute),
142 { old->linkage.val }
143 );
144 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
145 decl->body = old->body;
146 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
147 decl->members = GET_ACCEPT_V(members, Decl);
148 decl->extension = old->extension;
149 decl->uniqueId = old->uniqueId;
150 decl->storage = { old->storageClasses.val };
151
152 this->node = decl;
153 }
154
155 virtual void visit( UnionDecl * old ) override final {
156 auto decl = new ast::UnionDecl(
157 old->location,
158 old->name,
159 GET_ACCEPT_V(attributes, Attribute),
160 { old->linkage.val }
161 );
162 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
163 decl->body = old->body;
164 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
165 decl->members = GET_ACCEPT_V(members, Decl);
166 decl->extension = old->extension;
167 decl->uniqueId = old->uniqueId;
168 decl->storage = { old->storageClasses.val };
169
170 this->node = decl;
171 }
172
173 virtual void visit( EnumDecl * old ) override final {
174 auto decl = new ast::UnionDecl(
175 old->location,
176 old->name,
177 GET_ACCEPT_V(attributes, Attribute),
178 { old->linkage.val }
179 );
180 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
181 decl->body = old->body;
182 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
183 decl->members = GET_ACCEPT_V(members, Decl);
184 decl->extension = old->extension;
185 decl->uniqueId = old->uniqueId;
186 decl->storage = { old->storageClasses.val };
187
188 this->node = decl;
189 }
190
191 virtual void visit( TraitDecl * old ) override final {
192 auto decl = new ast::UnionDecl(
193 old->location,
194 old->name,
195 GET_ACCEPT_V(attributes, Attribute),
196 { old->linkage.val }
197 );
198 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
199 decl->body = old->body;
200 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
201 decl->members = GET_ACCEPT_V(members, Decl);
202 decl->extension = old->extension;
203 decl->uniqueId = old->uniqueId;
204 decl->storage = { old->storageClasses.val };
205
206 this->node = decl;
207 }
208
209 virtual void visit( TypeDecl * ) override final {
210
211 }
212
213 virtual void visit( TypedefDecl * old ) override final {
214 auto decl = new ast::TypedefDecl(
215 old->location,
216 old->name,
217 { old->storageClasses.val },
218 GET_ACCEPT_1(base, Type),
219 { old->linkage.val }
220 );
221 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
222 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
223 decl->extension = old->extension;
224 decl->uniqueId = old->uniqueId;
225 decl->storage = { old->storageClasses.val };
226
227 this->node = decl;
228 }
229
230 virtual void visit( AsmDecl * ) override final {
231
232 }
233
234 virtual void visit( StaticAssertDecl * ) override final {
235
236 }
237
238 virtual void visit( CompoundStmt * old ) override final {
239 auto stmt = new ast::CompoundStmt(
240 old->location,
241 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
242 GET_LABELS_V(old->labels)
243 );
244
245 this->node = stmt;
246 }
247
248 virtual void visit( ExprStmt * old ) override final {
249 this->node = new ast::ExprStmt(
250 old->location,
251 GET_ACCEPT_1(expr, Expr),
252 GET_LABELS_V(old->labels)
253 );
254 }
255
256 virtual void visit( AsmStmt * old ) override final {
257 this->node = new ast::AsmStmt(
258 old->location,
259 old->voltile,
260 GET_ACCEPT_1(instruction, Expr),
261 GET_ACCEPT_V(output, Expr),
262 GET_ACCEPT_V(input, Expr),
263 GET_ACCEPT_V(clobber, ConstantExpr),
264 GET_LABELS_V(old->gotolabels),
265 GET_LABELS_V(old->labels)
266 );
267 }
268
269 virtual void visit( DirectiveStmt * old ) override final {
270 this->node = new ast::DirectiveStmt(
271 old->location,
272 old->directive,
273 GET_LABELS_V(old->labels)
274 );
275 }
276
277 virtual void visit( IfStmt * old ) override final {
278 this->node = new ast::IfStmt(
279 old->location,
280 GET_ACCEPT_1(condition, Expr),
281 GET_ACCEPT_1(thenPart, Stmt),
282 GET_ACCEPT_1(elsePart, Stmt),
283 GET_ACCEPT_V(initialization, Stmt),
284 GET_LABELS_V(old->labels)
285 );
286 }
287
288 virtual void visit( SwitchStmt * old ) override final {
289 this->node = new ast::SwitchStmt(
290 old->location,
291 GET_ACCEPT_1(condition, Expr),
292 GET_ACCEPT_V(statements, Stmt),
293 GET_LABELS_V(old->labels)
294 );
295 }
296
297 virtual void visit( CaseStmt * old ) override final {
298 this->node = new ast::CaseStmt(
299 old->location,
300 GET_ACCEPT_1(condition, Expr),
301 GET_ACCEPT_V(stmts, Stmt),
302 GET_LABELS_V(old->labels)
303 );
304 }
305
306 virtual void visit( WhileStmt * old ) override final {
307 this->node = new ast::WhileStmt(
308 old->location,
309 GET_ACCEPT_1(condition, Expr),
310 GET_ACCEPT_1(body, Stmt),
311 GET_ACCEPT_V(initialization, Stmt),
312 old->isDoWhile,
313 GET_LABELS_V(old->labels)
314 );
315 }
316
317 virtual void visit( ForStmt * old ) override final {
318 this->node = new ast::ForStmt(
319 old->location,
320 GET_ACCEPT_V(initialization, Stmt),
321 GET_ACCEPT_1(condition, Expr),
322 GET_ACCEPT_1(increment, Expr),
323 GET_ACCEPT_1(body, Stmt),
324 GET_LABELS_V(old->labels)
325 );
326 }
327
328 virtual void visit( BranchStmt * old ) override final {
329 if (old->computedTarget) {
330 this->node = new ast::BranchStmt(
331 old->location,
332 GET_ACCEPT_1(computedTarget, Expr),
333 GET_LABELS_V(old->labels)
334 );
335 } else {
336 ast::BranchStmt::Kind kind;
337 switch (old->type) {
338 #define CASE(n) \
339 case BranchStmt::n: \
340 kind = ast::BranchStmt::n; \
341 break
342 CASE(Goto);
343 CASE(Break);
344 CASE(Continue);
345 CASE(FallThrough);
346 CASE(FallThroughDefault);
347 #undef CASE
348 default:
349 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
350 }
351
352 Label label = old->originalTarget;
353 auto stmt = new ast::BranchStmt(
354 old->location,
355 kind,
356 make_label(&label),
357 GET_LABELS_V(old->labels)
358 );
359 stmt->target = make_label(&old->target);
360 this->node = stmt;
361 }
362 }
363
364 virtual void visit( ReturnStmt * old ) override final {
365 this->node = new ast::ReturnStmt(
366 old->location,
367 GET_ACCEPT_1(expr, Expr),
368 GET_LABELS_V(old->labels)
369 );
370 }
371
372 virtual void visit( ThrowStmt * old ) override final {
373 ast::ThrowStmt::Kind kind;
374 switch (old->kind) {
375 case ThrowStmt::Terminate:
376 kind = ast::ThrowStmt::Terminate;
377 break;
378 case ThrowStmt::Resume:
379 kind = ast::ThrowStmt::Resume;
380 break;
381 default:
382 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
383 }
384
385 this->node = new ast::ThrowStmt(
386 old->location,
387 kind,
388 GET_ACCEPT_1(expr, Expr),
389 GET_ACCEPT_1(target, Expr),
390 GET_LABELS_V(old->labels)
391 );
392 }
393
394 virtual void visit( TryStmt * old ) override final {
395 this->node = new ast::TryStmt(
396 old->location,
397 GET_ACCEPT_1(block, CompoundStmt),
398 GET_ACCEPT_V(handlers, CatchStmt),
399 GET_ACCEPT_1(finallyBlock, FinallyStmt),
400 GET_LABELS_V(old->labels)
401 );
402 }
403
404 virtual void visit( CatchStmt * old ) override final {
405 ast::CatchStmt::Kind kind;
406 switch (old->kind) {
407 case CatchStmt::Terminate:
408 kind = ast::CatchStmt::Terminate;
409 break;
410 case CatchStmt::Resume:
411 kind = ast::CatchStmt::Resume;
412 break;
413 default:
414 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
415 }
416
417 this->node = new ast::CatchStmt(
418 old->location,
419 kind,
420 GET_ACCEPT_1(decl, Decl),
421 GET_ACCEPT_1(cond, Expr),
422 GET_ACCEPT_1(body, Stmt),
423 GET_LABELS_V(old->labels)
424 );
425 }
426
427 virtual void visit( FinallyStmt * old ) override final {
428 this->node = new ast::FinallyStmt(
429 old->location,
430 GET_ACCEPT_1(block, CompoundStmt),
431 GET_LABELS_V(old->labels)
432 );
433 }
434
435 virtual void visit( WaitForStmt * old ) override final {
436 ast::WaitForStmt * stmt = new ast::WaitForStmt(
437 old->location,
438 GET_LABELS_V(old->labels)
439 );
440
441 stmt->clauses.reserve( old->clauses.size() );
442 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
443 stmt->clauses.push_back({
444 ast::WaitForStmt::Target{
445 GET_ACCEPT_1(clauses[i].target.function, Expr),
446 GET_ACCEPT_V(clauses[i].target.arguments, Expr)
447 },
448 GET_ACCEPT_1(clauses[i].statement, Stmt),
449 GET_ACCEPT_1(clauses[i].condition, Expr)
450 });
451 }
452 stmt->timeout = {
453 GET_ACCEPT_1(timeout.time, Expr),
454 GET_ACCEPT_1(timeout.statement, Stmt),
455 GET_ACCEPT_1(timeout.condition, Expr),
456 };
457 stmt->orElse = {
458 GET_ACCEPT_1(timeout.statement, Stmt),
459 GET_ACCEPT_1(timeout.condition, Expr),
460 };
461
462 this->node = stmt;
463 }
464
465 virtual void visit( WithStmt * old ) override final {
466 this->node = new ast::WithStmt(
467 old->location,
468 GET_ACCEPT_V(exprs, Expr),
469 GET_ACCEPT_1(stmt, Stmt),
470 GET_LABELS_V(old->labels)
471 );
472 }
473
474 virtual void visit( NullStmt * old ) override final {
475 this->node = new ast::NullStmt(
476 old->location,
477 GET_LABELS_V(old->labels)
478 );
479 }
480
481 virtual void visit( DeclStmt * old ) override final {
482 this->node = new ast::DeclStmt(
483 old->location,
484 GET_ACCEPT_1(decl, Decl),
485 GET_LABELS_V(old->labels)
486 );
487 }
488
489 virtual void visit( ImplicitCtorDtorStmt * old ) override final {
490 this->node = new ast::ImplicitCtorDtorStmt(
491 old->location,
492 GET_ACCEPT_1(callStmt, Stmt),
493 GET_LABELS_V(old->labels)
494 );
495 }
496
497 virtual void visit( ApplicationExpr * ) override final {
498
499 }
500
501 virtual void visit( UntypedExpr * ) override final {
502
503 }
504
505 virtual void visit( NameExpr * ) override final {
506
507 }
508
509 virtual void visit( CastExpr * ) override final {
510
511 }
512
513 virtual void visit( KeywordCastExpr * ) override final {
514
515 }
516
517 virtual void visit( VirtualCastExpr * ) override final {
518
519 }
520
521 virtual void visit( AddressExpr * ) override final {
522
523 }
524
525 virtual void visit( LabelAddressExpr * ) override final {
526
527 }
528
529 virtual void visit( UntypedMemberExpr * ) override final {
530
531 }
532
533 virtual void visit( MemberExpr * ) override final {
534
535 }
536
537 virtual void visit( VariableExpr * ) override final {
538
539 }
540
541 virtual void visit( ConstantExpr * ) override final {
542
543 }
544
545 virtual void visit( SizeofExpr * ) override final {
546
547 }
548
549 virtual void visit( AlignofExpr * ) override final {
550
551 }
552
553 virtual void visit( UntypedOffsetofExpr * ) override final {
554
555 }
556
557 virtual void visit( OffsetofExpr * ) override final {
558
559 }
560
561 virtual void visit( OffsetPackExpr * ) override final {
562
563 }
564
565 virtual void visit( LogicalExpr * ) override final {
566
567 }
568
569 virtual void visit( ConditionalExpr * ) override final {
570
571 }
572
573 virtual void visit( CommaExpr * ) override final {
574
575 }
576
577 virtual void visit( TypeExpr * ) override final {
578
579 }
580
581 virtual void visit( AsmExpr * ) override final {
582
583 }
584
585 virtual void visit( ImplicitCopyCtorExpr * ) override final {
586
587 }
588
589 virtual void visit( ConstructorExpr * ) override final {
590
591 }
592
593 virtual void visit( CompoundLiteralExpr * ) override final {
594
595 }
596
597 virtual void visit( RangeExpr * ) override final {
598
599 }
600
601 virtual void visit( UntypedTupleExpr * ) override final {
602
603 }
604
605 virtual void visit( TupleExpr * ) override final {
606
607 }
608
609 virtual void visit( TupleIndexExpr * ) override final {
610
611 }
612
613 virtual void visit( TupleAssignExpr * ) override final {
614
615 }
616
617 virtual void visit( StmtExpr * ) override final {
618
619 }
620
621 virtual void visit( UniqueExpr * ) override final {
622
623 }
624
625 virtual void visit( UntypedInitExpr * ) override final {
626
627 }
628
629 virtual void visit( InitExpr * ) override final {
630
631 }
632
633 virtual void visit( DeletedExpr * ) override final {
634
635 }
636
637 virtual void visit( DefaultArgExpr * ) override final {
638
639 }
640
641 virtual void visit( GenericExpr * ) override final {
642
643 }
644
645 virtual void visit( VoidType * ) override final {
646
647 }
648
649 virtual void visit( BasicType * ) override final {
650
651 }
652
653 virtual void visit( PointerType * ) override final {
654
655 }
656
657 virtual void visit( ArrayType * ) override final {
658
659 }
660
661 virtual void visit( ReferenceType * ) override final {
662
663 }
664
665 virtual void visit( QualifiedType * ) override final {
666
667 }
668
669 virtual void visit( FunctionType * ) override final {
670
671 }
672
673 virtual void visit( StructInstType * ) override final {
674
675 }
676
677 virtual void visit( UnionInstType * ) override final {
678
679 }
680
681 virtual void visit( EnumInstType * ) override final {
682
683 }
684
685 virtual void visit( TraitInstType * ) override final {
686
687 }
688
689 virtual void visit( TypeInstType * ) override final {
690
691 }
692
693 virtual void visit( TupleType * ) override final {
694
695 }
696
697 virtual void visit( TypeofType * ) override final {
698
699 }
700
701 virtual void visit( AttrType * ) override final {
702
703 }
704
705 virtual void visit( VarArgsType * ) override final {
706
707 }
708
709 virtual void visit( ZeroType * ) override final {
710
711 }
712
713 virtual void visit( OneType * ) override final {
714
715 }
716
717 virtual void visit( GlobalScopeType * ) override final {
718
719 }
720
721 virtual void visit( Designation * ) override final {
722
723 }
724
725 virtual void visit( SingleInit * ) override final {
726
727 }
728
729 virtual void visit( ListInit * ) override final {
730
731 }
732
733 virtual void visit( ConstructorInit * ) override final {
734
735 }
736
737 virtual void visit( Constant * ) override final {
738
739 }
740
741 virtual void visit( Attribute * ) override final {
742
743 }
744
745 virtual void visit( AttrExpr * ) override final {
746
747 assert( 0 );
748 }
749};
750
751#undef GET_LABELS_V
752#undef GET_ACCEPT_V
753#undef GET_ACCEPT_1
754
755std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > & translationUnit ) {
756 ConverterOldToNew c;
757 std::list< ast::ptr< ast::Decl > > decls;
758 for(auto d : translationUnit) {
759 d->accept( c );
760 decls.emplace_back( c.decl() );
761 delete d;
762 }
763 return decls;
764}
Note: See TracBrowser for help on using the repository browser.