source: src/AST/Convert.cpp@ 8b34df0

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 8b34df0 was 8b34df0, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed incorrect copying in InferredParameters conversion

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