source: src/AST/Convert.cpp@ 7e1cb79

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 7e1cb79 was 2d019af, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

parser global pragmas, fixes #241

  • Property mode set to 100644
File size: 78.9 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 : Peter A. Buhr
12// Last Modified On : Fri Mar 12 18:43:51 2021
13// Update Count : 36
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::VarArgsType * node ) override final {
1359 return visitType( node, new VarArgsType{ cv( node ) } );
1360 }
1361
1362 const ast::Type * visit( const ast::ZeroType * node ) override final {
1363 return visitType( node, new ZeroType{ cv( node ) } );
1364 }
1365
1366 const ast::Type * visit( const ast::OneType * node ) override final {
1367 return visitType( node, new OneType{ cv( node ) } );
1368 }
1369
1370 const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
1371 return visitType( node, new GlobalScopeType{} );
1372 }
1373
1374 const ast::Designation * visit( const ast::Designation * node ) override final {
1375 auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
1376 designation->location = node->location;
1377 this->node = designation;
1378 return nullptr;
1379 }
1380
1381 const ast::Init * visit( const ast::SingleInit * node ) override final {
1382 auto init = new SingleInit(
1383 get<Expression>().accept1( node->value ),
1384 ast::MaybeConstruct == node->maybeConstructed
1385 );
1386 init->location = node->location;
1387 this->node = init;
1388 return nullptr;
1389 }
1390
1391 const ast::Init * visit( const ast::ListInit * node ) override final {
1392 auto init = new ListInit(
1393 get<Initializer>().acceptL( node->initializers ),
1394 get<Designation>().acceptL( node->designations ),
1395 ast::MaybeConstruct == node->maybeConstructed
1396 );
1397 init->location = node->location;
1398 this->node = init;
1399 return nullptr;
1400 }
1401
1402 const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1403 auto init = new ConstructorInit(
1404 get<Statement>().accept1( node->ctor ),
1405 get<Statement>().accept1( node->dtor ),
1406 get<Initializer>().accept1( node->init )
1407 );
1408 init->location = node->location;
1409 this->node = init;
1410 return nullptr;
1411 }
1412
1413 const ast::Attribute * visit( const ast::Attribute * node ) override final {
1414 auto attr = new Attribute(
1415 node->name,
1416 get<Expression>().acceptL(node->params)
1417 );
1418 this->node = attr;
1419 return nullptr;
1420 }
1421
1422 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1423 // Handled by convertTypeSubstitution helper instead.
1424 // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
1425 assert( 0 );
1426 (void)node;
1427 return nullptr;
1428 }
1429};
1430
1431std::list< Declaration * > convert( const ast::TranslationUnit && translationUnit ) {
1432 ConverterNewToOld c;
1433 std::list< Declaration * > decls;
1434 for(auto d : translationUnit.decls) {
1435 decls.emplace_back( c.decl( d ) );
1436 }
1437 return decls;
1438}
1439
1440//================================================================================================
1441
1442class ConverterOldToNew : public Visitor {
1443public:
1444 ast::Decl * decl() {
1445 return strict_dynamic_cast< ast::Decl * >( node );
1446 }
1447
1448 ConverterOldToNew() = default;
1449 ConverterOldToNew(const ConverterOldToNew &) = delete;
1450 ConverterOldToNew(ConverterOldToNew &&) = delete;
1451private:
1452 /// conversion output
1453 ast::Node * node = nullptr;
1454 /// cache of nodes that might be referenced by readonly<> for de-duplication
1455 /// in case that some nodes are dropped by conversion (due to possible structural change)
1456 /// use smart pointers in cache value to prevent accidental invalidation.
1457 /// at conversion stage, all created nodes are guaranteed to be unique, therefore
1458 /// const_casting out of smart pointers is permitted.
1459 std::unordered_map< const BaseSyntaxNode *, ast::readonly<ast::Node> > cache = {};
1460
1461 // Local Utilities:
1462
1463 template<typename NewT, typename OldT>
1464 NewT * getAccept1( OldT old ) {
1465 if ( ! old ) return nullptr;
1466 old->accept(*this);
1467 ast::Node * ret = node;
1468 node = nullptr;
1469 return strict_dynamic_cast< NewT * >( ret );
1470 }
1471
1472# define GET_ACCEPT_1(child, type) \
1473 getAccept1< ast::type, decltype( old->child ) >( old->child )
1474
1475 template<typename NewT, typename OldC>
1476 std::vector< ast::ptr<NewT> > getAcceptV( const OldC& old ) {
1477 std::vector< ast::ptr<NewT> > ret;
1478 ret.reserve( old.size() );
1479 for ( auto a : old ) {
1480 a->accept( *this );
1481 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1482 node = nullptr;
1483 }
1484 return ret;
1485 }
1486
1487# define GET_ACCEPT_V(child, type) \
1488 getAcceptV< ast::type, decltype( old->child ) >( old->child )
1489
1490 template<typename NewT, typename OldC>
1491 std::deque< ast::ptr<NewT> > getAcceptD( const OldC& old ) {
1492 std::deque< ast::ptr<NewT> > ret;
1493 for ( auto a : old ) {
1494 a->accept( *this );
1495 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1496 node = nullptr;
1497 }
1498 return ret;
1499 }
1500
1501# define GET_ACCEPT_D(child, type) \
1502 getAcceptD< ast::type, decltype( old->child ) >( old->child )
1503
1504 ast::Label make_label(const Label* old) {
1505 CodeLocation const & location =
1506 ( old->labelled ) ? old->labelled->location : CodeLocation();
1507 return ast::Label(
1508 location,
1509 old->name,
1510 GET_ACCEPT_V(attributes, Attribute)
1511 );
1512 }
1513
1514 template<template <class...> class C>
1515 C<ast::Label> make_labels(C<Label> olds) {
1516 C<ast::Label> ret;
1517 for (auto oldn : olds) {
1518 ret.push_back( make_label( &oldn ) );
1519 }
1520 return ret;
1521 }
1522
1523# define GET_LABELS_V(labels) \
1524 to<std::vector>::from( make_labels( std::move( labels ) ) )
1525
1526 static ast::CV::Qualifiers cv( const Type * ty ) { return { ty->tq.val }; }
1527
1528 /// returns true and sets `node` if in cache
1529 bool inCache( const BaseSyntaxNode * old ) {
1530 auto it = cache.find( old );
1531 if ( it == cache.end() ) return false;
1532 node = const_cast<ast::Node *>(it->second.get());
1533 return true;
1534 }
1535
1536 // Now all the visit functions:
1537
1538 virtual void visit( const ObjectDecl * old ) override final {
1539 auto&& type = GET_ACCEPT_1(type, Type);
1540 auto&& init = GET_ACCEPT_1(init, Init);
1541 auto&& bfwd = GET_ACCEPT_1(bitfieldWidth, Expr);
1542 auto&& attr = GET_ACCEPT_V(attributes, Attribute);
1543 if ( inCache( old ) ) {
1544 return;
1545 }
1546 auto decl = new ast::ObjectDecl(
1547 old->location,
1548 old->name,
1549 type,
1550 init,
1551 { old->get_storageClasses().val },
1552 { old->linkage.val },
1553 bfwd,
1554 std::move(attr),
1555 { old->get_funcSpec().val }
1556 );
1557 cache.emplace(old, decl);
1558 assert(cache.find( old ) != cache.end());
1559 decl->scopeLevel = old->scopeLevel;
1560 decl->mangleName = old->mangleName;
1561 decl->isDeleted = old->isDeleted;
1562 decl->asmName = GET_ACCEPT_1(asmName, Expr);
1563 decl->uniqueId = old->uniqueId;
1564 decl->extension = old->extension;
1565
1566 this->node = decl;
1567 }
1568
1569 virtual void visit( const FunctionDecl * old ) override final {
1570 if ( inCache( old ) ) return;
1571 auto paramVars = GET_ACCEPT_V(type->parameters, DeclWithType);
1572 auto returnVars = GET_ACCEPT_V(type->returnVals, DeclWithType);
1573 auto forall = GET_ACCEPT_V(type->forall, TypeDecl);
1574
1575 // function type is now derived from parameter decls instead of storing them
1576
1577 /*
1578 auto ftype = new ast::FunctionType((ast::ArgumentFlag)old->type->isVarArgs, cv(old->type));
1579 ftype->params.reserve(paramVars.size());
1580 ftype->returns.reserve(returnVars.size());
1581
1582 for (auto & v: paramVars) {
1583 ftype->params.emplace_back(v->get_type());
1584 }
1585 for (auto & v: returnVars) {
1586 ftype->returns.emplace_back(v->get_type());
1587 }
1588 ftype->forall = std::move(forall);
1589 */
1590
1591 // can function type have attributes? seems not to be the case.
1592 // visitType(old->type, ftype);
1593
1594 // collect assertions and put directly in FunctionDecl
1595 std::vector<ast::ptr<ast::DeclWithType>> assertions;
1596 for (auto & param: forall) {
1597 for (auto & asst: param->assertions) {
1598 assertf(asst->unique(), "newly converted decl must be unique");
1599 assertions.emplace_back(asst);
1600 }
1601 auto mut = param.get_and_mutate();
1602 assertf(mut == param, "newly converted decl must be unique");
1603 mut->assertions.clear();
1604 }
1605
1606 auto decl = new ast::FunctionDecl{
1607 old->location,
1608 old->name,
1609 // GET_ACCEPT_1(type, FunctionType),
1610 std::move(forall),
1611 std::move(paramVars),
1612 std::move(returnVars),
1613 {},
1614 { old->storageClasses.val },
1615 { old->linkage.val },
1616 GET_ACCEPT_V(attributes, Attribute),
1617 { old->get_funcSpec().val },
1618 old->type->isVarArgs
1619 };
1620
1621 // decl->type = ftype;
1622 cache.emplace( old, decl );
1623
1624 decl->assertions = std::move(assertions);
1625 decl->withExprs = GET_ACCEPT_V(withExprs, Expr);
1626 decl->stmts = GET_ACCEPT_1(statements, CompoundStmt);
1627 decl->scopeLevel = old->scopeLevel;
1628 decl->mangleName = old->mangleName;
1629 decl->isDeleted = old->isDeleted;
1630 decl->asmName = GET_ACCEPT_1(asmName, Expr);
1631 decl->uniqueId = old->uniqueId;
1632 decl->extension = old->extension;
1633
1634 this->node = decl;
1635
1636 if ( Validate::dereferenceOperator == old ) {
1637 ast::dereferenceOperator = decl;
1638 }
1639
1640 if ( Validate::dtorStructDestroy == old ) {
1641 ast::dtorStructDestroy = decl;
1642 }
1643 }
1644
1645 virtual void visit( const StructDecl * old ) override final {
1646 if ( inCache( old ) ) return;
1647 auto decl = new ast::StructDecl(
1648 old->location,
1649 old->name,
1650 (ast::AggregateDecl::Aggregate)old->kind,
1651 GET_ACCEPT_V(attributes, Attribute),
1652 { old->linkage.val }
1653 );
1654 cache.emplace( old, decl );
1655 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1656 decl->body = old->body;
1657 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1658 decl->members = GET_ACCEPT_V(members, Decl);
1659 decl->extension = old->extension;
1660 decl->uniqueId = old->uniqueId;
1661 decl->storage = { old->storageClasses.val };
1662
1663 this->node = decl;
1664
1665 if ( Validate::dtorStruct == old ) {
1666 ast::dtorStruct = decl;
1667 }
1668 }
1669
1670 virtual void visit( const UnionDecl * old ) override final {
1671 if ( inCache( old ) ) return;
1672 auto decl = new ast::UnionDecl(
1673 old->location,
1674 old->name,
1675 GET_ACCEPT_V(attributes, Attribute),
1676 { old->linkage.val }
1677 );
1678 cache.emplace( old, decl );
1679 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1680 decl->body = old->body;
1681 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1682 decl->members = GET_ACCEPT_V(members, Decl);
1683 decl->extension = old->extension;
1684 decl->uniqueId = old->uniqueId;
1685 decl->storage = { old->storageClasses.val };
1686
1687 this->node = decl;
1688 }
1689
1690 virtual void visit( const EnumDecl * old ) override final {
1691 if ( inCache( old ) ) return;
1692 auto decl = new ast::EnumDecl(
1693 old->location,
1694 old->name,
1695 GET_ACCEPT_V(attributes, Attribute),
1696 { old->linkage.val }
1697 );
1698 cache.emplace( old, decl );
1699 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1700 decl->body = old->body;
1701 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1702 decl->members = GET_ACCEPT_V(members, Decl);
1703 decl->extension = old->extension;
1704 decl->uniqueId = old->uniqueId;
1705 decl->storage = { old->storageClasses.val };
1706
1707 this->node = decl;
1708 }
1709
1710 virtual void visit( const TraitDecl * old ) override final {
1711 if ( inCache( old ) ) return;
1712 auto decl = new ast::TraitDecl(
1713 old->location,
1714 old->name,
1715 GET_ACCEPT_V(attributes, Attribute),
1716 { old->linkage.val }
1717 );
1718 cache.emplace( old, decl );
1719 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1720 decl->body = old->body;
1721 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1722 decl->members = GET_ACCEPT_V(members, Decl);
1723 decl->extension = old->extension;
1724 decl->uniqueId = old->uniqueId;
1725 decl->storage = { old->storageClasses.val };
1726
1727 this->node = decl;
1728 }
1729
1730 virtual void visit( const TypeDecl * old ) override final {
1731 if ( inCache( old ) ) return;
1732 auto decl = new ast::TypeDecl{
1733 old->location,
1734 old->name,
1735 { old->storageClasses.val },
1736 GET_ACCEPT_1(base, Type),
1737 (ast::TypeDecl::Kind)(unsigned)old->kind,
1738 old->sized,
1739 GET_ACCEPT_1(init, Type)
1740 };
1741 cache.emplace( old, decl );
1742 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1743 decl->extension = old->extension;
1744 decl->uniqueId = old->uniqueId;
1745
1746 this->node = decl;
1747 }
1748
1749 virtual void visit( const TypedefDecl * old ) override final {
1750 auto decl = new ast::TypedefDecl(
1751 old->location,
1752 old->name,
1753 { old->storageClasses.val },
1754 GET_ACCEPT_1(base, Type),
1755 { old->linkage.val }
1756 );
1757 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1758 decl->extension = old->extension;
1759 decl->uniqueId = old->uniqueId;
1760 decl->storage = { old->storageClasses.val };
1761
1762 this->node = decl;
1763 }
1764
1765 virtual void visit( const AsmDecl * old ) override final {
1766 auto decl = new ast::AsmDecl{
1767 old->location,
1768 GET_ACCEPT_1(stmt, AsmStmt)
1769 };
1770 decl->extension = old->extension;
1771 decl->uniqueId = old->uniqueId;
1772 decl->storage = { old->storageClasses.val };
1773
1774 this->node = decl;
1775 }
1776
1777 virtual void visit( const DirectiveDecl * old ) override final {
1778 auto decl = new ast::DirectiveDecl{
1779 old->location,
1780 GET_ACCEPT_1(stmt, DirectiveStmt)
1781 };
1782 decl->extension = old->extension;
1783 decl->uniqueId = old->uniqueId;
1784 decl->storage = { old->storageClasses.val };
1785
1786 this->node = decl;
1787 }
1788
1789 virtual void visit( const StaticAssertDecl * old ) override final {
1790 auto decl = new ast::StaticAssertDecl{
1791 old->location,
1792 GET_ACCEPT_1(condition, Expr),
1793 GET_ACCEPT_1(message, ConstantExpr)
1794 };
1795 decl->extension = old->extension;
1796 decl->uniqueId = old->uniqueId;
1797 decl->storage = { old->storageClasses.val };
1798
1799 this->node = decl;
1800 }
1801
1802 virtual void visit( const CompoundStmt * old ) override final {
1803 if ( inCache( old ) ) return;
1804 auto stmt = new ast::CompoundStmt(
1805 old->location,
1806 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1807 GET_LABELS_V(old->labels)
1808 );
1809
1810 this->node = stmt;
1811 cache.emplace( old, this->node );
1812 }
1813
1814 virtual void visit( const ExprStmt * old ) override final {
1815 if ( inCache( old ) ) return;
1816 this->node = new ast::ExprStmt(
1817 old->location,
1818 GET_ACCEPT_1(expr, Expr),
1819 GET_LABELS_V(old->labels)
1820 );
1821 cache.emplace( old, this->node );
1822 }
1823
1824 virtual void visit( const AsmStmt * old ) override final {
1825 if ( inCache( old ) ) return;
1826 this->node = new ast::AsmStmt(
1827 old->location,
1828 old->voltile,
1829 GET_ACCEPT_1(instruction, Expr),
1830 GET_ACCEPT_V(output, Expr),
1831 GET_ACCEPT_V(input, Expr),
1832 GET_ACCEPT_V(clobber, ConstantExpr),
1833 GET_LABELS_V(old->gotolabels),
1834 GET_LABELS_V(old->labels)
1835 );
1836 cache.emplace( old, this->node );
1837 }
1838
1839 virtual void visit( const DirectiveStmt * old ) override final {
1840 if ( inCache( old ) ) return;
1841 this->node = new ast::DirectiveStmt(
1842 old->location,
1843 old->directive,
1844 GET_LABELS_V(old->labels)
1845 );
1846 cache.emplace( old, this->node );
1847 }
1848
1849 virtual void visit( const IfStmt * old ) override final {
1850 if ( inCache( old ) ) return;
1851 this->node = new ast::IfStmt(
1852 old->location,
1853 GET_ACCEPT_1(condition, Expr),
1854 GET_ACCEPT_1(thenPart, Stmt),
1855 GET_ACCEPT_1(elsePart, Stmt),
1856 GET_ACCEPT_V(initialization, Stmt),
1857 GET_LABELS_V(old->labels)
1858 );
1859 cache.emplace( old, this->node );
1860 }
1861
1862 virtual void visit( const SwitchStmt * old ) override final {
1863 if ( inCache( old ) ) return;
1864 this->node = new ast::SwitchStmt(
1865 old->location,
1866 GET_ACCEPT_1(condition, Expr),
1867 GET_ACCEPT_V(statements, Stmt),
1868 GET_LABELS_V(old->labels)
1869 );
1870 cache.emplace( old, this->node );
1871 }
1872
1873 virtual void visit( const CaseStmt * old ) override final {
1874 if ( inCache( old ) ) return;
1875 this->node = new ast::CaseStmt(
1876 old->location,
1877 GET_ACCEPT_1(condition, Expr),
1878 GET_ACCEPT_V(stmts, Stmt),
1879 GET_LABELS_V(old->labels)
1880 );
1881 cache.emplace( old, this->node );
1882 }
1883
1884 virtual void visit( const WhileStmt * old ) override final {
1885 if ( inCache( old ) ) return;
1886 this->node = new ast::WhileStmt(
1887 old->location,
1888 GET_ACCEPT_1(condition, Expr),
1889 GET_ACCEPT_1(body, Stmt),
1890 GET_ACCEPT_V(initialization, Stmt),
1891 old->isDoWhile,
1892 GET_LABELS_V(old->labels)
1893 );
1894 cache.emplace( old, this->node );
1895 }
1896
1897 virtual void visit( const ForStmt * old ) override final {
1898 if ( inCache( old ) ) return;
1899 this->node = new ast::ForStmt(
1900 old->location,
1901 GET_ACCEPT_V(initialization, Stmt),
1902 GET_ACCEPT_1(condition, Expr),
1903 GET_ACCEPT_1(increment, Expr),
1904 GET_ACCEPT_1(body, Stmt),
1905 GET_LABELS_V(old->labels)
1906 );
1907 cache.emplace( old, this->node );
1908 }
1909
1910 virtual void visit( const BranchStmt * old ) override final {
1911 if ( inCache( old ) ) return;
1912 if (old->computedTarget) {
1913 this->node = new ast::BranchStmt(
1914 old->location,
1915 GET_ACCEPT_1(computedTarget, Expr),
1916 GET_LABELS_V(old->labels)
1917 );
1918 } else {
1919 ast::BranchStmt::Kind kind;
1920 switch (old->type) {
1921 #define CASE(n) \
1922 case BranchStmt::n: \
1923 kind = ast::BranchStmt::n; \
1924 break
1925 CASE(Goto);
1926 CASE(Break);
1927 CASE(Continue);
1928 CASE(FallThrough);
1929 CASE(FallThroughDefault);
1930 #undef CASE
1931 default:
1932 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1933 }
1934
1935 auto stmt = new ast::BranchStmt(
1936 old->location,
1937 kind,
1938 make_label(&old->originalTarget),
1939 GET_LABELS_V(old->labels)
1940 );
1941 stmt->target = make_label(&old->target);
1942 this->node = stmt;
1943 }
1944 cache.emplace( old, this->node );
1945 }
1946
1947 virtual void visit( const ReturnStmt * old ) override final {
1948 if ( inCache( old ) ) return;
1949 this->node = new ast::ReturnStmt(
1950 old->location,
1951 GET_ACCEPT_1(expr, Expr),
1952 GET_LABELS_V(old->labels)
1953 );
1954 cache.emplace( old, this->node );
1955 }
1956
1957 virtual void visit( const ThrowStmt * old ) override final {
1958 if ( inCache( old ) ) return;
1959 ast::ExceptionKind kind;
1960 switch (old->kind) {
1961 case ThrowStmt::Terminate:
1962 kind = ast::ExceptionKind::Terminate;
1963 break;
1964 case ThrowStmt::Resume:
1965 kind = ast::ExceptionKind::Resume;
1966 break;
1967 default:
1968 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1969 }
1970
1971 this->node = new ast::ThrowStmt(
1972 old->location,
1973 kind,
1974 GET_ACCEPT_1(expr, Expr),
1975 GET_ACCEPT_1(target, Expr),
1976 GET_LABELS_V(old->labels)
1977 );
1978 cache.emplace( old, this->node );
1979 }
1980
1981 virtual void visit( const TryStmt * old ) override final {
1982 if ( inCache( old ) ) return;
1983 this->node = new ast::TryStmt(
1984 old->location,
1985 GET_ACCEPT_1(block, CompoundStmt),
1986 GET_ACCEPT_V(handlers, CatchStmt),
1987 GET_ACCEPT_1(finallyBlock, FinallyStmt),
1988 GET_LABELS_V(old->labels)
1989 );
1990 cache.emplace( old, this->node );
1991 }
1992
1993 virtual void visit( const CatchStmt * old ) override final {
1994 if ( inCache( old ) ) return;
1995 ast::ExceptionKind kind;
1996 switch (old->kind) {
1997 case CatchStmt::Terminate:
1998 kind = ast::ExceptionKind::Terminate;
1999 break;
2000 case CatchStmt::Resume:
2001 kind = ast::ExceptionKind::Resume;
2002 break;
2003 default:
2004 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
2005 }
2006
2007 this->node = new ast::CatchStmt(
2008 old->location,
2009 kind,
2010 GET_ACCEPT_1(decl, Decl),
2011 GET_ACCEPT_1(cond, Expr),
2012 GET_ACCEPT_1(body, Stmt),
2013 GET_LABELS_V(old->labels)
2014 );
2015 cache.emplace( old, this->node );
2016 }
2017
2018 virtual void visit( const FinallyStmt * old ) override final {
2019 if ( inCache( old ) ) return;
2020 this->node = new ast::FinallyStmt(
2021 old->location,
2022 GET_ACCEPT_1(block, CompoundStmt),
2023 GET_LABELS_V(old->labels)
2024 );
2025 cache.emplace( old, this->node );
2026 }
2027
2028 virtual void visit( const SuspendStmt * old ) override final {
2029 if ( inCache( old ) ) return;
2030 ast::SuspendStmt::Type type;
2031 switch (old->type) {
2032 case SuspendStmt::Coroutine: type = ast::SuspendStmt::Coroutine; break;
2033 case SuspendStmt::Generator: type = ast::SuspendStmt::Generator; break;
2034 case SuspendStmt::None : type = ast::SuspendStmt::None ; break;
2035 default: abort();
2036 }
2037 this->node = new ast::SuspendStmt(
2038 old->location,
2039 GET_ACCEPT_1(then , CompoundStmt),
2040 type,
2041 GET_LABELS_V(old->labels)
2042 );
2043 cache.emplace( old, this->node );
2044 }
2045
2046 virtual void visit( const WaitForStmt * old ) override final {
2047 if ( inCache( old ) ) return;
2048 ast::WaitForStmt * stmt = new ast::WaitForStmt(
2049 old->location,
2050 GET_LABELS_V(old->labels)
2051 );
2052
2053 stmt->clauses.reserve( old->clauses.size() );
2054 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
2055 stmt->clauses.push_back({
2056 ast::WaitForStmt::Target{
2057 GET_ACCEPT_1(clauses[i].target.function, Expr),
2058 GET_ACCEPT_V(clauses[i].target.arguments, Expr)
2059 },
2060 GET_ACCEPT_1(clauses[i].statement, Stmt),
2061 GET_ACCEPT_1(clauses[i].condition, Expr)
2062 });
2063 }
2064 stmt->timeout = {
2065 GET_ACCEPT_1(timeout.time, Expr),
2066 GET_ACCEPT_1(timeout.statement, Stmt),
2067 GET_ACCEPT_1(timeout.condition, Expr),
2068 };
2069 stmt->orElse = {
2070 GET_ACCEPT_1(orelse.statement, Stmt),
2071 GET_ACCEPT_1(orelse.condition, Expr),
2072 };
2073
2074 this->node = stmt;
2075 cache.emplace( old, this->node );
2076 }
2077
2078 virtual void visit( const WithStmt * old ) override final {
2079 if ( inCache( old ) ) return;
2080 this->node = new ast::WithStmt(
2081 old->location,
2082 GET_ACCEPT_V(exprs, Expr),
2083 GET_ACCEPT_1(stmt, Stmt)
2084 );
2085 cache.emplace( old, this->node );
2086 }
2087
2088 virtual void visit( const NullStmt * old ) override final {
2089 if ( inCache( old ) ) return;
2090 this->node = new ast::NullStmt(
2091 old->location,
2092 GET_LABELS_V(old->labels)
2093 );
2094 cache.emplace( old, this->node );
2095 }
2096
2097 virtual void visit( const DeclStmt * old ) override final {
2098 if ( inCache( old ) ) return;
2099 this->node = new ast::DeclStmt(
2100 old->location,
2101 GET_ACCEPT_1(decl, Decl),
2102 GET_LABELS_V(old->labels)
2103 );
2104 cache.emplace( old, this->node );
2105 }
2106
2107 virtual void visit( const ImplicitCtorDtorStmt * old ) override final {
2108 if ( inCache( old ) ) return;
2109 auto stmt = new ast::ImplicitCtorDtorStmt(
2110 old->location,
2111 nullptr,
2112 GET_LABELS_V(old->labels)
2113 );
2114 cache.emplace( old, stmt );
2115 stmt->callStmt = GET_ACCEPT_1(callStmt, Stmt);
2116 this->node = stmt;
2117 }
2118
2119 // TypeSubstitution shouldn't exist yet in old.
2120 ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
2121
2122 if (!old) return nullptr;
2123 if (old->empty()) return nullptr;
2124 assert(false);
2125
2126 /*
2127 ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
2128
2129 for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
2130 rslt->add( old_i->first,
2131 getAccept1<ast::Type>(old_i->second) );
2132 }
2133
2134 return rslt;
2135 */
2136 }
2137
2138 void convertInferUnion(ast::Expr::InferUnion &newInferred,
2139 const std::map<UniqueId,ParamEntry> &oldInferParams,
2140 const std::vector<UniqueId> &oldResnSlots) {
2141
2142 assert( oldInferParams.empty() || oldResnSlots.empty() );
2143 // assert( newInferred.mode == ast::Expr::InferUnion::Empty );
2144
2145 if ( !oldInferParams.empty() ) {
2146 ast::InferredParams &tgt = newInferred.inferParams();
2147 for (auto & old : oldInferParams) {
2148 tgt[old.first] = ast::ParamEntry(
2149 old.second.decl,
2150 getAccept1<ast::Decl>(old.second.declptr),
2151 getAccept1<ast::Type>(old.second.actualType),
2152 getAccept1<ast::Type>(old.second.formalType),
2153 getAccept1<ast::Expr>(old.second.expr)
2154 );
2155 }
2156 } else if ( !oldResnSlots.empty() ) {
2157 ast::ResnSlots &tgt = newInferred.resnSlots();
2158 for (auto old : oldResnSlots) {
2159 tgt.push_back(old);
2160 }
2161 }
2162 }
2163
2164 ast::Expr * visitBaseExpr_SkipResultType( const Expression * old, ast::Expr * nw) {
2165
2166 nw->env = convertTypeSubstitution(old->env);
2167
2168 nw->extension = old->extension;
2169 convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
2170
2171 return nw;
2172 }
2173
2174 ast::Expr * visitBaseExpr( const Expression * old, ast::Expr * nw) {
2175
2176 nw->result = GET_ACCEPT_1(result, Type);
2177 return visitBaseExpr_SkipResultType(old, nw);;
2178 }
2179
2180 virtual void visit( const ApplicationExpr * old ) override final {
2181 this->node = visitBaseExpr( old,
2182 new ast::ApplicationExpr(
2183 old->location,
2184 GET_ACCEPT_1(function, Expr),
2185 GET_ACCEPT_V(args, Expr)
2186 )
2187 );
2188 }
2189
2190 virtual void visit( const UntypedExpr * old ) override final {
2191 this->node = visitBaseExpr( old,
2192 new ast::UntypedExpr(
2193 old->location,
2194 GET_ACCEPT_1(function, Expr),
2195 GET_ACCEPT_V(args, Expr)
2196 )
2197 );
2198 }
2199
2200 virtual void visit( const NameExpr * old ) override final {
2201 this->node = visitBaseExpr( old,
2202 new ast::NameExpr(
2203 old->location,
2204 old->get_name()
2205 )
2206 );
2207 }
2208
2209 virtual void visit( const CastExpr * old ) override final {
2210 this->node = visitBaseExpr( old,
2211 new ast::CastExpr(
2212 old->location,
2213 GET_ACCEPT_1(arg, Expr),
2214 old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
2215 )
2216 );
2217 }
2218
2219 virtual void visit( const KeywordCastExpr * old ) override final {
2220 ast::AggregateDecl::Aggregate castTarget = (ast::AggregateDecl::Aggregate)old->target;
2221 assert( ast::AggregateDecl::Generator <= castTarget && castTarget <= ast::AggregateDecl::Thread );
2222 this->node = visitBaseExpr( old,
2223 new ast::KeywordCastExpr(
2224 old->location,
2225 GET_ACCEPT_1(arg, Expr),
2226 castTarget,
2227 {old->concrete_target.field, old->concrete_target.getter}
2228 )
2229 );
2230 }
2231
2232 virtual void visit( const VirtualCastExpr * old ) override final {
2233 this->node = visitBaseExpr_SkipResultType( old,
2234 new ast::VirtualCastExpr(
2235 old->location,
2236 GET_ACCEPT_1(arg, Expr),
2237 GET_ACCEPT_1(result, Type)
2238 )
2239 );
2240 }
2241
2242 virtual void visit( const AddressExpr * old ) override final {
2243 this->node = visitBaseExpr( old,
2244 new ast::AddressExpr(
2245 old->location,
2246 GET_ACCEPT_1(arg, Expr)
2247 )
2248 );
2249 }
2250
2251 virtual void visit( const LabelAddressExpr * old ) override final {
2252 this->node = visitBaseExpr( old,
2253 new ast::LabelAddressExpr(
2254 old->location,
2255 make_label(&old->arg)
2256 )
2257 );
2258 }
2259
2260 virtual void visit( const UntypedMemberExpr * old ) override final {
2261 this->node = visitBaseExpr( old,
2262 new ast::UntypedMemberExpr(
2263 old->location,
2264 GET_ACCEPT_1(member, Expr),
2265 GET_ACCEPT_1(aggregate, Expr)
2266 )
2267 );
2268 }
2269
2270 virtual void visit( const MemberExpr * old ) override final {
2271 this->node = visitBaseExpr( old,
2272 new ast::MemberExpr(
2273 old->location,
2274 GET_ACCEPT_1(member, DeclWithType),
2275 GET_ACCEPT_1(aggregate, Expr),
2276 ast::MemberExpr::NoOpConstructionChosen
2277 )
2278 );
2279 }
2280
2281 virtual void visit( const VariableExpr * old ) override final {
2282 auto expr = new ast::VariableExpr(
2283 old->location
2284 );
2285
2286 expr->var = GET_ACCEPT_1(var, DeclWithType);
2287 visitBaseExpr( old, expr );
2288
2289 this->node = expr;
2290 }
2291
2292 virtual void visit( const ConstantExpr * old ) override final {
2293 ast::ConstantExpr *rslt = new ast::ConstantExpr(
2294 old->location,
2295 GET_ACCEPT_1(result, Type),
2296 old->constant.rep,
2297 old->constant.ival
2298 );
2299 rslt->underlyer = getAccept1< ast::Type, Type* >( old->constant.type );
2300 this->node = visitBaseExpr( old, rslt );
2301 }
2302
2303 virtual void visit( const SizeofExpr * old ) override final {
2304 assert (old->expr || old->type);
2305 assert (! (old->expr && old->type));
2306 ast::SizeofExpr *rslt;
2307 if (old->expr) {
2308 assert(!old->isType);
2309 rslt = new ast::SizeofExpr(
2310 old->location,
2311 GET_ACCEPT_1(expr, Expr)
2312 );
2313 }
2314 if (old->type) {
2315 assert(old->isType);
2316 rslt = new ast::SizeofExpr(
2317 old->location,
2318 GET_ACCEPT_1(type, Type)
2319 );
2320 }
2321 this->node = visitBaseExpr( old, rslt );
2322 }
2323
2324 virtual void visit( const AlignofExpr * old ) override final {
2325 assert (old->expr || old->type);
2326 assert (! (old->expr && old->type));
2327 ast::AlignofExpr *rslt;
2328 if (old->expr) {
2329 assert(!old->isType);
2330 rslt = new ast::AlignofExpr(
2331 old->location,
2332 GET_ACCEPT_1(expr, Expr)
2333 );
2334 }
2335 if (old->type) {
2336 assert(old->isType);
2337 rslt = new ast::AlignofExpr(
2338 old->location,
2339 GET_ACCEPT_1(type, Type)
2340 );
2341 }
2342 this->node = visitBaseExpr( old, rslt );
2343 }
2344
2345 virtual void visit( const UntypedOffsetofExpr * old ) override final {
2346 this->node = visitBaseExpr( old,
2347 new ast::UntypedOffsetofExpr(
2348 old->location,
2349 GET_ACCEPT_1(type, Type),
2350 old->member
2351 )
2352 );
2353 }
2354
2355 virtual void visit( const OffsetofExpr * old ) override final {
2356 this->node = visitBaseExpr( old,
2357 new ast::OffsetofExpr(
2358 old->location,
2359 GET_ACCEPT_1(type, Type),
2360 GET_ACCEPT_1(member, DeclWithType)
2361 )
2362 );
2363 }
2364
2365 virtual void visit( const OffsetPackExpr * old ) override final {
2366 this->node = visitBaseExpr( old,
2367 new ast::OffsetPackExpr(
2368 old->location,
2369 GET_ACCEPT_1(type, StructInstType)
2370 )
2371 );
2372 }
2373
2374 virtual void visit( const LogicalExpr * old ) override final {
2375 this->node = visitBaseExpr( old,
2376 new ast::LogicalExpr(
2377 old->location,
2378 GET_ACCEPT_1(arg1, Expr),
2379 GET_ACCEPT_1(arg2, Expr),
2380 old->get_isAnd() ?
2381 ast::LogicalFlag::AndExpr :
2382 ast::LogicalFlag::OrExpr
2383 )
2384 );
2385 }
2386
2387 virtual void visit( const ConditionalExpr * old ) override final {
2388 this->node = visitBaseExpr( old,
2389 new ast::ConditionalExpr(
2390 old->location,
2391 GET_ACCEPT_1(arg1, Expr),
2392 GET_ACCEPT_1(arg2, Expr),
2393 GET_ACCEPT_1(arg3, Expr)
2394 )
2395 );
2396 }
2397
2398 virtual void visit( const CommaExpr * old ) override final {
2399 this->node = visitBaseExpr( old,
2400 new ast::CommaExpr(
2401 old->location,
2402 GET_ACCEPT_1(arg1, Expr),
2403 GET_ACCEPT_1(arg2, Expr)
2404 )
2405 );
2406 }
2407
2408 virtual void visit( const TypeExpr * old ) override final {
2409 this->node = visitBaseExpr( old,
2410 new ast::TypeExpr(
2411 old->location,
2412 GET_ACCEPT_1(type, Type)
2413 )
2414 );
2415 }
2416
2417 virtual void visit( const AsmExpr * old ) override final {
2418 this->node = visitBaseExpr( old,
2419 new ast::AsmExpr(
2420 old->location,
2421 old->inout,
2422 GET_ACCEPT_1(constraint, Expr),
2423 GET_ACCEPT_1(operand, Expr)
2424 )
2425 );
2426 }
2427
2428 virtual void visit( const ImplicitCopyCtorExpr * old ) override final {
2429 auto rslt = new ast::ImplicitCopyCtorExpr(
2430 old->location,
2431 GET_ACCEPT_1(callExpr, ApplicationExpr)
2432 );
2433
2434 this->node = visitBaseExpr( old, rslt );
2435 }
2436
2437 virtual void visit( const ConstructorExpr * old ) override final {
2438 this->node = visitBaseExpr( old,
2439 new ast::ConstructorExpr(
2440 old->location,
2441 GET_ACCEPT_1(callExpr, Expr)
2442 )
2443 );
2444 }
2445
2446 virtual void visit( const CompoundLiteralExpr * old ) override final {
2447 this->node = visitBaseExpr_SkipResultType( old,
2448 new ast::CompoundLiteralExpr(
2449 old->location,
2450 GET_ACCEPT_1(result, Type),
2451 GET_ACCEPT_1(initializer, Init)
2452 )
2453 );
2454 }
2455
2456 virtual void visit( const RangeExpr * old ) override final {
2457 this->node = visitBaseExpr( old,
2458 new ast::RangeExpr(
2459 old->location,
2460 GET_ACCEPT_1(low, Expr),
2461 GET_ACCEPT_1(high, Expr)
2462 )
2463 );
2464 }
2465
2466 virtual void visit( const UntypedTupleExpr * old ) override final {
2467 this->node = visitBaseExpr( old,
2468 new ast::UntypedTupleExpr(
2469 old->location,
2470 GET_ACCEPT_V(exprs, Expr)
2471 )
2472 );
2473 }
2474
2475 virtual void visit( const TupleExpr * old ) override final {
2476 this->node = visitBaseExpr( old,
2477 new ast::TupleExpr(
2478 old->location,
2479 GET_ACCEPT_V(exprs, Expr)
2480 )
2481 );
2482 }
2483
2484 virtual void visit( const TupleIndexExpr * old ) override final {
2485 this->node = visitBaseExpr( old,
2486 new ast::TupleIndexExpr(
2487 old->location,
2488 GET_ACCEPT_1(tuple, Expr),
2489 old->index
2490 )
2491 );
2492 }
2493
2494 virtual void visit( const TupleAssignExpr * old ) override final {
2495 this->node = visitBaseExpr_SkipResultType( old,
2496 new ast::TupleAssignExpr(
2497 old->location,
2498 GET_ACCEPT_1(result, Type),
2499 GET_ACCEPT_1(stmtExpr, StmtExpr)
2500 )
2501 );
2502 }
2503
2504 virtual void visit( const StmtExpr * old ) override final {
2505 auto rslt = new ast::StmtExpr(
2506 old->location,
2507 GET_ACCEPT_1(statements, CompoundStmt)
2508 );
2509 rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2510 rslt->dtors = GET_ACCEPT_V(dtors , Expr);
2511
2512 this->node = visitBaseExpr_SkipResultType( old, rslt );
2513 }
2514
2515 virtual void visit( const UniqueExpr * old ) override final {
2516 auto rslt = new ast::UniqueExpr(
2517 old->location,
2518 GET_ACCEPT_1(expr, Expr),
2519 old->get_id()
2520 );
2521 rslt->object = GET_ACCEPT_1(object, ObjectDecl);
2522 rslt->var = GET_ACCEPT_1(var , VariableExpr);
2523
2524 this->node = visitBaseExpr( old, rslt );
2525 }
2526
2527 virtual void visit( const UntypedInitExpr * old ) override final {
2528 std::deque<ast::InitAlternative> initAlts;
2529 for (auto ia : old->initAlts) {
2530 initAlts.push_back(ast::InitAlternative(
2531 getAccept1< ast::Type, Type * >( ia.type ),
2532 getAccept1< ast::Designation, Designation * >( ia.designation )
2533 ));
2534 }
2535 this->node = visitBaseExpr( old,
2536 new ast::UntypedInitExpr(
2537 old->location,
2538 GET_ACCEPT_1(expr, Expr),
2539 std::move(initAlts)
2540 )
2541 );
2542 }
2543
2544 virtual void visit( const InitExpr * old ) override final {
2545 this->node = visitBaseExpr( old,
2546 new ast::InitExpr(
2547 old->location,
2548 GET_ACCEPT_1(expr, Expr),
2549 GET_ACCEPT_1(designation, Designation)
2550 )
2551 );
2552 }
2553
2554 virtual void visit( const DeletedExpr * old ) override final {
2555 this->node = visitBaseExpr( old,
2556 new ast::DeletedExpr(
2557 old->location,
2558 GET_ACCEPT_1(expr, Expr),
2559 inCache(old->deleteStmt) ?
2560 strict_dynamic_cast<ast::Decl*>(this->node) :
2561 GET_ACCEPT_1(deleteStmt, Decl)
2562 )
2563 );
2564 }
2565
2566 virtual void visit( const DefaultArgExpr * old ) override final {
2567 this->node = visitBaseExpr( old,
2568 new ast::DefaultArgExpr(
2569 old->location,
2570 GET_ACCEPT_1(expr, Expr)
2571 )
2572 );
2573 }
2574
2575 virtual void visit( const GenericExpr * old ) override final {
2576 std::vector<ast::GenericExpr::Association> associations;
2577 for (auto association : old->associations) {
2578 associations.push_back(ast::GenericExpr::Association(
2579 getAccept1< ast::Type, Type * >( association.type ),
2580 getAccept1< ast::Expr, Expression * >( association.expr )
2581 ));
2582 }
2583 this->node = visitBaseExpr( old,
2584 new ast::GenericExpr(
2585 old->location,
2586 GET_ACCEPT_1(control, Expr),
2587 std::move(associations)
2588 )
2589 );
2590 }
2591
2592 void visitType( const Type * old, ast::Type * type ) {
2593 // Some types do this in their constructor so add a check.
2594 if ( !old->attributes.empty() && type->attributes.empty() ) {
2595 type->attributes = GET_ACCEPT_V(attributes, Attribute);
2596 }
2597 this->node = type;
2598 }
2599
2600 virtual void visit( const VoidType * old ) override final {
2601 visitType( old, new ast::VoidType{ cv( old ) } );
2602 }
2603
2604 virtual void visit( const BasicType * old ) override final {
2605 auto type = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2606 // I believe this should always be a BasicType.
2607 if ( Validate::SizeType == old ) {
2608 ast::sizeType = type;
2609 }
2610 visitType( old, type );
2611 }
2612
2613 virtual void visit( const PointerType * old ) override final {
2614 visitType( old, new ast::PointerType{
2615 GET_ACCEPT_1( base, Type ),
2616 GET_ACCEPT_1( dimension, Expr ),
2617 (ast::LengthFlag)old->isVarLen,
2618 (ast::DimensionFlag)old->isStatic,
2619 cv( old )
2620 } );
2621 }
2622
2623 virtual void visit( const ArrayType * old ) override final {
2624 visitType( old, new ast::ArrayType{
2625 GET_ACCEPT_1( base, Type ),
2626 GET_ACCEPT_1( dimension, Expr ),
2627 (ast::LengthFlag)old->isVarLen,
2628 (ast::DimensionFlag)old->isStatic,
2629 cv( old )
2630 } );
2631 }
2632
2633 virtual void visit( const ReferenceType * old ) override final {
2634 visitType( old, new ast::ReferenceType{
2635 GET_ACCEPT_1( base, Type ),
2636 cv( old )
2637 } );
2638 }
2639
2640 virtual void visit( const QualifiedType * old ) override final {
2641 visitType( old, new ast::QualifiedType{
2642 GET_ACCEPT_1( parent, Type ),
2643 GET_ACCEPT_1( child, Type ),
2644 cv( old )
2645 } );
2646 }
2647
2648 virtual void visit( const FunctionType * old ) override final {
2649 auto ty = new ast::FunctionType {
2650 (ast::ArgumentFlag)old->isVarArgs,
2651 cv( old )
2652 };
2653 auto returnVars = GET_ACCEPT_V(returnVals, DeclWithType);
2654 auto paramVars = GET_ACCEPT_V(parameters, DeclWithType);
2655 // ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2656 // ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2657 for (auto & v: returnVars) {
2658 ty->returns.emplace_back(v->get_type());
2659 }
2660 for (auto & v: paramVars) {
2661 ty->params.emplace_back(v->get_type());
2662 }
2663 // xxx - when will this be non-null?
2664 // will have to create dangling (no-owner) decls to be pointed to
2665 auto foralls = GET_ACCEPT_V( forall, TypeDecl );
2666
2667 for (auto & param : foralls) {
2668 ty->forall.emplace_back(new ast::TypeInstType(param->name, param));
2669 for (auto asst : param->assertions) {
2670 ty->assertions.emplace_back(new ast::VariableExpr({}, asst));
2671 }
2672 }
2673 visitType( old, ty );
2674 }
2675
2676 void postvisit( const ReferenceToType * old, ast::BaseInstType * ty ) {
2677 ty->params = GET_ACCEPT_V( parameters, Expr );
2678 ty->hoistType = old->hoistType;
2679 visitType( old, ty );
2680 }
2681
2682 virtual void visit( const StructInstType * old ) override final {
2683 ast::StructInstType * ty;
2684 if ( old->baseStruct ) {
2685 ty = new ast::StructInstType{
2686 GET_ACCEPT_1( baseStruct, StructDecl ),
2687 cv( old ),
2688 GET_ACCEPT_V( attributes, Attribute )
2689 };
2690 } else {
2691 ty = new ast::StructInstType{
2692 old->name,
2693 cv( old ),
2694 GET_ACCEPT_V( attributes, Attribute )
2695 };
2696 }
2697 postvisit( old, ty );
2698 }
2699
2700 virtual void visit( const UnionInstType * old ) override final {
2701 ast::UnionInstType * ty;
2702 if ( old->baseUnion ) {
2703 ty = new ast::UnionInstType{
2704 GET_ACCEPT_1( baseUnion, UnionDecl ),
2705 cv( old ),
2706 GET_ACCEPT_V( attributes, Attribute )
2707 };
2708 } else {
2709 ty = new ast::UnionInstType{
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 EnumInstType * old ) override final {
2719 ast::EnumInstType * ty;
2720 if ( old->baseEnum ) {
2721 ty = new ast::EnumInstType{
2722 GET_ACCEPT_1( baseEnum, EnumDecl ),
2723 cv( old ),
2724 GET_ACCEPT_V( attributes, Attribute )
2725 };
2726 } else {
2727 ty = new ast::EnumInstType{
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 TraitInstType * old ) override final {
2737 ast::TraitInstType * ty;
2738 if ( old->baseTrait ) {
2739 ty = new ast::TraitInstType{
2740 GET_ACCEPT_1( baseTrait, TraitDecl ),
2741 cv( old ),
2742 GET_ACCEPT_V( attributes, Attribute )
2743 };
2744 } else {
2745 ty = new ast::TraitInstType{
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 TypeInstType * old ) override final {
2755 ast::TypeInstType * ty;
2756 if ( old->baseType ) {
2757 ty = new ast::TypeInstType{
2758 old->name,
2759 GET_ACCEPT_1( baseType, TypeDecl ),
2760 cv( old ),
2761 GET_ACCEPT_V( attributes, Attribute )
2762 };
2763 } else {
2764 ty = new ast::TypeInstType{
2765 old->name,
2766 old->isFtype ? ast::TypeDecl::Ftype : ast::TypeDecl::Dtype,
2767 cv( old ),
2768 GET_ACCEPT_V( attributes, Attribute )
2769 };
2770 }
2771 postvisit( old, ty );
2772 }
2773
2774 virtual void visit( const TupleType * old ) override final {
2775 visitType( old, new ast::TupleType{
2776 GET_ACCEPT_V( types, Type ),
2777 // members generated by TupleType c'tor
2778 cv( old )
2779 } );
2780 }
2781
2782 virtual void visit( const TypeofType * old ) override final {
2783 visitType( old, new ast::TypeofType{
2784 GET_ACCEPT_1( expr, Expr ),
2785 (ast::TypeofType::Kind)old->is_basetypeof,
2786 cv( old )
2787 } );
2788 }
2789
2790 virtual void visit( const AttrType * ) override final {
2791 assertf( false, "AttrType deprecated in new AST." );
2792 }
2793
2794 virtual void visit( const VarArgsType * old ) override final {
2795 visitType( old, new ast::VarArgsType{ cv( old ) } );
2796 }
2797
2798 virtual void visit( const ZeroType * old ) override final {
2799 visitType( old, new ast::ZeroType{ cv( old ) } );
2800 }
2801
2802 virtual void visit( const OneType * old ) override final {
2803 visitType( old, new ast::OneType{ cv( old ) } );
2804 }
2805
2806 virtual void visit( const GlobalScopeType * old ) override final {
2807 visitType( old, new ast::GlobalScopeType{} );
2808 }
2809
2810 virtual void visit( const Designation * old ) override final {
2811 this->node = new ast::Designation(
2812 old->location,
2813 GET_ACCEPT_D(designators, Expr)
2814 );
2815 }
2816
2817 virtual void visit( const SingleInit * old ) override final {
2818 this->node = new ast::SingleInit(
2819 old->location,
2820 GET_ACCEPT_1(value, Expr),
2821 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2822 );
2823 }
2824
2825 virtual void visit( const ListInit * old ) override final {
2826 this->node = new ast::ListInit(
2827 old->location,
2828 GET_ACCEPT_V(initializers, Init),
2829 GET_ACCEPT_V(designations, Designation),
2830 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::NoConstruct
2831 );
2832 }
2833
2834 virtual void visit( const ConstructorInit * old ) override final {
2835 this->node = new ast::ConstructorInit(
2836 old->location,
2837 GET_ACCEPT_1(ctor, Stmt),
2838 GET_ACCEPT_1(dtor, Stmt),
2839 GET_ACCEPT_1(init, Init)
2840 );
2841 }
2842
2843 virtual void visit( const Constant * ) override final {
2844 // Handled in visit( ConstantEpxr * ).
2845 // In the new tree, Constant fields are inlined into containing ConstantExpression.
2846 assert( 0 );
2847 }
2848
2849 virtual void visit( const Attribute * old ) override final {
2850 this->node = new ast::Attribute(
2851 old->name,
2852 GET_ACCEPT_V( parameters, Expr )
2853 );
2854 }
2855};
2856
2857#undef GET_LABELS_V
2858#undef GET_ACCEPT_V
2859#undef GET_ACCEPT_1
2860
2861ast::TranslationUnit convert( const std::list< Declaration * > && translationUnit ) {
2862 ConverterOldToNew c;
2863 ast::TranslationUnit unit;
2864 if (Validate::SizeType) {
2865 // this should be a BasicType.
2866 auto old = strict_dynamic_cast<BasicType *>(Validate::SizeType);
2867 ast::sizeType = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind };
2868 }
2869
2870 for(auto d : translationUnit) {
2871 d->accept( c );
2872 unit.decls.emplace_back( c.decl() );
2873 }
2874 deleteAll(translationUnit);
2875
2876 // Load the local static varables into the global store.
2877 unit.global.sizeType = ast::sizeType;
2878 unit.global.dereference = ast::dereferenceOperator;
2879 unit.global.dtorStruct = ast::dtorStruct;
2880 unit.global.dtorDestroy = ast::dtorStructDestroy;
2881
2882 return unit;
2883}
Note: See TracBrowser for help on using the repository browser.