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