source: src/AST/Convert.cpp@ 50202fa

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 50202fa was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 5 years ago

reimplement function type and eliminate deep copy

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