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