#pragma once template class PassVisitor; typedef std::function cleanup_func_t; class guard_value_impl { public: guard_value_impl() = default; ~guard_value_impl() { while( !cleanups.empty() ) { auto& cleanup = cleanups.top(); cleanup.func( cleanup.val ); cleanups.pop(); } } void push( cleanup_func_t && func, void* val ) { cleanups.emplace( std::move(func), val ); } private: struct cleanup_t { cleanup_func_t func; void * val; cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {} }; std::stack< cleanup_t > cleanups; }; typedef std::function< void( cleanup_func_t, void * ) > at_cleanup_t; class bool_ref { public: bool_ref() = default; ~bool_ref() = default; operator bool() { return *m_ref; } bool operator=( bool val ) { return *m_ref = val; } private: template friend class PassVisitor; void set( bool & val ) { m_ref = &val; }; bool * m_ref; }; //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Deep magic (a.k.a template meta programming) to make the templated visitor work // Basically the goal is to make 2 previsit_impl // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of // 'pass.previsit( node )' that compiles will be used for that node for that type // This requires that this option only compile for passes that actually define an appropriate visit. // SFINAE will make sure the compilation errors in this function don't halt the build. // See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing. // This is needed only to eliminate the need for passes to specify any kind of handlers. // The second implementation only works because it has a lower priority. This is due to the bogus last parameter. // The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0 // the first implementation takes priority in regards to overloading. // Mutator functions work along the same principal //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Visit template static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) { pass.previsit( node ); } template static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} template static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) { pass.postvisit( node ); } template static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} // Mutate template static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) { return pass.premutate( node ); } template static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {} template static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) { return pass.postmutate( node ); } template static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; } // Begin/End scope template static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) { pass.beginScope(); } template static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} template static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) { pass.endScope(); } template static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} // Fields #define FIELD_PTR( type, name ) \ template \ static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \ \ template \ static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ FIELD_PTR( TypeSubstitution *, env ) FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) FIELD_PTR( std::list< Declaration* >, declsToAddBefore ) FIELD_PTR( std::list< Declaration* >, declsToAddAfter ) FIELD_PTR( bool_ref, visit_children ) FIELD_PTR( at_cleanup_t, at_cleanup ) FIELD_PTR( PassVisitor * const, visitor )