source: src/Common/PassVisitor.proto.h @ 1131392

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 1131392 was 3268a58, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Some fixes after the header cleaning-tool

  • Property mode set to 100644
File size: 6.3 KB
Line 
1#pragma once
2// IWYU pragma: private, include "PassVisitor.h"
3
4template<typename pass_type>
5class PassVisitor;
6
7typedef std::function<void( void * )> cleanup_func_t;
8typedef std::function< void( cleanup_func_t, void * ) > at_cleanup_t;
9
10class guard_value_impl {
11public:
12        guard_value_impl( at_cleanup_t * at_cleanup ) {
13                if( at_cleanup ) {
14                        *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
15                                push( std::move( func ), val );
16                        };
17                }
18        }
19
20        ~guard_value_impl() {
21                while( !cleanups.empty() ) {
22                        auto& cleanup = cleanups.top();
23                        cleanup.func( cleanup.val );
24                        cleanups.pop();
25                }
26        }
27
28        void push( cleanup_func_t && func, void* val ) {
29                cleanups.emplace( std::move(func), val );
30        }
31
32private:
33        struct cleanup_t {
34                cleanup_func_t func;
35                void * val;
36
37                cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
38        };
39
40        std::stack< cleanup_t > cleanups;
41};
42
43
44class bool_ref {
45public:
46        bool_ref() = default;
47        ~bool_ref() = default;
48
49        operator bool() { return *m_ref; }
50        bool operator=( bool val ) { return *m_ref = val; }
51
52private:
53
54        template<typename pass>
55        friend class PassVisitor;
56
57        void set( bool & val ) { m_ref = &val; };
58
59        bool * m_ref;
60};
61
62//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
63// Deep magic (a.k.a template meta programming) to make the templated visitor work
64// Basically the goal is to make 2 previsit_impl
65// 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
66//     'pass.previsit( node )' that compiles will be used for that node for that type
67//     This requires that this option only compile for passes that actually define an appropriate visit.
68//     SFINAE will make sure the compilation errors in this function don't halt the build.
69//     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
70// 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
71//     This is needed only to eliminate the need for passes to specify any kind of handlers.
72//     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
73//     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
74//     the first implementation takes priority in regards to overloading.
75// Mutator functions work along the same principal
76//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
77// Visit
78template<typename pass_type, typename node_type>
79static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) {
80        pass.previsit( node );
81}
82
83template<typename pass_type, typename node_type>
84static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
85
86
87template<typename pass_type, typename node_type>
88static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) {
89        pass.postvisit( node );
90}
91
92template<typename pass_type, typename node_type>
93static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
94
95// Mutate
96template<typename pass_type, typename node_type>
97static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) {
98        return pass.premutate( node );
99}
100
101template<typename pass_type, typename node_type>
102static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
103
104
105template<typename return_type, typename pass_type, typename node_type>
106static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) {
107        return pass.postmutate( node );
108}
109
110template<typename return_type, typename pass_type, typename node_type>
111static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; }
112
113// Begin/End scope
114template<typename pass_type>
115static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) {
116        pass.beginScope();
117}
118
119template<typename pass_type>
120static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
121
122
123template<typename pass_type>
124static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) {
125        pass.endScope();
126}
127
128template<typename pass_type>
129static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
130
131// Fields
132#define FIELD_PTR( type, name )                                                                                                        \
133template<typename pass_type>                                                                                                           \
134static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \
135                                                                                                                                       \
136template<typename pass_type>                                                                                                           \
137static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;}    \
138
139FIELD_PTR( TypeSubstitution *, env )
140FIELD_PTR( std::list< Statement* >, stmtsToAddBefore )
141FIELD_PTR( std::list< Statement* >, stmtsToAddAfter  )
142FIELD_PTR( std::list< Declaration* >, declsToAddBefore )
143FIELD_PTR( std::list< Declaration* >, declsToAddAfter  )
144FIELD_PTR( bool_ref, visit_children )
145FIELD_PTR( at_cleanup_t, at_cleanup )
146FIELD_PTR( PassVisitor<pass_type> * const, visitor )
Note: See TracBrowser for help on using the repository browser.