Changeset 0c6596f for src/ResolvExpr


Ignore:
Timestamp:
Aug 27, 2017, 11:25:00 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:
1cb758f2
Parents:
2ae171d8
git-author:
Rob Schluntz <rschlunt@…> (08/27/17 11:24:53)
git-committer:
Rob Schluntz <rschlunt@…> (08/27/17 11:25:00)
Message:

Handle difference between reference casting and implicit conversion costs [fixes #39]

Location:
src/ResolvExpr
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CastCost.cc

    r2ae171d8 r0c6596f  
    5858                        return Cost::safe;
    5959                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
    60                         return convertToReferenceCost( src, refType, indexer, env );
     60                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer & indexer) {
     61                                return ptrsCastable( t1, t2, env, indexer );
     62                        });
    6163                } else {
    6264                        CastCost converter( dest, indexer, env );
  • src/ResolvExpr/ConversionCost.cc

    r2ae171d8 r0c6596f  
    4040#define PRINT(x)
    4141#endif
    42 
    4342        Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
    4443                if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
     
    7877                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
    7978                        PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
    80                         return convertToReferenceCost( src, refType, indexer, env );
     79                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const TypeEnvironment & env, const SymTab::Indexer &){
     80                                return ptrsAssignable( t1, t2, env );
     81                        });
    8182                } else {
    8283                        ConversionCost converter( dest, indexer, env );
     
    9091        }
    9192
    92         Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
     93        Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) {
    9394                PRINT( std::cerr << "convert to reference cost..." << std::endl; )
    9495                if ( diff > 0 ) {
    9596                        // TODO: document this
    96                         Cost cost = convertToReferenceCost( safe_dynamic_cast< ReferenceType * >( src )->get_base(), dest, diff-1, indexer, env );
     97                        Cost cost = convertToReferenceCost( safe_dynamic_cast< ReferenceType * >( src )->get_base(), dest, diff-1, indexer, env, func );
    9798                        cost.incReference();
    9899                        return cost;
    99100                } else if ( diff < -1 ) {
    100101                        // TODO: document this
    101                         Cost cost = convertToReferenceCost( src, safe_dynamic_cast< ReferenceType * >( dest )->get_base(), diff+1, indexer, env );
     102                        Cost cost = convertToReferenceCost( src, safe_dynamic_cast< ReferenceType * >( dest )->get_base(), diff+1, indexer, env, func );
    102103                        cost.incReference();
    103104                        return cost;
     
    110111                                        return Cost::safe;
    111112                                } else {  // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?
    112                                         int assignResult = ptrsAssignable( srcAsRef->get_base(), destAsRef->get_base(), env );
     113                                        int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer );
    113114                                        PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; )
    114115                                        if ( assignResult < 0 ) {
     
    157158        }
    158159
    159         Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
     160        Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) {
    160161                int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth();
    161                 return convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env );
     162                return convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func );
    162163        }
    163164
  • src/ResolvExpr/ConversionCost.h

    r2ae171d8 r0c6596f  
    1616#pragma once
    1717
     18#include <functional>         // for function
     19
    1820#include "Cost.h"             // for Cost
    1921#include "SynTree/Visitor.h"  // for Visitor
     
    2123
    2224namespace SymTab {
    23 class Indexer;
     25        class Indexer;
    2426}  // namespace SymTab
    2527
    2628namespace ResolvExpr {
    27 class TypeEnvironment;
     29        class TypeEnvironment;
    2830
    2931        class ConversionCost : public Visitor {
     
    5557        };
    5658
    57         Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env );
     59        typedef std::function<int(Type *, Type *, const TypeEnvironment &, const SymTab::Indexer &)> PtrsFunction;
     60        Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func );
    5861} // namespace ResolvExpr
    5962
Note: See TracChangeset for help on using the changeset viewer.