2013-03-19 12:52:10 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-03-20 15:52:45 +00:00
|
|
|
/// The compiler code necessary to implement the #[deriving(Eq)] and
|
|
|
|
/// #[deriving(IterBytes)] extensions.
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2013-01-09 03:37:25 +00:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-01-13 18:48:09 +00:00
|
|
|
use ast;
|
2013-03-26 20:38:07 +00:00
|
|
|
use ast::{TraitTyParamBound, Ty, bind_by_ref, deref, enum_def};
|
|
|
|
use ast::{expr, expr_match, ident, item, item_};
|
2013-02-15 05:50:03 +00:00
|
|
|
use ast::{item_enum, item_impl, item_struct, Generics};
|
|
|
|
use ast::{m_imm, meta_item, method};
|
2013-03-26 20:38:07 +00:00
|
|
|
use ast::{named_field, pat, pat_ident, public};
|
|
|
|
use ast::{struct_def, struct_variant_kind};
|
|
|
|
use ast::{tuple_variant_kind};
|
|
|
|
use ast::{ty_path, unnamed_field, variant};
|
2012-12-13 21:05:22 +00:00
|
|
|
use ext::base::ext_ctxt;
|
2012-12-23 22:41:37 +00:00
|
|
|
use ext::build;
|
2013-03-26 20:38:07 +00:00
|
|
|
use codemap::span;
|
2012-11-20 02:05:50 +00:00
|
|
|
use parse::token::special_idents::clownshoes_extensions;
|
2013-02-15 05:50:03 +00:00
|
|
|
use opt_vec;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2012-12-23 22:41:37 +00:00
|
|
|
use core::uint;
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub mod clone;
|
|
|
|
pub mod eq;
|
|
|
|
pub mod iter_bytes;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2013-03-25 20:21:04 +00:00
|
|
|
type ExpandDerivingStructDefFn<'self> = &'self fn(@ext_ctxt,
|
|
|
|
span,
|
|
|
|
x: &struct_def,
|
|
|
|
ident,
|
|
|
|
y: &Generics)
|
|
|
|
-> @item;
|
|
|
|
type ExpandDerivingEnumDefFn<'self> = &'self fn(@ext_ctxt,
|
|
|
|
span,
|
|
|
|
x: &enum_def,
|
|
|
|
ident,
|
|
|
|
y: &Generics)
|
|
|
|
-> @item;
|
2012-11-21 02:27:13 +00:00
|
|
|
|
2013-03-12 20:00:50 +00:00
|
|
|
pub fn expand_meta_deriving(cx: @ext_ctxt,
|
2013-03-11 20:47:23 +00:00
|
|
|
_span: span,
|
|
|
|
mitem: @meta_item,
|
|
|
|
in_items: ~[@item])
|
|
|
|
-> ~[@item] {
|
|
|
|
use ast::{meta_list, meta_name_value, meta_word};
|
|
|
|
|
|
|
|
match mitem.node {
|
|
|
|
meta_name_value(_, l) => {
|
|
|
|
cx.span_err(l.span, ~"unexpected value in `deriving`");
|
|
|
|
in_items
|
|
|
|
}
|
|
|
|
meta_word(_) | meta_list(_, []) => {
|
|
|
|
cx.span_warn(mitem.span, ~"empty trait list in `deriving`");
|
|
|
|
in_items
|
|
|
|
}
|
|
|
|
meta_list(_, titems) => {
|
|
|
|
do titems.foldr(in_items) |&titem, in_items| {
|
|
|
|
match titem.node {
|
|
|
|
meta_name_value(tname, _) |
|
|
|
|
meta_list(tname, _) |
|
|
|
|
meta_word(tname) => {
|
|
|
|
match *tname {
|
2013-03-19 12:52:10 +00:00
|
|
|
~"Clone" => clone::expand_deriving_clone(cx,
|
2013-03-11 20:47:23 +00:00
|
|
|
titem.span, titem, in_items),
|
2013-03-19 12:52:10 +00:00
|
|
|
~"Eq" => eq::expand_deriving_eq(cx, titem.span,
|
2013-03-11 20:47:23 +00:00
|
|
|
titem, in_items),
|
2013-03-19 12:52:10 +00:00
|
|
|
~"IterBytes" => iter_bytes::expand_deriving_iter_bytes(cx,
|
2013-03-11 20:47:23 +00:00
|
|
|
titem.span, titem, in_items),
|
|
|
|
tname => {
|
|
|
|
cx.span_err(titem.span, fmt!("unknown \
|
|
|
|
`deriving` trait: `%s`", tname));
|
|
|
|
in_items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn expand_deriving(cx: @ext_ctxt,
|
2012-11-21 02:27:13 +00:00
|
|
|
span: span,
|
|
|
|
in_items: ~[@item],
|
|
|
|
expand_deriving_struct_def: ExpandDerivingStructDefFn,
|
|
|
|
expand_deriving_enum_def: ExpandDerivingEnumDefFn)
|
|
|
|
-> ~[@item] {
|
2013-03-07 23:37:22 +00:00
|
|
|
let mut result = ~[];
|
2012-11-20 02:05:50 +00:00
|
|
|
for in_items.each |item| {
|
|
|
|
result.push(copy *item);
|
|
|
|
match item.node {
|
2013-02-15 05:50:03 +00:00
|
|
|
item_struct(struct_def, ref generics) => {
|
2012-11-20 02:05:50 +00:00
|
|
|
result.push(expand_deriving_struct_def(cx,
|
|
|
|
span,
|
|
|
|
struct_def,
|
2012-11-21 03:20:55 +00:00
|
|
|
item.ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics));
|
2012-11-20 02:05:50 +00:00
|
|
|
}
|
2013-02-15 05:50:03 +00:00
|
|
|
item_enum(ref enum_definition, ref generics) => {
|
2012-11-20 02:05:50 +00:00
|
|
|
result.push(expand_deriving_enum_def(cx,
|
|
|
|
span,
|
|
|
|
enum_definition,
|
2012-11-21 03:20:55 +00:00
|
|
|
item.ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics));
|
2012-11-20 02:05:50 +00:00
|
|
|
}
|
2012-11-20 21:01:02 +00:00
|
|
|
_ => ()
|
2012-11-20 02:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-07 23:37:22 +00:00
|
|
|
result
|
2012-11-20 02:05:50 +00:00
|
|
|
}
|
|
|
|
|
2013-03-12 20:00:50 +00:00
|
|
|
fn create_impl_item(cx: @ext_ctxt, span: span, +item: item_) -> @item {
|
2013-01-13 21:13:41 +00:00
|
|
|
@ast::item {
|
2012-11-20 02:05:50 +00:00
|
|
|
ident: clownshoes_extensions,
|
|
|
|
attrs: ~[],
|
|
|
|
id: cx.next_id(),
|
2013-02-15 09:15:53 +00:00
|
|
|
node: item,
|
2012-11-21 03:20:55 +00:00
|
|
|
vis: public,
|
2012-11-20 02:05:50 +00:00
|
|
|
span: span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn create_self_type_with_params(cx: @ext_ctxt,
|
2012-11-21 03:20:55 +00:00
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: &Generics)
|
2012-11-21 03:20:55 +00:00
|
|
|
-> @Ty {
|
|
|
|
// Create the type parameters on the `self` path.
|
2013-03-07 23:37:22 +00:00
|
|
|
let mut self_ty_params = ~[];
|
2013-02-15 05:50:03 +00:00
|
|
|
for generics.ty_params.each |ty_param| {
|
2012-11-21 03:20:55 +00:00
|
|
|
let self_ty_param = build::mk_simple_ty_path(cx,
|
|
|
|
span,
|
|
|
|
ty_param.ident);
|
2013-02-15 09:15:53 +00:00
|
|
|
self_ty_params.push(self_ty_param);
|
2012-11-21 03:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the type of `self`.
|
|
|
|
let self_type = build::mk_raw_path_(span,
|
|
|
|
~[ type_ident ],
|
2013-02-15 09:15:53 +00:00
|
|
|
self_ty_params);
|
2012-11-21 03:20:55 +00:00
|
|
|
let self_type = ty_path(self_type, cx.next_id());
|
2013-01-15 22:59:39 +00:00
|
|
|
@ast::Ty { id: cx.next_id(), node: self_type, span: span }
|
2012-11-21 03:20:55 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn create_derived_impl(cx: @ext_ctxt,
|
2012-11-20 02:05:50 +00:00
|
|
|
span: span,
|
|
|
|
type_ident: ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics: &Generics,
|
2012-11-21 02:27:13 +00:00
|
|
|
methods: &[@method],
|
2012-11-21 03:20:55 +00:00
|
|
|
trait_path: &[ident])
|
2012-11-20 02:05:50 +00:00
|
|
|
-> @item {
|
2013-02-15 05:50:03 +00:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Given that we are deriving a trait `Tr` for a type `T<'a, ...,
|
|
|
|
* 'z, A, ..., Z>`, creates an impl like:
|
|
|
|
*
|
|
|
|
* impl<'a, ..., 'z, A:Tr, ..., Z: Tr> Tr for T<A, ..., Z> { ... }
|
|
|
|
*
|
|
|
|
* FIXME(#5090): Remove code duplication between this and the
|
|
|
|
* code in auto_encode.rs
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Copy the lifetimes
|
|
|
|
let impl_lifetimes = generics.lifetimes.map(|l| {
|
|
|
|
build::mk_lifetime(cx, l.span, l.ident)
|
|
|
|
});
|
|
|
|
|
2012-11-21 03:20:55 +00:00
|
|
|
// Create the type parameters.
|
2013-02-15 05:50:03 +00:00
|
|
|
let impl_ty_params = generics.ty_params.map(|ty_param| {
|
2012-12-23 22:41:37 +00:00
|
|
|
let bound = build::mk_ty_path_global(cx,
|
|
|
|
span,
|
|
|
|
trait_path.map(|x| *x));
|
2013-02-15 05:50:03 +00:00
|
|
|
let bounds = @opt_vec::with(TraitTyParamBound(bound));
|
|
|
|
build::mk_ty_param(cx, ty_param.ident, bounds)
|
|
|
|
});
|
2012-11-21 03:20:55 +00:00
|
|
|
|
2012-11-21 02:27:13 +00:00
|
|
|
// Create the reference to the trait.
|
2013-01-13 18:48:09 +00:00
|
|
|
let trait_path = ast::path {
|
2012-11-20 02:05:50 +00:00
|
|
|
span: span,
|
2012-12-23 22:41:37 +00:00
|
|
|
global: true,
|
2012-11-21 02:27:13 +00:00
|
|
|
idents: trait_path.map(|x| *x),
|
2012-11-20 02:05:50 +00:00
|
|
|
rp: None,
|
|
|
|
types: ~[]
|
|
|
|
};
|
2013-02-15 09:15:53 +00:00
|
|
|
let trait_path = @trait_path;
|
2013-01-16 00:05:20 +00:00
|
|
|
let trait_ref = ast::trait_ref {
|
2012-11-21 02:27:13 +00:00
|
|
|
path: trait_path,
|
2012-12-07 03:12:25 +00:00
|
|
|
ref_id: cx.next_id()
|
2012-11-20 02:05:50 +00:00
|
|
|
};
|
2013-02-15 09:15:53 +00:00
|
|
|
let trait_ref = @trait_ref;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
|
|
|
// Create the type of `self`.
|
2012-11-21 03:20:55 +00:00
|
|
|
let self_type = create_self_type_with_params(cx,
|
|
|
|
span,
|
|
|
|
type_ident,
|
2013-02-15 05:50:03 +00:00
|
|
|
generics);
|
2012-11-20 02:05:50 +00:00
|
|
|
|
|
|
|
// Create the impl item.
|
2013-02-15 05:50:03 +00:00
|
|
|
let impl_item = item_impl(Generics {lifetimes: impl_lifetimes,
|
|
|
|
ty_params: impl_ty_params},
|
2012-11-20 02:05:50 +00:00
|
|
|
Some(trait_ref),
|
|
|
|
self_type,
|
2012-11-21 02:27:13 +00:00
|
|
|
methods.map(|x| *x));
|
2013-02-15 09:15:53 +00:00
|
|
|
return create_impl_item(cx, span, impl_item);
|
2012-11-20 02:05:50 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn create_subpatterns(cx: @ext_ctxt,
|
2012-12-10 21:04:04 +00:00
|
|
|
span: span,
|
|
|
|
prefix: ~str,
|
|
|
|
n: uint)
|
|
|
|
-> ~[@pat] {
|
2013-03-07 23:37:22 +00:00
|
|
|
let mut subpats = ~[];
|
2012-12-10 21:04:04 +00:00
|
|
|
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);
|
2013-02-15 09:15:53 +00:00
|
|
|
let subpat = build::mk_pat(cx, span, subpat);
|
2012-12-10 21:04:04 +00:00
|
|
|
subpats.push(subpat);
|
|
|
|
}
|
2013-03-07 23:37:22 +00:00
|
|
|
return subpats;
|
2012-12-10 21:04:04 +00:00
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn is_struct_tuple(struct_def: &struct_def) -> bool {
|
2013-03-04 23:33:05 +00:00
|
|
|
struct_def.fields.len() > 0 && struct_def.fields.all(|f| {
|
|
|
|
match f.node.kind {
|
|
|
|
named_field(*) => false,
|
|
|
|
unnamed_field => true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn create_enum_variant_pattern(cx: @ext_ctxt,
|
2012-11-20 20:59:37 +00:00
|
|
|
span: span,
|
2012-11-21 03:20:55 +00:00
|
|
|
variant: &variant,
|
2012-11-20 20:59:37 +00:00
|
|
|
prefix: ~str)
|
2012-11-21 03:20:55 +00:00
|
|
|
-> @pat {
|
2012-11-20 20:59:37 +00:00
|
|
|
let variant_ident = variant.node.name;
|
|
|
|
match variant.node.kind {
|
|
|
|
tuple_variant_kind(ref variant_args) => {
|
|
|
|
if variant_args.len() == 0 {
|
2013-01-25 00:24:45 +00:00
|
|
|
return build::mk_pat_ident_with_binding_mode(
|
|
|
|
cx, span, variant_ident, ast::bind_infer);
|
2012-11-20 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let matching_path = build::mk_raw_path(span, ~[ variant_ident ]);
|
2012-12-10 21:04:04 +00:00
|
|
|
let subpats = create_subpatterns(cx,
|
|
|
|
span,
|
|
|
|
prefix,
|
|
|
|
variant_args.len());
|
|
|
|
|
2013-02-15 09:15:53 +00:00
|
|
|
return build::mk_pat_enum(cx, span, matching_path, subpats);
|
2012-11-20 20:59:37 +00:00
|
|
|
}
|
2012-12-10 21:04:04 +00: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-15 04:52:28 +00:00
|
|
|
let field_pats = do struct_def.fields.mapi |i, struct_field| {
|
2012-12-10 21:04:04 +00:00
|
|
|
let ident = match struct_field.node.kind {
|
|
|
|
named_field(ident, _, _) => ident,
|
|
|
|
unnamed_field => {
|
|
|
|
cx.span_bug(span, ~"unexpected unnamed field");
|
|
|
|
}
|
|
|
|
};
|
2013-01-15 04:52:28 +00:00
|
|
|
ast::field_pat { ident: ident, pat: subpats[i] }
|
|
|
|
};
|
2012-12-10 21:04:04 +00:00
|
|
|
|
2013-01-15 04:52:28 +00:00
|
|
|
build::mk_pat_struct(cx, span, matching_path, field_pats)
|
2012-11-20 20:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 02:27:51 +00:00
|
|
|
pub fn variant_arg_count(_cx: @ext_ctxt, _span: span, variant: &variant) -> uint {
|
2012-11-20 20:59:37 +00:00
|
|
|
match variant.node.kind {
|
2013-02-25 05:27:51 +00:00
|
|
|
tuple_variant_kind(ref args) => args.len(),
|
|
|
|
struct_variant_kind(ref struct_def) => struct_def.fields.len(),
|
2012-11-20 20:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 12:52:10 +00:00
|
|
|
pub fn expand_enum_or_struct_match(cx: @ext_ctxt,
|
2013-03-04 23:33:05 +00:00
|
|
|
span: span,
|
|
|
|
arms: ~[ ast::arm ])
|
|
|
|
-> @expr {
|
|
|
|
let self_ident = cx.ident_of(~"self");
|
|
|
|
let self_expr = build::mk_path(cx, span, ~[ self_ident ]);
|
|
|
|
let self_expr = build::mk_unary(cx, span, deref, self_expr);
|
|
|
|
let self_match_expr = expr_match(self_expr, arms);
|
|
|
|
build::mk_expr(cx, span, self_match_expr)
|
|
|
|
}
|