source: src/AST/Convert.cpp@ b408364

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

One DeclWithType::asmName conversion was missing.

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