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 "ResolvExpr/Cost.h" // for Cost, Cost::infinity
|
---|
24 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet
|
---|
25 | #include "SynTree/Visitor.h" // for Visitor
|
---|
26 | #include "SynTree/SynTree.h" // for Visitor Nodes
|
---|
27 |
|
---|
28 | namespace SymTab {
|
---|
29 | class Indexer;
|
---|
30 | } // namespace SymTab
|
---|
31 |
|
---|
32 | namespace ResolvExpr {
|
---|
33 | class AlternativeFinder : public Visitor {
|
---|
34 | public:
|
---|
35 | AlternativeFinder( const SymTab::Indexer &indexer, const TypeEnvironment &env );
|
---|
36 |
|
---|
37 | AlternativeFinder( const AlternativeFinder& o )
|
---|
38 | : indexer(o.indexer), alternatives(o.alternatives), env(o.env),
|
---|
39 | targetType(o.targetType) {}
|
---|
40 |
|
---|
41 | AlternativeFinder( AlternativeFinder&& o )
|
---|
42 | : indexer(o.indexer), alternatives(std::move(o.alternatives)), env(o.env),
|
---|
43 | targetType(o.targetType) {}
|
---|
44 |
|
---|
45 | AlternativeFinder& operator= ( const AlternativeFinder& o ) {
|
---|
46 | if (&o == this) return *this;
|
---|
47 |
|
---|
48 | // horrific nasty hack to rebind references...
|
---|
49 | alternatives.~AltList();
|
---|
50 | new(this) AlternativeFinder(o);
|
---|
51 | return *this;
|
---|
52 | }
|
---|
53 |
|
---|
54 | AlternativeFinder& operator= ( AlternativeFinder&& o ) {
|
---|
55 | if (&o == this) return *this;
|
---|
56 |
|
---|
57 | // horrific nasty hack to rebind references...
|
---|
58 | alternatives.~AltList();
|
---|
59 | new(this) AlternativeFinder(std::move(o));
|
---|
60 | return *this;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void find( Expression *expr, bool adjust = false, bool prune = true, bool failFast = true );
|
---|
64 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types
|
---|
65 | void findWithAdjustment( Expression *expr );
|
---|
66 | /// Calls find with the adjust flag set and prune flag unset; pruning ensures there is at most one alternative per result type
|
---|
67 | void findWithoutPrune( Expression *expr );
|
---|
68 | /// Calls find with the adjust and prune flags set, failFast flags unset; fail fast ensures that there is at least one resulting alternative
|
---|
69 | void maybeFind( Expression *expr );
|
---|
70 | AltList &get_alternatives() { return alternatives; }
|
---|
71 |
|
---|
72 | // make this look like an STL container so that we can apply generic algorithms
|
---|
73 | typedef Alternative value_type;
|
---|
74 | typedef AltList::iterator iterator;
|
---|
75 | typedef AltList::const_iterator const_iterator;
|
---|
76 | AltList::iterator begin() { return alternatives.begin(); }
|
---|
77 | AltList::iterator end() { return alternatives.end(); }
|
---|
78 | AltList::const_iterator begin() const { return alternatives.begin(); }
|
---|
79 | AltList::const_iterator end() const { return alternatives.end(); }
|
---|
80 |
|
---|
81 | const SymTab::Indexer &get_indexer() const { return indexer; }
|
---|
82 | const TypeEnvironment &get_environ() const { return env; }
|
---|
83 |
|
---|
84 | /// Runs a new alternative finder on each element in [begin, end)
|
---|
85 | /// and writes each alternative finder to out.
|
---|
86 | template< typename InputIterator, typename OutputIterator >
|
---|
87 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out );
|
---|
88 | private:
|
---|
89 | virtual void visit( ApplicationExpr *applicationExpr );
|
---|
90 | virtual void visit( UntypedExpr *untypedExpr );
|
---|
91 | virtual void visit( AddressExpr *addressExpr );
|
---|
92 | virtual void visit( LabelAddressExpr *labelExpr );
|
---|
93 | virtual void visit( CastExpr *castExpr );
|
---|
94 | virtual void visit( VirtualCastExpr *castExpr );
|
---|
95 | virtual void visit( UntypedMemberExpr *memberExpr );
|
---|
96 | virtual void visit( MemberExpr *memberExpr );
|
---|
97 | virtual void visit( NameExpr *variableExpr );
|
---|
98 | virtual void visit( VariableExpr *variableExpr );
|
---|
99 | virtual void visit( ConstantExpr *constantExpr );
|
---|
100 | virtual void visit( SizeofExpr *sizeofExpr );
|
---|
101 | virtual void visit( AlignofExpr *alignofExpr );
|
---|
102 | virtual void visit( UntypedOffsetofExpr *offsetofExpr );
|
---|
103 | virtual void visit( OffsetofExpr *offsetofExpr );
|
---|
104 | virtual void visit( OffsetPackExpr *offsetPackExpr );
|
---|
105 | virtual void visit( AttrExpr *attrExpr );
|
---|
106 | virtual void visit( LogicalExpr *logicalExpr );
|
---|
107 | virtual void visit( ConditionalExpr *conditionalExpr );
|
---|
108 | virtual void visit( CommaExpr *commaExpr );
|
---|
109 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
|
---|
110 | virtual void visit( ConstructorExpr * ctorExpr );
|
---|
111 | virtual void visit( RangeExpr * rangeExpr );
|
---|
112 | virtual void visit( UntypedTupleExpr *tupleExpr );
|
---|
113 | virtual void visit( TupleExpr *tupleExpr );
|
---|
114 | virtual void visit( TupleIndexExpr *tupleExpr );
|
---|
115 | virtual void visit( TupleAssignExpr *tupleExpr );
|
---|
116 | virtual void visit( UniqueExpr *unqExpr );
|
---|
117 | virtual void visit( StmtExpr *stmtExpr );
|
---|
118 | virtual void visit( UntypedInitExpr *initExpr );
|
---|
119 |
|
---|
120 | /// Adds alternatives for anonymous members
|
---|
121 | void addAnonConversions( const Alternative & alt );
|
---|
122 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
|
---|
123 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
124 | /// Adds alternatives for member expressions where the left side has tuple type
|
---|
125 | void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
|
---|
126 | /// Adds alternatives for offsetof expressions, given the base type and name of the member
|
---|
127 | template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name );
|
---|
128 | template<typename OutputIterator>
|
---|
129 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const std::vector< AlternativeFinder >& args, OutputIterator out );
|
---|
130 | template< typename OutputIterator >
|
---|
131 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out );
|
---|
132 | void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env );
|
---|
133 |
|
---|
134 | const SymTab::Indexer &indexer;
|
---|
135 | AltList alternatives;
|
---|
136 | const TypeEnvironment &env;
|
---|
137 | Type * targetType = nullptr;
|
---|
138 | }; // AlternativeFinder
|
---|
139 |
|
---|
140 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer, TypeEnvironment &env );
|
---|
141 | void referenceToRvalueConversion( Expression *& expr );
|
---|
142 |
|
---|
143 | template< typename InputIterator, typename OutputIterator >
|
---|
144 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ) {
|
---|
145 | AltList alternatives;
|
---|
146 |
|
---|
147 | // select the alternatives that have the minimum parameter cost
|
---|
148 | Cost minCost = Cost::infinity;
|
---|
149 | for ( InputIterator i = begin; i != end; ++i ) {
|
---|
150 | if ( i->cost < minCost ) {
|
---|
151 | minCost = i->cost;
|
---|
152 | i->cost = i->cvtCost;
|
---|
153 | alternatives.clear();
|
---|
154 | alternatives.push_back( *i );
|
---|
155 | } else if ( i->cost == minCost ) {
|
---|
156 | i->cost = i->cvtCost;
|
---|
157 | alternatives.push_back( *i );
|
---|
158 | }
|
---|
159 | }
|
---|
160 | std::copy( alternatives.begin(), alternatives.end(), out );
|
---|
161 | }
|
---|
162 |
|
---|
163 | Cost sumCost( const AltList &in );
|
---|
164 |
|
---|
165 | template< typename InputIterator >
|
---|
166 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) {
|
---|
167 | while ( begin != end ) {
|
---|
168 | result.simpleCombine( (*begin++).env );
|
---|
169 | }
|
---|
170 | }
|
---|
171 | } // namespace ResolvExpr
|
---|
172 |
|
---|
173 | // Local Variables: //
|
---|
174 | // tab-width: 4 //
|
---|
175 | // mode: c++ //
|
---|
176 | // compile-command: "make install" //
|
---|
177 | // End: //
|
---|