[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
|
---|
| 11 | // Last Modified By : Aaron B. Moss
|
---|
| 12 | // Last Modified On : Wed May 8 11:00:00 2019
|
---|
| 13 | // Update Count : 2
|
---|
| 14 | //
|
---|
| 15 |
|
---|
[cedb545] | 16 | #pragma once
|
---|
| 17 |
|
---|
[2bb4a01] | 18 | #include <cassert>
|
---|
| 19 | #include <iosfwd>
|
---|
[cedb545] | 20 |
|
---|
| 21 | namespace ast {
|
---|
[2bb4a01] | 22 |
|
---|
| 23 | class Visitor;
|
---|
| 24 |
|
---|
| 25 | /// Base class for all AST nodes.
|
---|
| 26 | /// Keeps both strong and weak reference counts.
|
---|
[f47f887] | 27 | class Node {
|
---|
| 28 | public:
|
---|
[14cebb7a] | 29 | // override defaults to ensure assignment doesn't
|
---|
[2bb4a01] | 30 | // change/share reference counts
|
---|
| 31 | Node() = default;
|
---|
| 32 | Node(const Node&) : strong_ref(0), weak_ref(0) {}
|
---|
| 33 | Node(Node&&) : strong_ref(0), weak_ref(0) {}
|
---|
| 34 | Node& operator= (const Node&) = delete;
|
---|
| 35 | Node& operator= (Node&&) = delete;
|
---|
[f47f887] | 36 | virtual ~Node() = default;
|
---|
[cedb545] | 37 |
|
---|
[2bb4a01] | 38 | virtual Node* accept( Visitor& v ) = 0;
|
---|
| 39 |
|
---|
| 40 | /// Types of node references
|
---|
[f47f887] | 41 | enum class ref_type {
|
---|
| 42 | strong,
|
---|
| 43 | weak
|
---|
| 44 | };
|
---|
[cedb545] | 45 |
|
---|
[f47f887] | 46 | inline void increment(ref_type ref) const {
|
---|
| 47 | switch (ref) {
|
---|
| 48 | case ref_type::strong: strong_ref++; break;
|
---|
| 49 | case ref_type::weak : weak_ref ++; break;
|
---|
[cedb545] | 50 | }
|
---|
[f47f887] | 51 | }
|
---|
[cedb545] | 52 |
|
---|
[f47f887] | 53 | inline void decrement(ref_type ref) const {
|
---|
| 54 | switch (ref) {
|
---|
| 55 | case ref_type::strong: strong_ref--; break;
|
---|
| 56 | case ref_type::weak : weak_ref --; break;
|
---|
[cedb545] | 57 | }
|
---|
| 58 |
|
---|
[f47f887] | 59 | if(!strong_ref && !weak_ref) {
|
---|
| 60 | delete this;
|
---|
[6a625de] | 61 | }
|
---|
[f47f887] | 62 | }
|
---|
[6a625de] | 63 |
|
---|
[f47f887] | 64 | template<typename node_t>
|
---|
| 65 | friend auto mutate(const node_t * node);
|
---|
| 66 |
|
---|
| 67 | private:
|
---|
[2bb4a01] | 68 | /// Make a copy of this node; should be overridden in subclass with more precise return type
|
---|
| 69 | virtual Node* clone() const = 0;
|
---|
| 70 |
|
---|
[f47f887] | 71 | mutable size_t strong_ref = 0;
|
---|
| 72 | mutable size_t weak_ref = 0;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | // Mutate a node, non-member function to avoid static type
|
---|
| 76 | // problems and be able to use auto return
|
---|
| 77 | template<typename node_t>
|
---|
| 78 | auto mutate(const node_t * node) {
|
---|
| 79 | assertf(
|
---|
| 80 | node->strong_count >= 1,
|
---|
| 81 | "Error: attempting to mutate a node that appears to have been linked"
|
---|
| 82 | );
|
---|
| 83 | if (node->strong_count == 1) {
|
---|
| 84 | return const_cast<node_t *>(node);
|
---|
[6a625de] | 85 | }
|
---|
| 86 |
|
---|
[f47f887] | 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 |
|
---|
[2bb4a01] | 94 | std::ostream& operator<< ( std::ostream& out, const Node* node );
|
---|
| 95 |
|
---|
[f47f887] | 96 | // Base class for the smart pointer types
|
---|
| 97 | // should never really be used.
|
---|
| 98 | template< typename node_t, enum Node::ref_type ref_t>
|
---|
| 99 | class ptr_base {
|
---|
| 100 | public:
|
---|
| 101 | ptr_base() : node(nullptr) {}
|
---|
| 102 | ptr_base( node_t * n ) : node(n) { if( !node ) node->increment(ref_t); }
|
---|
| 103 | ~ptr_base() { if( node ) node->decrement(ref_t); }
|
---|
| 104 |
|
---|
| 105 | template< enum Node::ref_type o_ref_t >
|
---|
| 106 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
|
---|
| 107 | if( !node ) return;
|
---|
| 108 | node->increment(ref_t);
|
---|
[6a625de] | 109 | }
|
---|
| 110 |
|
---|
[f47f887] | 111 | template< enum Node::ref_type o_ref_t >
|
---|
| 112 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
|
---|
| 113 | if( node ) node->increment(ref_t);
|
---|
| 114 | }
|
---|
[cedb545] | 115 |
|
---|
[f47f887] | 116 | template< enum Node::ref_type o_ref_t >
|
---|
| 117 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
|
---|
| 118 | assign(o.node);
|
---|
| 119 | return *this;
|
---|
| 120 | }
|
---|
[cedb545] | 121 |
|
---|
[f47f887] | 122 | template< enum Node::ref_type o_ref_t >
|
---|
| 123 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
|
---|
| 124 | if(o.node == node) return *this;
|
---|
| 125 | assign(o.node);
|
---|
| 126 | return *this;
|
---|
| 127 | }
|
---|
[cedb545] | 128 |
|
---|
[f47f887] | 129 | const node_t * get() const { return node; }
|
---|
| 130 | const node_t * operator->() const { return node; }
|
---|
| 131 | const node_t & operator* () const { return *node; }
|
---|
| 132 | explicit operator bool() const { return node; }
|
---|
| 133 | operator const node_t * const() const { return node; }
|
---|
[cedb545] | 134 |
|
---|
[f47f887] | 135 | using ptr = const node_t *;
|
---|
[cedb545] | 136 |
|
---|
[f47f887] | 137 | private:
|
---|
| 138 | void assign(node_t * other ) {
|
---|
| 139 | if( other ) other->increment(ref_t);
|
---|
| 140 | if( node ) node ->decrement(ref_t);
|
---|
| 141 | node = other;
|
---|
| 142 | }
|
---|
[cedb545] | 143 |
|
---|
[f47f887] | 144 | protected:
|
---|
| 145 | node_t * node;
|
---|
| 146 | };
|
---|
[cedb545] | 147 |
|
---|
[2bb4a01] | 148 | /// Owning pointer to node
|
---|
[f47f887] | 149 | template< typename node_t >
|
---|
| 150 | using ptr = ptr_base< node_t, Node::ref_type::strong >;
|
---|
[cedb545] | 151 |
|
---|
[2bb4a01] | 152 | /// Observing pointer to node
|
---|
[f47f887] | 153 | template< typename node_t >
|
---|
| 154 | using readonly = ptr_base< node_t, Node::ref_type::weak >;
|
---|
[2bb4a01] | 155 | }
|
---|
| 156 |
|
---|
| 157 | // Local Variables: //
|
---|
| 158 | // tab-width: 4 //
|
---|
| 159 | // mode: c++ //
|
---|
| 160 | // compile-command: "make install" //
|
---|
| 161 | // End: //
|
---|