use llvm_sys::core::*;
use llvm_sys::prelude::LLVMValueRef;
use std::marker::PhantomData;
use crate::types::*;
use crate::values::*;
use crate::*;
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct CallInstruction<'ctx>(LLVMValueRef, PhantomData<&'ctx ()>);
impl_instr_debug!(CallInstruction);
impl_as_operand_for_instr!(CallInstruction);
impl_send_sync!(CallInstruction);
impl<'ctx> GetType<'ctx> for CallInstruction<'ctx> {}
impl<'ctx> GetDebugMetadata<'ctx> for CallInstruction<'ctx> {}
impl<'ctx> InstructionDebugLoc for CallInstruction<'ctx> {}
impl<'ctx> InstructionTrait<'ctx> for CallInstruction<'ctx> {}
impl<'ctx> CallInstruction<'ctx> {
pub fn callee_function(&self) -> Option<Function<'ctx>> {
match self.callee() {
Operand::Constant(Constant::Function(f)) => Some(f),
_ => None,
}
}
pub fn callee_inline_asm(&self) -> Option<InlineAsm<'ctx>> {
match self.callee() {
Operand::InlineAsm(ia) => Some(ia),
_ => None,
}
}
pub fn callee_function_type(&self) -> FunctionType<'ctx> {
FunctionType::from_llvm(unsafe { LLVMGetElementType(self.callee().get_type().type_ref()) })
}
pub fn callee(&self) -> Operand<'ctx> {
let operand_id = self.num_arguments();
Operand::from_llvm(unsafe { LLVMGetOperand(self.0, operand_id as u32) })
}
pub fn num_arguments(&self) -> usize {
let num_operands = unsafe { LLVMGetNumOperands(self.0) };
num_operands as usize - 1
}
pub fn arguments(&self) -> Vec<Operand<'ctx>> {
(0..self.num_arguments())
.map(|i| Operand::from_llvm(unsafe { LLVMGetOperand(self.0, i as u32) }))
.collect()
}
pub fn argument(&self, index: usize) -> Option<Operand<'ctx>> {
if index < self.num_arguments() {
Some(Operand::from_llvm(unsafe { LLVMGetOperand(self.0, index as u32) }))
} else {
None
}
}
pub fn is_tail_call(&self) -> bool {
unsafe { LLVMIsTailCall(self.0) == 1 }
}
pub fn is_inline_asm_call(&self) -> bool {
match self.callee() {
Operand::InlineAsm(_) => true,
_ => false,
}
}
pub fn is_intrinsic_call(&self) -> bool {
unsafe { !LLVMIsAIntrinsicInst(self.0).is_null() }
}
}
impl<'ctx> ValueOpcode for CallInstruction<'ctx> {
fn opcode(&self) -> Opcode {
Opcode::Call
}
}
impl<'ctx> AsInstruction<'ctx> for CallInstruction<'ctx> {
fn as_instruction(&self) -> Instruction<'ctx> {
Instruction::Call(*self)
}
}
impl_positional_value_ref!(CallInstruction, 0);
impl_positional_from_llvm_value!(CallInstruction);