Changeset acb33f15 for src/ResolvExpr
- Timestamp:
- May 13, 2024, 10:26:52 AM (12 months ago)
- Branches:
- master
- Children:
- 31f4837
- Parents:
- 41c8312
- Location:
- src/ResolvExpr
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/ResolvExpr/CandidateFinder.cpp ¶
r41c8312 racb33f15 906 906 } 907 907 CandidateRef & choice = winners.front(); 908 choice->cost .incSafe();908 choice->cost = Cost::unsafe; 909 909 candidates.emplace_back( std::move(choice) ); 910 910 } … … 955 955 956 956 CandidateFinder funcFinder( context, tenv ); 957 std::string funcName; 957 958 if (auto nameExpr = untypedExpr->func.as<ast::NameExpr>()) { 959 funcName = nameExpr->name; 958 960 auto kind = ast::SymbolTable::getSpecialFunctionKind(nameExpr->name); 959 961 if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) { … … 1019 1021 CandidateList found; 1020 1022 SemanticErrorException errors; 1023 1021 1024 for ( CandidateRef & func : funcFinder ) { 1022 1025 try { … … 1093 1096 Cost cvtCost = computeApplicationConversionCost( withFunc, symtab ); 1094 1097 1098 if (funcName == "?|?") { 1095 1099 PRINT( 1096 1100 auto appExpr = withFunc->expr.strict_as< ast::ApplicationExpr >(); … … 1108 1112 std::cerr << "cost of conversion is:" << cvtCost << std::endl; 1109 1113 ) 1110 1114 } 1111 1115 if ( cvtCost != Cost::infinity ) { 1112 1116 withFunc->cvtCost = cvtCost; … … 1774 1778 matches.clear(); 1775 1779 } 1776 // ambiguous case, still output candidates to print in error message 1777 if ( cand->cost == minExprCost && thisCost == minCastCost ) { 1778 auto commonAsEnumAttr = common.as<ast::EnumAttrType>(); 1779 if ( commonAsEnumAttr && commonAsEnumAttr->attr == ast::EnumAttribute::Value ) { 1780 auto callExpr = new ast::UntypedExpr( 1781 cand->expr->location, new ast::NameExpr( cand->expr->location, "valueE"), {cand->expr} ); 1782 CandidateFinder finder( context, env ); 1783 finder.find( callExpr ); 1784 CandidateList winners = findMinCost( finder.candidates ); 1785 if (winners.size() != 1) { 1786 SemanticError( callExpr, "Ambiguous expression in valueE..." ); 1787 } 1788 CandidateRef & choice = winners.front(); 1789 // assert( valueCall->result ); 1790 CandidateRef newCand = std::make_shared<Candidate>( 1791 new ast::InitExpr{ 1792 initExpr->location, 1793 // restructureCast( cand->expr, toType ), 1794 choice->expr, 1795 initAlt.designation }, 1796 std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost ); 1797 inferParameters( newCand, matches ); 1798 } else { 1799 CandidateRef newCand = std::make_shared<Candidate>( 1800 new ast::InitExpr{ 1801 initExpr->location, 1802 restructureCast( cand->expr, toType ), 1803 initAlt.designation }, 1804 std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost ); 1805 // currently assertions are always resolved immediately so this should have no effect. 1806 // if this somehow changes in the future (e.g. delayed by indeterminate return type) 1807 // we may need to revisit the logic. 1808 inferParameters( newCand, matches ); 1809 } 1810 } 1780 CandidateRef newCand = std::make_shared<Candidate>( 1781 new ast::InitExpr{ 1782 initExpr->location, 1783 restructureCast( cand->expr, toType ), 1784 initAlt.designation }, 1785 std::move(env), std::move( open ), std::move( need ), cand->cost + thisCost ); 1786 // currently assertions are always resolved immediately so this should have no effect. 1787 // if this somehow changes in the future (e.g. delayed by indeterminate return type) 1788 // we may need to revisit the logic. 1789 inferParameters( newCand, matches ); 1811 1790 } 1812 1791 } -
TabularUnified src/ResolvExpr/CommonType.cc ¶
r41c8312 racb33f15 388 388 const ast::EnumDecl* enumDecl = enumInst->base; 389 389 if ( !enumDecl->base ) { 390 ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];391 if (392 ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )393 || widen.first )394 && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )395 || widen.second )396 ) {397 result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };398 }399 }400 } else if ( auto type2AsAttr = dynamic_cast< const ast::EnumAttrType * >( type2 ) ) {401 if ( type2AsAttr->attr == ast::EnumAttribute::Posn ) {402 390 ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ]; 403 391 if ( … … 656 644 } 657 645 658 void postvisit( const ast::EnumAttrType * ) {}659 660 646 void postvisit( const ast::TraitInstType * ) {} 661 647 -
TabularUnified src/ResolvExpr/ConversionCost.cc ¶
r41c8312 racb33f15 279 279 if ( const ast::BasicType * dstAsBasic = dynamic_cast< const ast::BasicType * >( dst ) ) { 280 280 conversionCostFromBasicToBasic( basicType, dstAsBasic ); 281 } else if ( dynamic_cast< const ast::EnumAttrType *>(dst) ) { 282 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) }; 283 cost = costCalc( basicType, integer, srcIsLvalue, symtab, env ); 284 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 281 } else if ( auto dstAsEnumInst = dynamic_cast< const ast::EnumInstType * >( dst ) ) { 285 282 if ( dstAsEnumInst->base && !dstAsEnumInst->base->isTyped ) { 286 283 cost = Cost::unsafe; … … 373 370 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) }; 374 371 cost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 375 if ( cost < Cost::unsafe ) { 376 cost.incSafe(); 377 } 378 } 379 380 void ConversionCost::postvisit( const ast::EnumAttrType * src ) { 381 auto dstAsEnumAttrType = dynamic_cast<const ast::EnumAttrType *>(dst); 382 assert( src->attr != ast::EnumAttribute::Label ); 383 if ( src->attr == ast::EnumAttribute::Value ) { 384 if ( dstAsEnumAttrType && dstAsEnumAttrType->attr == ast::EnumAttribute::Value) { 385 cost = costCalc( src->instance, dstAsEnumAttrType->instance, srcIsLvalue, symtab, env ); 386 } else { 387 auto baseType = src->instance->base->base; 388 cost = costCalc( baseType, dst, srcIsLvalue, symtab, env ); 389 if ( cost < Cost::infinity ) { 390 cost.incUnsafe(); 391 } 392 } 393 } else { // ast::EnumAttribute::Posn 394 if ( auto dstBase = dynamic_cast<const ast::EnumInstType *>( dst ) ) { 395 cost = costCalc( src->instance, dstBase, srcIsLvalue, symtab, env ); 396 if ( cost < Cost::unsafe ) cost.incSafe(); 397 } else { 398 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) }; 399 cost = costCalc( integer, dst, srcIsLvalue, symtab, env ); 400 if ( cost < Cost::unsafe ) { 401 cost.incSafe(); 402 } 403 } 404 } 372 if ( !inst->base->isTyped ) { 373 if ( cost < Cost::unsafe ) { 374 cost.incSafe(); 375 } 376 return; 377 } 378 cost.incUnsafe(); 405 379 } 406 380 -
TabularUnified src/ResolvExpr/ConversionCost.h ¶
r41c8312 racb33f15 72 72 void postvisit( const ast::ZeroType * zeroType ); 73 73 void postvisit( const ast::OneType * oneType ); 74 void postvisit( const ast::EnumAttrType * posType );75 74 private: 76 75 // refactor for code resue -
TabularUnified src/ResolvExpr/Unify.cc ¶
r41c8312 racb33f15 275 275 276 276 void postvisit( const ast::VoidType * vt) { 277 result = dynamic_cast< const ast::VoidType * >( type2 ) 278 || tryToUnifyWithEnumValue(vt, type2, tenv, need, have, open, noWiden());277 result = dynamic_cast< const ast::VoidType * >( type2 ); 278 // || tryToUnifyWithEnumValue(vt, type2, tenv, need, have, open, noWiden()); 279 279 ; 280 280 } … … 284 284 result = basic->kind == basic2->kind; 285 285 } 286 result = result || tryToUnifyWithEnumValue(basic, type2, tenv, need, have, open, noWiden());286 // result = result || tryToUnifyWithEnumValue(basic, type2, tenv, need, have, open, noWiden()); 287 287 } 288 288 … … 293 293 noWiden()); 294 294 } 295 result = result || tryToUnifyWithEnumValue(pointer, type2, tenv, need, have, open, noWiden());295 // result = result || tryToUnifyWithEnumValue(pointer, type2, tenv, need, have, open, noWiden()); 296 296 } 297 297 … … 311 311 312 312 result = unifyExact( 313 array->base, array2->base, tenv, need, have, open, noWiden()) 314 || tryToUnifyWithEnumValue(array, type2, tenv, need, have, open, noWiden());313 array->base, array2->base, tenv, need, have, open, noWiden()); 314 // || tryToUnifyWithEnumValue(array, type2, tenv, need, have, open, noWiden()); 315 315 } 316 316 … … 404 404 } 405 405 406 bool tryToUnifyWithEnumValue( const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,407 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,408 WidenMode widen) {409 if ( auto attrType2 = dynamic_cast<const ast::EnumAttrType *>(type2)) {410 if (attrType2->attr == ast::EnumAttribute::Value) {411 return unifyExact( type1, attrType2->instance->base->base, env, need, have, open,412 widen);413 } else if (attrType2->attr == ast::EnumAttribute::Posn) {414 return unifyExact( type1, attrType2->instance, env, need, have, open, widen );415 }416 }417 return false;418 }419 420 406 public: 421 407 void postvisit( const ast::FunctionType * func ) { … … 527 513 void postvisit( const ast::StructInstType * aggrType ) { 528 514 handleGenericRefType( aggrType, type2 ); 529 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());530 515 } 531 516 532 517 void postvisit( const ast::UnionInstType * aggrType ) { 533 518 handleGenericRefType( aggrType, type2 ); 534 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());535 519 } 536 520 537 521 void postvisit( const ast::EnumInstType * aggrType ) { 538 522 handleRefType( aggrType, type2 ); 539 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());540 }541 542 void postvisit( const ast::EnumAttrType * enumAttr ) {543 // Lazy approach for now544 if ( auto otherPos = dynamic_cast< const ast::EnumAttrType *>( type2 ) ) {545 if ( enumAttr->match(otherPos) ) {546 result = otherPos;547 }548 }549 523 } 550 524 551 525 void postvisit( const ast::TraitInstType * aggrType ) { 552 526 handleRefType( aggrType, type2 ); 553 result = result || tryToUnifyWithEnumValue(aggrType, type2, tenv, need, have, open, noWiden());554 527 } 555 528 … … 560 533 this->result = otherInst; 561 534 } 562 result = result || tryToUnifyWithEnumValue(typeInst, type2, tenv, need, have, open, noWiden());563 535 } 564 536 … … 634 606 auto types2 = flatten( flat2 ); 635 607 636 result = unifyList( types, types2, tenv, need, have, open ) 637 || tryToUnifyWithEnumValue(tuple, type2, tenv, need, have, open, noWiden());608 result = unifyList( types, types2, tenv, need, have, open ); 609 // || tryToUnifyWithEnumValue(tuple, type2, tenv, need, have, open, noWiden()); 638 610 } 639 611 640 612 void postvisit( const ast::VarArgsType * vat) { 641 result = dynamic_cast< const ast::VarArgsType * >( type2 ) 642 || tryToUnifyWithEnumValue(vat, type2, tenv, need, have, open, noWiden());613 result = dynamic_cast< const ast::VarArgsType * >( type2 ); 614 // || tryToUnifyWithEnumValue(vat, type2, tenv, need, have, open, noWiden()); 643 615 } 644 616 645 617 void postvisit( const ast::ZeroType * zt) { 646 result = dynamic_cast< const ast::ZeroType * >( type2 ) 647 || tryToUnifyWithEnumValue(zt, type2, tenv, need, have, open, noWiden());618 result = dynamic_cast< const ast::ZeroType * >( type2 ); 619 // || tryToUnifyWithEnumValue(zt, type2, tenv, need, have, open, noWiden()); 648 620 } 649 621 650 622 void postvisit( const ast::OneType * ot) { 651 result = dynamic_cast< const ast::OneType * >( type2 ) 652 || tryToUnifyWithEnumValue(ot, type2, tenv, need, have, open, noWiden());623 result = dynamic_cast< const ast::OneType * >( type2 ); 624 // || tryToUnifyWithEnumValue(ot, type2, tenv, need, have, open, noWiden()); 653 625 } 654 626 };
Note: See TracChangeset
for help on using the changeset viewer.