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