source: src/AST/Convert.cpp@ ca995e3

Last change on this file since ca995e3 was cf3da24, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Fixed up some whitespace. Pretty minor stuff mostly.

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