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