2013-06-14 18:27:35 -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.
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
use ast::{MetaItem, Item, Expr};
|
2013-08-31 18:13:04 +02:00
|
|
|
use codemap::Span;
|
2013-06-14 18:27:35 -07:00
|
|
|
use ext::base::ExtCtxt;
|
|
|
|
use ext::build::AstBuilder;
|
|
|
|
use ext::deriving::generic::*;
|
|
|
|
|
2013-12-27 17:17:36 -07:00
|
|
|
pub fn expand_deriving_zero(cx: &ExtCtxt,
|
2013-08-31 18:13:04 +02:00
|
|
|
span: Span,
|
2013-07-19 21:51:37 +10:00
|
|
|
mitem: @MetaItem,
|
2014-01-09 15:05:33 +02:00
|
|
|
in_items: ~[@Item])
|
|
|
|
-> ~[@Item] {
|
2013-06-14 18:27:35 -07:00
|
|
|
let trait_def = TraitDef {
|
2013-12-07 11:57:44 +11:00
|
|
|
cx: cx, span: span,
|
|
|
|
|
2013-06-14 18:27:35 -07:00
|
|
|
path: Path::new(~["std", "num", "Zero"]),
|
|
|
|
additional_bounds: ~[],
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
methods: ~[
|
|
|
|
MethodDef {
|
|
|
|
name: "zero",
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: None,
|
|
|
|
args: ~[],
|
|
|
|
ret_ty: Self,
|
2013-11-19 11:13:34 +11:00
|
|
|
inline: true,
|
2013-06-14 18:27:35 -07:00
|
|
|
const_nonmatching: false,
|
|
|
|
combine_substructure: zero_substructure
|
|
|
|
},
|
|
|
|
MethodDef {
|
|
|
|
name: "is_zero",
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: borrowed_explicit_self(),
|
|
|
|
args: ~[],
|
|
|
|
ret_ty: Literal(Path::new(~["bool"])),
|
2013-11-19 11:13:34 +11:00
|
|
|
inline: true,
|
2013-06-14 18:27:35 -07:00
|
|
|
const_nonmatching: false,
|
|
|
|
combine_substructure: |cx, span, substr| {
|
|
|
|
cs_and(|cx, span, _, _| cx.span_bug(span,
|
|
|
|
"Non-matching enum \
|
|
|
|
variant in \
|
|
|
|
deriving(Zero)"),
|
|
|
|
cx, span, substr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_def.expand(mitem, in_items)
|
2013-06-14 18:27:35 -07:00
|
|
|
}
|
|
|
|
|
2014-01-27 15:25:37 +11:00
|
|
|
fn zero_substructure(cx: &ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
|
2013-06-14 18:27:35 -07:00
|
|
|
let zero_ident = ~[
|
|
|
|
cx.ident_of("std"),
|
|
|
|
cx.ident_of("num"),
|
|
|
|
cx.ident_of("Zero"),
|
|
|
|
cx.ident_of("zero")
|
|
|
|
];
|
2013-11-07 18:49:01 +11:00
|
|
|
let zero_call = |span| cx.expr_call_global(span, zero_ident.clone(), ~[]);
|
2013-06-14 18:27:35 -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-06-14 18:27:35 -07:00
|
|
|
} else {
|
2013-11-07 18:49:01 +11:00
|
|
|
let exprs = fields.map(|sp| zero_call(*sp));
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
|
2013-06-14 18:27:35 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 18:49:01 +11:00
|
|
|
Named(ref fields) => {
|
2013-11-20 16:23:04 -08:00
|
|
|
let zero_fields = fields.map(|&(ident, span)| {
|
2013-11-07 18:49:01 +11:00
|
|
|
cx.field_imm(span, ident, zero_call(span))
|
2013-11-20 16:23:04 -08:00
|
|
|
});
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_struct_ident(trait_span, substr.type_ident, zero_fields)
|
2013-06-14 18:27:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-28 12:22:53 -08:00
|
|
|
StaticEnum(..) => {
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.span_err(trait_span, "`Zero` cannot be derived for enums, only structs");
|
2014-01-18 01:53:10 +11:00
|
|
|
// let compilation continue
|
2014-01-27 15:25:37 +11:00
|
|
|
cx.expr_uint(trait_span, 0)
|
2013-06-14 18:27:35 -07:00
|
|
|
}
|
|
|
|
_ => cx.bug("Non-static method in `deriving(Zero)`")
|
|
|
|
};
|
|
|
|
}
|