Changeset 58fe85a for src/ResolvExpr/RenameVars.cc
- Timestamp:
- Jan 7, 2021, 3:27:00 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 2b4daf2, 64aeca0
- Parents:
- 3c64c668 (diff), eef8dfb (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/ResolvExpr/RenameVars.cc
r3c64c668 r58fe85a 30 30 #include "SynTree/Visitor.h" // for acceptAll, maybeAccept 31 31 32 #include "AST/Copy.hpp" 33 32 34 namespace ResolvExpr { 33 35 … … 36 38 int level = 0; 37 39 int resetCount = 0; 40 41 int next_expr_id = 1; 42 int next_usage_id = 1; 38 43 ScopedMap< std::string, std::string > nameMap; 39 44 ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap; 40 45 public: 41 46 void reset() { … … 44 49 } 45 50 46 using mapConstIterator = ScopedMap< std::string, std::string >::const_iterator;47 48 51 void rename( TypeInstType * type ) { 49 mapConstIteratorit = nameMap.find( type->name );52 auto it = nameMap.find( type->name ); 50 53 if ( it != nameMap.end() ) { 51 54 type->name = it->second; 52 55 } 56 } 57 58 void nextUsage() { 59 ++next_usage_id; 53 60 } 54 61 … … 65 72 // ditto for assertion names, the next level in 66 73 level++; 67 // acceptAll( td->assertions, *this ); 68 } // for 69 } // if 74 } 75 } 70 76 } 71 77 … … 77 83 78 84 const ast::TypeInstType * rename( const ast::TypeInstType * type ) { 79 mapConstIterator it = nameMap.find( type->name ); 80 if ( it != nameMap.end() ) { 81 ast::TypeInstType * mutType = ast::mutate( type ); 82 mutType->name = it->second; 83 type = mutType; 84 } 85 // 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; 94 type = mut; 95 } 96 85 97 return type; 86 98 } 87 99 88 template<typename NodeT> 89 const NodeT * openLevel( const NodeT * 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; 100 101 ast::TypeDecl * decl = ast::mutate( td.get() ); 102 decl->name = newname; 103 td = decl; 104 } 105 } 106 return type; 107 } 108 109 template<typename NodeT> 110 const NodeT * closeLevel( const NodeT * type ) { 111 if ( !type->forall.empty() ) { 112 nameMap.endScope(); 113 } 114 return type; 100 const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) { 101 if ( type->forall.empty() ) return type; 102 idMap.beginScope(); 103 104 // 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" ); 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; 126 } 127 128 return mutType; 129 } 130 131 void closeLevel( const ast::FunctionType * type ) { 132 if ( type->forall.empty() ) return; 133 idMap.endScope(); 115 134 } 116 135 }; … … 119 138 RenamingData renaming; 120 139 121 struct RenameVars {140 struct RenameVars_old { 122 141 void previsit( TypeInstType * instType ) { 123 142 renaming.openLevel( (Type*)instType ); … … 130 149 renaming.closeLevel( type ); 131 150 } 151 }; 152 153 struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ { 154 RenameMode mode; 132 155 133 156 const ast::FunctionType * previsit( const ast::FunctionType * type ) { 134 return renaming.openLevel( type ); 135 } 157 return renaming.openLevel( type, mode ); 158 } 159 160 /* 136 161 const ast::StructInstType * previsit( const ast::StructInstType * type ) { 137 162 return renaming.openLevel( type ); … … 143 168 return renaming.openLevel( type ); 144 169 } 170 */ 171 145 172 const ast::TypeInstType * previsit( const ast::TypeInstType * type ) { 146 return renaming.rename( renaming.openLevel( type ) ); 147 } 148 const ast::ParameterizedType * postvisit( const ast::ParameterizedType * type ) { 149 return renaming.closeLevel( type ); 173 if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type 174 return renaming.rename( type ); 175 } 176 void postvisit( const ast::FunctionType * type ) { 177 renaming.closeLevel( type ); 150 178 } 151 179 }; … … 154 182 155 183 void renameTyVars( Type * t ) { 156 PassVisitor<RenameVars > renamer;184 PassVisitor<RenameVars_old> renamer; 157 185 t->accept( renamer ); 158 186 } 159 187 160 const ast::Type * renameTyVars( const ast::Type * t ) { 161 ast::Pass<RenameVars> renamer; 188 const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) { 189 // ast::Type *tc = ast::deepCopy(t); 190 ast::Pass<RenameVars_new> renamer; 191 renamer.core.mode = mode; 192 if (mode == GEN_USAGE && reset) { 193 renaming.nextUsage(); 194 } 162 195 return t->accept( renamer ); 163 196 } … … 165 198 void resetTyVarRenaming() { 166 199 renaming.reset(); 200 renaming.nextUsage(); 167 201 } 168 202
Note:
See TracChangeset
for help on using the changeset viewer.