2013-04-10 16:31:51 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-16 08:55:57 +10:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The compiler code necessary to implement the #[deriving(Encodable)]
|
|
|
|
(and Decodable, in decodable.rs) extension. The idea here is that
|
|
|
|
type-defining items may be tagged with #[deriving(Encodable,
|
|
|
|
Decodable)].
|
|
|
|
|
|
|
|
For example, a type like:
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable)]
|
|
|
|
struct Node {id: uint}
|
|
|
|
|
|
|
|
would generate two implementations like:
|
|
|
|
|
2014-02-05 08:52:54 -08:00
|
|
|
impl<S:serialize::Encoder> Encodable<S> for Node {
|
2013-05-16 08:55:57 +10:00
|
|
|
fn encode(&self, s: &S) {
|
2013-11-20 16:23:04 -08:00
|
|
|
s.emit_struct("Node", 1, || {
|
2013-05-16 08:55:57 +10:00
|
|
|
s.emit_field("id", 0, || s.emit_uint(self.id))
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-05-16 08:55:57 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D:Decoder> Decodable for node_id {
|
|
|
|
fn decode(d: &D) -> Node {
|
2013-11-20 16:23:04 -08:00
|
|
|
d.read_struct("Node", 1, || {
|
2013-05-16 08:55:57 +10:00
|
|
|
Node {
|
|
|
|
id: d.read_field(~"x", 0, || decode(d))
|
|
|
|
}
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-05-16 08:55:57 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Other interesting scenarios are whe the item has type parameters or
|
|
|
|
references other non-built-in types. A type definition like:
|
|
|
|
|
|
|
|
#[deriving(Encodable, Decodable)]
|
2013-08-31 18:13:04 +02:00
|
|
|
struct spanned<T> {node: T, span: Span}
|
2013-05-16 08:55:57 +10:00
|
|
|
|
|
|
|
would yield functions like:
|
|
|
|
|
|
|
|
impl<
|
|
|
|
S: Encoder,
|
|
|
|
T: Encodable<S>
|
|
|
|
> spanned<T>: Encodable<S> {
|
|
|
|
fn encode<S:Encoder>(s: &S) {
|
2013-11-20 16:23:04 -08:00
|
|
|
s.emit_rec(|| {
|
2013-05-16 08:55:57 +10:00
|
|
|
s.emit_field("node", 0, || self.node.encode(s));
|
|
|
|
s.emit_field("span", 1, || self.span.encode(s));
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-05-16 08:55:57 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
|
|
|
D: Decoder,
|
|
|
|
T: Decodable<D>
|
|
|
|
> spanned<T>: Decodable<D> {
|
|
|
|
fn decode(d: &D) -> spanned<T> {
|
2013-11-20 16:23:04 -08:00
|
|
|
d.read_rec(|| {
|
2013-05-16 08:55:57 +10:00
|
|
|
{
|
|
|
|
node: d.read_field(~"node", 0, || decode(d)),
|
|
|
|
span: d.read_field(~"span", 1, || decode(d)),
|
|
|
|
}
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-05-16 08:55:57 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
use ast::{MetaItem, Item, Expr, MutMutable};
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::Span;
|
2013-05-17 21:27:17 +10:00
|
|
|
use ext::base::ExtCtxt;
|
2013-05-18 00:19:28 +10:00
|
|
|
use ext::build::AstBuilder;
|
2013-05-30 16:58:16 -05:00
|
|
|
use ext::deriving::generic::*;
|
2014-01-10 14:02:36 -08:00
|
|
|
use parse::token;
|
2013-05-30 16:58:16 -05:00
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
pub fn expand_deriving_encodable(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
span: Span,
|
2013-07-19 21:51:37 +10:00
|
|
|
mitem: @MetaItem,
|
2014-01-09 15:05:33 +02:00
|
|
|
in_items: ~[@Item]) -> ~[@Item] {
|
2013-05-30 16:58:16 -05:00
|
|
|
let trait_def = TraitDef {
|
2013-12-07 11:57:44 +11:00
|
|
|
cx: cx, span: span,
|
|
|
|
|
2014-02-05 08:52:54 -08:00
|
|
|
path: Path::new_(~["serialize", "Encodable"], None,
|
2013-05-30 16:58:16 -05:00
|
|
|
~[~Literal(Path::new_local("__E"))], true),
|
|
|
|
additional_bounds: ~[],
|
|
|
|
generics: LifetimeBounds {
|
|
|
|
lifetimes: ~[],
|
2014-02-05 08:52:54 -08:00
|
|
|
bounds: ~[("__E", ~[Path::new(~["serialize", "Encoder"])])],
|
2013-05-30 16:58:16 -05:00
|
|
|
},
|
|
|
|
methods: ~[
|
|
|
|
MethodDef {
|
|
|
|
name: "encode",
|
|
|
|
generics: LifetimeBounds::empty(),
|
2014-02-07 00:38:33 +02:00
|
|
|
explicit_self: borrowed_explicit_self(),
|
2013-05-30 16:58:16 -05:00
|
|
|
args: ~[Ptr(~Literal(Path::new_local("__E")),
|
2013-09-02 03:45:37 +02:00
|
|
|
Borrowed(None, MutMutable))],
|
2013-05-30 16:58:16 -05:00
|
|
|
ret_ty: nil_ty(),
|
2013-11-19 11:13:34 +11:00
|
|
|
inline: false,
|
2013-05-30 16:58:16 -05:00
|
|
|
const_nonmatching: true,
|
|
|
|
combine_substructure: encodable_substructure,
|
|
|
|
},
|
2013-04-10 16:31:51 -07:00
|
|
|
]
|
2013-05-30 16:58:16 -05:00
|
|
|
};
|
2013-04-10 16:31:51 -07:00
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_def.expand(mitem, in_items)
|
2013-04-10 16:31:51 -07:00
|
|
|
}
|
|
|
|
|
2014-01-27 15:25:37 +11:00
|
|
|
fn encodable_substructure(cx: &ExtCtxt, trait_span: Span,
|
2013-09-02 03:45:37 +02:00
|
|
|
substr: &Substructure) -> @Expr {
|
2013-05-30 16:58:16 -05:00
|
|
|
let encoder = substr.nonself_args[0];
|
|
|
|
// throw an underscore in front to suppress unused variable warnings
|
|
|
|
let blkarg = cx.ident_of("_e");
|
2014-01-27 15:25:37 +11:00
|
|
|
let blkencoder = cx.expr_ident(trait_span, blkarg);
|
2013-05-30 16:58:16 -05:00
|
|
|
let encode = cx.ident_of("encode");
|
|
|
|
|
|
|
|
return match *substr.fields {
|
|
|
|
Struct(ref fields) => {
|
|
|
|
let emit_struct_field = cx.ident_of("emit_struct_field");
|
|
|
|
let mut stmts = ~[];
|
2014-01-10 14:02:36 -08:00
|
|
|
for (i, &FieldInfo {
|
|
|
|
name,
|
|
|
|
self_,
|
|
|
|
span,
|
|
|
|
..
|
|
|
|
}) in fields.iter().enumerate() {
|
2014-01-27 15:25:37 +11:00
|
|
|
let name = match name {
|
2014-01-21 10:08:10 -08:00
|
|
|
Some(id) => token::get_ident(id.name),
|
2014-01-10 14:02:36 -08:00
|
|
|
None => {
|
|
|
|
token::intern_and_get_ident(format!("_field{}", i))
|
|
|
|
}
|
2013-05-30 16:58:16 -05:00
|
|
|
};
|
2014-01-27 15:25:37 +11:00
|
|
|
let enc = cx.expr_method_call(span, self_, encode, ~[blkencoder]);
|
2013-05-30 16:58:16 -05:00
|
|
|
let lambda = cx.lambda_expr_1(span, enc, blkarg);
|
|
|
|
let call = cx.expr_method_call(span, blkencoder,
|
|
|
|
emit_struct_field,
|
|
|
|
~[cx.expr_str(span, name),
|
|
|
|
cx.expr_uint(span, i),
|
|
|
|
lambda]);
|
|
|
|
stmts.push(cx.stmt_expr(call));
|
2013-04-10 16:31:51 -07:00
|
|
|
}
|
|
|
|
|
2014-01-27 15:25:37 +11:00
|
|
|
let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
|
2014-01-10 14:02:36 -08:00
|
|
|
cx.expr_method_call(trait_span,
|
|
|
|
encoder,
|
|
|
|
cx.ident_of("emit_struct"),
|
|
|
|
~[
|
|
|
|
cx.expr_str(trait_span,
|
|
|
|
token::get_ident(substr.type_ident.name)),
|
|
|
|
cx.expr_uint(trait_span, fields.len()),
|
|
|
|
blk
|
|
|
|
])
|
2013-05-30 16:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
EnumMatching(idx, variant, ref fields) => {
|
|
|
|
// We're not generating an AST that the borrow checker is expecting,
|
|
|
|
// so we need to generate a unique local variable to take the
|
|
|
|
// mutable loan out on, otherwise we get conflicts which don't
|
|
|
|
// actually exist.
|
2014-01-27 15:25:37 +11:00
|
|
|
let me = cx.stmt_let(trait_span, false, blkarg, encoder);
|
|
|
|
let encoder = cx.expr_ident(trait_span, blkarg);
|
2013-05-30 16:58:16 -05:00
|
|
|
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg");
|
|
|
|
let mut stmts = ~[];
|
2014-01-27 15:25:37 +11:00
|
|
|
for (i, &FieldInfo { self_, span, .. }) in fields.iter().enumerate() {
|
|
|
|
let enc = cx.expr_method_call(span, self_, encode, ~[blkencoder]);
|
2013-05-30 16:58:16 -05:00
|
|
|
let lambda = cx.lambda_expr_1(span, enc, blkarg);
|
|
|
|
let call = cx.expr_method_call(span, blkencoder,
|
|
|
|
emit_variant_arg,
|
|
|
|
~[cx.expr_uint(span, i),
|
|
|
|
lambda]);
|
|
|
|
stmts.push(cx.stmt_expr(call));
|
|
|
|
}
|
2013-04-10 16:31:51 -07:00
|
|
|
|
2014-01-27 15:25:37 +11:00
|
|
|
let blk = cx.lambda_stmts_1(trait_span, stmts, blkarg);
|
2014-01-10 14:02:36 -08:00
|
|
|
let name = cx.expr_str(trait_span,
|
2014-01-21 10:08:10 -08:00
|
|
|
token::get_ident(variant.node.name.name));
|
2014-01-27 15:25:37 +11:00
|
|
|
let call = cx.expr_method_call(trait_span, blkencoder,
|
2013-05-30 16:58:16 -05:00
|
|
|
cx.ident_of("emit_enum_variant"),
|
|
|
|
~[name,
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_uint(trait_span, idx),
|
|
|
|
cx.expr_uint(trait_span, fields.len()),
|
2013-05-30 16:58:16 -05:00
|
|
|
blk]);
|
2014-01-27 15:25:37 +11:00
|
|
|
let blk = cx.lambda_expr_1(trait_span, call, blkarg);
|
2014-01-10 14:02:36 -08:00
|
|
|
let ret = cx.expr_method_call(trait_span,
|
|
|
|
encoder,
|
2013-05-30 16:58:16 -05:00
|
|
|
cx.ident_of("emit_enum"),
|
2014-01-10 14:02:36 -08:00
|
|
|
~[
|
|
|
|
cx.expr_str(trait_span,
|
|
|
|
token::get_ident(substr.type_ident.name)),
|
|
|
|
blk
|
|
|
|
]);
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_block(cx.block(trait_span, ~[me], Some(ret)))
|
2013-05-30 16:58:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => cx.bug("expected Struct or EnumMatching in deriving(Encodable)")
|
2013-04-10 16:31:51 -07:00
|
|
|
};
|
2013-05-16 08:55:57 +10:00
|
|
|
}
|