syntax/ext: remove the ~str dependence of the deriving code.

This commit is contained in:
Huon Wilson 2013-05-17 18:41:14 +10:00
parent dc7b83d186
commit 8e9eba8013
10 changed files with 81 additions and 79 deletions

View file

@ -21,12 +21,12 @@ pub fn expand_deriving_clone(cx: @ext_ctxt,
in_items: ~[@item])
-> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"clone", ~"Clone"]),
path: Path::new(~["core", "clone", "Clone"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"clone",
name: "clone",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[],
@ -48,12 +48,12 @@ pub fn expand_deriving_deep_clone(cx: @ext_ctxt,
in_items: ~[@item])
-> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"clone", ~"DeepClone"]),
path: Path::new(~["core", "clone", "DeepClone"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"deep_clone",
name: "deep_clone",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[],

View file

@ -36,7 +36,7 @@ pub fn expand_deriving_eq(cx: @ext_ctxt,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[borrowed_self()],
ret_ty: Literal(Path::new(~[~"bool"])),
ret_ty: Literal(Path::new(~["bool"])),
const_nonmatching: true,
combine_substructure: $f
},
@ -44,12 +44,12 @@ pub fn expand_deriving_eq(cx: @ext_ctxt,
);
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"cmp", ~"Eq"]),
path: Path::new(~["core", "cmp", "Eq"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
md!(~"eq", cs_eq),
md!(~"ne", cs_ne)
md!("eq", cs_eq),
md!("ne", cs_ne)
]
};

View file

@ -26,7 +26,7 @@ pub fn expand_deriving_ord(cx: @ext_ctxt,
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[borrowed_self()],
ret_ty: Literal(Path::new(~[~"bool"])),
ret_ty: Literal(Path::new(~["bool"])),
const_nonmatching: false,
combine_substructure: |cx, span, substr|
cs_ord($less, $equal, cx, span, substr)
@ -37,15 +37,15 @@ pub fn expand_deriving_ord(cx: @ext_ctxt,
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"cmp", ~"Ord"]),
path: Path::new(~["core", "cmp", "Ord"]),
// XXX: Ord doesn't imply Eq yet
additional_bounds: ~[Literal(Path::new(~[~"core", ~"cmp", ~"Eq"]))],
additional_bounds: ~[Literal(Path::new(~["core", "cmp", "Eq"]))],
generics: LifetimeBounds::empty(),
methods: ~[
md!(~"lt", true, false),
md!(~"le", true, true),
md!(~"gt", false, false),
md!(~"ge", false, true)
md!("lt", true, false),
md!("le", true, true),
md!("gt", false, false),
md!("ge", false, true)
]
};

View file

