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