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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::*;
use squote::{quote, TokenStream};
use std::collections::*;

#[derive(Debug)]
pub struct RequiredInterface {
    pub name: TypeName,
    pub methods: Vec<Method>,
    pub kind: InterfaceKind,
}

impl PartialEq for RequiredInterface {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Eq for RequiredInterface {}

impl PartialOrd for RequiredInterface {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for RequiredInterface {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.name.cmp(&other.name)
    }
}

impl RequiredInterface {
    fn from_type_def(
        def: &winmd::TypeDef,
        calling_namespace: &'static str,
        kind: InterfaceKind,
    ) -> Self {
        let name = TypeName::from_type_def(def, calling_namespace);
        Self::from_type_name_and_kind(name, kind, calling_namespace)
    }

    fn from_type_name_and_kind(
        name: TypeName,
        kind: InterfaceKind,
        calling_namespace: &'static str,
    ) -> Self {
        let methods = name
            .def
            .methods()
            .enumerate()
            .map(|(count, method)| {
                Method::from_method_def(
                    &method,
                    (count + 6) as u32,
                    &name.generics,
                    calling_namespace,
                )
            })
            .collect();

        Self {
            name,
            methods,
            kind,
        }
    }

    pub fn gen_conversions(&self, from: &TokenStream, constraints: &TokenStream) -> TokenStream {
        match self.kind {
            InterfaceKind::Default => {
                let into = self.name.gen();
                quote! {
                    impl<#constraints> ::std::convert::From<#from> for #into {
                        fn from(value: #from) -> Self {
                            unsafe { ::std::mem::transmute(value) }
                        }
                    }
                    impl<#constraints> ::std::convert::From<&#from> for #into {
                        fn from(value: &#from) -> Self {
                            ::std::convert::From::from(::std::clone::Clone::clone(value))
                        }
                    }
                    impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, #into>> for #from {
                        fn into(self) -> ::windows::Param<'a, #into> {
                            ::windows::Param::Owned(::std::convert::Into::<#into>::into(self))
                        }
                    }
                    impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, #into>> for &'a #from {
                        fn into(self) -> ::windows::Param<'a, #into> {
                            ::windows::Param::Owned(::std::convert::Into::<#into>::into(::std::clone::Clone::clone(self)))
                        }
                    }
                }
            }
            InterfaceKind::NonDefault => {
                let into = self.name.gen();
                quote! {
                    impl<#constraints> ::std::convert::From<#from> for #into {
                        fn from(value: #from) -> Self {
                            ::std::convert::From::from(&value)
                        }
                    }
                    impl<#constraints> ::std::convert::From<&#from> for #into {
                        fn from(value: &#from) -> Self {
                            ::windows::Interface::cast(value).unwrap()
                        }
                    }
                    impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, #into>> for #from {
                        fn into(self) -> ::windows::Param<'a, #into> {
                            ::windows::Param::Owned(::std::convert::Into::<#into>::into(self))
                        }
                    }
                    impl<'a, #constraints> ::std::convert::Into<::windows::Param<'a, #into>> for &'a #from {
                        fn into(self) -> ::windows::Param<'a, #into> {
                            ::windows::Param::Owned(::std::convert::Into::<#into>::into(::std::clone::Clone::clone(self)))
                        }
                    }
                }
            }
            _ => TokenStream::new(),
        }
    }
}

pub fn add_type(
    vec: &mut Vec<RequiredInterface>,
    def: &winmd::TypeDef,
    calling_namespace: &'static str,
    kind: InterfaceKind,
) {
    vec.push(RequiredInterface::from_type_def(
        def,
        calling_namespace,
        kind,
    ));
}

pub fn add_dependencies(
    vec: &mut Vec<RequiredInterface>,
    name: &TypeName,
    calling_namespace: &'static str,
    strip_default: bool,
) {
    for required in name.def.interfaces() {
        let is_default = required.is_default();
        let required = required.interface();

        let required_name =
            TypeName::from_type_def_or_ref(&required, &name.generics, calling_namespace);

        if let Some(index) = vec.iter().position(|i| i.name == required_name) {
            if !strip_default && vec[index].kind == InterfaceKind::NonDefault && is_default {
                vec[index].kind = InterfaceKind::Default;
            }
        } else {
            let kind = if !strip_default && is_default {
                InterfaceKind::Default
            } else {
                InterfaceKind::NonDefault
            };

            add_dependencies(vec, &required_name, calling_namespace, strip_default);

            vec.push(RequiredInterface::from_type_name_and_kind(
                required_name,
                kind,
                calling_namespace,
            ));
        }
    }
}

pub fn gen_method(interfaces: &Vec<RequiredInterface>) -> TokenStream {
    let mut tokens = TokenStream::new();

    for interface in interfaces {
        for method in &interface.methods {
            tokens.combine(&method.gen_method(&interface.name, interface.kind));
        }
    }

    tokens
}

pub fn rename_collisions(interfaces: &mut Vec<RequiredInterface>) {
    // First sort interfaces to ensure a stable method renaming across versions.
    // TODO: Once fast abi support is added, sorting here will be unnecessary.
    // https://github.com/microsoft/windows-rs/issues/235
    interfaces.sort();

    let mut count = BTreeMap::new();

    for interface in interfaces {
        for method in &mut interface.methods {
            let count = count.entry(&method.name).or_insert(0);
            *count += 1;
            method.overload = *count;
        }
    }
}