source: src/AST/Convert.cpp@ d83b266

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d83b266 was cc64be1d, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added VTableType to the conversion passes.

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