@@ -31,8 +31,11 @@ import std.option.none;
31
31
import std. option . some ;
32
32
33
33
type ty_table = hashmap [ ast. def_id , @ty. t] ;
34
+ type ty_item_table = hashmap [ ast. def_id , @ast. item ] ;
35
+
34
36
type crate_ctxt = rec ( session . session sess,
35
37
@ty_table item_types ,
38
+ @ty_item_table item_items ,
36
39
vec[ ast. obj_field] obj_fields ,
37
40
mutable int next_var_id ) ;
38
41
@@ -41,7 +44,8 @@ type fn_ctxt = rec(@ty.t ret_ty,
41
44
@crate_ctxt ccx ) ;
42
45
43
46
// Used for ast_ty_to_ty() below.
44
- type ty_getter = fn ( ast. def_id ) -> @ty. t ;
47
+ type ty_and_params = rec ( vec[ ast. ty_param] params , @ty. t ty ) ;
48
+ type ty_getter = fn ( ast. def_id ) -> ty_and_params ;
45
49
46
50
// Replaces parameter types inside a type with type variables.
47
51
fn generalize_ty ( @crate_ctxt cx , @ty. t t ) -> @ty. t {
@@ -118,7 +122,7 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
118
122
case ( ast. def_ty ( ?id) ) {
119
123
// TODO: maybe record cname chains so we can do
120
124
// "foo = int" like OCaml?
121
- sty = getter ( id) . struct ;
125
+ sty = getter ( id) . ty . struct ;
122
126
}
123
127
case ( ast. def_ty_arg ( ?id) ) { sty = ty. ty_param ( id) ; }
124
128
case ( _) { fail; }
@@ -155,14 +159,39 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
155
159
// A convenience function to use a crate_ctxt to resolve names for
156
160
// ast_ty_to_ty.
157
161
fn ast_ty_to_ty_crate( @crate_ctxt ccx, & @ast. ty ast_ty) -> @ty. t {
158
- fn getter( @crate_ctxt ccx, ast. def_id id) -> @ty . t {
162
+ fn getter( @crate_ctxt ccx, ast. def_id id) -> ty_and_params {
159
163
check ( ccx. item_types. contains_key( id) ) ;
160
- ret ccx. item_types. get( id) ;
164
+ check ( ccx. item_items. contains_key( id) ) ;
165
+ auto ty = ccx. item_types. get( id) ;
166
+ auto item = ccx. item_items. get( id) ;
167
+ auto params = ty_params_of_item( item) ;
168
+ ret rec( params = params, ty = ty) ;
161
169
}
162
170
auto f = bind getter( ccx, _) ;
163
171
ret ast_ty_to_ty( f, ast_ty) ;
164
172
}
165
173
174
+ fn ty_params_of_item( @ast. item item) -> vec[ ast. ty_param] {
175
+ alt ( item. node) {
176
+ case ( ast. item_fn( _, _, ?p, _, _) ) {
177
+ ret p;
178
+ }
179
+ case ( ast. item_ty( _, _, ?p, _, _) ) {
180
+ ret p;
181
+ }
182
+ case ( ast. item_tag( _, _, ?p, _) ) {
183
+ ret p;
184
+ }
185
+ case ( ast. item_obj( _, _, ?p, _, _) ) {
186
+ ret p;
187
+ }
188
+ case ( _) {
189
+ let vec[ ast. ty_param] r = vec( ) ;
190
+ ret r;
191
+ }
192
+ }
193
+ }
194
+
166
195
// Item collection - a pair of bootstrap passes:
167
196
//
168
197
// 1. Collect the IDs of all type items (typedefs) and store them in a table.
@@ -175,16 +204,16 @@ fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
175
204
// AST, along with a table mapping item IDs to their types.
176
205
177
206
fn collect_item_types( session. session sess, @ast. crate crate)
178
- -> tup ( @ast. crate , @ty_table ) {
179
-
180
- type ty_item_table = hashmap [ ast. def_id , @ast. item ] ;
207
+ -> tup( @ast. crate , @ty_table, @ty_item_table) {
181
208
182
209
fn getter( @ty_item_table id_to_ty_item,
183
210
@ty_table item_to_ty,
184
- ast. def_id id ) -> @ty . t {
211
+ ast. def_id id) -> ty_and_params {
185
212
check ( id_to_ty_item. contains_key( id) ) ;
186
213
auto item = id_to_ty_item. get( id) ;
187
- ret ty_of_item( id_to_ty_item, item_to_ty, item) ;
214
+ auto ty = ty_of_item( id_to_ty_item, item_to_ty, item) ;
215
+ auto params = ty_params_of_item( item) ;
216
+ ret rec( params = params, ty = ty) ;
188
217
}
189
218
190
219
fn ty_of_arg( @ty_item_table id_to_ty_item,
@@ -485,7 +514,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
485
514
fold_item_tag = bind fold_item_tag ( _, _, _, _, _, _)
486
515
with * fld_2) ;
487
516
auto crate_ = fold. fold_crate [ @env] ( e, fld_2, crate ) ;
488
- ret tup( crate_, item_to_ty) ;
517
+ ret tup( crate_, item_to_ty, id_to_ty_item ) ;
489
518
}
490
519
491
520
fn unify ( & @fn_ctxt fcx , @ty. t expected , @ty. t actual ) -> ty. unify_result {
@@ -1634,6 +1663,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {
1634
1663
1635
1664
auto ccx = @rec ( sess=sess,
1636
1665
item_types=result. _1 ,
1666
+ item_items=result. _2 ,
1637
1667
obj_fields=fields,
1638
1668
mutable next_var_id=0 ) ;
1639
1669
0 commit comments