Changeset 58fe85a for src/AST/Node.hpp
- Timestamp:
- Jan 7, 2021, 3:27:00 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 2b4daf2, 64aeca0
- Parents:
- 3c64c668 (diff), eef8dfb (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Node.hpp
r3c64c668 r58fe85a 10 10 // Created On : Wed May 8 10:27:04 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Jun 3 13:26:00 201913 // Update Count : 512 // Last Modified On : Fri Jun 5 9:47:00 2020 13 // Update Count : 6 14 14 // 15 15 … … 38 38 Node& operator= (const Node&) = delete; 39 39 Node& operator= (Node&&) = delete; 40 virtual ~Node() = default;40 virtual ~Node() {} 41 41 42 42 virtual const Node * accept( Visitor & v ) const = 0; … … 49 49 50 50 bool unique() const { return strong_count == 1; } 51 bool isManaged() const {return strong_count > 0; } 51 52 52 53 private: … … 57 58 template<typename node_t> 58 59 friend node_t * mutate(const node_t * node); 60 template<typename node_t> 61 friend node_t * shallowCopy(const node_t * node); 59 62 60 63 mutable size_t strong_count = 0; … … 69 72 } 70 73 71 void decrement(ast::Node::ref_type ref ) const {74 void decrement(ast::Node::ref_type ref, bool do_delete = true) const { 72 75 switch (ref) { 73 76 case ref_type::strong: strong_count--; break; … … 75 78 } 76 79 77 if( !strong_count && !weak_count) {80 if( do_delete && !strong_count && !weak_count) { 78 81 delete this; 79 82 } … … 94 97 assertf( 95 98 node->weak_count == 0, 96 "Error: mutating node with weak references to it will invalid edsome references"99 "Error: mutating node with weak references to it will invalidate some references" 97 100 ); 98 101 return node->clone(); … … 104 107 // skip mutate if equivalent 105 108 if ( node->*field == val ) return node; 106 109 107 110 // mutate and return 108 111 node_t * ret = mutate( node ); … … 123 126 (ret->*field)[i] = std::forward< field_t >( val ); 124 127 return ret; 128 } 129 130 /// Mutate an entire indexed collection by cloning to accepted value 131 template<typename node_t, typename parent_t, typename coll_t> 132 const node_t * mutate_each( const node_t * node, coll_t parent_t::* field, Visitor & v ) { 133 for ( unsigned i = 0; i < (node->*field).size(); ++i ) { 134 node = mutate_field_index( node, field, i, (node->*field)[i]->accept( v ) ); 135 } 136 return node; 125 137 } 126 138 … … 217 229 const node_t & operator* () const { _check(); return *node; } 218 230 explicit operator bool() const { _check(); return node; } 219 operator const node_t * () const { _check(); return node; } 231 operator const node_t * () const & { _check(); return node; } 232 operator const node_t * () && = delete; 233 234 const node_t * release() { 235 const node_t * ret = node; 236 if ( node ) { 237 _dec(node, false); 238 node = nullptr; 239 } 240 return ret; 241 } 220 242 221 243 /// wrapper for convenient access to dynamic_cast … … 223 245 const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } 224 246 225 /// wrapper for convenient access to strict_dynamic_cast247 /// Wrapper that makes sure dynamic_cast returns non-null. 226 248 template<typename o_node_t> 227 const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } 249 const o_node_t * strict_as() const { 250 if (const o_node_t * ret = as<o_node_t>()) return ret; 251 _strict_fail(); 252 } 253 254 /// Wrapper that makes sure dynamic_cast does not fail. 255 template<typename o_node_t, decltype(nullptr) null> 256 const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; } 228 257 229 258 /// Returns a mutable version of the pointer in this node. … … 244 273 245 274 void _inc( const node_t * other ); 246 void _dec( const node_t * other );275 void _dec( const node_t * other, bool do_delete = true ); 247 276 void _check() const; 277 void _strict_fail() const __attribute__((noreturn)); 248 278 249 279 const node_t * node;
Note:
See TracChangeset
for help on using the changeset viewer.