source: src/AST/Convert.cpp@ 30fe96d

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

Another fix to break cycles in the converter.

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