[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
|
---|
[dc2e7e0] | 11 | // Last Modified By : Rob Schluntz
|
---|
| 12 | // Last Modified On : Tue Apr 19 11:44:53 2016
|
---|
[a32b204] | 13 | // Update Count : 2
|
---|
[dc2e7e0] | 14 | //
|
---|
[a32b204] | 15 |
|
---|
[d9a0e76] | 16 | #ifndef ALTERNATIVEFINDER_H
|
---|
| 17 | #define ALTERNATIVEFINDER_H
|
---|
[51b73452] | 18 |
|
---|
| 19 | #include <set>
|
---|
| 20 |
|
---|
| 21 | #include "Alternative.h"
|
---|
| 22 | #include "Unify.h"
|
---|
| 23 | #include "SynTree/SynTree.h"
|
---|
| 24 | #include "SymTab/Indexer.h"
|
---|
| 25 | #include "SynTree/TypeSubstitution.h"
|
---|
| 26 |
|
---|
| 27 | namespace ResolvExpr {
|
---|
[a32b204] | 28 | class AlternativeFinder : public Visitor {
|
---|
| 29 | public:
|
---|
| 30 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
|
---|
[b6fe7e6] | 31 | void find( Expression *expr, bool adjust = false, bool prune = true );
|
---|
[0f19d763] | 32 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
|
---|
[b6fe7e6] | 33 | void findWithAdjustment( Expression *expr, bool prune = true );
|
---|
[a32b204] | 34 | AltList &get_alternatives() { return alternatives; }
|
---|
| 35 |
|
---|
| 36 | // make this look like an STL container so that we can apply generic algorithms
|
---|
| 37 | typedef Alternative value_type;
|
---|
| 38 | typedef AltList::iterator iterator;
|
---|
| 39 | typedef AltList::const_iterator const_iterator;
|
---|
| 40 | AltList::iterator begin() { return alternatives.begin(); }
|
---|
| 41 | AltList::iterator end() { return alternatives.end(); }
|
---|
| 42 | AltList::const_iterator begin() const { return alternatives.begin(); }
|
---|
| 43 | AltList::const_iterator end() const { return alternatives.end(); }
|
---|
[51b73452] | 44 |
|
---|
[a32b204] | 45 | const SymTab::Indexer &get_indexer() const { return indexer; }
|
---|
| 46 | const TypeEnvironment &get_environ() const { return env; }
|
---|
| 47 | private:
|
---|
| 48 | virtual void visit( ApplicationExpr *applicationExpr );
|
---|
| 49 | virtual void visit( UntypedExpr *untypedExpr );
|
---|
| 50 | virtual void visit( AddressExpr *addressExpr );
|
---|
| 51 | virtual void visit( CastExpr *castExpr );
|
---|
| 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 );
|
---|
| 66 | virtual void visit( TupleExpr *tupleExpr );
|
---|
[dc2e7e0] | 67 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
|
---|
[b6fe7e6] | 68 | virtual void visit( ConstructorExpr * ctorExpr );
|
---|
[8f7cea1] | 69 | virtual void visit( TupleIndexExpr *tupleExpr );
|
---|
[aa8f9df] | 70 | virtual void visit( TupleAssignExpr *tupleExpr );
|
---|
[bf32bb8] | 71 | virtual void visit( UniqueExpr *unqExpr );
|
---|
[5af62f1] | 72 | /// Runs a new alternative finder on each element in [begin, end)
|
---|
| 73 | /// and writes each alternative finder to out.
|
---|
[a32b204] | 74 | template< typename InputIterator, typename OutputIterator >
|
---|
| 75 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
|
---|
[51b73452] | 76 |
|
---|
[2a4b088] | 77 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
|
---|
[add7117] | 78 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
[848ce71] | 79 | /// Adds alternatives for member expressions where the left side has tuple type
|
---|
| 80 | void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
[2a4b088] | 81 | /// Adds alternatives for offsetof expressions, given the base type and name of the member
|
---|
| 82 | template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
|
---|
[aefcc3b] | 83 | bool instantiateFunction( std::list< DeclarationWithType* >& formals, const AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave, AltList & out );
|
---|
[a32b204] | 84 | template< typename OutputIterator >
|
---|
[aefcc3b] | 85 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const AltList &actualAlt, OutputIterator out );
|
---|
[a32b204] | 86 | template< typename OutputIterator >
|
---|
| 87 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
|
---|
| 88 | void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env );
|
---|
[51b73452] | 89 |
|
---|
[a32b204] | 90 | const SymTab::Indexer &indexer;
|
---|
| 91 | AltList alternatives;
|
---|
| 92 | const TypeEnvironment &env;
|
---|
| 93 | }; // AlternativeFinder
|
---|
| 94 |
|
---|
| 95 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
|
---|
[5af62f1] | 96 |
|
---|
[77971f6] | 97 | void resolveObject( const SymTab::Indexer & indexer, ObjectDecl * objectDecl );
|
---|
| 98 |
|
---|
[5af62f1] | 99 | template< typename InputIterator, typename OutputIterator >
|
---|
| 100 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
| 101 | AltList alternatives;
|
---|
| 102 |
|
---|
| 103 | // select the alternatives that have the minimum parameter cost
|
---|
| 104 | Cost minCost = Cost::infinity;
|
---|
| 105 | for ( InputIterator i = begin; i != end; ++i ) {
|
---|
| 106 | if ( i->cost < minCost ) {
|
---|
| 107 | minCost = i->cost;
|
---|
| 108 | i->cost = i->cvtCost;
|
---|
| 109 | alternatives.clear();
|
---|
| 110 | alternatives.push_back( *i );
|
---|
| 111 | } else if ( i->cost == minCost ) {
|
---|
| 112 | i->cost = i->cvtCost;
|
---|
| 113 | alternatives.push_back( *i );
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | std::copy( alternatives.begin(), alternatives.end(), out );
|
---|
| 117 | }
|
---|
[908cc83] | 118 |
|
---|
| 119 | Cost sumCost( const AltList &in );
|
---|
[ac9ca96] | 120 |
|
---|
| 121 | template< typename InputIterator >
|
---|
| 122 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
|
---|
| 123 | while ( begin != end ) {
|
---|
| 124 | result.simpleCombine( (*begin++).env );
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[51b73452] | 127 | } // namespace ResolvExpr
|
---|
| 128 |
|
---|
[d9a0e76] | 129 | #endif // ALTERNATIVEFINDER_H
|
---|
[a32b204] | 130 |
|
---|
| 131 | // Local Variables: //
|
---|
| 132 | // tab-width: 4 //
|
---|
| 133 | // mode: c++ //
|
---|
| 134 | // compile-command: "make install" //
|
---|
| 135 | // End: //
|
---|