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 // AbstractTypeUsers - Implement a list of the users that need to be notified
33 // if I am a type, and I get resolved into a more concrete type.
35 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
38 DerivedType(TypeID id) : Type("", id) {}
40 assert(AbstractTypeUsers.empty());
43 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
44 /// that the current type has transitioned from being abstract to being
47 void notifyUsesThatTypeBecameConcrete();
49 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
50 /// another (more concrete) type, we must eliminate all references to other
51 /// types, to avoid some circular reference problems.
53 void dropAllTypeUses();
55 void RefCountIsZero() const {
56 if (AbstractTypeUsers.empty())
63 //===--------------------------------------------------------------------===//
64 // Abstract Type handling methods - These types have special lifetimes, which
65 // are managed by (add|remove)AbstractTypeUser. See comments in
66 // AbstractTypeUser.h for more information.
68 /// addAbstractTypeUser - Notify an abstract type that there is a new user of
69 /// it. This function is called primarily by the PATypeHandle class.
71 void addAbstractTypeUser(AbstractTypeUser *U) const {
72 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
73 AbstractTypeUsers.push_back(U);
76 /// removeAbstractTypeUser - Notify an abstract type that a user of the class
77 /// no longer has a handle to the type. This function is called primarily by
78 /// the PATypeHandle class. When there are no users of the abstract type, it
79 /// is annihilated, because there is no way to get a reference to it ever
82 void removeAbstractTypeUser(AbstractTypeUser *U) const;
84 /// refineAbstractTypeTo - This function is used to when it is discovered that
85 /// the 'this' abstract type is actually equivalent to the NewType specified.
86 /// This causes all users of 'this' to switch to reference the more concrete
87 /// type NewType and for 'this' to be deleted.
89 void refineAbstractTypeTo(const Type *NewType);
91 void dump() const { Type::dump(); }
93 // Methods for support type inquiry through isa, cast, and dyn_cast:
94 static inline bool classof(const DerivedType *T) { return true; }
95 static inline bool classof(const Type *T) {
96 return T->isDerivedType();
98 static inline bool classof(const Value *V) {
99 return isa<Type>(V) && classof(cast<Type>(V));
104 /// FunctionType - Class to represent function types
106 class FunctionType : public DerivedType {
107 friend class TypeMap<FunctionValType, FunctionType>;
110 FunctionType(const FunctionType &); // Do not implement
111 const FunctionType &operator=(const FunctionType &); // Do not implement
113 /// This should really be private, but it squelches a bogus warning
114 /// from GCC to make them protected: warning: `class FunctionType' only
115 /// defines private constructors and has no friends
117 /// Private ctor - Only can be created by a static member...
119 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
123 /// FunctionType::get - This static method is the primary way of constructing
126 static FunctionType *get(const Type *Result,
127 const std::vector<const Type*> &Params,
130 inline bool isVarArg() const { return isVarArgs; }
131 inline const Type *getReturnType() const { return ContainedTys[0]; }
133 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
134 param_iterator param_begin() const { return ContainedTys.begin()+1; }
135 param_iterator param_end() const { return ContainedTys.end(); }
137 // Parameter type accessors...
138 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
140 /// getNumParams - Return the number of fixed parameters this function type
141 /// requires. This does not consider varargs.
143 unsigned getNumParams() const { return ContainedTys.size()-1; }
145 // Implement the AbstractTypeUser interface.
146 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
147 virtual void typeBecameConcrete(const DerivedType *AbsTy);
149 // Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const FunctionType *T) { return true; }
151 static inline bool classof(const Type *T) {
152 return T->getTypeID() == FunctionTyID;
154 static inline bool classof(const Value *V) {
155 return isa<Type>(V) && classof(cast<Type>(V));
160 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
162 class CompositeType : public DerivedType {
164 inline CompositeType(TypeID id) : DerivedType(id) { }
167 /// getTypeAtIndex - Given an index value into the type, return the type of
170 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
171 virtual bool indexValid(const Value *V) const = 0;
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const CompositeType *T) { return true; }
175 static inline bool classof(const Type *T) {
176 return T->getTypeID() == ArrayTyID ||
177 T->getTypeID() == StructTyID ||
178 T->getTypeID() == PointerTyID;
180 static inline bool classof(const Value *V) {
181 return isa<Type>(V) && classof(cast<Type>(V));
186 /// StructType - Class to represent struct types
188 class StructType : public CompositeType {
189 friend class TypeMap<StructValType, StructType>;
190 StructType(const StructType &); // Do not implement
191 const StructType &operator=(const StructType &); // Do not implement
194 /// This should really be private, but it squelches a bogus warning
195 /// from GCC to make them protected: warning: `class StructType' only
196 /// defines private constructors and has no friends
198 /// Private ctor - Only can be created by a static member...
200 StructType(const std::vector<const Type*> &Types);
203 /// StructType::get - This static method is the primary way to create a
206 static StructType *get(const std::vector<const Type*> &Params);
208 // Iterator access to the elements
209 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
210 element_iterator element_begin() const { return ContainedTys.begin(); }
211 element_iterator element_end() const { return ContainedTys.end(); }
213 // Random access to the elements
214 unsigned getNumElements() const { return ContainedTys.size(); }
215 const Type *getElementType(unsigned N) const {
216 assert(N < ContainedTys.size() && "Element number out of range!");
217 return ContainedTys[N];
220 /// getTypeAtIndex - Given an index value into the type, return the type of
221 /// the element. For a structure type, this must be a constant value...
223 virtual const Type *getTypeAtIndex(const Value *V) const ;
224 virtual bool indexValid(const Value *V) const;
226 // Implement the AbstractTypeUser interface.
227 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
228 virtual void typeBecameConcrete(const DerivedType *AbsTy);
230 // Methods for support type inquiry through isa, cast, and dyn_cast:
231 static inline bool classof(const StructType *T) { return true; }
232 static inline bool classof(const Type *T) {
233 return T->getTypeID() == StructTyID;
235 static inline bool classof(const Value *V) {
236 return isa<Type>(V) && classof(cast<Type>(V));
241 /// SequentialType - This is the superclass of the array and pointer type
242 /// classes. Both of these represent "arrays" in memory. The array type
243 /// represents a specifically sized array, pointer types are unsized/unknown
244 /// size arrays. SequentialType holds the common features of both, which stem
245 /// from the fact that both lay their components out in memory identically.
247 class SequentialType : public CompositeType {
248 SequentialType(const SequentialType &); // Do not implement!
249 const SequentialType &operator=(const SequentialType &); // Do not implement!
251 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
252 ContainedTys.reserve(1);
253 ContainedTys.push_back(PATypeHandle(ElType, this));
257 inline const Type *getElementType() const { return ContainedTys[0]; }
259 /// getTypeAtIndex - Given an index value into the type, return the type of
260 /// the element. For sequential types, there is only one subtype...
262 virtual const Type *getTypeAtIndex(const Value *V) const {
263 return ContainedTys[0];
265 virtual bool indexValid(const Value *V) const {
266 const Type *Ty = V->getType();
267 switch (Ty->getTypeID()) {
271 case Type::ULongTyID:
278 // Methods for support type inquiry through isa, cast, and dyn_cast:
279 static inline bool classof(const SequentialType *T) { return true; }
280 static inline bool classof(const Type *T) {
281 return T->getTypeID() == ArrayTyID ||
282 T->getTypeID() == PointerTyID;
284 static inline bool classof(const Value *V) {
285 return isa<Type>(V) && classof(cast<Type>(V));
290 /// ArrayType - Class to represent array types
292 class ArrayType : public SequentialType {
293 friend class TypeMap<ArrayValType, ArrayType>;
294 unsigned NumElements;
296 ArrayType(const ArrayType &); // Do not implement
297 const ArrayType &operator=(const ArrayType &); // Do not implement
299 /// This should really be private, but it squelches a bogus warning
300 /// from GCC to make them protected: warning: `class ArrayType' only
301 /// defines private constructors and has no friends
303 /// Private ctor - Only can be created by a static member...
305 ArrayType(const Type *ElType, unsigned NumEl);
308 /// ArrayType::get - This static method is the primary way to construct an
311 static ArrayType *get(const Type *ElementType, unsigned NumElements);
313 inline unsigned getNumElements() const { return NumElements; }
315 // Implement the AbstractTypeUser interface.
316 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
317 virtual void typeBecameConcrete(const DerivedType *AbsTy);
319 // Methods for support type inquiry through isa, cast, and dyn_cast:
320 static inline bool classof(const ArrayType *T) { return true; }
321 static inline bool classof(const Type *T) {
322 return T->getTypeID() == ArrayTyID;
324 static inline bool classof(const Value *V) {
325 return isa<Type>(V) && classof(cast<Type>(V));
330 /// PointerType - Class to represent pointers
332 class PointerType : public SequentialType {
333 friend class TypeMap<PointerValType, PointerType>;
334 PointerType(const PointerType &); // Do not implement
335 const PointerType &operator=(const PointerType &); // Do not implement
337 // This should really be private, but it squelches a bogus warning
338 // from GCC to make them protected: warning: `class PointerType' only
339 // defines private constructors and has no friends
341 // Private ctor - Only can be created by a static member...
342 PointerType(const Type *ElType);
345 /// PointerType::get - This is the only way to construct a new pointer type.
346 static PointerType *get(const Type *ElementType);
348 // Implement the AbstractTypeUser interface.
349 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
350 virtual void typeBecameConcrete(const DerivedType *AbsTy);
352 // Implement support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const PointerType *T) { return true; }
354 static inline bool classof(const Type *T) {
355 return T->getTypeID() == PointerTyID;
357 static inline bool classof(const Value *V) {
358 return isa<Type>(V) && classof(cast<Type>(V));
363 /// OpaqueType - Class to represent abstract types
365 class OpaqueType : public DerivedType {
366 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
367 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
369 /// This should really be private, but it squelches a bogus warning
370 /// from GCC to make them protected: warning: `class OpaqueType' only
371 /// defines private constructors and has no friends
373 /// Private ctor - Only can be created by a static member...
377 /// OpaqueType::get - Static factory method for the OpaqueType class...
379 static OpaqueType *get() {
380 return new OpaqueType(); // All opaque types are distinct
383 // Implement the AbstractTypeUser interface.
384 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
385 abort(); // FIXME: this is not really an AbstractTypeUser!
387 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
388 abort(); // FIXME: this is not really an AbstractTypeUser!
391 // Implement support for type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const OpaqueType *T) { return true; }
393 static inline bool classof(const Type *T) {
394 return T->getTypeID() == OpaqueTyID;
396 static inline bool classof(const Value *V) {
397 return isa<Type>(V) && classof(cast<Type>(V));
401 } // End llvm namespace