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;
35 class DerivedType : public Type {
39 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 *T) { 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 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 /// sbyte/ubyte, 0xFFFF for shorts, 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 *T) { 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 {
141 /// Function parameters can have attributes to indicate how they should be
142 /// treated by optimizations and code generation. This enumeration lists the
143 /// set of possible attributes.
144 /// @brief Function parameter attributes enumeration.
145 enum ParameterAttributes {
146 NoAttributeSet = 0, ///< No attribute value has been set
147 ZExtAttribute = 1, ///< zero extended before/after call
148 SExtAttribute = 1 << 1, ///< sign extended before/after call
149 NoReturnAttribute = 1 << 2, ///< mark the function as not returning
150 InRegAttribute = 1 << 3, ///< force argument to be passed in register
151 StructRetAttribute= 1 << 4 ///< hidden pointer to structure to return
153 typedef std::vector<ParameterAttributes> ParamAttrsList;
155 friend class TypeMap<FunctionValType, FunctionType>;
157 ParamAttrsList *ParamAttrs;
159 FunctionType(const FunctionType &); // Do not implement
160 const FunctionType &operator=(const FunctionType &); // Do not implement
161 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
162 bool IsVarArgs, const ParamAttrsList &Attrs);
165 /// FunctionType::get - This static method is the primary way of constructing
168 static FunctionType *get(
169 const Type *Result, ///< The result type
170 const std::vector<const Type*> &Params, ///< The types of the parameters
171 bool isVarArg, ///< Whether this is a variable argument length function
172 const ParamAttrsList & Attrs = ParamAttrsList()
173 ///< Indicates the parameter attributes to use, if any. The 0th entry
174 ///< in the list refers to the return type. Parameters are numbered
178 inline bool isVarArg() const { return isVarArgs; }
179 inline const Type *getReturnType() const { return ContainedTys[0]; }
181 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
182 param_iterator param_begin() const { return ContainedTys.begin()+1; }
183 param_iterator param_end() const { return ContainedTys.end(); }
185 // Parameter type accessors...
186 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
188 /// getNumParams - Return the number of fixed parameters this function type
189 /// requires. This does not consider varargs.
191 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
193 bool isStructReturn() const {
194 return (getNumParams() && paramHasAttr(1, StructRetAttribute));
197 /// The parameter attributes for the \p ith parameter are returned. The 0th
198 /// parameter refers to the return type of the function.
199 /// @returns The ParameterAttributes for the \p ith parameter.
200 /// @brief Get the attributes for a parameter
201 ParameterAttributes getParamAttrs(unsigned i) const;
203 /// @brief Determine if a parameter attribute is set
204 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
205 return getParamAttrs(i) & attr;
208 /// @brief Return the number of parameter attributes this type has.
209 unsigned getNumAttrs() const {
210 return (ParamAttrs ? unsigned(ParamAttrs->size()) : 0);
213 /// @brief Convert a ParameterAttribute into its assembly text
214 static std::string getParamAttrsText(ParameterAttributes Attr);
216 // Implement the AbstractTypeUser interface.
217 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
218 virtual void typeBecameConcrete(const DerivedType *AbsTy);
220 // Methods for support type inquiry through isa, cast, and dyn_cast:
221 static inline bool classof(const FunctionType *T) { return true; }
222 static inline bool classof(const Type *T) {
223 return T->getTypeID() == FunctionTyID;
228 /// CompositeType - Common super class of ArrayType, StructType, PointerType
230 class CompositeType : public DerivedType {
232 inline CompositeType(TypeID id) : DerivedType(id) { }
235 /// getTypeAtIndex - Given an index value into the type, return the type of
238 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
239 virtual bool indexValid(const Value *V) const = 0;
241 // Methods for support type inquiry through isa, cast, and dyn_cast:
242 static inline bool classof(const CompositeType *T) { return true; }
243 static inline bool classof(const Type *T) {
244 return T->getTypeID() == ArrayTyID ||
245 T->getTypeID() == StructTyID ||
246 T->getTypeID() == PointerTyID ||
247 T->getTypeID() == VectorTyID;
252 /// StructType - Class to represent struct types
254 class StructType : public CompositeType {
255 friend class TypeMap<StructValType, StructType>;
256 StructType(const StructType &); // Do not implement
257 const StructType &operator=(const StructType &); // Do not implement
258 StructType(const std::vector<const Type*> &Types, bool isPacked);
260 /// StructType::get - This static method is the primary way to create a
263 static StructType *get(const std::vector<const Type*> &Params,
264 bool isPacked=false);
266 // Iterator access to the elements
267 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
268 element_iterator element_begin() const { return ContainedTys.begin(); }
269 element_iterator element_end() const { return ContainedTys.end(); }
271 // Random access to the elements
272 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
273 const Type *getElementType(unsigned N) const {
274 assert(N < ContainedTys.size() && "Element number out of range!");
275 return ContainedTys[N];
278 /// getTypeAtIndex - Given an index value into the type, return the type of
279 /// the element. For a structure type, this must be a constant value...
281 virtual const Type *getTypeAtIndex(const Value *V) const ;
282 virtual bool indexValid(const Value *V) const;
284 // Implement the AbstractTypeUser interface.
285 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
286 virtual void typeBecameConcrete(const DerivedType *AbsTy);
288 // Methods for support type inquiry through isa, cast, and dyn_cast:
289 static inline bool classof(const StructType *T) { return true; }
290 static inline bool classof(const Type *T) {
291 return T->getTypeID() == StructTyID;
294 bool isPacked() const { return getSubclassData(); }
298 /// SequentialType - This is the superclass of the array, pointer and packed
299 /// type classes. All of these represent "arrays" in memory. The array type
300 /// represents a specifically sized array, pointer types are unsized/unknown
301 /// size arrays, vector types represent specifically sized arrays that
302 /// allow for use of SIMD instructions. SequentialType holds the common
303 /// features of all, which stem from the fact that all three lay their
304 /// components out in memory identically.
306 class SequentialType : public CompositeType {
307 SequentialType(const SequentialType &); // Do not implement!
308 const SequentialType &operator=(const SequentialType &); // Do not implement!
310 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
311 ContainedTys.reserve(1);
312 ContainedTys.push_back(PATypeHandle(ElType, this));
316 inline const Type *getElementType() const { return ContainedTys[0]; }
318 virtual bool indexValid(const Value *V) const;
320 /// getTypeAtIndex - Given an index value into the type, return the type of
321 /// the element. For sequential types, there is only one subtype...
323 virtual const Type *getTypeAtIndex(const Value *V) const {
324 return ContainedTys[0];
327 // Methods for support type inquiry through isa, cast, and dyn_cast:
328 static inline bool classof(const SequentialType *T) { return true; }
329 static inline bool classof(const Type *T) {
330 return T->getTypeID() == ArrayTyID ||
331 T->getTypeID() == PointerTyID ||
332 T->getTypeID() == VectorTyID;
337 /// ArrayType - Class to represent array types
339 class ArrayType : public SequentialType {
340 friend class TypeMap<ArrayValType, ArrayType>;
341 uint64_t NumElements;
343 ArrayType(const ArrayType &); // Do not implement
344 const ArrayType &operator=(const ArrayType &); // Do not implement
345 ArrayType(const Type *ElType, uint64_t NumEl);
347 /// ArrayType::get - This static method is the primary way to construct an
350 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
352 inline uint64_t getNumElements() const { return NumElements; }
354 // Implement the AbstractTypeUser interface.
355 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
356 virtual void typeBecameConcrete(const DerivedType *AbsTy);
358 // Methods for support type inquiry through isa, cast, and dyn_cast:
359 static inline bool classof(const ArrayType *T) { return true; }
360 static inline bool classof(const Type *T) {
361 return T->getTypeID() == ArrayTyID;
365 /// VectorType - Class to represent vector types
367 class VectorType : public SequentialType {
368 friend class TypeMap<VectorValType, VectorType>;
369 unsigned NumElements;
371 VectorType(const VectorType &); // Do not implement
372 const VectorType &operator=(const VectorType &); // Do not implement
373 VectorType(const Type *ElType, unsigned NumEl);
375 /// VectorType::get - This static method is the primary way to construct an
378 static VectorType *get(const Type *ElementType, unsigned NumElements);
380 /// @brief Return the number of elements in the Vector type.
381 inline unsigned getNumElements() const { return NumElements; }
383 /// @brief Return the number of bits in the Vector type.
384 inline unsigned getBitWidth() const {
385 return NumElements *getElementType()->getPrimitiveSizeInBits();
388 // Implement the AbstractTypeUser interface.
389 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
390 virtual void typeBecameConcrete(const DerivedType *AbsTy);
392 // Methods for support type inquiry through isa, cast, and dyn_cast:
393 static inline bool classof(const VectorType *T) { return true; }
394 static inline bool classof(const Type *T) {
395 return T->getTypeID() == VectorTyID;
400 /// PointerType - Class to represent pointers
402 class PointerType : public SequentialType {
403 friend class TypeMap<PointerValType, PointerType>;
404 PointerType(const PointerType &); // Do not implement
405 const PointerType &operator=(const PointerType &); // Do not implement
406 PointerType(const Type *ElType);
408 /// PointerType::get - This is the only way to construct a new pointer type.
409 static PointerType *get(const Type *ElementType);
411 // Implement the AbstractTypeUser interface.
412 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
413 virtual void typeBecameConcrete(const DerivedType *AbsTy);
415 // Implement support type inquiry through isa, cast, and dyn_cast:
416 static inline bool classof(const PointerType *T) { return true; }
417 static inline bool classof(const Type *T) {
418 return T->getTypeID() == PointerTyID;
423 /// OpaqueType - Class to represent abstract types
425 class OpaqueType : public DerivedType {
426 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
427 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
430 /// OpaqueType::get - Static factory method for the OpaqueType class...
432 static OpaqueType *get() {
433 return new OpaqueType(); // All opaque types are distinct
436 // Implement the AbstractTypeUser interface.
437 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
438 abort(); // FIXME: this is not really an AbstractTypeUser!
440 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
441 abort(); // FIXME: this is not really an AbstractTypeUser!
444 // Implement support for type inquiry through isa, cast, and dyn_cast:
445 static inline bool classof(const OpaqueType *T) { return true; }
446 static inline bool classof(const Type *T) {
447 return T->getTypeID() == OpaqueTyID;
451 } // End llvm namespace