source: src/AST/Pass.proto.hpp @ e4b6cf78

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

New Pass visitor now supports void postvisits by returning the original node

  • Property mode set to 100644
File size: 12.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Pass.impl.hpp --
8//
9// Author           : Thierry Delisle
10// Created On       : Thu May 09 15::37::05 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#pragma once
17// IWYU pragma: private, include "Pass.hpp"
18
19namespace ast {
20template<typename pass_type>
21class Pass;
22
23namespace __pass {
24        typedef std::function<void( void * )> cleanup_func_t;
25        typedef std::function<void( cleanup_func_t, void * )> at_cleanup_t;
26
27
28        // boolean reference that may be null
29        // either refers to a boolean value or is null and returns true
30        class bool_ref {
31        public:
32                bool_ref() = default;
33                ~bool_ref() = default;
34
35                operator bool() { return m_ref ? *m_ref : true; }
36                bool operator=( bool val ) { assert(m_ref); return *m_ref = val; }
37
38        private:
39
40                friend class visit_children_guard;
41
42                bool * set( bool * val ) {
43                        bool * prev = m_ref;
44                        m_ref = val;
45                        return prev;
46                }
47
48                bool * m_ref = nullptr;
49        };
50
51        // Implementation of the guard value
52        // Created inside the visit scope
53        class guard_value {
54        public:
55                /// Push onto the cleanup
56                guard_value( at_cleanup_t * at_cleanup ) {
57                        if( at_cleanup ) {
58                                *at_cleanup = [this]( cleanup_func_t && func, void* val ) {
59                                        push( std::move( func ), val );
60                                };
61                        }
62                }
63
64                ~guard_value() {
65                        while( !cleanups.empty() ) {
66                                auto& cleanup = cleanups.top();
67                                cleanup.func( cleanup.val );
68                                cleanups.pop();
69                        }
70                }
71
72                void push( cleanup_func_t && func, void* val ) {
73                        cleanups.emplace( std::move(func), val );
74                }
75
76        private:
77                struct cleanup_t {
78                        cleanup_func_t func;
79                        void * val;
80
81                        cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
82                };
83
84                std::stack< cleanup_t > cleanups;
85        };
86
87        // Guard structure implementation for whether or not children should be visited
88        class visit_children_guard {
89        public:
90
91                visit_children_guard( bool_ref * ref )
92                        : m_val ( true )
93                        , m_prev( ref ? ref->set( &m_val ) : nullptr )
94                        , m_ref ( ref )
95                {}
96
97                ~visit_children_guard() {
98                        if( m_ref ) {
99                                m_ref->set( m_prev );
100                        }
101                }
102
103                operator bool() { return m_val; }
104
105        private:
106                bool       m_val;
107                bool     * m_prev;
108                bool_ref * m_ref;
109        };
110
111        /// "Short hand" to check if this is a valid previsit function
112        /// Mostly used to make the static_assert look (and print) prettier
113        template<typename pass_t, typename node_t>
114        struct is_valid_previsit {
115                using ret_t = decltype( ((pass_t*)nullptr)->previsit( (const node_t *)nullptr ) );
116
117                static constexpr bool value = std::is_void< ret_t >::value ||
118                        std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
119        };
120
121        /// Used by previsit implementation
122        /// We need to reassign the result to 'node', unless the function
123        /// returns void, then we just leave 'node' unchanged
124        template<bool is_void>
125        struct __assign;
126
127        template<>
128        struct __assign<true> {
129                template<typename pass_t, typename node_t>
130                static inline void result( pass_t & pass, const node_t * & node ) {
131                        pass.previsit( node );
132                }
133        };
134
135        template<>
136        struct __assign<false> {
137                template<typename pass_t, typename node_t>
138                static inline void result( pass_t & pass, const node_t * & node ) {
139                        node = pass.previsit( node );
140                        assertf(node, "Previsit must not return NULL");
141                }
142        };
143
144        /// Used by postvisit implementation
145        /// We need to return the result unless the function
146        /// returns void, then we just return the original node
147        template<bool is_void>
148        struct __return;
149
150        template<>
151        struct __return<true> {
152                template<typename pass_t, typename node_t>
153                static inline const node_t * result( pass_t & pass, const node_t * & node ) {
154                        pass.postvisit( node );
155                        return node;
156                }
157        };
158
159        template<>
160        struct __return<false> {
161                template<typename pass_t, typename node_t>
162                static inline auto result( pass_t & pass, const node_t * & node ) {
163                        return pass.postvisit( node );
164                }
165        };
166
167        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
168        // Deep magic (a.k.a template meta programming) to make the templated visitor work
169        // Basically the goal is to make 2 previsit
170        // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
171        //     'pass.previsit( node )' that compiles will be used for that node for that type
172        //     This requires that this option only compile for passes that actually define an appropriate visit.
173        //     SFINAE will make sure the compilation errors in this function don't halt the build.
174        //     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
175        // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
176        //     This is needed only to eliminate the need for passes to specify any kind of handlers.
177        //     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
178        //     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
179        //     the first implementation takes priority in regards to overloading.
180        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
181        // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
182        template<typename pass_t, typename node_t>
183        static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
184                static_assert(
185                        is_valid_previsit<pass_t, node_t>::value,
186                        "Previsit may not change the type of the node. It must return its paremeter or void."
187                );
188
189                __assign<
190                        std::is_void<
191                                decltype( pass.previsit( node ) )
192                        >::value
193                >::result( pass, node );
194        }
195
196        template<typename pass_t, typename node_t>
197        static inline auto previsit( pass_t &, const node_t *, long ) {}
198
199        // PostVisit : never mutates the passed pointer but may return a different node
200        template<typename pass_t, typename node_t>
201        static inline auto postvisit( pass_t & pass, const node_t * node, int ) ->
202                decltype( pass.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
203        {
204                return __return<
205                        std::is_void<
206                                decltype( pass.postvisit( node ) )
207                        >::value
208                >::result( pass, node );
209        }
210
211        template<typename pass_t, typename node_t>
212        static inline const node_t * postvisit( pass_t &, const node_t * node, long ) { return node; }
213
214        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
215        // Deep magic (a.k.a template meta programming) continued
216        // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
217        // from in order to get extra functionallity for example
218        // class ErrorChecker : WithShortCircuiting { ... };
219        // Pass<ErrorChecker> checker;
220        // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
221        // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
222        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
223        // For several accessories, the feature is enabled by detecting that a specific field is present
224        // Use a macro the encapsulate the logic of detecting a particular field
225        // The type is not strictly enforced but does match the accessory
226        #define FIELD_PTR( name, default_type ) \
227        template< typename pass_t > \
228        static inline auto name( pass_t & pass, int ) -> decltype( &pass.name ) { return &pass.name; } \
229        \
230        template< typename pass_t > \
231        static inline default_type * name( pass_t &, long ) { return nullptr; }
232
233        // List of fields and their expected types
234        FIELD_PTR( env, const ast::TypeSubstitution * )
235        FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
236        FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
237        FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
238        FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
239        FIELD_PTR( visit_children, __pass::bool_ref )
240        FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
241        FIELD_PTR( visitor, ast::Pass<pass_t> * const )
242
243        // Remove the macro to make sure we don't clash
244        #undef FIELD_PTR
245
246        // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
247        // All passes which have such functions are assumed desire this behaviour
248        // detect it using the same strategy
249        namespace scope {
250                template<typename pass_t>
251                static inline auto enter( pass_t & pass, int ) -> decltype( pass.beginScope(), void() ) {
252                        pass.beginScope();
253                }
254
255                template<typename pass_t>
256                static inline void enter( pass_t &, long ) {}
257
258                template<typename pass_t>
259                static inline auto leave( pass_t & pass, int ) -> decltype( pass.endScope(), void() ) {
260                        pass.endScope();
261                }
262
263                template<typename pass_t>
264                static inline void leave( pass_t &, long ) {}
265        };
266
267        // Finally certain pass desire an up to date indexer automatically
268        // detect the presence of a member name indexer and call all the members appropriately
269        namespace indexer {
270                // Some simple scoping rules
271                template<typename pass_t>
272                static inline auto enter( pass_t & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) {
273                        pass.indexer.enterScope();
274                }
275
276                template<typename pass_t>
277                static inline auto enter( pass_t &, long ) {}
278
279                template<typename pass_t>
280                static inline auto leave( pass_t & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) {
281                        pass.indexer.leaveScope();
282                }
283
284                template<typename pass_t>
285                static inline auto leave( pass_t &, long ) {}
286
287                // The indexer has 2 kind of functions mostly, 1 argument and 2 arguments
288                // Create macro to condense these common patterns
289                #define INDEXER_FUNC1( func, type ) \
290                template<typename pass_t> \
291                static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) {\
292                        pass.indexer.func( arg ); \
293                } \
294                \
295                template<typename pass_t> \
296                static inline void func( pass_t &, long, type ) {}
297
298                #define INDEXER_FUNC2( func, type1, type2 ) \
299                template<typename pass_t> \
300                static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void () ) {\
301                        pass.indexer.func( arg1, arg2 ); \
302                } \
303                        \
304                template<typename pass_t> \
305                static inline void func( pass_t &, long, type1, type2 ) {}
306
307                INDEXER_FUNC1( addId     , const DeclWithType *  );
308                INDEXER_FUNC1( addType   , const NamedTypeDecl * );
309                INDEXER_FUNC1( addStruct , const StructDecl *    );
310                INDEXER_FUNC1( addEnum   , const EnumDecl *      );
311                INDEXER_FUNC1( addUnion  , const UnionDecl *     );
312                INDEXER_FUNC1( addTrait  , const TraitDecl *     );
313                INDEXER_FUNC2( addWith   , const std::vector< ptr<Expr> > &, const Node * );
314                INDEXER_FUNC2( addWith   , const std::list  < ptr<Expr> > &, const Node * );
315
316                // A few extra functions have more complicated behaviour, they are hand written
317                template<typename pass_t>
318                static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) {
319                        ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
320                        fwd->params = decl->params;
321                        pass.indexer.addStruct( fwd );
322                }
323
324                template<typename pass_t>
325                static inline void addStructFwd( pass_t &, long, const ast::StructDecl * ) {}
326
327                template<typename pass_t>
328                static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) {
329                        UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
330                        fwd->params = decl->params;
331                        pass.indexer.addUnion( fwd );
332                }
333
334                template<typename pass_t>
335                static inline void addUnionFwd( pass_t &, long, const ast::UnionDecl * ) {}
336
337                template<typename pass_t>
338                static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) {
339                        if ( ! pass.indexer.lookupStruct( str ) ) {
340                                pass.indexer.addStruct( str );
341                        }
342                }
343
344                template<typename pass_t>
345                static inline void addStruct( pass_t &, long, const std::string & ) {}
346
347                template<typename pass_t>
348                static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
349                        if ( ! pass.indexer.lookupUnion( str ) ) {
350                                pass.indexer.addUnion( str );
351                        }
352                }
353
354                template<typename pass_t>
355                static inline void addUnion( pass_t &, long, const std::string & ) {}
356
357                #undef INDEXER_FUNC1
358                #undef INDEXER_FUNC2
359        };
360};
361};
Note: See TracBrowser for help on using the repository browser.