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