source: src/AST/Convert.cpp@ 73973b6

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 73973b6 was 7030dab, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' into new-ast

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