source: src/AST/Pass.impl.hpp @ ab5c0008

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ab5c0008 was 665f432, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed trac #149 where operand names in asm statements where incorrectly resolved (i.e., should not have been resolved)

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