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"
27 class LLVMContextImpl;
28 template<class GraphType> struct GraphTraits;
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
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.
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)
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
69 NumTypeIDs, // Must remain as last defined ID
70 LastPrimitiveTyID = X86_MMXTyID,
71 FirstDerivedTyID = IntegerTyID
75 /// Context - This refers to the LLVMContext in which this type was uniqued.
78 TypeID ID : 8; // The current base type of this type.
79 unsigned SubclassData : 24; // Space for subclasses to store data
82 friend class LLVMContextImpl;
83 explicit Type(LLVMContext &C, TypeID tid)
84 : Context(C), ID(tid), SubclassData(0),
85 NumContainedTys(0), ContainedTys(0) {}
88 unsigned getSubclassData() const { return SubclassData; }
89 void setSubclassData(unsigned val) {
91 // Ensure we don't have any accidental truncation.
92 assert(SubclassData == val && "Subclass data too large for field");
95 /// NumContainedTys - Keeps track of how many Type*'s there are in the
96 /// ContainedTys list.
97 unsigned NumContainedTys;
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;
107 void print(raw_ostream &O) const;
110 /// getContext - Return the LLVMContext in which this type was uniqued.
111 LLVMContext &getContext() const { return Context; }
113 //===--------------------------------------------------------------------===//
114 // Accessors for working with types.
117 /// getTypeID - Return the type id for the type. This will return one
118 /// of the TypeID enum elements defined above.
120 TypeID getTypeID() const { return ID; }
122 /// isVoidTy - Return true if this is 'void'.
123 bool isVoidTy() const { return ID == VoidTyID; }
125 /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
126 bool isHalfTy() const { return ID == HalfTyID; }
128 /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
129 bool isFloatTy() const { return ID == FloatTyID; }
131 /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
132 bool isDoubleTy() const { return ID == DoubleTyID; }
134 /// isX86_FP80Ty - Return true if this is x86 long double.
135 bool isX86_FP80Ty() const { return ID == X86_FP80TyID; }
137 /// isFP128Ty - Return true if this is 'fp128'.
138 bool isFP128Ty() const { return ID == FP128TyID; }
140 /// isPPC_FP128Ty - Return true if this is powerpc long double.
141 bool isPPC_FP128Ty() const { return ID == PPC_FP128TyID; }
143 /// isFloatingPointTy - Return true if this is one of the five floating point
145 bool isFloatingPointTy() const {
146 return ID == HalfTyID || ID == FloatTyID || ID == DoubleTyID ||
147 ID == X86_FP80TyID || ID == FP128TyID || ID == PPC_FP128TyID;
150 /// isX86_MMXTy - Return true if this is X86 MMX.
151 bool isX86_MMXTy() const { return ID == X86_MMXTyID; }
153 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
155 bool isFPOrFPVectorTy() const;
157 /// isLabelTy - Return true if this is 'label'.
158 bool isLabelTy() const { return ID == LabelTyID; }
160 /// isMetadataTy - Return true if this is 'metadata'.
161 bool isMetadataTy() const { return ID == MetadataTyID; }
163 /// isIntegerTy - True if this is an instance of IntegerType.
165 bool isIntegerTy() const { return ID == IntegerTyID; }
167 /// isIntegerTy - Return true if this is an IntegerType of the given width.
168 bool isIntegerTy(unsigned Bitwidth) const;
170 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
173 bool isIntOrIntVectorTy() const;
175 /// isFunctionTy - True if this is an instance of FunctionType.
177 bool isFunctionTy() const { return ID == FunctionTyID; }
179 /// isStructTy - True if this is an instance of StructType.
181 bool isStructTy() const { return ID == StructTyID; }
183 /// isArrayTy - True if this is an instance of ArrayType.
185 bool isArrayTy() const { return ID == ArrayTyID; }
187 /// isPointerTy - True if this is an instance of PointerType.
189 bool isPointerTy() const { return ID == PointerTyID; }
191 /// isVectorTy - True if this is an instance of VectorType.
193 bool isVectorTy() const { return ID == VectorTyID; }
195 /// canLosslesslyBitCastTo - Return true if this type could be converted
196 /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
197 /// are valid for types of the same size only where no re-interpretation of
198 /// the bits is done.
199 /// @brief Determine if this type could be losslessly bitcast to Ty
200 bool canLosslesslyBitCastTo(Type *Ty) const;
202 /// isEmptyTy - Return true if this type is empty, that is, it has no
203 /// elements or all its elements are empty.
204 bool isEmptyTy() const;
206 /// Here are some useful little methods to query what type derived types are
207 /// Note that all other types can just compare to see if this == Type::xxxTy;
209 bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; }
210 bool isDerivedType() const { return ID >= FirstDerivedTyID; }
212 /// isFirstClassType - Return true if the type is "first class", meaning it
213 /// is a valid type for a Value.
215 bool isFirstClassType() const {
216 return ID != FunctionTyID && ID != VoidTyID;
219 /// isSingleValueType - Return true if the type is a valid type for a
220 /// register in codegen. This includes all first-class types except struct
223 bool isSingleValueType() const {
224 return (ID != VoidTyID && isPrimitiveType()) ||
225 ID == IntegerTyID || ID == PointerTyID || ID == VectorTyID;
228 /// isAggregateType - Return true if the type is an aggregate type. This
229 /// means it is valid as the first operand of an insertvalue or
230 /// extractvalue instruction. This includes struct and array types, but
231 /// does not include vector types.
233 bool isAggregateType() const {
234 return ID == StructTyID || ID == ArrayTyID;
237 /// isSized - Return true if it makes sense to take the size of this type. To
238 /// get the actual size for a particular target, it is reasonable to use the
239 /// TargetData subsystem to do this.
241 bool isSized() const {
242 // If it's a primitive, it is always sized.
243 if (ID == IntegerTyID || isFloatingPointTy() || ID == PointerTyID ||
246 // If it is not something that can have a size (e.g. a function or label),
247 // it doesn't have a size.
248 if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID)
250 // Otherwise we have to try harder to decide.
251 return isSizedDerivedType();
254 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
255 /// primitive type. These are fixed by LLVM and are not target dependent.
256 /// This will return zero if the type does not have a size or is not a
259 /// Note that this may not reflect the size of memory allocated for an
260 /// instance of the type or the number of bytes that are written when an
261 /// instance of the type is stored to memory. The TargetData class provides
262 /// additional query functions to provide this information.
264 unsigned getPrimitiveSizeInBits() const;
266 /// getScalarSizeInBits - If this is a vector type, return the
267 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
268 /// getPrimitiveSizeInBits value for this type.
269 unsigned getScalarSizeInBits();
271 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
272 /// is only valid on floating point types. If the FP type does not
273 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
274 int getFPMantissaWidth() const;
276 /// getScalarType - If this is a vector type, return the element type,
277 /// otherwise return 'this'.
278 Type *getScalarType();
280 /// getNumElements - If this is a vector type, return the number of elements,
281 /// otherwise return zero.
282 unsigned getNumElements();
284 //===--------------------------------------------------------------------===//
285 // Type Iteration support.
287 typedef Type * const *subtype_iterator;
288 subtype_iterator subtype_begin() const { return ContainedTys; }
289 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
291 /// getContainedType - This method is used to implement the type iterator
292 /// (defined a the end of the file). For derived types, this returns the
293 /// types 'contained' in the derived type.
295 Type *getContainedType(unsigned i) const {
296 assert(i < NumContainedTys && "Index out of range!");
297 return ContainedTys[i];
300 /// getNumContainedTypes - Return the number of types in the derived type.
302 unsigned getNumContainedTypes() const { return NumContainedTys; }
304 //===--------------------------------------------------------------------===//
305 // Static members exported by the Type class itself. Useful for getting
306 // instances of Type.
309 /// getPrimitiveType - Return a type based on an identifier.
310 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
312 //===--------------------------------------------------------------------===//
313 // These are the builtin types that are always available.
315 static Type *getVoidTy(LLVMContext &C);
316 static Type *getLabelTy(LLVMContext &C);
317 static Type *getHalfTy(LLVMContext &C);
318 static Type *getFloatTy(LLVMContext &C);
319 static Type *getDoubleTy(LLVMContext &C);
320 static Type *getMetadataTy(LLVMContext &C);
321 static Type *getX86_FP80Ty(LLVMContext &C);
322 static Type *getFP128Ty(LLVMContext &C);
323 static Type *getPPC_FP128Ty(LLVMContext &C);
324 static Type *getX86_MMXTy(LLVMContext &C);
325 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
326 static IntegerType *getInt1Ty(LLVMContext &C);
327 static IntegerType *getInt8Ty(LLVMContext &C);
328 static IntegerType *getInt16Ty(LLVMContext &C);
329 static IntegerType *getInt32Ty(LLVMContext &C);
330 static IntegerType *getInt64Ty(LLVMContext &C);
332 //===--------------------------------------------------------------------===//
333 // Convenience methods for getting pointer types with one of the above builtin
336 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
337 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
338 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
339 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
340 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
341 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
342 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
343 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
344 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
345 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
346 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
347 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
348 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
350 /// Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const Type *) { return true; }
353 /// getPointerTo - Return a pointer to the current type. This is equivalent
354 /// to PointerType::get(Foo, AddrSpace).
355 PointerType *getPointerTo(unsigned AddrSpace = 0);
358 /// isSizedDerivedType - Derived types like structures and arrays are sized
359 /// iff all of the members of the type are sized as well. Since asking for
360 /// their size is relatively uncommon, move this operation out of line.
361 bool isSizedDerivedType() const;
364 // Printing of types.
365 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
370 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
371 template <> struct isa_impl<PointerType, Type> {
372 static inline bool doit(const Type &Ty) {
373 return Ty.getTypeID() == Type::PointerTyID;
378 //===----------------------------------------------------------------------===//
379 // Provide specializations of GraphTraits to be able to treat a type as a
380 // graph of sub types.
383 template <> struct GraphTraits<Type*> {
384 typedef Type NodeType;
385 typedef Type::subtype_iterator ChildIteratorType;
387 static inline NodeType *getEntryNode(Type *T) { return T; }
388 static inline ChildIteratorType child_begin(NodeType *N) {
389 return N->subtype_begin();
391 static inline ChildIteratorType child_end(NodeType *N) {
392 return N->subtype_end();
396 template <> struct GraphTraits<const Type*> {
397 typedef const Type NodeType;
398 typedef Type::subtype_iterator ChildIteratorType;
400 static inline NodeType *getEntryNode(NodeType *T) { return T; }
401 static inline ChildIteratorType child_begin(NodeType *N) {
402 return N->subtype_begin();
404 static inline ChildIteratorType child_end(NodeType *N) {
405 return N->subtype_end();
409 } // End llvm namespace