Reshuffling of APIs
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ----*- C++ -*--=//
2 //
3 // This file contains the declarations of classes that represent "derived 
4 // types".  These are things like "arrays of x" or "structure of x, y, z" or
5 // "method returning x taking (y,z) as parameters", etc...
6 //
7 // The implementations of these classes live in the Type.cpp file.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
13
14 #include "llvm/Type.h"
15
16 class DerivedType : public Type {
17   char isRefining;                                   // Used for recursive types
18
19   // AbstractTypeUsers - Implement a list of the users that need to be notified
20   // if I am a type, and I get resolved into a more concrete type.
21   //
22   ///// FIXME: kill mutable nonsense when Type's are not const
23   mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
24
25 protected:
26   inline DerivedType(PrimitiveID id) : Type("", id) {
27     isRefining = 0;
28   }
29   ~DerivedType() {
30     assert(AbstractTypeUsers.empty());
31   }
32
33   // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
34   // has been refined a bit.  The pointer is still valid and still should be
35   // used, but the subtypes have changed.
36   //
37   void typeIsRefined();
38
39   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
40   // another (more concrete) type, we must eliminate all references to other
41   // types, to avoid some circular reference problems.  This also removes the
42   // type from the internal tables of available types.
43   virtual void dropAllTypeUses(bool inMap) = 0;
44   
45
46   void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
47
48 public:
49
50   //===--------------------------------------------------------------------===//
51   // Abstract Type handling methods - These types have special lifetimes, which
52   // are managed by (add|remove)AbstractTypeUser. See comments in
53   // AbstractTypeUser.h for more information.
54
55   // addAbstractTypeUser - Notify an abstract type that there is a new user of
56   // it.  This function is called primarily by the PATypeHandle class.
57   //
58   void addAbstractTypeUser(AbstractTypeUser *U) const;
59
60   // removeAbstractTypeUser - Notify an abstract type that a user of the class
61   // no longer has a handle to the type.  This function is called primarily by
62   // the PATypeHandle class.  When there are no users of the abstract type, it
63   // is annihilated, because there is no way to get a reference to it ever
64   // again.
65   //
66   void removeAbstractTypeUser(AbstractTypeUser *U) const;
67
68   // refineAbstractTypeTo - This function is used to when it is discovered that
69   // the 'this' abstract type is actually equivalent to the NewType specified.
70   // This causes all users of 'this' to switch to reference the more concrete
71   // type NewType and for 'this' to be deleted.
72   //
73   void refineAbstractTypeTo(const Type *NewType) {
74     refineAbstractTypeToInternal(NewType, true);
75   }
76
77   // Methods for support type inquiry through isa, cast, and dyn_cast:
78   static inline bool classof(const DerivedType *T) { return true; }
79   static inline bool classof(const Type *T) {
80     return T->isDerivedType();
81   }
82   static inline bool classof(const Value *V) {
83     return isa<Type>(V) && classof(cast<Type>(V));
84   }
85 };
86
87
88
89
90 struct FunctionType : public DerivedType {
91   typedef std::vector<PATypeHandle> ParamTypes;
92 private:
93   PATypeHandle ResultType;
94   ParamTypes ParamTys;
95   bool isVarArgs;
96
97   FunctionType(const FunctionType &);                   // Do not implement
98   const FunctionType &operator=(const FunctionType &);  // Do not implement
99 protected:
100   // This should really be private, but it squelches a bogus warning
101   // from GCC to make them protected:  warning: `class FunctionType' only 
102   // defines private constructors and has no friends
103
104   // Private ctor - Only can be created by a static member...
105   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
106                bool IsVarArgs);
107
108   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
109   // another (more concrete) type, we must eliminate all references to other
110   // types, to avoid some circular reference problems.  This also removes the
111   // type from the internal tables of available types.
112   virtual void dropAllTypeUses(bool inMap);
113
114 public:
115
116   inline bool isVarArg() const { return isVarArgs; }
117   inline const Type *getReturnType() const { return ResultType; }
118   inline const ParamTypes &getParamTypes() const { return ParamTys; }
119
120   // Parameter type accessors...
121   const Type *getParamType(unsigned i) const { return ParamTys[i]; }
122
123   // getNumParams - Return the number of fixed parameters this function type
124   // requires.  This does not consider varargs.
125   //
126   unsigned getNumParams() const { return ParamTys.size(); }
127
128
129   virtual const Type *getContainedType(unsigned i) const {
130     return i == 0 ? ResultType : 
131                     (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
132   }
133   virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
134
135   // refineAbstractType - Called when a contained type is found to be more
136   // concrete - this could potentially change us from an abstract type to a
137   // concrete type.
138   //
139   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
140
141   static FunctionType *get(const Type *Result,
142                            const std::vector<const Type*> &Params,
143                            bool isVarArg);
144
145
146   // Methods for support type inquiry through isa, cast, and dyn_cast:
147   static inline bool classof(const FunctionType *T) { return true; }
148   static inline bool classof(const Type *T) {
149     return T->getPrimitiveID() == FunctionTyID;
150   }
151   static inline bool classof(const Value *V) {
152     return isa<Type>(V) && classof(cast<Type>(V));
153   }
154 };
155
156
157 // CompositeType - Common super class of ArrayType, StructType, and PointerType
158 //
159 class CompositeType : public DerivedType {
160 protected:
161   inline CompositeType(PrimitiveID id) : DerivedType(id) { }
162 public:
163
164   // getTypeAtIndex - Given an index value into the type, return the type of the
165   // element.
166   //
167   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
168   virtual bool indexValid(const Value *V) const = 0;
169
170   // getIndexType - Return the type required of indices for this composite.
171   // For structures, this is ubyte, for arrays, this is uint
172   //
173   virtual const Type *getIndexType() const = 0;
174
175
176   // Methods for support type inquiry through isa, cast, and dyn_cast:
177   static inline bool classof(const CompositeType *T) { return true; }
178   static inline bool classof(const Type *T) {
179     return T->getPrimitiveID() == ArrayTyID || 
180            T->getPrimitiveID() == StructTyID ||
181            T->getPrimitiveID() == PointerTyID;
182   }
183   static inline bool classof(const Value *V) {
184     return isa<Type>(V) && classof(cast<Type>(V));
185   }
186 };
187
188
189 class StructType : public CompositeType {
190 public:
191   typedef std::vector<PATypeHandle> ElementTypes;
192
193 private:
194   ElementTypes ETypes;                              // Element types of struct
195
196   StructType(const StructType &);                   // Do not implement
197   const StructType &operator=(const StructType &);  // Do not implement
198
199 protected:
200   // This should really be private, but it squelches a bogus warning
201   // from GCC to make them protected:  warning: `class StructType' only 
202   // defines private constructors and has no friends
203
204   // Private ctor - Only can be created by a static member...
205   StructType(const std::vector<const Type*> &Types);
206
207   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
208   // another (more concrete) type, we must eliminate all references to other
209   // types, to avoid some circular reference problems.  This also removes the
210   // type from the internal tables of available types.
211   virtual void dropAllTypeUses(bool inMap);
212   
213 public:
214   inline const ElementTypes &getElementTypes() const { return ETypes; }
215
216   virtual const Type *getContainedType(unsigned i) const { 
217     return i < ETypes.size() ? ETypes[i].get() : 0;
218   }
219   virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
220
221   // getTypeAtIndex - Given an index value into the type, return the type of the
222   // element.  For a structure type, this must be a constant value...
223   //
224   virtual const Type *getTypeAtIndex(const Value *V) const ;
225   virtual bool indexValid(const Value *V) const;
226
227   // getIndexType - Return the type required of indices for this composite.
228   // For structures, this is ubyte, for arrays, this is uint
229   //
230   virtual const Type *getIndexType() const { return Type::UByteTy; }
231
232   // refineAbstractType - Called when a contained type is found to be more
233   // concrete - this could potentially change us from an abstract type to a
234   // concrete type.
235   //
236   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
237
238   static StructType *get(const std::vector<const Type*> &Params);
239
240   // Methods for support type inquiry through isa, cast, and dyn_cast:
241   static inline bool classof(const StructType *T) { return true; }
242   static inline bool classof(const Type *T) {
243     return T->getPrimitiveID() == StructTyID;
244   }
245   static inline bool classof(const Value *V) {
246     return isa<Type>(V) && classof(cast<Type>(V));
247   }
248 };
249
250
251 // SequentialType - This is the superclass of the array and pointer type
252 // classes.  Both of these represent "arrays" in memory.  The array type
253 // represents a specifically sized array, pointer types are unsized/unknown size
254 // arrays.  SequentialType holds the common features of both, which stem from
255 // the fact that both lay their components out in memory identically.
256 //
257 class SequentialType : public CompositeType {
258   SequentialType(const SequentialType &);                  // Do not implement!
259   const SequentialType &operator=(const SequentialType &); // Do not implement!
260 protected:
261   PATypeHandle ElementType;
262
263   SequentialType(PrimitiveID TID, const Type *ElType)
264     : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
265   }
266
267 public:
268   inline const Type *getElementType() const { return ElementType; }
269
270   virtual const Type *getContainedType(unsigned i) const { 
271     return i == 0 ? ElementType.get() : 0;
272   }
273   virtual unsigned getNumContainedTypes() const { return 1; }
274
275   // getTypeAtIndex - Given an index value into the type, return the type of the
276   // element.  For sequential types, there is only one subtype...
277   //
278   virtual const Type *getTypeAtIndex(const Value *V) const {
279     return ElementType.get();
280   }
281   virtual bool indexValid(const Value *V) const {
282     return V->getType() == Type::LongTy;   // Must be a 'long' index
283   }
284
285   // getIndexType() - Return the type required of indices for this composite.
286   // For structures, this is ubyte, for arrays, this is uint
287   //
288   virtual const Type *getIndexType() const { return Type::LongTy; }
289
290   // Methods for support type inquiry through isa, cast, and dyn_cast:
291   static inline bool classof(const SequentialType *T) { return true; }
292   static inline bool classof(const Type *T) {
293     return T->getPrimitiveID() == ArrayTyID ||
294            T->getPrimitiveID() == PointerTyID;
295   }
296   static inline bool classof(const Value *V) {
297     return isa<Type>(V) && classof(cast<Type>(V));
298   }
299 };
300
301
302 class ArrayType : public SequentialType {
303   unsigned NumElements;
304
305   ArrayType(const ArrayType &);                   // Do not implement
306   const ArrayType &operator=(const ArrayType &);  // Do not implement
307 protected:
308   // This should really be private, but it squelches a bogus warning
309   // from GCC to make them protected:  warning: `class ArrayType' only 
310   // defines private constructors and has no friends
311
312   // Private ctor - Only can be created by a static member...
313   ArrayType(const Type *ElType, unsigned NumEl);
314
315   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
316   // another (more concrete) type, we must eliminate all references to other
317   // types, to avoid some circular reference problems.  This also removes the
318   // type from the internal tables of available types.
319   virtual void dropAllTypeUses(bool inMap);
320
321 public:
322   inline unsigned    getNumElements() const { return NumElements; }
323
324   // refineAbstractType - Called when a contained type is found to be more
325   // concrete - this could potentially change us from an abstract type to a
326   // concrete type.
327   //
328   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
329
330   static ArrayType *get(const Type *ElementType, unsigned NumElements);
331
332   // Methods for support type inquiry through isa, cast, and dyn_cast:
333   static inline bool classof(const ArrayType *T) { return true; }
334   static inline bool classof(const Type *T) {
335     return T->getPrimitiveID() == ArrayTyID;
336   }
337   static inline bool classof(const Value *V) {
338     return isa<Type>(V) && classof(cast<Type>(V));
339   }
340 };
341
342
343
344 class PointerType : public SequentialType {
345   PointerType(const PointerType &);                   // Do not implement
346   const PointerType &operator=(const PointerType &);  // Do not implement
347 protected:
348   // This should really be private, but it squelches a bogus warning
349   // from GCC to make them protected:  warning: `class PointerType' only 
350   // defines private constructors and has no friends
351
352   // Private ctor - Only can be created by a static member...
353   PointerType(const Type *ElType);
354
355   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
356   // another (more concrete) type, we must eliminate all references to other
357   // types, to avoid some circular reference problems.  This also removes the
358   // type from the internal tables of available types.
359   virtual void dropAllTypeUses(bool inMap);
360 public:
361   // PointerType::get - Named constructor for pointer types...
362   static PointerType *get(const Type *ElementType);
363
364   // refineAbstractType - Called when a contained type is found to be more
365   // concrete - this could potentially change us from an abstract type to a
366   // concrete type.
367   //
368   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
369
370   // Methods for support type inquiry through isa, cast, and dyn_cast:
371   static inline bool classof(const PointerType *T) { return true; }
372   static inline bool classof(const Type *T) {
373     return T->getPrimitiveID() == PointerTyID;
374   }
375   static inline bool classof(const Value *V) {
376     return isa<Type>(V) && classof(cast<Type>(V));
377   }
378 };
379
380
381 class OpaqueType : public DerivedType {
382   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
383   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
384 protected:
385   // This should really be private, but it squelches a bogus warning
386   // from GCC to make them protected:  warning: `class OpaqueType' only 
387   // defines private constructors and has no friends
388
389   // Private ctor - Only can be created by a static member...
390   OpaqueType();
391
392   // dropAllTypeUses - When this (abstract) type is resolved to be equal to
393   // another (more concrete) type, we must eliminate all references to other
394   // types, to avoid some circular reference problems.
395   virtual void dropAllTypeUses(bool inMap) {}  // No type uses
396
397 public:
398
399   // get - Static factory method for the OpaqueType class...
400   static OpaqueType *get() {
401     return new OpaqueType();           // All opaque types are distinct
402   }
403
404   // Methods for support type inquiry through isa, cast, and dyn_cast:
405   static inline bool classof(const OpaqueType *T) { return true; }
406   static inline bool classof(const Type *T) {
407     return T->getPrimitiveID() == OpaqueTyID;
408   }
409   static inline bool classof(const Value *V) {
410     return isa<Type>(V) && classof(cast<Type>(V));
411   }
412 };
413
414
415 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
416 // These are defined here because they MUST be inlined, yet are dependent on 
417 // the definition of the Type class.  Of course Type derives from Value, which
418 // contains an AbstractTypeUser instance, so there is no good way to factor out
419 // the code.  Hence this bit of uglyness.
420 //
421 inline void PATypeHandle::addUser() {
422   assert(Ty && "Type Handle has a null type!");
423   if (Ty->isAbstract())
424     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
425 }
426 inline void PATypeHandle::removeUser() {
427   if (Ty->isAbstract())
428     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
429 }
430
431 inline void PATypeHandle::removeUserFromConcrete() {
432   if (!Ty->isAbstract())
433     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
434 }
435
436 #endif