source: src/AST/Convert.cpp@ 148ba7d

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 148ba7d was 6cebfef, checked in by caparsons <caparson@…>, 4 years ago

added mutex stmt monitor

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