source: src/AST/Convert.cpp @ 5d2db68

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

move FixInit? to new ast

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