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