source: src/AST/Node.hpp@ 1e97287

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 1e97287 was 1e97287, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Re-created the statement section of the AST.

  • Property mode set to 100644
File size: 4.2 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 : Wed May 15 16:02:00 2019
13// Update Count : 3
14//
15
16#pragma once
17
18#include <cassert>
19#include <iosfwd>
20
21namespace ast {
22
23class Visitor;
24
25/// Base class for all AST nodes.
26/// Keeps both strong and weak reference counts.
27class Node {
28public:
29 // override defaults to ensure assignment doesn't
30 // change/share reference counts
31 Node() = default;
32 Node(const Node&) : strong_count(0), weak_count(0) {}
33 Node(Node&&) : strong_count(0), weak_count(0) {}
34 Node& operator= (const Node&) = delete;
35 Node& operator= (Node&&) = delete;
36 virtual ~Node() = default;
37
38 virtual const Node * accept( Visitor & v ) const = 0;
39
40 /// Types of node references
41 enum class ref_type {
42 strong,
43 weak
44 };
45
46 inline void increment(ref_type ref) const {
47 switch (ref) {
48 case ref_type::strong: strong_count++; break;
49 case ref_type::weak : weak_count ++; break;
50 }
51 }
52
53 inline void decrement(ref_type ref) const {
54 switch (ref) {
55 case ref_type::strong: strong_count--; break;
56 case ref_type::weak : weak_count --; break;
57 }
58
59 if(!strong_count && !weak_count) {
60 delete this;
61 }
62 }
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;
66
67 /// Must be copied in ALL derived classes
68 template<typename node_t>
69 friend auto mutate(const node_t * node);
70
71 mutable size_t strong_count = 0;
72 mutable size_t weak_count = 0;
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);
85 }
86
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
94std::ostream& operator<< ( std::ostream& out, const Node* node );
95
96// Base class for the smart pointer types
97// should never really be used.
98template< typename node_t, enum Node::ref_type ref_t>
99class ptr_base {
100public:
101 ptr_base() : node(nullptr) {}
102 ptr_base( const node_t * n ) : node(n) { if( !node ) increment(node, ref_t); }
103 ~ptr_base() { if( node ) decrement(node, ref_t); }
104
105 template< enum Node::ref_type o_ref_t >
106 ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.node) {
107 if( !node ) return;
108 increment(node, ref_t);
109 }
110
111 template< enum Node::ref_type o_ref_t >
112 ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.node) {
113 if( node ) increment(node, ref_t);
114 }
115
116 template<typename o_node_t>
117 ptr_base & operator=( const o_node_t * node ) {
118 assign(strict_dynamic_cast<const node_t *>(node));
119 return *this;
120 }
121
122 template< enum Node::ref_type o_ref_t >
123 ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
124 assign(o.node);
125 return *this;
126 }
127
128 template< enum Node::ref_type o_ref_t >
129 ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
130 if(o.node == node) return *this;
131 assign(o.node);
132 return *this;
133 }
134
135 const node_t * get() const { return node; }
136 const node_t * operator->() const { return node; }
137 const node_t & operator* () const { return *node; }
138 explicit operator bool() const { return node; }
139 operator const node_t *() const { return node; }
140
141 template<typename o_node_t>
142 const o_node_t * as() const { return dynamic_cast<o_node_t *>(node); }
143
144 using ptr = const node_t *;
145
146private:
147 void assign(const node_t * other ) {
148 if( other ) increment(other, ref_t);
149 if( node ) decrement(node , ref_t);
150 node = other;
151 }
152
153protected:
154 const node_t * node;
155};
156
157/// Owning pointer to node
158template< typename node_t >
159using ptr = ptr_base< node_t, Node::ref_type::strong >;
160
161/// Observing pointer to node
162template< typename node_t >
163using readonly = ptr_base< node_t, Node::ref_type::weak >;
164}
165
166// Local Variables: //
167// tab-width: 4 //
168// mode: c++ //
169// compile-command: "make install" //
170// End: //
Note: See TracBrowser for help on using the repository browser.