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.
60 void dropAllTypeUses();
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));
121 /// FunctionType - Class to represent function types
123 class FunctionType : public DerivedType {
124 friend class TypeMap<FunctionValType, FunctionType>;
127 FunctionType(const FunctionType &); // Do not implement
128 const FunctionType &operator=(const FunctionType &); // Do not implement
130 /// This should really be private, but it squelches a bogus warning
131 /// from GCC to make them protected: warning: `class FunctionType' only
132 /// defines private constructors and has no friends
134 /// Private ctor - Only can be created by a static member...
136 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
140 /// FunctionType::get - This static method is the primary way of constructing
143 static FunctionType *get(const Type *Result,
144 const std::vector<const Type*> &Params,
147 inline bool isVarArg() const { return isVarArgs; }
148 inline const Type *getReturnType() const { return ContainedTys[0]; }
150 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
151 param_iterator param_begin() const { return ContainedTys.begin()+1; }
152 param_iterator param_end() const { return ContainedTys.end(); }
154 // Parameter type accessors...
155 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
157 /// getNumParams - Return the number of fixed parameters this function type
158 /// requires. This does not consider varargs.
160 unsigned getNumParams() const { return ContainedTys.size()-1; }
162 // Implement the AbstractTypeUser interface.
163 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
164 virtual void typeBecameConcrete(const DerivedType *AbsTy);
166 // Methods for support type inquiry through isa, cast, and dyn_cast:
167 static inline bool classof(const FunctionType *T) { return true; }
168 static inline bool classof(const Type *T) {
169 return T->getPrimitiveID() == FunctionTyID;
171 static inline bool classof(const Value *V) {
172 return isa<Type>(V) && classof(cast<Type>(V));
177 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
179 class CompositeType : public DerivedType {
181 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
184 /// getTypeAtIndex - Given an index value into the type, return the type of
187 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
188 virtual bool indexValid(const Value *V) const = 0;
190 // Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const CompositeType *T) { return true; }
192 static inline bool classof(const Type *T) {
193 return T->getPrimitiveID() == ArrayTyID ||
194 T->getPrimitiveID() == StructTyID ||
195 T->getPrimitiveID() == PointerTyID;
197 static inline bool classof(const Value *V) {
198 return isa<Type>(V) && classof(cast<Type>(V));
203 /// StructType - Class to represent struct types
205 class StructType : public CompositeType {
206 friend class TypeMap<StructValType, StructType>;
207 StructType(const StructType &); // Do not implement
208 const StructType &operator=(const StructType &); // Do not implement
211 /// This should really be private, but it squelches a bogus warning
212 /// from GCC to make them protected: warning: `class StructType' only
213 /// defines private constructors and has no friends
215 /// Private ctor - Only can be created by a static member...
217 StructType(const std::vector<const Type*> &Types);
220 /// StructType::get - This static method is the primary way to create a
223 static StructType *get(const std::vector<const Type*> &Params);
225 // Iterator access to the elements
226 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
227 element_iterator element_begin() const { return ContainedTys.begin(); }
228 element_iterator element_end() const { return ContainedTys.end(); }
230 // Random access to the elements
231 unsigned getNumElements() const { return ContainedTys.size(); }
232 const Type *getElementType(unsigned N) const {
233 assert(N < ContainedTys.size() && "Element number out of range!");
234 return ContainedTys[N];
237 /// getTypeAtIndex - Given an index value into the type, return the type of
238 /// the element. For a structure type, this must be a constant value...
240 virtual const Type *getTypeAtIndex(const Value *V) const ;
241 virtual bool indexValid(const Value *V) const;
243 // Implement the AbstractTypeUser interface.
244 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
245 virtual void typeBecameConcrete(const DerivedType *AbsTy);
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const StructType *T) { return true; }
249 static inline bool classof(const Type *T) {
250 return T->getPrimitiveID() == StructTyID;
252 static inline bool classof(const Value *V) {
253 return isa<Type>(V) && classof(cast<Type>(V));
258 /// SequentialType - This is the superclass of the array and pointer type
259 /// classes. Both of these represent "arrays" in memory. The array type
260 /// represents a specifically sized array, pointer types are unsized/unknown
261 /// size arrays. SequentialType holds the common features of both, which stem
262 /// from the fact that both lay their components out in memory identically.
264 class SequentialType : public CompositeType {
265 SequentialType(const SequentialType &); // Do not implement!
266 const SequentialType &operator=(const SequentialType &); // Do not implement!
268 SequentialType(PrimitiveID TID, const Type *ElType) : CompositeType(TID) {
269 ContainedTys.reserve(1);
270 ContainedTys.push_back(PATypeHandle(ElType, this));
274 inline const Type *getElementType() const { return ContainedTys[0]; }
276 /// getTypeAtIndex - Given an index value into the type, return the type of
277 /// the element. For sequential types, there is only one subtype...
279 virtual const Type *getTypeAtIndex(const Value *V) const {
280 return ContainedTys[0];
282 virtual bool indexValid(const Value *V) const {
283 return V->getType()->isInteger();
286 // Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const SequentialType *T) { return true; }
288 static inline bool classof(const Type *T) {
289 return T->getPrimitiveID() == ArrayTyID ||
290 T->getPrimitiveID() == PointerTyID;
292 static inline bool classof(const Value *V) {
293 return isa<Type>(V) && classof(cast<Type>(V));
298 /// ArrayType - Class to represent array types
300 class ArrayType : public SequentialType {
301 friend class TypeMap<ArrayValType, ArrayType>;
302 unsigned NumElements;
304 ArrayType(const ArrayType &); // Do not implement
305 const ArrayType &operator=(const ArrayType &); // Do not implement
307 /// This should really be private, but it squelches a bogus warning
308 /// from GCC to make them protected: warning: `class ArrayType' only
309 /// defines private constructors and has no friends
311 /// Private ctor - Only can be created by a static member...
313 ArrayType(const Type *ElType, unsigned NumEl);
316 /// ArrayType::get - This static method is the primary way to construct an
319 static ArrayType *get(const Type *ElementType, unsigned NumElements);
321 inline unsigned getNumElements() const { return NumElements; }
323 // Implement the AbstractTypeUser interface.
324 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
325 virtual void typeBecameConcrete(const DerivedType *AbsTy);
327 // Methods for support type inquiry through isa, cast, and dyn_cast:
328 static inline bool classof(const ArrayType *T) { return true; }
329 static inline bool classof(const Type *T) {
330 return T->getPrimitiveID() == ArrayTyID;
332 static inline bool classof(const Value *V) {
333 return isa<Type>(V) && classof(cast<Type>(V));
338 /// PointerType - Class to represent pointers
340 class PointerType : public SequentialType {
341 friend class TypeMap<PointerValType, PointerType>;
342 PointerType(const PointerType &); // Do not implement
343 const PointerType &operator=(const PointerType &); // Do not implement
345 // This should really be private, but it squelches a bogus warning
346 // from GCC to make them protected: warning: `class PointerType' only
347 // defines private constructors and has no friends
349 // Private ctor - Only can be created by a static member...
350 PointerType(const Type *ElType);
353 /// PointerType::get - This is the only way to construct a new pointer type.
354 static PointerType *get(const Type *ElementType);
356 // Implement the AbstractTypeUser interface.
357 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
358 virtual void typeBecameConcrete(const DerivedType *AbsTy);
360 // Implement support type inquiry through isa, cast, and dyn_cast:
361 static inline bool classof(const PointerType *T) { return true; }
362 static inline bool classof(const Type *T) {
363 return T->getPrimitiveID() == PointerTyID;
365 static inline bool classof(const Value *V) {
366 return isa<Type>(V) && classof(cast<Type>(V));
371 /// OpaqueType - Class to represent abstract types
373 class OpaqueType : public DerivedType {
374 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
375 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
377 /// This should really be private, but it squelches a bogus warning
378 /// from GCC to make them protected: warning: `class OpaqueType' only
379 /// defines private constructors and has no friends
381 /// Private ctor - Only can be created by a static member...
385 /// OpaqueType::get - Static factory method for the OpaqueType class...
387 static OpaqueType *get() {
388 return new OpaqueType(); // All opaque types are distinct
391 // Implement the AbstractTypeUser interface.
392 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
393 abort(); // FIXME: this is not really an AbstractTypeUser!
395 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
396 abort(); // FIXME: this is not really an AbstractTypeUser!
399 // Implement support for type inquiry through isa, cast, and dyn_cast:
400 static inline bool classof(const OpaqueType *T) { return true; }
401 static inline bool classof(const Type *T) {
402 return T->getPrimitiveID() == OpaqueTyID;
404 static inline bool classof(const Value *V) {
405 return isa<Type>(V) && classof(cast<Type>(V));
410 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
411 // These are defined here because they MUST be inlined, yet are dependent on
412 // the definition of the Type class. Of course Type derives from Value, which
413 // contains an AbstractTypeUser instance, so there is no good way to factor out
414 // the code. Hence this bit of uglyness.
416 inline void PATypeHandle::addUser() {
417 assert(Ty && "Type Handle has a null type!");
418 if (Ty->isAbstract())
419 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
421 inline void PATypeHandle::removeUser() {
422 if (Ty->isAbstract())
423 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
426 inline void PATypeHandle::removeUserFromConcrete() {
427 if (!Ty->isAbstract())
428 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
431 // Define inline methods for PATypeHolder...
433 inline void PATypeHolder::addRef() {
434 if (Ty->isAbstract())
435 cast<DerivedType>(Ty)->addRef();
438 inline void PATypeHolder::dropRef() {
439 if (Ty->isAbstract())
440 cast<DerivedType>(Ty)->dropRef();
443 /// get - This implements the forwarding part of the union-find algorithm for
444 /// abstract types. Before every access to the Type*, we check to see if the
445 /// type we are pointing to is forwarding to a new type. If so, we drop our
446 /// reference to the type.
448 inline const Type* PATypeHolder::get() const {
449 const Type *NewTy = Ty->getForwardedType();
450 if (!NewTy) return Ty;
451 return *const_cast<PATypeHolder*>(this) = NewTy;
454 } // End llvm namespace