Fix bug: 2003-01-30-LinkerRename.ll
[oota-llvm.git] / lib / VMCore / iCall.cpp
1 //===-- iCall.cpp - Implement the call & invoke instructions -----*- C++ -*--=//
2 //
3 // This file implements the call and invoke instructions.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOther.h"
8 #include "llvm/iTerminators.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Function.h"
11
12 //===----------------------------------------------------------------------===//
13 //                        CallInst Implementation
14 //===----------------------------------------------------------------------===//
15
16 CallInst::CallInst(Value *Func, const std::vector<Value*> &params, 
17                    const std::string &Name, Instruction *InsertBefore) 
18   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
19                                  ->getElementType())->getReturnType(),
20                 Instruction::Call, Name, InsertBefore) {
21   Operands.reserve(1+params.size());
22   Operands.push_back(Use(Func, this));
23
24   const FunctionType *MTy = 
25     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
26
27   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
28   assert(params.size() == PL.size() || 
29          (MTy->isVarArg() && params.size() > PL.size()) &&
30          "Calling a function with bad signature");
31   for (unsigned i = 0; i < params.size(); i++)
32     Operands.push_back(Use(params[i], this));
33 }
34
35 CallInst::CallInst(Value *Func, const std::string &Name,
36                    Instruction *InsertBefore)
37   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
38                                    ->getElementType())->getReturnType(),
39                 Instruction::Call, Name, InsertBefore) {
40   Operands.reserve(1);
41   Operands.push_back(Use(Func, this));
42   
43   const FunctionType *MTy = 
44     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
45
46   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
47   assert(PL.empty() && "Calling a function with bad signature");
48 }
49
50 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
51                    Instruction  *InsertBefore)
52   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
53                                    ->getElementType())->getReturnType(),
54                 Instruction::Call, Name, InsertBefore) {
55   Operands.reserve(2);
56   Operands.push_back(Use(Func, this));
57   
58   const FunctionType *MTy = 
59     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
60
61   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
62   assert(PL.size() == 1 || (MTy->isVarArg() && PL.empty()) &&
63          "Calling a function with bad signature");
64   Operands.push_back(Use(A, this));
65 }
66
67 CallInst::CallInst(const CallInst &CI) 
68   : Instruction(CI.getType(), Instruction::Call) {
69   Operands.reserve(CI.Operands.size());
70   for (unsigned i = 0; i < CI.Operands.size(); ++i)
71     Operands.push_back(Use(CI.Operands[i], this));
72 }
73
74 //===----------------------------------------------------------------------===//
75 //                        InvokeInst Implementation
76 //===----------------------------------------------------------------------===//
77
78 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
79                        BasicBlock *IfException,
80                        const std::vector<Value*> &params,
81                        const std::string &Name, Instruction *InsertBefore)
82   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
83                                     ->getElementType())->getReturnType(),
84                    Instruction::Invoke, Name, InsertBefore) {
85   Operands.reserve(3+params.size());
86   Operands.push_back(Use(Func, this));
87   Operands.push_back(Use((Value*)IfNormal, this));
88   Operands.push_back(Use((Value*)IfException, this));
89   const FunctionType *MTy = 
90     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
91   
92   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
93   assert((params.size() == PL.size()) || 
94          (MTy->isVarArg() && params.size() > PL.size()) &&
95          "Calling a function with bad signature");
96   
97   for (unsigned i = 0; i < params.size(); i++)
98     Operands.push_back(Use(params[i], this));
99 }
100
101 InvokeInst::InvokeInst(const InvokeInst &CI) 
102   : TerminatorInst(CI.getType(), Instruction::Invoke) {
103   Operands.reserve(CI.Operands.size());
104   for (unsigned i = 0; i < CI.Operands.size(); ++i)
105     Operands.push_back(Use(CI.Operands[i], this));
106 }
107