1 //===-- llvm/ConstantVals.h - Constant Value nodes ---------------*- C++ -*--=//
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
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
11 #include "llvm/User.h"
12 #include "Support/DataTypes.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 class Constant : public User {
24 inline Constant(const Type *Ty) : User(Ty, Value::ConstantVal) {}
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
35 virtual void destroyConstant() { assert(0 && "Not reached!"); }
36 void destroyConstantImpl();
38 // Specialize setName to handle symbol table majik...
39 virtual void setName(const string &name, SymbolTable *ST = 0);
41 virtual string getStrValue() const = 0;
43 // Static constructor to get a '0' constant of arbitrary type...
44 static Constant *getNullConstant(const Type *Ty);
46 // isNullValue - Return true if this is the value that would be returned by
48 virtual bool isNullValue() const = 0;
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;
59 //===----------------------------------------------------------------------===//
60 // Classes to represent constant pool variable defs
61 //===----------------------------------------------------------------------===//
63 //===---------------------------------------------------------------------------
64 // ConstantBool - Boolean Values
66 class ConstantBool : public Constant {
68 ConstantBool(const ConstantBool &); // DO NOT IMPLEMENT
72 static ConstantBool *True, *False; // The True & False values
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); }
78 // inverted - Return the opposite value of the current value.
79 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
81 virtual string getStrValue() const;
82 inline bool getValue() const { return Val; }
84 // isNullValue - Return true if this is the value that would be returned by
86 virtual bool isNullValue() const { return this == False; }
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);
93 static inline bool classof(const Value *V) {
94 return isa<Constant>(V) && classof(cast<Constant>(V));
99 //===---------------------------------------------------------------------------
100 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
101 // with integral constants easier.
103 class ConstantInt : public Constant {
109 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
110 ConstantInt(const Type *Ty, uint64_t V);
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.
117 bool equalsInt(unsigned char V) const {
119 "equals: Can only be used with very small positive constants!");
120 return Val.Unsigned == V;
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.
126 static ConstantInt *get(const Type *Ty, unsigned char V);
128 // isNullValue - Return true if this is the value that would be returned by
130 virtual bool isNullValue() const { return Val.Unsigned == 0; }
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));
141 //===---------------------------------------------------------------------------
142 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
144 class ConstantSInt : public ConstantInt {
145 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
147 ConstantSInt(const Type *Ty, int64_t V);
150 static ConstantSInt *get(const Type *Ty, int64_t V);
152 virtual string getStrValue() const;
154 static bool isValueValidForType(const Type *Ty, int64_t V);
155 inline int64_t getValue() const { return Val.Signed; }
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));
165 //===---------------------------------------------------------------------------
166 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
168 class ConstantUInt : public ConstantInt {
169 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
171 ConstantUInt(const Type *Ty, uint64_t V);
174 static ConstantUInt *get(const Type *Ty, uint64_t V);
176 virtual string getStrValue() const;
178 static bool isValueValidForType(const Type *Ty, uint64_t V);
179 inline uint64_t getValue() const { return Val.Unsigned; }
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));
190 //===---------------------------------------------------------------------------
191 // ConstantFP - Floating Point Values [float, double]
193 class ConstantFP : public Constant {
195 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
197 ConstantFP(const Type *Ty, double V);
200 static ConstantFP *get(const Type *Ty, double V);
202 virtual string getStrValue() const;
204 static bool isValueValidForType(const Type *Ty, double V);
205 inline double getValue() const { return Val; }
207 // isNullValue - Return true if this is the value that would be returned by
209 virtual bool isNullValue() const { return Val == 0; }
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));
220 //===---------------------------------------------------------------------------
221 // ConstantArray - Constant Array Declarations
223 class ConstantArray : public Constant {
224 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
226 ConstantArray(const ArrayType *T, const vector<Constant*> &Val);
229 virtual void destroyConstant();
231 static ConstantArray *get(const ArrayType *T, const vector<Constant*> &);
232 static ConstantArray *get(const string &Initializer);
234 virtual string getStrValue() const;
236 inline const vector<Use> &getValues() const { return Operands; }
238 // isNullValue - Return true if this is the value that would be returned by
240 virtual bool isNullValue() const { return false; }
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));
251 //===---------------------------------------------------------------------------
252 // ConstantStruct - Constant Struct Declarations
254 class ConstantStruct : public Constant {
255 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
257 ConstantStruct(const StructType *T, const vector<Constant*> &Val);
260 virtual void destroyConstant();
262 static ConstantStruct *get(const StructType *T,
263 const vector<Constant*> &V);
265 virtual string getStrValue() const;
267 inline const vector<Use> &getValues() const { return Operands; }
269 // isNullValue - Return true if this is the value that would be returned by
271 virtual bool isNullValue() const { return false; }
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));
281 //===---------------------------------------------------------------------------
282 // ConstantPointer - Constant Pointer Declarations
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
288 class ConstantPointer : public Constant {
289 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
291 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
292 ~ConstantPointer() {}
294 virtual string getStrValue() const = 0;
296 // isNullValue - Return true if this is the value that would be returned by
298 virtual bool isNullValue() const { return false; }
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));
308 // ConstantPointerNull - a constant pointer value that points to null
310 class ConstantPointerNull : public ConstantPointer {
311 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
313 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
314 inline ~ConstantPointerNull() {}
316 virtual string getStrValue() const;
318 static ConstantPointerNull *get(const PointerType *T);
320 // isNullValue - Return true if this is the value that would be returned by
322 virtual bool isNullValue() const { return true; }
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;
329 static inline bool classof(const Constant *CPV) {
330 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
332 static inline bool classof(const Value *V) {
333 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
338 // ConstantPointerRef - a constant pointer value that is initialized to
339 // point to a global value, which lies at a constant, fixed address.
341 class ConstantPointerRef : public ConstantPointer {
342 friend class Module; // Modules maintain these references
343 ConstantPointerRef(const ConstantPointerRef &); // DNI!
346 ConstantPointerRef(GlobalValue *GV);
347 ~ConstantPointerRef() {}
349 virtual void destroyConstant() { destroyConstantImpl(); }
351 static ConstantPointerRef *get(GlobalValue *GV);
353 virtual string getStrValue() const;
355 const GlobalValue *getValue() const {
356 return cast<GlobalValue>(Operands[0].get());
358 GlobalValue *getValue() {
359 return cast<GlobalValue>(Operands[0].get());
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;
367 static inline bool classof(const Constant *CPV) {
368 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
370 static inline bool classof(const Value *V) {
371 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
374 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
376 void mutateReference(GlobalValue *NewGV);