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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since cb25fc9 was cb25fc9, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added to new ast mixin for checker whether it is inFunction or not

  • Property mode set to 100644
File size: 15.1 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( typeSubs, 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        FIELD_PTR( infunc_ptr, const bool * const )
247
248        // Remove the macro to make sure we don't clash
249        #undef FIELD_PTR
250
251        template< typename core_t >
252        static inline auto beginTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
253                // Stats::Heap::stacktrace_push(core_t::traceId);
254        }
255
256        template< typename core_t >
257        static inline auto endTrace(core_t &, int) -> decltype( core_t::traceId, void() ) {
258                // Stats::Heap::stacktrace_pop();
259        }
260
261        template< typename core_t >
262        static void beginTrace(core_t &, long) {}
263
264        template< typename core_t >
265        static void endTrace(core_t &, long) {}
266
267        // Another feature of the templated visitor is that it calls beginScope()/endScope() for compound statement.
268        // All passes which have such functions are assumed desire this behaviour
269        // detect it using the same strategy
270        namespace scope {
271                template<typename core_t>
272                static inline auto enter( core_t & core, int ) -> decltype( core.beginScope(), void() ) {
273                        core.beginScope();
274                }
275
276                template<typename core_t>
277                static inline void enter( core_t &, long ) {}
278
279                template<typename core_t>
280                static inline auto leave( core_t & core, int ) -> decltype( core.endScope(), void() ) {
281                        core.endScope();
282                }
283
284                template<typename core_t>
285                static inline void leave( core_t &, long ) {}
286        } // namespace scope
287
288        // Certain passes desire an up to date symbol table automatically
289        // detect the presence of a member name `symtab` and call all the members appropriately
290        namespace symtab {
291                // Some simple scoping rules
292                template<typename core_t>
293                static inline auto enter( core_t & core, int ) -> decltype( core.symtab, void() ) {
294                        core.symtab.enterScope();
295                }
296
297                template<typename core_t>
298                static inline auto enter( core_t &, long ) {}
299
300                template<typename core_t>
301                static inline auto leave( core_t & core, int ) -> decltype( core.symtab, void() ) {
302                        core.symtab.leaveScope();
303                }
304
305                template<typename core_t>
306                static inline auto leave( core_t &, long ) {}
307
308                // The symbol table has 2 kind of functions mostly, 1 argument and 2 arguments
309                // Create macro to condense these common patterns
310                #define SYMTAB_FUNC1( func, type ) \
311                template<typename core_t> \
312                static inline auto func( core_t & core, int, type arg ) -> decltype( core.symtab.func( arg ), void() ) {\
313                        core.symtab.func( arg ); \
314                } \
315                \
316                template<typename core_t> \
317                static inline void func( core_t &, long, type ) {}
318
319                #define SYMTAB_FUNC2( func, type1, type2 ) \
320                template<typename core_t> \
321                static inline auto func( core_t & core, int, type1 arg1, type2 arg2 ) -> decltype( core.symtab.func( arg1, arg2 ), void () ) {\
322                        core.symtab.func( arg1, arg2 ); \
323                } \
324                        \
325                template<typename core_t> \
326                static inline void func( core_t &, long, type1, type2 ) {}
327
328                SYMTAB_FUNC1( addId     , const DeclWithType *  );
329                SYMTAB_FUNC1( addType   , const NamedTypeDecl * );
330                SYMTAB_FUNC1( addStruct , const StructDecl *    );
331                SYMTAB_FUNC1( addEnum   , const EnumDecl *      );
332                SYMTAB_FUNC1( addUnion  , const UnionDecl *     );
333                SYMTAB_FUNC1( addTrait  , const TraitDecl *     );
334                SYMTAB_FUNC2( addWith   , const std::vector< ptr<Expr> > &, const Decl * );
335
336                // A few extra functions have more complicated behaviour, they are hand written
337                template<typename core_t>
338                static inline auto addStructFwd( core_t & core, int, const ast::StructDecl * decl ) -> decltype( core.symtab.addStruct( decl ), void() ) {
339                        ast::StructDecl * fwd = new ast::StructDecl( decl->location, decl->name );
340                        fwd->params = decl->params;
341                        core.symtab.addStruct( fwd );
342                }
343
344                template<typename core_t>
345                static inline void addStructFwd( core_t &, long, const ast::StructDecl * ) {}
346
347                template<typename core_t>
348                static inline auto addUnionFwd( core_t & core, int, const ast::UnionDecl * decl ) -> decltype( core.symtab.addUnion( decl ), void() ) {
349                        UnionDecl * fwd = new UnionDecl( decl->location, decl->name );
350                        fwd->params = decl->params;
351                        core.symtab.addUnion( fwd );
352                }
353
354                template<typename core_t>
355                static inline void addUnionFwd( core_t &, long, const ast::UnionDecl * ) {}
356
357                template<typename core_t>
358                static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) {
359                        if ( ! core.symtab.lookupStruct( str ) ) {
360                                core.symtab.addStruct( str );
361                        }
362                }
363
364                template<typename core_t>
365                static inline void addStruct( core_t &, long, const std::string & ) {}
366
367                template<typename core_t>
368                static inline auto addUnion( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addUnion( str ), void() ) {
369                        if ( ! core.symtab.lookupUnion( str ) ) {
370                                core.symtab.addUnion( str );
371                        }
372                }
373
374                template<typename core_t>
375                static inline void addUnion( core_t &, long, const std::string & ) {}
376
377                #undef SYMTAB_FUNC1
378                #undef SYMTAB_FUNC2
379        } // namespace symtab
380
381        // Some passes need to mutate TypeDecl and properly update their pointing TypeInstType.
382        // Detect the presence of a member name `subs` and call all members appropriately
383        namespace forall {
384                // Some simple scoping rules
385                template<typename core_t>
386                static inline auto enter( core_t & core, int, const ast::ParameterizedType * type )
387                -> decltype( core.subs, void() ) {
388                        if ( ! type->forall.empty() ) core.subs.beginScope();
389                }
390
391                template<typename core_t>
392                static inline auto enter( core_t &, long, const ast::ParameterizedType * ) {}
393
394                template<typename core_t>
395                static inline auto leave( core_t & core, int, const ast::ParameterizedType * type )
396                -> decltype( core.subs, void() ) {
397                        if ( ! type->forall.empty() ) { core.subs.endScope(); }
398                }
399
400                template<typename core_t>
401                static inline auto leave( core_t &, long, const ast::ParameterizedType * ) {}
402
403                // Get the substitution table, if present
404                template<typename core_t>
405                static inline auto subs( core_t & core, int ) -> decltype( &core.subs ) {
406                        return &core.subs;
407                }
408
409                template<typename core_t>
410                static inline ast::ForallSubstitutionTable * subs( core_t &, long ) { return nullptr; }
411
412                // Replaces a TypeInstType's base TypeDecl according to the table
413                template<typename core_t>
414                static inline auto replace( core_t & core, int, const ast::TypeInstType *& inst )
415                -> decltype( core.subs, void() ) {
416                        inst = ast::mutate_field(
417                                inst, &ast::TypeInstType::base, core.subs.replace( inst->base ) );
418                }
419
420                template<typename core_t>
421                static inline auto replace( core_t &, long, const ast::TypeInstType *& ) {}
422
423        } // namespace forall
424
425        template<typename core_t>
426        static inline auto get_result( core_t & core, char ) -> decltype( core.result() ) {
427                return core.result();
428        }
429
430        template<typename core_t>
431        static inline auto get_result( core_t & core, int ) -> decltype( core.result ) {
432                return core.result;
433        }
434
435        template<typename core_t>
436        static inline void get_result( core_t &, long ) {}
437} // namespace __pass
438} // namespace ast
Note: See TracBrowser for help on using the repository browser.