Skip to main content

rustc_codegen_llvm/llvm/ffi/
branded.rs

1//! Lifetime-branded references to things manipulated by the LLVM bindings.
2
3use std::marker::PhantomData;
4use std::ptr;
5
6/// Corresponds to `LLVMContextRef`.
7#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for Context<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for Context<'ll> {
    #[inline]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<ContextRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomData<fn(&'ll ())
                    -> &'ll ()>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Context<'ll> { }Copy)]
8#[repr(transparent)]
9pub(crate) struct Context<'ll> {
10    llcx_raw: ptr::NonNull<ContextRaw>,
11    _invariant_ll: PhantomData<fn(&'ll ()) -> &'ll ()>,
12}
13
14/// Corresponds to `LLVMModuleRef`.
15#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for Module<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for Module<'ll> {
    #[inline]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<ModuleRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomData<fn(&'ll ())
                    -> &'ll ()>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Module<'ll> { }Copy)]
16#[repr(transparent)]
17pub(crate) struct Module<'ll> {
18    llmod_raw: ptr::NonNull<ModuleRaw>,
19    _invariant_ll: PhantomData<fn(&'ll ()) -> &'ll ()>,
20}
21
22impl<'ll> Module<'ll> {
23    pub(crate) fn llmod_raw(&self) -> ptr::NonNull<ModuleRaw> {
24        self.llmod_raw
25    }
26}
27
28unsafe extern "C" {
29    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Context`].
30    pub(crate) type ContextRaw;
31    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Module`].
32    pub(crate) type ModuleRaw;
33}
34
35/// Converts an unbranded pointer-to-context to a lifetime-branded [`Context`].
36///
37/// # Safety
38/// - The pointer must be a valid LLVM context.
39/// - If the context is passed to safe code, its caller-requested lifetime must be distinct
40///   from that of any other context, and must not outlive the underlying context.
41pub(crate) unsafe fn wrap_llcx<'ll>(llcx_raw: ptr::NonNull<ContextRaw>) -> Context<'ll> {
42    Context { llcx_raw, _invariant_ll: PhantomData }
43}
44
45/// Converts an unbranded pointer-to-module to a lifetime-branded [`Module`].
46///
47/// # Safety
48/// - The pointer must be a valid LLVM module within the specified context.
49/// - The module must not be disposed during the lifetime of its context.
50pub(crate) unsafe fn wrap_llmod<'ll>(
51    _llcx: Context<'ll>, // The context constrains `'ll` but isn't actually used.
52    llmod_raw: ptr::NonNull<ModuleRaw>,
53) -> Module<'ll> {
54    Module { llmod_raw, _invariant_ll: PhantomData }
55}