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