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