rustc_type_ir_macros/
lib.rs

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
use quote::{ToTokens, quote};
use syn::visit_mut::VisitMut;
use syn::{Attribute, parse_quote};
use synstructure::decl_derive;

decl_derive!(
    [TypeVisitable_Generic, attributes(type_visitable)] => type_visitable_derive
);
decl_derive!(
    [TypeFoldable_Generic, attributes(type_foldable)] => type_foldable_derive
);
decl_derive!(
    [Lift_Generic] => lift_derive
);

fn has_ignore_attr(attrs: &[Attribute], name: &'static str, meta: &'static str) -> bool {
    let mut ignored = false;
    attrs.iter().for_each(|attr| {
        if !attr.path().is_ident(name) {
            return;
        }
        let _ = attr.parse_nested_meta(|nested| {
            if nested.path.is_ident(meta) {
                ignored = true;
            }
            Ok(())
        });
    });

    ignored
}

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

    if !s.ast().generics.type_params().any(|ty| ty.ident == "I") {
        s.add_impl_generic(parse_quote! { I });
    }

    s.filter(|bi| !has_ignore_attr(&bi.ast().attrs, "type_visitable", "ignore"));

    s.add_where_predicate(parse_quote! { I: Interner });
    s.add_bounds(synstructure::AddBounds::Fields);
    let body_visit = s.each(|bind| {
        quote! {
            match ::rustc_ast_ir::visit::VisitorResult::branch(
                ::rustc_type_ir::visit::TypeVisitable::visit_with(#bind, __visitor)
            ) {
                ::core::ops::ControlFlow::Continue(()) => {},
                ::core::ops::ControlFlow::Break(r) => {
                    return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
                },
            }
        }
    });
    s.bind_with(|_| synstructure::BindStyle::Move);

    s.bound_impl(quote!(::rustc_type_ir::visit::TypeVisitable<I>), quote! {
        fn visit_with<__V: ::rustc_type_ir::visit::TypeVisitor<I>>(
            &self,
            __visitor: &mut __V
        ) -> __V::Result {
            match *self { #body_visit }
            <__V::Result as ::rustc_ast_ir::visit::VisitorResult>::output()
        }
    })
}

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

    if !s.ast().generics.type_params().any(|ty| ty.ident == "I") {
        s.add_impl_generic(parse_quote! { I });
    }

    s.add_where_predicate(parse_quote! { I: Interner });
    s.add_bounds(synstructure::AddBounds::Fields);
    s.bind_with(|_| synstructure::BindStyle::Move);
    let body_fold = s.each_variant(|vi| {
        let bindings = vi.bindings();
        vi.construct(|_, index| {
            let bind = &bindings[index];

            // retain value of fields with #[type_foldable(identity)]
            if has_ignore_attr(&bind.ast().attrs, "type_foldable", "identity") {
                bind.to_token_stream()
            } else {
                quote! {
                    ::rustc_type_ir::fold::TypeFoldable::try_fold_with(#bind, __folder)?
                }
            }
        })
    });

    // We filter fields which get ignored and don't require them to implement
    // `TypeFoldable`. We do so after generating `body_fold` as we still need
    // to generate code for them.
    s.filter(|bi| !has_ignore_attr(&bi.ast().attrs, "type_foldable", "identity"));
    s.add_bounds(synstructure::AddBounds::Fields);
    s.bound_impl(quote!(::rustc_type_ir::fold::TypeFoldable<I>), quote! {
        fn try_fold_with<__F: ::rustc_type_ir::fold::FallibleTypeFolder<I>>(
            self,
            __folder: &mut __F
        ) -> Result<Self, __F::Error> {
            Ok(match self { #body_fold })
        }
    })
}

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

    if !s.ast().generics.type_params().any(|ty| ty.ident == "I") {
        s.add_impl_generic(parse_quote! { I });
    }

    s.add_bounds(synstructure::AddBounds::None);
    s.add_where_predicate(parse_quote! { I: Interner });
    s.add_impl_generic(parse_quote! { J });
    s.add_where_predicate(parse_quote! { J: Interner });

    let mut wc = vec![];
    s.bind_with(|_| synstructure::BindStyle::Move);
    let body_fold = s.each_variant(|vi| {
        let bindings = vi.bindings();
        vi.construct(|field, index| {
            let ty = field.ty.clone();
            let lifted_ty = lift(ty.clone());
            wc.push(parse_quote! { #ty: ::rustc_type_ir::lift::Lift<J, Lifted = #lifted_ty> });
            let bind = &bindings[index];
            quote! {
                #bind.lift_to_interner(interner)?
            }
        })
    });
    for wc in wc {
        s.add_where_predicate(wc);
    }

    let (_, ty_generics, _) = s.ast().generics.split_for_impl();
    let name = s.ast().ident.clone();
    let self_ty: syn::Type = parse_quote! { #name #ty_generics };
    let lifted_ty = lift(self_ty);

    s.bound_impl(quote!(::rustc_type_ir::lift::Lift<J>), quote! {
        type Lifted = #lifted_ty;

        fn lift_to_interner(
            self,
            interner: J,
        ) -> Option<Self::Lifted> {
            Some(match self { #body_fold })
        }
    })
}

fn lift(mut ty: syn::Type) -> syn::Type {
    struct ItoJ;
    impl VisitMut for ItoJ {
        fn visit_type_path_mut(&mut self, i: &mut syn::TypePath) {
            if i.qself.is_none() {
                if let Some(first) = i.path.segments.first_mut() {
                    if first.ident == "I" {
                        *first = parse_quote! { J };
                    }
                }
            }
            syn::visit_mut::visit_type_path_mut(self, i);
        }
    }

    ItoJ.visit_type_mut(&mut ty);

    ty
}