Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r0f19d763 rde62360d  
    1010// Created On       : Sat May 16 23:52:08 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat May 16 23:55:30 2015
    13 // Update Count     : 3
     12// Last Modified On : Mon Jun 22 17:19:54 2015
     13// Update Count     : 17
    1414//
    1515
     
    8989                };
    9090
    91                 /// Prunes a list of alternatives down to those that have the minimum conversion cost for a given return type; skips ambiguous interpretations
    9291                template< typename InputIterator, typename OutputIterator >
    9392                void pruneAlternatives( InputIterator begin, InputIterator end, OutputIterator out, const SymTab::Indexer &indexer ) {
     
    108107                                                PRINT(
    109108                                                        std::cout << "cost " << candidate->cost << " beats " << mapPlace->second.candidate->cost << std::endl;
    110                                                         )
     109                                                )
    111110                                                selected[ mangleName ] = current;
    112111                                        } else if ( candidate->cost == mapPlace->second.candidate->cost ) {
    113112                                                PRINT(
    114113                                                        std::cout << "marking ambiguous" << std::endl;
    115                                                         )
     114                                                )
    116115                                                mapPlace->second.isAmbiguous = true;
    117116                                        }
     
    123122                        PRINT(
    124123                                std::cout << "there are " << selected.size() << " alternatives before elimination" << std::endl;
    125                                 )
     124                        )
    126125
    127126                        // accept the alternatives that were unambiguous
     
    184183                                std::cout << "findSubExprs" << std::endl;
    185184                                printAlts( finder.alternatives, std::cout );
    186                                 )
     185                        )
    187186                        *out++ = finder;
    188187                }
     
    206205                        std::cout << "alternatives before prune:" << std::endl;
    207206                        printAlts( alternatives, std::cout );
    208                         )
     207                )
    209208                AltList::iterator oldBegin = alternatives.begin();
    210209                pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ), indexer );
    211210                if ( alternatives.begin() == oldBegin ) {
    212                         std::ostrstream stream;
     211                        std::ostringstream stream;
    213212                        stream << "Can't choose between alternatives for expression ";
    214213                        expr->print( stream );
     
    217216                        findMinCost( alternatives.begin(), alternatives.end(), back_inserter( winners ) );
    218217                        printAlts( winners, stream, 8 );
    219                         throw SemanticError( std::string( stream.str(), stream.pcount() ) );
     218                        throw SemanticError( stream.str() );
    220219                }
    221220                alternatives.erase( oldBegin, alternatives.end() );
    222221                PRINT(
    223222                        std::cout << "there are " << alternatives.size() << " alternatives after elimination" << std::endl;
    224                         )
     223                )
    225224        }
    226225
     
    265264                                std::cout << "--- results are" << std::endl;
    266265                                printAll( (*actualExpr)->get_results(), std::cout, 8 );
    267                                 )
    268                                 std::list< DeclarationWithType* >::iterator startFormal = formal;
     266                        )
     267                        std::list< DeclarationWithType* >::iterator startFormal = formal;
    269268                        Cost actualCost;
    270269                        for ( std::list< Type* >::iterator actual = (*actualExpr)->get_results().begin(); actual != (*actualExpr)->get_results().end(); ++actual ) {
     
    282281                                        std::cout << std::endl << " to ";
    283282                                        (*formal)->get_type()->print( std::cout, 8 );
    284                                         )
    285                                         Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
     283                                )
     284                                Cost newCost = conversionCost( *actual, (*formal)->get_type(), indexer, alt.env );
    286285                                PRINT(
    287286                                        std::cout << std::endl << "cost is" << newCost << std::endl;
    288                                         )
    289 
    290                                         if ( newCost == Cost::infinity ) {
    291                                                 return newCost;
    292                                         }
     287                                )
     288
     289                                if ( newCost == Cost::infinity ) {
     290                                        return newCost;
     291                                }
    293292                                convCost += newCost;
    294293                                actualCost += newCost;
     
    354353
    355354        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;
    356356                simpleCombineEnvironments( actuals.begin(), actuals.end(), resultEnv );
    357357                // make sure we don't widen any existing bindings
     
    381381                                        (*actual)->print( std::cerr );
    382382                                        std::cerr << std::endl;
    383                                         )
     383                                )
    384384                                if ( ! unify( (*formal)->get_type(), *actual, resultEnv, resultNeed, resultHave, openVars, indexer ) ) {
    385385                                        return false;
     
    429429                                        std::cerr << "recursing with new set:" << std::endl;
    430430                                        printAssertionSet( newNeed, std::cerr, 8 );
    431                                         )
     431                                )
    432432                                inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out );
    433433                                return;
     
    444444                        curDecl->print( std::cerr );
    445445                        std::cerr << std::endl;
    446                         )
     446                )
    447447                std::list< DeclarationWithType* > candidates;
    448448                decls.lookupId( curDecl->get_name(), candidates );
     
    453453                                (*candidate)->print( std::cout );
    454454                                std::cout << std::endl;
    455                                 )
     455                        )
    456456                        AssertionSet newHave, newerNeed( newNeed );
    457457                        TypeEnvironment newEnv( newAlt.env );
     
    466466                                adjType->print( std::cerr );
    467467                                std::cerr << std::endl;
    468                                 )
     468                        )
    469469                        if ( unify( curDecl->get_type(), adjType, newEnv, newerNeed, newHave, newOpenVars, indexer ) ) {
    470470                                PRINT(
    471471                                        std::cerr << "success!" << std::endl;
    472                                         )
     472                                )
    473473                                SymTab::Indexer newDecls( decls );
    474474                                addToIndexer( newHave, newDecls );
     
    486486                                        (*candidate)->print( std::cout );
    487487                                        std::cout << std::endl;
    488                                         )
     488                                )
    489489                                ApplicationExpr *appExpr = static_cast< ApplicationExpr* >( newerAlt.expr );
    490490                                // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
     
    509509                        std::cout << "============= new indexer" << std::endl;
    510510                        decls.print( std::cout );
    511                         )
     511                )
    512512                addToIndexer( have, decls );
    513513                AssertionSet newNeed;
     
    533533                                std::cout << "need assertions:" << std::endl;
    534534                                printAssertionSet( resultNeed, std::cout, 8 );
    535                                 )
     535                        )
    536536                        inferParameters( resultNeed, resultHave, newAlt, openVars, out );
    537537                }
     
    542542                AlternativeFinder funcOpFinder( indexer, env );
    543543
    544                 AlternativeFinder funcFinder( indexer, env );
    545 
    546                 {
     544                AlternativeFinder funcFinder( indexer, env ); {
    547545                        NameExpr *fname;
    548546                        if ( ( fname = dynamic_cast<NameExpr *>( untypedExpr->get_function()))
    549                                  && ( fname->get_name() == std::string("LabAddress")) ) {
    550                                 alternatives.push_back( Alternative( untypedExpr, env, Cost()) );
     547                                 && ( fname->get_name() == std::string("&&")) ) {
     548                                alternatives.push_back( Alternative( untypedExpr->clone(), env, Cost()) );
    551549                                return;
    552550                        }
     
    562560                Tuples::TupleAssignSpotter tassign( this );
    563561                if ( tassign.isTupleAssignment( untypedExpr, possibilities ) ) {
    564                         // TODO take care of possible tuple assignments, or discard expression
     562                        // take care of possible tuple assignments, or discard expression
    565563                        return;
    566564                } // else ...
     
    572570                                std::cout << "working on alternative: " << std::endl;
    573571                                func->print( std::cout, 8 );
    574                                 )
     572                        )
    575573                        // check if the type is pointer to function
    576574                        PointerType *pointer;
     
    605603                                                std::cout << "known function ops:" << std::endl;
    606604                                                printAlts( funcOpFinder.alternatives, std::cout, 8 );
    607                                                 )
     605                                        )
    608606                                }
    609607
     
    644642                                withFunc->env.print( std::cout, 8 );
    645643                                std::cout << "cost of conversion is:" << cvtCost << std::endl;
    646                                 )
    647                                 if ( cvtCost != Cost::infinity ) {
    648                                         withFunc->cvtCost = cvtCost;
    649                                         alternatives.push_back( *withFunc );
    650                                 } // if
     644                        )
     645                        if ( cvtCost != Cost::infinity ) {
     646                                withFunc->cvtCost = cvtCost;
     647                                alternatives.push_back( *withFunc );
     648                        } // if
    651649                } // for
    652650                candidates.clear();
     
    749747                                newExpr.print( std::cerr );
    750748                                std::cerr << std::endl;
    751                                 )
     749                        )
    752750                        renameTypes( alternatives.back().expr );
    753751                        if ( StructInstType *structInst = dynamic_cast< StructInstType* >( (*i)->get_type() ) ) {
     
    796794                        argType->print( std::cout );
    797795                        std::cout << std::endl;
    798                         )
    799                         if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
    800                                 alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
    801                                 for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
    802                                         alternatives.back().expr->get_results().push_back( (*i)->get_type()->clone() );
    803                                 } // for
    804                         } // if
     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
    805803        }
    806804
Note: See TracChangeset for help on using the changeset viewer.