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/// FIXME(Zalathar): Use the stdlib's `PhantomInvariantLifetime` when its API is settled.
7type PhantomInvariantLifetime<'a> = PhantomData<fn(&'a ()) -> &'a ()>;
8
9/// Corresponds to `LLVMContextRef`.
10#[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]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<ContextRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomInvariantLifetime<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Context<'ll> { }Copy)]
11#[repr(transparent)]
12#[rustc_pass_by_value]
13pub(crate) struct Context<'ll> {
14    llcx_raw: ptr::NonNull<ContextRaw>,
15    _invariant_ll: PhantomInvariantLifetime<'ll>,
16}
17
18/// Corresponds to `LLVMModuleRef`.
19#[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]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<ModuleRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomInvariantLifetime<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Module<'ll> { }Copy)]
20#[repr(transparent)]
21#[rustc_pass_by_value]
22pub(crate) struct Module<'ll> {
23    llmod_raw: ptr::NonNull<ModuleRaw>,
24    _invariant_ll: PhantomInvariantLifetime<'ll>,
25}
26
27impl<'ll> Module<'ll> {
28    pub(crate) fn llmod_raw(self) -> ptr::NonNull<ModuleRaw> {
29        self.llmod_raw
30    }
31}
32
33unsafe extern "C" {
34    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Context`].
35    pub(crate) type ContextRaw;
36    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Module`].
37    pub(crate) type ModuleRaw;
38}
39
40/// Converts an unbranded pointer-to-context to a lifetime-branded [`Context`].
41///
42/// # Safety
43/// - The pointer must be a valid LLVM context.
44/// - If the context is passed to safe code, its caller-requested lifetime must be distinct
45///   from that of any other context, and must not outlive the underlying context.
46pub(crate) unsafe fn wrap_llcx<'ll>(llcx_raw: ptr::NonNull<ContextRaw>) -> Context<'ll> {
47    Context { llcx_raw, _invariant_ll: PhantomData }
48}
49
50/// Converts an unbranded pointer-to-module to a lifetime-branded [`Module`].
51///
52/// # Safety
53/// - The pointer must be a valid LLVM module within the specified context.
54/// - The module must not be disposed during the lifetime of its context.
55pub(crate) unsafe fn wrap_llmod<'ll>(
56    _llcx: Context<'ll>, // The context constrains `'ll` but isn't actually used.
57    llmod_raw: ptr::NonNull<ModuleRaw>,
58) -> Module<'ll> {
59    Module { llmod_raw, _invariant_ll: PhantomData }
60}
61
62/// Corresponds to `LLVMTypeRef`.
63///
64/// LLVM types are interned within their LLVM context ([`Context<'ll>`]), so they
65/// can be compared for equality (and hashed) based on their pointer address.
66#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for Type<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for Type<'ll> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<TypeRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomInvariantLifetime<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Type<'ll> { }Copy, #[automatically_derived]impl<'ll> ::core::marker::StructuralPartialEq for
    Type<'ll> {
}
#[automatically_derived]impl<'ll> ::core::cmp::PartialEq for Type<'ll> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.llty_raw == other.llty_raw &&
            self._invariant_ll == other._invariant_ll
    }
}PartialEq, #[automatically_derived]impl<'ll> ::core::cmp::Eq for Type<'ll> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ptr::NonNull<TypeRaw>>;
        let _: ::core::cmp::AssertParamIsEq<PhantomInvariantLifetime<'ll>>;
    }
}Eq, #[automatically_derived]impl<'ll> ::core::hash::Hash for Type<'ll> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.llty_raw, state);
        ::core::hash::Hash::hash(&self._invariant_ll, state)
    }
}Hash)]
67#[repr(transparent)]
68#[rustc_pass_by_value]
69pub(crate) struct Type<'ll> {
70    llty_raw: ptr::NonNull<TypeRaw>,
71    _invariant_ll: PhantomInvariantLifetime<'ll>,
72}
73
74unsafe extern "C" {
75    /// Pointee type of the raw pointer wrapped by [`Type`].
76    type TypeRaw;
77}
78
79/// Corresponds to `LLVMValueRef`.
80#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for Value<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for Value<'ll> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<ValueRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomInvariantLifetime<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Value<'ll> { }Copy, #[automatically_derived]impl<'ll> ::core::marker::StructuralPartialEq for
    Value<'ll> {
}
#[automatically_derived]impl<'ll> ::core::cmp::PartialEq for Value<'ll> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.llval_raw == other.llval_raw &&
            self._invariant_ll == other._invariant_ll
    }
}PartialEq, #[automatically_derived]impl<'ll> ::core::cmp::Eq for Value<'ll> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ptr::NonNull<ValueRaw>>;
        let _: ::core::cmp::AssertParamIsEq<PhantomInvariantLifetime<'ll>>;
    }
}Eq, #[automatically_derived]impl<'ll> ::core::hash::Hash for Value<'ll> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.llval_raw, state);
        ::core::hash::Hash::hash(&self._invariant_ll, state)
    }
}Hash)]
81#[repr(transparent)]
82#[rustc_pass_by_value]
83pub(crate) struct Value<'ll> {
84    llval_raw: ptr::NonNull<ValueRaw>,
85    _invariant_ll: PhantomInvariantLifetime<'ll>,
86}
87
88unsafe extern "C" {
89    /// Pointee type of the raw pointer wrapped by [`Value`].
90    type ValueRaw;
91}