[2bb4a01] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // Node.hpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Wed May 8 10:27:04 2019 |
---|
[1e97287] | 11 | // Last Modified By : Andrew Beach |
---|
[dafe9e1] | 12 | // Last Modified On : Mon Jun 3 13:26:00 2019 |
---|
| 13 | // Update Count : 5 |
---|
[2bb4a01] | 14 | // |
---|
| 15 | |
---|
[cedb545] | 16 | #pragma once |
---|
| 17 | |
---|
[2bb4a01] | 18 | #include <cassert> |
---|
| 19 | #include <iosfwd> |
---|
[cedb545] | 20 | |
---|
| 21 | namespace ast { |
---|
[2bb4a01] | 22 | |
---|
| 23 | class Visitor; |
---|
| 24 | |
---|
| 25 | /// Base class for all AST nodes. |
---|
| 26 | /// Keeps both strong and weak reference counts. |
---|
[f47f887] | 27 | class Node { |
---|
| 28 | public: |
---|
[14cebb7a] | 29 | // override defaults to ensure assignment doesn't |
---|
[2bb4a01] | 30 | // change/share reference counts |
---|
| 31 | Node() = default; |
---|
[6d51bd7] | 32 | Node(const Node&) : strong_count(0), weak_count(0) {} |
---|
| 33 | Node(Node&&) : strong_count(0), weak_count(0) {} |
---|
[2bb4a01] | 34 | Node& operator= (const Node&) = delete; |
---|
| 35 | Node& operator= (Node&&) = delete; |
---|
[f47f887] | 36 | virtual ~Node() = default; |
---|
[cedb545] | 37 | |
---|
[6d51bd7] | 38 | virtual const Node * accept( Visitor & v ) const = 0; |
---|
[2bb4a01] | 39 | |
---|
| 40 | /// Types of node references |
---|
[f47f887] | 41 | enum class ref_type { |
---|
| 42 | strong, |
---|
| 43 | weak |
---|
| 44 | }; |
---|
[cedb545] | 45 | |
---|
[87701b6] | 46 | private: |
---|
| 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 { |
---|
[f47f887] | 58 | switch (ref) { |
---|
[6d51bd7] | 59 | case ref_type::strong: strong_count++; break; |
---|
| 60 | case ref_type::weak : weak_count ++; break; |
---|
[cedb545] | 61 | } |
---|
[f47f887] | 62 | } |
---|
[cedb545] | 63 | |
---|
[87701b6] | 64 | void decrement(ast::Node::ref_type ref) const { |
---|
[f47f887] | 65 | switch (ref) { |
---|
[6d51bd7] | 66 | case ref_type::strong: strong_count--; break; |
---|
| 67 | case ref_type::weak : weak_count --; break; |
---|
[cedb545] | 68 | } |
---|
| 69 | |
---|
[6d51bd7] | 70 | if(!strong_count && !weak_count) { |
---|
[f47f887] | 71 | delete this; |
---|
[6a625de] | 72 | } |
---|
[f47f887] | 73 | } |
---|
[6a625de] | 74 | |
---|
[87701b6] | 75 | template< typename node_t, enum Node::ref_type ref_t > |
---|
| 76 | friend class ptr_base; |
---|
[f47f887] | 77 | }; |
---|
| 78 | |
---|
| 79 | // Mutate a node, non-member function to avoid static type |
---|
| 80 | // problems and be able to use auto return |
---|
| 81 | template<typename node_t> |
---|
[87701b6] | 82 | node_t * mutate( const node_t * node ) { |
---|
| 83 | if (node->strong_count <= 1) { |
---|
[f47f887] | 84 | return const_cast<node_t *>(node); |
---|
[6a625de] | 85 | } |
---|
| 86 | |
---|
[f47f887] | 87 | assertf( |
---|
| 88 | node->weak_count == 0, |
---|
| 89 | "Error: mutating node with weak references to it will invalided some references" |
---|
| 90 | ); |
---|
| 91 | return node->clone(); |
---|
| 92 | } |
---|
| 93 | |
---|
[54e41b3] | 94 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
[2bb4a01] | 95 | |
---|
[d8938622] | 96 | /// Call a visitor on a possibly-null node |
---|
| 97 | template<typename node_t> |
---|
| 98 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
| 99 | return n ? n->accept( v ) : nullptr; |
---|
| 100 | } |
---|
| 101 | |
---|
[9e1d485] | 102 | /// Base class for the smart pointer types |
---|
| 103 | /// should never really be used. |
---|
| 104 | template< typename node_t, enum Node::ref_type ref_t > |
---|
[f47f887] | 105 | class ptr_base { |
---|
| 106 | public: |
---|
| 107 | ptr_base() : node(nullptr) {} |
---|
[87701b6] | 108 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
[dafe9e1] | 109 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
[f47f887] | 110 | |
---|
[f685679] | 111 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
| 112 | if( node ) _inc(node); |
---|
| 113 | } |
---|
| 114 | |
---|
[d76c588] | 115 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
[f685679] | 116 | |
---|
[9e1d485] | 117 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 118 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) { |
---|
[87701b6] | 119 | if( node ) _inc(node); |
---|
[6a625de] | 120 | } |
---|
| 121 | |
---|
[9e1d485] | 122 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 123 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) { |
---|
[87701b6] | 124 | if( node ) _inc(node); |
---|
[6d51bd7] | 125 | } |
---|
| 126 | |
---|
| 127 | template<typename o_node_t> |
---|
| 128 | ptr_base & operator=( const o_node_t * node ) { |
---|
[dafe9e1] | 129 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
[6d51bd7] | 130 | return *this; |
---|
[f47f887] | 131 | } |
---|
[cedb545] | 132 | |
---|
[f685679] | 133 | ptr_base & operator=( const ptr_base & o ) { |
---|
| 134 | assign(o.node); |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | ptr_base & operator=( ptr_base && o ) { |
---|
[d76c588] | 139 | if ( node == o.node ) return *this; |
---|
| 140 | if ( node ) _dec(node); |
---|
| 141 | node = o.node; |
---|
| 142 | o.node = nullptr; |
---|
[f685679] | 143 | return *this; |
---|
| 144 | } |
---|
| 145 | |
---|
[9e1d485] | 146 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 147 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
| 148 | assign(o.node); |
---|
| 149 | return *this; |
---|
| 150 | } |
---|
[cedb545] | 151 | |
---|
[9e1d485] | 152 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 153 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
| 154 | assign(o.node); |
---|
| 155 | return *this; |
---|
| 156 | } |
---|
[cedb545] | 157 | |
---|
[f47f887] | 158 | const node_t * get() const { return node; } |
---|
| 159 | const node_t * operator->() const { return node; } |
---|
| 160 | const node_t & operator* () const { return *node; } |
---|
| 161 | explicit operator bool() const { return node; } |
---|
[3648d98] | 162 | operator const node_t * () const { return node; } |
---|
[cedb545] | 163 | |
---|
[54e41b3] | 164 | /// wrapper for convenient access to dynamic_cast |
---|
[fdbd4fd] | 165 | template<typename o_node_t> |
---|
[9e1d485] | 166 | const o_node_t * as() const { return dynamic_cast<const o_node_t *>(node); } |
---|
[fdbd4fd] | 167 | |
---|
[76ed81f] | 168 | /// Returns a mutable version of the pointer in this node. |
---|
| 169 | node_t * get_and_mutate(); |
---|
| 170 | |
---|
[54e41b3] | 171 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
| 172 | /// Returns a mutable version of the pointer in this node. |
---|
[10a1225] | 173 | node_t * set_and_mutate( const node_t * n ); |
---|
[54e41b3] | 174 | |
---|
[f47f887] | 175 | using ptr = const node_t *; |
---|
[cedb545] | 176 | |
---|
[f47f887] | 177 | private: |
---|
[9e1d485] | 178 | void assign( const node_t * other ) { |
---|
[87701b6] | 179 | if( other ) _inc(other); |
---|
| 180 | if( node ) _dec(node ); |
---|
[f47f887] | 181 | node = other; |
---|
| 182 | } |
---|
[cedb545] | 183 | |
---|
[87701b6] | 184 | void _inc( const node_t * other ); |
---|
| 185 | void _dec( const node_t * other ); |
---|
| 186 | |
---|
[f47f887] | 187 | protected: |
---|
[9e1d485] | 188 | const node_t * node; |
---|
[f47f887] | 189 | }; |
---|
[cedb545] | 190 | |
---|
[2bb4a01] | 191 | /// Owning pointer to node |
---|
[f47f887] | 192 | template< typename node_t > |
---|
| 193 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
[cedb545] | 194 | |
---|
[2bb4a01] | 195 | /// Observing pointer to node |
---|
[f47f887] | 196 | template< typename node_t > |
---|
| 197 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
[2bb4a01] | 198 | } |
---|
| 199 | |
---|
| 200 | // Local Variables: // |
---|
| 201 | // tab-width: 4 // |
---|
| 202 | // mode: c++ // |
---|
| 203 | // compile-command: "make install" // |
---|
[1e97287] | 204 | // End: // |
---|