source: src/ResolvExpr/AlternativeFinder.h @ 251ce80

ast-experimental
Last change on this file since 251ce80 was fed6a0f, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Header Clean-up: Moving more declarations to the header of the implementation file.

  • Property mode set to 100644
File size: 5.4 KB
Line 
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
30namespace SymTab {
31class Indexer;
32}  // namespace SymTab
33
34namespace ResolvExpr {
35        struct ArgPack;
36
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
42        /// First index is which argument, second index is which alternative for that argument,
43        /// third index is which exploded element of that alternative
44        using ExplodedArgs_old = std::vector< std::vector< ExplodedActual > >;
45
46        class AlternativeFinder {
47          public:
48                AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
49
50                AlternativeFinder( const AlternativeFinder& o )
51                        : indexer(o.indexer), alternatives(o.alternatives), env(o.env),
52                          targetType(o.targetType) {}
53
54                AlternativeFinder( AlternativeFinder&& o )
55                        : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env),
56                          targetType(o.targetType) {}
57
58                AlternativeFinder& operator= ( const AlternativeFinder& o ) {
59                        if (&o == this) return *this;
60
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;
69
70                        // horrific nasty hack to rebind references...
71                        alternatives.~AltList();
72                        new(this) AlternativeFinder(std::move(o));
73                        return *this;
74                }
75
76                void find( Expression *expr, ResolvMode mode = ResolvMode{} );
77                /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
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 );
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(); }
93
94                const SymTab::Indexer &get_indexer() const { return indexer; }
95                const TypeEnvironment &get_environ() const { return env; }
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 );
101          private:
102                struct Finder;
103                const SymTab::Indexer &indexer;
104                AltList alternatives;
105                const TypeEnvironment &env;
106                Type * targetType = nullptr;
107        }; // AlternativeFinder
108
109        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
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        }
130
131        Cost sumCost( const AltList &in );
132        void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 );
133
134        /// Adds type variables to the open variable set and marks their assertions
135        void makeUnifiableVars( Type *type, OpenVarSet &unifiableVars, AssertionSet &needAssertions );
136
137        template< typename InputIterator >
138        void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
139                while ( begin != end ) {
140                        result.simpleCombine( (*begin++).env );
141                }
142        }
143
144} // namespace ResolvExpr
145
146// Local Variables: //
147// tab-width: 4 //
148// mode: c++ //
149// compile-command: "make install" //
150// End: //
Note: See TracBrowser for help on using the repository browser.