1
Fork 0

Auto merge of #34772 - jseyfried:cleanup_interner, r=eddyb

Start cleaning up the string interner

r? @eddyb
This commit is contained in:
bors 2016-07-13 10:26:18 -07:00 committed by GitHub
commit 0b7fb80e1c
16 changed files with 146 additions and 396 deletions

View file

@ -211,7 +211,7 @@ pub fn compile_input(sess: &Session,
} }
// Discard interned strings as they are no longer required. // Discard interned strings as they are no longer required.
token::get_ident_interner().clear(); token::clear_ident_interner();
Ok((outputs, trans)) Ok((outputs, trans))
})?? })??
@ -480,7 +480,7 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
input: &Input) input: &Input)
-> PResult<'a, ast::Crate> { -> PResult<'a, ast::Crate> {
// These may be left in an incoherent state after a previous compile. // These may be left in an incoherent state after a previous compile.
// `clear_tables` and `get_ident_interner().clear()` can be used to free // `clear_tables` and `clear_ident_interner` can be used to free
// memory, but they do not restore the initial state. // memory, but they do not restore the initial state.
syntax::ext::mtwt::reset_tables(); syntax::ext::mtwt::reset_tables();
token::reset_ident_interner(); token::reset_ident_interner();

View file

@ -97,7 +97,7 @@ use rustc::session::early_error;
use syntax::{ast, json}; use syntax::{ast, json};
use syntax::codemap::{CodeMap, FileLoader, RealFileLoader}; use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
use syntax::feature_gate::{GatedCfg, UnstableFeatures}; use syntax::feature_gate::{GatedCfg, UnstableFeatures};
use syntax::parse::{self, PResult, token}; use syntax::parse::{self, PResult};
use syntax_pos::MultiSpan; use syntax_pos::MultiSpan;
use errors::emitter::Emitter; use errors::emitter::Emitter;
@ -201,7 +201,7 @@ pub fn run_compiler_with_file_loader<'a, L>(args: &[String],
}; };
let dep_graph = DepGraph::new(sopts.build_dep_graph()); let dep_graph = DepGraph::new(sopts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let codemap = Rc::new(CodeMap::with_file_loader(loader)); let codemap = Rc::new(CodeMap::with_file_loader(loader));
let sess = session::build_session_with_codemap(sopts, let sess = session::build_session_with_codemap(sopts,
&dep_graph, &dep_graph,
@ -432,7 +432,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
return None; return None;
} }
let dep_graph = DepGraph::new(sopts.build_dep_graph()); let dep_graph = DepGraph::new(sopts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = build_session(sopts.clone(), let sess = build_session(sopts.clone(),
&dep_graph, &dep_graph,
None, None,

View file

@ -106,7 +106,7 @@ fn test_env<F>(source_string: &str,
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore(); let _ignore = dep_graph.in_ignore();
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(options, &dep_graph, None, diagnostic_handler, let sess = session::build_session_(options, &dep_graph, None, diagnostic_handler,
Rc::new(CodeMap::new()), cstore.clone()); Rc::new(CodeMap::new()), cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

View file

@ -557,7 +557,6 @@ impl<'a> CrateReader<'a> {
let source_name = format!("<{} macros>", item.ident); let source_name = format!("<{} macros>", item.ident);
let mut macros = vec![]; let mut macros = vec![];
decoder::each_exported_macro(ekrate.metadata.as_slice(), decoder::each_exported_macro(ekrate.metadata.as_slice(),
&self.cstore.intr,
|name, attrs, span, body| { |name, attrs, span, body| {
// NB: Don't use parse::parse_tts_from_source_str because it parses with // NB: Don't use parse::parse_tts_from_source_str because it parses with
// quote_depth > 0. // quote_depth > 0.

View file

@ -127,7 +127,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
{ {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_adt_def(&self.intr, &cdata, def.index, tcx) decoder::get_adt_def(&cdata, def.index, tcx)
} }
fn method_arg_names(&self, did: DefId) -> Vec<String> fn method_arg_names(&self, did: DefId) -> Vec<String>
@ -140,13 +140,13 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
fn item_name(&self, def: DefId) -> ast::Name { fn item_name(&self, def: DefId) -> ast::Name {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_item_name(&self.intr, &cdata, def.index) decoder::get_item_name(&cdata, def.index)
} }
fn opt_item_name(&self, def: DefId) -> Option<ast::Name> { fn opt_item_name(&self, def: DefId) -> Option<ast::Name> {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::maybe_get_item_name(&self.intr, &cdata, def.index) decoder::maybe_get_item_name(&cdata, def.index)
} }
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
@ -176,7 +176,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
{ {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_provided_trait_methods(self.intr.clone(), &cdata, def.index, tcx) decoder::get_provided_trait_methods(&cdata, def.index, tcx)
} }
fn trait_item_def_ids(&self, def: DefId) fn trait_item_def_ids(&self, def: DefId)
@ -222,7 +222,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
-> Vec<Rc<ty::AssociatedConst<'tcx>>> { -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_associated_consts(self.intr.clone(), &cdata, def.index, tcx) decoder::get_associated_consts(&cdata, def.index, tcx)
} }
fn impl_parent(&self, impl_def: DefId) -> Option<DefId> { fn impl_parent(&self, impl_def: DefId) -> Option<DefId> {
@ -243,11 +243,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
{ {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_impl_or_trait_item( decoder::get_impl_or_trait_item(&cdata, def.index, tcx)
self.intr.clone(),
&cdata,
def.index,
tcx)
} }
fn is_const_fn(&self, did: DefId) -> bool fn is_const_fn(&self, did: DefId) -> bool
@ -460,7 +456,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
{ {
self.dep_graph.read(DepNode::MetaData(def)); self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate); let cdata = self.get_crate_data(def.krate);
decoder::get_struct_field_names(&self.intr, &cdata, def.index) decoder::get_struct_field_names(&cdata, def.index)
} }
fn item_children(&self, def_id: DefId) -> Vec<ChildItem> fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
@ -469,14 +465,9 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
let mut result = vec![]; let mut result = vec![];
let crate_data = self.get_crate_data(def_id.krate); let crate_data = self.get_crate_data(def_id.krate);
let get_crate_data = |cnum| self.get_crate_data(cnum); let get_crate_data = |cnum| self.get_crate_data(cnum);
decoder::each_child_of_item( decoder::each_child_of_item(&crate_data, def_id.index, get_crate_data, |def, name, vis| {
self.intr.clone(), &crate_data, result.push(ChildItem { def: def, name: name, vis: vis });
def_id.index, get_crate_data, });
|def, name, vis| result.push(ChildItem {
def: def,
name: name,
vis: vis
}));
result result
} }
@ -485,13 +476,9 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
let mut result = vec![]; let mut result = vec![];
let crate_data = self.get_crate_data(cnum); let crate_data = self.get_crate_data(cnum);
let get_crate_data = |cnum| self.get_crate_data(cnum); let get_crate_data = |cnum| self.get_crate_data(cnum);
decoder::each_top_level_item_of_crate( decoder::each_top_level_item_of_crate(&crate_data, get_crate_data, |def, name, vis| {
self.intr.clone(), &crate_data, get_crate_data, result.push(ChildItem { def: def, name: name, vis: vis });
|def, name, vis| result.push(ChildItem { });
def: def,
name: name,
vis: vis
}));
result result
} }

View file

@ -37,7 +37,6 @@ use flate::Bytes;
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use syntax::codemap; use syntax::codemap;
use syntax::parse::token::IdentInterner;
use syntax_pos; use syntax_pos;
pub use middle::cstore::{NativeLibraryKind, LinkagePreference}; pub use middle::cstore::{NativeLibraryKind, LinkagePreference};
@ -106,13 +105,11 @@ pub struct CStore {
used_libraries: RefCell<Vec<(String, NativeLibraryKind)>>, used_libraries: RefCell<Vec<(String, NativeLibraryKind)>>,
used_link_args: RefCell<Vec<String>>, used_link_args: RefCell<Vec<String>>,
statically_included_foreign_items: RefCell<NodeSet>, statically_included_foreign_items: RefCell<NodeSet>,
pub intr: Rc<IdentInterner>,
pub visible_parent_map: RefCell<DefIdMap<DefId>>, pub visible_parent_map: RefCell<DefIdMap<DefId>>,
} }
impl CStore { impl CStore {
pub fn new(dep_graph: &DepGraph, pub fn new(dep_graph: &DepGraph) -> CStore {
intr: Rc<IdentInterner>) -> CStore {
CStore { CStore {
dep_graph: dep_graph.clone(), dep_graph: dep_graph.clone(),
metas: RefCell::new(FnvHashMap()), metas: RefCell::new(FnvHashMap()),
@ -120,7 +117,6 @@ impl CStore {
used_crate_sources: RefCell::new(Vec::new()), used_crate_sources: RefCell::new(Vec::new()),
used_libraries: RefCell::new(Vec::new()), used_libraries: RefCell::new(Vec::new()),
used_link_args: RefCell::new(Vec::new()), used_link_args: RefCell::new(Vec::new()),
intr: intr,
statically_included_foreign_items: RefCell::new(NodeSet()), statically_included_foreign_items: RefCell::new(NodeSet()),
visible_parent_map: RefCell::new(FnvHashMap()), visible_parent_map: RefCell::new(FnvHashMap()),
} }

View file

@ -53,7 +53,7 @@ use rbml::reader;
use rbml; use rbml;
use rustc_serialize::Decodable; use rustc_serialize::Decodable;
use syntax::attr; use syntax::attr;
use syntax::parse::token::{self, IdentInterner}; use syntax::parse::token;
use syntax::ast; use syntax::ast;
use syntax::abi::Abi; use syntax::abi::Abi;
use syntax::codemap; use syntax::codemap;
@ -284,17 +284,14 @@ fn item_trait_ref<'a, 'tcx>(doc: rbml::Doc, tcx: TyCtxt<'a, 'tcx, 'tcx>, cdata:
doc_trait_ref(tp, tcx, cdata) doc_trait_ref(tp, tcx, cdata)
} }
fn item_name(intr: &IdentInterner, item: rbml::Doc) -> ast::Name { fn item_name(item: rbml::Doc) -> ast::Name {
maybe_item_name(intr, item).expect("no item in item_name") maybe_item_name(item).expect("no item in item_name")
} }
fn maybe_item_name(intr: &IdentInterner, item: rbml::Doc) -> Option<ast::Name> { fn maybe_item_name(item: rbml::Doc) -> Option<ast::Name> {
reader::maybe_get_doc(item, tag_paths_data_name).map(|name| { reader::maybe_get_doc(item, tag_paths_data_name).map(|name| {
let string = name.as_str_slice(); let string = name.as_str_slice();
match intr.find(string) { token::intern(string)
None => token::intern(string),
Some(val) => val,
}
}) })
} }
@ -400,8 +397,7 @@ pub fn get_trait_def<'a, 'tcx>(cdata: Cmd,
associated_type_names) associated_type_names)
} }
pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner, pub fn get_adt_def<'a, 'tcx>(cdata: Cmd,
cdata: Cmd,
item_id: DefIndex, item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>) tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::AdtDefMaster<'tcx> -> ty::AdtDefMaster<'tcx>
@ -412,9 +408,7 @@ pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner,
_ => bug!("unexpected family: {:?}", family), _ => bug!("unexpected family: {:?}", family),
} }
} }
fn get_enum_variants<'tcx>(intr: &IdentInterner, fn get_enum_variants<'tcx>(cdata: Cmd, doc: rbml::Doc) -> Vec<ty::VariantDefData<'tcx, 'tcx>> {
cdata: Cmd,
doc: rbml::Doc) -> Vec<ty::VariantDefData<'tcx, 'tcx>> {
let mut disr_val = 0; let mut disr_val = 0;
reader::tagged_docs(doc, tag_items_data_item_variant).map(|p| { reader::tagged_docs(doc, tag_items_data_item_variant).map(|p| {
let did = translated_def_id(cdata, p); let did = translated_def_id(cdata, p);
@ -428,16 +422,14 @@ pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner,
ty::VariantDefData { ty::VariantDefData {
did: did, did: did,
name: item_name(intr, item), name: item_name(item),
fields: get_variant_fields(intr, cdata, item), fields: get_variant_fields(cdata, item),
disr_val: ConstInt::Infer(disr), disr_val: ConstInt::Infer(disr),
kind: expect_variant_kind(item_family(item)), kind: expect_variant_kind(item_family(item)),
} }
}).collect() }).collect()
} }
fn get_variant_fields<'tcx>(intr: &IdentInterner, fn get_variant_fields<'tcx>(cdata: Cmd, doc: rbml::Doc) -> Vec<ty::FieldDefData<'tcx, 'tcx>> {
cdata: Cmd,
doc: rbml::Doc) -> Vec<ty::FieldDefData<'tcx, 'tcx>> {
let mut index = 0; let mut index = 0;
reader::tagged_docs(doc, tag_item_field).map(|f| { reader::tagged_docs(doc, tag_item_field).map(|f| {
let ff = item_family(f); let ff = item_family(f);
@ -446,24 +438,23 @@ pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner,
_ => bug!("expected field, found {:?}", ff) _ => bug!("expected field, found {:?}", ff)
}; };
ty::FieldDefData::new(item_def_id(f, cdata), ty::FieldDefData::new(item_def_id(f, cdata),
item_name(intr, f), item_name(f),
struct_field_family_to_visibility(ff)) struct_field_family_to_visibility(ff))
}).chain(reader::tagged_docs(doc, tag_item_unnamed_field).map(|f| { }).chain(reader::tagged_docs(doc, tag_item_unnamed_field).map(|f| {
let ff = item_family(f); let ff = item_family(f);
let name = intr.intern(&index.to_string()); let name = token::with_ident_interner(|interner| interner.intern(index.to_string()));
index += 1; index += 1;
ty::FieldDefData::new(item_def_id(f, cdata), name, ty::FieldDefData::new(item_def_id(f, cdata), name,
struct_field_family_to_visibility(ff)) struct_field_family_to_visibility(ff))
})).collect() })).collect()
} }
fn get_struct_variant<'tcx>(intr: &IdentInterner, fn get_struct_variant<'tcx>(cdata: Cmd,
cdata: Cmd,
doc: rbml::Doc, doc: rbml::Doc,
did: DefId) -> ty::VariantDefData<'tcx, 'tcx> { did: DefId) -> ty::VariantDefData<'tcx, 'tcx> {
ty::VariantDefData { ty::VariantDefData {
did: did, did: did,
name: item_name(intr, doc), name: item_name(doc),
fields: get_variant_fields(intr, cdata, doc), fields: get_variant_fields(cdata, doc),
disr_val: ConstInt::Infer(0), disr_val: ConstInt::Infer(0),
kind: expect_variant_kind(item_family(doc)), kind: expect_variant_kind(item_family(doc)),
} }
@ -475,7 +466,7 @@ pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner,
let (kind, variants) = match item_family(doc) { let (kind, variants) = match item_family(doc) {
Enum => { Enum => {
(ty::AdtKind::Enum, (ty::AdtKind::Enum,
get_enum_variants(intr, cdata, doc)) get_enum_variants(cdata, doc))
} }
Struct(..) => { Struct(..) => {
// Use separate constructor id for unit/tuple structs and reuse did for braced structs. // Use separate constructor id for unit/tuple structs and reuse did for braced structs.
@ -483,7 +474,7 @@ pub fn get_adt_def<'a, 'tcx>(intr: &IdentInterner,
translated_def_id(cdata, ctor_doc) translated_def_id(cdata, ctor_doc)
}); });
(ty::AdtKind::Struct, (ty::AdtKind::Struct,
vec![get_struct_variant(intr, cdata, doc, ctor_did.unwrap_or(did))]) vec![get_struct_variant(cdata, doc, ctor_did.unwrap_or(did))])
} }
_ => bug!("get_adt_def called on a non-ADT {:?} - {:?}", _ => bug!("get_adt_def called on a non-ADT {:?} - {:?}",
item_family(doc), did) item_family(doc), did)
@ -663,8 +654,7 @@ pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
}) })
} }
fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>, fn each_child_of_item_or_crate<F, G>(cdata: Cmd,
cdata: Cmd,
item_doc: rbml::Doc, item_doc: rbml::Doc,
mut get_crate_data: G, mut get_crate_data: G,
mut callback: F) where mut callback: F) where
@ -690,7 +680,7 @@ fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
// Get the item. // Get the item.
if let Some(child_item_doc) = crate_data.get_item(child_def_id.index) { if let Some(child_item_doc) = crate_data.get_item(child_def_id.index) {
// Hand off the item to the callback. // Hand off the item to the callback.
let child_name = item_name(&intr, child_item_doc); let child_name = item_name(child_item_doc);
let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id); let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
let visibility = item_visibility(child_item_doc); let visibility = item_visibility(child_item_doc);
callback(def_like, child_name, visibility); callback(def_like, child_name, visibility);
@ -711,7 +701,7 @@ fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
if let Some(impl_method_doc) = cdata.get_item(impl_item_def_id.index) { if let Some(impl_method_doc) = cdata.get_item(impl_item_def_id.index) {
if let StaticMethod = item_family(impl_method_doc) { if let StaticMethod = item_family(impl_method_doc) {
// Hand off the static method to the callback. // Hand off the static method to the callback.
let static_method_name = item_name(&intr, impl_method_doc); let static_method_name = item_name(impl_method_doc);
let static_method_def_like = item_to_def_like(cdata, impl_method_doc, let static_method_def_like = item_to_def_like(cdata, impl_method_doc,
impl_item_def_id); impl_item_def_id);
callback(static_method_def_like, callback(static_method_def_like,
@ -755,13 +745,9 @@ fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
} }
/// Iterates over each child of the given item. /// Iterates over each child of the given item.
pub fn each_child_of_item<F, G>(intr: Rc<IdentInterner>, pub fn each_child_of_item<F, G>(cdata: Cmd, id: DefIndex, get_crate_data: G, callback: F)
cdata: Cmd, where F: FnMut(DefLike, ast::Name, ty::Visibility),
id: DefIndex, G: FnMut(ast::CrateNum) -> Rc<CrateMetadata>,
get_crate_data: G,
callback: F) where
F: FnMut(DefLike, ast::Name, ty::Visibility),
G: FnMut(ast::CrateNum) -> Rc<CrateMetadata>,
{ {
// Find the item. // Find the item.
let item_doc = match cdata.get_item(id) { let item_doc = match cdata.get_item(id) {
@ -769,40 +755,31 @@ pub fn each_child_of_item<F, G>(intr: Rc<IdentInterner>,
Some(item_doc) => item_doc, Some(item_doc) => item_doc,
}; };
each_child_of_item_or_crate(intr, each_child_of_item_or_crate(cdata, item_doc, get_crate_data, callback)
cdata,
item_doc,
get_crate_data,
callback)
} }
/// Iterates over all the top-level crate items. /// Iterates over all the top-level crate items.
pub fn each_top_level_item_of_crate<F, G>(intr: Rc<IdentInterner>, pub fn each_top_level_item_of_crate<F, G>(cdata: Cmd, get_crate_data: G, callback: F)
cdata: Cmd, where F: FnMut(DefLike, ast::Name, ty::Visibility),
get_crate_data: G, G: FnMut(ast::CrateNum) -> Rc<CrateMetadata>,
callback: F) where
F: FnMut(DefLike, ast::Name, ty::Visibility),
G: FnMut(ast::CrateNum) -> Rc<CrateMetadata>,
{ {
let root_doc = rbml::Doc::new(cdata.data()); let root_doc = rbml::Doc::new(cdata.data());
let misc_info_doc = reader::get_doc(root_doc, tag_misc_info); let misc_info_doc = reader::get_doc(root_doc, tag_misc_info);
let crate_items_doc = reader::get_doc(misc_info_doc, let crate_items_doc = reader::get_doc(misc_info_doc,
tag_misc_info_crate_items); tag_misc_info_crate_items);
each_child_of_item_or_crate(intr, each_child_of_item_or_crate(cdata,
cdata,
crate_items_doc, crate_items_doc,
get_crate_data, get_crate_data,
callback) callback)
} }
pub fn get_item_name(intr: &IdentInterner, cdata: Cmd, id: DefIndex) -> ast::Name { pub fn get_item_name(cdata: Cmd, id: DefIndex) -> ast::Name {
item_name(intr, cdata.lookup_item(id)) item_name(cdata.lookup_item(id))
} }
pub fn maybe_get_item_name(intr: &IdentInterner, cdata: Cmd, id: DefIndex) pub fn maybe_get_item_name(cdata: Cmd, id: DefIndex) -> Option<ast::Name> {
-> Option<ast::Name> { maybe_item_name(cdata.lookup_item(id))
maybe_item_name(intr, cdata.lookup_item(id))
} }
pub fn maybe_get_item_ast<'a, 'tcx>(cdata: Cmd, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex) pub fn maybe_get_item_ast<'a, 'tcx>(cdata: Cmd, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex)
@ -955,12 +932,9 @@ pub fn get_impl_items(cdata: Cmd, impl_id: DefIndex)
}).collect() }).collect()
} }
pub fn get_trait_name(intr: Rc<IdentInterner>, pub fn get_trait_name(cdata: Cmd, id: DefIndex) -> ast::Name {
cdata: Cmd,
id: DefIndex)
-> ast::Name {
let doc = cdata.lookup_item(id); let doc = cdata.lookup_item(id);
item_name(&intr, doc) item_name(doc)
} }
pub fn is_static_method(cdata: Cmd, id: DefIndex) -> bool { pub fn is_static_method(cdata: Cmd, id: DefIndex) -> bool {
@ -973,10 +947,7 @@ pub fn is_static_method(cdata: Cmd, id: DefIndex) -> bool {
} }
} }
pub fn get_impl_or_trait_item<'a, 'tcx>(intr: Rc<IdentInterner>, pub fn get_impl_or_trait_item<'a, 'tcx>(cdata: Cmd, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
cdata: Cmd,
id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Option<ty::ImplOrTraitItem<'tcx>> { -> Option<ty::ImplOrTraitItem<'tcx>> {
let item_doc = cdata.lookup_item(id); let item_doc = cdata.lookup_item(id);
@ -993,7 +964,7 @@ pub fn get_impl_or_trait_item<'a, 'tcx>(intr: Rc<IdentInterner>,
_ => ImplContainer(container_id), _ => ImplContainer(container_id),
}; };
let name = item_name(&intr, item_doc); let name = item_name(item_doc);
let vis = item_visibility(item_doc); let vis = item_visibility(item_doc);
let defaultness = item_defaultness(item_doc); let defaultness = item_defaultness(item_doc);
@ -1068,8 +1039,7 @@ pub fn get_item_variances(cdata: Cmd, id: DefIndex) -> ty::ItemVariances {
Decodable::decode(&mut decoder).unwrap() Decodable::decode(&mut decoder).unwrap()
} }
pub fn get_provided_trait_methods<'a, 'tcx>(intr: Rc<IdentInterner>, pub fn get_provided_trait_methods<'a, 'tcx>(cdata: Cmd,
cdata: Cmd,
id: DefIndex, id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>) tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Vec<Rc<ty::Method<'tcx>>> { -> Vec<Rc<ty::Method<'tcx>>> {
@ -1080,10 +1050,7 @@ pub fn get_provided_trait_methods<'a, 'tcx>(intr: Rc<IdentInterner>,
let mth = cdata.lookup_item(did.index); let mth = cdata.lookup_item(did.index);
if item_sort(mth) == Some('p') { if item_sort(mth) == Some('p') {
let trait_item = get_impl_or_trait_item(intr.clone(), let trait_item = get_impl_or_trait_item(cdata, did.index, tcx);
cdata,
did.index,
tcx);
if let Some(ty::MethodTraitItem(ref method)) = trait_item { if let Some(ty::MethodTraitItem(ref method)) = trait_item {
Some((*method).clone()) Some((*method).clone())
} else { } else {
@ -1095,10 +1062,7 @@ pub fn get_provided_trait_methods<'a, 'tcx>(intr: Rc<IdentInterner>,
}).collect() }).collect()
} }
pub fn get_associated_consts<'a, 'tcx>(intr: Rc<IdentInterner>, pub fn get_associated_consts<'a, 'tcx>(cdata: Cmd, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
cdata: Cmd,
id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Vec<Rc<ty::AssociatedConst<'tcx>>> { -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
let item = cdata.lookup_item(id); let item = cdata.lookup_item(id);
@ -1109,10 +1073,7 @@ pub fn get_associated_consts<'a, 'tcx>(intr: Rc<IdentInterner>,
match item_sort(ac_doc) { match item_sort(ac_doc) {
Some('C') | Some('c') => { Some('C') | Some('c') => {
let trait_item = get_impl_or_trait_item(intr.clone(), let trait_item = get_impl_or_trait_item(cdata, did.index, tcx);
cdata,
did.index,
tcx);
if let Some(ty::ConstTraitItem(ref ac)) = trait_item { if let Some(ty::ConstTraitItem(ref ac)) = trait_item {
Some((*ac).clone()) Some((*ac).clone())
} else { } else {
@ -1180,14 +1141,13 @@ fn struct_field_family_to_visibility(family: Family) -> ty::Visibility {
} }
} }
pub fn get_struct_field_names(intr: &IdentInterner, cdata: Cmd, id: DefIndex) pub fn get_struct_field_names(cdata: Cmd, id: DefIndex) -> Vec<ast::Name> {
-> Vec<ast::Name> {
let item = cdata.lookup_item(id); let item = cdata.lookup_item(id);
let mut index = 0; let mut index = 0;
reader::tagged_docs(item, tag_item_field).map(|an_item| { reader::tagged_docs(item, tag_item_field).map(|an_item| {
item_name(intr, an_item) item_name(an_item)
}).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| { }).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| {
let name = intr.intern(&index.to_string()); let name = token::with_ident_interner(|interner| interner.intern(index.to_string()));
index += 1; index += 1;
name name
})).collect() })).collect()
@ -1503,12 +1463,12 @@ pub fn get_plugin_registrar_fn(data: &[u8]) -> Option<DefIndex> {
.map(|doc| DefIndex::from_u32(reader::doc_as_u32(doc))) .map(|doc| DefIndex::from_u32(reader::doc_as_u32(doc)))
} }
pub fn each_exported_macro<F>(data: &[u8], intr: &IdentInterner, mut f: F) where pub fn each_exported_macro<F>(data: &[u8], mut f: F) where
F: FnMut(ast::Name, Vec<ast::Attribute>, Span, String) -> bool, F: FnMut(ast::Name, Vec<ast::Attribute>, Span, String) -> bool,
{ {
let macros = reader::get_doc(rbml::Doc::new(data), tag_macro_defs); let macros = reader::get_doc(rbml::Doc::new(data), tag_macro_defs);
for macro_doc in reader::tagged_docs(macros, tag_macro_def) { for macro_doc in reader::tagged_docs(macros, tag_macro_def) {
let name = item_name(intr, macro_doc); let name = item_name(macro_doc);
let attrs = get_attributes(macro_doc); let attrs = get_attributes(macro_doc);
let span = get_macro_span(macro_doc); let span = get_macro_span(macro_doc);
let body = reader::get_doc(macro_doc, tag_macro_def_body); let body = reader::get_doc(macro_doc, tag_macro_def_body);

View file

@ -81,7 +81,7 @@ pub struct UniqueTypeId(ast::Name);
// UniqueTypeIds. // UniqueTypeIds.
pub struct TypeMap<'tcx> { pub struct TypeMap<'tcx> {
// The UniqueTypeIds created so far // The UniqueTypeIds created so far
unique_id_interner: Interner<Rc<String>>, unique_id_interner: Interner,
// A map from UniqueTypeId to debuginfo metadata for that type. This is a 1:1 mapping. // A map from UniqueTypeId to debuginfo metadata for that type. This is a 1:1 mapping.
unique_id_to_metadata: FnvHashMap<UniqueTypeId, DIType>, unique_id_to_metadata: FnvHashMap<UniqueTypeId, DIType>,
// A map from types to debuginfo metadata. This is a N:1 mapping. // A map from types to debuginfo metadata. This is a N:1 mapping.
@ -313,7 +313,7 @@ impl<'tcx> TypeMap<'tcx> {
// Trim to size before storing permanently // Trim to size before storing permanently
unique_type_id.shrink_to_fit(); unique_type_id.shrink_to_fit();
let key = self.unique_id_interner.intern(Rc::new(unique_type_id)); let key = self.unique_id_interner.intern(unique_type_id);
self.type_to_unique_id.insert(type_, UniqueTypeId(key)); self.type_to_unique_id.insert(type_, UniqueTypeId(key));
return UniqueTypeId(key); return UniqueTypeId(key);
@ -383,7 +383,7 @@ impl<'tcx> TypeMap<'tcx> {
let enum_variant_type_id = format!("{}::{}", let enum_variant_type_id = format!("{}::{}",
&self.get_unique_type_id_as_string(enum_type_id), &self.get_unique_type_id_as_string(enum_type_id),
variant_name); variant_name);
let interner_key = self.unique_id_interner.intern(Rc::new(enum_variant_type_id)); let interner_key = self.unique_id_interner.intern(enum_variant_type_id);
UniqueTypeId(interner_key) UniqueTypeId(interner_key)
} }
} }

View file

@ -32,7 +32,6 @@ use rustc::dep_graph::DepNode;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use syntax::ast::{self, NodeId}; use syntax::ast::{self, NodeId};
use syntax::{attr,errors}; use syntax::{attr,errors};
use syntax::parse::token;
use type_of; use type_of;
use glue; use glue;
use abi::{Abi, FnType}; use abi::{Abi, FnType};
@ -605,8 +604,8 @@ fn push_type_params<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
for projection in projections { for projection in projections {
let projection = projection.skip_binder(); let projection = projection.skip_binder();
let name = token::get_ident_interner().get(projection.projection_ty.item_name); let name = &projection.projection_ty.item_name.as_str();
output.push_str(&name[..]); output.push_str(name);
output.push_str("="); output.push_str("=");
push_unique_type_name(tcx, projection.ty, output); push_unique_type_name(tcx, projection.ty, output);
output.push_str(", "); output.push_str(", ");

View file

@ -24,7 +24,6 @@ use rustc_metadata::cstore::CStore;
use syntax::{ast, codemap}; use syntax::{ast, codemap};
use syntax::feature_gate::UnstableFeatures; use syntax::feature_gate::UnstableFeatures;
use syntax::parse::token;
use errors; use errors;
use errors::emitter::ColorConfig; use errors::emitter::ColorConfig;
@ -136,7 +135,7 @@ pub fn run_core(search_paths: SearchPaths,
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore(); let _ignore = dep_graph.in_ignore();
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts, &dep_graph, cpath, diagnostic_handler, let sess = session::build_session_(sessopts, &dep_graph, cpath, diagnostic_handler,
codemap, cstore.clone()); codemap, cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

View file

@ -37,7 +37,6 @@ use rustc_resolve::MakeGlobMap;
use syntax::codemap::CodeMap; use syntax::codemap::CodeMap;
use errors; use errors;
use errors::emitter::ColorConfig; use errors::emitter::ColorConfig;
use syntax::parse::token;
use core; use core;
use clean; use clean;
@ -82,7 +81,7 @@ pub fn run(input: &str,
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let _ignore = dep_graph.in_ignore(); let _ignore = dep_graph.in_ignore();
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts, let sess = session::build_session_(sessopts,
&dep_graph, &dep_graph,
Some(input_path.clone()), Some(input_path.clone()),
@ -239,7 +238,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter); let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
let dep_graph = DepGraph::new(false); let dep_graph = DepGraph::new(false);
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = session::build_session_(sessopts, let sess = session::build_session_(sessopts,
&dep_graph, &dep_graph,
None, None,

View file

@ -259,7 +259,6 @@ pub struct Parser<'a> {
pub restrictions: Restrictions, pub restrictions: Restrictions,
pub quote_depth: usize, // not (yet) related to the quasiquoter pub quote_depth: usize, // not (yet) related to the quasiquoter
pub reader: Box<Reader+'a>, pub reader: Box<Reader+'a>,
pub interner: Rc<token::IdentInterner>,
/// The set of seen errors about obsolete syntax. Used to suppress /// The set of seen errors about obsolete syntax. Used to suppress
/// extra detail when the same error is seen twice /// extra detail when the same error is seen twice
pub obsolete_set: HashSet<ObsoleteSyntax>, pub obsolete_set: HashSet<ObsoleteSyntax>,
@ -356,7 +355,6 @@ impl<'a> Parser<'a> {
Parser { Parser {
reader: rdr, reader: rdr,
interner: token::get_ident_interner(),
sess: sess, sess: sess,
cfg: cfg, cfg: cfg,
token: tok0.tok, token: tok0.tok,

View file

@ -17,11 +17,11 @@ pub use self::Token::*;
use ast::{self, BinOpKind}; use ast::{self, BinOpKind};
use ext::mtwt; use ext::mtwt;
use ptr::P; use ptr::P;
use util::interner::{RcStr, StrInterner}; use util::interner::Interner;
use util::interner;
use tokenstream; use tokenstream;
use serialize::{Decodable, Decoder, Encodable, Encoder}; use serialize::{Decodable, Decoder, Encodable, Encoder};
use std::cell::RefCell;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
@ -397,7 +397,7 @@ macro_rules! declare_keywords {(
} }
fn mk_fresh_ident_interner() -> IdentInterner { fn mk_fresh_ident_interner() -> IdentInterner {
interner::StrInterner::prefill(&[$($string,)*]) Interner::prefill(&[$($string,)*])
} }
}} }}
@ -473,22 +473,25 @@ declare_keywords! {
} }
// looks like we can get rid of this completely... // looks like we can get rid of this completely...
pub type IdentInterner = StrInterner; pub type IdentInterner = Interner;
// if an interner exists in TLS, return it. Otherwise, prepare a // if an interner exists in TLS, return it. Otherwise, prepare a
// fresh one. // fresh one.
// FIXME(eddyb) #8726 This should probably use a thread-local reference. // FIXME(eddyb) #8726 This should probably use a thread-local reference.
pub fn get_ident_interner() -> Rc<IdentInterner> { pub fn with_ident_interner<T, F: FnOnce(&mut IdentInterner) -> T>(f: F) -> T {
thread_local!(static KEY: Rc<::parse::token::IdentInterner> = { thread_local!(static KEY: RefCell<IdentInterner> = {
Rc::new(mk_fresh_ident_interner()) RefCell::new(mk_fresh_ident_interner())
}); });
KEY.with(|k| k.clone()) KEY.with(|interner| f(&mut *interner.borrow_mut()))
} }
/// Reset the ident interner to its initial state. /// Reset the ident interner to its initial state.
pub fn reset_ident_interner() { pub fn reset_ident_interner() {
let interner = get_ident_interner(); with_ident_interner(|interner| *interner = mk_fresh_ident_interner());
interner.reset(mk_fresh_ident_interner()); }
pub fn clear_ident_interner() {
with_ident_interner(|interner| *interner = IdentInterner::new());
} }
/// Represents a string stored in the thread-local interner. Because the /// Represents a string stored in the thread-local interner. Because the
@ -502,19 +505,19 @@ pub fn reset_ident_interner() {
/// somehow. /// somehow.
#[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)] #[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
pub struct InternedString { pub struct InternedString {
string: RcStr, string: Rc<String>,
} }
impl InternedString { impl InternedString {
#[inline] #[inline]
pub fn new(string: &'static str) -> InternedString { pub fn new(string: &'static str) -> InternedString {
InternedString { InternedString {
string: RcStr::new(string), string: Rc::new(string.to_owned()),
} }
} }
#[inline] #[inline]
fn new_from_rc_str(string: RcStr) -> InternedString { fn new_from_rc_str(string: Rc<String>) -> InternedString {
InternedString { InternedString {
string: string, string: string,
} }
@ -522,8 +525,7 @@ impl InternedString {
#[inline] #[inline]
pub fn new_from_name(name: ast::Name) -> InternedString { pub fn new_from_name(name: ast::Name) -> InternedString {
let interner = get_ident_interner(); with_ident_interner(|interner| InternedString::new_from_rc_str(interner.get(name)))
InternedString::new_from_rc_str(interner.get(name))
} }
} }
@ -611,13 +613,13 @@ pub fn intern_and_get_ident(s: &str) -> InternedString {
/// Maps a string to its interned representation. /// Maps a string to its interned representation.
#[inline] #[inline]
pub fn intern(s: &str) -> ast::Name { pub fn intern(s: &str) -> ast::Name {
get_ident_interner().intern(s) with_ident_interner(|interner| interner.intern(s))
} }
/// gensym's a new usize, using the current interner. /// gensym's a new usize, using the current interner.
#[inline] #[inline]
pub fn gensym(s: &str) -> ast::Name { pub fn gensym(s: &str) -> ast::Name {
get_ident_interner().gensym(s) with_ident_interner(|interner| interner.gensym(s))
} }
/// Maps a string to an identifier with an empty syntax context. /// Maps a string to an identifier with an empty syntax context.
@ -636,8 +638,7 @@ pub fn gensym_ident(s: &str) -> ast::Ident {
// note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src))); // note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src)));
// that is, that the new name and the old one are connected to ptr_eq strings. // that is, that the new name and the old one are connected to ptr_eq strings.
pub fn fresh_name(src: ast::Ident) -> ast::Name { pub fn fresh_name(src: ast::Ident) -> ast::Name {
let interner = get_ident_interner(); with_ident_interner(|interner| interner.gensym_copy(src.name))
interner.gensym_copy(src.name)
// following: debug version. Could work in final except that it's incompatible with // following: debug version. Could work in final except that it's incompatible with
// good error messages and uses of struct names in ambiguous could-be-binding // good error messages and uses of struct names in ambiguous could-be-binding
// locations. Also definitely destroys the guarantee given above about ptr_eq. // locations. Also definitely destroys the guarantee given above about ptr_eq.

View file

@ -15,212 +15,72 @@
use ast::Name; use ast::Name;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
pub struct Interner<T> { #[derive(PartialEq, Eq, Hash)]
map: RefCell<HashMap<T, Name>>, struct RcStr(Rc<String>);
vect: RefCell<Vec<T> >,
}
// when traits can extend traits, we should extend index<Name,T> to get []
impl<T: Eq + Hash + Clone + 'static> Interner<T> {
pub fn new() -> Interner<T> {
Interner {
map: RefCell::new(HashMap::new()),
vect: RefCell::new(Vec::new()),
}
}
pub fn prefill(init: &[T]) -> Interner<T> {
let rv = Interner::new();
for v in init {
rv.intern((*v).clone());
}
rv
}
pub fn intern(&self, val: T) -> Name {
let mut map = self.map.borrow_mut();
if let Some(&idx) = (*map).get(&val) {
return idx;
}
let mut vect = self.vect.borrow_mut();
let new_idx = Name((*vect).len() as u32);
(*map).insert(val.clone(), new_idx);
(*vect).push(val);
new_idx
}
pub fn gensym(&self, val: T) -> Name {
let mut vect = self.vect.borrow_mut();
let new_idx = Name((*vect).len() as u32);
// leave out of .map to avoid colliding
(*vect).push(val);
new_idx
}
pub fn get(&self, idx: Name) -> T {
let vect = self.vect.borrow();
(*vect)[idx.0 as usize].clone()
}
pub fn len(&self) -> usize {
let vect = self.vect.borrow();
(*vect).len()
}
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
where T: Borrow<Q>, Q: Eq + Hash {
let map = self.map.borrow();
match (*map).get(val) {
Some(v) => Some(*v),
None => None,
}
}
pub fn clear(&self) {
*self.map.borrow_mut() = HashMap::new();
*self.vect.borrow_mut() = Vec::new();
}
}
#[derive(Clone, PartialEq, Hash, PartialOrd)]
pub struct RcStr {
string: Rc<String>,
}
impl RcStr {
pub fn new(string: &str) -> RcStr {
RcStr {
string: Rc::new(string.to_string()),
}
}
}
impl Eq for RcStr {}
impl Ord for RcStr {
fn cmp(&self, other: &RcStr) -> Ordering {
self[..].cmp(&other[..])
}
}
impl fmt::Debug for RcStr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self[..].fmt(f)
}
}
impl fmt::Display for RcStr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self[..].fmt(f)
}
}
impl Borrow<str> for RcStr { impl Borrow<str> for RcStr {
fn borrow(&self) -> &str { fn borrow(&self) -> &str {
&self.string[..] &self.0
} }
} }
impl Deref for RcStr { #[derive(Default)]
type Target = str; pub struct Interner {
names: HashMap<RcStr, Name>,
fn deref(&self) -> &str { &self.string[..] } strings: Vec<Rc<String>>,
}
/// A StrInterner differs from Interner<String> in that it accepts
/// &str rather than RcStr, resulting in less allocation.
pub struct StrInterner {
map: RefCell<HashMap<RcStr, Name>>,
vect: RefCell<Vec<RcStr> >,
} }
/// When traits can extend traits, we should extend index<Name,T> to get [] /// When traits can extend traits, we should extend index<Name,T> to get []
impl StrInterner { impl Interner {
pub fn new() -> StrInterner { pub fn new() -> Self {
StrInterner { Interner::default()
map: RefCell::new(HashMap::new()), }
vect: RefCell::new(Vec::new()),
pub fn prefill(init: &[&str]) -> Self {
let mut this = Interner::new();
for &string in init {
this.intern(string);
} }
this
} }
pub fn prefill(init: &[&str]) -> StrInterner { pub fn intern<T: Borrow<str> + Into<String>>(&mut self, string: T) -> Name {
let rv = StrInterner::new(); if let Some(&name) = self.names.get(string.borrow()) {
for &v in init { rv.intern(v); } return name;
rv
}
pub fn intern(&self, val: &str) -> Name {
let mut map = self.map.borrow_mut();
if let Some(&idx) = map.get(val) {
return idx;
} }
let new_idx = Name(self.len() as u32); let name = Name(self.strings.len() as u32);
let val = RcStr::new(val); let string = Rc::new(string.into());
map.insert(val.clone(), new_idx); self.strings.push(string.clone());
self.vect.borrow_mut().push(val); self.names.insert(RcStr(string), name);
new_idx name
} }
pub fn gensym(&self, val: &str) -> Name { pub fn gensym(&mut self, string: &str) -> Name {
let new_idx = Name(self.len() as u32); let gensym = Name(self.strings.len() as u32);
// leave out of .map to avoid colliding // leave out of `names` to avoid colliding
self.vect.borrow_mut().push(RcStr::new(val)); self.strings.push(Rc::new(string.to_owned()));
new_idx gensym
} }
// I want these gensyms to share name pointers /// Create a gensym with the same name as an existing entry.
// with existing entries. This would be automatic, pub fn gensym_copy(&mut self, name: Name) -> Name {
// except that the existing gensym creates its let gensym = Name(self.strings.len() as u32);
// own managed ptr using to_managed. I think that // leave out of `names` to avoid colliding
// adding this utility function is the most let string = self.strings[name.0 as usize].clone();
// lightweight way to get what I want, though not self.strings.push(string);
// necessarily the cleanest. gensym
/// Create a gensym with the same name as an existing
/// entry.
pub fn gensym_copy(&self, idx : Name) -> Name {
let new_idx = Name(self.len() as u32);
// leave out of map to avoid colliding
let mut vect = self.vect.borrow_mut();
let existing = (*vect)[idx.0 as usize].clone();
vect.push(existing);
new_idx
} }
pub fn get(&self, idx: Name) -> RcStr { pub fn get(&self, name: Name) -> Rc<String> {
(*self.vect.borrow())[idx.0 as usize].clone() self.strings[name.0 as usize].clone()
} }
pub fn len(&self) -> usize { pub fn find(&self, string: &str) -> Option<Name> {
self.vect.borrow().len() self.names.get(string).cloned()
}
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
where RcStr: Borrow<Q>, Q: Eq + Hash {
match (*self.map.borrow()).get(val) {
Some(v) => Some(*v),
None => None,
}
}
pub fn clear(&self) {
*self.map.borrow_mut() = HashMap::new();
*self.vect.borrow_mut() = Vec::new();
}
pub fn reset(&self, other: StrInterner) {
*self.map.borrow_mut() = other.map.into_inner();
*self.vect.borrow_mut() = other.vect.into_inner();
} }
} }
@ -230,53 +90,8 @@ mod tests {
use ast::Name; use ast::Name;
#[test] #[test]
#[should_panic] fn interner_tests() {
fn i1 () { let mut i: Interner = Interner::new();
let i : Interner<RcStr> = Interner::new();
i.get(Name(13));
}
#[test]
fn interner_tests () {
let i : Interner<RcStr> = Interner::new();
// first one is zero:
assert_eq!(i.intern(RcStr::new("dog")), Name(0));
// re-use gets the same entry:
assert_eq!(i.intern(RcStr::new("dog")), Name(0));
// different string gets a different #:
assert_eq!(i.intern(RcStr::new("cat")), Name(1));
assert_eq!(i.intern(RcStr::new("cat")), Name(1));
// dog is still at zero
assert_eq!(i.intern(RcStr::new("dog")), Name(0));
// gensym gets 3
assert_eq!(i.gensym(RcStr::new("zebra") ), Name(2));
// gensym of same string gets new number :
assert_eq!(i.gensym (RcStr::new("zebra") ), Name(3));
// gensym of *existing* string gets new number:
assert_eq!(i.gensym(RcStr::new("dog")), Name(4));
assert_eq!(i.get(Name(0)), RcStr::new("dog"));
assert_eq!(i.get(Name(1)), RcStr::new("cat"));
assert_eq!(i.get(Name(2)), RcStr::new("zebra"));
assert_eq!(i.get(Name(3)), RcStr::new("zebra"));
assert_eq!(i.get(Name(4)), RcStr::new("dog"));
}
#[test]
fn i3 () {
let i : Interner<RcStr> = Interner::prefill(&[
RcStr::new("Alan"),
RcStr::new("Bob"),
RcStr::new("Carol")
]);
assert_eq!(i.get(Name(0)), RcStr::new("Alan"));
assert_eq!(i.get(Name(1)), RcStr::new("Bob"));
assert_eq!(i.get(Name(2)), RcStr::new("Carol"));
assert_eq!(i.intern(RcStr::new("Bob")), Name(1));
}
#[test]
fn string_interner_tests() {
let i : StrInterner = StrInterner::new();
// first one is zero: // first one is zero:
assert_eq!(i.intern("dog"), Name(0)); assert_eq!(i.intern("dog"), Name(0));
// re-use gets the same entry: // re-use gets the same entry:
@ -294,13 +109,13 @@ mod tests {
assert_eq!(i.gensym("dog"), Name(4)); assert_eq!(i.gensym("dog"), Name(4));
// gensym tests again with gensym_copy: // gensym tests again with gensym_copy:
assert_eq!(i.gensym_copy(Name(2)), Name(5)); assert_eq!(i.gensym_copy(Name(2)), Name(5));
assert_eq!(i.get(Name(5)), RcStr::new("zebra")); assert_eq!(*i.get(Name(5)), "zebra");
assert_eq!(i.gensym_copy(Name(2)), Name(6)); assert_eq!(i.gensym_copy(Name(2)), Name(6));
assert_eq!(i.get(Name(6)), RcStr::new("zebra")); assert_eq!(*i.get(Name(6)), "zebra");
assert_eq!(i.get(Name(0)), RcStr::new("dog")); assert_eq!(*i.get(Name(0)), "dog");
assert_eq!(i.get(Name(1)), RcStr::new("cat")); assert_eq!(*i.get(Name(1)), "cat");
assert_eq!(i.get(Name(2)), RcStr::new("zebra")); assert_eq!(*i.get(Name(2)), "zebra");
assert_eq!(i.get(Name(3)), RcStr::new("zebra")); assert_eq!(*i.get(Name(3)), "zebra");
assert_eq!(i.get(Name(4)), RcStr::new("dog")); assert_eq!(*i.get(Name(4)), "dog");
} }
} }

View file

@ -30,18 +30,16 @@ use std::thread::Builder;
use rustc::dep_graph::DepGraph; use rustc::dep_graph::DepGraph;
use rustc::hir::map as ast_map; use rustc::hir::map as ast_map;
use rustc::middle::cstore::{CrateStore, LinkagePreference}; use rustc::middle::cstore::LinkagePreference;
use rustc::ty; use rustc::ty;
use rustc::session::config::{self, basic_options, build_configuration, Input, Options}; use rustc::session::config::{self, basic_options, build_configuration, Input, Options};
use rustc::session::build_session; use rustc::session::build_session;
use rustc_driver::{driver, abort_on_err}; use rustc_driver::{driver, abort_on_err};
use rustc_resolve::MakeGlobMap; use rustc_resolve::MakeGlobMap;
use rustc_metadata::creader::read_local_crates;
use rustc_metadata::cstore::CStore; use rustc_metadata::cstore::CStore;
use libc::c_void; use libc::c_void;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use syntax::parse::token;
fn main() { fn main() {
// Currently trips an assertion on i686-msvc, presumably because the support // Currently trips an assertion on i686-msvc, presumably because the support
@ -226,7 +224,7 @@ fn compile_program(input: &str, sysroot: PathBuf)
let handle = thread.spawn(move || { let handle = thread.spawn(move || {
let opts = build_exec_options(sysroot); let opts = build_exec_options(sysroot);
let dep_graph = DepGraph::new(opts.build_dep_graph()); let dep_graph = DepGraph::new(opts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = build_session(opts, let sess = build_session(opts,
&dep_graph, &dep_graph,
None, None,

View file

@ -23,7 +23,6 @@ use rustc::session::config::{basic_options, build_configuration, Input, OutputTy
use rustc_driver::driver::{compile_input, CompileController, anon_src}; use rustc_driver::driver::{compile_input, CompileController, anon_src};
use rustc_metadata::cstore::CStore; use rustc_metadata::cstore::CStore;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use syntax::parse::token;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
@ -57,7 +56,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>) {
let descriptions = Registry::new(&rustc::DIAGNOSTICS); let descriptions = Registry::new(&rustc::DIAGNOSTICS);
let dep_graph = DepGraph::new(opts.build_dep_graph()); let dep_graph = DepGraph::new(opts.build_dep_graph());
let cstore = Rc::new(CStore::new(&dep_graph, token::get_ident_interner())); let cstore = Rc::new(CStore::new(&dep_graph));
let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone()); let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
(sess, cstore) (sess, cstore)