Value.h revision 143da691f066e24a9e5272e1cabea4c446ee8cc1
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 <string>
12#include <list>
13
14class User;
15class Type;
16template<class ValueSubclass, class ItemParentType> class ValueHolder;
17
18//===----------------------------------------------------------------------===//
19//                                 Value Class
20//===----------------------------------------------------------------------===//
21
22class Value {
23public:
24  enum ValueTy {
25    TypeVal,                // This is an instance of Type
26    ConstantVal,            // This is an instance of ConstPoolVal
27    MethodArgumentVal,      // This is an instance of MethodArgument
28    InstructionVal,         // This is an instance of Instruction
29
30    BasicBlockVal,          // This is an instance of BasicBlock
31    MethodVal,              // This is an instance of Method
32    ModuleVal,              // This is an instance of Module
33  };
34
35private:
36  list<User *> Uses;
37  string Name;
38  const Type *Ty;
39  ValueTy VTy;
40
41  Value(const Value &);              // Do not implement
42protected:
43  inline void setType(const Type *ty) { Ty = ty; }
44public:
45  Value(const Type *Ty, ValueTy vty, const string &name = "");
46  virtual ~Value();
47
48  inline const Type *getType() const { return Ty; }
49  inline ValueTy getValueType() const { return VTy; }
50
51  inline bool hasName() const { return Name != ""; }
52  inline const string &getName() const { return Name; }
53  virtual void setName(const string &name) { Name = name; }
54
55
56  // replaceAllUsesWith - Go through the uses list for this definition and make
57  // each use point to "D" instead of "this".  After this completes, 'this's
58  // use list should be empty.
59  //
60  void replaceAllUsesWith(Value *D);
61
62  //----------------------------------------------------------------------
63  // Methods for handling the list of uses of this DEF.
64  //
65  typedef list<User*>::iterator       use_iterator;
66  typedef list<User*>::const_iterator use_const_iterator;
67
68  inline unsigned           use_size()  const { return Uses.size();  }
69  inline bool               use_empty() const { return Uses.empty(); }
70  inline use_iterator       use_begin()       { return Uses.begin(); }
71  inline use_const_iterator use_begin() const { return Uses.begin(); }
72  inline use_iterator       use_end()         { return Uses.end();   }
73  inline use_const_iterator use_end()   const { return Uses.end();   }
74
75  inline void use_push_back(User *I)   { Uses.push_back(I); }
76  User *use_remove(use_iterator &I);
77
78  inline void addUse(User *I)      { Uses.push_back(I); }
79  void killUse(User *I);
80};
81
82// UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
83// list of a definition node up-to-date really easy.
84//
85template<class ValueSubclass>
86class UseTy {
87  ValueSubclass *Val;
88  User *U;
89public:
90  inline UseTy<ValueSubclass>(ValueSubclass *v, User *user) {
91    Val = v; U = user;
92    if (Val) Val->addUse(U);
93  }
94
95  inline ~UseTy<ValueSubclass>() { if (Val) Val->killUse(U); }
96
97  inline operator ValueSubclass *() const { return Val; }
98
99  inline UseTy<ValueSubclass>(const UseTy<ValueSubclass> &user) {
100    Val = 0;
101    U = user.U;
102    operator=(user.Val);
103  }
104  inline ValueSubclass *operator=(ValueSubclass *V) {
105    if (Val) Val->killUse(U);
106    Val = V;
107    if (V) V->addUse(U);
108    return V;
109  }
110
111  inline       ValueSubclass *operator->()       { return Val; }
112  inline const ValueSubclass *operator->() const { return Val; }
113
114  inline UseTy<ValueSubclass> &operator=(const UseTy<ValueSubclass> &user) {
115    if (Val) Val->killUse(U);
116    Val = user.Val;
117    Val->addUse(U);
118    return *this;
119  }
120};
121
122typedef UseTy<Value> Use;
123
124#endif
125