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"
20 //===---------------------------------------------------------------------------
21 // ConstantBool - Boolean Values
23 class ConstantBool : public Constant {
28 static ConstantBool *True, *False; // The True & False values
30 // Factory objects - Return objects of the specified value
31 static ConstantBool *get(bool Value) { return Value ? True : False; }
32 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
34 // inverted - Return the opposite value of the current value.
35 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
37 inline bool getValue() const { return Val; }
39 // isNullValue - Return true if this is the value that would be returned by
41 virtual bool isNullValue() const { return this == False; }
43 // Methods for support type inquiry through isa, cast, and dyn_cast:
44 static inline bool classof(const ConstantBool *) { return true; }
45 static bool classof(const Constant *CPV) {
46 return (CPV == True) | (CPV == False);
48 static inline bool classof(const Value *V) {
49 return isa<Constant>(V) && classof(cast<Constant>(V));
54 //===---------------------------------------------------------------------------
55 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
56 // with integral constants easier.
58 class ConstantInt : public Constant {
64 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
65 ConstantInt(const Type *Ty, uint64_t V);
68 // equalsInt - Provide a helper method that can be used to determine if the
69 // constant contained within is equal to a constant. This only works for very
70 // small values, because this is all that can be represented with all types.
72 bool equalsInt(unsigned char V) const {
74 "equals: Can only be used with very small positive constants!");
75 return Val.Unsigned == V;
78 // ConstantInt::get static method: return a constant pool int with the
79 // specified value. as above, we work only with very small values here.
81 static ConstantInt *get(const Type *Ty, unsigned char V);
83 // isNullValue - Return true if this is the value that would be returned by
85 virtual bool isNullValue() const { return Val.Unsigned == 0; }
87 // Methods for support type inquiry through isa, cast, and dyn_cast:
88 static inline bool classof(const ConstantInt *) { return true; }
89 static bool classof(const Constant *CPV); // defined in CPV.cpp
90 static inline bool classof(const Value *V) {
91 return isa<Constant>(V) && classof(cast<Constant>(V));
96 //===---------------------------------------------------------------------------
97 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
99 class ConstantSInt : public ConstantInt {
100 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
102 ConstantSInt(const Type *Ty, int64_t V);
105 static ConstantSInt *get(const Type *Ty, int64_t V);
107 static bool isValueValidForType(const Type *Ty, int64_t V);
108 inline int64_t getValue() const { return Val.Signed; }
110 // Methods for support type inquiry through isa, cast, and dyn_cast:
111 static inline bool classof(const ConstantSInt *) { return true; }
112 static bool classof(const Constant *CPV); // defined in CPV.cpp
113 static inline bool classof(const Value *V) {
114 return isa<Constant>(V) && classof(cast<Constant>(V));
118 //===---------------------------------------------------------------------------
119 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
121 class ConstantUInt : public ConstantInt {
122 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
124 ConstantUInt(const Type *Ty, uint64_t V);
127 static ConstantUInt *get(const Type *Ty, uint64_t V);
129 static bool isValueValidForType(const Type *Ty, uint64_t V);
130 inline uint64_t getValue() const { return Val.Unsigned; }
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const ConstantUInt *) { 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));
141 //===---------------------------------------------------------------------------
142 // ConstantFP - Floating Point Values [float, double]
144 class ConstantFP : public Constant {
146 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
148 ConstantFP(const Type *Ty, double V);
151 static ConstantFP *get(const Type *Ty, double V);
153 static bool isValueValidForType(const Type *Ty, double V);
154 inline double getValue() const { return Val; }
156 // isNullValue - Return true if this is the value that would be returned by
158 virtual bool isNullValue() const { return Val == 0; }
160 // Methods for support type inquiry through isa, cast, and dyn_cast:
161 static inline bool classof(const ConstantFP *) { return true; }
162 static bool classof(const Constant *CPV); // defined in CPV.cpp
163 static inline bool classof(const Value *V) {
164 return isa<Constant>(V) && classof(cast<Constant>(V));
169 //===---------------------------------------------------------------------------
170 // ConstantArray - Constant Array Declarations
172 class ConstantArray : public Constant {
173 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
175 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
178 virtual void destroyConstant();
180 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
181 static ConstantArray *get(const std::string &Initializer);
183 inline const ArrayType *getType() const {
184 return (ArrayType*)Value::getType();
187 inline const std::vector<Use> &getValues() const { return Operands; }
189 // isNullValue - Return true if this is the value that would be returned by
191 virtual bool isNullValue() const { return false; }
193 // Methods for support type inquiry through isa, cast, and dyn_cast:
194 static inline bool classof(const ConstantArray *) { return true; }
195 static bool classof(const Constant *CPV); // defined in CPV.cpp
196 static inline bool classof(const Value *V) {
197 return isa<Constant>(V) && classof(cast<Constant>(V));
202 //===---------------------------------------------------------------------------
203 // ConstantStruct - Constant Struct Declarations
205 class ConstantStruct : public Constant {
206 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
208 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
211 virtual void destroyConstant();
213 static ConstantStruct *get(const StructType *T,
214 const std::vector<Constant*> &V);
216 inline const StructType *getType() const {
217 return (StructType*)Value::getType();
220 inline const std::vector<Use> &getValues() const { return Operands; }
222 // isNullValue - Return true if this is the value that would be returned by
224 virtual bool isNullValue() const { return false; }
226 // Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const ConstantStruct *) { return true; }
228 static bool classof(const Constant *CPV); // defined in CPV.cpp
229 static inline bool classof(const Value *V) {
230 return isa<Constant>(V) && classof(cast<Constant>(V));
234 //===---------------------------------------------------------------------------
235 // ConstantPointer - Constant Pointer Declarations
237 // The ConstantPointer class represents a null pointer of a specific type. For
238 // a more specific/useful instance, a subclass of ConstantPointer should be
241 class ConstantPointer : public Constant {
242 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
244 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
245 ~ConstantPointer() {}
247 inline const PointerType *getType() const {
248 return (PointerType*)Value::getType();
251 // isNullValue - Return true if this is the value that would be returned by
253 virtual bool isNullValue() const { return false; }
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const ConstantPointer *) { return true; }
257 static bool classof(const Constant *CPV); // defined in Constants.cpp
258 static inline bool classof(const Value *V) {
259 return isa<Constant>(V) && classof(cast<Constant>(V));
263 // ConstantPointerNull - a constant pointer value that points to null
265 class ConstantPointerNull : public ConstantPointer {
266 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
268 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
269 inline ~ConstantPointerNull() {}
272 static ConstantPointerNull *get(const PointerType *T);
274 // isNullValue - Return true if this is the value that would be returned by
276 virtual bool isNullValue() const { return true; }
278 // Methods for support type inquiry through isa, cast, and dyn_cast:
279 static inline bool classof(const ConstantPointerNull *) { return true; }
280 static inline bool classof(const ConstantPointer *P) {
281 return (P->getNumOperands() == 0 && P->isNullValue());
283 static inline bool classof(const Constant *CPV) {
284 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
286 static inline bool classof(const Value *V) {
287 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
292 // ConstantPointerRef - a constant pointer value that is initialized to
293 // point to a global value, which lies at a constant, fixed address.
295 class ConstantPointerRef : public ConstantPointer {
296 friend class Module; // Modules maintain these references
297 ConstantPointerRef(const ConstantPointerRef &); // DNI!
300 ConstantPointerRef(GlobalValue *GV);
301 ~ConstantPointerRef() {}
303 virtual void destroyConstant() { destroyConstantImpl(); }
305 static ConstantPointerRef *get(GlobalValue *GV);
307 const GlobalValue *getValue() const {
308 return cast<GlobalValue>(Operands[0].get());
310 GlobalValue *getValue() {
311 return cast<GlobalValue>(Operands[0].get());
314 // Methods for support type inquiry through isa, cast, and dyn_cast:
315 static inline bool classof(const ConstantPointerRef *) { return true; }
316 static inline bool classof(const ConstantPointer *CPV) {
317 // check for a single operand (the target value)
318 return (CPV->getNumOperands() == 1);
320 static inline bool classof(const Constant *CPV) {
321 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
323 static inline bool classof(const Value *V) {
324 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
327 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
329 // Returns the number of uses of OldV that were replaced.
330 virtual unsigned mutateReferences(Value* OldV, Value *NewV);
335 // ConstantExpr - a constant value that is initialized with
336 // an expression using other constant values. This is only used
337 // to represent values that cannot be evaluated at compile-time
338 // (e.g., something derived from an address) because it does
339 // not have a mechanism to store the actual value.
340 // Use the appropriate Constant subclass above for known constants.
342 class ConstantExpr : public Constant {
343 unsigned iType; // operation type
346 ConstantExpr(unsigned opCode, Constant *C, const Type *Ty);
347 ConstantExpr(unsigned opCode, Constant* C1, Constant* C2, const Type *Ty);
348 ConstantExpr(unsigned opCode, Constant* C,
349 const std::vector<Constant*> &IdxList, const Type *Ty);
352 virtual void destroyConstant();
355 // Static methods to construct a ConstantExpr of different kinds.
356 static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty);
357 static ConstantExpr *get(unsigned Opcode,
358 Constant *C1, Constant *C2, const Type *Ty);
359 static ConstantExpr *get(unsigned Opcode, Constant *C,
360 const std::vector<Constant*> &IdxList,
363 // isNullValue - Return true if this is the value that would be returned by
365 virtual bool isNullValue() const { return false; }
367 // getOpcode - Return the opcode at the root of this constant expression
368 unsigned getOpcode() const { return iType; }
370 // getOpcodeName - Return a string representation for an opcode.
371 static const char *getOpcodeName(unsigned opCode);
372 const char *getOpcodeName() const {
373 return getOpcodeName(getOpcode());
376 // isConstantExpr - Return true if this is a ConstantExpr
377 virtual bool isConstantExpr() const { return true; }
379 // Methods for support type inquiry through isa, cast, and dyn_cast:
380 static inline bool classof(const ConstantExpr *) { return true; }
381 static inline bool classof(const Constant *CPV) {
382 return CPV->isConstantExpr();
384 static inline bool classof(const Value *V) {
385 return isa<Constant>(V) && classof(cast<Constant>(V));
389 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
391 // Returns the number of uses of OldV that were replaced.
392 virtual unsigned mutateReferences(Value* OldV, Value *NewV);