1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
18 //===---------------------------------------------------------------------------
19 // ConstantBool - Boolean Values
21 class ConstantBool : public Constant {
23 ConstantBool(const ConstantBool &); // DO NOT IMPLEMENT
27 static ConstantBool *True, *False; // The True & False values
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); }
33 // inverted - Return the opposite value of the current value.
34 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
36 inline bool getValue() const { return Val; }
38 // isNullValue - Return true if this is the value that would be returned by
40 virtual bool isNullValue() const { return this == False; }
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);
47 static inline bool classof(const Value *V) {
48 return isa<Constant>(V) && classof(cast<Constant>(V));
53 //===---------------------------------------------------------------------------
54 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
55 // with integral constants easier.
57 class ConstantInt : public Constant {
63 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
64 ConstantInt(const Type *Ty, uint64_t V);
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.
71 bool equalsInt(unsigned char V) const {
73 "equals: Can only be used with very small positive constants!");
74 return Val.Unsigned == V;
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.
80 static ConstantInt *get(const Type *Ty, unsigned char V);
82 // isNullValue - Return true if this is the value that would be returned by
84 virtual bool isNullValue() const { return Val.Unsigned == 0; }
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));
95 //===---------------------------------------------------------------------------
96 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
98 class ConstantSInt : public ConstantInt {
99 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
101 ConstantSInt(const Type *Ty, int64_t V);
104 static ConstantSInt *get(const Type *Ty, int64_t V);
106 static bool isValueValidForType(const Type *Ty, int64_t V);
107 inline int64_t getValue() const { return Val.Signed; }
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));
117 //===---------------------------------------------------------------------------
118 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
120 class ConstantUInt : public ConstantInt {
121 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
123 ConstantUInt(const Type *Ty, uint64_t V);
126 static ConstantUInt *get(const Type *Ty, uint64_t V);
128 static bool isValueValidForType(const Type *Ty, uint64_t V);
129 inline uint64_t getValue() const { return Val.Unsigned; }
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));
140 //===---------------------------------------------------------------------------
141 // ConstantFP - Floating Point Values [float, double]
143 class ConstantFP : public Constant {
145 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
147 ConstantFP(const Type *Ty, double V);
150 static ConstantFP *get(const Type *Ty, double V);
152 static bool isValueValidForType(const Type *Ty, double V);
153 inline double getValue() const { return Val; }
155 // isNullValue - Return true if this is the value that would be returned by
157 virtual bool isNullValue() const { return Val == 0; }
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));
168 //===---------------------------------------------------------------------------
169 // ConstantArray - Constant Array Declarations
171 class ConstantArray : public Constant {
172 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
174 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
177 virtual void destroyConstant();
179 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
180 static ConstantArray *get(const std::string &Initializer);
182 inline const ArrayType *getType() const {
183 return (ArrayType*)Value::getType();
186 inline const std::vector<Use> &getValues() const { return Operands; }
188 // isNullValue - Return true if this is the value that would be returned by
190 virtual bool isNullValue() const { return false; }
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));
201 //===---------------------------------------------------------------------------
202 // ConstantStruct - Constant Struct Declarations
204 class ConstantStruct : public Constant {
205 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
207 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
210 virtual void destroyConstant();
212 static ConstantStruct *get(const StructType *T,
213 const std::vector<Constant*> &V);
215 inline const StructType *getType() const {
216 return (StructType*)Value::getType();
219 inline const std::vector<Use> &getValues() const { return Operands; }
221 // isNullValue - Return true if this is the value that would be returned by
223 virtual bool isNullValue() const { return false; }
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));
233 //===---------------------------------------------------------------------------
234 // ConstantPointer - Constant Pointer Declarations
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
240 class ConstantPointer : public Constant {
241 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
243 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
244 ~ConstantPointer() {}
246 inline const PointerType *getType() const {
247 return (PointerType*)Value::getType();
250 // isNullValue - Return true if this is the value that would be returned by
252 virtual bool isNullValue() const { return false; }
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));
262 // ConstantPointerNull - a constant pointer value that points to null
264 class ConstantPointerNull : public ConstantPointer {
265 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
267 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
268 inline ~ConstantPointerNull() {}
271 static ConstantPointerNull *get(const PointerType *T);
273 // isNullValue - Return true if this is the value that would be returned by
275 virtual bool isNullValue() const { return true; }
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;
282 static inline bool classof(const Constant *CPV) {
283 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
285 static inline bool classof(const Value *V) {
286 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
291 // ConstantPointerRef - a constant pointer value that is initialized to
292 // point to a global value, which lies at a constant, fixed address.
294 class ConstantPointerRef : public ConstantPointer {
295 friend class Module; // Modules maintain these references
296 ConstantPointerRef(const ConstantPointerRef &); // DNI!
299 ConstantPointerRef(GlobalValue *GV);
300 ~ConstantPointerRef() {}
302 virtual void destroyConstant() { destroyConstantImpl(); }
304 static ConstantPointerRef *get(GlobalValue *GV);
306 const GlobalValue *getValue() const {
307 return cast<GlobalValue>(Operands[0].get());
309 GlobalValue *getValue() {
310 return cast<GlobalValue>(Operands[0].get());
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;
318 static inline bool classof(const Constant *CPV) {
319 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
321 static inline bool classof(const Value *V) {
322 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
325 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
327 void mutateReference(GlobalValue *NewGV);