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"
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
36 class DerivedType : public Type {
40 explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, 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();
54 /// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo
55 /// that performs no locking. Only used for internal recursion.
56 void unlockedRefineAbstractTypeTo(const Type *NewType);
60 //===--------------------------------------------------------------------===//
61 // Abstract Type handling methods - These types have special lifetimes, which
62 // are managed by (add|remove)AbstractTypeUser. See comments in
63 // AbstractTypeUser.h for more information.
65 /// refineAbstractTypeTo - This function is used to when it is discovered that
66 /// the 'this' abstract type is actually equivalent to the NewType specified.
67 /// This causes all users of 'this' to switch to reference the more concrete
68 /// type NewType and for 'this' to be deleted.
70 void refineAbstractTypeTo(const Type *NewType);
72 void dump() const { Type::dump(); }
74 // Methods for support type inquiry through isa, cast, and dyn_cast:
75 static inline bool classof(const DerivedType *) { return true; }
76 static inline bool classof(const Type *T) {
77 return T->isDerivedType();
81 /// Class to represent integer types. Note that this class is also used to
82 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
84 /// @brief Integer representation type
85 class IntegerType : public DerivedType {
86 friend class LLVMContextImpl;
89 explicit IntegerType(LLVMContext &C, unsigned NumBits) :
90 DerivedType(C, IntegerTyID) {
91 setSubclassData(NumBits);
93 friend class TypeMap<IntegerValType, IntegerType>;
95 /// This enum is just used to hold constants we need for IntegerType.
97 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
98 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
99 ///< Note that bit width is stored in the Type classes SubclassData field
100 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
103 /// This static method is the primary way of constructing an IntegerType.
104 /// If an IntegerType with the same NumBits value was previously instantiated,
105 /// that instance will be returned. Otherwise a new one will be created. Only
106 /// one instance with a given NumBits value is ever created.
107 /// @brief Get or create an IntegerType instance.
108 static const IntegerType* get(LLVMContext &C, unsigned NumBits);
110 /// @brief Get the number of bits in this IntegerType
111 unsigned getBitWidth() const { return getSubclassData(); }
113 /// getBitMask - Return a bitmask with ones set for all of the bits
114 /// that can be set by an unsigned version of this type. This is 0xFF for
115 /// i8, 0xFFFF for i16, etc.
116 uint64_t getBitMask() const {
117 return ~uint64_t(0UL) >> (64-getBitWidth());
120 /// getSignBit - Return a uint64_t with just the most significant bit set (the
121 /// sign bit, if the value is treated as a signed number).
122 uint64_t getSignBit() const {
123 return 1ULL << (getBitWidth()-1);
126 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
127 /// @returns a bit mask with ones set for all the bits of this type.
128 /// @brief Get a bit mask for this type.
129 APInt getMask() const;
131 /// This method determines if the width of this IntegerType is a power-of-2
132 /// in terms of 8 bit bytes.
133 /// @returns true if this is a power-of-2 byte width.
134 /// @brief Is this a power-of-2 byte-width IntegerType ?
135 bool isPowerOf2ByteWidth() const;
137 // Methods for support type inquiry through isa, cast, and dyn_cast:
138 static inline bool classof(const IntegerType *) { return true; }
139 static inline bool classof(const Type *T) {
140 return T->getTypeID() == IntegerTyID;
145 /// FunctionType - Class to represent function types
147 class FunctionType : public DerivedType {
148 friend class TypeMap<FunctionValType, FunctionType>;
151 FunctionType(const FunctionType &); // Do not implement
152 const FunctionType &operator=(const FunctionType &); // Do not implement
153 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
157 /// FunctionType::get - This static method is the primary way of constructing
160 static FunctionType *get(
161 const Type *Result, ///< The result type
162 const std::vector<const Type*> &Params, ///< The types of the parameters
163 bool isVarArg ///< Whether this is a variable argument length function
166 /// FunctionType::get - Create a FunctionType taking no parameters.
168 static FunctionType *get(
169 const Type *Result, ///< The result type
170 bool isVarArg ///< Whether this is a variable argument length function
172 return get(Result, std::vector<const Type *>(), isVarArg);
175 /// isValidReturnType - Return true if the specified type is valid as a return
177 static bool isValidReturnType(const Type *RetTy);
179 /// isValidArgumentType - Return true if the specified type is valid as an
181 static bool isValidArgumentType(const Type *ArgTy);
183 inline bool isVarArg() const { return isVarArgs; }
184 inline const Type *getReturnType() const { return ContainedTys[0]; }
186 typedef Type::subtype_iterator param_iterator;
187 param_iterator param_begin() const { return ContainedTys + 1; }
188 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
190 // Parameter type accessors...
191 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
193 /// getNumParams - Return the number of fixed parameters this function type
194 /// requires. This does not consider varargs.
196 unsigned getNumParams() const { return NumContainedTys - 1; }
198 // Implement the AbstractTypeUser interface.
199 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
200 virtual void typeBecameConcrete(const DerivedType *AbsTy);
202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const FunctionType *) { return true; }
204 static inline bool classof(const Type *T) {
205 return T->getTypeID() == FunctionTyID;
210 /// CompositeType - Common super class of ArrayType, StructType, PointerType
212 class CompositeType : public DerivedType {
214 inline explicit CompositeType(LLVMContext &C, TypeID id) :
215 DerivedType(C, id) { }
218 /// getTypeAtIndex - Given an index value into the type, return the type of
221 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
222 virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
223 virtual bool indexValid(const Value *V) const = 0;
224 virtual bool indexValid(unsigned Idx) const = 0;
226 // Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const CompositeType *) { return true; }
228 static inline bool classof(const Type *T) {
229 return T->getTypeID() == ArrayTyID ||
230 T->getTypeID() == StructTyID ||
231 T->getTypeID() == PointerTyID ||
232 T->getTypeID() == VectorTyID;
237 /// StructType - Class to represent struct types
239 class StructType : public CompositeType {
240 friend class TypeMap<StructValType, StructType>;
241 StructType(const StructType &); // Do not implement
242 const StructType &operator=(const StructType &); // Do not implement
243 StructType(LLVMContext &C,
244 const std::vector<const Type*> &Types, bool isPacked);
246 /// StructType::get - This static method is the primary way to create a
249 static StructType *get(LLVMContext &Context,
250 const std::vector<const Type*> &Params,
251 bool isPacked=false);
253 /// StructType::get - Create an empty structure type.
255 static StructType *get(LLVMContext &Context, bool isPacked=false) {
256 return get(Context, std::vector<const Type*>(), isPacked);
259 /// StructType::get - This static method is a convenience method for
260 /// creating structure types by specifying the elements as arguments.
261 /// Note that this method always returns a non-packed struct. To get
262 /// an empty struct, pass NULL, NULL.
263 static StructType *get(LLVMContext &Context,
264 const Type *type, ...) END_WITH_NULL;
266 /// isValidElementType - Return true if the specified type is valid as a
268 static bool isValidElementType(const Type *ElemTy);
270 // Iterator access to the elements
271 typedef Type::subtype_iterator element_iterator;
272 element_iterator element_begin() const { return ContainedTys; }
273 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
275 // Random access to the elements
276 unsigned getNumElements() const { return NumContainedTys; }
277 const Type *getElementType(unsigned N) const {
278 assert(N < NumContainedTys && "Element number out of range!");
279 return ContainedTys[N];
282 /// getTypeAtIndex - Given an index value into the type, return the type of
283 /// the element. For a structure type, this must be a constant value...
285 virtual const Type *getTypeAtIndex(const Value *V) const;
286 virtual const Type *getTypeAtIndex(unsigned Idx) const;
287 virtual bool indexValid(const Value *V) const;
288 virtual bool indexValid(unsigned Idx) const;
290 // Implement the AbstractTypeUser interface.
291 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
292 virtual void typeBecameConcrete(const DerivedType *AbsTy);
294 // Methods for support type inquiry through isa, cast, and dyn_cast:
295 static inline bool classof(const StructType *) { return true; }
296 static inline bool classof(const Type *T) {
297 return T->getTypeID() == StructTyID;
300 bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
304 /// SequentialType - This is the superclass of the array, pointer and vector
305 /// type classes. All of these represent "arrays" in memory. The array type
306 /// represents a specifically sized array, pointer types are unsized/unknown
307 /// size arrays, vector types represent specifically sized arrays that
308 /// allow for use of SIMD instructions. SequentialType holds the common
309 /// features of all, which stem from the fact that all three lay their
310 /// components out in memory identically.
312 class SequentialType : public CompositeType {
313 PATypeHandle ContainedType; ///< Storage for the single contained type
314 SequentialType(const SequentialType &); // Do not implement!
315 const SequentialType &operator=(const SequentialType &); // Do not implement!
317 // avoiding warning: 'this' : used in base member initializer list
318 SequentialType* this_() { return this; }
320 SequentialType(TypeID TID, const Type *ElType)
321 : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
322 ContainedTys = &ContainedType;
327 inline const Type *getElementType() const { return ContainedTys[0]; }
329 virtual bool indexValid(const Value *V) const;
330 virtual bool indexValid(unsigned) const {
334 /// getTypeAtIndex - Given an index value into the type, return the type of
335 /// the element. For sequential types, there is only one subtype...
337 virtual const Type *getTypeAtIndex(const Value *) const {
338 return ContainedTys[0];
340 virtual const Type *getTypeAtIndex(unsigned) const {
341 return ContainedTys[0];
344 // Methods for support type inquiry through isa, cast, and dyn_cast:
345 static inline bool classof(const SequentialType *) { return true; }
346 static inline bool classof(const Type *T) {
347 return T->getTypeID() == ArrayTyID ||
348 T->getTypeID() == PointerTyID ||
349 T->getTypeID() == VectorTyID;
354 /// ArrayType - Class to represent array types
356 class ArrayType : public SequentialType {
357 friend class TypeMap<ArrayValType, ArrayType>;
358 uint64_t NumElements;
360 ArrayType(const ArrayType &); // Do not implement
361 const ArrayType &operator=(const ArrayType &); // Do not implement
362 ArrayType(const Type *ElType, uint64_t NumEl);
364 /// ArrayType::get - This static method is the primary way to construct an
367 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
369 /// isValidElementType - Return true if the specified type is valid as a
371 static bool isValidElementType(const Type *ElemTy);
373 inline uint64_t getNumElements() const { return NumElements; }
375 // Implement the AbstractTypeUser interface.
376 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
377 virtual void typeBecameConcrete(const DerivedType *AbsTy);
379 // Methods for support type inquiry through isa, cast, and dyn_cast:
380 static inline bool classof(const ArrayType *) { return true; }
381 static inline bool classof(const Type *T) {
382 return T->getTypeID() == ArrayTyID;
386 /// VectorType - Class to represent vector types
388 class VectorType : public SequentialType {
389 friend class TypeMap<VectorValType, VectorType>;
390 unsigned NumElements;
392 VectorType(const VectorType &); // Do not implement
393 const VectorType &operator=(const VectorType &); // Do not implement
394 VectorType(const Type *ElType, unsigned NumEl);
396 /// VectorType::get - This static method is the primary way to construct an
399 static VectorType *get(const Type *ElementType, unsigned NumElements);
401 /// VectorType::getInteger - This static method gets a VectorType with the
402 /// same number of elements as the input type, and the element type is an
403 /// integer type of the same width as the input element type.
405 static VectorType *getInteger(const VectorType *VTy) {
406 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
407 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
408 return VectorType::get(EltTy, VTy->getNumElements());
411 /// VectorType::getExtendedElementVectorType - This static method is like
412 /// getInteger except that the element types are twice as wide as the
413 /// elements in the input type.
415 static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
416 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
417 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
418 return VectorType::get(EltTy, VTy->getNumElements());
421 /// VectorType::getTruncatedElementVectorType - This static method is like
422 /// getInteger except that the element types are half as wide as the
423 /// elements in the input type.
425 static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
426 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
427 assert((EltBits & 1) == 0 &&
428 "Cannot truncate vector element with odd bit-width");
429 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
430 return VectorType::get(EltTy, VTy->getNumElements());
433 /// isValidElementType - Return true if the specified type is valid as a
435 static bool isValidElementType(const Type *ElemTy);
437 /// @brief Return the number of elements in the Vector type.
438 inline unsigned getNumElements() const { return NumElements; }
440 /// @brief Return the number of bits in the Vector type.
441 inline unsigned getBitWidth() const {
442 return NumElements * getElementType()->getPrimitiveSizeInBits();
445 // Implement the AbstractTypeUser interface.
446 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
447 virtual void typeBecameConcrete(const DerivedType *AbsTy);
449 // Methods for support type inquiry through isa, cast, and dyn_cast:
450 static inline bool classof(const VectorType *) { return true; }
451 static inline bool classof(const Type *T) {
452 return T->getTypeID() == VectorTyID;
457 /// PointerType - Class to represent pointers
459 class PointerType : public SequentialType {
460 friend class TypeMap<PointerValType, PointerType>;
461 unsigned AddressSpace;
463 PointerType(const PointerType &); // Do not implement
464 const PointerType &operator=(const PointerType &); // Do not implement
465 explicit PointerType(const Type *ElType, unsigned AddrSpace);
467 /// PointerType::get - This constructs a pointer to an object of the specified
468 /// type in a numbered address space.
469 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
471 /// PointerType::getUnqual - This constructs a pointer to an object of the
472 /// specified type in the generic address space (address space zero).
473 static PointerType *getUnqual(const Type *ElementType) {
474 return PointerType::get(ElementType, 0);
477 /// isValidElementType - Return true if the specified type is valid as a
479 static bool isValidElementType(const Type *ElemTy);
481 /// @brief Return the address space of the Pointer type.
482 inline unsigned getAddressSpace() const { return AddressSpace; }
484 // Implement the AbstractTypeUser interface.
485 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
486 virtual void typeBecameConcrete(const DerivedType *AbsTy);
488 // Implement support type inquiry through isa, cast, and dyn_cast:
489 static inline bool classof(const PointerType *) { return true; }
490 static inline bool classof(const Type *T) {
491 return T->getTypeID() == PointerTyID;
496 /// OpaqueType - Class to represent abstract types
498 class OpaqueType : public DerivedType {
499 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
500 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
501 OpaqueType(LLVMContext &C);
503 /// OpaqueType::get - Static factory method for the OpaqueType class...
505 static OpaqueType *get(LLVMContext &C) {
506 return new OpaqueType(C); // All opaque types are distinct
509 // Implement support for type inquiry through isa, cast, and dyn_cast:
510 static inline bool classof(const OpaqueType *) { return true; }
511 static inline bool classof(const Type *T) {
512 return T->getTypeID() == OpaqueTyID;
516 } // End llvm namespace