Change is*Type to be a casting convertion operator
[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 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TYPE_H
16 #define LLVM_TYPE_H
17
18 #include "llvm/Value.h"
19
20 namespace opt {
21   class ConstRules;
22 }
23 class ConstPoolVal;
24 class MethodType;
25 class ArrayType;
26 class StructType;
27 class PointerType;
28
29 class Type : public Value {
30 public:
31   //===--------------------------------------------------------------------===//
32   // Definitions of all of the base types for the Type system.  Based on this
33   // value, you can cast to a "DerivedType" subclass (see DerivedTypes.h)
34   // Note: If you add an element to this, you need to add an element to the 
35   // Type::getPrimitiveType function, or else things will break!
36   //
37   enum PrimitiveID {
38     VoidTyID = 0  , BoolTyID,           //  0, 1: Basics...
39     UByteTyID     , SByteTyID,          //  2, 3: 8 bit types...
40     UShortTyID    , ShortTyID,          //  4, 5: 16 bit types...
41     UIntTyID      , IntTyID,            //  6, 7: 32 bit types...
42     ULongTyID     , LongTyID,           //  8, 9: 64 bit types...
43
44     FloatTyID     , DoubleTyID,         // 10,11: Floating point types...
45
46     TypeTyID,                           // 12   : Type definitions
47     LabelTyID     , LockTyID,           // 13,14: Labels... mutexes...
48
49     // TODO: Kill FillerTyID.  It just makes FirstDerivedTyID = 0x10
50     FillerTyID ,                        // 15   : filler
51
52     // Derived types... see DerivedTypes.h file...
53     // Make sure FirstDerivedTyID stays up to date!!!
54     MethodTyID    , ModuleTyID,         // Methods... Modules...
55     ArrayTyID     , PointerTyID,        // Array... pointer...
56     StructTyID    , PackedTyID,         // Structure... SIMD 'packed' format...
57     //...
58
59     NumPrimitiveIDs,                    // Must remain as last defined ID
60     FirstDerivedTyID = MethodTyID,
61   };
62
63 private:
64   PrimitiveID ID;    // The current base type of this type...
65   unsigned    UID;   // The unique ID number for this class
66
67   // ConstRulesImpl - See Opt/ConstantHandling.h for more info
68   mutable const opt::ConstRules *ConstRulesImpl;
69
70 protected:
71   // ctor is protected, so only subclasses can create Type objects...
72   Type(const string &Name, PrimitiveID id);
73 public:
74   virtual ~Type() {}
75
76   // isSigned - Return whether a numeric type is signed.
77   virtual bool isSigned() const { return 0; }
78   
79   // isUnsigned - Return whether a numeric type is unsigned.  This is not 
80   // quite the complement of isSigned... nonnumeric types return false as they
81   // do with isSigned.
82   // 
83   virtual bool isUnsigned() const { return 0; }
84
85   // isIntegral - Equilivent to isSigned() || isUnsigned, but with only a single
86   // virtual function invocation.
87   //
88   virtual bool isIntegral() const { return 0; }
89   
90   inline unsigned getUniqueID() const { return UID; }
91   inline PrimitiveID getPrimitiveID() const { return ID; }
92
93   // getPrimitiveType/getUniqueIDType - Return a type based on an identifier.
94   static const Type *getPrimitiveType(PrimitiveID IDNumber);
95   static const Type *getUniqueIDType(unsigned UID);
96
97   // Methods for dealing with constants uniformly.  See Opt/ConstantHandling.h
98   // for more info on this...
99   //
100   inline const opt::ConstRules *getConstRules() const { return ConstRulesImpl; }
101   inline void setConstRules(const opt::ConstRules *R) const { ConstRulesImpl = R; }
102
103 public:   // These are the builtin types that are always available...
104   static const Type *VoidTy , *BoolTy;
105   static const Type *SByteTy, *UByteTy,
106                     *ShortTy, *UShortTy,
107                     *IntTy  , *UIntTy, 
108                     *LongTy , *ULongTy;
109   static const Type *FloatTy, *DoubleTy;
110
111   static const Type *TypeTy , *LabelTy, *LockTy;
112
113   // Here are some useful little methods to query what type derived types are
114   // Note that all other types can just compare to see if this == Type::xxxTy;
115   //
116   inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
117   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
118
119   inline bool isLabelType()     const { return this == LabelTy; }
120   inline const MethodType *isMethodType() const { 
121     return ID == MethodTyID ? (const MethodType*)this : 0;
122   }
123   inline bool isModuleType()    const { return ID == ModuleTyID;     }
124   inline const ArrayType *isArrayType() const { 
125     return ID == ArrayTyID ? (const ArrayType*)this : 0;
126   }
127   inline const PointerType *isPointerType() const { 
128     return ID == PointerTyID ? (const PointerType*)this : 0;
129   }
130   inline const StructType *isStructType() const {
131     return ID == StructTyID ? (const StructType*)this : 0;
132   }
133 };
134
135 #endif