Changeset 7c64920 for src/ResolvExpr
- Timestamp:
- May 25, 2015, 1:57:22 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 53a2e97
- Parents:
- 5887db27
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
r5887db27 r7c64920 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sat May 16 23:52:08 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sat May 16 23:55:30201513 // Update Count : 311 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu May 21 16:21:09 2015 13 // Update Count : 12 14 14 // 15 15 … … 107 107 PRINT( 108 108 std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl; 109 110 109 ) 110 selected[ mangleName ] = current; 111 111 } else if ( candidate->cost == mapPlace->second.candidate->cost ) { 112 112 PRINT( 113 113 std::cout << "marking ambiguous" << std::endl; 114 115 114 ) 115 mapPlace->second.isAmbiguous = true; 116 116 } 117 117 } else { … … 122 122 PRINT( 123 123 std::cout << "there are " << selected.size() << " alternatives before elimination" << std::endl; 124 ) 125 126 // accept the alternatives that were unambiguous 127 for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) { 128 if ( ! target->second.isAmbiguous ) { 129 Alternative &alt = *target->second.candidate; 130 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) { 131 alt.env.applyFree( *result ); 132 } 133 *out++ = alt; 124 ) 125 126 // accept the alternatives that were unambiguous 127 for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) { 128 if ( ! target->second.isAmbiguous ) { 129 Alternative &alt = *target->second.candidate; 130 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) { 131 alt.env.applyFree( *result ); 134 132 } 135 } 133 *out++ = alt; 134 } 135 } 136 136 137 137 } … … 183 183 std::cout << "findSubExprs" << std::endl; 184 184 printAlts( finder.alternatives, std::cout ); 185 186 185 ) 186 *out++ = finder; 187 187 } 188 188 } … … 205 205 std::cout << "alternatives before prune:" << std::endl; 206 206 printAlts( alternatives, std::cout ); 207 208 207 ) 208 AltList::iterator oldBegin = alternatives.begin(); 209 209 pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer ); 210 210 if ( alternatives.begin() == oldBegin ) { … … 221 221 PRINT( 222 222 std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl; 223 224 223 ) 224 } 225 225 226 226 void AlternativeFinder::findWithAdjustment( Expression *expr ) { … … 264 264 std::cout << "--- results are" << std::endl; 265 265 printAll( (*actualExpr)->get_results(), std::cout, 8 ); 266 267 266 ) 267 std::list< DeclarationWithType* >::iterator startFormal = formal; 268 268 Cost actualCost; 269 269 for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) { … … 281 281 std::cout << std::endl << " to "; 282 282 (*formal)->get_type()->print( std::cout, 8 ); 283 284 283 ) 284 Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env ); 285 285 PRINT( 286 286 std::cout << std::endl << "cost is" << newCost << std::endl; 287 288 289 290 291 287 ) 288 289 if ( newCost == Cost::infinity ) { 290 return newCost; 291 } 292 292 convCost += newCost; 293 293 actualCost += newCost; … … 381 381 (*actual)->print( std::cerr ); 382 382 std::cerr << std::endl; 383 384 385 386 383 ) 384 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) { 385 return false; 386 } 387 387 formal++; 388 388 } … … 429 429 std::cerr << "recursing with new set:" << std::endl; 430 430 printAssertionSet( newNeed, std::cerr, 8 ); 431 432 431 ) 432 inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out ); 433 433 return; 434 434 } … … 444 444 curDecl->print( std::cerr ); 445 445 std::cerr << std::endl; 446 447 446 ) 447 std::list< DeclarationWithType* > candidates; 448 448 decls.lookupId( curDecl->get_name(), candidates ); 449 449 /// if ( candidates.empty() ) { std::cout << "no candidates!" << std::endl; } … … 453 453 (*candidate)->print( std::cout ); 454 454 std::cout << std::endl; 455 456 455 ) 456 AssertionSet newHave, newerNeed( newNeed ); 457 457 TypeEnvironment newEnv( newAlt.env ); 458 458 OpenVarSet newOpenVars( openVars ); … … 466 466 adjType->print( std::cerr ); 467 467 std::cerr << std::endl; 468 ) 469 if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) { 470 PRINT( 471 std::cerr << "success!" << std::endl; 468 472 ) 469 if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) { 470 PRINT( 471 std::cerr << "success!" << std::endl; 472 ) 473 SymTab::Indexer newDecls( decls ); 474 addToIndexer( newHave, newDecls ); 475 Alternative newerAlt( newAlt ); 476 newerAlt.env = newEnv; 477 assert( (*candidate)->get_uniqueId() ); 478 Expression *varExpr = new VariableExpr( static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ) ); 479 deleteAll( varExpr->get_results() ); 480 varExpr->get_results().clear(); 481 varExpr->get_results().push_front( adjType->clone() ); 482 PRINT( 483 std::cout << "satisfying assertion " << curDecl->get_uniqueId() << " "; 484 curDecl->print( std::cout ); 485 std::cout << " with declaration " << (*candidate)->get_uniqueId() << " "; 486 (*candidate)->print( std::cout ); 487 std::cout << std::endl; 488 ) 489 ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr ); 490 // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions 491 appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr ); 492 inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, level, indexer, out ); 493 } else { 494 delete adjType; 495 } 473 SymTab::Indexer newDecls( decls ); 474 addToIndexer( newHave, newDecls ); 475 Alternative newerAlt( newAlt ); 476 newerAlt.env = newEnv; 477 assert( (*candidate)->get_uniqueId() ); 478 Expression *varExpr = new VariableExpr( static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) ) ); 479 deleteAll( varExpr->get_results() ); 480 varExpr->get_results().clear(); 481 varExpr->get_results().push_front( adjType->clone() ); 482 PRINT( 483 std::cout << "satisfying assertion " << curDecl->get_uniqueId() << " "; 484 curDecl->print( std::cout ); 485 std::cout << " with declaration " << (*candidate)->get_uniqueId() << " "; 486 (*candidate)->print( std::cout ); 487 std::cout << std::endl; 488 ) 489 ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr ); 490 // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions 491 appExpr->get_inferParams()[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr ); 492 inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, level, indexer, out ); 493 } else { 494 delete adjType; 495 } 496 496 } 497 497 } … … 509 509 std::cout << "============= new indexer" << std::endl; 510 510 decls.print( std::cout ); 511 512 511 ) 512 addToIndexer( have, decls ); 513 513 AssertionSet newNeed; 514 514 inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, 0, indexer, out ); … … 533 533 std::cout << "need assertions:" << std::endl; 534 534 printAssertionSet( resultNeed, std::cout, 8 ); 535 536 535 ) 536 inferParameters( resultNeed, resultHave, newAlt, openVars, out ); 537 537 } 538 538 } … … 570 570 std::cout << "working on alternative: " << std::endl; 571 571 func->print( std::cout, 8 ); 572 573 574 572 ) 573 // check if the type is pointer to function 574 PointerType *pointer; 575 575 if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) { 576 576 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { … … 603 603 std::cout << "known function ops:" << std::endl; 604 604 printAlts( funcOpFinder.alternatives, std::cout, 8 ); 605 606 605 ) 606 } 607 607 608 608 for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) { … … 642 642 withFunc->env.print( std::cout, 8 ); 643 643 std::cout << "cost of conversion is:" << cvtCost << std::endl; 644 645 646 647 648 644 ) 645 if ( cvtCost != Cost::infinity ) { 646 withFunc->cvtCost = cvtCost; 647 alternatives.push_back( *withFunc ); 648 } // if 649 649 } // for 650 650 candidates.clear(); … … 737 737 indexer.lookupId( nameExpr->get_name(), declList ); 738 738 PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; ) 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 739 for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) { 740 VariableExpr newExpr( *i, nameExpr->get_argName() ); 741 alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) ); 742 PRINT( 743 std::cerr << "decl is "; 744 (*i)->print( std::cerr ); 745 std::cerr << std::endl; 746 std::cerr << "newExpr is "; 747 newExpr.print( std::cerr ); 748 std::cerr << std::endl; 749 ) 750 renameTypes( alternatives.back().expr ); 751 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) { 752 addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" ); 753 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) { 754 addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" ); 755 } // if 756 } // for 757 757 } 758 758 … … 794 794 argType->print( std::cout ); 795 795 std::cout << std::endl; 796 797 798 799 800 801 802 796 ) 797 if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) { 798 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) ); 799 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) { 800 alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() ); 801 } // for 802 } // if 803 803 } 804 804
Note: See TracChangeset
for help on using the changeset viewer.