LLVM API Documentation

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