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