Skip to content
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

Proof of concept: Template functions #2650

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
160 changes: 91 additions & 69 deletions bindgen/ir/comp.rs
Expand Up @@ -1265,6 +1265,82 @@

let mut maybe_anonymous_struct_field = None;
cursor.visit(|cur| {
let mut handle_method_cursor =
|cur: clang::Cursor, ci: &mut CompInfo| {
let is_virtual = cur.method_is_virtual();
let is_static = cur.method_is_static();
debug_assert!(!(is_static && is_virtual), "How?");

ci.has_destructor |= cur.kind() == CXCursor_Destructor;
ci.has_own_virtual_method |= is_virtual;

// This used to not be here, but then I tried generating
// stylo bindings with this (without path filters), and
// cried a lot with a method in gfx/Point.h
// (ToUnknownPoint), that somehow was causing the same type
// to be inserted in the map two times.
//
// I couldn't make a reduced test case, but anyway...
// Methods of template functions not only used to be inlined,
// but also instantiated, and we wouldn't be able to call
// them, so just bail out.
if !ci.template_params.is_empty() {
return Some(CXChildVisit_Continue);
}

// NB: This gets us an owned `Function`, not a
// `FunctionSig`.
let signature =
match Item::parse(cur, Some(potential_id), ctx) {
Ok(item)
if ctx
.resolve_item(item)
.kind()
.is_function() =>
{
item
}
_ => return Some(CXChildVisit_Continue),
};

let signature = signature.expect_function_id(ctx);

match cur.kind() {
CXCursor_Constructor => {
ci.constructors.push(signature);
}
CXCursor_Destructor => {
let kind = if is_virtual {
MethodKind::VirtualDestructor {
pure_virtual: cur.method_is_pure_virtual(),
}
} else {
MethodKind::Destructor
};
ci.destructor = Some((kind, signature));
}
CXCursor_CXXMethod => {
let is_const = cur.method_is_const();
let method_kind = if is_static {
MethodKind::Static
} else if is_virtual {
MethodKind::Virtual {
pure_virtual: cur.method_is_pure_virtual(),
}
} else {
MethodKind::Normal
};

let method =
Method::new(method_kind, signature, is_const);

ci.methods.push(method);
}
_ => unreachable!("How can we see this here?"),
};
None
};

if cur.kind() != CXCursor_FieldDecl {
if let Some((ty, clang_ty, public, offset)) =
maybe_anonymous_struct_field.take()
Expand Down Expand Up @@ -1454,77 +1530,24 @@
}
CXCursor_Constructor | CXCursor_Destructor |
CXCursor_CXXMethod => {
let is_virtual = cur.method_is_virtual();
let is_static = cur.method_is_static();
debug_assert!(!(is_static && is_virtual), "How?");

ci.has_destructor |= cur.kind() == CXCursor_Destructor;
ci.has_own_virtual_method |= is_virtual;

// This used to not be here, but then I tried generating
// stylo bindings with this (without path filters), and
// cried a lot with a method in gfx/Point.h
// (ToUnknownPoint), that somehow was causing the same type
// to be inserted in the map two times.
//
// I couldn't make a reduced test case, but anyway...
// Methods of template functions not only used to be inlined,
// but also instantiated, and we wouldn't be able to call
// them, so just bail out.
if !ci.template_params.is_empty() {
return CXChildVisit_Continue;
if let Some(res) = handle_method_cursor(cur, &mut ci) {
return res;
}

// NB: This gets us an owned `Function`, not a
// `FunctionSig`.
let signature =
match Item::parse(cur, Some(potential_id), ctx) {
Ok(item)
if ctx
.resolve_item(item)
.kind()
.is_function() =>
{
item
}
_ => return CXChildVisit_Continue,
};

let signature = signature.expect_function_id(ctx);

match cur.kind() {
CXCursor_Constructor => {
ci.constructors.push(signature);
}
CXCursor_Destructor => {
let kind = if is_virtual {
MethodKind::VirtualDestructor {
pure_virtual: cur.method_is_pure_virtual(),
}
} else {
MethodKind::Destructor
};
ci.destructor = Some((kind, signature));
}
CXCursor_CXXMethod => {
let is_const = cur.method_is_const();
let method_kind = if is_static {
MethodKind::Static
} else if is_virtual {
MethodKind::Virtual {
pure_virtual: cur.method_is_pure_virtual(),
}
CXCursor_FunctionTemplate => {
cur.visit(|child| {
match child.kind() {

Check warning on line 1539 in bindgen/ir/comp.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

you seem to be trying to use `match` for an equality check. Consider using `if`
CXCursor_CXXMethod => {
if let Some(res) =
handle_method_cursor(child, &mut ci)
{
return res;
}
} else {
MethodKind::Normal
};

let method =
Method::new(method_kind, signature, is_const);

ci.methods.push(method);
}
_ => {}
}
_ => unreachable!("How can we see this here?"),
}
CXChildVisit_Continue
});
}
CXCursor_NonTypeTemplateParameter => {
ci.has_non_type_template_params = true;
Expand All @@ -1550,7 +1573,6 @@
// Intentionally not handled
CXCursor_CXXAccessSpecifier |
CXCursor_CXXFinalAttr |
CXCursor_FunctionTemplate |
CXCursor_ConversionFunction => {}
_ => {
warn!(
Expand Down
4 changes: 4 additions & 0 deletions bindgen/ir/function.rs
Expand Up @@ -710,6 +710,10 @@ impl ClangSubItemParser for Function {
) -> Result<ParseResult<Self>, ParseError> {
use clang_sys::*;

if cursor.kind() == CXCursor_FunctionTemplate {
return Err(ParseError::Recurse);
}

let kind = match FunctionKind::from_cursor(&cursor) {
None => return Err(ParseError::Continue),
Some(k) => k,
Expand Down