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, public AbstractTypeUser {
34 // AbstractTypeUsers - Implement a list of the users that need to be notified
35 // if I am a type, and I get resolved into a more concrete type.
37 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
40 DerivedType(TypeID id) : Type("", id) {}
42 assert(AbstractTypeUsers.empty());
45 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
46 /// that the current type has transitioned from being abstract to being
49 void notifyUsesThatTypeBecameConcrete();
51 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
52 /// another (more concrete) type, we must eliminate all references to other
53 /// types, to avoid some circular reference problems.
55 void dropAllTypeUses();
57 void RefCountIsZero() const {
58 if (AbstractTypeUsers.empty())
65 //===--------------------------------------------------------------------===//
66 // Abstract Type handling methods - These types have special lifetimes, which
67 // are managed by (add|remove)AbstractTypeUser. See comments in
68 // AbstractTypeUser.h for more information.
70 /// addAbstractTypeUser - Notify an abstract type that there is a new user of
71 /// it. This function is called primarily by the PATypeHandle class.
73 void addAbstractTypeUser(AbstractTypeUser *U) const {
74 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
75 AbstractTypeUsers.push_back(U);
78 /// removeAbstractTypeUser - Notify an abstract type that a user of the class
79 /// no longer has a handle to the type. This function is called primarily by
80 /// the PATypeHandle class. When there are no users of the abstract type, it
81 /// is annihilated, because there is no way to get a reference to it ever
84 void removeAbstractTypeUser(AbstractTypeUser *U) const;
86 /// refineAbstractTypeTo - This function is used to when it is discovered that
87 /// the 'this' abstract type is actually equivalent to the NewType specified.
88 /// This causes all users of 'this' to switch to reference the more concrete
89 /// type NewType and for 'this' to be deleted.
91 void refineAbstractTypeTo(const Type *NewType);
93 void dump() const { Type::dump(); }
95 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const DerivedType *T) { return true; }
97 static inline bool classof(const Type *T) {
98 return T->isDerivedType();
103 /// FunctionType - Class to represent function types
105 class FunctionType : public DerivedType {
106 friend class TypeMap<FunctionValType, FunctionType>;
109 FunctionType(const FunctionType &); // Do not implement
110 const FunctionType &operator=(const FunctionType &); // Do not implement
112 /// This should really be private, but it squelches a bogus warning
113 /// from GCC to make them protected: warning: `class FunctionType' only
114 /// defines private constructors and has no friends
116 /// Private ctor - Only can be created by a static member...
118 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
122 /// FunctionType::get - This static method is the primary way of constructing
125 static FunctionType *get(const Type *Result,
126 const std::vector<const Type*> &Params,
129 inline bool isVarArg() const { return isVarArgs; }
130 inline const Type *getReturnType() const { return ContainedTys[0]; }
132 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
133 param_iterator param_begin() const { return ContainedTys.begin()+1; }
134 param_iterator param_end() const { return ContainedTys.end(); }
136 // Parameter type accessors...
137 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
139 /// getNumParams - Return the number of fixed parameters this function type
140 /// requires. This does not consider varargs.
142 unsigned getNumParams() const { return ContainedTys.size()-1; }
144 // Implement the AbstractTypeUser interface.
145 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
146 virtual void typeBecameConcrete(const DerivedType *AbsTy);
148 // Methods for support type inquiry through isa, cast, and dyn_cast:
149 static inline bool classof(const FunctionType *T) { return true; }
150 static inline bool classof(const Type *T) {
151 return T->getTypeID() == FunctionTyID;
156 /// CompositeType - Common super class of ArrayType, StructType, PointerType
158 class CompositeType : public DerivedType {
160 inline CompositeType(TypeID id) : DerivedType(id) { }
163 /// getTypeAtIndex - Given an index value into the type, return the type of
166 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
167 virtual bool indexValid(const Value *V) const = 0;
169 // Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const CompositeType *T) { return true; }
171 static inline bool classof(const Type *T) {
172 return T->getTypeID() == ArrayTyID ||
173 T->getTypeID() == StructTyID ||
174 T->getTypeID() == PointerTyID ||
175 T->getTypeID() == PackedTyID;
180 /// StructType - Class to represent struct types
182 class StructType : public CompositeType {
183 friend class TypeMap<StructValType, StructType>;
184 StructType(const StructType &); // Do not implement
185 const StructType &operator=(const StructType &); // Do not implement
188 /// This should really be private, but it squelches a bogus warning
189 /// from GCC to make them protected: warning: `class StructType' only
190 /// defines private constructors and has no friends
192 /// Private ctor - Only can be created by a static member...
194 StructType(const std::vector<const Type*> &Types);
197 /// StructType::get - This static method is the primary way to create a
200 static StructType *get(const std::vector<const Type*> &Params);
202 // Iterator access to the elements
203 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
204 element_iterator element_begin() const { return ContainedTys.begin(); }
205 element_iterator element_end() const { return ContainedTys.end(); }
207 // Random access to the elements
208 unsigned getNumElements() const { return ContainedTys.size(); }
209 const Type *getElementType(unsigned N) const {
210 assert(N < ContainedTys.size() && "Element number out of range!");
211 return ContainedTys[N];
214 /// getTypeAtIndex - Given an index value into the type, return the type of
215 /// the element. For a structure type, this must be a constant value...
217 virtual const Type *getTypeAtIndex(const Value *V) const ;
218 virtual bool indexValid(const Value *V) const;
220 // Implement the AbstractTypeUser interface.
221 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
222 virtual void typeBecameConcrete(const DerivedType *AbsTy);
224 // Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const StructType *T) { return true; }
226 static inline bool classof(const Type *T) {
227 return T->getTypeID() == StructTyID;
232 /// SequentialType - This is the superclass of the array, pointer and packed
233 /// type classes. All of these represent "arrays" in memory. The array type
234 /// represents a specifically sized array, pointer types are unsized/unknown
235 /// size arrays, packed types represent specifically sized arrays that
236 /// allow for use of SIMD instructions. SequentialType holds the common
237 /// features of all, which stem from the fact that all three lay their
238 /// components out in memory identically.
240 class SequentialType : public CompositeType {
241 SequentialType(const SequentialType &); // Do not implement!
242 const SequentialType &operator=(const SequentialType &); // Do not implement!
244 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
245 ContainedTys.reserve(1);
246 ContainedTys.push_back(PATypeHandle(ElType, this));
250 inline const Type *getElementType() const { return ContainedTys[0]; }
252 virtual bool indexValid(const Value *V) const;
254 /// getTypeAtIndex - Given an index value into the type, return the type of
255 /// the element. For sequential types, there is only one subtype...
257 virtual const Type *getTypeAtIndex(const Value *V) const {
258 return ContainedTys[0];
261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const SequentialType *T) { return true; }
263 static inline bool classof(const Type *T) {
264 return T->getTypeID() == ArrayTyID ||
265 T->getTypeID() == PointerTyID ||
266 T->getTypeID() == PackedTyID;
271 /// ArrayType - Class to represent array types
273 class ArrayType : public SequentialType {
274 friend class TypeMap<ArrayValType, ArrayType>;
275 unsigned NumElements;
277 ArrayType(const ArrayType &); // Do not implement
278 const ArrayType &operator=(const ArrayType &); // Do not implement
280 /// This should really be private, but it squelches a bogus warning
281 /// from GCC to make them protected: warning: `class ArrayType' only
282 /// defines private constructors and has no friends
284 /// Private ctor - Only can be created by a static member...
286 ArrayType(const Type *ElType, unsigned NumEl);
289 /// ArrayType::get - This static method is the primary way to construct an
292 static ArrayType *get(const Type *ElementType, unsigned NumElements);
294 inline unsigned getNumElements() const { return NumElements; }
296 // Implement the AbstractTypeUser interface.
297 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
298 virtual void typeBecameConcrete(const DerivedType *AbsTy);
300 // Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool classof(const ArrayType *T) { return true; }
302 static inline bool classof(const Type *T) {
303 return T->getTypeID() == ArrayTyID;
307 /// PackedType - Class to represent packed types
309 class PackedType : public SequentialType {
310 friend class TypeMap<PackedValType, PackedType>;
311 unsigned NumElements;
313 PackedType(const PackedType &); // Do not implement
314 const PackedType &operator=(const PackedType &); // Do not implement
316 /// This should really be private, but it squelches a bogus warning
317 /// from GCC to make them protected: warning: `class PackedType' only
318 /// defines private constructors and has no friends
320 /// Private ctor - Only can be created by a static member...
322 PackedType(const Type *ElType, unsigned NumEl);
325 /// PackedType::get - This static method is the primary way to construct an
328 static PackedType *get(const Type *ElementType, unsigned NumElements);
330 inline unsigned getNumElements() const { return NumElements; }
332 // Implement the AbstractTypeUser interface.
333 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
334 virtual void typeBecameConcrete(const DerivedType *AbsTy);
336 // Methods for support type inquiry through isa, cast, and dyn_cast:
337 static inline bool classof(const PackedType *T) { return true; }
338 static inline bool classof(const Type *T) {
339 return T->getTypeID() == PackedTyID;
344 /// PointerType - Class to represent pointers
346 class PointerType : public SequentialType {
347 friend class TypeMap<PointerValType, PointerType>;
348 PointerType(const PointerType &); // Do not implement
349 const PointerType &operator=(const PointerType &); // Do not implement
351 // This should really be private, but it squelches a bogus warning
352 // from GCC to make them protected: warning: `class PointerType' only
353 // defines private constructors and has no friends
355 // Private ctor - Only can be created by a static member...
356 PointerType(const Type *ElType);
359 /// PointerType::get - This is the only way to construct a new pointer type.
360 static PointerType *get(const Type *ElementType);
362 // Implement the AbstractTypeUser interface.
363 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
364 virtual void typeBecameConcrete(const DerivedType *AbsTy);
366 // Implement support type inquiry through isa, cast, and dyn_cast:
367 static inline bool classof(const PointerType *T) { return true; }
368 static inline bool classof(const Type *T) {
369 return T->getTypeID() == PointerTyID;
374 /// OpaqueType - Class to represent abstract types
376 class OpaqueType : public DerivedType {
377 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
378 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
380 /// This should really be private, but it squelches a bogus warning
381 /// from GCC to make them protected: warning: `class OpaqueType' only
382 /// defines private constructors and has no friends
384 /// Private ctor - Only can be created by a static member...
388 /// OpaqueType::get - Static factory method for the OpaqueType class...
390 static OpaqueType *get() {
391 return new OpaqueType(); // All opaque types are distinct
394 // Implement the AbstractTypeUser interface.
395 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
396 abort(); // FIXME: this is not really an AbstractTypeUser!
398 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
399 abort(); // FIXME: this is not really an AbstractTypeUser!
402 // Implement support for type inquiry through isa, cast, and dyn_cast:
403 static inline bool classof(const OpaqueType *T) { return true; }
404 static inline bool classof(const Type *T) {
405 return T->getTypeID() == OpaqueTyID;
409 } // End llvm namespace