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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ce12e2b was e3d7f9f, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Const PassVisitor? now supports the indexer

  • Property mode set to 100644
File size: 12.1 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
43class bool_ref {
44public:
45        bool_ref() = default;
46        ~bool_ref() = default;
47
48        operator bool() { return m_ref ? *m_ref : true; }
49        bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
50
51private:
52
53        friend class ChildrenGuard;
54
55        bool * set( bool * val ) {
56                bool * prev = m_ref;
57                m_ref = val;
58                return prev;
59        }
60
61        bool * m_ref = nullptr;
62};
63
64class ChildrenGuard {
65public:
66
67        ChildrenGuard( bool_ref * ref )
68                : m_val ( true )
69                , m_prev( ref ? ref->set( &m_val ) : nullptr )
70                , m_ref ( ref )
71        {}
72
73        ~ChildrenGuard() {
74                if( m_ref ) {
75                        m_ref->set( m_prev );
76                }
77        }
78
79        operator bool() { return m_val; }
80
81private:
82        bool       m_val;
83        bool     * m_prev;
84        bool_ref * m_ref;
85};
86
87//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
88// Deep magic (a.k.a template meta programming) to make the templated visitor work
89// Basically the goal is to make 2 previsit_impl
90// 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
91//     'pass.previsit( node )' that compiles will be used for that node for that type
92//     This requires that this option only compile for passes that actually define an appropriate visit.
93//     SFINAE will make sure the compilation errors in this function don't halt the build.
94//     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
95// 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
96//     This is needed only to eliminate the need for passes to specify any kind of handlers.
97//     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
98//     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
99//     the first implementation takes priority in regards to overloading.
100// Mutator functions work along the same principal
101//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
102// Visit
103template<typename pass_type, typename node_type>
104static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) {
105        pass.previsit( node );
106}
107
108template<typename pass_type, typename node_type>
109static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
110
111
112template<typename pass_type, typename node_type>
113static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) {
114        pass.postvisit( node );
115}
116
117template<typename pass_type, typename node_type>
118static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
119
120template<typename pass_type, typename node_type>
121static inline auto previsit_impl( pass_type& pass, const node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.previsit( node ), void() ) {
122        pass.previsit( node );
123}
124
125template<typename pass_type, typename node_type>
126static inline void previsit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) const node_type * node, __attribute__((unused)) long unused ) {}
127
128
129template<typename pass_type, typename node_type>
130static inline auto postvisit_impl( pass_type& pass, const node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postvisit( node ), void() ) {
131        pass.postvisit( node );
132}
133
134template<typename pass_type, typename node_type>
135static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) const node_type * node, __attribute__((unused)) long unused ) {}
136
137//---------------------------------------------------------
138// Mutate
139template<typename pass_type, typename node_type>
140static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.premutate( node ), void() ) {
141        return pass.premutate( node );
142}
143
144template<typename pass_type, typename node_type>
145static inline void premutate_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) node_type * node, __attribute__((unused)) long unused ) {}
146
147
148template<typename return_type, typename pass_type, typename node_type>
149static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) -> decltype( pass.postmutate( node ) ) {
150        return pass.postmutate( node );
151}
152
153template<typename return_type, typename pass_type, typename node_type>
154static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; }
155
156//---------------------------------------------------------
157// Begin/End scope
158template<typename pass_type>
159static inline auto begin_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.beginScope(), void() ) {
160        pass.beginScope();
161}
162
163template<typename pass_type>
164static inline void begin_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
165
166
167template<typename pass_type>
168static inline auto end_scope_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( pass.endScope(), void() ) {
169        pass.endScope();
170}
171
172template<typename pass_type>
173static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {}
174
175//---------------------------------------------------------
176// Fields
177#define FIELD_PTR( type, name )                                                                                                        \
178template<typename pass_type>                                                                                                           \
179static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \
180                                                                                                                                       \
181template<typename pass_type>                                                                                                           \
182static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;}    \
183
184FIELD_PTR( const TypeSubstitution *, env )
185FIELD_PTR( std::list< Statement* >, stmtsToAddBefore )
186FIELD_PTR( std::list< Statement* >, stmtsToAddAfter  )
187FIELD_PTR( std::list< Declaration* >, declsToAddBefore )
188FIELD_PTR( std::list< Declaration* >, declsToAddAfter  )
189FIELD_PTR( bool_ref, visit_children )
190FIELD_PTR( at_cleanup_t, at_cleanup )
191FIELD_PTR( PassVisitor<pass_type> * const, visitor )
192
193#undef FIELD_PTR
194
195//---------------------------------------------------------
196// Indexer
197template<typename pass_type>
198static inline auto indexer_impl_enterScope( pass_type & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) {
199        pass.indexer.enterScope();
200}
201
202template<typename pass_type>
203static inline auto indexer_impl_enterScope( pass_type &, long ) {}
204
205template<typename pass_type>
206static inline auto indexer_impl_leaveScope( pass_type & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) {
207        pass.indexer.leaveScope();
208}
209
210template<typename pass_type>
211static inline auto indexer_impl_leaveScope( pass_type &, long ) {}
212
213
214#define INDEXER_FUNC1( func, type )                                                                                             \
215template<typename pass_type>                                                                                                   \
216static inline auto indexer_impl_##func ( pass_type & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) {   \
217        pass.indexer.func( arg );                                                                                                \
218}                                                                                                                              \
219template<typename pass_type>                                                                                                   \
220static inline void indexer_impl_##func ( pass_type &, long, type ) { }
221
222#define INDEXER_FUNC2( func, type1, type2 )                                                                                             \
223template<typename pass_type>                                                                                                   \
224static inline auto indexer_impl_##func ( pass_type & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void() ) {   \
225        pass.indexer.func( arg1, arg2 );                                                                                                \
226}                                                                                                                              \
227template<typename pass_type>                                                                                                   \
228static inline void indexer_impl_##func ( pass_type &, long, type1, type2 ) { }
229
230
231INDEXER_FUNC1( addId     , const DeclarationWithType *       );
232INDEXER_FUNC1( addType   , const NamedTypeDecl *             );
233INDEXER_FUNC1( addStruct , const StructDecl *                );
234INDEXER_FUNC1( addEnum   , const EnumDecl *                  );
235INDEXER_FUNC1( addUnion  , const UnionDecl *                 );
236INDEXER_FUNC1( addTrait  , const TraitDecl *                 );
237INDEXER_FUNC2( addWith   , const std::list< Expression * > &, const BaseSyntaxNode * );
238
239#undef INDEXER_FUNC1
240#undef INDEXER_FUNC2
241
242template<typename pass_type>
243static inline auto indexer_impl_addStructFwd( pass_type & pass, int, const StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) {
244        StructDecl * fwd = new StructDecl( decl->name );
245        cloneAll( decl->parameters, fwd->parameters );
246        pass.indexer.addStruct( fwd );
247}
248
249template<typename pass_type>
250static inline auto indexer_impl_addStructFwd( pass_type &, long, const StructDecl * ) {}
251
252template<typename pass_type>
253static inline auto indexer_impl_addUnionFwd( pass_type & pass, int, const UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) {
254        UnionDecl * fwd = new UnionDecl( decl->name );
255        cloneAll( decl->parameters, fwd->parameters );
256        pass.indexer.addUnion( fwd );
257}
258
259template<typename pass_type>
260static inline auto indexer_impl_addUnionFwd( pass_type &, long, const UnionDecl * ) {}
261
262template<typename pass_type>
263static inline auto indexer_impl_addStruct( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) {
264        if ( ! pass.indexer.lookupStruct( str ) ) {
265                pass.indexer.addStruct( str );
266        }
267}
268
269template<typename pass_type>
270static inline auto indexer_impl_addStruct( pass_type &, long, const std::string & ) {}
271
272template<typename pass_type>
273static inline auto indexer_impl_addUnion( pass_type & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
274        if ( ! pass.indexer.lookupUnion( str ) ) {
275                pass.indexer.addUnion( str );
276        }
277}
278
279template<typename pass_type>
280static inline auto indexer_impl_addUnion( pass_type &, long, const std::string & ) {}
Note: See TracBrowser for help on using the repository browser.