Add a missing #include
[oota-llvm.git] / include / llvm / Value.h
1 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the very important Value class.  This is subclassed by a
11 // bunch of other important classes, like Instruction, Function, Type, etc...
12 //
13 // This file also defines the Use<> template for users of value.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_VALUE_H
18 #define LLVM_VALUE_H
19
20 #include "llvm/AbstractTypeUser.h"
21 #include "llvm/Use.h"
22 #include "Support/Casting.h"
23 #include <string>
24
25 namespace llvm {
26
27 class Constant;
28 class Argument;
29 class Instruction;
30 struct BasicBlock;
31 class GlobalValue;
32 class Function;
33 class GlobalVariable;
34 class SymbolTable;
35
36 //===----------------------------------------------------------------------===//
37 //                                 Value Class
38 //===----------------------------------------------------------------------===//
39
40 /// Value - The base class of all values computed by a program that may be used
41 /// as operands to other values.
42 ///
43 struct Value {
44 private:
45   unsigned SubclassID;               // Subclass identifier (for isa/dyn_cast)
46   PATypeHolder Ty;
47   iplist<Use> Uses;
48   std::string Name;
49
50   void operator=(const Value &);     // Do not implement
51   Value(const Value &);              // Do not implement
52
53 public:
54   Value(const Type *Ty, unsigned scid, const std::string &name = "");
55   virtual ~Value();
56   
57   /// dump - Support for debugging, callable in GDB: V->dump()
58   //
59   virtual void dump() const;
60
61   /// print - Implement operator<< on Value...
62   ///
63   virtual void print(std::ostream &O) const = 0;
64   
65   /// All values are typed, get the type of this value.
66   ///
67   inline const Type *getType() const { return Ty; }
68   
69   // All values can potentially be named...
70   inline bool               hasName() const { return !Name.empty(); }
71   inline const std::string &getName() const { return Name; }
72
73   virtual void setName(const std::string &name, SymbolTable * = 0) {
74     Name = name;
75   }
76   
77   /// replaceAllUsesWith - Go through the uses list for this definition and make
78   /// each use point to "V" instead of "this".  After this completes, 'this's 
79   /// use list is guaranteed to be empty.
80   ///
81   void replaceAllUsesWith(Value *V);
82
83   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
84   // Only use when in type resolution situations!
85   void uncheckedReplaceAllUsesWith(Value *V);
86
87   //----------------------------------------------------------------------
88   // Methods for handling the vector of uses of this Value.
89   //
90   typedef UseListIteratorWrapper      use_iterator;
91   typedef UseListConstIteratorWrapper use_const_iterator;
92
93   unsigned           use_size()  const { return Uses.size();  }
94   bool               use_empty() const { return Uses.empty(); }
95   use_iterator       use_begin()       { return Uses.begin(); }
96   use_const_iterator use_begin() const { return Uses.begin(); }
97   use_iterator       use_end()         { return Uses.end();   }
98   use_const_iterator use_end()   const { return Uses.end();   }
99   User             *use_back()         { return Uses.back().getUser(); }
100   const User       *use_back()  const  { return Uses.back().getUser(); }
101
102   /// hasOneUse - Return true if there is exactly one user of this value.  This
103   /// is specialized because it is a common request and does not require
104   /// traversing the whole use list.
105   ///
106   bool hasOneUse() const {
107     iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
108     if (I == E) return false;
109     return ++I == E;
110   }
111
112   /// addUse/killUse - These two methods should only be used by the Use class.
113   ///
114   void addUse(Use &U)  { Uses.push_back(&U); }
115   void killUse(Use &U) { Uses.remove(&U); }
116
117   /// getValueType - Return an ID for the concrete type of this object.  This is
118   /// used to implement the classof checks.  This should not be used for any
119   /// other purpose, as the values may change as LLVM evolves.  Also, note that
120   /// starting with the InstructionVal value, the value stored is actually the
121   /// Instruction opcode, so there are more than just these values possible here
122   /// (and Instruction must be last).
123   ///
124   enum ValueTy {
125     ArgumentVal,            // This is an instance of Argument
126     BasicBlockVal,          // This is an instance of BasicBlock
127     FunctionVal,            // This is an instance of Function
128     GlobalVariableVal,      // This is an instance of GlobalVariable
129     ConstantVal,            // This is an instance of Constant
130     InstructionVal,         // This is an instance of Instruction
131   };
132   unsigned getValueType() const {
133     return SubclassID;
134   }
135
136 private:
137   /// FIXME: this is a gross hack, needed by another gross hack.  Eliminate!
138   void setValueType(unsigned VT) { SubclassID = VT; }
139   friend class Instruction;
140 };
141
142 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
143   if (V == 0)
144     OS << "<null> value!\n";
145   else
146     V->print(OS);
147   return OS;
148 }
149
150 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
151   V.print(OS);
152   return OS;
153 }
154
155
156 inline User *UseListIteratorWrapper::operator*() const {
157   return Super::operator*().getUser();
158 }
159
160 inline const User *UseListConstIteratorWrapper::operator*() const {
161   return Super::operator*().getUser();
162 }
163
164
165 Use::Use(Value *v, User *user) : Val(v), U(user) {
166   if (Val) Val->addUse(*this);
167 }
168
169 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
170   if (Val) Val->addUse(*this);
171 }
172
173 Use::~Use() {
174   if (Val) Val->killUse(*this);
175 }
176
177 void Use::set(Value *V) { 
178   if (Val) Val->killUse(*this);
179   Val = V;
180   if (V) V->addUse(*this);
181 }
182
183
184 // isa - Provide some specializations of isa so that we don't have to include
185 // the subtype header files to test to see if the value is a subclass...
186 //
187 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
188   return Val.getValueType() == Value::ConstantVal; 
189 }
190 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
191   return Val.getValueType() == Value::ArgumentVal;
192 }
193 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
194   return Val.getValueType() >= Value::InstructionVal;
195 }
196 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
197   return Val.getValueType() == Value::BasicBlockVal;
198 }
199 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
200   return Val.getValueType() == Value::FunctionVal;
201 }
202 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
203   return Val.getValueType() == Value::GlobalVariableVal;
204 }
205 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
206   return isa<GlobalVariable>(Val) || isa<Function>(Val);
207 }
208
209 } // End llvm namespace
210
211 #endif