source: src/AST/Convert.cpp@ 4da152a

ADT ast-experimental
Last change on this file since 4da152a was 44547b0, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Removed the ObjectDecl fields now represented on InlineValueDecl. Removed unused new AST content from Validate (should recompile less often.

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