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 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 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 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 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 pub fn has_explicit_clone(&self) -> bool {
190 self.for_each_method(|m| ControlFlow::continue_until(m.is_clone())).is_break()
191 }
192
193 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 #![allow(clippy::mutable_key_type)]
259 #[allow(clippy::unnecessary_to_owned)]
260 self
261 .bases()
262 .into_owned()
263 .into_iter()
264 .flat_map(|b| {
265 let mut out = b.all_bases();
266 out.insert(b);
267 out
268 })
269 .collect()
270 }
271
272 pub fn has_descendants(&self) -> bool {
273 match self {
274 &Self::Clang { gen_env, .. } => gen_env.descendants_of(&self.cpp_name(CppNameStyle::Reference)).is_some(),
275 Self::Desc(_) => false,
276 }
277 }
278
279 pub fn descendants(&self) -> HashSet<Class<'tu, 'ge>> {
280 #![allow(clippy::mutable_key_type)]
281 match self {
282 &Self::Clang { gen_env, .. } => gen_env
283 .descendants_of(&self.cpp_name(CppNameStyle::Reference))
284 .into_iter()
285 .flat_map(|desc| desc.iter().map(|e| Self::new(*e, gen_env)))
286 .collect(),
287 Self::Desc(_) => HashSet::new(),
288 }
289 }
290
291 pub fn all_descendants(&self) -> HashSet<Class<'tu, 'ge>> {
292 #![allow(clippy::mutable_key_type)]
293 self
294 .descendants()
295 .into_iter()
296 .flat_map(|descendant| {
297 let mut out = descendant.all_descendants();
298 out.insert(descendant);
299 out
300 })
301 .collect()
302 }
303
304 pub fn all_family(&self) -> HashSet<Class<'tu, 'ge>> {
305 #![allow(clippy::mutable_key_type)]
306 fn collect<'tu, 'ge>(out: &mut HashSet<Class<'tu, 'ge>>, cls: Class<'tu, 'ge>) {
307 if out.insert(cls.clone()) {
308 #[allow(clippy::unnecessary_to_owned)]
309 for base in cls.bases().into_owned() {
310 collect(out, base);
311 }
312 for desc in cls.descendants() {
313 collect(out, desc);
314 }
315 }
316 }
317
318 let mut out = HashSet::new();
319 collect(&mut out, self.clone());
320 out
321 }
322
323 pub fn has_methods(&self) -> bool {
324 self.for_each_method(|_| ControlFlow::Break(())).is_break()
325 }
326
327 #[inline]
328 pub fn for_each_method(&self, mut predicate: impl FnMut(Func<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
329 match self {
330 &Self::Clang { entity, gen_env, .. } => entity.walk_methods_while(|f| predicate(Func::new(f, gen_env))),
331 Self::Desc(_) => ControlFlow::Continue(()),
332 }
333 }
334
335 pub fn methods(&self, filter: impl Fn(&Func) -> bool) -> Vec<Func<'tu, 'ge>> {
336 match self {
337 Class::Clang { entity, gen_env, .. } => {
338 let mut out = Vec::with_capacity(32);
339 let _ = entity.walk_methods_while(|func_entity| {
340 let func = Func::new(func_entity, gen_env);
341 let func: Func = if let Some(func_fact) = gen_env.settings.func_replace.get(&mut func.matcher()) {
342 func_fact(&func)
343 } else {
344 func
345 };
346 if func.is_generic() {
347 if let Some(specs) = gen_env.settings.func_specialize.get(&mut func.matcher()) {
348 for spec in specs {
349 let spec_func = func.clone().specialize(spec);
350 if filter(&spec_func) {
351 out.push(spec_func);
352 }
353 }
354 return ControlFlow::Continue(());
355 }
356 }
357 if filter(&func) {
358 out.push(func);
359 }
360 ControlFlow::Continue(())
361 });
362 for inject_func_fact in &gen_env.settings.func_inject {
363 let inject_func: Func = inject_func_fact();
364 if let Some(cls) = inject_func.kind().as_class_method() {
365 if cls == self && filter(&inject_func) {
366 out.push(inject_func);
367 }
368 }
369 }
370 out
371 }
372 Class::Desc(_) => vec![],
373 }
374 }
375
376 pub fn has_fields(&self) -> bool {
377 self.for_each_field(|_| ControlFlow::Break(())).is_break()
378 }
379
380 #[inline]
381 pub fn for_each_field(&self, mut predicate: impl FnMut(Field<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
382 match self {
383 &Self::Clang { entity, gen_env, .. } => entity.walk_fields_while(|f| predicate(Field::new(f, gen_env))),
384 Self::Desc(_) => ControlFlow::Continue(()),
385 }
386 }
387
388 pub fn fields(&self, filter: impl Fn(&Field) -> bool) -> Vec<Field<'tu, 'ge>> {
389 let mut out = Vec::with_capacity(32);
390 let _ = self.for_each_field(|f| {
391 if filter(&f) {
392 out.push(f);
393 }
394 ControlFlow::Continue(())
395 });
396 out
397 }
398
399 #[inline]
400 pub fn for_each_const(&self, mut predicate: impl FnMut(Const<'tu>) -> ControlFlow<()>) -> ControlFlow<()> {
401 match self {
402 &Self::Clang { entity, .. } => entity.walk_consts_while(|f| predicate(Const::new(f))),
403 Self::Desc(_) => ControlFlow::Continue(()),
404 }
405 }
406
407 pub fn consts(&self) -> Vec<Const<'tu>> {
408 let mut out = Vec::with_capacity(8);
409 let _ = self.for_each_const(|c| {
410 out.push(c);
411 ControlFlow::Continue(())
412 });
413 out
414 }
415
416 pub fn field_methods<'f>(
417 &self,
418 fields: &'f [Field<'tu, 'ge>],
419 constness_filter: Option<Constness>,
420 ) -> impl Iterator<Item = Func<'tu, 'ge>> + 'f {
421 match self {
422 &Self::Clang { gen_env, .. } => {
423 let cls = self.clone();
424 let accessor_generator = move |fld: &Field<'tu, 'ge>| {
425 let doc_comment = Rc::from(fld.doc_comment());
426 let def_loc = fld.file_line_name().location;
427 let rust_module = fld.rust_module();
428 let mut fld_type_ref = fld.type_ref();
429 let fld_refname = fld.cpp_name(CppNameStyle::Reference);
430 if let Some(type_hint) = gen_env.settings.property_override.get(fld_refname.as_ref()) {
431 fld_type_ref.to_mut().set_type_hint(type_hint.clone());
432 } else {
433 let fld_type_kind = fld_type_ref.kind();
434 if fld_type_kind
435 .as_pointer()
436 .is_some_and(|inner| inner.kind().as_primitive().is_some())
437 && !fld_type_kind.is_char_ptr_string(fld_type_ref.type_hint())
438 {
439 fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
440 } else if fld_type_kind.as_class().is_some_and(|cls| cls.kind().is_trait()) {
441 fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::TraitClassConcrete);
442 }
443 }
444 let fld_type_kind = fld_type_ref.kind();
445 let return_kind = ReturnKind::infallible(fld_type_kind.return_as_naked(fld_type_ref.type_hint()));
446 let fld_const = fld.constness();
447 let passed_by_ref = fld_type_kind.can_return_as_direct_reference();
448 let prop_tweak = gen_env.settings.property_tweaks.get(fld_refname.as_ref());
449 let rust_custom_leafname = prop_tweak.and_then(|tweak| tweak.rename);
450 let read_write = prop_tweak
451 .and_then(|tweak| tweak.read_write)
452 .unwrap_or(PropertyReadWrite::ReadWrite);
453 let fld_declname = fld_refname.localname();
454 let (mut read_const_yield, mut read_mut_yield) = if read_write.is_read() {
455 if fld_const.is_mut() && passed_by_ref {
456 let read_const_func = if constness_filter.map_or(true, |c| c.is_const()) {
457 Some(Func::new_desc(
458 FuncDesc::new(
459 FuncKind::FieldAccessor(cls.clone(), fld.clone()),
460 Constness::Const,
461 return_kind,
462 fld_declname,
463 rust_module,
464 [],
465 fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
466 )
467 .def_loc(def_loc.clone())
468 .doc_comment(Rc::clone(&doc_comment))
469 .cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
470 .maybe_rust_custom_leafname(rust_custom_leafname),
471 ))
472 } else {
473 None
474 };
475 let read_mut_func = if constness_filter.map_or(true, |c| c.is_mut()) {
476 Some(Func::new_desc(
477 FuncDesc::new(
478 FuncKind::FieldAccessor(cls.clone(), fld.clone()),
479 Constness::Mut,
480 return_kind,
481 format!("{fld_declname}Mut"),
482 rust_module,
483 [],
484 fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
485 )
486 .def_loc(def_loc.clone())
487 .doc_comment(Rc::clone(&doc_comment))
488 .cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
489 .maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("{name}_mut"))),
490 ))
491 } else {
492 None
493 };
494 (read_const_func, read_mut_func)
495 } else {
496 let single_read_func = if constness_filter.map_or(true, |c| c == fld_const) {
497 Some(Func::new_desc(
498 FuncDesc::new(
499 FuncKind::FieldAccessor(cls.clone(), fld.clone()),
500 fld_const,
501 return_kind,
502 fld_declname,
503 rust_module,
504 [],
505 fld_type_ref.as_ref().clone(),
506 )
507 .def_loc(def_loc.clone())
508 .doc_comment(Rc::clone(&doc_comment))
509 .cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
510 .maybe_rust_custom_leafname(rust_custom_leafname),
511 ))
512 } else {
513 None
514 };
515 (single_read_func, None)
516 }
517 } else {
518 (None, None)
519 };
520 let mut write_yield = if read_write.is_write()
521 && constness_filter.map_or(true, |c| c.is_mut())
522 && !fld_type_ref.constness().is_const()
523 && !fld_type_kind.as_fixed_array().is_some()
524 {
525 let (first_letter, rest) = fld_declname.capitalize_first_ascii_letter().expect("Empty fld_declname");
526 Some(Func::new_desc(
527 FuncDesc::new(
528 FuncKind::FieldAccessor(cls.clone(), fld.clone()),
529 Constness::Mut,
530 ReturnKind::InfallibleNaked,
531 format!("set{first_letter}{rest}"),
532 rust_module,
533 [Field::new_desc(FieldDesc {
534 cpp_fullname: "val".into(),
535 type_ref: fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
536 default_value: fld.default_value().map(|v| v.into()),
537 })],
538 TypeRefDesc::void(),
539 )
540 .doc_comment(doc_comment)
541 .def_loc(def_loc)
542 .cpp_body(FuncCppBody::ManualCall("{{name}} = {{args}}".into()))
543 .maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("set_{name}"))),
544 ))
545 } else {
546 None
547 };
548 iter::from_fn(move || {
549 read_const_yield
550 .take()
551 .or_else(|| read_mut_yield.take())
552 .or_else(|| write_yield.take())
553 })
554 };
555 FieldMethodsIter::Clang(fields.iter().flat_map(accessor_generator))
556 }
557 Self::Desc(_) => FieldMethodsIter::Desc,
558 }
559 }
560
561 fn definition_entity(entity: Entity<'tu>) -> Entity<'tu> {
564 entity.get_template().unwrap_or(entity).get_definition().unwrap_or(entity)
565 }
566
567 pub fn is_definition(&self) -> bool {
568 match self {
569 &Self::Clang { entity, .. } => {
570 let class_loc = entity.get_location();
571 let def_loc = entity.get_definition().and_then(|d| d.get_location());
572 match (class_loc, def_loc) {
573 (Some(class_loc), Some(def_loc)) => class_loc == def_loc,
574 (_, None) => false,
575 _ => true,
576 }
577 }
578 Self::Desc(_) => true,
579 }
580 }
581
582 pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
583 self
584 .fields(|f| f.exclude_kind().is_included())
585 .into_iter()
586 .flat_map(|f| f.type_ref().generated_types())
587 .chain(
588 self
589 .methods(|m| m.exclude_kind().is_included())
590 .into_iter()
591 .flat_map(|m| m.generated_types()),
592 )
593 .collect()
594 }
595}
596
597impl<'tu> ToEntity<'tu> for &Class<'tu, '_> {
598 fn to_entity(self) -> Option<Entity<'tu>> {
599 match self {
600 Class::Clang { entity, .. } => Some(*entity),
601 Class::Desc(_) => None,
602 }
603 }
604}
605
606impl Element for Class<'_, '_> {
607 fn exclude_kind(&self) -> ExcludeKind {
608 match self {
609 Self::Clang { .. } => DefaultElement::exclude_kind(self)
610 .with_is_ignored(|| match self.kind() {
611 ClassKind::Other => true,
612 ClassKind::System => {
613 !settings::IMPLEMENTED_SYSTEM_CLASSES.contains(self.cpp_name(CppNameStyle::Reference).as_ref())
614 }
615 ClassKind::Simple | ClassKind::Boxed | ClassKind::BoxedForced => match self.template_kind() {
616 TemplateKind::Template => true,
617 TemplateKind::No => !self.is_definition() || self.cpp_namespace() == "",
618 TemplateKind::Specialization(_) => {
619 !settings::IMPLEMENTED_GENERICS.contains(self.cpp_name(CppNameStyle::Reference).as_ref())
620 }
621 },
622 })
623 .with_is_excluded(|| match self.kind() {
624 ClassKind::System | ClassKind::Other => true,
625 ClassKind::Simple => false,
626 ClassKind::Boxed | ClassKind::BoxedForced => self.has_private_destructor(),
627 }),
628 Self::Desc(desc) => desc.exclude_kind,
629 }
630 }
631
632 fn is_system(&self) -> bool {
633 match self {
634 &Self::Clang { entity, .. } => DefaultElement::is_system(entity),
635 Self::Desc(desc) => matches!(desc.kind, ClassKind::System),
636 }
637 }
638
639 fn is_public(&self) -> bool {
640 match self {
641 &Self::Clang { entity, .. } => DefaultElement::is_public(entity),
642 Self::Desc(desc) => desc.is_public,
643 }
644 }
645
646 fn doc_comment(&self) -> Cow<'_, str> {
647 match self {
648 Self::Clang { entity, .. } => entity.doc_comment(),
649 Self::Desc(_) => "".into(),
650 }
651 }
652
653 fn cpp_namespace(&self) -> Cow<'_, str> {
654 #[inline(always)]
655 fn inner(cpp_fullname: &str) -> Cow<'_, str> {
656 cpp_fullname.namespace().into()
657 }
658
659 match self {
660 Self::Clang {
661 custom_fullname: Some(cpp_fullname),
662 ..
663 } => inner(cpp_fullname.as_ref()),
664 Self::Clang { entity, .. } => DefaultElement::cpp_namespace(*entity).into(),
665 Self::Desc(desc) => inner(desc.cpp_fullname.as_ref()),
666 }
667 }
668
669 fn cpp_name(&self, style: CppNameStyle) -> Cow<'_, str> {
670 match self {
671 Self::Clang {
672 custom_fullname: Some(cpp_fullname),
673 ..
674 } => cpp_fullname.cpp_name_from_fullname(style).into(),
675 &Self::Clang { entity, .. } => DefaultElement::cpp_name(self, entity, style),
676 Self::Desc(desc) => desc.cpp_fullname.cpp_name_from_fullname(style).into(),
677 }
678 }
679}
680
681impl Hash for Class<'_, '_> {
682 fn hash<H: Hasher>(&self, state: &mut H) {
683 match self {
684 Self::Clang { entity, .. } => entity.hash(state),
685 Self::Desc(desc) => desc.hash(state),
686 }
687 }
688}
689
690impl PartialEq for Class<'_, '_> {
691 fn eq(&self, other: &Self) -> bool {
692 self.cpp_name(CppNameStyle::Reference) == other.cpp_name(CppNameStyle::Reference) && self.kind() == other.kind()
693 }
694}
695
696impl Eq for Class<'_, '_> {}
697
698impl<'me> NameDebug<'me> for &'me Class<'me, '_> {
699 fn file_line_name(self) -> LocationName<'me> {
700 match self {
701 Class::Clang { entity, .. } => entity.file_line_name(),
702 Class::Desc(desc) => LocationName::new(DefinitionLocation::Generated, desc.cpp_fullname.as_ref()),
703 }
704 }
705}
706
707impl fmt::Debug for Class<'_, '_> {
708 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
709 let mut props = vec![];
710 if self.can_be_simple() {
711 props.push("can_be_simple");
712 }
713 if self.template_kind().is_template() {
714 props.push("template");
715 }
716 if self.template_kind().as_template_specialization().is_some() {
717 props.push("template_specialization");
718 }
719 if self.is_abstract() {
720 props.push("abstract");
721 }
722 if self.is_polymorphic() {
723 props.push("polymorphic");
724 }
725 if self.kind().is_trait() {
726 props.push("trait");
727 }
728 if self.as_enum().is_some() {
729 props.push("enum");
730 }
731 if self.has_explicit_clone() {
732 props.push("has_explicit_clone");
733 }
734 if self.has_implicit_clone() {
735 props.push("has_implicit_clone");
736 }
737 if self.has_virtual_destructor() {
738 props.push("has_virtual_dtor");
739 }
740 if self.has_private_destructor() {
741 props.push("has_private_dtor")
742 }
743 if self.has_bases() {
744 props.push("has_bases");
745 }
746 if self.has_descendants() {
747 props.push("has_descendants");
748 }
749 if self.has_methods() {
750 props.push("has_methods");
751 }
752 if self.has_fields() {
753 props.push("has_fields");
754 }
755 if !self.consts().is_empty() {
756 props.push("has_consts");
757 }
758 if self.is_definition() {
759 props.push("definition");
760 }
761 if matches!(
762 self,
763 Self::Clang {
764 custom_fullname: Some(_),
765 ..
766 }
767 ) {
768 props.push("custom_fullname");
769 }
770 let mut debug_struct = f.debug_struct(match self {
771 Self::Clang { .. } => "Class::Clang",
772 Self::Desc(_) => "Class::Desc",
773 });
774 self
775 .update_debug_struct(&mut debug_struct)
776 .field("kind", &self.kind())
777 .field("props", &props.join(", "))
778 .field("string_type", &self.string_type())
779 .finish()
780 }
781}
782
783#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
784pub enum ClassKind {
785 Simple,
787 Boxed,
789 BoxedForced,
791 System,
793 Other,
795}
796
797impl ClassKind {
798 pub fn is_simple(self) -> bool {
799 match self {
800 Self::Simple => true,
801 Self::Boxed | Self::BoxedForced | Self::System | Self::Other => false,
802 }
803 }
804
805 pub fn is_boxed(self) -> bool {
806 match self {
807 Self::Boxed | Self::BoxedForced => true,
808 Self::Simple | Self::Other | Self::System => false,
809 }
810 }
811
812 pub fn is_trait(&self) -> bool {
813 match self {
814 Self::Boxed | Self::BoxedForced | Self::System => true,
815 Self::Simple | Self::Other => false,
816 }
817 }
818}
819
820#[derive(Clone, Debug, PartialEq, Eq, Hash)]
821pub enum TemplateKind<'tu, 'ge> {
822 No,
824 Template,
826 Specialization(Class<'tu, 'ge>),
828}
829
830impl<'tu, 'ge> TemplateKind<'tu, 'ge> {
831 pub fn is_template(&self) -> bool {
832 match self {
833 TemplateKind::Template => true,
834 TemplateKind::No | TemplateKind::Specialization(_) => false,
835 }
836 }
837
838 pub fn as_template_specialization(&self) -> Option<&Class<'tu, 'ge>> {
839 match self {
840 TemplateKind::Specialization(cls) => Some(cls),
841 TemplateKind::No | TemplateKind::Template => None,
842 }
843 }
844}
845
846pub enum FieldMethodsIter<'tu: 'ge, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> {
847 Clang(I),
848 Desc,
849}
850
851impl<'tu, 'ge, I: Iterator<Item = Func<'tu, 'ge>>> Iterator for FieldMethodsIter<'tu, 'ge, I> {
852 type Item = Func<'tu, 'ge>;
853
854 fn next(&mut self) -> Option<Self::Item> {
855 match self {
856 FieldMethodsIter::Clang(iter) => iter.next(),
857 FieldMethodsIter::Desc => None,
858 }
859 }
860}