Ignore:
Timestamp:
Nov 8, 2017, 5:43:33 PM (8 years ago)
Author:
Aaron Moss <a3moss@…>
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:
954908d
Parents:
78315272 (diff), e35f30a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ConversionCost.cc

    r78315272 r3f7e12cb  
    1010// Created On       : Sun May 17 07:06:19 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar  2 17:35:46 2016
    13 // Update Count     : 6
     12// Last Modified On : Mon Sep 25 15:43:34 2017
     13// Update Count     : 10
    1414//
    1515
     
    2828
    2929namespace ResolvExpr {
    30         const Cost Cost::zero = Cost( 0, 0, 0, 0 );
    31         const Cost Cost::infinity = Cost( -1, -1, -1, -1 );
    32         const Cost Cost::unsafe = Cost( 1, 0, 0, 0 );
    33         const Cost Cost::poly = Cost( 0, 1, 0, 0 );
    34         const Cost Cost::safe = Cost( 0, 0, 1, 0 );
    35         const Cost Cost::reference = Cost( 0, 0, 0, 1 );
     30        const Cost Cost::zero =      Cost(  0,  0,  0, 0 );
     31        const Cost Cost::infinity =  Cost( -1, -1, -1, -1 );
     32        const Cost Cost::unsafe =    Cost(  1,  0,  0, 0 );
     33        const Cost Cost::poly =      Cost(  0,  1,  0, 0 );
     34        const Cost Cost::safe =      Cost(  0,  0,  1, 0 );
     35        const Cost Cost::reference = Cost(  0,  0,  0, 1 );
    3636
    3737#if 0
     
    113113                                        int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer );
    114114                                        PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; )
    115                                         if ( assignResult < 0 ) {
     115                                        if ( assignResult > 0 ) {
    116116                                                return Cost::safe;
    117                                         } else if ( assignResult > 0 ) {
     117                                        } else if ( assignResult < 0 ) {
    118118                                                return Cost::unsafe;
    119119                                        } // if
     
    219219*/
    220220
    221         static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] =
    222         {
    223         /* Src \ Dest:  Bool    Char    SChar   UChar   Short   UShort  Int     UInt    Long    ULong   LLong   ULLong  Float   Double  LDbl    FCplex  DCplex  LDCplex FImag   DImag   LDImag */
    224                 /* Bool */      { 0,    1,              1,              2,              3,              4,              5,              6,              6,              7,              8,              9,              10,             11,             12,             11,             12,             13,             -1,             -1,             -1 },
    225                 /* Char */      { -1,   0,              -1,             1,              2,              3,              4,              5,              5,              6,              7,              8,              9,              10,             11,             10,             11,             12,             -1,             -1,             -1 },
    226                 /* SChar */ { -1,       -1,             0,              1,              2,              3,              4,              5,              5,              6,              7,              8,              9,              10,             11,             10,             11,             12,             -1,             -1,             -1 },
    227                 /* UChar */ { -1,       -1,             -1,             0,              1,              2,              3,              4,              4,              5,              6,              7,              8,              9,              10,             9,              10,             11,             -1,             -1,             -1 },
    228                 /* Short */ { -1,       -1,             -1,             -1,             0,              1,              2,              3,              3,              4,              5,              6,              7,              8,              9,              8,              9,              10,             -1,             -1,             -1 },
    229                 /* UShort */{ -1,       -1,             -1,             -1,             -1,             0,              1,              2,              2,              3,              4,              5,              6,              7,              8,              7,              8,              9,              -1,             -1,             -1 },
    230                 /* Int */       { -1,   -1,             -1,             -1,             -1,             -1,             0,              1,              1,              2,              3,              4,              5,              6,              7,              6,              7,              8,              -1,             -1,             -1 },
    231                 /* UInt */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             1,              2,              3,              4,              5,              6,              5,              6,              7,              -1,             -1,             -1 },
    232                 /* Long */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              5,              6,              5,              6,              7,              -1,             -1,             -1 },
    233                 /* ULong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              5,              4,              5,              6,              -1,             -1,             -1 },
    234                 /* LLong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              4,              3,              4,              5,              -1,             -1,             -1 },
    235                 /* ULLong */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              2,              3,              4,              -1,             -1,             -1 },
    236                 /* Float */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              1,              2,              3,              -1,             -1,             -1 },
    237                 /* Double */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             1,              2,              -1,             -1,             -1 },
    238                 /* LDbl */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             1,              -1,             -1,             -1 },
    239                 /* FCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              -1,             -1,             -1 },
    240                 /* DCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             -1,             -1 },
    241                 /* LDCplex */{ -1,      -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1 },
    242                 /* FImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              0,              1,              2 },
    243                 /* DImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              -1,             0,              1 },
    244                 /* LDImag */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             0 }
     221        static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = {
     222        /* Src \ Dest:  Bool    Char    SChar   UChar   Short   UShort  Int     UInt    Long    ULong   LLong   ULLong  Float   Double  LDbl    FCplex  DCplex  LDCplex FImag   DImag   LDImag  I128,   U128 */
     223                /* Bool */      { 0,    1,              1,              2,              3,              4,              5,              6,              6,              7,              8,              9,              12,             13,             14,             12,             13,             14,             -1,             -1,             -1,             10,             11,     },
     224                /* Char */      { -1,   0,              -1,             1,              2,              3,              4,              5,              5,              6,              7,              8,              11,             12,             13,             11,             12,             13,             -1,             -1,             -1,             9,              10,     },
     225                /* SChar */ { -1,       -1,             0,              1,              2,              3,              4,              5,              5,              6,              7,              8,              11,             12,             13,             11,             12,             13,             -1,             -1,             -1,             9,              10,     },
     226                /* UChar */ { -1,       -1,             -1,             0,              1,              2,              3,              4,              4,              5,              6,              7,              10,             11,             12,             10,             11,             12,             -1,             -1,             -1,             8,              9,      },
     227                /* Short */ { -1,       -1,             -1,             -1,             0,              1,              2,              3,              3,              4,              5,              6,              9,              10,             11,             9,              10,             11,             -1,             -1,             -1,             7,              8,      },
     228                /* UShort */{ -1,       -1,             -1,             -1,             -1,             0,              1,              2,              2,              3,              4,              5,              8,              9,              10,             8,              9,              10,             -1,             -1,             -1,             6,              7,      },
     229                /* Int */       { -1,   -1,             -1,             -1,             -1,             -1,             0,              1,              1,              2,              3,              4,              7,              8,              9,              7,              8,              9,              -1,             -1,             -1,             5,              6,      },
     230                /* UInt */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             1,              2,              3,              6,              7,              8,              6,              7,              8,              -1,             -1,             -1,             4,              5,      },
     231                /* Long */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              6,              7,              8,              6,              7,              8,              -1,             -1,             -1,             4,              5,      },
     232                /* ULong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              5,              6,              7,              5,              6,              7,              -1,             -1,             -1,             3,              4,      },
     233                /* LLong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              4,              5,              6,              4,              5,              6,              -1,             -1,             -1,             2,              3,      },
     234                /* ULLong */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              3,              4,              5,              3,              4,              5,              -1,             -1,             -1,             1,              2,      },
     235
     236                /* Float */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              1,              2,              3,              -1,             -1,             -1,             -1,             -1,     },
     237                /* Double */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             1,              2,              -1,             -1,             -1,             -1,             -1,     },
     238                /* LDbl */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             1,              -1,             -1,             -1,             -1,             -1,     },
     239                /* FCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              -1,             -1,             -1,             -1,             -1,     },
     240                /* DCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             -1,             -1,             -1,             -1,     },
     241                /* LDCplex */{ -1,      -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,     },
     242                /* FImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              0,              1,              2,              -1,             -1,     },
     243                /* DImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              -1,             0,              1,              -1,             -1,     },
     244                /* LDImag */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             0,              -1,             -1,     },
     245
     246                /* I128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             2,              3,              4,              3,              4,              5,              -1,             -1,             -1,             0,              1,      },
     247                /* U128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              2,              3,              4,              -1,             -1,             -1,             -1,             0,      },
    245248        };
    246249
     
    266269        }
    267270
    268         void ConversionCost::visit(PointerType *pointerType) {
     271        void ConversionCost::visit( PointerType * pointerType ) {
    269272                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
    270273                        PRINT( std::cerr << pointerType << " ===> " << destAsPtr; )
     
    281284                                }
    282285                        } else {  // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?
    283                                 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env );
     286                                int assignResult = ptrsAssignable( pointerType->base, destAsPtr->base, env );
    284287                                PRINT( std::cerr << " :: " << assignResult << std::endl; )
    285                                 if ( assignResult < 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) {
     288                                if ( assignResult > 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) {
    286289                                        cost = Cost::safe;
    287                                 } else if ( assignResult > 0 ) {
     290                                } else if ( assignResult < 0 ) {
    288291                                        cost = Cost::unsafe;
    289292                                } // if
    290293                                // assignResult == 0 means Cost::Infinity
    291294                        } // if
    292                 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) {
     295                } else if ( dynamic_cast< ZeroType * >( dest ) ) {
    293296                        cost = Cost::unsafe;
    294297                } // if
    295298        }
    296299
    297         void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {}
    298 
    299         void ConversionCost::visit(ReferenceType *refType) {
     300        void ConversionCost::visit( ArrayType * ) {}
     301
     302        void ConversionCost::visit( ReferenceType * refType ) {
    300303                // Note: dest can never be a reference, since it would have been caught in an earlier check
    301304                assert( ! dynamic_cast< ReferenceType * >( dest ) );
     
    303306                // recursively compute conversion cost from T1 to T2.
    304307                // cv can be safely dropped because of 'implicit dereference' behavior.
    305                 refType->get_base()->accept( *this );
    306                 if ( refType->get_base()->get_qualifiers() == dest->get_qualifiers() ) {
     308                refType->base->accept( *this );
     309                if ( refType->base->get_qualifiers() == dest->get_qualifiers() ) {
    307310                        cost.incReference();  // prefer exact qualifiers
    308                 } else if ( refType->get_base()->get_qualifiers() < dest->get_qualifiers() ) {
     311                } else if ( refType->base->get_qualifiers() < dest->get_qualifiers() ) {
    309312                        cost.incSafe(); // then gaining qualifiers
    310313                } else {
     
    314317        }
    315318
    316         void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {}
    317 
    318         void ConversionCost::visit(StructInstType *inst) {
     319        void ConversionCost::visit( FunctionType * ) {}
     320
     321        void ConversionCost::visit( StructInstType * inst ) {
    319322                if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
    320                         if ( inst->get_name() == destAsInst->get_name() ) {
    321                                 cost = Cost::zero;
    322                         } // if
    323                 } // if
    324         }
    325 
    326         void ConversionCost::visit(UnionInstType *inst) {
    327                 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
    328                         if ( inst->get_name() == destAsInst->get_name() ) {
    329                                 cost = Cost::zero;
    330                         } // if
    331                 } // if
    332         }
    333 
    334         void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) {
     323                        if ( inst->name == destAsInst->name ) {
     324                                cost = Cost::zero;
     325                        } // if
     326                } // if
     327        }
     328
     329        void ConversionCost::visit( UnionInstType * inst ) {
     330                if ( UnionInstType *destAsInst = dynamic_cast< UnionInstType* >( dest ) ) {
     331                        if ( inst->name == destAsInst->name ) {
     332                                cost = Cost::zero;
     333                        } // if
     334                } // if
     335        }
     336
     337        void ConversionCost::visit( EnumInstType * ) {
    335338                static Type::Qualifiers q;
    336339                static BasicType integer( q, BasicType::SignedInt );
     
    341344        }
    342345
    343         void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) {
    344         }
    345 
    346         void ConversionCost::visit(TypeInstType *inst) {
     346        void ConversionCost::visit( TraitInstType * ) {}
     347
     348        void ConversionCost::visit( TypeInstType *inst ) {
    347349                EqvClass eqvClass;
    348350                NamedTypeDecl *namedType;
     
    363365        }
    364366
    365         void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) {
     367        void ConversionCost::visit( TupleType * tupleType ) {
    366368                Cost c = Cost::zero;
    367                 if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) {
    368                         std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin();
    369                         std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin();
     369                if ( TupleType * destAsTuple = dynamic_cast< TupleType * >( dest ) ) {
     370                        std::list< Type * >::const_iterator srcIt = tupleType->get_types().begin();
     371                        std::list< Type * >::const_iterator destIt = destAsTuple->get_types().begin();
    370372                        while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) {
    371373                                Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env );
     
    383385        }
    384386
    385         void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) {
     387        void ConversionCost::visit( VarArgsType * ) {
    386388                if ( dynamic_cast< VarArgsType* >( dest ) ) {
    387389                        cost = Cost::zero;
     
    389391        }
    390392
    391         void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) {
    392                 if ( dynamic_cast< ZeroType* >( dest ) ) {
     393        void ConversionCost::visit( ZeroType * ) {
     394                if ( dynamic_cast< ZeroType * >( dest ) ) {
    393395                        cost = Cost::zero;
    394396                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
     
    406408        }
    407409
    408         void ConversionCost::visit( __attribute((unused)) OneType *oneType) {
    409                 if ( dynamic_cast< OneType* >( dest ) ) {
     410        void ConversionCost::visit( OneType * ) {
     411                if ( dynamic_cast< OneType * >( dest ) ) {
    410412                        cost = Cost::zero;
    411413                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
Note: See TracChangeset for help on using the changeset viewer.