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

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

Minor fixes to which previsits are acceptable

  • Property mode set to 100644
File size: 11.3 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        template<typename pass_t, typename node_t>
112        struct is_valid_previsit {
113                using ret_t = decltype( ((pass_t*)nullptr)->previsit( (const node_t *)nullptr ) );
114
115                static constexpr bool value = std::is_void< ret_t >::value ||
116                        std::is_base_of<const node_t, typename std::remove_pointer<ret_t>::type >::value;
117        };
118
119        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
120        // Deep magic (a.k.a template meta programming) to make the templated visitor work
121        // Basically the goal is to make 2 previsit
122        // 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
123        //     'pass.previsit( node )' that compiles will be used for that node for that type
124        //     This requires that this option only compile for passes that actually define an appropriate visit.
125        //     SFINAE will make sure the compilation errors in this function don't halt the build.
126        //     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
127        // 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
128        //     This is needed only to eliminate the need for passes to specify any kind of handlers.
129        //     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
130        //     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
131        //     the first implementation takes priority in regards to overloading.
132        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
133        // PreVisit : may mutate the pointer passed in if the node is mutated in the previsit call
134        template<typename pass_t, typename node_t>
135        static inline auto previsit( pass_t & pass, const node_t * & node, int ) -> decltype( pass.previsit( node ), void() ) {
136                static_assert(
137                        is_valid_previsit<pass_t, node_t>::value,
138                        "Previsit may not change the type of the node. It must return its paremeter or void."
139                );
140                if(std::is_void< decltype( pass.previsit(node) ) >::value) {
141                        pass.previsit( node );
142                } else {
143                        node = pass.previsit( node );
144                        assert(node);
145                }
146        }
147
148        template<typename pass_t, typename node_t>
149        static inline auto previsit( pass_t &, const node_t *, long ) {}
150
151        // PostVisit : never mutates the passed pointer but may return a different node
152        template<typename pass_t, typename node_t>
153        static inline auto postvisit( pass_t & pass, const node_t * node, int ) ->
154                decltype( pass.postvisit( node ), node->accept( *(Visitor*)nullptr ) )
155        {
156                return pass.postvisit( node );
157        }
158
159        template<typename pass_t, typename node_t>
160        static inline const node_t * postvisit( pass_t &, const node_t * node, long ) { return node; }
161
162        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
163        // Deep magic (a.k.a template meta programming) continued
164        // To make the templated visitor be more expressive, we allow 'accessories' : classes/structs the implementation can inherit
165        // from in order to get extra functionallity for example
166        // class ErrorChecker : WithShortCircuiting { ... };
167        // Pass<ErrorChecker> checker;
168        // this would define a pass that uses the templated visitor with the additionnal feature that it has short circuiting
169        // Note that in all cases the accessories are not required but guarantee the requirements of the feature is matched
170        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
171        // For several accessories, the feature is enabled by detecting that a specific field is present
172        // Use a macro the encapsulate the logic of detecting a particular field
173        // The type is not strictly enforced but does match the accessory
174        #define FIELD_PTR( name, default_type ) \
175        template< typename pass_t > \
176        static inline auto name( pass_t & pass, int ) -> decltype( &pass.name ) { return &pass.name; } \
177        \
178        template< typename pass_t > \
179        static inline default_type * name( pass_t &, long ) { return nullptr; }
180
181        // List of fields and their expected types
182        FIELD_PTR( env, const ast::TypeSubstitution * )
183        FIELD_PTR( stmtsToAddBefore, std::list< ast::ptr< ast::Stmt > > )
184        FIELD_PTR( stmtsToAddAfter , std::list< ast::ptr< ast::Stmt > > )
185        FIELD_PTR( declsToAddBefore, std::list< ast::ptr< ast::Decl > > )
186        FIELD_PTR( declsToAddAfter , std::list< ast::ptr< ast::Decl > > )
187        FIELD_PTR( visit_children, __pass::bool_ref )
188        FIELD_PTR( at_cleanup, __pass::at_cleanup_t )
189        FIELD_PTR( visitor, ast::Pass<pass_t> * const )
190
191        // Remove the macro to make sure we don't clash
192        #undef FIELD_PTR
193
194        // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
195        // All passes which have such functions are assumed desire this behaviour
196        // detect it using the same strategy
197        namespace scope {
198                template<typename pass_t>
199                static inline auto enter( pass_t & pass, int ) -> decltype( pass.beginScope(), void() ) {
200                        pass.beginScope();
201                }
202
203                template<typename pass_t>
204                static inline void enter( pass_t &, long ) {}
205
206                template<typename pass_t>
207                static inline auto leave( pass_t & pass, int ) -> decltype( pass.endScope(), void() ) {
208                        pass.endScope();
209                }
210
211                template<typename pass_t>
212                static inline void leave( pass_t &, long ) {}
213        };
214
215        // Finally certain pass desire an up to date indexer automatically
216        // detect the presence of a member name indexer and call all the members appropriately
217        namespace indexer {
218                // Some simple scoping rules
219                template<typename pass_t>
220                static inline auto enter( pass_t & pass, int ) -> decltype( pass.indexer.enterScope(), void() ) {
221                        pass.indexer.enterScope();
222                }
223
224                template<typename pass_t>
225                static inline auto enter( pass_t &, long ) {}
226
227                template<typename pass_t>
228                static inline auto leave( pass_t & pass, int ) -> decltype( pass.indexer.leaveScope(), void() ) {
229                        pass.indexer.leaveScope();
230                }
231
232                template<typename pass_t>
233                static inline auto leave( pass_t &, long ) {}
234
235                // The indexer has 2 kind of functions mostly, 1 argument and 2 arguments
236                // Create macro to condense these common patterns
237                #define INDEXER_FUNC1( func, type ) \
238                template<typename pass_t> \
239                static inline auto func( pass_t & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) {\
240                        pass.indexer.func( arg ); \
241                } \
242                \
243                template<typename pass_t> \
244                static inline void func( pass_t &, long, type ) {}
245
246                #define INDEXER_FUNC2( func, type1, type2 ) \
247                template<typename pass_t> \
248                static inline auto func( pass_t & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void () ) {\
249                        pass.indexer.func( arg1, arg2 ); \
250                } \
251                        \
252                template<typename pass_t> \
253                static inline void func( pass_t &, long, type1, type2 ) {}
254
255                INDEXER_FUNC1( addId     , const DeclWithType *  );
256                INDEXER_FUNC1( addType   , const NamedTypeDecl * );
257                INDEXER_FUNC1( addStruct , const StructDecl *    );
258                INDEXER_FUNC1( addEnum   , const EnumDecl *      );
259                INDEXER_FUNC1( addUnion  , const UnionDecl *     );
260                INDEXER_FUNC1( addTrait  , const TraitDecl *     );
261                INDEXER_FUNC2( addWith   , const std::vector< ptr<Expr> > &, const Node * );
262                INDEXER_FUNC2( addWith   , const std::list  < ptr<Expr> > &, const Node * );
263
264                // A few extra functions have more complicated behaviour, they are hand written
265                template<typename pass_t>
266                static inline auto addStructFwd( pass_t & pass, int, const ast::StructDecl * decl ) -> decltype( pass.indexer.addStruct( decl ), void() ) {
267                        ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
268                        fwd->params = decl->params;
269                        pass.indexer.addStruct( fwd );
270                }
271
272                template<typename pass_t>
273                static inline void addStructFwd( pass_t &, long, const ast::StructDecl * ) {}
274
275                template<typename pass_t>
276                static inline auto addUnionFwd( pass_t & pass, int, const ast::UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) {
277                        UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
278                        fwd->params = decl->params;
279                        pass.indexer.addUnion( fwd );
280                }
281
282                template<typename pass_t>
283                static inline void addUnionFwd( pass_t &, long, const ast::UnionDecl * ) {}
284
285                template<typename pass_t>
286                static inline auto addStruct( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addStruct( str ), void() ) {
287                        if ( ! pass.indexer.lookupStruct( str ) ) {
288                                pass.indexer.addStruct( str );
289                        }
290                }
291
292                template<typename pass_t>
293                static inline void addStruct( pass_t &, long, const std::string & ) {}
294
295                template<typename pass_t>
296                static inline auto addUnion( pass_t & pass, int, const std::string & str ) -> decltype( pass.indexer.addUnion( str ), void() ) {
297                        if ( ! pass.indexer.lookupUnion( str ) ) {
298                                pass.indexer.addUnion( str );
299                        }
300                }
301
302                template<typename pass_t>
303                static inline void addUnion( pass_t &, long, const std::string & ) {}
304
305                #undef INDEXER_FUNC1
306                #undef INDEXER_FUNC2
307        };
308};
309};
Note: See TracBrowser for help on using the repository browser.