Changeset 90320ac for src/AST


Ignore:
Timestamp:
Apr 12, 2024, 3:10:04 PM (8 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
7a36848
Parents:
b78c54f
Message:

Clean-up in the Pass template around call_accept. Removed overloads that no longer contain unique behaviour. Redundent early exits have been removed. Updated the names and removed redundent checks following up the result* type update. New constructors for the delta helper type show how the data is actually used. The differs functions are unused (that value is stored in the result* types).

Location:
src/AST
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Pass.hpp

    rb78c54f r90320ac  
    252252private:
    253253
    254         __pass::result1<ast::Stmt> call_accept( const ast::Stmt * );
    255         __pass::result1<ast::Expr> call_accept( const ast::Expr * );
    256 
    257         /// This has a `type` member that is the return type for the
    258         /// generic call_accept if the generic call_accept is defined.
     254        /// The return type of the general call_accept function.
    259255        template< typename node_t >
    260         using generic_call_accept_result =
    261                 std::enable_if<
    262                                 !std::is_base_of<ast::Expr, node_t>::value &&
    263                                 !std::is_base_of<ast::Stmt, node_t>::value
    264                         , __pass::result1<
    265                                 typename std::remove_pointer< typename std::result_of<
    266                                         decltype(&node_t::accept)(node_t*, type&) >::type >::type
    267                         >
     256        using call_accept_result_t = __pass::result1<
     257                typename std::remove_pointer< typename std::result_of<
     258                        decltype(&node_t::accept)(node_t*, type&) >::type >::type
    268259                >;
    269260
    270261        template< typename node_t >
    271         auto call_accept( const node_t * node )
    272                 -> typename generic_call_accept_result<node_t>::type;
     262        auto call_accept( const node_t * node ) -> call_accept_result_t<node_t>;
    273263
    274264        // requests WithStmtsToAdd directly add to this statement, as if it is a compound.
  • src/AST/Pass.impl.hpp

    rb78c54f r90320ac  
    109109                return val;
    110110        }
    111 
    112         //------------------------------
    113         /// Check if value was mutated, different for pointers and containers
    114         template<typename lhs_t, typename rhs_t>
    115         bool differs( const lhs_t * old_val, const rhs_t * new_val ) {
    116                 return old_val != new_val;
    117         }
    118 
    119         template< template <class...> class container_t, typename node_t >
    120         bool differs( const container_t<ast::ptr< node_t >> &, const container_t<ast::ptr< node_t >> & new_val ) {
    121                 return !new_val.empty();
    122         }
    123111}
    124112
     
    126114template< typename node_t >
    127115auto ast::Pass< core_t >::call_accept( const node_t * node ) ->
    128         typename ast::Pass< core_t >::template generic_call_accept_result<node_t>::type
    129 {
     116                ast::Pass< core_t >::call_accept_result_t<node_t> {
    130117        __pedantic_pass_assert( __visit_children() );
    131118        __pedantic_pass_assert( node );
    132119
    133         static_assert( !std::is_base_of<ast::Expr, node_t>::value, "ERROR" );
    134         static_assert( !std::is_base_of<ast::Stmt, node_t>::value, "ERROR" );
    135 
    136120        auto nval = node->accept( *this );
    137         __pass::result1<
    138                 typename std::remove_pointer< decltype( node->accept(*this) ) >::type
    139         > res;
    140         res.differs = nval != node;
    141         res.value = nval;
    142         return res;
    143 }
    144 
    145 template< typename core_t >
    146 ast::__pass::template result1<ast::Expr> ast::Pass< core_t >::call_accept( const ast::Expr * expr ) {
    147         __pedantic_pass_assert( __visit_children() );
    148         __pedantic_pass_assert( expr );
    149 
    150         auto nval = expr->accept( *this );
    151         return { nval != expr, nval };
    152 }
    153 
    154 template< typename core_t >
    155 ast::__pass::template result1<ast::Stmt> ast::Pass< core_t >::call_accept( const ast::Stmt * stmt ) {
    156         __pedantic_pass_assert( __visit_children() );
    157         __pedantic_pass_assert( stmt );
    158 
    159         const ast::Stmt * nval = stmt->accept( *this );
    160         return { nval != stmt, nval };
     121        return { nval != node, nval };
    161122}
    162123
     
    230191ast::__pass::template resultNstmt<container_t> ast::Pass< core_t >::call_accept( const container_t< ptr<Stmt> > & statements ) {
    231192        __pedantic_pass_assert( __visit_children() );
    232         if ( statements.empty() ) return {};
     193        __pedantic_pass_assert( !statements.empty() );
    233194
    234195        // We are going to aggregate errors for all these statements
     
    263224                        const ast::Stmt * new_stmt = stmt->accept( *this );
    264225                        assert( new_stmt );
    265                         if ( new_stmt != stmt ) { new_kids.differs = true; }
    266226
    267227                        // Make sure that it is either adding statements or declartions but not both
     
    276236                        // Now add the statement if there is one
    277237                        if ( new_stmt != stmt ) {
    278                                 new_kids.values.emplace_back( new_stmt, i, false );
     238                                new_kids.differs = true;
     239                                new_kids.values.emplace_back( new_stmt );
    279240                        } else {
    280                                 new_kids.values.emplace_back( nullptr, i, true );
     241                                new_kids.values.emplace_back( i );
    281242                        }
    282243
     
    298259ast::__pass::template resultN<container_t, node_t> ast::Pass< core_t >::call_accept( const container_t< ast::ptr<node_t> > & container ) {
    299260        __pedantic_pass_assert( __visit_children() );
    300         if ( container.empty() ) return {};
     261        __pedantic_pass_assert( !container.empty() );
     262
     263        // Collect errors from processing all these nodes.
    301264        SemanticErrorException errors;
    302265
     
    342305        static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR" );
    343306
    344         auto new_val = call_accept( old_val );
    345 
    346         static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value /* || std::is_same<int, decltype(old_val)>::value */, "ERROR" );
    347 
    348         if ( new_val.differs ) {
     307        auto result = call_accept( old_val );
     308        if ( result.differs ) {
    349309                auto new_parent = __pass::mutate<core_t>(parent);
    350                 new_val.apply(new_parent, field);
     310                result.apply( new_parent, field );
    351311                parent = new_parent;
    352312        }
     
    366326        static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR" );
    367327
    368         auto new_val = call_accept_top( old_val );
    369 
    370         static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value /* || std::is_same<int, decltype(old_val)>::value */, "ERROR" );
    371 
    372         if ( new_val.differs ) {
     328        auto result = call_accept_top( old_val );
     329        if ( result.differs ) {
    373330                auto new_parent = __pass::mutate<core_t>(parent);
    374                 new_val.apply(new_parent, field);
     331                result.apply( new_parent, field );
    375332                parent = new_parent;
    376333        }
     
    390347        static_assert( !std::is_same<const ast::Node * &, decltype(old_val)>::value, "ERROR" );
    391348
    392         auto new_val = call_accept_as_compound( old_val );
    393 
    394         static_assert( !std::is_same<const ast::Node *, decltype(new_val)>::value || std::is_same<int, decltype(old_val)>::value, "ERROR" );
    395 
    396         if ( new_val.differs ) {
     349        auto result = call_accept_as_compound( old_val );
     350        if ( result.differs ) {
    397351                auto new_parent = __pass::mutate<core_t>(parent);
    398                 new_val.apply( new_parent, child );
     352                result.apply( new_parent, child );
    399353                parent = new_parent;
    400354        }
  • src/AST/Pass.proto.hpp

    rb78c54f r90320ac  
    154154                bool is_old;
    155155
    156                 delta(const Stmt * s, ssize_t i, bool old) :
    157                         new_val(s), old_idx(i), is_old(old) {}
     156                explicit delta(const Stmt * s) : new_val(s), old_idx(-1), is_old(false) {}
     157                explicit delta(ssize_t i) : new_val(nullptr), old_idx(i), is_old(true) {}
    158158        };
    159159
     
    188188                std::transform( stmts->begin(), stmts->end(), std::back_inserter( values ),
    189189                        [](ast::ptr<ast::Stmt>& stmt) -> delta {
    190                                 return delta( stmt.release(), -1, false );
     190                                return delta( stmt.release() );
    191191                        });
    192192                stmts->clear();
     
    201201                        [](ast::ptr<ast::Decl>& decl) -> delta {
    202202                                ast::Decl const * d = decl.release();
    203                                 return delta( new DeclStmt( d->location, d ), -1, false );
     203                                return delta( new DeclStmt( d->location, d ) );
    204204                        });
    205205                decls->clear();
Note: See TracChangeset for help on using the changeset viewer.