source: src/AST/Convert.cpp @ 1387ea0

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 1387ea0 was 1387ea0, checked in by Andrew Beach <ajbeach@…>, 2 years ago

We can now thread the global/special declarations from convert to convert using the TranslationUnit?.

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