1 //===-- llvm/Type.h - Classes for handling data types ------------*- C++ -*--=//
3 // This file contains the declaration of the Type class. For more "Type" type
4 // stuff, look in DerivedTypes.h.
6 // Note that instances of the Type class are immutable: once they are created,
7 // they are never changed. Also note that only one instance of a particular
8 // type is ever created. Thus seeing if two types are equal is a matter of
9 // doing a trivial pointer comparison.
11 // Types, once allocated, are never free'd.
13 // Opaque types are simple derived types with no state. There may be many
14 // different Opaque type objects floating around, but two are only considered
15 // identical if they are pointer equals of each other. This allows us to have
16 // two opaque types that end up resolving to different concrete types later.
18 // Opaque types are also kinda wierd and scary and different because they have
19 // to keep a list of uses of the type. When, through linking, parsing, or
20 // bytecode reading, they become resolved, they need to find and update all
21 // users of the unknown type, causing them to reference a new, more concrete
22 // type. Opaque types are deleted when their use list dwindles to zero users.
24 //===----------------------------------------------------------------------===//
29 #include "llvm/Value.h"
30 #include "Support/GraphTraits.h"
31 #include "Support/iterator"
40 class Type : public Value {
42 ///===-------------------------------------------------------------------===//
43 /// Definitions of all of the base types for the Type system. Based on this
44 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
45 /// Note: If you add an element to this, you need to add an element to the
46 /// Type::getPrimitiveType function, or else things will break!
49 VoidTyID = 0 , BoolTyID, // 0, 1: Basics...
50 UByteTyID , SByteTyID, // 2, 3: 8 bit types...
51 UShortTyID , ShortTyID, // 4, 5: 16 bit types...
52 UIntTyID , IntTyID, // 6, 7: 32 bit types...
53 ULongTyID , LongTyID, // 8, 9: 64 bit types...
55 FloatTyID , DoubleTyID, // 10,11: Floating point types...
57 TypeTyID, // 12 : Type definitions
58 LabelTyID , // 13 : Labels...
60 // Derived types... see DerivedTypes.h file...
61 // Make sure FirstDerivedTyID stays up to date!!!
62 FunctionTyID , StructTyID, // Functions... Structs...
63 ArrayTyID , PointerTyID, // Array... pointer...
64 OpaqueTyID, // Opaque type instances...
65 //PackedTyID , // SIMD 'packed' format... TODO
68 NumPrimitiveIDs, // Must remain as last defined ID
69 FirstDerivedTyID = FunctionTyID,
73 PrimitiveID ID; // The current base type of this type...
74 unsigned UID; // The unique ID number for this class
75 bool Abstract; // True if type contains an OpaqueType
76 bool Recursive; // True if the type is recursive
79 /// ctor is protected, so only subclasses can create Type objects...
80 Type(const std::string &Name, PrimitiveID id);
83 /// setName - Associate the name with this type in the symbol table, but don't
84 /// set the local name to be equal specified name.
86 virtual void setName(const std::string &Name, SymbolTable *ST = 0);
88 /// Types can become nonabstract later, if they are refined.
90 inline void setAbstract(bool Val) { Abstract = Val; }
92 /// Types can become recursive later, if they are refined.
94 inline void setRecursive(bool Val) { Recursive = Val; }
97 virtual void print(std::ostream &O) const;
99 //===--------------------------------------------------------------------===//
100 // Property accessors for dealing with types... Some of these virtual methods
101 // are defined in private classes defined in Type.cpp for primitive types.
104 /// getPrimitiveID - Return the base type of the type. This will return one
105 /// of the PrimitiveID enum elements defined above.
107 inline PrimitiveID getPrimitiveID() const { return ID; }
109 /// getUniqueID - Returns the UID of the type. This can be thought of as a
110 /// small integer version of the pointer to the type class. Two types that
111 /// are structurally different have different UIDs. This can be used for
112 /// indexing types into an array.
114 inline unsigned getUniqueID() const { return UID; }
116 /// getDescription - Return the string representation of the type...
117 const std::string &getDescription() const;
119 /// isSigned - Return whether an integral numeric type is signed. This is
120 /// true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for
121 /// Float and Double.
123 virtual bool isSigned() const { return 0; }
125 /// isUnsigned - Return whether a numeric type is unsigned. This is not quite
126 /// the complement of isSigned... nonnumeric types return false as they do
127 /// with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and
130 virtual bool isUnsigned() const { return 0; }
132 /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
133 /// single virtual function invocation.
135 virtual bool isInteger() const { return 0; }
137 /// isIntegral - Returns true if this is an integral type, which is either
138 /// BoolTy or one of the Integer types.
140 bool isIntegral() const { return isInteger() || this == BoolTy; }
142 /// isFloatingPoint - Return true if this is one of the two floating point
144 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
146 /// isAbstract - True if the type is either an Opaque type, or is a derived
147 /// type that includes an opaque type somewhere in it.
149 inline bool isAbstract() const { return Abstract; }
151 /// isRecursive - True if the type graph contains a cycle.
153 inline bool isRecursive() const { return Recursive; }
155 /// isLosslesslyConvertibleTo - Return true if this type can be converted to
156 /// 'Ty' without any reinterpretation of bits. For example, uint to int.
158 bool isLosslesslyConvertibleTo(const Type *Ty) const;
161 /// Here are some useful little methods to query what type derived types are
162 /// Note that all other types can just compare to see if this == Type::xxxTy;
164 inline bool isPrimitiveType() const { return ID < FirstDerivedTyID; }
165 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; }
167 /// isFirstClassType - Return true if the value is holdable in a register.
168 inline bool isFirstClassType() const {
169 return isPrimitiveType() || ID == PointerTyID;
172 /// isSized - Return true if it makes sense to take the size of this type. To
173 /// get the actual size for a particular target, it is reasonable to use the
174 /// TargetData subsystem to do this.
176 bool isSized() const {
177 return ID != VoidTyID && ID != TypeTyID &&
178 ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
181 /// getPrimitiveSize - Return the basic size of this type if it is a primative
182 /// type. These are fixed by LLVM and are not target dependent. This will
183 /// return zero if the type does not have a size or is not a primitive type.
185 unsigned getPrimitiveSize() const;
188 //===--------------------------------------------------------------------===//
189 // Type Iteration support
192 typedef TypeIterator subtype_iterator;
193 inline subtype_iterator subtype_begin() const; // DEFINED BELOW
194 inline subtype_iterator subtype_end() const; // DEFINED BELOW
196 /// getContainedType - This method is used to implement the type iterator
197 /// (defined a the end of the file). For derived types, this returns the
198 /// types 'contained' in the derived type, returning 0 when 'i' becomes
199 /// invalid. This allows the user to iterate over the types in a struct, for
200 /// example, really easily.
202 virtual const Type *getContainedType(unsigned i) const { return 0; }
204 /// getNumContainedTypes - Return the number of types in the derived type
205 virtual unsigned getNumContainedTypes() const { return 0; }
207 //===--------------------------------------------------------------------===//
208 // Static members exported by the Type class itself. Useful for getting
209 // instances of Type.
212 /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
213 static const Type *getPrimitiveType(PrimitiveID IDNumber);
214 static const Type *getUniqueIDType(unsigned UID);
216 //===--------------------------------------------------------------------===//
217 // These are the builtin types that are always available...
219 static Type *VoidTy , *BoolTy;
220 static Type *SByteTy, *UByteTy,
224 static Type *FloatTy, *DoubleTy;
226 static Type *TypeTy , *LabelTy;
228 /// Methods for support type inquiry through isa, cast, and dyn_cast:
229 static inline bool classof(const Type *T) { return true; }
230 static inline bool classof(const Value *V) {
231 return V->getValueType() == Value::TypeVal;
234 #include "llvm/Type.def"
237 class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
238 const Type * const Ty;
241 typedef TypeIterator _Self;
243 inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
244 inline ~TypeIterator() {}
246 inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
247 inline bool operator!=(const _Self& x) const { return !operator==(x); }
249 inline pointer operator*() const { return Ty->getContainedType(Idx); }
250 inline pointer operator->() const { return operator*(); }
252 inline _Self& operator++() { ++Idx; return *this; } // Preincrement
253 inline _Self operator++(int) { // Postincrement
254 _Self tmp = *this; ++*this; return tmp;
257 inline _Self& operator--() { --Idx; return *this; } // Predecrement
258 inline _Self operator--(int) { // Postdecrement
259 _Self tmp = *this; --*this; return tmp;
264 inline Type::TypeIterator Type::subtype_begin() const {
265 return TypeIterator(this, 0);
268 inline Type::TypeIterator Type::subtype_end() const {
269 return TypeIterator(this, getNumContainedTypes());
273 // Provide specializations of GraphTraits to be able to treat a type as a
274 // graph of sub types...
276 template <> struct GraphTraits<Type*> {
277 typedef Type NodeType;
278 typedef Type::subtype_iterator ChildIteratorType;
280 static inline NodeType *getEntryNode(Type *T) { return T; }
281 static inline ChildIteratorType child_begin(NodeType *N) {
282 return N->subtype_begin();
284 static inline ChildIteratorType child_end(NodeType *N) {
285 return N->subtype_end();
289 template <> struct GraphTraits<const Type*> {
290 typedef const Type NodeType;
291 typedef Type::subtype_iterator ChildIteratorType;
293 static inline NodeType *getEntryNode(const Type *T) { return T; }
294 static inline ChildIteratorType child_begin(NodeType *N) {
295 return N->subtype_begin();
297 static inline ChildIteratorType child_end(NodeType *N) {
298 return N->subtype_end();
302 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) {
303 return Ty.getPrimitiveID() == Type::PointerTyID;