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