about summary refs log tree commit diff
path: root/compiler/rustc_macros/src/visitable.rs
blob: a7a82538eabe27bf897d06345841a3a446f688ef (plain)
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
use quote::quote;
use synstructure::BindingInfo;

pub(super) fn visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
    if let syn::Data::Union(_) = s.ast().data {
        panic!("cannot derive on union")
    }

    let has_attr = |bind: &BindingInfo<'_>, name| {
        let mut found = false;
        bind.ast().attrs.iter().for_each(|attr| {
            if !attr.path().is_ident("visitable") {
                return;
            }
            let _ = attr.parse_nested_meta(|nested| {
                if nested.path.is_ident(name) {
                    found = true;
                }
                Ok(())
            });
        });
        found
    };

    let get_attr = |bind: &BindingInfo<'_>, name: &str| {
        let mut content = None;
        bind.ast().attrs.iter().for_each(|attr| {
            if !attr.path().is_ident("visitable") {
                return;
            }
            let _ = attr.parse_nested_meta(|nested| {
                if nested.path.is_ident(name) {
                    let value = nested.value()?;
                    let value = value.parse()?;
                    content = Some(value);
                }
                Ok(())
            });
        });
        content
    };

    s.add_bounds(synstructure::AddBounds::Generics);
    s.bind_with(|_| synstructure::BindStyle::Ref);
    let ref_visit = s.each(|bind| {
        let extra = get_attr(bind, "extra").unwrap_or(quote! {});
        if has_attr(bind, "ignore") {
            quote! {}
        } else {
            quote! { rustc_ast_ir::try_visit!(crate::visit::Visitable::visit(#bind, __visitor, (#extra))) }
        }
    });

    s.bind_with(|_| synstructure::BindStyle::RefMut);
    let mut_visit = s.each(|bind| {
        let extra = get_attr(bind, "extra").unwrap_or(quote! {});
        if has_attr(bind, "ignore") {
            quote! {}
        } else {
            quote! { crate::mut_visit::MutVisitable::visit_mut(#bind, __visitor, (#extra)) }
        }
    });

    s.gen_impl(quote! {
        gen impl<'__ast, __V> crate::visit::Walkable<'__ast, __V> for @Self
            where __V: crate::visit::Visitor<'__ast>,
        {
            fn walk_ref(&'__ast self, __visitor: &mut __V) -> __V::Result {
                match *self { #ref_visit }
                <__V::Result as rustc_ast_ir::visit::VisitorResult>::output()
            }
        }

        gen impl<__V> crate::mut_visit::MutWalkable<__V> for @Self
            where __V: crate::mut_visit::MutVisitor,
        {
            fn walk_mut(&mut self, __visitor: &mut __V) {
                match *self { #mut_visit }
            }
        }
    })
}