Skip to content

Commit ab95fd0

Browse files
committed
chore: allow both wasm_import and raw-dylib
1 parent 89b43dc commit ab95fd0

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() {
@@ -592,9 +592,9 @@ impl CodeGenerator for Module {
592592
}
593593
};
594594

595-
if !ctx.options().enable_cxx_namespaces ||
596-
(self.is_inline() &&
597-
!ctx.options().conservative_inline_namespaces)
595+
if !ctx.options().enable_cxx_namespaces
596+
|| (self.is_inline()
597+
&& !ctx.options().conservative_inline_namespaces)
598598
{
599599
codegen_self(result, &mut false);
600600
return;
@@ -844,19 +844,19 @@ impl CodeGenerator for Type {
844844
debug_assert!(item.is_enabled_for_codegen(ctx));
845845

846846
match *self.kind() {
847-
TypeKind::Void |
848-
TypeKind::NullPtr |
849-
TypeKind::Int(..) |
850-
TypeKind::Float(..) |
851-
TypeKind::Complex(..) |
852-
TypeKind::Array(..) |
853-
TypeKind::Vector(..) |
854-
TypeKind::Pointer(..) |
855-
TypeKind::Reference(..) |
856-
TypeKind::Function(..) |
857-
TypeKind::ResolvedTypeRef(..) |
858-
TypeKind::Opaque |
859-
TypeKind::TypeParam => {
847+
TypeKind::Void
848+
| TypeKind::NullPtr
849+
| TypeKind::Int(..)
850+
| TypeKind::Float(..)
851+
| TypeKind::Complex(..)
852+
| TypeKind::Array(..)
853+
| TypeKind::Vector(..)
854+
| TypeKind::Pointer(..)
855+
| TypeKind::Reference(..)
856+
| TypeKind::Function(..)
857+
| TypeKind::ResolvedTypeRef(..)
858+
| TypeKind::Opaque
859+
| TypeKind::TypeParam => {
860860
// These items don't need code generation, they only need to be
861861
// converted to rust types in fields, arguments, and such.
862862
// NOTE(emilio): If you add to this list, make sure to also add
@@ -1020,11 +1020,11 @@ impl CodeGenerator for Type {
10201020

10211021
// We prefer using `pub use` over `pub type` because of:
10221022
// https://github.com/rust-lang/rust/issues/26264
1023-
if matches!(inner_rust_type, syn::Type::Path(_)) &&
1024-
outer_params.is_empty() &&
1025-
!is_opaque &&
1026-
alias_style == AliasVariation::TypeAlias &&
1027-
inner_item.expect_type().canonical_type(ctx).is_enum()
1023+
if matches!(inner_rust_type, syn::Type::Path(_))
1024+
&& outer_params.is_empty()
1025+
&& !is_opaque
1026+
&& alias_style == AliasVariation::TypeAlias
1027+
&& inner_item.expect_type().canonical_type(ctx).is_enum()
10281028
{
10291029
tokens.append_all(quote! {
10301030
pub use
@@ -1201,9 +1201,9 @@ impl CodeGenerator for Vtable<'_> {
12011201
// For now, we will only generate vtables for classes that:
12021202
// - do not inherit from others (compilers merge VTable from primary parent class).
12031203
// - do not contain a virtual destructor (requires ordering; platforms generate different vtables).
1204-
if ctx.options().vtable_generation &&
1205-
self.comp_info.base_members().is_empty() &&
1206-
self.comp_info.destructor().is_none()
1204+
if ctx.options().vtable_generation
1205+
&& self.comp_info.base_members().is_empty()
1206+
&& self.comp_info.destructor().is_none()
12071207
{
12081208
let class_ident = ctx.rust_ident(self.item_id.canonical_name(ctx));
12091209

@@ -1792,8 +1792,8 @@ impl FieldCodegen<'_> for BitfieldUnit {
17921792
continue;
17931793
}
17941794

1795-
if layout.size > RUST_DERIVE_IN_ARRAY_LIMIT &&
1796-
!ctx.options().rust_features().larger_arrays
1795+
if layout.size > RUST_DERIVE_IN_ARRAY_LIMIT
1796+
&& !ctx.options().rust_features().larger_arrays
17971797
{
17981798
continue;
17991799
}
@@ -2395,10 +2395,10 @@ impl CodeGenerator for CompInfo {
23952395

23962396
// if a type has both a "packed" attribute and an "align(N)" attribute, then check if the
23972397
// "packed" attr is redundant, and do not include it if so.
2398-
if packed &&
2399-
!is_opaque &&
2400-
!(explicit_align.is_some() &&
2401-
self.already_packed(ctx).unwrap_or(false))
2398+
if packed
2399+
&& !is_opaque
2400+
&& !(explicit_align.is_some()
2401+
&& self.already_packed(ctx).unwrap_or(false))
24022402
{
24032403
let n = layout.map_or(1, |l| l.align);
24042404
assert!(ctx.options().rust_features().repr_packed_n || n == 1);
@@ -2439,32 +2439,32 @@ impl CodeGenerator for CompInfo {
24392439

24402440
let derivable_traits = derives_of_item(item, ctx, packed);
24412441
if !derivable_traits.contains(DerivableTraits::DEBUG) {
2442-
needs_debug_impl = ctx.options().derive_debug &&
2443-
ctx.options().impl_debug &&
2444-
!ctx.no_debug_by_name(item) &&
2445-
!item.annotations().disallow_debug();
2442+
needs_debug_impl = ctx.options().derive_debug
2443+
&& ctx.options().impl_debug
2444+
&& !ctx.no_debug_by_name(item)
2445+
&& !item.annotations().disallow_debug();
24462446
}
24472447

24482448
if !derivable_traits.contains(DerivableTraits::DEFAULT) {
2449-
needs_default_impl = ctx.options().derive_default &&
2450-
!self.is_forward_declaration() &&
2451-
!ctx.no_default_by_name(item) &&
2452-
!item.annotations().disallow_default();
2449+
needs_default_impl = ctx.options().derive_default
2450+
&& !self.is_forward_declaration()
2451+
&& !ctx.no_default_by_name(item)
2452+
&& !item.annotations().disallow_default();
24532453
}
24542454

24552455
let all_template_params = item.all_template_params(ctx);
24562456

2457-
if derivable_traits.contains(DerivableTraits::COPY) &&
2458-
!derivable_traits.contains(DerivableTraits::CLONE)
2457+
if derivable_traits.contains(DerivableTraits::COPY)
2458+
&& !derivable_traits.contains(DerivableTraits::CLONE)
24592459
{
24602460
needs_clone_impl = true;
24612461
}
24622462

24632463
if !derivable_traits.contains(DerivableTraits::PARTIAL_EQ) {
2464-
needs_partialeq_impl = ctx.options().derive_partialeq &&
2465-
ctx.options().impl_partialeq &&
2466-
ctx.lookup_can_derive_partialeq_or_partialord(item.id()) ==
2467-
CanDerive::Manually;
2464+
needs_partialeq_impl = ctx.options().derive_partialeq
2465+
&& ctx.options().impl_partialeq
2466+
&& ctx.lookup_can_derive_partialeq_or_partialord(item.id())
2467+
== CanDerive::Manually;
24682468
}
24692469

24702470
let mut derives: Vec<_> = derivable_traits.into();
@@ -2646,8 +2646,8 @@ impl CodeGenerator for CompInfo {
26462646
.collect()
26472647
};
26482648

2649-
let uninit_decl = if check_field_offset.is_empty() ||
2650-
compile_time
2649+
let uninit_decl = if check_field_offset.is_empty()
2650+
|| compile_time
26512651
{
26522652
None
26532653
} else {
@@ -2993,11 +2993,11 @@ impl Method {
29932993
let cc = &ctx.options().codegen_config;
29942994
match self.kind() {
29952995
MethodKind::Constructor => cc.constructors(),
2996-
MethodKind::Destructor |
2997-
MethodKind::VirtualDestructor { .. } => cc.destructors(),
2998-
MethodKind::Static |
2999-
MethodKind::Normal |
3000-
MethodKind::Virtual { .. } => cc.methods(),
2996+
MethodKind::Destructor
2997+
| MethodKind::VirtualDestructor { .. } => cc.destructors(),
2998+
MethodKind::Static
2999+
| MethodKind::Normal
3000+
| MethodKind::Virtual { .. } => cc.methods(),
30013001
}
30023002
});
30033003

@@ -3517,8 +3517,8 @@ impl EnumBuilder {
35173517
}
35183518
variants
35193519
}
3520-
EnumBuilderKind::Consts { .. } |
3521-
EnumBuilderKind::ModuleConsts { .. } => {
3520+
EnumBuilderKind::Consts { .. }
3521+
| EnumBuilderKind::ModuleConsts { .. } => {
35223522
let mut variants = vec![];
35233523

35243524
for v in self.enum_variants {
@@ -3640,8 +3640,8 @@ impl CodeGenerator for Enum {
36403640
let repr_translated;
36413641
let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) {
36423642
Some(repr)
3643-
if !ctx.options().translate_enum_integer_types &&
3644-
!variation.is_rust() =>
3643+
if !ctx.options().translate_enum_integer_types
3644+
&& !variation.is_rust() =>
36453645
{
36463646
repr
36473647
}
@@ -3712,10 +3712,10 @@ impl CodeGenerator for Enum {
37123712
// Clone/Eq/PartialEq/Hash, even if we don't generate those by
37133713
// default.
37143714
derives.insert(
3715-
DerivableTraits::CLONE |
3716-
DerivableTraits::HASH |
3717-
DerivableTraits::PARTIAL_EQ |
3718-
DerivableTraits::EQ,
3715+
DerivableTraits::CLONE
3716+
| DerivableTraits::HASH
3717+
| DerivableTraits::PARTIAL_EQ
3718+
| DerivableTraits::EQ,
37193719
);
37203720
let mut derives: Vec<_> = derives.into();
37213721
for derive in item.annotations().derives() {
@@ -3856,8 +3856,8 @@ impl CodeGenerator for Enum {
38563856
Entry::Occupied(ref entry) => {
38573857
if variation.is_rust() {
38583858
let variant_name = ctx.rust_mangle(variant.name());
3859-
let mangled_name = if is_toplevel ||
3860-
enum_ty.name().is_some()
3859+
let mangled_name = if is_toplevel
3860+
|| enum_ty.name().is_some()
38613861
{
38623862
variant_name
38633863
} else {
@@ -3916,8 +3916,8 @@ impl CodeGenerator for Enum {
39163916
// If it's an unnamed enum, or constification is enforced,
39173917
// we also generate a constant so it can be properly
39183918
// accessed.
3919-
if (variation.is_rust() && enum_ty.name().is_none()) ||
3920-
variant.force_constification()
3919+
if (variation.is_rust() && enum_ty.name().is_none())
3920+
|| variant.force_constification()
39213921
{
39223922
let mangled_name = if is_toplevel {
39233923
variant_name.clone()
@@ -4360,16 +4360,17 @@ impl TryToRustTy for Type {
43604360
inst.try_to_rust_ty(ctx, item)
43614361
}
43624362
TypeKind::ResolvedTypeRef(inner) => inner.try_to_rust_ty(ctx, &()),
4363-
TypeKind::TemplateAlias(..) |
4364-
TypeKind::Alias(..) |
4365-
TypeKind::BlockPointer(..) => {
4363+
TypeKind::TemplateAlias(..)
4364+
| TypeKind::Alias(..)
4365+
| TypeKind::BlockPointer(..) => {
43664366
if self.is_block_pointer() && !ctx.options().generate_block {
43674367
let void = c_void(ctx);
43684368
return Ok(void.to_ptr(/* is_const = */ false));
43694369
}
43704370

4371-
if item.is_opaque(ctx, &()) &&
4372-
item.used_template_params(ctx)
4371+
if item.is_opaque(ctx, &())
4372+
&& item
4373+
.used_template_params(ctx)
43734374
.into_iter()
43744375
.any(|param| param.is_template_param(ctx, &()))
43754376
{
@@ -4385,8 +4386,8 @@ impl TryToRustTy for Type {
43854386
}
43864387
TypeKind::Comp(ref info) => {
43874388
let template_params = item.all_template_params(ctx);
4388-
if info.has_non_type_template_params() ||
4389-
(item.is_opaque(ctx, &()) && !template_params.is_empty())
4389+
if info.has_non_type_template_params()
4390+
|| (item.is_opaque(ctx, &()) && !template_params.is_empty())
43904391
{
43914392
return self.try_to_opaque(ctx, item);
43924393
}
@@ -4717,28 +4718,29 @@ impl CodeGenerator for Function {
47174718
// Unfortunately this can't piggyback on the `attributes` list because
47184719
// the #[link(wasm_import_module)] needs to happen before the `extern
47194720
// "C"` block. It doesn't get picked up properly otherwise
4720-
let link_attribute = match (
4721-
ctx.options().wasm_import_module_name.as_ref(),
4722-
&ctx.options().windows_link_as_raw_dylib,
4723-
) {
4724-
(Some(_), (Some(_), _)) => {
4725-
panic!("Cannot link against a wasm import module and a raw dylib at the same time");
4726-
}
4727-
(Some(name), (None, _)) => {
4728-
Some(quote! { #[link(wasm_import_module = #name)] })
4721+
let mut link_attribute = quote! {};
4722+
if let Some(ref wasm_name) = ctx.options().wasm_import_module_name {
4723+
link_attribute.extend(quote! {
4724+
#[link(wasm_import_module = #wasm_name)]
4725+
});
4726+
}
4727+
if let (Some(ref windows_name), verbatim) =
4728+
ctx.options().windows_link_as_raw_dylib
4729+
{
4730+
if verbatim {
4731+
link_attribute.extend(quote! {
4732+
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib", modifiers = "+verbatim"))]
4733+
});
4734+
} else {
4735+
link_attribute.extend(quote! {
4736+
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib"))]
4737+
});
47294738
}
4730-
(None, (Some(name), false)) => Some(
4731-
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib"))] },
4732-
),
4733-
(None, (Some(name), true)) => Some(
4734-
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib", modifiers = "+verbatim"))] },
4735-
),
4736-
_ => None,
4737-
};
4739+
}
47384740

4739-
let should_wrap = is_internal &&
4740-
ctx.options().wrap_static_fns &&
4741-
link_name_attr.is_none();
4741+
let should_wrap = is_internal
4742+
&& ctx.options().wrap_static_fns
4743+
&& link_name_attr.is_none();
47424744

47434745
if should_wrap {
47444746
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
@@ -5282,8 +5284,8 @@ pub(crate) mod utils {
52825284
std::fs::create_dir_all(dir)?;
52835285
}
52845286

5285-
let is_cpp = args_are_cpp(&context.options().clang_args) ||
5286-
context
5287+
let is_cpp = args_are_cpp(&context.options().clang_args)
5288+
|| context
52875289
.options()
52885290
.input_headers
52895291
.iter()
@@ -5381,8 +5383,8 @@ pub(crate) mod utils {
53815383
ctx: &BindgenContext,
53825384
result: &mut Vec<proc_macro2::TokenStream>,
53835385
) {
5384-
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT) ||
5385-
ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
5386+
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT)
5387+
|| ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
53865388
{
53875389
return;
53885390
}

0 commit comments

Comments
 (0)