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