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