source: src/AST/Convert.cpp @ 293dc1c

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

TranslationUnit? is now used at the top-level of the new-ast passes.

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