source: src/AST/Convert.cpp@ d21dd3cb

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d21dd3cb was 954c954, checked in by Fangren Yu <f37yu@…>, 5 years ago

Move function argument and return variable declarations from FunctionType to FunctionDecl

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