windows_bindgen/
method_names.rs

1use super::*;
2
3#[derive(Default)]
4pub struct MethodNames(HashMap<String, u32>);
5
6impl MethodNames {
7    pub fn new() -> Self {
8        Self(HashMap::new())
9    }
10
11    pub fn add(&mut self, method: MethodDef) -> TokenStream {
12        let name = method_def_special_name(method);
13        let overload = self.0.entry(name.to_string()).or_insert(0);
14        *overload += 1;
15        if *overload > 1 {
16            format!("{name}{overload}").into()
17        } else {
18            to_ident(&name)
19        }
20    }
21}
22
23fn method_def_special_name(row: MethodDef) -> String {
24    let name = row.name();
25    if row.flags().contains(MethodAttributes::SpecialName) {
26        if name.starts_with("get") {
27            name[4..].to_string()
28        } else if name.starts_with("put") {
29            format!("Set{}", &name[4..])
30        } else if name.starts_with("add") {
31            name[4..].to_string()
32        } else if name.starts_with("remove") {
33            format!("Remove{}", &name[7..])
34        } else {
35            name.to_string()
36        }
37    } else {
38        if let Some(attribute) = row.find_attribute("OverloadAttribute") {
39            for (_, arg) in attribute.args() {
40                if let Value::Str(overload) = arg {
41                    // Detect generated overload and revert back to original name.
42                    if let Some(suffix) = overload.strip_prefix(name) {
43                        if suffix.parse::<u32>().is_ok() {
44                            return name.to_string();
45                        }
46                    }
47
48                    return overload.to_string();
49                }
50            }
51        }
52        name.to_string()
53    }
54}