source: src/AST/Convert.cpp@ a8ed717

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 a8ed717 was 81da70a5, checked in by Fangren Yu <f37yu@…>, 5 years ago

actually fill inferred params and disable a check

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