source: src/AST/Pass.hpp @ e0115286

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

More comments and naming conventions fix

  • Property mode set to 100644
File size: 14.4 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.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 "AST/Pass.hpp"
18
19#include <functional>
20#include <list>
21#include <stack>
22
23#include "AST/Fwd.hpp"
24#include "AST/Node.hpp"
25#include "AST/Decl.hpp"
26#include "AST/Visitor.hpp"
27
28#include "SymTab/Indexer.h"
29
30// Private prelude header, needed for some of the magic tricks this class pulls off
31#include "AST/Pass.proto.hpp"
32
33namespace ast {
34//-------------------------------------------------------------------------------------------------
35// Templated visitor type
36// To use declare a Pass< YOUR VISITOR TYPE >
37// The visitor type should specify the previsit/postvisit for types that are desired.
38// Note: previsit/postvisit must be **public** members
39//
40// Several additional features are available through inheritance
41// | WithTypeSubstitution - provides polymorphic const TypeSubstitution * env for the
42//                          current expression
43// | WithStmtsToAdd       - provides the ability to insert statements before or after the current
44//                          statement by adding new statements into stmtsToAddBefore or
45//                          stmtsToAddAfter respectively.
46// | WithDeclsToAdd       - provides the ability to insert declarations before or after the current
47//                          declarations by adding new DeclStmt into declsToAddBefore or
48//                          declsToAddAfter respectively.
49// | WithShortCircuiting  - provides the ability to skip visiting child nodes; set visit_children
50//                          to false in pre{visit,visit} to skip visiting children
51// | WithGuards           - provides the ability to save/restore data like a LIFO stack; to save,
52//                          call GuardValue with the variable to save, the variable will
53//                          automatically be restored to its previous value after the corresponding
54//                          postvisit/postmutate teminates.
55// | WithVisitorRef       - provides an pointer to the templated visitor wrapper
56// | WithIndexer          - provides indexer functionality (i.e. up-to-date symbol table)
57//-------------------------------------------------------------------------------------------------
58template< typename pass_t >
59class Pass final : public ast::Visitor {
60public:
61        /// Forward any arguments to the pass constructor
62        /// Propagate 'this' if necessary
63        template< typename... Args >
64        Pass( Args &&... args)
65                : pass( std::forward<Args>( args )... )
66        {
67                // After the pass is constructed, check if it wants the have a pointer to the wrapping visitor
68                typedef Pass<pass_t> this_t;
69                this_t * const * visitor = __pass::visitor(pass, 0);
70                if(visitor) {
71                        *const_cast<this_t **>( visitor ) = this;
72                }
73        }
74
75        virtual ~Pass() = default;
76
77        /// Storage for the actual pass
78        pass_t pass;
79
80        /// Visit function declarations
81        virtual DeclWithType *     visit( const ObjectDecl           * ) override final;
82        virtual DeclWithType *     visit( const FunctionDecl         * ) override final;
83        virtual Decl *             visit( const StructDecl           * ) override final;
84        virtual Decl *             visit( const UnionDecl            * ) override final;
85        virtual Decl *             visit( const EnumDecl             * ) override final;
86        virtual Decl *             visit( const TraitDecl            * ) override final;
87        virtual Decl *             visit( const TypeDecl             * ) override final;
88        virtual Decl *             visit( const TypedefDecl          * ) override final;
89        virtual AsmDecl *          visit( const AsmDecl              * ) override final;
90        virtual StaticAssertDecl * visit( const StaticAssertDecl     * ) override final;
91        virtual CompoundStmt *     visit( const CompoundStmt         * ) override final;
92        virtual Stmt *             visit( const ExprStmt             * ) override final;
93        virtual Stmt *             visit( const AsmStmt              * ) override final;
94        virtual Stmt *             visit( const DirectiveStmt        * ) override final;
95        virtual Stmt *             visit( const IfStmt               * ) override final;
96        virtual Stmt *             visit( const WhileStmt            * ) override final;
97        virtual Stmt *             visit( const ForStmt              * ) override final;
98        virtual Stmt *             visit( const SwitchStmt           * ) override final;
99        virtual Stmt *             visit( const CaseStmt             * ) override final;
100        virtual Stmt *             visit( const BranchStmt           * ) override final;
101        virtual Stmt *             visit( const ReturnStmt           * ) override final;
102        virtual Stmt *             visit( const ThrowStmt            * ) override final;
103        virtual Stmt *             visit( const TryStmt              * ) override final;
104        virtual Stmt *             visit( const CatchStmt            * ) override final;
105        virtual Stmt *             visit( const FinallyStmt          * ) override final;
106        virtual Stmt *             visit( const WaitForStmt          * ) override final;
107        virtual Stmt *             visit( const WithStmt             * ) override final;
108        virtual NullStmt *         visit( const NullStmt             * ) override final;
109        virtual Stmt *             visit( const DeclStmt             * ) override final;
110        virtual Stmt *             visit( const ImplicitCtorDtorStmt * ) override final;
111        virtual Expr *             visit( const ApplicationExpr      * ) override final;
112        virtual Expr *             visit( const UntypedExpr          * ) override final;
113        virtual Expr *             visit( const NameExpr             * ) override final;
114        virtual Expr *             visit( const AddressExpr          * ) override final;
115        virtual Expr *             visit( const LabelAddressExpr     * ) override final;
116        virtual Expr *             visit( const CastExpr             * ) override final;
117        virtual Expr *             visit( const KeywordCastExpr      * ) override final;
118        virtual Expr *             visit( const VirtualCastExpr      * ) override final;
119        virtual Expr *             visit( const UntypedMemberExpr    * ) override final;
120        virtual Expr *             visit( const MemberExpr           * ) override final;
121        virtual Expr *             visit( const VariableExpr         * ) override final;
122        virtual Expr *             visit( const ConstantExpr         * ) override final;
123        virtual Expr *             visit( const SizeofExpr           * ) override final;
124        virtual Expr *             visit( const AlignofExpr          * ) override final;
125        virtual Expr *             visit( const UntypedOffsetofExpr  * ) override final;
126        virtual Expr *             visit( const OffsetofExpr         * ) override final;
127        virtual Expr *             visit( const OffsetPackExpr       * ) override final;
128        virtual Expr *             visit( const AttrExpr             * ) override final;
129        virtual Expr *             visit( const LogicalExpr          * ) override final;
130        virtual Expr *             visit( const ConditionalExpr      * ) override final;
131        virtual Expr *             visit( const CommaExpr            * ) override final;
132        virtual Expr *             visit( const TypeExpr             * ) override final;
133        virtual Expr *             visit( const AsmExpr              * ) override final;
134        virtual Expr *             visit( const ImplicitCopyCtorExpr * ) override final;
135        virtual Expr *             visit( const ConstructorExpr      * ) override final;
136        virtual Expr *             visit( const CompoundLiteralExpr  * ) override final;
137        virtual Expr *             visit( const RangeExpr            * ) override final;
138        virtual Expr *             visit( const UntypedTupleExpr     * ) override final;
139        virtual Expr *             visit( const TupleExpr            * ) override final;
140        virtual Expr *             visit( const TupleIndexExpr       * ) override final;
141        virtual Expr *             visit( const TupleAssignExpr      * ) override final;
142        virtual Expr *             visit( const StmtExpr             * ) override final;
143        virtual Expr *             visit( const UniqueExpr           * ) override final;
144        virtual Expr *             visit( const UntypedInitExpr      * ) override final;
145        virtual Expr *             visit( const InitExpr             * ) override final;
146        virtual Expr *             visit( const DeletedExpr          * ) override final;
147        virtual Expr *             visit( const DefaultArgExpr       * ) override final;
148        virtual Expr *             visit( const GenericExpr          * ) override final;
149        virtual Type *             visit( const VoidType             * ) override final;
150        virtual Type *             visit( const BasicType            * ) override final;
151        virtual Type *             visit( const PointerType          * ) override final;
152        virtual Type *             visit( const ArrayType            * ) override final;
153        virtual Type *             visit( const ReferenceType        * ) override final;
154        virtual Type *             visit( const QualifiedType        * ) override final;
155        virtual Type *             visit( const FunctionType         * ) override final;
156        virtual Type *             visit( const StructInstType       * ) override final;
157        virtual Type *             visit( const UnionInstType        * ) override final;
158        virtual Type *             visit( const EnumInstType         * ) override final;
159        virtual Type *             visit( const TraitInstType        * ) override final;
160        virtual Type *             visit( const TypeInstType         * ) override final;
161        virtual Type *             visit( const TupleType            * ) override final;
162        virtual Type *             visit( const TypeofType           * ) override final;
163        virtual Type *             visit( const AttrType             * ) override final;
164        virtual Type *             visit( const VarArgsType          * ) override final;
165        virtual Type *             visit( const ZeroType             * ) override final;
166        virtual Type *             visit( const OneType              * ) override final;
167        virtual Type *             visit( const GlobalScopeType      * ) override final;
168        virtual Designation *      visit( const Designation          * ) override final;
169        virtual Init *             visit( const SingleInit           * ) override final;
170        virtual Init *             visit( const ListInit             * ) override final;
171        virtual Init *             visit( const ConstructorInit      * ) override final;
172        virtual Constant *         visit( const Constant             * ) override final;
173        virtual Attribute *        visit( const Attribute            * ) override final;
174        virtual TypeSubstitution * visit( const TypeSubstitution     * ) override final;
175
176        friend void acceptAll( std::list< ptr<Decl> > & decls, Pass<pass_t>& visitor );
177private:
178
179        bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(pass, 0); return ptr ? *ptr : true; }
180
181private:
182        /// Logic to call the accept and mutate the parent if needed, delegates call to accept
183        template<typename parent_t, typename child_t>
184        void maybe_accept(parent_t * & , typename parent_t::child_t *);
185
186        Stmt * call_accept( const Stmt * );
187        Expr * call_accept( const Expr * );
188
189        template< template <class> class container_t >
190        container_t< ptr<Stmt> > call_accept( const container_t< ptr<Stmt> > & );
191
192        template< template <class> class container_t, typename node_t >
193        container_t< ptr<node_t> > call_accept( const container_t< ptr<node_t> > & container );
194
195private:
196        /// Internal RAII guard for indexer features
197        struct guard_indexer {
198                guard_indexer( Pass<pass_t> & pass ): pass( pass ) { __pass::indexer::enter(pass, 0); }
199                ~guard_indexer()                                   { __pass::indexer::leave(pass, 0); }
200                Pass<pass_t> & pass;
201        };
202
203        /// Internal RAII guard for scope features
204        struct guard_scope {
205                guard_scope( Pass<pass_t> & pass ): pass( pass ) { __pass::scope::enter(pass, 0); }
206                ~guard_scope()                                   { __pass::scope::leave(pass, 0); }
207                Pass<pass_t> & pass;
208        };
209};
210
211template<typename pass_t>
212void acceptAll( std::list< ptr<Decl> >, Pass<pass_t>& visitor );
213
214//-------------------------------------------------------------------------------------------------
215// PASS ACCESSORIES
216//-------------------------------------------------------------------------------------------------
217
218template<typename T>
219using std_list = std::list<T>;
220
221/// Keep track of the polymorphic const TypeSubstitution * env for the current expression
222struct WithConstTypeSubstitution {
223        const TypeSubstitution * env = nullptr;
224};
225
226/// Used if visitor requires added statements before or after the current node.
227/// The Pass template handles what *before* and *after* means automatically
228template< template<class> class container_t = std_list >
229struct WithStmtsToAdd {
230        container_t< ptr<Stmt> > stmtsToAddBefore;
231        container_t< ptr<Stmt> > stmtsToAddAfter;
232};
233
234/// Used if visitor requires added declarations before or after the current node.
235/// The Pass template handles what *before* and *after* means automatically
236template< template<class> class container_t = std_list >
237struct WithDeclsToAdd {
238        container_t< ptr<Decl> > declsToAddBefore;
239        container_t< ptr<Decl> > declsToAddAfter;
240};
241
242/// Use if visitation should stop at certain levels
243/// set visit_children false of all child nodes should be ignored
244struct WithShortCircuiting {
245        __pass::bool_ref visit_children;
246};
247
248/// Used to restore values/functions/etc. when the Pass finishes visiting this node
249class WithGuards {
250        __pass::at_cleanup_t at_cleanup;
251
252public:
253        /// When this node is finished being visited, restore the value of a variable
254        template< typename T >
255        void GuardValue( T& val ) {
256                at_cleanup( [ val ]( void * newVal ) {
257                        * static_cast< T * >( newVal ) = val;
258                }, static_cast< void * >( & val ) );
259        }
260
261        /// On the object, all beginScope now and endScope when the current node is finished being visited
262        template< typename T >
263        void GuardScope( T& val ) {
264                val.beginScope();
265                at_cleanup( []( void * val ) {
266                        static_cast< T * >( val )->endScope();
267                }, static_cast< void * >( & val ) );
268        }
269
270        /// When this node is finished being visited, call a function
271        template< typename Func >
272        void GuardAction( Func func ) {
273                at_cleanup( [func](void *) { func(); }, nullptr );
274        }
275};
276
277/// Used to get a pointer to the pass with its wrapped type
278template<typename pass_t>
279struct WithVisitorRef {
280        Pass<pass_t> * const visitor = nullptr;
281};
282
283/// Use when the templated visitor should update the indexer
284struct WithIndexer {
285        SymTab::Indexer indexer;
286};
287}
Note: See TracBrowser for help on using the repository browser.