From 2ad1b52277c960ffab05573a0ff6a30a3fa1d617 Mon Sep 17 00:00:00 2001 From: Emilio Cobos Álvarez Date: Sat, 7 Jan 2017 02:04:47 +0100 Subject: codegen: Don't make auto-opaque types without template params. --- libbindgen/src/codegen/mod.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'libbindgen/src/codegen/mod.rs') diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index 6e63a791..d12c3d2a 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -1879,7 +1879,13 @@ impl ToRustTy for Type { TypeKind::ResolvedTypeRef(inner) => inner.to_rust_ty(ctx), TypeKind::TemplateAlias(ref spelling, inner, _) | TypeKind::Alias(ref spelling, inner) => { - if item.is_opaque(ctx) { + let applicable_named_args = + item.applicable_template_args(ctx) + .into_iter() + .filter(|arg| ctx.resolve_type(*arg).is_named()) + .collect::>(); + + if item.is_opaque(ctx) && !applicable_named_args.is_empty() { // Pray if there's no available layout. let layout = self.layout(ctx).unwrap_or_else(Layout::zero); BlobTyBuilder::new(layout).build() @@ -1888,11 +1894,15 @@ impl ToRustTy for Type { inner) { ty } else { - utils::build_templated_path(item, ctx, true) + utils::build_templated_path(item, + ctx, + applicable_named_args) } } TypeKind::Comp(ref info) => { - if item.is_opaque(ctx) || info.has_non_type_template_params() { + let template_args = item.applicable_template_args(ctx); + if info.has_non_type_template_params() || + (item.is_opaque(ctx) && !template_args.is_empty()) { return match self.layout(ctx) { Some(layout) => BlobTyBuilder::new(layout).build(), None => { @@ -1904,7 +1914,7 @@ impl ToRustTy for Type { }; } - utils::build_templated_path(item, ctx, false) + utils::build_templated_path(item, ctx, template_args) } TypeKind::BlockPointer => { let void = raw_type(ctx, "c_void"); @@ -2215,23 +2225,15 @@ mod utils { pub fn build_templated_path(item: &Item, ctx: &BindgenContext, - only_named: bool) + template_args: Vec) -> P { let path = item.namespace_aware_canonical_path(ctx); - let builder = aster::AstBuilder::new().ty().path(); - let template_args = if only_named { - item.applicable_template_args(ctx) - .iter() - .filter(|arg| ctx.resolve_type(**arg).is_named()) - .map(|arg| arg.to_rust_ty(ctx)) - .collect::>() - } else { - item.applicable_template_args(ctx) - .iter() - .map(|arg| arg.to_rust_ty(ctx)) - .collect::>() - }; + + let template_args = template_args + .iter() + .map(|arg| arg.to_rust_ty(ctx)) + .collect::>(); // XXX: I suck at aster. if path.len() == 1 { -- cgit v1.2.3