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: combine_substructure(|a, b, c| {
41 default_substructure(a, b, c)
42 })
43 })
44 };
45 trait_def.expand(cx, mitem, item, push)
46 }
47
48 fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
49 let default_ident = vec!(
50 cx.ident_of("std"),
51 cx.ident_of("default"),
52 cx.ident_of("Default"),
53 cx.ident_of("default")
54 );
55 let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
56
57 return match *substr.fields {
58 StaticStruct(_, ref summary) => {
59 match *summary {
60 Unnamed(ref fields) => {
61 if fields.is_empty() {
62 cx.expr_ident(trait_span, substr.type_ident)
63 } else {
64 let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
65 cx.expr_call_ident(trait_span, substr.type_ident, exprs)
66 }
67 }
68 Named(ref fields) => {
69 let default_fields = fields.iter().map(|&(ident, span)| {
70 cx.field_imm(span, ident, default_call(span))
71 }).collect();
72 cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
73 }
74 }
75 }
76 StaticEnum(..) => {
77 cx.span_err(trait_span, "`Default` cannot be derived for enums, only structs");
78 // let compilation continue
79 cx.expr_uint(trait_span, 0)
80 }
81 _ => cx.span_bug(trait_span, "Non-static method in `deriving(Default)`")
82 };
83 }