source: src/AST/Convert.cpp@ d6c464d

ADT
Last change on this file since d6c464d was 561354f, checked in by JiadaL <j82liang@…>, 2 years ago

Save progress

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