Changeset 3f7e12cb for src/ResolvExpr/ConversionCost.cc
- Timestamp:
- Nov 8, 2017, 5:43:33 PM (8 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ConversionCost.cc
r78315272 r3f7e12cb 10 10 // Created On : Sun May 17 07:06:19 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Mar 2 17:35:46 201613 // Update Count : 612 // Last Modified On : Mon Sep 25 15:43:34 2017 13 // Update Count : 10 14 14 // 15 15 … … 28 28 29 29 namespace 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 ); 36 36 37 37 #if 0 … … 113 113 int assignResult = func( srcAsRef->get_base(), destAsRef->get_base(), env, indexer ); 114 114 PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; ) 115 if ( assignResult <0 ) {115 if ( assignResult > 0 ) { 116 116 return Cost::safe; 117 } else if ( assignResult >0 ) {117 } else if ( assignResult < 0 ) { 118 118 return Cost::unsafe; 119 119 } // if … … 219 219 */ 220 220 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, }, 245 248 }; 246 249 … … 266 269 } 267 270 268 void ConversionCost::visit( PointerType *pointerType) {271 void ConversionCost::visit( PointerType * pointerType ) { 269 272 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { 270 273 PRINT( std::cerr << pointerType << " ===> " << destAsPtr; ) … … 281 284 } 282 285 } 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 ); 284 287 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() ) { 286 289 cost = Cost::safe; 287 } else if ( assignResult >0 ) {290 } else if ( assignResult < 0 ) { 288 291 cost = Cost::unsafe; 289 292 } // if 290 293 // assignResult == 0 means Cost::Infinity 291 294 } // if 292 } else if ( dynamic_cast< ZeroType * >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr) {295 } else if ( dynamic_cast< ZeroType * >( dest ) ) { 293 296 cost = Cost::unsafe; 294 297 } // if 295 298 } 296 299 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 ) { 300 303 // Note: dest can never be a reference, since it would have been caught in an earlier check 301 304 assert( ! dynamic_cast< ReferenceType * >( dest ) ); … … 303 306 // recursively compute conversion cost from T1 to T2. 304 307 // 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() ) { 307 310 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() ) { 309 312 cost.incSafe(); // then gaining qualifiers 310 313 } else { … … 314 317 } 315 318 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 ) { 319 322 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 * ) { 335 338 static Type::Qualifiers q; 336 339 static BasicType integer( q, BasicType::SignedInt ); … … 341 344 } 342 345 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 ) { 347 349 EqvClass eqvClass; 348 350 NamedTypeDecl *namedType; … … 363 365 } 364 366 365 void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) {367 void ConversionCost::visit( TupleType * tupleType ) { 366 368 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(); 370 372 while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { 371 373 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); … … 383 385 } 384 386 385 void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) {387 void ConversionCost::visit( VarArgsType * ) { 386 388 if ( dynamic_cast< VarArgsType* >( dest ) ) { 387 389 cost = Cost::zero; … … 389 391 } 390 392 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 ) ) { 393 395 cost = Cost::zero; 394 396 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { … … 406 408 } 407 409 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 ) ) { 410 412 cost = Cost::zero; 411 413 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
Note:
See TracChangeset
for help on using the changeset viewer.