This file uses the Value class without a forward decl
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations of classes that represent "derived 
11 // types".  These are things like "arrays of x" or "structure of x, y, z" or
12 // "method returning x taking (y,z) as parameters", etc...
13 //
14 // The implementations of these classes live in the Type.cpp file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
20
21 #include "llvm/Type.h"
22
23 namespace llvm {
24
25 class Value;
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
28 class ArrayValType;
29 class StructValType;
30 class PointerValType;
31
32 class DerivedType : public Type, public AbstractTypeUser {
33   // AbstractTypeUsers - Implement a list of the users that need to be notified
34   // if I am a type, and I get resolved into a more concrete type.
35   //
36   mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
37
38 protected:
39   DerivedType(TypeID id) : Type("", id) {}
40   ~DerivedType() {
41     assert(AbstractTypeUsers.empty());
42   }
43
44   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
45   /// that the current type has transitioned from being abstract to being
46   /// concrete.
47   ///
48   void notifyUsesThatTypeBecameConcrete();
49
50   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
51   /// another (more concrete) type, we must eliminate all references to other
52   /// types, to avoid some circular reference problems.
53   ///
54   void dropAllTypeUses();
55
56   void RefCountIsZero() const {
57     if (AbstractTypeUsers.empty())
58       delete this;
59   }
60
61   
62 public:
63
64   //===--------------------------------------------------------------------===//
65   // Abstract Type handling methods - These types have special lifetimes, which
66   // are managed by (add|remove)AbstractTypeUser. See comments in
67   // AbstractTypeUser.h for more information.
68
69   /// addAbstractTypeUser - Notify an abstract type that there is a new user of
70   /// it.  This function is called primarily by the PATypeHandle class.
71   ///
72   void addAbstractTypeUser(AbstractTypeUser *U) const {
73     assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
74     AbstractTypeUsers.push_back(U);
75   }
76
77   /// removeAbstractTypeUser - Notify an abstract type that a user of the class
78   /// no longer has a handle to the type.  This function is called primarily by
79   /// the PATypeHandle class.  When there are no users of the abstract type, it
80   /// is annihilated, because there is no way to get a reference to it ever
81   /// again.
82   ///
83   void removeAbstractTypeUser(AbstractTypeUser *U) const;
84
85   /// refineAbstractTypeTo - This function is used to when it is discovered that
86   /// the 'this' abstract type is actually equivalent to the NewType specified.
87   /// This causes all users of 'this' to switch to reference the more concrete
88   /// type NewType and for 'this' to be deleted.
89   ///
90   void refineAbstractTypeTo(const Type *NewType);
91
92   void dump() const { Type::dump(); }
93
94   // Methods for support type inquiry through isa, cast, and dyn_cast:
95   static inline bool classof(const DerivedType *T) { return true; }
96   static inline bool classof(const Type *T) {
97     return T->isDerivedType();
98   }
99 };
100
101
102 /// FunctionType - Class to represent function types
103 ///
104 class FunctionType : public DerivedType {
105   friend class TypeMap<FunctionValType, FunctionType>;
106   bool isVarArgs;
107
108   FunctionType(const FunctionType &);                   // Do not implement
109   const FunctionType &operator=(const FunctionType &);  // Do not implement
110 protected:
111   /// This should really be private, but it squelches a bogus warning
112   /// from GCC to make them protected:  warning: `class FunctionType' only 
113   /// defines private constructors and has no friends
114   ///
115   /// Private ctor - Only can be created by a static member...
116   ///
117   FunctionType(const Type *Result, const std::vector<const Type*> &Params, 
118                bool IsVarArgs);
119
120 public:
121   /// FunctionType::get - This static method is the primary way of constructing
122   /// a FunctionType
123   ///
124   static FunctionType *get(const Type *Result,
125                            const std::vector<const Type*> &Params,
126                            bool isVarArg);
127
128   inline bool isVarArg() const { return isVarArgs; }
129   inline const Type *getReturnType() const { return ContainedTys[0]; }
130
131   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
132   param_iterator param_begin() const { return ContainedTys.begin()+1; }
133   param_iterator param_end() const { return ContainedTys.end(); }
134
135   // Parameter type accessors...
136   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
137
138   /// getNumParams - Return the number of fixed parameters this function type
139   /// requires.  This does not consider varargs.
140   ///
141   unsigned getNumParams() const { return ContainedTys.size()-1; }
142
143   // Implement the AbstractTypeUser interface.
144   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
145   virtual void typeBecameConcrete(const DerivedType *AbsTy);
146   
147   // Methods for support type inquiry through isa, cast, and dyn_cast:
148   static inline bool classof(const FunctionType *T) { return true; }
149   static inline bool classof(const Type *T) {
150     return T->getTypeID() == FunctionTyID;
151   }
152 };
153
154
155 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
156 ///
157 class CompositeType : public DerivedType {
158 protected:
159   inline CompositeType(TypeID id) : DerivedType(id) { }
160 public:
161
162   /// getTypeAtIndex - Given an index value into the type, return the type of
163   /// the element.
164   ///
165   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
166   virtual bool indexValid(const Value *V) const = 0;
167
168   // Methods for support type inquiry through isa, cast, and dyn_cast:
169   static inline bool classof(const CompositeType *T) { return true; }
170   static inline bool classof(const Type *T) {
171     return T->getTypeID() == ArrayTyID || 
172            T->getTypeID() == StructTyID ||
173            T->getTypeID() == PointerTyID;
174   }
175 };
176
177
178 /// StructType - Class to represent struct types
179 ///
180 class StructType : public CompositeType {
181   friend class TypeMap<StructValType, StructType>;
182   StructType(const StructType &);                   // Do not implement
183   const StructType &operator=(const StructType &);  // Do not implement
184
185 protected:
186   /// This should really be private, but it squelches a bogus warning
187   /// from GCC to make them protected:  warning: `class StructType' only 
188   /// defines private constructors and has no friends
189   ///
190   /// Private ctor - Only can be created by a static member...
191   ///
192   StructType(const std::vector<const Type*> &Types);
193
194 public:
195   /// StructType::get - This static method is the primary way to create a
196   /// StructType.
197   ///
198   static StructType *get(const std::vector<const Type*> &Params);
199
200   // Iterator access to the elements
201   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
202   element_iterator element_begin() const { return ContainedTys.begin(); }
203   element_iterator element_end() const { return ContainedTys.end(); }
204
205   // Random access to the elements
206   unsigned getNumElements() const { return ContainedTys.size(); }
207   const Type *getElementType(unsigned N) const {
208     assert(N < ContainedTys.size() && "Element number out of range!");
209     return ContainedTys[N];
210   }
211
212   /// getTypeAtIndex - Given an index value into the type, return the type of
213   /// the element.  For a structure type, this must be a constant value...
214   ///
215   virtual const Type *getTypeAtIndex(const Value *V) const ;
216   virtual bool indexValid(const Value *V) const;
217
218   // Implement the AbstractTypeUser interface.
219   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
220   virtual void typeBecameConcrete(const DerivedType *AbsTy);
221
222   // Methods for support type inquiry through isa, cast, and dyn_cast:
223   static inline bool classof(const StructType *T) { return true; }
224   static inline bool classof(const Type *T) {
225     return T->getTypeID() == StructTyID;
226   }
227 };
228
229
230 /// SequentialType - This is the superclass of the array and pointer type
231 /// classes.  Both of these represent "arrays" in memory.  The array type
232 /// represents a specifically sized array, pointer types are unsized/unknown
233 /// size arrays.  SequentialType holds the common features of both, which stem
234 /// from the fact that both lay their components out in memory identically.
235 ///
236 class SequentialType : public CompositeType {
237   SequentialType(const SequentialType &);                  // Do not implement!
238   const SequentialType &operator=(const SequentialType &); // Do not implement!
239 protected:
240   SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
241     ContainedTys.reserve(1);
242     ContainedTys.push_back(PATypeHandle(ElType, this));
243   }
244
245 public:
246   inline const Type *getElementType() const { return ContainedTys[0]; }
247
248   virtual bool indexValid(const Value *V) const;
249
250   /// getTypeAtIndex - Given an index value into the type, return the type of
251   /// the element.  For sequential types, there is only one subtype...
252   ///
253   virtual const Type *getTypeAtIndex(const Value *V) const {
254     return ContainedTys[0];
255   }
256
257   // Methods for support type inquiry through isa, cast, and dyn_cast:
258   static inline bool classof(const SequentialType *T) { return true; }
259   static inline bool classof(const Type *T) {
260     return T->getTypeID() == ArrayTyID ||
261            T->getTypeID() == PointerTyID;
262   }
263 };
264
265
266 /// ArrayType - Class to represent array types
267 ///
268 class ArrayType : public SequentialType {
269   friend class TypeMap<ArrayValType, ArrayType>;
270   unsigned NumElements;
271
272   ArrayType(const ArrayType &);                   // Do not implement
273   const ArrayType &operator=(const ArrayType &);  // Do not implement
274 protected:
275   /// This should really be private, but it squelches a bogus warning
276   /// from GCC to make them protected:  warning: `class ArrayType' only 
277   /// defines private constructors and has no friends
278   ///
279   /// Private ctor - Only can be created by a static member...
280   ///
281   ArrayType(const Type *ElType, unsigned NumEl);
282
283 public:
284   /// ArrayType::get - This static method is the primary way to construct an
285   /// ArrayType
286   ///
287   static ArrayType *get(const Type *ElementType, unsigned NumElements);
288
289   inline unsigned    getNumElements() const { return NumElements; }
290
291   // Implement the AbstractTypeUser interface.
292   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
293   virtual void typeBecameConcrete(const DerivedType *AbsTy);
294
295   // Methods for support type inquiry through isa, cast, and dyn_cast:
296   static inline bool classof(const ArrayType *T) { return true; }
297   static inline bool classof(const Type *T) {
298     return T->getTypeID() == ArrayTyID;
299   }
300 };
301
302
303 /// PointerType - Class to represent pointers
304 ///
305 class PointerType : public SequentialType {
306   friend class TypeMap<PointerValType, PointerType>;
307   PointerType(const PointerType &);                   // Do not implement
308   const PointerType &operator=(const PointerType &);  // Do not implement
309 protected:
310   // This should really be private, but it squelches a bogus warning
311   // from GCC to make them protected:  warning: `class PointerType' only 
312   // defines private constructors and has no friends
313
314   // Private ctor - Only can be created by a static member...
315   PointerType(const Type *ElType);
316
317 public:
318   /// PointerType::get - This is the only way to construct a new pointer type.
319   static PointerType *get(const Type *ElementType);
320
321   // Implement the AbstractTypeUser interface.
322   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
323   virtual void typeBecameConcrete(const DerivedType *AbsTy);
324
325   // Implement support type inquiry through isa, cast, and dyn_cast:
326   static inline bool classof(const PointerType *T) { return true; }
327   static inline bool classof(const Type *T) {
328     return T->getTypeID() == PointerTyID;
329   }
330 };
331
332
333 /// OpaqueType - Class to represent abstract types
334 ///
335 class OpaqueType : public DerivedType {
336   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
337   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
338 protected:
339   /// This should really be private, but it squelches a bogus warning
340   /// from GCC to make them protected:  warning: `class OpaqueType' only 
341   /// defines private constructors and has no friends
342   ///
343   /// Private ctor - Only can be created by a static member...
344   OpaqueType();
345
346 public:
347   /// OpaqueType::get - Static factory method for the OpaqueType class...
348   ///
349   static OpaqueType *get() {
350     return new OpaqueType();           // All opaque types are distinct
351   }
352
353   // Implement the AbstractTypeUser interface.
354   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
355     abort();   // FIXME: this is not really an AbstractTypeUser!
356   }
357   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
358     abort();   // FIXME: this is not really an AbstractTypeUser!
359   }
360
361   // Implement support for type inquiry through isa, cast, and dyn_cast:
362   static inline bool classof(const OpaqueType *T) { return true; }
363   static inline bool classof(const Type *T) {
364     return T->getTypeID() == OpaqueTyID;
365   }
366 };
367
368 } // End llvm namespace
369
370 #endif