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 // setDerivedTypeProperties - Based on the subtypes, set the name of this
40 // type so that it is printed nicely by the type printer. Also calculate
41 // whether this type is abstract or not. Used by the constructor and when
42 // the type is refined.
44 void setDerivedTypeProperties();
48 //===--------------------------------------------------------------------===//
49 // Abstract Type handling methods - These types have special lifetimes, which
50 // are managed by (add|remove)AbstractTypeUser. See comments in
51 // AbstractTypeUser.h for more information.
53 // addAbstractTypeUser - Notify an abstract type that there is a new user of
54 // it. This function is called primarily by the PATypeHandle class.
56 void addAbstractTypeUser(AbstractTypeUser *U) const;
58 // removeAbstractTypeUser - Notify an abstract type that a user of the class
59 // no longer has a handle to the type. This function is called primarily by
60 // the PATypeHandle class. When there are no users of the abstract type, it
61 // is anihilated, because there is no way to get a reference to it ever again.
63 void removeAbstractTypeUser(AbstractTypeUser *U) const;
65 // refineAbstractTypeTo - This function is used to when it is discovered that
66 // the 'this' abstract type is actually equivalent to the NewType specified.
67 // This causes all users of 'this' to switch to reference the more concrete
68 // type NewType and for 'this' to be deleted.
70 void refineAbstractTypeTo(const Type *NewType);
72 // Methods for support type inquiry through isa, cast, and dyn_cast:
73 static inline bool classof(const DerivedType *T) { return true; }
74 static inline bool classof(const Type *T) {
75 return T->isDerivedType();
77 static inline bool classof(const Value *V) {
78 return isa<Type>(V) && classof(cast<Type>(V));
85 class FunctionType : public DerivedType {
87 typedef std::vector<PATypeHandle> ParamTypes;
89 PATypeHandle ResultType;
93 FunctionType(const FunctionType &); // Do not implement
94 const FunctionType &operator=(const FunctionType &); // Do not implement
96 // This should really be private, but it squelches a bogus warning
97 // from GCC to make them protected: warning: `class FunctionType' only
98 // defines private constructors and has no friends
100 // Private ctor - Only can be created by a static member...
101 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
106 inline bool isVarArg() const { return isVarArgs; }
107 inline const Type *getReturnType() const { return ResultType; }
108 inline const ParamTypes &getParamTypes() const { return ParamTys; }
110 // Parameter type accessors...
111 const Type *getParamType(unsigned i) const { return ParamTys[i]; }
113 // getNumParams - Return the number of fixed parameters this function type
114 // requires. This does not consider varargs.
116 unsigned getNumParams() const { return ParamTys.size(); }
119 virtual const Type *getContainedType(unsigned i) const {
120 return i == 0 ? ResultType :
121 (i <= ParamTys.size() ? ParamTys[i-1].get() : 0);
123 virtual unsigned getNumContainedTypes() const { return ParamTys.size()+1; }
125 // refineAbstractType - Called when a contained type is found to be more
126 // concrete - this could potentially change us from an abstract type to a
129 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
131 static FunctionType *get(const Type *Result,
132 const std::vector<const Type*> &Params,
136 // Methods for support type inquiry through isa, cast, and dyn_cast:
137 static inline bool classof(const FunctionType *T) { return true; }
138 static inline bool classof(const Type *T) {
139 return T->getPrimitiveID() == FunctionTyID;
141 static inline bool classof(const Value *V) {
142 return isa<Type>(V) && classof(cast<Type>(V));
147 // CompositeType - Common super class of ArrayType, StructType, and PointerType
149 class CompositeType : public DerivedType {
151 inline CompositeType(PrimitiveID id) : DerivedType(id) { }
155 // getTypeAtIndex - Given an index value into the type, return the type of the
158 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
159 virtual bool indexValid(const Value *V) const = 0;
161 // getIndexType - Return the type required of indices for this composite.
162 // For structures, this is ubyte, for arrays, this is uint
164 virtual const Type *getIndexType() const = 0;
167 // Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const CompositeType *T) { return true; }
169 static inline bool classof(const Type *T) {
170 return T->getPrimitiveID() == ArrayTyID ||
171 T->getPrimitiveID() == StructTyID ||
172 T->getPrimitiveID() == PointerTyID;
174 static inline bool classof(const Value *V) {
175 return isa<Type>(V) && classof(cast<Type>(V));
180 class StructType : public CompositeType {
182 typedef std::vector<PATypeHandle> ElementTypes;
185 ElementTypes ETypes; // Element types of struct
187 StructType(const StructType &); // Do not implement
188 const StructType &operator=(const StructType &); // Do not implement
191 // This should really be private, but it squelches a bogus warning
192 // from GCC to make them protected: warning: `class StructType' only
193 // defines private constructors and has no friends
195 // Private ctor - Only can be created by a static member...
196 StructType(const std::vector<const Type*> &Types);
199 inline const ElementTypes &getElementTypes() const { return ETypes; }
201 virtual const Type *getContainedType(unsigned i) const {
202 return i < ETypes.size() ? ETypes[i].get() : 0;
204 virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
206 // getTypeAtIndex - Given an index value into the type, return the type of the
207 // element. For a structure type, this must be a constant value...
209 virtual const Type *getTypeAtIndex(const Value *V) const ;
210 virtual bool indexValid(const Value *V) const;
212 // getIndexType - Return the type required of indices for this composite.
213 // For structures, this is ubyte, for arrays, this is uint
215 virtual const Type *getIndexType() const { return Type::UByteTy; }
217 // refineAbstractType - Called when a contained type is found to be more
218 // concrete - this could potentially change us from an abstract type to a
221 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
223 static StructType *get(const std::vector<const Type*> &Params);
225 // Methods for support type inquiry through isa, cast, and dyn_cast:
226 static inline bool classof(const StructType *T) { return true; }
227 static inline bool classof(const Type *T) {
228 return T->getPrimitiveID() == StructTyID;
230 static inline bool classof(const Value *V) {
231 return isa<Type>(V) && classof(cast<Type>(V));
236 // SequentialType - This is the superclass of the array and pointer type
237 // classes. Both of these represent "arrays" in memory. The array type
238 // represents a specifically sized array, pointer types are unsized/unknown size
239 // arrays. SequentialType holds the common features of both, which stem from
240 // the fact that both lay their components out in memory identically.
242 class SequentialType : public CompositeType {
243 SequentialType(const SequentialType &); // Do not implement!
244 const SequentialType &operator=(const SequentialType &); // Do not implement!
246 PATypeHandle ElementType;
248 SequentialType(PrimitiveID TID, const Type *ElType)
249 : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) {
253 inline const Type *getElementType() const { return ElementType; }
255 virtual const Type *getContainedType(unsigned i) const {
256 return i == 0 ? ElementType.get() : 0;
258 virtual unsigned getNumContainedTypes() const { return 1; }
260 // getTypeAtIndex - Given an index value into the type, return the type of the
261 // element. For sequential types, there is only one subtype...
263 virtual const Type *getTypeAtIndex(const Value *V) const {
264 return ElementType.get();
266 virtual bool indexValid(const Value *V) const {
267 return V->getType() == Type::LongTy; // Must be a 'long' index
270 // getIndexType() - Return the type required of indices for this composite.
271 // For structures, this is ubyte, for arrays, this is uint
273 virtual const Type *getIndexType() const { return Type::LongTy; }
275 // Methods for support type inquiry through isa, cast, and dyn_cast:
276 static inline bool classof(const SequentialType *T) { return true; }
277 static inline bool classof(const Type *T) {
278 return T->getPrimitiveID() == ArrayTyID ||
279 T->getPrimitiveID() == PointerTyID;
281 static inline bool classof(const Value *V) {
282 return isa<Type>(V) && classof(cast<Type>(V));
287 class ArrayType : public SequentialType {
288 unsigned NumElements;
290 ArrayType(const ArrayType &); // Do not implement
291 const ArrayType &operator=(const ArrayType &); // Do not implement
293 // This should really be private, but it squelches a bogus warning
294 // from GCC to make them protected: warning: `class ArrayType' only
295 // defines private constructors and has no friends
298 // Private ctor - Only can be created by a static member...
299 ArrayType(const Type *ElType, unsigned NumEl);
301 inline unsigned getNumElements() const { return NumElements; }
303 // refineAbstractType - Called when a contained type is found to be more
304 // concrete - this could potentially change us from an abstract type to a
307 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
309 static ArrayType *get(const Type *ElementType, unsigned NumElements);
311 // Methods for support type inquiry through isa, cast, and dyn_cast:
312 static inline bool classof(const ArrayType *T) { return true; }
313 static inline bool classof(const Type *T) {
314 return T->getPrimitiveID() == ArrayTyID;
316 static inline bool classof(const Value *V) {
317 return isa<Type>(V) && classof(cast<Type>(V));
323 class PointerType : public SequentialType {
324 PointerType(const PointerType &); // Do not implement
325 const PointerType &operator=(const PointerType &); // Do not implement
327 // This should really be private, but it squelches a bogus warning
328 // from GCC to make them protected: warning: `class PointerType' only
329 // defines private constructors and has no friends
332 // Private ctor - Only can be created by a static member...
333 PointerType(const Type *ElType);
335 // PointerType::get - Named constructor for pointer types...
336 static PointerType *get(const Type *ElementType);
338 // refineAbstractType - Called when a contained type is found to be more
339 // concrete - this could potentially change us from an abstract type to a
342 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
344 // Methods for support type inquiry through isa, cast, and dyn_cast:
345 static inline bool classof(const PointerType *T) { return true; }
346 static inline bool classof(const Type *T) {
347 return T->getPrimitiveID() == PointerTyID;
349 static inline bool classof(const Value *V) {
350 return isa<Type>(V) && classof(cast<Type>(V));
355 class OpaqueType : public DerivedType {
357 OpaqueType(const OpaqueType &); // Do not implement
358 const OpaqueType &operator=(const OpaqueType &); // Do not implement
360 // This should really be private, but it squelches a bogus warning
361 // from GCC to make them protected: warning: `class OpaqueType' only
362 // defines private constructors and has no friends
364 // Private ctor - Only can be created by a static member...
369 // get - Static factory method for the OpaqueType class...
370 static OpaqueType *get() {
371 return new OpaqueType(); // All opaque types are distinct
374 // Methods for support type inquiry through isa, cast, and dyn_cast:
375 static inline bool classof(const OpaqueType *T) { return true; }
376 static inline bool classof(const Type *T) {
377 return T->getPrimitiveID() == OpaqueTyID;
379 static inline bool classof(const Value *V) {
380 return isa<Type>(V) && classof(cast<Type>(V));
385 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
386 // These are defined here because they MUST be inlined, yet are dependent on
387 // the definition of the Type class. Of course Type derives from Value, which
388 // contains an AbstractTypeUser instance, so there is no good way to factor out
389 // the code. Hence this bit of uglyness.
391 inline void PATypeHandle::addUser() {
392 assert(Ty && "Type Handle has a null type!");
393 if (Ty->isAbstract())
394 cast<DerivedType>(Ty)->addAbstractTypeUser(User);
396 inline void PATypeHandle::removeUser() {
397 if (Ty->isAbstract())
398 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);
401 inline void PATypeHandle::removeUserFromConcrete() {
402 if (!Ty->isAbstract())
403 cast<DerivedType>(Ty)->removeAbstractTypeUser(User);