Type.h revision f70c22b019494723d0e706f93d6542dfaa6e73a5
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. 19// 20// Opaque types are simple derived types with no state. There may be many 21// different Opaque type objects floating around, but two are only considered 22// identical if they are pointer equals of each other. This allows us to have 23// two opaque types that end up resolving to different concrete types later. 24// 25// Opaque types are also kinda wierd and scary and different because they have 26// to keep a list of uses of the type. When, through linking, parsing, or 27// bytecode reading, they become resolved, they need to find and update all 28// users of the unknown type, causing them to reference a new, more concrete 29// type. Opaque types are deleted when their use list dwindles to zero users. 30// 31//===----------------------------------------------------------------------===// 32 33#ifndef LLVM_TYPE_H 34#define LLVM_TYPE_H 35 36#include "llvm/Value.h" 37#include "Support/GraphTraits.h" 38#include "Support/iterator" 39#include <vector> 40 41namespace llvm { 42 43class DerivedType; 44class FunctionType; 45class ArrayType; 46class PointerType; 47class StructType; 48class OpaqueType; 49 50struct Type : public Value { 51 ///===-------------------------------------------------------------------===// 52 /// Definitions of all of the base types for the Type system. Based on this 53 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h) 54 /// Note: If you add an element to this, you need to add an element to the 55 /// Type::getPrimitiveType function, or else things will break! 56 /// 57 enum TypeID { 58 VoidTyID = 0 , BoolTyID, // 0, 1: Basics... 59 UByteTyID , SByteTyID, // 2, 3: 8 bit types... 60 UShortTyID , ShortTyID, // 4, 5: 16 bit types... 61 UIntTyID , IntTyID, // 6, 7: 32 bit types... 62 ULongTyID , LongTyID, // 8, 9: 64 bit types... 63 64 FloatTyID , DoubleTyID, // 10,11: Floating point types... 65 66 TypeTyID, // 12 : Type definitions 67 LabelTyID , // 13 : Labels... 68 69 // Derived types... see DerivedTypes.h file... 70 // Make sure FirstDerivedTyID stays up to date!!! 71 FunctionTyID , StructTyID, // Functions... Structs... 72 ArrayTyID , PointerTyID, // Array... pointer... 73 OpaqueTyID, // Opaque type instances... 74 //PackedTyID , // SIMD 'packed' format... TODO 75 //... 76 77 NumTypeIDs, // Must remain as last defined ID 78 FirstDerivedTyID = FunctionTyID, 79 }; 80 81private: 82 TypeID ID; // The current base type of this type... 83 unsigned UID; // The unique ID number for this class 84 bool Abstract; // True if type contains an OpaqueType 85 86 /// RefCount - This counts the number of PATypeHolders that are pointing to 87 /// this type. When this number falls to zero, if the type is abstract and 88 /// has no AbstractTypeUsers, the type is deleted. This is only sensical for 89 /// derived types. 90 /// 91 mutable unsigned RefCount; 92 93 const Type *getForwardedTypeInternal() const; 94protected: 95 /// ctor is protected, so only subclasses can create Type objects... 96 Type(const std::string &Name, TypeID id); 97 virtual ~Type() {} 98 99 100 /// Types can become nonabstract later, if they are refined. 101 /// 102 inline void setAbstract(bool Val) { Abstract = Val; } 103 104 /// isTypeAbstract - This method is used to calculate the Abstract bit. 105 /// 106 bool isTypeAbstract(); 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 /// setName - Associate the name with this type in the symbol table, but don't 131 /// set the local name to be equal specified name. 132 /// 133 virtual void setName(const std::string &Name, SymbolTable *ST = 0); 134 135 //===--------------------------------------------------------------------===// 136 // Property accessors for dealing with types... Some of these virtual methods 137 // are defined in private classes defined in Type.cpp for primitive types. 138 // 139 140 /// getTypeID - Return the type id for the type. This will return one 141 /// of the TypeID enum elements defined above. 142 /// 143 inline TypeID getTypeID() const { return ID; } 144 145 /// getUniqueID - Returns the UID of the type. This can be thought of as a 146 /// small integer version of the pointer to the type class. Two types that 147 /// are structurally different have different UIDs. This can be used for 148 /// indexing types into an array. 149 /// 150 inline unsigned getUniqueID() const { return UID; } 151 152 /// getDescription - Return the string representation of the type... 153 const std::string &getDescription() const; 154 155 /// isSigned - Return whether an integral numeric type is signed. This is 156 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for 157 /// Float and Double. 158 /// 159 virtual bool isSigned() const { return 0; } 160 161 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite 162 /// the complement of isSigned... nonnumeric types return false as they do 163 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and 164 /// ULongTy 165 /// 166 virtual bool isUnsigned() const { return 0; } 167 168 /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a 169 /// single virtual function invocation. 170 /// 171 virtual bool isInteger() const { return 0; } 172 173 /// isIntegral - Returns true if this is an integral type, which is either 174 /// BoolTy or one of the Integer types. 175 /// 176 bool isIntegral() const { return isInteger() || this == BoolTy; } 177 178 /// isFloatingPoint - Return true if this is one of the two floating point 179 /// types 180 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; } 181 182 /// isAbstract - True if the type is either an Opaque type, or is a derived 183 /// type that includes an opaque type somewhere in it. 184 /// 185 inline bool isAbstract() const { return Abstract; } 186 187 /// isLosslesslyConvertibleTo - Return true if this type can be converted to 188 /// 'Ty' without any reinterpretation of bits. For example, uint to int. 189 /// 190 bool isLosslesslyConvertibleTo(const Type *Ty) const; 191 192 193 /// Here are some useful little methods to query what type derived types are 194 /// Note that all other types can just compare to see if this == Type::xxxTy; 195 /// 196 inline bool isPrimitiveType() const { return ID < FirstDerivedTyID; } 197 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; } 198 199 /// isFirstClassType - Return true if the value is holdable in a register. 200 inline bool isFirstClassType() const { 201 return (ID != VoidTyID && ID < TypeTyID) || ID == PointerTyID; 202 } 203 204 /// isSized - Return true if it makes sense to take the size of this type. To 205 /// get the actual size for a particular target, it is reasonable to use the 206 /// TargetData subsystem to do this. 207 /// 208 bool isSized() const { 209 return ID != VoidTyID && ID != TypeTyID && 210 ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID; 211 } 212 213 /// getPrimitiveSize - Return the basic size of this type if it is a primative 214 /// type. These are fixed by LLVM and are not target dependent. This will 215 /// return zero if the type does not have a size or is not a primitive type. 216 /// 217 unsigned getPrimitiveSize() const; 218 219 /// getUnsignedVersion - If this is an integer type, return the unsigned 220 /// variant of this type. For example int -> uint. 221 const Type *getUnsignedVersion() const; 222 223 /// getSignedVersion - If this is an integer type, return the signed variant 224 /// of this type. For example uint -> int. 225 const Type *getSignedVersion() const; 226 227 /// getForwaredType - Return the type that this type has been resolved to if 228 /// it has been resolved to anything. This is used to implement the 229 /// union-find algorithm for type resolution, and shouldn't be used by general 230 /// purpose clients. 231 const Type *getForwardedType() const { 232 if (!ForwardType) return 0; 233 return getForwardedTypeInternal(); 234 } 235 236 //===--------------------------------------------------------------------===// 237 // Type Iteration support 238 // 239 typedef std::vector<PATypeHandle>::const_iterator subtype_iterator; 240 subtype_iterator subtype_begin() const { return ContainedTys.begin(); } 241 subtype_iterator subtype_end() const { return ContainedTys.end(); } 242 243 /// getContainedType - This method is used to implement the type iterator 244 /// (defined a the end of the file). For derived types, this returns the 245 /// types 'contained' in the derived type. 246 /// 247 const Type *getContainedType(unsigned i) const { 248 assert(i < ContainedTys.size() && "Index out of range!"); 249 return ContainedTys[i]; 250 } 251 252 /// getNumContainedTypes - Return the number of types in the derived type. 253 /// 254 unsigned getNumContainedTypes() const { return ContainedTys.size(); } 255 256 //===--------------------------------------------------------------------===// 257 // Static members exported by the Type class itself. Useful for getting 258 // instances of Type. 259 // 260 261 /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier. 262 static const Type *getPrimitiveType(TypeID IDNumber); 263 static const Type *getUniqueIDType(unsigned UID); 264 265 //===--------------------------------------------------------------------===// 266 // These are the builtin types that are always available... 267 // 268 static Type *VoidTy , *BoolTy; 269 static Type *SByteTy, *UByteTy, 270 *ShortTy, *UShortTy, 271 *IntTy , *UIntTy, 272 *LongTy , *ULongTy; 273 static Type *FloatTy, *DoubleTy; 274 275 static Type *TypeTy , *LabelTy; 276 277 /// Methods for support type inquiry through isa, cast, and dyn_cast: 278 static inline bool classof(const Type *T) { return true; } 279 static inline bool classof(const Value *V) { 280 return V->getValueType() == Value::TypeVal; 281 } 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 refernce 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 } 308private: 309 virtual void RefCountIsZero() const { 310 abort(); // only on derived types! 311 } 312 313}; 314 315//===----------------------------------------------------------------------===// 316// Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. 317// These are defined here because they MUST be inlined, yet are dependent on 318// the definition of the Type class. Of course Type derives from Value, which 319// contains an AbstractTypeUser instance, so there is no good way to factor out 320// the code. Hence this bit of uglyness. 321// 322// In the long term, Type should not derive from Value, allowing 323// AbstractTypeUser.h to #include Type.h, allowing us to eliminate this 324// nastyness entirely. 325// 326inline void PATypeHandle::addUser() { 327 assert(Ty && "Type Handle has a null type!"); 328 if (Ty->isAbstract()) 329 Ty->addAbstractTypeUser(User); 330} 331inline void PATypeHandle::removeUser() { 332 if (Ty->isAbstract()) 333 Ty->removeAbstractTypeUser(User); 334} 335 336inline void PATypeHandle::removeUserFromConcrete() { 337 if (!Ty->isAbstract()) 338 Ty->removeAbstractTypeUser(User); 339} 340 341// Define inline methods for PATypeHolder... 342 343inline void PATypeHolder::addRef() { 344 if (Ty->isAbstract()) 345 Ty->addRef(); 346} 347 348inline void PATypeHolder::dropRef() { 349 if (Ty->isAbstract()) 350 Ty->dropRef(); 351} 352 353/// get - This implements the forwarding part of the union-find algorithm for 354/// abstract types. Before every access to the Type*, we check to see if the 355/// type we are pointing to is forwarding to a new type. If so, we drop our 356/// reference to the type. 357/// 358inline const Type* PATypeHolder::get() const { 359 const Type *NewTy = Ty->getForwardedType(); 360 if (!NewTy) return Ty; 361 return *const_cast<PATypeHolder*>(this) = NewTy; 362} 363 364 365 366//===----------------------------------------------------------------------===// 367// Provide specializations of GraphTraits to be able to treat a type as a 368// graph of sub types... 369 370template <> struct GraphTraits<Type*> { 371 typedef Type NodeType; 372 typedef Type::subtype_iterator ChildIteratorType; 373 374 static inline NodeType *getEntryNode(Type *T) { return T; } 375 static inline ChildIteratorType child_begin(NodeType *N) { 376 return N->subtype_begin(); 377 } 378 static inline ChildIteratorType child_end(NodeType *N) { 379 return N->subtype_end(); 380 } 381}; 382 383template <> struct GraphTraits<const Type*> { 384 typedef const Type NodeType; 385 typedef Type::subtype_iterator ChildIteratorType; 386 387 static inline NodeType *getEntryNode(const Type *T) { return T; } 388 static inline ChildIteratorType child_begin(NodeType *N) { 389 return N->subtype_begin(); 390 } 391 static inline ChildIteratorType child_end(NodeType *N) { 392 return N->subtype_end(); 393 } 394}; 395 396template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 397 return Ty.getTypeID() == Type::PointerTyID; 398} 399 400} // End llvm namespace 401 402#endif 403