source: src/AST/Convert.cpp@ dd92fe9

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

remove ParameterizedType and put content into FunctionType

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