2013-09-11 21:51:13 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2015-04-28 17:34:39 +12:00
|
|
|
use ast::{MetaItem, Expr};
|
2013-09-11 21:51:13 -07:00
|
|
|
use codemap::Span;
|
2015-04-28 17:34:39 +12:00
|
|
|
use ext::base::{ExtCtxt, Annotatable};
|
2013-09-11 21:51:13 -07:00
|
|
|
use ext::build::AstBuilder;
|
|
|
|
|
use ext::deriving::generic::*;
|
2014-05-29 12:19:05 +09:00
|
|
|
use ext::deriving::generic::ty::*;
|
2014-04-23 22:43:45 +08:00
|
|
|
use parse::token::InternedString;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2015-03-26 18:07:49 -07:00
|
|
|
pub fn expand_deriving_default(cx: &mut ExtCtxt,
|
|
|
|
|
span: Span,
|
|
|
|
|
mitem: &MetaItem,
|
2015-04-28 17:34:39 +12:00
|
|
|
item: Annotatable,
|
|
|
|
|
push: &mut FnMut(Annotatable))
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2014-04-23 22:43:45 +08:00
|
|
|
let inline = cx.meta_word(span, InternedString::new("inline"));
|
|
|
|
|
let attrs = vec!(cx.attribute(span, inline));
|
2013-09-11 21:51:13 -07:00
|
|
|
let trait_def = TraitDef {
|
2014-02-08 19:39:53 -05:00
|
|
|
span: span,
|
2014-02-28 13:09:09 -08:00
|
|
|
attributes: Vec::new(),
|
2014-09-07 14:57:26 -07:00
|
|
|
path: path_std!(cx, core::default::Default),
|
2014-02-28 13:09:09 -08:00
|
|
|
additional_bounds: Vec::new(),
|
2013-09-11 21:51:13 -07:00
|
|
|
generics: LifetimeBounds::empty(),
|
2014-02-28 13:09:09 -08:00
|
|
|
methods: vec!(
|
2013-09-11 21:51:13 -07:00
|
|
|
MethodDef {
|
|
|
|
|
name: "default",
|
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
|
explicit_self: None,
|
2014-02-28 13:09:09 -08:00
|
|
|
args: Vec::new(),
|
2015-02-10 14:55:45 +01:00
|
|
|
ret_ty: Self_,
|
2014-04-23 22:43:45 +08:00
|
|
|
attributes: attrs,
|
2015-05-17 11:28:19 +05:30
|
|
|
is_unsafe: false,
|
2015-02-15 09:52:21 +01:00
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
2014-04-21 23:25:18 -07:00
|
|
|
default_substructure(a, b, c)
|
2015-02-15 09:52:21 +01:00
|
|
|
}))
|
2015-01-25 00:29:24 -05:00
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
associated_types: Vec::new(),
|
2013-09-11 21:51:13 -07:00
|
|
|
};
|
2015-05-12 14:15:02 +12:00
|
|
|
trait_def.expand(cx, mitem, &item, push)
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
2014-02-28 13:09:09 -08:00
|
|
|
let default_ident = vec!(
|
2014-09-07 14:57:26 -07:00
|
|
|
cx.ident_of_std("core"),
|
2013-09-11 21:51:13 -07:00
|
|
|
cx.ident_of("default"),
|
|
|
|
|
cx.ident_of("Default"),
|
|
|
|
|
cx.ident_of("default")
|
2014-02-28 13:09:09 -08:00
|
|
|
);
|
2015-02-01 12:44:15 -05:00
|
|
|
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
|
2013-09-11 21:51:13 -07:00
|
|
|
|
|
|
|
|
return match *substr.fields {
|
|
|
|
|
StaticStruct(_, ref summary) => {
|
|
|
|
|
match *summary {
|
2013-11-07 18:49:01 +11:00
|
|
|
Unnamed(ref fields) => {
|
|
|
|
|
if fields.is_empty() {
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_ident(trait_span, substr.type_ident)
|
2013-09-11 21:51:13 -07:00
|
|
|
} else {
|
2014-03-28 20:42:34 +01:00
|
|
|
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-07 18:49:01 +11:00
|
|
|
Named(ref fields) => {
|
2014-03-28 20:42:34 +01:00
|
|
|
let default_fields = fields.iter().map(|&(ident, span)| {
|
2013-11-07 18:49:01 +11:00
|
|
|
cx.field_imm(span, ident, default_call(span))
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-28 12:22:53 -08:00
|
|
|
StaticEnum(..) => {
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.span_err(trait_span, "`Default` cannot be derived for enums, only structs");
|
2014-01-18 01:53:10 +11:00
|
|
|
// let compilation continue
|
2015-01-17 23:49:08 +00:00
|
|
|
cx.expr_usize(trait_span, 0)
|
2013-09-11 21:51:13 -07:00
|
|
|
}
|
2015-01-12 02:02:38 -08:00
|
|
|
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`")
|
2013-09-11 21:51:13 -07:00
|
|
|
};
|
|
|
|
|
}
|