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