Fix include guards so they exactly match file names.
[oota-llvm.git] / include / llvm / IR / Type.h
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_IR_TYPE_H
16 #define LLVM_IR_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 six 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 { return getScalarType()->isFloatingPointTy(); }
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 { return getScalarType()->isIntegerTy(); }
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   /// isPtrOrPtrVectorTy - Return true if this is a pointer type or a vector of
207   /// pointer types.
208   ///
209   bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
210  
211   /// isVectorTy - True if this is an instance of VectorType.
212   ///
213   bool isVectorTy() const { return getTypeID() == VectorTyID; }
214
215   /// canLosslesslyBitCastTo - Return true if this type could be converted 
216   /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts 
217   /// are valid for types of the same size only where no re-interpretation of 
218   /// the bits is done.
219   /// @brief Determine if this type could be losslessly bitcast to Ty
220   bool canLosslesslyBitCastTo(Type *Ty) const;
221
222   /// isEmptyTy - Return true if this type is empty, that is, it has no
223   /// elements or all its elements are empty.
224   bool isEmptyTy() const;
225
226   /// Here are some useful little methods to query what type derived types are
227   /// Note that all other types can just compare to see if this == Type::xxxTy;
228   ///
229   bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; }
230   bool isDerivedType()   const { return getTypeID() >= FirstDerivedTyID; }
231
232   /// isFirstClassType - Return true if the type is "first class", meaning it
233   /// is a valid type for a Value.
234   ///
235   bool isFirstClassType() const {
236     return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
237   }
238
239   /// isSingleValueType - Return true if the type is a valid type for a
240   /// register in codegen.  This includes all first-class types except struct
241   /// and array types.
242   ///
243   bool isSingleValueType() const {
244     return (getTypeID() != VoidTyID && isPrimitiveType()) ||
245             getTypeID() == IntegerTyID || getTypeID() == PointerTyID ||
246             getTypeID() == VectorTyID;
247   }
248
249   /// isAggregateType - Return true if the type is an aggregate type. This
250   /// means it is valid as the first operand of an insertvalue or
251   /// extractvalue instruction. This includes struct and array types, but
252   /// does not include vector types.
253   ///
254   bool isAggregateType() const {
255     return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
256   }
257
258   /// isSized - Return true if it makes sense to take the size of this type.  To
259   /// get the actual size for a particular target, it is reasonable to use the
260   /// DataLayout subsystem to do this.
261   ///
262   bool isSized() const {
263     // If it's a primitive, it is always sized.
264     if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
265         getTypeID() == PointerTyID ||
266         getTypeID() == X86_MMXTyID)
267       return true;
268     // If it is not something that can have a size (e.g. a function or label),
269     // it doesn't have a size.
270     if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
271         getTypeID() != VectorTyID)
272       return false;
273     // Otherwise we have to try harder to decide.
274     return isSizedDerivedType();
275   }
276
277   /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
278   /// primitive type.  These are fixed by LLVM and are not target dependent.
279   /// This will return zero if the type does not have a size or is not a
280   /// primitive type.
281   ///
282   /// Note that this may not reflect the size of memory allocated for an
283   /// instance of the type or the number of bytes that are written when an
284   /// instance of the type is stored to memory. The DataLayout class provides
285   /// additional query functions to provide this information.
286   ///
287   unsigned getPrimitiveSizeInBits() const;
288
289   /// getScalarSizeInBits - If this is a vector type, return the
290   /// getPrimitiveSizeInBits value for the element type. Otherwise return the
291   /// getPrimitiveSizeInBits value for this type.
292   unsigned getScalarSizeInBits();
293
294   /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
295   /// is only valid on floating point types.  If the FP type does not
296   /// have a stable mantissa (e.g. ppc long double), this method returns -1.
297   int getFPMantissaWidth() const;
298
299   /// getScalarType - If this is a vector type, return the element type,
300   /// otherwise return 'this'.
301   const Type *getScalarType() const;
302   Type *getScalarType();
303
304   //===--------------------------------------------------------------------===//
305   // Type Iteration support.
306   //
307   typedef Type * const *subtype_iterator;
308   subtype_iterator subtype_begin() const { return ContainedTys; }
309   subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
310
311   /// getContainedType - This method is used to implement the type iterator
312   /// (defined a the end of the file).  For derived types, this returns the
313   /// types 'contained' in the derived type.
314   ///
315   Type *getContainedType(unsigned i) const {
316     assert(i < NumContainedTys && "Index out of range!");
317     return ContainedTys[i];
318   }
319
320   /// getNumContainedTypes - Return the number of types in the derived type.
321   ///
322   unsigned getNumContainedTypes() const { return NumContainedTys; }
323
324   //===--------------------------------------------------------------------===//
325   // Helper methods corresponding to subclass methods.  This forces a cast to
326   // the specified subclass and calls its accessor.  "getVectorNumElements" (for
327   // example) is shorthand for cast<VectorType>(Ty)->getNumElements().  This is
328   // only intended to cover the core methods that are frequently used, helper
329   // methods should not be added here.
330   
331   unsigned getIntegerBitWidth() const;
332
333   Type *getFunctionParamType(unsigned i) const;
334   unsigned getFunctionNumParams() const;
335   bool isFunctionVarArg() const;
336   
337   StringRef getStructName() const;
338   unsigned getStructNumElements() const;
339   Type *getStructElementType(unsigned N) const;
340   
341   Type *getSequentialElementType() const;
342   
343   uint64_t getArrayNumElements() const;
344   Type *getArrayElementType() const { return getSequentialElementType(); }
345
346   unsigned getVectorNumElements() const;
347   Type *getVectorElementType() const { return getSequentialElementType(); }
348
349   Type *getPointerElementType() const { return getSequentialElementType(); }
350
351   /// \brief Get the address space of this pointer or pointer vector type.
352   unsigned getPointerAddressSpace() const;
353   
354   //===--------------------------------------------------------------------===//
355   // Static members exported by the Type class itself.  Useful for getting
356   // instances of Type.
357   //
358
359   /// getPrimitiveType - Return a type based on an identifier.
360   static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
361
362   //===--------------------------------------------------------------------===//
363   // These are the builtin types that are always available.
364   //
365   static Type *getVoidTy(LLVMContext &C);
366   static Type *getLabelTy(LLVMContext &C);
367   static Type *getHalfTy(LLVMContext &C);
368   static Type *getFloatTy(LLVMContext &C);
369   static Type *getDoubleTy(LLVMContext &C);
370   static Type *getMetadataTy(LLVMContext &C);
371   static Type *getX86_FP80Ty(LLVMContext &C);
372   static Type *getFP128Ty(LLVMContext &C);
373   static Type *getPPC_FP128Ty(LLVMContext &C);
374   static Type *getX86_MMXTy(LLVMContext &C);
375   static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
376   static IntegerType *getInt1Ty(LLVMContext &C);
377   static IntegerType *getInt8Ty(LLVMContext &C);
378   static IntegerType *getInt16Ty(LLVMContext &C);
379   static IntegerType *getInt32Ty(LLVMContext &C);
380   static IntegerType *getInt64Ty(LLVMContext &C);
381
382   //===--------------------------------------------------------------------===//
383   // Convenience methods for getting pointer types with one of the above builtin
384   // types as pointee.
385   //
386   static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
387   static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
388   static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
389   static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
390   static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
391   static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
392   static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
393   static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
394   static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
395   static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
396   static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
397   static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
398   static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
399
400   /// getPointerTo - Return a pointer to the current type.  This is equivalent
401   /// to PointerType::get(Foo, AddrSpace).
402   PointerType *getPointerTo(unsigned AddrSpace = 0);
403
404 private:
405   /// isSizedDerivedType - Derived types like structures and arrays are sized
406   /// iff all of the members of the type are sized as well.  Since asking for
407   /// their size is relatively uncommon, move this operation out of line.
408   bool isSizedDerivedType() const;
409 };
410
411 // Printing of types.
412 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
413   T.print(OS);
414   return OS;
415 }
416
417 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
418 template <> struct isa_impl<PointerType, Type> {
419   static inline bool doit(const Type &Ty) {
420     return Ty.getTypeID() == Type::PointerTyID;
421   }
422 };
423
424   
425 //===----------------------------------------------------------------------===//
426 // Provide specializations of GraphTraits to be able to treat a type as a
427 // graph of sub types.
428
429
430 template <> struct GraphTraits<Type*> {
431   typedef Type NodeType;
432   typedef Type::subtype_iterator ChildIteratorType;
433
434   static inline NodeType *getEntryNode(Type *T) { return T; }
435   static inline ChildIteratorType child_begin(NodeType *N) {
436     return N->subtype_begin();
437   }
438   static inline ChildIteratorType child_end(NodeType *N) {
439     return N->subtype_end();
440   }
441 };
442
443 template <> struct GraphTraits<const Type*> {
444   typedef const Type NodeType;
445   typedef Type::subtype_iterator ChildIteratorType;
446
447   static inline NodeType *getEntryNode(NodeType *T) { return T; }
448   static inline ChildIteratorType child_begin(NodeType *N) {
449     return N->subtype_begin();
450   }
451   static inline ChildIteratorType child_end(NodeType *N) {
452     return N->subtype_end();
453   }
454 };
455
456 } // End llvm namespace
457
458 #endif