[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 | //
|
---|
[51b73452] | 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
|
---|
[51b73452] | 20 |
|
---|
[e0e9a0b] | 21 | #include "AST/ForallSubstitutionTable.hpp"
|
---|
[f5edcb4] | 22 | #include "AST/Pass.hpp"
|
---|
| 23 | #include "AST/Type.hpp"
|
---|
[ad51cc2] | 24 | #include "Common/PassVisitor.h"
|
---|
[f5edcb4] | 25 | #include "Common/ScopedMap.h"
|
---|
[ea6332d] | 26 | #include "Common/SemanticError.h" // for SemanticError
|
---|
[51b73452] | 27 | #include "RenameVars.h"
|
---|
[ea6332d] | 28 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl, Dec...
|
---|
| 29 | #include "SynTree/Expression.h" // for Expression
|
---|
| 30 | #include "SynTree/Type.h" // for Type, TypeInstType, TraitInstType
|
---|
| 31 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept
|
---|
[51b73452] | 32 |
|
---|
| 33 | namespace ResolvExpr {
|
---|
[ad51cc2] | 34 |
|
---|
[f5edcb4] | 35 | namespace {
|
---|
| 36 | class RenamingData {
|
---|
| 37 | int level = 0;
|
---|
| 38 | int resetCount = 0;
|
---|
| 39 | ScopedMap< std::string, std::string > nameMap;
|
---|
| 40 | public:
|
---|
[e0e9a0b] | 41 | ast::ForallSubstitutionTable subs;
|
---|
| 42 |
|
---|
[f5edcb4] | 43 | void reset() {
|
---|
[ad51cc2] | 44 | level = 0;
|
---|
[f5edcb4] | 45 | ++resetCount;
|
---|
[ad51cc2] | 46 | }
|
---|
| 47 |
|
---|
[f5edcb4] | 48 | void rename( TypeInstType * type ) {
|
---|
[e0e9a0b] | 49 | auto it = nameMap.find( type->name );
|
---|
[f5edcb4] | 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++;
|
---|
[e0e9a0b] | 67 | }
|
---|
| 68 | }
|
---|
[ad51cc2] | 69 | }
|
---|
| 70 |
|
---|
[f5edcb4] | 71 | void closeLevel( Type * type ) {
|
---|
| 72 | if ( !type->forall.empty() ) {
|
---|
| 73 | nameMap.endScope();
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
|
---|
[e0e9a0b] | 78 | // re-linking of base type handled by WithForallSubstitutor
|
---|
| 79 |
|
---|
| 80 | // rename
|
---|
| 81 | auto it = nameMap.find( type->name );
|
---|
[f5edcb4] | 82 | if ( it != nameMap.end() ) {
|
---|
[e0e9a0b] | 83 | // unconditionally mutate because map will *always* have different name,
|
---|
| 84 | // if this mutates, will *always* have been mutated by ForallSubstitutor above
|
---|
| 85 | ast::TypeInstType * mut = ast::mutate( type );
|
---|
| 86 | mut->name = it->second;
|
---|
| 87 | type = mut;
|
---|
[f5edcb4] | 88 | }
|
---|
[e0e9a0b] | 89 |
|
---|
[f5edcb4] | 90 | return type;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | template<typename NodeT>
|
---|
| 94 | const NodeT * openLevel( const NodeT * type ) {
|
---|
[e0e9a0b] | 95 | if ( type->forall.empty() ) return type;
|
---|
| 96 |
|
---|
| 97 | nameMap.beginScope();
|
---|
| 98 |
|
---|
| 99 | // Load new names from this forall clause and perform renaming.
|
---|
| 100 | NodeT * mutType = ast::mutate( type );
|
---|
| 101 | assert( type == mutType && "mutated type must be unique from ForallSubstitutor" );
|
---|
| 102 | for ( ast::ptr< ast::TypeDecl > & td : mutType->forall ) {
|
---|
| 103 | std::ostringstream output;
|
---|
| 104 | output << "_" << resetCount << "_" << level << "_" << td->name;
|
---|
| 105 | std::string newname = output.str();
|
---|
| 106 | nameMap[ td->name ] = newname;
|
---|
| 107 | ++level;
|
---|
| 108 |
|
---|
| 109 | ast::TypeDecl * mutDecl = ast::mutate( td.get() );
|
---|
| 110 | assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" );
|
---|
| 111 | mutDecl->name = newname;
|
---|
| 112 | // assertion above means `td = mutDecl;` is unnecessary
|
---|
[f5edcb4] | 113 | }
|
---|
[e0e9a0b] | 114 | // assertion above means `type = mutType;` is unnecessary
|
---|
| 115 |
|
---|
[f5edcb4] | 116 | return type;
|
---|
[ad51cc2] | 117 | }
|
---|
[898ae07] | 118 |
|
---|
[e0e9a0b] | 119 | void closeLevel( const ast::ParameterizedType * type ) {
|
---|
| 120 | if ( type->forall.empty() ) return;
|
---|
| 121 |
|
---|
| 122 | nameMap.endScope();
|
---|
[f5edcb4] | 123 | }
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | // Global State:
|
---|
| 127 | RenamingData renaming;
|
---|
| 128 |
|
---|
[e0e9a0b] | 129 | struct RenameVars_old {
|
---|
[f5edcb4] | 130 | void previsit( TypeInstType * instType ) {
|
---|
| 131 | renaming.openLevel( (Type*)instType );
|
---|
| 132 | renaming.rename( instType );
|
---|
| 133 | }
|
---|
| 134 | void previsit( Type * type ) {
|
---|
| 135 | renaming.openLevel( type );
|
---|
| 136 | }
|
---|
| 137 | void postvisit( Type * type ) {
|
---|
| 138 | renaming.closeLevel( type );
|
---|
| 139 | }
|
---|
[e0e9a0b] | 140 | };
|
---|
| 141 |
|
---|
| 142 | struct RenameVars_new /*: public ast::WithForallSubstitutor*/ {
|
---|
| 143 | #warning when old RenameVars goes away, replace hack below with global pass inheriting from WithForallSubstitutor
|
---|
| 144 | ast::ForallSubstitutionTable & subs = renaming.subs;
|
---|
[f5edcb4] | 145 |
|
---|
| 146 | const ast::FunctionType * previsit( const ast::FunctionType * type ) {
|
---|
| 147 | return renaming.openLevel( type );
|
---|
| 148 | }
|
---|
| 149 | const ast::StructInstType * previsit( const ast::StructInstType * type ) {
|
---|
| 150 | return renaming.openLevel( type );
|
---|
| 151 | }
|
---|
| 152 | const ast::UnionInstType * previsit( const ast::UnionInstType * type ) {
|
---|
| 153 | return renaming.openLevel( type );
|
---|
| 154 | }
|
---|
| 155 | const ast::TraitInstType * previsit( const ast::TraitInstType * type ) {
|
---|
| 156 | return renaming.openLevel( type );
|
---|
| 157 | }
|
---|
| 158 | const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
|
---|
| 159 | return renaming.rename( renaming.openLevel( type ) );
|
---|
| 160 | }
|
---|
[e0e9a0b] | 161 | void postvisit( const ast::ParameterizedType * type ) {
|
---|
| 162 | renaming.closeLevel( type );
|
---|
[f5edcb4] | 163 | }
|
---|
| 164 | };
|
---|
| 165 |
|
---|
| 166 | } // namespace
|
---|
| 167 |
|
---|
| 168 | void renameTyVars( Type * t ) {
|
---|
[e0e9a0b] | 169 | PassVisitor<RenameVars_old> renamer;
|
---|
[f5edcb4] | 170 | t->accept( renamer );
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | const ast::Type * renameTyVars( const ast::Type * t ) {
|
---|
[e0e9a0b] | 174 | ast::Pass<RenameVars_new> renamer;
|
---|
[f5edcb4] | 175 | return t->accept( renamer );
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | void resetTyVarRenaming() {
|
---|
| 179 | renaming.reset();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[51b73452] | 182 | } // namespace ResolvExpr
|
---|
[a32b204] | 183 |
|
---|
| 184 | // Local Variables: //
|
---|
| 185 | // tab-width: 4 //
|
---|
| 186 | // mode: c++ //
|
---|
| 187 | // compile-command: "make install" //
|
---|
| 188 | // End: //
|
---|