1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
//! Helpers for code generation that don't need macro expansion.
use proc_macro2::{Ident, Span};
use crate::ir::context::BindgenContext;
use crate::ir::layout::Layout;
pub(crate) mod attributes {
use proc_macro2::{Ident, Span, TokenStream};
use std::{borrow::Cow, str::FromStr};
pub(crate) fn repr(which: &str) -> TokenStream {
let which = Ident::new(which, Span::call_site());
quote! {
#[repr( #which )]
}
}
pub(crate) fn repr_list(which_ones: &[&str]) -> TokenStream {
let which_ones = which_ones
.iter()
.map(|one| TokenStream::from_str(one).expect("repr to be valid"));
quote! {
#[repr( #( #which_ones ),* )]
}
}
pub(crate) fn derives(which_ones: &[&str]) -> TokenStream {
let which_ones = which_ones
.iter()
.map(|one| TokenStream::from_str(one).expect("derive to be valid"));
quote! {
#[derive( #( #which_ones ),* )]
}
}
pub(crate) fn inline() -> TokenStream {
quote! {
#[inline]
}
}
pub(crate) fn must_use() -> TokenStream {
quote! {
#[must_use]
}
}
pub(crate) fn non_exhaustive() -> TokenStream {
quote! {
#[non_exhaustive]
}
}
pub(crate) fn doc(comment: &str) -> TokenStream {
if comment.is_empty() {
quote!()
} else {
quote!(#[doc = #comment])
}
}
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]
}
}
}
/// The `ffi_safe` argument should be true if this is a type that the user might
/// reasonably use, e.g. not struct padding, where the `__BindgenOpaqueArray` is
/// just noise.
/// TODO: Should this be `MaybeUninit`, since padding bytes are effectively
/// uninitialized?
pub(crate) fn blob(
ctx: &BindgenContext,
layout: Layout,
ffi_safe: bool,
) -> 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 = opaque.known_rust_type_for_array().unwrap_or_else(|| {
warn!("Found unknown alignment on code generation!");
syn::parse_quote! { u8 }
});
let data_len = opaque.array_size().unwrap_or(layout.size);
if data_len == 1 {
ty
} else if ffi_safe && ctx.options().rust_features().min_const_generics {
ctx.generated_opaque_array();
if ctx.options().enable_cxx_namespaces {
syn::parse_quote! { root::__BindgenOpaqueArray<#ty, #data_len> }
} else {
syn::parse_quote! { __BindgenOpaqueArray<#ty, #data_len> }
}
} else {
// This is not FFI safe as an argument; the struct above is
// preferable.
syn::parse_quote! { [ #ty ; #data_len ] }
}
}
/// Integer type of the same size as the given `Layout`.
pub(crate) fn integer_type(layout: Layout) -> Option<syn::Type> {
Layout::known_type_for_size(layout.size)
}
pub(crate) const BITFIELD_UNIT: &str = "__BindgenBitfieldUnit";
/// 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 bitfield_unit_name = Ident::new(BITFIELD_UNIT, Span::call_site());
let ty = syn::parse_quote! { #bitfield_unit_name<[u8; #size]> };
if ctx.options().enable_cxx_namespaces {
return syn::parse_quote! { root::#ty };
}
ty
}
pub(crate) mod ast_ty {
use crate::ir::context::BindgenContext;
use crate::ir::function::FunctionSig;
use crate::ir::layout::Layout;
use crate::ir::ty::{FloatKind, IntKind};
use crate::RustTarget;
use proc_macro2::TokenStream;
use std::str::FromStr;
pub(crate) fn c_void(ctx: &BindgenContext) -> syn::Type {
// ctypes_prefix takes precedence
match ctx.options().ctypes_prefix {
Some(ref prefix) => {
let prefix = TokenStream::from_str(prefix.as_str()).unwrap();
syn::parse_quote! { #prefix::c_void }
}
None => {
if ctx.options().use_core {
syn::parse_quote! { ::core::ffi::c_void }
} else {
syn::parse_quote! { ::std::os::raw::c_void }
}
}
}
}
pub(crate) fn raw_type(ctx: &BindgenContext, name: &str) -> syn::Type {
let ident = ctx.rust_ident_raw(name);
match ctx.options().ctypes_prefix {
Some(ref prefix) => {
let prefix = TokenStream::from_str(prefix.as_str()).unwrap();
syn::parse_quote! { #prefix::#ident }
}
None => {
if ctx.options().use_core &&
ctx.options().rust_features().core_ffi_c
{
syn::parse_quote! { ::core::ffi::#ident }
} else {
syn::parse_quote! { ::std::os::raw::#ident }
}
}
}
}
pub(crate) fn int_kind_rust_type(
ctx: &BindgenContext,
ik: IntKind,
layout: Option<Layout>,
) -> syn::Type {
match ik {
IntKind::Bool => syn::parse_quote! { bool },
IntKind::Char { .. } => raw_type(ctx, "c_char"),
// The following is used only when an unusual command-line
// argument is used. bindgen_cchar16_t is not a real type;
// but this allows downstream postprocessors to distinguish
// this case and do something special for C++ bindings
// containing the C++ type char16_t.
IntKind::Char16 => syn::parse_quote! { bindgen_cchar16_t },
IntKind::SChar => raw_type(ctx, "c_schar"),
IntKind::UChar => raw_type(ctx, "c_uchar"),
IntKind::Short => raw_type(ctx, "c_short"),
IntKind::UShort => raw_type(ctx, "c_ushort"),
IntKind::Int => raw_type(ctx, "c_int"),
IntKind::UInt => raw_type(ctx, "c_uint"),
IntKind::Long => raw_type(ctx, "c_long"),
IntKind::ULong => raw_type(ctx, "c_ulong"),
IntKind::LongLong => raw_type(ctx, "c_longlong"),
IntKind::ULongLong => raw_type(ctx, "c_ulonglong"),
IntKind::WChar => {
let layout =
layout.expect("Couldn't compute wchar_t's layout?");
Layout::known_type_for_size(layout.size)
.expect("Non-representable wchar_t?")
}
IntKind::I8 => syn::parse_quote! { i8 },
IntKind::U8 => syn::parse_quote! { u8 },
IntKind::I16 => syn::parse_quote! { i16 },
IntKind::U16 => syn::parse_quote! { u16 },
IntKind::I32 => syn::parse_quote! { i32 },
IntKind::U32 => syn::parse_quote! { u32 },
IntKind::I64 => syn::parse_quote! { i64 },
IntKind::U64 => syn::parse_quote! { u64 },
IntKind::Custom { name, .. } => {
syn::parse_str(name).expect("Invalid integer type.")
}
IntKind::U128 => {
if true {
syn::parse_quote! { u128 }
} else {
// Best effort thing, but wrong alignment
// unfortunately.
syn::parse_quote! { [u64; 2] }
}
}
IntKind::I128 => {
if true {
syn::parse_quote! { i128 }
} else {
syn::parse_quote! { [u64; 2] }
}
}
}
}
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, _) => {
if let Some(layout) = 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(layout)
.unwrap_or(syn::parse_quote! { f64 }),
}
} else {
debug_assert!(
false,
"How didn't we know the layout for a primitive type?"
);
syn::parse_quote! { f64 }
}
}
(FloatKind::Float128, _) => {
if true {
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();
let rust_target = ctx.options().rust_target;
if f.is_nan() {
// FIXME: This should be done behind a `RustFeature` instead
#[allow(deprecated)]
let tokens = if rust_target >= RustTarget::Stable_1_43 {
quote! {
f64::NAN
}
} else {
quote! {
::#prefix::f64::NAN
}
};
return Ok(tokens);
}
if f.is_infinite() {
let tokens = if f.is_sign_positive() {
// FIXME: This should be done behind a `RustFeature` instead
#[allow(deprecated)]
if rust_target >= RustTarget::Stable_1_43 {
quote! {
f64::INFINITY
}
} else {
quote! {
::#prefix::f64::INFINITY
}
}
} else {
// FIXME: This should be done behind a `RustFeature` instead
#[allow(deprecated)]
// Negative infinity
if rust_target >= RustTarget::Stable_1_43 {
quote! {
f64::NEG_INFINITY
}
} else {
quote! {
::#prefix::f64::NEG_INFINITY
}
}
};
return Ok(tokens);
}
warn!("Unknown non-finite float number: {f:?}");
Err(())
}
pub(crate) fn arguments_from_signature(
signature: &FunctionSig,
ctx: &BindgenContext,
) -> Vec<TokenStream> {
let mut unnamed_arguments = 0;
signature
.argument_types()
.iter()
.map(|&(ref name, _ty)| {
let name = if let Some(ref name) = *name {
ctx.rust_ident(name)
} else {
unnamed_arguments += 1;
ctx.rust_ident(format!("arg{unnamed_arguments}"))
};
quote! { #name }
})
.collect()
}
}
|