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 // Methods for support type inquiry through isa, cast, and dyn_cast:
202 static inline bool classof(const CompositeType *T) { return true; }
203 static inline bool classof(const Type *T) {
204 return T->getPrimitiveID() == ArrayTyID ||
205 T->getPrimitiveID() == StructTyID ||
206 T->getPrimitiveID() == PointerTyID;
208 static inline bool classof(const Value *V) {
209 return isa<Type>(V) && classof(cast<Type>(V));
214 struct StructType : public CompositeType {
215 friend class TypeMap<StructValType, StructType>;
216 typedef std::vector<PATypeHandle> ElementTypes;
219 ElementTypes ETypes; // Element types of struct
221 StructType(const StructType &); // Do not implement
222 const StructType &operator=(const StructType &); // Do not implement
225 // This should really be private, but it squelches a bogus warning
226 // from GCC to make them protected: warning: `class StructType' only
227 // defines private constructors and has no friends
229 // Private ctor - Only can be created by a static member...
230 StructType(const std::vector<const Type*> &Types);
232 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
233 // another (more concrete) type, we must eliminate all references to other
234 // types, to avoid some circular reference problems.
235 virtual void dropAllTypeUses();
238 /// StructType::get - This static method is the primary way to create a
240 static StructType *get(const std::vector<const Type*> &Params);
242 inline const ElementTypes &getElementTypes() const { return ETypes; }
244 virtual const Type *getContainedType(unsigned i) const {
245 return ETypes[i].get();
247 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
249 // getTypeAtIndex - Given an index value into the type, return the type of the
250 // element. For a structure type, this must be a constant value...
252 virtual const Type *getTypeAtIndex(const Value *V) const ;
253 virtual bool indexValid(const Value *V) const;
255 // Implement the AbstractTypeUser interface.
256 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
257 virtual void typeBecameConcrete(const DerivedType *AbsTy);
259 // Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const StructType *T) { return true; }
261 static inline bool classof(const Type *T) {
262 return T->getPrimitiveID() == StructTyID;
264 static inline bool classof(const Value *V) {
265 return isa<Type>(V) && classof(cast<Type>(V));
270 // SequentialType - This is the superclass of the array and pointer type
271 // classes. Both of these represent "arrays" in memory. The array type
272 // represents a specifically sized array, pointer types are unsized/unknown size
273 // arrays. SequentialType holds the common features of both, which stem from
274 // the fact that both lay their components out in memory identically.
276 class SequentialType : public CompositeType {
277 SequentialType(const SequentialType &); // Do not implement!
278 const SequentialType &operator=(const SequentialType &); // Do not implement!
280 PATypeHandle ElementType;
282 SequentialType(PrimitiveID TID, const Type *ElType)
283 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
287 inline const Type *getElementType() const { return ElementType; }
289 virtual const Type *getContainedType(unsigned i) const {
290 return ElementType.get();
292 virtual unsigned getNumContainedTypes() const { return 1; }
294 // getTypeAtIndex - Given an index value into the type, return the type of the
295 // element. For sequential types, there is only one subtype...
297 virtual const Type *getTypeAtIndex(const Value *V) const {
298 return ElementType.get();
300 virtual bool indexValid(const Value *V) const {
301 return V->getType()->isInteger();
304 // Methods for support type inquiry through isa, cast, and dyn_cast:
305 static inline bool classof(const SequentialType *T) { return true; }
306 static inline bool classof(const Type *T) {
307 return T->getPrimitiveID() == ArrayTyID ||
308 T->getPrimitiveID() == PointerTyID;
310 static inline bool classof(const Value *V) {
311 return isa<Type>(V) && classof(cast<Type>(V));
316 class ArrayType : public SequentialType {
317 friend class TypeMap<ArrayValType, ArrayType>;
318 unsigned NumElements;
320 ArrayType(const ArrayType &); // Do not implement
321 const ArrayType &operator=(const ArrayType &); // Do not implement
323 // This should really be private, but it squelches a bogus warning
324 // from GCC to make them protected: warning: `class ArrayType' only
325 // defines private constructors and has no friends
327 // Private ctor - Only can be created by a static member...
328 ArrayType(const Type *ElType, unsigned NumEl);
330 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
331 // another (more concrete) type, we must eliminate all references to other
332 // types, to avoid some circular reference problems.
333 virtual void dropAllTypeUses();
336 /// ArrayType::get - This static method is the primary way to construct an
338 static ArrayType *get(const Type *ElementType, unsigned NumElements);
340 inline unsigned getNumElements() const { return NumElements; }
342 // Implement the AbstractTypeUser interface.
343 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
344 virtual void typeBecameConcrete(const DerivedType *AbsTy);
346 // Methods for support type inquiry through isa, cast, and dyn_cast:
347 static inline bool classof(const ArrayType *T) { return true; }
348 static inline bool classof(const Type *T) {
349 return T->getPrimitiveID() == ArrayTyID;
351 static inline bool classof(const Value *V) {
352 return isa<Type>(V) && classof(cast<Type>(V));
358 class PointerType : public SequentialType {
359 friend class TypeMap<PointerValType, PointerType>;
360 PointerType(const PointerType &); // Do not implement
361 const PointerType &operator=(const PointerType &); // Do not implement
363 // This should really be private, but it squelches a bogus warning
364 // from GCC to make them protected: warning: `class PointerType' only
365 // defines private constructors and has no friends
367 // Private ctor - Only can be created by a static member...
368 PointerType(const Type *ElType);
370 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
371 // another (more concrete) type, we must eliminate all references to other
372 // types, to avoid some circular reference problems.
373 virtual void dropAllTypeUses();
375 /// PointerType::get - This is the only way to construct a new pointer type.
376 static PointerType *get(const Type *ElementType);
378 // Implement the AbstractTypeUser interface.
379 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
380 virtual void typeBecameConcrete(const DerivedType *AbsTy);
382 // Implement support type inquiry through isa, cast, and dyn_cast:
383 static inline bool classof(const PointerType *T) { return true; }
384 static inline bool classof(const Type *T) {
385 return T->getPrimitiveID() == PointerTyID;
387 static inline bool classof(const Value *V) {
388 return isa<Type>(V) && classof(cast<Type>(V));
393 class OpaqueType : public DerivedType {
394 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
395 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
397 // This should really be private, but it squelches a bogus warning
398 // from GCC to make them protected: warning: `class OpaqueType' only
399 // defines private constructors and has no friends
401 // Private ctor - Only can be created by a static member...
404 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
405 // another (more concrete) type, we must eliminate all references to other
406 // types, to avoid some circular reference problems.
407 virtual void dropAllTypeUses() {
408 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
412 // OpaqueType::get - Static factory method for the OpaqueType class...
413 static OpaqueType *get() {
414 return new OpaqueType(); // All opaque types are distinct
417 // Implement the AbstractTypeUser interface.
418 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
419 abort(); // FIXME: this is not really an AbstractTypeUser!
421 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
422 abort(); // FIXME: this is not really an AbstractTypeUser!
425 // Implement support for type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const OpaqueType *T) { return true; }
427 static inline bool classof(const Type *T) {
428 return T->getPrimitiveID() == OpaqueTyID;
430 static inline bool classof(const Value *V) {
431 return isa<Type>(V) && classof(cast<Type>(V));
436 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
437 // These are defined here because they MUST be inlined, yet are dependent on
438 // the definition of the Type class. Of course Type derives from Value, which
439 // contains an AbstractTypeUser instance, so there is no good way to factor out
440 // the code. Hence this bit of uglyness.
442 inline void PATypeHandle::addUser() {
443 assert(Ty && "Type Handle has a null type!");
444 if (Ty->isAbstract())
445 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
447 inline void PATypeHandle::removeUser() {
448 if (Ty->isAbstract())
449 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
452 inline void PATypeHandle::removeUserFromConcrete() {
453 if (!Ty->isAbstract())
454 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
457 // Define inline methods for PATypeHolder...
459 inline void PATypeHolder::addRef() {
460 if (Ty->isAbstract())
461 cast<DerivedType>(Ty)->addRef();
464 inline void PATypeHolder::dropRef() {
465 if (Ty->isAbstract())
466 cast<DerivedType>(Ty)->dropRef();
469 /// get - This implements the forwarding part of the union-find algorithm for
470 /// abstract types. Before every access to the Type*, we check to see if the
471 /// type we are pointing to is forwarding to a new type. If so, we drop our
472 /// reference to the type.
473 inline const Type* PATypeHolder::get() const {
474 const Type *NewTy = Ty->getForwardedType();
475 if (!NewTy) return Ty;
476 return *const_cast<PATypeHolder*>(this) = NewTy;
479 } // End llvm namespace