2015-04-12 18:19:48 +00:00
|
|
|
// Copyright 2012-2015 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.
|
|
|
|
|
|
|
|
#![feature(plugin_registrar, rustc_private)]
|
2016-11-01 01:13:35 +00:00
|
|
|
#![doc(html_logo_url = "https://rust-num.github.io/num/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "https://rust-num.github.io/num/favicon.ico",
|
|
|
|
html_root_url = "https://rust-num.github.io/num/",
|
2016-11-02 18:51:10 +00:00
|
|
|
html_playground_url = "http://play.integer32.com/")]
|
2015-04-12 18:19:48 +00:00
|
|
|
|
|
|
|
extern crate syntax;
|
2015-12-19 09:57:26 +00:00
|
|
|
extern crate syntax_ext;
|
2015-12-02 01:40:51 +00:00
|
|
|
extern crate rustc_plugin;
|
2015-04-12 18:19:48 +00:00
|
|
|
|
2016-02-15 21:44:51 +00:00
|
|
|
use syntax::ast::{MetaItem, Expr, BinOpKind};
|
2015-04-12 18:19:48 +00:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2015-05-14 17:19:47 +00:00
|
|
|
use syntax::ext::base::{ExtCtxt, Annotatable};
|
2015-04-12 18:19:48 +00:00
|
|
|
use syntax::ext::build::AstBuilder;
|
2015-12-19 09:57:26 +00:00
|
|
|
use syntax_ext::deriving::generic::*;
|
|
|
|
use syntax_ext::deriving::generic::ty::*;
|
2016-11-28 17:40:50 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2015-04-12 18:19:48 +00:00
|
|
|
use syntax::ptr::P;
|
2015-05-14 17:19:47 +00:00
|
|
|
use syntax::ext::base::MultiDecorator;
|
2015-04-12 18:19:48 +00:00
|
|
|
|
2015-12-02 01:40:51 +00:00
|
|
|
use rustc_plugin::Registry;
|
2015-04-12 18:19:48 +00:00
|
|
|
|
|
|
|
macro_rules! pathvec {
|
|
|
|
($($x:ident)::+) => (
|
|
|
|
vec![ $( stringify!($x) ),+ ]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! path {
|
|
|
|
($($x:tt)*) => (
|
2015-12-19 09:57:26 +00:00
|
|
|
::syntax_ext::deriving::generic::ty::Path::new( pathvec!( $($x)* ) )
|
2015-04-12 18:19:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! path_local {
|
|
|
|
($x:ident) => (
|
2015-12-19 09:57:26 +00:00
|
|
|
::syntax_ext::deriving::generic::ty::Path::new_local(stringify!($x))
|
2015-04-12 18:19:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! pathvec_std {
|
2015-08-10 19:11:56 +00:00
|
|
|
($cx:expr, $first:ident :: $($rest:ident)::+) => ({
|
|
|
|
let mut v = pathvec!($($rest)::+);
|
|
|
|
if let Some(s) = $cx.crate_root {
|
|
|
|
v.insert(0, s);
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
2015-08-10 19:11:56 +00:00
|
|
|
v
|
|
|
|
})
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
|
|
|
|
span: Span,
|
|
|
|
mitem: &MetaItem,
|
2015-05-24 19:10:49 +00:00
|
|
|
item: &Annotatable,
|
2015-05-14 17:19:47 +00:00
|
|
|
push: &mut FnMut(Annotatable))
|
2015-04-12 18:19:48 +00:00
|
|
|
{
|
2016-11-28 17:40:50 +00:00
|
|
|
let inline = cx.meta_word(span, Symbol::intern("inline"));
|
2015-04-12 18:19:48 +00:00
|
|
|
let attrs = vec!(cx.attribute(span, inline));
|
|
|
|
let trait_def = TraitDef {
|
2015-09-01 20:12:02 +00:00
|
|
|
is_unsafe: false,
|
2015-04-12 18:19:48 +00:00
|
|
|
span: span,
|
|
|
|
attributes: Vec::new(),
|
|
|
|
path: path!(num::FromPrimitive),
|
|
|
|
additional_bounds: Vec::new(),
|
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
methods: vec!(
|
|
|
|
MethodDef {
|
|
|
|
name: "from_i64",
|
2015-05-18 09:48:54 +00:00
|
|
|
is_unsafe: false,
|
2016-05-18 23:24:33 +00:00
|
|
|
unify_fieldless_variants: false,
|
2015-04-12 18:19:48 +00:00
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: None,
|
|
|
|
args: vec!(Literal(path_local!(i64))),
|
|
|
|
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
|
|
|
|
None,
|
|
|
|
vec!(Box::new(Self_)),
|
|
|
|
true)),
|
|
|
|
// #[inline] liable to cause code-bloat
|
|
|
|
attributes: attrs.clone(),
|
|
|
|
combine_substructure: combine_substructure(Box::new(|c, s, sub| {
|
|
|
|
cs_from("i64", c, s, sub)
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
MethodDef {
|
|
|
|
name: "from_u64",
|
2015-05-18 09:48:54 +00:00
|
|
|
is_unsafe: false,
|
2016-05-18 23:24:33 +00:00
|
|
|
unify_fieldless_variants: false,
|
2015-04-12 18:19:48 +00:00
|
|
|
generics: LifetimeBounds::empty(),
|
|
|
|
explicit_self: None,
|
|
|
|
args: vec!(Literal(path_local!(u64))),
|
|
|
|
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
|
|
|
|
None,
|
|
|
|
vec!(Box::new(Self_)),
|
|
|
|
true)),
|
|
|
|
// #[inline] liable to cause code-bloat
|
|
|
|
attributes: attrs,
|
|
|
|
combine_substructure: combine_substructure(Box::new(|c, s, sub| {
|
|
|
|
cs_from("u64", c, s, sub)
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
associated_types: Vec::new(),
|
2016-09-18 20:24:38 +00:00
|
|
|
supports_unions: false,
|
2015-04-12 18:19:48 +00:00
|
|
|
};
|
|
|
|
|
2015-05-14 17:19:47 +00:00
|
|
|
trait_def.expand(cx, mitem, &item, push)
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
|
|
|
if substr.nonself_args.len() != 1 {
|
|
|
|
cx.span_bug(trait_span, "incorrect number of arguments in `derive(FromPrimitive)`")
|
|
|
|
}
|
|
|
|
|
|
|
|
let n = &substr.nonself_args[0];
|
|
|
|
|
|
|
|
match *substr.fields {
|
|
|
|
StaticStruct(..) => {
|
|
|
|
cx.span_err(trait_span, "`FromPrimitive` cannot be derived for structs");
|
2016-11-28 17:40:50 +00:00
|
|
|
return cx.expr_fail(trait_span, Symbol::intern(""));
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
|
|
|
StaticEnum(enum_def, _) => {
|
|
|
|
if enum_def.variants.is_empty() {
|
|
|
|
cx.span_err(trait_span,
|
|
|
|
"`FromPrimitive` cannot be derived for enums with no variants");
|
2016-11-28 17:40:50 +00:00
|
|
|
return cx.expr_fail(trait_span, Symbol::intern(""));
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut arms = Vec::new();
|
|
|
|
|
|
|
|
for variant in &enum_def.variants {
|
2015-10-27 18:51:35 +00:00
|
|
|
match variant.node.data {
|
2015-10-23 17:49:01 +00:00
|
|
|
ast::VariantData::Unit(..) => {
|
2015-04-12 18:19:48 +00:00
|
|
|
let span = variant.span;
|
|
|
|
|
|
|
|
// expr for `$n == $variant as $name`
|
|
|
|
let path = cx.path(span, vec![substr.type_ident, variant.node.name]);
|
|
|
|
let variant = cx.expr_path(path);
|
|
|
|
let ty = cx.ty_ident(span, cx.ident_of(name));
|
|
|
|
let cast = cx.expr_cast(span, variant.clone(), ty);
|
2016-02-15 21:44:51 +00:00
|
|
|
let guard = cx.expr_binary(span, BinOpKind::Eq, n.clone(), cast);
|
2015-04-12 18:19:48 +00:00
|
|
|
|
|
|
|
// expr for `Some($variant)`
|
|
|
|
let body = cx.expr_some(span, variant);
|
|
|
|
|
|
|
|
// arm for `_ if $guard => $body`
|
|
|
|
let arm = ast::Arm {
|
|
|
|
attrs: vec!(),
|
|
|
|
pats: vec!(cx.pat_wild(span)),
|
|
|
|
guard: Some(guard),
|
|
|
|
body: body,
|
2017-09-19 23:27:02 +00:00
|
|
|
beginning_vert: None,
|
2015-04-12 18:19:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
arms.push(arm);
|
|
|
|
}
|
2015-10-23 17:49:01 +00:00
|
|
|
ast::VariantData::Tuple(..) => {
|
|
|
|
cx.span_err(trait_span,
|
|
|
|
"`FromPrimitive` cannot be derived for \
|
|
|
|
enum variants with arguments");
|
|
|
|
return cx.expr_fail(trait_span,
|
2016-11-28 17:40:50 +00:00
|
|
|
Symbol::intern(""));
|
2015-10-23 17:49:01 +00:00
|
|
|
}
|
|
|
|
ast::VariantData::Struct(..) => {
|
2015-04-12 18:19:48 +00:00
|
|
|
cx.span_err(trait_span,
|
|
|
|
"`FromPrimitive` cannot be derived for enums \
|
|
|
|
with struct variants");
|
|
|
|
return cx.expr_fail(trait_span,
|
2016-11-28 17:40:50 +00:00
|
|
|
Symbol::intern(""));
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// arm for `_ => None`
|
|
|
|
let arm = ast::Arm {
|
|
|
|
attrs: vec!(),
|
|
|
|
pats: vec!(cx.pat_wild(trait_span)),
|
|
|
|
guard: None,
|
|
|
|
body: cx.expr_none(trait_span),
|
2017-09-19 23:27:02 +00:00
|
|
|
beginning_vert: None,
|
2015-04-12 18:19:48 +00:00
|
|
|
};
|
|
|
|
arms.push(arm);
|
|
|
|
|
|
|
|
cx.expr_match(trait_span, n.clone(), arms)
|
|
|
|
}
|
|
|
|
_ => cx.span_bug(trait_span, "expected StaticEnum in derive(FromPrimitive)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[plugin_registrar]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn plugin_registrar(reg: &mut Registry) {
|
|
|
|
reg.register_syntax_extension(
|
2016-11-28 17:40:50 +00:00
|
|
|
Symbol::intern("derive_NumFromPrimitive"),
|
2015-05-14 17:19:47 +00:00
|
|
|
MultiDecorator(Box::new(expand_deriving_from_primitive)));
|
2015-04-12 18:19:48 +00:00
|
|
|
}
|