[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> |
---|
[b8524ca] | 19 | #include <cstddef> // for nullptr_t |
---|
[2bb4a01] | 20 | #include <iosfwd> |
---|
[b7d92b96] | 21 | #include <type_traits> // for remove_reference |
---|
[cedb545] | 22 | |
---|
[1346914] | 23 | #include "Common/ErrorObjects.h" // for SemanticErrorException |
---|
| 24 | |
---|
[cedb545] | 25 | namespace ast { |
---|
[2bb4a01] | 26 | |
---|
| 27 | class Visitor; |
---|
| 28 | |
---|
| 29 | /// Base class for all AST nodes. |
---|
| 30 | /// Keeps both strong and weak reference counts. |
---|
[f47f887] | 31 | class Node { |
---|
| 32 | public: |
---|
[14cebb7a] | 33 | // override defaults to ensure assignment doesn't |
---|
[2bb4a01] | 34 | // change/share reference counts |
---|
[bcb311b] | 35 | Node() = default; |
---|
| 36 | Node(const Node&) : strong_count(0), weak_count(0) {} |
---|
| 37 | Node(Node&&) : strong_count(0), weak_count(0) {} |
---|
[2bb4a01] | 38 | Node& operator= (const Node&) = delete; |
---|
| 39 | Node& operator= (Node&&) = delete; |
---|
[bcb311b] | 40 | virtual ~Node() {} |
---|
[cedb545] | 41 | |
---|
[6d51bd7] | 42 | virtual const Node * accept( Visitor & v ) const = 0; |
---|
[2bb4a01] | 43 | |
---|
| 44 | /// Types of node references |
---|
[f47f887] | 45 | enum class ref_type { |
---|
| 46 | strong, |
---|
| 47 | weak |
---|
| 48 | }; |
---|
[cedb545] | 49 | |
---|
[4864a73] | 50 | bool unique() const { return strong_count == 1; } |
---|
| 51 | |
---|
[87701b6] | 52 | private: |
---|
| 53 | /// Make a copy of this node; should be overridden in subclass with more precise return type |
---|
| 54 | virtual Node * clone() const = 0; |
---|
| 55 | |
---|
| 56 | /// Must be copied in ALL derived classes |
---|
| 57 | template<typename node_t> |
---|
| 58 | friend node_t * mutate(const node_t * node); |
---|
[99da267] | 59 | template<typename node_t> |
---|
| 60 | friend node_t * shallowCopy(const node_t * node); |
---|
[87701b6] | 61 | |
---|
| 62 | mutable size_t strong_count = 0; |
---|
| 63 | mutable size_t weak_count = 0; |
---|
[4864a73] | 64 | mutable bool was_ever_strong = false; |
---|
[87701b6] | 65 | |
---|
| 66 | void increment(ref_type ref) const { |
---|
[f47f887] | 67 | switch (ref) { |
---|
[4864a73] | 68 | case ref_type::strong: strong_count++; was_ever_strong = true; break; |
---|
[6d51bd7] | 69 | case ref_type::weak : weak_count ++; break; |
---|
[cedb545] | 70 | } |
---|
[f47f887] | 71 | } |
---|
[cedb545] | 72 | |
---|
[7d0881c] | 73 | void decrement(ast::Node::ref_type ref, bool do_delete = true) const { |
---|
[f47f887] | 74 | switch (ref) { |
---|
[6d51bd7] | 75 | case ref_type::strong: strong_count--; break; |
---|
| 76 | case ref_type::weak : weak_count --; break; |
---|
[cedb545] | 77 | } |
---|
| 78 | |
---|
[7d0881c] | 79 | if( do_delete && !strong_count && !weak_count) { |
---|
[f47f887] | 80 | delete this; |
---|
[6a625de] | 81 | } |
---|
[f47f887] | 82 | } |
---|
[6a625de] | 83 | |
---|
[87701b6] | 84 | template< typename node_t, enum Node::ref_type ref_t > |
---|
| 85 | friend class ptr_base; |
---|
[f47f887] | 86 | }; |
---|
| 87 | |
---|
[b7d92b96] | 88 | /// Mutate a node, non-member function to avoid static type |
---|
| 89 | /// problems and be able to use auto return |
---|
[f47f887] | 90 | template<typename node_t> |
---|
[87701b6] | 91 | node_t * mutate( const node_t * node ) { |
---|
| 92 | if (node->strong_count <= 1) { |
---|
[f47f887] | 93 | return const_cast<node_t *>(node); |
---|
[6a625de] | 94 | } |
---|
| 95 | |
---|
[f47f887] | 96 | assertf( |
---|
| 97 | node->weak_count == 0, |
---|
[2890212] | 98 | "Error: mutating node with weak references to it will invalidate some references" |
---|
[f47f887] | 99 | ); |
---|
| 100 | return node->clone(); |
---|
| 101 | } |
---|
| 102 | |
---|
[b7d92b96] | 103 | /// Mutate a node field (only clones if not equal to existing value) |
---|
[18e683b] | 104 | template<typename node_t, typename parent_t, typename field_t, typename assn_t> |
---|
| 105 | const node_t * mutate_field( const node_t * node, field_t parent_t::* field, assn_t && val ) { |
---|
[2d11663] | 106 | // skip mutate if equivalent |
---|
[b7d92b96] | 107 | if ( node->*field == val ) return node; |
---|
[2890212] | 108 | |
---|
[2d11663] | 109 | // mutate and return |
---|
| 110 | node_t * ret = mutate( node ); |
---|
| 111 | ret->*field = std::forward< assn_t >( val ); |
---|
| 112 | return ret; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /// Mutate a single index of a node field (only clones if not equal to existing value) |
---|
[18e683b] | 116 | template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t> |
---|
[2d11663] | 117 | const node_t * mutate_field_index( |
---|
[18e683b] | 118 | const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val |
---|
[2d11663] | 119 | ) { |
---|
| 120 | // skip mutate if equivalent |
---|
| 121 | if ( (node->*field)[i] == val ) return node; |
---|
| 122 | |
---|
| 123 | // mutate and return |
---|
[b7d92b96] | 124 | node_t * ret = mutate( node ); |
---|
[2d11663] | 125 | (ret->*field)[i] = std::forward< field_t >( val ); |
---|
[b7d92b96] | 126 | return ret; |
---|
| 127 | } |
---|
| 128 | |
---|
[e0e9a0b] | 129 | /// Mutate an entire indexed collection by cloning to accepted value |
---|
| 130 | template<typename node_t, typename parent_t, typename coll_t> |
---|
| 131 | const node_t * mutate_each( const node_t * node, coll_t parent_t::* field, Visitor & v ) { |
---|
| 132 | for ( unsigned i = 0; i < (node->*field).size(); ++i ) { |
---|
| 133 | node = mutate_field_index( node, field, i, (node->*field)[i]->accept( v ) ); |
---|
| 134 | } |
---|
| 135 | return node; |
---|
| 136 | } |
---|
| 137 | |
---|
[54e41b3] | 138 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
[2bb4a01] | 139 | |
---|
[d8938622] | 140 | /// Call a visitor on a possibly-null node |
---|
| 141 | template<typename node_t> |
---|
| 142 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
| 143 | return n ? n->accept( v ) : nullptr; |
---|
| 144 | } |
---|
| 145 | |
---|
[1346914] | 146 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed |
---|
| 147 | template< typename Container > |
---|
| 148 | void accept_each( const Container & c, Visitor & v ) { |
---|
| 149 | SemanticErrorException errors; |
---|
| 150 | for ( const auto & i : c ) { |
---|
| 151 | try { |
---|
| 152 | if ( i ) { |
---|
| 153 | i->accept( v ); |
---|
| 154 | } |
---|
| 155 | } catch ( SemanticErrorException & e ) { |
---|
| 156 | errors.append( e ); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | if ( ! errors.isEmpty() ) { |
---|
| 160 | throw errors; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
[9e1d485] | 164 | /// Base class for the smart pointer types |
---|
| 165 | /// should never really be used. |
---|
| 166 | template< typename node_t, enum Node::ref_type ref_t > |
---|
[f47f887] | 167 | class ptr_base { |
---|
| 168 | public: |
---|
| 169 | ptr_base() : node(nullptr) {} |
---|
[87701b6] | 170 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
[dafe9e1] | 171 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
[f47f887] | 172 | |
---|
[f685679] | 173 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
| 174 | if( node ) _inc(node); |
---|
| 175 | } |
---|
| 176 | |
---|
[d76c588] | 177 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
[f685679] | 178 | |
---|
[9e1d485] | 179 | template< enum Node::ref_type o_ref_t > |
---|
[60aaa51d] | 180 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { |
---|
[87701b6] | 181 | if( node ) _inc(node); |
---|
[6a625de] | 182 | } |
---|
| 183 | |
---|
[9e1d485] | 184 | template< enum Node::ref_type o_ref_t > |
---|
[60aaa51d] | 185 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { |
---|
[87701b6] | 186 | if( node ) _inc(node); |
---|
[6d51bd7] | 187 | } |
---|
| 188 | |
---|
| 189 | template<typename o_node_t> |
---|
| 190 | ptr_base & operator=( const o_node_t * node ) { |
---|
[dafe9e1] | 191 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
[6d51bd7] | 192 | return *this; |
---|
[f47f887] | 193 | } |
---|
[cedb545] | 194 | |
---|
[b8524ca] | 195 | ptr_base & operator=( std::nullptr_t ) { |
---|
| 196 | if ( node ) _dec(node); |
---|
| 197 | node = nullptr; |
---|
| 198 | return *this; |
---|
| 199 | } |
---|
| 200 | |
---|
[f685679] | 201 | ptr_base & operator=( const ptr_base & o ) { |
---|
| 202 | assign(o.node); |
---|
| 203 | return *this; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | ptr_base & operator=( ptr_base && o ) { |
---|
[d76c588] | 207 | if ( node == o.node ) return *this; |
---|
| 208 | if ( node ) _dec(node); |
---|
| 209 | node = o.node; |
---|
| 210 | o.node = nullptr; |
---|
[f685679] | 211 | return *this; |
---|
| 212 | } |
---|
| 213 | |
---|
[9e1d485] | 214 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 215 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
[60aaa51d] | 216 | assign(o.get()); |
---|
[f47f887] | 217 | return *this; |
---|
| 218 | } |
---|
[cedb545] | 219 | |
---|
[9e1d485] | 220 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 221 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
[60aaa51d] | 222 | assign(o.get()); |
---|
[f47f887] | 223 | return *this; |
---|
| 224 | } |
---|
[cedb545] | 225 | |
---|
[4864a73] | 226 | const node_t * get() const { _check(); return node; } |
---|
| 227 | const node_t * operator->() const { _check(); return node; } |
---|
| 228 | const node_t & operator* () const { _check(); return *node; } |
---|
| 229 | explicit operator bool() const { _check(); return node; } |
---|
| 230 | operator const node_t * () const { _check(); return node; } |
---|
[cedb545] | 231 | |
---|
[7d0881c] | 232 | const node_t * release() { |
---|
| 233 | const node_t * ret = node; |
---|
| 234 | if ( node ) { |
---|
| 235 | _dec(node, false); |
---|
| 236 | node = nullptr; |
---|
| 237 | } |
---|
| 238 | return ret; |
---|
| 239 | } |
---|
| 240 | |
---|
[54e41b3] | 241 | /// wrapper for convenient access to dynamic_cast |
---|
[fdbd4fd] | 242 | template<typename o_node_t> |
---|
[4864a73] | 243 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } |
---|
[fdbd4fd] | 244 | |
---|
[ee574a2] | 245 | /// wrapper for convenient access to strict_dynamic_cast |
---|
| 246 | template<typename o_node_t> |
---|
[546e712] | 247 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } |
---|
[ee574a2] | 248 | |
---|
[76ed81f] | 249 | /// Returns a mutable version of the pointer in this node. |
---|
| 250 | node_t * get_and_mutate(); |
---|
| 251 | |
---|
[54e41b3] | 252 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
| 253 | /// Returns a mutable version of the pointer in this node. |
---|
[10a1225] | 254 | node_t * set_and_mutate( const node_t * n ); |
---|
[54e41b3] | 255 | |
---|
[f47f887] | 256 | using ptr = const node_t *; |
---|
[cedb545] | 257 | |
---|
[f47f887] | 258 | private: |
---|
[9e1d485] | 259 | void assign( const node_t * other ) { |
---|
[87701b6] | 260 | if( other ) _inc(other); |
---|
| 261 | if( node ) _dec(node ); |
---|
[f47f887] | 262 | node = other; |
---|
| 263 | } |
---|
[cedb545] | 264 | |
---|
[87701b6] | 265 | void _inc( const node_t * other ); |
---|
[7d0881c] | 266 | void _dec( const node_t * other, bool do_delete = true ); |
---|
[4864a73] | 267 | void _check() const; |
---|
[87701b6] | 268 | |
---|
[9e1d485] | 269 | const node_t * node; |
---|
[f47f887] | 270 | }; |
---|
[cedb545] | 271 | |
---|
[2bb4a01] | 272 | /// Owning pointer to node |
---|
[f47f887] | 273 | template< typename node_t > |
---|
| 274 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
[cedb545] | 275 | |
---|
[2bb4a01] | 276 | /// Observing pointer to node |
---|
[f47f887] | 277 | template< typename node_t > |
---|
| 278 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
[2bb4a01] | 279 | } |
---|
| 280 | |
---|
| 281 | // Local Variables: // |
---|
| 282 | // tab-width: 4 // |
---|
| 283 | // mode: c++ // |
---|
| 284 | // compile-command: "make install" // |
---|
[1e97287] | 285 | // End: // |
---|