Changes in src/ResolvExpr/RenameVars.cc [4e13e2a:f5edcb4]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/RenameVars.cc
r4e13e2a rf5edcb4 19 19 #include <utility> // for pair 20 20 21 #include "AST/ForallSubstitutionTable.hpp"22 21 #include "AST/Pass.hpp" 23 22 #include "AST/Type.hpp" … … 31 30 #include "SynTree/Visitor.h" // for acceptAll, maybeAccept 32 31 33 #include "AST/Copy.hpp"34 35 32 namespace ResolvExpr { 36 33 … … 40 37 int resetCount = 0; 41 38 ScopedMap< std::string, std::string > nameMap; 39 42 40 public: 43 ast::ForallSubstitutionTable subs;44 45 41 void reset() { 46 42 level = 0; … … 48 44 } 49 45 46 using mapConstIterator = ScopedMap< std::string, std::string >::const_iterator; 47 50 48 void rename( TypeInstType * type ) { 51 autoit = nameMap.find( type->name );49 mapConstIterator it = nameMap.find( type->name ); 52 50 if ( it != nameMap.end() ) { 53 51 type->name = it->second; … … 67 65 // ditto for assertion names, the next level in 68 66 level++; 69 } 70 } 67 // acceptAll( td->assertions, *this ); 68 } // for 69 } // if 71 70 } 72 71 … … 78 77 79 78 const ast::TypeInstType * rename( const ast::TypeInstType * type ) { 80 // re-linking of base type handled by WithForallSubstitutor 81 82 // rename 83 auto it = nameMap.find( type->name ); 79 mapConstIterator it = nameMap.find( type->name ); 84 80 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; 89 type = mut; 81 ast::TypeInstType * mutType = ast::mutate( type ); 82 mutType->name = it->second; 83 type = mutType; 90 84 } 91 92 85 return type; 93 86 } … … 95 88 template<typename NodeT> 96 89 const NodeT * openLevel( const NodeT * type ) { 97 if ( type->forall.empty() ) return type; 90 if ( !type->forall.empty() ) { 91 nameMap.beginScope(); 92 // Load new names from this forall clause and perform renaming. 93 NodeT * mutType = ast::mutate( type ); 94 for ( ast::ptr< ast::TypeDecl > & td : mutType->forall ) { 95 std::ostringstream output; 96 output << "_" << resetCount << "_" << level << "_" << td->name; 97 std::string newname( output.str() ); 98 nameMap[ td->name ] = newname; 99 ++level; 98 100 99 nameMap.beginScope(); 100 101 // Load new names from this forall clause and perform renaming. 102 NodeT * mutType = ast::mutate( type ); 103 assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); 104 for ( ast::ptr< ast::TypeDecl > & td : mutType->forall ) { 105 std::ostringstream output; 106 output << "_" << resetCount << "_" << level << "_" << td->name; 107 std::string newname = output.str(); 108 nameMap[ td->name ] = newname; 109 ++level; 110 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 101 ast::TypeDecl * decl = ast::mutate( td.get() ); 102 decl->name = newname; 103 td = decl; 104 } 115 105 } 116 // assertion above means `type = mutType;` is unnecessary117 118 106 return type; 119 107 } 120 108 121 void closeLevel( const ast::ParameterizedType * type ) { 122 if ( type->forall.empty() ) return; 123 124 nameMap.endScope(); 109 template<typename NodeT> 110 const NodeT * closeLevel( const NodeT * type ) { 111 if ( !type->forall.empty() ) { 112 nameMap.endScope(); 113 } 114 return type; 125 115 } 126 116 }; … … 129 119 RenamingData renaming; 130 120 131 struct RenameVars _old{121 struct RenameVars { 132 122 void previsit( TypeInstType * instType ) { 133 123 renaming.openLevel( (Type*)instType ); … … 140 130 renaming.closeLevel( type ); 141 131 } 142 };143 144 struct RenameVars_new /*: public ast::WithForallSubstitutor*/ {145 #warning when old RenameVars goes away, replace hack below with global pass inheriting from WithForallSubstitutor146 ast::ForallSubstitutionTable & subs = renaming.subs;147 132 148 133 const ast::FunctionType * previsit( const ast::FunctionType * type ) { … … 161 146 return renaming.rename( renaming.openLevel( type ) ); 162 147 } 163 voidpostvisit( const ast::ParameterizedType * type ) {164 re naming.closeLevel( type );148 const ast::ParameterizedType * postvisit( const ast::ParameterizedType * type ) { 149 return renaming.closeLevel( type ); 165 150 } 166 151 }; … … 169 154 170 155 void renameTyVars( Type * t ) { 171 PassVisitor<RenameVars _old> renamer;156 PassVisitor<RenameVars> renamer; 172 157 t->accept( renamer ); 173 158 } 174 159 175 160 const ast::Type * renameTyVars( const ast::Type * t ) { 176 ast::Type *tc = ast::deepCopy(t); 177 ast::Pass<RenameVars_new> renamer; 178 // return t->accept( renamer ); 179 return tc->accept( renamer ); 161 ast::Pass<RenameVars> renamer; 162 return t->accept( renamer ); 180 163 } 181 164
Note:
See TracChangeset
for help on using the changeset viewer.