ROSE  0.11.145.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 #if 0 // [Robb Matzke 2021-03-17]: unused
63  bool _keepMarkedLocations;
64 #endif
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) // blame schordan
185 #if 0 // [Robb Matzke 2021-03-17]: unused
186  ,_left(l),_right(r)
187 #endif
188  {}
189  std::string toString() { return "binop()";}
190  bool performOperation(MatchStatus& status, RoseAst::iterator& i, SingleMatchResult& vb) {
191  switch(_op) {
192 #if 0
193  case C_NEQ: return _left->performOperation(status,i,vb) != _right->performOperation(status,i,vb);
194  case C_EQ: return _left->performOperation(status,i,vb) == _right->performOperation(status,i,vb);
195 #endif
196  default: throw "Error: unknown operation in where-expression.";
197  }
198  }
199  private:
200  int _op;
201 #if 0 // [Robb Matzke 2021-03-17]: unused
202  MatchOperation* _left;
203  MatchOperation* _right;
204 #endif
205 };
206 
208  public:
209  MatchOpUnaryOp(int,MatchOperation* o) {}
210  std::string toString() { return "not-implemented-yet";}
211 };
213  public:
214  MatchOpConstant(int) {}
215  std::string toString() { return "not-implemented-yet";}
216 };
217 
218 
220  public:
221  MatchOpAccessVariable(std::string) {}
222  std::string toString() { return "not-implemented-yet";}
223 };
225  public:
226  MatchOpAccessRoseAstAttribute(std::string, std::string) {}
227  std::string toString() { return "not-implemented-yet";}
228 };
230  public:
231  MatchOpAccessUserAstAttribute(std::string, std::string) {}
232  std::string toString() { return "not-implemented-yet";}
233 };
234 
235 #endif
AST iterator.
Definition: RoseAst.h:54
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846