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