Tweak argument
[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 <iostream>
24
25 namespace llvm {
26
27 class Type;
28 class Constant;
29 class Argument;
30 class Instruction;
31 class BasicBlock;
32 class GlobalValue;
33 class Function;
34 class GlobalVariable;
35 class SymbolTable;
36
37 //===----------------------------------------------------------------------===//
38 //                                 Value Class
39 //===----------------------------------------------------------------------===//
40
41 /// Value - The base class of all values computed by a program that may be used
42 /// as operands to other values.
43 ///
44 struct Value {
45   enum ValueTy {
46     TypeVal,                // This is an instance of Type
47     ConstantVal,            // This is an instance of Constant
48     ArgumentVal,            // This is an instance of Argument
49     InstructionVal,         // This is an instance of Instruction
50     BasicBlockVal,          // This is an instance of BasicBlock
51     FunctionVal,            // This is an instance of Function
52     GlobalVariableVal,      // This is an instance of GlobalVariable
53   };
54
55 private:
56   iplist<Use> Uses;
57   std::string Name;
58   PATypeHolder Ty;
59   ValueTy VTy;
60
61   void operator=(const Value &);     // Do not implement
62   Value(const Value &);              // Do not implement
63 public:
64   Value(const Type *Ty, ValueTy vty, const std::string &name = "");
65   virtual ~Value();
66   
67   /// dump - Support for debugging, callable in GDB: V->dump()
68   //
69   virtual void dump() const;
70
71   /// print - Implement operator<< on Value...
72   ///
73   virtual void print(std::ostream &O) const = 0;
74   
75   /// All values are typed, get the type of this value.
76   ///
77   inline const Type *getType() const { return Ty; }
78   
79   // All values can potentially be named...
80   inline bool               hasName() const { return !Name.empty(); }
81   inline const std::string &getName() const { return Name; }
82
83   virtual void setName(const std::string &name, SymbolTable * = 0) {
84     Name = name;
85   }
86   
87   /// getValueType - Return the immediate subclass of this Value.
88   ///
89   inline ValueTy getValueType() const { return VTy; }
90   
91   /// replaceAllUsesWith - Go through the uses list for this definition and make
92   /// each use point to "V" instead of "this".  After this completes, 'this's 
93   /// use list is guaranteed to be empty.
94   ///
95   void replaceAllUsesWith(Value *V);
96
97   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
98   // Only use when in type resolution situations!
99   void uncheckedReplaceAllUsesWith(Value *V);
100
101   //----------------------------------------------------------------------
102   // Methods for handling the vector of uses of this Value.
103   //
104   typedef UseListIteratorWrapper      use_iterator;
105   typedef UseListConstIteratorWrapper use_const_iterator;
106
107   unsigned           use_size()  const { return Uses.size();  }
108   bool               use_empty() const { return Uses.empty(); }
109   use_iterator       use_begin()       { return Uses.begin(); }
110   use_const_iterator use_begin() const { return Uses.begin(); }
111   use_iterator       use_end()         { return Uses.end();   }
112   use_const_iterator use_end()   const { return Uses.end();   }
113   User             *use_back()         { return Uses.back().getUser(); }
114   const User       *use_back()  const  { return Uses.back().getUser(); }
115
116   /// hasOneUse - Return true if there is exactly one user of this value.  This
117   /// is specialized because it is a common request and does not require
118   /// traversing the whole use list.
119   ///
120   bool hasOneUse() const {
121     iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
122     if (I == E) return false;
123     return ++I == E;
124   }
125
126   /// addUse/killUse - These two methods should only be used by the Use class.
127   ///
128   void addUse(Use &U)  { Uses.push_back(&U); }
129   void killUse(Use &U) { Uses.remove(&U); }
130 };
131
132 inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
133   if (V == 0)
134     OS << "<null> value!\n";
135   else
136     V->print(OS);
137   return OS;
138 }
139
140 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
141   V.print(OS);
142   return OS;
143 }
144
145
146 inline User *UseListIteratorWrapper::operator*() const {
147   return Super::operator*().getUser();
148 }
149
150 inline const User *UseListConstIteratorWrapper::operator*() const {
151   return Super::operator*().getUser();
152 }
153
154
155 Use::Use(Value *v, User *user) : Val(v), U(user) {
156   if (Val) Val->addUse(*this);
157 }
158
159 Use::Use(const Use &u) : Val(u.Val), U(u.U) {
160   if (Val) Val->addUse(*this);
161 }
162
163 Use::~Use() {
164   if (Val) Val->killUse(*this);
165 }
166
167 void Use::set(Value *V) { 
168   if (Val) Val->killUse(*this);
169   Val = V;
170   if (V) V->addUse(*this);
171 }
172
173
174 // isa - Provide some specializations of isa so that we don't have to include
175 // the subtype header files to test to see if the value is a subclass...
176 //
177 template <> inline bool isa_impl<Type, Value>(const Value &Val) { 
178   return Val.getValueType() == Value::TypeVal;
179 }
180 template <> inline bool isa_impl<Constant, Value>(const Value &Val) { 
181   return Val.getValueType() == Value::ConstantVal; 
182 }
183 template <> inline bool isa_impl<Argument, Value>(const Value &Val) { 
184   return Val.getValueType() == Value::ArgumentVal;
185 }
186 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) { 
187   return Val.getValueType() == Value::InstructionVal;
188 }
189 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) { 
190   return Val.getValueType() == Value::BasicBlockVal;
191 }
192 template <> inline bool isa_impl<Function, Value>(const Value &Val) { 
193   return Val.getValueType() == Value::FunctionVal;
194 }
195 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) { 
196   return Val.getValueType() == Value::GlobalVariableVal;
197 }
198 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) { 
199   return isa<GlobalVariable>(Val) || isa<Function>(Val);
200 }
201
202 } // End llvm namespace
203
204 #endif