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 | void find( Expression *expr, bool adjust = false, bool prune = true ); |
---|
37 | /// Calls find with the adjust flag set; adjustment turns array and function types into equivalent pointer types |
---|
38 | void findWithAdjustment( Expression *expr, bool prune = true ); |
---|
39 | AltList &get_alternatives() { return alternatives; } |
---|
40 | |
---|
41 | // make this look like an STL container so that we can apply generic algorithms |
---|
42 | typedef Alternative value_type; |
---|
43 | typedef AltList::iterator iterator; |
---|
44 | typedef AltList::const_iterator const_iterator; |
---|
45 | AltList::iterator begin() { return alternatives.begin(); } |
---|
46 | AltList::iterator end() { return alternatives.end(); } |
---|
47 | AltList::const_iterator begin() const { return alternatives.begin(); } |
---|
48 | AltList::const_iterator end() const { return alternatives.end(); } |
---|
49 | |
---|
50 | const SymTab::Indexer &get_indexer() const { return indexer; } |
---|
51 | const TypeEnvironment &get_environ() const { return env; } |
---|
52 | private: |
---|
53 | virtual void visit( ApplicationExpr *applicationExpr ); |
---|
54 | virtual void visit( UntypedExpr *untypedExpr ); |
---|
55 | virtual void visit( AddressExpr *addressExpr ); |
---|
56 | virtual void visit( LabelAddressExpr *labelExpr ); |
---|
57 | virtual void visit( CastExpr *castExpr ); |
---|
58 | virtual void visit( VirtualCastExpr *castExpr ); |
---|
59 | virtual void visit( UntypedMemberExpr *memberExpr ); |
---|
60 | virtual void visit( MemberExpr *memberExpr ); |
---|
61 | virtual void visit( NameExpr *variableExpr ); |
---|
62 | virtual void visit( VariableExpr *variableExpr ); |
---|
63 | virtual void visit( ConstantExpr *constantExpr ); |
---|
64 | virtual void visit( SizeofExpr *sizeofExpr ); |
---|
65 | virtual void visit( AlignofExpr *alignofExpr ); |
---|
66 | virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
67 | virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
68 | virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
69 | virtual void visit( AttrExpr *attrExpr ); |
---|
70 | virtual void visit( LogicalExpr *logicalExpr ); |
---|
71 | virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
72 | virtual void visit( CommaExpr *commaExpr ); |
---|
73 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ); |
---|
74 | virtual void visit( ConstructorExpr * ctorExpr ); |
---|
75 | virtual void visit( RangeExpr * rangeExpr ); |
---|
76 | virtual void visit( UntypedTupleExpr *tupleExpr ); |
---|
77 | virtual void visit( TupleExpr *tupleExpr ); |
---|
78 | virtual void visit( TupleIndexExpr *tupleExpr ); |
---|
79 | virtual void visit( TupleAssignExpr *tupleExpr ); |
---|
80 | virtual void visit( UniqueExpr *unqExpr ); |
---|
81 | virtual void visit( StmtExpr *stmtExpr ); |
---|
82 | virtual void visit( UntypedInitExpr *initExpr ); |
---|
83 | /// Runs a new alternative finder on each element in [begin, end) |
---|
84 | /// and writes each alternative finder to out. |
---|
85 | template< typename InputIterator, typename OutputIterator > |
---|
86 | void findSubExprs( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
87 | |
---|
88 | /// Adds alternatives for anonymous members |
---|
89 | void addAnonConversions( const Alternative & alt ); |
---|
90 | /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member |
---|
91 | template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); |
---|
92 | /// Adds alternatives for member expressions where the left side has tuple type |
---|
93 | void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ); |
---|
94 | /// Adds alternatives for offsetof expressions, given the base type and name of the member |
---|
95 | template< typename StructOrUnionType > void addOffsetof( StructOrUnionType *aggInst, const std::string &name ); |
---|
96 | bool instantiateFunction( std::list< DeclarationWithType* >& formals, const AltList &actuals, bool isVarArgs, OpenVarSet& openVars, TypeEnvironment &resultEnv, AssertionSet &resultNeed, AssertionSet &resultHave, AltList & out ); |
---|
97 | template< typename OutputIterator > |
---|
98 | void makeFunctionAlternatives( const Alternative &func, FunctionType *funcType, const AltList &actualAlt, OutputIterator out ); |
---|
99 | template< typename OutputIterator > |
---|
100 | void inferParameters( const AssertionSet &need, AssertionSet &have, const Alternative &newAlt, OpenVarSet &openVars, OutputIterator out ); |
---|
101 | void resolveAttr( DeclarationWithType *funcDecl, FunctionType *function, Type *argType, const TypeEnvironment &env ); |
---|
102 | |
---|
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 | |
---|
133 | template< typename InputIterator > |
---|
134 | void simpleCombineEnvironments( InputIterator begin, InputIterator end, TypeEnvironment &result ) { |
---|
135 | while ( begin != end ) { |
---|
136 | result.simpleCombine( (*begin++).env ); |
---|
137 | } |
---|
138 | } |
---|
139 | } // namespace ResolvExpr |
---|
140 | |
---|
141 | // Local Variables: // |
---|
142 | // tab-width: 4 // |
---|
143 | // mode: c++ // |
---|
144 | // compile-command: "make install" // |
---|
145 | // End: // |
---|