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