source: src/AST/Convert.cpp@ 790d835

Last change on this file since 790d835 was 3d9d017, checked in by caparson <caparson@…>, 2 years ago

added cofor implementation

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