Skip to content

Commit 87465c6

Browse files
committed
Deglobalize FUNC_REPLACE and switch to function matching
1 parent 0cf56fd commit 87465c6

File tree

9 files changed

+148
-136
lines changed

9 files changed

+148
-136
lines changed

binding-generator/src/bin/settings-cleanup.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ struct FunctionFinder<'tu, 'f> {
1616
pub func_exclude_unused: RefCell<&'f mut HashSet<&'static str>>,
1717
pub func_cfg_attr_unused: RefCell<&'f mut HashSet<&'static str>>,
1818
pub func_unsafe_unused: RefCell<&'f mut HashSet<FuncId<'static>>>,
19-
pub func_replace_unused: RefCell<&'f mut HashSet<FuncId<'static>>>,
2019
}
2120

2221
impl<'tu, 'f> FunctionFinder<'tu, 'f> {
@@ -27,7 +26,6 @@ impl<'tu, 'f> FunctionFinder<'tu, 'f> {
2726
self.func_exclude_unused.borrow_mut().remove(identifier.as_str());
2827
self.func_cfg_attr_unused.borrow_mut().remove(identifier.as_str());
2928
self.func_unsafe_unused.borrow_mut().remove(&func_id);
30-
self.func_replace_unused.borrow_mut().remove(&func_id);
3129
}
3230
}
3331

@@ -85,7 +83,6 @@ fn main() {
8583
let mut func_exclude_unused = settings::FUNC_EXCLUDE.clone();
8684
let mut func_cfg_attr_unused = settings::FUNC_CFG_ATTR.keys().copied().collect::<HashSet<_>>();
8785
let mut func_unsafe_unused = settings::FUNC_UNSAFE.clone();
88-
let mut func_replace_unused = settings::FUNC_REPLACE.keys().cloned().collect::<HashSet<_>>();
8986
for opencv_header_dir in opencv_header_dirs {
9087
println!("Processing header dir: {}", opencv_header_dir.display());
9188
let modules = opencv_header_dir
@@ -109,7 +106,6 @@ fn main() {
109106
func_exclude_unused: RefCell::new(&mut func_exclude_unused),
110107
func_cfg_attr_unused: RefCell::new(&mut func_cfg_attr_unused),
111108
func_unsafe_unused: RefCell::new(&mut func_unsafe_unused),
112-
func_replace_unused: RefCell::new(&mut func_replace_unused),
113109
});
114110
});
115111
}
@@ -120,6 +116,4 @@ fn main() {
120116
show(func_cfg_attr_unused);
121117
println!("Unused entries in settings::FUNC_UNSAFE ({}):", func_unsafe_unused.len());
122118
show(func_unsafe_unused);
123-
println!("Unused entries in settings::FUNC_REPLACE ({}):", func_replace_unused.len());
124-
show(func_replace_unused);
125119
}

binding-generator/src/class.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -330,28 +330,27 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
330330
}
331331

332332
pub fn methods(&self) -> Vec<Func<'tu, 'ge>> {
333-
let mut out = Vec::with_capacity(32);
334-
self.for_each_method(|func| {
335-
let func: Func = if let Some(func_fact) = settings::FUNC_REPLACE.get(&func.func_id()) {
336-
func_fact(&func)
337-
} else {
338-
func
339-
};
340-
if let Self::Clang { gen_env, .. } = self {
341-
if func.is_generic() {
342-
if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
343-
for spec in specs {
344-
out.push(func.clone().specialize(spec));
333+
match self {
334+
Class::Clang { entity, gen_env, .. } => {
335+
let mut out = Vec::with_capacity(32);
336+
entity.walk_methods_while(|func_entity| {
337+
let func = Func::new(func_entity, gen_env);
338+
let func: Func = if let Some(func_fact) = gen_env.settings.func_replace.get(&mut func.matcher()) {
339+
func_fact(&func)
340+
} else {
341+
func
342+
};
343+
if func.is_generic() {
344+
if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
345+
for spec in specs {
346+
out.push(func.clone().specialize(spec));
347+
}
348+
return ControlFlow::Continue(());
345349
}
346-
return ControlFlow::Continue(());
347350
}
348-
}
349-
}
350-
out.push(func);
351-
ControlFlow::Continue(())
352-
});
353-
match self {
354-
Class::Clang { gen_env, .. } => {
351+
out.push(func);
352+
ControlFlow::Continue(())
353+
});
355354
for inject_func_fact in &gen_env.settings.func_inject {
356355
let inject_func: Func = inject_func_fact();
357356
if let Some(cls) = inject_func.kind().as_class_method() {
@@ -360,10 +359,10 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
360359
}
361360
}
362361
}
362+
out
363363
}
364-
Class::Desc(_) => {}
365-
};
366-
out
364+
Class::Desc(_) => vec![],
365+
}
367366
}
368367

