1 //===-- llvm/Type.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 declaration of the Type class. For more "Type" type
11 // stuff, look in DerivedTypes.h.
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.
18 // Types, once allocated, are never free'd.
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.
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.
31 //===----------------------------------------------------------------------===//
36 #include "llvm/Value.h"
37 #include "Support/GraphTraits.h"
38 #include "Support/iterator"
47 struct Type : public Value {
48 ///===-------------------------------------------------------------------===//
49 /// Definitions of all of the base types for the Type system. Based on this
50 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
51 /// Note: If you add an element to this, you need to add an element to the
52 /// Type::getPrimitiveType function, or else things will break!
55 VoidTyID = 0 , BoolTyID, // 0, 1: Basics...
56 UByteTyID , SByteTyID, // 2, 3: 8 bit types...
57 UShortTyID , ShortTyID, // 4, 5: 16 bit types...
58 UIntTyID , IntTyID, // 6, 7: 32 bit types...
59 ULongTyID , LongTyID, // 8, 9: 64 bit types...
61 FloatTyID , DoubleTyID, // 10,11: Floating point types...
63 TypeTyID, // 12 : Type definitions
64 LabelTyID , // 13 : Labels...
66 // Derived types... see DerivedTypes.h file...
67 // Make sure FirstDerivedTyID stays up to date!!!
68 FunctionTyID , StructTyID, // Functions... Structs...
69 ArrayTyID , PointerTyID, // Array... pointer...
70 OpaqueTyID, // Opaque type instances...
71 //PackedTyID , // SIMD 'packed' format... TODO
74 NumPrimitiveIDs, // Must remain as last defined ID
75 FirstDerivedTyID = FunctionTyID,
79 PrimitiveID ID; // The current base type of this type...
80 unsigned UID; // The unique ID number for this class
81 bool Abstract; // True if type contains an OpaqueType
83 const Type *getForwardedTypeInternal() const;
85 /// ctor is protected, so only subclasses can create Type objects...
86 Type(const std::string &Name, PrimitiveID id);
89 /// setName - Associate the name with this type in the symbol table, but don't
90 /// set the local name to be equal specified name.
92 virtual void setName(const std::string &Name, SymbolTable *ST = 0);
94 /// Types can become nonabstract later, if they are refined.
96 inline void setAbstract(bool Val) { Abstract = Val; }
98 /// isTypeAbstract - This method is used to calculate the Abstract bit.
100 bool isTypeAbstract();
102 /// ForwardType - This field is used to implement the union find scheme for
103 /// abstract types. When types are refined to other types, this field is set
104 /// to the more refined type. Only abstract types can be forwarded.
105 mutable const Type *ForwardType;
108 virtual void print(std::ostream &O) const;
110 //===--------------------------------------------------------------------===//
111 // Property accessors for dealing with types... Some of these virtual methods
112 // are defined in private classes defined in Type.cpp for primitive types.
115 /// getPrimitiveID - Return the base type of the type. This will return one
116 /// of the PrimitiveID enum elements defined above.
118 inline PrimitiveID getPrimitiveID() const { return ID; }
120 /// getUniqueID - Returns the UID of the type. This can be thought of as a
121 /// small integer version of the pointer to the type class. Two types that
122 /// are structurally different have different UIDs. This can be used for
123 /// indexing types into an array.
125 inline unsigned getUniqueID() const { return UID; }
127 /// getDescription - Return the string representation of the type...
128 const std::string &getDescription() const;
130 /// isSigned - Return whether an integral numeric type is signed. This is
131 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for
132 /// Float and Double.
134 virtual bool isSigned() const { return 0; }
136 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite
137 /// the complement of isSigned... nonnumeric types return false as they do
138 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and
141 virtual bool isUnsigned() const { return 0; }
143 /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
144 /// single virtual function invocation.
146 virtual bool isInteger() const { return 0; }
148 /// isIntegral - Returns true if this is an integral type, which is either
149 /// BoolTy or one of the Integer types.
151 bool isIntegral() const { return isInteger() || this == BoolTy; }
153 /// isFloatingPoint - Return true if this is one of the two floating point
155 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
157 /// isAbstract - True if the type is either an Opaque type, or is a derived
158 /// type that includes an opaque type somewhere in it.
160 inline bool isAbstract() const { return Abstract; }
162 /// isLosslesslyConvertibleTo - Return true if this type can be converted to
163 /// 'Ty' without any reinterpretation of bits. For example, uint to int.
165 bool isLosslesslyConvertibleTo(const Type *Ty) const;
168 /// Here are some useful little methods to query what type derived types are
169 /// Note that all other types can just compare to see if this == Type::xxxTy;
171 inline bool isPrimitiveType() const { return ID < FirstDerivedTyID; }
172 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; }
174 /// isFirstClassType - Return true if the value is holdable in a register.
175 inline bool isFirstClassType() const {
176 return isPrimitiveType() || ID == PointerTyID;
179 /// isSized - Return true if it makes sense to take the size of this type. To
180 /// get the actual size for a particular target, it is reasonable to use the
181 /// TargetData subsystem to do this.
183 bool isSized() const {
184 return ID != VoidTyID && ID != TypeTyID &&
185 ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
188 /// getPrimitiveSize - Return the basic size of this type if it is a primative
189 /// type. These are fixed by LLVM and are not target dependent. This will
190 /// return zero if the type does not have a size or is not a primitive type.
192 unsigned getPrimitiveSize() const;
194 /// getForwaredType - Return the type that this type has been resolved to if
195 /// it has been resolved to anything. This is used to implement the
196 /// union-find algorithm for type resolution.
197 const Type *getForwardedType() const {
198 if (!ForwardType) return 0;
199 return getForwardedTypeInternal();
202 //===--------------------------------------------------------------------===//
203 // Type Iteration support
206 typedef TypeIterator subtype_iterator;
207 inline subtype_iterator subtype_begin() const; // DEFINED BELOW
208 inline subtype_iterator subtype_end() const; // DEFINED BELOW
210 /// getContainedType - This method is used to implement the type iterator
211 /// (defined a the end of the file). For derived types, this returns the
212 /// types 'contained' in the derived type.
214 virtual const Type *getContainedType(unsigned i) const {
215 assert(0 && "No contained types!");
219 /// getNumContainedTypes - Return the number of types in the derived type
220 virtual unsigned getNumContainedTypes() const { return 0; }
222 //===--------------------------------------------------------------------===//
223 // Static members exported by the Type class itself. Useful for getting
224 // instances of Type.
227 /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
228 static const Type *getPrimitiveType(PrimitiveID IDNumber);
229 static const Type *getUniqueIDType(unsigned UID);
231 //===--------------------------------------------------------------------===//
232 // These are the builtin types that are always available...
234 static Type *VoidTy , *BoolTy;
235 static Type *SByteTy, *UByteTy,
239 static Type *FloatTy, *DoubleTy;
241 static Type *TypeTy , *LabelTy;
243 /// Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const Type *T) { return true; }
245 static inline bool classof(const Value *V) {
246 return V->getValueType() == Value::TypeVal;
249 #include "llvm/Type.def"
252 class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
253 const Type * const Ty;
256 typedef TypeIterator _Self;
258 TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
261 const _Self &operator=(const _Self &RHS) {
262 assert(Ty == RHS.Ty && "Cannot assign from different types!");
267 bool operator==(const _Self& x) const { return Idx == x.Idx; }
268 bool operator!=(const _Self& x) const { return !operator==(x); }
270 pointer operator*() const { return Ty->getContainedType(Idx); }
271 pointer operator->() const { return operator*(); }
273 _Self& operator++() { ++Idx; return *this; } // Preincrement
274 _Self operator++(int) { // Postincrement
275 _Self tmp = *this; ++*this; return tmp;
278 _Self& operator--() { --Idx; return *this; } // Predecrement
279 _Self operator--(int) { // Postdecrement
280 _Self tmp = *this; --*this; return tmp;
285 inline Type::TypeIterator Type::subtype_begin() const {
286 return TypeIterator(this, 0);
289 inline Type::TypeIterator Type::subtype_end() const {
290 return TypeIterator(this, getNumContainedTypes());
294 // Provide specializations of GraphTraits to be able to treat a type as a
295 // graph of sub types...
297 template <> struct GraphTraits<Type*> {
298 typedef Type NodeType;
299 typedef Type::subtype_iterator ChildIteratorType;
301 static inline NodeType *getEntryNode(Type *T) { return T; }
302 static inline ChildIteratorType child_begin(NodeType *N) {
303 return N->subtype_begin();
305 static inline ChildIteratorType child_end(NodeType *N) {
306 return N->subtype_end();
310 template <> struct GraphTraits<const Type*> {
311 typedef const Type NodeType;
312 typedef Type::subtype_iterator ChildIteratorType;
314 static inline NodeType *getEntryNode(const Type *T) { return T; }
315 static inline ChildIteratorType child_begin(NodeType *N) {
316 return N->subtype_begin();
318 static inline ChildIteratorType child_end(NodeType *N) {
319 return N->subtype_end();
323 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) {
324 return Ty.getPrimitiveID() == Type::PointerTyID;