source: src/AST/Pass.proto.hpp@ 451d958

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 451d958 was c600df1, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added ast::WithConstTranslationUnit to give access to the surrounding TranslationUnit.

  • Property mode set to 100644
File size: 15.6 KB
RevLine 
[04124c4]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
[f47f887]16#pragma once
17// IWYU pragma: private, include "Pass.hpp"
18
[c15085d]19#include "Common/Stats/Heap.h"
20
[f47f887]21namespace ast {
[7ff3e522]22template<typename core_t>
[04124c4]23class Pass;
[f47f887]24
[a056f56]25struct TranslationUnit;
[293dc1c]26
[d3aa64f1]27struct PureVisitor;
28
[04124c4]29namespace __pass {
30 typedef std::function<void( void * )> cleanup_func_t;
31 typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
[f47f887]32
33
[04124c4]34 // boolean reference that may be null
35 // either refers to a boolean value or is null and returns true
36 class bool_ref {
37 public:
38 bool_ref() = default;
39 ~bool_ref() = default;
[f47f887]40
[04124c4]41 operator bool() { return m_ref ? *m_ref : true; }
42 bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
[f47f887]43
[04124c4]44 private:
[f47f887]45
[04124c4]46 friend class visit_children_guard;
[f47f887]47
[04124c4]48 bool * set( bool * val ) {
49 bool * prev = m_ref;
50 m_ref = val;
51 return prev;
52 }
[f47f887]53
[04124c4]54 bool * m_ref = nullptr;
55 };
[f47f887]56
[04124c4]57 // Implementation of the guard value
58 // Created inside the visit scope
59 class guard_value {
60 public:
61 /// Push onto the cleanup
62 guard_value( at_cleanup_t * at_cleanup ) {
63 if( at_cleanup ) {
64 *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
65 push( std::move( func ), val );
66 };
[f47f887]67 }
[04124c4]68 }
[f47f887]69
[04124c4]70 ~guard_value() {
71 while( !cleanups.empty() ) {
72 auto& cleanup = cleanups.top();
73 cleanup.func( cleanup.val );
74 cleanups.pop();
[f47f887]75 }
[04124c4]76 }
[f47f887]77
[04124c4]78 void push( cleanup_func_t && func, void* val ) {
79 cleanups.emplace( std::move(func), val );
80 }
[f47f887]81
[04124c4]82 private:
83 struct cleanup_t {
84 cleanup_func_t func;
85 void * val;
[f47f887]86
[04124c4]87 cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
[f47f887]88 };
89
[c408483]90 std::stack< cleanup_t, std::vector<cleanup_t> > cleanups;
[04124c4]91 };
[f47f887]92
[04124c4]93 // Guard structure implementation for whether or not children should be visited
94 class visit_children_guard {
95 public:
[f47f887]96
[04124c4]97 visit_children_guard( bool_ref * ref )
98 : m_val ( true )
99 , m_prev( ref ? ref->set( &m_val ) : nullptr )
100 , m_ref ( ref )
101 {}
102
103 ~visit_children_guard() {
104 if( m_ref ) {
105 m_ref->set( m_prev );
[f47f887]106 }
[04124c4]107 }
[f47f887]108
[04124c4]109 operator bool() { return m_val; }
[f47f887]110
[04124c4]111 private:
112 bool m_val;
113 bool * m_prev;
114 bool_ref * m_ref;
115 };
[f47f887]116
[e4b6cf78]117 /// "Short hand" to check if this is a valid previsit function
118 /// Mostly used to make the static_assert look (and print) prettier
[7ff3e522]119 template<typename core_t, typename node_t>
[dff6452]120 struct is_valid_previsit {
[93f74c0f]121 using ret_t = decltype( std::declval<core_t*>()->previsit( std::declval<const node_t *>() ) );
[dff6452]122
123 static constexpr bool value = std::is_void< ret_t >::value ||
124 std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
125 };
126
[e4b6cf78]127 /// Used by previsit implementation
128 /// We need to reassign the result to 'node', unless the function
129 /// returns void, then we just leave 'node' unchanged
[0b8bf27]130 template<bool is_void>
131 struct __assign;
132
133 template<>
134 struct __assign<true> {
[7ff3e522]135 template<typename core_t, typename node_t>
136 static inline void result( core_t & core, const node_t * & node ) {
137 core.previsit( node );
[0b8bf27]138 }
139 };
140
141 template<>
142 struct __assign<false> {
[7ff3e522]143 template<typename core_t, typename node_t>
144 static inline void result( core_t & core, const node_t * & node ) {
145 node = core.previsit( node );
[0b8bf27]146 assertf(node, "Previsit must not return NULL");
147 }
148 };
149
[e4b6cf78]150 /// Used by postvisit implementation
151 /// We need to return the result unless the function
152 /// returns void, then we just return the original node
153 template<bool is_void>
154 struct __return;
155
156 template<>
157 struct __return<true> {
[7ff3e522]158 template<typename core_t, typename node_t>
159 static inline const node_t * result( core_t & core, const node_t * & node ) {
160 core.postvisit( node );
[e4b6cf78]161 return node;
162 }
163 };
164
165 template<>
166 struct __return<false> {
[7ff3e522]167 template<typename core_t, typename node_t>
168 static inline auto result( core_t & core, const node_t * & node ) {
169 return core.postvisit( node );
[e4b6cf78]170 }
171 };
172
[04124c4]173 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
174 // Deep magic (a.k.a template meta programming) to make the templated visitor work
175 // Basically the goal is to make 2 previsit
176 // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
177 // 'pass.previsit( node )' that compiles will be used for that node for that type
178 // This requires that this option only compile for passes that actually define an appropriate visit.
179 // SFINAE will make sure the compilation errors in this function don't halt the build.
180 // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
181 // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
182 // This is needed only to eliminate the need for passes to specify any kind of handlers.
183 // The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
184 // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
185 // the first implementation takes priority in regards to overloading.
186 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
187 // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
[7ff3e522]188 template<typename core_t, typename node_t>
189 static inline auto previsit( core_t & core, const node_t * & node, int ) -> decltype( core.previsit( node ), void() ) {
[b0abc8a0]190 static_assert(
[7ff3e522]191 is_valid_previsit<core_t, node_t>::value,
[dff6452]192 "Previsit may not change the type of the node. It must return its paremeter or void."
[b0abc8a0]193 );
[0b8bf27]194
195 __assign<
196 std::is_void<
[7ff3e522]197 decltype( core.previsit( node ) )
[0b8bf27]198 >::value
[7ff3e522]199 >::result( core, node );
[04124c4]200 }
201
[7ff3e522]202 template<typename core_t, typename node_t>
203 static inline auto previsit( core_t &, const node_t *, long ) {}
[04124c4]204
205 // PostVisit : never mutates the passed pointer but may return a different node
[7ff3e522]206 template<typename core_t, typename node_t>
207 static inline auto postvisit( core_t & core, const node_t * node, int ) ->
208 decltype( core.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
[dff6452]209 {
[e4b6cf78]210 return __return<
211 std::is_void<
[7ff3e522]212 decltype( core.postvisit( node ) )
[e4b6cf78]213 >::value
[7ff3e522]214 >::result( core, node );
[04124c4]215 }
216
[7ff3e522]217 template<typename core_t, typename node_t>
218 static inline const node_t * postvisit( core_t &, const node_t * node, long ) { return node; }
[04124c4]219
220 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
221 // Deep magic (a.k.a template meta programming) continued
222 // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
223 // from in order to get extra functionallity for example
224 // class ErrorChecker : WithShortCircuiting { ... };
225 // Pass<ErrorChecker> checker;
226 // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
227 // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
228 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
229 // For several accessories, the feature is enabled by detecting that a specific field is present
230 // Use a macro the encapsulate the logic of detecting a particular field
231 // The type is not strictly enforced but does match the accessory
232 #define FIELD_PTR( name, default_type ) \
[7ff3e522]233 template< typename core_t > \
234 static inline auto name( core_t & core, int ) -> decltype( &core.name ) { return &core.name; } \
[04124c4]235 \
[7ff3e522]236 template< typename core_t > \
237 static inline default_type * name( core_t &, long ) { return nullptr; }
[04124c4]238
239 // List of fields and their expected types
[b2a11ba]240 FIELD_PTR( typeSubs, const ast::TypeSubstitution * )
[04124c4]241 FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
242 FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
243 FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
244 FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
245 FIELD_PTR( visit_children, __pass::bool_ref )
246 FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
[7ff3e522]247 FIELD_PTR( visitor, ast::Pass<core_t> * const )
[04124c4]248
249 // Remove the macro to make sure we don't clash
250 #undef FIELD_PTR
251
[7ff3e522]252 template< typename core_t >
253 static inline auto beginTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
254 // Stats::Heap::stacktrace_push(core_t::traceId);
[c15085d]255 }
256
[7ff3e522]257 template< typename core_t >
258 static inline auto endTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
[0d070ca]259 // Stats::Heap::stacktrace_pop();
[c15085d]260 }
261
[7ff3e522]262 template< typename core_t >
263 static void beginTrace(core_t &, long) {}
[c15085d]264
[7ff3e522]265 template< typename core_t >
266 static void endTrace(core_t &, long) {}
[c15085d]267
[0dd9a5e]268 // Allows visitor to handle an error on top-level declarations, and possibly suppress the error.
269 // If onError() returns false, the error will be ignored. By default, it returns true.
270
271 template< typename core_t >
[e00c22f]272 static bool on_error (core_t &, ptr<Decl> &, long) { return true; }
[0dd9a5e]273
274 template< typename core_t >
[e00c22f]275 static auto on_error (core_t & core, ptr<Decl> & decl, int) -> decltype(core.on_error(decl)) {
[a056f56]276 return core.on_error(decl);
[0dd9a5e]277 }
278
[04124c4]279 // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
280 // All passes which have such functions are assumed desire this behaviour
281 // detect it using the same strategy
282 namespace scope {
[7ff3e522]283 template<typename core_t>
284 static inline auto enter( core_t & core, int ) -> decltype( core.beginScope(), void() ) {
285 core.beginScope();
[f47f887]286 }
287
[7ff3e522]288 template<typename core_t>
289 static inline void enter( core_t &, long ) {}
[f47f887]290
[7ff3e522]291 template<typename core_t>
292 static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) {
293 core.endScope();
[f47f887]294 }
295
[7ff3e522]296 template<typename core_t>
297 static inline void leave( core_t &, long ) {}
[e0e9a0b]298 } // namespace scope
[f47f887]299
[e0e9a0b]300 // Certain passes desire an up to date symbol table automatically
[0e42794]301 // detect the presence of a member name `symtab` and call all the members appropriately
302 namespace symtab {
[04124c4]303 // Some simple scoping rules
[7ff3e522]304 template<typename core_t>
305 static inline auto enter( core_t & core, int ) -> decltype( core.symtab, void() ) {
306 core.symtab.enterScope();
[04124c4]307 }
[f47f887]308
[7ff3e522]309 template<typename core_t>
310 static inline auto enter( core_t &, long ) {}
[f47f887]311
[7ff3e522]312 template<typename core_t>
313 static inline auto leave( core_t & core, int ) -> decltype( core.symtab, void() ) {
314 core.symtab.leaveScope();
[04124c4]315 }
[f47f887]316
[7ff3e522]317 template<typename core_t>
318 static inline auto leave( core_t &, long ) {}
[f47f887]319
[0e42794]320 // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments
[04124c4]321 // Create macro to condense these common patterns
[0e42794]322 #define SYMTAB_FUNC1( func, type ) \
[7ff3e522]323 template<typename core_t> \
324 static inline auto func( core_t & core, int, type arg ) -> decltype( core.symtab.func( arg ), void() ) {\
325 core.symtab.func( arg ); \
[04124c4]326 } \
327 \
[7ff3e522]328 template<typename core_t> \
329 static inline void func( core_t &, long, type ) {}
[04124c4]330
[0e42794]331 #define SYMTAB_FUNC2( func, type1, type2 ) \
[7ff3e522]332 template<typename core_t> \
333 static inline auto func( core_t & core, int, type1 arg1, type2 arg2 ) -> decltype( core.symtab.func( arg1, arg2 ), void () ) {\
334 core.symtab.func( arg1, arg2 ); \
[04124c4]335 } \
[f47f887]336 \
[7ff3e522]337 template<typename core_t> \
338 static inline void func( core_t &, long, type1, type2 ) {}
[04124c4]339
[0e42794]340 SYMTAB_FUNC1( addId , const DeclWithType * );
341 SYMTAB_FUNC1( addType , const NamedTypeDecl * );
342 SYMTAB_FUNC1( addStruct , const StructDecl * );
343 SYMTAB_FUNC1( addEnum , const EnumDecl * );
344 SYMTAB_FUNC1( addUnion , const UnionDecl * );
345 SYMTAB_FUNC1( addTrait , const TraitDecl * );
[396b830]346 SYMTAB_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Decl * );
[04124c4]347
348 // A few extra functions have more complicated behaviour, they are hand written
[7ff3e522]349 template<typename core_t>
350 static inline auto addStructFwd( core_t & core, int, const ast::StructDecl * decl ) -> decltype( core.symtab.addStruct( decl ), void() ) {
[6d51bd7]351 ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
[87701b6]352 fwd->params = decl->params;
[7ff3e522]353 core.symtab.addStruct( fwd );
[6d51bd7]354 }
355
[7ff3e522]356 template<typename core_t>
357 static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {}
[6d51bd7]358
[7ff3e522]359 template<typename core_t>
360 static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) {
[6d51bd7]361 UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
[87701b6]362 fwd->params = decl->params;
[7ff3e522]363 core.symtab.addUnion( fwd );
[6d51bd7]364 }
365
[7ff3e522]366 template<typename core_t>
367 static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {}
[6d51bd7]368
[7ff3e522]369 template<typename core_t>
370 static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) {
371 if ( ! core.symtab.lookupStruct( str ) ) {
372 core.symtab.addStruct( str );
[6d51bd7]373 }
374 }
375
[7ff3e522]376 template<typename core_t>
377 static inline void addStruct( core_t &, long, const std::string & ) {}
[6d51bd7]378
[7ff3e522]379 template<typename core_t>
380 static inline auto addUnion( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnion( str ), void() ) {
381 if ( ! core.symtab.lookupUnion( str ) ) {
382 core.symtab.addUnion( str );
[6d51bd7]383 }
384 }
385
[7ff3e522]386 template<typename core_t>
387 static inline void addUnion( core_t &, long, const std::string & ) {}
[04124c4]388
[0e42794]389 #undef SYMTAB_FUNC1
390 #undef SYMTAB_FUNC2
[e0e9a0b]391 } // namespace symtab
392
393 // Some passes need to mutate TypeDecl and properly update their pointing TypeInstType.
394 // Detect the presence of a member name `subs` and call all members appropriately
395 namespace forall {
396 // Some simple scoping rules
[7ff3e522]397 template<typename core_t>
[361bf01]398 static inline auto enter( core_t & core, int, const ast::FunctionType * type )
[7ff3e522]399 -> decltype( core.subs, void() ) {
400 if ( ! type->forall.empty() ) core.subs.beginScope();
[e0e9a0b]401 }
402
[7ff3e522]403 template<typename core_t>
[361bf01]404 static inline auto enter( core_t &, long, const ast::FunctionType * ) {}
[e0e9a0b]405
[7ff3e522]406 template<typename core_t>
[361bf01]407 static inline auto leave( core_t & core, int, const ast::FunctionType * type )
[7ff3e522]408 -> decltype( core.subs, void() ) {
409 if ( ! type->forall.empty() ) { core.subs.endScope(); }
[e0e9a0b]410 }
411
[7ff3e522]412 template<typename core_t>
[361bf01]413 static inline auto leave( core_t &, long, const ast::FunctionType * ) {}
[e0e9a0b]414
415 // Replaces a TypeInstType's base TypeDecl according to the table
[7ff3e522]416 template<typename core_t>
417 static inline auto replace( core_t & core, int, const ast::TypeInstType *& inst )
418 -> decltype( core.subs, void() ) {
[396b830]419 inst = ast::mutate_field(
[7ff3e522]420 inst, &ast::TypeInstType::base, core.subs.replace( inst->base ) );
[e0e9a0b]421 }
422
[7ff3e522]423 template<typename core_t>
424 static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {}
[e0e9a0b]425
426 } // namespace forall
[e6b42e7]427
[c600df1]428 // For passes that need access to the global context. Sreaches `translationUnit`
429 namespace translation_unit {
430 template<typename core_t>
431 static inline auto get_cptr( core_t & core, int )
432 -> decltype( &core.translationUnit ) {
433 return &core.translationUnit;
434 }
435
436 template<typename core_t>
437 static inline const TranslationUnit ** get_cptr( core_t &, long ) {
438 return nullptr;
439 }
440 }
441
[e6b42e7]442 template<typename core_t>
443 static inline auto get_result( core_t & core, char ) -> decltype( core.result() ) {
444 return core.result();
445 }
446
447 template<typename core_t>
448 static inline auto get_result( core_t & core, int ) -> decltype( core.result ) {
449 return core.result;
450 }
451
452 template<typename core_t>
453 static inline void get_result( core_t &, long ) {}
[e0e9a0b]454} // namespace __pass
[aebf5b0]455} // namespace ast
Note: See TracBrowser for help on using the repository browser.