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;
32 class DerivedType : public Type, public AbstractTypeUser {
33 /// RefCount - This counts the number of PATypeHolders that are pointing to
34 /// this type. When this number falls to zero, if the type is abstract and
35 /// has no AbstractTypeUsers, the type is deleted.
37 mutable unsigned RefCount;
39 // AbstractTypeUsers - Implement a list of the users that need to be notified
40 // if I am a type, and I get resolved into a more concrete type.
42 ///// FIXME: kill mutable nonsense when Type's are not const
43 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
46 DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {
49 assert(AbstractTypeUsers.empty());
52 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
53 /// that the current type has transitioned from being abstract to being
56 void notifyUsesThatTypeBecameConcrete();
58 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
59 // another (more concrete) type, we must eliminate all references to other
60 // types, to avoid some circular reference problems.
61 virtual void dropAllTypeUses() = 0;
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);
94 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
98 void dropRef() const {
99 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
100 assert(RefCount && "No objects are currently referencing this object!");
102 // If this is the last PATypeHolder using this object, and there are no
103 // PATypeHandles using it, the type is dead, delete it now.
104 if (--RefCount == 0 && AbstractTypeUsers.empty())
109 void dump() const { Value::dump(); }
111 // Methods for support type inquiry through isa, cast, and dyn_cast:
112 static inline bool classof(const DerivedType *T) { return true; }
113 static inline bool classof(const Type *T) {
114 return T->isDerivedType();
116 static inline bool classof(const Value *V) {
117 return isa<Type>(V) && classof(cast<Type>(V));
124 struct FunctionType : public DerivedType {
125 typedef std::vector<PATypeHandle> ParamTypes;
126 friend class TypeMap<FunctionValType, FunctionType>;
128 PATypeHandle ResultType;
132 FunctionType(const FunctionType &); // Do not implement
133 const FunctionType &operator=(const FunctionType &); // Do not implement
135 // This should really be private, but it squelches a bogus warning
136 // from GCC to make them protected: warning: `class FunctionType' only
137 // defines private constructors and has no friends
139 // Private ctor - Only can be created by a static member...
140 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
143 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
144 // another (more concrete) type, we must eliminate all references to other
145 // types, to avoid some circular reference problems.
146 virtual void dropAllTypeUses();
149 /// FunctionType::get - This static method is the primary way of constructing
151 static FunctionType *get(const Type *Result,
152 const std::vector<const Type*> &Params,
155 inline bool isVarArg() const { return isVarArgs; }
156 inline const Type *getReturnType() const { return ResultType; }
157 inline const ParamTypes &getParamTypes() const { return ParamTys; }
159 // Parameter type accessors...
160 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
162 // getNumParams - Return the number of fixed parameters this function type
163 // requires. This does not consider varargs.
165 unsigned getNumParams() const { return ParamTys.size(); }
168 virtual const Type *getContainedType(unsigned i) const {
169 return i == 0 ? ResultType.get() : ParamTys[i-1].get();
171 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
173 // Implement the AbstractTypeUser interface.
174 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
175 virtual void typeBecameConcrete(const DerivedType *AbsTy);
177 // Methods for support type inquiry through isa, cast, and dyn_cast:
178 static inline bool classof(const FunctionType *T) { return true; }
179 static inline bool classof(const Type *T) {
180 return T->getPrimitiveID() == FunctionTyID;
182 static inline bool classof(const Value *V) {
183 return isa<Type>(V) && classof(cast<Type>(V));
188 // CompositeType - Common super class of ArrayType, StructType, and PointerType
190 class CompositeType : public DerivedType {
192 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
195 // getTypeAtIndex - Given an index value into the type, return the type of the
198 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
199 virtual bool indexValid(const Value *V) const = 0;
201 // getIndexType - Return the type required of indices for this composite.
202 // For structures, this is ubyte, for arrays, this is uint
204 virtual const Type *getIndexType() const = 0;
207 // Methods for support type inquiry through isa, cast, and dyn_cast:
208 static inline bool classof(const CompositeType *T) { return true; }
209 static inline bool classof(const Type *T) {
210 return T->getPrimitiveID() == ArrayTyID ||
211 T->getPrimitiveID() == StructTyID ||
212 T->getPrimitiveID() == PointerTyID;
214 static inline bool classof(const Value *V) {
215 return isa<Type>(V) && classof(cast<Type>(V));
220 struct StructType : public CompositeType {
221 friend class TypeMap<StructValType, StructType>;
222 typedef std::vector<PATypeHandle> ElementTypes;
225 ElementTypes ETypes; // Element types of struct
227 StructType(const StructType &); // Do not implement
228 const StructType &operator=(const StructType &); // Do not implement
231 // This should really be private, but it squelches a bogus warning
232 // from GCC to make them protected: warning: `class StructType' only
233 // defines private constructors and has no friends
235 // Private ctor - Only can be created by a static member...
236 StructType(const std::vector<const Type*> &Types);
238 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
239 // another (more concrete) type, we must eliminate all references to other
240 // types, to avoid some circular reference problems.
241 virtual void dropAllTypeUses();
244 /// StructType::get - This static method is the primary way to create a
246 static StructType *get(const std::vector<const Type*> &Params);
248 inline const ElementTypes &getElementTypes() const { return ETypes; }
250 virtual const Type *getContainedType(unsigned i) const {
251 return ETypes[i].get();
253 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
255 // getTypeAtIndex - Given an index value into the type, return the type of the
256 // element. For a structure type, this must be a constant value...
258 virtual const Type *getTypeAtIndex(const Value *V) const ;
259 virtual bool indexValid(const Value *V) const;
261 // getIndexType - Return the type required of indices for this composite.
262 // For structures, this is ubyte, for arrays, this is uint
264 virtual const Type *getIndexType() const { return Type::UByteTy; }
266 // Implement the AbstractTypeUser interface.
267 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
268 virtual void typeBecameConcrete(const DerivedType *AbsTy);
270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool classof(const StructType *T) { return true; }
272 static inline bool classof(const Type *T) {
273 return T->getPrimitiveID() == StructTyID;
275 static inline bool classof(const Value *V) {
276 return isa<Type>(V) && classof(cast<Type>(V));
281 // SequentialType - This is the superclass of the array and pointer type
282 // classes. Both of these represent "arrays" in memory. The array type
283 // represents a specifically sized array, pointer types are unsized/unknown size
284 // arrays. SequentialType holds the common features of both, which stem from
285 // the fact that both lay their components out in memory identically.
287 class SequentialType : public CompositeType {
288 SequentialType(const SequentialType &); // Do not implement!
289 const SequentialType &operator=(const SequentialType &); // Do not implement!
291 PATypeHandle ElementType;
293 SequentialType(PrimitiveID TID, const Type *ElType)
294 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
298 inline const Type *getElementType() const { return ElementType; }
300 virtual const Type *getContainedType(unsigned i) const {
301 return ElementType.get();
303 virtual unsigned getNumContainedTypes() const { return 1; }
305 // getTypeAtIndex - Given an index value into the type, return the type of the
306 // element. For sequential types, there is only one subtype...
308 virtual const Type *getTypeAtIndex(const Value *V) const {
309 return ElementType.get();
311 virtual bool indexValid(const Value *V) const {
312 return V->getType() == Type::LongTy; // Must be a 'long' index
315 // getIndexType() - Return the type required of indices for this composite.
316 // For structures, this is ubyte, for arrays, this is uint
318 virtual const Type *getIndexType() const { return Type::LongTy; }
320 // Methods for support type inquiry through isa, cast, and dyn_cast:
321 static inline bool classof(const SequentialType *T) { return true; }
322 static inline bool classof(const Type *T) {
323 return T->getPrimitiveID() == ArrayTyID ||
324 T->getPrimitiveID() == PointerTyID;
326 static inline bool classof(const Value *V) {
327 return isa<Type>(V) && classof(cast<Type>(V));
332 class ArrayType : public SequentialType {
333 friend class TypeMap<ArrayValType, ArrayType>;
334 unsigned NumElements;
336 ArrayType(const ArrayType &); // Do not implement
337 const ArrayType &operator=(const ArrayType &); // Do not implement
339 // This should really be private, but it squelches a bogus warning
340 // from GCC to make them protected: warning: `class ArrayType' only
341 // defines private constructors and has no friends
343 // Private ctor - Only can be created by a static member...
344 ArrayType(const Type *ElType, unsigned NumEl);
346 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
347 // another (more concrete) type, we must eliminate all references to other
348 // types, to avoid some circular reference problems.
349 virtual void dropAllTypeUses();
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.
389 virtual void dropAllTypeUses();
391 /// PointerType::get - This is the only way to construct a new pointer type.
392 static PointerType *get(const Type *ElementType);
394 // Implement the AbstractTypeUser interface.
395 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
396 virtual void typeBecameConcrete(const DerivedType *AbsTy);
398 // Implement support type inquiry through isa, cast, and dyn_cast:
399 static inline bool classof(const PointerType *T) { return true; }
400 static inline bool classof(const Type *T) {
401 return T->getPrimitiveID() == PointerTyID;
403 static inline bool classof(const Value *V) {
404 return isa<Type>(V) && classof(cast<Type>(V));
409 class OpaqueType : public DerivedType {
410 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
411 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
413 // This should really be private, but it squelches a bogus warning
414 // from GCC to make them protected: warning: `class OpaqueType' only
415 // defines private constructors and has no friends
417 // Private ctor - Only can be created by a static member...
420 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
421 // another (more concrete) type, we must eliminate all references to other
422 // types, to avoid some circular reference problems.
423 virtual void dropAllTypeUses() {
424 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
428 // OpaqueType::get - Static factory method for the OpaqueType class...
429 static OpaqueType *get() {
430 return new OpaqueType(); // All opaque types are distinct
433 // Implement the AbstractTypeUser interface.
434 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
435 abort(); // FIXME: this is not really an AbstractTypeUser!
437 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
438 abort(); // FIXME: this is not really an AbstractTypeUser!
441 // Implement support for type inquiry through isa, cast, and dyn_cast:
442 static inline bool classof(const OpaqueType *T) { return true; }
443 static inline bool classof(const Type *T) {
444 return T->getPrimitiveID() == OpaqueTyID;
446 static inline bool classof(const Value *V) {
447 return isa<Type>(V) && classof(cast<Type>(V));
452 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
453 // These are defined here because they MUST be inlined, yet are dependent on
454 // the definition of the Type class. Of course Type derives from Value, which
455 // contains an AbstractTypeUser instance, so there is no good way to factor out
456 // the code. Hence this bit of uglyness.
458 inline void PATypeHandle::addUser() {
459 assert(Ty && "Type Handle has a null type!");
460 if (Ty->isAbstract())
461 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
463 inline void PATypeHandle::removeUser() {
464 if (Ty->isAbstract())
465 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
468 inline void PATypeHandle::removeUserFromConcrete() {
469 if (!Ty->isAbstract())
470 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
473 // Define inline methods for PATypeHolder...
475 inline void PATypeHolder::addRef() {
476 if (Ty->isAbstract())
477 cast<DerivedType>(Ty)->addRef();
480 inline void PATypeHolder::dropRef() {
481 if (Ty->isAbstract())
482 cast<DerivedType>(Ty)->dropRef();
485 /// get - This implements the forwarding part of the union-find algorithm for
486 /// abstract types. Before every access to the Type*, we check to see if the
487 /// type we are pointing to is forwarding to a new type. If so, we drop our
488 /// reference to the type.
489 inline const Type* PATypeHolder::get() const {
490 const Type *NewTy = Ty->getForwardedType();
491 if (!NewTy) return Ty;
492 return *const_cast<PATypeHolder*>(this) = NewTy;
495 } // End llvm namespace