source: src/AST/Convert.cpp@ 74ad8c0

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 74ad8c0 was 74ad8c0, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Implemented initializers and designation conversion.

  • Property mode set to 100644
File size: 67.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 : Andrew Beach
12// Last Modified On : Tue May 21 15:30:00 2019
13// Update Count : 5
14//
15
16#include "Convert.hpp"
17
18#include <unordered_map>
19
20#include "AST/Attribute.hpp"
21#include "AST/Decl.hpp"
22#include "AST/Expr.hpp"
23#include "AST/Init.hpp"
24#include "AST/Stmt.hpp"
25#include "AST/TypeSubstitution.hpp"
26
27#include "SynTree/Attribute.h"
28#include "SynTree/Declaration.h"
29#include "SynTree/TypeSubstitution.h"
30
31//================================================================================================
32// Utilities
33template<template <class...> class C>
34struct to {
35 template<typename T>
36 static auto from( T && v ) -> C< typename T::value_type > {
37 C< typename T::value_type > l;
38 std::move(std::begin(v), std::end(v), std::back_inserter(l));
39 return l;
40 }
41};
42
43//================================================================================================
44class ConverterNewToOld : public ast::Visitor {
45 BaseSyntaxNode * node = nullptr;
46 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
47 Cache cache;
48
49 template<typename T>
50 struct Getter {
51 ConverterNewToOld & visitor;
52
53 template<typename U, enum ast::Node::ref_type R>
54 T * accept1( const ast::ptr_base<U, R> & ptr ) {
55 if ( ! ptr ) return nullptr;
56 ptr->accept( visitor );
57 T * ret = strict_dynamic_cast< T * >( visitor.node );
58 visitor.node = nullptr;
59 return ret;
60 }
61
62 template<typename U>
63 std::list< T * > acceptL( const U & container ) {
64 std::list< T * > ret;
65 for (auto ptr : container ) {
66 ret.emplace_back( accept1( ptr ) );
67 }
68 return ret;
69 }
70 };
71
72 template<typename T>
73 Getter<T> get() {
74 return Getter<T>{ *this };
75 }
76
77 Label makeLabel(Statement * labelled, const ast::Label& label) {
78 return Label(
79 label.name,
80 labelled,
81 get<Attribute>().acceptL(label.attributes)
82 );
83 }
84
85 template<template <class...> class C>
86 std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
87 std::list<Label> ret;
88 for (auto label : labels) {
89 ret.push_back( makeLabel(labelled, label) );
90 }
91 return ret;
92 }
93
94 /// get new qualifiers from old type
95 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
96
97 /// returns true and sets `node` if in cache
98 bool inCache( const ast::Node * node ) {
99 auto it = cache.find( node );
100 if ( it == cache.end() ) return false;
101 this->node = it->second;
102 return true;
103 }
104
105public:
106 Declaration * decl( const ast::Decl * declNode ) {
107 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
108 }
109
110private:
111 void declPostamble( Declaration * decl, const ast::Decl * node ) {
112 decl->location = node->location;
113 // name comes from constructor
114 // linkage comes from constructor
115 decl->extension = node->extension;
116 decl->uniqueId = node->uniqueId;
117 // storageClasses comes from constructor
118 this->node = decl;
119 }
120
121 const ast::DeclWithType * declWithTypePostamble (
122 DeclarationWithType * decl, const ast::DeclWithType * node ) {
123 declPostamble( decl, node );
124 decl->mangleName = node->mangleName;
125 decl->scopeLevel = node->scopeLevel;
126 decl->asmName = get<Expression>().accept1( node->asmName );
127 // attributes comes from constructor
128 decl->isDeleted = node->isDeleted;
129 // fs comes from constructor
130 cache.emplace( node, decl );
131 return nullptr;
132 }
133
134 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
135 if ( inCache( node ) ) return nullptr;
136 auto decl = new ObjectDecl(
137 node->name,
138 Type::StorageClasses( node->storage.val ),
139 LinkageSpec::Spec( node->linkage.val ),
140 get<Expression>().accept1( node->bitfieldWidth ),
141 get<Type>().accept1( node->type ),
142 get<Initializer>().accept1( node->init ),
143 get<Attribute>().acceptL( node->attributes ),
144 Type::FuncSpecifiers( node->funcSpec.val )
145 );
146 return declWithTypePostamble( decl, node );
147 }
148
149 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
150 if ( inCache( node ) ) return nullptr;
151 auto decl = new FunctionDecl(
152 node->name,
153 Type::StorageClasses( node->storage.val ),
154 LinkageSpec::Spec( node->linkage.val ),
155 get<FunctionType>().accept1( node->type ),
156 get<CompoundStmt>().accept1( node->stmts ),
157 get<Attribute>().acceptL( node->attributes ),
158 Type::FuncSpecifiers( node->funcSpec.val )
159 );
160 decl->withExprs = get<Expression>().acceptL( node->withExprs );
161 return declWithTypePostamble( decl, node );
162 }
163
164 // NamedTypeDecl
165 const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) {
166 declPostamble( decl, node );
167 // base comes from constructor
168 decl->parameters = get<TypeDecl>().acceptL( node->params );
169 decl->assertions = get<DeclarationWithType>().acceptL( node->assertions );
170 return nullptr;
171 }
172
173 const ast::Decl * visit( const ast::TypeDecl * node ) override final {
174 if ( inCache( node ) ) return nullptr;
175 auto decl = new TypeDecl(
176 node->name,
177 Type::StorageClasses( node->storage.val ),
178 get<Type>().accept1( node->base ),
179 (TypeDecl::Kind)(unsigned)node->kind,
180 node->sized,
181 get<Type>().accept1( node->init )
182 );
183 cache.emplace( node, decl );
184 return namedTypePostamble( decl, node );
185 }
186
187 const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
188 auto decl = new TypedefDecl(
189 node->name,
190 node->location,
191 Type::StorageClasses( node->storage.val ),
192 get<Type>().accept1( node->base ),
193 LinkageSpec::Spec( node->linkage.val )
194 );
195 return namedTypePostamble( decl, node );
196 }
197
198 const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) {
199 decl->members = get<Declaration>().acceptL( node->members );
200 decl->parameters = get<TypeDecl>().acceptL( node->params );
201 decl->body = node->body;
202 // attributes come from constructor
203 decl->parent = get<AggregateDecl>().accept1( node->parent );
204 cache.emplace( node, decl );
205 return nullptr;
206 }
207
208 const ast::Decl * visit( const ast::StructDecl * node ) override final {
209 if ( inCache( node ) ) return nullptr;
210 auto decl = new StructDecl(
211 node->name,
212 node->kind,
213 get<Attribute>().acceptL( node->attributes ),
214 LinkageSpec::Spec( node->linkage.val )
215 );
216 return aggregatePostamble( decl, node );
217 }
218
219 const ast::Decl * visit( const ast::UnionDecl * node ) override final {
220 if ( inCache( node ) ) return nullptr;
221 auto decl = new UnionDecl(
222 node->name,
223 get<Attribute>().acceptL( node->attributes ),
224 LinkageSpec::Spec( node->linkage.val )
225 );
226 return aggregatePostamble( decl, node );
227 }
228
229 const ast::Decl * visit( const ast::EnumDecl * node ) override final {
230 if ( inCache( node ) ) return nullptr;
231 auto decl = new EnumDecl(
232 node->name,
233 get<Attribute>().acceptL( node->attributes ),
234 LinkageSpec::Spec( node->linkage.val )
235 );
236 return aggregatePostamble( decl, node );
237 }
238
239 const ast::Decl * visit( const ast::TraitDecl * node ) override final {
240 if ( inCache( node ) ) return nullptr;
241 auto decl = new TraitDecl(
242 node->name,
243 {},
244 LinkageSpec::Spec( node->linkage.val )
245 );
246 return aggregatePostamble( decl, node );
247 }
248
249 const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
250 auto decl = new AsmDecl( get<AsmStmt>().accept1( node->stmt ) );
251 declPostamble( decl, node );
252 return nullptr;
253 }
254
255 const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
256 auto decl = new StaticAssertDecl(
257 get<Expression>().accept1( node->cond ),
258 get<ConstantExpr>().accept1( node->msg )
259 );
260 declPostamble( decl, node );
261 return nullptr;
262 }
263
264 const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
265 stmt->location = node->location;
266 stmt->labels = makeLabelL( stmt, node->labels );
267 this->node = stmt;
268 return nullptr;
269 }
270
271 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
272 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
273 stmtPostamble( stmt, node );
274 return nullptr;
275 }
276
277 const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
278 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
279 return stmtPostamble( stmt, node );
280 }
281
282 const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
283 auto stmt = new AsmStmt(
284 node->isVolatile,
285 get<Expression>().accept1( node->instruction ),
286 get<Expression>().acceptL( node->output ),
287 get<Expression>().acceptL( node->input ),
288 get<ConstantExpr>().acceptL( node->clobber ),
289 makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
290 );
291 return stmtPostamble( stmt, node );
292 }
293
294 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
295 auto stmt = new DirectiveStmt( node->directive );
296 return stmtPostamble( stmt, node );
297 }
298
299 const ast::Stmt * visit( const ast::IfStmt * node ) override final {
300 auto stmt = new IfStmt(
301 get<Expression>().accept1( node->cond ),
302 get<Statement>().accept1( node->thenPart ),
303 get<Statement>().accept1( node->elsePart ),
304 get<Statement>().acceptL( node->inits )
305 );
306 return stmtPostamble( stmt, node );
307 }
308
309 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
310 auto stmt = new SwitchStmt(
311 get<Expression>().accept1( node->cond ),
312 get<Statement>().acceptL( node->stmts )
313 );
314 return stmtPostamble( stmt, node );
315 }
316
317 const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
318 auto stmt = new CaseStmt(
319 get<Expression>().accept1( node->cond ),
320 get<Statement>().acceptL( node->stmts ),
321 node->isDefault()
322 );
323 return stmtPostamble( stmt, node );
324 }
325
326 const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
327 auto inits = get<Statement>().acceptL( node->inits );
328 auto stmt = new WhileStmt(
329 get<Expression>().accept1( node->cond ),
330 get<Statement>().accept1( node->body ),
331 inits,
332 node->isDoWhile
333 );
334 return stmtPostamble( stmt, node );
335 }
336
337 const ast::Stmt * visit( const ast::ForStmt * node ) override final {
338 auto stmt = new ForStmt(
339 get<Statement>().acceptL( node->inits ),
340 get<Expression>().accept1( node->cond ),
341 get<Expression>().accept1( node->inc ),
342 get<Statement>().accept1( node->body )
343 );
344 return stmtPostamble( stmt, node );
345 }
346
347 const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
348 BranchStmt * stmt;
349 if (node->computedTarget) {
350 stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
351 BranchStmt::Goto );
352 } else {
353 BranchStmt::Type type;
354 switch (node->kind) {
355 #define CASE(n) \
356 case ast::BranchStmt::n: \
357 type = BranchStmt::n; \
358 break
359 CASE(Goto);
360 CASE(Break);
361 CASE(Continue);
362 CASE(FallThrough);
363 CASE(FallThroughDefault);
364 #undef CASE
365 default:
366 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
367 }
368
369 // The labels here are also weird.
370 stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
371 stmt->target = makeLabel( stmt, node->target );
372 }
373 return stmtPostamble( stmt, node );
374 }
375
376 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
377 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
378 return stmtPostamble( stmt, node );
379 }
380
381 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
382 ThrowStmt::Kind kind;
383 switch (node->kind) {
384 case ast::ThrowStmt::Terminate:
385 kind = ThrowStmt::Terminate;
386 break;
387 case ast::ThrowStmt::Resume:
388 kind = ThrowStmt::Resume;
389 break;
390 default:
391 assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
392 }
393 auto stmt = new ThrowStmt(
394 kind,
395 get<Expression>().accept1( node->expr ),
396 get<Expression>().accept1( node->target )
397 );
398 return stmtPostamble( stmt, node );
399 }
400
401 const ast::Stmt * visit( const ast::TryStmt * node ) override final {
402 auto handlers = get<CatchStmt>().acceptL( node->handlers );
403 auto stmt = new TryStmt(
404 get<CompoundStmt>().accept1( node->body ),
405 handlers,
406 get<FinallyStmt>().accept1( node->finally )
407 );
408 return stmtPostamble( stmt, node );
409 }
410
411 const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
412 CatchStmt::Kind kind;
413 switch (node->kind) {
414 case ast::CatchStmt::Terminate:
415 kind = CatchStmt::Terminate;
416 break;
417 case ast::CatchStmt::Resume:
418 kind = CatchStmt::Resume;
419 break;
420 default:
421 assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
422 }
423 auto stmt = new CatchStmt(
424 kind,
425 get<Declaration>().accept1( node->decl ),
426 get<Expression>().accept1( node->cond ),
427 get<Statement>().accept1( node->body )
428 );
429 return stmtPostamble( stmt, node );
430 }
431
432 const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
433 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
434 return stmtPostamble( stmt, node );
435 }
436
437 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
438 auto stmt = new WaitForStmt;
439 stmt->clauses.reserve( node->clauses.size() );
440 for ( auto clause : node->clauses ) {
441 stmt->clauses.push_back({{
442 get<Expression>().accept1( clause.target.func ),
443 get<Expression>().acceptL( clause.target.args ),
444 },
445 get<Statement>().accept1( clause.stmt ),
446 get<Expression>().accept1( clause.cond ),
447 });
448 }
449 stmt->timeout = {
450 get<Expression>().accept1( node->timeout.time ),
451 get<Statement>().accept1( node->timeout.stmt ),
452 get<Expression>().accept1( node->timeout.cond ),
453 };
454 stmt->orelse = {
455 get<Statement>().accept1( node->orElse.stmt ),
456 get<Expression>().accept1( node->orElse.cond ),
457 };
458 return stmtPostamble( stmt, node );
459 }
460
461 const ast::Stmt * visit( const ast::WithStmt * node ) override final {
462 auto stmt = new WithStmt(
463 get<Expression>().acceptL( node->exprs ),
464 get<Statement>().accept1( node->stmt )
465 );
466 return stmtPostamble( stmt, node );
467 }
468
469 const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
470 auto stmt = new NullStmt();
471 stmtPostamble( stmt, node );
472 return nullptr;
473 }
474
475 const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
476 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
477 return stmtPostamble( stmt, node );
478 }
479
480 const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
481 (void)node;
482 return nullptr;
483 }
484
485 TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
486
487 TypeSubstitution *rslt = new TypeSubstitution();
488
489 for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
490 rslt->add( src_i->first,
491 get<Type>().accept1(src_i->second) );
492 }
493
494 for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
495 rslt->addVar( src_i->first,
496 get<Expression>().accept1(src_i->second) );
497 }
498
499 return rslt;
500 }
501
502 void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
503 std::vector<UniqueId> &tgtResnSlots,
504 const ast::Expr::InferUnion &srcInferred ) {
505
506 assert( tgtInferParams.empty() );
507 assert( tgtResnSlots.empty() );
508
509 if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
510 const ast::InferredParams &srcParams = srcInferred.inferParamsConst();
511 for (auto srcParam : srcParams) {
512 tgtInferParams[srcParam.first] = ParamEntry(
513 srcParam.second.decl,
514 get<Type>().accept1(srcParam.second.actualType),
515 get<Type>().accept1(srcParam.second.formalType),
516 get<Expression>().accept1(srcParam.second.expr)
517 );
518 }
519 } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots ) {
520 const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst();
521 for (auto srcSlot : srcSlots) {
522 tgtResnSlots.push_back(srcSlot);
523 }
524 }
525 }
526
527 Expression * visitBaseExpr_skipResultType(const ast::Expr * src, Expression * tgt) {
528
529 tgt->location = src->location;
530 tgt->env = convertTypeSubstitution(src->env);
531 tgt->extension = src->extension;
532
533 convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
534 return tgt;
535 }
536
537 Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
538
539 tgt->result = get<Type>().accept1(src->result);
540 return visitBaseExpr_skipResultType(src, tgt);
541 }
542
543 const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
544 auto expr = visitBaseExpr( node,
545 new ApplicationExpr(
546 get<Expression>().accept1(node->func),
547 get<Expression>().acceptL(node->args)
548 )
549 );
550 this->node = expr;
551 return nullptr;
552 }
553
554 const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
555 auto expr = visitBaseExpr( node,
556 new UntypedExpr(
557 get<Expression>().accept1(node->func),
558 get<Expression>().acceptL(node->args)
559 )
560 );
561 this->node = expr;
562 return nullptr;
563 }
564
565 const ast::Expr * visit( const ast::NameExpr * node ) override final {
566 auto expr = visitBaseExpr( node,
567 new NameExpr(
568 node->name
569 )
570 );
571 this->node = expr;
572 return nullptr;
573 }
574
575 const ast::Expr * visit( const ast::AddressExpr * node ) override final {
576 auto expr = visitBaseExpr( node,
577 new AddressExpr(
578 get<Expression>().accept1(node->arg)
579 )
580 );
581 this->node = expr;
582 return nullptr;
583 }
584
585 const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
586 auto expr = visitBaseExpr( node,
587 new LabelAddressExpr(
588 makeLabel(nullptr, node->arg)
589 )
590 );
591 this->node = expr;
592 return nullptr;
593 }
594
595 const ast::Expr * visit( const ast::CastExpr * node ) override final {
596 auto expr = visitBaseExpr( node,
597 new CastExpr(
598 get<Expression>().accept1(node->arg),
599 (node->isGenerated == ast::GeneratedCast)
600 )
601 );
602 this->node = expr;
603 return nullptr;
604 }
605
606 const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
607 KeywordCastExpr::Target castTarget = KeywordCastExpr::NUMBER_OF_TARGETS;
608 switch (node->target) {
609 case ast::KeywordCastExpr::Coroutine:
610 castTarget = KeywordCastExpr::Coroutine;
611 break;
612 case ast::KeywordCastExpr::Thread:
613 castTarget = KeywordCastExpr::Thread;
614 break;
615 case ast::KeywordCastExpr::Monitor:
616 castTarget = KeywordCastExpr::Monitor;
617 break;
618 default:
619 break;
620 }
621 assert ( castTarget < KeywordCastExpr::NUMBER_OF_TARGETS );
622 auto expr = visitBaseExpr( node,
623 new KeywordCastExpr(
624 get<Expression>().accept1(node->arg),
625 castTarget
626 )
627 );
628 this->node = expr;
629 return nullptr;
630 }
631
632 const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
633 auto expr = visitBaseExpr_skipResultType( node,
634 new VirtualCastExpr(
635 get<Expression>().accept1(node->arg),
636 get<Type>().accept1(node->result)
637 )
638 );
639 this->node = expr;
640 return nullptr;
641 }
642
643 const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
644 auto expr = visitBaseExpr( node,
645 new UntypedMemberExpr(
646 get<Expression>().accept1(node->member),
647 get<Expression>().accept1(node->aggregate)
648 )
649 );
650 this->node = expr;
651 return nullptr;
652 }
653
654 const ast::Expr * visit( const ast::MemberExpr * node ) override final {
655 auto expr = visitBaseExpr( node,
656 new MemberExpr(
657 inCache(node->member) ?
658 dynamic_cast<DeclarationWithType *>(this->node) :
659 get<DeclarationWithType>().accept1(node->member),
660 get<Expression>().accept1(node->aggregate)
661 )
662 );
663 this->node = expr;
664 return nullptr;
665 }
666
667 const ast::Expr * visit( const ast::VariableExpr * node ) override final {
668 auto expr = visitBaseExpr( node,
669 new VariableExpr(
670 inCache(node->var) ?
671 dynamic_cast<DeclarationWithType *>(this->node) :
672 get<DeclarationWithType>().accept1(node->var)
673 )
674 );
675 this->node = expr;
676 return nullptr;
677 }
678
679 bool isIntlikeConstantType(const ast::Type *t) {
680 if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) {
681 if ( basicType->isInteger() ) {
682 return true;
683 }
684 } else if ( dynamic_cast< const ast::OneType * >( t ) ) {
685 return true;
686 } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) {
687 return true;
688 } else if ( dynamic_cast< const ast::PointerType * >( t ) ) {
689 // null pointer constants, with zero int-values
690 return true;
691 }
692 return false;
693 }
694
695 bool isFloatlikeConstantType(const ast::Type *t) {
696 if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) {
697 if ( ! bty->isInteger() ) {
698 return true;
699 }
700 }
701 return false;
702 }
703
704 bool isStringlikeConstantType(const ast::Type *t) {
705 if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) {
706 if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) {
707 if ( bty->kind == ast::BasicType::Kind::Char ) {
708 return true;
709 }
710 }
711 }
712 return false;
713 }
714
715 const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
716 ConstantExpr *rslt = nullptr;
717 if (isIntlikeConstantType(node->result)) {
718 rslt = new ConstantExpr(Constant(
719 get<Type>().accept1(node->result),
720 node->rep,
721 (unsigned long long) node->intValue()
722 ));
723 } else if (isFloatlikeConstantType(node->result)) {
724 rslt = new ConstantExpr(Constant(
725 get<Type>().accept1(node->result),
726 node->rep,
727 (double) node->floatValue()
728 ));
729 } else if (isStringlikeConstantType(node->result)) {
730 rslt = new ConstantExpr(Constant::from_string(
731 node->rep
732 ));
733 }
734 assert(rslt);
735 auto expr = visitBaseExpr( node, rslt );
736 this->node = expr;
737 return nullptr;
738 }
739
740 const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
741 assert (node->expr || node->type);
742 assert (! (node->expr && node->type));
743 SizeofExpr *rslt;
744 if (node->expr) {
745 rslt = new SizeofExpr(
746 get<Expression>().accept1(node->expr)
747 );
748 assert (!rslt->isType);
749 }
750 if (node->type) {
751 rslt = new SizeofExpr(
752 get<Type>().accept1(node->type)
753 );
754 assert (rslt->isType);
755 }
756 auto expr = visitBaseExpr( node, rslt );
757 this->node = expr;
758 return nullptr;
759 }
760
761 const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
762 assert (node->expr || node->type);
763 assert (! (node->expr && node->type));
764 AlignofExpr *rslt;
765 if (node->expr) {
766 rslt = new AlignofExpr(
767 get<Expression>().accept1(node->expr)
768 );
769 assert (!rslt->isType);
770 }
771 if (node->type) {
772 rslt = new AlignofExpr(
773 get<Type>().accept1(node->type)
774 );
775 assert (rslt->isType);
776 }
777 auto expr = visitBaseExpr( node, rslt );
778 this->node = expr;
779 return nullptr;
780 }
781
782 const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
783 auto expr = visitBaseExpr( node,
784 new UntypedOffsetofExpr(
785 get<Type>().accept1(node->type),
786 node->member
787 )
788 );
789 this->node = expr;
790 return nullptr;
791 }
792
793 const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
794 auto expr = visitBaseExpr( node,
795 new OffsetofExpr(
796 get<Type>().accept1(node->type),
797 inCache(node->member) ?
798 dynamic_cast<DeclarationWithType *>(this->node) :
799 get<DeclarationWithType>().accept1(node->member)
800 )
801 );
802 this->node = expr;
803 return nullptr;
804 }
805
806 const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
807 auto expr = visitBaseExpr( node,
808 new OffsetPackExpr(
809 get<StructInstType>().accept1(node->type)
810 )
811 );
812 this->node = expr;
813 return nullptr;
814 }
815
816 const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
817 assert (node->isAnd == ast::LogicalFlag::AndExpr ||
818 node->isAnd == ast::LogicalFlag::OrExpr );
819 auto expr = visitBaseExpr( node,
820 new LogicalExpr(
821 get<Expression>().accept1(node->arg1),
822 get<Expression>().accept1(node->arg2),
823 (node->isAnd == ast::LogicalFlag::AndExpr)
824 )
825 );
826 this->node = expr;
827 return nullptr;
828 }
829
830 const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
831 auto expr = visitBaseExpr( node,
832 new ConditionalExpr(
833 get<Expression>().accept1(node->arg1),
834 get<Expression>().accept1(node->arg2),
835 get<Expression>().accept1(node->arg3)
836 )
837 );
838 this->node = expr;
839 return nullptr;
840 }
841
842 const ast::Expr * visit( const ast::CommaExpr * node ) override final {
843 auto expr = visitBaseExpr( node,
844 new CommaExpr(
845 get<Expression>().accept1(node->arg1),
846 get<Expression>().accept1(node->arg2)
847 )
848 );
849 this->node = expr;
850 return nullptr;
851 }
852
853 const ast::Expr * visit( const ast::TypeExpr * node ) override final {
854 auto expr = visitBaseExpr( node,
855 new TypeExpr(
856 get<Type>().accept1(node->type)
857 )
858 );
859 this->node = expr;
860 return nullptr;
861 }
862
863 const ast::Expr * visit( const ast::AsmExpr * node ) override final {
864 auto expr = visitBaseExpr( node,
865 new AsmExpr(
866 get<Expression>().accept1(node->inout),
867 get<Expression>().accept1(node->constraint),
868 get<Expression>().accept1(node->operand)
869 )
870 );
871 this->node = expr;
872 return nullptr;
873 }
874
875 const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
876 auto rslt = new ImplicitCopyCtorExpr(
877 get<ApplicationExpr>().accept1(node->callExpr)
878 );
879
880 rslt->tempDecls = get<ObjectDecl>().acceptL(node->tempDecls);
881 rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
882 rslt->dtors = get<Expression>().acceptL(node->dtors);
883
884 auto expr = visitBaseExpr( node, rslt );
885 this->node = expr;
886 return nullptr;
887 }
888
889 const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
890 auto expr = visitBaseExpr( node,
891 new ConstructorExpr(
892 get<Expression>().accept1(node->callExpr)
893 )
894 );
895 this->node = expr;
896 return nullptr;
897 }
898
899 const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
900 auto expr = visitBaseExpr_skipResultType( node,
901 new CompoundLiteralExpr(
902 get<Type>().accept1(node->result),
903 get<Initializer>().accept1(node->init)
904 )
905 );
906 this->node = expr;
907 return nullptr;
908 }
909
910 const ast::Expr * visit( const ast::RangeExpr * node ) override final {
911 auto expr = visitBaseExpr( node,
912 new RangeExpr(
913 get<Expression>().accept1(node->low),
914 get<Expression>().accept1(node->high)
915 )
916 );
917 this->node = expr;
918 return nullptr;
919 }
920
921 const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
922 auto expr = visitBaseExpr( node,
923 new UntypedTupleExpr(
924 get<Expression>().acceptL(node->exprs)
925 )
926 );
927 this->node = expr;
928 return nullptr;
929 }
930
931 const ast::Expr * visit( const ast::TupleExpr * node ) override final {
932 auto expr = visitBaseExpr( node,
933 new UntypedTupleExpr(
934 get<Expression>().acceptL(node->exprs)
935 )
936 );
937 this->node = expr;
938 return nullptr;
939 }
940
941 const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
942 auto expr = visitBaseExpr( node,
943 new TupleIndexExpr(
944 get<Expression>().accept1(node->tuple),
945 node->index
946 )
947 );
948 this->node = expr;
949 return nullptr;
950 }
951
952 const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
953 auto expr = visitBaseExpr( node,
954 new TupleAssignExpr(
955 get<StmtExpr>().accept1(node->stmtExpr)
956 )
957 );
958 this->node = expr;
959 return nullptr;
960 }
961
962 const ast::Expr * visit( const ast::StmtExpr * node ) override final {
963 auto rslt = new StmtExpr(
964 get<CompoundStmt>().accept1(node->stmts)
965 );
966
967 rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
968 rslt->dtors = get<Expression>().acceptL(node->dtors);
969
970 auto expr = visitBaseExpr( node, rslt );
971 this->node = expr;
972 return nullptr;
973 }
974
975 const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
976 auto rslt = new UniqueExpr(
977 get<Expression>().accept1(node->expr)
978 );
979
980 rslt->object = get<ObjectDecl> ().accept1(node->object);
981 rslt->var = get<VariableExpr>().accept1(node->var);
982
983 auto expr = visitBaseExpr( node, rslt );
984 this->node = expr;
985 return nullptr;
986 }
987
988 const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
989 std::list<InitAlternative> initAlts;
990 for (auto ia : node->initAlts) {
991 initAlts.push_back(InitAlternative(
992 get<Type> ().accept1(ia.type),
993 get<Designation>().accept1(ia.designation)
994 ));
995 }
996 auto expr = visitBaseExpr( node,
997 new UntypedInitExpr(
998 get<Expression>().accept1(node->expr),
999 initAlts
1000 )
1001 );
1002 this->node = expr;
1003 return nullptr;
1004 }
1005
1006 const ast::Expr * visit( const ast::InitExpr * node ) override final {
1007 auto expr = visitBaseExpr( node,
1008 new InitExpr(
1009 get<Expression>().accept1(node->expr),
1010 get<Designation>().accept1(node->designation)
1011 )
1012 );
1013 this->node = expr;
1014 return nullptr;
1015 }
1016
1017 const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
1018 auto expr = visitBaseExpr( node,
1019 new DeletedExpr(
1020 get<Expression>().accept1(node->expr),
1021 inCache(node->deleteStmt) ?
1022 this->node :
1023 get<BaseSyntaxNode>().accept1(node->deleteStmt)
1024 )
1025 );
1026 this->node = expr;
1027 return nullptr;
1028 }
1029
1030 const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
1031 auto expr = visitBaseExpr( node,
1032 new DefaultArgExpr(
1033 get<Expression>().accept1(node->expr)
1034 )
1035 );
1036 this->node = expr;
1037 return nullptr;
1038 }
1039
1040 const ast::Expr * visit( const ast::GenericExpr * node ) override final {
1041 std::list<GenericExpr::Association> associations;
1042 for (auto association : node->associations) {
1043 associations.push_back(GenericExpr::Association(
1044 get<Type> ().accept1(association.type),
1045 get<Expression>().accept1(association.expr)
1046 ));
1047 }
1048 auto expr = visitBaseExpr( node,
1049 new GenericExpr(
1050 get<Expression>().accept1(node->control),
1051 associations
1052 )
1053 );
1054 this->node = expr;
1055 return nullptr;
1056 }
1057
1058 const ast::Type * visit( const ast::VoidType * node ) override final {
1059 this->node = new VoidType{ cv( node ) };
1060 return nullptr;
1061 }
1062
1063 const ast::Type * visit( const ast::BasicType * node ) override final {
1064 this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
1065 return nullptr;
1066 }
1067
1068 const ast::Type * visit( const ast::PointerType * node ) override final {
1069 this->node = new PointerType{
1070 cv( node ),
1071 get<Type>().accept1( node->base ),
1072 get<Expression>().accept1( node->dimension ),
1073 (bool)node->isVarLen,
1074 (bool)node->isStatic
1075 };
1076 return nullptr;
1077 }
1078
1079 const ast::Type * visit( const ast::ArrayType * node ) override final {
1080 this->node = new ArrayType{
1081 cv( node ),
1082 get<Type>().accept1( node->base ),
1083 get<Expression>().accept1( node->dimension ),
1084 (bool)node->isVarLen,
1085 (bool)node->isStatic
1086 };
1087 return nullptr;
1088 }
1089
1090 const ast::Type * visit( const ast::ReferenceType * node ) override final {
1091 this->node = new ReferenceType{
1092 cv( node ),
1093 get<Type>().accept1( node->base )
1094 };
1095 return nullptr;
1096 }
1097
1098 const ast::Type * visit( const ast::QualifiedType * node ) override final {
1099 this->node = new QualifiedType{
1100 cv( node ),
1101 get<Type>().accept1( node->parent ),
1102 get<Type>().accept1( node->child )
1103 };
1104 return nullptr;
1105 }
1106
1107 const ast::Type * visit( const ast::FunctionType * node ) override final {
1108 auto ty = new FunctionType {
1109 cv( node ),
1110 (bool)node->isVarArgs
1111 };
1112 ty->returnVals = get<DeclarationWithType>().acceptL( node->returns );
1113 ty->parameters = get<DeclarationWithType>().acceptL( node->params );
1114 ty->forall = get<TypeDecl>().acceptL( node->forall );
1115 this->node = ty;
1116 return nullptr;
1117 }
1118
1119 void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {
1120 ty->forall = get<TypeDecl>().acceptL( old->forall );
1121 ty->parameters = get<Expression>().acceptL( old->params );
1122 ty->hoistType = old->hoistType;
1123 }
1124
1125 const ast::Type * visit( const ast::StructInstType * node ) override final {
1126 StructInstType * ty;
1127 if ( node->base ) {
1128 ty = new StructInstType{
1129 cv( node ),
1130 get<StructDecl>().accept1( node->base ),
1131 get<Attribute>().acceptL( node->attributes )
1132 };
1133 } else {
1134 ty = new StructInstType{
1135 cv( node ),
1136 node->name,
1137 get<Attribute>().acceptL( node->attributes )
1138 };
1139 }
1140 postvisit( node, ty );
1141 this->node = ty;
1142 return nullptr;
1143 }
1144
1145 const ast::Type * visit( const ast::UnionInstType * node ) override final {
1146 UnionInstType * ty;
1147 if ( node->base ) {
1148 ty = new UnionInstType{
1149 cv( node ),
1150 get<UnionDecl>().accept1( node->base ),
1151 get<Attribute>().acceptL( node->attributes )
1152 };
1153 } else {
1154 ty = new UnionInstType{
1155 cv( node ),
1156 node->name,
1157 get<Attribute>().acceptL( node->attributes )
1158 };
1159 }
1160 postvisit( node, ty );
1161 this->node = ty;
1162 return nullptr;
1163 }
1164
1165 const ast::Type * visit( const ast::EnumInstType * node ) override final {
1166 EnumInstType * ty;
1167 if ( node->base ) {
1168 ty = new EnumInstType{
1169 cv( node ),
1170 get<EnumDecl>().accept1( node->base ),
1171 get<Attribute>().acceptL( node->attributes )
1172 };
1173 } else {
1174 ty = new EnumInstType{
1175 cv( node ),
1176 node->name,
1177 get<Attribute>().acceptL( node->attributes )
1178 };
1179 }
1180 postvisit( node, ty );
1181 this->node = ty;
1182 return nullptr;
1183 }
1184
1185 const ast::Type * visit( const ast::TraitInstType * node ) override final {
1186 TraitInstType * ty;
1187 if ( node->base ) {
1188 ty = new TraitInstType{
1189 cv( node ),
1190 get<TraitDecl>().accept1( node->base ),
1191 get<Attribute>().acceptL( node->attributes )
1192 };
1193 } else {
1194 ty = new TraitInstType{
1195 cv( node ),
1196 node->name,
1197 get<Attribute>().acceptL( node->attributes )
1198 };
1199 }
1200 postvisit( node, ty );
1201 this->node = ty;
1202 return nullptr;
1203 }
1204
1205 const ast::Type * visit( const ast::TypeInstType * node ) override final {
1206 TypeInstType * ty;
1207 if ( node->base ) {
1208 ty = new TypeInstType{
1209 cv( node ),
1210 node->name,
1211 get<TypeDecl>().accept1( node->base ),
1212 get<Attribute>().acceptL( node->attributes )
1213 };
1214 } else {
1215 ty = new TypeInstType{
1216 cv( node ),
1217 node->name,
1218 node->kind == ast::TypeVar::Ftype,
1219 get<Attribute>().acceptL( node->attributes )
1220 };
1221 }
1222 postvisit( node, ty );
1223 this->node = ty;
1224 return nullptr;
1225 }
1226
1227 const ast::Type * visit( const ast::TupleType * node ) override final {
1228 this->node = new TupleType{
1229 cv( node ),
1230 get<Type>().acceptL( node->types )
1231 // members generated by TupleType c'tor
1232 };
1233 return nullptr;
1234 }
1235
1236 const ast::Type * visit( const ast::TypeofType * node ) override final {
1237 this->node = new TypeofType{
1238 cv( node ),
1239 get<Expression>().accept1( node->expr ),
1240 (bool)node->kind
1241 };
1242 return nullptr;
1243 }
1244
1245 const ast::Type * visit( const ast::VarArgsType * node ) override final {
1246 this->node = new VarArgsType{ cv( node ) };
1247 return nullptr;
1248 }
1249
1250 const ast::Type * visit( const ast::ZeroType * node ) override final {
1251 this->node = new ZeroType{ cv( node ) };
1252 return nullptr;
1253 }
1254
1255 const ast::Type * visit( const ast::OneType * node ) override final {
1256 this->node = new OneType{ cv( node ) };
1257 return nullptr;
1258 }
1259
1260 const ast::Type * visit( const ast::GlobalScopeType * ) override final {
1261 this->node = new GlobalScopeType{};
1262 return nullptr;
1263 }
1264
1265 const ast::Designation * visit( const ast::Designation * node ) override final {
1266 auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
1267 designation->location = node->location;
1268 this->node = designation;
1269 return nullptr;
1270 }
1271
1272 const ast::Init * visit( const ast::SingleInit * node ) override final {
1273 auto init = new SingleInit(
1274 get<Expression>().accept1( node->value ),
1275 ast::MaybeConstruct == node->maybeConstructed
1276 );
1277 init->location = node->location;
1278 this->node = init;
1279 return nullptr;
1280 }
1281
1282 const ast::Init * visit( const ast::ListInit * node ) override final {
1283 auto init = new ListInit(
1284 get<Initializer>().acceptL( node->initializers ),
1285 get<Designation>().acceptL( node->designations ),
1286 ast::MaybeConstruct == node->maybeConstructed
1287 );
1288 init->location = node->location;
1289 this->node = init;
1290 return nullptr;
1291 }
1292
1293 const ast::Init * visit( const ast::ConstructorInit * node ) override final {
1294 auto init = new ConstructorInit(
1295 get<Statement>().accept1( node->ctor ),
1296 get<Statement>().accept1( node->dtor ),
1297 get<Initializer>().accept1( node->init )
1298 );
1299 init->location = node->location;
1300 this->node = init;
1301 return nullptr;
1302 }
1303
1304 const ast::Attribute * visit( const ast::Attribute * node ) override final {
1305 (void)node;
1306 return nullptr;
1307 }
1308
1309 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
1310 (void)node;
1311 return nullptr;
1312 }
1313};
1314
1315std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > && translationUnit ) {
1316 ConverterNewToOld c;
1317 std::list< Declaration * > decls;
1318 for(auto d : translationUnit) {
1319 decls.emplace_back( c.decl( d ) );
1320 delete d;
1321 }
1322 return decls;
1323}
1324
1325//================================================================================================
1326
1327class ConverterOldToNew : public Visitor {
1328public:
1329 ast::Decl * decl() {
1330 return strict_dynamic_cast< ast::Decl * >( node );
1331 }
1332private:
1333 /// conversion output
1334 ast::Node * node;
1335 /// cache of nodes that might be referenced by readonly<> for de-duplication
1336 std::unordered_map< BaseSyntaxNode *, ast::Node * > cache;
1337
1338 // Local Utilities:
1339
1340 template<typename NewT, typename OldT>
1341 NewT * getAccept1( OldT old ) {
1342 if ( ! old ) return nullptr;
1343 old->accept(*this);
1344 return strict_dynamic_cast< NewT * >( node );
1345 }
1346
1347# define GET_ACCEPT_1(child, type) \
1348 getAccept1< ast::type, decltype( old->child ) >( old->child )
1349
1350 template<typename NewT, typename OldC>
1351 std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
1352 std::vector< ast::ptr<NewT> > ret;
1353 ret.reserve( old.size() );
1354 for ( auto a : old ) {
1355 a->accept( *this );
1356 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
1357 }
1358 return ret;
1359 }
1360
1361# define GET_ACCEPT_V(child, type) \
1362 getAcceptV< ast::type, decltype( old->child ) >( old->child )
1363
1364 ast::Label make_label(Label* old) {
1365 return ast::Label(
1366 old->labelled->location,
1367 old->name,
1368 GET_ACCEPT_V(attributes, Attribute)
1369 );
1370 }
1371
1372 template<template <class...> class C>
1373 C<ast::Label> make_labels(C<Label> olds) {
1374 C<ast::Label> ret;
1375 for (auto oldn : olds) {
1376 ret.push_back( make_label( &oldn ) );
1377 }
1378 return ret;
1379 }
1380
1381# define GET_LABELS_V(labels) \
1382 to<std::vector>::from( make_labels( std::move( labels ) ) )
1383
1384 static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; }
1385
1386 /// returns true and sets `node` if in cache
1387 bool inCache( BaseSyntaxNode * old ) {
1388 auto it = cache.find( old );
1389 if ( it == cache.end() ) return false;
1390 node = it->second;
1391 return true;
1392 }
1393
1394 // Now all the visit functions:
1395
1396 virtual void visit( ObjectDecl * old ) override final {
1397 if ( inCache( old ) ) return;
1398 auto decl = new ast::ObjectDecl(
1399 old->location,
1400 old->name,
1401 GET_ACCEPT_1(type, Type),
1402 GET_ACCEPT_1(init, Init),
1403 { old->get_storageClasses().val },
1404 { old->linkage.val },
1405 GET_ACCEPT_1(bitfieldWidth, Expr),
1406 GET_ACCEPT_V(attributes, Attribute),
1407 { old->get_funcSpec().val }
1408 );
1409 decl->scopeLevel = old->scopeLevel;
1410 decl->mangleName = old->mangleName;
1411 decl->isDeleted = old->isDeleted;
1412 decl->uniqueId = old->uniqueId;
1413 decl->extension = old->extension;
1414 cache.emplace( old, decl );
1415
1416 this->node = decl;
1417 }
1418
1419 virtual void visit( FunctionDecl * old ) override final {
1420 if ( inCache( old ) ) return;
1421 auto decl = new ast::FunctionDecl{
1422 old->location,
1423 old->name,
1424 GET_ACCEPT_1(type, FunctionType),
1425 GET_ACCEPT_1(statements, CompoundStmt),
1426 { old->storageClasses.val },
1427 { old->linkage.val },
1428 GET_ACCEPT_V(attributes, Attribute),
1429 { old->get_funcSpec().val }
1430 };
1431 decl->scopeLevel = old->scopeLevel;
1432 decl->mangleName = old->mangleName;
1433 decl->isDeleted = old->isDeleted;
1434 decl->uniqueId = old->uniqueId;
1435 decl->extension = old->extension;
1436 cache.emplace( old, decl );
1437
1438 this->node = decl;
1439 }
1440
1441 virtual void visit( StructDecl * old ) override final {
1442 if ( inCache( old ) ) return;
1443 auto decl = new ast::StructDecl(
1444 old->location,
1445 old->name,
1446 old->kind,
1447 GET_ACCEPT_V(attributes, Attribute),
1448 { old->linkage.val }
1449 );
1450 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1451 decl->body = old->body;
1452 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1453 decl->members = GET_ACCEPT_V(members, Decl);
1454 decl->extension = old->extension;
1455 decl->uniqueId = old->uniqueId;
1456 decl->storage = { old->storageClasses.val };
1457 cache.emplace( old, decl );
1458
1459 this->node = decl;
1460 }
1461
1462 virtual void visit( UnionDecl * old ) override final {
1463 if ( inCache( old ) ) return;
1464 auto decl = new ast::UnionDecl(
1465 old->location,
1466 old->name,
1467 GET_ACCEPT_V(attributes, Attribute),
1468 { old->linkage.val }
1469 );
1470 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1471 decl->body = old->body;
1472 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1473 decl->members = GET_ACCEPT_V(members, Decl);
1474 decl->extension = old->extension;
1475 decl->uniqueId = old->uniqueId;
1476 decl->storage = { old->storageClasses.val };
1477 cache.emplace( old, decl );
1478
1479 this->node = decl;
1480 }
1481
1482 virtual void visit( EnumDecl * old ) override final {
1483 if ( inCache( old ) ) return;
1484 auto decl = new ast::UnionDecl(
1485 old->location,
1486 old->name,
1487 GET_ACCEPT_V(attributes, Attribute),
1488 { old->linkage.val }
1489 );
1490 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1491 decl->body = old->body;
1492 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1493 decl->members = GET_ACCEPT_V(members, Decl);
1494 decl->extension = old->extension;
1495 decl->uniqueId = old->uniqueId;
1496 decl->storage = { old->storageClasses.val };
1497 cache.emplace( old, decl );
1498
1499 this->node = decl;
1500 }
1501
1502 virtual void visit( TraitDecl * old ) override final {
1503 if ( inCache( old ) ) return;
1504 auto decl = new ast::UnionDecl(
1505 old->location,
1506 old->name,
1507 GET_ACCEPT_V(attributes, Attribute),
1508 { old->linkage.val }
1509 );
1510 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
1511 decl->body = old->body;
1512 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1513 decl->members = GET_ACCEPT_V(members, Decl);
1514 decl->extension = old->extension;
1515 decl->uniqueId = old->uniqueId;
1516 decl->storage = { old->storageClasses.val };
1517 cache.emplace( old, decl );
1518
1519 this->node = decl;
1520 }
1521
1522 virtual void visit( TypeDecl * old ) override final {
1523 if ( inCache( old ) ) return;
1524 auto decl = new ast::TypeDecl{
1525 old->location,
1526 old->name,
1527 { old->storageClasses.val },
1528 GET_ACCEPT_1(base, Type),
1529 (ast::TypeVar::Kind)(unsigned)old->kind,
1530 old->sized,
1531 GET_ACCEPT_1(init, Type)
1532 };
1533 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1534 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1535 decl->extension = old->extension;
1536 decl->uniqueId = old->uniqueId;
1537 cache.emplace( old, decl );
1538
1539 this->node = decl;
1540 }
1541
1542 virtual void visit( TypedefDecl * old ) override final {
1543 auto decl = new ast::TypedefDecl(
1544 old->location,
1545 old->name,
1546 { old->storageClasses.val },
1547 GET_ACCEPT_1(base, Type),
1548 { old->linkage.val }
1549 );
1550 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
1551 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
1552 decl->extension = old->extension;
1553 decl->uniqueId = old->uniqueId;
1554 decl->storage = { old->storageClasses.val };
1555
1556 this->node = decl;
1557 }
1558
1559 virtual void visit( AsmDecl * old ) override final {
1560 auto decl = new ast::AsmDecl{
1561 old->location,
1562 GET_ACCEPT_1(stmt, AsmStmt)
1563 };
1564 decl->extension = old->extension;
1565 decl->uniqueId = old->uniqueId;
1566 decl->storage = { old->storageClasses.val };
1567
1568 this->node = decl;
1569 }
1570
1571 virtual void visit( StaticAssertDecl * old ) override final {
1572 auto decl = new ast::StaticAssertDecl{
1573 old->location,
1574 GET_ACCEPT_1(condition, Expr),
1575 GET_ACCEPT_1(message, ConstantExpr)
1576 };
1577 decl->extension = old->extension;
1578 decl->uniqueId = old->uniqueId;
1579 decl->storage = { old->storageClasses.val };
1580
1581 this->node = decl;
1582 }
1583
1584 virtual void visit( CompoundStmt * old ) override final {
1585 auto stmt = new ast::CompoundStmt(
1586 old->location,
1587 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
1588 GET_LABELS_V(old->labels)
1589 );
1590
1591 this->node = stmt;
1592 }
1593
1594 virtual void visit( ExprStmt * old ) override final {
1595 this->node = new ast::ExprStmt(
1596 old->location,
1597 GET_ACCEPT_1(expr, Expr),
1598 GET_LABELS_V(old->labels)
1599 );
1600 }
1601
1602 virtual void visit( AsmStmt * old ) override final {
1603 this->node = new ast::AsmStmt(
1604 old->location,
1605 old->voltile,
1606 GET_ACCEPT_1(instruction, Expr),
1607 GET_ACCEPT_V(output, Expr),
1608 GET_ACCEPT_V(input, Expr),
1609 GET_ACCEPT_V(clobber, ConstantExpr),
1610 GET_LABELS_V(old->gotolabels),
1611 GET_LABELS_V(old->labels)
1612 );
1613 }
1614
1615 virtual void visit( DirectiveStmt * old ) override final {
1616 this->node = new ast::DirectiveStmt(
1617 old->location,
1618 old->directive,
1619 GET_LABELS_V(old->labels)
1620 );
1621 }
1622
1623 virtual void visit( IfStmt * old ) override final {
1624 this->node = new ast::IfStmt(
1625 old->location,
1626 GET_ACCEPT_1(condition, Expr),
1627 GET_ACCEPT_1(thenPart, Stmt),
1628 GET_ACCEPT_1(elsePart, Stmt),
1629 GET_ACCEPT_V(initialization, Stmt),
1630 GET_LABELS_V(old->labels)
1631 );
1632 }
1633
1634 virtual void visit( SwitchStmt * old ) override final {
1635 this->node = new ast::SwitchStmt(
1636 old->location,
1637 GET_ACCEPT_1(condition, Expr),
1638 GET_ACCEPT_V(statements, Stmt),
1639 GET_LABELS_V(old->labels)
1640 );
1641 }
1642
1643 virtual void visit( CaseStmt * old ) override final {
1644 this->node = new ast::CaseStmt(
1645 old->location,
1646 GET_ACCEPT_1(condition, Expr),
1647 GET_ACCEPT_V(stmts, Stmt),
1648 GET_LABELS_V(old->labels)
1649 );
1650 }
1651
1652 virtual void visit( WhileStmt * old ) override final {
1653 this->node = new ast::WhileStmt(
1654 old->location,
1655 GET_ACCEPT_1(condition, Expr),
1656 GET_ACCEPT_1(body, Stmt),
1657 GET_ACCEPT_V(initialization, Stmt),
1658 old->isDoWhile,
1659 GET_LABELS_V(old->labels)
1660 );
1661 }
1662
1663 virtual void visit( ForStmt * old ) override final {
1664 this->node = new ast::ForStmt(
1665 old->location,
1666 GET_ACCEPT_V(initialization, Stmt),
1667 GET_ACCEPT_1(condition, Expr),
1668 GET_ACCEPT_1(increment, Expr),
1669 GET_ACCEPT_1(body, Stmt),
1670 GET_LABELS_V(old->labels)
1671 );
1672 }
1673
1674 virtual void visit( BranchStmt * old ) override final {
1675 if (old->computedTarget) {
1676 this->node = new ast::BranchStmt(
1677 old->location,
1678 GET_ACCEPT_1(computedTarget, Expr),
1679 GET_LABELS_V(old->labels)
1680 );
1681 } else {
1682 ast::BranchStmt::Kind kind;
1683 switch (old->type) {
1684 #define CASE(n) \
1685 case BranchStmt::n: \
1686 kind = ast::BranchStmt::n; \
1687 break
1688 CASE(Goto);
1689 CASE(Break);
1690 CASE(Continue);
1691 CASE(FallThrough);
1692 CASE(FallThroughDefault);
1693 #undef CASE
1694 default:
1695 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1696 }
1697
1698 Label label = old->originalTarget;
1699 auto stmt = new ast::BranchStmt(
1700 old->location,
1701 kind,
1702 make_label(&label),
1703 GET_LABELS_V(old->labels)
1704 );
1705 stmt->target = make_label(&old->target);
1706 this->node = stmt;
1707 }
1708 }
1709
1710 virtual void visit( ReturnStmt * old ) override final {
1711 this->node = new ast::ReturnStmt(
1712 old->location,
1713 GET_ACCEPT_1(expr, Expr),
1714 GET_LABELS_V(old->labels)
1715 );
1716 }
1717
1718 virtual void visit( ThrowStmt * old ) override final {
1719 ast::ThrowStmt::Kind kind;
1720 switch (old->kind) {
1721 case ThrowStmt::Terminate:
1722 kind = ast::ThrowStmt::Terminate;
1723 break;
1724 case ThrowStmt::Resume:
1725 kind = ast::ThrowStmt::Resume;
1726 break;
1727 default:
1728 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1729 }
1730
1731 this->node = new ast::ThrowStmt(
1732 old->location,
1733 kind,
1734 GET_ACCEPT_1(expr, Expr),
1735 GET_ACCEPT_1(target, Expr),
1736 GET_LABELS_V(old->labels)
1737 );
1738 }
1739
1740 virtual void visit( TryStmt * old ) override final {
1741 this->node = new ast::TryStmt(
1742 old->location,
1743 GET_ACCEPT_1(block, CompoundStmt),
1744 GET_ACCEPT_V(handlers, CatchStmt),
1745 GET_ACCEPT_1(finallyBlock, FinallyStmt),
1746 GET_LABELS_V(old->labels)
1747 );
1748 }
1749
1750 virtual void visit( CatchStmt * old ) override final {
1751 ast::CatchStmt::Kind kind;
1752 switch (old->kind) {
1753 case CatchStmt::Terminate:
1754 kind = ast::CatchStmt::Terminate;
1755 break;
1756 case CatchStmt::Resume:
1757 kind = ast::CatchStmt::Resume;
1758 break;
1759 default:
1760 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
1761 }
1762
1763 this->node = new ast::CatchStmt(
1764 old->location,
1765 kind,
1766 GET_ACCEPT_1(decl, Decl),
1767 GET_ACCEPT_1(cond, Expr),
1768 GET_ACCEPT_1(body, Stmt),
1769 GET_LABELS_V(old->labels)
1770 );
1771 }
1772
1773 virtual void visit( FinallyStmt * old ) override final {
1774 this->node = new ast::FinallyStmt(
1775 old->location,
1776 GET_ACCEPT_1(block, CompoundStmt),
1777 GET_LABELS_V(old->labels)
1778 );
1779 }
1780
1781 virtual void visit( WaitForStmt * old ) override final {
1782 ast::WaitForStmt * stmt = new ast::WaitForStmt(
1783 old->location,
1784 GET_LABELS_V(old->labels)
1785 );
1786
1787 stmt->clauses.reserve( old->clauses.size() );
1788 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
1789 stmt->clauses.push_back({
1790 ast::WaitForStmt::Target{
1791 GET_ACCEPT_1(clauses[i].target.function, Expr),
1792 GET_ACCEPT_V(clauses[i].target.arguments, Expr)
1793 },
1794 GET_ACCEPT_1(clauses[i].statement, Stmt),
1795 GET_ACCEPT_1(clauses[i].condition, Expr)
1796 });
1797 }
1798 stmt->timeout = {
1799 GET_ACCEPT_1(timeout.time, Expr),
1800 GET_ACCEPT_1(timeout.statement, Stmt),
1801 GET_ACCEPT_1(timeout.condition, Expr),
1802 };
1803 stmt->orElse = {
1804 GET_ACCEPT_1(timeout.statement, Stmt),
1805 GET_ACCEPT_1(timeout.condition, Expr),
1806 };
1807
1808 this->node = stmt;
1809 }
1810
1811 virtual void visit( WithStmt * old ) override final {
1812 this->node = new ast::WithStmt(
1813 old->location,
1814 GET_ACCEPT_V(exprs, Expr),
1815 GET_ACCEPT_1(stmt, Stmt),
1816 GET_LABELS_V(old->labels)
1817 );
1818 }
1819
1820 virtual void visit( NullStmt * old ) override final {
1821 this->node = new ast::NullStmt(
1822 old->location,
1823 GET_LABELS_V(old->labels)
1824 );
1825 }
1826
1827 virtual void visit( DeclStmt * old ) override final {
1828 this->node = new ast::DeclStmt(
1829 old->location,
1830 GET_ACCEPT_1(decl, Decl),
1831 GET_LABELS_V(old->labels)
1832 );
1833 }
1834
1835 virtual void visit( ImplicitCtorDtorStmt * old ) override final {
1836 this->node = new ast::ImplicitCtorDtorStmt(
1837 old->location,
1838 GET_ACCEPT_1(callStmt, Stmt),
1839 GET_LABELS_V(old->labels)
1840 );
1841 }
1842
1843 ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
1844
1845 ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
1846
1847 for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
1848 rslt->add( old_i->first,
1849 getAccept1<ast::Type>(old_i->second) );
1850 }
1851
1852 for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
1853 rslt->addVar( old_i->first,
1854 getAccept1<ast::Expr>(old_i->second) );
1855 }
1856
1857 return rslt;
1858 }
1859
1860 void convertInferUnion(ast::Expr::InferUnion &newInferred,
1861 const std::map<UniqueId,ParamEntry> &oldInferParams,
1862 const std::vector<UniqueId> &oldResnSlots) {
1863
1864 assert( oldInferParams.empty() || oldResnSlots.empty() );
1865 assert( newInferred.mode == ast::Expr::InferUnion::Empty );
1866
1867 if ( !oldInferParams.empty() ) {
1868 ast::InferredParams &tgt = newInferred.inferParams();
1869 for (auto old : oldInferParams) {
1870 tgt[old.first] = ast::ParamEntry(
1871 old.second.decl,
1872 getAccept1<ast::Type>(old.second.actualType),
1873 getAccept1<ast::Type>(old.second.formalType),
1874 getAccept1<ast::Expr>(old.second.expr)
1875 );
1876 }
1877 } else if ( !oldResnSlots.empty() ) {
1878 ast::ResnSlots &tgt = newInferred.resnSlots();
1879 for (auto old : oldResnSlots) {
1880 tgt.push_back(old);
1881 }
1882 }
1883 }
1884
1885 ast::Expr * visitBaseExpr_SkipResultType(Expression * old, ast::Expr * nw) {
1886
1887 nw->env = convertTypeSubstitution(old->env);
1888
1889 nw->extension = old->extension;
1890 convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
1891
1892 return nw;
1893 }
1894
1895 ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
1896
1897 nw->result = GET_ACCEPT_1(result, Type);
1898 return visitBaseExpr_SkipResultType(old, nw);;
1899 }
1900
1901 virtual void visit( ApplicationExpr * old ) override final {
1902 this->node = visitBaseExpr( old,
1903 new ast::ApplicationExpr(
1904 old->location,
1905 GET_ACCEPT_1(function, Expr),
1906 GET_ACCEPT_V(args, Expr)
1907 )
1908 );
1909 }
1910
1911 virtual void visit( UntypedExpr * old ) override final {
1912 this->node = visitBaseExpr( old,
1913 new ast::UntypedExpr(
1914 old->location,
1915 GET_ACCEPT_1(function, Expr),
1916 GET_ACCEPT_V(args, Expr)
1917 )
1918 );
1919 }
1920
1921 virtual void visit( NameExpr * old ) override final {
1922 this->node = visitBaseExpr( old,
1923 new ast::NameExpr(
1924 old->location,
1925 old->get_name()
1926 )
1927 );
1928 }
1929
1930 virtual void visit( CastExpr * old ) override final {
1931 this->node = visitBaseExpr( old,
1932 new ast::CastExpr(
1933 old->location,
1934 GET_ACCEPT_1(arg, Expr),
1935 old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
1936 )
1937 );
1938 }
1939
1940 virtual void visit( KeywordCastExpr * old) override final {
1941 ast::KeywordCastExpr::Target castTarget = ast::KeywordCastExpr::NUMBER_OF_TARGETS;
1942 switch (old->target) {
1943 case KeywordCastExpr::Coroutine:
1944 castTarget = ast::KeywordCastExpr::Coroutine;
1945 break;
1946 case KeywordCastExpr::Thread:
1947 castTarget = ast::KeywordCastExpr::Thread;
1948 break;
1949 case KeywordCastExpr::Monitor:
1950 castTarget = ast::KeywordCastExpr::Monitor;
1951 break;
1952 default:
1953 break;
1954 }
1955 assert ( castTarget < ast::KeywordCastExpr::NUMBER_OF_TARGETS );
1956 this->node = visitBaseExpr( old,
1957 new ast::KeywordCastExpr(
1958 old->location,
1959 GET_ACCEPT_1(arg, Expr),
1960 castTarget
1961 )
1962 );
1963 }
1964
1965 virtual void visit( VirtualCastExpr * old ) override final {
1966 this->node = visitBaseExpr_SkipResultType( old,
1967 new ast::VirtualCastExpr(
1968 old->location,
1969 GET_ACCEPT_1(arg, Expr),
1970 GET_ACCEPT_1(result, Type)
1971 )
1972 );
1973 }
1974
1975 virtual void visit( AddressExpr * old ) override final {
1976 this->node = visitBaseExpr( old,
1977 new ast::AddressExpr(
1978 old->location,
1979 GET_ACCEPT_1(arg, Expr)
1980 )
1981 );
1982 }
1983
1984 virtual void visit( LabelAddressExpr * old ) override final {
1985 this->node = visitBaseExpr( old,
1986 new ast::LabelAddressExpr(
1987 old->location,
1988 make_label(&old->arg)
1989 )
1990 );
1991 }
1992
1993 virtual void visit( UntypedMemberExpr * old ) override final {
1994 this->node = visitBaseExpr( old,
1995 new ast::UntypedMemberExpr(
1996 old->location,
1997 GET_ACCEPT_1(member, Expr),
1998 GET_ACCEPT_1(aggregate, Expr)
1999 )
2000 );
2001 }
2002
2003 virtual void visit( MemberExpr * old ) override final {
2004 this->node = visitBaseExpr( old,
2005 new ast::MemberExpr(
2006 old->location,
2007 inCache(old->member) ?
2008 dynamic_cast<ast::DeclWithType *>(this->node) :
2009 GET_ACCEPT_1(member, DeclWithType),
2010 GET_ACCEPT_1(aggregate, Expr)
2011 )
2012 );
2013 }
2014
2015 virtual void visit( VariableExpr * old ) override final {
2016 this->node = visitBaseExpr( old,
2017 new ast::VariableExpr(
2018 old->location,
2019 inCache(old->var) ?
2020 dynamic_cast<ast::DeclWithType *>(this->node) :
2021 GET_ACCEPT_1(var, DeclWithType)
2022 )
2023 );
2024 }
2025
2026 bool isIntlikeConstantType(const Type *t) {
2027 if ( const BasicType * basicType = dynamic_cast< const BasicType * >( t ) ) {
2028 if ( basicType->isInteger() ) {
2029 return true;
2030 }
2031 } else if ( dynamic_cast< const OneType * >( t ) ) {
2032 return true;
2033 } else if ( dynamic_cast< const ZeroType * >( t ) ) {
2034 return true;
2035 } else if ( dynamic_cast< const PointerType * >( t ) ) {
2036 // null pointer constants, with zero int-values
2037 return true;
2038 }
2039 return false;
2040 }
2041
2042 int isFloatlikeConstantType(const Type *t) {
2043 if ( const BasicType * bty = dynamic_cast< const BasicType * >( t ) ) {
2044 if ( ! bty->isInteger() ) {
2045 return true;
2046 }
2047 }
2048 return false;
2049 }
2050
2051 int isStringlikeConstantType(const Type *t) {
2052 if ( const ArrayType * aty = dynamic_cast< const ArrayType * >( t ) ) {
2053 if ( const BasicType * bty = dynamic_cast< const BasicType * >( aty->base ) ) {
2054 if ( bty->kind == BasicType::Kind::Char ) {
2055 return true;
2056 }
2057 }
2058 }
2059 return false;
2060 }
2061
2062 virtual void visit( ConstantExpr * old ) override final {
2063 ast::ConstantExpr *rslt = nullptr;
2064 if (isIntlikeConstantType(old->result)) {
2065 rslt = new ast::ConstantExpr(
2066 old->location,
2067 GET_ACCEPT_1(result, Type),
2068 old->constant.get_value(),
2069 (unsigned long long) old->intValue()
2070 );
2071 } else if (isFloatlikeConstantType(old->result)) {
2072 rslt = new ast::ConstantExpr(
2073 old->location,
2074 GET_ACCEPT_1(result, Type),
2075 old->constant.get_value(),
2076 (double) old->constant.get_dval()
2077 );
2078 } else if (isStringlikeConstantType(old->result)) {
2079 rslt = ast::ConstantExpr::from_string(
2080 old->location,
2081 old->constant.get_value()
2082 );
2083 }
2084 assert(rslt);
2085 this->node = visitBaseExpr( old, rslt );
2086 }
2087
2088 virtual void visit( SizeofExpr * old ) override final {
2089 assert (old->expr || old->type);
2090 assert (! (old->expr && old->type));
2091 ast::SizeofExpr *rslt;
2092 if (old->expr) {
2093 assert(!old->isType);
2094 rslt = new ast::SizeofExpr(
2095 old->location,
2096 GET_ACCEPT_1(expr, Expr)
2097 );
2098 }
2099 if (old->type) {
2100 assert(old->isType);
2101 rslt = new ast::SizeofExpr(
2102 old->location,
2103 GET_ACCEPT_1(type, Type)
2104 );
2105 }
2106 this->node = visitBaseExpr( old, rslt );
2107 }
2108
2109 virtual void visit( AlignofExpr * old ) override final {
2110 assert (old->expr || old->type);
2111 assert (! (old->expr && old->type));
2112 ast::AlignofExpr *rslt;
2113 if (old->expr) {
2114 assert(!old->isType);
2115 rslt = new ast::AlignofExpr(
2116 old->location,
2117 GET_ACCEPT_1(expr, Expr)
2118 );
2119 }
2120 if (old->type) {
2121 assert(old->isType);
2122 rslt = new ast::AlignofExpr(
2123 old->location,
2124 GET_ACCEPT_1(type, Type)
2125 );
2126 }
2127 this->node = visitBaseExpr( old, rslt );
2128 }
2129
2130 virtual void visit( UntypedOffsetofExpr * old ) override final {
2131 this->node = visitBaseExpr( old,
2132 new ast::UntypedOffsetofExpr(
2133 old->location,
2134 GET_ACCEPT_1(type, Type),
2135 old->member
2136 )
2137 );
2138 }
2139
2140 virtual void visit( OffsetofExpr * old ) override final {
2141 this->node = visitBaseExpr( old,
2142 new ast::OffsetofExpr(
2143 old->location,
2144 GET_ACCEPT_1(type, Type),
2145 inCache(old->member) ?
2146 dynamic_cast<ast::DeclWithType *>(this->node) :
2147 GET_ACCEPT_1(member, DeclWithType)
2148 )
2149 );
2150 }
2151
2152 virtual void visit( OffsetPackExpr * old ) override final {
2153 this->node = visitBaseExpr( old,
2154 new ast::OffsetPackExpr(
2155 old->location,
2156 GET_ACCEPT_1(type, StructInstType)
2157 )
2158 );
2159 }
2160
2161 virtual void visit( LogicalExpr * old ) override final {
2162 this->node = visitBaseExpr( old,
2163 new ast::LogicalExpr(
2164 old->location,
2165 GET_ACCEPT_1(arg1, Expr),
2166 GET_ACCEPT_1(arg2, Expr),
2167 old->get_isAnd() ?
2168 ast::LogicalFlag::AndExpr :
2169 ast::LogicalFlag::OrExpr
2170 )
2171 );
2172 }
2173
2174 virtual void visit( ConditionalExpr * old ) override final {
2175 this->node = visitBaseExpr( old,
2176 new ast::ConditionalExpr(
2177 old->location,
2178 GET_ACCEPT_1(arg1, Expr),
2179 GET_ACCEPT_1(arg2, Expr),
2180 GET_ACCEPT_1(arg3, Expr)
2181 )
2182 );
2183 }
2184
2185 virtual void visit( CommaExpr * old ) override final {
2186 this->node = visitBaseExpr( old,
2187 new ast::CommaExpr(
2188 old->location,
2189 GET_ACCEPT_1(arg1, Expr),
2190 GET_ACCEPT_1(arg2, Expr)
2191 )
2192 );
2193 }
2194
2195 virtual void visit( TypeExpr * old ) override final {
2196 this->node = visitBaseExpr( old,
2197 new ast::TypeExpr(
2198 old->location,
2199 GET_ACCEPT_1(type, Type)
2200 )
2201 );
2202 }
2203
2204 virtual void visit( AsmExpr * old ) override final {
2205 this->node = visitBaseExpr( old,
2206 new ast::AsmExpr(
2207 old->location,
2208 GET_ACCEPT_1(inout, Expr),
2209 GET_ACCEPT_1(constraint, Expr),
2210 GET_ACCEPT_1(operand, Expr)
2211 )
2212 );
2213 }
2214
2215 virtual void visit( ImplicitCopyCtorExpr * old ) override final {
2216 auto rslt = new ast::ImplicitCopyCtorExpr(
2217 old->location,
2218 GET_ACCEPT_1(callExpr, ApplicationExpr)
2219 );
2220
2221 rslt->tempDecls = GET_ACCEPT_V(tempDecls, ObjectDecl);
2222 rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2223 rslt->dtors = GET_ACCEPT_V(dtors, Expr);
2224
2225 this->node = visitBaseExpr( old, rslt );
2226 }
2227
2228 virtual void visit( ConstructorExpr * old ) override final {
2229 this->node = visitBaseExpr( old,
2230 new ast::ConstructorExpr(
2231 old->location,
2232 GET_ACCEPT_1(callExpr, Expr)
2233 )
2234 );
2235 }
2236
2237 virtual void visit( CompoundLiteralExpr * old ) override final {
2238 this->node = visitBaseExpr_SkipResultType( old,
2239 new ast::CompoundLiteralExpr(
2240 old->location,
2241 GET_ACCEPT_1(result, Type),
2242 GET_ACCEPT_1(initializer, Init)
2243 )
2244 );
2245 }
2246
2247 virtual void visit( RangeExpr * old ) override final {
2248 this->node = visitBaseExpr( old,
2249 new ast::RangeExpr(
2250 old->location,
2251 GET_ACCEPT_1(low, Expr),
2252 GET_ACCEPT_1(high, Expr)
2253 )
2254 );
2255 }
2256
2257 virtual void visit( UntypedTupleExpr * old ) override final {
2258 this->node = visitBaseExpr( old,
2259 new ast::UntypedTupleExpr(
2260 old->location,
2261 GET_ACCEPT_V(exprs, Expr)
2262 )
2263 );
2264 }
2265
2266 virtual void visit( TupleExpr * old ) override final {
2267 this->node = visitBaseExpr( old,
2268 new ast::TupleExpr(
2269 old->location,
2270 GET_ACCEPT_V(exprs, Expr)
2271 )
2272 );
2273 }
2274
2275 virtual void visit( TupleIndexExpr * old ) override final {
2276 this->node = visitBaseExpr( old,
2277 new ast::TupleIndexExpr(
2278 old->location,
2279 GET_ACCEPT_1(tuple, Expr),
2280 old->index
2281 )
2282 );
2283 }
2284
2285 virtual void visit( TupleAssignExpr * old ) override final {
2286 this->node = visitBaseExpr_SkipResultType( old,
2287 new ast::TupleAssignExpr(
2288 old->location,
2289 GET_ACCEPT_1(result, Type),
2290 GET_ACCEPT_1(stmtExpr, StmtExpr)
2291 )
2292 );
2293 }
2294
2295 virtual void visit( StmtExpr * old ) override final {
2296 auto rslt = new ast::StmtExpr(
2297 old->location,
2298 GET_ACCEPT_1(statements, CompoundStmt)
2299 );
2300 rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
2301 rslt->dtors = GET_ACCEPT_V(dtors , Expr);
2302
2303 this->node = visitBaseExpr_SkipResultType( old, rslt );
2304 }
2305
2306 virtual void visit( UniqueExpr * old ) override final {
2307 auto rslt = new ast::UniqueExpr(
2308 old->location,
2309 GET_ACCEPT_1(expr, Expr)
2310 );
2311 rslt->object = GET_ACCEPT_1(object, ObjectDecl);
2312 rslt->var = GET_ACCEPT_1(var , VariableExpr);
2313
2314 this->node = visitBaseExpr( old, rslt );
2315 }
2316
2317 virtual void visit( UntypedInitExpr * old ) override final {
2318 std::vector<ast::InitAlternative> initAlts;
2319 for (auto ia : old->initAlts) {
2320 initAlts.push_back(ast::InitAlternative(
2321 getAccept1< ast::Type, Type * >( ia.type ),
2322 getAccept1< ast::Designation, Designation * >( ia.designation )
2323 ));
2324 }
2325 this->node = visitBaseExpr( old,
2326 new ast::UntypedInitExpr(
2327 old->location,
2328 GET_ACCEPT_1(expr, Expr),
2329 std::move(initAlts)
2330 )
2331 );
2332 }
2333
2334 virtual void visit( InitExpr * old ) override final {
2335 this->node = visitBaseExpr( old,
2336 new ast::InitExpr(
2337 old->location,
2338 GET_ACCEPT_1(expr, Expr),
2339 GET_ACCEPT_1(designation, Designation)
2340 )
2341 );
2342 }
2343
2344 virtual void visit( DeletedExpr * old ) override final {
2345 this->node = visitBaseExpr( old,
2346 new ast::DeletedExpr(
2347 old->location,
2348 GET_ACCEPT_1(expr, Expr),
2349 inCache(old->deleteStmt) ?
2350 this->node :
2351 GET_ACCEPT_1(deleteStmt, Node)
2352 )
2353 );
2354 }
2355
2356 virtual void visit( DefaultArgExpr * old ) override final {
2357 this->node = visitBaseExpr( old,
2358 new ast::DefaultArgExpr(
2359 old->location,
2360 GET_ACCEPT_1(expr, Expr)
2361 )
2362 );
2363 }
2364
2365 virtual void visit( GenericExpr * old ) override final {
2366 std::vector<ast::GenericExpr::Association> associations;
2367 for (auto association : old->associations) {
2368 associations.push_back(ast::GenericExpr::Association(
2369 getAccept1< ast::Type, Type * >( association.type ),
2370 getAccept1< ast::Expr, Expression * >( association.expr )
2371 ));
2372 }
2373 this->node = visitBaseExpr( old,
2374 new ast::GenericExpr(
2375 old->location,
2376 GET_ACCEPT_1(control, Expr),
2377 std::move(associations)
2378 )
2379 );
2380 }
2381
2382 virtual void visit( VoidType * old ) override final {
2383 this->node = new ast::VoidType{ cv( old ) };
2384 }
2385
2386 virtual void visit( BasicType * old ) override final {
2387 this->node = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
2388 }
2389
2390 virtual void visit( PointerType * old ) override final {
2391 this->node = new ast::PointerType{
2392 GET_ACCEPT_1( base, Type ),
2393 GET_ACCEPT_1( dimension, Expr ),
2394 (ast::LengthFlag)old->isVarLen,
2395 (ast::DimensionFlag)old->isStatic,
2396 cv( old )
2397 };
2398 }
2399
2400 virtual void visit( ArrayType * old ) override final {
2401 this->node = new ast::ArrayType{
2402 GET_ACCEPT_1( base, Type ),
2403 GET_ACCEPT_1( dimension, Expr ),
2404 (ast::LengthFlag)old->isVarLen,
2405 (ast::DimensionFlag)old->isStatic,
2406 cv( old )
2407 };
2408 }
2409
2410 virtual void visit( ReferenceType * old ) override final {
2411 this->node = new ast::ReferenceType{
2412 GET_ACCEPT_1( base, Type ),
2413 cv( old )
2414 };
2415 }
2416
2417 virtual void visit( QualifiedType * old ) override final {
2418 this->node = new ast::QualifiedType{
2419 GET_ACCEPT_1( parent, Type ),
2420 GET_ACCEPT_1( child, Type ),
2421 cv( old )
2422 };
2423 }
2424
2425 virtual void visit( FunctionType * old ) override final {
2426 auto ty = new ast::FunctionType {
2427 (ast::ArgumentFlag)old->isVarArgs,
2428 cv( old )
2429 };
2430 ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
2431 ty->params = GET_ACCEPT_V( parameters, DeclWithType );
2432 ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2433 this->node = ty;
2434 }
2435
2436 void postvisit( ReferenceToType * old, ast::ReferenceToType * ty ) {
2437 ty->forall = GET_ACCEPT_V( forall, TypeDecl );
2438 ty->params = GET_ACCEPT_V( parameters, Expr );
2439 ty->hoistType = old->hoistType;
2440 }
2441
2442 virtual void visit( StructInstType * old ) override final {
2443 ast::StructInstType * ty;
2444 if ( old->baseStruct ) {
2445 ty = new ast::StructInstType{
2446 GET_ACCEPT_1( baseStruct, StructDecl ),
2447 cv( old ),
2448 GET_ACCEPT_V( attributes, Attribute )
2449 };
2450 } else {
2451 ty = new ast::StructInstType{
2452 old->name,
2453 cv( old ),
2454 GET_ACCEPT_V( attributes, Attribute )
2455 };
2456 }
2457 postvisit( old, ty );
2458 this->node = ty;
2459 }
2460
2461 virtual void visit( UnionInstType * old ) override final {
2462 ast::UnionInstType * ty;
2463 if ( old->baseUnion ) {
2464 ty = new ast::UnionInstType{
2465 GET_ACCEPT_1( baseUnion, UnionDecl ),
2466 cv( old ),
2467 GET_ACCEPT_V( attributes, Attribute )
2468 };
2469 } else {
2470 ty = new ast::UnionInstType{
2471 old->name,
2472 cv( old ),
2473 GET_ACCEPT_V( attributes, Attribute )
2474 };
2475 }
2476 postvisit( old, ty );
2477 this->node = ty;
2478 }
2479
2480 virtual void visit( EnumInstType * old ) override final {
2481 ast::EnumInstType * ty;
2482 if ( old->baseEnum ) {
2483 ty = new ast::EnumInstType{
2484 GET_ACCEPT_1( baseEnum, EnumDecl ),
2485 cv( old ),
2486 GET_ACCEPT_V( attributes, Attribute )
2487 };
2488 } else {
2489 ty = new ast::EnumInstType{
2490 old->name,
2491 cv( old ),
2492 GET_ACCEPT_V( attributes, Attribute )
2493 };
2494 }
2495 postvisit( old, ty );
2496 this->node = ty;
2497 }
2498
2499 virtual void visit( TraitInstType * old ) override final {
2500 ast::TraitInstType * ty;
2501 if ( old->baseTrait ) {
2502 ty = new ast::TraitInstType{
2503 GET_ACCEPT_1( baseTrait, TraitDecl ),
2504 cv( old ),
2505 GET_ACCEPT_V( attributes, Attribute )
2506 };
2507 } else {
2508 ty = new ast::TraitInstType{
2509 old->name,
2510 cv( old ),
2511 GET_ACCEPT_V( attributes, Attribute )
2512 };
2513 }
2514 postvisit( old, ty );
2515 this->node = ty;
2516 }
2517
2518 virtual void visit( TypeInstType * old ) override final {
2519 ast::TypeInstType * ty;
2520 if ( old->baseType ) {
2521 ty = new ast::TypeInstType{
2522 old->name,
2523 GET_ACCEPT_1( baseType, TypeDecl ),
2524 cv( old ),
2525 GET_ACCEPT_V( attributes, Attribute )
2526 };
2527 } else {
2528 ty = new ast::TypeInstType{
2529 old->name,
2530 old->isFtype ? ast::TypeVar::Ftype : ast::TypeVar::Dtype,
2531 cv( old ),
2532 GET_ACCEPT_V( attributes, Attribute )
2533 };
2534 }
2535 postvisit( old, ty );
2536 this->node = ty;
2537 }
2538
2539 virtual void visit( TupleType * old ) override final {
2540 this->node = new ast::TupleType{
2541 GET_ACCEPT_V( types, Type ),
2542 // members generated by TupleType c'tor
2543 cv( old )
2544 };
2545 }
2546
2547 virtual void visit( TypeofType * old ) override final {
2548 this->node = new ast::TypeofType{
2549 GET_ACCEPT_1( expr, Expr ),
2550 (ast::TypeofType::Kind)old->is_basetypeof,
2551 cv( old )
2552 };
2553 }
2554
2555 virtual void visit( AttrType * ) override final {
2556 assertf( false, "AttrType deprecated in new AST." );
2557 }
2558
2559 virtual void visit( VarArgsType * old ) override final {
2560 this->node = new ast::VarArgsType{ cv( old ) };
2561 }
2562
2563 virtual void visit( ZeroType * old ) override final {
2564 this->node = new ast::ZeroType{ cv( old ) };
2565 }
2566
2567 virtual void visit( OneType * old ) override final {
2568 this->node = new ast::OneType{ cv( old ) };
2569 }
2570
2571 virtual void visit( GlobalScopeType * ) override final {
2572 this->node = new ast::GlobalScopeType{};
2573 }
2574
2575 virtual void visit( Designation * old ) override final {
2576 this->node = new ast::Designation(
2577 old->location,
2578 GET_ACCEPT_V(designators, Expr)
2579 );
2580 }
2581
2582 virtual void visit( SingleInit * old ) override final {
2583 this->node = new ast::SingleInit(
2584 old->location,
2585 GET_ACCEPT_1(value, Expr),
2586 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
2587 );
2588 }
2589
2590 virtual void visit( ListInit * old ) override final {
2591 this->node = new ast::ListInit(
2592 old->location,
2593 GET_ACCEPT_V(initializers, Init),
2594 GET_ACCEPT_V(designations, Designation),
2595 (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
2596 );
2597 }
2598
2599 virtual void visit( ConstructorInit * old ) override final {
2600 this->node = new ast::ConstructorInit(
2601 old->location,
2602 GET_ACCEPT_1(ctor, Stmt),
2603 GET_ACCEPT_1(dtor, Stmt),
2604 GET_ACCEPT_1(init, Init)
2605 );
2606 }
2607
2608 virtual void visit( Constant * ) override final {
2609
2610 }
2611
2612 virtual void visit( Attribute * ) override final {
2613
2614 }
2615
2616 virtual void visit( AttrExpr * ) override final {
2617
2618 assert( 0 );
2619 }
2620};
2621
2622#undef GET_LABELS_V
2623#undef GET_ACCEPT_V
2624#undef GET_ACCEPT_1
2625
2626std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
2627 ConverterOldToNew c;
2628 std::list< ast::ptr< ast::Decl > > decls;
2629 for(auto d : translationUnit) {
2630 d->accept( c );
2631 decls.emplace_back( c.decl() );
2632 delete d;
2633 }
2634 return decls;
2635}
Note: See TracBrowser for help on using the repository browser.