1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
3 // This file contains the declarations of classes that represent "derived
4 // types". These are things like "arrays of x" or "structure of x, y, z" or
5 // "method returning x taking (y,z) as parameters", etc...
7 // The implementations of these classes live in the Type.cpp file.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
14 #include "llvm/Type.h"
16 template<class ValType, class TypeClass> class TypeMap;
17 class FunctionValType;
22 class DerivedType : public Type, public AbstractTypeUser {
23 /// RefCount - This counts the number of PATypeHolders that are pointing to
24 /// this type. When this number falls to zero, if the type is abstract and
25 /// has no AbstractTypeUsers, the type is deleted.
27 mutable unsigned RefCount;
29 // isRefining - Used for recursive types
32 // AbstractTypeUsers - Implement a list of the users that need to be notified
33 // if I am a type, and I get resolved into a more concrete type.
35 ///// FIXME: kill mutable nonsense when Type's are not const
36 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
39 DerivedType(PrimitiveID id) : Type("", id), RefCount(0), isRefining(0) {
42 assert(AbstractTypeUsers.empty());
45 // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
46 // has been refined a bit. The pointer is still valid and still should be
47 // used, but the subtypes have changed.
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. This also removes the
54 // type from the internal tables of available types.
55 virtual void dropAllTypeUses(bool inMap) = 0;
58 void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
62 //===--------------------------------------------------------------------===//
63 // Abstract Type handling methods - These types have special lifetimes, which
64 // are managed by (add|remove)AbstractTypeUser. See comments in
65 // AbstractTypeUser.h for more information.
67 // addAbstractTypeUser - Notify an abstract type that there is a new user of
68 // it. This function is called primarily by the PATypeHandle class.
70 void addAbstractTypeUser(AbstractTypeUser *U) const {
71 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
72 AbstractTypeUsers.push_back(U);
75 // removeAbstractTypeUser - Notify an abstract type that a user of the class
76 // no longer has a handle to the type. This function is called primarily by
77 // the PATypeHandle class. When there are no users of the abstract type, it
78 // is annihilated, because there is no way to get a reference to it ever
81 void removeAbstractTypeUser(AbstractTypeUser *U) const;
83 // refineAbstractTypeTo - This function is used to when it is discovered that
84 // the 'this' abstract type is actually equivalent to the NewType specified.
85 // This causes all users of 'this' to switch to reference the more concrete
86 // type NewType and for 'this' to be deleted.
88 void refineAbstractTypeTo(const Type *NewType) {
89 refineAbstractTypeToInternal(NewType, true);
93 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
97 void dropRef() const {
98 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
99 assert(RefCount && "No objects are currently referencing this object!");
101 // If this is the last PATypeHolder using this object, and there are no
102 // PATypeHandles using it, the type is dead, delete it now.
103 if (--RefCount == 0 && AbstractTypeUsers.empty())
108 void dump() const { Value::dump(); }
110 // Methods for support type inquiry through isa, cast, and dyn_cast:
111 static inline bool classof(const DerivedType *T) { return true; }
112 static inline bool classof(const Type *T) {
113 return T->isDerivedType();
115 static inline bool classof(const Value *V) {
116 return isa<Type>(V) && classof(cast<Type>(V));
123 struct FunctionType : public DerivedType {
124 typedef std::vector<PATypeHandle> ParamTypes;
125 friend class TypeMap<FunctionValType, FunctionType>;
127 PATypeHandle ResultType;
131 FunctionType(const FunctionType &); // Do not implement
132 const FunctionType &operator=(const FunctionType &); // Do not implement
134 // This should really be private, but it squelches a bogus warning
135 // from GCC to make them protected: warning: `class FunctionType' only
136 // defines private constructors and has no friends
138 // Private ctor - Only can be created by a static member...
139 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
142 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
143 // another (more concrete) type, we must eliminate all references to other
144 // types, to avoid some circular reference problems. This also removes the
145 // type from the internal tables of available types.
146 virtual void dropAllTypeUses(bool inMap);
150 inline bool isVarArg() const { return isVarArgs; }
151 inline const Type *getReturnType() const { return ResultType; }
152 inline const ParamTypes &getParamTypes() const { return ParamTys; }
154 // Parameter type accessors...
155 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
157 // getNumParams - Return the number of fixed parameters this function type
158 // requires. This does not consider varargs.
160 unsigned getNumParams() const { return ParamTys.size(); }
163 virtual const Type *getContainedType(unsigned i) const {
164 return i == 0 ? ResultType :
165 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
167 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
169 // refineAbstractType - Called when a contained type is found to be more
170 // concrete - this could potentially change us from an abstract type to a
173 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
175 static FunctionType *get(const Type *Result,
176 const std::vector<const Type*> &Params,
179 // Methods for support type inquiry through isa, cast, and dyn_cast:
180 static inline bool classof(const FunctionType *T) { return true; }
181 static inline bool classof(const Type *T) {
182 return T->getPrimitiveID() == FunctionTyID;
184 static inline bool classof(const Value *V) {
185 return isa<Type>(V) && classof(cast<Type>(V));
190 // CompositeType - Common super class of ArrayType, StructType, and PointerType
192 class CompositeType : public DerivedType {
194 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
197 // getTypeAtIndex - Given an index value into the type, return the type of the
200 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
201 virtual bool indexValid(const Value *V) const = 0;
203 // getIndexType - Return the type required of indices for this composite.
204 // For structures, this is ubyte, for arrays, this is uint
206 virtual const Type *getIndexType() const = 0;
209 // Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const CompositeType *T) { return true; }
211 static inline bool classof(const Type *T) {
212 return T->getPrimitiveID() == ArrayTyID ||
213 T->getPrimitiveID() == StructTyID ||
214 T->getPrimitiveID() == PointerTyID;
216 static inline bool classof(const Value *V) {
217 return isa<Type>(V) && classof(cast<Type>(V));
222 struct StructType : public CompositeType {
223 friend class TypeMap<StructValType, StructType>;
224 typedef std::vector<PATypeHandle> ElementTypes;
227 ElementTypes ETypes; // Element types of struct
229 StructType(const StructType &); // Do not implement
230 const StructType &operator=(const StructType &); // Do not implement
233 // This should really be private, but it squelches a bogus warning
234 // from GCC to make them protected: warning: `class StructType' only
235 // defines private constructors and has no friends
237 // Private ctor - Only can be created by a static member...
238 StructType(const std::vector<const Type*> &Types);
240 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
241 // another (more concrete) type, we must eliminate all references to other
242 // types, to avoid some circular reference problems. This also removes the
243 // type from the internal tables of available types.
244 virtual void dropAllTypeUses(bool inMap);
247 inline const ElementTypes &getElementTypes() const { return ETypes; }
249 virtual const Type *getContainedType(unsigned i) const {
250 return i < ETypes.size() ? ETypes[i].get() : 0;
252 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
254 // getTypeAtIndex - Given an index value into the type, return the type of the
255 // element. For a structure type, this must be a constant value...
257 virtual const Type *getTypeAtIndex(const Value *V) const ;
258 virtual bool indexValid(const Value *V) const;
260 // getIndexType - Return the type required of indices for this composite.
261 // For structures, this is ubyte, for arrays, this is uint
263 virtual const Type *getIndexType() const { return Type::UByteTy; }
265 // refineAbstractType - Called when a contained type is found to be more
266 // concrete - this could potentially change us from an abstract type to a
269 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
271 static StructType *get(const std::vector<const Type*> &Params);
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const StructType *T) { return true; }
275 static inline bool classof(const Type *T) {
276 return T->getPrimitiveID() == StructTyID;
278 static inline bool classof(const Value *V) {
279 return isa<Type>(V) && classof(cast<Type>(V));
284 // SequentialType - This is the superclass of the array and pointer type
285 // classes. Both of these represent "arrays" in memory. The array type
286 // represents a specifically sized array, pointer types are unsized/unknown size
287 // arrays. SequentialType holds the common features of both, which stem from
288 // the fact that both lay their components out in memory identically.
290 class SequentialType : public CompositeType {
291 SequentialType(const SequentialType &); // Do not implement!
292 const SequentialType &operator=(const SequentialType &); // Do not implement!
294 PATypeHandle ElementType;
296 SequentialType(PrimitiveID TID, const Type *ElType)
297 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
301 inline const Type *getElementType() const { return ElementType; }
303 virtual const Type *getContainedType(unsigned i) const {
304 return i == 0 ? ElementType.get() : 0;
306 virtual unsigned getNumContainedTypes() const { return 1; }
308 // getTypeAtIndex - Given an index value into the type, return the type of the
309 // element. For sequential types, there is only one subtype...
311 virtual const Type *getTypeAtIndex(const Value *V) const {
312 return ElementType.get();
314 virtual bool indexValid(const Value *V) const {
315 return V->getType() == Type::LongTy; // Must be a 'long' index
318 // getIndexType() - Return the type required of indices for this composite.
319 // For structures, this is ubyte, for arrays, this is uint
321 virtual const Type *getIndexType() const { return Type::LongTy; }
323 // Methods for support type inquiry through isa, cast, and dyn_cast:
324 static inline bool classof(const SequentialType *T) { return true; }
325 static inline bool classof(const Type *T) {
326 return T->getPrimitiveID() == ArrayTyID ||
327 T->getPrimitiveID() == PointerTyID;
329 static inline bool classof(const Value *V) {
330 return isa<Type>(V) && classof(cast<Type>(V));
335 class ArrayType : public SequentialType {
336 friend class TypeMap<ArrayValType, ArrayType>;
337 unsigned NumElements;
339 ArrayType(const ArrayType &); // Do not implement
340 const ArrayType &operator=(const ArrayType &); // Do not implement
342 // This should really be private, but it squelches a bogus warning
343 // from GCC to make them protected: warning: `class ArrayType' only
344 // defines private constructors and has no friends
346 // Private ctor - Only can be created by a static member...
347 ArrayType(const Type *ElType, unsigned NumEl);
349 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
350 // another (more concrete) type, we must eliminate all references to other
351 // types, to avoid some circular reference problems. This also removes the
352 // type from the internal tables of available types.
353 virtual void dropAllTypeUses(bool inMap);
356 inline unsigned getNumElements() const { return NumElements; }
358 // refineAbstractType - Called when a contained type is found to be more
359 // concrete - this could potentially change us from an abstract type to a
362 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
364 static ArrayType *get(const Type *ElementType, unsigned NumElements);
366 // Methods for support type inquiry through isa, cast, and dyn_cast:
367 static inline bool classof(const ArrayType *T) { return true; }
368 static inline bool classof(const Type *T) {
369 return T->getPrimitiveID() == ArrayTyID;
371 static inline bool classof(const Value *V) {
372 return isa<Type>(V) && classof(cast<Type>(V));
378 class PointerType : public SequentialType {
379 friend class TypeMap<PointerValType, PointerType>;
380 PointerType(const PointerType &); // Do not implement
381 const PointerType &operator=(const PointerType &); // Do not implement
383 // This should really be private, but it squelches a bogus warning
384 // from GCC to make them protected: warning: `class PointerType' only
385 // defines private constructors and has no friends
387 // Private ctor - Only can be created by a static member...
388 PointerType(const Type *ElType);
390 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
391 // another (more concrete) type, we must eliminate all references to other
392 // types, to avoid some circular reference problems. This also removes the
393 // type from the internal tables of available types.
394 virtual void dropAllTypeUses(bool inMap);
396 // PointerType::get - Named constructor for pointer types...
397 static PointerType *get(const Type *ElementType);
399 // refineAbstractType - Called when a contained type is found to be more
400 // concrete - this could potentially change us from an abstract type to a
403 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
405 // Methods for support type inquiry through isa, cast, and dyn_cast:
406 static inline bool classof(const PointerType *T) { return true; }
407 static inline bool classof(const Type *T) {
408 return T->getPrimitiveID() == PointerTyID;
410 static inline bool classof(const Value *V) {
411 return isa<Type>(V) && classof(cast<Type>(V));
416 class OpaqueType : public DerivedType {
417 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
418 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
420 // This should really be private, but it squelches a bogus warning
421 // from GCC to make them protected: warning: `class OpaqueType' only
422 // defines private constructors and has no friends
424 // Private ctor - Only can be created by a static member...
427 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
428 // another (more concrete) type, we must eliminate all references to other
429 // types, to avoid some circular reference problems.
430 virtual void dropAllTypeUses(bool inMap) {} // No type uses
434 // get - Static factory method for the OpaqueType class...
435 static OpaqueType *get() {
436 return new OpaqueType(); // All opaque types are distinct
439 // refineAbstractType - Called when a contained type is found to be more
440 // concrete - this could potentially change us from an abstract type to a
443 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
444 // This class never uses other types!
449 // Methods for support type inquiry through isa, cast, and dyn_cast:
450 static inline bool classof(const OpaqueType *T) { return true; }
451 static inline bool classof(const Type *T) {
452 return T->getPrimitiveID() == OpaqueTyID;
454 static inline bool classof(const Value *V) {
455 return isa<Type>(V) && classof(cast<Type>(V));
460 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
461 // These are defined here because they MUST be inlined, yet are dependent on
462 // the definition of the Type class. Of course Type derives from Value, which
463 // contains an AbstractTypeUser instance, so there is no good way to factor out
464 // the code. Hence this bit of uglyness.
466 inline void PATypeHandle::addUser() {
467 assert(Ty && "Type Handle has a null type!");
468 if (Ty->isAbstract())
469 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
471 inline void PATypeHandle::removeUser() {
472 if (Ty->isAbstract())
473 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
476 inline void PATypeHandle::removeUserFromConcrete() {
477 if (!Ty->isAbstract())
478 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
481 // Define inline methods for PATypeHolder...
483 inline void PATypeHolder::addRef() {
484 if (Ty->isAbstract())
485 cast<DerivedType>(Ty)->addRef();
488 inline void PATypeHolder::dropRef() {
489 if (Ty->isAbstract())
490 cast<DerivedType>(Ty)->dropRef();
493 /// get - This implements the forwarding part of the union-find algorithm for
494 /// abstract types. Before every access to the Type*, we check to see if the
495 /// type we are pointing to is forwarding to a new type. If so, we drop our
496 /// reference to the type.
497 inline const Type* PATypeHolder::get() const {
498 const Type *NewTy = Ty->getForwardedType();
499 if (!NewTy) return Ty;
500 return *const_cast<PATypeHolder*>(this) = NewTy;