source: src/AST/Node.hpp@ 3613e25

ADT ast-experimental pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 3613e25 was 7edd5c1, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Assorted fixes to the AST, found while I was trying to add more invarant checks.

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