Use the Support/iterator file to abstract out compiler differences
[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...
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 are
115   // structurally different have different UIDs.  This can be used for indexing
116   // types into an array.
117   //
118   inline unsigned getUniqueID() const { return UID; }
119
120   // getDescription - Return the string representation of the type...
121   inline const std::string &getDescription() const { return Desc; }
122
123   // isSigned - Return whether a numeric type is signed.
124   virtual bool isSigned() const { return 0; }
125   
126   // isUnsigned - Return whether a numeric type is unsigned.  This is not 
127   // quite the complement of isSigned... nonnumeric types return false as they
128   // do with isSigned.
129   // 
130   virtual bool isUnsigned() const { return 0; }
131
132   // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
133   // virtual function invocation.
134   //
135   virtual bool isIntegral() const { return 0; }
136
137   // isFloatingPoint - Return true if this is one of the two floating point
138   // types
139   bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
140
141   // isAbstract - True if the type is either an Opaque type, or is a derived
142   // type that includes an opaque type somewhere in it.  
143   //
144   inline bool isAbstract() const { return Abstract; }
145
146   // isRecursive - True if the type graph contains a cycle.
147   //
148   inline bool isRecursive() const { return Recursive; }
149
150   // isLosslesslyConvertableTo - Return true if this type can be converted to
151   // 'Ty' without any reinterpretation of bits.  For example, uint to int.
152   //
153   bool isLosslesslyConvertableTo(const Type *Ty) const;
154
155
156   // Here are some useful little methods to query what type derived types are
157   // Note that all other types can just compare to see if this == Type::xxxTy;
158   //
159   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
160   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
161
162   // isFirstClassType - Return true if the value is holdable in a register.
163   inline bool isFirstClassType() const {
164     return isPrimitiveType() || ID == PointerTyID;
165   }
166
167   // isSized - Return true if it makes sense to take the size of this type.  To
168   // get the actual size for a particular target, it is reasonable to use the
169   // TargetData subsystem to do this.
170   //
171   bool isSized() const {
172     return ID != VoidTyID && ID != TypeTyID &&
173            ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
174   }
175
176   // getPrimitiveSize - Return the basic size of this type if it is a primative
177   // type.  These are fixed by LLVM and are not target dependant.  This will
178   // return zero if the type does not have a size or is not a primitive type.
179   //
180   unsigned getPrimitiveSize() const;
181
182
183   //===--------------------------------------------------------------------===//
184   // Type Iteration support
185   //
186   class TypeIterator;
187   typedef TypeIterator subtype_iterator;
188   inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
189   inline subtype_iterator subtype_end() const;     // DEFINED BELOW
190
191   // getContainedType - This method is used to implement the type iterator
192   // (defined a the end of the file).  For derived types, this returns the types
193   // 'contained' in the derived type, returning 0 when 'i' becomes invalid. This
194   // allows the user to iterate over the types in a struct, for example, really
195   // easily.
196   //
197   virtual const Type *getContainedType(unsigned i) const { return 0; }
198
199   // getNumContainedTypes - Return the number of types in the derived type
200   virtual unsigned getNumContainedTypes() const { return 0; }
201
202   //===--------------------------------------------------------------------===//
203   // Static members exported by the Type class itself.  Useful for getting
204   // instances of Type.
205   //
206
207   // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
208   static const Type *getPrimitiveType(PrimitiveID IDNumber);
209   static const Type *getUniqueIDType(unsigned UID);
210
211   //===--------------------------------------------------------------------===//
212   // These are the builtin types that are always available...
213   //
214   static Type *VoidTy , *BoolTy;
215   static Type *SByteTy, *UByteTy,
216               *ShortTy, *UShortTy,
217               *IntTy  , *UIntTy, 
218               *LongTy , *ULongTy;
219   static Type *FloatTy, *DoubleTy;
220
221   static Type *TypeTy , *LabelTy;
222
223   // Methods for support type inquiry through isa, cast, and dyn_cast:
224   static inline bool classof(const Type *T) { return true; }
225   static inline bool classof(const Value *V) {
226     return V->getValueType() == Value::TypeVal;
227   }
228
229 #include "llvm/Type.def"
230
231 private:
232   class TypeIterator : public bidirectional_iterator<const Type, ptrdiff_t> {
233     const Type * const Ty;
234     unsigned Idx;
235
236     typedef TypeIterator _Self;
237   public:
238     inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
239     inline ~TypeIterator() {}
240     
241     inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
242     inline bool operator!=(const _Self& x) const { return !operator==(x); }
243     
244     inline pointer operator*() const { return Ty->getContainedType(Idx); }
245     inline pointer operator->() const { return operator*(); }
246     
247     inline _Self& operator++() { ++Idx; return *this; } // Preincrement
248     inline _Self operator++(int) { // Postincrement
249       _Self tmp = *this; ++*this; return tmp; 
250     }
251     
252     inline _Self& operator--() { --Idx; return *this; }  // Predecrement
253     inline _Self operator--(int) { // Postdecrement
254       _Self tmp = *this; --*this; return tmp;
255     }
256   };
257 };
258
259 inline Type::TypeIterator Type::subtype_begin() const {
260   return TypeIterator(this, 0);
261 }
262
263 inline Type::TypeIterator Type::subtype_end() const {
264   return TypeIterator(this, getNumContainedTypes());
265 }
266
267
268 // Provide specializations of GraphTraits to be able to treat a type as a 
269 // graph of sub types...
270
271 template <> struct GraphTraits<Type*> {
272   typedef Type NodeType;
273   typedef Type::subtype_iterator ChildIteratorType;
274
275   static inline NodeType *getEntryNode(Type *T) { return T; }
276   static inline ChildIteratorType child_begin(NodeType *N) { 
277     return N->subtype_begin(); 
278   }
279   static inline ChildIteratorType child_end(NodeType *N) { 
280     return N->subtype_end();
281   }
282 };
283
284 template <> struct GraphTraits<const Type*> {
285   typedef const Type NodeType;
286   typedef Type::subtype_iterator ChildIteratorType;
287
288   static inline NodeType *getEntryNode(const Type *T) { return T; }
289   static inline ChildIteratorType child_begin(NodeType *N) { 
290     return N->subtype_begin(); 
291   }
292   static inline ChildIteratorType child_end(NodeType *N) { 
293     return N->subtype_end();
294   }
295 };
296
297 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 
298   return Ty.getPrimitiveID() == Type::PointerTyID;
299 }
300
301 #endif