]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/default.rs
Honor hidden doc attribute of derivable trait methods
[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 ast::{MetaItem, Item, Expr};
12 use codemap::Span;
13 use ext::base::ExtCtxt;
14 use ext::build::AstBuilder;
15 use ext::deriving::generic::*;
16 use parse::token::InternedString;
17
18 pub fn expand_deriving_default(cx: &mut ExtCtxt,
19                             span: Span,
20                             mitem: @MetaItem,
21                             item: @Item,
22                             push: |@Item|) {
23     let inline = cx.meta_word(span, InternedString::new("inline"));
24     let attrs = vec!(cx.attribute(span, inline));
25     let trait_def = TraitDef {
26         span: span,
27         attributes: Vec::new(),
28         path: Path::new(vec!("std", "default", "Default")),
29         additional_bounds: Vec::new(),
30         generics: LifetimeBounds::empty(),
31         methods: vec!(
32             MethodDef {
33                 name: "default",
34                 generics: LifetimeBounds::empty(),
35                 explicit_self: None,
36                 args: Vec::new(),
37                 ret_ty: Self,
38                 attributes: attrs,
39                 const_nonmatching: false,
40                 combine_substructure: default_substructure
41             })
42     };
43     trait_def.expand(cx, mitem, item, push)
44 }
45
46 fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
47     let default_ident = vec!(
48         cx.ident_of("std"),
49         cx.ident_of("default"),
50         cx.ident_of("Default"),
51         cx.ident_of("default")
52     );
53     let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
54
55     return match *substr.fields {
56         StaticStruct(_, ref summary) => {
57             match *summary {
58                 Unnamed(ref fields) => {
59                     if fields.is_empty() {
60                         cx.expr_ident(trait_span, substr.type_ident)
61                     } else {
62                         let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
63                         cx.expr_call_ident(trait_span, substr.type_ident, exprs)
64                     }
65                 }
66                 Named(ref fields) => {
67                     let default_fields = fields.iter().map(|&(ident, span)| {
68                         cx.field_imm(span, ident, default_call(span))
69                     }).collect();
70                     cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
71                 }
72             }
73         }
74         StaticEnum(..) => {
75             cx.span_err(trait_span, "`Default` cannot be derived for enums, only structs");
76             // let compilation continue
77             cx.expr_uint(trait_span, 0)
78         }
79         _ => cx.span_bug(trait_span, "Non-static method in `deriving(Default)`")
80     };
81 }