Add explicit keywords.
[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 is distributed under the University of Illinois Open Source
6 // 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 (notice that it has only a single data member).
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_SUPPORT_CALLSITE_H
21 #define LLVM_SUPPORT_CALLSITE_H
22
23 #include "llvm/Instruction.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/ParameterAttributes.h"
26
27 namespace llvm {
28
29 class CallInst;
30 class InvokeInst;
31 class ParamAttrsList;
32
33 class CallSite {
34   Instruction *I;
35 public:
36   CallSite() : I(0) {}
37   CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI)) {}
38   CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II)) {}
39   CallSite(Instruction *C);
40   CallSite(const CallSite &CS) : I(CS.I) {}
41   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
42
43   /// CallSite::get - This static method is sort of like a constructor.  It will
44   /// create an appropriate call site for a Call or Invoke instruction, but it
45   /// can also create a null initialized CallSite object for something which is
46   /// NOT a call site.
47   ///
48   static CallSite get(Value *V) {
49     if (Instruction *I = dyn_cast<Instruction>(V)) {
50       if (I->getOpcode() == Instruction::Call)
51         return CallSite(reinterpret_cast<CallInst*>(I));
52       else if (I->getOpcode() == Instruction::Invoke)
53         return CallSite(reinterpret_cast<InvokeInst*>(I));
54     }
55     return CallSite();
56   }
57
58   /// getCallingConv/setCallingConv - get or set the calling convention of the
59   /// call.
60   unsigned getCallingConv() const;
61   void setCallingConv(unsigned CC);
62
63   /// getParamAttrs/setParamAttrs - get or set the parameter attributes of
64   /// the call.
65   const ParamAttrsList *getParamAttrs() const;
66   void setParamAttrs(const ParamAttrsList *PAL);
67
68   /// paramHasAttr - whether the call or the callee has the given attribute.
69   bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
70
71   /// @brief Determine if the call does not access memory.
72   bool doesNotAccessMemory() const;
73
74   /// @brief Determine if the call does not access or only reads memory.
75   bool onlyReadsMemory() const;
76
77   /// @brief Determine if the call cannot unwind.
78   bool doesNotThrow() const;
79   void setDoesNotThrow(bool doesNotThrow = true);
80
81   /// getType - Return the type of the instruction that generated this call site
82   ///
83   const Type *getType() const { return I->getType(); }
84
85   /// getInstruction - Return the instruction this call site corresponds to
86   ///
87   Instruction *getInstruction() const { return I; }
88
89   /// getCaller - Return the caller function for this call site
90   ///
91   Function *getCaller() const { return I->getParent()->getParent(); }
92
93   /// getCalledValue - Return the pointer to function that is being called...
94   ///
95   Value *getCalledValue() const {
96     assert(I && "Not a call or invoke instruction!");
97     return I->getOperand(0);
98   }
99
100   /// getCalledFunction - Return the function being called if this is a direct
101   /// call, otherwise return null (if it's an indirect call).
102   ///
103   Function *getCalledFunction() const {
104     return dyn_cast<Function>(getCalledValue());
105   }
106
107   /// setCalledFunction - Set the callee to the specified value...
108   ///
109   void setCalledFunction(Value *V) {
110     assert(I && "Not a call or invoke instruction!");
111     I->setOperand(0, V);
112   }
113
114   Value *getArgument(unsigned ArgNo) const {
115     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
116     return *(arg_begin()+ArgNo);
117   }
118
119   void setArgument(unsigned ArgNo, Value* newVal) {
120     assert(I && "Not a call or invoke instruction!");
121     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
122     if (I->getOpcode() == Instruction::Call)
123       I->setOperand(ArgNo+1, newVal); // Skip Function
124     else
125       I->setOperand(ArgNo+3, newVal); // Skip Function, BB, BB
126   }
127
128   /// arg_iterator - The type of iterator to use when looping over actual
129   /// arguments at this call site...
130   typedef User::op_iterator arg_iterator;
131
132   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
133   /// list for a call site.
134   ///
135   arg_iterator arg_begin() const {
136     assert(I && "Not a call or invoke instruction!");
137     if (I->getOpcode() == Instruction::Call)
138       return I->op_begin()+1; // Skip Function
139     else
140       return I->op_begin()+3; // Skip Function, BB, BB
141   }
142   arg_iterator arg_end() const { return I->op_end(); }
143   bool arg_empty() const { return arg_end() == arg_begin(); }
144   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
145
146   bool operator<(const CallSite &CS) const {
147     return getInstruction() < CS.getInstruction();
148   }
149 };
150
151 } // End llvm namespace
152
153 #endif