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