[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 |
---|
| 35 | Node() = default; |
---|
[6d51bd7] | 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; |
---|
[f47f887] | 40 | virtual ~Node() = default; |
---|
[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); |
---|
| 59 | |
---|
| 60 | mutable size_t strong_count = 0; |
---|
| 61 | mutable size_t weak_count = 0; |
---|
[4864a73] | 62 | mutable bool was_ever_strong = false; |
---|
[87701b6] | 63 | |
---|
| 64 | void increment(ref_type ref) const { |
---|
[f47f887] | 65 | switch (ref) { |
---|
[4864a73] | 66 | case ref_type::strong: strong_count++; was_ever_strong = true; break; |
---|
[6d51bd7] | 67 | case ref_type::weak : weak_count ++; break; |
---|
[cedb545] | 68 | } |
---|
[f47f887] | 69 | } |
---|
[cedb545] | 70 | |
---|
[87701b6] | 71 | void decrement(ast::Node::ref_type ref) const { |
---|
[f47f887] | 72 | switch (ref) { |
---|
[6d51bd7] | 73 | case ref_type::strong: strong_count--; break; |
---|
| 74 | case ref_type::weak : weak_count --; break; |
---|
[cedb545] | 75 | } |
---|
| 76 | |
---|
[6d51bd7] | 77 | if(!strong_count && !weak_count) { |
---|
[f47f887] | 78 | delete this; |
---|
[6a625de] | 79 | } |
---|
[f47f887] | 80 | } |
---|
[6a625de] | 81 | |
---|
[87701b6] | 82 | template< typename node_t, enum Node::ref_type ref_t > |
---|
| 83 | friend class ptr_base; |
---|
[f47f887] | 84 | }; |
---|
| 85 | |
---|
[b7d92b96] | 86 | /// Mutate a node, non-member function to avoid static type |
---|
| 87 | /// problems and be able to use auto return |
---|
[f47f887] | 88 | template<typename node_t> |
---|
[87701b6] | 89 | node_t * mutate( const node_t * node ) { |
---|
| 90 | if (node->strong_count <= 1) { |
---|
[f47f887] | 91 | return const_cast<node_t *>(node); |
---|
[6a625de] | 92 | } |
---|
| 93 | |
---|
[f47f887] | 94 | assertf( |
---|
| 95 | node->weak_count == 0, |
---|
| 96 | "Error: mutating node with weak references to it will invalided some references" |
---|
| 97 | ); |
---|
| 98 | return node->clone(); |
---|
| 99 | } |
---|
| 100 | |
---|
[b7d92b96] | 101 | /// Mutate a node field (only clones if not equal to existing value) |
---|
[18e683b] | 102 | template<typename node_t, typename parent_t, typename field_t, typename assn_t> |
---|
| 103 | const node_t * mutate_field( const node_t * node, field_t parent_t::* field, assn_t && val ) { |
---|
[2d11663] | 104 | // skip mutate if equivalent |
---|
[b7d92b96] | 105 | if ( node->*field == val ) return node; |
---|
| 106 | |
---|
[2d11663] | 107 | // mutate and return |
---|
| 108 | node_t * ret = mutate( node ); |
---|
| 109 | ret->*field = std::forward< assn_t >( val ); |
---|
| 110 | return ret; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /// Mutate a single index of a node field (only clones if not equal to existing value) |
---|
[18e683b] | 114 | template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t> |
---|
[2d11663] | 115 | const node_t * mutate_field_index( |
---|
[18e683b] | 116 | const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val |
---|
[2d11663] | 117 | ) { |
---|
| 118 | // skip mutate if equivalent |
---|
| 119 | if ( (node->*field)[i] == val ) return node; |
---|
| 120 | |
---|
| 121 | // mutate and return |
---|
[b7d92b96] | 122 | node_t * ret = mutate( node ); |
---|
[2d11663] | 123 | (ret->*field)[i] = std::forward< field_t >( val ); |
---|
[b7d92b96] | 124 | return ret; |
---|
| 125 | } |
---|
| 126 | |
---|
[54e41b3] | 127 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
[2bb4a01] | 128 | |
---|
[d8938622] | 129 | /// Call a visitor on a possibly-null node |
---|
| 130 | template<typename node_t> |
---|
| 131 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
| 132 | return n ? n->accept( v ) : nullptr; |
---|
| 133 | } |
---|
| 134 | |
---|
[1346914] | 135 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed |
---|
| 136 | template< typename Container > |
---|
| 137 | void accept_each( const Container & c, Visitor & v ) { |
---|
| 138 | SemanticErrorException errors; |
---|
| 139 | for ( const auto & i : c ) { |
---|
| 140 | try { |
---|
| 141 | if ( i ) { |
---|
| 142 | i->accept( v ); |
---|
| 143 | } |
---|
| 144 | } catch ( SemanticErrorException & e ) { |
---|
| 145 | errors.append( e ); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | if ( ! errors.isEmpty() ) { |
---|
| 149 | throw errors; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
[9e1d485] | 153 | /// Base class for the smart pointer types |
---|
| 154 | /// should never really be used. |
---|
| 155 | template< typename node_t, enum Node::ref_type ref_t > |
---|
[f47f887] | 156 | class ptr_base { |
---|
| 157 | public: |
---|
| 158 | ptr_base() : node(nullptr) {} |
---|
[87701b6] | 159 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
[dafe9e1] | 160 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
[f47f887] | 161 | |
---|
[f685679] | 162 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
| 163 | if( node ) _inc(node); |
---|
| 164 | } |
---|
| 165 | |
---|
[d76c588] | 166 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
[f685679] | 167 | |
---|
[9e1d485] | 168 | template< enum Node::ref_type o_ref_t > |
---|
[60aaa51d] | 169 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { |
---|
[87701b6] | 170 | if( node ) _inc(node); |
---|
[6a625de] | 171 | } |
---|
| 172 | |
---|
[9e1d485] | 173 | template< enum Node::ref_type o_ref_t > |
---|
[60aaa51d] | 174 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { |
---|
[87701b6] | 175 | if( node ) _inc(node); |
---|
[6d51bd7] | 176 | } |
---|
| 177 | |
---|
| 178 | template<typename o_node_t> |
---|
| 179 | ptr_base & operator=( const o_node_t * node ) { |
---|
[dafe9e1] | 180 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
[6d51bd7] | 181 | return *this; |
---|
[f47f887] | 182 | } |
---|
[cedb545] | 183 | |
---|
[b8524ca] | 184 | ptr_base & operator=( std::nullptr_t ) { |
---|
| 185 | if ( node ) _dec(node); |
---|
| 186 | node = nullptr; |
---|
| 187 | return *this; |
---|
| 188 | } |
---|
| 189 | |
---|
[f685679] | 190 | ptr_base & operator=( const ptr_base & o ) { |
---|
| 191 | assign(o.node); |
---|
| 192 | return *this; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | ptr_base & operator=( ptr_base && o ) { |
---|
[d76c588] | 196 | if ( node == o.node ) return *this; |
---|
| 197 | if ( node ) _dec(node); |
---|
| 198 | node = o.node; |
---|
| 199 | o.node = nullptr; |
---|
[f685679] | 200 | return *this; |
---|
| 201 | } |
---|
| 202 | |
---|
[9e1d485] | 203 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 204 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
[60aaa51d] | 205 | assign(o.get()); |
---|
[f47f887] | 206 | return *this; |
---|
| 207 | } |
---|
[cedb545] | 208 | |
---|
[9e1d485] | 209 | template< enum Node::ref_type o_ref_t > |
---|
[f47f887] | 210 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
[60aaa51d] | 211 | assign(o.get()); |
---|
[f47f887] | 212 | return *this; |
---|
| 213 | } |
---|
[cedb545] | 214 | |
---|
[4864a73] | 215 | const node_t * get() const { _check(); return node; } |
---|
| 216 | const node_t * operator->() const { _check(); return node; } |
---|
| 217 | const node_t & operator* () const { _check(); return *node; } |
---|
| 218 | explicit operator bool() const { _check(); return node; } |
---|
| 219 | operator const node_t * () const { _check(); return node; } |
---|
[cedb545] | 220 | |
---|
[54e41b3] | 221 | /// wrapper for convenient access to dynamic_cast |
---|
[fdbd4fd] | 222 | template<typename o_node_t> |
---|
[4864a73] | 223 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } |
---|
[fdbd4fd] | 224 | |
---|
[ee574a2] | 225 | /// wrapper for convenient access to strict_dynamic_cast |
---|
| 226 | template<typename o_node_t> |
---|
[546e712] | 227 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } |
---|
[ee574a2] | 228 | |
---|
[76ed81f] | 229 | /// Returns a mutable version of the pointer in this node. |
---|
| 230 | node_t * get_and_mutate(); |
---|
| 231 | |
---|
[54e41b3] | 232 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
| 233 | /// Returns a mutable version of the pointer in this node. |
---|
[10a1225] | 234 | node_t * set_and_mutate( const node_t * n ); |
---|
[54e41b3] | 235 | |
---|
[f47f887] | 236 | using ptr = const node_t *; |
---|
[cedb545] | 237 | |
---|
[f47f887] | 238 | private: |
---|
[9e1d485] | 239 | void assign( const node_t * other ) { |
---|
[87701b6] | 240 | if( other ) _inc(other); |
---|
| 241 | if( node ) _dec(node ); |
---|
[f47f887] | 242 | node = other; |
---|
| 243 | } |
---|
[cedb545] | 244 | |
---|
[87701b6] | 245 | void _inc( const node_t * other ); |
---|
| 246 | void _dec( const node_t * other ); |
---|
[4864a73] | 247 | void _check() const; |
---|
[87701b6] | 248 | |
---|
[9e1d485] | 249 | const node_t * node; |
---|
[f47f887] | 250 | }; |
---|
[cedb545] | 251 | |
---|
[2bb4a01] | 252 | /// Owning pointer to node |
---|
[f47f887] | 253 | template< typename node_t > |
---|
| 254 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
[cedb545] | 255 | |
---|
[2bb4a01] | 256 | /// Observing pointer to node |
---|
[f47f887] | 257 | template< typename node_t > |
---|
| 258 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
[2bb4a01] | 259 | } |
---|
| 260 | |
---|
| 261 | // Local Variables: // |
---|
| 262 | // tab-width: 4 // |
---|
| 263 | // mode: c++ // |
---|
| 264 | // compile-command: "make install" // |
---|
[1e97287] | 265 | // End: // |
---|