Added LLVM copyright header (for lack of a better term).
[oota-llvm.git] / include / llvm / Support / CallSite.h
1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the CallSite class, which is a handy wrapper for code that
11 // wants to treat Call and Invoke instructions in a generic way.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_CALLSITE_H
16 #define LLVM_SUPPORT_CALLSITE_H
17
18 #include "llvm/Instruction.h"
19
20 class CallInst;
21 class InvokeInst;
22
23 class CallSite {
24   Instruction *I;
25 public:
26   CallSite() : I(0) {}
27   CallSite(CallInst *CI) : I((Instruction*)CI) {}
28   CallSite(InvokeInst *II) : I((Instruction*)II) {}
29   CallSite(const CallSite &CS) : I(CS.I) {}
30   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
31
32   /// CallSite::get - This static method is sort of like a constructor.  It will
33   /// create an appropriate call site for a Call or Invoke instruction, but it
34   /// can also create a null initialized CallSite object for something which is
35   /// NOT a call site.
36   ///
37   static CallSite get(Value *V) {
38     if (Instruction *I = dyn_cast<Instruction>(V)) {
39       if (I->getOpcode() == Instruction::Call)
40         return CallSite((CallInst*)I);
41       else if (I->getOpcode() == Instruction::Invoke)
42         return CallSite((InvokeInst*)I);
43     }
44     return CallSite();
45   }
46
47   /// getInstruction - Return the instruction this call site corresponds to
48   ///
49   Instruction *getInstruction() const { return I; }
50
51   /// getCalledValue - Return the pointer to function that is being called...
52   ///
53   Value *getCalledValue() const { return I->getOperand(0); }
54
55   /// getCalledFunction - Return the function being called if this is a direct
56   /// call, otherwise return null (if it's an indirect call).
57   ///
58   Function *getCalledFunction() const {
59     return dyn_cast<Function>(getCalledValue());
60   }
61
62   /// setCalledFunction - Set the callee to the specified value...
63   ///
64   void setCalledFunction(Value *V) {
65     I->setOperand(0, V);
66   }
67
68   /// arg_iterator - The type of iterator to use when looping over actual
69   /// arguments at this call site...
70   typedef User::op_iterator arg_iterator;
71
72   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
73   /// list for a call site.
74   ///
75   arg_iterator arg_begin() const {
76     if (I->getOpcode() == Instruction::Call)
77       return I->op_begin()+1; // Skip Function
78     else
79       return I->op_begin()+3; // Skip Function, BB, BB
80   }
81   arg_iterator arg_end() const { return I->op_end(); }
82 };
83
84 #endif