Changeset 485cf59 for src


Ignore:
Timestamp:
Apr 8, 2024, 12:27:33 PM (8 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
0bbe172
Parents:
cb98d9d
Message:

Small improvement in the Pass template implementation. Thierry and I discussed this years ago but at the time we were not using a high enough C++ standard.

File:
1 edited

Legend:

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

    rcb98d9d r485cf59  
    226226                // Now the original containers should still have the unchanged values
    227227                // but also contain the new values.
    228         }
    229 };
    230 
    231 /// Used by previsit implementation
    232 /// We need to reassign the result to 'node', unless the function
    233 /// returns void, then we just leave 'node' unchanged
    234 template<bool is_void>
    235 struct __assign;
    236 
    237 template<>
    238 struct __assign<true> {
    239         template<typename core_t, typename node_t>
    240         static inline void result( core_t & core, const node_t * & node ) {
    241                 core.previsit( node );
    242         }
    243 };
    244 
    245 template<>
    246 struct __assign<false> {
    247         template<typename core_t, typename node_t>
    248         static inline void result( core_t & core, const node_t * & node ) {
    249                 node = core.previsit( node );
    250                 assertf(node, "Previsit must not return NULL");
    251         }
    252 };
    253 
    254 /// Used by postvisit implementation
    255 /// We need to return the result unless the function
    256 /// returns void, then we just return the original node
    257 template<bool is_void>
    258 struct __return;
    259 
    260 template<>
    261 struct __return<true> {
    262         template<typename core_t, typename node_t>
    263         static inline const node_t * result( core_t & core, const node_t * & node ) {
    264                 core.postvisit( node );
    265                 return node;
    266         }
    267 };
    268 
    269 template<>
    270 struct __return<false> {
    271         template<typename core_t, typename node_t>
    272         static inline auto result( core_t & core, const node_t * & node ) {
    273                 return core.postvisit( node );
    274228        }
    275229};
     
    297251        );
    298252
    299         __assign<
    300                 std::is_void<
    301                         decltype( core.previsit( node ) )
    302                 >::value
    303         >::result( core, node );
     253        // We need to reassign the result to 'node', unless the function
     254        // returns void, then we just leave 'node' unchanged
     255        if constexpr ( std::is_void_v<decltype( core.previsit( node ) )> ) {
     256                core.previsit( node );
     257        } else {
     258                node = core.previsit( node );
     259                assertf(node, "Previsit must not return NULL");
     260        }
    304261}
    305262
     
    312269        decltype( core.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
    313270{
    314         return __return<
    315                 std::is_void<
    316                         decltype( core.postvisit( node ) )
    317                 >::value
    318         >::result( core, node );
     271        // We need to return the result unless the function
     272        // returns void, then we just return the original node
     273        if constexpr ( std::is_void_v<decltype( core.postvisit( node ) )> ) {
     274                core.postvisit( node );
     275                return node;
     276        } else {
     277                return core.postvisit( node );
     278        }
    319279}
    320280
Note: See TracChangeset for help on using the changeset viewer.