source: src/AST/Convert.cpp@ 3a513d89

ADT
Last change on this file since 3a513d89 was 561354f, checked in by JiadaL <j82liang@…>, 2 years ago

Save progress

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