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 // Static constructor to get a '0' constant of arbitrary type...
42 static Constant *getNullConstant(const Type *Ty);
44 // isNullValue - Return true if this is the value that would be returned by
46 virtual bool isNullValue() const = 0;
48 virtual void print(std::ostream &O) const;
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 inline bool getValue() const { return Val; }
83 // isNullValue - Return true if this is the value that would be returned by
85 virtual bool isNullValue() const { return this == False; }
87 // Methods for support type inquiry through isa, cast, and dyn_cast:
88 static inline bool classof(const ConstantBool *) { return true; }
89 static bool classof(const Constant *CPV) {
90 return (CPV == True) | (CPV == False);
92 static inline bool classof(const Value *V) {
93 return isa<Constant>(V) && classof(cast<Constant>(V));
98 //===---------------------------------------------------------------------------
99 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
100 // with integral constants easier.
102 class ConstantInt : public Constant {
108 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
109 ConstantInt(const Type *Ty, uint64_t V);
112 // equalsInt - Provide a helper method that can be used to determine if the
113 // constant contained within is equal to a constant. This only works for very
114 // small values, because this is all that can be represented with all types.
116 bool equalsInt(unsigned char V) const {
118 "equals: Can only be used with very small positive constants!");
119 return Val.Unsigned == V;
122 // ConstantInt::get static method: return a constant pool int with the
123 // specified value. as above, we work only with very small values here.
125 static ConstantInt *get(const Type *Ty, unsigned char V);
127 // isNullValue - Return true if this is the value that would be returned by
129 virtual bool isNullValue() const { return Val.Unsigned == 0; }
131 // Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const ConstantInt *) { 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 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
143 class ConstantSInt : public ConstantInt {
144 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
146 ConstantSInt(const Type *Ty, int64_t V);
149 static ConstantSInt *get(const Type *Ty, int64_t V);
151 static bool isValueValidForType(const Type *Ty, int64_t V);
152 inline int64_t getValue() const { return Val.Signed; }
154 // Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const ConstantSInt *) { return true; }
156 static bool classof(const Constant *CPV); // defined in CPV.cpp
157 static inline bool classof(const Value *V) {
158 return isa<Constant>(V) && classof(cast<Constant>(V));
162 //===---------------------------------------------------------------------------
163 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
165 class ConstantUInt : public ConstantInt {
166 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
168 ConstantUInt(const Type *Ty, uint64_t V);
171 static ConstantUInt *get(const Type *Ty, uint64_t V);
173 static bool isValueValidForType(const Type *Ty, uint64_t V);
174 inline uint64_t getValue() const { return Val.Unsigned; }
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const ConstantUInt *) { return true; }
178 static bool classof(const Constant *CPV); // defined in CPV.cpp
179 static inline bool classof(const Value *V) {
180 return isa<Constant>(V) && classof(cast<Constant>(V));
185 //===---------------------------------------------------------------------------
186 // ConstantFP - Floating Point Values [float, double]
188 class ConstantFP : public Constant {
190 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
192 ConstantFP(const Type *Ty, double V);
195 static ConstantFP *get(const Type *Ty, double V);
197 static bool isValueValidForType(const Type *Ty, double V);
198 inline double getValue() const { return Val; }
200 // isNullValue - Return true if this is the value that would be returned by
202 virtual bool isNullValue() const { return Val == 0; }
204 // Methods for support type inquiry through isa, cast, and dyn_cast:
205 static inline bool classof(const ConstantFP *) { return true; }
206 static bool classof(const Constant *CPV); // defined in CPV.cpp
207 static inline bool classof(const Value *V) {
208 return isa<Constant>(V) && classof(cast<Constant>(V));
213 //===---------------------------------------------------------------------------
214 // ConstantArray - Constant Array Declarations
216 class ConstantArray : public Constant {
217 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
219 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
222 virtual void destroyConstant();
224 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
225 static ConstantArray *get(const std::string &Initializer);
227 inline const ArrayType *getType() const {
228 return (ArrayType*)Value::getType();
231 inline const std::vector<Use> &getValues() const { return Operands; }
233 // isNullValue - Return true if this is the value that would be returned by
235 virtual bool isNullValue() const { return false; }
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const ConstantArray *) { return true; }
239 static bool classof(const Constant *CPV); // defined in CPV.cpp
240 static inline bool classof(const Value *V) {
241 return isa<Constant>(V) && classof(cast<Constant>(V));
246 //===---------------------------------------------------------------------------
247 // ConstantStruct - Constant Struct Declarations
249 class ConstantStruct : public Constant {
250 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
252 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
255 virtual void destroyConstant();
257 static ConstantStruct *get(const StructType *T,
258 const std::vector<Constant*> &V);
260 inline const StructType *getType() const {
261 return (StructType*)Value::getType();
264 inline const std::vector<Use> &getValues() const { return Operands; }
266 // isNullValue - Return true if this is the value that would be returned by
268 virtual bool isNullValue() const { return false; }
270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool classof(const ConstantStruct *) { return true; }
272 static bool classof(const Constant *CPV); // defined in CPV.cpp
273 static inline bool classof(const Value *V) {
274 return isa<Constant>(V) && classof(cast<Constant>(V));
278 //===---------------------------------------------------------------------------
279 // ConstantPointer - Constant Pointer Declarations
281 // The ConstantPointer class represents a null pointer of a specific type. For
282 // a more specific/useful instance, a subclass of ConstantPointer should be
285 class ConstantPointer : public Constant {
286 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
288 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
289 ~ConstantPointer() {}
291 inline const PointerType *getType() const {
292 return (PointerType*)Value::getType();
295 // isNullValue - Return true if this is the value that would be returned by
297 virtual bool isNullValue() const { return false; }
299 // Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const ConstantPointer *) { return true; }
301 static bool classof(const Constant *CPV); // defined in CPV.cpp
302 static inline bool classof(const Value *V) {
303 return isa<Constant>(V) && classof(cast<Constant>(V));
307 // ConstantPointerNull - a constant pointer value that points to null
309 class ConstantPointerNull : public ConstantPointer {
310 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
312 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
313 inline ~ConstantPointerNull() {}
316 static ConstantPointerNull *get(const PointerType *T);
318 // isNullValue - Return true if this is the value that would be returned by
320 virtual bool isNullValue() const { return true; }
322 // Methods for support type inquiry through isa, cast, and dyn_cast:
323 static inline bool classof(const ConstantPointerNull *) { return true; }
324 static inline bool classof(const ConstantPointer *P) {
325 return P->getNumOperands() == 0;
327 static inline bool classof(const Constant *CPV) {
328 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
330 static inline bool classof(const Value *V) {
331 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
336 // ConstantPointerRef - a constant pointer value that is initialized to
337 // point to a global value, which lies at a constant, fixed address.
339 class ConstantPointerRef : public ConstantPointer {
340 friend class Module; // Modules maintain these references
341 ConstantPointerRef(const ConstantPointerRef &); // DNI!
344 ConstantPointerRef(GlobalValue *GV);
345 ~ConstantPointerRef() {}
347 virtual void destroyConstant() { destroyConstantImpl(); }
349 static ConstantPointerRef *get(GlobalValue *GV);
351 const GlobalValue *getValue() const {
352 return cast<GlobalValue>(Operands[0].get());
354 GlobalValue *getValue() {
355 return cast<GlobalValue>(Operands[0].get());
358 // Methods for support type inquiry through isa, cast, and dyn_cast:
359 static inline bool classof(const ConstantPointerRef *) { return true; }
360 static inline bool classof(const ConstantPointer *CPV) {
361 return CPV->getNumOperands() == 1;
363 static inline bool classof(const Constant *CPV) {
364 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
366 static inline bool classof(const Value *V) {
367 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
370 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
372 void mutateReference(GlobalValue *NewGV);