fix clang selfhost issue (shadowing)
[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 UnionValType;
31 class PointerValType;
32 class VectorValType;
33 class IntegerValType;
34 class APInt;
35 class LLVMContext;
36
37 class DerivedType : public Type {
38   friend class Type;
39
40 protected:
41   explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
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   /// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo
56   /// that performs no locking.  Only used for internal recursion.
57   void unlockedRefineAbstractTypeTo(const Type *NewType);
58   
59 public:
60
61   //===--------------------------------------------------------------------===//
62   // Abstract Type handling methods - These types have special lifetimes, which
63   // are managed by (add|remove)AbstractTypeUser. See comments in
64   // AbstractTypeUser.h for more information.
65
66   /// refineAbstractTypeTo - This function is used to when it is discovered that
67   /// the 'this' abstract type is actually equivalent to the NewType specified.
68   /// This causes all users of 'this' to switch to reference the more concrete
69   /// type NewType and for 'this' to be deleted.
70   ///
71   void refineAbstractTypeTo(const Type *NewType);
72
73   void dump() const { Type::dump(); }
74
75   // Methods for support type inquiry through isa, cast, and dyn_cast:
76   static inline bool classof(const DerivedType *) { return true; }
77   static inline bool classof(const Type *T) {
78     return T->isDerivedType();
79   }
80 };
81
82 /// Class to represent integer types. Note that this class is also used to
83 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
84 /// Int64Ty.
85 /// @brief Integer representation type
86 class IntegerType : public DerivedType {
87   friend class LLVMContextImpl;
88   
89 protected:
90   explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
91       DerivedType(C, IntegerTyID) {
92     setSubclassData(NumBits);
93   }
94   friend class TypeMap<IntegerValType, IntegerType>;
95 public:
96   /// This enum is just used to hold constants we need for IntegerType.
97   enum {
98     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
99     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
100       ///< Note that bit width is stored in the Type classes SubclassData field
101       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
102   };
103
104   /// This static method is the primary way of constructing an IntegerType.
105   /// If an IntegerType with the same NumBits value was previously instantiated,
106   /// that instance will be returned. Otherwise a new one will be created. Only
107   /// one instance with a given NumBits value is ever created.
108   /// @brief Get or create an IntegerType instance.
109   static const IntegerType* get(LLVMContext &C, unsigned NumBits);
110
111   /// @brief Get the number of bits in this IntegerType
112   unsigned getBitWidth() const { return getSubclassData(); }
113
114   /// getBitMask - Return a bitmask with ones set for all of the bits
115   /// that can be set by an unsigned version of this type.  This is 0xFF for
116   /// i8, 0xFFFF for i16, etc.
117   uint64_t getBitMask() const {
118     return ~uint64_t(0UL) >> (64-getBitWidth());
119   }
120
121   /// getSignBit - Return a uint64_t with just the most significant bit set (the
122   /// sign bit, if the value is treated as a signed number).
123   uint64_t getSignBit() const {
124     return 1ULL << (getBitWidth()-1);
125   }
126
127   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
128   /// @returns a bit mask with ones set for all the bits of this type.
129   /// @brief Get a bit mask for this type.
130   APInt getMask() const;
131
132   /// This method determines if the width of this IntegerType is a power-of-2
133   /// in terms of 8 bit bytes.
134   /// @returns true if this is a power-of-2 byte width.
135   /// @brief Is this a power-of-2 byte-width IntegerType ?
136   bool isPowerOf2ByteWidth() const;
137
138   // Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const IntegerType *) { return true; }
140   static inline bool classof(const Type *T) {
141     return T->getTypeID() == IntegerTyID;
142   }
143 };
144
145
146 /// FunctionType - Class to represent function types
147 ///
148 class FunctionType : public DerivedType {
149   friend class TypeMap<FunctionValType, FunctionType>;
150   bool isVarArgs;
151
152   FunctionType(const FunctionType &);                   // Do not implement
153   const FunctionType &operator=(const FunctionType &);  // Do not implement
154   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
155                bool IsVarArgs);
156
157 public:
158   /// FunctionType::get - This static method is the primary way of constructing
159   /// a FunctionType.
160   ///
161   static FunctionType *get(
162     const Type *Result, ///< The result type
163     const std::vector<const Type*> &Params, ///< The types of the parameters
164     bool isVarArg  ///< Whether this is a variable argument length function
165   );
166
167   /// FunctionType::get - Create a FunctionType taking no parameters.
168   ///
169   static FunctionType *get(
170     const Type *Result, ///< The result type
171     bool isVarArg  ///< Whether this is a variable argument length function
172   ) {
173     return get(Result, std::vector<const Type *>(), isVarArg);
174   }
175
176   /// isValidReturnType - Return true if the specified type is valid as a return
177   /// type.
178   static bool isValidReturnType(const Type *RetTy);
179
180   /// isValidArgumentType - Return true if the specified type is valid as an
181   /// argument type.
182   static bool isValidArgumentType(const Type *ArgTy);
183
184   inline bool isVarArg() const { return isVarArgs; }
185   inline const Type *getReturnType() const { return ContainedTys[0]; }
186
187   typedef Type::subtype_iterator param_iterator;
188   param_iterator param_begin() const { return ContainedTys + 1; }
189   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
190
191   // Parameter type accessors...
192   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
193
194   /// getNumParams - Return the number of fixed parameters this function type
195   /// requires.  This does not consider varargs.
196   ///
197   unsigned getNumParams() const { return NumContainedTys - 1; }
198
199   // Implement the AbstractTypeUser interface.
200   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
201   virtual void typeBecameConcrete(const DerivedType *AbsTy);
202
203   // Methods for support type inquiry through isa, cast, and dyn_cast:
204   static inline bool classof(const FunctionType *) { return true; }
205   static inline bool classof(const Type *T) {
206     return T->getTypeID() == FunctionTyID;
207   }
208 };
209
210
211 /// CompositeType - Common super class of ArrayType, StructType, PointerType
212 /// and VectorType
213 class CompositeType : public DerivedType {
214 protected:
215   inline explicit CompositeType(LLVMContext &C, TypeID id) :
216     DerivedType(C, id) { }
217 public:
218
219   /// getTypeAtIndex - Given an index value into the type, return the type of
220   /// the element.
221   ///
222   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
223   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
224   virtual bool indexValid(const Value *V) const = 0;
225   virtual bool indexValid(unsigned Idx) const = 0;
226
227   // Methods for support type inquiry through isa, cast, and dyn_cast:
228   static inline bool classof(const CompositeType *) { return true; }
229   static inline bool classof(const Type *T) {
230     return T->getTypeID() == ArrayTyID ||
231            T->getTypeID() == StructTyID ||
232            T->getTypeID() == PointerTyID ||
233            T->getTypeID() == VectorTyID ||
234            T->getTypeID() == UnionTyID;
235   }
236 };
237
238
239 /// StructType - Class to represent struct types
240 ///
241 class StructType : public CompositeType {
242   friend class TypeMap<StructValType, StructType>;
243   StructType(const StructType &);                   // Do not implement
244   const StructType &operator=(const StructType &);  // Do not implement
245   StructType(LLVMContext &C,
246              const std::vector<const Type*> &Types, bool isPacked);
247 public:
248   /// StructType::get - This static method is the primary way to create a
249   /// StructType.
250   ///
251   static StructType *get(LLVMContext &Context, 
252                          const std::vector<const Type*> &Params,
253                          bool isPacked=false);
254
255   /// StructType::get - Create an empty structure type.
256   ///
257   static StructType *get(LLVMContext &Context, bool isPacked=false) {
258     return get(Context, std::vector<const Type*>(), isPacked);
259   }
260
261   /// StructType::get - This static method is a convenience method for
262   /// creating structure types by specifying the elements as arguments.
263   /// Note that this method always returns a non-packed struct.  To get
264   /// an empty struct, pass NULL, NULL.
265   static StructType *get(LLVMContext &Context, 
266                          const Type *type, ...) END_WITH_NULL;
267
268   /// isValidElementType - Return true if the specified type is valid as a
269   /// element type.
270   static bool isValidElementType(const Type *ElemTy);
271
272   // Iterator access to the elements
273   typedef Type::subtype_iterator element_iterator;
274   element_iterator element_begin() const { return ContainedTys; }
275   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
276
277   // Random access to the elements
278   unsigned getNumElements() const { return NumContainedTys; }
279   const Type *getElementType(unsigned N) const {
280     assert(N < NumContainedTys && "Element number out of range!");
281     return ContainedTys[N];
282   }
283
284   /// getTypeAtIndex - Given an index value into the type, return the type of
285   /// the element.  For a structure type, this must be a constant value...
286   ///
287   virtual const Type *getTypeAtIndex(const Value *V) const;
288   virtual const Type *getTypeAtIndex(unsigned Idx) const;
289   virtual bool indexValid(const Value *V) const;
290   virtual bool indexValid(unsigned Idx) const;
291
292   // Implement the AbstractTypeUser interface.
293   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
294   virtual void typeBecameConcrete(const DerivedType *AbsTy);
295
296   // Methods for support type inquiry through isa, cast, and dyn_cast:
297   static inline bool classof(const StructType *) { return true; }
298   static inline bool classof(const Type *T) {
299     return T->getTypeID() == StructTyID;
300   }
301
302   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
303 };
304
305
306 /// UnionType - Class to represent union types. A union type is similar to
307 /// a structure, except that all member fields begin at offset 0.
308 ///
309 class UnionType : public CompositeType {
310   friend class TypeMap<UnionValType, UnionType>;
311   UnionType(const UnionType &);                   // Do not implement
312   const UnionType &operator=(const UnionType &);  // Do not implement
313   UnionType(LLVMContext &C, const Type* const* Types, unsigned NumTypes);
314 public:
315   /// UnionType::get - This static method is the primary way to create a
316   /// UnionType.
317   static UnionType *get(const Type* const* Types, unsigned NumTypes);
318
319   /// UnionType::get - This static method is a convenience method for
320   /// creating union types by specifying the elements as arguments.
321   static UnionType *get(const Type *type, ...) END_WITH_NULL;
322
323   /// isValidElementType - Return true if the specified type is valid as a
324   /// element type.
325   static bool isValidElementType(const Type *ElemTy);
326   
327   /// Given an element type, return the member index of that type, or -1
328   /// if there is no such member type.
329   int getElementTypeIndex(const Type *ElemTy) const;
330
331   // Iterator access to the elements
332   typedef Type::subtype_iterator element_iterator;
333   element_iterator element_begin() const { return ContainedTys; }
334   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
335
336   // Random access to the elements
337   unsigned getNumElements() const { return NumContainedTys; }
338   const Type *getElementType(unsigned N) const {
339     assert(N < NumContainedTys && "Element number out of range!");
340     return ContainedTys[N];
341   }
342
343   /// getTypeAtIndex - Given an index value into the type, return the type of
344   /// the element.  For a union type, this must be a constant value...
345   ///
346   virtual const Type *getTypeAtIndex(const Value *V) const;
347   virtual const Type *getTypeAtIndex(unsigned Idx) const;
348   virtual bool indexValid(const Value *V) const;
349   virtual bool indexValid(unsigned Idx) const;
350
351   // Implement the AbstractTypeUser interface.
352   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
353   virtual void typeBecameConcrete(const DerivedType *AbsTy);
354
355   // Methods for support type inquiry through isa, cast, and dyn_cast:
356   static inline bool classof(const UnionType *) { return true; }
357   static inline bool classof(const Type *T) {
358     return T->getTypeID() == UnionTyID;
359   }
360 };
361
362
363 /// SequentialType - This is the superclass of the array, pointer and vector
364 /// type classes.  All of these represent "arrays" in memory.  The array type
365 /// represents a specifically sized array, pointer types are unsized/unknown
366 /// size arrays, vector types represent specifically sized arrays that
367 /// allow for use of SIMD instructions.  SequentialType holds the common
368 /// features of all, which stem from the fact that all three lay their
369 /// components out in memory identically.
370 ///
371 class SequentialType : public CompositeType {
372   PATypeHandle ContainedType; ///< Storage for the single contained type
373   SequentialType(const SequentialType &);                  // Do not implement!
374   const SequentialType &operator=(const SequentialType &); // Do not implement!
375
376   // avoiding warning: 'this' : used in base member initializer list
377   SequentialType* this_() { return this; }
378 protected:
379   SequentialType(TypeID TID, const Type *ElType)
380     : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
381     ContainedTys = &ContainedType;
382     NumContainedTys = 1;
383   }
384
385 public:
386   inline const Type *getElementType() const { return ContainedTys[0]; }
387
388   virtual bool indexValid(const Value *V) const;
389   virtual bool indexValid(unsigned) const {
390     return true;
391   }
392
393   /// getTypeAtIndex - Given an index value into the type, return the type of
394   /// the element.  For sequential types, there is only one subtype...
395   ///
396   virtual const Type *getTypeAtIndex(const Value *) const {
397     return ContainedTys[0];
398   }
399   virtual const Type *getTypeAtIndex(unsigned) const {
400     return ContainedTys[0];
401   }
402
403   // Methods for support type inquiry through isa, cast, and dyn_cast:
404   static inline bool classof(const SequentialType *) { return true; }
405   static inline bool classof(const Type *T) {
406     return T->getTypeID() == ArrayTyID ||
407            T->getTypeID() == PointerTyID ||
408            T->getTypeID() == VectorTyID;
409   }
410 };
411
412
413 /// ArrayType - Class to represent array types
414 ///
415 class ArrayType : public SequentialType {
416   friend class TypeMap<ArrayValType, ArrayType>;
417   uint64_t NumElements;
418
419   ArrayType(const ArrayType &);                   // Do not implement
420   const ArrayType &operator=(const ArrayType &);  // Do not implement
421   ArrayType(const Type *ElType, uint64_t NumEl);
422 public:
423   /// ArrayType::get - This static method is the primary way to construct an
424   /// ArrayType
425   ///
426   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
427
428   /// isValidElementType - Return true if the specified type is valid as a
429   /// element type.
430   static bool isValidElementType(const Type *ElemTy);
431
432   inline uint64_t getNumElements() const { return NumElements; }
433
434   // Implement the AbstractTypeUser interface.
435   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
436   virtual void typeBecameConcrete(const DerivedType *AbsTy);
437
438   // Methods for support type inquiry through isa, cast, and dyn_cast:
439   static inline bool classof(const ArrayType *) { return true; }
440   static inline bool classof(const Type *T) {
441     return T->getTypeID() == ArrayTyID;
442   }
443 };
444
445 /// VectorType - Class to represent vector types
446 ///
447 class VectorType : public SequentialType {
448   friend class TypeMap<VectorValType, VectorType>;
449   unsigned NumElements;
450
451   VectorType(const VectorType &);                   // Do not implement
452   const VectorType &operator=(const VectorType &);  // Do not implement
453   VectorType(const Type *ElType, unsigned NumEl);
454 public:
455   /// VectorType::get - This static method is the primary way to construct an
456   /// VectorType
457   ///
458   static VectorType *get(const Type *ElementType, unsigned NumElements);
459
460   /// VectorType::getInteger - This static method gets a VectorType with the
461   /// same number of elements as the input type, and the element type is an
462   /// integer type of the same width as the input element type.
463   ///
464   static VectorType *getInteger(const VectorType *VTy) {
465     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
466     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
467     return VectorType::get(EltTy, VTy->getNumElements());
468   }
469
470   /// VectorType::getExtendedElementVectorType - This static method is like
471   /// getInteger except that the element types are twice as wide as the
472   /// elements in the input type.
473   ///
474   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
475     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
476     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
477     return VectorType::get(EltTy, VTy->getNumElements());
478   }
479
480   /// VectorType::getTruncatedElementVectorType - This static method is like
481   /// getInteger except that the element types are half as wide as the
482   /// elements in the input type.
483   ///
484   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
485     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
486     assert((EltBits & 1) == 0 &&
487            "Cannot truncate vector element with odd bit-width");
488     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
489     return VectorType::get(EltTy, VTy->getNumElements());
490   }
491
492   /// isValidElementType - Return true if the specified type is valid as a
493   /// element type.
494   static bool isValidElementType(const Type *ElemTy);
495
496   /// @brief Return the number of elements in the Vector type.
497   inline unsigned getNumElements() const { return NumElements; }
498
499   /// @brief Return the number of bits in the Vector type.
500   inline unsigned getBitWidth() const {
501     return NumElements * getElementType()->getPrimitiveSizeInBits();
502   }
503
504   // Implement the AbstractTypeUser interface.
505   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
506   virtual void typeBecameConcrete(const DerivedType *AbsTy);
507
508   // Methods for support type inquiry through isa, cast, and dyn_cast:
509   static inline bool classof(const VectorType *) { return true; }
510   static inline bool classof(const Type *T) {
511     return T->getTypeID() == VectorTyID;
512   }
513 };
514
515
516 /// PointerType - Class to represent pointers
517 ///
518 class PointerType : public SequentialType {
519   friend class TypeMap<PointerValType, PointerType>;
520   unsigned AddressSpace;
521
522   PointerType(const PointerType &);                   // Do not implement
523   const PointerType &operator=(const PointerType &);  // Do not implement
524   explicit PointerType(const Type *ElType, unsigned AddrSpace);
525 public:
526   /// PointerType::get - This constructs a pointer to an object of the specified
527   /// type in a numbered address space.
528   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
529
530   /// PointerType::getUnqual - This constructs a pointer to an object of the
531   /// specified type in the generic address space (address space zero).
532   static PointerType *getUnqual(const Type *ElementType) {
533     return PointerType::get(ElementType, 0);
534   }
535
536   /// isValidElementType - Return true if the specified type is valid as a
537   /// element type.
538   static bool isValidElementType(const Type *ElemTy);
539
540   /// @brief Return the address space of the Pointer type.
541   inline unsigned getAddressSpace() const { return AddressSpace; }
542
543   // Implement the AbstractTypeUser interface.
544   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
545   virtual void typeBecameConcrete(const DerivedType *AbsTy);
546
547   // Implement support type inquiry through isa, cast, and dyn_cast:
548   static inline bool classof(const PointerType *) { return true; }
549   static inline bool classof(const Type *T) {
550     return T->getTypeID() == PointerTyID;
551   }
552 };
553
554
555 /// OpaqueType - Class to represent abstract types
556 ///
557 class OpaqueType : public DerivedType {
558   friend class LLVMContextImpl;
559   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
560   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
561   OpaqueType(LLVMContext &C);
562 public:
563   /// OpaqueType::get - Static factory method for the OpaqueType class...
564   ///
565   static OpaqueType *get(LLVMContext &C);
566
567   // Implement support for type inquiry through isa, cast, and dyn_cast:
568   static inline bool classof(const OpaqueType *) { return true; }
569   static inline bool classof(const Type *T) {
570     return T->getTypeID() == OpaqueTyID;
571   }
572 };
573
574 } // End llvm namespace
575
576 #endif