1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use super::{general::StatusedTypeId, imports::Imports};
use crate::{
    analysis::{namespaces, rust_type::RustType},
    env::Env,
    library::TypeId,
    version::Version,
};

pub fn analyze(
    env: &Env,
    type_id: TypeId,
    version: Option<Version>,
    imports: &mut Imports,
    add_parent_types_import: bool,
) -> Vec<StatusedTypeId> {
    let mut parents = Vec::new();
    let gobject_id = env.library.find_type(0, "GObject.Object").unwrap();

    for &super_tid in env.class_hierarchy.supertypes(type_id) {
        // skip GObject, it's inherited implicitly
        if super_tid == gobject_id {
            continue;
        }

        let status = env.type_status(&super_tid.full_name(&env.library));

        parents.push(StatusedTypeId {
            type_id: super_tid,
            name: env.library.type_(super_tid).get_name(),
            status,
        });

        if !status.ignored() && super_tid.ns_id == namespaces::MAIN && !add_parent_types_import {
            if let Ok(rust_type) = RustType::try_new(env, super_tid) {
                let full_name = super_tid.full_name(&env.library);
                if let Some(parent_version) = env
                    .analysis
                    .objects
                    .get(&full_name)
                    .and_then(|info| info.version)
                {
                    if Some(parent_version) > version && parent_version > env.config.min_cfg_version
                    {
                        for import in rust_type.into_used_types() {
                            imports.add_with_version(
                                &format!("crate::{import}"),
                                Some(parent_version),
                            );
                        }
                    } else {
                        for import in rust_type.into_used_types() {
                            imports.add(&format!("crate::{import}"));
                        }
                    }
                } else {
                    for import in rust_type.into_used_types() {
                        imports.add(&format!("crate::{import}"));
                    }
                }
            }
        }
    }

    parents
}

pub fn dependencies(env: &Env, type_id: TypeId) -> Vec<TypeId> {
    let mut parents = Vec::new();
    let gobject_id = match env.library.find_type(0, "GObject.Object") {
        Some(gobject_id) => gobject_id,
        None => TypeId::tid_none(),
    };

    for &super_tid in env.class_hierarchy.supertypes(type_id) {
        // skip GObject, it's inherited implicitly
        if super_tid == gobject_id {
            continue;
        }

        let status = env.type_status(&super_tid.full_name(&env.library));

        if status.need_generate() {
            parents.push(super_tid);
        }
    }

    parents
}