[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[dc2e7e0] | 7 | // AlternativeFinder.h -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sat May 16 23:56:12 2015 |
---|
[a5f0529] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Jul 26 11:24:00 2017 |
---|
| 13 | // Update Count : 4 |
---|
[dc2e7e0] | 14 | // |
---|
[a32b204] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
| 18 | #include <set> |
---|
| 19 | |
---|
| 20 | #include "Alternative.h" |
---|
| 21 | #include "Unify.h" |
---|
| 22 | #include "SynTree/SynTree.h" |
---|
| 23 | #include "SymTab/Indexer.h" |
---|
| 24 | #include "SynTree/TypeSubstitution.h" |
---|
| 25 | |
---|
| 26 | namespace ResolvExpr { |
---|
[a32b204] | 27 | class AlternativeFinder : public Visitor { |
---|
| 28 | public: |
---|
| 29 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); |
---|
[b6fe7e6] | 30 | void find( Expression *expr, bool adjust = false, bool prune = true ); |
---|
[0f19d763] | 31 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types |
---|
[b6fe7e6] | 32 | void findWithAdjustment( Expression *expr, bool prune = true ); |
---|
[a32b204] | 33 | AltList &get_alternatives() { return alternatives; } |
---|
| 34 | |
---|
| 35 | // make this look like an STL container so that we can apply generic algorithms |
---|
| 36 | typedef Alternative value_type; |
---|
| 37 | typedef AltList::iterator iterator; |
---|
| 38 | typedef AltList::const_iterator const_iterator; |
---|
| 39 | AltList::iterator begin() { return alternatives.begin(); } |
---|
| 40 | AltList::iterator end() { return alternatives.end(); } |
---|
| 41 | AltList::const_iterator begin() const { return alternatives.begin(); } |
---|
| 42 | AltList::const_iterator end() const { return alternatives.end(); } |
---|
[51b7345] | 43 | |
---|
[a32b204] | 44 | const SymTab::Indexer &get_indexer() const { return indexer; } |
---|
| 45 | const TypeEnvironment &get_environ() const { return env; } |
---|
| 46 | private: |
---|
| 47 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
| 48 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
| 49 | virtual void visit( AddressExpr *addressExpr ); |
---|
| 50 | virtual void visit( CastExpr *castExpr ); |
---|
[a5f0529] | 51 | virtual void visit( VirtualCastExpr *castExpr ); |
---|
[a32b204] | 52 | virtual void visit( UntypedMemberExpr *memberExpr ); |
---|
| 53 | virtual void visit( MemberExpr *memberExpr ); |
---|
| 54 | virtual void visit( NameExpr *variableExpr ); |
---|
| 55 | virtual void visit( VariableExpr *variableExpr ); |
---|
[dc2e7e0] | 56 | virtual void visit( ConstantExpr *constantExpr ); |
---|
[a32b204] | 57 | virtual void visit( SizeofExpr *sizeofExpr ); |
---|
[25a054f] | 58 | virtual void visit( AlignofExpr *alignofExpr ); |
---|
[2a4b088] | 59 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
[25a054f] | 60 | virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
[afc1045] | 61 | virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
[a32b204] | 62 | virtual void visit( AttrExpr *attrExpr ); |
---|
| 63 | virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 64 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 65 | virtual void visit( CommaExpr *commaExpr ); |
---|
[dc2e7e0] | 66 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
[b6fe7e6] | 67 | virtual void visit( ConstructorExpr * ctorExpr ); |
---|
[8a5cad8] | 68 | virtual void visit( RangeExpr * rangeExpr ); |
---|
[907eccb] | 69 | virtual void visit( UntypedTupleExpr *tupleExpr ); |
---|
| 70 | virtual void visit( TupleExpr *tupleExpr ); |
---|
[8f7cea1] | 71 | virtual void visit( TupleIndexExpr *tupleExpr ); |
---|
[aa8f9df] | 72 | virtual void visit( TupleAssignExpr *tupleExpr ); |
---|
[bf32bb8] | 73 | virtual void visit( UniqueExpr *unqExpr ); |
---|
[722617d] | 74 | virtual void visit( StmtExpr *stmtExpr ); |
---|
[e4d829b] | 75 | virtual void visit( UntypedInitExpr *initExpr ); |
---|
[5af62f1] | 76 | /// Runs a new alternative finder on each element in [begin, end) |
---|
| 77 | /// and writes each alternative finder to out. |
---|
[a32b204] | 78 | template< typename InputIterator, typename OutputIterator > |
---|
| 79 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
[51b7345] | 80 | |
---|
[4b0f997] | 81 | /// Adds alternatives for anonymous members |
---|
| 82 | void addAnonConversions( const Alternative & alt ); |
---|
[2a4b088] | 83 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member |
---|
[add7117] | 84 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); |
---|
[848ce71] | 85 | /// Adds alternatives for member expressions where the left side has tuple type |
---|
| 86 | void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); |
---|
[2a4b088] | 87 | /// Adds alternatives for offsetof expressions, given the base type and name of the member |
---|
| 88 | template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name ); |
---|
[aefcc3b] | 89 | bool instantiateFunction( std::list< DeclarationWithType* >& formals, const AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave, AltList & out ); |
---|
[a32b204] | 90 | template< typename OutputIterator > |
---|
[aefcc3b] | 91 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const AltList &actualAlt, OutputIterator out ); |
---|
[a32b204] | 92 | template< typename OutputIterator > |
---|
| 93 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); |
---|
| 94 | void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ); |
---|
[51b7345] | 95 | |
---|
[a32b204] | 96 | const SymTab::Indexer &indexer; |
---|
| 97 | AltList alternatives; |
---|
| 98 | const TypeEnvironment &env; |
---|
[7933351] | 99 | Type * targetType = nullptr; |
---|
[a32b204] | 100 | }; // AlternativeFinder |
---|
| 101 | |
---|
| 102 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ); |
---|
[5af62f1] | 103 | |
---|
| 104 | template< typename InputIterator, typename OutputIterator > |
---|
| 105 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
| 106 | AltList alternatives; |
---|
| 107 | |
---|
| 108 | // select the alternatives that have the minimum parameter cost |
---|
| 109 | Cost minCost = Cost::infinity; |
---|
| 110 | for ( InputIterator i = begin; i != end; ++i ) { |
---|
| 111 | if ( i->cost < minCost ) { |
---|
| 112 | minCost = i->cost; |
---|
| 113 | i->cost = i->cvtCost; |
---|
| 114 | alternatives.clear(); |
---|
| 115 | alternatives.push_back( *i ); |
---|
| 116 | } else if ( i->cost == minCost ) { |
---|
| 117 | i->cost = i->cvtCost; |
---|
| 118 | alternatives.push_back( *i ); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | std::copy( alternatives.begin(), alternatives.end(), out ); |
---|
| 122 | } |
---|
[908cc83] | 123 | |
---|
| 124 | Cost sumCost( const AltList &in ); |
---|
[ac9ca96] | 125 | |
---|
| 126 | template< typename InputIterator > |
---|
| 127 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { |
---|
| 128 | while ( begin != end ) { |
---|
| 129 | result.simpleCombine( (*begin++).env ); |
---|
| 130 | } |
---|
| 131 | } |
---|
[51b7345] | 132 | } // namespace ResolvExpr |
---|
| 133 | |
---|
[a32b204] | 134 | // Local Variables: // |
---|
| 135 | // tab-width: 4 // |
---|
| 136 | // mode: c++ // |
---|
| 137 | // compile-command: "make install" // |
---|
| 138 | // End: // |
---|