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