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