Add a method to get the bit width of a packed type.
[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 PackedValType;
32
33 class DerivedType : public Type {
34   friend class Type;
35
36 protected:
37   DerivedType(TypeID id) : Type(id) {}
38
39   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
40   /// that the current type has transitioned from being abstract to being
41   /// concrete.
42   ///
43   void notifyUsesThatTypeBecameConcrete();
44
45   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
46   /// another (more concrete) type, we must eliminate all references to other
47   /// types, to avoid some circular reference problems.
48   ///
49   void dropAllTypeUses();
50
51 public:
52
53   //===--------------------------------------------------------------------===//
54   // Abstract Type handling methods - These types have special lifetimes, which
55   // are managed by (add|remove)AbstractTypeUser. See comments in
56   // AbstractTypeUser.h for more information.
57
58   /// refineAbstractTypeTo - This function is used to when it is discovered that
59   /// the 'this' abstract type is actually equivalent to the NewType specified.
60   /// This causes all users of 'this' to switch to reference the more concrete
61   /// type NewType and for 'this' to be deleted.
62   ///
63   void refineAbstractTypeTo(const Type *NewType);
64
65   void dump() const { Type::dump(); }
66
67   // Methods for support type inquiry through isa, cast, and dyn_cast:
68   static inline bool classof(const DerivedType *T) { return true; }
69   static inline bool classof(const Type *T) {
70     return T->isDerivedType();
71   }
72 };
73
74
75 /// FunctionType - Class to represent function types
76 ///
77 class FunctionType : public DerivedType {
78   friend class TypeMap<FunctionValType, FunctionType>;
79   bool isVarArgs;
80
81   FunctionType(const FunctionType &);                   // Do not implement
82   const FunctionType &operator=(const FunctionType &);  // Do not implement
83 protected:
84   /// This should really be private, but it squelches a bogus warning
85   /// from GCC to make them protected:  warning: `class FunctionType' only
86   /// defines private constructors and has no friends
87   ///
88   /// Private ctor - Only can be created by a static member...
89   ///
90   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
91                bool IsVarArgs);
92
93 public:
94   /// FunctionType::get - This static method is the primary way of constructing
95   /// a FunctionType
96   ///
97   static FunctionType *get(const Type *Result,
98                            const std::vector<const Type*> &Params,
99                            bool isVarArg);
100
101   inline bool isVarArg() const { return isVarArgs; }
102   inline const Type *getReturnType() const { return ContainedTys[0]; }
103
104   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
105   param_iterator param_begin() const { return ContainedTys.begin()+1; }
106   param_iterator param_end() const { return ContainedTys.end(); }
107
108   // Parameter type accessors...
109   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
110
111   /// getNumParams - Return the number of fixed parameters this function type
112   /// requires.  This does not consider varargs.
113   ///
114   unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
115
116   // Implement the AbstractTypeUser interface.
117   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
118   virtual void typeBecameConcrete(const DerivedType *AbsTy);
119
120   // Methods for support type inquiry through isa, cast, and dyn_cast:
121   static inline bool classof(const FunctionType *T) { return true; }
122   static inline bool classof(const Type *T) {
123     return T->getTypeID() == FunctionTyID;
124   }
125 };
126
127
128 /// CompositeType - Common super class of ArrayType, StructType, PointerType
129 /// and PackedType
130 class CompositeType : public DerivedType {
131 protected:
132   inline CompositeType(TypeID id) : DerivedType(id) { }
133 public:
134
135   /// getTypeAtIndex - Given an index value into the type, return the type of
136   /// the element.
137   ///
138   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
139   virtual bool indexValid(const Value *V) const = 0;
140
141   // Methods for support type inquiry through isa, cast, and dyn_cast:
142   static inline bool classof(const CompositeType *T) { return true; }
143   static inline bool classof(const Type *T) {
144     return T->getTypeID() == ArrayTyID ||
145            T->getTypeID() == StructTyID ||
146            T->getTypeID() == PointerTyID ||
147            T->getTypeID() == PackedTyID;
148   }
149 };
150
151
152 /// StructType - Class to represent struct types
153 ///
154 class StructType : public CompositeType {
155   friend class TypeMap<StructValType, StructType>;
156   StructType(const StructType &);                   // Do not implement
157   const StructType &operator=(const StructType &);  // Do not implement
158
159 protected:
160   /// This should really be private, but it squelches a bogus warning
161   /// from GCC to make them protected:  warning: `class StructType' only
162   /// defines private constructors and has no friends
163   ///
164   /// Private ctor - Only can be created by a static member...
165   ///
166   StructType(const std::vector<const Type*> &Types);
167
168 public:
169   /// StructType::get - This static method is the primary way to create a
170   /// StructType.
171   ///
172   static StructType *get(const std::vector<const Type*> &Params);
173
174   // Iterator access to the elements
175   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
176   element_iterator element_begin() const { return ContainedTys.begin(); }
177   element_iterator element_end() const { return ContainedTys.end(); }
178
179   // Random access to the elements
180   unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
181   const Type *getElementType(unsigned N) const {
182     assert(N < ContainedTys.size() && "Element number out of range!");
183     return ContainedTys[N];
184   }
185
186   /// getTypeAtIndex - Given an index value into the type, return the type of
187   /// the element.  For a structure type, this must be a constant value...
188   ///
189   virtual const Type *getTypeAtIndex(const Value *V) const ;
190   virtual bool indexValid(const Value *V) const;
191
192   // Implement the AbstractTypeUser interface.
193   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
194   virtual void typeBecameConcrete(const DerivedType *AbsTy);
195
196   // Methods for support type inquiry through isa, cast, and dyn_cast:
197   static inline bool classof(const StructType *T) { return true; }
198   static inline bool classof(const Type *T) {
199     return T->getTypeID() == StructTyID;
200   }
201 };
202
203
204 /// SequentialType - This is the superclass of the array, pointer and packed
205 /// type classes.  All of these represent "arrays" in memory.  The array type
206 /// represents a specifically sized array, pointer types are unsized/unknown
207 /// size arrays, packed types represent specifically sized arrays that
208 /// allow for use of SIMD instructions.  SequentialType holds the common
209 /// features of all, which stem from the fact that all three lay their
210 /// components out in memory identically.
211 ///
212 class SequentialType : public CompositeType {
213   SequentialType(const SequentialType &);                  // Do not implement!
214   const SequentialType &operator=(const SequentialType &); // Do not implement!
215 protected:
216   SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
217     ContainedTys.reserve(1);
218     ContainedTys.push_back(PATypeHandle(ElType, this));
219   }
220
221 public:
222   inline const Type *getElementType() const { return ContainedTys[0]; }
223
224   virtual bool indexValid(const Value *V) const;
225
226   /// getTypeAtIndex - Given an index value into the type, return the type of
227   /// the element.  For sequential types, there is only one subtype...
228   ///
229   virtual const Type *getTypeAtIndex(const Value *V) const {
230     return ContainedTys[0];
231   }
232
233   // Methods for support type inquiry through isa, cast, and dyn_cast:
234   static inline bool classof(const SequentialType *T) { return true; }
235   static inline bool classof(const Type *T) {
236     return T->getTypeID() == ArrayTyID ||
237            T->getTypeID() == PointerTyID ||
238            T->getTypeID() == PackedTyID;
239   }
240 };
241
242
243 /// ArrayType - Class to represent array types
244 ///
245 class ArrayType : public SequentialType {
246   friend class TypeMap<ArrayValType, ArrayType>;
247   uint64_t NumElements;
248
249   ArrayType(const ArrayType &);                   // Do not implement
250   const ArrayType &operator=(const ArrayType &);  // Do not implement
251 protected:
252   /// This should really be private, but it squelches a bogus warning
253   /// from GCC to make them protected:  warning: `class ArrayType' only
254   /// defines private constructors and has no friends
255   ///
256   /// Private ctor - Only can be created by a static member...
257   ///
258   ArrayType(const Type *ElType, uint64_t NumEl);
259
260 public:
261   /// ArrayType::get - This static method is the primary way to construct an
262   /// ArrayType
263   ///
264   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
265
266   inline uint64_t getNumElements() const { return NumElements; }
267
268   // Implement the AbstractTypeUser interface.
269   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
270   virtual void typeBecameConcrete(const DerivedType *AbsTy);
271
272   // Methods for support type inquiry through isa, cast, and dyn_cast:
273   static inline bool classof(const ArrayType *T) { return true; }
274   static inline bool classof(const Type *T) {
275     return T->getTypeID() == ArrayTyID;
276   }
277 };
278
279 /// PackedType - Class to represent packed types
280 ///
281 class PackedType : public SequentialType {
282   friend class TypeMap<PackedValType, PackedType>;
283   unsigned NumElements;
284
285   PackedType(const PackedType &);                   // Do not implement
286   const PackedType &operator=(const PackedType &);  // Do not implement
287 protected:
288   /// This should really be private, but it squelches a bogus warning
289   /// from GCC to make them protected:  warning: `class PackedType' only
290   /// defines private constructors and has no friends
291   ///
292   /// Private ctor - Only can be created by a static member...
293   ///
294   PackedType(const Type *ElType, unsigned NumEl);
295
296 public:
297   /// PackedType::get - This static method is the primary way to construct an
298   /// PackedType
299   ///
300   static PackedType *get(const Type *ElementType, unsigned NumElements);
301
302   /// @brief Return the number of elements in the Packed type.
303   inline unsigned getNumElements() const { return NumElements; }
304
305   /// @brief Return the number of bits in the Packed type.
306   inline unsigned getBitWidth() const { 
307     return NumElements *getElementType()->getPrimitiveSizeInBits();
308   }
309
310   // Implement the AbstractTypeUser interface.
311   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
312   virtual void typeBecameConcrete(const DerivedType *AbsTy);
313
314   // Methods for support type inquiry through isa, cast, and dyn_cast:
315   static inline bool classof(const PackedType *T) { return true; }
316   static inline bool classof(const Type *T) {
317     return T->getTypeID() == PackedTyID;
318   }
319 };
320
321
322 /// PointerType - Class to represent pointers
323 ///
324 class PointerType : public SequentialType {
325   friend class TypeMap<PointerValType, PointerType>;
326   PointerType(const PointerType &);                   // Do not implement
327   const PointerType &operator=(const PointerType &);  // Do not implement
328 protected:
329   // This should really be private, but it squelches a bogus warning
330   // from GCC to make them protected:  warning: `class PointerType' only
331   // defines private constructors and has no friends
332
333   // Private ctor - Only can be created by a static member...
334   PointerType(const Type *ElType);
335
336 public:
337   /// PointerType::get - This is the only way to construct a new pointer type.
338   static PointerType *get(const Type *ElementType);
339
340   // Implement the AbstractTypeUser interface.
341   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
342   virtual void typeBecameConcrete(const DerivedType *AbsTy);
343
344   // Implement support type inquiry through isa, cast, and dyn_cast:
345   static inline bool classof(const PointerType *T) { return true; }
346   static inline bool classof(const Type *T) {
347     return T->getTypeID() == PointerTyID;
348   }
349 };
350
351
352 /// OpaqueType - Class to represent abstract types
353 ///
354 class OpaqueType : public DerivedType {
355   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
356   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
357 protected:
358   /// This should really be private, but it squelches a bogus warning
359   /// from GCC to make them protected:  warning: `class OpaqueType' only
360   /// defines private constructors and has no friends
361   ///
362   /// Private ctor - Only can be created by a static member...
363   OpaqueType();
364
365 public:
366   /// OpaqueType::get - Static factory method for the OpaqueType class...
367   ///
368   static OpaqueType *get() {
369     return new OpaqueType();           // All opaque types are distinct
370   }
371
372   // Implement the AbstractTypeUser interface.
373   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
374     abort();   // FIXME: this is not really an AbstractTypeUser!
375   }
376   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
377     abort();   // FIXME: this is not really an AbstractTypeUser!
378   }
379
380   // Implement support for type inquiry through isa, cast, and dyn_cast:
381   static inline bool classof(const OpaqueType *T) { return true; }
382   static inline bool classof(const Type *T) {
383     return T->getTypeID() == OpaqueTyID;
384   }
385 };
386
387 } // End llvm namespace
388
389 #endif