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(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
102 /// FunctionType::get - This static method is the primary way of constructing
105 static FunctionType *get(Type *Result,
106 ArrayRef<Type*> Params, bool isVarArg);
108 /// FunctionType::get - Create a FunctionType taking no parameters.
110 static FunctionType *get(Type *Result, bool isVarArg);
112 /// isValidReturnType - Return true if the specified type is valid as a return
114 static bool isValidReturnType(Type *RetTy);
116 /// isValidArgumentType - Return true if the specified type is valid as an
118 static bool isValidArgumentType(Type *ArgTy);
120 bool isVarArg() const { return getSubclassData(); }
121 Type *getReturnType() const { return ContainedTys[0]; }
123 typedef Type::subtype_iterator param_iterator;
124 param_iterator param_begin() const { return ContainedTys + 1; }
125 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
127 // Parameter type accessors.
128 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
130 /// getNumParams - Return the number of fixed parameters this function type
131 /// requires. This does not consider varargs.
133 unsigned getNumParams() const { return NumContainedTys - 1; }
135 // Methods for support type inquiry through isa, cast, and dyn_cast.
136 static inline bool classof(const FunctionType *) { return true; }
137 static inline bool classof(const Type *T) {
138 return T->getTypeID() == FunctionTyID;
143 /// CompositeType - Common super class of ArrayType, StructType, PointerType
145 class CompositeType : public Type {
147 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
150 /// getTypeAtIndex - Given an index value into the type, return the type of
153 Type *getTypeAtIndex(const Value *V);
154 Type *getTypeAtIndex(unsigned Idx);
155 bool indexValid(const Value *V) const;
156 bool indexValid(unsigned Idx) const;
158 // Methods for support type inquiry through isa, cast, and dyn_cast.
159 static inline bool classof(const CompositeType *) { return true; }
160 static inline bool classof(const Type *T) {
161 return T->getTypeID() == ArrayTyID ||
162 T->getTypeID() == StructTyID ||
163 T->getTypeID() == PointerTyID ||
164 T->getTypeID() == VectorTyID;
169 /// StructType - Class to represent struct types. There are two different kinds
170 /// of struct types: Literal structs and Identified structs.
172 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
173 /// always have a body when created. You can get one of these by using one of
174 /// the StructType::get() forms.
176 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
177 /// uniqued. The names for identified structs are managed at the LLVMContext
178 /// level, so there can only be a single identified struct with a given name in
179 /// a particular LLVMContext. Identified structs may also optionally be opaque
180 /// (have no body specified). You get one of these by using one of the
181 /// StructType::create() forms.
183 /// Independent of what kind of struct you have, the body of a struct type are
184 /// laid out in memory consequtively with the elements directly one after the
185 /// other (if the struct is packed) or (if not packed) with padding between the
186 /// elements as defined by TargetData (which is required to match what the code
187 /// generator for a target expects).
189 class StructType : public CompositeType {
190 StructType(const StructType &); // Do not implement
191 const StructType &operator=(const StructType &); // Do not implement
192 StructType(LLVMContext &C)
193 : CompositeType(C, StructTyID), SymbolTableEntry(0) {}
195 // This is the contents of the SubClassData field.
201 /// SymbolTableEntry - For a named struct that actually has a name, this is a
202 /// pointer to the symbol table entry (maintained by LLVMContext) for the
203 /// struct. This is null if the type is an literal struct or if it is
204 /// a identified type that has an empty name.
206 void *SymbolTableEntry;
209 delete [] ContainedTys; // Delete the body.
212 /// StructType::create - This creates an identified struct.
213 static StructType *create(LLVMContext &Context, StringRef Name);
214 static StructType *create(LLVMContext &Context);
216 static StructType *create(ArrayRef<Type*> Elements,
218 bool isPacked = false);
219 static StructType *create(ArrayRef<Type*> Elements);
220 static StructType *create(LLVMContext &Context,
221 ArrayRef<Type*> Elements,
223 bool isPacked = false);
224 static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
225 static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL;
227 /// StructType::get - This static method is the primary way to create a
228 /// literal StructType.
229 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
230 bool isPacked = false);
232 /// StructType::get - Create an empty structure type.
234 static StructType *get(LLVMContext &Context, bool isPacked = false);
236 /// StructType::get - This static method is a convenience method for creating
237 /// structure types by specifying the elements as arguments. Note that this
238 /// method always returns a non-packed struct, and requires at least one
240 static StructType *get(Type *elt1, ...) END_WITH_NULL;
242 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
244 /// isLiteral - Return true if this type is uniqued by structural
245 /// equivalence, false if it is a struct definition.
246 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
248 /// isOpaque - Return true if this is a type with an identity that has no body
249 /// specified yet. These prints as 'opaque' in .ll files.
250 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
252 /// hasName - Return true if this is a named struct that has a non-empty name.
253 bool hasName() const { return SymbolTableEntry != 0; }
255 /// getName - Return the name for this struct type if it has an identity.
256 /// This may return an empty string for an unnamed struct type. Do not call
257 /// this on an literal type.
258 StringRef getName() const;
260 /// setName - Change the name of this type to the specified name, or to a name
261 /// with a suffix if there is a collision. Do not call this on an literal
263 void setName(StringRef Name);
265 /// setBody - Specify a body for an opaque identified type.
266 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
267 void setBody(Type *elt1, ...) END_WITH_NULL;
269 /// isValidElementType - Return true if the specified type is valid as a
271 static bool isValidElementType(Type *ElemTy);
274 // Iterator access to the elements.
275 typedef Type::subtype_iterator element_iterator;
276 element_iterator element_begin() const { return ContainedTys; }
277 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
279 /// isLayoutIdentical - Return true if this is layout identical to the
280 /// specified struct.
281 bool isLayoutIdentical(StructType *Other) const;
283 // Random access to the elements
284 unsigned getNumElements() const { return NumContainedTys; }
285 Type *getElementType(unsigned N) const {
286 assert(N < NumContainedTys && "Element number out of range!");
287 return ContainedTys[N];
290 // Methods for support type inquiry through isa, cast, and dyn_cast.
291 static inline bool classof(const StructType *) { return true; }
292 static inline bool classof(const Type *T) {
293 return T->getTypeID() == StructTyID;
297 /// SequentialType - This is the superclass of the array, pointer and vector
298 /// type classes. All of these represent "arrays" in memory. The array type
299 /// represents a specifically sized array, pointer types are unsized/unknown
300 /// size arrays, vector types represent specifically sized arrays that
301 /// allow for use of SIMD instructions. SequentialType holds the common
302 /// features of all, which stem from the fact that all three lay their
303 /// components out in memory identically.
305 class SequentialType : public CompositeType {
306 Type *ContainedType; ///< Storage for the single contained type.
307 SequentialType(const SequentialType &); // Do not implement!
308 const SequentialType &operator=(const SequentialType &); // Do not implement!
311 SequentialType(TypeID TID, Type *ElType)
312 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
313 ContainedTys = &ContainedType;
318 Type *getElementType() const { return ContainedTys[0]; }
320 // Methods for support type inquiry through isa, cast, and dyn_cast.
321 static inline bool classof(const SequentialType *) { return true; }
322 static inline bool classof(const Type *T) {
323 return T->getTypeID() == ArrayTyID ||
324 T->getTypeID() == PointerTyID ||
325 T->getTypeID() == VectorTyID;
330 /// ArrayType - Class to represent array types.
332 class ArrayType : public SequentialType {
333 uint64_t NumElements;
335 ArrayType(const ArrayType &); // Do not implement
336 const ArrayType &operator=(const ArrayType &); // Do not implement
337 ArrayType(Type *ElType, uint64_t NumEl);
339 /// ArrayType::get - This static method is the primary way to construct an
342 static ArrayType *get(Type *ElementType, uint64_t NumElements);
344 /// isValidElementType - Return true if the specified type is valid as a
346 static bool isValidElementType(Type *ElemTy);
348 uint64_t getNumElements() const { return NumElements; }
350 // Methods for support type inquiry through isa, cast, and dyn_cast.
351 static inline bool classof(const ArrayType *) { return true; }
352 static inline bool classof(const Type *T) {
353 return T->getTypeID() == ArrayTyID;
357 /// VectorType - Class to represent vector types.
359 class VectorType : public SequentialType {
360 unsigned NumElements;
362 VectorType(const VectorType &); // Do not implement
363 const VectorType &operator=(const VectorType &); // Do not implement
364 VectorType(Type *ElType, unsigned NumEl);
366 /// VectorType::get - This static method is the primary way to construct an
369 static VectorType *get(Type *ElementType, unsigned NumElements);
371 /// VectorType::getInteger - This static method gets a VectorType with the
372 /// same number of elements as the input type, and the element type is an
373 /// integer type of the same width as the input element type.
375 static VectorType *getInteger(VectorType *VTy) {
376 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
377 assert(EltBits && "Element size must be of a non-zero size");
378 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
379 return VectorType::get(EltTy, VTy->getNumElements());
382 /// VectorType::getExtendedElementVectorType - This static method is like
383 /// getInteger except that the element types are twice as wide as the
384 /// elements in the input type.
386 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
387 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
388 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
389 return VectorType::get(EltTy, VTy->getNumElements());
392 /// VectorType::getTruncatedElementVectorType - This static method is like
393 /// getInteger except that the element types are half as wide as the
394 /// elements in the input type.
396 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
397 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
398 assert((EltBits & 1) == 0 &&
399 "Cannot truncate vector element with odd bit-width");
400 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
401 return VectorType::get(EltTy, VTy->getNumElements());
404 /// isValidElementType - Return true if the specified type is valid as a
406 static bool isValidElementType(Type *ElemTy);
408 /// @brief Return the number of elements in the Vector type.
409 unsigned getNumElements() const { return NumElements; }
411 /// @brief Return the number of bits in the Vector type.
412 /// Returns zero when the vector is a vector of pointers.
413 unsigned getBitWidth() const {
414 return NumElements * getElementType()->getPrimitiveSizeInBits();
417 // Methods for support type inquiry through isa, cast, and dyn_cast.
418 static inline bool classof(const VectorType *) { return true; }
419 static inline bool classof(const Type *T) {
420 return T->getTypeID() == VectorTyID;
425 /// PointerType - Class to represent pointers.
427 class PointerType : public SequentialType {
428 PointerType(const PointerType &); // Do not implement
429 const PointerType &operator=(const PointerType &); // Do not implement
430 explicit PointerType(Type *ElType, unsigned AddrSpace);
432 /// PointerType::get - This constructs a pointer to an object of the specified
433 /// type in a numbered address space.
434 static PointerType *get(Type *ElementType, unsigned AddressSpace);
436 /// PointerType::getUnqual - This constructs a pointer to an object of the
437 /// specified type in the generic address space (address space zero).
438 static PointerType *getUnqual(Type *ElementType) {
439 return PointerType::get(ElementType, 0);
442 /// isValidElementType - Return true if the specified type is valid as a
444 static bool isValidElementType(Type *ElemTy);
446 /// @brief Return the address space of the Pointer type.
447 inline unsigned getAddressSpace() const { return getSubclassData(); }
449 // Implement support type inquiry through isa, cast, and dyn_cast.
450 static inline bool classof(const PointerType *) { return true; }
451 static inline bool classof(const Type *T) {
452 return T->getTypeID() == PointerTyID;
456 } // End llvm namespace