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
use std::collections::HashMap;

use crate::{env::Env, library, version::Version};

#[derive(Debug)]
pub struct Signature(Vec<library::TypeId>, library::TypeId, Option<Version>);

impl Signature {
    pub fn new(func: &library::Function) -> Self {
        let params = func.parameters.iter().map(|p| p.typ).collect();
        Self(params, func.ret.typ, func.version)
    }

    fn from_property(is_get: bool, typ: library::TypeId) -> Self {
        if is_get {
            Self(vec![Default::default()], typ, None)
        } else {
            Self(vec![Default::default(), typ], Default::default(), None)
        }
    }

    pub fn has_in_deps(
        &self,
        env: &Env,
        name: &str,
        deps: &[library::TypeId],
    ) -> (bool, Option<Version>) {
        for tid in deps {
            let full_name = tid.full_name(&env.library);
            if let Some(info) = env.analysis.objects.get(&full_name) {
                if let Some(signature) = info.signatures.get(name) {
                    if self.eq(signature) {
                        return (true, signature.2);
                    }
                }
            }
        }
        (false, None)
    }

    pub fn has_for_property(
        env: &Env,
        name: &str,
        is_get: bool,
        typ: library::TypeId,
        signatures: &Signatures,
        deps: &[library::TypeId],
    ) -> (bool, Option<Version>) {
        if let Some(params) = signatures.get(name) {
            return (true, params.2);
        }
        let this = Signature::from_property(is_get, typ);
        for tid in deps {
            let full_name = tid.full_name(&env.library);
            if let Some(info) = env.analysis.objects.get(&full_name) {
                if let Some(signature) = info.signatures.get(name) {
                    if this.property_eq(signature, is_get) {
                        return (true, signature.2);
                    }
                }
            }
        }
        (false, None)
    }

    fn eq(&self, other: &Signature) -> bool {
        other.1 == self.1 && other.0[1..] == self.0[1..]
    }

    fn property_eq(&self, other: &Signature, is_get: bool) -> bool {
        if self.eq(other) {
            true
        } else {
            // For getters for types like GdkRGBA
            is_get && other.0.len() == 2 && other.0[1] == self.1
        }
    }
}

pub type Signatures = HashMap<String, Signature>;