source: src/AST/Node.hpp @ e0d19f8

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e0d19f8 was 89c2f7c9, checked in by Aaron Moss <a3moss@…>, 5 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 4.2 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
12// Last Modified On : Wed May 15 16:02:00 2019
13// Update Count     : 3
[2bb4a01]14//
15
[cedb545]16#pragma once
17
[2bb4a01]18#include <cassert>
19#include <iosfwd>
[cedb545]20
21namespace ast {
[2bb4a01]22
23class Visitor;
24
25/// Base class for all AST nodes.
26/// Keeps both strong and weak reference counts.
[f47f887]27class Node {
28public:
[14cebb7a]29        // override defaults to ensure assignment doesn't
[2bb4a01]30        // change/share reference counts
31        Node() = default;
[6d51bd7]32        Node(const Node&) : strong_count(0), weak_count(0) {}
33        Node(Node&&) : strong_count(0), weak_count(0) {}
[2bb4a01]34        Node& operator= (const Node&) = delete;
35        Node& operator= (Node&&) = delete;
[f47f887]36        virtual ~Node() = default;
[cedb545]37
[6d51bd7]38        virtual const Node * accept( Visitor & v ) const = 0;
[2bb4a01]39
40        /// Types of node references
[f47f887]41        enum class ref_type {
42                strong,
43                weak
44        };
[cedb545]45
[f47f887]46        inline void increment(ref_type ref) const {
47                switch (ref) {
[6d51bd7]48                        case ref_type::strong: strong_count++; break;
49                        case ref_type::weak  : weak_count  ++; break;
[cedb545]50                }
[f47f887]51        }
[cedb545]52
[f47f887]53        inline void decrement(ref_type ref) const {
54                switch (ref) {
[6d51bd7]55                        case ref_type::strong: strong_count--; break;
56                        case ref_type::weak  : weak_count  --; break;
[cedb545]57                }
58
[6d51bd7]59                if(!strong_count && !weak_count) {
[f47f887]60                        delete this;
[6a625de]61                }
[f47f887]62        }
[6d51bd7]63private:
64        /// Make a copy of this node; should be overridden in subclass with more precise return type
65        virtual const Node * clone() const = 0;
[6a625de]66
[6d51bd7]67        /// Must be copied in ALL derived classes
[f47f887]68        template<typename node_t>
69        friend auto mutate(const node_t * node);
70
[6d51bd7]71        mutable size_t strong_count = 0;
72        mutable size_t weak_count = 0;
[f47f887]73};
74
75// Mutate a node, non-member function to avoid static type
76// problems and be able to use auto return
77template<typename node_t>
78auto mutate(const node_t * node) {
79        assertf(
80                node->strong_count >= 1,
81                "Error: attempting to mutate a node that appears to have been linked"
82        );
83        if (node->strong_count == 1) {
84                return const_cast<node_t *>(node);
[6a625de]85        }
86
[f47f887]87        assertf(
88                node->weak_count == 0,
89                "Error: mutating node with weak references to it will invalided some references"
90        );
91        return node->clone();
92}
93
[2bb4a01]94std::ostream& operator<< ( std::ostream& out, const Node* node );
95
[9e1d485]96/// Base class for the smart pointer types
97/// should never really be used.
98template< typename node_t, enum Node::ref_type ref_t >
[f47f887]99class ptr_base {
100public:
101        ptr_base() : node(nullptr) {}
[69bafd2]102        ptr_base( const node_t * n ) : node(n) { if( node ) increment(node, ref_t); }
[6d51bd7]103        ~ptr_base() { if( node ) decrement(node, ref_t); }
[f47f887]104
[9e1d485]105        template< enum Node::ref_type o_ref_t >
[f47f887]106        ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
[69bafd2]107                if( node ) increment(node, ref_t);
[6a625de]108        }
109
[9e1d485]110        template< enum Node::ref_type o_ref_t >
[f47f887]111        ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
[6d51bd7]112                if( node ) increment(node, ref_t);
113        }
114
115        template<typename o_node_t>
116        ptr_base & operator=( const o_node_t * node ) {
117                assign(strict_dynamic_cast<const node_t *>(node));
118                return *this;
[f47f887]119        }
[cedb545]120
[9e1d485]121        template< enum Node::ref_type o_ref_t >
[f47f887]122        ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
123                assign(o.node);
124                return *this;
125        }
[cedb545]126
[9e1d485]127        template< enum Node::ref_type o_ref_t >
[f47f887]128        ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
129                assign(o.node);
130                return *this;
131        }
[cedb545]132
[f47f887]133        const node_t * get() const { return  node; }
134        const node_t * operator->() const { return  node; }
135        const node_t & operator* () const { return *node; }
136        explicit operator bool() const { return node; }
[3648d98]137        operator const node_t * () const { return node; }
[cedb545]138
[fdbd4fd]139        template<typename o_node_t>
[9e1d485]140        const o_node_t * as() const { return dynamic_cast<const o_node_t *>(node); }
[fdbd4fd]141
[f47f887]142        using ptr = const node_t *;
[cedb545]143
[f47f887]144private:
[9e1d485]145        void assign( const node_t * other ) {
[6d51bd7]146                if( other ) increment(other, ref_t);
147                if( node  ) decrement(node , ref_t);
[f47f887]148                node = other;
149        }
[cedb545]150
[f47f887]151protected:
[9e1d485]152        const node_t * node;
[f47f887]153};
[cedb545]154
[2bb4a01]155/// Owning pointer to node
[f47f887]156template< typename node_t >
157using ptr = ptr_base< node_t, Node::ref_type::strong >;
[cedb545]158
[2bb4a01]159/// Observing pointer to node
[f47f887]160template< typename node_t >
161using readonly = ptr_base< node_t, Node::ref_type::weak >;
[2bb4a01]162}
163
164// Local Variables: //
165// tab-width: 4 //
166// mode: c++ //
167// compile-command: "make install" //
[1e97287]168// End: //
Note: See TracBrowser for help on using the repository browser.