source: src/AST/Convert.cpp @ e6cf857f

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since e6cf857f was 7edd5c1, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Assorted fixes to the AST, found while I was trying to add more invarant checks.

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