Start MCAsmStreamer implementation.
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.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 declarations of classes that represent "derived
11 // types".  These are things like "arrays of x" or "structure of x, y, z" or
12 // "method returning x taking (y,z) as parameters", etc...
13 //
14 // The implementations of these classes live in the Type.cpp file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
20
21 #include "llvm/Type.h"
22
23 namespace llvm {
24
25 class Value;
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
28 class ArrayValType;
29 class StructValType;
30 class PointerValType;
31 class VectorValType;
32 class IntegerValType;
33 class APInt;
34
35 class DerivedType : public Type {
36   friend class Type;
37
38 protected:
39   explicit DerivedType(TypeID id) : Type(id) {}
40
41   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
42   /// that the current type has transitioned from being abstract to being
43   /// concrete.
44   ///
45   void notifyUsesThatTypeBecameConcrete();
46
47   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
48   /// another (more concrete) type, we must eliminate all references to other
49   /// types, to avoid some circular reference problems.
50   ///
51   void dropAllTypeUses();
52
53   /// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo
54   /// that performs no locking.  Only used for internal recursion.
55   void unlockedRefineAbstractTypeTo(const Type *NewType);
56   
57 public:
58
59   //===--------------------------------------------------------------------===//
60   // Abstract Type handling methods - These types have special lifetimes, which
61   // are managed by (add|remove)AbstractTypeUser. See comments in
62   // AbstractTypeUser.h for more information.
63
64   /// refineAbstractTypeTo - This function is used to when it is discovered that
65   /// the 'this' abstract type is actually equivalent to the NewType specified.
66   /// This causes all users of 'this' to switch to reference the more concrete
67   /// type NewType and for 'this' to be deleted.
68   ///
69   void refineAbstractTypeTo(const Type *NewType);
70
71   void dump() const { Type::dump(); }
72
73   // Methods for support type inquiry through isa, cast, and dyn_cast:
74   static inline bool classof(const DerivedType *) { return true; }
75   static inline bool classof(const Type *T) {
76     return T->isDerivedType();
77   }
78 };
79
80 /// Class to represent integer types. Note that this class is also used to
81 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
82 /// Int64Ty.
83 /// @brief Integer representation type
84 class IntegerType : public DerivedType {
85 protected:
86   explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
87     setSubclassData(NumBits);
88   }
89   friend class TypeMap<IntegerValType, IntegerType>;
90 public:
91   /// This enum is just used to hold constants we need for IntegerType.
92   enum {
93     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
94     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
95       ///< Note that bit width is stored in the Type classes SubclassData field
96       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
97   };
98
99   /// This static method is the primary way of constructing an IntegerType.
100   /// If an IntegerType with the same NumBits value was previously instantiated,
101   /// that instance will be returned. Otherwise a new one will be created. Only
102   /// one instance with a given NumBits value is ever created.
103   /// @brief Get or create an IntegerType instance.
104   static const IntegerType* get(unsigned NumBits);
105
106   /// @brief Get the number of bits in this IntegerType
107   unsigned getBitWidth() const { return getSubclassData(); }
108
109   /// getBitMask - Return a bitmask with ones set for all of the bits
110   /// that can be set by an unsigned version of this type.  This is 0xFF for
111   /// i8, 0xFFFF for i16, etc.
112   uint64_t getBitMask() const {
113     return ~uint64_t(0UL) >> (64-getBitWidth());
114   }
115
116   /// getSignBit - Return a uint64_t with just the most significant bit set (the
117   /// sign bit, if the value is treated as a signed number).
118   uint64_t getSignBit() const {
119     return 1ULL << (getBitWidth()-1);
120   }
121
122   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
123   /// @returns a bit mask with ones set for all the bits of this type.
124   /// @brief Get a bit mask for this type.
125   APInt getMask() const;
126
127   /// This method determines if the width of this IntegerType is a power-of-2
128   /// in terms of 8 bit bytes.
129   /// @returns true if this is a power-of-2 byte width.
130   /// @brief Is this a power-of-2 byte-width IntegerType ?
131   bool isPowerOf2ByteWidth() const;
132
133   // Methods for support type inquiry through isa, cast, and dyn_cast:
134   static inline bool classof(const IntegerType *) { return true; }
135   static inline bool classof(const Type *T) {
136     return T->getTypeID() == IntegerTyID;
137   }
138 };
139
140
141 /// FunctionType - Class to represent function types
142 ///
143 class FunctionType : public DerivedType {
144   friend class TypeMap<FunctionValType, FunctionType>;
145   bool isVarArgs;
146
147   FunctionType(const FunctionType &);                   // Do not implement
148   const FunctionType &operator=(const FunctionType &);  // Do not implement
149   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
150                bool IsVarArgs);
151
152 public:
153   /// FunctionType::get - This static method is the primary way of constructing
154   /// a FunctionType.
155   ///
156   static FunctionType *get(
157     const Type *Result, ///< The result type
158     const std::vector<const Type*> &Params, ///< The types of the parameters
159     bool isVarArg  ///< Whether this is a variable argument length function
160   );
161
162   /// isValidReturnType - Return true if the specified type is valid as a return
163   /// type.
164   static bool isValidReturnType(const Type *RetTy);
165
166   /// isValidArgumentType - Return true if the specified type is valid as an
167   /// argument type.
168   static bool isValidArgumentType(const Type *ArgTy);
169
170   inline bool isVarArg() const { return isVarArgs; }
171   inline const Type *getReturnType() const { return ContainedTys[0]; }
172
173   typedef Type::subtype_iterator param_iterator;
174   param_iterator param_begin() const { return ContainedTys + 1; }
175   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
176
177   // Parameter type accessors...
178   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
179
180   /// getNumParams - Return the number of fixed parameters this function type
181   /// requires.  This does not consider varargs.
182   ///
183   unsigned getNumParams() const { return NumContainedTys - 1; }
184
185   // Implement the AbstractTypeUser interface.
186   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
187   virtual void typeBecameConcrete(const DerivedType *AbsTy);
188
189   // Methods for support type inquiry through isa, cast, and dyn_cast:
190   static inline bool classof(const FunctionType *) { return true; }
191   static inline bool classof(const Type *T) {
192     return T->getTypeID() == FunctionTyID;
193   }
194 };
195
196
197 /// CompositeType - Common super class of ArrayType, StructType, PointerType
198 /// and VectorType
199 class CompositeType : public DerivedType {
200 protected:
201   inline explicit CompositeType(TypeID id) : DerivedType(id) { }
202 public:
203
204   /// getTypeAtIndex - Given an index value into the type, return the type of
205   /// the element.
206   ///
207   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
208   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
209   virtual bool indexValid(const Value *V) const = 0;
210   virtual bool indexValid(unsigned Idx) const = 0;
211
212   // Methods for support type inquiry through isa, cast, and dyn_cast:
213   static inline bool classof(const CompositeType *) { return true; }
214   static inline bool classof(const Type *T) {
215     return T->getTypeID() == ArrayTyID ||
216            T->getTypeID() == StructTyID ||
217            T->getTypeID() == PointerTyID ||
218            T->getTypeID() == VectorTyID;
219   }
220 };
221
222
223 /// StructType - Class to represent struct types
224 ///
225 class StructType : public CompositeType {
226   friend class TypeMap<StructValType, StructType>;
227   StructType(const StructType &);                   // Do not implement
228   const StructType &operator=(const StructType &);  // Do not implement
229   StructType(const std::vector<const Type*> &Types, bool isPacked);
230 public:
231   /// StructType::get - This static method is the primary way to create a
232   /// StructType.
233   ///
234   static StructType *get(const std::vector<const Type*> &Params,
235                          bool isPacked=false);
236
237   /// StructType::get - This static method is a convenience method for
238   /// creating structure types by specifying the elements as arguments.
239   /// Note that this method always returns a non-packed struct.  To get
240   /// an empty struct, pass NULL, NULL.
241   static StructType *get(const Type *type, ...) END_WITH_NULL;
242
243   /// isValidElementType - Return true if the specified type is valid as a
244   /// element type.
245   static bool isValidElementType(const Type *ElemTy);
246
247   // Iterator access to the elements
248   typedef Type::subtype_iterator element_iterator;
249   element_iterator element_begin() const { return ContainedTys; }
250   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
251
252   // Random access to the elements
253   unsigned getNumElements() const { return NumContainedTys; }
254   const Type *getElementType(unsigned N) const {
255     assert(N < NumContainedTys && "Element number out of range!");
256     return ContainedTys[N];
257   }
258
259   /// getTypeAtIndex - Given an index value into the type, return the type of
260   /// the element.  For a structure type, this must be a constant value...
261   ///
262   virtual const Type *getTypeAtIndex(const Value *V) const;
263   virtual const Type *getTypeAtIndex(unsigned Idx) const;
264   virtual bool indexValid(const Value *V) const;
265   virtual bool indexValid(unsigned Idx) const;
266
267   // Implement the AbstractTypeUser interface.
268   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
269   virtual void typeBecameConcrete(const DerivedType *AbsTy);
270
271   // Methods for support type inquiry through isa, cast, and dyn_cast:
272   static inline bool classof(const StructType *) { return true; }
273   static inline bool classof(const Type *T) {
274     return T->getTypeID() == StructTyID;
275   }
276
277   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
278 };
279
280
281 /// SequentialType - This is the superclass of the array, pointer and vector
282 /// type classes.  All of these represent "arrays" in memory.  The array type
283 /// represents a specifically sized array, pointer types are unsized/unknown
284 /// size arrays, vector types represent specifically sized arrays that
285 /// allow for use of SIMD instructions.  SequentialType holds the common
286 /// features of all, which stem from the fact that all three lay their
287 /// components out in memory identically.
288 ///
289 class SequentialType : public CompositeType {
290   PATypeHandle ContainedType; ///< Storage for the single contained type
291   SequentialType(const SequentialType &);                  // Do not implement!
292   const SequentialType &operator=(const SequentialType &); // Do not implement!
293
294   // avoiding warning: 'this' : used in base member initializer list
295   SequentialType* this_() { return this; }
296 protected:
297   SequentialType(TypeID TID, const Type *ElType)
298     : CompositeType(TID), ContainedType(ElType, this_()) {
299     ContainedTys = &ContainedType;
300     NumContainedTys = 1;
301   }
302
303 public:
304   inline const Type *getElementType() const { return ContainedTys[0]; }
305
306   virtual bool indexValid(const Value *V) const;
307   virtual bool indexValid(unsigned) const {
308     return true;
309   }
310
311   /// getTypeAtIndex - Given an index value into the type, return the type of
312   /// the element.  For sequential types, there is only one subtype...
313   ///
314   virtual const Type *getTypeAtIndex(const Value *) const {
315     return ContainedTys[0];
316   }
317   virtual const Type *getTypeAtIndex(unsigned) const {
318     return ContainedTys[0];
319   }
320
321   // Methods for support type inquiry through isa, cast, and dyn_cast:
322   static inline bool classof(const SequentialType *) { return true; }
323   static inline bool classof(const Type *T) {
324     return T->getTypeID() == ArrayTyID ||
325            T->getTypeID() == PointerTyID ||
326            T->getTypeID() == VectorTyID;
327   }
328 };
329
330
331 /// ArrayType - Class to represent array types
332 ///
333 class ArrayType : public SequentialType {
334   friend class TypeMap<ArrayValType, ArrayType>;
335   uint64_t NumElements;
336
337   ArrayType(const ArrayType &);                   // Do not implement
338   const ArrayType &operator=(const ArrayType &);  // Do not implement
339   ArrayType(const Type *ElType, uint64_t NumEl);
340 public:
341   /// ArrayType::get - This static method is the primary way to construct an
342   /// ArrayType
343   ///
344   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
345
346   /// isValidElementType - Return true if the specified type is valid as a
347   /// element type.
348   static bool isValidElementType(const Type *ElemTy);
349
350   inline uint64_t getNumElements() const { return NumElements; }
351
352   // Implement the AbstractTypeUser interface.
353   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
354   virtual void typeBecameConcrete(const DerivedType *AbsTy);
355
356   // Methods for support type inquiry through isa, cast, and dyn_cast:
357   static inline bool classof(const ArrayType *) { return true; }
358   static inline bool classof(const Type *T) {
359     return T->getTypeID() == ArrayTyID;
360   }
361 };
362
363 /// VectorType - Class to represent vector types
364 ///
365 class VectorType : public SequentialType {
366   friend class TypeMap<VectorValType, VectorType>;
367   unsigned NumElements;
368
369   VectorType(const VectorType &);                   // Do not implement
370   const VectorType &operator=(const VectorType &);  // Do not implement
371   VectorType(const Type *ElType, unsigned NumEl);
372 public:
373   /// VectorType::get - This static method is the primary way to construct an
374   /// VectorType
375   ///
376   static VectorType *get(const Type *ElementType, unsigned NumElements);
377
378   /// VectorType::getInteger - This static method gets a VectorType with the
379   /// same number of elements as the input type, and the element type is an
380   /// integer type of the same width as the input element type.
381   ///
382   static VectorType *getInteger(const VectorType *VTy) {
383     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
384     const Type *EltTy = IntegerType::get(EltBits);
385     return VectorType::get(EltTy, VTy->getNumElements());
386   }
387
388   /// VectorType::getExtendedElementVectorType - This static method is like
389   /// getInteger except that the element types are twice as wide as the
390   /// elements in the input type.
391   ///
392   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
393     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
394     const Type *EltTy = IntegerType::get(EltBits * 2);
395     return VectorType::get(EltTy, VTy->getNumElements());
396   }
397
398   /// VectorType::getTruncatedElementVectorType - This static method is like
399   /// getInteger except that the element types are half as wide as the
400   /// elements in the input type.
401   ///
402   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
403     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
404     assert((EltBits & 1) == 0 &&
405            "Cannot truncate vector element with odd bit-width");
406     const Type *EltTy = IntegerType::get(EltBits / 2);
407     return VectorType::get(EltTy, VTy->getNumElements());
408   }
409
410   /// isValidElementType - Return true if the specified type is valid as a
411   /// element type.
412   static bool isValidElementType(const Type *ElemTy);
413
414   /// @brief Return the number of elements in the Vector type.
415   inline unsigned getNumElements() const { return NumElements; }
416
417   /// @brief Return the number of bits in the Vector type.
418   inline unsigned getBitWidth() const {
419     return NumElements *getElementType()->getPrimitiveSizeInBits();
420   }
421
422   // Implement the AbstractTypeUser interface.
423   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
424   virtual void typeBecameConcrete(const DerivedType *AbsTy);
425
426   // Methods for support type inquiry through isa, cast, and dyn_cast:
427   static inline bool classof(const VectorType *) { return true; }
428   static inline bool classof(const Type *T) {
429     return T->getTypeID() == VectorTyID;
430   }
431 };
432
433
434 /// PointerType - Class to represent pointers
435 ///
436 class PointerType : public SequentialType {
437   friend class TypeMap<PointerValType, PointerType>;
438   unsigned AddressSpace;
439
440   PointerType(const PointerType &);                   // Do not implement
441   const PointerType &operator=(const PointerType &);  // Do not implement
442   explicit PointerType(const Type *ElType, unsigned AddrSpace);
443 public:
444   /// PointerType::get - This constructs a pointer to an object of the specified
445   /// type in a numbered address space.
446   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
447
448   /// PointerType::getUnqual - This constructs a pointer to an object of the
449   /// specified type in the generic address space (address space zero).
450   static PointerType *getUnqual(const Type *ElementType) {
451     return PointerType::get(ElementType, 0);
452   }
453
454   /// isValidElementType - Return true if the specified type is valid as a
455   /// element type.
456   static bool isValidElementType(const Type *ElemTy);
457
458   /// @brief Return the address space of the Pointer type.
459   inline unsigned getAddressSpace() const { return AddressSpace; }
460
461   // Implement the AbstractTypeUser interface.
462   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
463   virtual void typeBecameConcrete(const DerivedType *AbsTy);
464
465   // Implement support type inquiry through isa, cast, and dyn_cast:
466   static inline bool classof(const PointerType *) { return true; }
467   static inline bool classof(const Type *T) {
468     return T->getTypeID() == PointerTyID;
469   }
470 };
471
472
473 /// OpaqueType - Class to represent abstract types
474 ///
475 class OpaqueType : public DerivedType {
476   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
477   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
478   OpaqueType();
479 public:
480   /// OpaqueType::get - Static factory method for the OpaqueType class...
481   ///
482   static OpaqueType *get() {
483     return new OpaqueType();           // All opaque types are distinct
484   }
485
486   // Implement support for type inquiry through isa, cast, and dyn_cast:
487   static inline bool classof(const OpaqueType *) { return true; }
488   static inline bool classof(const Type *T) {
489     return T->getTypeID() == OpaqueTyID;
490   }
491 };
492
493 } // End llvm namespace
494
495 #endif