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