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
38
39
40
41
42
43
44
45
use super::basic_block::BasicBlockRef;
use crate::core::context::ContextRef;

use crate::core::values::ValueRef;
use llvm_sys::core::{
    LLVMBuildRetVoid, LLVMCreateBuilderInContext, LLVMDisposeBuilder, LLVMPositionBuilderAtEnd,
};
use llvm_sys::prelude::LLVMBuilderRef;

/// LLVM Builder wrapper
pub struct BuilderRef(LLVMBuilderRef);

impl BuilderRef {
    /// Create LLVM module with name
    #[must_use]
    pub fn new(context: &ContextRef) -> Self {
        unsafe { Self(LLVMCreateBuilderInContext(**context)) }
    }

    /// Get raw builder reference
    #[must_use]
    pub const fn get(&self) -> LLVMBuilderRef {
        self.0
    }

    /// Set builder position at end
    pub fn position_at_end(&self, basic_block: &BasicBlockRef) {
        unsafe { LLVMPositionBuilderAtEnd(self.0, basic_block.get()) }
    }

    /// Set and return builder return void value
    #[must_use]
    pub fn build_ret_void(&self) -> ValueRef {
        unsafe { ValueRef::from(LLVMBuildRetVoid(self.0)) }
    }
}

impl Drop for BuilderRef {
    /// Dispose Builder
    fn drop(&mut self) {
        unsafe {
            LLVMDisposeBuilder(self.0);
        }
    }
}