Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Pass.impl.hpp

    r1df492a r9e23b446  
    155155                __pedantic_pass_assert( expr );
    156156
    157                 const ast::TypeSubstitution ** typeSubs_ptr = __pass::typeSubs( core, 0 );
    158                 if ( typeSubs_ptr && expr->env ) {
    159                         *typeSubs_ptr = expr->env;
    160                 }
    161 
    162157                auto nval = expr->accept( *this );
    163158                return { nval != expr, nval };
     
    174169
    175170        template< typename core_t >
     171        __pass::template result1<ast::Expr> ast::Pass< core_t >::call_accept_top( const ast::Expr * expr ) {
     172                __pedantic_pass_assert( __visit_children() );
     173                __pedantic_pass_assert( expr );
     174
     175                const ast::TypeSubstitution ** typeSubs_ptr = __pass::typeSubs( core, 0 );
     176                if ( typeSubs_ptr && expr->env ) {
     177                        *typeSubs_ptr = expr->env;
     178                }
     179
     180                auto nval = expr->accept( *this );
     181                return { nval != expr, nval };
     182        }
     183
     184        template< typename core_t >
    176185        __pass::template result1<ast::Stmt> ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) {
    177186                __pedantic_pass_assert( __visit_children() );
     
    182191
    183192                // get the stmts/decls that will need to be spliced in
    184                 auto stmts_before = __pass::stmtsToAddBefore( core, 0);
    185                 auto stmts_after  = __pass::stmtsToAddAfter ( core, 0);
    186                 auto decls_before = __pass::declsToAddBefore( core, 0);
    187                 auto decls_after  = __pass::declsToAddAfter ( core, 0);
     193                auto stmts_before = __pass::stmtsToAddBefore( core, 0 );
     194                auto stmts_after  = __pass::stmtsToAddAfter ( core, 0 );
     195                auto decls_before = __pass::declsToAddBefore( core, 0 );
     196                auto decls_after  = __pass::declsToAddAfter ( core, 0 );
    188197
    189198                // These may be modified by subnode but most be restored once we exit this statemnet.
     
    287296
    288297                // get the stmts/decls that will need to be spliced in
    289                 auto stmts_before = __pass::stmtsToAddBefore( core, 0);
    290                 auto stmts_after  = __pass::stmtsToAddAfter ( core, 0);
    291                 auto decls_before = __pass::declsToAddBefore( core, 0);
    292                 auto decls_after  = __pass::declsToAddAfter ( core, 0);
     298                auto stmts_before = __pass::stmtsToAddBefore( core, 0 );
     299                auto stmts_after  = __pass::stmtsToAddAfter ( core, 0 );
     300                auto decls_before = __pass::declsToAddBefore( core, 0 );
     301                auto decls_after  = __pass::declsToAddAfter ( core, 0 );
    293302
    294303                // These may be modified by subnode but most be restored once we exit this statemnet.
     
    317326                                assert(( empty( stmts_before ) && empty( stmts_after ))
    318327                                    || ( empty( decls_before ) && empty( decls_after )) );
    319 
    320 
    321328
    322329                                // Take all the statements which should have gone after, N/A for first iteration
     
    412419
    413420                auto new_val = call_accept( old_val );
     421
     422                static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value /* || std::is_same<int, decltype(old_val)>::value */, "ERROR");
     423
     424                if( new_val.differs ) {
     425                        auto new_parent = __pass::mutate<core_t>(parent);
     426                        new_val.apply(new_parent, field);
     427                        parent = new_parent;
     428                }
     429        }
     430
     431        template< typename core_t >
     432        template<typename node_t, typename super_t, typename field_t>
     433        void ast::Pass< core_t >::maybe_accept_top(
     434                const node_t * & parent,
     435                field_t super_t::*field
     436        ) {
     437                static_assert( std::is_base_of<super_t, node_t>::value, "Error deducing member object" );
     438
     439                if(__pass::skip(parent->*field)) return;
     440                const auto & old_val = __pass::get(parent->*field, 0);
     441
     442                static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR");
     443
     444                auto new_val = call_accept_top( old_val );
    414445
    415446                static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value /* || std::is_same<int, decltype(old_val)>::value */, "ERROR");
     
    650681        if ( __visit_children() ) {
    651682                // unlike structs, traits, and unions, enums inject their members into the global scope
    652                 maybe_accept( node, &EnumDecl::base );
    653683                maybe_accept( node, &EnumDecl::params     );
    654684                maybe_accept( node, &EnumDecl::members    );
     
    758788
    759789        if ( __visit_children() ) {
    760                 maybe_accept( node, &StaticAssertDecl::cond );
     790                maybe_accept_top( node, &StaticAssertDecl::cond );
    761791                maybe_accept( node, &StaticAssertDecl::msg  );
    762792        }
     
    800830
    801831        if ( __visit_children() ) {
    802                 maybe_accept( node, &ExprStmt::expr );
     832                maybe_accept_top( node, &ExprStmt::expr );
    803833        }
    804834
     
    841871                guard_symtab guard { *this };
    842872                maybe_accept( node, &IfStmt::inits    );
    843                 maybe_accept( node, &IfStmt::cond     );
     873                maybe_accept_top( node, &IfStmt::cond     );
    844874                maybe_accept_as_compound( node, &IfStmt::then );
    845875                maybe_accept_as_compound( node, &IfStmt::else_ );
     
    859889                guard_symtab guard { *this };
    860890                maybe_accept( node, &WhileDoStmt::inits );
    861                 maybe_accept( node, &WhileDoStmt::cond  );
     891                maybe_accept_top( node, &WhileDoStmt::cond  );
    862892                maybe_accept_as_compound( node, &WhileDoStmt::body  );
    863893        }
     
    877907                // xxx - old ast does not create WithStmtsToAdd scope for loop inits. should revisit this later.
    878908                maybe_accept( node, &ForStmt::inits );
    879                 maybe_accept( node, &ForStmt::cond  );
    880                 maybe_accept( node, &ForStmt::inc   );
     909                maybe_accept_top( node, &ForStmt::cond  );
     910                maybe_accept_top( node, &ForStmt::inc   );
    881911                maybe_accept_as_compound( node, &ForStmt::body  );
    882912        }
     
    892922
    893923        if ( __visit_children() ) {
    894                 maybe_accept( node, &SwitchStmt::cond  );
     924                maybe_accept_top( node, &SwitchStmt::cond  );
    895925                maybe_accept( node, &SwitchStmt::cases );
    896926        }
     
    906936
    907937        if ( __visit_children() ) {
    908                 maybe_accept( node, &CaseClause::cond  );
     938                maybe_accept_top( node, &CaseClause::cond  );
    909939                maybe_accept( node, &CaseClause::stmts );
    910940        }
     
    928958
    929959        if ( __visit_children() ) {
    930                 maybe_accept( node, &ReturnStmt::expr );
     960                maybe_accept_top( node, &ReturnStmt::expr );
    931961        }
    932962
     
    9731003                guard_symtab guard { *this };
    9741004                maybe_accept( node, &CatchClause::decl );
    975                 maybe_accept( node, &CatchClause::cond );
     1005                maybe_accept_top( node, &CatchClause::cond );
    9761006                maybe_accept_as_compound( node, &CatchClause::body );
    9771007        }
     
    20602090
    20612091        if ( __visit_children() ) {
    2062                 maybe_accept( node, &SingleInit::value );
     2092                maybe_accept_top( node, &SingleInit::value );
    20632093        }
    20642094
     
    21162146        if ( __visit_children() ) {
    21172147                bool mutated = false;
    2118                 std::unordered_map< ast::TypeInstType::TypeEnvKey, ast::ptr< ast::Type > > new_map;
    2119                 for ( const auto & p : node->typeEnv ) {
     2148                ast::TypeSubstitution::TypeMap new_map;
     2149                for ( const auto & p : node->typeMap ) {
    21202150                        guard_symtab guard { *this };
    21212151                        auto new_node = p.second->accept( *this );
     
    21252155                if (mutated) {
    21262156                        auto new_node = __pass::mutate<core_t>( node );
    2127                         new_node->typeEnv.swap( new_map );
     2157                        new_node->typeMap.swap( new_map );
    21282158                        node = new_node;
    21292159                }
Note: See TracChangeset for help on using the changeset viewer.