| 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.proto.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 | 
 | 
|---|
| 19 | #include "Common/Iterate.hpp"
 | 
|---|
| 20 | #include "Common/Stats/Heap.hpp"
 | 
|---|
| 21 | #include "Common/Utility.hpp"
 | 
|---|
| 22 | namespace ast {
 | 
|---|
| 23 |         template<typename core_t> class Pass;
 | 
|---|
| 24 |         class TranslationUnit;
 | 
|---|
| 25 |         struct PureVisitor;
 | 
|---|
| 26 |         template<typename node_t> node_t * deepCopy( const node_t * );
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #ifdef PEDANTIC_PASS_ASSERT
 | 
|---|
| 30 | #define __pedantic_pass_assert(...) assert(__VA_ARGS__)
 | 
|---|
| 31 | #define __pedantic_pass_assertf(...) assertf(__VA_ARGS__)
 | 
|---|
| 32 | #else
 | 
|---|
| 33 | #define __pedantic_pass_assert(...)
 | 
|---|
| 34 | #define __pedantic_pass_assertf(...)
 | 
|---|
| 35 | #endif
 | 
|---|
| 36 | 
 | 
|---|
| 37 | namespace ast::__pass {
 | 
|---|
| 38 | 
 | 
|---|
| 39 | typedef std::function<void( void * )> cleanup_func_t;
 | 
|---|
| 40 | typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
 | 
|---|
| 41 | 
 | 
|---|
| 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 {
 | 
|---|
| 48 | public:
 | 
|---|
| 49 |         /// Push onto the cleanup
 | 
|---|
| 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 |                 };
 | 
|---|
| 54 |         }
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         ~value_guard() {
 | 
|---|
| 57 |                 while( !cleanups.empty() ) {
 | 
|---|
| 58 |                         auto& cleanup = cleanups.top();
 | 
|---|
| 59 |                         cleanup.func( cleanup.val );
 | 
|---|
| 60 |                         cleanups.pop();
 | 
|---|
| 61 |                 }
 | 
|---|
| 62 |         }
 | 
|---|
| 63 | 
 | 
|---|
| 64 |         void push( cleanup_func_t && func, void* val ) {
 | 
|---|
| 65 |                 cleanups.emplace( std::move(func), val );
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | private:
 | 
|---|
| 69 |         struct cleanup_t {
 | 
|---|
| 70 |                 cleanup_func_t func;
 | 
|---|
| 71 |                 void * val;
 | 
|---|
| 72 | 
 | 
|---|
| 73 |                 cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
 | 
|---|
| 74 |         };
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         std::stack< cleanup_t, std::vector<cleanup_t> > cleanups;
 | 
|---|
| 77 | };
 | 
|---|
| 78 | 
 | 
|---|
| 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 >
 | 
|---|
| 86 |         void apply( object_t * object, field_t super_t::* field ) {
 | 
|---|
| 87 |                 object->*field = value;
 | 
|---|
| 88 |         }
 | 
|---|
| 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 | 
 | 
|---|
| 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) {}
 | 
|---|
| 102 |         };
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         bool differs = false;
 | 
|---|
| 105 |         container_t< delta > values;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         template< typename object_t, typename super_t, typename field_t >
 | 
|---|
| 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 |         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         template< template<class...> class incontainer_t >
 | 
|---|
| 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 {
 | 
|---|
| 134 |                                 return delta( stmt.release() );
 | 
|---|
| 135 |                         });
 | 
|---|
| 136 |                 stmts->clear();
 | 
|---|
| 137 |                 differs = true;
 | 
|---|
| 138 |         }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |         template< template<class...> class incontainer_t >
 | 
|---|
| 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();
 | 
|---|
| 147 |                                 return delta( new DeclStmt( d->location, d ) );
 | 
|---|
| 148 |                         });
 | 
|---|
| 149 |                 decls->clear();
 | 
|---|
| 150 |                 differs = true;
 | 
|---|
| 151 |         }
 | 
|---|
| 152 | };
 | 
|---|
| 153 | 
 | 
|---|
| 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;
 | 
|---|
| 159 | 
 | 
|---|
| 160 |         template< typename object_t, typename super_t, typename field_t >
 | 
|---|
| 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 |         }
 | 
|---|
| 173 | };
 | 
|---|
| 174 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 );
 | 
|---|
| 213 |                 assertf( node, "Previsit must not return nullptr." );
 | 
|---|
| 214 |         }
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 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 | {
 | 
|---|
| 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 |         }
 | 
|---|
| 233 | }
 | 
|---|
| 234 | 
 | 
|---|
| 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 > > )
 | 
|---|
| 263 | FIELD_PTR( visit_children, bool )
 | 
|---|
| 264 | FIELD_PTR( visitor, ast::Pass<core_t> * const )
 | 
|---|
| 265 | FIELD_PTR( translationUnit, const TranslationUnit * )
 | 
|---|
| 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 | }
 | 
|---|
| 274 | 
 | 
|---|
| 275 | template< typename core_t >
 | 
|---|
| 276 | static inline auto endTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
 | 
|---|
| 277 |         // Stats::Heap::stacktrace_pop();
 | 
|---|
| 278 | }
 | 
|---|
| 279 | 
 | 
|---|
| 280 | template< typename core_t >
 | 
|---|
| 281 | static void beginTrace(core_t &, long) {}
 | 
|---|
| 282 | 
 | 
|---|
| 283 | template< typename core_t >
 | 
