[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 |
|
---|
| 19 | namespace ast {
|
---|
[04124c4] | 20 | template<typename pass_type>
|
---|
| 21 | class Pass;
|
---|
[f47f887] | 22 |
|
---|
[04124c4] | 23 | namespace __pass {
|
---|
| 24 | typedef std::function<void( void * )> cleanup_func_t;
|
---|
| 25 | typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
|
---|
[f47f887] | 26 |
|
---|
| 27 |
|
---|
[04124c4] | 28 | // boolean reference that may be null
|
---|
| 29 | // either refers to a boolean value or is null and returns true
|
---|
| 30 | class bool_ref {
|
---|
| 31 | public:
|
---|
| 32 | bool_ref() = default;
|
---|
| 33 | ~bool_ref() = default;
|
---|
[f47f887] | 34 |
|
---|
[04124c4] | 35 | operator bool() { return m_ref ? *m_ref : true; }
|
---|
| 36 | bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
|
---|
[f47f887] | 37 |
|
---|
[04124c4] | 38 | private:
|
---|
[f47f887] | 39 |
|
---|
[04124c4] | 40 | friend class visit_children_guard;
|
---|
[f47f887] | 41 |
|
---|
[04124c4] | 42 | bool * set( bool * val ) {
|
---|
| 43 | bool * prev = m_ref;
|
---|
| 44 | m_ref = val;
|
---|
| 45 | return prev;
|
---|
| 46 | }
|
---|
[f47f887] | 47 |
|
---|
[04124c4] | 48 | bool * m_ref = nullptr;
|
---|
| 49 | };
|
---|
[f47f887] | 50 |
|
---|
[04124c4] | 51 | // Implementation of the guard value
|
---|
| 52 | // Created inside the visit scope
|
---|
| 53 | class guard_value {
|
---|
| 54 | public:
|
---|
| 55 | /// Push onto the cleanup
|
---|
| 56 | guard_value( at_cleanup_t * at_cleanup ) {
|
---|
| 57 | if( at_cleanup ) {
|
---|
| 58 | *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
|
---|
| 59 | push( std::move( func ), val );
|
---|
| 60 | };
|
---|
[f47f887] | 61 | }
|
---|
[04124c4] | 62 | }
|
---|
[f47f887] | 63 |
|
---|
[04124c4] | 64 | ~guard_value() {
|
---|
| 65 | while( !cleanups.empty() ) {
|
---|
| 66 | auto& cleanup = cleanups.top();
|
---|
| 67 | cleanup.func( cleanup.val );
|
---|
| 68 | cleanups.pop();
|
---|
[f47f887] | 69 | }
|
---|
[04124c4] | 70 | }
|
---|
[f47f887] | 71 |
|
---|
[04124c4] | 72 | void push( cleanup_func_t && func, void* val ) {
|
---|
| 73 | cleanups.emplace( std::move(func), val );
|
---|
| 74 | }
|
---|
[f47f887] | 75 |
|
---|
[04124c4] | 76 | private:
|
---|
| 77 | struct cleanup_t {
|
---|
| 78 | cleanup_func_t func;
|
---|
| 79 | void * val;
|
---|
[f47f887] | 80 |
|
---|
[04124c4] | 81 | cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
|
---|
[f47f887] | 82 | };
|
---|
| 83 |
|
---|
[04124c4] | 84 | std::stack< cleanup_t > cleanups;
|
---|
| 85 | };
|
---|
[f47f887] | 86 |
|
---|
[04124c4] | 87 | // Guard structure implementation for whether or not children should be visited
|
---|
| 88 | class visit_children_guard {
|
---|
| 89 | public:
|
---|
[f47f887] | 90 |
|
---|
[04124c4] | 91 | visit_children_guard( bool_ref * ref )
|
---|
| 92 | : m_val ( true )
|
---|
| 93 | , m_prev( ref ? ref->set( &m_val ) : nullptr )
|
---|
| 94 | , m_ref ( ref )
|
---|
| 95 | {}
|
---|
| 96 |
|
---|
| 97 | ~visit_children_guard() {
|
---|
| 98 | if( m_ref ) {
|
---|
| 99 | m_ref->set( m_prev );
|
---|
[f47f887] | 100 | }
|
---|
[04124c4] | 101 | }
|
---|
[f47f887] | 102 |
|
---|
[04124c4] | 103 | operator bool() { return m_val; }
|
---|
[f47f887] | 104 |
|
---|
[04124c4] | 105 | private:
|
---|
| 106 | bool m_val;
|
---|
| 107 | bool * m_prev;
|
---|
| 108 | bool_ref * m_ref;
|
---|
| 109 | };
|
---|
[f47f887] | 110 |
|
---|
[04124c4] | 111 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 112 | // Deep magic (a.k.a template meta programming) to make the templated visitor work
|
---|
| 113 | // Basically the goal is to make 2 previsit
|
---|
| 114 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
|
---|
| 115 | // 'pass.previsit( node )' that compiles will be used for that node for that type
|
---|
| 116 | // This requires that this option only compile for passes that actually define an appropriate visit.
|
---|
| 117 | // SFINAE will make sure the compilation errors in this function don't halt the build.
|
---|
| 118 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
|
---|
| 119 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
|
---|
| 120 | // This is needed only to eliminate the need for passes to specify any kind of handlers.
|
---|
| 121 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
|
---|
| 122 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
|
---|
| 123 | // the first implementation takes priority in regards to overloading.
|
---|
| 124 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 125 | // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
|
---|
| 126 | template<typename pass_t, typename node_t>
|
---|
| 127 | static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
|
---|
| 128 | node = pass.previsit( node );
|
---|
| 129 | assert(node);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | template<typename pass_t, typename node_t>
|
---|
| 133 | static inline auto previsit( pass_t &, const node_t *, long ) {}
|
---|
| 134 |
|
---|
| 135 | // PostVisit : never mutates the passed pointer but may return a different node
|
---|
| 136 | template<typename pass_t, typename node_t>
|
---|
| 137 | static inline auto postvisit( pass_t & pass, const node_t * node, int ) -> decltype( pass.postvisit( node ), (const node_t *)nullptr ) {
|
---|
| 138 | return pass.postvisit( node );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | template<typename pass_t, typename node_t>
|
---|
| 142 | static inline const node_t * postvisit( pass_t &, const node_t * node, long ) { return node; }
|
---|
| 143 |
|
---|
| 144 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 145 | // Deep magic (a.k.a template meta programming) continued
|
---|
| 146 | // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
|
---|
| 147 | // from in order to get extra functionallity for example
|
---|
| 148 | // class ErrorChecker : WithShortCircuiting { ... };
|
---|
| 149 | // Pass<ErrorChecker> checker;
|
---|
| 150 | // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
|
---|
| 151 | // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
|
---|
| 152 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 153 | // For several accessories, the feature is enabled by detecting that a specific field is present
|
---|
| 154 | // Use a macro the encapsulate the logic of detecting a particular field
|
---|
| 155 | // The type is not strictly enforced but does match the accessory
|
---|
| 156 | #define FIELD_PTR( name, default_type ) \
|
---|
| 157 | template< typename pass_t > \
|
---|
| 158 | static inline auto name( pass_t & pass, int ) -> decltype( &pass.name ) { return &pass.name; } \
|
---|
| 159 | \
|
---|
| 160 | template< typename pass_t > \
|
---|
| 161 | static inline default_type * name( pass_t &, long ) { return nullptr; }
|
---|
| 162 |
|
---|
| 163 | // List of fields and their expected types
|
---|
[6d51bd7] | 164 | FIELD_PTR( env, const ast::TypeSubstitution * )
|
---|
[04124c4] | 165 | FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
|
---|
| 166 | FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
|
---|
| 167 | FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
|
---|
| 168 | FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
|
---|
| 169 | FIELD_PTR( visit_children, __pass::bool_ref )
|
---|
| 170 | FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
|
---|
| 171 | FIELD_PTR( visitor, ast::Pass<pass_t> * const )
|
---|
| 172 |
|
---|
| 173 | // Remove the macro to make sure we don't clash
|
---|
| 174 | #undef FIELD_PTR
|
---|
| 175 |
|
---|
| 176 | // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
|
---|
| 177 | // All passes which have such functions are assumed desire this behaviour
|
---|
| 178 | // detect it using the same strategy
|
---|
| 179 | namespace scope {
|
---|
| 180 | template<typename pass_t>
|
---|
| 181 | static inline auto enter( pass_t & pass, int ) -> decltype( pass.beginScope(), void() ) {
|
---|
| 182 | pass.beginScope();
|
---|
[f47f887] | 183 | }
|
---|
| 184 |
|
---|
[04124c4] | 185 | template<typename pass_t>
|
---|
| 186 | static inline void enter( pass_t &, long ) {}
|
---|
[f47f887] | 187 |
|
---|
[04124c4] | 188 | template<typename pass_t>
|
---|
| 189 | static inline auto leave( pass_t & pass, int ) -> decltype( pass.endScope(), void() ) {
|
---|
| 190 | pass.endScope();
|
---|
[f47f887] | 191 | }
|
---|
| 192 |
|
---|
[04124c4] | 193 | template<typename pass_t>
|
---|
| 194 | static inline void leave( pass_t &, long ) {}
|
---|
| 195 | };
|
---|
[f47f887] | 196 |
|
---|
[04124c4] | 197 | // Finally certain pass desire an up to date indexer automatically
|
---|
| 198 | // detect the presence of a member name indexer and call all the members appropriately
|
---|
| 199 | namespace indexer {
|
---|
| 200 | // Some simple scoping rules
|
---|
| 201 | template<typename pass_t>
|
---|
| 202 | static inline auto enter( pass_t & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) {
|
---|
| 203 | pass.indexer.enterScope();
|
---|
| 204 | }
|
---|
[f47f887] | 205 |
|
---|
[04124c4] | 206 | template<typename pass_t>
|
---|
| 207 | static inline auto enter( pass_t &, long ) {}
|
---|
[f47f887] | 208 |
|
---|
[04124c4] | 209 | template<typename pass_t>
|
---|
| 210 | static inline auto leave( pass_t & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) {
|
---|
| 211 | pass.indexer.leaveScope();
|
---|
| 212 | }
|
---|
[f47f887] | 213 |
|
---|
[04124c4] | 214 | template<typename pass_t>
|
---|
| 215 | static inline auto leave( pass_t &, long ) {}
|
---|
[f47f887] | 216 |
|
---|
[04124c4] | 217 | // The indexer has 2 kind of functions mostly, 1 argument and 2 arguments
|
---|
| 218 | // Create macro to condense these common patterns
|
---|
| 219 | #define INDEXER_FUNC1( func, type ) \
|
---|
| 220 | template<typename pass_t> \
|
---|
| 221 | static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) {\
|
---|
| 222 | pass.indexer.func( arg ); \
|
---|
| 223 | } \
|
---|
| 224 | \
|
---|
| 225 | template<typename pass_t> \
|
---|
| 226 | static inline void func( pass_t &, long, type ) {}
|
---|
| 227 |
|
---|
| 228 | #define INDEXER_FUNC2( func, type1, type2 ) \
|
---|
| 229 | template<typename pass_t> \
|
---|
| 230 | static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void () ) {\
|
---|
| 231 | pass.indexer.func( arg1, arg2 ); \
|
---|
| 232 | } \
|
---|
[f47f887] | 233 | \
|
---|
[04124c4] | 234 | template<typename pass_t> \
|
---|
| 235 | static inline void func( pass_t &, long, type1, type2 ) {}
|
---|
| 236 |
|
---|
[6d51bd7] | 237 | INDEXER_FUNC1( addId , const DeclWithType * );
|
---|
| 238 | INDEXER_FUNC1( addType , const NamedTypeDecl * );
|
---|
| 239 | INDEXER_FUNC1( addStruct , const StructDecl * );
|
---|
| 240 | INDEXER_FUNC1( addEnum , const EnumDecl * );
|
---|
| 241 | INDEXER_FUNC1( addUnion , const UnionDecl * );
|
---|
| 242 | INDEXER_FUNC1( addTrait , const TraitDecl * );
|
---|
[10a1225] | 243 | INDEXER_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Node * );
|
---|
| 244 | INDEXER_FUNC2( addWith , const std::list < ptr<Expr> > &, const Node * );
|
---|
[04124c4] | 245 |
|
---|
| 246 | // A few extra functions have more complicated behaviour, they are hand written
|
---|
[6d51bd7] | 247 | template<typename pass_t>
|
---|
| 248 | static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) {
|
---|
| 249 | ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
|
---|
[87701b6] | 250 | fwd->params = decl->params;
|
---|
[6d51bd7] | 251 | pass.indexer.addStruct( fwd );
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | template<typename pass_t>
|
---|
| 255 | static inline void addStructFwd( pass_t &, long, const ast::StructDecl * ) {}
|
---|
| 256 |
|
---|
| 257 | template<typename pass_t>
|
---|
| 258 | static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) {
|
---|
| 259 | UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
|
---|
[87701b6] | 260 | fwd->params = decl->params;
|
---|
[6d51bd7] | 261 | pass.indexer.addUnion( fwd );
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | template<typename pass_t>
|
---|
| 265 | static inline void addUnionFwd( pass_t &, long, const ast::UnionDecl * ) {}
|
---|
| 266 |
|
---|
| 267 | template<typename pass_t>
|
---|
| 268 | static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) {
|
---|
| 269 | if ( ! pass.indexer.lookupStruct( str ) ) {
|
---|
| 270 | pass.indexer.addStruct( str );
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | template<typename pass_t>
|
---|
| 275 | static inline void addStruct( pass_t &, long, const std::string & ) {}
|
---|
| 276 |
|
---|
| 277 | template<typename pass_t>
|
---|
| 278 | static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
|
---|
| 279 | if ( ! pass.indexer.lookupUnion( str ) ) {
|
---|
| 280 | pass.indexer.addUnion( str );
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | template<typename pass_t>
|
---|
| 285 | static inline void addUnion( pass_t &, long, const std::string & ) {}
|
---|
[04124c4] | 286 |
|
---|
| 287 | #undef INDEXER_FUNC1
|
---|
| 288 | #undef INDEXER_FUNC2
|
---|
[f47f887] | 289 | };
|
---|
[04124c4] | 290 | };
|
---|
[f47f887] | 291 | };
|
---|