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 <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 | |
---|
52 | private: |
---|
53 | /// Make a copy of this node; should be overridden in subclass with more precise return type |
---|
54 | virtual Node * clone() const = 0; |
---|
55 | |
---|
56 | /// Must be copied in ALL derived classes |
---|
57 | template<typename node_t> |
---|
58 | friend node_t * mutate(const node_t * node); |
---|
59 | template<typename node_t> |
---|
60 | friend node_t * shallowCopy(const node_t * node); |
---|
61 | |
---|
62 | mutable size_t strong_count = 0; |
---|
63 | mutable size_t weak_count = 0; |
---|
64 | mutable bool was_ever_strong = false; |
---|
65 | |
---|
66 | void increment(ref_type ref) const { |
---|
67 | switch (ref) { |
---|
68 | case ref_type::strong: strong_count++; was_ever_strong = true; break; |
---|
69 | case ref_type::weak : weak_count ++; break; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void decrement(ast::Node::ref_type ref, bool do_delete = true) const { |
---|
74 | switch (ref) { |
---|
75 | case ref_type::strong: strong_count--; break; |
---|
76 | case ref_type::weak : weak_count --; break; |
---|
77 | } |
---|
78 | |
---|
79 | if( do_delete && !strong_count && !weak_count) { |
---|
80 | delete this; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | template< typename node_t, enum Node::ref_type ref_t > |
---|
85 | friend class ptr_base; |
---|
86 | }; |
---|
87 | |
---|
88 | /// Mutate a node, non-member function to avoid static type |
---|
89 | /// problems and be able to use auto return |
---|
90 | template<typename node_t> |
---|
91 | node_t * mutate( const node_t * node ) { |
---|
92 | if (node->strong_count <= 1) { |
---|
93 | return const_cast<node_t *>(node); |
---|
94 | } |
---|
95 | |
---|
96 | assertf( |
---|
97 | node->weak_count == 0, |
---|
98 | "Error: mutating node with weak references to it will invalidate some references" |
---|
99 | ); |
---|
100 | return node->clone(); |
---|
101 | } |
---|
102 | |
---|
103 | /// Mutate a node field (only clones if not equal to existing value) |
---|
104 | template<typename node_t, typename parent_t, typename field_t, typename assn_t> |
---|
105 | const node_t * mutate_field( const node_t * node, field_t parent_t::* field, assn_t && val ) { |
---|
106 | // skip mutate if equivalent |
---|
107 | if ( node->*field == val ) return node; |
---|
108 | |
---|
109 | // mutate and return |
---|
110 | node_t * ret = mutate( node ); |
---|
111 | ret->*field = std::forward< assn_t >( val ); |
---|
112 | return ret; |
---|
113 | } |
---|
114 | |
---|
115 | /// Mutate a single index of a node field (only clones if not equal to existing value) |
---|
116 | template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t> |
---|
117 | const node_t * mutate_field_index( |
---|
118 | const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val |
---|
119 | ) { |
---|
120 | // skip mutate if equivalent |
---|
121 | if ( (node->*field)[i] == val ) return node; |
---|
122 | |
---|
123 | // mutate and return |
---|
124 | node_t * ret = mutate( node ); |
---|
125 | (ret->*field)[i] = std::forward< field_t >( val ); |
---|
126 | return ret; |
---|
127 | } |
---|
128 | |
---|
129 | /// Mutate an entire indexed collection by cloning to accepted value |
---|
130 | template<typename node_t, typename parent_t, typename coll_t> |
---|
131 | const node_t * mutate_each( const node_t * node, coll_t parent_t::* field, Visitor & v ) { |
---|
132 | for ( unsigned i = 0; i < (node->*field).size(); ++i ) { |
---|
133 | node = mutate_field_index( node, field, i, (node->*field)[i]->accept( v ) ); |
---|
134 | } |
---|
135 | return node; |
---|
136 | } |
---|
137 | |
---|
138 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
139 | |
---|
140 | /// Call a visitor on a possibly-null node |
---|
141 | template<typename node_t> |
---|
142 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
143 | return n ? n->accept( v ) : nullptr; |
---|
144 | } |
---|
145 | |
---|
146 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed |
---|
147 | template< typename Container > |
---|
148 | void accept_each( const Container & c, Visitor & v ) { |
---|
149 | SemanticErrorException errors; |
---|
150 | for ( const auto & i : c ) { |
---|
151 | try { |
---|
152 | if ( i ) { |
---|
153 | i->accept( v ); |
---|
154 | } |
---|
155 | } catch ( SemanticErrorException & e ) { |
---|
156 | errors.append( e ); |
---|
157 | } |
---|
158 | } |
---|
159 | if ( ! errors.isEmpty() ) { |
---|
160 | throw errors; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | /// Base class for the smart pointer types |
---|
165 | /// should never really be used. |
---|
166 | template< typename node_t, enum Node::ref_type ref_t > |
---|
167 | class ptr_base { |
---|
168 | public: |
---|
169 | ptr_base() : node(nullptr) {} |
---|
170 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
171 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
172 | |
---|
173 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
174 | if( node ) _inc(node); |
---|
175 | } |
---|
176 | |
---|
177 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
178 | |
---|
179 | template< enum Node::ref_type o_ref_t > |
---|
180 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { |
---|
181 | if( node ) _inc(node); |
---|
182 | } |
---|
183 | |
---|
184 | template< enum Node::ref_type o_ref_t > |
---|
185 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { |
---|
186 | if( node ) _inc(node); |
---|
187 | } |
---|
188 | |
---|
189 | template<typename o_node_t> |
---|
190 | ptr_base & operator=( const o_node_t * node ) { |
---|
191 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
192 | return *this; |
---|
193 | } |
---|
194 | |
---|
195 | ptr_base & operator=( std::nullptr_t ) { |
---|
196 | if ( node ) _dec(node); |
---|
197 | node = nullptr; |
---|
198 | return *this; |
---|
199 | } |
---|
200 | |
---|
201 | ptr_base & operator=( const ptr_base & o ) { |
---|
202 | assign(o.node); |
---|
203 | return *this; |
---|
204 | } |
---|
205 | |
---|
206 | ptr_base & operator=( ptr_base && o ) { |
---|
207 | if ( node == o.node ) return *this; |
---|
208 | if ( node ) _dec(node); |
---|
209 | node = o.node; |
---|
210 | o.node = nullptr; |
---|
211 | return *this; |
---|
212 | } |
---|
213 | |
---|
214 | template< enum Node::ref_type o_ref_t > |
---|
215 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
216 | assign(o.get()); |
---|
217 | return *this; |
---|
218 | } |
---|
219 | |
---|
220 | template< enum Node::ref_type o_ref_t > |
---|
221 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
222 | assign(o.get()); |
---|
223 | return *this; |
---|
224 | } |
---|
225 | |
---|
226 | const node_t * get() const { _check(); return node; } |
---|
227 | const node_t * operator->() const { _check(); return node; } |
---|
228 | const node_t & operator* () const { _check(); return *node; } |
---|
229 | explicit operator bool() const { _check(); return node; } |
---|
230 | operator const node_t * () const { _check(); return node; } |
---|
231 | |
---|
232 | const node_t * release() { |
---|
233 | const node_t * ret = node; |
---|
234 | if ( node ) { |
---|
235 | _dec(node, false); |
---|
236 | node = nullptr; |
---|
237 | } |
---|
238 | return ret; |
---|
239 | } |
---|
240 | |
---|
241 | /// wrapper for convenient access to dynamic_cast |
---|
242 | template<typename o_node_t> |
---|
243 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } |
---|
244 | |
---|
245 | /// wrapper for convenient access to strict_dynamic_cast |
---|
246 | template<typename o_node_t> |
---|
247 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } |
---|
248 | |
---|
249 | /// Returns a mutable version of the pointer in this node. |
---|
250 | node_t * get_and_mutate(); |
---|
251 | |
---|
252 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
253 | /// Returns a mutable version of the pointer in this node. |
---|
254 | node_t * set_and_mutate( const node_t * n ); |
---|
255 | |
---|
256 | using ptr = const node_t *; |
---|
257 | |
---|
258 | private: |
---|
259 | void assign( const node_t * other ) { |
---|
260 | if( other ) _inc(other); |
---|
261 | if( node ) _dec(node ); |
---|
262 | node = other; |
---|
263 | } |
---|
264 | |
---|
265 | void _inc( const node_t * other ); |
---|
266 | void _dec( const node_t * other, bool do_delete = true ); |
---|
267 | void _check() const; |
---|
268 | |
---|
269 | const node_t * node; |
---|
270 | }; |
---|
271 | |
---|
272 | /// Owning pointer to node |
---|
273 | template< typename node_t > |
---|
274 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
275 | |
---|
276 | /// Observing pointer to node |
---|
277 | template< typename node_t > |
---|
278 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
279 | } |
---|
280 | |
---|
281 | // Local Variables: // |
---|
282 | // tab-width: 4 // |
---|
283 | // mode: c++ // |
---|
284 | // compile-command: "make install" // |
---|
285 | // End: // |
---|