source: src/AST/Convert.cpp@ b4d34fa

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

Fixed handling of "literals.cfa" string-detail test cases by simplifying constant analysis. Now a ConstantExpr is a minial passthrough from parser to code generator, with special-case analysis only for integer values. Awareness of how to build a string-constant type is back in ExpressionNode.cc; now, this knowlede is only needed there. AST conversion no longer specializes string-int-float constants; it just converts types and passes values through. Unused constant API features are removed, notably from-to-float and from-string.

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