Ignore:
Timestamp:
Jan 7, 2021, 2:55:57 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
58fe85a
Parents:
bdfc032 (diff), 44e37ef (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into dkobets-vector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/RenameVars.cc

    rbdfc032 reef8dfb  
    3030#include "SynTree/Visitor.h"       // for acceptAll, maybeAccept
    3131
     32#include "AST/Copy.hpp"
     33
    3234namespace ResolvExpr {
    3335
     
    3638                int level = 0;
    3739                int resetCount = 0;
     40
     41                int next_expr_id = 1;
     42                int next_usage_id = 1;
    3843                ScopedMap< std::string, std::string > nameMap;
    39 
     44                ScopedMap< std::string, ast::TypeInstType::TypeEnvKey > idMap;
    4045        public:
    4146                void reset() {
     
    4449                }
    4550
    46                 using mapConstIterator = ScopedMap< std::string, std::string >::const_iterator;
    47 
    4851                void rename( TypeInstType * type ) {
    49                         mapConstIterator it = nameMap.find( type->name );
     52                        auto it = nameMap.find( type->name );
    5053                        if ( it != nameMap.end() ) {
    5154                                type->name = it->second;
    5255                        }
     56                }
     57
     58                void nextUsage() {
     59                        ++next_usage_id;
    5360                }
    5461
     
    6572                                        // ditto for assertion names, the next level in
    6673                                        level++;
    67                                         // acceptAll( td->assertions, *this );
    68                                 } // for
    69                         } // if
     74                                }
     75                        }
    7076                }
    7177
     
    7783
    7884                const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
    79                         mapConstIterator it = nameMap.find( type->name );
    80                         if ( it != nameMap.end() ) {
    81                                 ast::TypeInstType * mutType = ast::mutate( type );
    82                                 mutType->name = it->second;
    83                     type = mutType;
    84                         }
     85                        // 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;
     94                    type = mut;
     95                        }
     96
    8597                        return type;
    8698                }
    8799
    88                 template<typename NodeT>
    89                 const NodeT * openLevel( const NodeT * type ) {
    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;
    100 
    101                                         ast::TypeDecl * decl = ast::mutate( td.get() );
    102                                         decl->name = newname;
    103                                         td = decl;
    104                                 }
    105                         }
    106                         return type;
    107                 }
    108 
    109                 template<typename NodeT>
    110                 const NodeT * closeLevel( const NodeT * type ) {
    111                         if ( !type->forall.empty() ) {
    112                                 nameMap.endScope();
    113                         }
    114                         return type;
     100                const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) {
     101                        if ( type->forall.empty() ) return type;
     102                        idMap.beginScope();
     103
     104                        // 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" );
     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;
     129                }
     130
     131                void closeLevel( const ast::FunctionType * type ) {
     132                        if ( type->forall.empty() ) return;
     133                        idMap.endScope();
    115134                }
    116135        };
     
    119138        RenamingData renaming;
    120139
    121         struct RenameVars {
     140        struct RenameVars_old {
    122141                void previsit( TypeInstType * instType ) {
    123142                        renaming.openLevel( (Type*)instType );
     
    130149                        renaming.closeLevel( type );
    131150                }
     151        };
     152
     153        struct RenameVars_new : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
     154                RenameMode mode;
    132155
    133156                const ast::FunctionType * previsit( const ast::FunctionType * type ) {
    134                         return renaming.openLevel( type );
    135                 }
     157                        return renaming.openLevel( type, mode );
     158                }
     159
     160                /*
    136161                const ast::StructInstType * previsit( const ast::StructInstType * type ) {
    137162                        return renaming.openLevel( type );
     
    143168                        return renaming.openLevel( type );
    144169                }
     170                */
     171
    145172                const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
    146                         return renaming.rename( renaming.openLevel( type ) );
    147                 }
    148                 const ast::ParameterizedType * postvisit( const ast::ParameterizedType * type ) {
    149                         return renaming.closeLevel( type );
     173                        if (mode == GEN_USAGE && !type->formal_usage) return type; // do not rename an actual type
     174                        return renaming.rename( type );
     175                }
     176                void postvisit( const ast::FunctionType * type ) {
     177                        renaming.closeLevel( type );
    150178                }
    151179        };
     
    154182
    155183void renameTyVars( Type * t ) {
    156         PassVisitor<RenameVars> renamer;
     184        PassVisitor<RenameVars_old> renamer;
    157185        t->accept( renamer );
    158186}
    159187
    160 const ast::Type * renameTyVars( const ast::Type * t ) {
    161         ast::Pass<RenameVars> renamer;
     188const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
     189        // ast::Type *tc = ast::deepCopy(t);
     190        ast::Pass<RenameVars_new> renamer;
     191        renamer.core.mode = mode;
     192        if (mode == GEN_USAGE && reset) {
     193                renaming.nextUsage();
     194        }
    162195        return t->accept( renamer );
    163196}
     
    165198void resetTyVarRenaming() {
    166199        renaming.reset();
     200        renaming.nextUsage();
    167201}
    168202
Note: See TracChangeset for help on using the changeset viewer.