Changes in src/AST/Pass.hpp [0240cd69:37cdd97]
- File:
-
- 1 edited
-
src/AST/Pass.hpp (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Pass.hpp
r0240cd69 r37cdd97 8 8 // 9 9 // Author : Thierry Delisle 10 // Created On : Thu May 09 15: 37:05 201910 // Created On : Thu May 09 15::37::05 2019 11 11 // Last Modified By : 12 12 // Last Modified On : … … 35 35 #include "AST/SymbolTable.hpp" 36 36 37 #include "AST/ForallSubstitutionTable.hpp"38 39 37 // Private prelude header, needed for some of the magic tricks this class pulls off 40 38 #include "AST/Pass.proto.hpp" … … 48 46 // 49 47 // Several additional features are available through inheritance 50 // | WithTypeSubstitution - provides polymorphic const TypeSubstitution * env for the 51 // current expression 52 // | WithStmtsToAdd - provides the ability to insert statements before or after the current 53 // statement by adding new statements into stmtsToAddBefore or 54 // stmtsToAddAfter respectively. 55 // | WithDeclsToAdd - provides the ability to insert declarations before or after the 56 // current declarations by adding new DeclStmt into declsToAddBefore or 57 // declsToAddAfter respectively. 58 // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children 59 // to false in pre{visit,visit} to skip visiting children 60 // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, 61 // call GuardValue with the variable to save, the variable will 62 // automatically be restored to its previous value after the 63 // corresponding postvisit/postmutate teminates. 64 // | WithVisitorRef - provides an pointer to the templated visitor wrapper 65 // | WithSymbolTable - provides symbol table functionality 66 // | WithForallSubstitutor - maintains links between TypeInstType and TypeDecl under mutation 48 // | WithTypeSubstitution - provides polymorphic const TypeSubstitution * env for the 49 // current expression 50 // | WithStmtsToAdd - provides the ability to insert statements before or after the current 51 // statement by adding new statements into stmtsToAddBefore or 52 // stmtsToAddAfter respectively. 53 // | WithDeclsToAdd - provides the ability to insert declarations before or after the current 54 // declarations by adding new DeclStmt into declsToAddBefore or 55 // declsToAddAfter respectively. 56 // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children 57 // to false in pre{visit,visit} to skip visiting children 58 // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, 59 // call GuardValue with the variable to save, the variable will 60 // automatically be restored to its previous value after the corresponding 61 // postvisit/postmutate teminates. 62 // | WithVisitorRef - provides an pointer to the templated visitor wrapper 63 // | WithSymbolTable - provides symbol table functionality 67 64 //------------------------------------------------------------------------------------------------- 68 template< typename core_t >65 template< typename pass_t > 69 66 class Pass final : public ast::Visitor { 70 67 public: 71 using core_type = core_t;72 using type = Pass<core_t>;73 74 68 /// Forward any arguments to the pass constructor 75 69 /// Propagate 'this' if necessary 76 70 template< typename... Args > 77 71 Pass( Args &&... args) 78 : core( std::forward<Args>( args )... )72 : pass( std::forward<Args>( args )... ) 79 73 { 80 74 // After the pass is constructed, check if it wants the have a pointer to the wrapping visitor 81 type * const * visitor = __pass::visitor(core, 0); 75 typedef Pass<pass_t> this_t; 76 this_t * const * visitor = __pass::visitor(pass, 0); 82 77 if(visitor) { 83 *const_cast<t ype**>( visitor ) = this;78 *const_cast<this_t **>( visitor ) = this; 84 79 } 85 80 } … … 87 82 virtual ~Pass() = default; 88 83 89 /// Construct and run a pass on a translation unit.90 template< typename... Args >91 static void run( std::list< ptr<Decl> > & decls, Args &&... args ) {92 Pass<core_t> visitor( std::forward<Args>( args )... );93 accept_all( decls, visitor );94 }95 96 template< typename... Args >97 static void run( std::list< ptr<Decl> > & decls ) {98 Pass<core_t> visitor;99 accept_all( decls, visitor );100 }101 102 84 /// Storage for the actual pass 103 core_t core;85 pass_t pass; 104 86 105 87 /// Visit function declarations … … 197 179 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * ) override final; 198 180 199 template<typename core_type>200 friend void accept_all( std::list< ptr<Decl> > & decls, Pass< core_type>& visitor );181 template<typename pass_type> 182 friend void accept_all( std::list< ptr<Decl> > & decls, Pass<pass_type>& visitor ); 201 183 private: 202 184 203 bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children( core, 0); return ptr ? *ptr : true; }185 bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(pass, 0); return ptr ? *ptr : true; } 204 186 205 187 private: … … 220 202 container_t< ptr<node_t> > call_accept( const container_t< ptr<node_t> > & container ); 221 203 222 /// Mutate forall-list, accounting for presence of type substitution map223 template<typename node_t>224 void mutate_forall( const node_t *& );225 226 204 public: 227 205 /// Logic to call the accept and mutate the parent if needed, delegates call to accept … … 232 210 /// Internal RAII guard for symbol table features 233 211 struct guard_symtab { 234 guard_symtab( Pass< core_t> & pass ): pass( pass ) { __pass::symtab::enter(pass.core, 0); }235 ~guard_symtab() { __pass::symtab::leave(pass .core, 0); }236 Pass< core_t> & pass;212 guard_symtab( Pass<pass_t> & pass ): pass( pass ) { __pass::symtab::enter(pass, 0); } 213 ~guard_symtab() { __pass::symtab::leave(pass, 0); } 214 Pass<pass_t> & pass; 237 215 }; 238 216 239 217 /// Internal RAII guard for scope features 240 218 struct guard_scope { 241 guard_scope( Pass<core_t> & pass ): pass( pass ) { __pass::scope::enter(pass.core, 0); } 242 ~guard_scope() { __pass::scope::leave(pass.core, 0); } 243 Pass<core_t> & pass; 244 }; 245 246 /// Internal RAII guard for forall substitutions 247 struct guard_forall_subs { 248 guard_forall_subs( Pass<core_t> & pass, const ParameterizedType * type ) 249 : pass( pass ), type( type ) { __pass::forall::enter(pass.core, 0, type ); } 250 ~guard_forall_subs() { __pass::forall::leave(pass.core, 0, type ); } 251 Pass<core_t> & pass; 252 const ParameterizedType * type; 219 guard_scope( Pass<pass_t> & pass ): pass( pass ) { __pass::scope::enter(pass, 0); } 220 ~guard_scope() { __pass::scope::leave(pass, 0); } 221 Pass<pass_t> & pass; 253 222 }; 254 223 … … 258 227 259 228 /// Apply a pass to an entire translation unit 260 template<typename core_t>261 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass< core_t> & visitor );229 template<typename pass_t> 230 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<pass_t> & visitor ); 262 231 263 232 //------------------------------------------------------------------------------------------------- … … 299 268 }; 300 269 301 template< typename core_t>302 friend auto __pass::at_cleanup( core_t & core, int ) -> decltype( &core.at_cleanup );270 template< typename pass_t> 271 friend auto __pass::at_cleanup( pass_t & pass, int ) -> decltype( &pass.at_cleanup ); 303 272 public: 304 273 … … 336 305 337 306 /// Used to get a pointer to the pass with its wrapped type 338 template<typename core_t>307 template<typename pass_t> 339 308 struct WithVisitorRef { 340 Pass< core_t> * const visitor = nullptr;309 Pass<pass_t> * const visitor = nullptr; 341 310 }; 342 311 … … 345 314 SymbolTable symtab; 346 315 }; 347 348 /// Use when the templated visitor needs to keep TypeInstType instances properly linked to TypeDecl349 struct WithForallSubstitutor {350 ForallSubstitutionTable subs;351 };352 353 316 } 354 317 … … 358 321 extern struct PassVisitorStats { 359 322 size_t depth = 0; 360 Stats::Counters::MaxCounter<double> * max ;361 Stats::Counters::AverageCounter<double> * avg ;323 Stats::Counters::MaxCounter<double> * max = nullptr; 324 Stats::Counters::AverageCounter<double> * avg = nullptr; 362 325 } pass_visitor_stats; 363 326 }
Note:
See TracChangeset
for help on using the changeset viewer.