| [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 | 
|---|
| [52a4d69] | 12 | // Last Modified On : Fri Jun 5 9:47:00 2020 | 
|---|
|  | 13 | // Update Count     : 6 | 
|---|
| [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; } | 
|---|
| [490fb92e] | 51 | bool isManaged() const {return strong_count > 0; } | 
|---|
| [4864a73] | 52 |  | 
|---|
| [87701b6] | 53 | private: | 
|---|
|  | 54 | /// Make a copy of this node; should be overridden in subclass with more precise return type | 
|---|
|  | 55 | virtual Node * clone() const = 0; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | /// Must be copied in ALL derived classes | 
|---|
|  | 58 | template<typename node_t> | 
|---|
|  | 59 | friend node_t * mutate(const node_t * node); | 
|---|
| [99da267] | 60 | template<typename node_t> | 
|---|
|  | 61 | friend node_t * shallowCopy(const node_t * node); | 
|---|
| [87701b6] | 62 |  | 
|---|
|  | 63 | mutable size_t strong_count = 0; | 
|---|
|  | 64 | mutable size_t weak_count = 0; | 
|---|
| [4864a73] | 65 | mutable bool was_ever_strong = false; | 
|---|
| [87701b6] | 66 |  | 
|---|
|  | 67 | void increment(ref_type ref) const { | 
|---|
| [f47f887] | 68 | switch (ref) { | 
|---|
| [4864a73] | 69 | case ref_type::strong: strong_count++; was_ever_strong = true; break; | 
|---|
| [6d51bd7] | 70 | case ref_type::weak  : weak_count  ++; break; | 
|---|
| [cedb545] | 71 | } | 
|---|
| [f47f887] | 72 | } | 
|---|
| [cedb545] | 73 |  | 
|---|
| [7d0881c] | 74 | void decrement(ast::Node::ref_type ref, bool do_delete = true) const { | 
|---|
| [f47f887] | 75 | switch (ref) { | 
|---|
| [6d51bd7] | 76 | case ref_type::strong: strong_count--; break; | 
|---|
|  | 77 | case ref_type::weak  : weak_count  --; break; | 
|---|
| [cedb545] | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [7d0881c] | 80 | if( do_delete && !strong_count && !weak_count) { | 
|---|
| [f47f887] | 81 | delete this; | 
|---|
| [6a625de] | 82 | } | 
|---|
| [f47f887] | 83 | } | 
|---|
| [6a625de] | 84 |  | 
|---|
| [87701b6] | 85 | template< typename node_t, enum Node::ref_type ref_t > | 
|---|
|  | 86 | friend class ptr_base; | 
|---|
| [f47f887] | 87 | }; | 
|---|
|  | 88 |  | 
|---|
| [b7d92b96] | 89 | /// Mutate a node, non-member function to avoid static type | 
|---|
|  | 90 | /// problems and be able to use auto return | 
|---|
| [f47f887] | 91 | template<typename node_t> | 
|---|
| [87701b6] | 92 | node_t * mutate( const node_t * node ) { | 
|---|
|  | 93 | if (node->strong_count <= 1) { | 
|---|
| [f47f887] | 94 | return const_cast<node_t *>(node); | 
|---|
| [6a625de] | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [f47f887] | 97 | assertf( | 
|---|
|  | 98 | node->weak_count == 0, | 
|---|
| [2890212] | 99 | "Error: mutating node with weak references to it will invalidate some references" | 
|---|
| [f47f887] | 100 | ); | 
|---|
|  | 101 | return node->clone(); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [b7d92b96] | 104 | /// Mutate a node field (only clones if not equal to existing value) | 
|---|
| [18e683b] | 105 | template<typename node_t, typename parent_t, typename field_t, typename assn_t> | 
|---|
|  | 106 | const node_t * mutate_field( const node_t * node, field_t parent_t::* field, assn_t && val ) { | 
|---|
| [2d11663] | 107 | // skip mutate if equivalent | 
|---|
| [b7d92b96] | 108 | if ( node->*field == val ) return node; | 
|---|
| [2890212] | 109 |  | 
|---|
| [2d11663] | 110 | // mutate and return | 
|---|
|  | 111 | node_t * ret = mutate( node ); | 
|---|
|  | 112 | ret->*field = std::forward< assn_t >( val ); | 
|---|
|  | 113 | return ret; | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | /// Mutate a single index of a node field (only clones if not equal to existing value) | 
|---|
| [18e683b] | 117 | template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t> | 
|---|
| [2d11663] | 118 | const node_t * mutate_field_index( | 
|---|
| [18e683b] | 119 | const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val | 
|---|
| [2d11663] | 120 | ) { | 
|---|
|  | 121 | // skip mutate if equivalent | 
|---|
|  | 122 | if  ( (node->*field)[i] == val ) return node; | 
|---|
|  | 123 |  | 
|---|
|  | 124 | // mutate and return | 
|---|
| [b7d92b96] | 125 | node_t * ret = mutate( node ); | 
|---|
| [2d11663] | 126 | (ret->*field)[i] = std::forward< field_t >( val ); | 
|---|
| [b7d92b96] | 127 | return ret; | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [e0e9a0b] | 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; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [54e41b3] | 139 | std::ostream& operator<< ( std::ostream& out, const Node * node ); | 
|---|
| [2bb4a01] | 140 |  | 
|---|
| [d8938622] | 141 | /// Call a visitor on a possibly-null node | 
|---|
|  | 142 | template<typename node_t> | 
|---|
|  | 143 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { | 
|---|
|  | 144 | return n ? n->accept( v ) : nullptr; | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
| [1346914] | 147 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed | 
|---|
|  | 148 | template< typename Container > | 
|---|
|  | 149 | void accept_each( const Container & c, Visitor & v ) { | 
|---|
|  | 150 | SemanticErrorException errors; | 
|---|
|  | 151 | for ( const auto & i : c ) { | 
|---|
|  | 152 | try { | 
|---|
|  | 153 | if ( i ) { | 
|---|
|  | 154 | i->accept( v ); | 
|---|
|  | 155 | } | 
|---|
|  | 156 | } catch ( SemanticErrorException & e ) { | 
|---|
|  | 157 | errors.append( e ); | 
|---|
|  | 158 | } | 
|---|
|  | 159 | } | 
|---|
|  | 160 | if ( ! errors.isEmpty() ) { | 
|---|
|  | 161 | throw errors; | 
|---|
|  | 162 | } | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
| [9e1d485] | 165 | /// Base class for the smart pointer types | 
|---|
|  | 166 | /// should never really be used. | 
|---|
|  | 167 | template< typename node_t, enum Node::ref_type ref_t > | 
|---|
| [f47f887] | 168 | class ptr_base { | 
|---|
|  | 169 | public: | 
|---|
|  | 170 | ptr_base() : node(nullptr) {} | 
|---|
| [87701b6] | 171 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } | 
|---|
| [dafe9e1] | 172 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } | 
|---|
| [f47f887] | 173 |  | 
|---|
| [f685679] | 174 | ptr_base( const ptr_base & o ) : node(o.node) { | 
|---|
|  | 175 | if( node ) _inc(node); | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
| [d76c588] | 178 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } | 
|---|
| [f685679] | 179 |  | 
|---|
| [9e1d485] | 180 | template< enum Node::ref_type o_ref_t > | 
|---|
| [60aaa51d] | 181 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { | 
|---|
| [87701b6] | 182 | if( node ) _inc(node); | 
|---|
| [6a625de] | 183 | } | 
|---|
|  | 184 |  | 
|---|
| [9e1d485] | 185 | template< enum Node::ref_type o_ref_t > | 
|---|
| [60aaa51d] | 186 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { | 
|---|
| [87701b6] | 187 | if( node ) _inc(node); | 
|---|
| [6d51bd7] | 188 | } | 
|---|
|  | 189 |  | 
|---|
|  | 190 | template<typename o_node_t> | 
|---|
|  | 191 | ptr_base & operator=( const o_node_t * node ) { | 
|---|
| [dafe9e1] | 192 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); | 
|---|
| [6d51bd7] | 193 | return *this; | 
|---|
| [f47f887] | 194 | } | 
|---|
| [cedb545] | 195 |  | 
|---|
| [b8524ca] | 196 | ptr_base & operator=( std::nullptr_t ) { | 
|---|
|  | 197 | if ( node ) _dec(node); | 
|---|
|  | 198 | node = nullptr; | 
|---|
|  | 199 | return *this; | 
|---|
|  | 200 | } | 
|---|
|  | 201 |  | 
|---|
| [f685679] | 202 | ptr_base & operator=( const ptr_base & o ) { | 
|---|
|  | 203 | assign(o.node); | 
|---|
|  | 204 | return *this; | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | ptr_base & operator=( ptr_base && o ) { | 
|---|
| [d76c588] | 208 | if ( node == o.node ) return *this; | 
|---|
|  | 209 | if ( node ) _dec(node); | 
|---|
|  | 210 | node = o.node; | 
|---|
|  | 211 | o.node = nullptr; | 
|---|
| [f685679] | 212 | return *this; | 
|---|
|  | 213 | } | 
|---|
|  | 214 |  | 
|---|
| [9e1d485] | 215 | template< enum Node::ref_type o_ref_t > | 
|---|
| [f47f887] | 216 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { | 
|---|
| [60aaa51d] | 217 | assign(o.get()); | 
|---|
| [f47f887] | 218 | return *this; | 
|---|
|  | 219 | } | 
|---|
| [cedb545] | 220 |  | 
|---|
| [9e1d485] | 221 | template< enum Node::ref_type o_ref_t > | 
|---|
| [f47f887] | 222 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { | 
|---|
| [60aaa51d] | 223 | assign(o.get()); | 
|---|
| [f47f887] | 224 | return *this; | 
|---|
|  | 225 | } | 
|---|
| [cedb545] | 226 |  | 
|---|
| [4864a73] | 227 | const node_t * get() const { _check(); return  node; } | 
|---|
|  | 228 | const node_t * operator->() const { _check(); return  node; } | 
|---|
|  | 229 | const node_t & operator* () const { _check(); return *node; } | 
|---|
|  | 230 | explicit operator bool() const { _check(); return node; } | 
|---|
| [302ef2a] | 231 | operator const node_t * () const & { _check(); return node; } | 
|---|
|  | 232 | operator const node_t * () && = delete; | 
|---|
| [cedb545] | 233 |  | 
|---|
| [7d0881c] | 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 | } | 
|---|
|  | 242 |  | 
|---|
| [54e41b3] | 243 | /// wrapper for convenient access to dynamic_cast | 
|---|
| [fdbd4fd] | 244 | template<typename o_node_t> | 
|---|
| [4864a73] | 245 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } | 
|---|
| [fdbd4fd] | 246 |  | 
|---|
| [52a4d69] | 247 | /// Wrapper that makes sure dynamic_cast returns non-null. | 
|---|
| [ee574a2] | 248 | template<typename o_node_t> | 
|---|
| [52a4d69] | 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; } | 
|---|
| [ee574a2] | 257 |  | 
|---|
| [76ed81f] | 258 | /// Returns a mutable version of the pointer in this node. | 
|---|
|  | 259 | node_t * get_and_mutate(); | 
|---|
|  | 260 |  | 
|---|
| [54e41b3] | 261 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. | 
|---|
|  | 262 | /// Returns a mutable version of the pointer in this node. | 
|---|
| [10a1225] | 263 | node_t * set_and_mutate( const node_t * n ); | 
|---|
| [54e41b3] | 264 |  | 
|---|
| [f47f887] | 265 | using ptr = const node_t *; | 
|---|
| [cedb545] | 266 |  | 
|---|
| [f47f887] | 267 | private: | 
|---|
| [9e1d485] | 268 | void assign( const node_t * other ) { | 
|---|
| [87701b6] | 269 | if( other ) _inc(other); | 
|---|
|  | 270 | if( node  ) _dec(node ); | 
|---|
| [f47f887] | 271 | node = other; | 
|---|
|  | 272 | } | 
|---|
| [cedb545] | 273 |  | 
|---|
| [87701b6] | 274 | void _inc( const node_t * other ); | 
|---|
| [7d0881c] | 275 | void _dec( const node_t * other, bool do_delete = true ); | 
|---|
| [4864a73] | 276 | void _check() const; | 
|---|
| [52a4d69] | 277 | void _strict_fail() const __attribute__((noreturn)); | 
|---|
| [87701b6] | 278 |  | 
|---|
| [9e1d485] | 279 | const node_t * node; | 
|---|
| [f47f887] | 280 | }; | 
|---|
| [cedb545] | 281 |  | 
|---|
| [2bb4a01] | 282 | /// Owning pointer to node | 
|---|
| [f47f887] | 283 | template< typename node_t > | 
|---|
|  | 284 | using ptr = ptr_base< node_t, Node::ref_type::strong >; | 
|---|
| [cedb545] | 285 |  | 
|---|
| [2bb4a01] | 286 | /// Observing pointer to node | 
|---|
| [f47f887] | 287 | template< typename node_t > | 
|---|
|  | 288 | using readonly = ptr_base< node_t, Node::ref_type::weak >; | 
|---|
| [2bb4a01] | 289 | } | 
|---|
|  | 290 |  | 
|---|
|  | 291 | // Local Variables: // | 
|---|
|  | 292 | // tab-width: 4 // | 
|---|
|  | 293 | // mode: c++ // | 
|---|
|  | 294 | // compile-command: "make install" // | 
|---|
| [1e97287] | 295 | // End: // | 
|---|