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-12-10 23:23:14 +09:00
|
|
|
use deriving::generic::*;
|
|
|
|
|
use deriving::generic::ty::*;
|
|
|
|
|
|
2016-07-19 23:02:06 +05:30
|
|
|
use syntax::ast::{Expr, MetaItem};
|
|
|
|
|
use syntax::ext::base::{Annotatable, ExtCtxt};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
|
use syntax::parse::token::InternedString;
|
|
|
|
|
use syntax::ptr::P;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
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-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2016-07-19 23:02:06 +05:30
|
|
|
push: &mut FnMut(Annotatable)) {
|
2014-04-23 22:43:45 +08:00
|
|
|
let inline = cx.meta_word(span, InternedString::new("inline"));
|
2016-07-19 23:02:06 +05:30
|
|
|
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(),
|
2015-08-29 14:50:05 -04:00
|
|
|
is_unsafe: false,
|
2016-07-19 23:02:06 +05:30
|
|
|
methods: vec![MethodDef {
|
|
|
|
|
name: "default",
|
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
|
explicit_self: None,
|
|
|
|
|
args: Vec::new(),
|
|
|
|
|
ret_ty: Self_,
|
|
|
|
|
attributes: attrs,
|
|
|
|
|
is_unsafe: false,
|
|
|
|
|
unify_fieldless_variants: false,
|
|
|
|
|
combine_substructure: combine_substructure(Box::new(|a, b, c| {
|
|
|
|
|
default_substructure(a, b, c)
|
|
|
|
|
})),
|
|
|
|
|
}],
|
2015-01-25 00:29:24 -05:00
|
|
|
associated_types: Vec::new(),
|
2013-09-11 21:51:13 -07:00
|
|
|
};
|
2015-05-22 21:10:14 +05:30
|
|
|
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> {
|
2015-07-29 17:01:14 -07:00
|
|
|
let default_ident = cx.std_path(&["default", "Default", "default"]);
|
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 {
|
2016-08-15 21:28:17 +03:00
|
|
|
Unnamed(ref fields, is_tuple) => {
|
|
|
|
|
if !is_tuple {
|
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) => {
|
2016-07-19 23:02:06 +05:30
|
|
|
let default_fields = fields.iter()
|
|
|
|
|
.map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))
|
|
|
|
|
.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(..) => {
|
2016-07-19 23:02:06 +05:30
|
|
|
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
|
|
|
}
|
2016-07-19 23:02:06 +05:30
|
|
|
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
|
2013-09-11 21:51:13 -07:00
|
|
|
};
|
|
|
|
|
}
|