LLVM API Documentation
00001 //===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the Type class. For more "Type" 00011 // stuff, look in DerivedTypes.h. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_TYPE_H 00016 #define LLVM_TYPE_H 00017 00018 #include "llvm/Support/Casting.h" 00019 #include "llvm/Support/DataTypes.h" 00020 00021 namespace llvm { 00022 00023 class PointerType; 00024 class IntegerType; 00025 class raw_ostream; 00026 class Module; 00027 class LLVMContext; 00028 class LLVMContextImpl; 00029 class StringRef; 00030 template<class GraphType> struct GraphTraits; 00031 00032 /// The instances of the Type class are immutable: once they are created, 00033 /// they are never changed. Also note that only one instance of a particular 00034 /// type is ever created. Thus seeing if two types are equal is a matter of 00035 /// doing a trivial pointer comparison. To enforce that no two equal instances 00036 /// are created, Type instances can only be created via static factory methods 00037 /// in class Type and in derived classes. Once allocated, Types are never 00038 /// free'd. 00039 /// 00040 class Type { 00041 public: 00042 //===--------------------------------------------------------------------===// 00043 /// Definitions of all of the base types for the Type system. Based on this 00044 /// value, you can cast to a class defined in DerivedTypes.h. 00045 /// Note: If you add an element to this, you need to add an element to the 00046 /// Type::getPrimitiveType function, or else things will break! 00047 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. 00048 /// 00049 enum TypeID { 00050 // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date. 00051 VoidTyID = 0, ///< 0: type with no size 00052 HalfTyID, ///< 1: 16-bit floating point type 00053 FloatTyID, ///< 2: 32-bit floating point type 00054 DoubleTyID, ///< 3: 64-bit floating point type 00055 X86_FP80TyID, ///< 4: 80-bit floating point type (X87) 00056 FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa) 00057 PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC) 00058 LabelTyID, ///< 7: Labels 00059 MetadataTyID, ///< 8: Metadata 00060 X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific) 00061 00062 // Derived types... see DerivedTypes.h file. 00063 // Make sure FirstDerivedTyID stays up to date! 00064 IntegerTyID, ///< 10: Arbitrary bit width integers 00065 FunctionTyID, ///< 11: Functions 00066 StructTyID, ///< 12: Structures 00067 ArrayTyID, ///< 13: Arrays 00068 PointerTyID, ///< 14: Pointers 00069 VectorTyID, ///< 15: SIMD 'packed' format, or other vector type 00070 00071 NumTypeIDs, // Must remain as last defined ID 00072 LastPrimitiveTyID = X86_MMXTyID, 00073 FirstDerivedTyID = IntegerTyID 00074 }; 00075 00076 private: 00077 /// Context - This refers to the LLVMContext in which this type was uniqued. 00078 LLVMContext &Context; 00079 00080 // Due to Ubuntu GCC bug 910363: 00081 // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363 00082 // Bitpack ID and SubclassData manually. 00083 // Note: TypeID : low 8 bit; SubclassData : high 24 bit. 00084 uint32_t IDAndSubclassData; 00085 00086 protected: 00087 friend class LLVMContextImpl; 00088 explicit Type(LLVMContext &C, TypeID tid) 00089 : Context(C), IDAndSubclassData(0), 00090 NumContainedTys(0), ContainedTys(0) { 00091 setTypeID(tid); 00092 } 00093 ~Type() {} 00094 00095 void setTypeID(TypeID ID) { 00096 IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00); 00097 assert(getTypeID() == ID && "TypeID data too large for field"); 00098 } 00099 00100 unsigned getSubclassData() const { return IDAndSubclassData >> 8; } 00101 00102 void setSubclassData(unsigned val) { 00103 IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8); 00104 // Ensure we don't have any accidental truncation. 00105 assert(getSubclassData() == val && "Subclass data too large for field"); 00106 } 00107 00108 /// NumContainedTys - Keeps track of how many Type*'s there are in the 00109 /// ContainedTys list. 00110 unsigned NumContainedTys; 00111 00112 /// ContainedTys - A pointer to the array of Types contained by this Type. 00113 /// For example, this includes the arguments of a function type, the elements 00114 /// of a structure, the pointee of a pointer, the element type of an array, 00115 /// etc. This pointer may be 0 for types that don't contain other types 00116 /// (Integer, Double, Float). 00117 Type * const *ContainedTys; 00118 00119 public: 00120 void print(raw_ostream &O) const; 00121 void dump() const; 00122 00123 /// getContext - Return the LLVMContext in which this type was uniqued. 00124 LLVMContext &getContext() const { return Context; } 00125 00126 //===--------------------------------------------------------------------===// 00127 // Accessors for working with types. 00128 // 00129 00130 /// getTypeID - Return the type id for the type. This will return one 00131 /// of the TypeID enum elements defined above. 00132 /// 00133 TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); } 00134 00135 /// isVoidTy - Return true if this is 'void'. 00136 bool isVoidTy() const { return getTypeID() == VoidTyID; } 00137 00138 /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type. 00139 bool isHalfTy() const { return getTypeID() == HalfTyID; } 00140 00141 /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type. 00142 bool isFloatTy() const { return getTypeID() == FloatTyID; } 00143 00144 /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type. 00145 bool isDoubleTy() const { return getTypeID() == DoubleTyID; } 00146 00147 /// isX86_FP80Ty - Return true if this is x86 long double. 00148 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; } 00149 00150 /// isFP128Ty - Return true if this is 'fp128'. 00151 bool isFP128Ty() const { return getTypeID() == FP128TyID; } 00152 00153 /// isPPC_FP128Ty - Return true if this is powerpc long double. 00154 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; } 00155 00156 /// isFloatingPointTy - Return true if this is one of the five floating point 00157 /// types 00158 bool isFloatingPointTy() const { 00159 return getTypeID() == HalfTyID || getTypeID() == FloatTyID || 00160 getTypeID() == DoubleTyID || 00161 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID || 00162 getTypeID() == PPC_FP128TyID; 00163 } 00164 00165 /// isX86_MMXTy - Return true if this is X86 MMX. 00166 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; } 00167 00168 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP. 00169 /// 00170 bool isFPOrFPVectorTy() const; 00171 00172 /// isLabelTy - Return true if this is 'label'. 00173 bool isLabelTy() const { return getTypeID() == LabelTyID; } 00174 00175 /// isMetadataTy - Return true if this is 'metadata'. 00176 bool isMetadataTy() const { return getTypeID() == MetadataTyID; } 00177 00178 /// isIntegerTy - True if this is an instance of IntegerType. 00179 /// 00180 bool isIntegerTy() const { return getTypeID() == IntegerTyID; } 00181 00182 /// isIntegerTy - Return true if this is an IntegerType of the given width. 00183 bool isIntegerTy(unsigned Bitwidth) const; 00184 00185 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of 00186 /// integer types. 00187 /// 00188 bool isIntOrIntVectorTy() const; 00189 00190 /// isFunctionTy - True if this is an instance of FunctionType. 00191 /// 00192 bool isFunctionTy() const { return getTypeID() == FunctionTyID; } 00193 00194 /// isStructTy - True if this is an instance of StructType. 00195 /// 00196 bool isStructTy() const { return getTypeID() == StructTyID; } 00197 00198 /// isArrayTy - True if this is an instance of ArrayType. 00199 /// 00200 bool isArrayTy() const { return getTypeID() == ArrayTyID; } 00201 00202 /// isPointerTy - True if this is an instance of PointerType. 00203 /// 00204 bool isPointerTy() const { return getTypeID() == PointerTyID; } 00205 00206 /// isVectorTy - True if this is an instance of VectorType. 00207 /// 00208 bool isVectorTy() const { return getTypeID() == VectorTyID; } 00209 00210 /// canLosslesslyBitCastTo - Return true if this type could be converted 00211 /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts 00212 /// are valid for types of the same size only where no re-interpretation of 00213 /// the bits is done. 00214 /// @brief Determine if this type could be losslessly bitcast to Ty 00215 bool canLosslesslyBitCastTo(Type *Ty) const; 00216 00217 /// isEmptyTy - Return true if this type is empty, that is, it has no 00218 /// elements or all its elements are empty. 00219 bool isEmptyTy() const; 00220 00221 /// Here are some useful little methods to query what type derived types are 00222 /// Note that all other types can just compare to see if this == Type::xxxTy; 00223 /// 00224 bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; } 00225 bool isDerivedType() const { return getTypeID() >= FirstDerivedTyID; } 00226 00227 /// isFirstClassType - Return true if the type is "first class", meaning it 00228 /// is a valid type for a Value. 00229 /// 00230 bool isFirstClassType() const { 00231 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID; 00232 } 00233 00234 /// isSingleValueType - Return true if the type is a valid type for a 00235 /// register in codegen. This includes all first-class types except struct 00236 /// and array types. 00237 /// 00238 bool isSingleValueType() const { 00239 return (getTypeID() != VoidTyID && isPrimitiveType()) || 00240 getTypeID() == IntegerTyID || getTypeID() == PointerTyID || 00241 getTypeID() == VectorTyID; 00242 } 00243 00244 /// isAggregateType - Return true if the type is an aggregate type. This 00245 /// means it is valid as the first operand of an insertvalue or 00246 /// extractvalue instruction. This includes struct and array types, but 00247 /// does not include vector types. 00248 /// 00249 bool isAggregateType() const { 00250 return getTypeID() == StructTyID || getTypeID() == ArrayTyID; 00251 } 00252 00253 /// isSized - Return true if it makes sense to take the size of this type. To 00254 /// get the actual size for a particular target, it is reasonable to use the 00255 /// TargetData subsystem to do this. 00256 /// 00257 bool isSized() const { 00258 // If it's a primitive, it is always sized. 00259 if (getTypeID() == IntegerTyID || isFloatingPointTy() || 00260 getTypeID() == PointerTyID || 00261 getTypeID() == X86_MMXTyID) 00262 return true; 00263 // If it is not something that can have a size (e.g. a function or label), 00264 // it doesn't have a size. 00265 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && 00266 getTypeID() != VectorTyID) 00267 return false; 00268 // Otherwise we have to try harder to decide. 00269 return isSizedDerivedType(); 00270 } 00271 00272 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a 00273 /// primitive type. These are fixed by LLVM and are not target dependent. 00274 /// This will return zero if the type does not have a size or is not a 00275 /// primitive type. 00276 /// 00277 /// Note that this may not reflect the size of memory allocated for an 00278 /// instance of the type or the number of bytes that are written when an 00279 /// instance of the type is stored to memory. The TargetData class provides 00280 /// additional query functions to provide this information. 00281 /// 00282 unsigned getPrimitiveSizeInBits() const; 00283 00284 /// getScalarSizeInBits - If this is a vector type, return the 00285 /// getPrimitiveSizeInBits value for the element type. Otherwise return the 00286 /// getPrimitiveSizeInBits value for this type. 00287 unsigned getScalarSizeInBits(); 00288 00289 /// getFPMantissaWidth - Return the width of the mantissa of this type. This 00290 /// is only valid on floating point types. If the FP type does not 00291 /// have a stable mantissa (e.g. ppc long double), this method returns -1. 00292 int getFPMantissaWidth() const; 00293 00294 /// getScalarType - If this is a vector type, return the element type, 00295 /// otherwise return 'this'. 00296 Type *getScalarType(); 00297 00298 //===--------------------------------------------------------------------===// 00299 // Type Iteration support. 00300 // 00301 typedef Type * const *subtype_iterator; 00302 subtype_iterator subtype_begin() const { return ContainedTys; } 00303 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} 00304 00305 /// getContainedType - This method is used to implement the type iterator 00306 /// (defined a the end of the file). For derived types, this returns the 00307 /// types 'contained' in the derived type. 00308 /// 00309 Type *getContainedType(unsigned i) const { 00310 assert(i < NumContainedTys && "Index out of range!"); 00311 return ContainedTys[i]; 00312 } 00313 00314 /// getNumContainedTypes - Return the number of types in the derived type. 00315 /// 00316 unsigned getNumContainedTypes() const { return NumContainedTys; } 00317 00318 //===--------------------------------------------------------------------===// 00319 // Helper methods corresponding to subclass methods. This forces a cast to 00320 // the specified subclass and calls its accessor. "getVectorNumElements" (for 00321 // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is 00322 // only intended to cover the core methods that are frequently used, helper 00323 // methods should not be added here. 00324 00325 unsigned getIntegerBitWidth() const; 00326 00327 Type *getFunctionParamType(unsigned i) const; 00328 unsigned getFunctionNumParams() const; 00329 bool isFunctionVarArg() const; 00330 00331 StringRef getStructName() const; 00332 unsigned getStructNumElements() const; 00333 Type *getStructElementType(unsigned N) const; 00334 00335 Type *getSequentialElementType() const; 00336 00337 uint64_t getArrayNumElements() const; 00338 Type *getArrayElementType() const { return getSequentialElementType(); } 00339 00340 unsigned getVectorNumElements() const; 00341 Type *getVectorElementType() const { return getSequentialElementType(); } 00342 00343 unsigned getPointerAddressSpace() const; 00344 Type *getPointerElementType() const { return getSequentialElementType(); } 00345 00346 //===--------------------------------------------------------------------===// 00347 // Static members exported by the Type class itself. Useful for getting 00348 // instances of Type. 00349 // 00350 00351 /// getPrimitiveType - Return a type based on an identifier. 00352 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); 00353 00354 //===--------------------------------------------------------------------===// 00355 // These are the builtin types that are always available. 00356 // 00357 static Type *getVoidTy(LLVMContext &C); 00358 static Type *getLabelTy(LLVMContext &C); 00359 static Type *getHalfTy(LLVMContext &C); 00360 static Type *getFloatTy(LLVMContext &C); 00361 static Type *getDoubleTy(LLVMContext &C); 00362 static Type *getMetadataTy(LLVMContext &C); 00363 static Type *getX86_FP80Ty(LLVMContext &C); 00364 static Type *getFP128Ty(LLVMContext &C); 00365 static Type *getPPC_FP128Ty(LLVMContext &C); 00366 static Type *getX86_MMXTy(LLVMContext &C); 00367 static IntegerType *getIntNTy(LLVMContext &C, unsigned N); 00368 static IntegerType *getInt1Ty(LLVMContext &C); 00369 static IntegerType *getInt8Ty(LLVMContext &C); 00370 static IntegerType *getInt16Ty(LLVMContext &C); 00371 static IntegerType *getInt32Ty(LLVMContext &C); 00372 static IntegerType *getInt64Ty(LLVMContext &C); 00373 00374 //===--------------------------------------------------------------------===// 00375 // Convenience methods for getting pointer types with one of the above builtin 00376 // types as pointee. 00377 // 00378 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); 00379 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); 00380 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); 00381 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); 00382 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); 00383 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); 00384 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); 00385 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); 00386 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); 00387 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); 00388 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); 00389 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); 00390 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); 00391 00392 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00393 static inline bool classof(const Type *) { return true; } 00394 00395 /// getPointerTo - Return a pointer to the current type. This is equivalent 00396 /// to PointerType::get(Foo, AddrSpace). 00397 PointerType *getPointerTo(unsigned AddrSpace = 0); 00398 00399 private: 00400 /// isSizedDerivedType - Derived types like structures and arrays are sized 00401 /// iff all of the members of the type are sized as well. Since asking for 00402 /// their size is relatively uncommon, move this operation out of line. 00403 bool isSizedDerivedType() const; 00404 }; 00405 00406 // Printing of types. 00407 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) { 00408 T.print(OS); 00409 return OS; 00410 } 00411 00412 // allow isa<PointerType>(x) to work without DerivedTypes.h included. 00413 template <> struct isa_impl<PointerType, Type> { 00414 static inline bool doit(const Type &Ty) { 00415 return Ty.getTypeID() == Type::PointerTyID; 00416 } 00417 }; 00418 00419 00420 //===----------------------------------------------------------------------===// 00421 // Provide specializations of GraphTraits to be able to treat a type as a 00422 // graph of sub types. 00423 00424 00425 template <> struct GraphTraits<Type*> { 00426 typedef Type NodeType; 00427 typedef Type::subtype_iterator ChildIteratorType; 00428 00429 static inline NodeType *getEntryNode(Type *T) { return T; } 00430 static inline ChildIteratorType child_begin(NodeType *N) { 00431 return N->subtype_begin(); 00432 } 00433 static inline ChildIteratorType child_end(NodeType *N) { 00434 return N->subtype_end(); 00435 } 00436 }; 00437 00438 template <> struct GraphTraits<const Type*> { 00439 typedef const Type NodeType; 00440 typedef Type::subtype_iterator ChildIteratorType; 00441 00442 static inline NodeType *getEntryNode(NodeType *T) { return T; } 00443 static inline ChildIteratorType child_begin(NodeType *N) { 00444 return N->subtype_begin(); 00445 } 00446 static inline ChildIteratorType child_end(NodeType *N) { 00447 return N->subtype_end(); 00448 } 00449 }; 00450 00451 } // End llvm namespace 00452 00453 #endif