There is no reason for Value to be an AbstractTypeUser. This just makes things
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
2 //
3 // This file contains the declarations of classes that represent "derived 
4 // types".  These are things like "arrays of x" or "structure of x, y, z" or
5 // "method returning x taking (y,z) as parameters", etc...
6 //
7 // The implementations of these classes live in the Type.cpp file.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
13
14 #include "llvm/Type.h"
15
16 template<class ValType, class TypeClass> class TypeMap;
17 class FunctionValType;
18 class ArrayValType;
19 class StructValType;
20 class PointerValType;
21
22 class DerivedType : public Type, public AbstractTypeUser {
23   char isRefining;                                   // Used for recursive types
24
25   // AbstractTypeUsers - Implement a list of the users that need to be notified
26   // if I am a type, and I get resolved into a more concrete type.
27   //
28   ///// FIXME: kill mutable nonsense when Type's are not const
29   mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
30
31 protected:
32   inline DerivedType(PrimitiveID id) : Type("", id) {
33     isRefining = 0;
34   }
35   ~DerivedType() {
36     assert(AbstractTypeUsers.empty());
37   }
38
39   // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
40   // has been refined a bit.  The pointer is still valid and still should be
41   // used, but the subtypes have changed.
42   //
43   void typeIsRefined();
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.  This also removes the
48   // type from the internal tables of available types.
49   virtual void dropAllTypeUses(bool inMap) = 0;
50   
51
52   void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
53
54 public:
55
56   //===--------------------------------------------------------------------===//
57   // Abstract Type handling methods - These types have special lifetimes, which
58   // are managed by (add|remove)AbstractTypeUser. See comments in
59   // AbstractTypeUser.h for more information.
60
61   // addAbstractTypeUser - Notify an abstract type that there is a new user of
62   // it.  This function is called primarily by the PATypeHandle class.
63   //
64   void addAbstractTypeUser(AbstractTypeUser *U) const;
65
66   // removeAbstractTypeUser - Notify an abstract type that a user of the class
67   // no longer has a handle to the type.  This function is called primarily by
68   // the PATypeHandle class.  When there are no users of the abstract type, it
69   // is annihilated, because there is no way to get a reference to it ever
70   // again.
71   //
72   void removeAbstractTypeUser(AbstractTypeUser *U) const;
73
74   // refineAbstractTypeTo - This function is used to when it is discovered that
75   // the 'this' abstract type is actually equivalent to the NewType specified.
76   // This causes all users of 'this' to switch to reference the more concrete
77   // type NewType and for 'this' to be deleted.
78   //
79   void refineAbstractTypeTo(const Type *NewType) {
80     refineAbstractTypeToInternal(NewType, true);
81   }
82
83   void dump() const { Value::dump(); }
84
85   // Methods for support type inquiry through isa, cast, and dyn_cast:
86   static inline bool classof(const DerivedType *T) { return true; }
87   static inline bool classof(const Type *T) {
88     return T->isDerivedType();
89   }
90   static inline bool classof(const Value *V) {
91     return isa<Type>(V) && classof(cast<Type>(V));
92   }
93 };
94
95
96
97
98 struct FunctionType : public DerivedType {
99   typedef std::vector<PATypeHandle> ParamTypes;
100   friend class TypeMap<FunctionValType, FunctionType>;
101 private:
102   PATypeHandle ResultType;
103   ParamTypes ParamTys;
104   bool isVarArgs;
105
106   FunctionType(const FunctionType &);                   // Do not implement
107   const FunctionType &operator=(const FunctionType &);  // Do not implement
108 protected:
109   // This should really be private, but it squelches a bogus warning
110   // from GCC to make them protected:  warning: `class FunctionType' only 
111   // defines private constructors and has no friends
112
113   // Private ctor - Only can be created by a static member...
114   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
115                bool IsVarArgs);
116
117   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
118   // another (more concrete) type, we must eliminate all references to other
119   // types, to avoid some circular reference problems.  This also removes the
120   // type from the internal tables of available types.
121   virtual void dropAllTypeUses(bool inMap);
122
123 public:
124
125   inline bool isVarArg() const { return isVarArgs; }
126   inline const Type *getReturnType() const { return ResultType; }
127   inline const ParamTypes &getParamTypes() const { return ParamTys; }
128
129   // Parameter type accessors...
130   const Type *getParamType(unsigned i) const { return ParamTys[i]; }
131
132   // getNumParams - Return the number of fixed parameters this function type
133   // requires.  This does not consider varargs.
134   //
135   unsigned getNumParams() const { return ParamTys.size(); }
136
137
138   virtual const Type *getContainedType(unsigned i) const {
139     return i == 0 ? ResultType : 
140                     (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
141   }
142   virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
143
144   // refineAbstractType - Called when a contained type is found to be more
145   // concrete - this could potentially change us from an abstract type to a
146   // concrete type.
147   //
148   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
149
150   static FunctionType *get(const Type *Result,
151                            const std::vector<const Type*> &Params,
152                            bool isVarArg);
153
154   // Methods for support type inquiry through isa, cast, and dyn_cast:
155   static inline bool classof(const FunctionType *T) { return true; }
156   static inline bool classof(const Type *T) {
157     return T->getPrimitiveID() == FunctionTyID;
158   }
159   static inline bool classof(const Value *V) {
160     return isa<Type>(V) && classof(cast<Type>(V));
161   }
162 };
163
164
165 // CompositeType - Common super class of ArrayType, StructType, and PointerType
166 //
167 class CompositeType : public DerivedType {
168 protected:
169   inline CompositeType(PrimitiveID id) : DerivedType(id) { }
170 public:
171
172   // getTypeAtIndex - Given an index value into the type, return the type of the
173   // element.
174   //
175   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
176   virtual bool indexValid(const Value *V) const = 0;
177
178   // getIndexType - Return the type required of indices for this composite.
179   // For structures, this is ubyte, for arrays, this is uint
180   //
181   virtual const Type *getIndexType() const = 0;
182
183
184   // Methods for support type inquiry through isa, cast, and dyn_cast:
185   static inline bool classof(const CompositeType *T) { return true; }
186   static inline bool classof(const Type *T) {
187     return T->getPrimitiveID() == ArrayTyID || 
188            T->getPrimitiveID() == StructTyID ||
189            T->getPrimitiveID() == PointerTyID;
190   }
191   static inline bool classof(const Value *V) {
192     return isa<Type>(V) && classof(cast<Type>(V));
193   }
194 };
195
196
197 struct StructType : public CompositeType {
198   friend class TypeMap<StructValType, StructType>;
199   typedef std::vector<PATypeHandle> ElementTypes;
200
201 private:
202   ElementTypes ETypes;                              // Element types of struct
203
204   StructType(const StructType &);                   // Do not implement
205   const StructType &operator=(const StructType &);  // Do not implement
206
207 protected:
208   // This should really be private, but it squelches a bogus warning
209   // from GCC to make them protected:  warning: `class StructType' only 
210   // defines private constructors and has no friends
211
212   // Private ctor - Only can be created by a static member...
213   StructType(const std::vector<const Type*> &Types);
214
215   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
216   // another (more concrete) type, we must eliminate all references to other
217   // types, to avoid some circular reference problems.  This also removes the
218   // type from the internal tables of available types.
219   virtual void dropAllTypeUses(bool inMap);
220   
221 public:
222   inline const ElementTypes &getElementTypes() const { return ETypes; }
223
224   virtual const Type *getContainedType(unsigned i) const { 
225     return i < ETypes.size() ? ETypes[i].get() : 0;
226   }
227   virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
228
229   // getTypeAtIndex - Given an index value into the type, return the type of the
230   // element.  For a structure type, this must be a constant value...
231   //
232   virtual const Type *getTypeAtIndex(const Value *V) const ;
233   virtual bool indexValid(const Value *V) const;
234
235   // getIndexType - Return the type required of indices for this composite.
236   // For structures, this is ubyte, for arrays, this is uint
237   //
238   virtual const Type *getIndexType() const { return Type::UByteTy; }
239
240   // refineAbstractType - Called when a contained type is found to be more
241   // concrete - this could potentially change us from an abstract type to a
242   // concrete type.
243   //
244   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
245
246   static StructType *get(const std::vector<const Type*> &Params);
247
248   // Methods for support type inquiry through isa, cast, and dyn_cast:
249   static inline bool classof(const StructType *T) { return true; }
250   static inline bool classof(const Type *T) {
251     return T->getPrimitiveID() == StructTyID;
252   }
253   static inline bool classof(const Value *V) {
254     return isa<Type>(V) && classof(cast<Type>(V));
255   }
256 };
257
258
259 // SequentialType - This is the superclass of the array and pointer type
260 // classes.  Both of these represent "arrays" in memory.  The array type
261 // represents a specifically sized array, pointer types are unsized/unknown size
262 // arrays.  SequentialType holds the common features of both, which stem from
263 // the fact that both lay their components out in memory identically.
264 //
265 class SequentialType : public CompositeType {
266   SequentialType(const SequentialType &);                  // Do not implement!
267   const SequentialType &operator=(const SequentialType &); // Do not implement!
268 protected:
269   PATypeHandle ElementType;
270
271   SequentialType(PrimitiveID TID, const Type *ElType)
272     : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
273   }
274
275 public:
276   inline const Type *getElementType() const { return ElementType; }
277
278   virtual const Type *getContainedType(unsigned i) const { 
279     return i == 0 ? ElementType.get() : 0;
280   }
281   virtual unsigned getNumContainedTypes() const { return 1; }
282
283   // getTypeAtIndex - Given an index value into the type, return the type of the
284   // element.  For sequential types, there is only one subtype...
285   //
286   virtual const Type *getTypeAtIndex(const Value *V) const {
287     return ElementType.get();
288   }
289   virtual bool indexValid(const Value *V) const {
290     return V->getType() == Type::LongTy;   // Must be a 'long' index
291   }
292
293   // getIndexType() - Return the type required of indices for this composite.
294   // For structures, this is ubyte, for arrays, this is uint
295   //
296   virtual const Type *getIndexType() const { return Type::LongTy; }
297
298   // Methods for support type inquiry through isa, cast, and dyn_cast:
299   static inline bool classof(const SequentialType *T) { return true; }
300   static inline bool classof(const Type *T) {
301     return T->getPrimitiveID() == ArrayTyID ||
302            T->getPrimitiveID() == PointerTyID;
303   }
304   static inline bool classof(const Value *V) {
305     return isa<Type>(V) && classof(cast<Type>(V));
306   }
307 };
308
309
310 class ArrayType : public SequentialType {
311   friend class TypeMap<ArrayValType, ArrayType>;
312   unsigned NumElements;
313
314   ArrayType(const ArrayType &);                   // Do not implement
315   const ArrayType &operator=(const ArrayType &);  // Do not implement
316 protected:
317   // This should really be private, but it squelches a bogus warning
318   // from GCC to make them protected:  warning: `class ArrayType' only 
319   // defines private constructors and has no friends
320
321   // Private ctor - Only can be created by a static member...
322   ArrayType(const Type *ElType, unsigned NumEl);
323
324   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
325   // another (more concrete) type, we must eliminate all references to other
326   // types, to avoid some circular reference problems.  This also removes the
327   // type from the internal tables of available types.
328   virtual void dropAllTypeUses(bool inMap);
329
330 public:
331   inline unsigned    getNumElements() const { return NumElements; }
332
333   // refineAbstractType - Called when a contained type is found to be more
334   // concrete - this could potentially change us from an abstract type to a
335   // concrete type.
336   //
337   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
338
339   static ArrayType *get(const Type *ElementType, unsigned NumElements);
340
341   // Methods for support type inquiry through isa, cast, and dyn_cast:
342   static inline bool classof(const ArrayType *T) { return true; }
343   static inline bool classof(const Type *T) {
344     return T->getPrimitiveID() == ArrayTyID;
345   }
346   static inline bool classof(const Value *V) {
347     return isa<Type>(V) && classof(cast<Type>(V));
348   }
349 };
350
351
352
353 class PointerType : public SequentialType {
354   friend class TypeMap<PointerValType, PointerType>;
355   PointerType(const PointerType &);                   // Do not implement
356   const PointerType &operator=(const PointerType &);  // Do not implement
357 protected:
358   // This should really be private, but it squelches a bogus warning
359   // from GCC to make them protected:  warning: `class PointerType' only 
360   // defines private constructors and has no friends
361
362   // Private ctor - Only can be created by a static member...
363   PointerType(const Type *ElType);
364
365   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
366   // another (more concrete) type, we must eliminate all references to other
367   // types, to avoid some circular reference problems.  This also removes the
368   // type from the internal tables of available types.
369   virtual void dropAllTypeUses(bool inMap);
370 public:
371   // PointerType::get - Named constructor for pointer types...
372   static PointerType *get(const Type *ElementType);
373
374   // refineAbstractType - Called when a contained type is found to be more
375   // concrete - this could potentially change us from an abstract type to a
376   // concrete type.
377   //
378   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
379
380   // Methods for support type inquiry through isa, cast, and dyn_cast:
381   static inline bool classof(const PointerType *T) { return true; }
382   static inline bool classof(const Type *T) {
383     return T->getPrimitiveID() == PointerTyID;
384   }
385   static inline bool classof(const Value *V) {
386     return isa<Type>(V) && classof(cast<Type>(V));
387   }
388 };
389
390
391 class OpaqueType : public DerivedType {
392   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
393   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
394 protected:
395   // This should really be private, but it squelches a bogus warning
396   // from GCC to make them protected:  warning: `class OpaqueType' only 
397   // defines private constructors and has no friends
398
399   // Private ctor - Only can be created by a static member...
400   OpaqueType();
401
402   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
403   // another (more concrete) type, we must eliminate all references to other
404   // types, to avoid some circular reference problems.
405   virtual void dropAllTypeUses(bool inMap) {}  // No type uses
406
407 public:
408
409   // get - Static factory method for the OpaqueType class...
410   static OpaqueType *get() {
411     return new OpaqueType();           // All opaque types are distinct
412   }
413
414   // refineAbstractType - Called when a contained type is found to be more
415   // concrete - this could potentially change us from an abstract type to a
416   // concrete type.
417   //
418   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
419     // This class never uses other types!
420     abort();
421   }
422
423
424   // Methods for support type inquiry through isa, cast, and dyn_cast:
425   static inline bool classof(const OpaqueType *T) { return true; }
426   static inline bool classof(const Type *T) {
427     return T->getPrimitiveID() == OpaqueTyID;
428   }
429   static inline bool classof(const Value *V) {
430     return isa<Type>(V) && classof(cast<Type>(V));
431   }
432 };
433
434
435 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
436 // These are defined here because they MUST be inlined, yet are dependent on 
437 // the definition of the Type class.  Of course Type derives from Value, which
438 // contains an AbstractTypeUser instance, so there is no good way to factor out
439 // the code.  Hence this bit of uglyness.
440 //
441 inline void PATypeHandle::addUser() {
442   assert(Ty && "Type Handle has a null type!");
443   if (Ty->isAbstract())
444     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
445 }
446 inline void PATypeHandle::removeUser() {
447   if (Ty->isAbstract())
448     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
449 }
450
451 inline void PATypeHandle::removeUserFromConcrete() {
452   if (!Ty->isAbstract())
453     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
454 }
455
456 #endif