mirror of
https://github.com/servo/servo.git
synced 2025-07-30 10:40:27 +01:00
Make #[derive(JSTraceable)] a procedural macro
This commit is contained in:
parent
b0cac458b6
commit
391296278d
9 changed files with 98 additions and 68 deletions
|
@ -2,13 +2,10 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use syntax::ast::{Expr, MetaItem, Mutability};
|
||||
use syntax::ast::MetaItem;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ext::base::{Annotatable, ExtCtxt};
|
||||
use syntax::ext::build::AstBuilder;
|
||||
use syntax::ptr::P;
|
||||
use syntax_ext::deriving::generic::{Struct, Substructure, TraitDef, ty};
|
||||
use syntax_ext::deriving::generic::{combine_substructure, EnumMatching, FieldInfo, MethodDef};
|
||||
|
||||
pub fn expand_dom_struct(cx: &mut ExtCtxt, sp: Span, _: &MetaItem, anno: Annotatable) -> Annotatable {
|
||||
if let Annotatable::Item(item) = anno {
|
||||
|
@ -29,63 +26,3 @@ pub fn expand_dom_struct(cx: &mut ExtCtxt, sp: Span, _: &MetaItem, anno: Annotat
|
|||
anno
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the hook to expand `#[derive(JSTraceable)]` into an implementation of `JSTraceable`
|
||||
///
|
||||
/// The expansion basically calls `trace()` on all of the fields of the struct/enum, erroring if they do not
|
||||
/// implement the method.
|
||||
pub fn expand_jstraceable(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Annotatable,
|
||||
push: &mut FnMut(Annotatable)) {
|
||||
let trait_def = TraitDef {
|
||||
is_unsafe: false,
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
path: ty::Path::new(vec!("dom", "bindings", "trace", "JSTraceable")),
|
||||
additional_bounds: Vec::new(),
|
||||
generics: ty::LifetimeBounds::empty(),
|
||||
supports_unions: true,
|
||||
methods: vec![
|
||||
MethodDef {
|
||||
name: "trace",
|
||||
generics: ty::LifetimeBounds::empty(),
|
||||
explicit_self: ty::borrowed_explicit_self(),
|
||||
args: vec!(ty::Ptr(box ty::Literal(ty::Path::new(vec!("js", "jsapi", "JSTracer"))),
|
||||
ty::Raw(Mutability::Mutable))),
|
||||
ret_ty: ty::nil_ty(),
|
||||
attributes: vec![quote_attr!(cx, #[inline])],
|
||||
is_unsafe: false,
|
||||
combine_substructure: combine_substructure(box jstraceable_substructure),
|
||||
unify_fieldless_variants: true,
|
||||
}
|
||||
],
|
||||
associated_types: vec![],
|
||||
};
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
// Mostly copied from syntax::ext::deriving::hash
|
||||
/// Defines how the implementation for `trace()` is to be generated
|
||||
fn jstraceable_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
|
||||
let state_expr = if substr.nonself_args.len() == 1 {
|
||||
&substr.nonself_args[0]
|
||||
} else {
|
||||
cx.span_bug(trait_span, "incorrect number of arguments in `jstraceable`")
|
||||
};
|
||||
let trace_ident = substr.method_ident;
|
||||
let call_trace = |span, thing_expr| {
|
||||
let expr = cx.expr_method_call(span, thing_expr, trace_ident, vec!(state_expr.clone()));
|
||||
cx.stmt_expr(expr)
|
||||
};
|
||||
let mut stmts = Vec::new();
|
||||
|
||||
let fields = match *substr.fields {
|
||||
Struct(_, ref fs) | EnumMatching(_, _, ref fs) => fs,
|
||||
_ => cx.span_bug(trait_span, "impossible substructure in `jstraceable`")
|
||||
};
|
||||
|
||||
for &FieldInfo { ref self_, span, .. } in fields {
|
||||
stmts.push(call_trace(span, self_.clone()));
|
||||
}
|
||||
|
||||
cx.expr_block(cx.block(trait_span, stmts))
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ extern crate rustc;
|
|||
extern crate rustc_plugin;
|
||||
#[macro_use]
|
||||
extern crate syntax;
|
||||
extern crate syntax_ext;
|
||||
|
||||
use rustc_plugin::Registry;
|
||||
use syntax::ext::base::*;
|
||||
|
@ -44,8 +43,6 @@ mod utils;
|
|||
#[plugin_registrar]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
reg.register_syntax_extension(intern("dom_struct"), MultiModifier(box jstraceable::expand_dom_struct));
|
||||
reg.register_syntax_extension(intern("derive_JSTraceable"),
|
||||
MultiDecorator(box jstraceable::expand_jstraceable));
|
||||
reg.register_syntax_extension(intern("_generate_reflector"),
|
||||
MultiDecorator(box reflector::expand_reflector));
|
||||
reg.register_late_lint_pass(box lints::unrooted_must_root::UnrootedPass::new());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue