Make enum-valued bitfield large enough to avoid interpretation as negative values...
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 class ParamAttrsList;
35
36 class DerivedType : public Type {
37   friend class Type;
38
39 protected:
40   explicit DerivedType(TypeID id) : Type(id) {}
41
42   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
43   /// that the current type has transitioned from being abstract to being
44   /// concrete.
45   ///
46   void notifyUsesThatTypeBecameConcrete();
47
48   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
49   /// another (more concrete) type, we must eliminate all references to other
50   /// types, to avoid some circular reference problems.
51   ///
52   void dropAllTypeUses();
53
54 public:
55
56   //===--------------------------------------------------------------------===//
57   // Abstract Type handling methods - These types have special lifetimes, which
58   // are managed by (add|remove)AbstractTypeUser. See comments in
59   // AbstractTypeUser.h for more information.
60
61   /// refineAbstractTypeTo - This function is used to when it is discovered that
62   /// the 'this' abstract type is actually equivalent to the NewType specified.
63   /// This causes all users of 'this' to switch to reference the more concrete
64   /// type NewType and for 'this' to be deleted.
65   ///
66   void refineAbstractTypeTo(const Type *NewType);
67
68   void dump() const { Type::dump(); }
69
70   // Methods for support type inquiry through isa, cast, and dyn_cast:
71   static inline bool classof(const DerivedType *T) { return true; }
72   static inline bool classof(const Type *T) {
73     return T->isDerivedType();
74   }
75 };
76
77 /// Class to represent integer types. Note that this class is also used to
78 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
79 /// Int64Ty. 
80 /// @brief Integer representation type
81 class IntegerType : public DerivedType {
82 protected:
83   explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
84     setSubclassData(NumBits);
85   }
86   friend class TypeMap<IntegerValType, IntegerType>;
87 public:
88   /// This enum is just used to hold constants we need for IntegerType.
89   enum {
90     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
91     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
92       ///< Note that bit width is stored in the Type classes SubclassData field
93       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
94   };
95
96   /// This static method is the primary way of constructing an IntegerType. 
97   /// If an IntegerType with the same NumBits value was previously instantiated,
98   /// that instance will be returned. Otherwise a new one will be created. Only
99   /// one instance with a given NumBits value is ever created.
100   /// @brief Get or create an IntegerType instance.
101   static const IntegerType* get(unsigned NumBits);
102
103   /// @brief Get the number of bits in this IntegerType
104   unsigned getBitWidth() const { return getSubclassData(); }
105
106   /// getBitMask - Return a bitmask with ones set for all of the bits
107   /// that can be set by an unsigned version of this type.  This is 0xFF for
108   /// sbyte/ubyte, 0xFFFF for shorts, etc.
109   uint64_t getBitMask() const {
110     return ~uint64_t(0UL) >> (64-getBitWidth());
111   }
112
113   /// getSignBit - Return a uint64_t with just the most significant bit set (the
114   /// sign bit, if the value is treated as a signed number).
115   uint64_t getSignBit() const {
116     return 1ULL << (getBitWidth()-1);
117   }
118   
119   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
120   /// @returns a bit mask with ones set for all the bits of this type.
121   /// @brief Get a bit mask for this type.
122   APInt getMask() const;
123
124   /// This method determines if the width of this IntegerType is a power-of-2
125   /// in terms of 8 bit bytes. 
126   /// @returns true if this is a power-of-2 byte width.
127   /// @brief Is this a power-of-2 byte-width IntegerType ?
128   bool isPowerOf2ByteWidth() const;
129
130   // Methods for support type inquiry through isa, cast, and dyn_cast:
131   static inline bool classof(const IntegerType *T) { return true; }
132   static inline bool classof(const Type *T) {
133     return T->getTypeID() == IntegerTyID;
134   }
135 };
136
137
138 /// FunctionType - Class to represent function types
139 ///
140 class FunctionType : public DerivedType {
141   friend class TypeMap<FunctionValType, FunctionType>;
142   bool isVarArgs;
143   ParamAttrsList *ParamAttrs;
144
145   FunctionType(const FunctionType &);                   // Do not implement
146   const FunctionType &operator=(const FunctionType &);  // Do not implement
147   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
148                bool IsVarArgs, ParamAttrsList *Attrs = 0);
149
150 public:
151   virtual ~FunctionType();
152   /// FunctionType::get - This static method is the primary way of constructing
153   /// a FunctionType. 
154   ///
155   static FunctionType *get(
156     const Type *Result, ///< The result type
157     const std::vector<const Type*> &Params, ///< The types of the parameters
158     bool isVarArg, ///< Whether this is a variable argument length function
159     ParamAttrsList *Attrs = 0
160       ///< Indicates the parameter attributes to use, if any. The 0th entry
161       ///< in the list refers to the return type. Parameters are numbered
162       ///< starting at 1. This argument must be on the heap and FunctionType
163       ///< owns it after its passed here.
164   );
165
166   inline bool isVarArg() const { return isVarArgs; }
167   inline const Type *getReturnType() const { return ContainedTys[0]; }
168
169   typedef Type::subtype_iterator param_iterator;
170   param_iterator param_begin() const { return ContainedTys + 1; }
171   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
172
173   // Parameter type accessors...
174   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
175
176   /// getNumParams - Return the number of fixed parameters this function type
177   /// requires.  This does not consider varargs.
178   ///
179   unsigned getNumParams() const { return NumContainedTys - 1; }
180
181   bool isStructReturn() const;
182   
183   /// The parameter attributes for the \p ith parameter are returned. The 0th
184   /// parameter refers to the return type of the function.
185   /// @returns The ParameterAttributes for the \p ith parameter.
186   /// @brief Get the attributes for a parameter
187   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
188
189   // Implement the AbstractTypeUser interface.
190   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
191   virtual void typeBecameConcrete(const DerivedType *AbsTy);
192
193   // Methods for support type inquiry through isa, cast, and dyn_cast:
194   static inline bool classof(const FunctionType *T) { return true; }
195   static inline bool classof(const Type *T) {
196     return T->getTypeID() == FunctionTyID;
197   }
198 };
199
200
201 /// CompositeType - Common super class of ArrayType, StructType, PointerType
202 /// and VectorType
203 class CompositeType : public DerivedType {
204 protected:
205   inline explicit CompositeType(TypeID id) : DerivedType(id) { }
206 public:
207
208   /// getTypeAtIndex - Given an index value into the type, return the type of
209   /// the element.
210   ///
211   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
212   virtual bool indexValid(const Value *V) const = 0;
213
214   // Methods for support type inquiry through isa, cast, and dyn_cast:
215   static inline bool classof(const CompositeType *T) { return true; }
216   static inline bool classof(const Type *T) {
217     return T->getTypeID() == ArrayTyID ||
218            T->getTypeID() == StructTyID ||
219            T->getTypeID() == PointerTyID ||
220            T->getTypeID() == VectorTyID;
221   }
222 };
223
224
225 /// StructType - Class to represent struct types
226 ///
227 class StructType : public CompositeType {
228   friend class TypeMap<StructValType, StructType>;
229   StructType(const StructType &);                   // Do not implement
230   const StructType &operator=(const StructType &);  // Do not implement
231   StructType(const std::vector<const Type*> &Types, bool isPacked);
232 public:
233   /// StructType::get - This static method is the primary way to create a
234   /// StructType.
235   ///
236   static StructType *get(const std::vector<const Type*> &Params, 
237                          bool isPacked=false);
238
239   // Iterator access to the elements
240   typedef Type::subtype_iterator element_iterator;
241   element_iterator element_begin() const { return ContainedTys; }
242   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
243
244   // Random access to the elements
245   unsigned getNumElements() const { return NumContainedTys; }
246   const Type *getElementType(unsigned N) const {
247     assert(N < NumContainedTys && "Element number out of range!");
248     return ContainedTys[N];
249   }
250
251   /// getTypeAtIndex - Given an index value into the type, return the type of
252   /// the element.  For a structure type, this must be a constant value...
253   ///
254   virtual const Type *getTypeAtIndex(const Value *V) const ;
255   virtual bool indexValid(const Value *V) const;
256
257   // Implement the AbstractTypeUser interface.
258   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
259   virtual void typeBecameConcrete(const DerivedType *AbsTy);
260
261   // Methods for support type inquiry through isa, cast, and dyn_cast:
262   static inline bool classof(const StructType *T) { return true; }
263   static inline bool classof(const Type *T) {
264     return T->getTypeID() == StructTyID;
265   }
266
267   bool isPacked() const { return getSubclassData(); }
268 };
269
270
271 /// SequentialType - This is the superclass of the array, pointer and packed
272 /// type classes.  All of these represent "arrays" in memory.  The array type
273 /// represents a specifically sized array, pointer types are unsized/unknown
274 /// size arrays, vector types represent specifically sized arrays that
275 /// allow for use of SIMD instructions.  SequentialType holds the common
276 /// features of all, which stem from the fact that all three lay their
277 /// components out in memory identically.
278 ///
279 class SequentialType : public CompositeType {
280   PATypeHandle ContainedType; ///< Storage for the single contained type
281   SequentialType(const SequentialType &);                  // Do not implement!
282   const SequentialType &operator=(const SequentialType &); // Do not implement!
283 protected:
284   SequentialType(TypeID TID, const Type *ElType) 
285     : CompositeType(TID), ContainedType(ElType, this) {
286     ContainedTys = &ContainedType; 
287     NumContainedTys = 1;
288   }
289
290 public:
291   inline const Type *getElementType() const { return ContainedTys[0]; }
292
293   virtual bool indexValid(const Value *V) const;
294
295   /// getTypeAtIndex - Given an index value into the type, return the type of
296   /// the element.  For sequential types, there is only one subtype...
297   ///
298   virtual const Type *getTypeAtIndex(const Value *V) const {
299     return ContainedTys[0];
300   }
301
302   // Methods for support type inquiry through isa, cast, and dyn_cast:
303   static inline bool classof(const SequentialType *T) { return true; }
304   static inline bool classof(const Type *T) {
305     return T->getTypeID() == ArrayTyID ||
306            T->getTypeID() == PointerTyID ||
307            T->getTypeID() == VectorTyID;
308   }
309 };
310
311
312 /// ArrayType - Class to represent array types
313 ///
314 class ArrayType : public SequentialType {
315   friend class TypeMap<ArrayValType, ArrayType>;
316   uint64_t NumElements;
317
318   ArrayType(const ArrayType &);                   // Do not implement
319   const ArrayType &operator=(const ArrayType &);  // Do not implement
320   ArrayType(const Type *ElType, uint64_t NumEl);
321 public:
322   /// ArrayType::get - This static method is the primary way to construct an
323   /// ArrayType
324   ///
325   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
326
327   inline uint64_t getNumElements() const { return NumElements; }
328
329   // Implement the AbstractTypeUser interface.
330   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
331   virtual void typeBecameConcrete(const DerivedType *AbsTy);
332
333   // Methods for support type inquiry through isa, cast, and dyn_cast:
334   static inline bool classof(const ArrayType *T) { return true; }
335   static inline bool classof(const Type *T) {
336     return T->getTypeID() == ArrayTyID;
337   }
338 };
339
340 /// VectorType - Class to represent vector types
341 ///
342 class VectorType : public SequentialType {
343   friend class TypeMap<VectorValType, VectorType>;
344   unsigned NumElements;
345
346   VectorType(const VectorType &);                   // Do not implement
347   const VectorType &operator=(const VectorType &);  // Do not implement
348   VectorType(const Type *ElType, unsigned NumEl);
349 public:
350   /// VectorType::get - This static method is the primary way to construct an
351   /// VectorType
352   ///
353   static VectorType *get(const Type *ElementType, unsigned NumElements);
354
355   /// @brief Return the number of elements in the Vector type.
356   inline unsigned getNumElements() const { return NumElements; }
357
358   /// @brief Return the number of bits in the Vector type.
359   inline unsigned getBitWidth() const { 
360     return NumElements *getElementType()->getPrimitiveSizeInBits();
361   }
362
363   // Implement the AbstractTypeUser interface.
364   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
365   virtual void typeBecameConcrete(const DerivedType *AbsTy);
366
367   // Methods for support type inquiry through isa, cast, and dyn_cast:
368   static inline bool classof(const VectorType *T) { return true; }
369   static inline bool classof(const Type *T) {
370     return T->getTypeID() == VectorTyID;
371   }
372 };
373
374
375 /// PointerType - Class to represent pointers
376 ///
377 class PointerType : public SequentialType {
378   friend class TypeMap<PointerValType, PointerType>;
379   PointerType(const PointerType &);                   // Do not implement
380   const PointerType &operator=(const PointerType &);  // Do not implement
381   explicit PointerType(const Type *ElType);
382 public:
383   /// PointerType::get - This is the only way to construct a new pointer type.
384   static PointerType *get(const Type *ElementType);
385
386   // Implement the AbstractTypeUser interface.
387   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
388   virtual void typeBecameConcrete(const DerivedType *AbsTy);
389
390   // Implement support type inquiry through isa, cast, and dyn_cast:
391   static inline bool classof(const PointerType *T) { return true; }
392   static inline bool classof(const Type *T) {
393     return T->getTypeID() == PointerTyID;
394   }
395 };
396
397
398 /// OpaqueType - Class to represent abstract types
399 ///
400 class OpaqueType : public DerivedType {
401   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
402   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
403   OpaqueType();
404 public:
405   /// OpaqueType::get - Static factory method for the OpaqueType class...
406   ///
407   static OpaqueType *get() {
408     return new OpaqueType();           // All opaque types are distinct
409   }
410
411   // Implement the AbstractTypeUser interface.
412   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
413     abort();   // FIXME: this is not really an AbstractTypeUser!
414   }
415   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
416     abort();   // FIXME: this is not really an AbstractTypeUser!
417   }
418
419   // Implement support for type inquiry through isa, cast, and dyn_cast:
420   static inline bool classof(const OpaqueType *T) { return true; }
421   static inline bool classof(const Type *T) {
422     return T->getTypeID() == OpaqueTyID;
423   }
424 };
425
426 } // End llvm namespace
427
428 #endif