1 //===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the Type class. For more "Type"
11 // stuff, look in DerivedTypes.h.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/DataTypes.h"
28 class LLVMContextImpl;
30 template<class GraphType> struct GraphTraits;
32 /// The instances of the Type class are immutable: once they are created,
33 /// they are never changed. Also note that only one instance of a particular
34 /// type is ever created. Thus seeing if two types are equal is a matter of
35 /// doing a trivial pointer comparison. To enforce that no two equal instances
36 /// are created, Type instances can only be created via static factory methods
37 /// in class Type and in derived classes. Once allocated, Types are never
42 //===--------------------------------------------------------------------===//
43 /// Definitions of all of the base types for the Type system. Based on this
44 /// value, you can cast to a class defined in DerivedTypes.h.
45 /// Note: If you add an element to this, you need to add an element to the
46 /// Type::getPrimitiveType function, or else things will break!
47 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
50 // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
51 VoidTyID = 0, ///< 0: type with no size
52 HalfTyID, ///< 1: 16-bit floating point type
53 FloatTyID, ///< 2: 32-bit floating point type
54 DoubleTyID, ///< 3: 64-bit floating point type
55 X86_FP80TyID, ///< 4: 80-bit floating point type (X87)
56 FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa)
57 PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC)
58 LabelTyID, ///< 7: Labels
59 MetadataTyID, ///< 8: Metadata
60 X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific)
62 // Derived types... see DerivedTypes.h file.
63 // Make sure FirstDerivedTyID stays up to date!
64 IntegerTyID, ///< 10: Arbitrary bit width integers
65 FunctionTyID, ///< 11: Functions
66 StructTyID, ///< 12: Structures
67 ArrayTyID, ///< 13: Arrays
68 PointerTyID, ///< 14: Pointers
69 VectorTyID, ///< 15: SIMD 'packed' format, or other vector type
71 NumTypeIDs, // Must remain as last defined ID
72 LastPrimitiveTyID = X86_MMXTyID,
73 FirstDerivedTyID = IntegerTyID
77 /// Context - This refers to the LLVMContext in which this type was uniqued.
80 // Due to Ubuntu GCC bug 910363:
81 // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363
82 // Bitpack ID and SubclassData manually.
83 // Note: TypeID : low 8 bit; SubclassData : high 24 bit.
84 uint32_t IDAndSubclassData;
87 friend class LLVMContextImpl;
88 explicit Type(LLVMContext &C, TypeID tid)
89 : Context(C), IDAndSubclassData(0),
90 NumContainedTys(0), ContainedTys(0) {
95 void setTypeID(TypeID ID) {
96 IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00);
97 assert(getTypeID() == ID && "TypeID data too large for field");
100 unsigned getSubclassData() const { return IDAndSubclassData >> 8; }
102 void setSubclassData(unsigned val) {
103 IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8);
104 // Ensure we don't have any accidental truncation.
105 assert(getSubclassData() == val && "Subclass data too large for field");
108 /// NumContainedTys - Keeps track of how many Type*'s there are in the
109 /// ContainedTys list.
110 unsigned NumContainedTys;
112 /// ContainedTys - A pointer to the array of Types contained by this Type.
113 /// For example, this includes the arguments of a function type, the elements
114 /// of a structure, the pointee of a pointer, the element type of an array,
115 /// etc. This pointer may be 0 for types that don't contain other types
116 /// (Integer, Double, Float).
117 Type * const *ContainedTys;
120 void print(raw_ostream &O) const;
123 /// getContext - Return the LLVMContext in which this type was uniqued.
124 LLVMContext &getContext() const { return Context; }
126 //===--------------------------------------------------------------------===//
127 // Accessors for working with types.
130 /// getTypeID - Return the type id for the type. This will return one
131 /// of the TypeID enum elements defined above.
133 TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); }
135 /// isVoidTy - Return true if this is 'void'.
136 bool isVoidTy() const { return getTypeID() == VoidTyID; }
138 /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
139 bool isHalfTy() const { return getTypeID() == HalfTyID; }
141 /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
142 bool isFloatTy() const { return getTypeID() == FloatTyID; }
144 /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
145 bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
147 /// isX86_FP80Ty - Return true if this is x86 long double.
148 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
150 /// isFP128Ty - Return true if this is 'fp128'.
151 bool isFP128Ty() const { return getTypeID() == FP128TyID; }
153 /// isPPC_FP128Ty - Return true if this is powerpc long double.
154 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
156 /// isFloatingPointTy - Return true if this is one of the five floating point
158 bool isFloatingPointTy() const {
159 return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
160 getTypeID() == DoubleTyID ||
161 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
162 getTypeID() == PPC_FP128TyID;
165 /// isX86_MMXTy - Return true if this is X86 MMX.
166 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
168 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
170 bool isFPOrFPVectorTy() const;
172 /// isLabelTy - Return true if this is 'label'.
173 bool isLabelTy() const { return getTypeID() == LabelTyID; }
175 /// isMetadataTy - Return true if this is 'metadata'.
176 bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
178 /// isIntegerTy - True if this is an instance of IntegerType.
180 bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
182 /// isIntegerTy - Return true if this is an IntegerType of the given width.
183 bool isIntegerTy(unsigned Bitwidth) const;
185 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
188 bool isIntOrIntVectorTy() const;
190 /// isFunctionTy - True if this is an instance of FunctionType.
192 bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
194 /// isStructTy - True if this is an instance of StructType.
196 bool isStructTy() const { return getTypeID() == StructTyID; }
198 /// isArrayTy - True if this is an instance of ArrayType.
200 bool isArrayTy() const { return getTypeID() == ArrayTyID; }
202 /// isPointerTy - True if this is an instance of PointerType.
204 bool isPointerTy() const { return getTypeID() == PointerTyID; }
206 /// isVectorTy - True if this is an instance of VectorType.
208 bool isVectorTy() const { return getTypeID() == VectorTyID; }
210 /// canLosslesslyBitCastTo - Return true if this type could be converted
211 /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
212 /// are valid for types of the same size only where no re-interpretation of
213 /// the bits is done.
214 /// @brief Determine if this type could be losslessly bitcast to Ty
215 bool canLosslesslyBitCastTo(Type *Ty) const;
217 /// isEmptyTy - Return true if this type is empty, that is, it has no
218 /// elements or all its elements are empty.
219 bool isEmptyTy() const;
221 /// Here are some useful little methods to query what type derived types are
222 /// Note that all other types can just compare to see if this == Type::xxxTy;
224 bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; }
225 bool isDerivedType() const { return getTypeID() >= FirstDerivedTyID; }
227 /// isFirstClassType - Return true if the type is "first class", meaning it
228 /// is a valid type for a Value.
230 bool isFirstClassType() const {
231 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
234 /// isSingleValueType - Return true if the type is a valid type for a
235 /// register in codegen. This includes all first-class types except struct
238 bool isSingleValueType() const {
239 return (getTypeID() != VoidTyID && isPrimitiveType()) ||
240 getTypeID() == IntegerTyID || getTypeID() == PointerTyID ||
241 getTypeID() == VectorTyID;
244 /// isAggregateType - Return true if the type is an aggregate type. This
245 /// means it is valid as the first operand of an insertvalue or
246 /// extractvalue instruction. This includes struct and array types, but
247 /// does not include vector types.
249 bool isAggregateType() const {
250 return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
253 /// isSized - Return true if it makes sense to take the size of this type. To
254 /// get the actual size for a particular target, it is reasonable to use the
255 /// TargetData subsystem to do this.
257 bool isSized() const {
258 // If it's a primitive, it is always sized.
259 if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
260 getTypeID() == PointerTyID ||
261 getTypeID() == X86_MMXTyID)
263 // If it is not something that can have a size (e.g. a function or label),
264 // it doesn't have a size.
265 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
266 getTypeID() != VectorTyID)
268 // Otherwise we have to try harder to decide.
269 return isSizedDerivedType();
272 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
273 /// primitive type. These are fixed by LLVM and are not target dependent.
274 /// This will return zero if the type does not have a size or is not a
277 /// Note that this may not reflect the size of memory allocated for an
278 /// instance of the type or the number of bytes that are written when an
279 /// instance of the type is stored to memory. The TargetData class provides
280 /// additional query functions to provide this information.
282 unsigned getPrimitiveSizeInBits() const;
284 /// getScalarSizeInBits - If this is a vector type, return the
285 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
286 /// getPrimitiveSizeInBits value for this type.
287 unsigned getScalarSizeInBits();
289 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
290 /// is only valid on floating point types. If the FP type does not
291 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
292 int getFPMantissaWidth() const;
294 /// getScalarType - If this is a vector type, return the element type,
295 /// otherwise return 'this'.
296 Type *getScalarType();
298 //===--------------------------------------------------------------------===//
299 // Type Iteration support.
301 typedef Type * const *subtype_iterator;
302 subtype_iterator subtype_begin() const { return ContainedTys; }
303 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
305 /// getContainedType - This method is used to implement the type iterator
306 /// (defined a the end of the file). For derived types, this returns the
307 /// types 'contained' in the derived type.
309 Type *getContainedType(unsigned i) const {
310 assert(i < NumContainedTys && "Index out of range!");
311 return ContainedTys[i];
314 /// getNumContainedTypes - Return the number of types in the derived type.
316 unsigned getNumContainedTypes() const { return NumContainedTys; }
318 //===--------------------------------------------------------------------===//
319 // Helper methods corresponding to subclass methods. This forces a cast to
320 // the specified subclass and calls its accessor. "getVectorNumElements" (for
321 // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is
322 // only intended to cover the core methods that are frequently used, helper
323 // methods should not be added here.
325 unsigned getIntegerBitWidth() const;
327 Type *getFunctionParamType(unsigned i) const;
328 unsigned getFunctionNumParams() const;
329 bool isFunctionVarArg() const;
331 StringRef getStructName() const;
332 unsigned getStructNumElements() const;
333 Type *getStructElementType(unsigned N) const;
335 Type *getSequentialElementType() const;
337 uint64_t getArrayNumElements() const;
338 Type *getArrayElementType() const { return getSequentialElementType(); }
340 unsigned getVectorNumElements() const;
341 Type *getVectorElementType() const { return getSequentialElementType(); }
343 unsigned getPointerAddressSpace() const;
344 Type *getPointerElementType() const { return getSequentialElementType(); }
346 //===--------------------------------------------------------------------===//
347 // Static members exported by the Type class itself. Useful for getting
348 // instances of Type.
351 /// getPrimitiveType - Return a type based on an identifier.
352 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
354 //===--------------------------------------------------------------------===//
355 // These are the builtin types that are always available.
357 static Type *getVoidTy(LLVMContext &C);
358 static Type *getLabelTy(LLVMContext &C);
359 static Type *getHalfTy(LLVMContext &C);
360 static Type *getFloatTy(LLVMContext &C);
361 static Type *getDoubleTy(LLVMContext &C);
362 static Type *getMetadataTy(LLVMContext &C);
363 static Type *getX86_FP80Ty(LLVMContext &C);
364 static Type *getFP128Ty(LLVMContext &C);
365 static Type *getPPC_FP128Ty(LLVMContext &C);
366 static Type *getX86_MMXTy(LLVMContext &C);
367 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
368 static IntegerType *getInt1Ty(LLVMContext &C);
369 static IntegerType *getInt8Ty(LLVMContext &C);
370 static IntegerType *getInt16Ty(LLVMContext &C);
371 static IntegerType *getInt32Ty(LLVMContext &C);
372 static IntegerType *getInt64Ty(LLVMContext &C);
374 //===--------------------------------------------------------------------===//
375 // Convenience methods for getting pointer types with one of the above builtin
378 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
379 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
380 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
381 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
382 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
383 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
384 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
385 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
386 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
387 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
388 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
389 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
390 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
392 /// Methods for support type inquiry through isa, cast, and dyn_cast:
393 static inline bool classof(const Type *) { return true; }
395 /// getPointerTo - Return a pointer to the current type. This is equivalent
396 /// to PointerType::get(Foo, AddrSpace).
397 PointerType *getPointerTo(unsigned AddrSpace = 0);
400 /// isSizedDerivedType - Derived types like structures and arrays are sized
401 /// iff all of the members of the type are sized as well. Since asking for
402 /// their size is relatively uncommon, move this operation out of line.
403 bool isSizedDerivedType() const;
406 // Printing of types.
407 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
412 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
413 template <> struct isa_impl<PointerType, Type> {
414 static inline bool doit(const Type &Ty) {
415 return Ty.getTypeID() == Type::PointerTyID;
420 //===----------------------------------------------------------------------===//
421 // Provide specializations of GraphTraits to be able to treat a type as a
422 // graph of sub types.
425 template <> struct GraphTraits<Type*> {
426 typedef Type NodeType;
427 typedef Type::subtype_iterator ChildIteratorType;
429 static inline NodeType *getEntryNode(Type *T) { return T; }
430 static inline ChildIteratorType child_begin(NodeType *N) {
431 return N->subtype_begin();
433 static inline ChildIteratorType child_end(NodeType *N) {
434 return N->subtype_end();
438 template <> struct GraphTraits<const Type*> {
439 typedef const Type NodeType;
440 typedef Type::subtype_iterator ChildIteratorType;
442 static inline NodeType *getEntryNode(NodeType *T) { return T; }
443 static inline ChildIteratorType child_begin(NodeType *N) {
444 return N->subtype_begin();
446 static inline ChildIteratorType child_end(NodeType *N) {
447 return N->subtype_end();
451 } // End llvm namespace