source: src/AST/Convert.cpp @ 10a1225

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 10a1225 was 6f8e87d, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Filled in ConverterOldToNew? for Stmt. Also updated some utilities.

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