source: src/AST/Convert.cpp@ 148ba7d

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 148ba7d was 6cebfef, checked in by caparsons <caparson@…>, 4 years ago

added mutex stmt monitor

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