Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    ra40d503 r8a62d04  
    8383        }
    8484
    85         void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt ) {
    86                 Indenter indent = { Indenter::tabsize, indentAmt };
    87                 for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
    88                         i->print( os, indent );
    89                         os << std::endl;
    90                 }
    91         }
    92 
    9385        namespace {
     86                void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ) {
     87                        Indenter indent = { Indenter::tabsize, indentAmt };
     88                        for ( AltList::const_iterator i = list.begin(); i != list.end(); ++i ) {
     89                                i->print( os, indent );
     90                                os << std::endl;
     91                        }
     92                }
     93
    9494                void makeExprList( const AltList &in, std::list< Expression* > &out ) {
    9595                        for ( AltList::const_iterator i = in.begin(); i != in.end(); ++i ) {
     
    469469                        std::cerr << std::endl;
    470470                )
    471                 std::list< SymTab::Indexer::IdData > candidates;
     471                std::list< DeclarationWithType* > candidates;
    472472                decls.lookupId( curDecl->get_name(), candidates );
    473473///   if ( candidates.empty() ) { std::cerr << "no candidates!" << std::endl; }
    474                 for ( const auto & data : candidates ) {
    475                         DeclarationWithType * candidate = data.id;
     474                for ( std::list< DeclarationWithType* >::const_iterator candidate = candidates.begin(); candidate != candidates.end(); ++candidate ) {
    476475                        PRINT(
    477476                                std::cerr << "inferRecursive: candidate is ";
    478                                 candidate->print( std::cerr );
     477                                (*candidate)->print( std::cerr );
    479478                                std::cerr << std::endl;
    480479                        )
     
    483482                        TypeEnvironment newEnv( newAlt.env );
    484483                        OpenVarSet newOpenVars( openVars );
    485                         Type *adjType = candidate->get_type()->clone();
     484                        Type *adjType = (*candidate)->get_type()->clone();
    486485                        adjustExprType( adjType, newEnv, indexer );
    487486                        adjType->accept( global_renamer );
     
    501500                                Alternative newerAlt( newAlt );
    502501                                newerAlt.env = newEnv;
    503                                 assertf( candidate->get_uniqueId(), "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() );
     502                                assertf( (*candidate)->get_uniqueId(), "Assertion candidate does not have a unique ID: %s", toString( *candidate ).c_str() );
     503                                DeclarationWithType *candDecl = static_cast< DeclarationWithType* >( Declaration::declFromId( (*candidate)->get_uniqueId() ) );
    504504
    505505                                // everything with an empty idChain was pulled in by the current assertion.
     
    516516                                // DOESN'T WORK: grandchild nodes conflict with their cousins
    517517                                //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue;
    518                                 Expression *varExpr = data.combine();
     518                                Expression *varExpr = new VariableExpr( candDecl );
    519519                                delete varExpr->get_result();
    520520                                varExpr->set_result( adjType->clone() );
     
    522522                                        std::cerr << "satisfying assertion " << curDecl->get_uniqueId() << " ";
    523523                                        curDecl->print( std::cerr );
    524                                         std::cerr << " with declaration " << candidate->get_uniqueId() << " ";
    525                                         candidate->print( std::cerr );
     524                                        std::cerr << " with declaration " << (*candidate)->get_uniqueId() << " ";
     525                                        (*candidate)->print( std::cerr );
    526526                                        std::cerr << std::endl;
    527527                                )
     
    532532                                }
    533533                                // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
    534                                 (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( candidate->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
     534                                (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( (*candidate)->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
    535535                                inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out );
    536536                        } else {
     
    13171317
    13181318        void AlternativeFinder::visit( NameExpr *nameExpr ) {
    1319                 std::list< SymTab::Indexer::IdData > declList;
     1319                std::list< DeclarationWithType* > declList;
    13201320                indexer.lookupId( nameExpr->get_name(), declList );
    13211321                PRINT( std::cerr << "nameExpr is " << nameExpr->get_name() << std::endl; )
    1322                 for ( auto & data : declList ) {
    1323                         Expression * newExpr = data.combine();
    1324                         // xxx - add in extra cost for with-statement exprs?
    1325                         alternatives.push_back( Alternative( newExpr, env, Cost::zero ) );
     1322                for ( std::list< DeclarationWithType* >::iterator i = declList.begin(); i != declList.end(); ++i ) {
     1323                        VariableExpr newExpr( *i );
     1324                        alternatives.push_back( Alternative( newExpr.clone(), env, Cost::zero ) );
    13261325                        PRINT(
    13271326                                std::cerr << "decl is ";
    1328                                 data.id->print( std::cerr );
     1327                                (*i)->print( std::cerr );
    13291328                                std::cerr << std::endl;
    13301329                                std::cerr << "newExpr is ";
    1331                                 newExpr->print( std::cerr );
     1330                                newExpr.print( std::cerr );
    13321331                                std::cerr << std::endl;
    13331332                        )
     
    14211420        }
    14221421
    1423         namespace {
    1424                 void resolveAttr( SymTab::Indexer::IdData data, FunctionType *function, Type *argType, const TypeEnvironment &env, AlternativeFinder & finder ) {
    1425                         // assume no polymorphism
    1426                         // assume no implicit conversions
    1427                         assert( function->get_parameters().size() == 1 );
    1428                         PRINT(
    1429                                 std::cerr << "resolvAttr: funcDecl is ";
    1430                                 data.id->print( std::cerr );
    1431                                 std::cerr << " argType is ";
    1432                                 argType->print( std::cerr );
    1433                                 std::cerr << std::endl;
    1434                         )
    1435                         const SymTab::Indexer & indexer = finder.get_indexer();
    1436                         AltList & alternatives = finder.get_alternatives();
    1437                         if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
    1438                                 alternatives.push_back( Alternative( new AttrExpr( data.combine(), argType->clone() ), env, Cost::zero ) );
    1439                                 for ( DeclarationWithType * retVal : function->returnVals ) {
    1440                                         alternatives.back().expr->result = retVal->get_type()->clone();
    1441                                 } // for
    1442                         } // if
    1443                 }
     1422        void AlternativeFinder::resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ) {
     1423                // assume no polymorphism
     1424                // assume no implicit conversions
     1425                assert( function->get_parameters().size() == 1 );
     1426                PRINT(
     1427                        std::cerr << "resolvAttr: funcDecl is ";
     1428                        funcDecl->print( std::cerr );
     1429                        std::cerr << " argType is ";
     1430                        argType->print( std::cerr );
     1431                        std::cerr << std::endl;
     1432                )
     1433                if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
     1434                        alternatives.push_back( Alternative( new AttrExpr( new VariableExpr( funcDecl ), argType->clone() ), env, Cost::zero ) );
     1435                        for ( std::list< DeclarationWithType* >::iterator i = function->get_returnVals().begin(); i != function->get_returnVals().end(); ++i ) {
     1436                                alternatives.back().expr->set_result( (*i)->get_type()->clone() );
     1437                        } // for
     1438                } // if
    14441439        }
    14451440
     
    14481443                NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
    14491444                assert( nameExpr );
    1450                 std::list< SymTab::Indexer::IdData > attrList;
     1445                std::list< DeclarationWithType* > attrList;
    14511446                indexer.lookupId( nameExpr->get_name(), attrList );
    14521447                if ( attrExpr->get_isType() || attrExpr->get_expr() ) {
    1453                         for ( auto & data : attrList ) {
    1454                                 DeclarationWithType * id = data.id;
     1448                        for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
    14551449                                // check if the type is function
    1456                                 if ( FunctionType *function = dynamic_cast< FunctionType* >( id->get_type() ) ) {
     1450                                if ( FunctionType *function = dynamic_cast< FunctionType* >( (*i)->get_type() ) ) {
    14571451                                        // assume exactly one parameter
    14581452                                        if ( function->get_parameters().size() == 1 ) {
    14591453                                                if ( attrExpr->get_isType() ) {
    1460                                                         resolveAttr( data, function, attrExpr->get_type(), env, *this );
     1454                                                        resolveAttr( *i, function, attrExpr->get_type(), env );
    14611455                                                } else {
    14621456                                                        AlternativeFinder finder( indexer, env );
     
    14641458                                                        for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) {
    14651459                                                                if ( choice->expr->get_result()->size() == 1 ) {
    1466                                                                         resolveAttr(data, function, choice->expr->get_result(), choice->env, *this );
     1460                                                                        resolveAttr(*i, function, choice->expr->get_result(), choice->env );
    14671461                                                                } // fi
    14681462                                                        } // for
     
    14721466                        } // for
    14731467                } else {
    1474                         for ( auto & data : attrList ) {
    1475                                 alternatives.push_back( Alternative( data.combine(), env, Cost::zero ) );
     1468                        for ( std::list< DeclarationWithType* >::iterator i = attrList.begin(); i != attrList.end(); ++i ) {
     1469                                VariableExpr newExpr( *i );
     1470                                alternatives.push_back( Alternative( newExpr.clone(), env, Cost::zero ) );
    14761471                                renameTypes( alternatives.back().expr );
    14771472                        } // for
Note: See TracChangeset for help on using the changeset viewer.