Changeset 81e1f32b for src/ResolvExpr


Ignore:
Timestamp:
Jan 23, 2018, 10:49:53 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
15d248e, f7d6bb0
Parents:
d2d50d7 (diff), fc67d6f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/ResolvExpr
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    rd2d50d7 r81e1f32b  
    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 );
     
    152213
    153214                void renameTypes( Expression *expr ) {
    154                         expr->get_result()->accept( global_renamer );
     215                        renameTyVars( expr->result );
    155216                }
    156217        } // namespace
     
    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        }
     
    485547                        Type *adjType = candidate->get_type()->clone();
    486548                        adjustExprType( adjType, newEnv, indexer );
    487                         adjType->accept( global_renamer );
     549                        renameTyVars( adjType );
    488550                        PRINT(
    489551                                std::cerr << "unifying ";
     
    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;
     
    595657
    596658                ArgPack()
    597                         : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0), 
     659                        : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0),
    598660                          tupleStart(0), nextExpl(0), explAlt(0) {}
    599661
     
    706768
    707769                                                if ( nTuples > 0 || ! results[i].expr ) {
    708                                                         // first iteration or no expression to clone, 
     770                                                        // first iteration or no expression to clone,
    709771                                                        // push empty tuple expression
    710772                                                        newResult.parent = i;
     
    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
  • src/ResolvExpr/AlternativeFinder.h

    rd2d50d7 r81e1f32b  
    3838        using ExplodedArgs = std::vector< std::vector< ExplodedActual > >;
    3939
    40         class AlternativeFinder : public Visitor {
     40        class AlternativeFinder {
    4141          public:
    4242                AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
     
    9494                void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
    9595          private:
    96                 virtual void visit( ApplicationExpr *applicationExpr );
    97                 virtual void visit( UntypedExpr *untypedExpr );
    98                 virtual void visit( AddressExpr *addressExpr );
    99                 virtual void visit( LabelAddressExpr *labelExpr );
    100                 virtual void visit( CastExpr *castExpr );
    101                 virtual void visit( VirtualCastExpr *castExpr );
    102                 virtual void visit( UntypedMemberExpr *memberExpr );
    103                 virtual void visit( MemberExpr *memberExpr );
    104                 virtual void visit( NameExpr *variableExpr );
    105                 virtual void visit( VariableExpr *variableExpr );
    106                 virtual void visit( ConstantExpr *constantExpr );
    107                 virtual void visit( SizeofExpr *sizeofExpr );
    108                 virtual void visit( AlignofExpr *alignofExpr );
    109                 virtual void visit( UntypedOffsetofExpr *offsetofExpr );
    110                 virtual void visit( OffsetofExpr *offsetofExpr );
    111                 virtual void visit( OffsetPackExpr *offsetPackExpr );
    112                 virtual void visit( AttrExpr *attrExpr );
    113                 virtual void visit( LogicalExpr *logicalExpr );
    114                 virtual void visit( ConditionalExpr *conditionalExpr );
    115                 virtual void visit( CommaExpr *commaExpr );
    116                 virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
    117                 virtual void visit( ConstructorExpr * ctorExpr );
    118                 virtual void visit( RangeExpr * rangeExpr );
    119                 virtual void visit( UntypedTupleExpr *tupleExpr );
    120                 virtual void visit( TupleExpr *tupleExpr );
    121                 virtual void visit( TupleIndexExpr *tupleExpr );
    122                 virtual void visit( TupleAssignExpr *tupleExpr );
    123                 virtual void visit( UniqueExpr *unqExpr );
    124                 virtual void visit( StmtExpr *stmtExpr );
    125                 virtual void visit( UntypedInitExpr *initExpr );
    126 
    127                 /// Adds alternatives for anonymous members
    128                 void addAnonConversions( const Alternative & alt );
    129                 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
    130                 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
    131                 /// Adds alternatives for member expressions where the left side has tuple type
    132                 void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
    133                 /// Adds alternatives for offsetof expressions, given the base type and name of the member
    134                 template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
    135                 /// Takes a final result and checks if its assertions can be satisfied
    136                 template<typename OutputIterator>
    137                 void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );
    138                 /// Finds matching alternatives for a function, given a set of arguments
    139                 template<typename OutputIterator>
    140                 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out );
    141                 /// Checks if assertion parameters match for a new alternative
    142                 template< typename OutputIterator >
    143                 void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
    144 
     96                struct Finder;
    14597                const SymTab::Indexer &indexer;
    14698                AltList alternatives;
  • src/ResolvExpr/CommonType.cc

    rd2d50d7 r81e1f32b  
    1818#include <utility>                       // for pair
    1919
     20#include "Common/PassVisitor.h"
    2021#include "ResolvExpr/TypeEnvironment.h"  // for OpenVarSet, AssertionSet
    2122#include "SymTab/Indexer.h"              // for Indexer
     
    2930
    3031namespace ResolvExpr {
    31         class CommonType : public Visitor {
    32           public:
     32        struct CommonType : public WithShortCircuiting {
    3333                CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
    3434                Type *get_result() const { return result; }
     35
     36                void previsit( BaseSyntaxNode * ) { visit_children = false; }
     37
     38                void postvisit( VoidType * voidType );
     39                void postvisit( BasicType * basicType );
     40                void postvisit( PointerType * pointerType );
     41                void postvisit( ArrayType * arrayType );
     42                void postvisit( ReferenceType * refType );
     43                void postvisit( FunctionType * functionType );
     44                void postvisit( StructInstType * aggregateUseType );
     45                void postvisit( UnionInstType * aggregateUseType );
     46                void postvisit( EnumInstType * aggregateUseType );
     47                void postvisit( TraitInstType * aggregateUseType );
     48                void postvisit( TypeInstType * aggregateUseType );
     49                void postvisit( TupleType * tupleType );
     50                void postvisit( VarArgsType * varArgsType );
     51                void postvisit( ZeroType * zeroType );
     52                void postvisit( OneType * oneType );
     53
    3554          private:
    36                 virtual void visit( VoidType *voidType );
    37                 virtual void visit( BasicType *basicType );
    38                 virtual void visit( PointerType *pointerType );
    39                 virtual void visit( ArrayType *arrayType );
    40                 virtual void visit( ReferenceType *refType );
    41                 virtual void visit( FunctionType *functionType );
    42                 virtual void visit( StructInstType *aggregateUseType );
    43                 virtual void visit( UnionInstType *aggregateUseType );
    44                 virtual void visit( EnumInstType *aggregateUseType );
    45                 virtual void visit( TraitInstType *aggregateUseType );
    46                 virtual void visit( TypeInstType *aggregateUseType );
    47                 virtual void visit( TupleType *tupleType );
    48                 virtual void visit( VarArgsType *varArgsType );
    49                 virtual void visit( ZeroType *zeroType );
    50                 virtual void visit( OneType *oneType );
    51 
    5255                template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer );
    5356                template< typename RefType > void handleRefType( RefType *inst, Type *other );
     
    8083
    8184        Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) {
    82                 CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars );
     85                PassVisitor<CommonType> visitor( type2, widenFirst, widenSecond, indexer, env, openVars );
    8386
    8487                int depth1 = type1->referenceDepth();
     
    116119
    117120                type1->accept( visitor );
    118                 Type *result = visitor.get_result();
     121                Type *result = visitor.pass.get_result();
    119122                if ( ! result ) {
    120123                        // this appears to be handling for opaque type declarations
     
    188191        }
    189192
    190         void CommonType::visit( VoidType * ) {}
    191 
    192         void CommonType::visit( BasicType *basicType ) {
     193        void CommonType::postvisit( VoidType * ) {}
     194
     195        void CommonType::postvisit( BasicType *basicType ) {
    193196                if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
    194197                        BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ];
     
    219222        }
    220223
    221         void CommonType::visit( PointerType *pointerType ) {
     224        void CommonType::postvisit( PointerType *pointerType ) {
    222225                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
    223226                        // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl;
     
    254257        }
    255258
    256         void CommonType::visit( ArrayType * ) {}
    257 
    258         void CommonType::visit( ReferenceType *refType ) {
     259        void CommonType::postvisit( ArrayType * ) {}
     260
     261        void CommonType::postvisit( ReferenceType *refType ) {
    259262                if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
    260263                        // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl;
     
    291294        }
    292295
    293         void CommonType::visit( FunctionType * ) {}
    294         void CommonType::visit( StructInstType * ) {}
    295         void CommonType::visit( UnionInstType * ) {}
    296 
    297         void CommonType::visit( EnumInstType *enumInstType ) {
     296        void CommonType::postvisit( FunctionType * ) {}
     297        void CommonType::postvisit( StructInstType * ) {}
     298        void CommonType::postvisit( UnionInstType * ) {}
     299
     300        void CommonType::postvisit( EnumInstType *enumInstType ) {
    298301                if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
    299302                        // reuse BasicType, EnumInstType code by swapping type2 with enumInstType
    300                         ValueGuard< Type * > temp( type2 );
    301                         type2 = enumInstType;
    302                         temp.old->accept( *this );
    303                 } // if
    304         }
    305 
    306         void CommonType::visit( TraitInstType * ) {
    307         }
    308 
    309         void CommonType::visit( TypeInstType *inst ) {
     303                        result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars );
     304                } // if
     305        }
     306
     307        void CommonType::postvisit( TraitInstType * ) {
     308        }
     309
     310        void CommonType::postvisit( TypeInstType *inst ) {
    310311                if ( widenFirst ) {
    311312                        NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
     
    329330        }
    330331
    331         void CommonType::visit( TupleType * ) {}
    332         void CommonType::visit( VarArgsType * ) {}
    333 
    334         void CommonType::visit( ZeroType *zeroType ) {
     332        void CommonType::postvisit( TupleType * ) {}
     333        void CommonType::postvisit( VarArgsType * ) {}
     334
     335        void CommonType::postvisit( ZeroType *zeroType ) {
    335336                if ( widenFirst ) {
    336337                        if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
     
    346347        }
    347348
    348         void CommonType::visit( OneType *oneType ) {
     349        void CommonType::postvisit( OneType *oneType ) {
    349350                if ( widenFirst ) {
    350351                        if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
  • src/ResolvExpr/CurrentObject.cc

    rd2d50d7 r81e1f32b  
    443443                                return new UnionIterator( uit );
    444444                        } else {
    445                                 assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" );
     445                                assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str() );
    446446                                return new SimpleIterator( type );
    447447                        }
  • src/ResolvExpr/PtrsAssignable.cc

    rd2d50d7 r81e1f32b  
    1414//
    1515
     16#include "Common/PassVisitor.h"
    1617#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
    1718#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
     
    2021
    2122namespace ResolvExpr {
    22         class PtrsAssignable : public Visitor {
    23           public:
     23        struct PtrsAssignable : public WithShortCircuiting {
    2424                PtrsAssignable( Type *dest, const TypeEnvironment &env );
    2525
    2626                int get_result() const { return result; }
    2727
    28                 virtual void visit( VoidType *voidType );
    29                 virtual void visit( BasicType *basicType );
    30                 virtual void visit( PointerType *pointerType );
    31                 virtual void visit( ArrayType *arrayType );
    32                 virtual void visit( FunctionType *functionType );
    33                 virtual void visit( StructInstType *inst );
    34                 virtual void visit( UnionInstType *inst );
    35                 virtual void visit( EnumInstType *inst );
    36                 virtual void visit( TraitInstType *inst );
    37                 virtual void visit( TypeInstType *inst );
    38                 virtual void visit( TupleType *tupleType );
    39                 virtual void visit( VarArgsType *varArgsType );
    40                 virtual void visit( ZeroType *zeroType );
    41                 virtual void visit( OneType *oneType );
     28                void previsit( Type * ) { visit_children = false; }
     29
     30                void postvisit( VoidType * voidType );
     31                void postvisit( BasicType * basicType );
     32                void postvisit( PointerType * pointerType );
     33                void postvisit( ArrayType * arrayType );
     34                void postvisit( FunctionType * functionType );
     35                void postvisit( StructInstType * inst );
     36                void postvisit( UnionInstType * inst );
     37                void postvisit( EnumInstType * inst );
     38                void postvisit( TraitInstType * inst );
     39                void postvisit( TypeInstType * inst );
     40                void postvisit( TupleType * tupleType );
     41                void postvisit( VarArgsType * varArgsType );
     42                void postvisit( ZeroType * zeroType );
     43                void postvisit( OneType * oneType );
    4244          private:
    4345                Type *dest;
     
    5961                        return -1;
    6062                } else {
    61                         PtrsAssignable ptrs( dest, env );
     63                        PassVisitor<PtrsAssignable> ptrs( dest, env );
    6264                        src->accept( ptrs );
    63                         return ptrs.get_result();
     65                        return ptrs.pass.get_result();
    6466                } // if
    6567        }
     
    6769        PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}
    6870
    69         void PtrsAssignable::visit( VoidType * ) {
     71        void PtrsAssignable::postvisit( VoidType * ) {
    7072                // T * = void * is disallowed - this is a change from C, where any
    7173                // void * can be assigned or passed to a non-void pointer without a cast.
    7274        }
    7375
    74         void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {}
    75         void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {}
    76         void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {}
    77         void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {}
     76        void PtrsAssignable::postvisit( __attribute__((unused)) BasicType *basicType ) {}
     77        void PtrsAssignable::postvisit( __attribute__((unused)) PointerType *pointerType ) {}
     78        void PtrsAssignable::postvisit( __attribute__((unused)) ArrayType *arrayType ) {}
     79        void PtrsAssignable::postvisit( __attribute__((unused)) FunctionType *functionType ) {}
    7880
    79         void PtrsAssignable::visit(  __attribute__((unused)) StructInstType *inst ) {}
    80         void PtrsAssignable::visit(  __attribute__((unused)) UnionInstType *inst ) {}
     81        void PtrsAssignable::postvisit(  __attribute__((unused)) StructInstType *inst ) {}
     82        void PtrsAssignable::postvisit(  __attribute__((unused)) UnionInstType *inst ) {}
    8183
    82         void PtrsAssignable::visit( EnumInstType * ) {
     84        void PtrsAssignable::postvisit( EnumInstType * ) {
    8385                if ( dynamic_cast< BasicType* >( dest ) ) {
    8486                        // int * = E *, etc. is safe. This isn't technically correct, as each
     
    9193        }
    9294
    93         void PtrsAssignable::visit(  __attribute__((unused)) TraitInstType *inst ) {}
    94         void PtrsAssignable::visit( TypeInstType *inst ) {
     95        void PtrsAssignable::postvisit(  __attribute__((unused)) TraitInstType *inst ) {}
     96        void PtrsAssignable::postvisit( TypeInstType *inst ) {
    9597                EqvClass eqvClass;
    9698                if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) {
     
    100102        }
    101103
    102         void PtrsAssignable::visit(  __attribute__((unused)) TupleType *tupleType ) {}
    103         void PtrsAssignable::visit(  __attribute__((unused)) VarArgsType *varArgsType ) {}
    104         void PtrsAssignable::visit(  __attribute__((unused)) ZeroType *zeroType ) {}
    105         void PtrsAssignable::visit(  __attribute__((unused)) OneType *oneType ) {}
     104        void PtrsAssignable::postvisit(  __attribute__((unused)) TupleType *tupleType ) {}
     105        void PtrsAssignable::postvisit(  __attribute__((unused)) VarArgsType *varArgsType ) {}
     106        void PtrsAssignable::postvisit(  __attribute__((unused)) ZeroType *zeroType ) {}
     107        void PtrsAssignable::postvisit(  __attribute__((unused)) OneType *oneType ) {}
    106108
    107109} // namespace ResolvExpr
  • src/ResolvExpr/PtrsCastable.cc

    rd2d50d7 r81e1f32b  
    1414//
    1515
     16#include "Common/PassVisitor.h"
    1617#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
    1718#include "SymTab/Indexer.h"              // for Indexer
     
    2122#include "typeops.h"                     // for ptrsAssignable
    2223
    23 
    2424namespace ResolvExpr {
    25         class PtrsCastable : public Visitor {
     25        struct PtrsCastable : public WithShortCircuiting {
    2626          public:
    2727                PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
     
    2929                int get_result() const { return result; }
    3030
    31                 virtual void visit(VoidType *voidType);
    32                 virtual void visit(BasicType *basicType);
    33                 virtual void visit(PointerType *pointerType);
    34                 virtual void visit(ArrayType *arrayType);
    35                 virtual void visit(FunctionType *functionType);
    36                 virtual void visit(StructInstType *inst);
    37                 virtual void visit(UnionInstType *inst);
    38                 virtual void visit(EnumInstType *inst);
    39                 virtual void visit(TraitInstType *inst);
    40                 virtual void visit(TypeInstType *inst);
    41                 virtual void visit(TupleType *tupleType);
    42                 virtual void visit(VarArgsType *varArgsType);
    43                 virtual void visit(ZeroType *zeroType);
    44                 virtual void visit(OneType *oneType);
     31                void previsit( Type * ) { visit_children = false; }
     32
     33                void postvisit( VoidType * voidType );
     34                void postvisit( BasicType * basicType );
     35                void postvisit( PointerType * pointerType );
     36                void postvisit( ArrayType * arrayType );
     37                void postvisit( FunctionType * functionType );
     38                void postvisit( StructInstType * inst );
     39                void postvisit( UnionInstType * inst );
     40                void postvisit( EnumInstType * inst );
     41                void postvisit( TraitInstType * inst );
     42                void postvisit( TypeInstType * inst );
     43                void postvisit( TupleType * tupleType );
     44                void postvisit( VarArgsType * varArgsType );
     45                void postvisit( ZeroType * zeroType );
     46                void postvisit( OneType * oneType );
    4547          private:
    4648                Type *dest;
     
    7981                        EqvClass eqvClass;
    8082                        if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
     83                                // xxx - should this be ptrsCastable?
    8184                                return ptrsAssignable( src, eqvClass.type, env );
    8285                        } // if
     
    8588                        return objectCast( src, env, indexer );
    8689                } else {
    87                         PtrsCastable ptrs( dest, env, indexer );
     90                        PassVisitor<PtrsCastable> ptrs( dest, env, indexer );
    8891                        src->accept( ptrs );
    89                         return ptrs.get_result();
     92                        return ptrs.pass.get_result();
    9093                } // if
    9194        }
     
    9598        }
    9699
    97         void PtrsCastable::visit( VoidType * ) {
     100        void PtrsCastable::postvisit( VoidType * ) {
    98101                result = objectCast( dest, env, indexer );
    99102        }
    100103
    101         void PtrsCastable::visit( BasicType * ) {
     104        void PtrsCastable::postvisit( BasicType * ) {
    102105                result = objectCast( dest, env, indexer );
    103106        }
    104107
    105         void PtrsCastable::visit( PointerType * ) {
     108        void PtrsCastable::postvisit( PointerType * ) {
    106109                result = objectCast( dest, env, indexer );
    107110        }
    108111
    109         void PtrsCastable::visit( ArrayType * ) {
     112        void PtrsCastable::postvisit( ArrayType * ) {
    110113                result = objectCast( dest, env, indexer );
    111114        }
    112115
    113         void PtrsCastable::visit( FunctionType * ) {
     116        void PtrsCastable::postvisit( FunctionType * ) {
    114117                // result = -1;
    115118                result = functionCast( dest, env, indexer );
    116119        }
    117120
    118         void PtrsCastable::visit( StructInstType * ) {
     121        void PtrsCastable::postvisit( StructInstType * ) {
    119122                result = objectCast( dest, env, indexer );
    120123        }
    121124
    122         void PtrsCastable::visit( UnionInstType * ) {
     125        void PtrsCastable::postvisit( UnionInstType * ) {
    123126                result = objectCast( dest, env, indexer );
    124127        }
    125128
    126         void PtrsCastable::visit( EnumInstType * ) {
     129        void PtrsCastable::postvisit( EnumInstType * ) {
    127130                if ( dynamic_cast< EnumInstType* >( dest ) ) {
    128131                        result = 1;
     
    138141        }
    139142
    140         void PtrsCastable::visit( TraitInstType * ) {}
     143        void PtrsCastable::postvisit( TraitInstType * ) {}
    141144
    142         void PtrsCastable::visit(TypeInstType *inst) {
     145        void PtrsCastable::postvisit(TypeInstType *inst) {
    143146                //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
    144147                result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
    145148        }
    146149
    147         void PtrsCastable::visit( TupleType * ) {
     150        void PtrsCastable::postvisit( TupleType * ) {
    148151                result = objectCast( dest, env, indexer );
    149152        }
    150153
    151         void PtrsCastable::visit( VarArgsType * ) {
     154        void PtrsCastable::postvisit( VarArgsType * ) {
    152155                result = objectCast( dest, env, indexer );
    153156        }
    154157
    155         void PtrsCastable::visit( ZeroType * ) {
     158        void PtrsCastable::postvisit( ZeroType * ) {
    156159                result = objectCast( dest, env, indexer );
    157160        }
    158161
    159         void PtrsCastable::visit( OneType * ) {
     162        void PtrsCastable::postvisit( OneType * ) {
    160163                result = objectCast( dest, env, indexer );
    161164        }
  • src/ResolvExpr/RenameVars.cc

    rd2d50d7 r81e1f32b  
    1919#include <utility>                 // for pair
    2020
     21#include "Common/PassVisitor.h"
    2122#include "Common/SemanticError.h"  // for SemanticError
    2223#include "RenameVars.h"
     
    2728
    2829namespace ResolvExpr {
    29         RenameVars global_renamer;
     30        namespace {
     31                struct RenameVars {
     32                        RenameVars();
     33                        void reset();
    3034
    31         RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
    32                 mapStack.push_front( std::map< std::string, std::string >() );
     35                        void previsit( TypeInstType * instType );
     36                        void previsit( Type * );
     37                        void postvisit( Type * );
     38
     39                  private:
     40                        int level, resetCount;
     41                        std::list< std::map< std::string, std::string > > mapStack;
     42                };
     43
     44                PassVisitor<RenameVars> global_renamer;
     45        } // namespace
     46
     47        void renameTyVars( Type * t ) {
     48                t->accept( global_renamer );
    3349        }
    3450
    35         void RenameVars::reset() {
    36                 level = 0;
    37                 resetCount++;
     51        void resetTyVarRenaming() {
     52                global_renamer.pass.reset();
    3853        }
    3954
    40         void RenameVars::visit( VoidType *voidType ) {
    41                 typeBefore( voidType );
    42                 typeAfter( voidType );
    43         }
     55        namespace {
     56                RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
     57                        mapStack.push_front( std::map< std::string, std::string >() );
     58                }
    4459
    45         void RenameVars::visit( BasicType *basicType ) {
    46                 typeBefore( basicType );
    47                 typeAfter( basicType );
    48         }
     60                void RenameVars::reset() {
     61                        level = 0;
     62                        resetCount++;
     63                }
    4964
    50         void RenameVars::visit( PointerType *pointerType ) {
    51                 typeBefore( pointerType );
    52                 maybeAccept( pointerType->get_base(), *this );
    53                 typeAfter( pointerType );
    54         }
     65                void RenameVars::previsit( TypeInstType * instType ) {
     66                        previsit( (Type *)instType );
     67                        std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->name );
     68                        if ( i != mapStack.front().end() ) {
     69                                instType->name = i->second;
     70                        } // if
     71                }
    5572
    56         void RenameVars::visit( ArrayType *arrayType ) {
    57                 typeBefore( arrayType );
    58                 maybeAccept( arrayType->get_dimension(), *this );
    59                 maybeAccept( arrayType->get_base(), *this );
    60                 typeAfter( arrayType );
    61         }
     73                void RenameVars::previsit( Type * type ) {
     74                        if ( ! type->forall.empty() ) {
     75                                // copies current name mapping into new mapping
     76                                mapStack.push_front( mapStack.front() );
     77                                // renames all "forall" type names to `_${level}_${name}'
     78                                for ( auto td : type->forall ) {
     79                                        std::ostringstream output;
     80                                        output << "_" << resetCount << "_" << level << "_" << td->name;
     81                                        std::string newname( output.str() );
     82                                        mapStack.front()[ td->get_name() ] = newname;
     83                                        td->name = newname;
     84                                        // ditto for assertion names, the next level in
     85                                        level++;
     86                                        // acceptAll( td->assertions, *this );
     87                                } // for
     88                        } // if
     89                }
    6290
    63         void RenameVars::visit( FunctionType *functionType ) {
    64                 typeBefore( functionType );
    65                 acceptAll( functionType->get_returnVals(), *this );
    66                 acceptAll( functionType->get_parameters(), *this );
    67                 typeAfter( functionType );
    68         }
    69 
    70         void RenameVars::visit( StructInstType *aggregateUseType ) {
    71                 typeBefore( aggregateUseType );
    72                 acceptAll( aggregateUseType->get_parameters(), *this );
    73                 typeAfter( aggregateUseType );
    74         }
    75 
    76         void RenameVars::visit( UnionInstType *aggregateUseType ) {
    77                 typeBefore( aggregateUseType );
    78                 acceptAll( aggregateUseType->get_parameters(), *this );
    79                 typeAfter( aggregateUseType );
    80         }
    81 
    82         void RenameVars::visit( EnumInstType *aggregateUseType ) {
    83                 typeBefore( aggregateUseType );
    84                 acceptAll( aggregateUseType->get_parameters(), *this );
    85                 typeAfter( aggregateUseType );
    86         }
    87 
    88         void RenameVars::visit( TraitInstType *aggregateUseType ) {
    89                 typeBefore( aggregateUseType );
    90                 acceptAll( aggregateUseType->get_parameters(), *this );
    91                 typeAfter( aggregateUseType );
    92         }
    93 
    94         void RenameVars::visit( TypeInstType *instType ) {
    95                 typeBefore( instType );
    96                 std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() );
    97                 if ( i != mapStack.front().end() ) {
    98                         instType->set_name( i->second );
    99                 } else {
    100                 } // if
    101                 acceptAll( instType->get_parameters(), *this );
    102                 typeAfter( instType );
    103         }
    104 
    105         void RenameVars::visit( TupleType *tupleType ) {
    106                 typeBefore( tupleType );
    107                 acceptAll( tupleType->get_types(), *this );
    108                 typeAfter( tupleType );
    109         }
    110 
    111         void RenameVars::visit( VarArgsType *varArgsType ) {
    112                 typeBefore( varArgsType );
    113                 typeAfter( varArgsType );
    114         }
    115 
    116         void RenameVars::visit( ZeroType *zeroType ) {
    117                 typeBefore( zeroType );
    118                 typeAfter( zeroType );
    119         }
    120 
    121         void RenameVars::visit( OneType *oneType ) {
    122                 typeBefore( oneType );
    123                 typeAfter( oneType );
    124         }
    125 
    126         void RenameVars::typeBefore( Type *type ) {
    127                 if ( ! type->get_forall().empty() ) {
    128                         // copies current name mapping into new mapping
    129                         mapStack.push_front( mapStack.front() );
    130                         // renames all "forall" type names to `_${level}_${name}'
    131                         for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
    132                                 std::ostringstream output;
    133                                 output << "_" << resetCount << "_" << level << "_" << (*i)->get_name();
    134                                 std::string newname( output.str() );
    135                                 mapStack.front()[ (*i)->get_name() ] = newname;
    136                                 (*i)->set_name( newname );
    137                                 // ditto for assertion names, the next level in
    138                                 level++;
    139                                 acceptAll( (*i)->get_assertions(), *this );
    140                         } // for
    141                 } // if
    142         }
    143 
    144         void RenameVars::typeAfter( Type *type ) {
    145                 // clears name mapping added by typeBefore()
    146                 if ( ! type->get_forall().empty() ) {
    147                         mapStack.pop_front();
    148                 } // if
    149         }
    150 
     91                void RenameVars::postvisit( Type * type ) {
     92                        // clears name mapping added by typeBefore()
     93                        if ( ! type->forall.empty() ) {
     94                                mapStack.pop_front();
     95                        } // if
     96                }
     97        } // namespace
    15198} // namespace ResolvExpr
    15299
  • src/ResolvExpr/RenameVars.h

    rd2d50d7 r81e1f32b  
    2424
    2525namespace ResolvExpr {
     26        /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID
     27        void renameTyVars( Type * );
    2628
    27         /// Provides a consistent renaming of forall type names in a hierarchy by prefixing them with a unique "level" ID
    28         class RenameVars : public Visitor {
    29           public:
    30                 RenameVars();
    31                 void reset();
    32           private:
    33                 virtual void visit( VoidType *basicType );
    34                 virtual void visit( BasicType *basicType );
    35                 virtual void visit( PointerType *pointerType );
    36                 virtual void visit( ArrayType *arrayType );
    37                 virtual void visit( FunctionType *functionType );
    38                 virtual void visit( StructInstType *aggregateUseType );
    39                 virtual void visit( UnionInstType *aggregateUseType );
    40                 virtual void visit( EnumInstType *aggregateUseType );
    41                 virtual void visit( TraitInstType *aggregateUseType );
    42                 virtual void visit( TypeInstType *aggregateUseType );
    43                 virtual void visit( TupleType *tupleType );
    44                 virtual void visit( VarArgsType *varArgsType );
    45                 virtual void visit( ZeroType *zeroType );
    46                 virtual void visit( OneType *oneType );
    47 
    48                 void typeBefore( Type *type );
    49                 void typeAfter( Type *type );
    50                 int level, resetCount;
    51                 std::list< std::map< std::string, std::string > > mapStack;
    52         };
    53 
    54         extern RenameVars global_renamer;
     29        /// resets internal state of renamer to avoid overflow
     30        void resetTyVarRenaming();
    5531} // namespace ResolvExpr
    5632
  • src/ResolvExpr/Resolver.cc

    rd2d50d7 r81e1f32b  
    132132
    133133        void findVoidExpression( Expression *& untyped, const SymTab::Indexer &indexer ) {
    134                 global_renamer.reset();
     134                resetTyVarRenaming();
    135135                TypeEnvironment env;
    136136                Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
  • src/ResolvExpr/Unify.cc

    rd2d50d7 r81e1f32b  
    4444namespace ResolvExpr {
    4545
    46         class Unify : public Visitor {
    47           public:
     46        struct Unify : public WithShortCircuiting {
    4847                Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
    4948
    5049                bool get_result() const { return result; }
     50
     51                void previsit( BaseSyntaxNode * ) { visit_children = false; }
     52
     53                void postvisit( VoidType * voidType );
     54                void postvisit( BasicType * basicType );
     55                void postvisit( PointerType * pointerType );
     56                void postvisit( ArrayType * arrayType );
     57                void postvisit( ReferenceType * refType );
     58                void postvisit( FunctionType * functionType );
     59                void postvisit( StructInstType * aggregateUseType );
     60                void postvisit( UnionInstType * aggregateUseType );
     61                void postvisit( EnumInstType * aggregateUseType );
     62                void postvisit( TraitInstType * aggregateUseType );
     63                void postvisit( TypeInstType * aggregateUseType );
     64                void postvisit( TupleType * tupleType );
     65                void postvisit( VarArgsType * varArgsType );
     66                void postvisit( ZeroType * zeroType );
     67                void postvisit( OneType * oneType );
     68
    5169          private:
    52                 virtual void visit(VoidType *voidType);
    53                 virtual void visit(BasicType *basicType);
    54                 virtual void visit(PointerType *pointerType);
    55                 virtual void visit(ArrayType *arrayType);
    56                 virtual void visit(ReferenceType *refType);
    57                 virtual void visit(FunctionType *functionType);
    58                 virtual void visit(StructInstType *aggregateUseType);
    59                 virtual void visit(UnionInstType *aggregateUseType);
    60                 virtual void visit(EnumInstType *aggregateUseType);
    61                 virtual void visit(TraitInstType *aggregateUseType);
    62                 virtual void visit(TypeInstType *aggregateUseType);
    63                 virtual void visit(TupleType *tupleType);
    64                 virtual void visit(VarArgsType *varArgsType);
    65                 virtual void visit(ZeroType *zeroType);
    66                 virtual void visit(OneType *oneType);
    67 
    6870                template< typename RefType > void handleRefType( RefType *inst, Type *other );
    6971                template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
     
    325327                        result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
    326328                } else {
    327                         Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
     329                        PassVisitor<Unify> comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
    328330                        type1->accept( comparator );
    329                         result = comparator.get_result();
     331                        result = comparator.pass.get_result();
    330332                } // if
    331333#ifdef DEBUG
     
    404406        }
    405407
    406         void Unify::visit( __attribute__((unused)) VoidType *voidType) {
     408        void Unify::postvisit( __attribute__((unused)) VoidType *voidType) {
    407409                result = dynamic_cast< VoidType* >( type2 );
    408410        }
    409411
    410         void Unify::visit(BasicType *basicType) {
     412        void Unify::postvisit(BasicType *basicType) {
    411413                if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
    412414                        result = basicType->get_kind() == otherBasic->get_kind();
     
    436438        }
    437439
    438         void Unify::visit(PointerType *pointerType) {
     440        void Unify::postvisit(PointerType *pointerType) {
    439441                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
    440442                        result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
     
    444446        }
    445447
    446         void Unify::visit(ReferenceType *refType) {
     448        void Unify::postvisit(ReferenceType *refType) {
    447449                if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
    448450                        result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
     
    452454        }
    453455
    454         void Unify::visit(ArrayType *arrayType) {
     456        void Unify::postvisit(ArrayType *arrayType) {
    455457                ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
    456458                // to unify, array types must both be VLA or both not VLA
     
    567569        }
    568570
    569         void Unify::visit(FunctionType *functionType) {
     571        void Unify::postvisit(FunctionType *functionType) {
    570572                FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
    571573                if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
     
    669671        }
    670672
    671         void Unify::visit(StructInstType *structInst) {
     673        void Unify::postvisit(StructInstType *structInst) {
    672674                handleGenericRefType( structInst, type2 );
    673675        }
    674676
    675         void Unify::visit(UnionInstType *unionInst) {
     677        void Unify::postvisit(UnionInstType *unionInst) {
    676678                handleGenericRefType( unionInst, type2 );
    677679        }
    678680
    679         void Unify::visit(EnumInstType *enumInst) {
     681        void Unify::postvisit(EnumInstType *enumInst) {
    680682                handleRefType( enumInst, type2 );
    681683        }
    682684
    683         void Unify::visit(TraitInstType *contextInst) {
     685        void Unify::postvisit(TraitInstType *contextInst) {
    684686                handleRefType( contextInst, type2 );
    685687        }
    686688
    687         void Unify::visit(TypeInstType *typeInst) {
     689        void Unify::postvisit(TypeInstType *typeInst) {
    688690                assert( openVars.find( typeInst->get_name() ) == openVars.end() );
    689691                TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
     
    740742        }
    741743
    742         void Unify::visit(TupleType *tupleType) {
     744        void Unify::postvisit(TupleType *tupleType) {
    743745                if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
    744746                        std::unique_ptr<TupleType> flat1( tupleType->clone() );
     
    757759        }
    758760
    759         void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
     761        void Unify::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {
    760762                result = dynamic_cast< VarArgsType* >( type2 );
    761763        }
    762764
    763         void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
     765        void Unify::postvisit( __attribute__((unused)) ZeroType *zeroType ) {
    764766                result = dynamic_cast< ZeroType* >( type2 );
    765767        }
    766768
    767         void Unify::visit( __attribute__((unused)) OneType *oneType ) {
     769        void Unify::postvisit( __attribute__((unused)) OneType *oneType ) {
    768770                result = dynamic_cast< OneType* >( type2 );
    769771        }
Note: See TracChangeset for help on using the changeset viewer.