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 : Peter A. Buhr
|
---|
12 | // Last Modified On : Wed Dec 11 21:39:32 2019
|
---|
13 | // Update Count : 33
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "Convert.hpp"
|
---|
17 |
|
---|
18 | #include <deque>
|
---|
19 | #include <unordered_map>
|
---|
20 |
|
---|
21 | #include "AST/Attribute.hpp"
|
---|
22 | #include "AST/Decl.hpp"
|
---|
23 | #include "AST/Expr.hpp"
|
---|
24 | #include "AST/Init.hpp"
|
---|
25 | #include "AST/Stmt.hpp"
|
---|
26 | #include "AST/TypeSubstitution.hpp"
|
---|
27 |
|
---|
28 | #include "SymTab/Autogen.h"
|
---|
29 | #include "SynTree/Attribute.h"
|
---|
30 | #include "SynTree/Declaration.h"
|
---|
31 | #include "SynTree/TypeSubstitution.h"
|
---|
32 |
|
---|
33 | #include "Validate/FindSpecialDecls.h"
|
---|
34 |
|
---|
35 | //================================================================================================
|
---|
36 | // Utilities
|
---|
37 | template<template <class...> class C>
|
---|
38 | struct to {
|
---|
39 | template<typename T>
|
---|
40 | static auto from( T && v ) -> C< typename T::value_type > {
|
---|
41 | C< typename T::value_type > l;
|
---|
42 | std::move(std::begin(v), std::end(v), std::back_inserter(l));
|
---|
43 | return l;
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | //================================================================================================
|
---|
48 | namespace {
|
---|
49 |
|
---|
50 | // This is to preserve the FindSpecialDecls hack. It does not (and perhaps should not)
|
---|
51 | // allow us to use the same stratagy in the new ast.
|
---|
52 | ast::Type * sizeType = nullptr;
|
---|
53 | ast::FunctionDecl * dereferenceOperator = nullptr;
|
---|
54 | ast::StructDecl * dtorStruct = nullptr;
|
---|
55 | ast::FunctionDecl * dtorStructDestroy = nullptr;
|
---|
56 |
|
---|
57 | }
|
---|
58 |
|
---|
59 | //================================================================================================
|
---|
60 | class ConverterNewToOld : public ast::Visitor {
|
---|
61 | BaseSyntaxNode * node = nullptr;
|
---|
62 | using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;
|
---|
63 | Cache cache;
|
---|
64 |
|
---|
65 | template<typename T>
|
---|
66 | struct Getter {
|
---|
67 | ConverterNewToOld & visitor;
|
---|
68 |
|
---|
69 | template<typename U, enum ast::Node::ref_type R>
|
---|
70 | T * accept1( const ast::ptr_base<U, R> & ptr ) {
|
---|
71 | if ( ! ptr ) return nullptr;
|
---|
72 | ptr->accept( visitor );
|
---|
73 | T * ret = strict_dynamic_cast< T * >( visitor.node );
|
---|
74 | visitor.node = nullptr;
|
---|
75 | return ret;
|
---|
76 | }
|
---|
77 |
|
---|
78 | template<typename U>
|
---|
79 | std::list< T * > acceptL( const U & container ) {
|
---|
80 | std::list< T * > ret;
|
---|
81 | for ( auto ptr : container ) {
|
---|
82 | ret.emplace_back( accept1( ptr ) );
|
---|
83 | }
|
---|
84 | return ret;
|
---|
85 | }
|
---|
86 | };
|
---|
87 |
|
---|
88 | template<typename T>
|
---|
89 | Getter<T> get() {
|
---|
90 | return Getter<T>{ *this };
|
---|
91 | }
|
---|
92 |
|
---|
93 | Label makeLabel(Statement * labelled, const ast::Label& label) {
|
---|
94 | // This probably will leak memory, but only until we get rid of the old tree.
|
---|
95 | if ( nullptr == labelled && label.location.isSet() ) {
|
---|
96 | labelled = new NullStmt();
|
---|
97 | labelled->location = label.location;
|
---|
98 | }
|
---|
99 | return Label(
|
---|
100 | label.name,
|
---|
101 | labelled,
|
---|
102 | get<Attribute>().acceptL(label.attributes)
|
---|
103 | );
|
---|
104 | }
|
---|
105 |
|
---|
106 | template<template <class...> class C>
|
---|
107 | std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) {
|
---|
108 | std::list<Label> ret;
|
---|
109 | for (auto label : labels) {
|
---|
110 | ret.push_back( makeLabel(labelled, label) );
|
---|
111 | }
|
---|
112 | return ret;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /// get new qualifiers from old type
|
---|
116 | Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }
|
---|
117 |
|
---|
118 | /// returns true and sets `node` if in cache
|
---|
119 | bool inCache( const ast::Node * node ) {
|
---|
120 | auto it = cache.find( node );
|
---|
121 | if ( it == cache.end() ) return false;
|
---|
122 | this->node = it->second;
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public:
|
---|
127 | Declaration * decl( const ast::Decl * declNode ) {
|
---|
128 | return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) );
|
---|
129 | }
|
---|
130 |
|
---|
131 | private:
|
---|
132 | void declPostamble( Declaration * decl, const ast::Decl * node ) {
|
---|
133 | decl->location = node->location;
|
---|
134 | // name comes from constructor
|
---|
135 | // linkage comes from constructor
|
---|
136 | decl->extension = node->extension;
|
---|
137 | decl->uniqueId = node->uniqueId;
|
---|
138 | // storageClasses comes from constructor
|
---|
139 | this->node = decl;
|
---|
140 | }
|
---|
141 |
|
---|
142 | const ast::DeclWithType * declWithTypePostamble (
|
---|
143 | DeclarationWithType * decl, const ast::DeclWithType * node ) {
|
---|
144 | cache.emplace( node, decl );
|
---|
145 | decl->mangleName = node->mangleName;
|
---|
146 | decl->scopeLevel = node->scopeLevel;
|
---|
147 | decl->asmName = get<Expression>().accept1( node->asmName );
|
---|
148 | // attributes comes from constructor
|
---|
149 | decl->isDeleted = node->isDeleted;
|
---|
150 | // fs comes from constructor
|
---|
151 | declPostamble( decl, node );
|
---|
152 | return nullptr;
|
---|
153 | }
|
---|
154 |
|
---|
155 | const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final {
|
---|
156 | auto&& bfwd = get<Expression>().accept1( node->bitfieldWidth );
|
---|
157 | auto&& type = get<Type>().accept1( node->type );
|
---|
158 | auto&& init = get<Initializer>().accept1( node->init );
|
---|
159 | auto&& attr = get<Attribute>().acceptL( node->attributes );
|
---|
160 | if ( inCache( node ) ) {
|
---|
161 | return nullptr;
|
---|
162 | }
|
---|
163 | auto decl = new ObjectDecl(
|
---|
164 | node->name,
|
---|
165 | Type::StorageClasses( node->storage.val ),
|
---|
166 | LinkageSpec::Spec( node->linkage.val ),
|
---|
167 | bfwd,
|
---|
168 | type,
|
---|
169 | init,
|
---|
170 | attr,
|
---|
171 | Type::FuncSpecifiers( node->funcSpec.val )
|
---|
172 | );
|
---|
173 | return declWithTypePostamble( decl, node );
|
---|
174 | }
|
---|
175 |
|
---|
176 | const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final {
|
---|
177 | if ( inCache( node ) ) return nullptr;
|
---|
178 | auto decl = new FunctionDecl(
|
---|
179 | node->name,
|
---|
180 | Type::StorageClasses( node->storage.val ),
|
---|
181 | LinkageSpec::Spec( node->linkage.val ),
|
---|
182 | get<FunctionType>().accept1( node->type ),
|
---|
183 | {},
|
---|
184 | get<Attribute>().acceptL( node->attributes ),
|
---|
185 | Type::FuncSpecifiers( node->funcSpec.val )
|
---|
186 | );
|
---|
187 | cache.emplace( node, decl );
|
---|
188 | decl->statements = get<CompoundStmt>().accept1( node->stmts );
|
---|
189 | decl->withExprs = get<Expression>().acceptL( node->withExprs );
|
---|
190 | if ( dereferenceOperator == node ) {
|
---|
191 | Validate::dereferenceOperator = decl;
|
---|
192 | }
|
---|
193 | if ( dtorStructDestroy == node ) {
|
---|
194 | Validate::dtorStructDestroy = decl;
|
---|
195 | }
|
---|
196 | return declWithTypePostamble( decl, node );
|
---|
197 | }
|
---|
198 |
|
---|
199 | const ast::Decl * namedTypePostamble( NamedTypeDecl * decl, const ast::NamedTypeDecl * node ) {
|
---|
200 | // base comes from constructor
|
---|
201 | decl->parameters = get<TypeDecl>().acceptL( node->params );
|
---|
202 | decl->assertions = get<DeclarationWithType>().acceptL( node->assertions );
|
---|
203 | declPostamble( decl, node );
|
---|
204 | return nullptr;
|
---|
205 | }
|
---|
206 |
|
---|
207 | const ast::Decl * visit( const ast::TypeDecl * node ) override final {
|
---|
208 | if ( inCache( node ) ) return nullptr;
|
---|
209 | auto decl = new TypeDecl(
|
---|
210 | node->name,
|
---|
211 | Type::StorageClasses( node->storage.val ),
|
---|
212 | get<Type>().accept1( node->base ),
|
---|
213 | (TypeDecl::Kind)(unsigned)node->kind,
|
---|
214 | node->sized,
|
---|
215 | get<Type>().accept1( node->init )
|
---|
216 | );
|
---|
217 | cache.emplace( node, decl );
|
---|
218 | return namedTypePostamble( decl, node );
|
---|
219 | }
|
---|
220 |
|
---|
221 | const ast::Decl * visit( const ast::TypedefDecl * node ) override final {
|
---|
222 | auto decl = new TypedefDecl(
|
---|
223 | node->name,
|
---|
224 | node->location,
|
---|
225 | Type::StorageClasses( node->storage.val ),
|
---|
226 | get<Type>().accept1( node->base ),
|
---|
227 | LinkageSpec::Spec( node->linkage.val )
|
---|
228 | );
|
---|
229 | return namedTypePostamble( decl, node );
|
---|
230 | }
|
---|
231 |
|
---|
232 | const ast::Decl * aggregatePostamble( AggregateDecl * decl, const ast::AggregateDecl * node ) {
|
---|
233 | cache.emplace( node, decl );
|
---|
234 | decl->members = get<Declaration>().acceptL( node->members );
|
---|
235 | decl->parameters = get<TypeDecl>().acceptL( node->params );
|
---|
236 | decl->body = node->body;
|
---|
237 | // attributes come from constructor
|
---|
238 | decl->parent = get<AggregateDecl>().accept1( node->parent );
|
---|
239 | declPostamble( decl, node );
|
---|
240 | return nullptr;
|
---|
241 | }
|
---|
242 |
|
---|
243 | const ast::Decl * visit( const ast::StructDecl * node ) override final {
|
---|
244 | if ( inCache( node ) ) return nullptr;
|
---|
245 | auto decl = new StructDecl(
|
---|
246 | node->name,
|
---|
247 | (AggregateDecl::Aggregate)node->kind,
|
---|
248 | get<Attribute>().acceptL( node->attributes ),
|
---|
249 | LinkageSpec::Spec( node->linkage.val )
|
---|
250 | );
|
---|
251 |
|
---|
252 | if ( dtorStruct == node ) {
|
---|
253 | Validate::dtorStruct = decl;
|
---|
254 | }
|
---|
255 |
|
---|
256 | return aggregatePostamble( decl, node );
|
---|
257 | }
|
---|
258 |
|
---|
259 | const ast::Decl * visit( const ast::UnionDecl * node ) override final {
|
---|
260 | if ( inCache( node ) ) return nullptr;
|
---|
261 | auto decl = new UnionDecl(
|
---|
262 | node->name,
|
---|
263 | get<Attribute>().acceptL( node->attributes ),
|
---|
264 | LinkageSpec::Spec( node->linkage.val )
|
---|
265 | );
|
---|
266 | return aggregatePostamble( decl, node );
|
---|
267 | }
|
---|
268 |
|
---|
269 | const ast::Decl * visit( const ast::EnumDecl * node ) override final {
|
---|
270 | if ( inCache( node ) ) return nullptr;
|
---|
271 | auto decl = new EnumDecl(
|
---|
272 | node->name,
|
---|
273 | get<Attribute>().acceptL( node->attributes ),
|
---|
274 | LinkageSpec::Spec( node->linkage.val )
|
---|
275 | );
|
---|
276 | return aggregatePostamble( decl, node );
|
---|
277 | }
|
---|
278 |
|
---|
279 | const ast::Decl * visit( const ast::TraitDecl * node ) override final {
|
---|
280 | if ( inCache( node ) ) return nullptr;
|
---|
281 | auto decl = new TraitDecl(
|
---|
282 | node->name,
|
---|
283 | {},
|
---|
284 | LinkageSpec::Spec( node->linkage.val )
|
---|
285 | );
|
---|
286 | return aggregatePostamble( decl, node );
|
---|
287 | }
|
---|
288 |
|
---|
289 | const ast::AsmDecl * visit( const ast::AsmDecl * node ) override final {
|
---|
290 | auto decl = new AsmDecl( get<AsmStmt>().accept1( node->stmt ) );
|
---|
291 | declPostamble( decl, node );
|
---|
292 | return nullptr;
|
---|
293 | }
|
---|
294 |
|
---|
295 | const ast::StaticAssertDecl * visit( const ast::StaticAssertDecl * node ) override final {
|
---|
296 | auto decl = new StaticAssertDecl(
|
---|
297 | get<Expression>().accept1( node->cond ),
|
---|
298 | get<ConstantExpr>().accept1( node->msg )
|
---|
299 | );
|
---|
300 | declPostamble( decl, node );
|
---|
301 | return nullptr;
|
---|
302 | }
|
---|
303 |
|
---|
304 | const ast::Stmt * stmtPostamble( Statement * stmt, const ast::Stmt * node ) {
|
---|
305 | cache.emplace( node, stmt );
|
---|
306 | stmt->location = node->location;
|
---|
307 | stmt->labels = makeLabelL( stmt, node->labels );
|
---|
308 | this->node = stmt;
|
---|
309 | return nullptr;
|
---|
310 | }
|
---|
311 |
|
---|
312 | const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final {
|
---|
313 | if ( inCache( node ) ) return nullptr;
|
---|
314 | auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) );
|
---|
315 | stmtPostamble( stmt, node );
|
---|
316 | return nullptr;
|
---|
317 | }
|
---|
318 |
|
---|
319 | const ast::Stmt * visit( const ast::ExprStmt * node ) override final {
|
---|
320 | if ( inCache( node ) ) return nullptr;
|
---|
321 | auto stmt = new ExprStmt( nullptr );
|
---|
322 | cache.emplace( node, stmt );
|
---|
323 | stmt->expr = get<Expression>().accept1( node->expr );
|
---|
324 | return stmtPostamble( stmt, node );
|
---|
325 | }
|
---|
326 |
|
---|
327 | const ast::Stmt * visit( const ast::AsmStmt * node ) override final {
|
---|
328 | if ( inCache( node ) ) return nullptr;
|
---|
329 | auto stmt = new AsmStmt(
|
---|
330 | node->isVolatile,
|
---|
331 | get<Expression>().accept1( node->instruction ),
|
---|
332 | get<Expression>().acceptL( node->output ),
|
---|
333 | get<Expression>().acceptL( node->input ),
|
---|
334 | get<ConstantExpr>().acceptL( node->clobber ),
|
---|
335 | makeLabelL( nullptr, node->gotoLabels ) // What are these labelling?
|
---|
336 | );
|
---|
337 | return stmtPostamble( stmt, node );
|
---|
338 | }
|
---|
339 |
|
---|
340 | const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final {
|
---|
341 | if ( inCache( node ) ) return nullptr;
|
---|
342 | auto stmt = new DirectiveStmt( node->directive );
|
---|
343 | return stmtPostamble( stmt, node );
|
---|
344 | }
|
---|
345 |
|
---|
346 | const ast::Stmt * visit( const ast::IfStmt * node ) override final {
|
---|
347 | if ( inCache( node ) ) return nullptr;
|
---|
348 | auto stmt = new IfStmt(
|
---|
349 | get<Expression>().accept1( node->cond ),
|
---|
350 | get<Statement>().accept1( node->thenPart ),
|
---|
351 | get<Statement>().accept1( node->elsePart ),
|
---|
352 | get<Statement>().acceptL( node->inits )
|
---|
353 | );
|
---|
354 | return stmtPostamble( stmt, node );
|
---|
355 | }
|
---|
356 |
|
---|
357 | const ast::Stmt * visit( const ast::SwitchStmt * node ) override final {
|
---|
358 | if ( inCache( node ) ) return nullptr;
|
---|
359 | auto stmt = new SwitchStmt(
|
---|
360 | get<Expression>().accept1( node->cond ),
|
---|
361 | get<Statement>().acceptL( node->stmts )
|
---|
362 | );
|
---|
363 | return stmtPostamble( stmt, node );
|
---|
364 | }
|
---|
365 |
|
---|
366 | const ast::Stmt * visit( const ast::CaseStmt * node ) override final {
|
---|
367 | if ( inCache( node ) ) return nullptr;
|
---|
368 | auto stmt = new CaseStmt(
|
---|
369 | get<Expression>().accept1( node->cond ),
|
---|
370 | get<Statement>().acceptL( node->stmts ),
|
---|
371 | node->isDefault()
|
---|
372 | );
|
---|
373 | return stmtPostamble( stmt, node );
|
---|
374 | }
|
---|
375 |
|
---|
376 | const ast::Stmt * visit( const ast::WhileStmt * node ) override final {
|
---|
377 | if ( inCache( node ) ) return nullptr;
|
---|
378 | auto inits = get<Statement>().acceptL( node->inits );
|
---|
379 | auto stmt = new WhileStmt(
|
---|
380 | get<Expression>().accept1( node->cond ),
|
---|
381 | get<Statement>().accept1( node->body ),
|
---|
382 | inits,
|
---|
383 | node->isDoWhile
|
---|
384 | );
|
---|
385 | return stmtPostamble( stmt, node );
|
---|
386 | }
|
---|
387 |
|
---|
388 | const ast::Stmt * visit( const ast::ForStmt * node ) override final {
|
---|
389 | if ( inCache( node ) ) return nullptr;
|
---|
390 | auto stmt = new ForStmt(
|
---|
391 | get<Statement>().acceptL( node->inits ),
|
---|
392 | get<Expression>().accept1( node->cond ),
|
---|
393 | get<Expression>().accept1( node->inc ),
|
---|
394 | get<Statement>().accept1( node->body )
|
---|
395 | );
|
---|
396 | return stmtPostamble( stmt, node );
|
---|
397 | }
|
---|
398 |
|
---|
399 | const ast::Stmt * visit( const ast::BranchStmt * node ) override final {
|
---|
400 | if ( inCache( node ) ) return nullptr;
|
---|
401 | BranchStmt * stmt;
|
---|
402 | if (node->computedTarget) {
|
---|
403 | stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ),
|
---|
404 | BranchStmt::Goto );
|
---|
405 | } else {
|
---|
406 | BranchStmt::Type type;
|
---|
407 | switch (node->kind) {
|
---|
408 | #define CASE(n) \
|
---|
409 | case ast::BranchStmt::n: \
|
---|
410 | type = BranchStmt::n; \
|
---|
411 | break
|
---|
412 | CASE(Goto);
|
---|
413 | CASE(Break);
|
---|
414 | CASE(Continue);
|
---|
415 | CASE(FallThrough);
|
---|
416 | CASE(FallThroughDefault);
|
---|
417 | #undef CASE
|
---|
418 | default:
|
---|
419 | assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind);
|
---|
420 | }
|
---|
421 |
|
---|
422 | // The labels here are also weird.
|
---|
423 | stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type );
|
---|
424 | stmt->target = makeLabel( stmt, node->target );
|
---|
425 | }
|
---|
426 | return stmtPostamble( stmt, node );
|
---|
427 | }
|
---|
428 |
|
---|
429 | const ast::Stmt * visit( const ast::ReturnStmt * node ) override final {
|
---|
430 | if ( inCache( node ) ) return nullptr;
|
---|
431 | auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) );
|
---|
432 | return stmtPostamble( stmt, node );
|
---|
433 | }
|
---|
434 |
|
---|
435 | const ast::Stmt * visit( const ast::ThrowStmt * node ) override final {
|
---|
436 | if ( inCache( node ) ) return nullptr;
|
---|
437 | ThrowStmt::Kind kind;
|
---|
438 | switch (node->kind) {
|
---|
439 | case ast::ExceptionKind::Terminate:
|
---|
440 | kind = ThrowStmt::Terminate;
|
---|
441 | break;
|
---|
442 | case ast::ExceptionKind::Resume:
|
---|
443 | kind = ThrowStmt::Resume;
|
---|
444 | break;
|
---|
445 | default:
|
---|
446 | assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind);
|
---|
447 | }
|
---|
448 | auto stmt = new ThrowStmt(
|
---|
449 | kind,
|
---|
450 | get<Expression>().accept1( node->expr ),
|
---|
451 | get<Expression>().accept1( node->target )
|
---|
452 | );
|
---|
453 | return stmtPostamble( stmt, node );
|
---|
454 | }
|
---|
455 |
|
---|
456 | const ast::Stmt * visit( const ast::TryStmt * node ) override final {
|
---|
457 | if ( inCache( node ) ) return nullptr;
|
---|
458 | auto handlers = get<CatchStmt>().acceptL( node->handlers );
|
---|
459 | auto stmt = new TryStmt(
|
---|
460 | get<CompoundStmt>().accept1( node->body ),
|
---|
461 | handlers,
|
---|
462 | get<FinallyStmt>().accept1( node->finally )
|
---|
463 | );
|
---|
464 | return stmtPostamble( stmt, node );
|
---|
465 | }
|
---|
466 |
|
---|
467 | const ast::Stmt * visit( const ast::CatchStmt * node ) override final {
|
---|
468 | if ( inCache( node ) ) return nullptr;
|
---|
469 | CatchStmt::Kind kind;
|
---|
470 | switch (node->kind) {
|
---|
471 | case ast::ExceptionKind::Terminate:
|
---|
472 | kind = CatchStmt::Terminate;
|
---|
473 | break;
|
---|
474 | case ast::ExceptionKind::Resume:
|
---|
475 | kind = CatchStmt::Resume;
|
---|
476 | break;
|
---|
477 | default:
|
---|
478 | assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind);
|
---|
479 | }
|
---|
480 | auto stmt = new CatchStmt(
|
---|
481 | kind,
|
---|
482 | get<Declaration>().accept1( node->decl ),
|
---|
483 | get<Expression>().accept1( node->cond ),
|
---|
484 | get<Statement>().accept1( node->body )
|
---|
485 | );
|
---|
486 | return stmtPostamble( stmt, node );
|
---|
487 | }
|
---|
488 |
|
---|
489 | const ast::Stmt * visit( const ast::FinallyStmt * node ) override final {
|
---|
490 | if ( inCache( node ) ) return nullptr;
|
---|
491 | auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) );
|
---|
492 | return stmtPostamble( stmt, node );
|
---|
493 | }
|
---|
494 |
|
---|
495 | const ast::Stmt * visit(const ast::SuspendStmt * node ) override final {
|
---|
496 | if ( inCache( node ) ) return nullptr;
|
---|
497 | auto stmt = new SuspendStmt();
|
---|
498 | stmt->then = get<CompoundStmt>().accept1( node->then );
|
---|
499 | switch(node->type) {
|
---|
500 | case ast::SuspendStmt::None : stmt->type = SuspendStmt::None ; break;
|
---|
501 | case ast::SuspendStmt::Coroutine: stmt->type = SuspendStmt::Coroutine; break;
|
---|
502 | case ast::SuspendStmt::Generator: stmt->type = SuspendStmt::Generator; break;
|
---|
503 | }
|
---|
504 | return stmtPostamble( stmt, node );
|
---|
505 | }
|
---|
506 |
|
---|
507 | const ast::Stmt * visit( const ast::WaitForStmt * node ) override final {
|
---|
508 | if ( inCache( node ) ) return nullptr;
|
---|
509 | auto stmt = new WaitForStmt;
|
---|
510 | stmt->clauses.reserve( node->clauses.size() );
|
---|
511 | for ( auto clause : node->clauses ) {
|
---|
512 | stmt->clauses.push_back({{
|
---|
513 | get<Expression>().accept1( clause.target.func ),
|
---|
514 | get<Expression>().acceptL( clause.target.args ),
|
---|
515 | },
|
---|
516 | get<Statement>().accept1( clause.stmt ),
|
---|
517 | get<Expression>().accept1( clause.cond ),
|
---|
518 | });
|
---|
519 | }
|
---|
520 | stmt->timeout = {
|
---|
521 | get<Expression>().accept1( node->timeout.time ),
|
---|
522 | get<Statement>().accept1( node->timeout.stmt ),
|
---|
523 | get<Expression>().accept1( node->timeout.cond ),
|
---|
524 | };
|
---|
525 | stmt->orelse = {
|
---|
526 | get<Statement>().accept1( node->orElse.stmt ),
|
---|
527 | get<Expression>().accept1( node->orElse.cond ),
|
---|
528 | };
|
---|
529 | return stmtPostamble( stmt, node );
|
---|
530 | }
|
---|
531 |
|
---|
532 | const ast::Decl * visit( const ast::WithStmt * node ) override final {
|
---|
533 | if ( inCache( node ) ) return nullptr;
|
---|
534 | auto stmt = new WithStmt(
|
---|
535 | get<Expression>().acceptL( node->exprs ),
|
---|
536 | get<Statement>().accept1( node->stmt )
|
---|
537 | );
|
---|
538 | declPostamble( stmt, node );
|
---|
539 | return nullptr;
|
---|
540 | }
|
---|
541 |
|
---|
542 | const ast::NullStmt * visit( const ast::NullStmt * node ) override final {
|
---|
543 | if ( inCache( node ) ) return nullptr;
|
---|
544 | auto stmt = new NullStmt();
|
---|
545 | stmtPostamble( stmt, node );
|
---|
546 | return nullptr;
|
---|
547 | }
|
---|
548 |
|
---|
549 | const ast::Stmt * visit( const ast::DeclStmt * node ) override final {
|
---|
550 | if ( inCache( node ) ) return nullptr;
|
---|
551 | auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) );
|
---|
552 | return stmtPostamble( stmt, node );
|
---|
553 | }
|
---|
554 |
|
---|
555 | const ast::Stmt * visit( const ast::ImplicitCtorDtorStmt * node ) override final {
|
---|
556 | if ( inCache( node ) ) return nullptr;
|
---|
557 | auto stmt = new ImplicitCtorDtorStmt{
|
---|
558 | get<Statement>().accept1( node->callStmt )
|
---|
559 | };
|
---|
560 | return stmtPostamble( stmt, node );
|
---|
561 | }
|
---|
562 |
|
---|
563 | TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) {
|
---|
564 |
|
---|
565 | if (!src) return nullptr;
|
---|
566 |
|
---|
567 | TypeSubstitution *rslt = new TypeSubstitution();
|
---|
568 |
|
---|
569 | for (decltype(src->begin()) src_i = src->begin(); src_i != src->end(); src_i++) {
|
---|
570 | rslt->add( src_i->first,
|
---|
571 | get<Type>().accept1(src_i->second) );
|
---|
572 | }
|
---|
573 |
|
---|
574 | for (decltype(src->beginVar()) src_i = src->beginVar(); src_i != src->endVar(); src_i++) {
|
---|
575 | rslt->addVar( src_i->first,
|
---|
576 | get<Expression>().accept1(src_i->second) );
|
---|
577 | }
|
---|
578 |
|
---|
579 | return rslt;
|
---|
580 | }
|
---|
581 |
|
---|
582 | void convertInferUnion(std::map<UniqueId,ParamEntry> &tgtInferParams,
|
---|
583 | std::vector<UniqueId> &tgtResnSlots,
|
---|
584 | const ast::Expr::InferUnion &srcInferred ) {
|
---|
585 |
|
---|
586 | assert( tgtInferParams.empty() );
|
---|
587 | assert( tgtResnSlots.empty() );
|
---|
588 |
|
---|
589 | if ( srcInferred.mode == ast::Expr::InferUnion::Params ) {
|
---|
590 | const ast::InferredParams &srcParams = srcInferred.inferParams();
|
---|
591 | for (auto & srcParam : srcParams) {
|
---|
592 | auto res = tgtInferParams.emplace(srcParam.first, ParamEntry(
|
---|
593 | srcParam.second.decl,
|
---|
594 | get<Declaration>().accept1(srcParam.second.declptr),
|
---|
595 | get<Type>().accept1(srcParam.second.actualType),
|
---|
596 | get<Type>().accept1(srcParam.second.formalType),
|
---|
597 | get<Expression>().accept1(srcParam.second.expr)
|
---|
598 | ));
|
---|
599 | assert(res.second);
|
---|
600 | }
|
---|
601 | } else if ( srcInferred.mode == ast::Expr::InferUnion::Slots ) {
|
---|
602 | const ast::ResnSlots &srcSlots = srcInferred.resnSlots();
|
---|
603 | for (auto srcSlot : srcSlots) {
|
---|
604 | tgtResnSlots.push_back(srcSlot);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | Expression * visitBaseExpr_skipResultType(const ast::Expr * src, Expression * tgt) {
|
---|
610 |
|
---|
611 | tgt->location = src->location;
|
---|
612 | tgt->env = convertTypeSubstitution(src->env);
|
---|
613 | tgt->extension = src->extension;
|
---|
614 |
|
---|
615 | convertInferUnion(tgt->inferParams, tgt->resnSlots, src->inferred);
|
---|
616 | return tgt;
|
---|
617 | }
|
---|
618 |
|
---|
619 | Expression * visitBaseExpr(const ast::Expr * src, Expression * tgt) {
|
---|
620 |
|
---|
621 | tgt->result = get<Type>().accept1(src->result);
|
---|
622 | // Unconditionally use a clone of the result type.
|
---|
623 | // We know this will leak some objects: much of the immediate conversion result.
|
---|
624 | // In some cases, using the conversion result directly gives unintended object sharing.
|
---|
625 | // A parameter (ObjectDecl, a child of a FunctionType) is shared by the weak-ref cache.
|
---|
626 | // But tgt->result must be fully owned privately by tgt.
|
---|
627 | // Applying these conservative copies here means
|
---|
628 | // - weak references point at the declaration's copy, not these expr.result copies (good)
|
---|
629 | // - we copy more objects than really needed (bad, tolerated)
|
---|
630 | if (tgt->result) {
|
---|
631 | tgt->result = tgt->result->clone();
|
---|
632 | }
|
---|
633 | return visitBaseExpr_skipResultType(src, tgt);
|
---|
634 | }
|
---|
635 |
|
---|
636 | const ast::Expr * visit( const ast::ApplicationExpr * node ) override final {
|
---|
637 | auto expr = visitBaseExpr( node,
|
---|
638 | new ApplicationExpr(
|
---|
639 | get<Expression>().accept1(node->func),
|
---|
640 | get<Expression>().acceptL(node->args)
|
---|
641 | )
|
---|
642 | );
|
---|
643 | this->node = expr;
|
---|
644 | return nullptr;
|
---|
645 | }
|
---|
646 |
|
---|
647 | const ast::Expr * visit( const ast::UntypedExpr * node ) override final {
|
---|
648 | auto expr = visitBaseExpr( node,
|
---|
649 | new UntypedExpr(
|
---|
650 | get<Expression>().accept1(node->func),
|
---|
651 | get<Expression>().acceptL(node->args)
|
---|
652 | )
|
---|
653 | );
|
---|
654 | this->node = expr;
|
---|
655 | return nullptr;
|
---|
656 | }
|
---|
657 |
|
---|
658 | const ast::Expr * visit( const ast::NameExpr * node ) override final {
|
---|
659 | auto expr = visitBaseExpr( node,
|
---|
660 | new NameExpr(
|
---|
661 | node->name
|
---|
662 | )
|
---|
663 | );
|
---|
664 | this->node = expr;
|
---|
665 | return nullptr;
|
---|
666 | }
|
---|
667 |
|
---|
668 | const ast::Expr * visit( const ast::AddressExpr * node ) override final {
|
---|
669 | auto expr = visitBaseExpr( node,
|
---|
670 | new AddressExpr(
|
---|
671 | get<Expression>().accept1(node->arg)
|
---|
672 | )
|
---|
673 | );
|
---|
674 | this->node = expr;
|
---|
675 | return nullptr;
|
---|
676 | }
|
---|
677 |
|
---|
678 | const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final {
|
---|
679 | auto expr = visitBaseExpr( node,
|
---|
680 | new LabelAddressExpr(
|
---|
681 | makeLabel(nullptr, node->arg)
|
---|
682 | )
|
---|
683 | );
|
---|
684 | this->node = expr;
|
---|
685 | return nullptr;
|
---|
686 | }
|
---|
687 |
|
---|
688 | const ast::Expr * visit( const ast::CastExpr * node ) override final {
|
---|
689 | auto expr = visitBaseExpr( node,
|
---|
690 | new CastExpr(
|
---|
691 | get<Expression>().accept1(node->arg),
|
---|
692 | (node->isGenerated == ast::GeneratedCast)
|
---|
693 | )
|
---|
694 | );
|
---|
695 | this->node = expr;
|
---|
696 | return nullptr;
|
---|
697 | }
|
---|
698 |
|
---|
699 | const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final {
|
---|
700 | AggregateDecl::Aggregate castTarget = (AggregateDecl::Aggregate)node->target;
|
---|
701 | assert( AggregateDecl::Generator <= castTarget && castTarget <= AggregateDecl::Thread );
|
---|
702 | auto expr = visitBaseExpr( node,
|
---|
703 | new KeywordCastExpr(
|
---|
704 | get<Expression>().accept1(node->arg),
|
---|
705 | castTarget
|
---|
706 | )
|
---|
707 | );
|
---|
708 | this->node = expr;
|
---|
709 | return nullptr;
|
---|
710 | }
|
---|
711 |
|
---|
712 | const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final {
|
---|
713 | auto expr = visitBaseExpr_skipResultType( node,
|
---|
714 | new VirtualCastExpr(
|
---|
715 | get<Expression>().accept1(node->arg),
|
---|
716 | get<Type>().accept1(node->result)
|
---|
717 | )
|
---|
718 | );
|
---|
719 | this->node = expr;
|
---|
720 | return nullptr;
|
---|
721 | }
|
---|
722 |
|
---|
723 | const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final {
|
---|
724 | auto expr = visitBaseExpr( node,
|
---|
725 | new UntypedMemberExpr(
|
---|
726 | get<Expression>().accept1(node->member),
|
---|
727 | get<Expression>().accept1(node->aggregate)
|
---|
728 | )
|
---|
729 | );
|
---|
730 | this->node = expr;
|
---|
731 | return nullptr;
|
---|
732 | }
|
---|
733 |
|
---|
734 | const ast::Expr * visit( const ast::MemberExpr * node ) override final {
|
---|
735 | auto expr = visitBaseExpr( node,
|
---|
736 | new MemberExpr(
|
---|
737 | get<DeclarationWithType>().accept1(node->member),
|
---|
738 | get<Expression>().accept1(node->aggregate)
|
---|
739 | )
|
---|
740 | );
|
---|
741 | this->node = expr;
|
---|
742 | return nullptr;
|
---|
743 | }
|
---|
744 |
|
---|
745 | const ast::Expr * visit( const ast::VariableExpr * node ) override final {
|
---|
746 | auto expr = new VariableExpr();
|
---|
747 | expr->var = get<DeclarationWithType>().accept1(node->var);
|
---|
748 | visitBaseExpr( node, expr );
|
---|
749 | this->node = expr;
|
---|
750 | return nullptr;
|
---|
751 | }
|
---|
752 |
|
---|
753 | const ast::Expr * visit( const ast::ConstantExpr * node ) override final {
|
---|
754 | // Old world: two types: rslt->constant.type, rslt->result
|
---|
755 | // New workd: one public type: node->result, plus node->underlyer only to support roundtrip conversion
|
---|
756 | // preserving underlyer because the correct type for string literals is complicated to construct,
|
---|
757 | // and distinguishing a string from other literals using the type is hard to do accurately
|
---|
758 | // Both worlds: the outer, expression-level type can change during resolution
|
---|
759 | // for a string, that's char[k] before-resolve and char * after
|
---|
760 | // Old world: the inner Constant type stays what it was built with
|
---|
761 | // for a string, that's char[k] always
|
---|
762 | // Both worlds: the "rep" field of a constant is the C source file fragment that compiles to the desired value
|
---|
763 | // for a string, that includes outer quotes, backslashes, et al cases from the Literals test
|
---|
764 | ConstantExpr *rslt = new ConstantExpr(Constant(
|
---|
765 | get<Type>().accept1(node->underlyer),
|
---|
766 | node->rep,
|
---|
767 | node->ival));
|
---|
768 | auto expr = visitBaseExpr( node, rslt );
|
---|
769 | this->node = expr;
|
---|
770 | return nullptr;
|
---|
771 | }
|
---|
772 |
|
---|
773 | const ast::Expr * visit( const ast::SizeofExpr * node ) override final {
|
---|
774 | assert (node->expr || node->type);
|
---|
775 | assert (! (node->expr && node->type));
|
---|
776 | SizeofExpr *rslt;
|
---|
777 | if (node->expr) {
|
---|
778 | rslt = new SizeofExpr(
|
---|
779 | get<Expression>().accept1(node->expr)
|
---|
780 | );
|
---|
781 | assert (!rslt->isType);
|
---|
782 | }
|
---|
783 | else {
|
---|
784 | assert(node->type);
|
---|
785 | rslt = new SizeofExpr(
|
---|
786 | get<Type>().accept1(node->type)
|
---|
787 | );
|
---|
788 | assert (rslt->isType);
|
---|
789 | }
|
---|
790 | auto expr = visitBaseExpr( node, rslt );
|
---|
791 | this->node = expr;
|
---|
792 | return nullptr;
|
---|
793 | }
|
---|
794 |
|
---|
795 | const ast::Expr * visit( const ast::AlignofExpr * node ) override final {
|
---|
796 | assert (node->expr || node->type);
|
---|
797 | assert (! (node->expr && node->type));
|
---|
798 | AlignofExpr *rslt;
|
---|
799 | if (node->expr) {
|
---|
800 | rslt = new AlignofExpr(
|
---|
801 | get<Expression>().accept1(node->expr)
|
---|
802 | );
|
---|
803 | assert (!rslt->isType);
|
---|
804 | }
|
---|
805 | else {
|
---|
806 | assert(node->type);
|
---|
807 | rslt = new AlignofExpr(
|
---|
808 | get<Type>().accept1(node->type)
|
---|
809 | );
|
---|
810 | assert (rslt->isType);
|
---|
811 | }
|
---|
812 | auto expr = visitBaseExpr( node, rslt );
|
---|
813 | this->node = expr;
|
---|
814 | return nullptr;
|
---|
815 | }
|
---|
816 |
|
---|
817 | const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final {
|
---|
818 | auto expr = visitBaseExpr( node,
|
---|
819 | new UntypedOffsetofExpr(
|
---|
820 | get<Type>().accept1(node->type),
|
---|
821 | node->member
|
---|
822 | )
|
---|
823 | );
|
---|
824 | this->node = expr;
|
---|
825 | return nullptr;
|
---|
826 | }
|
---|
827 |
|
---|
828 | const ast::Expr * visit( const ast::OffsetofExpr * node ) override final {
|
---|
829 | auto expr = visitBaseExpr( node,
|
---|
830 | new OffsetofExpr(
|
---|
831 | get<Type>().accept1(node->type),
|
---|
832 | get<DeclarationWithType>().accept1(node->member)
|
---|
833 | )
|
---|
834 | );
|
---|
835 | this->node = expr;
|
---|
836 | return nullptr;
|
---|
837 | }
|
---|
838 |
|
---|
839 | const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final {
|
---|
840 | auto expr = visitBaseExpr( node,
|
---|
841 | new OffsetPackExpr(
|
---|
842 | get<StructInstType>().accept1(node->type)
|
---|
843 | )
|
---|
844 | );
|
---|
845 | this->node = expr;
|
---|
846 | return nullptr;
|
---|
847 | }
|
---|
848 |
|
---|
849 | const ast::Expr * visit( const ast::LogicalExpr * node ) override final {
|
---|
850 | assert (node->isAnd == ast::LogicalFlag::AndExpr ||
|
---|
851 | node->isAnd == ast::LogicalFlag::OrExpr );
|
---|
852 | auto expr = visitBaseExpr( node,
|
---|
853 | new LogicalExpr(
|
---|
854 | get<Expression>().accept1(node->arg1),
|
---|
855 | get<Expression>().accept1(node->arg2),
|
---|
856 | (node->isAnd == ast::LogicalFlag::AndExpr)
|
---|
857 | )
|
---|
858 | );
|
---|
859 | this->node = expr;
|
---|
860 | return nullptr;
|
---|
861 | }
|
---|
862 |
|
---|
863 | const ast::Expr * visit( const ast::ConditionalExpr * node ) override final {
|
---|
864 | auto expr = visitBaseExpr( node,
|
---|
865 | new ConditionalExpr(
|
---|
866 | get<Expression>().accept1(node->arg1),
|
---|
867 | get<Expression>().accept1(node->arg2),
|
---|
868 | get<Expression>().accept1(node->arg3)
|
---|
869 | )
|
---|
870 | );
|
---|
871 | this->node = expr;
|
---|
872 | return nullptr;
|
---|
873 | }
|
---|
874 |
|
---|
875 | const ast::Expr * visit( const ast::CommaExpr * node ) override final {
|
---|
876 | auto expr = visitBaseExpr( node,
|
---|
877 | new CommaExpr(
|
---|
878 | get<Expression>().accept1(node->arg1),
|
---|
879 | get<Expression>().accept1(node->arg2)
|
---|
880 | )
|
---|
881 | );
|
---|
882 | this->node = expr;
|
---|
883 | return nullptr;
|
---|
884 | }
|
---|
885 |
|
---|
886 | const ast::Expr * visit( const ast::TypeExpr * node ) override final {
|
---|
887 | auto expr = visitBaseExpr( node,
|
---|
888 | new TypeExpr(
|
---|
889 | get<Type>().accept1(node->type)
|
---|
890 | )
|
---|
891 | );
|
---|
892 | this->node = expr;
|
---|
893 | return nullptr;
|
---|
894 | }
|
---|
895 |
|
---|
896 | const ast::Expr * visit( const ast::AsmExpr * node ) override final {
|
---|
897 | auto expr = visitBaseExpr( node,
|
---|
898 | new AsmExpr(
|
---|
899 | new std::string(node->inout),
|
---|
900 | get<Expression>().accept1(node->constraint),
|
---|
901 | get<Expression>().accept1(node->operand)
|
---|
902 | )
|
---|
903 | );
|
---|
904 | this->node = expr;
|
---|
905 | return nullptr;
|
---|
906 | }
|
---|
907 |
|
---|
908 | const ast::Expr * visit( const ast::ImplicitCopyCtorExpr * node ) override final {
|
---|
909 | auto rslt = new ImplicitCopyCtorExpr(
|
---|
910 | get<ApplicationExpr>().accept1(node->callExpr)
|
---|
911 | );
|
---|
912 |
|
---|
913 | auto expr = visitBaseExpr( node, rslt );
|
---|
914 | this->node = expr;
|
---|
915 | return nullptr;
|
---|
916 | }
|
---|
917 |
|
---|
918 | const ast::Expr * visit( const ast::ConstructorExpr * node ) override final {
|
---|
919 | auto expr = visitBaseExpr( node,
|
---|
920 | new ConstructorExpr(
|
---|
921 | get<Expression>().accept1(node->callExpr)
|
---|
922 | )
|
---|
923 | );
|
---|
924 | this->node = expr;
|
---|
925 | return nullptr;
|
---|
926 | }
|
---|
927 |
|
---|
928 | const ast::Expr * visit( const ast::CompoundLiteralExpr * node ) override final {
|
---|
929 | auto expr = visitBaseExpr_skipResultType( node,
|
---|
930 | new CompoundLiteralExpr(
|
---|
931 | get<Type>().accept1(node->result),
|
---|
932 | get<Initializer>().accept1(node->init)
|
---|
933 | )
|
---|
934 | );
|
---|
935 | this->node = expr;
|
---|
936 | return nullptr;
|
---|
937 | }
|
---|
938 |
|
---|
939 | const ast::Expr * visit( const ast::RangeExpr * node ) override final {
|
---|
940 | auto expr = visitBaseExpr( node,
|
---|
941 | new RangeExpr(
|
---|
942 | get<Expression>().accept1(node->low),
|
---|
943 | get<Expression>().accept1(node->high)
|
---|
944 | )
|
---|
945 | );
|
---|
946 | this->node = expr;
|
---|
947 | return nullptr;
|
---|
948 | }
|
---|
949 |
|
---|
950 | const ast::Expr * visit( const ast::UntypedTupleExpr * node ) override final {
|
---|
951 | auto expr = visitBaseExpr( node,
|
---|
952 | new UntypedTupleExpr(
|
---|
953 | get<Expression>().acceptL(node->exprs)
|
---|
954 | )
|
---|
955 | );
|
---|
956 | this->node = expr;
|
---|
957 | return nullptr;
|
---|
958 | }
|
---|
959 |
|
---|
960 | const ast::Expr * visit( const ast::TupleExpr * node ) override final {
|
---|
961 | auto expr = visitBaseExpr( node,
|
---|
962 | new TupleExpr(
|
---|
963 | get<Expression>().acceptL(node->exprs)
|
---|
964 | )
|
---|
965 | );
|
---|
966 | this->node = expr;
|
---|
967 | return nullptr;
|
---|
968 | }
|
---|
969 |
|
---|
970 | const ast::Expr * visit( const ast::TupleIndexExpr * node ) override final {
|
---|
971 | auto expr = visitBaseExpr( node,
|
---|
972 | new TupleIndexExpr(
|
---|
973 | get<Expression>().accept1(node->tuple),
|
---|
974 | node->index
|
---|
975 | )
|
---|
976 | );
|
---|
977 | this->node = expr;
|
---|
978 | return nullptr;
|
---|
979 | }
|
---|
980 |
|
---|
981 | const ast::Expr * visit( const ast::TupleAssignExpr * node ) override final {
|
---|
982 | auto expr = visitBaseExpr( node,
|
---|
983 | new TupleAssignExpr(
|
---|
984 | get<StmtExpr>().accept1(node->stmtExpr)
|
---|
985 | )
|
---|
986 | );
|
---|
987 | this->node = expr;
|
---|
988 | return nullptr;
|
---|
989 | }
|
---|
990 |
|
---|
991 | const ast::Expr * visit( const ast::StmtExpr * node ) override final {
|
---|
992 | auto rslt = new StmtExpr(
|
---|
993 | get<CompoundStmt>().accept1(node->stmts)
|
---|
994 | );
|
---|
995 |
|
---|
996 | rslt->returnDecls = get<ObjectDecl>().acceptL(node->returnDecls);
|
---|
997 | rslt->dtors = get<Expression>().acceptL(node->dtors);
|
---|
998 |
|
---|
999 | auto expr = visitBaseExpr( node, rslt );
|
---|
1000 | this->node = expr;
|
---|
1001 | return nullptr;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | const ast::Expr * visit( const ast::UniqueExpr * node ) override final {
|
---|
1005 | auto rslt = new UniqueExpr(
|
---|
1006 | get<Expression>().accept1(node->expr),
|
---|
1007 | node->id
|
---|
1008 | );
|
---|
1009 |
|
---|
1010 | rslt->object = get<ObjectDecl> ().accept1(node->object);
|
---|
1011 | rslt->var = get<VariableExpr>().accept1(node->var);
|
---|
1012 |
|
---|
1013 | auto expr = visitBaseExpr( node, rslt );
|
---|
1014 | this->node = expr;
|
---|
1015 | return nullptr;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | const ast::Expr * visit( const ast::UntypedInitExpr * node ) override final {
|
---|
1019 | std::list<InitAlternative> initAlts;
|
---|
1020 | for (auto ia : node->initAlts) {
|
---|
1021 | initAlts.push_back(InitAlternative(
|
---|
1022 | get<Type> ().accept1(ia.type),
|
---|
1023 | get<Designation>().accept1(ia.designation)
|
---|
1024 | ));
|
---|
1025 | }
|
---|
1026 | auto expr = visitBaseExpr( node,
|
---|
1027 | new UntypedInitExpr(
|
---|
1028 | get<Expression>().accept1(node->expr),
|
---|
1029 | initAlts
|
---|
1030 | )
|
---|
1031 | );
|
---|
1032 | this->node = expr;
|
---|
1033 | return nullptr;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | const ast::Expr * visit( const ast::InitExpr * node ) override final {
|
---|
1037 | auto expr = visitBaseExpr( node,
|
---|
1038 | new InitExpr(
|
---|
1039 | get<Expression>().accept1(node->expr),
|
---|
1040 | get<Designation>().accept1(node->designation)
|
---|
1041 | )
|
---|
1042 | );
|
---|
1043 | this->node = expr;
|
---|
1044 | return nullptr;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | const ast::Expr * visit( const ast::DeletedExpr * node ) override final {
|
---|
1048 | auto expr = visitBaseExpr( node,
|
---|
1049 | new DeletedExpr(
|
---|
1050 | get<Expression>().accept1(node->expr),
|
---|
1051 | inCache(node->deleteStmt) ?
|
---|
1052 | strict_dynamic_cast<Declaration*>(this->node) :
|
---|
1053 | get<Declaration>().accept1(node->deleteStmt)
|
---|
1054 | )
|
---|
1055 | );
|
---|
1056 | this->node = expr;
|
---|
1057 | return nullptr;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | const ast::Expr * visit( const ast::DefaultArgExpr * node ) override final {
|
---|
1061 | auto expr = visitBaseExpr( node,
|
---|
1062 | new DefaultArgExpr(
|
---|
1063 | get<Expression>().accept1(node->expr)
|
---|
1064 | )
|
---|
1065 | );
|
---|
1066 | this->node = expr;
|
---|
1067 | return nullptr;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | const ast::Expr * visit( const ast::GenericExpr * node ) override final {
|
---|
1071 | std::list<GenericExpr::Association> associations;
|
---|
1072 | for (auto association : node->associations) {
|
---|
1073 | associations.push_back(GenericExpr::Association(
|
---|
1074 | get<Type> ().accept1(association.type),
|
---|
1075 | get<Expression>().accept1(association.expr)
|
---|
1076 | ));
|
---|
1077 | }
|
---|
1078 | auto expr = visitBaseExpr( node,
|
---|
1079 | new GenericExpr(
|
---|
1080 | get<Expression>().accept1(node->control),
|
---|
1081 | associations
|
---|
1082 | )
|
---|
1083 | );
|
---|
1084 | this->node = expr;
|
---|
1085 | return nullptr;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | const ast::Type * visitType( const ast::Type * node, Type * type ) {
|
---|
1089 | // Some types do this in their constructor so add a check.
|
---|
1090 | if ( !node->attributes.empty() && type->attributes.empty() ) {
|
---|
1091 | type->attributes = get<Attribute>().acceptL( node->attributes );
|
---|
1092 | }
|
---|
1093 | this->node = type;
|
---|
1094 | return nullptr;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | const ast::Type * visit( const ast::VoidType * node ) override final {
|
---|
1098 | return visitType( node, new VoidType{ cv( node ) } );
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | const ast::Type * visit( const ast::BasicType * node ) override final {
|
---|
1102 | auto type = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };
|
---|
1103 | // I believe this should always be a BasicType.
|
---|
1104 | if ( sizeType == node ) {
|
---|
1105 | Validate::SizeType = type;
|
---|
1106 | }
|
---|
1107 | return visitType( node, type );
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | const ast::Type * visit( const ast::PointerType * node ) override final {
|
---|
1111 | return visitType( node, new PointerType{
|
---|
1112 | cv( node ),
|
---|
1113 | get<Type>().accept1( node->base ),
|
---|
1114 | get<Expression>().accept1( node->dimension ),
|
---|
1115 | (bool)node->isVarLen,
|
---|
1116 | (bool)node->isStatic
|
---|
1117 | } );
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | const ast::Type * visit( const ast::ArrayType * node ) override final {
|
---|
1121 | return visitType( node, new ArrayType{
|
---|
1122 | cv( node ),
|
---|
1123 | get<Type>().accept1( node->base ),
|
---|
1124 | get<Expression>().accept1( node->dimension ),
|
---|
1125 | (bool)node->isVarLen,
|
---|
1126 | (bool)node->isStatic
|
---|
1127 | } );
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | const ast::Type * visit( const ast::ReferenceType * node ) override final {
|
---|
1131 | return visitType( node, new ReferenceType{
|
---|
1132 | cv( node ),
|
---|
1133 | get<Type>().accept1( node->base )
|
---|
1134 | } );
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | const ast::Type * visit( const ast::QualifiedType * node ) override final {
|
---|
1138 | return visitType( node, new QualifiedType{
|
---|
1139 | cv( node ),
|
---|
1140 | get<Type>().accept1( node->parent ),
|
---|
1141 | get<Type>().accept1( node->child )
|
---|
1142 | } );
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | const ast::Type * visit( const ast::FunctionType * node ) override final {
|
---|
1146 | auto ty = new FunctionType {
|
---|
1147 | cv( node ),
|
---|
1148 | (bool)node->isVarArgs
|
---|
1149 | };
|
---|
1150 | ty->returnVals = get<DeclarationWithType>().acceptL( node->returns );
|
---|
1151 | ty->parameters = get<DeclarationWithType>().acceptL( node->params );
|
---|
1152 | ty->forall = get<TypeDecl>().acceptL( node->forall );
|
---|
1153 | return visitType( node, ty );
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | const ast::Type * postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {
|
---|
1157 | ty->forall = get<TypeDecl>().acceptL( old->forall );
|
---|
1158 | ty->parameters = get<Expression>().acceptL( old->params );
|
---|
1159 | ty->hoistType = old->hoistType;
|
---|
1160 | return visitType( old, ty );
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | const ast::Type * visit( const ast::StructInstType * node ) override final {
|
---|
1164 | StructInstType * ty;
|
---|
1165 | if ( node->base ) {
|
---|
1166 | ty = new StructInstType{
|
---|
1167 | cv( node ),
|
---|
1168 | get<StructDecl>().accept1( node->base ),
|
---|
1169 | get<Attribute>().acceptL( node->attributes )
|
---|
1170 | };
|
---|
1171 | } else {
|
---|
1172 | ty = new StructInstType{
|
---|
1173 | cv( node ),
|
---|
1174 | node->name,
|
---|
1175 | get<Attribute>().acceptL( node->attributes )
|
---|
1176 | };
|
---|
1177 | }
|
---|
1178 | return postvisit( node, ty );
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | const ast::Type * visit( const ast::UnionInstType * node ) override final {
|
---|
1182 | UnionInstType * ty;
|
---|
1183 | if ( node->base ) {
|
---|
1184 | ty = new UnionInstType{
|
---|
1185 | cv( node ),
|
---|
1186 | get<UnionDecl>().accept1( node->base ),
|
---|
1187 | get<Attribute>().acceptL( node->attributes )
|
---|
1188 | };
|
---|
1189 | } else {
|
---|
1190 | ty = new UnionInstType{
|
---|
1191 | cv( node ),
|
---|
1192 | node->name,
|
---|
1193 | get<Attribute>().acceptL( node->attributes )
|
---|
1194 | };
|
---|
1195 | }
|
---|
1196 | return postvisit( node, ty );
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | const ast::Type * visit( const ast::EnumInstType * node ) override final {
|
---|
1200 | EnumInstType * ty;
|
---|
1201 | if ( node->base ) {
|
---|
1202 | ty = new EnumInstType{
|
---|
1203 | cv( node ),
|
---|
1204 | get<EnumDecl>().accept1( node->base ),
|
---|
1205 | get<Attribute>().acceptL( node->attributes )
|
---|
1206 | };
|
---|
1207 | } else {
|
---|
1208 | ty = new EnumInstType{
|
---|
1209 | cv( node ),
|
---|
1210 | node->name,
|
---|
1211 | get<Attribute>().acceptL( node->attributes )
|
---|
1212 | };
|
---|
1213 | }
|
---|
1214 | return postvisit( node, ty );
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | const ast::Type * visit( const ast::TraitInstType * node ) override final {
|
---|
1218 | TraitInstType * ty;
|
---|
1219 | if ( node->base ) {
|
---|
1220 | ty = new TraitInstType{
|
---|
1221 | cv( node ),
|
---|
1222 | get<TraitDecl>().accept1( node->base ),
|
---|
1223 | get<Attribute>().acceptL( node->attributes )
|
---|
1224 | };
|
---|
1225 | } else {
|
---|
1226 | ty = new TraitInstType{
|
---|
1227 | cv( node ),
|
---|
1228 | node->name,
|
---|
1229 | get<Attribute>().acceptL( node->attributes )
|
---|
1230 | };
|
---|
1231 | }
|
---|
1232 | return postvisit( node, ty );
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | const ast::Type * visit( const ast::TypeInstType * node ) override final {
|
---|
1236 | TypeInstType * ty;
|
---|
1237 | if ( node->base ) {
|
---|
1238 | ty = new TypeInstType{
|
---|
1239 | cv( node ),
|
---|
1240 | node->name,
|
---|
1241 | get<TypeDecl>().accept1( node->base ),
|
---|
1242 | get<Attribute>().acceptL( node->attributes )
|
---|
1243 | };
|
---|
1244 | } else {
|
---|
1245 | ty = new TypeInstType{
|
---|
1246 | cv( node ),
|
---|
1247 | node->name,
|
---|
1248 | node->kind == ast::TypeDecl::Ftype,
|
---|
1249 | get<Attribute>().acceptL( node->attributes )
|
---|
1250 | };
|
---|
1251 | }
|
---|
1252 | return postvisit( node, ty );
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | const ast::Type * visit( const ast::TupleType * node ) override final {
|
---|
1256 | return visitType( node, new TupleType{
|
---|
1257 | cv( node ),
|
---|
1258 | get<Type>().acceptL( node->types )
|
---|
1259 | // members generated by TupleType c'tor
|
---|
1260 | } );
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | const ast::Type * visit( const ast::TypeofType * node ) override final {
|
---|
1264 | return visitType( node, new TypeofType{
|
---|
1265 | cv( node ),
|
---|
1266 | get<Expression>().accept1( node->expr ),
|
---|
1267 | (bool)node->kind
|
---|
1268 | } );
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | const ast::Type * visit( const ast::VarArgsType * node ) override final {
|
---|
1272 | return visitType( node, new VarArgsType{ cv( node ) } );
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | const ast::Type * visit( const ast::ZeroType * node ) override final {
|
---|
1276 | return visitType( node, new ZeroType{ cv( node ) } );
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | const ast::Type * visit( const ast::OneType * node ) override final {
|
---|
1280 | return visitType( node, new OneType{ cv( node ) } );
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | const ast::Type * visit( const ast::GlobalScopeType * node ) override final {
|
---|
1284 | return visitType( node, new GlobalScopeType{} );
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | const ast::Designation * visit( const ast::Designation * node ) override final {
|
---|
1288 | auto designation = new Designation( get<Expression>().acceptL( node->designators ) );
|
---|
1289 | designation->location = node->location;
|
---|
1290 | this->node = designation;
|
---|
1291 | return nullptr;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | const ast::Init * visit( const ast::SingleInit * node ) override final {
|
---|
1295 | auto init = new SingleInit(
|
---|
1296 | get<Expression>().accept1( node->value ),
|
---|
1297 | ast::MaybeConstruct == node->maybeConstructed
|
---|
1298 | );
|
---|
1299 | init->location = node->location;
|
---|
1300 | this->node = init;
|
---|
1301 | return nullptr;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | const ast::Init * visit( const ast::ListInit * node ) override final {
|
---|
1305 | auto init = new ListInit(
|
---|
1306 | get<Initializer>().acceptL( node->initializers ),
|
---|
1307 | get<Designation>().acceptL( node->designations ),
|
---|
1308 | ast::MaybeConstruct == node->maybeConstructed
|
---|
1309 | );
|
---|
1310 | init->location = node->location;
|
---|
1311 | this->node = init;
|
---|
1312 | return nullptr;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | const ast::Init * visit( const ast::ConstructorInit * node ) override final {
|
---|
1316 | auto init = new ConstructorInit(
|
---|
1317 | get<Statement>().accept1( node->ctor ),
|
---|
1318 | get<Statement>().accept1( node->dtor ),
|
---|
1319 | get<Initializer>().accept1( node->init )
|
---|
1320 | );
|
---|
1321 | init->location = node->location;
|
---|
1322 | this->node = init;
|
---|
1323 | return nullptr;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | const ast::Attribute * visit( const ast::Attribute * node ) override final {
|
---|
1327 | auto attr = new Attribute(
|
---|
1328 | node->name,
|
---|
1329 | get<Expression>().acceptL(node->params)
|
---|
1330 | );
|
---|
1331 | this->node = attr;
|
---|
1332 | return nullptr;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | const ast::TypeSubstitution * visit( const ast::TypeSubstitution * node ) override final {
|
---|
1336 | // Handled by convertTypeSubstitution helper instead.
|
---|
1337 | // TypeSubstitution is not a node in the old model, so the conversion result wouldn't fit in this->node.
|
---|
1338 | assert( 0 );
|
---|
1339 | (void)node;
|
---|
1340 | return nullptr;
|
---|
1341 | }
|
---|
1342 | };
|
---|
1343 |
|
---|
1344 | std::list< Declaration * > convert( const std::list< ast::ptr< ast::Decl > > && translationUnit ) {
|
---|
1345 | ConverterNewToOld c;
|
---|
1346 | std::list< Declaration * > decls;
|
---|
1347 | for(auto d : translationUnit) {
|
---|
1348 | decls.emplace_back( c.decl( d ) );
|
---|
1349 | }
|
---|
1350 | return decls;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | //================================================================================================
|
---|
1354 |
|
---|
1355 | class ConverterOldToNew : public Visitor {
|
---|
1356 | public:
|
---|
1357 | ast::Decl * decl() {
|
---|
1358 | return strict_dynamic_cast< ast::Decl * >( node );
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | ConverterOldToNew() = default;
|
---|
1362 | ConverterOldToNew(const ConverterOldToNew &) = delete;
|
---|
1363 | ConverterOldToNew(ConverterOldToNew &&) = delete;
|
---|
1364 | private:
|
---|
1365 | /// conversion output
|
---|
1366 | ast::Node * node = nullptr;
|
---|
1367 | /// cache of nodes that might be referenced by readonly<> for de-duplication
|
---|
1368 | std::unordered_map< const BaseSyntaxNode *, ast::Node * > cache = {};
|
---|
1369 |
|
---|
1370 | // Local Utilities:
|
---|
1371 |
|
---|
1372 | template<typename NewT, typename OldT>
|
---|
1373 | NewT * getAccept1( OldT old ) {
|
---|
1374 | if ( ! old ) return nullptr;
|
---|
1375 | old->accept(*this);
|
---|
1376 | ast::Node * ret = node;
|
---|
1377 | node = nullptr;
|
---|
1378 | return strict_dynamic_cast< NewT * >( ret );
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | # define GET_ACCEPT_1(child, type) \
|
---|
1382 | getAccept1< ast::type, decltype( old->child ) >( old->child )
|
---|
1383 |
|
---|
1384 | template<typename NewT, typename OldC>
|
---|
1385 | std::vector< ast::ptr<NewT> > getAcceptV( const OldC& old ) {
|
---|
1386 | std::vector< ast::ptr<NewT> > ret;
|
---|
1387 | ret.reserve( old.size() );
|
---|
1388 | for ( auto a : old ) {
|
---|
1389 | a->accept( *this );
|
---|
1390 | ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
|
---|
1391 | node = nullptr;
|
---|
1392 | }
|
---|
1393 | return ret;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | # define GET_ACCEPT_V(child, type) \
|
---|
1397 | getAcceptV< ast::type, decltype( old->child ) >( old->child )
|
---|
1398 |
|
---|
1399 | template<typename NewT, typename OldC>
|
---|
1400 | std::deque< ast::ptr<NewT> > getAcceptD( const OldC& old ) {
|
---|
1401 | std::deque< ast::ptr<NewT> > ret;
|
---|
1402 | for ( auto a : old ) {
|
---|
1403 | a->accept( *this );
|
---|
1404 | ret.emplace_back( strict_dynamic_cast< NewT * >(node) );
|
---|
1405 | node = nullptr;
|
---|
1406 | }
|
---|
1407 | return ret;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | # define GET_ACCEPT_D(child, type) \
|
---|
1411 | getAcceptD< ast::type, decltype( old->child ) >( old->child )
|
---|
1412 |
|
---|
1413 | ast::Label make_label(const Label* old) {
|
---|
1414 | CodeLocation const & location =
|
---|
1415 | ( old->labelled ) ? old->labelled->location : CodeLocation();
|
---|
1416 | return ast::Label(
|
---|
1417 | location,
|
---|
1418 | old->name,
|
---|
1419 | GET_ACCEPT_V(attributes, Attribute)
|
---|
1420 | );
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | template<template <class...> class C>
|
---|
1424 | C<ast::Label> make_labels(C<Label> olds) {
|
---|
1425 | C<ast::Label> ret;
|
---|
1426 | for (auto oldn : olds) {
|
---|
1427 | ret.push_back( make_label( &oldn ) );
|
---|
1428 | }
|
---|
1429 | return ret;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | # define GET_LABELS_V(labels) \
|
---|
1433 | to<std::vector>::from( make_labels( std::move( labels ) ) )
|
---|
1434 |
|
---|
1435 | static ast::CV::Qualifiers cv( const Type * ty ) { return { ty->tq.val }; }
|
---|
1436 |
|
---|
1437 | /// returns true and sets `node` if in cache
|
---|
1438 | bool inCache( const BaseSyntaxNode * old ) {
|
---|
1439 | auto it = cache.find( old );
|
---|
1440 | if ( it == cache.end() ) return false;
|
---|
1441 | node = it->second;
|
---|
1442 | return true;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | // Now all the visit functions:
|
---|
1446 |
|
---|
1447 | virtual void visit( const ObjectDecl * old ) override final {
|
---|
1448 | auto&& type = GET_ACCEPT_1(type, Type);
|
---|
1449 | auto&& init = GET_ACCEPT_1(init, Init);
|
---|
1450 | auto&& bfwd = GET_ACCEPT_1(bitfieldWidth, Expr);
|
---|
1451 | auto&& attr = GET_ACCEPT_V(attributes, Attribute);
|
---|
1452 | if ( inCache( old ) ) {
|
---|
1453 | return;
|
---|
1454 | }
|
---|
1455 | auto decl = new ast::ObjectDecl(
|
---|
1456 | old->location,
|
---|
1457 | old->name,
|
---|
1458 | type,
|
---|
1459 | init,
|
---|
1460 | { old->get_storageClasses().val },
|
---|
1461 | { old->linkage.val },
|
---|
1462 | bfwd,
|
---|
1463 | std::move(attr),
|
---|
1464 | { old->get_funcSpec().val }
|
---|
1465 | );
|
---|
1466 | cache.emplace(old, decl);
|
---|
1467 | assert(cache.find( old ) != cache.end());
|
---|
1468 | decl->scopeLevel = old->scopeLevel;
|
---|
1469 | decl->mangleName = old->mangleName;
|
---|
1470 | decl->isDeleted = old->isDeleted;
|
---|
1471 | decl->asmName = GET_ACCEPT_1(asmName, Expr);
|
---|
1472 | decl->uniqueId = old->uniqueId;
|
---|
1473 | decl->extension = old->extension;
|
---|
1474 |
|
---|
1475 | this->node = decl;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | virtual void visit( const FunctionDecl * old ) override final {
|
---|
1479 | if ( inCache( old ) ) return;
|
---|
1480 | auto decl = new ast::FunctionDecl{
|
---|
1481 | old->location,
|
---|
1482 | old->name,
|
---|
1483 | GET_ACCEPT_1(type, FunctionType),
|
---|
1484 | {},
|
---|
1485 | { old->storageClasses.val },
|
---|
1486 | { old->linkage.val },
|
---|
1487 | GET_ACCEPT_V(attributes, Attribute),
|
---|
1488 | { old->get_funcSpec().val }
|
---|
1489 | };
|
---|
1490 | cache.emplace( old, decl );
|
---|
1491 | decl->withExprs = GET_ACCEPT_V(withExprs, Expr);
|
---|
1492 | decl->stmts = GET_ACCEPT_1(statements, CompoundStmt);
|
---|
1493 | decl->scopeLevel = old->scopeLevel;
|
---|
1494 | decl->mangleName = old->mangleName;
|
---|
1495 | decl->isDeleted = old->isDeleted;
|
---|
1496 | decl->asmName = GET_ACCEPT_1(asmName, Expr);
|
---|
1497 | decl->uniqueId = old->uniqueId;
|
---|
1498 | decl->extension = old->extension;
|
---|
1499 |
|
---|
1500 | this->node = decl;
|
---|
1501 |
|
---|
1502 | if ( Validate::dereferenceOperator == old ) {
|
---|
1503 | dereferenceOperator = decl;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | if ( Validate::dtorStructDestroy == old ) {
|
---|
1507 | dtorStructDestroy = decl;
|
---|
1508 | }
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | virtual void visit( const StructDecl * old ) override final {
|
---|
1512 | if ( inCache( old ) ) return;
|
---|
1513 | auto decl = new ast::StructDecl(
|
---|
1514 | old->location,
|
---|
1515 | old->name,
|
---|
1516 | (ast::AggregateDecl::Aggregate)old->kind,
|
---|
1517 | GET_ACCEPT_V(attributes, Attribute),
|
---|
1518 | { old->linkage.val }
|
---|
1519 | );
|
---|
1520 | cache.emplace( old, decl );
|
---|
1521 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
|
---|
1522 | decl->body = old->body;
|
---|
1523 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1524 | decl->members = GET_ACCEPT_V(members, Decl);
|
---|
1525 | decl->extension = old->extension;
|
---|
1526 | decl->uniqueId = old->uniqueId;
|
---|
1527 | decl->storage = { old->storageClasses.val };
|
---|
1528 |
|
---|
1529 | this->node = decl;
|
---|
1530 |
|
---|
1531 | if ( Validate::dtorStruct == old ) {
|
---|
1532 | dtorStruct = decl;
|
---|
1533 | }
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | virtual void visit( const UnionDecl * old ) override final {
|
---|
1537 | if ( inCache( old ) ) return;
|
---|
1538 | auto decl = new ast::UnionDecl(
|
---|
1539 | old->location,
|
---|
1540 | old->name,
|
---|
1541 | GET_ACCEPT_V(attributes, Attribute),
|
---|
1542 | { old->linkage.val }
|
---|
1543 | );
|
---|
1544 | cache.emplace( old, decl );
|
---|
1545 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
|
---|
1546 | decl->body = old->body;
|
---|
1547 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1548 | decl->members = GET_ACCEPT_V(members, Decl);
|
---|
1549 | decl->extension = old->extension;
|
---|
1550 | decl->uniqueId = old->uniqueId;
|
---|
1551 | decl->storage = { old->storageClasses.val };
|
---|
1552 |
|
---|
1553 | this->node = decl;
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | virtual void visit( const EnumDecl * old ) override final {
|
---|
1557 | if ( inCache( old ) ) return;
|
---|
1558 | auto decl = new ast::EnumDecl(
|
---|
1559 | old->location,
|
---|
1560 | old->name,
|
---|
1561 | GET_ACCEPT_V(attributes, Attribute),
|
---|
1562 | { old->linkage.val }
|
---|
1563 | );
|
---|
1564 | cache.emplace( old, decl );
|
---|
1565 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
|
---|
1566 | decl->body = old->body;
|
---|
1567 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1568 | decl->members = GET_ACCEPT_V(members, Decl);
|
---|
1569 | decl->extension = old->extension;
|
---|
1570 | decl->uniqueId = old->uniqueId;
|
---|
1571 | decl->storage = { old->storageClasses.val };
|
---|
1572 |
|
---|
1573 | this->node = decl;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | virtual void visit( const TraitDecl * old ) override final {
|
---|
1577 | if ( inCache( old ) ) return;
|
---|
1578 | auto decl = new ast::TraitDecl(
|
---|
1579 | old->location,
|
---|
1580 | old->name,
|
---|
1581 | GET_ACCEPT_V(attributes, Attribute),
|
---|
1582 | { old->linkage.val }
|
---|
1583 | );
|
---|
1584 | cache.emplace( old, decl );
|
---|
1585 | decl->parent = GET_ACCEPT_1(parent, AggregateDecl);
|
---|
1586 | decl->body = old->body;
|
---|
1587 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1588 | decl->members = GET_ACCEPT_V(members, Decl);
|
---|
1589 | decl->extension = old->extension;
|
---|
1590 | decl->uniqueId = old->uniqueId;
|
---|
1591 | decl->storage = { old->storageClasses.val };
|
---|
1592 |
|
---|
1593 | this->node = decl;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | virtual void visit( const TypeDecl * old ) override final {
|
---|
1597 | if ( inCache( old ) ) return;
|
---|
1598 | auto decl = new ast::TypeDecl{
|
---|
1599 | old->location,
|
---|
1600 | old->name,
|
---|
1601 | { old->storageClasses.val },
|
---|
1602 | GET_ACCEPT_1(base, Type),
|
---|
1603 | (ast::TypeDecl::Kind)(unsigned)old->kind,
|
---|
1604 | old->sized,
|
---|
1605 | GET_ACCEPT_1(init, Type)
|
---|
1606 | };
|
---|
1607 | cache.emplace( old, decl );
|
---|
1608 | decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
|
---|
1609 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1610 | decl->extension = old->extension;
|
---|
1611 | decl->uniqueId = old->uniqueId;
|
---|
1612 |
|
---|
1613 | this->node = decl;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | virtual void visit( const TypedefDecl * old ) override final {
|
---|
1617 | auto decl = new ast::TypedefDecl(
|
---|
1618 | old->location,
|
---|
1619 | old->name,
|
---|
1620 | { old->storageClasses.val },
|
---|
1621 | GET_ACCEPT_1(base, Type),
|
---|
1622 | { old->linkage.val }
|
---|
1623 | );
|
---|
1624 | decl->assertions = GET_ACCEPT_V(assertions, DeclWithType);
|
---|
1625 | decl->params = GET_ACCEPT_V(parameters, TypeDecl);
|
---|
1626 | decl->extension = old->extension;
|
---|
1627 | decl->uniqueId = old->uniqueId;
|
---|
1628 | decl->storage = { old->storageClasses.val };
|
---|
1629 |
|
---|
1630 | this->node = decl;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | virtual void visit( const AsmDecl * old ) override final {
|
---|
1634 | auto decl = new ast::AsmDecl{
|
---|
1635 | old->location,
|
---|
1636 | GET_ACCEPT_1(stmt, AsmStmt)
|
---|
1637 | };
|
---|
1638 | decl->extension = old->extension;
|
---|
1639 | decl->uniqueId = old->uniqueId;
|
---|
1640 | decl->storage = { old->storageClasses.val };
|
---|
1641 |
|
---|
1642 | this->node = decl;
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | virtual void visit( const StaticAssertDecl * old ) override final {
|
---|
1646 | auto decl = new ast::StaticAssertDecl{
|
---|
1647 | old->location,
|
---|
1648 | GET_ACCEPT_1(condition, Expr),
|
---|
1649 | GET_ACCEPT_1(message, ConstantExpr)
|
---|
1650 | };
|
---|
1651 | decl->extension = old->extension;
|
---|
1652 | decl->uniqueId = old->uniqueId;
|
---|
1653 | decl->storage = { old->storageClasses.val };
|
---|
1654 |
|
---|
1655 | this->node = decl;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | virtual void visit( const CompoundStmt * old ) override final {
|
---|
1659 | if ( inCache( old ) ) return;
|
---|
1660 | auto stmt = new ast::CompoundStmt(
|
---|
1661 | old->location,
|
---|
1662 | to<std::list>::from( GET_ACCEPT_V(kids, Stmt) ),
|
---|
1663 | GET_LABELS_V(old->labels)
|
---|
1664 | );
|
---|
1665 |
|
---|
1666 | this->node = stmt;
|
---|
1667 | cache.emplace( old, this->node );
|
---|
1668 | }
|
---|
1669 |
|
---|
1670 | virtual void visit( const ExprStmt * old ) override final {
|
---|
1671 | if ( inCache( old ) ) return;
|
---|
1672 | this->node = new ast::ExprStmt(
|
---|
1673 | old->location,
|
---|
1674 | GET_ACCEPT_1(expr, Expr),
|
---|
1675 | GET_LABELS_V(old->labels)
|
---|
1676 | );
|
---|
1677 | cache.emplace( old, this->node );
|
---|
1678 | }
|
---|
1679 |
|
---|
1680 | virtual void visit( const AsmStmt * old ) override final {
|
---|
1681 | if ( inCache( old ) ) return;
|
---|
1682 | this->node = new ast::AsmStmt(
|
---|
1683 | old->location,
|
---|
1684 | old->voltile,
|
---|
1685 | GET_ACCEPT_1(instruction, Expr),
|
---|
1686 | GET_ACCEPT_V(output, Expr),
|
---|
1687 | GET_ACCEPT_V(input, Expr),
|
---|
1688 | GET_ACCEPT_V(clobber, ConstantExpr),
|
---|
1689 | GET_LABELS_V(old->gotolabels),
|
---|
1690 | GET_LABELS_V(old->labels)
|
---|
1691 | );
|
---|
1692 | cache.emplace( old, this->node );
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | virtual void visit( const DirectiveStmt * old ) override final {
|
---|
1696 | if ( inCache( old ) ) return;
|
---|
1697 | this->node = new ast::DirectiveStmt(
|
---|
1698 | old->location,
|
---|
1699 | old->directive,
|
---|
1700 | GET_LABELS_V(old->labels)
|
---|
1701 | );
|
---|
1702 | cache.emplace( old, this->node );
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | virtual void visit( const IfStmt * old ) override final {
|
---|
1706 | if ( inCache( old ) ) return;
|
---|
1707 | this->node = new ast::IfStmt(
|
---|
1708 | old->location,
|
---|
1709 | GET_ACCEPT_1(condition, Expr),
|
---|
1710 | GET_ACCEPT_1(thenPart, Stmt),
|
---|
1711 | GET_ACCEPT_1(elsePart, Stmt),
|
---|
1712 | GET_ACCEPT_V(initialization, Stmt),
|
---|
1713 | GET_LABELS_V(old->labels)
|
---|
1714 | );
|
---|
1715 | cache.emplace( old, this->node );
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | virtual void visit( const SwitchStmt * old ) override final {
|
---|
1719 | if ( inCache( old ) ) return;
|
---|
1720 | this->node = new ast::SwitchStmt(
|
---|
1721 | old->location,
|
---|
1722 | GET_ACCEPT_1(condition, Expr),
|
---|
1723 | GET_ACCEPT_V(statements, Stmt),
|
---|
1724 | GET_LABELS_V(old->labels)
|
---|
1725 | );
|
---|
1726 | cache.emplace( old, this->node );
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 | virtual void visit( const CaseStmt * old ) override final {
|
---|
1730 | if ( inCache( old ) ) return;
|
---|
1731 | this->node = new ast::CaseStmt(
|
---|
1732 | old->location,
|
---|
1733 | GET_ACCEPT_1(condition, Expr),
|
---|
1734 | GET_ACCEPT_V(stmts, Stmt),
|
---|
1735 | GET_LABELS_V(old->labels)
|
---|
1736 | );
|
---|
1737 | cache.emplace( old, this->node );
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | virtual void visit( const WhileStmt * old ) override final {
|
---|
1741 | if ( inCache( old ) ) return;
|
---|
1742 | this->node = new ast::WhileStmt(
|
---|
1743 | old->location,
|
---|
1744 | GET_ACCEPT_1(condition, Expr),
|
---|
1745 | GET_ACCEPT_1(body, Stmt),
|
---|
1746 | GET_ACCEPT_V(initialization, Stmt),
|
---|
1747 | old->isDoWhile,
|
---|
1748 | GET_LABELS_V(old->labels)
|
---|
1749 | );
|
---|
1750 | cache.emplace( old, this->node );
|
---|
1751 | }
|
---|
1752 |
|
---|
1753 | virtual void visit( const ForStmt * old ) override final {
|
---|
1754 | if ( inCache( old ) ) return;
|
---|
1755 | this->node = new ast::ForStmt(
|
---|
1756 | old->location,
|
---|
1757 | GET_ACCEPT_V(initialization, Stmt),
|
---|
1758 | GET_ACCEPT_1(condition, Expr),
|
---|
1759 | GET_ACCEPT_1(increment, Expr),
|
---|
1760 | GET_ACCEPT_1(body, Stmt),
|
---|
1761 | GET_LABELS_V(old->labels)
|
---|
1762 | );
|
---|
1763 | cache.emplace( old, this->node );
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | virtual void visit( const BranchStmt * old ) override final {
|
---|
1767 | if ( inCache( old ) ) return;
|
---|
1768 | if (old->computedTarget) {
|
---|
1769 | this->node = new ast::BranchStmt(
|
---|
1770 | old->location,
|
---|
1771 | GET_ACCEPT_1(computedTarget, Expr),
|
---|
1772 | GET_LABELS_V(old->labels)
|
---|
1773 | );
|
---|
1774 | } else {
|
---|
1775 | ast::BranchStmt::Kind kind;
|
---|
1776 | switch (old->type) {
|
---|
1777 | #define CASE(n) \
|
---|
1778 | case BranchStmt::n: \
|
---|
1779 | kind = ast::BranchStmt::n; \
|
---|
1780 | break
|
---|
1781 | CASE(Goto);
|
---|
1782 | CASE(Break);
|
---|
1783 | CASE(Continue);
|
---|
1784 | CASE(FallThrough);
|
---|
1785 | CASE(FallThroughDefault);
|
---|
1786 | #undef CASE
|
---|
1787 | default:
|
---|
1788 | assertf(false, "Invalid BranchStmt::Type %d\n", old->type);
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | auto stmt = new ast::BranchStmt(
|
---|
1792 | old->location,
|
---|
1793 | kind,
|
---|
1794 | make_label(&old->originalTarget),
|
---|
1795 | GET_LABELS_V(old->labels)
|
---|
1796 | );
|
---|
1797 | stmt->target = make_label(&old->target);
|
---|
1798 | this->node = stmt;
|
---|
1799 | }
|
---|
1800 | cache.emplace( old, this->node );
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | virtual void visit( const ReturnStmt * old ) override final {
|
---|
1804 | if ( inCache( old ) ) return;
|
---|
1805 | this->node = new ast::ReturnStmt(
|
---|
1806 | old->location,
|
---|
1807 | GET_ACCEPT_1(expr, Expr),
|
---|
1808 | GET_LABELS_V(old->labels)
|
---|
1809 | );
|
---|
1810 | cache.emplace( old, this->node );
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | virtual void visit( const ThrowStmt * old ) override final {
|
---|
1814 | if ( inCache( old ) ) return;
|
---|
1815 | ast::ExceptionKind kind;
|
---|
1816 | switch (old->kind) {
|
---|
1817 | case ThrowStmt::Terminate:
|
---|
1818 | kind = ast::ExceptionKind::Terminate;
|
---|
1819 | break;
|
---|
1820 | case ThrowStmt::Resume:
|
---|
1821 | kind = ast::ExceptionKind::Resume;
|
---|
1822 | break;
|
---|
1823 | default:
|
---|
1824 | assertf(false, "Invalid ThrowStmt::Kind %d\n", old->kind);
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 | this->node = new ast::ThrowStmt(
|
---|
1828 | old->location,
|
---|
1829 | kind,
|
---|
1830 | GET_ACCEPT_1(expr, Expr),
|
---|
1831 | GET_ACCEPT_1(target, Expr),
|
---|
1832 | GET_LABELS_V(old->labels)
|
---|
1833 | );
|
---|
1834 | cache.emplace( old, this->node );
|
---|
1835 | }
|
---|
1836 |
|
---|
1837 | virtual void visit( const TryStmt * old ) override final {
|
---|
1838 | if ( inCache( old ) ) return;
|
---|
1839 | this->node = new ast::TryStmt(
|
---|
1840 | old->location,
|
---|
1841 | GET_ACCEPT_1(block, CompoundStmt),
|
---|
1842 | GET_ACCEPT_V(handlers, CatchStmt),
|
---|
1843 | GET_ACCEPT_1(finallyBlock, FinallyStmt),
|
---|
1844 | GET_LABELS_V(old->labels)
|
---|
1845 | );
|
---|
1846 | cache.emplace( old, this->node );
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | virtual void visit( const CatchStmt * old ) override final {
|
---|
1850 | if ( inCache( old ) ) return;
|
---|
1851 | ast::ExceptionKind kind;
|
---|
1852 | switch (old->kind) {
|
---|
1853 | case CatchStmt::Terminate:
|
---|
1854 | kind = ast::ExceptionKind::Terminate;
|
---|
1855 | break;
|
---|
1856 | case CatchStmt::Resume:
|
---|
1857 | kind = ast::ExceptionKind::Resume;
|
---|
1858 | break;
|
---|
1859 | default:
|
---|
1860 | assertf(false, "Invalid CatchStmt::Kind %d\n", old->kind);
|
---|
1861 | }
|
---|
1862 |
|
---|
1863 | this->node = new ast::CatchStmt(
|
---|
1864 | old->location,
|
---|
1865 | kind,
|
---|
1866 | GET_ACCEPT_1(decl, Decl),
|
---|
1867 | GET_ACCEPT_1(cond, Expr),
|
---|
1868 | GET_ACCEPT_1(body, Stmt),
|
---|
1869 | GET_LABELS_V(old->labels)
|
---|
1870 | );
|
---|
1871 | cache.emplace( old, this->node );
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | virtual void visit( const FinallyStmt * old ) override final {
|
---|
1875 | if ( inCache( old ) ) return;
|
---|
1876 | this->node = new ast::FinallyStmt(
|
---|
1877 | old->location,
|
---|
1878 | GET_ACCEPT_1(block, CompoundStmt),
|
---|
1879 | GET_LABELS_V(old->labels)
|
---|
1880 | );
|
---|
1881 | cache.emplace( old, this->node );
|
---|
1882 | }
|
---|
1883 |
|
---|
1884 | virtual void visit( const SuspendStmt * old ) override final {
|
---|
1885 | if ( inCache( old ) ) return;
|
---|
1886 | ast::SuspendStmt::Type type;
|
---|
1887 | switch (old->type) {
|
---|
1888 | case SuspendStmt::Coroutine: type = ast::SuspendStmt::Coroutine; break;
|
---|
1889 | case SuspendStmt::Generator: type = ast::SuspendStmt::Generator; break;
|
---|
1890 | case SuspendStmt::None : type = ast::SuspendStmt::None ; break;
|
---|
1891 | default: abort();
|
---|
1892 | }
|
---|
1893 | this->node = new ast::SuspendStmt(
|
---|
1894 | old->location,
|
---|
1895 | GET_ACCEPT_1(then , CompoundStmt),
|
---|
1896 | type,
|
---|
1897 | GET_LABELS_V(old->labels)
|
---|
1898 | );
|
---|
1899 | cache.emplace( old, this->node );
|
---|
1900 | }
|
---|
1901 |
|
---|
1902 | virtual void visit( const WaitForStmt * old ) override final {
|
---|
1903 | if ( inCache( old ) ) return;
|
---|
1904 | ast::WaitForStmt * stmt = new ast::WaitForStmt(
|
---|
1905 | old->location,
|
---|
1906 | GET_LABELS_V(old->labels)
|
---|
1907 | );
|
---|
1908 |
|
---|
1909 | stmt->clauses.reserve( old->clauses.size() );
|
---|
1910 | for (size_t i = 0 ; i < old->clauses.size() ; ++i) {
|
---|
1911 | stmt->clauses.push_back({
|
---|
1912 | ast::WaitForStmt::Target{
|
---|
1913 | GET_ACCEPT_1(clauses[i].target.function, Expr),
|
---|
1914 | GET_ACCEPT_V(clauses[i].target.arguments, Expr)
|
---|
1915 | },
|
---|
1916 | GET_ACCEPT_1(clauses[i].statement, Stmt),
|
---|
1917 | GET_ACCEPT_1(clauses[i].condition, Expr)
|
---|
1918 | });
|
---|
1919 | }
|
---|
1920 | stmt->timeout = {
|
---|
1921 | GET_ACCEPT_1(timeout.time, Expr),
|
---|
1922 | GET_ACCEPT_1(timeout.statement, Stmt),
|
---|
1923 | GET_ACCEPT_1(timeout.condition, Expr),
|
---|
1924 | };
|
---|
1925 | stmt->orElse = {
|
---|
1926 | GET_ACCEPT_1(orelse.statement, Stmt),
|
---|
1927 | GET_ACCEPT_1(orelse.condition, Expr),
|
---|
1928 | };
|
---|
1929 |
|
---|
1930 | this->node = stmt;
|
---|
1931 | cache.emplace( old, this->node );
|
---|
1932 | }
|
---|
1933 |
|
---|
1934 | virtual void visit( const WithStmt * old ) override final {
|
---|
1935 | if ( inCache( old ) ) return;
|
---|
1936 | this->node = new ast::WithStmt(
|
---|
1937 | old->location,
|
---|
1938 | GET_ACCEPT_V(exprs, Expr),
|
---|
1939 | GET_ACCEPT_1(stmt, Stmt)
|
---|
1940 | );
|
---|
1941 | cache.emplace( old, this->node );
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | virtual void visit( const NullStmt * old ) override final {
|
---|
1945 | if ( inCache( old ) ) return;
|
---|
1946 | this->node = new ast::NullStmt(
|
---|
1947 | old->location,
|
---|
1948 | GET_LABELS_V(old->labels)
|
---|
1949 | );
|
---|
1950 | cache.emplace( old, this->node );
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 | virtual void visit( const DeclStmt * old ) override final {
|
---|
1954 | if ( inCache( old ) ) return;
|
---|
1955 | this->node = new ast::DeclStmt(
|
---|
1956 | old->location,
|
---|
1957 | GET_ACCEPT_1(decl, Decl),
|
---|
1958 | GET_LABELS_V(old->labels)
|
---|
1959 | );
|
---|
1960 | cache.emplace( old, this->node );
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | virtual void visit( const ImplicitCtorDtorStmt * old ) override final {
|
---|
1964 | if ( inCache( old ) ) return;
|
---|
1965 | auto stmt = new ast::ImplicitCtorDtorStmt(
|
---|
1966 | old->location,
|
---|
1967 | nullptr,
|
---|
1968 | GET_LABELS_V(old->labels)
|
---|
1969 | );
|
---|
1970 | cache.emplace( old, stmt );
|
---|
1971 | stmt->callStmt = GET_ACCEPT_1(callStmt, Stmt);
|
---|
1972 | this->node = stmt;
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 | ast::TypeSubstitution * convertTypeSubstitution(const TypeSubstitution * old) {
|
---|
1976 |
|
---|
1977 | if (!old) return nullptr;
|
---|
1978 |
|
---|
1979 | ast::TypeSubstitution *rslt = new ast::TypeSubstitution();
|
---|
1980 |
|
---|
1981 | for (decltype(old->begin()) old_i = old->begin(); old_i != old->end(); old_i++) {
|
---|
1982 | rslt->add( old_i->first,
|
---|
1983 | getAccept1<ast::Type>(old_i->second) );
|
---|
1984 | }
|
---|
1985 |
|
---|
1986 | for (decltype(old->beginVar()) old_i = old->beginVar(); old_i != old->endVar(); old_i++) {
|
---|
1987 | rslt->addVar( old_i->first,
|
---|
1988 | getAccept1<ast::Expr>(old_i->second) );
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | return rslt;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | void convertInferUnion(ast::Expr::InferUnion &newInferred,
|
---|
1995 | const std::map<UniqueId,ParamEntry> &oldInferParams,
|
---|
1996 | const std::vector<UniqueId> &oldResnSlots) {
|
---|
1997 |
|
---|
1998 | assert( oldInferParams.empty() || oldResnSlots.empty() );
|
---|
1999 | assert( newInferred.mode == ast::Expr::InferUnion::Empty );
|
---|
2000 |
|
---|
2001 | if ( !oldInferParams.empty() ) {
|
---|
2002 | ast::InferredParams &tgt = newInferred.inferParams();
|
---|
2003 | for (auto & old : oldInferParams) {
|
---|
2004 | tgt[old.first] = ast::ParamEntry(
|
---|
2005 | old.second.decl,
|
---|
2006 | getAccept1<ast::Decl>(old.second.declptr),
|
---|
2007 | getAccept1<ast::Type>(old.second.actualType),
|
---|
2008 | getAccept1<ast::Type>(old.second.formalType),
|
---|
2009 | getAccept1<ast::Expr>(old.second.expr)
|
---|
2010 | );
|
---|
2011 | }
|
---|
2012 | } else if ( !oldResnSlots.empty() ) {
|
---|
2013 | ast::ResnSlots &tgt = newInferred.resnSlots();
|
---|
2014 | for (auto old : oldResnSlots) {
|
---|
2015 | tgt.push_back(old);
|
---|
2016 | }
|
---|
2017 | }
|
---|
2018 | }
|
---|
2019 |
|
---|
2020 | ast::Expr * visitBaseExpr_SkipResultType( const Expression * old, ast::Expr * nw) {
|
---|
2021 |
|
---|
2022 | nw->env = convertTypeSubstitution(old->env);
|
---|
2023 |
|
---|
2024 | nw->extension = old->extension;
|
---|
2025 | convertInferUnion(nw->inferred, old->inferParams, old->resnSlots);
|
---|
2026 |
|
---|
2027 | return nw;
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | ast::Expr * visitBaseExpr( const Expression * old, ast::Expr * nw) {
|
---|
2031 |
|
---|
2032 | nw->result = GET_ACCEPT_1(result, Type);
|
---|
2033 | return visitBaseExpr_SkipResultType(old, nw);;
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | virtual void visit( const ApplicationExpr * old ) override final {
|
---|
2037 | this->node = visitBaseExpr( old,
|
---|
2038 | new ast::ApplicationExpr(
|
---|
2039 | old->location,
|
---|
2040 | GET_ACCEPT_1(function, Expr),
|
---|
2041 | GET_ACCEPT_V(args, Expr)
|
---|
2042 | )
|
---|
2043 | );
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | virtual void visit( const UntypedExpr * old ) override final {
|
---|
2047 | this->node = visitBaseExpr( old,
|
---|
2048 | new ast::UntypedExpr(
|
---|
2049 | old->location,
|
---|
2050 | GET_ACCEPT_1(function, Expr),
|
---|
2051 | GET_ACCEPT_V(args, Expr)
|
---|
2052 | )
|
---|
2053 | );
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | virtual void visit( const NameExpr * old ) override final {
|
---|
2057 | this->node = visitBaseExpr( old,
|
---|
2058 | new ast::NameExpr(
|
---|
2059 | old->location,
|
---|
2060 | old->get_name()
|
---|
2061 | )
|
---|
2062 | );
|
---|
2063 | }
|
---|
2064 |
|
---|
2065 | virtual void visit( const CastExpr * old ) override final {
|
---|
2066 | this->node = visitBaseExpr( old,
|
---|
2067 | new ast::CastExpr(
|
---|
2068 | old->location,
|
---|
2069 | GET_ACCEPT_1(arg, Expr),
|
---|
2070 | old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast
|
---|
2071 | )
|
---|
2072 | );
|
---|
2073 | }
|
---|
2074 |
|
---|
2075 | virtual void visit( const KeywordCastExpr * old ) override final {
|
---|
2076 | ast::AggregateDecl::Aggregate castTarget = (ast::AggregateDecl::Aggregate)old->target;
|
---|
2077 | assert( ast::AggregateDecl::Generator <= castTarget && castTarget <= ast::AggregateDecl::Thread );
|
---|
2078 | this->node = visitBaseExpr( old,
|
---|
2079 | new ast::KeywordCastExpr(
|
---|
2080 | old->location,
|
---|
2081 | GET_ACCEPT_1(arg, Expr),
|
---|
2082 | castTarget
|
---|
2083 | )
|
---|
2084 | );
|
---|
2085 | }
|
---|
2086 |
|
---|
2087 | virtual void visit( const VirtualCastExpr * old ) override final {
|
---|
2088 | this->node = visitBaseExpr_SkipResultType( old,
|
---|
2089 | new ast::VirtualCastExpr(
|
---|
2090 | old->location,
|
---|
2091 | GET_ACCEPT_1(arg, Expr),
|
---|
2092 | GET_ACCEPT_1(result, Type)
|
---|
2093 | )
|
---|
2094 | );
|
---|
2095 | }
|
---|
2096 |
|
---|
2097 | virtual void visit( const AddressExpr * old ) override final {
|
---|
2098 | this->node = visitBaseExpr( old,
|
---|
2099 | new ast::AddressExpr(
|
---|
2100 | old->location,
|
---|
2101 | GET_ACCEPT_1(arg, Expr)
|
---|
2102 | )
|
---|
2103 | );
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | virtual void visit( const LabelAddressExpr * old ) override final {
|
---|
2107 | this->node = visitBaseExpr( old,
|
---|
2108 | new ast::LabelAddressExpr(
|
---|
2109 | old->location,
|
---|
2110 | make_label(&old->arg)
|
---|
2111 | )
|
---|
2112 | );
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | virtual void visit( const UntypedMemberExpr * old ) override final {
|
---|
2116 | this->node = visitBaseExpr( old,
|
---|
2117 | new ast::UntypedMemberExpr(
|
---|
2118 | old->location,
|
---|
2119 | GET_ACCEPT_1(member, Expr),
|
---|
2120 | GET_ACCEPT_1(aggregate, Expr)
|
---|
2121 | )
|
---|
2122 | );
|
---|
2123 | }
|
---|
2124 |
|
---|
2125 | virtual void visit( const MemberExpr * old ) override final {
|
---|
2126 | this->node = visitBaseExpr( old,
|
---|
2127 | new ast::MemberExpr(
|
---|
2128 | old->location,
|
---|
2129 | GET_ACCEPT_1(member, DeclWithType),
|
---|
2130 | GET_ACCEPT_1(aggregate, Expr),
|
---|
2131 | ast::MemberExpr::NoOpConstructionChosen
|
---|
2132 | )
|
---|
2133 | );
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | virtual void visit( const VariableExpr * old ) override final {
|
---|
2137 | auto expr = new ast::VariableExpr(
|
---|
2138 | old->location
|
---|
2139 | );
|
---|
2140 |
|
---|
2141 | expr->var = GET_ACCEPT_1(var, DeclWithType);
|
---|
2142 | visitBaseExpr( old, expr );
|
---|
2143 |
|
---|
2144 | this->node = expr;
|
---|
2145 | }
|
---|
2146 |
|
---|
2147 | virtual void visit( const ConstantExpr * old ) override final {
|
---|
2148 | ast::ConstantExpr *rslt = new ast::ConstantExpr(
|
---|
2149 | old->location,
|
---|
2150 | GET_ACCEPT_1(result, Type),
|
---|
2151 | old->constant.rep,
|
---|
2152 | old->constant.ival
|
---|
2153 | );
|
---|
2154 | rslt->underlyer = getAccept1< ast::Type, Type* >( old->constant.type );
|
---|
2155 | this->node = visitBaseExpr( old, rslt );
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | virtual void visit( const SizeofExpr * old ) override final {
|
---|
2159 | assert (old->expr || old->type);
|
---|
2160 | assert (! (old->expr && old->type));
|
---|
2161 | ast::SizeofExpr *rslt;
|
---|
2162 | if (old->expr) {
|
---|
2163 | assert(!old->isType);
|
---|
2164 | rslt = new ast::SizeofExpr(
|
---|
2165 | old->location,
|
---|
2166 | GET_ACCEPT_1(expr, Expr)
|
---|
2167 | );
|
---|
2168 | }
|
---|
2169 | if (old->type) {
|
---|
2170 | assert(old->isType);
|
---|
2171 | rslt = new ast::SizeofExpr(
|
---|
2172 | old->location,
|
---|
2173 | GET_ACCEPT_1(type, Type)
|
---|
2174 | );
|
---|
2175 | }
|
---|
2176 | this->node = visitBaseExpr( old, rslt );
|
---|
2177 | }
|
---|
2178 |
|
---|
2179 | virtual void visit( const AlignofExpr * old ) override final {
|
---|
2180 | assert (old->expr || old->type);
|
---|
2181 | assert (! (old->expr && old->type));
|
---|
2182 | ast::AlignofExpr *rslt;
|
---|
2183 | if (old->expr) {
|
---|
2184 | assert(!old->isType);
|
---|
2185 | rslt = new ast::AlignofExpr(
|
---|
2186 | old->location,
|
---|
2187 | GET_ACCEPT_1(expr, Expr)
|
---|
2188 | );
|
---|
2189 | }
|
---|
2190 | if (old->type) {
|
---|
2191 | assert(old->isType);
|
---|
2192 | rslt = new ast::AlignofExpr(
|
---|
2193 | old->location,
|
---|
2194 | GET_ACCEPT_1(type, Type)
|
---|
2195 | );
|
---|
2196 | }
|
---|
2197 | this->node = visitBaseExpr( old, rslt );
|
---|
2198 | }
|
---|
2199 |
|
---|
2200 | virtual void visit( const UntypedOffsetofExpr * old ) override final {
|
---|
2201 | this->node = visitBaseExpr( old,
|
---|
2202 | new ast::UntypedOffsetofExpr(
|
---|
2203 | old->location,
|
---|
2204 | GET_ACCEPT_1(type, Type),
|
---|
2205 | old->member
|
---|
2206 | )
|
---|
2207 | );
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 | virtual void visit( const OffsetofExpr * old ) override final {
|
---|
2211 | this->node = visitBaseExpr( old,
|
---|
2212 | new ast::OffsetofExpr(
|
---|
2213 | old->location,
|
---|
2214 | GET_ACCEPT_1(type, Type),
|
---|
2215 | GET_ACCEPT_1(member, DeclWithType)
|
---|
2216 | )
|
---|
2217 | );
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | virtual void visit( const OffsetPackExpr * old ) override final {
|
---|
2221 | this->node = visitBaseExpr( old,
|
---|
2222 | new ast::OffsetPackExpr(
|
---|
2223 | old->location,
|
---|
2224 | GET_ACCEPT_1(type, StructInstType)
|
---|
2225 | )
|
---|
2226 | );
|
---|
2227 | }
|
---|
2228 |
|
---|
2229 | virtual void visit( const LogicalExpr * old ) override final {
|
---|
2230 | this->node = visitBaseExpr( old,
|
---|
2231 | new ast::LogicalExpr(
|
---|
2232 | old->location,
|
---|
2233 | GET_ACCEPT_1(arg1, Expr),
|
---|
2234 | GET_ACCEPT_1(arg2, Expr),
|
---|
2235 | old->get_isAnd() ?
|
---|
2236 | ast::LogicalFlag::AndExpr :
|
---|
2237 | ast::LogicalFlag::OrExpr
|
---|
2238 | )
|
---|
2239 | );
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | virtual void visit( const ConditionalExpr * old ) override final {
|
---|
2243 | this->node = visitBaseExpr( old,
|
---|
2244 | new ast::ConditionalExpr(
|
---|
2245 | old->location,
|
---|
2246 | GET_ACCEPT_1(arg1, Expr),
|
---|
2247 | GET_ACCEPT_1(arg2, Expr),
|
---|
2248 | GET_ACCEPT_1(arg3, Expr)
|
---|
2249 | )
|
---|
2250 | );
|
---|
2251 | }
|
---|
2252 |
|
---|
2253 | virtual void visit( const CommaExpr * old ) override final {
|
---|
2254 | this->node = visitBaseExpr( old,
|
---|
2255 | new ast::CommaExpr(
|
---|
2256 | old->location,
|
---|
2257 | GET_ACCEPT_1(arg1, Expr),
|
---|
2258 | GET_ACCEPT_1(arg2, Expr)
|
---|
2259 | )
|
---|
2260 | );
|
---|
2261 | }
|
---|
2262 |
|
---|
2263 | virtual void visit( const TypeExpr * old ) override final {
|
---|
2264 | this->node = visitBaseExpr( old,
|
---|
2265 | new ast::TypeExpr(
|
---|
2266 | old->location,
|
---|
2267 | GET_ACCEPT_1(type, Type)
|
---|
2268 | )
|
---|
2269 | );
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | virtual void visit( const AsmExpr * old ) override final {
|
---|
2273 | this->node = visitBaseExpr( old,
|
---|
2274 | new ast::AsmExpr(
|
---|
2275 | old->location,
|
---|
2276 | old->inout,
|
---|
2277 | GET_ACCEPT_1(constraint, Expr),
|
---|
2278 | GET_ACCEPT_1(operand, Expr)
|
---|
2279 | )
|
---|
2280 | );
|
---|
2281 | }
|
---|
2282 |
|
---|
2283 | virtual void visit( const ImplicitCopyCtorExpr * old ) override final {
|
---|
2284 | auto rslt = new ast::ImplicitCopyCtorExpr(
|
---|
2285 | old->location,
|
---|
2286 | GET_ACCEPT_1(callExpr, ApplicationExpr)
|
---|
2287 | );
|
---|
2288 |
|
---|
2289 | this->node = visitBaseExpr( old, rslt );
|
---|
2290 | }
|
---|
2291 |
|
---|
2292 | virtual void visit( const ConstructorExpr * old ) override final {
|
---|
2293 | this->node = visitBaseExpr( old,
|
---|
2294 | new ast::ConstructorExpr(
|
---|
2295 | old->location,
|
---|
2296 | GET_ACCEPT_1(callExpr, Expr)
|
---|
2297 | )
|
---|
2298 | );
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 | virtual void visit( const CompoundLiteralExpr * old ) override final {
|
---|
2302 | this->node = visitBaseExpr_SkipResultType( old,
|
---|
2303 | new ast::CompoundLiteralExpr(
|
---|
2304 | old->location,
|
---|
2305 | GET_ACCEPT_1(result, Type),
|
---|
2306 | GET_ACCEPT_1(initializer, Init)
|
---|
2307 | )
|
---|
2308 | );
|
---|
2309 | }
|
---|
2310 |
|
---|
2311 | virtual void visit( const RangeExpr * old ) override final {
|
---|
2312 | this->node = visitBaseExpr( old,
|
---|
2313 | new ast::RangeExpr(
|
---|
2314 | old->location,
|
---|
2315 | GET_ACCEPT_1(low, Expr),
|
---|
2316 | GET_ACCEPT_1(high, Expr)
|
---|
2317 | )
|
---|
2318 | );
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | virtual void visit( const UntypedTupleExpr * old ) override final {
|
---|
2322 | this->node = visitBaseExpr( old,
|
---|
2323 | new ast::UntypedTupleExpr(
|
---|
2324 | old->location,
|
---|
2325 | GET_ACCEPT_V(exprs, Expr)
|
---|
2326 | )
|
---|
2327 | );
|
---|
2328 | }
|
---|
2329 |
|
---|
2330 | virtual void visit( const TupleExpr * old ) override final {
|
---|
2331 | this->node = visitBaseExpr( old,
|
---|
2332 | new ast::TupleExpr(
|
---|
2333 | old->location,
|
---|
2334 | GET_ACCEPT_V(exprs, Expr)
|
---|
2335 | )
|
---|
2336 | );
|
---|
2337 | }
|
---|
2338 |
|
---|
2339 | virtual void visit( const TupleIndexExpr * old ) override final {
|
---|
2340 | this->node = visitBaseExpr( old,
|
---|
2341 | new ast::TupleIndexExpr(
|
---|
2342 | old->location,
|
---|
2343 | GET_ACCEPT_1(tuple, Expr),
|
---|
2344 | old->index
|
---|
2345 | )
|
---|
2346 | );
|
---|
2347 | }
|
---|
2348 |
|
---|
2349 | virtual void visit( const TupleAssignExpr * old ) override final {
|
---|
2350 | this->node = visitBaseExpr_SkipResultType( old,
|
---|
2351 | new ast::TupleAssignExpr(
|
---|
2352 | old->location,
|
---|
2353 | GET_ACCEPT_1(result, Type),
|
---|
2354 | GET_ACCEPT_1(stmtExpr, StmtExpr)
|
---|
2355 | )
|
---|
2356 | );
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 | virtual void visit( const StmtExpr * old ) override final {
|
---|
2360 | auto rslt = new ast::StmtExpr(
|
---|
2361 | old->location,
|
---|
2362 | GET_ACCEPT_1(statements, CompoundStmt)
|
---|
2363 | );
|
---|
2364 | rslt->returnDecls = GET_ACCEPT_V(returnDecls, ObjectDecl);
|
---|
2365 | rslt->dtors = GET_ACCEPT_V(dtors , Expr);
|
---|
2366 |
|
---|
2367 | this->node = visitBaseExpr_SkipResultType( old, rslt );
|
---|
2368 | }
|
---|
2369 |
|
---|
2370 | virtual void visit( const UniqueExpr * old ) override final {
|
---|
2371 | auto rslt = new ast::UniqueExpr(
|
---|
2372 | old->location,
|
---|
2373 | GET_ACCEPT_1(expr, Expr),
|
---|
2374 | old->get_id()
|
---|
2375 | );
|
---|
2376 | rslt->object = GET_ACCEPT_1(object, ObjectDecl);
|
---|
2377 | rslt->var = GET_ACCEPT_1(var , VariableExpr);
|
---|
2378 |
|
---|
2379 | this->node = visitBaseExpr( old, rslt );
|
---|
2380 | }
|
---|
2381 |
|
---|
2382 | virtual void visit( const UntypedInitExpr * old ) override final {
|
---|
2383 | std::deque<ast::InitAlternative> initAlts;
|
---|
2384 | for (auto ia : old->initAlts) {
|
---|
2385 | initAlts.push_back(ast::InitAlternative(
|
---|
2386 | getAccept1< ast::Type, Type * >( ia.type ),
|
---|
2387 | getAccept1< ast::Designation, Designation * >( ia.designation )
|
---|
2388 | ));
|
---|
2389 | }
|
---|
2390 | this->node = visitBaseExpr( old,
|
---|
2391 | new ast::UntypedInitExpr(
|
---|
2392 | old->location,
|
---|
2393 | GET_ACCEPT_1(expr, Expr),
|
---|
2394 | std::move(initAlts)
|
---|
2395 | )
|
---|
2396 | );
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | virtual void visit( const InitExpr * old ) override final {
|
---|
2400 | this->node = visitBaseExpr( old,
|
---|
2401 | new ast::InitExpr(
|
---|
2402 | old->location,
|
---|
2403 | GET_ACCEPT_1(expr, Expr),
|
---|
2404 | GET_ACCEPT_1(designation, Designation)
|
---|
2405 | )
|
---|
2406 | );
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | virtual void visit( const DeletedExpr * old ) override final {
|
---|
2410 | this->node = visitBaseExpr( old,
|
---|
2411 | new ast::DeletedExpr(
|
---|
2412 | old->location,
|
---|
2413 | GET_ACCEPT_1(expr, Expr),
|
---|
2414 | inCache(old->deleteStmt) ?
|
---|
2415 | strict_dynamic_cast<ast::Decl*>(this->node) :
|
---|
2416 | GET_ACCEPT_1(deleteStmt, Decl)
|
---|
2417 | )
|
---|
2418 | );
|
---|
2419 | }
|
---|
2420 |
|
---|
2421 | virtual void visit( const DefaultArgExpr * old ) override final {
|
---|
2422 | this->node = visitBaseExpr( old,
|
---|
2423 | new ast::DefaultArgExpr(
|
---|
2424 | old->location,
|
---|
2425 | GET_ACCEPT_1(expr, Expr)
|
---|
2426 | )
|
---|
2427 | );
|
---|
2428 | }
|
---|
2429 |
|
---|
2430 | virtual void visit( const GenericExpr * old ) override final {
|
---|
2431 | std::vector<ast::GenericExpr::Association> associations;
|
---|
2432 | for (auto association : old->associations) {
|
---|
2433 | associations.push_back(ast::GenericExpr::Association(
|
---|
2434 | getAccept1< ast::Type, Type * >( association.type ),
|
---|
2435 | getAccept1< ast::Expr, Expression * >( association.expr )
|
---|
2436 | ));
|
---|
2437 | }
|
---|
2438 | this->node = visitBaseExpr( old,
|
---|
2439 | new ast::GenericExpr(
|
---|
2440 | old->location,
|
---|
2441 | GET_ACCEPT_1(control, Expr),
|
---|
2442 | std::move(associations)
|
---|
2443 | )
|
---|
2444 | );
|
---|
2445 | }
|
---|
2446 |
|
---|
2447 | void visitType( const Type * old, ast::Type * type ) {
|
---|
2448 | // Some types do this in their constructor so add a check.
|
---|
2449 | if ( !old->attributes.empty() && type->attributes.empty() ) {
|
---|
2450 | type->attributes = GET_ACCEPT_V(attributes, Attribute);
|
---|
2451 | }
|
---|
2452 | this->node = type;
|
---|
2453 | }
|
---|
2454 |
|
---|
2455 | virtual void visit( const VoidType * old ) override final {
|
---|
2456 | visitType( old, new ast::VoidType{ cv( old ) } );
|
---|
2457 | }
|
---|
2458 |
|
---|
2459 | virtual void visit( const BasicType * old ) override final {
|
---|
2460 | auto type = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) };
|
---|
2461 | // I believe this should always be a BasicType.
|
---|
2462 | if ( Validate::SizeType == old ) {
|
---|
2463 | sizeType = type;
|
---|
2464 | }
|
---|
2465 | visitType( old, type );
|
---|
2466 | }
|
---|
2467 |
|
---|
2468 | virtual void visit( const PointerType * old ) override final {
|
---|
2469 | visitType( old, new ast::PointerType{
|
---|
2470 | GET_ACCEPT_1( base, Type ),
|
---|
2471 | GET_ACCEPT_1( dimension, Expr ),
|
---|
2472 | (ast::LengthFlag)old->isVarLen,
|
---|
2473 | (ast::DimensionFlag)old->isStatic,
|
---|
2474 | cv( old )
|
---|
2475 | } );
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | virtual void visit( const ArrayType * old ) override final {
|
---|
2479 | visitType( old, new ast::ArrayType{
|
---|
2480 | GET_ACCEPT_1( base, Type ),
|
---|
2481 | GET_ACCEPT_1( dimension, Expr ),
|
---|
2482 | (ast::LengthFlag)old->isVarLen,
|
---|
2483 | (ast::DimensionFlag)old->isStatic,
|
---|
2484 | cv( old )
|
---|
2485 | } );
|
---|
2486 | }
|
---|
2487 |
|
---|
2488 | virtual void visit( const ReferenceType * old ) override final {
|
---|
2489 | visitType( old, new ast::ReferenceType{
|
---|
2490 | GET_ACCEPT_1( base, Type ),
|
---|
2491 | cv( old )
|
---|
2492 | } );
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | virtual void visit( const QualifiedType * old ) override final {
|
---|
2496 | visitType( old, new ast::QualifiedType{
|
---|
2497 | GET_ACCEPT_1( parent, Type ),
|
---|
2498 | GET_ACCEPT_1( child, Type ),
|
---|
2499 | cv( old )
|
---|
2500 | } );
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | virtual void visit( const FunctionType * old ) override final {
|
---|
2504 | auto ty = new ast::FunctionType {
|
---|
2505 | (ast::ArgumentFlag)old->isVarArgs,
|
---|
2506 | cv( old )
|
---|
2507 | };
|
---|
2508 | ty->returns = GET_ACCEPT_V( returnVals, DeclWithType );
|
---|
2509 | ty->params = GET_ACCEPT_V( parameters, DeclWithType );
|
---|
2510 | ty->forall = GET_ACCEPT_V( forall, TypeDecl );
|
---|
2511 | visitType( old, ty );
|
---|
2512 | }
|
---|
2513 |
|
---|
2514 | void postvisit( const ReferenceToType * old, ast::ReferenceToType * ty ) {
|
---|
2515 | ty->forall = GET_ACCEPT_V( forall, TypeDecl );
|
---|
2516 | ty->params = GET_ACCEPT_V( parameters, Expr );
|
---|
2517 | ty->hoistType = old->hoistType;
|
---|
2518 | visitType( old, ty );
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 | virtual void visit( const StructInstType * old ) override final {
|
---|
2522 | ast::StructInstType * ty;
|
---|
2523 | if ( old->baseStruct ) {
|
---|
2524 | ty = new ast::StructInstType{
|
---|
2525 | GET_ACCEPT_1( baseStruct, StructDecl ),
|
---|
2526 | cv( old ),
|
---|
2527 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2528 | };
|
---|
2529 | } else {
|
---|
2530 | ty = new ast::StructInstType{
|
---|
2531 | old->name,
|
---|
2532 | cv( old ),
|
---|
2533 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2534 | };
|
---|
2535 | }
|
---|
2536 | postvisit( old, ty );
|
---|
2537 | }
|
---|
2538 |
|
---|
2539 | virtual void visit( const UnionInstType * old ) override final {
|
---|
2540 | ast::UnionInstType * ty;
|
---|
2541 | if ( old->baseUnion ) {
|
---|
2542 | ty = new ast::UnionInstType{
|
---|
2543 | GET_ACCEPT_1( baseUnion, UnionDecl ),
|
---|
2544 | cv( old ),
|
---|
2545 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2546 | };
|
---|
2547 | } else {
|
---|
2548 | ty = new ast::UnionInstType{
|
---|
2549 | old->name,
|
---|
2550 | cv( old ),
|
---|
2551 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2552 | };
|
---|
2553 | }
|
---|
2554 | postvisit( old, ty );
|
---|
2555 | }
|
---|
2556 |
|
---|
2557 | virtual void visit( const EnumInstType * old ) override final {
|
---|
2558 | ast::EnumInstType * ty;
|
---|
2559 | if ( old->baseEnum ) {
|
---|
2560 | ty = new ast::EnumInstType{
|
---|
2561 | GET_ACCEPT_1( baseEnum, EnumDecl ),
|
---|
2562 | cv( old ),
|
---|
2563 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2564 | };
|
---|
2565 | } else {
|
---|
2566 | ty = new ast::EnumInstType{
|
---|
2567 | old->name,
|
---|
2568 | cv( old ),
|
---|
2569 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2570 | };
|
---|
2571 | }
|
---|
2572 | postvisit( old, ty );
|
---|
2573 | }
|
---|
2574 |
|
---|
2575 | virtual void visit( const TraitInstType * old ) override final {
|
---|
2576 | ast::TraitInstType * ty;
|
---|
2577 | if ( old->baseTrait ) {
|
---|
2578 | ty = new ast::TraitInstType{
|
---|
2579 | GET_ACCEPT_1( baseTrait, TraitDecl ),
|
---|
2580 | cv( old ),
|
---|
2581 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2582 | };
|
---|
2583 | } else {
|
---|
2584 | ty = new ast::TraitInstType{
|
---|
2585 | old->name,
|
---|
2586 | cv( old ),
|
---|
2587 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2588 | };
|
---|
2589 | }
|
---|
2590 | postvisit( old, ty );
|
---|
2591 | }
|
---|
2592 |
|
---|
2593 | virtual void visit( const TypeInstType * old ) override final {
|
---|
2594 | ast::TypeInstType * ty;
|
---|
2595 | if ( old->baseType ) {
|
---|
2596 | ty = new ast::TypeInstType{
|
---|
2597 | old->name,
|
---|
2598 | GET_ACCEPT_1( baseType, TypeDecl ),
|
---|
2599 | cv( old ),
|
---|
2600 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2601 | };
|
---|
2602 | } else {
|
---|
2603 | ty = new ast::TypeInstType{
|
---|
2604 | old->name,
|
---|
2605 | old->isFtype ? ast::TypeDecl::Ftype : ast::TypeDecl::Dtype,
|
---|
2606 | cv( old ),
|
---|
2607 | GET_ACCEPT_V( attributes, Attribute )
|
---|
2608 | };
|
---|
2609 | }
|
---|
2610 | postvisit( old, ty );
|
---|
2611 | }
|
---|
2612 |
|
---|
2613 | virtual void visit( const TupleType * old ) override final {
|
---|
2614 | visitType( old, new ast::TupleType{
|
---|
2615 | GET_ACCEPT_V( types, Type ),
|
---|
2616 | // members generated by TupleType c'tor
|
---|
2617 | cv( old )
|
---|
2618 | } );
|
---|
2619 | }
|
---|
2620 |
|
---|
2621 | virtual void visit( const TypeofType * old ) override final {
|
---|
2622 | visitType( old, new ast::TypeofType{
|
---|
2623 | GET_ACCEPT_1( expr, Expr ),
|
---|
2624 | (ast::TypeofType::Kind)old->is_basetypeof,
|
---|
2625 | cv( old )
|
---|
2626 | } );
|
---|
2627 | }
|
---|
2628 |
|
---|
2629 | virtual void visit( const AttrType * ) override final {
|
---|
2630 | assertf( false, "AttrType deprecated in new AST." );
|
---|
2631 | }
|
---|
2632 |
|
---|
2633 | virtual void visit( const VarArgsType * old ) override final {
|
---|
2634 | visitType( old, new ast::VarArgsType{ cv( old ) } );
|
---|
2635 | }
|
---|
2636 |
|
---|
2637 | virtual void visit( const ZeroType * old ) override final {
|
---|
2638 | visitType( old, new ast::ZeroType{ cv( old ) } );
|
---|
2639 | }
|
---|
2640 |
|
---|
2641 | virtual void visit( const OneType * old ) override final {
|
---|
2642 | visitType( old, new ast::OneType{ cv( old ) } );
|
---|
2643 | }
|
---|
2644 |
|
---|
2645 | virtual void visit( const GlobalScopeType * old ) override final {
|
---|
2646 | visitType( old, new ast::GlobalScopeType{} );
|
---|
2647 | }
|
---|
2648 |
|
---|
2649 | virtual void visit( const Designation * old ) override final {
|
---|
2650 | this->node = new ast::Designation(
|
---|
2651 | old->location,
|
---|
2652 | GET_ACCEPT_D(designators, Expr)
|
---|
2653 | );
|
---|
2654 | }
|
---|
2655 |
|
---|
2656 | virtual void visit( const SingleInit * old ) override final {
|
---|
2657 | this->node = new ast::SingleInit(
|
---|
2658 | old->location,
|
---|
2659 | GET_ACCEPT_1(value, Expr),
|
---|
2660 | (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
|
---|
2661 | );
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | virtual void visit( const ListInit * old ) override final {
|
---|
2665 | this->node = new ast::ListInit(
|
---|
2666 | old->location,
|
---|
2667 | GET_ACCEPT_V(initializers, Init),
|
---|
2668 | GET_ACCEPT_V(designations, Designation),
|
---|
2669 | (old->get_maybeConstructed()) ? ast::MaybeConstruct : ast::DoConstruct
|
---|
2670 | );
|
---|
2671 | }
|
---|
2672 |
|
---|
2673 | virtual void visit( const ConstructorInit * old ) override final {
|
---|
2674 | this->node = new ast::ConstructorInit(
|
---|
2675 | old->location,
|
---|
2676 | GET_ACCEPT_1(ctor, Stmt),
|
---|
2677 | GET_ACCEPT_1(dtor, Stmt),
|
---|
2678 | GET_ACCEPT_1(init, Init)
|
---|
2679 | );
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 | virtual void visit( const Constant * ) override final {
|
---|
2683 | // Handled in visit( ConstantEpxr * ).
|
---|
2684 | // In the new tree, Constant fields are inlined into containing ConstantExpression.
|
---|
2685 | assert( 0 );
|
---|
2686 | }
|
---|
2687 |
|
---|
2688 | virtual void visit( const Attribute * old ) override final {
|
---|
2689 | this->node = new ast::Attribute(
|
---|
2690 | old->name,
|
---|
2691 | GET_ACCEPT_V( parameters, Expr )
|
---|
2692 | );
|
---|
2693 | }
|
---|
2694 | };
|
---|
2695 |
|
---|
2696 | #undef GET_LABELS_V
|
---|
2697 | #undef GET_ACCEPT_V
|
---|
2698 | #undef GET_ACCEPT_1
|
---|
2699 |
|
---|
2700 | std::list< ast::ptr< ast::Decl > > convert( const std::list< Declaration * > && translationUnit ) {
|
---|
2701 | ConverterOldToNew c;
|
---|
2702 | std::list< ast::ptr< ast::Decl > > decls;
|
---|
2703 | for(auto d : translationUnit) {
|
---|
2704 | d->accept( c );
|
---|
2705 | decls.emplace_back( c.decl() );
|
---|
2706 | }
|
---|
2707 | deleteAll(translationUnit);
|
---|
2708 | return decls;
|
---|
2709 | }
|
---|