llvm_sys_wrapper/
struct_type.rs

1extern crate llvm_sys;
2
3use self::llvm_sys::core::*;
4use self::llvm_sys::prelude::*;
5use cstring_manager::CStringManager;
6
7pub struct Struct {
8    struct_type: LLVMTypeRef
9}
10
11impl Struct {
12    pub fn new_with_name(ctx: LLVMContextRef, name: &str) -> Struct {
13        let name_ptr = CStringManager::new_cstring_as_ptr(name);
14        let struct_ty = unsafe { LLVMStructCreateNamed(ctx, name_ptr) };
15
16        Struct {
17            struct_type: struct_ty
18        }
19    }
20
21    pub fn new(ctx: LLVMContextRef, fields: &mut [LLVMTypeRef], packed: bool) -> Struct {
22        let struct_ty = unsafe { LLVMStructTypeInContext(ctx, fields.as_mut_ptr(), fields.len() as u32, if packed {1}else{0}) };
23
24        Struct {
25            struct_type: struct_ty
26        }
27    }
28
29    #[inline]
30    pub fn new_const_struct(constant_values: &mut [LLVMValueRef], packed: bool) -> LLVMValueRef {
31        unsafe { LLVMConstStruct(constant_values.as_mut_ptr(), constant_values.len() as u32, if packed {1}else{0}) }
32    }
33
34    pub fn as_ref(&self) -> LLVMTypeRef {
35        self.struct_type
36    }
37
38    #[inline]
39    pub fn set_body(&self, fields: &mut [LLVMTypeRef], packed: bool){
40        unsafe { LLVMStructSetBody(self.struct_type, fields.as_mut_ptr(), fields.len() as u32, if packed {1}else{0}) }
41    }
42}