Ignore:
Timestamp:
Jan 22, 2018, 11:46:50 AM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
e5c74d1
Parents:
73367a8
git-author:
Rob Schluntz <rschlunt@…> (01/19/18 15:52:52)
git-committer:
Rob Schluntz <rschlunt@…> (01/22/18 11:46:50)
Message:

Convert AlternativeFinder? to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r73367a8 r13deae88  
    6060
    6161namespace ResolvExpr {
     62        struct AlternativeFinder::Finder : public WithShortCircuiting {
     63                Finder( AlternativeFinder & altFinder ) : altFinder( altFinder ), indexer( altFinder.indexer ), alternatives( altFinder.alternatives ), env( altFinder.env ), targetType( altFinder.targetType )  {}
     64
     65                void previsit( BaseSyntaxNode * ) { visit_children = false; }
     66
     67                void postvisit( ApplicationExpr * applicationExpr );
     68                void postvisit( UntypedExpr * untypedExpr );
     69                void postvisit( AddressExpr * addressExpr );
     70                void postvisit( LabelAddressExpr * labelExpr );
     71                void postvisit( CastExpr * castExpr );
     72                void postvisit( VirtualCastExpr * castExpr );
     73                void postvisit( UntypedMemberExpr * memberExpr );
     74                void postvisit( MemberExpr * memberExpr );
     75                void postvisit( NameExpr * variableExpr );
     76                void postvisit( VariableExpr * variableExpr );
     77                void postvisit( ConstantExpr * constantExpr );
     78                void postvisit( SizeofExpr * sizeofExpr );
     79                void postvisit( AlignofExpr * alignofExpr );
     80                void postvisit( UntypedOffsetofExpr * offsetofExpr );
     81                void postvisit( OffsetofExpr * offsetofExpr );
     82                void postvisit( OffsetPackExpr * offsetPackExpr );
     83                void postvisit( AttrExpr * attrExpr );
     84                void postvisit( LogicalExpr * logicalExpr );
     85                void postvisit( ConditionalExpr * conditionalExpr );
     86                void postvisit( CommaExpr * commaExpr );
     87                void postvisit( ImplicitCopyCtorExpr  * impCpCtorExpr );
     88                void postvisit( ConstructorExpr  * ctorExpr );
     89                void postvisit( RangeExpr  * rangeExpr );
     90                void postvisit( UntypedTupleExpr * tupleExpr );
     91                void postvisit( TupleExpr * tupleExpr );
     92                void postvisit( TupleIndexExpr * tupleExpr );
     93                void postvisit( TupleAssignExpr * tupleExpr );
     94                void postvisit( UniqueExpr * unqExpr );
     95                void postvisit( StmtExpr * stmtExpr );
     96                void postvisit( UntypedInitExpr * initExpr );
     97
     98                /// Adds alternatives for anonymous members
     99                void addAnonConversions( const Alternative & alt );
     100                /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
     101                template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
     102                /// Adds alternatives for member expressions where the left side has tuple type
     103                void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
     104                /// Adds alternatives for offsetof expressions, given the base type and name of the member
     105                template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
     106                /// Takes a final result and checks if its assertions can be satisfied
     107                template<typename OutputIterator>
     108                void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );
     109                /// Finds matching alternatives for a function, given a set of arguments
     110                template<typename OutputIterator>
     111                void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out );
     112                /// Checks if assertion parameters match for a new alternative
     113                template< typename OutputIterator >
     114                void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
     115        private:
     116                AlternativeFinder & altFinder;
     117                const SymTab::Indexer &indexer;
     118                AltList & alternatives;
     119                const TypeEnvironment &env;
     120                Type *& targetType;
     121        };
     122
    62123        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ) {
    63124                CastExpr *castToVoid = new CastExpr( expr );
     
    185246
    186247        void AlternativeFinder::find( Expression *expr, bool adjust, bool prune, bool failFast ) {
    187                 expr->accept( *this );
     248                PassVisitor<Finder> finder( *this );
     249                expr->accept( finder );
    188250                if ( failFast && alternatives.empty() ) {
    189251                        PRINT(
     
    244306        }
    245307
    246         void AlternativeFinder::addAnonConversions( const Alternative & alt ) {
     308        void AlternativeFinder::Finder::addAnonConversions( const Alternative & alt ) {
    247309                // adds anonymous member interpretations whenever an aggregate value type is seen.
    248310                // it's okay for the aggregate expression to have reference type -- cast it to the base type to treat the aggregate as the referenced value
     
    265327
    266328        template< typename StructOrUnionType >
    267         void AlternativeFinder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
     329        void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
    268330                // by this point, member must be a name expr
    269331                NameExpr * nameExpr = dynamic_cast< NameExpr * >( member );
     
    284346        }
    285347
    286         void AlternativeFinder::addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
     348        void AlternativeFinder::Finder::addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
    287349                if ( ConstantExpr * constantExpr = dynamic_cast< ConstantExpr * >( member ) ) {
    288350                        // get the value of the constant expression as an int, must be between 0 and the length of the tuple type to have meaning
     
    308370        }
    309371
    310         void AlternativeFinder::visit( ApplicationExpr *applicationExpr ) {
     372        void AlternativeFinder::Finder::postvisit( ApplicationExpr *applicationExpr ) {
    311373                alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) );
    312374        }
     
    541603
    542604        template< typename OutputIterator >
    543         void AlternativeFinder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
     605        void AlternativeFinder::Finder::inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ) {
    544606//      PRINT(
    545607//          std::cerr << "inferParameters: assertions needed are" << std::endl;
     
    892954
    893955        template<typename OutputIterator>
    894         void AlternativeFinder::validateFunctionAlternative( const Alternative &func, ArgPack& result,
     956        void AlternativeFinder::Finder::validateFunctionAlternative( const Alternative &func, ArgPack& result,
    895957                        const std::vector<ArgPack>& results, OutputIterator out ) {
    896958                ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
     
    915977
    916978        template<typename OutputIterator>
    917         void AlternativeFinder::makeFunctionAlternatives( const Alternative &func,
     979        void AlternativeFinder::Finder::makeFunctionAlternatives( const Alternative &func,
    918980                        FunctionType *funcType, const ExplodedArgs &args, OutputIterator out ) {
    919981                OpenVarSet funcOpenVars;
     
    10221084        }
    10231085
    1024         void AlternativeFinder::visit( UntypedExpr *untypedExpr ) {
     1086        void AlternativeFinder::Finder::postvisit( UntypedExpr *untypedExpr ) {
    10251087                AlternativeFinder funcFinder( indexer, env );
    10261088                funcFinder.findWithAdjustment( untypedExpr->get_function() );
     
    10291091
    10301092                std::vector< AlternativeFinder > argAlternatives;
    1031                 findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(),
     1093                altFinder.findSubExprs( untypedExpr->begin_args(), untypedExpr->end_args(),
    10321094                        back_inserter( argAlternatives ) );
    10331095
    10341096                // take care of possible tuple assignments
    10351097                // if not tuple assignment, assignment is taken care of as a normal function call
    1036                 Tuples::handleTupleAssignment( *this, untypedExpr, argAlternatives );
     1098                Tuples::handleTupleAssignment( altFinder, untypedExpr, argAlternatives );
    10371099
    10381100                // find function operators
     
    11721234                        // fix this issue in a more robust way.
    11731235                        targetType = nullptr;
    1174                         visit( untypedExpr );
     1236                        postvisit( untypedExpr );
    11751237                }
    11761238        }
     
    11811243        }
    11821244
    1183         void AlternativeFinder::visit( AddressExpr *addressExpr ) {
     1245        void AlternativeFinder::Finder::postvisit( AddressExpr *addressExpr ) {
    11841246                AlternativeFinder finder( indexer, env );
    11851247                finder.find( addressExpr->get_arg() );
     
    11921254        }
    11931255
    1194         void AlternativeFinder::visit( LabelAddressExpr * expr ) {
     1256        void AlternativeFinder::Finder::postvisit( LabelAddressExpr * expr ) {
    11951257                alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } );
    11961258        }
     
    12231285        }
    12241286
    1225         void AlternativeFinder::visit( CastExpr *castExpr ) {
     1287        void AlternativeFinder::Finder::postvisit( CastExpr *castExpr ) {
    12261288                Type *& toType = castExpr->get_result();
    12271289                assert( toType );
     
    12781340        }
    12791341
    1280         void AlternativeFinder::visit( VirtualCastExpr * castExpr ) {
     1342        void AlternativeFinder::Finder::postvisit( VirtualCastExpr * castExpr ) {
    12811343                assertf( castExpr->get_result(), "Implicate virtual cast targets not yet supported." );
    12821344                AlternativeFinder finder( indexer, env );
     
    12901352        }
    12911353
    1292         void AlternativeFinder::visit( UntypedMemberExpr *memberExpr ) {
     1354        void AlternativeFinder::Finder::postvisit( UntypedMemberExpr *memberExpr ) {
    12931355                AlternativeFinder funcFinder( indexer, env );
    12941356                funcFinder.findWithAdjustment( memberExpr->get_aggregate() );
     
    13121374        }
    13131375
    1314         void AlternativeFinder::visit( MemberExpr *memberExpr ) {
     1376        void AlternativeFinder::Finder::postvisit( MemberExpr *memberExpr ) {
    13151377                alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) );
    13161378        }
    13171379
    1318         void AlternativeFinder::visit( NameExpr *nameExpr ) {
     1380        void AlternativeFinder::Finder::postvisit( NameExpr *nameExpr ) {
    13191381                std::list< SymTab::Indexer::IdData > declList;
    13201382                indexer.lookupId( nameExpr->get_name(), declList );
     
    13371399        }
    13381400
    1339         void AlternativeFinder::visit( VariableExpr *variableExpr ) {
     1401        void AlternativeFinder::Finder::postvisit( VariableExpr *variableExpr ) {
    13401402                // not sufficient to clone here, because variable's type may have changed
    13411403                // since the VariableExpr was originally created.
     
    13431405        }
    13441406
    1345         void AlternativeFinder::visit( ConstantExpr *constantExpr ) {
     1407        void AlternativeFinder::Finder::postvisit( ConstantExpr *constantExpr ) {
    13461408                alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) );
    13471409        }
    13481410
    1349         void AlternativeFinder::visit( SizeofExpr *sizeofExpr ) {
     1411        void AlternativeFinder::Finder::postvisit( SizeofExpr *sizeofExpr ) {
    13501412                if ( sizeofExpr->get_isType() ) {
    13511413                        Type * newType = sizeofExpr->get_type()->clone();
     
    13681430        }
    13691431
    1370         void AlternativeFinder::visit( AlignofExpr *alignofExpr ) {
     1432        void AlternativeFinder::Finder::postvisit( AlignofExpr *alignofExpr ) {
    13711433                if ( alignofExpr->get_isType() ) {
    13721434                        Type * newType = alignofExpr->get_type()->clone();
     
    13901452
    13911453        template< typename StructOrUnionType >
    1392         void AlternativeFinder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {
     1454        void AlternativeFinder::Finder::addOffsetof( StructOrUnionType *aggInst, const std::string &name ) {
    13931455                std::list< Declaration* > members;
    13941456                aggInst->lookup( name, members );
     
    14031465        }
    14041466
    1405         void AlternativeFinder::visit( UntypedOffsetofExpr *offsetofExpr ) {
     1467        void AlternativeFinder::Finder::postvisit( UntypedOffsetofExpr *offsetofExpr ) {
    14061468                AlternativeFinder funcFinder( indexer, env );
    14071469                // xxx - resolveTypeof?
     
    14131475        }
    14141476
    1415         void AlternativeFinder::visit( OffsetofExpr *offsetofExpr ) {
     1477        void AlternativeFinder::Finder::postvisit( OffsetofExpr *offsetofExpr ) {
    14161478                alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
    14171479        }
    14181480
    1419         void AlternativeFinder::visit( OffsetPackExpr *offsetPackExpr ) {
     1481        void AlternativeFinder::Finder::postvisit( OffsetPackExpr *offsetPackExpr ) {
    14201482                alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) );
    14211483        }
     
    14441506        }
    14451507
    1446         void AlternativeFinder::visit( AttrExpr *attrExpr ) {
     1508        void AlternativeFinder::Finder::postvisit( AttrExpr *attrExpr ) {
    14471509                // assume no 'pointer-to-attribute'
    14481510                NameExpr *nameExpr = dynamic_cast< NameExpr* >( attrExpr->get_attr() );
     
    14581520                                        if ( function->get_parameters().size() == 1 ) {
    14591521                                                if ( attrExpr->get_isType() ) {
    1460                                                         resolveAttr( data, function, attrExpr->get_type(), env, *this );
     1522                                                        resolveAttr( data, function, attrExpr->get_type(), env, altFinder);
    14611523                                                } else {
    14621524                                                        AlternativeFinder finder( indexer, env );
     
    14641526                                                        for ( AltList::iterator choice = finder.alternatives.begin(); choice != finder.alternatives.end(); ++choice ) {
    14651527                                                                if ( choice->expr->get_result()->size() == 1 ) {
    1466                                                                         resolveAttr(data, function, choice->expr->get_result(), choice->env, *this );
     1528                                                                        resolveAttr(data, function, choice->expr->get_result(), choice->env, altFinder );
    14671529                                                                } // fi
    14681530                                                        } // for
     
    14791541        }
    14801542
    1481         void AlternativeFinder::visit( LogicalExpr *logicalExpr ) {
     1543        void AlternativeFinder::Finder::postvisit( LogicalExpr *logicalExpr ) {
    14821544                AlternativeFinder firstFinder( indexer, env );
    14831545                firstFinder.findWithAdjustment( logicalExpr->get_arg1() );
     
    14921554        }
    14931555
    1494         void AlternativeFinder::visit( ConditionalExpr *conditionalExpr ) {
     1556        void AlternativeFinder::Finder::postvisit( ConditionalExpr *conditionalExpr ) {
    14951557                // find alternatives for condition
    14961558                AlternativeFinder firstFinder( indexer, env );
     
    15241586        }
    15251587
    1526         void AlternativeFinder::visit( CommaExpr *commaExpr ) {
     1588        void AlternativeFinder::Finder::postvisit( CommaExpr *commaExpr ) {
    15271589                TypeEnvironment newEnv( env );
    15281590                Expression *newFirstArg = resolveInVoidContext( commaExpr->get_arg1(), indexer, newEnv );
     
    15351597        }
    15361598
    1537         void AlternativeFinder::visit( RangeExpr * rangeExpr ) {
     1599        void AlternativeFinder::Finder::postvisit( RangeExpr * rangeExpr ) {
    15381600                // resolve low and high, accept alternatives whose low and high types unify
    15391601                AlternativeFinder firstFinder( indexer, env );
     
    15571619        }
    15581620
    1559         void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) {
     1621        void AlternativeFinder::Finder::postvisit( UntypedTupleExpr *tupleExpr ) {
    15601622                std::vector< AlternativeFinder > subExprAlternatives;
    1561                 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(),
     1623                altFinder.findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(),
    15621624                        back_inserter( subExprAlternatives ) );
    15631625                std::vector< AltList > possibilities;
     
    15751637        }
    15761638
    1577         void AlternativeFinder::visit( TupleExpr *tupleExpr ) {
     1639        void AlternativeFinder::Finder::postvisit( TupleExpr *tupleExpr ) {
    15781640                alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) );
    15791641        }
    15801642
    1581         void AlternativeFinder::visit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
     1643        void AlternativeFinder::Finder::postvisit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
    15821644                alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) );
    15831645        }
    15841646
    1585         void AlternativeFinder::visit( ConstructorExpr * ctorExpr ) {
     1647        void AlternativeFinder::Finder::postvisit( ConstructorExpr * ctorExpr ) {
    15861648                AlternativeFinder finder( indexer, env );
    15871649                // don't prune here, since it's guaranteed all alternatives will have the same type
     
    15931655        }
    15941656
    1595         void AlternativeFinder::visit( TupleIndexExpr *tupleExpr ) {
     1657        void AlternativeFinder::Finder::postvisit( TupleIndexExpr *tupleExpr ) {
    15961658                alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) );
    15971659        }
    15981660
    1599         void AlternativeFinder::visit( TupleAssignExpr *tupleAssignExpr ) {
     1661        void AlternativeFinder::Finder::postvisit( TupleAssignExpr *tupleAssignExpr ) {
    16001662                alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) );
    16011663        }
    16021664
    1603         void AlternativeFinder::visit( UniqueExpr *unqExpr ) {
     1665        void AlternativeFinder::Finder::postvisit( UniqueExpr *unqExpr ) {
    16041666                AlternativeFinder finder( indexer, env );
    16051667                finder.findWithAdjustment( unqExpr->get_expr() );
     
    16111673        }
    16121674
    1613         void AlternativeFinder::visit( StmtExpr *stmtExpr ) {
     1675        void AlternativeFinder::Finder::postvisit( StmtExpr *stmtExpr ) {
    16141676                StmtExpr * newStmtExpr = stmtExpr->clone();
    16151677                ResolvExpr::resolveStmtExpr( newStmtExpr, indexer );
     
    16181680        }
    16191681
    1620         void AlternativeFinder::visit( UntypedInitExpr *initExpr ) {
     1682        void AlternativeFinder::Finder::postvisit( UntypedInitExpr *initExpr ) {
    16211683                // handle each option like a cast
    16221684                AltList candidates;
    1623                 PRINT( std::cerr << "untyped init expr: " << initExpr << std::endl; )
     1685                PRINT(
     1686                        std::cerr << "untyped init expr: " << initExpr << std::endl;
     1687                )
    16241688                // O(N^2) checks of d-types with e-types
    16251689                for ( InitAlternative & initAlt : initExpr->get_initAlts() ) {
     
    16371701                                AssertionSet needAssertions, haveAssertions;
    16381702                                OpenVarSet openVars;  // find things in env that don't have a "representative type" and claim those are open vars?
    1639                                 PRINT( std::cerr << "  @ " << toType << " " << initAlt.designation << std::endl; )
     1703                                PRINT(
     1704                                        std::cerr << "  @ " << toType << " " << initAlt.designation << std::endl;
     1705                                 )
    16401706                                // It's possible that a cast can throw away some values in a multiply-valued expression.  (An example is a
    16411707                                // cast-to-void, which casts from one value to zero.)  Figure out the prefix of the subexpression results
Note: See TracChangeset for help on using the changeset viewer.