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"
25 template<class ValType, class TypeClass> class TypeMap;
26 class FunctionValType;
31 class DerivedType : public Type, public AbstractTypeUser {
32 /// RefCount - This counts the number of PATypeHolders that are pointing to
33 /// this type. When this number falls to zero, if the type is abstract and
34 /// has no AbstractTypeUsers, the type is deleted.
36 mutable unsigned RefCount;
38 // AbstractTypeUsers - Implement a list of the users that need to be notified
39 // if I am a type, and I get resolved into a more concrete type.
41 ///// FIXME: kill mutable nonsense when Types are not const
42 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
45 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 void dropAllTypeUses();
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 class FunctionType : public DerivedType {
123 friend class TypeMap<FunctionValType, FunctionType>;
126 FunctionType(const FunctionType &); // Do not implement
127 const FunctionType &operator=(const FunctionType &); // Do not implement
129 // This should really be private, but it squelches a bogus warning
130 // from GCC to make them protected: warning: `class FunctionType' only
131 // defines private constructors and has no friends
133 // Private ctor - Only can be created by a static member...
134 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
138 /// FunctionType::get - This static method is the primary way of constructing
140 static FunctionType *get(const Type *Result,
141 const std::vector<const Type*> &Params,
144 inline bool isVarArg() const { return isVarArgs; }
145 inline const Type *getReturnType() const { return ContainedTys[0]; }
147 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
148 param_iterator param_begin() const { return ContainedTys.begin()+1; }
149 param_iterator param_end() const { return ContainedTys.end(); }
151 // Parameter type accessors...
152 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
154 // getNumParams - Return the number of fixed parameters this function type
155 // requires. This does not consider varargs.
157 unsigned getNumParams() const { return ContainedTys.size()-1; }
159 // Implement the AbstractTypeUser interface.
160 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
161 virtual void typeBecameConcrete(const DerivedType *AbsTy);
163 // Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const FunctionType *T) { return true; }
165 static inline bool classof(const Type *T) {
166 return T->getPrimitiveID() == FunctionTyID;
168 static inline bool classof(const Value *V) {
169 return isa<Type>(V) && classof(cast<Type>(V));
174 // CompositeType - Common super class of ArrayType, StructType, and PointerType
176 class CompositeType : public DerivedType {
178 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
181 // getTypeAtIndex - Given an index value into the type, return the type of the
184 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
185 virtual bool indexValid(const Value *V) const = 0;
187 // Methods for support type inquiry through isa, cast, and dyn_cast:
188 static inline bool classof(const CompositeType *T) { return true; }
189 static inline bool classof(const Type *T) {
190 return T->getPrimitiveID() == ArrayTyID ||
191 T->getPrimitiveID() == StructTyID ||
192 T->getPrimitiveID() == PointerTyID;
194 static inline bool classof(const Value *V) {
195 return isa<Type>(V) && classof(cast<Type>(V));
200 class StructType : public CompositeType {
201 friend class TypeMap<StructValType, StructType>;
202 StructType(const StructType &); // Do not implement
203 const StructType &operator=(const StructType &); // Do not implement
206 // This should really be private, but it squelches a bogus warning
207 // from GCC to make them protected: warning: `class StructType' only
208 // defines private constructors and has no friends
210 // Private ctor - Only can be created by a static member...
211 StructType(const std::vector<const Type*> &Types);
214 /// StructType::get - This static method is the primary way to create a
216 static StructType *get(const std::vector<const Type*> &Params);
218 // Iterator access to the elements
219 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
220 element_iterator element_begin() const { return ContainedTys.begin(); }
221 element_iterator element_end() const { return ContainedTys.end(); }
223 // Random access to the elements
224 unsigned getNumElements() const { return ContainedTys.size(); }
225 const Type *getElementType(unsigned N) const {
226 assert(N < ContainedTys.size() && "Element number out of range!");
227 return ContainedTys[N];
230 // getTypeAtIndex - Given an index value into the type, return the type of the
231 // element. For a structure type, this must be a constant value...
233 virtual const Type *getTypeAtIndex(const Value *V) const ;
234 virtual bool indexValid(const Value *V) const;
236 // Implement the AbstractTypeUser interface.
237 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
238 virtual void typeBecameConcrete(const DerivedType *AbsTy);
240 // Methods for support type inquiry through isa, cast, and dyn_cast:
241 static inline bool classof(const StructType *T) { return true; }
242 static inline bool classof(const Type *T) {
243 return T->getPrimitiveID() == StructTyID;
245 static inline bool classof(const Value *V) {
246 return isa<Type>(V) && classof(cast<Type>(V));
251 // SequentialType - This is the superclass of the array and pointer type
252 // classes. Both of these represent "arrays" in memory. The array type
253 // represents a specifically sized array, pointer types are unsized/unknown size
254 // arrays. SequentialType holds the common features of both, which stem from
255 // the fact that both lay their components out in memory identically.
257 class SequentialType : public CompositeType {
258 SequentialType(const SequentialType &); // Do not implement!
259 const SequentialType &operator=(const SequentialType &); // Do not implement!
261 SequentialType(PrimitiveID TID, const Type *ElType) : CompositeType(TID) {
262 ContainedTys.reserve(1);
263 ContainedTys.push_back(PATypeHandle(ElType, this));
267 inline const Type *getElementType() const { return ContainedTys[0]; }
269 // getTypeAtIndex - Given an index value into the type, return the type of the
270 // element. For sequential types, there is only one subtype...
272 virtual const Type *getTypeAtIndex(const Value *V) const {
273 return ContainedTys[0];
275 virtual bool indexValid(const Value *V) const {
276 return V->getType()->isInteger();
279 // Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const SequentialType *T) { return true; }
281 static inline bool classof(const Type *T) {
282 return T->getPrimitiveID() == ArrayTyID ||
283 T->getPrimitiveID() == PointerTyID;
285 static inline bool classof(const Value *V) {
286 return isa<Type>(V) && classof(cast<Type>(V));
291 class ArrayType : public SequentialType {
292 friend class TypeMap<ArrayValType, ArrayType>;
293 unsigned NumElements;
295 ArrayType(const ArrayType &); // Do not implement
296 const ArrayType &operator=(const ArrayType &); // Do not implement
298 // This should really be private, but it squelches a bogus warning
299 // from GCC to make them protected: warning: `class ArrayType' only
300 // defines private constructors and has no friends
302 // Private ctor - Only can be created by a static member...
303 ArrayType(const Type *ElType, unsigned NumEl);
306 /// ArrayType::get - This static method is the primary way to construct an
308 static ArrayType *get(const Type *ElementType, unsigned NumElements);
310 inline unsigned getNumElements() const { return NumElements; }
312 // Implement the AbstractTypeUser interface.
313 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
314 virtual void typeBecameConcrete(const DerivedType *AbsTy);
316 // Methods for support type inquiry through isa, cast, and dyn_cast:
317 static inline bool classof(const ArrayType *T) { return true; }
318 static inline bool classof(const Type *T) {
319 return T->getPrimitiveID() == ArrayTyID;
321 static inline bool classof(const Value *V) {
322 return isa<Type>(V) && classof(cast<Type>(V));
328 class PointerType : public SequentialType {
329 friend class TypeMap<PointerValType, PointerType>;
330 PointerType(const PointerType &); // Do not implement
331 const PointerType &operator=(const PointerType &); // Do not implement
333 // This should really be private, but it squelches a bogus warning
334 // from GCC to make them protected: warning: `class PointerType' only
335 // defines private constructors and has no friends
337 // Private ctor - Only can be created by a static member...
338 PointerType(const Type *ElType);
341 /// PointerType::get - This is the only way to construct a new pointer type.
342 static PointerType *get(const Type *ElementType);
344 // Implement the AbstractTypeUser interface.
345 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
346 virtual void typeBecameConcrete(const DerivedType *AbsTy);
348 // Implement support type inquiry through isa, cast, and dyn_cast:
349 static inline bool classof(const PointerType *T) { return true; }
350 static inline bool classof(const Type *T) {
351 return T->getPrimitiveID() == PointerTyID;
353 static inline bool classof(const Value *V) {
354 return isa<Type>(V) && classof(cast<Type>(V));
359 class OpaqueType : public DerivedType {
360 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
361 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
363 // This should really be private, but it squelches a bogus warning
364 // from GCC to make them protected: warning: `class OpaqueType' only
365 // defines private constructors and has no friends
367 // Private ctor - Only can be created by a static member...
371 // OpaqueType::get - Static factory method for the OpaqueType class...
372 static OpaqueType *get() {
373 return new OpaqueType(); // All opaque types are distinct
376 // Implement the AbstractTypeUser interface.
377 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
378 abort(); // FIXME: this is not really an AbstractTypeUser!
380 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
381 abort(); // FIXME: this is not really an AbstractTypeUser!
384 // Implement support for type inquiry through isa, cast, and dyn_cast:
385 static inline bool classof(const OpaqueType *T) { return true; }
386 static inline bool classof(const Type *T) {
387 return T->getPrimitiveID() == OpaqueTyID;
389 static inline bool classof(const Value *V) {
390 return isa<Type>(V) && classof(cast<Type>(V));
395 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
396 // These are defined here because they MUST be inlined, yet are dependent on
397 // the definition of the Type class. Of course Type derives from Value, which
398 // contains an AbstractTypeUser instance, so there is no good way to factor out
399 // the code. Hence this bit of uglyness.
401 inline void PATypeHandle::addUser() {
402 assert(Ty && "Type Handle has a null type!");
403 if (Ty->isAbstract())
404 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
406 inline void PATypeHandle::removeUser() {
407 if (Ty->isAbstract())
408 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
411 inline void PATypeHandle::removeUserFromConcrete() {
412 if (!Ty->isAbstract())
413 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
416 // Define inline methods for PATypeHolder...
418 inline void PATypeHolder::addRef() {
419 if (Ty->isAbstract())
420 cast<DerivedType>(Ty)->addRef();
423 inline void PATypeHolder::dropRef() {
424 if (Ty->isAbstract())
425 cast<DerivedType>(Ty)->dropRef();
428 /// get - This implements the forwarding part of the union-find algorithm for
429 /// abstract types. Before every access to the Type*, we check to see if the
430 /// type we are pointing to is forwarding to a new type. If so, we drop our
431 /// reference to the type.
432 inline const Type* PATypeHolder::get() const {
433 const Type *NewTy = Ty->getForwardedType();
434 if (!NewTy) return Ty;
435 return *const_cast<PATypeHolder*>(this) = NewTy;
438 } // End llvm namespace