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;
33 class DerivedType : public Type {
37 DerivedType(TypeID id) : Type(id) {}
39 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
40 /// that the current type has transitioned from being abstract to being
43 void notifyUsesThatTypeBecameConcrete();
45 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
46 /// another (more concrete) type, we must eliminate all references to other
47 /// types, to avoid some circular reference problems.
49 void dropAllTypeUses();
53 //===--------------------------------------------------------------------===//
54 // Abstract Type handling methods - These types have special lifetimes, which
55 // are managed by (add|remove)AbstractTypeUser. See comments in
56 // AbstractTypeUser.h for more information.
58 /// refineAbstractTypeTo - This function is used to when it is discovered that
59 /// the 'this' abstract type is actually equivalent to the NewType specified.
60 /// This causes all users of 'this' to switch to reference the more concrete
61 /// type NewType and for 'this' to be deleted.
63 void refineAbstractTypeTo(const Type *NewType);
65 void dump() const { Type::dump(); }
67 // Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const DerivedType *T) { return true; }
69 static inline bool classof(const Type *T) {
70 return T->isDerivedType();
75 /// FunctionType - Class to represent function types
77 class FunctionType : public DerivedType {
79 /// Function parameters can have attributes to indicate how they should be
80 /// treated by optimizations and code generation. This enumeration lists the
81 /// set of possible attributes.
82 /// @brief Function parameter attributes enumeration.
83 enum ParameterAttributes {
84 NoAttributeSet = 0, ///< No attribute value has been set on the parameter
85 ZExtAttribute = 1, ///< The parameter should be zero extended before call
86 SExtAttribute = 2 ///< The parameter should be sign extended before call
88 typedef std::vector<ParameterAttributes> ParamAttrsList;
90 friend class TypeMap<FunctionValType, FunctionType>;
92 ParamAttrsList *ParamAttrs;
94 FunctionType(const FunctionType &); // Do not implement
95 const FunctionType &operator=(const FunctionType &); // Do not implement
96 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
97 bool IsVarArgs, const ParamAttrsList &Attrs);
100 /// FunctionType::get - This static method is the primary way of constructing
103 static FunctionType *get(
104 const Type *Result, ///< The result type
105 const std::vector<const Type*> &Params, ///< The types of the parameters
106 bool isVarArg, ///< Whether this is a variable argument length function
107 const ParamAttrsList & Attrs = ParamAttrsList()
108 ///< Indicates the parameter attributes to use, if any. The 0th entry
109 ///< in the list refers to the return type. Parameters are numbered
113 inline bool isVarArg() const { return isVarArgs; }
114 inline const Type *getReturnType() const { return ContainedTys[0]; }
116 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
117 param_iterator param_begin() const { return ContainedTys.begin()+1; }
118 param_iterator param_end() const { return ContainedTys.end(); }
120 // Parameter type accessors...
121 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
123 /// getNumParams - Return the number of fixed parameters this function type
124 /// requires. This does not consider varargs.
126 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
128 /// The parameter attributes for the \p ith parameter are returned. The 0th
129 /// parameter refers to the return type of the function.
130 /// @returns The ParameterAttributes for the \p ith parameter.
131 /// @brief Get the attributes for a parameter
132 ParameterAttributes getParamAttrs(unsigned i) const;
134 /// @brief Determine if a parameter attribute is set
135 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
136 return getParamAttrs(i) & attr;
139 /// @brief Return the number of parameter attributes this type has.
140 unsigned getNumAttrs() const {
141 return (ParamAttrs ? unsigned(ParamAttrs->size()) : 0);
144 /// @brief Convert a ParameterAttribute into its assembly text
145 static const char * getParamAttrsText(ParameterAttributes Attr);
147 // Implement the AbstractTypeUser interface.
148 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
149 virtual void typeBecameConcrete(const DerivedType *AbsTy);
151 // Methods for support type inquiry through isa, cast, and dyn_cast:
152 static inline bool classof(const FunctionType *T) { return true; }
153 static inline bool classof(const Type *T) {
154 return T->getTypeID() == FunctionTyID;
159 /// CompositeType - Common super class of ArrayType, StructType, PointerType
161 class CompositeType : public DerivedType {
163 inline CompositeType(TypeID id) : DerivedType(id) { }
166 /// getTypeAtIndex - Given an index value into the type, return the type of
169 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
170 virtual bool indexValid(const Value *V) const = 0;
172 // Methods for support type inquiry through isa, cast, and dyn_cast:
173 static inline bool classof(const CompositeType *T) { return true; }
174 static inline bool classof(const Type *T) {
175 return T->getTypeID() == ArrayTyID ||
176 T->getTypeID() == StructTyID ||
177 T->getTypeID() == PointerTyID ||
178 T->getTypeID() == PackedTyID;
183 /// StructType - Class to represent struct types
185 class StructType : public CompositeType {
186 friend class TypeMap<StructValType, StructType>;
187 StructType(const StructType &); // Do not implement
188 const StructType &operator=(const StructType &); // Do not implement
191 /// This should really be private, but it squelches a bogus warning
192 /// from GCC to make them protected: warning: `class StructType' only
193 /// defines private constructors and has no friends
195 /// Private ctor - Only can be created by a static member...
197 StructType(const std::vector<const Type*> &Types, bool isPacked);
200 /// StructType::get - This static method is the primary way to create a
203 static StructType *get(const std::vector<const Type*> &Params,
204 bool isPacked=false);
206 // Iterator access to the elements
207 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
208 element_iterator element_begin() const { return ContainedTys.begin(); }
209 element_iterator element_end() const { return ContainedTys.end(); }
211 // Random access to the elements
212 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
213 const Type *getElementType(unsigned N) const {
214 assert(N < ContainedTys.size() && "Element number out of range!");
215 return ContainedTys[N];
218 /// getTypeAtIndex - Given an index value into the type, return the type of
219 /// the element. For a structure type, this must be a constant value...
221 virtual const Type *getTypeAtIndex(const Value *V) const ;
222 virtual bool indexValid(const Value *V) const;
224 // Implement the AbstractTypeUser interface.
225 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
226 virtual void typeBecameConcrete(const DerivedType *AbsTy);
228 // Methods for support type inquiry through isa, cast, and dyn_cast:
229 static inline bool classof(const StructType *T) { return true; }
230 static inline bool classof(const Type *T) {
231 return T->getTypeID() == StructTyID;
234 bool isPacked() const { return getSubclassData(); }
238 /// SequentialType - This is the superclass of the array, pointer and packed
239 /// type classes. All of these represent "arrays" in memory. The array type
240 /// represents a specifically sized array, pointer types are unsized/unknown
241 /// size arrays, packed types represent specifically sized arrays that
242 /// allow for use of SIMD instructions. SequentialType holds the common
243 /// features of all, which stem from the fact that all three lay their
244 /// components out in memory identically.
246 class SequentialType : public CompositeType {
247 SequentialType(const SequentialType &); // Do not implement!
248 const SequentialType &operator=(const SequentialType &); // Do not implement!
250 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
251 ContainedTys.reserve(1);
252 ContainedTys.push_back(PATypeHandle(ElType, this));
256 inline const Type *getElementType() const { return ContainedTys[0]; }
258 virtual bool indexValid(const Value *V) const;
260 /// getTypeAtIndex - Given an index value into the type, return the type of
261 /// the element. For sequential types, there is only one subtype...
263 virtual const Type *getTypeAtIndex(const Value *V) const {
264 return ContainedTys[0];
267 // Methods for support type inquiry through isa, cast, and dyn_cast:
268 static inline bool classof(const SequentialType *T) { return true; }
269 static inline bool classof(const Type *T) {
270 return T->getTypeID() == ArrayTyID ||
271 T->getTypeID() == PointerTyID ||
272 T->getTypeID() == PackedTyID;
277 /// ArrayType - Class to represent array types
279 class ArrayType : public SequentialType {
280 friend class TypeMap<ArrayValType, ArrayType>;
281 uint64_t NumElements;
283 ArrayType(const ArrayType &); // Do not implement
284 const ArrayType &operator=(const ArrayType &); // Do not implement
286 /// This should really be private, but it squelches a bogus warning
287 /// from GCC to make them protected: warning: `class ArrayType' only
288 /// defines private constructors and has no friends
290 /// Private ctor - Only can be created by a static member...
292 ArrayType(const Type *ElType, uint64_t NumEl);
295 /// ArrayType::get - This static method is the primary way to construct an
298 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
300 inline uint64_t getNumElements() const { return NumElements; }
302 // Implement the AbstractTypeUser interface.
303 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
304 virtual void typeBecameConcrete(const DerivedType *AbsTy);
306 // Methods for support type inquiry through isa, cast, and dyn_cast:
307 static inline bool classof(const ArrayType *T) { return true; }
308 static inline bool classof(const Type *T) {
309 return T->getTypeID() == ArrayTyID;
313 /// PackedType - Class to represent packed types
315 class PackedType : public SequentialType {
316 friend class TypeMap<PackedValType, PackedType>;
317 unsigned NumElements;
319 PackedType(const PackedType &); // Do not implement
320 const PackedType &operator=(const PackedType &); // Do not implement
322 /// This should really be private, but it squelches a bogus warning
323 /// from GCC to make them protected: warning: `class PackedType' only
324 /// defines private constructors and has no friends
326 /// Private ctor - Only can be created by a static member...
328 PackedType(const Type *ElType, unsigned NumEl);
331 /// PackedType::get - This static method is the primary way to construct an
334 static PackedType *get(const Type *ElementType, unsigned NumElements);
336 /// @brief Return the number of elements in the Packed type.
337 inline unsigned getNumElements() const { return NumElements; }
339 /// @brief Return the number of bits in the Packed type.
340 inline unsigned getBitWidth() const {
341 return NumElements *getElementType()->getPrimitiveSizeInBits();
344 // Implement the AbstractTypeUser interface.
345 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
346 virtual void typeBecameConcrete(const DerivedType *AbsTy);
348 // Methods for support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const PackedType *T) { return true; }
350 static inline bool classof(const Type *T) {
351 return T->getTypeID() == PackedTyID;
356 /// PointerType - Class to represent pointers
358 class PointerType : public SequentialType {
359 friend class TypeMap<PointerValType, PointerType>;
360 PointerType(const PointerType &); // Do not implement
361 const PointerType &operator=(const PointerType &); // Do not implement
363 // This should really be private, but it squelches a bogus warning
364 // from GCC to make them protected: warning: `class PointerType' only
365 // defines private constructors and has no friends
367 // Private ctor - Only can be created by a static member...
368 PointerType(const Type *ElType);
371 /// PointerType::get - This is the only way to construct a new pointer type.
372 static PointerType *get(const Type *ElementType);
374 // Implement the AbstractTypeUser interface.
375 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
376 virtual void typeBecameConcrete(const DerivedType *AbsTy);
378 // Implement support type inquiry through isa, cast, and dyn_cast:
379 static inline bool classof(const PointerType *T) { return true; }
380 static inline bool classof(const Type *T) {
381 return T->getTypeID() == PointerTyID;
386 /// OpaqueType - Class to represent abstract types
388 class OpaqueType : public DerivedType {
389 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
390 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
392 /// This should really be private, but it squelches a bogus warning
393 /// from GCC to make them protected: warning: `class OpaqueType' only
394 /// defines private constructors and has no friends
396 /// Private ctor - Only can be created by a static member...
400 /// OpaqueType::get - Static factory method for the OpaqueType class...
402 static OpaqueType *get() {
403 return new OpaqueType(); // All opaque types are distinct
406 // Implement the AbstractTypeUser interface.
407 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
408 abort(); // FIXME: this is not really an AbstractTypeUser!
410 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
411 abort(); // FIXME: this is not really an AbstractTypeUser!
414 // Implement support for type inquiry through isa, cast, and dyn_cast:
415 static inline bool classof(const OpaqueType *T) { return true; }
416 static inline bool classof(const Type *T) {
417 return T->getTypeID() == OpaqueTyID;
421 } // End llvm namespace