@@ -756,28 +756,113 @@ fn simple_ty_code(&sty st) -> uint {
756
756
// Type hashing. This function is private to this module (and slow); external
757
757
// users should use `hash_ty()` instead.
758
758
fn hash_type_structure( & sty st) -> uint {
759
+ fn hash_uint( uint id, uint n) -> uint {
760
+ auto h = id;
761
+ h += h << 5 u + n;
762
+ ret h;
763
+ }
759
764
760
- auto s = simple_ty_code( st) ;
761
- if ( s != 0xffff u) {
762
- ret s;
765
+ fn hash_def( uint id, ast. def_id did) -> uint {
766
+ auto h = id;
767
+ h += h << 5 u + ( did. _0 as uint) ;
768
+ h += h << 5 u + ( did. _1 as uint) ;
769
+ ret h;
763
770
}
764
- auto f = def_to_str;
765
771
766
- // FIXME: Gross. Use structural hashing when we have it.
767
- auto fake_ty = @rec( struct =st, cname=none[ str ] , hash=0 u) ;
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 << 5 u + 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 << 5 u + hash_ty( a. ty) ;
782
+ }
783
+ h += h << 5 u + hash_ty( rty) ;
784
+ ret h;
785
+ }
786
+
787
+ alt ( st) {
788
+ case ( ty_nil) { ret 0 u; }
789
+ case ( ty_bool) { ret 1 u; }
790
+ case ( ty_int) { ret 2 u; }
791
+ case ( ty_float) { ret 3 u; }
792
+ case ( ty_uint) { ret 4 u; }
793
+ case ( ty_machine( ?tm) ) {
794
+ alt ( tm) {
795
+ case ( common. ty_i8) { ret 5 u; }
796
+ case ( common. ty_i16) { ret 6 u; }
797
+ case ( common. ty_i32) { ret 7 u; }
798
+ case ( common. ty_i64) { ret 8 u; }
799
+
800
+ case ( common. ty_u8) { ret 9 u; }
801
+ case ( common. ty_u16) { ret 10 u; }
802
+ case ( common. ty_u32) { ret 11 u; }
803
+ case ( common. ty_u64) { ret 12 u; }
804
+
805
+ case ( common. ty_f32) { ret 13 u; }
806
+ case ( common. ty_f64) { ret 14 u; }
807
+ }
808
+ }
809
+ case ( ty_char) { ret 15 u; }
810
+ case ( ty_str) { ret 16 u; }
811
+ case ( ty_tag( ?did, ?tys) ) {
812
+ auto h = hash_def( 17 u, did) ;
813
+ for ( @ty. t typ in tys) {
814
+ h += h << 5 u + hash_ty( typ) ;
815
+ }
816
+ ret h;
817
+ }
818
+ case ( ty_box( ?mt) ) { ret hash_subty( 18 u, mt. ty) ; }
819
+ case ( ty_vec( ?mt) ) { ret hash_subty( 19 u, mt. ty) ; }
820
+ case ( ty_port( ?typ) ) { ret hash_subty( 20 u, typ) ; }
821
+ case ( ty_chan( ?typ) ) { ret hash_subty( 21 u, typ) ; }
822
+ case ( ty_task) { ret 22 u; }
823
+ case ( ty_tup( ?mts) ) {
824
+ auto h = 23 u;
825
+ for ( mt tm in mts) {
826
+ h += h << 5 u + hash_ty( tm. ty) ;
827
+ }
828
+ ret h;
829
+ }
830
+ case ( ty_rec( ?fields) ) {
831
+ auto h = 24 u;
832
+ for ( field f in fields) {
833
+ h += h << 5 u + hash_ty( f. mt. ty) ;
834
+ }
835
+ ret h;
836
+ }
837
+ case ( ty_fn( _, ?args, ?rty) ) { ret hash_fn( 25 u, args, rty) ; }
838
+ case ( ty_native_fn( _, ?args, ?rty) ) { ret hash_fn( 26 u, args, rty) ; }
839
+ case ( ty_obj( ?methods) ) {
840
+ auto h = 27 u;
841
+ for ( method m in methods) {
842
+ h += h << 5 u + _str. hash( m. ident) ;
843
+ }
844
+ ret h;
845
+ }
846
+ case ( ty_var( ?v) ) { ret hash_uint( 28 u, v as uint) ; }
847
+ case ( ty_local( ?did) ) { ret hash_def( 29 u, did) ; }
848
+ case ( ty_param( ?pid) ) { ret hash_uint( 30 u, pid) ; }
849
+ case ( ty_bound_param( ?pid) ) { ret hash_uint( 31 u, pid) ; }
850
+ case ( ty_type) { ret 32 u; }
851
+ case ( ty_native) { ret 33 u; }
852
+ }
769
853
}
770
854
771
855
fn hash_ty( & @t typ) -> uint { ret typ. hash; }
772
856
773
857
fn eq_ty( & @t a, & @t b) -> bool {
774
-
775
- auto sa = simple_ty_code( a. struct ) ;
776
- if ( sa != 0xffff u) {
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;
779
862
}
780
863
864
+ // TODO: shortcut for simple types
865
+
781
866
// FIXME: this is gross, but I think it's safe, and I don't think writing
782
867
// a giant function to handle all the cases is necessary when structural
783
868
// equality will someday save the day.
0 commit comments