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 const 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, const ParamAttrsList *Attrs = 0);
151 /// FunctionType::get - This static method is the primary way of constructing
154 static FunctionType *get(
155 const Type *Result, ///< The result type
156 const std::vector<const Type*> &Params, ///< The types of the parameters
157 bool isVarArg, ///< Whether this is a variable argument length function
158 const ParamAttrsList *Attrs = 0
159 ///< Indicates the parameter attributes to use, if any. The 0th entry
160 ///< in the list refers to the return type. Parameters are numbered
161 ///< starting at 1. This argument must be on the heap and FunctionType
162 ///< owns it after its passed here.
165 inline bool isVarArg() const { return isVarArgs; }
166 inline const Type *getReturnType() const { return ContainedTys[0]; }
168 typedef Type::subtype_iterator param_iterator;
169 param_iterator param_begin() const { return ContainedTys + 1; }
170 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
172 // Parameter type accessors...
173 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
175 /// getNumParams - Return the number of fixed parameters this function type
176 /// requires. This does not consider varargs.
178 unsigned getNumParams() const { return NumContainedTys - 1; }
180 bool isStructReturn() const;
182 /// The parameter attributes for the \p ith parameter are returned. The 0th
183 /// parameter refers to the return type of the function.
184 /// @returns The ParameterAttributes for the \p ith parameter.
185 /// @brief Get the attributes for a parameter
186 const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
188 // Implement the AbstractTypeUser interface.
189 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
190 virtual void typeBecameConcrete(const DerivedType *AbsTy);
192 // Methods for support type inquiry through isa, cast, and dyn_cast:
193 static inline bool classof(const FunctionType *T) { return true; }
194 static inline bool classof(const Type *T) {
195 return T->getTypeID() == FunctionTyID;
200 /// CompositeType - Common super class of ArrayType, StructType, PointerType
202 class CompositeType : public DerivedType {
204 inline explicit CompositeType(TypeID id) : DerivedType(id) { }
207 /// getTypeAtIndex - Given an index value into the type, return the type of
210 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
211 virtual bool indexValid(const Value *V) const = 0;
213 // Methods for support type inquiry through isa, cast, and dyn_cast:
214 static inline bool classof(const CompositeType *T) { return true; }
215 static inline bool classof(const Type *T) {
216 return T->getTypeID() == ArrayTyID ||
217 T->getTypeID() == StructTyID ||
218 T->getTypeID() == PointerTyID ||
219 T->getTypeID() == VectorTyID;
224 /// StructType - Class to represent struct types
226 class StructType : public CompositeType {
227 friend class TypeMap<StructValType, StructType>;
228 StructType(const StructType &); // Do not implement
229 const StructType &operator=(const StructType &); // Do not implement
230 StructType(const std::vector<const Type*> &Types, bool isPacked);
232 /// StructType::get - This static method is the primary way to create a
235 static StructType *get(const std::vector<const Type*> &Params,
236 bool isPacked=false);
238 // Iterator access to the elements
239 typedef Type::subtype_iterator element_iterator;
240 element_iterator element_begin() const { return ContainedTys; }
241 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
243 // Random access to the elements
244 unsigned getNumElements() const { return NumContainedTys; }
245 const Type *getElementType(unsigned N) const {
246 assert(N < NumContainedTys && "Element number out of range!");
247 return ContainedTys[N];
250 /// getTypeAtIndex - Given an index value into the type, return the type of
251 /// the element. For a structure type, this must be a constant value...
253 virtual const Type *getTypeAtIndex(const Value *V) const ;
254 virtual bool indexValid(const Value *V) const;
256 // Implement the AbstractTypeUser interface.
257 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
258 virtual void typeBecameConcrete(const DerivedType *AbsTy);
260 // Methods for support type inquiry through isa, cast, and dyn_cast:
261 static inline bool classof(const StructType *T) { return true; }
262 static inline bool classof(const Type *T) {
263 return T->getTypeID() == StructTyID;
266 bool isPacked() const { return getSubclassData(); }
270 /// SequentialType - This is the superclass of the array, pointer and vector
271 /// type classes. All of these represent "arrays" in memory. The array type
272 /// represents a specifically sized array, pointer types are unsized/unknown
273 /// size arrays, vector types represent specifically sized arrays that
274 /// allow for use of SIMD instructions. SequentialType holds the common
275 /// features of all, which stem from the fact that all three lay their
276 /// components out in memory identically.
278 class SequentialType : public CompositeType {
279 PATypeHandle ContainedType; ///< Storage for the single contained type
280 SequentialType(const SequentialType &); // Do not implement!
281 const SequentialType &operator=(const SequentialType &); // Do not implement!
283 SequentialType(TypeID TID, const Type *ElType)
284 : CompositeType(TID), ContainedType(ElType, this) {
285 ContainedTys = &ContainedType;
290 inline const Type *getElementType() const { return ContainedTys[0]; }
292 virtual bool indexValid(const Value *V) const;
294 /// getTypeAtIndex - Given an index value into the type, return the type of
295 /// the element. For sequential types, there is only one subtype...
297 virtual const Type *getTypeAtIndex(const Value *V) const {
298 return ContainedTys[0];
301 // Methods for support type inquiry through isa, cast, and dyn_cast:
302 static inline bool classof(const SequentialType *T) { return true; }
303 static inline bool classof(const Type *T) {
304 return T->getTypeID() == ArrayTyID ||
305 T->getTypeID() == PointerTyID ||
306 T->getTypeID() == VectorTyID;
311 /// ArrayType - Class to represent array types
313 class ArrayType : public SequentialType {
314 friend class TypeMap<ArrayValType, ArrayType>;
315 uint64_t NumElements;
317 ArrayType(const ArrayType &); // Do not implement
318 const ArrayType &operator=(const ArrayType &); // Do not implement
319 ArrayType(const Type *ElType, uint64_t NumEl);
321 /// ArrayType::get - This static method is the primary way to construct an
324 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
326 inline uint64_t getNumElements() const { return NumElements; }
328 // Implement the AbstractTypeUser interface.
329 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
330 virtual void typeBecameConcrete(const DerivedType *AbsTy);
332 // Methods for support type inquiry through isa, cast, and dyn_cast:
333 static inline bool classof(const ArrayType *T) { return true; }
334 static inline bool classof(const Type *T) {
335 return T->getTypeID() == ArrayTyID;
339 /// VectorType - Class to represent vector types
341 class VectorType : public SequentialType {
342 friend class TypeMap<VectorValType, VectorType>;
343 unsigned NumElements;
345 VectorType(const VectorType &); // Do not implement
346 const VectorType &operator=(const VectorType &); // Do not implement
347 VectorType(const Type *ElType, unsigned NumEl);
349 /// VectorType::get - This static method is the primary way to construct an
352 static VectorType *get(const Type *ElementType, unsigned NumElements);
354 /// @brief Return the number of elements in the Vector type.
355 inline unsigned getNumElements() const { return NumElements; }
357 /// @brief Return the number of bits in the Vector type.
358 inline unsigned getBitWidth() const {
359 return NumElements *getElementType()->getPrimitiveSizeInBits();
362 // Implement the AbstractTypeUser interface.
363 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
364 virtual void typeBecameConcrete(const DerivedType *AbsTy);
366 // Methods for support type inquiry through isa, cast, and dyn_cast:
367 static inline bool classof(const VectorType *T) { return true; }
368 static inline bool classof(const Type *T) {
369 return T->getTypeID() == VectorTyID;
374 /// PointerType - Class to represent pointers
376 class PointerType : public SequentialType {
377 friend class TypeMap<PointerValType, PointerType>;
378 PointerType(const PointerType &); // Do not implement
379 const PointerType &operator=(const PointerType &); // Do not implement
380 explicit PointerType(const Type *ElType);
382 /// PointerType::get - This is the only way to construct a new pointer type.
383 static PointerType *get(const Type *ElementType);
385 // Implement the AbstractTypeUser interface.
386 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
387 virtual void typeBecameConcrete(const DerivedType *AbsTy);
389 // Implement support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const PointerType *T) { return true; }
391 static inline bool classof(const Type *T) {
392 return T->getTypeID() == PointerTyID;
397 /// OpaqueType - Class to represent abstract types
399 class OpaqueType : public DerivedType {
400 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
401 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
404 /// OpaqueType::get - Static factory method for the OpaqueType class...
406 static OpaqueType *get() {
407 return new OpaqueType(); // All opaque types are distinct
410 // Implement support for type inquiry through isa, cast, and dyn_cast:
411 static inline bool classof(const OpaqueType *T) { return true; }
412 static inline bool classof(const Type *T) {
413 return T->getTypeID() == OpaqueTyID;
417 } // End llvm namespace