Skip to content

Commit 27f8b73

Browse files
committed
chore: allow both wasm_import and raw-dylib
1 parent a23b050 commit 27f8b73

File tree

1 file changed

+111
-109
lines changed

1 file changed

+111
-109
lines changed

bindgen/codegen/mod.rs

Lines changed: 111 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -429,21 +429,21 @@ impl WithImplicitTemplateParams for syn::Type {
429429
unreachable!("we resolved item through type refs")
430430
}
431431
// None of these types ever have implicit template parameters.
432-
TypeKind::Void |
433-
TypeKind::NullPtr |
434-
TypeKind::Pointer(..) |
435-
TypeKind::Reference(..) |
436-
TypeKind::Int(..) |
437-
TypeKind::Float(..) |
438-
TypeKind::Complex(..) |
439-
TypeKind::Array(..) |
440-
TypeKind::TypeParam |
441-
TypeKind::Opaque |
442-
TypeKind::Function(..) |
443-
TypeKind::Enum(..) |
444-
TypeKind::ObjCId |
445-
TypeKind::ObjCSel |
446-
TypeKind::TemplateInstantiation(..) => None,
432+
TypeKind::Void
433+
| TypeKind::NullPtr
434+
| TypeKind::Pointer(..)
435+
| TypeKind::Reference(..)
436+
| TypeKind::Int(..)
437+
| TypeKind::Float(..)
438+
| TypeKind::Complex(..)
439+
| TypeKind::Array(..)
440+
| TypeKind::TypeParam
441+
| TypeKind::Opaque
442+
| TypeKind::Function(..)
443+
| TypeKind::Enum(..)
444+
| TypeKind::ObjCId
445+
| TypeKind::ObjCSel
446+
| TypeKind::TemplateInstantiation(..) => None,
447447
_ => {
448448
let params = item.used_template_params(ctx);
449449
if params.is_empty() {
@@ -590,9 +590,9 @@ impl CodeGenerator for Module {
590590
}
591591
};
592592

593-
if !ctx.options().enable_cxx_namespaces ||
594-
(self.is_inline() &&
595-
!ctx.options().conservative_inline_namespaces)
593+
if !ctx.options().enable_cxx_namespaces
594+
|| (self.is_inline()
595+
&& !ctx.options().conservative_inline_namespaces)
596596
{
597597
codegen_self(result, &mut false);
598598
return;
@@ -836,19 +836,19 @@ impl CodeGenerator for Type {
836836
debug_assert!(item.is_enabled_for_codegen(ctx));
837837

838838
match *self.kind() {
839-
TypeKind::Void |
840-
TypeKind::NullPtr |
841-
TypeKind::Int(..) |
842-
TypeKind::Float(..) |
843-
TypeKind::Complex(..) |
844-
TypeKind::Array(..) |
845-
TypeKind::Vector(..) |
846-
TypeKind::Pointer(..) |
847-
TypeKind::Reference(..) |
848-
TypeKind::Function(..) |
849-
TypeKind::ResolvedTypeRef(..) |
850-
TypeKind::Opaque |
851-
TypeKind::TypeParam => {
839+
TypeKind::Void
840+
| TypeKind::NullPtr
841+
| TypeKind::Int(..)
842+
| TypeKind::Float(..)
843+
| TypeKind::Complex(..)
844+
| TypeKind::Array(..)
845+
| TypeKind::Vector(..)
846+
| TypeKind::Pointer(..)
847+
| TypeKind::Reference(..)
848+
| TypeKind::Function(..)
849+
| TypeKind::ResolvedTypeRef(..)
850+
| TypeKind::Opaque
851+
| TypeKind::TypeParam => {
852852
// These items don't need code generation, they only need to be
853853
// converted to rust types in fields, arguments, and such.
854854
// NOTE(emilio): If you add to this list, make sure to also add
@@ -1004,11 +1004,11 @@ impl CodeGenerator for Type {
10041004

10051005
// We prefer using `pub use` over `pub type` because of:
10061006
// https://github.com/rust-lang/rust/issues/26264
1007-
if matches!(inner_rust_type, syn::Type::Path(_)) &&
1008-
outer_params.is_empty() &&
1009-
!is_opaque &&
1010-
alias_style == AliasVariation::TypeAlias &&
1011-
inner_item.expect_type().canonical_type(ctx).is_enum()
1007+
if matches!(inner_rust_type, syn::Type::Path(_))
1008+
&& outer_params.is_empty()
1009+
&& !is_opaque
1010+
&& alias_style == AliasVariation::TypeAlias
1011+
&& inner_item.expect_type().canonical_type(ctx).is_enum()
10121012
{
10131013
tokens.append_all(quote! {
10141014
pub use
@@ -1185,9 +1185,9 @@ impl CodeGenerator for Vtable<'_> {
11851185
// For now, we will only generate vtables for classes that:
11861186
// - do not inherit from others (compilers merge VTable from primary parent class).
11871187
// - do not contain a virtual destructor (requires ordering; platforms generate different vtables).
1188-
if ctx.options().vtable_generation &&
1189-
self.comp_info.base_members().is_empty() &&
1190-
self.comp_info.destructor().is_none()
1188+
if ctx.options().vtable_generation
1189+
&& self.comp_info.base_members().is_empty()
1190+
&& self.comp_info.destructor().is_none()
11911191
{
11921192
let class_ident = ctx.rust_ident(self.item_id.canonical_name(ctx));
11931193

@@ -2367,10 +2367,10 @@ impl CodeGenerator for CompInfo {
23672367

23682368
// if a type has both a "packed" attribute and an "align(N)" attribute, then check if the
23692369
// "packed" attr is redundant, and do not include it if so.
2370-
if packed &&
2371-
!is_opaque &&
2372-
!(explicit_align.is_some() &&
2373-
self.already_packed(ctx).unwrap_or(false))
2370+
if packed
2371+
&& !is_opaque
2372+
&& !(explicit_align.is_some()
2373+
&& self.already_packed(ctx).unwrap_or(false))
23742374
{
23752375
let n = layout.map_or(1, |l| l.align);
23762376
let packed_repr = if n == 1 {
@@ -2410,32 +2410,32 @@ impl CodeGenerator for CompInfo {
24102410

24112411
let derivable_traits = derives_of_item(item, ctx, packed);
24122412
if !derivable_traits.contains(DerivableTraits::DEBUG) {
2413-
needs_debug_impl = ctx.options().derive_debug &&
2414-
ctx.options().impl_debug &&
2415-
!ctx.no_debug_by_name(item) &&
2416-
!item.annotations().disallow_debug();
2413+
needs_debug_impl = ctx.options().derive_debug
2414+
&& ctx.options().impl_debug
2415+
&& !ctx.no_debug_by_name(item)
2416+
&& !item.annotations().disallow_debug();
24172417
}
24182418

24192419
if !derivable_traits.contains(DerivableTraits::DEFAULT) {
2420-
needs_default_impl = ctx.options().derive_default &&
2421-
!self.is_forward_declaration() &&
2422-
!ctx.no_default_by_name(item) &&
2423-
!item.annotations().disallow_default();
2420+
needs_default_impl = ctx.options().derive_default
2421+
&& !self.is_forward_declaration()
2422+
&& !ctx.no_default_by_name(item)
2423+
&& !item.annotations().disallow_default();
24242424
}
24252425

24262426
let all_template_params = item.all_template_params(ctx);
24272427

2428-
if derivable_traits.contains(DerivableTraits::COPY) &&
2429-
!derivable_traits.contains(DerivableTraits::CLONE)
2428+
if derivable_traits.contains(DerivableTraits::COPY)
2429+
&& !derivable_traits.contains(DerivableTraits::CLONE)
24302430
{
24312431
needs_clone_impl = true;
24322432
}
24332433

24342434
if !derivable_traits.contains(DerivableTraits::PARTIAL_EQ) {
2435-
needs_partialeq_impl = ctx.options().derive_partialeq &&
2436-
ctx.options().impl_partialeq &&
2437-
ctx.lookup_can_derive_partialeq_or_partialord(item.id()) ==
2438-
CanDerive::Manually;
2435+
needs_partialeq_impl = ctx.options().derive_partialeq
2436+
&& ctx.options().impl_partialeq
2437+
&& ctx.lookup_can_derive_partialeq_or_partialord(item.id())
2438+
== CanDerive::Manually;
24392439
}
24402440

24412441
let mut derives: Vec<_> = derivable_traits.into();
@@ -2617,8 +2617,8 @@ impl CodeGenerator for CompInfo {
26172617
.collect()
26182618
};
26192619

2620-
let uninit_decl = if check_field_offset.is_empty() ||
2621-
compile_time
2620+
let uninit_decl = if check_field_offset.is_empty()
2621+
|| compile_time
26222622
{
26232623
None
26242624
} else {
@@ -2954,11 +2954,11 @@ impl Method {
29542954
let cc = &ctx.options().codegen_config;
29552955
match self.kind() {
29562956
MethodKind::Constructor => cc.constructors(),
2957-
MethodKind::Destructor |
2958-
MethodKind::VirtualDestructor { .. } => cc.destructors(),
2959-
MethodKind::Static |
2960-
MethodKind::Normal |
2961-
MethodKind::Virtual { .. } => cc.methods(),
2957+
MethodKind::Destructor
2958+
| MethodKind::VirtualDestructor { .. } => cc.destructors(),
2959+
MethodKind::Static
2960+
| MethodKind::Normal
2961+
| MethodKind::Virtual { .. } => cc.methods(),
29622962
}
29632963
});
29642964

@@ -3459,8 +3459,8 @@ impl EnumBuilder {
34593459
}
34603460
variants
34613461
}
3462-
EnumBuilderKind::Consts { .. } |
3463-
EnumBuilderKind::ModuleConsts { .. } => {
3462+
EnumBuilderKind::Consts { .. }
3463+
| EnumBuilderKind::ModuleConsts { .. } => {
34643464
let mut variants = vec![];
34653465

34663466
for v in self.enum_variants {
@@ -3585,8 +3585,8 @@ impl CodeGenerator for Enum {
35853585
let repr_translated;
35863586
let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) {
35873587
Some(repr)
3588-
if !ctx.options().translate_enum_integer_types &&
3589-
!variation.is_rust() =>
3588+
if !ctx.options().translate_enum_integer_types
3589+
&& !variation.is_rust() =>
35903590
{
35913591
repr
35923592
}
@@ -3657,10 +3657,10 @@ impl CodeGenerator for Enum {
36573657
// Clone/Eq/PartialEq/Hash, even if we don't generate those by
36583658
// default.
36593659
derives.insert(
3660-
DerivableTraits::CLONE |
3661-
DerivableTraits::HASH |
3662-
DerivableTraits::PARTIAL_EQ |
3663-
DerivableTraits::EQ,
3660+
DerivableTraits::CLONE
3661+
| DerivableTraits::HASH
3662+
| DerivableTraits::PARTIAL_EQ
3663+
| DerivableTraits::EQ,
36643664
);
36653665
let mut derives: Vec<_> = derives.into();
36663666
for derive in item.annotations().derives() {
@@ -3801,8 +3801,8 @@ impl CodeGenerator for Enum {
38013801
Entry::Occupied(ref entry) => {
38023802
if variation.is_rust() {
38033803
let variant_name = ctx.rust_mangle(variant.name());
3804-
let mangled_name = if is_toplevel ||
3805-
enum_ty.name().is_some()
3804+
let mangled_name = if is_toplevel
3805+
|| enum_ty.name().is_some()
38063806
{
38073807
variant_name
38083808
} else {
@@ -3861,8 +3861,8 @@ impl CodeGenerator for Enum {
38613861
// If it's an unnamed enum, or constification is enforced,
38623862
// we also generate a constant so it can be properly
38633863
// accessed.
3864-
if (variation.is_rust() && enum_ty.name().is_none()) ||
3865-
variant.force_constification()
3864+
if (variation.is_rust() && enum_ty.name().is_none())
3865+
|| variant.force_constification()
38663866
{
38673867
let mangled_name = if is_toplevel {
38683868
variant_name.clone()
@@ -4305,16 +4305,17 @@ impl TryToRustTy for Type {
43054305
inst.try_to_rust_ty(ctx, item)
43064306
}
43074307
TypeKind::ResolvedTypeRef(inner) => inner.try_to_rust_ty(ctx, &()),
4308-
TypeKind::TemplateAlias(..) |
4309-
TypeKind::Alias(..) |
4310-
TypeKind::BlockPointer(..) => {
4308+
TypeKind::TemplateAlias(..)
4309+
| TypeKind::Alias(..)
4310+
| TypeKind::BlockPointer(..) => {
43114311
if self.is_block_pointer() && !ctx.options().generate_block {
43124312
let void = c_void(ctx);
43134313
return Ok(void.to_ptr(/* is_const = */ false));
43144314
}
43154315

4316-
if item.is_opaque(ctx, &()) &&
4317-
item.used_template_params(ctx)
4316+
if item.is_opaque(ctx, &())
4317+
&& item
4318+
.used_template_params(ctx)
43184319
.into_iter()
43194320
.any(|param| param.is_template_param(ctx, &()))
43204321
{
@@ -4330,8 +4331,8 @@ impl TryToRustTy for Type {
43304331
}
43314332
TypeKind::Comp(ref info) => {
43324333
let template_params = item.all_template_params(ctx);
4333-
if info.has_non_type_template_params() ||
4334-
(item.is_opaque(ctx, &()) && !template_params.is_empty())
4334+
if info.has_non_type_template_params()
4335+
|| (item.is_opaque(ctx, &()) && !template_params.is_empty())
43354336
{
43364337
return self.try_to_opaque(ctx, item);
43374338
}
@@ -4371,8 +4372,8 @@ impl TryToRustTy for Type {
43714372
if inner_ty.canonical_type(ctx).is_function() || is_objc_pointer
43724373
{
43734374
Ok(ty)
4374-
} else if ctx.options().generate_cxx_nonnull_references &&
4375-
matches!(self.kind(), TypeKind::Reference(_))
4375+
} else if ctx.options().generate_cxx_nonnull_references
4376+
&& matches!(self.kind(), TypeKind::Reference(_))
43764377
{
43774378
// It's UB to pass null values in place of C++ references
43784379
let prefix = ctx.trait_prefix();
@@ -4668,28 +4669,29 @@ impl CodeGenerator for Function {
46684669
// Unfortunately this can't piggyback on the `attributes` list because
46694670
// the #[link(wasm_import_module)] needs to happen before the `extern
46704671
// "C"` block. It doesn't get picked up properly otherwise
4671-
let link_attribute = match (
4672-
ctx.options().wasm_import_module_name.as_ref(),
4673-
&ctx.options().windows_link_as_raw_dylib,
4674-
) {
4675-
(Some(_), (Some(_), _)) => {
4676-
panic!("Cannot link against a wasm import module and a raw dylib at the same time");
4677-
}
4678-
(Some(name), (None, _)) => {
4679-
Some(quote! { #[link(wasm_import_module = #name)] })
4672+
let mut link_attribute = quote! {};
4673+
if let Some(ref wasm_name) = ctx.options().wasm_import_module_name {
4674+
link_attribute.extend(quote! {
4675+
#[link(wasm_import_module = #wasm_name)]
4676+
});
4677+
}
4678+
if let (Some(ref windows_name), verbatim) =
4679+
ctx.options().windows_link_as_raw_dylib
4680+
{
4681+
if verbatim {
4682+
link_attribute.extend(quote! {
4683+
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib", modifiers = "+verbatim"))]
4684+
});
4685+
} else {
4686+
link_attribute.extend(quote! {
4687+
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib"))]
4688+
});
46804689
}
4681-
(None, (Some(name), false)) => Some(
4682-
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib"))] },
4683-
),
4684-
(None, (Some(name), true)) => Some(
4685-
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib", modifiers = "+verbatim"))] },
4686-
),
4687-
_ => None,
4688-
};
4690+
}
46894691

4690-
let should_wrap = is_internal &&
4691-
ctx.options().wrap_static_fns &&
4692-
link_name_attr.is_none();
4692+
let should_wrap = is_internal
4693+
&& ctx.options().wrap_static_fns
4694+
&& link_name_attr.is_none();
46934695

46944696
if should_wrap {
46954697
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
@@ -5233,8 +5235,8 @@ pub(crate) mod utils {
52335235
std::fs::create_dir_all(dir)?;
52345236
}
52355237

5236-
let is_cpp = args_are_cpp(&context.options().clang_args) ||
5237-
context
5238+
let is_cpp = args_are_cpp(&context.options().clang_args)
5239+
|| context
52385240
.options()
52395241
.input_headers
52405242
.iter()
@@ -5332,8 +5334,8 @@ pub(crate) mod utils {
53325334
ctx: &BindgenContext,
53335335
result: &mut Vec<proc_macro2::TokenStream>,
53345336
) {
5335-
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT) ||
5336-
ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
5337+
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT)
5338+
|| ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
53375339
{
53385340
return;
53395341
}

0 commit comments

Comments
 (0)