From 6e2ae2c2c131bfbf2d8ab1b8ff9874e23abb7003 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 17 Jan 2013 22:21:07 -0800 Subject: remove support for records from auto_encode --- src/libsyntax/ext/auto_encode.rs | 257 ++++++++++++--------------------------- 1 file changed, 75 insertions(+), 182 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/auto_encode.rs b/src/libsyntax/ext/auto_encode.rs index 470b6e540ed..77f23031135 100644 --- a/src/libsyntax/ext/auto_encode.rs +++ b/src/libsyntax/ext/auto_encode.rs @@ -130,20 +130,6 @@ fn expand_auto_encode( do vec::flat_map(in_items) |item| { if item.attrs.any(is_auto_encode) { match item.node { - ast::item_ty( - @ast::Ty {node: ast::ty_rec(ref fields), _}, - tps - ) => { - let ser_impl = mk_rec_ser_impl( - cx, - item.span, - item.ident, - (*fields), - tps - ); - - ~[filter_attrs(*item), ser_impl] - }, ast::item_struct(@ast::struct_def { fields, _}, tps) => { let ser_impl = mk_struct_ser_impl( cx, @@ -199,20 +185,6 @@ fn expand_auto_decode( do vec::flat_map(in_items) |item| { if item.attrs.any(is_auto_decode) { match item.node { - ast::item_ty( - @ast::Ty {node: ast::ty_rec(ref fields), _}, - tps - ) => { - let deser_impl = mk_rec_deser_impl( - cx, - item.span, - item.ident, - (*fields), - tps - ); - - ~[filter_attrs(*item), deser_impl] - }, ast::item_struct(@ast::struct_def { fields, _}, tps) => { let deser_impl = mk_struct_deser_impl( cx, @@ -693,67 +665,48 @@ fn mk_deser_method( } } -fn mk_rec_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[ast::ty_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `__s.emit_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_rec") - ), - ~[cx.lambda_stmts(span, fields)] - ); - - mk_ser_impl(cx, span, ident, tps, body) -} - -fn mk_rec_deser_impl( +fn mk_struct_ser_impl( cx: ext_ctxt, span: span, ident: ast::ident, - fields: ~[ast::ty_field], + fields: ~[@ast::struct_field], tps: ~[ast::ty_param] ) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_rec_fields(fields)); - - // ast for `read_rec(|| $(fields))` - let body = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_rec") - ), - ~[ - cx.lambda_expr( - cx.expr( + let fields = do mk_struct_fields(fields).mapi |idx, field| { + // ast for `|| self.$(name).encode(__s)` + let expr_lambda = cx.lambda_expr( + cx.expr_call( + span, + cx.expr_field( span, - ast::expr_rec(fields, None) - ) + cx.expr_field( + span, + cx.expr_var(span, ~"self"), + field.ident + ), + cx.ident_of(~"encode") + ), + ~[cx.expr_var(span, ~"__s")] ) - ] - ); - - mk_deser_impl(cx, span, ident, tps, body) -} + ); -fn mk_struct_ser_impl( - cx: ext_ctxt, - span: span, - ident: ast::ident, - fields: ~[@ast::struct_field], - tps: ~[ast::ty_param] -) -> @ast::item { - let fields = mk_ser_fields(cx, span, mk_struct_fields(fields)); + // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` + cx.stmt( + cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__s"), + cx.ident_of(~"emit_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ) + ) + }; // ast for `__s.emit_struct($(name), || $(fields))` let ser_body = cx.expr_call( @@ -780,7 +733,47 @@ fn mk_struct_deser_impl( fields: ~[@ast::struct_field], tps: ~[ast::ty_param] ) -> @ast::item { - let fields = mk_deser_fields(cx, span, mk_struct_fields(fields)); + let fields = do mk_struct_fields(fields).mapi |idx, field| { + // ast for `|| std::serialize::decode(__d)` + let expr_lambda = cx.lambda( + cx.expr_blk( + cx.expr_call( + span, + cx.expr_path_global(span, ~[ + cx.ident_of(~"std"), + cx.ident_of(~"serialize"), + cx.ident_of(~"Decodable"), + cx.ident_of(~"decode"), + ]), + ~[cx.expr_var(span, ~"__d")] + ) + ) + ); + + // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` + let expr: @ast::expr = cx.expr_call( + span, + cx.expr_field( + span, + cx.expr_var(span, ~"__d"), + cx.ident_of(~"read_field") + ), + ~[ + cx.lit_str(span, @cx.str_of(field.ident)), + cx.lit_uint(span, idx), + expr_lambda, + ] + ); + + ast::spanned { + node: ast::field_ { + mutbl: field.mutbl, + ident: field.ident, + expr: expr, + }, + span: span, + } + }; // ast for `read_struct($(name), || $(fields))` let body = cx.expr_call( @@ -818,16 +811,6 @@ struct field { mutbl: ast::mutability, } -fn mk_rec_fields(fields: ~[ast::ty_field]) -> ~[field] { - do fields.map |field| { - field { - span: field.span, - ident: field.node.ident, - mutbl: field.node.mt.mutbl, - } - } -} - fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { do fields.map |field| { let (ident, mutbl) = match field.node.kind { @@ -847,96 +830,6 @@ fn mk_struct_fields(fields: ~[@ast::struct_field]) -> ~[field] { } } -fn mk_ser_fields( - cx: ext_ctxt, - span: span, - fields: ~[field] -) -> ~[@ast::stmt] { - do fields.mapi |idx, field| { - // ast for `|| self.$(name).encode(__s)` - let expr_lambda = cx.lambda_expr( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"self"), - field.ident - ), - cx.ident_of(~"encode") - ), - ~[cx.expr_var(span, ~"__s")] - ) - ); - - // ast for `__s.emit_field($(name), $(idx), $(expr_lambda))` - cx.stmt( - cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__s"), - cx.ident_of(~"emit_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ) - ) - } -} - -fn mk_deser_fields( - cx: ext_ctxt, - span: span, - fields: ~[field] -) -> ~[ast::field] { - do fields.mapi |idx, field| { - // ast for `|| std::serialize::decode(__d)` - let expr_lambda = cx.lambda( - cx.expr_blk( - cx.expr_call( - span, - cx.expr_path_global(span, ~[ - cx.ident_of(~"std"), - cx.ident_of(~"serialize"), - cx.ident_of(~"Decodable"), - cx.ident_of(~"decode"), - ]), - ~[cx.expr_var(span, ~"__d")] - ) - ) - ); - - // ast for `__d.read_field($(name), $(idx), $(expr_lambda))` - let expr: @ast::expr = cx.expr_call( - span, - cx.expr_field( - span, - cx.expr_var(span, ~"__d"), - cx.ident_of(~"read_field") - ), - ~[ - cx.lit_str(span, @cx.str_of(field.ident)), - cx.lit_uint(span, idx), - expr_lambda, - ] - ); - - ast::spanned { - node: ast::field_ { - mutbl: field.mutbl, - ident: field.ident, - expr: expr, - }, - span: span, - } - } -} - fn mk_enum_ser_impl( cx: ext_ctxt, span: span, -- cgit 1.4.1-3-g733a5