| 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 | //
 | 
|---|
| 7 | // AlternativeFinder.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Sat May 16 23:56:12 2015
 | 
|---|
| 11 | // Last Modified By : Rob Schluntz
 | 
|---|
| 12 | // Last Modified On : Tue Apr 19 11:44:53 2016
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef ALTERNATIVEFINDER_H
 | 
|---|
| 17 | #define ALTERNATIVEFINDER_H
 | 
|---|
| 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 {
 | 
|---|
| 28 |         class AlternativeFinder : public Visitor {
 | 
|---|
| 29 |           public:
 | 
|---|
| 30 |                 AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 31 |                 void find( Expression *expr, bool adjust = false, bool prune = true );
 | 
|---|
| 32 |                 /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
 | 
|---|
| 33 |                 void findWithAdjustment( Expression *expr, bool prune = true );
 | 
|---|
| 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(); }
 | 
|---|
| 44 | 
 | 
|---|
| 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 );
 | 
|---|
| 56 |                 virtual void visit( ConstantExpr *constantExpr );
 | 
|---|
| 57 |                 virtual void visit( SizeofExpr *sizeofExpr );
 | 
|---|
| 58 |                 virtual void visit( AlignofExpr *alignofExpr );
 | 
|---|
| 59 |                 virtual void visit( UntypedOffsetofExpr *offsetofExpr );
 | 
|---|
| 60 |                 virtual void visit( OffsetofExpr *offsetofExpr );
 | 
|---|
| 61 |                 virtual void visit( OffsetPackExpr *offsetPackExpr );
 | 
|---|
| 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( ImplicitCopyCtorExpr * impCpCtorExpr );
 | 
|---|
| 67 |                 virtual void visit( ConstructorExpr * ctorExpr );
 | 
|---|
| 68 |                 virtual void visit( RangeExpr * rangeExpr );
 | 
|---|
| 69 |                 virtual void visit( UntypedTupleExpr *tupleExpr );
 | 
|---|
| 70 |                 virtual void visit( TupleExpr *tupleExpr );
 | 
|---|
| 71 |                 virtual void visit( TupleIndexExpr *tupleExpr );
 | 
|---|
| 72 |                 virtual void visit( TupleAssignExpr *tupleExpr );
 | 
|---|
| 73 |                 virtual void visit( UniqueExpr *unqExpr );
 | 
|---|
| 74 |                 virtual void visit( StmtExpr *stmtExpr );
 | 
|---|
| 75 |                 /// Runs a new alternative finder on each element in [begin, end)
 | 
|---|
| 76 |                 /// and writes each alternative finder to out.
 | 
|---|
| 77 |                 template< typename InputIterator, typename OutputIterator >
 | 
|---|
| 78 |                 void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
 | 
|---|
| 79 | 
 | 
|---|
| 80 |                 /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
 | 
|---|
| 81 |                 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
 | 
|---|
| 82 |                 /// Adds alternatives for member expressions where the left side has tuple type
 | 
|---|
| 83 |                 void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
 | 
|---|
| 84 |                 /// Adds alternatives for offsetof expressions, given the base type and name of the member
 | 
|---|
| 85 |                 template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
 | 
|---|
| 86 |                 bool instantiateFunction( std::list< DeclarationWithType* >& formals, const AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave, AltList & out );
 | 
|---|
| 87 |                 template< typename OutputIterator >
 | 
|---|
| 88 |                 void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const AltList &actualAlt, OutputIterator out );
 | 
|---|
| 89 |                 template< typename OutputIterator >
 | 
|---|
| 90 |                 void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
 | 
|---|
| 91 |                 void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env );
 | 
|---|
| 92 | 
 | 
|---|
| 93 |                 const SymTab::Indexer &indexer;
 | 
|---|
| 94 |                 AltList alternatives;
 | 
|---|
| 95 |                 const TypeEnvironment &env;
 | 
|---|
| 96 |                 Type * targetType = nullptr;
 | 
|---|
| 97 |         }; // AlternativeFinder
 | 
|---|
| 98 | 
 | 
|---|
| 99 |         Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         template< typename InputIterator, typename OutputIterator >
 | 
|---|
| 102 |         void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
 | 
|---|
| 103 |                 AltList alternatives;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |                 // select the alternatives that have the minimum parameter cost
 | 
|---|
| 106 |                 Cost minCost = Cost::infinity;
 | 
|---|
| 107 |                 for ( InputIterator i = begin; i != end; ++i ) {
 | 
|---|
| 108 |                         if ( i->cost < minCost ) {
 | 
|---|
| 109 |                                 minCost = i->cost;
 | 
|---|
| 110 |                                 i->cost = i->cvtCost;
 | 
|---|
| 111 |                                 alternatives.clear();
 | 
|---|
| 112 |                                 alternatives.push_back( *i );
 | 
|---|
| 113 |                         } else if ( i->cost == minCost ) {
 | 
|---|
| 114 |                                 i->cost = i->cvtCost;
 | 
|---|
| 115 |                                 alternatives.push_back( *i );
 | 
|---|
| 116 |                         }
 | 
|---|
| 117 |                 }
 | 
|---|
| 118 |                 std::copy( alternatives.begin(), alternatives.end(), out );
 | 
|---|
| 119 |         }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         Cost sumCost( const AltList &in );
 | 
|---|
| 122 | 
 | 
|---|
| 123 |         template< typename InputIterator >
 | 
|---|
| 124 |         void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
 | 
|---|
| 125 |                 while ( begin != end ) {
 | 
|---|
| 126 |                         result.simpleCombine( (*begin++).env );
 | 
|---|
| 127 |                 }
 | 
|---|
| 128 |         }
 | 
|---|
| 129 | } // namespace ResolvExpr
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #endif // ALTERNATIVEFINDER_H
 | 
|---|
| 132 | 
 | 
|---|
| 133 | // Local Variables: //
 | 
|---|
| 134 | // tab-width: 4 //
 | 
|---|
| 135 | // mode: c++ //
 | 
|---|
| 136 | // compile-command: "make install" //
 | 
|---|
| 137 | // End: //
 | 
|---|