[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[8c49c0e] | 7 | // RenameVars.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:05:18 2015 |
---|
[f5edcb4] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thr Jun 20 17:39:00 2019 |
---|
| 13 | // Update Count : 8 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[ea6332d] | 16 | #include <ext/alloc_traits.h> // for __alloc_traits<>::value_type |
---|
| 17 | #include <memory> // for allocator_traits<>::value_type |
---|
| 18 | #include <sstream> // for operator<<, basic_ostream, ostring... |
---|
| 19 | #include <utility> // for pair |
---|
[51b7345] | 20 | |
---|
[f5edcb4] | 21 | #include "AST/Pass.hpp" |
---|
| 22 | #include "AST/Type.hpp" |
---|
[ad51cc2] | 23 | #include "Common/PassVisitor.h" |
---|
[f5edcb4] | 24 | #include "Common/ScopedMap.h" |
---|
[ea6332d] | 25 | #include "Common/SemanticError.h" // for SemanticError |
---|
[51b7345] | 26 | #include "RenameVars.h" |
---|
[ea6332d] | 27 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl, Dec... |
---|
| 28 | #include "SynTree/Expression.h" // for Expression |
---|
| 29 | #include "SynTree/Type.h" // for Type, TypeInstType, TraitInstType |
---|
| 30 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
[51b7345] | 31 | |
---|
[99da267] | 32 | #include "AST/Copy.hpp" |
---|
| 33 | |
---|
[51b7345] | 34 | namespace ResolvExpr { |
---|
[ad51cc2] | 35 | |
---|
[f5edcb4] | 36 | namespace { |
---|
| 37 | class RenamingData { |
---|
| 38 | int level = 0; |
---|
| 39 | int resetCount = 0; |
---|
[3e5dd913] | 40 | |
---|
| 41 | int next_expr_id = 1; |
---|
| 42 | int next_usage_id = 1; |
---|
[f5edcb4] | 43 | ScopedMap< std::string, std::string > nameMap; |
---|
[93c10de] | 44 | ScopedMap< std::string, ast::TypeEnvKey > idMap; |
---|
[f5edcb4] | 45 | public: |
---|
| 46 | void reset() { |
---|
[ad51cc2] | 47 | level = 0; |
---|
[f5edcb4] | 48 | ++resetCount; |
---|
[ad51cc2] | 49 | } |
---|
| 50 | |
---|
[f5edcb4] | 51 | void rename( TypeInstType * type ) { |
---|
[e0e9a0b] | 52 | auto it = nameMap.find( type->name ); |
---|
[f5edcb4] | 53 | if ( it != nameMap.end() ) { |
---|
| 54 | type->name = it->second; |
---|
| 55 | } |
---|
[ad51cc2] | 56 | } |
---|
| 57 | |
---|
[3e5dd913] | 58 | void nextUsage() { |
---|
| 59 | ++next_usage_id; |
---|
| 60 | } |
---|
| 61 | |
---|
[f5edcb4] | 62 | void openLevel( Type * type ) { |
---|
[ad51cc2] | 63 | if ( ! type->forall.empty() ) { |
---|
[f5edcb4] | 64 | nameMap.beginScope(); |
---|
[ad51cc2] | 65 | // renames all "forall" type names to `_${level}_${name}' |
---|
| 66 | for ( auto td : type->forall ) { |
---|
| 67 | std::ostringstream output; |
---|
| 68 | output << "_" << resetCount << "_" << level << "_" << td->name; |
---|
| 69 | std::string newname( output.str() ); |
---|
[f5edcb4] | 70 | nameMap[ td->get_name() ] = newname; |
---|
[ad51cc2] | 71 | td->name = newname; |
---|
| 72 | // ditto for assertion names, the next level in |
---|
| 73 | level++; |
---|
[e0e9a0b] | 74 | } |
---|
| 75 | } |
---|
[ad51cc2] | 76 | } |
---|
| 77 | |
---|
[f5edcb4] | 78 | void closeLevel( Type * type ) { |
---|
| 79 | if ( !type->forall.empty() ) { |
---|
| 80 | nameMap.endScope(); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | const ast::TypeInstType * rename( const ast::TypeInstType * type ) { |
---|
[3e5dd913] | 85 | auto it = idMap.find( type->name ); |
---|
[f76dd1a] | 86 | if ( it == idMap.end() ) return type; |
---|
[e0e9a0b] | 87 | |
---|
[f76dd1a] | 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 | return mut; |
---|
[f5edcb4] | 95 | } |
---|
| 96 | |
---|
[3e5dd913] | 97 | const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) { |
---|
[e0e9a0b] | 98 | if ( type->forall.empty() ) return type; |
---|
[3e5dd913] | 99 | idMap.beginScope(); |
---|
[e0e9a0b] | 100 | |
---|
| 101 | // Load new names from this forall clause and perform renaming. |
---|
[3e5dd913] | 102 | auto mutType = ast::shallowCopy( type ); |
---|
| 103 | // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" ); |
---|
| 104 | for ( auto & td : mutType->forall ) { |
---|
| 105 | auto mut = ast::shallowCopy( td.get() ); |
---|
| 106 | // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" ); |
---|
| 107 | |
---|
| 108 | if (mode == GEN_EXPR_ID) { |
---|
| 109 | mut->expr_id = next_expr_id; |
---|
| 110 | mut->formal_usage = -1; |
---|
| 111 | ++next_expr_id; |
---|
| 112 | } |
---|
| 113 | else if (mode == GEN_USAGE) { |
---|
| 114 | assertf(mut->expr_id, "unfilled expression id in generating candidate type"); |
---|
| 115 | mut->formal_usage = next_usage_id; |
---|
| 116 | } |
---|
| 117 | else { |
---|
| 118 | assert(false); |
---|
| 119 | } |
---|
[93c10de] | 120 | idMap[ td->name ] = ast::TypeEnvKey( *mut ); |
---|
| 121 | |
---|
[3e5dd913] | 122 | td = mut; |
---|
[f5edcb4] | 123 | } |
---|
[e0e9a0b] | 124 | |
---|
[3e5dd913] | 125 | return mutType; |
---|
[ad51cc2] | 126 | } |
---|
[898ae07] | 127 | |
---|
[361bf01] | 128 | void closeLevel( const ast::FunctionType * type ) { |
---|
[e0e9a0b] | 129 | if ( type->forall.empty() ) return; |
---|
[3e5dd913] | 130 | idMap.endScope(); |
---|
[f5edcb4] | 131 | } |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | // Global State: |
---|
| 135 | RenamingData renaming; |
---|
| 136 | |
---|
[e0e9a0b] | 137 | struct RenameVars_old { |
---|
[f5edcb4] | 138 | void previsit( TypeInstType * instType ) { |
---|
| 139 | renaming.openLevel( (Type*)instType ); |
---|
| 140 | renaming.rename( instType ); |
---|
| 141 | } |
---|
| 142 | void previsit( Type * type ) { |
---|
| 143 | renaming.openLevel( type ); |
---|
| 144 | } |
---|
| 145 | void postvisit( Type * type ) { |
---|
| 146 | renaming.closeLevel( type ); |
---|
| 147 | } |
---|
[e0e9a0b] | 148 | }; |
---|
[4e13e2a] | 149 | |
---|
[3e5dd913] | 150 | struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ { |
---|
| 151 | RenameMode mode; |
---|
[f5edcb4] | 152 | |
---|
| 153 | const ast::FunctionType * previsit( const ast::FunctionType * type ) { |
---|
[3e5dd913] | 154 | return renaming.openLevel( type, mode ); |
---|
[f5edcb4] | 155 | } |
---|
[361bf01] | 156 | |
---|
| 157 | /* |
---|
[f5edcb4] | 158 | const ast::StructInstType * previsit( const ast::StructInstType * type ) { |
---|
| 159 | return renaming.openLevel( type ); |
---|
| 160 | } |
---|
| 161 | const ast::UnionInstType * previsit( const ast::UnionInstType * type ) { |
---|
| 162 | return renaming.openLevel( type ); |
---|
| 163 | } |
---|
| 164 | const ast::TraitInstType * previsit( const ast::TraitInstType * type ) { |
---|
| 165 | return renaming.openLevel( type ); |
---|
| 166 | } |
---|
[361bf01] | 167 | */ |
---|
| 168 | |
---|
[f5edcb4] | 169 | const ast::TypeInstType * previsit( const ast::TypeInstType * type ) { |
---|
[3e5dd913] | 170 | if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type |
---|
[361bf01] | 171 | return renaming.rename( type ); |
---|
[f5edcb4] | 172 | } |
---|
[361bf01] | 173 | void postvisit( const ast::FunctionType * type ) { |
---|
[e0e9a0b] | 174 | renaming.closeLevel( type ); |
---|
[f5edcb4] | 175 | } |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | } // namespace |
---|
| 179 | |
---|
| 180 | void renameTyVars( Type * t ) { |
---|
[e0e9a0b] | 181 | PassVisitor<RenameVars_old> renamer; |
---|
[f5edcb4] | 182 | t->accept( renamer ); |
---|
| 183 | } |
---|
| 184 | |
---|
[7583c02] | 185 | const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) { |
---|
[e0e9a0b] | 186 | ast::Pass<RenameVars_new> renamer; |
---|
[3e5dd913] | 187 | renamer.core.mode = mode; |
---|
[7583c02] | 188 | if (mode == GEN_USAGE && reset) { |
---|
[3e5dd913] | 189 | renaming.nextUsage(); |
---|
| 190 | } |
---|
| 191 | return t->accept( renamer ); |
---|
[f5edcb4] | 192 | } |
---|
| 193 | |
---|
| 194 | void resetTyVarRenaming() { |
---|
| 195 | renaming.reset(); |
---|
[7583c02] | 196 | renaming.nextUsage(); |
---|
[f5edcb4] | 197 | } |
---|
| 198 | |
---|
[51b7345] | 199 | } // namespace ResolvExpr |
---|
[a32b204] | 200 | |
---|
| 201 | // Local Variables: // |
---|
| 202 | // tab-width: 4 // |
---|
| 203 | // mode: c++ // |
---|
| 204 | // compile-command: "make install" // |
---|
| 205 | // End: // |
---|