]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/default.rs
rollup merge of #20642: michaelwoerister/sane-source-locations-pt1
[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 ext::deriving::generic::ty::*;
17 use parse::token::InternedString;
18 use ptr::P;
19
20 pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
21                                   span: Span,
22                                   mitem: &MetaItem,
23                                   item: &Item,
24                                   push: F) where
25     F: FnOnce(P<Item>),
26 {
27     let inline = cx.meta_word(span, InternedString::new("inline"));
28     let attrs = vec!(cx.attribute(span, inline));
29     let trait_def = TraitDef {
30         span: span,
31         attributes: Vec::new(),
32         path: Path::new(vec!("std", "default", "Default")),
33         additional_bounds: Vec::new(),
34         generics: LifetimeBounds::empty(),
35         methods: vec!(
36             MethodDef {
37                 name: "default",
38                 generics: LifetimeBounds::empty(),
39                 explicit_self: None,
40                 args: Vec::new(),
41                 ret_ty: Self,
42                 attributes: attrs,
43                 combine_substructure: combine_substructure(box |a, b, c| {
44                     default_substructure(a, b, c)
45                 })
46             })
47     };
48     trait_def.expand(cx, mitem, item, push)
49 }
50
51 fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
52     let default_ident = vec!(
53         cx.ident_of("std"),
54         cx.ident_of("default"),
55         cx.ident_of("Default"),
56         cx.ident_of("default")
57     );
58     let default_call = |&: span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
59
60     return match *substr.fields {
61         StaticStruct(_, ref summary) => {
62             match *summary {
63                 Unnamed(ref fields) => {
64                     if fields.is_empty() {
65                         cx.expr_ident(trait_span, substr.type_ident)
66                     } else {
67                         let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
68                         cx.expr_call_ident(trait_span, substr.type_ident, exprs)
69                     }
70                 }
71                 Named(ref fields) => {
72                     let default_fields = fields.iter().map(|&(ident, span)| {
73                         cx.field_imm(span, ident, default_call(span))
74                     }).collect();
75                     cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
76                 }
77             }
78         }
79         StaticEnum(..) => {
80             cx.span_err(trait_span, "`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 }