LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

User.h

Go to the documentation of this file.
00001 //===-- llvm/User.h - User class definition ---------------------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class defines the interface that one who 'use's a Value must implement.
00011 // Each instance of the Value class keeps track of what User's have handles
00012 // to it.
00013 //
00014 //  * Instructions are the largest class of User's.
00015 //  * Constants may be users of other constants (think arrays and stuff)
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_USER_H
00020 #define LLVM_USER_H
00021 
00022 #include "llvm/Value.h"
00023 #include <vector>
00024 
00025 namespace llvm {
00026 
00027 class User : public Value {
00028   User(const User &);             // Do not implement
00029 protected:
00030   std::vector<Use> Operands;
00031 public:
00032   User(const Type *Ty, unsigned vty, const std::string &name = "")
00033     : Value(Ty, vty, name) {}
00034 
00035   inline Value *getOperand(unsigned i) { 
00036     assert(i < Operands.size() && "getOperand() out of range!");
00037     return Operands[i];
00038   }
00039   inline const Value *getOperand(unsigned i) const {
00040     assert(i < Operands.size() && "getOperand() const out of range!");
00041     return Operands[i];
00042   }
00043   inline void setOperand(unsigned i, Value *Val) {
00044     assert(i < Operands.size() && "setOperand() out of range!");
00045     Operands[i] = Val;
00046   }
00047   inline unsigned getNumOperands() const { return (unsigned)Operands.size(); }
00048 
00049   // ---------------------------------------------------------------------------
00050   // Operand Iterator interface...
00051   //
00052   typedef std::vector<Use>::iterator       op_iterator;
00053   typedef std::vector<Use>::const_iterator const_op_iterator;
00054 
00055   void op_reserve(unsigned NumElements) { Operands.reserve(NumElements); }
00056 
00057   inline op_iterator       op_begin()       { return Operands.begin(); }
00058   inline const_op_iterator op_begin() const { return Operands.begin(); }
00059   inline op_iterator       op_end()         { return Operands.end(); }
00060   inline const_op_iterator op_end()   const { return Operands.end(); }
00061 
00062   /// op_erase - This method is used to remove one of the arguments from the
00063   /// operands list.  Only use this if you know what you are doing.
00064   ///
00065   op_iterator op_erase(op_iterator I) { return Operands.erase(I); }
00066   op_iterator op_erase(op_iterator I, op_iterator E) {
00067     return Operands.erase(I, E);
00068   }
00069 
00070   // dropAllReferences() - This function is in charge of "letting go" of all
00071   // objects that this User refers to.  This allows one to
00072   // 'delete' a whole class at a time, even though there may be circular
00073   // references... first all references are dropped, and all use counts go to
00074   // zero.  Then everything is delete'd for real.  Note that no operations are
00075   // valid on an object that has "dropped all references", except operator 
00076   // delete.
00077   //
00078   inline void dropAllReferences() {
00079     Operands.clear();
00080   }
00081 
00082   /// replaceUsesOfWith - Replaces all references to the "From" definition with
00083   /// references to the "To" definition.
00084   ///
00085   void replaceUsesOfWith(Value *From, Value *To);
00086 
00087   // Methods for support type inquiry through isa, cast, and dyn_cast:
00088   static inline bool classof(const User *) { return true; }
00089   static inline bool classof(const Value *V) {
00090     return isa<Instruction>(V) || isa<Constant>(V);
00091   }
00092 };
00093 
00094 template<> struct simplify_type<User::op_iterator> {
00095   typedef Value* SimpleType;
00096   
00097   static SimpleType getSimplifiedValue(const User::op_iterator &Val) {
00098     return static_cast<SimpleType>(Val->get());
00099   }
00100 };
00101 template<> struct simplify_type<const User::op_iterator>
00102   : public simplify_type<User::op_iterator> {};
00103 
00104 template<> struct simplify_type<User::const_op_iterator> {
00105   typedef Value* SimpleType;
00106   
00107   static SimpleType getSimplifiedValue(const User::const_op_iterator &Val) {
00108     return static_cast<SimpleType>(Val->get());
00109   }
00110 };
00111 template<> struct simplify_type<const User::const_op_iterator>
00112   : public simplify_type<User::const_op_iterator> {};
00113 
00114 } // End llvm namespace
00115 
00116 #endif