Land the long talked about "type system rewrite" patch. This
[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 class APInt;
28 class LLVMContext;
29 template<typename T> class ArrayRef;
30 class StringRef;
31
32 class DerivedType : public Type {
33 protected:
34   explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
35 public:
36
37   // Methods for support type inquiry through isa, cast, and dyn_cast.
38   static inline bool classof(const DerivedType *) { return true; }
39   static inline bool classof(const Type *T) {
40     return T->isDerivedType();
41   }
42 };
43
44 /// Class to represent integer types. Note that this class is also used to
45 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
46 /// Int64Ty.
47 /// @brief Integer representation type
48 class IntegerType : public DerivedType {
49   friend class LLVMContextImpl;
50   
51 protected:
52   explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
53       DerivedType(C, IntegerTyID) {
54     setSubclassData(NumBits);
55   }
56 public:
57   /// This enum is just used to hold constants we need for IntegerType.
58   enum {
59     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
60     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
61       ///< Note that bit width is stored in the Type classes SubclassData field
62       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
63   };
64
65   /// This static method is the primary way of constructing an IntegerType.
66   /// If an IntegerType with the same NumBits value was previously instantiated,
67   /// that instance will be returned. Otherwise a new one will be created. Only
68   /// one instance with a given NumBits value is ever created.
69   /// @brief Get or create an IntegerType instance.
70   static IntegerType *get(LLVMContext &C, unsigned NumBits);
71
72   /// @brief Get the number of bits in this IntegerType
73   unsigned getBitWidth() const { return getSubclassData(); }
74
75   /// getBitMask - Return a bitmask with ones set for all of the bits
76   /// that can be set by an unsigned version of this type.  This is 0xFF for
77   /// i8, 0xFFFF for i16, etc.
78   uint64_t getBitMask() const {
79     return ~uint64_t(0UL) >> (64-getBitWidth());
80   }
81
82   /// getSignBit - Return a uint64_t with just the most significant bit set (the
83   /// sign bit, if the value is treated as a signed number).
84   uint64_t getSignBit() const {
85     return 1ULL << (getBitWidth()-1);
86   }
87
88   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
89   /// @returns a bit mask with ones set for all the bits of this type.
90   /// @brief Get a bit mask for this type.
91   APInt getMask() const;
92
93   /// This method determines if the width of this IntegerType is a power-of-2
94   /// in terms of 8 bit bytes.
95   /// @returns true if this is a power-of-2 byte width.
96   /// @brief Is this a power-of-2 byte-width IntegerType ?
97   bool isPowerOf2ByteWidth() const;
98
99   // Methods for support type inquiry through isa, cast, and dyn_cast.
100   static inline bool classof(const IntegerType *) { return true; }
101   static inline bool classof(const Type *T) {
102     return T->getTypeID() == IntegerTyID;
103   }
104 };
105
106
107 /// FunctionType - Class to represent function types
108 ///
109 class FunctionType : public DerivedType {
110   FunctionType(const FunctionType &);                   // Do not implement
111   const FunctionType &operator=(const FunctionType &);  // Do not implement
112   FunctionType(const Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
113
114 public:
115   /// FunctionType::get - This static method is the primary way of constructing
116   /// a FunctionType.
117   ///
118   static FunctionType *get(const Type *Result,
119                            ArrayRef<const Type*> Params, bool isVarArg);
120   static FunctionType *get(const Type *Result,
121                            ArrayRef<Type*> Params, bool isVarArg);
122
123   /// FunctionType::get - Create a FunctionType taking no parameters.
124   ///
125   static FunctionType *get(const Type *Result, bool isVarArg);
126   
127   /// isValidReturnType - Return true if the specified type is valid as a return
128   /// type.
129   static bool isValidReturnType(const Type *RetTy);
130
131   /// isValidArgumentType - Return true if the specified type is valid as an
132   /// argument type.
133   static bool isValidArgumentType(const Type *ArgTy);
134
135   bool isVarArg() const { return getSubclassData(); }
136   Type *getReturnType() const { return ContainedTys[0]; }
137
138   typedef Type::subtype_iterator param_iterator;
139   param_iterator param_begin() const { return ContainedTys + 1; }
140   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
141
142   // Parameter type accessors.
143   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
144
145   /// getNumParams - Return the number of fixed parameters this function type
146   /// requires.  This does not consider varargs.
147   ///
148   unsigned getNumParams() const { return NumContainedTys - 1; }
149
150   // Methods for support type inquiry through isa, cast, and dyn_cast.
151   static inline bool classof(const FunctionType *) { return true; }
152   static inline bool classof(const Type *T) {
153     return T->getTypeID() == FunctionTyID;
154   }
155 };
156
157
158 /// CompositeType - Common super class of ArrayType, StructType, PointerType
159 /// and VectorType.
160 class CompositeType : public DerivedType {
161 protected:
162   explicit CompositeType(LLVMContext &C, TypeID tid) : DerivedType(C, tid) { }
163 public:
164
165   /// getTypeAtIndex - Given an index value into the type, return the type of
166   /// the element.
167   ///
168   Type *getTypeAtIndex(const Value *V) const;
169   Type *getTypeAtIndex(unsigned Idx) const;
170   bool indexValid(const Value *V) const;
171   bool indexValid(unsigned Idx) const;
172
173   // Methods for support type inquiry through isa, cast, and dyn_cast.
174   static inline bool classof(const CompositeType *) { return true; }
175   static inline bool classof(const Type *T) {
176     return T->getTypeID() == ArrayTyID ||
177            T->getTypeID() == StructTyID ||
178            T->getTypeID() == PointerTyID ||
179            T->getTypeID() == VectorTyID;
180   }
181 };
182
183
184 /// StructType - Class to represent struct types, both normal and packed.
185 /// Besides being optionally packed, structs can be either "anonymous" or may
186 /// have an identity.  Anonymous structs are uniqued by structural equivalence,
187 /// but types are each unique when created, and optionally have a name.
188 ///
189 class StructType : public CompositeType {
190   StructType(const StructType &);                   // Do not implement
191   const StructType &operator=(const StructType &);  // Do not implement
192   StructType(LLVMContext &C)
193     : CompositeType(C, StructTyID), SymbolTableEntry(0) {}
194   enum {
195     // This is the contents of the SubClassData field.
196     SCDB_HasBody = 1,
197     SCDB_Packed = 2,
198     SCDB_IsAnonymous = 4
199   };
200   
201   /// SymbolTableEntry - For a named struct that actually has a name, this is a
202   /// pointer to the symbol table entry (maintained by LLVMContext) for the
203   /// struct.  This is null if the type is an anonymous struct or if it is
204   /// 
205   void *SymbolTableEntry;
206 public:
207   /// StructType::createNamed - This creates a named struct with no body
208   /// specified.  If the name is empty, it creates an unnamed struct, which has
209   /// a unique identity but no actual name.
210   static StructType *createNamed(LLVMContext &Context, StringRef Name);
211   
212   static StructType *createNamed(StringRef Name, ArrayRef<Type*> Elements,
213                                  bool isPacked = false);
214   static StructType *createNamed(LLVMContext &Context, StringRef Name,
215                                  ArrayRef<Type*> Elements,
216                                  bool isPacked = false);
217   static StructType *createNamed(StringRef Name, Type *elt1, ...) END_WITH_NULL;
218
219   /// StructType::get - This static method is the primary way to create a
220   /// StructType.
221   ///
222   /// FIXME: Remove the 'const Type*' version of this when types are pervasively
223   /// de-constified.
224   static StructType *get(LLVMContext &Context, ArrayRef<const Type*> Elements,
225                          bool isPacked = false);
226   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
227                          bool isPacked = false);
228
229   /// StructType::get - Create an empty structure type.
230   ///
231   static StructType *get(LLVMContext &Context, bool isPacked = false);
232   
233   /// StructType::get - This static method is a convenience method for creating
234   /// structure types by specifying the elements as arguments.  Note that this
235   /// method always returns a non-packed struct, and requires at least one
236   /// element type.
237   static StructType *get(const Type *elt1, ...) END_WITH_NULL;
238
239   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
240   
241   /// isAnonymous - Return true if this type is uniqued by structural
242   /// equivalence, false if it has an identity.
243   bool isAnonymous() const {return (getSubclassData() & SCDB_IsAnonymous) != 0;}
244   
245   /// isOpaque - Return true if this is a type with an identity that has no body
246   /// specified yet.  These prints as 'opaque' in .ll files.
247   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
248   
249   /// hasName - Return true if this is a named struct that has a non-empty name.
250   bool hasName() const { return SymbolTableEntry != 0; }
251   
252   /// getName - Return the name for this struct type if it has an identity.
253   /// This may return an empty string for an unnamed struct type.  Do not call
254   /// this on an anonymous type.
255   StringRef getName() const;
256   
257   /// setName - Change the name of this type to the specified name, or to a name
258   /// with a suffix if there is a collision.  Do not call this on an anonymous
259   /// type.
260   void setName(StringRef Name);
261
262   /// setBody - Specify a body for an opaque type.
263   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
264   void setBody(Type *elt1, ...) END_WITH_NULL;
265   
266   /// isValidElementType - Return true if the specified type is valid as a
267   /// element type.
268   static bool isValidElementType(const Type *ElemTy);
269   
270
271   // Iterator access to the elements.
272   typedef Type::subtype_iterator element_iterator;
273   element_iterator element_begin() const { return ContainedTys; }
274   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
275
276   /// isLayoutIdentical - Return true if this is layout identical to the
277   /// specified struct.
278   bool isLayoutIdentical(const StructType *Other) const;  
279   
280   // Random access to the elements
281   unsigned getNumElements() const { return NumContainedTys; }
282   Type *getElementType(unsigned N) const {
283     assert(N < NumContainedTys && "Element number out of range!");
284     return ContainedTys[N];
285   }
286
287   // Methods for support type inquiry through isa, cast, and dyn_cast.
288   static inline bool classof(const StructType *) { return true; }
289   static inline bool classof(const Type *T) {
290     return T->getTypeID() == StructTyID;
291   }
292 };
293
294 /// SequentialType - This is the superclass of the array, pointer and vector
295 /// type classes.  All of these represent "arrays" in memory.  The array type
296 /// represents a specifically sized array, pointer types are unsized/unknown
297 /// size arrays, vector types represent specifically sized arrays that
298 /// allow for use of SIMD instructions.  SequentialType holds the common
299 /// features of all, which stem from the fact that all three lay their
300 /// components out in memory identically.
301 ///
302 class SequentialType : public CompositeType {
303   Type *ContainedType;               ///< Storage for the single contained type.
304   SequentialType(const SequentialType &);                  // Do not implement!
305   const SequentialType &operator=(const SequentialType &); // Do not implement!
306
307 protected:
308   SequentialType(TypeID TID, Type *ElType)
309     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
310     ContainedTys = &ContainedType;
311     NumContainedTys = 1;
312   }
313
314 public:
315   Type *getElementType() const { return ContainedTys[0]; }
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   uint64_t NumElements;
331
332   ArrayType(const ArrayType &);                   // Do not implement
333   const ArrayType &operator=(const ArrayType &);  // Do not implement
334   ArrayType(Type *ElType, uint64_t NumEl);
335 public:
336   /// ArrayType::get - This static method is the primary way to construct an
337   /// ArrayType
338   ///
339   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
340
341   /// isValidElementType - Return true if the specified type is valid as a
342   /// element type.
343   static bool isValidElementType(const Type *ElemTy);
344
345   uint64_t getNumElements() const { return NumElements; }
346
347   // Methods for support type inquiry through isa, cast, and dyn_cast.
348   static inline bool classof(const ArrayType *) { return true; }
349   static inline bool classof(const Type *T) {
350     return T->getTypeID() == ArrayTyID;
351   }
352 };
353
354 /// VectorType - Class to represent vector types.
355 ///
356 class VectorType : public SequentialType {
357   unsigned NumElements;
358
359   VectorType(const VectorType &);                   // Do not implement
360   const VectorType &operator=(const VectorType &);  // Do not implement
361   VectorType(Type *ElType, unsigned NumEl);
362 public:
363   /// VectorType::get - This static method is the primary way to construct an
364   /// VectorType.
365   ///
366   static VectorType *get(const Type *ElementType, unsigned NumElements);
367
368   /// VectorType::getInteger - This static method gets a VectorType with the
369   /// same number of elements as the input type, and the element type is an
370   /// integer type of the same width as the input element type.
371   ///
372   static VectorType *getInteger(const VectorType *VTy) {
373     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
374     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
375     return VectorType::get(EltTy, VTy->getNumElements());
376   }
377
378   /// VectorType::getExtendedElementVectorType - This static method is like
379   /// getInteger except that the element types are twice as wide as the
380   /// elements in the input type.
381   ///
382   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
383     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
384     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
385     return VectorType::get(EltTy, VTy->getNumElements());
386   }
387
388   /// VectorType::getTruncatedElementVectorType - This static method is like
389   /// getInteger except that the element types are half as wide as the
390   /// elements in the input type.
391   ///
392   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
393     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
394     assert((EltBits & 1) == 0 &&
395            "Cannot truncate vector element with odd bit-width");
396     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
397     return VectorType::get(EltTy, VTy->getNumElements());
398   }
399
400   /// isValidElementType - Return true if the specified type is valid as a
401   /// element type.
402   static bool isValidElementType(const Type *ElemTy);
403
404   /// @brief Return the number of elements in the Vector type.
405   unsigned getNumElements() const { return NumElements; }
406
407   /// @brief Return the number of bits in the Vector type.
408   unsigned getBitWidth() const {
409     return NumElements * getElementType()->getPrimitiveSizeInBits();
410   }
411
412   // Methods for support type inquiry through isa, cast, and dyn_cast.
413   static inline bool classof(const VectorType *) { return true; }
414   static inline bool classof(const Type *T) {
415     return T->getTypeID() == VectorTyID;
416   }
417 };
418
419
420 /// PointerType - Class to represent pointers.
421 ///
422 class PointerType : public SequentialType {
423   PointerType(const PointerType &);                   // Do not implement
424   const PointerType &operator=(const PointerType &);  // Do not implement
425   explicit PointerType(Type *ElType, unsigned AddrSpace);
426 public:
427   /// PointerType::get - This constructs a pointer to an object of the specified
428   /// type in a numbered address space.
429   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
430
431   /// PointerType::getUnqual - This constructs a pointer to an object of the
432   /// specified type in the generic address space (address space zero).
433   static PointerType *getUnqual(const Type *ElementType) {
434     return PointerType::get(ElementType, 0);
435   }
436
437   /// isValidElementType - Return true if the specified type is valid as a
438   /// element type.
439   static bool isValidElementType(const Type *ElemTy);
440
441   /// @brief Return the address space of the Pointer type.
442   inline unsigned getAddressSpace() const { return getSubclassData(); }
443
444   // Implement support type inquiry through isa, cast, and dyn_cast.
445   static inline bool classof(const PointerType *) { return true; }
446   static inline bool classof(const Type *T) {
447     return T->getTypeID() == PointerTyID;
448   }
449 };
450
451 } // End llvm namespace
452
453 #endif