source: src/AST/Convert.cpp@ f6cc734e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since f6cc734e was f6cc734e, checked in by Michael Brooks <mlbrooks@…>, 7 years ago

Fixing new-resolver bug with incorrectly reused FunctionType pieces in old-model AST after convert-back. (See code comments for detail.) Symptom was crash (with pure virtual method call, due to double delete) during InstantiateGeneric pass on Bootloader. Now libcfa build progresses to next file, src/prelude.o.

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