source: src/AST/Convert.cpp@ 6ce9a4f2

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

attempt to fix sizeType in new ast

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