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"
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
32 class DerivedType : public Type, public AbstractTypeUser {
33 // AbstractTypeUsers - Implement a list of the users that need to be notified
34 // if I am a type, and I get resolved into a more concrete type.
36 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
39 DerivedType(TypeID id) : Type("", id) {}
41 assert(AbstractTypeUsers.empty());
44 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
45 /// that the current type has transitioned from being abstract to being
48 void notifyUsesThatTypeBecameConcrete();
50 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
51 /// another (more concrete) type, we must eliminate all references to other
52 /// types, to avoid some circular reference problems.
54 void dropAllTypeUses();
56 void RefCountIsZero() const {
57 if (AbstractTypeUsers.empty())
64 //===--------------------------------------------------------------------===//
65 // Abstract Type handling methods - These types have special lifetimes, which
66 // are managed by (add|remove)AbstractTypeUser. See comments in
67 // AbstractTypeUser.h for more information.
69 /// addAbstractTypeUser - Notify an abstract type that there is a new user of
70 /// it. This function is called primarily by the PATypeHandle class.
72 void addAbstractTypeUser(AbstractTypeUser *U) const {
73 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
74 AbstractTypeUsers.push_back(U);
77 /// removeAbstractTypeUser - Notify an abstract type that a user of the class
78 /// no longer has a handle to the type. This function is called primarily by
79 /// the PATypeHandle class. When there are no users of the abstract type, it
80 /// is annihilated, because there is no way to get a reference to it ever
83 void removeAbstractTypeUser(AbstractTypeUser *U) const;
85 /// refineAbstractTypeTo - This function is used to when it is discovered that
86 /// the 'this' abstract type is actually equivalent to the NewType specified.
87 /// This causes all users of 'this' to switch to reference the more concrete
88 /// type NewType and for 'this' to be deleted.
90 void refineAbstractTypeTo(const Type *NewType);
92 void dump() const { Type::dump(); }
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const DerivedType *T) { return true; }
96 static inline bool classof(const Type *T) {
97 return T->isDerivedType();
102 /// FunctionType - Class to represent function types
104 class FunctionType : public DerivedType {
105 friend class TypeMap<FunctionValType, FunctionType>;
108 FunctionType(const FunctionType &); // Do not implement
109 const FunctionType &operator=(const FunctionType &); // Do not implement
111 /// This should really be private, but it squelches a bogus warning
112 /// from GCC to make them protected: warning: `class FunctionType' only
113 /// defines private constructors and has no friends
115 /// Private ctor - Only can be created by a static member...
117 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
121 /// FunctionType::get - This static method is the primary way of constructing
124 static FunctionType *get(const Type *Result,
125 const std::vector<const Type*> &Params,
128 inline bool isVarArg() const { return isVarArgs; }
129 inline const Type *getReturnType() const { return ContainedTys[0]; }
131 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
132 param_iterator param_begin() const { return ContainedTys.begin()+1; }
133 param_iterator param_end() const { return ContainedTys.end(); }
135 // Parameter type accessors...
136 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
138 /// getNumParams - Return the number of fixed parameters this function type
139 /// requires. This does not consider varargs.
141 unsigned getNumParams() const { return ContainedTys.size()-1; }
143 // Implement the AbstractTypeUser interface.
144 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
145 virtual void typeBecameConcrete(const DerivedType *AbsTy);
147 // Methods for support type inquiry through isa, cast, and dyn_cast:
148 static inline bool classof(const FunctionType *T) { return true; }
149 static inline bool classof(const Type *T) {
150 return T->getTypeID() == FunctionTyID;
155 /// CompositeType - Common super class of ArrayType, StructType, and PointerType
157 class CompositeType : public DerivedType {
159 inline CompositeType(TypeID id) : DerivedType(id) { }
162 /// getTypeAtIndex - Given an index value into the type, return the type of
165 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
166 virtual bool indexValid(const Value *V) 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->getTypeID() == ArrayTyID ||
172 T->getTypeID() == StructTyID ||
173 T->getTypeID() == PointerTyID;
178 /// StructType - Class to represent struct types
180 class StructType : public CompositeType {
181 friend class TypeMap<StructValType, StructType>;
182 StructType(const StructType &); // Do not implement
183 const StructType &operator=(const StructType &); // Do not implement
186 /// This should really be private, but it squelches a bogus warning
187 /// from GCC to make them protected: warning: `class StructType' only
188 /// defines private constructors and has no friends
190 /// Private ctor - Only can be created by a static member...
192 StructType(const std::vector<const Type*> &Types);
195 /// StructType::get - This static method is the primary way to create a
198 static StructType *get(const std::vector<const Type*> &Params);
200 // Iterator access to the elements
201 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
202 element_iterator element_begin() const { return ContainedTys.begin(); }
203 element_iterator element_end() const { return ContainedTys.end(); }
205 // Random access to the elements
206 unsigned getNumElements() const { return ContainedTys.size(); }
207 const Type *getElementType(unsigned N) const {
208 assert(N < ContainedTys.size() && "Element number out of range!");
209 return ContainedTys[N];
212 /// getTypeAtIndex - Given an index value into the type, return the type of
213 /// the element. For a structure type, this must be a constant value...
215 virtual const Type *getTypeAtIndex(const Value *V) const ;
216 virtual bool indexValid(const Value *V) const;
218 // Implement the AbstractTypeUser interface.
219 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
220 virtual void typeBecameConcrete(const DerivedType *AbsTy);
222 // Methods for support type inquiry through isa, cast, and dyn_cast:
223 static inline bool classof(const StructType *T) { return true; }
224 static inline bool classof(const Type *T) {
225 return T->getTypeID() == StructTyID;
230 /// SequentialType - This is the superclass of the array and pointer type
231 /// classes. Both of these represent "arrays" in memory. The array type
232 /// represents a specifically sized array, pointer types are unsized/unknown
233 /// size arrays. SequentialType holds the common features of both, which stem
234 /// from the fact that both lay their components out in memory identically.
236 class SequentialType : public CompositeType {
237 SequentialType(const SequentialType &); // Do not implement!
238 const SequentialType &operator=(const SequentialType &); // Do not implement!
240 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
241 ContainedTys.reserve(1);
242 ContainedTys.push_back(PATypeHandle(ElType, this));
246 inline const Type *getElementType() const { return ContainedTys[0]; }
248 virtual bool indexValid(const Value *V) const;
250 /// getTypeAtIndex - Given an index value into the type, return the type of
251 /// the element. For sequential types, there is only one subtype...
253 virtual const Type *getTypeAtIndex(const Value *V) const {
254 return ContainedTys[0];
257 // Methods for support type inquiry through isa, cast, and dyn_cast:
258 static inline bool classof(const SequentialType *T) { return true; }
259 static inline bool classof(const Type *T) {
260 return T->getTypeID() == ArrayTyID ||
261 T->getTypeID() == PointerTyID;
266 /// ArrayType - Class to represent array types
268 class ArrayType : public SequentialType {
269 friend class TypeMap<ArrayValType, ArrayType>;
270 unsigned NumElements;
272 ArrayType(const ArrayType &); // Do not implement
273 const ArrayType &operator=(const ArrayType &); // Do not implement
275 /// This should really be private, but it squelches a bogus warning
276 /// from GCC to make them protected: warning: `class ArrayType' only
277 /// defines private constructors and has no friends
279 /// Private ctor - Only can be created by a static member...
281 ArrayType(const Type *ElType, unsigned NumEl);
284 /// ArrayType::get - This static method is the primary way to construct an
287 static ArrayType *get(const Type *ElementType, unsigned NumElements);
289 inline unsigned getNumElements() const { return NumElements; }
291 // Implement the AbstractTypeUser interface.
292 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
293 virtual void typeBecameConcrete(const DerivedType *AbsTy);
295 // Methods for support type inquiry through isa, cast, and dyn_cast:
296 static inline bool classof(const ArrayType *T) { return true; }
297 static inline bool classof(const Type *T) {
298 return T->getTypeID() == ArrayTyID;
303 /// PointerType - Class to represent pointers
305 class PointerType : public SequentialType {
306 friend class TypeMap<PointerValType, PointerType>;
307 PointerType(const PointerType &); // Do not implement
308 const PointerType &operator=(const PointerType &); // Do not implement
310 // This should really be private, but it squelches a bogus warning
311 // from GCC to make them protected: warning: `class PointerType' only
312 // defines private constructors and has no friends
314 // Private ctor - Only can be created by a static member...
315 PointerType(const Type *ElType);
318 /// PointerType::get - This is the only way to construct a new pointer type.
319 static PointerType *get(const Type *ElementType);
321 // Implement the AbstractTypeUser interface.
322 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
323 virtual void typeBecameConcrete(const DerivedType *AbsTy);
325 // Implement support type inquiry through isa, cast, and dyn_cast:
326 static inline bool classof(const PointerType *T) { return true; }
327 static inline bool classof(const Type *T) {
328 return T->getTypeID() == PointerTyID;
333 /// OpaqueType - Class to represent abstract types
335 class OpaqueType : public DerivedType {
336 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
337 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
339 /// This should really be private, but it squelches a bogus warning
340 /// from GCC to make them protected: warning: `class OpaqueType' only
341 /// defines private constructors and has no friends
343 /// Private ctor - Only can be created by a static member...
347 /// OpaqueType::get - Static factory method for the OpaqueType class...
349 static OpaqueType *get() {
350 return new OpaqueType(); // All opaque types are distinct
353 // Implement the AbstractTypeUser interface.
354 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
355 abort(); // FIXME: this is not really an AbstractTypeUser!
357 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
358 abort(); // FIXME: this is not really an AbstractTypeUser!
361 // Implement support for type inquiry through isa, cast, and dyn_cast:
362 static inline bool classof(const OpaqueType *T) { return true; }
363 static inline bool classof(const Type *T) {
364 return T->getTypeID() == OpaqueTyID;
368 } // End llvm namespace