librustc: Refactor language items to have IDs. rs=refactor

Preparation for fixing the performance problems with language item collection.
This commit is contained in:
Patrick Walton 2013-01-06 12:05:34 -08:00
parent 96e01a67c7
commit 0ab7f9129e
9 changed files with 171 additions and 124 deletions

View File

@ -48,6 +48,7 @@ export get_type;
export get_impl_traits;
export get_impl_method;
export get_item_path;
export get_lang_items;
export maybe_get_item_ast, found_ast, found, found_parent, not_found;
export ProvidedTraitMethodInfo;
export StaticMethodInfo;

View File

@ -33,104 +33,157 @@ use core::ptr;
use std::map::HashMap;
use str_eq = str::eq;
struct LanguageItems {
mut const_trait: Option<def_id>,
mut copy_trait: Option<def_id>,
mut owned_trait: Option<def_id>,
mut durable_trait: Option<def_id>,
pub enum LangItem {
ConstTraitLangItem, // 0
CopyTraitLangItem, // 1
OwnedTraitLangItem, // 2
DurableTraitLangItem, // 3
mut drop_trait: Option<def_id>,
DropTraitLangItem, // 4
mut add_trait: Option<def_id>,
mut sub_trait: Option<def_id>,
mut mul_trait: Option<def_id>,
mut div_trait: Option<def_id>,
mut modulo_trait: Option<def_id>,
mut neg_trait: Option<def_id>,
mut bitxor_trait: Option<def_id>,
mut bitand_trait: Option<def_id>,
mut bitor_trait: Option<def_id>,
mut shl_trait: Option<def_id>,
mut shr_trait: Option<def_id>,
mut index_trait: Option<def_id>,
AddTraitLangItem, // 5
SubTraitLangItem, // 6
MulTraitLangItem, // 7
DivTraitLangItem, // 8
ModuloTraitLangItem, // 9
NegTraitLangItem, // 10
BitXorTraitLangItem, // 11
BitAndTraitLangItem, // 12
BitOrTraitLangItem, // 13
ShlTraitLangItem, // 14
ShrTraitLangItem, // 15
IndexTraitLangItem, // 16
mut eq_trait: Option<def_id>,
mut ord_trait: Option<def_id>,
EqTraitLangItem, // 17
OrdTraitLangItem, // 18
mut str_eq_fn: Option<def_id>,
mut uniq_str_eq_fn: Option<def_id>,
mut annihilate_fn: Option<def_id>,
mut log_type_fn: Option<def_id>
StrEqFnLangItem, // 19
UniqStrEqFnLangItem, // 20
AnnihilateFnLangItem, // 21
LogTypeFnLangItem, // 22
}
mod language_items {
#[legacy_exports];
fn make() -> LanguageItems {
struct LanguageItems {
items: [ Option<def_id> * 23 ]
}
impl LanguageItems {
static pub fn new() -> LanguageItems {
LanguageItems {
const_trait: None,
copy_trait: None,
owned_trait: None,
durable_trait: None,
drop_trait: None,
add_trait: None,
sub_trait: None,
mul_trait: None,
div_trait: None,
modulo_trait: None,
neg_trait: None,
bitxor_trait: None,
bitand_trait: None,
bitor_trait: None,
shl_trait: None,
shr_trait: None,
index_trait: None,
eq_trait: None,
ord_trait: None,
str_eq_fn: None,
uniq_str_eq_fn: None,
annihilate_fn: None,
log_type_fn: None
items: [ None, ..23 ]
}
}
// XXX: Method macros sure would be nice here.
pub fn const_trait(&const self) -> def_id {
self.items[ConstTraitLangItem as uint].get()
}
pub fn copy_trait(&const self) -> def_id {
self.items[CopyTraitLangItem as uint].get()
}
pub fn owned_trait(&const self) -> def_id {
self.items[OwnedTraitLangItem as uint].get()
}
pub fn durable_trait(&const self) -> def_id {
self.items[DurableTraitLangItem as uint].get()
}
pub fn drop_trait(&const self) -> def_id {
self.items[DropTraitLangItem as uint].get()
}
pub fn add_trait(&const self) -> def_id {
self.items[AddTraitLangItem as uint].get()
}
pub fn sub_trait(&const self) -> def_id {
self.items[SubTraitLangItem as uint].get()
}
pub fn mul_trait(&const self) -> def_id {
self.items[MulTraitLangItem as uint].get()
}
pub fn div_trait(&const self) -> def_id {
self.items[DivTraitLangItem as uint].get()
}
pub fn modulo_trait(&const self) -> def_id {
self.items[ModuloTraitLangItem as uint].get()
}
pub fn neg_trait(&const self) -> def_id {
self.items[NegTraitLangItem as uint].get()
}
pub fn bitxor_trait(&const self) -> def_id {
self.items[BitXorTraitLangItem as uint].get()
}
pub fn bitand_trait(&const self) -> def_id {
self.items[BitAndTraitLangItem as uint].get()
}
pub fn bitor_trait(&const self) -> def_id {
self.items[BitOrTraitLangItem as uint].get()
}
pub fn shl_trait(&const self) -> def_id {
self.items[ShlTraitLangItem as uint].get()
}
pub fn shr_trait(&const self) -> def_id {
self.items[ShrTraitLangItem as uint].get()
}
pub fn index_trait(&const self) -> def_id {
self.items[IndexTraitLangItem as uint].get()
}
pub fn eq_trait(&const self) -> def_id {
self.items[EqTraitLangItem as uint].get()
}
pub fn ord_trait(&const self) -> def_id {
self.items[OrdTraitLangItem as uint].get()
}
pub fn str_eq_fn(&const self) -> def_id {
self.items[StrEqFnLangItem as uint].get()
}
pub fn uniq_str_eq_fn(&const self) -> def_id {
self.items[UniqStrEqFnLangItem as uint].get()
}
pub fn annihilate_fn(&const self) -> def_id {
self.items[AnnihilateFnLangItem as uint].get()
}
pub fn log_type_fn(&const self) -> def_id {
self.items[LogTypeFnLangItem as uint].get()
}
}
fn LanguageItemCollector(crate: @crate, session: Session,
items: &r/LanguageItems)
-> LanguageItemCollector/&r {
fn LanguageItemCollector(crate: @crate,
session: Session,
items: &r/mut LanguageItems)
-> LanguageItemCollector/&r {
let item_refs = HashMap();
item_refs.insert(~"const", &mut items.const_trait);
item_refs.insert(~"copy", &mut items.copy_trait);
item_refs.insert(~"owned", &mut items.owned_trait);
item_refs.insert(~"durable", &mut items.durable_trait);
item_refs.insert(~"const", ConstTraitLangItem as uint);
item_refs.insert(~"copy", CopyTraitLangItem as uint);
item_refs.insert(~"owned", OwnedTraitLangItem as uint);
item_refs.insert(~"durable", DurableTraitLangItem as uint);
item_refs.insert(~"drop", &mut items.drop_trait);
item_refs.insert(~"drop", DropTraitLangItem as uint);
item_refs.insert(~"add", &mut items.add_trait);
item_refs.insert(~"sub", &mut items.sub_trait);
item_refs.insert(~"mul", &mut items.mul_trait);
item_refs.insert(~"div", &mut items.div_trait);
item_refs.insert(~"modulo", &mut items.modulo_trait);
item_refs.insert(~"neg", &mut items.neg_trait);
item_refs.insert(~"bitxor", &mut items.bitxor_trait);
item_refs.insert(~"bitand", &mut items.bitand_trait);
item_refs.insert(~"bitor", &mut items.bitor_trait);
item_refs.insert(~"shl", &mut items.shl_trait);
item_refs.insert(~"shr", &mut items.shr_trait);
item_refs.insert(~"index", &mut items.index_trait);
item_refs.insert(~"add", AddTraitLangItem as uint);
item_refs.insert(~"sub", SubTraitLangItem as uint);
item_refs.insert(~"mul", MulTraitLangItem as uint);
item_refs.insert(~"div", DivTraitLangItem as uint);
item_refs.insert(~"modulo", ModuloTraitLangItem as uint);
item_refs.insert(~"neg", NegTraitLangItem as uint);
item_refs.insert(~"bitxor", BitXorTraitLangItem as uint);
item_refs.insert(~"bitand", BitAndTraitLangItem as uint);
item_refs.insert(~"bitor", BitOrTraitLangItem as uint);
item_refs.insert(~"shl", ShlTraitLangItem as uint);
item_refs.insert(~"shr", ShrTraitLangItem as uint);
item_refs.insert(~"index", IndexTraitLangItem as uint);
item_refs.insert(~"eq", &mut items.eq_trait);
item_refs.insert(~"ord", &mut items.ord_trait);
item_refs.insert(~"eq", EqTraitLangItem as uint);
item_refs.insert(~"ord", OrdTraitLangItem as uint);
item_refs.insert(~"str_eq", &mut items.str_eq_fn);
item_refs.insert(~"uniq_str_eq", &mut items.uniq_str_eq_fn);
item_refs.insert(~"annihilate", &mut items.annihilate_fn);
item_refs.insert(~"log_type", &mut items.log_type_fn);
item_refs.insert(~"str_eq", StrEqFnLangItem as uint);
item_refs.insert(~"uniq_str_eq", UniqStrEqFnLangItem as uint);
item_refs.insert(~"annihilate", AnnihilateFnLangItem as uint);
item_refs.insert(~"log_type", LogTypeFnLangItem as uint);
LanguageItemCollector {
crate: crate,
@ -141,16 +194,15 @@ fn LanguageItemCollector(crate: @crate, session: Session,
}
struct LanguageItemCollector {
items: &LanguageItems,
items: &mut LanguageItems,
crate: @crate,
session: Session,
item_refs: HashMap<~str,&mut Option<def_id>>,
item_refs: HashMap<~str,uint>,
}
impl LanguageItemCollector {
fn match_and_collect_meta_item(item_def_id: def_id,
meta_item: meta_item) {
match meta_item.node {
@ -177,12 +229,11 @@ impl LanguageItemCollector {
None => {
// Didn't match.
}
Some(item_ref) => {
Some(item_index) => {
// Check for duplicates.
match copy *item_ref {
match self.items.items[item_index] {
Some(original_def_id)
if original_def_id != item_def_id => {
self.session.err(fmt!("duplicate entry for `%s`",
value));
}
@ -192,7 +243,7 @@ impl LanguageItemCollector {
}
// Matched.
*item_ref = Some(item_def_id);
self.items.items[item_index] = Some(item_def_id);
}
}
}
@ -240,7 +291,7 @@ impl LanguageItemCollector {
fn check_completeness() {
for self.item_refs.each |key, item_ref| {
match *item_ref {
match self.items.items[item_ref] {
None => {
self.session.err(fmt!("no item found for `%s`", key));
}
@ -259,8 +310,8 @@ impl LanguageItemCollector {
}
fn collect_language_items(crate: @crate, session: Session) -> LanguageItems {
let items = language_items::make();
let collector = LanguageItemCollector(crate, session, &items);
let mut items = LanguageItems::new();
let collector = LanguageItemCollector(crate, session, &mut items);
collector.collect();
copy items
}

View File

@ -5225,60 +5225,60 @@ impl Resolver {
}
expr_binary(add, _, _) | expr_assign_op(add, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.add_trait);
self.lang_items.add_trait());
}
expr_binary(subtract, _, _) | expr_assign_op(subtract, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.sub_trait);
self.lang_items.sub_trait());
}
expr_binary(mul, _, _) | expr_assign_op(mul, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.mul_trait);
self.lang_items.mul_trait());
}
expr_binary(div, _, _) | expr_assign_op(div, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.div_trait);
self.lang_items.div_trait());
}
expr_binary(rem, _, _) | expr_assign_op(rem, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.modulo_trait);
self.lang_items.modulo_trait());
}
expr_binary(bitxor, _, _) | expr_assign_op(bitxor, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.bitxor_trait);
self.lang_items.bitxor_trait());
}
expr_binary(bitand, _, _) | expr_assign_op(bitand, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.bitand_trait);
self.lang_items.bitand_trait());
}
expr_binary(bitor, _, _) | expr_assign_op(bitor, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.bitor_trait);
self.lang_items.bitor_trait());
}
expr_binary(shl, _, _) | expr_assign_op(shl, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.shl_trait);
self.lang_items.shl_trait());
}
expr_binary(shr, _, _) | expr_assign_op(shr, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.shr_trait);
self.lang_items.shr_trait());
}
expr_binary(lt, _, _) | expr_binary(le, _, _) |
expr_binary(ge, _, _) | expr_binary(gt, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.ord_trait);
self.lang_items.ord_trait());
}
expr_binary(eq, _, _) | expr_binary(ne, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.eq_trait);
self.lang_items.eq_trait());
}
expr_unary(neg, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.neg_trait);
self.lang_items.neg_trait());
}
expr_index(*) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.index_trait);
self.lang_items.index_trait());
}
_ => {
// Nothing to do.
@ -5397,9 +5397,9 @@ impl Resolver {
}
}
fn add_fixed_trait_for_expr(expr_id: node_id, +trait_id: Option<def_id>) {
fn add_fixed_trait_for_expr(expr_id: node_id, +trait_id: def_id) {
let traits = @DVec();
traits.push(trait_id.get());
traits.push(trait_id);
self.trait_map.insert(expr_id, traits);
}

View File

@ -1022,7 +1022,7 @@ fn compare_values(cx: block, lhs: ValueRef, rhs: ValueRef, rhs_t: ty::t) ->
Store(cx, lhs, scratch_lhs);
let scratch_rhs = alloca(cx, val_ty(rhs));
Store(cx, rhs, scratch_rhs);
let did = cx.tcx().lang_items.uniq_str_eq_fn.get();
let did = cx.tcx().lang_items.uniq_str_eq_fn();
let bcx = callee::trans_rtcall_or_lang_call(cx, did,
~[scratch_lhs,
scratch_rhs],
@ -1033,7 +1033,7 @@ fn compare_values(cx: block, lhs: ValueRef, rhs: ValueRef, rhs_t: ty::t) ->
ty::ty_estr(_) => {
let scratch_result = scratch_datum(cx, ty::mk_bool(cx.tcx()),
false);
let did = cx.tcx().lang_items.str_eq_fn.get();
let did = cx.tcx().lang_items.str_eq_fn();
let bcx = callee::trans_rtcall_or_lang_call(cx, did,
~[lhs, rhs],
expr::SaveIn(

View File

@ -2697,7 +2697,7 @@ fn fill_crate_map(ccx: @crate_ctxt, map: ValueRef) {
subcrates.push(C_int(ccx, 0));
let llannihilatefn;
let annihilate_def_id = ccx.tcx.lang_items.annihilate_fn.get();
let annihilate_def_id = ccx.tcx.lang_items.annihilate_fn();
if annihilate_def_id.crate == ast::local_crate {
llannihilatefn = get_item_val(ccx, annihilate_def_id.node);
} else {

View File

@ -208,7 +208,7 @@ fn trans_log(log_ex: @ast::expr,
// Call the polymorphic log function
let val = val_datum.to_ref_llval(bcx);
let did = bcx.tcx().lang_items.log_type_fn.get();
let did = bcx.tcx().lang_items.log_type_fn();
let bcx = callee::trans_rtcall_or_lang_call_with_type_params(
bcx, did, ~[level, val], ~[val_datum.ty], expr::Ignore);
bcx

View File

@ -956,7 +956,7 @@ impl CoherenceChecker {
fn populate_destructor_table() {
let coherence_info = &self.crate_context.coherence_info;
let tcx = self.crate_context.tcx;
let drop_trait = tcx.lang_items.drop_trait.get();
let drop_trait = tcx.lang_items.drop_trait();
let impls_opt = coherence_info.extension_methods.find(drop_trait);
let impls;

View File

@ -897,20 +897,15 @@ fn compute_bounds(ccx: @crate_ctxt,
let ity = ast_ty_to_ty(ccx, empty_rscope, **b);
match ty::get(ity).sty {
ty::ty_trait(did, _, _) => {
let d = Some(did);
if d == li.owned_trait {
if did == li.owned_trait() {
~[ty::bound_owned]
}
else if d == li.copy_trait {
} else if did == li.copy_trait() {
~[ty::bound_copy]
}
else if d == li.const_trait {
} else if did == li.const_trait() {
~[ty::bound_const]
}
else if d == li.durable_trait {
} else if did == li.durable_trait() {
~[ty::bound_durable]
}
else {
} else {
// Must be a user-defined trait
~[ty::bound_trait(ity)]
}

View File

@ -27,7 +27,7 @@ use driver::driver::{optgroups, build_session_options, build_session,
use driver::diagnostic;
use syntax::{ast, attr, parse};
use syntax::parse::parse_crate_from_source_str;
use middle::lang_items::{LanguageItems, language_items};
use middle::lang_items::LanguageItems;
use util::ppaux::ty_to_str;
use syntax::ast_util::dummy_sp;
use middle::ty::{FnTyBase, FnMeta, FnSig};
@ -53,7 +53,7 @@ fn setup_env(test_name: &str, source_string: &str) -> Env {
let freevars = HashMap();
let region_paramd_items = HashMap();
let region_map = HashMap();
let lang_items = language_items::make();
let lang_items = LanguageItems::new();
let parse_sess = parse::new_parse_sess(None);
let crate = parse_crate_from_source_str(