source: src/AST/Convert.cpp @ ccb8c8a

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

remove ParameterizedType? and put content into FunctionType?

  • Property mode set to 100644
File size: 77.0 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::ptr<ast::Type> sizeType = nullptr;
58const ast::FunctionDecl * dereferenceOperator = nullptr;
59const ast::StructDecl   * dtorStruct = nullptr;
60const ast::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->parameters = get<Expression>().acceptL( old->params );
1220                ty->hoistType = old->hoistType;
1221                return visitType( old, ty );
1222        }
1223
1224        const ast::Type * visit( const ast::StructInstType * node ) override final {
1225                StructInstType * ty;
1226                if ( node->base ) {
1227                        ty = new StructInstType{
1228                                cv( node ),
1229                                get<StructDecl>().accept1( node->base ),
1230                                get<Attribute>().acceptL( node->attributes )
1231                        };
1232                } else {
1233                        ty = new StructInstType{
1234                                cv( node ),
1235                                node->name,
1236                                get<Attribute>().acceptL( node->attributes )
1237                        };
1238                }
1239                return postvisit( node, ty );
1240        }
1241
1242        const ast::Type * visit( const ast::UnionInstType * node ) override final {
1243                UnionInstType * ty;
1244                if ( node->base ) {
1245                        ty = new UnionInstType{
1246                                cv( node ),
1247                                get<UnionDecl>().accept1( node->base ),
1248                                get<Attribute>().acceptL( node->attributes )
1249                        };
1250                } else {
1251                        ty = new UnionInstType{
1252                                cv( node ),
1253                                node->name,
1254                                get<Attribute>().acceptL( node->attributes )
1255                        };
1256                }
1257                return postvisit( node, ty );
1258        }
1259
1260        const ast::Type * visit( const ast::EnumInstType * node ) override final {
1261                EnumInstType * ty;
1262                if ( node->base ) {
1263                        ty = new EnumInstType{
1264                                cv( node ),
1265                                get<EnumDecl>().accept1( node->base ),
1266                                get<Attribute>().acceptL( node->attributes )
1267                        };
1268                } else {
1269                        ty = new EnumInstType{
1270                                cv( node ),
1271                                node->name,
1272                                get<Attribute>().acceptL( node->attributes )
1273                        };
1274                }
1275                return postvisit( node, ty );
1276        }
1277
1278        const ast::Type * visit( const ast::TraitInstType * node ) override final {
1279                TraitInstType * ty;
1280                if ( node->base ) {
1281                        ty = new TraitInstType{
1282                                cv( node ),
1283                                get<TraitDecl>().accept1( node->base ),
1284                                get<Attribute>().acceptL( node->attributes )
1285                        };
1286                } else {
1287                        ty = new TraitInstType{
1288                                cv( node ),
1289                                node->name,
1290                                get<Attribute>().acceptL( node->attributes )
1291                        };
1292                }
1293                return postvisit( node, ty );
1294        }
1295
1296        const ast::Type * visit( const ast::TypeInstType * node ) override final {
1297                TypeInstType * ty;
1298                if ( node->base ) {
1299                        ty = new TypeInstType{
1300                                cv( node ),
1301                                node->name,
1302                                get<TypeDecl>().accept1( node->base ),
1303                                get<Attribute>().acceptL( node->attributes )
1304                        };
1305                } else {
1306                        ty = new TypeInstType{
1307                                cv( node ),
1308                                node->name,
1309                                node->kind == ast::TypeDecl::Ftype,
1310                                get<Attribute>().acceptL( node->attributes )
1311                        };
1312                }
1313                return postvisit( node, ty );
1314        }
1315
1316        const ast::Type * visit( const ast::TupleType * node ) override final {
1317                return visitType( node, new TupleType{
1318                        cv( node ),
1319                        get<Type>().acceptL( node->types )
1320                        // members generated by TupleType c'tor
1321                } );
1322        }
1323
1324        const ast::Type * visit( const ast::TypeofType * node ) override final {
1325                return visitType( node, new TypeofType{
1326                        cv( node ),
1327                        get<Expression>().accept1( node->expr ),
1328                        (bool)node->kind
1329                } );
1330        }
1331
1332        const ast::Type * visit( const ast::VarArgsType * node ) override final {
1333                return visitType( node, new VarArgsType{ cv( node ) } );
1334        }
1335
1336        const ast::Type * visit( const ast::ZeroType * node ) override final {
1337                return visitType( node, new ZeroType{ cv( node ) } );
1338        }
1339
1340        const ast::Type * visit( const ast::OneType * node ) override final {
1341                return visitType( node, new OneType{ cv( node ) } );
1342        }
1343
1344        const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
1345                return visitType( node, new GlobalScopeType{} );
1346        }
1347
1348        const ast::Designation * visit( const ast::Designation * node ) override final {
1349                auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
1350                designation->location = node->location;
1351                this->node = designation;
1352                return nullptr;
1353        }
1354
1355        const ast::Init * visit( const ast::SingleInit * node ) override final {
1356                auto init = new SingleInit(
1357                        get<Expression>().accept1( node->value ),
1358                        ast::MaybeConstruct == node->maybeConstructed
1359                );
1360                init->location = node->location;
1361                this->node = init;
1362                return nullptr;
1363        }
1364
1365        const ast::Init * visit( const ast::ListInit * node ) override final {
1366                auto init = new ListInit(
1367                        get<Initializer>().acceptL( node->initializers ),
1368                        get<Designation>().acceptL( node->designations ),
1369                        ast::MaybeConstruct == node->maybeConstructed
1370                );
1371                init->location = node->location;
1372                this->node = init;
1373                return nullptr;
1374        }
1375
1376        const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1377                auto init = new ConstructorInit(
1378                        get<Statement>().accept1( node->ctor ),
1379                        get<Statement>().accept1( node->dtor ),
1380                        get<Initializer>().accept1( node->init )
1381                );
1382                init->location = node->location;
1383                this->node = init;
1384                return nullptr;
1385        }
1386
1387        const ast::Attribute * visit( const ast::Attribute * node ) override final {
1388                auto attr = new Attribute(
1389                        node->name,
1390                        get<Expression>().acceptL(node->params)
1391                );
1392                this->node = attr;
1393                return nullptr;
1394        }
1395
1396        const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1397                // Handled by convertTypeSubstitution helper instead.
1398                // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
1399                assert( 0 );
1400                (void)node;
1401                return nullptr;
1402        }
1403};
1404
1405std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ) {
1406        ConverterNewToOld c;
1407        std::list< Declaration * > decls;
1408        for(auto d : translationUnit.decls) {
1409                decls.emplace_back( c.decl( d ) );
1410        }
1411        return decls;
1412}
1413
1414//================================================================================================
1415
1416class ConverterOldToNew : public Visitor {
1417public:
1418        ast::Decl * decl() {
1419                return strict_dynamic_cast< ast::Decl * >( node );
1420        }
1421
1422        ConverterOldToNew() = default;
1423        ConverterOldToNew(const ConverterOldToNew &) = delete;
1424        ConverterOldToNew(ConverterOldToNew &&) = delete;
1425private:
1426        /// conversion output
1427        ast::Node * node = nullptr;
1428        /// cache of nodes that might be referenced by readonly<> for de-duplication
1429        /// in case that some nodes are dropped by conversion (due to possible structural change)
1430        /// use smart pointers in cache value to prevent accidental invalidation.
1431        /// at conversion stage, all created nodes are guaranteed to be unique, therefore
1432        /// const_casting out of smart pointers is permitted.
1433        std::unordered_map< const BaseSyntaxNode *, ast::ptr<ast::Node> > cache = {};
1434
1435        // Local Utilities:
1436
1437        template<typename NewT, typename OldT>
1438        NewT * getAccept1( OldT old ) {
1439                if ( ! old ) return nullptr;
1440                old->accept(*this);
1441                ast::Node * ret = node;
1442                node = nullptr;
1443                return strict_dynamic_cast< NewT * >( ret );
1444        }
1445
1446#       define GET_ACCEPT_1(child, type) \
1447                getAccept1< ast::type, decltype( old->child ) >( old->child )
1448
1449        template<typename NewT, typename OldC>
1450        std::vector< ast::ptr<NewT> > getAcceptV( const OldC& old ) {
1451                std::vector< ast::ptr<NewT> > ret;
1452                ret.reserve( old.size() );
1453                for ( auto a : old ) {
1454                        a->accept( *this );
1455                        ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1456                        node = nullptr;
1457                }
1458                return ret;
1459        }
1460
1461#       define GET_ACCEPT_V(child, type) \
1462                getAcceptV< ast::type, decltype( old->child ) >( old->child )
1463
1464        template<typename NewT, typename OldC>
1465        std::deque< ast::ptr<NewT> > getAcceptD( const OldC& old ) {
1466                std::deque< ast::ptr<NewT> > ret;
1467                for ( auto a : old ) {
1468                        a->accept( *this );
1469                        ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1470                        node = nullptr;
1471                }
1472                return ret;
1473        }
1474
1475#       define GET_ACCEPT_D(child, type) \
1476                getAcceptD< ast::type, decltype( old->child ) >( old->child )
1477
1478        ast::Label make_label(const Label* old) {
1479                CodeLocation const & location =
1480                    ( old->labelled ) ? old->labelled->location : CodeLocation();
1481                return ast::Label(
1482                        location,
1483                        old->name,
1484                        GET_ACCEPT_V(attributes, Attribute)
1485                );
1486        }
1487
1488        template<template <class...> class C>
1489        C<ast::Label> make_labels(C<Label> olds) {
1490                C<ast::Label> ret;
1491                for (auto oldn : olds) {
1492                        ret.push_back( make_label( &oldn ) );
1493                }
1494                return ret;
1495        }
1496
1497#       define GET_LABELS_V(labels) \
1498                to<std::vector>::from( make_labels( std::move( labels ) ) )
1499
1500        static ast::CV::Qualifiers cv( const Type * ty ) { return { ty->tq.val }; }
1501
1502        /// returns true and sets `node` if in cache
1503        bool inCache( const BaseSyntaxNode * old ) {
1504                auto it = cache.find( old );
1505                if ( it == cache.end() ) return false;
1506                node = const_cast<ast::Node *>(it->second.get());
1507                return true;
1508        }
1509
1510        // Now all the visit functions:
1511
1512        virtual void visit( const ObjectDecl * old ) override final {
1513                auto&& type = GET_ACCEPT_1(type, Type);
1514                auto&& init = GET_ACCEPT_1(init, Init);
1515                auto&& bfwd = GET_ACCEPT_1(bitfieldWidth, Expr);
1516                auto&& attr = GET_ACCEPT_V(attributes, Attribute);
1517                if ( inCache( old ) ) {
1518                        return;
1519                }
1520                auto decl = new ast::ObjectDecl(
1521                        old->location,
1522                        old->name,
1523                        type,
1524                        init,
1525                        { old->get_storageClasses().val },
1526                        { old->linkage.val },
1527                        bfwd,
1528                        std::move(attr),
1529                        { old->get_funcSpec().val }
1530                );
1531                cache.emplace(old, decl);
1532                assert(cache.find( old ) != cache.end());
1533                decl->scopeLevel = old->scopeLevel;
1534                decl->mangleName = old->mangleName;
1535                decl->isDeleted  = old->isDeleted;
1536                decl->asmName    = GET_ACCEPT_1(asmName, Expr);
1537                decl->uniqueId   = old->uniqueId;
1538                decl->extension  = old->extension;
1539
1540                this->node = decl;
1541        }
1542
1543        virtual void visit( const FunctionDecl * old ) override final {
1544                if ( inCache( old ) ) return;
1545                auto paramVars = GET_ACCEPT_V(type->parameters, DeclWithType);
1546                auto returnVars = GET_ACCEPT_V(type->returnVals, DeclWithType);
1547                auto forall = GET_ACCEPT_V(type->forall, TypeDecl);
1548
1549                // function type is now derived from parameter decls instead of storing them
1550
1551                /*
1552                auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
1553                ftype->params.reserve(paramVars.size());
1554                ftype->returns.reserve(returnVars.size());
1555
1556                for (auto & v: paramVars) {
1557                        ftype->params.emplace_back(v->get_type());
1558                }
1559                for (auto & v: returnVars) {
1560                        ftype->returns.emplace_back(v->get_type());
1561                }
1562                ftype->forall = std::move(forall);
1563                */
1564
1565                // can function type have attributes? seems not to be the case.
1566                // visitType(old->type, ftype);
1567
1568                auto decl = new ast::FunctionDecl{
1569                        old->location,
1570                        old->name,
1571                        // GET_ACCEPT_1(type, FunctionType),
1572                        std::move(forall),
1573                        std::move(paramVars),
1574                        std::move(returnVars),
1575                        {},
1576                        { old->storageClasses.val },
1577                        { old->linkage.val },
1578                        GET_ACCEPT_V(attributes, Attribute),
1579                        { old->get_funcSpec().val },
1580                        old->type->isVarArgs
1581                };
1582
1583                // decl->type = ftype;
1584                cache.emplace( old, decl );
1585
1586                decl->withExprs = GET_ACCEPT_V(withExprs, Expr);
1587                decl->stmts = GET_ACCEPT_1(statements, CompoundStmt);
1588                decl->scopeLevel = old->scopeLevel;
1589                decl->mangleName = old->mangleName;
1590                decl->isDeleted  = old->isDeleted;
1591                decl->asmName    = GET_ACCEPT_1(asmName, Expr);
1592                decl->uniqueId   = old->uniqueId;
1593                decl->extension  = old->extension;
1594
1595                this->node = decl;
1596
1597                if ( Validate::dereferenceOperator == old ) {
1598                        ast::dereferenceOperator = decl;
1599                }
1600
1601                if ( Validate::dtorStructDestroy == old ) {
1602                        ast::dtorStructDestroy = decl;
1603                }
1604        }
1605
1606        virtual void visit( const StructDecl * old ) override final {
1607                if ( inCache( old ) ) return;
1608                auto decl = new ast::StructDecl(
1609                        old->location,
1610                        old->name,
1611                        (ast::AggregateDecl::Aggregate)old->kind,
1612                        GET_ACCEPT_V(attributes, Attribute),
1613                        { old->linkage.val }
1614                );
1615                cache.emplace( old, decl );
1616                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1617                decl->body   = old->body;
1618                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1619                decl->members    = GET_ACCEPT_V(members, Decl);
1620                decl->extension  = old->extension;
1621                decl->uniqueId   = old->uniqueId;
1622                decl->storage    = { old->storageClasses.val };
1623
1624                this->node = decl;
1625
1626                if ( Validate::dtorStruct == old ) {
1627                        ast::dtorStruct = decl;
1628                }
1629        }
1630
1631        virtual void visit( const UnionDecl * old ) override final {
1632                if ( inCache( old ) ) return;
1633                auto decl = new ast::UnionDecl(
1634                        old->location,
1635                        old->name,
1636                        GET_ACCEPT_V(attributes, Attribute),
1637                        { old->linkage.val }
1638                );
1639                cache.emplace( old, decl );
1640                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1641                decl->body   = old->body;
1642                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1643                decl->members    = GET_ACCEPT_V(members, Decl);
1644                decl->extension  = old->extension;
1645                decl->uniqueId   = old->uniqueId;
1646                decl->storage    = { old->storageClasses.val };
1647
1648                this->node = decl;
1649        }
1650
1651        virtual void visit( const EnumDecl * old ) override final {
1652                if ( inCache( old ) ) return;
1653                auto decl = new ast::EnumDecl(
1654                        old->location,
1655                        old->name,
1656                        GET_ACCEPT_V(attributes, Attribute),
1657                        { old->linkage.val }
1658                );
1659                cache.emplace( old, decl );
1660                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1661                decl->body   = old->body;
1662                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1663                decl->members    = GET_ACCEPT_V(members, Decl);
1664                decl->extension  = old->extension;
1665                decl->uniqueId   = old->uniqueId;
1666                decl->storage    = { old->storageClasses.val };
1667
1668                this->node = decl;
1669        }
1670
1671        virtual void visit( const TraitDecl * old ) override final {
1672                if ( inCache( old ) ) return;
1673                auto decl = new ast::TraitDecl(
1674                        old->location,
1675                        old->name,
1676                        GET_ACCEPT_V(attributes, Attribute),
1677                        { old->linkage.val }
1678                );
1679                cache.emplace( old, decl );
1680                decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1681                decl->body   = old->body;
1682                decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1683                decl->members    = GET_ACCEPT_V(members, Decl);
1684                decl->extension  = old->extension;
1685                decl->uniqueId   = old->uniqueId;
1686                decl->storage    = { old->storageClasses.val };
1687
1688                this->node = decl;
1689        }
1690
1691        virtual void visit( const TypeDecl * old ) override final {
1692                if ( inCache( old ) ) return;
1693                auto decl = new ast::TypeDecl{
1694                        old->location,
1695                        old->name,
1696                        { old->storageClasses.val },
1697                        GET_ACCEPT_1(base, Type),
1698                        (ast::TypeDecl::Kind)(unsigned)old->kind,
1699                        old->sized,
1700                        GET_ACCEPT_1(init, Type)
1701                };
1702                cache.emplace( old, decl );
1703                decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1704                decl->extension  = old->extension;
1705                decl->uniqueId   = old->uniqueId;
1706
1707                this->node = decl;
1708        }
1709
1710        virtual void visit( const TypedefDecl * old ) override final {
1711                auto decl = new ast::TypedefDecl(
1712                        old->location,
1713                        old->name,
1714                        { old->storageClasses.val },
1715                        GET_ACCEPT_1(base, Type),
1716                        { old->linkage.val }
1717                );
1718                decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1719                decl->extension  = old->extension;
1720                decl->uniqueId   = old->uniqueId;
1721                decl->storage    = { old->storageClasses.val };
1722
1723                this->node = decl;
1724        }
1725
1726        virtual void visit( const AsmDecl * old ) override final {
1727                auto decl = new ast::AsmDecl{
1728                        old->location,
1729                        GET_ACCEPT_1(stmt, AsmStmt)
1730                };
1731                decl->extension  = old->extension;
1732                decl->uniqueId   = old->uniqueId;
1733                decl->storage    = { old->storageClasses.val };
1734
1735                this->node = decl;
1736        }
1737
1738        virtual void visit( const StaticAssertDecl * old ) override final {
1739                auto decl = new ast::StaticAssertDecl{
1740                        old->location,
1741                        GET_ACCEPT_1(condition, Expr),
1742                        GET_ACCEPT_1(message, ConstantExpr)
1743                };
1744                decl->extension  = old->extension;
1745                decl->uniqueId   = old->uniqueId;
1746                decl->storage    = { old->storageClasses.val };
1747
1748                this->node = decl;
1749        }
1750
1751        virtual void visit( const CompoundStmt * old ) override final {
1752                if ( inCache( old ) ) return;
1753                auto stmt = new ast::CompoundStmt(
1754                        old->location,
1755                        to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1756                        GET_LABELS_V(old->labels)
1757                );
1758
1759                this->node = stmt;
1760                cache.emplace( old, this->node );
1761        }
1762
1763        virtual void visit( const ExprStmt * old ) override final {
1764                if ( inCache( old ) ) return;
1765                this->node = new ast::ExprStmt(
1766                        old->location,
1767                        GET_ACCEPT_1(expr, Expr),
1768                        GET_LABELS_V(old->labels)
1769                );
1770                cache.emplace( old, this->node );
1771        }
1772
1773        virtual void visit( const AsmStmt * old ) override final {
1774                if ( inCache( old ) ) return;
1775                this->node = new ast::AsmStmt(
1776                        old->location,
1777                        old->voltile,
1778                        GET_ACCEPT_1(instruction, Expr),
1779                        GET_ACCEPT_V(output, Expr),
1780                        GET_ACCEPT_V(input, Expr),
1781                        GET_ACCEPT_V(clobber, ConstantExpr),
1782                        GET_LABELS_V(old->gotolabels),
1783                        GET_LABELS_V(old->labels)
1784                );
1785                cache.emplace( old, this->node );
1786        }
1787
1788        virtual void visit( const DirectiveStmt * old ) override final {
1789                if ( inCache( old ) ) return;
1790                this->node = new ast::DirectiveStmt(
1791                        old->location,
1792                        old->directive,
1793                        GET_LABELS_V(old->labels)
1794                );
1795                cache.emplace( old, this->node );
1796        }
1797
1798        virtual void visit( const IfStmt * old ) override final {
1799                if ( inCache( old ) ) return;
1800                this->node = new ast::IfStmt(
1801                        old->location,
1802                        GET_ACCEPT_1(condition, Expr),
1803                        GET_ACCEPT_1(thenPart, Stmt),
1804                        GET_ACCEPT_1(elsePart, Stmt),
1805                        GET_ACCEPT_V(initialization, Stmt),
1806                        GET_LABELS_V(old->labels)
1807                );
1808                cache.emplace( old, this->node );
1809        }
1810
1811        virtual void visit( const SwitchStmt * old ) override final {
1812                if ( inCache( old ) ) return;
1813                this->node = new ast::SwitchStmt(
1814                        old->location,
1815                        GET_ACCEPT_1(condition, Expr),
1816                        GET_ACCEPT_V(statements, Stmt),
1817                        GET_LABELS_V(old->labels)
1818                );
1819                cache.emplace( old, this->node );
1820        }
1821
1822        virtual void visit( const CaseStmt * old ) override final {
1823                if ( inCache( old ) ) return;
1824                this->node = new ast::CaseStmt(
1825                        old->location,
1826                        GET_ACCEPT_1(condition, Expr),
1827                        GET_ACCEPT_V(stmts, Stmt),
1828                        GET_LABELS_V(old->labels)
1829                );
1830                cache.emplace( old, this->node );
1831        }
1832
1833        virtual void visit( const WhileStmt * old ) override final {
1834                if ( inCache( old ) ) return;
1835                this->node = new ast::WhileStmt(
1836                        old->location,
1837                        GET_ACCEPT_1(condition, Expr),
1838                        GET_ACCEPT_1(body, Stmt),
1839                        GET_ACCEPT_V(initialization, Stmt),
1840                        old->isDoWhile,
1841                        GET_LABELS_V(old->labels)
1842                );
1843                cache.emplace( old, this->node );
1844        }
1845
1846        virtual void visit( const ForStmt * old ) override final {
1847                if ( inCache( old ) ) return;
1848                this->node = new ast::ForStmt(
1849                        old->location,
1850                        GET_ACCEPT_V(initialization, Stmt),
1851                        GET_ACCEPT_1(condition, Expr),
1852                        GET_ACCEPT_1(increment, Expr),
1853                        GET_ACCEPT_1(body, Stmt),
1854                        GET_LABELS_V(old->labels)
1855                );
1856                cache.emplace( old, this->node );
1857        }
1858
1859        virtual void visit( const BranchStmt * old ) override final {
1860                if ( inCache( old ) ) return;
1861                if (old->computedTarget) {
1862                        this->node = new ast::BranchStmt(
1863                                old->location,
1864                                GET_ACCEPT_1(computedTarget, Expr),
1865                                GET_LABELS_V(old->labels)
1866                        );
1867                } else {
1868                        ast::BranchStmt::Kind kind;
1869                        switch (old->type) {
1870                        #define CASE(n) \
1871                        case BranchStmt::n: \
1872                                kind = ast::BranchStmt::n; \
1873                                break
1874                        CASE(Goto);
1875                        CASE(Break);
1876                        CASE(Continue);
1877                        CASE(FallThrough);
1878                        CASE(FallThroughDefault);
1879                        #undef CASE
1880                        default:
1881                                assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1882                        }
1883
1884                        auto stmt = new ast::BranchStmt(
1885                                old->location,
1886                                kind,
1887                                make_label(&old->originalTarget),
1888                                GET_LABELS_V(old->labels)
1889                        );
1890                        stmt->target = make_label(&old->target);
1891                        this->node = stmt;
1892                }
1893                cache.emplace( old, this->node );
1894        }
1895
1896        virtual void visit( const ReturnStmt * old ) override final {
1897                if ( inCache( old ) ) return;
1898                this->node = new ast::ReturnStmt(
1899                        old->location,
1900                        GET_ACCEPT_1(expr, Expr),
1901                        GET_LABELS_V(old->labels)
1902                );
1903                cache.emplace( old, this->node );
1904        }
1905
1906        virtual void visit( const ThrowStmt * old ) override final {
1907                if ( inCache( old ) ) return;
1908                ast::ExceptionKind kind;
1909                switch (old->kind) {
1910                case ThrowStmt::Terminate:
1911                        kind = ast::ExceptionKind::Terminate;
1912                        break;
1913                case ThrowStmt::Resume:
1914                        kind = ast::ExceptionKind::Resume;
1915                        break;
1916                default:
1917                        assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1918                }
1919
1920                this->node = new ast::ThrowStmt(
1921                        old->location,
1922                        kind,
1923                        GET_ACCEPT_1(expr, Expr),
1924                        GET_ACCEPT_1(target, Expr),
1925                        GET_LABELS_V(old->labels)
1926                );
1927                cache.emplace( old, this->node );
1928        }
1929
1930        virtual void visit( const TryStmt * old ) override final {
1931                if ( inCache( old ) ) return;
1932                this->node = new ast::TryStmt(
1933                        old->location,
1934                        GET_ACCEPT_1(block, CompoundStmt),
1935                        GET_ACCEPT_V(handlers, CatchStmt),
1936                        GET_ACCEPT_1(finallyBlock, FinallyStmt),
1937                        GET_LABELS_V(old->labels)
1938                );
1939                cache.emplace( old, this->node );
1940        }
1941
1942        virtual void visit( const CatchStmt * old ) override final {
1943                if ( inCache( old ) ) return;
1944                ast::ExceptionKind kind;
1945                switch (old->kind) {
1946                case CatchStmt::Terminate:
1947                        kind = ast::ExceptionKind::Terminate;
1948                        break;
1949                case CatchStmt::Resume:
1950                        kind = ast::ExceptionKind::Resume;
1951                        break;
1952                default:
1953                        assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
1954                }
1955
1956                this->node = new ast::CatchStmt(
1957                        old->location,
1958                        kind,
1959                        GET_ACCEPT_1(decl, Decl),
1960                        GET_ACCEPT_1(cond, Expr),
1961                        GET_ACCEPT_1(body, Stmt),
1962                        GET_LABELS_V(old->labels)
1963                );
1964                cache.emplace( old, this->node );
1965        }
1966
1967        virtual void visit( const FinallyStmt * old ) override final {
1968                if ( inCache( old ) ) return;
1969                this->node = new ast::FinallyStmt(
1970                        old->location,
1971                        GET_ACCEPT_1(block, CompoundStmt),
1972                        GET_LABELS_V(old->labels)
1973                );
1974                cache.emplace( old, this->node );
1975        }
1976
1977        virtual void visit( const SuspendStmt * old ) override final {
1978                if ( inCache( old ) ) return;
1979                ast::SuspendStmt::Type type;
1980                switch (old->type) {
1981                        case SuspendStmt::Coroutine: type = ast::SuspendStmt::Coroutine; break;
1982                        case SuspendStmt::Generator: type = ast::SuspendStmt::Generator; break;
1983                        case SuspendStmt::None     : type = ast::SuspendStmt::None     ; break;
1984                        default: abort();
1985                }
1986                this->node = new ast::SuspendStmt(
1987                        old->location,
1988                        GET_ACCEPT_1(then  , CompoundStmt),
1989                        type,
1990                        GET_LABELS_V(old->labels)
1991                );
1992                cache.emplace( old, this->node );
1993        }
1994
1995        virtual void visit( const WaitForStmt * old ) override final {
1996                if ( inCache( old ) ) return;
1997                ast::WaitForStmt * stmt = new ast::WaitForStmt(
1998                        old->location,
1999                        GET_LABELS_V(old->labels)
2000                );
2001
2002                stmt->clauses.reserve( old->clauses.size() );
2003                for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
2004                        stmt->clauses.push_back({
2005                                ast::WaitForStmt::Target{
2006                                        GET_ACCEPT_1(clauses[i].target.function, Expr),
2007                                        GET_ACCEPT_V(clauses[i].target.arguments, Expr)
2008                                },
2009                                GET_ACCEPT_1(clauses[i].statement, Stmt),
2010                                GET_ACCEPT_1(clauses[i].condition, Expr)
2011                        });
2012                }
2013                stmt->timeout = {
2014                        GET_ACCEPT_1(timeout.time, Expr),
2015                        GET_ACCEPT_1(timeout.statement, Stmt),
2016                        GET_ACCEPT_1(timeout.condition, Expr),
2017                };
2018                stmt->orElse = {
2019                        GET_ACCEPT_1(orelse.statement, Stmt),
2020                        GET_ACCEPT_1(orelse.condition, Expr),
2021                };
2022
2023                this->node = stmt;
2024                cache.emplace( old, this->node );
2025        }
2026
2027        virtual void visit( const WithStmt * old ) override final {
2028                if ( inCache( old ) ) return;
2029                this->node = new ast::WithStmt(
2030                        old->location,
2031                        GET_ACCEPT_V(exprs, Expr),
2032                        GET_ACCEPT_1(stmt, Stmt)
2033                );
2034                cache.emplace( old, this->node );
2035        }
2036
2037        virtual void visit( const NullStmt * old ) override final {
2038                if ( inCache( old ) ) return;
2039                this->node = new ast::NullStmt(
2040                        old->location,
2041                        GET_LABELS_V(old->labels)
2042                );
2043                cache.emplace( old, this->node );
2044        }
2045
2046        virtual void visit( const DeclStmt * old ) override final {
2047                if ( inCache( old ) ) return;
2048                this->node = new ast::DeclStmt(
2049                        old->location,
2050                        GET_ACCEPT_1(decl, Decl),
2051                        GET_LABELS_V(old->labels)
2052                );
2053                cache.emplace( old, this->node );
2054        }
2055
2056        virtual void visit( const ImplicitCtorDtorStmt * old ) override final {
2057                if ( inCache( old ) ) return;
2058                auto stmt = new ast::ImplicitCtorDtorStmt(
2059                        old->location,
2060                        nullptr,
2061                        GET_LABELS_V(old->labels)
2062                );
2063                cache.emplace( old, stmt );
2064                stmt->callStmt = GET_ACCEPT_1(callStmt, Stmt);
2065                this->node = stmt;
2066        }
2067
2068        ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
2069
2070                if (!old) return nullptr;
2071
2072                ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
2073
2074                for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
2075                        rslt->add( old_i->first,
2076                                   getAccept1<ast::Type>(old_i->second) );
2077                }
2078
2079                for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
2080                        rslt->addVar( old_i->first,
2081                                      getAccept1<ast::Expr>(old_i->second) );
2082                }
2083
2084                return rslt;
2085        }
2086
2087        void convertInferUnion(ast::Expr::InferUnion               &newInferred,
2088                                                   const std::map<UniqueId,ParamEntry> &oldInferParams,
2089                                                   const std::vector<UniqueId>         &oldResnSlots) {
2090
2091                assert( oldInferParams.empty() || oldResnSlots.empty() );
2092                // assert( newInferred.mode == ast::Expr::InferUnion::Empty );
2093
2094                if ( !oldInferParams.empty() ) {
2095                        ast::InferredParams &tgt = newInferred.inferParams();
2096                        for (auto & old : oldInferParams) {
2097                                tgt[old.first] = ast::ParamEntry(
2098                                        old.second.decl,
2099                                        getAccept1<ast::Decl>(old.second.declptr),
2100                                        getAccept1<ast::Type>(old.second.actualType),
2101                                        getAccept1<ast::Type>(old.second.formalType),
2102                                        getAccept1<ast::Expr>(old.second.expr)
2103                                );
2104                        }
2105                } else if ( !oldResnSlots.empty() ) {
2106                        ast::ResnSlots &tgt = newInferred.resnSlots();
2107                        for (auto old : oldResnSlots) {
2108                                tgt.push_back(old);
2109                        }
2110                }
2111        }
2112
2113        ast::Expr * visitBaseExpr_SkipResultType( const Expression * old, ast::Expr * nw) {
2114
2115                nw->env    = convertTypeSubstitution(old->env);
2116
2117                nw->extension = old->extension;
2118                convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
2119
2120                return nw;
2121        }
2122
2123        ast::Expr * visitBaseExpr( const Expression * old, ast::Expr * nw) {
2124
2125                nw->result = GET_ACCEPT_1(result, Type);
2126                return visitBaseExpr_SkipResultType(old, nw);;
2127        }
2128
2129        virtual void visit( const ApplicationExpr * old ) override final {
2130                this->node = visitBaseExpr( old,
2131                        new ast::ApplicationExpr(
2132                                old->location,
2133                                GET_ACCEPT_1(function, Expr),
2134                                GET_ACCEPT_V(args, Expr)
2135                        )
2136                );
2137        }
2138
2139        virtual void visit( const UntypedExpr * old ) override final {
2140                this->node = visitBaseExpr( old,
2141                        new ast::UntypedExpr(
2142                                old->location,
2143                                GET_ACCEPT_1(function, Expr),
2144                                GET_ACCEPT_V(args, Expr)
2145                        )
2146                );
2147        }
2148
2149        virtual void visit( const NameExpr * old ) override final {
2150                this->node = visitBaseExpr( old,
2151                        new ast::NameExpr(
2152                                old->location,
2153                                old->get_name()
2154                        )
2155                );
2156        }
2157
2158        virtual void visit( const CastExpr * old ) override final {
2159                this->node = visitBaseExpr( old,
2160                        new ast::CastExpr(
2161                                old->location,
2162                                GET_ACCEPT_1(arg, Expr),
2163                                old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
2164                        )
2165                );
2166        }
2167
2168        virtual void visit( const KeywordCastExpr * old ) override final {
2169                ast::AggregateDecl::Aggregate castTarget = (ast::AggregateDecl::Aggregate)old->target;
2170                assert( ast::AggregateDecl::Generator <= castTarget && castTarget <= ast::AggregateDecl::Thread );
2171                this->node = visitBaseExpr( old,
2172                        new ast::KeywordCastExpr(
2173                                old->location,
2174                                GET_ACCEPT_1(arg, Expr),
2175                                castTarget,
2176                                {old->concrete_target.field, old->concrete_target.getter}
2177                        )
2178                );
2179        }
2180
2181        virtual void visit( const VirtualCastExpr * old ) override final {
2182                this->node = visitBaseExpr_SkipResultType( old,
2183                        new ast::VirtualCastExpr(
2184                                old->location,
2185                                GET_ACCEPT_1(arg, Expr),
2186                                GET_ACCEPT_1(result, Type)
2187                        )
2188                );
2189        }
2190
2191        virtual void visit( const AddressExpr * old ) override final {
2192                this->node = visitBaseExpr( old,
2193                        new ast::AddressExpr(
2194                                old->location,
2195                                GET_ACCEPT_1(arg, Expr)
2196                        )
2197                );
2198        }
2199
2200        virtual void visit( const LabelAddressExpr * old ) override final {
2201                this->node = visitBaseExpr( old,
2202                        new ast::LabelAddressExpr(
2203                                old->location,
2204                                make_label(&old->arg)
2205                        )
2206                );
2207        }
2208
2209        virtual void visit( const UntypedMemberExpr * old ) override final {
2210                this->node = visitBaseExpr( old,
2211                        new ast::UntypedMemberExpr(
2212                                old->location,
2213                                GET_ACCEPT_1(member, Expr),
2214                                GET_ACCEPT_1(aggregate, Expr)
2215                        )
2216                );
2217        }
2218
2219        virtual void visit( const MemberExpr * old ) override final {
2220                this->node = visitBaseExpr( old,
2221                        new ast::MemberExpr(
2222                                old->location,
2223                                GET_ACCEPT_1(member, DeclWithType),
2224                                GET_ACCEPT_1(aggregate, Expr),
2225                                ast::MemberExpr::NoOpConstructionChosen
2226                        )
2227                );
2228        }
2229
2230        virtual void visit( const VariableExpr * old ) override final {
2231                auto expr = new ast::VariableExpr(
2232                        old->location
2233                );
2234
2235                expr->var = GET_ACCEPT_1(var, DeclWithType);
2236                visitBaseExpr( old, expr );
2237
2238                this->node = expr;
2239        }
2240
2241        virtual void visit( const ConstantExpr * old ) override final {
2242                ast::ConstantExpr *rslt = new ast::ConstantExpr(
2243                        old->location,
2244                        GET_ACCEPT_1(result, Type),
2245                        old->constant.rep,
2246                        old->constant.ival
2247                );
2248                rslt->underlyer = getAccept1< ast::Type, Type* >( old->constant.type );
2249                this->node = visitBaseExpr( old, rslt );
2250        }
2251
2252        virtual void visit( const SizeofExpr * old ) override final {
2253                assert (old->expr || old->type);
2254                assert (! (old->expr && old->type));
2255                ast::SizeofExpr *rslt;
2256                if (old->expr) {
2257                        assert(!old->isType);
2258                        rslt = new ast::SizeofExpr(
2259                                old->location,
2260                                GET_ACCEPT_1(expr, Expr)
2261                        );
2262                }
2263                if (old->type) {
2264                        assert(old->isType);
2265                        rslt = new ast::SizeofExpr(
2266                                old->location,
2267                                GET_ACCEPT_1(type, Type)
2268                        );
2269                }
2270                this->node = visitBaseExpr( old, rslt );
2271        }
2272
2273        virtual void visit( const AlignofExpr * old ) override final {
2274                assert (old->expr || old->type);
2275                assert (! (old->expr && old->type));
2276                ast::AlignofExpr *rslt;
2277                if (old->expr) {
2278                        assert(!old->isType);
2279                        rslt = new ast::AlignofExpr(
2280                                old->location,
2281                                GET_ACCEPT_1(expr, Expr)
2282                        );
2283                }
2284                if (old->type) {
2285                        assert(old->isType);
2286                        rslt = new ast::AlignofExpr(
2287                                old->location,
2288                                GET_ACCEPT_1(type, Type)
2289                        );
2290                }
2291                this->node = visitBaseExpr( old, rslt );
2292        }
2293
2294        virtual void visit( const UntypedOffsetofExpr * old ) override final {
2295                this->node = visitBaseExpr( old,
2296                        new ast::UntypedOffsetofExpr(
2297                                old->location,
2298                                GET_ACCEPT_1(type, Type),
2299                                old->member
2300                        )
2301                );
2302        }
2303
2304        virtual void visit( const OffsetofExpr * old ) override final {
2305                this->node = visitBaseExpr( old,
2306                        new ast::OffsetofExpr(
2307                                old->location,
2308                                GET_ACCEPT_1(type, Type),
2309                                GET_ACCEPT_1(member, DeclWithType)
2310                        )
2311                );
2312        }
2313
2314        virtual void visit( const OffsetPackExpr * old ) override final {
2315                this->node = visitBaseExpr( old,
2316                        new ast::OffsetPackExpr(
2317                                old->location,
2318                                GET_ACCEPT_1(type, StructInstType)
2319                        )
2320                );
2321        }
2322
2323        virtual void visit( const LogicalExpr * old ) override final {
2324                this->node = visitBaseExpr( old,
2325                        new ast::LogicalExpr(
2326                                old->location,
2327                                GET_ACCEPT_1(arg1, Expr),
2328                                GET_ACCEPT_1(arg2, Expr),
2329                                old->get_isAnd() ?
2330                                        ast::LogicalFlag::AndExpr :
2331                                        ast::LogicalFlag::OrExpr
2332                        )
2333                );
2334        }
2335
2336        virtual void visit( const ConditionalExpr * old ) override final {
2337                this->node = visitBaseExpr( old,
2338                        new ast::ConditionalExpr(
2339                                old->location,
2340                                GET_ACCEPT_1(arg1, Expr),
2341                                GET_ACCEPT_1(arg2, Expr),
2342                                GET_ACCEPT_1(arg3, Expr)
2343                        )
2344                );
2345        }
2346
2347        virtual void visit( const CommaExpr * old ) override final {
2348                this->node = visitBaseExpr( old,
2349                        new ast::CommaExpr(
2350                                old->location,
2351                                GET_ACCEPT_1(arg1, Expr),
2352                                GET_ACCEPT_1(arg2, Expr)
2353                        )
2354                );
2355        }
2356
2357        virtual void visit( const TypeExpr * old ) override final {
2358                this->node = visitBaseExpr( old,
2359                        new ast::TypeExpr(
2360                                old->location,
2361                                GET_ACCEPT_1(type, Type)
2362                        )
2363                );
2364        }
2365
2366        virtual void visit( const AsmExpr * old ) override final {
2367                this->node = visitBaseExpr( old,
2368                        new ast::AsmExpr(
2369                                old->location,
2370                                old->inout,
2371                                GET_ACCEPT_1(constraint, Expr),
2372                                GET_ACCEPT_1(operand, Expr)
2373                        )
2374                );
2375        }
2376
2377        virtual void visit( const ImplicitCopyCtorExpr * old ) override final {
2378                auto rslt = new ast::ImplicitCopyCtorExpr(
2379                        old->location,
2380                        GET_ACCEPT_1(callExpr, ApplicationExpr)
2381                );
2382
2383                this->node = visitBaseExpr( old, rslt );
2384        }
2385
2386        virtual void visit( const ConstructorExpr * old ) override final {
2387                this->node = visitBaseExpr( old,
2388                        new ast::ConstructorExpr(
2389                                old->location,
2390                                GET_ACCEPT_1(callExpr, Expr)
2391                        )
2392                );
2393        }
2394
2395        virtual void visit( const CompoundLiteralExpr * old ) override final {
2396                this->node = visitBaseExpr_SkipResultType( old,
2397                        new ast::CompoundLiteralExpr(
2398                                old->location,
2399                                GET_ACCEPT_1(result, Type),
2400                                GET_ACCEPT_1(initializer, Init)
2401                        )
2402                );
2403        }
2404
2405        virtual void visit( const RangeExpr * old ) override final {
2406                this->node = visitBaseExpr( old,
2407                        new ast::RangeExpr(
2408                                old->location,
2409                                GET_ACCEPT_1(low, Expr),
2410                                GET_ACCEPT_1(high, Expr)
2411                        )
2412                );
2413        }
2414
2415        virtual void visit( const UntypedTupleExpr * old ) override final {
2416                this->node = visitBaseExpr( old,
2417                        new ast::UntypedTupleExpr(
2418                                old->location,
2419                                GET_ACCEPT_V(exprs, Expr)
2420                        )
2421                );
2422        }
2423
2424        virtual void visit( const TupleExpr * old ) override final {
2425                this->node = visitBaseExpr( old,
2426                        new ast::TupleExpr(
2427                                old->location,
2428                                GET_ACCEPT_V(exprs, Expr)
2429                        )
2430                );
2431        }
2432
2433        virtual void visit( const TupleIndexExpr * old ) override final {
2434                this->node = visitBaseExpr( old,
2435                        new ast::TupleIndexExpr(
2436                                old->location,
2437                                GET_ACCEPT_1(tuple, Expr),
2438                                old->index
2439                        )
2440                );
2441        }
2442
2443        virtual void visit( const TupleAssignExpr * old ) override final {
2444                this->node = visitBaseExpr_SkipResultType( old,
2445                        new ast::TupleAssignExpr(
2446                                old->location,
2447                                GET_ACCEPT_1(result, Type),
2448                                GET_ACCEPT_1(stmtExpr, StmtExpr)
2449                        )
2450                );
2451        }
2452
2453        virtual void visit( const StmtExpr * old ) override final {
2454                auto rslt = new ast::StmtExpr(
2455                        old->location,
2456                        GET_ACCEPT_1(statements, CompoundStmt)
2457                );
2458                rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2459                rslt->dtors       = GET_ACCEPT_V(dtors      , Expr);
2460
2461                this->node = visitBaseExpr_SkipResultType( old, rslt );
2462        }
2463
2464        virtual void visit( const UniqueExpr * old ) override final {
2465                auto rslt = new ast::UniqueExpr(
2466                        old->location,
2467                        GET_ACCEPT_1(expr, Expr),
2468                        old->get_id()
2469                );
2470                rslt->object = GET_ACCEPT_1(object, ObjectDecl);
2471                rslt->var    = GET_ACCEPT_1(var   , VariableExpr);
2472
2473                this->node = visitBaseExpr( old, rslt );
2474        }
2475
2476        virtual void visit( const UntypedInitExpr * old ) override final {
2477                std::deque<ast::InitAlternative> initAlts;
2478                for (auto ia : old->initAlts) {
2479                        initAlts.push_back(ast::InitAlternative(
2480                                getAccept1< ast::Type, Type * >( ia.type ),
2481                                getAccept1< ast::Designation, Designation * >( ia.designation )
2482                        ));
2483                }
2484                this->node = visitBaseExpr( old,
2485                        new ast::UntypedInitExpr(
2486                                old->location,
2487                                GET_ACCEPT_1(expr, Expr),
2488                                std::move(initAlts)
2489                        )
2490                );
2491        }
2492
2493        virtual void visit( const InitExpr * old ) override final {
2494                this->node = visitBaseExpr( old,
2495                        new ast::InitExpr(
2496                                old->location,
2497                                GET_ACCEPT_1(expr, Expr),
2498                                GET_ACCEPT_1(designation, Designation)
2499                        )
2500                );
2501        }
2502
2503        virtual void visit( const DeletedExpr * old ) override final {
2504                this->node = visitBaseExpr( old,
2505                        new ast::DeletedExpr(
2506                                old->location,
2507                                GET_ACCEPT_1(expr, Expr),
2508                                inCache(old->deleteStmt) ?
2509                                        strict_dynamic_cast<ast::Decl*>(this->node) :
2510                                        GET_ACCEPT_1(deleteStmt, Decl)
2511                        )
2512                );
2513        }
2514
2515        virtual void visit( const DefaultArgExpr * old ) override final {
2516                this->node = visitBaseExpr( old,
2517                        new ast::DefaultArgExpr(
2518                                old->location,
2519                                GET_ACCEPT_1(expr, Expr)
2520                        )
2521                );
2522        }
2523
2524        virtual void visit( const GenericExpr * old ) override final {
2525                std::vector<ast::GenericExpr::Association> associations;
2526                for (auto association : old->associations) {
2527                        associations.push_back(ast::GenericExpr::Association(
2528                                getAccept1< ast::Type, Type * >( association.type ),
2529                                getAccept1< ast::Expr, Expression * >( association.expr )
2530                        ));
2531                }
2532                this->node = visitBaseExpr( old,
2533                        new ast::GenericExpr(
2534                                old->location,
2535                                GET_ACCEPT_1(control, Expr),
2536                                std::move(associations)
2537                        )
2538                );
2539        }
2540
2541        void visitType( const Type * old, ast::Type * type ) {
2542                // Some types do this in their constructor so add a check.
2543                if ( !old->attributes.empty() && type->attributes.empty() ) {
2544                        type->attributes = GET_ACCEPT_V(attributes, Attribute);
2545                }
2546                this->node = type;
2547        }
2548
2549        virtual void visit( const VoidType * old ) override final {
2550                visitType( old, new ast::VoidType{ cv( old ) } );
2551        }
2552
2553        virtual void visit( const BasicType * old ) override final {
2554                auto type = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2555                // I believe this should always be a BasicType.
2556                if ( Validate::SizeType == old ) {
2557                        ast::sizeType = type;
2558                }
2559                visitType( old, type );
2560        }
2561
2562        virtual void visit( const PointerType * old ) override final {
2563                visitType( old, new ast::PointerType{
2564                        GET_ACCEPT_1( base, Type ),
2565                        GET_ACCEPT_1( dimension, Expr ),
2566                        (ast::LengthFlag)old->isVarLen,
2567                        (ast::DimensionFlag)old->isStatic,
2568                        cv( old )
2569                } );
2570        }
2571
2572        virtual void visit( const ArrayType * old ) override final {
2573                visitType( old, new ast::ArrayType{
2574                        GET_ACCEPT_1( base, Type ),
2575                        GET_ACCEPT_1( dimension, Expr ),
2576                        (ast::LengthFlag)old->isVarLen,
2577                        (ast::DimensionFlag)old->isStatic,
2578                        cv( old )
2579                } );
2580        }
2581
2582        virtual void visit( const ReferenceType * old ) override final {
2583                visitType( old, new ast::ReferenceType{
2584                        GET_ACCEPT_1( base, Type ),
2585                        cv( old )
2586                } );
2587        }
2588
2589        virtual void visit( const QualifiedType * old ) override final {
2590                visitType( old, new ast::QualifiedType{
2591                        GET_ACCEPT_1( parent, Type ),
2592                        GET_ACCEPT_1( child, Type ),
2593                        cv( old )
2594                } );
2595        }
2596
2597        virtual void visit( const FunctionType * old ) override final {
2598                auto ty = new ast::FunctionType {
2599                        (ast::ArgumentFlag)old->isVarArgs,
2600                        cv( old )
2601                };
2602                auto returnVars = GET_ACCEPT_V(returnVals, DeclWithType);
2603                auto paramVars = GET_ACCEPT_V(parameters, DeclWithType);
2604                // ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2605                // ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2606                for (auto & v: returnVars) {
2607                        ty->returns.emplace_back(v->get_type());
2608                }
2609                for (auto & v: paramVars) {
2610                        ty->params.emplace_back(v->get_type());
2611                }
2612                ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2613                visitType( old, ty );
2614        }
2615
2616        void postvisit( const ReferenceToType * old, ast::BaseInstType * ty ) {
2617                ty->params = GET_ACCEPT_V( parameters, Expr );
2618                ty->hoistType = old->hoistType;
2619                visitType( old, ty );
2620        }
2621
2622        virtual void visit( const StructInstType * old ) override final {
2623                ast::StructInstType * ty;
2624                if ( old->baseStruct ) {
2625                        ty = new ast::StructInstType{
2626                                GET_ACCEPT_1( baseStruct, StructDecl ),
2627                                cv( old ),
2628                                GET_ACCEPT_V( attributes, Attribute )
2629                        };
2630                } else {
2631                        ty = new ast::StructInstType{
2632                                old->name,
2633                                cv( old ),
2634                                GET_ACCEPT_V( attributes, Attribute )
2635                        };
2636                }
2637                postvisit( old, ty );
2638        }
2639
2640        virtual void visit( const UnionInstType * old ) override final {
2641                ast::UnionInstType * ty;
2642                if ( old->baseUnion ) {
2643                        ty = new ast::UnionInstType{
2644                                GET_ACCEPT_1( baseUnion, UnionDecl ),
2645                                cv( old ),
2646                                GET_ACCEPT_V( attributes, Attribute )
2647                        };
2648                } else {
2649                        ty = new ast::UnionInstType{
2650                                old->name,
2651                                cv( old ),
2652                                GET_ACCEPT_V( attributes, Attribute )
2653                        };
2654                }
2655                postvisit( old, ty );
2656        }
2657
2658        virtual void visit( const EnumInstType * old ) override final {
2659                ast::EnumInstType * ty;
2660                if ( old->baseEnum ) {
2661                        ty = new ast::EnumInstType{
2662                                GET_ACCEPT_1( baseEnum, EnumDecl ),
2663                                cv( old ),
2664                                GET_ACCEPT_V( attributes, Attribute )
2665                        };
2666                } else {
2667                        ty = new ast::EnumInstType{
2668                                old->name,
2669                                cv( old ),
2670                                GET_ACCEPT_V( attributes, Attribute )
2671                        };
2672                }
2673                postvisit( old, ty );
2674        }
2675
2676        virtual void visit( const TraitInstType * old ) override final {
2677                ast::TraitInstType * ty;
2678                if ( old->baseTrait ) {
2679                        ty = new ast::TraitInstType{
2680                                GET_ACCEPT_1( baseTrait, TraitDecl ),
2681                                cv( old ),
2682                                GET_ACCEPT_V( attributes, Attribute )
2683                        };
2684                } else {
2685                        ty = new ast::TraitInstType{
2686                                old->name,
2687                                cv( old ),
2688                                GET_ACCEPT_V( attributes, Attribute )
2689                        };
2690                }
2691                postvisit( old, ty );
2692        }
2693
2694        virtual void visit( const TypeInstType * old ) override final {
2695                ast::TypeInstType * ty;
2696                if ( old->baseType ) {
2697                        ty = new ast::TypeInstType{
2698                                old->name,
2699                                GET_ACCEPT_1( baseType, TypeDecl ),
2700                                cv( old ),
2701                                GET_ACCEPT_V( attributes, Attribute )
2702                        };
2703                } else {
2704                        ty = new ast::TypeInstType{
2705                                old->name,
2706                                old->isFtype ? ast::TypeDecl::Ftype : ast::TypeDecl::Dtype,
2707                                cv( old ),
2708                                GET_ACCEPT_V( attributes, Attribute )
2709                        };
2710                }
2711                postvisit( old, ty );
2712        }
2713
2714        virtual void visit( const TupleType * old ) override final {
2715                visitType( old, new ast::TupleType{
2716                        GET_ACCEPT_V( types, Type ),
2717                        // members generated by TupleType c'tor
2718                        cv( old )
2719                } );
2720        }
2721
2722        virtual void visit( const TypeofType * old ) override final {
2723                visitType( old, new ast::TypeofType{
2724                        GET_ACCEPT_1( expr, Expr ),
2725                        (ast::TypeofType::Kind)old->is_basetypeof,
2726                        cv( old )
2727                } );
2728        }
2729
2730        virtual void visit( const AttrType * ) override final {
2731                assertf( false, "AttrType deprecated in new AST." );
2732        }
2733
2734        virtual void visit( const VarArgsType * old ) override final {
2735                visitType( old, new ast::VarArgsType{ cv( old ) } );
2736        }
2737
2738        virtual void visit( const ZeroType * old ) override final {
2739                visitType( old, new ast::ZeroType{ cv( old ) } );
2740        }
2741
2742        virtual void visit( const OneType * old ) override final {
2743                visitType( old, new ast::OneType{ cv( old ) } );
2744        }
2745
2746        virtual void visit( const GlobalScopeType * old ) override final {
2747                visitType( old, new ast::GlobalScopeType{} );
2748        }
2749
2750        virtual void visit( const Designation * old ) override final {
2751                this->node = new ast::Designation(
2752                        old->location,
2753                        GET_ACCEPT_D(designators, Expr)
2754                );
2755        }
2756
2757        virtual void visit( const SingleInit * old ) override final {
2758                this->node = new ast::SingleInit(
2759                        old->location,
2760                        GET_ACCEPT_1(value, Expr),
2761                        (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2762                );
2763        }
2764
2765        virtual void visit( const ListInit * old ) override final {
2766                this->node = new ast::ListInit(
2767                        old->location,
2768                        GET_ACCEPT_V(initializers, Init),
2769                        GET_ACCEPT_V(designations, Designation),
2770                        (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2771                );
2772        }
2773
2774        virtual void visit( const ConstructorInit * old ) override final {
2775                this->node = new ast::ConstructorInit(
2776                        old->location,
2777                        GET_ACCEPT_1(ctor, Stmt),
2778                        GET_ACCEPT_1(dtor, Stmt),
2779                        GET_ACCEPT_1(init, Init)
2780                );
2781        }
2782
2783        virtual void visit( const Constant * ) override final {
2784                // Handled in visit( ConstantEpxr * ).
2785                // In the new tree, Constant fields are inlined into containing ConstantExpression.
2786                assert( 0 );
2787        }
2788
2789        virtual void visit( const Attribute * old ) override final {
2790                this->node = new ast::Attribute(
2791                        old->name,
2792                        GET_ACCEPT_V( parameters, Expr )
2793                );
2794        }
2795};
2796
2797#undef GET_LABELS_V
2798#undef GET_ACCEPT_V
2799#undef GET_ACCEPT_1
2800
2801ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ) {
2802        ConverterOldToNew c;
2803        ast::TranslationUnit unit;
2804        if (Validate::SizeType) {
2805                // this should be a BasicType.
2806                auto old = strict_dynamic_cast<BasicType *>(Validate::SizeType);
2807                ast::sizeType = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind };
2808        }
2809
2810        for(auto d : translationUnit) {
2811                d->accept( c );
2812                unit.decls.emplace_back( c.decl() );
2813        }
2814        deleteAll(translationUnit);
2815
2816        // Load the local static varables into the global store.
2817        unit.global.sizeType = ast::sizeType;
2818        unit.global.dereference = ast::dereferenceOperator;
2819        unit.global.dtorStruct = ast::dtorStruct;
2820        unit.global.dtorDestroy = ast::dtorStructDestroy;
2821
2822        return unit;
2823}
Note: See TracBrowser for help on using the repository browser.