1 use crate::deriving::generic::ty::*;
2 use crate::deriving::generic::*;
3 use crate::deriving::path_std;
4 
5 use rustc_ast::ptr::P;
6 use rustc_ast::{self as ast, Expr, LocalKind, MetaItem};
7 use rustc_expand::base::{Annotatable, ExtCtxt};
8 use rustc_span::symbol::{sym, Ident};
9 use rustc_span::{Span, DUMMY_SP};
10 
make_mut_borrow(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<Expr>) -> P<Expr>11 fn make_mut_borrow(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<Expr>) -> P<Expr> {
12     cx.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Mut, expr))
13 }
14 
expand_deriving_debug( cx: &mut ExtCtxt<'_>, span: Span, mitem: &MetaItem, item: &Annotatable, push: &mut dyn FnMut(Annotatable), )15 pub fn expand_deriving_debug(
16     cx: &mut ExtCtxt<'_>,
17     span: Span,
18     mitem: &MetaItem,
19     item: &Annotatable,
20     push: &mut dyn FnMut(Annotatable),
21 ) {
22     // &mut ::std::fmt::Formatter
23     let fmtr =
24         Ptr(Box::new(Literal(path_std!(fmt::Formatter))), Borrowed(None, ast::Mutability::Mut));
25 
26     let trait_def = TraitDef {
27         span,
28         attributes: Vec::new(),
29         path: path_std!(fmt::Debug),
30         additional_bounds: Vec::new(),
31         generics: Bounds::empty(),
32         is_unsafe: false,
33         supports_unions: false,
34         methods: vec![MethodDef {
35             name: sym::fmt,
36             generics: Bounds::empty(),
37             explicit_self: borrowed_explicit_self(),
38             args: vec![(fmtr, sym::f)],
39             ret_ty: Literal(path_std!(fmt::Result)),
40             attributes: Vec::new(),
41             is_unsafe: false,
42             unify_fieldless_variants: false,
43             combine_substructure: combine_substructure(Box::new(|a, b, c| {
44                 show_substructure(a, b, c)
45             })),
46         }],
47         associated_types: Vec::new(),
48     };
49     trait_def.expand(cx, mitem, item, push)
50 }
51 
52 /// We use the debug builders to do the heavy lifting here
show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr>53 fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {
54     // build fmt.debug_struct(<name>).field(<fieldname>, &<fieldval>)....build()
55     // or fmt.debug_tuple(<name>).field(&<fieldval>)....build()
56     // based on the "shape".
57     let (ident, vdata, fields) = match substr.fields {
58         Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
59         EnumMatching(_, _, v, fields) => (v.ident, &v.data, fields),
60         EnumNonMatchingCollapsed(..) | StaticStruct(..) | StaticEnum(..) => {
61             cx.span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")
62         }
63     };
64 
65     // We want to make sure we have the ctxt set so that we can use unstable methods
66     let span = cx.with_def_site_ctxt(span);
67     let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
68     let fmt = substr.nonself_args[0].clone();
69 
70     // Special fast path for unit variants. In the common case of an enum that is entirely unit
71     // variants (i.e. a C-like enum), this fast path allows LLVM to eliminate the entire switch in
72     // favor of a lookup table.
73     if let ast::VariantData::Unit(..) = vdata {
74         let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
75         let expr = cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]);
76         let stmts = vec![cx.stmt_expr(expr)];
77         let block = cx.block(span, stmts);
78         return cx.expr_block(block);
79     }
80 
81     let builder = Ident::new(sym::debug_trait_builder, span);
82     let builder_expr = cx.expr_ident(span, builder);
83 
84     let mut stmts = Vec::with_capacity(fields.len() + 2);
85     let fn_path_finish;
86     match vdata {
87         ast::VariantData::Unit(..) => {
88             cx.span_bug(span, "unit variants should have been handled above");
89         }
90         ast::VariantData::Tuple(..) => {
91             // tuple struct/"normal" variant
92             let fn_path_debug_tuple = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_tuple]);
93             let expr = cx.expr_call_global(span, fn_path_debug_tuple, vec![fmt, name]);
94             let expr = make_mut_borrow(cx, span, expr);
95             stmts.push(cx.stmt_let(span, false, builder, expr));
96 
97             for field in fields {
98                 // Use double indirection to make sure this works for unsized types
99                 let field = cx.expr_addr_of(field.span, field.self_.clone());
100                 let field = cx.expr_addr_of(field.span, field);
101 
102                 let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::field]);
103                 let expr =
104                     cx.expr_call_global(span, fn_path_field, vec![builder_expr.clone(), field]);
105 
106                 // Use `let _ = expr;` to avoid triggering the
107                 // unused_results lint.
108                 stmts.push(stmt_let_underscore(cx, span, expr));
109             }
110 
111             fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugTuple, sym::finish]);
112         }
113         ast::VariantData::Struct(..) => {
114             // normal struct/struct variant
115             let fn_path_debug_struct = cx.std_path(&[sym::fmt, sym::Formatter, sym::debug_struct]);
116             let expr = cx.expr_call_global(span, fn_path_debug_struct, vec![fmt, name]);
117             let expr = make_mut_borrow(cx, span, expr);
118             stmts.push(cx.stmt_let(DUMMY_SP, false, builder, expr));
119 
120             for field in fields {
121                 let name = cx.expr_lit(
122                     field.span,
123                     ast::LitKind::Str(field.name.unwrap().name, ast::StrStyle::Cooked),
124                 );
125 
126                 // Use double indirection to make sure this works for unsized types
127                 let fn_path_field = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::field]);
128                 let field = cx.expr_addr_of(field.span, field.self_.clone());
129                 let field = cx.expr_addr_of(field.span, field);
130                 let expr = cx.expr_call_global(
131                     span,
132                     fn_path_field,
133                     vec![builder_expr.clone(), name, field],
134                 );
135                 stmts.push(stmt_let_underscore(cx, span, expr));
136             }
137             fn_path_finish = cx.std_path(&[sym::fmt, sym::DebugStruct, sym::finish]);
138         }
139     }
140 
141     let expr = cx.expr_call_global(span, fn_path_finish, vec![builder_expr]);
142 
143     stmts.push(cx.stmt_expr(expr));
144     let block = cx.block(span, stmts);
145     cx.expr_block(block)
146 }
147 
stmt_let_underscore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast::Stmt148 fn stmt_let_underscore(cx: &mut ExtCtxt<'_>, sp: Span, expr: P<ast::Expr>) -> ast::Stmt {
149     let local = P(ast::Local {
150         pat: cx.pat_wild(sp),
151         ty: None,
152         id: ast::DUMMY_NODE_ID,
153         kind: LocalKind::Init(expr),
154         span: sp,
155         attrs: ast::AttrVec::new(),
156         tokens: None,
157     });
158     ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
159 }
160