source: src/AST/Convert.cpp@ a62749f

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

Better support for loose labels.

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