[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 | |
---|
[ea6332d] | 18 | #include <algorithm> // for copy |
---|
| 19 | #include <list> // for list |
---|
| 20 | #include <string> // for string |
---|
[51b7345] | 21 | |
---|
[ea6332d] | 22 | #include "Alternative.h" // for AltList, Alternative |
---|
[a8b27c6] | 23 | #include "ExplodedActual.h" // for ExplodedActual |
---|
[ea6332d] | 24 | #include "ResolvExpr/Cost.h" // for Cost, Cost::infinity |
---|
| 25 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet |
---|
| 26 | #include "SynTree/Visitor.h" // for Visitor |
---|
| 27 | #include "SynTree/SynTree.h" // for Visitor Nodes |
---|
| 28 | |
---|
| 29 | namespace SymTab { |
---|
| 30 | class Indexer; |
---|
| 31 | } // namespace SymTab |
---|
[51b7345] | 32 | |
---|
| 33 | namespace ResolvExpr { |
---|
[452747a] | 34 | struct ArgPack; |
---|
| 35 | |
---|
[178e4ec] | 36 | /// First index is which argument, second index is which alternative for that argument, |
---|
[a8b27c6] | 37 | /// third index is which exploded element of that alternative |
---|
| 38 | using ExplodedArgs = std::vector< std::vector< ExplodedActual > >; |
---|
[178e4ec] | 39 | |
---|
[a32b204] | 40 | class AlternativeFinder : public Visitor { |
---|
| 41 | public: |
---|
| 42 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); |
---|
[aeb75b1] | 43 | |
---|
| 44 | AlternativeFinder( const AlternativeFinder& o ) |
---|
[452747a] | 45 | : indexer(o.indexer), alternatives(o.alternatives), env(o.env), |
---|
[aeb75b1] | 46 | targetType(o.targetType) {} |
---|
[452747a] | 47 | |
---|
[aeb75b1] | 48 | AlternativeFinder( AlternativeFinder&& o ) |
---|
[452747a] | 49 | : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), |
---|
[aeb75b1] | 50 | targetType(o.targetType) {} |
---|
[452747a] | 51 | |
---|
[aeb75b1] | 52 | AlternativeFinder& operator= ( const AlternativeFinder& o ) { |
---|
| 53 | if (&o == this) return *this; |
---|
[452747a] | 54 | |
---|
[aeb75b1] | 55 | // horrific nasty hack to rebind references... |
---|
| 56 | alternatives.~AltList(); |
---|
| 57 | new(this) AlternativeFinder(o); |
---|
| 58 | return *this; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | AlternativeFinder& operator= ( AlternativeFinder&& o ) { |
---|
| 62 | if (&o == this) return *this; |
---|
[452747a] | 63 | |
---|
[aeb75b1] | 64 | // horrific nasty hack to rebind references... |
---|
| 65 | alternatives.~AltList(); |
---|
| 66 | new(this) AlternativeFinder(std::move(o)); |
---|
| 67 | return *this; |
---|
| 68 | } |
---|
| 69 | |
---|
[4e66a18] | 70 | void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true ); |
---|
[0f19d763] | 71 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types |
---|
[4e66a18] | 72 | void findWithAdjustment( Expression *expr ); |
---|
| 73 | /// Calls find with the adjust flag set and prune flag unset; pruning ensures there is at most one alternative per result type |
---|
| 74 | void findWithoutPrune( Expression *expr ); |
---|
| 75 | /// Calls find with the adjust and prune flags set, failFast flags unset; fail fast ensures that there is at least one resulting alternative |
---|
| 76 | void maybeFind( Expression *expr ); |
---|
[a32b204] | 77 | AltList &get_alternatives() { return alternatives; } |
---|
| 78 | |
---|
| 79 | // make this look like an STL container so that we can apply generic algorithms |
---|
| 80 | typedef Alternative value_type; |
---|
| 81 | typedef AltList::iterator iterator; |
---|
| 82 | typedef AltList::const_iterator const_iterator; |
---|
| 83 | AltList::iterator begin() { return alternatives.begin(); } |
---|
| 84 | AltList::iterator end() { return alternatives.end(); } |
---|
| 85 | AltList::const_iterator begin() const { return alternatives.begin(); } |
---|
| 86 | AltList::const_iterator end() const { return alternatives.end(); } |
---|
[51b7345] | 87 | |
---|
[a32b204] | 88 | const SymTab::Indexer &get_indexer() const { return indexer; } |
---|
| 89 | const TypeEnvironment &get_environ() const { return env; } |
---|
[1dcd9554] | 90 | |
---|
| 91 | /// Runs a new alternative finder on each element in [begin, end) |
---|
| 92 | /// and writes each alternative finder to out. |
---|
| 93 | template< typename InputIterator, typename OutputIterator > |
---|
| 94 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
[a32b204] | 95 | private: |
---|
| 96 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
| 97 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
| 98 | virtual void visit( AddressExpr *addressExpr ); |
---|
[5809461] | 99 | virtual void visit( LabelAddressExpr *labelExpr ); |
---|
[a32b204] | 100 | virtual void visit( CastExpr *castExpr ); |
---|
[a5f0529] | 101 | virtual void visit( VirtualCastExpr *castExpr ); |
---|
[a32b204] | 102 | virtual void visit( UntypedMemberExpr *memberExpr ); |
---|
| 103 | virtual void visit( MemberExpr *memberExpr ); |
---|
| 104 | virtual void visit( NameExpr *variableExpr ); |
---|
| 105 | virtual void visit( VariableExpr *variableExpr ); |
---|
[dc2e7e0] | 106 | virtual void visit( ConstantExpr *constantExpr ); |
---|
[a32b204] | 107 | virtual void visit( SizeofExpr *sizeofExpr ); |
---|
[25a054f] | 108 | virtual void visit( AlignofExpr *alignofExpr ); |
---|
[2a4b088] | 109 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
[25a054f] | 110 | virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
[afc1045] | 111 | virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
[a32b204] | 112 | virtual void visit( AttrExpr *attrExpr ); |
---|
| 113 | virtual void visit( LogicalExpr *logicalExpr ); |
---|
| 114 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
| 115 | virtual void visit( CommaExpr *commaExpr ); |
---|
[dc2e7e0] | 116 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
[b6fe7e6] | 117 | virtual void visit( ConstructorExpr * ctorExpr ); |
---|
[8a5cad8] | 118 | virtual void visit( RangeExpr * rangeExpr ); |
---|
[907eccb] | 119 | virtual void visit( UntypedTupleExpr *tupleExpr ); |
---|
| 120 | virtual void visit( TupleExpr *tupleExpr ); |
---|
[8f7cea1] | 121 | virtual void visit( TupleIndexExpr *tupleExpr ); |
---|
[aa8f9df] | 122 | virtual void visit( TupleAssignExpr *tupleExpr ); |
---|
[bf32bb8] | 123 | virtual void visit( UniqueExpr *unqExpr ); |
---|
[722617d] | 124 | virtual void visit( StmtExpr *stmtExpr ); |
---|
[e4d829b] | 125 | virtual void visit( UntypedInitExpr *initExpr ); |
---|
[51b7345] | 126 | |
---|
[4b0f997] | 127 | /// Adds alternatives for anonymous members |
---|
| 128 | void addAnonConversions( const Alternative & alt ); |
---|
[2a4b088] | 129 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member |
---|
[add7117] | 130 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); |
---|
[848ce71] | 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 ); |
---|
[2a4b088] | 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 ); |
---|
[403b388] | 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 |
---|
[e472d54] | 139 | template<typename OutputIterator> |
---|
[a8b27c6] | 140 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out ); |
---|
[403b388] | 141 | /// Checks if assertion parameters match for a new alternative |
---|
[a32b204] | 142 | template< typename OutputIterator > |
---|
| 143 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); |
---|
[51b7345] | 144 | |
---|
[a32b204] | 145 | const SymTab::Indexer &indexer; |
---|
| 146 | AltList alternatives; |
---|
| 147 | const TypeEnvironment &env; |
---|
[7933351] | 148 | Type * targetType = nullptr; |
---|
[a32b204] | 149 | }; // AlternativeFinder |
---|
| 150 | |
---|
| 151 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ); |
---|
[1dcd9554] | 152 | void referenceToRvalueConversion( Expression *& expr ); |
---|
[5af62f1] | 153 | |
---|
| 154 | template< typename InputIterator, typename OutputIterator > |
---|
| 155 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
| 156 | AltList alternatives; |
---|
| 157 | |
---|
| 158 | // select the alternatives that have the minimum parameter cost |
---|
| 159 | Cost minCost = Cost::infinity; |
---|
| 160 | for ( InputIterator i = begin; i != end; ++i ) { |
---|
| 161 | if ( i->cost < minCost ) { |
---|
| 162 | minCost = i->cost; |
---|
| 163 | i->cost = i->cvtCost; |
---|
| 164 | alternatives.clear(); |
---|
| 165 | alternatives.push_back( *i ); |
---|
| 166 | } else if ( i->cost == minCost ) { |
---|
| 167 | i->cost = i->cvtCost; |
---|
| 168 | alternatives.push_back( *i ); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | std::copy( alternatives.begin(), alternatives.end(), out ); |
---|
| 172 | } |
---|
[908cc83] | 173 | |
---|
| 174 | Cost sumCost( const AltList &in ); |
---|
[1e8bbac9] | 175 | void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ); |
---|
[ac9ca96] | 176 | |
---|
| 177 | template< typename InputIterator > |
---|
| 178 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { |
---|
| 179 | while ( begin != end ) { |
---|
| 180 | result.simpleCombine( (*begin++).env ); |
---|
| 181 | } |
---|
| 182 | } |
---|
[1e8bbac9] | 183 | |
---|
[51b7345] | 184 | } // namespace ResolvExpr |
---|
| 185 | |
---|
[a32b204] | 186 | // Local Variables: // |
---|
| 187 | // tab-width: 4 // |
---|
| 188 | // mode: c++ // |
---|
| 189 | // compile-command: "make install" // |
---|
| 190 | // End: // |
---|