| 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 | // | 
|---|
| 7 | // AlternativeFinder.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sat May 16 23:56:12 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Wed Jul 26 11:24:00 2017 | 
|---|
| 13 | // Update Count     : 4 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <algorithm>                     // for copy | 
|---|
| 19 | #include <list>                          // for list | 
|---|
| 20 | #include <string>                        // for string | 
|---|
| 21 |  | 
|---|
| 22 | #include "Alternative.h"                 // for AltList, Alternative | 
|---|
| 23 | #include "ExplodedActual.h"              // for ExplodedActual | 
|---|
| 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 | 
|---|
| 32 |  | 
|---|
| 33 | namespace ResolvExpr { | 
|---|
| 34 | struct ArgPack; | 
|---|
| 35 |  | 
|---|
| 36 | /// First index is which argument, second index is which alternative for that argument, | 
|---|
| 37 | /// third index is which exploded element of that alternative | 
|---|
| 38 | using ExplodedArgs = std::vector< std::vector< ExplodedActual > >; | 
|---|
| 39 |  | 
|---|
| 40 | class AlternativeFinder { | 
|---|
| 41 | public: | 
|---|
| 42 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env ); | 
|---|
| 43 |  | 
|---|
| 44 | AlternativeFinder( const AlternativeFinder& o ) | 
|---|
| 45 | : indexer(o.indexer), alternatives(o.alternatives), env(o.env), | 
|---|
| 46 | targetType(o.targetType) {} | 
|---|
| 47 |  | 
|---|
| 48 | AlternativeFinder( AlternativeFinder&& o ) | 
|---|
| 49 | : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env), | 
|---|
| 50 | targetType(o.targetType) {} | 
|---|
| 51 |  | 
|---|
| 52 | AlternativeFinder& operator= ( const AlternativeFinder& o ) { | 
|---|
| 53 | if (&o == this) return *this; | 
|---|
| 54 |  | 
|---|
| 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; | 
|---|
| 63 |  | 
|---|
| 64 | // horrific nasty hack to rebind references... | 
|---|
| 65 | alternatives.~AltList(); | 
|---|
| 66 | new(this) AlternativeFinder(std::move(o)); | 
|---|
| 67 | return *this; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true ); | 
|---|
| 71 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types | 
|---|
| 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 ); | 
|---|
| 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(); } | 
|---|
| 87 |  | 
|---|
| 88 | const SymTab::Indexer &get_indexer() const { return indexer; } | 
|---|
| 89 | const TypeEnvironment &get_environ() const { return env; } | 
|---|
| 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 ); | 
|---|
| 95 | private: | 
|---|
| 96 | struct Finder; | 
|---|
| 97 | const SymTab::Indexer &indexer; | 
|---|
| 98 | AltList alternatives; | 
|---|
| 99 | const TypeEnvironment &env; | 
|---|
| 100 | Type * targetType = nullptr; | 
|---|
| 101 | }; // AlternativeFinder | 
|---|
| 102 |  | 
|---|
| 103 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env ); | 
|---|
| 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 | } | 
|---|
| 124 |  | 
|---|
| 125 | Cost sumCost( const AltList &in ); | 
|---|
| 126 | void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 ); | 
|---|
| 127 |  | 
|---|
| 128 | /// Adds type variables to the open variable set and marks their assertions | 
|---|
| 129 | void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions ); | 
|---|
| 130 |  | 
|---|
| 131 | template< typename InputIterator > | 
|---|
| 132 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { | 
|---|
| 133 | while ( begin != end ) { | 
|---|
| 134 | result.simpleCombine( (*begin++).env ); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | } // namespace ResolvExpr | 
|---|
| 139 |  | 
|---|
| 140 | // Local Variables: // | 
|---|
| 141 | // tab-width: 4 // | 
|---|
| 142 | // mode: c++ // | 
|---|
| 143 | // compile-command: "make install" // | 
|---|
| 144 | // End: // | 
|---|