Changeset 7ebaa56 for src/ResolvExpr


Ignore:
Timestamp:
Jul 17, 2017, 3:05:57 PM (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:
e6cee92
Parents:
89be1c68
git-author:
Rob Schluntz <rschlunt@…> (07/17/17 14:58:13)
git-committer:
Rob Schluntz <rschlunt@…> (07/17/17 15:05:57)
Message:

Add reference cost field to cost tuple

Location:
src/ResolvExpr
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    r89be1c68 r7ebaa56  
    2121
    2222namespace ResolvExpr {
    23         const Cost Cost::zero = Cost( 0, 0, 0 );
    24         const Cost Cost::infinity = Cost( -1, -1, -1 );
    25         const Cost Cost::unsafe = Cost( 1, 0, 0 );
    26         const Cost Cost::poly = Cost( 0, 1, 0 );
    27         const Cost Cost::safe = Cost( 0, 0, 1 );
     23        const Cost Cost::zero = Cost( 0, 0, 0, 0 );
     24        const Cost Cost::infinity = Cost( -1, -1, -1, -1 );
     25        const Cost Cost::unsafe = Cost( 1, 0, 0, 0 );
     26        const Cost Cost::poly = Cost( 0, 1, 0, 0 );
     27        const Cost Cost::safe = Cost( 0, 0, 1, 0 );
     28        const Cost Cost::reference = Cost( 0, 0, 0, 1 );
    2829
    2930
     
    8081                        // TODO: document this
    8182                        Cost cost = convertToReferenceCost( safe_dynamic_cast< ReferenceType * >( src )->get_base(), dest, diff-1, indexer, env );
    82                         // TODO: increment reference cost
    83                         cost.incSafe();
     83                        cost.incReference();
    8484                        return cost;
    8585                } else if ( diff < -1 ) {
    8686                        // TODO: document this
    8787                        Cost cost = convertToReferenceCost( src, safe_dynamic_cast< ReferenceType * >( dest )->get_base(), diff+1, indexer, env );
    88                         // TODO: increment reference cost
    89                         cost.incSafe();
     88                        cost.incReference();
    9089                        return cost;
    9190                } else if ( diff == 0 ) {
     
    119118                                        // lvalue-to-reference conversion:  cv lvalue T => cv T &
    120119                                        if ( src->get_qualifiers() == destAsRef->get_base()->get_qualifiers() ) {
    121                                                 return Cost::safe; // cost needs to be non-zero to add cast
     120                                                return Cost::reference; // cost needs to be non-zero to add cast
    122121                                        } if ( src->get_qualifiers() < destAsRef->get_base()->get_qualifiers() ) {
    123                                                 return Cost::safe + Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same
     122                                                return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same
    124123                                        } else {
    125124                                                return Cost::unsafe;
     
    273272                // cv can be safely dropped because of 'implicit dereference' behavior.
    274273                refType->get_base()->accept( *this );
    275                 // TODO: increment reference cost
    276                 cost.incSafe();
     274                cost.incReference();
    277275        }
    278276
  • src/ResolvExpr/Cost.h

    r89be1c68 r7ebaa56  
    2222        class Cost {
    2323          private:
    24                 Cost( int unsafeCost, int polyCost, int safeCost );
     24                Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost );
    2525
    2626          public:
     
    2828                Cost & incPoly( int inc = 1 );
    2929                Cost & incSafe( int inc = 1 );
     30                Cost & incReference( int inc = 1 );
    3031
    3132                Cost operator+( const Cost &other ) const;
     
    3940                static const Cost zero;
    4041                static const Cost infinity;
     42
    4143                static const Cost unsafe;
    4244                static const Cost poly;
    4345                static const Cost safe;
     46                static const Cost reference;
    4447          private:
    4548                int compare( const Cost &other ) const;
     
    4851                int polyCost;
    4952                int safeCost;
     53                int referenceCost;
    5054        };
    5155
    52         inline Cost::Cost( int unsafeCost, int polyCost, int safeCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ) {}
     56        inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), referenceCost( referenceCost ) {}
    5357
    5458        inline Cost & Cost::incUnsafe( int inc ) {
     
    6771        }
    6872
     73        inline Cost & Cost::incReference( int inc ) {
     74                referenceCost += inc;
     75                return *this;
     76        }
     77
    6978        inline Cost Cost::operator+( const Cost &other ) const {
    70                 return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost );
     79                return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, referenceCost + other.referenceCost );
    7180        }
    7281
    7382        inline Cost Cost::operator-( const Cost &other ) const {
    74                 return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost );
     83                return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, referenceCost - other.referenceCost );
    7584        }
    7685
     
    7988                polyCost += other.polyCost;
    8089                safeCost += other.safeCost;
     90                referenceCost += other.referenceCost;
    8191                return *this;
    8292        }
     
    98108                } else if ( safeCost < other.safeCost ) {
    99109                        return true;
     110                } else if ( referenceCost > other.referenceCost ) {
     111                        return false;
     112                } else if ( referenceCost < other.referenceCost ) {
     113                        return true;
    100114                } else {
    101115                        return false;
     
    106120                return unsafeCost == other.unsafeCost
    107121                        && polyCost == other.polyCost
    108                         && safeCost == other.safeCost;
     122                        && safeCost == other.safeCost
     123                        && referenceCost == other.referenceCost;
    109124        }
    110125
     
    114129
    115130        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
    116                 os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << " )";
     131                os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << ", " << cost.referenceCost << " )";
    117132                return os;
    118133        }
Note: See TracChangeset for help on using the changeset viewer.