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