source: src/AST/Convert.cpp@ 6b419ce

Last change on this file since 6b419ce was 3d9d017, checked in by caparson <caparson@…>, 23 months ago

added cofor implementation

  • Property mode set to 100644
File size: 83.8 KB
Line 
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//
7// Convert.cpp -- Convert between the new and old syntax trees.
8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Apr 20 13:58:00 2022
13// Update Count : 43
14//
15
16#include "Convert.hpp"
17
18#include <deque>
19#include <unordered_map>
20
21#include "AST/Attribute.hpp"
22#include "AST/Copy.hpp"
23#include "AST/Decl.hpp"
24#include "AST/Expr.hpp"
25#include "AST/Init.hpp"
26#include "AST/Stmt.hpp"
27#include "AST/TranslationUnit.hpp"
28#include "AST/TypeSubstitution.hpp"
29
30#include "SymTab/Autogen.h"
31#include "SynTree/Attribute.h"
32#include "SynTree/Declaration.h"
33#include "SynTree/TypeSubstitution.h"
34
35#include "Validate/FindSpecialDecls.h"
36
37//================================================================================================
38// Utilities
39template<template <class...> class C>
40struct to {
41 template<typename T>
42 static auto from( T && v ) -> C< typename T::value_type > {
43 C< typename T::value_type > l;
44 std::move(std::begin(v), std::end(v), std::back_inserter(l));
45 return l;
46 }
47};
48
49//================================================================================================
50namespace ast {
51// These are the shared local information used by ConverterNewToOld and
52// ConverterOldToNew to update the global information in the two versions.
53
54static ast::ptr<ast::Type> sizeType = nullptr;
55static const ast::FunctionDecl * dereferenceOperator = nullptr;
56static const ast::StructDecl * dtorStruct = nullptr;
57static const ast::FunctionDecl * dtorStructDestroy = nullptr;
58
59}
60
61//================================================================================================
62class ConverterNewToOld : public ast::Visitor {
63 BaseSyntaxNode * node = nullptr;
64 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
65 Cache cache;
66
67 // Statements can no longer be shared.
68 // however, since StmtExprResult is now implemented, need to still maintain
69 // readonly references.
70 Cache readonlyCache;
71
72 template<typename T>
73 struct Getter {
74 ConverterNewToOld & visitor;
75
76 template<typename U, enum ast::Node::ref_type R>
77 T * accept1( const ast::ptr_base<U, R> & ptr ) {
78 if ( ! ptr ) return nullptr;
79 ptr->accept( visitor );
80 T * ret = strict_dynamic_cast< T * >( visitor.node );
81 visitor.node = nullptr;
82 return ret;
83 }
84
85 template<typename U>
86 std::list< T * > acceptL( const U & container ) {
87 std::list< T * > ret;
88 for ( auto ptr : container ) {
89 ret.emplace_back( accept1( ptr ) );
90 }
91 return ret;
92 }
93 };
94
95 template<typename T>
96 Getter<T> get() {
97 return Getter<T>{ *this };
98 }
99
100 Label makeLabel(Statement * labelled, const ast::Label& label) {
101 // This probably will leak memory, but only until we get rid of the old tree.
102 if ( nullptr == labelled && label.location.isSet() ) {
103 labelled = new NullStmt();
104 labelled->location = label.location;
105 }
106 return Label(
107 label.name,
108 labelled,
109 get<Attribute>().acceptL(label.attributes)
110 );
111 }
112
113 template<template <class...> class C>
114 std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
115 std::list<Label> ret;
116 for (auto label : labels) {
117 ret.push_back( makeLabel(labelled, label) );
118 }
119 return ret;
120 }
121
122 /// get new qualifiers from old type
123 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
124
125 /// returns true and sets `node` if in cache
126 bool inCache( const ast::Node * node ) {
127 auto it = cache.find( node );
128 if ( it == cache.end() ) return false;
129 this->node = it->second;
130 return true;
131 }
132
133public:
134 Declaration * decl( const ast::Decl * declNode ) {
135 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
136 }
137
138private:
139 void declPostamble( Declaration * decl, const ast::Decl * node ) {
140 decl->location = node->location;
141 // name comes from constructor
142 // linkage comes from constructor
143 decl->extension = node->extension;
144 decl->uniqueId = node->uniqueId;
145 // storageClasses comes from constructor
146 this->node = decl;
147 }
148
149 const ast::DeclWithType * declWithTypePostamble (
150 DeclarationWithType * decl, const ast::DeclWithType * node ) {
151 cache.emplace( node, decl );
152 decl->mangleName = node->mangleName;
153 decl->scopeLevel = node->scopeLevel;
154 decl->asmName = get<Expression>().accept1( node->asmName );
155 // attributes comes from constructor
156 decl->isDeleted = node->isDeleted;
157 // fs comes from constructor
158 declPostamble( decl, node );
159 return nullptr;
160 }
161
162 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
163 if ( inCache( node ) ) {
164 return nullptr;
165 }
166 auto bfwd = get<Expression>().accept1( node->bitfieldWidth );
167 auto type = get<Type>().accept1( node->type );
168 auto attr = get<Attribute>().acceptL( node->attributes );
169
170 // This field can be unset very early on (Pre-FixReturnTypes).
171 auto newType = (type) ? type->clone() : nullptr;
172
173 auto decl = new ObjectDecl(
174 node->name,
175 Type::StorageClasses( node->storage.val ),
176 LinkageSpec::Spec( node->linkage.val ),
177 bfwd,
178 newType,
179 nullptr, // prevent infinite loop
180 attr,
181 Type::FuncSpecifiers( node->funcSpec.val )
182 );
183
184 // handles the case where node->init references itself
185 // xxx - does it really happen?
186 declWithTypePostamble(decl, node);
187 auto init = get<Initializer>().accept1( node->init );
188 decl->init = init;
189
190 this->node = decl;
191 return nullptr;
192 }
193
194 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
195 if ( inCache( node ) ) return nullptr;
196
197 // function decl contains real variables that the type must use.
198 // the structural change means function type in and out of decl
199 // must be handled **differently** on convert back to old.
200 auto ftype = new FunctionType(
201 cv(node->type),
202 (bool)node->type->isVarArgs
203 );
204 ftype->returnVals = get<DeclarationWithType>().acceptL(node->returns);
205 ftype->parameters = get<DeclarationWithType>().acceptL(node->params);
206
207 ftype->forall = get<TypeDecl>().acceptL( node->type_params );
208 if (!node->assertions.empty()) {
209 assert(!ftype->forall.empty());
210 // find somewhere to place assertions back, for convenience it is the last slot
211 ftype->forall.back()->assertions = get<DeclarationWithType>().acceptL(node->assertions);
212 }
213
214 visitType(node->type, ftype);
215
216 auto decl = new FunctionDecl(
217 node->name,
218 Type::StorageClasses( node->storage.val ),
219 LinkageSpec::Spec( node->linkage.val ),
220 ftype,
221 //get<FunctionType>().accept1( node->type ),
222 {},
223 get<Attribute>().acceptL( node->attributes ),
224 Type::FuncSpecifiers( node->funcSpec.val )
225 );
226 cache.emplace( node, decl );
227 decl->statements = get<CompoundStmt>().accept1( node->stmts );
228 decl->withExprs = get<Expression>().acceptL( node->withExprs );
229 if ( ast::dereferenceOperator == node ) {
230 Validate::dereferenceOperator = decl;
231 }
232 if ( ast::dtorStructDestroy == node ) {
233 Validate::dtorStructDestroy = decl;
234 }
235 return declWithTypePostamble( decl, node );
236 }
237
238 // InlineMemberDecl vanish after EnumAndPointerDecay pass so no necessary to implement NewToOld
239 const ast::DeclWithType * visit( const ast::InlineMemberDecl * node ) override final {
240 assert( false );
241 (void) node;
242 return nullptr;
243 }
244
245 const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) {
246 // base comes from constructor
247 decl->assertions = get<DeclarationWithType>().acceptL( node->assertions );
248 declPostamble( decl, node );
249 return nullptr;
250 }
251
252 const ast::Decl * visit( const ast::TypeDecl * node ) override final {
253 if ( inCache( node ) ) return nullptr;
254 auto decl = new TypeDecl(
255 node->name,
256 Type::StorageClasses( node->storage.val ),
257 get<Type>().accept1( node->base ),
258 (TypeDecl::Kind)(unsigned)node->kind,
259 node->sized,
260 get<Type>().accept1( node->init )
261 );
262 cache.emplace( node, decl );
263 return namedTypePostamble( decl, node );
264 }
265
266 const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
267 auto decl = new TypedefDecl(
268 node->name,
269 node->location,
270 Type::StorageClasses( node->storage.val ),
271 get<Type>().accept1( node->base ),
272 LinkageSpec::Spec( node->linkage.val )
273 );
274 return namedTypePostamble( decl, node );
275 }
276
277 const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) {
278 cache.emplace( node, decl );
279 decl->members = get<Declaration>().acceptL( node->members );
280 decl->parameters = get<TypeDecl>().acceptL( node->params );
281 decl->body = node->body;
282 // attributes come from constructor
283 decl->parent = get<AggregateDecl>().accept1( node->parent );
284 declPostamble( decl, node );
285 return nullptr; // ??
286 }
287
288 const ast::Decl * visit( const ast::StructDecl * node ) override final {
289 if ( inCache( node ) ) return nullptr;
290 auto decl = new StructDecl(
291 node->name,
292 (AggregateDecl::Aggregate)node->kind,
293 get<Attribute>().acceptL( node->attributes ),
294 LinkageSpec::Spec( node->linkage.val )
295 );
296
297 if ( ast::dtorStruct == node ) {
298 Validate::dtorStruct = decl;
299 }
300
301 return aggregatePostamble( decl, node );
302 }
303
304 const ast::Decl * visit( const ast::UnionDecl * node ) override final {
305 if ( inCache( node ) ) return nullptr;
306 auto decl = new UnionDecl(
307 node->name,
308 get<Attribute>().acceptL( node->attributes ),
309 LinkageSpec::Spec( node->linkage.val )
310 );
311 return aggregatePostamble( decl, node );
312 }
313
314 const ast::Decl * visit( const ast::EnumDecl * node ) override final {
315 if ( inCache( node ) ) return nullptr;
316 auto decl = new EnumDecl(
317 node->name,
318 get<Attribute>().acceptL( node->attributes ),
319 node->isTyped,
320 LinkageSpec::Spec( node->linkage.val ),
321 get<Type>().accept1(node->base)
322 );
323 return aggregatePostamble( decl, node );
324 }
325
326 const ast::Decl * visit( const ast::TraitDecl * node ) override final {
327 if ( inCache( node ) ) return nullptr;
328 auto decl = new TraitDecl(
329 node->name,
330 {},
331 LinkageSpec::Spec( node->linkage.val )
332 );
333 return aggregatePostamble( decl, node );
334 }
335
336 const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
337 auto decl = new AsmDecl( get<AsmStmt>().accept1( node->stmt ) );
338 declPostamble( decl, node );
339 return nullptr;
340 }
341
342 const ast::DirectiveDecl * visit( const ast::DirectiveDecl * node ) override final {
343 auto decl = new DirectiveDecl( get<DirectiveStmt>().accept1( node->stmt ) );
344 declPostamble( decl, node );
345 return nullptr;
346 }
347
348 const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
349 auto decl = new StaticAssertDecl(
350 get<Expression>().accept1( node->cond ),
351 get<ConstantExpr>().accept1( node->msg )
352 );
353 declPostamble( decl, node );
354 return nullptr;
355 }
356
357 const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
358 // force statements in old tree to be unique.
359 // cache.emplace( node, stmt );
360 readonlyCache.emplace( node, stmt );
361 stmt->location = node->location;
362 stmt->labels = makeLabelL( stmt, node->labels );
363 this->node = stmt;
364 return nullptr;
365 }
366
367 void clausePostamble( Statement * stmt, const ast::StmtClause * node ) {
368 stmt->location = node->location;
369 this->node = stmt;
370 }
371
372 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
373 if ( inCache( node ) ) return nullptr;
374 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
375 stmtPostamble( stmt, node );
376 return nullptr;
377 }
378
379 const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
380 if ( inCache( node ) ) return nullptr;
381 auto stmt = new ExprStmt( nullptr );
382 stmt->expr = get<Expression>().accept1( node->expr );
383 return stmtPostamble( stmt, node );
384 }
385
386 const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
387 if ( inCache( node ) ) return nullptr;
388 auto stmt = new AsmStmt(
389 node->isVolatile,
390 get<Expression>().accept1( node->instruction ),
391 get<Expression>().acceptL( node->output ),
392 get<Expression>().acceptL( node->input ),
393 get<ConstantExpr>().acceptL( node->clobber ),
394 makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
395 );
396 return stmtPostamble( stmt, node );
397 }
398
399 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
400 if ( inCache( node ) ) return nullptr;
401 auto stmt = new DirectiveStmt( node->directive );
402 return stmtPostamble( stmt, node );
403 }
404
405 const ast::Stmt * visit( const ast::IfStmt * node ) override final {
406 if ( inCache( node ) ) return nullptr;
407 auto stmt = new IfStmt(
408 get<Expression>().accept1( node->cond ),
409 get<Statement>().accept1( node->then ),
410 get<Statement>().accept1( node->else_ ),
411 get<Statement>().acceptL( node->inits )
412 );
413 return stmtPostamble( stmt, node );
414 }
415
416 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
417 if ( inCache( node ) ) return nullptr;
418 auto stmt = new SwitchStmt(
419 get<Expression>().accept1( node->cond ),
420 get<Statement>().acceptL( node->cases )
421 );
422 return stmtPostamble( stmt, node );
423 }
424
425 const ast::CaseClause * visit( const ast::CaseClause * node ) override final {
426 if ( inCache( node ) ) return nullptr;
427 auto stmt = new CaseStmt(
428 get<Expression>().accept1( node->cond ),
429 get<Statement>().acceptL( node->stmts ),
430 node->isDefault()
431 );
432 clausePostamble( stmt, node );
433 return nullptr;
434 }
435
436 const ast::Stmt * visit( const ast::WhileDoStmt * node ) override final {
437 if ( inCache( node ) ) return nullptr;
438 auto inits = get<Statement>().acceptL( node->inits );
439 auto stmt = new WhileDoStmt(
440 get<Expression>().accept1( node->cond ),
441 get<Statement>().accept1( node->body ),
442 get<Statement>().accept1( node->else_ ),
443 inits,
444 node->isDoWhile
445 );
446 return stmtPostamble( stmt, node );
447 }
448
449 const ast::Stmt * visit( const ast::ForStmt * node ) override final {
450 if ( inCache( node ) ) return nullptr;
451 auto stmt = new ForStmt(
452 get<Statement>().acceptL( node->inits ),
453 get<Expression>().accept1( node->cond ),
454 get<Expression>().accept1( node->inc ),
455 get<Statement>().accept1( node->body ),
456 get<Statement>().accept1( node->else_ )
457 );
458 return stmtPostamble( stmt, node );
459 }
460
461 const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
462 if ( inCache( node ) ) return nullptr;
463 BranchStmt * stmt;
464 if (node->computedTarget) {
465 stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
466 BranchStmt::Goto );
467 } else {
468 BranchStmt::Type type;
469 switch (node->kind) {
470 #define CASE(n) \
471 case ast::BranchStmt::n: \
472 type = BranchStmt::n; \
473 break
474 CASE(Goto);
475 CASE(Break);
476 CASE(Continue);
477 CASE(FallThrough);
478 CASE(FallThroughDefault);
479 #undef CASE
480 default:
481 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
482 }
483
484 // The labels here are also weird.
485 stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
486 stmt->target = makeLabel( stmt, node->target );
487 }
488 return stmtPostamble( stmt, node );
489 }
490
491 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
492 if ( inCache( node ) ) return nullptr;
493 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
494 return stmtPostamble( stmt, node );
495 }
496
497 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
498 if ( inCache( node ) ) return nullptr;
499 ThrowStmt::Kind kind;
500 switch (node->kind) {
501 case ast::ExceptionKind::Terminate:
502 kind = ThrowStmt::Terminate;
503 break;
504 case ast::ExceptionKind::Resume:
505 kind = ThrowStmt::Resume;
506 break;
507 default:
508 assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
509 }
510 auto stmt = new ThrowStmt(
511 kind,
512 get<Expression>().accept1( node->expr ),
513 get<Expression>().accept1( node->target )
514 );
515 return stmtPostamble( stmt, node );
516 }
517
518 const ast::Stmt * visit( const ast::TryStmt * node ) override final {
519 if ( inCache( node ) ) return nullptr;
520 auto handlers = get<CatchStmt>().acceptL( node->handlers );
521 auto stmt = new TryStmt(
522 get<CompoundStmt>().accept1( node->body ),
523 handlers,
524 get<FinallyStmt>().accept1( node->finally )
525 );
526 return stmtPostamble( stmt, node );
527 }
528
529 const ast::CatchClause * visit( const ast::CatchClause * node ) override final {
530 if ( inCache( node ) ) return nullptr;
531 CatchStmt::Kind kind;
532 switch (node->kind) {
533 case ast::ExceptionKind::Terminate:
534 kind = CatchStmt::Terminate;
535 break;
536 case ast::ExceptionKind::Resume:
537 kind = CatchStmt::Resume;
538 break;
539 default:
540 assertf(false, "Invalid ast::ExceptionKind: %d\n", node->kind);
541 }
542 auto stmt = new CatchStmt(
543 kind,
544 get<Declaration>().accept1( node->decl ),
545 get<Expression>().accept1( node->cond ),
546 get<Statement>().accept1( node->body )
547 );
548 return clausePostamble( stmt, node ), nullptr;
549 }
550
551 const ast::FinallyClause * visit( const ast::FinallyClause * node ) override final {
552 if ( inCache( node ) ) return nullptr;
553 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
554 return clausePostamble( stmt, node ), nullptr;
555 }
556
557 const ast::Stmt * visit(const ast::SuspendStmt * node ) override final {
558 if ( inCache( node ) ) return nullptr;
559 auto stmt = new SuspendStmt();
560 stmt->then = get<CompoundStmt>().accept1( node->then );
561 switch (node->kind) {
562 case ast::SuspendStmt::None : stmt->type = SuspendStmt::None ; break;
563 case ast::SuspendStmt::Coroutine: stmt->type = SuspendStmt::Coroutine; break;
564 case ast::SuspendStmt::Generator: stmt->type = SuspendStmt::Generator; break;
565 }
566 return stmtPostamble( stmt, node );
567 }
568
569 const ast::WhenClause * visit( const ast::WhenClause * node ) override final {
570 // There is no old-AST WhenClause, so this should never be called.
571 assert( !node );
572 return nullptr;
573 }
574
575 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
576 if ( inCache( node ) ) return nullptr;
577 auto stmt = new WaitForStmt;
578 stmt->clauses.reserve( node->clauses.size() );
579 for ( auto clause : node->clauses ) {
580 stmt->clauses.push_back({{
581 get<Expression>().accept1( clause->target ),
582 get<Expression>().acceptL( clause->target_args ),
583 },
584 get<Statement>().accept1( clause->stmt ),
585 get<Expression>().accept1( clause->when_cond ),
586 });
587 }
588 stmt->timeout = {
589 get<Expression>().accept1( node->timeout_time ),
590 get<Statement>().accept1( node->timeout_stmt ),
591 get<Expression>().accept1( node->timeout_cond ),
592 };
593 stmt->orelse = {
594 get<Statement>().accept1( node->else_stmt ),
595 get<Expression>().accept1( node->else_cond ),
596 };
597 return stmtPostamble( stmt, node );
598 }
599
600 const ast::WaitForClause * visit( const ast::WaitForClause * node ) override final {
601 // There is no old-AST WaitForClause, so this should never be called.
602 assert( !node );
603 return nullptr;
604 }
605
606 const ast::Stmt * visit( const ast::WaitUntilStmt * node ) override final {
607 // There is no old-AST WaitUntilStmt, so this should never be called.
608 assert( !node );
609 return nullptr;
610 }
611
612 const ast::Decl * visit( const ast::WithStmt * node ) override final {
613 if ( inCache( node ) ) return nullptr;
614 auto stmt = new WithStmt(
615 get<Expression>().acceptL( node->exprs ),
616 get<Statement>().accept1( node->stmt )
617 );
618 declPostamble( stmt, node );
619 return nullptr;
620 }
621
622 const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
623 if ( inCache( node ) ) return nullptr;
624 auto stmt = new NullStmt();
625 stmtPostamble( stmt, node );
626 return nullptr;
627 }
628
629 const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
630 if ( inCache( node ) ) return nullptr;
631 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
632 return stmtPostamble( stmt, node );
633 }
634
635 const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
636 if ( inCache( node ) ) return nullptr;
637 auto stmt = new ImplicitCtorDtorStmt{
638 get<Statement>().accept1( node->callStmt )
639 };
640 return stmtPostamble( stmt, node );
641 }
642
643 const ast::Stmt * visit( const ast::MutexStmt * node ) override final {
644 if ( inCache( node ) ) return nullptr;
645 auto stmt = new MutexStmt(
646 get<Statement>().accept1( node->stmt ),
647 get<Expression>().acceptL( node->mutexObjs )
648 );
649 return stmtPostamble( stmt, node );
650 }
651
652 const ast::Stmt * visit( const ast::CorunStmt * node ) override final {
653 // There is no old-AST CorunStmt, so this should never be called.
654 assert( !node );
655 return nullptr;
656 }
657
658 const ast::Stmt * visit( const ast::CoforStmt * node ) override final {
659 // There is no old-AST CoforStmt, so this should never be called.
660 assert( !node );
661 return nullptr;
662 }
663
664 TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
665
666 if (!src) return nullptr;
667
668 TypeSubstitution *rslt = new TypeSubstitution();
669
670 for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
671 rslt->add( src_i->first.typeString(),
672 get<Type>().accept1(src_i->second) );
673 }
674
675 return rslt;
676 }
677
678 void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
679 std::vector<UniqueId> &tgtResnSlots,
680 const ast::Expr::InferUnion &srcInferred ) {
681
682 assert( tgtInferParams.empty() );
683 assert( tgtResnSlots.empty() );
684
685 if ( srcInferred.data.inferParams ) {
686 const ast::InferredParams &srcParams = srcInferred.inferParams();
687 for (auto & srcParam : srcParams) {
688 auto res = tgtInferParams.emplace(srcParam.first, ParamEntry(
689 srcParam.second.decl,
690 get<Declaration>().accept1(srcParam.second.declptr),
691 get<Type>().accept1(srcParam.second.actualType)->clone(),
692 get<Type>().accept1(srcParam.second.formalType)->clone(),
693 get<Expression>().accept1(srcParam.second.expr)->clone()
694 ));
695 assert(res.second);
696 }
697 }
698 if ( srcInferred.data.resnSlots ) {
699 const ast::ResnSlots &srcSlots = srcInferred.resnSlots();
700 for (auto srcSlot : srcSlots) {
701 tgtResnSlots.push_back(srcSlot);
702 }
703 }
704 }
705
706 Expression * visitBaseExpr_skipResultType(const ast::Expr * src, Expression * tgt) {
707
708 tgt->location = src->location;
709 tgt->env = convertTypeSubstitution(src->env);
710 tgt->extension = src->extension;
711
712 convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
713 return tgt;
714 }
715
716 Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
717
718 tgt->result = get<Type>().accept1(src->result);
719 // Unconditionally use a clone of the result type.
720 // We know this will leak some objects: much of the immediate conversion result.
721 // In some cases, using the conversion result directly gives unintended object sharing.
722 // A parameter (ObjectDecl, a child of a FunctionType) is shared by the weak-ref cache.
723 // But tgt->result must be fully owned privately by tgt.
724 // Applying these conservative copies here means
725 // - weak references point at the declaration's copy, not these expr.result copies (good)
726 // - we copy more objects than really needed (bad, tolerated)
727 if (tgt->result) {
728 tgt->result = tgt->result->clone();
729 }
730 return visitBaseExpr_skipResultType(src, tgt);
731 }
732
733 const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
734 auto expr = visitBaseExpr( node,
735 new ApplicationExpr(
736 get<Expression>().accept1(node->func),
737 get<Expression>().acceptL(node->args)
738 )
739 );
740 this->node = expr;
741 return nullptr;
742 }
743
744 const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
745 auto expr = visitBaseExpr( node,
746 new UntypedExpr(
747 get<Expression>().accept1(node->func),
748 get<Expression>().acceptL(node->args)
749 )
750 );
751 this->node = expr;
752 return nullptr;
753 }
754
755 const ast::Expr * visit( const ast::NameExpr * node ) override final {
756 auto expr = visitBaseExpr( node,
757 new NameExpr(
758 node->name
759 )
760 );
761 this->node = expr;
762 return nullptr;
763 }
764
765 const ast::Expr * visit( const ast::QualifiedNameExpr * node ) override final {
766 auto temp = new QualifiedNameExpr(
767 get<Declaration>().accept1(node->type_decl),
768 node->name
769 );
770 auto expr = visitBaseExpr( node,
771 temp
772 );
773 this->node = expr;
774 return nullptr;
775 }
776
777 const ast::Expr * visit( const ast::AddressExpr * node ) override final {
778 auto expr = visitBaseExpr( node,
779 new AddressExpr(
780 get<Expression>().accept1(node->arg)
781 )
782 );
783 this->node = expr;
784 return nullptr;
785 }
786
787 const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
788 auto expr = visitBaseExpr( node,
789 new LabelAddressExpr(
790 makeLabel(nullptr, node->arg)
791 )
792 );
793 this->node = expr;
794 return nullptr;
795 }
796
797 const ast::Expr * visit( const ast::CastExpr * node ) override final {
798 auto expr = visitBaseExpr( node,
799 new CastExpr(
800 get<Expression>().accept1(node->arg),
801 (node->isGenerated == ast::GeneratedCast)
802 )
803 );
804 this->node = expr;
805 return nullptr;
806 }
807
808 const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
809 AggregateDecl::Aggregate castTarget = (AggregateDecl::Aggregate)node->target;
810 assert( AggregateDecl::Generator <= castTarget && castTarget <= AggregateDecl::Thread );
811 auto expr = visitBaseExpr( node,
812 new KeywordCastExpr(
813 get<Expression>().accept1(node->arg),
814 castTarget,
815 {node->concrete_target.field, node->concrete_target.getter}
816 )
817 );
818 this->node = expr;
819 return nullptr;
820 }
821
822 const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
823 auto expr = visitBaseExpr_skipResultType( node,
824 new VirtualCastExpr(
825 get<Expression>().accept1(node->arg),
826 get<Type>().accept1(node->result)
827 )
828 );
829 this->node = expr;
830 return nullptr;
831 }
832
833 const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
834 auto expr = visitBaseExpr( node,
835 new UntypedMemberExpr(
836 get<Expression>().accept1(node->member),
837 get<Expression>().accept1(node->aggregate)
838 )
839 );
840 this->node = expr;
841 return nullptr;
842 }
843
844 const ast::Expr * visit( const ast::MemberExpr * node ) override final {
845 auto expr = visitBaseExpr( node,
846 new MemberExpr(
847 get<DeclarationWithType>().accept1(node->member),
848 get<Expression>().accept1(node->aggregate)
849 )
850 );
851 this->node = expr;
852 return nullptr;
853 }
854
855 const ast::Expr * visit( const ast::VariableExpr * node ) override final {
856 auto expr = new VariableExpr();
857 expr->var = get<DeclarationWithType>().accept1(node->var);
858 visitBaseExpr( node, expr );
859 this->node = expr;
860 return nullptr;
861 }
862
863 const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
864 // Old world: two types: rslt->constant.type, rslt->result
865 // New workd: one public type: node->result, plus node->underlyer only to support roundtrip conversion
866 // preserving underlyer because the correct type for string literals is complicated to construct,
867 // and distinguishing a string from other literals using the type is hard to do accurately
868 // Both worlds: the outer, expression-level type can change during resolution
869 // for a string, that's char[k] before-resolve and char * after
870 // Old world: the inner Constant type stays what it was built with
871 // for a string, that's char[k] always
872 // Both worlds: the "rep" field of a constant is the C source file fragment that compiles to the desired value
873 // for a string, that includes outer quotes, backslashes, et al cases from the Literals test
874 ConstantExpr *rslt = new ConstantExpr(Constant(
875 get<Type>().accept1(node->underlyer),
876 node->rep,
877 node->ival));
878 auto expr = visitBaseExpr( node, rslt );
879 this->node = expr;
880 return nullptr;
881 }
882
883 const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
884 assert (node->expr || node->type);
885 assert (! (node->expr && node->type));
886 SizeofExpr *rslt;
887 if (node->expr) {
888 rslt = new SizeofExpr(
889 get<Expression>().accept1(node->expr)
890 );
891 assert (!rslt->isType);
892 }
893 else {
894 assert(node->type);
895 rslt = new SizeofExpr(
896 get<Type>().accept1(node->type)
897 );
898 assert (rslt->isType);
899 }
900 auto expr = visitBaseExpr( node, rslt );
901 this->node = expr;
902 return nullptr;
903 }
904
905 const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
906 assert (node->expr || node->type);
907 assert (! (node->expr && node->type));
908 AlignofExpr *rslt;
909 if (node->expr) {
910 rslt = new AlignofExpr(
911 get<Expression>().accept1(node->expr)
912 );
913 assert (!rslt->isType);
914 }
915 else {
916 assert(node->type);
917 rslt = new AlignofExpr(
918 get<Type>().accept1(node->type)
919 );
920 assert (rslt->isType);
921 }
922 auto expr = visitBaseExpr( node, rslt );
923 this->node = expr;
924 return nullptr;
925 }
926
927 const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
928 auto expr = visitBaseExpr( node,
929 new UntypedOffsetofExpr(
930 get<Type>().accept1(node->type),
931 node->member
932 )
933 );
934 this->node = expr;
935 return nullptr;
936 }
937
938 const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
939 auto expr = visitBaseExpr( node,
940 new OffsetofExpr(
941 get<Type>().accept1(node->type),
942 get<DeclarationWithType>().accept1(node->member)
943 )
944 );
945 this->node = expr;
946 return nullptr;
947 }
948
949 const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
950 auto expr = visitBaseExpr( node,
951 new OffsetPackExpr(
952 get<StructInstType>().accept1(node->type)
953 )
954 );
955 this->node = expr;
956 return nullptr;
957 }
958
959 const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
960 assert (node->isAnd == ast::LogicalFlag::AndExpr ||
961 node->isAnd == ast::LogicalFlag::OrExpr );
962 auto expr = visitBaseExpr( node,
963 new LogicalExpr(
964 get<Expression>().accept1(node->arg1),
965 get<Expression>().accept1(node->arg2),
966 (node->isAnd == ast::LogicalFlag::AndExpr)
967 )
968 );
969 this->node = expr;
970 return nullptr;
971 }
972
973 const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
974 auto expr = visitBaseExpr( node,
975 new ConditionalExpr(
976 get<Expression>().accept1(node->arg1),
977 get<Expression>().accept1(node->arg2),
978 get<Expression>().accept1(node->arg3)
979 )
980 );
981 this->node = expr;
982 return nullptr;
983 }
984
985 const ast::Expr * visit( const ast::CommaExpr * node ) override final {
986 auto expr = visitBaseExpr( node,
987 new CommaExpr(
988 get<Expression>().accept1(node->arg1),
989 get<Expression>().accept1(node->arg2)
990 )
991 );
992 this->node = expr;
993 return nullptr;
994 }
995
996 const ast::Expr * visit( const ast::TypeExpr * node ) override final {
997 auto expr = visitBaseExpr( node,
998 new TypeExpr(
999 get<Type>().accept1(node->type)
1000 )
1001 );
1002 this->node = expr;
1003 return nullptr;
1004 }
1005
1006 const ast::Expr * visit( const ast::DimensionExpr * node ) override final {
1007 auto expr = visitBaseExpr( node, new DimensionExpr( node->name ) );
1008 this->node = expr;
1009 return nullptr;
1010 }
1011
1012 const ast::Expr * visit( const ast::AsmExpr * node ) override final {
1013 auto expr = visitBaseExpr( node,
1014 new AsmExpr(
1015 new std::string(node->inout),
1016 get<Expression>().accept1(node->constraint),
1017 get<Expression>().accept1(node->operand)
1018 )
1019 );
1020 this->node = expr;
1021 return nullptr;
1022 }
1023
1024 const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
1025 auto rslt = new ImplicitCopyCtorExpr(
1026 get<ApplicationExpr>().accept1(node->callExpr)
1027 );
1028
1029 auto expr = visitBaseExpr( node, rslt );
1030 this->node = expr;
1031 return nullptr;
1032 }
1033
1034 const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
1035 auto expr = visitBaseExpr( node,
1036 new ConstructorExpr(
1037 get<Expression>().accept1(node->callExpr)
1038 )
1039 );
1040 this->node = expr;
1041 return nullptr;
1042 }
1043
1044 const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
1045 auto expr = visitBaseExpr_skipResultType( node,
1046 new CompoundLiteralExpr(
1047 get<Type>().accept1(node->result),
1048 get<Initializer>().accept1(node->init)
1049 )
1050 );
1051 this->node = expr;
1052 return nullptr;
1053 }
1054
1055 const ast::Expr * visit( const ast::RangeExpr * node ) override final {
1056 auto expr = visitBaseExpr( node,
1057 new RangeExpr(
1058 get<Expression>().accept1(node->low),
1059 get<Expression>().accept1(node->high)
1060 )
1061 );
1062 this->node = expr;
1063 return nullptr;
1064 }
1065
1066 const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
1067 auto expr = visitBaseExpr( node,
1068 new UntypedTupleExpr(
1069 get<Expression>().acceptL(node->exprs)
1070 )
1071 );
1072 this->node = expr;
1073 return nullptr;
1074 }
1075
1076 const ast::Expr * visit( const ast::TupleExpr * node ) override final {
1077 auto expr = visitBaseExpr( node,
1078 new TupleExpr(
1079 get<Expression>().acceptL(node->exprs)
1080 )
1081 );
1082 this->node = expr;
1083 return nullptr;
1084 }
1085
1086 const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
1087 auto expr = visitBaseExpr( node,
1088 new TupleIndexExpr(
1089 get<Expression>().accept1(node->tuple),
1090 node->index
1091 )
1092 );
1093 this->node = expr;
1094 return nullptr;
1095 }
1096
1097 const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
1098 auto expr = visitBaseExpr( node,
1099 new TupleAssignExpr(
1100 get<StmtExpr>().accept1(node->stmtExpr)
1101 )
1102 );
1103 this->node = expr;
1104 return nullptr;
1105 }
1106
1107 const ast::Expr * visit( const ast::StmtExpr * node ) override final {
1108 auto rslt = new StmtExpr(
1109 get<CompoundStmt>().accept1(node->stmts)
1110 );
1111
1112 rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
1113 rslt->dtors = get<Expression>().acceptL(node->dtors);
1114
1115 // is this even used after convert?
1116 //if (tmp->resultExpr) {
1117 // // this MUST be found by children visit
1118 // rslt->resultExpr = strict_dynamic_cast<ExprStmt *>(readonlyCache.at(tmp->resultExpr));
1119 //}
1120
1121 auto expr = visitBaseExpr( node, rslt );
1122 this->node = expr;
1123 return nullptr;
1124 }
1125
1126 const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
1127 auto rslt = new UniqueExpr(
1128 get<Expression>().accept1(node->expr),
1129 node->id
1130 );
1131
1132 rslt->object = get<ObjectDecl> ().accept1(node->object);
1133 rslt->var = get<VariableExpr>().accept1(node->var);
1134
1135 auto expr = visitBaseExpr( node, rslt );
1136 this->node = expr->clone();
1137 return nullptr;
1138 }
1139
1140 const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
1141 std::list<InitAlternative> initAlts;
1142 for (auto ia : node->initAlts) {
1143 initAlts.push_back(InitAlternative(
1144 get<Type> ().accept1(ia.type),
1145 get<Designation>().accept1(ia.designation)
1146 ));
1147 }
1148 auto expr = visitBaseExpr( node,
1149 new UntypedInitExpr(
1150 get<Expression>().accept1(node->expr),
1151 initAlts
1152 )
1153 );
1154 this->node = expr;
1155 return nullptr;
1156 }
1157
1158 const ast::Expr * visit( const ast::InitExpr * node ) override final {
1159 auto expr = visitBaseExpr( node,
1160 new InitExpr(
1161 get<Expression>().accept1(node->expr),
1162 get<Designation>().accept1(node->designation)
1163 )
1164 );
1165 this->node = expr;
1166 return nullptr;
1167 }
1168
1169 const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
1170 auto expr = visitBaseExpr( node,
1171 new DeletedExpr(
1172 get<Expression>().accept1(node->expr),
1173 inCache(node->deleteStmt) ?
1174 strict_dynamic_cast<Declaration*>(this->node) :
1175 get<Declaration>().accept1(node->deleteStmt)
1176 )
1177 );
1178 this->node = expr;
1179 return nullptr;
1180 }
1181
1182 const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
1183 auto expr = visitBaseExpr( node,
1184 new DefaultArgExpr(
1185 get<Expression>().accept1(node->expr)
1186 )
1187 );
1188 this->node = expr;
1189 return nullptr;
1190 }
1191
1192 const ast::Expr * visit( const ast::GenericExpr * node ) override final {
1193 std::list<GenericExpr::Association> associations;
1194 for (auto association : node->associations) {
1195 associations.push_back(GenericExpr::Association(
1196 get<Type> ().accept1(association.type),
1197 get<Expression>().accept1(association.expr)
1198 ));
1199 }
1200 auto expr = visitBaseExpr( node,
1201 new GenericExpr(
1202 get<Expression>().accept1(node->control),
1203 associations
1204 )
1205 );
1206 this->node = expr;
1207 return nullptr;
1208 }
1209
1210 const ast::Type * visitType( const ast::Type * node, Type * type ) {
1211 // Some types do this in their constructor so add a check.
1212 if ( !node->attributes.empty() && type->attributes.empty() ) {
1213 type->attributes = get<Attribute>().acceptL( node->attributes );
1214 }
1215 this->node = type;
1216 return nullptr;
1217 }
1218
1219 const ast::Type * visit( const ast::VoidType * node ) override final {
1220 return visitType( node, new VoidType{ cv( node ) } );
1221 }
1222
1223 const ast::Type * visit( const ast::BasicType * node ) override final {
1224 auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
1225 // I believe this should always be a BasicType.
1226 if ( ast::sizeType == node ) {
1227 Validate::SizeType = type;
1228 }
1229 return visitType( node, type );
1230 }
1231
1232 const ast::Type * visit( const ast::PointerType * node ) override final {
1233 return visitType( node, new PointerType{
1234 cv( node ),
1235 get<Type>().accept1( node->base ),
1236 get<Expression>().accept1( node->dimension ),
1237 (bool)node->isVarLen,
1238 (bool)node->isStatic
1239 } );
1240 }
1241
1242 const ast::Type * visit( const ast::ArrayType * node ) override final {
1243 return visitType( node, new ArrayType{
1244 cv( node ),
1245 get<Type>().accept1( node->base ),
1246 get<Expression>().accept1( node->dimension ),
1247 (bool)node->isVarLen,
1248 (bool)node->isStatic
1249 } );
1250 }
1251
1252 const ast::Type * visit( const ast::ReferenceType * node ) override final {
1253 return visitType( node, new ReferenceType{
1254 cv( node ),
1255 get<Type>().accept1( node->base )
1256 } );
1257 }
1258
1259 const ast::Type * visit( const ast::QualifiedType * node ) override final {
1260 return visitType( node, new QualifiedType{
1261 cv( node ),
1262 get<Type>().accept1( node->parent ),
1263 get<Type>().accept1( node->child )
1264 } );
1265 }
1266
1267 const ast::Type * visit( const ast::FunctionType * node ) override final {
1268 static std::string dummy_paramvar_prefix = "__param_";
1269 static std::string dummy_returnvar_prefix = "__retval_";
1270
1271 auto ty = new FunctionType {
1272 cv( node ),
1273 (bool)node->isVarArgs
1274 };
1275 auto returns = get<Type>().acceptL(node->returns);
1276 auto params = get<Type>().acceptL(node->params);
1277
1278 int ret_index = 0;
1279 for (auto t: returns) {
1280 // xxx - LinkageSpec shouldn't matter but needs to be something
1281 ObjectDecl * dummy = new ObjectDecl(dummy_returnvar_prefix + std::to_string(ret_index++), {}, LinkageSpec::C, nullptr, t, nullptr);
1282 ty->returnVals.push_back(dummy);
1283 }
1284 int param_index = 0;
1285 for (auto t: params) {
1286 ObjectDecl * dummy = new ObjectDecl(dummy_paramvar_prefix + std::to_string(param_index++), {}, LinkageSpec::C, nullptr, t, nullptr);
1287 ty->parameters.push_back(dummy);
1288 }
1289
1290 // ty->returnVals = get<DeclarationWithType>().acceptL( node->returns );
1291 // ty->parameters = get<DeclarationWithType>().acceptL( node->params );
1292
1293 auto types = get<TypeInstType>().acceptL( node->forall );
1294 for (auto t : types) {
1295 auto newT = new TypeDecl(*t->baseType);
1296 newT->name = t->name; // converted by typeString()
1297 for (auto asst : newT->assertions) delete asst;
1298 newT->assertions.clear();
1299 ty->forall.push_back(newT);
1300 }
1301 auto assts = get<VariableExpr>().acceptL( node->assertions );
1302 if (!assts.empty()) {
1303 assert(!types.empty());
1304 for (auto asst : assts) {
1305 auto newDecl = new ObjectDecl(*strict_dynamic_cast<ObjectDecl*>(asst->var));
1306 delete newDecl->type;
1307 newDecl->type = asst->result->clone();
1308 newDecl->storageClasses.is_extern = true; // hack
1309 ty->forall.back()->assertions.push_back(newDecl);
1310 }
1311 }
1312
1313 return visitType( node, ty );
1314 }
1315
1316 const ast::Type * postvisit( const ast::BaseInstType * old, ReferenceToType * ty ) {
1317 ty->parameters = get<Expression>().acceptL( old->params );
1318 ty->hoistType = old->hoistType;
1319 return visitType( old, ty );
1320 }
1321
1322 const ast::Type * visit( const ast::StructInstType * node ) override final {
1323 StructInstType * ty;
1324 if ( node->base ) {
1325 ty = new StructInstType{
1326 cv( node ),
1327 get<StructDecl>().accept1( node->base ),
1328 get<Attribute>().acceptL( node->attributes )
1329 };
1330 } else {
1331 ty = new StructInstType{
1332 cv( node ),
1333 node->name,
1334 get<Attribute>().acceptL( node->attributes )
1335 };
1336 }
1337 return postvisit( node, ty );
1338 }
1339
1340 const ast::Type * visit( const ast::UnionInstType * node ) override final {
1341 UnionInstType * ty;
1342 if ( node->base ) {
1343 ty = new UnionInstType{
1344 cv( node ),
1345 get<UnionDecl>().accept1( node->base ),
1346 get<Attribute>().acceptL( node->attributes )
1347 };
1348 } else {
1349 ty = new UnionInstType{
1350 cv( node ),
1351 node->name,
1352 get<Attribute>().acceptL( node->attributes )
1353 };
1354 }
1355 return postvisit( node, ty );
1356 }
1357
1358 const ast::Type * visit( const ast::EnumInstType * node ) override final {
1359 EnumInstType * ty;
1360 if ( node->base ) {
1361 ty = new EnumInstType{
1362 cv( node ),
1363 get<EnumDecl>().accept1( node->base ),
1364 get<Attribute>().acceptL( node->attributes )
1365 };
1366 } else {
1367 ty = new EnumInstType{
1368 cv( node ),
1369 node->name,
1370 get<Attribute>().acceptL( node->attributes )
1371 };
1372 }
1373 return postvisit( node, ty );
1374 }
1375
1376 const ast::Type * visit( const ast::TraitInstType * node ) override final {
1377 TraitInstType * ty;
1378 if ( node->base ) {
1379 ty = new TraitInstType{
1380 cv( node ),
1381 get<TraitDecl>().accept1( node->base ),
1382 get<Attribute>().acceptL( node->attributes )
1383 };
1384 } else {
1385 ty = new TraitInstType{
1386 cv( node ),
1387 node->name,
1388 get<Attribute>().acceptL( node->attributes )
1389 };
1390 }
1391 return postvisit( node, ty );
1392 }
1393
1394 const ast::Type * visit( const ast::TypeInstType * node ) override final {
1395 TypeInstType * ty;
1396 if ( node->base ) {
1397 ty = new TypeInstType{
1398 cv( node ),
1399 node->typeString(),
1400 get<TypeDecl>().accept1( node->base ),
1401 get<Attribute>().acceptL( node->attributes )
1402 };
1403 } else {
1404 ty = new TypeInstType{
1405 cv( node ),
1406 node->typeString(),
1407 node->kind == ast::TypeDecl::Ftype,
1408 get<Attribute>().acceptL( node->attributes )
1409 };
1410 }
1411 return postvisit( node, ty );
1412 }
1413
1414 const ast::Type * visit( const ast::TupleType * node ) override final {
1415 return visitType( node, new TupleType{
1416 cv( node ),
1417 get<Type>().acceptL( node->types )
1418 // members generated by TupleType c'tor
1419 } );
1420 }
1421
1422 const ast::Type * visit( const ast::TypeofType * node ) override final {
1423 return visitType( node, new TypeofType{
1424 cv( node ),
1425 get<Expression>().accept1( node->expr ),
1426 (bool)node->kind
1427 } );
1428 }
1429
1430 const ast::Type * visit( const ast::VTableType * node ) override final {
1431 return visitType( node, new VTableType{
1432 cv( node ),
1433 get<Type>().accept1( node->base )
1434 } );
1435 }
1436
1437 const ast::Type * visit( const ast::VarArgsType * node ) override final {
1438 return visitType( node, new VarArgsType{ cv( node ) } );
1439 }
1440
1441 const ast::Type * visit( const ast::ZeroType * node ) override final {
1442 return visitType( node, new ZeroType{ cv( node ) } );
1443 }
1444
1445 const ast::Type * visit( const ast::OneType * node ) override final {
1446 return visitType( node, new OneType{ cv( node ) } );
1447 }
1448
1449 const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
1450 return visitType( node, new GlobalScopeType{} );
1451 }
1452
1453 const ast::Designation * visit( const ast::Designation * node ) override final {
1454 auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
1455 designation->location = node->location;
1456 this->node = designation;
1457 return nullptr;
1458 }
1459
1460 const ast::Init * visit( const ast::SingleInit * node ) override final {
1461 auto init = new SingleInit(
1462 get<Expression>().accept1( node->value ),
1463 ast::MaybeConstruct == node->maybeConstructed
1464 );
1465 init->location = node->location;
1466 this->node = init;
1467 return nullptr;
1468 }
1469
1470 const ast::Init * visit( const ast::ListInit * node ) override final {
1471 auto init = new ListInit(
1472 get<Initializer>().acceptL( node->initializers ),
1473 get<Designation>().acceptL( node->designations ),
1474 ast::MaybeConstruct == node->maybeConstructed
1475 );
1476 init->location = node->location;
1477 this->node = init;
1478 return nullptr;
1479 }
1480
1481 const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1482 auto init = new ConstructorInit(
1483 get<Statement>().accept1( node->ctor ),
1484 get<Statement>().accept1( node->dtor ),
1485 get<Initializer>().accept1( node->init )
1486 );
1487 init->location = node->location;
1488 this->node = init;
1489 return nullptr;
1490 }
1491
1492 const ast::Attribute * visit( const ast::Attribute * node ) override final {
1493 auto attr = new Attribute(
1494 node->name,
1495 get<Expression>().acceptL(node->params)
1496 );
1497 this->node = attr;
1498 return nullptr;
1499 }
1500
1501 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1502 // Handled by convertTypeSubstitution helper instead.
1503 // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
1504 assert( 0 );
1505 (void)node;
1506 return nullptr;
1507 }
1508};
1509
1510std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ) {
1511 // Copy values from the global store to the local static variables.
1512 ast::sizeType = translationUnit.global.sizeType;
1513 ast::dereferenceOperator = translationUnit.global.dereference;
1514 ast::dtorStruct = translationUnit.global.dtorStruct;
1515 ast::dtorStructDestroy = translationUnit.global.dtorDestroy;
1516
1517 ConverterNewToOld c;
1518 std::list< Declaration * > decls;
1519 for(auto d : translationUnit.decls) {
1520 decls.emplace_back( c.decl( d ) );
1521 }
1522 return decls;
1523}
1524
1525//================================================================================================
1526
1527class ConverterOldToNew : public Visitor {
1528public:
1529 ast::Decl * decl() {
1530 return strict_dynamic_cast< ast::Decl * >( node );
1531 }
1532
1533 ConverterOldToNew() = default;
1534 ConverterOldToNew(const ConverterOldToNew &) = delete;
1535 ConverterOldToNew(ConverterOldToNew &&) = delete;
1536private:
1537 /// conversion output
1538 ast::Node * node = nullptr;
1539 /// cache of nodes that might be referenced by readonly<> for de-duplication
1540 /// in case that some nodes are dropped by conversion (due to possible structural change)
1541 /// use smart pointers in cache value to prevent accidental invalidation.
1542 /// at conversion stage, all created nodes are guaranteed to be unique, therefore
1543 /// const_casting out of smart pointers is permitted.
1544 std::unordered_map< const BaseSyntaxNode *, ast::readonly<ast::Node> > cache = {};
1545
1546 // Local Utilities:
1547
1548 template<typename NewT, typename OldT>
1549 NewT * getAccept1( OldT old ) {
1550 if ( ! old ) return nullptr;
1551 old->accept(*this);
1552 ast::Node * ret = node;
1553 node = nullptr;
1554 return strict_dynamic_cast< NewT * >( ret );
1555 }
1556
1557# define GET_ACCEPT_1(child, type) \
1558 getAccept1< ast::type, decltype( old->child ) >( old->child )
1559
1560
1561 template<typename NewT, typename OldC>
1562 std::vector< ast::ptr<NewT> > getAcceptV( const OldC& old ) {
1563 std::vector< ast::ptr<NewT> > ret;
1564 ret.reserve( old.size() );
1565 for ( auto a : old ) {
1566 a->accept( *this );
1567 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1568 node = nullptr;
1569 }
1570 return ret;
1571 }
1572
1573# define GET_ACCEPT_V(child, type) \
1574 getAcceptV< ast::type, decltype( old->child ) >( old->child )
1575
1576# define GET_ACCEPT_E(child, type) \
1577 getAccept1< ast::type, decltype( old->base ) >( old->base )
1578
1579 template<typename NewT, typename OldC>
1580 std::deque< ast::ptr<NewT> > getAcceptD( const OldC& old ) {
1581 std::deque< ast::ptr<NewT> > ret;
1582 for ( auto a : old ) {
1583 a->accept( *this );
1584 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1585 node = nullptr;
1586 }
1587 return ret;
1588 }
1589
1590# define GET_ACCEPT_D(child, type) \
1591 getAcceptD< ast::type, decltype( old->child ) >( old->child )
1592
1593 ast::Label make_label(const Label* old) {
1594 CodeLocation const & location =
1595 ( old->labelled ) ? old->labelled->location : CodeLocation();
1596 return ast::Label(
1597 location,
1598 old->name,
1599 GET_ACCEPT_V(attributes, Attribute)
1600 );
1601 }
1602
1603 template<template <class...> class C>
1604 C<ast::Label> make_labels(C<Label> olds) {
1605 C<ast::Label> ret;
1606 for (auto oldn : olds) {
1607 ret.push_back( make_label( &oldn ) );
1608 }
1609 return ret;
1610 }
1611
1612# define GET_LABELS_V(labels) \
1613 to<std::vector>::from( make_labels( std::move( labels ) ) )
1614
1615 static ast::CV::Qualifiers cv( const Type * ty ) { return { ty->tq.val }; }
1616
1617 /// returns true and sets `node` if in cache
1618 bool inCache( const BaseSyntaxNode * old ) {
1619 auto it = cache.find( old );
1620 if ( it == cache.end() ) return false;
1621 node = const_cast<ast::Node *>(it->second.get());
1622 return true;
1623 }
1624
1625 // Now all the visit functions:
1626
1627 virtual void visit( const ObjectDecl * old ) override final {
1628 if ( inCache( old ) ) {
1629 return;
1630 }
1631 auto&& type = GET_ACCEPT_1(type, Type);
1632 auto&& init = GET_ACCEPT_1(init, Init);
1633 auto&& bfwd = GET_ACCEPT_1(bitfieldWidth, Expr);
1634 auto&& attr = GET_ACCEPT_V(attributes, Attribute);
1635
1636 auto decl = new ast::ObjectDecl(
1637 old->location,
1638 old->name,
1639 type,
1640 init,
1641 { old->get_storageClasses().val },
1642 { old->linkage.val },
1643 bfwd,
1644 std::move(attr),
1645 { old->get_funcSpec().val }
1646 );
1647 cache.emplace(old, decl);
1648 assert(cache.find( old ) != cache.end());
1649 decl->scopeLevel = old->scopeLevel;
1650 decl->mangleName = old->mangleName;
1651 decl->isDeleted = old->isDeleted;
1652 decl->asmName = GET_ACCEPT_1(asmName, Expr);
1653 decl->uniqueId = old->uniqueId;
1654 decl->extension = old->extension;
1655
1656 this->node = decl;
1657 }
1658
1659 virtual void visit( const FunctionDecl * old ) override final {
1660 if ( inCache( old ) ) return;
1661 auto paramVars = GET_ACCEPT_V(type->parameters, DeclWithType);
1662 auto returnVars = GET_ACCEPT_V(type->returnVals, DeclWithType);
1663 auto forall = GET_ACCEPT_V(type->forall, TypeDecl);
1664
1665 // function type is now derived from parameter decls instead of storing them
1666
1667 /*
1668 auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
1669 ftype->params.reserve(paramVars.size());
1670 ftype->returns.reserve(returnVars.size());
1671
1672 for (auto & v: paramVars) {
1673 ftype->params.emplace_back(v->get_type());
1674 }
1675 for (auto & v: returnVars) {
1676 ftype->returns.emplace_back(v->get_type());
1677 }
1678 ftype->forall = std::move(forall);
1679 */
1680
1681 // can function type have attributes? seems not to be the case.
1682 // visitType(old->type, ftype);
1683
1684 // collect assertions and put directly in FunctionDecl
1685 std::vector<ast::ptr<ast::DeclWithType>> assertions;
1686 for (auto & param: forall) {
1687 for (auto & asst: param->assertions) {
1688 assertf(asst->unique(), "newly converted decl must be unique");
1689 assertions.emplace_back(asst);
1690 }
1691 auto mut = param.get_and_mutate();
1692 assertf(mut == param, "newly converted decl must be unique");
1693 mut->assertions.clear();
1694 }
1695
1696 auto decl = new ast::FunctionDecl{
1697 old->location,
1698 old->name,
1699 // GET_ACCEPT_1(type, FunctionType),
1700 std::move(forall),
1701 std::move(assertions),
1702 std::move(paramVars),
1703 std::move(returnVars),
1704 {},
1705 { old->storageClasses.val },
1706 { old->linkage.val },
1707 GET_ACCEPT_V(attributes, Attribute),
1708 { old->get_funcSpec().val },
1709 (old->type->isVarArgs) ? ast::VariableArgs : ast::FixedArgs
1710 };
1711
1712 // decl->type = ftype;
1713 cache.emplace( old, decl );
1714
1715 decl->withExprs = GET_ACCEPT_V(withExprs, Expr);
1716 decl->stmts = GET_ACCEPT_1(statements, CompoundStmt);
1717 decl->scopeLevel = old->scopeLevel;
1718 decl->mangleName = old->mangleName;
1719 decl->isDeleted = old->isDeleted;
1720 decl->asmName = GET_ACCEPT_1(asmName, Expr);
1721 decl->uniqueId = old->uniqueId;
1722 decl->extension = old->extension;
1723
1724 this->node = decl;
1725
1726 if ( Validate::dereferenceOperator == old ) {
1727 ast::dereferenceOperator = decl;
1728 }
1729
1730 if ( Validate::dtorStructDestroy == old ) {
1731 ast::dtorStructDestroy = decl;
1732 }
1733 }
1734
1735 virtual void visit( const StructDecl * old ) override final {
1736 if ( inCache( old ) ) return;
1737 auto decl = new ast::StructDecl(
1738 old->location,
1739 old->name,
1740 (ast::AggregateDecl::Aggregate)old->kind,
1741 GET_ACCEPT_V(attributes, Attribute),
1742 { old->linkage.val }
1743 );
1744 cache.emplace( old, decl );
1745 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1746 decl->body = old->body;
1747 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1748 decl->members = GET_ACCEPT_V(members, Decl);
1749 decl->extension = old->extension;
1750 decl->uniqueId = old->uniqueId;
1751 decl->storage = { old->storageClasses.val };
1752
1753 this->node = decl;
1754
1755 if ( Validate::dtorStruct == old ) {
1756 ast::dtorStruct = decl;
1757 }
1758 }
1759
1760 virtual void visit( const UnionDecl * old ) override final {
1761 if ( inCache( old ) ) return;
1762 auto decl = new ast::UnionDecl(
1763 old->location,
1764 old->name,
1765 GET_ACCEPT_V(attributes, Attribute),
1766 { old->linkage.val }
1767 );
1768 cache.emplace( old, decl );
1769 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1770 decl->body = old->body;
1771 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1772 decl->members = GET_ACCEPT_V(members, Decl);
1773 decl->extension = old->extension;
1774 decl->uniqueId = old->uniqueId;
1775 decl->storage = { old->storageClasses.val };
1776
1777 this->node = decl;
1778 }
1779
1780
1781 virtual void visit( const EnumDecl * old ) override final {
1782 if ( inCache( old ) ) return;
1783 auto decl = new ast::EnumDecl(
1784 old->location,
1785 old->name,
1786 old->isTyped,
1787 GET_ACCEPT_V(attributes, Attribute),
1788 { old->linkage.val },
1789 GET_ACCEPT_1(base, Type),
1790 old->hide == EnumDecl::EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible,
1791 old->enumValues
1792 );
1793 cache.emplace( old, decl );
1794 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1795 decl->body = old->body;
1796 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1797 decl->members = GET_ACCEPT_V(members, Decl);
1798 decl->extension = old->extension;
1799 decl->uniqueId = old->uniqueId;
1800 decl->storage = { old->storageClasses.val };
1801 this->node = decl;
1802 }
1803
1804 virtual void visit( const TraitDecl * old ) override final {
1805 if ( inCache( old ) ) return;
1806 auto decl = new ast::TraitDecl(
1807 old->location,
1808 old->name,
1809 GET_ACCEPT_V(attributes, Attribute),
1810 { old->linkage.val }
1811 );
1812 cache.emplace( old, decl );
1813 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1814 decl->body = old->body;
1815 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1816 decl->members = GET_ACCEPT_V(members, Decl);
1817 decl->extension = old->extension;
1818 decl->uniqueId = old->uniqueId;
1819 decl->storage = { old->storageClasses.val };
1820
1821 this->node = decl;
1822 }
1823
1824 virtual void visit( const TypeDecl * old ) override final {
1825 if ( inCache( old ) ) return;
1826 auto decl = new ast::TypeDecl{
1827 old->location,
1828 old->name,
1829 { old->storageClasses.val },
1830 GET_ACCEPT_1(base, Type),
1831 (ast::TypeDecl::Kind)(unsigned)old->kind,
1832 old->sized,
1833 GET_ACCEPT_1(init, Type)
1834 };
1835 cache.emplace( old, decl );
1836 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1837 decl->extension = old->extension;
1838 decl->uniqueId = old->uniqueId;
1839
1840 this->node = decl;
1841 }
1842
1843 virtual void visit( const TypedefDecl * old ) override final {
1844 auto decl = new ast::TypedefDecl(
1845 old->location,
1846 old->name,
1847 { old->storageClasses.val },
1848 GET_ACCEPT_1(base, Type),
1849 { old->linkage.val }
1850 );
1851 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1852 decl->extension = old->extension;
1853 decl->uniqueId = old->uniqueId;
1854 decl->storage = { old->storageClasses.val };
1855
1856 this->node = decl;
1857 }
1858
1859 virtual void visit( const AsmDecl * old ) override final {
1860 auto decl = new ast::AsmDecl{
1861 old->location,
1862 GET_ACCEPT_1(stmt, AsmStmt)
1863 };
1864 decl->extension = old->extension;
1865 decl->uniqueId = old->uniqueId;
1866 decl->storage = { old->storageClasses.val };
1867
1868 this->node = decl;
1869 }
1870
1871 virtual void visit( const DirectiveDecl * old ) override final {
1872 auto decl = new ast::DirectiveDecl{
1873 old->location,
1874 GET_ACCEPT_1(stmt, DirectiveStmt)
1875 };
1876 decl->extension = old->extension;
1877 decl->uniqueId = old->uniqueId;
1878 decl->storage = { old->storageClasses.val };
1879
1880 this->node = decl;
1881 }
1882
1883 virtual void visit( const StaticAssertDecl * old ) override final {
1884 auto decl = new ast::StaticAssertDecl{
1885 old->location,
1886 GET_ACCEPT_1(condition, Expr),
1887 GET_ACCEPT_1(message, ConstantExpr)
1888 };
1889 decl->extension = old->extension;
1890 decl->uniqueId = old->uniqueId;
1891 decl->storage = { old->storageClasses.val };
1892
1893 this->node = decl;
1894 }
1895
1896 virtual void visit( const InlineMemberDecl * old ) override final {
1897 if ( inCache( old ) ) {
1898 return;
1899 }
1900 auto&& type = GET_ACCEPT_1(type, Type);
1901 auto&& attr = GET_ACCEPT_V(attributes, Attribute);
1902
1903 auto decl = new ast::InlineMemberDecl(
1904 old->location,
1905 old->name,
1906 type,
1907 { old->get_storageClasses().val },
1908 { old->linkage.val },
1909 std::move(attr),
1910 { old->get_funcSpec().val }
1911 );
1912 cache.emplace(old, decl);
1913 assert(cache.find( old ) != cache.end());
1914 decl->scopeLevel = old->scopeLevel;
1915 decl->mangleName = old->mangleName;
1916 decl->isDeleted = old->isDeleted;
1917 decl->asmName = GET_ACCEPT_1(asmName, Expr);
1918 decl->uniqueId = old->uniqueId;
1919 decl->extension = old->extension;
1920
1921 this->node = decl;
1922 }
1923
1924 virtual void visit( const CompoundStmt * old ) override final {
1925 if ( inCache( old ) ) return;
1926 auto stmt = new ast::CompoundStmt(
1927 old->location,
1928 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1929 GET_LABELS_V(old->labels)
1930 );
1931
1932 this->node = stmt;
1933 cache.emplace( old, this->node );
1934 }
1935
1936 virtual void visit( const ExprStmt * old ) override final {
1937 if ( inCache( old ) ) return;
1938 this->node = new ast::ExprStmt(
1939 old->location,
1940 GET_ACCEPT_1(expr, Expr),
1941 GET_LABELS_V(old->labels)
1942 );
1943 cache.emplace( old, this->node );
1944 }
1945
1946 virtual void visit( const AsmStmt * old ) override final {
1947 if ( inCache( old ) ) return;
1948 this->node = new ast::AsmStmt(
1949 old->location,
1950 old->voltile,
1951 GET_ACCEPT_1(instruction, Expr),
1952 GET_ACCEPT_V(output, Expr),
1953 GET_ACCEPT_V(input, Expr),
1954 GET_ACCEPT_V(clobber, ConstantExpr),
1955 GET_LABELS_V(old->gotolabels),
1956 GET_LABELS_V(old->labels)
1957 );
1958 cache.emplace( old, this->node );
1959 }
1960
1961 virtual void visit( const DirectiveStmt * old ) override final {
1962 if ( inCache( old ) ) return;
1963 this->node = new ast::DirectiveStmt(
1964 old->location,
1965 old->directive,
1966 GET_LABELS_V(old->labels)
1967 );
1968 cache.emplace( old, this->node );
1969 }
1970
1971 virtual void visit( const IfStmt * old ) override final {
1972 if ( inCache( old ) ) return;
1973 this->node = new ast::IfStmt(
1974 old->location,
1975 GET_ACCEPT_1(condition, Expr),
1976 GET_ACCEPT_1(then, Stmt),
1977 GET_ACCEPT_1(else_, Stmt),
1978 GET_ACCEPT_V(initialization, Stmt),
1979 GET_LABELS_V(old->labels)
1980 );
1981 cache.emplace( old, this->node );
1982 }
1983
1984 virtual void visit( const SwitchStmt * old ) override final {
1985 if ( inCache( old ) ) return;
1986 this->node = new ast::SwitchStmt(
1987 old->location,
1988 GET_ACCEPT_1(condition, Expr),
1989 GET_ACCEPT_V(statements, CaseClause),
1990 GET_LABELS_V(old->labels)
1991 );
1992 cache.emplace( old, this->node );
1993 }
1994
1995 virtual void visit( const CaseStmt * old ) override final {
1996 if ( inCache( old ) ) return;
1997 this->node = new ast::CaseClause(
1998 old->location,
1999 GET_ACCEPT_1(condition, Expr),
2000 GET_ACCEPT_V(stmts, Stmt)
2001 );
2002 auto labels = GET_LABELS_V(old->labels);
2003 assertf(labels.empty(), "Labels found on CaseStmt.");
2004 cache.emplace( old, this->node );
2005 }
2006
2007 virtual void visit( const WhileDoStmt * old ) override final {
2008 if ( inCache( old ) ) return;
2009 this->node = new ast::WhileDoStmt(
2010 old->location,
2011 GET_ACCEPT_1(condition, Expr),
2012 GET_ACCEPT_1(body, Stmt),
2013 GET_ACCEPT_1(else_, Stmt),
2014 GET_ACCEPT_V(initialization, Stmt),
2015 (old->isDoWhile) ? ast::DoWhile : ast::While,
2016 GET_LABELS_V(old->labels)
2017 );
2018 cache.emplace( old, this->node );
2019 }
2020
2021 virtual void visit( const ForStmt * old ) override final {
2022 if ( inCache( old ) ) return;
2023 this->node = new ast::ForStmt(
2024 old->location,
2025 GET_ACCEPT_V(initialization, Stmt),
2026 GET_ACCEPT_1(condition, Expr),
2027 GET_ACCEPT_1(increment, Expr),
2028 GET_ACCEPT_1(body, Stmt),
2029 GET_ACCEPT_1(else_, Stmt),
2030 GET_LABELS_V(old->labels)
2031 );
2032 cache.emplace( old, this->node );
2033 }
2034
2035 virtual void visit( const BranchStmt * old ) override final {
2036 if ( inCache( old ) ) return;
2037 if (old->computedTarget) {
2038 this->node = new ast::BranchStmt(
2039 old->location,
2040 GET_ACCEPT_1(computedTarget, Expr),
2041 GET_LABELS_V(old->labels)
2042 );
2043 } else {
2044 ast::BranchStmt::Kind kind;
2045 switch (old->type) {
2046 #define CASE(n) \
2047 case BranchStmt::n: \
2048 kind = ast::BranchStmt::n; \
2049 break
2050 CASE(Goto);
2051 CASE(Break);
2052 CASE(Continue);
2053 CASE(FallThrough);
2054 CASE(FallThroughDefault);
2055 #undef CASE
2056 default:
2057 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
2058 }
2059
2060 auto stmt = new ast::BranchStmt(
2061 old->location,
2062 kind,
2063 make_label(&old->originalTarget),
2064 GET_LABELS_V(old->labels)
2065 );
2066 stmt->target = make_label(&old->target);
2067 this->node = stmt;
2068 }
2069 cache.emplace( old, this->node );
2070 }
2071
2072 virtual void visit( const ReturnStmt * old ) override final {
2073 if ( inCache( old ) ) return;
2074 this->node = new ast::ReturnStmt(
2075 old->location,
2076 GET_ACCEPT_1(expr, Expr),
2077 GET_LABELS_V(old->labels)
2078 );
2079 cache.emplace( old, this->node );
2080 }
2081
2082 virtual void visit( const ThrowStmt * old ) override final {
2083 if ( inCache( old ) ) return;
2084 ast::ExceptionKind kind;
2085 switch (old->kind) {
2086 case ThrowStmt::Terminate:
2087 kind = ast::ExceptionKind::Terminate;
2088 break;
2089 case ThrowStmt::Resume:
2090 kind = ast::ExceptionKind::Resume;
2091 break;
2092 default:
2093 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
2094 }
2095
2096 this->node = new ast::ThrowStmt(
2097 old->location,
2098 kind,
2099 GET_ACCEPT_1(expr, Expr),
2100 GET_ACCEPT_1(target, Expr),
2101 GET_LABELS_V(old->labels)
2102 );
2103 cache.emplace( old, this->node );
2104 }
2105
2106 virtual void visit( const TryStmt * old ) override final {
2107 if ( inCache( old ) ) return;
2108 this->node = new ast::TryStmt(
2109 old->location,
2110 GET_ACCEPT_1(block, CompoundStmt),
2111 GET_ACCEPT_V(handlers, CatchClause),
2112 GET_ACCEPT_1(finallyBlock, FinallyClause),
2113 GET_LABELS_V(old->labels)
2114 );
2115 cache.emplace( old, this->node );
2116 }
2117
2118 virtual void visit( const CatchStmt * old ) override final {
2119 if ( inCache( old ) ) return;
2120 ast::ExceptionKind kind;
2121 switch (old->kind) {
2122 case CatchStmt::Terminate:
2123 kind = ast::ExceptionKind::Terminate;
2124 break;
2125 case CatchStmt::Resume:
2126 kind = ast::ExceptionKind::Resume;
2127 break;
2128 default:
2129 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
2130 }
2131
2132 this->node = new ast::CatchClause(
2133 old->location,
2134 kind,
2135 GET_ACCEPT_1(decl, Decl),
2136 GET_ACCEPT_1(cond, Expr),
2137 GET_ACCEPT_1(body, Stmt)
2138 );
2139 auto labels = GET_LABELS_V(old->labels);
2140 assertf(labels.empty(), "Labels found on CatchStmt.");
2141 cache.emplace( old, this->node );
2142 }
2143
2144 virtual void visit( const FinallyStmt * old ) override final {
2145 if ( inCache( old ) ) return;
2146 this->node = new ast::FinallyClause(
2147 old->location,
2148 GET_ACCEPT_1(block, CompoundStmt)
2149 );
2150 auto labels = GET_LABELS_V(old->labels);
2151 assertf(labels.empty(), "Labels found on FinallyStmt.");
2152 cache.emplace( old, this->node );
2153 }
2154
2155 virtual void visit( const SuspendStmt * old ) override final {
2156 if ( inCache( old ) ) return;
2157 ast::SuspendStmt::Kind type;
2158 switch (old->type) {
2159 case SuspendStmt::Coroutine: type = ast::SuspendStmt::Coroutine; break;
2160 case SuspendStmt::Generator: type = ast::SuspendStmt::Generator; break;
2161 case SuspendStmt::None : type = ast::SuspendStmt::None ; break;
2162 default: abort();
2163 }
2164 this->node = new ast::SuspendStmt(
2165 old->location,
2166 GET_ACCEPT_1(then , CompoundStmt),
2167 type,
2168 GET_LABELS_V(old->labels)
2169 );
2170 cache.emplace( old, this->node );
2171 }
2172
2173 virtual void visit( const WaitForStmt * old ) override final {
2174 if ( inCache( old ) ) return;
2175 ast::WaitForStmt * stmt = new ast::WaitForStmt(
2176 old->location,
2177 GET_LABELS_V(old->labels)
2178 );
2179
2180 stmt->clauses.reserve( old->clauses.size() );
2181 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
2182 auto clause = new ast::WaitForClause( old->location );
2183
2184 clause->target = GET_ACCEPT_1(clauses[i].target.function, Expr);
2185 clause->target_args = GET_ACCEPT_V(clauses[i].target.arguments, Expr);
2186 clause->stmt = GET_ACCEPT_1(clauses[i].statement, Stmt);
2187 clause->when_cond = GET_ACCEPT_1(clauses[i].condition, Expr);
2188
2189 stmt->clauses.push_back( clause );
2190 }
2191 stmt->timeout_time = GET_ACCEPT_1(timeout.time, Expr);
2192 stmt->timeout_stmt = GET_ACCEPT_1(timeout.statement, Stmt);
2193 stmt->timeout_cond = GET_ACCEPT_1(timeout.condition, Expr);
2194 stmt->else_stmt = GET_ACCEPT_1(orelse.statement, Stmt);
2195 stmt->else_cond = GET_ACCEPT_1(orelse.condition, Expr);
2196
2197 this->node = stmt;
2198 cache.emplace( old, this->node );
2199 }
2200
2201 virtual void visit( const WithStmt * old ) override final {
2202 if ( inCache( old ) ) return;
2203 this->node = new ast::WithStmt(
2204 old->location,
2205 GET_ACCEPT_V(exprs, Expr),
2206 GET_ACCEPT_1(stmt, Stmt)
2207 );
2208 cache.emplace( old, this->node );
2209 }
2210
2211 virtual void visit( const NullStmt * old ) override final {
2212 if ( inCache( old ) ) return;
2213 this->node = new ast::NullStmt(
2214 old->location,
2215 GET_LABELS_V(old->labels)
2216 );
2217 cache.emplace( old, this->node );
2218 }
2219
2220 virtual void visit( const DeclStmt * old ) override final {
2221 if ( inCache( old ) ) return;
2222 this->node = new ast::DeclStmt(
2223 old->location,
2224 GET_ACCEPT_1(decl, Decl),
2225 GET_LABELS_V(old->labels)
2226 );
2227 cache.emplace( old, this->node );
2228 }
2229
2230 virtual void visit( const ImplicitCtorDtorStmt * old ) override final {
2231 if ( inCache( old ) ) return;
2232 auto stmt = new ast::ImplicitCtorDtorStmt(
2233 old->location,
2234 nullptr,
2235 GET_LABELS_V(old->labels)
2236 );
2237 cache.emplace( old, stmt );
2238 stmt->callStmt = GET_ACCEPT_1(callStmt, Stmt);
2239 this->node = stmt;
2240 }
2241
2242 virtual void visit( const MutexStmt * old ) override final {
2243 if ( inCache( old ) ) return;
2244 this->node = new ast::MutexStmt(
2245 old->location,
2246 GET_ACCEPT_1(stmt, Stmt),
2247 GET_ACCEPT_V(mutexObjs, Expr)
2248 );
2249 cache.emplace( old, this->node );
2250 }
2251
2252 // TypeSubstitution shouldn't exist yet in old.
2253 ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
2254 if (!old) return nullptr;
2255 if (old->empty()) return nullptr;
2256 assert(false);
2257
2258 /*
2259 ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
2260
2261 for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
2262 rslt->add( old_i->first,
2263 getAccept1<ast::Type>(old_i->second) );
2264 }
2265
2266 return rslt;
2267 */
2268 }
2269
2270 void convertInferUnion(ast::Expr::InferUnion &newInferred,
2271 const std::map<UniqueId,ParamEntry> &oldInferParams,
2272 const std::vector<UniqueId> &oldResnSlots) {
2273
2274 assert( oldInferParams.empty() || oldResnSlots.empty() );
2275 // assert( newInferred.mode == ast::Expr::InferUnion::Empty );
2276
2277 if ( !oldInferParams.empty() ) {
2278 ast::InferredParams &tgt = newInferred.inferParams();
2279 for (auto & old : oldInferParams) {
2280 tgt[old.first] = ast::ParamEntry(
2281 old.second.decl,
2282 getAccept1<ast::Decl>(old.second.declptr),
2283 getAccept1<ast::Type>(old.second.actualType),
2284 getAccept1<ast::Type>(old.second.formalType),
2285 getAccept1<ast::Expr>(old.second.expr)
2286 );
2287 }
2288 } else if ( !oldResnSlots.empty() ) {
2289 ast::ResnSlots &tgt = newInferred.resnSlots();
2290 for (auto old : oldResnSlots) {
2291 tgt.push_back(old);
2292 }
2293 }
2294 }
2295
2296 ast::Expr * visitBaseExpr_SkipResultType( const Expression * old, ast::Expr * nw) {
2297
2298 nw->env = convertTypeSubstitution(old->env);
2299
2300 nw->extension = old->extension;
2301 convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
2302
2303 return nw;
2304 }
2305
2306 ast::Expr * visitBaseExpr( const Expression * old, ast::Expr * nw) {
2307
2308 nw->result = GET_ACCEPT_1(result, Type);
2309 return visitBaseExpr_SkipResultType(old, nw);;
2310 }
2311
2312 virtual void visit( const ApplicationExpr * old ) override final {
2313 this->node = visitBaseExpr( old,
2314 new ast::ApplicationExpr(
2315 old->location,
2316 GET_ACCEPT_1(function, Expr),
2317 GET_ACCEPT_V(args, Expr)
2318 )
2319 );
2320 }
2321
2322 virtual void visit( const UntypedExpr * old ) override final {
2323 this->node = visitBaseExpr( old,
2324 new ast::UntypedExpr(
2325 old->location,
2326 GET_ACCEPT_1(function, Expr),
2327 GET_ACCEPT_V(args, Expr)
2328 )
2329 );
2330 }
2331
2332 virtual void visit( const NameExpr * old ) override final {
2333 this->node = visitBaseExpr( old,
2334 new ast::NameExpr(
2335 old->location,
2336 old->get_name()
2337 )
2338 );
2339 }
2340
2341 virtual void visit( const QualifiedNameExpr * old ) override final {
2342 this->node = visitBaseExpr( old,
2343 new ast::QualifiedNameExpr (
2344 old->location,
2345 GET_ACCEPT_1(type_decl, Decl),
2346 old->name
2347 )
2348 );
2349 }
2350
2351 virtual void visit( const CastExpr * old ) override final {
2352 this->node = visitBaseExpr( old,
2353 new ast::CastExpr(
2354 old->location,
2355 GET_ACCEPT_1(arg, Expr),
2356 old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast,
2357 (ast::CastExpr::CastKind) old->kind
2358 )
2359 );
2360 }
2361
2362 virtual void visit( const KeywordCastExpr * old ) override final {
2363 ast::AggregateDecl::Aggregate castTarget = (ast::AggregateDecl::Aggregate)old->target;
2364 assert( ast::AggregateDecl::Generator <= castTarget && castTarget <= ast::AggregateDecl::Thread );
2365 this->node = visitBaseExpr( old,
2366 new ast::KeywordCastExpr(
2367 old->location,
2368 GET_ACCEPT_1(arg, Expr),
2369 castTarget,
2370 {old->concrete_target.field, old->concrete_target.getter}
2371 )
2372 );
2373 }
2374
2375 virtual void visit( const VirtualCastExpr * old ) override final {
2376 this->node = visitBaseExpr_SkipResultType( old,
2377 new ast::VirtualCastExpr(
2378 old->location,
2379 GET_ACCEPT_1(arg, Expr),
2380 GET_ACCEPT_1(result, Type)
2381 )
2382 );
2383 }
2384
2385 virtual void visit( const AddressExpr * old ) override final {
2386 this->node = visitBaseExpr( old,
2387 new ast::AddressExpr(
2388 old->location,
2389 GET_ACCEPT_1(arg, Expr)
2390 )
2391 );
2392 }
2393
2394 virtual void visit( const LabelAddressExpr * old ) override final {
2395 this->node = visitBaseExpr( old,
2396 new ast::LabelAddressExpr(
2397 old->location,
2398 make_label(&old->arg)
2399 )
2400 );
2401 }
2402
2403 virtual void visit( const UntypedMemberExpr * old ) override final {
2404 this->node = visitBaseExpr( old,
2405 new ast::UntypedMemberExpr(
2406 old->location,
2407 GET_ACCEPT_1(member, Expr),
2408 GET_ACCEPT_1(aggregate, Expr)
2409 )
2410 );
2411 }
2412
2413 virtual void visit( const MemberExpr * old ) override final {
2414 this->node = visitBaseExpr( old,
2415 new ast::MemberExpr(
2416 old->location,
2417 GET_ACCEPT_1(member, DeclWithType),
2418 GET_ACCEPT_1(aggregate, Expr),
2419 ast::MemberExpr::NoOpConstructionChosen
2420 )
2421 );
2422 }
2423
2424 virtual void visit( const VariableExpr * old ) override final {
2425 auto expr = new ast::VariableExpr(
2426 old->location
2427 );
2428
2429 expr->var = GET_ACCEPT_1(var, DeclWithType);
2430 visitBaseExpr( old, expr );
2431
2432 this->node = expr;
2433 }
2434
2435 virtual void visit( const ConstantExpr * old ) override final {
2436 ast::ConstantExpr *rslt = new ast::ConstantExpr(
2437 old->location,
2438 GET_ACCEPT_1(result, Type),
2439 old->constant.rep,
2440 old->constant.ival
2441 );
2442 rslt->underlyer = getAccept1< ast::Type, Type* >( old->constant.type );
2443 this->node = visitBaseExpr( old, rslt );
2444 }
2445
2446 virtual void visit( const SizeofExpr * old ) override final {
2447 assert (old->expr || old->type);
2448 assert (! (old->expr && old->type));
2449 ast::SizeofExpr *rslt;
2450 if (old->expr) {
2451 assert(!old->isType);
2452 rslt = new ast::SizeofExpr(
2453 old->location,
2454 GET_ACCEPT_1(expr, Expr)
2455 );
2456 }
2457 if (old->type) {
2458 assert(old->isType);
2459 rslt = new ast::SizeofExpr(
2460 old->location,
2461 GET_ACCEPT_1(type, Type)
2462 );
2463 }
2464 this->node = visitBaseExpr( old, rslt );
2465 }
2466
2467 virtual void visit( const AlignofExpr * old ) override final {
2468 assert (old->expr || old->type);
2469 assert (! (old->expr && old->type));
2470 ast::AlignofExpr *rslt;
2471 if (old->expr) {
2472 assert(!old->isType);
2473 rslt = new ast::AlignofExpr(
2474 old->location,
2475 GET_ACCEPT_1(expr, Expr)
2476 );
2477 }
2478 if (old->type) {
2479 assert(old->isType);
2480 rslt = new ast::AlignofExpr(
2481 old->location,
2482 GET_ACCEPT_1(type, Type)
2483 );
2484 }
2485 this->node = visitBaseExpr( old, rslt );
2486 }
2487
2488 virtual void visit( const UntypedOffsetofExpr * old ) override final {
2489 this->node = visitBaseExpr( old,
2490 new ast::UntypedOffsetofExpr(
2491 old->location,
2492 GET_ACCEPT_1(type, Type),
2493 old->member
2494 )
2495 );
2496 }
2497
2498 virtual void visit( const OffsetofExpr * old ) override final {
2499 this->node = visitBaseExpr( old,
2500 new ast::OffsetofExpr(
2501 old->location,
2502 GET_ACCEPT_1(type, Type),
2503 GET_ACCEPT_1(member, DeclWithType)
2504 )
2505 );
2506 }
2507
2508 virtual void visit( const OffsetPackExpr * old ) override final {
2509 this->node = visitBaseExpr( old,
2510 new ast::OffsetPackExpr(
2511 old->location,
2512 GET_ACCEPT_1(type, StructInstType)
2513 )
2514 );
2515 }
2516
2517 virtual void visit( const LogicalExpr * old ) override final {
2518 this->node = visitBaseExpr( old,
2519 new ast::LogicalExpr(
2520 old->location,
2521 GET_ACCEPT_1(arg1, Expr),
2522 GET_ACCEPT_1(arg2, Expr),
2523 old->get_isAnd() ?
2524 ast::LogicalFlag::AndExpr :
2525 ast::LogicalFlag::OrExpr
2526 )
2527 );
2528 }
2529
2530 virtual void visit( const ConditionalExpr * old ) override final {
2531 this->node = visitBaseExpr( old,
2532 new ast::ConditionalExpr(
2533 old->location,
2534 GET_ACCEPT_1(arg1, Expr),
2535 GET_ACCEPT_1(arg2, Expr),
2536 GET_ACCEPT_1(arg3, Expr)
2537 )
2538 );
2539 }
2540
2541 virtual void visit( const CommaExpr * old ) override final {
2542 this->node = visitBaseExpr( old,
2543 new ast::CommaExpr(
2544 old->location,
2545 GET_ACCEPT_1(arg1, Expr),
2546 GET_ACCEPT_1(arg2, Expr)
2547 )
2548 );
2549 }
2550
2551 virtual void visit( const TypeExpr * old ) override final {
2552 this->node = visitBaseExpr( old,
2553 new ast::TypeExpr(
2554 old->location,
2555 GET_ACCEPT_1(type, Type)
2556 )
2557 );
2558 }
2559
2560 virtual void visit( const DimensionExpr * old ) override final {
2561 this->node = visitBaseExpr( old,
2562 new ast::DimensionExpr( old->location, old->name )
2563 );
2564 }
2565
2566 virtual void visit( const AsmExpr * old ) override final {
2567 this->node = visitBaseExpr( old,
2568 new ast::AsmExpr(
2569 old->location,
2570 old->inout,
2571 GET_ACCEPT_1(constraint, Expr),
2572 GET_ACCEPT_1(operand, Expr)
2573 )
2574 );
2575 }
2576
2577 virtual void visit( const ImplicitCopyCtorExpr * old ) override final {
2578 auto rslt = new ast::ImplicitCopyCtorExpr(
2579 old->location,
2580 GET_ACCEPT_1(callExpr, ApplicationExpr)
2581 );
2582
2583 this->node = visitBaseExpr( old, rslt );
2584 }
2585
2586 virtual void visit( const ConstructorExpr * old ) override final {
2587 this->node = visitBaseExpr( old,
2588 new ast::ConstructorExpr(
2589 old->location,
2590 GET_ACCEPT_1(callExpr, Expr)
2591 )
2592 );
2593 }
2594
2595 virtual void visit( const CompoundLiteralExpr * old ) override final {
2596 this->node = visitBaseExpr_SkipResultType( old,
2597 new ast::CompoundLiteralExpr(
2598 old->location,
2599 GET_ACCEPT_1(result, Type),
2600 GET_ACCEPT_1(initializer, Init)
2601 )
2602 );
2603 }
2604
2605 virtual void visit( const RangeExpr * old ) override final {
2606 this->node = visitBaseExpr( old,
2607 new ast::RangeExpr(
2608 old->location,
2609 GET_ACCEPT_1(low, Expr),
2610 GET_ACCEPT_1(high, Expr)
2611 )
2612 );
2613 }
2614
2615 virtual void visit( const UntypedTupleExpr * old ) override final {
2616 this->node = visitBaseExpr( old,
2617 new ast::UntypedTupleExpr(
2618 old->location,
2619 GET_ACCEPT_V(exprs, Expr)
2620 )
2621 );
2622 }
2623
2624 virtual void visit( const TupleExpr * old ) override final {
2625 this->node = visitBaseExpr( old,
2626 new ast::TupleExpr(
2627 old->location,
2628 GET_ACCEPT_V(exprs, Expr)
2629 )
2630 );
2631 }
2632
2633 virtual void visit( const TupleIndexExpr * old ) override final {
2634 this->node = visitBaseExpr( old,
2635 new ast::TupleIndexExpr(
2636 old->location,
2637 GET_ACCEPT_1(tuple, Expr),
2638 old->index
2639 )
2640 );
2641 }
2642
2643 virtual void visit( const TupleAssignExpr * old ) override final {
2644 this->node = visitBaseExpr_SkipResultType( old,
2645 new ast::TupleAssignExpr(
2646 old->location,
2647 GET_ACCEPT_1(result, Type),
2648 GET_ACCEPT_1(stmtExpr, StmtExpr)
2649 )
2650 );
2651 }
2652
2653 virtual void visit( const StmtExpr * old ) override final {
2654 auto rslt = new ast::StmtExpr(
2655 old->location,
2656 GET_ACCEPT_1(statements, CompoundStmt)
2657 );
2658 rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2659 rslt->dtors = GET_ACCEPT_V(dtors , Expr);
2660
2661 this->node = visitBaseExpr_SkipResultType( old, rslt );
2662 }
2663
2664 virtual void visit( const UniqueExpr * old ) override final {
2665 auto rslt = new ast::UniqueExpr(
2666 old->location,
2667 GET_ACCEPT_1(expr, Expr),
2668 old->get_id()
2669 );
2670 rslt->object = GET_ACCEPT_1(object, ObjectDecl);
2671 rslt->var = GET_ACCEPT_1(var , VariableExpr);
2672
2673 this->node = visitBaseExpr( old, rslt );
2674 }
2675
2676 virtual void visit( const UntypedInitExpr * old ) override final {
2677 std::deque<ast::InitAlternative> initAlts;
2678 for (auto ia : old->initAlts) {
2679 initAlts.push_back(ast::InitAlternative(
2680 getAccept1< ast::Type, Type * >( ia.type ),
2681 getAccept1< ast::Designation, Designation * >( ia.designation )
2682 ));
2683 }
2684 this->node = visitBaseExpr( old,
2685 new ast::UntypedInitExpr(
2686 old->location,
2687 GET_ACCEPT_1(expr, Expr),
2688 std::move(initAlts)
2689 )
2690 );
2691 }
2692
2693 virtual void visit( const InitExpr * old ) override final {
2694 this->node = visitBaseExpr( old,
2695 new ast::InitExpr(
2696 old->location,
2697 GET_ACCEPT_1(expr, Expr),
2698 GET_ACCEPT_1(designation, Designation)
2699 )
2700 );
2701 }
2702
2703 virtual void visit( const DeletedExpr * old ) override final {
2704 this->node = visitBaseExpr( old,
2705 new ast::DeletedExpr(
2706 old->location,
2707 GET_ACCEPT_1(expr, Expr),
2708 inCache(old->deleteStmt) ?
2709 strict_dynamic_cast<ast::Decl*>(this->node) :
2710 GET_ACCEPT_1(deleteStmt, Decl)
2711 )
2712 );
2713 }
2714
2715 virtual void visit( const DefaultArgExpr * old ) override final {
2716 this->node = visitBaseExpr( old,
2717 new ast::DefaultArgExpr(
2718 old->location,
2719 GET_ACCEPT_1(expr, Expr)
2720 )
2721 );
2722 }
2723
2724 virtual void visit( const GenericExpr * old ) override final {
2725 std::vector<ast::GenericExpr::Association> associations;
2726 for (auto association : old->associations) {
2727 associations.push_back(ast::GenericExpr::Association(
2728 getAccept1< ast::Type, Type * >( association.type ),
2729 getAccept1< ast::Expr, Expression * >( association.expr )
2730 ));
2731 }
2732 this->node = visitBaseExpr( old,
2733 new ast::GenericExpr(
2734 old->location,
2735 GET_ACCEPT_1(control, Expr),
2736 std::move(associations)
2737 )
2738 );
2739 }
2740
2741 void visitType( const Type * old, ast::Type * type ) {
2742 // Some types do this in their constructor so add a check.
2743 if ( !old->attributes.empty() && type->attributes.empty() ) {
2744 type->attributes = GET_ACCEPT_V(attributes, Attribute);
2745 }
2746 this->node = type;
2747 }
2748
2749 virtual void visit( const VoidType * old ) override final {
2750 visitType( old, new ast::VoidType{ cv( old ) } );
2751 }
2752
2753 virtual void visit( const BasicType * old ) override final {
2754 auto type = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2755 // I believe this should always be a BasicType.
2756 if ( Validate::SizeType == old ) {
2757 ast::sizeType = type;
2758 }
2759 visitType( old, type );
2760 }
2761
2762 virtual void visit( const PointerType * old ) override final {
2763 visitType( old, new ast::PointerType{
2764 GET_ACCEPT_1( base, Type ),
2765 GET_ACCEPT_1( dimension, Expr ),
2766 (ast::LengthFlag)old->isVarLen,
2767 (ast::DimensionFlag)old->isStatic,
2768 cv( old )
2769 } );
2770 }
2771
2772 virtual void visit( const ArrayType * old ) override final {
2773 visitType( old, new ast::ArrayType{
2774 GET_ACCEPT_1( base, Type ),
2775 GET_ACCEPT_1( dimension, Expr ),
2776 (ast::LengthFlag)old->isVarLen,
2777 (ast::DimensionFlag)old->isStatic,
2778 cv( old )
2779 } );
2780 }
2781
2782 virtual void visit( const ReferenceType * old ) override final {
2783 visitType( old, new ast::ReferenceType{
2784 GET_ACCEPT_1( base, Type ),
2785 cv( old )
2786 } );
2787 }
2788
2789 virtual void visit( const QualifiedType * old ) override final {
2790 visitType( old, new ast::QualifiedType{
2791 GET_ACCEPT_1( parent, Type ),
2792 GET_ACCEPT_1( child, Type ),
2793 cv( old )
2794 } );
2795 }
2796
2797 virtual void visit( const FunctionType * old ) override final {
2798 auto ty = new ast::FunctionType {
2799 (ast::ArgumentFlag)old->isVarArgs,
2800 cv( old )
2801 };
2802 auto returnVars = GET_ACCEPT_V(returnVals, DeclWithType);
2803 auto paramVars = GET_ACCEPT_V(parameters, DeclWithType);
2804 // ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2805 // ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2806 for (auto & v: returnVars) {
2807 ty->returns.emplace_back(v->get_type());
2808 }
2809 for (auto & v: paramVars) {
2810 ty->params.emplace_back(v->get_type());
2811 }
2812 // xxx - when will this be non-null?
2813 // will have to create dangling (no-owner) decls to be pointed to
2814 auto foralls = GET_ACCEPT_V( forall, TypeDecl );
2815
2816 for (auto & param : foralls) {
2817 ty->forall.emplace_back(new ast::TypeInstType(param));
2818 for (auto asst : param->assertions) {
2819 ty->assertions.emplace_back(
2820 new ast::VariableExpr(param->location, asst));
2821 }
2822 }
2823 visitType( old, ty );
2824 }
2825
2826 void postvisit( const ReferenceToType * old, ast::BaseInstType * ty ) {
2827 ty->params = GET_ACCEPT_V( parameters, Expr );
2828 ty->hoistType = old->hoistType;
2829 visitType( old, ty );
2830 }
2831
2832 virtual void visit( const StructInstType * old ) override final {
2833 ast::StructInstType * ty;
2834 if ( old->baseStruct ) {
2835 ty = new ast::StructInstType{
2836 GET_ACCEPT_1( baseStruct, StructDecl ),
2837 cv( old ),
2838 GET_ACCEPT_V( attributes, Attribute )
2839 };
2840 } else {
2841 ty = new ast::StructInstType{
2842 old->name,
2843 cv( old ),
2844 GET_ACCEPT_V( attributes, Attribute )
2845 };
2846 }
2847 postvisit( old, ty );
2848 }
2849
2850 virtual void visit( const UnionInstType * old ) override final {
2851 ast::UnionInstType * ty;
2852 if ( old->baseUnion ) {
2853 ty = new ast::UnionInstType{
2854 GET_ACCEPT_1( baseUnion, UnionDecl ),
2855 cv( old ),
2856 GET_ACCEPT_V( attributes, Attribute )
2857 };
2858 } else {
2859 ty = new ast::UnionInstType{
2860 old->name,
2861 cv( old ),
2862 GET_ACCEPT_V( attributes, Attribute )
2863 };
2864 }
2865 postvisit( old, ty );
2866 }
2867
2868 virtual void visit( const EnumInstType * old ) override final {
2869 ast::EnumInstType * ty;
2870 if ( old->baseEnum ) {
2871 ty = new ast::EnumInstType{
2872 GET_ACCEPT_1( baseEnum, EnumDecl ),
2873 cv( old ),
2874 GET_ACCEPT_V( attributes, Attribute )
2875 };
2876 } else {
2877 ty = new ast::EnumInstType{
2878 old->name,
2879 cv( old ),
2880 GET_ACCEPT_V( attributes, Attribute )
2881 };
2882 }
2883 postvisit( old, ty );
2884 }
2885
2886 virtual void visit( const TraitInstType * old ) override final {
2887 ast::TraitInstType * ty;
2888 if ( old->baseTrait ) {
2889 ty = new ast::TraitInstType{
2890 GET_ACCEPT_1( baseTrait, TraitDecl ),
2891 cv( old ),
2892 GET_ACCEPT_V( attributes, Attribute )
2893 };
2894 } else {
2895 ty = new ast::TraitInstType{
2896 old->name,
2897 cv( old ),
2898 GET_ACCEPT_V( attributes, Attribute )
2899 };
2900 }
2901 postvisit( old, ty );
2902 }
2903
2904 virtual void visit( const TypeInstType * old ) override final {
2905 ast::TypeInstType * ty;
2906 if ( old->baseType ) {
2907 ty = new ast::TypeInstType{
2908 old->name,
2909 GET_ACCEPT_1( baseType, TypeDecl ),
2910 cv( old ),
2911 GET_ACCEPT_V( attributes, Attribute )
2912 };
2913 } else {
2914 ty = new ast::TypeInstType{
2915 old->name,
2916 old->isFtype ? ast::TypeDecl::Ftype : ast::TypeDecl::Dtype,
2917 cv( old ),
2918 GET_ACCEPT_V( attributes, Attribute )
2919 };
2920 }
2921 postvisit( old, ty );
2922 }
2923
2924 virtual void visit( const TupleType * old ) override final {
2925 visitType( old, new ast::TupleType{
2926 GET_ACCEPT_V( types, Type ),
2927 // members generated by TupleType c'tor
2928 cv( old )
2929 } );
2930 }
2931
2932 virtual void visit( const TypeofType * old ) override final {
2933 visitType( old, new ast::TypeofType{
2934 GET_ACCEPT_1( expr, Expr ),
2935 (ast::TypeofType::Kind)old->is_basetypeof,
2936 cv( old )
2937 } );
2938 }
2939
2940 virtual void visit( const VTableType * old ) override final {
2941 visitType( old, new ast::VTableType{
2942 GET_ACCEPT_1( base, Type ),
2943 cv( old )
2944 } );
2945 }
2946
2947 virtual void visit( const AttrType * ) override final {
2948 assertf( false, "AttrType deprecated in new AST." );
2949 }
2950
2951 virtual void visit( const VarArgsType * old ) override final {
2952 visitType( old, new ast::VarArgsType{ cv( old ) } );
2953 }
2954
2955 virtual void visit( const ZeroType * old ) override final {
2956 visitType( old, new ast::ZeroType{ cv( old ) } );
2957 }
2958
2959 virtual void visit( const OneType * old ) override final {
2960 visitType( old, new ast::OneType{ cv( old ) } );
2961 }
2962
2963 virtual void visit( const GlobalScopeType * old ) override final {
2964 visitType( old, new ast::GlobalScopeType{} );
2965 }
2966
2967 virtual void visit( const Designation * old ) override final {
2968 this->node = new ast::Designation(
2969 old->location,
2970 GET_ACCEPT_D(designators, Expr)
2971 );
2972 }
2973
2974 virtual void visit( const SingleInit * old ) override final {
2975 this->node = new ast::SingleInit(
2976 old->location,
2977 GET_ACCEPT_1(value, Expr),
2978 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2979 );
2980 }
2981
2982 virtual void visit( const ListInit * old ) override final {
2983 this->node = new ast::ListInit(
2984 old->location,
2985 GET_ACCEPT_V(initializers, Init),
2986 GET_ACCEPT_V(designations, Designation),
2987 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2988 );
2989 }
2990
2991 virtual void visit( const ConstructorInit * old ) override final {
2992 this->node = new ast::ConstructorInit(
2993 old->location,
2994 GET_ACCEPT_1(ctor, Stmt),
2995 GET_ACCEPT_1(dtor, Stmt),
2996 GET_ACCEPT_1(init, Init)
2997 );
2998 }
2999
3000 virtual void visit( const Constant * ) override final {
3001 // Handled in visit( ConstantEpxr * ).
3002 // In the new tree, Constant fields are inlined into containing ConstantExpression.
3003 assert( 0 );
3004 }
3005
3006 virtual void visit( const Attribute * old ) override final {
3007 this->node = new ast::Attribute(
3008 old->name,
3009 GET_ACCEPT_V( parameters, Expr )
3010 );
3011 }
3012};
3013
3014#undef GET_LABELS_V
3015#undef GET_ACCEPT_V
3016#undef GET_ACCEPT_1
3017
3018ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ) {
3019 ConverterOldToNew c;
3020 ast::TranslationUnit unit;
3021 if (Validate::SizeType) {
3022 // this should be a BasicType.
3023 auto old = strict_dynamic_cast<BasicType *>(Validate::SizeType);
3024 ast::sizeType = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind };
3025 }
3026
3027 for(auto d : translationUnit) {
3028 d->accept( c );
3029 unit.decls.emplace_back( c.decl() );
3030 }
3031 deleteAll(translationUnit);
3032
3033 // Load the local static varables into the global store.
3034 unit.global.sizeType = ast::sizeType;
3035 unit.global.dereference = ast::dereferenceOperator;
3036 unit.global.dtorStruct = ast::dtorStruct;
3037 unit.global.dtorDestroy = ast::dtorStructDestroy;
3038
3039 return unit;
3040}
Note: See TracBrowser for help on using the repository browser.