Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    rc1ed2ee r6f096d2  
    336336                }
    337337
    338                 if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->result ) ) {
     338                if ( StructInstType * structInst = dynamic_cast< StructInstType* >( aggrExpr->result ) ) {
    339339                        addAggMembers( structInst, aggrExpr.get(), alt, alt.cost+Cost::safe, "" );
    340                 } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->result ) ) {
     340                } else if ( UnionInstType * unionInst = dynamic_cast< UnionInstType* >( aggrExpr->result ) ) {
    341341                        addAggMembers( unionInst, aggrExpr.get(), alt, alt.cost+Cost::safe, "" );
    342342                } // if
     
    344344
    345345        template< typename StructOrUnionType >
    346         void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Alternative& alt, const Cost &newCost, const std::string & name ) {
     346        void AlternativeFinder::Finder::addAggMembers( StructOrUnionType * aggInst, Expression * expr, const Alternative& alt, const Cost &newCost, const std::string & name ) {
    347347                std::list< Declaration* > members;
    348348                aggInst->lookup( name, members );
    349349
    350350                for ( Declaration * decl : members ) {
    351                         if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
     351                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType* >( decl ) ) {
    352352                                // addAnonAlternatives uses vector::push_back, which invalidates references to existing elements, so
    353353                                // can't construct in place and use vector::back
     
    362362        }
    363363
    364         void AlternativeFinder::Finder::addTupleMembers( TupleType *tupleType, Expression *expr,                        const Alternative &alt, const Cost &newCost, Expression *member ) {
     364        void AlternativeFinder::Finder::addTupleMembers( TupleType * tupleType, Expression * expr, const Alternative &alt, const Cost &newCost, Expression * member ) {
    365365                if ( ConstantExpr * constantExpr = dynamic_cast< ConstantExpr * >( member ) ) {
    366366                        // get the value of the constant expression as an int, must be between 0 and the length of the tuple type to have meaning
     
    368368                        std::string tmp;
    369369                        if ( val >= 0 && (unsigned long long)val < tupleType->size() ) {
    370                                 alternatives.push_back( Alternative{ 
     370                                alternatives.push_back( Alternative{
    371371                                        alt, new TupleIndexExpr( expr->clone(), val ), newCost } );
    372372                        } // if
     
    374374        }
    375375
    376         void AlternativeFinder::Finder::postvisit( ApplicationExpr *applicationExpr ) {
     376        void AlternativeFinder::Finder::postvisit( ApplicationExpr * applicationExpr ) {
    377377                alternatives.push_back( Alternative{ applicationExpr->clone(), env } );
    378378        }
     
    475475                }
    476476
    477                 // specialization cost of return types can't be accounted for directly, it disables 
     477                // specialization cost of return types can't be accounted for directly, it disables
    478478                // otherwise-identical calls, like this example based on auto-newline in the I/O lib:
    479479                //
     
    12261226                                // count one safe conversion for each value that is thrown away
    12271227                                thisCost.incSafe( discardedValues );
    1228                                 Alternative newAlt{ 
    1229                                         restructureCast( alt.expr->clone(), toType, castExpr->isGenerated ), 
     1228                                Alternative newAlt{
     1229                                        restructureCast( alt.expr->clone(), toType, castExpr->isGenerated ),
    12301230                                        alt.env, openVars, needAssertions, alt.cost, alt.cost + thisCost };
    12311231                                inferParameters( newAlt, back_inserter( candidates ) );
     
    13281328                if ( sizeofExpr->get_isType() ) {
    13291329                        Type * newType = sizeofExpr->get_type()->clone();
    1330                         alternatives.push_back( Alternative{ 
     1330                        alternatives.push_back( Alternative{
    13311331                                new SizeofExpr{ resolveTypeof( newType, indexer ) }, env } );
    13321332                } else {
     
    13431343                        Alternative &choice = winners.front();
    13441344                        referenceToRvalueConversion( choice.expr, choice.cost );
    1345                         alternatives.push_back( Alternative{ 
     1345                        alternatives.push_back( Alternative{
    13461346                                choice, new SizeofExpr( choice.expr->clone() ), Cost::zero } );
    13471347                } // if
     
    13511351                if ( alignofExpr->get_isType() ) {
    13521352                        Type * newType = alignofExpr->get_type()->clone();
    1353                         alternatives.push_back( Alternative{ 
     1353                        alternatives.push_back( Alternative{
    13541354                                new AlignofExpr{ resolveTypeof( newType, indexer ) }, env } );
    13551355                } else {
     
    13661366                        Alternative &choice = winners.front();
    13671367                        referenceToRvalueConversion( choice.expr, choice.cost );
    1368                         alternatives.push_back( Alternative{ 
     1368                        alternatives.push_back( Alternative{
    13691369                                choice, new AlignofExpr{ choice.expr->clone() }, Cost::zero } );
    13701370                } // if
     
    13771377                for ( std::list< Declaration* >::const_iterator i = members.begin(); i != members.end(); ++i ) {
    13781378                        if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( *i ) ) {
    1379                                 alternatives.push_back( Alternative{ 
     1379                                alternatives.push_back( Alternative{
    13801380                                        new OffsetofExpr{ aggInst->clone(), dwt }, env } );
    13811381                                renameTypes( alternatives.back().expr );
     
    14051405
    14061406        namespace {
    1407                 void resolveAttr( SymTab::Indexer::IdData data, FunctionType *function, Type *argType, const TypeEnvironment &env, AlternativeFinder & finder ) {
     1407                void resolveAttr( SymTab::Indexer::IdData data, const FunctionType * function, Type * argType, const TypeEnvironment &env, AlternativeFinder & finder ) {
    14081408                        // assume no polymorphism
    14091409                        // assume no implicit conversions
    1410                         assert( function->get_parameters().size() == 1 );
     1410                        assert( function->parameters.size() == 1 );
    14111411                        PRINT(
    14121412                                std::cerr << "resolvAttr: funcDecl is ";
     
    14181418                        const SymTab::Indexer & indexer = finder.get_indexer();
    14191419                        AltList & alternatives = finder.get_alternatives();
    1420                         if ( typesCompatibleIgnoreQualifiers( argType, function->get_parameters().front()->get_type(), indexer, env ) ) {
     1420                        if ( typesCompatibleIgnoreQualifiers( argType, function->parameters.front()->get_type(), indexer, env ) ) {
    14211421                                Cost cost = Cost::zero;
    14221422                                Expression * newExpr = data.combine( cost );
    1423                                 alternatives.push_back( Alternative{ 
    1424                                         new AttrExpr{ newExpr, argType->clone() }, env, OpenVarSet{}, 
     1423                                alternatives.push_back( Alternative{
     1424                                        new AttrExpr{ newExpr, argType->clone() }, env, OpenVarSet{},
    14251425                                        AssertionList{}, Cost::zero, cost } );
    14261426                                for ( DeclarationWithType * retVal : function->returnVals ) {
     
    14311431        }
    14321432
    1433         void AlternativeFinder::Finder::postvisit( AttrExpr *attrExpr ) {
     1433        void AlternativeFinder::Finder::postvisit( AttrExpr * attrExpr ) {
    14341434                // assume no 'pointer-to-attribute'
    1435                 NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
     1435                NameExpr * nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
    14361436                assert( nameExpr );
    14371437                std::list< SymTab::Indexer::IdData > attrList;
     
    14391439                if ( attrExpr->get_isType() || attrExpr->get_expr() ) {
    14401440                        for ( auto & data : attrList ) {
    1441                                 DeclarationWithType * id = data.id;
     1441                                const DeclarationWithType * id = data.id;
    14421442                                // check if the type is function
    1443                                 if ( FunctionType *function = dynamic_cast< FunctionType* >( id->get_type() ) ) {
     1443                                if ( const FunctionType * function = dynamic_cast< const FunctionType * >( id->get_type() ) ) {
    14441444                                        // assume exactly one parameter
    1445                                         if ( function->get_parameters().size() == 1 ) {
     1445                                        if ( function->parameters.size() == 1 ) {
    14461446                                                if ( attrExpr->get_isType() ) {
    14471447                                                        resolveAttr( data, function, attrExpr->get_type(), env, altFinder);
     
    14621462                                Cost cost = Cost::zero;
    14631463                                Expression * newExpr = data.combine( cost );
    1464                                 alternatives.push_back( Alternative{ 
     1464                                alternatives.push_back( Alternative{
    14651465                                        newExpr, env, OpenVarSet{}, AssertionList{}, Cost::zero, cost } );
    14661466                                renameTypes( alternatives.back().expr );
     
    14691469        }
    14701470
    1471         void AlternativeFinder::Finder::postvisit( LogicalExpr *logicalExpr ) {
     1471        void AlternativeFinder::Finder::postvisit( LogicalExpr * logicalExpr ) {
    14721472                AlternativeFinder firstFinder( indexer, env );
    14731473                firstFinder.findWithAdjustment( logicalExpr->get_arg1() );
     
    14861486                                cloneAll( second.need, need );
    14871487
    1488                                 LogicalExpr *newExpr = new LogicalExpr{ 
     1488                                LogicalExpr *newExpr = new LogicalExpr{
    14891489                                        first.expr->clone(), second.expr->clone(), logicalExpr->get_isAnd() };
    1490                                 alternatives.push_back( Alternative{ 
    1491                                         newExpr, std::move(compositeEnv), std::move(openVars), 
     1490                                alternatives.push_back( Alternative{
     1491                                        newExpr, std::move(compositeEnv), std::move(openVars),
    14921492                                        AssertionList( need.begin(), need.end() ), first.cost + second.cost } );
    14931493                        }
     
    15221522                                        cloneAll( third.need, need );
    15231523                                        AssertionSet have;
    1524                                        
     1524
    15251525                                        // unify true and false types, then infer parameters to produce new alternatives
    15261526                                        Type* commonType = nullptr;
    1527                                         if ( unify( second.expr->result, third.expr->result, compositeEnv, 
     1527                                        if ( unify( second.expr->result, third.expr->result, compositeEnv,
    15281528                                                        need, have, openVars, indexer, commonType ) ) {
    1529                                                 ConditionalExpr *newExpr = new ConditionalExpr{ 
     1529                                                ConditionalExpr *newExpr = new ConditionalExpr{
    15301530                                                        first.expr->clone(), second.expr->clone(), third.expr->clone() };
    15311531                                                newExpr->result = commonType ? commonType : second.expr->result->clone();
    15321532                                                // convert both options to the conditional result type
    15331533                                                Cost cost = first.cost + second.cost + third.cost;
    1534                                                 cost += computeExpressionConversionCost( 
     1534                                                cost += computeExpressionConversionCost(
    15351535                                                        newExpr->arg2, newExpr->result, indexer, compositeEnv );
    1536                                                 cost += computeExpressionConversionCost( 
     1536                                                cost += computeExpressionConversionCost(
    15371537                                                        newExpr->arg3, newExpr->result, indexer, compositeEnv );
    15381538                                                // output alternative
    1539                                                 Alternative newAlt{ 
    1540                                                         newExpr, std::move(compositeEnv), std::move(openVars), 
     1539                                                Alternative newAlt{
     1540                                                        newExpr, std::move(compositeEnv), std::move(openVars),
    15411541                                                        AssertionList( need.begin(), need.end() ), cost };
    15421542                                                inferParameters( newAlt, back_inserter( alternatives ) );
     
    15531553                secondFinder.findWithAdjustment( commaExpr->get_arg2() );
    15541554                for ( const Alternative & alt : secondFinder.alternatives ) {
    1555                         alternatives.push_back( Alternative{ 
     1555                        alternatives.push_back( Alternative{
    15561556                                alt, new CommaExpr{ newFirstArg->clone(), alt.expr->clone() }, alt.cost } );
    15571557                } // for
     
    15791579
    15801580                                Type* commonType = nullptr;
    1581                                 if ( unify( first.expr->result, second.expr->result, compositeEnv, need, have, 
     1581                                if ( unify( first.expr->result, second.expr->result, compositeEnv, need, have,
    15821582                                                openVars, indexer, commonType ) ) {
    1583                                         RangeExpr * newExpr = 
     1583                                        RangeExpr * newExpr =
    15841584                                                new RangeExpr{ first.expr->clone(), second.expr->clone() };
    15851585                                        newExpr->result = commonType ? commonType : first.expr->result->clone();
    1586                                         Alternative newAlt{ 
    1587                                                 newExpr, std::move(compositeEnv), std::move(openVars), 
     1586                                        Alternative newAlt{
     1587                                                newExpr, std::move(compositeEnv), std::move(openVars),
    15881588                                                AssertionList( need.begin(), need.end() ), first.cost + second.cost };
    15891589                                        inferParameters( newAlt, back_inserter( alternatives ) );
     
    16121612                                cloneAll( alt.need, need );
    16131613                        }
    1614                        
    1615                         alternatives.push_back( Alternative{ 
    1616                                 new TupleExpr{ exprs }, std::move(compositeEnv), std::move(openVars), 
     1614
     1615                        alternatives.push_back( Alternative{
     1616                                new TupleExpr{ exprs }, std::move(compositeEnv), std::move(openVars),
    16171617                                AssertionList( need.begin(), need.end() ), sumCost( alts ) } );
    16181618                } // for
     
    16331633                finder.findWithoutPrune( ctorExpr->get_callExpr() );
    16341634                for ( Alternative & alt : finder.alternatives ) {
    1635                         alternatives.push_back( Alternative{ 
     1635                        alternatives.push_back( Alternative{
    16361636                                alt, new ConstructorExpr( alt.expr->clone() ), alt.cost } );
    16371637                }
     
    16851685                                cloneAll( alt.need, need );
    16861686                                AssertionSet have;
    1687                                 OpenVarSet openVars( alt.openVars ); 
    1688                                 // xxx - find things in env that don't have a "representative type" and claim 
     1687                                OpenVarSet openVars( alt.openVars );
     1688                                // xxx - find things in env that don't have a "representative type" and claim
    16891689                                // those are open vars?
    16901690                                PRINT(
    16911691                                        std::cerr << "  @ " << toType << " " << initAlt.designation << std::endl;
    16921692                                )
    1693                                 // It's possible that a cast can throw away some values in a multiply-valued 
    1694                                 // expression. (An example is a cast-to-void, which casts from one value to 
    1695                                 // zero.)  Figure out the prefix of the subexpression results that are cast 
    1696                                 // directly.  The candidate is invalid if it has fewer results than there are 
     1693                                // It's possible that a cast can throw away some values in a multiply-valued
     1694                                // expression. (An example is a cast-to-void, which casts from one value to
     1695                                // zero.)  Figure out the prefix of the subexpression results that are cast
     1696                                // directly.  The candidate is invalid if it has fewer results than there are
    16971697                                // types to cast to.
    16981698                                int discardedValues = alt.expr->result->size() - toType->size();
    16991699                                if ( discardedValues < 0 ) continue;
    1700                                 // xxx - may need to go into tuple types and extract relevant types and use 
    1701                                 // unifyList. Note that currently, this does not allow casting a tuple to an 
     1700                                // xxx - may need to go into tuple types and extract relevant types and use
     1701                                // unifyList. Note that currently, this does not allow casting a tuple to an
    17021702                                // atomic type (e.g. (int)([1, 2, 3]))
    1703                                
     1703
    17041704                                // unification run for side-effects
    17051705                                unify( toType, alt.expr->result, newEnv, need, have, openVars, indexer );
     
    17101710                                        // count one safe conversion for each value that is thrown away
    17111711                                        thisCost.incSafe( discardedValues );
    1712                                         Alternative newAlt{ 
    1713                                                 new InitExpr{ 
    1714                                                         restructureCast( alt.expr->clone(), toType, true ), initAlt.designation->clone() }, 
    1715                                                 std::move(newEnv), std::move(openVars), 
     1712                                        Alternative newAlt{
     1713                                                new InitExpr{
     1714                                                        restructureCast( alt.expr->clone(), toType, true ), initAlt.designation->clone() },
     1715                                                std::move(newEnv), std::move(openVars),
    17161716                                                AssertionList( need.begin(), need.end() ), alt.cost, thisCost };
    17171717                                        inferParameters( newAlt, back_inserter( candidates ) );
Note: See TracChangeset for help on using the changeset viewer.