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