source: src/AST/Node.hpp@ 93744b5

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 93744b5 was 1346914, checked in by Aaron Moss <a3moss@…>, 6 years ago

Fix Mangler port to new AST

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