Changeset 13de4478 for src/ResolvExpr/RenameVars.cc
- Timestamp:
- Apr 23, 2024, 1:37:17 PM (3 weeks ago)
- Branches:
- master
- Children:
- 4a3eb1c
- Parents:
- 15215f02
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/RenameVars.cc
r15215f02 r13de4478 30 30 31 31 namespace { 32 class RenamingData {33 int level = 0;34 int resetCount = 0;35 32 36 int next_expr_id = 1; 37 int next_usage_id = 1; 38 ScopedMap< std::string, std::string > nameMap; 39 ScopedMap< std::string, ast::TypeEnvKey > idMap; 40 public: 41 void reset() { 42 level = 0; 43 ++resetCount; 33 class RenamingData { 34 int level = 0; 35 int resetCount = 0; 36 37 int next_expr_id = 1; 38 int next_usage_id = 1; 39 ScopedMap< std::string, std::string > nameMap; 40 ScopedMap< std::string, ast::TypeEnvKey > idMap; 41 public: 42 void reset() { 43 level = 0; 44 ++resetCount; 45 } 46 47 void nextUsage() { 48 ++next_usage_id; 49 } 50 51 const ast::TypeInstType * rename( const ast::TypeInstType * type ) { 52 auto it = idMap.find( type->name ); 53 if ( it == idMap.end() ) return type; 54 55 // Unconditionally mutate because map will *always* have different name. 56 ast::TypeInstType * mut = ast::shallowCopy( type ); 57 // Reconcile base node since some copies might have been made. 58 mut->base = it->second.base; 59 mut->formal_usage = it->second.formal_usage; 60 mut->expr_id = it->second.expr_id; 61 return mut; 62 } 63 64 const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) { 65 if ( type->forall.empty() ) return type; 66 idMap.beginScope(); 67 68 // Load new names from this forall clause and perform renaming. 69 auto mutType = ast::shallowCopy( type ); 70 // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); 71 for ( auto & td : mutType->forall ) { 72 auto mut = ast::shallowCopy( td.get() ); 73 // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" ); 74 75 if ( mode == GEN_EXPR_ID ) { 76 mut->expr_id = next_expr_id; 77 mut->formal_usage = -1; 78 ++next_expr_id; 79 } else if ( mode == GEN_USAGE ) { 80 assertf( mut->expr_id, "unfilled expression id in generating candidate type" ); 81 mut->formal_usage = next_usage_id; 82 } else { 83 assert(false); 84 } 85 idMap[ td->name ] = ast::TypeEnvKey( *mut ); 86 87 td = mut; 44 88 } 45 89 46 void nextUsage() { 47 ++next_usage_id; 48 } 90 return mutType; 91 } 49 92 50 const ast::TypeInstType * rename( const ast::TypeInstType * type ) { 51 auto it = idMap.find( type->name ); 52 if ( it == idMap.end() ) return type; 93 void closeLevel( const ast::FunctionType * type ) { 94 if ( type->forall.empty() ) return; 95 idMap.endScope(); 96 } 97 }; 53 98 54 // Unconditionally mutate because map will *always* have different name. 55 ast::TypeInstType * mut = ast::shallowCopy( type ); 56 // Reconcile base node since some copies might have been made. 57 mut->base = it->second.base; 58 mut->formal_usage = it->second.formal_usage; 59 mut->expr_id = it->second.expr_id; 60 return mut; 61 } 99 // Global State: 100 RenamingData renaming; 62 101 63 const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) { 64 if ( type->forall.empty() ) return type; 65 idMap.beginScope(); 102 struct RenameVars final : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ { 103 RenameMode mode; 66 104 67 // Load new names from this forall clause and perform renaming. 68 auto mutType = ast::shallowCopy( type ); 69 // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); 70 for ( auto & td : mutType->forall ) { 71 auto mut = ast::shallowCopy( td.get() ); 72 // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" ); 105 const ast::FunctionType * previsit( const ast::FunctionType * type ) { 106 return renaming.openLevel( type, mode ); 107 } 73 108 74 if (mode == GEN_EXPR_ID) { 75 mut->expr_id = next_expr_id; 76 mut->formal_usage = -1; 77 ++next_expr_id; 78 } 79 else if (mode == GEN_USAGE) { 80 assertf(mut->expr_id, "unfilled expression id in generating candidate type"); 81 mut->formal_usage = next_usage_id; 82 } 83 else { 84 assert(false); 85 } 86 idMap[ td->name ] = ast::TypeEnvKey( *mut ); 109 /* 110 const ast::StructInstType * previsit( const ast::StructInstType * type ) { 111 return renaming.openLevel( type ); 112 } 113 const ast::UnionInstType * previsit( const ast::UnionInstType * type ) { 114 return renaming.openLevel( type ); 115 } 116 const ast::TraitInstType * previsit( const ast::TraitInstType * type ) { 117 return renaming.openLevel( type ); 118 } 119 */ 87 120 88 td = mut; 89 } 90 91 return mutType; 92 } 93 94 void closeLevel( const ast::FunctionType * type ) { 95 if ( type->forall.empty() ) return; 96 idMap.endScope(); 97 } 98 }; 99 100 // Global State: 101 RenamingData renaming; 102 103 struct RenameVars final : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ { 104 RenameMode mode; 105 106 const ast::FunctionType * previsit( const ast::FunctionType * type ) { 107 return renaming.openLevel( type, mode ); 108 } 109 110 /* 111 const ast::StructInstType * previsit( const ast::StructInstType * type ) { 112 return renaming.openLevel( type ); 113 } 114 const ast::UnionInstType * previsit( const ast::UnionInstType * type ) { 115 return renaming.openLevel( type ); 116 } 117 const ast::TraitInstType * previsit( const ast::TraitInstType * type ) { 118 return renaming.openLevel( type ); 119 } 120 */ 121 122 const ast::TypeInstType * previsit( const ast::TypeInstType * type ) { 123 if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type 124 return renaming.rename( type ); 125 } 126 void postvisit( const ast::FunctionType * type ) { 127 renaming.closeLevel( type ); 128 } 129 }; 121 const ast::TypeInstType * previsit( const ast::TypeInstType * type ) { 122 // Do not rename an actual type. 123 if ( mode == GEN_USAGE && !type->formal_usage ) return type; 124 return renaming.rename( type ); 125 } 126 void postvisit( const ast::FunctionType * type ) { 127 renaming.closeLevel( type ); 128 } 129 }; 130 130 131 131 } // namespace
Note: See TracChangeset
for help on using the changeset viewer.