Skip to main content

opencv_binding_generator/
class.rs

1use std::borrow::Cow;
2use std::collections::HashSet;
3use std::hash::{Hash, Hasher};
4use std::ops::ControlFlow;
5use std::rc::Rc;
6use std::{fmt, iter};
7
8use clang::{Accessibility, Entity, EntityKind};
9pub use desc::ClassDesc;
10
11use crate::debug::{DefinitionLocation, LocationName};
12use crate::element::ExcludeKind;
13use crate::entity::{ControlFlowExt, ToEntity};
14use crate::field::FieldDesc;
15use crate::func::{FuncCppBody, FuncDesc, FuncKind, ReturnKind};
16use crate::settings::PropertyReadWrite;
17use crate::type_ref::{Constness, CppNameStyle, StrEnc, StrType, TypeRef, TypeRefDesc, TypeRefTypeHint};
18use crate::writer::rust_native::element::RustElement;
19use crate::{
20	settings, ClassKindOverride, Const, DefaultElement, Element, EntityExt, Enum, Field, Func, GeneratedType, GeneratorEnv,
21	NameDebug, StrExt,
22};
23
24mod desc;
25
26#[derive(Clone)]
27pub enum Class<'tu, 'ge> {
28	Clang {
29		entity: Entity<'tu>,
30		custom_fullname: Option<Rc<str>>,
31		gen_env: &'ge GeneratorEnv<'tu>,
32	},
33	Desc(Rc<ClassDesc<'tu, 'ge>>),
34}
35
36impl<'tu, 'ge> Class<'tu, 'ge> {
37	pub fn new(entity: Entity<'tu>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
38		Self::Clang {
39			entity,
40			custom_fullname: None,
41			gen_env,
42		}
43	}
44
45	pub fn new_ext(entity: Entity<'tu>, custom_fullname: impl Into<Rc<str>>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
46		Self::Clang {
47			entity,
48			custom_fullname: Some(custom_fullname.into()),
49			gen_env,
50		}
51	}
52
53	pub fn new_desc(desc: ClassDesc<'tu, 'ge>) -> Self {
54		Self::Desc(Rc::new(desc))
55	}
56
57	/// Checks whether a class can be simple on Rust side, i.e. represented by plain struct with fields
58	pub fn can_be_simple(&self) -> bool {
59		let cpp_refname = self.cpp_name(CppNameStyle::Reference);
60		settings::IMPLEMENTED_GENERICS.contains(cpp_refname.as_ref())
61			|| self.has_fields()
62				&& !self.has_descendants()
63				&& !self.has_bases()
64				&& !self
65					.for_each_field(|field| {
66						let type_ref = field.type_ref();
67						ControlFlow::continue_until(!type_ref.kind().is_copy(type_ref.type_hint()))
68					})
69					.is_break()
70	}
71
72	pub fn kind(&self) -> ClassKind {
73		match self {
74			&Self::Clang { entity, gen_env, .. } => {
75				if settings::ELEMENT_EXCLUDE_KIND
76					.get(self.cpp_name(CppNameStyle::Reference).as_ref())
77					.is_some_and(|ek| ek.is_excluded())
78				{
79					return ClassKind::Other;
80				}
81				match gen_env.get_export_config(entity).map(|c| c.class_kind_override) {
82					Some(ClassKindOverride::Simple) => {
83						if self.can_be_simple() {
84							ClassKind::Simple
85						} else {
86							ClassKind::BoxedForced
87						}
88					}
89					Some(ClassKindOverride::Boxed) => ClassKind::Boxed,
90					Some(ClassKindOverride::BoxedForced) => ClassKind::BoxedForced,
91					Some(ClassKindOverride::System) => ClassKind::System,
92					None => {
93						if self.is_system() {
94							ClassKind::System
95						} else if let Some(kind) = gen_env.get_class_kind(entity) {
96							match kind {
97								ClassKind::Simple if !self.can_be_simple() => ClassKind::BoxedForced,
98								_ => kind,
99							}
100						} else {
101							ClassKind::Other
102						}
103					}
104				}
105			}
106			Self::Desc(desc) => desc.kind,
107		}
108	}
109
110	pub fn type_ref(&self) -> TypeRef<'tu, 'ge> {
111		match self {
112			&Self::Clang { entity, gen_env, .. } => TypeRef::new(entity.get_type().expect("Can't get class type"), gen_env),
113			Self::Desc(desc) => TypeRef::guess(desc.cpp_fullname.as_ref(), desc.rust_module),
114		}
115	}
116
117	/// Returns `Some` with the string type if the current class name refers to a C++ `std::string` or `cv::String`
118	pub fn string_type(&self) -> Option<StrType> {
119		let cpp_refname = self.cpp_name(CppNameStyle::Reference);
120		if cpp_refname.starts_with("std::") && cpp_refname.ends_with("::string") {
121			Some(StrType::StdString(StrEnc::Text))
122		} else if cpp_refname == "cv::String" {
123			Some(StrType::CvString(StrEnc::Text))
124		} else {
125			None
126		}
127	}
128
129	pub fn template_kind(&self) -> TemplateKind<'tu, 'ge> {
130		match self {
131			&Self::Clang { entity, gen_env, .. } => {
132				if entity.get_template_kind().is_some() {
133					TemplateKind::Template
134				} else if let Some(template_entity) = entity.get_template() {
135					TemplateKind::Specialization(Self::new(template_entity, gen_env))
136				} else {
137					TemplateKind::No
138				}
139			}
140			Self::Desc(desc) => desc.template_kind.clone(),
141		}
142	}
143
144	pub fn is_abstract(&self) -> bool {
145		match self {
146			&Self::Clang { entity, .. } => entity.is_abstract_record(),
147			Self::Desc(desc) => desc.is_abstract,
148		}
149	}
150
151	/// True if a class has virtual methods
152	pub fn is_polymorphic(&self) -> bool {
153		match self {
154			Self::Clang { entity, .. } => entity
155				.walk_methods_while(|f| ControlFlow::continue_until(f.is_virtual_method() || f.is_pure_virtual_method()))
156				.is_break(),
157			Self::Desc(_) => false,
158		}
159	}
160
161	/// Special case of an empty class with only an anonymous enum inside (e.g., DrawLinesMatchesFlags)
162	pub fn as_enum(&self) -> Option<Enum<'tu, 'ge>> {
163		match self {
164			&Self::Clang { entity, gen_env, .. } => {
165				if !self.has_methods() && !self.has_fields() && !self.has_descendants() && !self.has_bases() {
166					let children = entity.get_children();
167					if let [single] = children.as_slice() {
168						if matches!(single.get_kind(), EntityKind::EnumDecl) {
169							Some(Enum::new_ext(
170								*single,
171								self.cpp_name(CppNameStyle::Declaration).as_ref(),
172								gen_env,
173							))
174						} else {
175							None
176						}
177					} else {
178						None
179					}
180				} else {
181					None
182				}
183			}
184			Self::Desc(_) => None,
185		}
186	}
187
188	/// Class has an explicit method named `clone()`
189	pub fn has_explicit_clone(&self) -> bool {
190		self.for_each_method(|m| ControlFlow::continue_until(m.is_clone())).is_break()
191	}
192
193	/// Class is simple (i.e. constructor-copiable in C++), but can't be simple in Rust
194	pub fn has_implicit_clone(&self) -> bool {
195		!self.is_abstract() && matches!(self.kind(), ClassKind::BoxedForced) && !self.has_virtual_destructor()
196	}
197
198	pub fn has_implicit_default_constructor(&self) -> bool {
199		match self {
200			&Self::Clang { entity, .. } => !entity
201				.walk_children_while(|f| {
202					if matches!(f.get_kind(), EntityKind::Constructor) {
203						ControlFlow::Break(())
204					} else {
205						ControlFlow::Continue(())
206					}
207				})
208				.is_break(),
209			Self::Desc(_) => false,
210		}
211	}
212
213	pub fn has_virtual_destructor(&self) -> bool {
214		match self {
215			Class::Clang { entity, .. } => entity
216				.walk_children_while(|f| ControlFlow::continue_until(f.get_kind() == EntityKind::Destructor && f.is_virtual_method()))
217				.is_break(),
218			Class::Desc(_) => false,
219		}
220	}
221
222	pub fn has_private_destructor(&self) -> bool {
223		match self {
224			Class::Clang { entity, .. } => entity
225				.walk_children_while(|f| {
226					ControlFlow::continue_until(
227						f.get_kind() == EntityKind::Destructor && f.get_accessibility() != Some(Accessibility::Public),
228					)
229				})
230				.is_break(),
231			Class::Desc(_) => false,
232		}
233	}
234
235	pub fn has_bases(&self) -> bool {
236		match self {
237			&Self::Clang { entity, .. } => entity.walk_bases_while(|_| ControlFlow::Break(())).is_break(),
238			Self::Desc(desc) => !desc.bases.is_empty(),
239		}
240	}
241
242	pub fn bases(&self) -> Cow<'_, [Class<'tu, 'ge>]> {
243		match self {
244			&Self::Clang { entity, gen_env, .. } => {
245				let mut out = vec![];
246				let entity = entity.get_template().unwrap_or(entity);
247				let _ = entity.walk_bases_while(|child| {
248					out.push(Self::new(Self::definition_entity(child), gen_env));
249					ControlFlow::Continue(())
250				});
251				out.into()
252			}
253			Self::Desc(desc) => desc.bases.as_ref().into(),
254		}
255	}
256
257	pub fn all_bases(&self) -> HashSet<Class<'tu, 'ge>> {
258		#![expect(clippy::mutable_key_type)]
259		self
260			.bases()
261			.into_owned()
262			.into_iter()
263			.flat_map(|b| {
264				let mut out = b.all_bases();
265				out.insert(b);
266				out
267			})
268			.collect()
269	}
270
271	pub fn has_descendants(&self) -> bool {
272		match self {
273			&Self::Clang { gen_env, .. } => gen_env.descendants_of(&self.cpp_name(CppNameStyle::Reference)).is_some(),
274			Self::Desc(_) => false,
275		}
276	}
277
278	pub fn descendants(&self) -> HashSet<Class<'tu, 'ge>> {
279		#![expect(clippy::mutable_key_type)]
280		match self {
281			&Self::Clang { gen_env, .. } => gen_env
282				.descendants_of(&self.cpp_name(CppNameStyle::Reference))
283				.into_iter()
284				.flat_map(|desc| desc.iter().map(|e| Self::new(*e, gen_env)))
285				.collect(),
286			Self::Desc(_) => HashSet::new(),
287		}
288	}
289
290	pub fn all_descendants(&self) -> HashSet<Class<'tu, 'ge>> {
291		#![expect(clippy::mutable_key_type)]
292		self
293			.descendants()
294			.into_iter()
295			.flat_map(|descendant| {
296				let mut out = descendant.all_descendants();
297				out.insert(descendant);
298				out
299			})
300			.collect()
301	}
302
303	pub fn all_family(&self) -> HashSet<Class<'tu, 'ge>> {
304		#![expect(clippy::mutable_key_type)]
305		fn collect<'tu, 'ge>(out: &mut HashSet<Class<'tu, 'ge>>, cls: Class<'tu, 'ge>) {
306			if out.insert(cls.clone()) {
307				for base in cls.bases().into_owned() {
308					collect(out, base);
309				}
310				for desc in cls.descendants() {
311					collect(out, desc);
312				}
313			}
314		}
315
316		let mut out = HashSet::new();
317		collect(&mut out, self.clone());
318		out
319	}
320
321	pub fn has_methods(&self) -> bool {
322		self.for_each_method(|_| ControlFlow::Break(())).is_break()
323	}
324
325	#[inline]
326	pub fn for_each_method(&self, mut predicate: impl FnMut(Func<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
327		match self {
328			&Self::Clang { entity, gen_env, .. } => entity.walk_methods_while(|f| predicate(Func::new(f, gen_env))),
329			Self::Desc(_) => ControlFlow::Continue(()),
330		}
331	}
332
333	pub fn methods(&self, filter: impl Fn(&Func) -> bool) -> Vec<Func<'tu, 'ge>> {
334		match self {
335			Class::Clang { entity, gen_env, .. } => {
336				let mut out = Vec::with_capacity(32);
337				let _ = entity.walk_methods_while(|func_entity| {
338					let func = Func::new(func_entity, gen_env);
339					let func: Func = if let Some(func_fact) = gen_env.settings.func_replace.get(&mut func.matcher()) {
340						func_fact(&func)
341					} else {
342						func
343					};
344					if func.is_generic() {
345						if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
346							for spec in specs {
347								let spec_func = func.clone().specialize(spec);
348								if filter(&spec_func) {
349									out.push(spec_func);
350								}
351							}
352							return ControlFlow::Continue(());
353						}
354					}
355					if filter(&func) {
356						out.push(func);
357					}
358					ControlFlow::Continue(())
359				});
360				for inject_func_fact in &gen_env.settings.func_inject {
361					let inject_func: Func = inject_func_fact();
362					if let Some(cls) = inject_func.kind().as_class_method() {
363						if cls == self && filter(&inject_func) {
364							out.push(inject_func);
365						}
366					}
367				}
368				out
369			}
370			Class::Desc(_) => vec![],
371		}
372	}
373
374	pub fn has_fields(&self) -> bool {
375		self.for_each_field(|_| ControlFlow::Break(())).is_break()
376	}
377
378	#[inline]
379	pub fn for_each_field(&self, mut predicate: impl FnMut(Field<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
380		match self {
381			&Self::Clang { entity, gen_env, .. } => entity.walk_fields_while(|f| predicate(Field::new(f, gen_env))),
382			Self::Desc(_) => ControlFlow::Continue(()),
383		}
384	}
385
386	pub fn fields(&self, filter: impl Fn(&Field) -> bool) -> Vec<Field<'tu, 'ge>> {
387		let mut out = Vec::with_capacity(32);
388		let _ = self.for_each_field(|f| {
389			if filter(&f) {
390				out.push(f);
391			}
392			ControlFlow::Continue(())
393		});
394		out
395	}
396
397	#[inline]
398	pub fn for_each_const(&self, mut predicate: impl FnMut(Const<'tu>) -> ControlFlow<()>) -> ControlFlow<()> {
399		match self {
400			&Self::Clang { entity, .. } => entity.walk_consts_while(|f| predicate(Const::new(f))),
401			Self::Desc(_) => ControlFlow::Continue(()),
402		}
403	}
404
405	pub fn consts(&self) -> Vec<Const<'tu>> {
406		let mut out = Vec::with_capacity(8);
407		let _ = self.for_each_const(|c| {
408			out.push(c);
409			ControlFlow::Continue(())
410		});
411		out
412	}
413
414	pub fn field_methods<'f>(
415		&self,
416		fields: &'f [Field<'tu, 'ge>],
417		constness_filter: Option<Constness>,
418	) -> impl Iterator<Item = Func<'tu, 'ge>> + 'f {
419		match self {
420			&Self::Clang { gen_env, .. } => {
421				let cls = self.clone();
422				let accessor_generator = move |fld: &Field<'tu, 'ge>| {
423					let doc_comment = Rc::from(fld.doc_comment());
424					let def_loc = fld.file_line_name().location;
425					let rust_module = fld.rust_module();
426					let mut fld_type_ref = fld.type_ref();
427					let fld_refname = fld.cpp_name(CppNameStyle::Reference);
428					if let Some(type_hint) = gen_env.settings.property_override.get(fld_refname.as_ref()) {
429						fld_type_ref.to_mut().set_type_hint(type_hint.clone());
430					} else {
431						let fld_type_kind = fld_type_ref.kind();
432						if fld_type_kind
433							.as_pointer()
434							.is_some_and(|inner| inner.kind().as_primitive().is_some())
435							&& !fld_type_kind.is_char_ptr_string(fld_type_ref.type_hint())
436						{
437							fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
438						} else if fld_type_kind.as_class().is_some_and(|cls| cls.kind().is_trait()) {
439							fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::TraitClassConcrete);
440						}
441					}
442					let fld_type_kind = fld_type_ref.kind();
443					let return_kind = ReturnKind::infallible(fld_type_kind.return_as_naked(fld_type_ref.type_hint()));
444					let fld_const = fld.constness();
445					let passed_by_ref = fld_type_kind.can_return_as_direct_reference();
446					let prop_tweak = gen_env.settings.property_tweaks.get(fld_refname.as_ref());
447					let rust_custom_leafname = prop_tweak.and_then(|tweak| tweak.rename);
448					let read_write = prop_tweak
449						.and_then(|tweak| tweak.read_write)
450						.unwrap_or(PropertyReadWrite::ReadWrite);
451					let fld_declname = fld_refname.localname();
452					let (mut read_const_yield, mut read_mut_yield) = if read_write.is_read() {
453						if fld_const.is_mut() && passed_by_ref {
454							let read_const_func = if constness_filter.is_none_or(|c| c.is_const()) {
455								Some(Func::new_desc(
456									FuncDesc::new(
457										FuncKind::FieldAccessor(cls.clone(), fld.clone()),
458										Constness::Const,
459										return_kind,
460										fld_declname,
461										rust_module,
462										[],
463										fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
464									)
465									.def_loc(def_loc.clone())
466									.doc_comment(Rc::clone(&doc_comment))
467									.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
468									.maybe_rust_custom_leafname(rust_custom_leafname),
469								))
470							} else {
471								None
472							};
473							let read_mut_func = if constness_filter.is_none_or(|c| c.is_mut()) {
474								Some(Func::new_desc(
475									FuncDesc::new(
476										FuncKind::FieldAccessor(cls.clone(), fld.clone()),
477										Constness::Mut,
478										return_kind,
479										format!("{fld_declname}Mut"),
480										rust_module,
481										[],
482										fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
483									)
484									.def_loc(def_loc.clone())
485									.doc_comment(Rc::clone(&doc_comment))
486									.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
487									.maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("{name}_mut"))),
488								))
489							} else {
490								None
491							};
492							(read_const_func, read_mut_func)
493						} else {
494							let single_read_func = if constness_filter.is_none_or(|c| c == fld_const) {
495								Some(Func::new_desc(
496									FuncDesc::new(
497										FuncKind::FieldAccessor(cls.clone(), fld.clone()),
498										fld_const,
499										return_kind,
500										fld_declname,
501										rust_module,
502										[],
503										fld_type_ref.as_ref().clone(),
504									)
505									.def_loc(def_loc.clone())
506									.doc_comment(Rc::clone(&doc_comment))
507									.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
508									.maybe_rust_custom_leafname(rust_custom_leafname),
509								))
510							} else {
511								None
512							};
513							(single_read_func, None)
514						}
515					} else {
516						(None, None)
517					};
518					let mut write_yield = if read_write.is_write()
519						&& constness_filter.is_none_or(|c| c.is_mut())
520						&& !fld_type_ref.constness().is_const()
521						&& !fld_type_kind.as_fixed_array().is_some()
522					{
523						let (first_letter, rest) = fld_declname.capitalize_first_ascii_letter().expect("Empty fld_declname");
524						Some(Func::new_desc(
525							FuncDesc::new(
526								FuncKind::FieldAccessor(cls.clone(), fld.clone()),
527								Constness::Mut,
528								ReturnKind::InfallibleNaked,
529								format!("set{first_letter}{rest}"),
530								rust_module,
531								[Field::new_desc(FieldDesc {
532									cpp_fullname: "val".into(),
533									type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
534									default_value: fld.default_value().map(|v| v.into()),
535								})],
536								TypeRefDesc::void(),
537							)
538							.doc_comment(doc_comment)
539							.def_loc(def_loc)
540							.cpp_body(FuncCppBody::ManualCall("{{name}} = {{args}}".into()))
541							.maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("set_{name}"))),
542						))
543					} else {
544						None
545					};
546					iter::from_fn(move || {
547						read_const_yield
548							.take()
549							.or_else(|| read_mut_yield.take())
550							.or_else(|| write_yield.take())
551					})
552				};
553				FieldMethodsIter::Clang(fields.iter().flat_map(accessor_generator))
554			}
555			Self::Desc(_) => FieldMethodsIter::Desc,
556		}
557	}
558
559	/// Returns an entity that defines current class, for specialized classes (Point_<int>) it's the template (Point_<T>), for
560	/// not fully defined classes it goes to its definition location.
561	fn definition_entity(entity: Entity<'tu>) -> Entity<'tu> {
562		entity.get_template().unwrap_or(entity).get_definition().unwrap_or(entity)
563	}
564
565	pub fn is_definition(&self) -> bool {
566		match self {
567			&Self::Clang { entity, .. } => {
568				let class_loc = entity.get_location();
569				let def_loc = entity.get_definition().and_then(|d| d.get_location());
570				match (class_loc, def_loc) {
571					(Some(class_loc), Some(def_loc)) => class_loc == def_loc,
572					(_, None) => false,
573					_ => true,
574				}
575			}
576			Self::Desc(_) => true,
577		}
578	}
579
580	pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
581		self
582			.fields(|f| f.exclude_kind().is_included())
583			.into_iter()
584			.flat_map(|f| f.type_ref().generated_types())
585			.chain(
586				self
587					.methods(|m| m.exclude_kind().is_included())
588					.into_iter()
589					.flat_map(|m| m.generated_types()),
590			)
591			.collect()
592	}
593}
594
595impl<'tu> ToEntity<'tu> for &Class<'tu, '_> {
596	fn to_entity(self) -> Option<Entity<'tu>> {
597		match self {
598			Class::Clang { entity, .. } => Some(*entity),
599			Class::Desc(_) => None,
600		}
601	}
602}
603
604impl Element for Class<'_, '_> {
605	fn exclude_kind(&self) -> ExcludeKind {
606		match self {
607			Self::Clang { .. } => DefaultElement::exclude_kind(self)
608				.with_is_ignored(|| match self.kind() {
609					ClassKind::Other => true,
610					ClassKind::System => {
611						!settings::IMPLEMENTED_SYSTEM_CLASSES.contains(self.cpp_name(CppNameStyle::Reference).as_ref())
612					}
613					ClassKind::Simple | ClassKind::Boxed | ClassKind::BoxedForced => match self.template_kind() {
614						TemplateKind::Template => true,
615						TemplateKind::No => !self.is_definition() || self.cpp_namespace() == "",
616						TemplateKind::Specialization(_) => {
617							!settings::IMPLEMENTED_GENERICS.contains(self.cpp_name(CppNameStyle::Reference).as_ref())
618						}
619					},
620				})
621				.with_is_excluded(|| match self.kind() {
622					ClassKind::System | ClassKind::Other => true,
623					ClassKind::Simple => false,
624					ClassKind::Boxed | ClassKind::BoxedForced => self.has_private_destructor(),
625				}),
626			Self::Desc(desc) => desc.exclude_kind,
627		}
628	}
629
630	fn is_system(&self) -> bool {
631		match self {
632			&Self::Clang { entity, .. } => DefaultElement::is_system(entity),
633			Self::Desc(desc) => matches!(desc.kind, ClassKind::System),
634		}
635	}
636
637	fn is_public(&self) -> bool {
638		match self {
639			&Self::Clang { entity, .. } => DefaultElement::is_public(entity),
640			Self::Desc(desc) => desc.is_public,
641		}
642	}
643
644	fn doc_comment(&self) -> Cow<'_, str> {
645		match self {
646			Self::Clang { entity, .. } => entity.doc_comment(),
647			Self::Desc(_) => "".into(),
648		}
649	}
650
651	fn cpp_namespace(&self) -> Cow<'_, str> {
652		#[inline(always)]
653		fn inner(cpp_fullname: &str) -> Cow<'_, str> {
654			cpp_fullname.namespace().into()
655		}
656
657		match self {
658			Self::Clang {
659				custom_fullname: Some(cpp_fullname),
660				..
661			} => inner(cpp_fullname.as_ref()),
662			Self::Clang { entity, .. } => DefaultElement::cpp_namespace(*entity).into(),
663			Self::Desc(desc) => inner(desc.cpp_fullname.as_ref()),
664		}
665	}
666
667	fn cpp_name(&self, style: CppNameStyle) -> Cow<'_, str> {
668		match self {
669			Self::Clang {
670				custom_fullname: Some(cpp_fullname),
671				..
672			} => cpp_fullname.cpp_name_from_fullname(style).into(),
673			&Self::Clang { entity, .. } => DefaultElement::cpp_name(self, entity, style),
674			Self::Desc(desc) => desc.cpp_fullname.cpp_name_from_fullname(style).into(),
675		}
676	}
677}
678
679impl Hash for Class<'_, '_> {
680	fn hash<H: Hasher>(&self, state: &mut H) {
681		match self {
682			Self::Clang { entity, .. } => entity.hash(state),
683			Self::Desc(desc) => desc.hash(state),
684		}
685	}
686}
687
688impl PartialEq for Class<'_, '_> {
689	fn eq(&self, other: &Self) -> bool {
690		self.cpp_name(CppNameStyle::Reference) == other.cpp_name(CppNameStyle::Reference) && self.kind() == other.kind()
691	}
692}
693
694impl Eq for Class<'_, '_> {}
695
696impl<'me> NameDebug<'me> for &'me Class<'me, '_> {
697	fn file_line_name(self) -> LocationName<'me> {
698		match self {
699			Class::Clang { entity, .. } => entity.file_line_name(),
700			Class::Desc(desc) => LocationName::new(DefinitionLocation::Generated, desc.cpp_fullname.as_ref()),
701		}
702	}
703}
704
705impl fmt::Debug for Class<'_, '_> {
706	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
707		let mut props = vec![];
708		if self.can_be_simple() {
709			props.push("can_be_simple");
710		}
711		if self.template_kind().is_template() {
712			props.push("template");
713		}
714		if self.template_kind().as_template_specialization().is_some() {
715			props.push("template_specialization");
716		}
717		if self.is_abstract() {
718			props.push("abstract");
719		}
720		if self.is_polymorphic() {
721			props.push("polymorphic");
722		}
723		if self.kind().is_trait() {
724			props.push("trait");
725		}
726		if self.as_enum().is_some() {
727			props.push("enum");
728		}
729		if self.has_explicit_clone() {
730			props.push("has_explicit_clone");
731		}
732		if self.has_implicit_clone() {
733			props.push("has_implicit_clone");
734		}
735		if self.has_virtual_destructor() {
736			props.push("has_virtual_dtor");
737		}
738		if self.has_private_destructor() {
739			props.push("has_private_dtor")
740		}
741		if self.has_bases() {
742			props.push("has_bases");
743		}
744		if self.has_descendants() {
745			props.push("has_descendants");
746		}
747		if self.has_methods() {
748			props.push("has_methods");
749		}
750		if self.has_fields() {
751			props.push("has_fields");
752		}
753		if !self.consts().is_empty() {
754			props.push("has_consts");
755		}
756		if self.is_definition() {
757			props.push("definition");
758		}
759		if matches!(
760			self,
761			Self::Clang {
762				custom_fullname: Some(_),
763				..
764			}
765		) {
766			props.push("custom_fullname");
767		}
768		let mut debug_struct = f.debug_struct(match self {
769			Self::Clang { .. } => "Class::Clang",
770			Self::Desc(_) => "Class::Desc",
771		});
772		self
773			.update_debug_struct(&mut debug_struct)
774			.field("kind", &self.kind())
775			.field("props", &props.join(", "))
776			.field("string_type", &self.string_type())
777			.finish()
778	}
779}
780
781#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
782pub enum ClassKind {
783	/// Simple class, check [`Class::can_be_simple`] for more details
784	Simple,
785	/// Opaque class where all access to its fields is happening by C++ side using a pointer
786	Boxed,
787	/// Marked simple but forced to be boxed by presence of non-simple fields or descendants
788	BoxedForced,
789	/// System class like `std::string`
790	System,
791	/// Class is something else, generally ignored
792	Other,
793}
794
795impl ClassKind {
796	pub fn is_simple(self) -> bool {
797		match self {
798			Self::Simple => true,
799			Self::Boxed | Self::BoxedForced | Self::System | Self::Other => false,
800		}
801	}
802
803	pub fn is_boxed(self) -> bool {
804		match self {
805			Self::Boxed | Self::BoxedForced => true,
806			Self::Simple | Self::Other | Self::System => false,
807		}
808	}
809
810	pub fn is_trait(&self) -> bool {
811		match self {
812			Self::Boxed | Self::BoxedForced | Self::System => true,
813			Self::Simple | Self::Other => false,
814		}
815	}
816}
817
818#[derive(Clone, Debug, PartialEq, Eq, Hash)]
819pub enum TemplateKind<'tu, 'ge> {
820	/// Not a template or a specialization
821	No,
822	/// Base class template, e.g. `Point_<T>`
823	Template,
824	/// A specific instance (`Point_<int>`) of the class template (`Point_<T>`)
825	Specialization(Class<'tu, 'ge>),
826}
827
828impl<'tu, 'ge> TemplateKind<'tu, 'ge> {
829	pub fn is_template(&self) -> bool {
830		match self {
831			TemplateKind::Template => true,
832			TemplateKind::No | TemplateKind::Specialization(_) => false,
833		}
834	}
835
836	pub fn as_template_specialization(&self) -> Option<&Class<'tu, 'ge>> {
837		match self {
838			TemplateKind::Specialization(cls) => Some(cls),
839			TemplateKind::No | TemplateKind::Template => None,
840		}
841	}
842}
843
844pub enum FieldMethodsIter<'tu: 'ge, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> {
845	Clang(I),
846	Desc,
847}
848
849impl<'tu, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> Iterator for FieldMethodsIter<'tu, 'ge, I> {
850	type Item = Func<'tu, 'ge>;
851
852	fn next(&mut self) -> Option<Self::Item> {
853		match self {
854			FieldMethodsIter::Clang(iter) => iter.next(),
855			FieldMethodsIter::Desc => None,
856		}
857	}
858}