remove two useless functions. Just inherit Type's implementation instead.
[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   /// FunctionType::get - This static method is the primary way of constructing
152   /// a FunctionType. 
153   ///
154   static FunctionType *get(
155     const Type *Result, ///< The result type
156     const std::vector<const Type*> &Params, ///< The types of the parameters
157     bool isVarArg, ///< Whether this is a variable argument length function
158     ParamAttrsList *Attrs = 0
159       ///< Indicates the parameter attributes to use, if any. The 0th entry
160       ///< in the list refers to the return type. Parameters are numbered
161       ///< starting at 1. This argument must be on the heap and FunctionType
162       ///< owns it after its passed here.
163   );
164
165   inline bool isVarArg() const { return isVarArgs; }
166   inline const Type *getReturnType() const { return ContainedTys[0]; }
167
168   typedef Type::subtype_iterator param_iterator;
169   param_iterator param_begin() const { return ContainedTys + 1; }
170   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
171
172   // Parameter type accessors...
173   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
174
175   /// getNumParams - Return the number of fixed parameters this function type
176   /// requires.  This does not consider varargs.
177   ///
178   unsigned getNumParams() const { return NumContainedTys - 1; }
179
180   bool isStructReturn() const;
181   
182   /// The parameter attributes for the \p ith parameter are returned. The 0th
183   /// parameter refers to the return type of the function.
184   /// @returns The ParameterAttributes for the \p ith parameter.
185   /// @brief Get the attributes for a parameter
186   const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
187
188   // Implement the AbstractTypeUser interface.
189   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
190   virtual void typeBecameConcrete(const DerivedType *AbsTy);
191
192   // Methods for support type inquiry through isa, cast, and dyn_cast:
193   static inline bool classof(const FunctionType *T) { return true; }
194   static inline bool classof(const Type *T) {
195     return T->getTypeID() == FunctionTyID;
196   }
197 };
198
199
200 /// CompositeType - Common super class of ArrayType, StructType, PointerType
201 /// and VectorType
202 class CompositeType : public DerivedType {
203 protected:
204   inline explicit CompositeType(TypeID id) : DerivedType(id) { }
205 public:
206
207   /// getTypeAtIndex - Given an index value into the type, return the type of
208   /// the element.
209   ///
210   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
211   virtual bool indexValid(const Value *V) const = 0;
212
213   // Methods for support type inquiry through isa, cast, and dyn_cast:
214   static inline bool classof(const CompositeType *T) { return true; }
215   static inline bool classof(const Type *T) {
216     return T->getTypeID() == ArrayTyID ||
217            T->getTypeID() == StructTyID ||
218            T->getTypeID() == PointerTyID ||
219            T->getTypeID() == VectorTyID;
220   }
221 };
222
223
224 /// StructType - Class to represent struct types
225 ///
226 class StructType : public CompositeType {
227   friend class TypeMap<StructValType, StructType>;
228   StructType(const StructType &);                   // Do not implement
229   const StructType &operator=(const StructType &);  // Do not implement
230   StructType(const std::vector<const Type*> &Types, bool isPacked);
231 public:
232   /// StructType::get - This static method is the primary way to create a
233   /// StructType.
234   ///
235   static StructType *get(const std::vector<const Type*> &Params, 
236                          bool isPacked=false);
237
238   // Iterator access to the elements
239   typedef Type::subtype_iterator element_iterator;
240   element_iterator element_begin() const { return ContainedTys; }
241   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
242
243   // Random access to the elements
244   unsigned getNumElements() const { return NumContainedTys; }
245   const Type *getElementType(unsigned N) const {
246     assert(N < NumContainedTys && "Element number out of range!");
247     return ContainedTys[N];
248   }
249
250   /// getTypeAtIndex - Given an index value into the type, return the type of
251   /// the element.  For a structure type, this must be a constant value...
252   ///
253   virtual const Type *getTypeAtIndex(const Value *V) const ;
254   virtual bool indexValid(const Value *V) const;
255
256   // Implement the AbstractTypeUser interface.
257   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
258   virtual void typeBecameConcrete(const DerivedType *AbsTy);
259
260   // Methods for support type inquiry through isa, cast, and dyn_cast:
261   static inline bool classof(const StructType *T) { return true; }
262   static inline bool classof(const Type *T) {
263     return T->getTypeID() == StructTyID;
264   }
265
266   bool isPacked() const { return getSubclassData(); }
267 };
268
269
270 /// SequentialType - This is the superclass of the array, pointer and packed
271 /// type classes.  All of these represent "arrays" in memory.  The array type
272 /// represents a specifically sized array, pointer types are unsized/unknown
273 /// size arrays, vector types represent specifically sized arrays that
274 /// allow for use of SIMD instructions.  SequentialType holds the common
275 /// features of all, which stem from the fact that all three lay their
276 /// components out in memory identically.
277 ///
278 class SequentialType : public CompositeType {
279   PATypeHandle ContainedType; ///< Storage for the single contained type
280   SequentialType(const SequentialType &);                  // Do not implement!
281   const SequentialType &operator=(const SequentialType &); // Do not implement!
282 protected:
283   SequentialType(TypeID TID, const Type *ElType) 
284     : CompositeType(TID), ContainedType(ElType, this) {
285     ContainedTys = &ContainedType; 
286     NumContainedTys = 1;
287   }
288
289 public:
290   inline const Type *getElementType() const { return ContainedTys[0]; }
291
292   virtual bool indexValid(const Value *V) const;
293
294   /// getTypeAtIndex - Given an index value into the type, return the type of
295   /// the element.  For sequential types, there is only one subtype...
296   ///
297   virtual const Type *getTypeAtIndex(const Value *V) const {
298     return ContainedTys[0];
299   }
300
301   // Methods for support type inquiry through isa, cast, and dyn_cast:
302   static inline bool classof(const SequentialType *T) { return true; }
303   static inline bool classof(const Type *T) {
304     return T->getTypeID() == ArrayTyID ||
305            T->getTypeID() == PointerTyID ||
306            T->getTypeID() == VectorTyID;
307   }
308 };
309
310
311 /// ArrayType - Class to represent array types
312 ///
313 class ArrayType : public SequentialType {
314   friend class TypeMap<ArrayValType, ArrayType>;
315   uint64_t NumElements;
316
317   ArrayType(const ArrayType &);                   // Do not implement
318   const ArrayType &operator=(const ArrayType &);  // Do not implement
319   ArrayType(const Type *ElType, uint64_t NumEl);
320 public:
321   /// ArrayType::get - This static method is the primary way to construct an
322   /// ArrayType
323   ///
324   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
325
326   inline uint64_t getNumElements() const { return NumElements; }
327
328   // Implement the AbstractTypeUser interface.
329   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
330   virtual void typeBecameConcrete(const DerivedType *AbsTy);
331
332   // Methods for support type inquiry through isa, cast, and dyn_cast:
333   static inline bool classof(const ArrayType *T) { return true; }
334   static inline bool classof(const Type *T) {
335     return T->getTypeID() == ArrayTyID;
336   }
337 };
338
339 /// VectorType - Class to represent vector types
340 ///
341 class VectorType : public SequentialType {
342   friend class TypeMap<VectorValType, VectorType>;
343   unsigned NumElements;
344
345   VectorType(const VectorType &);                   // Do not implement
346   const VectorType &operator=(const VectorType &);  // Do not implement
347   VectorType(const Type *ElType, unsigned NumEl);
348 public:
349   /// VectorType::get - This static method is the primary way to construct an
350   /// VectorType
351   ///
352   static VectorType *get(const Type *ElementType, unsigned NumElements);
353
354   /// @brief Return the number of elements in the Vector type.
355   inline unsigned getNumElements() const { return NumElements; }
356
357   /// @brief Return the number of bits in the Vector type.
358   inline unsigned getBitWidth() const { 
359     return NumElements *getElementType()->getPrimitiveSizeInBits();
360   }
361
362   // Implement the AbstractTypeUser interface.
363   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
364   virtual void typeBecameConcrete(const DerivedType *AbsTy);
365
366   // Methods for support type inquiry through isa, cast, and dyn_cast:
367   static inline bool classof(const VectorType *T) { return true; }
368   static inline bool classof(const Type *T) {
369     return T->getTypeID() == VectorTyID;
370   }
371 };
372
373
374 /// PointerType - Class to represent pointers
375 ///
376 class PointerType : public SequentialType {
377   friend class TypeMap<PointerValType, PointerType>;
378   PointerType(const PointerType &);                   // Do not implement
379   const PointerType &operator=(const PointerType &);  // Do not implement
380   explicit PointerType(const Type *ElType);
381 public:
382   /// PointerType::get - This is the only way to construct a new pointer type.
383   static PointerType *get(const Type *ElementType);
384
385   // Implement the AbstractTypeUser interface.
386   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
387   virtual void typeBecameConcrete(const DerivedType *AbsTy);
388
389   // Implement support type inquiry through isa, cast, and dyn_cast:
390   static inline bool classof(const PointerType *T) { return true; }
391   static inline bool classof(const Type *T) {
392     return T->getTypeID() == PointerTyID;
393   }
394 };
395
396
397 /// OpaqueType - Class to represent abstract types
398 ///
399 class OpaqueType : public DerivedType {
400   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
401   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
402   OpaqueType();
403 public:
404   /// OpaqueType::get - Static factory method for the OpaqueType class...
405   ///
406   static OpaqueType *get() {
407     return new OpaqueType();           // All opaque types are distinct
408   }
409
410   // Implement support for type inquiry through isa, cast, and dyn_cast:
411   static inline bool classof(const OpaqueType *T) { return true; }
412   static inline bool classof(const Type *T) {
413     return T->getTypeID() == OpaqueTyID;
414   }
415 };
416
417 } // End llvm namespace
418
419 #endif