source: src/AST/Node.hpp @ 111a2ab3

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 111a2ab3 was 2377ca2, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Updated some names on mutate functions to me more consistent with some 'better' names we came up with more recently.

  • Property mode set to 100644
File size: 8.4 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
[8631c84]12// Last Modified On : Fri Mar 25 10:33:00 2022
13// Update Count     : 7
[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)
[2377ca2]105template<typename node_t, typename super_t, typename field_t, typename assn_t>
106const node_t * mutate_field( const node_t * node, field_t super_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)
[2377ca2]117template<typename node_t, typename super_t, typename coll_t, typename ind_t, typename field_t>
[2d11663]118const node_t * mutate_field_index(
[2377ca2]119        const node_t * node, coll_t super_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
[2377ca2]131template<typename node_t, typename super_t, typename coll_t>
132const node_t * mutate_each( const node_t * node, coll_t super_t::* field, Visitor & v ) {
[e0e9a0b]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
[8631c84]232        /// Swaps the nodes contained within two pointers.
233        void swap( ptr_base & other ) noexcept;
234
[4864a73]235        const node_t * get() const { _check(); return  node; }
236        const node_t * operator->() const { _check(); return  node; }
237        const node_t & operator* () const { _check(); return *node; }
238        explicit operator bool() const { _check(); return node; }
[302ef2a]239        operator const node_t * () const & { _check(); return node; }
240        operator const node_t * () && = delete;
[cedb545]241
[7d0881c]242        const node_t * release() {
243                const node_t * ret = node;
244                if ( node ) {
245                        _dec(node, false);
246                        node = nullptr;
247                }
248                return ret;
249        }
250
[54e41b3]251        /// wrapper for convenient access to dynamic_cast
[fdbd4fd]252        template<typename o_node_t>
[4864a73]253        const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
[fdbd4fd]254
[52a4d69]255        /// Wrapper that makes sure dynamic_cast returns non-null.
[ee574a2]256        template<typename o_node_t>
[52a4d69]257        const o_node_t * strict_as() const {
258                if (const o_node_t * ret = as<o_node_t>()) return ret;
259                _strict_fail();
260        }
261
262        /// Wrapper that makes sure dynamic_cast does not fail.
263        template<typename o_node_t, decltype(nullptr) null>
264        const o_node_t * strict_as() const { return node ? strict_as<o_node_t>() : nullptr; }
[ee574a2]265
[76ed81f]266        /// Returns a mutable version of the pointer in this node.
267        node_t * get_and_mutate();
268
[54e41b3]269        /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
270        /// Returns a mutable version of the pointer in this node.
[10a1225]271        node_t * set_and_mutate( const node_t * n );
[54e41b3]272
[f47f887]273        using ptr = const node_t *;
[cedb545]274
[f47f887]275private:
[9e1d485]276        void assign( const node_t * other ) {
[87701b6]277                if( other ) _inc(other);
278                if( node  ) _dec(node );
[f47f887]279                node = other;
280        }
[cedb545]281
[87701b6]282        void _inc( const node_t * other );
[7d0881c]283        void _dec( const node_t * other, bool do_delete = true );
[4864a73]284        void _check() const;
[52a4d69]285        void _strict_fail() const __attribute__((noreturn));
[87701b6]286
[9e1d485]287        const node_t * node;
[f47f887]288};
[cedb545]289
[2bb4a01]290/// Owning pointer to node
[f47f887]291template< typename node_t >
292using ptr = ptr_base< node_t, Node::ref_type::strong >;
[cedb545]293
[2bb4a01]294/// Observing pointer to node
[f47f887]295template< typename node_t >
296using readonly = ptr_base< node_t, Node::ref_type::weak >;
[8631c84]297
298/// Non-member swap that an participate in overload resolution.
299template< typename node_t, enum Node::ref_type ref_t >
300void swap( ptr_base< node_t, ref_t > & l, ptr_base< node_t, ref_t > & r ) {
301        l.swap( r );
302}
303
[2bb4a01]304}
305
306// Local Variables: //
307// tab-width: 4 //
308// mode: c++ //
309// compile-command: "make install" //
[1e97287]310// End: //
Note: See TracBrowser for help on using the repository browser.