Add annotation support to value
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class -------------*- C++ -*--=//
2 //
3 // This file defines the very important Value class.  This is subclassed by a
4 // bunch of other important classes, like Def, Method, Module, Type, etc...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_VALUE_H
9 #define LLVM_VALUE_H
10
11 #include <list>
12 #include "llvm/Annotation.h"
13
14 class User;
15 class Type;
16 class ConstPoolVal;
17 class MethodArgument;
18 class Instruction;
19 class BasicBlock;
20 class Method;
21 class Module;
22 template<class ValueSubclass, class ItemParentType, class SymTabType> 
23   class ValueHolder;
24
25 //===----------------------------------------------------------------------===//
26 //                                 Value Class
27 //===----------------------------------------------------------------------===//
28
29 class Value : public Annotable {   // Value's are annotable
30 public:
31   enum ValueTy {
32     TypeVal,                // This is an instance of Type
33     ConstantVal,            // This is an instance of ConstPoolVal
34     MethodArgumentVal,      // This is an instance of MethodArgument
35     InstructionVal,         // This is an instance of Instruction
36
37     BasicBlockVal,          // This is an instance of BasicBlock
38     MethodVal,              // This is an instance of Method
39     ModuleVal,              // This is an instance of Module
40   };
41
42 private:
43   list<User *> Uses;
44   string Name;
45   const Type *Ty;
46   ValueTy VTy;
47
48   Value(const Value &);              // Do not implement
49 protected:
50   inline void setType(const Type *ty) { Ty = ty; }
51 public:
52   Value(const Type *Ty, ValueTy vty, const string &name = "");
53   virtual ~Value();
54
55   inline const Type *getType() const { return Ty; }
56
57   // All values can potentially be named...
58   inline bool hasName() const { return Name != ""; }
59   inline const string &getName() const { return Name; }
60   virtual void setName(const string &name) { Name = name; }
61
62   // Methods for determining the subtype of this Value.  The getValueType()
63   // method returns the type of the value directly.  The cast*() methods are
64   // equilivent to using dynamic_cast<>... if the cast is successful, this is
65   // returned, otherwise you get a null pointer, allowing expressions like this:
66   //
67   // if (Instruction *I = Val->castInstruction()) { ... }
68   //
69   // This section also defines a family of isType, isConstant, isMethodArgument,
70   // etc functions...
71   //
72   // The family of functions Val->cast<type>Asserting() is used in the same
73   // way as the Val->cast<type>() instructions, but they assert the expected
74   // type instead of checking it at runtime.
75   //
76   inline ValueTy getValueType() const { return VTy; }
77
78   // Use a macro to define the functions, otherwise these definitions are just
79   // really long and ugly.
80 #define CAST_FN(NAME, CLASS)                                              \
81   inline bool is##NAME() const { return VTy == NAME##Val; }               \
82   inline const CLASS *cast##NAME() const { /*const version */             \
83     return is##NAME() ? (const CLASS*)this : 0;                           \
84   }                                                                       \
85   inline CLASS *cast##NAME() {         /* nonconst version */             \
86     return is##NAME() ? (CLASS*)this : 0;                                 \
87   }                                                                       \
88   inline const CLASS *cast##NAME##Asserting() const { /*const version */  \
89     assert(is##NAME() && "Expected Value Type: " #NAME);                  \
90     return (const CLASS*)this;                                            \
91   }                                                                       \
92   inline CLASS *cast##NAME##Asserting() {         /* nonconst version */  \
93     assert(is##NAME() && "Expected Value Type: " #NAME);                  \
94     return (CLASS*)this;                                                  \
95   }                                                                       \
96
97   CAST_FN(Constant      ,       ConstPoolVal  )
98   CAST_FN(MethodArgument,       MethodArgument)
99   CAST_FN(Instruction   ,       Instruction   )
100   CAST_FN(BasicBlock    ,       BasicBlock    )
101   CAST_FN(Method        ,       Method        )
102   CAST_FN(Module        ,       Module        )
103 #undef CAST_FN
104
105   // Type value is special, because there is no nonconst version of functions!
106   inline bool isType() const { return VTy == TypeVal; }
107   inline const Type *castType() const {
108     return (VTy == TypeVal) ? (const Type*)this : 0;
109   }
110   inline const Type *castTypeAsserting() const {
111     assert(isType() && "Expected Value Type: Type");
112     return (const Type*)this;
113   }
114
115   // replaceAllUsesWith - Go through the uses list for this definition and make
116   // each use point to "D" instead of "this".  After this completes, 'this's 
117   // use list should be empty.
118   //
119   void replaceAllUsesWith(Value *D);
120
121   //----------------------------------------------------------------------
122   // Methods for handling the list of uses of this DEF.
123   //
124   typedef list<User*>::iterator       use_iterator;
125   typedef list<User*>::const_iterator use_const_iterator;
126
127   inline unsigned           use_size()  const { return Uses.size();  }
128   inline bool               use_empty() const { return Uses.empty(); }
129   inline use_iterator       use_begin()       { return Uses.begin(); }
130   inline use_const_iterator use_begin() const { return Uses.begin(); }
131   inline use_iterator       use_end()         { return Uses.end();   }
132   inline use_const_iterator use_end()   const { return Uses.end();   }
133
134   inline void use_push_back(User *I)   { Uses.push_back(I); }
135   User *use_remove(use_iterator &I);
136
137   inline void addUse(User *I)      { Uses.push_back(I); }
138   void killUse(User *I);
139 };
140
141 // UseTy and it's friendly typedefs (Use) are here to make keeping the "use" 
142 // list of a definition node up-to-date really easy.
143 //
144 template<class ValueSubclass>
145 class UseTy {
146   ValueSubclass *Val;
147   User *U;
148 public:
149   inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
150     Val = v; U = user;
151     if (Val) Val->addUse(U);
152   }
153
154   inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
155
156   inline operator ValueSubclass *() const { return Val; }
157
158   inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
159     Val = 0;
160     U = user.U;
161     operator=(user.Val);
162   }
163   inline ValueSubclass *operator=(ValueSubclass *V) { 
164     if (Val) Val->killUse(U);
165     Val = V;
166     if (V) V->addUse(U);
167     return V;
168   }
169
170   inline       ValueSubclass *operator->()       { return Val; }
171   inline const ValueSubclass *operator->() const { return Val; }
172
173   inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
174     if (Val) Val->killUse(U);
175     Val = user.Val;
176     Val->addUse(U);
177     return *this;
178   }
179 };
180
181 typedef UseTy<Value> Use;
182
183 #endif