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 // AbstractTypeUsers - Implement a list of the users that need to be notified
30 // if I am a type, and I get resolved into a more concrete type.
32 ///// FIXME: kill mutable nonsense when Type's are not const
33 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
36 DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {
39 assert(AbstractTypeUsers.empty());
42 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
43 /// that the current type has transitioned from being abstract to being
46 void notifyUsesThatTypeBecameConcrete();
48 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
49 // another (more concrete) type, we must eliminate all references to other
50 // types, to avoid some circular reference problems. This also removes the
51 // type from the internal tables of available types.
52 virtual void dropAllTypeUses(bool inMap) = 0;
55 void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
59 //===--------------------------------------------------------------------===//
60 // Abstract Type handling methods - These types have special lifetimes, which
61 // are managed by (add|remove)AbstractTypeUser. See comments in
62 // AbstractTypeUser.h for more information.
64 // addAbstractTypeUser - Notify an abstract type that there is a new user of
65 // it. This function is called primarily by the PATypeHandle class.
67 void addAbstractTypeUser(AbstractTypeUser *U) const {
68 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
69 AbstractTypeUsers.push_back(U);
72 // removeAbstractTypeUser - Notify an abstract type that a user of the class
73 // no longer has a handle to the type. This function is called primarily by
74 // the PATypeHandle class. When there are no users of the abstract type, it
75 // is annihilated, because there is no way to get a reference to it ever
78 void removeAbstractTypeUser(AbstractTypeUser *U) const;
80 // refineAbstractTypeTo - This function is used to when it is discovered that
81 // the 'this' abstract type is actually equivalent to the NewType specified.
82 // This causes all users of 'this' to switch to reference the more concrete
83 // type NewType and for 'this' to be deleted.
85 void refineAbstractTypeTo(const Type *NewType) {
86 refineAbstractTypeToInternal(NewType, true);
90 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
94 void dropRef() const {
95 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
96 assert(RefCount && "No objects are currently referencing this object!");
98 // If this is the last PATypeHolder using this object, and there are no
99 // PATypeHandles using it, the type is dead, delete it now.
100 if (--RefCount == 0 && AbstractTypeUsers.empty())
105 void dump() const { Value::dump(); }
107 // Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const DerivedType *T) { return true; }
109 static inline bool classof(const Type *T) {
110 return T->isDerivedType();
112 static inline bool classof(const Value *V) {
113 return isa<Type>(V) && classof(cast<Type>(V));
120 struct FunctionType : public DerivedType {
121 typedef std::vector<PATypeHandle> ParamTypes;
122 friend class TypeMap<FunctionValType, FunctionType>;
124 PATypeHandle ResultType;
128 FunctionType(const FunctionType &); // Do not implement
129 const FunctionType &operator=(const FunctionType &); // Do not implement
131 // This should really be private, but it squelches a bogus warning
132 // from GCC to make them protected: warning: `class FunctionType' only
133 // defines private constructors and has no friends
135 // Private ctor - Only can be created by a static member...
136 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
139 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
140 // another (more concrete) type, we must eliminate all references to other
141 // types, to avoid some circular reference problems. This also removes the
142 // type from the internal tables of available types.
143 virtual void dropAllTypeUses(bool inMap);
146 /// FunctionType::get - This static method is the primary way of constructing
148 static FunctionType *get(const Type *Result,
149 const std::vector<const Type*> &Params,
152 inline bool isVarArg() const { return isVarArgs; }
153 inline const Type *getReturnType() const { return ResultType; }
154 inline const ParamTypes &getParamTypes() const { return ParamTys; }
156 // Parameter type accessors...
157 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
159 // getNumParams - Return the number of fixed parameters this function type
160 // requires. This does not consider varargs.
162 unsigned getNumParams() const { return ParamTys.size(); }
165 virtual const Type *getContainedType(unsigned i) const {
166 return i == 0 ? ResultType :
167 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
169 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
171 // Implement the AbstractTypeUser interface.
172 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
173 virtual void typeBecameConcrete(const DerivedType *AbsTy);
175 // Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const FunctionType *T) { return true; }
177 static inline bool classof(const Type *T) {
178 return T->getPrimitiveID() == FunctionTyID;
180 static inline bool classof(const Value *V) {
181 return isa<Type>(V) && classof(cast<Type>(V));
186 // CompositeType - Common super class of ArrayType, StructType, and PointerType
188 class CompositeType : public DerivedType {
190 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
193 // getTypeAtIndex - Given an index value into the type, return the type of the
196 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
197 virtual bool indexValid(const Value *V) const = 0;
199 // getIndexType - Return the type required of indices for this composite.
200 // For structures, this is ubyte, for arrays, this is uint
202 virtual const Type *getIndexType() const = 0;
205 // Methods for support type inquiry through isa, cast, and dyn_cast:
206 static inline bool classof(const CompositeType *T) { return true; }
207 static inline bool classof(const Type *T) {
208 return T->getPrimitiveID() == ArrayTyID ||
209 T->getPrimitiveID() == StructTyID ||
210 T->getPrimitiveID() == PointerTyID;
212 static inline bool classof(const Value *V) {
213 return isa<Type>(V) && classof(cast<Type>(V));
218 struct StructType : public CompositeType {
219 friend class TypeMap<StructValType, StructType>;
220 typedef std::vector<PATypeHandle> ElementTypes;
223 ElementTypes ETypes; // Element types of struct
225 StructType(const StructType &); // Do not implement
226 const StructType &operator=(const StructType &); // Do not implement
229 // This should really be private, but it squelches a bogus warning
230 // from GCC to make them protected: warning: `class StructType' only
231 // defines private constructors and has no friends
233 // Private ctor - Only can be created by a static member...
234 StructType(const std::vector<const Type*> &Types);
236 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
237 // another (more concrete) type, we must eliminate all references to other
238 // types, to avoid some circular reference problems. This also removes the
239 // type from the internal tables of available types.
240 virtual void dropAllTypeUses(bool inMap);
243 /// StructType::get - This static method is the primary way to create a
245 static StructType *get(const std::vector<const Type*> &Params);
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 // Implement the AbstractTypeUser interface.
266 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
267 virtual void typeBecameConcrete(const DerivedType *AbsTy);
269 // Methods for support type inquiry through isa, cast, and dyn_cast:
270 static inline bool classof(const StructType *T) { return true; }
271 static inline bool classof(const Type *T) {
272 return T->getPrimitiveID() == StructTyID;
274 static inline bool classof(const Value *V) {
275 return isa<Type>(V) && classof(cast<Type>(V));
280 // SequentialType - This is the superclass of the array and pointer type
281 // classes. Both of these represent "arrays" in memory. The array type
282 // represents a specifically sized array, pointer types are unsized/unknown size
283 // arrays. SequentialType holds the common features of both, which stem from
284 // the fact that both lay their components out in memory identically.
286 class SequentialType : public CompositeType {
287 SequentialType(const SequentialType &); // Do not implement!
288 const SequentialType &operator=(const SequentialType &); // Do not implement!
290 PATypeHandle ElementType;
292 SequentialType(PrimitiveID TID, const Type *ElType)
293 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
297 inline const Type *getElementType() const { return ElementType; }
299 virtual const Type *getContainedType(unsigned i) const {
300 return i == 0 ? ElementType.get() : 0;
302 virtual unsigned getNumContainedTypes() const { return 1; }
304 // getTypeAtIndex - Given an index value into the type, return the type of the
305 // element. For sequential types, there is only one subtype...
307 virtual const Type *getTypeAtIndex(const Value *V) const {
308 return ElementType.get();
310 virtual bool indexValid(const Value *V) const {
311 return V->getType() == Type::LongTy; // Must be a 'long' index
314 // getIndexType() - Return the type required of indices for this composite.
315 // For structures, this is ubyte, for arrays, this is uint
317 virtual const Type *getIndexType() const { return Type::LongTy; }
319 // Methods for support type inquiry through isa, cast, and dyn_cast:
320 static inline bool classof(const SequentialType *T) { return true; }
321 static inline bool classof(const Type *T) {
322 return T->getPrimitiveID() == ArrayTyID ||
323 T->getPrimitiveID() == PointerTyID;
325 static inline bool classof(const Value *V) {
326 return isa<Type>(V) && classof(cast<Type>(V));
331 class ArrayType : public SequentialType {
332 friend class TypeMap<ArrayValType, ArrayType>;
333 unsigned NumElements;
335 ArrayType(const ArrayType &); // Do not implement
336 const ArrayType &operator=(const ArrayType &); // Do not implement
338 // This should really be private, but it squelches a bogus warning
339 // from GCC to make them protected: warning: `class ArrayType' only
340 // defines private constructors and has no friends
342 // Private ctor - Only can be created by a static member...
343 ArrayType(const Type *ElType, unsigned NumEl);
345 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
346 // another (more concrete) type, we must eliminate all references to other
347 // types, to avoid some circular reference problems. This also removes the
348 // type from the internal tables of available types.
349 virtual void dropAllTypeUses(bool inMap);
352 /// ArrayType::get - This static method is the primary way to construct an
354 static ArrayType *get(const Type *ElementType, unsigned NumElements);
356 inline unsigned getNumElements() const { return NumElements; }
358 // Implement the AbstractTypeUser interface.
359 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
360 virtual void typeBecameConcrete(const DerivedType *AbsTy);
362 // Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const ArrayType *T) { return true; }
364 static inline bool classof(const Type *T) {
365 return T->getPrimitiveID() == ArrayTyID;
367 static inline bool classof(const Value *V) {
368 return isa<Type>(V) && classof(cast<Type>(V));
374 class PointerType : public SequentialType {
375 friend class TypeMap<PointerValType, PointerType>;
376 PointerType(const PointerType &); // Do not implement
377 const PointerType &operator=(const PointerType &); // Do not implement
379 // This should really be private, but it squelches a bogus warning
380 // from GCC to make them protected: warning: `class PointerType' only
381 // defines private constructors and has no friends
383 // Private ctor - Only can be created by a static member...
384 PointerType(const Type *ElType);
386 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
387 // another (more concrete) type, we must eliminate all references to other
388 // types, to avoid some circular reference problems. This also removes the
389 // type from the internal tables of available types.
390 virtual void dropAllTypeUses(bool inMap);
392 /// PointerType::get - This is the only way to construct a new pointer type.
393 static PointerType *get(const Type *ElementType);
395 // Implement the AbstractTypeUser interface.
396 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
397 virtual void typeBecameConcrete(const DerivedType *AbsTy);
399 // Implement support type inquiry through isa, cast, and dyn_cast:
400 static inline bool classof(const PointerType *T) { return true; }
401 static inline bool classof(const Type *T) {
402 return T->getPrimitiveID() == PointerTyID;
404 static inline bool classof(const Value *V) {
405 return isa<Type>(V) && classof(cast<Type>(V));
410 class OpaqueType : public DerivedType {
411 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
412 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
414 // This should really be private, but it squelches a bogus warning
415 // from GCC to make them protected: warning: `class OpaqueType' only
416 // defines private constructors and has no friends
418 // Private ctor - Only can be created by a static member...
421 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
422 // another (more concrete) type, we must eliminate all references to other
423 // types, to avoid some circular reference problems.
424 virtual void dropAllTypeUses(bool inMap) {} // No type uses
427 // OpaqueType::get - Static factory method for the OpaqueType class...
428 static OpaqueType *get() {
429 return new OpaqueType(); // All opaque types are distinct
432 // Implement the AbstractTypeUser interface.
433 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
434 abort(); // FIXME: this is not really an AbstractTypeUser!
436 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
437 abort(); // FIXME: this is not really an AbstractTypeUser!
440 // Implement support for type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const OpaqueType *T) { return true; }
442 static inline bool classof(const Type *T) {
443 return T->getPrimitiveID() == OpaqueTyID;
445 static inline bool classof(const Value *V) {
446 return isa<Type>(V) && classof(cast<Type>(V));
451 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
452 // These are defined here because they MUST be inlined, yet are dependent on
453 // the definition of the Type class. Of course Type derives from Value, which
454 // contains an AbstractTypeUser instance, so there is no good way to factor out
455 // the code. Hence this bit of uglyness.
457 inline void PATypeHandle::addUser() {
458 assert(Ty && "Type Handle has a null type!");
459 if (Ty->isAbstract())
460 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
462 inline void PATypeHandle::removeUser() {
463 if (Ty->isAbstract())
464 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
467 inline void PATypeHandle::removeUserFromConcrete() {
468 if (!Ty->isAbstract())
469 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
472 // Define inline methods for PATypeHolder...
474 inline void PATypeHolder::addRef() {
475 if (Ty->isAbstract())
476 cast<DerivedType>(Ty)->addRef();
479 inline void PATypeHolder::dropRef() {
480 if (Ty->isAbstract())
481 cast<DerivedType>(Ty)->dropRef();
484 /// get - This implements the forwarding part of the union-find algorithm for
485 /// abstract types. Before every access to the Type*, we check to see if the
486 /// type we are pointing to is forwarding to a new type. If so, we drop our
487 /// reference to the type.
488 inline const Type* PATypeHolder::get() const {
489 const Type *NewTy = Ty->getForwardedType();
490 if (!NewTy) return Ty;
491 return *const_cast<PATypeHolder*>(this) = NewTy;