369368
pub fn has_fields(&self) -> bool {

binding-generator/src/func.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,10 @@ impl<'tu> ToEntity<'tu> for &Func<'tu, '_> {
648648

649649
impl Element for Func<'_, '_> {
650650
fn exclude_kind(&self) -> ExcludeKind {
651-
let func_id = self.func_id();
652-
if settings::FUNC_REPLACE.contains_key(&func_id) {
653-
return ExcludeKind::Included.with_is_excluded(|| settings::FUNC_EXCLUDE.contains(self.identifier().as_str()));
654-
}
655-
let kind = self.kind();
656651
DefaultElement::exclude_kind(self)
657652
.with_reference_exclude_kind(|| self.return_type_ref().exclude_kind())
658653
.with_is_excluded(|| {
654+
let kind = self.kind();
659655
let identifier = self.identifier();
660656
let is_unavailable = match self {
661657
Func::Clang { entity, .. } => entity.get_availability() == Availability::Unavailable,

binding-generator/src/generator.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,10 @@ impl<'tu, 'r, V: GeneratorVisitor<'tu>> OpenCvWalker<'tu, 'r, V> {
274274

275275
fn process_func(visitor: &mut V, func_names: &mut NamePool, gen_env: &GeneratorEnv<'tu>, func_decl: Entity<'tu>) {
276276
if let Some(e) = gen_env.get_export_config(func_decl) {
277-
let func = Func::new(func_decl, gen_env);
278-
let func_id = func.func_id();
279-
let func: Func = if let Some(func_fact) = settings::FUNC_REPLACE.get(&func_id) {
280-
func_fact(&func)
281-
} else {
282-
Func::new(func_decl, gen_env)
283-
};
277+
let mut func = Func::new(func_decl, gen_env);
278+
if let Some(func_fact) = gen_env.settings.func_replace.get(&mut func.matcher()) {
279+
func = func_fact(&func)
280+
}
284281
if func.exclude_kind().is_included() {
285282
let mut processor = |mut func: Func<'tu, '_>| {
286283
func.generated_types().into_iter().for_each(|dep| {

binding-generator/src/settings.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub use func_cfg_attr::FUNC_CFG_ATTR;
5050
pub use func_exclude::FUNC_EXCLUDE;
5151
pub use func_inject::{func_inject_factory, FuncFactory, FuncInject};
5252
pub use func_rename::{func_rename_factory, FuncRename};
53-
pub use func_replace::{FuncInheritFactory, FUNC_REPLACE};
53+
pub use func_replace::{func_replace_factory, FuncInheritFactory, FuncReplace};
5454
pub use func_specialize::{func_specialize_factory, FuncSpec, FuncSpecialize};
5555
pub use func_unsafe::FUNC_UNSAFE;
5656
pub use generator_module_tweaks::{generator_module_tweaks_factory, ModuleTweak};
@@ -90,6 +90,7 @@ pub struct Settings {
9090
pub force_infallible: ForceInfallible,
9191
pub func_inject: FuncInject,
9292
pub func_rename: FuncRename,
93+
pub func_replace: FuncReplace,
9394
pub func_specialize: FuncSpecialize,
9495
pub generator_module_tweaks: ModuleTweak,
9596
pub property_rename: PropertyRename,
@@ -103,6 +104,7 @@ impl Settings {
103104
force_infallible: ForceInfallible::empty(),
104105
func_inject: FuncInject::default(),
105106
func_rename: FuncRename::default(),
107+
func_replace: FuncReplace::empty(),
106108
func_specialize: FuncMatcher::empty(),
107109
generator_module_tweaks: ModuleTweak::empty(),
108110
property_rename: PropertyRename::default(),
@@ -116,6 +118,7 @@ impl Settings {
116118
force_infallible: force_infallible_factory(module),
117119
func_inject: func_inject_factory(module),
118120
func_rename: func_rename_factory(module),
121+
func_replace: func_replace_factory(module),
119122
func_specialize: func_specialize_factory(module),
120123
generator_module_tweaks: generator_module_tweaks_factory(module),
121124
property_rename: property_rename_factory(module),

binding-generator/src/settings/func_exclude.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
88
// ### core ###
99
"cv_AsyncArray__getImpl_const",
1010
"cv_Mat_Mat_const_MatR_const_RangeX", // duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
11-
"cv_Mat_at_const_VecLint__cnGR", // fixme: due to FuncId only taking into account arg name we generate extra Mat::at with VecN args
12-
"cv_Mat_at_const_const_VecLint__cnGR", // fixme: due to FuncId only taking into account arg name we generate extra Mat::at with VecN args
13-
"cv_Mat_copySize_const_MatR", // internal function
14-
"cv_Mat_operator___const_const_RangeX", // duplicate of cv_Mat_operator___const_const_vectorLRangeGR, but with pointers
15-
"cv_Mat_propStep_const_MatStep", // MatStep type prevents assignment
16-
"cv_Mat_push_back__const_voidX", // internal method
17-
"cv_Mat_operatorST_const_MatR", // unsafe with the safe version that moves
11+
"cv_Mat_copySize_const_MatR", // internal function
12+
"cv_Mat_operator___const_const_RangeX", // duplicate of cv_Mat_operator___const_const_vectorLRangeGR, but with pointers
13+
"cv_Mat_propStep_const_MatStep", // MatStep type prevents assignment
14+
"cv_Mat_push_back__const_voidX", // internal method
15+
"cv_Mat_operatorST_const_MatR", // unsafe with the safe version that moves
1816
"cv_UMat_UMat_const_UMatR_const_RangeX", // duplicate of cv_UMat_UMat_UMat_VectorOfRange, but with pointers
19-
"cv_UMat_copySize_const_UMatR", // internal function
17+
"cv_UMat_copySize_const_UMatR", // internal function
2018
"cv_UMat_operator___const_const_RangeX", // duplicate of cv_UMat_operator___const_const_vectorLRangeGR, but with pointers
21-
"cv_UMat_propStep_const_MatStep", // MatStep type prevents assignment
22-
"cv_RNG_MT19937_operator__", // the same as calling to_u32() or next()
19+
"cv_UMat_propStep_const_MatStep", // MatStep type prevents assignment
20+
"cv_RNG_MT19937_operator__", // the same as calling to_u32() or next()
2321
"cv_addImpl_int_const_charX",
2422
"cv_calcCovarMatrix_const_MatX_int_MatR_MatR_int_int", // duplicate of cv_calcCovarMatrix_const__InputArrayR_const__OutputArrayR_const__InputOutputArrayR_int_int, but with pointers
2523
"cv_cv_abs_short",
@@ -52,18 +50,18 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
5250
"cv_RMat_access_const_Access", // use of deleted function ‘cv::RMat::View::View(const cv::RMat::View&)’
5351
// ### hdf ###
5452
"cv_hdf_HDF5_dscreate_const_const_int_const_int_const_int_const_StringR_const_int_const_intX", // has corresponding Vector version
55-
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
56-
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
57-
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX", // has corresponding Vector version
58-
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
59-
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
60-
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
53+
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
54+
"cv_hdf_HDF5_dsinsert_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
55+
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX", // has corresponding Vector version
56+
"cv_hdf_HDF5_dsread_const_const__OutputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
57+
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX", // has corresponding Vector version
58+
"cv_hdf_HDF5_dswrite_const_const__InputArrayR_const_StringR_const_intX_const_intX", // has corresponding Vector version
6159
// ### imgproc ###
6260
"cv_calcBackProject_const_MatX_int_const_intX_const_SparseMatR_const__OutputArrayR_const_floatXX_double_bool", // slice pointers
6361
"cv_calcBackProject_const_MatX_int_const_intX_const__InputArrayR_const__OutputArrayR_const_floatXX_double_bool", // slice pointers
64-
"cv_calcHist_const_MatX_int_const_intX_const__InputArrayR_SparseMatR_int_const_intX_const_floatXX_bool_bool", // slice pointers
62+
"cv_calcHist_const_MatX_int_const_intX_const__InputArrayR_SparseMatR_int_const_intX_const_floatXX_bool_bool", // slice pointers
6563
"cv_calcHist_const_MatX_int_const_intX_const__InputArrayR_const__OutputArrayR_int_const_intX_const_floatXX_bool_bool", // slice pointers
66-
"cv_fillConvexPoly_MatR_const_PointX_int_const_ScalarR_int_int", // 3.2 3.4
64+
"cv_fillConvexPoly_MatR_const_PointX_int_const_ScalarR_int_int", // 3.2 3.4
6765
"cv_fillConvexPoly_const__InputOutputArrayR_const_PointX_int_const_ScalarR_int_int",
6866
"cv_fillPoly_MatR_const_PointXX_const_intX_int_const_ScalarR_int_int_Point", // 3.2
6967
"cv_fillPoly_const__InputOutputArrayR_const_PointXX_const_intX_int_const_ScalarR_int_int_Point",
Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
use std::borrow::Cow;
22
use std::collections::HashMap;
33

4-
use once_cell::sync::Lazy;
5-
64
use crate::class::ClassDesc;
7-
use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncRustBody, FuncRustExtern, InheritConfig, ReturnKind};
5+
use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncMatcher, FuncRustBody, FuncRustExtern, InheritConfig, ReturnKind};
86
use crate::type_ref::{Constness, TypeRef};
9-
use crate::{Func, FuncId};
7+
use crate::Func;
8+
9+
pub type FuncReplace = FuncMatcher<'static, FuncInheritFactory>;
1010

1111
pub type FuncInheritFactory = for<'tu, 'ge> fn(&Func<'tu, 'ge>) -> Func<'tu, 'ge>;
1212

13-
pub static FUNC_REPLACE: Lazy<HashMap<FuncId, FuncInheritFactory>> = Lazy::new(|| {
13+
pub fn func_replace_factory(module: &str) -> FuncReplace {
14+
match module {
15+
"core" => core_factory(),
16+
_ => FuncReplace::empty(),
17+
}
18+
}
19+
20+
fn core_factory() -> FuncReplace {
1421
const MAT_FORWARD_INHERIT_CONFIG: InheritConfig = InheritConfig {
1522
kind: false,
1623
name: false,
@@ -38,73 +45,77 @@ pub static FUNC_REPLACE: Lazy<HashMap<FuncId, FuncInheritFactory>> = Lazy::new(|
3845
.rust_generic_decls([("T".to_string(), "core::DataType".to_string())])
3946
}
4047

41-
HashMap::from([
42-
(
43-
FuncId::new_mut("cv::Mat::at", ["i0"]),
44-
(|f| {
45-
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_mut"))
46-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
47-
}) as FuncInheritFactory,
48-
),
49-
(
50-
FuncId::new_const("cv::Mat::at", ["i0"]),
51-
(|f| Func::new_desc(make_at_forward(Constness::Const)).inheriting(f, MAT_FORWARD_INHERIT_CONFIG)) as FuncInheritFactory,
52-
),
53-
(
54-
FuncId::new_mut("cv::Mat::at", ["row", "col"]),
55-
(|f| {
56-
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_2d_mut"))
57-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
58-
}) as FuncInheritFactory,
59-
),
60-
(
61-
FuncId::new_const("cv::Mat::at", ["row", "col"]),
62-
(|f| {
63-
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_2d"))
64-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
65-
}) as FuncInheritFactory,
66-
),
67-
(
68-
FuncId::new_mut("cv::Mat::at", ["i0", "i1", "i2"]),
69-
(|f| {
70-
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_3d_mut"))
71-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
72-
}) as FuncInheritFactory,
73-
),
74-
(
75-
FuncId::new_const("cv::Mat::at", ["i0", "i1", "i2"]),
76-
(|f| {
77-
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_3d"))
78-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
79-
}) as FuncInheritFactory,
80-
),
81-
(
82-
FuncId::new_mut("cv::Mat::at", ["pt"]),
83-
(|f| {
84-
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_pt_mut"))
85-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
86-
}) as FuncInheritFactory,
87-
),
88-
(
89-
FuncId::new_const("cv::Mat::at", ["pt"]),
90-
(|f| {
91-
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_pt"))
92-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
93-
}) as FuncInheritFactory,
94-
),
95-
(
96-
FuncId::new_mut("cv::Mat::at", ["idx"]),
97-
(|f| {
98-
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_nd_mut"))
99-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
100-
}) as FuncInheritFactory,
101-
),
102-
(
103-
FuncId::new_const("cv::Mat::at", ["idx"]),
104-
(|f| {
105-
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_nd"))
106-
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
107-
}) as FuncInheritFactory,
108-
),
109-
])
110-
});
48+
FuncMatcher::create(HashMap::from([(
49+
"cv::Mat::at",
50+
vec![
51+
(
52+
pred!(mut, ["i0"]),
53+
(|f| {
54+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_mut"))
55+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
56+
}) as FuncInheritFactory,
57+
),
58+
(
59+
pred!(const, ["i0"]),
60+
(|f| Func::new_desc(make_at_forward(Constness::Const)).inheriting(f, MAT_FORWARD_INHERIT_CONFIG))
61+
as FuncInheritFactory,
62+
),
63+
(
64+
pred!(mut, ["row", "col"]),
65+
(|f| {
66+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_2d_mut"))
67+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
68+
}) as FuncInheritFactory,
69+
),
70+
(
71+
pred!(const, ["row", "col"]),
72+
(|f| {
73+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_2d"))
74+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
75+
}) as FuncInheritFactory,
76+
),
77+
(
78+
pred!(mut, ["i0", "i1", "i2"]),
79+
(|f| {
80+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_3d_mut"))
81+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
82+
}) as FuncInheritFactory,
83+
),
84+
(
85+
pred!(const, ["i0", "i1", "i2"]),
86+
(|f| {
87+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_3d"))
88+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
89+
}) as FuncInheritFactory,
90+
),
91+
(
92+
pred!(mut, ["pt"]),
93+
(|f| {
94+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_pt_mut"))
95+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
96+
}) as FuncInheritFactory,
97+
),
98+
(
99+
pred!(const, ["pt"]),
100+
(|f| {
101+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_pt"))
102+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
103+
}) as FuncInheritFactory,
104+
),
105+
(
106+
pred!(mut, ["idx"]),
107+
(|f| {
108+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_nd_mut"))
109+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
110+
}) as FuncInheritFactory,
111+
),
112+
(
113+
pred!(const, ["idx"]),
114+
(|f| {
115+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_nd"))
116+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
117+
}) as FuncInheritFactory,
118+
),
119+
],
120+
)]))
121+
}

0 commit comments

Comments
 (0)