@@ -49,115 +49,120 @@ const uint tag_index_table = 0x15u;
49
49
// to/from def_ids in the string rep. Whatever format you choose should not
50
50
// contain pipe characters.
51
51
52
- // Callback to translate defs to strs or back.
53
- type def_str = fn ( ast. def_id ) -> str ;
52
+ mod Encode {
54
53
55
- fn ty_str ( ty . t t , def_str ds ) -> str {
56
- ret sty_str ( ty . struct ( t ) , ds ) ;
57
- }
54
+ type ctxt = rec (
55
+ fn ( ast . def_id ) -> str ds // Callback to translate defs to strs.
56
+ ) ;
58
57
59
- fn mt_str ( & ty. mt mt , def_str ds) -> str {
60
- auto mut_str;
61
- alt ( mt. mut ) {
62
- case ( ast. imm ) { mut_str = "" ; }
63
- case ( ast. mut ) { mut_str = "m" ; }
64
- case ( ast. maybe_mut ) { mut_str = "?" ; }
58
+ fn ty_str ( @ctxt cx , ty . t t) -> str {
59
+ ret sty_str ( cx, ty. struct ( t) ) ;
65
60
}
66
- ret mut_str + ty_str ( mt. ty , ds) ;
67
- }
68
61
69
- fn sty_str ( ty. sty st , def_str ds) -> str {
70
- alt ( st) {
71
- case ( ty. ty_nil ) { ret "n" ; }
72
- case ( ty. ty_bool ) { ret "b";}
73
- case (ty.ty_int) {ret " i"; }
74
- case ( ty. ty_uint ) { ret "u" ; }
75
- case ( ty. ty_float ) { ret "l" ; }
76
- case ( ty. ty_machine ( ?mach) ) {
77
- alt ( mach) {
78
- case ( common. ty_u8 ) { ret "Mb" ; }
79
- case ( common. ty_u16 ) { ret "Mw" ; }
80
- case ( common. ty_u32 ) { ret "Ml" ; }
81
- case ( common. ty_u64 ) { ret "Md" ; }
82
- case ( common. ty_i8 ) { ret "MB" ; }
83
- case ( common. ty_i16 ) { ret "MW" ; }
84
- case ( common. ty_i32 ) { ret "ML" ; }
85
- case ( common. ty_i64 ) { ret "MD" ; }
86
- case ( common. ty_f32 ) { ret "Mf" ; }
87
- case ( common. ty_f64 ) { ret "MF" ; }
88
- }
62
+ fn mt_str ( @ctxt cx , & ty. mt mt ) -> str {
63
+ auto mut_str;
64
+ alt ( mt. mut ) {
65
+ case ( ast. imm ) { mut_str = "" ; }
66
+ case ( ast. mut ) { mut_str = "m" ; }
67
+ case ( ast. maybe_mut ) { mut_str = "?" ; }
89
68
}
90
- case ( ty. ty_char ) { ret "c";}
91
- case (ty.ty_str) {ret " s"; }
92
- case ( ty. ty_tag ( ?def, ?tys) ) { // TODO restore def_id
93
- auto acc = "t[" + ds ( def) + "|" ;
94
- for ( ty. t t in tys) { acc += ty_str ( t, ds) ; }
95
- ret acc + "]" ;
96
- }
97
- case ( ty. ty_box ( ?mt) ) { ret "@" + mt_str ( mt, ds) ; }
98
- case ( ty. ty_vec ( ?mt) ) { ret "V" + mt_str ( mt, ds) ; }
99
- case ( ty. ty_port ( ?t) ) { ret "P" + ty_str ( t, ds) ; }
100
- case ( ty. ty_chan ( ?t) ) { ret "C" + ty_str ( t, ds) ; }
101
- case ( ty. ty_tup ( ?mts) ) {
102
- auto acc = "T[" ;
103
- for ( ty. mt mt in mts) { acc += mt_str ( mt, ds) ; }
104
- ret acc + "]" ;
105
- }
106
- case ( ty. ty_rec ( ?fields) ) {
107
- auto acc = "R[" ;
108
- for ( ty. field field in fields) {
109
- acc += field. ident + "=" ;
110
- acc += mt_str ( field. mt , ds) ;
69
+ ret mut_str + ty_str ( cx, mt. ty ) ;
70
+ }
71
+
72
+ fn sty_str ( @ctxt cx , ty. sty st ) -> str {
73
+ alt ( st) {
74
+ case ( ty. ty_nil ) { ret "n" ; }
75
+ case ( ty. ty_bool ) { ret "b";}
76
+ case (ty.ty_int) {ret " i"; }
77
+ case ( ty. ty_uint ) { ret "u" ; }
78
+ case ( ty. ty_float ) { ret "l" ; }
79
+ case ( ty. ty_machine ( ?mach) ) {
80
+ alt ( mach) {
81
+ case ( common. ty_u8 ) { ret "Mb" ; }
82
+ case ( common. ty_u16 ) { ret "Mw" ; }
83
+ case ( common. ty_u32 ) { ret "Ml" ; }
84
+ case ( common. ty_u64 ) { ret "Md" ; }
85
+ case ( common. ty_i8 ) { ret "MB" ; }
86
+ case ( common. ty_i16 ) { ret "MW" ; }
87
+ case ( common. ty_i32 ) { ret "ML" ; }
88
+ case ( common. ty_i64 ) { ret "MD" ; }
89
+ case ( common. ty_f32 ) { ret "Mf" ; }
90
+ case ( common. ty_f64 ) { ret "MF" ; }
91
+ }
111
92
}
112
- ret acc + "]" ;
113
- }
114
- case ( ty. ty_fn ( ?proto, ?args, ?out) ) {
115
- ret proto_str ( proto) + ty_fn_str ( args, out, ds) ;
116
- }
117
- case ( ty. ty_native_fn ( ?abi, ?args, ?out) ) {
118
- auto abistr;
119
- alt ( abi) {
120
- case ( ast. native_abi_rust ) { abistr = "r" ; }
121
- case ( ast. native_abi_cdecl ) { abistr = "c" ; }
122
- case ( ast. native_abi_llvm ) { abistr = "l" ; }
93
+ case ( ty. ty_char ) { ret "c";}
94
+ case (ty.ty_str) {ret " s"; }
95
+ case ( ty. ty_tag ( ?def, ?tys) ) { // TODO restore def_id
96
+ auto acc = "t[" + cx. ds ( def) + "|" ;
97
+ for ( ty. t t in tys) { acc += ty_str ( cx, t) ; }
98
+ ret acc + "]" ;
123
99
}
124
- ret "N" + abistr + ty_fn_str ( args, out, ds) ;
125
- }
126
- case ( ty. ty_obj ( ?methods) ) {
127
- auto acc = "O[" ;
128
- for ( ty. method m in methods) {
129
- acc += proto_str ( m. proto ) ;
130
- acc += m. ident ;
131
- acc += ty_fn_str ( m. inputs , m. output , ds) ;
100
+ case ( ty. ty_box ( ?mt) ) { ret "@" + mt_str ( cx, mt) ; }
101
+ case ( ty. ty_vec ( ?mt) ) { ret "V" + mt_str ( cx, mt) ; }
102
+ case ( ty. ty_port ( ?t) ) { ret "P" + ty_str ( cx, t) ; }
103
+ case ( ty. ty_chan ( ?t) ) { ret "C" + ty_str ( cx, t) ; }
104
+ case ( ty. ty_tup ( ?mts) ) {
105
+ auto acc = "T[" ;
106
+ for ( ty. mt mt in mts) { acc += mt_str ( cx, mt) ; }
107
+ ret acc + "]" ;
108
+ }
109
+ case ( ty. ty_rec ( ?fields) ) {
110
+ auto acc = "R[" ;
111
+ for ( ty. field field in fields) {
112
+ acc += field. ident + "=" ;
113
+ acc += mt_str ( cx, field. mt ) ;
114
+ }
115
+ ret acc + "]" ;
132
116
}
133
- ret acc + "]" ;
117
+ case ( ty. ty_fn ( ?proto, ?args, ?out) ) {
118
+ ret proto_str ( proto) + ty_fn_str ( cx, args, out) ;
119
+ }
120
+ case ( ty. ty_native_fn ( ?abi, ?args, ?out) ) {
121
+ auto abistr;
122
+ alt ( abi) {
123
+ case ( ast. native_abi_rust ) { abistr = "r" ; }
124
+ case ( ast. native_abi_cdecl ) { abistr = "c" ; }
125
+ case ( ast. native_abi_llvm ) { abistr = "l" ; }
126
+ }
127
+ ret "N" + abistr + ty_fn_str ( cx, args, out) ;
128
+ }
129
+ case ( ty. ty_obj ( ?methods) ) {
130
+ auto acc = "O[" ;
131
+ for ( ty. method m in methods) {
132
+ acc += proto_str ( m. proto ) ;
133
+ acc += m. ident ;
134
+ acc += ty_fn_str ( cx, m. inputs , m. output ) ;
135
+ }
136
+ ret acc + "]" ;
137
+ }
138
+ case ( ty. ty_var ( ?id) ) { ret "X" + common. istr ( id) ; }
139
+ case ( ty. ty_native ) { ret "E" ; }
140
+ case ( ty. ty_param ( ?id) ) { ret "p" + common. uistr ( id) ; }
141
+ case ( ty. ty_type ) { ret "Y" ; }
142
+
143
+ // These two don't appear in crate metadata, but are here because
144
+ // `hash_ty()` uses this function.
145
+ case ( ty. ty_bound_param ( ?id) ) { ret "o" + common. uistr ( id) ; }
146
+ case ( ty. ty_local ( ?def) ) { ret "L" + cx. ds ( def) ; }
134
147
}
135
- case ( ty. ty_var ( ?id) ) { ret "X" + common. istr ( id) ; }
136
- case ( ty. ty_native ) { ret "E" ; }
137
- case ( ty. ty_param ( ?id) ) { ret "p" + common. uistr ( id) ; }
138
- case ( ty. ty_type ) { ret "Y" ; }
139
-
140
- // These two don't appear in crate metadata, but are here because
141
- // `hash_ty()` uses this function.
142
- case ( ty. ty_bound_param ( ?id) ) { ret "o" + common. uistr ( id) ; }
143
- case ( ty. ty_local ( ?def) ) { ret "L" + ds ( def) ; }
144
148
}
145
- }
146
149
147
- fn proto_str ( ast. proto proto ) -> str {
148
- alt ( proto) {
149
- case ( ast. proto_iter ) { ret "W" ; }
150
- case ( ast. proto_fn ) { ret "F" ; }
150
+ fn proto_str ( ast. proto proto ) -> str {
151
+ alt ( proto) {
152
+ case ( ast. proto_iter ) { ret "W" ; }
153
+ case ( ast. proto_fn ) { ret "F" ; }
154
+ }
151
155
}
152
- }
153
156
154
- fn ty_fn_str ( vec[ ty. arg] args , ty. t out , def_str ds) -> str {
155
- auto acc = "[" ;
156
- for ( ty. arg arg in args) {
157
- if ( arg. mode == ast. alias ) { acc += "&" ; }
158
- acc += ty_str ( arg. ty , ds) ;
157
+ fn ty_fn_str ( @ctxt cx , vec[ ty. arg] args , ty. t out ) -> str {
158
+ auto acc = "[" ;
159
+ for ( ty. arg arg in args) {
160
+ if ( arg. mode == ast. alias ) { acc += "&" ; }
161
+ acc += ty_str ( cx, arg. ty ) ;
162
+ }
163
+ ret acc + "]" + ty_str ( cx, out) ;
159
164
}
160
- ret acc + "]" + ty_str ( out , ds ) ;
165
+
161
166
}
162
167
163
168
@@ -329,8 +334,11 @@ fn encode_variant_id(&ebml.writer ebml_w, ast.def_id vid) {
329
334
330
335
fn encode_type ( & ebml. writer ebml_w , ty. t typ ) {
331
336
ebml. start_tag ( ebml_w, tag_items_data_item_type) ;
337
+
332
338
auto f = def_to_str;
333
- ebml_w. writer . write ( _str. bytes ( ty_str ( typ, f) ) ) ;
339
+ auto ty_str_ctxt = @rec ( ds=f) ;
340
+ ebml_w. writer . write ( _str. bytes ( Encode . ty_str ( ty_str_ctxt, typ) ) ) ;
341
+
334
342
ebml. end_tag ( ebml_w) ;
335
343
}
336
344
0 commit comments