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

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 fe97a7d8 was 62423350, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType?.

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