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;
34 class DerivedType : public Type {
38 DerivedType(TypeID id) : Type(id) {}
40 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
41 /// that the current type has transitioned from being abstract to being
44 void notifyUsesThatTypeBecameConcrete();
46 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
47 /// another (more concrete) type, we must eliminate all references to other
48 /// types, to avoid some circular reference problems.
50 void dropAllTypeUses();
54 //===--------------------------------------------------------------------===//
55 // Abstract Type handling methods - These types have special lifetimes, which
56 // are managed by (add|remove)AbstractTypeUser. See comments in
57 // AbstractTypeUser.h for more information.
59 /// refineAbstractTypeTo - This function is used to when it is discovered that
60 /// the 'this' abstract type is actually equivalent to the NewType specified.
61 /// This causes all users of 'this' to switch to reference the more concrete
62 /// type NewType and for 'this' to be deleted.
64 void refineAbstractTypeTo(const Type *NewType);
66 void dump() const { Type::dump(); }
68 // Methods for support type inquiry through isa, cast, and dyn_cast:
69 static inline bool classof(const DerivedType *T) { return true; }
70 static inline bool classof(const Type *T) {
71 return T->isDerivedType();
75 /// Class to represent integer types. Note that this class is also used to
76 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
78 /// @brief Integer representation type
79 class IntegerType : public DerivedType {
81 IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
82 setSubclassData(NumBits);
84 friend class TypeMap<IntegerValType, IntegerType>;
86 /// This enum is just used to hold constants we need for IntegerType.
88 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
89 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
90 ///< Note that bit width is stored in the Type classes SubclassData field
91 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
94 /// This static method is the primary way of constructing an IntegerType.
95 /// If an IntegerType with the same NumBits value was previously instantiated,
96 /// that instance will be returned. Otherwise a new one will be created. Only
97 /// one instance with a given NumBits value is ever created.
98 /// @brief Get or create an IntegerType instance.
99 static const IntegerType* get(unsigned NumBits);
101 /// @brief Get the number of bits in this IntegerType
102 unsigned getBitWidth() const { return getSubclassData(); }
104 // Methods for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const IntegerType *T) { return true; }
106 static inline bool classof(const Type *T) {
107 return T->getTypeID() == IntegerTyID;
112 /// FunctionType - Class to represent function types
114 class FunctionType : public DerivedType {
116 /// Function parameters can have attributes to indicate how they should be
117 /// treated by optimizations and code generation. This enumeration lists the
118 /// set of possible attributes.
119 /// @brief Function parameter attributes enumeration.
120 enum ParameterAttributes {
121 NoAttributeSet = 0, ///< No attribute value has been set
122 ZExtAttribute = 1, ///< zero extended before/after call
123 SExtAttribute = 1 << 1, ///< sign extended before/after call
124 NoReturnAttribute = 1 << 2 ///< mark the function as not returning
126 typedef std::vector<ParameterAttributes> ParamAttrsList;
128 friend class TypeMap<FunctionValType, FunctionType>;
130 ParamAttrsList *ParamAttrs;
132 FunctionType(const FunctionType &); // Do not implement
133 const FunctionType &operator=(const FunctionType &); // Do not implement
134 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
135 bool IsVarArgs, const ParamAttrsList &Attrs);
138 /// FunctionType::get - This static method is the primary way of constructing
141 static FunctionType *get(
142 const Type *Result, ///< The result type
143 const std::vector<const Type*> &Params, ///< The types of the parameters
144 bool isVarArg, ///< Whether this is a variable argument length function
145 const ParamAttrsList & Attrs = ParamAttrsList()
146 ///< Indicates the parameter attributes to use, if any. The 0th entry
147 ///< in the list refers to the return type. Parameters are numbered
151 inline bool isVarArg() const { return isVarArgs; }
152 inline const Type *getReturnType() const { return ContainedTys[0]; }
154 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
155 param_iterator param_begin() const { return ContainedTys.begin()+1; }
156 param_iterator param_end() const { return ContainedTys.end(); }
158 // Parameter type accessors...
159 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
161 /// getNumParams - Return the number of fixed parameters this function type
162 /// requires. This does not consider varargs.
164 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
166 /// The parameter attributes for the \p ith parameter are returned. The 0th
167 /// parameter refers to the return type of the function.
168 /// @returns The ParameterAttributes for the \p ith parameter.
169 /// @brief Get the attributes for a parameter
170 ParameterAttributes getParamAttrs(unsigned i) const;
172 /// @brief Determine if a parameter attribute is set
173 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
174 return getParamAttrs(i) & attr;
177 /// @brief Return the number of parameter attributes this type has.
178 unsigned getNumAttrs() const {
179 return (ParamAttrs ? unsigned(ParamAttrs->size()) : 0);
182 /// @brief Convert a ParameterAttribute into its assembly text
183 static std::string getParamAttrsText(ParameterAttributes Attr);
185 // Implement the AbstractTypeUser interface.
186 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
187 virtual void typeBecameConcrete(const DerivedType *AbsTy);
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const FunctionType *T) { return true; }
191 static inline bool classof(const Type *T) {
192 return T->getTypeID() == FunctionTyID;
197 /// CompositeType - Common super class of ArrayType, StructType, PointerType
199 class CompositeType : public DerivedType {
201 inline CompositeType(TypeID id) : DerivedType(id) { }
204 /// getTypeAtIndex - Given an index value into the type, return the type of
207 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
208 virtual bool indexValid(const Value *V) const = 0;
210 // Methods for support type inquiry through isa, cast, and dyn_cast:
211 static inline bool classof(const CompositeType *T) { return true; }
212 static inline bool classof(const Type *T) {
213 return T->getTypeID() == ArrayTyID ||
214 T->getTypeID() == StructTyID ||
215 T->getTypeID() == PointerTyID ||
216 T->getTypeID() == PackedTyID;
221 /// StructType - Class to represent struct types
223 class StructType : public CompositeType {
224 friend class TypeMap<StructValType, StructType>;
225 StructType(const StructType &); // Do not implement
226 const StructType &operator=(const StructType &); // Do not implement
227 StructType(const std::vector<const Type*> &Types, bool isPacked);
229 /// StructType::get - This static method is the primary way to create a
232 static StructType *get(const std::vector<const Type*> &Params,
233 bool isPacked=false);
235 // Iterator access to the elements
236 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
237 element_iterator element_begin() const { return ContainedTys.begin(); }
238 element_iterator element_end() const { return ContainedTys.end(); }
240 // Random access to the elements
241 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
242 const Type *getElementType(unsigned N) const {
243 assert(N < ContainedTys.size() && "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 bool indexValid(const Value *V) const;
253 // Implement the AbstractTypeUser interface.
254 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
255 virtual void typeBecameConcrete(const DerivedType *AbsTy);
257 // Methods for support type inquiry through isa, cast, and dyn_cast:
258 static inline bool classof(const StructType *T) { return true; }
259 static inline bool classof(const Type *T) {
260 return T->getTypeID() == StructTyID;
263 bool isPacked() const { return getSubclassData(); }
267 /// SequentialType - This is the superclass of the array, pointer and packed
268 /// type classes. All of these represent "arrays" in memory. The array type
269 /// represents a specifically sized array, pointer types are unsized/unknown
270 /// size arrays, packed types represent specifically sized arrays that
271 /// allow for use of SIMD instructions. SequentialType holds the common
272 /// features of all, which stem from the fact that all three lay their
273 /// components out in memory identically.
275 class SequentialType : public CompositeType {
276 SequentialType(const SequentialType &); // Do not implement!
277 const SequentialType &operator=(const SequentialType &); // Do not implement!
279 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
280 ContainedTys.reserve(1);
281 ContainedTys.push_back(PATypeHandle(ElType, this));
285 inline const Type *getElementType() const { return ContainedTys[0]; }
287 virtual bool indexValid(const Value *V) const;
289 /// getTypeAtIndex - Given an index value into the type, return the type of
290 /// the element. For sequential types, there is only one subtype...
292 virtual const Type *getTypeAtIndex(const Value *V) const {
293 return ContainedTys[0];
296 // Methods for support type inquiry through isa, cast, and dyn_cast:
297 static inline bool classof(const SequentialType *T) { return true; }
298 static inline bool classof(const Type *T) {
299 return T->getTypeID() == ArrayTyID ||
300 T->getTypeID() == PointerTyID ||
301 T->getTypeID() == PackedTyID;
306 /// ArrayType - Class to represent array types
308 class ArrayType : public SequentialType {
309 friend class TypeMap<ArrayValType, ArrayType>;
310 uint64_t NumElements;
312 ArrayType(const ArrayType &); // Do not implement
313 const ArrayType &operator=(const ArrayType &); // Do not implement
314 ArrayType(const Type *ElType, uint64_t NumEl);
316 /// ArrayType::get - This static method is the primary way to construct an
319 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
321 inline uint64_t getNumElements() const { return NumElements; }
323 // Implement the AbstractTypeUser interface.
324 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
325 virtual void typeBecameConcrete(const DerivedType *AbsTy);
327 // Methods for support type inquiry through isa, cast, and dyn_cast:
328 static inline bool classof(const ArrayType *T) { return true; }
329 static inline bool classof(const Type *T) {
330 return T->getTypeID() == ArrayTyID;
334 /// PackedType - Class to represent packed types
336 class PackedType : public SequentialType {
337 friend class TypeMap<PackedValType, PackedType>;
338 unsigned NumElements;
340 PackedType(const PackedType &); // Do not implement
341 const PackedType &operator=(const PackedType &); // Do not implement
342 PackedType(const Type *ElType, unsigned NumEl);
344 /// PackedType::get - This static method is the primary way to construct an
347 static PackedType *get(const Type *ElementType, unsigned NumElements);
349 /// @brief Return the number of elements in the Packed type.
350 inline unsigned getNumElements() const { return NumElements; }
352 /// @brief Return the number of bits in the Packed type.
353 inline unsigned getBitWidth() const {
354 return NumElements *getElementType()->getPrimitiveSizeInBits();
357 // Implement the AbstractTypeUser interface.
358 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
359 virtual void typeBecameConcrete(const DerivedType *AbsTy);
361 // Methods for support type inquiry through isa, cast, and dyn_cast:
362 static inline bool classof(const PackedType *T) { return true; }
363 static inline bool classof(const Type *T) {
364 return T->getTypeID() == PackedTyID;
369 /// PointerType - Class to represent pointers
371 class PointerType : public SequentialType {
372 friend class TypeMap<PointerValType, PointerType>;
373 PointerType(const PointerType &); // Do not implement
374 const PointerType &operator=(const PointerType &); // Do not implement
375 PointerType(const Type *ElType);
377 /// PointerType::get - This is the only way to construct a new pointer type.
378 static PointerType *get(const Type *ElementType);
380 // Implement the AbstractTypeUser interface.
381 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
382 virtual void typeBecameConcrete(const DerivedType *AbsTy);
384 // Implement support type inquiry through isa, cast, and dyn_cast:
385 static inline bool classof(const PointerType *T) { return true; }
386 static inline bool classof(const Type *T) {
387 return T->getTypeID() == PointerTyID;
392 /// OpaqueType - Class to represent abstract types
394 class OpaqueType : public DerivedType {
395 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
396 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
399 /// OpaqueType::get - Static factory method for the OpaqueType class...
401 static OpaqueType *get() {
402 return new OpaqueType(); // All opaque types are distinct
405 // Implement the AbstractTypeUser interface.
406 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
407 abort(); // FIXME: this is not really an AbstractTypeUser!
409 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
410 abort(); // FIXME: this is not really an AbstractTypeUser!
413 // Implement support for type inquiry through isa, cast, and dyn_cast:
414 static inline bool classof(const OpaqueType *T) { return true; }
415 static inline bool classof(const Type *T) {
416 return T->getTypeID() == OpaqueTyID;
420 } // End llvm namespace