source: src/AST/Convert.cpp @ 6f4b7f2

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

Finish Stmt printers, implement short_mode for Decls

  • Property mode set to 100644
File size: 69.9 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 : Thu May 23 16:59:00 2019
13// Update Count     : 6
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                cache.emplace( node, decl );
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                declPostamble( decl, node );
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        const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) {
165                // base comes from constructor
166                decl->parameters = get<TypeDecl>().acceptL( node->params );
167                decl->assertions = get<DeclarationWithType>().acceptL( node->assertions );
168                declPostamble( decl, node );
169                return nullptr;
170        }
171
172        const ast::Decl * visit( const ast::TypeDecl * node ) override final {
173                if ( inCache( node ) ) return nullptr;
174                auto decl = new TypeDecl(
175                        node->name,
176                        Type::StorageClasses( node->storage.val ),
177                        get<Type>().accept1( node->base ),
178                        (TypeDecl::Kind)(unsigned)node->kind,
179                        node->sized,
180                        get<Type>().accept1( node->init )
181                );
182                cache.emplace( node, decl );
183                return namedTypePostamble( decl, node );
184        }
185
186        const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
187                auto decl = new TypedefDecl(
188                        node->name,
189                        node->location,
190                        Type::StorageClasses( node->storage.val ),
191            get<Type>().accept1( node->base ),
192                        LinkageSpec::Spec( node->linkage.val )
193                );
194                return namedTypePostamble( decl, node );
195        }
196
197        const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) {
198                cache.emplace( node, decl );
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                declPostamble( decl, node );
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                cache.emplace( node, stmt );
266                stmt->location = node->location;
267                stmt->labels = makeLabelL( stmt, node->labels );
268                this->node = stmt;
269                return nullptr;
270        }
271
272        const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
273                if ( inCache( node ) ) return nullptr;
274                auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
275                stmtPostamble( stmt, node );
276                return nullptr;
277        }
278
279        const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
280                if ( inCache( node ) ) return nullptr;
281                auto stmt = new ExprStmt( nullptr );
282                cache.emplace( node, stmt );
283                stmt->expr = get<Expression>().accept1( node->expr );
284                return stmtPostamble( stmt, node );
285        }
286
287        const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
288                if ( inCache( node ) ) return nullptr;
289                auto stmt = new AsmStmt(
290                        node->isVolatile,
291                        get<Expression>().accept1( node->instruction ),
292                        get<Expression>().acceptL( node->output ),
293                        get<Expression>().acceptL( node->input ),
294                        get<ConstantExpr>().acceptL( node->clobber ),
295                        makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
296                );
297                return stmtPostamble( stmt, node );
298        }
299
300        const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
301                if ( inCache( node ) ) return nullptr;
302                auto stmt = new DirectiveStmt( node->directive );
303                return stmtPostamble( stmt, node );
304        }
305
306        const ast::Stmt * visit( const ast::IfStmt * node ) override final {
307                if ( inCache( node ) ) return nullptr;
308                auto stmt = new IfStmt(
309                        get<Expression>().accept1( node->cond ),
310                        get<Statement>().accept1( node->thenPart ),
311                        get<Statement>().accept1( node->elsePart ),
312                        get<Statement>().acceptL( node->inits )
313                );
314                return stmtPostamble( stmt, node );
315        }
316
317        const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
318                if ( inCache( node ) ) return nullptr;
319                auto stmt = new SwitchStmt(
320                        get<Expression>().accept1( node->cond ),
321                        get<Statement>().acceptL( node->stmts )
322                );
323                return stmtPostamble( stmt, node );
324        }
325
326        const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
327                if ( inCache( node ) ) return nullptr;
328                auto stmt = new CaseStmt(
329                        get<Expression>().accept1( node->cond ),
330                        get<Statement>().acceptL( node->stmts ),
331                        node->isDefault()
332                );
333                return stmtPostamble( stmt, node );
334        }
335
336        const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
337                if ( inCache( node ) ) return nullptr;
338                auto inits = get<Statement>().acceptL( node->inits );
339                auto stmt = new WhileStmt(
340                        get<Expression>().accept1( node->cond ),
341                        get<Statement>().accept1( node->body ),
342                        inits,
343                        node->isDoWhile
344                );
345                return stmtPostamble( stmt, node );
346        }
347
348        const ast::Stmt * visit( const ast::ForStmt * node ) override final {
349                if ( inCache( node ) ) return nullptr;
350                auto stmt = new ForStmt(
351                        get<Statement>().acceptL( node->inits ),
352                        get<Expression>().accept1( node->cond ),
353                        get<Expression>().accept1( node->inc ),
354                        get<Statement>().accept1( node->body )
355                );
356                return stmtPostamble( stmt, node );
357        }
358
359        const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
360                if ( inCache( node ) ) return nullptr;
361                BranchStmt * stmt;
362                if (node->computedTarget) {
363                        stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
364                                BranchStmt::Goto );
365                } else {
366                        BranchStmt::Type type;
367                        switch (node->kind) {
368                        #define CASE(n) \
369                        case ast::BranchStmt::n: \
370                                type = BranchStmt::n; \
371                                break
372                        CASE(Goto);
373                        CASE(Break);
374                        CASE(Continue);
375                        CASE(FallThrough);
376                        CASE(FallThroughDefault);
377                        #undef CASE
378                        default:
379                                assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
380                        }
381
382                        // The labels here are also weird.
383                        stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
384                        stmt->target = makeLabel( stmt, node->target );
385                }
386                return stmtPostamble( stmt, node );
387        }
388
389        const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
390                if ( inCache( node ) ) return nullptr;
391                auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
392                return stmtPostamble( stmt, node );
393        }
394
395        const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
396                if ( inCache( node ) ) return nullptr;
397                ThrowStmt::Kind kind;
398                switch (node->kind) {
399                case ast::ExceptionKind::Terminate:
400                        kind = ThrowStmt::Terminate;
401                        break;
402                case ast::ExceptionKind::Resume:
403                        kind = ThrowStmt::Resume;
404                        break;
405                default:
406                        assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
407                }
408                auto stmt = new ThrowStmt(
409                        kind,
410                        get<Expression>().accept1( node->expr ),
411                        get<Expression>().accept1( node->target )
412                );
413                return stmtPostamble( stmt, node );
414        }
415
416        const ast::Stmt * visit( const ast::TryStmt * node ) override final {
417                if ( inCache( node ) ) return nullptr;
418                auto handlers = get<CatchStmt>().acceptL( node->handlers );
419                auto stmt = new TryStmt(
420                        get<CompoundStmt>().accept1( node->body ),
421                        handlers,
422                        get<FinallyStmt>().accept1( node->finally )
423                );
424                return stmtPostamble( stmt, node );
425        }
426
427        const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
428                if ( inCache( node ) ) return nullptr;
429                CatchStmt::Kind kind;
430                switch (node->kind) {
431                case ast::ExceptionKind::Terminate:
432                        kind = CatchStmt::Terminate;
433                        break;
434                case ast::ExceptionKind::Resume:
435                        kind = CatchStmt::Resume;
436                        break;
437                default:
438                        assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
439                }
440                auto stmt = new CatchStmt(
441                        kind,
442                        get<Declaration>().accept1( node->decl ),
443                        get<Expression>().accept1( node->cond ),
444                        get<Statement>().accept1( node->body )
445                );
446                return stmtPostamble( stmt, node );
447        }
448
449        const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
450                if ( inCache( node ) ) return nullptr;
451                auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
452                return stmtPostamble( stmt, node );
453        }
454
455        const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
456                if ( inCache( node ) ) return nullptr;
457                auto stmt = new WaitForStmt;
458                stmt->clauses.reserve( node->clauses.size() );
459                for ( auto clause : node->clauses ) {
460                        stmt->clauses.push_back({{
461                                        get<Expression>().accept1( clause.target.func ),
462                                        get<Expression>().acceptL( clause.target.args ),
463                                },
464                                get<Statement>().accept1( clause.stmt ),
465                                get<Expression>().accept1( clause.cond ),
466                        });
467                }
468                stmt->timeout = {
469                        get<Expression>().accept1( node->timeout.time ),
470                        get<Statement>().accept1( node->timeout.stmt ),
471                        get<Expression>().accept1( node->timeout.cond ),
472                };
473                stmt->orelse = {
474                        get<Statement>().accept1( node->orElse.stmt ),
475                        get<Expression>().accept1( node->orElse.cond ),
476                };
477                return stmtPostamble( stmt, node );
478        }
479
480        const ast::Stmt * visit( const ast::WithStmt * node ) override final {
481                if ( inCache( node ) ) return nullptr;
482                auto stmt = new WithStmt(
483                        get<Expression>().acceptL( node->exprs ),
484                        get<Statement>().accept1( node->stmt )
485                );
486                return stmtPostamble( stmt, node );
487        }
488
489        const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
490                if ( inCache( node ) ) return nullptr;
491                auto stmt = new NullStmt();
492                stmtPostamble( stmt, node );
493                return nullptr;
494        }
495
496        const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
497                if ( inCache( node ) ) return nullptr;
498                auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
499                return stmtPostamble( stmt, node );
500        }
501
502        const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
503                if ( inCache( node ) ) return nullptr;
504                auto stmt = new ImplicitCtorDtorStmt{
505                        get<Statement>().accept1( node->callStmt )
506                };
507                return stmtPostamble( stmt, node );
508        }
509
510        TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
511
512                if (!src) return nullptr;
513
514                TypeSubstitution *rslt = new TypeSubstitution();
515
516                for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
517                        rslt->add( src_i->first,
518                                   get<Type>().accept1(src_i->second) );
519                }
520
521                for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
522                        rslt->addVar( src_i->first,
523                                      get<Expression>().accept1(src_i->second) );
524                }
525
526                return rslt;
527        }
528
529        void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
530                                                   std::vector<UniqueId>         &tgtResnSlots,
531                                                   const ast::Expr::InferUnion   &srcInferred ) {
532
533                assert( tgtInferParams.empty() );
534                assert( tgtResnSlots.empty() );
535
536                if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
537                        const ast::InferredParams &srcParams = srcInferred.inferParamsConst();
538                        for (auto srcParam : srcParams) {
539                                tgtInferParams[srcParam.first] = ParamEntry(
540                                        srcParam.second.decl,
541                                        get<Type>().accept1(srcParam.second.actualType),
542                                        get<Type>().accept1(srcParam.second.formalType),
543                                        get<Expression>().accept1(srcParam.second.expr)
544                                );
545                        }
546                } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots  ) {
547                        const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst();
548                        for (auto srcSlot : srcSlots) {
549                                tgtResnSlots.push_back(srcSlot);
550                        }
551                }
552        }
553
554        Expression * visitBaseExpr_skipResultType(const ast::Expr * src, Expression * tgt) {
555
556                tgt->location  = src->location;
557                tgt->env       = convertTypeSubstitution(src->env);
558                tgt->extension = src->extension;
559
560                convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
561                return tgt;
562        }
563
564        Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
565
566                tgt->result = get<Type>().accept1(src->result);
567                return visitBaseExpr_skipResultType(src, tgt);
568        }
569
570        const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
571                auto expr = visitBaseExpr( node,
572                        new ApplicationExpr(
573                                get<Expression>().accept1(node->func),
574                                get<Expression>().acceptL(node->args)
575                        )
576                );
577                this->node = expr;
578                return nullptr;
579        }
580
581        const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
582                auto expr = visitBaseExpr( node,
583                        new UntypedExpr(
584                                get<Expression>().accept1(node->func),
585                                get<Expression>().acceptL(node->args)
586                        )
587                );
588                this->node = expr;
589                return nullptr;
590        }
591
592        const ast::Expr * visit( const ast::NameExpr * node ) override final {
593                auto expr = visitBaseExpr( node,
594                        new NameExpr(
595                                node->name
596                        )
597                );
598                this->node = expr;
599                return nullptr;
600        }
601
602        const ast::Expr * visit( const ast::AddressExpr * node ) override final {
603                auto expr = visitBaseExpr( node,
604                        new AddressExpr(
605                                get<Expression>().accept1(node->arg)
606                        )
607                );
608                this->node = expr;
609                return nullptr;
610        }
611
612        const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
613                auto expr = visitBaseExpr( node,
614                        new LabelAddressExpr(
615                                makeLabel(nullptr, node->arg)
616                        )
617                );
618                this->node = expr;
619                return nullptr;
620        }
621
622        const ast::Expr * visit( const ast::CastExpr * node ) override final {
623                auto expr = visitBaseExpr( node,
624                        new CastExpr(
625                                get<Expression>().accept1(node->arg),
626                                (node->isGenerated == ast::GeneratedCast)
627                        )
628                );
629                this->node = expr;
630                return nullptr;
631        }
632
633        const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
634                KeywordCastExpr::Target castTarget = KeywordCastExpr::NUMBER_OF_TARGETS;
635                switch (node->target) {
636                        case ast::KeywordCastExpr::Coroutine:
637                                castTarget = KeywordCastExpr::Coroutine;
638                                break;
639                        case ast::KeywordCastExpr::Thread:
640                                castTarget = KeywordCastExpr::Thread;
641                                break;
642                        case ast::KeywordCastExpr::Monitor:
643                                castTarget = KeywordCastExpr::Monitor;
644                                break;
645                        default:
646                                break;
647                }
648                assert ( castTarget < KeywordCastExpr::NUMBER_OF_TARGETS );
649                auto expr = visitBaseExpr( node,
650                        new KeywordCastExpr(
651                                get<Expression>().accept1(node->arg),
652                                castTarget
653                        )
654                );
655                this->node = expr;
656                return nullptr;
657        }
658
659        const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
660                auto expr = visitBaseExpr_skipResultType( node,
661                        new VirtualCastExpr(
662                                get<Expression>().accept1(node->arg),
663                                get<Type>().accept1(node->result)
664                        )
665                );
666                this->node = expr;
667                return nullptr;
668        }
669
670        const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
671                auto expr = visitBaseExpr( node,
672                        new UntypedMemberExpr(
673                                get<Expression>().accept1(node->member),
674                                get<Expression>().accept1(node->aggregate)
675                        )
676                );
677                this->node = expr;
678                return nullptr;
679        }
680
681        const ast::Expr * visit( const ast::MemberExpr * node ) override final {
682                auto expr = visitBaseExpr( node,
683                        new MemberExpr(
684                                inCache(node->member) ?
685                                        dynamic_cast<DeclarationWithType *>(this->node) :
686                                        get<DeclarationWithType>().accept1(node->member),
687                                get<Expression>().accept1(node->aggregate)
688                        )
689                );
690                this->node = expr;
691                return nullptr;
692        }
693
694        const ast::Expr * visit( const ast::VariableExpr * node ) override final {
695                auto expr = visitBaseExpr( node,
696                        new VariableExpr(
697                                inCache(node->var) ?
698                                        dynamic_cast<DeclarationWithType *>(this->node) :
699                                        get<DeclarationWithType>().accept1(node->var)
700                        )
701                );
702                this->node = expr;
703                return nullptr;
704        }
705
706        const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
707                ConstantExpr *rslt = nullptr;
708                switch ( node->kind ) {
709                case ast::ConstantExpr::Integer:
710                        rslt = new ConstantExpr{Constant{
711                                get<Type>().accept1( node->result ),
712                                node->rep,
713                                (unsigned long long) node->intValue()
714                        }};
715                        break;
716                case ast::ConstantExpr::FloatingPoint:
717                        rslt = new ConstantExpr{Constant{
718                                get<Type>().accept1(node->result),
719                                node->rep,
720                                (double) node->floatValue()
721                        }};
722                        break;
723                case ast::ConstantExpr::String:
724                        rslt = new ConstantExpr{Constant::from_string( node->rep )};
725                        break;
726                }
727                assert(rslt);
728                auto expr = visitBaseExpr( node, rslt );
729                this->node = expr;
730                return nullptr;
731        }
732
733        const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
734                assert (node->expr || node->type);
735                assert (! (node->expr && node->type));
736                SizeofExpr *rslt;
737                if (node->expr) {
738                        rslt = new SizeofExpr(
739                                get<Expression>().accept1(node->expr)
740                        );
741                        assert (!rslt->isType);
742                }
743                if (node->type) {
744                        rslt = new SizeofExpr(
745                                get<Type>().accept1(node->type)
746                        );
747                        assert (rslt->isType);
748                }
749                auto expr = visitBaseExpr( node, rslt );
750                this->node = expr;
751                return nullptr;
752        }
753
754        const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
755                assert (node->expr || node->type);
756                assert (! (node->expr && node->type));
757                AlignofExpr *rslt;
758                if (node->expr) {
759                        rslt = new AlignofExpr(
760                                get<Expression>().accept1(node->expr)
761                        );
762                        assert (!rslt->isType);
763                }
764                if (node->type) {
765                        rslt = new AlignofExpr(
766                                get<Type>().accept1(node->type)
767                        );
768                        assert (rslt->isType);
769                }
770                auto expr = visitBaseExpr( node, rslt );
771                this->node = expr;
772                return nullptr;
773        }
774
775        const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
776                auto expr = visitBaseExpr( node,
777                        new UntypedOffsetofExpr(
778                                get<Type>().accept1(node->type),
779                                node->member
780                        )
781                );
782                this->node = expr;
783                return nullptr;
784        }
785
786        const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
787                auto expr = visitBaseExpr( node,
788                        new OffsetofExpr(
789                                get<Type>().accept1(node->type),
790                                inCache(node->member) ?
791                                        dynamic_cast<DeclarationWithType *>(this->node) :
792                                        get<DeclarationWithType>().accept1(node->member)
793                        )
794                );
795                this->node = expr;
796                return nullptr;
797        }
798
799        const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
800                auto expr = visitBaseExpr( node,
801                        new OffsetPackExpr(
802                                get<StructInstType>().accept1(node->type)
803                        )
804                );
805                this->node = expr;
806                return nullptr;
807        }
808
809        const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
810                assert (node->isAnd == ast::LogicalFlag::AndExpr ||
811                                node->isAnd == ast::LogicalFlag::OrExpr );
812                auto expr = visitBaseExpr( node,
813                        new LogicalExpr(
814                                get<Expression>().accept1(node->arg1),
815                                get<Expression>().accept1(node->arg2),
816                                (node->isAnd == ast::LogicalFlag::AndExpr)
817                        )
818                );
819                this->node = expr;
820                return nullptr;
821        }
822
823        const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
824                auto expr = visitBaseExpr( node,
825                        new ConditionalExpr(
826                                get<Expression>().accept1(node->arg1),
827                                get<Expression>().accept1(node->arg2),
828                                get<Expression>().accept1(node->arg3)
829                        )
830                );
831                this->node = expr;
832                return nullptr;
833        }
834
835        const ast::Expr * visit( const ast::CommaExpr * node ) override final {
836                auto expr = visitBaseExpr( node,
837                        new CommaExpr(
838                                get<Expression>().accept1(node->arg1),
839                                get<Expression>().accept1(node->arg2)
840                        )
841                );
842                this->node = expr;
843                return nullptr;
844        }
845
846        const ast::Expr * visit( const ast::TypeExpr * node ) override final {
847                auto expr = visitBaseExpr( node,
848                        new TypeExpr(
849                                get<Type>().accept1(node->type)
850                        )
851                );
852                this->node = expr;
853                return nullptr;
854        }
855
856        const ast::Expr * visit( const ast::AsmExpr * node ) override final {
857                auto expr = visitBaseExpr( node,
858                        new AsmExpr(
859                                get<Expression>().accept1(node->inout),
860                                get<Expression>().accept1(node->constraint),
861                                get<Expression>().accept1(node->operand)
862                        )
863                );
864                this->node = expr;
865                return nullptr;
866        }
867
868        const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
869                auto rslt = new ImplicitCopyCtorExpr(
870                        get<ApplicationExpr>().accept1(node->callExpr)
871                );
872
873                rslt->tempDecls = get<ObjectDecl>().acceptL(node->tempDecls);
874                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
875                rslt->dtors = get<Expression>().acceptL(node->dtors);
876
877                auto expr = visitBaseExpr( node, rslt );
878                this->node = expr;
879                return nullptr;
880        }
881
882        const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
883                auto expr = visitBaseExpr( node,
884                        new ConstructorExpr(
885                                get<Expression>().accept1(node->callExpr)
886                        )
887                );
888                this->node = expr;
889                return nullptr;
890        }
891
892        const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
893                auto expr = visitBaseExpr_skipResultType( node,
894                        new CompoundLiteralExpr(
895                                get<Type>().accept1(node->result),
896                                get<Initializer>().accept1(node->init)
897                        )
898                );
899                this->node = expr;
900                return nullptr;
901        }
902
903        const ast::Expr * visit( const ast::RangeExpr * node ) override final {
904                auto expr = visitBaseExpr( node,
905                        new RangeExpr(
906                                get<Expression>().accept1(node->low),
907                                get<Expression>().accept1(node->high)
908                        )
909                );
910                this->node = expr;
911                return nullptr;
912        }
913
914        const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
915                auto expr = visitBaseExpr( node,
916                        new UntypedTupleExpr(
917                                get<Expression>().acceptL(node->exprs)
918                        )
919                );
920                this->node = expr;
921                return nullptr;
922        }
923
924        const ast::Expr * visit( const ast::TupleExpr * node ) override final {
925                auto expr = visitBaseExpr( node,
926                        new UntypedTupleExpr(
927                                get<Expression>().acceptL(node->exprs)
928                        )
929                );
930                this->node = expr;
931                return nullptr;
932        }
933
934        const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
935                auto expr = visitBaseExpr( node,
936                        new TupleIndexExpr(
937                                get<Expression>().accept1(node->tuple),
938                                node->index
939                        )
940                );
941                this->node = expr;
942                return nullptr;
943        }
944
945        const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
946                auto expr = visitBaseExpr( node,
947                        new TupleAssignExpr(
948                                get<StmtExpr>().accept1(node->stmtExpr)
949                        )
950                );
951                this->node = expr;
952                return nullptr;
953        }
954
955        const ast::Expr * visit( const ast::StmtExpr * node ) override final {
956                auto rslt = new StmtExpr(
957                        get<CompoundStmt>().accept1(node->stmts)
958                );
959
960                rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
961                rslt->dtors       = get<Expression>().acceptL(node->dtors);
962
963                auto expr = visitBaseExpr( node, rslt );
964                this->node = expr;
965                return nullptr;
966        }
967
968        const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
969                auto rslt = new UniqueExpr(
970                        get<Expression>().accept1(node->expr)
971                );
972
973                rslt->object = get<ObjectDecl>  ().accept1(node->object);
974                rslt->var    = get<VariableExpr>().accept1(node->var);
975
976                auto expr = visitBaseExpr( node, rslt );
977                this->node = expr;
978                return nullptr;
979        }
980
981        const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
982                std::list<InitAlternative> initAlts;
983                for (auto ia : node->initAlts) {
984                        initAlts.push_back(InitAlternative(
985                                get<Type>       ().accept1(ia.type),
986                                get<Designation>().accept1(ia.designation)
987                        ));
988                }
989                auto expr = visitBaseExpr( node,
990                        new UntypedInitExpr(
991                                get<Expression>().accept1(node->expr),
992                                initAlts
993                        )
994                );
995                this->node = expr;
996                return nullptr;
997        }
998
999        const ast::Expr * visit( const ast::InitExpr * node ) override final {
1000                auto expr = visitBaseExpr( node,
1001                        new InitExpr(
1002                                get<Expression>().accept1(node->expr),
1003                                get<Designation>().accept1(node->designation)
1004                        )
1005                );
1006                this->node = expr;
1007                return nullptr;
1008        }
1009
1010        const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
1011                auto expr = visitBaseExpr( node,
1012                        new DeletedExpr(
1013                                get<Expression>().accept1(node->expr),
1014                                inCache(node->deleteStmt) ?
1015                                        this->node :
1016                                        get<BaseSyntaxNode>().accept1(node->deleteStmt)
1017                        )
1018                );
1019                this->node = expr;
1020                return nullptr;
1021        }
1022
1023        const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
1024                auto expr = visitBaseExpr( node,
1025                        new DefaultArgExpr(
1026                                get<Expression>().accept1(node->expr)
1027                        )
1028                );
1029                this->node = expr;
1030                return nullptr;
1031        }
1032
1033        const ast::Expr * visit( const ast::GenericExpr * node ) override final {
1034                std::list<GenericExpr::Association> associations;
1035                for (auto association : node->associations) {
1036                        associations.push_back(GenericExpr::Association(
1037                                get<Type>      ().accept1(association.type),
1038                                get<Expression>().accept1(association.expr)
1039                        ));
1040                }
1041                auto expr = visitBaseExpr( node,
1042                        new GenericExpr(
1043                                get<Expression>().accept1(node->control),
1044                                associations
1045                        )
1046                );
1047                this->node = expr;
1048                return nullptr;
1049        }
1050
1051        const ast::Type * visit( const ast::VoidType * node ) override final {
1052                this->node = new VoidType{ cv( node ) };
1053                return nullptr;
1054        }
1055
1056        const ast::Type * visit( const ast::BasicType * node ) override final {
1057                this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
1058                return nullptr;
1059        }
1060
1061        const ast::Type * visit( const ast::PointerType * node ) override final {
1062                this->node = new PointerType{
1063                        cv( node ),
1064                        get<Type>().accept1( node->base ),
1065                        get<Expression>().accept1( node->dimension ),
1066                        (bool)node->isVarLen,
1067                        (bool)node->isStatic
1068                };
1069                return nullptr;
1070        }
1071
1072        const ast::Type * visit( const ast::ArrayType * node ) override final {
1073                this->node = new ArrayType{
1074                        cv( node ),
1075                        get<Type>().accept1( node->base ),
1076                        get<Expression>().accept1( node->dimension ),
1077                        (bool)node->isVarLen,
1078                        (bool)node->isStatic
1079                };
1080                return nullptr;
1081        }
1082
1083        const ast::Type * visit( const ast::ReferenceType * node ) override final {
1084                this->node = new ReferenceType{
1085                        cv( node ),
1086                        get<Type>().accept1( node->base )
1087                };
1088                return nullptr;
1089        }
1090
1091        const ast::Type * visit( const ast::QualifiedType * node ) override final {
1092                this->node = new QualifiedType{
1093                        cv( node ),
1094                        get<Type>().accept1( node->parent ),
1095                        get<Type>().accept1( node->child )
1096                };
1097                return nullptr;
1098        }
1099
1100        const ast::Type * visit( const ast::FunctionType * node ) override final {
1101                auto ty = new FunctionType {
1102                        cv( node ),
1103                        (bool)node->isVarArgs
1104                };
1105                ty->returnVals = get<DeclarationWithType>().acceptL( node->returns );
1106                ty->parameters = get<DeclarationWithType>().acceptL( node->params );
1107                ty->forall = get<TypeDecl>().acceptL( node->forall );
1108                this->node = ty;
1109                return nullptr;
1110        }
1111
1112        void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {
1113                ty->forall = get<TypeDecl>().acceptL( old->forall );
1114                ty->parameters = get<Expression>().acceptL( old->params );
1115                ty->hoistType = old->hoistType;
1116        }
1117
1118        const ast::Type * visit( const ast::StructInstType * node ) override final {
1119                StructInstType * ty;
1120                if ( node->base ) {
1121                        ty = new StructInstType{
1122                                cv( node ),
1123                                get<StructDecl>().accept1( node->base ),
1124                                get<Attribute>().acceptL( node->attributes )
1125                        };
1126                } else {
1127                        ty = new StructInstType{
1128                                cv( node ),
1129                                node->name,
1130                                get<Attribute>().acceptL( node->attributes )
1131                        };
1132                }
1133                postvisit( node, ty );
1134                this->node = ty;
1135                return nullptr;
1136        }
1137
1138        const ast::Type * visit( const ast::UnionInstType * node ) override final {
1139                UnionInstType * ty;
1140                if ( node->base ) {
1141                        ty = new UnionInstType{
1142                                cv( node ),
1143                                get<UnionDecl>().accept1( node->base ),
1144                                get<Attribute>().acceptL( node->attributes )
1145                        };
1146                } else {
1147                        ty = new UnionInstType{
1148                                cv( node ),
1149                                node->name,
1150                                get<Attribute>().acceptL( node->attributes )
1151                        };
1152                }
1153                postvisit( node, ty );
1154                this->node = ty;
1155                return nullptr;
1156        }
1157
1158        const ast::Type * visit( const ast::EnumInstType * node ) override final {
1159                EnumInstType * ty;
1160                if ( node->base ) {
1161                        ty = new EnumInstType{
1162                                cv( node ),
1163                                get<EnumDecl>().accept1( node->base ),
1164                                get<Attribute>().acceptL( node->attributes )
1165                        };
1166                } else {
1167                        ty = new EnumInstType{
1168                                cv( node ),
1169                                node->name,
1170                                get<Attribute>().acceptL( node->attributes )
1171                        };
1172                }
1173                postvisit( node, ty );
1174                this->node = ty;
1175                return nullptr;
1176        }
1177
1178        const ast::Type * visit( const ast::TraitInstType * node ) override final {
1179                TraitInstType * ty;
1180                if ( node->base ) {
1181                        ty = new TraitInstType{
1182                                cv( node ),
1183                                get<TraitDecl>().accept1( node->base ),
1184                                get<Attribute>().acceptL( node->attributes )
1185                        };
1186                } else {
1187                        ty = new TraitInstType{
1188                                cv( node ),
1189                                node->name,
1190                                get<Attribute>().acceptL( node->attributes )
1191                        };
1192                }
1193                postvisit( node, ty );
1194                this->node = ty;
1195                return nullptr;
1196        }
1197
1198        const ast::Type * visit( const ast::TypeInstType * node ) override final {
1199                TypeInstType * ty;
1200                if ( node->base ) {
1201                        ty = new TypeInstType{
1202                                cv( node ),
1203                                node->name,
1204                                get<TypeDecl>().accept1( node->base ),
1205                                get<Attribute>().acceptL( node->attributes )
1206                        };
1207                } else {
1208                        ty = new TypeInstType{
1209                                cv( node ),
1210                                node->name,
1211                                node->kind == ast::TypeVar::Ftype,
1212                                get<Attribute>().acceptL( node->attributes )
1213                        };
1214                }
1215                postvisit( node, ty );
1216                this->node = ty;
1217                return nullptr;
1218        }
1219
1220        const ast::Type * visit( const ast::TupleType * node ) override final {
1221                this->node = new TupleType{
1222                        cv( node ),
1223                        get<Type>().acceptL( node->types )
1224                        // members generated by TupleType c'tor
1225                };
1226                return nullptr;
1227        }
1228
1229        const ast::Type * visit( const ast::TypeofType * node ) override final {
1230                this->node = new TypeofType{
1231                        cv( node ),
1232                        get<Expression>().accept1( node->expr ),
1233                        (bool)node->kind
1234                };
1235                return nullptr;
1236        }
1237
1238        const ast::Type * visit( const ast::VarArgsType * node ) override final {
1239                this->node = new VarArgsType{ cv( node ) };
1240                return nullptr;
1241        }
1242
1243        const ast::Type * visit( const ast::ZeroType * node ) override final {
1244                this->node = new ZeroType{ cv( node ) };
1245                return nullptr;
1246        }
1247
1248        const ast::Type * visit( const ast::OneType * node ) override final {
1249                this->node = new OneType{ cv( node ) };
1250                return nullptr;
1251        }
1252
1253        const ast::Type * visit( const ast::GlobalScopeType * ) override final {
1254                this->node = new GlobalScopeType{};
1255                return nullptr;
1256        }
1257
1258        const ast::Designation * visit( const ast::Designation * node ) override final {
1259                auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
1260                designation->location = node->location;
1261                this->node = designation;
1262                return nullptr;
1263        }
1264
1265        const ast::Init * visit( const ast::SingleInit * node ) override final {
1266                auto init = new SingleInit(
1267                        get<Expression>().accept1( node->value ),
1268                        ast::MaybeConstruct == node->maybeConstructed
1269                );
1270                init->location = node->location;
1271                this->node = init;
1272                return nullptr;
1273        }
1274
1275        const ast::Init * visit( const ast::ListInit * node ) override final {
1276                auto init = new ListInit(
1277                        get<Initializer>().acceptL( node->initializers ),
1278                        get<Designation>().acceptL( node->designations ),
1279                        ast::MaybeConstruct == node->maybeConstructed
1280                );
1281                init->location = node->location;
1282                this->node = init;
1283                return nullptr;
1284        }
1285
1286        const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1287                auto init = new ConstructorInit(
1288                        get<Statement>().accept1( node->ctor ),
1289                        get<Statement>().accept1( node->dtor ),
1290                        get<Initializer>().accept1( node->init )
1291                );
1292                init->location = node->location;
1293                this->node = init;
1294                return nullptr;
1295        }
1296
1297        const ast::Attribute * visit( const ast::Attribute * node ) override final {
1298                auto attr = new Attribute(
1299                        node->name,
1300                        get<Expression>().acceptL(node->params)
1301                );
1302                this->node = attr;
1303                return nullptr;
1304        }
1305
1306        const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1307                // Handled by convertTypeSubstitution helper instead.
1308                // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
1309                assert( 0 );
1310                (void)node;
1311                return nullptr;
1312        }
1313};
1314
1315std::list< Declaration * > convert( const std::list< ast::ptr< ast::Decl > > && translationUnit ) {
1316        ConverterNewToOld c;
1317        std::list< Declaration * > decls;
1318        for(auto d : translationUnit) {
1319                decls.emplace_back( c.decl( d ) );
1320        }
1321        return decls;
1322}
1323
1324//================================================================================================
1325
1326class ConverterOldToNew : public Visitor {
1327public:
1328        ast::Decl * decl() {
1329                return strict_dynamic_cast< ast::Decl * >( node );
1330        }
1331private:
1332        /// conversion output
1333        ast::Node * node;
1334        /// cache of nodes that might be referenced by readonly<> for de-duplication
1335        std::unordered_map< BaseSyntaxNode *, ast::Node * > cache;
1336
1337        // Local Utilities:
1338
1339        template<typename NewT, typename OldT>
1340        NewT * getAccept1( OldT old ) {
1341                if ( ! old ) return nullptr;
1342                old->accept(*this);
1343                return strict_dynamic_cast< NewT * >( node );
1344        }
1345
1346#       define GET_ACCEPT_1(child, type) \
1347                getAccept1< ast::type, decltype( old->child ) >( old->child )
1348
1349        template<typename NewT, typename OldC>
1350        std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
1351                std::vector< ast::ptr<NewT> > ret;
1352                ret.reserve( old.size() );
1353                for ( auto a : old ) {
1354                        a->accept( *this );
1355                        ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1356                }
1357                return ret;
1358        }
1359
1360#       define GET_ACCEPT_V(child, type) \
1361                getAcceptV< ast::type, decltype( old->child ) >( old->child )
1362
1363        ast::Label make_label(Label* old) {
1364                return ast::Label(
1365                        old->labelled->location,
1366                        old->name,
1367                        GET_ACCEPT_V(attributes, Attribute)
1368                );
1369        }
1370
1371        template<template <class...> class C>
1372        C<ast::Label> make_labels(C<Label> olds) {
1373                C<ast::Label> ret;
1374                for (auto oldn : olds) {
1375                        ret.push_back( make_label( &oldn ) );
1376                }
1377                return ret;
1378        }
1379
1380#       define GET_LABELS_V(labels) \
1381                to<std::vector>::from( make_labels( std::move( labels ) ) )
1382
1383        static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; }
1384
1385        /// returns true and sets `node` if in cache
1386        bool inCache( BaseSyntaxNode * old ) {
1387                auto it = cache.find( old );
1388                if ( it == cache.end() ) return false;
1389                node = it->second;
1390                return true;
1391        }
1392
1393        // Now all the visit functions:
1394
1395        virtual void visit( ObjectDecl * old ) override final {
1396                if ( inCache( old ) ) return;
1397                auto decl = new ast::ObjectDecl(
1398                        old->location,
1399                        old->name,
1400                        GET_ACCEPT_1(type, Type),
1401                        GET_ACCEPT_1(init, Init),
1402                        { old->get_storageClasses().val },
1403                        { old->linkage.val },
1404                        GET_ACCEPT_1(bitfieldWidth, Expr),
1405                        GET_ACCEPT_V(attributes, Attribute),
1406                        { old->get_funcSpec().val }
1407                );
1408                cache.emplace( old, decl );
1409                decl->scopeLevel = old->scopeLevel;
1410                decl->mangleName = old->mangleName;
1411                decl->isDeleted  = old->isDeleted;
1412                decl->uniqueId   = old->uniqueId;
1413                decl->extension  = old->extension;
1414
1415                this->node = decl;
1416        }
1417
1418        virtual void visit( FunctionDecl * old ) override final {
1419                if ( inCache( old ) ) return;
1420                auto decl = new ast::FunctionDecl{
1421                        old->location,
1422                        old->name,
1423                        GET_ACCEPT_1(type, FunctionType),
1424                        GET_ACCEPT_1(statements, CompoundStmt),
1425                        { old->storageClasses.val },
1426                        { old->linkage.val },
1427                        GET_ACCEPT_V(attributes, Attribute),
1428                        { old->get_funcSpec().val }
1429                };
1430                cache.emplace( old, decl );
1431                decl->scopeLevel = old->scopeLevel;
1432                decl->mangleName = old->mangleName;
1433                decl->isDeleted  = old->isDeleted;
1434                decl->uniqueId   = old->uniqueId;
1435                decl->extension  = old->extension;
1436
1437                this->node = decl;
1438        }
1439
1440        virtual void visit( StructDecl * old ) override final {
1441                if ( inCache( old ) ) return;
1442                auto decl = new ast::StructDecl(
1443                        old->location,
1444                        old->name,
1445                        old->kind,
1446                        GET_ACCEPT_V(attributes, Attribute),
1447                        { old->linkage.val }
1448                );
1449                cache.emplace( old, decl );
1450                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1451                decl->body   = old->body;
1452                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1453                decl->members    = GET_ACCEPT_V(members, Decl);
1454                decl->extension  = old->extension;
1455                decl->uniqueId   = old->uniqueId;
1456                decl->storage    = { old->storageClasses.val };
1457
1458                this->node = decl;
1459        }
1460
1461        virtual void visit( UnionDecl * old ) override final {
1462                if ( inCache( old ) ) return;
1463                auto decl = new ast::UnionDecl(
1464                        old->location,
1465                        old->name,
1466                        GET_ACCEPT_V(attributes, Attribute),
1467                        { old->linkage.val }
1468                );
1469                cache.emplace( old, decl );
1470                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1471                decl->body   = old->body;
1472                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1473                decl->members    = GET_ACCEPT_V(members, Decl);
1474                decl->extension  = old->extension;
1475                decl->uniqueId   = old->uniqueId;
1476                decl->storage    = { old->storageClasses.val };
1477
1478                this->node = decl;
1479        }
1480
1481        virtual void visit( EnumDecl * old ) override final {
1482                if ( inCache( old ) ) return;
1483                auto decl = new ast::UnionDecl(
1484                        old->location,
1485                        old->name,
1486                        GET_ACCEPT_V(attributes, Attribute),
1487                        { old->linkage.val }
1488                );
1489                cache.emplace( old, decl );
1490                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1491                decl->body   = old->body;
1492                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1493                decl->members    = GET_ACCEPT_V(members, Decl);
1494                decl->extension  = old->extension;
1495                decl->uniqueId   = old->uniqueId;
1496                decl->storage    = { old->storageClasses.val };
1497
1498                this->node = decl;
1499        }
1500
1501        virtual void visit( TraitDecl * old ) override final {
1502                if ( inCache( old ) ) return;
1503                auto decl = new ast::UnionDecl(
1504                        old->location,
1505                        old->name,
1506                        GET_ACCEPT_V(attributes, Attribute),
1507                        { old->linkage.val }
1508                );
1509                cache.emplace( old, decl );
1510                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1511                decl->body   = old->body;
1512                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1513                decl->members    = GET_ACCEPT_V(members, Decl);
1514                decl->extension  = old->extension;
1515                decl->uniqueId   = old->uniqueId;
1516                decl->storage    = { old->storageClasses.val };
1517
1518                this->node = decl;
1519        }
1520
1521        virtual void visit( TypeDecl * old ) override final {
1522                if ( inCache( old ) ) return;
1523                auto decl = new ast::TypeDecl{
1524                        old->location,
1525                        old->name,
1526                        { old->storageClasses.val },
1527                        GET_ACCEPT_1(base, Type),
1528                        (ast::TypeVar::Kind)(unsigned)old->kind,
1529                        old->sized,
1530                        GET_ACCEPT_1(init, Type)
1531                };
1532                cache.emplace( old, decl );
1533                decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1534                decl->params     = GET_ACCEPT_V(parameters, TypeDecl);
1535                decl->extension  = old->extension;
1536                decl->uniqueId   = old->uniqueId;
1537
1538                this->node = decl;
1539        }
1540
1541        virtual void visit( TypedefDecl * old ) override final {
1542                auto decl = new ast::TypedefDecl(
1543                        old->location,
1544                        old->name,
1545                        { old->storageClasses.val },
1546                        GET_ACCEPT_1(base, Type),
1547                        { old->linkage.val }
1548                );
1549                decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1550                decl->params     = GET_ACCEPT_V(parameters, TypeDecl);
1551                decl->extension  = old->extension;
1552                decl->uniqueId   = old->uniqueId;
1553                decl->storage    = { old->storageClasses.val };
1554
1555                this->node = decl;
1556        }
1557
1558        virtual void visit( AsmDecl * old ) override final {
1559                auto decl = new ast::AsmDecl{
1560                        old->location,
1561                        GET_ACCEPT_1(stmt, AsmStmt)
1562                };
1563                decl->extension  = old->extension;
1564                decl->uniqueId   = old->uniqueId;
1565                decl->storage    = { old->storageClasses.val };
1566
1567                this->node = decl;
1568        }
1569
1570        virtual void visit( StaticAssertDecl * old ) override final {
1571                auto decl = new ast::StaticAssertDecl{
1572                        old->location,
1573                        GET_ACCEPT_1(condition, Expr),
1574                        GET_ACCEPT_1(message, ConstantExpr)
1575                };
1576                decl->extension  = old->extension;
1577                decl->uniqueId   = old->uniqueId;
1578                decl->storage    = { old->storageClasses.val };
1579
1580                this->node = decl;
1581        }
1582
1583        virtual void visit( CompoundStmt * old ) override final {
1584                if ( inCache( old ) ) return;
1585                auto stmt = new ast::CompoundStmt(
1586                        old->location,
1587                        to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1588                        GET_LABELS_V(old->labels)
1589                );
1590
1591                this->node = stmt;
1592                cache.emplace( old, this->node );
1593        }
1594
1595        virtual void visit( ExprStmt * old ) override final {
1596                if ( inCache( old ) ) return;
1597                this->node = new ast::ExprStmt(
1598                        old->location,
1599                        GET_ACCEPT_1(expr, Expr),
1600                        GET_LABELS_V(old->labels)
1601                );
1602                cache.emplace( old, this->node );
1603        }
1604
1605        virtual void visit( AsmStmt * old ) override final {
1606                if ( inCache( old ) ) return;
1607                this->node = new ast::AsmStmt(
1608                        old->location,
1609                        old->voltile,
1610                        GET_ACCEPT_1(instruction, Expr),
1611                        GET_ACCEPT_V(output, Expr),
1612                        GET_ACCEPT_V(input, Expr),
1613                        GET_ACCEPT_V(clobber, ConstantExpr),
1614                        GET_LABELS_V(old->gotolabels),
1615                        GET_LABELS_V(old->labels)
1616                );
1617                cache.emplace( old, this->node );
1618        }
1619
1620        virtual void visit( DirectiveStmt * old ) override final {
1621                if ( inCache( old ) ) return;
1622                this->node = new ast::DirectiveStmt(
1623                        old->location,
1624                        old->directive,
1625                        GET_LABELS_V(old->labels)
1626                );
1627                cache.emplace( old, this->node );
1628        }
1629
1630        virtual void visit( IfStmt * old ) override final {
1631                if ( inCache( old ) ) return;
1632                this->node = new ast::IfStmt(
1633                        old->location,
1634                        GET_ACCEPT_1(condition, Expr),
1635                        GET_ACCEPT_1(thenPart, Stmt),
1636                        GET_ACCEPT_1(elsePart, Stmt),
1637                        GET_ACCEPT_V(initialization, Stmt),
1638                        GET_LABELS_V(old->labels)
1639                );
1640                cache.emplace( old, this->node );
1641        }
1642
1643        virtual void visit( SwitchStmt * old ) override final {
1644                if ( inCache( old ) ) return;
1645                this->node = new ast::SwitchStmt(
1646                        old->location,
1647                        GET_ACCEPT_1(condition, Expr),
1648                        GET_ACCEPT_V(statements, Stmt),
1649                        GET_LABELS_V(old->labels)
1650                );
1651                cache.emplace( old, this->node );
1652        }
1653
1654        virtual void visit( CaseStmt * old ) override final {
1655                if ( inCache( old ) ) return;
1656                this->node = new ast::CaseStmt(
1657                        old->location,
1658                        GET_ACCEPT_1(condition, Expr),
1659                        GET_ACCEPT_V(stmts, Stmt),
1660                        GET_LABELS_V(old->labels)
1661                );
1662                cache.emplace( old, this->node );
1663        }
1664
1665        virtual void visit( WhileStmt * old ) override final {
1666                if ( inCache( old ) ) return;
1667                this->node = new ast::WhileStmt(
1668                        old->location,
1669                        GET_ACCEPT_1(condition, Expr),
1670                        GET_ACCEPT_1(body, Stmt),
1671                        GET_ACCEPT_V(initialization, Stmt),
1672                        old->isDoWhile,
1673                        GET_LABELS_V(old->labels)
1674                );
1675                cache.emplace( old, this->node );
1676        }
1677
1678        virtual void visit( ForStmt * old ) override final {
1679                if ( inCache( old ) ) return;
1680                this->node = new ast::ForStmt(
1681                        old->location,
1682                        GET_ACCEPT_V(initialization, Stmt),
1683                        GET_ACCEPT_1(condition, Expr),
1684                        GET_ACCEPT_1(increment, Expr),
1685                        GET_ACCEPT_1(body, Stmt),
1686                        GET_LABELS_V(old->labels)
1687                );
1688                cache.emplace( old, this->node );
1689        }
1690
1691        virtual void visit( BranchStmt * old ) override final {
1692                if ( inCache( old ) ) return;
1693                if (old->computedTarget) {
1694                        this->node = new ast::BranchStmt(
1695                                old->location,
1696                                GET_ACCEPT_1(computedTarget, Expr),
1697                                GET_LABELS_V(old->labels)
1698                        );
1699                } else {
1700                        ast::BranchStmt::Kind kind;
1701                        switch (old->type) {
1702                        #define CASE(n) \
1703                        case BranchStmt::n: \
1704                                kind = ast::BranchStmt::n; \
1705                                break
1706                        CASE(Goto);
1707                        CASE(Break);
1708                        CASE(Continue);
1709                        CASE(FallThrough);
1710                        CASE(FallThroughDefault);
1711                        #undef CASE
1712                        default:
1713                                assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1714                        }
1715
1716                        Label label = old->originalTarget;
1717                        auto stmt = new ast::BranchStmt(
1718                                old->location,
1719                                kind,
1720                                make_label(&label),
1721                                GET_LABELS_V(old->labels)
1722                        );
1723                        stmt->target = make_label(&old->target);
1724                        this->node = stmt;
1725                }
1726                cache.emplace( old, this->node );
1727        }
1728
1729        virtual void visit( ReturnStmt * old ) override final {
1730                if ( inCache( old ) ) return;
1731                this->node = new ast::ReturnStmt(
1732                        old->location,
1733                        GET_ACCEPT_1(expr, Expr),
1734                        GET_LABELS_V(old->labels)
1735                );
1736                cache.emplace( old, this->node );
1737        }
1738
1739        virtual void visit( ThrowStmt * old ) override final {
1740                if ( inCache( old ) ) return;
1741                ast::ExceptionKind kind;
1742                switch (old->kind) {
1743                case ThrowStmt::Terminate:
1744                        kind = ast::ExceptionKind::Terminate;
1745                        break;
1746                case ThrowStmt::Resume:
1747                        kind = ast::ExceptionKind::Resume;
1748                        break;
1749                default:
1750                        assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1751                }
1752
1753                this->node = new ast::ThrowStmt(
1754                        old->location,
1755                        kind,
1756                        GET_ACCEPT_1(expr, Expr),
1757                        GET_ACCEPT_1(target, Expr),
1758                        GET_LABELS_V(old->labels)
1759                );
1760                cache.emplace( old, this->node );
1761        }
1762
1763        virtual void visit( TryStmt * old ) override final {
1764                if ( inCache( old ) ) return;
1765                this->node = new ast::TryStmt(
1766                        old->location,
1767                        GET_ACCEPT_1(block, CompoundStmt),
1768                        GET_ACCEPT_V(handlers, CatchStmt),
1769                        GET_ACCEPT_1(finallyBlock, FinallyStmt),
1770                        GET_LABELS_V(old->labels)
1771                );
1772                cache.emplace( old, this->node );
1773        }
1774
1775        virtual void visit( CatchStmt * old ) override final {
1776                if ( inCache( old ) ) return;
1777                ast::ExceptionKind kind;
1778                switch (old->kind) {
1779                case CatchStmt::Terminate:
1780                        kind = ast::ExceptionKind::Terminate;
1781                        break;
1782                case CatchStmt::Resume:
1783                        kind = ast::ExceptionKind::Resume;
1784                        break;
1785                default:
1786                        assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
1787                }
1788
1789                this->node = new ast::CatchStmt(
1790                        old->location,
1791                        kind,
1792                        GET_ACCEPT_1(decl, Decl),
1793                        GET_ACCEPT_1(cond, Expr),
1794                        GET_ACCEPT_1(body, Stmt),
1795                        GET_LABELS_V(old->labels)
1796                );
1797                cache.emplace( old, this->node );
1798        }
1799
1800        virtual void visit( FinallyStmt * old ) override final {
1801                if ( inCache( old ) ) return;
1802                this->node = new ast::FinallyStmt(
1803                        old->location,
1804                        GET_ACCEPT_1(block, CompoundStmt),
1805                        GET_LABELS_V(old->labels)
1806                );
1807                cache.emplace( old, this->node );
1808        }
1809
1810        virtual void visit( WaitForStmt * old ) override final {
1811                if ( inCache( old ) ) return;
1812                ast::WaitForStmt * stmt = new ast::WaitForStmt(
1813                        old->location,
1814                        GET_LABELS_V(old->labels)
1815                );
1816
1817                stmt->clauses.reserve( old->clauses.size() );
1818                for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
1819                        stmt->clauses.push_back({
1820                                ast::WaitForStmt::Target{
1821                                        GET_ACCEPT_1(clauses[i].target.function, Expr),
1822                                        GET_ACCEPT_V(clauses[i].target.arguments, Expr)
1823                                },
1824                                GET_ACCEPT_1(clauses[i].statement, Stmt),
1825                                GET_ACCEPT_1(clauses[i].condition, Expr)
1826                        });
1827                }
1828                stmt->timeout = {
1829                        GET_ACCEPT_1(timeout.time, Expr),
1830                        GET_ACCEPT_1(timeout.statement, Stmt),
1831                        GET_ACCEPT_1(timeout.condition, Expr),
1832                };
1833                stmt->orElse = {
1834                        GET_ACCEPT_1(timeout.statement, Stmt),
1835                        GET_ACCEPT_1(timeout.condition, Expr),
1836                };
1837
1838                this->node = stmt;
1839                cache.emplace( old, this->node );
1840        }
1841
1842        virtual void visit( WithStmt * old ) override final {
1843                if ( inCache( old ) ) return;
1844                this->node = new ast::WithStmt(
1845                        old->location,
1846                        GET_ACCEPT_V(exprs, Expr),
1847                        GET_ACCEPT_1(stmt, Stmt),
1848                        GET_LABELS_V(old->labels)
1849                );
1850                cache.emplace( old, this->node );
1851        }
1852
1853        virtual void visit( NullStmt * old ) override final {
1854                if ( inCache( old ) ) return;
1855                this->node = new ast::NullStmt(
1856                        old->location,
1857                        GET_LABELS_V(old->labels)
1858                );
1859                cache.emplace( old, this->node );
1860        }
1861
1862        virtual void visit( DeclStmt * old ) override final {
1863                if ( inCache( old ) ) return;
1864                this->node = new ast::DeclStmt(
1865                        old->location,
1866                        GET_ACCEPT_1(decl, Decl),
1867                        GET_LABELS_V(old->labels)
1868                );
1869                cache.emplace( old, this->node );
1870        }
1871
1872        virtual void visit( ImplicitCtorDtorStmt * old ) override final {
1873                if ( inCache( old ) ) return;
1874                auto stmt = new ast::ImplicitCtorDtorStmt(
1875                        old->location,
1876                        nullptr,
1877                        GET_LABELS_V(old->labels)
1878                );
1879                this->node = stmt;
1880                cache.emplace( old, this->node );
1881                stmt->callStmt = GET_ACCEPT_1(callStmt, Stmt);
1882        }
1883
1884        ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
1885
1886                if (!old) return nullptr;
1887
1888                ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
1889
1890                for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
1891                        rslt->add( old_i->first,
1892                                   getAccept1<ast::Type>(old_i->second) );
1893                }
1894
1895                for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
1896                        rslt->addVar( old_i->first,
1897                                      getAccept1<ast::Expr>(old_i->second) );
1898                }
1899
1900                return rslt;
1901        }
1902
1903        void convertInferUnion(ast::Expr::InferUnion               &newInferred,
1904                                                   const std::map<UniqueId,ParamEntry> &oldInferParams,
1905                                                   const std::vector<UniqueId>         &oldResnSlots) {
1906
1907                assert( oldInferParams.empty() || oldResnSlots.empty() );
1908                assert( newInferred.mode == ast::Expr::InferUnion::Empty );
1909
1910                if ( !oldInferParams.empty() ) {
1911                        ast::InferredParams &tgt = newInferred.inferParams();
1912                        for (auto old : oldInferParams) {
1913                                tgt[old.first] = ast::ParamEntry(
1914                                        old.second.decl,
1915                                        getAccept1<ast::Type>(old.second.actualType),
1916                                        getAccept1<ast::Type>(old.second.formalType),
1917                                        getAccept1<ast::Expr>(old.second.expr)
1918                                );
1919                        }
1920                } else if ( !oldResnSlots.empty() ) {
1921                        ast::ResnSlots &tgt = newInferred.resnSlots();
1922                        for (auto old : oldResnSlots) {
1923                                tgt.push_back(old);
1924                        }
1925                }
1926        }
1927
1928        ast::Expr * visitBaseExpr_SkipResultType(Expression * old, ast::Expr * nw) {
1929
1930                nw->env    = convertTypeSubstitution(old->env);
1931
1932                nw->extension = old->extension;
1933                convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
1934
1935                return nw;
1936        }
1937
1938        ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
1939
1940                nw->result = GET_ACCEPT_1(result, Type);
1941                return visitBaseExpr_SkipResultType(old, nw);;
1942        }
1943
1944        virtual void visit( ApplicationExpr * old ) override final {
1945                this->node = visitBaseExpr( old,
1946                        new ast::ApplicationExpr(
1947                                old->location,
1948                                GET_ACCEPT_1(function, Expr),
1949                                GET_ACCEPT_V(args, Expr)
1950                        )
1951                );
1952        }
1953
1954        virtual void visit( UntypedExpr * old ) override final {
1955                this->node = visitBaseExpr( old,
1956                        new ast::UntypedExpr(
1957                                old->location,
1958                                GET_ACCEPT_1(function, Expr),
1959                                GET_ACCEPT_V(args, Expr)
1960                        )
1961                );
1962        }
1963
1964        virtual void visit( NameExpr * old ) override final {
1965                this->node = visitBaseExpr( old,
1966                        new ast::NameExpr(
1967                                old->location,
1968                                old->get_name()
1969                        )
1970                );
1971        }
1972
1973        virtual void visit( CastExpr * old ) override final {
1974                this->node = visitBaseExpr( old,
1975                        new ast::CastExpr(
1976                                old->location,
1977                                GET_ACCEPT_1(arg, Expr),
1978                                old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
1979                        )
1980                );
1981        }
1982
1983        virtual void visit( KeywordCastExpr * old) override final {
1984                ast::KeywordCastExpr::Target castTarget = ast::KeywordCastExpr::NUMBER_OF_TARGETS;
1985                switch (old->target) {
1986                        case KeywordCastExpr::Coroutine:
1987                                castTarget = ast::KeywordCastExpr::Coroutine;
1988                                break;
1989                        case KeywordCastExpr::Thread:
1990                                castTarget = ast::KeywordCastExpr::Thread;
1991                                break;
1992                        case KeywordCastExpr::Monitor:
1993                                castTarget = ast::KeywordCastExpr::Monitor;
1994                                break;
1995                        default:
1996                                break;
1997                }
1998                assert ( castTarget < ast::KeywordCastExpr::NUMBER_OF_TARGETS );
1999                this->node = visitBaseExpr( old,
2000                        new ast::KeywordCastExpr(
2001                                old->location,
2002                                GET_ACCEPT_1(arg, Expr),
2003                                castTarget
2004                        )
2005                );
2006        }
2007
2008        virtual void visit( VirtualCastExpr * old ) override final {
2009                this->node = visitBaseExpr_SkipResultType( old,
2010                        new ast::VirtualCastExpr(
2011                                old->location,
2012                                GET_ACCEPT_1(arg, Expr),
2013                                GET_ACCEPT_1(result, Type)
2014                        )
2015                );
2016        }
2017
2018        virtual void visit( AddressExpr * old ) override final {
2019                this->node = visitBaseExpr( old,
2020                        new ast::AddressExpr(
2021                                old->location,
2022                                GET_ACCEPT_1(arg, Expr)
2023                        )
2024                );
2025        }
2026
2027        virtual void visit( LabelAddressExpr * old ) override final {
2028                this->node = visitBaseExpr( old,
2029                        new ast::LabelAddressExpr(
2030                                old->location,
2031                                make_label(&old->arg)
2032                        )
2033                );
2034        }
2035
2036        virtual void visit( UntypedMemberExpr * old ) override final {
2037                this->node = visitBaseExpr( old,
2038                        new ast::UntypedMemberExpr(
2039                                old->location,
2040                                GET_ACCEPT_1(member, Expr),
2041                                GET_ACCEPT_1(aggregate, Expr)
2042                        )
2043                );
2044        }
2045
2046        virtual void visit( MemberExpr * old ) override final {
2047                this->node = visitBaseExpr( old,
2048                        new ast::MemberExpr(
2049                                old->location,
2050                                inCache(old->member) ?
2051                                        dynamic_cast<ast::DeclWithType *>(this->node) :
2052                                        GET_ACCEPT_1(member, DeclWithType),
2053                                GET_ACCEPT_1(aggregate, Expr)
2054                        )
2055                );
2056        }
2057
2058        virtual void visit( VariableExpr * old ) override final {
2059                this->node = visitBaseExpr( old,
2060                        new ast::VariableExpr(
2061                                old->location,
2062                                inCache(old->var) ?
2063                                        dynamic_cast<ast::DeclWithType *>(this->node) :
2064                                        GET_ACCEPT_1(var, DeclWithType)
2065                        )
2066                );
2067        }
2068
2069        bool isIntlikeConstantType(const Type *t) {
2070                if ( const BasicType * basicType = dynamic_cast< const BasicType * >( t ) ) {
2071                        if ( basicType->isInteger() ) {
2072                                return true;
2073                        }
2074                } else if ( dynamic_cast< const OneType * >( t ) ) {
2075                        return true;
2076                } else if ( dynamic_cast< const ZeroType * >( t ) ) {
2077                        return true;
2078                } else if ( dynamic_cast< const PointerType * >( t ) ) {
2079                        // null pointer constants, with zero int-values
2080                        return true;
2081                }
2082                return false;
2083        }
2084
2085        int isFloatlikeConstantType(const Type *t) {
2086                if ( const BasicType * bty = dynamic_cast< const BasicType * >( t ) ) {
2087                        if ( ! bty->isInteger() ) {
2088                                return true;
2089                        }
2090                }
2091                return false;
2092        }
2093
2094        int isStringlikeConstantType(const Type *t) {
2095                if ( const ArrayType * aty = dynamic_cast< const ArrayType * >( t ) ) {
2096                        if ( const BasicType * bty = dynamic_cast< const BasicType * >( aty->base ) ) {
2097                           if ( bty->kind == BasicType::Kind::Char ) {
2098                                   return true;
2099                           }
2100                        }
2101                }
2102                return false;
2103        }
2104
2105        virtual void visit( ConstantExpr * old ) override final {
2106                ast::ConstantExpr *rslt = nullptr;
2107                if (isIntlikeConstantType(old->result)) {
2108                        rslt = new ast::ConstantExpr(
2109                                old->location,
2110                                GET_ACCEPT_1(result, Type),
2111                                old->constant.get_value(),
2112                                (unsigned long long) old->intValue()
2113                        );
2114                } else if (isFloatlikeConstantType(old->result)) {
2115                        rslt = new ast::ConstantExpr(
2116                                old->location,
2117                                GET_ACCEPT_1(result, Type),
2118                                old->constant.get_value(),
2119                                (double) old->constant.get_dval()
2120                        );
2121                } else if (isStringlikeConstantType(old->result)) {
2122                        rslt = ast::ConstantExpr::from_string(
2123                                old->location,
2124                                old->constant.get_value()
2125                        );
2126                }
2127                assert(rslt);
2128                this->node = visitBaseExpr( old, rslt );
2129        }
2130
2131        virtual void visit( SizeofExpr * old ) override final {
2132                assert (old->expr || old->type);
2133                assert (! (old->expr && old->type));
2134                ast::SizeofExpr *rslt;
2135                if (old->expr) {
2136                        assert(!old->isType);
2137                        rslt = new ast::SizeofExpr(
2138                                old->location,
2139                                GET_ACCEPT_1(expr, Expr)
2140                        );
2141                }
2142                if (old->type) {
2143                        assert(old->isType);
2144                        rslt = new ast::SizeofExpr(
2145                                old->location,
2146                                GET_ACCEPT_1(type, Type)
2147                        );
2148                }
2149                this->node = visitBaseExpr( old, rslt );
2150        }
2151
2152        virtual void visit( AlignofExpr * old ) override final {
2153                assert (old->expr || old->type);
2154                assert (! (old->expr && old->type));
2155                ast::AlignofExpr *rslt;
2156                if (old->expr) {
2157                        assert(!old->isType);
2158                        rslt = new ast::AlignofExpr(
2159                                old->location,
2160                                GET_ACCEPT_1(expr, Expr)
2161                        );
2162                }
2163                if (old->type) {
2164                        assert(old->isType);
2165                        rslt = new ast::AlignofExpr(
2166                                old->location,
2167                                GET_ACCEPT_1(type, Type)
2168                        );
2169                }
2170                this->node = visitBaseExpr( old, rslt );
2171        }
2172
2173        virtual void visit( UntypedOffsetofExpr * old ) override final {
2174                this->node = visitBaseExpr( old,
2175                        new ast::UntypedOffsetofExpr(
2176                                old->location,
2177                                GET_ACCEPT_1(type, Type),
2178                                old->member
2179                        )
2180                );
2181        }
2182
2183        virtual void visit( OffsetofExpr * old ) override final {
2184                this->node = visitBaseExpr( old,
2185                        new ast::OffsetofExpr(
2186                                old->location,
2187                                GET_ACCEPT_1(type, Type),
2188                                inCache(old->member) ?
2189                                        dynamic_cast<ast::DeclWithType *>(this->node) :
2190                                        GET_ACCEPT_1(member, DeclWithType)
2191                        )
2192                );
2193        }
2194
2195        virtual void visit( OffsetPackExpr * old ) override final {
2196                this->node = visitBaseExpr( old,
2197                        new ast::OffsetPackExpr(
2198                                old->location,
2199                                GET_ACCEPT_1(type, StructInstType)
2200                        )
2201                );
2202        }
2203
2204        virtual void visit( LogicalExpr * old ) override final {
2205                this->node = visitBaseExpr( old,
2206                        new ast::LogicalExpr(
2207                                old->location,
2208                                GET_ACCEPT_1(arg1, Expr),
2209                                GET_ACCEPT_1(arg2, Expr),
2210                                old->get_isAnd() ?
2211                                        ast::LogicalFlag::AndExpr :
2212                                        ast::LogicalFlag::OrExpr
2213                        )
2214                );
2215        }
2216
2217        virtual void visit( ConditionalExpr * old ) override final {
2218                this->node = visitBaseExpr( old,
2219                        new ast::ConditionalExpr(
2220                                old->location,
2221                                GET_ACCEPT_1(arg1, Expr),
2222                                GET_ACCEPT_1(arg2, Expr),
2223                                GET_ACCEPT_1(arg3, Expr)
2224                        )
2225                );
2226        }
2227
2228        virtual void visit( CommaExpr * old ) override final {
2229                this->node = visitBaseExpr( old,
2230                        new ast::CommaExpr(
2231                                old->location,
2232                                GET_ACCEPT_1(arg1, Expr),
2233                                GET_ACCEPT_1(arg2, Expr)
2234                        )
2235                );
2236        }
2237
2238        virtual void visit( TypeExpr * old ) override final {
2239                this->node = visitBaseExpr( old,
2240                        new ast::TypeExpr(
2241                                old->location,
2242                                GET_ACCEPT_1(type, Type)
2243                        )
2244                );
2245        }
2246
2247        virtual void visit( AsmExpr * old ) override final {
2248                this->node = visitBaseExpr( old,
2249                        new ast::AsmExpr(
2250                                old->location,
2251                                GET_ACCEPT_1(inout, Expr),
2252                                GET_ACCEPT_1(constraint, Expr),
2253                                GET_ACCEPT_1(operand, Expr)
2254                        )
2255                );
2256        }
2257
2258        virtual void visit( ImplicitCopyCtorExpr * old ) override final {
2259                auto rslt = new ast::ImplicitCopyCtorExpr(
2260                        old->location,
2261                        GET_ACCEPT_1(callExpr, ApplicationExpr)
2262                );
2263
2264                rslt->tempDecls = GET_ACCEPT_V(tempDecls, ObjectDecl);
2265                rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2266                rslt->dtors = GET_ACCEPT_V(dtors, Expr);
2267
2268                this->node = visitBaseExpr( old, rslt );
2269        }
2270
2271        virtual void visit( ConstructorExpr * old ) override final {
2272                this->node = visitBaseExpr( old,
2273                        new ast::ConstructorExpr(
2274                                old->location,
2275                                GET_ACCEPT_1(callExpr, Expr)
2276                        )
2277                );
2278        }
2279
2280        virtual void visit( CompoundLiteralExpr * old ) override final {
2281                this->node = visitBaseExpr_SkipResultType( old,
2282                        new ast::CompoundLiteralExpr(
2283                                old->location,
2284                                GET_ACCEPT_1(result, Type),
2285                                GET_ACCEPT_1(initializer, Init)
2286                        )
2287                );
2288        }
2289
2290        virtual void visit( RangeExpr * old ) override final {
2291                this->node = visitBaseExpr( old,
2292                        new ast::RangeExpr(
2293                                old->location,
2294                                GET_ACCEPT_1(low, Expr),
2295                                GET_ACCEPT_1(high, Expr)
2296                        )
2297                );
2298        }
2299
2300        virtual void visit( UntypedTupleExpr * old ) override final {
2301                this->node = visitBaseExpr( old,
2302                        new ast::UntypedTupleExpr(
2303                                old->location,
2304                                GET_ACCEPT_V(exprs, Expr)
2305                        )
2306                );
2307        }
2308
2309        virtual void visit( TupleExpr * old ) override final {
2310                this->node = visitBaseExpr( old,
2311                        new ast::TupleExpr(
2312                                old->location,
2313                                GET_ACCEPT_V(exprs, Expr)
2314                        )
2315                );
2316        }
2317
2318        virtual void visit( TupleIndexExpr * old ) override final {
2319                this->node = visitBaseExpr( old,
2320                        new ast::TupleIndexExpr(
2321                                old->location,
2322                                GET_ACCEPT_1(tuple, Expr),
2323                                old->index
2324                        )
2325                );
2326        }
2327
2328        virtual void visit( TupleAssignExpr * old ) override final {
2329                this->node = visitBaseExpr_SkipResultType( old,
2330                        new ast::TupleAssignExpr(
2331                                old->location,
2332                                GET_ACCEPT_1(result, Type),
2333                                GET_ACCEPT_1(stmtExpr, StmtExpr)
2334                        )
2335                );
2336        }
2337
2338        virtual void visit( StmtExpr * old ) override final {
2339                auto rslt = new ast::StmtExpr(
2340                        old->location,
2341                        GET_ACCEPT_1(statements, CompoundStmt)
2342                );
2343                rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2344                rslt->dtors       = GET_ACCEPT_V(dtors      , Expr);
2345
2346                this->node = visitBaseExpr_SkipResultType( old, rslt );
2347        }
2348
2349        virtual void visit( UniqueExpr * old ) override final {
2350                auto rslt = new ast::UniqueExpr(
2351                        old->location,
2352                        GET_ACCEPT_1(expr, Expr)
2353                );
2354                rslt->object = GET_ACCEPT_1(object, ObjectDecl);
2355                rslt->var    = GET_ACCEPT_1(var   , VariableExpr);
2356
2357                this->node = visitBaseExpr( old, rslt );
2358        }
2359
2360        virtual void visit( UntypedInitExpr * old ) override final {
2361                std::vector<ast::InitAlternative> initAlts;
2362                for (auto ia : old->initAlts) {
2363                        initAlts.push_back(ast::InitAlternative(
2364                                getAccept1< ast::Type, Type * >( ia.type ),
2365                                getAccept1< ast::Designation, Designation * >( ia.designation )
2366                        ));
2367                }
2368                this->node = visitBaseExpr( old,
2369                        new ast::UntypedInitExpr(
2370                                old->location,
2371                                GET_ACCEPT_1(expr, Expr),
2372                                std::move(initAlts)
2373                        )
2374                );
2375        }
2376
2377        virtual void visit( InitExpr * old ) override final {
2378                this->node = visitBaseExpr( old,
2379                        new ast::InitExpr(
2380                                old->location,
2381                                GET_ACCEPT_1(expr, Expr),
2382                                GET_ACCEPT_1(designation, Designation)
2383                        )
2384                );
2385        }
2386
2387        virtual void visit( DeletedExpr * old ) override final {
2388                this->node = visitBaseExpr( old,
2389                        new ast::DeletedExpr(
2390                                old->location,
2391                                GET_ACCEPT_1(expr, Expr),
2392                                inCache(old->deleteStmt) ?
2393                                        this->node :
2394                                        GET_ACCEPT_1(deleteStmt, Node)
2395                        )
2396                );
2397        }
2398
2399        virtual void visit( DefaultArgExpr * old ) override final {
2400                this->node = visitBaseExpr( old,
2401                        new ast::DefaultArgExpr(
2402                                old->location,
2403                                GET_ACCEPT_1(expr, Expr)
2404                        )
2405                );
2406        }
2407
2408        virtual void visit( GenericExpr * old ) override final {
2409                std::vector<ast::GenericExpr::Association> associations;
2410                for (auto association : old->associations) {
2411                        associations.push_back(ast::GenericExpr::Association(
2412                                getAccept1< ast::Type, Type * >( association.type ),
2413                                getAccept1< ast::Expr, Expression * >( association.expr )
2414                        ));
2415                }
2416                this->node = visitBaseExpr( old,
2417                        new ast::GenericExpr(
2418                                old->location,
2419                                GET_ACCEPT_1(control, Expr),
2420                                std::move(associations)
2421                        )
2422                );
2423        }
2424
2425        virtual void visit( VoidType * old ) override final {
2426                this->node = new ast::VoidType{ cv( old ) };
2427        }
2428
2429        virtual void visit( BasicType * old ) override final {
2430                this->node = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2431        }
2432
2433        virtual void visit( PointerType * old ) override final {
2434                this->node = new ast::PointerType{
2435                        GET_ACCEPT_1( base, Type ),
2436                        GET_ACCEPT_1( dimension, Expr ),
2437                        (ast::LengthFlag)old->isVarLen,
2438                        (ast::DimensionFlag)old->isStatic,
2439                        cv( old )
2440                };
2441        }
2442
2443        virtual void visit( ArrayType * old ) override final {
2444                this->node = new ast::ArrayType{
2445                        GET_ACCEPT_1( base, Type ),
2446                        GET_ACCEPT_1( dimension, Expr ),
2447                        (ast::LengthFlag)old->isVarLen,
2448                        (ast::DimensionFlag)old->isStatic,
2449                        cv( old )
2450                };
2451        }
2452
2453        virtual void visit( ReferenceType * old ) override final {
2454                this->node = new ast::ReferenceType{
2455                        GET_ACCEPT_1( base, Type ),
2456                        cv( old )
2457                };
2458        }
2459
2460        virtual void visit( QualifiedType * old ) override final {
2461                this->node = new ast::QualifiedType{
2462                        GET_ACCEPT_1( parent, Type ),
2463                        GET_ACCEPT_1( child, Type ),
2464                        cv( old )
2465                };
2466        }
2467
2468        virtual void visit( FunctionType * old ) override final {
2469                auto ty = new ast::FunctionType {
2470                        (ast::ArgumentFlag)old->isVarArgs,
2471                        cv( old )
2472                };
2473                ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2474                ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2475                ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2476                this->node = ty;
2477        }
2478
2479        void postvisit( ReferenceToType * old, ast::ReferenceToType * ty ) {
2480                ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2481                ty->params = GET_ACCEPT_V( parameters, Expr );
2482                ty->hoistType = old->hoistType;
2483        }
2484
2485        virtual void visit( StructInstType * old ) override final {
2486                ast::StructInstType * ty;
2487                if ( old->baseStruct ) {
2488                        ty = new ast::StructInstType{
2489                                GET_ACCEPT_1( baseStruct, StructDecl ),
2490                                cv( old ),
2491                                GET_ACCEPT_V( attributes, Attribute )
2492                        };
2493                } else {
2494                        ty = new ast::StructInstType{
2495                                old->name,
2496                                cv( old ),
2497                                GET_ACCEPT_V( attributes, Attribute )
2498                        };
2499                }
2500                postvisit( old, ty );
2501                this->node = ty;
2502        }
2503
2504        virtual void visit( UnionInstType * old ) override final {
2505                ast::UnionInstType * ty;
2506                if ( old->baseUnion ) {
2507                        ty = new ast::UnionInstType{
2508                                GET_ACCEPT_1( baseUnion, UnionDecl ),
2509                                cv( old ),
2510                                GET_ACCEPT_V( attributes, Attribute )
2511                        };
2512                } else {
2513                        ty = new ast::UnionInstType{
2514                                old->name,
2515                                cv( old ),
2516                                GET_ACCEPT_V( attributes, Attribute )
2517                        };
2518                }
2519                postvisit( old, ty );
2520                this->node = ty;
2521        }
2522
2523        virtual void visit( EnumInstType * old ) override final {
2524                ast::EnumInstType * ty;
2525                if ( old->baseEnum ) {
2526                        ty = new ast::EnumInstType{
2527                                GET_ACCEPT_1( baseEnum, EnumDecl ),
2528                                cv( old ),
2529                                GET_ACCEPT_V( attributes, Attribute )
2530                        };
2531                } else {
2532                        ty = new ast::EnumInstType{
2533                                old->name,
2534                                cv( old ),
2535                                GET_ACCEPT_V( attributes, Attribute )
2536                        };
2537                }
2538                postvisit( old, ty );
2539                this->node = ty;
2540        }
2541
2542        virtual void visit( TraitInstType * old ) override final {
2543                ast::TraitInstType * ty;
2544                if ( old->baseTrait ) {
2545                        ty = new ast::TraitInstType{
2546                                GET_ACCEPT_1( baseTrait, TraitDecl ),
2547                                cv( old ),
2548                                GET_ACCEPT_V( attributes, Attribute )
2549                        };
2550                } else {
2551                        ty = new ast::TraitInstType{
2552                                old->name,
2553                                cv( old ),
2554                                GET_ACCEPT_V( attributes, Attribute )
2555                        };
2556                }
2557                postvisit( old, ty );
2558                this->node = ty;
2559        }
2560
2561        virtual void visit( TypeInstType * old ) override final {
2562                ast::TypeInstType * ty;
2563                if ( old->baseType ) {
2564                        ty = new ast::TypeInstType{
2565                                old->name,
2566                                GET_ACCEPT_1( baseType, TypeDecl ),
2567                                cv( old ),
2568                                GET_ACCEPT_V( attributes, Attribute )
2569                        };
2570                } else {
2571                        ty = new ast::TypeInstType{
2572                                old->name,
2573                                old->isFtype ? ast::TypeVar::Ftype : ast::TypeVar::Dtype,
2574                                cv( old ),
2575                                GET_ACCEPT_V( attributes, Attribute )
2576                        };
2577                }
2578                postvisit( old, ty );
2579                this->node = ty;
2580        }
2581
2582        virtual void visit( TupleType * old ) override final {
2583                this->node = new ast::TupleType{
2584                        GET_ACCEPT_V( types, Type ),
2585                        // members generated by TupleType c'tor
2586                        cv( old )
2587                };
2588        }
2589
2590        virtual void visit( TypeofType * old ) override final {
2591                this->node = new ast::TypeofType{
2592                        GET_ACCEPT_1( expr, Expr ),
2593                        (ast::TypeofType::Kind)old->is_basetypeof,
2594                        cv( old )
2595                };
2596        }
2597
2598        virtual void visit( AttrType * ) override final {
2599                assertf( false, "AttrType deprecated in new AST." );
2600        }
2601
2602        virtual void visit( VarArgsType * old ) override final {
2603                this->node = new ast::VarArgsType{ cv( old ) };
2604        }
2605
2606        virtual void visit( ZeroType * old ) override final {
2607                this->node = new ast::ZeroType{ cv( old ) };
2608        }
2609
2610        virtual void visit( OneType * old ) override final {
2611                this->node = new ast::OneType{ cv( old ) };
2612        }
2613
2614        virtual void visit( GlobalScopeType * ) override final {
2615                this->node = new ast::GlobalScopeType{};
2616        }
2617
2618        virtual void visit( Designation * old ) override final {
2619                this->node = new ast::Designation(
2620                        old->location,
2621                        GET_ACCEPT_V(designators, Expr)
2622                );
2623        }
2624
2625        virtual void visit( SingleInit * old ) override final {
2626                this->node = new ast::SingleInit(
2627                        old->location,
2628                        GET_ACCEPT_1(value, Expr),
2629                        (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
2630                );
2631        }
2632
2633        virtual void visit( ListInit * old ) override final {
2634                this->node = new ast::ListInit(
2635                        old->location,
2636                        GET_ACCEPT_V(initializers, Init),
2637                        GET_ACCEPT_V(designations, Designation),
2638                        (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
2639                );
2640        }
2641
2642        virtual void visit( ConstructorInit * old ) override final {
2643                this->node = new ast::ConstructorInit(
2644                        old->location,
2645                        GET_ACCEPT_1(ctor, Stmt),
2646                        GET_ACCEPT_1(dtor, Stmt),
2647                        GET_ACCEPT_1(init, Init)
2648                );
2649        }
2650
2651        virtual void visit( Constant * ) override final {
2652                // Handled in visit( ConstantEpxr * ).
2653                // In the new tree, Constant fields are inlined into containing ConstantExpression.
2654                assert( 0 );
2655        }
2656
2657        virtual void visit( Attribute * old ) override final {
2658                this->node = new ast::Attribute(
2659                        old->name,
2660                        GET_ACCEPT_V( parameters, Expr )
2661                );
2662        }
2663
2664        virtual void visit( AttrExpr * ) override final {
2665                assertf( false, "AttrExpr deprecated in new AST." );
2666        }
2667};
2668
2669#undef GET_LABELS_V
2670#undef GET_ACCEPT_V
2671#undef GET_ACCEPT_1
2672
2673std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
2674        ConverterOldToNew c;
2675        std::list< ast::ptr< ast::Decl > > decls;
2676        for(auto d : translationUnit) {
2677                d->accept( c );
2678                decls.emplace_back( c.decl() );
2679        }
2680        deleteAll(translationUnit);
2681        return decls;
2682}
Note: See TracBrowser for help on using the repository browser.