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"
17 template<class ValType, class TypeClass> class TypeMap;
18 class FunctionValType;
23 class DerivedType : public Type, public AbstractTypeUser {
24 /// RefCount - This counts the number of PATypeHolders that are pointing to
25 /// this type. When this number falls to zero, if the type is abstract and
26 /// has no AbstractTypeUsers, the type is deleted.
28 mutable unsigned RefCount;
30 // AbstractTypeUsers - Implement a list of the users that need to be notified
31 // if I am a type, and I get resolved into a more concrete type.
33 ///// FIXME: kill mutable nonsense when Type's are not const
34 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
37 DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {
40 assert(AbstractTypeUsers.empty());
43 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
44 /// that the current type has transitioned from being abstract to being
47 void notifyUsesThatTypeBecameConcrete();
49 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
50 // another (more concrete) type, we must eliminate all references to other
51 // types, to avoid some circular reference problems.
52 virtual void dropAllTypeUses() = 0;
56 //===--------------------------------------------------------------------===//
57 // Abstract Type handling methods - These types have special lifetimes, which
58 // are managed by (add|remove)AbstractTypeUser. See comments in
59 // AbstractTypeUser.h for more information.
61 // addAbstractTypeUser - Notify an abstract type that there is a new user of
62 // it. This function is called primarily by the PATypeHandle class.
64 void addAbstractTypeUser(AbstractTypeUser *U) const {
65 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
66 AbstractTypeUsers.push_back(U);
69 // removeAbstractTypeUser - Notify an abstract type that a user of the class
70 // no longer has a handle to the type. This function is called primarily by
71 // the PATypeHandle class. When there are no users of the abstract type, it
72 // is annihilated, because there is no way to get a reference to it ever
75 void removeAbstractTypeUser(AbstractTypeUser *U) const;
77 // refineAbstractTypeTo - This function is used to when it is discovered that
78 // the 'this' abstract type is actually equivalent to the NewType specified.
79 // This causes all users of 'this' to switch to reference the more concrete
80 // type NewType and for 'this' to be deleted.
82 void refineAbstractTypeTo(const Type *NewType);
85 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
89 void dropRef() const {
90 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
91 assert(RefCount && "No objects are currently referencing this object!");
93 // If this is the last PATypeHolder using this object, and there are no
94 // PATypeHandles using it, the type is dead, delete it now.
95 if (--RefCount == 0 && AbstractTypeUsers.empty())
100 void dump() const { Value::dump(); }
102 // Methods for support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const DerivedType *T) { return true; }
104 static inline bool classof(const Type *T) {
105 return T->isDerivedType();
107 static inline bool classof(const Value *V) {
108 return isa<Type>(V) && classof(cast<Type>(V));
115 struct FunctionType : public DerivedType {
116 typedef std::vector<PATypeHandle> ParamTypes;
117 friend class TypeMap<FunctionValType, FunctionType>;
119 PATypeHandle ResultType;
123 FunctionType(const FunctionType &); // Do not implement
124 const FunctionType &operator=(const FunctionType &); // Do not implement
126 // This should really be private, but it squelches a bogus warning
127 // from GCC to make them protected: warning: `class FunctionType' only
128 // defines private constructors and has no friends
130 // Private ctor - Only can be created by a static member...
131 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
134 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
135 // another (more concrete) type, we must eliminate all references to other
136 // types, to avoid some circular reference problems.
137 virtual void dropAllTypeUses();
140 /// FunctionType::get - This static method is the primary way of constructing
142 static FunctionType *get(const Type *Result,
143 const std::vector<const Type*> &Params,
146 inline bool isVarArg() const { return isVarArgs; }
147 inline const Type *getReturnType() const { return ResultType; }
148 inline const ParamTypes &getParamTypes() const { return ParamTys; }
150 // Parameter type accessors...
151 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
153 // getNumParams - Return the number of fixed parameters this function type
154 // requires. This does not consider varargs.
156 unsigned getNumParams() const { return ParamTys.size(); }
159 virtual const Type *getContainedType(unsigned i) const {
160 return i == 0 ? ResultType.get() : ParamTys[i-1].get();
162 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
164 // Implement the AbstractTypeUser interface.
165 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
166 virtual void typeBecameConcrete(const DerivedType *AbsTy);
168 // Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const FunctionType *T) { return true; }
170 static inline bool classof(const Type *T) {
171 return T->getPrimitiveID() == FunctionTyID;
173 static inline bool classof(const Value *V) {
174 return isa<Type>(V) && classof(cast<Type>(V));
179 // CompositeType - Common super class of ArrayType, StructType, and PointerType
181 class CompositeType : public DerivedType {
183 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
186 // getTypeAtIndex - Given an index value into the type, return the type of the
189 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
190 virtual bool indexValid(const Value *V) const = 0;
192 // getIndexType - Return the type required of indices for this composite.
193 // For structures, this is ubyte, for arrays, this is uint
195 virtual const Type *getIndexType() const = 0;
198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static inline bool classof(const CompositeType *T) { return true; }
200 static inline bool classof(const Type *T) {
201 return T->getPrimitiveID() == ArrayTyID ||
202 T->getPrimitiveID() == StructTyID ||
203 T->getPrimitiveID() == PointerTyID;
205 static inline bool classof(const Value *V) {
206 return isa<Type>(V) && classof(cast<Type>(V));
211 struct StructType : public CompositeType {
212 friend class TypeMap<StructValType, StructType>;
213 typedef std::vector<PATypeHandle> ElementTypes;
216 ElementTypes ETypes; // Element types of struct
218 StructType(const StructType &); // Do not implement
219 const StructType &operator=(const StructType &); // Do not implement
222 // This should really be private, but it squelches a bogus warning
223 // from GCC to make them protected: warning: `class StructType' only
224 // defines private constructors and has no friends
226 // Private ctor - Only can be created by a static member...
227 StructType(const std::vector<const Type*> &Types);
229 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
230 // another (more concrete) type, we must eliminate all references to other
231 // types, to avoid some circular reference problems.
232 virtual void dropAllTypeUses();
235 /// StructType::get - This static method is the primary way to create a
237 static StructType *get(const std::vector<const Type*> &Params);
239 inline const ElementTypes &getElementTypes() const { return ETypes; }
241 virtual const Type *getContainedType(unsigned i) const {
242 return ETypes[i].get();
244 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
246 // getTypeAtIndex - Given an index value into the type, return the type of the
247 // element. For a structure type, this must be a constant value...
249 virtual const Type *getTypeAtIndex(const Value *V) const ;
250 virtual bool indexValid(const Value *V) const;
252 // getIndexType - Return the type required of indices for this composite.
253 // For structures, this is ubyte, for arrays, this is uint
255 virtual const Type *getIndexType() const { return Type::UByteTy; }
257 // Implement the AbstractTypeUser interface.
258 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
259 virtual void typeBecameConcrete(const DerivedType *AbsTy);
261 // Methods for support type inquiry through isa, cast, and dyn_cast:
262 static inline bool classof(const StructType *T) { return true; }
263 static inline bool classof(const Type *T) {
264 return T->getPrimitiveID() == StructTyID;
266 static inline bool classof(const Value *V) {
267 return isa<Type>(V) && classof(cast<Type>(V));
272 // SequentialType - This is the superclass of the array and pointer type
273 // classes. Both of these represent "arrays" in memory. The array type
274 // represents a specifically sized array, pointer types are unsized/unknown size
275 // arrays. SequentialType holds the common features of both, which stem from
276 // the fact that both lay their components out in memory identically.
278 class SequentialType : public CompositeType {
279 SequentialType(const SequentialType &); // Do not implement!
280 const SequentialType &operator=(const SequentialType &); // Do not implement!
282 PATypeHandle ElementType;
284 SequentialType(PrimitiveID TID, const Type *ElType)
285 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
289 inline const Type *getElementType() const { return ElementType; }
291 virtual const Type *getContainedType(unsigned i) const {
292 return ElementType.get();
294 virtual unsigned getNumContainedTypes() const { return 1; }
296 // getTypeAtIndex - Given an index value into the type, return the type of the
297 // element. For sequential types, there is only one subtype...
299 virtual const Type *getTypeAtIndex(const Value *V) const {
300 return ElementType.get();
302 virtual bool indexValid(const Value *V) const {
303 return V->getType() == Type::LongTy; // Must be a 'long' index
306 // getIndexType() - Return the type required of indices for this composite.
307 // For structures, this is ubyte, for arrays, this is uint
309 virtual const Type *getIndexType() const { return Type::LongTy; }
311 // Methods for support type inquiry through isa, cast, and dyn_cast:
312 static inline bool classof(const SequentialType *T) { return true; }
313 static inline bool classof(const Type *T) {
314 return T->getPrimitiveID() == ArrayTyID ||
315 T->getPrimitiveID() == PointerTyID;
317 static inline bool classof(const Value *V) {
318 return isa<Type>(V) && classof(cast<Type>(V));
323 class ArrayType : public SequentialType {
324 friend class TypeMap<ArrayValType, ArrayType>;
325 unsigned NumElements;
327 ArrayType(const ArrayType &); // Do not implement
328 const ArrayType &operator=(const ArrayType &); // Do not implement
330 // This should really be private, but it squelches a bogus warning
331 // from GCC to make them protected: warning: `class ArrayType' only
332 // defines private constructors and has no friends
334 // Private ctor - Only can be created by a static member...
335 ArrayType(const Type *ElType, unsigned NumEl);
337 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
338 // another (more concrete) type, we must eliminate all references to other
339 // types, to avoid some circular reference problems.
340 virtual void dropAllTypeUses();
343 /// ArrayType::get - This static method is the primary way to construct an
345 static ArrayType *get(const Type *ElementType, unsigned NumElements);
347 inline unsigned getNumElements() const { return NumElements; }
349 // Implement the AbstractTypeUser interface.
350 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
351 virtual void typeBecameConcrete(const DerivedType *AbsTy);
353 // Methods for support type inquiry through isa, cast, and dyn_cast:
354 static inline bool classof(const ArrayType *T) { return true; }
355 static inline bool classof(const Type *T) {
356 return T->getPrimitiveID() == ArrayTyID;
358 static inline bool classof(const Value *V) {
359 return isa<Type>(V) && classof(cast<Type>(V));
365 class PointerType : public SequentialType {
366 friend class TypeMap<PointerValType, PointerType>;
367 PointerType(const PointerType &); // Do not implement
368 const PointerType &operator=(const PointerType &); // Do not implement
370 // This should really be private, but it squelches a bogus warning
371 // from GCC to make them protected: warning: `class PointerType' only
372 // defines private constructors and has no friends
374 // Private ctor - Only can be created by a static member...
375 PointerType(const Type *ElType);
377 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
378 // another (more concrete) type, we must eliminate all references to other
379 // types, to avoid some circular reference problems.
380 virtual void dropAllTypeUses();
382 /// PointerType::get - This is the only way to construct a new pointer type.
383 static PointerType *get(const Type *ElementType);
385 // Implement the AbstractTypeUser interface.
386 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
387 virtual void typeBecameConcrete(const DerivedType *AbsTy);
389 // Implement support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const PointerType *T) { return true; }
391 static inline bool classof(const Type *T) {
392 return T->getPrimitiveID() == PointerTyID;
394 static inline bool classof(const Value *V) {
395 return isa<Type>(V) && classof(cast<Type>(V));
400 class OpaqueType : public DerivedType {
401 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
402 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
404 // This should really be private, but it squelches a bogus warning
405 // from GCC to make them protected: warning: `class OpaqueType' only
406 // defines private constructors and has no friends
408 // Private ctor - Only can be created by a static member...
411 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
412 // another (more concrete) type, we must eliminate all references to other
413 // types, to avoid some circular reference problems.
414 virtual void dropAllTypeUses() {
415 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
419 // OpaqueType::get - Static factory method for the OpaqueType class...
420 static OpaqueType *get() {
421 return new OpaqueType(); // All opaque types are distinct
424 // Implement the AbstractTypeUser interface.
425 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
426 abort(); // FIXME: this is not really an AbstractTypeUser!
428 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
429 abort(); // FIXME: this is not really an AbstractTypeUser!
432 // Implement support for type inquiry through isa, cast, and dyn_cast:
433 static inline bool classof(const OpaqueType *T) { return true; }
434 static inline bool classof(const Type *T) {
435 return T->getPrimitiveID() == OpaqueTyID;
437 static inline bool classof(const Value *V) {
438 return isa<Type>(V) && classof(cast<Type>(V));
443 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
444 // These are defined here because they MUST be inlined, yet are dependent on
445 // the definition of the Type class. Of course Type derives from Value, which
446 // contains an AbstractTypeUser instance, so there is no good way to factor out
447 // the code. Hence this bit of uglyness.
449 inline void PATypeHandle::addUser() {
450 assert(Ty && "Type Handle has a null type!");
451 if (Ty->isAbstract())
452 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
454 inline void PATypeHandle::removeUser() {
455 if (Ty->isAbstract())
456 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
459 inline void PATypeHandle::removeUserFromConcrete() {
460 if (!Ty->isAbstract())
461 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
464 // Define inline methods for PATypeHolder...
466 inline void PATypeHolder::addRef() {
467 if (Ty->isAbstract())
468 cast<DerivedType>(Ty)->addRef();
471 inline void PATypeHolder::dropRef() {
472 if (Ty->isAbstract())
473 cast<DerivedType>(Ty)->dropRef();
476 /// get - This implements the forwarding part of the union-find algorithm for
477 /// abstract types. Before every access to the Type*, we check to see if the
478 /// type we are pointing to is forwarding to a new type. If so, we drop our
479 /// reference to the type.
480 inline const Type* PATypeHolder::get() const {
481 const Type *NewTy = Ty->getForwardedType();
482 if (!NewTy) return Ty;
483 return *const_cast<PATypeHolder*>(this) = NewTy;