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