source: src/AST/Convert.cpp@ fa2c005

ADT
Last change on this file since fa2c005 was fa2c005, checked in by JiadaL <j82liang@…>, 3 years ago

Finish Adt POC

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