source: src/AST/Convert.cpp@ 04396aa

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 04396aa was 558d13b, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

fix conversion bug in constants; string constants were being mistaken for int after resolution when their type changes from char[k] to char*

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