@@ -31,7 +31,13 @@ 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 ] ;
34
+
35
+ tag any_item {
36
+ any_item_rust( @ast. item ) ;
37
+ any_item_native ( @ast. native_item ) ;
38
+ }
39
+
40
+ type ty_item_table = hashmap [ ast. def_id , any_item] ;
35
41
36
42
type crate_ctxt = rec ( session . session sess,
37
43
@ty_table item_types ,
@@ -161,6 +167,9 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
161
167
case ( ast. def_ty( ?id) ) {
162
168
sty = instantiate( getter, id, path. node. types) . struct ;
163
169
}
170
+ case ( ast. def_native_ty( ?id) ) {
171
+ sty = instantiate( getter, id, path. node. types) . struct ;
172
+ }
164
173
case ( ast. def_obj( ?id) ) {
165
174
sty = instantiate( getter, id, path. node. types) . struct ;
166
175
}
@@ -196,23 +205,36 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
196
205
ret @rec( struct=sty, mut =mut , cname=cname) ;
197
206
}
198
207
208
+ fn actual_type( @ty. t t, @ast. item item) -> @ty. t {
209
+ alt ( item. node) {
210
+ case ( ast. item_obj( _, _, _, _, _) ) {
211
+ // An obj used as a type name refers to the output type of the
212
+ // item (constructor).
213
+ ret middle. ty. ty_fn_ret( t) ;
214
+ }
215
+ case ( _) { }
216
+ }
217
+
218
+ ret t;
219
+ }
220
+
199
221
// A convenience function to use a crate_ctxt to resolve names for
200
222
// ast_ty_to_ty.
201
223
fn ast_ty_to_ty_crate( @crate_ctxt ccx, & @ast. ty ast_ty) -> @ty. t {
202
224
fn getter( @crate_ctxt ccx, ast. def_id id) -> ty_and_params {
203
225
check ( ccx. item_items. contains_key( id) ) ;
204
226
check ( ccx. item_types. contains_key( id) ) ;
205
- auto item = ccx. item_items. get( id) ;
227
+ auto it = ccx. item_items. get( id) ;
206
228
auto ty = ccx. item_types. get( id) ;
207
- auto params = ty_params_of_item( item) ;
208
-
209
- alt ( item. node) {
210
- case ( ast. item_obj( _, _, _, _, _) ) {
211
- // An obj used as a type name refers to the output type of the
212
- // item (constructor).
213
- ty = middle. ty. ty_fn_ret( ty) ;
229
+ auto params;
230
+ alt ( it) {
231
+ case ( any_item_rust( ?item) ) {
232
+ ty = actual_type( ty, item) ;
233
+ params = ty_params_of_item( item) ;
214
234
}
215
- case ( _) { }
235
+ case ( any_item_native( ?native_item) ) {
236
+ params = ty_params_of_native_item( native_item) ;
237
+ }
216
238
}
217
239
218
240
ret rec( params = params, ty = ty) ;
@@ -242,6 +264,18 @@ fn ty_params_of_item(@ast.item item) -> vec[ast.ty_param] {
242
264
}
243
265
}
244
266
267
+ fn ty_params_of_native_item( @ast. native_item item) -> vec[ ast. ty_param] {
268
+ alt ( item. node) {
269
+ case ( ast. native_item_fn( _, _, ?p, _) ) {
270
+ ret p;
271
+ }
272
+ case ( _) {
273
+ let vec[ ast. ty_param] r = vec( ) ;
274
+ ret r;
275
+ }
276
+ }
277
+ }
278
+
245
279
// Item collection - a pair of bootstrap passes:
246
280
//
247
281
// 1. Collect the IDs of all type items (typedefs) and store them in a table.
@@ -253,24 +287,40 @@ fn ty_params_of_item(@ast.item item) -> vec[ast.ty_param] {
253
287
// We then annotate the AST with the resulting types and return the annotated
254
288
// AST, along with a table mapping item IDs to their types.
255
289
290
+ fn ty_of_fn_decl( @ty_item_table id_to_ty_item,
291
+ @ty_table item_to_ty,
292
+ fn( & @ast. ty ast_ty) -> @ty. t convert,
293
+ fn( & ast. arg a) -> arg ty_of_arg,
294
+ & ast. fn_decl decl,
295
+ ast. def_id def_id) -> @ty. t {
296
+ auto input_tys = _vec. map[ ast. arg, arg] ( ty_of_arg, decl. inputs) ;
297
+ auto output_ty = convert( decl. output) ;
298
+ auto t_fn = plain_ty( ty. ty_fn( input_tys, output_ty) ) ;
299
+ item_to_ty. insert( def_id, t_fn) ;
300
+ ret t_fn;
301
+ }
302
+
256
303
fn collect_item_types( session. session sess, @ast. crate crate)
257
304
-> tup( @ast. crate , @ty_table, @ty_item_table) {
258
305
259
306
fn getter( @ty_item_table id_to_ty_item,
260
307
@ty_table item_to_ty,
261
308
ast. def_id id) -> ty_and_params {
262
309
check ( id_to_ty_item. contains_key( id) ) ;
263
- auto item = id_to_ty_item. get( id) ;
264
- auto ty = ty_of_item( id_to_ty_item, item_to_ty, item) ;
265
- auto params = ty_params_of_item( item) ;
266
-
267
- alt ( item. node) {
268
- case ( ast. item_obj( _, _, _, _, _) ) {
269
- // An obj used as a type name refers to the output type of the
270
- // item (constructor).
271
- ty = middle. ty. ty_fn_ret( ty) ;
310
+ auto it = id_to_ty_item. get( id) ;
311
+ auto ty;
312
+ auto params;
313
+ alt ( it) {
314
+ case ( any_item_rust( ?item) ) {
315
+ ty = ty_of_item( id_to_ty_item, item_to_ty, item) ;
316
+ ty = actual_type( ty, item) ;
317
+ params = ty_params_of_item( item) ;
318
+ }
319
+ case ( any_item_native( ?native_item) ) {
320
+ ty = ty_of_native_item( id_to_ty_item, item_to_ty,
321
+ native_item) ;
322
+ params = ty_params_of_native_item( native_item) ;
272
323
}
273
- case ( _) { }
274
324
}
275
325
276
326
ret rec( params = params, ty = ty) ;
@@ -340,16 +390,9 @@ fn collect_item_types(session.session sess, @ast.crate crate)
340
390
}
341
391
342
392
case ( ast. item_fn( ?ident, ?fn_info, _, ?def_id, _) ) {
343
- // TODO: handle ty-params
344
-
345
393
auto f = bind ty_of_arg( id_to_ty_item, item_to_ty, _) ;
346
- auto input_tys = _vec. map[ ast. arg, arg] ( f,
347
- fn_info. decl. inputs) ;
348
- auto output_ty = convert( fn_info. decl. output) ;
349
-
350
- auto t_fn = plain_ty( ty. ty_fn( input_tys, output_ty) ) ;
351
- item_to_ty. insert( def_id, t_fn) ;
352
- ret t_fn;
394
+ ret ty_of_fn_decl( id_to_ty_item, item_to_ty, convert, f,
395
+ fn_info. decl, def_id) ;
353
396
}
354
397
355
398
case ( ast. item_obj( ?ident, ?obj_info, _, ?def_id, _) ) {
@@ -385,6 +428,30 @@ fn collect_item_types(session.session sess, @ast.crate crate)
385
428
}
386
429
}
387
430
431
+ fn ty_of_native_item( @ty_item_table id_to_ty_item,
432
+ @ty_table item_to_ty,
433
+ @ast. native_item it) -> @ty. t {
434
+ alt ( it. node) {
435
+ case ( ast. native_item_fn( ?ident, ?fn_decl, ?params, ?def_id) ) {
436
+ auto get = bind getter( id_to_ty_item, item_to_ty, _) ;
437
+ auto convert = bind ast_ty_to_ty( get, _) ;
438
+ auto f = bind ty_of_arg( id_to_ty_item, item_to_ty, _) ;
439
+ ret ty_of_fn_decl( id_to_ty_item, item_to_ty, convert, f,
440
+ fn_decl, def_id) ;
441
+ }
442
+ case ( ast. native_item_ty( _, ?def_id) ) {
443
+ if ( item_to_ty. contains_key( def_id) ) {
444
+ // Avoid repeating work.
445
+ ret item_to_ty. get( def_id) ;
446
+ }
447
+ auto x =
448
+ @rec( struct=ty. ty_native, mut =ast. imm, cname=none[ str] ) ;
449
+ item_to_ty. insert( def_id, x) ;
450
+ ret x;
451
+ }
452
+ }
453
+ }
454
+
388
455
fn get_tag_variant_types( @ty_item_table id_to_ty_item,
389
456
@ty_table item_to_ty,
390
457
& ast. def_id tag_id,
@@ -422,25 +489,38 @@ fn collect_item_types(session.session sess, @ast.crate crate)
422
489
423
490
// First pass: collect all type item IDs.
424
491
auto module = crate . node. module;
425
- auto id_to_ty_item = @common. new_def_hash[ @ast . item ] ( ) ;
492
+ auto id_to_ty_item = @common. new_def_hash[ any_item ] ( ) ;
426
493
fn collect( & @ty_item_table id_to_ty_item, @ast. item i)
427
494
-> @ty_item_table {
428
495
alt ( i. node) {
429
496
case ( ast. item_ty( _, _, _, ?def_id, _) ) {
430
- id_to_ty_item. insert( def_id, i ) ;
497
+ id_to_ty_item. insert( def_id, any_item_rust ( i ) ) ;
431
498
}
432
499
case ( ast. item_tag( _, _, _, ?def_id) ) {
433
- id_to_ty_item. insert( def_id, i ) ;
500
+ id_to_ty_item. insert( def_id, any_item_rust ( i ) ) ;
434
501
}
435
502
case ( ast. item_obj( _, _, _, ?def_id, _) ) {
436
- id_to_ty_item. insert( def_id, i ) ;
503
+ id_to_ty_item. insert( def_id, any_item_rust ( i ) ) ;
437
504
}
438
505
case ( _) { /* empty */ }
439
506
}
440
507
ret id_to_ty_item;
441
508
}
509
+ fn collect_native( & @ty_item_table id_to_ty_item, @ast. native_item i)
510
+ -> @ty_item_table {
511
+ alt ( i. node) {
512
+ case ( ast. native_item_ty( _, ?def_id) ) {
513
+ id_to_ty_item. insert( def_id, any_item_native( i) ) ;
514
+ }
515
+ case ( ast. native_item_fn( _, _, _, ?def_id) ) {
516
+ id_to_ty_item. insert( def_id, any_item_native( i) ) ;
517
+ }
518
+ }
519
+ ret id_to_ty_item;
520
+ }
442
521
auto fld_1 = fold. new_identity_fold[ @ty_item_table] ( ) ;
443
- fld_1 = @rec( update_env_for_item = bind collect( _, _)
522
+ fld_1 = @rec( update_env_for_item = bind collect( _, _) ,
523
+ update_env_for_native_item = bind collect_native( _, _)
444
524
with * fld_1) ;
445
525
fold. fold_crate[ @ty_item_table] ( id_to_ty_item, fld_1, crate ) ;
446
526
@@ -473,6 +553,11 @@ fn collect_item_types(session.session sess, @ast.crate crate)
473
553
ret e;
474
554
}
475
555
556
+ fn convert_native( & @env e, @ast. native_item i) -> @env {
557
+ ty_of_native_item( e. id_to_ty_item, e. item_to_ty, i) ;
558
+ ret e;
559
+ }
560
+
476
561
fn fold_item_const( & @env e, & span sp, ast. ident i,
477
562
@ast. ty t, @ast. expr ex,
478
563
ast. def_id id, ast. ann a) -> @ast. item {
@@ -575,6 +660,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
575
660
auto fld_2 = fold. new_identity_fold [ @env] ( ) ;
576
661
fld_2 =
577
662
@rec ( update_env_for_item = bind convert ( _, _) ,
663
+ update_env_for_native_item = bind convert_native ( _, _) ,
578
664
fold_item_const = bind fold_item_const ( _, _, _, _, _, _, _) ,
579
665
fold_item_fn = bind fold_item_fn ( _, _, _, _, _, _, _) ,
580
666
fold_item_obj = bind fold_item_obj ( _, _, _, _, _, _, _) ,
@@ -1150,6 +1236,10 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
1150
1236
check ( fcx. ccx . item_types . contains_key ( id) ) ;
1151
1237
t = generalize_ty ( fcx. ccx , fcx. ccx . item_types . get ( id) ) ;
1152
1238
}
1239
+ case ( ast. def_native_fn ( ?id) ) {
1240
+ check ( fcx. ccx . item_types . contains_key ( id) ) ;
1241
+ t = generalize_ty ( fcx. ccx , fcx. ccx . item_types . get ( id) ) ;
1242
+ }
1153
1243
case ( ast. def_const ( ?id) ) {
1154
1244
check ( fcx. ccx . item_types . contains_key ( id) ) ;
1155
1245
t = fcx. ccx . item_types . get ( id) ;
0 commit comments