From 3ee630bbee1e35933b6296e4749fcdd882a81f90 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Thu, 22 Dec 2011 14:24:36 +0100 Subject: [PATCH] Add ty::new_ty_hash, clean up comparing of ty::t's --- src/comp/metadata/encoder.rs | 2 +- src/comp/middle/debuginfo.rs | 7 +++-- src/comp/middle/trans.rs | 19 +++++--------- src/comp/middle/ty.rs | 51 ++++++++++++++---------------------- 4 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index 1e844d39bca..169a1354f98 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -653,7 +653,7 @@ fn encode_hash(ebml_w: ebml::writer, hash: str) { fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str { - let abbrevs = map::mk_hashmap(ty::hash_ty, ty::eq_ty); + let abbrevs = ty::new_ty_hash(); let ecx = @{ccx: cx, type_abbrevs: abbrevs}; let string_w = io::string_writer(); diff --git a/src/comp/middle/debuginfo.rs b/src/comp/middle/debuginfo.rs index 96868cd0bf0..19b1578b29a 100644 --- a/src/comp/middle/debuginfo.rs +++ b/src/comp/middle/debuginfo.rs @@ -267,8 +267,7 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) let cache = get_cache(cx); let tg = BasicTypeDescriptorTag; alt cached_metadata::<@metadata>( - cache, tg, - {|md| ty::hash_ty(t) == ty::hash_ty(md.data.hash)}) { + cache, tg, {|md| t == md.data.hash}) { option::some(md) { ret md; } option::none. {} } @@ -311,7 +310,7 @@ fn create_basic_type(cx: @crate_ctxt, t: ty::t, ty: @ast::ty) lli32(0), //XXX flags? lli32(encoding)]; let llnode = llmdnode(lldata); - let mdval = @{node: llnode, data: {hash: ty::hash_ty(t)}}; + let mdval = @{node: llnode, data: {hash: t}}; update_cache(cache, tg, tydesc_metadata(mdval)); add_named_metadata(cx, "llvm.dbg.ty", llnode); ret mdval; @@ -333,7 +332,7 @@ fn create_pointer_type(cx: @crate_ctxt, t: ty::t, span: codemap::span, //let cu_node = create_compile_unit(cx, fname); let llnode = create_derived_type(tg, file_node.node, "", 0, size * 8, align * 8, 0, pointee.node); - let mdval = @{node: llnode, data: {hash: ty::hash_ty(t)}}; + let mdval = @{node: llnode, data: {hash: t}}; //update_cache(cache, tg, tydesc_metadata(mdval)); add_named_metadata(cx, "llvm.dbg.ty", llnode); ret mdval; diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index cc2259d6e0d..786ab1886c8 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -4348,7 +4348,7 @@ fn new_fn_ctxt_w_id(cx: @local_ctxt, sp: span, llfndecl: ValueRef, lllocals: new_int_hash::(), llupvars: new_int_hash::(), mutable lltydescs: [], - derived_tydescs: map::mk_hashmap(ty::hash_ty, ty::eq_ty), + derived_tydescs: ty::new_ty_hash(), id: id, ret_style: rstyle, sp: sp, @@ -5642,13 +5642,6 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt, tn.associate("taskptr", taskptr_type); let tydesc_type = T_tydesc(targ_cfg); tn.associate("tydesc", tydesc_type); - let hasher = ty::hash_ty; - let eqer = ty::eq_ty; - let tag_sizes = map::mk_hashmap::(hasher, eqer); - let tydescs = map::mk_hashmap::(hasher, eqer); - let lltypes = map::mk_hashmap::(hasher, eqer); - let sha1s = map::mk_hashmap::(hasher, eqer); - let short_names = map::mk_hashmap::(hasher, eqer); let crate_map = decl_crate_map(sess, link_meta.name, llmod); let dbg_cx = if sess.get_opts().debuginfo { option::some(@{llmetadata: map::new_int_hash(), @@ -5669,18 +5662,18 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt, item_symbols: new_int_hash::(), mutable main_fn: none::, link_meta: link_meta, - tag_sizes: tag_sizes, + tag_sizes: ty::new_ty_hash(), discrims: ast_util::new_def_id_hash::(), discrim_symbols: new_int_hash::(), consts: new_int_hash::(), obj_methods: new_int_hash::<()>(), - tydescs: tydescs, + tydescs: ty::new_ty_hash(), module_data: new_str_hash::(), - lltypes: lltypes, + lltypes: ty::new_ty_hash(), names: namegen(0), sha: sha, - type_sha1s: sha1s, - type_short_names: short_names, + type_sha1s: ty::new_ty_hash(), + type_short_names: ty::new_ty_hash(), tcx: tcx, mut_map: mut_map, copy_map: copy_map, diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index ef6177822e3..a1cb5cd371b 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -36,7 +36,6 @@ export constr_table; export count_ty_params; export ctxt; export def_has_ty_params; -export eq_ty; export expr_has_ty_params; export expr_ty; export expr_ty_params_and_ty; @@ -47,7 +46,6 @@ export field_idx; export get_field; export fm_general; export get_element_type; -export hash_ty; export idx_nil; export is_binopable; export is_pred_ty; @@ -103,6 +101,7 @@ export stmt_node_id; export sty; export substitute_type_params; export t; +export new_ty_hash; export tag_variants; export tag_variant_with_id; export ty_param_substs_opt_and_ty; @@ -398,12 +397,16 @@ fn mk_rcache() -> creader_cache { ret map::mk_hashmap(hash_cache_entry, eq_cache_entries); } +fn new_ty_hash() -> map::hashmap { map::new_uint_hash() } fn mk_ctxt(s: session::session, dm: resolve::def_map, amap: ast_map::map, freevars: freevars::freevar_map) -> ctxt { let ntt: node_type_table = @smallintmap::mk::(); let tcache = new_def_hash::(); + fn eq_raw_ty(&&a: @raw_t, &&b: @raw_t) -> bool { + ret a.hash == b.hash && a.struct == b.struct; + } let ts = @interner::mk::<@raw_t>(hash_raw_ty, eq_raw_ty); let cx = @{ts: ts, @@ -414,9 +417,9 @@ fn mk_ctxt(s: session::session, dm: resolve::def_map, amap: ast_map::map, freevars: freevars, tcache: tcache, rcache: mk_rcache(), - short_names_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), - needs_drop_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), - kind_cache: map::mk_hashmap(ty::hash_ty, ty::eq_ty), + short_names_cache: new_ty_hash(), + needs_drop_cache: new_ty_hash(), + kind_cache: new_ty_hash(), ast_ty_to_ty_cache: map::mk_hashmap(ast_util::hash_ty, ast_util::eq_ty), tag_var_cache: @smallintmap::mk()}; @@ -1233,8 +1236,7 @@ fn type_autoderef(cx: ctxt, t: ty::t) -> ty::t { ret t1; } -// Type hashing. This function is private to this module (and slow); external -// users should use `hash_ty()` instead. +// Type hashing. fn hash_type_structure(st: sty) -> uint { fn hash_uint(id: uint, n: uint) -> uint { let h = id; @@ -1249,7 +1251,7 @@ fn hash_type_structure(st: sty) -> uint { } fn hash_subty(id: uint, subty: t) -> uint { let h = id; - h += (h << 5u) + hash_ty(subty); + h += (h << 5u) + subty; ret h; } fn hash_subtys(id: uint, subtys: [t]) -> uint { @@ -1284,8 +1286,8 @@ fn hash_type_structure(st: sty) -> uint { fn hash_fn(id: uint, args: [arg], rty: t) -> uint { let h = id; - for a: arg in args { h += (h << 5u) + hash_ty(a.ty); } - h += (h << 5u) + hash_ty(rty); + for a: arg in args { h += (h << 5u) + a.ty; } + h += (h << 5u) + rty; ret h; } alt st { @@ -1308,14 +1310,14 @@ fn hash_type_structure(st: sty) -> uint { ty_str. { ret 17u; } ty_tag(did, tys) { let h = hash_def(18u, did); - for typ: t in tys { h += (h << 5u) + hash_ty(typ); } + for typ: t in tys { h += (h << 5u) + typ; } ret h; } ty_box(mt) { ret hash_subty(19u, mt.ty); } ty_vec(mt) { ret hash_subty(21u, mt.ty); } ty_rec(fields) { let h = 26u; - for f: field in fields { h += (h << 5u) + hash_ty(f.mt.ty); } + for f: field in fields { h += (h << 5u) + f.mt.ty; } ret h; } ty_tup(ts) { ret hash_subtys(25u, ts); } @@ -1354,13 +1356,6 @@ fn hash_type_structure(st: sty) -> uint { fn hash_raw_ty(&&rt: @raw_t) -> uint { ret rt.hash; } -fn hash_ty(&&typ: t) -> uint { ret typ; } - - -// Type equality. This function is private to this module (and slow); external -// users should use `eq_ty()` instead. -fn eq_int(&&x: uint, &&y: uint) -> bool { ret x == y; } - fn arg_eq(eq: fn(T, T) -> bool, a: @sp_constr_arg, b: @sp_constr_arg) -> bool { alt a.node { @@ -1389,6 +1384,7 @@ fn args_eq(eq: fn(T, T) -> bool, a: [@sp_constr_arg], } fn constr_eq(c: @constr, d: @constr) -> bool { + fn eq_int(&&x: uint, &&y: uint) -> bool { ret x == y; } ret path_to_str(c.node.path) == path_to_str(d.node.path) && // FIXME: hack args_eq(eq_int, c.node.args, d.node.args); @@ -1401,16 +1397,6 @@ fn constrs_eq(cs: [@constr], ds: [@constr]) -> bool { ret true; } -// This function is private to this module. -fn eq_raw_ty(&&a: @raw_t, &&b: @raw_t) -> bool { - ret a.hash == b.hash && a.struct == b.struct; -} - - -// This is the equality function the public should use. It works as long as -// the types are interned. -fn eq_ty(&&a: t, &&b: t) -> bool { a == b } - // Type lookups fn node_id_to_ty_param_substs_opt_and_ty(cx: ctxt, id: ast::node_id) -> ty_param_substs_opt_and_ty { @@ -2088,12 +2074,15 @@ mod unify { fn unify_step(cx: @ctxt, expected: t, actual: t, variance: variance) -> result { - // TODO: rewrite this using tuple pattern matching when available, to + // FIXME: rewrite this using tuple pattern matching when available, to // avoid all this rightward drift and spikiness. + // NOTE: we have tuple matching now, but that involves copying the + // matched elements into a tuple first, which is expensive, since sty + // holds vectors, which are currently unique // Fast path. + if expected == actual { ret ures_ok(expected); } - if eq_ty(expected, actual) { ret ures_ok(expected); } // Stage 1: Handle the cases in which one side or another is a type // variable.