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 {
38 DerivedType(TypeID id) : Type(id) {}
40 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
41 /// that the current type has transitioned from being abstract to being
44 void notifyUsesThatTypeBecameConcrete();
46 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
47 /// another (more concrete) type, we must eliminate all references to other
48 /// types, to avoid some circular reference problems.
50 void dropAllTypeUses();
54 //===--------------------------------------------------------------------===//
55 // Abstract Type handling methods - These types have special lifetimes, which
56 // are managed by (add|remove)AbstractTypeUser. See comments in
57 // AbstractTypeUser.h for more information.
59 /// refineAbstractTypeTo - This function is used to when it is discovered that
60 /// the 'this' abstract type is actually equivalent to the NewType specified.
61 /// This causes all users of 'this' to switch to reference the more concrete
62 /// type NewType and for 'this' to be deleted.
64 void refineAbstractTypeTo(const Type *NewType);
66 void dump() const { Type::dump(); }
68 // Methods for support type inquiry through isa, cast, and dyn_cast:
69 static inline bool classof(const DerivedType *T) { return true; }
70 static inline bool classof(const Type *T) {
71 return T->isDerivedType();
76 /// FunctionType - Class to represent function types
78 class FunctionType : public DerivedType {
79 friend class TypeMap<FunctionValType, FunctionType>;
82 FunctionType(const FunctionType &); // Do not implement
83 const FunctionType &operator=(const FunctionType &); // Do not implement
85 /// This should really be private, but it squelches a bogus warning
86 /// from GCC to make them protected: warning: `class FunctionType' only
87 /// defines private constructors and has no friends
89 /// Private ctor - Only can be created by a static member...
91 FunctionType(const Type *Result, const std::vector<const Type*> &Params,
95 /// FunctionType::get - This static method is the primary way of constructing
98 static FunctionType *get(const Type *Result,
99 const std::vector<const Type*> &Params,
102 inline bool isVarArg() const { return isVarArgs; }
103 inline const Type *getReturnType() const { return ContainedTys[0]; }
105 typedef std::vector<PATypeHandle>::const_iterator param_iterator;
106 param_iterator param_begin() const { return ContainedTys.begin()+1; }
107 param_iterator param_end() const { return ContainedTys.end(); }
109 // Parameter type accessors...
110 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
112 /// getNumParams - Return the number of fixed parameters this function type
113 /// requires. This does not consider varargs.
115 unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
117 // Implement the AbstractTypeUser interface.
118 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
119 virtual void typeBecameConcrete(const DerivedType *AbsTy);
121 // Methods for support type inquiry through isa, cast, and dyn_cast:
122 static inline bool classof(const FunctionType *T) { return true; }
123 static inline bool classof(const Type *T) {
124 return T->getTypeID() == FunctionTyID;
129 /// CompositeType - Common super class of ArrayType, StructType, PointerType
131 class CompositeType : public DerivedType {
133 inline CompositeType(TypeID id) : DerivedType(id) { }
136 /// getTypeAtIndex - Given an index value into the type, return the type of
139 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
140 virtual bool indexValid(const Value *V) const = 0;
142 // Methods for support type inquiry through isa, cast, and dyn_cast:
143 static inline bool classof(const CompositeType *T) { return true; }
144 static inline bool classof(const Type *T) {
145 return T->getTypeID() == ArrayTyID ||
146 T->getTypeID() == StructTyID ||
147 T->getTypeID() == PointerTyID ||
148 T->getTypeID() == PackedTyID;
153 /// StructType - Class to represent struct types
155 class StructType : public CompositeType {
156 friend class TypeMap<StructValType, StructType>;
157 StructType(const StructType &); // Do not implement
158 const StructType &operator=(const StructType &); // Do not implement
161 /// This should really be private, but it squelches a bogus warning
162 /// from GCC to make them protected: warning: `class StructType' only
163 /// defines private constructors and has no friends
165 /// Private ctor - Only can be created by a static member...
167 StructType(const std::vector<const Type*> &Types);
170 /// StructType::get - This static method is the primary way to create a
173 static StructType *get(const std::vector<const Type*> &Params);
175 // Iterator access to the elements
176 typedef std::vector<PATypeHandle>::const_iterator element_iterator;
177 element_iterator element_begin() const { return ContainedTys.begin(); }
178 element_iterator element_end() const { return ContainedTys.end(); }
180 // Random access to the elements
181 unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
182 const Type *getElementType(unsigned N) const {
183 assert(N < ContainedTys.size() && "Element number out of range!");
184 return ContainedTys[N];
187 /// getTypeAtIndex - Given an index value into the type, return the type of
188 /// the element. For a structure type, this must be a constant value...
190 virtual const Type *getTypeAtIndex(const Value *V) const ;
191 virtual bool indexValid(const Value *V) const;
193 // Implement the AbstractTypeUser interface.
194 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
195 virtual void typeBecameConcrete(const DerivedType *AbsTy);
197 // Methods for support type inquiry through isa, cast, and dyn_cast:
198 static inline bool classof(const StructType *T) { return true; }
199 static inline bool classof(const Type *T) {
200 return T->getTypeID() == StructTyID;
205 /// SequentialType - This is the superclass of the array, pointer and packed
206 /// type classes. All of these represent "arrays" in memory. The array type
207 /// represents a specifically sized array, pointer types are unsized/unknown
208 /// size arrays, packed types represent specifically sized arrays that
209 /// allow for use of SIMD instructions. SequentialType holds the common
210 /// features of all, which stem from the fact that all three lay their
211 /// components out in memory identically.
213 class SequentialType : public CompositeType {
214 SequentialType(const SequentialType &); // Do not implement!
215 const SequentialType &operator=(const SequentialType &); // Do not implement!
217 SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
218 ContainedTys.reserve(1);
219 ContainedTys.push_back(PATypeHandle(ElType, this));
223 inline const Type *getElementType() const { return ContainedTys[0]; }
225 virtual bool indexValid(const Value *V) const;
227 /// getTypeAtIndex - Given an index value into the type, return the type of
228 /// the element. For sequential types, there is only one subtype...
230 virtual const Type *getTypeAtIndex(const Value *V) const {
231 return ContainedTys[0];
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const SequentialType *T) { return true; }
236 static inline bool classof(const Type *T) {
237 return T->getTypeID() == ArrayTyID ||
238 T->getTypeID() == PointerTyID ||
239 T->getTypeID() == PackedTyID;
244 /// ArrayType - Class to represent array types
246 class ArrayType : public SequentialType {
247 friend class TypeMap<ArrayValType, ArrayType>;
248 uint64_t NumElements;
250 ArrayType(const ArrayType &); // Do not implement
251 const ArrayType &operator=(const ArrayType &); // Do not implement
253 /// This should really be private, but it squelches a bogus warning
254 /// from GCC to make them protected: warning: `class ArrayType' only
255 /// defines private constructors and has no friends
257 /// Private ctor - Only can be created by a static member...
259 ArrayType(const Type *ElType, uint64_t NumEl);
262 /// ArrayType::get - This static method is the primary way to construct an
265 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
267 inline uint64_t getNumElements() const { return NumElements; }
269 // Implement the AbstractTypeUser interface.
270 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
271 virtual void typeBecameConcrete(const DerivedType *AbsTy);
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const ArrayType *T) { return true; }
275 static inline bool classof(const Type *T) {
276 return T->getTypeID() == ArrayTyID;
280 /// PackedType - Class to represent packed types
282 class PackedType : public SequentialType {
283 friend class TypeMap<PackedValType, PackedType>;
284 unsigned NumElements;
286 PackedType(const PackedType &); // Do not implement
287 const PackedType &operator=(const PackedType &); // Do not implement
289 /// This should really be private, but it squelches a bogus warning
290 /// from GCC to make them protected: warning: `class PackedType' only
291 /// defines private constructors and has no friends
293 /// Private ctor - Only can be created by a static member...
295 PackedType(const Type *ElType, unsigned NumEl);
298 /// PackedType::get - This static method is the primary way to construct an
301 static PackedType *get(const Type *ElementType, unsigned NumElements);
303 inline unsigned getNumElements() const { return NumElements; }
305 // Implement the AbstractTypeUser interface.
306 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
307 virtual void typeBecameConcrete(const DerivedType *AbsTy);
309 // Methods for support type inquiry through isa, cast, and dyn_cast:
310 static inline bool classof(const PackedType *T) { return true; }
311 static inline bool classof(const Type *T) {
312 return T->getTypeID() == PackedTyID;
317 /// PointerType - Class to represent pointers
319 class PointerType : public SequentialType {
320 friend class TypeMap<PointerValType, PointerType>;
321 PointerType(const PointerType &); // Do not implement
322 const PointerType &operator=(const PointerType &); // Do not implement
324 // This should really be private, but it squelches a bogus warning
325 // from GCC to make them protected: warning: `class PointerType' only
326 // defines private constructors and has no friends
328 // Private ctor - Only can be created by a static member...
329 PointerType(const Type *ElType);
332 /// PointerType::get - This is the only way to construct a new pointer type.
333 static PointerType *get(const Type *ElementType);
335 // Implement the AbstractTypeUser interface.
336 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
337 virtual void typeBecameConcrete(const DerivedType *AbsTy);
339 // Implement support type inquiry through isa, cast, and dyn_cast:
340 static inline bool classof(const PointerType *T) { return true; }
341 static inline bool classof(const Type *T) {
342 return T->getTypeID() == PointerTyID;
347 /// OpaqueType - Class to represent abstract types
349 class OpaqueType : public DerivedType {
350 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
351 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
353 /// This should really be private, but it squelches a bogus warning
354 /// from GCC to make them protected: warning: `class OpaqueType' only
355 /// defines private constructors and has no friends
357 /// Private ctor - Only can be created by a static member...
361 /// OpaqueType::get - Static factory method for the OpaqueType class...
363 static OpaqueType *get() {
364 return new OpaqueType(); // All opaque types are distinct
367 // Implement the AbstractTypeUser interface.
368 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
369 abort(); // FIXME: this is not really an AbstractTypeUser!
371 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
372 abort(); // FIXME: this is not really an AbstractTypeUser!
375 // Implement support for type inquiry through isa, cast, and dyn_cast:
376 static inline bool classof(const OpaqueType *T) { return true; }
377 static inline bool classof(const Type *T) {
378 return T->getTypeID() == OpaqueTyID;
382 } // End llvm namespace