Type.h revision f32f56862a550fed2c943c4a4acadc56ce13ab07
1//===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declaration of the Type class.  For more "Type" type
11// stuff, look in DerivedTypes.h.
12//
13// Note that instances of the Type class are immutable: once they are created,
14// they are never changed.  Also note that only one instance of a particular
15// type is ever created.  Thus seeing if two types are equal is a matter of
16// doing a trivial pointer comparison.
17//
18// Types, once allocated, are never free'd.
19//
20// Opaque types are simple derived types with no state.  There may be many
21// different Opaque type objects floating around, but two are only considered
22// identical if they are pointer equals of each other.  This allows us to have
23// two opaque types that end up resolving to different concrete types later.
24//
25// Opaque types are also kinda wierd and scary and different because they have
26// to keep a list of uses of the type.  When, through linking, parsing, or
27// bytecode reading, they become resolved, they need to find and update all
28// users of the unknown type, causing them to reference a new, more concrete
29// type.  Opaque types are deleted when their use list dwindles to zero users.
30//
31//===----------------------------------------------------------------------===//
32
33#ifndef LLVM_TYPE_H
34#define LLVM_TYPE_H
35
36#include "llvm/Value.h"
37#include "Support/GraphTraits.h"
38#include "Support/iterator"
39#include <vector>
40
41namespace llvm {
42
43class DerivedType;
44class FunctionType;
45class ArrayType;
46class PointerType;
47class StructType;
48class OpaqueType;
49
50struct Type : public Value {
51  ///===-------------------------------------------------------------------===//
52  /// Definitions of all of the base types for the Type system.  Based on this
53  /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
54  /// Note: If you add an element to this, you need to add an element to the
55  /// Type::getPrimitiveType function, or else things will break!
56  ///
57  enum PrimitiveID {
58    VoidTyID = 0  , BoolTyID,           //  0, 1: Basics...
59    UByteTyID     , SByteTyID,          //  2, 3: 8 bit types...
60    UShortTyID    , ShortTyID,          //  4, 5: 16 bit types...
61    UIntTyID      , IntTyID,            //  6, 7: 32 bit types...
62    ULongTyID     , LongTyID,           //  8, 9: 64 bit types...
63
64    FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
65
66    TypeTyID,                           // 12   : Type definitions
67    LabelTyID     ,                     // 13   : Labels...
68
69    // Derived types... see DerivedTypes.h file...
70    // Make sure FirstDerivedTyID stays up to date!!!
71    FunctionTyID  , StructTyID,         // Functions... Structs...
72    ArrayTyID     , PointerTyID,        // Array... pointer...
73    OpaqueTyID,                         // Opaque type instances...
74    //PackedTyID  ,                     // SIMD 'packed' format... TODO
75    //...
76
77    NumPrimitiveIDs,                    // Must remain as last defined ID
78    FirstDerivedTyID = FunctionTyID,
79  };
80
81private:
82  PrimitiveID ID;        // The current base type of this type...
83  unsigned    UID;       // The unique ID number for this class
84  bool        Abstract;  // True if type contains an OpaqueType
85
86  const Type *getForwardedTypeInternal() const;
87protected:
88  /// ctor is protected, so only subclasses can create Type objects...
89  Type(const std::string &Name, PrimitiveID id);
90  virtual ~Type() {}
91
92  /// setName - Associate the name with this type in the symbol table, but don't
93  /// set the local name to be equal specified name.
94  ///
95  virtual void setName(const std::string &Name, SymbolTable *ST = 0);
96
97  /// Types can become nonabstract later, if they are refined.
98  ///
99  inline void setAbstract(bool Val) { Abstract = Val; }
100
101  /// isTypeAbstract - This method is used to calculate the Abstract bit.
102  ///
103  bool isTypeAbstract();
104
105  /// ForwardType - This field is used to implement the union find scheme for
106  /// abstract types.  When types are refined to other types, this field is set
107  /// to the more refined type.  Only abstract types can be forwarded.
108  mutable const Type *ForwardType;
109
110  /// ContainedTys - The list of types contained by this one.  For example, this
111  /// includes the arguments of a function type, the elements of the structure,
112  /// the pointee of a pointer, etc.  Note that keeping this vector in the Type
113  /// class wastes some space for types that do not contain anything (such as
114  /// primitive types).  However, keeping it here allows the subtype_* members
115  /// to be implemented MUCH more efficiently, and dynamically very few types do
116  /// not contain any elements (most are derived).
117  std::vector<PATypeHandle> ContainedTys;
118
119public:
120  virtual void print(std::ostream &O) const;
121
122  //===--------------------------------------------------------------------===//
123  // Property accessors for dealing with types... Some of these virtual methods
124  // are defined in private classes defined in Type.cpp for primitive types.
125  //
126
127  /// getPrimitiveID - Return the base type of the type.  This will return one
128  /// of the PrimitiveID enum elements defined above.
129  ///
130  inline PrimitiveID getPrimitiveID() const { return ID; }
131
132  /// getUniqueID - Returns the UID of the type.  This can be thought of as a
133  /// small integer version of the pointer to the type class.  Two types that
134  /// are structurally different have different UIDs.  This can be used for
135  /// indexing types into an array.
136  ///
137  inline unsigned getUniqueID() const { return UID; }
138
139  /// getDescription - Return the string representation of the type...
140  const std::string &getDescription() const;
141
142  /// isSigned - Return whether an integral numeric type is signed.  This is
143  /// true for SByteTy, ShortTy, IntTy, LongTy.  Note that this is not true for
144  /// Float and Double.
145  //
146  virtual bool isSigned() const { return 0; }
147
148  /// isUnsigned - Return whether a numeric type is unsigned.  This is not quite
149  /// the complement of isSigned... nonnumeric types return false as they do
150  /// with isSigned.  This returns true for UByteTy, UShortTy, UIntTy, and
151  /// ULongTy
152  ///
153  virtual bool isUnsigned() const { return 0; }
154
155  /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
156  /// single virtual function invocation.
157  ///
158  virtual bool isInteger() const { return 0; }
159
160  /// isIntegral - Returns true if this is an integral type, which is either
161  /// BoolTy or one of the Integer types.
162  ///
163  bool isIntegral() const { return isInteger() || this == BoolTy; }
164
165  /// isFloatingPoint - Return true if this is one of the two floating point
166  /// types
167  bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
168
169  /// isAbstract - True if the type is either an Opaque type, or is a derived
170  /// type that includes an opaque type somewhere in it.
171  ///
172  inline bool isAbstract() const { return Abstract; }
173
174  /// isLosslesslyConvertibleTo - Return true if this type can be converted to
175  /// 'Ty' without any reinterpretation of bits.  For example, uint to int.
176  ///
177  bool isLosslesslyConvertibleTo(const Type *Ty) const;
178
179
180  /// Here are some useful little methods to query what type derived types are
181  /// Note that all other types can just compare to see if this == Type::xxxTy;
182  ///
183  inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
184  inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
185
186  /// isFirstClassType - Return true if the value is holdable in a register.
187  inline bool isFirstClassType() const {
188    return (ID != VoidTyID && ID < TypeTyID) || ID == PointerTyID;
189  }
190
191  /// isSized - Return true if it makes sense to take the size of this type.  To
192  /// get the actual size for a particular target, it is reasonable to use the
193  /// TargetData subsystem to do this.
194  ///
195  bool isSized() const {
196    return ID != VoidTyID && ID != TypeTyID &&
197           ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
198  }
199
200  /// getPrimitiveSize - Return the basic size of this type if it is a primative
201  /// type.  These are fixed by LLVM and are not target dependent.  This will
202  /// return zero if the type does not have a size or is not a primitive type.
203  ///
204  unsigned getPrimitiveSize() const;
205
206  /// getForwaredType - Return the type that this type has been resolved to if
207  /// it has been resolved to anything.  This is used to implement the
208  /// union-find algorithm for type resolution.
209  const Type *getForwardedType() const {
210    if (!ForwardType) return 0;
211    return getForwardedTypeInternal();
212  }
213
214  //===--------------------------------------------------------------------===//
215  // Type Iteration support
216  //
217  typedef std::vector<PATypeHandle>::const_iterator subtype_iterator;
218  subtype_iterator subtype_begin() const { return ContainedTys.begin(); }
219  subtype_iterator subtype_end() const { return ContainedTys.end(); }
220
221  /// getContainedType - This method is used to implement the type iterator
222  /// (defined a the end of the file).  For derived types, this returns the
223  /// types 'contained' in the derived type.
224  ///
225  const Type *getContainedType(unsigned i) const {
226    assert(i < ContainedTys.size() && "Index out of range!");
227    return ContainedTys[i];
228  }
229
230  /// getNumContainedTypes - Return the number of types in the derived type.
231  ///
232  unsigned getNumContainedTypes() const { return ContainedTys.size(); }
233
234  //===--------------------------------------------------------------------===//
235  // Static members exported by the Type class itself.  Useful for getting
236  // instances of Type.
237  //
238
239  /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
240  static const Type *getPrimitiveType(PrimitiveID IDNumber);
241  static const Type *getUniqueIDType(unsigned UID);
242
243  //===--------------------------------------------------------------------===//
244  // These are the builtin types that are always available...
245  //
246  static Type *VoidTy , *BoolTy;
247  static Type *SByteTy, *UByteTy,
248              *ShortTy, *UShortTy,
249              *IntTy  , *UIntTy,
250              *LongTy , *ULongTy;
251  static Type *FloatTy, *DoubleTy;
252
253  static Type *TypeTy , *LabelTy;
254
255  /// Methods for support type inquiry through isa, cast, and dyn_cast:
256  static inline bool classof(const Type *T) { return true; }
257  static inline bool classof(const Value *V) {
258    return V->getValueType() == Value::TypeVal;
259  }
260
261#include "llvm/Type.def"
262};
263
264// Provide specializations of GraphTraits to be able to treat a type as a
265// graph of sub types...
266
267template <> struct GraphTraits<Type*> {
268  typedef Type NodeType;
269  typedef Type::subtype_iterator ChildIteratorType;
270
271  static inline NodeType *getEntryNode(Type *T) { return T; }
272  static inline ChildIteratorType child_begin(NodeType *N) {
273    return N->subtype_begin();
274  }
275  static inline ChildIteratorType child_end(NodeType *N) {
276    return N->subtype_end();
277  }
278};
279
280template <> struct GraphTraits<const Type*> {
281  typedef const Type NodeType;
282  typedef Type::subtype_iterator ChildIteratorType;
283
284  static inline NodeType *getEntryNode(const Type *T) { return T; }
285  static inline ChildIteratorType child_begin(NodeType *N) {
286    return N->subtype_begin();
287  }
288  static inline ChildIteratorType child_end(NodeType *N) {
289    return N->subtype_end();
290  }
291};
292
293template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) {
294  return Ty.getPrimitiveID() == Type::PointerTyID;
295}
296
297} // End llvm namespace
298
299#endif
300