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
//! The compiler code necessary for `#[derive(RustcDecodable)]`. See encodable.rs for more.

use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::pathvec_std;
use rustc_ast::ptr::P;
use rustc_ast::{self as ast, Expr, MetaItem, Mutability};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span;
use thin_vec::{thin_vec, ThinVec};

pub(crate) fn expand_deriving_rustc_decodable(
    cx: &ExtCtxt<'_>,
    span: Span,
    mitem: &MetaItem,
    item: &Annotatable,
    push: &mut dyn FnMut(Annotatable),
    is_const: bool,
) {
    let krate = sym::rustc_serialize;
    let typaram = sym::__D;

    let trait_def = TraitDef {
        span,
        path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
        skip_path_as_bound: false,
        needs_copy_as_bound_if_packed: true,
        additional_bounds: Vec::new(),
        supports_unions: false,
        methods: vec![MethodDef {
            name: sym::decode,
            generics: Bounds {
                bounds: vec![(
                    typaram,
                    vec![Path::new_(vec![krate, sym::Decoder], vec![], PathKind::Global)],
                )],
            },
            explicit_self: false,
            nonself_args: vec![(
                Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut),
                sym::d,
            )],
            ret_ty: Path(Path::new_(
                pathvec_std!(result::Result),
                vec![
                    Box::new(Self_),
                    Box::new(Path(Path::new_(vec![typaram, sym::Error], vec![], PathKind::Local))),
                ],
                PathKind::Std,
            )),
            attributes: ast::AttrVec::new(),
            fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
            combine_substructure: combine_substructure(Box::new(|a, b, c| {
                decodable_substructure(a, b, c, krate)
            })),
        }],
        associated_types: Vec::new(),
        is_const,
    };

    trait_def.expand(cx, mitem, item, push)
}

fn decodable_substructure(
    cx: &ExtCtxt<'_>,
    trait_span: Span,
    substr: &Substructure<'_>,
    krate: Symbol,
) -> BlockOrExpr {
    let decoder = substr.nonselflike_args[0].clone();
    let recurse = vec![
        Ident::new(krate, trait_span),
        Ident::new(sym::Decodable, trait_span),
        Ident::new(sym::decode, trait_span),
    ];
    let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse));
    // throw an underscore in front to suppress unused variable warnings
    let blkarg = Ident::new(sym::_d, trait_span);
    let blkdecoder = cx.expr_ident(trait_span, blkarg);

    let expr = match substr.fields {
        StaticStruct(_, summary) => {
            let nfields = match summary {
                Unnamed(fields, _) => fields.len(),
                Named(fields) => fields.len(),
            };
            let fn_read_struct_field_path: Vec<_> =
                cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_struct_field]);

            let path = cx.path_ident(trait_span, substr.type_ident);
            let result =
                decode_static_fields(cx, trait_span, path, summary, |cx, span, name, field| {
                    cx.expr_try(
                        span,
                        cx.expr_call_global(
                            span,
                            fn_read_struct_field_path.clone(),
                            thin_vec![
                                blkdecoder.clone(),
                                cx.expr_str(span, name),
                                cx.expr_usize(span, field),
                                exprdecode.clone(),
                            ],
                        ),
                    )
                });
            let result = cx.expr_ok(trait_span, result);
            let fn_read_struct_path: Vec<_> =
                cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_struct]);

            cx.expr_call_global(
                trait_span,
                fn_read_struct_path,
                thin_vec![
                    decoder,
                    cx.expr_str(trait_span, substr.type_ident.name),
                    cx.expr_usize(trait_span, nfields),
                    cx.lambda1(trait_span, result, blkarg),
                ],
            )
        }
        StaticEnum(_, fields) => {
            let variant = Ident::new(sym::i, trait_span);

            let mut arms = ThinVec::with_capacity(fields.len() + 1);
            let mut variants = ThinVec::with_capacity(fields.len());

            let fn_read_enum_variant_arg_path: Vec<_> =
                cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_enum_variant_arg]);

            for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() {
                variants.push(cx.expr_str(v_span, ident.name));

                let path = cx.path(trait_span, vec![substr.type_ident, ident]);
                let decoded =
                    decode_static_fields(cx, v_span, path, parts, |cx, span, _, field| {
                        let idx = cx.expr_usize(span, field);
                        cx.expr_try(
                            span,
                            cx.expr_call_global(
                                span,
                                fn_read_enum_variant_arg_path.clone(),
                                thin_vec![blkdecoder.clone(), idx, exprdecode.clone()],
                            ),
                        )
                    });

                arms.push(cx.arm(v_span, cx.pat_lit(v_span, cx.expr_usize(v_span, i)), decoded));
            }

            arms.push(cx.arm_unreachable(trait_span));

            let result = cx.expr_ok(
                trait_span,
                cx.expr_match(trait_span, cx.expr_ident(trait_span, variant), arms),
            );
            let lambda = cx.lambda(trait_span, vec![blkarg, variant], result);
            let variant_array_ref = cx.expr_array_ref(trait_span, variants);
            let fn_read_enum_variant_path: Vec<_> =
                cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_enum_variant]);
            let result = cx.expr_call_global(
                trait_span,
                fn_read_enum_variant_path,
                thin_vec![blkdecoder, variant_array_ref, lambda],
            );
            let fn_read_enum_path: Vec<_> =
                cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_enum]);

            cx.expr_call_global(
                trait_span,
                fn_read_enum_path,
                thin_vec![
                    decoder,
                    cx.expr_str(trait_span, substr.type_ident.name),
                    cx.lambda1(trait_span, result, blkarg),
                ],
            )
        }
        _ => cx.dcx().bug("expected StaticEnum or StaticStruct in derive(Decodable)"),
    };
    BlockOrExpr::new_expr(expr)
}

/// Creates a decoder for a single enum variant/struct:
/// - `outer_pat_path` is the path to this enum variant/struct
/// - `getarg` should retrieve the `usize`-th field with name `@str`.
fn decode_static_fields<F>(
    cx: &ExtCtxt<'_>,
    trait_span: Span,
    outer_pat_path: ast::Path,
    fields: &StaticFields,
    mut getarg: F,
) -> P<Expr>
where
    F: FnMut(&ExtCtxt<'_>, Span, Symbol, usize) -> P<Expr>,
{
    match fields {
        Unnamed(fields, is_tuple) => {
            let path_expr = cx.expr_path(outer_pat_path);
            if matches!(is_tuple, IsTuple::No) {
                path_expr
            } else {
                let fields = fields
                    .iter()
                    .enumerate()
                    .map(|(i, &span)| getarg(cx, span, Symbol::intern(&format!("_field{i}")), i))
                    .collect();

                cx.expr_call(trait_span, path_expr, fields)
            }
        }
        Named(fields) => {
            // use the field's span to get nicer error messages.
            let fields = fields
                .iter()
                .enumerate()
                .map(|(i, &(ident, span))| {
                    let arg = getarg(cx, span, ident.name, i);
                    cx.field_imm(span, ident, arg)
                })
                .collect();
            cx.expr_struct(trait_span, outer_pat_path, fields)
        }
    }
}