Type.h revision f8db8a04328f11fada2cf81f4b8b2431b3fb1548
1//===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains the declaration of the Type class. For more "Type" type 11// stuff, look in DerivedTypes.h. 12// 13// Note that instances of the Type class are immutable: once they are created, 14// they are never changed. Also note that only one instance of a particular 15// type is ever created. Thus seeing if two types are equal is a matter of 16// doing a trivial pointer comparison. 17// 18// Types, once allocated, are never free'd, unless they are an abstract type 19// that is resolved to a more concrete type. 20// 21// Opaque types are simple derived types with no state. There may be many 22// different Opaque type objects floating around, but two are only considered 23// identical if they are pointer equals of each other. This allows us to have 24// two opaque types that end up resolving to different concrete types later. 25// 26// Opaque types are also kinda weird and scary and different because they have 27// to keep a list of uses of the type. When, through linking, parsing, or 28// bytecode reading, they become resolved, they need to find and update all 29// users of the unknown type, causing them to reference a new, more concrete 30// type. Opaque types are deleted when their use list dwindles to zero users. 31// 32//===----------------------------------------------------------------------===// 33 34#ifndef LLVM_TYPE_H 35#define LLVM_TYPE_H 36 37#include "AbstractTypeUser.h" 38#include "llvm/Support/Casting.h" 39#include "llvm/ADT/GraphTraits.h" 40#include "llvm/ADT/iterator" 41#include <vector> 42 43namespace llvm { 44 45class ArrayType; 46class DerivedType; 47class FunctionType; 48class OpaqueType; 49class PointerType; 50class StructType; 51class PackedType; 52 53class Type { 54public: 55 ///===-------------------------------------------------------------------===// 56 /// Definitions of all of the base types for the Type system. Based on this 57 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h) 58 /// Note: If you add an element to this, you need to add an element to the 59 /// Type::getPrimitiveType function, or else things will break! 60 /// 61 enum TypeID { 62 // PrimitiveTypes .. make sure LastPrimitiveTyID stays up to date 63 VoidTyID = 0 , BoolTyID, // 0, 1: Basics... 64 UByteTyID , SByteTyID, // 2, 3: 8 bit types... 65 UShortTyID , ShortTyID, // 4, 5: 16 bit types... 66 UIntTyID , IntTyID, // 6, 7: 32 bit types... 67 ULongTyID , LongTyID, // 8, 9: 64 bit types... 68 FloatTyID , DoubleTyID, // 10,11: Floating point types... 69 LabelTyID , // 12 : Labels... 70 71 // Derived types... see DerivedTypes.h file... 72 // Make sure FirstDerivedTyID stays up to date!!! 73 FunctionTyID , StructTyID, // Functions... Structs... 74 ArrayTyID , PointerTyID, // Array... pointer... 75 OpaqueTyID, // Opaque type instances... 76 PackedTyID, // SIMD 'packed' format... 77 //... 78 79 NumTypeIDs, // Must remain as last defined ID 80 LastPrimitiveTyID = LabelTyID, 81 FirstDerivedTyID = FunctionTyID, 82 }; 83 84private: 85 TypeID ID : 8; // The current base type of this type. 86 bool Abstract; // True if type contains an OpaqueType 87 88 /// RefCount - This counts the number of PATypeHolders that are pointing to 89 /// this type. When this number falls to zero, if the type is abstract and 90 /// has no AbstractTypeUsers, the type is deleted. This is only sensical for 91 /// derived types. 92 /// 93 mutable unsigned RefCount; 94 95 const Type *getForwardedTypeInternal() const; 96protected: 97 Type(const std::string& Name, TypeID id); 98 virtual ~Type() {} 99 100 /// Types can become nonabstract later, if they are refined. 101 /// 102 inline void setAbstract(bool Val) { Abstract = Val; } 103 104 // PromoteAbstractToConcrete - This is an internal method used to calculate 105 // change "Abstract" from true to false when types are refined. 106 void PromoteAbstractToConcrete(); 107 108 unsigned getRefCount() const { return RefCount; } 109 110 /// ForwardType - This field is used to implement the union find scheme for 111 /// abstract types. When types are refined to other types, this field is set 112 /// to the more refined type. Only abstract types can be forwarded. 113 mutable const Type *ForwardType; 114 115 /// ContainedTys - The list of types contained by this one. For example, this 116 /// includes the arguments of a function type, the elements of the structure, 117 /// the pointee of a pointer, etc. Note that keeping this vector in the Type 118 /// class wastes some space for types that do not contain anything (such as 119 /// primitive types). However, keeping it here allows the subtype_* members 120 /// to be implemented MUCH more efficiently, and dynamically very few types do 121 /// not contain any elements (most are derived). 122 std::vector<PATypeHandle> ContainedTys; 123 124public: 125 virtual void print(std::ostream &O) const; 126 127 /// @brief Debugging support: print to stderr 128 virtual void dump() const; 129 130 //===--------------------------------------------------------------------===// 131 // Property accessors for dealing with types... Some of these virtual methods 132 // are defined in private classes defined in Type.cpp for primitive types. 133 // 134 135 /// getTypeID - Return the type id for the type. This will return one 136 /// of the TypeID enum elements defined above. 137 /// 138 inline TypeID getTypeID() const { return ID; } 139 140 /// getDescription - Return the string representation of the type... 141 const std::string &getDescription() const; 142 143 /// isSigned - Return whether an integral numeric type is signed. This is 144 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for 145 /// Float and Double. 146 /// 147 bool isSigned() const { 148 return ID == SByteTyID || ID == ShortTyID || 149 ID == IntTyID || ID == LongTyID; 150 } 151 152 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite 153 /// the complement of isSigned... nonnumeric types return false as they do 154 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and 155 /// ULongTy 156 /// 157 bool isUnsigned() const { 158 return ID == UByteTyID || ID == UShortTyID || 159 ID == UIntTyID || ID == ULongTyID; 160 } 161 162 /// isInteger - Equivalent to isSigned() || isUnsigned() 163 /// 164 bool isInteger() const { return ID >= UByteTyID && ID <= LongTyID; } 165 166 /// isIntegral - Returns true if this is an integral type, which is either 167 /// BoolTy or one of the Integer types. 168 /// 169 bool isIntegral() const { return isInteger() || this == BoolTy; } 170 171 /// isFloatingPoint - Return true if this is one of the two floating point 172 /// types 173 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; } 174 175 /// isAbstract - True if the type is either an Opaque type, or is a derived 176 /// type that includes an opaque type somewhere in it. 177 /// 178 inline bool isAbstract() const { return Abstract; } 179 180 /// isLosslesslyConvertibleTo - Return true if this type can be converted to 181 /// 'Ty' without any reinterpretation of bits. For example, uint to int. 182 /// 183 bool isLosslesslyConvertibleTo(const Type *Ty) const; 184 185 186 /// Here are some useful little methods to query what type derived types are 187 /// Note that all other types can just compare to see if this == Type::xxxTy; 188 /// 189 inline bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; } 190 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; } 191 192 /// isFirstClassType - Return true if the value is holdable in a register. 193 /// 194 inline bool isFirstClassType() const { 195 return (ID != VoidTyID && ID <= LastPrimitiveTyID) || 196 ID == PointerTyID || ID == PackedTyID; 197 } 198 199 /// isSized - Return true if it makes sense to take the size of this type. To 200 /// get the actual size for a particular target, it is reasonable to use the 201 /// TargetData subsystem to do this. 202 /// 203 bool isSized() const { 204 // If it's a primitive, it is always sized. 205 if (ID >= BoolTyID && ID <= DoubleTyID || ID == PointerTyID) 206 return true; 207 // If it is not something that can have a size (e.g. a function or label), 208 // it doesn't have a size. 209 if (ID != StructTyID && ID != ArrayTyID && ID != PackedTyID) 210 return false; 211 // If it is something that can have a size and it's concrete, it definitely 212 // has a size, otherwise we have to try harder to decide. 213 return !isAbstract() || isSizedDerivedType(); 214 } 215 216 /// getPrimitiveSize - Return the basic size of this type if it is a primitive 217 /// type. These are fixed by LLVM and are not target dependent. This will 218 /// return zero if the type does not have a size or is not a primitive type. 219 /// 220 unsigned getPrimitiveSize() const; 221 222 /// getUnsignedVersion - If this is an integer type, return the unsigned 223 /// variant of this type. For example int -> uint. 224 const Type *getUnsignedVersion() const; 225 226 /// getSignedVersion - If this is an integer type, return the signed variant 227 /// of this type. For example uint -> int. 228 const Type *getSignedVersion() const; 229 230 /// getForwaredType - Return the type that this type has been resolved to if 231 /// it has been resolved to anything. This is used to implement the 232 /// union-find algorithm for type resolution, and shouldn't be used by general 233 /// purpose clients. 234 const Type *getForwardedType() const { 235 if (!ForwardType) return 0; 236 return getForwardedTypeInternal(); 237 } 238 239 //===--------------------------------------------------------------------===// 240 // Type Iteration support 241 // 242 typedef std::vector<PATypeHandle>::const_iterator subtype_iterator; 243 subtype_iterator subtype_begin() const { return ContainedTys.begin(); } 244 subtype_iterator subtype_end() const { return ContainedTys.end(); } 245 246 /// getContainedType - This method is used to implement the type iterator 247 /// (defined a the end of the file). For derived types, this returns the 248 /// types 'contained' in the derived type. 249 /// 250 const Type *getContainedType(unsigned i) const { 251 assert(i < ContainedTys.size() && "Index out of range!"); 252 return ContainedTys[i]; 253 } 254 255 /// getNumContainedTypes - Return the number of types in the derived type. 256 /// 257 typedef std::vector<PATypeHandle>::size_type size_type; 258 size_type getNumContainedTypes() const { return ContainedTys.size(); } 259 260 //===--------------------------------------------------------------------===// 261 // Static members exported by the Type class itself. Useful for getting 262 // instances of Type. 263 // 264 265 /// getPrimitiveType - Return a type based on an identifier. 266 static const Type *getPrimitiveType(TypeID IDNumber); 267 268 //===--------------------------------------------------------------------===// 269 // These are the builtin types that are always available... 270 // 271 static Type *VoidTy , *BoolTy; 272 static Type *SByteTy, *UByteTy, 273 *ShortTy, *UShortTy, 274 *IntTy , *UIntTy, 275 *LongTy , *ULongTy; 276 static Type *FloatTy, *DoubleTy; 277 278 static Type* LabelTy; 279 280 /// Methods for support type inquiry through isa, cast, and dyn_cast: 281 static inline bool classof(const Type *T) { return true; } 282 283#include "llvm/Type.def" 284 285 // Virtual methods used by callbacks below. These should only be implemented 286 // in the DerivedType class. 287 virtual void addAbstractTypeUser(AbstractTypeUser *U) const { 288 abort(); // Only on derived types! 289 } 290 virtual void removeAbstractTypeUser(AbstractTypeUser *U) const { 291 abort(); // Only on derived types! 292 } 293 294 void addRef() const { 295 assert(isAbstract() && "Cannot add a reference to a non-abstract type!"); 296 ++RefCount; 297 } 298 299 void dropRef() const { 300 assert(isAbstract() && "Cannot drop a reference to a non-abstract type!"); 301 assert(RefCount && "No objects are currently referencing this object!"); 302 303 // If this is the last PATypeHolder using this object, and there are no 304 // PATypeHandles using it, the type is dead, delete it now. 305 if (--RefCount == 0) 306 RefCountIsZero(); 307 } 308 309 /// clearAllTypeMaps - This method frees all internal memory used by the 310 /// type subsystem, which can be used in environments where this memory is 311 /// otherwise reported as a leak. 312 static void clearAllTypeMaps(); 313 314private: 315 /// isSizedDerivedType - Derived types like structures and arrays are sized 316 /// iff all of the members of the type are sized as well. Since asking for 317 /// their size is relatively uncommon, move this operation out of line. 318 bool isSizedDerivedType() const; 319 320 virtual void RefCountIsZero() const { 321 abort(); // only on derived types! 322 } 323 324}; 325 326//===----------------------------------------------------------------------===// 327// Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. 328// These are defined here because they MUST be inlined, yet are dependent on 329// the definition of the Type class. Of course Type derives from Value, which 330// contains an AbstractTypeUser instance, so there is no good way to factor out 331// the code. Hence this bit of uglyness. 332// 333// In the long term, Type should not derive from Value, allowing 334// AbstractTypeUser.h to #include Type.h, allowing us to eliminate this 335// nastyness entirely. 336// 337inline void PATypeHandle::addUser() { 338 assert(Ty && "Type Handle has a null type!"); 339 if (Ty->isAbstract()) 340 Ty->addAbstractTypeUser(User); 341} 342inline void PATypeHandle::removeUser() { 343 if (Ty->isAbstract()) 344 Ty->removeAbstractTypeUser(User); 345} 346 347inline void PATypeHandle::removeUserFromConcrete() { 348 if (!Ty->isAbstract()) 349 Ty->removeAbstractTypeUser(User); 350} 351 352// Define inline methods for PATypeHolder... 353 354inline void PATypeHolder::addRef() { 355 if (Ty->isAbstract()) 356 Ty->addRef(); 357} 358 359inline void PATypeHolder::dropRef() { 360 if (Ty->isAbstract()) 361 Ty->dropRef(); 362} 363 364/// get - This implements the forwarding part of the union-find algorithm for 365/// abstract types. Before every access to the Type*, we check to see if the 366/// type we are pointing to is forwarding to a new type. If so, we drop our 367/// reference to the type. 368/// 369inline Type* PATypeHolder::get() const { 370 const Type *NewTy = Ty->getForwardedType(); 371 if (!NewTy) return const_cast<Type*>(Ty); 372 return *const_cast<PATypeHolder*>(this) = NewTy; 373} 374 375 376 377//===----------------------------------------------------------------------===// 378// Provide specializations of GraphTraits to be able to treat a type as a 379// graph of sub types... 380 381template <> struct GraphTraits<Type*> { 382 typedef Type NodeType; 383 typedef Type::subtype_iterator ChildIteratorType; 384 385 static inline NodeType *getEntryNode(Type *T) { return T; } 386 static inline ChildIteratorType child_begin(NodeType *N) { 387 return N->subtype_begin(); 388 } 389 static inline ChildIteratorType child_end(NodeType *N) { 390 return N->subtype_end(); 391 } 392}; 393 394template <> struct GraphTraits<const Type*> { 395 typedef const Type NodeType; 396 typedef Type::subtype_iterator ChildIteratorType; 397 398 static inline NodeType *getEntryNode(const Type *T) { return T; } 399 static inline ChildIteratorType child_begin(NodeType *N) { 400 return N->subtype_begin(); 401 } 402 static inline ChildIteratorType child_end(NodeType *N) { 403 return N->subtype_end(); 404 } 405}; 406 407template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 408 return Ty.getTypeID() == Type::PointerTyID; 409} 410 411std::ostream &operator<<(std::ostream &OS, const Type &T); 412 413} // End llvm namespace 414 415#endif 416