]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/clone.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.git] / src / libsyntax / ext / deriving / clone.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_clone<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", "clone", "Clone")),
33         additional_bounds: Vec::new(),
34         generics: LifetimeBounds::empty(),
35         methods: vec!(
36             MethodDef {
37                 name: "clone",
38                 generics: LifetimeBounds::empty(),
39                 explicit_self: borrowed_explicit_self(),
40                 args: Vec::new(),
41                 ret_ty: Self,
42                 attributes: attrs,
43                 combine_substructure: combine_substructure(box |c, s, sub| {
44                     cs_clone("Clone", c, s, sub)
45                 }),
46             }
47         ),
48         associated_types: Vec::new(),
49     };
50
51     trait_def.expand(cx, mitem, item, push)
52 }
53
54 fn cs_clone(
55     name: &str,
56     cx: &mut ExtCtxt, trait_span: Span,
57     substr: &Substructure) -> P<Expr> {
58     let ctor_path;
59     let all_fields;
60     let fn_path = vec![
61         cx.ident_of("std"),
62         cx.ident_of("clone"),
63         cx.ident_of("Clone"),
64         cx.ident_of("clone"),
65     ];
66     let subcall = |field: &FieldInfo| {
67         let args = vec![cx.expr_addr_of(field.span, field.self_.clone())];
68
69         cx.expr_call_global(field.span, fn_path.clone(), args)
70     };
71
72     match *substr.fields {
73         Struct(ref af) => {
74             ctor_path = cx.path(trait_span, vec![substr.type_ident]);
75             all_fields = af;
76         }
77         EnumMatching(_, variant, ref af) => {
78             ctor_path = cx.path(trait_span, vec![substr.type_ident, variant.node.name]);
79             all_fields = af;
80         },
81         EnumNonMatchingCollapsed (..) => {
82             cx.span_bug(trait_span,
83                         &format!("non-matching enum variants in \
84                                  `derive({})`", name)[])
85         }
86         StaticEnum(..) | StaticStruct(..) => {
87             cx.span_bug(trait_span,
88                         &format!("static method in `derive({})`", name)[])
89         }
90     }
91
92     if all_fields.len() >= 1 && all_fields[0].name.is_none() {
93         // enum-like
94         let subcalls = all_fields.iter().map(subcall).collect();
95         let path = cx.expr_path(ctor_path);
96         cx.expr_call(trait_span, path, subcalls)
97     } else {
98         // struct-like
99         let fields = all_fields.iter().map(|field| {
100             let ident = match field.name {
101                 Some(i) => i,
102                 None => {
103                     cx.span_bug(trait_span,
104                                 &format!("unnamed field in normal struct in \
105                                          `derive({})`", name)[])
106                 }
107             };
108             cx.field_imm(field.span, ident, subcall(field))
109         }).collect::<Vec<_>>();
110
111         if fields.is_empty() {
112             // no fields, so construct like `None`
113             cx.expr_path(ctor_path)
114         } else {
115             cx.expr_struct(trait_span, ctor_path, fields)
116         }
117     }
118 }