1 //===-- llvm/ConstPoolVals.h - Constant Value nodes --------------*- C++ -*--=//
3 // This file contains the declarations for the ConstPoolVal 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 "llvm/Support/DataTypes.h"
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
22 class ConstPoolVal : public User {
24 inline ConstPoolVal(const Type *Ty) : User(Ty, Value::ConstantVal) {}
27 // Specialize setName to handle symbol table majik...
28 virtual void setName(const string &name, SymbolTable *ST = 0);
30 virtual string getStrValue() const = 0;
32 // Static constructor to get a '0' constant of arbitrary type...
33 static ConstPoolVal *getNullConstant(const Type *Ty);
38 //===----------------------------------------------------------------------===//
39 // Classes to represent constant pool variable defs
40 //===----------------------------------------------------------------------===//
42 //===---------------------------------------------------------------------------
43 // ConstPoolBool - Boolean Values
45 class ConstPoolBool : public ConstPoolVal {
47 ConstPoolBool(const ConstPoolBool &); // DO NOT IMPLEMENT
48 ConstPoolBool(bool V);
50 static ConstPoolBool *True, *False; // The True & False values
52 // Factory objects - Return objects of the specified value
53 static ConstPoolBool *get(bool Value) { return Value ? True : False; }
54 static ConstPoolBool *get(const Type *Ty, bool Value) { return get(Value); }
56 // inverted - Return the opposite value of the current value.
57 inline ConstPoolBool *inverted() const { return (this==True) ? False : True; }
59 virtual string getStrValue() const;
60 inline bool getValue() const { return Val; }
64 //===---------------------------------------------------------------------------
65 // ConstPoolInt - Superclass of ConstPoolSInt & ConstPoolUInt, to make dealing
66 // with integral constants easier.
68 class ConstPoolInt : public ConstPoolVal {
74 ConstPoolInt(const ConstPoolInt &); // DO NOT IMPLEMENT
75 ConstPoolInt(const Type *Ty, uint64_t V);
77 // equalsInt - Provide a helper method that can be used to determine if the
78 // constant contained within is equal to a constant. This only works for very
79 // small values, because this is all that can be represented with all types.
81 bool equalsInt(unsigned char V) const {
83 "equals: Can only be used with very small positive constants!");
84 return Val.Unsigned == V;
87 // ConstPoolInt::get static method: return a constant pool int with the
88 // specified value. as above, we work only with very small values here.
90 static ConstPoolInt *get(const Type *Ty, unsigned char V);
94 //===---------------------------------------------------------------------------
95 // ConstPoolSInt - Signed Integer Values [sbyte, short, int, long]
97 class ConstPoolSInt : public ConstPoolInt {
98 ConstPoolSInt(const ConstPoolSInt &); // DO NOT IMPLEMENT
100 ConstPoolSInt(const Type *Ty, int64_t V);
102 static ConstPoolSInt *get(const Type *Ty, int64_t V);
104 virtual string getStrValue() const;
106 static bool isValueValidForType(const Type *Ty, int64_t V);
107 inline int64_t getValue() const { return Val.Signed; }
111 //===---------------------------------------------------------------------------
112 // ConstPoolUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
114 class ConstPoolUInt : public ConstPoolInt {
115 ConstPoolUInt(const ConstPoolUInt &); // DO NOT IMPLEMENT
117 ConstPoolUInt(const Type *Ty, uint64_t V);
119 static ConstPoolUInt *get(const Type *Ty, uint64_t V);
121 virtual string getStrValue() const;
123 static bool isValueValidForType(const Type *Ty, uint64_t V);
124 inline uint64_t getValue() const { return Val.Unsigned; }
128 //===---------------------------------------------------------------------------
129 // ConstPoolFP - Floating Point Values [float, double]
131 class ConstPoolFP : public ConstPoolVal {
133 ConstPoolFP(const ConstPoolFP &); // DO NOT IMPLEMENT
135 ConstPoolFP(const Type *Ty, double V);
137 static ConstPoolFP *get(const Type *Ty, double V);
139 virtual string getStrValue() const;
141 static bool isValueValidForType(const Type *Ty, double V);
142 inline double getValue() const { return Val; }
146 //===---------------------------------------------------------------------------
147 // ConstPoolArray - Constant Array Declarations
149 class ConstPoolArray : public ConstPoolVal {
150 ConstPoolArray(const ConstPoolArray &); // DO NOT IMPLEMENT
152 ConstPoolArray(const ArrayType *T, const vector<ConstPoolVal*> &Val);
154 static ConstPoolArray *get(const ArrayType *T, const vector<ConstPoolVal*> &);
156 virtual string getStrValue() const;
158 inline const vector<Use> &getValues() const { return Operands; }
162 //===---------------------------------------------------------------------------
163 // ConstPoolStruct - Constant Struct Declarations
165 class ConstPoolStruct : public ConstPoolVal {
166 ConstPoolStruct(const ConstPoolStruct &); // DO NOT IMPLEMENT
168 ConstPoolStruct(const StructType *T, const vector<ConstPoolVal*> &Val);
170 static ConstPoolStruct *get(const StructType *T,
171 const vector<ConstPoolVal*> &V);
173 virtual string getStrValue() const;
175 inline const vector<Use> &getValues() const { return Operands; }
178 //===---------------------------------------------------------------------------
179 // External functions
182 // Convenience functions to get the value of an integer constant, for an
183 // appropriate integer or non-integer type that can be held in an integer.
184 // The type of the argument must be the following:
185 // GetSignedIntConstantValue: signed integer or bool
186 // GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
187 // GetConstantValueAsSignedInt: any of the above, but the value
188 // must fit into a int64_t.
190 // isValidConstant is set to true if a valid constant was found.
192 int64_t GetSignedIntConstantValue (const Value* val, bool& isValidConst);
193 uint64_t GetUnsignedIntConstantValue (const Value* val, bool& isValidConst);
194 int64_t GetConstantValueAsSignedInt (const Value* val, bool& isValidConst);
196 //===---------------------------------------------------------------------------