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 Types are not const
43 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
46 DerivedType(PrimitiveID id) : Type("", id), RefCount(0) {}
48 assert(AbstractTypeUsers.empty());
51 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
52 /// that the current type has transitioned from being abstract to being
55 void notifyUsesThatTypeBecameConcrete();
57 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
58 // another (more concrete) type, we must eliminate all references to other
59 // types, to avoid some circular reference problems.
60 virtual void dropAllTypeUses() = 0;
64 //===--------------------------------------------------------------------===//
65 // Abstract Type handling methods - These types have special lifetimes, which
66 // are managed by (add|remove)AbstractTypeUser. See comments in
67 // AbstractTypeUser.h for more information.
69 // addAbstractTypeUser - Notify an abstract type that there is a new user of
70 // it. This function is called primarily by the PATypeHandle class.
72 void addAbstractTypeUser(AbstractTypeUser *U) const {
73 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
74 AbstractTypeUsers.push_back(U);
77 // removeAbstractTypeUser - Notify an abstract type that a user of the class
78 // no longer has a handle to the type. This function is called primarily by
79 // the PATypeHandle class. When there are no users of the abstract type, it
80 // is annihilated, because there is no way to get a reference to it ever
83 void removeAbstractTypeUser(AbstractTypeUser *U) const;
85 // refineAbstractTypeTo - This function is used to when it is discovered that
86 // the 'this' abstract type is actually equivalent to the NewType specified.
87 // This causes all users of 'this' to switch to reference the more concrete
88 // type NewType and for 'this' to be deleted.
90 void refineAbstractTypeTo(const Type *NewType);
93 assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
97 void dropRef() const {
98 assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
99 assert(RefCount && "No objects are currently referencing this object!");
101 // If this is the last PATypeHolder using this object, and there are no
102 // PATypeHandles using it, the type is dead, delete it now.
103 if (--RefCount == 0 && AbstractTypeUsers.empty())
108 void dump() const { Value::dump(); }
110 // Methods for support type inquiry through isa, cast, and dyn_cast:
111 static inline bool classof(const DerivedType *T) { return true; }
112 static inline bool classof(const Type *T) {
113 return T->isDerivedType();
115 static inline bool classof(const Value *V) {
116 return isa<Type>(V) && classof(cast<Type>(V));
123 struct FunctionType : public DerivedType {
124 typedef std::vector<PATypeHandle> ParamTypes;
125 friend class TypeMap<FunctionValType, FunctionType>;
127 PATypeHandle ResultType;
131 FunctionType(const FunctionType &); // Do not implement
132 const FunctionType &operator=(const FunctionType &); // Do not implement
134 // This should really be private, but it squelches a bogus warning
135 // from GCC to make them protected: warning: `class FunctionType' only
136 // defines private constructors and has no friends
138 // Private ctor - Only can be created by a static member...
139 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
142 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
143 // another (more concrete) type, we must eliminate all references to other
144 // types, to avoid some circular reference problems.
145 virtual void dropAllTypeUses();
148 /// FunctionType::get - This static method is the primary way of constructing
150 static FunctionType *get(const Type *Result,
151 const std::vector<const Type*> &Params,
154 inline bool isVarArg() const { return isVarArgs; }
155 inline const Type *getReturnType() const { return ResultType; }
156 inline const ParamTypes &getParamTypes() const { return ParamTys; }
158 // Parameter type accessors...
159 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
161 // getNumParams - Return the number of fixed parameters this function type
162 // requires. This does not consider varargs.
164 unsigned getNumParams() const { return ParamTys.size(); }
167 virtual const Type *getContainedType(unsigned i) const {
168 return i == 0 ? ResultType.get() : ParamTys[i-1].get();
170 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
172 // Implement the AbstractTypeUser interface.
173 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
174 virtual void typeBecameConcrete(const DerivedType *AbsTy);
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const FunctionType *T) { return true; }
178 static inline bool classof(const Type *T) {
179 return T->getPrimitiveID() == FunctionTyID;
181 static inline bool classof(const Value *V) {
182 return isa<Type>(V) && classof(cast<Type>(V));
187 // CompositeType - Common super class of ArrayType, StructType, and PointerType
189 class CompositeType : public DerivedType {
191 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
194 // getTypeAtIndex - Given an index value into the type, return the type of the
197 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
198 virtual bool indexValid(const Value *V) const = 0;
200 // Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const CompositeType *T) { return true; }
202 static inline bool classof(const Type *T) {
203 return T->getPrimitiveID() == ArrayTyID ||
204 T->getPrimitiveID() == StructTyID ||
205 T->getPrimitiveID() == PointerTyID;
207 static inline bool classof(const Value *V) {
208 return isa<Type>(V) && classof(cast<Type>(V));
213 struct StructType : public CompositeType {
214 friend class TypeMap<StructValType, StructType>;
215 typedef std::vector<PATypeHandle> ElementTypes;
218 ElementTypes ETypes; // Element types of struct
220 StructType(const StructType &); // Do not implement
221 const StructType &operator=(const StructType &); // Do not implement
224 // This should really be private, but it squelches a bogus warning
225 // from GCC to make them protected: warning: `class StructType' only
226 // defines private constructors and has no friends
228 // Private ctor - Only can be created by a static member...
229 StructType(const std::vector<const Type*> &Types);
231 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
232 // another (more concrete) type, we must eliminate all references to other
233 // types, to avoid some circular reference problems.
234 virtual void dropAllTypeUses();
237 /// StructType::get - This static method is the primary way to create a
239 static StructType *get(const std::vector<const Type*> &Params);
241 inline const ElementTypes &getElementTypes() const { return ETypes; }
243 virtual const Type *getContainedType(unsigned i) const {
244 return ETypes[i].get();
246 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
248 // getTypeAtIndex - Given an index value into the type, return the type of the
249 // element. For a structure type, this must be a constant value...
251 virtual const Type *getTypeAtIndex(const Value *V) const ;
252 virtual bool indexValid(const Value *V) const;
254 // Implement the AbstractTypeUser interface.
255 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
256 virtual void typeBecameConcrete(const DerivedType *AbsTy);
258 // Methods for support type inquiry through isa, cast, and dyn_cast:
259 static inline bool classof(const StructType *T) { return true; }
260 static inline bool classof(const Type *T) {
261 return T->getPrimitiveID() == StructTyID;
263 static inline bool classof(const Value *V) {
264 return isa<Type>(V) && classof(cast<Type>(V));
269 // SequentialType - This is the superclass of the array and pointer type
270 // classes. Both of these represent "arrays" in memory. The array type
271 // represents a specifically sized array, pointer types are unsized/unknown size
272 // arrays. SequentialType holds the common features of both, which stem from
273 // the fact that both lay their components out in memory identically.
275 class SequentialType : public CompositeType {
276 SequentialType(const SequentialType &); // Do not implement!
277 const SequentialType &operator=(const SequentialType &); // Do not implement!
279 PATypeHandle ElementType;
281 SequentialType(PrimitiveID TID, const Type *ElType)
282 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
286 inline const Type *getElementType() const { return ElementType; }
288 virtual const Type *getContainedType(unsigned i) const {
289 return ElementType.get();
291 virtual unsigned getNumContainedTypes() const { return 1; }
293 // getTypeAtIndex - Given an index value into the type, return the type of the
294 // element. For sequential types, there is only one subtype...
296 virtual const Type *getTypeAtIndex(const Value *V) const {
297 return ElementType.get();
299 virtual bool indexValid(const Value *V) const {
300 return V->getType()->isInteger();
303 // Methods for support type inquiry through isa, cast, and dyn_cast:
304 static inline bool classof(const SequentialType *T) { return true; }
305 static inline bool classof(const Type *T) {
306 return T->getPrimitiveID() == ArrayTyID ||
307 T->getPrimitiveID() == PointerTyID;
309 static inline bool classof(const Value *V) {
310 return isa<Type>(V) && classof(cast<Type>(V));
315 class ArrayType : public SequentialType {
316 friend class TypeMap<ArrayValType, ArrayType>;
317 unsigned NumElements;
319 ArrayType(const ArrayType &); // Do not implement
320 const ArrayType &operator=(const ArrayType &); // Do not implement
322 // This should really be private, but it squelches a bogus warning
323 // from GCC to make them protected: warning: `class ArrayType' only
324 // defines private constructors and has no friends
326 // Private ctor - Only can be created by a static member...
327 ArrayType(const Type *ElType, unsigned NumEl);
329 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
330 // another (more concrete) type, we must eliminate all references to other
331 // types, to avoid some circular reference problems.
332 virtual void dropAllTypeUses();
335 /// ArrayType::get - This static method is the primary way to construct an
337 static ArrayType *get(const Type *ElementType, unsigned NumElements);
339 inline unsigned getNumElements() const { return NumElements; }
341 // Implement the AbstractTypeUser interface.
342 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
343 virtual void typeBecameConcrete(const DerivedType *AbsTy);
345 // Methods for support type inquiry through isa, cast, and dyn_cast:
346 static inline bool classof(const ArrayType *T) { return true; }
347 static inline bool classof(const Type *T) {
348 return T->getPrimitiveID() == ArrayTyID;
350 static inline bool classof(const Value *V) {
351 return isa<Type>(V) && classof(cast<Type>(V));
357 class PointerType : public SequentialType {
358 friend class TypeMap<PointerValType, PointerType>;
359 PointerType(const PointerType &); // Do not implement
360 const PointerType &operator=(const PointerType &); // Do not implement
362 // This should really be private, but it squelches a bogus warning
363 // from GCC to make them protected: warning: `class PointerType' only
364 // defines private constructors and has no friends
366 // Private ctor - Only can be created by a static member...
367 PointerType(const Type *ElType);
369 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
370 // another (more concrete) type, we must eliminate all references to other
371 // types, to avoid some circular reference problems.
372 virtual void dropAllTypeUses();
374 /// PointerType::get - This is the only way to construct a new pointer type.
375 static PointerType *get(const Type *ElementType);
377 // Implement the AbstractTypeUser interface.
378 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
379 virtual void typeBecameConcrete(const DerivedType *AbsTy);
381 // Implement support type inquiry through isa, cast, and dyn_cast:
382 static inline bool classof(const PointerType *T) { return true; }
383 static inline bool classof(const Type *T) {
384 return T->getPrimitiveID() == PointerTyID;
386 static inline bool classof(const Value *V) {
387 return isa<Type>(V) && classof(cast<Type>(V));
392 class OpaqueType : public DerivedType {
393 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
394 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
396 // This should really be private, but it squelches a bogus warning
397 // from GCC to make them protected: warning: `class OpaqueType' only
398 // defines private constructors and has no friends
400 // Private ctor - Only can be created by a static member...
403 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
404 // another (more concrete) type, we must eliminate all references to other
405 // types, to avoid some circular reference problems.
406 virtual void dropAllTypeUses() {
407 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
411 // OpaqueType::get - Static factory method for the OpaqueType class...
412 static OpaqueType *get() {
413 return new OpaqueType(); // All opaque types are distinct
416 // Implement the AbstractTypeUser interface.
417 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
418 abort(); // FIXME: this is not really an AbstractTypeUser!
420 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
421 abort(); // FIXME: this is not really an AbstractTypeUser!
424 // Implement support for type inquiry through isa, cast, and dyn_cast:
425 static inline bool classof(const OpaqueType *T) { return true; }
426 static inline bool classof(const Type *T) {
427 return T->getPrimitiveID() == OpaqueTyID;
429 static inline bool classof(const Value *V) {
430 return isa<Type>(V) && classof(cast<Type>(V));
435 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
436 // These are defined here because they MUST be inlined, yet are dependent on
437 // the definition of the Type class. Of course Type derives from Value, which
438 // contains an AbstractTypeUser instance, so there is no good way to factor out
439 // the code. Hence this bit of uglyness.
441 inline void PATypeHandle::addUser() {
442 assert(Ty && "Type Handle has a null type!");
443 if (Ty->isAbstract())
444 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
446 inline void PATypeHandle::removeUser() {
447 if (Ty->isAbstract())
448 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
451 inline void PATypeHandle::removeUserFromConcrete() {
452 if (!Ty->isAbstract())
453 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
456 // Define inline methods for PATypeHolder...
458 inline void PATypeHolder::addRef() {
459 if (Ty->isAbstract())
460 cast<DerivedType>(Ty)->addRef();
463 inline void PATypeHolder::dropRef() {
464 if (Ty->isAbstract())
465 cast<DerivedType>(Ty)->dropRef();
468 /// get - This implements the forwarding part of the union-find algorithm for
469 /// abstract types. Before every access to the Type*, we check to see if the
470 /// type we are pointing to is forwarding to a new type. If so, we drop our
471 /// reference to the type.
472 inline const Type* PATypeHolder::get() const {
473 const Type *NewTy = Ty->getForwardedType();
474 if (!NewTy) return Ty;
475 return *const_cast<PATypeHolder*>(this) = NewTy;
478 } // End llvm namespace