1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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...
14 // The implementations of these classes live in the Type.cpp file.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
21 #include "llvm/Type.h"
22 #include "llvm/ADT/SmallVector.h"
27 template<class ValType, class TypeClass> class TypeMap;
28 class FunctionValType;
36 class DerivedType : public Type {
40 explicit DerivedType(TypeID id) : Type(id) {}
42 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
43 /// that the current type has transitioned from being abstract to being
46 void notifyUsesThatTypeBecameConcrete();
48 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
49 /// another (more concrete) type, we must eliminate all references to other
50 /// types, to avoid some circular reference problems.
52 void dropAllTypeUses();
56 //===--------------------------------------------------------------------===//
57 // Abstract Type handling methods - These types have special lifetimes, which
58 // are managed by (add|remove)AbstractTypeUser. See comments in
59 // AbstractTypeUser.h for more information.
61 /// refineAbstractTypeTo - This function is used to when it is discovered that
62 /// the 'this' abstract type is actually equivalent to the NewType specified.
63 /// This causes all users of 'this' to switch to reference the more concrete
64 /// type NewType and for 'this' to be deleted.
66 void refineAbstractTypeTo(const Type *NewType);
68 void dump() const { Type::dump(); }
70 // Methods for support type inquiry through isa, cast, and dyn_cast:
71 static inline bool classof(const DerivedType *T) { return true; }
72 static inline bool classof(const Type *T) {
73 return T->isDerivedType();
77 /// Class to represent integer types. Note that this class is also used to
78 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
80 /// @brief Integer representation type
81 class IntegerType : public DerivedType {
83 explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
84 setSubclassData(NumBits);
86 friend class TypeMap<IntegerValType, IntegerType>;
88 /// This enum is just used to hold constants we need for IntegerType.
90 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
91 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
92 ///< Note that bit width is stored in the Type classes SubclassData field
93 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
96 /// This static method is the primary way of constructing an IntegerType.
97 /// If an IntegerType with the same NumBits value was previously instantiated,
98 /// that instance will be returned. Otherwise a new one will be created. Only
99 /// one instance with a given NumBits value is ever created.
100 /// @brief Get or create an IntegerType instance.
101 static const IntegerType* get(unsigned NumBits);
103 /// @brief Get the number of bits in this IntegerType
104 unsigned getBitWidth() const { return getSubclassData(); }
106 /// getBitMask - Return a bitmask with ones set for all of the bits
107 /// that can be set by an unsigned version of this type. This is 0xFF for
108 /// sbyte/ubyte, 0xFFFF for shorts, etc.
109 uint64_t getBitMask() const {
110 return ~uint64_t(0UL) >> (64-getBitWidth());
113 /// getSignBit - Return a uint64_t with just the most significant bit set (the
114 /// sign bit, if the value is treated as a signed number).
115 uint64_t getSignBit() const {
116 return 1ULL << (getBitWidth()-1);
119 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
120 /// @returns a bit mask with ones set for all the bits of this type.
121 /// @brief Get a bit mask for this type.
122 APInt getMask() const;
124 /// This method determines if the width of this IntegerType is a power-of-2
125 /// in terms of 8 bit bytes.
126 /// @returns true if this is a power-of-2 byte width.
127 /// @brief Is this a power-of-2 byte-width IntegerType ?
128 bool isPowerOf2ByteWidth() const;
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const IntegerType *T) { return true; }
132 static inline bool classof(const Type *T) {
133 return T->getTypeID() == IntegerTyID;
138 /// FunctionType - Class to represent function types
140 class FunctionType : public DerivedType {
141 friend class TypeMap<FunctionValType, FunctionType>;
144 FunctionType(const FunctionType &); // Do not implement
145 const FunctionType &operator=(const FunctionType &); // Do not implement
146 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
148 FunctionType(const Type *Result, const SmallVectorImpl<const Type*> &Params,
152 /// FunctionType::get - This static method is the primary way of constructing
155 static FunctionType *get(
156 const Type *Result, ///< The result type
157 const std::vector<const Type*> &Params, ///< The types of the parameters
158 bool isVarArg ///< Whether this is a variable argument length function
161 static FunctionType *get(
162 const Type *Result, ///< The result type
163 const SmallVectorImpl<const Type*> &Params, ///< The types of the parameters
164 bool isVarArg ///< Whether this is a variable argument length function
167 inline bool isVarArg() const { return isVarArgs; }
168 inline const Type *getReturnType() const { return ContainedTys[0]; }
170 typedef Type::subtype_iterator param_iterator;
171 param_iterator param_begin() const { return ContainedTys + 1; }
172 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
174 // Parameter type accessors...
175 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
177 /// getNumParams - Return the number of fixed parameters this function type
178 /// requires. This does not consider varargs.
180 unsigned getNumParams() const { return NumContainedTys - 1; }
182 // Implement the AbstractTypeUser interface.
183 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
184 virtual void typeBecameConcrete(const DerivedType *AbsTy);
186 // Methods for support type inquiry through isa, cast, and dyn_cast:
187 static inline bool classof(const FunctionType *T) { return true; }
188 static inline bool classof(const Type *T) {
189 return T->getTypeID() == FunctionTyID;
194 /// CompositeType - Common super class of ArrayType, StructType, PointerType
196 class CompositeType : public DerivedType {
198 inline explicit CompositeType(TypeID id) : DerivedType(id) { }
201 /// getTypeAtIndex - Given an index value into the type, return the type of
204 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
205 virtual bool indexValid(const Value *V) const = 0;
207 // Methods for support type inquiry through isa, cast, and dyn_cast:
208 static inline bool classof(const CompositeType *T) { return true; }
209 static inline bool classof(const Type *T) {
210 return T->getTypeID() == ArrayTyID ||
211 T->getTypeID() == StructTyID ||
212 T->getTypeID() == PointerTyID ||
213 T->getTypeID() == VectorTyID;
218 /// StructType - Class to represent struct types
220 class StructType : public CompositeType {
221 friend class TypeMap<StructValType, StructType>;
222 StructType(const StructType &); // Do not implement
223 const StructType &operator=(const StructType &); // Do not implement
224 StructType(const std::vector<const Type*> &Types, bool isPacked);
226 /// StructType::get - This static method is the primary way to create a
229 static StructType *get(const std::vector<const Type*> &Params,
230 bool isPacked=false);
232 // Iterator access to the elements
233 typedef Type::subtype_iterator element_iterator;
234 element_iterator element_begin() const { return ContainedTys; }
235 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
237 // Random access to the elements
238 unsigned getNumElements() const { return NumContainedTys; }
239 const Type *getElementType(unsigned N) const {
240 assert(N < NumContainedTys && "Element number out of range!");
241 return ContainedTys[N];
244 /// getTypeAtIndex - Given an index value into the type, return the type of
245 /// the element. For a structure type, this must be a constant value...
247 virtual const Type *getTypeAtIndex(const Value *V) const ;
248 virtual bool indexValid(const Value *V) const;
250 // Implement the AbstractTypeUser interface.
251 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
252 virtual void typeBecameConcrete(const DerivedType *AbsTy);
254 // Methods for support type inquiry through isa, cast, and dyn_cast:
255 static inline bool classof(const StructType *T) { return true; }
256 static inline bool classof(const Type *T) {
257 return T->getTypeID() == StructTyID;
260 bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
264 /// SequentialType - This is the superclass of the array, pointer and vector
265 /// type classes. All of these represent "arrays" in memory. The array type
266 /// represents a specifically sized array, pointer types are unsized/unknown
267 /// size arrays, vector types represent specifically sized arrays that
268 /// allow for use of SIMD instructions. SequentialType holds the common
269 /// features of all, which stem from the fact that all three lay their
270 /// components out in memory identically.
272 class SequentialType : public CompositeType {
273 PATypeHandle ContainedType; ///< Storage for the single contained type
274 SequentialType(const SequentialType &); // Do not implement!
275 const SequentialType &operator=(const SequentialType &); // Do not implement!
277 // avoiding warning: 'this' : used in base member initializer list
278 SequentialType* this_() { return this; }
280 SequentialType(TypeID TID, const Type *ElType)
281 : CompositeType(TID), ContainedType(ElType, this_()) {
282 ContainedTys = &ContainedType;
287 inline const Type *getElementType() const { return ContainedTys[0]; }
289 virtual bool indexValid(const Value *V) const;
291 /// getTypeAtIndex - Given an index value into the type, return the type of
292 /// the element. For sequential types, there is only one subtype...
294 virtual const Type *getTypeAtIndex(const Value *V) const {
295 return ContainedTys[0];
298 // Methods for support type inquiry through isa, cast, and dyn_cast:
299 static inline bool classof(const SequentialType *T) { return true; }
300 static inline bool classof(const Type *T) {
301 return T->getTypeID() == ArrayTyID ||
302 T->getTypeID() == PointerTyID ||
303 T->getTypeID() == VectorTyID;
308 /// ArrayType - Class to represent array types
310 class ArrayType : public SequentialType {
311 friend class TypeMap<ArrayValType, ArrayType>;
312 uint64_t NumElements;
314 ArrayType(const ArrayType &); // Do not implement
315 const ArrayType &operator=(const ArrayType &); // Do not implement
316 ArrayType(const Type *ElType, uint64_t NumEl);
318 /// ArrayType::get - This static method is the primary way to construct an
321 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
323 inline uint64_t getNumElements() const { return NumElements; }
325 // Implement the AbstractTypeUser interface.
326 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
327 virtual void typeBecameConcrete(const DerivedType *AbsTy);
329 // Methods for support type inquiry through isa, cast, and dyn_cast:
330 static inline bool classof(const ArrayType *T) { return true; }
331 static inline bool classof(const Type *T) {
332 return T->getTypeID() == ArrayTyID;
336 /// VectorType - Class to represent vector types
338 class VectorType : public SequentialType {
339 friend class TypeMap<VectorValType, VectorType>;
340 unsigned NumElements;
342 VectorType(const VectorType &); // Do not implement
343 const VectorType &operator=(const VectorType &); // Do not implement
344 VectorType(const Type *ElType, unsigned NumEl);
346 /// VectorType::get - This static method is the primary way to construct an
349 static VectorType *get(const Type *ElementType, unsigned NumElements);
351 /// @brief Return the number of elements in the Vector type.
352 inline unsigned getNumElements() const { return NumElements; }
354 /// @brief Return the number of bits in the Vector type.
355 inline unsigned getBitWidth() const {
356 return NumElements *getElementType()->getPrimitiveSizeInBits();
359 // Implement the AbstractTypeUser interface.
360 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
361 virtual void typeBecameConcrete(const DerivedType *AbsTy);
363 // Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const VectorType *T) { return true; }
365 static inline bool classof(const Type *T) {
366 return T->getTypeID() == VectorTyID;
371 /// PointerType - Class to represent pointers
373 class PointerType : public SequentialType {
374 friend class TypeMap<PointerValType, PointerType>;
375 unsigned AddressSpace;
377 PointerType(const PointerType &); // Do not implement
378 const PointerType &operator=(const PointerType &); // Do not implement
379 explicit PointerType(const Type *ElType, unsigned AddrSpace);
381 /// PointerType::get - This constructs a pointer to an object of the specified
382 /// type in a numbered address space.
383 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
385 /// PointerType::getUnqual - This constructs a pointer to an object of the
386 /// specified type in the generic address space (address space zero).
387 static PointerType *getUnqual(const Type *ElementType) {
388 return PointerType::get(ElementType, 0);
391 /// @brief Return the address space of the Pointer type.
392 inline unsigned getAddressSpace() const { return AddressSpace; }
394 // Implement the AbstractTypeUser interface.
395 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
396 virtual void typeBecameConcrete(const DerivedType *AbsTy);
398 // Implement support type inquiry through isa, cast, and dyn_cast:
399 static inline bool classof(const PointerType *T) { return true; }
400 static inline bool classof(const Type *T) {
401 return T->getTypeID() == PointerTyID;
406 /// OpaqueType - Class to represent abstract types
408 class OpaqueType : public DerivedType {
409 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
410 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
413 /// OpaqueType::get - Static factory method for the OpaqueType class...
415 static OpaqueType *get() {
416 return new OpaqueType(); // All opaque types are distinct
419 // Implement support for type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const OpaqueType *T) { return true; }
421 static inline bool classof(const Type *T) {
422 return T->getTypeID() == OpaqueTyID;
426 } // End llvm namespace