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