2019-02-04 21:49:54 +09:00
|
|
|
use crate::deriving::path_std;
|
|
|
|
|
use crate::deriving::generic::*;
|
|
|
|
|
use crate::deriving::generic::ty::*;
|
2015-12-10 23:23:14 +09:00
|
|
|
|
2016-07-19 23:02:06 +05:30
|
|
|
use syntax::ast::{Expr, MetaItem};
|
2018-12-16 20:23:27 +03:00
|
|
|
use syntax::ext::base::{Annotatable, DummyResult, ExtCtxt};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ext::build::AstBuilder;
|
|
|
|
|
use syntax::ptr::P;
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2019-02-04 21:49:54 +09:00
|
|
|
use syntax::span_err;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2014-05-16 00:16:13 -07:00
|
|
|
|
2019-02-04 21:49:54 +09:00
|
|
|
pub fn expand_deriving_default(cx: &mut ExtCtxt<'_>,
|
2015-03-26 18:07:49 -07:00
|
|
|
span: Span,
|
|
|
|
|
mitem: &MetaItem,
|
2015-05-22 21:10:14 +05:30
|
|
|
item: &Annotatable,
|
2018-07-12 11:58:16 +02:00
|
|
|
push: &mut dyn FnMut(Annotatable)) {
|
2016-11-16 08:21:52 +00:00
|
|
|
let inline = cx.meta_word(span, Symbol::intern("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 {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2014-02-28 13:09:09 -08:00
|
|
|
attributes: Vec::new(),
|
2017-12-06 10:50:55 -08:00
|
|
|
path: path_std!(cx, 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-08-29 11:14:25 +00:00
|
|
|
supports_unions: 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
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:49:54 +09: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(..) => {
|
2018-08-16 02:10:54 +03:00
|
|
|
span_err!(cx, trait_span, E0665,
|
|
|
|
|
"`Default` cannot be derived for enums, only structs");
|
2014-01-18 01:53:10 +11:00
|
|
|
// let compilation continue
|
2018-12-20 03:57:48 +03:00
|
|
|
DummyResult::raw_expr(trait_span, true)
|
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
|
|
|
};
|
|
|
|
|
}
|