Home | History | Annotate | Download | only in llvm
      1 //===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains the declaration of the Type class.  For more "Type"
     11 // stuff, look in DerivedTypes.h.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_TYPE_H
     16 #define LLVM_TYPE_H
     17 
     18 #include "llvm/Support/Casting.h"
     19 #include "llvm/Support/DataTypes.h"
     20 
     21 namespace llvm {
     22 
     23 class PointerType;
     24 class IntegerType;
     25 class raw_ostream;
     26 class Module;
     27 class LLVMContext;
     28 class LLVMContextImpl;
     29 class StringRef;
     30 template<class GraphType> struct GraphTraits;
     31 
     32 /// The instances of the Type class are immutable: once they are created,
     33 /// they are never changed.  Also note that only one instance of a particular
     34 /// type is ever created.  Thus seeing if two types are equal is a matter of
     35 /// doing a trivial pointer comparison. To enforce that no two equal instances
     36 /// are created, Type instances can only be created via static factory methods
     37 /// in class Type and in derived classes.  Once allocated, Types are never
     38 /// free'd.
     39 ///
     40 class Type {
     41 public:
     42   //===--------------------------------------------------------------------===//
     43   /// Definitions of all of the base types for the Type system.  Based on this
     44   /// value, you can cast to a class defined in DerivedTypes.h.
     45   /// Note: If you add an element to this, you need to add an element to the
     46   /// Type::getPrimitiveType function, or else things will break!
     47   /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
     48   ///
     49   enum TypeID {
     50     // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
     51     VoidTyID = 0,    ///<  0: type with no size
     52     HalfTyID,        ///<  1: 16-bit floating point type
     53     FloatTyID,       ///<  2: 32-bit floating point type
     54     DoubleTyID,      ///<  3: 64-bit floating point type
     55     X86_FP80TyID,    ///<  4: 80-bit floating point type (X87)
     56     FP128TyID,       ///<  5: 128-bit floating point type (112-bit mantissa)
     57     PPC_FP128TyID,   ///<  6: 128-bit floating point type (two 64-bits, PowerPC)
     58     LabelTyID,       ///<  7: Labels
     59     MetadataTyID,    ///<  8: Metadata
     60     X86_MMXTyID,     ///<  9: MMX vectors (64 bits, X86 specific)
     61 
     62     // Derived types... see DerivedTypes.h file.
     63     // Make sure FirstDerivedTyID stays up to date!
     64     IntegerTyID,     ///< 10: Arbitrary bit width integers
     65     FunctionTyID,    ///< 11: Functions
     66     StructTyID,      ///< 12: Structures
     67     ArrayTyID,       ///< 13: Arrays
     68     PointerTyID,     ///< 14: Pointers
     69     VectorTyID,      ///< 15: SIMD 'packed' format, or other vector type
     70 
     71     NumTypeIDs,                         // Must remain as last defined ID
     72     LastPrimitiveTyID = X86_MMXTyID,
     73     FirstDerivedTyID = IntegerTyID
     74   };
     75 
     76 private:
     77   /// Context - This refers to the LLVMContext in which this type was uniqued.
     78   LLVMContext &Context;
     79 
     80   // Due to Ubuntu GCC bug 910363:
     81   // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363
     82   // Bitpack ID and SubclassData manually.
     83   // Note: TypeID : low 8 bit; SubclassData : high 24 bit.
     84   uint32_t IDAndSubclassData;
     85 
     86 protected:
     87   friend class LLVMContextImpl;
     88   explicit Type(LLVMContext &C, TypeID tid)
     89     : Context(C), IDAndSubclassData(0),
     90       NumContainedTys(0), ContainedTys(0) {
     91     setTypeID(tid);
     92   }
     93   ~Type() {}
     94 
     95   void setTypeID(TypeID ID) {
     96     IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00);
     97     assert(getTypeID() == ID && "TypeID data too large for field");
     98   }
     99 
    100   unsigned getSubclassData() const { return IDAndSubclassData >> 8; }
    101 
    102   void setSubclassData(unsigned val) {
    103     IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8);
    104     // Ensure we don't have any accidental truncation.
    105     assert(getSubclassData() == val && "Subclass data too large for field");
    106   }
    107 
    108   /// NumContainedTys - Keeps track of how many Type*'s there are in the
    109   /// ContainedTys list.
    110   unsigned NumContainedTys;
    111 
    112   /// ContainedTys - A pointer to the array of Types contained by this Type.
    113   /// For example, this includes the arguments of a function type, the elements
    114   /// of a structure, the pointee of a pointer, the element type of an array,
    115   /// etc.  This pointer may be 0 for types that don't contain other types
    116   /// (Integer, Double, Float).
    117   Type * const *ContainedTys;
    118 
    119 public:
    120   void print(raw_ostream &O) const;
    121   void dump() const;
    122 
    123   /// getContext - Return the LLVMContext in which this type was uniqued.
    124   LLVMContext &getContext() const { return Context; }
    125 
    126   //===--------------------------------------------------------------------===//
    127   // Accessors for working with types.
    128   //
    129 
    130   /// getTypeID - Return the type id for the type.  This will return one
    131   /// of the TypeID enum elements defined above.
    132   ///
    133   TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); }
    134 
    135   /// isVoidTy - Return true if this is 'void'.
    136   bool isVoidTy() const { return getTypeID() == VoidTyID; }
    137 
    138   /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
    139   bool isHalfTy() const { return getTypeID() == HalfTyID; }
    140 
    141   /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
    142   bool isFloatTy() const { return getTypeID() == FloatTyID; }
    143 
    144   /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
    145   bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
    146 
    147   /// isX86_FP80Ty - Return true if this is x86 long double.
    148   bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
    149 
    150   /// isFP128Ty - Return true if this is 'fp128'.
    151   bool isFP128Ty() const { return getTypeID() == FP128TyID; }
    152 
    153   /// isPPC_FP128Ty - Return true if this is powerpc long double.
    154   bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
    155 
    156   /// isFloatingPointTy - Return true if this is one of the five floating point
    157   /// types
    158   bool isFloatingPointTy() const {
    159     return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
    160            getTypeID() == DoubleTyID ||
    161            getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
    162            getTypeID() == PPC_FP128TyID;
    163   }
    164 
    165   /// isX86_MMXTy - Return true if this is X86 MMX.
    166   bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
    167 
    168   /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
    169   ///
    170   bool isFPOrFPVectorTy() const;
    171 
    172   /// isLabelTy - Return true if this is 'label'.
    173   bool isLabelTy() const { return getTypeID() == LabelTyID; }
    174 
    175   /// isMetadataTy - Return true if this is 'metadata'.
    176   bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
    177 
    178   /// isIntegerTy - True if this is an instance of IntegerType.
    179   ///
    180   bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
    181 
    182   /// isIntegerTy - Return true if this is an IntegerType of the given width.
    183   bool isIntegerTy(unsigned Bitwidth) const;
    184 
    185   /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
    186   /// integer types.
    187   ///
    188   bool isIntOrIntVectorTy() const;
    189 
    190   /// isFunctionTy - True if this is an instance of FunctionType.
    191   ///
    192   bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
    193 
    194   /// isStructTy - True if this is an instance of StructType.
    195   ///
    196   bool isStructTy() const { return getTypeID() == StructTyID; }
    197 
    198   /// isArrayTy - True if this is an instance of ArrayType.
    199   ///
    200   bool isArrayTy() const { return getTypeID() == ArrayTyID; }
    201 
    202   /// isPointerTy - True if this is an instance of PointerType.
    203   ///
    204   bool isPointerTy() const { return getTypeID() == PointerTyID; }
    205 
    206   /// isVectorTy - True if this is an instance of VectorType.
    207   ///
    208   bool isVectorTy() const { return getTypeID() == VectorTyID; }
    209 
    210   /// canLosslesslyBitCastTo - Return true if this type could be converted
    211   /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
    212   /// are valid for types of the same size only where no re-interpretation of
    213   /// the bits is done.
    214   /// @brief Determine if this type could be losslessly bitcast to Ty
    215   bool canLosslesslyBitCastTo(Type *Ty) const;
    216 
    217   /// isEmptyTy - Return true if this type is empty, that is, it has no
    218   /// elements or all its elements are empty.
    219   bool isEmptyTy() const;
    220 
    221   /// Here are some useful little methods to query what type derived types are
    222   /// Note that all other types can just compare to see if this == Type::xxxTy;
    223   ///
    224   bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; }
    225   bool isDerivedType()   const { return getTypeID() >= FirstDerivedTyID; }
    226 
    227   /// isFirstClassType - Return true if the type is "first class", meaning it
    228   /// is a valid type for a Value.
    229   ///
    230   bool isFirstClassType() const {
    231     return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
    232   }
    233 
    234   /// isSingleValueType - Return true if the type is a valid type for a
    235   /// register in codegen.  This includes all first-class types except struct
    236   /// and array types.
    237   ///
    238   bool isSingleValueType() const {
    239     return (getTypeID() != VoidTyID && isPrimitiveType()) ||
    240             getTypeID() == IntegerTyID || getTypeID() == PointerTyID ||
    241             getTypeID() == VectorTyID;
    242   }
    243 
    244   /// isAggregateType - Return true if the type is an aggregate type. This
    245   /// means it is valid as the first operand of an insertvalue or
    246   /// extractvalue instruction. This includes struct and array types, but
    247   /// does not include vector types.
    248   ///
    249   bool isAggregateType() const {
    250     return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
    251   }
    252 
    253   /// isSized - Return true if it makes sense to take the size of this type.  To
    254   /// get the actual size for a particular target, it is reasonable to use the
    255   /// TargetData subsystem to do this.
    256   ///
    257   bool isSized() const {
    258     // If it's a primitive, it is always sized.
    259     if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
    260         getTypeID() == PointerTyID ||
    261         getTypeID() == X86_MMXTyID)
    262       return true;
    263     // If it is not something that can have a size (e.g. a function or label),
    264     // it doesn't have a size.
    265     if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
    266         getTypeID() != VectorTyID)
    267       return false;
    268     // Otherwise we have to try harder to decide.
    269     return isSizedDerivedType();
    270   }
    271 
    272   /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
    273   /// primitive type.  These are fixed by LLVM and are not target dependent.
    274   /// This will return zero if the type does not have a size or is not a
    275   /// primitive type.
    276   ///
    277   /// Note that this may not reflect the size of memory allocated for an
    278   /// instance of the type or the number of bytes that are written when an
    279   /// instance of the type is stored to memory. The TargetData class provides
    280   /// additional query functions to provide this information.
    281   ///
    282   unsigned getPrimitiveSizeInBits() const;
    283 
    284   /// getScalarSizeInBits - If this is a vector type, return the
    285   /// getPrimitiveSizeInBits value for the element type. Otherwise return the
    286   /// getPrimitiveSizeInBits value for this type.
    287   unsigned getScalarSizeInBits();
    288 
    289   /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
    290   /// is only valid on floating point types.  If the FP type does not
    291   /// have a stable mantissa (e.g. ppc long double), this method returns -1.
    292   int getFPMantissaWidth() const;
    293 
    294   /// getScalarType - If this is a vector type, return the element type,
    295   /// otherwise return 'this'.
    296   Type *getScalarType();
    297 
    298   //===--------------------------------------------------------------------===//
    299   // Type Iteration support.
    300   //
    301   typedef Type * const *subtype_iterator;
    302   subtype_iterator subtype_begin() const { return ContainedTys; }
    303   subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
    304 
    305   /// getContainedType - This method is used to implement the type iterator
    306   /// (defined a the end of the file).  For derived types, this returns the
    307   /// types 'contained' in the derived type.
    308   ///
    309   Type *getContainedType(unsigned i) const {
    310     assert(i < NumContainedTys && "Index out of range!");
    311     return ContainedTys[i];
    312   }
    313 
    314   /// getNumContainedTypes - Return the number of types in the derived type.
    315   ///
    316   unsigned getNumContainedTypes() const { return NumContainedTys; }
    317 
    318   //===--------------------------------------------------------------------===//
    319   // Helper methods corresponding to subclass methods.  This forces a cast to
    320   // the specified subclass and calls its accessor.  "getVectorNumElements" (for
    321   // example) is shorthand for cast<VectorType>(Ty)->getNumElements().  This is
    322   // only intended to cover the core methods that are frequently used, helper
    323   // methods should not be added here.
    324 
    325   unsigned getIntegerBitWidth() const;
    326 
    327   Type *getFunctionParamType(unsigned i) const;
    328   unsigned getFunctionNumParams() const;
    329   bool isFunctionVarArg() const;
    330 
    331   StringRef getStructName() const;
    332   unsigned getStructNumElements() const;
    333   Type *getStructElementType(unsigned N) const;
    334 
    335   Type *getSequentialElementType() const;
    336 
    337   uint64_t getArrayNumElements() const;
    338   Type *getArrayElementType() const { return getSequentialElementType(); }
    339 
    340   unsigned getVectorNumElements() const;
    341   Type *getVectorElementType() const { return getSequentialElementType(); }
    342 
    343   unsigned getPointerAddressSpace() const;
    344   Type *getPointerElementType() const { return getSequentialElementType(); }
    345 
    346   //===--------------------------------------------------------------------===//
    347   // Static members exported by the Type class itself.  Useful for getting
    348   // instances of Type.
    349   //
    350 
    351   /// getPrimitiveType - Return a type based on an identifier.
    352   static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
    353 
    354   //===--------------------------------------------------------------------===//
    355   // These are the builtin types that are always available.
    356   //
    357   static Type *getVoidTy(LLVMContext &C);
    358   static Type *getLabelTy(LLVMContext &C);
    359   static Type *getHalfTy(LLVMContext &C);
    360   static Type *getFloatTy(LLVMContext &C);
    361   static Type *getDoubleTy(LLVMContext &C);
    362   static Type *getMetadataTy(LLVMContext &C);
    363   static Type *getX86_FP80Ty(LLVMContext &C);
    364   static Type *getFP128Ty(LLVMContext &C);
    365   static Type *getPPC_FP128Ty(LLVMContext &C);
    366   static Type *getX86_MMXTy(LLVMContext &C);
    367   static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
    368   static IntegerType *getInt1Ty(LLVMContext &C);
    369   static IntegerType *getInt8Ty(LLVMContext &C);
    370   static IntegerType *getInt16Ty(LLVMContext &C);
    371   static IntegerType *getInt32Ty(LLVMContext &C);
    372   static IntegerType *getInt64Ty(LLVMContext &C);
    373 
    374   //===--------------------------------------------------------------------===//
    375   // Convenience methods for getting pointer types with one of the above builtin
    376   // types as pointee.
    377   //
    378   static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
    379   static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
    380   static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
    381   static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
    382   static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
    383   static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
    384   static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
    385   static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
    386   static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
    387   static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
    388   static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
    389   static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
    390   static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
    391 
    392   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    393   static inline bool classof(const Type *) { return true; }
    394 
    395   /// getPointerTo - Return a pointer to the current type.  This is equivalent
    396   /// to PointerType::get(Foo, AddrSpace).
    397   PointerType *getPointerTo(unsigned AddrSpace = 0);
    398 
    399 private:
    400   /// isSizedDerivedType - Derived types like structures and arrays are sized
    401   /// iff all of the members of the type are sized as well.  Since asking for
    402   /// their size is relatively uncommon, move this operation out of line.
    403   bool isSizedDerivedType() const;
    404 };
    405 
    406 // Printing of types.
    407 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
    408   T.print(OS);
    409   return OS;
    410 }
    411 
    412 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
    413 template <> struct isa_impl<PointerType, Type> {
    414   static inline bool doit(const Type &Ty) {
    415     return Ty.getTypeID() == Type::PointerTyID;
    416   }
    417 };
    418 
    419 
    420 //===----------------------------------------------------------------------===//
    421 // Provide specializations of GraphTraits to be able to treat a type as a
    422 // graph of sub types.
    423 
    424 
    425 template <> struct GraphTraits<Type*> {
    426   typedef Type NodeType;
    427   typedef Type::subtype_iterator ChildIteratorType;
    428 
    429   static inline NodeType *getEntryNode(Type *T) { return T; }
    430   static inline ChildIteratorType child_begin(NodeType *N) {
    431     return N->subtype_begin();
    432   }
    433   static inline ChildIteratorType child_end(NodeType *N) {
    434     return N->subtype_end();
    435   }
    436 };
    437 
    438 template <> struct GraphTraits<const Type*> {
    439   typedef const Type NodeType;
    440   typedef Type::subtype_iterator ChildIteratorType;
    441 
    442   static inline NodeType *getEntryNode(NodeType *T) { return T; }
    443   static inline ChildIteratorType child_begin(NodeType *N) {
    444     return N->subtype_begin();
    445   }
    446   static inline ChildIteratorType child_end(NodeType *N) {
    447     return N->subtype_end();
    448   }
    449 };
    450 
    451 } // End llvm namespace
    452 
    453 #endif
    454