Changeset e5d9274 for src/ResolvExpr/ConversionCost.cc
- Timestamp:
- Jun 2, 2022, 3:11:21 PM (3 years ago)
- Branches:
- ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
- Children:
- ced5e2a
- Parents:
- 015925a (diff), fc134a48 (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
r015925a re5d9274 321 321 } 322 322 323 // refactor for code resue 324 void ConversionCost::conversionCostFromBasicToBasic(const BasicType * src, const BasicType * dest) { 325 int tableResult = costMatrix[ src->kind ][ dest->kind ]; 326 if ( tableResult == -1 ) { 327 cost = Cost::unsafe; 328 } else { 329 cost = Cost::zero; 330 cost.incSafe( tableResult ); 331 cost.incSign( signMatrix[ src->kind ][ dest->kind ] ); 332 } // if 333 } // ConversionCost::conversionCostFromBasicToBasic 334 323 335 void ConversionCost::postvisit(const BasicType * basicType) { 324 336 if ( const BasicType * destAsBasic = dynamic_cast< const BasicType * >( dest ) ) { 325 int tableResult = costMatrix[ basicType->kind ][ destAsBasic->kind ]; 326 if ( tableResult == -1 ) { 327 cost = Cost::unsafe; 328 } else { 329 cost = Cost::zero; 330 cost.incSafe( tableResult ); 331 cost.incSign( signMatrix[ basicType->kind ][ destAsBasic->kind ] ); 332 } // if 333 } else if ( dynamic_cast< const EnumInstType * >( dest ) ) { 334 // xxx - not positive this is correct, but appears to allow casting int => enum 335 // TODO 336 EnumDecl * decl = dynamic_cast< const EnumInstType * >( dest )->baseEnum; 337 if ( decl->base ) { 338 cost = Cost::infinity; 337 conversionCostFromBasicToBasic(basicType, destAsBasic); 338 } else if ( const EnumInstType * enumInst = dynamic_cast< const EnumInstType * >( dest ) ) { 339 const EnumDecl * base_enum = enumInst->baseEnum; 340 if ( const Type * base = base_enum->base ) { // if the base enum has a base (if it is typed) 341 if ( const BasicType * enumBaseAstBasic = dynamic_cast< const BasicType *> (base) ) { 342 conversionCostFromBasicToBasic(basicType, enumBaseAstBasic); 343 } else { 344 cost = Cost::infinity; 345 } // if 339 346 } else { 340 347 cost = Cost::unsafe; … … 398 405 void ConversionCost::postvisit( const FunctionType * ) {} 399 406 400 void ConversionCost::postvisit( const EnumInstType * ) { 401 static Type::Qualifiers q; 402 static BasicType integer( q, BasicType::SignedInt ); 403 cost = costFunc( &integer, dest, srcIsLvalue, indexer, env ); // safe if dest >= int 407 void ConversionCost::postvisit( const EnumInstType * enumInst) { 408 const EnumDecl * enumDecl = enumInst -> baseEnum; 409 if ( const Type * enumType = enumDecl -> base ) { // if it is a typed enum 410 cost = costFunc( enumType, dest, srcIsLvalue, indexer, env ); 411 } else { 412 static Type::Qualifiers q; 413 static BasicType integer( q, BasicType::SignedInt ); 414 cost = costFunc( &integer, dest, srcIsLvalue, indexer, env ); // safe if dest >= int 415 } // if 404 416 if ( cost < Cost::unsafe ) { 405 cost.incSafe();417 cost.incSafe(); 406 418 } // if 407 419 } … … 604 616 } 605 617 618 void ConversionCost_new::conversionCostFromBasicToBasic( const ast::BasicType * src, const ast::BasicType* dest ) { 619 int tableResult = costMatrix[ src->kind ][ dest->kind ]; 620 if ( tableResult == -1 ) { 621 cost = Cost::unsafe; 622 } else { 623 cost = Cost::zero; 624 cost.incSafe( tableResult ); 625 cost.incSign( signMatrix[ src->kind ][ dest->kind ] ); 626 } 627 } 628 606 629 void ConversionCost_new::postvisit( const ast::BasicType * basicType ) { 607 630 if ( const ast::BasicType * dstAsBasic = dynamic_cast< const ast::BasicType * >( dst ) ) { 608 int tableResult = costMatrix[ basicType->kind ][ dstAsBasic->kind ]; 609 if ( tableResult == -1 ) { 610 cost = Cost::unsafe; 611 } else { 612 cost = Cost::zero; 613 cost.incSafe( tableResult ); 614 cost.incSign( signMatrix[ basicType->kind ][ dstAsBasic->kind ] ); 615 } 616 } else if ( dynamic_cast< const ast::EnumInstType * >( dst ) ) { 617 // xxx - not positive this is correct, but appears to allow casting int => enum 618 const ast::EnumDecl * decl = (dynamic_cast< const ast::EnumInstType * >( dst ))->base.get(); 619 if ( decl->base ) { 620 cost = Cost::infinity; 621 } else { 622 cost = Cost::unsafe; 623 } // if 631 conversionCostFromBasicToBasic( basicType, dstAsBasic ); 632 } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 633 const ast::EnumDecl * enumDecl = enumInst->base.get(); 634 if ( const ast::Type * enumType = enumDecl->base.get() ) { 635 if ( const ast::BasicType * enumTypeAsBasic = dynamic_cast<const ast::BasicType *>(enumType) ) { 636 conversionCostFromBasicToBasic( basicType, enumTypeAsBasic ); 637 } else { 638 cost = Cost::infinity; 639 } 640 } else { 641 cost = Cost::unsafe; 642 } 624 643 } 625 644 } … … 673 692 674 693 void ConversionCost_new::postvisit( const ast::EnumInstType * enumInstType ) { 675 (void)enumInstType; 676 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) }; 677 cost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 694 const ast::EnumDecl * baseEnum = enumInstType->base; 695 if ( const ast::Type * baseType = baseEnum->base ) { 696 cost = costCalc( baseType, dst, srcIsLvalue, symtab, env ); 697 } else { 698 (void)enumInstType; 699 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicType::SignedInt ) }; 700 cost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 701 } 678 702 if ( cost < Cost::unsafe ) { 679 703 cost.incSafe();
Note:
See TracChangeset
for help on using the changeset viewer.