Changeset 6d51bd7 for src/AST/Node.hpp


Ignore:
Timestamp:
May 15, 2019, 10:15:44 AM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
be567e9
Parents:
712348a
Message:

Fixes to the new templated pass and started on conversions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Node.hpp

    r712348a r6d51bd7  
    3030        // change/share reference counts
    3131        Node() = default;
    32         Node(const Node&) : strong_ref(0), weak_ref(0) {}
    33         Node(Node&&) : strong_ref(0), weak_ref(0) {}
     32        Node(const Node&) : strong_count(0), weak_count(0) {}
     33        Node(Node&&) : strong_count(0), weak_count(0) {}
    3434        Node& operator= (const Node&) = delete;
    3535        Node& operator= (Node&&) = delete;
    3636        virtual ~Node() = default;
    3737
    38         virtual Node* accept( Visitor& v ) = 0;
     38        virtual const Node * accept( Visitor & v ) const = 0;
    3939
    4040        /// Types of node references
     
    4646        inline void increment(ref_type ref) const {
    4747                switch (ref) {
    48                         case ref_type::strong: strong_ref++; break;
    49                         case ref_type::weak  : weak_ref  ++; break;
     48                        case ref_type::strong: strong_count++; break;
     49                        case ref_type::weak  : weak_count  ++; break;
    5050                }
    5151        }
     
    5353        inline void decrement(ref_type ref) const {
    5454                switch (ref) {
    55                         case ref_type::strong: strong_ref--; break;
    56                         case ref_type::weak  : weak_ref  --; break;
     55                        case ref_type::strong: strong_count--; break;
     56                        case ref_type::weak  : weak_count  --; break;
    5757                }
    5858
    59                 if(!strong_ref && !weak_ref) {
     59                if(!strong_count && !weak_count) {
    6060                        delete this;
    6161                }
    6262        }
     63private:
     64        /// Make a copy of this node; should be overridden in subclass with more precise return type
     65        virtual const Node * clone() const = 0;
    6366
     67        /// Must be copied in ALL derived classes
    6468        template<typename node_t>
    6569        friend auto mutate(const node_t * node);
    6670
    67 private:
    68         /// Make a copy of this node; should be overridden in subclass with more precise return type
    69         virtual Node* clone() const = 0;
    70 
    71         mutable size_t strong_ref = 0;
    72         mutable size_t weak_ref = 0;
     71        mutable size_t strong_count = 0;
     72        mutable size_t weak_count = 0;
    7373};
    7474
     
    100100public:
    101101        ptr_base() : node(nullptr) {}
    102         ptr_base( node_t * n ) : node(n) { if( !node ) node->increment(ref_t); }
    103         ~ptr_base() { if( node ) node->decrement(ref_t); }
     102        ptr_base( const node_t * n ) : node(n) { if( !node ) increment(node, ref_t); }
     103        ~ptr_base() { if( node ) decrement(node, ref_t); }
    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) {
    107107                if( !node ) return;
    108                 node->increment(ref_t);
     108                increment(node, ref_t);
    109109        }
    110110
    111111        template< enum  Node::ref_type o_ref_t >
    112112        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
    113                 if( node ) node->increment(ref_t);
     113                if( node ) increment(node, ref_t);
     114        }
     115
     116        template<typename o_node_t>
     117        ptr_base & operator=( const o_node_t * node ) {
     118                assign(strict_dynamic_cast<const node_t *>(node));
     119                return *this;
    114120        }
    115121
     
    136142
    137143private:
    138         void assign(node_t * other ) {
    139                 if( other ) other->increment(ref_t);
    140                 if( node  ) node ->decrement(ref_t);
     144        void assign(const node_t * other ) {
     145                if( other ) increment(other, ref_t);
     146                if( node  ) decrement(node , ref_t);
    141147                node = other;
    142148        }
    143149
    144150protected:
    145         node_t * node;
     151        const node_t * node;
    146152};
    147153
Note: See TracChangeset for help on using the changeset viewer.