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