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