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