ROSE  0.11.2.0
MatchOperation.h
1 #ifndef MATCHOPERATION_H
2 #define MATCHOPERATION_H
3 
4 /*************************************************************
5  * Author : Markus Schordan *
6  *************************************************************/
7 
8 #include <string>
9 #include <list>
10 #include <sstream>
11 #include <iostream>
12 #include <set>
13 #include <map>
14 #include "RoseAst.h"
15 
16 typedef std::map<std::string,SgNode*> SingleMatchVarBindings;
18 typedef std::list<SingleMatchMarkedLocation> SingleMatchMarkedLocations;
19 typedef std::list<SingleMatchVarBindings> MatchResult;
20 
21 class MatchOpSequence;
22 
24  SingleMatchVarBindings singleMatchVarBindings;
25  SingleMatchMarkedLocations singleMatchMarkedLocations;
26  bool success;
27  void clear();
28  bool isSMRMarkedLocation(RoseAst::iterator& i);
29 };
30 
31 class MatchStatus {
32  public:
33  MatchStatus():debug(false),_allMatchVarBindings(0){
34  resetAllMatchVarBindings();
35  resetAllMarkedLocations();
36  }
37  ~MatchStatus() {
38  delete _allMatchVarBindings;
39  }
40  enum PatternMatchMode {MATCHMODE_SHALLOW, MATCHMODE_DEEP, MATCHMODE_SINGLE};
41  enum CheckNodeMode {NODECHECKMODE_TYPEID,NODECHECKMODE_VARIANT};
42  bool isMarkedLocationAddress(RoseAst::iterator& i);
43  void resetAllMatchVarBindings();
44  void resetAllMarkedLocations();
45  public:
46  bool debug;
47  void mergeOtherStatus(MatchStatus& other);
48  void mergeSingleMatchResult(SingleMatchResult& other);
49  std::list<SingleMatchVarBindings>* _allMatchVarBindings;
50  SingleMatchMarkedLocations _allMatchMarkedLocations;
51 
52  /* adds a single var binding to map of var bindings */
53  void addVarBinding(std::string varname,SgNode* node);
54  /* adds a single marked location to set of marked locations */
55  void addMarkedLocation(SingleMatchMarkedLocation locIter);
56 
57  /* updates state to include new match result and resets all data
58  to be ready for new match */
59  void commitSingleMatchResult();
60  private:
61  SingleMatchResult current_smr;
62  bool _keepMarkedLocations;
63 };
64 
66  public:
67  virtual std::string toString()=0;
68  virtual bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
69 };
70 
71 class MatchOpSequence : public std::list<MatchOperation*>{
72  // we are using default std::list constructors
73  public:
74  std::string toString();
75  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
76 };
77 
78 class MatchOpOr : public MatchOperation {
79  public:
80  MatchOpOr(MatchOpSequence* l, MatchOpSequence* r):_left(l),_right(r){}
81  std::string toString();
82  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
83  private:
84  MatchOpSequence* _left;
85  MatchOpSequence* _right;
86 };
87 
89  public:
90  MatchOpVariableAssignment(std::string varName);
91  std::string toString();
92  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
93  private:
94  std::string _varName;
95 };
96 
98  public:
99  MatchOpCheckNode(std::string nodename);
100  std::string toString();
101  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
102  private:
103  std::string _nodename;
104 };
105 
107  public:
108  MatchOpCheckNodeSet(std::string nodenameset);
109  std::string toString();
110  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
111  private:
112  std::string _nodenameset;
113 };
114 
116  public:
117  MatchOpArityCheck(size_t arity);
118  MatchOpArityCheck(size_t minarity, size_t maxarity);
119  std::string toString();
120  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
121  private:
122  size_t _minarity;
123  size_t _maxarity;
124 };
125 
126 /*
127 for (size_t idx = 0; idx < numberOfSuccessors; idx++)
128 child = node->get_traversalSuccessorByIndex(idx);
129 node->get_numberOfTraversalSuccessors();
130 */
131 
132 /* This operation skips the subtree of the current node. This is equivalent
133 to assuming that this node has no children. It also performs one forward operation.
134 */
135 
137  public:
138  MatchOpForward();
139  std::string toString();
140  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
141 };
142 
144  public:
146  std::string toString();
147  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
148  private:
149 };
150 
152  public:
153  MatchOpMarkNode();
154  std::string toString();
155  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
156  private:
157 };
158 
160  public:
162  std::string toString();
163  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
164  private:
165 };
166 
168  public:
169  MatchOpDotDot();
170  std::string toString();
171  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb);
172  private:
173 };
174 typedef MatchOpSequence MatchOperationList; // TODO: eliminate type alias
175 typedef std::list<MatchOperationList*> MatchOperationListList;
176 
177 //#include "matcherparser.h" // we are using yytokentype in MatchOpBinaryOp
178 // TODO: fake type for testing
179 
181  public:
182  MatchOpBinaryOp(int op,MatchOperation* l,MatchOperation* r):_op(op),_left(l),_right(r) {}
183  std::string toString() { return "binop()";}
184  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb) {
185  switch(_op) {
186 #if 0
187  case C_NEQ: return _left->performOperation(status,i,vb) != _right->performOperation(status,i,vb);
188  case C_EQ: return _left->performOperation(status,i,vb) == _right->performOperation(status,i,vb);
189 #endif
190  default: throw "Error: unknown operation in where-expression.";
191  }
192  }
193  private:
194  int _op;
195  MatchOperation* _left;
196  MatchOperation* _right;
197 };
198 
200  public:
201  MatchOpUnaryOp(int,MatchOperation* o) {}
202  std::string toString() { return "not-implemented-yet";}
203 };
205  public:
206  MatchOpConstant(int) {}
207  std::string toString() { return "not-implemented-yet";}
208 };
209 
210 
212  public:
213  MatchOpAccessVariable(std::string) {}
214  std::string toString() { return "not-implemented-yet";}
215 };
217  public:
218  MatchOpAccessRoseAstAttribute(std::string, std::string) {}
219  std::string toString() { return "not-implemented-yet";}
220 };
222  public:
223  MatchOpAccessUserAstAttribute(std::string, std::string) {}
224  std::string toString() { return "not-implemented-yet";}
225 };
226 
227 #endif
AST iterator.
Definition: RoseAst.h:54
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9311