Port more over to ast-builder

This commit is contained in:
Erick Tryzelaar 2015-03-07 15:08:44 -08:00
parent 124a306cd7
commit 4a83639b33

View File

@ -41,7 +41,6 @@ use syntax::ext::deriving::generic::ty::{
borrowed_explicit_self, borrowed_explicit_self,
}; };
use syntax::parse::token; use syntax::parse::token;
use syntax::owned_slice::OwnedSlice;
use syntax::ptr::P; use syntax::ptr::P;
use rustc::plugin::Registry; use rustc::plugin::Registry;
@ -123,6 +122,9 @@ fn serialize_substructure(cx: &ExtCtxt,
span: Span, span: Span,
substr: &Substructure, substr: &Substructure,
item: &Item) -> P<Expr> { item: &Item) -> P<Expr> {
let ctx = builder::Ctx::new();
let builder = builder::AstBuilder::new(&ctx).span(span);
let visitor = substr.nonself_args[0].clone(); let visitor = substr.nonself_args[0].clone();
match (&item.node, &*substr.fields) { match (&item.node, &*substr.fields) {
@ -139,27 +141,33 @@ fn serialize_substructure(cx: &ExtCtxt,
match (named_fields.is_empty(), unnamed_fields.is_empty()) { match (named_fields.is_empty(), unnamed_fields.is_empty()) {
(true, true) => { (true, true) => {
serialize_unit_struct(cx, serialize_unit_struct(
span, cx,
visitor, builder,
substr.type_ident) visitor,
substr.type_ident,
)
} }
(true, false) => { (true, false) => {
serialize_tuple_struct(cx, serialize_tuple_struct(
span, cx,
visitor, builder,
substr.type_ident, visitor,
&unnamed_fields, substr.type_ident,
generics) &unnamed_fields,
generics,
)
} }
(false, true) => { (false, true) => {
serialize_struct(cx, serialize_struct(
span, cx,
visitor, builder,
substr.type_ident, visitor,
&named_fields, substr.type_ident,
struct_def, &named_fields,
generics) struct_def,
generics,
)
} }
(false, false) => { (false, false) => {
panic!("struct has named and unnamed fields") panic!("struct has named and unnamed fields")
@ -168,39 +176,41 @@ fn serialize_substructure(cx: &ExtCtxt,
} }
(&ast::ItemEnum(_, ref generics), &EnumMatching(_idx, variant, ref fields)) => { (&ast::ItemEnum(_, ref generics), &EnumMatching(_idx, variant, ref fields)) => {
serialize_enum(cx, serialize_enum(
span, cx,
visitor, span,
substr.type_ident, builder,
variant, visitor,
&fields, substr.type_ident,
generics) variant,
&fields,
generics,
)
} }
_ => cx.bug("expected Struct or EnumMatching in derive_serialize") _ => cx.bug("expected Struct or EnumMatching in derive_serialize")
} }
} }
fn serialize_unit_struct(cx: &ExtCtxt, fn serialize_unit_struct(
span: Span, cx: &ExtCtxt,
visitor: P<Expr>, builder: builder::AstBuilder,
type_ident: Ident) -> P<Expr> { visitor: P<Expr>,
let type_name = cx.expr_str( type_ident: Ident
span, ) -> P<Expr> {
token::get_ident(type_ident)); let type_name = builder.expr().str(type_ident);
quote_expr!(cx, $visitor.visit_named_unit($type_name)) quote_expr!(cx, $visitor.visit_named_unit($type_name))
} }
fn serialize_tuple_struct(cx: &ExtCtxt, fn serialize_tuple_struct(
span: Span, cx: &ExtCtxt,
visitor: P<Expr>, builder: builder::AstBuilder,
type_ident: Ident, visitor: P<Expr>,
fields: &[Span], type_ident: Ident,
generics: &ast::Generics) -> P<Expr> { fields: &[Span],
let ctx = builder::Ctx::new(); generics: &ast::Generics
let builder = builder::AstBuilder::new(&ctx).span(span); ) -> P<Expr> {
let type_name = builder.expr().str(type_ident); let type_name = builder.expr().str(type_ident);
let len = fields.len(); let len = fields.len();
@ -270,16 +280,15 @@ fn serialize_tuple_struct(cx: &ExtCtxt,
}) })
} }
fn serialize_struct(cx: &ExtCtxt, fn serialize_struct(
span: Span, cx: &ExtCtxt,
visitor: P<Expr>, builder: builder::AstBuilder,
type_ident: Ident, visitor: P<Expr>,
fields: &[(Ident, Span)], type_ident: Ident,
struct_def: &StructDef, fields: &[(Ident, Span)],
generics: &ast::Generics) -> P<Expr> { struct_def: &StructDef,
let ctx = builder::Ctx::new(); generics: &ast::Generics
let builder = builder::AstBuilder::new(&ctx).span(span); ) -> P<Expr> {
let type_name = builder.expr().str(type_ident); let type_name = builder.expr().str(type_ident);
let len = fields.len(); let len = fields.len();
@ -290,7 +299,7 @@ fn serialize_struct(cx: &ExtCtxt,
let arms: Vec<ast::Arm> = fields.iter() let arms: Vec<ast::Arm> = fields.iter()
.zip(aliases.iter()) .zip(aliases.iter())
.enumerate() .enumerate()
.map(|(i, (&(name, span), alias_lit))| { .map(|(i, (&(name, _), alias_lit))| {
let first = builder.expr().bool(i == 0); let first = builder.expr().bool(i == 0);
let expr = match *alias_lit { let expr = match *alias_lit {
@ -360,15 +369,15 @@ fn serialize_struct(cx: &ExtCtxt,
fn serialize_enum( fn serialize_enum(
cx: &ExtCtxt, cx: &ExtCtxt,
span: Span, span: Span,
builder: builder::AstBuilder,
visitor: P<Expr>, visitor: P<Expr>,
type_ident: Ident, type_ident: Ident,
variant: &ast::Variant, variant: &ast::Variant,
fields: &[FieldInfo], fields: &[FieldInfo],
generics: &ast::Generics, generics: &ast::Generics,
) -> P<Expr> { ) -> P<Expr> {
let type_name = cx.expr_str(span, token::get_ident(type_ident)); let type_name = builder.expr().str(type_ident);
let variant_ident = variant.node.name; let variant_name = builder.expr().str(variant.node.name);
let variant_name = cx.expr_str(span, token::get_ident(variant_ident));
if fields.is_empty() { if fields.is_empty() {
quote_expr!(cx, quote_expr!(cx,
@ -379,20 +388,22 @@ fn serialize_enum(
) )
} else { } else {
serialize_variant( serialize_variant(
cx, cx,
span, span,
visitor, builder,
type_name, visitor,
variant_name, type_name,
generics, variant_name,
variant, generics,
fields) variant,
fields)
} }
} }
fn serialize_variant( fn serialize_variant(
cx: &ExtCtxt, cx: &ExtCtxt,
span: Span, span: Span,
builder: builder::AstBuilder,
visitor: P<ast::Expr>, visitor: P<ast::Expr>,
type_name: P<ast::Expr>, type_name: P<ast::Expr>,
variant_name: P<ast::Expr>, variant_name: P<ast::Expr>,
@ -400,55 +411,13 @@ fn serialize_variant(
variant: &ast::Variant, variant: &ast::Variant,
fields: &[FieldInfo], fields: &[FieldInfo],
) -> P<Expr> { ) -> P<Expr> {
let mut generics = generics.clone(); let generics = builder.from_generics(generics.clone())
.add_lifetime_bound("'__a")
// We need to constrain all the lifetimes to the lifetime of the visitor. .add_ty_param_bound(
let visitor_lifetime = cx.lifetime(span, cx.name_of("'__a")); builder.path().global().ids(&["serde2", "ser", "Serialize"]).build()
)
generics.lifetimes = .lifetime_name("'__a")
Some(ast::LifetimeDef { .build();
lifetime: visitor_lifetime,
bounds: vec![],
}).into_iter().chain(generics.lifetimes.iter()
.map(|lifetime_def| {
let mut bounds = lifetime_def.bounds.clone();
bounds.push(visitor_lifetime.clone());
ast::LifetimeDef {
lifetime: lifetime_def.lifetime.clone(),
bounds: bounds,
}
})
).collect();
let serialize_path = cx.path_global(
span,
vec![
cx.ident_of("serde2"),
cx.ident_of("ser"),
cx.ident_of("Serialize"),
],
);
// Make sure all the type parameters are constrained by the visitor lifetime.
let ty_params = generics.ty_params.iter()
.map(|ty_param| {
let serialize_bound = cx.typarambound(serialize_path.clone());
let visitor_bound = ast::RegionTyParamBound(visitor_lifetime.clone());
let mut bounds = ty_param.bounds.clone().into_vec();
bounds.push(serialize_bound);
bounds.push(visitor_bound);
cx.typaram(
ty_param.span,
ty_param.ident,
OwnedSlice::from_vec(bounds),
None)
})
.collect();
generics.ty_params = OwnedSlice::from_vec(ty_params);
let ( let (
trait_name, trait_name,
@ -479,85 +448,32 @@ fn serialize_variant(
let len = fields.len(); let len = fields.len();
let visitor_field_names: Vec<ast::Ident> = (0 .. len) let visitor_field_names: Vec<ast::Ident> = (0 .. len)
.map(|i| token::str_to_ident(&format!("field{}", i))) .map(|i| builder.id(&format!("field{}", i)))
.collect(); .collect();
let mut visitor_fields = vec![ let visitor_ident = builder.id("__Visitor");
respan(
span,
ast::StructField_ {
kind: ast::NamedField(
cx.ident_of("state"),
ast::Visibility::Inherited,
),
id: ast::DUMMY_NODE_ID,
ty: cx.ty_ident(span, cx.ident_of("u32")),
attrs: Vec::new(),
}
),
];
visitor_fields.extend( let mut struct_builder = builder.item().struct_(visitor_ident)
visitor_field_names.iter() .with_generics(generics.clone())
.zip(tys.iter()) .field("state").u32();
.map(|(name, ty)| {
respan(
span,
ast::StructField_ {
kind: ast::NamedField(
*name,
ast::Visibility::Inherited,
),
id: ast::DUMMY_NODE_ID,
ty: cx.ty_rptr(
span,
ty.clone(),
Some(cx.lifetime(span, cx.name_of("'__a"))),
ast::MutImmutable,
),
attrs: Vec::new(),
}
)
})
);
let visitor_ident = cx.ident_of("__Visitor"); for (name, ty) in visitor_field_names.iter().zip(tys.iter()) {
struct_builder = struct_builder.field(name)
.ref_().lifetime("'__a").build_ty(ty.clone());
}
let visitor_struct = cx.item_struct_poly( let visitor_struct = struct_builder.build();
span,
visitor_ident,
ast::StructDef {
fields: visitor_fields,
ctor_id: None,
},
generics.clone(),
);
let mut visitor_field_exprs = vec![ let mut struct_builder = builder.expr().struct_path(visitor_ident)
cx.field_imm( .field("state").u32(0);
span,
cx.ident_of("state"),
quote_expr!(cx, 0),
),
];
visitor_field_exprs.extend( for (name, field) in visitor_field_names.iter().zip(fields.iter()) {
visitor_field_names.iter() struct_builder = struct_builder.field(name)
.zip(fields.iter()) .addr_of()
.map(|(name, field)| { .build_expr(field.self_.clone());
let e = cx.expr_addr_of( }
span,
field.self_.clone(),
);
cx.field_imm(span, *name, e) let visitor_expr = struct_builder.build();
})
);
let visitor_expr = cx.expr_struct(
span,
cx.path_ident(span, visitor_ident),
visitor_field_exprs);
let mut first = true; let mut first = true;
@ -607,32 +523,18 @@ fn serialize_variant(
}) })
.collect(); .collect();
let trait_path = cx.path_global( let trait_path = builder.path()
span, .global()
vec![ .ids(&["serde2", "ser"]).id(trait_name)
cx.ident_of("serde2"), .build();
cx.ident_of("ser"),
trait_name,
],
);
let trait_ref = cx.trait_ref(trait_path); let trait_ref = cx.trait_ref(trait_path);
let opt_trait_ref = Some(trait_ref); let opt_trait_ref = Some(trait_ref);
let self_ty = cx.ty_path( let self_ty = builder.ty()
cx.path_all( .path()
span, .segment("__Visitor").with_generics(generics.clone()).build()
false, .build();
vec![cx.ident_of("__Visitor")],
generics.lifetimes.iter()
.map(|lifetime| lifetime.lifetime)
.collect(),
generics.ty_params.iter()
.map(|ty_param| cx.ty_ident(span, ty_param.ident))
.collect(),
vec![],
),
);
let impl_ident = ast_util::impl_pretty_name(&opt_trait_ref, Some(&self_ty)); let impl_ident = ast_util::impl_pretty_name(&opt_trait_ref, Some(&self_ty));