]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/deriving/default.rs
Replace AstBuilder with inherent methods
[rust.git] / src / libsyntax_ext / deriving / default.rs
1 use crate::deriving::path_std;
2 use crate::deriving::generic::*;
3 use crate::deriving::generic::ty::*;
4
5 use syntax::ast::{Expr, MetaItem};
6 use syntax::ext::base::{Annotatable, DummyResult, ExtCtxt};
7 use syntax::ptr::P;
8 use syntax::symbol::{kw, sym};
9 use syntax::span_err;
10 use syntax_pos::Span;
11
12 pub fn expand_deriving_default(cx: &mut ExtCtxt<'_>,
13                                span: Span,
14                                mitem: &MetaItem,
15                                item: &Annotatable,
16                                push: &mut dyn FnMut(Annotatable)) {
17     let inline = cx.meta_word(span, sym::inline);
18     let attrs = vec![cx.attribute(inline)];
19     let trait_def = TraitDef {
20         span,
21         attributes: Vec::new(),
22         path: path_std!(cx, default::Default),
23         additional_bounds: Vec::new(),
24         generics: LifetimeBounds::empty(),
25         is_unsafe: false,
26         supports_unions: false,
27         methods: vec![MethodDef {
28                           name: "default",
29                           generics: LifetimeBounds::empty(),
30                           explicit_self: None,
31                           args: Vec::new(),
32                           ret_ty: Self_,
33                           attributes: attrs,
34                           is_unsafe: false,
35                           unify_fieldless_variants: false,
36                           combine_substructure: combine_substructure(Box::new(|a, b, c| {
37                               default_substructure(a, b, c)
38                           })),
39                       }],
40         associated_types: Vec::new(),
41     };
42     trait_def.expand(cx, mitem, item, push)
43 }
44
45 fn default_substructure(cx: &mut ExtCtxt<'_>,
46                         trait_span: Span,
47                         substr: &Substructure<'_>)
48                         -> P<Expr> {
49     // Note that `kw::Default` is "default" and `sym::Default` is "Default"!
50     let default_ident = cx.std_path(&[kw::Default, sym::Default, kw::Default]);
51     let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
52
53     return match *substr.fields {
54         StaticStruct(_, ref summary) => {
55             match *summary {
56                 Unnamed(ref fields, is_tuple) => {
57                     if !is_tuple {
58                         cx.expr_ident(trait_span, substr.type_ident)
59                     } else {
60                         let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
61                         cx.expr_call_ident(trait_span, substr.type_ident, exprs)
62                     }
63                 }
64                 Named(ref fields) => {
65                     let default_fields = fields.iter()
66                         .map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
67                         .collect();
68                     cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
69                 }
70             }
71         }
72         StaticEnum(..) => {
73             span_err!(cx, trait_span, E0665,
74                       "`Default` cannot be derived for enums, only structs");
75             // let compilation continue
76             DummyResult::raw_expr(trait_span, true)
77         }
78         _ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
79     };
80 }