doxygenize comments in header
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
2 //
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
10
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
13
14 class ArrayType;
15 class StructType;
16 class PointerType;
17
18
19 //===---------------------------------------------------------------------------
20 // ConstantIntegral - Shared superclass of boolean and integer constants.
21 //
22 // This class just defines some common interfaces to be implemented.
23 //
24 class ConstantIntegral : public Constant {
25 protected:
26   ConstantIntegral(const Type *Ty) : Constant(Ty) {}
27 public:
28
29   // isNullValue - Return true if this is the value that would be returned by
30   // getNullValue.
31   //
32   virtual bool isNullValue() const = 0;
33
34   // isMaxValue - Return true if this is the largest value that may be
35   // represented by this type.
36   //
37   virtual bool isMaxValue() const = 0;
38
39   // isMinValue - Return true if this is the smallest value that may be
40   // represented by this type.
41   //
42   virtual bool isMinValue() const = 0;
43
44   // isAllOnesValue - Return true if every bit in this constant is set to true.
45   //
46   virtual bool isAllOnesValue() const = 0;
47
48   // Static constructor to get the maximum/minimum/allones constant of specified
49   // (integral) type...
50   //
51   static ConstantIntegral *getMaxValue(const Type *Ty);
52   static ConstantIntegral *getMinValue(const Type *Ty);
53   static ConstantIntegral *getAllOnesValue(const Type *Ty);
54
55   // Methods for support type inquiry through isa, cast, and dyn_cast:
56   static inline bool classof(const ConstantIntegral *) { return true; }
57   static bool classof(const Constant *CPV);  // defined in Constants.cpp
58   static inline bool classof(const Value *V) {
59     return isa<Constant>(V) && classof(cast<Constant>(V));
60   }
61 };
62
63
64 //===---------------------------------------------------------------------------
65 // ConstantBool - Boolean Values
66 //
67 class ConstantBool : public ConstantIntegral {
68   bool Val;
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   // getNullValue.
85   //
86   virtual bool isNullValue() const { return this == False; }
87   virtual bool isMaxValue() const { return this == True; }
88   virtual bool isMinValue() const { return this == False; }
89   virtual bool isAllOnesValue() const { return this == True; }
90
91   // Methods for support type inquiry through isa, cast, and dyn_cast:
92   static inline bool classof(const ConstantBool *) { return true; }
93   static bool classof(const Constant *CPV) {
94     return (CPV == True) | (CPV == False);
95   }
96   static inline bool classof(const Value *V) {
97     return isa<Constant>(V) && classof(cast<Constant>(V));
98   }
99 };
100
101
102 //===---------------------------------------------------------------------------
103 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
104 // with integral constants easier.
105 //
106 class ConstantInt : public ConstantIntegral {
107 protected:
108   union {
109     int64_t  Signed;
110     uint64_t Unsigned;
111   } Val;
112   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
113   ConstantInt(const Type *Ty, uint64_t V);
114   ~ConstantInt() {}
115 public:
116   // equalsInt - Provide a helper method that can be used to determine if the 
117   // constant contained within is equal to a constant.  This only works for very
118   // small values, because this is all that can be represented with all types.
119   //
120   bool equalsInt(unsigned char V) const {
121     assert(V <= 127 &&
122            "equals: Can only be used with very small positive constants!");
123     return Val.Unsigned == V;
124   }
125
126   // ConstantInt::get static method: return a constant pool int with the
127   // specified value.  as above, we work only with very small values here.
128   //
129   static ConstantInt *get(const Type *Ty, unsigned char V);
130
131   // isNullValue - Return true if this is the value that would be returned by
132   // getNullValue.
133   virtual bool isNullValue() const { return Val.Unsigned == 0; }
134   virtual bool isAllOnesValue() const { return Val.Signed == -1; }
135   virtual bool isMaxValue() const = 0;
136   virtual bool isMinValue() const = 0;
137
138   // Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const ConstantInt *) { return true; }
140   static bool classof(const Constant *CPV);  // defined in Constants.cpp
141   static inline bool classof(const Value *V) {
142     return isa<Constant>(V) && classof(cast<Constant>(V));
143   }
144 };
145
146
147 //===---------------------------------------------------------------------------
148 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
149 //
150 class ConstantSInt : public ConstantInt {
151   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
152 protected:
153   ConstantSInt(const Type *Ty, int64_t V);
154   ~ConstantSInt() {}
155 public:
156   static ConstantSInt *get(const Type *Ty, int64_t V);
157
158   static bool isValueValidForType(const Type *Ty, int64_t V);
159   inline int64_t getValue() const { return Val.Signed; }
160
161   // isMaxValue - Return true if this is the largest value that may be
162   // represented by this type.
163   //
164   virtual bool isMaxValue() const {
165     int64_t V = getValue();
166     if (V < 0) return false;    // Be careful about wrap-around on 'long's
167     ++V;
168     return !isValueValidForType(getType(), V) || V < 0;
169   }
170
171   // isMinValue - Return true if this is the smallest value that may be
172   // represented by this type.
173   //
174   virtual bool isMinValue() const {
175     int64_t V = getValue();
176     if (V > 0) return false;    // Be careful about wrap-around on 'long's
177     --V;
178     return !isValueValidForType(getType(), V) || V > 0;
179   }
180
181   // Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const ConstantSInt *) { return true; }
183   static bool classof(const Constant *CPV);  // defined in Constants.cpp
184   static inline bool classof(const Value *V) {
185     return isa<Constant>(V) && classof(cast<Constant>(V));
186   }
187 };
188
189 //===---------------------------------------------------------------------------
190 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
191 //
192 class ConstantUInt : public ConstantInt {
193   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
194 protected:
195   ConstantUInt(const Type *Ty, uint64_t V);
196   ~ConstantUInt() {}
197 public:
198   static ConstantUInt *get(const Type *Ty, uint64_t V);
199
200   static bool isValueValidForType(const Type *Ty, uint64_t V);
201   inline uint64_t getValue() const { return Val.Unsigned; }
202
203   // isMaxValue - Return true if this is the largest value that may be
204   // represented by this type.
205   //
206   virtual bool isMaxValue() const { return isAllOnesValue(); }
207   virtual bool isMinValue() const { return getValue() == 0; }
208
209   // Methods for support type inquiry through isa, cast, and dyn_cast:
210   static inline bool classof(const ConstantUInt *) { return true; }
211   static bool classof(const Constant *CPV);  // defined in Constants.cpp
212   static inline bool classof(const Value *V) {
213     return isa<Constant>(V) && classof(cast<Constant>(V));
214   }
215 };
216
217
218 //===---------------------------------------------------------------------------
219 // ConstantFP - Floating Point Values [float, double]
220 //
221 class ConstantFP : public Constant {
222   double Val;
223   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
224 protected:
225   ConstantFP(const Type *Ty, double V);
226   ~ConstantFP() {}
227 public:
228   static ConstantFP *get(const Type *Ty, double V);
229
230   static bool isValueValidForType(const Type *Ty, double V);
231   inline double getValue() const { return Val; }
232
233   // isNullValue - Return true if this is the value that would be returned by
234   // getNullValue.
235   virtual bool isNullValue() const { return Val == 0; }
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const ConstantFP *) { return true; }
239   static bool classof(const Constant *CPV);  // defined in Constants.cpp
240   static inline bool classof(const Value *V) {
241     return isa<Constant>(V) && classof(cast<Constant>(V));
242   }
243 };
244
245
246 //===---------------------------------------------------------------------------
247 // ConstantArray - Constant Array Declarations
248 //
249 class ConstantArray : public Constant {
250   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
251 protected:
252   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
253   ~ConstantArray() {}
254
255 public:
256   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
257   static ConstantArray *get(const std::string &Initializer);
258   
259   inline const ArrayType *getType() const {
260     return (ArrayType*)Value::getType();
261   }
262
263   inline const std::vector<Use> &getValues() const { return Operands; }
264
265   // isNullValue - Return true if this is the value that would be returned by
266   // getNullValue.
267   virtual bool isNullValue() const { return false; }
268
269   virtual void destroyConstant();
270
271   // Methods for support type inquiry through isa, cast, and dyn_cast:
272   static inline bool classof(const ConstantArray *) { return true; }
273   static bool classof(const Constant *CPV);  // defined in Constants.cpp
274   static inline bool classof(const Value *V) {
275     return isa<Constant>(V) && classof(cast<Constant>(V));
276   }
277 };
278
279
280 //===---------------------------------------------------------------------------
281 // ConstantStruct - Constant Struct Declarations
282 //
283 class ConstantStruct : public Constant {
284   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
285 protected:
286   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
287   ~ConstantStruct() {}
288
289 public:
290   static ConstantStruct *get(const StructType *T,
291                              const std::vector<Constant*> &V);
292
293   inline const StructType *getType() const {
294     return (StructType*)Value::getType();
295   }
296
297   inline const std::vector<Use> &getValues() const { return Operands; }
298
299   // isNullValue - Return true if this is the value that would be returned by
300   // getNullValue.
301   virtual bool isNullValue() const { return false; }
302
303   virtual void destroyConstant();
304
305   // Methods for support type inquiry through isa, cast, and dyn_cast:
306   static inline bool classof(const ConstantStruct *) { return true; }
307   static bool classof(const Constant *CPV);  // defined in Constants.cpp
308   static inline bool classof(const Value *V) {
309     return isa<Constant>(V) && classof(cast<Constant>(V));
310   }
311 };
312
313 //===---------------------------------------------------------------------------
314 // ConstantPointer - Constant Pointer Declarations
315 //
316 // The ConstantPointer class represents a null pointer of a specific type. For
317 // a more specific/useful instance, a subclass of ConstantPointer should be
318 // used.
319 //
320 class ConstantPointer : public Constant {
321   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
322 protected:
323   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
324   ~ConstantPointer() {}
325 public:
326   inline const PointerType *getType() const {
327     return (PointerType*)Value::getType();
328   }
329
330   // isNullValue - Return true if this is the value that would be returned by
331   // getNullValue.
332   virtual bool isNullValue() const { return false; }
333
334   // Methods for support type inquiry through isa, cast, and dyn_cast:
335   static inline bool classof(const ConstantPointer *) { return true; }
336   static bool classof(const Constant *CPV);  // defined in Constants.cpp
337   static inline bool classof(const Value *V) {
338     return isa<Constant>(V) && classof(cast<Constant>(V));
339   }
340 };
341
342 // ConstantPointerNull - a constant pointer value that points to null
343 //
344 class ConstantPointerNull : public ConstantPointer {
345   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
346 protected:
347   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
348   inline ~ConstantPointerNull() {}
349 public:
350
351   static ConstantPointerNull *get(const PointerType *T);
352
353   // isNullValue - Return true if this is the value that would be returned by
354   // getNullValue.
355   virtual bool isNullValue() const { return true; }
356
357   virtual void destroyConstant();
358
359   // Methods for support type inquiry through isa, cast, and dyn_cast:
360   static inline bool classof(const ConstantPointerNull *) { return true; }
361   static inline bool classof(const ConstantPointer *P) {
362     return (P->getNumOperands() == 0 && P->isNullValue());
363   }
364   static inline bool classof(const Constant *CPV) {
365     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
366   }
367   static inline bool classof(const Value *V) {
368     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
369   }
370 };
371
372
373 // ConstantPointerRef - a constant pointer value that is initialized to
374 // point to a global value, which lies at a constant, fixed address.
375 //
376 class ConstantPointerRef : public ConstantPointer {
377   friend class Module;   // Modules maintain these references
378   ConstantPointerRef(const ConstantPointerRef &); // DNI!
379
380 protected:
381   ConstantPointerRef(GlobalValue *GV);
382   ~ConstantPointerRef() {}
383 public:
384   static ConstantPointerRef *get(GlobalValue *GV);
385
386   const GlobalValue *getValue() const { 
387     return cast<GlobalValue>(Operands[0].get());
388   }
389   GlobalValue *getValue() {
390     return cast<GlobalValue>(Operands[0].get());
391   }
392
393   virtual void destroyConstant();
394
395   // Methods for support type inquiry through isa, cast, and dyn_cast:
396   static inline bool classof(const ConstantPointerRef *) { return true; }
397   static inline bool classof(const ConstantPointer *CPV) {
398     // check for a single operand (the target value)
399     return (CPV->getNumOperands() == 1);
400   }
401   static inline bool classof(const Constant *CPV) {
402     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
403   }
404   static inline bool classof(const Value *V) {
405     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
406   }
407
408   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
409   // NOT USE THIS!!
410   // Returns the number of uses of OldV that were replaced.
411   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
412   // END WARNING!!
413 };
414
415
416 // ConstantExpr - a constant value that is initialized with
417 // an expression using other constant values.  This is only used
418 // to represent values that cannot be evaluated at compile-time
419 // (e.g., something derived from an address) because it does
420 // not have a mechanism to store the actual value.
421 // Use the appropriate Constant subclass above for known constants.
422 //
423 class ConstantExpr : public Constant {
424   unsigned iType;      // Operation type
425   
426 protected:
427   ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
428   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
429   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
430                const Type *DestTy);
431   ~ConstantExpr() {}
432   
433 public:
434   // Static methods to construct a ConstantExpr of different kinds.
435   
436   // Cast constant expr
437   static ConstantExpr *getCast(Constant *C, const Type *Ty);
438
439   // Binary constant expr - Use with binary operators...
440   static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
441
442   // Getelementptr form...
443   static ConstantExpr *getGetElementPtr(Constant *C,
444                                         const std::vector<Constant*> &IdxList);
445   
446   // isNullValue - Return true if this is the value that would be returned by
447   // getNullValue.
448   virtual bool isNullValue() const { return false; }
449   
450   // getOpcode - Return the opcode at the root of this constant expression
451   unsigned getOpcode() const { return iType; }
452
453   // getOpcodeName - Return a string representation for an opcode.
454   const char *getOpcodeName() const;
455   
456   // isConstantExpr - Return true if this is a ConstantExpr
457   virtual bool isConstantExpr() const { return true; }
458
459   virtual void destroyConstant();
460     
461   // Methods for support type inquiry through isa, cast, and dyn_cast:
462   static inline bool classof(const ConstantExpr *) { return true; }
463   static inline bool classof(const Constant *CPV) {
464     return CPV->isConstantExpr();
465   }
466   static inline bool classof(const Value *V) {
467     return isa<Constant>(V) && classof(cast<Constant>(V));
468   }
469
470 public:
471   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
472   // NOT USE THIS!!
473   // Returns the number of uses of OldV that were replaced.
474   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
475   // END WARNING!!
476 };
477
478
479 #endif