2014-01-30 19:29:35 +01:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-03-28 21:50:10 +11: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.
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
//! Some code that abstracts away much of the boilerplate of writing
|
2014-12-31 17:25:18 +13:00
|
|
|
//! `derive` instances for traits. Among other things it manages getting
|
2014-06-09 13:12:30 -07:00
|
|
|
//! access to the fields of the 4 different sorts of structs and enum
|
|
|
|
//! variants, as well as creating the method and impl ast instances.
|
|
|
|
//!
|
|
|
|
//! Supported features (fairly exhaustive):
|
|
|
|
//!
|
|
|
|
//! - Methods taking any number of parameters of any type, and returning
|
|
|
|
//! any type, other than vectors, bottom and closures.
|
|
|
|
//! - Generating `impl`s for types with type parameters and lifetimes
|
|
|
|
//! (e.g. `Option<T>`), the parameters are automatically given the
|
|
|
|
//! current trait as a bound. (This includes separate type parameters
|
|
|
|
//! and lifetimes for methods.)
|
2015-01-13 00:00:19 +11:00
|
|
|
//! - Additional bounds on the type parameters (`TraitDef.additional_bounds`)
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! The most important thing for implementers is the `Substructure` and
|
|
|
|
//! `SubstructureFields` objects. The latter groups 5 possibilities of the
|
|
|
|
//! arguments:
|
|
|
|
//!
|
|
|
|
//! - `Struct`, when `Self` is a struct (including tuple structs, e.g
|
2015-01-17 23:55:21 +00:00
|
|
|
//! `struct T(i32, char)`).
|
2014-06-09 13:12:30 -07:00
|
|
|
//! - `EnumMatching`, when `Self` is an enum and all the arguments are the
|
|
|
|
//! same variant of the enum (e.g. `Some(1)`, `Some(3)` and `Some(4)`)
|
2014-07-07 09:13:49 +02:00
|
|
|
//! - `EnumNonMatchingCollapsed` when `Self` is an enum and the arguments
|
|
|
|
//! are not the same variant (e.g. `None`, `Some(1)` and `None`).
|
2014-06-09 13:12:30 -07:00
|
|
|
//! - `StaticEnum` and `StaticStruct` for static methods, where the type
|
|
|
|
//! being derived upon is either an enum or struct respectively. (Any
|
|
|
|
//! argument with type Self is just grouped among the non-self
|
|
|
|
//! arguments.)
|
|
|
|
//!
|
|
|
|
//! In the first two cases, the values from the corresponding fields in
|
2014-07-07 09:13:49 +02:00
|
|
|
//! all the arguments are grouped together. For `EnumNonMatchingCollapsed`
|
2014-06-09 13:12:30 -07:00
|
|
|
//! this isn't possible (different variants have different fields), so the
|
2014-07-07 09:13:49 +02:00
|
|
|
//! fields are inaccessible. (Previous versions of the deriving infrastructure
|
|
|
|
//! had a way to expand into code that could access them, at the cost of
|
|
|
|
//! generating exponential amounts of code; see issue #15375). There are no
|
2014-06-09 13:12:30 -07:00
|
|
|
//! fields with values in the static cases, so these are treated entirely
|
|
|
|
//! differently.
|
|
|
|
//!
|
|
|
|
//! The non-static cases have `Option<ident>` in several places associated
|
|
|
|
//! with field `expr`s. This represents the name of the field it is
|
|
|
|
//! associated with. It is only not `None` when the associated field has
|
|
|
|
//! an identifier in the source code. For example, the `x`s in the
|
|
|
|
//! following snippet
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2015-01-17 23:55:21 +00:00
|
|
|
//! struct A { x : i32 }
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2015-01-17 23:55:21 +00:00
|
|
|
//! struct B(i32);
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! enum C {
|
2015-01-17 23:55:21 +00:00
|
|
|
//! C0(i32),
|
|
|
|
//! C1 { x: i32 }
|
2014-06-09 13:12:30 -07:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2015-01-17 23:55:21 +00:00
|
|
|
//! The `i32`s in `B` and `C0` don't have an identifier, so the
|
2014-06-09 13:12:30 -07:00
|
|
|
//! `Option<ident>`s would be `None` for them.
|
|
|
|
//!
|
|
|
|
//! In the static cases, the structure is summarised, either into the just
|
|
|
|
//! spans of the fields or a list of spans and the field idents (for tuple
|
|
|
|
//! structs and record structs, respectively), or a list of these, for
|
|
|
|
//! enums (one for each variant). For empty struct and empty enum
|
|
|
|
//! variants, it is represented as a count of 0.
|
|
|
|
//!
|
2015-01-13 00:00:19 +11:00
|
|
|
//! # "`cs`" functions
|
|
|
|
//!
|
|
|
|
//! The `cs_...` functions ("combine substructure) are designed to
|
|
|
|
//! make life easier by providing some pre-made recipes for common
|
|
|
|
//! tasks; mostly calling the function being derived on all the
|
|
|
|
//! arguments and then combining them back together in some way (or
|
|
|
|
//! letting the user chose that). They are not meant to be the only
|
|
|
|
//! way to handle the structures that this code creates.
|
|
|
|
//!
|
2014-06-09 13:12:30 -07:00
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! The following simplified `PartialEq` is used for in-code examples:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! trait PartialEq {
|
|
|
|
//! fn eq(&self, other: &Self);
|
|
|
|
//! }
|
2015-01-17 23:55:21 +00:00
|
|
|
//! impl PartialEq for i32 {
|
|
|
|
//! fn eq(&self, other: &i32) -> bool {
|
2014-06-09 13:12:30 -07:00
|
|
|
//! *self == *other
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Some examples of the values of `SubstructureFields` follow, using the
|
|
|
|
//! above `PartialEq`, `A`, `B` and `C`.
|
|
|
|
//!
|
|
|
|
//! ## Structs
|
|
|
|
//!
|
|
|
|
//! When generating the `expr` for the `A` impl, the `SubstructureFields` is
|
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2015-01-13 00:00:19 +11:00
|
|
|
//! Struct(vec![FieldInfo {
|
2014-06-09 13:12:30 -07:00
|
|
|
//! span: <span of x>
|
|
|
|
//! name: Some(<ident of x>),
|
|
|
|
//! self_: <expr for &self.x>,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! other: vec![<expr for &other.x]
|
2014-06-09 13:12:30 -07:00
|
|
|
//! }])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! For the `B` impl, called with `B(a)` and `B(b)`,
|
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2015-01-13 00:00:19 +11:00
|
|
|
//! Struct(vec![FieldInfo {
|
2015-01-17 23:55:21 +00:00
|
|
|
//! span: <span of `i32`>,
|
2014-06-09 13:12:30 -07:00
|
|
|
//! name: None,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! self_: <expr for &a>
|
|
|
|
//! other: vec![<expr for &b>]
|
2014-06-09 13:12:30 -07:00
|
|
|
//! }])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! ## Enums
|
|
|
|
//!
|
|
|
|
//! When generating the `expr` for a call with `self == C0(a)` and `other
|
|
|
|
//! == C0(b)`, the SubstructureFields is
|
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2014-06-09 13:12:30 -07:00
|
|
|
//! EnumMatching(0, <ast::Variant for C0>,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! vec![FieldInfo {
|
2015-01-17 23:55:21 +00:00
|
|
|
//! span: <span of i32>
|
2014-06-09 13:12:30 -07:00
|
|
|
//! name: None,
|
|
|
|
//! self_: <expr for &a>,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! other: vec![<expr for &b>]
|
2014-06-09 13:12:30 -07:00
|
|
|
//! }])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2014-07-06 21:19:12 +02:00
|
|
|
//! For `C1 {x}` and `C1 {x}`,
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2014-06-09 13:12:30 -07:00
|
|
|
//! EnumMatching(1, <ast::Variant for C1>,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! vec![FieldInfo {
|
2014-06-09 13:12:30 -07:00
|
|
|
//! span: <span of x>
|
|
|
|
//! name: Some(<ident of x>),
|
|
|
|
//! self_: <expr for &self.x>,
|
2015-01-13 00:00:19 +11:00
|
|
|
//! other: vec![<expr for &other.x>]
|
2014-06-09 13:12:30 -07:00
|
|
|
//! }])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! For `C0(a)` and `C1 {x}` ,
|
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2014-07-06 21:19:12 +02:00
|
|
|
//! EnumNonMatchingCollapsed(
|
2015-01-13 00:00:19 +11:00
|
|
|
//! vec![<ident of self>, <ident of __arg_1>],
|
2014-07-06 21:19:12 +02:00
|
|
|
//! &[<ast::Variant for C0>, <ast::Variant for C1>],
|
|
|
|
//! &[<ident for self index value>, <ident of __arg_1 index value>])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2014-07-06 21:19:12 +02:00
|
|
|
//! It is the same for when the arguments are flipped to `C1 {x}` and
|
|
|
|
//! `C0(a)`; the only difference is what the values of the identifiers
|
|
|
|
//! <ident for self index value> and <ident of __arg_1 index value> will
|
|
|
|
//! be in the generated code.
|
|
|
|
//!
|
|
|
|
//! `EnumNonMatchingCollapsed` deliberately provides far less information
|
|
|
|
//! than is generally available for a given pair of variants; see #15375
|
|
|
|
//! for discussion.
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
|
|
|
//! ## Static
|
|
|
|
//!
|
2015-01-13 00:00:19 +11:00
|
|
|
//! A static method on the types above would result in,
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```{.text}
|
2015-01-13 00:00:19 +11:00
|
|
|
//! StaticStruct(<ast::StructDef of A>, Named(vec![(<ident of x>, <span of x>)]))
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2015-01-13 00:00:19 +11:00
|
|
|
//! StaticStruct(<ast::StructDef of B>, Unnamed(vec![<span of x>]))
|
2014-06-09 13:12:30 -07:00
|
|
|
//!
|
2015-01-13 00:00:19 +11:00
|
|
|
//! StaticEnum(<ast::EnumDef of C>,
|
2015-01-17 23:55:21 +00:00
|
|
|
//! vec![(<ident of C0>, <span of C0>, Unnamed(vec![<span of i32>])),
|
2015-01-13 00:00:19 +11:00
|
|
|
//! (<ident of C1>, <span of C1>, Named(vec![(<ident of x>, <span of x>)]))])
|
2014-09-16 13:27:34 +02:00
|
|
|
//! ```
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::StaticFields::*;
|
|
|
|
pub use self::SubstructureFields::*;
|
|
|
|
use self::StructType::*;
|
|
|
|
|
2014-04-21 23:25:18 -07:00
|
|
|
use std::cell::RefCell;
|
2014-09-13 19:06:01 +03:00
|
|
|
use std::vec;
|
2014-04-21 23:25:18 -07:00
|
|
|
|
2014-05-28 22:26:56 -07:00
|
|
|
use abi::Abi;
|
|
|
|
use abi;
|
2013-03-28 21:50:10 +11:00
|
|
|
use ast;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ast::{EnumDef, Expr, Ident, Generics, StructDef};
|
2014-02-14 07:07:09 +02:00
|
|
|
use ast_util;
|
2014-05-20 00:07:24 -07:00
|
|
|
use attr;
|
2014-05-11 01:11:33 -07:00
|
|
|
use attr::AttrMetaMethods;
|
2013-05-17 21:27:17 +10:00
|
|
|
use ext::base::ExtCtxt;
|
2013-05-18 00:19:28 +10:00
|
|
|
use ext::build::AstBuilder;
|
2015-01-03 22:42:21 -05:00
|
|
|
use codemap::{self, DUMMY_SP};
|
2013-12-07 11:57:44 +11:00
|
|
|
use codemap::Span;
|
2014-09-13 19:06:01 +03:00
|
|
|
use fold::MoveMap;
|
2014-03-20 01:52:37 +11:00
|
|
|
use owned_slice::OwnedSlice;
|
2014-01-08 10:35:15 -08:00
|
|
|
use parse::token::InternedString;
|
2014-07-06 15:10:57 -07:00
|
|
|
use parse::token::special_idents;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-08-12 20:31:30 -07:00
|
|
|
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self, Ty};
|
2014-05-29 12:19:05 +09:00
|
|
|
|
|
|
|
pub mod ty;
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
pub struct TraitDef<'a> {
|
2014-12-31 17:25:18 +13:00
|
|
|
/// The span for the current #[derive(Foo)] header.
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2013-12-07 11:57:44 +11:00
|
|
|
|
2014-03-27 15:39:48 -07:00
|
|
|
pub attributes: Vec<ast::Attribute>,
|
2014-02-14 08:09:51 -08:00
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
/// Path of the trait, including any type parameters
|
2014-03-27 15:39:48 -07:00
|
|
|
pub path: Path<'a>,
|
2014-02-08 19:39:53 -05:00
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
/// Additional bounds required of any type parameters of the type,
|
|
|
|
/// other than the current trait
|
2014-03-27 15:39:48 -07:00
|
|
|
pub additional_bounds: Vec<Ty<'a>>,
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-02-05 08:52:54 -08:00
|
|
|
/// Any extra lifetimes and/or bounds, e.g. `D: serialize::Decoder`
|
2014-03-27 15:39:48 -07:00
|
|
|
pub generics: LifetimeBounds<'a>,
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-03-27 15:39:48 -07:00
|
|
|
pub methods: Vec<MethodDef<'a>>,
|
2015-01-25 00:29:24 -05:00
|
|
|
|
|
|
|
pub associated_types: Vec<(ast::Ident, Ty<'a>)>,
|
2014-03-27 15:39:48 -07:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
pub struct MethodDef<'a> {
|
2013-03-28 21:50:10 +11:00
|
|
|
/// name of the method
|
2014-03-27 15:39:48 -07:00
|
|
|
pub name: &'a str,
|
2014-03-02 11:33:24 +11:00
|
|
|
/// List of generics, e.g. `R: rand::Rng`
|
2014-03-27 15:39:48 -07:00
|
|
|
pub generics: LifetimeBounds<'a>,
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
/// Whether there is a self argument (outer Option) i.e. whether
|
|
|
|
/// this is a static function, and whether it is a pointer (inner
|
|
|
|
/// Option)
|
2014-03-27 15:39:48 -07:00
|
|
|
pub explicit_self: Option<Option<PtrTy<'a>>>,
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
/// Arguments other than the self argument
|
2014-03-27 15:39:48 -07:00
|
|
|
pub args: Vec<Ty<'a>>,
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
/// Return type
|
2014-03-27 15:39:48 -07:00
|
|
|
pub ret_ty: Ty<'a>,
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-04-23 22:43:45 +08:00
|
|
|
pub attributes: Vec<ast::Attribute>,
|
2013-11-19 11:13:34 +11:00
|
|
|
|
2014-04-21 23:25:18 -07:00
|
|
|
pub combine_substructure: RefCell<CombineSubstructureFunc<'a>>,
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// All the data about the data structure/method being derived upon.
|
2013-12-09 23:16:18 -08:00
|
|
|
pub struct Substructure<'a> {
|
2013-05-07 01:23:51 +10:00
|
|
|
/// ident of self
|
2014-03-27 15:39:48 -07:00
|
|
|
pub type_ident: Ident,
|
2013-05-07 01:23:51 +10:00
|
|
|
/// ident of the method
|
2014-03-27 15:39:48 -07:00
|
|
|
pub method_ident: Ident,
|
2014-09-17 15:02:26 +02:00
|
|
|
/// dereferenced access to any `Self` or `Ptr(Self, _)` arguments
|
2014-09-13 19:06:01 +03:00
|
|
|
pub self_args: &'a [P<Expr>],
|
2013-05-07 01:23:51 +10:00
|
|
|
/// verbatim access to any other arguments
|
2014-09-13 19:06:01 +03:00
|
|
|
pub nonself_args: &'a [P<Expr>],
|
2014-03-27 15:39:48 -07:00
|
|
|
pub fields: &'a SubstructureFields<'a>
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2013-11-07 18:49:01 +11:00
|
|
|
/// Summary of the relevant parts of a struct/enum field.
|
|
|
|
pub struct FieldInfo {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub span: Span,
|
2013-11-07 18:49:01 +11:00
|
|
|
/// None for tuple structs/normal enum variants, Some for normal
|
|
|
|
/// structs/struct enum variants.
|
2014-03-27 15:39:48 -07:00
|
|
|
pub name: Option<Ident>,
|
2013-11-07 18:49:01 +11:00
|
|
|
/// The expression corresponding to this field of `self`
|
|
|
|
/// (specifically, a reference to it).
|
2014-09-13 19:06:01 +03:00
|
|
|
pub self_: P<Expr>,
|
2013-11-07 18:49:01 +11:00
|
|
|
/// The expressions corresponding to references to this field in
|
2014-09-17 15:02:26 +02:00
|
|
|
/// the other `Self` arguments.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub other: Vec<P<Expr>>,
|
2014-03-27 15:39:48 -07:00
|
|
|
}
|
2013-11-07 18:49:01 +11:00
|
|
|
|
|
|
|
/// Fields for a static method
|
|
|
|
pub enum StaticFields {
|
2014-09-17 15:12:58 +02:00
|
|
|
/// Tuple structs/enum variants like this.
|
2014-05-16 00:16:13 -07:00
|
|
|
Unnamed(Vec<Span>),
|
2013-11-07 18:49:01 +11:00
|
|
|
/// Normal structs/struct variants.
|
2014-05-16 00:16:13 -07:00
|
|
|
Named(Vec<(Ident, Span)>),
|
2013-11-07 18:49:01 +11:00
|
|
|
}
|
|
|
|
|
2014-09-17 15:12:58 +02:00
|
|
|
/// A summary of the possible sets of fields.
|
2013-12-09 23:16:18 -08:00
|
|
|
pub enum SubstructureFields<'a> {
|
2014-05-16 00:16:13 -07:00
|
|
|
Struct(Vec<FieldInfo>),
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Matching variants of the enum: variant index, ast::Variant,
|
|
|
|
/// fields: the field name is only non-`None` in the case of a struct
|
|
|
|
/// variant.
|
2015-01-17 23:33:05 +00:00
|
|
|
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo>),
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-09-17 15:02:26 +02:00
|
|
|
/// Non-matching variants of the enum, but with all state hidden from
|
|
|
|
/// the consequent code. The first component holds `Ident`s for all of
|
|
|
|
/// the `Self` arguments; the second component is a slice of all of the
|
2014-09-16 13:27:34 +02:00
|
|
|
/// variants for the enum itself, and the third component is a list of
|
2014-09-17 15:02:26 +02:00
|
|
|
/// `Ident`s bound to the variant index values for each of the actual
|
|
|
|
/// input `Self` arguments.
|
2014-09-13 19:06:01 +03:00
|
|
|
EnumNonMatchingCollapsed(Vec<Ident>, &'a [P<ast::Variant>], &'a [Ident]),
|
2014-07-06 21:19:12 +02:00
|
|
|
|
2014-09-17 15:02:26 +02:00
|
|
|
/// A static method where `Self` is a struct.
|
2014-01-09 15:05:33 +02:00
|
|
|
StaticStruct(&'a ast::StructDef, StaticFields),
|
2014-09-17 15:02:26 +02:00
|
|
|
/// A static method where `Self` is an enum.
|
2014-05-16 00:16:13 -07:00
|
|
|
StaticEnum(&'a ast::EnumDef, Vec<(Ident, Span, StaticFields)>),
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Combine the values of all the fields together. The last argument is
|
2014-09-17 15:12:58 +02:00
|
|
|
/// all the fields of all the structures.
|
2013-12-09 23:16:18 -08:00
|
|
|
pub type CombineSubstructureFunc<'a> =
|
2015-01-01 18:32:49 -05:00
|
|
|
Box<FnMut(&mut ExtCtxt, Span, &Substructure) -> P<Expr> + 'a>;
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Deal with non-matching enum variants. The tuple is a list of
|
2014-09-17 15:02:26 +02:00
|
|
|
/// identifiers (one for each `Self` argument, which could be any of the
|
2014-09-16 13:27:34 +02:00
|
|
|
/// variants since they have been collapsed together) and the identifiers
|
2014-09-17 15:02:26 +02:00
|
|
|
/// holding the variant index value for each of the `Self` arguments. The
|
|
|
|
/// last argument is all the non-`Self` args of the method being derived.
|
2014-07-06 21:19:12 +02:00
|
|
|
pub type EnumNonMatchCollapsedFunc<'a> =
|
2015-01-01 18:32:49 -05:00
|
|
|
Box<FnMut(&mut ExtCtxt, Span, (&[Ident], &[Ident]), &[P<Expr>]) -> P<Expr> + 'a>;
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-04-21 23:25:18 -07:00
|
|
|
pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>)
|
|
|
|
-> RefCell<CombineSubstructureFunc<'a>> {
|
|
|
|
RefCell::new(f)
|
|
|
|
}
|
|
|
|
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> TraitDef<'a> {
|
2014-12-08 13:28:32 -05:00
|
|
|
pub fn expand<F>(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
mitem: &ast::MetaItem,
|
|
|
|
item: &ast::Item,
|
|
|
|
push: F) where
|
|
|
|
F: FnOnce(P<ast::Item>),
|
|
|
|
{
|
2014-05-11 01:11:33 -07:00
|
|
|
let newitem = match item.node {
|
2014-05-16 00:16:13 -07:00
|
|
|
ast::ItemStruct(ref struct_def, ref generics) => {
|
2014-05-11 01:11:33 -07:00
|
|
|
self.expand_struct_def(cx,
|
2014-05-16 00:16:13 -07:00
|
|
|
&**struct_def,
|
2014-05-11 01:11:33 -07:00
|
|
|
item.ident,
|
|
|
|
generics)
|
2013-06-07 17:46:44 +10:00
|
|
|
}
|
2014-02-12 23:53:52 -08:00
|
|
|
ast::ItemEnum(ref enum_def, ref generics) => {
|
2014-05-11 01:11:33 -07:00
|
|
|
self.expand_enum_def(cx,
|
|
|
|
enum_def,
|
|
|
|
item.ident,
|
|
|
|
generics)
|
2014-02-12 23:53:52 -08:00
|
|
|
}
|
2014-11-18 14:02:40 -08:00
|
|
|
_ => {
|
2014-12-31 17:25:18 +13:00
|
|
|
cx.span_err(mitem.span, "`derive` may only be applied to structs and enums");
|
2014-11-18 14:02:40 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-05-11 01:11:33 -07:00
|
|
|
};
|
|
|
|
// Keep the lint attributes of the previous item to control how the
|
|
|
|
// generated implementations are linted
|
|
|
|
let mut attrs = newitem.attrs.clone();
|
|
|
|
attrs.extend(item.attrs.iter().filter(|a| {
|
|
|
|
match a.name().get() {
|
|
|
|
"allow" | "warn" | "deny" | "forbid" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}).map(|a| a.clone()));
|
2014-09-13 19:06:01 +03:00
|
|
|
push(P(ast::Item {
|
2014-05-11 01:11:33 -07:00
|
|
|
attrs: attrs,
|
|
|
|
..(*newitem).clone()
|
2014-09-13 19:06:01 +03:00
|
|
|
}))
|
2013-06-07 17:46:44 +10:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Given that we are deriving a trait `Tr` for a type `T<'a, ...,
|
|
|
|
/// 'z, A, ..., Z>`, creates an impl like:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
2014-09-17 15:02:26 +02:00
|
|
|
/// impl<'a, ..., 'z, A:Tr B1 B2, ..., Z: Tr B1 B2> Tr for T<A, ..., Z> { ... }
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// where B1, B2, ... are the bounds given by `bounds_paths`.'
|
2013-12-07 11:57:44 +11:00
|
|
|
fn create_derived_impl(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2014-02-14 08:09:51 -08:00
|
|
|
type_ident: Ident,
|
|
|
|
generics: &Generics,
|
2014-09-13 19:06:01 +03:00
|
|
|
methods: Vec<P<ast::Method>>) -> P<ast::Item> {
|
2013-12-07 11:57:44 +11:00
|
|
|
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2015-01-25 00:29:24 -05:00
|
|
|
// Transform associated types from `deriving::ty::Ty` into `ast::Typedef`
|
|
|
|
let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
|
|
|
|
P(ast::Typedef {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: self.span,
|
|
|
|
ident: ident,
|
|
|
|
vis: ast::Inherited,
|
|
|
|
attrs: Vec::new(),
|
|
|
|
typ: type_def.to_ty(cx,
|
|
|
|
self.span,
|
|
|
|
type_ident,
|
|
|
|
generics
|
|
|
|
),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2014-12-14 00:35:35 -05:00
|
|
|
let Generics { mut lifetimes, ty_params, mut where_clause } =
|
2014-03-19 23:16:56 +11:00
|
|
|
self.generics.to_generics(cx, self.span, type_ident, generics);
|
2014-03-20 01:52:37 +11:00
|
|
|
let mut ty_params = ty_params.into_vec();
|
2014-03-19 23:16:56 +11:00
|
|
|
|
2013-06-07 17:30:38 +10:00
|
|
|
// Copy the lifetimes
|
2014-08-05 22:59:24 -04:00
|
|
|
lifetimes.extend(generics.lifetimes.iter().map(|l| (*l).clone()));
|
2014-03-19 23:16:56 +11:00
|
|
|
|
2013-06-07 17:30:38 +10:00
|
|
|
// Create the type parameters.
|
2014-03-20 14:12:56 +01:00
|
|
|
ty_params.extend(generics.ty_params.iter().map(|ty_param| {
|
2013-06-07 17:30:38 +10:00
|
|
|
// I don't think this can be moved out of the loop, since
|
|
|
|
// a TyParamBound requires an ast id
|
2014-03-28 20:42:34 +01:00
|
|
|
let mut bounds: Vec<_> =
|
2013-06-07 17:30:38 +10:00
|
|
|
// extra restrictions on the generics parameters to the type being derived upon
|
2014-03-28 20:42:34 +01:00
|
|
|
self.additional_bounds.iter().map(|p| {
|
2014-02-08 19:39:53 -05:00
|
|
|
cx.typarambound(p.to_path(cx, self.span,
|
|
|
|
type_ident, generics))
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2013-06-07 17:30:38 +10:00
|
|
|
// require the current trait
|
2013-07-02 12:47:32 -07:00
|
|
|
bounds.push(cx.typarambound(trait_path.clone()));
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
// also add in any bounds from the declaration
|
2015-01-31 12:20:46 -05:00
|
|
|
for declared_bound in &*ty_param.bounds {
|
2014-08-27 21:46:52 -04:00
|
|
|
bounds.push((*declared_bound).clone());
|
|
|
|
}
|
|
|
|
|
2014-04-03 13:38:45 +13:00
|
|
|
cx.typaram(self.span,
|
|
|
|
ty_param.ident,
|
|
|
|
OwnedSlice::from_vec(bounds),
|
|
|
|
None)
|
2014-03-19 23:16:56 +11:00
|
|
|
}));
|
2014-12-14 00:35:35 -05:00
|
|
|
|
|
|
|
// and similarly for where clauses
|
|
|
|
where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
|
|
|
|
match *clause {
|
|
|
|
ast::WherePredicate::BoundPredicate(ref wb) => {
|
|
|
|
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
|
|
|
|
span: self.span,
|
2014-12-20 02:29:19 -08:00
|
|
|
bounded_ty: wb.bounded_ty.clone(),
|
2014-12-14 00:35:35 -05:00
|
|
|
bounds: OwnedSlice::from_vec(wb.bounds.iter().map(|b| b.clone()).collect())
|
|
|
|
})
|
|
|
|
}
|
2014-12-20 02:29:19 -08:00
|
|
|
ast::WherePredicate::RegionPredicate(ref rb) => {
|
|
|
|
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
|
|
|
|
span: self.span,
|
|
|
|
lifetime: rb.lifetime,
|
2014-12-20 02:48:43 -08:00
|
|
|
bounds: rb.bounds.iter().map(|b| b.clone()).collect()
|
2014-12-20 02:29:19 -08:00
|
|
|
})
|
|
|
|
}
|
2014-12-14 00:35:35 -05:00
|
|
|
ast::WherePredicate::EqPredicate(ref we) => {
|
|
|
|
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: self.span,
|
|
|
|
path: we.path.clone(),
|
|
|
|
ty: we.ty.clone()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2014-03-19 23:16:56 +11:00
|
|
|
let trait_generics = Generics {
|
|
|
|
lifetimes: lifetimes,
|
2014-08-11 09:32:26 -07:00
|
|
|
ty_params: OwnedSlice::from_vec(ty_params),
|
2014-12-14 00:35:35 -05:00
|
|
|
where_clause: where_clause
|
2014-03-19 23:16:56 +11:00
|
|
|
};
|
2013-06-07 17:30:38 +10:00
|
|
|
|
|
|
|
// Create the reference to the trait.
|
|
|
|
let trait_ref = cx.trait_ref(trait_path);
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-06-07 17:30:38 +10:00
|
|
|
// Create the type parameters on the `self` path.
|
2013-11-20 16:23:04 -08:00
|
|
|
let self_ty_params = generics.ty_params.map(|ty_param| {
|
2013-12-07 11:57:44 +11:00
|
|
|
cx.ty_ident(self.span, ty_param.ident)
|
2013-11-20 16:23:04 -08:00
|
|
|
});
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-08-05 22:59:24 -04:00
|
|
|
let self_lifetimes: Vec<ast::Lifetime> =
|
|
|
|
generics.lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(|ld| ld.lifetime)
|
|
|
|
.collect();
|
2013-06-07 17:30:38 +10:00
|
|
|
|
|
|
|
// Create the type of `self`.
|
2013-12-07 11:57:44 +11:00
|
|
|
let self_type = cx.ty_path(
|
2014-02-28 13:09:09 -08:00
|
|
|
cx.path_all(self.span, false, vec!( type_ident ), self_lifetimes,
|
2014-11-29 17:08:30 +13:00
|
|
|
self_ty_params.into_vec(), Vec::new()));
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-03-15 22:49:41 -07:00
|
|
|
let attr = cx.attribute(
|
2013-12-07 11:57:44 +11:00
|
|
|
self.span,
|
2014-03-15 22:49:41 -07:00
|
|
|
cx.meta_word(self.span,
|
|
|
|
InternedString::new("automatically_derived")));
|
2014-05-23 08:39:26 -07:00
|
|
|
// Just mark it now since we know that it'll end up used downstream
|
|
|
|
attr::mark_used(&attr);
|
2014-02-14 07:07:09 +02:00
|
|
|
let opt_trait_ref = Some(trait_ref);
|
2014-05-16 00:16:13 -07:00
|
|
|
let ident = ast_util::impl_pretty_name(&opt_trait_ref, &*self_type);
|
2014-10-14 23:05:01 -07:00
|
|
|
let mut a = vec![attr];
|
|
|
|
a.extend(self.attributes.iter().map(|a| a.clone()));
|
2013-06-07 17:30:38 +10:00
|
|
|
cx.item(
|
2013-12-07 11:57:44 +11:00
|
|
|
self.span,
|
2014-02-14 07:07:09 +02:00
|
|
|
ident,
|
2014-10-14 23:05:01 -07:00
|
|
|
a,
|
2014-12-10 06:15:06 -05:00
|
|
|
ast::ItemImpl(ast::Unsafety::Normal,
|
2014-12-28 23:33:18 +01:00
|
|
|
ast::ImplPolarity::Positive,
|
2014-12-10 06:15:06 -05:00
|
|
|
trait_generics,
|
2014-08-04 13:56:56 -07:00
|
|
|
opt_trait_ref,
|
|
|
|
self_type,
|
2014-09-14 20:27:36 -07:00
|
|
|
methods.into_iter()
|
2014-08-04 13:56:56 -07:00
|
|
|
.map(|method| {
|
|
|
|
ast::MethodImplItem(method)
|
2015-01-25 00:29:24 -05:00
|
|
|
}).chain(
|
|
|
|
associated_types.map(|type_| {
|
|
|
|
ast::TypeImplItem(type_)
|
|
|
|
})
|
|
|
|
).collect()))
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-02-21 21:33:23 -08:00
|
|
|
fn expand_struct_def(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
struct_def: &StructDef,
|
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
generics: &Generics) -> P<ast::Item> {
|
2014-03-28 20:42:34 +01:00
|
|
|
let methods = self.methods.iter().map(|method_def| {
|
2013-04-30 08:49:48 -07:00
|
|
|
let (explicit_self, self_args, nonself_args, tys) =
|
2014-02-08 19:39:53 -05:00
|
|
|
method_def.split_self_nonself_args(
|
|
|
|
cx, self, type_ident, generics);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
let body = if method_def.is_static() {
|
|
|
|
method_def.expand_static_struct_method_body(
|
2014-02-08 19:39:53 -05:00
|
|
|
cx,
|
2013-12-07 11:57:44 +11:00
|
|
|
self,
|
2013-05-07 01:23:51 +10:00
|
|
|
struct_def,
|
|
|
|
type_ident,
|
2015-01-07 11:58:31 -05:00
|
|
|
&self_args[],
|
|
|
|
&nonself_args[])
|
2013-03-28 21:50:10 +11:00
|
|
|
} else {
|
2014-02-08 19:39:53 -05:00
|
|
|
method_def.expand_struct_method_body(cx,
|
|
|
|
self,
|
2013-03-28 21:50:10 +11:00
|
|
|
struct_def,
|
2013-05-07 01:23:51 +10:00
|
|
|
type_ident,
|
2015-01-07 11:58:31 -05:00
|
|
|
&self_args[],
|
|
|
|
&nonself_args[])
|
2013-03-28 21:50:10 +11:00
|
|
|
};
|
|
|
|
|
2014-05-28 22:26:56 -07:00
|
|
|
method_def.create_method(cx,
|
|
|
|
self,
|
|
|
|
type_ident,
|
|
|
|
generics,
|
|
|
|
abi::Rust,
|
|
|
|
explicit_self,
|
|
|
|
tys,
|
2013-05-07 01:23:51 +10:00
|
|
|
body)
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
self.create_derived_impl(cx, type_ident, generics, methods)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-02-21 21:33:23 -08:00
|
|
|
fn expand_enum_def(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2014-01-09 15:05:33 +02:00
|
|
|
enum_def: &EnumDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
generics: &Generics) -> P<ast::Item> {
|
2014-03-28 20:42:34 +01:00
|
|
|
let methods = self.methods.iter().map(|method_def| {
|
2013-04-30 08:49:48 -07:00
|
|
|
let (explicit_self, self_args, nonself_args, tys) =
|
2014-02-08 19:39:53 -05:00
|
|
|
method_def.split_self_nonself_args(cx, self,
|
|
|
|
type_ident, generics);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
let body = if method_def.is_static() {
|
|
|
|
method_def.expand_static_enum_method_body(
|
2014-02-08 19:39:53 -05:00
|
|
|
cx,
|
2013-12-07 11:57:44 +11:00
|
|
|
self,
|
2013-05-07 01:23:51 +10:00
|
|
|
enum_def,
|
|
|
|
type_ident,
|
2015-01-07 11:58:31 -05:00
|
|
|
&self_args[],
|
|
|
|
&nonself_args[])
|
2013-05-07 01:23:51 +10:00
|
|
|
} else {
|
2014-02-08 19:39:53 -05:00
|
|
|
method_def.expand_enum_method_body(cx,
|
|
|
|
self,
|
2013-05-07 01:23:51 +10:00
|
|
|
enum_def,
|
|
|
|
type_ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args,
|
2015-01-07 11:58:31 -05:00
|
|
|
&nonself_args[])
|
2013-05-07 01:23:51 +10:00
|
|
|
};
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-05-28 22:26:56 -07:00
|
|
|
method_def.create_method(cx,
|
|
|
|
self,
|
|
|
|
type_ident,
|
|
|
|
generics,
|
|
|
|
abi::Rust,
|
|
|
|
explicit_self,
|
|
|
|
tys,
|
2013-05-07 01:23:51 +10:00
|
|
|
body)
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
self.create_derived_impl(cx, type_ident, generics, methods)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
fn variant_to_pat(cx: &mut ExtCtxt, sp: Span, enum_ident: ast::Ident, variant: &ast::Variant)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> P<ast::Pat> {
|
2014-11-06 00:05:53 -08:00
|
|
|
let path = cx.path(sp, vec![enum_ident, variant.node.name]);
|
2014-07-06 21:19:12 +02:00
|
|
|
cx.pat(sp, match variant.node.kind {
|
2014-11-06 00:05:53 -08:00
|
|
|
ast::TupleVariantKind(..) => ast::PatEnum(path, None),
|
|
|
|
ast::StructVariantKind(..) => ast::PatStruct(path, Vec::new(), true),
|
2014-07-06 21:19:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> MethodDef<'a> {
|
2013-03-28 21:50:10 +11:00
|
|
|
fn call_substructure_method(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_: &TraitDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: &[P<Expr>],
|
|
|
|
nonself_args: &[P<Expr>],
|
2013-03-28 21:50:10 +11:00
|
|
|
fields: &SubstructureFields)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> P<Expr> {
|
2013-03-28 21:50:10 +11:00
|
|
|
let substructure = Substructure {
|
|
|
|
type_ident: type_ident,
|
2014-02-08 19:39:53 -05:00
|
|
|
method_ident: cx.ident_of(self.name),
|
2013-05-07 01:23:51 +10:00
|
|
|
self_args: self_args,
|
|
|
|
nonself_args: nonself_args,
|
2013-03-28 21:50:10 +11:00
|
|
|
fields: fields
|
|
|
|
};
|
2014-04-21 23:25:18 -07:00
|
|
|
let mut f = self.combine_substructure.borrow_mut();
|
|
|
|
let f: &mut CombineSubstructureFunc = &mut *f;
|
2015-01-06 09:24:46 -08:00
|
|
|
f(cx, trait_.span, &substructure)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
fn get_ret_ty(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_: &TraitDef,
|
|
|
|
generics: &Generics,
|
|
|
|
type_ident: Ident)
|
|
|
|
-> P<ast::Ty> {
|
|
|
|
self.ret_ty.to_ty(cx, trait_.span, type_ident, generics)
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_static(&self) -> bool {
|
2013-04-30 08:49:48 -07:00
|
|
|
self.explicit_self.is_none()
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
fn split_self_nonself_args(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_: &TraitDef,
|
|
|
|
type_ident: Ident,
|
|
|
|
generics: &Generics)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> (ast::ExplicitSelf, Vec<P<Expr>>, Vec<P<Expr>>, Vec<(Ident, P<ast::Ty>)>) {
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut self_args = Vec::new();
|
|
|
|
let mut nonself_args = Vec::new();
|
|
|
|
let mut arg_tys = Vec::new();
|
2013-05-07 01:23:51 +10:00
|
|
|
let mut nonstatic = false;
|
|
|
|
|
2013-04-30 08:49:48 -07:00
|
|
|
let ast_explicit_self = match self.explicit_self {
|
2013-05-12 00:25:31 -04:00
|
|
|
Some(ref self_ptr) => {
|
2013-12-07 11:57:44 +11:00
|
|
|
let (self_expr, explicit_self) =
|
2014-02-08 19:39:53 -05:00
|
|
|
ty::get_explicit_self(cx, trait_.span, self_ptr);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
self_args.push(self_expr);
|
|
|
|
nonstatic = true;
|
2013-04-30 08:49:48 -07:00
|
|
|
|
|
|
|
explicit_self
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
2014-01-09 15:05:33 +02:00
|
|
|
None => codemap::respan(trait_.span, ast::SelfStatic),
|
2013-04-30 08:49:48 -07:00
|
|
|
};
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for (i, ty) in self.args.iter().enumerate() {
|
2014-02-08 19:39:53 -05:00
|
|
|
let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics);
|
2015-01-07 11:58:31 -05:00
|
|
|
let ident = cx.ident_of(&format!("__arg_{}", i)[]);
|
2013-05-07 01:23:51 +10:00
|
|
|
arg_tys.push((ident, ast_ty));
|
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
let arg_expr = cx.expr_ident(trait_.span, ident);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
|
|
|
match *ty {
|
|
|
|
// for static methods, just treat any Self
|
|
|
|
// arguments as a normal arg
|
|
|
|
Self if nonstatic => {
|
|
|
|
self_args.push(arg_expr);
|
|
|
|
}
|
2014-05-05 18:56:44 -07:00
|
|
|
Ptr(box Self, _) if nonstatic => {
|
2014-02-08 19:39:53 -05:00
|
|
|
self_args.push(cx.expr_deref(trait_.span, arg_expr))
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
nonself_args.push(arg_expr);
|
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-04-30 08:49:48 -07:00
|
|
|
(ast_explicit_self, self_args, nonself_args, arg_tys)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
fn create_method(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_: &TraitDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2013-05-07 01:23:51 +10:00
|
|
|
generics: &Generics,
|
2014-05-28 22:26:56 -07:00
|
|
|
abi: Abi,
|
2014-01-09 15:05:33 +02:00
|
|
|
explicit_self: ast::ExplicitSelf,
|
2014-02-28 13:09:09 -08:00
|
|
|
arg_types: Vec<(Ident, P<ast::Ty>)> ,
|
2014-09-13 19:06:01 +03:00
|
|
|
body: P<Expr>) -> P<ast::Method> {
|
2013-05-07 01:23:51 +10:00
|
|
|
// create the generics that aren't for Self
|
2014-02-08 19:39:53 -05:00
|
|
|
let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics);
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
let self_arg = match explicit_self.node {
|
|
|
|
ast::SelfStatic => None,
|
2014-07-06 15:10:57 -07:00
|
|
|
// creating fresh self id
|
|
|
|
_ => Some(ast::Arg::new_self(trait_.span, ast::MutImmutable, special_idents::self_))
|
2014-01-27 14:18:36 +02:00
|
|
|
};
|
2014-02-09 07:37:33 -05:00
|
|
|
let args = {
|
2014-09-14 20:27:36 -07:00
|
|
|
let args = arg_types.into_iter().map(|(name, ty)| {
|
2014-02-09 07:37:33 -05:00
|
|
|
cx.arg(trait_.span, name, ty)
|
|
|
|
});
|
2014-09-14 20:27:36 -07:00
|
|
|
self_arg.into_iter().chain(args).collect()
|
2014-02-09 07:37:33 -05:00
|
|
|
};
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident);
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
let method_ident = cx.ident_of(self.name);
|
|
|
|
let fn_decl = cx.fn_decl(args, ret_type);
|
|
|
|
let body_block = cx.block_expr(body);
|
2013-03-28 21:50:10 +11:00
|
|
|
|
|
|
|
// Create the method.
|
2014-09-13 19:06:01 +03:00
|
|
|
P(ast::Method {
|
2014-04-23 22:43:45 +08:00
|
|
|
attrs: self.attributes.clone(),
|
2013-09-06 22:11:55 -04:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-12-07 11:57:44 +11:00
|
|
|
span: trait_.span,
|
2014-07-11 21:22:11 -07:00
|
|
|
node: ast::MethDecl(method_ident,
|
|
|
|
fn_generics,
|
2014-05-28 22:26:56 -07:00
|
|
|
abi,
|
2014-07-11 21:22:11 -07:00
|
|
|
explicit_self,
|
2014-12-09 10:36:46 -05:00
|
|
|
ast::Unsafety::Normal,
|
2014-07-11 21:22:11 -07:00
|
|
|
fn_decl,
|
|
|
|
body_block,
|
|
|
|
ast::Inherited)
|
2014-09-13 19:06:01 +03:00
|
|
|
})
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
2014-12-31 17:25:18 +13:00
|
|
|
/// #[derive(PartialEq)]
|
2015-01-17 23:55:21 +00:00
|
|
|
/// struct A { x: i32, y: i32 }
|
2014-09-16 13:27:34 +02:00
|
|
|
///
|
|
|
|
/// // equivalent to:
|
|
|
|
/// impl PartialEq for A {
|
|
|
|
/// fn eq(&self, __arg_1: &A) -> bool {
|
|
|
|
/// match *self {
|
|
|
|
/// A {x: ref __self_0_0, y: ref __self_0_1} => {
|
|
|
|
/// match *__arg_1 {
|
|
|
|
/// A {x: ref __self_1_0, y: ref __self_1_1} => {
|
|
|
|
/// __self_0_0.eq(__self_1_0) && __self_0_1.eq(__self_1_1)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2013-05-07 01:23:51 +10:00
|
|
|
fn expand_struct_method_body(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_: &TraitDef,
|
2014-01-09 15:05:33 +02:00
|
|
|
struct_def: &StructDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: &[P<Expr>],
|
|
|
|
nonself_args: &[P<Expr>])
|
|
|
|
-> P<Expr> {
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut raw_fields = Vec::new(); // ~[[fields of self],
|
2013-06-04 21:43:41 -07:00
|
|
|
// [fields of next Self arg], [etc]]
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut patterns = Vec::new();
|
2015-01-28 01:01:48 +00:00
|
|
|
for i in 0..self_args.len() {
|
2014-11-06 00:05:53 -08:00
|
|
|
let struct_path= cx.path(DUMMY_SP, vec!( type_ident ));
|
2014-05-16 10:45:16 -07:00
|
|
|
let (pat, ident_expr) =
|
|
|
|
trait_.create_struct_pattern(cx,
|
2014-11-06 00:05:53 -08:00
|
|
|
struct_path,
|
2014-05-16 10:45:16 -07:00
|
|
|
struct_def,
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("__self_{}",
|
|
|
|
i)[],
|
2014-05-16 10:45:16 -07:00
|
|
|
ast::MutImmutable);
|
2013-05-07 01:23:51 +10:00
|
|
|
patterns.push(pat);
|
|
|
|
raw_fields.push(ident_expr);
|
2013-08-01 18:35:46 -04:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
// transpose raw_fields
|
2014-02-13 09:46:46 -08:00
|
|
|
let fields = if raw_fields.len() > 0 {
|
2014-09-14 20:27:36 -07:00
|
|
|
let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter());
|
2014-09-13 19:06:01 +03:00
|
|
|
let first_field = raw_fields.next().unwrap();
|
2014-12-19 21:52:10 +01:00
|
|
|
let mut other_fields: Vec<vec::IntoIter<(Span, Option<Ident>, P<Expr>)>>
|
2014-09-13 19:06:01 +03:00
|
|
|
= raw_fields.collect();
|
|
|
|
first_field.map(|(span, opt_id, field)| {
|
2014-02-13 09:46:46 -08:00
|
|
|
FieldInfo {
|
|
|
|
span: span,
|
|
|
|
name: opt_id,
|
|
|
|
self_: field,
|
2014-09-14 20:27:36 -07:00
|
|
|
other: other_fields.iter_mut().map(|l| {
|
2014-09-13 19:06:01 +03:00
|
|
|
match l.next().unwrap() {
|
|
|
|
(_, _, ex) => ex
|
|
|
|
}
|
|
|
|
}).collect()
|
2014-02-13 09:46:46 -08:00
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
} else {
|
|
|
|
cx.span_bug(trait_.span,
|
|
|
|
"no self arguments to non-static method in generic \
|
2014-12-31 17:25:18 +13:00
|
|
|
`derive`")
|
2013-05-07 01:23:51 +10:00
|
|
|
};
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
// body of the inner most destructuring match
|
|
|
|
let mut body = self.call_substructure_method(
|
2014-02-08 19:39:53 -05:00
|
|
|
cx,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_,
|
2013-05-07 01:23:51 +10:00
|
|
|
type_ident,
|
|
|
|
self_args,
|
|
|
|
nonself_args,
|
|
|
|
&Struct(fields));
|
|
|
|
|
|
|
|
// make a series of nested matches, to destructure the
|
2014-06-09 00:00:52 -04:00
|
|
|
// structs. This is actually right-to-left, but it shouldn't
|
2013-05-07 01:23:51 +10:00
|
|
|
// matter.
|
2014-09-13 19:06:01 +03:00
|
|
|
for (arg_expr, pat) in self_args.iter().zip(patterns.iter()) {
|
|
|
|
body = cx.expr_match(trait_.span, arg_expr.clone(),
|
|
|
|
vec!( cx.arm(trait_.span, vec!(pat.clone()), body) ))
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
2013-05-07 01:23:51 +10:00
|
|
|
body
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
fn expand_static_struct_method_body(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_: &TraitDef,
|
2014-01-09 15:05:33 +02:00
|
|
|
struct_def: &StructDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: &[P<Expr>],
|
|
|
|
nonself_args: &[P<Expr>])
|
|
|
|
-> P<Expr> {
|
2014-02-08 19:39:53 -05:00
|
|
|
let summary = trait_.summarise_struct(cx, struct_def);
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
self.call_substructure_method(cx,
|
|
|
|
trait_,
|
2013-05-07 01:23:51 +10:00
|
|
|
type_ident,
|
|
|
|
self_args, nonself_args,
|
|
|
|
&StaticStruct(struct_def, summary))
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
2014-12-31 17:25:18 +13:00
|
|
|
/// #[derive(PartialEq)]
|
2014-09-16 13:27:34 +02:00
|
|
|
/// enum A {
|
|
|
|
/// A1,
|
2015-01-17 23:55:21 +00:00
|
|
|
/// A2(i32)
|
2014-09-16 13:27:34 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // is equivalent to
|
|
|
|
///
|
|
|
|
/// impl PartialEq for A {
|
|
|
|
/// fn eq(&self, __arg_1: &A) -> ::bool {
|
|
|
|
/// match (&*self, &*__arg_1) {
|
|
|
|
/// (&A1, &A1) => true,
|
|
|
|
/// (&A2(ref __self_0),
|
|
|
|
/// &A2(ref __arg_1_0)) => (*__self_0).eq(&(*__arg_1_0)),
|
|
|
|
/// _ => {
|
2015-01-28 01:01:48 +00:00
|
|
|
/// let __self_vi = match *self { A1(..) => 0, A2(..) => 1 };
|
|
|
|
/// let __arg_1_vi = match *__arg_1 { A1(..) => 0, A2(..) => 1 };
|
2014-09-16 13:27:34 +02:00
|
|
|
/// false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// (Of course `__self_vi` and `__arg_1_vi` are unused for
|
|
|
|
/// `PartialEq`, and those subcomputations will hopefully be removed
|
|
|
|
/// as their results are unused. The point of `__self_vi` and
|
|
|
|
/// `__arg_1_vi` is for `PartialOrd`; see #15503.)
|
2013-03-28 21:50:10 +11:00
|
|
|
fn expand_enum_method_body(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_: &TraitDef,
|
2014-01-09 15:05:33 +02:00
|
|
|
enum_def: &EnumDef,
|
2013-09-02 02:50:59 +02:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: Vec<P<Expr>>,
|
|
|
|
nonself_args: &[P<Expr>])
|
|
|
|
-> P<Expr> {
|
2014-07-07 09:13:49 +02:00
|
|
|
self.build_enum_match_tuple(
|
|
|
|
cx, trait_, enum_def, type_ident, self_args, nonself_args)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Creates a match for a tuple of all `self_args`, where either all
|
|
|
|
/// variants match, or it falls into a catch-all for when one variant
|
|
|
|
/// does not match.
|
|
|
|
|
|
|
|
/// There are N + 1 cases because is a case for each of the N
|
|
|
|
/// variants where all of the variants match, and one catch-all for
|
|
|
|
/// when one does not match.
|
|
|
|
|
|
|
|
/// The catch-all handler is provided access the variant index values
|
|
|
|
/// for each of the self-args, carried in precomputed variables. (Nota
|
|
|
|
/// bene: the variant index values are not necessarily the
|
|
|
|
/// discriminant values. See issue #15523.)
|
|
|
|
|
|
|
|
/// ```{.text}
|
|
|
|
/// match (this, that, ...) {
|
|
|
|
/// (Variant1, Variant1, Variant1) => ... // delegate Matching on Variant1
|
|
|
|
/// (Variant2, Variant2, Variant2) => ... // delegate Matching on Variant2
|
|
|
|
/// ...
|
|
|
|
/// _ => {
|
2015-01-28 01:01:48 +00:00
|
|
|
/// let __this_vi = match this { Variant1 => 0, Variant2 => 1, ... };
|
|
|
|
/// let __that_vi = match that { Variant1 => 0, Variant2 => 1, ... };
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ... // catch-all remainder can inspect above variant index values.
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-07-06 21:19:12 +02:00
|
|
|
fn build_enum_match_tuple(
|
|
|
|
&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_: &TraitDef,
|
|
|
|
enum_def: &EnumDef,
|
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: Vec<P<Expr>>,
|
|
|
|
nonself_args: &[P<Expr>]) -> P<Expr> {
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
let sp = trait_.span;
|
|
|
|
let variants = &enum_def.variants;
|
|
|
|
|
|
|
|
let self_arg_names = self_args.iter().enumerate()
|
|
|
|
.map(|(arg_count, _self_arg)| {
|
|
|
|
if arg_count == 0 {
|
|
|
|
"__self".to_string()
|
|
|
|
} else {
|
|
|
|
format!("__arg_{}", arg_count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
let self_arg_idents = self_arg_names.iter()
|
2015-01-07 11:58:31 -05:00
|
|
|
.map(|name|cx.ident_of(&name[]))
|
2014-07-06 21:19:12 +02:00
|
|
|
.collect::<Vec<ast::Ident>>();
|
|
|
|
|
|
|
|
// The `vi_idents` will be bound, solely in the catch-all, to
|
2015-01-17 23:33:05 +00:00
|
|
|
// a series of let statements mapping each self_arg to a usize
|
2014-07-06 21:19:12 +02:00
|
|
|
// corresponding to its variant index.
|
2014-09-13 19:06:01 +03:00
|
|
|
let vi_idents: Vec<ast::Ident> = self_arg_names.iter()
|
2015-01-07 11:58:31 -05:00
|
|
|
.map(|name| { let vi_suffix = format!("{}_vi", &name[]);
|
|
|
|
cx.ident_of(&vi_suffix[]) })
|
2014-07-06 21:19:12 +02:00
|
|
|
.collect::<Vec<ast::Ident>>();
|
|
|
|
|
|
|
|
// Builds, via callback to call_substructure_method, the
|
|
|
|
// delegated expression that handles the catch-all case,
|
|
|
|
// using `__variants_tuple` to drive logic if necessary.
|
|
|
|
let catch_all_substructure = EnumNonMatchingCollapsed(
|
2015-01-07 11:58:31 -05:00
|
|
|
self_arg_idents, &variants[], &vi_idents[]);
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// These arms are of the form:
|
|
|
|
// (Variant1, Variant1, ...) => Body1
|
|
|
|
// (Variant2, Variant2, ...) => Body2
|
|
|
|
// ...
|
|
|
|
// where each tuple has length = self_args.len()
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut match_arms: Vec<ast::Arm> = variants.iter().enumerate()
|
|
|
|
.map(|(index, variant)| {
|
2014-12-30 21:02:53 -05:00
|
|
|
let mk_self_pat = |&: cx: &mut ExtCtxt, self_arg_name: &str| {
|
2014-11-06 00:05:53 -08:00
|
|
|
let (p, idents) = trait_.create_enum_variant_pattern(cx, type_ident,
|
|
|
|
&**variant,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_arg_name,
|
|
|
|
ast::MutImmutable);
|
2014-12-05 15:56:25 -08:00
|
|
|
(cx.pat(sp, ast::PatRegion(p, ast::MutImmutable)), idents)
|
2014-09-13 19:06:01 +03:00
|
|
|
};
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// A single arm has form (&VariantK, &VariantK, ...) => BodyK
|
|
|
|
// (see "Final wrinkle" note below for why.)
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut subpats = Vec::with_capacity(self_arg_names.len());
|
|
|
|
let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1);
|
|
|
|
let first_self_pat_idents = {
|
2015-01-07 11:58:31 -05:00
|
|
|
let (p, idents) = mk_self_pat(cx, &self_arg_names[0][]);
|
2014-09-13 19:06:01 +03:00
|
|
|
subpats.push(p);
|
|
|
|
idents
|
|
|
|
};
|
2015-01-31 12:20:46 -05:00
|
|
|
for self_arg_name in self_arg_names.tail() {
|
2015-01-07 11:58:31 -05:00
|
|
|
let (p, idents) = mk_self_pat(cx, &self_arg_name[]);
|
2014-09-13 19:06:01 +03:00
|
|
|
subpats.push(p);
|
|
|
|
self_pats_idents.push(idents);
|
|
|
|
}
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// Here is the pat = `(&VariantK, &VariantK, ...)`
|
2014-11-09 16:14:15 +01:00
|
|
|
let single_pat = cx.pat_tuple(sp, subpats);
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// For the BodyK, we need to delegate to our caller,
|
|
|
|
// passing it an EnumMatching to indicate which case
|
|
|
|
// we are in.
|
|
|
|
|
|
|
|
// All of the Self args have the same variant in these
|
2014-09-13 19:06:01 +03:00
|
|
|
// cases. So we transpose the info in self_pats_idents
|
|
|
|
// to gather the getter expressions together, in the
|
|
|
|
// form that EnumMatching expects.
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// The transposition is driven by walking across the
|
|
|
|
// arg fields of the variant for the first self pat.
|
2014-09-14 20:27:36 -07:00
|
|
|
let field_tuples = first_self_pat_idents.into_iter().enumerate()
|
2014-07-06 21:19:12 +02:00
|
|
|
// For each arg field of self, pull out its getter expr ...
|
2014-09-13 19:06:01 +03:00
|
|
|
.map(|(field_index, (sp, opt_ident, self_getter_expr))| {
|
2014-07-06 21:19:12 +02:00
|
|
|
// ... but FieldInfo also wants getter expr
|
|
|
|
// for matching other arguments of Self type;
|
2014-09-13 19:06:01 +03:00
|
|
|
// so walk across the *other* self_pats_idents
|
|
|
|
// and pull out getter for same field in each
|
|
|
|
// of them (using `field_index` tracked above).
|
2014-07-06 21:19:12 +02:00
|
|
|
// That is the heart of the transposition.
|
2014-09-13 19:06:01 +03:00
|
|
|
let others = self_pats_idents.iter().map(|fields| {
|
2014-10-14 23:05:01 -07:00
|
|
|
let (_, _opt_ident, ref other_getter_expr) =
|
|
|
|
fields[field_index];
|
2014-07-06 21:19:12 +02:00
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
// All Self args have same variant, so
|
|
|
|
// opt_idents are the same. (Assert
|
|
|
|
// here to make it self-evident that
|
|
|
|
// it is okay to ignore `_opt_ident`.)
|
|
|
|
assert!(opt_ident == _opt_ident);
|
2014-07-06 21:19:12 +02:00
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
other_getter_expr.clone()
|
|
|
|
}).collect::<Vec<P<Expr>>>();
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
FieldInfo { span: sp,
|
|
|
|
name: opt_ident,
|
|
|
|
self_: self_getter_expr,
|
|
|
|
other: others,
|
|
|
|
}
|
|
|
|
}).collect::<Vec<FieldInfo>>();
|
|
|
|
|
|
|
|
// Now, for some given VariantK, we have built up
|
|
|
|
// expressions for referencing every field of every
|
|
|
|
// Self arg, assuming all are instances of VariantK.
|
|
|
|
// Build up code associated with such a case.
|
2014-07-07 16:35:15 -07:00
|
|
|
let substructure = EnumMatching(index,
|
2014-09-13 19:06:01 +03:00
|
|
|
&**variant,
|
2014-07-07 16:35:15 -07:00
|
|
|
field_tuples);
|
2014-07-06 21:19:12 +02:00
|
|
|
let arm_expr = self.call_substructure_method(
|
2015-01-07 11:58:31 -05:00
|
|
|
cx, trait_, type_ident, &self_args[], nonself_args,
|
2014-07-06 21:19:12 +02:00
|
|
|
&substructure);
|
|
|
|
|
|
|
|
cx.arm(sp, vec![single_pat], arm_expr)
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// We will usually need the catch-all after matching the
|
|
|
|
// tuples `(VariantK, VariantK, ...)` for each VariantK of the
|
|
|
|
// enum. But:
|
|
|
|
//
|
|
|
|
// * when there is only one Self arg, the arms above suffice
|
|
|
|
// (and the deriving we call back into may not be prepared to
|
|
|
|
// handle EnumNonMatchCollapsed), and,
|
|
|
|
//
|
|
|
|
// * when the enum has only one variant, the single arm that
|
|
|
|
// is already present always suffices.
|
|
|
|
//
|
|
|
|
// * In either of the two cases above, if we *did* add a
|
|
|
|
// catch-all `_` match, it would trigger the
|
|
|
|
// unreachable-pattern error.
|
|
|
|
//
|
|
|
|
if variants.len() > 1 && self_args.len() > 1 {
|
2014-09-13 19:06:01 +03:00
|
|
|
let arms: Vec<ast::Arm> = variants.iter().enumerate()
|
|
|
|
.map(|(index, variant)| {
|
2014-11-06 00:05:53 -08:00
|
|
|
let pat = variant_to_pat(cx, sp, type_ident, &**variant);
|
2015-01-08 20:13:14 +11:00
|
|
|
let lit = ast::LitInt(index as u64, ast::UnsignedIntLit(ast::TyUs(false)));
|
2014-07-06 21:19:12 +02:00
|
|
|
cx.arm(sp, vec![pat], cx.expr_lit(sp, lit))
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// Build a series of let statements mapping each self_arg
|
2015-01-17 23:33:05 +00:00
|
|
|
// to a usize corresponding to its variant index.
|
2014-07-06 21:19:12 +02:00
|
|
|
// i.e. for `enum E<T> { A, B(1), C(T, T) }`, and a deriving
|
|
|
|
// with three Self args, builds three statements:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// let __self0_vi = match self {
|
2015-01-28 01:01:48 +00:00
|
|
|
// A => 0, B(..) => 1, C(..) => 2
|
2014-07-06 21:19:12 +02:00
|
|
|
// };
|
|
|
|
// let __self1_vi = match __arg1 {
|
2015-01-28 01:01:48 +00:00
|
|
|
// A => 0, B(..) => 1, C(..) => 2
|
2014-07-06 21:19:12 +02:00
|
|
|
// };
|
|
|
|
// let __self2_vi = match __arg2 {
|
2015-01-28 01:01:48 +00:00
|
|
|
// A => 0, B(..) => 1, C(..) => 2
|
2014-07-06 21:19:12 +02:00
|
|
|
// };
|
|
|
|
// ```
|
2014-09-13 19:06:01 +03:00
|
|
|
let mut index_let_stmts: Vec<P<ast::Stmt>> = Vec::new();
|
|
|
|
for (&ident, self_arg) in vi_idents.iter().zip(self_args.iter()) {
|
|
|
|
let variant_idx = cx.expr_match(sp, self_arg.clone(), arms.clone());
|
2014-07-06 21:19:12 +02:00
|
|
|
let let_stmt = cx.stmt_let(sp, false, ident, variant_idx);
|
|
|
|
index_let_stmts.push(let_stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
let arm_expr = self.call_substructure_method(
|
2015-01-07 11:58:31 -05:00
|
|
|
cx, trait_, type_ident, &self_args[], nonself_args,
|
2014-07-06 21:19:12 +02:00
|
|
|
&catch_all_substructure);
|
|
|
|
|
|
|
|
// Builds the expression:
|
|
|
|
// {
|
|
|
|
// let __self0_vi = ...;
|
|
|
|
// let __self1_vi = ...;
|
|
|
|
// ...
|
|
|
|
// <delegated expression referring to __self0_vi, et al.>
|
|
|
|
// }
|
|
|
|
let arm_expr = cx.expr_block(
|
2015-01-13 17:30:17 +02:00
|
|
|
cx.block_all(sp, index_let_stmts, Some(arm_expr)));
|
2014-07-06 21:19:12 +02:00
|
|
|
|
|
|
|
// Builds arm:
|
|
|
|
// _ => { let __self0_vi = ...;
|
|
|
|
// let __self1_vi = ...;
|
|
|
|
// ...
|
|
|
|
// <delegated expression as above> }
|
|
|
|
let catch_all_match_arm =
|
|
|
|
cx.arm(sp, vec![cx.pat_wild(sp)], arm_expr);
|
|
|
|
|
|
|
|
match_arms.push(catch_all_match_arm);
|
|
|
|
|
|
|
|
} else if variants.len() == 0 {
|
|
|
|
// As an additional wrinkle, For a zero-variant enum A,
|
|
|
|
// currently the compiler
|
|
|
|
// will accept `fn (a: &Self) { match *a { } }`
|
|
|
|
// but rejects `fn (a: &Self) { match (&*a,) { } }`
|
|
|
|
// as well as `fn (a: &Self) { match ( *a,) { } }`
|
|
|
|
//
|
|
|
|
// This means that the strategy of building up a tuple of
|
|
|
|
// all Self arguments fails when Self is a zero variant
|
|
|
|
// enum: rustc rejects the expanded program, even though
|
|
|
|
// the actual code tends to be impossible to execute (at
|
|
|
|
// least safely), according to the type system.
|
|
|
|
//
|
|
|
|
// The most expedient fix for this is to just let the
|
|
|
|
// code fall through to the catch-all. But even this is
|
|
|
|
// error-prone, since the catch-all as defined above would
|
|
|
|
// generate code like this:
|
|
|
|
//
|
|
|
|
// _ => { let __self0 = match *self { };
|
|
|
|
// let __self1 = match *__arg_0 { };
|
|
|
|
// <catch-all-expr> }
|
|
|
|
//
|
|
|
|
// Which is yields bindings for variables which type
|
|
|
|
// inference cannot resolve to unique types.
|
|
|
|
//
|
|
|
|
// One option to the above might be to add explicit type
|
|
|
|
// annotations. But the *only* reason to go down that path
|
|
|
|
// would be to try to make the expanded output consistent
|
|
|
|
// with the case when the number of enum variants >= 1.
|
|
|
|
//
|
|
|
|
// That just isn't worth it. In fact, trying to generate
|
|
|
|
// sensible code for *any* deriving on a zero-variant enum
|
|
|
|
// does not make sense. But at the same time, for now, we
|
|
|
|
// do not want to cause a compile failure just because the
|
|
|
|
// user happened to attach a deriving to their
|
|
|
|
// zero-variant enum.
|
|
|
|
//
|
|
|
|
// Instead, just generate a failing expression for the
|
|
|
|
// zero variant case, skipping matches and also skipping
|
|
|
|
// delegating back to the end user code entirely.
|
|
|
|
//
|
|
|
|
// (See also #4499 and #12609; note that some of the
|
|
|
|
// discussions there influence what choice we make here;
|
|
|
|
// e.g. if we feature-gate `match x { ... }` when x refers
|
|
|
|
// to an uninhabited type (e.g. a zero-variant enum or a
|
|
|
|
// type holding such an enum), but do not feature-gate
|
|
|
|
// zero-variant enums themselves, then attempting to
|
2015-01-28 08:34:18 -05:00
|
|
|
// derive Debug on such a type could here generate code
|
2014-07-06 21:19:12 +02:00
|
|
|
// that needs the feature gate enabled.)
|
|
|
|
|
|
|
|
return cx.expr_unreachable(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final wrinkle: the self_args are expressions that deref
|
|
|
|
// down to desired l-values, but we cannot actually deref
|
|
|
|
// them when they are fed as r-values into a tuple
|
|
|
|
// expression; here add a layer of borrowing, turning
|
|
|
|
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
|
2014-09-13 19:06:01 +03:00
|
|
|
let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg));
|
2014-07-06 21:19:12 +02:00
|
|
|
let match_arg = cx.expr(sp, ast::ExprTup(borrowed_self_args));
|
|
|
|
cx.expr_match(sp, match_arg, match_arms)
|
|
|
|
}
|
|
|
|
|
2013-05-07 01:23:51 +10:00
|
|
|
fn expand_static_enum_method_body(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2013-12-07 11:57:44 +11:00
|
|
|
trait_: &TraitDef,
|
2014-01-09 15:05:33 +02:00
|
|
|
enum_def: &EnumDef,
|
2013-11-07 18:49:01 +11:00
|
|
|
type_ident: Ident,
|
2014-09-13 19:06:01 +03:00
|
|
|
self_args: &[P<Expr>],
|
|
|
|
nonself_args: &[P<Expr>])
|
|
|
|
-> P<Expr> {
|
2014-03-28 20:42:34 +01:00
|
|
|
let summary = enum_def.variants.iter().map(|v| {
|
2013-05-07 01:23:51 +10:00
|
|
|
let ident = v.node.name;
|
|
|
|
let summary = match v.node.kind {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TupleVariantKind(ref args) => {
|
2014-03-28 20:42:34 +01:00
|
|
|
Unnamed(args.iter().map(|va| trait_.set_expn_info(cx, va.ty.span)).collect())
|
2013-12-07 13:43:22 +11:00
|
|
|
}
|
2014-05-16 00:16:13 -07:00
|
|
|
ast::StructVariantKind(ref struct_def) => {
|
|
|
|
trait_.summarise_struct(cx, &**struct_def)
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
|
|
|
};
|
2014-01-27 15:25:37 +11:00
|
|
|
(ident, v.span, summary)
|
2014-03-28 20:42:34 +01:00
|
|
|
}).collect();
|
2014-02-08 19:39:53 -05:00
|
|
|
self.call_substructure_method(cx, trait_, type_ident,
|
2013-05-07 01:23:51 +10:00
|
|
|
self_args, nonself_args,
|
|
|
|
&StaticEnum(enum_def, summary))
|
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq)] // dogfooding!
|
2013-12-07 11:57:44 +11:00
|
|
|
enum StructType {
|
|
|
|
Unknown, Record, Tuple
|
|
|
|
}
|
|
|
|
|
|
|
|
// general helper methods.
|
|
|
|
impl<'a> TraitDef<'a> {
|
2014-02-08 19:39:53 -05:00
|
|
|
fn set_expn_info(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
mut to_set: Span) -> Span {
|
2013-12-23 15:08:23 +01:00
|
|
|
let trait_name = match self.path.path.last() {
|
2014-12-31 17:25:18 +13:00
|
|
|
None => cx.span_bug(self.span, "trait with empty path in generic `derive`"),
|
2013-12-07 13:43:22 +11:00
|
|
|
Some(name) => *name
|
|
|
|
};
|
2014-09-17 19:01:33 +03:00
|
|
|
to_set.expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
|
2013-12-07 13:43:22 +11:00
|
|
|
call_site: to_set,
|
|
|
|
callee: codemap::NameAndSpan {
|
2015-01-12 02:02:38 -08:00
|
|
|
name: format!("derive({})", trait_name),
|
2013-12-07 13:43:22 +11:00
|
|
|
format: codemap::MacroAttribute,
|
|
|
|
span: Some(self.span)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
to_set
|
|
|
|
}
|
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
fn summarise_struct(&self,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
struct_def: &StructDef) -> StaticFields {
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut named_idents = Vec::new();
|
|
|
|
let mut just_spans = Vec::new();
|
2013-12-07 11:57:44 +11:00
|
|
|
for field in struct_def.fields.iter(){
|
2014-02-08 19:39:53 -05:00
|
|
|
let sp = self.set_expn_info(cx, field.span);
|
2013-12-07 11:57:44 +11:00
|
|
|
match field.node.kind {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
|
2014-03-25 16:53:52 -07:00
|
|
|
ast::UnnamedField(..) => just_spans.push(sp),
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
2013-05-07 01:23:51 +10:00
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
match (just_spans.is_empty(), named_idents.is_empty()) {
|
2014-02-08 19:39:53 -05:00
|
|
|
(false, false) => cx.span_bug(self.span,
|
|
|
|
"a struct with named and unnamed \
|
2014-12-31 17:25:18 +13:00
|
|
|
fields in generic `derive`"),
|
2013-12-07 11:57:44 +11:00
|
|
|
// named fields
|
|
|
|
(_, false) => Named(named_idents),
|
|
|
|
// tuple structs (includes empty structs)
|
|
|
|
(_, _) => Unnamed(just_spans)
|
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
fn create_subpatterns(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2014-06-30 18:02:14 -07:00
|
|
|
field_paths: Vec<ast::SpannedIdent> ,
|
2013-09-02 03:45:37 +02:00
|
|
|
mutbl: ast::Mutability)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> Vec<P<ast::Pat>> {
|
2014-03-28 20:42:34 +01:00
|
|
|
field_paths.iter().map(|path| {
|
2014-02-08 19:39:53 -05:00
|
|
|
cx.pat(path.span,
|
2013-12-07 11:57:44 +11:00
|
|
|
ast::PatIdent(ast::BindByRef(mutbl), (*path).clone(), None))
|
2014-09-13 19:06:01 +03:00
|
|
|
}).collect()
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
fn create_struct_pattern(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2014-11-06 00:05:53 -08:00
|
|
|
struct_path: ast::Path,
|
2014-01-09 15:05:33 +02:00
|
|
|
struct_def: &StructDef,
|
2013-12-07 11:57:44 +11:00
|
|
|
prefix: &str,
|
|
|
|
mutbl: ast::Mutability)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) {
|
2013-12-07 11:57:44 +11:00
|
|
|
if struct_def.fields.is_empty() {
|
2014-11-06 00:05:53 -08:00
|
|
|
return (cx.pat_enum(self.span, struct_path, vec![]), vec![]);
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut paths = Vec::new();
|
|
|
|
let mut ident_expr = Vec::new();
|
2013-12-07 11:57:44 +11:00
|
|
|
let mut struct_type = Unknown;
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
for (i, struct_field) in struct_def.fields.iter().enumerate() {
|
2014-02-08 19:39:53 -05:00
|
|
|
let sp = self.set_expn_info(cx, struct_field.span);
|
2013-12-07 11:57:44 +11:00
|
|
|
let opt_id = match struct_field.node.kind {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::NamedField(ident, _) if (struct_type == Unknown ||
|
|
|
|
struct_type == Record) => {
|
2013-12-07 11:57:44 +11:00
|
|
|
struct_type = Record;
|
|
|
|
Some(ident)
|
|
|
|
}
|
2014-03-25 16:53:52 -07:00
|
|
|
ast::UnnamedField(..) if (struct_type == Unknown ||
|
|
|
|
struct_type == Tuple) => {
|
2013-12-07 11:57:44 +11:00
|
|
|
struct_type = Tuple;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => {
|
2014-12-31 17:25:18 +13:00
|
|
|
cx.span_bug(sp, "a struct with named and unnamed fields in `derive`");
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
|
|
|
};
|
2015-01-07 11:58:31 -05:00
|
|
|
let ident = cx.ident_of(&format!("{}_{}", prefix, i)[]);
|
2014-06-30 18:02:14 -07:00
|
|
|
paths.push(codemap::Spanned{span: sp, node: ident});
|
2014-02-08 19:39:53 -05:00
|
|
|
let val = cx.expr(
|
2014-06-30 18:02:14 -07:00
|
|
|
sp, ast::ExprParen(cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)))));
|
2014-01-28 00:20:50 +11:00
|
|
|
ident_expr.push((sp, opt_id, val));
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
let subpats = self.create_subpatterns(cx, paths, mutbl);
|
2013-12-07 11:57:44 +11:00
|
|
|
|
|
|
|
// struct_type is definitely not Unknown, since struct_def.fields
|
|
|
|
// must be nonempty to reach here
|
|
|
|
let pattern = if struct_type == Record {
|
2014-09-14 20:27:36 -07:00
|
|
|
let field_pats = subpats.into_iter().zip(ident_expr.iter()).map(|(pat, &(_, id, _))| {
|
2013-12-07 11:57:44 +11:00
|
|
|
// id is guaranteed to be Some
|
2014-10-06 13:36:53 +13:00
|
|
|
codemap::Spanned {
|
|
|
|
span: pat.span,
|
2014-10-27 00:11:26 -07:00
|
|
|
node: ast::FieldPat { ident: id.unwrap(), pat: pat, is_shorthand: false },
|
2014-10-06 13:36:53 +13:00
|
|
|
}
|
2013-12-07 11:57:44 +11:00
|
|
|
}).collect();
|
2014-11-06 00:05:53 -08:00
|
|
|
cx.pat_struct(self.span, struct_path, field_pats)
|
2013-12-07 11:57:44 +11:00
|
|
|
} else {
|
2014-11-06 00:05:53 -08:00
|
|
|
cx.pat_enum(self.span, struct_path, subpats)
|
2013-06-07 17:30:38 +10:00
|
|
|
};
|
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
(pattern, ident_expr)
|
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2013-12-07 11:57:44 +11:00
|
|
|
fn create_enum_variant_pattern(&self,
|
2014-02-08 19:39:53 -05:00
|
|
|
cx: &mut ExtCtxt,
|
2014-11-06 00:05:53 -08:00
|
|
|
enum_ident: ast::Ident,
|
2014-01-09 15:05:33 +02:00
|
|
|
variant: &ast::Variant,
|
2013-12-07 11:57:44 +11:00
|
|
|
prefix: &str,
|
|
|
|
mutbl: ast::Mutability)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) {
|
2013-12-07 11:57:44 +11:00
|
|
|
let variant_ident = variant.node.name;
|
2014-11-06 00:05:53 -08:00
|
|
|
let variant_path = cx.path(variant.span, vec![enum_ident, variant_ident]);
|
2013-12-07 11:57:44 +11:00
|
|
|
match variant.node.kind {
|
2014-01-09 15:05:33 +02:00
|
|
|
ast::TupleVariantKind(ref variant_args) => {
|
2013-12-07 11:57:44 +11:00
|
|
|
if variant_args.is_empty() {
|
2014-11-06 00:05:53 -08:00
|
|
|
return (cx.pat_enum(variant.span, variant_path, vec![]), vec![]);
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut paths = Vec::new();
|
|
|
|
let mut ident_expr = Vec::new();
|
2013-12-07 11:57:44 +11:00
|
|
|
for (i, va) in variant_args.iter().enumerate() {
|
2014-02-08 19:39:53 -05:00
|
|
|
let sp = self.set_expn_info(cx, va.ty.span);
|
2015-01-07 11:58:31 -05:00
|
|
|
let ident = cx.ident_of(&format!("{}_{}", prefix, i)[]);
|
2014-06-30 18:02:14 -07:00
|
|
|
let path1 = codemap::Spanned{span: sp, node: ident};
|
|
|
|
paths.push(path1);
|
|
|
|
let expr_path = cx.expr_path(cx.path_ident(sp, ident));
|
|
|
|
let val = cx.expr(sp, ast::ExprParen(cx.expr_deref(sp, expr_path)));
|
2014-01-28 00:20:50 +11:00
|
|
|
ident_expr.push((sp, None, val));
|
2013-12-07 11:57:44 +11:00
|
|
|
}
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-02-08 19:39:53 -05:00
|
|
|
let subpats = self.create_subpatterns(cx, paths, mutbl);
|
2013-06-07 17:30:38 +10:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
(cx.pat_enum(variant.span, variant_path, subpats),
|
2013-12-07 11:57:44 +11:00
|
|
|
ident_expr)
|
|
|
|
}
|
2014-05-16 00:16:13 -07:00
|
|
|
ast::StructVariantKind(ref struct_def) => {
|
2014-11-06 00:05:53 -08:00
|
|
|
self.create_struct_pattern(cx, variant_path, &**struct_def,
|
2013-12-07 11:57:44 +11:00
|
|
|
prefix, mutbl)
|
2013-06-07 17:30:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 21:50:10 +11:00
|
|
|
/* helpful premade recipes */
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Fold the fields. `use_foldl` controls whether this is done
|
|
|
|
/// left-to-right (`true`) or right-to-left (`false`).
|
2014-12-08 13:28:32 -05:00
|
|
|
pub fn cs_fold<F>(use_foldl: bool,
|
|
|
|
mut f: F,
|
|
|
|
base: P<Expr>,
|
2015-01-01 18:32:49 -05:00
|
|
|
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
2014-12-08 13:28:32 -05:00
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_span: Span,
|
|
|
|
substructure: &Substructure)
|
|
|
|
-> P<Expr> where
|
|
|
|
F: FnMut(&mut ExtCtxt, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>,
|
|
|
|
{
|
2013-03-28 21:50:10 +11:00
|
|
|
match *substructure.fields {
|
2013-05-12 00:25:31 -04:00
|
|
|
EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => {
|
2013-03-28 21:50:10 +11:00
|
|
|
if use_foldl {
|
2013-11-20 16:23:04 -08:00
|
|
|
all_fields.iter().fold(base, |old, field| {
|
2014-02-28 12:54:01 -08:00
|
|
|
f(cx,
|
|
|
|
field.span,
|
|
|
|
old,
|
2014-09-13 19:06:01 +03:00
|
|
|
field.self_.clone(),
|
2015-01-07 11:58:31 -05:00
|
|
|
&field.other[])
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-03-28 21:50:10 +11:00
|
|
|
} else {
|
2014-03-16 16:04:29 -07:00
|
|
|
all_fields.iter().rev().fold(base, |old, field| {
|
2014-02-28 12:54:01 -08:00
|
|
|
f(cx,
|
|
|
|
field.span,
|
|
|
|
old,
|
2014-09-13 19:06:01 +03:00
|
|
|
field.self_.clone(),
|
2015-01-07 11:58:31 -05:00
|
|
|
&field.other[])
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
},
|
2014-07-06 21:19:12 +02:00
|
|
|
EnumNonMatchingCollapsed(ref all_args, _, tuple) =>
|
2015-01-07 11:58:31 -05:00
|
|
|
enum_nonmatch_f(cx, trait_span, (&all_args[], tuple),
|
2015-01-06 09:24:46 -08:00
|
|
|
substructure.nonself_args),
|
2013-11-28 12:22:53 -08:00
|
|
|
StaticEnum(..) | StaticStruct(..) => {
|
2014-12-31 17:25:18 +13:00
|
|
|
cx.span_bug(trait_span, "static function in `derive`")
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Call the method that is being derived on all the fields, and then
|
|
|
|
/// process the collected results. i.e.
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-13 00:00:19 +11:00
|
|
|
/// f(cx, span, vec![self_1.method(__arg_1_1, __arg_2_1),
|
|
|
|
/// self_2.method(__arg_1_2, __arg_2_2)])
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-12-08 13:28:32 -05:00
|
|
|
pub fn cs_same_method<F>(f: F,
|
2015-01-01 18:32:49 -05:00
|
|
|
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
2014-12-08 13:28:32 -05:00
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_span: Span,
|
|
|
|
substructure: &Substructure)
|
|
|
|
-> P<Expr> where
|
|
|
|
F: FnOnce(&mut ExtCtxt, Span, Vec<P<Expr>>) -> P<Expr>,
|
|
|
|
{
|
2013-03-28 21:50:10 +11:00
|
|
|
match *substructure.fields {
|
2013-05-12 00:25:31 -04:00
|
|
|
EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => {
|
2013-03-28 21:50:10 +11:00
|
|
|
// call self_n.method(other_1_n, other_2_n, ...)
|
2014-03-28 20:42:34 +01:00
|
|
|
let called = all_fields.iter().map(|field| {
|
2013-11-07 18:49:01 +11:00
|
|
|
cx.expr_method_call(field.span,
|
2014-09-13 19:06:01 +03:00
|
|
|
field.self_.clone(),
|
2013-05-19 15:53:42 +10:00
|
|
|
substructure.method_ident,
|
2014-03-28 20:42:34 +01:00
|
|
|
field.other.iter()
|
2014-09-13 19:06:01 +03:00
|
|
|
.map(|e| cx.expr_addr_of(field.span, e.clone()))
|
2014-03-28 20:42:34 +01:00
|
|
|
.collect())
|
|
|
|
}).collect();
|
2013-03-28 21:50:10 +11:00
|
|
|
|
2013-11-07 18:49:01 +11:00
|
|
|
f(cx, trait_span, called)
|
2013-03-28 21:50:10 +11:00
|
|
|
},
|
2014-07-06 21:19:12 +02:00
|
|
|
EnumNonMatchingCollapsed(ref all_self_args, _, tuple) =>
|
2015-01-07 11:58:31 -05:00
|
|
|
enum_nonmatch_f(cx, trait_span, (&all_self_args[], tuple),
|
2015-01-06 09:24:46 -08:00
|
|
|
substructure.nonself_args),
|
2013-11-28 12:22:53 -08:00
|
|
|
StaticEnum(..) | StaticStruct(..) => {
|
2014-12-31 17:25:18 +13:00
|
|
|
cx.span_bug(trait_span, "static function in `derive`")
|
2013-05-07 01:23:51 +10:00
|
|
|
}
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Fold together the results of calling the derived method on all the
|
|
|
|
/// fields. `use_foldl` controls whether this is done left-to-right
|
|
|
|
/// (`true`) or right-to-left (`false`).
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-12-08 13:28:32 -05:00
|
|
|
pub fn cs_same_method_fold<F>(use_foldl: bool,
|
|
|
|
mut f: F,
|
|
|
|
base: P<Expr>,
|
|
|
|
enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
|
|
|
cx: &mut ExtCtxt,
|
|
|
|
trait_span: Span,
|
|
|
|
substructure: &Substructure)
|
|
|
|
-> P<Expr> where
|
|
|
|
F: FnMut(&mut ExtCtxt, Span, P<Expr>, P<Expr>) -> P<Expr>,
|
|
|
|
{
|
2013-03-28 21:50:10 +11:00
|
|
|
cs_same_method(
|
|
|
|
|cx, span, vals| {
|
|
|
|
if use_foldl {
|
2014-09-14 20:27:36 -07:00
|
|
|
vals.into_iter().fold(base.clone(), |old, new| {
|
2013-03-28 21:50:10 +11:00
|
|
|
f(cx, span, old, new)
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-03-28 21:50:10 +11:00
|
|
|
} else {
|
2014-09-14 20:27:36 -07:00
|
|
|
vals.into_iter().rev().fold(base.clone(), |old, new| {
|
2013-03-28 21:50:10 +11:00
|
|
|
f(cx, span, old, new)
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
enum_nonmatch_f,
|
2013-11-07 18:49:01 +11:00
|
|
|
cx, trait_span, substructure)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
2014-09-16 13:27:34 +02:00
|
|
|
/// Use a given binop to combine the result of calling the derived method
|
|
|
|
/// on all the fields.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2015-01-13 14:24:37 +11:00
|
|
|
pub fn cs_binop(binop: ast::BinOp_, base: P<Expr>,
|
2014-07-07 09:13:49 +02:00
|
|
|
enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
2014-02-06 08:50:21 +11:00
|
|
|
cx: &mut ExtCtxt, trait_span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
substructure: &Substructure) -> P<Expr> {
|
2013-03-28 21:50:10 +11:00
|
|
|
cs_same_method_fold(
|
|
|
|
true, // foldl is good enough
|
|
|
|
|cx, span, old, new| {
|
2013-05-19 15:53:42 +10:00
|
|
|
cx.expr_binary(span,
|
|
|
|
binop,
|
|
|
|
old, new)
|
2013-03-28 21:50:10 +11:00
|
|
|
|
|
|
|
},
|
|
|
|
base,
|
|
|
|
enum_nonmatch_f,
|
2013-11-07 18:49:01 +11:00
|
|
|
cx, trait_span, substructure)
|
2013-03-28 21:50:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// cs_binop with binop == or
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-07-07 09:13:49 +02:00
|
|
|
pub fn cs_or(enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
2014-02-06 08:50:21 +11:00
|
|
|
cx: &mut ExtCtxt, span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
substructure: &Substructure) -> P<Expr> {
|
2013-09-02 03:45:37 +02:00
|
|
|
cs_binop(ast::BiOr, cx.expr_bool(span, false),
|
2013-03-28 21:50:10 +11:00
|
|
|
enum_nonmatch_f,
|
|
|
|
cx, span, substructure)
|
|
|
|
}
|
2013-09-16 21:12:18 -07:00
|
|
|
|
2013-03-28 21:50:10 +11:00
|
|
|
/// cs_binop with binop == and
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-07-07 09:13:49 +02:00
|
|
|
pub fn cs_and(enum_nonmatch_f: EnumNonMatchCollapsedFunc,
|
2014-02-06 08:50:21 +11:00
|
|
|
cx: &mut ExtCtxt, span: Span,
|
2014-09-13 19:06:01 +03:00
|
|
|
substructure: &Substructure) -> P<Expr> {
|
2013-09-02 03:45:37 +02:00
|
|
|
cs_binop(ast::BiAnd, cx.expr_bool(span, true),
|
2013-03-28 21:50:10 +11:00
|
|
|
enum_nonmatch_f,
|
|
|
|
cx, span, substructure)
|
|
|
|
}
|