source: src/AST/Convert.cpp @ b0abc8a0

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b0abc8a0 was ed5e798, checked in by Aaron Moss <a3moss@…>, 5 years ago

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

  • Property mode set to 100644
File size: 58.4 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 -- Convert between the new and old syntax trees.
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 : Tue May 21 15:30:00 2019
13// Update Count     : 5
14//
15
16#include "Convert.hpp"
17
18#include <unordered_map>
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#include "AST/TypeSubstitution.hpp"
26
27#include "SynTree/Attribute.h"
28#include "SynTree/Declaration.h"
29#include "SynTree/TypeSubstitution.h"
30
31//================================================================================================
32// Utilities
33template<template <class...> class C>
34struct to {
35        template<typename T>
36        static auto from( T && v ) -> C< typename T::value_type > {
37                C< typename T::value_type > l;
38                std::move(std::begin(v), std::end(v), std::back_inserter(l));
39                return l;
40        }
41};
42
43//================================================================================================
44class ConverterNewToOld : public ast::Visitor {
45        BaseSyntaxNode * node = nullptr;
46        using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
47        Cache cache;
48
49        template<typename T>
50        struct Getter {
51                ConverterNewToOld & visitor;
52
53                template<typename U, enum ast::Node::ref_type R>
54                T * accept1( const ast::ptr_base<U, R> & ptr ) {
55                        if ( ! ptr ) return nullptr;
56                        ptr->accept( visitor );
57                        T * ret = strict_dynamic_cast< T * >( visitor.node );
58                        visitor.node = nullptr;
59                        return ret;
60                }
61
62                template<typename U>
63                std::list< T * > acceptL( const U & container ) {
64                        std::list< T * > ret;
65                        for (auto ptr : container ) {
66                                ret.emplace_back( accept1( ptr ) );
67                        }
68                        return ret;
69                }
70        };
71
72    template<typename T>
73    Getter<T> get() {
74        return Getter<T>{ *this };
75    }
76
77        Label makeLabel(Statement * labelled, const ast::Label& label) {
78                return Label(
79                        label.name,
80                        labelled,
81                        get<Attribute>().acceptL(label.attributes)
82                );
83        }
84
85        template<template <class...> class C>
86        std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
87                std::list<Label> ret;
88                for (auto label : labels) {
89                        ret.push_back( makeLabel(labelled, label) );
90                }
91                return ret;
92        }
93
94        /// get new qualifiers from old type
95        Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
96
97        /// returns true and sets `node` if in cache
98        bool inCache( const ast::Node * node ) {
99                auto it = cache.find( node );
100                if ( it == cache.end() ) return false;
101                this->node = it->second;
102                return true;
103        }
104
105public:
106        Declaration * decl( const ast::Decl * declNode ) {
107                return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
108        }
109
110private:
111        void declPostamble( Declaration * decl, const ast::Decl * node ) {
112                decl->location = node->location;
113                // name comes from constructor
114                // linkage comes from constructor
115                decl->extension = node->extension;
116                decl->uniqueId = node->uniqueId;
117                // storageClasses comes from constructor
118                this->node = decl;
119        }
120
121        const ast::DeclWithType * declWithTypePostamble (
122                        DeclarationWithType * decl, const ast::DeclWithType * node ) {
123                declPostamble( decl, node );
124                decl->mangleName = node->mangleName;
125                decl->scopeLevel = node->scopeLevel;
126                decl->asmName = get<Expression>().accept1( node->asmName );
127                // attributes comes from constructor
128                decl->isDeleted = node->isDeleted;
129                // fs comes from constructor
130                cache.emplace( node, decl );
131                return nullptr;
132        }
133
134        const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
135                if ( inCache( node ) ) return nullptr;
136                auto decl = new ObjectDecl(
137                        node->name,
138                        Type::StorageClasses( node->storage.val ),
139                        LinkageSpec::Spec( node->linkage.val ),
140                        get<Expression>().accept1( node->bitfieldWidth ),
141                        get<Type>().accept1( node->type ),
142                        get<Initializer>().accept1( node->init ),
143                        get<Attribute>().acceptL( node->attributes ),
144                        Type::FuncSpecifiers( node->funcSpec.val )
145                );
146                return declWithTypePostamble( decl, node );
147        }
148
149        const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
150                if ( inCache( node ) ) return nullptr;
151                auto decl = new FunctionDecl(
152                        node->name,
153                        Type::StorageClasses( node->storage.val ),
154                        LinkageSpec::Spec( node->linkage.val ),
155                        get<FunctionType>().accept1( node->type ),
156                        get<CompoundStmt>().accept1( node->stmts ),
157                        get<Attribute>().acceptL( node->attributes ),
158                        Type::FuncSpecifiers( node->funcSpec.val )
159                );
160                decl->withExprs = get<Expression>().acceptL( node->withExprs );
161                return declWithTypePostamble( decl, node );
162        }
163
164        // NamedTypeDecl
165        const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) {
166                declPostamble( decl, node );
167                // base comes from constructor
168                decl->parameters = get<TypeDecl>().acceptL( node->params );
169                decl->assertions = get<DeclarationWithType>().acceptL( node->assertions );
170                return nullptr;
171        }
172
173        const ast::Decl * visit( const ast::TypeDecl * node ) override final {
174                if ( inCache( node ) ) return nullptr;
175                auto decl = new TypeDecl(
176                        node->name,
177                        Type::StorageClasses( node->storage.val ),
178                        get<Type>().accept1( node->base ),
179                        (TypeDecl::Kind)(unsigned)node->kind,
180                        node->sized,
181                        get<Type>().accept1( node->init )
182                );
183                cache.emplace( node, decl );
184                return namedTypePostamble( decl, node );
185        }
186
187        const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
188                auto decl = new TypedefDecl(
189                        node->name,
190                        node->location,
191                        Type::StorageClasses( node->storage.val ),
192            get<Type>().accept1( node->base ),
193                        LinkageSpec::Spec( node->linkage.val )
194                );
195                return namedTypePostamble( decl, node );
196        }
197
198        const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) {
199                decl->members = get<Declaration>().acceptL( node->members );
200                decl->parameters = get<TypeDecl>().acceptL( node->params );
201                decl->body = node->body;
202                // attributes come from constructor
203                decl->parent = get<AggregateDecl>().accept1( node->parent );
204                cache.emplace( node, decl );
205                return nullptr;
206        }
207
208        const ast::Decl * visit( const ast::StructDecl * node ) override final {
209                if ( inCache( node ) ) return nullptr;
210                auto decl = new StructDecl(
211                        node->name,
212                        node->kind,
213                        get<Attribute>().acceptL( node->attributes ),
214                        LinkageSpec::Spec( node->linkage.val )
215                );
216                return aggregatePostamble( decl, node );
217        }
218
219        const ast::Decl * visit( const ast::UnionDecl * node ) override final {
220                if ( inCache( node ) ) return nullptr;
221                auto decl = new UnionDecl(
222                        node->name,
223                        get<Attribute>().acceptL( node->attributes ),
224                        LinkageSpec::Spec( node->linkage.val )
225                );
226                return aggregatePostamble( decl, node );
227        }
228
229        const ast::Decl * visit( const ast::EnumDecl * node ) override final {
230                if ( inCache( node ) ) return nullptr;
231                auto decl = new EnumDecl(
232                        node->name,
233                        get<Attribute>().acceptL( node->attributes ),
234                        LinkageSpec::Spec( node->linkage.val )
235                );
236                return aggregatePostamble( decl, node );
237        }
238
239        const ast::Decl * visit( const ast::TraitDecl * node ) override final {
240                if ( inCache( node ) ) return nullptr;
241                auto decl = new TraitDecl(
242                        node->name,
243                        {},
244                        LinkageSpec::Spec( node->linkage.val )
245                );
246                return aggregatePostamble( decl, node );
247        }
248
249        const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
250                auto decl = new AsmDecl( get<AsmStmt>().accept1( node->stmt ) );
251                declPostamble( decl, node );
252                return nullptr;
253        }
254
255        const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
256                auto decl = new StaticAssertDecl(
257                        get<Expression>().accept1( node->cond ),
258                        get<ConstantExpr>().accept1( node->msg )
259                );
260                declPostamble( decl, node );
261                return nullptr;
262        }
263
264        const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
265                stmt->location = node->location;
266                stmt->labels = makeLabelL( stmt, node->labels );
267                this->node = stmt;
268                return nullptr;
269        }
270
271        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
272                auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
273                stmtPostamble( stmt, node );
274                return nullptr;
275        }
276
277        const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
278                auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
279                return stmtPostamble( stmt, node );
280        }
281
282        const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
283                auto stmt = new AsmStmt(
284                        node->isVolatile,
285                        get<Expression>().accept1( node->instruction ),
286                        get<Expression>().acceptL( node->output ),
287                        get<Expression>().acceptL( node->input ),
288                        get<ConstantExpr>().acceptL( node->clobber ),
289                        makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
290                );
291                return stmtPostamble( stmt, node );
292        }
293
294        const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
295                auto stmt = new DirectiveStmt( node->directive );
296                return stmtPostamble( stmt, node );
297        }
298
299        const ast::Stmt * visit( const ast::IfStmt * node ) override final {
300                auto stmt = new IfStmt(
301                        get<Expression>().accept1( node->cond ),
302                        get<Statement>().accept1( node->thenPart ),
303                        get<Statement>().accept1( node->elsePart ),
304                        get<Statement>().acceptL( node->inits )
305                );
306                return stmtPostamble( stmt, node );
307        }
308
309        const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
310                auto stmt = new SwitchStmt(
311                        get<Expression>().accept1( node->cond ),
312                        get<Statement>().acceptL( node->stmts )
313                );
314                return stmtPostamble( stmt, node );
315        }
316
317        const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
318                auto stmt = new CaseStmt(
319                        get<Expression>().accept1( node->cond ),
320                        get<Statement>().acceptL( node->stmts ),
321                        node->isDefault()
322                );
323                return stmtPostamble( stmt, node );
324        }
325
326        const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
327                auto inits = get<Statement>().acceptL( node->inits );
328                auto stmt = new WhileStmt(
329                        get<Expression>().accept1( node->cond ),
330                        get<Statement>().accept1( node->body ),
331                        inits,
332                        node->isDoWhile
333                );
334                return stmtPostamble( stmt, node );
335        }
336
337        const ast::Stmt * visit( const ast::ForStmt * node ) override final {
338                auto stmt = new ForStmt(
339                        get<Statement>().acceptL( node->inits ),
340                        get<Expression>().accept1( node->cond ),
341                        get<Expression>().accept1( node->inc ),
342                        get<Statement>().accept1( node->body )
343                );
344                return stmtPostamble( stmt, node );
345        }
346
347        const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
348                BranchStmt * stmt;
349                if (node->computedTarget) {
350                        stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
351                                BranchStmt::Goto );
352                } else {
353                        BranchStmt::Type type;
354                        switch (node->kind) {
355                        #define CASE(n) \
356                        case ast::BranchStmt::n: \
357                                type = BranchStmt::n; \
358                                break
359                        CASE(Goto);
360                        CASE(Break);
361                        CASE(Continue);
362                        CASE(FallThrough);
363                        CASE(FallThroughDefault);
364                        #undef CASE
365                        default:
366                                assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
367                        }
368
369                        // The labels here are also weird.
370                        stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
371                        stmt->target = makeLabel( stmt, node->target );
372                }
373                return stmtPostamble( stmt, node );
374        }
375
376        const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
377                auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
378                return stmtPostamble( stmt, node );
379        }
380
381        const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
382                ThrowStmt::Kind kind;
383                switch (node->kind) {
384                case ast::ThrowStmt::Terminate:
385                        kind = ThrowStmt::Terminate;
386                        break;
387                case ast::ThrowStmt::Resume:
388                        kind = ThrowStmt::Resume;
389                        break;
390                default:
391                        assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
392                }
393                auto stmt = new ThrowStmt(
394                        kind,
395                        get<Expression>().accept1( node->expr ),
396                        get<Expression>().accept1( node->target )
397                );
398                return stmtPostamble( stmt, node );
399        }
400
401        const ast::Stmt * visit( const ast::TryStmt * node ) override final {
402                auto handlers = get<CatchStmt>().acceptL( node->handlers );
403                auto stmt = new TryStmt(
404                        get<CompoundStmt>().accept1( node->body ),
405                        handlers,
406                        get<FinallyStmt>().accept1( node->finally )
407                );
408                return stmtPostamble( stmt, node );
409        }
410
411        const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
412                CatchStmt::Kind kind;
413                switch (node->kind) {
414                case ast::CatchStmt::Terminate:
415                        kind = CatchStmt::Terminate;
416                        break;
417                case ast::CatchStmt::Resume:
418                        kind = CatchStmt::Resume;
419                        break;
420                default:
421                        assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
422                }
423                auto stmt = new CatchStmt(
424                        kind,
425                        get<Declaration>().accept1( node->decl ),
426                        get<Expression>().accept1( node->cond ),
427                        get<Statement>().accept1( node->body )
428                );
429                return stmtPostamble( stmt, node );
430        }
431
432        const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
433                auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
434                return stmtPostamble( stmt, node );
435        }
436
437        const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
438                auto stmt = new WaitForStmt;
439                stmt->clauses.reserve( node->clauses.size() );
440                for ( auto clause : node->clauses ) {
441                        stmt->clauses.push_back({{
442                                        get<Expression>().accept1( clause.target.func ),
443                                        get<Expression>().acceptL( clause.target.args ),
444                                },
445                                get<Statement>().accept1( clause.stmt ),
446                                get<Expression>().accept1( clause.cond ),
447                        });
448                }
449                stmt->timeout = {
450                        get<Expression>().accept1( node->timeout.time ),
451                        get<Statement>().accept1( node->timeout.stmt ),
452                        get<Expression>().accept1( node->timeout.cond ),
453                };
454                stmt->orelse = {
455                        get<Statement>().accept1( node->orElse.stmt ),
456                        get<Expression>().accept1( node->orElse.cond ),
457                };
458                return stmtPostamble( stmt, node );
459        }
460
461        const ast::Stmt * visit( const ast::WithStmt * node ) override final {
462                auto stmt = new WithStmt(
463                        get<Expression>().acceptL( node->exprs ),
464                        get<Statement>().accept1( node->stmt )
465                );
466                return stmtPostamble( stmt, node );
467        }
468
469        const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
470                auto stmt = new NullStmt();
471                stmtPostamble( stmt, node );
472                return nullptr;
473        }
474
475        const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
476                auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
477                return stmtPostamble( stmt, node );
478        }
479
480        const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
481                (void)node;
482                return nullptr;
483        }
484
485        TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
486
487                TypeSubstitution *rslt = new TypeSubstitution();
488
489                for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
490                        rslt->add( src_i->first,
491                                   get<Type>().accept1(src_i->second) );
492                }
493
494                for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
495                        rslt->addVar( src_i->first,
496                                      get<Expression>().accept1(src_i->second) );
497                }
498
499                return rslt;
500        }
501
502        void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
503                                                   std::vector<UniqueId>         &tgtResnSlots,
504                                                   const ast::Expr::InferUnion   &srcInferred ) {
505
506                assert( tgtInferParams.empty() );
507                assert( tgtResnSlots.empty() );
508
509                if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
510                        const ast::InferredParams &srcParams = srcInferred.inferParamsConst();
511                        for (auto srcParam : srcParams) {
512                                tgtInferParams[srcParam.first] = ParamEntry(
513                                        srcParam.second.decl,
514                                        get<Type>().accept1(srcParam.second.actualType),
515                                        get<Type>().accept1(srcParam.second.formalType),
516                                        get<Expression>().accept1(srcParam.second.expr)
517                                );
518                        }
519                } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots  ) {
520                        const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst();
521                        for (auto srcSlot : srcSlots) {
522                                tgtResnSlots.push_back(srcSlot);
523                        }
524                }
525        }
526
527        Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
528
529                tgt->location = src->location;
530
531                tgt->result = get<Type>().accept1(src->result);
532                tgt->env    = convertTypeSubstitution(src->env);
533
534                tgt->extension = src->extension;
535                convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
536
537                return tgt;
538        }
539
540        const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
541                auto expr = visitBaseExpr( node,
542                        new ApplicationExpr(
543                                get<Expression>().accept1(node->func),
544                                get<Expression>().acceptL(node->args)
545                        )
546                );
547                this->node = expr;
548                return nullptr;
549        }
550
551        const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
552                auto expr = visitBaseExpr( node,
553                        new UntypedExpr(
554                                get<Expression>().accept1(node->func),
555                                get<Expression>().acceptL(node->args)
556                        )
557                );
558                this->node = expr;
559                return nullptr;
560        }
561
562        const ast::Expr * visit( const ast::NameExpr * node ) override final {
563                auto expr = visitBaseExpr( node,
564                        new NameExpr(
565                                node->name
566                        )
567                );
568                this->node = expr;
569                return nullptr;
570        }
571
572        const ast::Expr * visit( const ast::AddressExpr * node ) override final {
573                auto expr = visitBaseExpr( node,
574                        new AddressExpr(
575                                get<Expression>().accept1(node->arg)
576                        )
577                );
578                this->node = expr;
579                return nullptr;
580        }
581
582        const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
583                auto expr = visitBaseExpr( node,
584                        new LabelAddressExpr(
585                                makeLabel(nullptr, node->arg)
586                        )
587                );
588                this->node = expr;
589                return nullptr;
590        }
591
592        const ast::Expr * visit( const ast::CastExpr * node ) override final {
593                auto expr = visitBaseExpr( node,
594                        new CastExpr(
595                                get<Expression>().accept1(node->arg),
596                                (node->isGenerated == ast::GeneratedCast)
597                        )
598                );
599                this->node = expr;
600                return nullptr;
601        }
602
603        const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
604                KeywordCastExpr::Target castTarget = KeywordCastExpr::NUMBER_OF_TARGETS;
605                switch (node->target) {
606                        case ast::KeywordCastExpr::Coroutine:
607                                castTarget = KeywordCastExpr::Coroutine;
608                                break;
609                        case ast::KeywordCastExpr::Thread:
610                                castTarget = KeywordCastExpr::Thread;
611                                break;
612                        case ast::KeywordCastExpr::Monitor:
613                                castTarget = KeywordCastExpr::Monitor;
614                                break;
615                        default:
616                                break;
617                }
618                assert ( castTarget < KeywordCastExpr::NUMBER_OF_TARGETS );
619                auto expr = visitBaseExpr( node,
620                        new KeywordCastExpr(
621                                get<Expression>().accept1(node->arg),
622                                castTarget
623                        )
624                );
625                this->node = expr;
626                return nullptr;
627        }
628
629        const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
630                auto expr = visitBaseExpr( node,
631                        new VirtualCastExpr(
632                                get<Expression>().accept1(node->arg),
633                                nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr
634                        )
635                );
636                this->node = expr;
637                return nullptr;
638        }
639
640        const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
641                auto expr = visitBaseExpr( node,
642                        new UntypedMemberExpr(
643                                get<Expression>().accept1(node->member),
644                                get<Expression>().accept1(node->aggregate)
645                        )
646                );
647                this->node = expr;
648                return nullptr;
649        }
650
651        const ast::Expr * visit( const ast::MemberExpr * node ) override final {
652                auto expr = visitBaseExpr( node,
653                        new MemberExpr(
654                                inCache(node->member) ?
655                                        dynamic_cast<DeclarationWithType *>(this->node) :
656                                        get<DeclarationWithType>().accept1(node->member),
657                                get<Expression>().accept1(node->aggregate)
658                        )
659                );
660                this->node = expr;
661                return nullptr;
662        }
663
664        const ast::Expr * visit( const ast::VariableExpr * node ) override final {
665                auto expr = visitBaseExpr( node,
666                        new VariableExpr(
667                                inCache(node->var) ?
668                                        dynamic_cast<DeclarationWithType *>(this->node) :
669                                        get<DeclarationWithType>().accept1(node->var)
670                        )
671                );
672                this->node = expr;
673                return nullptr;
674        }
675
676        bool isIntlikeConstantType(const ast::Type *t) {
677                if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) {
678                        if ( basicType->isInteger() ) {
679                                return true;
680                        }
681                } else if ( dynamic_cast< const ast::OneType * >( t ) ) {
682                        return true;
683                } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) {
684                        return true;
685                } else if ( dynamic_cast< const ast::PointerType * >( t ) ) {
686                        // null pointer constants, with zero int-values
687                        return true;
688                }
689                return false;
690        }
691
692        bool isFloatlikeConstantType(const ast::Type *t) {
693                if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) {
694                        if ( ! bty->isInteger() ) {
695                                return true;
696                        }
697                }
698                return false;
699        }
700
701        bool isStringlikeConstantType(const ast::Type *t) {
702                if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) {
703                        if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) {
704                           if ( bty->kind == ast::BasicType::Kind::Char ) {
705                                   return true;
706                           }
707                        }
708                }
709                return false;
710        }
711
712        const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
713                ConstantExpr *rslt = nullptr;
714                if (isIntlikeConstantType(node->result)) {
715                        rslt = new ConstantExpr(Constant(
716                                get<Type>().accept1(node->result),
717                                node->rep,
718                                (unsigned long long) node->intValue()
719                        ));
720                } else if (isFloatlikeConstantType(node->result)) {
721                        rslt = new ConstantExpr(Constant(
722                                get<Type>().accept1(node->result),
723                                node->rep,
724                                (double) node->floatValue()
725                        ));
726                } else if (isStringlikeConstantType(node->result)) {
727                        rslt = new ConstantExpr(Constant::from_string(
728                                node->rep
729                        ));
730                }
731                assert(rslt);
732                auto expr = visitBaseExpr( node, rslt );
733                this->node = expr;
734                return nullptr;
735        }
736
737        const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
738                assert (node->expr || node->type);
739                assert (! (node->expr && node->type));
740                SizeofExpr *rslt;
741                if (node->expr) {
742                        rslt = new SizeofExpr(
743                                get<Expression>().accept1(node->expr)
744                        );
745                        assert (!rslt->isType);
746                }
747                if (node->type) {
748                        rslt = new SizeofExpr(
749                                get<Type>().accept1(node->type)
750                        );
751                        assert (rslt->isType);
752                }
753                auto expr = visitBaseExpr( node, rslt );
754                this->node = expr;
755                return nullptr;
756        }
757
758        const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
759                assert (node->expr || node->type);
760                assert (! (node->expr && node->type));
761                AlignofExpr *rslt;
762                if (node->expr) {
763                        rslt = new AlignofExpr(
764                                get<Expression>().accept1(node->expr)
765                        );
766                        assert (!rslt->isType);
767                }
768                if (node->type) {
769                        rslt = new AlignofExpr(
770                                get<Type>().accept1(node->type)
771                        );
772                        assert (rslt->isType);
773                }
774                auto expr = visitBaseExpr( node, rslt );
775                this->node = expr;
776                return nullptr;
777        }
778
779        const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
780                auto expr = visitBaseExpr( node,
781                        new UntypedOffsetofExpr(
782                                get<Type>().accept1(node->type),
783                                node->member
784                        )
785                );
786                this->node = expr;
787                return nullptr;
788        }
789
790        const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
791                auto expr = visitBaseExpr( node,
792                        new OffsetofExpr(
793                                get<Type>().accept1(node->type),
794                                inCache(node->member) ?
795                                        dynamic_cast<DeclarationWithType *>(this->node) :
796                                        get<DeclarationWithType>().accept1(node->member)
797                        )
798                );
799                this->node = expr;
800                return nullptr;
801        }
802
803        const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
804                auto expr = visitBaseExpr( node,
805                        new OffsetPackExpr(
806                                get<StructInstType>().accept1(node->type)
807                        )
808                );
809                this->node = expr;
810                return nullptr;
811        }
812
813        const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
814                assert (node->isAnd == ast::LogicalFlag::AndExpr ||
815                                node->isAnd == ast::LogicalFlag::OrExpr );
816                auto expr = visitBaseExpr( node,
817                        new LogicalExpr(
818                                get<Expression>().accept1(node->arg1),
819                                get<Expression>().accept1(node->arg2),
820                                (node->isAnd == ast::LogicalFlag::AndExpr)
821                        )
822                );
823                this->node = expr;
824                return nullptr;
825        }
826
827        const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
828                auto expr = visitBaseExpr( node,
829                        new ConditionalExpr(
830                                get<Expression>().accept1(node->arg1),
831                                get<Expression>().accept1(node->arg2),
832                                get<Expression>().accept1(node->arg3)
833                        )
834                );
835                this->node = expr;
836                return nullptr;
837        }
838
839        const ast::Expr * visit( const ast::CommaExpr * node ) override final {
840                auto expr = visitBaseExpr( node,
841                        new CommaExpr(
842                                get<Expression>().accept1(node->arg1),
843                                get<Expression>().accept1(node->arg2)
844                        )
845                );
846                this->node = expr;
847                return nullptr;
848        }
849
850        const ast::Expr * visit( const ast::TypeExpr * node ) override final {
851                (void)node;
852                return nullptr;
853        }
854
855        const ast::Expr * visit( const ast::AsmExpr * node ) override final {
856                (void)node;
857                return nullptr;
858        }
859
860        const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
861                (void)node;
862                return nullptr;
863        }
864
865        const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
866                (void)node;
867                return nullptr;
868        }
869
870        const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
871                (void)node;
872                return nullptr;
873        }
874
875        const ast::Expr * visit( const ast::RangeExpr * node ) override final {
876                (void)node;
877                return nullptr;
878        }
879
880        const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
881                (void)node;
882                return nullptr;
883        }
884
885        const ast::Expr * visit( const ast::TupleExpr * node ) override final {
886                (void)node;
887                return nullptr;
888        }
889
890        const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
891                (void)node;
892                return nullptr;
893        }
894
895        const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
896                (void)node;
897                return nullptr;
898        }
899
900        const ast::Expr * visit( const ast::StmtExpr * node ) override final {
901                (void)node;
902                return nullptr;
903        }
904
905        const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
906                (void)node;
907                return nullptr;
908        }
909
910        const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
911                (void)node;
912                return nullptr;
913        }
914
915        const ast::Expr * visit( const ast::InitExpr * node ) override final {
916                (void)node;
917                return nullptr;
918        }
919
920        const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
921                (void)node;
922                return nullptr;
923        }
924
925        const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
926                (void)node;
927                return nullptr;
928        }
929
930        const ast::Expr * visit( const ast::GenericExpr * node ) override final {
931                (void)node;
932                return nullptr;
933        }
934
935        const ast::Type * visit( const ast::VoidType * node ) override final {
936                this->node = new VoidType{ cv( node ) };
937                return nullptr;
938        }
939
940        const ast::Type * visit( const ast::BasicType * node ) override final {
941                this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
942                return nullptr;
943        }
944
945        const ast::Type * visit( const ast::PointerType * node ) override final {
946                this->node = new PointerType{
947                        cv( node ),
948                        get<Type>().accept1( node->base ),
949                        get<Expression>().accept1( node->dimension ),
950                        (bool)node->isVarLen,
951                        (bool)node->isStatic
952                };
953                return nullptr;
954        }
955
956        const ast::Type * visit( const ast::ArrayType * node ) override final {
957                this->node = new ArrayType{
958                        cv( node ),
959                        get<Type>().accept1( node->base ),
960                        get<Expression>().accept1( node->dimension ),
961                        (bool)node->isVarLen,
962                        (bool)node->isStatic
963                };
964                return nullptr;
965        }
966
967        const ast::Type * visit( const ast::ReferenceType * node ) override final {
968                this->node = new ReferenceType{
969                        cv( node ),
970                        get<Type>().accept1( node->base )
971                };
972                return nullptr;
973        }
974
975        const ast::Type * visit( const ast::QualifiedType * node ) override final {
976                this->node = new QualifiedType{
977                        cv( node ),
978                        get<Type>().accept1( node->parent ),
979                        get<Type>().accept1( node->child )
980                };
981                return nullptr;
982        }
983
984        const ast::Type * visit( const ast::FunctionType * node ) override final {
985                auto ty = new FunctionType {
986                        cv( node ), 
987                        (bool)node->isVarArgs
988                };
989                ty->returnVals = get<DeclarationWithType>().acceptL( node->returns );
990                ty->parameters = get<DeclarationWithType>().acceptL( node->params );
991                ty->forall = get<TypeDecl>().acceptL( node->forall );
992                this->node = ty;
993                return nullptr;
994        }
995
996        void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {
997                ty->forall = get<TypeDecl>().acceptL( old->forall );
998                ty->parameters = get<Expression>().acceptL( old->params );
999                ty->hoistType = old->hoistType;
1000        }
1001
1002        const ast::Type * visit( const ast::StructInstType * node ) override final {
1003                StructInstType * ty;
1004                if ( node->base ) {
1005                        ty = new StructInstType{
1006                                cv( node ),
1007                                get<StructDecl>().accept1( node->base ),
1008                                get<Attribute>().acceptL( node->attributes )
1009                        };
1010                } else {
1011                        ty = new StructInstType{
1012                                cv( node ),
1013                                node->name,
1014                                get<Attribute>().acceptL( node->attributes )
1015                        };
1016                }
1017                postvisit( node, ty );
1018                this->node = ty;
1019                return nullptr;
1020        }
1021
1022        const ast::Type * visit( const ast::UnionInstType * node ) override final {
1023                UnionInstType * ty;
1024                if ( node->base ) {
1025                        ty = new UnionInstType{
1026                                cv( node ),
1027                                get<UnionDecl>().accept1( node->base ),
1028                                get<Attribute>().acceptL( node->attributes )
1029                        };
1030                } else {
1031                        ty = new UnionInstType{
1032                                cv( node ),
1033                                node->name,
1034                                get<Attribute>().acceptL( node->attributes )
1035                        };
1036                }
1037                postvisit( node, ty );
1038                this->node = ty;
1039                return nullptr;
1040        }
1041
1042        const ast::Type * visit( const ast::EnumInstType * node ) override final {
1043                EnumInstType * ty;
1044                if ( node->base ) {
1045                        ty = new EnumInstType{
1046                                cv( node ),
1047                                get<EnumDecl>().accept1( node->base ),
1048                                get<Attribute>().acceptL( node->attributes )
1049                        };
1050                } else {
1051                        ty = new EnumInstType{
1052                                cv( node ),
1053                                node->name,
1054                                get<Attribute>().acceptL( node->attributes )
1055                        };
1056                }
1057                postvisit( node, ty );
1058                this->node = ty;
1059                return nullptr;
1060        }
1061
1062        const ast::Type * visit( const ast::TraitInstType * node ) override final {
1063                TraitInstType * ty;
1064                if ( node->base ) {
1065                        ty = new TraitInstType{
1066                                cv( node ),
1067                                get<TraitDecl>().accept1( node->base ),
1068                                get<Attribute>().acceptL( node->attributes )
1069                        };
1070                } else {
1071                        ty = new TraitInstType{
1072                                cv( node ),
1073                                node->name,
1074                                get<Attribute>().acceptL( node->attributes )
1075                        };
1076                }
1077                postvisit( node, ty );
1078                this->node = ty;
1079                return nullptr;
1080        }
1081
1082        const ast::Type * visit( const ast::TypeInstType * node ) override final {
1083                TypeInstType * ty;
1084                if ( node->base ) {
1085                        ty = new TypeInstType{
1086                                cv( node ),
1087                                node->name,
1088                                get<TypeDecl>().accept1( node->base ),
1089                                get<Attribute>().acceptL( node->attributes )
1090                        };
1091                } else {
1092                        ty = new TypeInstType{
1093                                cv( node ),
1094                                node->name,
1095                                node->kind == ast::TypeVar::Ftype,
1096                                get<Attribute>().acceptL( node->attributes )
1097                        };
1098                }
1099                postvisit( node, ty );
1100                this->node = ty;
1101                return nullptr;
1102        }
1103
1104        const ast::Type * visit( const ast::TupleType * node ) override final {
1105                this->node = new TupleType{
1106                        cv( node ),
1107                        get<Type>().acceptL( node->types )
1108                        // members generated by TupleType c'tor
1109                };
1110                return nullptr;
1111        }
1112
1113        const ast::Type * visit( const ast::TypeofType * node ) override final {
1114                this->node = new TypeofType{
1115                        cv( node ),
1116                        get<Expression>().accept1( node->expr ),
1117                        (bool)node->kind
1118                };
1119                return nullptr;
1120        }
1121
1122        const ast::Type * visit( const ast::VarArgsType * node ) override final {
1123                this->node = new VarArgsType{ cv( node ) };
1124                return nullptr;
1125        }
1126
1127        const ast::Type * visit( const ast::ZeroType * node ) override final {
1128                this->node = new ZeroType{ cv( node ) };
1129                return nullptr;
1130        }
1131
1132        const ast::Type * visit( const ast::OneType * node ) override final {
1133                this->node = new OneType{ cv( node ) };
1134                return nullptr;
1135        }
1136
1137        const ast::Type * visit( const ast::GlobalScopeType * ) override final {
1138                this->node = new GlobalScopeType{};
1139                return nullptr;
1140        }
1141
1142        const ast::Designation * visit( const ast::Designation * node ) override final {
1143                (void)node;
1144                return nullptr;
1145        }
1146
1147        const ast::Init * visit( const ast::SingleInit * node ) override final {
1148                (void)node;
1149                return nullptr;
1150        }
1151
1152        const ast::Init * visit( const ast::ListInit * node ) override final {
1153                (void)node;
1154                return nullptr;
1155        }
1156
1157        const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1158                (void)node;
1159                return nullptr;
1160        }
1161
1162        const ast::Attribute * visit( const ast::Attribute * node ) override final {
1163                (void)node;
1164                return nullptr;
1165        }
1166
1167        const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1168                (void)node;
1169                return nullptr;
1170        }
1171};
1172
1173std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > && translationUnit ) {
1174        ConverterNewToOld c;
1175        std::list< Declaration * > decls;
1176        for(auto d : translationUnit) {
1177                decls.emplace_back( c.decl( d ) );
1178                delete d;
1179        }
1180        return decls;
1181}
1182
1183//================================================================================================
1184
1185class ConverterOldToNew : public Visitor {
1186public:
1187        ast::Decl * decl() {
1188                return strict_dynamic_cast< ast::Decl * >( node );
1189        }
1190private:
1191        /// conversion output
1192        ast::Node * node;
1193        /// cache of nodes that might be referenced by readonly<> for de-duplication
1194        std::unordered_map< BaseSyntaxNode *, ast::Node * > cache;
1195
1196        // Local Utilities:
1197
1198        template<typename NewT, typename OldT>
1199        NewT * getAccept1( OldT old ) {
1200                if ( ! old ) return nullptr;
1201                old->accept(*this);
1202                return strict_dynamic_cast< NewT * >( node );
1203        }
1204
1205#       define GET_ACCEPT_1(child, type) \
1206                getAccept1< ast::type, decltype( old->child ) >( old->child )
1207
1208        template<typename NewT, typename OldC>
1209        std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
1210                std::vector< ast::ptr<NewT> > ret;
1211                ret.reserve( old.size() );
1212                for ( auto a : old ) {
1213                        a->accept( *this );
1214                        ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1215                }
1216                return ret;
1217        }
1218
1219#       define GET_ACCEPT_V(child, type) \
1220                getAcceptV< ast::type, decltype( old->child ) >( old->child )
1221
1222        ast::Label make_label(Label* old) {
1223                return ast::Label(
1224                        old->labelled->location,
1225                        old->name,
1226                        GET_ACCEPT_V(attributes, Attribute)
1227                );
1228        }
1229
1230        template<template <class...> class C>
1231        C<ast::Label> make_labels(C<Label> olds) {
1232                C<ast::Label> ret;
1233                for (auto oldn : olds) {
1234                        ret.push_back( make_label( &oldn ) );
1235                }
1236                return ret;
1237        }
1238
1239#       define GET_LABELS_V(labels) \
1240                to<std::vector>::from( make_labels( std::move( labels ) ) )
1241       
1242        static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; }
1243
1244        /// returns true and sets `node` if in cache
1245        bool inCache( BaseSyntaxNode * old ) {
1246                auto it = cache.find( old );
1247                if ( it == cache.end() ) return false;
1248                node = it->second;
1249                return true;
1250        }
1251
1252        // Now all the visit functions:
1253
1254        virtual void visit( ObjectDecl * old ) override final {
1255                if ( inCache( old ) ) return;
1256                auto decl = new ast::ObjectDecl(
1257                        old->location,
1258                        old->name,
1259                        GET_ACCEPT_1(type, Type),
1260                        GET_ACCEPT_1(init, Init),
1261                        { old->get_storageClasses().val },
1262                        { old->linkage.val },
1263                        GET_ACCEPT_1(bitfieldWidth, Expr),
1264                        GET_ACCEPT_V(attributes, Attribute),
1265                        { old->get_funcSpec().val }
1266                );
1267                decl->scopeLevel = old->scopeLevel;
1268                decl->mangleName = old->mangleName;
1269                decl->isDeleted  = old->isDeleted;
1270                decl->uniqueId   = old->uniqueId;
1271                decl->extension  = old->extension;
1272                cache.emplace( old, decl );
1273
1274                this->node = decl;
1275        }
1276
1277        virtual void visit( FunctionDecl * old ) override final {
1278                if ( inCache( old ) ) return;
1279                // TODO
1280                auto decl = (ast::FunctionDecl *)nullptr;
1281                cache.emplace( old, decl );
1282        }
1283
1284        virtual void visit( StructDecl * old ) override final {
1285                if ( inCache( old ) ) return;
1286                auto decl = new ast::StructDecl(
1287                        old->location,
1288                        old->name,
1289                        old->kind,
1290                        GET_ACCEPT_V(attributes, Attribute),
1291                        { old->linkage.val }
1292                );
1293                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1294                decl->body   = old->body;
1295                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1296                decl->members    = GET_ACCEPT_V(members, Decl);
1297                decl->extension  = old->extension;
1298                decl->uniqueId   = old->uniqueId;
1299                decl->storage    = { old->storageClasses.val };
1300                cache.emplace( old, decl );
1301
1302                this->node = decl;
1303        }
1304
1305        virtual void visit( UnionDecl * old ) override final {
1306                if ( inCache( old ) ) return;
1307                auto decl = new ast::UnionDecl(
1308                        old->location,
1309                        old->name,
1310                        GET_ACCEPT_V(attributes, Attribute),
1311                        { old->linkage.val }
1312                );
1313                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1314                decl->body   = old->body;
1315                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1316                decl->members    = GET_ACCEPT_V(members, Decl);
1317                decl->extension  = old->extension;
1318                decl->uniqueId   = old->uniqueId;
1319                decl->storage    = { old->storageClasses.val };
1320                cache.emplace( old, decl );
1321
1322                this->node = decl;
1323        }
1324
1325        virtual void visit( EnumDecl * old ) override final {
1326                if ( inCache( old ) ) return;
1327                auto decl = new ast::UnionDecl(
1328                        old->location,
1329                        old->name,
1330                        GET_ACCEPT_V(attributes, Attribute),
1331                        { old->linkage.val }
1332                );
1333                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1334                decl->body   = old->body;
1335                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1336                decl->members    = GET_ACCEPT_V(members, Decl);
1337                decl->extension  = old->extension;
1338                decl->uniqueId   = old->uniqueId;
1339                decl->storage    = { old->storageClasses.val };
1340                cache.emplace( old, decl );
1341
1342                this->node = decl;
1343        }
1344
1345        virtual void visit( TraitDecl * old ) override final {
1346                if ( inCache( old ) ) return;
1347                auto decl = new ast::UnionDecl(
1348                        old->location,
1349                        old->name,
1350                        GET_ACCEPT_V(attributes, Attribute),
1351                        { old->linkage.val }
1352                );
1353                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1354                decl->body   = old->body;
1355                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1356                decl->members    = GET_ACCEPT_V(members, Decl);
1357                decl->extension  = old->extension;
1358                decl->uniqueId   = old->uniqueId;
1359                decl->storage    = { old->storageClasses.val };
1360                cache.emplace( old, decl );
1361
1362                this->node = decl;
1363        }
1364
1365        virtual void visit( TypeDecl * old ) override final {
1366                if ( inCache( old ) ) return;
1367                // TODO
1368                auto decl = (ast::TypeDecl *)nullptr;
1369                cache.emplace( old, decl );
1370        }
1371
1372        virtual void visit( TypedefDecl * old ) override final {
1373                auto decl = new ast::TypedefDecl(
1374                        old->location,
1375                        old->name,
1376                        { old->storageClasses.val },
1377                        GET_ACCEPT_1(base, Type),
1378                        { old->linkage.val }
1379                );
1380                decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1381                decl->params     = GET_ACCEPT_V(parameters, TypeDecl);
1382                decl->extension  = old->extension;
1383                decl->uniqueId   = old->uniqueId;
1384                decl->storage    = { old->storageClasses.val };
1385
1386                this->node = decl;
1387        }
1388
1389        virtual void visit( AsmDecl * ) override final {
1390
1391        }
1392
1393        virtual void visit( StaticAssertDecl * ) override final {
1394
1395        }
1396
1397        virtual void visit( CompoundStmt * old ) override final {
1398                auto stmt = new ast::CompoundStmt(
1399                        old->location,
1400                        to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1401                        GET_LABELS_V(old->labels)
1402                );
1403
1404                this->node = stmt;
1405        }
1406
1407        virtual void visit( ExprStmt * old ) override final {
1408                this->node = new ast::ExprStmt(
1409                        old->location,
1410                        GET_ACCEPT_1(expr, Expr),
1411                        GET_LABELS_V(old->labels)
1412                );
1413        }
1414
1415        virtual void visit( AsmStmt * old ) override final {
1416                this->node = new ast::AsmStmt(
1417                        old->location,
1418                        old->voltile,
1419                        GET_ACCEPT_1(instruction, Expr),
1420                        GET_ACCEPT_V(output, Expr),
1421                        GET_ACCEPT_V(input, Expr),
1422                        GET_ACCEPT_V(clobber, ConstantExpr),
1423                        GET_LABELS_V(old->gotolabels),
1424                        GET_LABELS_V(old->labels)
1425                );
1426        }
1427
1428        virtual void visit( DirectiveStmt * old ) override final {
1429                this->node = new ast::DirectiveStmt(
1430                        old->location,
1431                        old->directive,
1432                        GET_LABELS_V(old->labels)
1433                );
1434        }
1435
1436        virtual void visit( IfStmt * old ) override final {
1437                this->node = new ast::IfStmt(
1438                        old->location,
1439                        GET_ACCEPT_1(condition, Expr),
1440                        GET_ACCEPT_1(thenPart, Stmt),
1441                        GET_ACCEPT_1(elsePart, Stmt),
1442                        GET_ACCEPT_V(initialization, Stmt),
1443                        GET_LABELS_V(old->labels)
1444                );
1445        }
1446
1447        virtual void visit( SwitchStmt * old ) override final {
1448                this->node = new ast::SwitchStmt(
1449                        old->location,
1450                        GET_ACCEPT_1(condition, Expr),
1451                        GET_ACCEPT_V(statements, Stmt),
1452                        GET_LABELS_V(old->labels)
1453                );
1454        }
1455
1456        virtual void visit( CaseStmt * old ) override final {
1457                this->node = new ast::CaseStmt(
1458                        old->location,
1459                        GET_ACCEPT_1(condition, Expr),
1460                        GET_ACCEPT_V(stmts, Stmt),
1461                        GET_LABELS_V(old->labels)
1462                );
1463        }
1464
1465        virtual void visit( WhileStmt * old ) override final {
1466                this->node = new ast::WhileStmt(
1467                        old->location,
1468                        GET_ACCEPT_1(condition, Expr),
1469                        GET_ACCEPT_1(body, Stmt),
1470                        GET_ACCEPT_V(initialization, Stmt),
1471                        old->isDoWhile,
1472                        GET_LABELS_V(old->labels)
1473                );
1474        }
1475
1476        virtual void visit( ForStmt * old ) override final {
1477                this->node = new ast::ForStmt(
1478                        old->location,
1479                        GET_ACCEPT_V(initialization, Stmt),
1480                        GET_ACCEPT_1(condition, Expr),
1481                        GET_ACCEPT_1(increment, Expr),
1482                        GET_ACCEPT_1(body, Stmt),
1483                        GET_LABELS_V(old->labels)
1484                );
1485        }
1486
1487        virtual void visit( BranchStmt * old ) override final {
1488                if (old->computedTarget) {
1489                        this->node = new ast::BranchStmt(
1490                                old->location,
1491                                GET_ACCEPT_1(computedTarget, Expr),
1492                                GET_LABELS_V(old->labels)
1493                        );
1494                } else {
1495                        ast::BranchStmt::Kind kind;
1496                        switch (old->type) {
1497                        #define CASE(n) \
1498                        case BranchStmt::n: \
1499                                kind = ast::BranchStmt::n; \
1500                                break
1501                        CASE(Goto);
1502                        CASE(Break);
1503                        CASE(Continue);
1504                        CASE(FallThrough);
1505                        CASE(FallThroughDefault);
1506                        #undef CASE
1507                        default:
1508                                assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1509                        }
1510
1511                        Label label = old->originalTarget;
1512                        auto stmt = new ast::BranchStmt(
1513                                old->location,
1514                                kind,
1515                                make_label(&label),
1516                                GET_LABELS_V(old->labels)
1517                        );
1518                        stmt->target = make_label(&old->target);
1519                        this->node = stmt;
1520                }
1521        }
1522
1523        virtual void visit( ReturnStmt * old ) override final {
1524                this->node = new ast::ReturnStmt(
1525                        old->location,
1526                        GET_ACCEPT_1(expr, Expr),
1527                        GET_LABELS_V(old->labels)
1528                );
1529        }
1530
1531        virtual void visit( ThrowStmt * old ) override final {
1532                ast::ThrowStmt::Kind kind;
1533                switch (old->kind) {
1534                case ThrowStmt::Terminate:
1535                        kind = ast::ThrowStmt::Terminate;
1536                        break;
1537                case ThrowStmt::Resume:
1538                        kind = ast::ThrowStmt::Resume;
1539                        break;
1540                default:
1541                        assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1542                }
1543
1544                this->node = new ast::ThrowStmt(
1545                        old->location,
1546                        kind,
1547                        GET_ACCEPT_1(expr, Expr),
1548                        GET_ACCEPT_1(target, Expr),
1549                        GET_LABELS_V(old->labels)
1550                );
1551        }
1552
1553        virtual void visit( TryStmt * old ) override final {
1554                this->node = new ast::TryStmt(
1555                        old->location,
1556                        GET_ACCEPT_1(block, CompoundStmt),
1557                        GET_ACCEPT_V(handlers, CatchStmt),
1558                        GET_ACCEPT_1(finallyBlock, FinallyStmt),
1559                        GET_LABELS_V(old->labels)
1560                );
1561        }
1562
1563        virtual void visit( CatchStmt * old ) override final {
1564                ast::CatchStmt::Kind kind;
1565                switch (old->kind) {
1566                case CatchStmt::Terminate:
1567                        kind = ast::CatchStmt::Terminate;
1568                        break;
1569                case CatchStmt::Resume:
1570                        kind = ast::CatchStmt::Resume;
1571                        break;
1572                default:
1573                        assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
1574                }
1575
1576                this->node = new ast::CatchStmt(
1577                        old->location,
1578                        kind,
1579                        GET_ACCEPT_1(decl, Decl),
1580                        GET_ACCEPT_1(cond, Expr),
1581                        GET_ACCEPT_1(body, Stmt),
1582                        GET_LABELS_V(old->labels)
1583                );
1584        }
1585
1586        virtual void visit( FinallyStmt * old ) override final {
1587                this->node = new ast::FinallyStmt(
1588                        old->location,
1589                        GET_ACCEPT_1(block, CompoundStmt),
1590                        GET_LABELS_V(old->labels)
1591                );
1592        }
1593
1594        virtual void visit( WaitForStmt * old ) override final {
1595                ast::WaitForStmt * stmt = new ast::WaitForStmt(
1596                        old->location,
1597                        GET_LABELS_V(old->labels)
1598                );
1599
1600                stmt->clauses.reserve( old->clauses.size() );
1601                for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
1602                        stmt->clauses.push_back({
1603                                ast::WaitForStmt::Target{
1604                                        GET_ACCEPT_1(clauses[i].target.function, Expr),
1605                                        GET_ACCEPT_V(clauses[i].target.arguments, Expr)
1606                                },
1607                                GET_ACCEPT_1(clauses[i].statement, Stmt),
1608                                GET_ACCEPT_1(clauses[i].condition, Expr)
1609                        });
1610                }
1611                stmt->timeout = {
1612                        GET_ACCEPT_1(timeout.time, Expr),
1613                        GET_ACCEPT_1(timeout.statement, Stmt),
1614                        GET_ACCEPT_1(timeout.condition, Expr),
1615                };
1616                stmt->orElse = {
1617                        GET_ACCEPT_1(timeout.statement, Stmt),
1618                        GET_ACCEPT_1(timeout.condition, Expr),
1619                };
1620
1621                this->node = stmt;
1622        }
1623
1624        virtual void visit( WithStmt * old ) override final {
1625                this->node = new ast::WithStmt(
1626                        old->location,
1627                        GET_ACCEPT_V(exprs, Expr),
1628                        GET_ACCEPT_1(stmt, Stmt),
1629                        GET_LABELS_V(old->labels)
1630                );
1631        }
1632
1633        virtual void visit( NullStmt * old ) override final {
1634                this->node = new ast::NullStmt(
1635                        old->location,
1636                        GET_LABELS_V(old->labels)
1637                );
1638        }
1639
1640        virtual void visit( DeclStmt * old ) override final {
1641                this->node = new ast::DeclStmt(
1642                        old->location,
1643                        GET_ACCEPT_1(decl, Decl),
1644                        GET_LABELS_V(old->labels)
1645                );
1646        }
1647
1648        virtual void visit( ImplicitCtorDtorStmt * old ) override final {
1649                this->node = new ast::ImplicitCtorDtorStmt(
1650                        old->location,
1651                        GET_ACCEPT_1(callStmt, Stmt),
1652                        GET_LABELS_V(old->labels)
1653                );
1654        }
1655
1656        ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
1657
1658                ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
1659
1660                for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
1661                        rslt->add( old_i->first,
1662                                   getAccept1<ast::Type>(old_i->second) );
1663                }
1664
1665                for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
1666                        rslt->addVar( old_i->first,
1667                                      getAccept1<ast::Expr>(old_i->second) );
1668                }
1669
1670                return rslt;
1671        }
1672
1673        void convertInferUnion(ast::Expr::InferUnion               &newInferred,
1674                                                   const std::map<UniqueId,ParamEntry> &oldInferParams,
1675                                                   const std::vector<UniqueId>         &oldResnSlots) {
1676
1677                assert( oldInferParams.empty() || oldResnSlots.empty() );
1678                assert( newInferred.mode == ast::Expr::InferUnion::Empty );
1679
1680                if ( !oldInferParams.empty() ) {
1681                        ast::InferredParams &tgt = newInferred.inferParams();
1682                        for (auto old : oldInferParams) {
1683                                tgt[old.first] = ast::ParamEntry(
1684                                        old.second.decl,
1685                                        getAccept1<ast::Type>(old.second.actualType),
1686                                        getAccept1<ast::Type>(old.second.formalType),
1687                                        getAccept1<ast::Expr>(old.second.expr)
1688                                );
1689                        }
1690                } else if ( !oldResnSlots.empty() ) {
1691                        ast::ResnSlots &tgt = newInferred.resnSlots();
1692                        for (auto old : oldResnSlots) {
1693                                tgt.push_back(old);
1694                        }
1695                }
1696        }
1697
1698        ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
1699
1700                nw->result = GET_ACCEPT_1(result, Type);
1701                nw->env    = convertTypeSubstitution(old->env);
1702
1703                nw->extension = old->extension;
1704                convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
1705
1706                return nw;
1707        }
1708
1709        virtual void visit( ApplicationExpr * old ) override final {
1710                this->node = visitBaseExpr( old,
1711                        new ast::ApplicationExpr(
1712                                old->location,
1713                                GET_ACCEPT_1(function, Expr),
1714                                GET_ACCEPT_V(args, Expr)
1715                        )
1716                );
1717        }
1718
1719        virtual void visit( UntypedExpr * old ) override final {
1720                this->node = visitBaseExpr( old,
1721                        new ast::UntypedExpr(
1722                                old->location,
1723                                GET_ACCEPT_1(function, Expr),
1724                                GET_ACCEPT_V(args, Expr)
1725                        )
1726                );
1727        }
1728
1729        virtual void visit( NameExpr * old ) override final {
1730                this->node = visitBaseExpr( old,
1731                        new ast::NameExpr(
1732                                old->location,
1733                                old->get_name()
1734                        )
1735                );
1736        }
1737
1738        virtual void visit( CastExpr * old ) override final {
1739                this->node = visitBaseExpr( old,
1740                        new ast::CastExpr(
1741                                old->location,
1742                                GET_ACCEPT_1(arg, Expr),
1743                                old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
1744                        )
1745                );
1746        }
1747
1748        virtual void visit( KeywordCastExpr * old) override final {
1749                ast::KeywordCastExpr::Target castTarget = ast::KeywordCastExpr::NUMBER_OF_TARGETS;
1750                switch (old->target) {
1751                        case KeywordCastExpr::Coroutine:
1752                                castTarget = ast::KeywordCastExpr::Coroutine;
1753                                break;
1754                        case KeywordCastExpr::Thread:
1755                                castTarget = ast::KeywordCastExpr::Thread;
1756                                break;
1757                        case KeywordCastExpr::Monitor:
1758                                castTarget = ast::KeywordCastExpr::Monitor;
1759                                break;
1760                        default:
1761                                break;
1762                }
1763                assert ( castTarget < ast::KeywordCastExpr::NUMBER_OF_TARGETS );
1764                this->node = visitBaseExpr( old,
1765                        new ast::KeywordCastExpr(
1766                                old->location,
1767                                GET_ACCEPT_1(arg, Expr),
1768                                castTarget
1769                        )
1770                );
1771        }
1772
1773        virtual void visit( VirtualCastExpr * old ) override final {
1774                this->node = visitBaseExpr( old,
1775                        new ast::VirtualCastExpr(
1776                                old->location,
1777                                GET_ACCEPT_1(arg, Expr),
1778                                nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr
1779                        )
1780                );
1781        }
1782
1783        virtual void visit( AddressExpr * old ) override final {
1784                this->node = visitBaseExpr( old,
1785                        new ast::AddressExpr(
1786                                old->location,
1787                                GET_ACCEPT_1(arg, Expr)
1788                        )
1789                );
1790        }
1791
1792        virtual void visit( LabelAddressExpr * old ) override final {
1793                this->node = visitBaseExpr( old,
1794                        new ast::LabelAddressExpr(
1795                                old->location,
1796                                make_label(&old->arg)
1797                        )
1798                );
1799        }
1800
1801        virtual void visit( UntypedMemberExpr * old ) override final {
1802                this->node = visitBaseExpr( old,
1803                        new ast::UntypedMemberExpr(
1804                                old->location,
1805                                GET_ACCEPT_1(member, Expr),
1806                                GET_ACCEPT_1(aggregate, Expr)
1807                        )
1808                );
1809        }
1810
1811        virtual void visit( MemberExpr * old ) override final {
1812                this->node = visitBaseExpr( old,
1813                        new ast::MemberExpr(
1814                                old->location,
1815                                inCache(old->member) ?
1816                                        dynamic_cast<ast::DeclWithType *>(this->node) :
1817                                        GET_ACCEPT_1(member, DeclWithType),
1818                                GET_ACCEPT_1(aggregate, Expr)
1819                        )
1820                );
1821        }
1822
1823        virtual void visit( VariableExpr * old ) override final {
1824                this->node = visitBaseExpr( old,
1825                        new ast::VariableExpr(
1826                                old->location,
1827                                inCache(old->var) ?
1828                                        dynamic_cast<ast::DeclWithType *>(this->node) :
1829                                        GET_ACCEPT_1(var, DeclWithType)
1830                        )
1831                );
1832        }
1833
1834        bool isIntlikeConstantType(const Type *t) {
1835                if ( const BasicType * basicType = dynamic_cast< const BasicType * >( t ) ) {
1836                        if ( basicType->isInteger() ) {
1837                                return true;
1838                        }
1839                } else if ( dynamic_cast< const OneType * >( t ) ) {
1840                        return true;
1841                } else if ( dynamic_cast< const ZeroType * >( t ) ) {
1842                        return true;
1843                } else if ( dynamic_cast< const PointerType * >( t ) ) {
1844                        // null pointer constants, with zero int-values
1845                        return true;
1846                }
1847                return false;
1848        }
1849
1850        int isFloatlikeConstantType(const Type *t) {
1851                if ( const BasicType * bty = dynamic_cast< const BasicType * >( t ) ) {
1852                        if ( ! bty->isInteger() ) {
1853                                return true;
1854                        }
1855                }
1856                return false;
1857        }
1858
1859        int isStringlikeConstantType(const Type *t) {
1860                if ( const ArrayType * aty = dynamic_cast< const ArrayType * >( t ) ) {
1861                        if ( const BasicType * bty = dynamic_cast< const BasicType * >( aty->base ) ) {
1862                           if ( bty->kind == BasicType::Kind::Char ) {
1863                                   return true;
1864                           }
1865                        }
1866                }
1867                return false;
1868        }
1869
1870        virtual void visit( ConstantExpr * old ) override final {
1871                ast::ConstantExpr *rslt = nullptr;
1872                if (isIntlikeConstantType(old->result)) {
1873                        rslt = new ast::ConstantExpr(
1874                                old->location,
1875                                GET_ACCEPT_1(result, Type),
1876                                old->constant.get_value(),
1877                                (unsigned long long) old->intValue()
1878                        );
1879                } else if (isFloatlikeConstantType(old->result)) {
1880                        rslt = new ast::ConstantExpr(
1881                                old->location,
1882                                GET_ACCEPT_1(result, Type), 
1883                                old->constant.get_value(), 
1884                                (double) old->constant.get_dval()
1885                        );
1886                } else if (isStringlikeConstantType(old->result)) {
1887                        rslt = ast::ConstantExpr::from_string(
1888                                old->location,
1889                                old->constant.get_value()
1890                        );
1891                }
1892                assert(rslt);
1893                this->node = visitBaseExpr( old, rslt );
1894        }
1895
1896        virtual void visit( SizeofExpr * old ) override final {
1897                assert (old->expr || old->type);
1898                assert (! (old->expr && old->type));
1899                ast::SizeofExpr *rslt;
1900                if (old->expr) {
1901                        assert(!old->isType);
1902                        rslt = new ast::SizeofExpr(
1903                                old->location, 
1904                                GET_ACCEPT_1(expr, Expr)
1905                        );
1906                }
1907                if (old->type) {
1908                        assert(old->isType);
1909                        rslt = new ast::SizeofExpr(
1910                                old->location, 
1911                                GET_ACCEPT_1(type, Type)
1912                        );
1913                }
1914                this->node = visitBaseExpr( old, rslt );
1915        }
1916
1917        virtual void visit( AlignofExpr * old ) override final {
1918                assert (old->expr || old->type);
1919                assert (! (old->expr && old->type));
1920                ast::AlignofExpr *rslt;
1921                if (old->expr) {
1922                        assert(!old->isType);
1923                        rslt = new ast::AlignofExpr(
1924                                old->location, 
1925                                GET_ACCEPT_1(expr, Expr)
1926                        );
1927                }
1928                if (old->type) {
1929                        assert(old->isType);
1930                        rslt = new ast::AlignofExpr(
1931                                old->location, 
1932                                GET_ACCEPT_1(type, Type)
1933                        );
1934                }
1935                this->node = visitBaseExpr( old, rslt );
1936        }
1937
1938        virtual void visit( UntypedOffsetofExpr * old ) override final {
1939                this->node = visitBaseExpr( old,
1940                        new ast::UntypedOffsetofExpr(
1941                                old->location,
1942                                GET_ACCEPT_1(type, Type),
1943                                old->member
1944                        )
1945                );
1946        }
1947
1948        virtual void visit( OffsetofExpr * old ) override final {
1949                this->node = visitBaseExpr( old,
1950                        new ast::OffsetofExpr(
1951                                old->location,
1952                                GET_ACCEPT_1(type, Type),
1953                                inCache(old->member) ?
1954                                        dynamic_cast<ast::DeclWithType *>(this->node) :
1955                                        GET_ACCEPT_1(member, DeclWithType)
1956                        )
1957                );
1958        }
1959
1960        virtual void visit( OffsetPackExpr * old ) override final {
1961                this->node = visitBaseExpr( old,
1962                        new ast::OffsetPackExpr(
1963                                old->location,
1964                                GET_ACCEPT_1(type, StructInstType)
1965                        )
1966                );
1967        }
1968
1969        virtual void visit( LogicalExpr * old ) override final {
1970                this->node = visitBaseExpr( old,
1971                        new ast::LogicalExpr(
1972                                old->location,
1973                                GET_ACCEPT_1(arg1, Expr),
1974                                GET_ACCEPT_1(arg2, Expr),
1975                                old->get_isAnd() ? 
1976                                        ast::LogicalFlag::AndExpr :
1977                                        ast::LogicalFlag::OrExpr
1978                        )
1979                );
1980        }
1981
1982        virtual void visit( ConditionalExpr * old ) override final {
1983                this->node = visitBaseExpr( old,
1984                        new ast::ConditionalExpr(
1985                                old->location,
1986                                GET_ACCEPT_1(arg1, Expr),
1987                                GET_ACCEPT_1(arg2, Expr),
1988                                GET_ACCEPT_1(arg3, Expr)
1989                        )
1990                );
1991        }
1992
1993        virtual void visit( CommaExpr * old ) override final {
1994                this->node = visitBaseExpr( old,
1995                        new ast::CommaExpr(
1996                                old->location,
1997                                GET_ACCEPT_1(arg1, Expr),
1998                                GET_ACCEPT_1(arg2, Expr)
1999                        )
2000                );
2001        }
2002
2003        virtual void visit( TypeExpr * ) override final {
2004
2005        }
2006
2007        virtual void visit( AsmExpr * ) override final {
2008
2009        }
2010
2011        virtual void visit( ImplicitCopyCtorExpr * ) override final {
2012
2013        }
2014
2015        virtual void visit( ConstructorExpr *  ) override final {
2016
2017        }
2018
2019        virtual void visit( CompoundLiteralExpr * ) override final {
2020
2021        }
2022
2023        virtual void visit( RangeExpr * ) override final {
2024
2025        }
2026
2027        virtual void visit( UntypedTupleExpr * ) override final {
2028
2029        }
2030
2031        virtual void visit( TupleExpr * ) override final {
2032
2033        }
2034
2035        virtual void visit( TupleIndexExpr * ) override final {
2036
2037        }
2038
2039        virtual void visit( TupleAssignExpr * ) override final {
2040
2041        }
2042
2043        virtual void visit( StmtExpr *  ) override final {
2044
2045        }
2046
2047        virtual void visit( UniqueExpr *  ) override final {
2048
2049        }
2050
2051        virtual void visit( UntypedInitExpr *  ) override final {
2052
2053        }
2054
2055        virtual void visit( InitExpr *  ) override final {
2056
2057        }
2058
2059        virtual void visit( DeletedExpr * ) override final {
2060
2061        }
2062
2063        virtual void visit( DefaultArgExpr * ) override final {
2064
2065        }
2066
2067        virtual void visit( GenericExpr * ) override final {
2068
2069        }
2070
2071        virtual void visit( VoidType * old ) override final {
2072                this->node = new ast::VoidType{ cv( old ) };
2073        }
2074
2075        virtual void visit( BasicType * old ) override final {
2076                this->node = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2077        }
2078
2079        virtual void visit( PointerType * old ) override final {
2080                this->node = new ast::PointerType{
2081                        GET_ACCEPT_1( base, Type ),
2082                        GET_ACCEPT_1( dimension, Expr ),
2083                        (ast::LengthFlag)old->isVarLen,
2084                        (ast::DimensionFlag)old->isStatic,
2085                        cv( old )
2086                };
2087        }
2088
2089        virtual void visit( ArrayType * old ) override final {
2090                this->node = new ast::ArrayType{
2091                        GET_ACCEPT_1( base, Type ),
2092                        GET_ACCEPT_1( dimension, Expr ),
2093                        (ast::LengthFlag)old->isVarLen,
2094                        (ast::DimensionFlag)old->isStatic,
2095                        cv( old )
2096                };
2097        }
2098
2099        virtual void visit( ReferenceType * old ) override final {
2100                this->node = new ast::ReferenceType{
2101                        GET_ACCEPT_1( base, Type ),
2102                        cv( old )
2103                };
2104        }
2105
2106        virtual void visit( QualifiedType * old ) override final {
2107                this->node = new ast::QualifiedType{
2108                        GET_ACCEPT_1( parent, Type ),
2109                        GET_ACCEPT_1( child, Type ),
2110                        cv( old )
2111                };
2112        }
2113
2114        virtual void visit( FunctionType * old ) override final {
2115                auto ty = new ast::FunctionType {
2116                        (ast::ArgumentFlag)old->isVarArgs,
2117                        cv( old )
2118                };
2119                ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2120                ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2121                ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2122                this->node = ty;
2123        }
2124
2125        void postvisit( ReferenceToType * old, ast::ReferenceToType * ty ) {
2126                ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2127                ty->params = GET_ACCEPT_V( parameters, Expr );
2128                ty->hoistType = old->hoistType;
2129        }
2130
2131        virtual void visit( StructInstType * old ) override final {
2132                ast::StructInstType * ty;
2133                if ( old->baseStruct ) {
2134                        ty = new ast::StructInstType{
2135                                GET_ACCEPT_1( baseStruct, StructDecl ),
2136                                cv( old ),
2137                                GET_ACCEPT_V( attributes, Attribute )
2138                        };
2139                } else {
2140                        ty = new ast::StructInstType{
2141                                old->name,
2142                                cv( old ),
2143                                GET_ACCEPT_V( attributes, Attribute )
2144                        };
2145                }
2146                postvisit( old, ty );
2147                this->node = ty;
2148        }
2149
2150        virtual void visit( UnionInstType * old ) override final {
2151                ast::UnionInstType * ty;
2152                if ( old->baseUnion ) {
2153                        ty = new ast::UnionInstType{
2154                                GET_ACCEPT_1( baseUnion, UnionDecl ),
2155                                cv( old ),
2156                                GET_ACCEPT_V( attributes, Attribute )
2157                        };
2158                } else {
2159                        ty = new ast::UnionInstType{
2160                                old->name,
2161                                cv( old ),
2162                                GET_ACCEPT_V( attributes, Attribute )
2163                        };
2164                }
2165                postvisit( old, ty );
2166                this->node = ty;
2167        }
2168
2169        virtual void visit( EnumInstType * old ) override final {
2170                ast::EnumInstType * ty;
2171                if ( old->baseEnum ) {
2172                        ty = new ast::EnumInstType{
2173                                GET_ACCEPT_1( baseEnum, EnumDecl ),
2174                                cv( old ),
2175                                GET_ACCEPT_V( attributes, Attribute )
2176                        };
2177                } else {
2178                        ty = new ast::EnumInstType{
2179                                old->name,
2180                                cv( old ),
2181                                GET_ACCEPT_V( attributes, Attribute )
2182                        };
2183                }
2184                postvisit( old, ty );
2185                this->node = ty;
2186        }
2187
2188        virtual void visit( TraitInstType * old ) override final {
2189                ast::TraitInstType * ty;
2190                if ( old->baseTrait ) {
2191                        ty = new ast::TraitInstType{
2192                                GET_ACCEPT_1( baseTrait, TraitDecl ),
2193                                cv( old ),
2194                                GET_ACCEPT_V( attributes, Attribute )
2195                        };
2196                } else {
2197                        ty = new ast::TraitInstType{
2198                                old->name,
2199                                cv( old ),
2200                                GET_ACCEPT_V( attributes, Attribute )
2201                        };
2202                }
2203                postvisit( old, ty );
2204                this->node = ty;
2205        }
2206
2207        virtual void visit( TypeInstType * old ) override final {
2208                ast::TypeInstType * ty;
2209                if ( old->baseType ) {
2210                        ty = new ast::TypeInstType{
2211                                old->name,
2212                                GET_ACCEPT_1( baseType, TypeDecl ),
2213                                cv( old ),
2214                                GET_ACCEPT_V( attributes, Attribute )
2215                        };
2216                } else {
2217                        ty = new ast::TypeInstType{
2218                                old->name,
2219                                old->isFtype ? ast::TypeVar::Ftype : ast::TypeVar::Dtype,
2220                                cv( old ),
2221                                GET_ACCEPT_V( attributes, Attribute )
2222                        };
2223                }
2224                postvisit( old, ty );
2225                this->node = ty;
2226        }
2227
2228        virtual void visit( TupleType * old ) override final {
2229                this->node = new ast::TupleType{
2230                        GET_ACCEPT_V( types, Type ),
2231                        // members generated by TupleType c'tor
2232                        cv( old )
2233                };
2234        }
2235
2236        virtual void visit( TypeofType * old ) override final {
2237                this->node = new ast::TypeofType{
2238                        GET_ACCEPT_1( expr, Expr ),
2239                        (ast::TypeofType::Kind)old->is_basetypeof,
2240                        cv( old )
2241                };
2242        }
2243
2244        virtual void visit( AttrType * ) override final {
2245                assertf( false, "AttrType deprecated in new AST." );
2246        }
2247
2248        virtual void visit( VarArgsType * old ) override final {
2249                this->node = new ast::VarArgsType{ cv( old ) };
2250        }
2251
2252        virtual void visit( ZeroType * old ) override final {
2253                this->node = new ast::ZeroType{ cv( old ) };
2254        }
2255
2256        virtual void visit( OneType * old ) override final {
2257                this->node = new ast::OneType{ cv( old ) };
2258        }
2259
2260        virtual void visit( GlobalScopeType * ) override final {
2261                this->node = new ast::GlobalScopeType{};
2262        }
2263
2264        virtual void visit( Designation * ) override final {
2265
2266        }
2267
2268        virtual void visit( SingleInit * ) override final {
2269
2270        }
2271
2272        virtual void visit( ListInit * ) override final {
2273
2274        }
2275
2276        virtual void visit( ConstructorInit * ) override final {
2277
2278        }
2279
2280        virtual void visit( Constant * ) override final {
2281
2282        }
2283
2284        virtual void visit( Attribute * ) override final {
2285
2286        }
2287
2288        virtual void visit( AttrExpr * ) override final {
2289
2290                assert( 0 );
2291        }
2292};
2293
2294#undef GET_LABELS_V
2295#undef GET_ACCEPT_V
2296#undef GET_ACCEPT_1
2297
2298std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
2299        ConverterOldToNew c;
2300        std::list< ast::ptr< ast::Decl > > decls;
2301        for(auto d : translationUnit) {
2302                d->accept( c );
2303                decls.emplace_back( c.decl() );
2304                delete d;
2305        }
2306        return decls;
2307}
Note: See TracBrowser for help on using the repository browser.