-
Notifications
You must be signed in to change notification settings - Fork 739
Wrap template and opaque types in a marker. #3151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// bindgen-flags: --use-opaque-newtype-wrapper --raw-line '#[repr(transparent)] pub struct __bindgen_marker_Opaque<T: ?Sized>(T);' -- -x c++ -std=c++14 | ||
|
||
class Bar {}; | ||
|
||
template <int N> | ||
class Foo { | ||
public: | ||
int a[N]; | ||
}; | ||
|
||
// Non-type template parameters are one of the cases where bindgen is | ||
// forced to generate an opaque type. Ensure we spot that and annotate | ||
// it. | ||
void take_foo(Foo<3> foo); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// bindgen-flags: --use-reference-newtype-wrapper --raw-line '#[repr(transparent)] pub struct __bindgen_marker_Reference<T: ?Sized>(*const T); #[repr(transparent)] pub struct __bindgen_marker_RValueReference<T: ?Sized>(*const T);' -- -x c++ -std=c++14 | ||
|
||
const int& receive_reference(const int& input) { | ||
return input; | ||
} | ||
int& receive_mut_reference(int& input) { | ||
return input; | ||
} | ||
void receive_rvalue_reference(int&& input) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2292,7 +2292,13 @@ impl CodeGenerator for CompInfo { | |
|
||
if has_address { | ||
let layout = Layout::new(1, 1); | ||
let ty = helpers::blob(ctx, Layout::new(1, 1), false); | ||
// Normally for opaque data we use helpers::blob, | ||
// which may wrap it in __bindgen_marker_Opaque<T>. | ||
// Downstream tools may then use this as a sign that | ||
// the type is complex or can't be stack-allocated, | ||
// etc. But in this case this one-byte field is harmless | ||
// so we'll just represent it without that extra marker. | ||
let ty = helpers::blob_inner(ctx, Layout::new(1, 1), false); | ||
struct_layout.saw_field_with_layout( | ||
"_address", | ||
layout, | ||
|
@@ -4358,7 +4364,7 @@ impl TryToRustTy for Type { | |
utils::build_path(item, ctx) | ||
} | ||
TypeKind::Opaque => self.try_to_opaque(ctx, item), | ||
TypeKind::Pointer(inner) | TypeKind::Reference(inner) => { | ||
TypeKind::Pointer(inner) | TypeKind::Reference(inner, _) => { | ||
// Check that this type has the same size as the target's pointer type. | ||
let size = self.get_layout(ctx, item).size; | ||
if size != ctx.target_pointer_size() { | ||
|
@@ -4391,7 +4397,23 @@ impl TryToRustTy for Type { | |
{ | ||
Ok(ty) | ||
} else { | ||
Ok(ty.to_ptr(is_const)) | ||
let ty_ptr = ty.to_ptr(is_const); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I guess that means that this should be something like: "if reference, then |
||
Ok(if let syn::Type::Ptr(ty_ptr_inside) = &ty_ptr { | ||
// It always should be Type::Ptr, but just in case | ||
if let TypeKind::Reference(_, reference_kind) = | ||
self.kind() | ||
{ | ||
helpers::reference( | ||
ty_ptr_inside.clone(), | ||
*reference_kind, | ||
ctx, | ||
) | ||
} else { | ||
ty_ptr | ||
} | ||
} else { | ||
ty_ptr | ||
}) | ||
} | ||
} | ||
TypeKind::TypeParam => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we handle
Pointer
andReference
meaningfully differently somewhere else? I wonder if instead of addingReference(..., ReferenceKind)
we should just havePointer(inner, PointerKind)
wherePointerKind { Regular, LValueReference, RValueReference }
or so.