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 Jun 5 9:47:00 2020
|
---|
13 | // Update Count : 6
|
---|
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 that makes sure dynamic_cast returns non-null.
|
---|
246 | template<typename o_node_t>
|
---|
247 | const o_node_t * strict_as() const {
|
---|
248 | if (const o_node_t * ret = as<o_node_t>()) return ret;
|
---|
249 | _strict_fail();
|
---|
250 | }
|
---|
251 |
|
---|
252 | /// Wrapper that makes sure dynamic_cast does not fail.
|
---|
253 | template<typename o_node_t, decltype(nullptr) null>
|
---|
254 | const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; }
|
---|
255 |
|
---|
256 | /// Returns a mutable version of the pointer in this node.
|
---|
257 | node_t * get_and_mutate();
|
---|
258 |
|
---|
259 | /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
|
---|
260 | /// Returns a mutable version of the pointer in this node.
|
---|
261 | node_t * set_and_mutate( const node_t * n );
|
---|
262 |
|
---|
263 | using ptr = const node_t *;
|
---|
264 |
|
---|
265 | private:
|
---|
266 | void assign( const node_t * other ) {
|
---|
267 | if( other ) _inc(other);
|
---|
268 | if( node ) _dec(node );
|
---|
269 | node = other;
|
---|
270 | }
|
---|
271 |
|
---|
272 | void _inc( const node_t * other );
|
---|
273 | void _dec( const node_t * other, bool do_delete = true );
|
---|
274 | void _check() const;
|
---|
275 | void _strict_fail() const __attribute__((noreturn));
|
---|
276 |
|
---|
277 | const node_t * node;
|
---|
278 | };
|
---|
279 |
|
---|
280 | /// Owning pointer to node
|
---|
281 | template< typename node_t >
|
---|
282 | using ptr = ptr_base< node_t, Node::ref_type::strong >;
|
---|
283 |
|
---|
284 | /// Observing pointer to node
|
---|
285 | template< typename node_t >
|
---|
286 | using readonly = ptr_base< node_t, Node::ref_type::weak >;
|
---|
287 | }
|
---|
288 |
|
---|
289 | // Local Variables: //
|
---|
290 | // tab-width: 4 //
|
---|
291 | // mode: c++ //
|
---|
292 | // compile-command: "make install" //
|
---|
293 | // End: //
|
---|