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"
24 template<class ValType, class TypeClass> class TypeMap;
25 class FunctionValType;
30 class DerivedType : public Type, public AbstractTypeUser {
31 /// RefCount - This counts the number of PATypeHolders that are pointing to
32 /// this type. When this number falls to zero, if the type is abstract and
33 /// has no AbstractTypeUsers, the type is deleted.
35 mutable unsigned RefCount;
37 // AbstractTypeUsers - Implement a list of the users that need to be notified
38 // if I am a type, and I get resolved into a more concrete type.
40 ///// FIXME: kill mutable nonsense when Type's are not const
41 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
44 DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {
47 assert(AbstractTypeUsers.empty());
50 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
51 /// that the current type has transitioned from being abstract to being
54 void notifyUsesThatTypeBecameConcrete();
56 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
57 // another (more concrete) type, we must eliminate all references to other
58 // types, to avoid some circular reference problems.
59 virtual void dropAllTypeUses() = 0;
63 //===--------------------------------------------------------------------===//
64 // Abstract Type handling methods - These types have special lifetimes, which
65 // are managed by (add|remove)AbstractTypeUser. See comments in
66 // AbstractTypeUser.h for more information.
68 // addAbstractTypeUser - Notify an abstract type that there is a new user of
69 // it. This function is called primarily by the PATypeHandle class.
71 void addAbstractTypeUser(AbstractTypeUser *U) const {
72 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
73 AbstractTypeUsers.push_back(U);
76 // removeAbstractTypeUser - Notify an abstract type that a user of the class
77 // no longer has a handle to the type. This function is called primarily by
78 // the PATypeHandle class. When there are no users of the abstract type, it
79 // is annihilated, because there is no way to get a reference to it ever
82 void removeAbstractTypeUser(AbstractTypeUser *U) const;
84 // refineAbstractTypeTo - This function is used to when it is discovered that
85 // the 'this' abstract type is actually equivalent to the NewType specified.
86 // This causes all users of 'this' to switch to reference the more concrete
87 // type NewType and for 'this' to be deleted.
89 void refineAbstractTypeTo(const Type *NewType);
92 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
96 void dropRef() const {
97 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
98 assert(RefCount && "No objects are currently referencing this object!");
100 // If this is the last PATypeHolder using this object, and there are no
101 // PATypeHandles using it, the type is dead, delete it now.
102 if (--RefCount == 0 && AbstractTypeUsers.empty())
107 void dump() const { Value::dump(); }
109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool classof(const DerivedType *T) { return true; }
111 static inline bool classof(const Type *T) {
112 return T->isDerivedType();
114 static inline bool classof(const Value *V) {
115 return isa<Type>(V) && classof(cast<Type>(V));
122 struct FunctionType : public DerivedType {
123 typedef std::vector<PATypeHandle> ParamTypes;
124 friend class TypeMap<FunctionValType, FunctionType>;
126 PATypeHandle ResultType;
130 FunctionType(const FunctionType &); // Do not implement
131 const FunctionType &operator=(const FunctionType &); // Do not implement
133 // This should really be private, but it squelches a bogus warning
134 // from GCC to make them protected: warning: `class FunctionType' only
135 // defines private constructors and has no friends
137 // Private ctor - Only can be created by a static member...
138 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
141 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
142 // another (more concrete) type, we must eliminate all references to other
143 // types, to avoid some circular reference problems.
144 virtual void dropAllTypeUses();
147 /// FunctionType::get - This static method is the primary way of constructing
149 static FunctionType *get(const Type *Result,
150 const std::vector<const Type*> &Params,
153 inline bool isVarArg() const { return isVarArgs; }
154 inline const Type *getReturnType() const { return ResultType; }
155 inline const ParamTypes &getParamTypes() const { return ParamTys; }
157 // Parameter type accessors...
158 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
160 // getNumParams - Return the number of fixed parameters this function type
161 // requires. This does not consider varargs.
163 unsigned getNumParams() const { return ParamTys.size(); }
166 virtual const Type *getContainedType(unsigned i) const {
167 return i == 0 ? ResultType.get() : ParamTys[i-1].get();
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.
239 virtual void dropAllTypeUses();
242 /// StructType::get - This static method is the primary way to create a
244 static StructType *get(const std::vector<const Type*> &Params);
246 inline const ElementTypes &getElementTypes() const { return ETypes; }
248 virtual const Type *getContainedType(unsigned i) const {
249 return ETypes[i].get();
251 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
253 // getTypeAtIndex - Given an index value into the type, return the type of the
254 // element. For a structure type, this must be a constant value...
256 virtual const Type *getTypeAtIndex(const Value *V) const ;
257 virtual bool indexValid(const Value *V) const;
259 // getIndexType - Return the type required of indices for this composite.
260 // For structures, this is ubyte, for arrays, this is uint
262 virtual const Type *getIndexType() const { return Type::UByteTy; }
264 // Implement the AbstractTypeUser interface.
265 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
266 virtual void typeBecameConcrete(const DerivedType *AbsTy);
268 // Methods for support type inquiry through isa, cast, and dyn_cast:
269 static inline bool classof(const StructType *T) { return true; }
270 static inline bool classof(const Type *T) {
271 return T->getPrimitiveID() == StructTyID;
273 static inline bool classof(const Value *V) {
274 return isa<Type>(V) && classof(cast<Type>(V));
279 // SequentialType - This is the superclass of the array and pointer type
280 // classes. Both of these represent "arrays" in memory. The array type
281 // represents a specifically sized array, pointer types are unsized/unknown size
282 // arrays. SequentialType holds the common features of both, which stem from
283 // the fact that both lay their components out in memory identically.
285 class SequentialType : public CompositeType {
286 SequentialType(const SequentialType &); // Do not implement!
287 const SequentialType &operator=(const SequentialType &); // Do not implement!
289 PATypeHandle ElementType;
291 SequentialType(PrimitiveID TID, const Type *ElType)
292 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
296 inline const Type *getElementType() const { return ElementType; }
298 virtual const Type *getContainedType(unsigned i) const {
299 return ElementType.get();
301 virtual unsigned getNumContainedTypes() const { return 1; }
303 // getTypeAtIndex - Given an index value into the type, return the type of the
304 // element. For sequential types, there is only one subtype...
306 virtual const Type *getTypeAtIndex(const Value *V) const {
307 return ElementType.get();
309 virtual bool indexValid(const Value *V) const {
310 return V->getType() == Type::LongTy; // Must be a 'long' index
313 // getIndexType() - Return the type required of indices for this composite.
314 // For structures, this is ubyte, for arrays, this is uint
316 virtual const Type *getIndexType() const { return Type::LongTy; }
318 // Methods for support type inquiry through isa, cast, and dyn_cast:
319 static inline bool classof(const SequentialType *T) { return true; }
320 static inline bool classof(const Type *T) {
321 return T->getPrimitiveID() == ArrayTyID ||
322 T->getPrimitiveID() == PointerTyID;
324 static inline bool classof(const Value *V) {
325 return isa<Type>(V) && classof(cast<Type>(V));
330 class ArrayType : public SequentialType {
331 friend class TypeMap<ArrayValType, ArrayType>;
332 unsigned NumElements;
334 ArrayType(const ArrayType &); // Do not implement
335 const ArrayType &operator=(const ArrayType &); // Do not implement
337 // This should really be private, but it squelches a bogus warning
338 // from GCC to make them protected: warning: `class ArrayType' only
339 // defines private constructors and has no friends
341 // Private ctor - Only can be created by a static member...
342 ArrayType(const Type *ElType, unsigned NumEl);
344 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
345 // another (more concrete) type, we must eliminate all references to other
346 // types, to avoid some circular reference problems.
347 virtual void dropAllTypeUses();
350 /// ArrayType::get - This static method is the primary way to construct an
352 static ArrayType *get(const Type *ElementType, unsigned NumElements);
354 inline unsigned getNumElements() const { return NumElements; }
356 // Implement the AbstractTypeUser interface.
357 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
358 virtual void typeBecameConcrete(const DerivedType *AbsTy);
360 // Methods for support type inquiry through isa, cast, and dyn_cast:
361 static inline bool classof(const ArrayType *T) { return true; }
362 static inline bool classof(const Type *T) {
363 return T->getPrimitiveID() == ArrayTyID;
365 static inline bool classof(const Value *V) {
366 return isa<Type>(V) && classof(cast<Type>(V));
372 class PointerType : public SequentialType {
373 friend class TypeMap<PointerValType, PointerType>;
374 PointerType(const PointerType &); // Do not implement
375 const PointerType &operator=(const PointerType &); // Do not implement
377 // This should really be private, but it squelches a bogus warning
378 // from GCC to make them protected: warning: `class PointerType' only
379 // defines private constructors and has no friends
381 // Private ctor - Only can be created by a static member...
382 PointerType(const Type *ElType);
384 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
385 // another (more concrete) type, we must eliminate all references to other
386 // types, to avoid some circular reference problems.
387 virtual void dropAllTypeUses();
389 /// PointerType::get - This is the only way to construct a new pointer type.
390 static PointerType *get(const Type *ElementType);
392 // Implement the AbstractTypeUser interface.
393 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
394 virtual void typeBecameConcrete(const DerivedType *AbsTy);
396 // Implement support type inquiry through isa, cast, and dyn_cast:
397 static inline bool classof(const PointerType *T) { return true; }
398 static inline bool classof(const Type *T) {
399 return T->getPrimitiveID() == PointerTyID;
401 static inline bool classof(const Value *V) {
402 return isa<Type>(V) && classof(cast<Type>(V));
407 class OpaqueType : public DerivedType {
408 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
409 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
411 // This should really be private, but it squelches a bogus warning
412 // from GCC to make them protected: warning: `class OpaqueType' only
413 // defines private constructors and has no friends
415 // Private ctor - Only can be created by a static member...
418 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
419 // another (more concrete) type, we must eliminate all references to other
420 // types, to avoid some circular reference problems.
421 virtual void dropAllTypeUses() {
422 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
426 // OpaqueType::get - Static factory method for the OpaqueType class...
427 static OpaqueType *get() {
428 return new OpaqueType(); // All opaque types are distinct
431 // Implement the AbstractTypeUser interface.
432 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
433 abort(); // FIXME: this is not really an AbstractTypeUser!
435 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
436 abort(); // FIXME: this is not really an AbstractTypeUser!
439 // Implement support for type inquiry through isa, cast, and dyn_cast:
440 static inline bool classof(const OpaqueType *T) { return true; }
441 static inline bool classof(const Type *T) {
442 return T->getPrimitiveID() == OpaqueTyID;
444 static inline bool classof(const Value *V) {
445 return isa<Type>(V) && classof(cast<Type>(V));
450 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
451 // These are defined here because they MUST be inlined, yet are dependent on
452 // the definition of the Type class. Of course Type derives from Value, which
453 // contains an AbstractTypeUser instance, so there is no good way to factor out
454 // the code. Hence this bit of uglyness.
456 inline void PATypeHandle::addUser() {
457 assert(Ty && "Type Handle has a null type!");
458 if (Ty->isAbstract())
459 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
461 inline void PATypeHandle::removeUser() {
462 if (Ty->isAbstract())
463 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
466 inline void PATypeHandle::removeUserFromConcrete() {
467 if (!Ty->isAbstract())
468 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
471 // Define inline methods for PATypeHolder...
473 inline void PATypeHolder::addRef() {
474 if (Ty->isAbstract())
475 cast<DerivedType>(Ty)->addRef();
478 inline void PATypeHolder::dropRef() {
479 if (Ty->isAbstract())
480 cast<DerivedType>(Ty)->dropRef();
483 /// get - This implements the forwarding part of the union-find algorithm for
484 /// abstract types. Before every access to the Type*, we check to see if the
485 /// type we are pointing to is forwarding to a new type. If so, we drop our
486 /// reference to the type.
487 inline const Type* PATypeHolder::get() const {
488 const Type *NewTy = Ty->getForwardedType();
489 if (!NewTy) return Ty;
490 return *const_cast<PATypeHolder*>(this) = NewTy;