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 class FunctionType : public DerivedType {
124 friend class TypeMap<FunctionValType, FunctionType>;
125 PATypeHandle ResultType;
126 typedef std::vector<PATypeHandle> ParamTypes;
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; }
156 typedef ParamTypes::const_iterator param_iterator;
157 param_iterator param_begin() const { return ParamTys.begin(); }
158 param_iterator param_end() const { return ParamTys.end(); }
160 // Parameter type accessors...
161 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
163 // getNumParams - Return the number of fixed parameters this function type
164 // requires. This does not consider varargs.
166 unsigned getNumParams() const { return ParamTys.size(); }
169 virtual const Type *getContainedType(unsigned i) const {
170 return i == 0 ? ResultType.get() : ParamTys[i-1].get();
172 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
174 // Implement the AbstractTypeUser interface.
175 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
176 virtual void typeBecameConcrete(const DerivedType *AbsTy);
178 // Methods for support type inquiry through isa, cast, and dyn_cast:
179 static inline bool classof(const FunctionType *T) { return true; }
180 static inline bool classof(const Type *T) {
181 return T->getPrimitiveID() == FunctionTyID;
183 static inline bool classof(const Value *V) {
184 return isa<Type>(V) && classof(cast<Type>(V));
189 // CompositeType - Common super class of ArrayType, StructType, and PointerType
191 class CompositeType : public DerivedType {
193 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
196 // getTypeAtIndex - Given an index value into the type, return the type of the
199 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
200 virtual bool indexValid(const Value *V) const = 0;
202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const CompositeType *T) { return true; }
204 static inline bool classof(const Type *T) {
205 return T->getPrimitiveID() == ArrayTyID ||
206 T->getPrimitiveID() == StructTyID ||
207 T->getPrimitiveID() == PointerTyID;
209 static inline bool classof(const Value *V) {
210 return isa<Type>(V) && classof(cast<Type>(V));
215 struct StructType : public CompositeType {
216 friend class TypeMap<StructValType, StructType>;
217 typedef std::vector<PATypeHandle> ElementTypes;
220 ElementTypes ETypes; // Element types of struct
222 StructType(const StructType &); // Do not implement
223 const StructType &operator=(const StructType &); // Do not implement
226 // This should really be private, but it squelches a bogus warning
227 // from GCC to make them protected: warning: `class StructType' only
228 // defines private constructors and has no friends
230 // Private ctor - Only can be created by a static member...
231 StructType(const std::vector<const Type*> &Types);
233 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
234 // another (more concrete) type, we must eliminate all references to other
235 // types, to avoid some circular reference problems.
236 virtual void dropAllTypeUses();
239 /// StructType::get - This static method is the primary way to create a
241 static StructType *get(const std::vector<const Type*> &Params);
243 inline const ElementTypes &getElementTypes() const { return ETypes; }
245 virtual const Type *getContainedType(unsigned i) const {
246 return ETypes[i].get();
248 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
250 // getTypeAtIndex - Given an index value into the type, return the type of the
251 // element. For a structure type, this must be a constant value...
253 virtual const Type *getTypeAtIndex(const Value *V) const ;
254 virtual bool indexValid(const Value *V) const;
256 // Implement the AbstractTypeUser interface.
257 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
258 virtual void typeBecameConcrete(const DerivedType *AbsTy);
260 // Methods for support type inquiry through isa, cast, and dyn_cast:
261 static inline bool classof(const StructType *T) { return true; }
262 static inline bool classof(const Type *T) {
263 return T->getPrimitiveID() == StructTyID;
265 static inline bool classof(const Value *V) {
266 return isa<Type>(V) && classof(cast<Type>(V));
271 // SequentialType - This is the superclass of the array and pointer type
272 // classes. Both of these represent "arrays" in memory. The array type
273 // represents a specifically sized array, pointer types are unsized/unknown size
274 // arrays. SequentialType holds the common features of both, which stem from
275 // the fact that both lay their components out in memory identically.
277 class SequentialType : public CompositeType {
278 SequentialType(const SequentialType &); // Do not implement!
279 const SequentialType &operator=(const SequentialType &); // Do not implement!
281 PATypeHandle ElementType;
283 SequentialType(PrimitiveID TID, const Type *ElType)
284 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
288 inline const Type *getElementType() const { return ElementType; }
290 virtual const Type *getContainedType(unsigned i) const {
291 return ElementType.get();
293 virtual unsigned getNumContainedTypes() const { return 1; }
295 // getTypeAtIndex - Given an index value into the type, return the type of the
296 // element. For sequential types, there is only one subtype...
298 virtual const Type *getTypeAtIndex(const Value *V) const {
299 return ElementType.get();
301 virtual bool indexValid(const Value *V) const {
302 return V->getType()->isInteger();
305 // Methods for support type inquiry through isa, cast, and dyn_cast:
306 static inline bool classof(const SequentialType *T) { return true; }
307 static inline bool classof(const Type *T) {
308 return T->getPrimitiveID() == ArrayTyID ||
309 T->getPrimitiveID() == PointerTyID;
311 static inline bool classof(const Value *V) {
312 return isa<Type>(V) && classof(cast<Type>(V));
317 class ArrayType : public SequentialType {
318 friend class TypeMap<ArrayValType, ArrayType>;
319 unsigned NumElements;
321 ArrayType(const ArrayType &); // Do not implement
322 const ArrayType &operator=(const ArrayType &); // Do not implement
324 // This should really be private, but it squelches a bogus warning
325 // from GCC to make them protected: warning: `class ArrayType' only
326 // defines private constructors and has no friends
328 // Private ctor - Only can be created by a static member...
329 ArrayType(const Type *ElType, unsigned NumEl);
331 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
332 // another (more concrete) type, we must eliminate all references to other
333 // types, to avoid some circular reference problems.
334 virtual void dropAllTypeUses();
337 /// ArrayType::get - This static method is the primary way to construct an
339 static ArrayType *get(const Type *ElementType, unsigned NumElements);
341 inline unsigned getNumElements() const { return NumElements; }
343 // Implement the AbstractTypeUser interface.
344 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
345 virtual void typeBecameConcrete(const DerivedType *AbsTy);
347 // Methods for support type inquiry through isa, cast, and dyn_cast:
348 static inline bool classof(const ArrayType *T) { return true; }
349 static inline bool classof(const Type *T) {
350 return T->getPrimitiveID() == ArrayTyID;
352 static inline bool classof(const Value *V) {
353 return isa<Type>(V) && classof(cast<Type>(V));
359 class PointerType : public SequentialType {
360 friend class TypeMap<PointerValType, PointerType>;
361 PointerType(const PointerType &); // Do not implement
362 const PointerType &operator=(const PointerType &); // Do not implement
364 // This should really be private, but it squelches a bogus warning
365 // from GCC to make them protected: warning: `class PointerType' only
366 // defines private constructors and has no friends
368 // Private ctor - Only can be created by a static member...
369 PointerType(const Type *ElType);
371 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
372 // another (more concrete) type, we must eliminate all references to other
373 // types, to avoid some circular reference problems.
374 virtual void dropAllTypeUses();
376 /// PointerType::get - This is the only way to construct a new pointer type.
377 static PointerType *get(const Type *ElementType);
379 // Implement the AbstractTypeUser interface.
380 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
381 virtual void typeBecameConcrete(const DerivedType *AbsTy);
383 // Implement support type inquiry through isa, cast, and dyn_cast:
384 static inline bool classof(const PointerType *T) { return true; }
385 static inline bool classof(const Type *T) {
386 return T->getPrimitiveID() == PointerTyID;
388 static inline bool classof(const Value *V) {
389 return isa<Type>(V) && classof(cast<Type>(V));
394 class OpaqueType : public DerivedType {
395 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
396 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
398 // This should really be private, but it squelches a bogus warning
399 // from GCC to make them protected: warning: `class OpaqueType' only
400 // defines private constructors and has no friends
402 // Private ctor - Only can be created by a static member...
405 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
406 // another (more concrete) type, we must eliminate all references to other
407 // types, to avoid some circular reference problems.
408 virtual void dropAllTypeUses() {
409 // FIXME: THIS IS NOT AN ABSTRACT TYPE USER!
413 // OpaqueType::get - Static factory method for the OpaqueType class...
414 static OpaqueType *get() {
415 return new OpaqueType(); // All opaque types are distinct
418 // Implement the AbstractTypeUser interface.
419 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
420 abort(); // FIXME: this is not really an AbstractTypeUser!
422 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
423 abort(); // FIXME: this is not really an AbstractTypeUser!
426 // Implement support for type inquiry through isa, cast, and dyn_cast:
427 static inline bool classof(const OpaqueType *T) { return true; }
428 static inline bool classof(const Type *T) {
429 return T->getPrimitiveID() == OpaqueTyID;
431 static inline bool classof(const Value *V) {
432 return isa<Type>(V) && classof(cast<Type>(V));
437 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
438 // These are defined here because they MUST be inlined, yet are dependent on
439 // the definition of the Type class. Of course Type derives from Value, which
440 // contains an AbstractTypeUser instance, so there is no good way to factor out
441 // the code. Hence this bit of uglyness.
443 inline void PATypeHandle::addUser() {
444 assert(Ty && "Type Handle has a null type!");
445 if (Ty->isAbstract())
446 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
448 inline void PATypeHandle::removeUser() {
449 if (Ty->isAbstract())
450 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
453 inline void PATypeHandle::removeUserFromConcrete() {
454 if (!Ty->isAbstract())
455 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
458 // Define inline methods for PATypeHolder...
460 inline void PATypeHolder::addRef() {
461 if (Ty->isAbstract())
462 cast<DerivedType>(Ty)->addRef();
465 inline void PATypeHolder::dropRef() {
466 if (Ty->isAbstract())
467 cast<DerivedType>(Ty)->dropRef();
470 /// get - This implements the forwarding part of the union-find algorithm for
471 /// abstract types. Before every access to the Type*, we check to see if the
472 /// type we are pointing to is forwarding to a new type. If so, we drop our
473 /// reference to the type.
474 inline const Type* PATypeHolder::get() const {
475 const Type *NewTy = Ty->getForwardedType();
476 if (!NewTy) return Ty;
477 return *const_cast<PATypeHolder*>(this) = NewTy;
480 } // End llvm namespace