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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d3aa64f was d3aa64f, checked in by Fangren Yu <f37yu@…>, 4 years ago

pure visitor interface for new ast

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