source: src/AST/Convert.cpp @ 74e3263

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 74e3263 was 07de76b, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove file TypeVar?.h* and put TypeVar::Kind into TypeDecl?, move LinkageSpec?.* from directory Parse to SynTree?

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