提交 449ebeea 编写于 作者: P Patrick Walton

librustc: De-`@mut` the AST map

上级 12ad1b06
......@@ -489,7 +489,8 @@ fn encode_reexported_static_methods(ecx: &EncodeContext,
ebml_w: &mut writer::Encoder,
mod_path: &[ast_map::path_elt],
exp: &middle::resolve::Export2) {
match ecx.tcx.items.find(&exp.def_id.node) {
let items = ecx.tcx.items.borrow();
match items.get().find(&exp.def_id.node) {
Some(&ast_map::node_item(item, path)) => {
let original_name = ecx.tcx.sess.str_of(item.ident);
......@@ -1338,7 +1339,8 @@ fn my_visit_item(i: @item,
ebml_w: &mut writer::Encoder,
ecx_ptr: *int,
index: @RefCell<~[entry<i64>]>) {
match items.get_copy(&i.id) {
let items = items.borrow();
match items.get().get_copy(&i.id) {
ast_map::node_item(_, pt) => {
let mut ebml_w = unsafe {
ebml_w.unsafe_clone()
......@@ -1356,7 +1358,8 @@ fn my_visit_foreign_item(ni: @foreign_item,
ebml_w: &mut writer::Encoder,
ecx_ptr:*int,
index: @RefCell<~[entry<i64>]>) {
match items.get_copy(&ni.id) {
let items = items.borrow();
match items.get().get_copy(&ni.id) {
ast_map::node_foreign_item(_, abi, _, pt) => {
debug!("writing foreign item {}::{}",
ast_map::path_to_str(
......
......@@ -788,7 +788,8 @@ pub fn append_loan_path_to_str(&self,
out: &mut ~str) {
match *loan_path {
LpVar(id) => {
match self.tcx.items.find(&id) {
let items = self.tcx.items.borrow();
match items.get().find(&id) {
Some(&ast_map::node_local(ref ident)) => {
out.push_str(token::ident_to_str(ident));
}
......
......@@ -266,13 +266,15 @@ fn visit_expr(&mut self, e: @Expr, _: ()) {
let def_map = self.env.def_map.borrow();
match def_map.get().find(&e.id) {
Some(&DefStatic(def_id, _)) if
ast_util::is_local(def_id) =>
match self.env.ast_map.get_copy(&def_id.node) {
ast_util::is_local(def_id) => {
let ast_map = self.env.ast_map.borrow();
match ast_map.get().get_copy(&def_id.node) {
ast_map::node_item(it, _) => {
self.visit_item(it, ());
}
_ => fail!("const not bound to an item")
},
}
}
_ => ()
}
},
......
......@@ -107,7 +107,9 @@ fn variant_expr(variants: &[ast::P<ast::variant>], id: ast::NodeId) -> Option<@E
}
if ast_util::is_local(enum_def) {
match tcx.items.find(&enum_def.node) {
{
let items = tcx.items.borrow();
match items.get().find(&enum_def.node) {
None => None,
Some(&ast_map::node_item(it, _)) => match it.node {
item_enum(ast::enum_def { variants: ref variants }, _) => {
......@@ -117,6 +119,7 @@ fn variant_expr(variants: &[ast::P<ast::variant>], id: ast::NodeId) -> Option<@E
},
Some(_) => None
}
}
} else {
{
let extern_const_variants = tcx.extern_const_variants.borrow();
......@@ -155,18 +158,22 @@ fn variant_expr(variants: &[ast::P<ast::variant>], id: ast::NodeId) -> Option<@E
}
}
pub fn lookup_const_by_id(tcx: ty::ctxt,
def_id: ast::DefId)
pub fn lookup_const_by_id(tcx: ty::ctxt, def_id: ast::DefId)
-> Option<@Expr> {
if ast_util::is_local(def_id) {
match tcx.items.find(&def_id.node) {
{
let items = tcx.items.borrow();
match items.get().find(&def_id.node) {
None => None,
Some(&ast_map::node_item(it, _)) => match it.node {
item_static(_, ast::MutImmutable, const_expr) => Some(const_expr),
item_static(_, ast::MutImmutable, const_expr) => {
Some(const_expr)
}
_ => None
},
Some(_) => None
}
}
} else {
{
let extern_const_statics = tcx.extern_const_statics.borrow();
......
......@@ -34,7 +34,9 @@ fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
if !is_local(def_id) {
return false;
}
match tcx.items.find(&def_id.node) {
let items = tcx.items.borrow();
match items.get().find(&def_id.node) {
Some(&ast_map::node_item(..))
| Some(&ast_map::node_method(..))
| Some(&ast_map::node_foreign_item(..))
......@@ -130,7 +132,9 @@ fn mark_live_symbols(&mut self) {
continue
}
scanned.insert(id);
match self.tcx.items.find(&id) {
let items = self.tcx.items.borrow();
match items.get().find(&id) {
Some(node) => {
self.live_symbols.insert(id);
self.visit_node(node);
......
......@@ -74,7 +74,9 @@ fn find_item(item: @item, ctxt: &mut EntryContext) {
match item.node {
item_fn(..) => {
if item.ident.name == special_idents::main.name {
match ctxt.ast_map.find(&item.id) {
{
let ast_map = ctxt.ast_map.borrow();
match ast_map.get().find(&item.id) {
Some(&ast_map::node_item(_, path)) => {
if path.len() == 0 {
// This is a top-level function so can be 'main'
......@@ -93,6 +95,7 @@ fn find_item(item: @item, ctxt: &mut EntryContext) {
_ => unreachable!()
}
}
}
if attr::contains_name(item.attrs, "main") {
if ctxt.attr_main_fn.is_none() {
......
......@@ -1238,7 +1238,8 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
let stability = if ast_util::is_local(id) {
// this crate
match cx.tcx.items.find(&id.node) {
let items = cx.tcx.items.borrow();
match items.get().find(&id.node) {
Some(ast_node) => {
let s = ast_node.with_attrs(|attrs| {
attrs.map(|a| {
......
......@@ -413,7 +413,8 @@ fn def_privacy(&self, did: ast::DefId) -> PrivacyResult {
let mut closest_private_id = did.node;
loop {
debug!("privacy - examining {}", self.nodestr(closest_private_id));
let vis = match self.tcx.items.find(&closest_private_id) {
let items = self.tcx.items.borrow();
let vis = match items.get().find(&closest_private_id) {
// If this item is a method, then we know for sure that it's an
// actual method and not a static method. The reason for this is
// that these cases are only hit in the ExprMethodCall
......@@ -519,7 +520,8 @@ fn ensure_public(&self, span: Span, to_check: ast::DefId,
self.tcx.sess.span_err(span, format!("{} is inaccessible",
msg));
}
match self.tcx.items.find(&id) {
let items = self.tcx.items.borrow();
match items.get().find(&id) {
Some(&ast_map::node_item(item, _)) => {
let desc = match item.node {
ast::item_mod(..) => "module",
......
......@@ -65,12 +65,17 @@ fn method_might_be_inlined(tcx: ty::ctxt, method: &ast::method,
return true
}
if is_local(impl_src) {
match tcx.items.find(&impl_src.node) {
Some(&ast_map::node_item(item, _)) => item_might_be_inlined(item),
{
let items = tcx.items.borrow();
match items.get().find(&impl_src.node) {
Some(&ast_map::node_item(item, _)) => {
item_might_be_inlined(item)
}
Some(..) | None => {
tcx.sess.span_bug(method.span, "impl did is not an item")
}
}
}
} else {
tcx.sess.span_bug(method.span, "found a foreign impl as a parent of a \
local method")
......@@ -208,7 +213,8 @@ fn def_id_represents_local_inlined_item(tcx: ty::ctxt, def_id: ast::DefId)
}
let node_id = def_id.node;
match tcx.items.find(&node_id) {
let items = tcx.items.borrow();
match items.get().find(&node_id) {
Some(&ast_map::node_item(item, _)) => {
match item.node {
ast::item_fn(..) => item_might_be_inlined(item),
......@@ -229,7 +235,7 @@ fn def_id_represents_local_inlined_item(tcx: ty::ctxt, def_id: ast::DefId)
// Check the impl. If the generics on the self type of the
// impl require inlining, this method does too.
assert!(impl_did.crate == ast::LOCAL_CRATE);
match tcx.items.find(&impl_did.node) {
match items.get().find(&impl_did.node) {
Some(&ast_map::node_item(item, _)) => {
match item.node {
ast::item_impl(ref generics, _, _, _) => {
......@@ -288,7 +294,8 @@ fn propagate(&self) {
};
scanned.insert(search_item);
match self.tcx.items.find(&search_item) {
let items = self.tcx.items.borrow();
match items.get().find(&search_item) {
Some(item) => self.propagate_node(item, search_item,
&mut visitor),
None if search_item == ast::CRATE_NODE_ID => {}
......
......@@ -2210,10 +2210,13 @@ fn visit_item(&mut self, i: @ast::item, _:()) {
pub fn trans_item(ccx: @CrateContext, item: &ast::item) {
let _icx = push_ctxt("trans_item");
let path = match ccx.tcx.items.get_copy(&item.id) {
let path = {
let items = ccx.tcx.items.borrow();
match items.get().get_copy(&item.id) {
ast_map::node_item(_, p) => p,
// tjc: ?
_ => fail!("trans_item"),
}
};
match item.node {
ast::item_fn(decl, purity, _abis, ref generics, body) => {
......@@ -2508,7 +2511,10 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::NodeId) -> ValueRef {
Some(v) => v,
None => {
let mut foreign = false;
let item = ccx.tcx.items.get_copy(&id);
let item = {
let items = ccx.tcx.items.borrow();
items.get().get_copy(&id)
};
let val = match item {
ast_map::node_item(i, pth) => {
......
......@@ -360,9 +360,11 @@ pub fn trans_fn_ref_with_vtables(
if type_params.len() > 0 || is_default {
must_monomorphise = true;
} else if def_id.crate == ast::LOCAL_CRATE {
{
let items = ccx.tcx.items.borrow();
let map_node = session::expect(
ccx.sess,
ccx.tcx.items.find(&def_id.node),
items.get().find(&def_id.node),
|| format!("local item should be in ast map"));
match *map_node {
......@@ -373,6 +375,7 @@ pub fn trans_fn_ref_with_vtables(
must_monomorphise = false;
}
}
}
} else {
must_monomorphise = false;
}
......
......@@ -165,7 +165,13 @@ pub fn get_const_val(cx: @CrateContext,
if !ast_util::is_local(def_id) {
def_id = inline::maybe_instantiate_inline(cx, def_id);
}
match cx.tcx.items.get_copy(&def_id.node) {
let opt_item = {
let items = cx.tcx.items.borrow();
items.get().get_copy(&def_id.node)
};
match opt_item {
ast_map::node_item(@ast::item {
node: ast::item_static(_, ast::MutImmutable, _), ..
}, _) => {
......
......@@ -323,7 +323,10 @@ pub fn create_captured_var_metadata(bcx: @Block,
let cx = bcx.ccx();
let ast_item = cx.tcx.items.find_copy(&node_id);
let ast_item = {
let items = cx.tcx.items.borrow();
items.get().find_copy(&node_id)
};
let variable_ident = match ast_item {
None => {
cx.sess.span_bug(span, "debuginfo::create_captured_var_metadata() - NodeId not found");
......@@ -422,7 +425,10 @@ pub fn create_self_argument_metadata(bcx: @Block,
}
// Extract the span of the self argument from the method's AST
let fnitem = bcx.ccx().tcx.items.get_copy(&bcx.fcx.id);
let fnitem = {
let items = bcx.ccx().tcx.items.borrow();
items.get().get_copy(&bcx.fcx.id)
};
let span = match fnitem {
ast_map::node_method(@ast::method { explicit_self: explicit_self, .. }, _, _) => {
explicit_self.span
......@@ -609,7 +615,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
let empty_generics = ast::Generics { lifetimes: opt_vec::Empty, ty_params: opt_vec::Empty };
let fnitem = cx.tcx.items.get_copy(&fn_ast_id);
let fnitem = {
let items = cx.tcx.items.borrow();
items.get().get_copy(&fn_ast_id)
};
let (ident, fn_decl, generics, top_level_block, span, has_path) = match fnitem {
ast_map::node_item(ref item, _) => {
match item.node {
......@@ -1092,7 +1101,8 @@ fn scope_metadata(fcx: &FunctionContext,
match scope_map.get().find_copy(&node_id) {
Some(scope_metadata) => scope_metadata,
None => {
let node = fcx.ccx.tcx.items.get_copy(&node_id);
let items = fcx.ccx.tcx.items.borrow();
let node = items.get().get_copy(&node_id);
fcx.ccx.sess.span_bug(span,
format!("debuginfo: Could not find scope info for node {:?}", node));
......@@ -1411,15 +1421,19 @@ fn describe_enum_variant(cx: &CrateContext,
// Find the source code location of the variant's definition
let variant_definition_span = if variant_info.id.crate == ast::LOCAL_CRATE {
match cx.tcx.items.find(&variant_info.id.node) {
{
let items = cx.tcx.items.borrow();
match items.get().find(&variant_info.id.node) {
Some(&ast_map::node_variant(ref variant, _, _)) => variant.span,
ref node => {
cx.sess.span_warn(span,
format!("debuginfo::enum_metadata()::adt_struct_metadata() - Unexpected node \
format!("debuginfo::enum_metadata()::\
adt_struct_metadata() - Unexpected node \
type: {:?}. This is a bug.", node));
codemap::DUMMY_SP
}
}
}
} else {
// For definitions from other crates we have no location information available.
codemap::DUMMY_SP
......@@ -2296,16 +2310,20 @@ fn get_namespace_and_span_for_item(cx: &CrateContext,
-> (DIScope, Span) {
let containing_scope = namespace_for_item(cx, def_id, warning_span).scope;
let definition_span = if def_id.crate == ast::LOCAL_CRATE {
let definition_span = match cx.tcx.items.find(&def_id.node) {
{
let items = cx.tcx.items.borrow();
let definition_span = match items.get().find(&def_id.node) {
Some(&ast_map::node_item(@ast::item { span, .. }, _)) => span,
ref node => {
cx.sess.span_warn(warning_span,
format!("debuginfo::get_namespace_and_span_for_item() \
format!("debuginfo::\
get_namespace_and_span_for_item() \
- Unexpected node type: {:?}", *node));
codemap::DUMMY_SP
}
};
definition_span
}
} else {
// For external items there is no span information
codemap::DUMMY_SP
......
......@@ -355,9 +355,16 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
for &foreign_item in foreign_mod.items.iter() {
match foreign_item.node {
ast::foreign_item_fn(..) => {
let (abis, mut path) = match ccx.tcx.items.get_copy(&foreign_item.id) {
ast_map::node_foreign_item(_, abis, _, path) => (abis, (*path).clone()),
_ => fail!("Unable to find foreign item in tcx.items table.")
let items = ccx.tcx.items.borrow();
let (abis, mut path) =
match items.get().get_copy(&foreign_item.id) {
ast_map::node_foreign_item(_, abis, _, path) => {
(abis, (*path).clone())
}
_ => {
fail!("Unable to find foreign item in tcx.items \
table.")
}
};
if !(abis.is_rust() || abis.is_intrinsic()) {
path.push(ast_map::path_name(foreign_item.ident));
......
......@@ -346,9 +346,12 @@ fn count_zeros_intrinsic(bcx: @Block, name: &'static str) {
let in_type_size = machine::llbitsize_of_real(ccx, llintype);
let out_type_size = machine::llbitsize_of_real(ccx, llouttype);
if in_type_size != out_type_size {
let sp = match ccx.tcx.items.get_copy(&ref_id.unwrap()) {
let sp = {
let items = ccx.tcx.items.borrow();
match items.get().get_copy(&ref_id.unwrap()) {
ast_map::node_expr(e) => e.span,
_ => fail!("transmute has non-expr arg"),
}
};
let pluralize = |n| if 1u == n { "" } else { "s" };
ccx.sess.span_fatal(sp,
......
......@@ -246,12 +246,15 @@ pub fn trans_static_method_callee(bcx: @Block,
generics.type_param_defs.len();
let mname = if method_id.crate == ast::LOCAL_CRATE {
match bcx.tcx().items.get_copy(&method_id.node) {
{
let items = bcx.tcx().items.borrow();
match items.get().get_copy(&method_id.node) {
ast_map::node_trait_method(trait_method, _, _) => {
ast_util::trait_method_to_ty_method(trait_method).ident
}
_ => fail!("callee is not a trait method")
}
}
} else {
let path = csearch::get_item_path(bcx.tcx(), method_id);
match path[path.len()-1] {
......
......@@ -95,12 +95,16 @@ pub fn monomorphic_fn(ccx: @CrateContext,
// calling a static provided method. This is sort of unfortunate.
let mut is_static_provided = None;
let map_node = session::expect(
let map_node = {
let items = ccx.tcx.items.borrow();
session::expect(
ccx.sess,
ccx.tcx.items.find_copy(&fn_id.node),
|| format!("While monomorphizing {:?}, couldn't find it in the item map \
(may have attempted to monomorphize an item \
defined in a different crate?)", fn_id));
items.get().find_copy(&fn_id.node),
|| format!("While monomorphizing {:?}, couldn't find it in the \
item map (may have attempted to monomorphize an item \
defined in a different crate?)", fn_id))
};
// Get the path so that we can create a symbol
let (pt, name, span) = match map_node {
ast_map::node_item(i, pt) => (pt, i.ident, i.span),
......
......@@ -3556,7 +3556,9 @@ pub fn provided_source(cx: ctxt, id: ast::DefId) -> Option<ast::DefId> {
pub fn provided_trait_methods(cx: ctxt, id: ast::DefId) -> ~[@Method] {
if is_local(id) {
match cx.items.find(&id.node) {
{
let items = cx.items.borrow();
match items.get().find(&id.node) {
Some(&ast_map::node_item(@ast::item {
node: item_trait(_, _, ref ms),
..
......@@ -3567,6 +3569,7 @@ pub fn provided_trait_methods(cx: ctxt, id: ast::DefId) -> ~[@Method] {
_ => cx.sess.bug(format!("provided_trait_methods: {:?} is not a trait",
id))
}
}
} else {
csearch::get_provided_trait_methods(cx, id)
}
......@@ -3675,7 +3678,9 @@ pub fn impl_trait_ref(cx: ctxt, id: ast::DefId) -> Option<@TraitRef> {
let ret = if id.crate == ast::LOCAL_CRATE {
debug!("(impl_trait_ref) searching for trait impl {:?}", id);
match cx.items.find(&id.node) {
{
let items = cx.items.borrow();
match items.get().find(&id.node) {
Some(&ast_map::node_item(@ast::item {
node: ast::item_impl(_, ref opt_trait, _, _),
..},
......@@ -3687,6 +3692,7 @@ pub fn impl_trait_ref(cx: ctxt, id: ast::DefId) -> Option<@TraitRef> {
}
_ => None
}
}
} else {
csearch::get_impl_trait(cx, id)
};
......@@ -3857,14 +3863,16 @@ pub fn has_dtor(cx: ctxt, struct_id: DefId) -> bool {
pub fn item_path(cx: ctxt, id: ast::DefId) -> ast_map::path {
if id.crate != ast::LOCAL_CRATE {
csearch::get_item_path(cx, id)
} else {
return csearch::get_item_path(cx, id)
}
// FIXME (#5521): uncomment this code and don't have a catch-all at the
// end of the match statement. Favor explicitly listing
// each variant.
// let node = cx.items.get(&id.node);
// match *node {
match *cx.items.get(&id.node) {
let items = cx.items.borrow();
match *items.get().get(&id.node) {
ast_map::node_item(item, path) => {
let item_elt = match item.node {
item_mod(_) | item_foreign_mod(_) => {
......@@ -3905,7 +3913,6 @@ pub fn item_path(cx: ctxt, id: ast::DefId) -> ast_map::path {
cx.sess.bug(format!("cannot find item_path for node {:?}", node));
}
}
}
}
pub fn enum_is_univariant(cx: ctxt, id: ast::DefId) -> bool {
......@@ -3936,7 +3943,9 @@ pub fn enum_variants(cx: ctxt, id: ast::DefId) -> @~[@VariantInfo] {
call eval_const_expr, it should never get called twice for the same
expr, since check_enum_variants also updates the enum_var_cache
*/
match cx.items.get_copy(&id.node) {
{
let items = cx.items.borrow();
match items.get().get_copy(&id.node) {
ast_map::node_item(@ast::item {
node: ast::item_enum(ref enum_definition, _),
..
......@@ -3971,6 +3980,7 @@ pub fn enum_variants(cx: ctxt, id: ast::DefId) -> @~[@VariantInfo] {
}
_ => cx.sess.bug("enum_variants: id not bound to an enum")
}
}
};
{
......@@ -4040,12 +4050,18 @@ pub fn lookup_trait_def(cx: ctxt, did: ast::DefId) -> @ty::TraitDef {
// decoder to use iterators instead of higher-order functions.)
pub fn each_attr(tcx: ctxt, did: DefId, f: |@MetaItem| -> bool) -> bool {
if is_local(did) {
match tcx.items.find(&did.node) {
Some(&ast_map::node_item(@ast::item {attrs: ref attrs, ..}, _)) =>
{
let items = tcx.items.borrow();
match items.get().find(&did.node) {
Some(&ast_map::node_item(@ast::item {
attrs: ref attrs,
..
}, _)) =>
attrs.iter().advance(|attr| f(attr.node.value)),
_ => tcx.sess.bug(format!("has_attr: {:?} is not an item",
did))
}
}
} else {
let mut cont = true;
csearch::get_item_attrs(tcx.cstore, did, |meta_items| {
......@@ -4120,7 +4136,9 @@ pub fn lookup_field_type(tcx: ctxt,
// Fails if the id is not bound to a struct.
pub fn lookup_struct_fields(cx: ctxt, did: ast::DefId) -> ~[field_ty] {
if did.crate == ast::LOCAL_CRATE {
match cx.items.find(&did.node) {
{
let items = cx.items.borrow();
match items.get().find(&did.node) {
Some(&ast_map::node_item(i,_)) => {
match i.node {
ast::item_struct(struct_def, _) => {
......@@ -4148,7 +4166,7 @@ pub fn lookup_struct_fields(cx: ctxt, did: ast::DefId) -> ~[field_ty] {
}
}
}
else {
} else {
return csearch::get_struct_fields(cx.sess.cstore, did);
}
}
......@@ -4658,7 +4676,8 @@ pub fn populate_implementations_for_trait_if_necessary(
/// If it implements no trait, return `None`.
pub fn trait_id_of_impl(tcx: ctxt,
def_id: ast::DefId) -> Option<ast::DefId> {
let node = match tcx.items.find(&def_id.node) {
let items = tcx.items.borrow();
let node = match items.get().find(&def_id.node) {
Some(node) => node,
None => return None
};
......
......@@ -1309,11 +1309,18 @@ fn report_candidate(&self, idx: uint, origin: &method_origin) {
fn report_static_candidate(&self, idx: uint, did: DefId) {
let span = if did.crate == ast::LOCAL_CRATE {
match self.tcx().items.find(&did.node) {
{
let items = self.tcx().items.borrow();
match items.get().find(&did.node) {
Some(&ast_map::node_method(m, _, _))
| Some(&ast_map::node_trait_method(@ast::provided(m), _, _)) => m.span,
| Some(&ast_map::node_trait_method(@ast::provided(m),
_,
_)) => {
m.span
}
_ => fail!("report_static_candidate: bad item {:?}", did)
}
}
} else {
self.expr.span
};
......
......@@ -568,10 +568,8 @@ pub fn ast_type_is_defined_in_local_crate(&self, original_type: &ast::Ty)
// Make sure that this type precisely names a nominal
// type.
match self.crate_context
.tcx
.items
.find(&def_id.node) {
let items = self.crate_context.tcx.items.borrow();
match items.get().find(&def_id.node) {
None => {
self.crate_context.tcx.sess.span_bug(
original_type.span,
......@@ -628,7 +626,8 @@ pub fn create_impl_from_item(&self, item: @item) -> @Impl {
pub fn span_of_impl(&self, implementation: @Impl) -> Span {
assert_eq!(implementation.did.crate, LOCAL_CRATE);
match self.crate_context.tcx.items.find(&implementation.did.node) {
let items = self.crate_context.tcx.items.borrow();
match items.get().find(&implementation.did.node) {
Some(&node_item(item, _)) => {
return item.span;
}
......@@ -732,14 +731,19 @@ pub fn populate_destructor_table(&self) {
_ => {
// Destructors only work on nominal types.
if impl_info.did.crate == ast::LOCAL_CRATE {
match tcx.items.find(&impl_info.did.node) {
{
let items = tcx.items.borrow();
match items.get().find(&impl_info.did.node) {
Some(&ast_map::node_item(@ref item, _)) => {
tcx.sess.span_err((*item).span,
"the Drop trait may only be implemented on \
structures");
"the Drop trait may \
only be implemented \
on structures");
}
_ => {
tcx.sess.bug("didn't find impl in ast map");
tcx.sess.bug("didn't find impl in ast \
map");
}
}
}
} else {
......
......@@ -106,12 +106,12 @@ fn tcx(&self) -> ty::ctxt { self.tcx }
fn get_item_ty(&self, id: ast::DefId) -> ty::ty_param_bounds_and_ty {
if id.crate != ast::LOCAL_CRATE {
csearch::get_type(self.tcx, id)
} else {
match self.tcx.items.find(&id.node) {
Some(&ast_map::node_item(item, _)) => {
ty_of_item(self, item)
return csearch::get_type(self.tcx, id)
}
let items = self.tcx.items.borrow();
match items.get().find(&id.node) {
Some(&ast_map::node_item(item, _)) => ty_of_item(self, item),
Some(&ast_map::node_foreign_item(foreign_item, abis, _, _)) => {
ty_of_foreign_item(self, foreign_item, abis)
}
......@@ -121,7 +121,6 @@ fn get_item_ty(&self, id: ast::DefId) -> ty::ty_param_bounds_and_ty {
}
}
}
}
fn get_trait_def(&self, id: ast::DefId) -> @ty::TraitDef {
get_trait_def(self, id)
......@@ -187,7 +186,8 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
trait_id: ast::NodeId)
{
let tcx = ccx.tcx;
match tcx.items.get_copy(&trait_id) {
let items = tcx.items.borrow();
match items.get().get_copy(&trait_id) {
ast_map::node_item(@ast::item {
node: ast::item_trait(ref generics, _, ref ms),
..
......@@ -715,7 +715,8 @@ pub fn convert_foreign(ccx: &CrateCtxt, i: &ast::foreign_item) {
// map, and I regard each time that I use it as a personal and
// moral failing, but at the moment it seems like the only
// convenient way to extract the ABI. - ndm
let abis = match ccx.tcx.items.find(&i.id) {
let items = ccx.tcx.items.borrow();
let abis = match items.get().find(&i.id) {
Some(&ast_map::node_foreign_item(_, abis, _, _)) => abis,
ref x => {
ccx.tcx.sess.bug(format!("unexpected sort of item \
......@@ -765,14 +766,15 @@ pub fn instantiate_trait_ref(ccx: &CrateCtxt,
fn get_trait_def(ccx: &CrateCtxt, trait_id: ast::DefId) -> @ty::TraitDef {
if trait_id.crate != ast::LOCAL_CRATE {
ty::lookup_trait_def(ccx.tcx, trait_id)
} else {
match ccx.tcx.items.get(&trait_id.node) {
return ty::lookup_trait_def(ccx.tcx, trait_id)
}
let items = ccx.tcx.items.borrow();
match items.get().get(&trait_id.node) {
&ast_map::node_item(item, _) => trait_def_of_item(ccx, item),
_ => ccx.tcx.sess.bug(format!("get_trait_def({}): not an item",
trait_id.node))
}
}
}
pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::item) -> @ty::TraitDef {
......
......@@ -350,7 +350,8 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
let main_t = ty::node_id_to_type(tcx, main_id);
match ty::get(main_t).sty {
ty::ty_bare_fn(..) => {
match tcx.items.find(&main_id) {
let items = tcx.items.borrow();
match items.get().find(&main_id) {
Some(&ast_map::node_item(it,_)) => {
match it.node {
ast::item_fn(_, _, _, ref ps, _)
......@@ -395,7 +396,8 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
let start_t = ty::node_id_to_type(tcx, start_id);
match ty::get(start_t).sty {
ty::ty_bare_fn(_) => {
match tcx.items.find(&start_id) {
let items = tcx.items.borrow();
match items.get().find(&start_id) {
Some(&ast_map::node_item(it,_)) => {
match it.node {
ast::item_fn(_,_,_,ref ps,_)
......
......@@ -72,7 +72,8 @@ pub fn explain_region_and_span(cx: ctxt, region: ty::Region)
-> (~str, Option<Span>) {
return match region {
ReScope(node_id) => {
match cx.items.find(&node_id) {
let items = cx.items.borrow();
match items.get().find(&node_id) {
Some(&ast_map::node_block(ref blk)) => {
explain_span(cx, "block", blk.span)
}
......@@ -113,7 +114,8 @@ pub fn explain_region_and_span(cx: ctxt, region: ty::Region)
bound_region_ptr_to_str(cx, fr.bound_region))
};
match cx.items.find(&fr.scope_id) {
let items = cx.items.borrow();
match items.get().find(&fr.scope_id) {
Some(&ast_map::node_block(ref blk)) => {
let (msg, opt_span) = explain_span(cx, "block", blk.span);
(format!("{} {}", prefix, msg), opt_span)
......@@ -172,7 +174,8 @@ pub fn bound_region_to_str(cx: ctxt,
}
pub fn ReScope_id_to_str(cx: ctxt, node_id: ast::NodeId) -> ~str {
match cx.items.find(&node_id) {
let items = cx.items.borrow();
match items.get().find(&node_id) {
Some(&ast_map::node_block(ref blk)) => {
format!("<block at {}>",
cx.sess.codemap.span_to_str(blk.span))
......@@ -740,18 +743,23 @@ fn repr(&self, tcx: ctxt) -> ~str {
// a path for a def-id, so I'll just make a best effort for now
// and otherwise fallback to just printing the crate/node pair
if self.crate == ast::LOCAL_CRATE {
match tcx.items.find(&self.node) {
{
let items = tcx.items.borrow();
match items.get().find(&self.node) {
Some(&ast_map::node_item(..)) |
Some(&ast_map::node_foreign_item(..)) |
Some(&ast_map::node_method(..)) |
Some(&ast_map::node_trait_method(..)) |
Some(&ast_map::node_variant(..)) |
Some(&ast_map::node_struct_ctor(..)) => {
return format!("{:?}:{}", *self, ty::item_path_str(tcx, *self));
return format!("{:?}:{}",
*self,
ty::item_path_str(tcx, *self));
}
_ => {}
}
}
}
return format!("{:?}", *self);
}
}
......
......@@ -23,6 +23,7 @@
use visit::{Visitor, fn_kind};
use visit;
use std::cell::RefCell;
use std::hashmap::HashMap;
use std::vec;
......@@ -192,7 +193,7 @@ pub fn with_attrs<T>(&self, f: |Option<&[Attribute]>| -> T) -> T {
}
}
pub type map = @mut HashMap<NodeId, ast_node>;
pub type map = @RefCell<HashMap<NodeId, ast_node>>;
pub struct Ctx {
map: map,
......@@ -215,8 +216,10 @@ fn map_method(&mut self,
} else {
node_method(m, impl_did, impl_path)
};
self.map.insert(m.id, entry);
self.map.insert(m.self_id, node_local(special_idents::self_));
let mut map = self.map.borrow_mut();
map.get().insert(m.id, entry);
map.get().insert(m.self_id, node_local(special_idents::self_));
}
fn map_struct_def(&mut self,
......@@ -231,7 +234,8 @@ fn map_struct_def(&mut self,
Some(ctor_id) => {
match parent_node {
node_item(item, _) => {
self.map.insert(ctor_id,
let mut map = self.map.borrow_mut();
map.get().insert(ctor_id,
node_struct_ctor(struct_def,
item,
p));
......@@ -243,13 +247,17 @@ fn map_struct_def(&mut self,
}
fn map_expr(&mut self, ex: @Expr) {
self.map.insert(ex.id, node_expr(ex));
{
let mut map = self.map.borrow_mut();
map.get().insert(ex.id, node_expr(ex));
}
// Expressions which are or might be calls:
{
let r = ex.get_callee_id();
for callee_id in r.iter() {
self.map.insert(*callee_id, node_callee_scope(ex));
let mut map = self.map.borrow_mut();
map.get().insert(*callee_id, node_callee_scope(ex));
}
}
......@@ -263,7 +271,8 @@ fn map_fn(&mut self,
sp: codemap::Span,
id: NodeId) {
for a in decl.inputs.iter() {
self.map.insert(a.id, node_arg(a.pat));
let mut map = self.map.borrow_mut();
map.get().insert(a.id, node_arg(a.pat));
}
match *fk {
visit::fk_method(name, _, _) => { self.path.push(path_name(name)) }
......@@ -277,12 +286,19 @@ fn map_fn(&mut self,
}
fn map_stmt(&mut self, stmt: @Stmt) {
self.map.insert(stmt_id(stmt), node_stmt(stmt));
{
let mut map = self.map.borrow_mut();
map.get().insert(stmt_id(stmt), node_stmt(stmt));
}
visit::walk_stmt(self, stmt, ());
}
fn map_block(&mut self, b: P<Block>) {
self.map.insert(b.id, node_block(b));
{
let mut map = self.map.borrow_mut();
map.get().insert(b.id, node_block(b));
}
visit::walk_block(self, b, ());
}
......@@ -290,7 +306,8 @@ fn map_pat(&mut self, pat: &Pat) {
match pat.node {
PatIdent(_, ref path, _) => {
// Note: this is at least *potentially* a pattern...
self.map.insert(pat.id,
let mut map = self.map.borrow_mut();
map.get().insert(pat.id,
node_local(ast_util::path_to_ident(path)));
}
_ => ()
......@@ -304,7 +321,10 @@ impl Visitor<()> for Ctx {
fn visit_item(&mut self, i: @item, _: ()) {
// clone is FIXME #2543
let item_path = @self.path.clone();
self.map.insert(i.id, node_item(i, item_path));
{
let mut map = self.map.borrow_mut();
map.get().insert(i.id, node_item(i, item_path));
}
match i.node {
item_impl(_, ref maybe_trait, ty, ref ms) => {
// Right now the ident on impls is __extensions__ which isn't
......@@ -323,7 +343,8 @@ fn visit_item(&mut self, i: @item, _: ()) {
item_enum(ref enum_definition, _) => {
for &v in enum_definition.variants.iter() {
let elt = path_name(i.ident);
self.map.insert(v.node.id,
let mut map = self.map.borrow_mut();
map.get().insert(v.node.id,
node_variant(v, i, self.extend(elt)));
}
}
......@@ -336,7 +357,8 @@ fn visit_item(&mut self, i: @item, _: ()) {
inherited => i.vis
};
self.map.insert(nitem.id,
let mut map = self.map.borrow_mut();
map.get().insert(nitem.id,
node_foreign_item(*nitem,
nm.abis,
visibility,
......@@ -355,7 +377,8 @@ fn visit_item(&mut self, i: @item, _: ()) {
}
item_trait(_, ref traits, ref methods) => {
for p in traits.iter() {
self.map.insert(p.ref_id, node_item(i, item_path));
let mut map = self.map.borrow_mut();
map.get().insert(p.ref_id, node_item(i, item_path));
}
for tm in methods.iter() {
let ext = { self.extend(path_name(i.ident)) };
......@@ -364,7 +387,8 @@ fn visit_item(&mut self, i: @item, _: ()) {
required(ref m) => {
let entry =
node_trait_method(@(*tm).clone(), d_id, ext);
self.map.insert(m.id, entry);
let mut map = self.map.borrow_mut();
map.get().insert(m.id, entry);
}
provided(m) => {
self.map_method(d_id, ext, m, true);
......@@ -420,7 +444,7 @@ fn visit_ty(&mut self, typ: &Ty, _: ()) {
pub fn map_crate(diag: @SpanHandler, c: &Crate) -> map {
let cx = @mut Ctx {
map: @mut HashMap::new(),
map: @RefCell::new(HashMap::new()),
path: ~[],
diag: diag,
};
......@@ -450,7 +474,8 @@ pub fn map_decoded_item(diag: @SpanHandler,
match *ii {
ii_item(..) => {} // fallthrough
ii_foreign(i) => {
cx.map.insert(i.id, node_foreign_item(i,
let mut map = cx.map.borrow_mut();
map.get().insert(i.id, node_foreign_item(i,
AbiSet::Intrinsic(),
i.vis, // Wrong but OK
@path));
......@@ -465,7 +490,8 @@ pub fn map_decoded_item(diag: @SpanHandler,
}
pub fn node_id_to_str(map: map, id: NodeId, itr: @ident_interner) -> ~str {
match map.find(&id) {
let map = map.borrow();
match map.get().find(&id) {
None => {
format!("unknown node (id={})", id)
}
......@@ -529,7 +555,8 @@ pub fn node_id_to_str(map: map, id: NodeId, itr: @ident_interner) -> ~str {
pub fn node_item_query<Result>(items: map, id: NodeId, query: |@item| -> Result, error_msg: ~str)
-> Result {
match items.find(&id) {
let items = items.borrow();
match items.get().find(&id) {
Some(&node_item(it, _)) => query(it),
_ => fail!("{}", error_msg)
}
......@@ -538,7 +565,8 @@ pub fn node_item_query<Result>(items: map, id: NodeId, query: |@item| -> Result,
pub fn node_span(items: map,
id: ast::NodeId)
-> Span {
match items.find(&id) {
let items = items.borrow();
match items.get().find(&id) {
Some(&node_item(item, _)) => item.span,
Some(&node_foreign_item(foreign_item, _, _, _)) => foreign_item.span,
Some(&node_trait_method(@required(ref type_method), _, _)) => type_method.span,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册