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() && super_tid.ns_id == namespaces::MAIN && !add_parent_types_import {
34            if let Ok(rust_type) = RustType::try_new(env, super_tid) {
35                let full_name = super_tid.full_name(&env.library);
36                if let Some(parent_version) = env
37                    .analysis
38                    .objects
39                    .get(&full_name)
40                    .and_then(|info| info.version)
41                {
42                    if Some(parent_version) > version && parent_version > env.config.min_cfg_version
43                    {
44                        for import in rust_type.into_used_types() {
45                            imports.add_with_version(
46                                &format!("crate::{import}"),
47                                Some(parent_version),
48                            );
49                        }
50                    } else {
51                        for import in rust_type.into_used_types() {
52                            imports.add(&format!("crate::{import}"));
53                        }
54                    }
55                } else {
56                    for import in rust_type.into_used_types() {
57                        imports.add(&format!("crate::{import}"));
58                    }
59                }
60            }
61        }
62    }
63
64    parents
65}
66
67pub fn dependencies(env: &Env, type_id: TypeId) -> Vec<TypeId> {
68    let mut parents = Vec::new();
69    let gobject_id = match env.library.find_type(0, "GObject.Object") {
70        Some(gobject_id) => gobject_id,
71        None => TypeId::tid_none(),
72    };
73
74    for &super_tid in env.class_hierarchy.supertypes(type_id) {
75        // skip GObject, it's inherited implicitly
76        if super_tid == gobject_id {
77            continue;
78        }
79
80        let status = env.type_status(&super_tid.full_name(&env.library));
81
82        if status.need_generate() {
83            parents.push(super_tid);
84        }
85    }
86
87    parents
88}