| 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 <iosfwd>
|
|---|
| 20 |
|
|---|
| 21 | #include "Common/ErrorObjects.h" // for SemanticErrorException
|
|---|
| 22 |
|
|---|
| 23 | namespace ast {
|
|---|
| 24 |
|
|---|
| 25 | class Visitor;
|
|---|
| 26 |
|
|---|
| 27 | /// Base class for all AST nodes.
|
|---|
| 28 | /// Keeps both strong and weak reference counts.
|
|---|
| 29 | class Node {
|
|---|
| 30 | public:
|
|---|
| 31 | // override defaults to ensure assignment doesn't
|
|---|
| 32 | // change/share reference counts
|
|---|
| 33 | Node() = default;
|
|---|
| 34 | Node(const Node&) : strong_count(0), weak_count(0) {}
|
|---|
| 35 | Node(Node&&) : strong_count(0), weak_count(0) {}
|
|---|
| 36 | Node& operator= (const Node&) = delete;
|
|---|
| 37 | Node& operator= (Node&&) = delete;
|
|---|
| 38 | virtual ~Node() = default;
|
|---|
| 39 |
|
|---|
| 40 | virtual const Node * accept( Visitor & v ) const = 0;
|
|---|
| 41 |
|
|---|
| 42 | /// Types of node references
|
|---|
| 43 | enum class ref_type {
|
|---|
| 44 | strong,
|
|---|
| 45 | weak
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | private:
|
|---|
| 49 | /// Make a copy of this node; should be overridden in subclass with more precise return type
|
|---|
| 50 | virtual Node * clone() const = 0;
|
|---|
| 51 |
|
|---|
| 52 | /// Must be copied in ALL derived classes
|
|---|
| 53 | template<typename node_t>
|
|---|
| 54 | friend node_t * mutate(const node_t * node);
|
|---|
| 55 |
|
|---|
| 56 | mutable size_t strong_count = 0;
|
|---|
| 57 | mutable size_t weak_count = 0;
|
|---|
| 58 |
|
|---|
| 59 | void increment(ref_type ref) const {
|
|---|
| 60 | switch (ref) {
|
|---|
| 61 | case ref_type::strong: strong_count++; break;
|
|---|
| 62 | case ref_type::weak : weak_count ++; break;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void decrement(ast::Node::ref_type ref) const {
|
|---|
| 67 | switch (ref) {
|
|---|
| 68 | case ref_type::strong: strong_count--; break;
|
|---|
| 69 | case ref_type::weak : weak_count --; break;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if(!strong_count && !weak_count) {
|
|---|
| 73 | delete this;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | template< typename node_t, enum Node::ref_type ref_t >
|
|---|
| 78 | friend class ptr_base;
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | // Mutate a node, non-member function to avoid static type
|
|---|
| 82 | // problems and be able to use auto return
|
|---|
| 83 | template<typename node_t>
|
|---|
| 84 | node_t * mutate( const node_t * node ) {
|
|---|
| 85 | if (node->strong_count <= 1) {
|
|---|
| 86 | return const_cast<node_t *>(node);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | assertf(
|
|---|
| 90 | node->weak_count == 0,
|
|---|
| 91 | "Error: mutating node with weak references to it will invalided some references"
|
|---|
| 92 | );
|
|---|
| 93 | return node->clone();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | std::ostream& operator<< ( std::ostream& out, const Node * node );
|
|---|
| 97 |
|
|---|
| 98 | /// Call a visitor on a possibly-null node
|
|---|
| 99 | template<typename node_t>
|
|---|
| 100 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) {
|
|---|
| 101 | return n ? n->accept( v ) : nullptr;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed
|
|---|
| 105 | template< typename Container >
|
|---|
| 106 | void accept_each( const Container & c, Visitor & v ) {
|
|---|
| 107 | SemanticErrorException errors;
|
|---|
| 108 | for ( const auto & i : c ) {
|
|---|
| 109 | try {
|
|---|
| 110 | if ( i ) {
|
|---|
| 111 | i->accept( v );
|
|---|
| 112 | }
|
|---|
| 113 | } catch ( SemanticErrorException & e ) {
|
|---|
| 114 | errors.append( e );
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | if ( ! errors.isEmpty() ) {
|
|---|
| 118 | throw errors;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /// Base class for the smart pointer types
|
|---|
| 123 | /// should never really be used.
|
|---|
| 124 | template< typename node_t, enum Node::ref_type ref_t >
|
|---|
| 125 | class ptr_base {
|
|---|
| 126 | public:
|
|---|
| 127 | ptr_base() : node(nullptr) {}
|
|---|
| 128 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
|
|---|
| 129 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
|
|---|
| 130 |
|
|---|
| 131 | ptr_base( const ptr_base & o ) : node(o.node) {
|
|---|
| 132 | if( node ) _inc(node);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
|
|---|
| 136 |
|
|---|
| 137 | template< enum Node::ref_type o_ref_t >
|
|---|
| 138 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
|
|---|
| 139 | if( node ) _inc(node);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | template< enum Node::ref_type o_ref_t >
|
|---|
| 143 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
|
|---|
| 144 | if( node ) _inc(node);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | template<typename o_node_t>
|
|---|
| 148 | ptr_base & operator=( const o_node_t * node ) {
|
|---|
| 149 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
|
|---|
| 150 | return *this;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | ptr_base & operator=( const ptr_base & o ) {
|
|---|
| 154 | assign(o.node);
|
|---|
| 155 | return *this;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | ptr_base & operator=( ptr_base && o ) {
|
|---|
| 159 | if ( node == o.node ) return *this;
|
|---|
| 160 | if ( node ) _dec(node);
|
|---|
| 161 | node = o.node;
|
|---|
| 162 | o.node = nullptr;
|
|---|
| 163 | return *this;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | template< enum Node::ref_type o_ref_t >
|
|---|
| 167 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
|
|---|
| 168 | assign(o.node);
|
|---|
| 169 | return *this;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | template< enum Node::ref_type o_ref_t >
|
|---|
| 173 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
|
|---|
| 174 | assign(o.node);
|
|---|
| 175 | return *this;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | const node_t * get() const { return node; }
|
|---|
| 179 | const node_t * operator->() const { return node; }
|
|---|
| 180 | const node_t & operator* () const { return *node; }
|
|---|
| 181 | explicit operator bool() const { return node; }
|
|---|
| 182 | operator const node_t * () const { return node; }
|
|---|
| 183 |
|
|---|
| 184 | /// wrapper for convenient access to dynamic_cast
|
|---|
| 185 | template<typename o_node_t>
|
|---|
| 186 | const o_node_t * as() const { return dynamic_cast<const o_node_t *>(node); }
|
|---|
| 187 |
|
|---|
| 188 | /// wrapper for convenient access to strict_dynamic_cast
|
|---|
| 189 | template<typename o_node_t>
|
|---|
| 190 | const o_node_t * strict_as() const { return strict_dynamic_cast<const o_node_t *>(node); }
|
|---|
| 191 |
|
|---|
| 192 | /// Returns a mutable version of the pointer in this node.
|
|---|
| 193 | node_t * get_and_mutate();
|
|---|
| 194 |
|
|---|
| 195 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
|
|---|
| 196 | /// Returns a mutable version of the pointer in this node.
|
|---|
| 197 | node_t * set_and_mutate( const node_t * n );
|
|---|
| 198 |
|
|---|
| 199 | using ptr = const node_t *;
|
|---|
| 200 |
|
|---|
| 201 | private:
|
|---|
| 202 | void assign( const node_t * other ) {
|
|---|
| 203 | if( other ) _inc(other);
|
|---|
| 204 | if( node ) _dec(node );
|
|---|
| 205 | node = other;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | void _inc( const node_t * other );
|
|---|
| 209 | void _dec( const node_t * other );
|
|---|
| 210 |
|
|---|
| 211 | protected:
|
|---|
| 212 | const node_t * node;
|
|---|
| 213 | };
|
|---|
| 214 |
|
|---|
| 215 | /// Owning pointer to node
|
|---|
| 216 | template< typename node_t >
|
|---|
| 217 | using ptr = ptr_base< node_t, Node::ref_type::strong >;
|
|---|
| 218 |
|
|---|
| 219 | /// Observing pointer to node
|
|---|
| 220 | template< typename node_t >
|
|---|
| 221 | using readonly = ptr_base< node_t, Node::ref_type::weak >;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // Local Variables: //
|
|---|
| 225 | // tab-width: 4 //
|
|---|
| 226 | // mode: c++ //
|
|---|
| 227 | // compile-command: "make install" //
|
|---|
| 228 | // End: //
|
|---|