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