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