remove unions from LLVM IR. They are severely buggy and not
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations of classes that represent "derived
11 // types".  These are things like "arrays of x" or "structure of x, y, z" or
12 // "method returning x taking (y,z) as parameters", etc...
13 //
14 // The implementations of these classes live in the Type.cpp file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
20
21 #include "llvm/Type.h"
22
23 namespace llvm {
24
25 class Value;
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
28 class ArrayValType;
29 class StructValType;
30 class PointerValType;
31 class VectorValType;
32 class IntegerValType;
33 class APInt;
34 class LLVMContext;
35
36 class DerivedType : public Type {
37   friend class Type;
38
39 protected:
40   explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
41
42   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
43   /// that the current type has transitioned from being abstract to being
44   /// concrete.
45   ///
46   void notifyUsesThatTypeBecameConcrete();
47
48   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
49   /// another (more concrete) type, we must eliminate all references to other
50   /// types, to avoid some circular reference problems.
51   ///
52   void dropAllTypeUses();
53
54 public:
55
56   //===--------------------------------------------------------------------===//
57   // Abstract Type handling methods - These types have special lifetimes, which
58   // are managed by (add|remove)AbstractTypeUser. See comments in
59   // AbstractTypeUser.h for more information.
60
61   /// refineAbstractTypeTo - This function is used to when it is discovered that
62   /// the 'this' abstract type is actually equivalent to the NewType specified.
63   /// This causes all users of 'this' to switch to reference the more concrete
64   /// type NewType and for 'this' to be deleted.
65   ///
66   void refineAbstractTypeTo(const Type *NewType);
67
68   void dump() const { Type::dump(); }
69
70   // Methods for support type inquiry through isa, cast, and dyn_cast:
71   static inline bool classof(const DerivedType *) { return true; }
72   static inline bool classof(const Type *T) {
73     return T->isDerivedType();
74   }
75 };
76
77 /// Class to represent integer types. Note that this class is also used to
78 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
79 /// Int64Ty.
80 /// @brief Integer representation type
81 class IntegerType : public DerivedType {
82   friend class LLVMContextImpl;
83   
84 protected:
85   explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
86       DerivedType(C, IntegerTyID) {
87     setSubclassData(NumBits);
88   }
89   friend class TypeMap<IntegerValType, IntegerType>;
90 public:
91   /// This enum is just used to hold constants we need for IntegerType.
92   enum {
93     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
94     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
95       ///< Note that bit width is stored in the Type classes SubclassData field
96       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
97   };
98
99   /// This static method is the primary way of constructing an IntegerType.
100   /// If an IntegerType with the same NumBits value was previously instantiated,
101   /// that instance will be returned. Otherwise a new one will be created. Only
102   /// one instance with a given NumBits value is ever created.
103   /// @brief Get or create an IntegerType instance.
104   static const IntegerType* get(LLVMContext &C, unsigned NumBits);
105
106   /// @brief Get the number of bits in this IntegerType
107   unsigned getBitWidth() const { return getSubclassData(); }
108
109   /// getBitMask - Return a bitmask with ones set for all of the bits
110   /// that can be set by an unsigned version of this type.  This is 0xFF for
111   /// i8, 0xFFFF for i16, etc.
112   uint64_t getBitMask() const {
113     return ~uint64_t(0UL) >> (64-getBitWidth());
114   }
115
116   /// getSignBit - Return a uint64_t with just the most significant bit set (the
117   /// sign bit, if the value is treated as a signed number).
118   uint64_t getSignBit() const {
119     return 1ULL << (getBitWidth()-1);
120   }
121
122   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
123   /// @returns a bit mask with ones set for all the bits of this type.
124   /// @brief Get a bit mask for this type.
125   APInt getMask() const;
126
127   /// This method determines if the width of this IntegerType is a power-of-2
128   /// in terms of 8 bit bytes.
129   /// @returns true if this is a power-of-2 byte width.
130   /// @brief Is this a power-of-2 byte-width IntegerType ?
131   bool isPowerOf2ByteWidth() const;
132
133   // Methods for support type inquiry through isa, cast, and dyn_cast:
134   static inline bool classof(const IntegerType *) { return true; }
135   static inline bool classof(const Type *T) {
136     return T->getTypeID() == IntegerTyID;
137   }
138 };
139
140
141 /// FunctionType - Class to represent function types
142 ///
143 class FunctionType : public DerivedType {
144   friend class TypeMap<FunctionValType, FunctionType>;
145   bool isVarArgs;
146
147   FunctionType(const FunctionType &);                   // Do not implement
148   const FunctionType &operator=(const FunctionType &);  // Do not implement
149   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
150                bool IsVarArgs);
151
152 public:
153   /// FunctionType::get - This static method is the primary way of constructing
154   /// a FunctionType.
155   ///
156   static FunctionType *get(
157     const Type *Result, ///< The result type
158     const std::vector<const Type*> &Params, ///< The types of the parameters
159     bool isVarArg  ///< Whether this is a variable argument length function
160   );
161
162   /// FunctionType::get - Create a FunctionType taking no parameters.
163   ///
164   static FunctionType *get(
165     const Type *Result, ///< The result type
166     bool isVarArg  ///< Whether this is a variable argument length function
167   ) {
168     return get(Result, std::vector<const Type *>(), isVarArg);
169   }
170
171   /// isValidReturnType - Return true if the specified type is valid as a return
172   /// type.
173   static bool isValidReturnType(const Type *RetTy);
174
175   /// isValidArgumentType - Return true if the specified type is valid as an
176   /// argument type.
177   static bool isValidArgumentType(const Type *ArgTy);
178
179   inline bool isVarArg() const { return isVarArgs; }
180   inline const Type *getReturnType() const { return ContainedTys[0]; }
181
182   typedef Type::subtype_iterator param_iterator;
183   param_iterator param_begin() const { return ContainedTys + 1; }
184   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
185
186   // Parameter type accessors...
187   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
188
189   /// getNumParams - Return the number of fixed parameters this function type
190   /// requires.  This does not consider varargs.
191   ///
192   unsigned getNumParams() const { return NumContainedTys - 1; }
193
194   // Implement the AbstractTypeUser interface.
195   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
196   virtual void typeBecameConcrete(const DerivedType *AbsTy);
197
198   // Methods for support type inquiry through isa, cast, and dyn_cast:
199   static inline bool classof(const FunctionType *) { return true; }
200   static inline bool classof(const Type *T) {
201     return T->getTypeID() == FunctionTyID;
202   }
203 };
204
205
206 /// CompositeType - Common super class of ArrayType, StructType, PointerType
207 /// and VectorType
208 class CompositeType : public DerivedType {
209 protected:
210   inline explicit CompositeType(LLVMContext &C, TypeID id) :
211     DerivedType(C, id) { }
212 public:
213
214   /// getTypeAtIndex - Given an index value into the type, return the type of
215   /// the element.
216   ///
217   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
218   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
219   virtual bool indexValid(const Value *V) const = 0;
220   virtual bool indexValid(unsigned Idx) const = 0;
221
222   // Methods for support type inquiry through isa, cast, and dyn_cast:
223   static inline bool classof(const CompositeType *) { return true; }
224   static inline bool classof(const Type *T) {
225     return T->getTypeID() == ArrayTyID ||
226            T->getTypeID() == StructTyID ||
227            T->getTypeID() == PointerTyID ||
228            T->getTypeID() == VectorTyID;
229   }
230 };
231
232
233 /// StructType - Class to represent struct types
234 ///
235 class StructType : public CompositeType {
236   friend class TypeMap<StructValType, StructType>;
237   StructType(const StructType &);                   // Do not implement
238   const StructType &operator=(const StructType &);  // Do not implement
239   StructType(LLVMContext &C,
240              const std::vector<const Type*> &Types, bool isPacked);
241 public:
242   /// StructType::get - This static method is the primary way to create a
243   /// StructType.
244   ///
245   static StructType *get(LLVMContext &Context, 
246                          const std::vector<const Type*> &Params,
247                          bool isPacked=false);
248
249   /// StructType::get - Create an empty structure type.
250   ///
251   static StructType *get(LLVMContext &Context, bool isPacked=false) {
252     return get(Context, std::vector<const Type*>(), isPacked);
253   }
254
255   /// StructType::get - This static method is a convenience method for
256   /// creating structure types by specifying the elements as arguments.
257   /// Note that this method always returns a non-packed struct.  To get
258   /// an empty struct, pass NULL, NULL.
259   static StructType *get(LLVMContext &Context, 
260                          const Type *type, ...) END_WITH_NULL;
261
262   /// isValidElementType - Return true if the specified type is valid as a
263   /// element type.
264   static bool isValidElementType(const Type *ElemTy);
265
266   // Iterator access to the elements
267   typedef Type::subtype_iterator element_iterator;
268   element_iterator element_begin() const { return ContainedTys; }
269   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
270
271   // Random access to the elements
272   unsigned getNumElements() const { return NumContainedTys; }
273   const Type *getElementType(unsigned N) const {
274     assert(N < NumContainedTys && "Element number out of range!");
275     return ContainedTys[N];
276   }
277
278   /// getTypeAtIndex - Given an index value into the type, return the type of
279   /// the element.  For a structure type, this must be a constant value...
280   ///
281   virtual const Type *getTypeAtIndex(const Value *V) const;
282   virtual const Type *getTypeAtIndex(unsigned Idx) const;
283   virtual bool indexValid(const Value *V) const;
284   virtual bool indexValid(unsigned Idx) const;
285
286   // Implement the AbstractTypeUser interface.
287   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
288   virtual void typeBecameConcrete(const DerivedType *AbsTy);
289
290   // Methods for support type inquiry through isa, cast, and dyn_cast:
291   static inline bool classof(const StructType *) { return true; }
292   static inline bool classof(const Type *T) {
293     return T->getTypeID() == StructTyID;
294   }
295
296   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
297 };
298
299 /// SequentialType - This is the superclass of the array, pointer and vector
300 /// type classes.  All of these represent "arrays" in memory.  The array type
301 /// represents a specifically sized array, pointer types are unsized/unknown
302 /// size arrays, vector types represent specifically sized arrays that
303 /// allow for use of SIMD instructions.  SequentialType holds the common
304 /// features of all, which stem from the fact that all three lay their
305 /// components out in memory identically.
306 ///
307 class SequentialType : public CompositeType {
308   PATypeHandle ContainedType; ///< Storage for the single contained type
309   SequentialType(const SequentialType &);                  // Do not implement!
310   const SequentialType &operator=(const SequentialType &); // Do not implement!
311
312   // avoiding warning: 'this' : used in base member initializer list
313   SequentialType* this_() { return this; }
314 protected:
315   SequentialType(TypeID TID, const Type *ElType)
316     : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
317     ContainedTys = &ContainedType;
318     NumContainedTys = 1;
319   }
320
321 public:
322   inline const Type *getElementType() const { return ContainedTys[0]; }
323
324   virtual bool indexValid(const Value *V) const;
325   virtual bool indexValid(unsigned) const {
326     return true;
327   }
328
329   /// getTypeAtIndex - Given an index value into the type, return the type of
330   /// the element.  For sequential types, there is only one subtype...
331   ///
332   virtual const Type *getTypeAtIndex(const Value *) const {
333     return ContainedTys[0];
334   }
335   virtual const Type *getTypeAtIndex(unsigned) const {
336     return ContainedTys[0];
337   }
338
339   // Methods for support type inquiry through isa, cast, and dyn_cast:
340   static inline bool classof(const SequentialType *) { return true; }
341   static inline bool classof(const Type *T) {
342     return T->getTypeID() == ArrayTyID ||
343            T->getTypeID() == PointerTyID ||
344            T->getTypeID() == VectorTyID;
345   }
346 };
347
348
349 /// ArrayType - Class to represent array types
350 ///
351 class ArrayType : public SequentialType {
352   friend class TypeMap<ArrayValType, ArrayType>;
353   uint64_t NumElements;
354
355   ArrayType(const ArrayType &);                   // Do not implement
356   const ArrayType &operator=(const ArrayType &);  // Do not implement
357   ArrayType(const Type *ElType, uint64_t NumEl);
358 public:
359   /// ArrayType::get - This static method is the primary way to construct an
360   /// ArrayType
361   ///
362   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
363
364   /// isValidElementType - Return true if the specified type is valid as a
365   /// element type.
366   static bool isValidElementType(const Type *ElemTy);
367
368   inline uint64_t getNumElements() const { return NumElements; }
369
370   // Implement the AbstractTypeUser interface.
371   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
372   virtual void typeBecameConcrete(const DerivedType *AbsTy);
373
374   // Methods for support type inquiry through isa, cast, and dyn_cast:
375   static inline bool classof(const ArrayType *) { return true; }
376   static inline bool classof(const Type *T) {
377     return T->getTypeID() == ArrayTyID;
378   }
379 };
380
381 /// VectorType - Class to represent vector types
382 ///
383 class VectorType : public SequentialType {
384   friend class TypeMap<VectorValType, VectorType>;
385   unsigned NumElements;
386
387   VectorType(const VectorType &);                   // Do not implement
388   const VectorType &operator=(const VectorType &);  // Do not implement
389   VectorType(const Type *ElType, unsigned NumEl);
390 public:
391   /// VectorType::get - This static method is the primary way to construct an
392   /// VectorType
393   ///
394   static VectorType *get(const Type *ElementType, unsigned NumElements);
395
396   /// VectorType::getInteger - This static method gets a VectorType with the
397   /// same number of elements as the input type, and the element type is an
398   /// integer type of the same width as the input element type.
399   ///
400   static VectorType *getInteger(const VectorType *VTy) {
401     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
402     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
403     return VectorType::get(EltTy, VTy->getNumElements());
404   }
405
406   /// VectorType::getExtendedElementVectorType - This static method is like
407   /// getInteger except that the element types are twice as wide as the
408   /// elements in the input type.
409   ///
410   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
411     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
412     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
413     return VectorType::get(EltTy, VTy->getNumElements());
414   }
415
416   /// VectorType::getTruncatedElementVectorType - This static method is like
417   /// getInteger except that the element types are half as wide as the
418   /// elements in the input type.
419   ///
420   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
421     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
422     assert((EltBits & 1) == 0 &&
423            "Cannot truncate vector element with odd bit-width");
424     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
425     return VectorType::get(EltTy, VTy->getNumElements());
426   }
427
428   /// isValidElementType - Return true if the specified type is valid as a
429   /// element type.
430   static bool isValidElementType(const Type *ElemTy);
431
432   /// @brief Return the number of elements in the Vector type.
433   inline unsigned getNumElements() const { return NumElements; }
434
435   /// @brief Return the number of bits in the Vector type.
436   inline unsigned getBitWidth() const {
437     return NumElements * getElementType()->getPrimitiveSizeInBits();
438   }
439
440   // Implement the AbstractTypeUser interface.
441   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
442   virtual void typeBecameConcrete(const DerivedType *AbsTy);
443
444   // Methods for support type inquiry through isa, cast, and dyn_cast:
445   static inline bool classof(const VectorType *) { return true; }
446   static inline bool classof(const Type *T) {
447     return T->getTypeID() == VectorTyID;
448   }
449 };
450
451
452 /// PointerType - Class to represent pointers
453 ///
454 class PointerType : public SequentialType {
455   friend class TypeMap<PointerValType, PointerType>;
456   unsigned AddressSpace;
457
458   PointerType(const PointerType &);                   // Do not implement
459   const PointerType &operator=(const PointerType &);  // Do not implement
460   explicit PointerType(const Type *ElType, unsigned AddrSpace);
461 public:
462   /// PointerType::get - This constructs a pointer to an object of the specified
463   /// type in a numbered address space.
464   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
465
466   /// PointerType::getUnqual - This constructs a pointer to an object of the
467   /// specified type in the generic address space (address space zero).
468   static PointerType *getUnqual(const Type *ElementType) {
469     return PointerType::get(ElementType, 0);
470   }
471
472   /// isValidElementType - Return true if the specified type is valid as a
473   /// element type.
474   static bool isValidElementType(const Type *ElemTy);
475
476   /// @brief Return the address space of the Pointer type.
477   inline unsigned getAddressSpace() const { return AddressSpace; }
478
479   // Implement the AbstractTypeUser interface.
480   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
481   virtual void typeBecameConcrete(const DerivedType *AbsTy);
482
483   // Implement support type inquiry through isa, cast, and dyn_cast:
484   static inline bool classof(const PointerType *) { return true; }
485   static inline bool classof(const Type *T) {
486     return T->getTypeID() == PointerTyID;
487   }
488 };
489
490
491 /// OpaqueType - Class to represent abstract types
492 ///
493 class OpaqueType : public DerivedType {
494   friend class LLVMContextImpl;
495   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
496   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
497   OpaqueType(LLVMContext &C);
498 public:
499   /// OpaqueType::get - Static factory method for the OpaqueType class...
500   ///
501   static OpaqueType *get(LLVMContext &C);
502
503   // Implement support for type inquiry through isa, cast, and dyn_cast:
504   static inline bool classof(const OpaqueType *) { return true; }
505   static inline bool classof(const Type *T) {
506     return T->getTypeID() == OpaqueTyID;
507   }
508 };
509
510 } // End llvm namespace
511
512 #endif