Type.h revision 1afcace3a3a138b1b18e5c6270caa8dae2261ae2
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 DerivedType;
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 "DerivedType" subclass (see 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    FloatTyID,       ///<  1: 32-bit floating point type
52    DoubleTyID,      ///<  2: 64-bit floating point type
53    X86_FP80TyID,    ///<  3: 80-bit floating point type (X87)
54    FP128TyID,       ///<  4: 128-bit floating point type (112-bit mantissa)
55    PPC_FP128TyID,   ///<  5: 128-bit floating point type (two 64-bits, PowerPC)
56    LabelTyID,       ///<  6: Labels
57    MetadataTyID,    ///<  7: Metadata
58    X86_MMXTyID,     ///<  8: MMX vectors (64 bits, X86 specific)
59
60    // Derived types... see DerivedTypes.h file.
61    // Make sure FirstDerivedTyID stays up to date!
62    IntegerTyID,     ///<  9: Arbitrary bit width integers
63    FunctionTyID,    ///< 10: Functions
64    StructTyID,      ///< 11: Structures
65    ArrayTyID,       ///< 12: Arrays
66    PointerTyID,     ///< 13: Pointers
67    VectorTyID,      ///< 14: 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 ID == VoidTyID; }
124
125  /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
126  bool isFloatTy() const { return ID == FloatTyID; }
127
128  /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
129  bool isDoubleTy() const { return ID == DoubleTyID; }
130
131  /// isX86_FP80Ty - Return true if this is x86 long double.
132  bool isX86_FP80Ty() const { return ID == X86_FP80TyID; }
133
134  /// isFP128Ty - Return true if this is 'fp128'.
135  bool isFP128Ty() const { return ID == FP128TyID; }
136
137  /// isPPC_FP128Ty - Return true if this is powerpc long double.
138  bool isPPC_FP128Ty() const { return ID == PPC_FP128TyID; }
139
140  /// isFloatingPointTy - Return true if this is one of the five floating point
141  /// types
142  bool isFloatingPointTy() const {
143    return ID == FloatTyID || ID == DoubleTyID ||
144      ID == X86_FP80TyID || ID == FP128TyID || ID == PPC_FP128TyID;
145  }
146
147  /// isX86_MMXTy - Return true if this is X86 MMX.
148  bool isX86_MMXTy() const { return ID == X86_MMXTyID; }
149
150  /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
151  ///
152  bool isFPOrFPVectorTy() const;
153
154  /// isLabelTy - Return true if this is 'label'.
155  bool isLabelTy() const { return ID == LabelTyID; }
156
157  /// isMetadataTy - Return true if this is 'metadata'.
158  bool isMetadataTy() const { return ID == MetadataTyID; }
159
160  /// isIntegerTy - True if this is an instance of IntegerType.
161  ///
162  bool isIntegerTy() const { return ID == IntegerTyID; }
163
164  /// isIntegerTy - Return true if this is an IntegerType of the given width.
165  bool isIntegerTy(unsigned Bitwidth) const;
166
167  /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
168  /// integer types.
169  ///
170  bool isIntOrIntVectorTy() const;
171
172  /// isFunctionTy - True if this is an instance of FunctionType.
173  ///
174  bool isFunctionTy() const { return ID == FunctionTyID; }
175
176  /// isStructTy - True if this is an instance of StructType.
177  ///
178  bool isStructTy() const { return ID == StructTyID; }
179
180  /// isArrayTy - True if this is an instance of ArrayType.
181  ///
182  bool isArrayTy() const { return ID == ArrayTyID; }
183
184  /// isPointerTy - True if this is an instance of PointerType.
185  ///
186  bool isPointerTy() const { return ID == PointerTyID; }
187
188  /// isVectorTy - True if this is an instance of VectorType.
189  ///
190  bool isVectorTy() const { return ID == VectorTyID; }
191
192  /// canLosslesslyBitCastTo - Return true if this type could be converted
193  /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
194  /// are valid for types of the same size only where no re-interpretation of
195  /// the bits is done.
196  /// @brief Determine if this type could be losslessly bitcast to Ty
197  bool canLosslesslyBitCastTo(const Type *Ty) const;
198
199  /// isEmptyTy - Return true if this type is empty, that is, it has no
200  /// elements or all its elements are empty.
201  bool isEmptyTy() const;
202
203  /// Here are some useful little methods to query what type derived types are
204  /// Note that all other types can just compare to see if this == Type::xxxTy;
205  ///
206  bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; }
207  bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
208
209  /// isFirstClassType - Return true if the type is "first class", meaning it
210  /// is a valid type for a Value.
211  ///
212  bool isFirstClassType() const {
213    return ID != FunctionTyID && ID != VoidTyID;
214  }
215
216  /// isSingleValueType - Return true if the type is a valid type for a
217  /// register in codegen.  This includes all first-class types except struct
218  /// and array types.
219  ///
220  bool isSingleValueType() const {
221    return (ID != VoidTyID && isPrimitiveType()) ||
222            ID == IntegerTyID || ID == PointerTyID || ID == VectorTyID;
223  }
224
225  /// isAggregateType - Return true if the type is an aggregate type. This
226  /// means it is valid as the first operand of an insertvalue or
227  /// extractvalue instruction. This includes struct and array types, but
228  /// does not include vector types.
229  ///
230  bool isAggregateType() const {
231    return ID == StructTyID || ID == ArrayTyID;
232  }
233
234  /// isSized - Return true if it makes sense to take the size of this type.  To
235  /// get the actual size for a particular target, it is reasonable to use the
236  /// TargetData subsystem to do this.
237  ///
238  bool isSized() const {
239    // If it's a primitive, it is always sized.
240    if (ID == IntegerTyID || isFloatingPointTy() || ID == PointerTyID ||
241        ID == X86_MMXTyID)
242      return true;
243    // If it is not something that can have a size (e.g. a function or label),
244    // it doesn't have a size.
245    if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID)
246      return false;
247    // Otherwise we have to try harder to decide.
248    return isSizedDerivedType();
249  }
250
251  /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
252  /// primitive type.  These are fixed by LLVM and are not target dependent.
253  /// This will return zero if the type does not have a size or is not a
254  /// primitive type.
255  ///
256  /// Note that this may not reflect the size of memory allocated for an
257  /// instance of the type or the number of bytes that are written when an
258  /// instance of the type is stored to memory. The TargetData class provides
259  /// additional query functions to provide this information.
260  ///
261  unsigned getPrimitiveSizeInBits() const;
262
263  /// getScalarSizeInBits - If this is a vector type, return the
264  /// getPrimitiveSizeInBits value for the element type. Otherwise return the
265  /// getPrimitiveSizeInBits value for this type.
266  unsigned getScalarSizeInBits() const;
267
268  /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
269  /// is only valid on floating point types.  If the FP type does not
270  /// have a stable mantissa (e.g. ppc long double), this method returns -1.
271  int getFPMantissaWidth() const;
272
273  /// getScalarType - If this is a vector type, return the element type,
274  /// otherwise return 'this'.
275  const Type *getScalarType() const;
276
277  //===--------------------------------------------------------------------===//
278  // Type Iteration support.
279  //
280  typedef Type * const *subtype_iterator;
281  subtype_iterator subtype_begin() const { return ContainedTys; }
282  subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
283
284  /// getContainedType - This method is used to implement the type iterator
285  /// (defined a the end of the file).  For derived types, this returns the
286  /// types 'contained' in the derived type.
287  ///
288  Type *getContainedType(unsigned i) const {
289    assert(i < NumContainedTys && "Index out of range!");
290    return ContainedTys[i];
291  }
292
293  /// getNumContainedTypes - Return the number of types in the derived type.
294  ///
295  unsigned getNumContainedTypes() const { return NumContainedTys; }
296
297  //===--------------------------------------------------------------------===//
298  // Static members exported by the Type class itself.  Useful for getting
299  // instances of Type.
300  //
301
302  /// getPrimitiveType - Return a type based on an identifier.
303  static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
304
305  //===--------------------------------------------------------------------===//
306  // These are the builtin types that are always available.
307  //
308  static Type *getVoidTy(LLVMContext &C);
309  static Type *getLabelTy(LLVMContext &C);
310  static Type *getFloatTy(LLVMContext &C);
311  static Type *getDoubleTy(LLVMContext &C);
312  static Type *getMetadataTy(LLVMContext &C);
313  static Type *getX86_FP80Ty(LLVMContext &C);
314  static Type *getFP128Ty(LLVMContext &C);
315  static Type *getPPC_FP128Ty(LLVMContext &C);
316  static Type *getX86_MMXTy(LLVMContext &C);
317  static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
318  static IntegerType *getInt1Ty(LLVMContext &C);
319  static IntegerType *getInt8Ty(LLVMContext &C);
320  static IntegerType *getInt16Ty(LLVMContext &C);
321  static IntegerType *getInt32Ty(LLVMContext &C);
322  static IntegerType *getInt64Ty(LLVMContext &C);
323
324  //===--------------------------------------------------------------------===//
325  // Convenience methods for getting pointer types with one of the above builtin
326  // types as pointee.
327  //
328  static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
329  static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
330  static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
331  static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
332  static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
333  static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
334  static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
335  static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
336  static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
337  static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
338  static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
339  static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
340
341  /// Methods for support type inquiry through isa, cast, and dyn_cast:
342  static inline bool classof(const Type *) { return true; }
343
344  /// getPointerTo - Return a pointer to the current type.  This is equivalent
345  /// to PointerType::get(Foo, AddrSpace).
346  PointerType *getPointerTo(unsigned AddrSpace = 0) const;
347
348private:
349  /// isSizedDerivedType - Derived types like structures and arrays are sized
350  /// iff all of the members of the type are sized as well.  Since asking for
351  /// their size is relatively uncommon, move this operation out of line.
352  bool isSizedDerivedType() const;
353};
354
355// Printing of types.
356static inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
357  T.print(OS);
358  return OS;
359}
360
361// allow isa<PointerType>(x) to work without DerivedTypes.h included.
362template <> struct isa_impl<PointerType, Type> {
363  static inline bool doit(const Type &Ty) {
364    return Ty.getTypeID() == Type::PointerTyID;
365  }
366};
367
368
369//===----------------------------------------------------------------------===//
370// Provide specializations of GraphTraits to be able to treat a type as a
371// graph of sub types.
372
373
374template <> struct GraphTraits<Type*> {
375  typedef Type NodeType;
376  typedef Type::subtype_iterator ChildIteratorType;
377
378  static inline NodeType *getEntryNode(Type *T) { return T; }
379  static inline ChildIteratorType child_begin(NodeType *N) {
380    return N->subtype_begin();
381  }
382  static inline ChildIteratorType child_end(NodeType *N) {
383    return N->subtype_end();
384  }
385};
386
387template <> struct GraphTraits<const Type*> {
388  typedef const Type NodeType;
389  typedef Type::subtype_iterator ChildIteratorType;
390
391  static inline NodeType *getEntryNode(const Type *T) { return T; }
392  static inline ChildIteratorType child_begin(NodeType *N) {
393    return N->subtype_begin();
394  }
395  static inline ChildIteratorType child_end(NodeType *N) {
396    return N->subtype_end();
397  }
398};
399
400} // End llvm namespace
401
402#endif
403