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