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