Changeset ad51cc2 for src/ResolvExpr


Ignore:
Timestamp:
Jan 16, 2018, 1:37:40 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
d7d9a60
Parents:
f07c1e6
Message:

Convert RenameVars? to PassVisitor?

Location:
src/ResolvExpr
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    rf07c1e6 rad51cc2  
    152152
    153153                void renameTypes( Expression *expr ) {
    154                         expr->get_result()->accept( global_renamer );
     154                        renameTyVars( expr->result );
    155155                }
    156156        } // namespace
     
    485485                        Type *adjType = candidate->get_type()->clone();
    486486                        adjustExprType( adjType, newEnv, indexer );
    487                         adjType->accept( global_renamer );
     487                        renameTyVars( adjType );
    488488                        PRINT(
    489489                                std::cerr << "unifying ";
     
    595595
    596596                ArgPack()
    597                         : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0), 
     597                        : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0),
    598598                          tupleStart(0), nextExpl(0), explAlt(0) {}
    599599
     
    706706
    707707                                                if ( nTuples > 0 || ! results[i].expr ) {
    708                                                         // first iteration or no expression to clone, 
     708                                                        // first iteration or no expression to clone,
    709709                                                        // push empty tuple expression
    710710                                                        newResult.parent = i;
  • src/ResolvExpr/RenameVars.cc

    rf07c1e6 rad51cc2  
    1919#include <utility>                 // for pair
    2020
     21#include "Common/PassVisitor.h"
    2122#include "Common/SemanticError.h"  // for SemanticError
    2223#include "RenameVars.h"
     
    2728
    2829namespace ResolvExpr {
    29         RenameVars global_renamer;
     30        namespace {
     31                struct RenameVars {
     32                        RenameVars();
     33                        void reset();
    3034
    31         RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
    32                 mapStack.push_front( std::map< std::string, std::string >() );
     35                        void previsit( TypeInstType * instType );
     36                        void previsit( Type * );
     37                        void postvisit( Type * );
     38
     39                  private:
     40                        int level, resetCount;
     41                        std::list< std::map< std::string, std::string > > mapStack;
     42                };
     43
     44                PassVisitor<RenameVars> global_renamer;
     45        } // namespace
     46
     47        void renameTyVars( Type * t ) {
     48                t->accept( global_renamer );
    3349        }
    3450
    35         void RenameVars::reset() {
    36                 level = 0;
    37                 resetCount++;
     51        void resetTyVarRenaming() {
     52                global_renamer.pass.reset();
    3853        }
    3954
    40         void RenameVars::visit( VoidType *voidType ) {
    41                 typeBefore( voidType );
    42                 typeAfter( voidType );
    43         }
     55        namespace {
     56                RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
     57                        mapStack.push_front( std::map< std::string, std::string >() );
     58                }
    4459
    45         void RenameVars::visit( BasicType *basicType ) {
    46                 typeBefore( basicType );
    47                 typeAfter( basicType );
    48         }
     60                void RenameVars::reset() {
     61                        level = 0;
     62                        resetCount++;
     63                }
    4964
    50         void RenameVars::visit( PointerType *pointerType ) {
    51                 typeBefore( pointerType );
    52                 maybeAccept( pointerType->get_base(), *this );
    53                 typeAfter( pointerType );
    54         }
     65                void RenameVars::previsit( TypeInstType * instType ) {
     66                        previsit( (Type *)instType );
     67                        std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->name );
     68                        if ( i != mapStack.front().end() ) {
     69                                instType->name = i->second;
     70                        } // if
     71                }
    5572
    56         void RenameVars::visit( ArrayType *arrayType ) {
    57                 typeBefore( arrayType );
    58                 maybeAccept( arrayType->get_dimension(), *this );
    59                 maybeAccept( arrayType->get_base(), *this );
    60                 typeAfter( arrayType );
    61         }
     73                void RenameVars::previsit( Type * type ) {
     74                        if ( ! type->forall.empty() ) {
     75                                // copies current name mapping into new mapping
     76                                mapStack.push_front( mapStack.front() );
     77                                // renames all "forall" type names to `_${level}_${name}'
     78                                for ( auto td : type->forall ) {
     79                                        std::ostringstream output;
     80                                        output << "_" << resetCount << "_" << level << "_" << td->name;
     81                                        std::string newname( output.str() );
     82                                        mapStack.front()[ td->get_name() ] = newname;
     83                                        td->name = newname;
     84                                        // ditto for assertion names, the next level in
     85                                        level++;
     86                                        // acceptAll( td->assertions, *this );
     87                                } // for
     88                        } // if
     89                }
    6290
    63         void RenameVars::visit( FunctionType *functionType ) {
    64                 typeBefore( functionType );
    65                 acceptAll( functionType->get_returnVals(), *this );
    66                 acceptAll( functionType->get_parameters(), *this );
    67                 typeAfter( functionType );
    68         }
    69 
    70         void RenameVars::visit( StructInstType *aggregateUseType ) {
    71                 typeBefore( aggregateUseType );
    72                 acceptAll( aggregateUseType->get_parameters(), *this );
    73                 typeAfter( aggregateUseType );
    74         }
    75 
    76         void RenameVars::visit( UnionInstType *aggregateUseType ) {
    77                 typeBefore( aggregateUseType );
    78                 acceptAll( aggregateUseType->get_parameters(), *this );
    79                 typeAfter( aggregateUseType );
    80         }
    81 
    82         void RenameVars::visit( EnumInstType *aggregateUseType ) {
    83                 typeBefore( aggregateUseType );
    84                 acceptAll( aggregateUseType->get_parameters(), *this );
    85                 typeAfter( aggregateUseType );
    86         }
    87 
    88         void RenameVars::visit( TraitInstType *aggregateUseType ) {
    89                 typeBefore( aggregateUseType );
    90                 acceptAll( aggregateUseType->get_parameters(), *this );
    91                 typeAfter( aggregateUseType );
    92         }
    93 
    94         void RenameVars::visit( TypeInstType *instType ) {
    95                 typeBefore( instType );
    96                 std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() );
    97                 if ( i != mapStack.front().end() ) {
    98                         instType->set_name( i->second );
    99                 } else {
    100                 } // if
    101                 acceptAll( instType->get_parameters(), *this );
    102                 typeAfter( instType );
    103         }
    104 
    105         void RenameVars::visit( TupleType *tupleType ) {
    106                 typeBefore( tupleType );
    107                 acceptAll( tupleType->get_types(), *this );
    108                 typeAfter( tupleType );
    109         }
    110 
    111         void RenameVars::visit( VarArgsType *varArgsType ) {
    112                 typeBefore( varArgsType );
    113                 typeAfter( varArgsType );
    114         }
    115 
    116         void RenameVars::visit( ZeroType *zeroType ) {
    117                 typeBefore( zeroType );
    118                 typeAfter( zeroType );
    119         }
    120 
    121         void RenameVars::visit( OneType *oneType ) {
    122                 typeBefore( oneType );
    123                 typeAfter( oneType );
    124         }
    125 
    126         void RenameVars::typeBefore( Type *type ) {
    127                 if ( ! type->get_forall().empty() ) {
    128                         // copies current name mapping into new mapping
    129                         mapStack.push_front( mapStack.front() );
    130                         // renames all "forall" type names to `_${level}_${name}'
    131                         for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
    132                                 std::ostringstream output;
    133                                 output << "_" << resetCount << "_" << level << "_" << (*i)->get_name();
    134                                 std::string newname( output.str() );
    135                                 mapStack.front()[ (*i)->get_name() ] = newname;
    136                                 (*i)->set_name( newname );
    137                                 // ditto for assertion names, the next level in
    138                                 level++;
    139                                 acceptAll( (*i)->get_assertions(), *this );
    140                         } // for
    141                 } // if
    142         }
    143 
    144         void RenameVars::typeAfter( Type *type ) {
    145                 // clears name mapping added by typeBefore()
    146                 if ( ! type->get_forall().empty() ) {
    147                         mapStack.pop_front();
    148                 } // if
    149         }
    150 
     91                void RenameVars::postvisit( Type * type ) {
     92                        // clears name mapping added by typeBefore()
     93                        if ( ! type->forall.empty() ) {
     94                                mapStack.pop_front();
     95                        } // if
     96                }
     97        } // namespace
    15198} // namespace ResolvExpr
    15299
  • src/ResolvExpr/RenameVars.h

    rf07c1e6 rad51cc2  
    2424
    2525namespace ResolvExpr {
     26        /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID
     27        void renameTyVars( Type * );
    2628
    27         /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID
    28         class RenameVars : public Visitor {
    29           public:
    30                 RenameVars();
    31                 void reset();
    32           private:
    33                 virtual void visit( VoidType *basicType );
    34                 virtual void visit( BasicType *basicType );
    35                 virtual void visit( PointerType *pointerType );
    36                 virtual void visit( ArrayType *arrayType );
    37                 virtual void visit( FunctionType *functionType );
    38                 virtual void visit( StructInstType *aggregateUseType );
    39                 virtual void visit( UnionInstType *aggregateUseType );
    40                 virtual void visit( EnumInstType *aggregateUseType );
    41                 virtual void visit( TraitInstType *aggregateUseType );
    42                 virtual void visit( TypeInstType *aggregateUseType );
    43                 virtual void visit( TupleType *tupleType );
    44                 virtual void visit( VarArgsType *varArgsType );
    45                 virtual void visit( ZeroType *zeroType );
    46                 virtual void visit( OneType *oneType );
    47 
    48                 void typeBefore( Type *type );
    49                 void typeAfter( Type *type );
    50                 int level, resetCount;
    51                 std::list< std::map< std::string, std::string > > mapStack;
    52         };
    53 
    54         extern RenameVars global_renamer;
     29        /// resets internal state of renamer to avoid overflow
     30        void resetTyVarRenaming();
    5531} // namespace ResolvExpr
    5632
  • src/ResolvExpr/Resolver.cc

    rf07c1e6 rad51cc2  
    132132
    133133        void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) {
    134                 global_renamer.reset();
     134                resetTyVarRenaming();
    135135                TypeEnvironment env;
    136136                Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
Note: See TracChangeset for help on using the changeset viewer.