source: src/AST/Convert.cpp@ 890f67a

ADT ast-experimental
Last change on this file since 890f67a was b0d9ff7, checked in by JiadaL <j82liang@…>, 3 years ago

Fix up the QualifiedNameExpr. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

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