Changeset eb211bf


Ignore:
Timestamp:
Feb 11, 2022, 4:57:07 PM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
0db48ca, ea89e36
Parents:
0240a7cb
Message:

Did some clean-up with the ast::Pass class. Moved some things out of the main header and cut out some duplication.

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Pass.hpp

    r0240a7cb reb211bf  
    239239private:
    240240
    241         // Regular nodes
     241        __pass::result1<ast::Stmt> call_accept( const ast::Stmt * );
     242        __pass::result1<ast::Expr> call_accept( const ast::Expr * );
     243
     244        /// This has a `type` member that is the return type for the
     245        /// generic call_accept if the generic call_accept is defined.
    242246        template< typename node_t >
    243         struct result1 {
    244                 bool differs;
    245                 const node_t * value;
    246 
    247                 template< typename object_t, typename super_t, typename field_t >
    248                 void apply(object_t *, field_t super_t::* field);
    249         };
    250 
    251         result1<ast::Stmt> call_accept( const ast::Stmt * );
    252         result1<ast::Expr> call_accept( const ast::Expr * );
     247        using generic_call_accept_result =
     248                std::enable_if<
     249                                !std::is_base_of<ast::Expr, node_t>::value &&
     250                                !std::is_base_of<ast::Stmt, node_t>::value
     251                        , __pass::result1<
     252                                typename std::remove_pointer< typename std::result_of<
     253                                        decltype(&node_t::accept)(node_t*, type&) >::type >::type
     254                        >
     255                >;
    253256
    254257        template< typename node_t >
    255258        auto call_accept( const node_t * node )
    256                 -> typename std::enable_if<
    257                                 !std::is_base_of<ast::Expr, node_t>::value &&
    258                                 !std::is_base_of<ast::Stmt, node_t>::value
    259                         , result1<
    260                                 typename std::remove_pointer< decltype( node->accept(*this) ) >::type
    261                         >
    262                 >::type;
     259                -> typename generic_call_accept_result<node_t>::type;
    263260
    264261        // requests WithStmtsToAdd directly add to this statement, as if it is a compound.
    265         result1<ast::Stmt> call_accept_as_compound(const ast::Stmt *);
    266 
    267         // Container of statements
     262        __pass::result1<ast::Stmt> call_accept_as_compound(const ast::Stmt *);
     263
    268264        template< template <class...> class container_t >
    269         struct resultNstmt {
    270                 struct delta {
    271                         ptr<Stmt> nval;
    272                         ssize_t old_idx;
    273                         bool is_old;
    274 
    275                         delta(const Stmt * s, ssize_t i, bool old) : nval{s}, old_idx{i}, is_old{old} {}
    276                 };
    277 
    278                 bool differs;
    279                 container_t< delta > values;
    280 
    281                 resultNstmt() : differs(false), values{} {}
    282                 resultNstmt(bool diff, container_t< delta > && vals) : differs(diff), values(vals) {}
    283 
    284                 template< typename object_t, typename super_t, typename field_t >
    285                 void apply(object_t *, field_t super_t::* field);
    286 
    287                 template< template <class...> class incontainer_t >
    288                 void take_all( incontainer_t<ast::ptr<ast::Stmt>> * stmts ) {
    289                         if(!stmts || stmts->empty()) return;
    290 
    291                         std::transform(stmts->begin(), stmts->end(), std::back_inserter( values ), [](ast::ptr<ast::Stmt>& decl) -> delta {
    292                                         return delta( decl.release(), -1, false );
    293                                 });
    294                         stmts->clear();
    295                         differs = true;
    296                 }
    297 
    298                 template< template <class...> class incontainer_t >
    299                 void take_all( incontainer_t<ast::ptr<ast::Decl>> * decls ) {
    300                         if(!decls || decls->empty()) return;
    301 
    302                         std::transform(decls->begin(), decls->end(), std::back_inserter( values ), [](ast::ptr<ast::Decl>& decl) -> auto {
    303                                         auto loc = decl->location;
    304                                         auto stmt = new DeclStmt( loc, decl.release() );
    305                                         return delta( stmt, -1, false );
    306                                 });
    307                         decls->clear();
    308                         differs = true;
    309                 }
    310         };
    311 
    312         template< template <class...> class container_t >
    313         resultNstmt<container_t> call_accept( const container_t< ptr<Stmt> > & );
    314 
    315         // Container of something
     265        __pass::resultNstmt<container_t> call_accept( const container_t< ptr<Stmt> > & );
     266
    316267        template< template <class...> class container_t, typename node_t >
    317         struct resultN {
    318                 bool differs;
    319                 container_t<ptr<node_t>> values;
    320 
    321                 template< typename object_t, typename super_t, typename field_t >
    322                 void apply(object_t *, field_t super_t::* field);
    323         };
    324 
    325         template< template <class...> class container_t, typename node_t >
    326         resultN< container_t, node_t > call_accept( const container_t< ptr<node_t> > & container );
     268        __pass::resultN< container_t, node_t > call_accept( const container_t< ptr<node_t> > & container );
    327269
    328270public:
  • src/AST/Pass.impl.hpp

    r0240a7cb reb211bf  
    111111                }
    112112
    113 
    114113                //------------------------------
    115114                /// Check if value was mutated, different for pointers and containers
     
    125124        }
    126125
    127 
    128         template< typename core_t >
    129126        template< typename node_t >
    130127        template< typename object_t, typename super_t, typename field_t >
    131         void ast::Pass< core_t >::result1< node_t >::apply(object_t * object, field_t super_t::* field) {
     128        void __pass::result1< node_t >::apply( object_t * object, field_t super_t::* field ) {
    132129                object->*field = value;
    133130        }
     
    136133        template< typename node_t >
    137134        auto ast::Pass< core_t >::call_accept( const node_t * node )
    138                 -> typename std::enable_if<
    139                                 !std::is_base_of<ast::Expr, node_t>::value &&
    140                                 !std::is_base_of<ast::Stmt, node_t>::value
    141                         , ast::Pass< core_t >::result1<
    142                                 typename std::remove_pointer< decltype( node->accept(*this) ) >::type
    143                         >
    144                 >::type
     135                -> typename ast::Pass< core_t >::template generic_call_accept_result<node_t>::type
    145136        {
    146137                __pedantic_pass_assert( __visit_children() );
     
    151142
    152143                auto nval = node->accept( *this );
    153                 ast::Pass< core_t >::result1<
     144                __pass::result1<
    154145                        typename std::remove_pointer< decltype( node->accept(*this) ) >::type
    155146                > res;
     
    160151
    161152        template< typename core_t >
    162         typename ast::Pass< core_t >::template result1<ast::Expr> ast::Pass< core_t >::call_accept( const ast::Expr * expr ) {
     153        __pass::template result1<ast::Expr> ast::Pass< core_t >::call_accept( const ast::Expr * expr ) {
    163154                __pedantic_pass_assert( __visit_children() );
    164155                __pedantic_pass_assert( expr );
     
    174165
    175166        template< typename core_t >
    176         typename ast::Pass< core_t >::template result1<ast::Stmt> ast::Pass< core_t >::call_accept( const ast::Stmt * stmt ) {
     167        __pass::template result1<ast::Stmt> ast::Pass< core_t >::call_accept( const ast::Stmt * stmt ) {
    177168                __pedantic_pass_assert( __visit_children() );
    178169                __pedantic_pass_assert( stmt );
     
    183174
    184175        template< typename core_t >
    185         typename ast::Pass< core_t >::template result1<ast::Stmt> ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) {
     176        __pass::template result1<ast::Stmt> ast::Pass< core_t >::call_accept_as_compound( const ast::Stmt * stmt ) {
    186177                __pedantic_pass_assert( __visit_children() );
    187178                __pedantic_pass_assert( stmt );
     
    233224        }
    234225
    235         template< typename core_t >
    236226        template< template <class...> class container_t >
    237227        template< typename object_t, typename super_t, typename field_t >
    238         void ast::Pass< core_t >::resultNstmt<container_t>::apply(object_t * object, field_t super_t::* field) {
     228        void __pass::resultNstmt<container_t>::apply(object_t * object, field_t super_t::* field) {
    239229                auto & container = object->*field;
    240230                __pedantic_pass_assert( container.size() <= values.size() );
     
    243233
    244234                container_t<ptr<Stmt>> nvals;
    245                 for(delta & d : values) {
    246                         if( d.is_old ) {
     235                for (delta & d : values) {
     236                        if ( d.is_old ) {
    247237                                __pedantic_pass_assert( cit.idx <= d.old_idx );
    248238                                std::advance( cit, d.old_idx - cit.idx );
    249239                                nvals.push_back( std::move( (*cit).val) );
    250240                        } else {
    251                                 nvals.push_back( std::move(d.nval) );
     241                                nvals.push_back( std::move(d.new_val) );
    252242                        }
    253243                }
    254244
    255                 object->*field = std::move(nvals);
     245                container = std::move(nvals);
     246        }
     247
     248        template< template <class...> class container_t >
     249        template< template <class...> class incontainer_t >
     250        void __pass::resultNstmt< container_t >::take_all( incontainer_t<ptr<Stmt>> * stmts ) {
     251                if (!stmts || stmts->empty()) return;
     252
     253                std::transform(stmts->begin(), stmts->end(), std::back_inserter( values ),
     254                        [](ast::ptr<ast::Stmt>& stmt) -> delta {
     255                                return delta( stmt.release(), -1, false );
     256                        });
     257                stmts->clear();
     258                differs = true;
     259        }
     260
     261        template< template<class...> class container_t >
     262        template< template<class...> class incontainer_t >
     263        void __pass::resultNstmt< container_t >::take_all( incontainer_t<ptr<Decl>> * decls ) {
     264                if (!decls || decls->empty()) return;
     265
     266                std::transform(decls->begin(), decls->end(), std::back_inserter( values ),
     267                        [](ast::ptr<ast::Decl>& decl) -> delta {
     268                                auto loc = decl->location;
     269                                auto stmt = new DeclStmt( loc, decl.release() );
     270                                return delta( stmt, -1, false );
     271                        });
     272                decls->clear();
     273                differs = true;
    256274        }
    257275
    258276        template< typename core_t >
    259277        template< template <class...> class container_t >
    260         typename ast::Pass< core_t >::template resultNstmt<container_t> ast::Pass< core_t >::call_accept( const container_t< ptr<Stmt> > & statements ) {
     278        __pass::template resultNstmt<container_t> ast::Pass< core_t >::call_accept( const container_t< ptr<Stmt> > & statements ) {
    261279                __pedantic_pass_assert( __visit_children() );
    262280                if( statements.empty() ) return {};
     
    285303                pass_visitor_stats.avg->push(pass_visitor_stats.depth);
    286304
    287                 resultNstmt<container_t> new_kids;
     305                __pass::resultNstmt<container_t> new_kids;
    288306                for( auto value : enumerate( statements ) ) {
    289307                        try {
     
    327345        }
    328346
    329         template< typename core_t >
    330347        template< template <class...> class container_t, typename node_t >
    331348        template< typename object_t, typename super_t, typename field_t >
    332         void ast::Pass< core_t >::resultN<container_t, node_t>::apply(object_t * object, field_t super_t::* field) {
     349        void __pass::resultN<container_t, node_t>::apply(object_t * object, field_t super_t::* field) {
    333350                auto & container = object->*field;
    334351                __pedantic_pass_assert( container.size() == values.size() );
     
    346363        template< typename core_t >
    347364        template< template <class...> class container_t, typename node_t >
    348         typename ast::Pass< core_t >::template resultN<container_t, node_t> ast::Pass< core_t >::call_accept( const container_t< ast::ptr<node_t> > & container ) {
     365        __pass::template resultN<container_t, node_t> ast::Pass< core_t >::call_accept( const container_t< ast::ptr<node_t> > & container ) {
    349366                __pedantic_pass_assert( __visit_children() );
    350367                if( container.empty() ) return {};
     
    378395                if ( ! errors.isEmpty() ) { throw errors; }
    379396
    380                 return ast::Pass< core_t >::resultN<container_t, node_t>{ mutated, new_kids };
     397                return ast::__pass::resultN<container_t, node_t>{ mutated, new_kids };
    381398        }
    382399
  • src/AST/Pass.proto.hpp

    r0240a7cb reb211bf  
    123123                static constexpr bool value = std::is_void< ret_t >::value ||
    124124                        std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
     125        };
     126
     127        /// The result is a single node.
     128        template< typename node_t >
     129        struct result1 {
     130                bool differs;
     131                const node_t * value;
     132
     133                template< typename object_t, typename super_t, typename field_t >
     134                void apply( object_t *, field_t super_t::* field );
     135        };
     136
     137        /// The result is a container of statements.
     138        template< template<class...> class container_t >
     139        struct resultNstmt {
     140                /// The delta/change on a single node.
     141                struct delta {
     142                        ptr<Stmt> new_val;
     143                        ssize_t old_idx;
     144                        bool is_old;
     145
     146                        delta(const Stmt * s, ssize_t i, bool old) :
     147                                new_val(s), old_idx(i), is_old(old) {}
     148                };
     149
     150                bool differs;
     151                container_t< delta > values;
     152
     153                template< typename object_t, typename super_t, typename field_t >
     154                void apply( object_t *, field_t super_t::* field );
     155
     156                template< template<class...> class incontainer_t >
     157                void take_all( incontainer_t<ptr<Stmt>> * stmts );
     158
     159                template< template<class...> class incontainer_t >
     160                void take_all( incontainer_t<ptr<Decl>> * decls );
     161        };
     162
     163        /// The result is a container of nodes.
     164        template< template<class...> class container_t, typename node_t >
     165        struct resultN {
     166                bool differs;
     167                container_t<ptr<node_t>> values;
     168
     169                template< typename object_t, typename super_t, typename field_t >
     170                void apply( object_t *, field_t super_t::* field );
    125171        };
    126172
  • src/Common/CodeLocation.h

    r0240a7cb reb211bf  
    2525        /// Create a new unset CodeLocation.
    2626        CodeLocation() = default;
    27 
    2827
    2928        /// Create a new CodeLocation with the given values.
Note: See TracChangeset for help on using the changeset viewer.