llvm_lib/
builder.rs

1use super::basic_block::BasicBlockRef;
2use crate::core::context::ContextRef;
3
4use crate::core::values::ValueRef;
5use llvm_sys::core::{
6    LLVMBuildRetVoid, LLVMCreateBuilderInContext, LLVMDisposeBuilder, LLVMPositionBuilderAtEnd,
7};
8use llvm_sys::prelude::LLVMBuilderRef;
9
10/// LLVM Builder wrapper
11pub struct BuilderRef(LLVMBuilderRef);
12
13impl BuilderRef {
14    /// Create LLVM module with name
15    #[must_use]
16    pub fn new(context: &ContextRef) -> Self {
17        unsafe { Self(LLVMCreateBuilderInContext(**context)) }
18    }
19
20    /// Get raw builder reference
21    #[must_use]
22    pub const fn get(&self) -> LLVMBuilderRef {
23        self.0
24    }
25
26    /// Set builder position at end
27    pub fn position_at_end(&self, basic_block: &BasicBlockRef) {
28        unsafe { LLVMPositionBuilderAtEnd(self.0, basic_block.get()) }
29    }
30
31    /// Set and return builder return void value
32    #[must_use]
33    pub fn build_ret_void(&self) -> ValueRef {
34        unsafe { ValueRef::from(LLVMBuildRetVoid(self.0)) }
35    }
36}
37
38impl Drop for BuilderRef {
39    /// Dispose Builder
40    fn drop(&mut self) {
41        unsafe {
42            LLVMDisposeBuilder(self.0);
43        }
44    }
45}