ba4ac3415fd2d3b037cb3a3b2543c7a03df4a599
[oota-llvm.git] / include / llvm / Type.h
1 //===-- llvm/Type.h - Classes for handling data types -----------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Type class.  For more "Type" type
11 // stuff, look in DerivedTypes.h.
12 //
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.
17 //
18 // Types, once allocated, are never free'd.
19 //
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.
24 //
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.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #ifndef LLVM_TYPE_H
34 #define LLVM_TYPE_H
35
36 #include "llvm/Value.h"
37 #include "Support/GraphTraits.h"
38 #include "Support/iterator"
39
40 namespace llvm {
41
42 class DerivedType;
43 class FunctionType;
44 class ArrayType;
45 class PointerType;
46 class StructType;
47 class OpaqueType;
48
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!
55   ///
56   enum PrimitiveID {
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...
62
63     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
64
65     TypeTyID,                           // 12   : Type definitions
66     LabelTyID     ,                     // 13   : Labels... 
67
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
74     //...
75
76     NumPrimitiveIDs,                    // Must remain as last defined ID
77     FirstDerivedTyID = FunctionTyID,
78   };
79
80 private:
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
84
85   const Type *getForwardedTypeInternal() const;
86 protected:
87   /// ctor is protected, so only subclasses can create Type objects...
88   Type(const std::string &Name, PrimitiveID id);
89   virtual ~Type() {}
90
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.
93   ///
94   virtual void setName(const std::string &Name, SymbolTable *ST = 0);
95
96   /// Types can become nonabstract later, if they are refined.
97   ///
98   inline void setAbstract(bool Val) { Abstract = Val; }
99
100   /// isTypeAbstract - This method is used to calculate the Abstract bit.
101   ///
102   bool isTypeAbstract();
103
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;
108
109 public:
110   virtual void print(std::ostream &O) const;
111
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.
115   //
116
117   /// getPrimitiveID - Return the base type of the type.  This will return one
118   /// of the PrimitiveID enum elements defined above.
119   ///
120   inline PrimitiveID getPrimitiveID() const { return ID; }
121
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.
126   ///
127   inline unsigned getUniqueID() const { return UID; }
128
129   /// getDescription - Return the string representation of the type...
130   const std::string &getDescription() const;
131
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.
135   //
136   virtual bool isSigned() const { return 0; }
137   
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
141   /// ULongTy
142   /// 
143   virtual bool isUnsigned() const { return 0; }
144
145   /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
146   /// single virtual function invocation.
147   ///
148   virtual bool isInteger() const { return 0; }
149
150   /// isIntegral - Returns true if this is an integral type, which is either
151   /// BoolTy or one of the Integer types.
152   ///
153   bool isIntegral() const { return isInteger() || this == BoolTy; }
154
155   /// isFloatingPoint - Return true if this is one of the two floating point
156   /// types
157   bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
158
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.  
161   ///
162   inline bool isAbstract() const { return Abstract; }
163
164   /// isLosslesslyConvertibleTo - Return true if this type can be converted to
165   /// 'Ty' without any reinterpretation of bits.  For example, uint to int.
166   ///
167   bool isLosslesslyConvertibleTo(const Type *Ty) const;
168
169
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;
172   ///
173   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
174   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
175
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;
179   }
180
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.
184   ///
185   bool isSized() const {
186     return ID != VoidTyID && ID != TypeTyID &&
187            ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
188   }
189
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.
193   ///
194   unsigned getPrimitiveSize() const;
195
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();
202   }
203
204   //===--------------------------------------------------------------------===//
205   // Type Iteration support
206   //
207   class TypeIterator;
208   typedef TypeIterator subtype_iterator;
209   inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
210   inline subtype_iterator subtype_end() const;     // DEFINED BELOW
211
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.
215   ///
216   virtual const Type *getContainedType(unsigned i) const {
217     assert(0 && "No contained types!");
218     return 0;
219   }
220
221   /// getNumContainedTypes - Return the number of types in the derived type
222   virtual unsigned getNumContainedTypes() const { return 0; }
223
224   //===--------------------------------------------------------------------===//
225   // Static members exported by the Type class itself.  Useful for getting
226   // instances of Type.
227   //
228
229   /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
230   static const Type *getPrimitiveType(PrimitiveID IDNumber);
231   static const Type *getUniqueIDType(unsigned UID);
232
233   //===--------------------------------------------------------------------===//
234   // These are the builtin types that are always available...
235   //
236   static Type *VoidTy , *BoolTy;
237   static Type *SByteTy, *UByteTy,
238               *ShortTy, *UShortTy,
239               *IntTy  , *UIntTy, 
240               *LongTy , *ULongTy;
241   static Type *FloatTy, *DoubleTy;
242
243   static Type *TypeTy , *LabelTy;
244
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;
249   }
250
251 #include "llvm/Type.def"
252
253 private:
254   class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
255     const Type * const Ty;
256     unsigned Idx;
257
258     typedef TypeIterator _Self;
259   public:
260     TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
261     ~TypeIterator() {}
262
263     const _Self &operator=(const _Self &RHS) {
264       assert(Ty == RHS.Ty && "Cannot assign from different types!");
265       Idx = RHS.Idx;
266       return *this;
267     }
268     
269     bool operator==(const _Self& x) const { return Idx == x.Idx; }
270     bool operator!=(const _Self& x) const { return !operator==(x); }
271     
272     pointer operator*() const { return Ty->getContainedType(Idx); }
273     pointer operator->() const { return operator*(); }
274     
275     _Self& operator++() { ++Idx; return *this; } // Preincrement
276     _Self operator++(int) { // Postincrement
277       _Self tmp = *this; ++*this; return tmp; 
278     }
279     
280     _Self& operator--() { --Idx; return *this; }  // Predecrement
281     _Self operator--(int) { // Postdecrement
282       _Self tmp = *this; --*this; return tmp;
283     }
284   };
285 };
286
287 inline Type::TypeIterator Type::subtype_begin() const {
288   return TypeIterator(this, 0);
289 }
290
291 inline Type::TypeIterator Type::subtype_end() const {
292   return TypeIterator(this, getNumContainedTypes());
293 }
294
295
296 // Provide specializations of GraphTraits to be able to treat a type as a 
297 // graph of sub types...
298
299 template <> struct GraphTraits<Type*> {
300   typedef Type NodeType;
301   typedef Type::subtype_iterator ChildIteratorType;
302
303   static inline NodeType *getEntryNode(Type *T) { return T; }
304   static inline ChildIteratorType child_begin(NodeType *N) { 
305     return N->subtype_begin(); 
306   }
307   static inline ChildIteratorType child_end(NodeType *N) { 
308     return N->subtype_end();
309   }
310 };
311
312 template <> struct GraphTraits<const Type*> {
313   typedef const Type NodeType;
314   typedef Type::subtype_iterator ChildIteratorType;
315
316   static inline NodeType *getEntryNode(const Type *T) { return T; }
317   static inline ChildIteratorType child_begin(NodeType *N) { 
318     return N->subtype_begin(); 
319   }
320   static inline ChildIteratorType child_end(NodeType *N) { 
321     return N->subtype_end();
322   }
323 };
324
325 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 
326   return Ty.getPrimitiveID() == Type::PointerTyID;
327 }
328
329 } // End llvm namespace
330
331 #endif