Skip to content

Commit 60b1abc

Browse files
committed
Cleanup
1 parent db6fc08 commit 60b1abc

36 files changed

+911
-845
lines changed

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ impl<'tu> EntityWalkerVisitor<'tu> for &mut FunctionFinder<'tu> {
4343
| EntityKind::StructDecl => {
4444
let c = Class::new(entity, &self.gen_env);
4545
if !c.template_kind().is_template() {
46-
c.methods().into_iter().for_each(|f| self.update_used_func(&f));
47-
let fields = c.fields();
48-
c.field_methods(fields.iter(), None)
49-
.into_iter()
46+
c.methods(|_| true).into_iter().for_each(|f| self.update_used_func(&f));
47+
c.field_methods(&c.fields(|_| true), None)
5048
.for_each(|f| self.update_used_func(&f));
5149
entity.walk_methods_while(|child| {
5250
let func = Func::new(child, &self.gen_env);

binding-generator/src/class.rs

+42-24
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
5555
Self::Desc(Rc::new(desc))
5656
}
5757

58-
/// Checks whether a class can be simple on Rust side, i.e. represented by plain struct with public fields
58+
/// Checks whether a class can be simple on Rust side, i.e. represented by plain struct with fields
5959
pub fn can_be_simple(&self) -> bool {
6060
let cpp_refname = self.cpp_name(CppNameStyle::Reference);
6161
settings::IMPLEMENTED_GENERICS.contains(cpp_refname.as_ref())
@@ -329,7 +329,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
329329
}
330330
}
331331

332-
pub fn methods(&self) -> Vec<Func<'tu, 'ge>> {
332+
pub fn methods(&self, filter: impl Fn(&Func) -> bool) -> Vec<Func<'tu, 'ge>> {
333333
match self {
334334
Class::Clang { entity, gen_env, .. } => {
335335
let mut out = Vec::with_capacity(32);
@@ -343,18 +343,23 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
343343
if func.is_generic() {
344344
if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
345345
for spec in specs {
346-
out.push(func.clone().specialize(spec));
346+
let spec_func = func.clone().specialize(spec);
347+
if filter(&spec_func) {
348+
out.push(spec_func);
349+
}
347350
}
348351
return ControlFlow::Continue(());
349352
}
350353
}
351-
out.push(func);
354+
if filter(&func) {
355+
out.push(func);
356+
}
352357
ControlFlow::Continue(())
353358
});
354359
for inject_func_fact in &gen_env.settings.func_inject {
355360
let inject_func: Func = inject_func_fact();
356361
if let Some(cls) = inject_func.kind().as_class_method() {
357-
if cls == self {
362+
if cls == self && filter(&inject_func) {
358363
out.push(inject_func);
359364
}
360365
}
@@ -377,10 +382,12 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
377382
}
378383
}
379384

380-
pub fn fields(&self) -> Vec<Field<'tu, 'ge>> {
385+
pub fn fields(&self, filter: impl Fn(&Field) -> bool) -> Vec<Field<'tu, 'ge>> {
381386
let mut out = Vec::with_capacity(32);
382387
self.for_each_field(|f| {
383-
out.push(f);
388+
if filter(&f) {
389+
out.push(f);
390+
}
384391
ControlFlow::Continue(())
385392
});
386393
out
@@ -405,16 +412,13 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
405412

406413
pub fn field_methods<'f>(
407414
&self,
408-
fields: impl Iterator<Item = &'f Field<'tu, 'ge>>,
415+
fields: &'f [Field<'tu, 'ge>],
409416
constness_filter: Option<Constness>,
410-
) -> Vec<Func<'tu, 'ge>>
411-
where
412-
'tu: 'f,
413-
'ge: 'f,
414-
{
417+
) -> impl Iterator<Item = Func<'tu, 'ge>> + 'f {
415418
match self {
416419
&Self::Clang { gen_env, .. } => {
417-
let accessor_generator = |fld: &Field<'tu, 'ge>| {
420+
let cls = self.clone();
421+
let accessor_generator = move |fld: &Field<'tu, 'ge>| {
418422
let doc_comment = Rc::from(fld.doc_comment());
419423
let def_loc = fld.file_line_name().location;
420424
let rust_module = Rc::from(fld.rust_module());
@@ -449,7 +453,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
449453
let read_const_func = if constness_filter.map_or(true, |c| c.is_const()) {
450454
Some(Func::new_desc(
451455
FuncDesc::new(
452-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
456+
FuncKind::FieldAccessor(cls.clone(), fld.clone()),
453457
Constness::Const,
454458
return_kind,
455459
fld_declname,
@@ -468,7 +472,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
468472
let read_mut_func = if constness_filter.map_or(true, |c| c.is_mut()) {
469473
Some(Func::new_desc(
470474
FuncDesc::new(
471-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
475+
FuncKind::FieldAccessor(cls.clone(), fld.clone()),
472476
Constness::Mut,
473477
return_kind,
474478
format!("{fld_declname}Mut"),
@@ -489,7 +493,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
489493
let single_read_func = if constness_filter.map_or(true, |c| c == fld_const) {
490494
Some(Func::new_desc(
491495
FuncDesc::new(
492-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
496+
FuncKind::FieldAccessor(cls.clone(), fld.clone()),
493497
fld_const,
494498
return_kind,
495499
fld_declname,
@@ -518,7 +522,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
518522
let (first_letter, rest) = fld_declname.capitalize_first_ascii_letter().expect("Empty fld_declname");
519523
Some(Func::new_desc(
520524
FuncDesc::new(
521-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
525+
FuncKind::FieldAccessor(cls.clone(), fld.clone()),
522526
Constness::Mut,
523527
ReturnKind::InfallibleNaked,
524528
format!("set{first_letter}{rest}"),
@@ -545,9 +549,9 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
545549
.or_else(|| write_yield.take())
546550
})
547551
};
548-
fields.flat_map(accessor_generator).collect()
552+
FieldMethodsIter::Clang(fields.iter().flat_map(accessor_generator))
549553
}
550-
Self::Desc(_) => vec![],
554+
Self::Desc(_) => FieldMethodsIter::Desc,
551555
}
552556
}
553557

@@ -574,15 +578,13 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
574578

575579
pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
576580
self
577-
.fields()
581+
.fields(|f| f.exclude_kind().is_included())
578582
.into_iter()
579-
.filter(|f| f.exclude_kind().is_included())
580583
.flat_map(|f| f.type_ref().generated_types())
581584
.chain(
582585
self
583-
.methods()
586+
.methods(|m| m.exclude_kind().is_included())
584587
.into_iter()
585-
.filter(|m| m.exclude_kind().is_included())
586588
.flat_map(|m| m.generated_types()),
587589
)
588590
.collect()
@@ -837,3 +839,19 @@ impl<'tu, 'ge> TemplateKind<'tu, 'ge> {
837839
}
838840
}
839841
}
842+
843+
pub enum FieldMethodsIter<'tu: 'ge, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> {
844+
Clang(I),
845+
Desc,
846+
}
847+
848+
impl<'tu, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> Iterator for FieldMethodsIter<'tu, 'ge, I> {
849+
type Item = Func<'tu, 'ge>;
850+
851+
fn next(&mut self) -> Option<Self::Item> {
852+
match self {
853+
FieldMethodsIter::Clang(iter) => iter.next(),
854+
FieldMethodsIter::Desc => None,
855+
}
856+
}
857+
}

binding-generator/src/constant.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ pub fn render_constant_cpp(tokens: &[Token]) -> String {
8080

8181
pub fn render_evaluation_result_rust(result: EvaluationResult) -> Value {
8282
match result {
83-
EvaluationResult::Unexposed => {
84-
panic!("Can't render evaluation result")
85-
}
83+
EvaluationResult::Unexposed => panic!("Can't render evaluation result"),
8684
EvaluationResult::SignedInteger(x) => Value {
8785
kind: ValueKind::Integer,
8886
value: x.to_string(),
@@ -135,9 +133,7 @@ impl<'tu> Const<'tu> {
135133
.to_string(),
136134
}),
137135
EntityKind::VarDecl => self.entity.evaluate().map(render_evaluation_result_rust),
138-
_ => {
139-
unreachable!("Invalid entity type for constant")
140-
}
136+
_ => unreachable!("Invalid entity type for constant"),
141137
}
142138
}
143139
}
@@ -187,6 +183,7 @@ impl<'me> NameDebug<'me> for &'me Const<'_> {
187183
pub enum ValueKind {
188184
Integer,
189185
UnsignedInteger,
186+
Usize,
190187
Float,
191188
Double,
192189
String,

binding-generator/src/element.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl DefaultElement {
3737
}
3838

3939
pub fn is_public(entity: Entity) -> bool {
40+
// MSRV: use `is_none_or` when MSRV is 1.82
4041
entity.get_accessibility().map_or(true, |a| Accessibility::Public == a)
4142
}
4243

binding-generator/src/func.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,10 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
427427

428428
pub fn is_clone(&self) -> bool {
429429
if self.cpp_name(CppNameStyle::Declaration) == "clone" {
430-
if let Some(c) = self.kind().as_instance_method() {
431-
!self.has_arguments() && self.return_type_ref().kind().as_class().is_some_and(|r| r.as_ref() == c)
432-
} else {
433-
false
434-
}
430+
self
431+
.kind()
432+
.as_instance_method()
433+
.is_some_and(|c| !self.has_arguments() && self.return_type_ref().kind().as_class().is_some_and(|r| r.as_ref() == c))
435434
} else {
436435
false
437436
}
@@ -449,7 +448,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
449448
&Self::Clang { entity, gen_env, .. } => {
450449
let mut out = match self.kind().as_ref() {
451450
FuncKind::Constructor(cls) => cls.type_ref(),
452-
// `operator =` returns a reference to the `self` value and it's quite cumbersome to handle correctly
451+
// `operator =` returns a reference to the `self` value, and it's quite cumbersome to handle correctly
453452
FuncKind::InstanceOperator(_, OperatorKind::Set) => TypeRefDesc::void(),
454453
FuncKind::Function
455454
| FuncKind::InstanceMethod(..)

binding-generator/src/generator.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,13 @@ pub struct Generator {
361361

362362
impl Drop for Generator {
363363
fn drop(&mut self) {
364-
#[cfg(any(not(windows), feature = "clang-runtime"))]
365-
{
364+
if !(cfg!(windows) && cfg!(feature = "clang-runtime") && clang::get_version().contains(" 19.")) {
366365
// `clang` has an issue on Windows when running with `runtime` feature and clang-19:
367366
// https://github.com/KyleMayes/clang-rs/issues/63
368367
// So we avoid dropping clang in that case as a workaround.
369368
// `clang::get_version()` is string like "Apple clang version 15.0.0 (clang-1500.1.0.2.5)"
370-
if !clang::get_version().contains(" 19.") {
371-
unsafe {
372-
ManuallyDrop::drop(&mut self.clang);
373-
}
369+
unsafe {
370+
ManuallyDrop::drop(&mut self.clang);
374371
}
375372
}
376373
}
@@ -448,8 +445,7 @@ impl Generator {
448445
.collect::<Vec<_>>();
449446
args.push("-DOCVRS_PARSING_HEADERS".into());
450447
args.push("-includeocvrs_common.hpp".into());
451-
// need to have c++14 here because VS headers contain features that require it
452-
args.push("-std=c++14".into());
448+
args.push("-std=c++17".into());
453449
// allow us to use some custom clang args
454450
let clang_arg = env::var_os("OPENCV_CLANG_ARGS");
455451
if let Some(clang_arg) = clang_arg.as_ref().and_then(|s| s.to_str()) {

binding-generator/src/iterator_ext.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::StringExt;
22

33
pub trait IteratorExt {
4-
fn join(&mut self, sep: &str) -> String;
4+
fn join(self, sep: &str) -> String;
55
}
66

77
impl<T: Iterator<Item = impl AsRef<str>>> IteratorExt for T {
8-
fn join(&mut self, sep: &str) -> String {
8+
fn join(self, sep: &str) -> String {
99
let mut out = String::new();
1010
out.extend_join(self, sep);
1111
out

binding-generator/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
3434
pub use iterator_ext::IteratorExt;
3535
pub use map_borrowed::CowMapBorrowedExt;
3636
use memoize::{MemoizeMap, MemoizeMapExt};
37-
use name_pool::NamePool;
3837
use smart_ptr::SmartPtr;
3938
pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
4039
use tuple::Tuple;

binding-generator/src/name_pool.rs

-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ impl NamePool {
2424
out
2525
}
2626

27-
pub fn add_name(&mut self, name: impl Into<Cow<'static, str>>) -> MakeUniqueNameResult {
28-
let mut name = name.into();
29-
self.make_unique_name(&mut name)
30-
}
31-
3227
pub fn into_disambiguator<T, I, CB>(mut self, args: I, mut name_cb: CB) -> impl Iterator<Item = (String, T)>
3328
where
3429
I: IntoIterator<Item = T>,

binding-generator/src/settings.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub use argument_override::{
4646
arg_override_factory, property_override_factory, return_override_factory, ArgOverride, PropertyOverride, ReturnOverride,
4747
ARG_OVERRIDE_SELF,
4848
};
49+
pub use const_tweak::CONST_TYPE_OVERRIDE;
4950
pub use element_exclude_kind::ELEMENT_EXCLUDE_KIND;
5051
pub use element_export_tweak::ELEMENT_EXPORT_TWEAK;
5152
pub use force_infallible::{force_infallible_factory, ForceInfallible};
@@ -70,6 +71,7 @@ use crate::type_ref::TypeRef;
7071

7172
mod argument_names;
7273
mod argument_override;
74+
mod const_tweak;
7375
mod element_exclude_kind;
7476
mod element_export_tweak;
7577
mod force_infallible;
@@ -168,9 +170,6 @@ impl Settings {
168170
}
169171
}
170172

171-
// fixme, generalize, make it use constant::ValueKind
172-
pub static CONST_TYPE_USIZE: Lazy<HashSet<&str>> = Lazy::new(|| HashSet::from(["Mat_AUTO_STEP"]));
173-
174173
/// map of reserved Rust keywords and their replacement to be used in var, function and class names
175174
/// key: reserved keyword
176175
/// value: replacement
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::collections::HashMap;
2+
3+
use once_cell::sync::Lazy;
4+
5+
use crate::constant::ValueKind;
6+
7+
pub static CONST_TYPE_OVERRIDE: Lazy<HashMap<&str, ValueKind>> =
8+
Lazy::new(|| HashMap::from([("Mat_AUTO_STEP", ValueKind::Usize)]));

0 commit comments

Comments
 (0)