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