[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 |
|
---|
[e4b6cf78] | 111 | /// "Short hand" to check if this is a valid previsit function
|
---|
| 112 | /// Mostly used to make the static_assert look (and print) prettier
|
---|
[dff6452] | 113 | template<typename pass_t, typename node_t>
|
---|
| 114 | struct is_valid_previsit {
|
---|
| 115 | using ret_t = decltype( ((pass_t*)nullptr)->previsit( (const node_t *)nullptr ) );
|
---|
| 116 |
|
---|
| 117 | static constexpr bool value = std::is_void< ret_t >::value ||
|
---|
| 118 | std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
|
---|
| 119 | };
|
---|
| 120 |
|
---|
[e4b6cf78] | 121 | /// Used by previsit implementation
|
---|
| 122 | /// We need to reassign the result to 'node', unless the function
|
---|
| 123 | /// returns void, then we just leave 'node' unchanged
|
---|
[0b8bf27] | 124 | template<bool is_void>
|
---|
| 125 | struct __assign;
|
---|
| 126 |
|
---|
| 127 | template<>
|
---|
| 128 | struct __assign<true> {
|
---|
| 129 | template<typename pass_t, typename node_t>
|
---|
| 130 | static inline void result( pass_t & pass, const node_t * & node ) {
|
---|
| 131 | pass.previsit( node );
|
---|
| 132 | }
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | template<>
|
---|
| 136 | struct __assign<false> {
|
---|
| 137 | template<typename pass_t, typename node_t>
|
---|
| 138 | static inline void result( pass_t & pass, const node_t * & node ) {
|
---|
| 139 | node = pass.previsit( node );
|
---|
| 140 | assertf(node, "Previsit must not return NULL");
|
---|
| 141 | }
|
---|
| 142 | };
|
---|
| 143 |
|
---|
[e4b6cf78] | 144 | /// Used by postvisit implementation
|
---|
| 145 | /// We need to return the result unless the function
|
---|
| 146 | /// returns void, then we just return the original node
|
---|
| 147 | template<bool is_void>
|
---|
| 148 | struct __return;
|
---|
| 149 |
|
---|
| 150 | template<>
|
---|
| 151 | struct __return<true> {
|
---|
| 152 | template<typename pass_t, typename node_t>
|
---|
| 153 | static inline const node_t * result( pass_t & pass, const node_t * & node ) {
|
---|
| 154 | pass.postvisit( node );
|
---|
| 155 | return node;
|
---|
| 156 | }
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | template<>
|
---|
| 160 | struct __return<false> {
|
---|
| 161 | template<typename pass_t, typename node_t>
|
---|
| 162 | static inline auto result( pass_t & pass, const node_t * & node ) {
|
---|
| 163 | return pass.postvisit( node );
|
---|
| 164 | }
|
---|
| 165 | };
|
---|
| 166 |
|
---|
[04124c4] | 167 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 168 | // Deep magic (a.k.a template meta programming) to make the templated visitor work
|
---|
| 169 | // Basically the goal is to make 2 previsit
|
---|
| 170 | // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
|
---|
| 171 | // 'pass.previsit( node )' that compiles will be used for that node for that type
|
---|
| 172 | // This requires that this option only compile for passes that actually define an appropriate visit.
|
---|
| 173 | // SFINAE will make sure the compilation errors in this function don't halt the build.
|
---|
| 174 | // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
|
---|
| 175 | // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
|
---|
| 176 | // This is needed only to eliminate the need for passes to specify any kind of handlers.
|
---|
| 177 | // The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
|
---|
| 178 | // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
|
---|
| 179 | // the first implementation takes priority in regards to overloading.
|
---|
| 180 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 181 | // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
|
---|
| 182 | template<typename pass_t, typename node_t>
|
---|
| 183 | static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
|
---|
[b0abc8a0] | 184 | static_assert(
|
---|
[dff6452] | 185 | is_valid_previsit<pass_t, node_t>::value,
|
---|
| 186 | "Previsit may not change the type of the node. It must return its paremeter or void."
|
---|
[b0abc8a0] | 187 | );
|
---|
[0b8bf27] | 188 |
|
---|
| 189 | __assign<
|
---|
| 190 | std::is_void<
|
---|
| 191 | decltype( pass.previsit( node ) )
|
---|
| 192 | >::value
|
---|
| 193 | >::result( pass, node );
|
---|
[04124c4] | 194 | }
|
---|
| 195 |
|
---|
| 196 | template<typename pass_t, typename node_t>
|
---|
| 197 | static inline auto previsit( pass_t &, const node_t *, long ) {}
|
---|
| 198 |
|
---|
| 199 | // PostVisit : never mutates the passed pointer but may return a different node
|
---|
| 200 | template<typename pass_t, typename node_t>
|
---|
[dff6452] | 201 | static inline auto postvisit( pass_t & pass, const node_t * node, int ) ->
|
---|
| 202 | decltype( pass.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
|
---|
| 203 | {
|
---|
[e4b6cf78] | 204 | return __return<
|
---|
| 205 | std::is_void<
|
---|
| 206 | decltype( pass.postvisit( node ) )
|
---|
| 207 | >::value
|
---|
| 208 | >::result( pass, node );
|
---|
[04124c4] | 209 | }
|
---|
| 210 |
|
---|
| 211 | template<typename pass_t, typename node_t>
|
---|
| 212 | static inline const node_t * postvisit( pass_t &, const node_t * node, long ) { return node; }
|
---|
| 213 |
|
---|
| 214 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 215 | // Deep magic (a.k.a template meta programming) continued
|
---|
| 216 | // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
|
---|
| 217 | // from in order to get extra functionallity for example
|
---|
| 218 | // class ErrorChecker : WithShortCircuiting { ... };
|
---|
| 219 | // Pass<ErrorChecker> checker;
|
---|
| 220 | // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
|
---|
| 221 | // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
|
---|
| 222 | //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---|
| 223 | // For several accessories, the feature is enabled by detecting that a specific field is present
|
---|
| 224 | // Use a macro the encapsulate the logic of detecting a particular field
|
---|
| 225 | // The type is not strictly enforced but does match the accessory
|
---|
| 226 | #define FIELD_PTR( name, default_type ) \
|
---|
| 227 | template< typename pass_t > \
|
---|
| 228 | static inline auto name( pass_t & pass, int ) -> decltype( &pass.name ) { return &pass.name; } \
|
---|
| 229 | \
|
---|
| 230 | template< typename pass_t > \
|
---|
| 231 | static inline default_type * name( pass_t &, long ) { return nullptr; }
|
---|
| 232 |
|
---|
| 233 | // List of fields and their expected types
|
---|
[6d51bd7] | 234 | FIELD_PTR( env, const ast::TypeSubstitution * )
|
---|
[04124c4] | 235 | FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
|
---|
| 236 | FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
|
---|
| 237 | FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
|
---|
| 238 | FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
|
---|
| 239 | FIELD_PTR( visit_children, __pass::bool_ref )
|
---|
| 240 | FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
|
---|
| 241 | FIELD_PTR( visitor, ast::Pass<pass_t> * const )
|
---|
| 242 |
|
---|
| 243 | // Remove the macro to make sure we don't clash
|
---|
| 244 | #undef FIELD_PTR
|
---|
| 245 |
|
---|
| 246 | // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
|
---|
| 247 | // All passes which have such functions are assumed desire this behaviour
|
---|
| 248 | // detect it using the same strategy
|
---|
| 249 | namespace scope {
|
---|
| 250 | template<typename pass_t>
|
---|
| 251 | static inline auto enter( pass_t & pass, int ) -> decltype( pass.beginScope(), void() ) {
|
---|
| 252 | pass.beginScope();
|
---|
[f47f887] | 253 | }
|
---|
| 254 |
|
---|
[04124c4] | 255 | template<typename pass_t>
|
---|
| 256 | static inline void enter( pass_t &, long ) {}
|
---|
[f47f887] | 257 |
|
---|
[04124c4] | 258 | template<typename pass_t>
|
---|
| 259 | static inline auto leave( pass_t & pass, int ) -> decltype( pass.endScope(), void() ) {
|
---|
| 260 | pass.endScope();
|
---|
[f47f887] | 261 | }
|
---|
| 262 |
|
---|
[04124c4] | 263 | template<typename pass_t>
|
---|
| 264 | static inline void leave( pass_t &, long ) {}
|
---|
| 265 | };
|
---|
[f47f887] | 266 |
|
---|
[0e42794] | 267 | // Finally certain pass desire an up to date symbol table automatically
|
---|
| 268 | // detect the presence of a member name `symtab` and call all the members appropriately
|
---|
| 269 | namespace symtab {
|
---|
[04124c4] | 270 | // Some simple scoping rules
|
---|
| 271 | template<typename pass_t>
|
---|
[0e42794] | 272 | static inline auto enter( pass_t & pass, int ) -> decltype( pass.symtab.enterScope(), void() ) {
|
---|
| 273 | pass.symtab.enterScope();
|
---|
[04124c4] | 274 | }
|
---|
[f47f887] | 275 |
|
---|
[04124c4] | 276 | template<typename pass_t>
|
---|
| 277 | static inline auto enter( pass_t &, long ) {}
|
---|
[f47f887] | 278 |
|
---|
[04124c4] | 279 | template<typename pass_t>
|
---|
[0e42794] | 280 | static inline auto leave( pass_t & pass, int ) -> decltype( pass.symtab.leaveScope(), void() ) {
|
---|
| 281 | pass.symtab.leaveScope();
|
---|
[04124c4] | 282 | }
|
---|
[f47f887] | 283 |
|
---|
[04124c4] | 284 | template<typename pass_t>
|
---|
| 285 | static inline auto leave( pass_t &, long ) {}
|
---|
[f47f887] | 286 |
|
---|
[0e42794] | 287 | // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments
|
---|
[04124c4] | 288 | // Create macro to condense these common patterns
|
---|
[0e42794] | 289 | #define SYMTAB_FUNC1( func, type ) \
|
---|
[04124c4] | 290 | template<typename pass_t> \
|
---|
[0e42794] | 291 | static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.symtab.func( arg ), void() ) {\
|
---|
| 292 | pass.symtab.func( arg ); \
|
---|
[04124c4] | 293 | } \
|
---|
| 294 | \
|
---|
| 295 | template<typename pass_t> \
|
---|
| 296 | static inline void func( pass_t &, long, type ) {}
|
---|
| 297 |
|
---|
[0e42794] | 298 | #define SYMTAB_FUNC2( func, type1, type2 ) \
|
---|
[04124c4] | 299 | template<typename pass_t> \
|
---|
[0e42794] | 300 | static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.symtab.func( arg1, arg2 ), void () ) {\
|
---|
| 301 | pass.symtab.func( arg1, arg2 ); \
|
---|
[04124c4] | 302 | } \
|
---|
[f47f887] | 303 | \
|
---|
[04124c4] | 304 | template<typename pass_t> \
|
---|
| 305 | static inline void func( pass_t &, long, type1, type2 ) {}
|
---|
| 306 |
|
---|
[0e42794] | 307 | SYMTAB_FUNC1( addId , const DeclWithType * );
|
---|
| 308 | SYMTAB_FUNC1( addType , const NamedTypeDecl * );
|
---|
| 309 | SYMTAB_FUNC1( addStruct , const StructDecl * );
|
---|
| 310 | SYMTAB_FUNC1( addEnum , const EnumDecl * );
|
---|
| 311 | SYMTAB_FUNC1( addUnion , const UnionDecl * );
|
---|
| 312 | SYMTAB_FUNC1( addTrait , const TraitDecl * );
|
---|
| 313 | SYMTAB_FUNC2( addWith , const std::vector< ptr<Expr> > &, const Node * );
|
---|
[04124c4] | 314 |
|
---|
| 315 | // A few extra functions have more complicated behaviour, they are hand written
|
---|
[6d51bd7] | 316 | template<typename pass_t>
|
---|
[0e42794] | 317 | static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.symtab.addStruct( decl ), void() ) {
|
---|
[6d51bd7] | 318 | ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
|
---|
[87701b6] | 319 | fwd->params = decl->params;
|
---|
[0e42794] | 320 | pass.symtab.addStruct( fwd );
|
---|
[6d51bd7] | 321 | }
|
---|
| 322 |
|
---|
| 323 | template<typename pass_t>
|
---|
| 324 | static inline void addStructFwd( pass_t &, long, const ast::StructDecl * ) {}
|
---|
| 325 |
|
---|
| 326 | template<typename pass_t>
|
---|
[0e42794] | 327 | static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.symtab.addUnion( decl ), void() ) {
|
---|
[6d51bd7] | 328 | UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
|
---|
[87701b6] | 329 | fwd->params = decl->params;
|
---|
[0e42794] | 330 | pass.symtab.addUnion( fwd );
|
---|
[6d51bd7] | 331 | }
|
---|
| 332 |
|
---|
| 333 | template<typename pass_t>
|
---|
| 334 | static inline void addUnionFwd( pass_t &, long, const ast::UnionDecl * ) {}
|
---|
| 335 |
|
---|
| 336 | template<typename pass_t>
|
---|
[0e42794] | 337 | static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.symtab.addStruct( str ), void() ) {
|
---|
| 338 | if ( ! pass.symtab.lookupStruct( str ) ) {
|
---|
| 339 | pass.symtab.addStruct( str );
|
---|
[6d51bd7] | 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | template<typename pass_t>
|
---|
| 344 | static inline void addStruct( pass_t &, long, const std::string & ) {}
|
---|
| 345 |
|
---|
| 346 | template<typename pass_t>
|
---|
[0e42794] | 347 | static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.symtab.addUnion( str ), void() ) {
|
---|
| 348 | if ( ! pass.symtab.lookupUnion( str ) ) {
|
---|
| 349 | pass.symtab.addUnion( str );
|
---|
[6d51bd7] | 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | template<typename pass_t>
|
---|
| 354 | static inline void addUnion( pass_t &, long, const std::string & ) {}
|
---|
[04124c4] | 355 |
|
---|
[0e42794] | 356 | #undef SYMTAB_FUNC1
|
---|
| 357 | #undef SYMTAB_FUNC2
|
---|
[f47f887] | 358 | };
|
---|
[04124c4] | 359 | };
|
---|
[f47f887] | 360 | };
|
---|