source: src/AST/Convert.cpp@ f6cc734e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since f6cc734e was f6cc734e, checked in by Michael Brooks <mlbrooks@…>, 7 years ago

Fixing new-resolver bug with incorrectly reused FunctionType pieces in old-model AST after convert-back. (See code comments for detail.) Symptom was crash (with pure virtual method call, due to double delete) during InstantiateGeneric pass on Bootloader. Now libcfa build progresses to next file, src/prelude.o.

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