1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
3 // This file contains the declarations of classes that represent "derived
4 // types". These are things like "arrays of x" or "structure of x, y, z" or
5 // "method returning x taking (y,z) as parameters", etc...
7 // The implementations of these classes live in the Type.cpp file.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
14 #include "llvm/Type.h"
16 template<class ValType, class TypeClass> class TypeMap;
17 class FunctionValType;
22 class DerivedType : public Type, public AbstractTypeUser {
23 char isRefining; // Used for recursive types
25 // AbstractTypeUsers - Implement a list of the users that need to be notified
26 // if I am a type, and I get resolved into a more concrete type.
28 ///// FIXME: kill mutable nonsense when Type's are not const
29 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
32 inline DerivedType(PrimitiveID id) : Type("", id) {
36 assert(AbstractTypeUsers.empty());
39 // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
40 // has been refined a bit. The pointer is still valid and still should be
41 // used, but the subtypes have changed.
45 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
46 // another (more concrete) type, we must eliminate all references to other
47 // types, to avoid some circular reference problems. This also removes the
48 // type from the internal tables of available types.
49 virtual void dropAllTypeUses(bool inMap) = 0;
52 void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
56 //===--------------------------------------------------------------------===//
57 // Abstract Type handling methods - These types have special lifetimes, which
58 // are managed by (add|remove)AbstractTypeUser. See comments in
59 // AbstractTypeUser.h for more information.
61 // addAbstractTypeUser - Notify an abstract type that there is a new user of
62 // it. This function is called primarily by the PATypeHandle class.
64 void addAbstractTypeUser(AbstractTypeUser *U) const;
66 // removeAbstractTypeUser - Notify an abstract type that a user of the class
67 // no longer has a handle to the type. This function is called primarily by
68 // the PATypeHandle class. When there are no users of the abstract type, it
69 // is annihilated, because there is no way to get a reference to it ever
72 void removeAbstractTypeUser(AbstractTypeUser *U) const;
74 // refineAbstractTypeTo - This function is used to when it is discovered that
75 // the 'this' abstract type is actually equivalent to the NewType specified.
76 // This causes all users of 'this' to switch to reference the more concrete
77 // type NewType and for 'this' to be deleted.
79 void refineAbstractTypeTo(const Type *NewType) {
80 refineAbstractTypeToInternal(NewType, true);
83 void dump() const { Value::dump(); }
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const DerivedType *T) { return true; }
87 static inline bool classof(const Type *T) {
88 return T->isDerivedType();
90 static inline bool classof(const Value *V) {
91 return isa<Type>(V) && classof(cast<Type>(V));
98 struct FunctionType : public DerivedType {
99 typedef std::vector<PATypeHandle> ParamTypes;
100 friend class TypeMap<FunctionValType, FunctionType>;
102 PATypeHandle ResultType;
106 FunctionType(const FunctionType &); // Do not implement
107 const FunctionType &operator=(const FunctionType &); // Do not implement
109 // This should really be private, but it squelches a bogus warning
110 // from GCC to make them protected: warning: `class FunctionType' only
111 // defines private constructors and has no friends
113 // Private ctor - Only can be created by a static member...
114 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
117 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
118 // another (more concrete) type, we must eliminate all references to other
119 // types, to avoid some circular reference problems. This also removes the
120 // type from the internal tables of available types.
121 virtual void dropAllTypeUses(bool inMap);
125 inline bool isVarArg() const { return isVarArgs; }
126 inline const Type *getReturnType() const { return ResultType; }
127 inline const ParamTypes &getParamTypes() const { return ParamTys; }
129 // Parameter type accessors...
130 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
132 // getNumParams - Return the number of fixed parameters this function type
133 // requires. This does not consider varargs.
135 unsigned getNumParams() const { return ParamTys.size(); }
138 virtual const Type *getContainedType(unsigned i) const {
139 return i == 0 ? ResultType :
140 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
142 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
144 // refineAbstractType - Called when a contained type is found to be more
145 // concrete - this could potentially change us from an abstract type to a
148 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
150 static FunctionType *get(const Type *Result,
151 const std::vector<const Type*> &Params,
154 // Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const FunctionType *T) { return true; }
156 static inline bool classof(const Type *T) {
157 return T->getPrimitiveID() == FunctionTyID;
159 static inline bool classof(const Value *V) {
160 return isa<Type>(V) && classof(cast<Type>(V));
165 // CompositeType - Common super class of ArrayType, StructType, and PointerType
167 class CompositeType : public DerivedType {
169 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
172 // getTypeAtIndex - Given an index value into the type, return the type of the
175 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
176 virtual bool indexValid(const Value *V) const = 0;
178 // getIndexType - Return the type required of indices for this composite.
179 // For structures, this is ubyte, for arrays, this is uint
181 virtual const Type *getIndexType() const = 0;
184 // Methods for support type inquiry through isa, cast, and dyn_cast:
185 static inline bool classof(const CompositeType *T) { return true; }
186 static inline bool classof(const Type *T) {
187 return T->getPrimitiveID() == ArrayTyID ||
188 T->getPrimitiveID() == StructTyID ||
189 T->getPrimitiveID() == PointerTyID;
191 static inline bool classof(const Value *V) {
192 return isa<Type>(V) && classof(cast<Type>(V));
197 struct StructType : public CompositeType {
198 friend class TypeMap<StructValType, StructType>;
199 typedef std::vector<PATypeHandle> ElementTypes;
202 ElementTypes ETypes; // Element types of struct
204 StructType(const StructType &); // Do not implement
205 const StructType &operator=(const StructType &); // Do not implement
208 // This should really be private, but it squelches a bogus warning
209 // from GCC to make them protected: warning: `class StructType' only
210 // defines private constructors and has no friends
212 // Private ctor - Only can be created by a static member...
213 StructType(const std::vector<const Type*> &Types);
215 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
216 // another (more concrete) type, we must eliminate all references to other
217 // types, to avoid some circular reference problems. This also removes the
218 // type from the internal tables of available types.
219 virtual void dropAllTypeUses(bool inMap);
222 inline const ElementTypes &getElementTypes() const { return ETypes; }
224 virtual const Type *getContainedType(unsigned i) const {
225 return i < ETypes.size() ? ETypes[i].get() : 0;
227 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
229 // getTypeAtIndex - Given an index value into the type, return the type of the
230 // element. For a structure type, this must be a constant value...
232 virtual const Type *getTypeAtIndex(const Value *V) const ;
233 virtual bool indexValid(const Value *V) const;
235 // getIndexType - Return the type required of indices for this composite.
236 // For structures, this is ubyte, for arrays, this is uint
238 virtual const Type *getIndexType() const { return Type::UByteTy; }
240 // refineAbstractType - Called when a contained type is found to be more
241 // concrete - this could potentially change us from an abstract type to a
244 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
246 static StructType *get(const std::vector<const Type*> &Params);
248 // Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const StructType *T) { return true; }
250 static inline bool classof(const Type *T) {
251 return T->getPrimitiveID() == StructTyID;
253 static inline bool classof(const Value *V) {
254 return isa<Type>(V) && classof(cast<Type>(V));
259 // SequentialType - This is the superclass of the array and pointer type
260 // classes. Both of these represent "arrays" in memory. The array type
261 // represents a specifically sized array, pointer types are unsized/unknown size
262 // arrays. SequentialType holds the common features of both, which stem from
263 // the fact that both lay their components out in memory identically.
265 class SequentialType : public CompositeType {
266 SequentialType(const SequentialType &); // Do not implement!
267 const SequentialType &operator=(const SequentialType &); // Do not implement!
269 PATypeHandle ElementType;
271 SequentialType(PrimitiveID TID, const Type *ElType)
272 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
276 inline const Type *getElementType() const { return ElementType; }
278 virtual const Type *getContainedType(unsigned i) const {
279 return i == 0 ? ElementType.get() : 0;
281 virtual unsigned getNumContainedTypes() const { return 1; }
283 // getTypeAtIndex - Given an index value into the type, return the type of the
284 // element. For sequential types, there is only one subtype...
286 virtual const Type *getTypeAtIndex(const Value *V) const {
287 return ElementType.get();
289 virtual bool indexValid(const Value *V) const {
290 return V->getType() == Type::LongTy; // Must be a 'long' index
293 // getIndexType() - Return the type required of indices for this composite.
294 // For structures, this is ubyte, for arrays, this is uint
296 virtual const Type *getIndexType() const { return Type::LongTy; }
298 // Methods for support type inquiry through isa, cast, and dyn_cast:
299 static inline bool classof(const SequentialType *T) { return true; }
300 static inline bool classof(const Type *T) {
301 return T->getPrimitiveID() == ArrayTyID ||
302 T->getPrimitiveID() == PointerTyID;
304 static inline bool classof(const Value *V) {
305 return isa<Type>(V) && classof(cast<Type>(V));
310 class ArrayType : public SequentialType {
311 friend class TypeMap<ArrayValType, ArrayType>;
312 unsigned NumElements;
314 ArrayType(const ArrayType &); // Do not implement
315 const ArrayType &operator=(const ArrayType &); // Do not implement
317 // This should really be private, but it squelches a bogus warning
318 // from GCC to make them protected: warning: `class ArrayType' only
319 // defines private constructors and has no friends
321 // Private ctor - Only can be created by a static member...
322 ArrayType(const Type *ElType, unsigned NumEl);
324 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
325 // another (more concrete) type, we must eliminate all references to other
326 // types, to avoid some circular reference problems. This also removes the
327 // type from the internal tables of available types.
328 virtual void dropAllTypeUses(bool inMap);
331 inline unsigned getNumElements() const { return NumElements; }
333 // refineAbstractType - Called when a contained type is found to be more
334 // concrete - this could potentially change us from an abstract type to a
337 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
339 static ArrayType *get(const Type *ElementType, unsigned NumElements);
341 // Methods for support type inquiry through isa, cast, and dyn_cast:
342 static inline bool classof(const ArrayType *T) { return true; }
343 static inline bool classof(const Type *T) {
344 return T->getPrimitiveID() == ArrayTyID;
346 static inline bool classof(const Value *V) {
347 return isa<Type>(V) && classof(cast<Type>(V));
353 class PointerType : public SequentialType {
354 friend class TypeMap<PointerValType, PointerType>;
355 PointerType(const PointerType &); // Do not implement
356 const PointerType &operator=(const PointerType &); // Do not implement
358 // This should really be private, but it squelches a bogus warning
359 // from GCC to make them protected: warning: `class PointerType' only
360 // defines private constructors and has no friends
362 // Private ctor - Only can be created by a static member...
363 PointerType(const Type *ElType);
365 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
366 // another (more concrete) type, we must eliminate all references to other
367 // types, to avoid some circular reference problems. This also removes the
368 // type from the internal tables of available types.
369 virtual void dropAllTypeUses(bool inMap);
371 // PointerType::get - Named constructor for pointer types...
372 static PointerType *get(const Type *ElementType);
374 // refineAbstractType - Called when a contained type is found to be more
375 // concrete - this could potentially change us from an abstract type to a
378 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
380 // Methods for support type inquiry through isa, cast, and dyn_cast:
381 static inline bool classof(const PointerType *T) { return true; }
382 static inline bool classof(const Type *T) {
383 return T->getPrimitiveID() == PointerTyID;
385 static inline bool classof(const Value *V) {
386 return isa<Type>(V) && classof(cast<Type>(V));
391 class OpaqueType : public DerivedType {
392 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
393 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
395 // This should really be private, but it squelches a bogus warning
396 // from GCC to make them protected: warning: `class OpaqueType' only
397 // defines private constructors and has no friends
399 // Private ctor - Only can be created by a static member...
402 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
403 // another (more concrete) type, we must eliminate all references to other
404 // types, to avoid some circular reference problems.
405 virtual void dropAllTypeUses(bool inMap) {} // No type uses
409 // get - Static factory method for the OpaqueType class...
410 static OpaqueType *get() {
411 return new OpaqueType(); // All opaque types are distinct
414 // refineAbstractType - Called when a contained type is found to be more
415 // concrete - this could potentially change us from an abstract type to a
418 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
419 // This class never uses other types!
424 // Methods for support 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);