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