source: src/AST/Convert.cpp@ a62749f

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

Better support for loose labels.

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