Rename the intrinsic enum values for llvm.va_* from Intrinsic::va_* to
[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 #include <vector>
40
41 namespace llvm {
42
43 class DerivedType;
44 class FunctionType;
45 class ArrayType;
46 class PointerType;
47 class StructType;
48 class OpaqueType;
49
50 struct Type : public Value {
51   ///===-------------------------------------------------------------------===//
52   /// Definitions of all of the base types for the Type system.  Based on this
53   /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
54   /// Note: If you add an element to this, you need to add an element to the 
55   /// Type::getPrimitiveType function, or else things will break!
56   ///
57   enum PrimitiveID {
58     VoidTyID = 0  , BoolTyID,           //  0, 1: Basics...
59     UByteTyID     , SByteTyID,          //  2, 3: 8 bit types...
60     UShortTyID    , ShortTyID,          //  4, 5: 16 bit types...
61     UIntTyID      , IntTyID,            //  6, 7: 32 bit types...
62     ULongTyID     , LongTyID,           //  8, 9: 64 bit types...
63
64     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
65
66     TypeTyID,                           // 12   : Type definitions
67     LabelTyID     ,                     // 13   : Labels... 
68
69     // Derived types... see DerivedTypes.h file...
70     // Make sure FirstDerivedTyID stays up to date!!!
71     FunctionTyID  , StructTyID,         // Functions... Structs...
72     ArrayTyID     , PointerTyID,        // Array... pointer...
73     OpaqueTyID,                         // Opaque type instances...
74     //PackedTyID  ,                     // SIMD 'packed' format... TODO
75     //...
76
77     NumPrimitiveIDs,                    // Must remain as last defined ID
78     FirstDerivedTyID = FunctionTyID,
79   };
80
81 private:
82   PrimitiveID ID;        // The current base type of this type...
83   unsigned    UID;       // The unique ID number for this class
84   bool        Abstract;  // True if type contains an OpaqueType
85
86   /// RefCount - This counts the number of PATypeHolders that are pointing to
87   /// this type.  When this number falls to zero, if the type is abstract and
88   /// has no AbstractTypeUsers, the type is deleted.  This is only sensical for
89   /// derived types.
90   ///
91   mutable unsigned RefCount;
92
93   const Type *getForwardedTypeInternal() const;
94 protected:
95   /// ctor is protected, so only subclasses can create Type objects...
96   Type(const std::string &Name, PrimitiveID id);
97   virtual ~Type() {}
98
99   /// setName - Associate the name with this type in the symbol table, but don't
100   /// set the local name to be equal specified name.
101   ///
102   virtual void setName(const std::string &Name, SymbolTable *ST = 0);
103
104   /// Types can become nonabstract later, if they are refined.
105   ///
106   inline void setAbstract(bool Val) { Abstract = Val; }
107
108   /// isTypeAbstract - This method is used to calculate the Abstract bit.
109   ///
110   bool isTypeAbstract();
111
112   unsigned getRefCount() const { return RefCount; }
113
114   /// ForwardType - This field is used to implement the union find scheme for
115   /// abstract types.  When types are refined to other types, this field is set
116   /// to the more refined type.  Only abstract types can be forwarded.
117   mutable const Type *ForwardType;
118
119   /// ContainedTys - The list of types contained by this one.  For example, this
120   /// includes the arguments of a function type, the elements of the structure,
121   /// the pointee of a pointer, etc.  Note that keeping this vector in the Type
122   /// class wastes some space for types that do not contain anything (such as
123   /// primitive types).  However, keeping it here allows the subtype_* members
124   /// to be implemented MUCH more efficiently, and dynamically very few types do
125   /// not contain any elements (most are derived).
126   std::vector<PATypeHandle> ContainedTys;
127
128 public:
129   virtual void print(std::ostream &O) const;
130
131   //===--------------------------------------------------------------------===//
132   // Property accessors for dealing with types... Some of these virtual methods
133   // are defined in private classes defined in Type.cpp for primitive types.
134   //
135
136   /// getPrimitiveID - Return the base type of the type.  This will return one
137   /// of the PrimitiveID enum elements defined above.
138   ///
139   inline PrimitiveID getPrimitiveID() const { return ID; }
140
141   /// getUniqueID - Returns the UID of the type.  This can be thought of as a
142   /// small integer version of the pointer to the type class.  Two types that
143   /// are structurally different have different UIDs.  This can be used for
144   /// indexing types into an array.
145   ///
146   inline unsigned getUniqueID() const { return UID; }
147
148   /// getDescription - Return the string representation of the type...
149   const std::string &getDescription() const;
150
151   /// isSigned - Return whether an integral numeric type is signed.  This is
152   /// true for SByteTy, ShortTy, IntTy, LongTy.  Note that this is not true for
153   /// Float and Double.
154   ///
155   virtual bool isSigned() const { return 0; }
156   
157   /// isUnsigned - Return whether a numeric type is unsigned.  This is not quite
158   /// the complement of isSigned... nonnumeric types return false as they do
159   /// with isSigned.  This returns true for UByteTy, UShortTy, UIntTy, and
160   /// ULongTy
161   /// 
162   virtual bool isUnsigned() const { return 0; }
163
164   /// isInteger - Equilivent to isSigned() || isUnsigned(), but with only a
165   /// single virtual function invocation.
166   ///
167   virtual bool isInteger() const { return 0; }
168
169   /// isIntegral - Returns true if this is an integral type, which is either
170   /// BoolTy or one of the Integer types.
171   ///
172   bool isIntegral() const { return isInteger() || this == BoolTy; }
173
174   /// isFloatingPoint - Return true if this is one of the two floating point
175   /// types
176   bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
177
178   /// isAbstract - True if the type is either an Opaque type, or is a derived
179   /// type that includes an opaque type somewhere in it.  
180   ///
181   inline bool isAbstract() const { return Abstract; }
182
183   /// isLosslesslyConvertibleTo - Return true if this type can be converted to
184   /// 'Ty' without any reinterpretation of bits.  For example, uint to int.
185   ///
186   bool isLosslesslyConvertibleTo(const Type *Ty) const;
187
188
189   /// Here are some useful little methods to query what type derived types are
190   /// Note that all other types can just compare to see if this == Type::xxxTy;
191   ///
192   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
193   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
194
195   /// isFirstClassType - Return true if the value is holdable in a register.
196   inline bool isFirstClassType() const {
197     return (ID != VoidTyID && ID < TypeTyID) || ID == PointerTyID;
198   }
199
200   /// isSized - Return true if it makes sense to take the size of this type.  To
201   /// get the actual size for a particular target, it is reasonable to use the
202   /// TargetData subsystem to do this.
203   ///
204   bool isSized() const {
205     return ID != VoidTyID && ID != TypeTyID &&
206            ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
207   }
208
209   /// getPrimitiveSize - Return the basic size of this type if it is a primative
210   /// type.  These are fixed by LLVM and are not target dependent.  This will
211   /// return zero if the type does not have a size or is not a primitive type.
212   ///
213   unsigned getPrimitiveSize() const;
214
215   /// getForwaredType - Return the type that this type has been resolved to if
216   /// it has been resolved to anything.  This is used to implement the
217   /// union-find algorithm for type resolution.
218   const Type *getForwardedType() const {
219     if (!ForwardType) return 0;
220     return getForwardedTypeInternal();
221   }
222
223   //===--------------------------------------------------------------------===//
224   // Type Iteration support
225   //
226   typedef std::vector<PATypeHandle>::const_iterator subtype_iterator;
227   subtype_iterator subtype_begin() const { return ContainedTys.begin(); }
228   subtype_iterator subtype_end() const { return ContainedTys.end(); }
229
230   /// getContainedType - This method is used to implement the type iterator
231   /// (defined a the end of the file).  For derived types, this returns the
232   /// types 'contained' in the derived type.
233   ///
234   const Type *getContainedType(unsigned i) const {
235     assert(i < ContainedTys.size() && "Index out of range!");
236     return ContainedTys[i];
237   }
238
239   /// getNumContainedTypes - Return the number of types in the derived type.
240   ///
241   unsigned getNumContainedTypes() const { return ContainedTys.size(); }
242
243   //===--------------------------------------------------------------------===//
244   // Static members exported by the Type class itself.  Useful for getting
245   // instances of Type.
246   //
247
248   /// getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
249   static const Type *getPrimitiveType(PrimitiveID IDNumber);
250   static const Type *getUniqueIDType(unsigned UID);
251
252   //===--------------------------------------------------------------------===//
253   // These are the builtin types that are always available...
254   //
255   static Type *VoidTy , *BoolTy;
256   static Type *SByteTy, *UByteTy,
257               *ShortTy, *UShortTy,
258               *IntTy  , *UIntTy, 
259               *LongTy , *ULongTy;
260   static Type *FloatTy, *DoubleTy;
261
262   static Type *TypeTy , *LabelTy;
263
264   /// Methods for support type inquiry through isa, cast, and dyn_cast:
265   static inline bool classof(const Type *T) { return true; }
266   static inline bool classof(const Value *V) {
267     return V->getValueType() == Value::TypeVal;
268   }
269
270 #include "llvm/Type.def"
271
272   // Virtual methods used by callbacks below.  These should only be implemented
273   // in the DerivedType class.
274   virtual void addAbstractTypeUser(AbstractTypeUser *U) const {
275     abort(); // Only on derived types!
276   }
277   virtual void removeAbstractTypeUser(AbstractTypeUser *U) const {
278     abort(); // Only on derived types!
279   }
280
281   void addRef() const {
282     assert(isAbstract() && "Cannot add a reference to a non-abstract type!");
283     ++RefCount;
284   }
285   
286   void dropRef() const {
287     assert(isAbstract() && "Cannot drop a refernce to a non-abstract type!");
288     assert(RefCount && "No objects are currently referencing this object!");
289
290     // If this is the last PATypeHolder using this object, and there are no
291     // PATypeHandles using it, the type is dead, delete it now.
292     if (--RefCount == 0)
293       RefCountIsZero();
294   }
295 private:
296   virtual void RefCountIsZero() const {
297     abort(); // only on derived types!
298   }
299
300 };
301
302 //===----------------------------------------------------------------------===//
303 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class.
304 // These are defined here because they MUST be inlined, yet are dependent on 
305 // the definition of the Type class.  Of course Type derives from Value, which
306 // contains an AbstractTypeUser instance, so there is no good way to factor out
307 // the code.  Hence this bit of uglyness.
308 //
309 // In the long term, Type should not derive from Value, allowing
310 // AbstractTypeUser.h to #include Type.h, allowing us to eliminate this
311 // nastyness entirely.
312 //
313 inline void PATypeHandle::addUser() {
314   assert(Ty && "Type Handle has a null type!");
315   if (Ty->isAbstract())
316     Ty->addAbstractTypeUser(User);
317 }
318 inline void PATypeHandle::removeUser() {
319   if (Ty->isAbstract())
320     Ty->removeAbstractTypeUser(User);
321 }
322
323 inline void PATypeHandle::removeUserFromConcrete() {
324   if (!Ty->isAbstract())
325     Ty->removeAbstractTypeUser(User);
326 }
327
328 // Define inline methods for PATypeHolder...
329
330 inline void PATypeHolder::addRef() {
331   if (Ty->isAbstract())
332     Ty->addRef();
333 }
334
335 inline void PATypeHolder::dropRef() {
336   if (Ty->isAbstract())
337     Ty->dropRef();
338 }
339
340 /// get - This implements the forwarding part of the union-find algorithm for
341 /// abstract types.  Before every access to the Type*, we check to see if the
342 /// type we are pointing to is forwarding to a new type.  If so, we drop our
343 /// reference to the type.
344 ///
345 inline const Type* PATypeHolder::get() const {
346   const Type *NewTy = Ty->getForwardedType();
347   if (!NewTy) return Ty;
348   return *const_cast<PATypeHolder*>(this) = NewTy;
349 }
350
351
352
353 //===----------------------------------------------------------------------===//
354 // Provide specializations of GraphTraits to be able to treat a type as a 
355 // graph of sub types...
356
357 template <> struct GraphTraits<Type*> {
358   typedef Type NodeType;
359   typedef Type::subtype_iterator ChildIteratorType;
360
361   static inline NodeType *getEntryNode(Type *T) { return T; }
362   static inline ChildIteratorType child_begin(NodeType *N) { 
363     return N->subtype_begin(); 
364   }
365   static inline ChildIteratorType child_end(NodeType *N) { 
366     return N->subtype_end();
367   }
368 };
369
370 template <> struct GraphTraits<const Type*> {
371   typedef const Type NodeType;
372   typedef Type::subtype_iterator ChildIteratorType;
373
374   static inline NodeType *getEntryNode(const Type *T) { return T; }
375   static inline ChildIteratorType child_begin(NodeType *N) { 
376     return N->subtype_begin(); 
377   }
378   static inline ChildIteratorType child_end(NodeType *N) { 
379     return N->subtype_end();
380   }
381 };
382
383 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 
384   return Ty.getPrimitiveID() == Type::PointerTyID;
385 }
386
387 } // End llvm namespace
388
389 #endif