[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
|
---|
[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
|
---|
[51b73452] | 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 |
|
---|
[13deae88] | 40 | class AlternativeFinder {
|
---|
[a32b204] | 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(); }
|
---|
[51b73452] | 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:
|
---|
[13deae88] | 96 | struct Finder;
|
---|
[a32b204] | 97 | const SymTab::Indexer &indexer;
|
---|
| 98 | AltList alternatives;
|
---|
| 99 | const TypeEnvironment &env;
|
---|
[7933351] | 100 | Type * targetType = nullptr;
|
---|
[a32b204] | 101 | }; // AlternativeFinder
|
---|
| 102 |
|
---|
| 103 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
|
---|
[5af62f1] | 104 |
|
---|
| 105 | template< typename InputIterator, typename OutputIterator >
|
---|
| 106 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
| 107 | AltList alternatives;
|
---|
| 108 |
|
---|
| 109 | // select the alternatives that have the minimum parameter cost
|
---|
| 110 | Cost minCost = Cost::infinity;
|
---|
| 111 | for ( InputIterator i = begin; i != end; ++i ) {
|
---|
| 112 | if ( i->cost < minCost ) {
|
---|
| 113 | minCost = i->cost;
|
---|
| 114 | i->cost = i->cvtCost;
|
---|
| 115 | alternatives.clear();
|
---|
| 116 | alternatives.push_back( *i );
|
---|
| 117 | } else if ( i->cost == minCost ) {
|
---|
| 118 | i->cost = i->cvtCost;
|
---|
| 119 | alternatives.push_back( *i );
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | std::copy( alternatives.begin(), alternatives.end(), out );
|
---|
| 123 | }
|
---|
[908cc83] | 124 |
|
---|
| 125 | Cost sumCost( const AltList &in );
|
---|
[1e8bbac9] | 126 | void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 );
|
---|
[ac9ca96] | 127 |
|
---|
[6f326b1] | 128 | /// Adds type variables to the open variable set and marks their assertions
|
---|
| 129 | void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions );
|
---|
| 130 |
|
---|
[ac9ca96] | 131 | template< typename InputIterator >
|
---|
| 132 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
|
---|
| 133 | while ( begin != end ) {
|
---|
| 134 | result.simpleCombine( (*begin++).env );
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[1e8bbac9] | 137 |
|
---|
[51b73452] | 138 | } // namespace ResolvExpr
|
---|
| 139 |
|
---|
[a32b204] | 140 | // Local Variables: //
|
---|
| 141 | // tab-width: 4 //
|
---|
| 142 | // mode: c++ //
|
---|
| 143 | // compile-command: "make install" //
|
---|
| 144 | // End: //
|
---|