remove Type::getVAArgsPromotedType, which is dead, and tidy up a bit.
[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/ADT/ArrayRef.h"
23 #include "llvm/Support/DataTypes.h"
24
25 namespace llvm {
26
27 class Value;
28 template<class ValType, class TypeClass> class TypeMap;
29 class FunctionValType;
30 class ArrayValType;
31 class StructValType;
32 class PointerValType;
33 class VectorValType;
34 class IntegerValType;
35 class APInt;
36 class LLVMContext;
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   bool isVarArgs;
148
149   FunctionType(const FunctionType &);                   // Do not implement
150   const FunctionType &operator=(const FunctionType &);  // Do not implement
151   FunctionType(const Type *Result, ArrayRef<const Type*> Params,
152                bool IsVarArgs);
153
154 public:
155   /// FunctionType::get - This static method is the primary way of constructing
156   /// a FunctionType.
157   ///
158   static FunctionType *get(
159     const Type *Result, ///< The result type
160     ArrayRef<const Type*> Params, ///< The types of the parameters
161     bool isVarArg  ///< Whether this is a variable argument length function
162   );
163
164   /// FunctionType::get - Create a FunctionType taking no parameters.
165   ///
166   static FunctionType *get(
167     const Type *Result, ///< The result type
168     bool isVarArg  ///< Whether this is a variable argument length function
169   ) {
170     return get(Result, ArrayRef<const Type *>(), isVarArg);
171   }
172
173   /// isValidReturnType - Return true if the specified type is valid as a return
174   /// type.
175   static bool isValidReturnType(const Type *RetTy);
176
177   /// isValidArgumentType - Return true if the specified type is valid as an
178   /// argument type.
179   static bool isValidArgumentType(const Type *ArgTy);
180
181   inline bool isVarArg() const { return isVarArgs; }
182   inline const Type *getReturnType() const { return ContainedTys[0]; }
183
184   typedef Type::subtype_iterator param_iterator;
185   param_iterator param_begin() const { return ContainedTys + 1; }
186   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
187
188   // Parameter type accessors...
189   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
190
191   /// getNumParams - Return the number of fixed parameters this function type
192   /// requires.  This does not consider varargs.
193   ///
194   unsigned getNumParams() const { return NumContainedTys - 1; }
195
196   // Implement the AbstractTypeUser interface.
197   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
198   virtual void typeBecameConcrete(const DerivedType *AbsTy);
199
200   // Methods for support type inquiry through isa, cast, and dyn_cast:
201   static inline bool classof(const FunctionType *) { return true; }
202   static inline bool classof(const Type *T) {
203     return T->getTypeID() == FunctionTyID;
204   }
205 };
206
207
208 /// CompositeType - Common super class of ArrayType, StructType, PointerType
209 /// and VectorType
210 class CompositeType : public DerivedType {
211 protected:
212   inline explicit CompositeType(LLVMContext &C, TypeID id) :
213     DerivedType(C, id) { }
214 public:
215
216   /// getTypeAtIndex - Given an index value into the type, return the type of
217   /// the element.
218   ///
219   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
220   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
221   virtual bool indexValid(const Value *V) const = 0;
222   virtual bool indexValid(unsigned Idx) const = 0;
223
224   // Methods for support type inquiry through isa, cast, and dyn_cast:
225   static inline bool classof(const CompositeType *) { return true; }
226   static inline bool classof(const Type *T) {
227     return T->getTypeID() == ArrayTyID ||
228            T->getTypeID() == StructTyID ||
229            T->getTypeID() == PointerTyID ||
230            T->getTypeID() == VectorTyID;
231   }
232 };
233
234
235 /// StructType - Class to represent struct types
236 ///
237 class StructType : public CompositeType {
238   friend class TypeMap<StructValType, StructType>;
239   StructType(const StructType &);                   // Do not implement
240   const StructType &operator=(const StructType &);  // Do not implement
241   StructType(LLVMContext &C, ArrayRef<const Type*> Types, bool isPacked);
242 public:
243   /// StructType::get - This static method is the primary way to create a
244   /// StructType.
245   ///
246   static StructType *get(LLVMContext &Context, 
247                          ArrayRef<const Type*> Params,
248                          bool isPacked=false);
249
250   /// StructType::get - Create an empty structure type.
251   ///
252   static StructType *get(LLVMContext &Context, bool isPacked=false) {
253     return get(Context, llvm::ArrayRef<const Type*>(), isPacked);
254   }
255
256   /// StructType::get - This static method is a convenience method for
257   /// creating structure types by specifying the elements as arguments.
258   /// Note that this method always returns a non-packed struct.  To get
259   /// an empty struct, pass NULL, NULL.
260   static StructType *get(LLVMContext &Context, 
261                          const Type *type, ...) END_WITH_NULL;
262
263   /// isValidElementType - Return true if the specified type is valid as a
264   /// element type.
265   static bool isValidElementType(const Type *ElemTy);
266
267   // Iterator access to the elements
268   typedef Type::subtype_iterator element_iterator;
269   element_iterator element_begin() const { return ContainedTys; }
270   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
271
272   // Random access to the elements
273   unsigned getNumElements() const { return NumContainedTys; }
274   const Type *getElementType(unsigned N) const {
275     assert(N < NumContainedTys && "Element number out of range!");
276     return ContainedTys[N];
277   }
278
279   /// getTypeAtIndex - Given an index value into the type, return the type of
280   /// the element.  For a structure type, this must be a constant value...
281   ///
282   virtual const Type *getTypeAtIndex(const Value *V) const;
283   virtual const Type *getTypeAtIndex(unsigned Idx) const;
284   virtual bool indexValid(const Value *V) const;
285   virtual bool indexValid(unsigned Idx) const;
286
287   // Implement the AbstractTypeUser interface.
288   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
289   virtual void typeBecameConcrete(const DerivedType *AbsTy);
290
291   // Methods for support type inquiry through isa, cast, and dyn_cast:
292   static inline bool classof(const StructType *) { return true; }
293   static inline bool classof(const Type *T) {
294     return T->getTypeID() == StructTyID;
295   }
296
297   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
298 };
299
300 /// SequentialType - This is the superclass of the array, pointer and vector
301 /// type classes.  All of these represent "arrays" in memory.  The array type
302 /// represents a specifically sized array, pointer types are unsized/unknown
303 /// size arrays, vector types represent specifically sized arrays that
304 /// allow for use of SIMD instructions.  SequentialType holds the common
305 /// features of all, which stem from the fact that all three lay their
306 /// components out in memory identically.
307 ///
308 class SequentialType : public CompositeType {
309   PATypeHandle ContainedType; ///< Storage for the single contained type
310   SequentialType(const SequentialType &);                  // Do not implement!
311   const SequentialType &operator=(const SequentialType &); // Do not implement!
312
313   // avoiding warning: 'this' : used in base member initializer list
314   SequentialType* this_() { return this; }
315 protected:
316   SequentialType(TypeID TID, const Type *ElType)
317     : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
318     ContainedTys = &ContainedType;
319     NumContainedTys = 1;
320   }
321
322 public:
323   inline const Type *getElementType() const { return ContainedTys[0]; }
324
325   virtual bool indexValid(const Value *V) const;
326   virtual bool indexValid(unsigned) const {
327     return true;
328   }
329
330   /// getTypeAtIndex - Given an index value into the type, return the type of
331   /// the element.  For sequential types, there is only one subtype...
332   ///
333   virtual const Type *getTypeAtIndex(const Value *) const {
334     return ContainedTys[0];
335   }
336   virtual const Type *getTypeAtIndex(unsigned) const {
337     return ContainedTys[0];
338   }
339
340   // Methods for support type inquiry through isa, cast, and dyn_cast:
341   static inline bool classof(const SequentialType *) { return true; }
342   static inline bool classof(const Type *T) {
343     return T->getTypeID() == ArrayTyID ||
344            T->getTypeID() == PointerTyID ||
345            T->getTypeID() == VectorTyID;
346   }
347 };
348
349
350 /// ArrayType - Class to represent array types
351 ///
352 class ArrayType : public SequentialType {
353   friend class TypeMap<ArrayValType, ArrayType>;
354   uint64_t NumElements;
355
356   ArrayType(const ArrayType &);                   // Do not implement
357   const ArrayType &operator=(const ArrayType &);  // Do not implement
358   ArrayType(const Type *ElType, uint64_t NumEl);
359 public:
360   /// ArrayType::get - This static method is the primary way to construct an
361   /// ArrayType
362   ///
363   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
364
365   /// isValidElementType - Return true if the specified type is valid as a
366   /// element type.
367   static bool isValidElementType(const Type *ElemTy);
368
369   inline uint64_t getNumElements() const { return NumElements; }
370
371   // Implement the AbstractTypeUser interface.
372   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
373   virtual void typeBecameConcrete(const DerivedType *AbsTy);
374
375   // Methods for support type inquiry through isa, cast, and dyn_cast:
376   static inline bool classof(const ArrayType *) { return true; }
377   static inline bool classof(const Type *T) {
378     return T->getTypeID() == ArrayTyID;
379   }
380 };
381
382 /// VectorType - Class to represent vector types
383 ///
384 class VectorType : public SequentialType {
385   friend class TypeMap<VectorValType, VectorType>;
386   unsigned NumElements;
387
388   VectorType(const VectorType &);                   // Do not implement
389   const VectorType &operator=(const VectorType &);  // Do not implement
390   VectorType(const Type *ElType, unsigned NumEl);
391 public:
392   /// VectorType::get - This static method is the primary way to construct an
393   /// VectorType
394   ///
395   static VectorType *get(const Type *ElementType, unsigned NumElements);
396
397   /// VectorType::getInteger - This static method gets a VectorType with the
398   /// same number of elements as the input type, and the element type is an
399   /// integer type of the same width as the input element type.
400   ///
401   static VectorType *getInteger(const VectorType *VTy) {
402     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
403     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
404     return VectorType::get(EltTy, VTy->getNumElements());
405   }
406
407   /// VectorType::getExtendedElementVectorType - This static method is like
408   /// getInteger except that the element types are twice as wide as the
409   /// elements in the input type.
410   ///
411   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
412     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
413     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
414     return VectorType::get(EltTy, VTy->getNumElements());
415   }
416
417   /// VectorType::getTruncatedElementVectorType - This static method is like
418   /// getInteger except that the element types are half as wide as the
419   /// elements in the input type.
420   ///
421   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
422     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
423     assert((EltBits & 1) == 0 &&
424            "Cannot truncate vector element with odd bit-width");
425     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
426     return VectorType::get(EltTy, VTy->getNumElements());
427   }
428
429   /// isValidElementType - Return true if the specified type is valid as a
430   /// element type.
431   static bool isValidElementType(const Type *ElemTy);
432
433   /// @brief Return the number of elements in the Vector type.
434   inline unsigned getNumElements() const { return NumElements; }
435
436   /// @brief Return the number of bits in the Vector type.
437   inline unsigned getBitWidth() const {
438     return NumElements * getElementType()->getPrimitiveSizeInBits();
439   }
440
441   // Implement the AbstractTypeUser interface.
442   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
443   virtual void typeBecameConcrete(const DerivedType *AbsTy);
444
445   // Methods for support type inquiry through isa, cast, and dyn_cast:
446   static inline bool classof(const VectorType *) { return true; }
447   static inline bool classof(const Type *T) {
448     return T->getTypeID() == VectorTyID;
449   }
450 };
451
452
453 /// PointerType - Class to represent pointers
454 ///
455 class PointerType : public SequentialType {
456   friend class TypeMap<PointerValType, PointerType>;
457   unsigned AddressSpace;
458
459   PointerType(const PointerType &);                   // Do not implement
460   const PointerType &operator=(const PointerType &);  // Do not implement
461   explicit PointerType(const Type *ElType, unsigned AddrSpace);
462 public:
463   /// PointerType::get - This constructs a pointer to an object of the specified
464   /// type in a numbered address space.
465   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
466
467   /// PointerType::getUnqual - This constructs a pointer to an object of the
468   /// specified type in the generic address space (address space zero).
469   static PointerType *getUnqual(const Type *ElementType) {
470     return PointerType::get(ElementType, 0);
471   }
472
473   /// isValidElementType - Return true if the specified type is valid as a
474   /// element type.
475   static bool isValidElementType(const Type *ElemTy);
476
477   /// @brief Return the address space of the Pointer type.
478   inline unsigned getAddressSpace() const { return AddressSpace; }
479
480   // Implement the AbstractTypeUser interface.
481   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
482   virtual void typeBecameConcrete(const DerivedType *AbsTy);
483
484   // Implement support type inquiry through isa, cast, and dyn_cast:
485   static inline bool classof(const PointerType *) { return true; }
486   static inline bool classof(const Type *T) {
487     return T->getTypeID() == PointerTyID;
488   }
489 };
490
491
492 /// OpaqueType - Class to represent abstract types
493 ///
494 class OpaqueType : public DerivedType {
495   friend class LLVMContextImpl;
496   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
497   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
498   OpaqueType(LLVMContext &C);
499 public:
500   /// OpaqueType::get - Static factory method for the OpaqueType class...
501   ///
502   static OpaqueType *get(LLVMContext &C);
503
504   // Implement support for type inquiry through isa, cast, and dyn_cast:
505   static inline bool classof(const OpaqueType *) { return true; }
506   static inline bool classof(const Type *T) {
507     return T->getTypeID() == OpaqueTyID;
508   }
509 };
510
511 } // End llvm namespace
512
513 #endif