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 // "function 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/Support/DataTypes.h"
29 template<typename T> class ArrayRef;
32 /// Class to represent integer types. Note that this class is also used to
33 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
35 /// @brief Integer representation type
36 class IntegerType : public Type {
37 friend class LLVMContextImpl;
40 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
41 setSubclassData(NumBits);
44 /// This enum is just used to hold constants we need for IntegerType.
46 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
47 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
48 ///< Note that bit width is stored in the Type classes SubclassData field
49 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
52 /// This static method is the primary way of constructing an IntegerType.
53 /// If an IntegerType with the same NumBits value was previously instantiated,
54 /// that instance will be returned. Otherwise a new one will be created. Only
55 /// one instance with a given NumBits value is ever created.
56 /// @brief Get or create an IntegerType instance.
57 static IntegerType *get(LLVMContext &C, unsigned NumBits);
59 /// @brief Get the number of bits in this IntegerType
60 unsigned getBitWidth() const { return getSubclassData(); }
62 /// getBitMask - Return a bitmask with ones set for all of the bits
63 /// that can be set by an unsigned version of this type. This is 0xFF for
64 /// i8, 0xFFFF for i16, etc.
65 uint64_t getBitMask() const {
66 return ~uint64_t(0UL) >> (64-getBitWidth());
69 /// getSignBit - Return a uint64_t with just the most significant bit set (the
70 /// sign bit, if the value is treated as a signed number).
71 uint64_t getSignBit() const {
72 return 1ULL << (getBitWidth()-1);
75 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
76 /// @returns a bit mask with ones set for all the bits of this type.
77 /// @brief Get a bit mask for this type.
78 APInt getMask() const;
80 /// This method determines if the width of this IntegerType is a power-of-2
81 /// in terms of 8 bit bytes.
82 /// @returns true if this is a power-of-2 byte width.
83 /// @brief Is this a power-of-2 byte-width IntegerType ?
84 bool isPowerOf2ByteWidth() const;
86 // Methods for support type inquiry through isa, cast, and dyn_cast.
87 static inline bool classof(const IntegerType *) { return true; }
88 static inline bool classof(const Type *T) {
89 return T->getTypeID() == IntegerTyID;
94 /// FunctionType - Class to represent function types
96 class FunctionType : public Type {
97 FunctionType(const FunctionType &); // Do not implement
98 const FunctionType &operator=(const FunctionType &); // Do not implement
99 FunctionType(const Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
102 /// FunctionType::get - This static method is the primary way of constructing
105 static FunctionType *get(const Type *Result,
106 ArrayRef<const Type*> Params, bool isVarArg);
107 static FunctionType *get(const Type *Result,
108 ArrayRef<Type*> Params, bool isVarArg);
110 /// FunctionType::get - Create a FunctionType taking no parameters.
112 static FunctionType *get(const Type *Result, bool isVarArg);
114 /// isValidReturnType - Return true if the specified type is valid as a return
116 static bool isValidReturnType(const Type *RetTy);
118 /// isValidArgumentType - Return true if the specified type is valid as an
120 static bool isValidArgumentType(const Type *ArgTy);
122 bool isVarArg() const { return getSubclassData(); }
123 Type *getReturnType() const { return ContainedTys[0]; }
125 typedef Type::subtype_iterator param_iterator;
126 param_iterator param_begin() const { return ContainedTys + 1; }
127 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
129 // Parameter type accessors.
130 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
132 /// getNumParams - Return the number of fixed parameters this function type
133 /// requires. This does not consider varargs.
135 unsigned getNumParams() const { return NumContainedTys - 1; }
137 // Methods for support type inquiry through isa, cast, and dyn_cast.
138 static inline bool classof(const FunctionType *) { return true; }
139 static inline bool classof(const Type *T) {
140 return T->getTypeID() == FunctionTyID;
145 /// CompositeType - Common super class of ArrayType, StructType, PointerType
147 class CompositeType : public Type {
149 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
152 /// getTypeAtIndex - Given an index value into the type, return the type of
155 Type *getTypeAtIndex(const Value *V) const;
156 Type *getTypeAtIndex(unsigned Idx) const;
157 bool indexValid(const Value *V) const;
158 bool indexValid(unsigned Idx) const;
160 // Methods for support type inquiry through isa, cast, and dyn_cast.
161 static inline bool classof(const CompositeType *) { return true; }
162 static inline bool classof(const Type *T) {
163 return T->getTypeID() == ArrayTyID ||
164 T->getTypeID() == StructTyID ||
165 T->getTypeID() == PointerTyID ||
166 T->getTypeID() == VectorTyID;
171 /// StructType - Class to represent struct types, both normal and packed.
172 /// Besides being optionally packed, structs can be either "anonymous" or may
173 /// have an identity. Anonymous structs are uniqued by structural equivalence,
174 /// but types are each unique when created, and optionally have a name.
176 class StructType : public CompositeType {
177 StructType(const StructType &); // Do not implement
178 const StructType &operator=(const StructType &); // Do not implement
179 StructType(LLVMContext &C)
180 : CompositeType(C, StructTyID), SymbolTableEntry(0) {}
182 // This is the contents of the SubClassData field.
188 /// SymbolTableEntry - For a named struct that actually has a name, this is a
189 /// pointer to the symbol table entry (maintained by LLVMContext) for the
190 /// struct. This is null if the type is an anonymous struct or if it is
191 /// a named type that has an empty name.
193 void *SymbolTableEntry;
195 /// StructType::createNamed - This creates a named struct with no body
196 /// specified. If the name is empty, it creates an unnamed struct, which has
197 /// a unique identity but no actual name.
198 static StructType *createNamed(LLVMContext &Context, StringRef Name);
200 static StructType *createNamed(StringRef Name, ArrayRef<Type*> Elements,
201 bool isPacked = false);
202 static StructType *createNamed(LLVMContext &Context, StringRef Name,
203 ArrayRef<Type*> Elements,
204 bool isPacked = false);
205 static StructType *createNamed(StringRef Name, Type *elt1, ...) END_WITH_NULL;
207 /// StructType::get - This static method is the primary way to create a
210 /// FIXME: Remove the 'const Type*' version of this when types are pervasively
212 static StructType *get(LLVMContext &Context, ArrayRef<const Type*> Elements,
213 bool isPacked = false);
214 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
215 bool isPacked = false);
217 /// StructType::get - Create an empty structure type.
219 static StructType *get(LLVMContext &Context, bool isPacked = false);
221 /// StructType::get - This static method is a convenience method for creating
222 /// structure types by specifying the elements as arguments. Note that this
223 /// method always returns a non-packed struct, and requires at least one
225 static StructType *get(const Type *elt1, ...) END_WITH_NULL;
227 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
229 /// isAnonymous - Return true if this type is uniqued by structural
230 /// equivalence, false if it has an identity.
231 bool isAnonymous() const {return (getSubclassData() & SCDB_IsAnonymous) != 0;}
233 /// isOpaque - Return true if this is a type with an identity that has no body
234 /// specified yet. These prints as 'opaque' in .ll files.
235 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
237 /// hasName - Return true if this is a named struct that has a non-empty name.
238 bool hasName() const { return SymbolTableEntry != 0; }
240 /// getName - Return the name for this struct type if it has an identity.
241 /// This may return an empty string for an unnamed struct type. Do not call
242 /// this on an anonymous type.
243 StringRef getName() const;
245 /// setName - Change the name of this type to the specified name, or to a name
246 /// with a suffix if there is a collision. Do not call this on an anonymous
248 void setName(StringRef Name);
250 /// setBody - Specify a body for an opaque type.
251 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
252 void setBody(Type *elt1, ...) END_WITH_NULL;
254 /// isValidElementType - Return true if the specified type is valid as a
256 static bool isValidElementType(const Type *ElemTy);
259 // Iterator access to the elements.
260 typedef Type::subtype_iterator element_iterator;
261 element_iterator element_begin() const { return ContainedTys; }
262 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
264 /// isLayoutIdentical - Return true if this is layout identical to the
265 /// specified struct.
266 bool isLayoutIdentical(const StructType *Other) const;
268 // Random access to the elements
269 unsigned getNumElements() const { return NumContainedTys; }
270 Type *getElementType(unsigned N) const {
271 assert(N < NumContainedTys && "Element number out of range!");
272 return ContainedTys[N];
275 // Methods for support type inquiry through isa, cast, and dyn_cast.
276 static inline bool classof(const StructType *) { return true; }
277 static inline bool classof(const Type *T) {
278 return T->getTypeID() == StructTyID;
282 /// SequentialType - This is the superclass of the array, pointer and vector
283 /// type classes. All of these represent "arrays" in memory. The array type
284 /// represents a specifically sized array, pointer types are unsized/unknown
285 /// size arrays, vector types represent specifically sized arrays that
286 /// allow for use of SIMD instructions. SequentialType holds the common
287 /// features of all, which stem from the fact that all three lay their
288 /// components out in memory identically.
290 class SequentialType : public CompositeType {
291 Type *ContainedType; ///< Storage for the single contained type.
292 SequentialType(const SequentialType &); // Do not implement!
293 const SequentialType &operator=(const SequentialType &); // Do not implement!
296 SequentialType(TypeID TID, Type *ElType)
297 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
298 ContainedTys = &ContainedType;
303 Type *getElementType() const { return ContainedTys[0]; }
305 // Methods for support type inquiry through isa, cast, and dyn_cast.
306 static inline bool classof(const SequentialType *) { return true; }
307 static inline bool classof(const Type *T) {
308 return T->getTypeID() == ArrayTyID ||
309 T->getTypeID() == PointerTyID ||
310 T->getTypeID() == VectorTyID;
315 /// ArrayType - Class to represent array types.
317 class ArrayType : public SequentialType {
318 uint64_t NumElements;
320 ArrayType(const ArrayType &); // Do not implement
321 const ArrayType &operator=(const ArrayType &); // Do not implement
322 ArrayType(Type *ElType, uint64_t NumEl);
324 /// ArrayType::get - This static method is the primary way to construct an
327 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
329 /// isValidElementType - Return true if the specified type is valid as a
331 static bool isValidElementType(const Type *ElemTy);
333 uint64_t getNumElements() const { return NumElements; }
335 // Methods for support type inquiry through isa, cast, and dyn_cast.
336 static inline bool classof(const ArrayType *) { return true; }
337 static inline bool classof(const Type *T) {
338 return T->getTypeID() == ArrayTyID;
342 /// VectorType - Class to represent vector types.
344 class VectorType : public SequentialType {
345 unsigned NumElements;
347 VectorType(const VectorType &); // Do not implement
348 const VectorType &operator=(const VectorType &); // Do not implement
349 VectorType(Type *ElType, unsigned NumEl);
351 /// VectorType::get - This static method is the primary way to construct an
354 static VectorType *get(const Type *ElementType, unsigned NumElements);
356 /// VectorType::getInteger - This static method gets a VectorType with the
357 /// same number of elements as the input type, and the element type is an
358 /// integer type of the same width as the input element type.
360 static VectorType *getInteger(const VectorType *VTy) {
361 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
362 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
363 return VectorType::get(EltTy, VTy->getNumElements());
366 /// VectorType::getExtendedElementVectorType - This static method is like
367 /// getInteger except that the element types are twice as wide as the
368 /// elements in the input type.
370 static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
371 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
372 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
373 return VectorType::get(EltTy, VTy->getNumElements());
376 /// VectorType::getTruncatedElementVectorType - This static method is like
377 /// getInteger except that the element types are half as wide as the
378 /// elements in the input type.
380 static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
381 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
382 assert((EltBits & 1) == 0 &&
383 "Cannot truncate vector element with odd bit-width");
384 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
385 return VectorType::get(EltTy, VTy->getNumElements());
388 /// isValidElementType - Return true if the specified type is valid as a
390 static bool isValidElementType(const Type *ElemTy);
392 /// @brief Return the number of elements in the Vector type.
393 unsigned getNumElements() const { return NumElements; }
395 /// @brief Return the number of bits in the Vector type.
396 unsigned getBitWidth() const {
397 return NumElements * getElementType()->getPrimitiveSizeInBits();
400 // Methods for support type inquiry through isa, cast, and dyn_cast.
401 static inline bool classof(const VectorType *) { return true; }
402 static inline bool classof(const Type *T) {
403 return T->getTypeID() == VectorTyID;
408 /// PointerType - Class to represent pointers.
410 class PointerType : public SequentialType {
411 PointerType(const PointerType &); // Do not implement
412 const PointerType &operator=(const PointerType &); // Do not implement
413 explicit PointerType(Type *ElType, unsigned AddrSpace);
415 /// PointerType::get - This constructs a pointer to an object of the specified
416 /// type in a numbered address space.
417 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
419 /// PointerType::getUnqual - This constructs a pointer to an object of the
420 /// specified type in the generic address space (address space zero).
421 static PointerType *getUnqual(const Type *ElementType) {
422 return PointerType::get(ElementType, 0);
425 /// isValidElementType - Return true if the specified type is valid as a
427 static bool isValidElementType(const Type *ElemTy);
429 /// @brief Return the address space of the Pointer type.
430 inline unsigned getAddressSpace() const { return getSubclassData(); }
432 // Implement support type inquiry through isa, cast, and dyn_cast.
433 static inline bool classof(const PointerType *) { return true; }
434 static inline bool classof(const Type *T) {
435 return T->getTypeID() == PointerTyID;
439 } // End llvm namespace