// Copyright 2022 the Gigamono authors. All rights reserved. GPL-3.0 License. use std::ffi::CString; use anyhow::Result; use llvm_sys::{core::LLVMAddFunction, prelude::LLVMValueRef}; use super::{module::LLModule, types::LLFunctionType}; /// This is a wrapper for LLVM Function. pub(crate) struct LLFunction { function_ref: LLVMValueRef, } impl LLFunction { /// Creates a new LLVM function. /// /// # Safety /// I'm unsure of the safety using a temporary `CString` here. /// From the doc, it appears it may use a reference or copy the value depending on context. /// /// - https://llvm.org/doxygen/Twine_8h_source.html#l00477 /// - https://llvm.org/doxygen/Value_8cpp_source.html#l00315 pub(crate) fn new(name: &str, module: &LLModule, signature: &LLFunctionType) -> Result { // TODO(appcypher): Investigate safety properly. Ok(Self { function_ref: unsafe { LLVMAddFunction( module.as_ptr(), CString::new(name)?.as_ptr(), signature.as_ptr(), ) }, }) } }