source: src/AST/Pass.impl.hpp @ 81da70a5

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 81da70a5 was 7030dab, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Merge branch 'master' into new-ast

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