[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 |
---|
[59cf83b] | 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Fri Oct -5 10:01:00 2018 |
---|
| 13 | // Update Count : 5 |
---|
[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 |
---|
[59cf83b] | 26 | #include "ResolvMode.h" // for ResolvMode |
---|
[ea6332d] | 27 | #include "SynTree/Visitor.h" // for Visitor |
---|
| 28 | #include "SynTree/SynTree.h" // for Visitor Nodes |
---|
| 29 | |
---|
| 30 | namespace SymTab { |
---|
| 31 | class Indexer; |
---|
| 32 | } // namespace SymTab |
---|
[51b7345] | 33 | |
---|
| 34 | namespace ResolvExpr { |
---|
[452747a] | 35 | struct ArgPack; |
---|
| 36 | |
---|
[fed6a0f] | 37 | Cost computeConversionCost( Type * actualType, Type * formalType, bool actualIsLvalue, |
---|
| 38 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); |
---|
| 39 | |
---|
| 40 | void referenceToRvalueConversion( Expression *& expr, Cost & cost ); |
---|
| 41 | |
---|
[178e4ec] | 42 | /// First index is which argument, second index is which alternative for that argument, |
---|
[a8b27c6] | 43 | /// third index is which exploded element of that alternative |
---|
[432ce7a] | 44 | using ExplodedArgs_old = std::vector< std::vector< ExplodedActual > >; |
---|
[178e4ec] | 45 | |
---|
[13deae88] | 46 | class AlternativeFinder { |
---|
[a32b204] | 47 | public: |
---|
| 48 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); |
---|
[aeb75b1] | 49 | |
---|
| 50 | AlternativeFinder( const AlternativeFinder& o ) |
---|
[452747a] | 51 | : indexer(o.indexer), alternatives(o.alternatives), env(o.env), |
---|
[aeb75b1] | 52 | targetType(o.targetType) {} |
---|
[452747a] | 53 | |
---|
[aeb75b1] | 54 | AlternativeFinder( AlternativeFinder&& o ) |
---|
[452747a] | 55 | : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), |
---|
[aeb75b1] | 56 | targetType(o.targetType) {} |
---|
[452747a] | 57 | |
---|
[aeb75b1] | 58 | AlternativeFinder& operator= ( const AlternativeFinder& o ) { |
---|
| 59 | if (&o == this) return *this; |
---|
[452747a] | 60 | |
---|
[aeb75b1] | 61 | // horrific nasty hack to rebind references... |
---|
| 62 | alternatives.~AltList(); |
---|
| 63 | new(this) AlternativeFinder(o); |
---|
| 64 | return *this; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | AlternativeFinder& operator= ( AlternativeFinder&& o ) { |
---|
| 68 | if (&o == this) return *this; |
---|
[452747a] | 69 | |
---|
[aeb75b1] | 70 | // horrific nasty hack to rebind references... |
---|
| 71 | alternatives.~AltList(); |
---|
| 72 | new(this) AlternativeFinder(std::move(o)); |
---|
| 73 | return *this; |
---|
| 74 | } |
---|
| 75 | |
---|
[59cf83b] | 76 | void find( Expression *expr, ResolvMode mode = ResolvMode{} ); |
---|
[0f19d763] | 77 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types |
---|
[4e66a18] | 78 | void findWithAdjustment( Expression *expr ); |
---|
| 79 | /// Calls find with the adjust flag set and prune flag unset; pruning ensures there is at most one alternative per result type |
---|
| 80 | void findWithoutPrune( Expression *expr ); |
---|
| 81 | /// Calls find with the adjust and prune flags set, failFast flags unset; fail fast ensures that there is at least one resulting alternative |
---|
| 82 | void maybeFind( Expression *expr ); |
---|
[a32b204] | 83 | AltList &get_alternatives() { return alternatives; } |
---|
| 84 | |
---|
| 85 | // make this look like an STL container so that we can apply generic algorithms |
---|
| 86 | typedef Alternative value_type; |
---|
| 87 | typedef AltList::iterator iterator; |
---|
| 88 | typedef AltList::const_iterator const_iterator; |
---|
| 89 | AltList::iterator begin() { return alternatives.begin(); } |
---|
| 90 | AltList::iterator end() { return alternatives.end(); } |
---|
| 91 | AltList::const_iterator begin() const { return alternatives.begin(); } |
---|
| 92 | AltList::const_iterator end() const { return alternatives.end(); } |
---|
[51b7345] | 93 | |
---|
[a32b204] | 94 | const SymTab::Indexer &get_indexer() const { return indexer; } |
---|
| 95 | const TypeEnvironment &get_environ() const { return env; } |
---|
[1dcd9554] | 96 | |
---|
| 97 | /// Runs a new alternative finder on each element in [begin, end) |
---|
| 98 | /// and writes each alternative finder to out. |
---|
| 99 | template< typename InputIterator, typename OutputIterator > |
---|
| 100 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
[a32b204] | 101 | private: |
---|
[13deae88] | 102 | struct Finder; |
---|
[a32b204] | 103 | const SymTab::Indexer &indexer; |
---|
| 104 | AltList alternatives; |
---|
| 105 | const TypeEnvironment &env; |
---|
[7933351] | 106 | Type * targetType = nullptr; |
---|
[a32b204] | 107 | }; // AlternativeFinder |
---|
| 108 | |
---|
| 109 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ); |
---|
[5af62f1] | 110 | |
---|
| 111 | template< typename InputIterator, typename OutputIterator > |
---|
| 112 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
| 113 | AltList alternatives; |
---|
| 114 | |
---|
| 115 | // select the alternatives that have the minimum parameter cost |
---|
| 116 | Cost minCost = Cost::infinity; |
---|
| 117 | for ( InputIterator i = begin; i != end; ++i ) { |
---|
| 118 | if ( i->cost < minCost ) { |
---|
| 119 | minCost = i->cost; |
---|
| 120 | i->cost = i->cvtCost; |
---|
| 121 | alternatives.clear(); |
---|
| 122 | alternatives.push_back( *i ); |
---|
| 123 | } else if ( i->cost == minCost ) { |
---|
| 124 | i->cost = i->cvtCost; |
---|
| 125 | alternatives.push_back( *i ); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | std::copy( alternatives.begin(), alternatives.end(), out ); |
---|
| 129 | } |
---|
[908cc83] | 130 | |
---|
| 131 | Cost sumCost( const AltList &in ); |
---|
[1e8bbac9] | 132 | void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ); |
---|
[ac9ca96] | 133 | |
---|
[6f326b1] | 134 | /// Adds type variables to the open variable set and marks their assertions |
---|
| 135 | void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ); |
---|
| 136 | |
---|
[ac9ca96] | 137 | template< typename InputIterator > |
---|
| 138 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { |
---|
| 139 | while ( begin != end ) { |
---|
| 140 | result.simpleCombine( (*begin++).env ); |
---|
| 141 | } |
---|
| 142 | } |
---|
[1e8bbac9] | 143 | |
---|
[51b7345] | 144 | } // namespace ResolvExpr |
---|
| 145 | |
---|
[a32b204] | 146 | // Local Variables: // |
---|
| 147 | // tab-width: 4 // |
---|
| 148 | // mode: c++ // |
---|
| 149 | // compile-command: "make install" // |
---|
| 150 | // End: // |
---|