llvm_sys_wrapper/
function.rs
1#![allow(dead_code)]
2
3extern crate llvm_sys;
4
5use self::llvm_sys::core::*;
6use self::llvm_sys::prelude::*;
7use cstring_manager::CStringManager;
8
9#[derive(Debug)]
10pub struct Function {
11 llvm_function: LLVMValueRef,
12 llvm_module: LLVMModuleRef,
13 function_type: LLVMTypeRef,
14}
15
16impl Function {
17 pub fn new(module: LLVMModuleRef, name: &str, function_type: LLVMTypeRef) -> Function {
18 let function_name_ptr = CStringManager::new_cstring_as_ptr(name);
19 let function = unsafe { LLVMAddFunction(module, function_name_ptr, function_type) };
20 Function {
21 llvm_function: function,
22 llvm_module: module,
23 function_type: function_type
24 }
25 }
26
27 pub fn from_ptr(func_ptr: LLVMValueRef) -> Function {
28 Function {
29 llvm_function: func_ptr,
30 llvm_module: 0 as LLVMModuleRef,
31 function_type: 0 as LLVMTypeRef,
32 }
33 }
34
35 pub fn append_basic_block(&self, name: &str) -> LLVMBasicBlockRef {
36 let label_name_ptr = CStringManager::new_cstring_as_ptr(name);
37 if self.llvm_module.is_null() {
38 unsafe { LLVMAppendBasicBlock(self.llvm_function, label_name_ptr) }
39 }else{
40 let context = unsafe { LLVMGetModuleContext(self.llvm_module) };
41 unsafe { LLVMAppendBasicBlockInContext(context, self.llvm_function, label_name_ptr) }
42 }
43 }
44
45 pub fn as_ref(&self) -> LLVMValueRef {
46 self.llvm_function
47 }
48
49 #[inline]
50 pub fn get_param(&self, index: u32) -> LLVMValueRef {
51 unsafe { LLVMGetParam(self.llvm_function, index) }
52 }
53
54 #[inline]
55 pub fn params_count(&self) -> u32 {
56 unsafe { LLVMCountParams(self.llvm_function) }
57 }
58
59 #[inline]
60 pub fn get_function_type(&self) -> LLVMTypeRef {
61 self.function_type
62 }
63
64 #[inline]
65 pub fn get_return_type(&self) -> LLVMTypeRef {
66 unsafe { LLVMGetReturnType(self.function_type) }
67 }
68
69 #[inline]
70 pub fn get_param_types(&self) -> LLVMTypeRef {
71 let mut types : LLVMTypeRef = 0 as LLVMTypeRef;
72 let ptr: *mut LLVMTypeRef = &mut types;
73 unsafe {
74 LLVMGetParamTypes(self.function_type, ptr);
75 }
76 types
77 }
78}