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