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