1 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This class defines the interface that one who 'use's a Value must implement.
11 // Each instance of the Value class keeps track of what User's have handles
14 // * Instructions are the largest class of User's.
15 // * Constants may be users of other constants (think arrays and stuff)
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Value.h"
27 class User : public Value {
28 User(const User &); // Do not implement
30 std::vector<Use> Operands;
32 User(const Type *Ty, unsigned vty, const std::string &name = "")
33 : Value(Ty, vty, name) {}
35 inline Value *getOperand(unsigned i) {
36 assert(i < Operands.size() && "getOperand() out of range!");
39 inline const Value *getOperand(unsigned i) const {
40 assert(i < Operands.size() && "getOperand() const out of range!");
43 inline void setOperand(unsigned i, Value *Val) {
44 assert(i < Operands.size() && "setOperand() out of range!");
47 inline unsigned getNumOperands() const { return (unsigned)Operands.size(); }
49 // ---------------------------------------------------------------------------
50 // Operand Iterator interface...
52 typedef std::vector<Use>::iterator op_iterator;
53 typedef std::vector<Use>::const_iterator const_op_iterator;
55 void op_reserve(unsigned NumElements) { Operands.reserve(NumElements); }
57 inline op_iterator op_begin() { return Operands.begin(); }
58 inline const_op_iterator op_begin() const { return Operands.begin(); }
59 inline op_iterator op_end() { return Operands.end(); }
60 inline const_op_iterator op_end() const { return Operands.end(); }
62 /// op_erase - This method is used to remove one of the arguments from the
63 /// operands list. Only use this if you know what you are doing.
65 op_iterator op_erase(op_iterator I) { return Operands.erase(I); }
66 op_iterator op_erase(op_iterator I, op_iterator E) {
67 return Operands.erase(I, E);
70 // dropAllReferences() - This function is in charge of "letting go" of all
71 // objects that this User refers to. This allows one to
72 // 'delete' a whole class at a time, even though there may be circular
73 // references... first all references are dropped, and all use counts go to
74 // zero. Then everything is delete'd for real. Note that no operations are
75 // valid on an object that has "dropped all references", except operator
78 inline void dropAllReferences() {
82 /// replaceUsesOfWith - Replaces all references to the "From" definition with
83 /// references to the "To" definition.
85 void replaceUsesOfWith(Value *From, Value *To);
87 // Methods for support type inquiry through isa, cast, and dyn_cast:
88 static inline bool classof(const User *) { return true; }
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) || isa<Constant>(V);
94 template<> struct simplify_type<User::op_iterator> {
95 typedef Value* SimpleType;
97 static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
98 return static_cast<SimpleType>(Val->get());
101 template<> struct simplify_type<const User::op_iterator>
102 : public simplify_type<User::op_iterator> {};
104 template<> struct simplify_type<User::const_op_iterator> {
105 typedef Value* SimpleType;
107 static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
108 return static_cast<SimpleType>(Val->get());
111 template<> struct simplify_type<const User::const_op_iterator>
112 : public simplify_type<User::const_op_iterator> {};
114 } // End llvm namespace