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 : public Visitor {
|
---|
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 | virtual void visit( ApplicationExpr *applicationExpr );
|
---|
97 | virtual void visit( UntypedExpr *untypedExpr );
|
---|
98 | virtual void visit( AddressExpr *addressExpr );
|
---|
99 | virtual void visit( LabelAddressExpr *labelExpr );
|
---|
100 | virtual void visit( CastExpr *castExpr );
|
---|
101 | virtual void visit( VirtualCastExpr *castExpr );
|
---|
102 | virtual void visit( UntypedMemberExpr *memberExpr );
|
---|
103 | virtual void visit( MemberExpr *memberExpr );
|
---|
104 | virtual void visit( NameExpr *variableExpr );
|
---|
105 | virtual void visit( VariableExpr *variableExpr );
|
---|
106 | virtual void visit( ConstantExpr *constantExpr );
|
---|
107 | virtual void visit( SizeofExpr *sizeofExpr );
|
---|
108 | virtual void visit( AlignofExpr *alignofExpr );
|
---|
109 | virtual void visit( UntypedOffsetofExpr *offsetofExpr );
|
---|
110 | virtual void visit( OffsetofExpr *offsetofExpr );
|
---|
111 | virtual void visit( OffsetPackExpr *offsetPackExpr );
|
---|
112 | virtual void visit( AttrExpr *attrExpr );
|
---|
113 | virtual void visit( LogicalExpr *logicalExpr );
|
---|
114 | virtual void visit( ConditionalExpr *conditionalExpr );
|
---|
115 | virtual void visit( CommaExpr *commaExpr );
|
---|
116 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
|
---|
117 | virtual void visit( ConstructorExpr * ctorExpr );
|
---|
118 | virtual void visit( RangeExpr * rangeExpr );
|
---|
119 | virtual void visit( UntypedTupleExpr *tupleExpr );
|
---|
120 | virtual void visit( TupleExpr *tupleExpr );
|
---|
121 | virtual void visit( TupleIndexExpr *tupleExpr );
|
---|
122 | virtual void visit( TupleAssignExpr *tupleExpr );
|
---|
123 | virtual void visit( UniqueExpr *unqExpr );
|
---|
124 | virtual void visit( StmtExpr *stmtExpr );
|
---|
125 | virtual void visit( UntypedInitExpr *initExpr );
|
---|
126 |
|
---|
127 | /// Adds alternatives for anonymous members
|
---|
128 | void addAnonConversions( const Alternative & alt );
|
---|
129 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
|
---|
130 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
131 | /// Adds alternatives for member expressions where the left side has tuple type
|
---|
132 | void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
133 | /// Adds alternatives for offsetof expressions, given the base type and name of the member
|
---|
134 | template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
|
---|
135 | /// Takes a final result and checks if its assertions can be satisfied
|
---|
136 | template<typename OutputIterator>
|
---|
137 | void validateFunctionAlternative( const Alternative &func, ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out );
|
---|
138 | /// Finds matching alternatives for a function, given a set of arguments
|
---|
139 | template<typename OutputIterator>
|
---|
140 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const ExplodedArgs& args, OutputIterator out );
|
---|
141 | /// Checks if assertion parameters match for a new alternative
|
---|
142 | template< typename OutputIterator >
|
---|
143 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
|
---|
144 |
|
---|
145 | const SymTab::Indexer &indexer;
|
---|
146 | AltList alternatives;
|
---|
147 | const TypeEnvironment &env;
|
---|
148 | Type * targetType = nullptr;
|
---|
149 | }; // AlternativeFinder
|
---|
150 |
|
---|
151 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
|
---|
152 |
|
---|
153 | template< typename InputIterator, typename OutputIterator >
|
---|
154 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
155 | AltList alternatives;
|
---|
156 |
|
---|
157 | // select the alternatives that have the minimum parameter cost
|
---|
158 | Cost minCost = Cost::infinity;
|
---|
159 | for ( InputIterator i = begin; i != end; ++i ) {
|
---|
160 | if ( i->cost < minCost ) {
|
---|
161 | minCost = i->cost;
|
---|
162 | i->cost = i->cvtCost;
|
---|
163 | alternatives.clear();
|
---|
164 | alternatives.push_back( *i );
|
---|
165 | } else if ( i->cost == minCost ) {
|
---|
166 | i->cost = i->cvtCost;
|
---|
167 | alternatives.push_back( *i );
|
---|
168 | }
|
---|
169 | }
|
---|
170 | std::copy( alternatives.begin(), alternatives.end(), out );
|
---|
171 | }
|
---|
172 |
|
---|
173 | Cost sumCost( const AltList &in );
|
---|
174 | void printAlts( const AltList &list, std::ostream &os, unsigned int indentAmt = 0 );
|
---|
175 |
|
---|
176 | template< typename InputIterator >
|
---|
177 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
|
---|
178 | while ( begin != end ) {
|
---|
179 | result.simpleCombine( (*begin++).env );
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | } // namespace ResolvExpr
|
---|
184 |
|
---|
185 | // Local Variables: //
|
---|
186 | // tab-width: 4 //
|
---|
187 | // mode: c++ //
|
---|
188 | // compile-command: "make install" //
|
---|
189 | // End: //
|
---|