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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3d2852a was 37cdd97, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added a ast node for suspend statements

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