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