Changeset 246c245 for src/AST/Node.hpp


Ignore:
Timestamp:
May 16, 2019, 6:51:18 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
8ff178d, d66e7b7
Parents:
9b4f329 (diff), 6f8e87d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.hpp

    r9b4f329 r246c245  
    4444        };
    4545
    46         inline void increment(ref_type ref) const {
     46private:
     47        /// Make a copy of this node; should be overridden in subclass with more precise return type
     48        virtual Node * clone() const = 0;
     49
     50        /// Must be copied in ALL derived classes
     51        template<typename node_t>
     52        friend node_t * mutate(const node_t * node);
     53
     54        mutable size_t strong_count = 0;
     55        mutable size_t weak_count = 0;
     56
     57        void increment(ref_type ref) const {
    4758                switch (ref) {
    4859                        case ref_type::strong: strong_count++; break;
     
    5162        }
    5263
    53         inline void decrement(ref_type ref) const {
     64        void decrement(ast::Node::ref_type ref) const {
    5465                switch (ref) {
    5566                        case ref_type::strong: strong_count--; break;
     
    6172                }
    6273        }
    63 private:
    64         /// Make a copy of this node; should be overridden in subclass with more precise return type
    65         virtual const Node * clone() const = 0;
    6674
    67         /// Must be copied in ALL derived classes
    68         template<typename node_t>
    69         friend auto mutate(const node_t * node);
    70 
    71         mutable size_t strong_count = 0;
    72         mutable size_t weak_count = 0;
     75        template< typename node_t, enum Node::ref_type ref_t >
     76        friend class ptr_base;
    7377};
    7478
     
    7680// problems and be able to use auto return
    7781template<typename node_t>
    78 auto mutate( const node_t * node ) {
    79         assertf(
    80                 node->strong_count >= 1,
    81                 "Error: attempting to mutate a node that appears to have been linked"
    82         );
    83         if (node->strong_count == 1) {
     82node_t * mutate( const node_t * node ) {
     83        if (node->strong_count <= 1) {
    8484                return const_cast<node_t *>(node);
    8585        }
     
    100100public:
    101101        ptr_base() : node(nullptr) {}
    102         ptr_base( const node_t * n ) : node(n) { if( node ) increment(node, ref_t); }
    103         ~ptr_base() { if( node ) decrement(node, ref_t); }
     102        ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
     103        ~ptr_base() { if( node ) _dec(node); }
    104104
    105105        template< enum Node::ref_type o_ref_t >
    106106        ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
    107                 if( node ) increment(node, ref_t);
     107                if( node ) _inc(node);
    108108        }
    109109
    110110        template< enum Node::ref_type o_ref_t >
    111111        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
    112                 if( node ) increment(node, ref_t);
     112                if( node ) _inc(node);
    113113        }
    114114
     
    147147                assign( n );
    148148                // get mutable version of `n`
    149                 auto r = mutate( node ); 
     149                auto r = mutate( node );
    150150                // re-assign mutable version in case `mutate()` produced a new pointer
    151151                assign( r );
     
    157157private:
    158158        void assign( const node_t * other ) {
    159                 if( other ) increment(other, ref_t);
    160                 if( node  ) decrement(node , ref_t);
     159                if( other ) _inc(other);
     160                if( node  ) _dec(node );
    161161                node = other;
    162162        }
     163
     164        void _inc( const node_t * other );
     165        void _dec( const node_t * other );
    163166
    164167protected:
Note: See TracChangeset for help on using the changeset viewer.