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