source: src/AST/Convert.cpp @ 374cb117

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 374cb117 was 374cb117, checked in by JiadaL <j82liang@…>, 2 years ago

Replace the interface for EnumDecl? node construction to support generic enum types

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