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