1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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(TypeID id) : Type(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();
56 //===--------------------------------------------------------------------===//
57 // Abstract Type handling methods - These types have special lifetimes, which
58 // are managed by (add|remove)AbstractTypeUser. See comments in
59 // AbstractTypeUser.h for more information.
61 /// refineAbstractTypeTo - This function is used to when it is discovered that
62 /// the 'this' abstract type is actually equivalent to the NewType specified.
63 /// This causes all users of 'this' to switch to reference the more concrete
64 /// type NewType and for 'this' to be deleted.
66 void refineAbstractTypeTo(const Type *NewType);
68 void dump() const { Type::dump(); }
70 // Methods for support type inquiry through isa, cast, and dyn_cast:
71 static inline bool classof(const DerivedType *T) { return true; }
72 static inline bool classof(const Type *T) {
73 return T->isDerivedType();
77 /// Class to represent integer types. Note that this class is also used to
78 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
80 /// @brief Integer representation type
81 class IntegerType : public DerivedType {
83 explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
84 setSubclassData(NumBits);
86 friend class TypeMap<IntegerValType, IntegerType>;
88 /// This enum is just used to hold constants we need for IntegerType.
90 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
91 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
92 ///< Note that bit width is stored in the Type classes SubclassData field
93 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
96 /// This static method is the primary way of constructing an IntegerType.
97 /// If an IntegerType with the same NumBits value was previously instantiated,
98 /// that instance will be returned. Otherwise a new one will be created. Only
99 /// one instance with a given NumBits value is ever created.
100 /// @brief Get or create an IntegerType instance.
101 static const IntegerType* get(unsigned NumBits);
103 /// @brief Get the number of bits in this IntegerType
104 unsigned getBitWidth() const { return getSubclassData(); }
106 /// getBitMask - Return a bitmask with ones set for all of the bits
107 /// that can be set by an unsigned version of this type. This is 0xFF for
108 /// sbyte/ubyte, 0xFFFF for shorts, etc.
109 uint64_t getBitMask() const {
110 return ~uint64_t(0UL) >> (64-getBitWidth());
113 /// getSignBit - Return a uint64_t with just the most significant bit set (the
114 /// sign bit, if the value is treated as a signed number).
115 uint64_t getSignBit() const {
116 return 1ULL << (getBitWidth()-1);
119 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
120 /// @returns a bit mask with ones set for all the bits of this type.
121 /// @brief Get a bit mask for this type.
122 APInt getMask() const;
124 /// This method determines if the width of this IntegerType is a power-of-2
125 /// in terms of 8 bit bytes.
126 /// @returns true if this is a power-of-2 byte width.
127 /// @brief Is this a power-of-2 byte-width IntegerType ?
128 bool isPowerOf2ByteWidth() const;
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const IntegerType *T) { return true; }
132 static inline bool classof(const Type *T) {
133 return T->getTypeID() == IntegerTyID;
138 /// FunctionType - Class to represent function types
140 class FunctionType : public DerivedType {
141 friend class TypeMap<FunctionValType, FunctionType>;
143 ParamAttrsList *ParamAttrs;
145 FunctionType(const FunctionType &); // Do not implement
146 const FunctionType &operator=(const FunctionType &); // Do not implement
147 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
148 bool IsVarArgs, ParamAttrsList *Attrs = 0);
151 virtual ~FunctionType();
152 /// FunctionType::get - This static method is the primary way of constructing
155 static FunctionType *get(
156 const Type *Result, ///< The result type
157 const std::vector<const Type*> &Params, ///< The types of the parameters
158 bool isVarArg, ///< Whether this is a variable argument length function
159 ParamAttrsList *Attrs = 0
160 ///< Indicates the parameter attributes to use, if any. The 0th entry
161 ///< in the list refers to the return type. Parameters are numbered
162 ///< starting at 1. This argument must be on the heap and FunctionType
163 ///< owns it after its passed here.
166 inline bool isVarArg() const { return isVarArgs; }
167 inline const Type *getReturnType() const { return ContainedTys[0]; }
169 typedef Type::subtype_iterator param_iterator;
170 param_iterator param_begin() const { return ContainedTys + 1; }
171 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
173 // Parameter type accessors...
174 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
176 /// getNumParams - Return the number of fixed parameters this function type
177 /// requires. This does not consider varargs.
179 unsigned getNumParams() const { return NumContainedTys - 1; }
181 bool isStructReturn() const;
183 /// The parameter attributes for the \p ith parameter are returned. The 0th
184 /// parameter refers to the return type of the function.
185 /// @returns The ParameterAttributes for the \p ith parameter.
186 /// @brief Get the attributes for a parameter
187 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
189 // Implement the AbstractTypeUser interface.
190 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
191 virtual void typeBecameConcrete(const DerivedType *AbsTy);
193 // Methods for support type inquiry through isa, cast, and dyn_cast:
194 static inline bool classof(const FunctionType *T) { return true; }
195 static inline bool classof(const Type *T) {
196 return T->getTypeID() == FunctionTyID;
201 /// CompositeType - Common super class of ArrayType, StructType, PointerType
203 class CompositeType : public DerivedType {
205 inline explicit CompositeType(TypeID id) : DerivedType(id) { }
208 /// getTypeAtIndex - Given an index value into the type, return the type of
211 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
212 virtual bool indexValid(const Value *V) const = 0;
214 // Methods for support type inquiry through isa, cast, and dyn_cast:
215 static inline bool classof(const CompositeType *T) { return true; }
216 static inline bool classof(const Type *T) {
217 return T->getTypeID() == ArrayTyID ||
218 T->getTypeID() == StructTyID ||
219 T->getTypeID() == PointerTyID ||
220 T->getTypeID() == VectorTyID;
225 /// StructType - Class to represent struct types
227 class StructType : public CompositeType {
228 friend class TypeMap<StructValType, StructType>;
229 StructType(const StructType &); // Do not implement
230 const StructType &operator=(const StructType &); // Do not implement
231 StructType(const std::vector<const Type*> &Types, bool isPacked);
233 /// StructType::get - This static method is the primary way to create a
236 static StructType *get(const std::vector<const Type*> &Params,
237 bool isPacked=false);
239 // Iterator access to the elements
240 typedef Type::subtype_iterator element_iterator;
241 element_iterator element_begin() const { return ContainedTys; }
242 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
244 // Random access to the elements
245 unsigned getNumElements() const { return NumContainedTys; }
246 const Type *getElementType(unsigned N) const {
247 assert(N < NumContainedTys && "Element number out of range!");
248 return ContainedTys[N];
251 /// getTypeAtIndex - Given an index value into the type, return the type of
252 /// the element. For a structure type, this must be a constant value...
254 virtual const Type *getTypeAtIndex(const Value *V) const ;
255 virtual bool indexValid(const Value *V) const;
257 // Implement the AbstractTypeUser interface.
258 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
259 virtual void typeBecameConcrete(const DerivedType *AbsTy);
261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const StructType *T) { return true; }
263 static inline bool classof(const Type *T) {
264 return T->getTypeID() == StructTyID;
267 bool isPacked() const { return getSubclassData(); }
271 /// SequentialType - This is the superclass of the array, pointer and packed
272 /// type classes. All of these represent "arrays" in memory. The array type
273 /// represents a specifically sized array, pointer types are unsized/unknown
274 /// size arrays, vector types represent specifically sized arrays that
275 /// allow for use of SIMD instructions. SequentialType holds the common
276 /// features of all, which stem from the fact that all three lay their
277 /// components out in memory identically.
279 class SequentialType : public CompositeType {
280 PATypeHandle ContainedType; ///< Storage for the single contained type
281 SequentialType(const SequentialType &); // Do not implement!
282 const SequentialType &operator=(const SequentialType &); // Do not implement!
284 SequentialType(TypeID TID, const Type *ElType)
285 : CompositeType(TID), ContainedType(ElType, this) {
286 ContainedTys = &ContainedType;
291 inline const Type *getElementType() const { return ContainedTys[0]; }
293 virtual bool indexValid(const Value *V) const;
295 /// getTypeAtIndex - Given an index value into the type, return the type of
296 /// the element. For sequential types, there is only one subtype...
298 virtual const Type *getTypeAtIndex(const Value *V) const {
299 return ContainedTys[0];
302 // Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const SequentialType *T) { return true; }
304 static inline bool classof(const Type *T) {
305 return T->getTypeID() == ArrayTyID ||
306 T->getTypeID() == PointerTyID ||
307 T->getTypeID() == VectorTyID;
312 /// ArrayType - Class to represent array types
314 class ArrayType : public SequentialType {
315 friend class TypeMap<ArrayValType, ArrayType>;
316 uint64_t NumElements;
318 ArrayType(const ArrayType &); // Do not implement
319 const ArrayType &operator=(const ArrayType &); // Do not implement
320 ArrayType(const Type *ElType, uint64_t NumEl);
322 /// ArrayType::get - This static method is the primary way to construct an
325 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
327 inline uint64_t getNumElements() const { return NumElements; }
329 // Implement the AbstractTypeUser interface.
330 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
331 virtual void typeBecameConcrete(const DerivedType *AbsTy);
333 // Methods for support type inquiry through isa, cast, and dyn_cast:
334 static inline bool classof(const ArrayType *T) { return true; }
335 static inline bool classof(const Type *T) {
336 return T->getTypeID() == ArrayTyID;
340 /// VectorType - Class to represent vector types
342 class VectorType : public SequentialType {
343 friend class TypeMap<VectorValType, VectorType>;
344 unsigned NumElements;
346 VectorType(const VectorType &); // Do not implement
347 const VectorType &operator=(const VectorType &); // Do not implement
348 VectorType(const Type *ElType, unsigned NumEl);
350 /// VectorType::get - This static method is the primary way to construct an
353 static VectorType *get(const Type *ElementType, unsigned NumElements);
355 /// @brief Return the number of elements in the Vector type.
356 inline unsigned getNumElements() const { return NumElements; }
358 /// @brief Return the number of bits in the Vector type.
359 inline unsigned getBitWidth() const {
360 return NumElements *getElementType()->getPrimitiveSizeInBits();
363 // Implement the AbstractTypeUser interface.
364 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
365 virtual void typeBecameConcrete(const DerivedType *AbsTy);
367 // Methods for support type inquiry through isa, cast, and dyn_cast:
368 static inline bool classof(const VectorType *T) { return true; }
369 static inline bool classof(const Type *T) {
370 return T->getTypeID() == VectorTyID;
375 /// PointerType - Class to represent pointers
377 class PointerType : public SequentialType {
378 friend class TypeMap<PointerValType, PointerType>;
379 PointerType(const PointerType &); // Do not implement
380 const PointerType &operator=(const PointerType &); // Do not implement
381 explicit PointerType(const Type *ElType);
383 /// PointerType::get - This is the only way to construct a new pointer type.
384 static PointerType *get(const Type *ElementType);
386 // Implement the AbstractTypeUser interface.
387 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
388 virtual void typeBecameConcrete(const DerivedType *AbsTy);
390 // Implement support type inquiry through isa, cast, and dyn_cast:
391 static inline bool classof(const PointerType *T) { return true; }
392 static inline bool classof(const Type *T) {
393 return T->getTypeID() == PointerTyID;
398 /// OpaqueType - Class to represent abstract types
400 class OpaqueType : public DerivedType {
401 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
402 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
405 /// OpaqueType::get - Static factory method for the OpaqueType class...
407 static OpaqueType *get() {
408 return new OpaqueType(); // All opaque types are distinct
411 // Implement the AbstractTypeUser interface.
412 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
413 abort(); // FIXME: this is not really an AbstractTypeUser!
415 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
416 abort(); // FIXME: this is not really an AbstractTypeUser!
419 // Implement support for type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const OpaqueType *T) { return true; }
421 static inline bool classof(const Type *T) {
422 return T->getTypeID() == OpaqueTyID;
426 } // End llvm namespace