LLVM API Documentation
00001 //===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declaration of the Type class. For more "Type" type 00011 // stuff, look in DerivedTypes.h. 00012 // 00013 // Note that instances of the Type class are immutable: once they are created, 00014 // they are never changed. Also note that only one instance of a particular 00015 // type is ever created. Thus seeing if two types are equal is a matter of 00016 // doing a trivial pointer comparison. 00017 // 00018 // Types, once allocated, are never free'd, unless they are an abstract type 00019 // that is resolved to a more concrete type. 00020 // 00021 // Opaque types are simple derived types with no state. There may be many 00022 // different Opaque type objects floating around, but two are only considered 00023 // identical if they are pointer equals of each other. This allows us to have 00024 // two opaque types that end up resolving to different concrete types later. 00025 // 00026 // Opaque types are also kinda weird and scary and different because they have 00027 // to keep a list of uses of the type. When, through linking, parsing, or 00028 // bytecode reading, they become resolved, they need to find and update all 00029 // users of the unknown type, causing them to reference a new, more concrete 00030 // type. Opaque types are deleted when their use list dwindles to zero users. 00031 // 00032 //===----------------------------------------------------------------------===// 00033 00034 #ifndef LLVM_TYPE_H 00035 #define LLVM_TYPE_H 00036 00037 #include "llvm/AbstractTypeUser.h" 00038 #include "llvm/Support/Casting.h" 00039 #include "llvm/Support/DataTypes.h" 00040 #include "llvm/ADT/GraphTraits.h" 00041 #include "llvm/ADT/iterator" 00042 #include <string> 00043 #include <vector> 00044 00045 namespace llvm { 00046 00047 class ArrayType; 00048 class DerivedType; 00049 class FunctionType; 00050 class OpaqueType; 00051 class PointerType; 00052 class StructType; 00053 class PackedType; 00054 class TypeMapBase; 00055 00056 class Type : public AbstractTypeUser { 00057 public: 00058 ///===-------------------------------------------------------------------===// 00059 /// Definitions of all of the base types for the Type system. Based on this 00060 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h) 00061 /// Note: If you add an element to this, you need to add an element to the 00062 /// Type::getPrimitiveType function, or else things will break! 00063 /// 00064 enum TypeID { 00065 // PrimitiveTypes .. make sure LastPrimitiveTyID stays up to date 00066 VoidTyID = 0 , BoolTyID, // 0, 1: Basics... 00067 UByteTyID , SByteTyID, // 2, 3: 8 bit types... 00068 UShortTyID , ShortTyID, // 4, 5: 16 bit types... 00069 UIntTyID , IntTyID, // 6, 7: 32 bit types... 00070 ULongTyID , LongTyID, // 8, 9: 64 bit types... 00071 FloatTyID , DoubleTyID, // 10,11: Floating point types... 00072 LabelTyID , // 12 : Labels... 00073 00074 // Derived types... see DerivedTypes.h file... 00075 // Make sure FirstDerivedTyID stays up to date!!! 00076 FunctionTyID , StructTyID, // Functions... Structs... 00077 ArrayTyID , PointerTyID, // Array... pointer... 00078 OpaqueTyID, // Opaque type instances... 00079 PackedTyID, // SIMD 'packed' format... 00080 //... 00081 00082 NumTypeIDs, // Must remain as last defined ID 00083 LastPrimitiveTyID = LabelTyID, 00084 FirstDerivedTyID = FunctionTyID 00085 }; 00086 00087 private: 00088 TypeID ID : 8; // The current base type of this type. 00089 bool Abstract : 1; // True if type contains an OpaqueType 00090 00091 /// RefCount - This counts the number of PATypeHolders that are pointing to 00092 /// this type. When this number falls to zero, if the type is abstract and 00093 /// has no AbstractTypeUsers, the type is deleted. This is only sensical for 00094 /// derived types. 00095 /// 00096 mutable unsigned RefCount; 00097 00098 const Type *getForwardedTypeInternal() const; 00099 protected: 00100 Type(const char *Name, TypeID id); 00101 Type(TypeID id) : ID(id), Abstract(false), RefCount(0), ForwardType(0) {} 00102 virtual ~Type() { 00103 assert(AbstractTypeUsers.empty()); 00104 } 00105 00106 /// Types can become nonabstract later, if they are refined. 00107 /// 00108 inline void setAbstract(bool Val) { Abstract = Val; } 00109 00110 unsigned getRefCount() const { return RefCount; } 00111 00112 /// ForwardType - This field is used to implement the union find scheme for 00113 /// abstract types. When types are refined to other types, this field is set 00114 /// to the more refined type. Only abstract types can be forwarded. 00115 mutable const Type *ForwardType; 00116 00117 /// ContainedTys - The list of types contained by this one. For example, this 00118 /// includes the arguments of a function type, the elements of the structure, 00119 /// the pointee of a pointer, etc. Note that keeping this vector in the Type 00120 /// class wastes some space for types that do not contain anything (such as 00121 /// primitive types). However, keeping it here allows the subtype_* members 00122 /// to be implemented MUCH more efficiently, and dynamically very few types do 00123 /// not contain any elements (most are derived). 00124 std::vector<PATypeHandle> ContainedTys; 00125 00126 /// AbstractTypeUsers - Implement a list of the users that need to be notified 00127 /// if I am a type, and I get resolved into a more concrete type. 00128 /// 00129 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers; 00130 public: 00131 void print(std::ostream &O) const; 00132 00133 /// @brief Debugging support: print to stderr 00134 void dump() const; 00135 00136 //===--------------------------------------------------------------------===// 00137 // Property accessors for dealing with types... Some of these virtual methods 00138 // are defined in private classes defined in Type.cpp for primitive types. 00139 // 00140 00141 /// getTypeID - Return the type id for the type. This will return one 00142 /// of the TypeID enum elements defined above. 00143 /// 00144 inline TypeID getTypeID() const { return ID; } 00145 00146 /// getDescription - Return the string representation of the type... 00147 const std::string &getDescription() const; 00148 00149 /// isSigned - Return whether an integral numeric type is signed. This is 00150 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for 00151 /// Float and Double. 00152 /// 00153 bool isSigned() const { 00154 return ID == SByteTyID || ID == ShortTyID || 00155 ID == IntTyID || ID == LongTyID; 00156 } 00157 00158 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite 00159 /// the complement of isSigned... nonnumeric types return false as they do 00160 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and 00161 /// ULongTy 00162 /// 00163 bool isUnsigned() const { 00164 return ID == UByteTyID || ID == UShortTyID || 00165 ID == UIntTyID || ID == ULongTyID; 00166 } 00167 00168 /// isInteger - Equivalent to isSigned() || isUnsigned() 00169 /// 00170 bool isInteger() const { return ID >= UByteTyID && ID <= LongTyID; } 00171 00172 /// isIntegral - Returns true if this is an integral type, which is either 00173 /// BoolTy or one of the Integer types. 00174 /// 00175 bool isIntegral() const { return isInteger() || this == BoolTy; } 00176 00177 /// isFloatingPoint - Return true if this is one of the two floating point 00178 /// types 00179 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; } 00180 00181 /// isAbstract - True if the type is either an Opaque type, or is a derived 00182 /// type that includes an opaque type somewhere in it. 00183 /// 00184 inline bool isAbstract() const { return Abstract; } 00185 00186 /// isLosslesslyConvertibleTo - Return true if this type can be converted to 00187 /// 'Ty' without any reinterpretation of bits. For example, uint to int. 00188 /// 00189 bool isLosslesslyConvertibleTo(const Type *Ty) const; 00190 00191 00192 /// Here are some useful little methods to query what type derived types are 00193 /// Note that all other types can just compare to see if this == Type::xxxTy; 00194 /// 00195 inline bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; } 00196 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; } 00197 00198 /// isFirstClassType - Return true if the value is holdable in a register. 00199 /// 00200 inline bool isFirstClassType() const { 00201 return (ID != VoidTyID && ID <= LastPrimitiveTyID) || 00202 ID == PointerTyID || ID == PackedTyID; 00203 } 00204 00205 /// isSized - Return true if it makes sense to take the size of this type. To 00206 /// get the actual size for a particular target, it is reasonable to use the 00207 /// TargetData subsystem to do this. 00208 /// 00209 bool isSized() const { 00210 // If it's a primitive, it is always sized. 00211 if (ID >= BoolTyID && ID <= DoubleTyID || ID == PointerTyID) 00212 return true; 00213 // If it is not something that can have a size (e.g. a function or label), 00214 // it doesn't have a size. 00215 if (ID != StructTyID && ID != ArrayTyID && ID != PackedTyID) 00216 return false; 00217 // If it is something that can have a size and it's concrete, it definitely 00218 // has a size, otherwise we have to try harder to decide. 00219 return !isAbstract() || isSizedDerivedType(); 00220 } 00221 00222 /// getPrimitiveSize - Return the basic size of this type if it is a primitive 00223 /// type. These are fixed by LLVM and are not target dependent. This will 00224 /// return zero if the type does not have a size or is not a primitive type. 00225 /// 00226 unsigned getPrimitiveSize() const; 00227 unsigned getPrimitiveSizeInBits() const; 00228 00229 /// getUnsignedVersion - If this is an integer type, return the unsigned 00230 /// variant of this type. For example int -> uint. 00231 const Type *getUnsignedVersion() const; 00232 00233 /// getSignedVersion - If this is an integer type, return the signed variant 00234 /// of this type. For example uint -> int. 00235 const Type *getSignedVersion() const; 00236 00237 /// getIntegralTypeMask - Return a bitmask with ones set for all of the bits 00238 /// that can be set by an unsigned version of this type. This is 0xFF for 00239 /// sbyte/ubyte, 0xFFFF for shorts, etc. 00240 uint64_t getIntegralTypeMask() const { 00241 assert(isIntegral() && "This only works for integral types!"); 00242 return ~0ULL >> (64-getPrimitiveSizeInBits()); 00243 } 00244 00245 /// getForwaredType - Return the type that this type has been resolved to if 00246 /// it has been resolved to anything. This is used to implement the 00247 /// union-find algorithm for type resolution, and shouldn't be used by general 00248 /// purpose clients. 00249 const Type *getForwardedType() const { 00250 if (!ForwardType) return 0; 00251 return getForwardedTypeInternal(); 00252 } 00253 00254 /// getVAArgsPromotedType - Return the type an argument of this type 00255 /// will be promoted to if passed through a variable argument 00256 /// function. 00257 const Type *getVAArgsPromotedType() const { 00258 if (ID == BoolTyID || ID == UByteTyID || ID == UShortTyID) 00259 return Type::UIntTy; 00260 else if (ID == SByteTyID || ID == ShortTyID) 00261 return Type::IntTy; 00262 else if (ID == FloatTyID) 00263 return Type::DoubleTy; 00264 else 00265 return this; 00266 } 00267 00268 //===--------------------------------------------------------------------===// 00269 // Type Iteration support 00270 // 00271 typedef std::vector<PATypeHandle>::const_iterator subtype_iterator; 00272 subtype_iterator subtype_begin() const { return ContainedTys.begin(); } 00273 subtype_iterator subtype_end() const { return ContainedTys.end(); } 00274 00275 /// getContainedType - This method is used to implement the type iterator 00276 /// (defined a the end of the file). For derived types, this returns the 00277 /// types 'contained' in the derived type. 00278 /// 00279 const Type *getContainedType(unsigned i) const { 00280 assert(i < ContainedTys.size() && "Index out of range!"); 00281 return ContainedTys[i]; 00282 } 00283 00284 /// getNumContainedTypes - Return the number of types in the derived type. 00285 /// 00286 typedef std::vector<PATypeHandle>::size_type size_type; 00287 size_type getNumContainedTypes() const { return ContainedTys.size(); } 00288 00289 //===--------------------------------------------------------------------===// 00290 // Static members exported by the Type class itself. Useful for getting 00291 // instances of Type. 00292 // 00293 00294 /// getPrimitiveType - Return a type based on an identifier. 00295 static const Type *getPrimitiveType(TypeID IDNumber); 00296 00297 //===--------------------------------------------------------------------===// 00298 // These are the builtin types that are always available... 00299 // 00300 static Type *VoidTy , *BoolTy; 00301 static Type *SByteTy, *UByteTy, 00302 *ShortTy, *UShortTy, 00303 *IntTy , *UIntTy, 00304 *LongTy , *ULongTy; 00305 static Type *FloatTy, *DoubleTy; 00306 00307 static Type* LabelTy; 00308 00309 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00310 static inline bool classof(const Type *T) { return true; } 00311 00312 void addRef() const { 00313 assert(isAbstract() && "Cannot add a reference to a non-abstract type!"); 00314 ++RefCount; 00315 } 00316 00317 void dropRef() const { 00318 assert(isAbstract() && "Cannot drop a reference to a non-abstract type!"); 00319 assert(RefCount && "No objects are currently referencing this object!"); 00320 00321 // If this is the last PATypeHolder using this object, and there are no 00322 // PATypeHandles using it, the type is dead, delete it now. 00323 if (--RefCount == 0 && AbstractTypeUsers.empty()) 00324 delete this; 00325 } 00326 00327 /// addAbstractTypeUser - Notify an abstract type that there is a new user of 00328 /// it. This function is called primarily by the PATypeHandle class. 00329 /// 00330 void addAbstractTypeUser(AbstractTypeUser *U) const { 00331 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!"); 00332 AbstractTypeUsers.push_back(U); 00333 } 00334 00335 /// removeAbstractTypeUser - Notify an abstract type that a user of the class 00336 /// no longer has a handle to the type. This function is called primarily by 00337 /// the PATypeHandle class. When there are no users of the abstract type, it 00338 /// is annihilated, because there is no way to get a reference to it ever 00339 /// again. 00340 /// 00341 void removeAbstractTypeUser(AbstractTypeUser *U) const; 00342 00343 /// clearAllTypeMaps - This method frees all internal memory used by the 00344 /// type subsystem, which can be used in environments where this memory is 00345 /// otherwise reported as a leak. 00346 static void clearAllTypeMaps(); 00347 00348 private: 00349 /// isSizedDerivedType - Derived types like structures and arrays are sized 00350 /// iff all of the members of the type are sized as well. Since asking for 00351 /// their size is relatively uncommon, move this operation out of line. 00352 bool isSizedDerivedType() const; 00353 00354 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00355 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00356 00357 protected: 00358 // PromoteAbstractToConcrete - This is an internal method used to calculate 00359 // change "Abstract" from true to false when types are refined. 00360 void PromoteAbstractToConcrete(); 00361 friend class TypeMapBase; 00362 }; 00363 00364 //===----------------------------------------------------------------------===// 00365 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. 00366 // These are defined here because they MUST be inlined, yet are dependent on 00367 // the definition of the Type class. Of course Type derives from Value, which 00368 // contains an AbstractTypeUser instance, so there is no good way to factor out 00369 // the code. Hence this bit of uglyness. 00370 // 00371 // In the long term, Type should not derive from Value, allowing 00372 // AbstractTypeUser.h to #include Type.h, allowing us to eliminate this 00373 // nastyness entirely. 00374 // 00375 inline void PATypeHandle::addUser() { 00376 assert(Ty && "Type Handle has a null type!"); 00377 if (Ty->isAbstract()) 00378 Ty->addAbstractTypeUser(User); 00379 } 00380 inline void PATypeHandle::removeUser() { 00381 if (Ty->isAbstract()) 00382 Ty->removeAbstractTypeUser(User); 00383 } 00384 00385 // Define inline methods for PATypeHolder... 00386 00387 inline void PATypeHolder::addRef() { 00388 if (Ty->isAbstract()) 00389 Ty->addRef(); 00390 } 00391 00392 inline void PATypeHolder::dropRef() { 00393 if (Ty->isAbstract()) 00394 Ty->dropRef(); 00395 } 00396 00397 /// get - This implements the forwarding part of the union-find algorithm for 00398 /// abstract types. Before every access to the Type*, we check to see if the 00399 /// type we are pointing to is forwarding to a new type. If so, we drop our 00400 /// reference to the type. 00401 /// 00402 inline Type* PATypeHolder::get() const { 00403 const Type *NewTy = Ty->getForwardedType(); 00404 if (!NewTy) return const_cast<Type*>(Ty); 00405 return *const_cast<PATypeHolder*>(this) = NewTy; 00406 } 00407 00408 00409 00410 //===----------------------------------------------------------------------===// 00411 // Provide specializations of GraphTraits to be able to treat a type as a 00412 // graph of sub types... 00413 00414 template <> struct GraphTraits<Type*> { 00415 typedef Type NodeType; 00416 typedef Type::subtype_iterator ChildIteratorType; 00417 00418 static inline NodeType *getEntryNode(Type *T) { return T; } 00419 static inline ChildIteratorType child_begin(NodeType *N) { 00420 return N->subtype_begin(); 00421 } 00422 static inline ChildIteratorType child_end(NodeType *N) { 00423 return N->subtype_end(); 00424 } 00425 }; 00426 00427 template <> struct GraphTraits<const Type*> { 00428 typedef const Type NodeType; 00429 typedef Type::subtype_iterator ChildIteratorType; 00430 00431 static inline NodeType *getEntryNode(const Type *T) { return T; } 00432 static inline ChildIteratorType child_begin(NodeType *N) { 00433 return N->subtype_begin(); 00434 } 00435 static inline ChildIteratorType child_end(NodeType *N) { 00436 return N->subtype_end(); 00437 } 00438 }; 00439 00440 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 00441 return Ty.getTypeID() == Type::PointerTyID; 00442 } 00443 00444 std::ostream &operator<<(std::ostream &OS, const Type &T); 00445 00446 } // End llvm namespace 00447 00448 #endif