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 and Opt/ConstantHandling.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"
41 class Type : public Value {
43 //===--------------------------------------------------------------------===//
44 // Definitions of all of the base types for the Type system. Based on this
45 // value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
46 // Note: If you add an element to this, you need to add an element to the
47 // Type::getPrimitiveType function, or else things will break!
50 VoidTyID = 0 , BoolTyID, // 0, 1: Basics...
51 UByteTyID , SByteTyID, // 2, 3: 8 bit types...
52 UShortTyID , ShortTyID, // 4, 5: 16 bit types...
53 UIntTyID , IntTyID, // 6, 7: 32 bit types...
54 ULongTyID , LongTyID, // 8, 9: 64 bit types...
56 FloatTyID , DoubleTyID, // 10,11: Floating point types...
58 TypeTyID, // 12 : Type definitions
59 LabelTyID , // 13 : Labels...
60 /*LockTyID , */ // 14 : mutex - TODO
62 // Derived types... see DerivedTypes.h file...
63 // Make sure FirstDerivedTyID stays up to date!!!
64 MethodTyID , ModuleTyID, // Methods... Modules...
65 ArrayTyID , PointerTyID, // Array... pointer...
66 StructTyID , OpaqueTyID, // Structure... Opaque type instances...
67 //PackedTyID , // SIMD 'packed' format... TODO
70 NumPrimitiveIDs, // Must remain as last defined ID
71 FirstDerivedTyID = MethodTyID,
75 PrimitiveID ID; // The current base type of this type...
76 unsigned UID; // The unique ID number for this class
77 string Desc; // The printed name of the string...
78 bool Abstract; // True if type contains an OpaqueType
79 bool Recursive; // True if the type is recursive
81 // ConstRulesImpl - See Opt/ConstantHandling.h for more info
82 mutable const opt::ConstRules *ConstRulesImpl;
85 // ctor is protected, so only subclasses can create Type objects...
86 Type(const string &Name, PrimitiveID id);
89 // When types are refined, they update their description to be more concrete.
91 inline void setDescription(const string &D) { Desc = D; }
93 // setName - Associate the name with this type in the symbol table, but don't
94 // set the local name to be equal specified name.
96 virtual void setName(const string &Name, SymbolTable *ST = 0);
98 // Types can become nonabstract later, if they are refined.
100 inline void setAbstract(bool Val) { Abstract = Val; }
102 // Types can become recursive later, if they are refined.
104 inline void setRecursive(bool Val) { Recursive = Val; }
108 //===--------------------------------------------------------------------===//
109 // Property accessors for dealing with types...
112 // getPrimitiveID - Return the base type of the type. This will return one
113 // of the PrimitiveID enum elements defined above.
115 inline PrimitiveID getPrimitiveID() const { return ID; }
117 // getUniqueID - Returns the UID of the type. This can be thought of as a
118 // small integer version of the pointer to the type class. Two types that are
119 // structurally different have different UIDs. This can be used for indexing
120 // types into an array.
122 inline unsigned getUniqueID() const { return UID; }
124 // getDescription - Return the string representation of the type...
125 inline const string &getDescription() const { return Desc; }
127 // isSigned - Return whether a numeric type is signed.
128 virtual bool isSigned() const { return 0; }
130 // isUnsigned - Return whether a numeric type is unsigned. This is not
131 // quite the complement of isSigned... nonnumeric types return false as they
134 virtual bool isUnsigned() const { return 0; }
136 // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
137 // virtual function invocation.
139 virtual bool isIntegral() const { return 0; }
141 // isAbstract - True if the type is either an Opaque type, or is a derived
142 // type that includes an opaque type somewhere in it.
144 inline bool isAbstract() const { return Abstract; }
146 // isRecursive - True if the type graph contains a cycle.
148 inline bool isRecursive() const { return Recursive; }
150 //===--------------------------------------------------------------------===//
151 // Type Iteration support
154 typedef TypeIterator contype_iterator;
155 inline contype_iterator contype_begin() const; // DEFINED BELOW
156 inline contype_iterator contype_end() const; // DEFINED BELOW
158 // getContainedType - This method is used to implement the type iterator
159 // (defined a the end of the file). For derived types, this returns the types
160 // 'contained' in the derived type, returning 0 when 'i' becomes invalid. This
161 // allows the user to iterate over the types in a struct, for example, really
164 virtual const Type *getContainedType(unsigned i) const { return 0; }
166 // getNumContainedTypes - Return the number of types in the derived type
167 virtual unsigned getNumContainedTypes() const { return 0; }
169 //===--------------------------------------------------------------------===//
170 // Static members exported by the Type class itself. Useful for getting
171 // instances of Type.
174 // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
175 static const Type *getPrimitiveType(PrimitiveID IDNumber);
176 static const Type *getUniqueIDType(unsigned UID);
178 // Methods for dealing with constants uniformly. See Opt/ConstantHandling.h
179 // for more info on this...
181 inline const opt::ConstRules *getConstRules() const { return ConstRulesImpl; }
182 inline void setConstRules(const opt::ConstRules *R) const { ConstRulesImpl=R;}
184 //===--------------------------------------------------------------------===//
185 // These are the builtin types that are always available...
187 static const Type *VoidTy , *BoolTy;
188 static const Type *SByteTy, *UByteTy,
192 static const Type *FloatTy, *DoubleTy;
194 static const Type *TypeTy , *LabelTy; //, *LockTy;
196 // Here are some useful little methods to query what type derived types are
197 // Note that all other types can just compare to see if this == Type::xxxTy;
199 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; }
200 inline bool isPrimitiveType() const { return ID < FirstDerivedTyID; }
202 inline bool isLabelType() const { return this == LabelTy; }
204 inline const DerivedType *castDerivedType() const {
205 return isDerivedType() ? (const DerivedType*)this : 0;
207 inline const DerivedType *castDerivedTypeAsserting() const {
208 assert(isDerivedType());
209 return (const DerivedType*)this;
212 inline const MethodType *isMethodType() const {
213 return ID == MethodTyID ? (const MethodType*)this : 0;
215 inline bool isModuleType() const { return ID == ModuleTyID; }
216 inline const ArrayType *isArrayType() const {
217 return ID == ArrayTyID ? (const ArrayType*)this : 0;
219 inline const PointerType *isPointerType() const {
220 return ID == PointerTyID ? (const PointerType*)this : 0;
222 inline const StructType *isStructType() const {
223 return ID == StructTyID ? (const StructType*)this : 0;
225 inline const OpaqueType *isOpaqueType() const {
226 return ID == OpaqueTyID ? (const OpaqueType*)this : 0;
230 class TypeIterator : public std::bidirectional_iterator<const Type,
232 const Type * const Ty;
235 typedef TypeIterator _Self;
237 inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
238 inline ~TypeIterator() {}
240 inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
241 inline bool operator!=(const _Self& x) const { return !operator==(x); }
243 inline pointer operator*() const { return Ty->getContainedType(Idx); }
244 inline pointer operator->() const { return operator*(); }
246 inline _Self& operator++() { ++Idx; return *this; } // Preincrement
247 inline _Self operator++(int) { // Postincrement
248 _Self tmp = *this; ++*this; return tmp;
251 inline _Self& operator--() { --Idx; return *this; } // Predecrement
252 inline _Self operator--(int) { // Postdecrement
253 _Self tmp = *this; --*this; return tmp;
258 inline Type::TypeIterator Type::contype_begin() const {
259 return TypeIterator(this, 0);
262 inline Type::TypeIterator Type::contype_end() const {
263 return TypeIterator(this, getNumContainedTypes());