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