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