Sprinkle liberally with comments, saute with doxygen until readable.
[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   ///
60   void dropAllTypeUses();
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 /// FunctionType - Class to represent function types
122 ///
123 class FunctionType : public DerivedType {
124   friend class TypeMap<FunctionValType, FunctionType>;
125   bool isVarArgs;
126
127   FunctionType(const FunctionType &);                   // Do not implement
128   const FunctionType &operator=(const FunctionType &);  // Do not implement
129 protected:
130   /// This should really be private, but it squelches a bogus warning
131   /// from GCC to make them protected:  warning: `class FunctionType' only 
132   /// defines private constructors and has no friends
133   ///
134   /// Private ctor - Only can be created by a static member...
135   ///
136   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
137                bool IsVarArgs);
138
139 public:
140   /// FunctionType::get - This static method is the primary way of constructing
141   /// a FunctionType
142   ///
143   static FunctionType *get(const Type *Result,
144                            const std::vector<const Type*> &Params,
145                            bool isVarArg);
146
147   inline bool isVarArg() const { return isVarArgs; }
148   inline const Type *getReturnType() const { return ContainedTys[0]; }
149
150   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
151   param_iterator param_begin() const { return ContainedTys.begin()+1; }
152   param_iterator param_end() const { return ContainedTys.end(); }
153
154   // Parameter type accessors...
155   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
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 ContainedTys.size()-1; }
161
162   // Implement the AbstractTypeUser interface.
163   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
164   virtual void typeBecameConcrete(const DerivedType *AbsTy);
165   
166   // Methods for support type inquiry through isa, cast, and dyn_cast:
167   static inline bool classof(const FunctionType *T) { return true; }
168   static inline bool classof(const Type *T) {
169     return T->getPrimitiveID() == FunctionTyID;
170   }
171   static inline bool classof(const Value *V) {
172     return isa<Type>(V) && classof(cast<Type>(V));
173   }
174 };
175
176
177 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
178 ///
179 class CompositeType : public DerivedType {
180 protected:
181   inline CompositeType(PrimitiveID id) : DerivedType(id) { }
182 public:
183
184   /// getTypeAtIndex - Given an index value into the type, return the type of
185   /// the element.
186   ///
187   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
188   virtual bool indexValid(const Value *V) const = 0;
189
190   // Methods for support type inquiry through isa, cast, and dyn_cast:
191   static inline bool classof(const CompositeType *T) { return true; }
192   static inline bool classof(const Type *T) {
193     return T->getPrimitiveID() == ArrayTyID || 
194            T->getPrimitiveID() == StructTyID ||
195            T->getPrimitiveID() == PointerTyID;
196   }
197   static inline bool classof(const Value *V) {
198     return isa<Type>(V) && classof(cast<Type>(V));
199   }
200 };
201
202
203 /// StructType - Class to represent struct types
204 ///
205 class StructType : public CompositeType {
206   friend class TypeMap<StructValType, StructType>;
207   StructType(const StructType &);                   // Do not implement
208   const StructType &operator=(const StructType &);  // Do not implement
209
210 protected:
211   /// This should really be private, but it squelches a bogus warning
212   /// from GCC to make them protected:  warning: `class StructType' only 
213   /// defines private constructors and has no friends
214   ///
215   /// Private ctor - Only can be created by a static member...
216   ///
217   StructType(const std::vector<const Type*> &Types);
218
219 public:
220   /// StructType::get - This static method is the primary way to create a
221   /// StructType.
222   ///
223   static StructType *get(const std::vector<const Type*> &Params);
224
225   // Iterator access to the elements
226   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
227   element_iterator element_begin() const { return ContainedTys.begin(); }
228   element_iterator element_end() const { return ContainedTys.end(); }
229
230   // Random access to the elements
231   unsigned getNumElements() const { return ContainedTys.size(); }
232   const Type *getElementType(unsigned N) const {
233     assert(N < ContainedTys.size() && "Element number out of range!");
234     return ContainedTys[N];
235   }
236
237   /// getTypeAtIndex - Given an index value into the type, return the type of
238   /// the element.  For a structure type, this must be a constant value...
239   ///
240   virtual const Type *getTypeAtIndex(const Value *V) const ;
241   virtual bool indexValid(const Value *V) const;
242
243   // Implement the AbstractTypeUser interface.
244   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
245   virtual void typeBecameConcrete(const DerivedType *AbsTy);
246
247   // Methods for support type inquiry through isa, cast, and dyn_cast:
248   static inline bool classof(const StructType *T) { return true; }
249   static inline bool classof(const Type *T) {
250     return T->getPrimitiveID() == StructTyID;
251   }
252   static inline bool classof(const Value *V) {
253     return isa<Type>(V) && classof(cast<Type>(V));
254   }
255 };
256
257
258 /// SequentialType - This is the superclass of the array and pointer type
259 /// classes.  Both of these represent "arrays" in memory.  The array type
260 /// represents a specifically sized array, pointer types are unsized/unknown
261 /// size arrays.  SequentialType holds the common features of both, which stem
262 /// from the fact that both lay their components out in memory identically.
263 ///
264 class SequentialType : public CompositeType {
265   SequentialType(const SequentialType &);                  // Do not implement!
266   const SequentialType &operator=(const SequentialType &); // Do not implement!
267 protected:
268   SequentialType(PrimitiveID TID, const Type *ElType) : CompositeType(TID) {
269     ContainedTys.reserve(1);
270     ContainedTys.push_back(PATypeHandle(ElType, this));
271   }
272
273 public:
274   inline const Type *getElementType() const { return ContainedTys[0]; }
275
276   /// getTypeAtIndex - Given an index value into the type, return the type of
277   /// the element.  For sequential types, there is only one subtype...
278   ///
279   virtual const Type *getTypeAtIndex(const Value *V) const {
280     return ContainedTys[0];
281   }
282   virtual bool indexValid(const Value *V) const {
283     return V->getType()->isInteger();
284   }
285
286   // Methods for support type inquiry through isa, cast, and dyn_cast:
287   static inline bool classof(const SequentialType *T) { return true; }
288   static inline bool classof(const Type *T) {
289     return T->getPrimitiveID() == ArrayTyID ||
290            T->getPrimitiveID() == PointerTyID;
291   }
292   static inline bool classof(const Value *V) {
293     return isa<Type>(V) && classof(cast<Type>(V));
294   }
295 };
296
297
298 /// ArrayType - Class to represent array types
299 ///
300 class ArrayType : public SequentialType {
301   friend class TypeMap<ArrayValType, ArrayType>;
302   unsigned NumElements;
303
304   ArrayType(const ArrayType &);                   // Do not implement
305   const ArrayType &operator=(const ArrayType &);  // Do not implement
306 protected:
307   /// This should really be private, but it squelches a bogus warning
308   /// from GCC to make them protected:  warning: `class ArrayType' only 
309   /// defines private constructors and has no friends
310   ///
311   /// Private ctor - Only can be created by a static member...
312   ///
313   ArrayType(const Type *ElType, unsigned NumEl);
314
315 public:
316   /// ArrayType::get - This static method is the primary way to construct an
317   /// ArrayType
318   ///
319   static ArrayType *get(const Type *ElementType, unsigned NumElements);
320
321   inline unsigned    getNumElements() const { return NumElements; }
322
323   // Implement the AbstractTypeUser interface.
324   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
325   virtual void typeBecameConcrete(const DerivedType *AbsTy);
326
327   // Methods for support type inquiry through isa, cast, and dyn_cast:
328   static inline bool classof(const ArrayType *T) { return true; }
329   static inline bool classof(const Type *T) {
330     return T->getPrimitiveID() == ArrayTyID;
331   }
332   static inline bool classof(const Value *V) {
333     return isa<Type>(V) && classof(cast<Type>(V));
334   }
335 };
336
337
338 /// PointerType - Class to represent pointers
339 ///
340 class PointerType : public SequentialType {
341   friend class TypeMap<PointerValType, PointerType>;
342   PointerType(const PointerType &);                   // Do not implement
343   const PointerType &operator=(const PointerType &);  // Do not implement
344 protected:
345   // This should really be private, but it squelches a bogus warning
346   // from GCC to make them protected:  warning: `class PointerType' only 
347   // defines private constructors and has no friends
348
349   // Private ctor - Only can be created by a static member...
350   PointerType(const Type *ElType);
351
352 public:
353   /// PointerType::get - This is the only way to construct a new pointer type.
354   static PointerType *get(const Type *ElementType);
355
356   // Implement the AbstractTypeUser interface.
357   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
358   virtual void typeBecameConcrete(const DerivedType *AbsTy);
359
360   // Implement support type inquiry through isa, cast, and dyn_cast:
361   static inline bool classof(const PointerType *T) { return true; }
362   static inline bool classof(const Type *T) {
363     return T->getPrimitiveID() == PointerTyID;
364   }
365   static inline bool classof(const Value *V) {
366     return isa<Type>(V) && classof(cast<Type>(V));
367   }
368 };
369
370
371 /// OpaqueType - Class to represent abstract types
372 ///
373 class OpaqueType : public DerivedType {
374   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
375   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
376 protected:
377   /// This should really be private, but it squelches a bogus warning
378   /// from GCC to make them protected:  warning: `class OpaqueType' only 
379   /// defines private constructors and has no friends
380   ///
381   /// Private ctor - Only can be created by a static member...
382   OpaqueType();
383
384 public:
385   /// OpaqueType::get - Static factory method for the OpaqueType class...
386   ///
387   static OpaqueType *get() {
388     return new OpaqueType();           // All opaque types are distinct
389   }
390
391   // Implement the AbstractTypeUser interface.
392   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
393     abort();   // FIXME: this is not really an AbstractTypeUser!
394   }
395   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
396     abort();   // FIXME: this is not really an AbstractTypeUser!
397   }
398
399   // Implement support for type inquiry through isa, cast, and dyn_cast:
400   static inline bool classof(const OpaqueType *T) { return true; }
401   static inline bool classof(const Type *T) {
402     return T->getPrimitiveID() == OpaqueTyID;
403   }
404   static inline bool classof(const Value *V) {
405     return isa<Type>(V) && classof(cast<Type>(V));
406   }
407 };
408
409
410 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
411 // These are defined here because they MUST be inlined, yet are dependent on 
412 // the definition of the Type class.  Of course Type derives from Value, which
413 // contains an AbstractTypeUser instance, so there is no good way to factor out
414 // the code.  Hence this bit of uglyness.
415 //
416 inline void PATypeHandle::addUser() {
417   assert(Ty && "Type Handle has a null type!");
418   if (Ty->isAbstract())
419     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
420 }
421 inline void PATypeHandle::removeUser() {
422   if (Ty->isAbstract())
423     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
424 }
425
426 inline void PATypeHandle::removeUserFromConcrete() {
427   if (!Ty->isAbstract())
428     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
429 }
430
431 // Define inline methods for PATypeHolder...
432
433 inline void PATypeHolder::addRef() {
434   if (Ty->isAbstract())
435     cast<DerivedType>(Ty)->addRef();
436 }
437
438 inline void PATypeHolder::dropRef() {
439   if (Ty->isAbstract())
440     cast<DerivedType>(Ty)->dropRef();
441 }
442
443 /// get - This implements the forwarding part of the union-find algorithm for
444 /// abstract types.  Before every access to the Type*, we check to see if the
445 /// type we are pointing to is forwarding to a new type.  If so, we drop our
446 /// reference to the type.
447 ///
448 inline const Type* PATypeHolder::get() const {
449   const Type *NewTy = Ty->getForwardedType();
450   if (!NewTy) return Ty;
451   return *const_cast<PATypeHolder*>(this) = NewTy;
452 }
453
454 } // End llvm namespace
455
456 #endif