source: src/AST/Convert.cpp@ 3da7c19

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 3da7c19 was 558d13b, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

fix conversion bug in constants; string constants were being mistaken for int after resolution when their type changes from char[k] to char*

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