2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-11-19 20:05:50 -06:00
|
|
|
/// The compiler code necessary to implement the #[deriving_eq] and
|
2012-11-20 20:27:13 -06:00
|
|
|
/// #[deriving_iter_bytes] extensions.
|
2012-11-19 20:05:50 -06:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-01-13 12:48:09 -06:00
|
|
|
use ast;
|
2013-01-10 13:16:54 -06:00
|
|
|
use ast::{TraitTyParamBound, Ty, and, bind_by_ref, binop, deref, enum_def};
|
|
|
|
use ast::{enum_variant_kind, expr, expr_match, ident, item, item_};
|
|
|
|
use ast::{item_enum, item_impl, item_struct, m_imm, meta_item, method};
|
|
|
|
use ast::{named_field, or, pat, pat_ident, pat_wild, public, pure_fn};
|
|
|
|
use ast::{re_anon, spanned, stmt, struct_def, struct_variant_kind};
|
|
|
|
use ast::{sty_by_ref, sty_region, tuple_variant_kind, ty_nil, ty_param};
|
|
|
|
use ast::{ty_param_bound, ty_path, ty_rptr, unnamed_field, variant};
|
2012-12-13 15:05:22 -06:00
|
|
|
use ext::base::ext_ctxt;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ext::build;
|
2012-11-19 20:05:50 -06:00
|
|
|
use codemap::span;
|
|
|
|
use parse::token::special_idents::clownshoes_extensions;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::dvec;
|
|
|
|
use core::uint;
|
|
|
|
|
2012-11-19 20:05:50 -06:00
|
|
|
enum Junction {
|
|
|
|
Conjunction,
|
|
|
|
Disjunction,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Junction {
|
|
|
|
fn to_binop(self) -> binop {
|
|
|
|
match self {
|
|
|
|
Conjunction => and,
|
|
|
|
Disjunction => or,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
type ExpandDerivingStructDefFn = &fn(ext_ctxt,
|
|
|
|
span,
|
|
|
|
x: &struct_def,
|
2012-11-20 21:20:55 -06:00
|
|
|
ident,
|
|
|
|
+y: ~[ty_param])
|
2012-11-20 20:27:13 -06:00
|
|
|
-> @item;
|
2012-11-20 21:20:55 -06:00
|
|
|
type ExpandDerivingEnumDefFn = &fn(ext_ctxt,
|
|
|
|
span,
|
|
|
|
x: &enum_def,
|
|
|
|
ident,
|
|
|
|
+y: ~[ty_param])
|
|
|
|
-> @item;
|
2012-11-20 20:27:13 -06:00
|
|
|
|
2012-11-19 20:05:50 -06:00
|
|
|
pub fn expand_deriving_eq(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
_mitem: meta_item,
|
|
|
|
in_items: ~[@item])
|
|
|
|
-> ~[@item] {
|
2012-11-20 20:27:13 -06:00
|
|
|
expand_deriving(cx,
|
|
|
|
span,
|
|
|
|
in_items,
|
|
|
|
expand_deriving_eq_struct_def,
|
|
|
|
expand_deriving_eq_enum_def)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand_deriving_iter_bytes(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
_mitem: meta_item,
|
|
|
|
in_items: ~[@item])
|
|
|
|
-> ~[@item] {
|
|
|
|
expand_deriving(cx,
|
|
|
|
span,
|
|
|
|
in_items,
|
|
|
|
expand_deriving_iter_bytes_struct_def,
|
|
|
|
expand_deriving_iter_bytes_enum_def)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
in_items: ~[@item],
|
|
|
|
expand_deriving_struct_def: ExpandDerivingStructDefFn,
|
|
|
|
expand_deriving_enum_def: ExpandDerivingEnumDefFn)
|
|
|
|
-> ~[@item] {
|
2012-11-19 20:05:50 -06:00
|
|
|
let result = dvec::DVec();
|
|
|
|
for in_items.each |item| {
|
|
|
|
result.push(copy *item);
|
|
|
|
match item.node {
|
2012-12-10 15:47:54 -06:00
|
|
|
item_struct(struct_def, copy ty_params) => {
|
2012-11-19 20:05:50 -06:00
|
|
|
result.push(expand_deriving_struct_def(cx,
|
|
|
|
span,
|
|
|
|
struct_def,
|
2012-11-20 21:20:55 -06:00
|
|
|
item.ident,
|
|
|
|
move ty_params));
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
2012-11-20 21:20:55 -06:00
|
|
|
item_enum(ref enum_definition, copy ty_params) => {
|
2012-11-19 20:05:50 -06:00
|
|
|
result.push(expand_deriving_enum_def(cx,
|
|
|
|
span,
|
|
|
|
enum_definition,
|
2012-11-20 21:20:55 -06:00
|
|
|
item.ident,
|
|
|
|
move ty_params));
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
2012-11-20 15:01:02 -06:00
|
|
|
_ => ()
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dvec::unwrap(move result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_impl_item(cx: ext_ctxt, span: span, +item: item_) -> @item {
|
2013-01-13 15:13:41 -06:00
|
|
|
@ast::item {
|
2012-11-19 20:05:50 -06:00
|
|
|
ident: clownshoes_extensions,
|
|
|
|
attrs: ~[],
|
|
|
|
id: cx.next_id(),
|
|
|
|
node: move item,
|
2012-11-20 21:20:55 -06:00
|
|
|
vis: public,
|
2012-11-19 20:05:50 -06:00
|
|
|
span: span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a method from the given expression, the signature of which
|
|
|
|
/// conforms to the `eq` or `ne` method.
|
2012-11-20 20:27:13 -06:00
|
|
|
fn create_eq_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
method_ident: ident,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params: &[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
body: @expr)
|
|
|
|
-> @method {
|
2012-11-19 20:05:50 -06:00
|
|
|
// Create the type of the `other` parameter.
|
2012-11-20 21:20:55 -06:00
|
|
|
let arg_path_type = create_self_type_with_params(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
ty_params);
|
2013-01-15 18:05:20 -06:00
|
|
|
let arg_region = @ast::region { id: cx.next_id(), node: re_anon };
|
2013-01-14 23:36:27 -06:00
|
|
|
let arg_type = ty_rptr(
|
|
|
|
arg_region,
|
|
|
|
ast::mt { ty: arg_path_type, mutbl: m_imm }
|
|
|
|
);
|
2013-01-15 16:59:39 -06:00
|
|
|
let arg_type = @ast::Ty {
|
|
|
|
id: cx.next_id(),
|
|
|
|
node: arg_type,
|
|
|
|
span: span,
|
|
|
|
};
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the `other` parameter.
|
|
|
|
let other_ident = cx.ident_of(~"__other");
|
2012-11-20 20:27:13 -06:00
|
|
|
let arg = build::mk_arg(cx, span, other_ident, arg_type);
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the type of the return value.
|
|
|
|
let bool_ident = cx.ident_of(~"bool");
|
|
|
|
let output_type = build::mk_raw_path(span, ~[ bool_ident ]);
|
|
|
|
let output_type = ty_path(output_type, cx.next_id());
|
2013-01-15 16:59:39 -06:00
|
|
|
let output_type = @ast::Ty {
|
2012-11-19 20:05:50 -06:00
|
|
|
id: cx.next_id(),
|
2013-01-15 16:59:39 -06:00
|
|
|
node: output_type,
|
|
|
|
span: span,
|
2012-11-19 20:05:50 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create the function declaration.
|
2012-11-20 20:27:13 -06:00
|
|
|
let fn_decl = build::mk_fn_decl(~[ move arg ], output_type);
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the body block.
|
|
|
|
let body_block = build::mk_simple_block(cx, span, body);
|
|
|
|
|
|
|
|
// Create the method.
|
2012-12-27 13:36:00 -06:00
|
|
|
let self_ty = spanned { node: sty_region(m_imm), span: span };
|
2013-01-15 18:05:20 -06:00
|
|
|
@ast::method {
|
2012-11-19 20:05:50 -06:00
|
|
|
ident: method_ident,
|
|
|
|
attrs: ~[],
|
|
|
|
tps: ~[],
|
|
|
|
self_ty: self_ty,
|
|
|
|
purity: pure_fn,
|
|
|
|
decl: move fn_decl,
|
|
|
|
body: move body_block,
|
|
|
|
id: cx.next_id(),
|
|
|
|
span: span,
|
|
|
|
self_id: cx.next_id(),
|
|
|
|
vis: public
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
|
2012-11-20 21:20:55 -06:00
|
|
|
fn create_self_type_with_params(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
|
|
|
ty_params: &[ty_param])
|
|
|
|
-> @Ty {
|
|
|
|
// Create the type parameters on the `self` path.
|
|
|
|
let self_ty_params = dvec::DVec();
|
|
|
|
for ty_params.each |ty_param| {
|
|
|
|
let self_ty_param = build::mk_simple_ty_path(cx,
|
|
|
|
span,
|
|
|
|
ty_param.ident);
|
|
|
|
self_ty_params.push(move self_ty_param);
|
|
|
|
}
|
|
|
|
let self_ty_params = dvec::unwrap(move self_ty_params);
|
|
|
|
|
|
|
|
// Create the type of `self`.
|
|
|
|
let self_type = build::mk_raw_path_(span,
|
|
|
|
~[ type_ident ],
|
|
|
|
move self_ty_params);
|
|
|
|
let self_type = ty_path(self_type, cx.next_id());
|
2013-01-15 16:59:39 -06:00
|
|
|
@ast::Ty { id: cx.next_id(), node: self_type, span: span }
|
2012-11-20 21:20:55 -06:00
|
|
|
}
|
|
|
|
|
2012-11-19 20:05:50 -06:00
|
|
|
fn create_derived_impl(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
+ty_params: ~[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
methods: &[@method],
|
2012-11-20 21:20:55 -06:00
|
|
|
trait_path: &[ident])
|
2012-11-19 20:05:50 -06:00
|
|
|
-> @item {
|
2012-11-20 21:20:55 -06:00
|
|
|
// Create the type parameters.
|
|
|
|
let impl_ty_params = dvec::DVec();
|
|
|
|
for ty_params.each |ty_param| {
|
2012-12-23 16:41:37 -06:00
|
|
|
let bound = build::mk_ty_path_global(cx,
|
|
|
|
span,
|
|
|
|
trait_path.map(|x| *x));
|
2013-01-10 13:16:54 -06:00
|
|
|
let bounds = @~[ TraitTyParamBound(bound) ];
|
2012-11-20 21:20:55 -06:00
|
|
|
let impl_ty_param = build::mk_ty_param(cx, ty_param.ident, bounds);
|
|
|
|
impl_ty_params.push(move impl_ty_param);
|
|
|
|
}
|
|
|
|
let impl_ty_params = dvec::unwrap(move impl_ty_params);
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
// Create the reference to the trait.
|
2013-01-13 12:48:09 -06:00
|
|
|
let trait_path = ast::path {
|
2012-11-19 20:05:50 -06:00
|
|
|
span: span,
|
2012-12-23 16:41:37 -06:00
|
|
|
global: true,
|
2012-11-20 20:27:13 -06:00
|
|
|
idents: trait_path.map(|x| *x),
|
2012-11-19 20:05:50 -06:00
|
|
|
rp: None,
|
|
|
|
types: ~[]
|
|
|
|
};
|
2012-11-20 20:27:13 -06:00
|
|
|
let trait_path = @move trait_path;
|
2013-01-15 18:05:20 -06:00
|
|
|
let trait_ref = ast::trait_ref {
|
2012-11-20 20:27:13 -06:00
|
|
|
path: trait_path,
|
2012-12-06 21:12:25 -06:00
|
|
|
ref_id: cx.next_id()
|
2012-11-19 20:05:50 -06:00
|
|
|
};
|
|
|
|
let trait_ref = @move trait_ref;
|
|
|
|
|
|
|
|
// Create the type of `self`.
|
2012-11-20 21:20:55 -06:00
|
|
|
let self_type = create_self_type_with_params(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
ty_params);
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the impl item.
|
2012-11-20 21:20:55 -06:00
|
|
|
let impl_item = item_impl(move impl_ty_params,
|
2012-11-19 20:05:50 -06:00
|
|
|
Some(trait_ref),
|
|
|
|
self_type,
|
2012-11-20 20:27:13 -06:00
|
|
|
methods.map(|x| *x));
|
2012-11-19 20:05:50 -06:00
|
|
|
return create_impl_item(cx, span, move impl_item);
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn create_derived_eq_impl(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
+ty_params: ~[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
eq_method: @method,
|
|
|
|
ne_method: @method)
|
|
|
|
-> @item {
|
|
|
|
let methods = [ eq_method, ne_method ];
|
|
|
|
let trait_path = [
|
|
|
|
cx.ident_of(~"core"),
|
|
|
|
cx.ident_of(~"cmp"),
|
|
|
|
cx.ident_of(~"Eq")
|
|
|
|
];
|
2012-11-20 21:20:55 -06:00
|
|
|
create_derived_impl(cx, span, type_ident, ty_params, methods, trait_path)
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_derived_iter_bytes_impl(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
+ty_params: ~[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
method: @method)
|
|
|
|
-> @item {
|
2012-11-20 21:20:55 -06:00
|
|
|
let methods = [ method ];
|
2012-11-20 20:27:13 -06:00
|
|
|
let trait_path = [
|
|
|
|
cx.ident_of(~"core"),
|
|
|
|
cx.ident_of(~"to_bytes"),
|
|
|
|
cx.ident_of(~"IterBytes")
|
|
|
|
];
|
2012-11-20 21:20:55 -06:00
|
|
|
create_derived_impl(cx, span, type_ident, ty_params, methods, trait_path)
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a method from the given set of statements conforming to the
|
|
|
|
// signature of the `iter_bytes` method.
|
|
|
|
fn create_iter_bytes_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
+statements: ~[@stmt])
|
|
|
|
-> @method {
|
|
|
|
// Create the `lsb0` parameter.
|
|
|
|
let bool_ident = cx.ident_of(~"bool");
|
|
|
|
let lsb0_arg_type = build::mk_simple_ty_path(cx, span, bool_ident);
|
|
|
|
let lsb0_ident = cx.ident_of(~"__lsb0");
|
|
|
|
let lsb0_arg = build::mk_arg(cx, span, lsb0_ident, lsb0_arg_type);
|
|
|
|
|
|
|
|
// Create the `f` parameter.
|
|
|
|
let core_ident = cx.ident_of(~"core");
|
|
|
|
let to_bytes_ident = cx.ident_of(~"to_bytes");
|
|
|
|
let cb_ident = cx.ident_of(~"Cb");
|
|
|
|
let core_to_bytes_cb_ident = ~[ core_ident, to_bytes_ident, cb_ident ];
|
|
|
|
let f_arg_type = build::mk_ty_path(cx, span, core_to_bytes_cb_ident);
|
|
|
|
let f_ident = cx.ident_of(~"__f");
|
|
|
|
let f_arg = build::mk_arg(cx, span, f_ident, f_arg_type);
|
|
|
|
|
|
|
|
// Create the type of the return value.
|
2013-01-15 16:59:39 -06:00
|
|
|
let output_type = @ast::Ty { id: cx.next_id(), node: ty_nil, span: span };
|
2012-11-20 20:27:13 -06:00
|
|
|
|
|
|
|
// Create the function declaration.
|
|
|
|
let inputs = ~[ move lsb0_arg, move f_arg ];
|
|
|
|
let fn_decl = build::mk_fn_decl(move inputs, output_type);
|
|
|
|
|
|
|
|
// Create the body block.
|
|
|
|
let body_block = build::mk_block_(cx, span, move statements);
|
|
|
|
|
|
|
|
// Create the method.
|
2012-12-27 13:36:00 -06:00
|
|
|
let self_ty = spanned { node: sty_region(m_imm), span: span };
|
2012-11-20 20:27:13 -06:00
|
|
|
let method_ident = cx.ident_of(~"iter_bytes");
|
2013-01-15 18:05:20 -06:00
|
|
|
@ast::method {
|
2012-11-20 20:27:13 -06:00
|
|
|
ident: method_ident,
|
|
|
|
attrs: ~[],
|
|
|
|
tps: ~[],
|
|
|
|
self_ty: self_ty,
|
|
|
|
purity: pure_fn,
|
|
|
|
decl: move fn_decl,
|
|
|
|
body: move body_block,
|
|
|
|
id: cx.next_id(),
|
|
|
|
span: span,
|
|
|
|
self_id: cx.next_id(),
|
|
|
|
vis: public
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-10 15:04:04 -06:00
|
|
|
fn create_subpatterns(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
prefix: ~str,
|
|
|
|
n: uint)
|
|
|
|
-> ~[@pat] {
|
|
|
|
let subpats = dvec::DVec();
|
|
|
|
for uint::range(0, n) |_i| {
|
|
|
|
// Create the subidentifier.
|
|
|
|
let index = subpats.len().to_str();
|
|
|
|
let ident = cx.ident_of(prefix + index);
|
|
|
|
|
|
|
|
// Create the subpattern.
|
|
|
|
let subpath = build::mk_raw_path(span, ~[ ident ]);
|
|
|
|
let subpat = pat_ident(bind_by_ref(m_imm), subpath, None);
|
|
|
|
let subpat = build::mk_pat(cx, span, move subpat);
|
|
|
|
subpats.push(subpat);
|
|
|
|
}
|
|
|
|
return dvec::unwrap(move subpats);
|
|
|
|
}
|
|
|
|
|
2012-11-20 14:59:37 -06:00
|
|
|
fn create_enum_variant_pattern(cx: ext_ctxt,
|
|
|
|
span: span,
|
2012-11-20 21:20:55 -06:00
|
|
|
variant: &variant,
|
2012-11-20 14:59:37 -06:00
|
|
|
prefix: ~str)
|
2012-11-20 21:20:55 -06:00
|
|
|
-> @pat {
|
2012-11-20 14:59:37 -06:00
|
|
|
let variant_ident = variant.node.name;
|
|
|
|
match variant.node.kind {
|
|
|
|
tuple_variant_kind(ref variant_args) => {
|
|
|
|
if variant_args.len() == 0 {
|
|
|
|
return build::mk_pat_ident(cx, span, variant_ident);
|
|
|
|
}
|
|
|
|
|
|
|
|
let matching_path = build::mk_raw_path(span, ~[ variant_ident ]);
|
2012-12-10 15:04:04 -06:00
|
|
|
let subpats = create_subpatterns(cx,
|
|
|
|
span,
|
|
|
|
prefix,
|
|
|
|
variant_args.len());
|
|
|
|
|
2012-11-20 14:59:37 -06:00
|
|
|
return build::mk_pat_enum(cx, span, matching_path, move subpats);
|
|
|
|
}
|
2012-12-10 15:04:04 -06:00
|
|
|
struct_variant_kind(struct_def) => {
|
|
|
|
let matching_path = build::mk_raw_path(span, ~[ variant_ident ]);
|
|
|
|
let subpats = create_subpatterns(cx,
|
|
|
|
span,
|
|
|
|
prefix,
|
|
|
|
struct_def.fields.len());
|
|
|
|
|
2013-01-14 22:52:28 -06:00
|
|
|
let field_pats = do struct_def.fields.mapi |i, struct_field| {
|
2012-12-10 15:04:04 -06:00
|
|
|
let ident = match struct_field.node.kind {
|
|
|
|
named_field(ident, _, _) => ident,
|
|
|
|
unnamed_field => {
|
|
|
|
cx.span_bug(span, ~"unexpected unnamed field");
|
|
|
|
}
|
|
|
|
};
|
2013-01-14 22:52:28 -06:00
|
|
|
ast::field_pat { ident: ident, pat: subpats[i] }
|
|
|
|
};
|
2012-12-10 15:04:04 -06:00
|
|
|
|
2013-01-14 22:52:28 -06:00
|
|
|
build::mk_pat_struct(cx, span, matching_path, field_pats)
|
2012-11-20 14:59:37 -06:00
|
|
|
}
|
|
|
|
enum_variant_kind(*) => {
|
|
|
|
cx.span_unimpl(span, ~"enum variants for `deriving`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn call_substructure_eq_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
self_field: @expr,
|
|
|
|
other_field_ref: @expr,
|
|
|
|
method_ident: ident,
|
|
|
|
junction: Junction,
|
|
|
|
chain_expr: &mut Option<@expr>) {
|
2012-11-20 14:59:37 -06:00
|
|
|
// Call the substructure method.
|
|
|
|
let self_method = build::mk_access_(cx, span, self_field, method_ident);
|
|
|
|
let self_call = build::mk_call_(cx,
|
|
|
|
span,
|
|
|
|
self_method,
|
|
|
|
~[ other_field_ref ]);
|
|
|
|
|
|
|
|
// Connect to the outer expression if necessary.
|
|
|
|
*chain_expr = match *chain_expr {
|
|
|
|
None => Some(self_call),
|
|
|
|
Some(copy old_outer_expr) => {
|
|
|
|
let binop = junction.to_binop();
|
|
|
|
let chain_expr = build::mk_binary(cx,
|
|
|
|
span,
|
|
|
|
binop,
|
|
|
|
old_outer_expr,
|
|
|
|
self_call);
|
|
|
|
Some(chain_expr)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn finish_eq_chain_expr(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
chain_expr: Option<@expr>,
|
|
|
|
junction: Junction)
|
|
|
|
-> @expr {
|
2012-11-20 14:59:37 -06:00
|
|
|
match chain_expr {
|
|
|
|
None => {
|
|
|
|
match junction {
|
|
|
|
Conjunction => build::mk_bool(cx, span, true),
|
|
|
|
Disjunction => build::mk_bool(cx, span, false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ref outer_expr) => *outer_expr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn call_substructure_iter_bytes_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
self_field: @expr)
|
|
|
|
-> @stmt {
|
|
|
|
// Gather up the parameters we want to chain along.
|
|
|
|
let lsb0_ident = cx.ident_of(~"__lsb0");
|
|
|
|
let f_ident = cx.ident_of(~"__f");
|
|
|
|
let lsb0_expr = build::mk_path(cx, span, ~[ lsb0_ident ]);
|
|
|
|
let f_expr = build::mk_path(cx, span, ~[ f_ident ]);
|
|
|
|
|
|
|
|
// Call the substructure method.
|
|
|
|
let iter_bytes_ident = cx.ident_of(~"iter_bytes");
|
|
|
|
let self_method = build::mk_access_(cx,
|
|
|
|
span,
|
|
|
|
self_field,
|
|
|
|
iter_bytes_ident);
|
|
|
|
let self_call = build::mk_call_(cx,
|
|
|
|
span,
|
|
|
|
self_method,
|
|
|
|
~[ lsb0_expr, f_expr ]);
|
|
|
|
|
|
|
|
// Create a statement out of this expression.
|
|
|
|
build::mk_stmt(cx, span, self_call)
|
|
|
|
}
|
|
|
|
|
2012-11-20 14:59:37 -06:00
|
|
|
fn variant_arg_count(cx: ext_ctxt, span: span, variant: &variant) -> uint {
|
|
|
|
match variant.node.kind {
|
|
|
|
tuple_variant_kind(args) => args.len(),
|
|
|
|
struct_variant_kind(struct_def) => struct_def.fields.len(),
|
|
|
|
enum_variant_kind(*) => {
|
|
|
|
cx.span_bug(span, ~"variant_arg_count: enum variants deprecated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn expand_deriving_eq_struct_def(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
struct_def: &struct_def,
|
2012-11-20 21:20:55 -06:00
|
|
|
type_ident: ident,
|
|
|
|
+ty_params: ~[ty_param])
|
2012-11-20 20:27:13 -06:00
|
|
|
-> @item {
|
2012-11-19 20:05:50 -06:00
|
|
|
// Create the methods.
|
|
|
|
let eq_ident = cx.ident_of(~"eq");
|
|
|
|
let ne_ident = cx.ident_of(~"ne");
|
2012-11-20 20:27:13 -06:00
|
|
|
let eq_method = expand_deriving_eq_struct_method(cx,
|
|
|
|
span,
|
|
|
|
struct_def,
|
|
|
|
eq_ident,
|
|
|
|
type_ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params,
|
2012-11-20 20:27:13 -06:00
|
|
|
Conjunction);
|
|
|
|
let ne_method = expand_deriving_eq_struct_method(cx,
|
|
|
|
span,
|
|
|
|
struct_def,
|
|
|
|
ne_ident,
|
|
|
|
type_ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params,
|
2012-11-20 20:27:13 -06:00
|
|
|
Disjunction);
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the implementation.
|
2012-11-20 21:20:55 -06:00
|
|
|
return create_derived_eq_impl(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
move ty_params,
|
|
|
|
eq_method,
|
|
|
|
ne_method);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving_eq_enum_def(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
enum_definition: &enum_def,
|
|
|
|
type_ident: ident,
|
|
|
|
+ty_params: ~[ty_param])
|
|
|
|
-> @item {
|
|
|
|
// Create the methods.
|
|
|
|
let eq_ident = cx.ident_of(~"eq");
|
|
|
|
let ne_ident = cx.ident_of(~"ne");
|
|
|
|
let eq_method = expand_deriving_eq_enum_method(cx,
|
|
|
|
span,
|
|
|
|
enum_definition,
|
|
|
|
eq_ident,
|
|
|
|
type_ident,
|
|
|
|
ty_params,
|
|
|
|
Conjunction);
|
|
|
|
let ne_method = expand_deriving_eq_enum_method(cx,
|
|
|
|
span,
|
|
|
|
enum_definition,
|
|
|
|
ne_ident,
|
|
|
|
type_ident,
|
|
|
|
ty_params,
|
|
|
|
Disjunction);
|
|
|
|
|
|
|
|
// Create the implementation.
|
|
|
|
return create_derived_eq_impl(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
move ty_params,
|
|
|
|
eq_method,
|
|
|
|
ne_method);
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn expand_deriving_iter_bytes_struct_def(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
struct_def: &struct_def,
|
2012-11-20 21:20:55 -06:00
|
|
|
type_ident: ident,
|
|
|
|
+ty_params: ~[ty_param])
|
2012-11-20 20:27:13 -06:00
|
|
|
-> @item {
|
|
|
|
// Create the method.
|
|
|
|
let method = expand_deriving_iter_bytes_struct_method(cx,
|
|
|
|
span,
|
|
|
|
struct_def);
|
|
|
|
|
|
|
|
// Create the implementation.
|
2012-11-20 21:20:55 -06:00
|
|
|
return create_derived_iter_bytes_impl(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
move ty_params,
|
|
|
|
method);
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving_iter_bytes_enum_def(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
enum_definition: &enum_def,
|
2012-11-20 21:20:55 -06:00
|
|
|
type_ident: ident,
|
|
|
|
+ty_params: ~[ty_param])
|
2012-11-20 20:27:13 -06:00
|
|
|
-> @item {
|
|
|
|
// Create the method.
|
|
|
|
let method = expand_deriving_iter_bytes_enum_method(cx,
|
|
|
|
span,
|
|
|
|
enum_definition);
|
|
|
|
|
|
|
|
// Create the implementation.
|
2012-11-20 21:20:55 -06:00
|
|
|
return create_derived_iter_bytes_impl(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
|
|
|
move ty_params,
|
|
|
|
method);
|
2012-11-20 20:27:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving_eq_struct_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
struct_def: &struct_def,
|
|
|
|
method_ident: ident,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params: &[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
junction: Junction)
|
|
|
|
-> @method {
|
2012-11-19 20:05:50 -06:00
|
|
|
let self_ident = cx.ident_of(~"self");
|
|
|
|
let other_ident = cx.ident_of(~"__other");
|
|
|
|
|
|
|
|
// Create the body of the method.
|
|
|
|
let mut outer_expr = None;
|
|
|
|
for struct_def.fields.each |struct_field| {
|
|
|
|
match struct_field.node.kind {
|
|
|
|
named_field(ident, _, _) => {
|
|
|
|
// Create the accessor for the other field.
|
|
|
|
let other_field = build::mk_access(cx,
|
|
|
|
span,
|
|
|
|
~[ other_ident ],
|
|
|
|
ident);
|
|
|
|
let other_field_ref = build::mk_addr_of(cx,
|
|
|
|
span,
|
|
|
|
other_field);
|
|
|
|
|
|
|
|
// Create the accessor for this field.
|
|
|
|
let self_field = build::mk_access(cx,
|
|
|
|
span,
|
|
|
|
~[ self_ident ],
|
|
|
|
ident);
|
|
|
|
|
|
|
|
// Call the substructure method.
|
2012-11-20 20:27:13 -06:00
|
|
|
call_substructure_eq_method(cx,
|
|
|
|
span,
|
|
|
|
self_field,
|
|
|
|
other_field_ref,
|
|
|
|
method_ident,
|
|
|
|
junction,
|
|
|
|
&mut outer_expr);
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
unnamed_field => {
|
|
|
|
cx.span_unimpl(span, ~"unnamed fields with `deriving_eq`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the method itself.
|
2012-11-20 20:27:13 -06:00
|
|
|
let body = finish_eq_chain_expr(cx, span, outer_expr, junction);
|
2012-11-20 21:20:55 -06:00
|
|
|
return create_eq_method(cx,
|
|
|
|
span,
|
|
|
|
method_ident,
|
|
|
|
type_ident,
|
|
|
|
ty_params,
|
|
|
|
body);
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
fn expand_deriving_iter_bytes_struct_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
struct_def: &struct_def)
|
|
|
|
-> @method {
|
|
|
|
let self_ident = cx.ident_of(~"self");
|
|
|
|
|
|
|
|
// Create the body of the method.
|
|
|
|
let statements = dvec::DVec();
|
|
|
|
for struct_def.fields.each |struct_field| {
|
|
|
|
match struct_field.node.kind {
|
|
|
|
named_field(ident, _, _) => {
|
|
|
|
// Create the accessor for this field.
|
|
|
|
let self_field = build::mk_access(cx,
|
|
|
|
span,
|
|
|
|
~[ self_ident ],
|
|
|
|
ident);
|
|
|
|
|
|
|
|
// Call the substructure method.
|
|
|
|
let stmt = call_substructure_iter_bytes_method(cx,
|
|
|
|
span,
|
|
|
|
self_field);
|
|
|
|
statements.push(stmt);
|
|
|
|
}
|
|
|
|
unnamed_field => {
|
|
|
|
cx.span_unimpl(span,
|
|
|
|
~"unnamed fields with `deriving_iter_bytes`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the method itself.
|
|
|
|
let statements = dvec::unwrap(move statements);
|
|
|
|
return create_iter_bytes_method(cx, span, move statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving_eq_enum_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
enum_definition: &enum_def,
|
|
|
|
method_ident: ident,
|
|
|
|
type_ident: ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params: &[ty_param],
|
2012-11-20 20:27:13 -06:00
|
|
|
junction: Junction)
|
|
|
|
-> @method {
|
2012-11-19 20:05:50 -06:00
|
|
|
let self_ident = cx.ident_of(~"self");
|
|
|
|
let other_ident = cx.ident_of(~"__other");
|
|
|
|
|
|
|
|
let is_eq;
|
|
|
|
match junction {
|
|
|
|
Conjunction => is_eq = true,
|
|
|
|
Disjunction => is_eq = false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the arms of the self match in the method body.
|
|
|
|
let self_arms = dvec::DVec();
|
|
|
|
for enum_definition.variants.each |self_variant| {
|
|
|
|
let other_arms = dvec::DVec();
|
|
|
|
|
|
|
|
// Create the matching pattern.
|
2012-11-20 14:59:37 -06:00
|
|
|
let matching_pat = create_enum_variant_pattern(cx,
|
|
|
|
span,
|
|
|
|
self_variant,
|
|
|
|
~"__other");
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the matching pattern body.
|
2012-11-20 14:59:37 -06:00
|
|
|
let mut matching_body_expr = None;
|
|
|
|
for uint::range(0, variant_arg_count(cx, span, self_variant)) |i| {
|
|
|
|
// Create the expression for the other field.
|
|
|
|
let other_field_ident = cx.ident_of(~"__other" + i.to_str());
|
|
|
|
let other_field = build::mk_path(cx,
|
|
|
|
span,
|
|
|
|
~[ other_field_ident ]);
|
|
|
|
|
|
|
|
// Create the expression for this field.
|
|
|
|
let self_field_ident = cx.ident_of(~"__self" + i.to_str());
|
|
|
|
let self_field = build::mk_path(cx, span, ~[ self_field_ident ]);
|
|
|
|
|
|
|
|
// Call the substructure method.
|
2012-11-20 20:27:13 -06:00
|
|
|
call_substructure_eq_method(cx,
|
|
|
|
span,
|
|
|
|
self_field,
|
|
|
|
other_field,
|
|
|
|
method_ident,
|
|
|
|
junction,
|
|
|
|
&mut matching_body_expr);
|
2012-11-20 14:59:37 -06:00
|
|
|
}
|
|
|
|
|
2012-11-20 20:27:13 -06:00
|
|
|
let matching_body_expr = finish_eq_chain_expr(cx,
|
|
|
|
span,
|
|
|
|
matching_body_expr,
|
|
|
|
junction);
|
2012-11-19 20:05:50 -06:00
|
|
|
let matching_body_block = build::mk_simple_block(cx,
|
|
|
|
span,
|
|
|
|
matching_body_expr);
|
|
|
|
|
|
|
|
// Create the matching arm.
|
2013-01-14 22:52:28 -06:00
|
|
|
let matching_arm = ast::arm {
|
2012-11-19 20:05:50 -06:00
|
|
|
pats: ~[ matching_pat ],
|
|
|
|
guard: None,
|
|
|
|
body: move matching_body_block
|
|
|
|
};
|
|
|
|
other_arms.push(move matching_arm);
|
|
|
|
|
2012-12-11 17:40:49 -06:00
|
|
|
// Maybe generate a non-matching case. If there is only one
|
|
|
|
// variant then there will always be a match.
|
|
|
|
if enum_definition.variants.len() > 1 {
|
|
|
|
// Create the nonmatching pattern.
|
2013-01-14 22:52:28 -06:00
|
|
|
let nonmatching_pat = @ast::pat {
|
2012-12-11 17:40:49 -06:00
|
|
|
id: cx.next_id(),
|
|
|
|
node: pat_wild,
|
|
|
|
span: span
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the nonmatching pattern body.
|
|
|
|
let nonmatching_expr = build::mk_bool(cx, span, !is_eq);
|
2012-12-11 18:04:35 -06:00
|
|
|
let nonmatching_body_block =
|
|
|
|
build::mk_simple_block(cx,
|
|
|
|
span,
|
|
|
|
nonmatching_expr);
|
2012-12-11 17:40:49 -06:00
|
|
|
|
|
|
|
// Create the nonmatching arm.
|
2013-01-14 22:52:28 -06:00
|
|
|
let nonmatching_arm = ast::arm {
|
2012-12-11 17:40:49 -06:00
|
|
|
pats: ~[ nonmatching_pat ],
|
|
|
|
guard: None,
|
2013-01-14 22:52:28 -06:00
|
|
|
body: nonmatching_body_block,
|
2012-12-11 17:40:49 -06:00
|
|
|
};
|
2013-01-14 22:52:28 -06:00
|
|
|
other_arms.push(nonmatching_arm);
|
2012-12-11 17:40:49 -06:00
|
|
|
}
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the self pattern.
|
2012-11-20 14:59:37 -06:00
|
|
|
let self_pat = create_enum_variant_pattern(cx,
|
|
|
|
span,
|
|
|
|
self_variant,
|
|
|
|
~"__self");
|
2012-11-19 20:05:50 -06:00
|
|
|
|
|
|
|
// Create the self pattern body.
|
|
|
|
let other_expr = build::mk_path(cx, span, ~[ other_ident ]);
|
|
|
|
let other_expr = build::mk_unary(cx, span, deref, other_expr);
|
|
|
|
let other_arms = dvec::unwrap(move other_arms);
|
|
|
|
let other_match_expr = expr_match(other_expr, move other_arms);
|
|
|
|
let other_match_expr = build::mk_expr(cx,
|
|
|
|
span,
|
|
|
|
move other_match_expr);
|
|
|
|
let other_match_body_block = build::mk_simple_block(cx,
|
|
|
|
span,
|
|
|
|
other_match_expr);
|
|
|
|
|
|
|
|
// Create the self arm.
|
2013-01-14 22:52:28 -06:00
|
|
|
let self_arm = ast::arm {
|
2012-11-19 20:05:50 -06:00
|
|
|
pats: ~[ self_pat ],
|
|
|
|
guard: None,
|
2013-01-14 22:52:28 -06:00
|
|
|
body: other_match_body_block,
|
2012-11-19 20:05:50 -06:00
|
|
|
};
|
|
|
|
self_arms.push(move self_arm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the method body.
|
|
|
|
let self_expr = build::mk_path(cx, span, ~[ self_ident ]);
|
|
|
|
let self_expr = build::mk_unary(cx, span, deref, self_expr);
|
|
|
|
let self_arms = dvec::unwrap(move self_arms);
|
|
|
|
let self_match_expr = expr_match(self_expr, move self_arms);
|
|
|
|
let self_match_expr = build::mk_expr(cx, span, move self_match_expr);
|
|
|
|
|
|
|
|
// Create the method.
|
2012-11-20 20:27:13 -06:00
|
|
|
return create_eq_method(cx,
|
|
|
|
span,
|
|
|
|
method_ident,
|
|
|
|
type_ident,
|
2012-11-20 21:20:55 -06:00
|
|
|
ty_params,
|
2012-11-20 20:27:13 -06:00
|
|
|
self_match_expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_deriving_iter_bytes_enum_method(cx: ext_ctxt,
|
|
|
|
span: span,
|
|
|
|
enum_definition: &enum_def)
|
|
|
|
-> @method {
|
|
|
|
// Create the arms of the match in the method body.
|
2013-01-14 22:52:28 -06:00
|
|
|
let arms = do enum_definition.variants.mapi |i, variant| {
|
2012-11-20 20:27:13 -06:00
|
|
|
// Create the matching pattern.
|
|
|
|
let pat = create_enum_variant_pattern(cx, span, variant, ~"__self");
|
|
|
|
|
|
|
|
// Determine the discriminant. We will feed this value to the byte
|
|
|
|
// iteration function.
|
|
|
|
let discriminant;
|
|
|
|
match variant.node.disr_expr {
|
|
|
|
Some(copy disr_expr) => discriminant = disr_expr,
|
|
|
|
None => discriminant = build::mk_uint(cx, span, i),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Feed the discriminant to the byte iteration function.
|
|
|
|
let stmts = dvec::DVec();
|
|
|
|
let discrim_stmt = call_substructure_iter_bytes_method(cx,
|
|
|
|
span,
|
|
|
|
discriminant);
|
|
|
|
stmts.push(discrim_stmt);
|
|
|
|
|
|
|
|
// Feed each argument in this variant to the byte iteration function
|
|
|
|
// as well.
|
|
|
|
for uint::range(0, variant_arg_count(cx, span, variant)) |j| {
|
|
|
|
// Create the expression for this field.
|
|
|
|
let field_ident = cx.ident_of(~"__self" + j.to_str());
|
|
|
|
let field = build::mk_path(cx, span, ~[ field_ident ]);
|
|
|
|
|
|
|
|
// Call the substructure method.
|
|
|
|
let stmt = call_substructure_iter_bytes_method(cx, span, field);
|
|
|
|
stmts.push(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the pattern body.
|
|
|
|
let stmts = dvec::unwrap(move stmts);
|
|
|
|
let match_body_block = build::mk_block_(cx, span, move stmts);
|
|
|
|
|
|
|
|
// Create the arm.
|
2013-01-14 22:52:28 -06:00
|
|
|
ast::arm {
|
2012-11-20 20:27:13 -06:00
|
|
|
pats: ~[ pat ],
|
|
|
|
guard: None,
|
2013-01-14 22:52:28 -06:00
|
|
|
body: match_body_block,
|
|
|
|
}
|
|
|
|
};
|
2012-11-20 20:27:13 -06:00
|
|
|
|
|
|
|
// Create the method body.
|
|
|
|
let self_ident = cx.ident_of(~"self");
|
|
|
|
let self_expr = build::mk_path(cx, span, ~[ self_ident ]);
|
2012-11-28 13:36:04 -06:00
|
|
|
let self_expr = build::mk_unary(cx, span, deref, self_expr);
|
2013-01-14 22:52:28 -06:00
|
|
|
let self_match_expr = expr_match(self_expr, arms);
|
|
|
|
let self_match_expr = build::mk_expr(cx, span, self_match_expr);
|
2012-11-20 20:27:13 -06:00
|
|
|
let self_match_stmt = build::mk_stmt(cx, span, self_match_expr);
|
|
|
|
|
|
|
|
// Create the method.
|
2013-01-14 22:52:28 -06:00
|
|
|
create_iter_bytes_method(cx, span, ~[ self_match_stmt ])
|
2012-11-19 20:05:50 -06:00
|
|
|
}
|
|
|
|
|