source: src/AST/Pass.impl.hpp @ 12b4ab4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 12b4ab4 was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 3 years ago

reimplement function type and eliminate deep copy

  • Property mode set to 100644
File size: 57.3 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// StaticAssertDecl
649template< typename core_t >
650const ast::StaticAssertDecl * ast::Pass< core_t >::visit( const ast::StaticAssertDecl * node ) {
651        VISIT_START( node );
652
653        VISIT(
654                maybe_accept( node, &StaticAssertDecl::cond );
655                maybe_accept( node, &StaticAssertDecl::msg  );
656        )
657
658        VISIT_END( StaticAssertDecl, node );
659}
660
661//--------------------------------------------------------------------------
662// CompoundStmt
663template< typename core_t >
664const ast::CompoundStmt * ast::Pass< core_t >::visit( const ast::CompoundStmt * node ) {
665        VISIT_START( node );
666        VISIT(
667                // Do not enter (or leave) a new scope if atFunctionTop. Remember to save the result.
668                auto guard1 = makeFuncGuard( [this, enterScope = !this->atFunctionTop]() {
669                        if ( enterScope ) {
670                                __pass::symtab::enter(core, 0);
671                                __pass::scope::enter(core, 0);
672                        }
673                }, [this, leaveScope = !this->atFunctionTop]() {
674                        if ( leaveScope ) {
675                                __pass::symtab::leave(core, 0);
676                                __pass::scope::leave(core, 0);
677                        }
678                });
679                ValueGuard< bool > guard2( atFunctionTop );
680                atFunctionTop = false;
681                guard_scope guard3 { *this };
682                maybe_accept( node, &CompoundStmt::kids );
683        )
684        VISIT_END( CompoundStmt, node );
685}
686
687//--------------------------------------------------------------------------
688// ExprStmt
689template< typename core_t >
690const ast::Stmt * ast::Pass< core_t >::visit( const ast::ExprStmt * node ) {
691        VISIT_START( node );
692
693        VISIT(
694                maybe_accept( node, &ExprStmt::expr );
695        )
696
697        VISIT_END( Stmt, node );
698}
699
700//--------------------------------------------------------------------------
701// AsmStmt
702template< typename core_t >
703const ast::Stmt * ast::Pass< core_t >::visit( const ast::AsmStmt * node ) {
704        VISIT_START( node )
705
706        VISIT(
707                maybe_accept( node, &AsmStmt::instruction );
708                maybe_accept( node, &AsmStmt::output      );
709                maybe_accept( node, &AsmStmt::input       );
710                maybe_accept( node, &AsmStmt::clobber     );
711        )
712
713        VISIT_END( Stmt, node );
714}
715
716//--------------------------------------------------------------------------
717// DirectiveStmt
718template< typename core_t >
719const ast::Stmt * ast::Pass< core_t >::visit( const ast::DirectiveStmt * node ) {
720        VISIT_START( node )
721
722        VISIT_END( Stmt, node );
723}
724
725//--------------------------------------------------------------------------
726// IfStmt
727template< typename core_t >
728const ast::Stmt * ast::Pass< core_t >::visit( const ast::IfStmt * node ) {
729        VISIT_START( node );
730
731        VISIT({
732                // if statements introduce a level of scope (for the initialization)
733                guard_symtab guard { *this };
734                maybe_accept( node, &IfStmt::inits    );
735                maybe_accept( node, &IfStmt::cond     );
736                maybe_accept_as_compound( node, &IfStmt::thenPart );
737                maybe_accept_as_compound( node, &IfStmt::elsePart );
738        })
739
740        VISIT_END( Stmt, node );
741}
742
743//--------------------------------------------------------------------------
744// WhileStmt
745template< typename core_t >
746const ast::Stmt * ast::Pass< core_t >::visit( const ast::WhileStmt * node ) {
747        VISIT_START( node );
748
749        VISIT({
750                // while statements introduce a level of scope (for the initialization)
751                guard_symtab guard { *this };
752                maybe_accept( node, &WhileStmt::inits );
753                maybe_accept( node, &WhileStmt::cond  );
754                maybe_accept_as_compound( node, &WhileStmt::body  );
755        })
756
757        VISIT_END( Stmt, node );
758}
759
760//--------------------------------------------------------------------------
761// ForStmt
762template< typename core_t >
763const ast::Stmt * ast::Pass< core_t >::visit( const ast::ForStmt * node ) {
764        VISIT_START( node );
765
766        VISIT({
767                // for statements introduce a level of scope (for the initialization)
768                guard_symtab guard { *this };
769                // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later.
770                maybe_accept( node, &ForStmt::inits );
771                maybe_accept( node, &ForStmt::cond  );
772                maybe_accept( node, &ForStmt::inc   );
773                maybe_accept_as_compound( node, &ForStmt::body  );
774        })
775
776        VISIT_END( Stmt, node );
777}
778
779//--------------------------------------------------------------------------
780// SwitchStmt
781template< typename core_t >
782const ast::Stmt * ast::Pass< core_t >::visit( const ast::SwitchStmt * node ) {
783        VISIT_START( node );
784
785        VISIT(
786                maybe_accept( node, &SwitchStmt::cond  );
787                maybe_accept( node, &SwitchStmt::stmts );
788        )
789
790        VISIT_END( Stmt, node );
791}
792
793//--------------------------------------------------------------------------
794// CaseStmt
795template< typename core_t >
796const ast::Stmt * ast::Pass< core_t >::visit( const ast::CaseStmt * node ) {
797        VISIT_START( node );
798
799        VISIT(
800                maybe_accept( node, &CaseStmt::cond  );
801                maybe_accept( node, &CaseStmt::stmts );
802        )
803
804        VISIT_END( Stmt, node );
805}
806
807//--------------------------------------------------------------------------
808// BranchStmt
809template< typename core_t >
810const ast::Stmt * ast::Pass< core_t >::visit( const ast::BranchStmt * node ) {
811        VISIT_START( node );
812        VISIT_END( Stmt, node );
813}
814
815//--------------------------------------------------------------------------
816// ReturnStmt
817template< typename core_t >
818const ast::Stmt * ast::Pass< core_t >::visit( const ast::ReturnStmt * node ) {
819        VISIT_START( node );
820
821        VISIT(
822                maybe_accept( node, &ReturnStmt::expr );
823        )
824
825        VISIT_END( Stmt, node );
826}
827
828//--------------------------------------------------------------------------
829// ThrowStmt
830template< typename core_t >
831const ast::Stmt * ast::Pass< core_t >::visit( const ast::ThrowStmt * node ) {
832        VISIT_START( node );
833
834        VISIT(
835                maybe_accept( node, &ThrowStmt::expr   );
836                maybe_accept( node, &ThrowStmt::target );
837        )
838
839        VISIT_END( Stmt, node );
840}
841
842//--------------------------------------------------------------------------
843// TryStmt
844template< typename core_t >
845const ast::Stmt * ast::Pass< core_t >::visit( const ast::TryStmt * node ) {
846        VISIT_START( node );
847
848        VISIT(
849                maybe_accept( node, &TryStmt::body     );
850                maybe_accept( node, &TryStmt::handlers );
851                maybe_accept( node, &TryStmt::finally  );
852        )
853
854        VISIT_END( Stmt, node );
855}
856
857//--------------------------------------------------------------------------
858// CatchStmt
859template< typename core_t >
860const ast::Stmt * ast::Pass< core_t >::visit( const ast::CatchStmt * node ) {
861        VISIT_START( node );
862
863        VISIT({
864                // catch statements introduce a level of scope (for the caught exception)
865                guard_symtab guard { *this };
866                maybe_accept( node, &CatchStmt::decl );
867                maybe_accept( node, &CatchStmt::cond );
868                maybe_accept_as_compound( node, &CatchStmt::body );
869        })
870
871        VISIT_END( Stmt, node );
872}
873
874//--------------------------------------------------------------------------
875// FinallyStmt
876template< typename core_t >
877const ast::Stmt * ast::Pass< core_t >::visit( const ast::FinallyStmt * node ) {
878        VISIT_START( node );
879
880        VISIT(
881                maybe_accept( node, &FinallyStmt::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::SuspendStmt * node ) {
891        VISIT_START( node );
892
893        VISIT(
894                maybe_accept( node, &SuspendStmt::then   );
895        )
896
897        VISIT_END( Stmt, node );
898}
899
900//--------------------------------------------------------------------------
901// WaitForStmt
902template< typename core_t >
903const ast::Stmt * ast::Pass< core_t >::visit( const ast::WaitForStmt * node ) {
904        VISIT_START( node );
905                // for( auto & clause : node->clauses ) {
906                //      maybeAccept_impl( clause.target.function, *this );
907                //      maybeAccept_impl( clause.target.arguments, *this );
908
909                //      maybeAccept_impl( clause.statement, *this );
910                //      maybeAccept_impl( clause.condition, *this );
911                // }
912
913        VISIT({
914                std::vector<WaitForStmt::Clause> new_clauses;
915                new_clauses.reserve( node->clauses.size() );
916                bool mutated = false;
917                for( const auto & clause : node->clauses ) {
918
919                        const Expr * func = clause.target.func ? clause.target.func->accept(*this) : nullptr;
920                        if(func != clause.target.func) mutated = true;
921
922                        std::vector<ptr<Expr>> new_args;
923                        new_args.reserve(clause.target.args.size());
924                        for( const auto & arg : clause.target.args ) {
925                                auto a = arg->accept(*this);
926                                new_args.push_back( a );
927                                if( a != arg ) mutated = true;
928                        }
929
930                        const Stmt * stmt = clause.stmt ? clause.stmt->accept(*this) : nullptr;
931                        if(stmt != clause.stmt) mutated = true;
932
933                        const Expr * cond = clause.cond ? clause.cond->accept(*this) : nullptr;
934                        if(cond != clause.cond) mutated = true;
935
936                        new_clauses.push_back( WaitForStmt::Clause{ {func, std::move(new_args) }, stmt, cond } );
937                }
938
939                if(mutated) {
940                        auto n = __pass::mutate<core_t>(node);
941                        n->clauses = std::move( new_clauses );
942                        node = n;
943                }
944        })
945
946        #define maybe_accept(field) \
947                if(node->field) { \
948                        auto nval = call_accept( node->field ); \
949                        if(nval != node->field ) { \
950                                auto nparent = __pass::mutate<core_t>(node); \
951                                nparent->field = nval; \
952                                node = nparent; \
953                        } \
954                }
955
956        VISIT(
957                maybe_accept( timeout.time );
958                maybe_accept( timeout.stmt );
959                maybe_accept( timeout.cond );
960                maybe_accept( orElse.stmt  );
961                maybe_accept( orElse.cond  );
962        )
963
964        #undef maybe_accept
965
966        VISIT_END( Stmt, node );
967}
968
969//--------------------------------------------------------------------------
970// WithStmt
971template< typename core_t >
972const ast::Decl * ast::Pass< core_t >::visit( const ast::WithStmt * node ) {
973        VISIT_START( node );
974
975        VISIT(
976                maybe_accept( node, &WithStmt::exprs );
977                {
978                        // catch statements introduce a level of scope (for the caught exception)
979                        guard_symtab guard { *this };
980                        __pass::symtab::addWith( core, 0, node->exprs, node );
981                        maybe_accept( node, &WithStmt::stmt );
982                }
983        )
984        VISIT_END( Stmt, node );
985}
986
987//--------------------------------------------------------------------------
988// NullStmt
989template< typename core_t >
990const ast::NullStmt * ast::Pass< core_t >::visit( const ast::NullStmt * node ) {
991        VISIT_START( node );
992        VISIT_END( NullStmt, node );
993}
994
995//--------------------------------------------------------------------------
996// DeclStmt
997template< typename core_t >
998const ast::Stmt * ast::Pass< core_t >::visit( const ast::DeclStmt * node ) {
999        VISIT_START( node );
1000
1001        VISIT(
1002                maybe_accept( node, &DeclStmt::decl );
1003        )
1004
1005        VISIT_END( Stmt, node );
1006}
1007
1008//--------------------------------------------------------------------------
1009// ImplicitCtorDtorStmt
1010template< typename core_t >
1011const ast::Stmt * ast::Pass< core_t >::visit( const ast::ImplicitCtorDtorStmt * node ) {
1012        VISIT_START( node );
1013
1014        // For now this isn't visited, it is unclear if this causes problem
1015        // if all tests are known to pass, remove this code
1016        VISIT(
1017                maybe_accept( node, &ImplicitCtorDtorStmt::callStmt );
1018        )
1019
1020        VISIT_END( Stmt, node );
1021}
1022
1023//--------------------------------------------------------------------------
1024// ApplicationExpr
1025template< typename core_t >
1026const ast::Expr * ast::Pass< core_t >::visit( const ast::ApplicationExpr * node ) {
1027        VISIT_START( node );
1028
1029        VISIT(
1030                {
1031                        guard_symtab guard { *this };
1032                        maybe_accept( node, &ApplicationExpr::result );
1033                }
1034                maybe_accept( node, &ApplicationExpr::func );
1035                maybe_accept( node, &ApplicationExpr::args );
1036        )
1037
1038        VISIT_END( Expr, node );
1039}
1040
1041//--------------------------------------------------------------------------
1042// UntypedExpr
1043template< typename core_t >
1044const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedExpr * node ) {
1045        VISIT_START( node );
1046
1047        VISIT(
1048                {
1049                        guard_symtab guard { *this };
1050                        maybe_accept( node, &UntypedExpr::result );
1051                }
1052
1053                maybe_accept( node, &UntypedExpr::args );
1054        )
1055
1056        VISIT_END( Expr, node );
1057}
1058
1059//--------------------------------------------------------------------------
1060// NameExpr
1061template< typename core_t >
1062const ast::Expr * ast::Pass< core_t >::visit( const ast::NameExpr * node ) {
1063        VISIT_START( node );
1064
1065        VISIT({
1066                guard_symtab guard { *this };
1067                maybe_accept( node, &NameExpr::result );
1068        })
1069
1070        VISIT_END( Expr, node );
1071}
1072
1073//--------------------------------------------------------------------------
1074// CastExpr
1075template< typename core_t >
1076const ast::Expr * ast::Pass< core_t >::visit( const ast::CastExpr * node ) {
1077        VISIT_START( node );
1078
1079        VISIT({
1080                        guard_symtab guard { *this };
1081                        maybe_accept( node, &CastExpr::result );
1082                }
1083                maybe_accept( node, &CastExpr::arg );
1084        )
1085
1086        VISIT_END( Expr, node );
1087}
1088
1089//--------------------------------------------------------------------------
1090// KeywordCastExpr
1091template< typename core_t >
1092const ast::Expr * ast::Pass< core_t >::visit( const ast::KeywordCastExpr * node ) {
1093        VISIT_START( node );
1094
1095        VISIT({
1096                        guard_symtab guard { *this };
1097                        maybe_accept( node, &KeywordCastExpr::result );
1098                }
1099                maybe_accept( node, &KeywordCastExpr::arg );
1100        )
1101
1102        VISIT_END( Expr, node );
1103}
1104
1105//--------------------------------------------------------------------------
1106// VirtualCastExpr
1107template< typename core_t >
1108const ast::Expr * ast::Pass< core_t >::visit( const ast::VirtualCastExpr * node ) {
1109        VISIT_START( node );
1110
1111        VISIT({
1112                        guard_symtab guard { *this };
1113                        maybe_accept( node, &VirtualCastExpr::result );
1114                }
1115                maybe_accept( node, &VirtualCastExpr::arg );
1116        )
1117
1118        VISIT_END( Expr, node );
1119}
1120
1121//--------------------------------------------------------------------------
1122// AddressExpr
1123template< typename core_t >
1124const ast::Expr * ast::Pass< core_t >::visit( const ast::AddressExpr * node ) {
1125        VISIT_START( node );
1126
1127        VISIT({
1128                        guard_symtab guard { *this };
1129                        maybe_accept( node, &AddressExpr::result );
1130                }
1131                maybe_accept( node, &AddressExpr::arg );
1132        )
1133
1134        VISIT_END( Expr, node );
1135}
1136
1137//--------------------------------------------------------------------------
1138// LabelAddressExpr
1139template< typename core_t >
1140const ast::Expr * ast::Pass< core_t >::visit( const ast::LabelAddressExpr * node ) {
1141        VISIT_START( node );
1142
1143        VISIT({
1144                guard_symtab guard { *this };
1145                maybe_accept( node, &LabelAddressExpr::result );
1146        })
1147
1148        VISIT_END( Expr, node );
1149}
1150
1151//--------------------------------------------------------------------------
1152// UntypedMemberExpr
1153template< typename core_t >
1154const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedMemberExpr * node ) {
1155        VISIT_START( node );
1156
1157        VISIT({
1158                        guard_symtab guard { *this };
1159                        maybe_accept( node, &UntypedMemberExpr::result );
1160                }
1161                maybe_accept( node, &UntypedMemberExpr::aggregate );
1162                maybe_accept( node, &UntypedMemberExpr::member    );
1163        )
1164
1165        VISIT_END( Expr, node );
1166}
1167
1168//--------------------------------------------------------------------------
1169// MemberExpr
1170template< typename core_t >
1171const ast::Expr * ast::Pass< core_t >::visit( const ast::MemberExpr * node ) {
1172        VISIT_START( node );
1173
1174        VISIT({
1175                        guard_symtab guard { *this };
1176                        maybe_accept( node, &MemberExpr::result );
1177                }
1178                maybe_accept( node, &MemberExpr::aggregate );
1179        )
1180
1181        VISIT_END( Expr, node );
1182}
1183
1184//--------------------------------------------------------------------------
1185// VariableExpr
1186template< typename core_t >
1187const ast::Expr * ast::Pass< core_t >::visit( const ast::VariableExpr * node ) {
1188        VISIT_START( node );
1189
1190        VISIT({
1191                guard_symtab guard { *this };
1192                maybe_accept( node, &VariableExpr::result );
1193        })
1194
1195        VISIT_END( Expr, node );
1196}
1197
1198//--------------------------------------------------------------------------
1199// ConstantExpr
1200template< typename core_t >
1201const ast::Expr * ast::Pass< core_t >::visit( const ast::ConstantExpr * node ) {
1202        VISIT_START( node );
1203
1204        VISIT({
1205                guard_symtab guard { *this };
1206                maybe_accept( node, &ConstantExpr::result );
1207        })
1208
1209        VISIT_END( Expr, node );
1210}
1211
1212//--------------------------------------------------------------------------
1213// SizeofExpr
1214template< typename core_t >
1215const ast::Expr * ast::Pass< core_t >::visit( const ast::SizeofExpr * node ) {
1216        VISIT_START( node );
1217
1218        VISIT({
1219                        guard_symtab guard { *this };
1220                        maybe_accept( node, &SizeofExpr::result );
1221                }
1222                if ( node->type ) {
1223                        maybe_accept( node, &SizeofExpr::type );
1224                } else {
1225                        maybe_accept( node, &SizeofExpr::expr );
1226                }
1227        )
1228
1229        VISIT_END( Expr, node );
1230}
1231
1232//--------------------------------------------------------------------------
1233// AlignofExpr
1234template< typename core_t >
1235const ast::Expr * ast::Pass< core_t >::visit( const ast::AlignofExpr * node ) {
1236        VISIT_START( node );
1237
1238        VISIT({
1239                        guard_symtab guard { *this };
1240                        maybe_accept( node, &AlignofExpr::result );
1241                }
1242                if ( node->type ) {
1243                        maybe_accept( node, &AlignofExpr::type );
1244                } else {
1245                        maybe_accept( node, &AlignofExpr::expr );
1246                }
1247        )
1248
1249        VISIT_END( Expr, node );
1250}
1251
1252//--------------------------------------------------------------------------
1253// UntypedOffsetofExpr
1254template< typename core_t >
1255const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedOffsetofExpr * node ) {
1256        VISIT_START( node );
1257
1258        VISIT({
1259                        guard_symtab guard { *this };
1260                        maybe_accept( node, &UntypedOffsetofExpr::result );
1261                }
1262                maybe_accept( node, &UntypedOffsetofExpr::type   );
1263        )
1264
1265        VISIT_END( Expr, node );
1266}
1267
1268//--------------------------------------------------------------------------
1269// OffsetofExpr
1270template< typename core_t >
1271const ast::Expr * ast::Pass< core_t >::visit( const ast::OffsetofExpr * node ) {
1272        VISIT_START( node );
1273
1274        VISIT({
1275                        guard_symtab guard { *this };
1276                        maybe_accept( node, &OffsetofExpr::result );
1277                }
1278                maybe_accept( node, &OffsetofExpr::type   );
1279        )
1280
1281        VISIT_END( Expr, node );
1282}
1283
1284//--------------------------------------------------------------------------
1285// OffsetPackExpr
1286template< typename core_t >
1287const ast::Expr * ast::Pass< core_t >::visit( const ast::OffsetPackExpr * node ) {
1288        VISIT_START( node );
1289
1290        VISIT({
1291                        guard_symtab guard { *this };
1292                        maybe_accept( node, &OffsetPackExpr::result );
1293                }
1294                maybe_accept( node, &OffsetPackExpr::type   );
1295        )
1296
1297        VISIT_END( Expr, node );
1298}
1299
1300//--------------------------------------------------------------------------
1301// LogicalExpr
1302template< typename core_t >
1303const ast::Expr * ast::Pass< core_t >::visit( const ast::LogicalExpr * node ) {
1304        VISIT_START( node );
1305
1306        VISIT({
1307                        guard_symtab guard { *this };
1308                        maybe_accept( node, &LogicalExpr::result );
1309                }
1310                maybe_accept( node, &LogicalExpr::arg1 );
1311                maybe_accept( node, &LogicalExpr::arg2 );
1312        )
1313
1314        VISIT_END( Expr, node );
1315}
1316
1317//--------------------------------------------------------------------------
1318// ConditionalExpr
1319template< typename core_t >
1320const ast::Expr * ast::Pass< core_t >::visit( const ast::ConditionalExpr * node ) {
1321        VISIT_START( node );
1322
1323        VISIT({
1324                        guard_symtab guard { *this };
1325                        maybe_accept( node, &ConditionalExpr::result );
1326                }
1327                maybe_accept( node, &ConditionalExpr::arg1 );
1328                maybe_accept( node, &ConditionalExpr::arg2 );
1329                maybe_accept( node, &ConditionalExpr::arg3 );
1330        )
1331
1332        VISIT_END( Expr, node );
1333}
1334
1335//--------------------------------------------------------------------------
1336// CommaExpr
1337template< typename core_t >
1338const ast::Expr * ast::Pass< core_t >::visit( const ast::CommaExpr * node ) {
1339        VISIT_START( node );
1340
1341        VISIT({
1342                        guard_symtab guard { *this };
1343                        maybe_accept( node, &CommaExpr::result );
1344                }
1345                maybe_accept( node, &CommaExpr::arg1 );
1346                maybe_accept( node, &CommaExpr::arg2 );
1347        )
1348
1349        VISIT_END( Expr, node );
1350}
1351
1352//--------------------------------------------------------------------------
1353// TypeExpr
1354template< typename core_t >
1355const ast::Expr * ast::Pass< core_t >::visit( const ast::TypeExpr * node ) {
1356        VISIT_START( node );
1357
1358        VISIT({
1359                        guard_symtab guard { *this };
1360                        maybe_accept( node, &TypeExpr::result );
1361                }
1362                maybe_accept( node, &TypeExpr::type );
1363        )
1364
1365        VISIT_END( Expr, node );
1366}
1367
1368//--------------------------------------------------------------------------
1369// AsmExpr
1370template< typename core_t >
1371const ast::Expr * ast::Pass< core_t >::visit( const ast::AsmExpr * node ) {
1372        VISIT_START( node );
1373
1374        VISIT({
1375                        guard_symtab guard { *this };
1376                        maybe_accept( node, &AsmExpr::result );
1377                }
1378                maybe_accept( node, &AsmExpr::constraint );
1379                maybe_accept( node, &AsmExpr::operand    );
1380        )
1381
1382        VISIT_END( Expr, node );
1383}
1384
1385//--------------------------------------------------------------------------
1386// ImplicitCopyCtorExpr
1387template< typename core_t >
1388const ast::Expr * ast::Pass< core_t >::visit( const ast::ImplicitCopyCtorExpr * node ) {
1389        VISIT_START( node );
1390
1391        VISIT({
1392                        guard_symtab guard { *this };
1393                        maybe_accept( node, &ImplicitCopyCtorExpr::result );
1394                }
1395                maybe_accept( node, &ImplicitCopyCtorExpr::callExpr    );
1396        )
1397
1398        VISIT_END( Expr, node );
1399}
1400
1401//--------------------------------------------------------------------------
1402// ConstructorExpr
1403template< typename core_t >
1404const ast::Expr * ast::Pass< core_t >::visit( const ast::ConstructorExpr * node ) {
1405        VISIT_START( node );
1406
1407        VISIT({
1408                        guard_symtab guard { *this };
1409                        maybe_accept( node, &ConstructorExpr::result );
1410                }
1411                maybe_accept( node, &ConstructorExpr::callExpr );
1412        )
1413
1414        VISIT_END( Expr, node );
1415}
1416
1417//--------------------------------------------------------------------------
1418// CompoundLiteralExpr
1419template< typename core_t >
1420const ast::Expr * ast::Pass< core_t >::visit( const ast::CompoundLiteralExpr * node ) {
1421        VISIT_START( node );
1422
1423        VISIT({
1424                        guard_symtab guard { *this };
1425                        maybe_accept( node, &CompoundLiteralExpr::result );
1426                }
1427                maybe_accept( node, &CompoundLiteralExpr::init );
1428        )
1429
1430        VISIT_END( Expr, node );
1431}
1432
1433//--------------------------------------------------------------------------
1434// RangeExpr
1435template< typename core_t >
1436const ast::Expr * ast::Pass< core_t >::visit( const ast::RangeExpr * node ) {
1437        VISIT_START( node );
1438
1439        VISIT({
1440                        guard_symtab guard { *this };
1441                        maybe_accept( node, &RangeExpr::result );
1442                }
1443                maybe_accept( node, &RangeExpr::low    );
1444                maybe_accept( node, &RangeExpr::high   );
1445        )
1446
1447        VISIT_END( Expr, node );
1448}
1449
1450//--------------------------------------------------------------------------
1451// UntypedTupleExpr
1452template< typename core_t >
1453const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedTupleExpr * node ) {
1454        VISIT_START( node );
1455
1456        VISIT({
1457                        guard_symtab guard { *this };
1458                        maybe_accept( node, &UntypedTupleExpr::result );
1459                }
1460                maybe_accept( node, &UntypedTupleExpr::exprs  );
1461        )
1462
1463        VISIT_END( Expr, node );
1464}
1465
1466//--------------------------------------------------------------------------
1467// TupleExpr
1468template< typename core_t >
1469const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleExpr * node ) {
1470        VISIT_START( node );
1471
1472        VISIT({
1473                        guard_symtab guard { *this };
1474                        maybe_accept( node, &TupleExpr::result );
1475                }
1476                maybe_accept( node, &TupleExpr::exprs  );
1477        )
1478
1479        VISIT_END( Expr, node );
1480}
1481
1482//--------------------------------------------------------------------------
1483// TupleIndexExpr
1484template< typename core_t >
1485const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleIndexExpr * node ) {
1486        VISIT_START( node );
1487
1488        VISIT({
1489                        guard_symtab guard { *this };
1490                        maybe_accept( node, &TupleIndexExpr::result );
1491                }
1492                maybe_accept( node, &TupleIndexExpr::tuple  );
1493        )
1494
1495        VISIT_END( Expr, node );
1496}
1497
1498//--------------------------------------------------------------------------
1499// TupleAssignExpr
1500template< typename core_t >
1501const ast::Expr * ast::Pass< core_t >::visit( const ast::TupleAssignExpr * node ) {
1502        VISIT_START( node );
1503
1504        VISIT({
1505                        guard_symtab guard { *this };
1506                        maybe_accept( node, &TupleAssignExpr::result );
1507                }
1508                maybe_accept( node, &TupleAssignExpr::stmtExpr );
1509        )
1510
1511        VISIT_END( Expr, node );
1512}
1513
1514//--------------------------------------------------------------------------
1515// StmtExpr
1516template< typename core_t >
1517const ast::Expr * ast::Pass< core_t >::visit( const ast::StmtExpr * node ) {
1518        VISIT_START( node );
1519
1520        VISIT(// don't want statements from outer CompoundStmts to be added to this StmtExpr
1521                // get the stmts that will need to be spliced in
1522                auto stmts_before = __pass::stmtsToAddBefore( core, 0);
1523                auto stmts_after  = __pass::stmtsToAddAfter ( core, 0);
1524
1525                // These may be modified by subnode but most be restored once we exit this statemnet.
1526                ValueGuardPtr< const ast::TypeSubstitution * > __old_env( __pass::typeSubs( core, 0 ) );
1527                ValueGuardPtr< typename std::remove_pointer< decltype(stmts_before) >::type > __old_decls_before( stmts_before );
1528                ValueGuardPtr< typename std::remove_pointer< decltype(stmts_after ) >::type > __old_decls_after ( stmts_after  );
1529
1530                {
1531                        guard_symtab guard { *this };
1532                        maybe_accept( node, &StmtExpr::result );
1533                }
1534                maybe_accept( node, &StmtExpr::stmts       );
1535                maybe_accept( node, &StmtExpr::returnDecls );
1536                maybe_accept( node, &StmtExpr::dtors       );
1537        )
1538
1539        VISIT_END( Expr, node );
1540}
1541
1542//--------------------------------------------------------------------------
1543// UniqueExpr
1544template< typename core_t >
1545const ast::Expr * ast::Pass< core_t >::visit( const ast::UniqueExpr * node ) {
1546        VISIT_START( node );
1547
1548        VISIT({
1549                        guard_symtab guard { *this };
1550                        maybe_accept( node, &UniqueExpr::result );
1551                }
1552                maybe_accept( node, &UniqueExpr::expr   );
1553        )
1554
1555        VISIT_END( Expr, node );
1556}
1557
1558//--------------------------------------------------------------------------
1559// UntypedInitExpr
1560template< typename core_t >
1561const ast::Expr * ast::Pass< core_t >::visit( const ast::UntypedInitExpr * node ) {
1562        VISIT_START( node );
1563
1564        VISIT({
1565                        guard_symtab guard { *this };
1566                        maybe_accept( node, &UntypedInitExpr::result );
1567                }
1568                maybe_accept( node, &UntypedInitExpr::expr   );
1569                // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver.
1570        )
1571
1572        VISIT_END( Expr, node );
1573}
1574
1575//--------------------------------------------------------------------------
1576// InitExpr
1577template< typename core_t >
1578const ast::Expr * ast::Pass< core_t >::visit( const ast::InitExpr * node ) {
1579        VISIT_START( node );
1580
1581        VISIT({
1582                        guard_symtab guard { *this };
1583                        maybe_accept( node, &InitExpr::result );
1584                }
1585                maybe_accept( node, &InitExpr::expr   );
1586                maybe_accept( node, &InitExpr::designation );
1587        )
1588
1589        VISIT_END( Expr, node );
1590}
1591
1592//--------------------------------------------------------------------------
1593// DeletedExpr
1594template< typename core_t >
1595const ast::Expr * ast::Pass< core_t >::visit( const ast::DeletedExpr * node ) {
1596        VISIT_START( node );
1597
1598        VISIT({
1599                        guard_symtab guard { *this };
1600                        maybe_accept( node, &DeletedExpr::result );
1601                }
1602                maybe_accept( node, &DeletedExpr::expr );
1603                // don't visit deleteStmt, because it is a pointer to somewhere else in the tree.
1604        )
1605
1606        VISIT_END( Expr, node );
1607}
1608
1609//--------------------------------------------------------------------------
1610// DefaultArgExpr
1611template< typename core_t >
1612const ast::Expr * ast::Pass< core_t >::visit( const ast::DefaultArgExpr * node ) {
1613        VISIT_START( node );
1614
1615        VISIT({
1616                        guard_symtab guard { *this };
1617                        maybe_accept( node, &DefaultArgExpr::result );
1618                }
1619                maybe_accept( node, &DefaultArgExpr::expr );
1620        )
1621
1622        VISIT_END( Expr, node );
1623}
1624
1625//--------------------------------------------------------------------------
1626// GenericExpr
1627template< typename core_t >
1628const ast::Expr * ast::Pass< core_t >::visit( const ast::GenericExpr * node ) {
1629        VISIT_START( node );
1630
1631        VISIT({
1632                        guard_symtab guard { *this };
1633                        maybe_accept( node, &GenericExpr::result );
1634                }
1635                maybe_accept( node, &GenericExpr::control );
1636
1637                std::vector<GenericExpr::Association> new_kids;
1638                new_kids.reserve(node->associations.size());
1639                bool mutated = false;
1640                for( const auto & assoc : node->associations ) {
1641                        const Type * type = nullptr;
1642                        if( assoc.type ) {
1643                                guard_symtab guard { *this };
1644                                type = assoc.type->accept( *this );
1645                                if( type != assoc.type ) mutated = true;
1646                        }
1647                        const Expr * expr = nullptr;
1648                        if( assoc.expr ) {
1649                                expr = assoc.expr->accept( *this );
1650                                if( expr != assoc.expr ) mutated = true;
1651                        }
1652                        new_kids.emplace_back( type, expr );
1653                }
1654
1655                if(mutated) {
1656                        auto n = __pass::mutate<core_t>(node);
1657                        n->associations = std::move( new_kids );
1658                        node = n;
1659                }
1660        )
1661
1662        VISIT_END( Expr, node );
1663}
1664
1665//--------------------------------------------------------------------------
1666// VoidType
1667template< typename core_t >
1668const ast::Type * ast::Pass< core_t >::visit( const ast::VoidType * node ) {
1669        VISIT_START( node );
1670
1671        VISIT_END( Type, node );
1672}
1673
1674//--------------------------------------------------------------------------
1675// BasicType
1676template< typename core_t >
1677const ast::Type * ast::Pass< core_t >::visit( const ast::BasicType * node ) {
1678        VISIT_START( node );
1679
1680        VISIT_END( Type, node );
1681}
1682
1683//--------------------------------------------------------------------------
1684// PointerType
1685template< typename core_t >
1686const ast::Type * ast::Pass< core_t >::visit( const ast::PointerType * node ) {
1687        VISIT_START( node );
1688
1689        VISIT(
1690                // xxx - should PointerType visit/mutate dimension?
1691                maybe_accept( node, &PointerType::base );
1692        )
1693
1694        VISIT_END( Type, node );
1695}
1696
1697//--------------------------------------------------------------------------
1698// ArrayType
1699template< typename core_t >
1700const ast::Type * ast::Pass< core_t >::visit( const ast::ArrayType * node ) {
1701        VISIT_START( node );
1702
1703        VISIT(
1704                maybe_accept( node, &ArrayType::dimension );
1705                maybe_accept( node, &ArrayType::base );
1706        )
1707
1708        VISIT_END( Type, node );
1709}
1710
1711//--------------------------------------------------------------------------
1712// ReferenceType
1713template< typename core_t >
1714const ast::Type * ast::Pass< core_t >::visit( const ast::ReferenceType * node ) {
1715        VISIT_START( node );
1716
1717        VISIT(
1718                maybe_accept( node, &ReferenceType::base );
1719        )
1720
1721        VISIT_END( Type, node );
1722}
1723
1724//--------------------------------------------------------------------------
1725// QualifiedType
1726template< typename core_t >
1727const ast::Type * ast::Pass< core_t >::visit( const ast::QualifiedType * node ) {
1728        VISIT_START( node );
1729
1730        VISIT(
1731                maybe_accept( node, &QualifiedType::parent );
1732                maybe_accept( node, &QualifiedType::child );
1733        )
1734
1735        VISIT_END( Type, node );
1736}
1737
1738//--------------------------------------------------------------------------
1739// FunctionType
1740template< typename core_t >
1741const ast::Type * ast::Pass< core_t >::visit( const ast::FunctionType * node ) {
1742        VISIT_START( node );
1743
1744        VISIT({
1745                // guard_forall_subs forall_guard { *this, node };
1746                // mutate_forall( node );
1747                maybe_accept( node, &FunctionType::assertions );
1748                maybe_accept( node, &FunctionType::returns );
1749                maybe_accept( node, &FunctionType::params  );
1750        })
1751
1752        VISIT_END( Type, node );
1753}
1754
1755//--------------------------------------------------------------------------
1756// StructInstType
1757template< typename core_t >
1758const ast::Type * ast::Pass< core_t >::visit( const ast::StructInstType * node ) {
1759        VISIT_START( node );
1760
1761        __pass::symtab::addStruct( core, 0, node->name );
1762
1763        VISIT({
1764                guard_symtab guard { *this };
1765                maybe_accept( node, &StructInstType::params );
1766        })
1767
1768        VISIT_END( Type, node );
1769}
1770
1771//--------------------------------------------------------------------------
1772// UnionInstType
1773template< typename core_t >
1774const ast::Type * ast::Pass< core_t >::visit( const ast::UnionInstType * node ) {
1775        VISIT_START( node );
1776
1777        __pass::symtab::addUnion( core, 0, node->name );
1778
1779        VISIT({
1780                guard_symtab guard { *this };
1781                maybe_accept( node, &UnionInstType::params );
1782        })
1783
1784        VISIT_END( Type, node );
1785}
1786
1787//--------------------------------------------------------------------------
1788// EnumInstType
1789template< typename core_t >
1790const ast::Type * ast::Pass< core_t >::visit( const ast::EnumInstType * node ) {
1791        VISIT_START( node );
1792
1793        VISIT({
1794                maybe_accept( node, &EnumInstType::params );
1795        })
1796
1797        VISIT_END( Type, node );
1798}
1799
1800//--------------------------------------------------------------------------
1801// TraitInstType
1802template< typename core_t >
1803const ast::Type * ast::Pass< core_t >::visit( const ast::TraitInstType * node ) {
1804        VISIT_START( node );
1805
1806        VISIT({
1807                maybe_accept( node, &TraitInstType::params );
1808        })
1809
1810        VISIT_END( Type, node );
1811}
1812
1813//--------------------------------------------------------------------------
1814// TypeInstType
1815template< typename core_t >
1816const ast::Type * ast::Pass< core_t >::visit( const ast::TypeInstType * node ) {
1817        VISIT_START( node );
1818
1819        VISIT(
1820                {
1821                        maybe_accept( node, &TypeInstType::params );
1822                }
1823                // ensure that base re-bound if doing substitution
1824                __pass::forall::replace( core, 0, node );
1825        )
1826
1827        VISIT_END( Type, node );
1828}
1829
1830//--------------------------------------------------------------------------
1831// TupleType
1832template< typename core_t >
1833const ast::Type * ast::Pass< core_t >::visit( const ast::TupleType * node ) {
1834        VISIT_START( node );
1835
1836        VISIT(
1837                maybe_accept( node, &TupleType::types );
1838                maybe_accept( node, &TupleType::members );
1839        )
1840
1841        VISIT_END( Type, node );
1842}
1843
1844//--------------------------------------------------------------------------
1845// TypeofType
1846template< typename core_t >
1847const ast::Type * ast::Pass< core_t >::visit( const ast::TypeofType * node ) {
1848        VISIT_START( node );
1849
1850        VISIT(
1851                maybe_accept( node, &TypeofType::expr );
1852        )
1853
1854        VISIT_END( Type, node );
1855}
1856
1857//--------------------------------------------------------------------------
1858// VarArgsType
1859template< typename core_t >
1860const ast::Type * ast::Pass< core_t >::visit( const ast::VarArgsType * node ) {
1861        VISIT_START( node );
1862
1863        VISIT_END( Type, node );
1864}
1865
1866//--------------------------------------------------------------------------
1867// ZeroType
1868template< typename core_t >
1869const ast::Type * ast::Pass< core_t >::visit( const ast::ZeroType * node ) {
1870        VISIT_START( node );
1871
1872        VISIT_END( Type, node );
1873}
1874
1875//--------------------------------------------------------------------------
1876// OneType
1877template< typename core_t >
1878const ast::Type * ast::Pass< core_t >::visit( const ast::OneType * node ) {
1879        VISIT_START( node );
1880
1881        VISIT_END( Type, node );
1882}
1883
1884//--------------------------------------------------------------------------
1885// GlobalScopeType
1886template< typename core_t >
1887const ast::Type * ast::Pass< core_t >::visit( const ast::GlobalScopeType * node ) {
1888        VISIT_START( node );
1889
1890        VISIT_END( Type, node );
1891}
1892
1893
1894//--------------------------------------------------------------------------
1895// Designation
1896template< typename core_t >
1897const ast::Designation * ast::Pass< core_t >::visit( const ast::Designation * node ) {
1898        VISIT_START( node );
1899
1900        VISIT( maybe_accept( node, &Designation::designators ); )
1901
1902        VISIT_END( Designation, node );
1903}
1904
1905//--------------------------------------------------------------------------
1906// SingleInit
1907template< typename core_t >
1908const ast::Init * ast::Pass< core_t >::visit( const ast::SingleInit * node ) {
1909        VISIT_START( node );
1910
1911        VISIT(
1912                maybe_accept( node, &SingleInit::value );
1913        )
1914
1915        VISIT_END( Init, node );
1916}
1917
1918//--------------------------------------------------------------------------
1919// ListInit
1920template< typename core_t >
1921const ast::Init * ast::Pass< core_t >::visit( const ast::ListInit * node ) {
1922        VISIT_START( node );
1923
1924        VISIT(
1925                maybe_accept( node, &ListInit::designations );
1926                maybe_accept( node, &ListInit::initializers );
1927        )
1928
1929        VISIT_END( Init, node );
1930}
1931
1932//--------------------------------------------------------------------------
1933// ConstructorInit
1934template< typename core_t >
1935const ast::Init * ast::Pass< core_t >::visit( const ast::ConstructorInit * node ) {
1936        VISIT_START( node );
1937
1938        VISIT(
1939                maybe_accept( node, &ConstructorInit::ctor );
1940                maybe_accept( node, &ConstructorInit::dtor );
1941                maybe_accept( node, &ConstructorInit::init );
1942        )
1943
1944        VISIT_END( Init, node );
1945}
1946
1947//--------------------------------------------------------------------------
1948// Attribute
1949template< typename core_t >
1950const ast::Attribute * ast::Pass< core_t >::visit( const ast::Attribute * node  )  {
1951        VISIT_START( node );
1952
1953        VISIT(
1954                maybe_accept( node, &Attribute::params );
1955        )
1956
1957        VISIT_END( Attribute, node );
1958}
1959
1960//--------------------------------------------------------------------------
1961// TypeSubstitution
1962template< typename core_t >
1963const ast::TypeSubstitution * ast::Pass< core_t >::visit( const ast::TypeSubstitution * node ) {
1964        VISIT_START( node );
1965
1966        VISIT(
1967                {
1968                        bool mutated = false;
1969                        std::unordered_map< ast::TypeInstType::TypeEnvKey, ast::ptr< ast::Type > > new_map;
1970                        for ( const auto & p : node->typeEnv ) {
1971                                guard_symtab guard { *this };
1972                                auto new_node = p.second->accept( *this );
1973                                if (new_node != p.second) mutated = true;
1974                                new_map.insert({ p.first, new_node });
1975                        }
1976                        if (mutated) {
1977                                auto new_node = __pass::mutate<core_t>( node );
1978                                new_node->typeEnv.swap( new_map );
1979                                node = new_node;
1980                        }
1981                }
1982        )
1983
1984        VISIT_END( TypeSubstitution, node );
1985}
1986
1987#undef VISIT_START
1988#undef VISIT
1989#undef VISIT_END
Note: See TracBrowser for help on using the repository browser.