source: src/AST/Convert.cpp@ fde0a58

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since fde0a58 was 3b0bc16, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

change class name WhileStmt to WhileDoStmt, add else clause to WhileDoStmt and ForStmt, change names thenPart/ElsePart to then/else_

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