@ -26,16 +26,16 @@ pub fn expand_deriving_totaleq(cx: @ext_ctxt,
}
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"cmp", ~"TotalEq"]),
path: Path::new(~["core", "cmp", "TotalEq"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"equals",
name: "equals",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[borrowed_self()],
ret_ty: Literal(Path::new(~[~"bool"])),
ret_ty: Literal(Path::new(~["bool"])),
const_nonmatching: true,
combine_substructure: cs_equals
}

View file

@ -20,16 +20,16 @@ pub fn expand_deriving_totalord(cx: @ext_ctxt,
mitem: @meta_item,
in_items: ~[@item]) -> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"cmp", ~"TotalOrd"]),
path: Path::new(~["core", "cmp", "TotalOrd"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"cmp",
name: "cmp",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[borrowed_self()],
ret_ty: Literal(Path::new(~[~"core", ~"cmp", ~"Ordering"])),
ret_ty: Literal(Path::new(~["core", "cmp", "Ordering"])),
const_nonmatching: false,
combine_substructure: cs_cmp
}

View file

@ -195,13 +195,13 @@ pub fn expand_deriving_generic(cx: @ext_ctxt,
pub struct TraitDef<'self> {
/// Path of the trait, including any type parameters
path: Path,
path: Path<'self>,
/// Additional bounds required of any type parameters of the type,
/// other than the current trait
additional_bounds: ~[Ty],
additional_bounds: ~[Ty<'self>],
/// Any extra lifetimes and/or bounds, e.g. `D: std::serialize::Decoder`
generics: LifetimeBounds,
generics: LifetimeBounds<'self>,
methods: ~[MethodDef<'self>]
}
@ -209,20 +209,20 @@ pub struct TraitDef<'self> {
pub struct MethodDef<'self> {
/// name of the method
name: ~str,
name: &'self str,
/// List of generics, e.g. `R: core::rand::Rng`
generics: LifetimeBounds,
generics: LifetimeBounds<'self>,
/// Whether there is a self argument (outer Option) i.e. whether
/// this is a static function, and whether it is a pointer (inner
/// Option)
explicit_self: Option<Option<PtrTy>>,
explicit_self: Option<Option<PtrTy<'self>>>,
/// Arguments other than the self argument
args: ~[Ty],
args: ~[Ty<'self>],
/// Return type
ret_ty: Ty,
ret_ty: Ty<'self>,
/// if the value of the nonmatching enums is independent of the
/// actual enum variants, i.e. can use _ => .. match.

View file

@ -19,19 +19,19 @@ pub fn expand_deriving_iter_bytes(cx: @ext_ctxt,
mitem: @meta_item,
in_items: ~[@item]) -> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"to_bytes", ~"IterBytes"]),
path: Path::new(~["core", "to_bytes", "IterBytes"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"iter_bytes",
name: "iter_bytes",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[
Literal(Path::new(~[~"bool"])),
Literal(Path::new(~[~"core", ~"to_bytes", ~"Cb"]))
Literal(Path::new(~["bool"])),
Literal(Path::new(~["core", "to_bytes", "Cb"]))
],
ret_ty: Literal(Path::new(~[~"bool"])),
ret_ty: Literal(Path::new(~["bool"])),
const_nonmatching: false,
combine_substructure: iter_bytes_substructure
}

View file

@ -21,20 +21,20 @@ pub fn expand_deriving_rand(cx: @ext_ctxt,
in_items: ~[@item])
-> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"rand", ~"Rand"]),
path: Path::new(~["core", "rand", "Rand"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"rand",
name: "rand",
generics: LifetimeBounds {
lifetimes: ~[],
bounds: ~[(~"R",
~[ Path::new(~[~"core", ~"rand", ~"Rng"]) ])]
bounds: ~[("R",
~[ Path::new(~["core", "rand", "Rng"]) ])]
},
explicit_self: None,
args: ~[
Ptr(~Literal(Path::new_local(~"R")),
Ptr(~Literal(Path::new_local("R")),
Borrowed(None, ast::m_mutbl))
],
ret_ty: Self,

View file

@ -20,16 +20,16 @@ pub fn expand_deriving_to_str(cx: @ext_ctxt,
in_items: ~[@item])
-> ~[@item] {
let trait_def = TraitDef {
path: Path::new(~[~"core", ~"to_str", ~"ToStr"]),
path: Path::new(~["core", "to_str", "ToStr"]),
additional_bounds: ~[],
generics: LifetimeBounds::empty(),
methods: ~[
MethodDef {
name: ~"to_str",
name: "to_str",
generics: LifetimeBounds::empty(),
explicit_self: borrowed_explicit_self(),
args: ~[],
ret_ty: Ptr(~Literal(Path::new_local(~"str")), Owned),
ret_ty: Ptr(~Literal(Path::new_local("str")), Owned),
const_nonmatching: false,
combine_substructure: to_str_substructure
}

View file

@ -21,31 +21,30 @@ use codemap::{span,respan};
use opt_vec;
/// The types of pointers
#[deriving(Eq)]
pub enum PtrTy {
pub enum PtrTy<'self> {
Owned, // ~
Managed(ast::mutability), // @[mut]
Borrowed(Option<~str>, ast::mutability), // &['lifetime] [mut]
Borrowed(Option<&'self str>, ast::mutability), // &['lifetime] [mut]
}
/// A path, e.g. `::core::option::Option::<int>` (global). Has support
/// for type parameters and a lifetime.
#[deriving(Eq)]
pub struct Path {
path: ~[~str],
lifetime: Option<~str>,
params: ~[~Ty],
pub struct Path<'self> {
path: ~[&'self str],
lifetime: Option<&'self str>,
params: ~[~Ty<'self>],
global: bool
}
pub impl Path {
fn new(path: ~[~str]) -> Path {
pub impl<'self> Path<'self> {
fn new<'r>(path: ~[&'r str]) -> Path<'r> {
Path::new_(path, None, ~[], true)
}
fn new_local(path: ~str) -> Path {
fn new_local<'r>(path: &'r str) -> Path<'r> {
Path::new_(~[ path ], None, ~[], false)
}
fn new_(path: ~[~str], lifetime: Option<~str>, params: ~[~Ty], global: bool) -> Path {
fn new_<'r>(path: ~[&'r str], lifetime: Option<&'r str>, params: ~[~Ty<'r>], global: bool)
-> Path<'r> {
Path {
path: path,
lifetime: lifetime,
@ -56,9 +55,9 @@ pub impl Path {
fn to_ty(&self, cx: @ext_ctxt, span: span,
self_ty: ident, self_generics: &Generics) -> @ast::Ty {
build::mk_ty_path_path(cx, span,
self.to_path(cx, span,
self_ty, self_generics))
build::mk_ty_path_path(cx, span,
self.to_path(cx, span,
self_ty, self_generics))
}
fn to_path(&self, cx: @ext_ctxt, span: span,
self_ty: ident, self_generics: &Generics) -> @ast::Path {
@ -75,45 +74,44 @@ pub impl Path {
}
/// A type. Supports pointers (except for *), Self, and literals
#[deriving(Eq)]
pub enum Ty {
pub enum Ty<'self> {
Self,
// &/~/@ Ty
Ptr(~Ty, PtrTy),
Ptr(~Ty<'self>, PtrTy<'self>),
// mod::mod::Type<[lifetime], [Params...]>, including a plain type
// parameter, and things like `int`
Literal(Path),
Literal(Path<'self>),
// includes nil
Tuple(~[Ty])
Tuple(~[Ty<'self>])
}
pub fn borrowed_ptrty() -> PtrTy {
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
Borrowed(None, ast::m_imm)
}
pub fn borrowed(ty: ~Ty) -> Ty {
pub fn borrowed<'r>(ty: ~Ty<'r>) -> Ty<'r> {
Ptr(ty, borrowed_ptrty())
}
pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
Some(Some(borrowed_ptrty()))
}
pub fn borrowed_self() -> Ty {
pub fn borrowed_self<'r>() -> Ty<'r> {
borrowed(~Self)
}
pub fn nil_ty() -> Ty {
pub fn nil_ty() -> Ty<'static> {
Tuple(~[])
}
fn mk_lifetime(cx: @ext_ctxt, span: span, lt: &Option<~str>) -> Option<@ast::Lifetime> {
fn mk_lifetime(cx: @ext_ctxt, span: span, lt: &Option<&str>) -> Option<@ast::Lifetime> {
match *lt {
Some(ref s) => Some(@build::mk_lifetime(cx, span, cx.ident_of(*s))),
None => None
}
}
pub impl Ty {
pub impl<'self> Ty<'self> {
fn to_ty(&self, cx: @ext_ctxt, span: span,
self_ty: ident, self_generics: &Generics) -> @ast::Ty {
match *self {
@ -174,7 +172,7 @@ pub impl Ty {
}
fn mk_ty_param(cx: @ext_ctxt, span: span, name: ~str, bounds: ~[Path],
fn mk_ty_param(cx: @ext_ctxt, span: span, name: &str, bounds: &[Path],
self_ident: ident, self_generics: &Generics) -> ast::TyParam {
let bounds = opt_vec::from(
do bounds.map |b| {
@ -192,24 +190,28 @@ fn mk_generics(lifetimes: ~[ast::Lifetime], ty_params: ~[ast::TyParam]) -> Gene
}
/// Lifetimes and bounds on type parameters
pub struct LifetimeBounds {
lifetimes: ~[~str],
bounds: ~[(~str, ~[Path])]
pub struct LifetimeBounds<'self> {
lifetimes: ~[&'self str],
bounds: ~[(&'self str, ~[Path<'self>])]
}
pub impl LifetimeBounds {
fn empty() -> LifetimeBounds {
pub impl<'self> LifetimeBounds<'self> {
fn empty() -> LifetimeBounds<'static> {
LifetimeBounds {
lifetimes: ~[], bounds: ~[]
}
}
fn to_generics(&self, cx: @ext_ctxt, span: span,
self_ty: ident, self_generics: &Generics) -> Generics {
let lifetimes = do self.lifetimes.map |&lt| {
build::mk_lifetime(cx, span, cx.ident_of(lt))
let lifetimes = do self.lifetimes.map |lt| {
build::mk_lifetime(cx, span, cx.ident_of(*lt))
};
let ty_params = do self.bounds.map |&(name, bounds)| {
mk_ty_param(cx, span, name, bounds, self_ty, self_generics)
let ty_params = do self.bounds.map |t| {
match t {
&(ref name, ref bounds) => {
mk_ty_param(cx, span, *name, *bounds, self_ty, self_generics)
}
}
};
mk_generics(lifetimes, ty_params)
}