Skip to content

Commit a6ff75b

Browse files
committed
---
yaml --- r: 2176 b: refs/heads/master c: 7596fcf h: refs/heads/master v: v3
1 parent 4dda144 commit a6ff75b

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 276a0f2de8d132e40ff1d29c32249ba1963bd73e
2+
refs/heads/master: 7596fcfba7e524977f42f24933173414f3e23cd1

trunk/src/comp/middle/ty.rs

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,28 +756,113 @@ fn simple_ty_code(&sty st) -> uint {
756756
// Type hashing. This function is private to this module (and slow); external
757757
// users should use `hash_ty()` instead.
758758
fn hash_type_structure(&sty st) -> uint {
759+
fn hash_uint(uint id, uint n) -> uint {
760+
auto h = id;
761+
h += h << 5u + n;
762+
ret h;
763+
}
759764

760-
auto s = simple_ty_code(st);
761-
if (s != 0xffffu) {
762-
ret s;
765+
fn hash_def(uint id, ast.def_id did) -> uint {
766+
auto h = id;
767+
h += h << 5u + (did._0 as uint);
768+
h += h << 5u + (did._1 as uint);
769+
ret h;
763770
}
764-
auto f = def_to_str;
765771

766-
// FIXME: Gross. Use structural hashing when we have it.
767-
auto fake_ty = @rec(struct=st, cname=none[str], hash=0u);
768-
ret _str.hash(metadata.ty_str(fake_ty, f));
772+
fn hash_subty(uint id, @t subty) -> uint {
773+
auto h = id;
774+
h += h << 5u + hash_ty(subty);
775+
ret h;
776+
}
777+
778+
fn hash_fn(uint id, vec[arg] args, @t rty) -> uint {
779+
auto h = id;
780+
for (arg a in args) {
781+
h += h << 5u + hash_ty(a.ty);
782+
}
783+
h += h << 5u + hash_ty(rty);
784+
ret h;
785+
}
786+
787+
alt (st) {
788+
case (ty_nil) { ret 0u; }
789+
case (ty_bool) { ret 1u; }
790+
case (ty_int) { ret 2u; }
791+
case (ty_float) { ret 3u; }
792+
case (ty_uint) { ret 4u; }
793+
case (ty_machine(?tm)) {
794+
alt (tm) {
795+
case (common.ty_i8) { ret 5u; }
796+
case (common.ty_i16) { ret 6u; }
797+
case (common.ty_i32) { ret 7u; }
798+
case (common.ty_i64) { ret 8u; }
799+
800+
case (common.ty_u8) { ret 9u; }
801+
case (common.ty_u16) { ret 10u; }
802+
case (common.ty_u32) { ret 11u; }
803+
case (common.ty_u64) { ret 12u; }
804+
805+
case (common.ty_f32) { ret 13u; }
806+
case (common.ty_f64) { ret 14u; }
807+
}
808+
}
809+
case (ty_char) { ret 15u; }
810+
case (ty_str) { ret 16u; }
811+
case (ty_tag(?did, ?tys)) {
812+
auto h = hash_def(17u, did);
813+
for (@ty.t typ in tys) {
814+
h += h << 5u + hash_ty(typ);
815+
}
816+
ret h;
817+
}
818+
case (ty_box(?mt)) { ret hash_subty(18u, mt.ty); }
819+
case (ty_vec(?mt)) { ret hash_subty(19u, mt.ty); }
820+
case (ty_port(?typ)) { ret hash_subty(20u, typ); }
821+
case (ty_chan(?typ)) { ret hash_subty(21u, typ); }
822+
case (ty_task) { ret 22u; }
823+
case (ty_tup(?mts)) {
824+
auto h = 23u;
825+
for (mt tm in mts) {
826+
h += h << 5u + hash_ty(tm.ty);
827+
}
828+
ret h;
829+
}
830+
case (ty_rec(?fields)) {
831+
auto h = 24u;
832+
for (field f in fields) {
833+
h += h << 5u + hash_ty(f.mt.ty);
834+
}
835+
ret h;
836+
}
837+
case (ty_fn(_, ?args, ?rty)) { ret hash_fn(25u, args, rty); }
838+
case (ty_native_fn(_, ?args, ?rty)) { ret hash_fn(26u, args, rty); }
839+
case (ty_obj(?methods)) {
840+
auto h = 27u;
841+
for (method m in methods) {
842+
h += h << 5u + _str.hash(m.ident);
843+
}
844+
ret h;
845+
}
846+
case (ty_var(?v)) { ret hash_uint(28u, v as uint); }
847+
case (ty_local(?did)) { ret hash_def(29u, did); }
848+
case (ty_param(?pid)) { ret hash_uint(30u, pid); }
849+
case (ty_bound_param(?pid)) { ret hash_uint(31u, pid); }
850+
case (ty_type) { ret 32u; }
851+
case (ty_native) { ret 33u; }
852+
}
769853
}
770854

771855
fn hash_ty(&@t typ) -> uint { ret typ.hash; }
772856

773857
fn eq_ty(&@t a, &@t b) -> bool {
774-
775-
auto sa = simple_ty_code(a.struct);
776-
if (sa != 0xffffu) {
777-
auto sb = simple_ty_code(b.struct);
778-
ret sa == sb;
858+
auto sa = hash_type_structure(a.struct);
859+
auto sb = hash_type_structure(b.struct);
860+
if (sa != sb) {
861+
ret false;
779862
}
780863

864+
// TODO: shortcut for simple types
865+
781866
// FIXME: this is gross, but I think it's safe, and I don't think writing
782867
// a giant function to handle all the cases is necessary when structural
783868
// equality will someday save the day.

0 commit comments

Comments
 (0)