source: src/AST/Convert.cpp@ 3fc0f2a

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 3fc0f2a was a2a85658, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

Fixed conversion of tuple assignments to reuse the common part correctly. For example, in [x,y] = foo().[a,b], foo should be called once. Convert-convert before resolve was causing foo to be called twice, because the unique ID of a UniqueExpr wasn't preserved on AST conversion. Symptom was failing test tuple/tupleMember (given convert-convert before resolve).

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