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