[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 |
|
---|
[f5edcb4] | 21 | #include "AST/Pass.hpp"
|
---|
| 22 | #include "AST/Type.hpp"
|
---|
| 23 | #include "Common/ScopedMap.h"
|
---|
[ea6332d] | 24 | #include "Common/SemanticError.h" // for SemanticError
|
---|
[51b73452] | 25 | #include "RenameVars.h"
|
---|
| 26 |
|
---|
[99da267] | 27 | #include "AST/Copy.hpp"
|
---|
| 28 |
|
---|
[51b73452] | 29 | namespace ResolvExpr {
|
---|
[ad51cc2] | 30 |
|
---|
[f5edcb4] | 31 | namespace {
|
---|
| 32 | class RenamingData {
|
---|
| 33 | int level = 0;
|
---|
| 34 | int resetCount = 0;
|
---|
[3e5dd913] | 35 |
|
---|
| 36 | int next_expr_id = 1;
|
---|
| 37 | int next_usage_id = 1;
|
---|
[f5edcb4] | 38 | ScopedMap< std::string, std::string > nameMap;
|
---|
[93c10de] | 39 | ScopedMap< std::string, ast::TypeEnvKey > idMap;
|
---|
[f5edcb4] | 40 | public:
|
---|
| 41 | void reset() {
|
---|
[ad51cc2] | 42 | level = 0;
|
---|
[f5edcb4] | 43 | ++resetCount;
|
---|
[ad51cc2] | 44 | }
|
---|
| 45 |
|
---|
[3e5dd913] | 46 | void nextUsage() {
|
---|
| 47 | ++next_usage_id;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[f5edcb4] | 50 | const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
|
---|
[3e5dd913] | 51 | auto it = idMap.find( type->name );
|
---|
[f76dd1a] | 52 | if ( it == idMap.end() ) return type;
|
---|
[e0e9a0b] | 53 |
|
---|
[f76dd1a] | 54 | // Unconditionally mutate because map will *always* have different name.
|
---|
| 55 | ast::TypeInstType * mut = ast::shallowCopy( type );
|
---|
| 56 | // Reconcile base node since some copies might have been made.
|
---|
| 57 | mut->base = it->second.base;
|
---|
| 58 | mut->formal_usage = it->second.formal_usage;
|
---|
| 59 | mut->expr_id = it->second.expr_id;
|
---|
| 60 | return mut;
|
---|
[f5edcb4] | 61 | }
|
---|
| 62 |
|
---|
[3e5dd913] | 63 | const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) {
|
---|
[e0e9a0b] | 64 | if ( type->forall.empty() ) return type;
|
---|
[3e5dd913] | 65 | idMap.beginScope();
|
---|
[e0e9a0b] | 66 |
|
---|
| 67 | // Load new names from this forall clause and perform renaming.
|
---|
[3e5dd913] | 68 | auto mutType = ast::shallowCopy( type );
|
---|
| 69 | // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" );
|
---|
| 70 | for ( auto & td : mutType->forall ) {
|
---|
| 71 | auto mut = ast::shallowCopy( td.get() );
|
---|
| 72 | // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" );
|
---|
| 73 |
|
---|
| 74 | if (mode == GEN_EXPR_ID) {
|
---|
| 75 | mut->expr_id = next_expr_id;
|
---|
| 76 | mut->formal_usage = -1;
|
---|
| 77 | ++next_expr_id;
|
---|
| 78 | }
|
---|
| 79 | else if (mode == GEN_USAGE) {
|
---|
| 80 | assertf(mut->expr_id, "unfilled expression id in generating candidate type");
|
---|
| 81 | mut->formal_usage = next_usage_id;
|
---|
| 82 | }
|
---|
| 83 | else {
|
---|
| 84 | assert(false);
|
---|
| 85 | }
|
---|
[93c10de] | 86 | idMap[ td->name ] = ast::TypeEnvKey( *mut );
|
---|
| 87 |
|
---|
[3e5dd913] | 88 | td = mut;
|
---|
[f5edcb4] | 89 | }
|
---|
[e0e9a0b] | 90 |
|
---|
[3e5dd913] | 91 | return mutType;
|
---|
[ad51cc2] | 92 | }
|
---|
[898ae07] | 93 |
|
---|
[361bf01] | 94 | void closeLevel( const ast::FunctionType * type ) {
|
---|
[e0e9a0b] | 95 | if ( type->forall.empty() ) return;
|
---|
[3e5dd913] | 96 | idMap.endScope();
|
---|
[f5edcb4] | 97 | }
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | // Global State:
|
---|
| 101 | RenamingData renaming;
|
---|
| 102 |
|
---|
[0bd3faf] | 103 | struct RenameVars final : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
|
---|
[3e5dd913] | 104 | RenameMode mode;
|
---|
[f5edcb4] | 105 |
|
---|
| 106 | const ast::FunctionType * previsit( const ast::FunctionType * type ) {
|
---|
[3e5dd913] | 107 | return renaming.openLevel( type, mode );
|
---|
[f5edcb4] | 108 | }
|
---|
[361bf01] | 109 |
|
---|
| 110 | /*
|
---|
[f5edcb4] | 111 | const ast::StructInstType * previsit( const ast::StructInstType * type ) {
|
---|
| 112 | return renaming.openLevel( type );
|
---|
| 113 | }
|
---|
| 114 | const ast::UnionInstType * previsit( const ast::UnionInstType * type ) {
|
---|
| 115 | return renaming.openLevel( type );
|
---|
| 116 | }
|
---|
| 117 | const ast::TraitInstType * previsit( const ast::TraitInstType * type ) {
|
---|
| 118 | return renaming.openLevel( type );
|
---|
| 119 | }
|
---|
[361bf01] | 120 | */
|
---|
| 121 |
|
---|
[f5edcb4] | 122 | const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
|
---|
[3e5dd913] | 123 | if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type
|
---|
[361bf01] | 124 | return renaming.rename( type );
|
---|
[f5edcb4] | 125 | }
|
---|
[361bf01] | 126 | void postvisit( const ast::FunctionType * type ) {
|
---|
[e0e9a0b] | 127 | renaming.closeLevel( type );
|
---|
[f5edcb4] | 128 | }
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | } // namespace
|
---|
| 132 |
|
---|
[7583c02] | 133 | const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
|
---|
[0bd3faf] | 134 | ast::Pass<RenameVars> renamer;
|
---|
[3e5dd913] | 135 | renamer.core.mode = mode;
|
---|
[7583c02] | 136 | if (mode == GEN_USAGE && reset) {
|
---|
[3e5dd913] | 137 | renaming.nextUsage();
|
---|
| 138 | }
|
---|
| 139 | return t->accept( renamer );
|
---|
[f5edcb4] | 140 | }
|
---|
| 141 |
|
---|
| 142 | void resetTyVarRenaming() {
|
---|
| 143 | renaming.reset();
|
---|
[7583c02] | 144 | renaming.nextUsage();
|
---|
[f5edcb4] | 145 | }
|
---|
| 146 |
|
---|
[51b73452] | 147 | } // namespace ResolvExpr
|
---|
[a32b204] | 148 |
|
---|
| 149 | // Local Variables: //
|
---|
| 150 | // tab-width: 4 //
|
---|
| 151 | // mode: c++ //
|
---|
| 152 | // compile-command: "make install" //
|
---|
| 153 | // End: //
|
---|