source: src/AST/Node.hpp@ 0026d67

ADT ast-experimental
Last change on this file since 0026d67 was 3f681b1, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Added the new invariant checks. There seems to be a few unset locations in the multi-level exit code.

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