2014-02-06 23:02:28 +11:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
use ast;
|
2015-04-28 17:34:39 +12:00
|
|
|
use ast::{MetaItem, Expr,};
|
2014-02-06 23:02:28 +11:00
|
|
|
use codemap::Span;
|
2015-04-28 17:34:39 +12:00
|
|
|
use ext::base::{ExtCtxt, Annotatable};
|
2014-02-06 23:02:28 +11:00
|
|
|
use ext::build::AstBuilder;
|
|
|
|
use ext::deriving::generic::*;
|
2014-05-29 12:19:05 +09:00
|
|
|
use ext::deriving::generic::ty::*;
|
2014-02-06 23:02:28 +11:00
|
|
|
use parse::token;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2014-02-06 23:02:28 +11:00
|
|
|
|
2015-10-18 19:03:42 +03:00
|
|
|
pub fn expand_deriving_debug(cx: &mut ExtCtxt,
|
2015-03-26 18:07:49 -07:00
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2015-04-28 17:34:39 +12:00
|
|
|
push: &mut FnMut(Annotatable))
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2014-02-06 23:02:28 +11:00
|
|
|
// &mut ::std::fmt::Formatter
|
2015-04-15 20:56:16 -07:00
|
|
|
let fmtr = Ptr(Box::new(Literal(path_std!(cx, core::fmt::Formatter))),
|
2014-02-06 23:02:28 +11:00
|
|
|
Borrowed(None, ast::MutMutable));
|
|
|
|
|
|
|
|
let trait_def = TraitDef {
|
2014-02-08 19:39:53 -05:00
|
|
|
span: span,
|
2014-02-28 13:09:09 -08:00
|
|
|
attributes: Vec::new(),
|
2014-09-07 14:57:26 -07:00
|
|
|
path: path_std!(cx, core::fmt::Debug),
|
2014-02-28 13:09:09 -08:00
|
|
|
additional_bounds: Vec::new(),
|
2014-02-06 23:02:28 +11:00
|
|
|
generics: LifetimeBounds::empty(),
|
2015-08-29 14:50:05 -04:00
|
|
|
is_unsafe: false,
|
2015-01-25 00:29:24 -05:00
|
|
|
methods: vec![
|
2014-02-06 23:02:28 +11:00
|
|
|
MethodDef {
|
|
|
|
name: "fmt",
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: borrowed_explicit_self(),
|
2014-02-28 13:09:09 -08:00
|
|
|
args: vec!(fmtr),
|
2014-09-07 14:57:26 -07:00
|
|
|
ret_ty: Literal(path_std!(cx, core::fmt::Result)),
|
2014-04-23 22:43:45 +08:00
|
|
|
attributes: Vec::new(),
|
2015-05-17 11:28:19 +05:30
|
|
|
is_unsafe: false,
|
2015-02-15 09:52:21 +01:00
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
2014-04-21 23:25:18 -07:00
|
|
|
show_substructure(a, b, c)
|
2015-02-15 09:52:21 +01:00
|
|
|
}))
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|
2015-01-25 00:29:24 -05:00
|
|
|
],
|
|
|
|
associated_types: Vec::new(),
|
2014-02-06 23:02:28 +11:00
|
|
|
};
|
2015-05-22 21:10:14 +05:30
|
|
|
trait_def.expand(cx, mitem, item, push)
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|
|
|
|
|
2015-03-07 19:36:36 -08:00
|
|
|
/// We use the debug builders to do the heavy lifting here
|
2014-02-06 23:02:28 +11:00
|
|
|
fn show_substructure(cx: &mut ExtCtxt, span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
substr: &Substructure) -> P<Expr> {
|
2015-03-07 19:36:36 -08:00
|
|
|
// build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
|
|
|
|
// or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
|
|
|
|
// based on the "shape".
|
2015-07-28 18:07:20 +02:00
|
|
|
let ident = match *substr.fields {
|
2014-02-06 23:02:28 +11:00
|
|
|
Struct(_) => substr.type_ident,
|
|
|
|
EnumMatching(_, v, _) => v.node.name,
|
2014-07-07 09:13:49 +02:00
|
|
|
EnumNonMatchingCollapsed(..) | StaticStruct(..) | StaticEnum(..) => {
|
2015-01-20 15:45:07 -08:00
|
|
|
cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-07 19:36:36 -08:00
|
|
|
// We want to make sure we have the expn_id set so that we can use unstable methods
|
|
|
|
let span = Span { expn_id: cx.backtrace(), .. span };
|
2015-07-28 18:07:20 +02:00
|
|
|
let name = cx.expr_lit(span, ast::Lit_::LitStr(ident.name.as_str(),
|
2015-03-07 19:36:36 -08:00
|
|
|
ast::StrStyle::CookedStr));
|
|
|
|
let mut expr = substr.nonself_args[0].clone();
|
2014-02-06 23:02:28 +11:00
|
|
|
|
|
|
|
match *substr.fields {
|
|
|
|
Struct(ref fields) | EnumMatching(_, _, ref fields) => {
|
2015-05-17 14:03:37 -07:00
|
|
|
|
2015-03-07 19:36:36 -08:00
|
|
|
if fields.is_empty() || fields[0].name.is_none() {
|
2014-02-06 23:02:28 +11:00
|
|
|
// tuple struct/"normal" variant
|
2015-03-07 19:36:36 -08:00
|
|
|
expr = cx.expr_method_call(span,
|
|
|
|
expr,
|
|
|
|
token::str_to_ident("debug_tuple"),
|
|
|
|
vec![name]);
|
|
|
|
|
|
|
|
for field in fields {
|
2015-05-17 14:03:37 -07:00
|
|
|
// Use double indirection to make sure this works for unsized types
|
|
|
|
let field = cx.expr_addr_of(field.span, field.self_.clone());
|
|
|
|
let field = cx.expr_addr_of(field.span, field);
|
|
|
|
|
2015-03-07 19:36:36 -08:00
|
|
|
expr = cx.expr_method_call(span,
|
|
|
|
expr,
|
|
|
|
token::str_to_ident("field"),
|
2015-05-17 14:03:37 -07:00
|
|
|
vec![field]);
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// normal struct/struct variant
|
2015-03-07 19:36:36 -08:00
|
|
|
expr = cx.expr_method_call(span,
|
|
|
|
expr,
|
|
|
|
token::str_to_ident("debug_struct"),
|
|
|
|
vec![name]);
|
|
|
|
|
|
|
|
for field in fields {
|
|
|
|
let name = cx.expr_lit(field.span, ast::Lit_::LitStr(
|
2015-07-28 18:07:20 +02:00
|
|
|
field.name.unwrap().name.as_str(),
|
2015-03-07 19:36:36 -08:00
|
|
|
ast::StrStyle::CookedStr));
|
2015-05-17 14:03:37 -07:00
|
|
|
|
|
|
|
// Use double indirection to make sure this works for unsized types
|
|
|
|
let field = cx.expr_addr_of(field.span, field.self_.clone());
|
|
|
|
let field = cx.expr_addr_of(field.span, field);
|
2015-03-07 19:36:36 -08:00
|
|
|
expr = cx.expr_method_call(span,
|
|
|
|
expr,
|
|
|
|
token::str_to_ident("field"),
|
2015-05-17 14:03:37 -07:00
|
|
|
vec![name, field]);
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
|
2015-03-07 19:36:36 -08:00
|
|
|
cx.expr_method_call(span,
|
|
|
|
expr,
|
|
|
|
token::str_to_ident("finish"),
|
|
|
|
vec![])
|
2014-02-06 23:02:28 +11:00
|
|
|
}
|