* Add support for Opaque & Abstract types.
[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 and Opt/ConstantHandling.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
31 namespace opt {
32   class ConstRules;
33 }
34 class DerivedType;
35 class MethodType;
36 class ArrayType;
37 class PointerType;
38 class StructType;
39 class OpaqueType;
40
41 class Type : public Value {
42 public:
43   //===--------------------------------------------------------------------===//
44   // Definitions of all of the base types for the Type system.  Based on this
45   // value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
46   // Note: If you add an element to this, you need to add an element to the 
47   // Type::getPrimitiveType function, or else things will break!
48   //
49   enum PrimitiveID {
50     VoidTyID = 0  , BoolTyID,           //  0, 1: Basics...
51     UByteTyID     , SByteTyID,          //  2, 3: 8 bit types...
52     UShortTyID    , ShortTyID,          //  4, 5: 16 bit types...
53     UIntTyID      , IntTyID,            //  6, 7: 32 bit types...
54     ULongTyID     , LongTyID,           //  8, 9: 64 bit types...
55
56     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
57
58     TypeTyID,                           // 12   : Type definitions
59     LabelTyID     ,                     // 13   : Labels... 
60     /*LockTyID , */                     // 14   : mutex - TODO
61
62     // Derived types... see DerivedTypes.h file...
63     // Make sure FirstDerivedTyID stays up to date!!!
64     MethodTyID    , ModuleTyID,         // Methods... Modules...
65     ArrayTyID     , PointerTyID,        // Array... pointer...
66     StructTyID    , OpaqueTyID,         // Structure... Opaque type instances...
67     //PackedTyID  ,                     // SIMD 'packed' format... TODO
68     //...
69
70     NumPrimitiveIDs,                    // Must remain as last defined ID
71     FirstDerivedTyID = MethodTyID,
72   };
73
74 private:
75   PrimitiveID ID;        // The current base type of this type...
76   unsigned    UID;       // The unique ID number for this class
77   string      Desc;      // The printed name of the string...
78   bool        Abstract;  // True if type contains an OpaqueType
79   bool        Recursive; // True if the type is recursive
80
81   // ConstRulesImpl - See Opt/ConstantHandling.h for more info
82   mutable const opt::ConstRules *ConstRulesImpl;
83
84 protected:
85   // ctor is protected, so only subclasses can create Type objects...
86   Type(const string &Name, PrimitiveID id);
87   virtual ~Type() {}
88
89   // When types are refined, they update their description to be more concrete.
90   //
91   inline void setDescription(const string &D) { Desc = D; }
92   
93   // setName - Associate the name with this type in the symbol table, but don't
94   // set the local name to be equal specified name.
95   //
96   virtual void setName(const string &Name, SymbolTable *ST = 0);
97
98   // Types can become nonabstract later, if they are refined.
99   //
100   inline void setAbstract(bool Val) { Abstract = Val; }
101
102   // Types can become recursive later, if they are refined.
103   //
104   inline void setRecursive(bool Val) { Recursive = Val; }
105
106 public:
107
108   //===--------------------------------------------------------------------===//
109   // Property accessors for dealing with types...
110   //
111
112   // getPrimitiveID - Return the base type of the type.  This will return one
113   // of the PrimitiveID enum elements defined above.
114   //
115   inline PrimitiveID getPrimitiveID() const { return ID; }
116
117   // getUniqueID - Returns the UID of the type.  This can be thought of as a 
118   // small integer version of the pointer to the type class.  Two types that are
119   // structurally different have different UIDs.  This can be used for indexing
120   // types into an array.
121   //
122   inline unsigned getUniqueID() const { return UID; }
123
124   // getDescription - Return the string representation of the type...
125   inline const string &getDescription() const { return Desc; }
126
127   // isSigned - Return whether a numeric type is signed.
128   virtual bool isSigned() const { return 0; }
129   
130   // isUnsigned - Return whether a numeric type is unsigned.  This is not 
131   // quite the complement of isSigned... nonnumeric types return false as they
132   // do with isSigned.
133   // 
134   virtual bool isUnsigned() const { return 0; }
135
136   // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
137   // virtual function invocation.
138   //
139   virtual bool isIntegral() const { return 0; }
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   //===--------------------------------------------------------------------===//
151   // Type Iteration support
152   //
153   class TypeIterator;
154   typedef TypeIterator contype_iterator;
155   inline contype_iterator contype_begin() const;   // DEFINED BELOW
156   inline contype_iterator contype_end() const;     // DEFINED BELOW
157
158   // getContainedType - This method is used to implement the type iterator
159   // (defined a the end of the file).  For derived types, this returns the types
160   // 'contained' in the derived type, returning 0 when 'i' becomes invalid. This
161   // allows the user to iterate over the types in a struct, for example, really
162   // easily.
163   //
164   virtual const Type *getContainedType(unsigned i) const { return 0; }
165
166   // getNumContainedTypes - Return the number of types in the derived type
167   virtual unsigned getNumContainedTypes() const { return 0; }
168
169   //===--------------------------------------------------------------------===//
170   // Static members exported by the Type class itself.  Useful for getting
171   // instances of Type.
172   //
173
174   // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
175   static const Type *getPrimitiveType(PrimitiveID IDNumber);
176   static const Type *getUniqueIDType(unsigned UID);
177
178   // Methods for dealing with constants uniformly.  See Opt/ConstantHandling.h
179   // for more info on this...
180   //
181   inline const opt::ConstRules *getConstRules() const { return ConstRulesImpl; }
182   inline void setConstRules(const opt::ConstRules *R) const { ConstRulesImpl=R;}
183
184   //===--------------------------------------------------------------------===//
185   // These are the builtin types that are always available...
186   //
187   static const Type *VoidTy , *BoolTy;
188   static const Type *SByteTy, *UByteTy,
189                     *ShortTy, *UShortTy,
190                     *IntTy  , *UIntTy, 
191                     *LongTy , *ULongTy;
192   static const Type *FloatTy, *DoubleTy;
193
194   static const Type *TypeTy , *LabelTy; //, *LockTy;
195
196   // Here are some useful little methods to query what type derived types are
197   // Note that all other types can just compare to see if this == Type::xxxTy;
198   //
199   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
200   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
201
202   inline bool isLabelType()     const { return this == LabelTy; }
203
204   inline const DerivedType *castDerivedType() const {
205     return isDerivedType() ? (const DerivedType*)this : 0;
206   }
207   inline const DerivedType *castDerivedTypeAsserting() const {
208     assert(isDerivedType());
209     return (const DerivedType*)this;
210   }
211
212   inline const MethodType *isMethodType() const {
213     return ID == MethodTyID ? (const MethodType*)this : 0;
214   }
215   inline bool isModuleType()    const { return ID == ModuleTyID;     }
216   inline const ArrayType *isArrayType() const { 
217     return ID == ArrayTyID ? (const ArrayType*)this : 0;
218   }
219   inline const PointerType *isPointerType() const { 
220     return ID == PointerTyID ? (const PointerType*)this : 0;
221   }
222   inline const StructType *isStructType() const {
223     return ID == StructTyID ? (const StructType*)this : 0;
224   }
225   inline const OpaqueType *isOpaqueType() const {
226     return ID == OpaqueTyID ? (const OpaqueType*)this : 0;
227   }
228
229 private:
230   class TypeIterator : public std::bidirectional_iterator<const Type,
231                                                           ptrdiff_t> {
232     const Type * const Ty;
233     unsigned Idx;
234
235     typedef TypeIterator _Self;
236   public:
237     inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
238     inline ~TypeIterator() {}
239     
240     inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
241     inline bool operator!=(const _Self& x) const { return !operator==(x); }
242     
243     inline pointer operator*() const { return Ty->getContainedType(Idx); }
244     inline pointer operator->() const { return operator*(); }
245     
246     inline _Self& operator++() { ++Idx; return *this; } // Preincrement
247     inline _Self operator++(int) { // Postincrement
248       _Self tmp = *this; ++*this; return tmp; 
249     }
250     
251     inline _Self& operator--() { --Idx; return *this; }  // Predecrement
252     inline _Self operator--(int) { // Postdecrement
253       _Self tmp = *this; --*this; return tmp;
254     }
255   };
256 };
257
258 inline Type::TypeIterator Type::contype_begin() const {
259   return TypeIterator(this, 0);
260 }
261
262 inline Type::TypeIterator Type::contype_end() const {
263   return TypeIterator(this, getNumContainedTypes());
264 }
265
266 #endif