source: src/AST/Convert.cpp @ f7e4f8e8

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

move FixInit? to new ast

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