Add FunctionType ctor variant that takes SmallVector params.
[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 #include "llvm/ADT/SmallVector.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
36 class DerivedType : public Type {
37   friend class Type;
38
39 protected:
40   explicit DerivedType(TypeID id) : Type(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 *T) { 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 protected:
83   explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
84     setSubclassData(NumBits);
85   }
86   friend class TypeMap<IntegerValType, IntegerType>;
87 public:
88   /// This enum is just used to hold constants we need for IntegerType.
89   enum {
90     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
91     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
92       ///< Note that bit width is stored in the Type classes SubclassData field
93       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
94   };
95
96   /// This static method is the primary way of constructing an IntegerType. 
97   /// If an IntegerType with the same NumBits value was previously instantiated,
98   /// that instance will be returned. Otherwise a new one will be created. Only
99   /// one instance with a given NumBits value is ever created.
100   /// @brief Get or create an IntegerType instance.
101   static const IntegerType* get(unsigned NumBits);
102
103   /// @brief Get the number of bits in this IntegerType
104   unsigned getBitWidth() const { return getSubclassData(); }
105
106   /// getBitMask - Return a bitmask with ones set for all of the bits
107   /// that can be set by an unsigned version of this type.  This is 0xFF for
108   /// sbyte/ubyte, 0xFFFF for shorts, etc.
109   uint64_t getBitMask() const {
110     return ~uint64_t(0UL) >> (64-getBitWidth());
111   }
112
113   /// getSignBit - Return a uint64_t with just the most significant bit set (the
114   /// sign bit, if the value is treated as a signed number).
115   uint64_t getSignBit() const {
116     return 1ULL << (getBitWidth()-1);
117   }
118   
119   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
120   /// @returns a bit mask with ones set for all the bits of this type.
121   /// @brief Get a bit mask for this type.
122   APInt getMask() const;
123
124   /// This method determines if the width of this IntegerType is a power-of-2
125   /// in terms of 8 bit bytes. 
126   /// @returns true if this is a power-of-2 byte width.
127   /// @brief Is this a power-of-2 byte-width IntegerType ?
128   bool isPowerOf2ByteWidth() const;
129
130   // Methods for support type inquiry through isa, cast, and dyn_cast:
131   static inline bool classof(const IntegerType *T) { return true; }
132   static inline bool classof(const Type *T) {
133     return T->getTypeID() == IntegerTyID;
134   }
135 };
136
137
138 /// FunctionType - Class to represent function types
139 ///
140 class FunctionType : public DerivedType {
141   friend class TypeMap<FunctionValType, FunctionType>;
142   bool isVarArgs;
143
144   FunctionType(const FunctionType &);                   // Do not implement
145   const FunctionType &operator=(const FunctionType &);  // Do not implement
146   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
147                bool IsVarArgs);
148   FunctionType(const Type *Result, const SmallVectorImpl<const Type*> &Params,
149                bool IsVarArgs);
150
151 public:
152   /// FunctionType::get - This static method is the primary way of constructing
153   /// a FunctionType. 
154   ///
155   static FunctionType *get(
156     const Type *Result, ///< The result type
157     const std::vector<const Type*> &Params, ///< The types of the parameters
158     bool isVarArg  ///< Whether this is a variable argument length function
159   );
160
161   static FunctionType *get(
162     const Type *Result, ///< The result type
163     const SmallVectorImpl<const Type*> &Params, ///< The types of the parameters
164     bool isVarArg  ///< Whether this is a variable argument length function
165   );
166
167   inline bool isVarArg() const { return isVarArgs; }
168   inline const Type *getReturnType() const { return ContainedTys[0]; }
169
170   typedef Type::subtype_iterator param_iterator;
171   param_iterator param_begin() const { return ContainedTys + 1; }
172   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
173
174   // Parameter type accessors...
175   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
176
177   /// getNumParams - Return the number of fixed parameters this function type
178   /// requires.  This does not consider varargs.
179   ///
180   unsigned getNumParams() const { return NumContainedTys - 1; }
181
182   // Implement the AbstractTypeUser interface.
183   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
184   virtual void typeBecameConcrete(const DerivedType *AbsTy);
185
186   // Methods for support type inquiry through isa, cast, and dyn_cast:
187   static inline bool classof(const FunctionType *T) { return true; }
188   static inline bool classof(const Type *T) {
189     return T->getTypeID() == FunctionTyID;
190   }
191 };
192
193
194 /// CompositeType - Common super class of ArrayType, StructType, PointerType
195 /// and VectorType
196 class CompositeType : public DerivedType {
197 protected:
198   inline explicit CompositeType(TypeID id) : DerivedType(id) { }
199 public:
200
201   /// getTypeAtIndex - Given an index value into the type, return the type of
202   /// the element.
203   ///
204   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
205   virtual bool indexValid(const Value *V) const = 0;
206
207   // Methods for support type inquiry through isa, cast, and dyn_cast:
208   static inline bool classof(const CompositeType *T) { return true; }
209   static inline bool classof(const Type *T) {
210     return T->getTypeID() == ArrayTyID ||
211            T->getTypeID() == StructTyID ||
212            T->getTypeID() == PointerTyID ||
213            T->getTypeID() == VectorTyID;
214   }
215 };
216
217
218 /// StructType - Class to represent struct types
219 ///
220 class StructType : public CompositeType {
221   friend class TypeMap<StructValType, StructType>;
222   StructType(const StructType &);                   // Do not implement
223   const StructType &operator=(const StructType &);  // Do not implement
224   StructType(const std::vector<const Type*> &Types, bool isPacked);
225 public:
226   /// StructType::get - This static method is the primary way to create a
227   /// StructType.
228   ///
229   static StructType *get(const std::vector<const Type*> &Params, 
230                          bool isPacked=false);
231
232   // Iterator access to the elements
233   typedef Type::subtype_iterator element_iterator;
234   element_iterator element_begin() const { return ContainedTys; }
235   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
236
237   // Random access to the elements
238   unsigned getNumElements() const { return NumContainedTys; }
239   const Type *getElementType(unsigned N) const {
240     assert(N < NumContainedTys && "Element number out of range!");
241     return ContainedTys[N];
242   }
243
244   /// getTypeAtIndex - Given an index value into the type, return the type of
245   /// the element.  For a structure type, this must be a constant value...
246   ///
247   virtual const Type *getTypeAtIndex(const Value *V) const ;
248   virtual bool indexValid(const Value *V) const;
249
250   // Implement the AbstractTypeUser interface.
251   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
252   virtual void typeBecameConcrete(const DerivedType *AbsTy);
253
254   // Methods for support type inquiry through isa, cast, and dyn_cast:
255   static inline bool classof(const StructType *T) { return true; }
256   static inline bool classof(const Type *T) {
257     return T->getTypeID() == StructTyID;
258   }
259
260   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
261 };
262
263
264 /// SequentialType - This is the superclass of the array, pointer and vector
265 /// type classes.  All of these represent "arrays" in memory.  The array type
266 /// represents a specifically sized array, pointer types are unsized/unknown
267 /// size arrays, vector types represent specifically sized arrays that
268 /// allow for use of SIMD instructions.  SequentialType holds the common
269 /// features of all, which stem from the fact that all three lay their
270 /// components out in memory identically.
271 ///
272 class SequentialType : public CompositeType {
273   PATypeHandle ContainedType; ///< Storage for the single contained type
274   SequentialType(const SequentialType &);                  // Do not implement!
275   const SequentialType &operator=(const SequentialType &); // Do not implement!
276
277   // avoiding warning: 'this' : used in base member initializer list
278   SequentialType* this_() { return this; }
279 protected:
280   SequentialType(TypeID TID, const Type *ElType) 
281     : CompositeType(TID), ContainedType(ElType, this_()) {
282     ContainedTys = &ContainedType; 
283     NumContainedTys = 1;
284   }
285
286 public:
287   inline const Type *getElementType() const { return ContainedTys[0]; }
288
289   virtual bool indexValid(const Value *V) const;
290
291   /// getTypeAtIndex - Given an index value into the type, return the type of
292   /// the element.  For sequential types, there is only one subtype...
293   ///
294   virtual const Type *getTypeAtIndex(const Value *V) const {
295     return ContainedTys[0];
296   }
297
298   // Methods for support type inquiry through isa, cast, and dyn_cast:
299   static inline bool classof(const SequentialType *T) { return true; }
300   static inline bool classof(const Type *T) {
301     return T->getTypeID() == ArrayTyID ||
302            T->getTypeID() == PointerTyID ||
303            T->getTypeID() == VectorTyID;
304   }
305 };
306
307
308 /// ArrayType - Class to represent array types
309 ///
310 class ArrayType : public SequentialType {
311   friend class TypeMap<ArrayValType, ArrayType>;
312   uint64_t NumElements;
313
314   ArrayType(const ArrayType &);                   // Do not implement
315   const ArrayType &operator=(const ArrayType &);  // Do not implement
316   ArrayType(const Type *ElType, uint64_t NumEl);
317 public:
318   /// ArrayType::get - This static method is the primary way to construct an
319   /// ArrayType
320   ///
321   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
322
323   inline uint64_t getNumElements() const { return NumElements; }
324
325   // Implement the AbstractTypeUser interface.
326   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
327   virtual void typeBecameConcrete(const DerivedType *AbsTy);
328
329   // Methods for support type inquiry through isa, cast, and dyn_cast:
330   static inline bool classof(const ArrayType *T) { return true; }
331   static inline bool classof(const Type *T) {
332     return T->getTypeID() == ArrayTyID;
333   }
334 };
335
336 /// VectorType - Class to represent vector types
337 ///
338 class VectorType : public SequentialType {
339   friend class TypeMap<VectorValType, VectorType>;
340   unsigned NumElements;
341
342   VectorType(const VectorType &);                   // Do not implement
343   const VectorType &operator=(const VectorType &);  // Do not implement
344   VectorType(const Type *ElType, unsigned NumEl);
345 public:
346   /// VectorType::get - This static method is the primary way to construct an
347   /// VectorType
348   ///
349   static VectorType *get(const Type *ElementType, unsigned NumElements);
350
351   /// @brief Return the number of elements in the Vector type.
352   inline unsigned getNumElements() const { return NumElements; }
353
354   /// @brief Return the number of bits in the Vector type.
355   inline unsigned getBitWidth() const { 
356     return NumElements *getElementType()->getPrimitiveSizeInBits();
357   }
358
359   // Implement the AbstractTypeUser interface.
360   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
361   virtual void typeBecameConcrete(const DerivedType *AbsTy);
362
363   // Methods for support type inquiry through isa, cast, and dyn_cast:
364   static inline bool classof(const VectorType *T) { return true; }
365   static inline bool classof(const Type *T) {
366     return T->getTypeID() == VectorTyID;
367   }
368 };
369
370
371 /// PointerType - Class to represent pointers
372 ///
373 class PointerType : public SequentialType {
374   friend class TypeMap<PointerValType, PointerType>;
375   unsigned AddressSpace;
376   
377   PointerType(const PointerType &);                   // Do not implement
378   const PointerType &operator=(const PointerType &);  // Do not implement
379   explicit PointerType(const Type *ElType, unsigned AddrSpace);
380 public:
381   /// PointerType::get - This constructs a pointer to an object of the specified 
382   /// type in a numbered address space.
383   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
384   
385   /// PointerType::getUnqual - This constructs a pointer to an object of the  
386   /// specified type in the generic address space (address space zero).
387   static PointerType *getUnqual(const Type *ElementType) { 
388     return PointerType::get(ElementType, 0);
389   }
390   
391   /// @brief Return the address space of the Pointer type.
392   inline unsigned getAddressSpace() const { return AddressSpace; }
393
394   // Implement the AbstractTypeUser interface.
395   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
396   virtual void typeBecameConcrete(const DerivedType *AbsTy);
397
398   // Implement support type inquiry through isa, cast, and dyn_cast:
399   static inline bool classof(const PointerType *T) { return true; }
400   static inline bool classof(const Type *T) {
401     return T->getTypeID() == PointerTyID;
402   }
403 };
404
405
406 /// OpaqueType - Class to represent abstract types
407 ///
408 class OpaqueType : public DerivedType {
409   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
410   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
411   OpaqueType();
412 public:
413   /// OpaqueType::get - Static factory method for the OpaqueType class...
414   ///
415   static OpaqueType *get() {
416     return new OpaqueType();           // All opaque types are distinct
417   }
418
419   // Implement support for type inquiry through isa, cast, and dyn_cast:
420   static inline bool classof(const OpaqueType *T) { return true; }
421   static inline bool classof(const Type *T) {
422     return T->getTypeID() == OpaqueTyID;
423   }
424 };
425
426 } // End llvm namespace
427
428 #endif