]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/encodable.rs
8fdb994ecdd80ba2142e8cf062abc35395565fe6
[rust.git] / src / libsyntax / ext / deriving / encodable.rs
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
11 /*!
12
13 The compiler code necessary to implement the `#[deriving(Encodable)]`
14 (and `Decodable`, in decodable.rs) extension.  The idea here is that
15 type-defining items may be tagged with `#[deriving(Encodable, Decodable)]`.
16
17 For example, a type like:
18
19 ```ignore
20 #[deriving(Encodable, Decodable)]
21 struct Node { id: uint }
22 ```
23
24 would generate two implementations like:
25
26 ```ignore
27 impl<S:serialize::Encoder> Encodable<S> for Node {
28     fn encode(&self, s: &S) {
29         s.emit_struct("Node", 1, || {
30             s.emit_field("id", 0, || s.emit_uint(self.id))
31         })
32     }
33 }
34
35 impl<D:Decoder> Decodable for node_id {
36     fn decode(d: &D) -> Node {
37         d.read_struct("Node", 1, || {
38             Node {
39                 id: d.read_field("x".to_owned(), 0, || decode(d))
40             }
41         })
42     }
43 }
44 ```
45
46 Other interesting scenarios are whe the item has type parameters or
47 references other non-built-in types.  A type definition like:
48
49 ```ignore
50 #[deriving(Encodable, Decodable)]
51 struct spanned<T> { node: T, span: Span }
52 ```
53
54 would yield functions like:
55
56 ```ignore
57     impl<
58         S: Encoder,
59         T: Encodable<S>
60     > spanned<T>: Encodable<S> {
61         fn encode<S:Encoder>(s: &S) {
62             s.emit_rec(|| {
63                 s.emit_field("node", 0, || self.node.encode(s));
64                 s.emit_field("span", 1, || self.span.encode(s));
65             })
66         }
67     }
68
69     impl<
70         D: Decoder,
71         T: Decodable<D>
72     > spanned<T>: Decodable<D> {
73         fn decode(d: &D) -> spanned<T> {
74             d.read_rec(|| {
75                 {
76                     node: d.read_field("node".to_owned(), 0, || decode(d)),
77                     span: d.read_field("span".to_owned(), 1, || decode(d)),
78                 }
79             })
80         }
81     }
82 ```
83 */
84
85 use ast;
86 use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil};
87 use codemap::Span;
88 use ext::base::ExtCtxt;
89 use ext::build::AstBuilder;
90 use ext::deriving::generic::*;
91 use parse::token;
92
93 pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
94                                  span: Span,
95                                  mitem: @MetaItem,
96                                  item: @Item,
97                                  push: |@Item|) {
98     let trait_def = TraitDef {
99         span: span,
100         attributes: Vec::new(),
101         path: Path::new_(vec!("serialize", "Encodable"), None,
102                          vec!(~Literal(Path::new_local("__S")),
103                               ~Literal(Path::new_local("__E"))), true),
104         additional_bounds: Vec::new(),
105         generics: LifetimeBounds {
106             lifetimes: Vec::new(),
107             bounds: vec!(("__S", ast::StaticSize, vec!(Path::new_(
108                             vec!("serialize", "Encoder"), None,
109                             vec!(~Literal(Path::new_local("__E"))), true))),
110                          ("__E", ast::StaticSize, vec!()))
111         },
112         methods: vec!(
113             MethodDef {
114                 name: "encode",
115                 generics: LifetimeBounds::empty(),
116                 explicit_self: borrowed_explicit_self(),
117                 args: vec!(Ptr(~Literal(Path::new_local("__S")),
118                             Borrowed(None, MutMutable))),
119                 ret_ty: Literal(Path::new_(vec!("std", "result", "Result"),
120                                            None,
121                                            vec!(~Tuple(Vec::new()),
122                                                 ~Literal(Path::new_local("__E"))),
123                                            true)),
124                 attributes: Vec::new(),
125                 const_nonmatching: true,
126                 combine_substructure: encodable_substructure,
127             })
128     };
129
130     trait_def.expand(cx, mitem, item, push)
131 }
132
133 fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
134                           substr: &Substructure) -> @Expr {
135     let encoder = substr.nonself_args[0];
136     // throw an underscore in front to suppress unused variable warnings
137     let blkarg = cx.ident_of("_e");
138     let blkencoder = cx.expr_ident(trait_span, blkarg);
139     let encode = cx.ident_of("encode");
140
141     return match *substr.fields {
142         Struct(ref fields) => {
143             let emit_struct_field = cx.ident_of("emit_struct_field");
144             let mut stmts = Vec::new();
145             let last = fields.len() - 1;
146             for (i, &FieldInfo {
147                     name,
148                     self_,
149                     span,
150                     ..
151                 }) in fields.iter().enumerate() {
152                 let name = match name {
153                     Some(id) => token::get_ident(id),
154                     None => {
155                         token::intern_and_get_ident(format!("_field{}", i))
156                     }
157                 };
158                 let enc = cx.expr_method_call(span, self_, encode, vec!(blkencoder));
159                 let lambda = cx.lambda_expr_1(span, enc, blkarg);
160                 let call = cx.expr_method_call(span, blkencoder,
161                                                emit_struct_field,
162                                                vec!(cx.expr_str(span, name),
163                                                  cx.expr_uint(span, i),
164                                                  lambda));
165
166                 // last call doesn't need a try!
167                 let call = if i != last {
168                     cx.expr_try(span, call)
169                 } else {
170                     cx.expr(span, ExprRet(Some(call)))
171                 };
172                 stmts.push(cx.stmt_expr(call));
173             }
174
175             let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
176             cx.expr_method_call(trait_span,
177                                 encoder,
178                                 cx.ident_of("emit_struct"),
179                                 vec!(
180                 cx.expr_str(trait_span, token::get_ident(substr.type_ident)),
181                 cx.expr_uint(trait_span, fields.len()),
182                 blk
183             ))
184         }
185
186         EnumMatching(idx, variant, ref fields) => {
187             // We're not generating an AST that the borrow checker is expecting,
188             // so we need to generate a unique local variable to take the
189             // mutable loan out on, otherwise we get conflicts which don't
190             // actually exist.
191             let me = cx.stmt_let(trait_span, false, blkarg, encoder);
192             let encoder = cx.expr_ident(trait_span, blkarg);
193             let emit_variant_arg = cx.ident_of("emit_enum_variant_arg");
194             let mut stmts = Vec::new();
195             let last = fields.len() - 1;
196             for (i, &FieldInfo { self_, span, .. }) in fields.iter().enumerate() {
197                 let enc = cx.expr_method_call(span, self_, encode, vec!(blkencoder));
198                 let lambda = cx.lambda_expr_1(span, enc, blkarg);
199                 let call = cx.expr_method_call(span, blkencoder,
200                                                emit_variant_arg,
201                                                vec!(cx.expr_uint(span, i),
202                                                  lambda));
203                 let call = if i != last {
204                     cx.expr_try(span, call)
205                 } else {
206                     cx.expr(span, ExprRet(Some(call)))
207                 };
208                 stmts.push(cx.stmt_expr(call));
209             }
210
211             // enums with no fields need to return Ok()
212             if stmts.len() == 0 {
213                 let ret_ok = cx.expr(trait_span,
214                                      ExprRet(Some(cx.expr_ok(trait_span,
215                                                              cx.expr_lit(trait_span, LitNil)))));
216                 stmts.push(cx.stmt_expr(ret_ok));
217             }
218
219             let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
220             let name = cx.expr_str(trait_span, token::get_ident(variant.node.name));
221             let call = cx.expr_method_call(trait_span, blkencoder,
222                                            cx.ident_of("emit_enum_variant"),
223                                            vec!(name,
224                                              cx.expr_uint(trait_span, idx),
225                                              cx.expr_uint(trait_span, fields.len()),
226                                              blk));
227             let blk = cx.lambda_expr_1(trait_span, call, blkarg);
228             let ret = cx.expr_method_call(trait_span,
229                                           encoder,
230                                           cx.ident_of("emit_enum"),
231                                           vec!(
232                 cx.expr_str(trait_span, token::get_ident(substr.type_ident)),
233                 blk
234             ));
235             cx.expr_block(cx.block(trait_span, vec!(me), Some(ret)))
236         }
237
238         _ => cx.bug("expected Struct or EnumMatching in deriving(Encodable)")
239     };
240 }