llvm_quick/core/
mod.rs

1use std::borrow::Borrow;
2use std::ffi::{c_char, CStr};
3use std::fmt::{Debug, Formatter};
4use std::mem::forget;
5use std::ops::Deref;
6use std::ptr::NonNull;
7
8use llvm_sys::core::*;
9
10pub mod basic_block;
11pub mod contexts;
12pub mod instruction_builders;
13pub mod instructions;
14pub mod memory_buffers;
15pub mod metadata;
16pub mod module_providers;
17pub mod modules;
18pub mod pass_managers;
19pub mod threading;
20pub mod types;
21pub mod values;
22
23/// Return the major, minor, and patch version of LLVM.
24pub fn get_version() -> (u32, u32, u32) {
25    let mut r = (0, 0, 0);
26    unsafe { LLVMGetVersion(&mut r.0, &mut r.1, &mut r.2) }
27    r
28}
29
30/// Deallocate and destroy all ManagedStatic variables.
31pub unsafe fn shutdown() {
32    unsafe { LLVMShutdown() }
33}
34
35pub struct Message {
36    ptr: NonNull<CStr>,
37}
38
39impl Message {
40    pub fn create(s: &CStr) -> Self {
41        unsafe { Self::from_raw(LLVMCreateMessage(s.as_ptr())) }
42    }
43}
44
45impl Drop for Message {
46    fn drop(&mut self) {
47        unsafe { LLVMDisposeMessage(self.as_raw()) }
48    }
49}
50
51impl Debug for Message {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        Debug::fmt(&self.deref(), f)
54    }
55}
56
57impl AsRef<CStr> for Message {
58    fn as_ref(&self) -> &CStr {
59        self.deref()
60    }
61}
62
63impl Borrow<CStr> for Message {
64    fn borrow(&self) -> &CStr {
65        self.deref()
66    }
67}
68
69impl Deref for Message {
70    type Target = CStr;
71
72    fn deref(&self) -> &Self::Target {
73        unsafe { self.ptr.as_ref() }
74    }
75}
76
77impl Message {
78    pub unsafe fn from_ptr(ptr: *mut c_char) -> Option<Self> {
79        if ptr.is_null() {
80            None
81        } else {
82            let ptr = unsafe { CStr::from_ptr(ptr).into() };
83            Some(Self { ptr })
84        }
85    }
86
87    pub unsafe fn from_raw(ptr: *mut c_char) -> Self {
88        debug_assert!(!ptr.is_null());
89        let ptr = unsafe { CStr::from_ptr(ptr).into() };
90        Self { ptr }
91    }
92
93    pub fn as_raw(&self) -> *mut c_char {
94        self.ptr.as_ptr() as _
95    }
96
97    pub fn into_raw(self) -> *mut c_char {
98        let ptr = self.as_raw();
99        forget(self);
100        ptr
101    }
102}
103
104#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
105pub struct IntrinsicId(pub u32);