9fdff68b402b68f4667909c1e7a2869f4d34e339
[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 Types are not const
43   mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
44
45 protected:
46   DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {}
47   ~DerivedType() {
48     assert(AbstractTypeUsers.empty());
49   }
50
51   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
52   /// that the current type has transitioned from being abstract to being
53   /// concrete.
54   ///
55   void notifyUsesThatTypeBecameConcrete();
56
57   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
58   // another (more concrete) type, we must eliminate all references to other
59   // types, to avoid some circular reference problems.
60   virtual void dropAllTypeUses() = 0;
61   
62 public:
63
64   //===--------------------------------------------------------------------===//
65   // Abstract Type handling methods - These types have special lifetimes, which
66   // are managed by (add|remove)AbstractTypeUser. See comments in
67   // AbstractTypeUser.h for more information.
68
69   // addAbstractTypeUser - Notify an abstract type that there is a new user of
70   // it.  This function is called primarily by the PATypeHandle class.
71   //
72   void addAbstractTypeUser(AbstractTypeUser *U) const {
73     assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
74     AbstractTypeUsers.push_back(U);
75   }
76
77   // removeAbstractTypeUser - Notify an abstract type that a user of the class
78   // no longer has a handle to the type.  This function is called primarily by
79   // the PATypeHandle class.  When there are no users of the abstract type, it
80   // is annihilated, because there is no way to get a reference to it ever
81   // again.
82   //
83   void removeAbstractTypeUser(AbstractTypeUser *U) const;
84
85   // refineAbstractTypeTo - This function is used to when it is discovered that
86   // the 'this' abstract type is actually equivalent to the NewType specified.
87   // This causes all users of 'this' to switch to reference the more concrete
88   // type NewType and for 'this' to be deleted.
89   //
90   void refineAbstractTypeTo(const Type *NewType);
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 class FunctionType : public DerivedType {
124   friend class TypeMap<FunctionValType, FunctionType>;
125   PATypeHandle ResultType;
126   std::vector<PATypeHandle> ParamTys;
127   bool isVarArgs;
128
129   FunctionType(const FunctionType &);                   // Do not implement
130   const FunctionType &operator=(const FunctionType &);  // Do not implement
131 protected:
132   // This should really be private, but it squelches a bogus warning
133   // from GCC to make them protected:  warning: `class FunctionType' only 
134   // defines private constructors and has no friends
135
136   // Private ctor - Only can be created by a static member...
137   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
138                bool IsVarArgs);
139
140   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
141   // another (more concrete) type, we must eliminate all references to other
142   // types, to avoid some circular reference problems.
143   virtual void dropAllTypeUses();
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
155   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
156   param_iterator param_begin() const { return ParamTys.begin(); }
157   param_iterator param_end() const { return ParamTys.end(); }
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 class StructType : public CompositeType {
215   friend class TypeMap<StructValType, StructType>;
216   std::vector<PATypeHandle> ETypes;                 // Element types of struct
217
218   StructType(const StructType &);                   // Do not implement
219   const StructType &operator=(const StructType &);  // Do not implement
220
221 protected:
222   // This should really be private, but it squelches a bogus warning
223   // from GCC to make them protected:  warning: `class StructType' only 
224   // defines private constructors and has no friends
225
226   // Private ctor - Only can be created by a static member...
227   StructType(const std::vector<const Type*> &Types);
228
229   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
230   // another (more concrete) type, we must eliminate all references to other
231   // types, to avoid some circular reference problems.
232   virtual void dropAllTypeUses();
233   
234 public:
235   /// StructType::get - This static method is the primary way to create a
236   /// StructType.
237   static StructType *get(const std::vector<const Type*> &Params);
238
239   // Iterator access to the elements
240   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
241   element_iterator element_begin() const { return ETypes.begin(); }
242   element_iterator element_end() const { return ETypes.end(); }
243
244   // Random access to the elements
245   unsigned getNumElements() const { return ETypes.size(); }
246   const Type *getElementType(unsigned N) const {
247     assert(N < ETypes.size() && "Element number out of range!");
248     return ETypes[N];
249   }
250
251   virtual const Type *getContainedType(unsigned i) const { 
252     return ETypes[i].get();
253   }
254   virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
255
256   // getTypeAtIndex - Given an index value into the type, return the type of the
257   // element.  For a structure type, this must be a constant value...
258   //
259   virtual const Type *getTypeAtIndex(const Value *V) const ;
260   virtual bool indexValid(const Value *V) const;
261
262   // Implement the AbstractTypeUser interface.
263   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
264   virtual void typeBecameConcrete(const DerivedType *AbsTy);
265
266   // Methods for support type inquiry through isa, cast, and dyn_cast:
267   static inline bool classof(const StructType *T) { return true; }
268   static inline bool classof(const Type *T) {
269     return T->getPrimitiveID() == StructTyID;
270   }
271   static inline bool classof(const Value *V) {
272     return isa<Type>(V) && classof(cast<Type>(V));
273   }
274 };
275
276
277 // SequentialType - This is the superclass of the array and pointer type
278 // classes.  Both of these represent "arrays" in memory.  The array type
279 // represents a specifically sized array, pointer types are unsized/unknown size
280 // arrays.  SequentialType holds the common features of both, which stem from
281 // the fact that both lay their components out in memory identically.
282 //
283 class SequentialType : public CompositeType {
284   SequentialType(const SequentialType &);                  // Do not implement!
285   const SequentialType &operator=(const SequentialType &); // Do not implement!
286 protected:
287   PATypeHandle ElementType;
288
289   SequentialType(PrimitiveID TID, const Type *ElType)
290     : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
291   }
292
293 public:
294   inline const Type *getElementType() const { return ElementType; }
295
296   virtual const Type *getContainedType(unsigned i) const { 
297     return ElementType.get();
298   }
299   virtual unsigned getNumContainedTypes() const { return 1; }
300
301   // getTypeAtIndex - Given an index value into the type, return the type of the
302   // element.  For sequential types, there is only one subtype...
303   //
304   virtual const Type *getTypeAtIndex(const Value *V) const {
305     return ElementType.get();
306   }
307   virtual bool indexValid(const Value *V) const {
308     return V->getType()->isInteger();
309   }
310
311   // Methods for support type inquiry through isa, cast, and dyn_cast:
312   static inline bool classof(const SequentialType *T) { return true; }
313   static inline bool classof(const Type *T) {
314     return T->getPrimitiveID() == ArrayTyID ||
315            T->getPrimitiveID() == PointerTyID;
316   }
317   static inline bool classof(const Value *V) {
318     return isa<Type>(V) && classof(cast<Type>(V));
319   }
320 };
321
322
323 class ArrayType : public SequentialType {
324   friend class TypeMap<ArrayValType, ArrayType>;
325   unsigned NumElements;
326
327   ArrayType(const ArrayType &);                   // Do not implement
328   const ArrayType &operator=(const ArrayType &);  // Do not implement
329 protected:
330   // This should really be private, but it squelches a bogus warning
331   // from GCC to make them protected:  warning: `class ArrayType' only 
332   // defines private constructors and has no friends
333
334   // Private ctor - Only can be created by a static member...
335   ArrayType(const Type *ElType, unsigned NumEl);
336
337   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
338   // another (more concrete) type, we must eliminate all references to other
339   // types, to avoid some circular reference problems.
340   virtual void dropAllTypeUses();
341
342 public:
343   /// ArrayType::get - This static method is the primary way to construct an
344   /// ArrayType
345   static ArrayType *get(const Type *ElementType, unsigned NumElements);
346
347   inline unsigned    getNumElements() const { return NumElements; }
348
349   // Implement the AbstractTypeUser interface.
350   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
351   virtual void typeBecameConcrete(const DerivedType *AbsTy);
352
353   // Methods for support type inquiry through isa, cast, and dyn_cast:
354   static inline bool classof(const ArrayType *T) { return true; }
355   static inline bool classof(const Type *T) {
356     return T->getPrimitiveID() == ArrayTyID;
357   }
358   static inline bool classof(const Value *V) {
359     return isa<Type>(V) && classof(cast<Type>(V));
360   }
361 };
362
363
364
365 class PointerType : public SequentialType {
366   friend class TypeMap<PointerValType, PointerType>;
367   PointerType(const PointerType &);                   // Do not implement
368   const PointerType &operator=(const PointerType &);  // Do not implement
369 protected:
370   // This should really be private, but it squelches a bogus warning
371   // from GCC to make them protected:  warning: `class PointerType' only 
372   // defines private constructors and has no friends
373
374   // Private ctor - Only can be created by a static member...
375   PointerType(const Type *ElType);
376
377   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
378   // another (more concrete) type, we must eliminate all references to other
379   // types, to avoid some circular reference problems.
380   virtual void dropAllTypeUses();
381 public:
382   /// PointerType::get - This is the only way to construct a new pointer type.
383   static PointerType *get(const Type *ElementType);
384
385   // Implement the AbstractTypeUser interface.
386   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
387   virtual void typeBecameConcrete(const DerivedType *AbsTy);
388
389   // Implement support type inquiry through isa, cast, and dyn_cast:
390   static inline bool classof(const PointerType *T) { return true; }
391   static inline bool classof(const Type *T) {
392     return T->getPrimitiveID() == PointerTyID;
393   }
394   static inline bool classof(const Value *V) {
395     return isa<Type>(V) && classof(cast<Type>(V));
396   }
397 };
398
399
400 class OpaqueType : public DerivedType {
401   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
402   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
403 protected:
404   // This should really be private, but it squelches a bogus warning
405   // from GCC to make them protected:  warning: `class OpaqueType' only 
406   // defines private constructors and has no friends
407
408   // Private ctor - Only can be created by a static member...
409   OpaqueType();
410
411   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
412   // another (more concrete) type, we must eliminate all references to other
413   // types, to avoid some circular reference problems.
414   virtual void dropAllTypeUses() {
415     // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
416   }  // No type uses
417
418 public:
419   // OpaqueType::get - Static factory method for the OpaqueType class...
420   static OpaqueType *get() {
421     return new OpaqueType();           // All opaque types are distinct
422   }
423
424   // Implement the AbstractTypeUser interface.
425   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
426     abort();   // FIXME: this is not really an AbstractTypeUser!
427   }
428   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
429     abort();   // FIXME: this is not really an AbstractTypeUser!
430   }
431
432   // Implement support for type inquiry through isa, cast, and dyn_cast:
433   static inline bool classof(const OpaqueType *T) { return true; }
434   static inline bool classof(const Type *T) {
435     return T->getPrimitiveID() == OpaqueTyID;
436   }
437   static inline bool classof(const Value *V) {
438     return isa<Type>(V) && classof(cast<Type>(V));
439   }
440 };
441
442
443 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
444 // These are defined here because they MUST be inlined, yet are dependent on 
445 // the definition of the Type class.  Of course Type derives from Value, which
446 // contains an AbstractTypeUser instance, so there is no good way to factor out
447 // the code.  Hence this bit of uglyness.
448 //
449 inline void PATypeHandle::addUser() {
450   assert(Ty && "Type Handle has a null type!");
451   if (Ty->isAbstract())
452     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
453 }
454 inline void PATypeHandle::removeUser() {
455   if (Ty->isAbstract())
456     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
457 }
458
459 inline void PATypeHandle::removeUserFromConcrete() {
460   if (!Ty->isAbstract())
461     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
462 }
463
464 // Define inline methods for PATypeHolder...
465
466 inline void PATypeHolder::addRef() {
467   if (Ty->isAbstract())
468     cast<DerivedType>(Ty)->addRef();
469 }
470
471 inline void PATypeHolder::dropRef() {
472   if (Ty->isAbstract())
473     cast<DerivedType>(Ty)->dropRef();
474 }
475
476 /// get - This implements the forwarding part of the union-find algorithm for
477 /// abstract types.  Before every access to the Type*, we check to see if the
478 /// type we are pointing to is forwarding to a new type.  If so, we drop our
479 /// reference to the type.
480 inline const Type* PATypeHolder::get() const {
481   const Type *NewTy = Ty->getForwardedType();
482   if (!NewTy) return Ty;
483   return *const_cast<PATypeHolder*>(this) = NewTy;
484 }
485
486 } // End llvm namespace
487
488 #endif