@@ -40,7 +40,7 @@ fn method_ty_to_fn_ty(method m) -> @ty.t {
40
40
//
41
41
// TODO: It'd be really nice to be able to hide this definition from the
42
42
// outside world, to enforce the above invariant.
43
- type t = rec ( sty struct , option. t[ str] cname ) ;
43
+ type t = rec ( sty struct , option. t[ str] cname , uint hash ) ;
44
44
45
45
// NB: If you change this, you'll probably want to change the corresponding
46
46
// AST structure in front/ast.rs as well.
@@ -107,28 +107,34 @@ type type_cache = hashmap[ast.def_id,ty_param_count_and_ty];
107
107
108
108
// Type constructors
109
109
110
- fn mk_nil ( ) -> @t { ret plain_ty ( ty_nil) ; }
111
- fn mk_bool ( ) -> @t { ret plain_ty ( ty_bool) ; }
112
- fn mk_int ( ) -> @t { ret plain_ty ( ty_int) ; }
113
- fn mk_float ( ) -> @t { ret plain_ty ( ty_float) ; }
114
- fn mk_uint ( ) -> @t { ret plain_ty ( ty_uint) ; }
115
- fn mk_mach ( util. common. ty_mach tm ) -> @t { ret plain_ty ( ty_machine ( tm) ) ; }
116
- fn mk_char ( ) -> @t { ret plain_ty ( ty_char) ; }
117
- fn mk_str ( ) -> @t { ret plain_ty ( ty_str) ; }
110
+ // This is a private constructor to this module. External users should always
111
+ // use the mk_foo() functions below.
112
+ fn gen_ty ( & sty st) -> @t {
113
+ ret @rec( struct=st, cname=none[ str] , hash=hash_type_structure ( st) ) ;
114
+ }
115
+
116
+ fn mk_nil ( ) -> @t { ret gen_ty ( ty_nil) ; }
117
+ fn mk_bool ( ) -> @t { ret gen_ty ( ty_bool) ; }
118
+ fn mk_int ( ) -> @t { ret gen_ty ( ty_int) ; }
119
+ fn mk_float ( ) -> @t { ret gen_ty ( ty_float) ; }
120
+ fn mk_uint ( ) -> @t { ret gen_ty ( ty_uint) ; }
121
+ fn mk_mach ( util. common. ty_mach tm ) -> @t { ret gen_ty ( ty_machine ( tm) ) ; }
122
+ fn mk_char ( ) -> @t { ret gen_ty ( ty_char) ; }
123
+ fn mk_str ( ) -> @t { ret gen_ty ( ty_str) ; }
118
124
119
125
fn mk_tag ( ast. def_id did , vec[ @t] tys ) -> @t {
120
- ret plain_ty ( ty_tag ( did, tys) ) ;
126
+ ret gen_ty ( ty_tag ( did, tys) ) ;
121
127
}
122
128
123
- fn mk_box ( mt tm) -> @t { ret plain_ty ( ty_box ( tm) ) ; }
129
+ fn mk_box ( mt tm) -> @t { ret gen_ty ( ty_box ( tm) ) ; }
124
130
fn mk_imm_box ( @t ty ) -> @t { ret mk_box ( rec ( ty=ty, mut=ast. imm ) ) ; }
125
131
126
- fn mk_vec ( mt tm) -> @t { ret plain_ty ( ty_vec ( tm) ) ; }
127
- fn mk_port ( @t ty ) -> @t { ret plain_ty ( ty_port ( ty) ) ; }
128
- fn mk_chan ( @t ty ) -> @t { ret plain_ty ( ty_chan ( ty) ) ; }
129
- fn mk_task ( ) -> @t { ret plain_ty ( ty_task) ; }
132
+ fn mk_vec ( mt tm) -> @t { ret gen_ty ( ty_vec ( tm) ) ; }
133
+ fn mk_port ( @t ty ) -> @t { ret gen_ty ( ty_port ( ty) ) ; }
134
+ fn mk_chan ( @t ty ) -> @t { ret gen_ty ( ty_chan ( ty) ) ; }
135
+ fn mk_task ( ) -> @t { ret gen_ty ( ty_task) ; }
130
136
131
- fn mk_tup ( vec[ mt] tms ) -> @t { ret plain_ty ( ty_tup ( tms) ) ; }
137
+ fn mk_tup ( vec[ mt] tms ) -> @t { ret gen_ty ( ty_tup ( tms) ) ; }
132
138
fn mk_imm_tup ( vec[ @t] tys ) -> @t {
133
139
// TODO: map
134
140
let vec[ ty. mt ] mts = vec ( ) ;
@@ -138,23 +144,23 @@ fn mk_imm_tup(vec[@t] tys) -> @t {
138
144
ret mk_tup( mts) ;
139
145
}
140
146
141
- fn mk_rec ( vec[ field] fs ) -> @t { ret plain_ty ( ty_rec ( fs) ) ; }
147
+ fn mk_rec ( vec[ field] fs ) -> @t { ret gen_ty ( ty_rec ( fs) ) ; }
142
148
143
149
fn mk_fn ( ast. proto proto , vec[ arg] args , @t ty ) -> @t {
144
- ret plain_ty ( ty_fn ( proto, args, ty) ) ;
150
+ ret gen_ty ( ty_fn ( proto, args, ty) ) ;
145
151
}
146
152
147
153
fn mk_native_fn ( ast. native_abi abi , vec[ arg] args , @t ty ) -> @t {
148
- ret plain_ty ( ty_native_fn ( abi, args, ty) ) ;
154
+ ret gen_ty ( ty_native_fn ( abi, args, ty) ) ;
149
155
}
150
156
151
- fn mk_obj ( vec[ method] meths ) -> @t { ret plain_ty ( ty_obj ( meths) ) ; }
152
- fn mk_var ( int v) -> @t { ret plain_ty ( ty_var ( v) ) ; }
153
- fn mk_local ( ast. def_id did ) -> @t { ret plain_ty ( ty_local ( did) ) ; }
154
- fn mk_param ( uint n) -> @t { ret plain_ty ( ty_param ( n) ) ; }
155
- fn mk_bound_param ( uint n) -> @t { ret plain_ty ( ty_bound_param ( n) ) ; }
156
- fn mk_type ( ) -> @t { ret plain_ty ( ty_type) ; }
157
- fn mk_native ( ) -> @t { ret plain_ty ( ty_native) ; }
157
+ fn mk_obj ( vec[ method] meths ) -> @t { ret gen_ty ( ty_obj ( meths) ) ; }
158
+ fn mk_var ( int v) -> @t { ret gen_ty ( ty_var ( v) ) ; }
159
+ fn mk_local ( ast. def_id did ) -> @t { ret gen_ty ( ty_local ( did) ) ; }
160
+ fn mk_param ( uint n) -> @t { ret gen_ty ( ty_param ( n) ) ; }
161
+ fn mk_bound_param ( uint n) -> @t { ret gen_ty ( ty_bound_param ( n) ) ; }
162
+ fn mk_type ( ) -> @t { ret gen_ty ( ty_type) ; }
163
+ fn mk_native ( ) -> @t { ret gen_ty ( ty_native) ; }
158
164
159
165
160
166
// Stringification
@@ -478,13 +484,15 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
478
484
// Type utilities
479
485
480
486
fn rename( @t typ, str new_cname) -> @t {
481
- ret @rec( struct =typ. struct , cname=some[ str ] ( new_cname) ) ;
487
+ ret @rec( struct =typ. struct , cname=some[ str ] ( new_cname) , hash=typ . hash ) ;
482
488
}
483
489
484
490
// Returns a type with the structural part taken from `struct_ty` and the
485
491
// canonical name from `cname_ty`.
486
492
fn copy_cname( @t struct_ty, @t cname_ty) -> @t {
487
- ret @rec( struct =struct_ty. struct , cname=cname_ty. cname) ;
493
+ ret @rec( struct =struct_ty. struct ,
494
+ cname=cname_ty. cname,
495
+ hash=struct_ty. hash) ;
488
496
}
489
497
490
498
// FIXME: remove me when == works on these tags.
@@ -707,16 +715,12 @@ fn type_param(@t ty) -> option.t[uint] {
707
715
ret none[ uint] ;
708
716
}
709
717
710
- fn plain_ty( & sty st) -> @t {
711
- ret @rec( struct =st, cname=none[ str ] ) ;
712
- }
713
-
714
718
fn def_to_str( ast. def_id did) -> str {
715
719
ret #fmt( "%d: %d", did. _0, did. _1) ;
716
720
}
717
721
718
- fn simple_ty_code( & @t ty ) -> uint {
719
- alt ( ty . struct ) {
722
+ fn simple_ty_code( & sty st ) -> uint {
723
+ alt ( st ) {
720
724
case ( ty_nil) { ret 0 u; }
721
725
case ( ty_bool) { ret 1 u; }
722
726
case ( ty_int) { ret 2 u; }
@@ -749,20 +753,28 @@ fn simple_ty_code(&@t ty) -> uint {
749
753
ret 0xffff u;
750
754
}
751
755
752
- fn hash_ty( & @t ty) -> uint {
753
- auto s = simple_ty_code( ty) ;
756
+ // Type hashing. This function is private to this module (and slow); external
757
+ // users should use `hash_ty()` instead.
758
+ fn hash_type_structure( & sty st) -> uint {
759
+
760
+ auto s = simple_ty_code( st) ;
754
761
if ( s != 0xffff u) {
755
762
ret s;
756
763
}
757
764
auto f = def_to_str;
758
- ret _str. hash( metadata. ty_str( ty, f) ) ;
765
+
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) ) ;
759
769
}
760
770
771
+ fn hash_ty( & @t typ) -> uint { ret typ. hash; }
772
+
761
773
fn eq_ty( & @t a, & @t b) -> bool {
762
774
763
- auto sa = simple_ty_code( a) ;
775
+ auto sa = simple_ty_code( a. struct ) ;
764
776
if ( sa != 0xffff u) {
765
- auto sb = simple_ty_code( b) ;
777
+ auto sb = simple_ty_code( b. struct ) ;
766
778
ret sa == sb;
767
779
}
768
780
0 commit comments