Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/RenameVars.cc

    re0e9a0b rf5edcb4  
    1919#include <utility>                 // for pair
    2020
    21 #include "AST/ForallSubstitutionTable.hpp"
    2221#include "AST/Pass.hpp"
    2322#include "AST/Type.hpp"
     
    3837                int resetCount = 0;
    3938                ScopedMap< std::string, std::string > nameMap;
     39
    4040        public:
    41                 ast::ForallSubstitutionTable subs;
    42 
    4341                void reset() {
    4442                        level = 0;
     
    4644                }
    4745
     46                using mapConstIterator = ScopedMap< std::string, std::string >::const_iterator;
     47
    4848                void rename( TypeInstType * type ) {
    49                         auto it = nameMap.find( type->name );
     49                        mapConstIterator it = nameMap.find( type->name );
    5050                        if ( it != nameMap.end() ) {
    5151                                type->name = it->second;
     
    6565                                        // ditto for assertion names, the next level in
    6666                                        level++;
    67                                 }
    68                         }
     67                                        // acceptAll( td->assertions, *this );
     68                                } // for
     69                        } // if
    6970                }
    7071
     
    7677
    7778                const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
    78                         // re-linking of base type handled by WithForallSubstitutor
    79 
    80                         // rename
    81                         auto it = nameMap.find( type->name );
     79                        mapConstIterator it = nameMap.find( type->name );
    8280                        if ( it != nameMap.end() ) {
    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;
     81                                ast::TypeInstType * mutType = ast::mutate( type );
     82                                mutType->name = it->second;
     83                    type = mutType;
    8884                        }
    89 
    9085                        return type;
    9186                }
     
    9388                template<typename NodeT>
    9489                const NodeT * openLevel( const NodeT * type ) {
    95                         if ( type->forall.empty() ) return type;
    96                        
    97                         nameMap.beginScope();
     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;
    98100
    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
     101                                        ast::TypeDecl * decl = ast::mutate( td.get() );
     102                                        decl->name = newname;
     103                                        td = decl;
     104                                }
    113105                        }
    114                         // assertion above means `type = mutType;` is unnecessary
    115 
    116106                        return type;
    117107                }
    118108
    119                 void closeLevel( const ast::ParameterizedType * type ) {
    120                         if ( type->forall.empty() ) return;
    121                        
    122                         nameMap.endScope();
     109                template<typename NodeT>
     110                const NodeT * closeLevel( const NodeT * type ) {
     111                        if ( !type->forall.empty() ) {
     112                                nameMap.endScope();
     113                        }
     114                        return type;
    123115                }
    124116        };
     
    127119        RenamingData renaming;
    128120
    129         struct RenameVars_old {
     121        struct RenameVars {
    130122                void previsit( TypeInstType * instType ) {
    131123                        renaming.openLevel( (Type*)instType );
     
    138130                        renaming.closeLevel( type );
    139131                }
    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;
    145132
    146133                const ast::FunctionType * previsit( const ast::FunctionType * type ) {
     
    159146                        return renaming.rename( renaming.openLevel( type ) );
    160147                }
    161                 void postvisit( const ast::ParameterizedType * type ) {
    162                         renaming.closeLevel( type );
     148                const ast::ParameterizedType * postvisit( const ast::ParameterizedType * type ) {
     149                        return renaming.closeLevel( type );
    163150                }
    164151        };
     
    167154
    168155void renameTyVars( Type * t ) {
    169         PassVisitor<RenameVars_old> renamer;
     156        PassVisitor<RenameVars> renamer;
    170157        t->accept( renamer );
    171158}
    172159
    173160const ast::Type * renameTyVars( const ast::Type * t ) {
    174         ast::Pass<RenameVars_new> renamer;
     161        ast::Pass<RenameVars> renamer;
    175162        return t->accept( renamer );
    176163}
Note: See TracChangeset for help on using the changeset viewer.