source: src/AST/Convert.cpp@ d83b266

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d83b266 was cc64be1d, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added VTableType to the conversion passes.

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