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