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