- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ConversionCost.cc
rb0837e4 re3e16bc 10 10 // Created On : Sun May 17 07:06:19 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Sep 25 15:43:34 201713 // Update Count : 1012 // Last Modified On : Wed Mar 2 17:35:46 2016 13 // Update Count : 6 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 = 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 … … 92 92 93 93 Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) { 94 PRINT( std::cerr << "convert to reference cost... diff " << diff<< std::endl; )94 PRINT( std::cerr << "convert to reference cost..." << std::endl; ) 95 95 if ( diff > 0 ) { 96 96 // TODO: document this … … 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 … … 128 128 ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest ); 129 129 assert( diff == -1 && destAsRef ); 130 PRINT( std::cerr << "dest is: " << dest << " / src is: " << src << std::endl; )131 130 if ( typesCompatibleIgnoreQualifiers( src, destAsRef->get_base(), indexer, env ) ) { 132 131 PRINT( std::cerr << "converting compatible base type" << std::endl; ) … … 219 218 */ 220 219 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, }, 220 static const int costMatrix[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = 221 { 222 /* Src \ Dest: Bool Char SChar UChar Short UShort Int UInt Long ULong LLong ULLong Float Double LDbl FCplex DCplex LDCplex FImag DImag LDImag */ 223 /* Bool */ { 0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 11, 12, 13, -1, -1, -1 }, 224 /* Char */ { -1, 0, -1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 225 /* SChar */ { -1, -1, 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12, -1, -1, -1 }, 226 /* UChar */ { -1, -1, -1, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 9, 10, 11, -1, -1, -1 }, 227 /* Short */ { -1, -1, -1, -1, 0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 8, 9, 10, -1, -1, -1 }, 228 /* UShort */{ -1, -1, -1, -1, -1, 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 7, 8, 9, -1, -1, -1 }, 229 /* Int */ { -1, -1, -1, -1, -1, -1, 0, 1, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, -1, -1, -1 }, 230 /* UInt */ { -1, -1, -1, -1, -1, -1, -1, 0, -1, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 231 /* Long */ { -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 5, 6, 7, -1, -1, -1 }, 232 /* ULong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 4, 5, 6, -1, -1, -1 }, 233 /* LLong */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 3, 4, 5, -1, -1, -1 }, 234 /* ULLong */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 2, 3, 4, -1, -1, -1 }, 235 /* Float */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 1, 2, 3, -1, -1, -1 }, 236 /* Double */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, 1, 2, -1, -1, -1 }, 237 /* LDbl */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, 1, -1, -1, -1 }, 238 /* FCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, -1, -1 }, 239 /* DCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, -1 }, 240 /* LDCplex */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 }, 241 /* FImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 0, 1, 2 }, 242 /* DImag */ { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, -1, 0, 1 }, 243 /* LDImag */{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 0 } 248 244 }; 249 245 … … 269 265 } 270 266 271 void ConversionCost::visit( PointerType * pointerType) {267 void ConversionCost::visit(PointerType *pointerType) { 272 268 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) { 273 269 PRINT( std::cerr << pointerType << " ===> " << destAsPtr; ) … … 284 280 } 285 281 } else { // xxx - this discards qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right? 286 int assignResult = ptrsAssignable( pointerType-> base, destAsPtr->base, env );282 int assignResult = ptrsAssignable( pointerType->get_base(), destAsPtr->get_base(), env ); 287 283 PRINT( std::cerr << " :: " << assignResult << std::endl; ) 288 if ( assignResult >0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) {284 if ( assignResult < 0 && pointerType->get_base()->get_qualifiers() <= destAsPtr->get_qualifiers() ) { 289 285 cost = Cost::safe; 290 } else if ( assignResult <0 ) {286 } else if ( assignResult > 0 ) { 291 287 cost = Cost::unsafe; 292 288 } // if 293 289 // assignResult == 0 means Cost::Infinity 294 290 } // if 295 } else if ( dynamic_cast< ZeroType * >( dest )) {291 } else if ( dynamic_cast< ZeroType* >( dest ) != nullptr || dynamic_cast< OneType* >( dest ) != nullptr ) { 296 292 cost = Cost::unsafe; 297 293 } // if 298 294 } 299 295 300 void ConversionCost::visit( ArrayType *) {}301 302 void ConversionCost::visit( ReferenceType * refType) {296 void ConversionCost::visit(__attribute((unused)) ArrayType *arrayType) {} 297 298 void ConversionCost::visit(ReferenceType *refType) { 303 299 // Note: dest can never be a reference, since it would have been caught in an earlier check 304 300 assert( ! dynamic_cast< ReferenceType * >( dest ) ); … … 306 302 // recursively compute conversion cost from T1 to T2. 307 303 // cv can be safely dropped because of 'implicit dereference' behavior. 308 refType-> base->accept( *this );309 if ( refType-> base->get_qualifiers() == dest->get_qualifiers() ) {304 refType->get_base()->accept( *this ); 305 if ( refType->get_base()->get_qualifiers() == dest->get_qualifiers() ) { 310 306 cost.incReference(); // prefer exact qualifiers 311 } else if ( refType-> base->get_qualifiers() < dest->get_qualifiers() ) {307 } else if ( refType->get_base()->get_qualifiers() < dest->get_qualifiers() ) { 312 308 cost.incSafe(); // then gaining qualifiers 313 309 } else { … … 317 313 } 318 314 319 void ConversionCost::visit( FunctionType *) {}320 321 void ConversionCost::visit( StructInstType * inst) {315 void ConversionCost::visit(__attribute((unused)) FunctionType *functionType) {} 316 317 void ConversionCost::visit(StructInstType *inst) { 322 318 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 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 *) {319 if ( inst->get_name() == destAsInst->get_name() ) { 320 cost = Cost::zero; 321 } // if 322 } // if 323 } 324 325 void ConversionCost::visit(UnionInstType *inst) { 326 if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) { 327 if ( inst->get_name() == destAsInst->get_name() ) { 328 cost = Cost::zero; 329 } // if 330 } // if 331 } 332 333 void ConversionCost::visit( __attribute((unused)) EnumInstType *inst ) { 338 334 static Type::Qualifiers q; 339 335 static BasicType integer( q, BasicType::SignedInt ); … … 344 340 } 345 341 346 void ConversionCost::visit( TraitInstType * ) {} 347 348 void ConversionCost::visit( TypeInstType *inst ) { 342 void ConversionCost::visit( __attribute((unused)) TraitInstType *inst) { 343 } 344 345 void ConversionCost::visit(TypeInstType *inst) { 349 346 EqvClass eqvClass; 350 347 NamedTypeDecl *namedType; … … 365 362 } 366 363 367 void ConversionCost::visit( TupleType * tupleType) {364 void ConversionCost::visit( __attribute((unused)) TupleType *tupleType) { 368 365 Cost c = Cost::zero; 369 if ( TupleType * destAsTuple = dynamic_cast< TupleType* >( dest ) ) {370 std::list< Type 371 std::list< Type 366 if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { 367 std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); 368 std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); 372 369 while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { 373 370 Cost newCost = conversionCost( *srcIt++, *destIt++, indexer, env ); … … 385 382 } 386 383 387 void ConversionCost::visit( VarArgsType *) {384 void ConversionCost::visit( __attribute((unused)) VarArgsType *varArgsType) { 388 385 if ( dynamic_cast< VarArgsType* >( dest ) ) { 389 386 cost = Cost::zero; … … 391 388 } 392 389 393 void ConversionCost::visit( ZeroType *) {394 if ( dynamic_cast< ZeroType 390 void ConversionCost::visit( __attribute((unused)) ZeroType *zeroType) { 391 if ( dynamic_cast< ZeroType* >( dest ) ) { 395 392 cost = Cost::zero; 396 393 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) { … … 408 405 } 409 406 410 void ConversionCost::visit( OneType *) {411 if ( dynamic_cast< OneType 407 void ConversionCost::visit( __attribute((unused)) OneType *oneType) { 408 if ( dynamic_cast< OneType* >( dest ) ) { 412 409 cost = Cost::zero; 413 410 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
Note:
See TracChangeset
for help on using the changeset viewer.