Changeset 0f19d763 for src/ResolvExpr
- Timestamp:
- Jun 17, 2015, 4:17:37 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:
- eb50842
- Parents:
- 09d789c
- Location:
- src/ResolvExpr
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
r09d789c r0f19d763 89 89 }; 90 90 91 /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations 91 92 template< typename InputIterator, typename OutputIterator > 92 93 void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out, const SymTab::Indexer &indexer ) { … … 108 109 std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl; 109 110 ) 110 111 selected[ mangleName ] = current; 111 112 } else if ( candidate->cost == mapPlace->second.candidate->cost ) { 112 113 PRINT( 113 114 std::cout << "marking ambiguous" << std::endl; 114 115 ) 115 116 mapPlace->second.isAmbiguous = true; 116 117 } 117 118 } else { … … 124 125 ) 125 126 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; 127 // accept the alternatives that were unambiguous 128 for ( std::map< std::string, PruneStruct >::iterator target = selected.begin(); target != selected.end(); ++target ) { 129 if ( ! target->second.isAmbiguous ) { 130 Alternative &alt = *target->second.candidate; 131 for ( std::list< Type* >::iterator result = alt.expr->get_results().begin(); result != alt.expr->get_results().end(); ++result ) { 132 alt.env.applyFree( *result ); 134 133 } 134 *out++ = alt; 135 135 } 136 } 136 137 137 138 } … … 184 185 printAlts( finder.alternatives, std::cout ); 185 186 ) 186 187 *out++ = finder; 187 188 } 188 189 } … … 206 207 printAlts( alternatives, std::cout ); 207 208 ) 208 209 AltList::iterator oldBegin = alternatives.begin(); 209 210 pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer ); 210 211 if ( alternatives.begin() == oldBegin ) { … … 222 223 std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl; 223 224 ) 224 225 } 225 226 226 227 void AlternativeFinder::findWithAdjustment( Expression *expr ) { … … 353 354 354 355 bool AlternativeFinder::instantiateFunction( std::list< DeclarationWithType* >& formals, /*const*/ AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave ) { 355 std::list< TypeEnvironment > toBeDone;356 356 simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv ); 357 357 // make sure we don't widen any existing bindings … … 382 382 std::cerr << std::endl; 383 383 ) 384 385 386 384 if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) { 385 return false; 386 } 387 387 formal++; 388 388 } … … 430 430 printAssertionSet( newNeed, std::cerr, 8 ); 431 431 ) 432 432 inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out ); 433 433 return; 434 434 } … … 445 445 std::cerr << std::endl; 446 446 ) 447 447 std::list< DeclarationWithType* > candidates; 448 448 decls.lookupId( curDecl->get_name(), candidates ); 449 449 /// if ( candidates.empty() ) { std::cout << "no candidates!" << std::endl; } … … 454 454 std::cout << std::endl; 455 455 ) 456 456 AssertionSet newHave, newerNeed( newNeed ); 457 457 TypeEnvironment newEnv( newAlt.env ); 458 458 OpenVarSet newOpenVars( openVars ); … … 467 467 std::cerr << std::endl; 468 468 ) 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 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 } 496 496 } 497 497 } … … 510 510 decls.print( std::cout ); 511 511 ) 512 512 addToIndexer( have, decls ); 513 513 AssertionSet newNeed; 514 514 inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, 0, indexer, out ); … … 534 534 printAssertionSet( resultNeed, std::cout, 8 ); 535 535 ) 536 536 inferParameters( resultNeed, resultHave, newAlt, openVars, out ); 537 537 } 538 538 } … … 542 542 AlternativeFinder funcOpFinder( indexer, env ); 543 543 544 AlternativeFinder funcFinder( indexer, env ); { 544 AlternativeFinder funcFinder( indexer, env ); 545 546 { 545 547 NameExpr *fname; 546 548 if ( ( fname = dynamic_cast<NameExpr *>( untypedExpr->get_function())) … … 560 562 Tuples::TupleAssignSpotter tassign( this ); 561 563 if ( tassign.isTupleAssignment( untypedExpr, possibilities ) ) { 562 // take care of possible tuple assignments, or discard expression564 // TODO take care of possible tuple assignments, or discard expression 563 565 return; 564 566 } // else ... … … 571 573 func->print( std::cout, 8 ); 572 574 ) 573 574 575 // check if the type is pointer to function 576 PointerType *pointer; 575 577 if ( func->expr->get_results().size() == 1 && ( pointer = dynamic_cast< PointerType* >( func->expr->get_results().front() ) ) ) { 576 578 if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) { … … 604 606 printAlts( funcOpFinder.alternatives, std::cout, 8 ); 605 607 ) 606 608 } 607 609 608 610 for ( AltList::const_iterator funcOp = funcOpFinder.alternatives.begin(); funcOp != funcOpFinder.alternatives.end(); ++funcOp ) { … … 737 739 indexer.lookupId( nameExpr->get_name(), declList ); 738 740 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 741 for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) { 742 VariableExpr newExpr( *i, nameExpr->get_argName() ); 743 alternatives.push_back( Alternative( newExpr.clone(), env, Cost() ) ); 744 PRINT( 745 std::cerr << "decl is "; 746 (*i)->print( std::cerr ); 747 std::cerr << std::endl; 748 std::cerr << "newExpr is "; 749 newExpr.print( std::cerr ); 750 std::cerr << std::endl; 751 ) 752 renameTypes( alternatives.back().expr ); 753 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) { 754 addAggMembers( structInst, &newExpr, Cost( 0, 0, 1 ), "" ); 755 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( (*i)->get_type() ) ) { 756 addAggMembers( unionInst, &newExpr, Cost( 0, 0, 1 ), "" ); 757 } // if 758 } // for 757 759 } 758 760 -
src/ResolvExpr/AlternativeFinder.h
r09d789c r0f19d763 30 30 AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); 31 31 void find( Expression *expr, bool adjust = false ); 32 /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types 32 33 void findWithAdjustment( Expression *expr ); 33 34 AltList &get_alternatives() { return alternatives; } -
src/ResolvExpr/RenameVars.cc
r09d789c r0f19d763 118 118 /// type->print( std::cout ); 119 119 /// std::cout << std::endl; 120 // copies current name mapping into new mapping 120 121 mapStack.push_front( mapStack.front() ); 122 // renames all "forall" type names to `_${level}_${name}' 121 123 for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { 122 124 std::ostrstream output; … … 125 127 mapStack.front()[ (*i)->get_name() ] = newname; 126 128 (*i)->set_name( newname ); 129 // ditto for assertion names, the next level in 127 130 level++; 128 131 acceptAll( (*i)->get_assertions(), *this ); … … 132 135 133 136 void RenameVars::typeAfter( Type *type ) { 137 // clears name mapping added by typeBefore() 134 138 if ( ! type->get_forall().empty() ) { 135 139 mapStack.pop_front(); -
src/ResolvExpr/RenameVars.h
r09d789c r0f19d763 25 25 26 26 namespace ResolvExpr { 27 28 /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID 27 29 class RenameVars : public Visitor { 28 30 public:
Note: See TracChangeset
for help on using the changeset viewer.