[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 | |
---|
| 32 | namespace ResolvExpr { |
---|
[ad51cc2] | 33 | |
---|
[f5edcb4] | 34 | namespace { |
---|
| 35 | class RenamingData { |
---|
| 36 | int level = 0; |
---|
| 37 | int resetCount = 0; |
---|
| 38 | ScopedMap< std::string, std::string > nameMap; |
---|
[ad51cc2] | 39 | |
---|
[f5edcb4] | 40 | public: |
---|
| 41 | void reset() { |
---|
[ad51cc2] | 42 | level = 0; |
---|
[f5edcb4] | 43 | ++resetCount; |
---|
[ad51cc2] | 44 | } |
---|
| 45 | |
---|
[f5edcb4] | 46 | using mapConstIterator = ScopedMap< std::string, std::string >::const_iterator; |
---|
| 47 | |
---|
| 48 | void rename( TypeInstType * type ) { |
---|
| 49 | mapConstIterator it = nameMap.find( type->name ); |
---|
| 50 | if ( it != nameMap.end() ) { |
---|
| 51 | type->name = it->second; |
---|
| 52 | } |
---|
[ad51cc2] | 53 | } |
---|
| 54 | |
---|
[f5edcb4] | 55 | void openLevel( Type * type ) { |
---|
[ad51cc2] | 56 | if ( ! type->forall.empty() ) { |
---|
[f5edcb4] | 57 | nameMap.beginScope(); |
---|
[ad51cc2] | 58 | // renames all "forall" type names to `_${level}_${name}' |
---|
| 59 | for ( auto td : type->forall ) { |
---|
| 60 | std::ostringstream output; |
---|
| 61 | output << "_" << resetCount << "_" << level << "_" << td->name; |
---|
| 62 | std::string newname( output.str() ); |
---|
[f5edcb4] | 63 | nameMap[ td->get_name() ] = newname; |
---|
[ad51cc2] | 64 | td->name = newname; |
---|
| 65 | // ditto for assertion names, the next level in |
---|
| 66 | level++; |
---|
| 67 | // acceptAll( td->assertions, *this ); |
---|
| 68 | } // for |
---|
| 69 | } // if |
---|
| 70 | } |
---|
| 71 | |
---|
[f5edcb4] | 72 | void closeLevel( Type * type ) { |
---|
| 73 | if ( !type->forall.empty() ) { |
---|
| 74 | nameMap.endScope(); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | 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 | return type; |
---|
| 86 | } |
---|
| 87 | |
---|
| 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; |
---|
[ad51cc2] | 107 | } |
---|
[898ae07] | 108 | |
---|
[f5edcb4] | 109 | template<typename NodeT> |
---|
| 110 | const NodeT * closeLevel( const NodeT * type ) { |
---|
| 111 | if ( !type->forall.empty() ) { |
---|
| 112 | nameMap.endScope(); |
---|
| 113 | } |
---|
| 114 | return type; |
---|
| 115 | } |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | // Global State: |
---|
| 119 | RenamingData renaming; |
---|
| 120 | |
---|
| 121 | struct RenameVars { |
---|
| 122 | void previsit( TypeInstType * instType ) { |
---|
| 123 | renaming.openLevel( (Type*)instType ); |
---|
| 124 | renaming.rename( instType ); |
---|
| 125 | } |
---|
| 126 | void previsit( Type * type ) { |
---|
| 127 | renaming.openLevel( type ); |
---|
| 128 | } |
---|
| 129 | void postvisit( Type * type ) { |
---|
| 130 | renaming.closeLevel( type ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | const ast::FunctionType * previsit( const ast::FunctionType * type ) { |
---|
| 134 | return renaming.openLevel( type ); |
---|
| 135 | } |
---|
| 136 | const ast::StructInstType * previsit( const ast::StructInstType * type ) { |
---|
| 137 | return renaming.openLevel( type ); |
---|
| 138 | } |
---|
| 139 | const ast::UnionInstType * previsit( const ast::UnionInstType * type ) { |
---|
| 140 | return renaming.openLevel( type ); |
---|
| 141 | } |
---|
| 142 | const ast::TraitInstType * previsit( const ast::TraitInstType * type ) { |
---|
| 143 | return renaming.openLevel( type ); |
---|
| 144 | } |
---|
| 145 | 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 ); |
---|
| 150 | } |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | } // namespace |
---|
| 154 | |
---|
| 155 | void renameTyVars( Type * t ) { |
---|
| 156 | PassVisitor<RenameVars> renamer; |
---|
| 157 | t->accept( renamer ); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | const ast::Type * renameTyVars( const ast::Type * t ) { |
---|
| 161 | ast::Pass<RenameVars> renamer; |
---|
| 162 | return t->accept( renamer ); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void resetTyVarRenaming() { |
---|
| 166 | renaming.reset(); |
---|
| 167 | } |
---|
| 168 | |
---|
[51b7345] | 169 | } // namespace ResolvExpr |
---|
[a32b204] | 170 | |
---|
| 171 | // Local Variables: // |
---|
| 172 | // tab-width: 4 // |
---|
| 173 | // mode: c++ // |
---|
| 174 | // compile-command: "make install" // |
---|
| 175 | // End: // |
---|