LLVM API Documentation

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(reinterpret_cast<Instruction*>(CI)) {}
00036   CallSite(InvokeInst *II) : I(reinterpret_cast<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(reinterpret_cast<CallInst*>(I));
00049       else if (I->getOpcode() == Instruction::Invoke)
00050         return CallSite(reinterpret_cast<InvokeInst*>(I));
00051     }
00052     return CallSite();
00053   }
00054 
00055   /// getCallingConv/setCallingConv - get or set the calling convention of the
00056   /// call.
00057   unsigned getCallingConv() const;
00058   void setCallingConv(unsigned CC);
00059 
00060   /// getType - Return the type of the instruction that generated this call site
00061   ///
00062   const Type *getType() const { return I->getType(); }
00063 
00064   /// getInstruction - Return the instruction this call site corresponds to
00065   ///
00066   Instruction *getInstruction() const { return I; }
00067 
00068   /// getCaller - Return the caller function for this call site
00069   ///
00070   Function *getCaller() const { return I->getParent()->getParent(); }
00071 
00072   /// getCalledValue - Return the pointer to function that is being called...
00073   ///
00074   Value *getCalledValue() const {
00075     assert(I && "Not a call or invoke instruction!");
00076     return I->getOperand(0);
00077   }
00078 
00079   /// getCalledFunction - Return the function being called if this is a direct
00080   /// call, otherwise return null (if it's an indirect call).
00081   ///
00082   Function *getCalledFunction() const {
00083     return dyn_cast<Function>(getCalledValue());
00084   }
00085 
00086   /// setCalledFunction - Set the callee to the specified value...
00087   ///
00088   void setCalledFunction(Value *V) {
00089     assert(I && "Not a call or invoke instruction!");
00090     I->setOperand(0, V);
00091   }
00092 
00093   Value *getArgument(unsigned ArgNo) const {
00094     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00095     return *(arg_begin()+ArgNo);
00096   }
00097 
00098   /// arg_iterator - The type of iterator to use when looping over actual
00099   /// arguments at this call site...
00100   typedef User::op_iterator arg_iterator;
00101 
00102   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
00103   /// list for a call site.
00104   ///
00105   arg_iterator arg_begin() const {
00106     assert(I && "Not a call or invoke instruction!");
00107     if (I->getOpcode() == Instruction::Call)
00108       return I->op_begin()+1; // Skip Function
00109     else
00110       return I->op_begin()+3; // Skip Function, BB, BB
00111   }
00112   arg_iterator arg_end() const { return I->op_end(); }
00113   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
00114 
00115   bool operator<(const CallSite &CS) const {
00116     return getInstruction() < CS.getInstruction();
00117   }
00118 };
00119 
00120 } // End llvm namespace
00121 
00122 #endif