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