source: src/AST/Node.hpp@ a62749f

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since a62749f was 60aaa51d, checked in by Aaron Moss <a3moss@…>, 6 years ago

More resolver porting; mostly CurrentObject

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