pub(crate) fn link_name<const MANGLE: bool>(name: &str) -> TokenStream { // LLVM mangles the name by default but it's already mangled. // Prefixing the name with \u{1} should tell LLVM to not mangle it. let name: Cow<'_, str> = if MANGLE {
name.into()
} else {
format!("\u{1}{}", name).into()
};
quote! { #[link_name = #name]
}
}
}
/// Generates a proper type for a field or type with a given `Layout`, that is, /// a type with the correct size and alignment restrictions. pub(crate) fn blob(ctx: &BindgenContext, layout: Layout) -> syn::Type { let opaque = layout.opaque();
// FIXME(emilio, #412): We fall back to byte alignment, but there are // some things that legitimately are more than 8-byte aligned. // // Eventually we should be able to `unwrap` here, but... let ty = match opaque.known_rust_type_for_array(ctx) {
Some(ty) => ty,
None => {
warn!("Found unknown alignment on code generation!");
syn::parse_quote! { u8 }
}
};
let data_len = opaque.array_size(ctx).unwrap_or(layout.size);
if data_len == 1 {
ty
} else {
syn::parse_quote! { [ #ty ; #data_len ] }
}
}
/// Integer type of the same size as the given `Layout`. pub(crate) fn integer_type(
ctx: &BindgenContext,
layout: Layout,
) -> Option<syn::Type> {
Layout::known_type_for_size(ctx, layout.size)
}
/// Generates a bitfield allocation unit type for a type with the given `Layout`. pub(crate) fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> syn::Type { let size = layout.size; let ty = syn::parse_quote! { __BindgenBitfieldUnit<[u8; #size]> };
if ctx.options().enable_cxx_namespaces { return syn::parse_quote! { root::#ty };
}
ty
}
pub(crate) mod ast_ty { usecrate::ir::context::BindgenContext; usecrate::ir::function::FunctionSig; usecrate::ir::layout::Layout; usecrate::ir::ty::{FloatKind, IntKind}; use proc_macro2::{self, TokenStream}; use std::str::FromStr;
pub(crate) fn float_kind_rust_type(
ctx: &BindgenContext,
fk: FloatKind,
layout: Option<Layout>,
) -> syn::Type { // TODO: we probably should take the type layout into account more // often? // // Also, maybe this one shouldn't be the default? match (fk, ctx.options().convert_floats) {
(FloatKind::Float16, _) => { // TODO: do f16 when rust lands it
ctx.generated_bindgen_float16(); if ctx.options().enable_cxx_namespaces {
syn::parse_quote! { root::__BindgenFloat16 }
} else {
syn::parse_quote! { __BindgenFloat16 }
}
}
(FloatKind::Float, true) => syn::parse_quote! { f32 },
(FloatKind::Double, true) => syn::parse_quote! { f64 },
(FloatKind::Float, false) => raw_type(ctx, "c_float"),
(FloatKind::Double, false) => raw_type(ctx, "c_double"),
(FloatKind::LongDouble, _) => { match layout {
Some(layout) => { match layout.size { 4 => syn::parse_quote! { f32 }, 8 => syn::parse_quote! { f64 }, // TODO(emilio): If rust ever gains f128 we should // use it here and below.
_ => super::integer_type(ctx, layout)
.unwrap_or(syn::parse_quote! { f64 }),
}
}
None => {
debug_assert!( false, "How didn't we know the layout for a primitive type?"
);
syn::parse_quote! { f64 }
}
}
}
(FloatKind::Float128, _) => { if ctx.options().rust_features.i128_and_u128 {
syn::parse_quote! { u128 }
} else {
syn::parse_quote! { [u64; 2] }
}
}
}
}
pub(crate) fn int_expr(val: i64) -> TokenStream { // Don't use quote! { #val } because that adds the type suffix. let val = proc_macro2::Literal::i64_unsuffixed(val);
quote!(#val)
}
pub(crate) fn uint_expr(val: u64) -> TokenStream { // Don't use quote! { #val } because that adds the type suffix. let val = proc_macro2::Literal::u64_unsuffixed(val);
quote!(#val)
}
pub(crate) fn cstr_expr(mut string: String) -> TokenStream {
string.push('\0'); let b = proc_macro2::Literal::byte_string(string.as_bytes());
quote! { #b
}
}
pub(crate) fn float_expr(
ctx: &BindgenContext,
f: f64,
) -> Result<TokenStream, ()> { if f.is_finite() { let val = proc_macro2::Literal::f64_unsuffixed(f);
return Ok(quote!(#val));
}
let prefix = ctx.trait_prefix();
if f.is_nan() { return Ok(quote! {
::#prefix::f64::NAN
});
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.