source: src/AST/Convert.cpp@ 2a54479

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 2a54479 was 2a54479, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Convert persevers Validate::SizeType. Removed the inCache checks I'm sure are duplicates.

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