source: src/AST/Pass.impl.hpp@ 6b2d444

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 6b2d444 was 6b2d444, checked in by caparsons <caparson@…>, 4 years ago

changed a few mutexstmt things

  • Property mode set to 100644
File size: 58.8 KB
Line 
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// ast::Pass.impl.hpp --
8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#pragma once
17// IWYU pragma: private, include "AST/Pass.hpp"
18
19#include <type_traits>
20#include <unordered_map>
21
22#include "AST/TranslationUnit.hpp"
23#include "AST/TypeSubstitution.hpp"
24
25#define VISIT_START( node ) \
26 using namespace ast; \
27 /* back-up the visit children */ \
28 __attribute__((unused)) ast::__pass::visit_children_guard guard1( ast::__pass::visit_children(core, 0) ); \
29 /* setup the scope for passes that want to run code at exit */ \
30 __attribute__((unused)) ast::__pass::guard_value guard2( ast::__pass::at_cleanup (core, 0) ); \
31 /* begin tracing memory allocation if requested by this pass */ \
32 __pass::beginTrace( core, 0 ); \
33 /* call the implementation of the previsit of this pass */ \
34 __pass::previsit( core, node, 0 );
35
36#define VISIT( code... ) \
37 /* if this node should visit its children */ \
38 if ( __visit_children() ) { \
39 /* visit the children */ \
40 code \
41 }
42
43#define VISIT_END( type, node ) \
44 /* call the implementation of the postvisit of this pass */ \
45 auto __return = __pass::postvisit( core, node, 0 ); \
46 assertf(__return, "post visit should never return null"); \
47 /* end tracing memory allocation if requested by this pass */ \
48 __pass::endTrace( core, 0 ); \
49 return __return;
50
51#ifdef PEDANTIC_PASS_ASSERT
52#define __pedantic_pass_assert(...) assert (__VA_ARGS__)
53#define __pedantic_pass_assertf(...) assertf(__VA_ARGS__)
54#else
55#define __pedantic_pass_assert(...)
56#define __pedantic_pass_assertf(...)
57#endif
58
59namespace ast {
60 template<typename node_t>
61 node_t * shallowCopy( const node_t * node );
62
63 namespace __pass {
64 // Check if this is either a null pointer or a pointer to an empty container
65 template<typename T>
66 static inline bool empty( T * ptr ) {
67 return !ptr || ptr->empty();
68 }
69
70 template< typename core_t, typename node_t >
71 static inline node_t* mutate(const node_t *node) {
72 return std::is_base_of<PureVisitor, core_t>::value ? ::ast::shallowCopy(node) : ::ast::mutate(node);
73 }
74
75 //------------------------------
76 template<typename it_t, template <class...> class container_t>
77 static inline void take_all( it_t it, container_t<ast::ptr<ast::Decl>> * decls, bool * mutated = nullptr ) {
78 if(empty(decls)) return;
79
80 std::transform(decls->begin(), decls->end(), it, [](const ast::Decl * decl) -> auto {
81 return new DeclStmt( decl->location, decl );
82 });
83 decls->clear();
84 if(mutated) *mutated = true;
85 }
86
87 template<typename it_t, template <class...> class container_t>
88 static inline void take_all( it_t it, container_t<ast::ptr<ast::Stmt>> * decls, bool * mutated = nullptr ) {
89 if(empty(decls)) return;
90
91 std::move(decls->begin(), decls->end(), it);
92 decls->clear();
93 if(mutated) *mutated = true;
94 }
95
96 //------------------------------
97 /// Check if should be skipped, different for pointers and containers
98 template<typename node_t>
99 bool skip( const ast::ptr<node_t> & val) {
100 return !val;
101 }
102
103 template< template <class...> class container_t, typename node_t >
104 bool skip( const container_t<ast::ptr< node_t >> & val ) {
105 return val.empty();
106 }
107
108 //------------------------------
109 /// Get the value to visit, different for pointers and containers
110 template<typename node_t>
111 auto get( const ast::ptr<node_t> & val, int ) -> decltype(val.get()) {
112 return val.get();
113 }
114
115 template<typename node_t>
116 const node_t & get( const node_t & val, long) {
117 return val;
118 }
119
120
121 //------------------------------
122 /// Check if value was mutated, different for pointers and containers
123 template<typename lhs_t, typename rhs_t>
124 bool differs( const lhs_t * old_val, const rhs_t * new_val ) {
125 return old_val != new_val;
126 }
127
128 template< template <class...> class container_t, typename node_t >
129 bool differs( const container_t<ast::ptr< node_t >> &, const container_t<ast::ptr< node_t >> & new_val ) {
130 return !new_val.empty();
131 }
132 }
133
134 template< typename core_t >
135 template< typename node_t >
136 auto ast::Pass< core_t >::call_accept( const node_t * node )
137 -> typename std::enable_if<
138 !std::is_base_of<ast::Expr, node_t>::value &&
139 !std::is_base_of<ast::Stmt, node_t>::value
140 , decltype( node->accept(*this) )
141 >::type
142 {
143 __pedantic_pass_assert( __visit_children() );
144 __pedantic_pass_assert( node );
145
146 static_assert( !std::is_base_of<ast::Expr, node_t>::value, "ERROR");
147 static_assert( !std::is_base_of<ast::Stmt, node_t>::value, "ERROR");
148
149 return node->accept( *this );
150 }
151
152 template< typename core_t >
153 const ast::Expr * ast::Pass< core_t >::call_accept( const ast::Expr * expr ) {
154 __pedantic_pass_assert( __visit_children() );
155 __pedantic_pass_assert( expr );
156
157 const ast::TypeSubstitution ** typeSubs_ptr = __pass::typeSubs( core, 0 );
158 if ( typeSubs_ptr && expr->env ) {
159 *typeSubs_ptr = expr->env;
160 }
161
162 return expr->accept( *this );
163 }
164
165 template< typename core_t >
166 const ast::Stmt * ast::Pass< core_t >::call_accept( const ast::Stmt * stmt ) {
167 __pedantic_pass_assert( __visit_children() );
168 __pedantic_pass_assert( stmt );
169
170 return stmt->accept( *this );
171 }
172
173 template< typename core_t >
174 const ast::Stmt * ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) {
175 __pedantic_pass_assert( __visit_children() );
176 __pedantic_pass_assert( stmt );
177
178 // add a few useful symbols to the scope
179 using __pass::empty;
180
181 // get the stmts/decls that will need to be spliced in
182 auto stmts_before = __pass::stmtsToAddBefore( core, 0);
183 auto stmts_after = __pass::stmtsToAddAfter ( core, 0);
184 auto decls_before = __pass::declsToAddBefore( core, 0);
185 auto decls_after = __pass::declsToAddAfter ( core, 0);
186
187 // These may be modified by subnode but most be restored once we exit this statemnet.
188 ValueGuardPtr< const ast::TypeSubstitution * > __old_env ( __pass::typeSubs( core, 0 ) );
189 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
190 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after );
191 ValueGuardPtr< typename std::remove_pointer< decltype(decls_before) >::type > __old_stmts_before( decls_before );
192 ValueGuardPtr< typename std::remove_pointer< decltype(decls_after ) >::type > __old_stmts_after ( decls_after );
193
194 // Now is the time to actually visit the node
195 const ast::Stmt * nstmt = stmt->accept( *this );
196
197 // If the pass doesn't want to add anything then we are done
198 if( empty(stmts_before) && empty(stmts_after) && empty(decls_before) && empty(decls_after) ) {
199 return nstmt;
200 }
201
202 // Make sure that it is either adding statements or declartions but not both
203 // this is because otherwise the order would be awkward to predict
204 assert(( empty( stmts_before ) && empty( stmts_after ))
205 || ( empty( decls_before ) && empty( decls_after )) );
206
207 // Create a new Compound Statement to hold the new decls/stmts
208 ast::CompoundStmt * compound = new ast::CompoundStmt( stmt->location );
209
210 // Take all the declarations that go before
211 __pass::take_all( std::back_inserter( compound->kids ), decls_before );
212 __pass::take_all( std::back_inserter( compound->kids ), stmts_before );
213
214 // Insert the original declaration
215 compound->kids.emplace_back( nstmt );
216
217 // Insert all the declarations that go before
218 __pass::take_all( std::back_inserter( compound->kids ), decls_after );
219 __pass::take_all( std::back_inserter( compound->kids ), stmts_after );
220
221 return compound;
222 }
223
224 template< typename core_t >
225 template< template <class...> class container_t >
226 container_t< ptr<Stmt> > ast::Pass< core_t >::call_accept( const container_t< ptr<Stmt> > & statements ) {
227 __pedantic_pass_assert( __visit_children() );
228 if( statements.empty() ) return {};
229
230 // We are going to aggregate errors for all these statements
231 SemanticErrorException errors;
232
233 // add a few useful symbols to the scope
234 using __pass::empty;
235
236 // get the stmts/decls that will need to be spliced in
237 auto stmts_before = __pass::stmtsToAddBefore( core, 0);
238 auto stmts_after = __pass::stmtsToAddAfter ( core, 0);
239 auto decls_before = __pass::declsToAddBefore( core, 0);
240 auto decls_after = __pass::declsToAddAfter ( core, 0);
241
242 // These may be modified by subnode but most be restored once we exit this statemnet.
243 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
244 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after );
245 ValueGuardPtr< typename std::remove_pointer< decltype(decls_before) >::type > __old_stmts_before( decls_before );
246 ValueGuardPtr< typename std::remove_pointer< decltype(decls_after ) >::type > __old_stmts_after ( decls_after );
247
248 // update pass statitistics
249 pass_visitor_stats.depth++;
250 pass_visitor_stats.max->push(pass_visitor_stats.depth);
251 pass_visitor_stats.avg->push(pass_visitor_stats.depth);
252
253 bool mutated = false;
254 container_t< ptr<Stmt> > new_kids;
255 for( const Stmt * stmt : statements ) {
256 try {
257 __pedantic_pass_assert( stmt );
258 const ast::Stmt * new_stmt = stmt->accept( *this );
259 assert( new_stmt );
260 if(new_stmt != stmt ) mutated = true;
261
262 // Make sure that it is either adding statements or declartions but not both
263 // this is because otherwise the order would be awkward to predict
264 assert(( empty( stmts_before ) && empty( stmts_after ))
265 || ( empty( decls_before ) && empty( decls_after )) );
266
267
268
269 // Take all the statements which should have gone after, N/A for first iteration
270 __pass::take_all( std::back_inserter( new_kids ), decls_before, &mutated );
271 __pass::take_all( std::back_inserter( new_kids ), stmts_before, &mutated );
272
273 // Now add the statement if there is one
274 new_kids.emplace_back( new_stmt );
275
276 // Take all the declarations that go before
277 __pass::take_all( std::back_inserter( new_kids ), decls_after, &mutated );
278 __pass::take_all( std::back_inserter( new_kids ), stmts_after, &mutated );
279 }
280 catch ( SemanticErrorException &e ) {
281 errors.append( e );
282 }
283 }
284 pass_visitor_stats.depth--;
285 if ( !errors.isEmpty() ) { throw errors; }
286
287 return mutated ? new_kids : container_t< ptr<Stmt> >();
288 }
289
290 template< typename core_t >
291 template< template <class...> class container_t, typename node_t >
292 container_t< ast::ptr<node_t> > ast::Pass< core_t >::call_accept( const container_t< ast::ptr<node_t> > & container ) {
293 __pedantic_pass_assert( __visit_children() );
294 if( container.empty() ) return {};
295 SemanticErrorException errors;
296
297 pass_visitor_stats.depth++;
298 pass_visitor_stats.max->push(pass_visitor_stats.depth);
299 pass_visitor_stats.avg->push(pass_visitor_stats.depth);
300
301 bool mutated = false;
302 container_t< ast::ptr<node_t> > new_kids;
303 for ( const node_t * node : container ) {
304 try {
305 __pedantic_pass_assert( node );
306 const node_t * new_stmt = strict_dynamic_cast< const node_t * >( node->accept( *this ) );
307 if(new_stmt != node ) mutated = true;
308
309 new_kids.emplace_back( new_stmt );
310 }
311 catch( SemanticErrorException &e ) {
312 errors.append( e );
313 }
314 }
315 pass_visitor_stats.depth--;
316 if ( ! errors.isEmpty() ) { throw errors; }
317
318 return mutated ? new_kids : container_t< ast::ptr<node_t> >();
319 }
320
321 template< typename core_t >
322 template<typename node_t, typename parent_t, typename child_t>
323 void ast::Pass< core_t >::maybe_accept(
324 const node_t * & parent,
325 child_t parent_t::*child
326 ) {
327 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" );
328
329 if(__pass::skip(parent->*child)) return;
330 const auto & old_val = __pass::get(parent->*child, 0);
331
332 static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR");
333
334 auto new_val = call_accept( old_val );
335
336 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR");
337
338 if( __pass::differs(old_val, new_val) ) {
339 auto new_parent = __pass::mutate<core_t>(parent);
340 new_parent->*child = new_val;
341 parent = new_parent;
342 }
343 }
344
345 template< typename core_t >
346 template<typename node_t, typename parent_t, typename child_t>
347 void ast::Pass< core_t >::maybe_accept_as_compound(
348 const node_t * & parent,
349 child_t parent_t::*child
350 ) {
351 static_assert( std::is_base_of<parent_t, node_t>::value, "Error deducing member object" );
352
353 if(__pass::skip(parent->*child)) return;
354 const auto & old_val = __pass::get(parent->*child, 0);
355
356 static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR");
357
358 auto new_val = call_accept_as_compound( old_val );
359
360 static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR");
361
362 if( __pass::differs(old_val, new_val) ) {
363 auto new_parent = __pass::mutate<core_t>(parent);
364 new_parent->*child = new_val;
365 parent = new_parent;
366 }
367 }
368
369}
370
371//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
372//========================================================================================================================================================================
373//========================================================================================================================================================================
374//========================================================================================================================================================================
375//========================================================================================================================================================================
376//========================================================================================================================================================================
377//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
378
379template< typename core_t >
380inline void ast::accept_all( std::list< ast::ptr<ast::Decl> > & decls, ast::Pass< core_t > & visitor ) {
381 // We are going to aggregate errors for all these statements
382 SemanticErrorException errors;
383
384 // add a few useful symbols to the scope
385 using __pass::empty;
386
387 // get the stmts/decls that will need to be spliced in
388 auto decls_before = __pass::declsToAddBefore( visitor.core, 0);
389 auto decls_after = __pass::declsToAddAfter ( visitor.core, 0);
390
391 // update pass statitistics
392 pass_visitor_stats.depth++;
393 pass_visitor_stats.max->push(pass_visitor_stats.depth);
394 pass_visitor_stats.avg->push(pass_visitor_stats.depth);
395
396 for ( std::list< ast::ptr<ast::Decl> >::iterator i = decls.begin(); ; ++i ) {
397 // splice in new declarations after previous decl
398 if ( !empty( decls_after ) ) { decls.splice( i, *decls_after ); }
399
400 if ( i == decls.end() ) break;
401
402 try {
403 // run visitor on declaration
404 ast::ptr<ast::Decl> & node = *i;
405 assert( node );
406 node = node->accept( visitor );
407 }
408 catch( SemanticErrorException &e ) {
409 if (__pass::on_error (visitor.core, *i, 0))
410 errors.append( e );
411 }
412
413 // splice in new declarations before current decl
414 if ( !empty( decls_before ) ) { decls.splice( i, *decls_before ); }
415 }
416 pass_visitor_stats.depth--;
417 if ( !errors.isEmpty() ) { throw errors; }
418}
419
420template< typename core_t >
421inline void ast::accept_all( ast::TranslationUnit & unit, ast::Pass< core_t > & visitor ) {
422 if ( auto ptr = __pass::translation_unit::get_cptr( visitor.core, 0 ) ) {
423 ValueGuard<const TranslationUnit *> guard( *ptr );
424 *ptr = &unit;
425 return ast::accept_all( unit.decls, visitor );
426 } else {
427 return ast::accept_all( unit.decls, visitor );
428 }
429}
430
431// A NOTE ON THE ORDER OF TRAVERSAL
432//
433// Types and typedefs have their base types visited before they are added to the type table. This is ok, since there is
434// no such thing as a recursive type or typedef.
435//
436// typedef struct { T *x; } T; // never allowed
437//
438// for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
439// members are traversed, and then the complete type should be added (assuming the type is completed by this particular
440// declaration).
441//
442// struct T { struct T *x; }; // allowed
443//
444// It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
445// traversal may modify the definition of the type and these modifications should be visible when the symbol table is
446// queried later in this pass.
447
448//--------------------------------------------------------------------------
449// ObjectDecl
450template< typename core_t >
451const ast::DeclWithType * ast::Pass< core_t >::visit( const ast::ObjectDecl * node ) {
452 VISIT_START( node );
453
454 VISIT(
455 {
456 guard_symtab guard { *this };
457 maybe_accept( node, &ObjectDecl::type );
458 }
459 maybe_accept( node, &ObjectDecl::init );
460 maybe_accept( node, &ObjectDecl::bitfieldWidth );
461 maybe_accept( node, &ObjectDecl::attributes );
462 )
463
464 __pass::symtab::addId( core, 0, node );
465
466 VISIT_END( DeclWithType, node );
467}
468
469//--------------------------------------------------------------------------
470// FunctionDecl
471template< typename core_t >
472const ast::DeclWithType * ast::Pass< core_t >::visit( const ast::FunctionDecl * node ) {
473 VISIT_START( node );
474
475 __pass::symtab::addId( core, 0, node );
476
477 VISIT(maybe_accept( node, &FunctionDecl::withExprs );)
478 {
479 // with clause introduces a level of scope (for the with expression members).
480 // with clause exprs are added to the symbol table before parameters so that parameters
481 // shadow with exprs and not the other way around.
482 guard_symtab guard { *this };
483 __pass::symtab::addWith( core, 0, node->withExprs, node );
484 {
485 guard_symtab guard { *this };
486 // implicit add __func__ identifier as specified in the C manual 6.4.2.2
487 static ast::ptr< ast::ObjectDecl > func{ new ast::ObjectDecl{
488 CodeLocation{}, "__func__",
489 new ast::ArrayType{
490 new ast::BasicType{ ast::BasicType::Char, ast::CV::Const },
491 nullptr, VariableLen, DynamicDim
492 }
493 } };
494 __pass::symtab::addId( core, 0, func );
495 VISIT(
496 // parameter declarations
497 maybe_accept( node, &FunctionDecl::params );
498 maybe_accept( node, &FunctionDecl::returns );
499 // type params and assertions
500 maybe_accept( node, &FunctionDecl::type_params );
501 maybe_accept( node, &FunctionDecl::assertions );
502 // First remember that we are now within a function.
503 ValueGuard< bool > oldInFunction( inFunction );
504 inFunction = true;
505 // The function body needs to have the same scope as parameters.
506 // A CompoundStmt will not enter a new scope if atFunctionTop is true.
507 ValueGuard< bool > oldAtFunctionTop( atFunctionTop );
508 atFunctionTop = true;
509 maybe_accept( node, &FunctionDecl::stmts );
510 maybe_accept( node, &FunctionDecl::attributes );
511 )
512 }
513 }
514
515 VISIT_END( DeclWithType, node );
516}
517
518//--------------------------------------------------------------------------
519// StructDecl
520template< typename core_t >
521const ast::Decl * ast::Pass< core_t >::visit( const ast::StructDecl * node ) {
522 VISIT_START( node );
523
524 // make up a forward declaration and add it before processing the members
525 // needs to be on the heap because addStruct saves the pointer
526 __pass::symtab::addStructFwd( core, 0, node );
527
528 VISIT({
529 guard_symtab guard { * this };
530 maybe_accept( node, &StructDecl::params );
531 maybe_accept( node, &StructDecl::members );
532 maybe_accept( node, &StructDecl::attributes );
533 })
534
535 // this addition replaces the forward declaration
536 __pass::symtab::addStruct( core, 0, node );
537
538 VISIT_END( Decl, node );
539}
540
541//--------------------------------------------------------------------------
542// UnionDecl
543template< typename core_t >
544const ast::Decl * ast::Pass< core_t >::visit( const ast::UnionDecl * node ) {
545 VISIT_START( node );
546
547 // make up a forward declaration and add it before processing the members
548 __pass::symtab::addUnionFwd( core, 0, node );
549
550 VISIT({
551 guard_symtab guard { * this };
552 maybe_accept( node, &UnionDecl::params );
553 maybe_accept( node, &UnionDecl::members );
554 maybe_accept( node, &UnionDecl::attributes );
555 })
556
557 __pass::symtab::addUnion( core, 0, node );
558
559 VISIT_END( Decl, node );
560}
561
562//--------------------------------------------------------------------------
563// EnumDecl
564template< typename core_t >
565const ast::Decl * ast::Pass< core_t >::visit( const ast::EnumDecl * node ) {
566 VISIT_START( node );
567
568 __pass::symtab::addEnum( core, 0, node );
569
570 VISIT(
571 // unlike structs, traits, and unions, enums inject their members into the global scope
572 maybe_accept( node, &EnumDecl::params );
573 maybe_accept( node, &EnumDecl::members );
574 maybe_accept( node, &EnumDecl::attributes );
575 )
576
577 VISIT_END( Decl, node );
578}
579
580//--------------------------------------------------------------------------
581// TraitDecl
582template< typename core_t >
583const ast::Decl * ast::Pass< core_t >::visit( const ast::TraitDecl * node ) {
584 VISIT_START( node );
585
586 VISIT({
587 guard_symtab guard { *this };
588 maybe_accept( node, &TraitDecl::params );
589 maybe_accept( node, &TraitDecl::members );
590 maybe_accept( node, &TraitDecl::attributes );
591 })
592
593 __pass::symtab::addTrait( core, 0, node );
594
595 VISIT_END( Decl, node );
596}
597
598//--------------------------------------------------------------------------
599// TypeDecl
600template< typename core_t >
601const ast::Decl * ast::Pass< core_t >::visit( const ast::TypeDecl * node ) {
602 VISIT_START( node );
603
604 VISIT({
605 guard_symtab guard { *this };
606 maybe_accept( node, &TypeDecl::base );
607 })
608
609 // see A NOTE ON THE ORDER OF TRAVERSAL, above
610 // note that assertions come after the type is added to the symtab, since they are not part of the type proper
611 // and may depend on the type itself
612 __pass::symtab::addType( core, 0, node );
613
614 VISIT(
615 maybe_accept( node, &TypeDecl::assertions );
616
617 {
618 guard_symtab guard { *this };
619 maybe_accept( node, &TypeDecl::init );
620 }
621 )
622
623 VISIT_END( Decl, node );
624}
625
626//--------------------------------------------------------------------------
627// TypedefDecl
628template< typename core_t >
629const ast::Decl * ast::Pass< core_t >::visit( const ast::TypedefDecl * node ) {
630 VISIT_START( node );
631
632 VISIT({
633 guard_symtab guard { *this };
634 maybe_accept( node, &TypedefDecl::base );
635 })
636
637 __pass::symtab::addType( core, 0, node );
638
639 VISIT( maybe_accept( node, &TypedefDecl::assertions ); )
640
641 VISIT_END( Decl, node );
642}
643
644//--------------------------------------------------------------------------
645// AsmDecl
646template< typename core_t >
647const ast::AsmDecl * ast::Pass< core_t >::visit( const ast::AsmDecl * node ) {
648 VISIT_START( node );
649
650 VISIT(
651 maybe_accept( node, &AsmDecl::stmt );
652 )
653
654 VISIT_END( AsmDecl, node );
655}
656
657//--------------------------------------------------------------------------
658// DirectiveDecl
659template< typename core_t >
660const ast::DirectiveDecl * ast::Pass< core_t >::visit( const ast::DirectiveDecl * node ) {
661 VISIT_START( node );
662
663 VISIT(
664 maybe_accept( node, &DirectiveDecl::stmt );
665 )
666
667 VISIT_END( DirectiveDecl, node );
668}
669
670//--------------------------------------------------------------------------
671// StaticAssertDecl
672template< typename core_t >
673const ast::StaticAssertDecl * ast::Pass< core_t >::visit( const ast::StaticAssertDecl * node ) {
674 VISIT_START( node );
675
676 VISIT(
677 maybe_accept( node, &StaticAssertDecl::cond );
678 maybe_accept( node, &StaticAssertDecl::msg );
679 )
680
681 VISIT_END( StaticAssertDecl, node );
682}
683
684//--------------------------------------------------------------------------
685// CompoundStmt
686template< typename core_t >
687const ast::CompoundStmt * ast::Pass< core_t >::visit( const ast::CompoundStmt * node ) {
688 VISIT_START( node );
689 VISIT(
690 // Do not enter (or leave) a new scope if atFunctionTop. Remember to save the result.
691 auto guard1 = makeFuncGuard( [this, enterScope = !this->atFunctionTop]() {
692 if ( enterScope ) {
693 __pass::symtab::enter(core, 0);
694 __pass::scope::enter(core, 0);
695 }
696 }, [this, leaveScope = !this->atFunctionTop]() {
697 if ( leaveScope ) {
698 __pass::symtab::leave(core, 0);
699 __pass::scope::leave(core, 0);
700 }
701 });
702 ValueGuard< bool > guard2( atFunctionTop );
703 atFunctionTop = false;
704 guard_scope guard3 { *this };
705 maybe_accept( node, &CompoundStmt::kids );
706 )
707 VISIT_END( CompoundStmt, node );
708}
709
710//--------------------------------------------------------------------------
711// ExprStmt
712template< typename core_t >
713const ast::Stmt * ast::Pass< core_t >::visit( const ast::ExprStmt * node ) {
714 VISIT_START( node );
715
716 VISIT(
717 maybe_accept( node, &ExprStmt::expr );
718 )
719
720 VISIT_END( Stmt, node );
721}
722
723//--------------------------------------------------------------------------
724// AsmStmt
725template< typename core_t >
726const ast::Stmt * ast::Pass< core_t >::visit( const ast::AsmStmt * node ) {
727 VISIT_START( node )
728
729 VISIT(
730 maybe_accept( node, &AsmStmt::instruction );
731 maybe_accept( node, &AsmStmt::output );
732 maybe_accept( node, &AsmStmt::input );
733 maybe_accept( node, &AsmStmt::clobber );
734 )
735
736 VISIT_END( Stmt, node );
737}
738
739//--------------------------------------------------------------------------
740// DirectiveStmt
741template< typename core_t >
742const ast::Stmt * ast::Pass< core_t >::visit( const ast::DirectiveStmt * node ) {
743 VISIT_START( node )
744
745 VISIT_END( Stmt, node );
746}
747
748//--------------------------------------------------------------------------
749// IfStmt
750template< typename core_t >
751const ast::Stmt * ast::Pass< core_t >::visit( const ast::IfStmt * node ) {
752 VISIT_START( node );
753
754 VISIT({
755 // if statements introduce a level of scope (for the initialization)
756 guard_symtab guard { *this };
757 maybe_accept( node, &IfStmt::inits );
758 maybe_accept( node, &IfStmt::cond );
759 maybe_accept_as_compound( node, &IfStmt::thenPart );
760 maybe_accept_as_compound( node, &IfStmt::elsePart );
761 })
762
763 VISIT_END( Stmt, node );
764}
765
766//--------------------------------------------------------------------------
767// WhileStmt
768template< typename core_t >
769const ast::Stmt * ast::Pass< core_t >::visit( const ast::WhileStmt * node ) {
770 VISIT_START( node );
771
772 VISIT({
773 // while statements introduce a level of scope (for the initialization)
774 guard_symtab guard { *this };
775 maybe_accept( node, &WhileStmt::inits );
776 maybe_accept( node, &WhileStmt::cond );
777 maybe_accept_as_compound( node, &WhileStmt::body );
778 })
779
780 VISIT_END( Stmt, node );
781}
782
783//--------------------------------------------------------------------------
784// ForStmt
785template< typename core_t >
786const ast::Stmt * ast::Pass< core_t >::visit( const ast::ForStmt * node ) {
787 VISIT_START( node );
788
789 VISIT({
790 // for statements introduce a level of scope (for the initialization)
791 guard_symtab guard { *this };
792 // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later.
793 maybe_accept( node, &ForStmt::inits );
794 maybe_accept( node, &ForStmt::cond );
795 maybe_accept( node, &ForStmt::inc );
796 maybe_accept_as_compound( node, &ForStmt::body );
797 })
798
799 VISIT_END( Stmt, node );
800}
801
802//--------------------------------------------------------------------------
803// SwitchStmt
804template< typename core_t >
805const ast::Stmt * ast::Pass< core_t >::visit( const ast::SwitchStmt * node ) {
806 VISIT_START( node );
807
808 VISIT(
809 maybe_accept( node, &SwitchStmt::cond );
810 maybe_accept( node, &SwitchStmt::stmts );
811 )
812
813 VISIT_END( Stmt, node );
814}
815
816//--------------------------------------------------------------------------
817// CaseStmt
818template< typename core_t >
819const ast::Stmt * ast::Pass< core_t >::visit( const ast::CaseStmt * node ) {
820 VISIT_START( node );
821
822 VISIT(
823 maybe_accept( node, &CaseStmt::cond );
824 maybe_accept( node, &CaseStmt::stmts );
825 )
826
827 VISIT_END( Stmt, node );
828}
829
830//--------------------------------------------------------------------------
831// BranchStmt
832template< typename core_t >
833const ast::Stmt * ast::Pass< core_t >::visit( const ast::BranchStmt * node ) {
834 VISIT_START( node );
835 VISIT_END( Stmt, node );
836}
837
838//--------------------------------------------------------------------------
839// ReturnStmt
840template< typename core_t >
841const ast::Stmt * ast::Pass< core_t >::visit( const ast::ReturnStmt * node ) {
842 VISIT_START( node );
843
844 VISIT(
845 maybe_accept( node, &ReturnStmt::expr );
846 )
847
848 VISIT_END( Stmt, node );
849}
850
851//--------------------------------------------------------------------------
852// ThrowStmt
853template< typename core_t >
854const ast::Stmt * ast::Pass< core_t >::visit( const ast::ThrowStmt * node ) {
855 VISIT_START( node );
856
857 VISIT(
858 maybe_accept( node, &ThrowStmt::expr );
859 maybe_accept( node, &ThrowStmt::target );
860 )
861
862 VISIT_END( Stmt, node );
863}
864
865//--------------------------------------------------------------------------
866// TryStmt
867template< typename core_t >
868const ast::Stmt * ast::Pass< core_t >::visit( const ast::TryStmt * node ) {
869 VISIT_START( node );
870
871 VISIT(
872 maybe_accept( node, &TryStmt::body );
873 maybe_accept( node, &TryStmt::handlers );
874 maybe_accept( node, &TryStmt::finally );
875 )
876
877 VISIT_END( Stmt, node );
878}
879
880//--------------------------------------------------------------------------
881// CatchStmt
882template< typename core_t >
883const ast::Stmt * ast::Pass< core_t >::visit( const ast::CatchStmt * node ) {
884 VISIT_START( node );
885
886 VISIT({
887 // catch statements introduce a level of scope (for the caught exception)
888 guard_symtab guard { *this };
889 maybe_accept( node, &CatchStmt::decl );
890 maybe_accept( node, &CatchStmt::cond );
891 maybe_accept_as_compound( node, &CatchStmt::body );
892 })
893
894 VISIT_END( Stmt, node );
895}
896
897//--------------------------------------------------------------------------
898// FinallyStmt
899template< typename core_t >
900const ast::Stmt * ast::Pass< core_t >::visit( const ast::FinallyStmt * node ) {
901 VISIT_START( node );
902
903 VISIT(
904 maybe_accept( node, &FinallyStmt::body );
905 )
906
907 VISIT_END( Stmt, node );
908}
909
910//--------------------------------------------------------------------------
911// FinallyStmt
912template< typename core_t >
913const ast::Stmt * ast::Pass< core_t >::visit( const ast::SuspendStmt * node ) {
914 VISIT_START( node );
915
916 VISIT(
917 maybe_accept( node, &SuspendStmt::then );
918 )
919
920 VISIT_END( Stmt, node );
921}
922
923//--------------------------------------------------------------------------
924// WaitForStmt
925template< typename core_t >
926const ast::Stmt * ast::Pass< core_t >::visit( const ast::WaitForStmt * node ) {
927 VISIT_START( node );
928 // for( auto & clause : node->clauses ) {
929 // maybeAccept_impl( clause.target.function, *this );
930 // maybeAccept_impl( clause.target.arguments, *this );
931
932 // maybeAccept_impl( clause.statement, *this );
933 // maybeAccept_impl( clause.condition, *this );
934 // }
935
936 VISIT({
937 std::vector<WaitForStmt::Clause> new_clauses;
938 new_clauses.reserve( node->clauses.size() );
939 bool mutated = false;
940 for( const auto & clause : node->clauses ) {
941
942 const Expr * func = clause.target.func ? clause.target.func->accept(*this) : nullptr;
943 if(func != clause.target.func) mutated = true;
944
945 std::vector<ptr<Expr>> new_args;
946 new_args.reserve(clause.target.args.size());
947 for( const auto & arg : clause.target.args ) {
948 auto a = arg->accept(*this);
949 new_args.push_back( a );
950 if( a != arg ) mutated = true;
951 }
952
953 const Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr;
954 if(stmt != clause.stmt) mutated = true;
955
956 const Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr;
957 if(cond != clause.cond) mutated = true;
958
959 new_clauses.push_back( WaitForStmt::Clause{ {func, std::move(new_args) }, stmt, cond } );
960 }
961
962 if(mutated) {
963 auto n = __pass::mutate<core_t>(node);
964 n->clauses = std::move( new_clauses );
965 node = n;
966 }
967 })
968
969 #define maybe_accept(field) \
970 if(node->field) { \
971 auto nval = call_accept( node->field ); \
972 if(nval != node->field ) { \
973 auto nparent = __pass::mutate<core_t>(node); \
974 nparent->field = nval; \
975 node = nparent; \
976 } \
977 }
978
979 VISIT(
980 maybe_accept( timeout.time );
981 maybe_accept( timeout.stmt );
982 maybe_accept( timeout.cond );
983 maybe_accept( orElse.stmt );
984 maybe_accept( orElse.cond );
985 )
986
987 #undef maybe_accept
988
989 VISIT_END( Stmt, node );
990}
991
992//--------------------------------------------------------------------------
993// WithStmt
994template< typename core_t >
995const ast::Decl * ast::Pass< core_t >::visit( const ast::WithStmt * node ) {
996 VISIT_START( node );
997
998 VISIT(
999 maybe_accept( node, &WithStmt::exprs );
1000 {
1001 // catch statements introduce a level of scope (for the caught exception)
1002 guard_symtab guard { *this };
1003 __pass::symtab::addWith( core, 0, node->exprs, node );
1004 maybe_accept( node, &WithStmt::stmt );
1005 }
1006 )
1007 VISIT_END( Stmt, node );
1008}
1009
1010//--------------------------------------------------------------------------
1011// NullStmt
1012template< typename core_t >
1013const ast::NullStmt * ast::Pass< core_t >::visit( const ast::NullStmt * node ) {
1014 VISIT_START( node );
1015 VISIT_END( NullStmt, node );
1016}
1017
1018//--------------------------------------------------------------------------
1019// DeclStmt
1020template< typename core_t >
1021const ast::Stmt * ast::Pass< core_t >::visit( const ast::DeclStmt * node ) {
1022 VISIT_START( node );
1023
1024 VISIT(
1025 maybe_accept( node, &DeclStmt::decl );
1026 )
1027
1028 VISIT_END( Stmt, node );
1029}
1030
1031//--------------------------------------------------------------------------
1032// ImplicitCtorDtorStmt
1033template< typename core_t >
1034const ast::Stmt * ast::Pass< core_t >::visit( const ast::ImplicitCtorDtorStmt * node ) {
1035 VISIT_START( node );
1036
1037 // For now this isn't visited, it is unclear if this causes problem
1038 // if all tests are known to pass, remove this code
1039 VISIT(
1040 maybe_accept( node, &ImplicitCtorDtorStmt::callStmt );
1041 )
1042
1043 VISIT_END( Stmt, node );
1044}
1045
1046//--------------------------------------------------------------------------
1047// MutexStmt
1048template< typename core_t >
1049const ast::Stmt * ast::Pass< core_t >::visit( const ast::MutexStmt * node ) {
1050 VISIT_START( node );
1051
1052 VISIT(
1053 maybe_accept( node, &MutexStmt::mutexObjs );
1054 {
1055 // mutex statements introduce a level of scope (for the initialization)
1056 guard_symtab guard { *this };
1057 maybe_accept( node, &MutexStmt::stmt );
1058
1059 }
1060 )
1061
1062 VISIT_END( Stmt, node );
1063}
1064
1065//--------------------------------------------------------------------------
1066// ApplicationExpr
1067template< typename core_t >
1068const ast::Expr * ast::Pass< core_t >::visit( const ast::ApplicationExpr * node ) {
1069 VISIT_START( node );
1070
1071 VISIT(
1072 {
1073 guard_symtab guard { *this };
1074 maybe_accept( node, &ApplicationExpr::result );
1075 }
1076 maybe_accept( node, &ApplicationExpr::func );
1077 maybe_accept( node, &ApplicationExpr::args );
1078 )
1079
1080 VISIT_END( Expr, node );
1081}
1082
1083//--------------------------------------------------------------------------
1084// UntypedExpr
1085template< typename core_t >
1086const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedExpr * node ) {
1087 VISIT_START( node );
1088
1089 VISIT(
1090 {
1091 guard_symtab guard { *this };
1092 maybe_accept( node, &UntypedExpr::result );
1093 }
1094
1095 maybe_accept( node, &UntypedExpr::args );
1096 )
1097
1098 VISIT_END( Expr, node );
1099}
1100
1101//--------------------------------------------------------------------------
1102// NameExpr
1103template< typename core_t >
1104const ast::Expr * ast::Pass< core_t >::visit( const ast::NameExpr * node ) {
1105 VISIT_START( node );
1106
1107 VISIT({
1108 guard_symtab guard { *this };
1109 maybe_accept( node, &NameExpr::result );
1110 })
1111
1112 VISIT_END( Expr, node );
1113}
1114
1115//--------------------------------------------------------------------------
1116// CastExpr
1117template< typename core_t >
1118const ast::Expr * ast::Pass< core_t >::visit( const ast::CastExpr * node ) {
1119 VISIT_START( node );
1120
1121 VISIT({
1122 guard_symtab guard { *this };
1123 maybe_accept( node, &CastExpr::result );
1124 }
1125 maybe_accept( node, &CastExpr::arg );
1126 )
1127
1128 VISIT_END( Expr, node );
1129}
1130
1131//--------------------------------------------------------------------------
1132// KeywordCastExpr
1133template< typename core_t >
1134const ast::Expr * ast::Pass< core_t >::visit( const ast::KeywordCastExpr * node ) {
1135 VISIT_START( node );
1136
1137 VISIT({
1138 guard_symtab guard { *this };
1139 maybe_accept( node, &KeywordCastExpr::result );
1140 }
1141 maybe_accept( node, &KeywordCastExpr::arg );
1142 )
1143
1144 VISIT_END( Expr, node );
1145}
1146
1147//--------------------------------------------------------------------------
1148// VirtualCastExpr
1149template< typename core_t >
1150const ast::Expr * ast::Pass< core_t >::visit( const ast::VirtualCastExpr * node ) {
1151 VISIT_START( node );
1152
1153 VISIT({
1154 guard_symtab guard { *this };
1155 maybe_accept( node, &VirtualCastExpr::result );
1156 }
1157 maybe_accept( node, &VirtualCastExpr::arg );
1158 )
1159
1160 VISIT_END( Expr, node );
1161}
1162
1163//--------------------------------------------------------------------------
1164// AddressExpr
1165template< typename core_t >
1166const ast::Expr * ast::Pass< core_t >::visit( const ast::AddressExpr * node ) {
1167 VISIT_START( node );
1168
1169 VISIT({
1170 guard_symtab guard { *this };
1171 maybe_accept( node, &AddressExpr::result );
1172 }
1173 maybe_accept( node, &AddressExpr::arg );
1174 )
1175
1176 VISIT_END( Expr, node );
1177}
1178
1179//--------------------------------------------------------------------------
1180// LabelAddressExpr
1181template< typename core_t >
1182const ast::Expr * ast::Pass< core_t >::visit( const ast::LabelAddressExpr * node ) {
1183 VISIT_START( node );
1184
1185 VISIT({
1186 guard_symtab guard { *this };
1187 maybe_accept( node, &LabelAddressExpr::result );
1188 })
1189
1190 VISIT_END( Expr, node );
1191}
1192
1193//--------------------------------------------------------------------------
1194// UntypedMemberExpr
1195template< typename core_t >
1196const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedMemberExpr * node ) {
1197 VISIT_START( node );
1198
1199 VISIT({
1200 guard_symtab guard { *this };
1201 maybe_accept( node, &UntypedMemberExpr::result );
1202 }
1203 maybe_accept( node, &UntypedMemberExpr::aggregate );
1204 maybe_accept( node, &UntypedMemberExpr::member );
1205 )
1206
1207 VISIT_END( Expr, node );
1208}
1209
1210//--------------------------------------------------------------------------
1211// MemberExpr
1212template< typename core_t >
1213const ast::Expr * ast::Pass< core_t >::visit( const ast::MemberExpr * node ) {
1214 VISIT_START( node );
1215
1216 VISIT({
1217 guard_symtab guard { *this };
1218 maybe_accept( node, &MemberExpr::result );
1219 }
1220 maybe_accept( node, &MemberExpr::aggregate );
1221 )
1222
1223 VISIT_END( Expr, node );
1224}
1225
1226//--------------------------------------------------------------------------
1227// VariableExpr
1228template< typename core_t >
1229const ast::Expr * ast::Pass< core_t >::visit( const ast::VariableExpr * node ) {
1230 VISIT_START( node );
1231
1232 VISIT({
1233 guard_symtab guard { *this };
1234 maybe_accept( node, &VariableExpr::result );
1235 })
1236
1237 VISIT_END( Expr, node );
1238}
1239
1240//--------------------------------------------------------------------------
1241// ConstantExpr
1242template< typename core_t >
1243const ast::Expr * ast::Pass< core_t >::visit( const ast::ConstantExpr * node ) {
1244 VISIT_START( node );
1245
1246 VISIT({
1247 guard_symtab guard { *this };
1248 maybe_accept( node, &ConstantExpr::result );
1249 })
1250
1251 VISIT_END( Expr, node );
1252}
1253
1254//--------------------------------------------------------------------------
1255// SizeofExpr
1256template< typename core_t >
1257const ast::Expr * ast::Pass< core_t >::visit( const ast::SizeofExpr * node ) {
1258 VISIT_START( node );
1259
1260 VISIT({
1261 guard_symtab guard { *this };
1262 maybe_accept( node, &SizeofExpr::result );
1263 }
1264 if ( node->type ) {
1265 maybe_accept( node, &SizeofExpr::type );
1266 } else {
1267 maybe_accept( node, &SizeofExpr::expr );
1268 }
1269 )
1270
1271 VISIT_END( Expr, node );
1272}
1273
1274//--------------------------------------------------------------------------
1275// AlignofExpr
1276template< typename core_t >
1277const ast::Expr * ast::Pass< core_t >::visit( const ast::AlignofExpr * node ) {
1278 VISIT_START( node );
1279
1280 VISIT({
1281 guard_symtab guard { *this };
1282 maybe_accept( node, &AlignofExpr::result );
1283 }
1284 if ( node->type ) {
1285 maybe_accept( node, &AlignofExpr::type );
1286 } else {
1287 maybe_accept( node, &AlignofExpr::expr );
1288 }
1289 )
1290
1291 VISIT_END( Expr, node );
1292}
1293
1294//--------------------------------------------------------------------------
1295// UntypedOffsetofExpr
1296template< typename core_t >
1297const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedOffsetofExpr * node ) {
1298 VISIT_START( node );
1299
1300 VISIT({
1301 guard_symtab guard { *this };
1302 maybe_accept( node, &UntypedOffsetofExpr::result );
1303 }
1304 maybe_accept( node, &UntypedOffsetofExpr::type );
1305 )
1306
1307 VISIT_END( Expr, node );
1308}
1309
1310//--------------------------------------------------------------------------
1311// OffsetofExpr
1312template< typename core_t >
1313const ast::Expr * ast::Pass< core_t >::visit( const ast::OffsetofExpr * node ) {
1314 VISIT_START( node );
1315
1316 VISIT({
1317 guard_symtab guard { *this };
1318 maybe_accept( node, &OffsetofExpr::result );
1319 }
1320 maybe_accept( node, &OffsetofExpr::type );
1321 )
1322
1323 VISIT_END( Expr, node );
1324}
1325
1326//--------------------------------------------------------------------------
1327// OffsetPackExpr
1328template< typename core_t >
1329const ast::Expr * ast::Pass< core_t >::visit( const ast::OffsetPackExpr * node ) {
1330 VISIT_START( node );
1331
1332 VISIT({
1333 guard_symtab guard { *this };
1334 maybe_accept( node, &OffsetPackExpr::result );
1335 }
1336 maybe_accept( node, &OffsetPackExpr::type );
1337 )
1338
1339 VISIT_END( Expr, node );
1340}
1341
1342//--------------------------------------------------------------------------
1343// LogicalExpr
1344template< typename core_t >
1345const ast::Expr * ast::Pass< core_t >::visit( const ast::LogicalExpr * node ) {
1346 VISIT_START( node );
1347
1348 VISIT({
1349 guard_symtab guard { *this };
1350 maybe_accept( node, &LogicalExpr::result );
1351 }
1352 maybe_accept( node, &LogicalExpr::arg1 );
1353 maybe_accept( node, &LogicalExpr::arg2 );
1354 )
1355
1356 VISIT_END( Expr, node );
1357}
1358
1359//--------------------------------------------------------------------------
1360// ConditionalExpr
1361template< typename core_t >
1362const ast::Expr * ast::Pass< core_t >::visit( const ast::ConditionalExpr * node ) {
1363 VISIT_START( node );
1364
1365 VISIT({
1366 guard_symtab guard { *this };
1367 maybe_accept( node, &ConditionalExpr::result );
1368 }
1369 maybe_accept( node, &ConditionalExpr::arg1 );
1370 maybe_accept( node, &ConditionalExpr::arg2 );
1371 maybe_accept( node, &ConditionalExpr::arg3 );
1372 )
1373
1374 VISIT_END( Expr, node );
1375}
1376
1377//--------------------------------------------------------------------------
1378// CommaExpr
1379template< typename core_t >
1380const ast::Expr * ast::Pass< core_t >::visit( const ast::CommaExpr * node ) {
1381 VISIT_START( node );
1382
1383 VISIT({
1384 guard_symtab guard { *this };
1385 maybe_accept( node, &CommaExpr::result );
1386 }
1387 maybe_accept( node, &CommaExpr::arg1 );
1388 maybe_accept( node, &CommaExpr::arg2 );
1389 )
1390
1391 VISIT_END( Expr, node );
1392}
1393
1394//--------------------------------------------------------------------------
1395// TypeExpr
1396template< typename core_t >
1397const ast::Expr * ast::Pass< core_t >::visit( const ast::TypeExpr * node ) {
1398 VISIT_START( node );
1399
1400 VISIT({
1401 guard_symtab guard { *this };
1402 maybe_accept( node, &TypeExpr::result );
1403 }
1404 maybe_accept( node, &TypeExpr::type );
1405 )
1406
1407 VISIT_END( Expr, node );
1408}
1409
1410//--------------------------------------------------------------------------
1411// AsmExpr
1412template< typename core_t >
1413const ast::Expr * ast::Pass< core_t >::visit( const ast::AsmExpr * node ) {
1414 VISIT_START( node );
1415
1416 VISIT({
1417 guard_symtab guard { *this };
1418 maybe_accept( node, &AsmExpr::result );
1419 }
1420 maybe_accept( node, &AsmExpr::constraint );
1421 maybe_accept( node, &AsmExpr::operand );
1422 )
1423
1424 VISIT_END( Expr, node );
1425}
1426
1427//--------------------------------------------------------------------------
1428// ImplicitCopyCtorExpr
1429template< typename core_t >
1430const ast::Expr * ast::Pass< core_t >::visit( const ast::ImplicitCopyCtorExpr * node ) {
1431 VISIT_START( node );
1432
1433 VISIT({
1434 guard_symtab guard { *this };
1435 maybe_accept( node, &ImplicitCopyCtorExpr::result );
1436 }
1437 maybe_accept( node, &ImplicitCopyCtorExpr::callExpr );
1438 )
1439
1440 VISIT_END( Expr, node );
1441}
1442
1443//--------------------------------------------------------------------------
1444// ConstructorExpr
1445template< typename core_t >
1446const ast::Expr * ast::Pass< core_t >::visit( const ast::ConstructorExpr * node ) {
1447 VISIT_START( node );
1448
1449 VISIT({
1450 guard_symtab guard { *this };
1451 maybe_accept( node, &ConstructorExpr::result );
1452 }
1453 maybe_accept( node, &ConstructorExpr::callExpr );
1454 )
1455
1456 VISIT_END( Expr, node );
1457}
1458
1459//--------------------------------------------------------------------------
1460// CompoundLiteralExpr
1461template< typename core_t >
1462const ast::Expr * ast::Pass< core_t >::visit( const ast::CompoundLiteralExpr * node ) {
1463 VISIT_START( node );
1464
1465 VISIT({
1466 guard_symtab guard { *this };
1467 maybe_accept( node, &CompoundLiteralExpr::result );
1468 }
1469 maybe_accept( node, &CompoundLiteralExpr::init );
1470 )
1471
1472 VISIT_END( Expr, node );
1473}
1474
1475//--------------------------------------------------------------------------
1476// RangeExpr
1477template< typename core_t >
1478const ast::Expr * ast::Pass< core_t >::visit( const ast::RangeExpr * node ) {
1479 VISIT_START( node );
1480
1481 VISIT({
1482 guard_symtab guard { *this };
1483 maybe_accept( node, &RangeExpr::result );
1484 }
1485 maybe_accept( node, &RangeExpr::low );
1486 maybe_accept( node, &RangeExpr::high );
1487 )
1488
1489 VISIT_END( Expr, node );
1490}
1491
1492//--------------------------------------------------------------------------
1493// UntypedTupleExpr
1494template< typename core_t >
1495const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedTupleExpr * node ) {
1496 VISIT_START( node );
1497
1498 VISIT({
1499 guard_symtab guard { *this };
1500 maybe_accept( node, &UntypedTupleExpr::result );
1501 }
1502 maybe_accept( node, &UntypedTupleExpr::exprs );
1503 )
1504
1505 VISIT_END( Expr, node );
1506}
1507
1508//--------------------------------------------------------------------------
1509// TupleExpr
1510template< typename core_t >
1511const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleExpr * node ) {
1512 VISIT_START( node );
1513
1514 VISIT({
1515 guard_symtab guard { *this };
1516 maybe_accept( node, &TupleExpr::result );
1517 }
1518 maybe_accept( node, &TupleExpr::exprs );
1519 )
1520
1521 VISIT_END( Expr, node );
1522}
1523
1524//--------------------------------------------------------------------------
1525// TupleIndexExpr
1526template< typename core_t >
1527const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleIndexExpr * node ) {
1528 VISIT_START( node );
1529
1530 VISIT({
1531 guard_symtab guard { *this };
1532 maybe_accept( node, &TupleIndexExpr::result );
1533 }
1534 maybe_accept( node, &TupleIndexExpr::tuple );
1535 )
1536
1537 VISIT_END( Expr, node );
1538}
1539
1540//--------------------------------------------------------------------------
1541// TupleAssignExpr
1542template< typename core_t >
1543const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleAssignExpr * node ) {
1544 VISIT_START( node );
1545
1546 VISIT({
1547 guard_symtab guard { *this };
1548 maybe_accept( node, &TupleAssignExpr::result );
1549 }
1550 maybe_accept( node, &TupleAssignExpr::stmtExpr );
1551 )
1552
1553 VISIT_END( Expr, node );
1554}
1555
1556//--------------------------------------------------------------------------
1557// StmtExpr
1558template< typename core_t >
1559const ast::Expr * ast::Pass< core_t >::visit( const ast::StmtExpr * node ) {
1560 VISIT_START( node );
1561
1562 VISIT(// don't want statements from outer CompoundStmts to be added to this StmtExpr
1563 // get the stmts that will need to be spliced in
1564 auto stmts_before = __pass::stmtsToAddBefore( core, 0);
1565 auto stmts_after = __pass::stmtsToAddAfter ( core, 0);
1566
1567 // These may be modified by subnode but most be restored once we exit this statemnet.
1568 ValueGuardPtr< const ast::TypeSubstitution * > __old_env( __pass::typeSubs( core, 0 ) );
1569 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
1570 ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after );
1571
1572 {
1573 guard_symtab guard { *this };
1574 maybe_accept( node, &StmtExpr::result );
1575 }
1576 maybe_accept( node, &StmtExpr::stmts );
1577 maybe_accept( node, &StmtExpr::returnDecls );
1578 maybe_accept( node, &StmtExpr::dtors );
1579 )
1580
1581 VISIT_END( Expr, node );
1582}
1583
1584//--------------------------------------------------------------------------
1585// UniqueExpr
1586template< typename core_t >
1587const ast::Expr * ast::Pass< core_t >::visit( const ast::UniqueExpr * node ) {
1588 VISIT_START( node );
1589
1590 VISIT({
1591 guard_symtab guard { *this };
1592 maybe_accept( node, &UniqueExpr::result );
1593 }
1594 maybe_accept( node, &UniqueExpr::expr );
1595 )
1596
1597 VISIT_END( Expr, node );
1598}
1599
1600//--------------------------------------------------------------------------
1601// UntypedInitExpr
1602template< typename core_t >
1603const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedInitExpr * node ) {
1604 VISIT_START( node );
1605
1606 VISIT({
1607 guard_symtab guard { *this };
1608 maybe_accept( node, &UntypedInitExpr::result );
1609 }
1610 maybe_accept( node, &UntypedInitExpr::expr );
1611 // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.
1612 )
1613
1614 VISIT_END( Expr, node );
1615}
1616
1617//--------------------------------------------------------------------------
1618// InitExpr
1619template< typename core_t >
1620const ast::Expr * ast::Pass< core_t >::visit( const ast::InitExpr * node ) {
1621 VISIT_START( node );
1622
1623 VISIT({
1624 guard_symtab guard { *this };
1625 maybe_accept( node, &InitExpr::result );
1626 }
1627 maybe_accept( node, &InitExpr::expr );
1628 maybe_accept( node, &InitExpr::designation );
1629 )
1630
1631 VISIT_END( Expr, node );
1632}
1633
1634//--------------------------------------------------------------------------
1635// DeletedExpr
1636template< typename core_t >
1637const ast::Expr * ast::Pass< core_t >::visit( const ast::DeletedExpr * node ) {
1638 VISIT_START( node );
1639
1640 VISIT({
1641 guard_symtab guard { *this };
1642 maybe_accept( node, &DeletedExpr::result );
1643 }
1644 maybe_accept( node, &DeletedExpr::expr );
1645 // don't visit deleteStmt, because it is a pointer to somewhere else in the tree.
1646 )
1647
1648 VISIT_END( Expr, node );
1649}
1650
1651//--------------------------------------------------------------------------
1652// DefaultArgExpr
1653template< typename core_t >
1654const ast::Expr * ast::Pass< core_t >::visit( const ast::DefaultArgExpr * node ) {
1655 VISIT_START( node );
1656
1657 VISIT({
1658 guard_symtab guard { *this };
1659 maybe_accept( node, &DefaultArgExpr::result );
1660 }
1661 maybe_accept( node, &DefaultArgExpr::expr );
1662 )
1663
1664 VISIT_END( Expr, node );
1665}
1666
1667//--------------------------------------------------------------------------
1668// GenericExpr
1669template< typename core_t >
1670const ast::Expr * ast::Pass< core_t >::visit( const ast::GenericExpr * node ) {
1671 VISIT_START( node );
1672
1673 VISIT({
1674 guard_symtab guard { *this };
1675 maybe_accept( node, &GenericExpr::result );
1676 }
1677 maybe_accept( node, &GenericExpr::control );
1678
1679 std::vector<GenericExpr::Association> new_kids;
1680 new_kids.reserve(node->associations.size());
1681 bool mutated = false;
1682 for( const auto & assoc : node->associations ) {
1683 const Type * type = nullptr;
1684 if( assoc.type ) {
1685 guard_symtab guard { *this };
1686 type = assoc.type->accept( *this );
1687 if( type != assoc.type ) mutated = true;
1688 }
1689 const Expr * expr = nullptr;
1690 if( assoc.expr ) {
1691 expr = assoc.expr->accept( *this );
1692 if( expr != assoc.expr ) mutated = true;
1693 }
1694 new_kids.emplace_back( type, expr );
1695 }
1696
1697 if(mutated) {
1698 auto n = __pass::mutate<core_t>(node);
1699 n->associations = std::move( new_kids );
1700 node = n;
1701 }
1702 )
1703
1704 VISIT_END( Expr, node );
1705}
1706
1707//--------------------------------------------------------------------------
1708// VoidType
1709template< typename core_t >
1710const ast::Type * ast::Pass< core_t >::visit( const ast::VoidType * node ) {
1711 VISIT_START( node );
1712
1713 VISIT_END( Type, node );
1714}
1715
1716//--------------------------------------------------------------------------
1717// BasicType
1718template< typename core_t >
1719const ast::Type * ast::Pass< core_t >::visit( const ast::BasicType * node ) {
1720 VISIT_START( node );
1721
1722 VISIT_END( Type, node );
1723}
1724
1725//--------------------------------------------------------------------------
1726// PointerType
1727template< typename core_t >
1728const ast::Type * ast::Pass< core_t >::visit( const ast::PointerType * node ) {
1729 VISIT_START( node );
1730
1731 VISIT(
1732 // xxx - should PointerType visit/mutate dimension?
1733 maybe_accept( node, &PointerType::base );
1734 )
1735
1736 VISIT_END( Type, node );
1737}
1738
1739//--------------------------------------------------------------------------
1740// ArrayType
1741template< typename core_t >
1742const ast::Type * ast::Pass< core_t >::visit( const ast::ArrayType * node ) {
1743 VISIT_START( node );
1744
1745 VISIT(
1746 maybe_accept( node, &ArrayType::dimension );
1747 maybe_accept( node, &ArrayType::base );
1748 )
1749
1750 VISIT_END( Type, node );
1751}
1752
1753//--------------------------------------------------------------------------
1754// ReferenceType
1755template< typename core_t >
1756const ast::Type * ast::Pass< core_t >::visit( const ast::ReferenceType * node ) {
1757 VISIT_START( node );
1758
1759 VISIT(
1760 maybe_accept( node, &ReferenceType::base );
1761 )
1762
1763 VISIT_END( Type, node );
1764}
1765
1766//--------------------------------------------------------------------------
1767// QualifiedType
1768template< typename core_t >
1769const ast::Type * ast::Pass< core_t >::visit( const ast::QualifiedType * node ) {
1770 VISIT_START( node );
1771
1772 VISIT(
1773 maybe_accept( node, &QualifiedType::parent );
1774 maybe_accept( node, &QualifiedType::child );
1775 )
1776
1777 VISIT_END( Type, node );
1778}
1779
1780//--------------------------------------------------------------------------
1781// FunctionType
1782template< typename core_t >
1783const ast::Type * ast::Pass< core_t >::visit( const ast::FunctionType * node ) {
1784 VISIT_START( node );
1785
1786 VISIT({
1787 // guard_forall_subs forall_guard { *this, node };
1788 // mutate_forall( node );
1789 maybe_accept( node, &FunctionType::assertions );
1790 maybe_accept( node, &FunctionType::returns );
1791 maybe_accept( node, &FunctionType::params );
1792 })
1793
1794 VISIT_END( Type, node );
1795}
1796
1797//--------------------------------------------------------------------------
1798// StructInstType
1799template< typename core_t >
1800const ast::Type * ast::Pass< core_t >::visit( const ast::StructInstType * node ) {
1801 VISIT_START( node );
1802
1803 __pass::symtab::addStruct( core, 0, node->name );
1804
1805 VISIT({
1806 guard_symtab guard { *this };
1807 maybe_accept( node, &StructInstType::params );
1808 })
1809
1810 VISIT_END( Type, node );
1811}
1812
1813//--------------------------------------------------------------------------
1814// UnionInstType
1815template< typename core_t >
1816const ast::Type * ast::Pass< core_t >::visit( const ast::UnionInstType * node ) {
1817 VISIT_START( node );
1818
1819 __pass::symtab::addUnion( core, 0, node->name );
1820
1821 VISIT({
1822 guard_symtab guard { *this };
1823 maybe_accept( node, &UnionInstType::params );
1824 })
1825
1826 VISIT_END( Type, node );
1827}
1828
1829//--------------------------------------------------------------------------
1830// EnumInstType
1831template< typename core_t >
1832const ast::Type * ast::Pass< core_t >::visit( const ast::EnumInstType * node ) {
1833 VISIT_START( node );
1834
1835 VISIT({
1836 maybe_accept( node, &EnumInstType::params );
1837 })
1838
1839 VISIT_END( Type, node );
1840}
1841
1842//--------------------------------------------------------------------------
1843// TraitInstType
1844template< typename core_t >
1845const ast::Type * ast::Pass< core_t >::visit( const ast::TraitInstType * node ) {
1846 VISIT_START( node );
1847
1848 VISIT({
1849 maybe_accept( node, &TraitInstType::params );
1850 })
1851
1852 VISIT_END( Type, node );
1853}
1854
1855//--------------------------------------------------------------------------
1856// TypeInstType
1857template< typename core_t >
1858const ast::Type * ast::Pass< core_t >::visit( const ast::TypeInstType * node ) {
1859 VISIT_START( node );
1860
1861 VISIT(
1862 {
1863 maybe_accept( node, &TypeInstType::params );
1864 }
1865 // ensure that base re-bound if doing substitution
1866 __pass::forall::replace( core, 0, node );
1867 )
1868
1869 VISIT_END( Type, node );
1870}
1871
1872//--------------------------------------------------------------------------
1873// TupleType
1874template< typename core_t >
1875const ast::Type * ast::Pass< core_t >::visit( const ast::TupleType * node ) {
1876 VISIT_START( node );
1877
1878 VISIT(
1879 maybe_accept( node, &TupleType::types );
1880 maybe_accept( node, &TupleType::members );
1881 )
1882
1883 VISIT_END( Type, node );
1884}
1885
1886//--------------------------------------------------------------------------
1887// TypeofType
1888template< typename core_t >
1889const ast::Type * ast::Pass< core_t >::visit( const ast::TypeofType * node ) {
1890 VISIT_START( node );
1891
1892 VISIT(
1893 maybe_accept( node, &TypeofType::expr );
1894 )
1895
1896 VISIT_END( Type, node );
1897}
1898
1899//--------------------------------------------------------------------------
1900// VTableType
1901template< typename core_t >
1902const ast::Type * ast::Pass< core_t >::visit( const ast::VTableType * node ) {
1903 VISIT_START( node );
1904
1905 VISIT(
1906 maybe_accept( node, &VTableType::base );
1907 )
1908
1909 VISIT_END( Type, node );
1910}
1911
1912//--------------------------------------------------------------------------
1913// VarArgsType
1914template< typename core_t >
1915const ast::Type * ast::Pass< core_t >::visit( const ast::VarArgsType * node ) {
1916 VISIT_START( node );
1917
1918 VISIT_END( Type, node );
1919}
1920
1921//--------------------------------------------------------------------------
1922// ZeroType
1923template< typename core_t >
1924const ast::Type * ast::Pass< core_t >::visit( const ast::ZeroType * node ) {
1925 VISIT_START( node );
1926
1927 VISIT_END( Type, node );
1928}
1929
1930//--------------------------------------------------------------------------
1931// OneType
1932template< typename core_t >
1933const ast::Type * ast::Pass< core_t >::visit( const ast::OneType * node ) {
1934 VISIT_START( node );
1935
1936 VISIT_END( Type, node );
1937}
1938
1939//--------------------------------------------------------------------------
1940// GlobalScopeType
1941template< typename core_t >
1942const ast::Type * ast::Pass< core_t >::visit( const ast::GlobalScopeType * node ) {
1943 VISIT_START( node );
1944
1945 VISIT_END( Type, node );
1946}
1947
1948
1949//--------------------------------------------------------------------------
1950// Designation
1951template< typename core_t >
1952const ast::Designation * ast::Pass< core_t >::visit( const ast::Designation * node ) {
1953 VISIT_START( node );
1954
1955 VISIT( maybe_accept( node, &Designation::designators ); )
1956
1957 VISIT_END( Designation, node );
1958}
1959
1960//--------------------------------------------------------------------------
1961// SingleInit
1962template< typename core_t >
1963const ast::Init * ast::Pass< core_t >::visit( const ast::SingleInit * node ) {
1964 VISIT_START( node );
1965
1966 VISIT(
1967 maybe_accept( node, &SingleInit::value );
1968 )
1969
1970 VISIT_END( Init, node );
1971}
1972
1973//--------------------------------------------------------------------------
1974// ListInit
1975template< typename core_t >
1976const ast::Init * ast::Pass< core_t >::visit( const ast::ListInit * node ) {
1977 VISIT_START( node );
1978
1979 VISIT(
1980 maybe_accept( node, &ListInit::designations );
1981 maybe_accept( node, &ListInit::initializers );
1982 )
1983
1984 VISIT_END( Init, node );
1985}
1986
1987//--------------------------------------------------------------------------
1988// ConstructorInit
1989template< typename core_t >
1990const ast::Init * ast::Pass< core_t >::visit( const ast::ConstructorInit * node ) {
1991 VISIT_START( node );
1992
1993 VISIT(
1994 maybe_accept( node, &ConstructorInit::ctor );
1995 maybe_accept( node, &ConstructorInit::dtor );
1996 maybe_accept( node, &ConstructorInit::init );
1997 )
1998
1999 VISIT_END( Init, node );
2000}
2001
2002//--------------------------------------------------------------------------
2003// Attribute
2004template< typename core_t >
2005const ast::Attribute * ast::Pass< core_t >::visit( const ast::Attribute * node ) {
2006 VISIT_START( node );
2007
2008 VISIT(
2009 maybe_accept( node, &Attribute::params );
2010 )
2011
2012 VISIT_END( Attribute, node );
2013}
2014
2015//--------------------------------------------------------------------------
2016// TypeSubstitution
2017template< typename core_t >
2018const ast::TypeSubstitution * ast::Pass< core_t >::visit( const ast::TypeSubstitution * node ) {
2019 VISIT_START( node );
2020
2021 VISIT(
2022 {
2023 bool mutated = false;
2024 std::unordered_map< ast::TypeInstType::TypeEnvKey, ast::ptr< ast::Type > > new_map;
2025 for ( const auto & p : node->typeEnv ) {
2026 guard_symtab guard { *this };
2027 auto new_node = p.second->accept( *this );
2028 if (new_node != p.second) mutated = true;
2029 new_map.insert({ p.first, new_node });
2030 }
2031 if (mutated) {
2032 auto new_node = __pass::mutate<core_t>( node );
2033 new_node->typeEnv.swap( new_map );
2034 node = new_node;
2035 }
2036 }
2037 )
2038
2039 VISIT_END( TypeSubstitution, node );
2040}
2041
2042#undef VISIT_START
2043#undef VISIT
2044#undef VISIT_END
Note: See TracBrowser for help on using the repository browser.