Skip to content

Commit af993d2

Browse files
committed
Introduce function matcher to improve efficiency of function tweaking
1 parent e30aaf6 commit af993d2

File tree

9 files changed

+591
-168
lines changed

9 files changed

+591
-168
lines changed

binding-generator/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
339339
};
340340
if let Self::Clang { gen_env, .. } = self {
341341
if func.is_generic() {
342-
if let Some(specs) = gen_env.settings.func_specialize.get(&func.func_id()) {
342+
if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
343343
for spec in specs {
344344
out.push(func.clone().specialize(spec));
345345
}

binding-generator/src/func.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::rc::Rc;
66

77
use clang::{Availability, Entity, EntityKind, ExceptionSpecification};
88
pub use desc::{FuncCppBody, FuncDesc, FuncRustBody, FuncRustExtern};
9-
pub use func_id::FuncId;
9+
pub use func_matcher::{FuncId, FuncMatchProperties, FuncMatcher, Pred};
1010
pub use kind::{FuncKind, OperatorKind, ReturnKind};
1111
use once_cell::sync::Lazy;
1212
use regex::bytes::Regex;
@@ -27,7 +27,7 @@ use crate::{
2727
};
2828

2929
mod desc;
30-
mod func_id;
30+
mod func_matcher;
3131
mod kind;
3232
mod slice_arg_finder;
3333

@@ -611,6 +611,10 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
611611
}
612612
}
613613

614+
pub fn matcher(&self) -> FuncMatchProperties {
615+
FuncMatchProperties::new(self, self.cpp_name(CppNameStyle::Reference))
616+
}
617+
614618
pub fn rust_body(&self) -> &FuncRustBody {
615619
match self {
616620
Self::Clang { .. } => &FuncRustBody::Auto,
@@ -708,13 +712,23 @@ impl Element for Func<'_, '_> {
708712
}
709713

710714
fn cpp_name(&self, style: CppNameStyle) -> Cow<str> {
711-
let decl_name = if self.kind().as_conversion_method().is_some() {
712-
format!("operator {}", self.return_type_ref().cpp_name(CppNameStyle::Reference)).into()
713-
} else {
714-
match self {
715-
&Self::Clang { entity, .. } => DefaultElement::cpp_name(self, entity, CppNameStyle::Declaration),
716-
Self::Desc(desc) => desc.cpp_name.cpp_name_from_fullname(CppNameStyle::Declaration).into(),
715+
let decl_name = match self {
716+
&Self::Clang { entity, gen_env, .. } => {
717+
if matches!(entity.get_kind(), EntityKind::ConversionFunction) {
718+
// this is to avoid calling `return_type_ref()` just to get the name of the type otherwise it causes infinite recursion
719+
// together with the call `cpp_name(Reference)` from `matcher()`
720+
let ret_type = TypeRef::new(entity.get_result_type().expect("Can't get result type"), gen_env);
721+
let ret_type = ret_type
722+
.kind()
723+
.as_reference()
724+
.map(|tref| tref.into_owned())
725+
.unwrap_or(ret_type);
726+
format!("operator {}", ret_type.cpp_name(CppNameStyle::Reference)).into()
727+
} else {
728+
DefaultElement::cpp_name(self, entity, CppNameStyle::Declaration)
729+
}
717730
}
731+
Self::Desc(desc) => desc.cpp_name.cpp_name_from_fullname(CppNameStyle::Declaration).into(),
718732
};
719733
match style {
720734
CppNameStyle::Declaration => decl_name,
@@ -742,7 +756,11 @@ impl<'me> NameDebug<'me> for &'me Func<'_, '_> {
742756
.iter()
743757
.map(|a| format!("{:?}", a.type_ref().render_lane()))
744758
.join(", ");
745-
format!("// {name}({render_lanes}) {func_id} {location}", func_id = self.func_id())
759+
format!(
760+
"// {name}({render_lanes}) {location}\n\
761+
// {func_match}",
762+
func_match = self.matcher().dump()
763+
)
746764
} else {
747765
"".to_string()
748766
}

binding-generator/src/func/func_id.rs

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)