[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)
|
---|
| 101 | template<typename node_t, typename field_t>
|
---|
| 102 | const node_t * mutate_field(
|
---|
| 103 | const node_t * node,
|
---|
| 104 | typename std::remove_const<typename std::remove_reference<field_t>::type>::type node_t::* field,
|
---|
| 105 | field_t&& val
|
---|
| 106 | ) {
|
---|
| 107 | if ( node->*field == val ) return node;
|
---|
| 108 |
|
---|
| 109 | node_t * ret = mutate( node );
|
---|
| 110 | ret->*field = std::forward< field_t >( val );
|
---|
| 111 | return ret;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[54e41b3] | 114 | std::ostream& operator<< ( std::ostream& out, const Node * node );
|
---|
[2bb4a01] | 115 |
|
---|
[d8938622] | 116 | /// Call a visitor on a possibly-null node
|
---|
| 117 | template<typename node_t>
|
---|
| 118 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) {
|
---|
| 119 | return n ? n->accept( v ) : nullptr;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[1346914] | 122 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed
|
---|
| 123 | template< typename Container >
|
---|
| 124 | void accept_each( const Container & c, Visitor & v ) {
|
---|
| 125 | SemanticErrorException errors;
|
---|
| 126 | for ( const auto & i : c ) {
|
---|
| 127 | try {
|
---|
| 128 | if ( i ) {
|
---|
| 129 | i->accept( v );
|
---|
| 130 | }
|
---|
| 131 | } catch ( SemanticErrorException & e ) {
|
---|
| 132 | errors.append( e );
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | if ( ! errors.isEmpty() ) {
|
---|
| 136 | throw errors;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[9e1d485] | 140 | /// Base class for the smart pointer types
|
---|
| 141 | /// should never really be used.
|
---|
| 142 | template< typename node_t, enum Node::ref_type ref_t >
|
---|
[f47f887] | 143 | class ptr_base {
|
---|
| 144 | public:
|
---|
| 145 | ptr_base() : node(nullptr) {}
|
---|
[87701b6] | 146 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
|
---|
[dafe9e1] | 147 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
|
---|
[f47f887] | 148 |
|
---|
[f685679] | 149 | ptr_base( const ptr_base & o ) : node(o.node) {
|
---|
| 150 | if( node ) _inc(node);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[d76c588] | 153 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
|
---|
[f685679] | 154 |
|
---|
[9e1d485] | 155 | template< enum Node::ref_type o_ref_t >
|
---|
[60aaa51d] | 156 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
|
---|
[87701b6] | 157 | if( node ) _inc(node);
|
---|
[6a625de] | 158 | }
|
---|
| 159 |
|
---|
[9e1d485] | 160 | template< enum Node::ref_type o_ref_t >
|
---|
[60aaa51d] | 161 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
|
---|
[87701b6] | 162 | if( node ) _inc(node);
|
---|
[6d51bd7] | 163 | }
|
---|
| 164 |
|
---|
| 165 | template<typename o_node_t>
|
---|
| 166 | ptr_base & operator=( const o_node_t * node ) {
|
---|
[dafe9e1] | 167 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
|
---|
[6d51bd7] | 168 | return *this;
|
---|
[f47f887] | 169 | }
|
---|
[cedb545] | 170 |
|
---|
[f685679] | 171 | ptr_base & operator=( const ptr_base & o ) {
|
---|
| 172 | assign(o.node);
|
---|
| 173 | return *this;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | ptr_base & operator=( ptr_base && o ) {
|
---|
[d76c588] | 177 | if ( node == o.node ) return *this;
|
---|
| 178 | if ( node ) _dec(node);
|
---|
| 179 | node = o.node;
|
---|
| 180 | o.node = nullptr;
|
---|
[f685679] | 181 | return *this;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[9e1d485] | 184 | template< enum Node::ref_type o_ref_t >
|
---|
[f47f887] | 185 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
|
---|
[60aaa51d] | 186 | assign(o.get());
|
---|
[f47f887] | 187 | return *this;
|
---|
| 188 | }
|
---|
[cedb545] | 189 |
|
---|
[9e1d485] | 190 | template< enum Node::ref_type o_ref_t >
|
---|
[f47f887] | 191 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
|
---|
[60aaa51d] | 192 | assign(o.get());
|
---|
[f47f887] | 193 | return *this;
|
---|
| 194 | }
|
---|
[cedb545] | 195 |
|
---|
[4864a73] | 196 | const node_t * get() const { _check(); return node; }
|
---|
| 197 | const node_t * operator->() const { _check(); return node; }
|
---|
| 198 | const node_t & operator* () const { _check(); return *node; }
|
---|
| 199 | explicit operator bool() const { _check(); return node; }
|
---|
| 200 | operator const node_t * () const { _check(); return node; }
|
---|
[cedb545] | 201 |
|
---|
[54e41b3] | 202 | /// wrapper for convenient access to dynamic_cast
|
---|
[fdbd4fd] | 203 | template<typename o_node_t>
|
---|
[4864a73] | 204 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
|
---|
[fdbd4fd] | 205 |
|
---|
[ee574a2] | 206 | /// wrapper for convenient access to strict_dynamic_cast
|
---|
| 207 | template<typename o_node_t>
|
---|
[546e712] | 208 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); }
|
---|
[ee574a2] | 209 |
|
---|
[76ed81f] | 210 | /// Returns a mutable version of the pointer in this node.
|
---|
| 211 | node_t * get_and_mutate();
|
---|
| 212 |
|
---|
[54e41b3] | 213 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
|
---|
| 214 | /// Returns a mutable version of the pointer in this node.
|
---|
[10a1225] | 215 | node_t * set_and_mutate( const node_t * n );
|
---|
[54e41b3] | 216 |
|
---|
[f47f887] | 217 | using ptr = const node_t *;
|
---|
[cedb545] | 218 |
|
---|
[f47f887] | 219 | private:
|
---|
[9e1d485] | 220 | void assign( const node_t * other ) {
|
---|
[87701b6] | 221 | if( other ) _inc(other);
|
---|
| 222 | if( node ) _dec(node );
|
---|
[f47f887] | 223 | node = other;
|
---|
| 224 | }
|
---|
[cedb545] | 225 |
|
---|
[87701b6] | 226 | void _inc( const node_t * other );
|
---|
| 227 | void _dec( const node_t * other );
|
---|
[4864a73] | 228 | void _check() const;
|
---|
[87701b6] | 229 |
|
---|
[9e1d485] | 230 | const node_t * node;
|
---|
[f47f887] | 231 | };
|
---|
[cedb545] | 232 |
|
---|
[2bb4a01] | 233 | /// Owning pointer to node
|
---|
[f47f887] | 234 | template< typename node_t >
|
---|
| 235 | using ptr = ptr_base< node_t, Node::ref_type::strong >;
|
---|
[cedb545] | 236 |
|
---|
[2bb4a01] | 237 | /// Observing pointer to node
|
---|
[f47f887] | 238 | template< typename node_t >
|
---|
| 239 | using readonly = ptr_base< node_t, Node::ref_type::weak >;
|
---|
[2bb4a01] | 240 | }
|
---|
| 241 |
|
---|
| 242 | // Local Variables: //
|
---|
| 243 | // tab-width: 4 //
|
---|
| 244 | // mode: c++ //
|
---|
| 245 | // compile-command: "make install" //
|
---|
[1e97287] | 246 | // End: //
|
---|