95f9a265d29f87e8b188c1c80284a40e638d4e95
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/ConstantVals.h - Constant Value nodes ---------------*- C++ -*--=//
2 //
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
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
10
11 #include "llvm/User.h"
12 #include "Support/DataTypes.h"
13
14 class ArrayType;
15 class StructType;
16 class PointerType;
17
18 //===----------------------------------------------------------------------===//
19 //                                Constant Class
20 //===----------------------------------------------------------------------===//
21
22 class Constant : public User {
23 protected:
24   inline Constant(const Type *Ty) : User(Ty, Value::ConstantVal) {}
25   ~Constant() {}
26
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
33   // delete this.
34   //
35   virtual void destroyConstant() { assert(0 && "Not reached!"); }
36   void destroyConstantImpl();
37 public:
38   // Specialize setName to handle symbol table majik...
39   virtual void setName(const std::string &name, SymbolTable *ST = 0);
40
41   // Static constructor to get a '0' constant of arbitrary type...
42   static Constant *getNullConstant(const Type *Ty);
43
44   // isNullValue - Return true if this is the value that would be returned by
45   // getNullConstant.
46   virtual bool isNullValue() const = 0;
47
48   virtual void print(std::ostream &O) const;
49
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;
54   }
55 };
56
57
58
59 //===----------------------------------------------------------------------===//
60 //              Classes to represent constant pool variable defs
61 //===----------------------------------------------------------------------===//
62
63 //===---------------------------------------------------------------------------
64 // ConstantBool - Boolean Values
65 //
66 class ConstantBool : public Constant {
67   bool Val;
68   ConstantBool(const ConstantBool &);     // DO NOT IMPLEMENT
69   ConstantBool(bool V);
70   ~ConstantBool() {}
71 public:
72   static ConstantBool *True, *False;  // The True & False values
73
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); }
77
78   // inverted - Return the opposite value of the current value.
79   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
80
81   inline bool getValue() const { return Val; }
82
83   // isNullValue - Return true if this is the value that would be returned by
84   // getNullConstant.
85   virtual bool isNullValue() const { return this == False; }
86
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);
91   }
92   static inline bool classof(const Value *V) {
93     return isa<Constant>(V) && classof(cast<Constant>(V));
94   }
95 };
96
97
98 //===---------------------------------------------------------------------------
99 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
100 // with integral constants easier.
101 //
102 class ConstantInt : public Constant {
103 protected:
104   union {
105     int64_t  Signed;
106     uint64_t Unsigned;
107   } Val;
108   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
109   ConstantInt(const Type *Ty, uint64_t V);
110   ~ConstantInt() {}
111 public:
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.
115   //
116   bool equalsInt(unsigned char V) const {
117     assert(V <= 127 &&
118            "equals: Can only be used with very small positive constants!");
119     return Val.Unsigned == V;
120   }
121
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.
124   //
125   static ConstantInt *get(const Type *Ty, unsigned char V);
126
127   // isNullValue - Return true if this is the value that would be returned by
128   // getNullConstant.
129   virtual bool isNullValue() const { return Val.Unsigned == 0; }
130
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));
136   }
137 };
138
139
140 //===---------------------------------------------------------------------------
141 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
142 //
143 class ConstantSInt : public ConstantInt {
144   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
145 protected:
146   ConstantSInt(const Type *Ty, int64_t V);
147   ~ConstantSInt() {}
148 public:
149   static ConstantSInt *get(const Type *Ty, int64_t V);
150
151   static bool isValueValidForType(const Type *Ty, int64_t V);
152   inline int64_t getValue() const { return Val.Signed; }
153
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));
159   }
160 };
161
162 //===---------------------------------------------------------------------------
163 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
164 //
165 class ConstantUInt : public ConstantInt {
166   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
167 protected:
168   ConstantUInt(const Type *Ty, uint64_t V);
169   ~ConstantUInt() {}
170 public:
171   static ConstantUInt *get(const Type *Ty, uint64_t V);
172
173   static bool isValueValidForType(const Type *Ty, uint64_t V);
174   inline uint64_t getValue() const { return Val.Unsigned; }
175
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));
181   }
182 };
183
184
185 //===---------------------------------------------------------------------------
186 // ConstantFP - Floating Point Values [float, double]
187 //
188 class ConstantFP : public Constant {
189   double Val;
190   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
191 protected:
192   ConstantFP(const Type *Ty, double V);
193   ~ConstantFP() {}
194 public:
195   static ConstantFP *get(const Type *Ty, double V);
196
197   static bool isValueValidForType(const Type *Ty, double V);
198   inline double getValue() const { return Val; }
199
200   // isNullValue - Return true if this is the value that would be returned by
201   // getNullConstant.
202   virtual bool isNullValue() const { return Val == 0; }
203
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));
209   }
210 };
211
212
213 //===---------------------------------------------------------------------------
214 // ConstantArray - Constant Array Declarations
215 //
216 class ConstantArray : public Constant {
217   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
218 protected:
219   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
220   ~ConstantArray() {}
221
222   virtual void destroyConstant();
223 public:
224   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
225   static ConstantArray *get(const std::string &Initializer);
226   
227   inline const ArrayType *getType() const {
228     return (ArrayType*)Value::getType();
229   }
230
231   inline const std::vector<Use> &getValues() const { return Operands; }
232
233   // isNullValue - Return true if this is the value that would be returned by
234   // getNullConstant.
235   virtual bool isNullValue() const { return false; }
236
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));
242   }
243 };
244
245
246 //===---------------------------------------------------------------------------
247 // ConstantStruct - Constant Struct Declarations
248 //
249 class ConstantStruct : public Constant {
250   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
251 protected:
252   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
253   ~ConstantStruct() {}
254
255   virtual void destroyConstant();
256 public:
257   static ConstantStruct *get(const StructType *T,
258                              const std::vector<Constant*> &V);
259
260   inline const StructType *getType() const {
261     return (StructType*)Value::getType();
262   }
263
264   inline const std::vector<Use> &getValues() const { return Operands; }
265
266   // isNullValue - Return true if this is the value that would be returned by
267   // getNullConstant.
268   virtual bool isNullValue() const { return false; }
269
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));
275   }
276 };
277
278 //===---------------------------------------------------------------------------
279 // ConstantPointer - Constant Pointer Declarations
280 //
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
283 // used.
284 //
285 class ConstantPointer : public Constant {
286   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
287 protected:
288   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
289   ~ConstantPointer() {}
290 public:
291   inline const PointerType *getType() const {
292     return (PointerType*)Value::getType();
293   }
294
295   // isNullValue - Return true if this is the value that would be returned by
296   // getNullConstant.
297   virtual bool isNullValue() const { return false; }
298
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));
304   }
305 };
306
307 // ConstantPointerNull - a constant pointer value that points to null
308 //
309 class ConstantPointerNull : public ConstantPointer {
310   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
311 protected:
312   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
313   inline ~ConstantPointerNull() {}
314 public:
315
316   static ConstantPointerNull *get(const PointerType *T);
317
318   // isNullValue - Return true if this is the value that would be returned by
319   // getNullConstant.
320   virtual bool isNullValue() const { return true; }
321
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;
326   }
327   static inline bool classof(const Constant *CPV) {
328     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
329   }
330   static inline bool classof(const Value *V) {
331     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
332   }
333 };
334
335
336 // ConstantPointerRef - a constant pointer value that is initialized to
337 // point to a global value, which lies at a constant, fixed address.
338 //
339 class ConstantPointerRef : public ConstantPointer {
340   friend class Module;   // Modules maintain these references
341   ConstantPointerRef(const ConstantPointerRef &); // DNI!
342
343 protected:
344   ConstantPointerRef(GlobalValue *GV);
345   ~ConstantPointerRef() {}
346
347   virtual void destroyConstant() { destroyConstantImpl(); }
348 public:
349   static ConstantPointerRef *get(GlobalValue *GV);
350
351   const GlobalValue *getValue() const { 
352     return cast<GlobalValue>(Operands[0].get());
353   }
354   GlobalValue *getValue() {
355     return cast<GlobalValue>(Operands[0].get());
356   }
357
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;
362   }
363   static inline bool classof(const Constant *CPV) {
364     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
365   }
366   static inline bool classof(const Value *V) {
367     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
368   }
369
370   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
371   // NOT USE THIS!!
372   void mutateReference(GlobalValue *NewGV);
373   // END WARNING!!
374 };
375
376
377
378 #endif