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