source: src/AST/Convert.cpp@ 89eff25

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 89eff25 was 2d019af, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

parser global pragmas, fixes #241

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