Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/RenameVars.cc

    r361bf01 r7583c02  
    1919#include <utility>                 // for pair
    2020
    21 #include "AST/ForallSubstitutionTable.hpp"
    2221#include "AST/Pass.hpp"
    2322#include "AST/Type.hpp"
     
    3938                int level = 0;
    4039                int resetCount = 0;
     40
     41                int next_expr_id = 1;
     42                int next_usage_id = 1;
    4143                ScopedMap< std::string, std::string > nameMap;
     44                ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap;
    4245        public:
    43                 ast::ForallSubstitutionTable subs;
    44 
    4546                void reset() {
    4647                        level = 0;
     
    5354                                type->name = it->second;
    5455                        }
     56                }
     57
     58                void nextUsage() {
     59                        ++next_usage_id;
    5560                }
    5661
     
    7883
    7984                const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
    80                         // re-linking of base type handled by WithForallSubstitutor
    81 
    8285                        // rename
    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;
     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;
    8994                    type = mut;
    9095                        }
     
    9398                }
    9499
    95                 const ast::FunctionType * openLevel( const ast::FunctionType * type ) {
     100                const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) {
    96101                        if ( type->forall.empty() ) return type;
    97 
    98                         nameMap.beginScope();
     102                        idMap.beginScope();
    99103
    100104                        // Load new names from this forall clause and perform renaming.
    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;
    110 
    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
    115                         }
    116                         // assertion above means `type = mutType;` is unnecessary
    117 
    118                         return type;
     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" );
     110
     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;
     126                        }
     127
     128                        return mutType;
    119129                }
    120130
    121131                void closeLevel( const ast::FunctionType * type ) {
    122132                        if ( type->forall.empty() ) return;
    123 
    124                         nameMap.endScope();
     133                        idMap.endScope();
    125134                }
    126135        };
     
    142151        };
    143152
    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;
     153        struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
     154                RenameMode mode;
    147155
    148156                const ast::FunctionType * previsit( const ast::FunctionType * type ) {
    149                         return renaming.openLevel( type );
     157                        return renaming.openLevel( type, mode );
    150158                }
    151159
     
    163171
    164172                const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
     173                        if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type
    165174                        return renaming.rename( type );
    166175                }
     
    177186}
    178187
    179 const ast::Type * renameTyVars( const ast::Type * t ) {
    180         ast::Type *tc = ast::deepCopy(t);
     188const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
     189        // ast::Type *tc = ast::deepCopy(t);
    181190        ast::Pass<RenameVars_new> renamer;
    182 //      return t->accept( renamer );
    183         return tc->accept( renamer );
     191        renamer.core.mode = mode;
     192        if (mode == GEN_USAGE && reset) {
     193                renaming.nextUsage();
     194        }
     195        return t->accept( renamer );
    184196}
    185197
    186198void resetTyVarRenaming() {
    187199        renaming.reset();
     200        renaming.nextUsage();
    188201}
    189202
Note: See TracChangeset for help on using the changeset viewer.