* Add virtual print methods
[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   virtual std::string getStrValue() const = 0;
42
43   // Static constructor to get a '0' constant of arbitrary type...
44   static Constant *getNullConstant(const Type *Ty);
45
46   // isNullValue - Return true if this is the value that would be returned by
47   // getNullConstant.
48   virtual bool isNullValue() const = 0;
49
50   virtual void print(std::ostream &O) const;
51
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;
56   }
57 };
58
59
60
61 //===----------------------------------------------------------------------===//
62 //              Classes to represent constant pool variable defs
63 //===----------------------------------------------------------------------===//
64
65 //===---------------------------------------------------------------------------
66 // ConstantBool - Boolean Values
67 //
68 class ConstantBool : public Constant {
69   bool Val;
70   ConstantBool(const ConstantBool &);     // DO NOT IMPLEMENT
71   ConstantBool(bool V);
72   ~ConstantBool() {}
73 public:
74   static ConstantBool *True, *False;  // The True & False values
75
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); }
79
80   // inverted - Return the opposite value of the current value.
81   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
82
83   virtual std::string getStrValue() const;
84   inline bool getValue() const { return Val; }
85
86   // isNullValue - Return true if this is the value that would be returned by
87   // getNullConstant.
88   virtual bool isNullValue() const { return this == False; }
89
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);
94   }
95   static inline bool classof(const Value *V) {
96     return isa<Constant>(V) && classof(cast<Constant>(V));
97   }
98 };
99
100
101 //===---------------------------------------------------------------------------
102 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
103 // with integral constants easier.
104 //
105 class ConstantInt : public Constant {
106 protected:
107   union {
108     int64_t  Signed;
109     uint64_t Unsigned;
110   } Val;
111   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
112   ConstantInt(const Type *Ty, uint64_t V);
113   ~ConstantInt() {}
114 public:
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.
118   //
119   bool equalsInt(unsigned char V) const {
120     assert(V <= 127 &&
121            "equals: Can only be used with very small positive constants!");
122     return Val.Unsigned == V;
123   }
124
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.
127   //
128   static ConstantInt *get(const Type *Ty, unsigned char V);
129
130   // isNullValue - Return true if this is the value that would be returned by
131   // getNullConstant.
132   virtual bool isNullValue() const { return Val.Unsigned == 0; }
133
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));
139   }
140 };
141
142
143 //===---------------------------------------------------------------------------
144 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
145 //
146 class ConstantSInt : public ConstantInt {
147   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
148 protected:
149   ConstantSInt(const Type *Ty, int64_t V);
150   ~ConstantSInt() {}
151 public:
152   static ConstantSInt *get(const Type *Ty, int64_t V);
153
154   virtual std::string getStrValue() const;
155
156   static bool isValueValidForType(const Type *Ty, int64_t V);
157   inline int64_t getValue() const { return Val.Signed; }
158
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));
164   }
165 };
166
167 //===---------------------------------------------------------------------------
168 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
169 //
170 class ConstantUInt : public ConstantInt {
171   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
172 protected:
173   ConstantUInt(const Type *Ty, uint64_t V);
174   ~ConstantUInt() {}
175 public:
176   static ConstantUInt *get(const Type *Ty, uint64_t V);
177
178   virtual std::string getStrValue() const;
179
180   static bool isValueValidForType(const Type *Ty, uint64_t V);
181   inline uint64_t getValue() const { return Val.Unsigned; }
182
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));
188   }
189 };
190
191
192 //===---------------------------------------------------------------------------
193 // ConstantFP - Floating Point Values [float, double]
194 //
195 class ConstantFP : public Constant {
196   double Val;
197   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
198 protected:
199   ConstantFP(const Type *Ty, double V);
200   ~ConstantFP() {}
201 public:
202   static ConstantFP *get(const Type *Ty, double V);
203
204   virtual std::string getStrValue() const;
205
206   static bool isValueValidForType(const Type *Ty, double V);
207   inline double getValue() const { return Val; }
208
209   // isNullValue - Return true if this is the value that would be returned by
210   // getNullConstant.
211   virtual bool isNullValue() const { return Val == 0; }
212
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));
218   }
219 };
220
221
222 //===---------------------------------------------------------------------------
223 // ConstantArray - Constant Array Declarations
224 //
225 class ConstantArray : public Constant {
226   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
227 protected:
228   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
229   ~ConstantArray() {}
230
231   virtual void destroyConstant();
232 public:
233   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
234   static ConstantArray *get(const std::string &Initializer);
235   
236   virtual std::string getStrValue() const;
237   inline const ArrayType *getType() const {
238     return (ArrayType*)Value::getType();
239   }
240
241   inline const std::vector<Use> &getValues() const { return Operands; }
242
243   // isNullValue - Return true if this is the value that would be returned by
244   // getNullConstant.
245   virtual bool isNullValue() const { return false; }
246
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));
252   }
253 };
254
255
256 //===---------------------------------------------------------------------------
257 // ConstantStruct - Constant Struct Declarations
258 //
259 class ConstantStruct : public Constant {
260   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
261 protected:
262   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
263   ~ConstantStruct() {}
264
265   virtual void destroyConstant();
266 public:
267   static ConstantStruct *get(const StructType *T,
268                              const std::vector<Constant*> &V);
269
270   virtual std::string getStrValue() const;
271   inline const StructType *getType() const {
272     return (StructType*)Value::getType();
273   }
274
275   inline const std::vector<Use> &getValues() const { return Operands; }
276
277   // isNullValue - Return true if this is the value that would be returned by
278   // getNullConstant.
279   virtual bool isNullValue() const { return false; }
280
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));
286   }
287 };
288
289 //===---------------------------------------------------------------------------
290 // ConstantPointer - Constant Pointer Declarations
291 //
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
294 // used.
295 //
296 class ConstantPointer : public Constant {
297   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
298 protected:
299   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
300   ~ConstantPointer() {}
301 public:
302   virtual std::string getStrValue() const = 0;
303   inline const PointerType *getType() const {
304     return (PointerType*)Value::getType();
305   }
306
307   // isNullValue - Return true if this is the value that would be returned by
308   // getNullConstant.
309   virtual bool isNullValue() const { return false; }
310
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));
316   }
317 };
318
319 // ConstantPointerNull - a constant pointer value that points to null
320 //
321 class ConstantPointerNull : public ConstantPointer {
322   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
323 protected:
324   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
325   inline ~ConstantPointerNull() {}
326 public:
327   virtual std::string getStrValue() const;
328
329   static ConstantPointerNull *get(const PointerType *T);
330
331   // isNullValue - Return true if this is the value that would be returned by
332   // getNullConstant.
333   virtual bool isNullValue() const { return true; }
334
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;
339   }
340   static inline bool classof(const Constant *CPV) {
341     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
342   }
343   static inline bool classof(const Value *V) {
344     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
345   }
346 };
347
348
349 // ConstantPointerRef - a constant pointer value that is initialized to
350 // point to a global value, which lies at a constant, fixed address.
351 //
352 class ConstantPointerRef : public ConstantPointer {
353   friend class Module;   // Modules maintain these references
354   ConstantPointerRef(const ConstantPointerRef &); // DNI!
355
356 protected:
357   ConstantPointerRef(GlobalValue *GV);
358   ~ConstantPointerRef() {}
359
360   virtual void destroyConstant() { destroyConstantImpl(); }
361 public:
362   static ConstantPointerRef *get(GlobalValue *GV);
363
364   virtual std::string getStrValue() const;
365
366   const GlobalValue *getValue() const { 
367     return cast<GlobalValue>(Operands[0].get());
368   }
369   GlobalValue *getValue() {
370     return cast<GlobalValue>(Operands[0].get());
371   }
372
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;
377   }
378   static inline bool classof(const Constant *CPV) {
379     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
380   }
381   static inline bool classof(const Value *V) {
382     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
383   }
384
385   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
386   // NOT USE THIS!!
387   void mutateReference(GlobalValue *NewGV);
388   // END WARNING!!
389 };
390
391
392
393 #endif