ROSE  0.9.9.139
variables.h
1 #ifndef VARIABLES_H
2 #define VARIABLES_H
3 
4 #include "genericDataflowCommon.h"
5 #include <map>
6 #include <vector>
7 #include <list>
8 #include <utility>
9 #include <set>
10 #include <string>
11 #include <iosfwd>
12 
13 class varID;
14 
15 /* #########################
16  ###### SHARED DATA ######
17  ######################### */
18 
19 
20 extern std::map<SgFunctionDefinition*, std::set<varID> > allVars;
21 extern std::map<SgFunctionDefinition*, std::set<varID> > activeVars;
22 
23 extern varID zeroVar;
24 extern varID oneVar;
25 extern varID allVar;
26 
27 /*// = true only after the variables module has been initialized
28 extern bool variables_module_initialized;
29 
30 void initVariables(SgProject* project);*/
31 
32 /* ##############################
33  ###### TYPE DEFINITIONS ######
34  ############################## */
35 
36 // virtual base class for other variable expressions
37 class variable
38 {
39  public:
40  //const char* str();
41  virtual std::string str() const = 0;
42 
43  virtual bool operator == (const variable &that) const = 0;
44  virtual bool operator < (const variable &that) const = 0;
45 
46  // returns the scope in which this array variable was declared
47  virtual SgScopeStatement* getScope() const=0;
48 
49  // returns a SgExpression that corresponds to an access of this variable
50  virtual SgExpression* toSgExpression() const=0;
51 
52  // returns true if this variable is global and false otherwise
53  virtual bool isGlobal() const=0;
54 
55  virtual ~variable() {}
56 };
57 
58 // type of variable ids, their sets and maps of sets
59 class varID : public variable
60 {
61  public:
62  std::vector<SgInitializedName *> components;
63  // Annotations associated with this variable
64  std::map<std::string, void*> annotations;
65  SgType* varType;
66  // special variables do not have associated SgInitializedName nodes and thus, we just give them a name
67  std::string name;
68 
69  varID(){ }
70 
71  varID(std::string name)
72  {
73  this->name = name;
74  varType = NULL;
75  genID();
76  }
77 
78  // creates a varID from an SgNode s.t. isValidVarExp(n) is true
79  varID(SgNode *n)
80  {
81  bool ret = init(n);
82  ROSE_ASSERT(ret);
83  genID();
84  }
85 
87  {
88  bool ret = init(name);
89  ROSE_ASSERT(ret);
90  genID();
91  }
92 
93  // pre-condition: isValidVarExp(refExp) evaluates to true
94  varID(const SgExpression *exp)
95  {
96  bool ret=init(exp);
97  ROSE_ASSERT(ret);
98  genID();
99  }
100 
101  varID(const varID& that) : variable()
102  {
103  init(that);
104  }
105 
106  // initializes this object from the given varID
107  bool init(const varID& that);
108 
109  // initializes this object from the given expression (assumed that isValidVarExp(n) is true)
110  // returns true on success, false on failure
111  bool init(SgNode *n);
112 
113  // initializes this object from the given expression (assumed that isValidVarExp(exp) is true)
114  // returns true on success, false on failure
115  bool init(const SgExpression *exp);
116 
117  // initializes this object from the given SgInitializedName (assumed that isValidVarExp(name) is true)
118  bool init(SgInitializedName* name);
119 
120  void operator = (const variable &that);
121 
122  // recursive function that pulls the SgInitializedNames of all the SgVarRefExps inside this SgDotExp
123  // returns true on success, false on failure
124  bool collectDotComponents(const SgDotExp* dotExp);
125 
126  // returns the scope in which this variable was declared
127  // for compound variables (i.e. those composed of dot expressions), it is the scope of the leftmost name
128  SgScopeStatement* getScope() const;
129 
130  // returns a SgExpression that corresponds to an access of this variable
131  SgExpression* toSgExpression() const;
132 
133 protected:
134  // returns a SgExpression that corresponds to an access of this variable, including only the components
135  // at or after the iterator rest into the components vector
136  SgExpression* toSgExpression_rec(std::vector<SgInitializedName *>::const_iterator rest) const;
137 
138 public:
139  // returns true if the given expression is one that can be represented as a variable in our representation
140  static bool isValidVarExp(const SgNode* exp);
141  static bool isValidVarExp(const SgExpression* exp);
142  static bool isValidVarExp(const SgInitializedName* exp);
143 
144 protected:
145  static bool isValidVarExp_rec(const SgExpression* exp);
146 
147 public:
148  void add(SgInitializedName *name);
149 
150  // Adds the given annotation to this variable. Returns true if this causes the variable's
151  // annotation state to change and false otherwise.
152  bool addAnnotation(const std::string& aName, void* annot);
153 
154  // Remove the given annotation from this variable. Returns true if this variable
155  // previously had this annotation and false otherwise.
156  bool remAnnotation(const std::string& aName);
157 
158  // Remove the given annotation from this variable. Returns true if this variable
159  // previously had this annotation and false otherwise.
160  bool remAllAnnotations();
161 
162  // Swap this variables annotations, removing [fromAnnotName -> fromAnnotVal] and replacing
163  // it with [toAnnotName -> toAnnotVal]. Returns true if this was successful (i.e. the variable
164  // does have the [fromAnnotName -> fromAnnotVal] annotation) and false otherwise.
165  // If the replacement occurs and this variable already has an annotation named
166  // toAnnotName, this annotation's value is replaced by toAnnotVal.
167  bool swapAnnotations(const std::string& fromAnnotName, void* fromAnnotVal,
168  const std::string& toAnnotName, void* toAnnotVal);
169 
170  // If this variable has the annotation with the given name, returns it. Otherwise, returns NULL.
171  void* getAnnotation(const std::string& aName) const;
172 
173  // If this variable has the annotation with the given name, returns true. Otherwise, returns false.
174  bool hasAnnotation(const std::string& aName) const;
175 
176  // If this variable has the annotation with ANY name in the given set, returns true. Otherwise, returns false.
177  bool hasAnyAnnotation(const std::set<std::string>& aNames) const;
178 
179  // If this variable has the annotation with EACH name in the given set, returns true. Otherwise, returns false.
180  bool hasAllAnnotations(const std::set<std::string>& aNames) const;
181 
182  // Returns the total number of annotations associated with this variable
183  int numAnnotations() const;
184 
185  // Returns the full map of all the annotations associated with this variable
186  const std::map<std::string, void*>& getAnnotations() const;
187 
188  /******************
189  *** COMPARISON ***
190  ******************/
191 
192 public:
193  bool equal(const varID& two) const;
194  bool lessThan(const varID& two) const;
195 
196  bool operator == (const variable &that) const;
197  bool operator != (const varID &that) const;
198  bool operator < (const variable &that) const;
199  bool operator > (const varID &that) const;
200  bool operator <= (const varID &that) const;
201  bool operator >= (const varID &that) const;
202 
203  /**************
204  *** OUTPUT ***
205  **************/
206 
207  // string representation of the variable reference.
208  // If noAnnot is true, excludes annotations from the name.
209  //const char* str();
210  std::string str() const;
211  std::string str(bool noAnnot) const;
212 
213  // string representation of the variable reference, with variable/field names augmented with
214  // the line numbers where they were defined. File names are omitted for brevity
215  //const char* str_linenum();
216  std::string str_linenum() const;
217 
218  // string representation of the variable reference, with the variable names replaced
219  // with the pointers to their declarations
220  //const char* str_ptr();
221  std::string str_ptr() const;
222 
223  /**********************
224  *** SEMANTINC INFO ***
225  **********************/
226 
227  // returns true if the last field in this variable has an array or pointer type
228  bool isArrayType() const;
229 
230  // returns true if any of its constituent SgInitializedNames are compiler-generated
231  bool isCompilerGenerated() const;
232 
233  // returns true if this variable is global and false otherwise
234  bool isGlobal() const;
235 
236  /*const bool isReferenceType()
237  {
238  return isSgReferenceType(components[components.size()-1]->get_typeptr());
239  }*/
240 
241  private:
242  // Unique ID generation and access functionality
243  // the maximum ID that has been generated for any variable
244  static long globalMaxID;
245  // the unique ID of this variable
246  long ID;
247  // generates a new ID for this variable and stores it in ID
248  void genID();
249 
250  public:
251  // returns this variable's ID
252  long getID() const;
253 };
254 
255 std::ostream &operator<<(std::ostream &stream, varID v);
256 std::ostream &operator<<(std::ostream &stream, const std::set<varID>::iterator& v);
257 //ostream &operator<<(ostream &stream, const std::set<varID>::const_iterator& v);
258 
259 //bool operator == ( const varID &one, const varID &two);
260 
261 // Variables are ordered in lexicographic order, with element-wise comparisons
262 // performed using the basic < operator for pointers.
263 //bool operator < ( const varID &one, const varID &two);
264 //bool operator > ( const varID &one, const varID &two);
265 
266 bool operator == (const varID &var, SgInitializedName* iName);
267 bool operator != (const varID &var, SgInitializedName* iName);
268 bool operator == (SgInitializedName* iName, const varID &var);
269 bool operator != (SgInitializedName* iName, const varID &var);
270 
271 bool operator == (const varID &var, SgExpression* expr);
272 bool operator != (const varID &var, SgExpression* expr);
273 bool operator == (SgExpression* expr, const varID &var);
274 bool operator != (SgExpression* expr, const varID &var);
275 
276 typedef std::set<varID, std::less<varID> > varIDSet;
277 typedef std::map<varID, varIDSet *> m_varID2setPtr;
278 typedef std::map<varID, quad> m_varID2quad;
279 typedef std::map<varID, std::string> m_varID2str;
280 typedef std::map<varID, bool> m_varID2bool;
281 typedef std::pair<varID, varID> varIDpair;
282 typedef std::list<varID> varIDlist;
283 typedef std::map<varID, m_varID2quad> m_varID2varID2quad;
284 
285 // type of number sets and maps of their sets
286 typedef std::set<quad, std::less<quad> > setQuad;
287 typedef std::map<quad, setQuad *> m_quad2setPtr;
288 
289 /* #################################
290  ###### FUNCTION PROTOTYPES ######
291  ################################# */
292 
293 // Returns the varID that corresponds to the given SgExpression
294 varID SgExpr2Var(const SgExpression* expr);
295 
296 // Returns true if the given expression can be interepreted as a concrete variable
297 bool isVarExpr(SgExpression* expr);
298 
299 // translate from an expression that uses a variable to that variable's unique id
300 // currently supported: a (SgVarRefExp), a.i (SgDotExp),
301 /*varID getVarReference( SgVarRefExp *exp );
302 varID getVarReference( SgDotExp * );
303 varID getVarReference( SgInitializedName * );*/
304 
305 // add the special variable Zero to the given set of variables
306 void addPredefinedVars(varIDSet &vars);
307 
308 // returns whether the variable with the given id exists in our set of interest
309 bool existsVariable( varID x, m_varID2str &vars2Name );
310 // returns whether the variable in the given reference expression exists in our
311 // set of interest
312 bool existsVariable( SgVarRefExp *x, m_varID2str &vars2Name );
313 
314 // returns whether this is a variable reference or dot expression (field reference)
315 bool isTypeConsidered( SgNode * exp);
316 
317 // returns the id in a variable reference or dot expression (field reference)
318 //varID getRefOfTypeConsidered( SgNode *exp );
319 
320 /*// mapping from variable declaration node pointer to string variable name for
321 // all the variables being used in the analysis of the current array
322 // (i.e. any variables involved in operations with the array, as
323 // defined in determineInterestSet()
324 m_varID2str vars2Name;
325 
326 // set of all 0, 1, -1, all constants being used in the program +/-1 and the sums of all the above
327 // may be used to make the widening operator more permissive
328 varIDSet allConstants;
329 */
330 
331 // returns a set of varIDs that correspond to all the variables (of SgDotExp/SgVarRefExp form)
332 // that were referenced in the given subtree
333 varIDSet getVarRefsInSubtree(SgNode* root);
334 
335 // returns a set of varIDs that correspond to all the variables (of SgDotExp/SgVarRefExp form)
336 // that were read from in the given subtree
337 varIDSet getReadVarRefsInSubtree(SgNode* root);
338 
339 // returns a set of varIDs that correspond to all the variables (of SgDotExp/SgVarRefExp form)
340 // that were written to in the given subtree
341 varIDSet getWriteVarRefsInSubtree(SgNode* root);
342 
343 // returns a set of varIDs that correspond to all the variables (of SgDotExp/SgVarRefExp form)
344 // that were referenced as arrays (i.e. var[i]) in the given subtree
345 varIDSet getArrayVarRefsInSubtree(SgNode* root);
346 
347 
348 class arrayElt : public variable
349 {
350  varID arrayVar;
351  std::list<SgExpression*>* indexExprs;
352 
353  public:
354  arrayElt(SgNode* expr);
355 
356  arrayElt(SgExpression* expr);
357 
358  std::string str() const;
359 
360  bool operator == (const variable &that_arg) const;
361 
362  bool operator < (const variable &that) const;
363 
364  // returns true if the given expression is one that can be represented as a variable in our representation
365  static bool isValidVarExp(const SgExpression* exp);
366 
367  // returns the scope in which this array variable was declared
368  // for compound variables (i.e. those composed of dot expressions), it is the scope of the leftmost name
369  SgScopeStatement* getScope() const;
370 
371  // returns a reference to the array variable, without any indexes
372  const varID& getArrayVar();
373 
374  // returns a reference to the index expressions
375  std::list<SgExpression*>* getIndexExprs();
376 
377  // returns a SgExpression that corresponds to an access of this variable
378  SgExpression* toSgExpression() const;
379 
380  protected:
381  // returns the SgPntrArrRefExp that corresponds to the index expressions in indexExprs that start with itIndexes
382  // precondition : itIndexes!=index.end()
383  SgPntrArrRefExp* toSgExpression_rec(std::list<SgExpression*>::reverse_iterator itIndexes) const;
384 
385  public:
386  // returns true if this variable is global and false otherwise
387  bool isGlobal() const;
388 };
389 
390 // returns a set of arrayElts that correspond to all the arrays (of SgDotExp/SgVarRefExp[SgExpression][][][]... form)
391 // that were referenced in the given subtree
392 std::set<arrayElt> getArrayRefsInSubtree(SgNode* root);
393 #endif
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope, etc.).
This class represents the base class for all types.
This class represents the notion of a declared variable.
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes, since similar to statement, expressions have a concrete location within the user's source code.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
This class represents the variable refernece in expressions.