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.h" // 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 | if ( ! errors.isEmpty() ) {
|
---|
164 | throw errors;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /// Base class for the smart pointer types
|
---|
169 | /// should never really be used.
|
---|
170 | template< typename node_t, enum Node::ref_type ref_t >
|
---|
171 | class ptr_base {
|
---|
172 | public:
|
---|
173 | ptr_base() : node(nullptr) {}
|
---|
174 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
|
---|
175 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
|
---|
176 |
|
---|
177 | ptr_base( const ptr_base & o ) : node(o.node) {
|
---|
178 | if( node ) _inc(node);
|
---|
179 | }
|
---|
180 |
|
---|
181 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
|
---|
182 |
|
---|
183 | template< enum Node::ref_type o_ref_t >
|
---|
184 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
|
---|
185 | if( node ) _inc(node);
|
---|
186 | }
|
---|
187 |
|
---|
188 | template< enum Node::ref_type o_ref_t >
|
---|
189 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
|
---|
190 | if( node ) _inc(node);
|
---|
191 | }
|
---|
192 |
|
---|
193 | ptr_base & operator=( const node_t * node ) {
|
---|
194 | assign( node );
|
---|
195 | return *this;
|
---|
196 | }
|
---|
197 |
|
---|
198 | template<typename o_node_t>
|
---|
199 | ptr_base & operator=( const o_node_t * node ) {
|
---|
200 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
|
---|
201 | return *this;
|
---|
202 | }
|
---|
203 |
|
---|
204 | ptr_base & operator=( std::nullptr_t ) {
|
---|
205 | if ( node ) _dec(node);
|
---|
206 | node = nullptr;
|
---|
207 | return *this;
|
---|
208 | }
|
---|
209 |
|
---|
210 | ptr_base & operator=( const ptr_base & o ) {
|
---|
211 | assign(o.node);
|
---|
212 | return *this;
|
---|
213 | }
|
---|
214 |
|
---|
215 | ptr_base & operator=( ptr_base && o ) {
|
---|
216 | if ( node == o.node ) return *this;
|
---|
217 | if ( node ) _dec(node);
|
---|
218 | node = o.node;
|
---|
219 | o.node = nullptr;
|
---|
220 | return *this;
|
---|
221 | }
|
---|
222 |
|
---|
223 | template< enum Node::ref_type o_ref_t >
|
---|
224 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
|
---|
225 | assign(o.get());
|
---|
226 | return *this;
|
---|
227 | }
|
---|
228 |
|
---|
229 | template< enum Node::ref_type o_ref_t >
|
---|
230 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
|
---|
231 | assign(o.get());
|
---|
232 | return *this;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /// Swaps the nodes contained within two pointers.
|
---|
236 | void swap( ptr_base & other ) noexcept;
|
---|
237 |
|
---|
238 | const node_t * get() const { _check(); return node; }
|
---|
239 | const node_t * operator->() const { _check(); return node; }
|
---|
240 | const node_t & operator* () const { _check(); return *node; }
|
---|
241 | explicit operator bool() const { _check(); return node; }
|
---|
242 | operator const node_t * () const & { _check(); return node; }
|
---|
243 | operator const node_t * () && = delete;
|
---|
244 |
|
---|
245 | const node_t * release() {
|
---|
246 | const node_t * ret = node;
|
---|
247 | if ( node ) {
|
---|
248 | _dec(node, false);
|
---|
249 | node = nullptr;
|
---|
250 | }
|
---|
251 | return ret;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /// wrapper for convenient access to dynamic_cast
|
---|
255 | template<typename o_node_t>
|
---|
256 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
|
---|
257 |
|
---|
258 | /// Wrapper that makes sure dynamic_cast returns non-null.
|
---|
259 | template<typename o_node_t>
|
---|
260 | const o_node_t * strict_as() const {
|
---|
261 | if (const o_node_t * ret = as<o_node_t>()) return ret;
|
---|
262 | _strict_fail();
|
---|
263 | }
|
---|
264 |
|
---|
265 | /// Wrapper that makes sure dynamic_cast does not fail.
|
---|
266 | template<typename o_node_t, decltype(nullptr) null>
|
---|
267 | const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; }
|
---|
268 |
|
---|
269 | /// Returns a mutable version of the pointer in this node.
|
---|
270 | node_t * get_and_mutate();
|
---|
271 |
|
---|
272 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
|
---|
273 | /// Returns a mutable version of the pointer in this node.
|
---|
274 | node_t * set_and_mutate( const node_t * n );
|
---|
275 |
|
---|
276 | using ptr = const node_t *;
|
---|
277 |
|
---|
278 | private:
|
---|
279 | void assign( const node_t * other ) {
|
---|
280 | if( other ) _inc(other);
|
---|
281 | if( node ) _dec(node );
|
---|
282 | node = other;
|
---|
283 | }
|
---|
284 |
|
---|
285 | void _inc( const node_t * other );
|
---|
286 | void _dec( const node_t * other, bool do_delete = true );
|
---|
287 | void _check() const;
|
---|
288 | void _strict_fail() const __attribute__((noreturn));
|
---|
289 |
|
---|
290 | const node_t * node;
|
---|
291 | };
|
---|
292 |
|
---|
293 | /// Owning pointer to node
|
---|
294 | template< typename node_t >
|
---|
295 | using ptr = ptr_base< node_t, Node::ref_type::strong >;
|
---|
296 |
|
---|
297 | /// Observing pointer to node
|
---|
298 | template< typename node_t >
|
---|
299 | using readonly = ptr_base< node_t, Node::ref_type::weak >;
|
---|
300 |
|
---|
301 | /// Non-member swap that an participate in overload resolution.
|
---|
302 | template< typename node_t, enum Node::ref_type ref_t >
|
---|
303 | void swap( ptr_base< node_t, ref_t > & l, ptr_base< node_t, ref_t > & r ) {
|
---|
304 | l.swap( r );
|
---|
305 | }
|
---|
306 |
|
---|
307 | }
|
---|
308 |
|
---|
309 | // Local Variables: //
|
---|
310 | // tab-width: 4 //
|
---|
311 | // mode: c++ //
|
---|
312 | // compile-command: "make install" //
|
---|
313 | // End: //
|
---|