| 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 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Mon Jun  3 13:26:00 2019 | 
|---|
| 13 | // Update Count     : 5 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <cassert> | 
|---|
| 19 | #include <cstddef>     // for nullptr_t | 
|---|
| 20 | #include <iosfwd> | 
|---|
| 21 | #include <type_traits> // for remove_reference | 
|---|
| 22 |  | 
|---|
| 23 | #include "Common/ErrorObjects.h"  // for SemanticErrorException | 
|---|
| 24 |  | 
|---|
| 25 | namespace ast { | 
|---|
| 26 |  | 
|---|
| 27 | class Visitor; | 
|---|
| 28 |  | 
|---|
| 29 | /// Base class for all AST nodes. | 
|---|
| 30 | /// Keeps both strong and weak reference counts. | 
|---|
| 31 | class Node { | 
|---|
| 32 | public: | 
|---|
| 33 | // override defaults to ensure assignment doesn't | 
|---|
| 34 | // change/share reference counts | 
|---|
| 35 | Node() = default; | 
|---|
| 36 | Node(const Node&) : strong_count(0), weak_count(0) {} | 
|---|
| 37 | Node(Node&&) : strong_count(0), weak_count(0) {} | 
|---|
| 38 | Node& operator= (const Node&) = delete; | 
|---|
| 39 | Node& operator= (Node&&) = delete; | 
|---|
| 40 | virtual ~Node() = default; | 
|---|
| 41 |  | 
|---|
| 42 | virtual const Node * accept( Visitor & v ) const = 0; | 
|---|
| 43 |  | 
|---|
| 44 | /// Types of node references | 
|---|
| 45 | enum class ref_type { | 
|---|
| 46 | strong, | 
|---|
| 47 | weak | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | bool unique() const { return strong_count == 1; } | 
|---|
| 51 |  | 
|---|
| 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; | 
|---|
| 62 | mutable bool was_ever_strong = false; | 
|---|
| 63 |  | 
|---|
| 64 | void increment(ref_type ref) const { | 
|---|
| 65 | switch (ref) { | 
|---|
| 66 | case ref_type::strong: strong_count++; was_ever_strong = true; break; | 
|---|
| 67 | case ref_type::weak  : weak_count  ++; break; | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void decrement(ast::Node::ref_type ref) const { | 
|---|
| 72 | switch (ref) { | 
|---|
| 73 | case ref_type::strong: strong_count--; break; | 
|---|
| 74 | case ref_type::weak  : weak_count  --; break; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | if(!strong_count && !weak_count) { | 
|---|
| 78 | delete this; | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | template< typename node_t, enum Node::ref_type ref_t > | 
|---|
| 83 | friend class ptr_base; | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | /// Mutate a node, non-member function to avoid static type | 
|---|
| 87 | /// problems and be able to use auto return | 
|---|
| 88 | template<typename node_t> | 
|---|
| 89 | node_t * mutate( const node_t * node ) { | 
|---|
| 90 | if (node->strong_count <= 1) { | 
|---|
| 91 | return const_cast<node_t *>(node); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 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 |  | 
|---|
| 101 | /// Mutate a node field (only clones if not equal to existing value) | 
|---|
| 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 ) { | 
|---|
| 104 | // skip mutate if equivalent | 
|---|
| 105 | if ( node->*field == val ) return node; | 
|---|
| 106 |  | 
|---|
| 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) | 
|---|
| 114 | template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t> | 
|---|
| 115 | const node_t * mutate_field_index( | 
|---|
| 116 | const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val | 
|---|
| 117 | ) { | 
|---|
| 118 | // skip mutate if equivalent | 
|---|
| 119 | if  ( (node->*field)[i] == val ) return node; | 
|---|
| 120 |  | 
|---|
| 121 | // mutate and return | 
|---|
| 122 | node_t * ret = mutate( node ); | 
|---|
| 123 | (ret->*field)[i] = std::forward< field_t >( val ); | 
|---|
| 124 | return ret; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | std::ostream& operator<< ( std::ostream& out, const Node * node ); | 
|---|
| 128 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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 > | 
|---|
| 156 | class ptr_base { | 
|---|
| 157 | public: | 
|---|
| 158 | ptr_base() : node(nullptr) {} | 
|---|
| 159 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } | 
|---|
| 160 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } | 
|---|
| 161 |  | 
|---|
| 162 | ptr_base( const ptr_base & o ) : node(o.node) { | 
|---|
| 163 | if( node ) _inc(node); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } | 
|---|
| 167 |  | 
|---|
| 168 | template< enum Node::ref_type o_ref_t > | 
|---|
| 169 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { | 
|---|
| 170 | if( node ) _inc(node); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | template< enum Node::ref_type o_ref_t > | 
|---|
| 174 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { | 
|---|
| 175 | if( node ) _inc(node); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | template<typename o_node_t> | 
|---|
| 179 | ptr_base & operator=( const o_node_t * node ) { | 
|---|
| 180 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); | 
|---|
| 181 | return *this; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | ptr_base & operator=( std::nullptr_t ) { | 
|---|
| 185 | if ( node ) _dec(node); | 
|---|
| 186 | node = nullptr; | 
|---|
| 187 | return *this; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | ptr_base & operator=( const ptr_base & o ) { | 
|---|
| 191 | assign(o.node); | 
|---|
| 192 | return *this; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | ptr_base & operator=( ptr_base && o ) { | 
|---|
| 196 | if ( node == o.node ) return *this; | 
|---|
| 197 | if ( node ) _dec(node); | 
|---|
| 198 | node = o.node; | 
|---|
| 199 | o.node = nullptr; | 
|---|
| 200 | return *this; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | template< enum Node::ref_type o_ref_t > | 
|---|
| 204 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { | 
|---|
| 205 | assign(o.get()); | 
|---|
| 206 | return *this; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | template< enum Node::ref_type o_ref_t > | 
|---|
| 210 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { | 
|---|
| 211 | assign(o.get()); | 
|---|
| 212 | return *this; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 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; } | 
|---|
| 220 |  | 
|---|
| 221 | /// wrapper for convenient access to dynamic_cast | 
|---|
| 222 | template<typename o_node_t> | 
|---|
| 223 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } | 
|---|
| 224 |  | 
|---|
| 225 | /// wrapper for convenient access to strict_dynamic_cast | 
|---|
| 226 | template<typename o_node_t> | 
|---|
| 227 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } | 
|---|
| 228 |  | 
|---|
| 229 | /// Returns a mutable version of the pointer in this node. | 
|---|
| 230 | node_t * get_and_mutate(); | 
|---|
| 231 |  | 
|---|
| 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. | 
|---|
| 234 | node_t * set_and_mutate( const node_t * n ); | 
|---|
| 235 |  | 
|---|
| 236 | using ptr = const node_t *; | 
|---|
| 237 |  | 
|---|
| 238 | private: | 
|---|
| 239 | void assign( const node_t * other ) { | 
|---|
| 240 | if( other ) _inc(other); | 
|---|
| 241 | if( node  ) _dec(node ); | 
|---|
| 242 | node = other; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | void _inc( const node_t * other ); | 
|---|
| 246 | void _dec( const node_t * other ); | 
|---|
| 247 | void _check() const; | 
|---|
| 248 |  | 
|---|
| 249 | const node_t * node; | 
|---|
| 250 | }; | 
|---|
| 251 |  | 
|---|
| 252 | /// Owning pointer to node | 
|---|
| 253 | template< typename node_t > | 
|---|
| 254 | using ptr = ptr_base< node_t, Node::ref_type::strong >; | 
|---|
| 255 |  | 
|---|
| 256 | /// Observing pointer to node | 
|---|
| 257 | template< typename node_t > | 
|---|
| 258 | using readonly = ptr_base< node_t, Node::ref_type::weak >; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | // Local Variables: // | 
|---|
| 262 | // tab-width: 4 // | 
|---|
| 263 | // mode: c++ // | 
|---|
| 264 | // compile-command: "make install" // | 
|---|
| 265 | // End: // | 
|---|