|---|
| 284 | static void endTrace(core_t &, long) {}
 | 
|---|
| 285 | 
 | 
|---|
| 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.
 | 
|---|
| 288 | 
 | 
|---|
| 289 | template< typename core_t >
 | 
|---|
| 290 | static bool on_error (core_t &, ptr<Decl> &, long) { return true; }
 | 
|---|
| 291 | 
 | 
|---|
| 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 | }
 | 
|---|
| 296 | 
 | 
|---|
| 297 | // These are the guards declared at the beginning of a visit.
 | 
|---|
| 298 | // They are replaced with empty objects when not used.
 | 
|---|
| 299 | 
 | 
|---|
| 300 | template< typename core_t, typename node_t >
 | 
|---|
| 301 | static inline auto make_location_guard( core_t & core, node_t * node, int )
 | 
|---|
| 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 | }
 | 
|---|
| 307 | 
 | 
|---|
| 308 | template< typename core_t, typename node_t >
 | 
|---|
| 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 )
 | 
|---|
| 328 |                 -> decltype( __pass::value_guard( core.at_cleanup ) ) {
 | 
|---|
| 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();
 | 
|---|
| 336 | }
 | 
|---|
| 337 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 346 | 
 | 
|---|
| 347 |         template<typename core_t>
 | 
|---|
| 348 |         static inline void enter( core_t &, long ) {}
 | 
|---|
| 349 | 
 | 
|---|
| 350 |         template<typename core_t>
 | 
|---|
| 351 |         static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) {
 | 
|---|
| 352 |                 core.endScope();
 | 
|---|
| 353 |         }
 | 
|---|
| 354 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 367 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 375 | 
 | 
|---|
| 376 |         template<typename core_t>
 | 
|---|
| 377 |         static inline auto leave( core_t &, long ) {}
 | 
|---|
| 378 | 
 | 
|---|
| 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() ) );
 | 
|---|
| 413 |                 }
 | 
|---|
| 414 |                 core.symtab.addStruct( fwd );
 | 
|---|
| 415 |         }
 | 
|---|
| 416 | 
 | 
|---|
| 417 |         template<typename core_t>
 | 
|---|
| 418 |         static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {}
 | 
|---|
| 419 | 
 | 
|---|
| 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() ) );
 | 
|---|
| 425 |                 }
 | 
|---|
| 426 |                 core.symtab.addUnion( fwd );
 | 
|---|
| 427 |         }
 | 
|---|
| 428 | 
 | 
|---|
| 429 |         template<typename core_t>
 | 
|---|
| 430 |         static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {}
 | 
|---|
| 431 | 
 | 
|---|
| 432 |         template<typename core_t>
 | 
|---|
| 433 |         static inline auto addStructId( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStructId( str ), void() ) {
 | 
|---|
| 434 |                 if ( ! core.symtab.lookupStruct( str ) ) {
 | 
|---|
| 435 |                         core.symtab.addStructId( str );
 | 
|---|
| 436 |                 }
 | 
|---|
| 437 |         }
 | 
|---|
| 438 | 
 | 
|---|
| 439 |         template<typename core_t>
 | 
|---|
| 440 |         static inline void addStructId( core_t &, long, const std::string & ) {}
 | 
|---|
| 441 | 
 | 
|---|
| 442 |         template<typename core_t>
 | 
|---|
| 443 |         static inline auto addUnionId( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnionId( str ), void() ) {
 | 
|---|
| 444 |                 if ( ! core.symtab.lookupUnion( str ) ) {
 | 
|---|
| 445 |                         core.symtab.addUnionId( str );
 | 
|---|
| 446 |                 }
 | 
|---|
| 447 |         }
 | 
|---|
| 448 | 
 | 
|---|
| 449 |         template<typename core_t>
 | 
|---|
| 450 |         static inline void addUnionId( core_t &, long, const std::string & ) {}
 | 
|---|
| 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 |         }
 | 
|---|
| 465 | 
 | 
|---|
| 466 |         template<typename core_t>
 | 
|---|
| 467 |         static inline auto enter( core_t &, long, const ast::FunctionType * ) {}
 | 
|---|
| 468 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 474 | 
 | 
|---|
| 475 |         template<typename core_t>
 | 
|---|
| 476 |         static inline auto leave( core_t &, long, const ast::FunctionType * ) {}
 | 
|---|
| 477 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 485 | 
 | 
|---|
| 486 |         template<typename core_t>
 | 
|---|
| 487 |         static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {}
 | 
|---|
| 488 | } // namespace forall
 | 
|---|
| 489 | 
 | 
|---|
| 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 |         }
 | 
|---|
| 496 | 
 | 
|---|
| 497 |         template<typename core_t>
 | 
|---|
| 498 |         static inline auto get( core_t & core, int ) -> decltype( core.result ) {
 | 
|---|
| 499 |                 return core.result;
 | 
|---|
| 500 |         }
 | 
|---|
| 501 | 
 | 
|---|
| 502 |         template<typename core_t>
 | 
|---|
| 503 |         static inline void get( core_t &, long ) {}
 | 
|---|
| 504 | }
 | 
|---|
| 505 | 
 | 
|---|
| 506 | } // namespace ast::__pass
 | 
|---|
| 507 | 
 | 
|---|
| 508 | #undef __pedantic_pass_assertf
 | 
|---|
| 509 | #undef __pedantic_pass_assert
 | 
|---|