source: src/AST/Node.hpp@ cef7430

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since cef7430 was a488783, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Translated the first half of validate-D. HoistControlStruct is pretty much the same, Autogen has changed a lot due to the changes in the AST.

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