Rename contype to subtype
[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 class DerivedType;
32 class MethodType;
33 class ArrayType;
34 class PointerType;
35 class StructType;
36 class OpaqueType;
37
38 class Type : public Value {
39 public:
40   //===--------------------------------------------------------------------===//
41   // Definitions of all of the base types for the Type system.  Based on this
42   // value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
43   // Note: If you add an element to this, you need to add an element to the 
44   // Type::getPrimitiveType function, or else things will break!
45   //
46   enum PrimitiveID {
47     VoidTyID = 0  , BoolTyID,           //  0, 1: Basics...
48     UByteTyID     , SByteTyID,          //  2, 3: 8 bit types...
49     UShortTyID    , ShortTyID,          //  4, 5: 16 bit types...
50     UIntTyID      , IntTyID,            //  6, 7: 32 bit types...
51     ULongTyID     , LongTyID,           //  8, 9: 64 bit types...
52
53     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
54
55     TypeTyID,                           // 12   : Type definitions
56     LabelTyID     ,                     // 13   : Labels... 
57     /*LockTyID , */                     // 14   : mutex - TODO
58
59     // Derived types... see DerivedTypes.h file...
60     // Make sure FirstDerivedTyID stays up to date!!!
61     MethodTyID    , ModuleTyID,         // Methods... Modules...
62     ArrayTyID     , PointerTyID,        // Array... pointer...
63     StructTyID    , OpaqueTyID,         // Structure... Opaque type instances...
64     //PackedTyID  ,                     // SIMD 'packed' format... TODO
65     //...
66
67     NumPrimitiveIDs,                    // Must remain as last defined ID
68     FirstDerivedTyID = MethodTyID,
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   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 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 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 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
102   //===--------------------------------------------------------------------===//
103   // Property accessors for dealing with types...
104   //
105
106   // getPrimitiveID - Return the base type of the type.  This will return one
107   // of the PrimitiveID enum elements defined above.
108   //
109   inline PrimitiveID getPrimitiveID() const { return ID; }
110
111   // getUniqueID - Returns the UID of the type.  This can be thought of as a 
112   // small integer version of the pointer to the type class.  Two types that are
113   // structurally different have different UIDs.  This can be used for indexing
114   // types into an array.
115   //
116   inline unsigned getUniqueID() const { return UID; }
117
118   // getDescription - Return the string representation of the type...
119   inline const string &getDescription() const { return Desc; }
120
121   // isSigned - Return whether a numeric type is signed.
122   virtual bool isSigned() const { return 0; }
123   
124   // isUnsigned - Return whether a numeric type is unsigned.  This is not 
125   // quite the complement of isSigned... nonnumeric types return false as they
126   // do with isSigned.
127   // 
128   virtual bool isUnsigned() const { return 0; }
129
130   // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
131   // virtual function invocation.
132   //
133   virtual bool isIntegral() const { return 0; }
134
135   // isAbstract - True if the type is either an Opaque type, or is a derived
136   // type that includes an opaque type somewhere in it.  
137   //
138   inline bool isAbstract() const { return Abstract; }
139
140   // isRecursive - True if the type graph contains a cycle.
141   //
142   inline bool isRecursive() const { return Recursive; }
143
144   //===--------------------------------------------------------------------===//
145   // Type Iteration support
146   //
147   class TypeIterator;
148   typedef TypeIterator subtype_iterator;
149   inline subtype_iterator subtype_begin() const;   // DEFINED BELOW
150   inline subtype_iterator subtype_end() const;     // DEFINED BELOW
151
152   // getContainedType - This method is used to implement the type iterator
153   // (defined a the end of the file).  For derived types, this returns the types
154   // 'contained' in the derived type, returning 0 when 'i' becomes invalid. This
155   // allows the user to iterate over the types in a struct, for example, really
156   // easily.
157   //
158   virtual const Type *getContainedType(unsigned i) const { return 0; }
159
160   // getNumContainedTypes - Return the number of types in the derived type
161   virtual unsigned getNumContainedTypes() const { return 0; }
162
163   //===--------------------------------------------------------------------===//
164   // Static members exported by the Type class itself.  Useful for getting
165   // instances of Type.
166   //
167
168   // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
169   static const Type *getPrimitiveType(PrimitiveID IDNumber);
170   static const Type *getUniqueIDType(unsigned UID);
171
172   //===--------------------------------------------------------------------===//
173   // These are the builtin types that are always available...
174   //
175   static const Type *VoidTy , *BoolTy;
176   static const Type *SByteTy, *UByteTy,
177                     *ShortTy, *UShortTy,
178                     *IntTy  , *UIntTy, 
179                     *LongTy , *ULongTy;
180   static const Type *FloatTy, *DoubleTy;
181
182   static const Type *TypeTy , *LabelTy; //, *LockTy;
183
184   // Here are some useful little methods to query what type derived types are
185   // Note that all other types can just compare to see if this == Type::xxxTy;
186   //
187   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
188   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
189
190   inline bool isLabelType()     const { return this == LabelTy; }
191
192   inline const DerivedType *castDerivedType() const {
193     return isDerivedType() ? (const DerivedType*)this : 0;
194   }
195   inline const DerivedType *castDerivedTypeAsserting() const {
196     assert(isDerivedType());
197     return (const DerivedType*)this;
198   }
199
200   inline const MethodType *isMethodType() const {
201     return ID == MethodTyID ? (const MethodType*)this : 0;
202   }
203   inline bool isModuleType()    const { return ID == ModuleTyID;     }
204   inline const ArrayType *isArrayType() const { 
205     return ID == ArrayTyID ? (const ArrayType*)this : 0;
206   }
207   inline const PointerType *isPointerType() const { 
208     return ID == PointerTyID ? (const PointerType*)this : 0;
209   }
210   inline const StructType *isStructType() const {
211     return ID == StructTyID ? (const StructType*)this : 0;
212   }
213   inline const OpaqueType *isOpaqueType() const {
214     return ID == OpaqueTyID ? (const OpaqueType*)this : 0;
215   }
216
217 private:
218   class TypeIterator : public std::bidirectional_iterator<const Type,
219                                                           ptrdiff_t> {
220     const Type * const Ty;
221     unsigned Idx;
222
223     typedef TypeIterator _Self;
224   public:
225     inline TypeIterator(const Type *ty, unsigned idx) : Ty(ty), Idx(idx) {}
226     inline ~TypeIterator() {}
227     
228     inline bool operator==(const _Self& x) const { return Idx == x.Idx; }
229     inline bool operator!=(const _Self& x) const { return !operator==(x); }
230     
231     inline pointer operator*() const { return Ty->getContainedType(Idx); }
232     inline pointer operator->() const { return operator*(); }
233     
234     inline _Self& operator++() { ++Idx; return *this; } // Preincrement
235     inline _Self operator++(int) { // Postincrement
236       _Self tmp = *this; ++*this; return tmp; 
237     }
238     
239     inline _Self& operator--() { --Idx; return *this; }  // Predecrement
240     inline _Self operator--(int) { // Postdecrement
241       _Self tmp = *this; --*this; return tmp;
242     }
243   };
244 };
245
246 inline Type::TypeIterator Type::subtype_begin() const {
247   return TypeIterator(this, 0);
248 }
249
250 inline Type::TypeIterator Type::subtype_end() const {
251   return TypeIterator(this, getNumContainedTypes());
252 }
253
254 #endif