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