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 // AbstractTypeUsers - Implement a list of the users that need to be notified
18 // if I am a type, and I get resolved into a more concrete type.
20 ///// FIXME: kill mutable nonsense when Type's are not const
21 mutable vector<AbstractTypeUser *> AbstractTypeUsers;
23 char isRefining; // Used for recursive types
26 inline DerivedType(PrimitiveID id) : Type("", id) {
30 // typeIsRefined - Notify AbstractTypeUsers of this type that the current type
31 // has been refined a bit. The pointer is still valid and still should be
32 // used, but the subtypes have changed.
36 // setDerivedTypeProperties - Based on the subtypes, set the name of this
37 // type so that it is printed nicely by the type printer. Also calculate
38 // whether this type is abstract or not. Used by the constructor and when
39 // the type is refined.
41 void setDerivedTypeProperties();
45 //===--------------------------------------------------------------------===//
46 // Abstract Type handling methods - These types have special lifetimes, which
47 // are managed by (add|remove)AbstractTypeUser. See comments in
48 // AbstractTypeUser.h for more information.
50 // addAbstractTypeUser - Notify an abstract type that there is a new user of
51 // it. This function is called primarily by the PATypeHandle class.
53 void addAbstractTypeUser(AbstractTypeUser *U) const {
54 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
56 cerr << " addAbstractTypeUser[" << (void*)this << ", " << getDescription()
57 << "][" << AbstractTypeUsers.size() << "] User = " << U << endl;
59 AbstractTypeUsers.push_back(U);
62 // removeAbstractTypeUser - Notify an abstract type that a user of the class
63 // no longer has a handle to the type. This function is called primarily by
64 // the PATypeHandle class. When there are no users of the abstract type, it
65 // is anihilated, because there is no way to get a reference to it ever again.
67 void removeAbstractTypeUser(AbstractTypeUser *U) const;
69 // getNumAbstractTypeUsers - Return the number of users registered to the type
70 inline unsigned getNumAbstractTypeUsers() const {
71 assert(isAbstract() && "getNumAbstractTypeUsers: Type not abstract!");
72 return AbstractTypeUsers.size();
75 // refineAbstractTypeTo - This function is used to when it is discovered that
76 // the 'this' abstract type is actually equivalent to the NewType specified.
77 // This causes all users of 'this' to switch to reference the more concrete
78 // type NewType and for 'this' to be deleted.
80 void refineAbstractTypeTo(const Type *NewType);
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const DerivedType *T) { return true; }
84 static inline bool classof(const Type *T) {
85 return T->isDerivedType();
87 static inline bool classof(const Value *V) {
88 return isa<Type>(V) && classof(cast<const Type>(V));
95 class MethodType : public DerivedType {
97 typedef vector<PATypeHandle<Type> > ParamTypes;
99 PATypeHandle<Type> ResultType;
103 MethodType(const MethodType &); // Do not implement
104 const MethodType &operator=(const MethodType &); // Do not implement
106 // This should really be private, but it squelches a bogus warning
107 // from GCC to make them protected: warning: `class MethodType' only
108 // defines private constructors and has no friends
110 // Private ctor - Only can be created by a static member...
111 MethodType(const Type *Result, const vector<const Type*> &Params,
116 inline bool isVarArg() const { return isVarArgs; }
117 inline const Type *getReturnType() const { return ResultType; }
118 inline const ParamTypes &getParamTypes() const { return ParamTys; }
121 virtual const Type *getContainedType(unsigned i) const {
122 return i == 0 ? ResultType :
123 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
125 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
127 // refineAbstractType - Called when a contained type is found to be more
128 // concrete - this could potentially change us from an abstract type to a
131 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
133 static MethodType *get(const Type *Result, const vector<const Type*> &Params,
137 // Methods for support type inquiry through isa, cast, and dyn_cast:
138 static inline bool classof(const MethodType *T) { return true; }
139 static inline bool classof(const Type *T) {
140 return T->getPrimitiveID() == MethodTyID;
142 static inline bool classof(const Value *V) {
143 return isa<Type>(V) && classof(cast<const Type>(V));
148 // CompositeType - Common super class of ArrayType, StructType, and PointerType
150 class CompositeType : public DerivedType {
152 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
156 // getTypeAtIndex - Given an index value into the type, return the type of the
159 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
160 virtual bool indexValid(const Value *V) const = 0;
162 // getIndexType - Return the type required of indices for this composite.
163 // For structures, this is ubyte, for arrays, this is uint
165 virtual const Type *getIndexType() const = 0;
168 // Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const CompositeType *T) { return true; }
170 static inline bool classof(const Type *T) {
171 return T->getPrimitiveID() == ArrayTyID ||
172 T->getPrimitiveID() == StructTyID ||
173 T->getPrimitiveID() == PointerTyID;
175 static inline bool classof(const Value *V) {
176 return isa<Type>(V) && classof(cast<const Type>(V));
181 class StructType : public CompositeType {
183 typedef vector<PATypeHandle<Type> > ElementTypes;
186 ElementTypes ETypes; // Element types of struct
188 StructType(const StructType &); // Do not implement
189 const StructType &operator=(const StructType &); // Do not implement
192 // This should really be private, but it squelches a bogus warning
193 // from GCC to make them protected: warning: `class StructType' only
194 // defines private constructors and has no friends
196 // Private ctor - Only can be created by a static member...
197 StructType(const vector<const Type*> &Types);
200 inline const ElementTypes &getElementTypes() const { return ETypes; }
202 virtual const Type *getContainedType(unsigned i) const {
203 return i < ETypes.size() ? ETypes[i].get() : 0;
205 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
207 // getTypeAtIndex - Given an index value into the type, return the type of the
208 // element. For a structure type, this must be a constant value...
210 virtual const Type *getTypeAtIndex(const Value *V) const ;
211 virtual bool indexValid(const Value *V) const;
213 // getIndexType - Return the type required of indices for this composite.
214 // For structures, this is ubyte, for arrays, this is uint
216 virtual const Type *getIndexType() const { return Type::UByteTy; }
218 // refineAbstractType - Called when a contained type is found to be more
219 // concrete - this could potentially change us from an abstract type to a
222 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
224 static StructType *get(const vector<const Type*> &Params);
226 // Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const StructType *T) { return true; }
228 static inline bool classof(const Type *T) {
229 return T->getPrimitiveID() == StructTyID;
231 static inline bool classof(const Value *V) {
232 return isa<Type>(V) && classof(cast<const Type>(V));
237 // SequentialType - This is the superclass of the array and pointer type
238 // classes. Both of these represent "arrays" in memory. The array type
239 // represents a specifically sized array, pointer types are unsized/unknown size
240 // arrays. SequentialType holds the common features of both, which stem from
241 // the fact that both lay their components out in memory identically.
243 class SequentialType : public CompositeType {
244 SequentialType(const SequentialType &); // Do not implement!
245 const SequentialType &operator=(const SequentialType &); // Do not implement!
247 PATypeHandle<Type> ElementType;
249 SequentialType(PrimitiveID TID, const Type *ElType)
250 : CompositeType(TID), ElementType(PATypeHandle<Type>(ElType, this)) {
254 inline const Type *getElementType() const { return ElementType; }
256 virtual const Type *getContainedType(unsigned i) const {
257 return i == 0 ? ElementType.get() : 0;
259 virtual unsigned getNumContainedTypes() const { return 1; }
261 // getTypeAtIndex - Given an index value into the type, return the type of the
262 // element. For sequential types, there is only one subtype...
264 virtual const Type *getTypeAtIndex(const Value *V) const {
265 return ElementType.get();
267 virtual bool indexValid(const Value *V) const {
268 return V->getType() == Type::UIntTy; // Must be an unsigned int index
271 // getIndexType() - Return the type required of indices for this composite.
272 // For structures, this is ubyte, for arrays, this is uint
274 virtual const Type *getIndexType() const { return Type::UIntTy; }
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const SequentialType *T) { return true; }
278 static inline bool classof(const Type *T) {
279 return T->getPrimitiveID() == ArrayTyID ||
280 T->getPrimitiveID() == PointerTyID;
282 static inline bool classof(const Value *V) {
283 return isa<Type>(V) && classof(cast<const Type>(V));
288 class ArrayType : public SequentialType {
289 unsigned NumElements;
291 ArrayType(const ArrayType &); // Do not implement
292 const ArrayType &operator=(const ArrayType &); // Do not implement
294 // This should really be private, but it squelches a bogus warning
295 // from GCC to make them protected: warning: `class ArrayType' only
296 // defines private constructors and has no friends
299 // Private ctor - Only can be created by a static member...
300 ArrayType(const Type *ElType, unsigned NumEl);
302 inline unsigned getNumElements() const { return NumElements; }
304 // refineAbstractType - Called when a contained type is found to be more
305 // concrete - this could potentially change us from an abstract type to a
308 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
310 static ArrayType *get(const Type *ElementType, unsigned NumElements);
312 // Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const ArrayType *T) { return true; }
314 static inline bool classof(const Type *T) {
315 return T->getPrimitiveID() == ArrayTyID;
317 static inline bool classof(const Value *V) {
318 return isa<Type>(V) && classof(cast<const Type>(V));
324 class PointerType : public SequentialType {
325 PointerType(const PointerType &); // Do not implement
326 const PointerType &operator=(const PointerType &); // Do not implement
328 // This should really be private, but it squelches a bogus warning
329 // from GCC to make them protected: warning: `class PointerType' only
330 // defines private constructors and has no friends
333 // Private ctor - Only can be created by a static member...
334 PointerType(const Type *ElType);
336 // PointerType::get - Named constructor for pointer types...
337 static PointerType *get(const Type *ElementType);
339 // refineAbstractType - Called when a contained type is found to be more
340 // concrete - this could potentially change us from an abstract type to a
343 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
345 // Methods for support type inquiry through isa, cast, and dyn_cast:
346 static inline bool classof(const PointerType *T) { return true; }
347 static inline bool classof(const Type *T) {
348 return T->getPrimitiveID() == PointerTyID;
350 static inline bool classof(const Value *V) {
351 return isa<Type>(V) && classof(cast<const Type>(V));
356 class OpaqueType : public DerivedType {
358 OpaqueType(const OpaqueType &); // Do not implement
359 const OpaqueType &operator=(const OpaqueType &); // Do not implement
361 // This should really be private, but it squelches a bogus warning
362 // from GCC to make them protected: warning: `class OpaqueType' only
363 // defines private constructors and has no friends
365 // Private ctor - Only can be created by a static member...
370 // get - Static factory method for the OpaqueType class...
371 static OpaqueType *get() {
372 return new OpaqueType(); // All opaque types are distinct
375 // Methods for support type inquiry through isa, cast, and dyn_cast:
376 static inline bool classof(const OpaqueType *T) { return true; }
377 static inline bool classof(const Type *T) {
378 return T->getPrimitiveID() == OpaqueTyID;
380 static inline bool classof(const Value *V) {
381 return isa<Type>(V) && classof(cast<const Type>(V));
386 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
387 // These are defined here because they MUST be inlined, yet are dependant on
388 // the definition of the Type class. Of course Type derives from Value, which
389 // contains an AbstractTypeUser instance, so there is no good way to factor out
390 // the code. Hence this bit of uglyness.
392 template <class TypeSubClass> void PATypeHandle<TypeSubClass>::addUser() {
393 assert(Ty && "Type Handle has a null type!");
394 if (Ty->isAbstract())
395 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
397 template <class TypeSubClass> void PATypeHandle<TypeSubClass>::removeUser() {
398 if (Ty->isAbstract())
399 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
402 template <class TypeSubClass>
403 void PATypeHandle<TypeSubClass>::removeUserFromConcrete() {
404 if (!Ty->isAbstract())
405 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);