Add a warning about not "new"ing or "delete"ing CallSites
[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 // NOTE: This class is supposed to have "value semantics". So it should be
14 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
15 // is efficiently copyable, assignable and constructable, with cost equivalent
16 // to copying a pointer. (You will notice that it has only a single data
17 // member.)
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_SUPPORT_CALLSITE_H
22 #define LLVM_SUPPORT_CALLSITE_H
23
24 #include "llvm/Instruction.h"
25
26 class CallInst;
27 class InvokeInst;
28
29 class CallSite {
30   Instruction *I;
31 public:
32   CallSite() : I(0) {}
33   CallSite(CallInst *CI) : I((Instruction*)CI) {}
34   CallSite(InvokeInst *II) : I((Instruction*)II) {}
35   CallSite(const CallSite &CS) : I(CS.I) {}
36   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
37
38   /// CallSite::get - This static method is sort of like a constructor.  It will
39   /// create an appropriate call site for a Call or Invoke instruction, but it
40   /// can also create a null initialized CallSite object for something which is
41   /// NOT a call site.
42   ///
43   static CallSite get(Value *V) {
44     if (Instruction *I = dyn_cast<Instruction>(V)) {
45       if (I->getOpcode() == Instruction::Call)
46         return CallSite((CallInst*)I);
47       else if (I->getOpcode() == Instruction::Invoke)
48         return CallSite((InvokeInst*)I);
49     }
50     return CallSite();
51   }
52
53   /// getType - Return the type of the instruction that generated this call site
54   ///
55   const Type *getType () const { return I->getType (); }
56
57   /// getInstruction - Return the instruction this call site corresponds to
58   ///
59   Instruction *getInstruction() const { return I; }
60
61   /// getCalledValue - Return the pointer to function that is being called...
62   ///
63   Value *getCalledValue() const {
64     assert(I && "Not a call or invoke instruction!");
65     return I->getOperand(0);
66   }
67
68   /// getCalledFunction - Return the function being called if this is a direct
69   /// call, otherwise return null (if it's an indirect call).
70   ///
71   /// FIXME: This should be inlined once ConstantPointerRefs are gone.  :(
72   Function *getCalledFunction() const;
73
74   /// setCalledFunction - Set the callee to the specified value...
75   ///
76   void setCalledFunction(Value *V) {
77     assert(I && "Not a call or invoke instruction!");
78     I->setOperand(0, V);
79   }
80
81   /// arg_iterator - The type of iterator to use when looping over actual
82   /// arguments at this call site...
83   typedef User::op_iterator arg_iterator;
84
85   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
86   /// list for a call site.
87   ///
88   arg_iterator arg_begin() const {
89     assert(I && "Not a call or invoke instruction!");
90     if (I->getOpcode() == Instruction::Call)
91       return I->op_begin()+1; // Skip Function
92     else
93       return I->op_begin()+3; // Skip Function, BB, BB
94   }
95   arg_iterator arg_end() const { return I->op_end(); }
96   unsigned arg_size() const { return arg_end() - arg_begin(); }
97
98   bool operator<(const CallSite &CS) const {
99     return getInstruction() < CS.getInstruction();
100   }
101 };
102
103 #endif