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