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