source: src/AST/Node.hpp @ 37b7d95

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 37b7d95 was 490fb92e, checked in by Fangren Yu <f37yu@…>, 3 years ago

move FixInit? to new ast

  • Property mode set to 100644
File size: 8.0 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 : Fri Jun 5 9:47:00 2020
13// Update Count     : 6
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
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);
60        template<typename node_t>
61        friend node_t * shallowCopy(const node_t * node);
62
63        mutable size_t strong_count = 0;
64        mutable size_t weak_count = 0;
65        mutable bool was_ever_strong = false;
66
67        void increment(ref_type ref) const {
68                switch (ref) {
69                        case ref_type::strong: strong_count++; was_ever_strong = true; break;
70                        case ref_type::weak  : weak_count  ++; break;
71                }
72        }
73
74        void decrement(ast::Node::ref_type ref, bool do_delete = true) const {
75                switch (ref) {
76                        case ref_type::strong: strong_count--; break;
77                        case ref_type::weak  : weak_count  --; break;
78                }
79
80                if( do_delete && !strong_count && !weak_count) {
81                        delete this;
82                }
83        }
84
85        template< typename node_t, enum Node::ref_type ref_t >
86        friend class ptr_base;
87};
88
89/// Mutate a node, non-member function to avoid static type
90/// problems and be able to use auto return
91template<typename node_t>
92node_t * mutate( const node_t * node ) {
93        if (node->strong_count <= 1) {
94                return const_cast<node_t *>(node);
95        }
96
97        assertf(
98                node->weak_count == 0,
99                "Error: mutating node with weak references to it will invalidate some references"
100        );
101        return node->clone();
102}
103
104/// Mutate a node field (only clones if not equal to existing value)
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 ) {
107        // skip mutate if equivalent
108        if ( node->*field == val ) return node;
109
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)
117template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t>
118const node_t * mutate_field_index(
119        const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val
120) {
121        // skip mutate if equivalent
122        if  ( (node->*field)[i] == val ) return node;
123
124        // mutate and return
125        node_t * ret = mutate( node );
126        (ret->*field)[i] = std::forward< field_t >( val );
127        return ret;
128}
129
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
139std::ostream& operator<< ( std::ostream& out, const Node * node );
140
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
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
165/// Base class for the smart pointer types
166/// should never really be used.
167template< typename node_t, enum Node::ref_type ref_t >
168class ptr_base {
169public:
170        ptr_base() : node(nullptr) {}
171        ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
172        ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
173
174        ptr_base( const ptr_base & o ) : node(o.node) {
175                if( node ) _inc(node);
176        }
177
178        ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
179
180        template< enum Node::ref_type o_ref_t >
181        ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
182                if( node ) _inc(node);
183        }
184
185        template< enum Node::ref_type o_ref_t >
186        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
187                if( node ) _inc(node);
188        }
189
190        template<typename o_node_t>
191        ptr_base & operator=( const o_node_t * node ) {
192                assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
193                return *this;
194        }
195
196        ptr_base & operator=( std::nullptr_t ) {
197                if ( node ) _dec(node);
198                node = nullptr;
199                return *this;
200        }
201
202        ptr_base & operator=( const ptr_base & o ) {
203                assign(o.node);
204                return *this;
205        }
206
207        ptr_base & operator=( ptr_base && o ) {
208                if ( node == o.node ) return *this;
209                if ( node ) _dec(node);
210                node = o.node;
211                o.node = nullptr;
212                return *this;
213        }
214
215        template< enum Node::ref_type o_ref_t >
216        ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
217                assign(o.get());
218                return *this;
219        }
220
221        template< enum Node::ref_type o_ref_t >
222        ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
223                assign(o.get());
224                return *this;
225        }
226
227        const node_t * get() const { _check(); return  node; }
228        const node_t * operator->() const { _check(); return  node; }
229        const node_t & operator* () const { _check(); return *node; }
230        explicit operator bool() const { _check(); return node; }
231        operator const node_t * () const { _check(); return node; }
232
233        const node_t * release() {
234                const node_t * ret = node;
235                if ( node ) {
236                        _dec(node, false);
237                        node = nullptr;
238                }
239                return ret;
240        }
241
242        /// wrapper for convenient access to dynamic_cast
243        template<typename o_node_t>
244        const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
245
246        /// Wrapper that makes sure dynamic_cast returns non-null.
247        template<typename o_node_t>
248        const o_node_t * strict_as() const {
249                if (const o_node_t * ret = as<o_node_t>()) return ret;
250                _strict_fail();
251        }
252
253        /// Wrapper that makes sure dynamic_cast does not fail.
254        template<typename o_node_t, decltype(nullptr) null>
255        const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; }
256
257        /// Returns a mutable version of the pointer in this node.
258        node_t * get_and_mutate();
259
260        /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
261        /// Returns a mutable version of the pointer in this node.
262        node_t * set_and_mutate( const node_t * n );
263
264        using ptr = const node_t *;
265
266private:
267        void assign( const node_t * other ) {
268                if( other ) _inc(other);
269                if( node  ) _dec(node );
270                node = other;
271        }
272
273        void _inc( const node_t * other );
274        void _dec( const node_t * other, bool do_delete = true );
275        void _check() const;
276        void _strict_fail() const __attribute__((noreturn));
277
278        const node_t * node;
279};
280
281/// Owning pointer to node
282template< typename node_t >
283using ptr = ptr_base< node_t, Node::ref_type::strong >;
284
285/// Observing pointer to node
286template< typename node_t >
287using readonly = ptr_base< node_t, Node::ref_type::weak >;
288}
289
290// Local Variables: //
291// tab-width: 4 //
292// mode: c++ //
293// compile-command: "make install" //
294// End: //
Note: See TracBrowser for help on using the repository browser.