| 1 |
//! Helpers for code generation that don't need macro expansion. |
| 2 |
|
| 3 |
use crate::ir::context::BindgenContext; |
| 4 |
use crate::ir::layout::Layout; |
| 5 |
use proc_macro2::{Ident, Span, TokenStream}; |
| 6 |
use quote::TokenStreamExt; |
| 7 |
|
| 8 |
pub(crate) mod attributes { |
| 9 |
use proc_macro2::{Ident, Span, TokenStream}; |
| 10 |
use std::{borrow::Cow, str::FromStr}; |
| 11 |
|
| 12 |
pub(crate) fn repr(which: &str) -> TokenStream { |
| 13 |
let which = Ident::new(which, Span::call_site()); |
| 14 |
quote! { |
| 15 |
#[repr( #which )] |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
pub(crate) fn repr_list(which_ones: &[&str]) -> TokenStream { |
| 20 |
let which_ones = which_ones |
| 21 |
.iter() |
| 22 |
.cloned() |
| 23 |
.map(|one| TokenStream::from_str(one).expect("repr to be valid")); |
| 24 |
quote! { |
| 25 |
#[repr( #( #which_ones ),* )] |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
pub(crate) fn derives(which_ones: &[&str]) -> TokenStream { |
| 30 |
let which_ones = which_ones |
| 31 |
.iter() |
| 32 |
.cloned() |
| 33 |
.map(|one| TokenStream::from_str(one).expect("derive to be valid")); |
| 34 |
quote! { |
| 35 |
#[derive( #( #which_ones ),* )] |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
pub(crate) fn inline() -> TokenStream { |
| 40 |
quote! { |
| 41 |
#[inline] |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
pub(crate) fn must_use() -> TokenStream { |
| 46 |
quote! { |
| 47 |
#[must_use] |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
pub(crate) fn non_exhaustive() -> TokenStream { |
| 52 |
quote! { |
| 53 |
#[non_exhaustive] |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) fn doc(comment: String) -> TokenStream { |
| 58 |
if comment.is_empty() { |
| 59 |
quote!() |
| 60 |
} else { |
| 61 |
quote!(#[doc = #comment]) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub(crate) fn link_name<const MANGLE: bool>(name: &str) -> TokenStream { |
| 66 |
// LLVM mangles the name by default but it's already mangled. |
| 67 |
// Prefixing the name with \u{1} should tell LLVM to not mangle it. |
| 68 |
let name: Cow<'_, str> = if MANGLE { |
| 69 |
name.into() |
| 70 |
} else { |
| 71 |
format!("\u{1}{}", name).into() |
| 72 |
}; |
| 73 |
|
| 74 |
quote! { |
| 75 |
#[link_name = #name] |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// Generates a proper type for a field or type with a given `Layout`, that is, |
| 81 |
/// a type with the correct size and alignment restrictions. |
| 82 |
pub(crate) fn blob(ctx: &BindgenContext, layout: Layout) -> TokenStream { |
| 83 |
let opaque = layout.opaque(); |
| 84 |
|
| 85 |
// FIXME(emilio, #412): We fall back to byte alignment, but there are |
| 86 |
// some things that legitimately are more than 8-byte aligned. |
| 87 |
// |
| 88 |
// Eventually we should be able to `unwrap` here, but... |
| 89 |
let ty_name = match opaque.known_rust_type_for_array(ctx) { |
| 90 |
Some(ty) => ty, |
| 91 |
None => { |
| 92 |
warn!("Found unknown alignment on code generation!"); |
| 93 |
"u8" |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
let ty_name = Ident::new(ty_name, Span::call_site()); |
| 98 |
|
| 99 |
let data_len = opaque.array_size(ctx).unwrap_or(layout.size); |
| 100 |
|
| 101 |
if data_len == 1 { |
| 102 |
quote! { |
| 103 |
#ty_name |
| 104 |
} |
| 105 |
} else { |
| 106 |
quote! { |
| 107 |
[ #ty_name ; #data_len ] |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/// Integer type of the same size as the given `Layout`. |
| 113 |
pub(crate) fn integer_type( |
| 114 |
ctx: &BindgenContext, |
| 115 |
layout: Layout, |
| 116 |
) -> Option<TokenStream> { |
| 117 |
let name = Layout::known_type_for_size(ctx, layout.size)?; |
| 118 |
let name = Ident::new(name, Span::call_site()); |
| 119 |
Some(quote! { #name }) |
| 120 |
} |
| 121 |
|
| 122 |
/// Generates a bitfield allocation unit type for a type with the given `Layout`. |
| 123 |
pub(crate) fn bitfield_unit( |
| 124 |
ctx: &BindgenContext, |
| 125 |
layout: Layout, |
| 126 |
) -> TokenStream { |
| 127 |
let mut tokens = quote! {}; |
| 128 |
|
| 129 |
if ctx.options().enable_cxx_namespaces { |
| 130 |
tokens.append_all(quote! { root:: }); |
| 131 |
} |
| 132 |
|
| 133 |
let size = layout.size; |
| 134 |
tokens.append_all(quote! { |
| 135 |
__BindgenBitfieldUnit<[u8; #size]> |
| 136 |
}); |
| 137 |
|
| 138 |
tokens |
| 139 |
} |
| 140 |
|
| 141 |
pub(crate) mod ast_ty { |
| 142 |
use crate::ir::context::BindgenContext; |
| 143 |
use crate::ir::function::FunctionSig; |
| 144 |
use crate::ir::layout::Layout; |
| 145 |
use crate::ir::ty::FloatKind; |
| 146 |
use proc_macro2::{self, TokenStream}; |
| 147 |
use std::str::FromStr; |
| 148 |
|
| 149 |
pub(crate) fn c_void(ctx: &BindgenContext) -> TokenStream { |
| 150 |
// ctypes_prefix takes precedence |
| 151 |
match ctx.options().ctypes_prefix { |
| 152 |
Some(ref prefix) => { |
| 153 |
let prefix = TokenStream::from_str(prefix.as_str()).unwrap(); |
| 154 |
quote! { |
| 155 |
#prefix::c_void |
| 156 |
} |
| 157 |
} |
| 158 |
None => { |
| 159 |
if ctx.options().use_core && |
| 160 |
ctx.options().rust_features.core_ffi_c_void |
| 161 |
{ |
| 162 |
quote! { ::core::ffi::c_void } |
| 163 |
} else { |
| 164 |
quote! { ::std::os::raw::c_void } |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
pub(crate) fn raw_type(ctx: &BindgenContext, name: &str) -> TokenStream { |
| 171 |
let ident = ctx.rust_ident_raw(name); |
| 172 |
match ctx.options().ctypes_prefix { |
| 173 |
Some(ref prefix) => { |
| 174 |
let prefix = TokenStream::from_str(prefix.as_str()).unwrap(); |
| 175 |
quote! { |
| 176 |
#prefix::#ident |
| 177 |
} |
| 178 |
} |
| 179 |
None => { |
| 180 |
if ctx.options().use_core && |
| 181 |
ctx.options().rust_features().core_ffi_c |
| 182 |
{ |
| 183 |
quote! { |
| 184 |
::core::ffi::#ident |
| 185 |
} |
| 186 |
} else { |
| 187 |
quote! { |
| 188 |
::std::os::raw::#ident |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
pub(crate) fn float_kind_rust_type( |
| 196 |
ctx: &BindgenContext, |
| 197 |
fk: FloatKind, |
| 198 |
layout: Option<Layout>, |
| 199 |
) -> TokenStream { |
| 200 |
// TODO: we probably should take the type layout into account more |
| 201 |
// often? |
| 202 |
// |
| 203 |
// Also, maybe this one shouldn't be the default? |
| 204 |
match (fk, ctx.options().convert_floats) { |
| 205 |
(FloatKind::Float, true) => quote! { f32 }, |
| 206 |
(FloatKind::Double, true) => quote! { f64 }, |
| 207 |
(FloatKind::Float, false) => raw_type(ctx, "c_float"), |
| 208 |
(FloatKind::Double, false) => raw_type(ctx, "c_double"), |
| 209 |
(FloatKind::LongDouble, _) => { |
| 210 |
match layout { |
| 211 |
Some(layout) => { |
| 212 |
match layout.size { |
| 213 |
4 => quote! { f32 }, |
| 214 |
8 => quote! { f64 }, |
| 215 |
// TODO(emilio): If rust ever gains f128 we should |
| 216 |
// use it here and below. |
| 217 |
_ => super::integer_type(ctx, layout) |
| 218 |
.unwrap_or(quote! { f64 }), |
| 219 |
} |
| 220 |
} |
| 221 |
None => { |
| 222 |
debug_assert!( |
| 223 |
false, |
| 224 |
"How didn't we know the layout for a primitive type?" |
| 225 |
); |
| 226 |
quote! { f64 } |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
(FloatKind::Float128, _) => { |
| 231 |
if ctx.options().rust_features.i128_and_u128 { |
| 232 |
quote! { u128 } |
| 233 |
} else { |
| 234 |
quote! { [u64; 2] } |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
pub(crate) fn int_expr(val: i64) -> TokenStream { |
| 241 |
// Don't use quote! { #val } because that adds the type suffix. |
| 242 |
let val = proc_macro2::Literal::i64_unsuffixed(val); |
| 243 |
quote!(#val) |
| 244 |
} |
| 245 |
|
| 246 |
pub(crate) fn uint_expr(val: u64) -> TokenStream { |
| 247 |
// Don't use quote! { #val } because that adds the type suffix. |
| 248 |
let val = proc_macro2::Literal::u64_unsuffixed(val); |
| 249 |
quote!(#val) |
| 250 |
} |
| 251 |
|
| 252 |
pub(crate) fn byte_array_expr(bytes: &[u8]) -> TokenStream { |
| 253 |
let mut bytes: Vec<_> = bytes.to_vec(); |
| 254 |
bytes.push(0); |
| 255 |
quote! { [ #(#bytes),* ] } |
| 256 |
} |
| 257 |
|
| 258 |
pub(crate) fn cstr_expr(mut string: String) -> TokenStream { |
| 259 |
string.push('\0'); |
| 260 |
let b = proc_macro2::Literal::byte_string(string.as_bytes()); |
| 261 |
quote! { |
| 262 |
#b |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
pub(crate) fn float_expr( |
| 267 |
ctx: &BindgenContext, |
| 268 |
f: f64, |
| 269 |
) -> Result<TokenStream, ()> { |
| 270 |
if f.is_finite() { |
| 271 |
let val = proc_macro2::Literal::f64_unsuffixed(f); |
| 272 |
|
| 273 |
return Ok(quote!(#val)); |
| 274 |
} |
| 275 |
|
| 276 |
let prefix = ctx.trait_prefix(); |
| 277 |
|
| 278 |
if f.is_nan() { |
| 279 |
return Ok(quote! { |
| 280 |
::#prefix::f64::NAN |
| 281 |
}); |
| 282 |
} |
| 283 |
|
| 284 |
if f.is_infinite() { |
| 285 |
return Ok(if f.is_sign_positive() { |
| 286 |
quote! { |
| 287 |
::#prefix::f64::INFINITY |
| 288 |
} |
| 289 |
} else { |
| 290 |
quote! { |
| 291 |
::#prefix::f64::NEG_INFINITY |
| 292 |
} |
| 293 |
}); |
| 294 |
} |
| 295 |
|
| 296 |
warn!("Unknown non-finite float number: {:?}", f); |
| 297 |
Err(()) |
| 298 |
} |
| 299 |
|
| 300 |
pub(crate) fn arguments_from_signature( |
| 301 |
signature: &FunctionSig, |
| 302 |
ctx: &BindgenContext, |
| 303 |
) -> Vec<TokenStream> { |
| 304 |
let mut unnamed_arguments = 0; |
| 305 |
signature |
| 306 |
.argument_types() |
| 307 |
.iter() |
| 308 |
.map(|&(ref name, _ty)| match *name { |
| 309 |
Some(ref name) => { |
| 310 |
let name = ctx.rust_ident(name); |
| 311 |
quote! { #name } |
| 312 |
} |
| 313 |
None => { |
| 314 |
unnamed_arguments += 1; |
| 315 |
let name = |
| 316 |
ctx.rust_ident(format!("arg{}", unnamed_arguments)); |
| 317 |
quote! { #name } |
| 318 |
} |
| 319 |
}) |
| 320 |
.collect() |
| 321 |
} |
| 322 |
} |