1 | #ifndef _TUPLE_ASSIGNMENT_H_ |
---|
2 | #define _TUPLE_ASSIGNMENT_H_ |
---|
3 | |
---|
4 | #include <string> |
---|
5 | #include <vector> |
---|
6 | #include "ResolvExpr/AlternativeFinder.h" |
---|
7 | |
---|
8 | #include "SynTree/Expression.h" |
---|
9 | #include "SynTree/Declaration.h" |
---|
10 | #include "SynTree/Type.h" |
---|
11 | |
---|
12 | namespace Tuples { |
---|
13 | class TupleAssignSpotter { |
---|
14 | public: |
---|
15 | // dispatcher for Tuple (multiple and mass) assignment operations |
---|
16 | TupleAssignSpotter( ResolvExpr::AlternativeFinder * ); |
---|
17 | ~TupleAssignSpotter() { delete matcher; matcher = 0; } |
---|
18 | |
---|
19 | bool pointsToTuple( Expression * ); |
---|
20 | static bool isTupleVar( DeclarationWithType * ); |
---|
21 | bool isTuple( Expression *, bool isRight = false ); |
---|
22 | bool isMVR( Expression * ); |
---|
23 | bool isTupleAssignment( UntypedExpr *, std::list<ResolvExpr::AltList> & ); |
---|
24 | bool match(); |
---|
25 | |
---|
26 | private: |
---|
27 | // records for assignment generation |
---|
28 | class Options { |
---|
29 | public: |
---|
30 | void add_option( ResolvExpr::AltList &opt ); |
---|
31 | std::list< ResolvExpr::AltList > get_best(); |
---|
32 | void print( std::ostream & ); |
---|
33 | int size() const { return options.size(); } |
---|
34 | ResolvExpr::AltList get_option( std::list< ResolvExpr::AltList >::size_type index ); |
---|
35 | |
---|
36 | // should really use the one in ResolvExpr/AlternativeFinder, but it's too coupled with the object |
---|
37 | template< typename InputIterator, typename OutputIterator > |
---|
38 | void findMinCost( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
39 | |
---|
40 | template< typename InputIterator, typename OutputIterator > |
---|
41 | void lift_intersection( InputIterator begin, InputIterator end, OutputIterator out ); |
---|
42 | private: |
---|
43 | std::list< ResolvExpr::AltList > options; |
---|
44 | std::vector< std::vector< ResolvExpr::Cost > > costMatrix; |
---|
45 | }; |
---|
46 | |
---|
47 | class Matcher { |
---|
48 | public: |
---|
49 | Matcher( /*TupleAssignSpotter &spot, */Expression *_lhs, Expression *_rhs ); |
---|
50 | virtual ~Matcher() {} |
---|
51 | virtual bool match( std::list< Expression * > &out ) = 0; |
---|
52 | virtual bool solve( std::list< Expression * > &assigns ) = 0; |
---|
53 | static UntypedExpr *createAssgn( Expression *left, Expression *right ); |
---|
54 | protected: |
---|
55 | Matcher() /*: own_spotter( TupleAssignSpotter(0) ) */{} |
---|
56 | void init(/* TupleAssignSpotter &, */Expression *_lhs, Expression *_rhs ); |
---|
57 | std::list< Expression * > lhs, rhs; |
---|
58 | //TupleAssignSpotter &own_spotter; |
---|
59 | }; |
---|
60 | |
---|
61 | class MassAssignMatcher : public Matcher { |
---|
62 | public: |
---|
63 | MassAssignMatcher( Expression *_lhs, Expression *_rhs ) : Matcher( _lhs, _rhs ) { |
---|
64 | rhs.push_back( _rhs ); |
---|
65 | } |
---|
66 | virtual bool match( std::list< Expression * > &out ); |
---|
67 | virtual bool solve( std::list< Expression * > &assigns ); |
---|
68 | private: |
---|
69 | //std::vector< ResolvExpr::AltList > optMass; |
---|
70 | }; |
---|
71 | |
---|
72 | class MultipleAssignMatcher : public Matcher { |
---|
73 | public: |
---|
74 | MultipleAssignMatcher( Expression *_lhs, Expression *_rhs ); |
---|
75 | virtual bool match( std::list< Expression * > &out ); |
---|
76 | virtual bool solve( std::list< Expression * > &assigns ); |
---|
77 | private: |
---|
78 | //Options options; |
---|
79 | }; |
---|
80 | |
---|
81 | friend class Matcher; |
---|
82 | |
---|
83 | ResolvExpr::AlternativeFinder *currentFinder; |
---|
84 | //std::list<Expression *> rhs, lhs; |
---|
85 | Expression *rhs, *lhs; |
---|
86 | Matcher *matcher; |
---|
87 | bool hasMatched; |
---|
88 | Options options; |
---|
89 | std::vector< ResolvExpr::AltList > optMass; |
---|
90 | }; |
---|
91 | |
---|
92 | |
---|
93 | ResolvExpr::Cost extract_cost( ResolvExpr::Alternative & ); |
---|
94 | |
---|
95 | template< typename InputIterator, typename OutputIterator > |
---|
96 | void |
---|
97 | findMinCostAlt( InputIterator begin, InputIterator end, OutputIterator out ) |
---|
98 | { |
---|
99 | using namespace ResolvExpr; |
---|
100 | AltList alternatives; |
---|
101 | |
---|
102 | // select the alternatives that have the minimum parameter cost |
---|
103 | Cost minCost = Cost::infinity; |
---|
104 | for( AltList::iterator i = begin; i != end; ++i ) { |
---|
105 | if( i->cost < minCost ) { |
---|
106 | minCost = i->cost; |
---|
107 | i->cost = i->cvtCost; |
---|
108 | alternatives.clear(); |
---|
109 | alternatives.push_back( *i ); |
---|
110 | } else if( i->cost == minCost ) { |
---|
111 | i->cost = i->cvtCost; |
---|
112 | alternatives.push_back( *i ); |
---|
113 | } |
---|
114 | } |
---|
115 | std::copy( alternatives.begin(), alternatives.end(), out ); |
---|
116 | } |
---|
117 | |
---|
118 | } // namespace Tuples |
---|
119 | |
---|
120 | |
---|
121 | #endif // #ifndef _TUPLE_ASSIGNMENT_H_ |
---|
122 | |
---|
123 | /* |
---|
124 | Local Variables: |
---|
125 | mode: c++ |
---|
126 | End: |
---|
127 | */ |
---|