source: src/Common/PassVisitor.proto.h @ 1c273d0

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 1c273d0 was 6e09f211, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Pass visitor:

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