Add assertions
[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 {
54     assert(I && "Not a call or invoke instruction!");
55     return I->getOperand(0);
56   }
57
58   /// getCalledFunction - Return the function being called if this is a direct
59   /// call, otherwise return null (if it's an indirect call).
60   ///
61   Function *getCalledFunction() const {
62     return dyn_cast<Function>(getCalledValue());
63   }
64
65   /// setCalledFunction - Set the callee to the specified value...
66   ///
67   void setCalledFunction(Value *V) {
68     assert(I && "Not a call or invoke instruction!");
69     I->setOperand(0, V);
70   }
71
72   /// arg_iterator - The type of iterator to use when looping over actual
73   /// arguments at this call site...
74   typedef User::op_iterator arg_iterator;
75
76   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
77   /// list for a call site.
78   ///
79   arg_iterator arg_begin() const {
80     assert(I && "Not a call or invoke instruction!");
81     if (I->getOpcode() == Instruction::Call)
82       return I->op_begin()+1; // Skip Function
83     else
84       return I->op_begin()+3; // Skip Function, BB, BB
85   }
86   arg_iterator arg_end() const { return I->op_end(); }
87 };
88
89 #endif