source: src/AST/Convert.cpp@ 60aaa51d

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 60aaa51d was 60aaa51d, checked in by Aaron Moss <a3moss@…>, 7 years ago

More resolver porting; mostly CurrentObject

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