Renamed inst_const_iterator -> const_inst_iterator
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/ConstantVals.h - Constant Value nodes ---------------*- C++ -*--=//
2 //
3 // This file contains the declarations for the Constant class and all of
4 // its subclasses, which represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
10
11 #include "llvm/User.h"
12 #include "Support/DataTypes.h"
13
14 class ArrayType;
15 class StructType;
16 class PointerType;
17
18 //===----------------------------------------------------------------------===//
19 //                                Constant Class
20 //===----------------------------------------------------------------------===//
21
22 class Constant : public User {
23 protected:
24   inline Constant(const Type *Ty) : User(Ty, Value::ConstantVal) {}
25   ~Constant() {}
26
27   // destroyConstant - Called if some element of this constant is no longer
28   // valid.  At this point only other constants may be on the use_list for this
29   // constant.  Any constants on our Use list must also be destroy'd.  The
30   // implementation must be sure to remove the constant from the list of
31   // available cached constants.  Implementations should call
32   // destroyConstantImpl as the last thing they do, to destroy all users and
33   // delete this.
34   //
35   virtual void destroyConstant() { assert(0 && "Not reached!"); }
36   void destroyConstantImpl();
37 public:
38   // Specialize setName to handle symbol table majik...
39   virtual void setName(const string &name, SymbolTable *ST = 0);
40
41   virtual string getStrValue() const = 0;
42
43   // Static constructor to get a '0' constant of arbitrary type...
44   static Constant *getNullConstant(const Type *Ty);
45
46   // isNullValue - Return true if this is the value that would be returned by
47   // getNullConstant.
48   virtual bool isNullValue() const = 0;
49
50   // Methods for support type inquiry through isa, cast, and dyn_cast:
51   static inline bool classof(const Constant *) { return true; }
52   static inline bool classof(const Value *V) {
53     return V->getValueType() == Value::ConstantVal;
54   }
55 };
56
57
58
59 //===----------------------------------------------------------------------===//
60 //              Classes to represent constant pool variable defs
61 //===----------------------------------------------------------------------===//
62
63 //===---------------------------------------------------------------------------
64 // ConstantBool - Boolean Values
65 //
66 class ConstantBool : public Constant {
67   bool Val;
68   ConstantBool(const ConstantBool &);     // DO NOT IMPLEMENT
69   ConstantBool(bool V);
70   ~ConstantBool() {}
71 public:
72   static ConstantBool *True, *False;  // The True & False values
73
74   // Factory objects - Return objects of the specified value
75   static ConstantBool *get(bool Value) { return Value ? True : False; }
76   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
77
78   // inverted - Return the opposite value of the current value.
79   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
80
81   virtual string getStrValue() const;
82   inline bool getValue() const { return Val; }
83
84   // isNullValue - Return true if this is the value that would be returned by
85   // getNullConstant.
86   virtual bool isNullValue() const { return this == False; }
87
88   // Methods for support type inquiry through isa, cast, and dyn_cast:
89   static inline bool classof(const ConstantBool *) { return true; }
90   static bool classof(const Constant *CPV) {
91     return (CPV == True) | (CPV == False);
92   }
93   static inline bool classof(const Value *V) {
94     return isa<Constant>(V) && classof(cast<Constant>(V));
95   }
96 };
97
98
99 //===---------------------------------------------------------------------------
100 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
101 // with integral constants easier.
102 //
103 class ConstantInt : public Constant {
104 protected:
105   union {
106     int64_t  Signed;
107     uint64_t Unsigned;
108   } Val;
109   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
110   ConstantInt(const Type *Ty, uint64_t V);
111   ~ConstantInt() {}
112 public:
113   // equalsInt - Provide a helper method that can be used to determine if the 
114   // constant contained within is equal to a constant.  This only works for very
115   // small values, because this is all that can be represented with all types.
116   //
117   bool equalsInt(unsigned char V) const {
118     assert(V <= 127 &&
119            "equals: Can only be used with very small positive constants!");
120     return Val.Unsigned == V;
121   }
122
123   // ConstantInt::get static method: return a constant pool int with the
124   // specified value.  as above, we work only with very small values here.
125   //
126   static ConstantInt *get(const Type *Ty, unsigned char V);
127
128   // isNullValue - Return true if this is the value that would be returned by
129   // getNullConstant.
130   virtual bool isNullValue() const { return Val.Unsigned == 0; }
131
132   // Methods for support type inquiry through isa, cast, and dyn_cast:
133   static inline bool classof(const ConstantInt *) { return true; }
134   static bool classof(const Constant *CPV);  // defined in CPV.cpp
135   static inline bool classof(const Value *V) {
136     return isa<Constant>(V) && classof(cast<Constant>(V));
137   }
138 };
139
140
141 //===---------------------------------------------------------------------------
142 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
143 //
144 class ConstantSInt : public ConstantInt {
145   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
146 protected:
147   ConstantSInt(const Type *Ty, int64_t V);
148   ~ConstantSInt() {}
149 public:
150   static ConstantSInt *get(const Type *Ty, int64_t V);
151
152   virtual string getStrValue() const;
153
154   static bool isValueValidForType(const Type *Ty, int64_t V);
155   inline int64_t getValue() const { return Val.Signed; }
156
157   // Methods for support type inquiry through isa, cast, and dyn_cast:
158   static inline bool classof(const ConstantSInt *) { return true; }
159   static bool classof(const Constant *CPV);  // defined in CPV.cpp
160   static inline bool classof(const Value *V) {
161     return isa<Constant>(V) && classof(cast<Constant>(V));
162   }
163 };
164
165 //===---------------------------------------------------------------------------
166 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
167 //
168 class ConstantUInt : public ConstantInt {
169   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
170 protected:
171   ConstantUInt(const Type *Ty, uint64_t V);
172   ~ConstantUInt() {}
173 public:
174   static ConstantUInt *get(const Type *Ty, uint64_t V);
175
176   virtual string getStrValue() const;
177
178   static bool isValueValidForType(const Type *Ty, uint64_t V);
179   inline uint64_t getValue() const { return Val.Unsigned; }
180
181   // Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const ConstantUInt *) { return true; }
183   static bool classof(const Constant *CPV);  // defined in CPV.cpp
184   static inline bool classof(const Value *V) {
185     return isa<Constant>(V) && classof(cast<Constant>(V));
186   }
187 };
188
189
190 //===---------------------------------------------------------------------------
191 // ConstantFP - Floating Point Values [float, double]
192 //
193 class ConstantFP : public Constant {
194   double Val;
195   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
196 protected:
197   ConstantFP(const Type *Ty, double V);
198   ~ConstantFP() {}
199 public:
200   static ConstantFP *get(const Type *Ty, double V);
201
202   virtual string getStrValue() const;
203
204   static bool isValueValidForType(const Type *Ty, double V);
205   inline double getValue() const { return Val; }
206
207   // isNullValue - Return true if this is the value that would be returned by
208   // getNullConstant.
209   virtual bool isNullValue() const { return Val == 0; }
210
211   // Methods for support type inquiry through isa, cast, and dyn_cast:
212   static inline bool classof(const ConstantFP *) { return true; }
213   static bool classof(const Constant *CPV);  // defined in CPV.cpp
214   static inline bool classof(const Value *V) {
215     return isa<Constant>(V) && classof(cast<Constant>(V));
216   }
217 };
218
219
220 //===---------------------------------------------------------------------------
221 // ConstantArray - Constant Array Declarations
222 //
223 class ConstantArray : public Constant {
224   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
225 protected:
226   ConstantArray(const ArrayType *T, const vector<Constant*> &Val);
227   ~ConstantArray() {}
228
229   virtual void destroyConstant();
230 public:
231   static ConstantArray *get(const ArrayType *T, const vector<Constant*> &);
232   static ConstantArray *get(const string &Initializer);
233   
234   virtual string getStrValue() const;
235
236   inline const vector<Use> &getValues() const { return Operands; }
237
238   // isNullValue - Return true if this is the value that would be returned by
239   // getNullConstant.
240   virtual bool isNullValue() const { return false; }
241
242   // Methods for support type inquiry through isa, cast, and dyn_cast:
243   static inline bool classof(const ConstantArray *) { return true; }
244   static bool classof(const Constant *CPV);  // defined in CPV.cpp
245   static inline bool classof(const Value *V) {
246     return isa<Constant>(V) && classof(cast<Constant>(V));
247   }
248 };
249
250
251 //===---------------------------------------------------------------------------
252 // ConstantStruct - Constant Struct Declarations
253 //
254 class ConstantStruct : public Constant {
255   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
256 protected:
257   ConstantStruct(const StructType *T, const vector<Constant*> &Val);
258   ~ConstantStruct() {}
259
260   virtual void destroyConstant();
261 public:
262   static ConstantStruct *get(const StructType *T,
263                               const vector<Constant*> &V);
264
265   virtual string getStrValue() const;
266
267   inline const vector<Use> &getValues() const { return Operands; }
268
269   // isNullValue - Return true if this is the value that would be returned by
270   // getNullConstant.
271   virtual bool isNullValue() const { return false; }
272
273   // Methods for support type inquiry through isa, cast, and dyn_cast:
274   static inline bool classof(const ConstantStruct *) { return true; }
275   static bool classof(const Constant *CPV);  // defined in CPV.cpp
276   static inline bool classof(const Value *V) {
277     return isa<Constant>(V) && classof(cast<Constant>(V));
278   }
279 };
280
281 //===---------------------------------------------------------------------------
282 // ConstantPointer - Constant Pointer Declarations
283 //
284 // The ConstantPointer class represents a null pointer of a specific type. For
285 // a more specific/useful instance, a subclass of ConstantPointer should be
286 // used.
287 //
288 class ConstantPointer : public Constant {
289   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
290 protected:
291   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
292   ~ConstantPointer() {}
293 public:
294   virtual string getStrValue() const = 0;
295
296   // isNullValue - Return true if this is the value that would be returned by
297   // getNullConstant.
298   virtual bool isNullValue() const { return false; }
299
300   // Methods for support type inquiry through isa, cast, and dyn_cast:
301   static inline bool classof(const ConstantPointer *) { return true; }
302   static bool classof(const Constant *CPV);  // defined in CPV.cpp
303   static inline bool classof(const Value *V) {
304     return isa<Constant>(V) && classof(cast<Constant>(V));
305   }
306 };
307
308 // ConstantPointerNull - a constant pointer value that points to null
309 //
310 class ConstantPointerNull : public ConstantPointer {
311   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
312 protected:
313   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
314   inline ~ConstantPointerNull() {}
315 public:
316   virtual string getStrValue() const;
317
318   static ConstantPointerNull *get(const PointerType *T);
319
320   // isNullValue - Return true if this is the value that would be returned by
321   // getNullConstant.
322   virtual bool isNullValue() const { return true; }
323
324   // Methods for support type inquiry through isa, cast, and dyn_cast:
325   static inline bool classof(const ConstantPointerNull *) { return true; }
326   static inline bool classof(const ConstantPointer *P) {
327     return P->getNumOperands() == 0;
328   }
329   static inline bool classof(const Constant *CPV) {
330     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
331   }
332   static inline bool classof(const Value *V) {
333     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
334   }
335 };
336
337
338 // ConstantPointerRef - a constant pointer value that is initialized to
339 // point to a global value, which lies at a constant, fixed address.
340 //
341 class ConstantPointerRef : public ConstantPointer {
342   friend class Module;   // Modules maintain these references
343   ConstantPointerRef(const ConstantPointerRef &); // DNI!
344
345 protected:
346   ConstantPointerRef(GlobalValue *GV);
347   ~ConstantPointerRef() {}
348
349   virtual void destroyConstant() { destroyConstantImpl(); }
350 public:
351   static ConstantPointerRef *get(GlobalValue *GV);
352
353   virtual string getStrValue() const;
354
355   const GlobalValue *getValue() const { 
356     return cast<GlobalValue>(Operands[0].get());
357   }
358   GlobalValue *getValue() {
359     return cast<GlobalValue>(Operands[0].get());
360   }
361
362   // Methods for support type inquiry through isa, cast, and dyn_cast:
363   static inline bool classof(const ConstantPointerRef *) { return true; }
364   static inline bool classof(const ConstantPointer *CPV) {
365     return CPV->getNumOperands() == 1;
366   }
367   static inline bool classof(const Constant *CPV) {
368     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
369   }
370   static inline bool classof(const Value *V) {
371     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
372   }
373
374   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
375   // NOT USE THIS!!
376   void mutateReference(GlobalValue *NewGV);
377   // END WARNING!!
378 };
379
380
381
382 #endif