Changes in src/ResolvExpr/RenameVars.cc [7583c02:361bf01]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/RenameVars.cc
r7583c02 r361bf01 19 19 #include <utility> // for pair 20 20 21 #include "AST/ForallSubstitutionTable.hpp" 21 22 #include "AST/Pass.hpp" 22 23 #include "AST/Type.hpp" … … 38 39 int level = 0; 39 40 int resetCount = 0; 41 ScopedMap< std::string, std::string > nameMap; 42 public: 43 ast::ForallSubstitutionTable subs; 40 44 41 int next_expr_id = 1;42 int next_usage_id = 1;43 ScopedMap< std::string, std::string > nameMap;44 ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap;45 public:46 45 void reset() { 47 46 level = 0; … … 54 53 type->name = it->second; 55 54 } 56 }57 58 void nextUsage() {59 ++next_usage_id;60 55 } 61 56 … … 83 78 84 79 const ast::TypeInstType * rename( const ast::TypeInstType * type ) { 80 // re-linking of base type handled by WithForallSubstitutor 81 85 82 // rename 86 auto it = idMap.find( type->name ); 87 if ( it != idMap.end() ) { 88 // unconditionally mutate because map will *always* have different name 89 ast::TypeInstType * mut = ast::shallowCopy( type ); 90 // reconcile base node since some copies might have been made 91 mut->base = it->second.base; 92 mut->formal_usage = it->second.formal_usage; 93 mut->expr_id = it->second.expr_id; 83 auto it = nameMap.find( type->name ); 84 if ( it != nameMap.end() ) { 85 // unconditionally mutate because map will *always* have different name, 86 // if this mutates, will *always* have been mutated by ForallSubstitutor above 87 ast::TypeInstType * mut = ast::mutate( type ); 88 mut->name = it->second; 94 89 type = mut; 95 90 } … … 98 93 } 99 94 100 const ast::FunctionType * openLevel( const ast::FunctionType * type , RenameMode mode) {95 const ast::FunctionType * openLevel( const ast::FunctionType * type ) { 101 96 if ( type->forall.empty() ) return type; 102 idMap.beginScope(); 97 98 nameMap.beginScope(); 103 99 104 100 // Load new names from this forall clause and perform renaming. 105 auto mutType = ast::shallowCopy( type ); 106 // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); 107 for ( auto & td : mutType->forall ) { 108 auto mut = ast::shallowCopy( td.get() ); 109 // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" ); 101 auto mutType = ast::mutate( type ); 102 assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); 103 for ( ast::ptr< ast::TypeDecl > & td : mutType->forall ) { 104 assertf(dynamic_cast<ast::FunctionType *>(mutType), "renaming vars in non-function type"); 105 std::ostringstream output; 106 output << "_" << resetCount << "_" << level << "_" << td->name; 107 std::string newname = output.str(); 108 nameMap[ td->name ] = newname; 109 ++level; 110 110 111 if (mode == GEN_EXPR_ID) { 112 mut->expr_id = next_expr_id; 113 mut->formal_usage = -1; 114 ++next_expr_id; 115 } 116 else if (mode == GEN_USAGE) { 117 assertf(mut->expr_id, "unfilled expression id in generating candidate type"); 118 mut->formal_usage = next_usage_id; 119 } 120 else { 121 assert(false); 122 } 123 idMap[ td->name ] = ast::TypeInstType::TypeEnvKey(*mut); 124 125 td = mut; 111 ast::TypeDecl * mutDecl = ast::mutate( td.get() ); 112 assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" ); 113 mutDecl->name = newname; 114 // assertion above means `td = mutDecl;` is unnecessary 126 115 } 116 // assertion above means `type = mutType;` is unnecessary 127 117 128 return mutType;118 return type; 129 119 } 130 120 131 121 void closeLevel( const ast::FunctionType * type ) { 132 122 if ( type->forall.empty() ) return; 133 idMap.endScope(); 123 124 nameMap.endScope(); 134 125 } 135 126 }; … … 151 142 }; 152 143 153 struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ { 154 RenameMode mode; 144 struct RenameVars_new /*: public ast::WithForallSubstitutor*/ { 145 #warning when old RenameVars goes away, replace hack below with global pass inheriting from WithForallSubstitutor 146 ast::ForallSubstitutionTable & subs = renaming.subs; 155 147 156 148 const ast::FunctionType * previsit( const ast::FunctionType * type ) { 157 return renaming.openLevel( type , mode);149 return renaming.openLevel( type ); 158 150 } 159 151 … … 171 163 172 164 const ast::TypeInstType * previsit( const ast::TypeInstType * type ) { 173 if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type174 165 return renaming.rename( type ); 175 166 } … … 186 177 } 187 178 188 const ast::Type * renameTyVars( const ast::Type * t , RenameMode mode, bool reset) {189 //ast::Type *tc = ast::deepCopy(t);179 const ast::Type * renameTyVars( const ast::Type * t ) { 180 ast::Type *tc = ast::deepCopy(t); 190 181 ast::Pass<RenameVars_new> renamer; 191 renamer.core.mode = mode; 192 if (mode == GEN_USAGE && reset) { 193 renaming.nextUsage(); 194 } 195 return t->accept( renamer ); 182 // return t->accept( renamer ); 183 return tc->accept( renamer ); 196 184 } 197 185 198 186 void resetTyVarRenaming() { 199 187 renaming.reset(); 200 renaming.nextUsage();201 188 } 202 189
Note: See TracChangeset
for help on using the changeset viewer.