]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/ext/deriving/show.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / libsyntax / ext / deriving / show.rs
1 // Copyright 2014 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 use ast;
12 use ast::{MetaItem, Expr,};
13 use codemap::Span;
14 use ext::base::{ExtCtxt, Annotatable};
15 use ext::build::AstBuilder;
16 use ext::deriving::generic::*;
17 use ext::deriving::generic::ty::*;
18 use parse::token;
19 use ptr::P;
20
21 pub fn expand_deriving_show(cx: &mut ExtCtxt,
22 span: Span,
23 mitem: &MetaItem,
24 item: Annotatable,
25 push: &mut FnMut(Annotatable))
26 {
27 // &mut ::std::fmt::Formatter
28 let fmtr = Ptr(Box::new(Literal(path_std!(cx, core::fmt::Formatter))),
29 Borrowed(None, ast::MutMutable));
30
31 let trait_def = TraitDef {
32 span: span,
33 attributes: Vec::new(),
34 path: path_std!(cx, core::fmt::Debug),
35 additional_bounds: Vec::new(),
36 generics: LifetimeBounds::empty(),
37 methods: vec![
38 MethodDef {
39 name: "fmt",
40 generics: LifetimeBounds::empty(),
41 explicit_self: borrowed_explicit_self(),
42 args: vec!(fmtr),
43 ret_ty: Literal(path_std!(cx, core::fmt::Result)),
44 attributes: Vec::new(),
45 combine_substructure: combine_substructure(Box::new(|a, b, c| {
46 show_substructure(a, b, c)
47 }))
48 }
49 ],
50 associated_types: Vec::new(),
51 };
52 trait_def.expand(cx, mitem, &item, push)
53 }
54
55 /// We use the debug builders to do the heavy lifting here
56 fn show_substructure(cx: &mut ExtCtxt, span: Span,
57 substr: &Substructure) -> P<Expr> {
58 // build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
59 // or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
60 // based on the "shape".
61 let name = match *substr.fields {
62 Struct(_) => substr.type_ident,
63 EnumMatching(_, v, _) => v.node.name,
64 EnumNonMatchingCollapsed(..) | StaticStruct(..) | StaticEnum(..) => {
65 cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
66 }
67 };
68
69 // We want to make sure we have the expn_id set so that we can use unstable methods
70 let span = Span { expn_id: cx.backtrace(), .. span };
71 let name = cx.expr_lit(span, ast::Lit_::LitStr(token::get_ident(name),
72 ast::StrStyle::CookedStr));
73 let mut expr = substr.nonself_args[0].clone();
74
75 match *substr.fields {
76 Struct(ref fields) | EnumMatching(_, _, ref fields) => {
77 if fields.is_empty() || fields[0].name.is_none() {
78 // tuple struct/"normal" variant
79 expr = cx.expr_method_call(span,
80 expr,
81 token::str_to_ident("debug_tuple"),
82 vec![name]);
83
84 for field in fields {
85 expr = cx.expr_method_call(span,
86 expr,
87 token::str_to_ident("field"),
88 vec![cx.expr_addr_of(field.span,
89 field.self_.clone())]);
90 }
91 } else {
92 // normal struct/struct variant
93 expr = cx.expr_method_call(span,
94 expr,
95 token::str_to_ident("debug_struct"),
96 vec![name]);
97
98 for field in fields {
99 let name = cx.expr_lit(field.span, ast::Lit_::LitStr(
100 token::get_ident(field.name.clone().unwrap()),
101 ast::StrStyle::CookedStr));
102 expr = cx.expr_method_call(span,
103 expr,
104 token::str_to_ident("field"),
105 vec![name,
106 cx.expr_addr_of(field.span,
107 field.self_.clone())]);
108 }
109 }
110 }
111 _ => unreachable!()
112 }
113
114 cx.expr_method_call(span,
115 expr,
116 token::str_to_ident("finish"),
117 vec![])
118 }