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;
33 class DerivedType : public Type {
37 DerivedType(TypeID id) : Type(id) {}
39 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
40 /// that the current type has transitioned from being abstract to being
43 void notifyUsesThatTypeBecameConcrete();
45 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
46 /// another (more concrete) type, we must eliminate all references to other
47 /// types, to avoid some circular reference problems.
49 void dropAllTypeUses();
53 //===--------------------------------------------------------------------===//
54 // Abstract Type handling methods - These types have special lifetimes, which
55 // are managed by (add|remove)AbstractTypeUser. See comments in
56 // AbstractTypeUser.h for more information.
58 /// refineAbstractTypeTo - This function is used to when it is discovered that
59 /// the 'this' abstract type is actually equivalent to the NewType specified.
60 /// This causes all users of 'this' to switch to reference the more concrete
61 /// type NewType and for 'this' to be deleted.
63 void refineAbstractTypeTo(const Type *NewType);
65 void dump() const { Type::dump(); }
67 // Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const DerivedType *T) { return true; }
69 static inline bool classof(const Type *T) {
70 return T->isDerivedType();
75 /// FunctionType - Class to represent function types
77 class FunctionType : public DerivedType {
78 friend class TypeMap<FunctionValType, FunctionType>;
81 FunctionType(const FunctionType &); // Do not implement
82 const FunctionType &operator=(const FunctionType &); // Do not implement
84 /// This should really be private, but it squelches a bogus warning
85 /// from GCC to make them protected: warning: `class FunctionType' only
86 /// defines private constructors and has no friends
88 /// Private ctor - Only can be created by a static member...
90 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
94 /// FunctionType::get - This static method is the primary way of constructing
97 static FunctionType *get(const Type *Result,
98 const std::vector<const Type*> &Params,
101 inline bool isVarArg() const { return isVarArgs; }
102 inline const Type *getReturnType() const { return ContainedTys[0]; }
104 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
105 param_iterator param_begin() const { return ContainedTys.begin()+1; }
106 param_iterator param_end() const { return ContainedTys.end(); }
108 // Parameter type accessors...
109 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
111 /// getNumParams - Return the number of fixed parameters this function type
112 /// requires. This does not consider varargs.
114 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
116 // Implement the AbstractTypeUser interface.
117 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
118 virtual void typeBecameConcrete(const DerivedType *AbsTy);
120 // Methods for support type inquiry through isa, cast, and dyn_cast:
121 static inline bool classof(const FunctionType *T) { return true; }
122 static inline bool classof(const Type *T) {
123 return T->getTypeID() == FunctionTyID;
128 /// CompositeType - Common super class of ArrayType, StructType, PointerType
130 class CompositeType : public DerivedType {
132 inline CompositeType(TypeID id) : DerivedType(id) { }
135 /// getTypeAtIndex - Given an index value into the type, return the type of
138 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
139 virtual bool indexValid(const Value *V) const = 0;
141 // Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const CompositeType *T) { return true; }
143 static inline bool classof(const Type *T) {
144 return T->getTypeID() == ArrayTyID ||
145 T->getTypeID() == StructTyID ||
146 T->getTypeID() == PointerTyID ||
147 T->getTypeID() == PackedTyID;
152 /// StructType - Class to represent struct types
154 class StructType : public CompositeType {
155 friend class TypeMap<StructValType, StructType>;
156 StructType(const StructType &); // Do not implement
157 const StructType &operator=(const StructType &); // Do not implement
160 /// This should really be private, but it squelches a bogus warning
161 /// from GCC to make them protected: warning: `class StructType' only
162 /// defines private constructors and has no friends
164 /// Private ctor - Only can be created by a static member...
166 StructType(const std::vector<const Type*> &Types);
169 /// StructType::get - This static method is the primary way to create a
172 static StructType *get(const std::vector<const Type*> &Params);
174 // Iterator access to the elements
175 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
176 element_iterator element_begin() const { return ContainedTys.begin(); }
177 element_iterator element_end() const { return ContainedTys.end(); }
179 // Random access to the elements
180 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
181 const Type *getElementType(unsigned N) const {
182 assert(N < ContainedTys.size() && "Element number out of range!");
183 return ContainedTys[N];
186 /// getTypeAtIndex - Given an index value into the type, return the type of
187 /// the element. For a structure type, this must be a constant value...
189 virtual const Type *getTypeAtIndex(const Value *V) const ;
190 virtual bool indexValid(const Value *V) const;
192 // Implement the AbstractTypeUser interface.
193 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
194 virtual void typeBecameConcrete(const DerivedType *AbsTy);
196 // Methods for support type inquiry through isa, cast, and dyn_cast:
197 static inline bool classof(const StructType *T) { return true; }
198 static inline bool classof(const Type *T) {
199 return T->getTypeID() == StructTyID;
204 /// SequentialType - This is the superclass of the array, pointer and packed
205 /// type classes. All of these represent "arrays" in memory. The array type
206 /// represents a specifically sized array, pointer types are unsized/unknown
207 /// size arrays, packed types represent specifically sized arrays that
208 /// allow for use of SIMD instructions. SequentialType holds the common
209 /// features of all, which stem from the fact that all three lay their
210 /// components out in memory identically.
212 class SequentialType : public CompositeType {
213 SequentialType(const SequentialType &); // Do not implement!
214 const SequentialType &operator=(const SequentialType &); // Do not implement!
216 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
217 ContainedTys.reserve(1);
218 ContainedTys.push_back(PATypeHandle(ElType, this));
222 inline const Type *getElementType() const { return ContainedTys[0]; }
224 virtual bool indexValid(const Value *V) const;
226 /// getTypeAtIndex - Given an index value into the type, return the type of
227 /// the element. For sequential types, there is only one subtype...
229 virtual const Type *getTypeAtIndex(const Value *V) const {
230 return ContainedTys[0];
233 // Methods for support type inquiry through isa, cast, and dyn_cast:
234 static inline bool classof(const SequentialType *T) { return true; }
235 static inline bool classof(const Type *T) {
236 return T->getTypeID() == ArrayTyID ||
237 T->getTypeID() == PointerTyID ||
238 T->getTypeID() == PackedTyID;
243 /// ArrayType - Class to represent array types
245 class ArrayType : public SequentialType {
246 friend class TypeMap<ArrayValType, ArrayType>;
247 uint64_t NumElements;
249 ArrayType(const ArrayType &); // Do not implement
250 const ArrayType &operator=(const ArrayType &); // Do not implement
252 /// This should really be private, but it squelches a bogus warning
253 /// from GCC to make them protected: warning: `class ArrayType' only
254 /// defines private constructors and has no friends
256 /// Private ctor - Only can be created by a static member...
258 ArrayType(const Type *ElType, uint64_t NumEl);
261 /// ArrayType::get - This static method is the primary way to construct an
264 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
266 inline uint64_t getNumElements() const { return NumElements; }
268 // Implement the AbstractTypeUser interface.
269 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
270 virtual void typeBecameConcrete(const DerivedType *AbsTy);
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const ArrayType *T) { return true; }
274 static inline bool classof(const Type *T) {
275 return T->getTypeID() == ArrayTyID;
279 /// PackedType - Class to represent packed types
281 class PackedType : public SequentialType {
282 friend class TypeMap<PackedValType, PackedType>;
283 unsigned NumElements;
285 PackedType(const PackedType &); // Do not implement
286 const PackedType &operator=(const PackedType &); // Do not implement
288 /// This should really be private, but it squelches a bogus warning
289 /// from GCC to make them protected: warning: `class PackedType' only
290 /// defines private constructors and has no friends
292 /// Private ctor - Only can be created by a static member...
294 PackedType(const Type *ElType, unsigned NumEl);
297 /// PackedType::get - This static method is the primary way to construct an
300 static PackedType *get(const Type *ElementType, unsigned NumElements);
302 inline unsigned getNumElements() const { return NumElements; }
304 // Implement the AbstractTypeUser interface.
305 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
306 virtual void typeBecameConcrete(const DerivedType *AbsTy);
308 // Methods for support type inquiry through isa, cast, and dyn_cast:
309 static inline bool classof(const PackedType *T) { return true; }
310 static inline bool classof(const Type *T) {
311 return T->getTypeID() == PackedTyID;
316 /// PointerType - Class to represent pointers
318 class PointerType : public SequentialType {
319 friend class TypeMap<PointerValType, PointerType>;
320 PointerType(const PointerType &); // Do not implement
321 const PointerType &operator=(const PointerType &); // Do not implement
323 // This should really be private, but it squelches a bogus warning
324 // from GCC to make them protected: warning: `class PointerType' only
325 // defines private constructors and has no friends
327 // Private ctor - Only can be created by a static member...
328 PointerType(const Type *ElType);
331 /// PointerType::get - This is the only way to construct a new pointer type.
332 static PointerType *get(const Type *ElementType);
334 // Implement the AbstractTypeUser interface.
335 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
336 virtual void typeBecameConcrete(const DerivedType *AbsTy);
338 // Implement support type inquiry through isa, cast, and dyn_cast:
339 static inline bool classof(const PointerType *T) { return true; }
340 static inline bool classof(const Type *T) {
341 return T->getTypeID() == PointerTyID;
346 /// OpaqueType - Class to represent abstract types
348 class OpaqueType : public DerivedType {
349 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
350 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
352 /// This should really be private, but it squelches a bogus warning
353 /// from GCC to make them protected: warning: `class OpaqueType' only
354 /// defines private constructors and has no friends
356 /// Private ctor - Only can be created by a static member...
360 /// OpaqueType::get - Static factory method for the OpaqueType class...
362 static OpaqueType *get() {
363 return new OpaqueType(); // All opaque types are distinct
366 // Implement the AbstractTypeUser interface.
367 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
368 abort(); // FIXME: this is not really an AbstractTypeUser!
370 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
371 abort(); // FIXME: this is not really an AbstractTypeUser!
374 // Implement support for 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->getTypeID() == OpaqueTyID;
381 } // End llvm namespace