Changeset e67a82d for src/AST/Pass.hpp
- Timestamp:
- Aug 20, 2020, 11:48:15 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- d685cb0
- Parents:
- 67ca73e (diff), 013b028 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Pass.hpp
r67ca73e re67a82d 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 37 39 // Private prelude header, needed for some of the magic tricks this class pulls off 38 40 #include "AST/Pass.proto.hpp" … … 46 48 // 47 49 // Several additional features are available through inheritance 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 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 64 67 //------------------------------------------------------------------------------------------------- 65 template< typename pass_t >68 template< typename core_t > 66 69 class Pass final : public ast::Visitor { 67 70 public: 71 using core_type = core_t; 72 using type = Pass<core_t>; 73 68 74 /// Forward any arguments to the pass constructor 69 75 /// Propagate 'this' if necessary 70 76 template< typename... Args > 71 77 Pass( Args &&... args) 72 : pass( std::forward<Args>( args )... )78 : core( std::forward<Args>( args )... ) 73 79 { 74 80 // After the pass is constructed, check if it wants the have a pointer to the wrapping visitor 75 typedef Pass<pass_t> this_t; 76 this_t * const * visitor = __pass::visitor(pass, 0); 81 type * const * visitor = __pass::visitor(core, 0); 77 82 if(visitor) { 78 *const_cast<t his_t**>( visitor ) = this;83 *const_cast<type **>( visitor ) = this; 79 84 } 80 85 } … … 82 87 virtual ~Pass() = default; 83 88 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 84 102 /// Storage for the actual pass 85 pass_t pass;103 core_t core; 86 104 87 105 /// Visit function declarations … … 179 197 const ast::TypeSubstitution * visit( const ast::TypeSubstitution * ) override final; 180 198 181 template<typename pass_type>182 friend void accept_all( std::list< ptr<Decl> > & decls, Pass< pass_type>& visitor );199 template<typename core_type> 200 friend void accept_all( std::list< ptr<Decl> > & decls, Pass<core_type>& visitor ); 183 201 private: 184 202 185 bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children( pass, 0); return ptr ? *ptr : true; }203 bool __visit_children() { __pass::bool_ref * ptr = __pass::visit_children(core, 0); return ptr ? *ptr : true; } 186 204 187 205 private: … … 202 220 container_t< ptr<node_t> > call_accept( const container_t< ptr<node_t> > & container ); 203 221 222 /// Mutate forall-list, accounting for presence of type substitution map 223 template<typename node_t> 224 void mutate_forall( const node_t *& ); 225 204 226 public: 205 227 /// Logic to call the accept and mutate the parent if needed, delegates call to accept … … 210 232 /// Internal RAII guard for symbol table features 211 233 struct guard_symtab { 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;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; 215 237 }; 216 238 217 239 /// Internal RAII guard for scope features 218 240 struct guard_scope { 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; 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; 222 253 }; 223 254 … … 227 258 228 259 /// Apply a pass to an entire translation unit 229 template<typename pass_t>230 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass< pass_t> & visitor );260 template<typename core_t> 261 void accept_all( std::list< ast::ptr<ast::Decl> > &, ast::Pass<core_t> & visitor ); 231 262 232 263 //------------------------------------------------------------------------------------------------- … … 268 299 }; 269 300 270 template< typename pass_t>271 friend auto __pass::at_cleanup( pass_t & pass, int ) -> decltype( &pass.at_cleanup );301 template< typename core_t> 302 friend auto __pass::at_cleanup( core_t & core, int ) -> decltype( &core.at_cleanup ); 272 303 public: 273 304 … … 305 336 306 337 /// Used to get a pointer to the pass with its wrapped type 307 template<typename pass_t>338 template<typename core_t> 308 339 struct WithVisitorRef { 309 Pass< pass_t> * const visitor = nullptr;340 Pass<core_t> * const visitor = nullptr; 310 341 }; 311 342 … … 314 345 SymbolTable symtab; 315 346 }; 347 348 /// Use when the templated visitor needs to keep TypeInstType instances properly linked to TypeDecl 349 struct WithForallSubstitutor { 350 ForallSubstitutionTable subs; 351 }; 352 316 353 } 317 354 … … 321 358 extern struct PassVisitorStats { 322 359 size_t depth = 0; 323 Stats::Counters::MaxCounter<double> * max = nullptr;324 Stats::Counters::AverageCounter<double> * avg = nullptr;360 Stats::Counters::MaxCounter<double> * max; 361 Stats::Counters::AverageCounter<double> * avg; 325 362 } pass_visitor_stats; 326 363 }
Note: See TracChangeset
for help on using the changeset viewer.