source: src/AST/Convert.cpp@ 8b34df0

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 8b34df0 was 8b34df0, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed incorrect copying in InferredParameters conversion

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