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