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 | /// call to debug on node creation/deletion |
---|
33 | void _trap(); |
---|
34 | public: |
---|
35 | // override defaults to ensure assignment doesn't |
---|
36 | // change/share reference counts |
---|
37 | Node() { _trap(); } |
---|
38 | Node(const Node&) : strong_count(0), weak_count(0) { _trap(); } |
---|
39 | Node(Node&&) : strong_count(0), weak_count(0) { _trap(); } |
---|
40 | Node& operator= (const Node&) = delete; |
---|
41 | Node& operator= (Node&&) = delete; |
---|
42 | virtual ~Node() { _trap(); } |
---|
43 | |
---|
44 | virtual const Node * accept( Visitor & v ) const = 0; |
---|
45 | |
---|
46 | /// Types of node references |
---|
47 | enum class ref_type { |
---|
48 | strong, |
---|
49 | weak |
---|
50 | }; |
---|
51 | |
---|
52 | bool unique() const { return strong_count == 1; } |
---|
53 | |
---|
54 | private: |
---|
55 | /// Make a copy of this node; should be overridden in subclass with more precise return type |
---|
56 | virtual Node * clone() const = 0; |
---|
57 | |
---|
58 | /// Must be copied in ALL derived classes |
---|
59 | template<typename node_t> |
---|
60 | friend node_t * mutate(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) const { |
---|
74 | switch (ref) { |
---|
75 | case ref_type::strong: strong_count--; break; |
---|
76 | case ref_type::weak : weak_count --; break; |
---|
77 | } |
---|
78 | |
---|
79 | if(!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 invalided 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 | std::ostream& operator<< ( std::ostream& out, const Node * node ); |
---|
130 | |
---|
131 | /// Call a visitor on a possibly-null node |
---|
132 | template<typename node_t> |
---|
133 | auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) { |
---|
134 | return n ? n->accept( v ) : nullptr; |
---|
135 | } |
---|
136 | |
---|
137 | /// Call a visitor on a collection of nodes, throwing any exceptions when completed |
---|
138 | template< typename Container > |
---|
139 | void accept_each( const Container & c, Visitor & v ) { |
---|
140 | SemanticErrorException errors; |
---|
141 | for ( const auto & i : c ) { |
---|
142 | try { |
---|
143 | if ( i ) { |
---|
144 | i->accept( v ); |
---|
145 | } |
---|
146 | } catch ( SemanticErrorException & e ) { |
---|
147 | errors.append( e ); |
---|
148 | } |
---|
149 | } |
---|
150 | if ( ! errors.isEmpty() ) { |
---|
151 | throw errors; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /// Base class for the smart pointer types |
---|
156 | /// should never really be used. |
---|
157 | template< typename node_t, enum Node::ref_type ref_t > |
---|
158 | class ptr_base { |
---|
159 | public: |
---|
160 | ptr_base() : node(nullptr) {} |
---|
161 | ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); } |
---|
162 | ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } } |
---|
163 | |
---|
164 | ptr_base( const ptr_base & o ) : node(o.node) { |
---|
165 | if( node ) _inc(node); |
---|
166 | } |
---|
167 | |
---|
168 | ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; } |
---|
169 | |
---|
170 | template< enum Node::ref_type o_ref_t > |
---|
171 | ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) { |
---|
172 | if( node ) _inc(node); |
---|
173 | } |
---|
174 | |
---|
175 | template< enum Node::ref_type o_ref_t > |
---|
176 | ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) { |
---|
177 | if( node ) _inc(node); |
---|
178 | } |
---|
179 | |
---|
180 | template<typename o_node_t> |
---|
181 | ptr_base & operator=( const o_node_t * node ) { |
---|
182 | assign( strict_dynamic_cast<const node_t *, nullptr>(node) ); |
---|
183 | return *this; |
---|
184 | } |
---|
185 | |
---|
186 | ptr_base & operator=( std::nullptr_t ) { |
---|
187 | if ( node ) _dec(node); |
---|
188 | node = nullptr; |
---|
189 | return *this; |
---|
190 | } |
---|
191 | |
---|
192 | ptr_base & operator=( const ptr_base & o ) { |
---|
193 | assign(o.node); |
---|
194 | return *this; |
---|
195 | } |
---|
196 | |
---|
197 | ptr_base & operator=( ptr_base && o ) { |
---|
198 | if ( node == o.node ) return *this; |
---|
199 | if ( node ) _dec(node); |
---|
200 | node = o.node; |
---|
201 | o.node = nullptr; |
---|
202 | return *this; |
---|
203 | } |
---|
204 | |
---|
205 | template< enum Node::ref_type o_ref_t > |
---|
206 | ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) { |
---|
207 | assign(o.get()); |
---|
208 | return *this; |
---|
209 | } |
---|
210 | |
---|
211 | template< enum Node::ref_type o_ref_t > |
---|
212 | ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) { |
---|
213 | assign(o.get()); |
---|
214 | return *this; |
---|
215 | } |
---|
216 | |
---|
217 | const node_t * get() const { _check(); return node; } |
---|
218 | const node_t * operator->() const { _check(); return node; } |
---|
219 | const node_t & operator* () const { _check(); return *node; } |
---|
220 | explicit operator bool() const { _check(); return node; } |
---|
221 | operator const node_t * () const { _check(); return node; } |
---|
222 | |
---|
223 | /// wrapper for convenient access to dynamic_cast |
---|
224 | template<typename o_node_t> |
---|
225 | const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); } |
---|
226 | |
---|
227 | /// wrapper for convenient access to strict_dynamic_cast |
---|
228 | template<typename o_node_t> |
---|
229 | const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); } |
---|
230 | |
---|
231 | /// Returns a mutable version of the pointer in this node. |
---|
232 | node_t * get_and_mutate(); |
---|
233 | |
---|
234 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere. |
---|
235 | /// Returns a mutable version of the pointer in this node. |
---|
236 | node_t * set_and_mutate( const node_t * n ); |
---|
237 | |
---|
238 | using ptr = const node_t *; |
---|
239 | |
---|
240 | private: |
---|
241 | void assign( const node_t * other ) { |
---|
242 | if( other ) _inc(other); |
---|
243 | if( node ) _dec(node ); |
---|
244 | node = other; |
---|
245 | } |
---|
246 | |
---|
247 | void _inc( const node_t * other ); |
---|
248 | void _dec( const node_t * other ); |
---|
249 | void _check() const; |
---|
250 | |
---|
251 | const node_t * node; |
---|
252 | }; |
---|
253 | |
---|
254 | /// Owning pointer to node |
---|
255 | template< typename node_t > |
---|
256 | using ptr = ptr_base< node_t, Node::ref_type::strong >; |
---|
257 | |
---|
258 | /// Observing pointer to node |
---|
259 | template< typename node_t > |
---|
260 | using readonly = ptr_base< node_t, Node::ref_type::weak >; |
---|
261 | } |
---|
262 | |
---|
263 | // Local Variables: // |
---|
264 | // tab-width: 4 // |
---|
265 | // mode: c++ // |
---|
266 | // compile-command: "make install" // |
---|
267 | // End: // |
---|