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 {
26 static ConstantBool *True, *False; // The True & False values
28 // Factory objects - Return objects of the specified value
29 static ConstantBool *get(bool Value) { return Value ? True : False; }
30 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
32 // inverted - Return the opposite value of the current value.
33 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
35 inline bool getValue() const { return Val; }
37 // isNullValue - Return true if this is the value that would be returned by
39 virtual bool isNullValue() const { return this == False; }
41 // Methods for support type inquiry through isa, cast, and dyn_cast:
42 static inline bool classof(const ConstantBool *) { return true; }
43 static bool classof(const Constant *CPV) {
44 return (CPV == True) | (CPV == False);
46 static inline bool classof(const Value *V) {
47 return isa<Constant>(V) && classof(cast<Constant>(V));
52 //===---------------------------------------------------------------------------
53 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
54 // with integral constants easier.
56 class ConstantInt : public Constant {
62 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
63 ConstantInt(const Type *Ty, uint64_t V);
66 // equalsInt - Provide a helper method that can be used to determine if the
67 // constant contained within is equal to a constant. This only works for very
68 // small values, because this is all that can be represented with all types.
70 bool equalsInt(unsigned char V) const {
72 "equals: Can only be used with very small positive constants!");
73 return Val.Unsigned == V;
76 // ConstantInt::get static method: return a constant pool int with the
77 // specified value. as above, we work only with very small values here.
79 static ConstantInt *get(const Type *Ty, unsigned char V);
81 // isNullValue - Return true if this is the value that would be returned by
83 virtual bool isNullValue() const { return Val.Unsigned == 0; }
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const ConstantInt *) { return true; }
87 static bool classof(const Constant *CPV); // defined in CPV.cpp
88 static inline bool classof(const Value *V) {
89 return isa<Constant>(V) && classof(cast<Constant>(V));
94 //===---------------------------------------------------------------------------
95 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
97 class ConstantSInt : public ConstantInt {
98 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
100 ConstantSInt(const Type *Ty, int64_t V);
103 static ConstantSInt *get(const Type *Ty, int64_t V);
105 static bool isValueValidForType(const Type *Ty, int64_t V);
106 inline int64_t getValue() const { return Val.Signed; }
108 // Methods for support type inquiry through isa, cast, and dyn_cast:
109 static inline bool classof(const ConstantSInt *) { return true; }
110 static bool classof(const Constant *CPV); // defined in CPV.cpp
111 static inline bool classof(const Value *V) {
112 return isa<Constant>(V) && classof(cast<Constant>(V));
116 //===---------------------------------------------------------------------------
117 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
119 class ConstantUInt : public ConstantInt {
120 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
122 ConstantUInt(const Type *Ty, uint64_t V);
125 static ConstantUInt *get(const Type *Ty, uint64_t V);
127 static bool isValueValidForType(const Type *Ty, uint64_t V);
128 inline uint64_t getValue() const { return Val.Unsigned; }
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const ConstantUInt *) { return true; }
132 static bool classof(const Constant *CPV); // defined in CPV.cpp
133 static inline bool classof(const Value *V) {
134 return isa<Constant>(V) && classof(cast<Constant>(V));
139 //===---------------------------------------------------------------------------
140 // ConstantFP - Floating Point Values [float, double]
142 class ConstantFP : public Constant {
144 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
146 ConstantFP(const Type *Ty, double V);
149 static ConstantFP *get(const Type *Ty, double V);
151 static bool isValueValidForType(const Type *Ty, double V);
152 inline double getValue() const { return Val; }
154 // isNullValue - Return true if this is the value that would be returned by
156 virtual bool isNullValue() const { return Val == 0; }
158 // Methods for support type inquiry through isa, cast, and dyn_cast:
159 static inline bool classof(const ConstantFP *) { return true; }
160 static bool classof(const Constant *CPV); // defined in CPV.cpp
161 static inline bool classof(const Value *V) {
162 return isa<Constant>(V) && classof(cast<Constant>(V));
167 //===---------------------------------------------------------------------------
168 // ConstantArray - Constant Array Declarations
170 class ConstantArray : public Constant {
171 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
173 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
176 virtual void destroyConstant();
178 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
179 static ConstantArray *get(const std::string &Initializer);
181 inline const ArrayType *getType() const {
182 return (ArrayType*)Value::getType();
185 inline const std::vector<Use> &getValues() const { return Operands; }
187 // isNullValue - Return true if this is the value that would be returned by
189 virtual bool isNullValue() const { return false; }
191 // Methods for support type inquiry through isa, cast, and dyn_cast:
192 static inline bool classof(const ConstantArray *) { return true; }
193 static bool classof(const Constant *CPV); // defined in CPV.cpp
194 static inline bool classof(const Value *V) {
195 return isa<Constant>(V) && classof(cast<Constant>(V));
200 //===---------------------------------------------------------------------------
201 // ConstantStruct - Constant Struct Declarations
203 class ConstantStruct : public Constant {
204 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
206 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
209 virtual void destroyConstant();
211 static ConstantStruct *get(const StructType *T,
212 const std::vector<Constant*> &V);
214 inline const StructType *getType() const {
215 return (StructType*)Value::getType();
218 inline const std::vector<Use> &getValues() const { return Operands; }
220 // isNullValue - Return true if this is the value that would be returned by
222 virtual bool isNullValue() const { return false; }
224 // Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const ConstantStruct *) { return true; }
226 static bool classof(const Constant *CPV); // defined in CPV.cpp
227 static inline bool classof(const Value *V) {
228 return isa<Constant>(V) && classof(cast<Constant>(V));
232 //===---------------------------------------------------------------------------
233 // ConstantPointer - Constant Pointer Declarations
235 // The ConstantPointer class represents a null pointer of a specific type. For
236 // a more specific/useful instance, a subclass of ConstantPointer should be
239 class ConstantPointer : public Constant {
240 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
242 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
243 ~ConstantPointer() {}
245 inline const PointerType *getType() const {
246 return (PointerType*)Value::getType();
249 // isNullValue - Return true if this is the value that would be returned by
251 virtual bool isNullValue() const { return false; }
253 // Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const ConstantPointer *) { return true; }
255 static bool classof(const Constant *CPV); // defined in Constants.cpp
256 static inline bool classof(const Value *V) {
257 return isa<Constant>(V) && classof(cast<Constant>(V));
261 // ConstantPointerNull - a constant pointer value that points to null
263 class ConstantPointerNull : public ConstantPointer {
264 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
266 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
267 inline ~ConstantPointerNull() {}
270 static ConstantPointerNull *get(const PointerType *T);
272 // isNullValue - Return true if this is the value that would be returned by
274 virtual bool isNullValue() const { return true; }
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const ConstantPointerNull *) { return true; }
278 static inline bool classof(const ConstantPointer *P) {
279 return (P->getNumOperands() == 0 && P->isNullValue());
281 static inline bool classof(const Constant *CPV) {
282 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
284 static inline bool classof(const Value *V) {
285 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
290 // ConstantPointerRef - a constant pointer value that is initialized to
291 // point to a global value, which lies at a constant, fixed address.
293 class ConstantPointerRef : public ConstantPointer {
294 friend class Module; // Modules maintain these references
295 ConstantPointerRef(const ConstantPointerRef &); // DNI!
298 ConstantPointerRef(GlobalValue *GV);
299 ~ConstantPointerRef() {}
301 virtual void destroyConstant() { destroyConstantImpl(); }
303 static ConstantPointerRef *get(GlobalValue *GV);
305 const GlobalValue *getValue() const {
306 return cast<GlobalValue>(Operands[0].get());
308 GlobalValue *getValue() {
309 return cast<GlobalValue>(Operands[0].get());
312 // Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const ConstantPointerRef *) { return true; }
314 static inline bool classof(const ConstantPointer *CPV) {
315 // check for a single operand (the target value)
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 // Returns the number of uses of OldV that were replaced.
328 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
333 // ConstantExpr - a constant value that is initialized with
334 // an expression using other constant values. This is only used
335 // to represent values that cannot be evaluated at compile-time
336 // (e.g., something derived from an address) because it does
337 // not have a mechanism to store the actual value.
338 // Use the appropriate Constant subclass above for known constants.
340 class ConstantExpr : public Constant {
341 unsigned iType; // Operation type
344 ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
345 ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
346 ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
350 virtual void destroyConstant();
353 // Static methods to construct a ConstantExpr of different kinds.
355 // Unary constant expr - Use with unary operators and casts
356 static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty);
358 // Binary constant expr - Use with binary operators...
359 static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
361 // Getelementptr form...
362 static ConstantExpr *getGetElementPtr(Constant *C,
363 const std::vector<Constant*> &IdxList);
365 // isNullValue - Return true if this is the value that would be returned by
367 virtual bool isNullValue() const { return false; }
369 // getOpcode - Return the opcode at the root of this constant expression
370 unsigned getOpcode() const { return iType; }
372 // getOpcodeName - Return a string representation for an opcode.
373 const char *getOpcodeName() const;
375 // isConstantExpr - Return true if this is a ConstantExpr
376 virtual bool isConstantExpr() const { return true; }
378 // Methods for support type inquiry through isa, cast, and dyn_cast:
379 static inline bool classof(const ConstantExpr *) { return true; }
380 static inline bool classof(const Constant *CPV) {
381 return CPV->isConstantExpr();
383 static inline bool classof(const Value *V) {
384 return isa<Constant>(V) && classof(cast<Constant>(V));
388 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
390 // Returns the number of uses of OldV that were replaced.
391 virtual unsigned mutateReferences(Value* OldV, Value *NewV);