*** empty log message ***
[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
32 class DerivedType;
33 class FunctionType;
34 class ArrayType;
35 class PointerType;
36 class StructType;
37 class OpaqueType;
38
39 class Type : public Value {
40 public:
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   std::string Desc;      // The printed name of the string...
75   bool        Abstract;  // True if type contains an OpaqueType
76   bool        Recursive; // True if the type is recursive
77
78 protected:
79   // ctor is protected, so only subclasses can create Type objects...
80   Type(const std::string &Name, PrimitiveID id);
81   virtual ~Type() {}
82
83   // When types are refined, they update their description to be more concrete.
84   //
85   inline void setDescription(const std::string &D) { Desc = D; }
86   
87   // setName - Associate the name with this type in the symbol table, but don't
88   // set the local name to be equal specified name.
89   //
90   virtual void setName(const std::string &Name, SymbolTable *ST = 0);
91
92   // Types can become nonabstract later, if they are refined.
93   //
94   inline void setAbstract(bool Val) { Abstract = Val; }
95
96   // Types can become recursive later, if they are refined.
97   //
98   inline void setRecursive(bool Val) { Recursive = Val; }
99
100 public:
101   virtual void print(std::ostream &O) const;
102
103   //===--------------------------------------------------------------------===//
104   // Property accessors for dealing with types...
105   //
106
107   // getPrimitiveID - Return the base type of the type.  This will return one
108   // of the PrimitiveID enum elements defined above.
109   //
110   inline PrimitiveID getPrimitiveID() const { return ID; }
111
112   // getUniqueID - Returns the UID of the type.  This can be thought of as a 
113   // small integer version of the pointer to the type class.  Two types that are
114   // structurally different have different UIDs.  This can be used for indexing
115   // types into an array.
116   //
117   inline unsigned getUniqueID() const { return UID; }
118
119   // getDescription - Return the string representation of the type...
120   inline const std::string &getDescription() const { return Desc; }
121
122   // isSigned - Return whether a numeric type is signed.
123   virtual bool isSigned() const { return 0; }
124   
125   // isUnsigned - Return whether a numeric type is unsigned.  This is not 
126   // quite the complement of isSigned... nonnumeric types return false as they
127   // do with isSigned.
128   // 
129   virtual bool isUnsigned() const { return 0; }
130
131   // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
132   // virtual function invocation.
133   //
134   virtual bool isIntegral() const { return 0; }
135
136   // isFloatingPoint - Return true if this is one of the two floating point
137   // types
138   bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID; }
139
140   // isAbstract - True if the type is either an Opaque type, or is a derived
141   // type that includes an opaque type somewhere in it.  
142   //
143   inline bool isAbstract() const { return Abstract; }
144
145   // isRecursive - True if the type graph contains a cycle.
146   //
147   inline bool isRecursive() const { return Recursive; }
148
149   // isLosslesslyConvertableTo - Return true if this type can be converted to
150   // 'Ty' without any reinterpretation of bits.  For example, uint to int.
151   //
152   bool isLosslesslyConvertableTo(const Type *Ty) const;
153
154
155   // Here are some useful little methods to query what type derived types are
156   // Note that all other types can just compare to see if this == Type::xxxTy;
157   //
158   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
159   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
160
161   // isFirstClassType - Return true if the value is holdable in a register.
162   inline bool isFirstClassType() const {
163     return isPrimitiveType() || ID == PointerTyID;
164   }
165
166   // isSized - Return true if it makes sense to take the size of this type.  To
167   // get the actual size for a particular target, it is reasonable to use the
168   // TargetData subsystem to do this.
169   //
170   bool isSized() const {
171     return ID != VoidTyID && ID != TypeTyID &&
172            ID != FunctionTyID && ID != LabelTyID && ID != OpaqueTyID;
173   }
174
175   // getPrimitiveSize - Return the basic size of this type if it is a primative
176   // type.  These are fixed by LLVM and are not target dependant.  This will
177   // return zero if the type does not have a size or is not a primitive type.
178   //
179   unsigned getPrimitiveSize() const;
180
181
182   //===--------------------------------------------------------------------===//
183   // Type Iteration support
184   //
185   class TypeIterator;
186   typedef TypeIterator subtype_iterator;
187   inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
188   inline subtype_iterator subtype_end() const;     // DEFINED BELOW
189
190   // getContainedType - This method is used to implement the type iterator
191   // (defined a the end of the file).  For derived types, this returns the types
192   // 'contained' in the derived type, returning 0 when 'i' becomes invalid. This
193   // allows the user to iterate over the types in a struct, for example, really
194   // easily.
195   //
196   virtual const Type *getContainedType(unsigned i) const { return 0; }
197
198   // getNumContainedTypes - Return the number of types in the derived type
199   virtual unsigned getNumContainedTypes() const { return 0; }
200
201   //===--------------------------------------------------------------------===//
202   // Static members exported by the Type class itself.  Useful for getting
203   // instances of Type.
204   //
205
206   // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
207   static const Type *getPrimitiveType(PrimitiveID IDNumber);
208   static const Type *getUniqueIDType(unsigned UID);
209
210   //===--------------------------------------------------------------------===//
211   // These are the builtin types that are always available...
212   //
213   static Type *VoidTy , *BoolTy;
214   static Type *SByteTy, *UByteTy,
215               *ShortTy, *UShortTy,
216               *IntTy  , *UIntTy, 
217               *LongTy , *ULongTy;
218   static Type *FloatTy, *DoubleTy;
219
220   static Type *TypeTy , *LabelTy;
221
222   // Methods for support type inquiry through isa, cast, and dyn_cast:
223   static inline bool classof(const Type *T) { return true; }
224   static inline bool classof(const Value *V) {
225     return V->getValueType() == Value::TypeVal;
226   }
227
228 #include "llvm/Type.def"
229
230 private:
231   class TypeIterator
232 #if __GNUC__ == 3
233     : public std::iterator<std::bidirectional_iterator_tag, const Type> {
234 #else
235     : public std::bidirectional_iterator<const Type, ptrdiff_t> {
236 #endif
237     const Type * const Ty;
238     unsigned Idx;
239
240     typedef TypeIterator _Self;
241   public:
242     inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
243     inline ~TypeIterator() {}
244     
245     inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
246     inline bool operator!=(const _Self& x) const { return !operator==(x); }
247     
248     inline pointer operator*() const { return Ty->getContainedType(Idx); }
249     inline pointer operator->() const { return operator*(); }
250     
251     inline _Self& operator++() { ++Idx; return *this; } // Preincrement
252     inline _Self operator++(int) { // Postincrement
253       _Self tmp = *this; ++*this; return tmp; 
254     }
255     
256     inline _Self& operator--() { --Idx; return *this; }  // Predecrement
257     inline _Self operator--(int) { // Postdecrement
258       _Self tmp = *this; --*this; return tmp;
259     }
260   };
261 };
262
263 inline Type::TypeIterator Type::subtype_begin() const {
264   return TypeIterator(this, 0);
265 }
266
267 inline Type::TypeIterator Type::subtype_end() const {
268   return TypeIterator(this, getNumContainedTypes());
269 }
270
271
272 // Provide specializations of GraphTraits to be able to treat a type as a 
273 // graph of sub types...
274
275 template <> struct GraphTraits<Type*> {
276   typedef Type NodeType;
277   typedef Type::subtype_iterator ChildIteratorType;
278
279   static inline NodeType *getEntryNode(Type *T) { return T; }
280   static inline ChildIteratorType child_begin(NodeType *N) { 
281     return N->subtype_begin(); 
282   }
283   static inline ChildIteratorType child_end(NodeType *N) { 
284     return N->subtype_end();
285   }
286 };
287
288 template <> struct GraphTraits<const Type*> {
289   typedef const Type NodeType;
290   typedef Type::subtype_iterator ChildIteratorType;
291
292   static inline NodeType *getEntryNode(const Type *T) { return T; }
293   static inline ChildIteratorType child_begin(NodeType *N) { 
294     return N->subtype_begin(); 
295   }
296   static inline ChildIteratorType child_end(NodeType *N) { 
297     return N->subtype_end();
298   }
299 };
300
301 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 
302   return Ty.getPrimitiveID() == Type::PointerTyID;
303 }
304
305 #endif