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 std::string &name, SymbolTable *ST = 0);
41 virtual std::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 std::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 std::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 std::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 std::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 std::vector<Constant*> &Val);
229 virtual void destroyConstant();
231 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
232 static ConstantArray *get(const std::string &Initializer);
234 virtual std::string getStrValue() const;
235 inline const ArrayType *getType() const {
236 return (ArrayType*)Value::getType();
239 inline const std::vector<Use> &getValues() const { return Operands; }
241 // isNullValue - Return true if this is the value that would be returned by
243 virtual bool isNullValue() const { return false; }
245 // Methods for support type inquiry through isa, cast, and dyn_cast:
246 static inline bool classof(const ConstantArray *) { return true; }
247 static bool classof(const Constant *CPV); // defined in CPV.cpp
248 static inline bool classof(const Value *V) {
249 return isa<Constant>(V) && classof(cast<Constant>(V));
254 //===---------------------------------------------------------------------------
255 // ConstantStruct - Constant Struct Declarations
257 class ConstantStruct : public Constant {
258 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
260 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
263 virtual void destroyConstant();
265 static ConstantStruct *get(const StructType *T,
266 const std::vector<Constant*> &V);
268 virtual std::string getStrValue() const;
269 inline const StructType *getType() const {
270 return (StructType*)Value::getType();
273 inline const std::vector<Use> &getValues() const { return Operands; }
275 // isNullValue - Return true if this is the value that would be returned by
277 virtual bool isNullValue() const { return false; }
279 // Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const ConstantStruct *) { return true; }
281 static bool classof(const Constant *CPV); // defined in CPV.cpp
282 static inline bool classof(const Value *V) {
283 return isa<Constant>(V) && classof(cast<Constant>(V));
287 //===---------------------------------------------------------------------------
288 // ConstantPointer - Constant Pointer Declarations
290 // The ConstantPointer class represents a null pointer of a specific type. For
291 // a more specific/useful instance, a subclass of ConstantPointer should be
294 class ConstantPointer : public Constant {
295 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
297 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
298 ~ConstantPointer() {}
300 virtual std::string getStrValue() const = 0;
301 inline const PointerType *getType() const {
302 return (PointerType*)Value::getType();
305 // isNullValue - Return true if this is the value that would be returned by
307 virtual bool isNullValue() const { return false; }
309 // Methods for support type inquiry through isa, cast, and dyn_cast:
310 static inline bool classof(const ConstantPointer *) { return true; }
311 static bool classof(const Constant *CPV); // defined in CPV.cpp
312 static inline bool classof(const Value *V) {
313 return isa<Constant>(V) && classof(cast<Constant>(V));
317 // ConstantPointerNull - a constant pointer value that points to null
319 class ConstantPointerNull : public ConstantPointer {
320 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
322 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
323 inline ~ConstantPointerNull() {}
325 virtual std::string getStrValue() const;
327 static ConstantPointerNull *get(const PointerType *T);
329 // isNullValue - Return true if this is the value that would be returned by
331 virtual bool isNullValue() const { return true; }
333 // Methods for support type inquiry through isa, cast, and dyn_cast:
334 static inline bool classof(const ConstantPointerNull *) { return true; }
335 static inline bool classof(const ConstantPointer *P) {
336 return P->getNumOperands() == 0;
338 static inline bool classof(const Constant *CPV) {
339 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
341 static inline bool classof(const Value *V) {
342 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
347 // ConstantPointerRef - a constant pointer value that is initialized to
348 // point to a global value, which lies at a constant, fixed address.
350 class ConstantPointerRef : public ConstantPointer {
351 friend class Module; // Modules maintain these references
352 ConstantPointerRef(const ConstantPointerRef &); // DNI!
355 ConstantPointerRef(GlobalValue *GV);
356 ~ConstantPointerRef() {}
358 virtual void destroyConstant() { destroyConstantImpl(); }
360 static ConstantPointerRef *get(GlobalValue *GV);
362 virtual std::string getStrValue() const;
364 const GlobalValue *getValue() const {
365 return cast<GlobalValue>(Operands[0].get());
367 GlobalValue *getValue() {
368 return cast<GlobalValue>(Operands[0].get());
371 // Methods for support type inquiry through isa, cast, and dyn_cast:
372 static inline bool classof(const ConstantPointerRef *) { return true; }
373 static inline bool classof(const ConstantPointer *CPV) {
374 return CPV->getNumOperands() == 1;
376 static inline bool classof(const Constant *CPV) {
377 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
379 static inline bool classof(const Value *V) {
380 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
383 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
385 void mutateReference(GlobalValue *NewGV);