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