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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use core::option::Option::{None, Some};
use core::result::Result::{Err, Ok};
use std::collections::BTreeMap;

use proc_macro_error::{emit_error, emit_warning};
use quote::ToTokens;
use syn::{
    Attribute, Error, GenericParam, Item, ItemImpl, ItemMod, ItemStruct, Result, Type,
};
use syn::parse::{Parse, ParseBuffer};
use syn::spanned::Spanned;
use syn::visit::Visit;

use crate::transformation::JavaPath;

struct AttribItemChecker {
    valid: bool,
}

impl AttribItemChecker {
    fn new() -> Self {
        AttribItemChecker { valid: true }
    }
}

impl<'ast> Visit<'ast> for AttribItemChecker {
    fn visit_item(&mut self, node: &'ast Item) {
        let has_package_attribute =
            |a: &Attribute| a.path.segments.first().unwrap().ident == "package";
        match node {
            Item::Struct(_) => {}
            Item::Const(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Enum(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type"; help = i.enum_token.span() => "replace `enum` with `struct`");
                self.valid = false;
            }
            Item::ExternCrate(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Fn(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::ForeignMod(i) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Impl(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Macro(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Macro2(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Mod(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Static(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Trait(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::TraitAlias(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Type(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Union(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Use(i) if i.attrs.iter().any(has_package_attribute) => {
                emit_error!(i.span(), "`package` attribute used on non-struct type");
                self.valid = false;
            }
            Item::Verbatim(_) => {}
            _ => {}
        }
    }
}

#[derive(Default)]
struct ImplAccumulator<'ast> {
    impls: Vec<&'ast ItemImpl>,
}

impl<'ast> Visit<'ast> for ImplAccumulator<'ast> {
    fn visit_item_impl(&mut self, node: &'ast ItemImpl) {
        self.impls.push(node);
    }
}

enum StructDeclarationKind {
    // structs with `package` attrib and impl
    Bridged,
    // structs with `package` attrib but no impl
    UnImpl,
    // structs without `package` attrib but with impl
    UnAttrib,
    // structs without `package` attrib and no impl
    Bare,
}

struct StructDeclVisitor<'ast> {
    module_structs: Vec<(&'ast ItemStruct, StructDeclarationKind)>,
    // all module impls
    module_impls: Vec<&'ast ItemImpl>,
}

impl<'ast> StructDeclVisitor<'ast> {
    fn new(module_impls: Vec<&'ast ItemImpl>) -> Self {
        StructDeclVisitor {
            module_structs: Vec::new(),
            module_impls,
        }
    }
}

impl<'ast> Visit<'ast> for StructDeclVisitor<'ast> {
    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        let struct_name = node.ident.to_string();
        let has_package_attrib = node
            .attrs
            .iter()
            .any(|a| a.path.segments.first().unwrap().ident == "package");
        let has_impl = self
            .module_impls
            .iter()
            .filter_map(|i| match &*i.self_ty {
                Type::Path(p) => Some(p.path.segments.last().unwrap().ident.to_string()),
                _ => None,
            })
            .any(|s| s == struct_name);

        let declaration_kind = match (has_package_attrib, has_impl) {
            (true, true) => StructDeclarationKind::Bridged,
            (true, false) => StructDeclarationKind::UnImpl,
            (false, true) => StructDeclarationKind::UnAttrib,
            (false, false) => StructDeclarationKind::Bare,
        };

        self.module_structs.push((node, declaration_kind))
    }
}

pub(crate) struct JNIBridgeModule {
    pub(crate) module_decl: ItemMod,
    pub(crate) package_map: BTreeMap<String, Option<JavaPath>>,
}

impl Parse for JNIBridgeModule {
    fn parse(input: &ParseBuffer) -> Result<Self> {
        let mut valid_input;
        let module_decl: ItemMod = input.parse().map_err(|e| {
            Error::new(
                e.span(),
                "`bridge` attribute is supported on mod items only",
            )
        })?;

        let mut attribute_checker = AttribItemChecker::new();
        attribute_checker.visit_item_mod(&module_decl);
        valid_input = attribute_checker.valid;

        let mut impl_visitor = ImplAccumulator::default();
        impl_visitor.visit_item_mod(&module_decl);

        let mut mod_visitor = StructDeclVisitor::new(impl_visitor.impls);
        mod_visitor.visit_item_mod(&module_decl);

        let bridged_structs: Vec<_> = mod_visitor.module_structs.into_iter()
            .filter_map(|(struct_item, decl_kind)| {
                match decl_kind {
                    StructDeclarationKind::Bridged => Some(struct_item),
                    StructDeclarationKind::UnImpl => {
                        emit_warning!(struct_item, "ignoring struct without declared methods"; help = "add methods using an `impl` block");
                        None
                    }
                    StructDeclarationKind::UnAttrib => {
                        emit_error!(struct_item, "struct without required `package` attribute");
                        valid_input = false;
                        None
                    }
                    StructDeclarationKind::Bare => {
                        emit_warning!(struct_item, "ignoring struct with no `package` attribute and no implementation";
                            help = struct_item.span() => "add a #[package(...)] attribute";
                            note = "structs with declared methods require package attribute for correct translation");
                        None
                    }
                }
            })
            .collect();

        let structs_idents: Vec<_> = bridged_structs.iter().map(|s| &s.ident).collect();
        let bridged_impls: Vec<_> = mod_visitor
            .module_impls
            .iter()
            .filter_map(|item_impl| match &*item_impl.self_ty {
                Type::Path(p) => {
                    structs_idents
                        .iter()
                        .position(|id| *id == &p.path.segments.last().unwrap().ident)
                        .map(|pos| {
                            (bridged_structs[pos], *item_impl)
                    })
                }
                _ => None,
            })
            .map(|(s, i)| (s.clone(), i.clone()))
            .collect();

        mod_visitor
            .module_impls
            .into_iter()
            .filter(|i| {
                if let Type::Path(p) = &*i.self_ty {
                    let impl_struct_name = p.path.segments.last().unwrap().ident.to_string();
                    let has_generics = i
                        .generics
                        .params
                        .iter()
                        .filter_map(|g| match g {
                            GenericParam::Type(t) => Some(&t.ident),
                            _ => None,
                        })
                        .next()
                        .is_some();

                    !bridged_impls
                        .iter()
                        .map(|(_, i)| i)
                        .filter_map(|i| {
                            // *Very* conservative check to avoid hassles with checking struct name in where clauses
                            // Should refactor into something proper or just delete this
                            if !has_generics {
                                match &*i.self_ty {
                                    Type::Path(p) => {
                                        Some(p.path.segments.last().unwrap().ident.to_string())
                                    }
                                    _ => None,
                                }
                            } else {
                                Some(impl_struct_name.clone()) // ignore this impl item
                            }
                        })
                        .any(|struct_name| struct_name == impl_struct_name)
                } else {
                    false
                }
            })
            .for_each(|lone_impl| {
                emit_error!(
                    lone_impl,
                    "impl declared without corresponding struct \"{}\"",
                    lone_impl.self_ty.to_token_stream()
                );
                valid_input = false;
            });

        let package_map: BTreeMap<String, Option<JavaPath>> = bridged_structs
            .iter()
            .map(|s| {
                let name = s.ident.to_string();
                let package_path = s
                    .attrs
                    .iter()
                    .filter(|a| a.path.segments.last().unwrap().ident == "package")
                    .map(|a| {
                        a.parse_args::<JavaPath>()
                            .unwrap()
                    })
                    .next()
                    .unwrap();

                let package = Some(package_path);

                (name, package)
            })
            .collect();

        if !valid_input {
            Err(Error::new(
                module_decl.span(),
                "`bridge` macro expansion failed due to previous errors",
            ))
        } else {
            Ok(JNIBridgeModule {
                module_decl,
                package_map,
            })
        }
    }
}