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