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 May 9 10:20:00 2022 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | #include <cstddef> // for nullptr_t |
---|
20 | #include <iosfwd> |
---|
21 | |
---|
22 | #include "Common/ErrorObjects.hpp" // for SemanticErrorException |
---|
23 | |
---|
24 | namespace ast { |
---|
25 | |
---|
26 | class Visitor; |
---|
27 | |
---|
28 | /// Base class for all AST nodes. |
---|
29 | /// Keeps both strong and weak reference counts. |
---|
30 | class Node { |
---|
31 | public: |
---|
32 | // override defaults to ensure assignment doesn't |
---|
33 | // change/share reference counts |
---|
34 | Node() = default; |
---|
35 | Node(const Node&) : strong_count(0), weak_count(0) {} |
---|
36 | Node(Node&&) : strong_count(0), weak_count(0) {} |
---|
37 | Node& operator=(const Node&) = delete; |
---|
38 | Node& operator=(Node&&) = delete; |
---|
39 | virtual ~Node() {} |
---|
40 | |
---|
41 | virtual const Node * accept( Visitor & v ) const = 0; |
---|
42 | |
---|
43 | /// Types of node references |
---|
44 | enum class ref_type { |
---|
45 | strong, |
---|
46 | weak |
---|
47 | }; |
---|
48 | |
---|
49 | bool unique() const { return strong_count == 1; } |
---|
50 | bool isManaged() const { return strong_count > 0; } |
---|
51 | bool isReferenced() const { return weak_count > 0; } |
---|
52 | bool isStable() const { |
---|
53 | return (1 == strong_count || (1 < strong_count && 0 == weak_count)); |
---|
54 | } |
---|
55 | |
---|
56 | private: |
---|
57 | /// Make a copy of this node; should be overridden in subclass with more precise return type |
---|
58 | virtual Node * clone() const = 0; |
---|
59 | |
---|
60 | /// Must be copied in ALL derived classes |
---|
61 | template<typename node_t> |
---|
62 | friend node_t * mutate(const node_t * node); |
---|
63 | template<typename node_t> |
---|
64 | friend node_t * shallowCopy(const node_t * node); |
---|
65 | |
---|
66 | mutable size_t strong_count = 0; |
---|
67 | mutable size_t weak_count = 0; |
---|
68 | mutable bool was_ever_strong = false; |
---|
69 | |
---|
70 | void increment(ref_type ref) const { |
---|
71 | switch (ref) { |
---|
72 | case ref_type::strong: strong_count++; was_ever_strong = true; break; |
---|
73 | case ref_type::weak : weak_count ++; break; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void decrement(ast::Node::ref_type ref, bool do_delete = true) const { |
---|
78 | switch (ref) { |
---|
79 | case ref_type::strong: strong_count--; break; |
---|
80 | case ref_type::weak : weak_count --; break; |
---|
81 | } |
---|
82 | |
---|
83 | if( do_delete && !strong_count && !weak_count) { |
---|
84 | delete this; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | template< typename node_t, enum Node::ref_type ref_t > |
---|
89 | friend class ptr_base; |
---|
90 | }; |
---|
91 | |
---|
92 | /// Mutate a node, non-member function to avoid static type |
---|
93 | /// problems and be able to use auto return |
---|
94 | template<typename node_t> |
---|
95 | node_t * mutate( const node_t * node ) { |
---|
96 | if (node->strong_count <= 1) { |
---|
97 | return const_cast<node_t *>(node); |
---|
98 | } |
---|
99 | |
---|
100 | assertf( |
---|
101 | node->weak_count == 0, |
---|
102 | "Error: mutating node with weak references to it will invalidate some references" |
---|
103 | ); |
---|
104 | return node->clone(); |
---|
105 | } |
---|
106 | |
---|
107 | /// Mutate a node field (only clones if not equal to existing value) |
---|
108 | template<typename node_t, typename super_t, typename field_t, typename assn_t> |
---|
109 | const node_t * mutate_field( const node_t * node, field_t super_t::* field, assn_t && val ) { |
---|
110 | // skip mutate if equivalent |
---|
111 | if ( node->*field == val ) return node; |
---|
112 | |
---|
113 | // mutate and return |
---|
114 | node_t * ret = mutate( node ); |
---|
115 | ret->*field = std::forward< assn_t >( val ); |
---|
116 | return ret; |
---|
117 | } |
---|
118 | |
---|
119 | /// Mutate a single index of a node field (only clones if not equal to existing value) |
---|
120 | template<typename node_t, typename super_t, typename coll_t, typename ind_t, typename field_t> |
---|
121 | const node_t * mutate_field_index( |
---|
122 | const node_t * node, coll_t super_t::* field, ind_t i, field_t && val |
---|
123 | ) { |
---|
124 | // skip mutate if equivalent |
---|
125 | if ( (node->*field)[i] == val ) return node; |
---|
126 | |
---|
127 | // mutate and return |
---|
128 | node_t * ret = mutate( node ); |
---|
129 | (ret->*field)[i] = std::forward< field_t >( val ); |
---|
130 | return ret; |
---|
131 | } |
---|
132 | |
---|
133 | /// Mutate an entire indexed collection by cloning to accepted value |
---|
134 | template<typename node_t, typename super_t, typename coll_t> |
---|
135 | const node_t * mutate_each( const node_t * node, coll_t super_t::* field, Visitor & v ) { |
---|
136 | for ( unsigned i = 0; i < (node->*field).size(); ++i ) { |
---|
137 | node = mutate_field_index( node, field, i, (node->*field)[i]->accept( v ) ); |
---|
138 | } |
---|
139 | return node; |
---|
140 | } |
---|
141 | |
---|
142 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
143 | |
---|
144 | /// Call a visitor on a possibly-null node |
---|
145 | template<typename node_t> |
---|
146 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
147 | return n ? n->accept( v ) : nullptr; |
---|
148 | } |
---|
149 | |
---|
150 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed |
---|
151 | template< typename Container > |
---|
152 | void accept_each( const Container & c, Visitor & v ) { |
---|
153 | SemanticErrorException errors; |
---|
154 | for ( const auto & i : c ) { |
---|
155 | try { |
---|
156 | if ( i ) { |
---|
157 | i->accept( v ); |
---|
158 | } |
---|
159 | } catch ( SemanticErrorException & e ) { |
---|
160 | errors.append( e ); |
---|
161 | } |
---|
162 | } |
---|
163 | errors.throwIfNonEmpty(); |
---|
164 | } |
---|
165 | |
---|
166 | /// Base class for the smart pointer types |
---|
167 | /// should never really be used. |
---|
168 | template< typename node_t, enum Node::ref_type ref_t > |
---|
169 | class ptr_base { |
---|
170 | public: |
---|
171 | ptr_base() : node(nullptr) {} |
---|
172 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
173 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
174 | |
---|
175 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
176 | if( node ) _inc(node); |
---|
177 | } |
---|
178 | |
---|
179 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
180 | |
---|
181 | template< enum Node::ref_type o_ref_t > |
---|
182 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { |
---|
183 | if( node ) _inc(node); |
---|
184 | } |
---|
185 | |
---|
186 | template< enum Node::ref_type o_ref_t > |
---|
187 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { |
---|
188 | if( node ) _inc(node); |
---|
189 | } |
---|
190 | |
---|
191 | ptr_base & operator=( const node_t * node ) { |
---|
192 | assign( node ); |
---|
193 | return *this; |
---|
194 | } |
---|
195 | |
---|
196 | template<typename o_node_t> |
---|
197 | ptr_base & operator=( const o_node_t * node ) { |
---|
198 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
199 | return *this; |
---|
200 | } |
---|
201 | |
---|
202 | ptr_base & operator=( std::nullptr_t ) { |
---|
203 | if ( node ) _dec(node); |
---|
204 | node = nullptr; |
---|
205 | return *this; |
---|
206 | } |
---|
207 | |
---|
208 | ptr_base & operator=( const ptr_base & o ) { |
---|
209 | assign(o.node); |
---|
210 | return *this; |
---|
211 | } |
---|
212 | |
---|
213 | ptr_base & operator=( ptr_base && o ) { |
---|
214 | if ( node == o.node ) return *this; |
---|
215 | if ( node ) _dec(node); |
---|
216 | node = o.node; |
---|
217 | o.node = nullptr; |
---|
218 | return *this; |
---|
219 | } |
---|
220 | |
---|
221 | template< enum Node::ref_type o_ref_t > |
---|
222 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
223 | assign(o.get()); |
---|
224 | return *this; |
---|
225 | } |
---|
226 | |
---|
227 | template< enum Node::ref_type o_ref_t > |
---|
228 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
229 | assign(o.get()); |
---|
230 | return *this; |
---|
231 | } |
---|
232 | |
---|
233 | /// Swaps the nodes contained within two pointers. |
---|
234 | void swap( ptr_base & other ) noexcept; |
---|
235 | |
---|
236 | const node_t * get() const { _check(); return node; } |
---|
237 | const node_t * operator->() const { _check(); return node; } |
---|
238 | const node_t & operator* () const { _check(); return *node; } |
---|
239 | explicit operator bool() const { _check(); return node; } |
---|
240 | operator const node_t * () const & { _check(); return node; } |
---|
241 | operator const node_t * () && = delete; |
---|
242 | |
---|
243 | const node_t * release() { |
---|
244 | const node_t * ret = node; |
---|
245 | if ( node ) { |
---|
246 | _dec(node, false); |
---|
247 | node = nullptr; |
---|
248 | } |
---|
249 | return ret; |
---|
250 | } |
---|
251 | |
---|
252 | /// wrapper for convenient access to dynamic_cast |
---|
253 | template<typename o_node_t> |
---|
254 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } |
---|
255 | |
---|
256 | /// Wrapper that makes sure dynamic_cast returns non-null. |
---|
257 | template<typename o_node_t> |
---|
258 | const o_node_t * strict_as() const { |
---|
259 | if (const o_node_t * ret = as<o_node_t>()) return ret; |
---|
260 | _strict_fail(); |
---|
261 | } |
---|
262 | |
---|
263 | /// Wrapper that makes sure dynamic_cast does not fail. |
---|
264 | template<typename o_node_t, decltype(nullptr) null> |
---|
265 | const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; } |
---|
266 | |
---|
267 | /// Returns a mutable version of the pointer in this node. |
---|
268 | node_t * get_and_mutate(); |
---|
269 | |
---|
270 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
271 | /// Returns a mutable version of the pointer in this node. |
---|
272 | node_t * set_and_mutate( const node_t * n ); |
---|
273 | |
---|
274 | using ptr = const node_t *; |
---|
275 | |
---|
276 | private: |
---|
277 | void assign( const node_t * other ) { |
---|
278 | if( other ) _inc(other); |
---|
279 | if( node ) _dec(node ); |
---|
280 | node = other; |
---|
281 | } |
---|
282 | |
---|
283 | void _inc( const node_t * other ); |
---|
284 | void _dec( const node_t * other, bool do_delete = true ); |
---|
285 | void _check() const; |
---|
286 | void _strict_fail() const __attribute__((noreturn)); |
---|
287 | |
---|
288 | const node_t * node; |
---|
289 | }; |
---|
290 | |
---|
291 | /// Owning pointer to node |
---|
292 | template< typename node_t > |
---|
293 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
294 | |
---|
295 | /// Observing pointer to node |
---|
296 | template< typename node_t > |
---|
297 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
298 | |
---|
299 | /// Non-member swap that an participate in overload resolution. |
---|
300 | template< typename node_t, enum Node::ref_type ref_t > |
---|
301 | void swap( ptr_base< node_t, ref_t > & l, ptr_base< node_t, ref_t > & r ) { |
---|
302 | l.swap( r ); |
---|
303 | } |
---|
304 | |
---|
305 | } |
---|
306 | |
---|
307 | // Local Variables: // |
---|
308 | // tab-width: 4 // |
---|
309 | // mode: c++ // |
---|
310 | // compile-command: "make install" // |
---|
311 | // End: // |
---|