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"
49 struct Type : public Value {
50 ///===-------------------------------------------------------------------===//
51 /// Definitions of all of the base types for the Type system. Based on this
52 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
53 /// Note: If you add an element to this, you need to add an element to the
54 /// Type::getPrimitiveType function, or else things will break!
57 VoidTyID = 0 , BoolTyID, // 0, 1: Basics...
58 UByteTyID , SByteTyID, // 2, 3: 8 bit types...
59 UShortTyID , ShortTyID, // 4, 5: 16 bit types...
60 UIntTyID , IntTyID, // 6, 7: 32 bit types...
61 ULongTyID , LongTyID, // 8, 9: 64 bit types...
63 FloatTyID , DoubleTyID, // 10,11: Floating point types...
65 TypeTyID, // 12 : Type definitions
66 LabelTyID , // 13 : Labels...
68 // Derived types... see DerivedTypes.h file...
69 // Make sure FirstDerivedTyID stays up to date!!!
70 FunctionTyID , StructTyID, // Functions... Structs...
71 ArrayTyID , PointerTyID, // Array... pointer...
72 OpaqueTyID, // Opaque type instances...
73 //PackedTyID , // SIMD 'packed' format... TODO
76 NumPrimitiveIDs, // Must remain as last defined ID
77 FirstDerivedTyID = FunctionTyID,
81 PrimitiveID ID; // The current base type of this type...
82 unsigned UID; // The unique ID number for this class
83 bool Abstract; // True if type contains an OpaqueType
85 const Type *getForwardedTypeInternal() const;
87 /// ctor is protected, so only subclasses can create Type objects...
88 Type(const std::string &Name, PrimitiveID id);
91 /// setName - Associate the name with this type in the symbol table, but don't
92 /// set the local name to be equal specified name.
94 virtual void setName(const std::string &Name, SymbolTable *ST = 0);
96 /// Types can become nonabstract later, if they are refined.
98 inline void setAbstract(bool Val) { Abstract = Val; }
100 /// isTypeAbstract - This method is used to calculate the Abstract bit.
102 bool isTypeAbstract();
104 /// ForwardType - This field is used to implement the union find scheme for
105 /// abstract types. When types are refined to other types, this field is set
106 /// to the more refined type. Only abstract types can be forwarded.
107 mutable const Type *ForwardType;
110 virtual void print(std::ostream &O) const;
112 //===--------------------------------------------------------------------===//
113 // Property accessors for dealing with types... Some of these virtual methods
114 // are defined in private classes defined in Type.cpp for primitive types.
117 /// getPrimitiveID - Return the base type of the type. This will return one
118 /// of the PrimitiveID enum elements defined above.
120 inline PrimitiveID getPrimitiveID() const { return ID; }
122 /// getUniqueID - Returns the UID of the type. This can be thought of as a
123 /// small integer version of the pointer to the type class. Two types that
124 /// are structurally different have different UIDs. This can be used for
125 /// indexing types into an array.
127 inline unsigned getUniqueID() const { return UID; }
129 /// getDescription - Return the string representation of the type...
130 const std::string &getDescription() const;
132 /// isSigned - Return whether an integral numeric type is signed. This is
133 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for
134 /// Float and Double.
136 virtual bool isSigned() const { return 0; }
138 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite
139 /// the complement of isSigned... nonnumeric types return false as they do
140 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and
143 virtual bool isUnsigned() const { return 0; }
145 /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
146 /// single virtual function invocation.
148 virtual bool isInteger() const { return 0; }
150 /// isIntegral - Returns true if this is an integral type, which is either
151 /// BoolTy or one of the Integer types.
153 bool isIntegral() const { return isInteger() || this == BoolTy; }
155 /// isFloatingPoint - Return true if this is one of the two floating point
157 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
159 /// isAbstract - True if the type is either an Opaque type, or is a derived
160 /// type that includes an opaque type somewhere in it.
162 inline bool isAbstract() const { return Abstract; }
164 /// isLosslesslyConvertibleTo - Return true if this type can be converted to
165 /// 'Ty' without any reinterpretation of bits. For example, uint to int.
167 bool isLosslesslyConvertibleTo(const Type *Ty) const;
170 /// Here are some useful little methods to query what type derived types are
171 /// Note that all other types can just compare to see if this == Type::xxxTy;
173 inline bool isPrimitiveType() const { return ID < FirstDerivedTyID; }
174 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; }
176 /// isFirstClassType - Return true if the value is holdable in a register.
177 inline bool isFirstClassType() const {
178 return (ID != VoidTyID && ID < TypeTyID) || ID == PointerTyID;
181 /// isSized - Return true if it makes sense to take the size of this type. To
182 /// get the actual size for a particular target, it is reasonable to use the
183 /// TargetData subsystem to do this.
185 bool isSized() const {
186 return ID != VoidTyID && ID != TypeTyID &&
187 ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
190 /// getPrimitiveSize - Return the basic size of this type if it is a primative
191 /// type. These are fixed by LLVM and are not target dependent. This will
192 /// return zero if the type does not have a size or is not a primitive type.
194 unsigned getPrimitiveSize() const;
196 /// getForwaredType - Return the type that this type has been resolved to if
197 /// it has been resolved to anything. This is used to implement the
198 /// union-find algorithm for type resolution.
199 const Type *getForwardedType() const {
200 if (!ForwardType) return 0;
201 return getForwardedTypeInternal();
204 //===--------------------------------------------------------------------===//
205 // Type Iteration support
208 typedef TypeIterator subtype_iterator;
209 inline subtype_iterator subtype_begin() const; // DEFINED BELOW
210 inline subtype_iterator subtype_end() const; // DEFINED BELOW
212 /// getContainedType - This method is used to implement the type iterator
213 /// (defined a the end of the file). For derived types, this returns the
214 /// types 'contained' in the derived type.
216 virtual const Type *getContainedType(unsigned i) const {
217 assert(0 && "No contained types!");
221 /// getNumContainedTypes - Return the number of types in the derived type
222 virtual unsigned getNumContainedTypes() const { return 0; }
224 //===--------------------------------------------------------------------===//
225 // Static members exported by the Type class itself. Useful for getting
226 // instances of Type.
229 /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
230 static const Type *getPrimitiveType(PrimitiveID IDNumber);
231 static const Type *getUniqueIDType(unsigned UID);
233 //===--------------------------------------------------------------------===//
234 // These are the builtin types that are always available...
236 static Type *VoidTy , *BoolTy;
237 static Type *SByteTy, *UByteTy,
241 static Type *FloatTy, *DoubleTy;
243 static Type *TypeTy , *LabelTy;
245 /// Methods for support type inquiry through isa, cast, and dyn_cast:
246 static inline bool classof(const Type *T) { return true; }
247 static inline bool classof(const Value *V) {
248 return V->getValueType() == Value::TypeVal;
251 #include "llvm/Type.def"
254 class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
255 const Type * const Ty;
258 typedef TypeIterator _Self;
260 TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
263 const _Self &operator=(const _Self &RHS) {
264 assert(Ty == RHS.Ty && "Cannot assign from different types!");
269 bool operator==(const _Self& x) const { return Idx == x.Idx; }
270 bool operator!=(const _Self& x) const { return !operator==(x); }
272 pointer operator*() const { return Ty->getContainedType(Idx); }
273 pointer operator->() const { return operator*(); }
275 _Self& operator++() { ++Idx; return *this; } // Preincrement
276 _Self operator++(int) { // Postincrement
277 _Self tmp = *this; ++*this; return tmp;
280 _Self& operator--() { --Idx; return *this; } // Predecrement
281 _Self operator--(int) { // Postdecrement
282 _Self tmp = *this; --*this; return tmp;
287 inline Type::TypeIterator Type::subtype_begin() const {
288 return TypeIterator(this, 0);
291 inline Type::TypeIterator Type::subtype_end() const {
292 return TypeIterator(this, getNumContainedTypes());
296 // Provide specializations of GraphTraits to be able to treat a type as a
297 // graph of sub types...
299 template <> struct GraphTraits<Type*> {
300 typedef Type NodeType;
301 typedef Type::subtype_iterator ChildIteratorType;
303 static inline NodeType *getEntryNode(Type *T) { return T; }
304 static inline ChildIteratorType child_begin(NodeType *N) {
305 return N->subtype_begin();
307 static inline ChildIteratorType child_end(NodeType *N) {
308 return N->subtype_end();
312 template <> struct GraphTraits<const Type*> {
313 typedef const Type NodeType;
314 typedef Type::subtype_iterator ChildIteratorType;
316 static inline NodeType *getEntryNode(const Type *T) { return T; }
317 static inline ChildIteratorType child_begin(NodeType *N) {
318 return N->subtype_begin();
320 static inline ChildIteratorType child_end(NodeType *N) {
321 return N->subtype_end();
325 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) {
326 return Ty.getPrimitiveID() == Type::PointerTyID;
329 } // End llvm namespace