Changeset 721cd19f


Ignore:
Timestamp:
Jan 11, 2018, 10:50:03 AM (7 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:
bd0b6b62
Parents:
80e8582
Message:

Refactor ConversionCost? and CastCost? to remove explicit recursive accept calls

Location:
src/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CastCost.cc

    r80e8582 r721cd19f  
    3333        class CastCost : public ConversionCost {
    3434          public:
    35                 CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
     35                CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
    3636
    3737                virtual void visit( BasicType *basicType );
     
    7474                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
    7575                        PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
    76                         return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) {
     76                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
    7777                                return ptrsCastable( t1, t2, env, indexer );
    7878                        });
    7979                } else {
    80                         CastCost converter( dest, indexer, env );
     80                        CastCost converter( dest, indexer, env, castCost );
    8181                        src->accept( converter );
    8282                        if ( converter.get_cost() == Cost::infinity ) {
     
    8989        }
    9090
    91         CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
    92                 : ConversionCost( dest, indexer, env ) {
     91        CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
     92                : ConversionCost( dest, indexer, env, costFunc ) {
    9393        }
    9494
  • src/ResolvExpr/ConversionCost.cc

    r80e8582 r721cd19f  
    7777                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
    7878                        PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
    79                         return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer &){
     79                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer &, const TypeEnvironment & env ){
    8080                                return ptrsAssignable( t1, t2, env );
    8181                        });
    8282                } else {
    83                         ConversionCost converter( dest, indexer, env );
     83                        ConversionCost converter( dest, indexer, env, conversionCost );
    8484                        src->accept( converter );
    8585                        if ( converter.get_cost() == Cost::infinity ) {
     
    120120                                        }
    121121                                } else {  // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?
    122                                         int assignResult = func( srcAsRef->base, destAsRef->base, env, indexer );
     122                                        int assignResult = func( srcAsRef->base, destAsRef->base, indexer, env );
    123123                                        PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; )
    124124                                        if ( assignResult > 0 ) {
     
    130130                        } else {
    131131                                PRINT( std::cerr << "reference to rvalue conversion" << std::endl; )
    132                                 ConversionCost converter( dest, indexer, env );
     132                                ConversionCost converter( dest, indexer, env, conversionCost );
    133133                                src->accept( converter );
    134134                                return converter.get_cost();
     
    173173        }
    174174
    175         ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
    176                 : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ) {
     175        ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
     176                : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ), costFunc( costFunc ) {
    177177        }
    178178
     
    320320                // recursively compute conversion cost from T1 to T2.
    321321                // cv can be safely dropped because of 'implicit dereference' behavior.
    322                 refType->base->accept( *this );
     322                cost = costFunc( refType->base, dest, indexer, env );
    323323                if ( refType->base->get_qualifiers() == dest->get_qualifiers() ) {
    324324                        cost.incReference();  // prefer exact qualifiers
     
    352352                static Type::Qualifiers q;
    353353                static BasicType integer( q, BasicType::SignedInt );
    354                 integer.accept( *this );  // safe if dest >= int
     354                cost = costFunc( &integer, dest, indexer, env );  // safe if dest >= int
    355355                if ( cost < Cost::unsafe ) {
    356356                        cost.incSafe();
     
    364364                NamedTypeDecl *namedType;
    365365                if ( env.lookup( inst->name, eqvClass ) ) {
    366                         cost = conversionCost( eqvClass.type, dest, indexer, env );
     366                        cost = costFunc( eqvClass.type, dest, indexer, env );
    367367                } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) {
    368368                        if ( inst->name == destAsInst->name ) {
     
    374374                        assert( type );
    375375                        if ( type->base ) {
    376                                 cost = conversionCost( type->base, dest, indexer, env ) + Cost::safe;
     376                                cost = costFunc( type->base, dest, indexer, env ) + Cost::safe;
    377377                        } // if
    378378                } // if
     
    385385                        std::list< Type * >::const_iterator destIt = destAsTuple->types.begin();
    386386                        while ( srcIt != tupleType->types.end() && destIt != destAsTuple->types.end() ) {
    387                                 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env );
     387                                Cost newCost = costFunc( *srcIt++, *destIt++, indexer, env );
    388388                                if ( newCost == Cost::infinity ) {
    389389                                        return;
  • src/ResolvExpr/ConversionCost.h

    r80e8582 r721cd19f  
    2929        class TypeEnvironment;
    3030
     31        typedef std::function<Cost(Type *, Type *, const SymTab::Indexer &, const TypeEnvironment &)> CostFunction;
    3132        class ConversionCost : public Visitor {
    3233          public:
    33                 ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
     34                ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction );
    3435
    3536                Cost get_cost() const { return cost; }
     
    5556                Cost cost;
    5657                const TypeEnvironment &env;
     58                CostFunction costFunc;
    5759        };
    5860
    59         typedef std::function<int(Type *, Type *, const TypeEnvironment &, const SymTab::Indexer &)> PtrsFunction;
     61        typedef std::function<int(Type *, Type *, const SymTab::Indexer &, const TypeEnvironment &)> PtrsFunction;
    6062        Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func );
    6163} // namespace ResolvExpr
Note: See TracChangeset for help on using the changeset viewer.