Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/RenameVars.cc

    r7583c02 r361bf01  
    1919#include <utility>                 // for pair
    2020
     21#include "AST/ForallSubstitutionTable.hpp"
    2122#include "AST/Pass.hpp"
    2223#include "AST/Type.hpp"
     
    3839                int level = 0;
    3940                int resetCount = 0;
     41                ScopedMap< std::string, std::string > nameMap;
     42        public:
     43                ast::ForallSubstitutionTable subs;
    4044
    41                 int next_expr_id = 1;
    42                 int next_usage_id = 1;
    43                 ScopedMap< std::string, std::string > nameMap;
    44                 ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap;
    45         public:
    4645                void reset() {
    4746                        level = 0;
     
    5453                                type->name = it->second;
    5554                        }
    56                 }
    57 
    58                 void nextUsage() {
    59                         ++next_usage_id;
    6055                }
    6156
     
    8378
    8479                const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
     80                        // re-linking of base type handled by WithForallSubstitutor
     81
    8582                        // 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;
     83                        auto it = nameMap.find( type->name );
     84                        if ( it != nameMap.end() ) {
     85                                // unconditionally mutate because map will *always* have different name,
     86                                // if this mutates, will *always* have been mutated by ForallSubstitutor above
     87                                ast::TypeInstType * mut = ast::mutate( type );
     88                                mut->name = it->second;
    9489                    type = mut;
    9590                        }
     
    9893                }
    9994
    100                 const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) {
     95                const ast::FunctionType * openLevel( const ast::FunctionType * type ) {
    10196                        if ( type->forall.empty() ) return type;
    102                         idMap.beginScope();
     97
     98                        nameMap.beginScope();
    10399
    104100                        // 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" );
     101                        auto mutType = ast::mutate( type );
     102                        assert( type == mutType && "mutated type must be unique from ForallSubstitutor" );
     103                        for ( ast::ptr< ast::TypeDecl > & td : mutType->forall ) {
     104                                assertf(dynamic_cast<ast::FunctionType *>(mutType), "renaming vars in non-function type");
     105                                std::ostringstream output;
     106                                output << "_" << resetCount << "_" << level << "_" << td->name;
     107                                std::string newname =  output.str();
     108                                nameMap[ td->name ] = newname;
     109                                ++level;
    110110
    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;
     111                                ast::TypeDecl * mutDecl = ast::mutate( td.get() );
     112                                assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" );
     113                                mutDecl->name = newname;
     114                                // assertion above means `td = mutDecl;` is unnecessary
    126115                        }
     116                        // assertion above means `type = mutType;` is unnecessary
    127117
    128                         return mutType;
     118                        return type;
    129119                }
    130120
    131121                void closeLevel( const ast::FunctionType * type ) {
    132122                        if ( type->forall.empty() ) return;
    133                         idMap.endScope();
     123
     124                        nameMap.endScope();
    134125                }
    135126        };
     
    151142        };
    152143
    153         struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
    154                 RenameMode mode;
     144        struct RenameVars_new /*: public ast::WithForallSubstitutor*/ {
     145                #warning when old RenameVars goes away, replace hack below with global pass inheriting from WithForallSubstitutor
     146                ast::ForallSubstitutionTable & subs = renaming.subs;
    155147
    156148                const ast::FunctionType * previsit( const ast::FunctionType * type ) {
    157                         return renaming.openLevel( type, mode );
     149                        return renaming.openLevel( type );
    158150                }
    159151
     
    171163
    172164                const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
    173                         if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type
    174165                        return renaming.rename( type );
    175166                }
     
    186177}
    187178
    188 const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
    189         // ast::Type *tc = ast::deepCopy(t);
     179const ast::Type * renameTyVars( const ast::Type * t ) {
     180        ast::Type *tc = ast::deepCopy(t);
    190181        ast::Pass<RenameVars_new> renamer;
    191         renamer.core.mode = mode;
    192         if (mode == GEN_USAGE && reset) {
    193                 renaming.nextUsage();
    194         }
    195         return t->accept( renamer );
     182//      return t->accept( renamer );
     183        return tc->accept( renamer );
    196184}
    197185
    198186void resetTyVarRenaming() {
    199187        renaming.reset();
    200         renaming.nextUsage();
    201188}
    202189
Note: See TracChangeset for help on using the changeset viewer.