1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::core::context::ContextRef;
use crate::core::values::ValueRef;
use crate::{CString, GetRef};
use llvm_sys::core::LLVMAppendBasicBlockInContext;
use llvm_sys::prelude::LLVMBasicBlockRef;

/// LLVM Basic block wrapper
pub struct BasicBlockRef(LLVMBasicBlockRef);

impl GetRef for BasicBlockRef {
    type RawRef = LLVMBasicBlockRef;
    fn get_ref(&self) -> Self::RawRef {
        self.0
    }
}

impl BasicBlockRef {
    // Get raw basic block reference
    #[must_use]
    pub const fn get(&self) -> LLVMBasicBlockRef {
        self.0
    }

    /// Append basic block in context
    /// TODO: return error
    #[must_use]
    pub fn append_in_context(context: &ContextRef, function: &ValueRef, name: &str) -> Self {
        unsafe {
            let c_name = CString::from(name);
            Self(LLVMAppendBasicBlockInContext(
                **context,
                **function,
                c_name.as_ptr(),
            ))
        }
    }
}