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