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 -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu May 09 15::37::05 2019 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #include "Convert.hpp" |
---|
17 | |
---|
18 | #include "AST/Pass.hpp" |
---|
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 | |
---|
26 | |
---|
27 | #include "SynTree/Attribute.h" |
---|
28 | #include "SynTree/Declaration.h" |
---|
29 | |
---|
30 | //================================================================================================ |
---|
31 | // Utilities |
---|
32 | template<template <class...> class C> |
---|
33 | struct to { |
---|
34 | template<typename T> |
---|
35 | static auto from( T && v ) -> C< typename T::value_type > { |
---|
36 | C< typename T::value_type > l; |
---|
37 | std::move(std::begin(v), std::end(v), std::back_inserter(l)); |
---|
38 | return l; |
---|
39 | } |
---|
40 | }; |
---|
41 | |
---|
42 | //================================================================================================ |
---|
43 | class ConverterNewToOld { |
---|
44 | public: |
---|
45 | std::list< Declaration * > translationUnit; |
---|
46 | }; |
---|
47 | |
---|
48 | std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > & translationUnit ) { |
---|
49 | ast::Pass<ConverterNewToOld> converter; |
---|
50 | ast::accept_all(translationUnit, converter); |
---|
51 | return converter.pass.translationUnit; |
---|
52 | } |
---|
53 | |
---|
54 | //================================================================================================ |
---|
55 | |
---|
56 | #define ACCEPT_1(name, child, type) \ |
---|
57 | old->child->accept(*this); \ |
---|
58 | auto name = strict_dynamic_cast< ast::type * >( node ); |
---|
59 | |
---|
60 | #define ACCEPT_N(name, child, type) \ |
---|
61 | std::vector< ast::ptr<ast::type> > name; \ |
---|
62 | name.reserve( old->child.size() ); \ |
---|
63 | for( auto a : old->child ) { \ |
---|
64 | a->accept( *this ); \ |
---|
65 | name.emplace_back( strict_dynamic_cast< ast::type * >(node) ); \ |
---|
66 | } |
---|
67 | |
---|
68 | class ConverterOldToNew : public Visitor { |
---|
69 | public: |
---|
70 | ast::Decl * decl() { |
---|
71 | return strict_dynamic_cast< ast::Decl * >( node ); |
---|
72 | } |
---|
73 | private: |
---|
74 | ast::Node * node; |
---|
75 | |
---|
76 | template<template <class...> class C> |
---|
77 | C<ast::Label> make_labels(C<Label> olds) { |
---|
78 | C<ast::Label> ret; |
---|
79 | for(auto oldn : olds) { |
---|
80 | auto old = &oldn; // to reuse the MACRO |
---|
81 | ACCEPT_N(attr, attributes, Attribute) |
---|
82 | ast::Label l( |
---|
83 | {}, |
---|
84 | old->get_name(), |
---|
85 | to<std::vector>::from( std::move( attr ) ) |
---|
86 | ); |
---|
87 | ret.push_back( l ); |
---|
88 | } |
---|
89 | return ret; |
---|
90 | } |
---|
91 | |
---|
92 | virtual void visit( ObjectDecl * old ) override final { |
---|
93 | ACCEPT_1(type, type, Type) |
---|
94 | ACCEPT_1(init, init, Init) |
---|
95 | ACCEPT_1(bitWd, bitfieldWidth, Expr) |
---|
96 | ACCEPT_N(attr, attributes, Attribute) |
---|
97 | |
---|
98 | auto decl = new ast::ObjectDecl( |
---|
99 | old->location, |
---|
100 | old->name, |
---|
101 | type, |
---|
102 | init, |
---|
103 | { old->get_storageClasses().val }, |
---|
104 | { old->linkage.val }, |
---|
105 | bitWd, |
---|
106 | std::move( attr ), |
---|
107 | { old->get_funcSpec().val } |
---|
108 | ); |
---|
109 | decl->scopeLevel = old->scopeLevel; |
---|
110 | decl->mangleName = old->mangleName; |
---|
111 | decl->isDeleted = old->isDeleted; |
---|
112 | decl->uniqueId = old->uniqueId; |
---|
113 | decl->extension = old->extension; |
---|
114 | |
---|
115 | this->node = decl; |
---|
116 | } |
---|
117 | |
---|
118 | virtual void visit( FunctionDecl * ) override final { |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | virtual void visit( StructDecl * old ) override final { |
---|
123 | ACCEPT_N(members, members, Decl) |
---|
124 | ACCEPT_N(params, parameters, TypeDecl) |
---|
125 | ACCEPT_1(parent, parent, AggregateDecl) |
---|
126 | ACCEPT_N(attr, attributes, Attribute) |
---|
127 | |
---|
128 | auto decl = new ast::StructDecl( |
---|
129 | old->location, |
---|
130 | old->name, |
---|
131 | old->kind, |
---|
132 | std::move( attr ), |
---|
133 | { old->linkage.val } |
---|
134 | ); |
---|
135 | decl->parent = parent; |
---|
136 | decl->body = old->body; |
---|
137 | decl->parameters = params; |
---|
138 | decl->members = members; |
---|
139 | decl->extension = old->extension; |
---|
140 | decl->uniqueId = old->uniqueId; |
---|
141 | decl->storage = { old->storageClasses.val }; |
---|
142 | |
---|
143 | this->node = decl; |
---|
144 | } |
---|
145 | |
---|
146 | virtual void visit( UnionDecl * old ) override final { |
---|
147 | ACCEPT_N(members, members, Decl) |
---|
148 | ACCEPT_N(params, parameters, TypeDecl) |
---|
149 | ACCEPT_1(parent, parent, AggregateDecl) |
---|
150 | ACCEPT_N(attr, attributes, Attribute) |
---|
151 | |
---|
152 | auto decl = new ast::UnionDecl( |
---|
153 | old->location, |
---|
154 | old->name, |
---|
155 | std::move( attr ), |
---|
156 | { old->linkage.val } |
---|
157 | ); |
---|
158 | decl->parent = parent; |
---|
159 | decl->body = old->body; |
---|
160 | decl->parameters = params; |
---|
161 | decl->members = members; |
---|
162 | decl->extension = old->extension; |
---|
163 | decl->uniqueId = old->uniqueId; |
---|
164 | decl->storage = { old->storageClasses.val }; |
---|
165 | |
---|
166 | this->node = decl; |
---|
167 | } |
---|
168 | |
---|
169 | virtual void visit( EnumDecl * old ) override final { |
---|
170 | ACCEPT_N(members, members, Decl) |
---|
171 | ACCEPT_N(params, parameters, TypeDecl) |
---|
172 | ACCEPT_1(parent, parent, AggregateDecl) |
---|
173 | ACCEPT_N(attr, attributes, Attribute) |
---|
174 | |
---|
175 | auto decl = new ast::UnionDecl( |
---|
176 | old->location, |
---|
177 | old->name, |
---|
178 | std::move( attr ), |
---|
179 | { old->linkage.val } |
---|
180 | ); |
---|
181 | decl->parent = parent; |
---|
182 | decl->body = old->body; |
---|
183 | decl->parameters = params; |
---|
184 | decl->members = members; |
---|
185 | decl->extension = old->extension; |
---|
186 | decl->uniqueId = old->uniqueId; |
---|
187 | decl->storage = { old->storageClasses.val }; |
---|
188 | |
---|
189 | this->node = decl; |
---|
190 | } |
---|
191 | |
---|
192 | virtual void visit( TraitDecl * old ) override final { |
---|
193 | ACCEPT_N(members, members, Decl) |
---|
194 | ACCEPT_N(params, parameters, TypeDecl) |
---|
195 | ACCEPT_1(parent, parent, AggregateDecl) |
---|
196 | ACCEPT_N(attr, attributes, Attribute) |
---|
197 | |
---|
198 | auto decl = new ast::UnionDecl( |
---|
199 | old->location, |
---|
200 | old->name, |
---|
201 | std::move( attr ), |
---|
202 | { old->linkage.val } |
---|
203 | ); |
---|
204 | decl->parent = parent; |
---|
205 | decl->body = old->body; |
---|
206 | decl->parameters = params; |
---|
207 | decl->members = members; |
---|
208 | decl->extension = old->extension; |
---|
209 | decl->uniqueId = old->uniqueId; |
---|
210 | decl->storage = { old->storageClasses.val }; |
---|
211 | |
---|
212 | this->node = decl; |
---|
213 | } |
---|
214 | |
---|
215 | virtual void visit( TypeDecl * ) override final { |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | virtual void visit( TypedefDecl * old ) override final { |
---|
220 | ACCEPT_1(type, base, Type) |
---|
221 | ACCEPT_N(params, parameters, TypeDecl) |
---|
222 | ACCEPT_N(asserts, assertions, DeclWithType) |
---|
223 | auto decl = new ast::TypedefDecl( |
---|
224 | old->location, |
---|
225 | old->name, |
---|
226 | { old->storageClasses.val }, |
---|
227 | type, |
---|
228 | { old->linkage.val } |
---|
229 | ); |
---|
230 | |
---|
231 | decl->assertions = asserts; |
---|
232 | decl->parameters = params; |
---|
233 | decl->extension = old->extension; |
---|
234 | decl->uniqueId = old->uniqueId; |
---|
235 | decl->storage = { old->storageClasses.val }; |
---|
236 | |
---|
237 | this->node = decl; |
---|
238 | } |
---|
239 | |
---|
240 | virtual void visit( AsmDecl * ) override final { |
---|
241 | |
---|
242 | } |
---|
243 | |
---|
244 | virtual void visit( StaticAssertDecl * ) override final { |
---|
245 | |
---|
246 | } |
---|
247 | |
---|
248 | virtual void visit( CompoundStmt * old ) override final { |
---|
249 | ACCEPT_N(kids, kids, Stmt) |
---|
250 | auto stmt = new ast::CompoundStmt( |
---|
251 | old->location, |
---|
252 | to<std::list>::from( std::move(kids) ) |
---|
253 | ); |
---|
254 | stmt->labels = to<std::vector>::from( make_labels( std::move( old->labels ) ) ); |
---|
255 | |
---|
256 | this->node = stmt; |
---|
257 | } |
---|
258 | |
---|
259 | virtual void visit( ExprStmt * old ) override final { |
---|
260 | ACCEPT_1(expr, expr, Expr) |
---|
261 | auto stmt = new ast::ExprStmt( |
---|
262 | old->location, |
---|
263 | expr |
---|
264 | ); |
---|
265 | stmt->labels = to<std::vector>::from( make_labels( std::move( old->labels ) ) ); |
---|
266 | |
---|
267 | this->node = stmt; |
---|
268 | } |
---|
269 | |
---|
270 | virtual void visit( AsmStmt * ) override final { |
---|
271 | |
---|
272 | } |
---|
273 | |
---|
274 | virtual void visit( DirectiveStmt * ) override final { |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | virtual void visit( IfStmt * ) override final { |
---|
279 | |
---|
280 | } |
---|
281 | |
---|
282 | virtual void visit( WhileStmt * ) override final { |
---|
283 | |
---|
284 | } |
---|
285 | |
---|
286 | virtual void visit( ForStmt * ) override final { |
---|
287 | |
---|
288 | } |
---|
289 | |
---|
290 | virtual void visit( SwitchStmt * ) override final { |
---|
291 | |
---|
292 | } |
---|
293 | |
---|
294 | virtual void visit( CaseStmt * ) override final { |
---|
295 | |
---|
296 | } |
---|
297 | |
---|
298 | virtual void visit( BranchStmt * ) override final { |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | virtual void visit( ReturnStmt * ) override final { |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | virtual void visit( ThrowStmt * ) override final { |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | virtual void visit( TryStmt * ) override final { |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | virtual void visit( CatchStmt * ) override final { |
---|
315 | |
---|
316 | } |
---|
317 | |
---|
318 | virtual void visit( FinallyStmt * ) override final { |
---|
319 | |
---|
320 | } |
---|
321 | |
---|
322 | virtual void visit( WaitForStmt * ) override final { |
---|
323 | |
---|
324 | } |
---|
325 | |
---|
326 | virtual void visit( WithStmt * ) override final { |
---|
327 | |
---|
328 | } |
---|
329 | |
---|
330 | virtual void visit( NullStmt * old ) override final { |
---|
331 | auto stmt = new ast::NullStmt( |
---|
332 | old->location, |
---|
333 | to<std::vector>::from( make_labels( std::move( old->labels ) ) ) |
---|
334 | ); |
---|
335 | |
---|
336 | this->node = stmt; |
---|
337 | } |
---|
338 | |
---|
339 | virtual void visit( DeclStmt * ) override final { |
---|
340 | |
---|
341 | } |
---|
342 | |
---|
343 | virtual void visit( ImplicitCtorDtorStmt * ) override final { |
---|
344 | |
---|
345 | } |
---|
346 | |
---|
347 | virtual void visit( ApplicationExpr * ) override final { |
---|
348 | |
---|
349 | } |
---|
350 | |
---|
351 | virtual void visit( UntypedExpr * ) override final { |
---|
352 | |
---|
353 | } |
---|
354 | |
---|
355 | virtual void visit( NameExpr * ) override final { |
---|
356 | |
---|
357 | } |
---|
358 | |
---|
359 | virtual void visit( CastExpr * ) override final { |
---|
360 | |
---|
361 | } |
---|
362 | |
---|
363 | virtual void visit( KeywordCastExpr * ) override final { |
---|
364 | |
---|
365 | } |
---|
366 | |
---|
367 | virtual void visit( VirtualCastExpr * ) override final { |
---|
368 | |
---|
369 | } |
---|
370 | |
---|
371 | virtual void visit( AddressExpr * ) override final { |
---|
372 | |
---|
373 | } |
---|
374 | |
---|
375 | virtual void visit( LabelAddressExpr * ) override final { |
---|
376 | |
---|
377 | } |
---|
378 | |
---|
379 | virtual void visit( UntypedMemberExpr * ) override final { |
---|
380 | |
---|
381 | } |
---|
382 | |
---|
383 | virtual void visit( MemberExpr * ) override final { |
---|
384 | |
---|
385 | } |
---|
386 | |
---|
387 | virtual void visit( VariableExpr * ) override final { |
---|
388 | |
---|
389 | } |
---|
390 | |
---|
391 | virtual void visit( ConstantExpr * ) override final { |
---|
392 | |
---|
393 | } |
---|
394 | |
---|
395 | virtual void visit( SizeofExpr * ) override final { |
---|
396 | |
---|
397 | } |
---|
398 | |
---|
399 | virtual void visit( AlignofExpr * ) override final { |
---|
400 | |
---|
401 | } |
---|
402 | |
---|
403 | virtual void visit( UntypedOffsetofExpr * ) override final { |
---|
404 | |
---|
405 | } |
---|
406 | |
---|
407 | virtual void visit( OffsetofExpr * ) override final { |
---|
408 | |
---|
409 | } |
---|
410 | |
---|
411 | virtual void visit( OffsetPackExpr * ) override final { |
---|
412 | |
---|
413 | } |
---|
414 | |
---|
415 | virtual void visit( LogicalExpr * ) override final { |
---|
416 | |
---|
417 | } |
---|
418 | |
---|
419 | virtual void visit( ConditionalExpr * ) override final { |
---|
420 | |
---|
421 | } |
---|
422 | |
---|
423 | virtual void visit( CommaExpr * ) override final { |
---|
424 | |
---|
425 | } |
---|
426 | |
---|
427 | virtual void visit( TypeExpr * ) override final { |
---|
428 | |
---|
429 | } |
---|
430 | |
---|
431 | virtual void visit( AsmExpr * ) override final { |
---|
432 | |
---|
433 | } |
---|
434 | |
---|
435 | virtual void visit( ImplicitCopyCtorExpr * ) override final { |
---|
436 | |
---|
437 | } |
---|
438 | |
---|
439 | virtual void visit( ConstructorExpr * ) override final { |
---|
440 | |
---|
441 | } |
---|
442 | |
---|
443 | virtual void visit( CompoundLiteralExpr * ) override final { |
---|
444 | |
---|
445 | } |
---|
446 | |
---|
447 | virtual void visit( RangeExpr * ) override final { |
---|
448 | |
---|
449 | } |
---|
450 | |
---|
451 | virtual void visit( UntypedTupleExpr * ) override final { |
---|
452 | |
---|
453 | } |
---|
454 | |
---|
455 | virtual void visit( TupleExpr * ) override final { |
---|
456 | |
---|
457 | } |
---|
458 | |
---|
459 | virtual void visit( TupleIndexExpr * ) override final { |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | virtual void visit( TupleAssignExpr * ) override final { |
---|
464 | |
---|
465 | } |
---|
466 | |
---|
467 | virtual void visit( StmtExpr * ) override final { |
---|
468 | |
---|
469 | } |
---|
470 | |
---|
471 | virtual void visit( UniqueExpr * ) override final { |
---|
472 | |
---|
473 | } |
---|
474 | |
---|
475 | virtual void visit( UntypedInitExpr * ) override final { |
---|
476 | |
---|
477 | } |
---|
478 | |
---|
479 | virtual void visit( InitExpr * ) override final { |
---|
480 | |
---|
481 | } |
---|
482 | |
---|
483 | virtual void visit( DeletedExpr * ) override final { |
---|
484 | |
---|
485 | } |
---|
486 | |
---|
487 | virtual void visit( DefaultArgExpr * ) override final { |
---|
488 | |
---|
489 | } |
---|
490 | |
---|
491 | virtual void visit( GenericExpr * ) override final { |
---|
492 | |
---|
493 | } |
---|
494 | |
---|
495 | virtual void visit( VoidType * ) override final { |
---|
496 | |
---|
497 | } |
---|
498 | |
---|
499 | virtual void visit( BasicType * ) override final { |
---|
500 | |
---|
501 | } |
---|
502 | |
---|
503 | virtual void visit( PointerType * ) override final { |
---|
504 | |
---|
505 | } |
---|
506 | |
---|
507 | virtual void visit( ArrayType * ) override final { |
---|
508 | |
---|
509 | } |
---|
510 | |
---|
511 | virtual void visit( ReferenceType * ) override final { |
---|
512 | |
---|
513 | } |
---|
514 | |
---|
515 | virtual void visit( QualifiedType * ) override final { |
---|
516 | |
---|
517 | } |
---|
518 | |
---|
519 | virtual void visit( FunctionType * ) override final { |
---|
520 | |
---|
521 | } |
---|
522 | |
---|
523 | virtual void visit( StructInstType * ) override final { |
---|
524 | |
---|
525 | } |
---|
526 | |
---|
527 | virtual void visit( UnionInstType * ) override final { |
---|
528 | |
---|
529 | } |
---|
530 | |
---|
531 | virtual void visit( EnumInstType * ) override final { |
---|
532 | |
---|
533 | } |
---|
534 | |
---|
535 | virtual void visit( TraitInstType * ) override final { |
---|
536 | |
---|
537 | } |
---|
538 | |
---|
539 | virtual void visit( TypeInstType * ) override final { |
---|
540 | |
---|
541 | } |
---|
542 | |
---|
543 | virtual void visit( TupleType * ) override final { |
---|
544 | |
---|
545 | } |
---|
546 | |
---|
547 | virtual void visit( TypeofType * ) override final { |
---|
548 | |
---|
549 | } |
---|
550 | |
---|
551 | virtual void visit( AttrType * ) override final { |
---|
552 | |
---|
553 | } |
---|
554 | |
---|
555 | virtual void visit( VarArgsType * ) override final { |
---|
556 | |
---|
557 | } |
---|
558 | |
---|
559 | virtual void visit( ZeroType * ) override final { |
---|
560 | |
---|
561 | } |
---|
562 | |
---|
563 | virtual void visit( OneType * ) override final { |
---|
564 | |
---|
565 | } |
---|
566 | |
---|
567 | virtual void visit( GlobalScopeType * ) override final { |
---|
568 | |
---|
569 | } |
---|
570 | |
---|
571 | virtual void visit( Designation * ) override final { |
---|
572 | |
---|
573 | } |
---|
574 | |
---|
575 | virtual void visit( SingleInit * ) override final { |
---|
576 | |
---|
577 | } |
---|
578 | |
---|
579 | virtual void visit( ListInit * ) override final { |
---|
580 | |
---|
581 | } |
---|
582 | |
---|
583 | virtual void visit( ConstructorInit * ) override final { |
---|
584 | |
---|
585 | } |
---|
586 | |
---|
587 | virtual void visit( Constant * ) override final { |
---|
588 | |
---|
589 | } |
---|
590 | |
---|
591 | virtual void visit( Attribute * ) override final { |
---|
592 | |
---|
593 | } |
---|
594 | }; |
---|
595 | |
---|
596 | #undef ACCEPT_N |
---|
597 | #undef ACCEPT_1 |
---|
598 | |
---|
599 | std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > & translationUnit ) { |
---|
600 | ConverterOldToNew c; |
---|
601 | std::list< ast::ptr< ast::Decl > > decls; |
---|
602 | for(auto d : translationUnit) { |
---|
603 | d->accept( c ); |
---|
604 | decls.emplace_back( c.decl() ); |
---|
605 | delete d; |
---|
606 | } |
---|
607 | return decls; |
---|
608 | } |
---|