]>
Commit | Line | Data |
---|---|---|
970d7e83 LB |
1 | // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc | 11 | //! The compiler code necessary for `#[derive(Decodable)]`. See encodable.rs for more. |
970d7e83 | 12 | |
9cc50fc6 SL |
13 | use deriving::generic::*; |
14 | use deriving::generic::ty::*; | |
15 | ||
16 | use syntax::ast; | |
7453a54e | 17 | use syntax::ast::{MetaItem, Expr, Mutability}; |
9cc50fc6 SL |
18 | use syntax::codemap::Span; |
19 | use syntax::ext::base::{ExtCtxt, Annotatable}; | |
20 | use syntax::ext::build::AstBuilder; | |
21 | use syntax::parse::token::InternedString; | |
22 | use syntax::parse::token; | |
23 | use syntax::ptr::P; | |
1a4d82fc | 24 | |
d9579d0f AL |
25 | pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt, |
26 | span: Span, | |
27 | mitem: &MetaItem, | |
62682a34 | 28 | item: &Annotatable, |
d9579d0f | 29 | push: &mut FnMut(Annotatable)) |
1a4d82fc JJ |
30 | { |
31 | expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize") | |
32 | } | |
33 | ||
d9579d0f AL |
34 | pub fn expand_deriving_decodable(cx: &mut ExtCtxt, |
35 | span: Span, | |
36 | mitem: &MetaItem, | |
62682a34 | 37 | item: &Annotatable, |
d9579d0f | 38 | push: &mut FnMut(Annotatable)) |
1a4d82fc JJ |
39 | { |
40 | expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize") | |
41 | } | |
970d7e83 | 42 | |
d9579d0f AL |
43 | fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, |
44 | span: Span, | |
45 | mitem: &MetaItem, | |
62682a34 | 46 | item: &Annotatable, |
d9579d0f AL |
47 | push: &mut FnMut(Annotatable), |
48 | krate: &'static str) | |
1a4d82fc | 49 | { |
e9174d1e | 50 | if cx.crate_root != Some("std") { |
85aaf69f | 51 | // FIXME(#21880): lift this requirement. |
e9174d1e SL |
52 | cx.span_err(span, "this trait cannot be derived with #![no_std] \ |
53 | or #![no_core]"); | |
54 | return | |
85aaf69f SL |
55 | } |
56 | ||
970d7e83 | 57 | let trait_def = TraitDef { |
1a4d82fc JJ |
58 | span: span, |
59 | attributes: Vec::new(), | |
60 | path: Path::new_(vec!(krate, "Decodable"), None, vec!(), true), | |
61 | additional_bounds: Vec::new(), | |
62 | generics: LifetimeBounds::empty(), | |
e9174d1e | 63 | is_unsafe: false, |
1a4d82fc | 64 | methods: vec!( |
970d7e83 LB |
65 | MethodDef { |
66 | name: "decode", | |
1a4d82fc JJ |
67 | generics: LifetimeBounds { |
68 | lifetimes: Vec::new(), | |
69 | bounds: vec!(("__D", vec!(Path::new_( | |
70 | vec!(krate, "Decoder"), None, | |
71 | vec!(), true)))) | |
72 | }, | |
970d7e83 | 73 | explicit_self: None, |
d9579d0f | 74 | args: vec!(Ptr(Box::new(Literal(Path::new_local("__D"))), |
7453a54e | 75 | Borrowed(None, Mutability::Mutable))), |
1a4d82fc | 76 | ret_ty: Literal(Path::new_( |
85aaf69f | 77 | pathvec_std!(cx, core::result::Result), |
1a4d82fc | 78 | None, |
d9579d0f | 79 | vec!(Box::new(Self_), Box::new(Literal(Path::new_( |
1a4d82fc | 80 | vec!["__D", "Error"], None, vec![], false |
d9579d0f | 81 | )))), |
1a4d82fc JJ |
82 | true |
83 | )), | |
84 | attributes: Vec::new(), | |
62682a34 | 85 | is_unsafe: false, |
c34b1796 | 86 | combine_substructure: combine_substructure(Box::new(|a, b, c| { |
1a4d82fc | 87 | decodable_substructure(a, b, c, krate) |
c34b1796 | 88 | })), |
85aaf69f SL |
89 | } |
90 | ), | |
91 | associated_types: Vec::new(), | |
970d7e83 LB |
92 | }; |
93 | ||
62682a34 | 94 | trait_def.expand(cx, mitem, item, push) |
970d7e83 LB |
95 | } |
96 | ||
1a4d82fc JJ |
97 | fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span, |
98 | substr: &Substructure, | |
99 | krate: &str) -> P<Expr> { | |
100 | let decoder = substr.nonself_args[0].clone(); | |
101 | let recurse = vec!(cx.ident_of(krate), | |
970d7e83 | 102 | cx.ident_of("Decodable"), |
1a4d82fc | 103 | cx.ident_of("decode")); |
85aaf69f | 104 | let exprdecode = cx.expr_path(cx.path_global(trait_span, recurse)); |
970d7e83 LB |
105 | // throw an underscore in front to suppress unused variable warnings |
106 | let blkarg = cx.ident_of("_d"); | |
1a4d82fc | 107 | let blkdecoder = cx.expr_ident(trait_span, blkarg); |
970d7e83 LB |
108 | |
109 | return match *substr.fields { | |
110 | StaticStruct(_, ref summary) => { | |
111 | let nfields = match *summary { | |
1a4d82fc JJ |
112 | Unnamed(ref fields) => fields.len(), |
113 | Named(ref fields) => fields.len() | |
970d7e83 LB |
114 | }; |
115 | let read_struct_field = cx.ident_of("read_struct_field"); | |
116 | ||
1a4d82fc JJ |
117 | let path = cx.path_ident(trait_span, substr.type_ident); |
118 | let result = decode_static_fields(cx, | |
119 | trait_span, | |
120 | path, | |
121 | summary, | |
122 | |cx, span, name, field| { | |
123 | cx.expr_try(span, | |
124 | cx.expr_method_call(span, blkdecoder.clone(), read_struct_field, | |
125 | vec!(cx.expr_str(span, name), | |
85aaf69f SL |
126 | cx.expr_usize(span, field), |
127 | exprdecode.clone()))) | |
1a4d82fc JJ |
128 | }); |
129 | let result = cx.expr_ok(trait_span, result); | |
130 | cx.expr_method_call(trait_span, | |
131 | decoder, | |
132 | cx.ident_of("read_struct"), | |
133 | vec!( | |
c1a9b12d | 134 | cx.expr_str(trait_span, substr.type_ident.name.as_str()), |
85aaf69f | 135 | cx.expr_usize(trait_span, nfields), |
1a4d82fc JJ |
136 | cx.lambda_expr_1(trait_span, result, blkarg) |
137 | )) | |
970d7e83 LB |
138 | } |
139 | StaticEnum(_, ref fields) => { | |
140 | let variant = cx.ident_of("i"); | |
141 | ||
1a4d82fc JJ |
142 | let mut arms = Vec::new(); |
143 | let mut variants = Vec::new(); | |
970d7e83 LB |
144 | let rvariant_arg = cx.ident_of("read_enum_variant_arg"); |
145 | ||
c1a9b12d SL |
146 | for (i, &(ident, v_span, ref parts)) in fields.iter().enumerate() { |
147 | variants.push(cx.expr_str(v_span, ident.name.as_str())); | |
1a4d82fc | 148 | |
c1a9b12d | 149 | let path = cx.path(trait_span, vec![substr.type_ident, ident]); |
1a4d82fc JJ |
150 | let decoded = decode_static_fields(cx, |
151 | v_span, | |
152 | path, | |
153 | parts, | |
154 | |cx, span, _, field| { | |
85aaf69f | 155 | let idx = cx.expr_usize(span, field); |
1a4d82fc JJ |
156 | cx.expr_try(span, |
157 | cx.expr_method_call(span, blkdecoder.clone(), rvariant_arg, | |
85aaf69f | 158 | vec!(idx, exprdecode.clone()))) |
1a4d82fc JJ |
159 | }); |
160 | ||
161 | arms.push(cx.arm(v_span, | |
85aaf69f | 162 | vec!(cx.pat_lit(v_span, cx.expr_usize(v_span, i))), |
970d7e83 LB |
163 | decoded)); |
164 | } | |
165 | ||
1a4d82fc | 166 | arms.push(cx.arm_unreachable(trait_span)); |
970d7e83 | 167 | |
1a4d82fc JJ |
168 | let result = cx.expr_ok(trait_span, |
169 | cx.expr_match(trait_span, | |
170 | cx.expr_ident(trait_span, variant), arms)); | |
171 | let lambda = cx.lambda_expr(trait_span, vec!(blkarg, variant), result); | |
172 | let variant_vec = cx.expr_vec(trait_span, variants); | |
173 | let variant_vec = cx.expr_addr_of(trait_span, variant_vec); | |
174 | let result = cx.expr_method_call(trait_span, blkdecoder, | |
970d7e83 | 175 | cx.ident_of("read_enum_variant"), |
1a4d82fc JJ |
176 | vec!(variant_vec, lambda)); |
177 | cx.expr_method_call(trait_span, | |
178 | decoder, | |
179 | cx.ident_of("read_enum"), | |
180 | vec!( | |
c1a9b12d | 181 | cx.expr_str(trait_span, substr.type_ident.name.as_str()), |
1a4d82fc JJ |
182 | cx.lambda_expr_1(trait_span, result, blkarg) |
183 | )) | |
970d7e83 | 184 | } |
85aaf69f | 185 | _ => cx.bug("expected StaticEnum or StaticStruct in derive(Decodable)") |
970d7e83 LB |
186 | }; |
187 | } | |
1a4d82fc JJ |
188 | |
189 | /// Create a decoder for a single enum variant/struct: | |
190 | /// - `outer_pat_path` is the path to this enum variant/struct | |
85aaf69f | 191 | /// - `getarg` should retrieve the `usize`-th field with name `@str`. |
1a4d82fc JJ |
192 | fn decode_static_fields<F>(cx: &mut ExtCtxt, |
193 | trait_span: Span, | |
194 | outer_pat_path: ast::Path, | |
195 | fields: &StaticFields, | |
196 | mut getarg: F) | |
197 | -> P<Expr> where | |
85aaf69f | 198 | F: FnMut(&mut ExtCtxt, Span, InternedString, usize) -> P<Expr>, |
1a4d82fc JJ |
199 | { |
200 | match *fields { | |
201 | Unnamed(ref fields) => { | |
202 | let path_expr = cx.expr_path(outer_pat_path); | |
203 | if fields.is_empty() { | |
204 | path_expr | |
205 | } else { | |
206 | let fields = fields.iter().enumerate().map(|(i, &span)| { | |
207 | getarg(cx, span, | |
c34b1796 | 208 | token::intern_and_get_ident(&format!("_field{}", i)), |
1a4d82fc JJ |
209 | i) |
210 | }).collect(); | |
211 | ||
212 | cx.expr_call(trait_span, path_expr, fields) | |
213 | } | |
214 | } | |
215 | Named(ref fields) => { | |
216 | // use the field's span to get nicer error messages. | |
c1a9b12d SL |
217 | let fields = fields.iter().enumerate().map(|(i, &(ident, span))| { |
218 | let arg = getarg(cx, span, ident.name.as_str(), i); | |
219 | cx.field_imm(span, ident, arg) | |
1a4d82fc JJ |
220 | }).collect(); |
221 | cx.expr_struct(trait_span, outer_pat_path, fields) | |
222 | } | |
223 | } | |
224 | } |