Rearrange code to eliminate warnings
[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 template<class ValType, class TypeClass> class TypeMap;
26 class FunctionValType;
27 class ArrayValType;
28 class StructValType;
29 class PointerValType;
30
31 class DerivedType : public Type, public AbstractTypeUser {
32   // AbstractTypeUsers - Implement a list of the users that need to be notified
33   // if I am a type, and I get resolved into a more concrete type.
34   //
35   mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
36
37 protected:
38   DerivedType(PrimitiveID id) : Type("", id) {}
39   ~DerivedType() {
40     assert(AbstractTypeUsers.empty());
41   }
42
43   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
44   /// that the current type has transitioned from being abstract to being
45   /// concrete.
46   ///
47   void notifyUsesThatTypeBecameConcrete();
48
49   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
50   /// another (more concrete) type, we must eliminate all references to other
51   /// types, to avoid some circular reference problems.
52   ///
53   void dropAllTypeUses();
54
55   void RefCountIsZero() const {
56     if (AbstractTypeUsers.empty())
57       delete this;
58   }
59
60   
61 public:
62
63   //===--------------------------------------------------------------------===//
64   // Abstract Type handling methods - These types have special lifetimes, which
65   // are managed by (add|remove)AbstractTypeUser. See comments in
66   // AbstractTypeUser.h for more information.
67
68   /// addAbstractTypeUser - Notify an abstract type that there is a new user of
69   /// it.  This function is called primarily by the PATypeHandle class.
70   ///
71   void addAbstractTypeUser(AbstractTypeUser *U) const {
72     assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
73     AbstractTypeUsers.push_back(U);
74   }
75
76   /// removeAbstractTypeUser - Notify an abstract type that a user of the class
77   /// no longer has a handle to the type.  This function is called primarily by
78   /// the PATypeHandle class.  When there are no users of the abstract type, it
79   /// is annihilated, because there is no way to get a reference to it ever
80   /// again.
81   ///
82   void removeAbstractTypeUser(AbstractTypeUser *U) const;
83
84   /// refineAbstractTypeTo - This function is used to when it is discovered that
85   /// the 'this' abstract type is actually equivalent to the NewType specified.
86   /// This causes all users of 'this' to switch to reference the more concrete
87   /// type NewType and for 'this' to be deleted.
88   ///
89   void refineAbstractTypeTo(const Type *NewType);
90
91   void dump() const { Value::dump(); }
92
93   // Methods for support type inquiry through isa, cast, and dyn_cast:
94   static inline bool classof(const DerivedType *T) { return true; }
95   static inline bool classof(const Type *T) {
96     return T->isDerivedType();
97   }
98   static inline bool classof(const Value *V) {
99     return isa<Type>(V) && classof(cast<Type>(V));
100   }
101 };
102
103
104 /// FunctionType - Class to represent function types
105 ///
106 class FunctionType : public DerivedType {
107   friend class TypeMap<FunctionValType, FunctionType>;
108   bool isVarArgs;
109
110   FunctionType(const FunctionType &);                   // Do not implement
111   const FunctionType &operator=(const FunctionType &);  // Do not implement
112 protected:
113   /// This should really be private, but it squelches a bogus warning
114   /// from GCC to make them protected:  warning: `class FunctionType' only 
115   /// defines private constructors and has no friends
116   ///
117   /// Private ctor - Only can be created by a static member...
118   ///
119   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
120                bool IsVarArgs);
121
122 public:
123   /// FunctionType::get - This static method is the primary way of constructing
124   /// a FunctionType
125   ///
126   static FunctionType *get(const Type *Result,
127                            const std::vector<const Type*> &Params,
128                            bool isVarArg);
129
130   inline bool isVarArg() const { return isVarArgs; }
131   inline const Type *getReturnType() const { return ContainedTys[0]; }
132
133   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
134   param_iterator param_begin() const { return ContainedTys.begin()+1; }
135   param_iterator param_end() const { return ContainedTys.end(); }
136
137   // Parameter type accessors...
138   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
139
140   /// getNumParams - Return the number of fixed parameters this function type
141   /// requires.  This does not consider varargs.
142   ///
143   unsigned getNumParams() const { return ContainedTys.size()-1; }
144
145   // Implement the AbstractTypeUser interface.
146   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
147   virtual void typeBecameConcrete(const DerivedType *AbsTy);
148   
149   // Methods for support type inquiry through isa, cast, and dyn_cast:
150   static inline bool classof(const FunctionType *T) { return true; }
151   static inline bool classof(const Type *T) {
152     return T->getPrimitiveID() == FunctionTyID;
153   }
154   static inline bool classof(const Value *V) {
155     return isa<Type>(V) && classof(cast<Type>(V));
156   }
157 };
158
159
160 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
161 ///
162 class CompositeType : public DerivedType {
163 protected:
164   inline CompositeType(PrimitiveID 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->getPrimitiveID() == ArrayTyID || 
177            T->getPrimitiveID() == StructTyID ||
178            T->getPrimitiveID() == PointerTyID;
179   }
180   static inline bool classof(const Value *V) {
181     return isa<Type>(V) && classof(cast<Type>(V));
182   }
183 };
184
185
186 /// StructType - Class to represent struct types
187 ///
188 class StructType : public CompositeType {
189   friend class TypeMap<StructValType, StructType>;
190   StructType(const StructType &);                   // Do not implement
191   const StructType &operator=(const StructType &);  // Do not implement
192
193 protected:
194   /// This should really be private, but it squelches a bogus warning
195   /// from GCC to make them protected:  warning: `class StructType' only 
196   /// defines private constructors and has no friends
197   ///
198   /// Private ctor - Only can be created by a static member...
199   ///
200   StructType(const std::vector<const Type*> &Types);
201
202 public:
203   /// StructType::get - This static method is the primary way to create a
204   /// StructType.
205   ///
206   static StructType *get(const std::vector<const Type*> &Params);
207
208   // Iterator access to the elements
209   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
210   element_iterator element_begin() const { return ContainedTys.begin(); }
211   element_iterator element_end() const { return ContainedTys.end(); }
212
213   // Random access to the elements
214   unsigned getNumElements() const { return ContainedTys.size(); }
215   const Type *getElementType(unsigned N) const {
216     assert(N < ContainedTys.size() && "Element number out of range!");
217     return ContainedTys[N];
218   }
219
220   /// getTypeAtIndex - Given an index value into the type, return the type of
221   /// the element.  For a structure type, this must be a constant value...
222   ///
223   virtual const Type *getTypeAtIndex(const Value *V) const ;
224   virtual bool indexValid(const Value *V) const;
225
226   // Implement the AbstractTypeUser interface.
227   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
228   virtual void typeBecameConcrete(const DerivedType *AbsTy);
229
230   // Methods for support type inquiry through isa, cast, and dyn_cast:
231   static inline bool classof(const StructType *T) { return true; }
232   static inline bool classof(const Type *T) {
233     return T->getPrimitiveID() == StructTyID;
234   }
235   static inline bool classof(const Value *V) {
236     return isa<Type>(V) && classof(cast<Type>(V));
237   }
238 };
239
240
241 /// SequentialType - This is the superclass of the array and pointer type
242 /// classes.  Both of these represent "arrays" in memory.  The array type
243 /// represents a specifically sized array, pointer types are unsized/unknown
244 /// size arrays.  SequentialType holds the common features of both, which stem
245 /// from the fact that both lay their components out in memory identically.
246 ///
247 class SequentialType : public CompositeType {
248   SequentialType(const SequentialType &);                  // Do not implement!
249   const SequentialType &operator=(const SequentialType &); // Do not implement!
250 protected:
251   SequentialType(PrimitiveID TID, const Type *ElType) : CompositeType(TID) {
252     ContainedTys.reserve(1);
253     ContainedTys.push_back(PATypeHandle(ElType, this));
254   }
255
256 public:
257   inline const Type *getElementType() const { return ContainedTys[0]; }
258
259   /// getTypeAtIndex - Given an index value into the type, return the type of
260   /// the element.  For sequential types, there is only one subtype...
261   ///
262   virtual const Type *getTypeAtIndex(const Value *V) const {
263     return ContainedTys[0];
264   }
265   virtual bool indexValid(const Value *V) const {
266     return V->getType()->isInteger();
267   }
268
269   // Methods for support type inquiry through isa, cast, and dyn_cast:
270   static inline bool classof(const SequentialType *T) { return true; }
271   static inline bool classof(const Type *T) {
272     return T->getPrimitiveID() == ArrayTyID ||
273            T->getPrimitiveID() == PointerTyID;
274   }
275   static inline bool classof(const Value *V) {
276     return isa<Type>(V) && classof(cast<Type>(V));
277   }
278 };
279
280
281 /// ArrayType - Class to represent array types
282 ///
283 class ArrayType : public SequentialType {
284   friend class TypeMap<ArrayValType, ArrayType>;
285   unsigned NumElements;
286
287   ArrayType(const ArrayType &);                   // Do not implement
288   const ArrayType &operator=(const ArrayType &);  // Do not implement
289 protected:
290   /// This should really be private, but it squelches a bogus warning
291   /// from GCC to make them protected:  warning: `class ArrayType' only 
292   /// defines private constructors and has no friends
293   ///
294   /// Private ctor - Only can be created by a static member...
295   ///
296   ArrayType(const Type *ElType, unsigned NumEl);
297
298 public:
299   /// ArrayType::get - This static method is the primary way to construct an
300   /// ArrayType
301   ///
302   static ArrayType *get(const Type *ElementType, unsigned NumElements);
303
304   inline unsigned    getNumElements() const { return NumElements; }
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 ArrayType *T) { return true; }
312   static inline bool classof(const Type *T) {
313     return T->getPrimitiveID() == ArrayTyID;
314   }
315   static inline bool classof(const Value *V) {
316     return isa<Type>(V) && classof(cast<Type>(V));
317   }
318 };
319
320
321 /// PointerType - Class to represent pointers
322 ///
323 class PointerType : public SequentialType {
324   friend class TypeMap<PointerValType, PointerType>;
325   PointerType(const PointerType &);                   // Do not implement
326   const PointerType &operator=(const PointerType &);  // Do not implement
327 protected:
328   // This should really be private, but it squelches a bogus warning
329   // from GCC to make them protected:  warning: `class PointerType' only 
330   // defines private constructors and has no friends
331
332   // Private ctor - Only can be created by a static member...
333   PointerType(const Type *ElType);
334
335 public:
336   /// PointerType::get - This is the only way to construct a new pointer type.
337   static PointerType *get(const Type *ElementType);
338
339   // Implement the AbstractTypeUser interface.
340   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
341   virtual void typeBecameConcrete(const DerivedType *AbsTy);
342
343   // Implement support type inquiry through isa, cast, and dyn_cast:
344   static inline bool classof(const PointerType *T) { return true; }
345   static inline bool classof(const Type *T) {
346     return T->getPrimitiveID() == PointerTyID;
347   }
348   static inline bool classof(const Value *V) {
349     return isa<Type>(V) && classof(cast<Type>(V));
350   }
351 };
352
353
354 /// OpaqueType - Class to represent abstract types
355 ///
356 class OpaqueType : public DerivedType {
357   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
358   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
359 protected:
360   /// This should really be private, but it squelches a bogus warning
361   /// from GCC to make them protected:  warning: `class OpaqueType' only 
362   /// defines private constructors and has no friends
363   ///
364   /// Private ctor - Only can be created by a static member...
365   OpaqueType();
366
367 public:
368   /// OpaqueType::get - Static factory method for the OpaqueType class...
369   ///
370   static OpaqueType *get() {
371     return new OpaqueType();           // All opaque types are distinct
372   }
373
374   // Implement the AbstractTypeUser interface.
375   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
376     abort();   // FIXME: this is not really an AbstractTypeUser!
377   }
378   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
379     abort();   // FIXME: this is not really an AbstractTypeUser!
380   }
381
382   // Implement support for type inquiry through isa, cast, and dyn_cast:
383   static inline bool classof(const OpaqueType *T) { return true; }
384   static inline bool classof(const Type *T) {
385     return T->getPrimitiveID() == OpaqueTyID;
386   }
387   static inline bool classof(const Value *V) {
388     return isa<Type>(V) && classof(cast<Type>(V));
389   }
390 };
391
392 } // End llvm namespace
393
394 #endif