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