source: src/Common/PassVisitor.proto.h@ 5ea7a22

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 5ea7a22 was 522363e, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix PassVisitor Indexer calls, aggregate top-level errors in PassVisitor, convert ForallPointerDecay and LinkReferenceToTypes to PassVisitor

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