source: src/AST/Node.hpp@ 4d5c5b6a

Last change on this file since 4d5c5b6a was 16ba4897, checked in by Andrew Beach <ajbeach@…>, 12 months ago

Replaced SemanticErrorException::isEmpty with ...::throwIfNonEmpty. This is how it was used in every context and it saves a bit of text (if not two lines) at every use. I considered putting this function in the header for better inlining, but this should have at least the same preformance as the last version.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[2bb4a01]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
[1e97287]11// Last Modified By : Andrew Beach
[7edd5c1]12// Last Modified On : Mon May 9 10:20:00 2022
13// Update Count : 8
[2bb4a01]14//
15
[cedb545]16#pragma once
17
[2bb4a01]18#include <cassert>
[b8524ca]19#include <cstddef> // for nullptr_t
[2bb4a01]20#include <iosfwd>
[cedb545]21
[c92bdcc]22#include "Common/ErrorObjects.hpp" // for SemanticErrorException
[1346914]23
[cedb545]24namespace ast {
[2bb4a01]25
26class Visitor;
27
28/// Base class for all AST nodes.
29/// Keeps both strong and weak reference counts.
[f47f887]30class Node {
31public:
[14cebb7a]32 // override defaults to ensure assignment doesn't
[2bb4a01]33 // change/share reference counts
[bcb311b]34 Node() = default;
35 Node(const Node&) : strong_count(0), weak_count(0) {}
36 Node(Node&&) : strong_count(0), weak_count(0) {}
[a0d1f1c]37 Node& operator=(const Node&) = delete;
38 Node& operator=(Node&&) = delete;
[bcb311b]39 virtual ~Node() {}
[cedb545]40
[6d51bd7]41 virtual const Node * accept( Visitor & v ) const = 0;
[2bb4a01]42
43 /// Types of node references
[f47f887]44 enum class ref_type {
45 strong,
46 weak
47 };
[cedb545]48
[4864a73]49 bool unique() const { return strong_count == 1; }
[7edd5c1]50 bool isManaged() const { return strong_count > 0; }
51 bool isReferenced() const { return weak_count > 0; }
[3f681b1]52 bool isStable() const {
53 return (1 == strong_count || (1 < strong_count && 0 == weak_count));
54 }
[4864a73]55
[87701b6]56private:
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);
[99da267]63 template<typename node_t>
64 friend node_t * shallowCopy(const node_t * node);
[87701b6]65
66 mutable size_t strong_count = 0;
67 mutable size_t weak_count = 0;
[4864a73]68 mutable bool was_ever_strong = false;
[87701b6]69
70 void increment(ref_type ref) const {
[f47f887]71 switch (ref) {
[4864a73]72 case ref_type::strong: strong_count++; was_ever_strong = true; break;
[6d51bd7]73 case ref_type::weak : weak_count ++; break;
[cedb545]74 }
[f47f887]75 }
[cedb545]76
[7d0881c]77 void decrement(ast::Node::ref_type ref, bool do_delete = true) const {
[f47f887]78 switch (ref) {
[6d51bd7]79 case ref_type::strong: strong_count--; break;
80 case ref_type::weak : weak_count --; break;
[cedb545]81 }
82
[7d0881c]83 if( do_delete && !strong_count && !weak_count) {
[f47f887]84 delete this;
[6a625de]85 }
[f47f887]86 }
[6a625de]87
[87701b6]88 template< typename node_t, enum Node::ref_type ref_t >
89 friend class ptr_base;
[f47f887]90};
91
[b7d92b96]92/// Mutate a node, non-member function to avoid static type
93/// problems and be able to use auto return
[f47f887]94template<typename node_t>
[87701b6]95node_t * mutate( const node_t * node ) {
96 if (node->strong_count <= 1) {
[f47f887]97 return const_cast<node_t *>(node);
[6a625de]98 }
99
[f47f887]100 assertf(
101 node->weak_count == 0,
[2890212]102 "Error: mutating node with weak references to it will invalidate some references"
[f47f887]103 );
104 return node->clone();
105}
106
[b7d92b96]107/// Mutate a node field (only clones if not equal to existing value)
[2377ca2]108template<typename node_t, typename super_t, typename field_t, typename assn_t>
109const node_t * mutate_field( const node_t * node, field_t super_t::* field, assn_t && val ) {
[2d11663]110 // skip mutate if equivalent
[b7d92b96]111 if ( node->*field == val ) return node;
[2890212]112
[2d11663]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)
[2377ca2]120template<typename node_t, typename super_t, typename coll_t, typename ind_t, typename field_t>
[2d11663]121const node_t * mutate_field_index(
[2377ca2]122 const node_t * node, coll_t super_t::* field, ind_t i, field_t && val
[2d11663]123) {
124 // skip mutate if equivalent
125 if ( (node->*field)[i] == val ) return node;
126
127 // mutate and return
[b7d92b96]128 node_t * ret = mutate( node );
[2d11663]129 (ret->*field)[i] = std::forward< field_t >( val );
[b7d92b96]130 return ret;
131}
132
[e0e9a0b]133/// Mutate an entire indexed collection by cloning to accepted value
[2377ca2]134template<typename node_t, typename super_t, typename coll_t>
135const node_t * mutate_each( const node_t * node, coll_t super_t::* field, Visitor & v ) {
[e0e9a0b]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
[54e41b3]142std::ostream& operator<< ( std::ostream& out, const Node * node );
[2bb4a01]143
[d8938622]144/// Call a visitor on a possibly-null node
145template<typename node_t>
146auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) {
147 return n ? n->accept( v ) : nullptr;
148}
149
[1346914]150/// Call a visitor on a collection of nodes, throwing any exceptions when completed
151template< typename Container >
152void 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 }
[16ba4897]163 errors.throwIfNonEmpty();
[1346914]164}
165
[9e1d485]166/// Base class for the smart pointer types
167/// should never really be used.
168template< typename node_t, enum Node::ref_type ref_t >
[f47f887]169class ptr_base {
170public:
171 ptr_base() : node(nullptr) {}
[87701b6]172 ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
[dafe9e1]173 ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
[f47f887]174
[f685679]175 ptr_base( const ptr_base & o ) : node(o.node) {
176 if( node ) _inc(node);
177 }
178
[d76c588]179 ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
[f685679]180
[9e1d485]181 template< enum Node::ref_type o_ref_t >
[60aaa51d]182 ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
[87701b6]183 if( node ) _inc(node);
[6a625de]184 }
185
[9e1d485]186 template< enum Node::ref_type o_ref_t >
[60aaa51d]187 ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
[87701b6]188 if( node ) _inc(node);
[6d51bd7]189 }
190
[a488783]191 ptr_base & operator=( const node_t * node ) {
192 assign( node );
193 return *this;
194 }
195
[6d51bd7]196 template<typename o_node_t>
197 ptr_base & operator=( const o_node_t * node ) {
[dafe9e1]198 assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
[6d51bd7]199 return *this;
[f47f887]200 }
[cedb545]201
[b8524ca]202 ptr_base & operator=( std::nullptr_t ) {
203 if ( node ) _dec(node);
204 node = nullptr;
205 return *this;
206 }
207
[f685679]208 ptr_base & operator=( const ptr_base & o ) {
209 assign(o.node);
210 return *this;
211 }
212
213 ptr_base & operator=( ptr_base && o ) {
[d76c588]214 if ( node == o.node ) return *this;
215 if ( node ) _dec(node);
216 node = o.node;
217 o.node = nullptr;
[f685679]218 return *this;
219 }
220
[9e1d485]221 template< enum Node::ref_type o_ref_t >
[f47f887]222 ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
[60aaa51d]223 assign(o.get());
[f47f887]224 return *this;
225 }
[cedb545]226
[9e1d485]227 template< enum Node::ref_type o_ref_t >
[f47f887]228 ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
[60aaa51d]229 assign(o.get());
[f47f887]230 return *this;
231 }
[cedb545]232
[8631c84]233 /// Swaps the nodes contained within two pointers.
234 void swap( ptr_base & other ) noexcept;
235
[4864a73]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; }
[302ef2a]240 operator const node_t * () const & { _check(); return node; }
241 operator const node_t * () && = delete;
[cedb545]242
[7d0881c]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
[54e41b3]252 /// wrapper for convenient access to dynamic_cast
[fdbd4fd]253 template<typename o_node_t>
[4864a73]254 const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
[fdbd4fd]255
[52a4d69]256 /// Wrapper that makes sure dynamic_cast returns non-null.
[ee574a2]257 template<typename o_node_t>
[52a4d69]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; }
[ee574a2]266
[76ed81f]267 /// Returns a mutable version of the pointer in this node.
268 node_t * get_and_mutate();
269
[54e41b3]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.
[10a1225]272 node_t * set_and_mutate( const node_t * n );
[54e41b3]273
[f47f887]274 using ptr = const node_t *;
[cedb545]275
[f47f887]276private:
[9e1d485]277 void assign( const node_t * other ) {
[87701b6]278 if( other ) _inc(other);
279 if( node ) _dec(node );
[f47f887]280 node = other;
281 }
[cedb545]282
[87701b6]283 void _inc( const node_t * other );
[7d0881c]284 void _dec( const node_t * other, bool do_delete = true );
[4864a73]285 void _check() const;
[52a4d69]286 void _strict_fail() const __attribute__((noreturn));
[87701b6]287
[9e1d485]288 const node_t * node;
[f47f887]289};
[cedb545]290
[2bb4a01]291/// Owning pointer to node
[f47f887]292template< typename node_t >
293using ptr = ptr_base< node_t, Node::ref_type::strong >;
[cedb545]294
[2bb4a01]295/// Observing pointer to node
[f47f887]296template< typename node_t >
297using readonly = ptr_base< node_t, Node::ref_type::weak >;
[8631c84]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
[2bb4a01]305}
306
307// Local Variables: //
308// tab-width: 4 //
309// mode: c++ //
310// compile-command: "make install" //
[1e97287]311// End: //
Note: See TracBrowser for help on using the repository browser.