source: src/AST/Pass.impl.hpp @ 9cc3a18

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9cc3a18 was 2d019af, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

parser global pragmas, fixes #241

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