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
85 ZExtAttribute = 1, ///< zero extended before/after call
86 SExtAttribute = 1 << 1, ///< sign extended before/after call
87 NoReturnAttribute = 1 << 2 ///< mark the function as not returning
89 typedef std::vector<ParameterAttributes> ParamAttrsList;
91 friend class TypeMap<FunctionValType, FunctionType>;
93 ParamAttrsList *ParamAttrs;
95 FunctionType(const FunctionType &); // Do not implement
96 const FunctionType &operator=(const FunctionType &); // Do not implement
97 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
98 bool IsVarArgs, const ParamAttrsList &Attrs);
101 /// FunctionType::get - This static method is the primary way of constructing
104 static FunctionType *get(
105 const Type *Result, ///< The result type
106 const std::vector<const Type*> &Params, ///< The types of the parameters
107 bool isVarArg, ///< Whether this is a variable argument length function
108 const ParamAttrsList & Attrs = ParamAttrsList()
109 ///< Indicates the parameter attributes to use, if any. The 0th entry
110 ///< in the list refers to the return type. Parameters are numbered
114 inline bool isVarArg() const { return isVarArgs; }
115 inline const Type *getReturnType() const { return ContainedTys[0]; }
117 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
118 param_iterator param_begin() const { return ContainedTys.begin()+1; }
119 param_iterator param_end() const { return ContainedTys.end(); }
121 // Parameter type accessors...
122 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
124 /// getNumParams - Return the number of fixed parameters this function type
125 /// requires. This does not consider varargs.
127 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
129 /// The parameter attributes for the \p ith parameter are returned. The 0th
130 /// parameter refers to the return type of the function.
131 /// @returns The ParameterAttributes for the \p ith parameter.
132 /// @brief Get the attributes for a parameter
133 ParameterAttributes getParamAttrs(unsigned i) const;
135 /// @brief Determine if a parameter attribute is set
136 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
137 return getParamAttrs(i) & attr;
140 /// @brief Return the number of parameter attributes this type has.
141 unsigned getNumAttrs() const {
142 return (ParamAttrs ? unsigned(ParamAttrs->size()) : 0);
145 /// @brief Convert a ParameterAttribute into its assembly text
146 static std::string getParamAttrsText(ParameterAttributes Attr);
148 // Implement the AbstractTypeUser interface.
149 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
150 virtual void typeBecameConcrete(const DerivedType *AbsTy);
152 // Methods for support type inquiry through isa, cast, and dyn_cast:
153 static inline bool classof(const FunctionType *T) { return true; }
154 static inline bool classof(const Type *T) {
155 return T->getTypeID() == FunctionTyID;
160 /// CompositeType - Common super class of ArrayType, StructType, PointerType
162 class CompositeType : public DerivedType {
164 inline CompositeType(TypeID id) : DerivedType(id) { }
167 /// getTypeAtIndex - Given an index value into the type, return the type of
170 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
171 virtual bool indexValid(const Value *V) const = 0;
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const CompositeType *T) { return true; }
175 static inline bool classof(const Type *T) {
176 return T->getTypeID() == ArrayTyID ||
177 T->getTypeID() == StructTyID ||
178 T->getTypeID() == PointerTyID ||
179 T->getTypeID() == PackedTyID;
184 /// StructType - Class to represent struct types
186 class StructType : public CompositeType {
187 friend class TypeMap<StructValType, StructType>;
188 StructType(const StructType &); // Do not implement
189 const StructType &operator=(const StructType &); // Do not implement
190 StructType(const std::vector<const Type*> &Types, bool isPacked);
192 /// StructType::get - This static method is the primary way to create a
195 static StructType *get(const std::vector<const Type*> &Params,
196 bool isPacked=false);
198 // Iterator access to the elements
199 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
200 element_iterator element_begin() const { return ContainedTys.begin(); }
201 element_iterator element_end() const { return ContainedTys.end(); }
203 // Random access to the elements
204 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
205 const Type *getElementType(unsigned N) const {
206 assert(N < ContainedTys.size() && "Element number out of range!");
207 return ContainedTys[N];
210 /// getTypeAtIndex - Given an index value into the type, return the type of
211 /// the element. For a structure type, this must be a constant value...
213 virtual const Type *getTypeAtIndex(const Value *V) const ;
214 virtual bool indexValid(const Value *V) const;
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 StructType *T) { return true; }
222 static inline bool classof(const Type *T) {
223 return T->getTypeID() == StructTyID;
226 bool isPacked() const { return getSubclassData(); }
230 /// SequentialType - This is the superclass of the array, pointer and packed
231 /// type classes. All of these represent "arrays" in memory. The array type
232 /// represents a specifically sized array, pointer types are unsized/unknown
233 /// size arrays, packed types represent specifically sized arrays that
234 /// allow for use of SIMD instructions. SequentialType holds the common
235 /// features of all, which stem from the fact that all three lay their
236 /// components out in memory identically.
238 class SequentialType : public CompositeType {
239 SequentialType(const SequentialType &); // Do not implement!
240 const SequentialType &operator=(const SequentialType &); // Do not implement!
242 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
243 ContainedTys.reserve(1);
244 ContainedTys.push_back(PATypeHandle(ElType, this));
248 inline const Type *getElementType() const { return ContainedTys[0]; }
250 virtual bool indexValid(const Value *V) const;
252 /// getTypeAtIndex - Given an index value into the type, return the type of
253 /// the element. For sequential types, there is only one subtype...
255 virtual const Type *getTypeAtIndex(const Value *V) const {
256 return ContainedTys[0];
259 // Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const SequentialType *T) { return true; }
261 static inline bool classof(const Type *T) {
262 return T->getTypeID() == ArrayTyID ||
263 T->getTypeID() == PointerTyID ||
264 T->getTypeID() == PackedTyID;
269 /// ArrayType - Class to represent array types
271 class ArrayType : public SequentialType {
272 friend class TypeMap<ArrayValType, ArrayType>;
273 uint64_t NumElements;
275 ArrayType(const ArrayType &); // Do not implement
276 const ArrayType &operator=(const ArrayType &); // Do not implement
277 ArrayType(const Type *ElType, uint64_t NumEl);
279 /// ArrayType::get - This static method is the primary way to construct an
282 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
284 inline uint64_t getNumElements() const { return NumElements; }
286 // Implement the AbstractTypeUser interface.
287 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
288 virtual void typeBecameConcrete(const DerivedType *AbsTy);
290 // Methods for support type inquiry through isa, cast, and dyn_cast:
291 static inline bool classof(const ArrayType *T) { return true; }
292 static inline bool classof(const Type *T) {
293 return T->getTypeID() == ArrayTyID;
297 /// PackedType - Class to represent packed types
299 class PackedType : public SequentialType {
300 friend class TypeMap<PackedValType, PackedType>;
301 unsigned NumElements;
303 PackedType(const PackedType &); // Do not implement
304 const PackedType &operator=(const PackedType &); // Do not implement
305 PackedType(const Type *ElType, unsigned NumEl);
307 /// PackedType::get - This static method is the primary way to construct an
310 static PackedType *get(const Type *ElementType, unsigned NumElements);
312 /// @brief Return the number of elements in the Packed type.
313 inline unsigned getNumElements() const { return NumElements; }
315 /// @brief Return the number of bits in the Packed type.
316 inline unsigned getBitWidth() const {
317 return NumElements *getElementType()->getPrimitiveSizeInBits();
320 // Implement the AbstractTypeUser interface.
321 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
322 virtual void typeBecameConcrete(const DerivedType *AbsTy);
324 // Methods for support type inquiry through isa, cast, and dyn_cast:
325 static inline bool classof(const PackedType *T) { return true; }
326 static inline bool classof(const Type *T) {
327 return T->getTypeID() == PackedTyID;
332 /// PointerType - Class to represent pointers
334 class PointerType : public SequentialType {
335 friend class TypeMap<PointerValType, PointerType>;
336 PointerType(const PointerType &); // Do not implement
337 const PointerType &operator=(const PointerType &); // Do not implement
338 PointerType(const Type *ElType);
340 /// PointerType::get - This is the only way to construct a new pointer type.
341 static PointerType *get(const Type *ElementType);
343 // Implement the AbstractTypeUser interface.
344 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
345 virtual void typeBecameConcrete(const DerivedType *AbsTy);
347 // Implement support type inquiry through isa, cast, and dyn_cast:
348 static inline bool classof(const PointerType *T) { return true; }
349 static inline bool classof(const Type *T) {
350 return T->getTypeID() == PointerTyID;
355 /// OpaqueType - Class to represent abstract types
357 class OpaqueType : public DerivedType {
358 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
359 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
362 /// OpaqueType::get - Static factory method for the OpaqueType class...
364 static OpaqueType *get() {
365 return new OpaqueType(); // All opaque types are distinct
368 // Implement the AbstractTypeUser interface.
369 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
370 abort(); // FIXME: this is not really an AbstractTypeUser!
372 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
373 abort(); // FIXME: this is not really an AbstractTypeUser!
376 // Implement support for type inquiry through isa, cast, and dyn_cast:
377 static inline bool classof(const OpaqueType *T) { return true; }
378 static inline bool classof(const Type *T) {
379 return T->getTypeID() == OpaqueTyID;
383 } // End llvm namespace