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