source: src/AST/Node.hpp @ 7d0881c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 7d0881c 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
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
[dafe9e1]12// Last Modified On : Mon Jun  3 13:26:00 2019
13// Update Count     : 5
[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; }
51
[87701b6]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;
[4864a73]62        mutable bool was_ever_strong = false;
[87701b6]63
64        void increment(ref_type ref) const {
[f47f887]65                switch (ref) {
[4864a73]66                        case ref_type::strong: strong_count++; was_ever_strong = true; break;
[6d51bd7]67                        case ref_type::weak  : weak_count  ++; break;
[cedb545]68                }
[f47f887]69        }
[cedb545]70
[7d0881c]71        void decrement(ast::Node::ref_type ref, bool do_delete = true) const {
[f47f887]72                switch (ref) {
[6d51bd7]73                        case ref_type::strong: strong_count--; break;
74                        case ref_type::weak  : weak_count  --; break;
[cedb545]75                }
76
[7d0881c]77                if( do_delete && !strong_count && !weak_count) {
[f47f887]78                        delete this;
[6a625de]79                }
[f47f887]80        }
[6a625de]81
[87701b6]82        template< typename node_t, enum Node::ref_type ref_t >
83        friend class ptr_base;
[f47f887]84};
85
[b7d92b96]86/// Mutate a node, non-member function to avoid static type
87/// problems and be able to use auto return
[f47f887]88template<typename node_t>
[87701b6]89node_t * mutate( const node_t * node ) {
90        if (node->strong_count <= 1) {
[f47f887]91                return const_cast<node_t *>(node);
[6a625de]92        }
93
[f47f887]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
[b7d92b96]101/// Mutate a node field (only clones if not equal to existing value)
[18e683b]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 ) {
[2d11663]104        // skip mutate if equivalent
[b7d92b96]105        if ( node->*field == val ) return node;
106       
[2d11663]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)
[18e683b]114template<typename node_t, typename parent_t, typename coll_t, typename ind_t, typename field_t>
[2d11663]115const node_t * mutate_field_index(
[18e683b]116        const node_t * node, coll_t parent_t::* field, ind_t i, field_t && val
[2d11663]117) {
118        // skip mutate if equivalent
119        if  ( (node->*field)[i] == val ) return node;
120
121        // mutate and return
[b7d92b96]122        node_t * ret = mutate( node );
[2d11663]123        (ret->*field)[i] = std::forward< field_t >( val );
[b7d92b96]124        return ret;
125}
126
[e0e9a0b]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
[54e41b3]136std::ostream& operator<< ( std::ostream& out, const Node * node );
[2bb4a01]137
[d8938622]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
[1346914]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
[9e1d485]162/// Base class for the smart pointer types
163/// should never really be used.
164template< typename node_t, enum Node::ref_type ref_t >
[f47f887]165class ptr_base {
166public:
167        ptr_base() : node(nullptr) {}
[87701b6]168        ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
[dafe9e1]169        ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
[f47f887]170
[f685679]171        ptr_base( const ptr_base & o ) : node(o.node) {
172                if( node ) _inc(node);
173        }
174
[d76c588]175        ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
[f685679]176
[9e1d485]177        template< enum Node::ref_type o_ref_t >
[60aaa51d]178        ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
[87701b6]179                if( node ) _inc(node);
[6a625de]180        }
181
[9e1d485]182        template< enum Node::ref_type o_ref_t >
[60aaa51d]183        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
[87701b6]184                if( node ) _inc(node);
[6d51bd7]185        }
186
187        template<typename o_node_t>
188        ptr_base & operator=( const o_node_t * node ) {
[dafe9e1]189                assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
[6d51bd7]190                return *this;
[f47f887]191        }
[cedb545]192
[b8524ca]193        ptr_base & operator=( std::nullptr_t ) {
194                if ( node ) _dec(node);
195                node = nullptr;
196                return *this;
197        }
198
[f685679]199        ptr_base & operator=( const ptr_base & o ) {
200                assign(o.node);
201                return *this;
202        }
203
204        ptr_base & operator=( ptr_base && o ) {
[d76c588]205                if ( node == o.node ) return *this;
206                if ( node ) _dec(node);
207                node = o.node;
208                o.node = nullptr;
[f685679]209                return *this;
210        }
211
[9e1d485]212        template< enum Node::ref_type o_ref_t >
[f47f887]213        ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
[60aaa51d]214                assign(o.get());
[f47f887]215                return *this;
216        }
[cedb545]217
[9e1d485]218        template< enum Node::ref_type o_ref_t >
[f47f887]219        ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
[60aaa51d]220                assign(o.get());
[f47f887]221                return *this;
222        }
[cedb545]223
[4864a73]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; }
[cedb545]229
[7d0881c]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
[54e41b3]239        /// wrapper for convenient access to dynamic_cast
[fdbd4fd]240        template<typename o_node_t>
[4864a73]241        const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
[fdbd4fd]242
[ee574a2]243        /// wrapper for convenient access to strict_dynamic_cast
244        template<typename o_node_t>
[546e712]245        const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); }
[ee574a2]246
[76ed81f]247        /// Returns a mutable version of the pointer in this node.
248        node_t * get_and_mutate();
249
[54e41b3]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.
[10a1225]252        node_t * set_and_mutate( const node_t * n );
[54e41b3]253
[f47f887]254        using ptr = const node_t *;
[cedb545]255
[f47f887]256private:
[9e1d485]257        void assign( const node_t * other ) {
[87701b6]258                if( other ) _inc(other);
259                if( node  ) _dec(node );
[f47f887]260                node = other;
261        }
[cedb545]262
[87701b6]263        void _inc( const node_t * other );
[7d0881c]264        void _dec( const node_t * other, bool do_delete = true );
[4864a73]265        void _check() const;
[87701b6]266
[9e1d485]267        const node_t * node;
[f47f887]268};
[cedb545]269
[2bb4a01]270/// Owning pointer to node
[f47f887]271template< typename node_t >
272using ptr = ptr_base< node_t, Node::ref_type::strong >;
[cedb545]273
[2bb4a01]274/// Observing pointer to node
[f47f887]275template< typename node_t >
276using readonly = ptr_base< node_t, Node::ref_type::weak >;
[2bb4a01]277}
278
279// Local Variables: //
280// tab-width: 4 //
281// mode: c++ //
282// compile-command: "make install" //
[1e97287]283// End: //
Note: See TracBrowser for help on using the repository browser.