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 class DerivedType : public Type {
17 char isRefining; // Used for recursive types
19 // AbstractTypeUsers - Implement a list of the users that need to be notified
20 // if I am a type, and I get resolved into a more concrete type.
22 ///// FIXME: kill mutable nonsense when Type's are not const
23 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
26 inline DerivedType(PrimitiveID id) : Type("", id) {
30 assert(AbstractTypeUsers.empty());
33 // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
34 // has been refined a bit. The pointer is still valid and still should be
35 // used, but the subtypes have changed.
39 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
40 // another (more concrete) type, we must eliminate all references to other
41 // types, to avoid some circular reference problems. This also removes the
42 // type from the internal tables of available types.
43 virtual void dropAllTypeUses(bool inMap) = 0;
46 void refineAbstractTypeToInternal(const Type *NewType, bool inMap);
50 //===--------------------------------------------------------------------===//
51 // Abstract Type handling methods - These types have special lifetimes, which
52 // are managed by (add|remove)AbstractTypeUser. See comments in
53 // AbstractTypeUser.h for more information.
55 // addAbstractTypeUser - Notify an abstract type that there is a new user of
56 // it. This function is called primarily by the PATypeHandle class.
58 void addAbstractTypeUser(AbstractTypeUser *U) const;
60 // removeAbstractTypeUser - Notify an abstract type that a user of the class
61 // no longer has a handle to the type. This function is called primarily by
62 // the PATypeHandle class. When there are no users of the abstract type, it
63 // is annihilated, because there is no way to get a reference to it ever
66 void removeAbstractTypeUser(AbstractTypeUser *U) const;
68 // refineAbstractTypeTo - This function is used to when it is discovered that
69 // the 'this' abstract type is actually equivalent to the NewType specified.
70 // This causes all users of 'this' to switch to reference the more concrete
71 // type NewType and for 'this' to be deleted.
73 void refineAbstractTypeTo(const Type *NewType) {
74 refineAbstractTypeToInternal(NewType, true);
77 // Methods for support type inquiry through isa, cast, and dyn_cast:
78 static inline bool classof(const DerivedType *T) { return true; }
79 static inline bool classof(const Type *T) {
80 return T->isDerivedType();
82 static inline bool classof(const Value *V) {
83 return isa<Type>(V) && classof(cast<Type>(V));
90 struct FunctionType : public DerivedType {
91 typedef std::vector<PATypeHandle> ParamTypes;
93 PATypeHandle ResultType;
97 FunctionType(const FunctionType &); // Do not implement
98 const FunctionType &operator=(const FunctionType &); // Do not implement
100 // This should really be private, but it squelches a bogus warning
101 // from GCC to make them protected: warning: `class FunctionType' only
102 // defines private constructors and has no friends
104 // Private ctor - Only can be created by a static member...
105 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
108 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
109 // another (more concrete) type, we must eliminate all references to other
110 // types, to avoid some circular reference problems. This also removes the
111 // type from the internal tables of available types.
112 virtual void dropAllTypeUses(bool inMap);
116 inline bool isVarArg() const { return isVarArgs; }
117 inline const Type *getReturnType() const { return ResultType; }
118 inline const ParamTypes &getParamTypes() const { return ParamTys; }
120 // Parameter type accessors...
121 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
123 // getNumParams - Return the number of fixed parameters this function type
124 // requires. This does not consider varargs.
126 unsigned getNumParams() const { return ParamTys.size(); }
129 virtual const Type *getContainedType(unsigned i) const {
130 return i == 0 ? ResultType :
131 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
133 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
135 // refineAbstractType - Called when a contained type is found to be more
136 // concrete - this could potentially change us from an abstract type to a
139 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
141 static FunctionType *get(const Type *Result,
142 const std::vector<const Type*> &Params,
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const FunctionType *T) { return true; }
148 static inline bool classof(const Type *T) {
149 return T->getPrimitiveID() == FunctionTyID;
151 static inline bool classof(const Value *V) {
152 return isa<Type>(V) && classof(cast<Type>(V));
157 // CompositeType - Common super class of ArrayType, StructType, and PointerType
159 class CompositeType : public DerivedType {
161 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
164 // getTypeAtIndex - Given an index value into the type, return the type of the
167 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
168 virtual bool indexValid(const Value *V) const = 0;
170 // getIndexType - Return the type required of indices for this composite.
171 // For structures, this is ubyte, for arrays, this is uint
173 virtual const Type *getIndexType() const = 0;
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const CompositeType *T) { return true; }
178 static inline bool classof(const Type *T) {
179 return T->getPrimitiveID() == ArrayTyID ||
180 T->getPrimitiveID() == StructTyID ||
181 T->getPrimitiveID() == PointerTyID;
183 static inline bool classof(const Value *V) {
184 return isa<Type>(V) && classof(cast<Type>(V));
189 class StructType : public CompositeType {
191 typedef std::vector<PATypeHandle> ElementTypes;
194 ElementTypes ETypes; // Element types of struct
196 StructType(const StructType &); // Do not implement
197 const StructType &operator=(const StructType &); // Do not implement
200 // This should really be private, but it squelches a bogus warning
201 // from GCC to make them protected: warning: `class StructType' only
202 // defines private constructors and has no friends
204 // Private ctor - Only can be created by a static member...
205 StructType(const std::vector<const Type*> &Types);
207 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
208 // another (more concrete) type, we must eliminate all references to other
209 // types, to avoid some circular reference problems. This also removes the
210 // type from the internal tables of available types.
211 virtual void dropAllTypeUses(bool inMap);
214 inline const ElementTypes &getElementTypes() const { return ETypes; }
216 virtual const Type *getContainedType(unsigned i) const {
217 return i < ETypes.size() ? ETypes[i].get() : 0;
219 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
221 // getTypeAtIndex - Given an index value into the type, return the type of the
222 // element. For a structure type, this must be a constant value...
224 virtual const Type *getTypeAtIndex(const Value *V) const ;
225 virtual bool indexValid(const Value *V) const;
227 // getIndexType - Return the type required of indices for this composite.
228 // For structures, this is ubyte, for arrays, this is uint
230 virtual const Type *getIndexType() const { return Type::UByteTy; }
232 // refineAbstractType - Called when a contained type is found to be more
233 // concrete - this could potentially change us from an abstract type to a
236 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
238 static StructType *get(const std::vector<const Type*> &Params);
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 PATypeHandle ElementType;
263 SequentialType(PrimitiveID TID, const Type *ElType)
264 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
268 inline const Type *getElementType() const { return ElementType; }
270 virtual const Type *getContainedType(unsigned i) const {
271 return i == 0 ? ElementType.get() : 0;
273 virtual unsigned getNumContainedTypes() const { return 1; }
275 // getTypeAtIndex - Given an index value into the type, return the type of the
276 // element. For sequential types, there is only one subtype...
278 virtual const Type *getTypeAtIndex(const Value *V) const {
279 return ElementType.get();
281 virtual bool indexValid(const Value *V) const {
282 return V->getType() == Type::LongTy; // Must be a 'long' index
285 // getIndexType() - Return the type required of indices for this composite.
286 // For structures, this is ubyte, for arrays, this is uint
288 virtual const Type *getIndexType() const { return Type::LongTy; }
290 // Methods for support type inquiry through isa, cast, and dyn_cast:
291 static inline bool classof(const SequentialType *T) { return true; }
292 static inline bool classof(const Type *T) {
293 return T->getPrimitiveID() == ArrayTyID ||
294 T->getPrimitiveID() == PointerTyID;
296 static inline bool classof(const Value *V) {
297 return isa<Type>(V) && classof(cast<Type>(V));
302 class ArrayType : public SequentialType {
303 unsigned NumElements;
305 ArrayType(const ArrayType &); // Do not implement
306 const ArrayType &operator=(const ArrayType &); // Do not implement
308 // This should really be private, but it squelches a bogus warning
309 // from GCC to make them protected: warning: `class ArrayType' only
310 // defines private constructors and has no friends
312 // Private ctor - Only can be created by a static member...
313 ArrayType(const Type *ElType, unsigned NumEl);
315 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
316 // another (more concrete) type, we must eliminate all references to other
317 // types, to avoid some circular reference problems. This also removes the
318 // type from the internal tables of available types.
319 virtual void dropAllTypeUses(bool inMap);
322 inline unsigned getNumElements() const { return NumElements; }
324 // refineAbstractType - Called when a contained type is found to be more
325 // concrete - this could potentially change us from an abstract type to a
328 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
330 static ArrayType *get(const Type *ElementType, unsigned NumElements);
332 // Methods for support type inquiry through isa, cast, and dyn_cast:
333 static inline bool classof(const ArrayType *T) { return true; }
334 static inline bool classof(const Type *T) {
335 return T->getPrimitiveID() == ArrayTyID;
337 static inline bool classof(const Value *V) {
338 return isa<Type>(V) && classof(cast<Type>(V));
344 class PointerType : public SequentialType {
345 PointerType(const PointerType &); // Do not implement
346 const PointerType &operator=(const PointerType &); // Do not implement
348 // This should really be private, but it squelches a bogus warning
349 // from GCC to make them protected: warning: `class PointerType' only
350 // defines private constructors and has no friends
352 // Private ctor - Only can be created by a static member...
353 PointerType(const Type *ElType);
355 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
356 // another (more concrete) type, we must eliminate all references to other
357 // types, to avoid some circular reference problems. This also removes the
358 // type from the internal tables of available types.
359 virtual void dropAllTypeUses(bool inMap);
361 // PointerType::get - Named constructor for pointer types...
362 static PointerType *get(const Type *ElementType);
364 // refineAbstractType - Called when a contained type is found to be more
365 // concrete - this could potentially change us from an abstract type to a
368 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
370 // Methods for support type inquiry through isa, cast, and dyn_cast:
371 static inline bool classof(const PointerType *T) { return true; }
372 static inline bool classof(const Type *T) {
373 return T->getPrimitiveID() == PointerTyID;
375 static inline bool classof(const Value *V) {
376 return isa<Type>(V) && classof(cast<Type>(V));
381 class OpaqueType : public DerivedType {
382 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
383 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
385 // This should really be private, but it squelches a bogus warning
386 // from GCC to make them protected: warning: `class OpaqueType' only
387 // defines private constructors and has no friends
389 // Private ctor - Only can be created by a static member...
392 // dropAllTypeUses - When this (abstract) type is resolved to be equal to
393 // another (more concrete) type, we must eliminate all references to other
394 // types, to avoid some circular reference problems.
395 virtual void dropAllTypeUses(bool inMap) {} // No type uses
399 // get - Static factory method for the OpaqueType class...
400 static OpaqueType *get() {
401 return new OpaqueType(); // All opaque types are distinct
404 // Methods for support type inquiry through isa, cast, and dyn_cast:
405 static inline bool classof(const OpaqueType *T) { return true; }
406 static inline bool classof(const Type *T) {
407 return T->getPrimitiveID() == OpaqueTyID;
409 static inline bool classof(const Value *V) {
410 return isa<Type>(V) && classof(cast<Type>(V));
415 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
416 // These are defined here because they MUST be inlined, yet are dependent on
417 // the definition of the Type class. Of course Type derives from Value, which
418 // contains an AbstractTypeUser instance, so there is no good way to factor out
419 // the code. Hence this bit of uglyness.
421 inline void PATypeHandle::addUser() {
422 assert(Ty && "Type Handle has a null type!");
423 if (Ty->isAbstract())
424 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
426 inline void PATypeHandle::removeUser() {
427 if (Ty->isAbstract())
428 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
431 inline void PATypeHandle::removeUserFromConcrete() {
432 if (!Ty->isAbstract())
433 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);