Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    r3e5dd913 r849720f  
    1010// Created On       : Sun May 17 07:06:19 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jul 29 16:11:00 2020
    13 // Update Count     : 28
     12// Last Modified On : Mon Aug 12 10:21:00 2019
     13// Update Count     : 27
    1414//
    1515
     
    392392        void ConversionCost::postvisit( const FunctionType * ) {}
    393393
     394        void ConversionCost::postvisit( const StructInstType * inst ) {
     395                if ( const StructInstType * destAsInst = dynamic_cast< const StructInstType * >( dest ) ) {
     396                        if ( inst->name == destAsInst->name ) {
     397                                cost = Cost::zero;
     398                        } // if
     399                } // if
     400        }
     401
     402        void ConversionCost::postvisit( const UnionInstType * inst ) {
     403                if ( const UnionInstType * destAsInst = dynamic_cast< const UnionInstType * >( dest ) ) {
     404                        if ( inst->name == destAsInst->name ) {
     405                                cost = Cost::zero;
     406                        } // if
     407                } // if
     408        }
     409
    394410        void ConversionCost::postvisit( const EnumInstType * ) {
    395411                static Type::Qualifiers q;
     
    481497        }
    482498
    483 namespace {
    484         # warning For overload resolution between the two versions.
    485         int localPtrsAssignable(const ast::Type * t1, const ast::Type * t2,
    486                         const ast::SymbolTable &, const ast::TypeEnvironment & env ) {
    487                 return ptrsAssignable( t1, t2, env );
    488         }
    489         Cost localConversionCost(
    490                 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
    491                 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
    492         ) { return conversionCost( src, dst, srcIsLvalue, symtab, env ); }
    493 }
     499static int localPtrsAssignable(const ast::Type * t1, const ast::Type * t2,
     500                const ast::SymbolTable &, const ast::TypeEnvironment & env ) {
     501        return ptrsAssignable( t1, t2, env );
     502}
     503
     504// TODO: This is used for overload resolution. It might be able to be dropped once the old system
     505// is removed.
     506static Cost localConversionCost(
     507        const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
     508        const ast::TypeEnvironment & env
     509) { return conversionCost( src, dst, symtab, env ); }
    494510
    495511Cost conversionCost(
    496         const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
    497         const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
     512        const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
     513        const ast::TypeEnvironment & env
    498514) {
    499515        if ( const ast::TypeInstType * inst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
    500                 if ( const ast::EqvClass * eqv = env.lookup( *inst ) ) {
     516                if ( const ast::EqvClass * eqv = env.lookup( inst->name ) ) {
    501517                        if ( eqv->bound ) {
    502                                 return conversionCost(src, eqv->bound, srcIsLvalue, symtab, env );
     518                                return conversionCost(src, eqv->bound, symtab, env );
    503519                        } else {
    504520                                return Cost::infinity;
     
    508524                        assertf( type, "Unexpected typedef." );
    509525                        if ( type->base ) {
    510                                 return conversionCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
     526                                return conversionCost( src, type->base, symtab, env ) + Cost::safe;
    511527                        }
    512528                }
     
    518534        } else if ( const ast::ReferenceType * refType =
    519535                         dynamic_cast< const ast::ReferenceType * >( dst ) ) {
    520                 return convertToReferenceCost( src, refType, srcIsLvalue, symtab, env, localPtrsAssignable );
     536                return convertToReferenceCost( src, refType, symtab, env, localPtrsAssignable );
    521537        } else {
    522                 return ast::Pass<ConversionCost_new>::read( src, dst, srcIsLvalue, symtab, env, localConversionCost );
    523         }
    524 }
    525 
    526 static Cost convertToReferenceCost( const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
     538                ast::Pass<ConversionCost_new> converter( dst, symtab, env, localConversionCost );
     539                src->accept( converter );
     540                return converter.pass.cost;
     541        }
     542}
     543
     544static Cost convertToReferenceCost( const ast::Type * src, const ast::Type * dst,
    527545                int diff, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env,
    528                 PtrsCalculation func ) {
     546                NumCostCalculation func ) {
    529547        if ( 0 < diff ) {
    530548                Cost cost = convertToReferenceCost(
    531                         strict_dynamic_cast< const ast::ReferenceType * >( src )->base, dst,
    532                         srcIsLvalue, (diff - 1), symtab, env, func );
     549                        strict_dynamic_cast< const ast::ReferenceType * >( src )->base,
     550                        dst, (diff - 1), symtab, env, func );
    533551                cost.incReference();
    534552                return cost;
     
    536554                Cost cost = convertToReferenceCost(
    537555                        src, strict_dynamic_cast< const ast::ReferenceType * >( dst )->base,
    538                         srcIsLvalue, (diff + 1), symtab, env, func );
     556                        (diff + 1), symtab, env, func );
    539557                cost.incReference();
    540558                return cost;
     
    561579                        }
    562580                } else {
    563                         return ast::Pass<ConversionCost_new>::read( src, dst, srcIsLvalue, symtab, env, localConversionCost );
     581                        ast::Pass<ConversionCost_new> converter( dst, symtab, env, localConversionCost );
     582                        src->accept( converter );
     583                        return converter.pass.cost;
    564584                }
    565585        } else {
     
    568588                assert( dstAsRef );
    569589                if ( typesCompatibleIgnoreQualifiers( src, dstAsRef->base, symtab, env ) ) {
    570                         if ( srcIsLvalue ) {
     590                        if ( src->is_lvalue() ) {
    571591                                if ( src->qualifiers == dstAsRef->base->qualifiers ) {
    572592                                        return Cost::reference;
     
    587607
    588608Cost convertToReferenceCost( const ast::Type * src, const ast::ReferenceType * dst,
    589                 bool srcIsLvalue, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env,
    590                 PtrsCalculation func ) {
     609            const ast::SymbolTable & symtab, const ast::TypeEnvironment & env,
     610                NumCostCalculation func ) {
    591611        int sdepth = src->referenceDepth(), ddepth = dst->referenceDepth();
    592         return convertToReferenceCost( src, dst, srcIsLvalue, sdepth - ddepth, symtab, env, func );
     612        return convertToReferenceCost( src, dst, sdepth - ddepth, symtab, env, func );
    593613}
    594614
     
    647667        assert( nullptr == dynamic_cast< const ast::ReferenceType * >( dst ) );
    648668
    649         cost = costCalc( refType->base, dst, srcIsLvalue, symtab, env );
     669        cost = costCalc( refType->base, dst, symtab, env );
    650670        if ( refType->base->qualifiers == dst->qualifiers ) {
    651671                cost.incReference();
     
    661681}
    662682
     683void ConversionCost_new::postvisit( const ast::StructInstType * structInstType ) {
     684        if ( const ast::StructInstType * dstAsInst =
     685                        dynamic_cast< const ast::StructInstType * >( dst ) ) {
     686                if ( structInstType->name == dstAsInst->name ) {
     687                        cost = Cost::zero;
     688                }
     689        }
     690}
     691
     692void ConversionCost_new::postvisit( const ast::UnionInstType * unionInstType ) {
     693        if ( const ast::UnionInstType * dstAsInst =
     694                        dynamic_cast< const ast::UnionInstType * >( dst ) ) {
     695                if ( unionInstType->name == dstAsInst->name ) {
     696                        cost = Cost::zero;
     697                }
     698        }
     699}
     700
    663701void ConversionCost_new::postvisit( const ast::EnumInstType * enumInstType ) {
    664702        (void)enumInstType;
    665         static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) };
    666         cost = costCalc( integer, dst, srcIsLvalue, symtab, env );
     703        static const ast::BasicType integer( ast::BasicType::SignedInt );
     704        cost = costCalc( &integer, dst, symtab, env );
    667705        if ( cost < Cost::unsafe ) {
    668706                cost.incSafe();
     
    675713
    676714void ConversionCost_new::postvisit( const ast::TypeInstType * typeInstType ) {
    677         if ( const ast::EqvClass * eqv = env.lookup( *typeInstType ) ) {
    678                 cost = costCalc( eqv->bound, dst, srcIsLvalue, symtab, env );
     715        if ( const ast::EqvClass * eqv = env.lookup( typeInstType->name ) ) {
     716                cost = costCalc( eqv->bound, dst, symtab, env );
    679717        } else if ( const ast::TypeInstType * dstAsInst =
    680718                        dynamic_cast< const ast::TypeInstType * >( dst ) ) {
    681                 if ( *typeInstType == *dstAsInst ) {
     719                if ( typeInstType->name == dstAsInst->name ) {
    682720                        cost = Cost::zero;
    683721                }
     
    686724                assertf( type, "Unexpected typedef.");
    687725                if ( type->base ) {
    688                         cost = costCalc( type->base, dst, srcIsLvalue, symtab, env ) + Cost::safe;
     726                        cost = costCalc( type->base, dst, symtab, env ) + Cost::safe;
    689727                }
    690728        }
     
    699737                auto dstEnd = dstAsTuple->types.end();
    700738                while ( srcIt != srcEnd && dstIt != dstEnd ) {
    701                         Cost newCost = costCalc( * srcIt++, * dstIt++, srcIsLvalue, symtab, env );
     739                        Cost newCost = costCalc( * srcIt++, * dstIt++, symtab, env );
    702740                        if ( newCost == Cost::infinity ) {
    703741                                return;
     
    734772                        cost.incSign( signMatrix[ ast::BasicType::SignedInt ][ dstAsBasic->kind ] );
    735773                }
    736         } else if ( dynamic_cast< const ast::PointerType * >( dst ) ) {
    737                 cost = Cost::zero;
    738                 // +1 for zero_t ->, +1 for disambiguation
    739                 cost.incSafe( maxIntCost + 2 );
    740774        }
    741775}
     
    755789                        cost.incSign( signMatrix[ ast::BasicType::SignedInt ][ dstAsBasic->kind ] );
    756790                }
    757         }
    758 }
    759 // size_t ConversionCost_new::traceId = Stats::Heap::new_stacktrace_id("ConversionCost");
     791        } else if ( dynamic_cast< const ast::PointerType * >( dst ) ) {
     792                cost = Cost::zero;
     793                cost.incSafe( maxIntCost + 2 );
     794        }
     795}
     796
    760797
    761798} // namespace ResolvExpr
Note: See TracChangeset for help on using the changeset viewer.