source: src/AST/Node.hpp@ aba20d2

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 aba20d2 was 2d11663, checked in by Aaron Moss <a3moss@…>, 6 years ago

resolver porting; finish top level of initialization

  • Property mode set to 100644
File size: 6.8 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, typename assn_t>
102const node_t * mutate_field( const node_t * node, field_t node_t::* field, assn_t && val ) {
103 // skip mutate if equivalent
104 if ( node->*field == val ) return node;
105
106 // mutate and return
107 node_t * ret = mutate( node );
108 ret->*field = std::forward< assn_t >( val );
109 return ret;
110}
111
112/// Mutate a single index of a node field (only clones if not equal to existing value)
113template<typename node_t, typename coll_t, typename ind_t, typename field_t>
114const node_t * mutate_field_index(
115 const node_t * node, coll_t node_t::* field, ind_t i, field_t && val
116) {
117 // skip mutate if equivalent
118 if ( (node->*field)[i] == val ) return node;
119
120 // mutate and return
121 node_t * ret = mutate( node );
122 (ret->*field)[i] = std::forward< field_t >( val );
123 return ret;
124}
125
126std::ostream& operator<< ( std::ostream& out, const Node * node );
127
128/// Call a visitor on a possibly-null node
129template<typename node_t>
130auto maybe_accept( const node_t * n, Visitor & v ) -> decltype( n->accept(v) ) {
131 return n ? n->accept( v ) : nullptr;
132}
133
134/// Call a visitor on a collection of nodes, throwing any exceptions when completed
135template< typename Container >
136void accept_each( const Container & c, Visitor & v ) {
137 SemanticErrorException errors;
138 for ( const auto & i : c ) {
139 try {
140 if ( i ) {
141 i->accept( v );
142 }
143 } catch ( SemanticErrorException & e ) {
144 errors.append( e );
145 }
146 }
147 if ( ! errors.isEmpty() ) {
148 throw errors;
149 }
150}
151
152/// Base class for the smart pointer types
153/// should never really be used.
154template< typename node_t, enum Node::ref_type ref_t >
155class ptr_base {
156public:
157 ptr_base() : node(nullptr) {}
158 ptr_base( const node_t * n ) : node(n) { if( node ) _inc(node); }
159 ~ptr_base() { if( node ) { auto tmp = node; node = nullptr; _dec(tmp); } }
160
161 ptr_base( const ptr_base & o ) : node(o.node) {
162 if( node ) _inc(node);
163 }
164
165 ptr_base( ptr_base && o ) : node(o.node) { o.node = nullptr; }
166
167 template< enum Node::ref_type o_ref_t >
168 ptr_base( const ptr_base<node_t, o_ref_t> & o ) : node(o.get()) {
169 if( node ) _inc(node);
170 }
171
172 template< enum Node::ref_type o_ref_t >
173 ptr_base( ptr_base<node_t, o_ref_t> && o ) : node(o.get()) {
174 if( node ) _inc(node);
175 }
176
177 template<typename o_node_t>
178 ptr_base & operator=( const o_node_t * node ) {
179 assign( strict_dynamic_cast<const node_t *, nullptr>(node) );
180 return *this;
181 }
182
183 ptr_base & operator=( const ptr_base & o ) {
184 assign(o.node);
185 return *this;
186 }
187
188 ptr_base & operator=( ptr_base && o ) {
189 if ( node == o.node ) return *this;
190 if ( node ) _dec(node);
191 node = o.node;
192 o.node = nullptr;
193 return *this;
194 }
195
196 template< enum Node::ref_type o_ref_t >
197 ptr_base & operator=( const ptr_base<node_t, o_ref_t> & o ) {
198 assign(o.get());
199 return *this;
200 }
201
202 template< enum Node::ref_type o_ref_t >
203 ptr_base & operator=( ptr_base<node_t, o_ref_t> && o ) {
204 assign(o.get());
205 return *this;
206 }
207
208 const node_t * get() const { _check(); return node; }
209 const node_t * operator->() const { _check(); return node; }
210 const node_t & operator* () const { _check(); return *node; }
211 explicit operator bool() const { _check(); return node; }
212 operator const node_t * () const { _check(); return node; }
213
214 /// wrapper for convenient access to dynamic_cast
215 template<typename o_node_t>
216 const o_node_t * as() const { _check(); return dynamic_cast<const o_node_t *>(node); }
217
218 /// wrapper for convenient access to strict_dynamic_cast
219 template<typename o_node_t>
220 const o_node_t * strict_as() const { _check(); return strict_dynamic_cast<const o_node_t *>(node); }
221
222 /// Returns a mutable version of the pointer in this node.
223 node_t * get_and_mutate();
224
225 /// Sets this pointer to a mutated version of a pointer (possibly) owned elsehere.
226 /// Returns a mutable version of the pointer in this node.
227 node_t * set_and_mutate( const node_t * n );
228
229 using ptr = const node_t *;
230
231private:
232 void assign( const node_t * other ) {
233 if( other ) _inc(other);
234 if( node ) _dec(node );
235 node = other;
236 }
237
238 void _inc( const node_t * other );
239 void _dec( const node_t * other );
240 void _check() const;
241
242 const node_t * node;
243};
244
245/// Owning pointer to node
246template< typename node_t >
247using ptr = ptr_base< node_t, Node::ref_type::strong >;
248
249/// Observing pointer to node
250template< typename node_t >
251using readonly = ptr_base< node_t, Node::ref_type::weak >;
252}
253
254// Local Variables: //
255// tab-width: 4 //
256// mode: c++ //
257// compile-command: "make install" //
258// End: //
Note: See TracBrowser for help on using the repository browser.