source: src/AST/Convert.cpp @ 3bc59b7

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3bc59b7 was 6a45bd78, checked in by Fangren Yu <f37yu@…>, 4 years ago

cleanup: remove params in TypeDecl? (never used)

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