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 <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 | using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; |
---|
47 | Cache cache; |
---|
48 | |
---|
49 | template<typename T> |
---|
50 | struct Getter { |
---|
51 | ConverterNewToOld & visitor; |
---|
52 | |
---|
53 | template<typename U, enum ast::Node::ref_type R> |
---|
54 | T * accept1( const ast::ptr_base<U, R> & ptr ) { |
---|
55 | if ( ! ptr ) return nullptr; |
---|
56 | ptr->accept( visitor ); |
---|
57 | T * ret = strict_dynamic_cast< T * >( visitor.node ); |
---|
58 | visitor.node = nullptr; |
---|
59 | return ret; |
---|
60 | } |
---|
61 | |
---|
62 | template<typename U> |
---|
63 | std::list< T * > acceptL( const U & container ) { |
---|
64 | std::list< T * > ret; |
---|
65 | for (auto ptr : container ) { |
---|
66 | ret.emplace_back( accept1( ptr ) ); |
---|
67 | } |
---|
68 | return ret; |
---|
69 | } |
---|
70 | }; |
---|
71 | |
---|
72 | template<typename T> |
---|
73 | Getter<T> get() { |
---|
74 | return Getter<T>{ *this }; |
---|
75 | } |
---|
76 | |
---|
77 | Label makeLabel(Statement * labelled, const ast::Label& label) { |
---|
78 | return Label( |
---|
79 | label.name, |
---|
80 | labelled, |
---|
81 | get<Attribute>().acceptL(label.attributes) |
---|
82 | ); |
---|
83 | } |
---|
84 | |
---|
85 | template<template <class...> class C> |
---|
86 | std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) { |
---|
87 | std::list<Label> ret; |
---|
88 | for (auto label : labels) { |
---|
89 | ret.push_back( makeLabel(labelled, label) ); |
---|
90 | } |
---|
91 | return ret; |
---|
92 | } |
---|
93 | |
---|
94 | /// get new qualifiers from old type |
---|
95 | Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; } |
---|
96 | |
---|
97 | /// returns true and sets `node` if in cache |
---|
98 | bool inCache( const ast::Node * node ) { |
---|
99 | auto it = cache.find( node ); |
---|
100 | if ( it == cache.end() ) return false; |
---|
101 | this->node = it->second; |
---|
102 | return true; |
---|
103 | } |
---|
104 | |
---|
105 | public: |
---|
106 | Declaration * decl( const ast::Decl * declNode ) { |
---|
107 | return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) ); |
---|
108 | } |
---|
109 | |
---|
110 | private: |
---|
111 | void declPostamble( Declaration * decl, const ast::Decl * node ) { |
---|
112 | decl->location = node->location; |
---|
113 | // name comes from constructor |
---|
114 | // linkage comes from constructor |
---|
115 | decl->extension = node->extension; |
---|
116 | decl->uniqueId = node->uniqueId; |
---|
117 | // storageClasses comes from constructor |
---|
118 | this->node = decl; |
---|
119 | } |
---|
120 | |
---|
121 | const ast::DeclWithType * declWithTypePostamble ( |
---|
122 | DeclarationWithType * decl, const ast::DeclWithType * node ) { |
---|
123 | declPostamble( decl, node ); |
---|
124 | decl->mangleName = node->mangleName; |
---|
125 | decl->scopeLevel = node->scopeLevel; |
---|
126 | decl->asmName = get<Expression>().accept1( node->asmName ); |
---|
127 | // attributes comes from constructor |
---|
128 | decl->isDeleted = node->isDeleted; |
---|
129 | // fs comes from constructor |
---|
130 | cache.emplace( node, decl ); |
---|
131 | return nullptr; |
---|
132 | } |
---|
133 | |
---|
134 | const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { |
---|
135 | if ( inCache( node ) ) return nullptr; |
---|
136 | auto decl = new ObjectDecl( |
---|
137 | node->name, |
---|
138 | Type::StorageClasses( node->storage.val ), |
---|
139 | LinkageSpec::Spec( node->linkage.val ), |
---|
140 | get<Expression>().accept1( node->bitfieldWidth ), |
---|
141 | get<Type>().accept1( node->type ), |
---|
142 | get<Initializer>().accept1( node->init ), |
---|
143 | get<Attribute>().acceptL( node->attributes ), |
---|
144 | Type::FuncSpecifiers( node->funcSpec.val ) |
---|
145 | ); |
---|
146 | return declWithTypePostamble( decl, node ); |
---|
147 | } |
---|
148 | |
---|
149 | const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final { |
---|
150 | if ( inCache( node ) ) return nullptr; |
---|
151 | auto decl = new FunctionDecl( |
---|
152 | node->name, |
---|
153 | Type::StorageClasses( node->storage.val ), |
---|
154 | LinkageSpec::Spec( node->linkage.val ), |
---|
155 | get<FunctionType>().accept1( node->type ), |
---|
156 | get<CompoundStmt>().accept1( node->stmts ), |
---|
157 | get<Attribute>().acceptL( node->attributes ), |
---|
158 | Type::FuncSpecifiers( node->funcSpec.val ) |
---|
159 | ); |
---|
160 | decl->withExprs = get<Expression>().acceptL( node->withExprs ); |
---|
161 | return declWithTypePostamble( decl, node ); |
---|
162 | } |
---|
163 | |
---|
164 | // NamedTypeDecl |
---|
165 | const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) { |
---|
166 | declPostamble( decl, node ); |
---|
167 | // base comes from constructor |
---|
168 | decl->parameters = get<TypeDecl>().acceptL( node->params ); |
---|
169 | decl->assertions = get<DeclarationWithType>().acceptL( node->assertions ); |
---|
170 | return nullptr; |
---|
171 | } |
---|
172 | |
---|
173 | const ast::Decl * visit( const ast::TypeDecl * node ) override final { |
---|
174 | if ( inCache( node ) ) return nullptr; |
---|
175 | auto decl = new TypeDecl( |
---|
176 | node->name, |
---|
177 | Type::StorageClasses( node->storage.val ), |
---|
178 | get<Type>().accept1( node->base ), |
---|
179 | (TypeDecl::Kind)(unsigned)node->kind, |
---|
180 | node->sized, |
---|
181 | get<Type>().accept1( node->init ) |
---|
182 | ); |
---|
183 | cache.emplace( node, decl ); |
---|
184 | return namedTypePostamble( decl, node ); |
---|
185 | } |
---|
186 | |
---|
187 | const ast::Decl * visit( const ast::TypedefDecl * node ) override final { |
---|
188 | auto decl = new TypedefDecl( |
---|
189 | node->name, |
---|
190 | node->location, |
---|
191 | Type::StorageClasses( node->storage.val ), |
---|
192 | get<Type>().accept1( node->base ), |
---|
193 | LinkageSpec::Spec( node->linkage.val ) |
---|
194 | ); |
---|
195 | return namedTypePostamble( decl, node ); |
---|
196 | } |
---|
197 | |
---|
198 | const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) { |
---|
199 | decl->members = get<Declaration>().acceptL( node->members ); |
---|
200 | decl->parameters = get<TypeDecl>().acceptL( node->params ); |
---|
201 | decl->body = node->body; |
---|
202 | // attributes come from constructor |
---|
203 | decl->parent = get<AggregateDecl>().accept1( node->parent ); |
---|
204 | cache.emplace( node, decl ); |
---|
205 | return nullptr; |
---|
206 | } |
---|
207 | |
---|
208 | const ast::Decl * visit( const ast::StructDecl * node ) override final { |
---|
209 | if ( inCache( node ) ) return nullptr; |
---|
210 | auto decl = new StructDecl( |
---|
211 | node->name, |
---|
212 | node->kind, |
---|
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::UnionDecl * node ) override final { |
---|
220 | if ( inCache( node ) ) return nullptr; |
---|
221 | auto decl = new UnionDecl( |
---|
222 | node->name, |
---|
223 | get<Attribute>().acceptL( node->attributes ), |
---|
224 | LinkageSpec::Spec( node->linkage.val ) |
---|
225 | ); |
---|
226 | return aggregatePostamble( decl, node ); |
---|
227 | } |
---|
228 | |
---|
229 | const ast::Decl * visit( const ast::EnumDecl * node ) override final { |
---|
230 | if ( inCache( node ) ) return nullptr; |
---|
231 | auto decl = new EnumDecl( |
---|
232 | node->name, |
---|
233 | get<Attribute>().acceptL( node->attributes ), |
---|
234 | LinkageSpec::Spec( node->linkage.val ) |
---|
235 | ); |
---|
236 | return aggregatePostamble( decl, node ); |
---|
237 | } |
---|
238 | |
---|
239 | const ast::Decl * visit( const ast::TraitDecl * node ) override final { |
---|
240 | if ( inCache( node ) ) return nullptr; |
---|
241 | auto decl = new TraitDecl( |
---|
242 | node->name, |
---|
243 | {}, |
---|
244 | LinkageSpec::Spec( node->linkage.val ) |
---|
245 | ); |
---|
246 | return aggregatePostamble( decl, node ); |
---|
247 | } |
---|
248 | |
---|
249 | const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final { |
---|
250 | auto decl = new AsmDecl( get<AsmStmt>().accept1( node->stmt ) ); |
---|
251 | declPostamble( decl, node ); |
---|
252 | return nullptr; |
---|
253 | } |
---|
254 | |
---|
255 | const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final { |
---|
256 | auto decl = new StaticAssertDecl( |
---|
257 | get<Expression>().accept1( node->cond ), |
---|
258 | get<ConstantExpr>().accept1( node->msg ) |
---|
259 | ); |
---|
260 | declPostamble( decl, node ); |
---|
261 | return nullptr; |
---|
262 | } |
---|
263 | |
---|
264 | const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) { |
---|
265 | stmt->location = node->location; |
---|
266 | stmt->labels = makeLabelL( stmt, node->labels ); |
---|
267 | this->node = stmt; |
---|
268 | return nullptr; |
---|
269 | } |
---|
270 | |
---|
271 | const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final { |
---|
272 | auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) ); |
---|
273 | stmtPostamble( stmt, node ); |
---|
274 | return nullptr; |
---|
275 | } |
---|
276 | |
---|
277 | const ast::Stmt * visit( const ast::ExprStmt * node ) override final { |
---|
278 | auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) ); |
---|
279 | return stmtPostamble( stmt, node ); |
---|
280 | } |
---|
281 | |
---|
282 | const ast::Stmt * visit( const ast::AsmStmt * node ) override final { |
---|
283 | auto stmt = new AsmStmt( |
---|
284 | node->isVolatile, |
---|
285 | get<Expression>().accept1( node->instruction ), |
---|
286 | get<Expression>().acceptL( node->output ), |
---|
287 | get<Expression>().acceptL( node->input ), |
---|
288 | get<ConstantExpr>().acceptL( node->clobber ), |
---|
289 | makeLabelL( nullptr, node->gotoLabels ) // What are these labelling? |
---|
290 | ); |
---|
291 | return stmtPostamble( stmt, node ); |
---|
292 | } |
---|
293 | |
---|
294 | const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final { |
---|
295 | auto stmt = new DirectiveStmt( node->directive ); |
---|
296 | return stmtPostamble( stmt, node ); |
---|
297 | } |
---|
298 | |
---|
299 | const ast::Stmt * visit( const ast::IfStmt * node ) override final { |
---|
300 | auto stmt = new IfStmt( |
---|
301 | get<Expression>().accept1( node->cond ), |
---|
302 | get<Statement>().accept1( node->thenPart ), |
---|
303 | get<Statement>().accept1( node->elsePart ), |
---|
304 | get<Statement>().acceptL( node->inits ) |
---|
305 | ); |
---|
306 | return stmtPostamble( stmt, node ); |
---|
307 | } |
---|
308 | |
---|
309 | const ast::Stmt * visit( const ast::SwitchStmt * node ) override final { |
---|
310 | auto stmt = new SwitchStmt( |
---|
311 | get<Expression>().accept1( node->cond ), |
---|
312 | get<Statement>().acceptL( node->stmts ) |
---|
313 | ); |
---|
314 | return stmtPostamble( stmt, node ); |
---|
315 | } |
---|
316 | |
---|
317 | const ast::Stmt * visit( const ast::CaseStmt * node ) override final { |
---|
318 | auto stmt = new CaseStmt( |
---|
319 | get<Expression>().accept1( node->cond ), |
---|
320 | get<Statement>().acceptL( node->stmts ), |
---|
321 | node->isDefault() |
---|
322 | ); |
---|
323 | return stmtPostamble( stmt, node ); |
---|
324 | } |
---|
325 | |
---|
326 | const ast::Stmt * visit( const ast::WhileStmt * node ) override final { |
---|
327 | auto inits = get<Statement>().acceptL( node->inits ); |
---|
328 | auto stmt = new WhileStmt( |
---|
329 | get<Expression>().accept1( node->cond ), |
---|
330 | get<Statement>().accept1( node->body ), |
---|
331 | inits, |
---|
332 | node->isDoWhile |
---|
333 | ); |
---|
334 | return stmtPostamble( stmt, node ); |
---|
335 | } |
---|
336 | |
---|
337 | const ast::Stmt * visit( const ast::ForStmt * node ) override final { |
---|
338 | auto stmt = new ForStmt( |
---|
339 | get<Statement>().acceptL( node->inits ), |
---|
340 | get<Expression>().accept1( node->cond ), |
---|
341 | get<Expression>().accept1( node->inc ), |
---|
342 | get<Statement>().accept1( node->body ) |
---|
343 | ); |
---|
344 | return stmtPostamble( stmt, node ); |
---|
345 | } |
---|
346 | |
---|
347 | const ast::Stmt * visit( const ast::BranchStmt * node ) override final { |
---|
348 | BranchStmt * stmt; |
---|
349 | if (node->computedTarget) { |
---|
350 | stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ), |
---|
351 | BranchStmt::Goto ); |
---|
352 | } else { |
---|
353 | BranchStmt::Type type; |
---|
354 | switch (node->kind) { |
---|
355 | #define CASE(n) \ |
---|
356 | case ast::BranchStmt::n: \ |
---|
357 | type = BranchStmt::n; \ |
---|
358 | break |
---|
359 | CASE(Goto); |
---|
360 | CASE(Break); |
---|
361 | CASE(Continue); |
---|
362 | CASE(FallThrough); |
---|
363 | CASE(FallThroughDefault); |
---|
364 | #undef CASE |
---|
365 | default: |
---|
366 | assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind); |
---|
367 | } |
---|
368 | |
---|
369 | // The labels here are also weird. |
---|
370 | stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type ); |
---|
371 | stmt->target = makeLabel( stmt, node->target ); |
---|
372 | } |
---|
373 | return stmtPostamble( stmt, node ); |
---|
374 | } |
---|
375 | |
---|
376 | const ast::Stmt * visit( const ast::ReturnStmt * node ) override final { |
---|
377 | auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) ); |
---|
378 | return stmtPostamble( stmt, node ); |
---|
379 | } |
---|
380 | |
---|
381 | const ast::Stmt * visit( const ast::ThrowStmt * node ) override final { |
---|
382 | ThrowStmt::Kind kind; |
---|
383 | switch (node->kind) { |
---|
384 | case ast::ThrowStmt::Terminate: |
---|
385 | kind = ThrowStmt::Terminate; |
---|
386 | break; |
---|
387 | case ast::ThrowStmt::Resume: |
---|
388 | kind = ThrowStmt::Resume; |
---|
389 | break; |
---|
390 | default: |
---|
391 | assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind); |
---|
392 | } |
---|
393 | auto stmt = new ThrowStmt( |
---|
394 | kind, |
---|
395 | get<Expression>().accept1( node->expr ), |
---|
396 | get<Expression>().accept1( node->target ) |
---|
397 | ); |
---|
398 | return stmtPostamble( stmt, node ); |
---|
399 | } |
---|
400 | |
---|
401 | const ast::Stmt * visit( const ast::TryStmt * node ) override final { |
---|
402 | auto handlers = get<CatchStmt>().acceptL( node->handlers ); |
---|
403 | auto stmt = new TryStmt( |
---|
404 | get<CompoundStmt>().accept1( node->body ), |
---|
405 | handlers, |
---|
406 | get<FinallyStmt>().accept1( node->finally ) |
---|
407 | ); |
---|
408 | return stmtPostamble( stmt, node ); |
---|
409 | } |
---|
410 | |
---|
411 | const ast::Stmt * visit( const ast::CatchStmt * node ) override final { |
---|
412 | CatchStmt::Kind kind; |
---|
413 | switch (node->kind) { |
---|
414 | case ast::CatchStmt::Terminate: |
---|
415 | kind = CatchStmt::Terminate; |
---|
416 | break; |
---|
417 | case ast::CatchStmt::Resume: |
---|
418 | kind = CatchStmt::Resume; |
---|
419 | break; |
---|
420 | default: |
---|
421 | assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind); |
---|
422 | } |
---|
423 | auto stmt = new CatchStmt( |
---|
424 | kind, |
---|
425 | get<Declaration>().accept1( node->decl ), |
---|
426 | get<Expression>().accept1( node->cond ), |
---|
427 | get<Statement>().accept1( node->body ) |
---|
428 | ); |
---|
429 | return stmtPostamble( stmt, node ); |
---|
430 | } |
---|
431 | |
---|
432 | const ast::Stmt * visit( const ast::FinallyStmt * node ) override final { |
---|
433 | auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) ); |
---|
434 | return stmtPostamble( stmt, node ); |
---|
435 | } |
---|
436 | |
---|
437 | const ast::Stmt * visit( const ast::WaitForStmt * node ) override final { |
---|
438 | auto stmt = new WaitForStmt; |
---|
439 | stmt->clauses.reserve( node->clauses.size() ); |
---|
440 | for ( auto clause : node->clauses ) { |
---|
441 | stmt->clauses.push_back({{ |
---|
442 | get<Expression>().accept1( clause.target.func ), |
---|
443 | get<Expression>().acceptL( clause.target.args ), |
---|
444 | }, |
---|
445 | get<Statement>().accept1( clause.stmt ), |
---|
446 | get<Expression>().accept1( clause.cond ), |
---|
447 | }); |
---|
448 | } |
---|
449 | stmt->timeout = { |
---|
450 | get<Expression>().accept1( node->timeout.time ), |
---|
451 | get<Statement>().accept1( node->timeout.stmt ), |
---|
452 | get<Expression>().accept1( node->timeout.cond ), |
---|
453 | }; |
---|
454 | stmt->orelse = { |
---|
455 | get<Statement>().accept1( node->orElse.stmt ), |
---|
456 | get<Expression>().accept1( node->orElse.cond ), |
---|
457 | }; |
---|
458 | return stmtPostamble( stmt, node ); |
---|
459 | } |
---|
460 | |
---|
461 | const ast::Stmt * visit( const ast::WithStmt * node ) override final { |
---|
462 | auto stmt = new WithStmt( |
---|
463 | get<Expression>().acceptL( node->exprs ), |
---|
464 | get<Statement>().accept1( node->stmt ) |
---|
465 | ); |
---|
466 | return stmtPostamble( stmt, node ); |
---|
467 | } |
---|
468 | |
---|
469 | const ast::NullStmt * visit( const ast::NullStmt * node ) override final { |
---|
470 | auto stmt = new NullStmt(); |
---|
471 | stmtPostamble( stmt, node ); |
---|
472 | return nullptr; |
---|
473 | } |
---|
474 | |
---|
475 | const ast::Stmt * visit( const ast::DeclStmt * node ) override final { |
---|
476 | auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) ); |
---|
477 | return stmtPostamble( stmt, node ); |
---|
478 | } |
---|
479 | |
---|
480 | const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final { |
---|
481 | (void)node; |
---|
482 | return nullptr; |
---|
483 | } |
---|
484 | |
---|
485 | TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) { |
---|
486 | |
---|
487 | TypeSubstitution *rslt = new TypeSubstitution(); |
---|
488 | |
---|
489 | for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) { |
---|
490 | rslt->add( src_i->first, |
---|
491 | get<Type>().accept1(src_i->second) ); |
---|
492 | } |
---|
493 | |
---|
494 | for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) { |
---|
495 | rslt->addVar( src_i->first, |
---|
496 | get<Expression>().accept1(src_i->second) ); |
---|
497 | } |
---|
498 | |
---|
499 | return rslt; |
---|
500 | } |
---|
501 | |
---|
502 | void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams, |
---|
503 | std::vector<UniqueId> &tgtResnSlots, |
---|
504 | const ast::Expr::InferUnion &srcInferred ) { |
---|
505 | |
---|
506 | assert( tgtInferParams.empty() ); |
---|
507 | assert( tgtResnSlots.empty() ); |
---|
508 | |
---|
509 | if ( srcInferred.mode == ast::Expr::InferUnion::Params ) { |
---|
510 | const ast::InferredParams &srcParams = srcInferred.inferParamsConst(); |
---|
511 | for (auto srcParam : srcParams) { |
---|
512 | tgtInferParams[srcParam.first] = ParamEntry( |
---|
513 | srcParam.second.decl, |
---|
514 | get<Type>().accept1(srcParam.second.actualType), |
---|
515 | get<Type>().accept1(srcParam.second.formalType), |
---|
516 | get<Expression>().accept1(srcParam.second.expr) |
---|
517 | ); |
---|
518 | } |
---|
519 | } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots ) { |
---|
520 | const ast::ResnSlots &srcSlots = srcInferred.resnSlotsConst(); |
---|
521 | for (auto srcSlot : srcSlots) { |
---|
522 | tgtResnSlots.push_back(srcSlot); |
---|
523 | } |
---|
524 | } |
---|
525 | } |
---|
526 | |
---|
527 | Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) { |
---|
528 | |
---|
529 | tgt->location = src->location; |
---|
530 | |
---|
531 | tgt->result = get<Type>().accept1(src->result); |
---|
532 | tgt->env = convertTypeSubstitution(src->env); |
---|
533 | |
---|
534 | tgt->extension = src->extension; |
---|
535 | convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred); |
---|
536 | |
---|
537 | return tgt; |
---|
538 | } |
---|
539 | |
---|
540 | const ast::Expr * visit( const ast::ApplicationExpr * node ) override final { |
---|
541 | auto expr = visitBaseExpr( node, |
---|
542 | new ApplicationExpr( |
---|
543 | get<Expression>().accept1(node->func), |
---|
544 | get<Expression>().acceptL(node->args) |
---|
545 | ) |
---|
546 | ); |
---|
547 | this->node = expr; |
---|
548 | return nullptr; |
---|
549 | } |
---|
550 | |
---|
551 | const ast::Expr * visit( const ast::UntypedExpr * node ) override final { |
---|
552 | auto expr = visitBaseExpr( node, |
---|
553 | new UntypedExpr( |
---|
554 | get<Expression>().accept1(node->func), |
---|
555 | get<Expression>().acceptL(node->args) |
---|
556 | ) |
---|
557 | ); |
---|
558 | this->node = expr; |
---|
559 | return nullptr; |
---|
560 | } |
---|
561 | |
---|
562 | const ast::Expr * visit( const ast::NameExpr * node ) override final { |
---|
563 | auto expr = visitBaseExpr( node, |
---|
564 | new NameExpr( |
---|
565 | node->name |
---|
566 | ) |
---|
567 | ); |
---|
568 | this->node = expr; |
---|
569 | return nullptr; |
---|
570 | } |
---|
571 | |
---|
572 | const ast::Expr * visit( const ast::AddressExpr * node ) override final { |
---|
573 | auto expr = visitBaseExpr( node, |
---|
574 | new AddressExpr( |
---|
575 | get<Expression>().accept1(node->arg) |
---|
576 | ) |
---|
577 | ); |
---|
578 | this->node = expr; |
---|
579 | return nullptr; |
---|
580 | } |
---|
581 | |
---|
582 | const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final { |
---|
583 | auto expr = visitBaseExpr( node, |
---|
584 | new LabelAddressExpr( |
---|
585 | makeLabel(nullptr, node->arg) |
---|
586 | ) |
---|
587 | ); |
---|
588 | this->node = expr; |
---|
589 | return nullptr; |
---|
590 | } |
---|
591 | |
---|
592 | const ast::Expr * visit( const ast::CastExpr * node ) override final { |
---|
593 | auto expr = visitBaseExpr( node, |
---|
594 | new CastExpr( |
---|
595 | get<Expression>().accept1(node->arg), |
---|
596 | (node->isGenerated == ast::GeneratedCast) |
---|
597 | ) |
---|
598 | ); |
---|
599 | this->node = expr; |
---|
600 | return nullptr; |
---|
601 | } |
---|
602 | |
---|
603 | const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final { |
---|
604 | KeywordCastExpr::Target castTarget = KeywordCastExpr::NUMBER_OF_TARGETS; |
---|
605 | switch (node->target) { |
---|
606 | case ast::KeywordCastExpr::Coroutine: |
---|
607 | castTarget = KeywordCastExpr::Coroutine; |
---|
608 | break; |
---|
609 | case ast::KeywordCastExpr::Thread: |
---|
610 | castTarget = KeywordCastExpr::Thread; |
---|
611 | break; |
---|
612 | case ast::KeywordCastExpr::Monitor: |
---|
613 | castTarget = KeywordCastExpr::Monitor; |
---|
614 | break; |
---|
615 | default: |
---|
616 | break; |
---|
617 | } |
---|
618 | assert ( castTarget < KeywordCastExpr::NUMBER_OF_TARGETS ); |
---|
619 | auto expr = visitBaseExpr( node, |
---|
620 | new KeywordCastExpr( |
---|
621 | get<Expression>().accept1(node->arg), |
---|
622 | castTarget |
---|
623 | ) |
---|
624 | ); |
---|
625 | this->node = expr; |
---|
626 | return nullptr; |
---|
627 | } |
---|
628 | |
---|
629 | const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final { |
---|
630 | auto expr = visitBaseExpr( node, |
---|
631 | new VirtualCastExpr( |
---|
632 | get<Expression>().accept1(node->arg), |
---|
633 | nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr |
---|
634 | ) |
---|
635 | ); |
---|
636 | this->node = expr; |
---|
637 | return nullptr; |
---|
638 | } |
---|
639 | |
---|
640 | const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final { |
---|
641 | auto expr = visitBaseExpr( node, |
---|
642 | new UntypedMemberExpr( |
---|
643 | get<Expression>().accept1(node->member), |
---|
644 | get<Expression>().accept1(node->aggregate) |
---|
645 | ) |
---|
646 | ); |
---|
647 | this->node = expr; |
---|
648 | return nullptr; |
---|
649 | } |
---|
650 | |
---|
651 | const ast::Expr * visit( const ast::MemberExpr * node ) override final { |
---|
652 | auto expr = visitBaseExpr( node, |
---|
653 | new MemberExpr( |
---|
654 | inCache(node->member) ? |
---|
655 | dynamic_cast<DeclarationWithType *>(this->node) : |
---|
656 | get<DeclarationWithType>().accept1(node->member), |
---|
657 | get<Expression>().accept1(node->aggregate) |
---|
658 | ) |
---|
659 | ); |
---|
660 | this->node = expr; |
---|
661 | return nullptr; |
---|
662 | } |
---|
663 | |
---|
664 | const ast::Expr * visit( const ast::VariableExpr * node ) override final { |
---|
665 | auto expr = visitBaseExpr( node, |
---|
666 | new VariableExpr( |
---|
667 | inCache(node->var) ? |
---|
668 | dynamic_cast<DeclarationWithType *>(this->node) : |
---|
669 | get<DeclarationWithType>().accept1(node->var) |
---|
670 | ) |
---|
671 | ); |
---|
672 | this->node = expr; |
---|
673 | return nullptr; |
---|
674 | } |
---|
675 | |
---|
676 | bool isIntlikeConstantType(const ast::Type *t) { |
---|
677 | if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
678 | if ( basicType->isInteger() ) { |
---|
679 | return true; |
---|
680 | } |
---|
681 | } else if ( dynamic_cast< const ast::OneType * >( t ) ) { |
---|
682 | return true; |
---|
683 | } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) { |
---|
684 | return true; |
---|
685 | } else if ( dynamic_cast< const ast::PointerType * >( t ) ) { |
---|
686 | // null pointer constants, with zero int-values |
---|
687 | return true; |
---|
688 | } |
---|
689 | return false; |
---|
690 | } |
---|
691 | |
---|
692 | bool isFloatlikeConstantType(const ast::Type *t) { |
---|
693 | if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) { |
---|
694 | if ( ! bty->isInteger() ) { |
---|
695 | return true; |
---|
696 | } |
---|
697 | } |
---|
698 | return false; |
---|
699 | } |
---|
700 | |
---|
701 | bool isStringlikeConstantType(const ast::Type *t) { |
---|
702 | if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) { |
---|
703 | if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) { |
---|
704 | if ( bty->kind == ast::BasicType::Kind::Char ) { |
---|
705 | return true; |
---|
706 | } |
---|
707 | } |
---|
708 | } |
---|
709 | return false; |
---|
710 | } |
---|
711 | |
---|
712 | const ast::Expr * visit( const ast::ConstantExpr * node ) override final { |
---|
713 | ConstantExpr *rslt = nullptr; |
---|
714 | if (isIntlikeConstantType(node->result)) { |
---|
715 | rslt = new ConstantExpr(Constant( |
---|
716 | get<Type>().accept1(node->result), |
---|
717 | node->rep, |
---|
718 | (unsigned long long) node->intValue() |
---|
719 | )); |
---|
720 | } else if (isFloatlikeConstantType(node->result)) { |
---|
721 | rslt = new ConstantExpr(Constant( |
---|
722 | get<Type>().accept1(node->result), |
---|
723 | node->rep, |
---|
724 | (double) node->floatValue() |
---|
725 | )); |
---|
726 | } else if (isStringlikeConstantType(node->result)) { |
---|
727 | rslt = new ConstantExpr(Constant::from_string( |
---|
728 | node->rep |
---|
729 | )); |
---|
730 | } |
---|
731 | assert(rslt); |
---|
732 | auto expr = visitBaseExpr( node, rslt ); |
---|
733 | this->node = expr; |
---|
734 | return nullptr; |
---|
735 | } |
---|
736 | |
---|
737 | const ast::Expr * visit( const ast::SizeofExpr * node ) override final { |
---|
738 | assert (node->expr || node->type); |
---|
739 | assert (! (node->expr && node->type)); |
---|
740 | SizeofExpr *rslt; |
---|
741 | if (node->expr) { |
---|
742 | rslt = new SizeofExpr( |
---|
743 | get<Expression>().accept1(node->expr) |
---|
744 | ); |
---|
745 | assert (!rslt->isType); |
---|
746 | } |
---|
747 | if (node->type) { |
---|
748 | rslt = new SizeofExpr( |
---|
749 | get<Type>().accept1(node->type) |
---|
750 | ); |
---|
751 | assert (rslt->isType); |
---|
752 | } |
---|
753 | auto expr = visitBaseExpr( node, rslt ); |
---|
754 | this->node = expr; |
---|
755 | return nullptr; |
---|
756 | } |
---|
757 | |
---|
758 | const ast::Expr * visit( const ast::AlignofExpr * node ) override final { |
---|
759 | assert (node->expr || node->type); |
---|
760 | assert (! (node->expr && node->type)); |
---|
761 | AlignofExpr *rslt; |
---|
762 | if (node->expr) { |
---|
763 | rslt = new AlignofExpr( |
---|
764 | get<Expression>().accept1(node->expr) |
---|
765 | ); |
---|
766 | assert (!rslt->isType); |
---|
767 | } |
---|
768 | if (node->type) { |
---|
769 | rslt = new AlignofExpr( |
---|
770 | get<Type>().accept1(node->type) |
---|
771 | ); |
---|
772 | assert (rslt->isType); |
---|
773 | } |
---|
774 | auto expr = visitBaseExpr( node, rslt ); |
---|
775 | this->node = expr; |
---|
776 | return nullptr; |
---|
777 | } |
---|
778 | |
---|
779 | const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final { |
---|
780 | auto expr = visitBaseExpr( node, |
---|
781 | new UntypedOffsetofExpr( |
---|
782 | get<Type>().accept1(node->type), |
---|
783 | node->member |
---|
784 | ) |
---|
785 | ); |
---|
786 | this->node = expr; |
---|
787 | return nullptr; |
---|
788 | } |
---|
789 | |
---|
790 | const ast::Expr * visit( const ast::OffsetofExpr * node ) override final { |
---|
791 | auto expr = visitBaseExpr( node, |
---|
792 | new OffsetofExpr( |
---|
793 | get<Type>().accept1(node->type), |
---|
794 | inCache(node->member) ? |
---|
795 | dynamic_cast<DeclarationWithType *>(this->node) : |
---|
796 | get<DeclarationWithType>().accept1(node->member) |
---|
797 | ) |
---|
798 | ); |
---|
799 | this->node = expr; |
---|
800 | return nullptr; |
---|
801 | } |
---|
802 | |
---|
803 | const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final { |
---|
804 | auto expr = visitBaseExpr( node, |
---|
805 | new OffsetPackExpr( |
---|
806 | get<StructInstType>().accept1(node->type) |
---|
807 | ) |
---|
808 | ); |
---|
809 | this->node = expr; |
---|
810 | return nullptr; |
---|
811 | } |
---|
812 | |
---|
813 | const ast::Expr * visit( const ast::LogicalExpr * node ) override final { |
---|
814 | assert (node->isAnd == ast::LogicalFlag::AndExpr || |
---|
815 | node->isAnd == ast::LogicalFlag::OrExpr ); |
---|
816 | auto expr = visitBaseExpr( node, |
---|
817 | new LogicalExpr( |
---|
818 | get<Expression>().accept1(node->arg1), |
---|
819 | get<Expression>().accept1(node->arg2), |
---|
820 | (node->isAnd == ast::LogicalFlag::AndExpr) |
---|
821 | ) |
---|
822 | ); |
---|
823 | this->node = expr; |
---|
824 | return nullptr; |
---|
825 | } |
---|
826 | |
---|
827 | const ast::Expr * visit( const ast::ConditionalExpr * node ) override final { |
---|
828 | auto expr = visitBaseExpr( node, |
---|
829 | new ConditionalExpr( |
---|
830 | get<Expression>().accept1(node->arg1), |
---|
831 | get<Expression>().accept1(node->arg2), |
---|
832 | get<Expression>().accept1(node->arg3) |
---|
833 | ) |
---|
834 | ); |
---|
835 | this->node = expr; |
---|
836 | return nullptr; |
---|
837 | } |
---|
838 | |
---|
839 | const ast::Expr * visit( const ast::CommaExpr * node ) override final { |
---|
840 | auto expr = visitBaseExpr( node, |
---|
841 | new CommaExpr( |
---|
842 | get<Expression>().accept1(node->arg1), |
---|
843 | get<Expression>().accept1(node->arg2) |
---|
844 | ) |
---|
845 | ); |
---|
846 | this->node = expr; |
---|
847 | return nullptr; |
---|
848 | } |
---|
849 | |
---|
850 | const ast::Expr * visit( const ast::TypeExpr * node ) override final { |
---|
851 | (void)node; |
---|
852 | return nullptr; |
---|
853 | } |
---|
854 | |
---|
855 | const ast::Expr * visit( const ast::AsmExpr * node ) override final { |
---|
856 | (void)node; |
---|
857 | return nullptr; |
---|
858 | } |
---|
859 | |
---|
860 | const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final { |
---|
861 | (void)node; |
---|
862 | return nullptr; |
---|
863 | } |
---|
864 | |
---|
865 | const ast::Expr * visit( const ast::ConstructorExpr * node ) override final { |
---|
866 | (void)node; |
---|
867 | return nullptr; |
---|
868 | } |
---|
869 | |
---|
870 | const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final { |
---|
871 | (void)node; |
---|
872 | return nullptr; |
---|
873 | } |
---|
874 | |
---|
875 | const ast::Expr * visit( const ast::RangeExpr * node ) override final { |
---|
876 | (void)node; |
---|
877 | return nullptr; |
---|
878 | } |
---|
879 | |
---|
880 | const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final { |
---|
881 | (void)node; |
---|
882 | return nullptr; |
---|
883 | } |
---|
884 | |
---|
885 | const ast::Expr * visit( const ast::TupleExpr * node ) override final { |
---|
886 | (void)node; |
---|
887 | return nullptr; |
---|
888 | } |
---|
889 | |
---|
890 | const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final { |
---|
891 | (void)node; |
---|
892 | return nullptr; |
---|
893 | } |
---|
894 | |
---|
895 | const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final { |
---|
896 | (void)node; |
---|
897 | return nullptr; |
---|
898 | } |
---|
899 | |
---|
900 | const ast::Expr * visit( const ast::StmtExpr * node ) override final { |
---|
901 | (void)node; |
---|
902 | return nullptr; |
---|
903 | } |
---|
904 | |
---|
905 | const ast::Expr * visit( const ast::UniqueExpr * node ) override final { |
---|
906 | (void)node; |
---|
907 | return nullptr; |
---|
908 | } |
---|
909 | |
---|
910 | const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final { |
---|
911 | (void)node; |
---|
912 | return nullptr; |
---|
913 | } |
---|
914 | |
---|
915 | const ast::Expr * visit( const ast::InitExpr * node ) override final { |
---|
916 | (void)node; |
---|
917 | return nullptr; |
---|
918 | } |
---|
919 | |
---|
920 | const ast::Expr * visit( const ast::DeletedExpr * node ) override final { |
---|
921 | (void)node; |
---|
922 | return nullptr; |
---|
923 | } |
---|
924 | |
---|
925 | const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final { |
---|
926 | (void)node; |
---|
927 | return nullptr; |
---|
928 | } |
---|
929 | |
---|
930 | const ast::Expr * visit( const ast::GenericExpr * node ) override final { |
---|
931 | (void)node; |
---|
932 | return nullptr; |
---|
933 | } |
---|
934 | |
---|
935 | const ast::Type * visit( const ast::VoidType * node ) override final { |
---|
936 | this->node = new VoidType{ cv( node ) }; |
---|
937 | return nullptr; |
---|
938 | } |
---|
939 | |
---|
940 | const ast::Type * visit( const ast::BasicType * node ) override final { |
---|
941 | this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind }; |
---|
942 | return nullptr; |
---|
943 | } |
---|
944 | |
---|
945 | const ast::Type * visit( const ast::PointerType * node ) override final { |
---|
946 | this->node = new PointerType{ |
---|
947 | cv( node ), |
---|
948 | get<Type>().accept1( node->base ), |
---|
949 | get<Expression>().accept1( node->dimension ), |
---|
950 | node->isVarLen, |
---|
951 | node->isStatic |
---|
952 | }; |
---|
953 | return nullptr; |
---|
954 | } |
---|
955 | |
---|
956 | const ast::Type * visit( const ast::ArrayType * node ) override final { |
---|
957 | this->node = new ArrayType{ |
---|
958 | cv( node ), |
---|
959 | get<Type>().accept1( node->base ), |
---|
960 | get<Expression>().accept1( node->dimension ), |
---|
961 | node->isVarLen, |
---|
962 | node->isStatic |
---|
963 | }; |
---|
964 | return nullptr; |
---|
965 | } |
---|
966 | |
---|
967 | const ast::Type * visit( const ast::ReferenceType * node ) override final { |
---|
968 | this->node = new ReferenceType{ |
---|
969 | cv( node ), |
---|
970 | get<Type>().accept1( node->base ) |
---|
971 | }; |
---|
972 | return nullptr; |
---|
973 | } |
---|
974 | |
---|
975 | const ast::Type * visit( const ast::QualifiedType * node ) override final { |
---|
976 | this->node = new QualifiedType{ |
---|
977 | cv( node ), |
---|
978 | get<Type>().accept1( node->parent ), |
---|
979 | get<Type>().accept1( node->child ) |
---|
980 | }; |
---|
981 | return nullptr; |
---|
982 | } |
---|
983 | |
---|
984 | const ast::Type * visit( const ast::FunctionType * node ) override final { |
---|
985 | auto ty = new FunctionType { cv( node ), node->isVarArgs }; |
---|
986 | ty->returnVals = get<DeclarationWithType>().acceptL( node->returns ); |
---|
987 | ty->parameters = get<DeclarationWithType>().acceptL( node->params ); |
---|
988 | ty->forall = get<TypeDecl>().acceptL( node->forall ); |
---|
989 | this->node = ty; |
---|
990 | return nullptr; |
---|
991 | } |
---|
992 | |
---|
993 | void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) { |
---|
994 | ty->forall = get<TypeDecl>().acceptL( old->forall ); |
---|
995 | ty->parameters = get<Expression>().acceptL( old->params ); |
---|
996 | ty->hoistType = old->hoistType; |
---|
997 | } |
---|
998 | |
---|
999 | const ast::Type * visit( const ast::StructInstType * node ) override final { |
---|
1000 | StructInstType * ty; |
---|
1001 | if ( node->base ) { |
---|
1002 | ty = new StructInstType{ |
---|
1003 | cv( node ), |
---|
1004 | get<StructDecl>().accept1( node->base ), |
---|
1005 | get<Attribute>().acceptL( node->attributes ) |
---|
1006 | }; |
---|
1007 | } else { |
---|
1008 | ty = new StructInstType{ |
---|
1009 | cv( node ), |
---|
1010 | node->name, |
---|
1011 | get<Attribute>().acceptL( node->attributes ) |
---|
1012 | }; |
---|
1013 | } |
---|
1014 | postvisit( node, ty ); |
---|
1015 | this->node = ty; |
---|
1016 | return nullptr; |
---|
1017 | } |
---|
1018 | |
---|
1019 | const ast::Type * visit( const ast::UnionInstType * node ) override final { |
---|
1020 | UnionInstType * ty; |
---|
1021 | if ( node->base ) { |
---|
1022 | ty = new UnionInstType{ |
---|
1023 | cv( node ), |
---|
1024 | get<UnionDecl>().accept1( node->base ), |
---|
1025 | get<Attribute>().acceptL( node->attributes ) |
---|
1026 | }; |
---|
1027 | } else { |
---|
1028 | ty = new UnionInstType{ |
---|
1029 | cv( node ), |
---|
1030 | node->name, |
---|
1031 | get<Attribute>().acceptL( node->attributes ) |
---|
1032 | }; |
---|
1033 | } |
---|
1034 | postvisit( node, ty ); |
---|
1035 | this->node = ty; |
---|
1036 | return nullptr; |
---|
1037 | } |
---|
1038 | |
---|
1039 | const ast::Type * visit( const ast::EnumInstType * node ) override final { |
---|
1040 | EnumInstType * ty; |
---|
1041 | if ( node->base ) { |
---|
1042 | ty = new EnumInstType{ |
---|
1043 | cv( node ), |
---|
1044 | get<EnumDecl>().accept1( node->base ), |
---|
1045 | get<Attribute>().acceptL( node->attributes ) |
---|
1046 | }; |
---|
1047 | } else { |
---|
1048 | ty = new EnumInstType{ |
---|
1049 | cv( node ), |
---|
1050 | node->name, |
---|
1051 | get<Attribute>().acceptL( node->attributes ) |
---|
1052 | }; |
---|
1053 | } |
---|
1054 | postvisit( node, ty ); |
---|
1055 | this->node = ty; |
---|
1056 | return nullptr; |
---|
1057 | } |
---|
1058 | |
---|
1059 | const ast::Type * visit( const ast::TraitInstType * node ) override final { |
---|
1060 | TraitInstType * ty; |
---|
1061 | if ( node->base ) { |
---|
1062 | ty = new TraitInstType{ |
---|
1063 | cv( node ), |
---|
1064 | get<TraitDecl>().accept1( node->base ), |
---|
1065 | get<Attribute>().acceptL( node->attributes ) |
---|
1066 | }; |
---|
1067 | } else { |
---|
1068 | ty = new TraitInstType{ |
---|
1069 | cv( node ), |
---|
1070 | node->name, |
---|
1071 | get<Attribute>().acceptL( node->attributes ) |
---|
1072 | }; |
---|
1073 | } |
---|
1074 | postvisit( node, ty ); |
---|
1075 | this->node = ty; |
---|
1076 | return nullptr; |
---|
1077 | } |
---|
1078 | |
---|
1079 | const ast::Type * visit( const ast::TypeInstType * node ) override final { |
---|
1080 | TypeInstType * ty; |
---|
1081 | if ( node->base ) { |
---|
1082 | ty = new TypeInstType{ |
---|
1083 | cv( node ), |
---|
1084 | node->name, |
---|
1085 | get<TypeDecl>().accept1( node->base ), |
---|
1086 | get<Attribute>().acceptL( node->attributes ) |
---|
1087 | }; |
---|
1088 | } else { |
---|
1089 | ty = new TypeInstType{ |
---|
1090 | cv( node ), |
---|
1091 | node->name, |
---|
1092 | node->kind == ast::TypeVar::Ftype, |
---|
1093 | get<Attribute>().acceptL( node->attributes ) |
---|
1094 | }; |
---|
1095 | } |
---|
1096 | postvisit( node, ty ); |
---|
1097 | this->node = ty; |
---|
1098 | return nullptr; |
---|
1099 | } |
---|
1100 | |
---|
1101 | const ast::Type * visit( const ast::TupleType * node ) override final { |
---|
1102 | (void)node; |
---|
1103 | return nullptr; |
---|
1104 | } |
---|
1105 | |
---|
1106 | const ast::Type * visit( const ast::TypeofType * node ) override final { |
---|
1107 | (void)node; |
---|
1108 | return nullptr; |
---|
1109 | } |
---|
1110 | |
---|
1111 | const ast::Type * visit( const ast::VarArgsType * node ) override final { |
---|
1112 | (void)node; |
---|
1113 | return nullptr; |
---|
1114 | } |
---|
1115 | |
---|
1116 | const ast::Type * visit( const ast::ZeroType * node ) override final { |
---|
1117 | (void)node; |
---|
1118 | return nullptr; |
---|
1119 | } |
---|
1120 | |
---|
1121 | const ast::Type * visit( const ast::OneType * node ) override final { |
---|
1122 | (void)node; |
---|
1123 | return nullptr; |
---|
1124 | } |
---|
1125 | |
---|
1126 | const ast::Type * visit( const ast::GlobalScopeType * node ) override final { |
---|
1127 | (void)node; |
---|
1128 | return nullptr; |
---|
1129 | } |
---|
1130 | |
---|
1131 | const ast::Designation * visit( const ast::Designation * node ) override final { |
---|
1132 | (void)node; |
---|
1133 | return nullptr; |
---|
1134 | } |
---|
1135 | |
---|
1136 | const ast::Init * visit( const ast::SingleInit * node ) override final { |
---|
1137 | (void)node; |
---|
1138 | return nullptr; |
---|
1139 | } |
---|
1140 | |
---|
1141 | const ast::Init * visit( const ast::ListInit * node ) override final { |
---|
1142 | (void)node; |
---|
1143 | return nullptr; |
---|
1144 | } |
---|
1145 | |
---|
1146 | const ast::Init * visit( const ast::ConstructorInit * node ) override final { |
---|
1147 | (void)node; |
---|
1148 | return nullptr; |
---|
1149 | } |
---|
1150 | |
---|
1151 | const ast::Attribute * visit( const ast::Attribute * node ) override final { |
---|
1152 | (void)node; |
---|
1153 | return nullptr; |
---|
1154 | } |
---|
1155 | |
---|
1156 | const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final { |
---|
1157 | (void)node; |
---|
1158 | return nullptr; |
---|
1159 | } |
---|
1160 | }; |
---|
1161 | |
---|
1162 | std::list< Declaration * > convert( std::list< ast::ptr< ast::Decl > > && translationUnit ) { |
---|
1163 | ConverterNewToOld c; |
---|
1164 | std::list< Declaration * > decls; |
---|
1165 | for(auto d : translationUnit) { |
---|
1166 | decls.emplace_back( c.decl( d ) ); |
---|
1167 | delete d; |
---|
1168 | } |
---|
1169 | return decls; |
---|
1170 | } |
---|
1171 | |
---|
1172 | //================================================================================================ |
---|
1173 | |
---|
1174 | class ConverterOldToNew : public Visitor { |
---|
1175 | public: |
---|
1176 | ast::Decl * decl() { |
---|
1177 | return strict_dynamic_cast< ast::Decl * >( node ); |
---|
1178 | } |
---|
1179 | private: |
---|
1180 | /// conversion output |
---|
1181 | ast::Node * node; |
---|
1182 | /// cache of nodes that might be referenced by readonly<> for de-duplication |
---|
1183 | std::unordered_map< BaseSyntaxNode *, ast::Node * > cache; |
---|
1184 | |
---|
1185 | // Local Utilities: |
---|
1186 | |
---|
1187 | template<typename NewT, typename OldT> |
---|
1188 | NewT * getAccept1( OldT old ) { |
---|
1189 | if ( ! old ) return nullptr; |
---|
1190 | old->accept(*this); |
---|
1191 | return strict_dynamic_cast< NewT * >( node ); |
---|
1192 | } |
---|
1193 | |
---|
1194 | # define GET_ACCEPT_1(child, type) \ |
---|
1195 | getAccept1< ast::type, decltype( old->child ) >( old->child ) |
---|
1196 | |
---|
1197 | template<typename NewT, typename OldC> |
---|
1198 | std::vector< ast::ptr<NewT> > getAcceptV( OldC& old ) { |
---|
1199 | std::vector< ast::ptr<NewT> > ret; |
---|
1200 | ret.reserve( old.size() ); |
---|
1201 | for ( auto a : old ) { |
---|
1202 | a->accept( *this ); |
---|
1203 | ret.emplace_back( strict_dynamic_cast< NewT * >(node) ); |
---|
1204 | } |
---|
1205 | return ret; |
---|
1206 | } |
---|
1207 | |
---|
1208 | # define GET_ACCEPT_V(child, type) \ |
---|
1209 | getAcceptV< ast::type, decltype( old->child ) >( old->child ) |
---|
1210 | |
---|
1211 | ast::Label make_label(Label* old) { |
---|
1212 | return ast::Label( |
---|
1213 | old->labelled->location, |
---|
1214 | old->name, |
---|
1215 | GET_ACCEPT_V(attributes, Attribute) |
---|
1216 | ); |
---|
1217 | } |
---|
1218 | |
---|
1219 | template<template <class...> class C> |
---|
1220 | C<ast::Label> make_labels(C<Label> olds) { |
---|
1221 | C<ast::Label> ret; |
---|
1222 | for (auto oldn : olds) { |
---|
1223 | ret.push_back( make_label( &oldn ) ); |
---|
1224 | } |
---|
1225 | return ret; |
---|
1226 | } |
---|
1227 | |
---|
1228 | # define GET_LABELS_V(labels) \ |
---|
1229 | to<std::vector>::from( make_labels( std::move( labels ) ) ) |
---|
1230 | |
---|
1231 | static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; } |
---|
1232 | |
---|
1233 | /// returns true and sets `node` if in cache |
---|
1234 | bool inCache( BaseSyntaxNode * old ) { |
---|
1235 | auto it = cache.find( old ); |
---|
1236 | if ( it == cache.end() ) return false; |
---|
1237 | node = it->second; |
---|
1238 | return true; |
---|
1239 | } |
---|
1240 | |
---|
1241 | // Now all the visit functions: |
---|
1242 | |
---|
1243 | virtual void visit( ObjectDecl * old ) override final { |
---|
1244 | if ( inCache( old ) ) return; |
---|
1245 | auto decl = new ast::ObjectDecl( |
---|
1246 | old->location, |
---|
1247 | old->name, |
---|
1248 | GET_ACCEPT_1(type, Type), |
---|
1249 | GET_ACCEPT_1(init, Init), |
---|
1250 | { old->get_storageClasses().val }, |
---|
1251 | { old->linkage.val }, |
---|
1252 | GET_ACCEPT_1(bitfieldWidth, Expr), |
---|
1253 | GET_ACCEPT_V(attributes, Attribute), |
---|
1254 | { old->get_funcSpec().val } |
---|
1255 | ); |
---|
1256 | decl->scopeLevel = old->scopeLevel; |
---|
1257 | decl->mangleName = old->mangleName; |
---|
1258 | decl->isDeleted = old->isDeleted; |
---|
1259 | decl->uniqueId = old->uniqueId; |
---|
1260 | decl->extension = old->extension; |
---|
1261 | cache.emplace( old, decl ); |
---|
1262 | |
---|
1263 | this->node = decl; |
---|
1264 | } |
---|
1265 | |
---|
1266 | virtual void visit( FunctionDecl * old ) override final { |
---|
1267 | if ( inCache( old ) ) return; |
---|
1268 | // TODO |
---|
1269 | auto decl = (ast::FunctionDecl *)nullptr; |
---|
1270 | cache.emplace( old, decl ); |
---|
1271 | } |
---|
1272 | |
---|
1273 | virtual void visit( StructDecl * old ) override final { |
---|
1274 | if ( inCache( old ) ) return; |
---|
1275 | auto decl = new ast::StructDecl( |
---|
1276 | old->location, |
---|
1277 | old->name, |
---|
1278 | old->kind, |
---|
1279 | GET_ACCEPT_V(attributes, Attribute), |
---|
1280 | { old->linkage.val } |
---|
1281 | ); |
---|
1282 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl); |
---|
1283 | decl->body = old->body; |
---|
1284 | decl->params = GET_ACCEPT_V(parameters, TypeDecl); |
---|
1285 | decl->members = GET_ACCEPT_V(members, Decl); |
---|
1286 | decl->extension = old->extension; |
---|
1287 | decl->uniqueId = old->uniqueId; |
---|
1288 | decl->storage = { old->storageClasses.val }; |
---|
1289 | cache.emplace( old, decl ); |
---|
1290 | |
---|
1291 | this->node = decl; |
---|
1292 | } |
---|
1293 | |
---|
1294 | virtual void visit( UnionDecl * old ) override final { |
---|
1295 | if ( inCache( old ) ) return; |
---|
1296 | auto decl = new ast::UnionDecl( |
---|
1297 | old->location, |
---|
1298 | old->name, |
---|
1299 | GET_ACCEPT_V(attributes, Attribute), |
---|
1300 | { old->linkage.val } |
---|
1301 | ); |
---|
1302 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl); |
---|
1303 | decl->body = old->body; |
---|
1304 | decl->params = GET_ACCEPT_V(parameters, TypeDecl); |
---|
1305 | decl->members = GET_ACCEPT_V(members, Decl); |
---|
1306 | decl->extension = old->extension; |
---|
1307 | decl->uniqueId = old->uniqueId; |
---|
1308 | decl->storage = { old->storageClasses.val }; |
---|
1309 | cache.emplace( old, decl ); |
---|
1310 | |
---|
1311 | this->node = decl; |
---|
1312 | } |
---|
1313 | |
---|
1314 | virtual void visit( EnumDecl * old ) override final { |
---|
1315 | if ( inCache( old ) ) return; |
---|
1316 | auto decl = new ast::UnionDecl( |
---|
1317 | old->location, |
---|
1318 | old->name, |
---|
1319 | GET_ACCEPT_V(attributes, Attribute), |
---|
1320 | { old->linkage.val } |
---|
1321 | ); |
---|
1322 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl); |
---|
1323 | decl->body = old->body; |
---|
1324 | decl->params = GET_ACCEPT_V(parameters, TypeDecl); |
---|
1325 | decl->members = GET_ACCEPT_V(members, Decl); |
---|
1326 | decl->extension = old->extension; |
---|
1327 | decl->uniqueId = old->uniqueId; |
---|
1328 | decl->storage = { old->storageClasses.val }; |
---|
1329 | cache.emplace( old, decl ); |
---|
1330 | |
---|
1331 | this->node = decl; |
---|
1332 | } |
---|
1333 | |
---|
1334 | virtual void visit( TraitDecl * old ) override final { |
---|
1335 | if ( inCache( old ) ) return; |
---|
1336 | auto decl = new ast::UnionDecl( |
---|
1337 | old->location, |
---|
1338 | old->name, |
---|
1339 | GET_ACCEPT_V(attributes, Attribute), |
---|
1340 | { old->linkage.val } |
---|
1341 | ); |
---|
1342 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl); |
---|
1343 | decl->body = old->body; |
---|
1344 | decl->params = GET_ACCEPT_V(parameters, TypeDecl); |
---|
1345 | decl->members = GET_ACCEPT_V(members, Decl); |
---|
1346 | decl->extension = old->extension; |
---|
1347 | decl->uniqueId = old->uniqueId; |
---|
1348 | decl->storage = { old->storageClasses.val }; |
---|
1349 | cache.emplace( old, decl ); |
---|
1350 | |
---|
1351 | this->node = decl; |
---|
1352 | } |
---|
1353 | |
---|
1354 | virtual void visit( TypeDecl * old ) override final { |
---|
1355 | if ( inCache( old ) ) return; |
---|
1356 | // TODO |
---|
1357 | auto decl = (ast::TypeDecl *)nullptr; |
---|
1358 | cache.emplace( old, decl ); |
---|
1359 | } |
---|
1360 | |
---|
1361 | virtual void visit( TypedefDecl * old ) override final { |
---|
1362 | auto decl = new ast::TypedefDecl( |
---|
1363 | old->location, |
---|
1364 | old->name, |
---|
1365 | { old->storageClasses.val }, |
---|
1366 | GET_ACCEPT_1(base, Type), |
---|
1367 | { old->linkage.val } |
---|
1368 | ); |
---|
1369 | decl->assertions = GET_ACCEPT_V(assertions, DeclWithType); |
---|
1370 | decl->params = GET_ACCEPT_V(parameters, TypeDecl); |
---|
1371 | decl->extension = old->extension; |
---|
1372 | decl->uniqueId = old->uniqueId; |
---|
1373 | decl->storage = { old->storageClasses.val }; |
---|
1374 | |
---|
1375 | this->node = decl; |
---|
1376 | } |
---|
1377 | |
---|
1378 | virtual void visit( AsmDecl * ) override final { |
---|
1379 | |
---|
1380 | } |
---|
1381 | |
---|
1382 | virtual void visit( StaticAssertDecl * ) override final { |
---|
1383 | |
---|
1384 | } |
---|
1385 | |
---|
1386 | virtual void visit( CompoundStmt * old ) override final { |
---|
1387 | auto stmt = new ast::CompoundStmt( |
---|
1388 | old->location, |
---|
1389 | to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ), |
---|
1390 | GET_LABELS_V(old->labels) |
---|
1391 | ); |
---|
1392 | |
---|
1393 | this->node = stmt; |
---|
1394 | } |
---|
1395 | |
---|
1396 | virtual void visit( ExprStmt * old ) override final { |
---|
1397 | this->node = new ast::ExprStmt( |
---|
1398 | old->location, |
---|
1399 | GET_ACCEPT_1(expr, Expr), |
---|
1400 | GET_LABELS_V(old->labels) |
---|
1401 | ); |
---|
1402 | } |
---|
1403 | |
---|
1404 | virtual void visit( AsmStmt * old ) override final { |
---|
1405 | this->node = new ast::AsmStmt( |
---|
1406 | old->location, |
---|
1407 | old->voltile, |
---|
1408 | GET_ACCEPT_1(instruction, Expr), |
---|
1409 | GET_ACCEPT_V(output, Expr), |
---|
1410 | GET_ACCEPT_V(input, Expr), |
---|
1411 | GET_ACCEPT_V(clobber, ConstantExpr), |
---|
1412 | GET_LABELS_V(old->gotolabels), |
---|
1413 | GET_LABELS_V(old->labels) |
---|
1414 | ); |
---|
1415 | } |
---|
1416 | |
---|
1417 | virtual void visit( DirectiveStmt * old ) override final { |
---|
1418 | this->node = new ast::DirectiveStmt( |
---|
1419 | old->location, |
---|
1420 | old->directive, |
---|
1421 | GET_LABELS_V(old->labels) |
---|
1422 | ); |
---|
1423 | } |
---|
1424 | |
---|
1425 | virtual void visit( IfStmt * old ) override final { |
---|
1426 | this->node = new ast::IfStmt( |
---|
1427 | old->location, |
---|
1428 | GET_ACCEPT_1(condition, Expr), |
---|
1429 | GET_ACCEPT_1(thenPart, Stmt), |
---|
1430 | GET_ACCEPT_1(elsePart, Stmt), |
---|
1431 | GET_ACCEPT_V(initialization, Stmt), |
---|
1432 | GET_LABELS_V(old->labels) |
---|
1433 | ); |
---|
1434 | } |
---|
1435 | |
---|
1436 | virtual void visit( SwitchStmt * old ) override final { |
---|
1437 | this->node = new ast::SwitchStmt( |
---|
1438 | old->location, |
---|
1439 | GET_ACCEPT_1(condition, Expr), |
---|
1440 | GET_ACCEPT_V(statements, Stmt), |
---|
1441 | GET_LABELS_V(old->labels) |
---|
1442 | ); |
---|
1443 | } |
---|
1444 | |
---|
1445 | virtual void visit( CaseStmt * old ) override final { |
---|
1446 | this->node = new ast::CaseStmt( |
---|
1447 | old->location, |
---|
1448 | GET_ACCEPT_1(condition, Expr), |
---|
1449 | GET_ACCEPT_V(stmts, Stmt), |
---|
1450 | GET_LABELS_V(old->labels) |
---|
1451 | ); |
---|
1452 | } |
---|
1453 | |
---|
1454 | virtual void visit( WhileStmt * old ) override final { |
---|
1455 | this->node = new ast::WhileStmt( |
---|
1456 | old->location, |
---|
1457 | GET_ACCEPT_1(condition, Expr), |
---|
1458 | GET_ACCEPT_1(body, Stmt), |
---|
1459 | GET_ACCEPT_V(initialization, Stmt), |
---|
1460 | old->isDoWhile, |
---|
1461 | GET_LABELS_V(old->labels) |
---|
1462 | ); |
---|
1463 | } |
---|
1464 | |
---|
1465 | virtual void visit( ForStmt * old ) override final { |
---|
1466 | this->node = new ast::ForStmt( |
---|
1467 | old->location, |
---|
1468 | GET_ACCEPT_V(initialization, Stmt), |
---|
1469 | GET_ACCEPT_1(condition, Expr), |
---|
1470 | GET_ACCEPT_1(increment, Expr), |
---|
1471 | GET_ACCEPT_1(body, Stmt), |
---|
1472 | GET_LABELS_V(old->labels) |
---|
1473 | ); |
---|
1474 | } |
---|
1475 | |
---|
1476 | virtual void visit( BranchStmt * old ) override final { |
---|
1477 | if (old->computedTarget) { |
---|
1478 | this->node = new ast::BranchStmt( |
---|
1479 | old->location, |
---|
1480 | GET_ACCEPT_1(computedTarget, Expr), |
---|
1481 | GET_LABELS_V(old->labels) |
---|
1482 | ); |
---|
1483 | } else { |
---|
1484 | ast::BranchStmt::Kind kind; |
---|
1485 | switch (old->type) { |
---|
1486 | #define CASE(n) \ |
---|
1487 | case BranchStmt::n: \ |
---|
1488 | kind = ast::BranchStmt::n; \ |
---|
1489 | break |
---|
1490 | CASE(Goto); |
---|
1491 | CASE(Break); |
---|
1492 | CASE(Continue); |
---|
1493 | CASE(FallThrough); |
---|
1494 | CASE(FallThroughDefault); |
---|
1495 | #undef CASE |
---|
1496 | default: |
---|
1497 | assertf(false, "Invalid BranchStmt::Type %d\n", old->type); |
---|
1498 | } |
---|
1499 | |
---|
1500 | Label label = old->originalTarget; |
---|
1501 | auto stmt = new ast::BranchStmt( |
---|
1502 | old->location, |
---|
1503 | kind, |
---|
1504 | make_label(&label), |
---|
1505 | GET_LABELS_V(old->labels) |
---|
1506 | ); |
---|
1507 | stmt->target = make_label(&old->target); |
---|
1508 | this->node = stmt; |
---|
1509 | } |
---|
1510 | } |
---|
1511 | |
---|
1512 | virtual void visit( ReturnStmt * old ) override final { |
---|
1513 | this->node = new ast::ReturnStmt( |
---|
1514 | old->location, |
---|
1515 | GET_ACCEPT_1(expr, Expr), |
---|
1516 | GET_LABELS_V(old->labels) |
---|
1517 | ); |
---|
1518 | } |
---|
1519 | |
---|
1520 | virtual void visit( ThrowStmt * old ) override final { |
---|
1521 | ast::ThrowStmt::Kind kind; |
---|
1522 | switch (old->kind) { |
---|
1523 | case ThrowStmt::Terminate: |
---|
1524 | kind = ast::ThrowStmt::Terminate; |
---|
1525 | break; |
---|
1526 | case ThrowStmt::Resume: |
---|
1527 | kind = ast::ThrowStmt::Resume; |
---|
1528 | break; |
---|
1529 | default: |
---|
1530 | assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind); |
---|
1531 | } |
---|
1532 | |
---|
1533 | this->node = new ast::ThrowStmt( |
---|
1534 | old->location, |
---|
1535 | kind, |
---|
1536 | GET_ACCEPT_1(expr, Expr), |
---|
1537 | GET_ACCEPT_1(target, Expr), |
---|
1538 | GET_LABELS_V(old->labels) |
---|
1539 | ); |
---|
1540 | } |
---|
1541 | |
---|
1542 | virtual void visit( TryStmt * old ) override final { |
---|
1543 | this->node = new ast::TryStmt( |
---|
1544 | old->location, |
---|
1545 | GET_ACCEPT_1(block, CompoundStmt), |
---|
1546 | GET_ACCEPT_V(handlers, CatchStmt), |
---|
1547 | GET_ACCEPT_1(finallyBlock, FinallyStmt), |
---|
1548 | GET_LABELS_V(old->labels) |
---|
1549 | ); |
---|
1550 | } |
---|
1551 | |
---|
1552 | virtual void visit( CatchStmt * old ) override final { |
---|
1553 | ast::CatchStmt::Kind kind; |
---|
1554 | switch (old->kind) { |
---|
1555 | case CatchStmt::Terminate: |
---|
1556 | kind = ast::CatchStmt::Terminate; |
---|
1557 | break; |
---|
1558 | case CatchStmt::Resume: |
---|
1559 | kind = ast::CatchStmt::Resume; |
---|
1560 | break; |
---|
1561 | default: |
---|
1562 | assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind); |
---|
1563 | } |
---|
1564 | |
---|
1565 | this->node = new ast::CatchStmt( |
---|
1566 | old->location, |
---|
1567 | kind, |
---|
1568 | GET_ACCEPT_1(decl, Decl), |
---|
1569 | GET_ACCEPT_1(cond, Expr), |
---|
1570 | GET_ACCEPT_1(body, Stmt), |
---|
1571 | GET_LABELS_V(old->labels) |
---|
1572 | ); |
---|
1573 | } |
---|
1574 | |
---|
1575 | virtual void visit( FinallyStmt * old ) override final { |
---|
1576 | this->node = new ast::FinallyStmt( |
---|
1577 | old->location, |
---|
1578 | GET_ACCEPT_1(block, CompoundStmt), |
---|
1579 | GET_LABELS_V(old->labels) |
---|
1580 | ); |
---|
1581 | } |
---|
1582 | |
---|
1583 | virtual void visit( WaitForStmt * old ) override final { |
---|
1584 | ast::WaitForStmt * stmt = new ast::WaitForStmt( |
---|
1585 | old->location, |
---|
1586 | GET_LABELS_V(old->labels) |
---|
1587 | ); |
---|
1588 | |
---|
1589 | stmt->clauses.reserve( old->clauses.size() ); |
---|
1590 | for (size_t i = 0 ; i < old->clauses.size() ; ++i) { |
---|
1591 | stmt->clauses.push_back({ |
---|
1592 | ast::WaitForStmt::Target{ |
---|
1593 | GET_ACCEPT_1(clauses[i].target.function, Expr), |
---|
1594 | GET_ACCEPT_V(clauses[i].target.arguments, Expr) |
---|
1595 | }, |
---|
1596 | GET_ACCEPT_1(clauses[i].statement, Stmt), |
---|
1597 | GET_ACCEPT_1(clauses[i].condition, Expr) |
---|
1598 | }); |
---|
1599 | } |
---|
1600 | stmt->timeout = { |
---|
1601 | GET_ACCEPT_1(timeout.time, Expr), |
---|
1602 | GET_ACCEPT_1(timeout.statement, Stmt), |
---|
1603 | GET_ACCEPT_1(timeout.condition, Expr), |
---|
1604 | }; |
---|
1605 | stmt->orElse = { |
---|
1606 | GET_ACCEPT_1(timeout.statement, Stmt), |
---|
1607 | GET_ACCEPT_1(timeout.condition, Expr), |
---|
1608 | }; |
---|
1609 | |
---|
1610 | this->node = stmt; |
---|
1611 | } |
---|
1612 | |
---|
1613 | virtual void visit( WithStmt * old ) override final { |
---|
1614 | this->node = new ast::WithStmt( |
---|
1615 | old->location, |
---|
1616 | GET_ACCEPT_V(exprs, Expr), |
---|
1617 | GET_ACCEPT_1(stmt, Stmt), |
---|
1618 | GET_LABELS_V(old->labels) |
---|
1619 | ); |
---|
1620 | } |
---|
1621 | |
---|
1622 | virtual void visit( NullStmt * old ) override final { |
---|
1623 | this->node = new ast::NullStmt( |
---|
1624 | old->location, |
---|
1625 | GET_LABELS_V(old->labels) |
---|
1626 | ); |
---|
1627 | } |
---|
1628 | |
---|
1629 | virtual void visit( DeclStmt * old ) override final { |
---|
1630 | this->node = new ast::DeclStmt( |
---|
1631 | old->location, |
---|
1632 | GET_ACCEPT_1(decl, Decl), |
---|
1633 | GET_LABELS_V(old->labels) |
---|
1634 | ); |
---|
1635 | } |
---|
1636 | |
---|
1637 | virtual void visit( ImplicitCtorDtorStmt * old ) override final { |
---|
1638 | this->node = new ast::ImplicitCtorDtorStmt( |
---|
1639 | old->location, |
---|
1640 | GET_ACCEPT_1(callStmt, Stmt), |
---|
1641 | GET_LABELS_V(old->labels) |
---|
1642 | ); |
---|
1643 | } |
---|
1644 | |
---|
1645 | ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) { |
---|
1646 | |
---|
1647 | ast::TypeSubstitution *rslt = new ast::TypeSubstitution(); |
---|
1648 | |
---|
1649 | for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) { |
---|
1650 | rslt->add( old_i->first, |
---|
1651 | getAccept1<ast::Type>(old_i->second) ); |
---|
1652 | } |
---|
1653 | |
---|
1654 | for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) { |
---|
1655 | rslt->addVar( old_i->first, |
---|
1656 | getAccept1<ast::Expr>(old_i->second) ); |
---|
1657 | } |
---|
1658 | |
---|
1659 | return rslt; |
---|
1660 | } |
---|
1661 | |
---|
1662 | void convertInferUnion(ast::Expr::InferUnion &newInferred, |
---|
1663 | const std::map<UniqueId,ParamEntry> &oldInferParams, |
---|
1664 | const std::vector<UniqueId> &oldResnSlots) { |
---|
1665 | |
---|
1666 | assert( oldInferParams.empty() || oldResnSlots.empty() ); |
---|
1667 | assert( newInferred.mode == ast::Expr::InferUnion::Empty ); |
---|
1668 | |
---|
1669 | if ( !oldInferParams.empty() ) { |
---|
1670 | ast::InferredParams &tgt = newInferred.inferParams(); |
---|
1671 | for (auto old : oldInferParams) { |
---|
1672 | tgt[old.first] = ast::ParamEntry( |
---|
1673 | old.second.decl, |
---|
1674 | getAccept1<ast::Type>(old.second.actualType), |
---|
1675 | getAccept1<ast::Type>(old.second.formalType), |
---|
1676 | getAccept1<ast::Expr>(old.second.expr) |
---|
1677 | ); |
---|
1678 | } |
---|
1679 | } else if ( !oldResnSlots.empty() ) { |
---|
1680 | ast::ResnSlots &tgt = newInferred.resnSlots(); |
---|
1681 | for (auto old : oldResnSlots) { |
---|
1682 | tgt.push_back(old); |
---|
1683 | } |
---|
1684 | } |
---|
1685 | } |
---|
1686 | |
---|
1687 | ast::Expr * visitBaseExpr(Expression * old, ast::Expr * nw) { |
---|
1688 | |
---|
1689 | nw->result = GET_ACCEPT_1(result, Type); |
---|
1690 | nw->env = convertTypeSubstitution(old->env); |
---|
1691 | |
---|
1692 | nw->extension = old->extension; |
---|
1693 | convertInferUnion(nw->inferred, old->inferParams, old->resnSlots); |
---|
1694 | |
---|
1695 | return nw; |
---|
1696 | } |
---|
1697 | |
---|
1698 | virtual void visit( ApplicationExpr * old ) override final { |
---|
1699 | this->node = visitBaseExpr( old, |
---|
1700 | new ast::ApplicationExpr( |
---|
1701 | old->location, |
---|
1702 | GET_ACCEPT_1(function, Expr), |
---|
1703 | GET_ACCEPT_V(args, Expr) |
---|
1704 | ) |
---|
1705 | ); |
---|
1706 | } |
---|
1707 | |
---|
1708 | virtual void visit( UntypedExpr * old ) override final { |
---|
1709 | this->node = visitBaseExpr( old, |
---|
1710 | new ast::UntypedExpr( |
---|
1711 | old->location, |
---|
1712 | GET_ACCEPT_1(function, Expr), |
---|
1713 | GET_ACCEPT_V(args, Expr) |
---|
1714 | ) |
---|
1715 | ); |
---|
1716 | } |
---|
1717 | |
---|
1718 | virtual void visit( NameExpr * old ) override final { |
---|
1719 | this->node = visitBaseExpr( old, |
---|
1720 | new ast::NameExpr( |
---|
1721 | old->location, |
---|
1722 | old->get_name() |
---|
1723 | ) |
---|
1724 | ); |
---|
1725 | } |
---|
1726 | |
---|
1727 | virtual void visit( CastExpr * old ) override final { |
---|
1728 | this->node = visitBaseExpr( old, |
---|
1729 | new ast::CastExpr( |
---|
1730 | old->location, |
---|
1731 | GET_ACCEPT_1(arg, Expr), |
---|
1732 | old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast |
---|
1733 | ) |
---|
1734 | ); |
---|
1735 | } |
---|
1736 | |
---|
1737 | virtual void visit( KeywordCastExpr * old) override final { |
---|
1738 | ast::KeywordCastExpr::Target castTarget = ast::KeywordCastExpr::NUMBER_OF_TARGETS; |
---|
1739 | switch (old->target) { |
---|
1740 | case KeywordCastExpr::Coroutine: |
---|
1741 | castTarget = ast::KeywordCastExpr::Coroutine; |
---|
1742 | break; |
---|
1743 | case KeywordCastExpr::Thread: |
---|
1744 | castTarget = ast::KeywordCastExpr::Thread; |
---|
1745 | break; |
---|
1746 | case KeywordCastExpr::Monitor: |
---|
1747 | castTarget = ast::KeywordCastExpr::Monitor; |
---|
1748 | break; |
---|
1749 | default: |
---|
1750 | break; |
---|
1751 | } |
---|
1752 | assert ( castTarget < ast::KeywordCastExpr::NUMBER_OF_TARGETS ); |
---|
1753 | this->node = visitBaseExpr( old, |
---|
1754 | new ast::KeywordCastExpr( |
---|
1755 | old->location, |
---|
1756 | GET_ACCEPT_1(arg, Expr), |
---|
1757 | castTarget |
---|
1758 | ) |
---|
1759 | ); |
---|
1760 | } |
---|
1761 | |
---|
1762 | virtual void visit( VirtualCastExpr * old ) override final { |
---|
1763 | this->node = visitBaseExpr( old, |
---|
1764 | new ast::VirtualCastExpr( |
---|
1765 | old->location, |
---|
1766 | GET_ACCEPT_1(arg, Expr), |
---|
1767 | nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr |
---|
1768 | ) |
---|
1769 | ); |
---|
1770 | } |
---|
1771 | |
---|
1772 | virtual void visit( AddressExpr * old ) override final { |
---|
1773 | this->node = visitBaseExpr( old, |
---|
1774 | new ast::AddressExpr( |
---|
1775 | old->location, |
---|
1776 | GET_ACCEPT_1(arg, Expr) |
---|
1777 | ) |
---|
1778 | ); |
---|
1779 | } |
---|
1780 | |
---|
1781 | virtual void visit( LabelAddressExpr * old ) override final { |
---|
1782 | this->node = visitBaseExpr( old, |
---|
1783 | new ast::LabelAddressExpr( |
---|
1784 | old->location, |
---|
1785 | make_label(&old->arg) |
---|
1786 | ) |
---|
1787 | ); |
---|
1788 | } |
---|
1789 | |
---|
1790 | virtual void visit( UntypedMemberExpr * old ) override final { |
---|
1791 | this->node = visitBaseExpr( old, |
---|
1792 | new ast::UntypedMemberExpr( |
---|
1793 | old->location, |
---|
1794 | GET_ACCEPT_1(member, Expr), |
---|
1795 | GET_ACCEPT_1(aggregate, Expr) |
---|
1796 | ) |
---|
1797 | ); |
---|
1798 | } |
---|
1799 | |
---|
1800 | virtual void visit( MemberExpr * old ) override final { |
---|
1801 | this->node = visitBaseExpr( old, |
---|
1802 | new ast::MemberExpr( |
---|
1803 | old->location, |
---|
1804 | inCache(old->member) ? |
---|
1805 | dynamic_cast<ast::DeclWithType *>(this->node) : |
---|
1806 | GET_ACCEPT_1(member, DeclWithType), |
---|
1807 | GET_ACCEPT_1(aggregate, Expr) |
---|
1808 | ) |
---|
1809 | ); |
---|
1810 | } |
---|
1811 | |
---|
1812 | virtual void visit( VariableExpr * old ) override final { |
---|
1813 | this->node = visitBaseExpr( old, |
---|
1814 | new ast::VariableExpr( |
---|
1815 | old->location, |
---|
1816 | inCache(old->var) ? |
---|
1817 | dynamic_cast<ast::DeclWithType *>(this->node) : |
---|
1818 | GET_ACCEPT_1(var, DeclWithType) |
---|
1819 | ) |
---|
1820 | ); |
---|
1821 | } |
---|
1822 | |
---|
1823 | bool isIntlikeConstantType(const Type *t) { |
---|
1824 | if ( const BasicType * basicType = dynamic_cast< const BasicType * >( t ) ) { |
---|
1825 | if ( basicType->isInteger() ) { |
---|
1826 | return true; |
---|
1827 | } |
---|
1828 | } else if ( dynamic_cast< const OneType * >( t ) ) { |
---|
1829 | return true; |
---|
1830 | } else if ( dynamic_cast< const ZeroType * >( t ) ) { |
---|
1831 | return true; |
---|
1832 | } else if ( dynamic_cast< const PointerType * >( t ) ) { |
---|
1833 | // null pointer constants, with zero int-values |
---|
1834 | return true; |
---|
1835 | } |
---|
1836 | return false; |
---|
1837 | } |
---|
1838 | |
---|
1839 | int isFloatlikeConstantType(const Type *t) { |
---|
1840 | if ( const BasicType * bty = dynamic_cast< const BasicType * >( t ) ) { |
---|
1841 | if ( ! bty->isInteger() ) { |
---|
1842 | return true; |
---|
1843 | } |
---|
1844 | } |
---|
1845 | return false; |
---|
1846 | } |
---|
1847 | |
---|
1848 | int isStringlikeConstantType(const Type *t) { |
---|
1849 | if ( const ArrayType * aty = dynamic_cast< const ArrayType * >( t ) ) { |
---|
1850 | if ( const BasicType * bty = dynamic_cast< const BasicType * >( aty->base ) ) { |
---|
1851 | if ( bty->kind == BasicType::Kind::Char ) { |
---|
1852 | return true; |
---|
1853 | } |
---|
1854 | } |
---|
1855 | } |
---|
1856 | return false; |
---|
1857 | } |
---|
1858 | |
---|
1859 | virtual void visit( ConstantExpr * old ) override final { |
---|
1860 | ast::ConstantExpr *rslt = nullptr; |
---|
1861 | if (isIntlikeConstantType(old->result)) { |
---|
1862 | rslt = new ast::ConstantExpr( |
---|
1863 | old->location, |
---|
1864 | GET_ACCEPT_1(result, Type), |
---|
1865 | old->constant.get_value(), |
---|
1866 | (unsigned long long) old->intValue() |
---|
1867 | ); |
---|
1868 | } else if (isFloatlikeConstantType(old->result)) { |
---|
1869 | rslt = new ast::ConstantExpr( |
---|
1870 | old->location, |
---|
1871 | GET_ACCEPT_1(result, Type), |
---|
1872 | old->constant.get_value(), |
---|
1873 | (double) old->constant.get_dval() |
---|
1874 | ); |
---|
1875 | } else if (isStringlikeConstantType(old->result)) { |
---|
1876 | rslt = ast::ConstantExpr::from_string( |
---|
1877 | old->location, |
---|
1878 | old->constant.get_value() |
---|
1879 | ); |
---|
1880 | } |
---|
1881 | assert(rslt); |
---|
1882 | this->node = visitBaseExpr( old, rslt ); |
---|
1883 | } |
---|
1884 | |
---|
1885 | virtual void visit( SizeofExpr * old ) override final { |
---|
1886 | assert (old->expr || old->type); |
---|
1887 | assert (! (old->expr && old->type)); |
---|
1888 | ast::SizeofExpr *rslt; |
---|
1889 | if (old->expr) { |
---|
1890 | assert(!old->isType); |
---|
1891 | rslt = new ast::SizeofExpr( |
---|
1892 | old->location, |
---|
1893 | GET_ACCEPT_1(expr, Expr) |
---|
1894 | ); |
---|
1895 | } |
---|
1896 | if (old->type) { |
---|
1897 | assert(old->isType); |
---|
1898 | rslt = new ast::SizeofExpr( |
---|
1899 | old->location, |
---|
1900 | GET_ACCEPT_1(type, Type) |
---|
1901 | ); |
---|
1902 | } |
---|
1903 | this->node = visitBaseExpr( old, rslt ); |
---|
1904 | } |
---|
1905 | |
---|
1906 | virtual void visit( AlignofExpr * old ) override final { |
---|
1907 | assert (old->expr || old->type); |
---|
1908 | assert (! (old->expr && old->type)); |
---|
1909 | ast::AlignofExpr *rslt; |
---|
1910 | if (old->expr) { |
---|
1911 | assert(!old->isType); |
---|
1912 | rslt = new ast::AlignofExpr( |
---|
1913 | old->location, |
---|
1914 | GET_ACCEPT_1(expr, Expr) |
---|
1915 | ); |
---|
1916 | } |
---|
1917 | if (old->type) { |
---|
1918 | assert(old->isType); |
---|
1919 | rslt = new ast::AlignofExpr( |
---|
1920 | old->location, |
---|
1921 | GET_ACCEPT_1(type, Type) |
---|
1922 | ); |
---|
1923 | } |
---|
1924 | this->node = visitBaseExpr( old, rslt ); |
---|
1925 | } |
---|
1926 | |
---|
1927 | virtual void visit( UntypedOffsetofExpr * old ) override final { |
---|
1928 | this->node = visitBaseExpr( old, |
---|
1929 | new ast::UntypedOffsetofExpr( |
---|
1930 | old->location, |
---|
1931 | GET_ACCEPT_1(type, Type), |
---|
1932 | old->member |
---|
1933 | ) |
---|
1934 | ); |
---|
1935 | } |
---|
1936 | |
---|
1937 | virtual void visit( OffsetofExpr * old ) override final { |
---|
1938 | this->node = visitBaseExpr( old, |
---|
1939 | new ast::OffsetofExpr( |
---|
1940 | old->location, |
---|
1941 | GET_ACCEPT_1(type, Type), |
---|
1942 | inCache(old->member) ? |
---|
1943 | dynamic_cast<ast::DeclWithType *>(this->node) : |
---|
1944 | GET_ACCEPT_1(member, DeclWithType) |
---|
1945 | ) |
---|
1946 | ); |
---|
1947 | } |
---|
1948 | |
---|
1949 | virtual void visit( OffsetPackExpr * old ) override final { |
---|
1950 | this->node = visitBaseExpr( old, |
---|
1951 | new ast::OffsetPackExpr( |
---|
1952 | old->location, |
---|
1953 | GET_ACCEPT_1(type, StructInstType) |
---|
1954 | ) |
---|
1955 | ); |
---|
1956 | } |
---|
1957 | |
---|
1958 | virtual void visit( LogicalExpr * old ) override final { |
---|
1959 | this->node = visitBaseExpr( old, |
---|
1960 | new ast::LogicalExpr( |
---|
1961 | old->location, |
---|
1962 | GET_ACCEPT_1(arg1, Expr), |
---|
1963 | GET_ACCEPT_1(arg2, Expr), |
---|
1964 | old->get_isAnd() ? |
---|
1965 | ast::LogicalFlag::AndExpr : |
---|
1966 | ast::LogicalFlag::OrExpr |
---|
1967 | ) |
---|
1968 | ); |
---|
1969 | } |
---|
1970 | |
---|
1971 | virtual void visit( ConditionalExpr * old ) override final { |
---|
1972 | this->node = visitBaseExpr( old, |
---|
1973 | new ast::ConditionalExpr( |
---|
1974 | old->location, |
---|
1975 | GET_ACCEPT_1(arg1, Expr), |
---|
1976 | GET_ACCEPT_1(arg2, Expr), |
---|
1977 | GET_ACCEPT_1(arg3, Expr) |
---|
1978 | ) |
---|
1979 | ); |
---|
1980 | } |
---|
1981 | |
---|
1982 | virtual void visit( CommaExpr * old ) override final { |
---|
1983 | this->node = visitBaseExpr( old, |
---|
1984 | new ast::CommaExpr( |
---|
1985 | old->location, |
---|
1986 | GET_ACCEPT_1(arg1, Expr), |
---|
1987 | GET_ACCEPT_1(arg2, Expr) |
---|
1988 | ) |
---|
1989 | ); |
---|
1990 | } |
---|
1991 | |
---|
1992 | virtual void visit( TypeExpr * ) override final { |
---|
1993 | |
---|
1994 | } |
---|
1995 | |
---|
1996 | virtual void visit( AsmExpr * ) override final { |
---|
1997 | |
---|
1998 | } |
---|
1999 | |
---|
2000 | virtual void visit( ImplicitCopyCtorExpr * ) override final { |
---|
2001 | |
---|
2002 | } |
---|
2003 | |
---|
2004 | virtual void visit( ConstructorExpr * ) override final { |
---|
2005 | |
---|
2006 | } |
---|
2007 | |
---|
2008 | virtual void visit( CompoundLiteralExpr * ) override final { |
---|
2009 | |
---|
2010 | } |
---|
2011 | |
---|
2012 | virtual void visit( RangeExpr * ) override final { |
---|
2013 | |
---|
2014 | } |
---|
2015 | |
---|
2016 | virtual void visit( UntypedTupleExpr * ) override final { |
---|
2017 | |
---|
2018 | } |
---|
2019 | |
---|
2020 | virtual void visit( TupleExpr * ) override final { |
---|
2021 | |
---|
2022 | } |
---|
2023 | |
---|
2024 | virtual void visit( TupleIndexExpr * ) override final { |
---|
2025 | |
---|
2026 | } |
---|
2027 | |
---|
2028 | virtual void visit( TupleAssignExpr * ) override final { |
---|
2029 | |
---|
2030 | } |
---|
2031 | |
---|
2032 | virtual void visit( StmtExpr * ) override final { |
---|
2033 | |
---|
2034 | } |
---|
2035 | |
---|
2036 | virtual void visit( UniqueExpr * ) override final { |
---|
2037 | |
---|
2038 | } |
---|
2039 | |
---|
2040 | virtual void visit( UntypedInitExpr * ) override final { |
---|
2041 | |
---|
2042 | } |
---|
2043 | |
---|
2044 | virtual void visit( InitExpr * ) override final { |
---|
2045 | |
---|
2046 | } |
---|
2047 | |
---|
2048 | virtual void visit( DeletedExpr * ) override final { |
---|
2049 | |
---|
2050 | } |
---|
2051 | |
---|
2052 | virtual void visit( DefaultArgExpr * ) override final { |
---|
2053 | |
---|
2054 | } |
---|
2055 | |
---|
2056 | virtual void visit( GenericExpr * ) override final { |
---|
2057 | |
---|
2058 | } |
---|
2059 | |
---|
2060 | virtual void visit( VoidType * old ) override final { |
---|
2061 | this->node = new ast::VoidType{ cv( old ) }; |
---|
2062 | } |
---|
2063 | |
---|
2064 | virtual void visit( BasicType * old ) override final { |
---|
2065 | this->node = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) }; |
---|
2066 | } |
---|
2067 | |
---|
2068 | virtual void visit( PointerType * old ) override final { |
---|
2069 | this->node = new ast::PointerType{ |
---|
2070 | GET_ACCEPT_1( base, Type ), |
---|
2071 | GET_ACCEPT_1( dimension, Expr ), |
---|
2072 | (ast::LengthFlag)old->isVarLen, |
---|
2073 | (ast::DimensionFlag)old->isStatic, |
---|
2074 | cv( old ) |
---|
2075 | }; |
---|
2076 | } |
---|
2077 | |
---|
2078 | virtual void visit( ArrayType * old ) override final { |
---|
2079 | this->node = new ast::ArrayType{ |
---|
2080 | GET_ACCEPT_1( base, Type ), |
---|
2081 | GET_ACCEPT_1( dimension, Expr ), |
---|
2082 | (ast::LengthFlag)old->isVarLen, |
---|
2083 | (ast::DimensionFlag)old->isStatic, |
---|
2084 | cv( old ) |
---|
2085 | }; |
---|
2086 | } |
---|
2087 | |
---|
2088 | virtual void visit( ReferenceType * old ) override final { |
---|
2089 | this->node = new ast::ReferenceType{ |
---|
2090 | GET_ACCEPT_1( base, Type ), |
---|
2091 | cv( old ) |
---|
2092 | }; |
---|
2093 | } |
---|
2094 | |
---|
2095 | virtual void visit( QualifiedType * old ) override final { |
---|
2096 | this->node = new ast::QualifiedType{ |
---|
2097 | GET_ACCEPT_1( parent, Type ), |
---|
2098 | GET_ACCEPT_1( child, Type ), |
---|
2099 | cv( old ) |
---|
2100 | }; |
---|
2101 | } |
---|
2102 | |
---|
2103 | virtual void visit( FunctionType * old ) override final { |
---|
2104 | auto ty = new ast::FunctionType { |
---|
2105 | (ast::ArgumentFlag)old->isVarArgs, |
---|
2106 | cv( old ) |
---|
2107 | }; |
---|
2108 | ty->returns = GET_ACCEPT_V( returnVals, DeclWithType ); |
---|
2109 | ty->params = GET_ACCEPT_V( parameters, DeclWithType ); |
---|
2110 | ty->forall = GET_ACCEPT_V( forall, TypeDecl ); |
---|
2111 | this->node = ty; |
---|
2112 | } |
---|
2113 | |
---|
2114 | void postvisit( ReferenceToType * old, ast::ReferenceToType * ty ) { |
---|
2115 | ty->forall = GET_ACCEPT_V( forall, TypeDecl ); |
---|
2116 | ty->params = GET_ACCEPT_V( parameters, Expr ); |
---|
2117 | ty->hoistType = old->hoistType; |
---|
2118 | } |
---|
2119 | |
---|
2120 | virtual void visit( StructInstType * old ) override final { |
---|
2121 | ast::StructInstType * ty; |
---|
2122 | if ( old->baseStruct ) { |
---|
2123 | ty = new ast::StructInstType{ |
---|
2124 | GET_ACCEPT_1( baseStruct, StructDecl ), |
---|
2125 | cv( old ), |
---|
2126 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2127 | }; |
---|
2128 | } else { |
---|
2129 | ty = new ast::StructInstType{ |
---|
2130 | old->name, |
---|
2131 | cv( old ), |
---|
2132 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2133 | }; |
---|
2134 | } |
---|
2135 | postvisit( old, ty ); |
---|
2136 | this->node = ty; |
---|
2137 | } |
---|
2138 | |
---|
2139 | virtual void visit( UnionInstType * old ) override final { |
---|
2140 | ast::UnionInstType * ty; |
---|
2141 | if ( old->baseUnion ) { |
---|
2142 | ty = new ast::UnionInstType{ |
---|
2143 | GET_ACCEPT_1( baseUnion, UnionDecl ), |
---|
2144 | cv( old ), |
---|
2145 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2146 | }; |
---|
2147 | } else { |
---|
2148 | ty = new ast::UnionInstType{ |
---|
2149 | old->name, |
---|
2150 | cv( old ), |
---|
2151 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2152 | }; |
---|
2153 | } |
---|
2154 | postvisit( old, ty ); |
---|
2155 | this->node = ty; |
---|
2156 | } |
---|
2157 | |
---|
2158 | virtual void visit( EnumInstType * old ) override final { |
---|
2159 | ast::EnumInstType * ty; |
---|
2160 | if ( old->baseEnum ) { |
---|
2161 | ty = new ast::EnumInstType{ |
---|
2162 | GET_ACCEPT_1( baseEnum, EnumDecl ), |
---|
2163 | cv( old ), |
---|
2164 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2165 | }; |
---|
2166 | } else { |
---|
2167 | ty = new ast::EnumInstType{ |
---|
2168 | old->name, |
---|
2169 | cv( old ), |
---|
2170 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2171 | }; |
---|
2172 | } |
---|
2173 | postvisit( old, ty ); |
---|
2174 | this->node = ty; |
---|
2175 | } |
---|
2176 | |
---|
2177 | virtual void visit( TraitInstType * old ) override final { |
---|
2178 | ast::TraitInstType * ty; |
---|
2179 | if ( old->baseTrait ) { |
---|
2180 | ty = new ast::TraitInstType{ |
---|
2181 | GET_ACCEPT_1( baseTrait, TraitDecl ), |
---|
2182 | cv( old ), |
---|
2183 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2184 | }; |
---|
2185 | } else { |
---|
2186 | ty = new ast::TraitInstType{ |
---|
2187 | old->name, |
---|
2188 | cv( old ), |
---|
2189 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2190 | }; |
---|
2191 | } |
---|
2192 | postvisit( old, ty ); |
---|
2193 | this->node = ty; |
---|
2194 | } |
---|
2195 | |
---|
2196 | virtual void visit( TypeInstType * old ) override final { |
---|
2197 | ast::TypeInstType * ty; |
---|
2198 | if ( old->baseType ) { |
---|
2199 | ty = new ast::TypeInstType{ |
---|
2200 | old->name, |
---|
2201 | GET_ACCEPT_1( baseType, TypeDecl ), |
---|
2202 | cv( old ), |
---|
2203 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2204 | }; |
---|
2205 | } else { |
---|
2206 | ty = new ast::TypeInstType{ |
---|
2207 | old->name, |
---|
2208 | old->isFtype ? ast::TypeVar::Ftype : ast::TypeVar::Dtype, |
---|
2209 | cv( old ), |
---|
2210 | GET_ACCEPT_V( attributes, Attribute ) |
---|
2211 | }; |
---|
2212 | } |
---|
2213 | postvisit( old, ty ); |
---|
2214 | this->node = ty; |
---|
2215 | } |
---|
2216 | |
---|
2217 | virtual void visit( TupleType * ) override final { |
---|
2218 | |
---|
2219 | } |
---|
2220 | |
---|
2221 | virtual void visit( TypeofType * ) override final { |
---|
2222 | |
---|
2223 | } |
---|
2224 | |
---|
2225 | virtual void visit( AttrType * ) override final { |
---|
2226 | |
---|
2227 | } |
---|
2228 | |
---|
2229 | virtual void visit( VarArgsType * ) override final { |
---|
2230 | |
---|
2231 | } |
---|
2232 | |
---|
2233 | virtual void visit( ZeroType * ) override final { |
---|
2234 | |
---|
2235 | } |
---|
2236 | |
---|
2237 | virtual void visit( OneType * ) override final { |
---|
2238 | |
---|
2239 | } |
---|
2240 | |
---|
2241 | virtual void visit( GlobalScopeType * ) override final { |
---|
2242 | |
---|
2243 | } |
---|
2244 | |
---|
2245 | virtual void visit( Designation * ) override final { |
---|
2246 | |
---|
2247 | } |
---|
2248 | |
---|
2249 | virtual void visit( SingleInit * ) override final { |
---|
2250 | |
---|
2251 | } |
---|
2252 | |
---|
2253 | virtual void visit( ListInit * ) override final { |
---|
2254 | |
---|
2255 | } |
---|
2256 | |
---|
2257 | virtual void visit( ConstructorInit * ) override final { |
---|
2258 | |
---|
2259 | } |
---|
2260 | |
---|
2261 | virtual void visit( Constant * ) override final { |
---|
2262 | |
---|
2263 | } |
---|
2264 | |
---|
2265 | virtual void visit( Attribute * ) override final { |
---|
2266 | |
---|
2267 | } |
---|
2268 | |
---|
2269 | virtual void visit( AttrExpr * ) override final { |
---|
2270 | |
---|
2271 | assert( 0 ); |
---|
2272 | } |
---|
2273 | }; |
---|
2274 | |
---|
2275 | #undef GET_LABELS_V |
---|
2276 | #undef GET_ACCEPT_V |
---|
2277 | #undef GET_ACCEPT_1 |
---|
2278 | |
---|
2279 | std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) { |
---|
2280 | ConverterOldToNew c; |
---|
2281 | std::list< ast::ptr< ast::Decl > > decls; |
---|
2282 | for(auto d : translationUnit) { |
---|
2283 | d->accept( c ); |
---|
2284 | decls.emplace_back( c.decl() ); |
---|
2285 | delete d; |
---|
2286 | } |
---|
2287 | return decls; |
---|
2288 | } |
---|