llvm_wrap/
context.rs

1//! A wrapper around the LLVM global context
2
3use super::*;
4use super::c_api::*;
5
6/// Creates a new module with the given name in this context
7pub fn create_module<S>(name: S) -> Module where S: AsRef<str> {
8    Module {
9        module: Some(
10            unsafe {
11                LLVMModuleCreateWithNameInContext(into_c(name).as_ptr(), context())
12            }
13        )
14    }
15}
16
17/// Creates a new builder in this context
18pub fn create_builder() -> Builder {
19    Builder {
20        builder: Some(
21            unsafe {
22                LLVMCreateBuilderInContext(context())
23            }
24        )
25    }
26}
27
28/// A constant struct with the given elements
29pub fn const_struct(elements: Vec<Value>, packed: bool) -> Value {
30    Value {
31        value: unsafe {
32            LLVMConstStructInContext(context(), val_vec(&elements).as_mut_ptr(), elements.len() as u32, packed as i32)
33        }
34    }
35}
36
37/// A constant string with the given value
38pub fn const_string<S>(string: S, null_terminated: bool) -> Value where S: AsRef<str> {
39    Value {
40        value: unsafe {
41            let string = string.as_ref();
42            LLVMConstStringInContext(context(), into_c(string).as_ptr(), string.len() as u32, null_terminated as i32)
43        }
44    }
45}