source: src/AST/Node.hpp @ 55b6476

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 55b6476 was 7d0881c, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Aaron added decrement-without delete option to Node infrastructure

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