source: src/AST/Pass.proto.hpp@ 68c9165

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 68c9165 was 0b8bf27, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed pass visitor so previsit can return void to signify it will never mutate

  • Property mode set to 100644
File size: 11.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 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// Pass.impl.hpp --
8//
9// Author : Thierry Delisle
10// Created On : Thu May 09 15::37::05 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#pragma once
17// IWYU pragma: private, include "Pass.hpp"
18
19namespace ast {
20template<typename pass_type>
21class Pass;
22
23namespace __pass {
24 typedef std::function<void( void * )> cleanup_func_t;
25 typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
26
27
28 // boolean reference that may be null
29 // either refers to a boolean value or is null and returns true
30 class bool_ref {
31 public:
32 bool_ref() = default;
33 ~bool_ref() = default;
34
35 operator bool() { return m_ref ? *m_ref : true; }
36 bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
37
38 private:
39
40 friend class visit_children_guard;
41
42 bool * set( bool * val ) {
43 bool * prev = m_ref;
44 m_ref = val;
45 return prev;
46 }
47
48 bool * m_ref = nullptr;
49 };
50
51 // Implementation of the guard value
52 // Created inside the visit scope
53 class guard_value {
54 public:
55 /// Push onto the cleanup
56 guard_value( at_cleanup_t * at_cleanup ) {
57 if( at_cleanup ) {
58 *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
59 push( std::move( func ), val );
60 };
61 }
62 }
63
64 ~guard_value() {
65 while( !cleanups.empty() ) {
66 auto& cleanup = cleanups.top();
67 cleanup.func( cleanup.val );
68 cleanups.pop();
69 }
70 }
71
72 void push( cleanup_func_t && func, void* val ) {
73 cleanups.emplace( std::move(func), val );
74 }
75
76 private:
77 struct cleanup_t {
78 cleanup_func_t func;
79 void * val;
80
81 cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
82 };
83
84 std::stack< cleanup_t > cleanups;
85 };
86
87 // Guard structure implementation for whether or not children should be visited
88 class visit_children_guard {
89 public:
90
91 visit_children_guard( bool_ref * ref )
92 : m_val ( true )
93 , m_prev( ref ? ref->set( &m_val ) : nullptr )
94 , m_ref ( ref )
95 {}
96
97 ~visit_children_guard() {
98 if( m_ref ) {
99 m_ref->set( m_prev );
100 }
101 }
102
103 operator bool() { return m_val; }
104
105 private:
106 bool m_val;
107 bool * m_prev;
108 bool_ref * m_ref;
109 };
110
111 template<typename pass_t, typename node_t>
112 struct is_valid_previsit {
113 using ret_t = decltype( ((pass_t*)nullptr)->previsit( (const node_t *)nullptr ) );
114
115 static constexpr bool value = std::is_void< ret_t >::value ||
116 std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
117 };
118
119 template<bool is_void>
120 struct __assign;
121
122 template<>
123 struct __assign<true> {
124 template<typename pass_t, typename node_t>
125 static inline void result( pass_t & pass, const node_t * & node ) {
126 pass.previsit( node );
127 }
128 };
129
130 template<>
131 struct __assign<false> {
132 template<typename pass_t, typename node_t>
133 static inline void result( pass_t & pass, const node_t * & node ) {
134 node = pass.previsit( node );
135 assertf(node, "Previsit must not return NULL");
136 }
137 };
138
139 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
140 // Deep magic (a.k.a template meta programming) to make the templated visitor work
141 // Basically the goal is to make 2 previsit
142 // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
143 // 'pass.previsit( node )' that compiles will be used for that node for that type
144 // This requires that this option only compile for passes that actually define an appropriate visit.
145 // SFINAE will make sure the compilation errors in this function don't halt the build.
146 // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
147 // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
148 // This is needed only to eliminate the need for passes to specify any kind of handlers.
149 // The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
150 // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
151 // the first implementation takes priority in regards to overloading.
152 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
153 // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
154 template<typename pass_t, typename node_t>
155 static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
156 static_assert(
157 is_valid_previsit<pass_t, node_t>::value,
158 "Previsit may not change the type of the node. It must return its paremeter or void."
159 );
160
161 __assign<
162 std::is_void<
163 decltype( pass.previsit( node ) )
164 >::value
165 >::result( pass, node );
166 }
167
168 template<typename pass_t, typename node_t>
169 static inline auto previsit( pass_t &, const node_t *, long ) {}
170
171 // PostVisit : never mutates the passed pointer but may return a different node
172 template<typename pass_t, typename node_t>
173 static inline auto postvisit( pass_t & pass, const node_t * node, int ) ->
174 decltype( pass.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
175 {
176 return pass.postvisit( node );
177 }
178
179 template<typename pass_t, typename node_t>
180 static inline const node_t * postvisit( pass_t &, const node_t * node, long ) { return node; }
181
182 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
183 // Deep magic (a.k.a template meta programming) continued
184 // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
185 // from in order to get extra functionallity for example
186 // class ErrorChecker : WithShortCircuiting { ... };
187 // Pass<ErrorChecker> checker;
188 // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
189 // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
190 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
191 // For several accessories, the feature is enabled by detecting that a specific field is present
192 // Use a macro the encapsulate the logic of detecting a particular field
193 // The type is not strictly enforced but does match the accessory
194 #define FIELD_PTR( name, default_type ) \
195 template< typename pass_t > \
196 static inline auto name( pass_t & pass, int ) -> decltype( &pass.name ) { return &pass.name; } \
197 \
198 template< typename pass_t > \
199 static inline default_type * name( pass_t &, long ) { return nullptr; }
200
201 // List of fields and their expected types
202 FIELD_PTR( env, const ast::TypeSubstitution * )
203 FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
204 FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
205 FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
206 FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
207 FIELD_PTR( visit_children, __pass::bool_ref )
208 FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
209 FIELD_PTR( visitor, ast::Pass<pass_t> * const )
210
211 // Remove the macro to make sure we don't clash
212 #undef FIELD_PTR
213
214 // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
215 // All passes which have such functions are assumed desire this behaviour
216 // detect it using the same strategy
217 namespace scope {
218 template<typename pass_t>
219 static inline auto enter( pass_t & pass, int ) -> decltype( pass.beginScope(), void() ) {
220 pass.beginScope();
221 }
222
223 template<typename pass_t>
224 static inline void enter( pass_t &, long ) {}
225
226 template<typename pass_t>
227 static inline auto leave( pass_t & pass, int ) -> decltype( pass.endScope(), void() ) {
228 pass.endScope();
229 }
230
231 template<typename pass_t>
232 static inline void leave( pass_t &, long ) {}
233 };
234
235 // Finally certain pass desire an up to date indexer automatically
236 // detect the presence of a member name indexer and call all the members appropriately
237 namespace indexer {
238 // Some simple scoping rules
239 template<typename pass_t>
240 static inline auto enter( pass_t & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) {
241 pass.indexer.enterScope();
242 }
243
244 template<typename pass_t>
245 static inline auto enter( pass_t &, long ) {}
246
247 template<typename pass_t>
248 static inline auto leave( pass_t & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) {
249 pass.indexer.leaveScope();
250 }
251
252 template<typename pass_t>
253 static inline auto leave( pass_t &, long ) {}
254
255 // The indexer has 2 kind of functions mostly, 1 argument and 2 arguments
256 // Create macro to condense these common patterns
257 #define INDEXER_FUNC1( func, type ) \
258 template<typename pass_t> \
259 static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) {\
260 pass.indexer.func( arg ); \
261 } \
262 \
263 template<typename pass_t> \
264 static inline void func( pass_t &, long, type ) {}
265
266 #define INDEXER_FUNC2( func, type1, type2 ) \
267 template<typename pass_t> \
268 static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void () ) {\
269 pass.indexer.func( arg1, arg2 ); \
270 } \
271 \
272 template<typename pass_t> \
273 static inline void func( pass_t &, long, type1, type2 ) {}
274
275 INDEXER_FUNC1( addId , const DeclWithType * );
276 INDEXER_FUNC1( addType , const NamedTypeDecl * );
277 INDEXER_FUNC1( addStruct , const StructDecl * );
278 INDEXER_FUNC1( addEnum , const EnumDecl * );
279 INDEXER_FUNC1( addUnion , const UnionDecl * );
280 INDEXER_FUNC1( addTrait , const TraitDecl * );
281 INDEXER_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Node * );
282 INDEXER_FUNC2( addWith , const std::list < ptr<Expr> > &, const Node * );
283
284 // A few extra functions have more complicated behaviour, they are hand written
285 template<typename pass_t>
286 static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) {
287 ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
288 fwd->params = decl->params;
289 pass.indexer.addStruct( fwd );
290 }
291
292 template<typename pass_t>
293 static inline void addStructFwd( pass_t &, long, const ast::StructDecl * ) {}
294
295 template<typename pass_t>
296 static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) {
297 UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
298 fwd->params = decl->params;
299 pass.indexer.addUnion( fwd );
300 }
301
302 template<typename pass_t>
303 static inline void addUnionFwd( pass_t &, long, const ast::UnionDecl * ) {}
304
305 template<typename pass_t>
306 static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) {
307 if ( ! pass.indexer.lookupStruct( str ) ) {
308 pass.indexer.addStruct( str );
309 }
310 }
311
312 template<typename pass_t>
313 static inline void addStruct( pass_t &, long, const std::string & ) {}
314
315 template<typename pass_t>
316 static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
317 if ( ! pass.indexer.lookupUnion( str ) ) {
318 pass.indexer.addUnion( str );
319 }
320 }
321
322 template<typename pass_t>
323 static inline void addUnion( pass_t &, long, const std::string & ) {}
324
325 #undef INDEXER_FUNC1
326 #undef INDEXER_FUNC2
327 };
328};
329};
Note: See TracBrowser for help on using the repository browser.