source: src/AST/Node.hpp @ 4864a73

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4864a73 was 4864a73, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added chain mutation and example use in resolver

  • Property mode set to 100644
File size: 5.9 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>
19#include <iosfwd>
[cedb545]20
[1346914]21#include "Common/ErrorObjects.h"  // for SemanticErrorException
22
[cedb545]23namespace ast {
[2bb4a01]24
25class Visitor;
26
27/// Base class for all AST nodes.
28/// Keeps both strong and weak reference counts.
[f47f887]29class Node {
30public:
[14cebb7a]31        // override defaults to ensure assignment doesn't
[2bb4a01]32        // change/share reference counts
33        Node() = default;
[6d51bd7]34        Node(const Node&) : strong_count(0), weak_count(0) {}
35        Node(Node&&) : strong_count(0), weak_count(0) {}
[2bb4a01]36        Node& operator= (const Node&) = delete;
37        Node& operator= (Node&&) = delete;
[f47f887]38        virtual ~Node() = default;
[cedb545]39
[6d51bd7]40        virtual const Node * accept( Visitor & v ) const = 0;
[2bb4a01]41
42        /// Types of node references
[f47f887]43        enum class ref_type {
44                strong,
45                weak
46        };
[cedb545]47
[4864a73]48        bool unique() const { return strong_count == 1; }
49
[87701b6]50private:
51        /// Make a copy of this node; should be overridden in subclass with more precise return type
52        virtual Node * clone() const = 0;
53
54        /// Must be copied in ALL derived classes
55        template<typename node_t>
56        friend node_t * mutate(const node_t * node);
57
58        mutable size_t strong_count = 0;
59        mutable size_t weak_count = 0;
[4864a73]60        mutable bool was_ever_strong = false;
[87701b6]61
62        void increment(ref_type ref) const {
[f47f887]63                switch (ref) {
[4864a73]64                        case ref_type::strong: strong_count++; was_ever_strong = true; break;
[6d51bd7]65                        case ref_type::weak  : weak_count  ++; break;
[cedb545]66                }
[f47f887]67        }
[cedb545]68
[87701b6]69        void decrement(ast::Node::ref_type ref) const {
[f47f887]70                switch (ref) {
[6d51bd7]71                        case ref_type::strong: strong_count--; break;
72                        case ref_type::weak  : weak_count  --; break;
[cedb545]73                }
74
[6d51bd7]75                if(!strong_count && !weak_count) {
[f47f887]76                        delete this;
[6a625de]77                }
[f47f887]78        }
[6a625de]79
[87701b6]80        template< typename node_t, enum Node::ref_type ref_t >
81        friend class ptr_base;
[f47f887]82};
83
84// Mutate a node, non-member function to avoid static type
85// problems and be able to use auto return
86template<typename node_t>
[87701b6]87node_t * mutate( const node_t * node ) {
88        if (node->strong_count <= 1) {
[f47f887]89                return const_cast<node_t *>(node);
[6a625de]90        }
91
[f47f887]92        assertf(
93                node->weak_count == 0,
94                "Error: mutating node with weak references to it will invalided some references"
95        );
96        return node->clone();
97}
98
[54e41b3]99std::ostream& operator<< ( std::ostream& out, const Node * node );
[2bb4a01]100
[d8938622]101/// Call a visitor on a possibly-null node
102template<typename node_t>
103auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) {
104        return n ? n->accept( v ) : nullptr;
105}
106
[1346914]107/// Call a visitor on a collection of nodes, throwing any exceptions when completed
108template< typename Container >
109void accept_each( const Container & c, Visitor & v ) {
110        SemanticErrorException errors;
111        for ( const auto & i : c ) {
112                try {
113                        if ( i ) {
114                                i->accept( v );
115                        }
116                } catch ( SemanticErrorException & e ) {
117                        errors.append( e );
118                }
119        }
120        if ( ! errors.isEmpty() ) {
121                throw errors;
122        }
123}
124
[9e1d485]125/// Base class for the smart pointer types
126/// should never really be used.
127template< typename node_t, enum Node::ref_type ref_t >
[f47f887]128class ptr_base {
129public:
130        ptr_base() : node(nullptr) {}
[87701b6]131        ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
[dafe9e1]132        ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
[f47f887]133
[f685679]134        ptr_base( const ptr_base & o ) : node(o.node) {
135                if( node ) _inc(node);
136        }
137
[d76c588]138        ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
[f685679]139
[9e1d485]140        template< enum Node::ref_type o_ref_t >
[f47f887]141        ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
[87701b6]142                if( node ) _inc(node);
[6a625de]143        }
144
[9e1d485]145        template< enum Node::ref_type o_ref_t >
[f47f887]146        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
[87701b6]147                if( node ) _inc(node);
[6d51bd7]148        }
149
150        template<typename o_node_t>
151        ptr_base & operator=( const o_node_t * node ) {
[dafe9e1]152                assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
[6d51bd7]153                return *this;
[f47f887]154        }
[cedb545]155
[f685679]156        ptr_base & operator=( const ptr_base & o ) {
157                assign(o.node);
158                return *this;
159        }
160
161        ptr_base & operator=( ptr_base && o ) {
[d76c588]162                if ( node == o.node ) return *this;
163                if ( node ) _dec(node);
164                node = o.node;
165                o.node = nullptr;
[f685679]166                return *this;
167        }
168
[9e1d485]169        template< enum Node::ref_type o_ref_t >
[f47f887]170        ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
171                assign(o.node);
172                return *this;
173        }
[cedb545]174
[9e1d485]175        template< enum Node::ref_type o_ref_t >
[f47f887]176        ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
177                assign(o.node);
178                return *this;
179        }
[cedb545]180
[4864a73]181        const node_t * get() const { _check(); return  node; }
182        const node_t * operator->() const { _check(); return  node; }
183        const node_t & operator* () const { _check(); return *node; }
184        explicit operator bool() const { _check(); return node; }
185        operator const node_t * () const { _check(); return node; }
[cedb545]186
[54e41b3]187        /// wrapper for convenient access to dynamic_cast
[fdbd4fd]188        template<typename o_node_t>
[4864a73]189        const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
[fdbd4fd]190
[ee574a2]191        /// wrapper for convenient access to strict_dynamic_cast
192        template<typename o_node_t>
193        const o_node_t * strict_as() const { return strict_dynamic_cast<const o_node_t *>(node); }
194
[76ed81f]195        /// Returns a mutable version of the pointer in this node.
196        node_t * get_and_mutate();
197
[54e41b3]198        /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
199        /// Returns a mutable version of the pointer in this node.
[10a1225]200        node_t * set_and_mutate( const node_t * n );
[54e41b3]201
[f47f887]202        using ptr = const node_t *;
[cedb545]203
[f47f887]204private:
[9e1d485]205        void assign( const node_t * other ) {
[87701b6]206                if( other ) _inc(other);
207                if( node  ) _dec(node );
[f47f887]208                node = other;
209        }
[cedb545]210
[87701b6]211        void _inc( const node_t * other );
212        void _dec( const node_t * other );
[4864a73]213        void _check() const;
[87701b6]214
[f47f887]215protected:
[9e1d485]216        const node_t * node;
[f47f887]217};
[cedb545]218
[2bb4a01]219/// Owning pointer to node
[f47f887]220template< typename node_t >
221using ptr = ptr_base< node_t, Node::ref_type::strong >;
[cedb545]222
[2bb4a01]223/// Observing pointer to node
[f47f887]224template< typename node_t >
225using readonly = ptr_base< node_t, Node::ref_type::weak >;
[2bb4a01]226}
227
228// Local Variables: //
229// tab-width: 4 //
230// mode: c++ //
231// compile-command: "make install" //
[1e97287]232// End: //
Note: See TracBrowser for help on using the repository browser.