Lexer doesn't create typehandle gross stuff now, parser does.
[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   // 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   virtual std::string getStrValue() const;
82   inline bool getValue() const { return Val; }
83
84   // isNullValue - Return true if this is the value that would be returned by
85   // getNullConstant.
86   virtual bool isNullValue() const { return this == False; }
87
88   // Methods for support type inquiry through isa, cast, and dyn_cast:
89   static inline bool classof(const ConstantBool *) { return true; }
90   static bool classof(const Constant *CPV) {
91     return (CPV == True) | (CPV == False);
92   }
93   static inline bool classof(const Value *V) {
94     return isa<Constant>(V) && classof(cast<Constant>(V));
95   }
96 };
97
98
99 //===---------------------------------------------------------------------------
100 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
101 // with integral constants easier.
102 //
103 class ConstantInt : public Constant {
104 protected:
105   union {
106     int64_t  Signed;
107     uint64_t Unsigned;
108   } Val;
109   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
110   ConstantInt(const Type *Ty, uint64_t V);
111   ~ConstantInt() {}
112 public:
113   // equalsInt - Provide a helper method that can be used to determine if the 
114   // constant contained within is equal to a constant.  This only works for very
115   // small values, because this is all that can be represented with all types.
116   //
117   bool equalsInt(unsigned char V) const {
118     assert(V <= 127 &&
119            "equals: Can only be used with very small positive constants!");
120     return Val.Unsigned == V;
121   }
122
123   // ConstantInt::get static method: return a constant pool int with the
124   // specified value.  as above, we work only with very small values here.
125   //
126   static ConstantInt *get(const Type *Ty, unsigned char V);
127
128   // isNullValue - Return true if this is the value that would be returned by
129   // getNullConstant.
130   virtual bool isNullValue() const { return Val.Unsigned == 0; }
131
132   // Methods for support type inquiry through isa, cast, and dyn_cast:
133   static inline bool classof(const ConstantInt *) { return true; }
134   static bool classof(const Constant *CPV);  // defined in CPV.cpp
135   static inline bool classof(const Value *V) {
136     return isa<Constant>(V) && classof(cast<Constant>(V));
137   }
138 };
139
140
141 //===---------------------------------------------------------------------------
142 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
143 //
144 class ConstantSInt : public ConstantInt {
145   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
146 protected:
147   ConstantSInt(const Type *Ty, int64_t V);
148   ~ConstantSInt() {}
149 public:
150   static ConstantSInt *get(const Type *Ty, int64_t V);
151
152   virtual std::string getStrValue() const;
153
154   static bool isValueValidForType(const Type *Ty, int64_t V);
155   inline int64_t getValue() const { return Val.Signed; }
156
157   // Methods for support type inquiry through isa, cast, and dyn_cast:
158   static inline bool classof(const ConstantSInt *) { return true; }
159   static bool classof(const Constant *CPV);  // defined in CPV.cpp
160   static inline bool classof(const Value *V) {
161     return isa<Constant>(V) && classof(cast<Constant>(V));
162   }
163 };
164
165 //===---------------------------------------------------------------------------
166 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
167 //
168 class ConstantUInt : public ConstantInt {
169   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
170 protected:
171   ConstantUInt(const Type *Ty, uint64_t V);
172   ~ConstantUInt() {}
173 public:
174   static ConstantUInt *get(const Type *Ty, uint64_t V);
175
176   virtual std::string getStrValue() const;
177
178   static bool isValueValidForType(const Type *Ty, uint64_t V);
179   inline uint64_t getValue() const { return Val.Unsigned; }
180
181   // Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const ConstantUInt *) { return true; }
183   static bool classof(const Constant *CPV);  // defined in CPV.cpp
184   static inline bool classof(const Value *V) {
185     return isa<Constant>(V) && classof(cast<Constant>(V));
186   }
187 };
188
189
190 //===---------------------------------------------------------------------------
191 // ConstantFP - Floating Point Values [float, double]
192 //
193 class ConstantFP : public Constant {
194   double Val;
195   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
196 protected:
197   ConstantFP(const Type *Ty, double V);
198   ~ConstantFP() {}
199 public:
200   static ConstantFP *get(const Type *Ty, double V);
201
202   virtual std::string getStrValue() const;
203
204   static bool isValueValidForType(const Type *Ty, double V);
205   inline double getValue() const { return Val; }
206
207   // isNullValue - Return true if this is the value that would be returned by
208   // getNullConstant.
209   virtual bool isNullValue() const { return Val == 0; }
210
211   // Methods for support type inquiry through isa, cast, and dyn_cast:
212   static inline bool classof(const ConstantFP *) { return true; }
213   static bool classof(const Constant *CPV);  // defined in CPV.cpp
214   static inline bool classof(const Value *V) {
215     return isa<Constant>(V) && classof(cast<Constant>(V));
216   }
217 };
218
219
220 //===---------------------------------------------------------------------------
221 // ConstantArray - Constant Array Declarations
222 //
223 class ConstantArray : public Constant {
224   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
225 protected:
226   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
227   ~ConstantArray() {}
228
229   virtual void destroyConstant();
230 public:
231   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
232   static ConstantArray *get(const std::string &Initializer);
233   
234   virtual std::string getStrValue() const;
235   inline const ArrayType *getType() const {
236     return (ArrayType*)Value::getType();
237   }
238
239   inline const std::vector<Use> &getValues() const { return Operands; }
240
241   // isNullValue - Return true if this is the value that would be returned by
242   // getNullConstant.
243   virtual bool isNullValue() const { return false; }
244
245   // Methods for support type inquiry through isa, cast, and dyn_cast:
246   static inline bool classof(const ConstantArray *) { return true; }
247   static bool classof(const Constant *CPV);  // defined in CPV.cpp
248   static inline bool classof(const Value *V) {
249     return isa<Constant>(V) && classof(cast<Constant>(V));
250   }
251 };
252
253
254 //===---------------------------------------------------------------------------
255 // ConstantStruct - Constant Struct Declarations
256 //
257 class ConstantStruct : public Constant {
258   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
259 protected:
260   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
261   ~ConstantStruct() {}
262
263   virtual void destroyConstant();
264 public:
265   static ConstantStruct *get(const StructType *T,
266                              const std::vector<Constant*> &V);
267
268   virtual std::string getStrValue() const;
269   inline const StructType *getType() const {
270     return (StructType*)Value::getType();
271   }
272
273   inline const std::vector<Use> &getValues() const { return Operands; }
274
275   // isNullValue - Return true if this is the value that would be returned by
276   // getNullConstant.
277   virtual bool isNullValue() const { return false; }
278
279   // Methods for support type inquiry through isa, cast, and dyn_cast:
280   static inline bool classof(const ConstantStruct *) { return true; }
281   static bool classof(const Constant *CPV);  // defined in CPV.cpp
282   static inline bool classof(const Value *V) {
283     return isa<Constant>(V) && classof(cast<Constant>(V));
284   }
285 };
286
287 //===---------------------------------------------------------------------------
288 // ConstantPointer - Constant Pointer Declarations
289 //
290 // The ConstantPointer class represents a null pointer of a specific type. For
291 // a more specific/useful instance, a subclass of ConstantPointer should be
292 // used.
293 //
294 class ConstantPointer : public Constant {
295   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
296 protected:
297   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
298   ~ConstantPointer() {}
299 public:
300   virtual std::string getStrValue() const = 0;
301   inline const PointerType *getType() const {
302     return (PointerType*)Value::getType();
303   }
304
305   // isNullValue - Return true if this is the value that would be returned by
306   // getNullConstant.
307   virtual bool isNullValue() const { return false; }
308
309   // Methods for support type inquiry through isa, cast, and dyn_cast:
310   static inline bool classof(const ConstantPointer *) { return true; }
311   static bool classof(const Constant *CPV);  // defined in CPV.cpp
312   static inline bool classof(const Value *V) {
313     return isa<Constant>(V) && classof(cast<Constant>(V));
314   }
315 };
316
317 // ConstantPointerNull - a constant pointer value that points to null
318 //
319 class ConstantPointerNull : public ConstantPointer {
320   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
321 protected:
322   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
323   inline ~ConstantPointerNull() {}
324 public:
325   virtual std::string getStrValue() const;
326
327   static ConstantPointerNull *get(const PointerType *T);
328
329   // isNullValue - Return true if this is the value that would be returned by
330   // getNullConstant.
331   virtual bool isNullValue() const { return true; }
332
333   // Methods for support type inquiry through isa, cast, and dyn_cast:
334   static inline bool classof(const ConstantPointerNull *) { return true; }
335   static inline bool classof(const ConstantPointer *P) {
336     return P->getNumOperands() == 0;
337   }
338   static inline bool classof(const Constant *CPV) {
339     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
340   }
341   static inline bool classof(const Value *V) {
342     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
343   }
344 };
345
346
347 // ConstantPointerRef - a constant pointer value that is initialized to
348 // point to a global value, which lies at a constant, fixed address.
349 //
350 class ConstantPointerRef : public ConstantPointer {
351   friend class Module;   // Modules maintain these references
352   ConstantPointerRef(const ConstantPointerRef &); // DNI!
353
354 protected:
355   ConstantPointerRef(GlobalValue *GV);
356   ~ConstantPointerRef() {}
357
358   virtual void destroyConstant() { destroyConstantImpl(); }
359 public:
360   static ConstantPointerRef *get(GlobalValue *GV);
361
362   virtual std::string getStrValue() const;
363
364   const GlobalValue *getValue() const { 
365     return cast<GlobalValue>(Operands[0].get());
366   }
367   GlobalValue *getValue() {
368     return cast<GlobalValue>(Operands[0].get());
369   }
370
371   // Methods for support type inquiry through isa, cast, and dyn_cast:
372   static inline bool classof(const ConstantPointerRef *) { return true; }
373   static inline bool classof(const ConstantPointer *CPV) {
374     return CPV->getNumOperands() == 1;
375   }
376   static inline bool classof(const Constant *CPV) {
377     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
378   }
379   static inline bool classof(const Value *V) {
380     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
381   }
382
383   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
384   // NOT USE THIS!!
385   void mutateReference(GlobalValue *NewGV);
386   // END WARNING!!
387 };
388
389
390
391 #endif