s/convertable/convertible/g
[oota-llvm.git] / include / llvm / Type.h
1 //===-- llvm/Type.h - Classes for handling data types ------------*- C++ -*--=//
2 //
3 // This file contains the declaration of the Type class.  For more "Type" type
4 // stuff, look in DerivedTypes.h.
5 //
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.
10 //
11 // Types, once allocated, are never free'd.
12 //
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.
17 //
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.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #ifndef LLVM_TYPE_H
27 #define LLVM_TYPE_H
28
29 #include "llvm/Value.h"
30 #include "Support/GraphTraits.h"
31 #include "Support/iterator"
32
33 class DerivedType;
34 class FunctionType;
35 class ArrayType;
36 class PointerType;
37 class StructType;
38 class OpaqueType;
39
40 class Type : public Value {
41 public:
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!
47   ///
48   enum PrimitiveID {
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...
54
55     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
56
57     TypeTyID,                           // 12   : Type definitions
58     LabelTyID     ,                     // 13   : Labels... 
59
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
66     //...
67
68     NumPrimitiveIDs,                    // Must remain as last defined ID
69     FirstDerivedTyID = FunctionTyID,
70   };
71
72 private:
73   PrimitiveID ID;        // The current base type of this type...
74   unsigned    UID;       // The unique ID number for this class
75   std::string Desc;      // The printed name of the string...
76   bool        Abstract;  // True if type contains an OpaqueType
77   bool        Recursive; // True if the type is recursive
78
79 protected:
80   /// ctor is protected, so only subclasses can create Type objects...
81   Type(const std::string &Name, PrimitiveID id);
82   virtual ~Type() {}
83
84   /// When types are refined, they update their description to be more concrete.
85   ///
86   inline void setDescription(const std::string &D) { Desc = D; }
87   
88   /// setName - Associate the name with this type in the symbol table, but don't
89   /// set the local name to be equal specified name.
90   ///
91   virtual void setName(const std::string &Name, SymbolTable *ST = 0);
92
93   /// Types can become nonabstract later, if they are refined.
94   ///
95   inline void setAbstract(bool Val) { Abstract = Val; }
96
97   /// Types can become recursive later, if they are refined.
98   ///
99   inline void setRecursive(bool Val) { Recursive = Val; }
100
101 public:
102   virtual void print(std::ostream &O) const;
103
104   //===--------------------------------------------------------------------===//
105   // Property accessors for dealing with types... Some of these virtual methods
106   // are defined in private classes defined in Type.cpp for primitive types.
107   //
108
109   /// getPrimitiveID - Return the base type of the type.  This will return one
110   /// of the PrimitiveID enum elements defined above.
111   ///
112   inline PrimitiveID getPrimitiveID() const { return ID; }
113
114   /// getUniqueID - Returns the UID of the type.  This can be thought of as a
115   /// small integer version of the pointer to the type class.  Two types that
116   /// are structurally different have different UIDs.  This can be used for
117   /// indexing types into an array.
118   ///
119   inline unsigned getUniqueID() const { return UID; }
120
121   /// getDescription - Return the string representation of the type...
122   inline const std::string &getDescription() const { return Desc; }
123
124   /// isSigned - Return whether an integral numeric type is signed.  This is
125   /// true for SByteTy, ShortTy, IntTy, LongTy.  Note that this is not true for
126   /// Float and Double.
127   //
128   virtual bool isSigned() const { return 0; }
129   
130   /// isUnsigned - Return whether a numeric type is unsigned.  This is not quite
131   /// the complement of isSigned... nonnumeric types return false as they do
132   /// with isSigned.  This returns true for UByteTy, UShortTy, UIntTy, and
133   /// ULongTy
134   /// 
135   virtual bool isUnsigned() const { return 0; }
136
137   /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
138   /// single virtual function invocation.
139   ///
140   virtual bool isInteger() const { return 0; }
141
142   /// isIntegral - Returns true if this is an integral type, which is either
143   /// BoolTy or one of the Integer types.
144   ///
145   bool isIntegral() const { return isInteger() || this == BoolTy; }
146
147   /// isFloatingPoint - Return true if this is one of the two floating point
148   /// types
149   bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
150
151   /// isAbstract - True if the type is either an Opaque type, or is a derived
152   /// type that includes an opaque type somewhere in it.  
153   ///
154   inline bool isAbstract() const { return Abstract; }
155
156   /// isRecursive - True if the type graph contains a cycle.
157   ///
158   inline bool isRecursive() const { return Recursive; }
159
160   /// isLosslesslyConvertibleTo - Return true if this type can be converted to
161   /// 'Ty' without any reinterpretation of bits.  For example, uint to int.
162   ///
163   bool isLosslesslyConvertibleTo(const Type *Ty) const;
164
165
166   /// Here are some useful little methods to query what type derived types are
167   /// Note that all other types can just compare to see if this == Type::xxxTy;
168   ///
169   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
170   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
171
172   /// isFirstClassType - Return true if the value is holdable in a register.
173   inline bool isFirstClassType() const {
174     return isPrimitiveType() || ID == PointerTyID;
175   }
176
177   /// isSized - Return true if it makes sense to take the size of this type.  To
178   /// get the actual size for a particular target, it is reasonable to use the
179   /// TargetData subsystem to do this.
180   ///
181   bool isSized() const {
182     return ID != VoidTyID && ID != TypeTyID &&
183            ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
184   }
185
186   /// getPrimitiveSize - Return the basic size of this type if it is a primative
187   /// type.  These are fixed by LLVM and are not target dependant.  This will
188   /// return zero if the type does not have a size or is not a primitive type.
189   ///
190   unsigned getPrimitiveSize() const;
191
192
193   //===--------------------------------------------------------------------===//
194   // Type Iteration support
195   //
196   class TypeIterator;
197   typedef TypeIterator subtype_iterator;
198   inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
199   inline subtype_iterator subtype_end() const;     // DEFINED BELOW
200
201   /// getContainedType - This method is used to implement the type iterator
202   /// (defined a the end of the file).  For derived types, this returns the
203   /// types 'contained' in the derived type, returning 0 when 'i' becomes
204   /// invalid. This allows the user to iterate over the types in a struct, for
205   /// example, really easily.
206   ///
207   virtual const Type *getContainedType(unsigned i) const { return 0; }
208
209   /// getNumContainedTypes - Return the number of types in the derived type
210   virtual unsigned getNumContainedTypes() const { return 0; }
211
212   //===--------------------------------------------------------------------===//
213   // Static members exported by the Type class itself.  Useful for getting
214   // instances of Type.
215   //
216
217   /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
218   static const Type *getPrimitiveType(PrimitiveID IDNumber);
219   static const Type *getUniqueIDType(unsigned UID);
220
221   //===--------------------------------------------------------------------===//
222   // These are the builtin types that are always available...
223   //
224   static Type *VoidTy , *BoolTy;
225   static Type *SByteTy, *UByteTy,
226               *ShortTy, *UShortTy,
227               *IntTy  , *UIntTy, 
228               *LongTy , *ULongTy;
229   static Type *FloatTy, *DoubleTy;
230
231   static Type *TypeTy , *LabelTy;
232
233   /// Methods for support type inquiry through isa, cast, and dyn_cast:
234   static inline bool classof(const Type *T) { return true; }
235   static inline bool classof(const Value *V) {
236     return V->getValueType() == Value::TypeVal;
237   }
238
239 #include "llvm/Type.def"
240
241 private:
242   class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
243     const Type * const Ty;
244     unsigned Idx;
245
246     typedef TypeIterator _Self;
247   public:
248     inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
249     inline ~TypeIterator() {}
250     
251     inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
252     inline bool operator!=(const _Self& x) const { return !operator==(x); }
253     
254     inline pointer operator*() const { return Ty->getContainedType(Idx); }
255     inline pointer operator->() const { return operator*(); }
256     
257     inline _Self& operator++() { ++Idx; return *this; } // Preincrement
258     inline _Self operator++(int) { // Postincrement
259       _Self tmp = *this; ++*this; return tmp; 
260     }
261     
262     inline _Self& operator--() { --Idx; return *this; }  // Predecrement
263     inline _Self operator--(int) { // Postdecrement
264       _Self tmp = *this; --*this; return tmp;
265     }
266   };
267 };
268
269 inline Type::TypeIterator Type::subtype_begin() const {
270   return TypeIterator(this, 0);
271 }
272
273 inline Type::TypeIterator Type::subtype_end() const {
274   return TypeIterator(this, getNumContainedTypes());
275 }
276
277
278 // Provide specializations of GraphTraits to be able to treat a type as a 
279 // graph of sub types...
280
281 template <> struct GraphTraits<Type*> {
282   typedef Type NodeType;
283   typedef Type::subtype_iterator ChildIteratorType;
284
285   static inline NodeType *getEntryNode(Type *T) { return T; }
286   static inline ChildIteratorType child_begin(NodeType *N) { 
287     return N->subtype_begin(); 
288   }
289   static inline ChildIteratorType child_end(NodeType *N) { 
290     return N->subtype_end();
291   }
292 };
293
294 template <> struct GraphTraits<const Type*> {
295   typedef const Type NodeType;
296   typedef Type::subtype_iterator ChildIteratorType;
297
298   static inline NodeType *getEntryNode(const Type *T) { return T; }
299   static inline ChildIteratorType child_begin(NodeType *N) { 
300     return N->subtype_begin(); 
301   }
302   static inline ChildIteratorType child_end(NodeType *N) { 
303     return N->subtype_end();
304   }
305 };
306
307 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 
308   return Ty.getPrimitiveID() == Type::PointerTyID;
309 }
310
311 #endif