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