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"
22 #include "llvm/Support/DataTypes.h"
27 template<class ValType, class TypeClass> class TypeMap;
28 class FunctionValType;
34 class DerivedType : public Type, public AbstractTypeUser {
35 // AbstractTypeUsers - Implement a list of the users that need to be notified
36 // if I am a type, and I get resolved into a more concrete type.
38 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers;
42 DerivedType(TypeID id) : Type("", id) {}
44 assert(AbstractTypeUsers.empty());
47 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
48 /// that the current type has transitioned from being abstract to being
51 void notifyUsesThatTypeBecameConcrete();
53 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
54 /// another (more concrete) type, we must eliminate all references to other
55 /// types, to avoid some circular reference problems.
57 void dropAllTypeUses();
59 void RefCountIsZero() const {
60 if (AbstractTypeUsers.empty())
67 //===--------------------------------------------------------------------===//
68 // Abstract Type handling methods - These types have special lifetimes, which
69 // are managed by (add|remove)AbstractTypeUser. See comments in
70 // AbstractTypeUser.h for more information.
72 /// addAbstractTypeUser - Notify an abstract type that there is a new user of
73 /// it. This function is called primarily by the PATypeHandle class.
75 void addAbstractTypeUser(AbstractTypeUser *U) const {
76 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!");
77 AbstractTypeUsers.push_back(U);
80 /// removeAbstractTypeUser - Notify an abstract type that a user of the class
81 /// no longer has a handle to the type. This function is called primarily by
82 /// the PATypeHandle class. When there are no users of the abstract type, it
83 /// is annihilated, because there is no way to get a reference to it ever
86 void removeAbstractTypeUser(AbstractTypeUser *U) const;
88 /// refineAbstractTypeTo - This function is used to when it is discovered that
89 /// the 'this' abstract type is actually equivalent to the NewType specified.
90 /// This causes all users of 'this' to switch to reference the more concrete
91 /// type NewType and for 'this' to be deleted.
93 void refineAbstractTypeTo(const Type *NewType);
95 void dump() const { Type::dump(); }
97 // Methods for support type inquiry through isa, cast, and dyn_cast:
98 static inline bool classof(const DerivedType *T) { return true; }
99 static inline bool classof(const Type *T) {
100 return T->isDerivedType();
105 /// FunctionType - Class to represent function types
107 class FunctionType : public DerivedType {
108 friend class TypeMap<FunctionValType, FunctionType>;
111 FunctionType(const FunctionType &); // Do not implement
112 const FunctionType &operator=(const FunctionType &); // Do not implement
114 /// This should really be private, but it squelches a bogus warning
115 /// from GCC to make them protected: warning: `class FunctionType' only
116 /// defines private constructors and has no friends
118 /// Private ctor - Only can be created by a static member...
120 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
124 /// FunctionType::get - This static method is the primary way of constructing
127 static FunctionType *get(const Type *Result,
128 const std::vector<const Type*> &Params,
131 inline bool isVarArg() const { return isVarArgs; }
132 inline const Type *getReturnType() const { return ContainedTys[0]; }
134 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
135 param_iterator param_begin() const { return ContainedTys.begin()+1; }
136 param_iterator param_end() const { return ContainedTys.end(); }
138 // Parameter type accessors...
139 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
141 /// getNumParams - Return the number of fixed parameters this function type
142 /// requires. This does not consider varargs.
144 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
146 // Implement the AbstractTypeUser interface.
147 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
148 virtual void typeBecameConcrete(const DerivedType *AbsTy);
150 // Methods for support type inquiry through isa, cast, and dyn_cast:
151 static inline bool classof(const FunctionType *T) { return true; }
152 static inline bool classof(const Type *T) {
153 return T->getTypeID() == FunctionTyID;
158 /// CompositeType - Common super class of ArrayType, StructType, PointerType
160 class CompositeType : public DerivedType {
162 inline CompositeType(TypeID id) : DerivedType(id) { }
165 /// getTypeAtIndex - Given an index value into the type, return the type of
168 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
169 virtual bool indexValid(const Value *V) const = 0;
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const CompositeType *T) { return true; }
173 static inline bool classof(const Type *T) {
174 return T->getTypeID() == ArrayTyID ||
175 T->getTypeID() == StructTyID ||
176 T->getTypeID() == PointerTyID ||
177 T->getTypeID() == PackedTyID;
182 /// StructType - Class to represent struct types
184 class StructType : public CompositeType {
185 friend class TypeMap<StructValType, StructType>;
186 StructType(const StructType &); // Do not implement
187 const StructType &operator=(const StructType &); // Do not implement
190 /// This should really be private, but it squelches a bogus warning
191 /// from GCC to make them protected: warning: `class StructType' only
192 /// defines private constructors and has no friends
194 /// Private ctor - Only can be created by a static member...
196 StructType(const std::vector<const Type*> &Types);
199 /// StructType::get - This static method is the primary way to create a
202 static StructType *get(const std::vector<const Type*> &Params);
204 // Iterator access to the elements
205 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
206 element_iterator element_begin() const { return ContainedTys.begin(); }
207 element_iterator element_end() const { return ContainedTys.end(); }
209 // Random access to the elements
210 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
211 const Type *getElementType(unsigned N) const {
212 assert(N < ContainedTys.size() && "Element number out of range!");
213 return ContainedTys[N];
216 /// getTypeAtIndex - Given an index value into the type, return the type of
217 /// the element. For a structure type, this must be a constant value...
219 virtual const Type *getTypeAtIndex(const Value *V) const ;
220 virtual bool indexValid(const Value *V) const;
222 // Implement the AbstractTypeUser interface.
223 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
224 virtual void typeBecameConcrete(const DerivedType *AbsTy);
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->getTypeID() == StructTyID;
234 /// SequentialType - This is the superclass of the array, pointer and packed
235 /// type classes. All of these represent "arrays" in memory. The array type
236 /// represents a specifically sized array, pointer types are unsized/unknown
237 /// size arrays, packed types represent specifically sized arrays that
238 /// allow for use of SIMD instructions. SequentialType holds the common
239 /// features of all, which stem from the fact that all three lay their
240 /// 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 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
247 ContainedTys.reserve(1);
248 ContainedTys.push_back(PATypeHandle(ElType, this));
252 inline const Type *getElementType() const { return ContainedTys[0]; }
254 virtual bool indexValid(const Value *V) const;
256 /// getTypeAtIndex - Given an index value into the type, return the type of
257 /// the element. For sequential types, there is only one subtype...
259 virtual const Type *getTypeAtIndex(const Value *V) const {
260 return ContainedTys[0];
263 // Methods for support type inquiry through isa, cast, and dyn_cast:
264 static inline bool classof(const SequentialType *T) { return true; }
265 static inline bool classof(const Type *T) {
266 return T->getTypeID() == ArrayTyID ||
267 T->getTypeID() == PointerTyID ||
268 T->getTypeID() == PackedTyID;
273 /// ArrayType - Class to represent array types
275 class ArrayType : public SequentialType {
276 friend class TypeMap<ArrayValType, ArrayType>;
277 uint64_t NumElements;
279 ArrayType(const ArrayType &); // Do not implement
280 const ArrayType &operator=(const ArrayType &); // Do not implement
282 /// This should really be private, but it squelches a bogus warning
283 /// from GCC to make them protected: warning: `class ArrayType' only
284 /// defines private constructors and has no friends
286 /// Private ctor - Only can be created by a static member...
288 ArrayType(const Type *ElType, uint64_t NumEl);
291 /// ArrayType::get - This static method is the primary way to construct an
294 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
296 inline uint64_t getNumElements() const { return NumElements; }
298 // Implement the AbstractTypeUser interface.
299 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
300 virtual void typeBecameConcrete(const DerivedType *AbsTy);
302 // Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const ArrayType *T) { return true; }
304 static inline bool classof(const Type *T) {
305 return T->getTypeID() == ArrayTyID;
309 /// PackedType - Class to represent packed types
311 class PackedType : public SequentialType {
312 friend class TypeMap<PackedValType, PackedType>;
313 unsigned NumElements;
315 PackedType(const PackedType &); // Do not implement
316 const PackedType &operator=(const PackedType &); // Do not implement
318 /// This should really be private, but it squelches a bogus warning
319 /// from GCC to make them protected: warning: `class PackedType' only
320 /// defines private constructors and has no friends
322 /// Private ctor - Only can be created by a static member...
324 PackedType(const Type *ElType, unsigned NumEl);
327 /// PackedType::get - This static method is the primary way to construct an
330 static PackedType *get(const Type *ElementType, unsigned NumElements);
332 inline unsigned getNumElements() const { return NumElements; }
334 // Implement the AbstractTypeUser interface.
335 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
336 virtual void typeBecameConcrete(const DerivedType *AbsTy);
338 // Methods for support type inquiry through isa, cast, and dyn_cast:
339 static inline bool classof(const PackedType *T) { return true; }
340 static inline bool classof(const Type *T) {
341 return T->getTypeID() == PackedTyID;
346 /// PointerType - Class to represent pointers
348 class PointerType : public SequentialType {
349 friend class TypeMap<PointerValType, PointerType>;
350 PointerType(const PointerType &); // Do not implement
351 const PointerType &operator=(const PointerType &); // Do not implement
353 // This should really be private, but it squelches a bogus warning
354 // from GCC to make them protected: warning: `class PointerType' only
355 // defines private constructors and has no friends
357 // Private ctor - Only can be created by a static member...
358 PointerType(const Type *ElType);
361 /// PointerType::get - This is the only way to construct a new pointer type.
362 static PointerType *get(const Type *ElementType);
364 // Implement the AbstractTypeUser interface.
365 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
366 virtual void typeBecameConcrete(const DerivedType *AbsTy);
368 // Implement support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const PointerType *T) { return true; }
370 static inline bool classof(const Type *T) {
371 return T->getTypeID() == PointerTyID;
376 /// OpaqueType - Class to represent abstract types
378 class OpaqueType : public DerivedType {
379 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
380 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
382 /// This should really be private, but it squelches a bogus warning
383 /// from GCC to make them protected: warning: `class OpaqueType' only
384 /// defines private constructors and has no friends
386 /// Private ctor - Only can be created by a static member...
390 /// OpaqueType::get - Static factory method for the OpaqueType class...
392 static OpaqueType *get() {
393 return new OpaqueType(); // All opaque types are distinct
396 // Implement the AbstractTypeUser interface.
397 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
398 abort(); // FIXME: this is not really an AbstractTypeUser!
400 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
401 abort(); // FIXME: this is not really an AbstractTypeUser!
404 // Implement support for 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->getTypeID() == OpaqueTyID;
411 } // End llvm namespace