diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-02-28 23:17:38 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-03-06 18:11:02 -0800 |
| commit | 0a8413292845f527b8cf6bb2d02c9923f782ff37 (patch) | |
| tree | 6c4877cab523a54019eb8e15820c8531913139cc /src/libsyntax | |
| parent | bec7b766fbbc9b56a1608e2573390e3894519840 (diff) | |
| download | rust-0a8413292845f527b8cf6bb2d02c9923f782ff37.tar.gz rust-0a8413292845f527b8cf6bb2d02c9923f782ff37.zip | |
syntax: Conditionally deriving(Hash) with Writers
If #[feature(default_type_parameters)] is enabled for a crate, then deriving(Hash) will expand with Hash<W: Writer> instead of Hash<SipState> so more hash algorithms can be used.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/hash.rs | 20 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 39 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/util/nodemap.rs | 75 |
5 files changed, 50 insertions, 92 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 79068d40469..459c0d1d0e3 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -283,7 +283,7 @@ pub struct ExtCtxt<'a> { parse_sess: @parse::ParseSess, cfg: ast::CrateConfig, backtrace: Option<@ExpnInfo>, - loader: &'a mut CrateLoader, + ecfg: expand::ExpansionConfig<'a>, mod_path: Vec<ast::Ident> , trace_mac: bool @@ -291,13 +291,13 @@ pub struct ExtCtxt<'a> { impl<'a> ExtCtxt<'a> { pub fn new<'a>(parse_sess: @parse::ParseSess, cfg: ast::CrateConfig, - loader: &'a mut CrateLoader) -> ExtCtxt<'a> { + ecfg: expand::ExpansionConfig<'a>) -> ExtCtxt<'a> { ExtCtxt { parse_sess: parse_sess, cfg: cfg, backtrace: None, - loader: loader, mod_path: Vec::new(), + ecfg: ecfg, trace_mac: false } } diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index 1d6cfab120d..299989d5fe6 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -22,19 +22,31 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { + let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter { + (Path::new_(vec!("std", "hash", "Hash"), None, + vec!(~Literal(Path::new_local("__H"))), true), + LifetimeBounds { + lifetimes: Vec::new(), + bounds: vec!(("__H", vec!(Path::new(vec!("std", "io", "Writer"))))), + }, + Path::new_local("__H")) + } else { + (Path::new(vec!("std", "hash", "Hash")), + LifetimeBounds::empty(), + Path::new(vec!("std", "hash", "sip", "SipState"))) + }; let hash_trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "hash", "Hash")), + path: path, additional_bounds: Vec::new(), - generics: LifetimeBounds::empty(), + generics: generics, methods: vec!( MethodDef { name: "hash", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec!(Ptr(~Literal(Path::new(vec!("std", "hash", "sip", "SipState"))), - Borrowed(None, MutMutable))), + args: vec!(Ptr(~Literal(args), Borrowed(None, MutMutable))), ret_ty: nil_ty(), inline: true, const_nonmatching: false, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 8b23de235b8..12eaa759ede 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -443,7 +443,7 @@ pub fn expand_view_item(vi: &ast::ViewItem, } fn load_extern_macros(krate: &ast::ViewItem, fld: &mut MacroExpander) { - let MacroCrate { lib, cnum } = fld.cx.loader.load_crate(krate); + let MacroCrate { lib, cnum } = fld.cx.ecfg.loader.load_crate(krate); let crate_name = match krate.node { ast::ViewItemExternMod(name, _, _) => name, @@ -451,7 +451,7 @@ fn load_extern_macros(krate: &ast::ViewItem, fld: &mut MacroExpander) { }; let name = format!("<{} macros>", token::get_ident(crate_name)); - let exported_macros = fld.cx.loader.get_exported_macros(cnum); + let exported_macros = fld.cx.ecfg.loader.get_exported_macros(cnum); for source in exported_macros.iter() { let item = parse::parse_item_from_source_str(name.clone(), (*source).clone(), @@ -468,7 +468,7 @@ fn load_extern_macros(krate: &ast::ViewItem, fld: &mut MacroExpander) { // Make sure the path contains a / or the linker will search for it. let path = os::make_absolute(&path); - let registrar = match fld.cx.loader.get_registrar_symbol(cnum) { + let registrar = match fld.cx.ecfg.loader.get_registrar_symbol(cnum) { Some(registrar) => registrar, None => return }; @@ -823,10 +823,15 @@ impl<'a> Folder for MacroExpander<'a> { } } +pub struct ExpansionConfig<'a> { + loader: &'a mut CrateLoader, + deriving_hash_type_parameter: bool, +} + pub fn expand_crate(parse_sess: @parse::ParseSess, - loader: &mut CrateLoader, + cfg: ExpansionConfig, c: Crate) -> Crate { - let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), loader); + let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg); let mut expander = MacroExpander { extsbox: syntax_expander_table(), cx: &mut cx, @@ -995,7 +1000,11 @@ mod test { Vec::new(),sess); // should fail: let mut loader = ErrLoader; - expand_crate(sess,&mut loader,crate_ast); + let cfg = ::syntax::ext::expand::ExpansionConfig { + loader: &mut loader, + deriving_hash_type_parameter: false, + }; + expand_crate(sess,cfg,crate_ast); } // make sure that macros can leave scope for modules @@ -1010,7 +1019,11 @@ mod test { Vec::new(),sess); // should fail: let mut loader = ErrLoader; - expand_crate(sess,&mut loader,crate_ast); + let cfg = ::syntax::ext::expand::ExpansionConfig { + loader: &mut loader, + deriving_hash_type_parameter: false, + }; + expand_crate(sess,cfg,crate_ast); } // macro_escape modules shouldn't cause macros to leave scope @@ -1024,7 +1037,11 @@ mod test { Vec::new(), sess); // should fail: let mut loader = ErrLoader; - expand_crate(sess, &mut loader, crate_ast); + let cfg = ::syntax::ext::expand::ExpansionConfig { + loader: &mut loader, + deriving_hash_type_parameter: false, + }; + expand_crate(sess, cfg, crate_ast); } #[test] fn test_contains_flatten (){ @@ -1062,7 +1079,11 @@ mod test { let (crate_ast,ps) = string_to_crate_and_sess(crate_str); // the cfg argument actually does matter, here... let mut loader = ErrLoader; - expand_crate(ps,&mut loader,crate_ast) + let cfg = ::syntax::ext::expand::ExpansionConfig { + loader: &mut loader, + deriving_hash_type_parameter: false, + }; + expand_crate(ps,cfg,crate_ast) } //fn expand_and_resolve(crate_str: @str) -> ast::crate { diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 0d7e54bb69d..0d465e8475c 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -26,7 +26,7 @@ This API is completely unstable and subject to change. html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://static.rust-lang.org/doc/master")]; -#[feature(macro_rules, globs, managed_boxes)]; +#[feature(macro_rules, globs, managed_boxes, default_type_params)]; #[allow(unknown_features)];// Note: remove it after a snapshot. #[feature(quote)]; diff --git a/src/libsyntax/util/nodemap.rs b/src/libsyntax/util/nodemap.rs deleted file mode 100644 index e9ace61eb95..00000000000 --- a/src/libsyntax/util/nodemap.rs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! An efficient hash map for node IDs - -use collections::hashmap; -use collections::{HashMap, HashSet}; -use std::hash::{Hasher, Hash}; -use std::iter; -use ast; - -pub type NodeMap<T> = HashMap<ast::NodeId, T, NodeHasher>; -pub type DefIdMap<T> = HashMap<ast::DefId, T, NodeHasher>; -pub type NodeSet = HashSet<ast::NodeId, NodeHasher>; -pub type DefIdSet = HashSet<ast::DefId, NodeHasher>; - -#[deriving(Clone)] -struct NodeHasher; - -impl Hasher<u64> for NodeHasher { - fn hash<T: Hash<u64>>(&self, t: &T) -> u64 { - let mut last = 0; - t.hash(&mut last); - return last - } -} - -impl Hash<u64> for ast::NodeId { - fn hash(&self, state: &mut u64) { - *state = self.get() as u64; - } -} - -impl Hash<u64> for ast::DefId { - fn hash(&self, state: &mut u64) { - let ast::DefId { krate, node } = *self; - // assert that these two types are each 32 bits - let krate: u32 = krate; - let node: u32 = node; - *state = (krate << 32) as u64 | (node as u64); - } -} - -// Hacks to get good names -pub mod NodeMap { - use collections::HashMap; - pub fn new<T>() -> super::NodeMap<T> { - HashMap::with_hasher(super::NodeHasher) - } -} -pub mod NodeSet { - use collections::HashSet; - pub fn new() -> super::NodeSet { - HashSet::with_hasher(super::NodeHasher) - } -} -pub mod DefIdMap { - use collections::HashMap; - pub fn new<T>() -> super::DefIdMap<T> { - HashMap::with_hasher(super::NodeHasher) - } -} -pub mod DefIdSet { - use collections::HashSet; - pub fn new() -> super::DefIdSet { - HashSet::with_hasher(super::NodeHasher) - } -} |
