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::{fmt, 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#[cfg_attr(bootstrap, rustc_pass_by_value)]
13#[cfg_attr(not(bootstrap), lint::pass_by_value)]
14pub(crate) struct Context<'ll> {
15    llcx_raw: ptr::NonNull<ContextRaw>,
16    _invariant_ll: PhantomInvariantLifetime<'ll>,
17}
18
19/// Corresponds to `LLVMModuleRef`.
20#[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)]
21#[repr(transparent)]
22#[cfg_attr(bootstrap, rustc_pass_by_value)]
23#[cfg_attr(not(bootstrap), lint::pass_by_value)]
24pub(crate) struct Module<'ll> {
25    llmod_raw: ptr::NonNull<ModuleRaw>,
26    _invariant_ll: PhantomInvariantLifetime<'ll>,
27}
28
29impl<'ll> Module<'ll> {
30    pub(crate) fn llmod_raw(self) -> ptr::NonNull<ModuleRaw> {
31        self.llmod_raw
32    }
33}
34
35unsafe extern "C" {
36    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Context`].
37    pub(crate) type ContextRaw;
38    /// Pointee of the raw pointer that is wrapped by a lifetime-branded [`Module`].
39    pub(crate) type ModuleRaw;
40}
41
42/// Converts an unbranded pointer-to-context to a lifetime-branded [`Context`].
43///
44/// # Safety
45/// - The pointer must be a valid LLVM context.
46/// - If the context is passed to safe code, its caller-requested lifetime must be distinct
47///   from that of any other context, and must not outlive the underlying context.
48pub(crate) unsafe fn wrap_llcx<'ll>(llcx_raw: ptr::NonNull<ContextRaw>) -> Context<'ll> {
49    Context { llcx_raw, _invariant_ll: PhantomData }
50}
51
52/// Converts an unbranded pointer-to-module to a lifetime-branded [`Module`].
53///
54/// # Safety
55/// - The pointer must be a valid LLVM module within the specified context.
56/// - The module must not be disposed during the lifetime of its context.
57pub(crate) unsafe fn wrap_llmod<'ll>(
58    _llcx: Context<'ll>, // The context constrains `'ll` but isn't actually used.
59    llmod_raw: ptr::NonNull<ModuleRaw>,
60) -> Module<'ll> {
61    Module { llmod_raw, _invariant_ll: PhantomData }
62}
63
64/// Corresponds to `LLVMTypeRef`.
65///
66/// LLVM types are interned within their LLVM context ([`Context<'ll>`]), so they
67/// can be compared for equality (and hashed) based on their pointer address.
68#[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)]
69#[repr(transparent)]
70#[cfg_attr(bootstrap, rustc_pass_by_value)]
71#[cfg_attr(not(bootstrap), lint::pass_by_value)]
72pub(crate) struct Type<'ll> {
73    llty_raw: ptr::NonNull<TypeRaw>,
74    _invariant_ll: PhantomInvariantLifetime<'ll>,
75}
76
77unsafe extern "C" {
78    /// Pointee type of the raw pointer wrapped by [`Type`].
79    type TypeRaw;
80}
81
82#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for FunctionSignature<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for
    FunctionSignature<'ll> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Type<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for
    FunctionSignature<'ll> {
}Copy, #[automatically_derived]impl<'ll> ::core::marker::StructuralPartialEq for
    FunctionSignature<'ll> {
}
#[automatically_derived]impl<'ll> ::core::cmp::PartialEq for
    FunctionSignature<'ll> {
    #[inline]
    fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]impl<'ll> ::core::cmp::Eq for FunctionSignature<'ll> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Type<'ll>>;
    }
}Eq, #[automatically_derived]impl<'ll> ::core::hash::Hash for
    FunctionSignature<'ll> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash)]
83#[repr(transparent)]
84#[cfg_attr(bootstrap, rustc_pass_by_value)]
85#[cfg_attr(not(bootstrap), lint::pass_by_value)]
86pub(crate) struct FunctionSignature<'ll>(Type<'ll>);
87
88impl<'ll> FunctionSignature<'ll> {
89    pub(crate) fn from_type(ty: Type<'ll>) -> Option<Self> {
90        if super::LLVMGetTypeKind(ty).to_rust() == super::TypeKind::Function {
91            Some(Self(ty))
92        } else {
93            None
94        }
95    }
96}
97
98impl fmt::Debug for FunctionSignature<'_> {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        fmt::Debug::fmt(&self.0, f)
101    }
102}
103
104/// Corresponds to `LLVMValueRef`.
105#[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)]
106#[repr(transparent)]
107#[cfg_attr(bootstrap, rustc_pass_by_value)]
108#[cfg_attr(not(bootstrap), lint::pass_by_value)]
109pub(crate) struct Value<'ll> {
110    llval_raw: ptr::NonNull<ValueRaw>,
111    _invariant_ll: PhantomInvariantLifetime<'ll>,
112}
113
114unsafe extern "C" {
115    /// Pointee type of the raw pointer wrapped by [`Value`].
116    type ValueRaw;
117}
118
119/// Corresponds to `LLVMAttributeRef`.
120#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'ll>
    ::core::clone::TrivialClone for Attribute<'ll> {
}
#[automatically_derived]impl<'ll> ::core::clone::Clone for Attribute<'ll> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<ptr::NonNull<AttributeRaw>>;
        let _:
                ::core::clone::AssertParamIsClone<PhantomInvariantLifetime<'ll>>;
        *self
    }
}Clone, #[automatically_derived]impl<'ll> ::core::marker::Copy for Attribute<'ll> { }Copy)]
121#[repr(transparent)]
122#[cfg_attr(bootstrap, rustc_pass_by_value)]
123#[cfg_attr(not(bootstrap), lint::pass_by_value)]
124pub(crate) struct Attribute<'ll> {
125    llattr_raw: ptr::NonNull<AttributeRaw>,
126    _invariant_ll: PhantomInvariantLifetime<'ll>,
127}
128
129unsafe extern "C" {
130    /// Pointee type of the raw pointer wrapped by [`Attribute`].
131    type AttributeRaw;
132}