For PR950:
[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 public:
79   /// Function parameters can have attributes to indicate how they should be
80   /// treated by optimizations and code generation. This enumeration lists the
81   /// set of possible attributes.
82   /// @brief Function parameter attributes enumeration.
83   enum ParameterAttributes {
84     NoAttributeSet = 0, ///< No attribute value has been set on the parameter
85     ZExtAttribute  = 1, ///< The parameter should be zero extended before call
86     SExtAttribute  = 2  ///< The parameter should be sign extended before call
87   };
88   typedef std::vector<ParameterAttributes> ParamAttrsList;
89 private:
90   friend class TypeMap<FunctionValType, FunctionType>;
91   bool isVarArgs;
92   ParamAttrsList *ParamAttrs;
93
94   FunctionType(const FunctionType &);                   // Do not implement
95   const FunctionType &operator=(const FunctionType &);  // Do not implement
96   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
97                bool IsVarArgs, const ParamAttrsList &Attrs);
98
99 public:
100   /// FunctionType::get - This static method is the primary way of constructing
101   /// a FunctionType. 
102   ///
103   static FunctionType *get(
104     const Type *Result, ///< The result type
105     const std::vector<const Type*> &Params, ///< The types of the parameters
106     bool isVarArg, ///< Whether this is a variable argument length function
107     const ParamAttrsList & Attrs = ParamAttrsList()
108       ///< Indicates the parameter attributes to use, if any. The 0th entry
109       ///< in the list refers to the return type. Parameters are numbered
110       ///< starting at 1. 
111   );
112
113   inline bool isVarArg() const { return isVarArgs; }
114   inline const Type *getReturnType() const { return ContainedTys[0]; }
115
116   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
117   param_iterator param_begin() const { return ContainedTys.begin()+1; }
118   param_iterator param_end() const { return ContainedTys.end(); }
119
120   // Parameter type accessors...
121   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
122
123   /// getNumParams - Return the number of fixed parameters this function type
124   /// requires.  This does not consider varargs.
125   ///
126   unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
127
128   /// The parameter attributes for the \p ith parameter are returned. The 0th
129   /// parameter refers to the return type of the function.
130   /// @returns The ParameterAttributes for the \p ith parameter.
131   /// @brief Get the attributes for a parameter
132   ParameterAttributes getParamAttrs(unsigned i) const;
133
134   /// @brief Determine if a parameter attribute is set
135   bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
136     return getParamAttrs(i) & attr;
137   }
138
139   /// @brief Return the number of parameter attributes this type has.
140   unsigned getNumAttrs() const { 
141     return (ParamAttrs ?  unsigned(ParamAttrs->size()) : 0);
142   }
143
144   /// @brief Convert a ParameterAttribute into its assembly text
145   static const char * getParamAttrsText(ParameterAttributes Attr);
146
147   // Implement the AbstractTypeUser interface.
148   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
149   virtual void typeBecameConcrete(const DerivedType *AbsTy);
150
151   // Methods for support type inquiry through isa, cast, and dyn_cast:
152   static inline bool classof(const FunctionType *T) { return true; }
153   static inline bool classof(const Type *T) {
154     return T->getTypeID() == FunctionTyID;
155   }
156 };
157
158
159 /// CompositeType - Common super class of ArrayType, StructType, PointerType
160 /// and PackedType
161 class CompositeType : public DerivedType {
162 protected:
163   inline CompositeType(TypeID id) : DerivedType(id) { }
164 public:
165
166   /// getTypeAtIndex - Given an index value into the type, return the type of
167   /// the element.
168   ///
169   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
170   virtual bool indexValid(const Value *V) const = 0;
171
172   // Methods for support type inquiry through isa, cast, and dyn_cast:
173   static inline bool classof(const CompositeType *T) { return true; }
174   static inline bool classof(const Type *T) {
175     return T->getTypeID() == ArrayTyID ||
176            T->getTypeID() == StructTyID ||
177            T->getTypeID() == PointerTyID ||
178            T->getTypeID() == PackedTyID;
179   }
180 };
181
182
183 /// StructType - Class to represent struct types
184 ///
185 class StructType : public CompositeType {
186   friend class TypeMap<StructValType, StructType>;
187   StructType(const StructType &);                   // Do not implement
188   const StructType &operator=(const StructType &);  // Do not implement
189
190 protected:
191   /// This should really be private, but it squelches a bogus warning
192   /// from GCC to make them protected:  warning: `class StructType' only
193   /// defines private constructors and has no friends
194   ///
195   /// Private ctor - Only can be created by a static member...
196   ///
197   StructType(const std::vector<const Type*> &Types, bool isPacked);
198
199 public:
200   /// StructType::get - This static method is the primary way to create a
201   /// StructType.
202   ///
203   static StructType *get(const std::vector<const Type*> &Params, 
204                          bool isPacked=false);
205
206   // Iterator access to the elements
207   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
208   element_iterator element_begin() const { return ContainedTys.begin(); }
209   element_iterator element_end() const { return ContainedTys.end(); }
210
211   // Random access to the elements
212   unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
213   const Type *getElementType(unsigned N) const {
214     assert(N < ContainedTys.size() && "Element number out of range!");
215     return ContainedTys[N];
216   }
217
218   /// getTypeAtIndex - Given an index value into the type, return the type of
219   /// the element.  For a structure type, this must be a constant value...
220   ///
221   virtual const Type *getTypeAtIndex(const Value *V) const ;
222   virtual bool indexValid(const Value *V) const;
223
224   // Implement the AbstractTypeUser interface.
225   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
226   virtual void typeBecameConcrete(const DerivedType *AbsTy);
227
228   // Methods for support type inquiry through isa, cast, and dyn_cast:
229   static inline bool classof(const StructType *T) { return true; }
230   static inline bool classof(const Type *T) {
231     return T->getTypeID() == StructTyID;
232   }
233
234   bool isPacked() const { return getSubclassData(); }
235 };
236
237
238 /// SequentialType - This is the superclass of the array, pointer and packed
239 /// type classes.  All of these represent "arrays" in memory.  The array type
240 /// represents a specifically sized array, pointer types are unsized/unknown
241 /// size arrays, packed types represent specifically sized arrays that
242 /// allow for use of SIMD instructions.  SequentialType holds the common
243 /// features of all, which stem from the fact that all three lay their
244 /// components out in memory identically.
245 ///
246 class SequentialType : public CompositeType {
247   SequentialType(const SequentialType &);                  // Do not implement!
248   const SequentialType &operator=(const SequentialType &); // Do not implement!
249 protected:
250   SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
251     ContainedTys.reserve(1);
252     ContainedTys.push_back(PATypeHandle(ElType, this));
253   }
254
255 public:
256   inline const Type *getElementType() const { return ContainedTys[0]; }
257
258   virtual bool indexValid(const Value *V) const;
259
260   /// getTypeAtIndex - Given an index value into the type, return the type of
261   /// the element.  For sequential types, there is only one subtype...
262   ///
263   virtual const Type *getTypeAtIndex(const Value *V) const {
264     return ContainedTys[0];
265   }
266
267   // Methods for support type inquiry through isa, cast, and dyn_cast:
268   static inline bool classof(const SequentialType *T) { return true; }
269   static inline bool classof(const Type *T) {
270     return T->getTypeID() == ArrayTyID ||
271            T->getTypeID() == PointerTyID ||
272            T->getTypeID() == PackedTyID;
273   }
274 };
275
276
277 /// ArrayType - Class to represent array types
278 ///
279 class ArrayType : public SequentialType {
280   friend class TypeMap<ArrayValType, ArrayType>;
281   uint64_t NumElements;
282
283   ArrayType(const ArrayType &);                   // Do not implement
284   const ArrayType &operator=(const ArrayType &);  // Do not implement
285 protected:
286   /// This should really be private, but it squelches a bogus warning
287   /// from GCC to make them protected:  warning: `class ArrayType' only
288   /// defines private constructors and has no friends
289   ///
290   /// Private ctor - Only can be created by a static member...
291   ///
292   ArrayType(const Type *ElType, uint64_t NumEl);
293
294 public:
295   /// ArrayType::get - This static method is the primary way to construct an
296   /// ArrayType
297   ///
298   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
299
300   inline uint64_t getNumElements() const { return NumElements; }
301
302   // Implement the AbstractTypeUser interface.
303   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
304   virtual void typeBecameConcrete(const DerivedType *AbsTy);
305
306   // Methods for support type inquiry through isa, cast, and dyn_cast:
307   static inline bool classof(const ArrayType *T) { return true; }
308   static inline bool classof(const Type *T) {
309     return T->getTypeID() == ArrayTyID;
310   }
311 };
312
313 /// PackedType - Class to represent packed types
314 ///
315 class PackedType : public SequentialType {
316   friend class TypeMap<PackedValType, PackedType>;
317   unsigned NumElements;
318
319   PackedType(const PackedType &);                   // Do not implement
320   const PackedType &operator=(const PackedType &);  // Do not implement
321 protected:
322   /// This should really be private, but it squelches a bogus warning
323   /// from GCC to make them protected:  warning: `class PackedType' only
324   /// defines private constructors and has no friends
325   ///
326   /// Private ctor - Only can be created by a static member...
327   ///
328   PackedType(const Type *ElType, unsigned NumEl);
329
330 public:
331   /// PackedType::get - This static method is the primary way to construct an
332   /// PackedType
333   ///
334   static PackedType *get(const Type *ElementType, unsigned NumElements);
335
336   /// @brief Return the number of elements in the Packed type.
337   inline unsigned getNumElements() const { return NumElements; }
338
339   /// @brief Return the number of bits in the Packed type.
340   inline unsigned getBitWidth() const { 
341     return NumElements *getElementType()->getPrimitiveSizeInBits();
342   }
343
344   // Implement the AbstractTypeUser interface.
345   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
346   virtual void typeBecameConcrete(const DerivedType *AbsTy);
347
348   // Methods for support type inquiry through isa, cast, and dyn_cast:
349   static inline bool classof(const PackedType *T) { return true; }
350   static inline bool classof(const Type *T) {
351     return T->getTypeID() == PackedTyID;
352   }
353 };
354
355
356 /// PointerType - Class to represent pointers
357 ///
358 class PointerType : public SequentialType {
359   friend class TypeMap<PointerValType, PointerType>;
360   PointerType(const PointerType &);                   // Do not implement
361   const PointerType &operator=(const PointerType &);  // Do not implement
362 protected:
363   // This should really be private, but it squelches a bogus warning
364   // from GCC to make them protected:  warning: `class PointerType' only
365   // defines private constructors and has no friends
366
367   // Private ctor - Only can be created by a static member...
368   PointerType(const Type *ElType);
369
370 public:
371   /// PointerType::get - This is the only way to construct a new pointer type.
372   static PointerType *get(const Type *ElementType);
373
374   // Implement the AbstractTypeUser interface.
375   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
376   virtual void typeBecameConcrete(const DerivedType *AbsTy);
377
378   // Implement support type inquiry through isa, cast, and dyn_cast:
379   static inline bool classof(const PointerType *T) { return true; }
380   static inline bool classof(const Type *T) {
381     return T->getTypeID() == PointerTyID;
382   }
383 };
384
385
386 /// OpaqueType - Class to represent abstract types
387 ///
388 class OpaqueType : public DerivedType {
389   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
390   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
391 protected:
392   /// This should really be private, but it squelches a bogus warning
393   /// from GCC to make them protected:  warning: `class OpaqueType' only
394   /// defines private constructors and has no friends
395   ///
396   /// Private ctor - Only can be created by a static member...
397   OpaqueType();
398
399 public:
400   /// OpaqueType::get - Static factory method for the OpaqueType class...
401   ///
402   static OpaqueType *get() {
403     return new OpaqueType();           // All opaque types are distinct
404   }
405
406   // Implement the AbstractTypeUser interface.
407   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
408     abort();   // FIXME: this is not really an AbstractTypeUser!
409   }
410   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
411     abort();   // FIXME: this is not really an AbstractTypeUser!
412   }
413
414   // Implement support for type inquiry through isa, cast, and dyn_cast:
415   static inline bool classof(const OpaqueType *T) { return true; }
416   static inline bool classof(const Type *T) {
417     return T->getTypeID() == OpaqueTyID;
418   }
419 };
420
421 } // End llvm namespace
422
423 #endif