source: src/AST/Convert.cpp@ 19e567dd

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 19e567dd was 19e567dd, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

expression conversion: first few cases and base members, both directions

  • Property mode set to 100644
File size: 37.4 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 : Fri May 17 16:01:00 2019
13// Update Count : 4
14//
15
16#include "Convert.hpp"
17
18#include "AST/Attribute.hpp"
19#include "AST/Decl.hpp"
20#include "AST/Expr.hpp"
21#include "AST/Init.hpp"
22#include "AST/Stmt.hpp"
23#include "AST/TypeSubstitution.hpp"
24
25#include "SynTree/Attribute.h"
26#include "SynTree/Declaration.h"
27#include "SynTree/TypeSubstitution.h"
28
29//================================================================================================
30// Utilities
31template<template <class...> class C>
32struct to {
33 template<typename T>
34 static auto from( T && v ) -> C< typename T::value_type > {
35 C< typename T::value_type > l;
36 std::move(std::begin(v), std::end(v), std::back_inserter(l));
37 return l;
38 }
39};
40
41//================================================================================================
42class ConverterNewToOld : public ast::Visitor {
43 BaseSyntaxNode * node = nullptr;
44
45 template<typename T>
46 struct Getter {
47 ConverterNewToOld & visitor;
48
49 template<typename U>
50 T * accept1( const ast::ptr<U> & ptr ) {
51 ptr->accept( visitor );
52 T * ret = strict_dynamic_cast< T * >( visitor.node );
53 visitor.node = nullptr;
54 return ret;
55 }
56
57 template<typename U>
58 std::list< T * > acceptL( const U & container ) {
59 std::list< T * > ret;
60 for (auto ptr : container ) {
61 ret.emplace_back( accept1( ptr ) );
62 }
63 return ret;
64 }
65 };
66
67 template<typename T>
68 Getter<T> get() {
69 return Getter<T>{ *this };
70 }
71
72 Label makeLabel(Statement * labelled, const ast::Label& label) {
73 return Label(
74 label.name,
75 labelled,
76 get<Attribute>().acceptL(label.attributes)
77 );
78 }
79
80 template<template <class...> class C>
81 std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
82 std::list<Label> ret;
83 for (auto label : labels) {
84 ret.push_back( makeLabel(labelled, label) );
85 }
86 return ret;
87 }
88
89public:
90 Declaration * decl( const ast::Decl * declNode ) {
91 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
92 }
93
94private:
95 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
96 (void)node;
97 return nullptr;
98 }
99
100 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
101 (void)node;
102 return nullptr;
103 }
104
105 const ast::Decl * visit( const ast::StructDecl * node ) override final {
106 (void)node;
107 return nullptr;
108 }
109
110 const ast::Decl * visit( const ast::UnionDecl * node ) override final {
111 (void)node;
112 return nullptr;
113 }
114
115 const ast::Decl * visit( const ast::EnumDecl * node ) override final {
116 (void)node;
117 return nullptr;
118 }
119
120 const ast::Decl * visit( const ast::TraitDecl * node ) override final {
121 (void)node;
122 return nullptr;
123 }
124
125 const ast::Decl * visit( const ast::TypeDecl * node ) override final {
126 (void)node;
127 return nullptr;
128 }
129
130 const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
131 (void)node;
132 return nullptr;
133 }
134
135 const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
136 (void)node;
137 return nullptr;
138 }
139
140 const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
141 (void)node;
142 return nullptr;
143 }
144
145 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
146 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
147 stmt->location = node->location;
148 stmt->labels = makeLabelL( stmt, node->labels );
149 this->node = stmt;
150 return nullptr;
151 }
152
153 const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
154 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) );
155 stmt->location = node->location;
156 stmt->labels = makeLabelL( stmt, node->labels );
157 this->node = stmt;
158 return nullptr;
159 }
160
161 const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
162 auto stmt = new AsmStmt(
163 node->isVolatile,
164 get<Expression>().accept1( node->instruction ),
165 get<Expression>().acceptL( node->output ),
166 get<Expression>().acceptL( node->input ),
167 get<ConstantExpr>().acceptL( node->clobber ),
168 makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
169 );
170 stmt->location = node->location;
171 stmt->labels = makeLabelL( stmt, node->labels );
172 this->node = stmt;
173 return nullptr;
174 }
175
176 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
177 auto stmt = new DirectiveStmt( node->directive );
178 stmt->location = node->location;
179 stmt->labels = makeLabelL( stmt, node->labels );
180 this->node = stmt;
181 return nullptr;
182 }
183
184 const ast::Stmt * visit( const ast::IfStmt * node ) override final {
185 auto stmt = new IfStmt(
186 get<Expression>().accept1( node->cond ),
187 get<Statement>().accept1( node->thenPart ),
188 get<Statement>().accept1( node->elsePart ),
189 get<Statement>().acceptL( node->inits )
190 );
191 stmt->location = node->location;
192 stmt->labels = makeLabelL( stmt, node->labels );
193 this->node = stmt;
194 return nullptr;
195 }
196
197 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
198 auto stmt = new SwitchStmt(
199 get<Expression>().accept1( node->cond ),
200 get<Statement>().acceptL( node->stmts )
201 );
202 stmt->location = node->location;
203 stmt->labels = makeLabelL( stmt, node->labels );
204 this->node = stmt;
205 return nullptr;
206 }
207
208 const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
209 auto stmt = new CaseStmt(
210 get<Expression>().accept1( node->cond ),
211 get<Statement>().acceptL( node->stmts ),
212 node->isDefault()
213 );
214 stmt->location = node->location;
215 stmt->labels = makeLabelL( stmt, node->labels );
216 this->node = stmt;
217 return nullptr;
218 }
219
220 const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
221 auto inits = get<Statement>().acceptL( node->inits );
222 auto stmt = new WhileStmt(
223 get<Expression>().accept1( node->cond ),
224 get<Statement>().accept1( node->body ),
225 inits,
226 node->isDoWhile
227 );
228 stmt->location = node->location;
229 stmt->labels = makeLabelL( stmt, node->labels );
230 this->node = stmt;
231 return nullptr;
232 }
233
234 const ast::Stmt * visit( const ast::ForStmt * node ) override final {
235 auto stmt = new ForStmt(
236 get<Statement>().acceptL( node->inits ),
237 get<Expression>().accept1( node->cond ),
238 get<Expression>().accept1( node->inc ),
239 get<Statement>().accept1( node->body )
240 );
241 stmt->location = node->location;
242 stmt->labels = makeLabelL( stmt, node->labels );
243 this->node = stmt;
244 return nullptr;
245 }
246
247 const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
248 BranchStmt * stmt;
249 if (node->computedTarget) {
250 stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
251 BranchStmt::Goto );
252 } else {
253 BranchStmt::Type type;
254 switch (node->kind) {
255 #define CASE(n) \
256 case ast::BranchStmt::n: \
257 type = BranchStmt::n; \
258 break
259 CASE(Goto);
260 CASE(Break);
261 CASE(Continue);
262 CASE(FallThrough);
263 CASE(FallThroughDefault);
264 #undef CASE
265 default:
266 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
267 }
268
269 // The labels here are also weird.
270 stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
271 stmt->target = makeLabel( stmt, node->target );
272 }
273 stmt->location = node->location;
274 stmt->labels = makeLabelL( stmt, node->labels );
275 this->node = stmt;
276 return nullptr;
277 }
278
279 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
280 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
281 stmt->location = node->location;
282 stmt->labels = makeLabelL( stmt, node->labels );
283 this->node = stmt;
284 return nullptr;
285 }
286
287 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
288 ThrowStmt::Kind kind;
289 switch (node->kind) {
290 case ast::ThrowStmt::Terminate:
291 kind = ThrowStmt::Terminate;
292 break;
293 case ast::ThrowStmt::Resume:
294 kind = ThrowStmt::Resume;
295 break;
296 default:
297 assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
298 }
299 auto stmt = new ThrowStmt(
300 kind,
301 get<Expression>().accept1( node->expr ),
302 get<Expression>().accept1( node->target )
303 );
304 stmt->location = node->location;
305 stmt->labels = makeLabelL( stmt, node->labels );
306 this->node = stmt;
307 return nullptr;
308 }
309
310 const ast::Stmt * visit( const ast::TryStmt * node ) override final {
311 auto handlers = get<CatchStmt>().acceptL( node->handlers );
312 auto stmt = new TryStmt(
313 get<CompoundStmt>().accept1( node->body ),
314 handlers,
315 get<FinallyStmt>().accept1( node->finally )
316 );
317 stmt->location = node->location;
318 stmt->labels = makeLabelL( stmt, node->labels );
319 this->node = stmt;
320 return nullptr;
321 }
322
323 const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
324 CatchStmt::Kind kind;
325 switch (node->kind) {
326 case ast::CatchStmt::Terminate:
327 kind = CatchStmt::Terminate;
328 break;
329 case ast::CatchStmt::Resume:
330 kind = CatchStmt::Resume;
331 break;
332 default:
333 assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
334 }
335 auto stmt = new CatchStmt(
336 kind,
337 get<Declaration>().accept1( node->decl ),
338 get<Expression>().accept1( node->cond ),
339 get<Statement>().accept1( node->body )
340 );
341 stmt->location = node->location;
342 stmt->labels = makeLabelL( stmt, node->labels );
343 this->node = stmt;
344 return nullptr;
345 }
346
347 const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
348 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
349 stmt->location = node->location;
350 stmt->labels = makeLabelL( stmt, node->labels );
351 this->node = stmt;
352 return nullptr;
353 }
354
355 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
356 auto stmt = new WaitForStmt;
357 stmt->clauses.reserve( node->clauses.size() );
358 for ( auto clause : node->clauses ) {
359 stmt->clauses.push_back({{
360 get<Expression>().accept1( clause.target.function ),
361 get<Expression>().acceptL( clause.target.arguments ),
362 },
363 get<Statement>().accept1( clause.stmt ),
364 get<Expression>().accept1( clause.cond ),
365 });
366 }
367 stmt->timeout = {
368 get<Expression>().accept1( node->timeout.time ),
369 get<Statement>().accept1( node->timeout.stmt ),
370 get<Expression>().accept1( node->timeout.cond ),
371 };
372 stmt->orelse = {
373 get<Statement>().accept1( node->orElse.stmt ),
374 get<Expression>().accept1( node->orElse.cond ),
375 };
376 stmt->location = node->location;
377 stmt->labels = makeLabelL( stmt, node->labels );
378 this->node = stmt;
379 return nullptr;
380 }
381
382 const ast::Stmt * visit( const ast::WithStmt * node ) override final {
383 auto stmt = new WithStmt(
384 get<Expression>().acceptL( node->exprs ),
385 get<Statement>().accept1( node->stmt )
386 );
387 stmt->location = node->location;
388 stmt->labels = makeLabelL( stmt, node->labels );
389 this->node = stmt;
390 return nullptr;
391 }
392
393 const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
394 auto stmt = new NullStmt();
395 stmt->location = node->location;
396 stmt->labels = makeLabelL( stmt, node->labels );
397 this->node = stmt;
398 return nullptr;
399 }
400
401 const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
402 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
403 stmt->location = node->location;
404 stmt->labels = makeLabelL( stmt, node->labels );
405 this->node = stmt;
406 return nullptr;
407 }
408
409 const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
410 (void)node;
411 return nullptr;
412 }
413
414 TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
415
416 TypeSubstitution *rslt = new TypeSubstitution();
417
418 for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
419 rslt->add( src_i->first,
420 get<Type>().accept1(src_i->second) );
421 }
422
423 for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
424 rslt->addVar( src_i->first,
425 get<Expression>().accept1(src_i->second) );
426 }
427
428 return rslt;
429 }
430
431 void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
432 std::vector<UniqueId> &tgtResnSlots,
433 const ast::Expr::InferUnion &srcInferred ) {
434
435 assert( tgtInferParams.empty() );
436 assert( tgtResnSlots.empty() );
437
438 if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
439 const ast::InferredParams &srcParams = srcInferred.inferParamsConst();
440 for (auto srcParam : srcParams) {
441 tgtInferParams[srcParam.first] = ParamEntry(
442 srcParam.second.decl,
443 get<Type>().accept1(srcParam.second.actualType),
444 get<Type>().accept1(srcParam.second.formalType),
445 get<Expression>().accept1(srcParam.second.expr)
446 );
447 }
448 } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots ) {
449 const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst();
450 for (auto srcSlot : srcSlots) {
451 tgtResnSlots.push_back(srcSlot);
452 }
453 }
454 }
455
456 Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
457
458 tgt->location = src->location;
459
460 tgt->result = get<Type>().accept1(src->result);
461 tgt->env = convertTypeSubstitution(src->env);
462
463 tgt->extension = src->extension;
464 convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
465
466 return tgt;
467 }
468
469 const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
470 auto expr = visitBaseExpr( node,
471 new ApplicationExpr(
472 get<Expression>().accept1(node->func),
473 get<Expression>().acceptL(node->args)
474 )
475 );
476 this->node = expr;
477 return nullptr;
478 }
479
480 const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
481 auto expr = visitBaseExpr( node,
482 new UntypedExpr(
483 get<Expression>().accept1(node->func),
484 get<Expression>().acceptL(node->args)
485 )
486 );
487 this->node = expr;
488 return nullptr;
489 }
490
491 const ast::Expr * visit( const ast::NameExpr * node ) override final {
492 auto expr = visitBaseExpr( node,
493 new NameExpr(
494 node->name
495 )
496 );
497 this->node = expr;
498 return nullptr;
499 }
500
501 const ast::Expr * visit( const ast::AddressExpr * node ) override final {
502 (void)node;
503 return nullptr;
504 }
505
506 const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
507 (void)node;
508 return nullptr;
509 }
510
511 const ast::Expr * visit( const ast::CastExpr * node ) override final {
512 (void)node;
513 return nullptr;
514 }
515
516 const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
517 (void)node;
518 return nullptr;
519 }
520
521 const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
522 (void)node;
523 return nullptr;
524 }
525
526 const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
527 (void)node;
528 return nullptr;
529 }
530
531 const ast::Expr * visit( const ast::MemberExpr * node ) override final {
532 (void)node;
533 return nullptr;
534 }
535
536 const ast::Expr * visit( const ast::VariableExpr * node ) override final {
537 (void)node;
538 return nullptr;
539 }
540
541 const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
542 (void)node;
543 return nullptr;
544 }
545
546 const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
547 (void)node;
548 return nullptr;
549 }
550
551 const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
552 (void)node;
553 return nullptr;
554 }
555
556 const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
557 (void)node;
558 return nullptr;
559 }
560
561 const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
562 (void)node;
563 return nullptr;
564 }
565
566 const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
567 (void)node;
568 return nullptr;
569 }
570
571 const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
572 (void)node;
573 return nullptr;
574 }
575
576 const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
577 (void)node;
578 return nullptr;
579 }
580
581 const ast::Expr * visit( const ast::CommaExpr * node ) override final {
582 (void)node;
583 return nullptr;
584 }
585
586 const ast::Expr * visit( const ast::TypeExpr * node ) override final {
587 (void)node;
588 return nullptr;
589 }
590
591 const ast::Expr * visit( const ast::AsmExpr * node ) override final {
592 (void)node;
593 return nullptr;
594 }
595
596 const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
597 (void)node;
598 return nullptr;
599 }
600
601 const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
602 (void)node;
603 return nullptr;
604 }
605
606 const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
607 (void)node;
608 return nullptr;
609 }
610
611 const ast::Expr * visit( const ast::RangeExpr * node ) override final {
612 (void)node;
613 return nullptr;
614 }
615
616 const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
617 (void)node;
618 return nullptr;
619 }
620
621 const ast::Expr * visit( const ast::TupleExpr * node ) override final {
622 (void)node;
623 return nullptr;
624 }
625
626 const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
627 (void)node;
628 return nullptr;
629 }
630
631 const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
632 (void)node;
633 return nullptr;
634 }
635
636 const ast::Expr * visit( const ast::StmtExpr * node ) override final {
637 (void)node;
638 return nullptr;
639 }
640
641 const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
642 (void)node;
643 return nullptr;
644 }
645
646 const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
647 (void)node;
648 return nullptr;
649 }
650
651 const ast::Expr * visit( const ast::InitExpr * node ) override final {
652 (void)node;
653 return nullptr;
654 }
655
656 const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
657 (void)node;
658 return nullptr;
659 }
660
661 const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
662 (void)node;
663 return nullptr;
664 }
665
666 const ast::Expr * visit( const ast::GenericExpr * node ) override final {
667 (void)node;
668 return nullptr;
669 }
670
671 const ast::Type * visit( const ast::VoidType * node ) override final {
672 (void)node;
673 return nullptr;
674 }
675
676 const ast::Type * visit( const ast::BasicType * node ) override final {
677 (void)node;
678 return nullptr;
679 }
680
681 const ast::Type * visit( const ast::PointerType * node ) override final {
682 (void)node;
683 return nullptr;
684 }
685
686 const ast::Type * visit( const ast::ArrayType * node ) override final {
687 (void)node;
688 return nullptr;
689 }
690
691 const ast::Type * visit( const ast::ReferenceType * node ) override final {
692 (void)node;
693 return nullptr;
694 }
695
696 const ast::Type * visit( const ast::QualifiedType * node ) override final {
697 (void)node;
698 return nullptr;
699 }
700
701 const ast::Type * visit( const ast::FunctionType * node ) override final {
702 (void)node;
703 return nullptr;
704 }
705
706 const ast::Type * visit( const ast::StructInstType * node ) override final {
707 (void)node;
708 return nullptr;
709 }
710
711 const ast::Type * visit( const ast::UnionInstType * node ) override final {
712 (void)node;
713 return nullptr;
714 }
715
716 const ast::Type * visit( const ast::EnumInstType * node ) override final {
717 (void)node;
718 return nullptr;
719 }
720
721 const ast::Type * visit( const ast::TraitInstType * node ) override final {
722 (void)node;
723 return nullptr;
724 }
725
726 const ast::Type * visit( const ast::TypeInstType * node ) override final {
727 (void)node;
728 return nullptr;
729 }
730
731 const ast::Type * visit( const ast::TupleType * node ) override final {
732 (void)node;
733 return nullptr;
734 }
735
736 const ast::Type * visit( const ast::TypeofType * node ) override final {
737 (void)node;
738 return nullptr;
739 }
740
741 const ast::Type * visit( const ast::VarArgsType * node ) override final {
742 (void)node;
743 return nullptr;
744 }
745
746 const ast::Type * visit( const ast::ZeroType * node ) override final {
747 (void)node;
748 return nullptr;
749 }
750
751 const ast::Type * visit( const ast::OneType * node ) override final {
752 (void)node;
753 return nullptr;
754 }
755
756 const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
757 (void)node;
758 return nullptr;
759 }
760
761 const ast::Designation * visit( const ast::Designation * node ) override final {
762 (void)node;
763 return nullptr;
764 }
765
766 const ast::Init * visit( const ast::SingleInit * node ) override final {
767 (void)node;
768 return nullptr;
769 }
770
771 const ast::Init * visit( const ast::ListInit * node ) override final {
772 (void)node;
773 return nullptr;
774 }
775
776 const ast::Init * visit( const ast::ConstructorInit * node ) override final {
777 (void)node;
778 return nullptr;
779 }
780
781 const ast::Attribute * visit( const ast::Attribute * node ) override final {
782 (void)node;
783 return nullptr;
784 }
785
786 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
787 (void)node;
788 return nullptr;
789 }
790};
791
792std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > && translationUnit ) {
793 ConverterNewToOld c;
794 std::list< Declaration * > decls;
795 for(auto d : translationUnit) {
796 decls.emplace_back( c.decl( d ) );
797 delete d;
798 }
799 return decls;
800}
801
802//================================================================================================
803
804class ConverterOldToNew : public Visitor {
805public:
806 ast::Decl * decl() {
807 return strict_dynamic_cast< ast::Decl * >( node );
808 }
809private:
810 ast::Node * node;
811
812 // Local Utilities:
813
814 template<typename NewT, typename OldT>
815 NewT * getAccept1( OldT old ) {
816 old->accept(*this);
817 return strict_dynamic_cast< NewT * >( node );
818 }
819
820# define GET_ACCEPT_1(child, type) \
821 getAccept1< ast::type, decltype( old->child ) >( old->child )
822
823 template<typename NewT, typename OldC>
824 std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) {
825 std::vector< ast::ptr<NewT> > ret;
826 ret.reserve( old.size() );
827 for ( auto a : old ) {
828 a->accept( *this );
829 ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
830 }
831 return ret;
832 }
833
834# define GET_ACCEPT_V(child, type) \
835 getAcceptV< ast::type, decltype( old->child ) >( old->child )
836
837 ast::Label make_label(Label* old) {
838 return ast::Label(
839 old->labelled->location,
840 old->name,
841 GET_ACCEPT_V(attributes, Attribute)
842 );
843 }
844
845 template<template <class...> class C>
846 C<ast::Label> make_labels(C<Label> olds) {
847 C<ast::Label> ret;
848 for (auto oldn : olds) {
849 ret.push_back( make_label( &oldn ) );
850 }
851 return ret;
852 }
853
854# define GET_LABELS_V(labels) \
855 to<std::vector>::from( make_labels( std::move( labels ) ) )
856
857 // Now all the visit functions:
858
859 virtual void visit( ObjectDecl * old ) override final {
860 auto decl = new ast::ObjectDecl(
861 old->location,
862 old->name,
863 GET_ACCEPT_1(type, Type),
864 GET_ACCEPT_1(init, Init),
865 { old->get_storageClasses().val },
866 { old->linkage.val },
867 GET_ACCEPT_1(bitfieldWidth, Expr),
868 GET_ACCEPT_V(attributes, Attribute),
869 { old->get_funcSpec().val }
870 );
871 decl->scopeLevel = old->scopeLevel;
872 decl->mangleName = old->mangleName;
873 decl->isDeleted = old->isDeleted;
874 decl->uniqueId = old->uniqueId;
875 decl->extension = old->extension;
876
877 this->node = decl;
878 }
879
880 virtual void visit( FunctionDecl * ) override final {
881
882 }
883
884 virtual void visit( StructDecl * old ) override final {
885 auto decl = new ast::StructDecl(
886 old->location,
887 old->name,
888 old->kind,
889 GET_ACCEPT_V(attributes, Attribute),
890 { old->linkage.val }
891 );
892 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
893 decl->body = old->body;
894 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
895 decl->members = GET_ACCEPT_V(members, Decl);
896 decl->extension = old->extension;
897 decl->uniqueId = old->uniqueId;
898 decl->storage = { old->storageClasses.val };
899
900 this->node = decl;
901 }
902
903 virtual void visit( UnionDecl * old ) override final {
904 auto decl = new ast::UnionDecl(
905 old->location,
906 old->name,
907 GET_ACCEPT_V(attributes, Attribute),
908 { old->linkage.val }
909 );
910 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
911 decl->body = old->body;
912 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
913 decl->members = GET_ACCEPT_V(members, Decl);
914 decl->extension = old->extension;
915 decl->uniqueId = old->uniqueId;
916 decl->storage = { old->storageClasses.val };
917
918 this->node = decl;
919 }
920
921 virtual void visit( EnumDecl * old ) override final {
922 auto decl = new ast::UnionDecl(
923 old->location,
924 old->name,
925 GET_ACCEPT_V(attributes, Attribute),
926 { old->linkage.val }
927 );
928 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
929 decl->body = old->body;
930 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
931 decl->members = GET_ACCEPT_V(members, Decl);
932 decl->extension = old->extension;
933 decl->uniqueId = old->uniqueId;
934 decl->storage = { old->storageClasses.val };
935
936 this->node = decl;
937 }
938
939 virtual void visit( TraitDecl * old ) override final {
940 auto decl = new ast::UnionDecl(
941 old->location,
942 old->name,
943 GET_ACCEPT_V(attributes, Attribute),
944 { old->linkage.val }
945 );
946 decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
947 decl->body = old->body;
948 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
949 decl->members = GET_ACCEPT_V(members, Decl);
950 decl->extension = old->extension;
951 decl->uniqueId = old->uniqueId;
952 decl->storage = { old->storageClasses.val };
953
954 this->node = decl;
955 }
956
957 virtual void visit( TypeDecl * ) override final {
958
959 }
960
961 virtual void visit( TypedefDecl * old ) override final {
962 auto decl = new ast::TypedefDecl(
963 old->location,
964 old->name,
965 { old->storageClasses.val },
966 GET_ACCEPT_1(base, Type),
967 { old->linkage.val }
968 );
969 decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
970 decl->params = GET_ACCEPT_V(parameters, TypeDecl);
971 decl->extension = old->extension;
972 decl->uniqueId = old->uniqueId;
973 decl->storage = { old->storageClasses.val };
974
975 this->node = decl;
976 }
977
978 virtual void visit( AsmDecl * ) override final {
979
980 }
981
982 virtual void visit( StaticAssertDecl * ) override final {
983
984 }
985
986 virtual void visit( CompoundStmt * old ) override final {
987 auto stmt = new ast::CompoundStmt(
988 old->location,
989 to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
990 GET_LABELS_V(old->labels)
991 );
992
993 this->node = stmt;
994 }
995
996 virtual void visit( ExprStmt * old ) override final {
997 this->node = new ast::ExprStmt(
998 old->location,
999 GET_ACCEPT_1(expr, Expr),
1000 GET_LABELS_V(old->labels)
1001 );
1002 }
1003
1004 virtual void visit( AsmStmt * old ) override final {
1005 this->node = new ast::AsmStmt(
1006 old->location,
1007 old->voltile,
1008 GET_ACCEPT_1(instruction, Expr),
1009 GET_ACCEPT_V(output, Expr),
1010 GET_ACCEPT_V(input, Expr),
1011 GET_ACCEPT_V(clobber, ConstantExpr),
1012 GET_LABELS_V(old->gotolabels),
1013 GET_LABELS_V(old->labels)
1014 );
1015 }
1016
1017 virtual void visit( DirectiveStmt * old ) override final {
1018 this->node = new ast::DirectiveStmt(
1019 old->location,
1020 old->directive,
1021 GET_LABELS_V(old->labels)
1022 );
1023 }
1024
1025 virtual void visit( IfStmt * old ) override final {
1026 this->node = new ast::IfStmt(
1027 old->location,
1028 GET_ACCEPT_1(condition, Expr),
1029 GET_ACCEPT_1(thenPart, Stmt),
1030 GET_ACCEPT_1(elsePart, Stmt),
1031 GET_ACCEPT_V(initialization, Stmt),
1032 GET_LABELS_V(old->labels)
1033 );
1034 }
1035
1036 virtual void visit( SwitchStmt * old ) override final {
1037 this->node = new ast::SwitchStmt(
1038 old->location,
1039 GET_ACCEPT_1(condition, Expr),
1040 GET_ACCEPT_V(statements, Stmt),
1041 GET_LABELS_V(old->labels)
1042 );
1043 }
1044
1045 virtual void visit( CaseStmt * old ) override final {
1046 this->node = new ast::CaseStmt(
1047 old->location,
1048 GET_ACCEPT_1(condition, Expr),
1049 GET_ACCEPT_V(stmts, Stmt),
1050 GET_LABELS_V(old->labels)
1051 );
1052 }
1053
1054 virtual void visit( WhileStmt * old ) override final {
1055 this->node = new ast::WhileStmt(
1056 old->location,
1057 GET_ACCEPT_1(condition, Expr),
1058 GET_ACCEPT_1(body, Stmt),
1059 GET_ACCEPT_V(initialization, Stmt),
1060 old->isDoWhile,
1061 GET_LABELS_V(old->labels)
1062 );
1063 }
1064
1065 virtual void visit( ForStmt * old ) override final {
1066 this->node = new ast::ForStmt(
1067 old->location,
1068 GET_ACCEPT_V(initialization, Stmt),
1069 GET_ACCEPT_1(condition, Expr),
1070 GET_ACCEPT_1(increment, Expr),
1071 GET_ACCEPT_1(body, Stmt),
1072 GET_LABELS_V(old->labels)
1073 );
1074 }
1075
1076 virtual void visit( BranchStmt * old ) override final {
1077 if (old->computedTarget) {
1078 this->node = new ast::BranchStmt(
1079 old->location,
1080 GET_ACCEPT_1(computedTarget, Expr),
1081 GET_LABELS_V(old->labels)
1082 );
1083 } else {
1084 ast::BranchStmt::Kind kind;
1085 switch (old->type) {
1086 #define CASE(n) \
1087 case BranchStmt::n: \
1088 kind = ast::BranchStmt::n; \
1089 break
1090 CASE(Goto);
1091 CASE(Break);
1092 CASE(Continue);
1093 CASE(FallThrough);
1094 CASE(FallThroughDefault);
1095 #undef CASE
1096 default:
1097 assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
1098 }
1099
1100 Label label = old->originalTarget;
1101 auto stmt = new ast::BranchStmt(
1102 old->location,
1103 kind,
1104 make_label(&label),
1105 GET_LABELS_V(old->labels)
1106 );
1107 stmt->target = make_label(&old->target);
1108 this->node = stmt;
1109 }
1110 }
1111
1112 virtual void visit( ReturnStmt * old ) override final {
1113 this->node = new ast::ReturnStmt(
1114 old->location,
1115 GET_ACCEPT_1(expr, Expr),
1116 GET_LABELS_V(old->labels)
1117 );
1118 }
1119
1120 virtual void visit( ThrowStmt * old ) override final {
1121 ast::ThrowStmt::Kind kind;
1122 switch (old->kind) {
1123 case ThrowStmt::Terminate:
1124 kind = ast::ThrowStmt::Terminate;
1125 break;
1126 case ThrowStmt::Resume:
1127 kind = ast::ThrowStmt::Resume;
1128 break;
1129 default:
1130 assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
1131 }
1132
1133 this->node = new ast::ThrowStmt(
1134 old->location,
1135 kind,
1136 GET_ACCEPT_1(expr, Expr),
1137 GET_ACCEPT_1(target, Expr),
1138 GET_LABELS_V(old->labels)
1139 );
1140 }
1141
1142 virtual void visit( TryStmt * old ) override final {
1143 this->node = new ast::TryStmt(
1144 old->location,
1145 GET_ACCEPT_1(block, CompoundStmt),
1146 GET_ACCEPT_V(handlers, CatchStmt),
1147 GET_ACCEPT_1(finallyBlock, FinallyStmt),
1148 GET_LABELS_V(old->labels)
1149 );
1150 }
1151
1152 virtual void visit( CatchStmt * old ) override final {
1153 ast::CatchStmt::Kind kind;
1154 switch (old->kind) {
1155 case CatchStmt::Terminate:
1156 kind = ast::CatchStmt::Terminate;
1157 break;
1158 case CatchStmt::Resume:
1159 kind = ast::CatchStmt::Resume;
1160 break;
1161 default:
1162 assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
1163 }
1164
1165 this->node = new ast::CatchStmt(
1166 old->location,
1167 kind,
1168 GET_ACCEPT_1(decl, Decl),
1169 GET_ACCEPT_1(cond, Expr),
1170 GET_ACCEPT_1(body, Stmt),
1171 GET_LABELS_V(old->labels)
1172 );
1173 }
1174
1175 virtual void visit( FinallyStmt * old ) override final {
1176 this->node = new ast::FinallyStmt(
1177 old->location,
1178 GET_ACCEPT_1(block, CompoundStmt),
1179 GET_LABELS_V(old->labels)
1180 );
1181 }
1182
1183 virtual void visit( WaitForStmt * old ) override final {
1184 ast::WaitForStmt * stmt = new ast::WaitForStmt(
1185 old->location,
1186 GET_LABELS_V(old->labels)
1187 );
1188
1189 stmt->clauses.reserve( old->clauses.size() );
1190 for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
1191 stmt->clauses.push_back({
1192 ast::WaitForStmt::Target{
1193 GET_ACCEPT_1(clauses[i].target.function, Expr),
1194 GET_ACCEPT_V(clauses[i].target.arguments, Expr)
1195 },
1196 GET_ACCEPT_1(clauses[i].statement, Stmt),
1197 GET_ACCEPT_1(clauses[i].condition, Expr)
1198 });
1199 }
1200 stmt->timeout = {
1201 GET_ACCEPT_1(timeout.time, Expr),
1202 GET_ACCEPT_1(timeout.statement, Stmt),
1203 GET_ACCEPT_1(timeout.condition, Expr),
1204 };
1205 stmt->orElse = {
1206 GET_ACCEPT_1(timeout.statement, Stmt),
1207 GET_ACCEPT_1(timeout.condition, Expr),
1208 };
1209
1210 this->node = stmt;
1211 }
1212
1213 virtual void visit( WithStmt * old ) override final {
1214 this->node = new ast::WithStmt(
1215 old->location,
1216 GET_ACCEPT_V(exprs, Expr),
1217 GET_ACCEPT_1(stmt, Stmt),
1218 GET_LABELS_V(old->labels)
1219 );
1220 }
1221
1222 virtual void visit( NullStmt * old ) override final {
1223 this->node = new ast::NullStmt(
1224 old->location,
1225 GET_LABELS_V(old->labels)
1226 );
1227 }
1228
1229 virtual void visit( DeclStmt * old ) override final {
1230 this->node = new ast::DeclStmt(
1231 old->location,
1232 GET_ACCEPT_1(decl, Decl),
1233 GET_LABELS_V(old->labels)
1234 );
1235 }
1236
1237 virtual void visit( ImplicitCtorDtorStmt * old ) override final {
1238 this->node = new ast::ImplicitCtorDtorStmt(
1239 old->location,
1240 GET_ACCEPT_1(callStmt, Stmt),
1241 GET_LABELS_V(old->labels)
1242 );
1243 }
1244
1245 ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
1246
1247 ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
1248
1249 for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
1250 rslt->add( old_i->first,
1251 getAccept1<ast::Type>(old_i->second) );
1252 }
1253
1254 for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
1255 rslt->addVar( old_i->first,
1256 getAccept1<ast::Expr>(old_i->second) );
1257 }
1258
1259 return rslt;
1260 }
1261
1262 void convertInferUnion(ast::Expr::InferUnion &newInferred,
1263 const std::map<UniqueId,ParamEntry> &oldInferParams,
1264 const std::vector<UniqueId> &oldResnSlots) {
1265
1266 assert( oldInferParams.empty() || oldResnSlots.empty() );
1267 assert( newInferred.mode == ast::Expr::InferUnion::Empty );
1268
1269 if ( !oldInferParams.empty() ) {
1270 ast::InferredParams &tgt = newInferred.inferParams();
1271 for (auto old : oldInferParams) {
1272 tgt[old.first] = ast::ParamEntry(
1273 old.second.decl,
1274 getAccept1<ast::Type>(old.second.actualType),
1275 getAccept1<ast::Type>(old.second.formalType),
1276 getAccept1<ast::Expr>(old.second.expr)
1277 );
1278 }
1279 } else if ( !oldResnSlots.empty() ) {
1280 ast::ResnSlots &tgt = newInferred.resnSlots();
1281 for (auto old : oldResnSlots) {
1282 tgt.push_back(old);
1283 }
1284 }
1285 }
1286
1287 ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) {
1288
1289 nw->result = GET_ACCEPT_1(result, Type);
1290 nw->env = convertTypeSubstitution(old->env);
1291
1292 nw->extension = old->extension;
1293 convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
1294
1295 return nw;
1296 }
1297
1298 virtual void visit( ApplicationExpr * old ) override final {
1299 this->node = visitBaseExpr( old,
1300 new ast::ApplicationExpr(
1301 old->location,
1302 GET_ACCEPT_1(function, Expr),
1303 GET_ACCEPT_V(args, Expr)
1304 )
1305 );
1306 }
1307
1308 virtual void visit( UntypedExpr * old ) override final {
1309 this->node = visitBaseExpr( old,
1310 new ast::UntypedExpr(
1311 old->location,
1312 GET_ACCEPT_1(function, Expr),
1313 GET_ACCEPT_V(args, Expr)
1314 )
1315 );
1316 }
1317
1318 virtual void visit( NameExpr * old ) override final {
1319 this->node = visitBaseExpr( old,
1320 new ast::NameExpr(
1321 old->location,
1322 old->get_name()
1323 )
1324 );
1325 }
1326
1327 virtual void visit( CastExpr * old ) override final {
1328 this->node = visitBaseExpr( old,
1329 new ast::CastExpr(
1330 old->location,
1331 nullptr, // cast's "to" type is expr's result type; converted in visitBaseExpr
1332 old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
1333 )
1334 );
1335 }
1336
1337 virtual void visit( KeywordCastExpr * ) override final {
1338
1339 }
1340
1341 virtual void visit( VirtualCastExpr * ) override final {
1342
1343 }
1344
1345 virtual void visit( AddressExpr * ) override final {
1346
1347 }
1348
1349 virtual void visit( LabelAddressExpr * ) override final {
1350
1351 }
1352
1353 virtual void visit( UntypedMemberExpr * ) override final {
1354
1355 }
1356
1357 virtual void visit( MemberExpr * ) override final {
1358
1359 }
1360
1361 virtual void visit( VariableExpr * ) override final {
1362
1363 }
1364
1365 virtual void visit( ConstantExpr * ) override final {
1366
1367 }
1368
1369 virtual void visit( SizeofExpr * ) override final {
1370
1371 }
1372
1373 virtual void visit( AlignofExpr * ) override final {
1374
1375 }
1376
1377 virtual void visit( UntypedOffsetofExpr * ) override final {
1378
1379 }
1380
1381 virtual void visit( OffsetofExpr * ) override final {
1382
1383 }
1384
1385 virtual void visit( OffsetPackExpr * ) override final {
1386
1387 }
1388
1389 virtual void visit( LogicalExpr * ) override final {
1390
1391 }
1392
1393 virtual void visit( ConditionalExpr * ) override final {
1394
1395 }
1396
1397 virtual void visit( CommaExpr * ) override final {
1398
1399 }
1400
1401 virtual void visit( TypeExpr * ) override final {
1402
1403 }
1404
1405 virtual void visit( AsmExpr * ) override final {
1406
1407 }
1408
1409 virtual void visit( ImplicitCopyCtorExpr * ) override final {
1410
1411 }
1412
1413 virtual void visit( ConstructorExpr * ) override final {
1414
1415 }
1416
1417 virtual void visit( CompoundLiteralExpr * ) override final {
1418
1419 }
1420
1421 virtual void visit( RangeExpr * ) override final {
1422
1423 }
1424
1425 virtual void visit( UntypedTupleExpr * ) override final {
1426
1427 }
1428
1429 virtual void visit( TupleExpr * ) override final {
1430
1431 }
1432
1433 virtual void visit( TupleIndexExpr * ) override final {
1434
1435 }
1436
1437 virtual void visit( TupleAssignExpr * ) override final {
1438
1439 }
1440
1441 virtual void visit( StmtExpr * ) override final {
1442
1443 }
1444
1445 virtual void visit( UniqueExpr * ) override final {
1446
1447 }
1448
1449 virtual void visit( UntypedInitExpr * ) override final {
1450
1451 }
1452
1453 virtual void visit( InitExpr * ) override final {
1454
1455 }
1456
1457 virtual void visit( DeletedExpr * ) override final {
1458
1459 }
1460
1461 virtual void visit( DefaultArgExpr * ) override final {
1462
1463 }
1464
1465 virtual void visit( GenericExpr * ) override final {
1466
1467 }
1468
1469 virtual void visit( VoidType * ) override final {
1470
1471 }
1472
1473 virtual void visit( BasicType * ) override final {
1474
1475 }
1476
1477 virtual void visit( PointerType * ) override final {
1478
1479 }
1480
1481 virtual void visit( ArrayType * ) override final {
1482
1483 }
1484
1485 virtual void visit( ReferenceType * ) override final {
1486
1487 }
1488
1489 virtual void visit( QualifiedType * ) override final {
1490
1491 }
1492
1493 virtual void visit( FunctionType * ) override final {
1494
1495 }
1496
1497 virtual void visit( StructInstType * ) override final {
1498
1499 }
1500
1501 virtual void visit( UnionInstType * ) override final {
1502
1503 }
1504
1505 virtual void visit( EnumInstType * ) override final {
1506
1507 }
1508
1509 virtual void visit( TraitInstType * ) override final {
1510
1511 }
1512
1513 virtual void visit( TypeInstType * ) override final {
1514
1515 }
1516
1517 virtual void visit( TupleType * ) override final {
1518
1519 }
1520
1521 virtual void visit( TypeofType * ) override final {
1522
1523 }
1524
1525 virtual void visit( AttrType * ) override final {
1526
1527 }
1528
1529 virtual void visit( VarArgsType * ) override final {
1530
1531 }
1532
1533 virtual void visit( ZeroType * ) override final {
1534
1535 }
1536
1537 virtual void visit( OneType * ) override final {
1538
1539 }
1540
1541 virtual void visit( GlobalScopeType * ) override final {
1542
1543 }
1544
1545 virtual void visit( Designation * ) override final {
1546
1547 }
1548
1549 virtual void visit( SingleInit * ) override final {
1550
1551 }
1552
1553 virtual void visit( ListInit * ) override final {
1554
1555 }
1556
1557 virtual void visit( ConstructorInit * ) override final {
1558
1559 }
1560
1561 virtual void visit( Constant * ) override final {
1562
1563 }
1564
1565 virtual void visit( Attribute * ) override final {
1566
1567 }
1568
1569 virtual void visit( AttrExpr * ) override final {
1570
1571 assert( 0 );
1572 }
1573};
1574
1575#undef GET_LABELS_V
1576#undef GET_ACCEPT_V
1577#undef GET_ACCEPT_1
1578
1579std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
1580 ConverterOldToNew c;
1581 std::list< ast::ptr< ast::Decl > > decls;
1582 for(auto d : translationUnit) {
1583 d->accept( c );
1584 decls.emplace_back( c.decl() );
1585 delete d;
1586 }
1587 return decls;
1588}
Note: See TracBrowser for help on using the repository browser.