source: src/AST/Convert.cpp@ 1958fec

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 1958fec was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 5 years ago

reimplement function type and eliminate deep copy

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