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;
37
class DerivedType : public Type {
41
explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
43
/// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
44
/// that the current type has transitioned from being abstract to being
47
void notifyUsesThatTypeBecameConcrete();
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.
53
void dropAllTypeUses();
55
/// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo
56
/// that performs no locking. Only used for internal recursion.
57
void unlockedRefineAbstractTypeTo(const Type *NewType);
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.
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.
71
void refineAbstractTypeTo(const Type *NewType);
73
void dump() const { Type::dump(); }
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();
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
85
/// @brief Integer representation type
86
class IntegerType : public DerivedType {
87
friend class LLVMContextImpl;
90
explicit IntegerType(LLVMContext &C, unsigned NumBits) :
91
DerivedType(C, IntegerTyID) {
92
setSubclassData(NumBits);
94
friend class TypeMap<IntegerValType, IntegerType>;
96
/// This enum is just used to hold constants we need for IntegerType.
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.
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);
111
/// @brief Get the number of bits in this IntegerType
112
unsigned getBitWidth() const { return getSubclassData(); }
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());
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);
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;
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;
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;
146
/// FunctionType - Class to represent function types
148
class FunctionType : public DerivedType {
149
friend class TypeMap<FunctionValType, FunctionType>;
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,
158
/// FunctionType::get - This static method is the primary way of constructing
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
167
/// FunctionType::get - Create a FunctionType taking no parameters.
169
static FunctionType *get(
170
const Type *Result, ///< The result type
171
bool isVarArg ///< Whether this is a variable argument length function
173
return get(Result, std::vector<const Type *>(), isVarArg);
176
/// isValidReturnType - Return true if the specified type is valid as a return
178
static bool isValidReturnType(const Type *RetTy);
180
/// isValidArgumentType - Return true if the specified type is valid as an
182
static bool isValidArgumentType(const Type *ArgTy);
184
inline bool isVarArg() const { return isVarArgs; }
185
inline const Type *getReturnType() const { return ContainedTys[0]; }
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]; }
191
// Parameter type accessors...
192
const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
194
/// getNumParams - Return the number of fixed parameters this function type
195
/// requires. This does not consider varargs.
197
unsigned getNumParams() const { return NumContainedTys - 1; }
199
// Implement the AbstractTypeUser interface.
200
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
201
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
211
/// CompositeType - Common super class of ArrayType, StructType, PointerType
213
class CompositeType : public DerivedType {
215
inline explicit CompositeType(LLVMContext &C, TypeID id) :
216
DerivedType(C, id) { }
219
/// getTypeAtIndex - Given an index value into the type, return the type of
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;
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;
239
/// StructType - Class to represent struct types
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);
248
/// StructType::get - This static method is the primary way to create a
251
static StructType *get(LLVMContext &Context,
252
const std::vector<const Type*> &Params,
253
bool isPacked=false);
255
/// StructType::get - Create an empty structure type.
257
static StructType *get(LLVMContext &Context, bool isPacked=false) {
258
return get(Context, std::vector<const Type*>(), isPacked);
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;
268
/// isValidElementType - Return true if the specified type is valid as a
270
static bool isValidElementType(const Type *ElemTy);
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];}
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];
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...
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;
292
// Implement the AbstractTypeUser interface.
293
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
294
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
302
bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
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.
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);
315
/// UnionType::get - This static method is the primary way to create a
317
static UnionType *get(const Type* const* Types, unsigned NumTypes);
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;
323
/// isValidElementType - Return true if the specified type is valid as a
325
static bool isValidElementType(const Type *ElemTy);
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;
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];}
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];
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...
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;
351
// Implement the AbstractTypeUser interface.
352
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
353
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
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.
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!
376
// avoiding warning: 'this' : used in base member initializer list
377
SequentialType* this_() { return this; }
379
SequentialType(TypeID TID, const Type *ElType)
380
: CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
381
ContainedTys = &ContainedType;
386
inline const Type *getElementType() const { return ContainedTys[0]; }
388
virtual bool indexValid(const Value *V) const;
389
virtual bool indexValid(unsigned) const {
393
/// getTypeAtIndex - Given an index value into the type, return the type of
394
/// the element. For sequential types, there is only one subtype...
396
virtual const Type *getTypeAtIndex(const Value *) const {
397
return ContainedTys[0];
399
virtual const Type *getTypeAtIndex(unsigned) const {
400
return ContainedTys[0];
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;
413
/// ArrayType - Class to represent array types
415
class ArrayType : public SequentialType {
416
friend class TypeMap<ArrayValType, ArrayType>;
417
uint64_t NumElements;
419
ArrayType(const ArrayType &); // Do not implement
420
const ArrayType &operator=(const ArrayType &); // Do not implement
421
ArrayType(const Type *ElType, uint64_t NumEl);
423
/// ArrayType::get - This static method is the primary way to construct an
426
static ArrayType *get(const Type *ElementType, uint64_t NumElements);
428
/// isValidElementType - Return true if the specified type is valid as a
430
static bool isValidElementType(const Type *ElemTy);
432
inline uint64_t getNumElements() const { return NumElements; }
434
// Implement the AbstractTypeUser interface.
435
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
436
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
445
/// VectorType - Class to represent vector types
447
class VectorType : public SequentialType {
448
friend class TypeMap<VectorValType, VectorType>;
449
unsigned NumElements;
451
VectorType(const VectorType &); // Do not implement
452
const VectorType &operator=(const VectorType &); // Do not implement
453
VectorType(const Type *ElType, unsigned NumEl);
455
/// VectorType::get - This static method is the primary way to construct an
458
static VectorType *get(const Type *ElementType, unsigned NumElements);
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.
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());
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.
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());
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.
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());
492
/// isValidElementType - Return true if the specified type is valid as a
494
static bool isValidElementType(const Type *ElemTy);
496
/// @brief Return the number of elements in the Vector type.
497
inline unsigned getNumElements() const { return NumElements; }
499
/// @brief Return the number of bits in the Vector type.
500
inline unsigned getBitWidth() const {
501
return NumElements * getElementType()->getPrimitiveSizeInBits();
504
// Implement the AbstractTypeUser interface.
505
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
506
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
516
/// PointerType - Class to represent pointers
518
class PointerType : public SequentialType {
519
friend class TypeMap<PointerValType, PointerType>;
520
unsigned AddressSpace;
522
PointerType(const PointerType &); // Do not implement
523
const PointerType &operator=(const PointerType &); // Do not implement
524
explicit PointerType(const Type *ElType, unsigned AddrSpace);
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);
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);
536
/// isValidElementType - Return true if the specified type is valid as a
538
static bool isValidElementType(const Type *ElemTy);
540
/// @brief Return the address space of the Pointer type.
541
inline unsigned getAddressSpace() const { return AddressSpace; }
543
// Implement the AbstractTypeUser interface.
544
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
545
virtual void typeBecameConcrete(const DerivedType *AbsTy);
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;
555
/// OpaqueType - Class to represent abstract types
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);
563
/// OpaqueType::get - Static factory method for the OpaqueType class...
565
static OpaqueType *get(LLVMContext &C);
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;
574
} // End llvm namespace