source: src/AST/Convert.cpp @ 4a60488

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4a60488 was 4a60488, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Merged from master taking the lvalue changes to expression and everything before that.

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