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;
35 class DerivedType : public Type {
39 explicit DerivedType(TypeID id) : Type(id) {}
41 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
42 /// that the current type has transitioned from being abstract to being
45 void notifyUsesThatTypeBecameConcrete();
47 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
48 /// another (more concrete) type, we must eliminate all references to other
49 /// types, to avoid some circular reference problems.
51 void dropAllTypeUses();
55 //===--------------------------------------------------------------------===//
56 // Abstract Type handling methods - These types have special lifetimes, which
57 // are managed by (add|remove)AbstractTypeUser. See comments in
58 // AbstractTypeUser.h for more information.
60 /// refineAbstractTypeTo - This function is used to when it is discovered that
61 /// the 'this' abstract type is actually equivalent to the NewType specified.
62 /// This causes all users of 'this' to switch to reference the more concrete
63 /// type NewType and for 'this' to be deleted.
65 void refineAbstractTypeTo(const Type *NewType);
67 void dump() const { Type::dump(); }
69 // Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const DerivedType *) { return true; }
71 static inline bool classof(const Type *T) {
72 return T->isDerivedType();
76 /// Class to represent integer types. Note that this class is also used to
77 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
79 /// @brief Integer representation type
80 class IntegerType : public DerivedType {
82 explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
83 setSubclassData(NumBits);
85 friend class TypeMap<IntegerValType, IntegerType>;
87 /// This enum is just used to hold constants we need for IntegerType.
89 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
90 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
91 ///< Note that bit width is stored in the Type classes SubclassData field
92 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
95 /// This static method is the primary way of constructing an IntegerType.
96 /// If an IntegerType with the same NumBits value was previously instantiated,
97 /// that instance will be returned. Otherwise a new one will be created. Only
98 /// one instance with a given NumBits value is ever created.
99 /// @brief Get or create an IntegerType instance.
100 static const IntegerType* get(unsigned NumBits);
102 /// @brief Get the number of bits in this IntegerType
103 unsigned getBitWidth() const { return getSubclassData(); }
105 /// getBitMask - Return a bitmask with ones set for all of the bits
106 /// that can be set by an unsigned version of this type. This is 0xFF for
107 /// i8, 0xFFFF for i16, etc.
108 uint64_t getBitMask() const {
109 return ~uint64_t(0UL) >> (64-getBitWidth());
112 /// getSignBit - Return a uint64_t with just the most significant bit set (the
113 /// sign bit, if the value is treated as a signed number).
114 uint64_t getSignBit() const {
115 return 1ULL << (getBitWidth()-1);
118 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
119 /// @returns a bit mask with ones set for all the bits of this type.
120 /// @brief Get a bit mask for this type.
121 APInt getMask() const;
123 /// This method determines if the width of this IntegerType is a power-of-2
124 /// in terms of 8 bit bytes.
125 /// @returns true if this is a power-of-2 byte width.
126 /// @brief Is this a power-of-2 byte-width IntegerType ?
127 bool isPowerOf2ByteWidth() const;
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const IntegerType *) { return true; }
131 static inline bool classof(const Type *T) {
132 return T->getTypeID() == IntegerTyID;
137 /// FunctionType - Class to represent function types
139 class FunctionType : public DerivedType {
140 friend class TypeMap<FunctionValType, FunctionType>;
143 FunctionType(const FunctionType &); // Do not implement
144 const FunctionType &operator=(const FunctionType &); // Do not implement
145 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
149 /// FunctionType::get - This static method is the primary way of constructing
152 static FunctionType *get(
153 const Type *Result, ///< The result type
154 const std::vector<const Type*> &Params, ///< The types of the parameters
155 bool isVarArg ///< Whether this is a variable argument length function
158 /// isValidReturnType - Return true if the specified type is valid as a return
160 static bool isValidReturnType(const Type *RetTy);
162 inline bool isVarArg() const { return isVarArgs; }
163 inline const Type *getReturnType() const { return ContainedTys[0]; }
165 typedef Type::subtype_iterator param_iterator;
166 param_iterator param_begin() const { return ContainedTys + 1; }
167 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
169 // Parameter type accessors...
170 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
172 /// getNumParams - Return the number of fixed parameters this function type
173 /// requires. This does not consider varargs.
175 unsigned getNumParams() const { return NumContainedTys - 1; }
177 // Implement the AbstractTypeUser interface.
178 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
179 virtual void typeBecameConcrete(const DerivedType *AbsTy);
181 // Methods for support type inquiry through isa, cast, and dyn_cast:
182 static inline bool classof(const FunctionType *) { return true; }
183 static inline bool classof(const Type *T) {
184 return T->getTypeID() == FunctionTyID;
189 /// CompositeType - Common super class of ArrayType, StructType, PointerType
191 class CompositeType : public DerivedType {
193 inline explicit CompositeType(TypeID id) : DerivedType(id) { }
196 /// getTypeAtIndex - Given an index value into the type, return the type of
199 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
200 virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
201 virtual bool indexValid(const Value *V) const = 0;
202 virtual bool indexValid(unsigned Idx) const = 0;
204 // Methods for support type inquiry through isa, cast, and dyn_cast:
205 static inline bool classof(const CompositeType *) { return true; }
206 static inline bool classof(const Type *T) {
207 return T->getTypeID() == ArrayTyID ||
208 T->getTypeID() == StructTyID ||
209 T->getTypeID() == PointerTyID ||
210 T->getTypeID() == VectorTyID;
215 /// StructType - Class to represent struct types
217 class StructType : public CompositeType {
218 friend class TypeMap<StructValType, StructType>;
219 StructType(const StructType &); // Do not implement
220 const StructType &operator=(const StructType &); // Do not implement
221 StructType(const std::vector<const Type*> &Types, bool isPacked);
223 /// StructType::get - This static method is the primary way to create a
226 static StructType *get(const std::vector<const Type*> &Params,
227 bool isPacked=false);
229 /// StructType::get - This static method is a convenience method for
230 /// creating structure types by specifying the elements as arguments.
231 /// Note that this method always returns a non-packed struct. To get
232 /// an empty struct, pass NULL, NULL.
233 static StructType *get(const Type *type, ...) END_WITH_NULL;
235 // Iterator access to the elements
236 typedef Type::subtype_iterator element_iterator;
237 element_iterator element_begin() const { return ContainedTys; }
238 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
240 // Random access to the elements
241 unsigned getNumElements() const { return NumContainedTys; }
242 const Type *getElementType(unsigned N) const {
243 assert(N < NumContainedTys && "Element number out of range!");
244 return ContainedTys[N];
247 /// getTypeAtIndex - Given an index value into the type, return the type of
248 /// the element. For a structure type, this must be a constant value...
250 virtual const Type *getTypeAtIndex(const Value *V) const;
251 virtual const Type *getTypeAtIndex(unsigned Idx) const;
252 virtual bool indexValid(const Value *V) const;
253 virtual bool indexValid(unsigned Idx) const;
255 // Implement the AbstractTypeUser interface.
256 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
257 virtual void typeBecameConcrete(const DerivedType *AbsTy);
259 // Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const StructType *) { return true; }
261 static inline bool classof(const Type *T) {
262 return T->getTypeID() == StructTyID;
265 bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
269 /// SequentialType - This is the superclass of the array, pointer and vector
270 /// type classes. All of these represent "arrays" in memory. The array type
271 /// represents a specifically sized array, pointer types are unsized/unknown
272 /// size arrays, vector types represent specifically sized arrays that
273 /// allow for use of SIMD instructions. SequentialType holds the common
274 /// features of all, which stem from the fact that all three lay their
275 /// components out in memory identically.
277 class SequentialType : public CompositeType {
278 PATypeHandle ContainedType; ///< Storage for the single contained type
279 SequentialType(const SequentialType &); // Do not implement!
280 const SequentialType &operator=(const SequentialType &); // Do not implement!
282 // avoiding warning: 'this' : used in base member initializer list
283 SequentialType* this_() { return this; }
285 SequentialType(TypeID TID, const Type *ElType)
286 : CompositeType(TID), ContainedType(ElType, this_()) {
287 ContainedTys = &ContainedType;
292 inline const Type *getElementType() const { return ContainedTys[0]; }
294 virtual bool indexValid(const Value *V) const;
295 virtual bool indexValid(unsigned) const {
299 /// getTypeAtIndex - Given an index value into the type, return the type of
300 /// the element. For sequential types, there is only one subtype...
302 virtual const Type *getTypeAtIndex(const Value *) const {
303 return ContainedTys[0];
305 virtual const Type *getTypeAtIndex(unsigned) const {
306 return ContainedTys[0];
309 // Methods for support type inquiry through isa, cast, and dyn_cast:
310 static inline bool classof(const SequentialType *) { return true; }
311 static inline bool classof(const Type *T) {
312 return T->getTypeID() == ArrayTyID ||
313 T->getTypeID() == PointerTyID ||
314 T->getTypeID() == VectorTyID;
319 /// ArrayType - Class to represent array types
321 class ArrayType : public SequentialType {
322 friend class TypeMap<ArrayValType, ArrayType>;
323 uint64_t NumElements;
325 ArrayType(const ArrayType &); // Do not implement
326 const ArrayType &operator=(const ArrayType &); // Do not implement
327 ArrayType(const Type *ElType, uint64_t NumEl);
329 /// ArrayType::get - This static method is the primary way to construct an
332 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
334 inline uint64_t getNumElements() const { return NumElements; }
336 // Implement the AbstractTypeUser interface.
337 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
338 virtual void typeBecameConcrete(const DerivedType *AbsTy);
340 // Methods for support type inquiry through isa, cast, and dyn_cast:
341 static inline bool classof(const ArrayType *) { return true; }
342 static inline bool classof(const Type *T) {
343 return T->getTypeID() == ArrayTyID;
347 /// VectorType - Class to represent vector types
349 class VectorType : public SequentialType {
350 friend class TypeMap<VectorValType, VectorType>;
351 unsigned NumElements;
353 VectorType(const VectorType &); // Do not implement
354 const VectorType &operator=(const VectorType &); // Do not implement
355 VectorType(const Type *ElType, unsigned NumEl);
357 /// VectorType::get - This static method is the primary way to construct an
360 static VectorType *get(const Type *ElementType, unsigned NumElements);
362 /// VectorType::getInteger - This static method gets a VectorType with the
363 /// same number of elements as the input type, and the element type is an
364 /// integer type of the same width as the input element type.
366 static VectorType *getInteger(const VectorType *VTy) {
367 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
368 const Type *EltTy = IntegerType::get(EltBits);
369 return VectorType::get(EltTy, VTy->getNumElements());
372 /// @brief Return the number of elements in the Vector type.
373 inline unsigned getNumElements() const { return NumElements; }
375 /// @brief Return the number of bits in the Vector type.
376 inline unsigned getBitWidth() const {
377 return NumElements *getElementType()->getPrimitiveSizeInBits();
380 // Implement the AbstractTypeUser interface.
381 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
382 virtual void typeBecameConcrete(const DerivedType *AbsTy);
384 // Methods for support type inquiry through isa, cast, and dyn_cast:
385 static inline bool classof(const VectorType *) { return true; }
386 static inline bool classof(const Type *T) {
387 return T->getTypeID() == VectorTyID;
392 /// PointerType - Class to represent pointers
394 class PointerType : public SequentialType {
395 friend class TypeMap<PointerValType, PointerType>;
396 unsigned AddressSpace;
398 PointerType(const PointerType &); // Do not implement
399 const PointerType &operator=(const PointerType &); // Do not implement
400 explicit PointerType(const Type *ElType, unsigned AddrSpace);
402 /// PointerType::get - This constructs a pointer to an object of the specified
403 /// type in a numbered address space.
404 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
406 /// PointerType::getUnqual - This constructs a pointer to an object of the
407 /// specified type in the generic address space (address space zero).
408 static PointerType *getUnqual(const Type *ElementType) {
409 return PointerType::get(ElementType, 0);
412 /// @brief Return the address space of the Pointer type.
413 inline unsigned getAddressSpace() const { return AddressSpace; }
415 // Implement the AbstractTypeUser interface.
416 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
417 virtual void typeBecameConcrete(const DerivedType *AbsTy);
419 // Implement support type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const PointerType *) { return true; }
421 static inline bool classof(const Type *T) {
422 return T->getTypeID() == PointerTyID;
427 /// OpaqueType - Class to represent abstract types
429 class OpaqueType : public DerivedType {
430 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
431 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
434 /// OpaqueType::get - Static factory method for the OpaqueType class...
436 static OpaqueType *get() {
437 return new OpaqueType(); // All opaque types are distinct
440 // Implement support for type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const OpaqueType *) { return true; }
442 static inline bool classof(const Type *T) {
443 return T->getTypeID() == OpaqueTyID;
447 } // End llvm namespace