Make it possible to get an empty struct using
[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 is distributed under the University of Illinois Open Source
6 // 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 VectorValType;
32 class IntegerValType;
33 class APInt;
34
35 class DerivedType : public Type {
36   friend class Type;
37
38 protected:
39   explicit DerivedType(TypeID id) : Type(id) {}
40
41   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
42   /// that the current type has transitioned from being abstract to being
43   /// concrete.
44   ///
45   void notifyUsesThatTypeBecameConcrete();
46
47   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
48   /// another (more concrete) type, we must eliminate all references to other
49   /// types, to avoid some circular reference problems.
50   ///
51   void dropAllTypeUses();
52
53 public:
54
55   //===--------------------------------------------------------------------===//
56   // Abstract Type handling methods - These types have special lifetimes, which
57   // are managed by (add|remove)AbstractTypeUser. See comments in
58   // AbstractTypeUser.h for more information.
59
60   /// refineAbstractTypeTo - This function is used to when it is discovered that
61   /// the 'this' abstract type is actually equivalent to the NewType specified.
62   /// This causes all users of 'this' to switch to reference the more concrete
63   /// type NewType and for 'this' to be deleted.
64   ///
65   void refineAbstractTypeTo(const Type *NewType);
66
67   void dump() const { Type::dump(); }
68
69   // Methods for support type inquiry through isa, cast, and dyn_cast:
70   static inline bool classof(const DerivedType *T) { return true; }
71   static inline bool classof(const Type *T) {
72     return T->isDerivedType();
73   }
74 };
75
76 /// Class to represent integer types. Note that this class is also used to
77 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
78 /// Int64Ty. 
79 /// @brief Integer representation type
80 class IntegerType : public DerivedType {
81 protected:
82   explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
83     setSubclassData(NumBits);
84   }
85   friend class TypeMap<IntegerValType, IntegerType>;
86 public:
87   /// This enum is just used to hold constants we need for IntegerType.
88   enum {
89     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
90     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
91       ///< Note that bit width is stored in the Type classes SubclassData field
92       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
93   };
94
95   /// This static method is the primary way of constructing an IntegerType. 
96   /// If an IntegerType with the same NumBits value was previously instantiated,
97   /// that instance will be returned. Otherwise a new one will be created. Only
98   /// one instance with a given NumBits value is ever created.
99   /// @brief Get or create an IntegerType instance.
100   static const IntegerType* get(unsigned NumBits);
101
102   /// @brief Get the number of bits in this IntegerType
103   unsigned getBitWidth() const { return getSubclassData(); }
104
105   /// getBitMask - Return a bitmask with ones set for all of the bits
106   /// that can be set by an unsigned version of this type.  This is 0xFF for
107   /// sbyte/ubyte, 0xFFFF for shorts, etc.
108   uint64_t getBitMask() const {
109     return ~uint64_t(0UL) >> (64-getBitWidth());
110   }
111
112   /// getSignBit - Return a uint64_t with just the most significant bit set (the
113   /// sign bit, if the value is treated as a signed number).
114   uint64_t getSignBit() const {
115     return 1ULL << (getBitWidth()-1);
116   }
117   
118   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
119   /// @returns a bit mask with ones set for all the bits of this type.
120   /// @brief Get a bit mask for this type.
121   APInt getMask() const;
122
123   /// This method determines if the width of this IntegerType is a power-of-2
124   /// in terms of 8 bit bytes. 
125   /// @returns true if this is a power-of-2 byte width.
126   /// @brief Is this a power-of-2 byte-width IntegerType ?
127   bool isPowerOf2ByteWidth() const;
128
129   // Methods for support type inquiry through isa, cast, and dyn_cast:
130   static inline bool classof(const IntegerType *T) { return true; }
131   static inline bool classof(const Type *T) {
132     return T->getTypeID() == IntegerTyID;
133   }
134 };
135
136
137 /// FunctionType - Class to represent function types
138 ///
139 class FunctionType : public DerivedType {
140   friend class TypeMap<FunctionValType, FunctionType>;
141   bool isVarArgs;
142
143   FunctionType(const FunctionType &);                   // Do not implement
144   const FunctionType &operator=(const FunctionType &);  // Do not implement
145   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
146                bool IsVarArgs);
147
148 public:
149   /// FunctionType::get - This static method is the primary way of constructing
150   /// a FunctionType. 
151   ///
152   static FunctionType *get(
153     const Type *Result, ///< The result type
154     const std::vector<const Type*> &Params, ///< The types of the parameters
155     bool isVarArg  ///< Whether this is a variable argument length function
156   );
157
158   inline bool isVarArg() const { return isVarArgs; }
159   inline const Type *getReturnType() const { return ContainedTys[0]; }
160
161   typedef Type::subtype_iterator param_iterator;
162   param_iterator param_begin() const { return ContainedTys + 1; }
163   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
164
165   // Parameter type accessors...
166   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
167
168   /// getNumParams - Return the number of fixed parameters this function type
169   /// requires.  This does not consider varargs.
170   ///
171   unsigned getNumParams() const { return NumContainedTys - 1; }
172
173   // Implement the AbstractTypeUser interface.
174   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
175   virtual void typeBecameConcrete(const DerivedType *AbsTy);
176
177   // Methods for support type inquiry through isa, cast, and dyn_cast:
178   static inline bool classof(const FunctionType *T) { return true; }
179   static inline bool classof(const Type *T) {
180     return T->getTypeID() == FunctionTyID;
181   }
182 };
183
184
185 /// CompositeType - Common super class of ArrayType, StructType, PointerType
186 /// and VectorType
187 class CompositeType : public DerivedType {
188 protected:
189   inline explicit CompositeType(TypeID id) : DerivedType(id) { }
190 public:
191
192   /// getTypeAtIndex - Given an index value into the type, return the type of
193   /// the element.
194   ///
195   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
196   virtual bool indexValid(const Value *V) const = 0;
197
198   // Methods for support type inquiry through isa, cast, and dyn_cast:
199   static inline bool classof(const CompositeType *T) { return true; }
200   static inline bool classof(const Type *T) {
201     return T->getTypeID() == ArrayTyID ||
202            T->getTypeID() == StructTyID ||
203            T->getTypeID() == PointerTyID ||
204            T->getTypeID() == VectorTyID;
205   }
206 };
207
208
209 /// StructType - Class to represent struct types
210 ///
211 class StructType : public CompositeType {
212   friend class TypeMap<StructValType, StructType>;
213   StructType(const StructType &);                   // Do not implement
214   const StructType &operator=(const StructType &);  // Do not implement
215   StructType(const std::vector<const Type*> &Types, bool isPacked);
216 public:
217   /// StructType::get - This static method is the primary way to create a
218   /// StructType.
219   ///
220   static StructType *get(const std::vector<const Type*> &Params, 
221                          bool isPacked=false);
222
223   /// StructType::get - This static method is a convenience method for
224   /// creating structure types by specifying the elements as arguments.
225   /// Note that this method always returns a non-packed struct.  To get
226   /// an empty struct, pass NULL, NULL.
227   static StructType *get(const Type *type, ...) END_WITH_NULL;
228
229   // Iterator access to the elements
230   typedef Type::subtype_iterator element_iterator;
231   element_iterator element_begin() const { return ContainedTys; }
232   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
233
234   // Random access to the elements
235   unsigned getNumElements() const { return NumContainedTys; }
236   const Type *getElementType(unsigned N) const {
237     assert(N < NumContainedTys && "Element number out of range!");
238     return ContainedTys[N];
239   }
240
241   /// getTypeAtIndex - Given an index value into the type, return the type of
242   /// the element.  For a structure type, this must be a constant value...
243   ///
244   virtual const Type *getTypeAtIndex(const Value *V) const ;
245   virtual bool indexValid(const Value *V) const;
246
247   // Implement the AbstractTypeUser interface.
248   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
249   virtual void typeBecameConcrete(const DerivedType *AbsTy);
250
251   // Methods for support type inquiry through isa, cast, and dyn_cast:
252   static inline bool classof(const StructType *T) { return true; }
253   static inline bool classof(const Type *T) {
254     return T->getTypeID() == StructTyID;
255   }
256
257   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
258 };
259
260
261 /// SequentialType - This is the superclass of the array, pointer and vector
262 /// type classes.  All of these represent "arrays" in memory.  The array type
263 /// represents a specifically sized array, pointer types are unsized/unknown
264 /// size arrays, vector types represent specifically sized arrays that
265 /// allow for use of SIMD instructions.  SequentialType holds the common
266 /// features of all, which stem from the fact that all three lay their
267 /// components out in memory identically.
268 ///
269 class SequentialType : public CompositeType {
270   PATypeHandle ContainedType; ///< Storage for the single contained type
271   SequentialType(const SequentialType &);                  // Do not implement!
272   const SequentialType &operator=(const SequentialType &); // Do not implement!
273
274   // avoiding warning: 'this' : used in base member initializer list
275   SequentialType* this_() { return this; }
276 protected:
277   SequentialType(TypeID TID, const Type *ElType) 
278     : CompositeType(TID), ContainedType(ElType, this_()) {
279     ContainedTys = &ContainedType; 
280     NumContainedTys = 1;
281   }
282
283 public:
284   inline const Type *getElementType() const { return ContainedTys[0]; }
285
286   virtual bool indexValid(const Value *V) const;
287
288   /// getTypeAtIndex - Given an index value into the type, return the type of
289   /// the element.  For sequential types, there is only one subtype...
290   ///
291   virtual const Type *getTypeAtIndex(const Value *V) const {
292     return ContainedTys[0];
293   }
294
295   // Methods for support type inquiry through isa, cast, and dyn_cast:
296   static inline bool classof(const SequentialType *T) { return true; }
297   static inline bool classof(const Type *T) {
298     return T->getTypeID() == ArrayTyID ||
299            T->getTypeID() == PointerTyID ||
300            T->getTypeID() == VectorTyID;
301   }
302 };
303
304
305 /// ArrayType - Class to represent array types
306 ///
307 class ArrayType : public SequentialType {
308   friend class TypeMap<ArrayValType, ArrayType>;
309   uint64_t NumElements;
310
311   ArrayType(const ArrayType &);                   // Do not implement
312   const ArrayType &operator=(const ArrayType &);  // Do not implement
313   ArrayType(const Type *ElType, uint64_t NumEl);
314 public:
315   /// ArrayType::get - This static method is the primary way to construct an
316   /// ArrayType
317   ///
318   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
319
320   inline uint64_t getNumElements() const { return NumElements; }
321
322   // Implement the AbstractTypeUser interface.
323   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
324   virtual void typeBecameConcrete(const DerivedType *AbsTy);
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->getTypeID() == ArrayTyID;
330   }
331 };
332
333 /// VectorType - Class to represent vector types
334 ///
335 class VectorType : public SequentialType {
336   friend class TypeMap<VectorValType, VectorType>;
337   unsigned NumElements;
338
339   VectorType(const VectorType &);                   // Do not implement
340   const VectorType &operator=(const VectorType &);  // Do not implement
341   VectorType(const Type *ElType, unsigned NumEl);
342 public:
343   /// VectorType::get - This static method is the primary way to construct an
344   /// VectorType
345   ///
346   static VectorType *get(const Type *ElementType, unsigned NumElements);
347
348   /// @brief Return the number of elements in the Vector type.
349   inline unsigned getNumElements() const { return NumElements; }
350
351   /// @brief Return the number of bits in the Vector type.
352   inline unsigned getBitWidth() const { 
353     return NumElements *getElementType()->getPrimitiveSizeInBits();
354   }
355
356   // Implement the AbstractTypeUser interface.
357   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
358   virtual void typeBecameConcrete(const DerivedType *AbsTy);
359
360   // Methods for support type inquiry through isa, cast, and dyn_cast:
361   static inline bool classof(const VectorType *T) { return true; }
362   static inline bool classof(const Type *T) {
363     return T->getTypeID() == VectorTyID;
364   }
365 };
366
367
368 /// PointerType - Class to represent pointers
369 ///
370 class PointerType : public SequentialType {
371   friend class TypeMap<PointerValType, PointerType>;
372   unsigned AddressSpace;
373   
374   PointerType(const PointerType &);                   // Do not implement
375   const PointerType &operator=(const PointerType &);  // Do not implement
376   explicit PointerType(const Type *ElType, unsigned AddrSpace);
377 public:
378   /// PointerType::get - This constructs a pointer to an object of the specified 
379   /// type in a numbered address space.
380   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
381   
382   /// PointerType::getUnqual - This constructs a pointer to an object of the  
383   /// specified type in the generic address space (address space zero).
384   static PointerType *getUnqual(const Type *ElementType) { 
385     return PointerType::get(ElementType, 0);
386   }
387   
388   /// @brief Return the address space of the Pointer type.
389   inline unsigned getAddressSpace() const { return AddressSpace; }
390
391   // Implement the AbstractTypeUser interface.
392   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
393   virtual void typeBecameConcrete(const DerivedType *AbsTy);
394
395   // Implement support type inquiry through isa, cast, and dyn_cast:
396   static inline bool classof(const PointerType *T) { return true; }
397   static inline bool classof(const Type *T) {
398     return T->getTypeID() == PointerTyID;
399   }
400 };
401
402
403 /// OpaqueType - Class to represent abstract types
404 ///
405 class OpaqueType : public DerivedType {
406   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
407   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
408   OpaqueType();
409 public:
410   /// OpaqueType::get - Static factory method for the OpaqueType class...
411   ///
412   static OpaqueType *get() {
413     return new OpaqueType();           // All opaque types are distinct
414   }
415
416   // Implement support for type inquiry through isa, cast, and dyn_cast:
417   static inline bool classof(const OpaqueType *T) { return true; }
418   static inline bool classof(const Type *T) {
419     return T->getTypeID() == OpaqueTyID;
420   }
421 };
422
423 } // End llvm namespace
424
425 #endif