Other minor cleanups while I'm in the area
[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 annihilated, because there is no way to get a reference to it ever
62   // again.
63   //
64   void removeAbstractTypeUser(AbstractTypeUser *U) const;
65
66   // refineAbstractTypeTo - This function is used to when it is discovered that
67   // the 'this' abstract type is actually equivalent to the NewType specified.
68   // This causes all users of 'this' to switch to reference the more concrete
69   // type NewType and for 'this' to be deleted.
70   //
71   void refineAbstractTypeTo(const Type *NewType);
72
73   // Methods for support type inquiry through isa, cast, and dyn_cast:
74   static inline bool classof(const DerivedType *T) { return true; }
75   static inline bool classof(const Type *T) {
76     return T->isDerivedType();
77   }
78   static inline bool classof(const Value *V) {
79     return isa<Type>(V) && classof(cast<Type>(V));
80   }
81 };
82
83
84
85
86 struct FunctionType : public DerivedType {
87   typedef std::vector<PATypeHandle> ParamTypes;
88 private:
89   PATypeHandle 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<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 public:
153
154   // getTypeAtIndex - Given an index value into the type, return the type of the
155   // element.
156   //
157   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
158   virtual bool indexValid(const Value *V) const = 0;
159
160   // getIndexType - Return the type required of indices for this composite.
161   // For structures, this is ubyte, for arrays, this is uint
162   //
163   virtual const Type *getIndexType() const = 0;
164
165
166   // Methods for support type inquiry through isa, cast, and dyn_cast:
167   static inline bool classof(const CompositeType *T) { return true; }
168   static inline bool classof(const Type *T) {
169     return T->getPrimitiveID() == ArrayTyID || 
170            T->getPrimitiveID() == StructTyID ||
171            T->getPrimitiveID() == PointerTyID;
172   }
173   static inline bool classof(const Value *V) {
174     return isa<Type>(V) && classof(cast<Type>(V));
175   }
176 };
177
178
179 class StructType : public CompositeType {
180 public:
181   typedef std::vector<PATypeHandle> ElementTypes;
182
183 private:
184   ElementTypes ETypes;                              // Element types of struct
185
186   StructType(const StructType &);                   // Do not implement
187   const StructType &operator=(const StructType &);  // Do not implement
188
189 protected:
190   // This should really be private, but it squelches a bogus warning
191   // from GCC to make them protected:  warning: `class StructType' only 
192   // defines private constructors and has no friends
193
194   // Private ctor - Only can be created by a static member...
195   StructType(const std::vector<const Type*> &Types);
196   
197 public:
198   inline const ElementTypes &getElementTypes() const { return ETypes; }
199
200   virtual const Type *getContainedType(unsigned i) const { 
201     return i < ETypes.size() ? ETypes[i].get() : 0;
202   }
203   virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
204
205   // getTypeAtIndex - Given an index value into the type, return the type of the
206   // element.  For a structure type, this must be a constant value...
207   //
208   virtual const Type *getTypeAtIndex(const Value *V) const ;
209   virtual bool indexValid(const Value *V) const;
210
211   // getIndexType - Return the type required of indices for this composite.
212   // For structures, this is ubyte, for arrays, this is uint
213   //
214   virtual const Type *getIndexType() const { return Type::UByteTy; }
215
216   // refineAbstractType - Called when a contained type is found to be more
217   // concrete - this could potentially change us from an abstract type to a
218   // concrete type.
219   //
220   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
221
222   static StructType *get(const std::vector<const Type*> &Params);
223
224   // Methods for support type inquiry through isa, cast, and dyn_cast:
225   static inline bool classof(const StructType *T) { return true; }
226   static inline bool classof(const Type *T) {
227     return T->getPrimitiveID() == StructTyID;
228   }
229   static inline bool classof(const Value *V) {
230     return isa<Type>(V) && classof(cast<Type>(V));
231   }
232 };
233
234
235 // SequentialType - This is the superclass of the array and pointer type
236 // classes.  Both of these represent "arrays" in memory.  The array type
237 // represents a specifically sized array, pointer types are unsized/unknown size
238 // arrays.  SequentialType holds the common features of both, which stem from
239 // the fact that both lay their components out in memory identically.
240 //
241 class SequentialType : public CompositeType {
242   SequentialType(const SequentialType &);                  // Do not implement!
243   const SequentialType &operator=(const SequentialType &); // Do not implement!
244 protected:
245   PATypeHandle ElementType;
246
247   SequentialType(PrimitiveID TID, const Type *ElType)
248     : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
249   }
250 public:
251   inline const Type *getElementType() const { return ElementType; }
252
253   virtual const Type *getContainedType(unsigned i) const { 
254     return i == 0 ? ElementType.get() : 0;
255   }
256   virtual unsigned getNumContainedTypes() const { return 1; }
257
258   // getTypeAtIndex - Given an index value into the type, return the type of the
259   // element.  For sequential types, there is only one subtype...
260   //
261   virtual const Type *getTypeAtIndex(const Value *V) const {
262     return ElementType.get();
263   }
264   virtual bool indexValid(const Value *V) const {
265     return V->getType() == Type::LongTy;   // Must be a 'long' index
266   }
267
268   // getIndexType() - Return the type required of indices for this composite.
269   // For structures, this is ubyte, for arrays, this is uint
270   //
271   virtual const Type *getIndexType() const { return Type::LongTy; }
272
273   // Methods for support type inquiry through isa, cast, and dyn_cast:
274   static inline bool classof(const SequentialType *T) { return true; }
275   static inline bool classof(const Type *T) {
276     return T->getPrimitiveID() == ArrayTyID ||
277            T->getPrimitiveID() == PointerTyID;
278   }
279   static inline bool classof(const Value *V) {
280     return isa<Type>(V) && classof(cast<Type>(V));
281   }
282 };
283
284
285 class ArrayType : public SequentialType {
286   unsigned NumElements;
287
288   ArrayType(const ArrayType &);                   // Do not implement
289   const ArrayType &operator=(const ArrayType &);  // Do not implement
290 protected:
291   // This should really be private, but it squelches a bogus warning
292   // from GCC to make them protected:  warning: `class ArrayType' only 
293   // defines private constructors and has no friends
294
295   // Private ctor - Only can be created by a static member...
296   ArrayType(const Type *ElType, unsigned NumEl);
297 public:
298   inline unsigned    getNumElements() const { return NumElements; }
299
300   // refineAbstractType - Called when a contained type is found to be more
301   // concrete - this could potentially change us from an abstract type to a
302   // concrete type.
303   //
304   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
305
306   static ArrayType *get(const Type *ElementType, unsigned NumElements);
307
308   // Methods for support type inquiry through isa, cast, and dyn_cast:
309   static inline bool classof(const ArrayType *T) { return true; }
310   static inline bool classof(const Type *T) {
311     return T->getPrimitiveID() == ArrayTyID;
312   }
313   static inline bool classof(const Value *V) {
314     return isa<Type>(V) && classof(cast<Type>(V));
315   }
316 };
317
318
319
320 class PointerType : public SequentialType {
321   PointerType(const PointerType &);                   // Do not implement
322   const PointerType &operator=(const PointerType &);  // Do not implement
323 protected:
324   // This should really be private, but it squelches a bogus warning
325   // from GCC to make them protected:  warning: `class PointerType' only 
326   // defines private constructors and has no friends
327
328   // Private ctor - Only can be created by a static member...
329   PointerType(const Type *ElType);
330 public:
331   // PointerType::get - Named constructor for pointer types...
332   static PointerType *get(const Type *ElementType);
333
334   // refineAbstractType - Called when a contained type is found to be more
335   // concrete - this could potentially change us from an abstract type to a
336   // concrete type.
337   //
338   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
339
340   // Methods for support type inquiry through isa, cast, and dyn_cast:
341   static inline bool classof(const PointerType *T) { return true; }
342   static inline bool classof(const Type *T) {
343     return T->getPrimitiveID() == PointerTyID;
344   }
345   static inline bool classof(const Value *V) {
346     return isa<Type>(V) && classof(cast<Type>(V));
347   }
348 };
349
350
351 class OpaqueType : public DerivedType {
352   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
353   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
354 protected:
355   // This should really be private, but it squelches a bogus warning
356   // from GCC to make them protected:  warning: `class OpaqueType' only 
357   // defines private constructors and has no friends
358
359   // Private ctor - Only can be created by a static member...
360   OpaqueType();
361 public:
362
363   // get - Static factory method for the OpaqueType class...
364   static OpaqueType *get() {
365     return new OpaqueType();           // All opaque types are distinct
366   }
367
368   // Methods for support type inquiry through isa, cast, and dyn_cast:
369   static inline bool classof(const OpaqueType *T) { return true; }
370   static inline bool classof(const Type *T) {
371     return T->getPrimitiveID() == OpaqueTyID;
372   }
373   static inline bool classof(const Value *V) {
374     return isa<Type>(V) && classof(cast<Type>(V));
375   }
376 };
377
378
379 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
380 // These are defined here because they MUST be inlined, yet are dependent on 
381 // the definition of the Type class.  Of course Type derives from Value, which
382 // contains an AbstractTypeUser instance, so there is no good way to factor out
383 // the code.  Hence this bit of uglyness.
384 //
385 inline void PATypeHandle::addUser() {
386   assert(Ty && "Type Handle has a null type!");
387   if (Ty->isAbstract())
388     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
389 }
390 inline void PATypeHandle::removeUser() {
391   if (Ty->isAbstract())
392     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
393 }
394
395 inline void PATypeHandle::removeUserFromConcrete() {
396   if (!Ty->isAbstract())
397     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
398 }
399
400 #endif