libgir/analysis/
supertypes.rs

1use super::{general::StatusedTypeId, imports::Imports};
2use crate::{
3    analysis::{namespaces, rust_type::RustType},
4    env::Env,
5    library::TypeId,
6    version::Version,
7};
8
9pub fn analyze(
10    env: &Env,
11    type_id: TypeId,
12    version: Option<Version>,
13    imports: &mut Imports,
14    add_parent_types_import: bool,
15) -> Vec<StatusedTypeId> {
16    let mut parents = Vec::new();
17    let gobject_id = env.library.find_type(0, "GObject.Object").unwrap();
18
19    for &super_tid in env.class_hierarchy.supertypes(type_id) {
20        // skip GObject, it's inherited implicitly
21        if super_tid == gobject_id {
22            continue;
23        }
24
25        let status = env.type_status(&super_tid.full_name(&env.library));
26
27        parents.push(StatusedTypeId {
28            type_id: super_tid,
29            name: env.library.type_(super_tid).get_name(),
30            status,
31        });
32
33        if !status.ignored()
34            && super_tid.ns_id == namespaces::MAIN
35            && !add_parent_types_import
36            && let Ok(rust_type) = RustType::try_new(env, super_tid)
37        {
38            let full_name = super_tid.full_name(&env.library);
39            if let Some(parent_version) = env
40                .analysis
41                .objects
42                .get(&full_name)
43                .and_then(|info| info.version)
44            {
45                if Some(parent_version) > version && parent_version > env.config.min_cfg_version {
46                    for import in rust_type.into_used_types() {
47                        imports.add_with_version(&format!("crate::{import}"), Some(parent_version));
48                    }
49                } else {
50                    for import in rust_type.into_used_types() {
51                        imports.add(&format!("crate::{import}"));
52                    }
53                }
54            } else {
55                for import in rust_type.into_used_types() {
56                    imports.add(&format!("crate::{import}"));
57                }
58            }
59        }
60    }
61
62    parents
63}
64
65pub fn dependencies(env: &Env, type_id: TypeId) -> Vec<TypeId> {
66    let mut parents = Vec::new();
67    let gobject_id = match env.library.find_type(0, "GObject.Object") {
68        Some(gobject_id) => gobject_id,
69        None => TypeId::tid_none(),
70    };
71
72    for &super_tid in env.class_hierarchy.supertypes(type_id) {
73        // skip GObject, it's inherited implicitly
74        if super_tid == gobject_id {
75            continue;
76        }
77
78        let status = env.type_status(&super_tid.full_name(&env.library));
79
80        if status.need_generate() {
81            parents.push(super_tid);
82        }
83    }
84
85    parents
86}