Type.h revision af7b4fb9bec3858f0374d713dcbf3398a23c6fbe
1//===-- llvm/Type.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 declaration of the Type class.  For more "Type"
11// stuff, look in DerivedTypes.h.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TYPE_H
16#define LLVM_TYPE_H
17
18#include "llvm/Support/Casting.h"
19#include "llvm/Support/DataTypes.h"
20
21namespace llvm {
22
23class PointerType;
24class IntegerType;
25class raw_ostream;
26class Module;
27class LLVMContext;
28class LLVMContextImpl;
29template<class GraphType> struct GraphTraits;
30
31/// The instances of the Type class are immutable: once they are created,
32/// they are never changed.  Also note that only one instance of a particular
33/// type is ever created.  Thus seeing if two types are equal is a matter of
34/// doing a trivial pointer comparison. To enforce that no two equal instances
35/// are created, Type instances can only be created via static factory methods
36/// in class Type and in derived classes.  Once allocated, Types are never
37/// free'd.
38///
39class Type {
40public:
41  //===--------------------------------------------------------------------===//
42  /// Definitions of all of the base types for the Type system.  Based on this
43  /// value, you can cast to a class defined in DerivedTypes.h.
44  /// Note: If you add an element to this, you need to add an element to the
45  /// Type::getPrimitiveType function, or else things will break!
46  /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
47  ///
48  enum TypeID {
49    // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
50    VoidTyID = 0,    ///<  0: type with no size
51    HalfTyID,        ///<  1: 16-bit floating point type
52    FloatTyID,       ///<  2: 32-bit floating point type
53    DoubleTyID,      ///<  3: 64-bit floating point type
54    X86_FP80TyID,    ///<  4: 80-bit floating point type (X87)
55    FP128TyID,       ///<  5: 128-bit floating point type (112-bit mantissa)
56    PPC_FP128TyID,   ///<  6: 128-bit floating point type (two 64-bits, PowerPC)
57    LabelTyID,       ///<  7: Labels
58    MetadataTyID,    ///<  8: Metadata
59    X86_MMXTyID,     ///<  9: MMX vectors (64 bits, X86 specific)
60
61    // Derived types... see DerivedTypes.h file.
62    // Make sure FirstDerivedTyID stays up to date!
63    IntegerTyID,     ///< 10: Arbitrary bit width integers
64    FunctionTyID,    ///< 11: Functions
65    StructTyID,      ///< 12: Structures
66    ArrayTyID,       ///< 13: Arrays
67    PointerTyID,     ///< 14: Pointers
68    VectorTyID,      ///< 15: SIMD 'packed' format, or other vector type
69
70    NumTypeIDs,                         // Must remain as last defined ID
71    LastPrimitiveTyID = X86_MMXTyID,
72    FirstDerivedTyID = IntegerTyID
73  };
74
75private:
76  /// Context - This refers to the LLVMContext in which this type was uniqued.
77  LLVMContext &Context;
78
79  // Due to Ubuntu GCC bug 910363:
80  // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363
81  // Bitpack ID and SubclassData manually.
82  // Note: TypeID : low 8 bit; SubclassData : high 24 bit.
83  uint32_t IDAndSubclassData;
84
85protected:
86  friend class LLVMContextImpl;
87  explicit Type(LLVMContext &C, TypeID tid)
88    : Context(C), IDAndSubclassData(0),
89      NumContainedTys(0), ContainedTys(0) {
90    setTypeID(tid);
91  }
92  ~Type() {}
93
94  void setTypeID(TypeID ID) {
95    IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00);
96    assert(getTypeID() == ID && "TypeID data too large for field");
97  }
98
99  unsigned getSubclassData() const { return IDAndSubclassData >> 8; }
100
101  void setSubclassData(unsigned val) {
102    IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8);
103    // Ensure we don't have any accidental truncation.
104    assert(getSubclassData() == val && "Subclass data too large for field");
105  }
106
107  /// NumContainedTys - Keeps track of how many Type*'s there are in the
108  /// ContainedTys list.
109  unsigned NumContainedTys;
110
111  /// ContainedTys - A pointer to the array of Types contained by this Type.
112  /// For example, this includes the arguments of a function type, the elements
113  /// of a structure, the pointee of a pointer, the element type of an array,
114  /// etc.  This pointer may be 0 for types that don't contain other types
115  /// (Integer, Double, Float).
116  Type * const *ContainedTys;
117
118public:
119  void print(raw_ostream &O) const;
120  void dump() const;
121
122  /// getContext - Return the LLVMContext in which this type was uniqued.
123  LLVMContext &getContext() const { return Context; }
124
125  //===--------------------------------------------------------------------===//
126  // Accessors for working with types.
127  //
128
129  /// getTypeID - Return the type id for the type.  This will return one
130  /// of the TypeID enum elements defined above.
131  ///
132  TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); }
133
134  /// isVoidTy - Return true if this is 'void'.
135  bool isVoidTy() const { return getTypeID() == VoidTyID; }
136
137  /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
138  bool isHalfTy() const { return getTypeID() == HalfTyID; }
139
140  /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
141  bool isFloatTy() const { return getTypeID() == FloatTyID; }
142
143  /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
144  bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
145
146  /// isX86_FP80Ty - Return true if this is x86 long double.
147  bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
148
149  /// isFP128Ty - Return true if this is 'fp128'.
150  bool isFP128Ty() const { return getTypeID() == FP128TyID; }
151
152  /// isPPC_FP128Ty - Return true if this is powerpc long double.
153  bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
154
155  /// isFloatingPointTy - Return true if this is one of the five floating point
156  /// types
157  bool isFloatingPointTy() const {
158    return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
159           getTypeID() == DoubleTyID ||
160           getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
161           getTypeID() == PPC_FP128TyID;
162  }
163
164  /// isX86_MMXTy - Return true if this is X86 MMX.
165  bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
166
167  /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
168  ///
169  bool isFPOrFPVectorTy() const;
170
171  /// isLabelTy - Return true if this is 'label'.
172  bool isLabelTy() const { return getTypeID() == LabelTyID; }
173
174  /// isMetadataTy - Return true if this is 'metadata'.
175  bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
176
177  /// isIntegerTy - True if this is an instance of IntegerType.
178  ///
179  bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
180
181  /// isIntegerTy - Return true if this is an IntegerType of the given width.
182  bool isIntegerTy(unsigned Bitwidth) const;
183
184  /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
185  /// integer types.
186  ///
187  bool isIntOrIntVectorTy() const;
188
189  /// isFunctionTy - True if this is an instance of FunctionType.
190  ///
191  bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
192
193  /// isStructTy - True if this is an instance of StructType.
194  ///
195  bool isStructTy() const { return getTypeID() == StructTyID; }
196
197  /// isArrayTy - True if this is an instance of ArrayType.
198  ///
199  bool isArrayTy() const { return getTypeID() == ArrayTyID; }
200
201  /// isPointerTy - True if this is an instance of PointerType.
202  ///
203  bool isPointerTy() const { return getTypeID() == PointerTyID; }
204
205  /// isVectorTy - True if this is an instance of VectorType.
206  ///
207  bool isVectorTy() const { return getTypeID() == VectorTyID; }
208
209  /// canLosslesslyBitCastTo - Return true if this type could be converted
210  /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
211  /// are valid for types of the same size only where no re-interpretation of
212  /// the bits is done.
213  /// @brief Determine if this type could be losslessly bitcast to Ty
214  bool canLosslesslyBitCastTo(Type *Ty) const;
215
216  /// isEmptyTy - Return true if this type is empty, that is, it has no
217  /// elements or all its elements are empty.
218  bool isEmptyTy() const;
219
220  /// Here are some useful little methods to query what type derived types are
221  /// Note that all other types can just compare to see if this == Type::xxxTy;
222  ///
223  bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; }
224  bool isDerivedType()   const { return getTypeID() >= FirstDerivedTyID; }
225
226  /// isFirstClassType - Return true if the type is "first class", meaning it
227  /// is a valid type for a Value.
228  ///
229  bool isFirstClassType() const {
230    return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
231  }
232
233  /// isSingleValueType - Return true if the type is a valid type for a
234  /// register in codegen.  This includes all first-class types except struct
235  /// and array types.
236  ///
237  bool isSingleValueType() const {
238    return (getTypeID() != VoidTyID && isPrimitiveType()) ||
239            getTypeID() == IntegerTyID || getTypeID() == PointerTyID ||
240            getTypeID() == VectorTyID;
241  }
242
243  /// isAggregateType - Return true if the type is an aggregate type. This
244  /// means it is valid as the first operand of an insertvalue or
245  /// extractvalue instruction. This includes struct and array types, but
246  /// does not include vector types.
247  ///
248  bool isAggregateType() const {
249    return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
250  }
251
252  /// isSized - Return true if it makes sense to take the size of this type.  To
253  /// get the actual size for a particular target, it is reasonable to use the
254  /// TargetData subsystem to do this.
255  ///
256  bool isSized() const {
257    // If it's a primitive, it is always sized.
258    if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
259        getTypeID() == PointerTyID ||
260        getTypeID() == X86_MMXTyID)
261      return true;
262    // If it is not something that can have a size (e.g. a function or label),
263    // it doesn't have a size.
264    if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
265        getTypeID() != VectorTyID)
266      return false;
267    // Otherwise we have to try harder to decide.
268    return isSizedDerivedType();
269  }
270
271  /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
272  /// primitive type.  These are fixed by LLVM and are not target dependent.
273  /// This will return zero if the type does not have a size or is not a
274  /// primitive type.
275  ///
276  /// Note that this may not reflect the size of memory allocated for an
277  /// instance of the type or the number of bytes that are written when an
278  /// instance of the type is stored to memory. The TargetData class provides
279  /// additional query functions to provide this information.
280  ///
281  unsigned getPrimitiveSizeInBits() const;
282
283  /// getScalarSizeInBits - If this is a vector type, return the
284  /// getPrimitiveSizeInBits value for the element type. Otherwise return the
285  /// getPrimitiveSizeInBits value for this type.
286  unsigned getScalarSizeInBits();
287
288  /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
289  /// is only valid on floating point types.  If the FP type does not
290  /// have a stable mantissa (e.g. ppc long double), this method returns -1.
291  int getFPMantissaWidth() const;
292
293  /// getScalarType - If this is a vector type, return the element type,
294  /// otherwise return 'this'.
295  Type *getScalarType();
296
297  //===--------------------------------------------------------------------===//
298  // Type Iteration support.
299  //
300  typedef Type * const *subtype_iterator;
301  subtype_iterator subtype_begin() const { return ContainedTys; }
302  subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
303
304  /// getContainedType - This method is used to implement the type iterator
305  /// (defined a the end of the file).  For derived types, this returns the
306  /// types 'contained' in the derived type.
307  ///
308  Type *getContainedType(unsigned i) const {
309    assert(i < NumContainedTys && "Index out of range!");
310    return ContainedTys[i];
311  }
312
313  /// getNumContainedTypes - Return the number of types in the derived type.
314  ///
315  unsigned getNumContainedTypes() const { return NumContainedTys; }
316
317  //===--------------------------------------------------------------------===//
318  // Static members exported by the Type class itself.  Useful for getting
319  // instances of Type.
320  //
321
322  /// getPrimitiveType - Return a type based on an identifier.
323  static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
324
325  //===--------------------------------------------------------------------===//
326  // These are the builtin types that are always available.
327  //
328  static Type *getVoidTy(LLVMContext &C);
329  static Type *getLabelTy(LLVMContext &C);
330  static Type *getHalfTy(LLVMContext &C);
331  static Type *getFloatTy(LLVMContext &C);
332  static Type *getDoubleTy(LLVMContext &C);
333  static Type *getMetadataTy(LLVMContext &C);
334  static Type *getX86_FP80Ty(LLVMContext &C);
335  static Type *getFP128Ty(LLVMContext &C);
336  static Type *getPPC_FP128Ty(LLVMContext &C);
337  static Type *getX86_MMXTy(LLVMContext &C);
338  static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
339  static IntegerType *getInt1Ty(LLVMContext &C);
340  static IntegerType *getInt8Ty(LLVMContext &C);
341  static IntegerType *getInt16Ty(LLVMContext &C);
342  static IntegerType *getInt32Ty(LLVMContext &C);
343  static IntegerType *getInt64Ty(LLVMContext &C);
344
345  //===--------------------------------------------------------------------===//
346  // Convenience methods for getting pointer types with one of the above builtin
347  // types as pointee.
348  //
349  static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
350  static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
351  static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
352  static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
353  static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
354  static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
355  static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
356  static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
357  static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
358  static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
359  static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
360  static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
361  static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
362
363  /// Methods for support type inquiry through isa, cast, and dyn_cast:
364  static inline bool classof(const Type *) { return true; }
365
366  /// getPointerTo - Return a pointer to the current type.  This is equivalent
367  /// to PointerType::get(Foo, AddrSpace).
368  PointerType *getPointerTo(unsigned AddrSpace = 0);
369
370private:
371  /// isSizedDerivedType - Derived types like structures and arrays are sized
372  /// iff all of the members of the type are sized as well.  Since asking for
373  /// their size is relatively uncommon, move this operation out of line.
374  bool isSizedDerivedType() const;
375};
376
377// Printing of types.
378static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
379  T.print(OS);
380  return OS;
381}
382
383// allow isa<PointerType>(x) to work without DerivedTypes.h included.
384template <> struct isa_impl<PointerType, Type> {
385  static inline bool doit(const Type &Ty) {
386    return Ty.getTypeID() == Type::PointerTyID;
387  }
388};
389
390
391//===----------------------------------------------------------------------===//
392// Provide specializations of GraphTraits to be able to treat a type as a
393// graph of sub types.
394
395
396template <> struct GraphTraits<Type*> {
397  typedef Type NodeType;
398  typedef Type::subtype_iterator ChildIteratorType;
399
400  static inline NodeType *getEntryNode(Type *T) { return T; }
401  static inline ChildIteratorType child_begin(NodeType *N) {
402    return N->subtype_begin();
403  }
404  static inline ChildIteratorType child_end(NodeType *N) {
405    return N->subtype_end();
406  }
407};
408
409template <> struct GraphTraits<const Type*> {
410  typedef const Type NodeType;
411  typedef Type::subtype_iterator ChildIteratorType;
412
413  static inline NodeType *getEntryNode(NodeType *T) { return T; }
414  static inline ChildIteratorType child_begin(NodeType *N) {
415    return N->subtype_begin();
416  }
417  static inline ChildIteratorType child_end(NodeType *N) {
418    return N->subtype_end();
419  }
420};
421
422} // End llvm namespace
423
424#endif
425