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 virtual void print(std::ostream &O) const;
52 // Methods for support type inquiry through isa, cast, and dyn_cast:
53 static inline bool classof(const Constant *) { return true; }
54 static inline bool classof(const Value *V) {
55 return V->getValueType() == Value::ConstantVal;
61 //===----------------------------------------------------------------------===//
62 // Classes to represent constant pool variable defs
63 //===----------------------------------------------------------------------===//
65 //===---------------------------------------------------------------------------
66 // ConstantBool - Boolean Values
68 class ConstantBool : public Constant {
70 ConstantBool(const ConstantBool &); // DO NOT IMPLEMENT
74 static ConstantBool *True, *False; // The True & False values
76 // Factory objects - Return objects of the specified value
77 static ConstantBool *get(bool Value) { return Value ? True : False; }
78 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
80 // inverted - Return the opposite value of the current value.
81 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
83 virtual std::string getStrValue() const;
84 inline bool getValue() const { return Val; }
86 // isNullValue - Return true if this is the value that would be returned by
88 virtual bool isNullValue() const { return this == False; }
90 // Methods for support type inquiry through isa, cast, and dyn_cast:
91 static inline bool classof(const ConstantBool *) { return true; }
92 static bool classof(const Constant *CPV) {
93 return (CPV == True) | (CPV == False);
95 static inline bool classof(const Value *V) {
96 return isa<Constant>(V) && classof(cast<Constant>(V));
101 //===---------------------------------------------------------------------------
102 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
103 // with integral constants easier.
105 class ConstantInt : public Constant {
111 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
112 ConstantInt(const Type *Ty, uint64_t V);
115 // equalsInt - Provide a helper method that can be used to determine if the
116 // constant contained within is equal to a constant. This only works for very
117 // small values, because this is all that can be represented with all types.
119 bool equalsInt(unsigned char V) const {
121 "equals: Can only be used with very small positive constants!");
122 return Val.Unsigned == V;
125 // ConstantInt::get static method: return a constant pool int with the
126 // specified value. as above, we work only with very small values here.
128 static ConstantInt *get(const Type *Ty, unsigned char V);
130 // isNullValue - Return true if this is the value that would be returned by
132 virtual bool isNullValue() const { return Val.Unsigned == 0; }
134 // Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const ConstantInt *) { return true; }
136 static bool classof(const Constant *CPV); // defined in CPV.cpp
137 static inline bool classof(const Value *V) {
138 return isa<Constant>(V) && classof(cast<Constant>(V));
143 //===---------------------------------------------------------------------------
144 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
146 class ConstantSInt : public ConstantInt {
147 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
149 ConstantSInt(const Type *Ty, int64_t V);
152 static ConstantSInt *get(const Type *Ty, int64_t V);
154 virtual std::string getStrValue() const;
156 static bool isValueValidForType(const Type *Ty, int64_t V);
157 inline int64_t getValue() const { return Val.Signed; }
159 // Methods for support type inquiry through isa, cast, and dyn_cast:
160 static inline bool classof(const ConstantSInt *) { return true; }
161 static bool classof(const Constant *CPV); // defined in CPV.cpp
162 static inline bool classof(const Value *V) {
163 return isa<Constant>(V) && classof(cast<Constant>(V));
167 //===---------------------------------------------------------------------------
168 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
170 class ConstantUInt : public ConstantInt {
171 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
173 ConstantUInt(const Type *Ty, uint64_t V);
176 static ConstantUInt *get(const Type *Ty, uint64_t V);
178 virtual std::string getStrValue() const;
180 static bool isValueValidForType(const Type *Ty, uint64_t V);
181 inline uint64_t getValue() const { return Val.Unsigned; }
183 // Methods for support type inquiry through isa, cast, and dyn_cast:
184 static inline bool classof(const ConstantUInt *) { return true; }
185 static bool classof(const Constant *CPV); // defined in CPV.cpp
186 static inline bool classof(const Value *V) {
187 return isa<Constant>(V) && classof(cast<Constant>(V));
192 //===---------------------------------------------------------------------------
193 // ConstantFP - Floating Point Values [float, double]
195 class ConstantFP : public Constant {
197 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
199 ConstantFP(const Type *Ty, double V);
202 static ConstantFP *get(const Type *Ty, double V);
204 virtual std::string getStrValue() const;
206 static bool isValueValidForType(const Type *Ty, double V);
207 inline double getValue() const { return Val; }
209 // isNullValue - Return true if this is the value that would be returned by
211 virtual bool isNullValue() const { return Val == 0; }
213 // Methods for support type inquiry through isa, cast, and dyn_cast:
214 static inline bool classof(const ConstantFP *) { return true; }
215 static bool classof(const Constant *CPV); // defined in CPV.cpp
216 static inline bool classof(const Value *V) {
217 return isa<Constant>(V) && classof(cast<Constant>(V));
222 //===---------------------------------------------------------------------------
223 // ConstantArray - Constant Array Declarations
225 class ConstantArray : public Constant {
226 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
228 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
231 virtual void destroyConstant();
233 static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
234 static ConstantArray *get(const std::string &Initializer);
236 virtual std::string getStrValue() const;
237 inline const ArrayType *getType() const {
238 return (ArrayType*)Value::getType();
241 inline const std::vector<Use> &getValues() const { return Operands; }
243 // isNullValue - Return true if this is the value that would be returned by
245 virtual bool isNullValue() const { return false; }
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const ConstantArray *) { return true; }
249 static bool classof(const Constant *CPV); // defined in CPV.cpp
250 static inline bool classof(const Value *V) {
251 return isa<Constant>(V) && classof(cast<Constant>(V));
256 //===---------------------------------------------------------------------------
257 // ConstantStruct - Constant Struct Declarations
259 class ConstantStruct : public Constant {
260 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
262 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
265 virtual void destroyConstant();
267 static ConstantStruct *get(const StructType *T,
268 const std::vector<Constant*> &V);
270 virtual std::string getStrValue() const;
271 inline const StructType *getType() const {
272 return (StructType*)Value::getType();
275 inline const std::vector<Use> &getValues() const { return Operands; }
277 // isNullValue - Return true if this is the value that would be returned by
279 virtual bool isNullValue() const { return false; }
281 // Methods for support type inquiry through isa, cast, and dyn_cast:
282 static inline bool classof(const ConstantStruct *) { return true; }
283 static bool classof(const Constant *CPV); // defined in CPV.cpp
284 static inline bool classof(const Value *V) {
285 return isa<Constant>(V) && classof(cast<Constant>(V));
289 //===---------------------------------------------------------------------------
290 // ConstantPointer - Constant Pointer Declarations
292 // The ConstantPointer class represents a null pointer of a specific type. For
293 // a more specific/useful instance, a subclass of ConstantPointer should be
296 class ConstantPointer : public Constant {
297 ConstantPointer(const ConstantPointer &); // DO NOT IMPLEMENT
299 inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
300 ~ConstantPointer() {}
302 virtual std::string getStrValue() const = 0;
303 inline const PointerType *getType() const {
304 return (PointerType*)Value::getType();
307 // isNullValue - Return true if this is the value that would be returned by
309 virtual bool isNullValue() const { return false; }
311 // Methods for support type inquiry through isa, cast, and dyn_cast:
312 static inline bool classof(const ConstantPointer *) { return true; }
313 static bool classof(const Constant *CPV); // defined in CPV.cpp
314 static inline bool classof(const Value *V) {
315 return isa<Constant>(V) && classof(cast<Constant>(V));
319 // ConstantPointerNull - a constant pointer value that points to null
321 class ConstantPointerNull : public ConstantPointer {
322 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
324 inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
325 inline ~ConstantPointerNull() {}
327 virtual std::string getStrValue() const;
329 static ConstantPointerNull *get(const PointerType *T);
331 // isNullValue - Return true if this is the value that would be returned by
333 virtual bool isNullValue() const { return true; }
335 // Methods for support type inquiry through isa, cast, and dyn_cast:
336 static inline bool classof(const ConstantPointerNull *) { return true; }
337 static inline bool classof(const ConstantPointer *P) {
338 return P->getNumOperands() == 0;
340 static inline bool classof(const Constant *CPV) {
341 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
343 static inline bool classof(const Value *V) {
344 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
349 // ConstantPointerRef - a constant pointer value that is initialized to
350 // point to a global value, which lies at a constant, fixed address.
352 class ConstantPointerRef : public ConstantPointer {
353 friend class Module; // Modules maintain these references
354 ConstantPointerRef(const ConstantPointerRef &); // DNI!
357 ConstantPointerRef(GlobalValue *GV);
358 ~ConstantPointerRef() {}
360 virtual void destroyConstant() { destroyConstantImpl(); }
362 static ConstantPointerRef *get(GlobalValue *GV);
364 virtual std::string getStrValue() const;
366 const GlobalValue *getValue() const {
367 return cast<GlobalValue>(Operands[0].get());
369 GlobalValue *getValue() {
370 return cast<GlobalValue>(Operands[0].get());
373 // Methods for support type inquiry through isa, cast, and dyn_cast:
374 static inline bool classof(const ConstantPointerRef *) { return true; }
375 static inline bool classof(const ConstantPointer *CPV) {
376 return CPV->getNumOperands() == 1;
378 static inline bool classof(const Constant *CPV) {
379 return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
381 static inline bool classof(const Value *V) {
382 return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
385 // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD
387 void mutateReference(GlobalValue *NewGV);