Lexer doesn't create typehandle gross stuff now, parser does.
[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   // 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<const Type>(V));
153   }
154 };
155
156 // TODO: FIXME
157 #ifndef MethodType
158 #define MethodType FunctionType
159 #endif
160
161
162 // CompositeType - Common super class of ArrayType, StructType, and PointerType
163 //
164 class CompositeType : public DerivedType {
165 protected:
166   inline CompositeType(PrimitiveID id) : DerivedType(id) { }
167
168 public:
169
170   // getTypeAtIndex - Given an index value into the type, return the type of the
171   // element.
172   //
173   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
174   virtual bool indexValid(const Value *V) const = 0;
175
176   // getIndexType - Return the type required of indices for this composite.
177   // For structures, this is ubyte, for arrays, this is uint
178   //
179   virtual const Type *getIndexType() const = 0;
180
181
182   // Methods for support type inquiry through isa, cast, and dyn_cast:
183   static inline bool classof(const CompositeType *T) { return true; }
184   static inline bool classof(const Type *T) {
185     return T->getPrimitiveID() == ArrayTyID || 
186            T->getPrimitiveID() == StructTyID ||
187            T->getPrimitiveID() == PointerTyID;
188   }
189   static inline bool classof(const Value *V) {
190     return isa<Type>(V) && classof(cast<const Type>(V));
191   }
192 };
193
194
195 class StructType : public CompositeType {
196 public:
197   typedef std::vector<PATypeHandle<Type> > ElementTypes;
198
199 private:
200   ElementTypes ETypes;                              // Element types of struct
201
202   StructType(const StructType &);                   // Do not implement
203   const StructType &operator=(const StructType &);  // Do not implement
204
205 protected:
206   // This should really be private, but it squelches a bogus warning
207   // from GCC to make them protected:  warning: `class StructType' only 
208   // defines private constructors and has no friends
209
210   // Private ctor - Only can be created by a static member...
211   StructType(const std::vector<const Type*> &Types);
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<const 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<Type> ElementType;
262
263   SequentialType(PrimitiveID TID, const Type *ElType)
264     : CompositeType(TID), ElementType(PATypeHandle<Type>(ElType, this)) {
265   }
266 public:
267
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::UIntTy;   // Must be an unsigned int 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::UIntTy; }
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<const 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
313   // Private ctor - Only can be created by a static member...
314   ArrayType(const Type *ElType, unsigned NumEl);
315 public:
316   inline unsigned    getNumElements() const { return NumElements; }
317
318   // refineAbstractType - Called when a contained type is found to be more
319   // concrete - this could potentially change us from an abstract type to a
320   // concrete type.
321   //
322   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
323
324   static ArrayType *get(const Type *ElementType, unsigned NumElements);
325
326   // Methods for support type inquiry through isa, cast, and dyn_cast:
327   static inline bool classof(const ArrayType *T) { return true; }
328   static inline bool classof(const Type *T) {
329     return T->getPrimitiveID() == ArrayTyID;
330   }
331   static inline bool classof(const Value *V) {
332     return isa<Type>(V) && classof(cast<const Type>(V));
333   }
334 };
335
336
337
338 class PointerType : public SequentialType {
339   PointerType(const PointerType &);                   // Do not implement
340   const PointerType &operator=(const PointerType &);  // Do not implement
341 protected:
342   // This should really be private, but it squelches a bogus warning
343   // from GCC to make them protected:  warning: `class PointerType' only 
344   // defines private constructors and has no friends
345
346
347   // Private ctor - Only can be created by a static member...
348   PointerType(const Type *ElType);
349 public:
350   // PointerType::get - Named constructor for pointer types...
351   static PointerType *get(const Type *ElementType);
352
353   // refineAbstractType - Called when a contained type is found to be more
354   // concrete - this could potentially change us from an abstract type to a
355   // concrete type.
356   //
357   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
358
359   // Methods for support type inquiry through isa, cast, and dyn_cast:
360   static inline bool classof(const PointerType *T) { return true; }
361   static inline bool classof(const Type *T) {
362     return T->getPrimitiveID() == PointerTyID;
363   }
364   static inline bool classof(const Value *V) {
365     return isa<Type>(V) && classof(cast<const Type>(V));
366   }
367 };
368
369
370 class OpaqueType : public DerivedType {
371 private:
372   OpaqueType(const OpaqueType &);                   // Do not implement
373   const OpaqueType &operator=(const OpaqueType &);  // Do not implement
374 protected:
375   // This should really be private, but it squelches a bogus warning
376   // from GCC to make them protected:  warning: `class OpaqueType' only 
377   // defines private constructors and has no friends
378
379   // Private ctor - Only can be created by a static member...
380   OpaqueType();
381
382 public:
383
384   // get - Static factory method for the OpaqueType class...
385   static OpaqueType *get() {
386     return new OpaqueType();           // All opaque types are distinct
387   }
388
389   // Methods for support type inquiry through isa, cast, and dyn_cast:
390   static inline bool classof(const OpaqueType *T) { return true; }
391   static inline bool classof(const Type *T) {
392     return T->getPrimitiveID() == OpaqueTyID;
393   }
394   static inline bool classof(const Value *V) {
395     return isa<Type>(V) && classof(cast<const Type>(V));
396   }
397 };
398
399
400 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
401 // These are defined here because they MUST be inlined, yet are dependant on 
402 // the definition of the Type class.  Of course Type derives from Value, which
403 // contains an AbstractTypeUser instance, so there is no good way to factor out
404 // the code.  Hence this bit of uglyness.
405 //
406 template <class TypeSubClass> void PATypeHandle<TypeSubClass>::addUser() {
407   assert(Ty && "Type Handle has a null type!");
408   if (Ty->isAbstract())
409     cast<DerivedType>(Ty)->addAbstractTypeUser(User);
410 }
411 template <class TypeSubClass> void PATypeHandle<TypeSubClass>::removeUser() {
412   if (Ty->isAbstract())
413     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
414 }
415
416 template <class TypeSubClass>
417 void PATypeHandle<TypeSubClass>::removeUserFromConcrete() {
418   if (!Ty->isAbstract())
419     cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
420 }
421
422 #endif