LLVM API Documentation

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

CallSite.h

Go to the documentation of this file.
00001 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- 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 file defines the CallSite class, which is a handy wrapper for code that
00011 // wants to treat Call and Invoke instructions in a generic way.
00012 //
00013 // NOTE: This class is supposed to have "value semantics". So it should be
00014 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
00015 // is efficiently copyable, assignable and constructable, with cost equivalent
00016 // to copying a pointer (notice that it has only a single data member).
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_SUPPORT_CALLSITE_H
00021 #define LLVM_SUPPORT_CALLSITE_H
00022 
00023 #include "llvm/Instruction.h"
00024 #include "llvm/BasicBlock.h"
00025 
00026 namespace llvm {
00027 
00028 class CallInst;
00029 class InvokeInst;
00030 
00031 class CallSite {
00032   Instruction *I;
00033 public:
00034   CallSite() : I(0) {}
00035   CallSite(CallInst *CI) : I((Instruction*)CI) {}
00036   CallSite(InvokeInst *II) : I((Instruction*)II) {}
00037   CallSite(const CallSite &CS) : I(CS.I) {}
00038   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
00039 
00040   /// CallSite::get - This static method is sort of like a constructor.  It will
00041   /// create an appropriate call site for a Call or Invoke instruction, but it
00042   /// can also create a null initialized CallSite object for something which is
00043   /// NOT a call site.
00044   ///
00045   static CallSite get(Value *V) {
00046     if (Instruction *I = dyn_cast<Instruction>(V)) {
00047       if (I->getOpcode() == Instruction::Call)
00048         return CallSite((CallInst*)I);
00049       else if (I->getOpcode() == Instruction::Invoke)
00050         return CallSite((InvokeInst*)I);
00051     }
00052     return CallSite();
00053   }
00054 
00055   /// getType - Return the type of the instruction that generated this call site
00056   ///
00057   const Type *getType() const { return I->getType(); }
00058 
00059   /// getInstruction - Return the instruction this call site corresponds to
00060   ///
00061   Instruction *getInstruction() const { return I; }
00062 
00063   /// getCaller - Return the caller function for this call site
00064   ///
00065   Function *getCaller() const { return I->getParent()->getParent(); }
00066 
00067   /// getCalledValue - Return the pointer to function that is being called...
00068   ///
00069   Value *getCalledValue() const {
00070     assert(I && "Not a call or invoke instruction!");
00071     return I->getOperand(0);
00072   }
00073 
00074   /// getCalledFunction - Return the function being called if this is a direct
00075   /// call, otherwise return null (if it's an indirect call).
00076   ///
00077   Function *getCalledFunction() const {
00078     return dyn_cast<Function>(getCalledValue());
00079   }
00080 
00081   /// setCalledFunction - Set the callee to the specified value...
00082   ///
00083   void setCalledFunction(Value *V) {
00084     assert(I && "Not a call or invoke instruction!");
00085     I->setOperand(0, V);
00086   }
00087 
00088   Value *getArgument(unsigned ArgNo) const {
00089     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00090     return *(arg_begin()+ArgNo);
00091   }
00092 
00093   /// arg_iterator - The type of iterator to use when looping over actual
00094   /// arguments at this call site...
00095   typedef User::op_iterator arg_iterator;
00096 
00097   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
00098   /// list for a call site.
00099   ///
00100   arg_iterator arg_begin() const {
00101     assert(I && "Not a call or invoke instruction!");
00102     if (I->getOpcode() == Instruction::Call)
00103       return I->op_begin()+1; // Skip Function
00104     else
00105       return I->op_begin()+3; // Skip Function, BB, BB
00106   }
00107   arg_iterator arg_end() const { return I->op_end(); }
00108   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
00109 
00110   bool operator<(const CallSite &CS) const {
00111     return getInstruction() < CS.getInstruction();
00112   }
00113 };
00114 
00115 } // End llvm namespace
00116 
00117 #endif