| 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 : Aaron B. Moss
 | 
|---|
| 12 | // Last Modified On : Fri Oct -5 10:01:00 2018
 | 
|---|
| 13 | // Update Count     : 5
 | 
|---|
| 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 "ResolvMode.h"                  // for ResolvMode
 | 
|---|
| 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
 | 
|---|
| 33 | 
 | 
|---|
| 34 | namespace ResolvExpr {
 | 
|---|
| 35 |         struct ArgPack;
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         /// First index is which argument, second index is which alternative for that argument,
 | 
|---|
| 38 |         /// third index is which exploded element of that alternative
 | 
|---|
| 39 |         using ExplodedArgs_old = std::vector< std::vector< ExplodedActual > >;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         class AlternativeFinder {
 | 
|---|
| 42 |           public:
 | 
|---|
| 43 |                 AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 44 | 
 | 
|---|
| 45 |                 AlternativeFinder( const AlternativeFinder& o )
 | 
|---|
| 46 |                         : indexer(o.indexer), alternatives(o.alternatives), env(o.env),
 | 
|---|
| 47 |                           targetType(o.targetType) {}
 | 
|---|
| 48 | 
 | 
|---|
| 49 |                 AlternativeFinder( AlternativeFinder&& o )
 | 
|---|
| 50 |                         : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env),
 | 
|---|
| 51 |                           targetType(o.targetType) {}
 | 
|---|
| 52 | 
 | 
|---|
| 53 |                 AlternativeFinder& operator= ( const AlternativeFinder& o ) {
 | 
|---|
| 54 |                         if (&o == this) return *this;
 | 
|---|
| 55 | 
 | 
|---|
| 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;
 | 
|---|
| 64 | 
 | 
|---|
| 65 |                         // horrific nasty hack to rebind references...
 | 
|---|
| 66 |                         alternatives.~AltList();
 | 
|---|
| 67 |                         new(this) AlternativeFinder(std::move(o));
 | 
|---|
| 68 |                         return *this;
 | 
|---|
| 69 |                 }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                 void find( Expression *expr, ResolvMode mode = ResolvMode{} );
 | 
|---|
| 72 |                 /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
 | 
|---|
| 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 );
 | 
|---|
| 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(); }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |                 const SymTab::Indexer &get_indexer() const { return indexer; }
 | 
|---|
| 90 |                 const TypeEnvironment &get_environ() const { return env; }
 | 
|---|
| 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 );
 | 
|---|
| 96 |           private:
 | 
|---|
| 97 |                 struct Finder;
 | 
|---|
| 98 |                 const SymTab::Indexer &indexer;
 | 
|---|
| 99 |                 AltList alternatives;
 | 
|---|
| 100 |                 const TypeEnvironment &env;
 | 
|---|
| 101 |                 Type * targetType = nullptr;
 | 
|---|
| 102 |         }; // AlternativeFinder
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
 | 
|---|
| 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 |         }
 | 
|---|
| 125 | 
 | 
|---|
| 126 |         Cost sumCost( const AltList &in );
 | 
|---|
| 127 |         void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 );
 | 
|---|
| 128 | 
 | 
|---|
| 129 |         /// Adds type variables to the open variable set and marks their assertions
 | 
|---|
| 130 |         void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions );
 | 
|---|
| 131 | 
 | 
|---|
| 132 |         template< typename InputIterator >
 | 
|---|
| 133 |         void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
 | 
|---|
| 134 |                 while ( begin != end ) {
 | 
|---|
| 135 |                         result.simpleCombine( (*begin++).env );
 | 
|---|
| 136 |                 }
 | 
|---|
| 137 |         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | } // namespace ResolvExpr
 | 
|---|
| 140 | 
 | 
|---|
| 141 | // Local Variables: //
 | 
|---|
| 142 | // tab-width: 4 //
 | 
|---|
| 143 | // mode: c++ //
 | 
|---|
| 144 | // compile-command: "make install" //
 | 
|---|
| 145 | // End: //
 | 
|---|