/
typeck.rs
2966 lines (2806 loc) · 108 KB
/
typeck.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import syntax::{ast, ast_util};
import ast::spanned;
import syntax::ast_util::{local_def, respan};
import syntax::visit;
import metadata::csearch;
import driver::session::session;
import util::common::*;
import syntax::codemap::span;
import pat_util::*;
import middle::ty;
import middle::ty::{node_id_to_type, arg, block_ty,
expr_ty, field, node_type_table, mk_nil,
ty_param_bounds_and_ty};
import util::ppaux::ty_to_str;
import middle::ty::unify::{ures_ok, ures_err, fix_ok, fix_err};
import core::{int, vec, str, option};
import std::smallintmap;
import std::map::{hashmap, new_int_hash};
import option::{none, some};
import syntax::print::pprust::*;
export check_crate;
export method_map, method_origin, method_static, method_param, method_iface;
export dict_map, dict_res, dict_origin, dict_static, dict_param, dict_iface;
enum method_origin {
method_static(ast::def_id),
// iface id, method num, param num, bound num
method_param(ast::def_id, uint, uint, uint),
method_iface(uint),
}
type method_map = hashmap<ast::node_id, method_origin>;
// Resolutions for bounds of all parameters, left to right, for a given path.
type dict_res = @[dict_origin];
enum dict_origin {
dict_static(ast::def_id, [ty::t], dict_res),
// Param number, bound number
dict_param(uint, uint),
dict_iface(ast::def_id),
}
type dict_map = hashmap<ast::node_id, dict_res>;
type ty_table = hashmap<ast::def_id, ty::t>;
// Used for typechecking the methods of an impl
enum self_info {
self_impl(ty::t),
}
type crate_ctxt = {mutable self_infos: [self_info],
impl_map: resolve::impl_map,
method_map: method_map,
dict_map: dict_map,
tcx: ty::ctxt};
type fn_ctxt =
// var_bindings, locals and next_var_id are shared
// with any nested functions that capture the environment
// (and with any functions whose environment is being captured).
{ret_ty: ty::t,
purity: ast::purity,
proto: ast::proto,
var_bindings: @ty::unify::var_bindings,
locals: hashmap<ast::node_id, int>,
next_var_id: @mutable int,
mutable fixups: [ast::node_id],
ccx: @crate_ctxt};
fn lookup_local(fcx: @fn_ctxt, sp: span, id: ast::node_id) -> int {
alt fcx.locals.find(id) {
some(x) { x }
_ {
fcx.ccx.tcx.sess.span_fatal(sp,
"internal error looking up a local var")
}
}
}
fn lookup_def(fcx: @fn_ctxt, sp: span, id: ast::node_id) -> ast::def {
alt fcx.ccx.tcx.def_map.find(id) {
some(x) { x }
_ {
fcx.ccx.tcx.sess.span_fatal(sp,
"internal error looking up a definition")
}
}
}
// Returns the type parameter count and the type for the given definition.
fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) ->
ty_param_bounds_and_ty {
alt defn {
ast::def_arg(id, _) {
assert (fcx.locals.contains_key(id.node));
let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, id.node));
ret {bounds: @[], ty: typ};
}
ast::def_local(id, _) {
assert (fcx.locals.contains_key(id.node));
let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, id.node));
ret {bounds: @[], ty: typ};
}
ast::def_self(id) {
alt get_self_info(fcx.ccx) {
some(self_impl(impl_t)) {
ret {bounds: @[], ty: impl_t};
}
}
}
ast::def_fn(id, _) | ast::def_const(id) |
ast::def_variant(_, id) { ret ty::lookup_item_type(fcx.ccx.tcx, id); }
ast::def_binding(id) {
assert (fcx.locals.contains_key(id.node));
let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, id.node));
ret {bounds: @[], ty: typ};
}
ast::def_mod(_) {
// Hopefully part of a path.
// TODO: return a type that's more poisonous, perhaps?
ret {bounds: @[], ty: ty::mk_nil(fcx.ccx.tcx)};
}
ast::def_ty(_) {
fcx.ccx.tcx.sess.span_fatal(sp, "expected value but found type");
}
ast::def_upvar(_, inner, _) {
ret ty_param_bounds_and_ty_for_def(fcx, sp, *inner);
}
_ {
// FIXME: handle other names.
fcx.ccx.tcx.sess.unimpl("definition variant");
}
}
}
// Instantiates the given path, which must refer to an item with the given
// number of type parameters and type.
fn instantiate_path(fcx: @fn_ctxt, pth: @ast::path,
tpt: ty_param_bounds_and_ty, sp: span,
id: ast::node_id) {
let ty_param_count = vec::len(*tpt.bounds);
let ty_substs_len = vec::len(pth.node.types);
if ty_substs_len > 0u {
if ty_param_count == 0u {
fcx.ccx.tcx.sess.span_fatal
(sp, "this item does not take type parameters");
} else if ty_substs_len > ty_param_count {
fcx.ccx.tcx.sess.span_fatal
(sp, "too many type parameter provided for this item");
} else if ty_substs_len < ty_param_count {
fcx.ccx.tcx.sess.span_fatal
(sp, "not enough type parameters provided for this item");
}
if ty_param_count == 0u {
fcx.ccx.tcx.sess.span_fatal(
sp, "this item does not take type parameters");
}
let substs = vec::map(pth.node.types, {|aty|
ast_ty_to_ty_crate(fcx.ccx, aty)
});
write_ty_substs(fcx.ccx.tcx, id, tpt.ty, substs);
} else if ty_param_count > 0u {
let vars = vec::init_fn(ty_param_count, {|_i| next_ty_var(fcx)});
write_ty_substs(fcx.ccx.tcx, id, tpt.ty, vars);
} else {
write_ty(fcx.ccx.tcx, id, tpt.ty);
}
}
// Type tests
fn structurally_resolved_type(fcx: @fn_ctxt, sp: span, tp: ty::t) -> ty::t {
alt ty::unify::resolve_type_structure(fcx.ccx.tcx, fcx.var_bindings, tp) {
fix_ok(typ_s) { ret typ_s; }
fix_err(_) {
fcx.ccx.tcx.sess.span_fatal
(sp, "the type of this value must be known in this context");
}
}
}
// Returns the one-level-deep structure of the given type.f
fn structure_of(fcx: @fn_ctxt, sp: span, typ: ty::t) -> ty::sty {
ret ty::struct(fcx.ccx.tcx, structurally_resolved_type(fcx, sp, typ));
}
// Returns the one-level-deep structure of the given type or none if it
// is not known yet.
fn structure_of_maybe(fcx: @fn_ctxt, _sp: span, typ: ty::t) ->
option::t<ty::sty> {
let r =
ty::unify::resolve_type_structure(fcx.ccx.tcx, fcx.var_bindings, typ);
ret alt r {
fix_ok(typ_s) { some(ty::struct(fcx.ccx.tcx, typ_s)) }
fix_err(_) { none }
}
}
fn type_is_integral(fcx: @fn_ctxt, sp: span, typ: ty::t) -> bool {
let typ_s = structurally_resolved_type(fcx, sp, typ);
ret ty::type_is_integral(fcx.ccx.tcx, typ_s);
}
fn type_is_scalar(fcx: @fn_ctxt, sp: span, typ: ty::t) -> bool {
let typ_s = structurally_resolved_type(fcx, sp, typ);
ret ty::type_is_scalar(fcx.ccx.tcx, typ_s);
}
fn type_is_c_like_enum(fcx: @fn_ctxt, sp: span, typ: ty::t) -> bool {
let typ_s = structurally_resolved_type(fcx, sp, typ);
ret ty::type_is_c_like_enum(fcx.ccx.tcx, typ_s);
}
// Parses the programmer's textual representation of a type into our internal
// notion of a type. `getter` is a function that returns the type
// corresponding to a definition ID:
fn default_arg_mode_for_ty(tcx: ty::ctxt, m: ast::mode,
ty: ty::t) -> ast::mode {
alt m {
ast::mode_infer {
alt ty::struct(tcx, ty) {
ty::ty_var(_) { ast::mode_infer }
_ {
if ty::type_is_immediate(tcx, ty) { ast::by_val }
else { ast::by_ref }
}
}
}
_ { m }
}
}
enum mode { m_collect, m_check, m_check_tyvar(@fn_ctxt), }
fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
fn getter(tcx: ty::ctxt, mode: mode, id: ast::def_id)
-> ty::ty_param_bounds_and_ty {
alt mode {
m_check | m_check_tyvar(_) { ty::lookup_item_type(tcx, id) }
m_collect {
if id.crate != ast::local_crate { csearch::get_type(tcx, id) }
else {
alt tcx.items.find(id.node) {
some(ast_map::node_item(item)) {
ty_of_item(tcx, mode, item)
}
some(ast_map::node_native_item(native_item)) {
ty_of_native_item(tcx, mode, native_item)
}
}
}
}
}
}
fn ast_arg_to_arg(tcx: ty::ctxt, mode: mode, arg: ast::arg)
-> {mode: ty::mode, ty: ty::t} {
let ty = ast_ty_to_ty(tcx, mode, arg.ty);
ret {mode: default_arg_mode_for_ty(tcx, arg.mode, ty), ty: ty};
}
alt tcx.ast_ty_to_ty_cache.find(ast_ty) {
some(some(ty)) { ret ty; }
some(none) {
tcx.sess.span_fatal(ast_ty.span, "illegal recursive type. \
insert a enum in the cycle, \
if this is desired)");
}
none { }
} /* go on */
tcx.ast_ty_to_ty_cache.insert(ast_ty, none::<ty::t>);
fn ast_mt_to_mt(tcx: ty::ctxt, mode: mode, mt: ast::mt) -> ty::mt {
ret {ty: ast_ty_to_ty(tcx, mode, mt.ty), mut: mt.mut};
}
fn instantiate(tcx: ty::ctxt, sp: span, mode: mode,
id: ast::def_id, args: [@ast::ty]) -> ty::t {
// TODO: maybe record cname chains so we can do
// "foo = int" like OCaml?
let ty_param_bounds_and_ty = getter(tcx, mode, id);
if vec::len(*ty_param_bounds_and_ty.bounds) == 0u {
ret ty_param_bounds_and_ty.ty;
}
// The typedef is type-parametric. Do the type substitution.
let param_bindings: [ty::t] = [];
if vec::len(args) != vec::len(*ty_param_bounds_and_ty.bounds) {
tcx.sess.span_fatal(sp, "Wrong number of type arguments for a \
polymorphic type");
}
for ast_ty: @ast::ty in args {
param_bindings += [ast_ty_to_ty(tcx, mode, ast_ty)];
}
let typ =
ty::substitute_type_params(tcx, param_bindings,
ty_param_bounds_and_ty.ty);
ret typ;
}
let typ;
alt ast_ty.node {
ast::ty_nil { typ = ty::mk_nil(tcx); }
ast::ty_bot { typ = ty::mk_bot(tcx); }
ast::ty_bool { typ = ty::mk_bool(tcx); }
ast::ty_int(it) { typ = ty::mk_mach_int(tcx, it); }
ast::ty_uint(uit) { typ = ty::mk_mach_uint(tcx, uit); }
ast::ty_float(ft) { typ = ty::mk_mach_float(tcx, ft); }
ast::ty_str { typ = ty::mk_str(tcx); }
ast::ty_box(mt) {
typ = ty::mk_box(tcx, ast_mt_to_mt(tcx, mode, mt));
}
ast::ty_uniq(mt) {
typ = ty::mk_uniq(tcx, ast_mt_to_mt(tcx, mode, mt));
}
ast::ty_vec(mt) {
typ = ty::mk_vec(tcx, ast_mt_to_mt(tcx, mode, mt));
}
ast::ty_ptr(mt) {
typ = ty::mk_ptr(tcx, ast_mt_to_mt(tcx, mode, mt));
}
ast::ty_tup(fields) {
let flds = vec::map(fields, bind ast_ty_to_ty(tcx, mode, _));
typ = ty::mk_tup(tcx, flds);
}
ast::ty_rec(fields) {
let flds: [field] = [];
for f: ast::ty_field in fields {
let tm = ast_mt_to_mt(tcx, mode, f.node.mt);
flds += [{ident: f.node.ident, mt: tm}];
}
typ = ty::mk_rec(tcx, flds);
}
ast::ty_fn(proto, decl) {
typ = ty::mk_fn(tcx, ty_of_fn_decl(tcx, mode, proto, decl));
}
ast::ty_path(path, id) {
alt tcx.def_map.find(id) {
some(ast::def_ty(id)) {
typ = instantiate(tcx, ast_ty.span, mode, id, path.node.types);
}
some(ast::def_native_ty(id)) { typ = getter(tcx, mode, id).ty; }
some(ast::def_ty_param(id, n)) {
typ = ty::mk_param(tcx, n, id);
}
some(_) {
tcx.sess.span_fatal(ast_ty.span,
"found type name used as a variable");
}
_ {
tcx.sess.span_fatal(ast_ty.span, "internal error in instantiate");
}
}
}
ast::ty_constr(t, cs) {
let out_cs = [];
for constr: @ast::ty_constr in cs {
out_cs += [ty::ast_constr_to_constr(tcx, constr)];
}
typ = ty::mk_constr(tcx, ast_ty_to_ty(tcx, mode, t), out_cs);
}
ast::ty_infer {
alt mode {
m_check_tyvar(fcx) { ret next_ty_var(fcx); }
_ { tcx.sess.span_bug(ast_ty.span,
"found `ty_infer` in unexpected place"); }
}
}
}
tcx.ast_ty_to_ty_cache.insert(ast_ty, some(typ));
ret typ;
}
fn ty_of_item(tcx: ty::ctxt, mode: mode, it: @ast::item)
-> ty::ty_param_bounds_and_ty {
alt it.node {
ast::item_const(t, _) {
let typ = ast_ty_to_ty(tcx, mode, t);
let tpt = {bounds: @[], ty: typ};
tcx.tcache.insert(local_def(it.id), tpt);
ret tpt;
}
ast::item_fn(decl, tps, _) {
ret ty_of_fn(tcx, mode, decl, tps, local_def(it.id));
}
ast::item_ty(t, tps) {
alt tcx.tcache.find(local_def(it.id)) {
some(tpt) { ret tpt; }
none { }
}
// Tell ast_ty_to_ty() that we want to perform a recursive
// call to resolve any named types.
let tpt = {bounds: ty_param_bounds(tcx, mode, tps),
ty: ty::mk_named(tcx, ast_ty_to_ty(tcx, mode, t),
@it.ident)};
tcx.tcache.insert(local_def(it.id), tpt);
ret tpt;
}
ast::item_res(decl, tps, _, _, _) {
let {bounds, params} = mk_ty_params(tcx, tps);
let t_arg = ty_of_arg(tcx, mode, decl.inputs[0]);
let t = ty::mk_named(tcx, ty::mk_res(tcx, local_def(it.id), t_arg.ty,
params),
@it.ident);
let t_res = {bounds: bounds, ty: t};
tcx.tcache.insert(local_def(it.id), t_res);
ret t_res;
}
ast::item_enum(_, tps) {
// Create a new generic polytype.
let {bounds, params} = mk_ty_params(tcx, tps);
let t = ty::mk_named(tcx, ty::mk_enum(tcx, local_def(it.id), params),
@it.ident);
let tpt = {bounds: bounds, ty: t};
tcx.tcache.insert(local_def(it.id), tpt);
ret tpt;
}
ast::item_iface(tps, ms) {
let {bounds, params} = mk_ty_params(tcx, tps);
let t = ty::mk_named(tcx, ty::mk_iface(tcx, local_def(it.id),
params),
@it.ident);
let tpt = {bounds: bounds, ty: t};
tcx.tcache.insert(local_def(it.id), tpt);
ret tpt;
}
ast::item_impl(_, _, _, _) | ast::item_mod(_) |
ast::item_native_mod(_) { fail; }
}
}
fn ty_of_native_item(tcx: ty::ctxt, mode: mode, it: @ast::native_item)
-> ty::ty_param_bounds_and_ty {
alt it.node {
ast::native_item_fn(fn_decl, params) {
ret ty_of_native_fn_decl(tcx, mode, fn_decl, params,
local_def(it.id));
}
ast::native_item_ty {
alt tcx.tcache.find(local_def(it.id)) {
some(tpt) { ret tpt; }
none { }
}
let t = ty::mk_native(tcx, local_def(it.id));
let t = ty::mk_named(tcx, t, @it.ident);
let tpt = {bounds: @[], ty: t};
tcx.tcache.insert(local_def(it.id), tpt);
ret tpt;
}
}
}
fn ty_of_arg(tcx: ty::ctxt, mode: mode, a: ast::arg) -> ty::arg {
let ty = ast_ty_to_ty(tcx, mode, a.ty);
{mode: default_arg_mode_for_ty(tcx, a.mode, ty), ty: ty}
}
fn ty_of_fn_decl(tcx: ty::ctxt, mode: mode,
proto: ast::proto, decl: ast::fn_decl) -> ty::fn_ty {
let input_tys = [];
for a: ast::arg in decl.inputs { input_tys += [ty_of_arg(tcx, mode, a)]; }
let output_ty = ast_ty_to_ty(tcx, mode, decl.output);
let out_constrs = [];
for constr: @ast::constr in decl.constraints {
out_constrs += [ty::ast_constr_to_constr(tcx, constr)];
}
{proto: proto, inputs: input_tys,
output: output_ty, ret_style: decl.cf, constraints: out_constrs}
}
fn ty_of_fn(tcx: ty::ctxt, mode: mode, decl: ast::fn_decl,
ty_params: [ast::ty_param], def_id: ast::def_id)
-> ty::ty_param_bounds_and_ty {
let bounds = ty_param_bounds(tcx, mode, ty_params);
let tofd = ty_of_fn_decl(tcx, mode, ast::proto_bare, decl);
let tpt = {bounds: bounds, ty: ty::mk_fn(tcx, tofd)};
tcx.tcache.insert(def_id, tpt);
ret tpt;
}
fn ty_of_native_fn_decl(tcx: ty::ctxt, mode: mode, decl: ast::fn_decl,
ty_params: [ast::ty_param], def_id: ast::def_id)
-> ty::ty_param_bounds_and_ty {
let input_tys = [], bounds = ty_param_bounds(tcx, mode, ty_params);
for a: ast::arg in decl.inputs { input_tys += [ty_of_arg(tcx, mode, a)]; }
let output_ty = ast_ty_to_ty(tcx, mode, decl.output);
let t_fn = ty::mk_fn(tcx, {proto: ast::proto_bare,
inputs: input_tys,
output: output_ty,
ret_style: ast::return_val,
constraints: []});
let tpt = {bounds: bounds, ty: t_fn};
tcx.tcache.insert(def_id, tpt);
ret tpt;
}
fn ty_param_bounds(tcx: ty::ctxt, mode: mode, params: [ast::ty_param])
-> @[ty::param_bounds] {
let result = [];
for param in params {
result += [alt tcx.ty_param_bounds.find(param.id) {
some(bs) { bs }
none {
let bounds = [];
for b in *param.bounds {
bounds += [alt b {
ast::bound_send { ty::bound_send }
ast::bound_copy { ty::bound_copy }
ast::bound_iface(t) {
let ity = ast_ty_to_ty(tcx, mode, t);
alt ty::struct(tcx, ity) {
ty::ty_iface(_, _) {}
_ {
tcx.sess.span_fatal(
t.span, "type parameter bounds must be \
interface types");
}
}
ty::bound_iface(ity)
}
}];
}
let boxed = @bounds;
tcx.ty_param_bounds.insert(param.id, boxed);
boxed
}
}];
}
@result
}
fn ty_of_method(tcx: ty::ctxt, mode: mode, m: @ast::method) -> ty::method {
{ident: m.ident, tps: ty_param_bounds(tcx, mode, m.tps),
fty: ty_of_fn_decl(tcx, mode, ast::proto_bare, m.decl)}
}
fn ty_of_ty_method(tcx: ty::ctxt, mode: mode, m: ast::ty_method)
-> ty::method {
{ident: m.ident, tps: ty_param_bounds(tcx, mode, m.tps),
fty: ty_of_fn_decl(tcx, mode, ast::proto_bare, m.decl)}
}
// A convenience function to use a crate_ctxt to resolve names for
// ast_ty_to_ty.
fn ast_ty_to_ty_crate(ccx: @crate_ctxt, &&ast_ty: @ast::ty) -> ty::t {
ret ast_ty_to_ty(ccx.tcx, m_check, ast_ty);
}
// A wrapper around ast_ty_to_ty_crate that handles ty_infer.
fn ast_ty_to_ty_crate_infer(ccx: @crate_ctxt, &&ast_ty: @ast::ty) ->
option::t<ty::t> {
alt ast_ty.node {
ast::ty_infer { none }
_ { some(ast_ty_to_ty_crate(ccx, ast_ty)) }
}
}
// Functions that write types into the node type table
fn write_ty(tcx: ty::ctxt, node_id: ast::node_id, ty: ty::t) {
smallintmap::insert(*tcx.node_types, node_id as uint, ty);
}
fn write_substs(tcx: ty::ctxt, node_id: ast::node_id, +substs: [ty::t]) {
tcx.node_type_substs.insert(node_id, substs);
}
fn write_ty_substs(tcx: ty::ctxt, node_id: ast::node_id, ty: ty::t,
+substs: [ty::t]) {
let ty = if ty::type_contains_params(tcx, ty) {
ty::substitute_type_params(tcx, substs, ty)
} else { ty };
write_ty(tcx, node_id, ty);
write_substs(tcx, node_id, substs);
}
fn write_nil(tcx: ty::ctxt, node_id: ast::node_id) {
write_ty(tcx, node_id, ty::mk_nil(tcx));
}
fn write_bot(tcx: ty::ctxt, node_id: ast::node_id) {
write_ty(tcx, node_id, ty::mk_bot(tcx));
}
fn mk_ty_params(tcx: ty::ctxt, atps: [ast::ty_param])
-> {bounds: @[ty::param_bounds], params: [ty::t]} {
let i = 0u, bounds = ty_param_bounds(tcx, m_collect, atps);
{bounds: bounds,
params: vec::map(atps, {|atp|
let t = ty::mk_param(tcx, i, local_def(atp.id));
i += 1u;
t
})}
}
fn compare_impl_method(tcx: ty::ctxt, sp: span, impl_m: ty::method,
impl_tps: uint, if_m: ty::method, substs: [ty::t]) {
if impl_m.tps != if_m.tps {
tcx.sess.span_err(sp, "method `" + if_m.ident +
"` has an incompatible set of type parameters");
} else {
let impl_fty = ty::mk_fn(tcx, impl_m.fty);
// Add dummy substs for the parameters of the impl method
let substs = substs + vec::init_fn(vec::len(*if_m.tps), {|i|
ty::mk_param(tcx, i + impl_tps, {crate: 0, node: 0})
});
let if_fty = ty::substitute_type_params(tcx, substs,
ty::mk_fn(tcx, if_m.fty));
alt ty::unify::unify(impl_fty, if_fty, ty::unify::precise, tcx) {
ty::unify::ures_err(err) {
tcx.sess.span_err(sp, "method `" + if_m.ident +
"` has an incompatible type: " +
ty::type_err_to_str(err));
}
_ {}
}
}
}
// Item collection - a pair of bootstrap passes:
//
// (1) Collect the IDs of all type items (typedefs) and store them in a table.
//
// (2) Translate the AST fragments that describe types to determine a type for
// each item. When we encounter a named type, we consult the table built
// in pass 1 to find its item, and recursively translate it.
//
// We then annotate the AST with the resulting types and return the annotated
// AST, along with a table mapping item IDs to their types.
//
// TODO: This logic is quite convoluted; it's a relic of the time when we
// actually wrote types directly into the AST and didn't have a type cache.
// Could use some cleanup. Consider topologically sorting in phase (1) above.
mod collect {
type ctxt = {tcx: ty::ctxt};
fn get_enum_variant_types(cx: @ctxt, enum_ty: ty::t,
variants: [ast::variant],
ty_params: [ast::ty_param]) {
// Create a set of parameter types shared among all the variants.
for variant: ast::variant in variants {
// Nullary enum constructors get turned into constants; n-ary enum
// constructors get turned into functions.
let result_ty = if vec::len(variant.node.args) == 0u {
enum_ty
} else {
// As above, tell ast_ty_to_ty() that trans_ty_item_to_ty()
// should be called to resolve named types.
let args: [arg] = [];
for va: ast::variant_arg in variant.node.args {
let arg_ty = ast_ty_to_ty(cx.tcx, m_collect, va.ty);
args += [{mode: ast::by_copy, ty: arg_ty}];
}
// FIXME: this will be different for constrained types
ty::mk_fn(cx.tcx,
{proto: ast::proto_box,
inputs: args, output: enum_ty,
ret_style: ast::return_val, constraints: []})
};
let tpt = {bounds: ty_param_bounds(cx.tcx, m_collect, ty_params),
ty: result_ty};
cx.tcx.tcache.insert(local_def(variant.node.id), tpt);
write_ty(cx.tcx, variant.node.id, result_ty);
}
}
fn convert(cx: @ctxt, it: @ast::item) {
alt it.node {
// These don't define types.
ast::item_mod(_) | ast::item_native_mod(_) {}
ast::item_enum(variants, ty_params) {
let tpt = ty_of_item(cx.tcx, m_collect, it);
write_ty(cx.tcx, it.id, tpt.ty);
get_enum_variant_types(cx, tpt.ty, variants, ty_params);
}
ast::item_impl(tps, ifce, selfty, ms) {
let i_bounds = ty_param_bounds(cx.tcx, m_collect, tps);
let my_methods = [];
for m in ms {
let bounds = ty_param_bounds(cx.tcx, m_collect, m.tps);
let mty = ty_of_method(cx.tcx, m_collect, m);
my_methods += [mty];
let fty = ty::mk_fn(cx.tcx, mty.fty);
cx.tcx.tcache.insert(local_def(m.id),
{bounds: @(*i_bounds + *bounds),
ty: fty});
write_ty(cx.tcx, m.id, fty);
}
write_ty(cx.tcx, it.id, ast_ty_to_ty(cx.tcx, m_collect,
selfty));
alt ifce {
some(t) {
let iface_ty = ast_ty_to_ty(cx.tcx, m_collect, t);
cx.tcx.tcache.insert(local_def(it.id),
{bounds: i_bounds, ty: iface_ty});
alt ty::struct(cx.tcx, iface_ty) {
ty::ty_iface(did, tys) {
for if_m in *ty::iface_methods(cx.tcx, did) {
alt vec::find(my_methods,
{|m| if_m.ident == m.ident}) {
some(m) {
compare_impl_method(cx.tcx, t.span, m,
vec::len(tps), if_m, tys);
}
none {
cx.tcx.sess.span_err(t.span, "missing method `" +
if_m.ident + "`");
}
}
}
}
_ {
cx.tcx.sess.span_fatal(t.span, "can only implement \
interface types");
}
}
}
_ {}
}
}
ast::item_res(decl, tps, _, dtor_id, ctor_id) {
let {bounds, params} = mk_ty_params(cx.tcx, tps);
let t_arg = ty_of_arg(cx.tcx, m_collect, decl.inputs[0]);
let t_res = ty::mk_res(cx.tcx, local_def(it.id), t_arg.ty,
params);
let t_ctor = ty::mk_fn(cx.tcx, {
proto: ast::proto_box,
inputs: [{mode: ast::by_copy with t_arg}],
output: t_res,
ret_style: ast::return_val, constraints: []
});
let t_dtor = ty::mk_fn(cx.tcx, {
proto: ast::proto_box,
inputs: [t_arg], output: ty::mk_nil(cx.tcx),
ret_style: ast::return_val, constraints: []
});
write_ty(cx.tcx, it.id, t_res);
write_ty(cx.tcx, ctor_id, t_ctor);
cx.tcx.tcache.insert(local_def(ctor_id),
{bounds: bounds, ty: t_ctor});
write_ty(cx.tcx, dtor_id, t_dtor);
}
ast::item_iface(_, ms) {
let tpt = ty_of_item(cx.tcx, m_collect, it);
write_ty(cx.tcx, it.id, tpt.ty);
ty::store_iface_methods(cx.tcx, it.id, @vec::map(ms, {|m|
ty_of_ty_method(cx.tcx, m_collect, m)
}));
}
_ {
// This call populates the type cache with the converted type
// of the item in passing. All we have to do here is to write
// it into the node type table.
let tpt = ty_of_item(cx.tcx, m_collect, it);
write_ty(cx.tcx, it.id, tpt.ty);
}
}
}
fn convert_native(cx: @ctxt, i: @ast::native_item) {
// As above, this call populates the type table with the converted
// type of the native item. We simply write it into the node type
// table.
let tpt = ty_of_native_item(cx.tcx, m_collect, i);
alt i.node {
ast::native_item_ty {
// FIXME: Native types have no annotation. Should they? --pcw
}
ast::native_item_fn(_, _) {
write_ty(cx.tcx, i.id, tpt.ty);
}
}
}
fn collect_item_types(tcx: ty::ctxt, crate: @ast::crate) {
let cx = @{tcx: tcx};
let visit =
visit::mk_simple_visitor(@{visit_item: bind convert(cx, _),
visit_native_item:
bind convert_native(cx, _)
with
*visit::default_simple_visitor()});
visit::visit_crate(*crate, (), visit);
}
}
// Type unification
mod unify {
fn unify(fcx: @fn_ctxt, expected: ty::t, actual: ty::t) ->
ty::unify::result {
ret ty::unify::unify(expected, actual,
ty::unify::in_bindings(fcx.var_bindings),
fcx.ccx.tcx);
}
}
// FIXME This is almost a duplicate of ty::type_autoderef, with structure_of
// instead of ty::struct.
fn do_autoderef(fcx: @fn_ctxt, sp: span, t: ty::t) -> ty::t {
let t1 = t;
while true {
alt structure_of(fcx, sp, t1) {
ty::ty_box(inner) | ty::ty_uniq(inner) {
alt ty::struct(fcx.ccx.tcx, t1) {
ty::ty_var(v1) {
if ty::occurs_check_fails(fcx.ccx.tcx, some(sp), v1,
ty::mk_box(fcx.ccx.tcx, inner)) {
break;
}
}
_ { }
}
t1 = inner.ty;
}
ty::ty_res(_, inner, tps) {
t1 = ty::substitute_type_params(fcx.ccx.tcx, tps, inner);
}
ty::ty_enum(did, tps) {
let variants = ty::enum_variants(fcx.ccx.tcx, did);
if vec::len(*variants) != 1u || vec::len(variants[0].args) != 1u {
ret t1;
}
t1 =
ty::substitute_type_params(fcx.ccx.tcx, tps,
variants[0].args[0]);
}
_ { ret t1; }
}
}
fail;
}
fn resolve_type_vars_if_possible(fcx: @fn_ctxt, typ: ty::t) -> ty::t {
alt ty::unify::fixup_vars(fcx.ccx.tcx, none, fcx.var_bindings, typ) {
fix_ok(new_type) { ret new_type; }
fix_err(_) { ret typ; }
}
}
// Demands - procedures that require that two types unify and emit an error
// message if they don't.
type ty_param_substs_and_ty = {substs: [ty::t], ty: ty::t};
mod demand {
fn simple(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t) ->
ty::t {
full(fcx, sp, expected, actual, []).ty
}
fn with_substs(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t,
ty_param_substs_0: [ty::t]) -> ty_param_substs_and_ty {
full(fcx, sp, expected, actual, ty_param_substs_0)
}
// Requires that the two types unify, and prints an error message if they
// don't. Returns the unified type and the type parameter substitutions.
fn full(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t,
ty_param_substs_0: [ty::t]) ->
ty_param_substs_and_ty {
let ty_param_substs: [mutable ty::t] = [mutable];
let ty_param_subst_var_ids: [int] = [];
for ty_param_subst: ty::t in ty_param_substs_0 {
// Generate a type variable and unify it with the type parameter
// substitution. We will then pull out these type variables.
let t_0 = next_ty_var(fcx);
ty_param_substs += [mutable t_0];
ty_param_subst_var_ids += [ty::ty_var_id(fcx.ccx.tcx, t_0)];
simple(fcx, sp, ty_param_subst, t_0);
}
fn mk_result(fcx: @fn_ctxt, result_ty: ty::t,
ty_param_subst_var_ids: [int]) ->
ty_param_substs_and_ty {
let result_ty_param_substs: [ty::t] = [];
for var_id: int in ty_param_subst_var_ids {
let tp_subst = ty::mk_var(fcx.ccx.tcx, var_id);
result_ty_param_substs += [tp_subst];
}
ret {substs: result_ty_param_substs, ty: result_ty};
}
alt unify::unify(fcx, expected, actual) {
ures_ok(t) { ret mk_result(fcx, t, ty_param_subst_var_ids); }
ures_err(err) {
let e_err = resolve_type_vars_if_possible(fcx, expected);
let a_err = resolve_type_vars_if_possible(fcx, actual);
fcx.ccx.tcx.sess.span_err(sp,
"mismatched types: expected `" +
ty_to_str(fcx.ccx.tcx, e_err) +
"` but found `" +
ty_to_str(fcx.ccx.tcx, a_err) +
"` (" + ty::type_err_to_str(err) +
")");
ret mk_result(fcx, expected, ty_param_subst_var_ids);
}
}
}
}
// Returns true if the two types unify and false if they don't.
fn are_compatible(fcx: @fn_ctxt, expected: ty::t, actual: ty::t) -> bool {
alt unify::unify(fcx, expected, actual) {
ures_ok(_) { ret true; }
ures_err(_) { ret false; }
}
}
// Returns the types of the arguments to a enum variant.
fn variant_arg_types(ccx: @crate_ctxt, _sp: span, vid: ast::def_id,
enum_ty_params: [ty::t]) -> [ty::t] {
let result: [ty::t] = [];
let tpt = ty::lookup_item_type(ccx.tcx, vid);
alt ty::struct(ccx.tcx, tpt.ty) {
ty::ty_fn(f) {
// N-ary variant.
for arg: ty::arg in f.inputs {
let arg_ty =
ty::substitute_type_params(ccx.tcx, enum_ty_params, arg.ty);
result += [arg_ty];
}
}
_ {
// Nullary variant. Do nothing, as there are no arguments.
}
}
/* result is a vector of the *expected* types of all the fields */
ret result;
}
// Type resolution: the phase that finds all the types in the AST with
// unresolved type variables and replaces "ty_var" types with their
// substitutions.
//
// TODO: inefficient since not all types have vars in them. It would be better
// to maintain a list of fixups.
mod writeback {
export resolve_type_vars_in_block;
export resolve_type_vars_in_expr;
fn resolve_type_vars_in_type(fcx: @fn_ctxt, sp: span, typ: ty::t) ->
option::t<ty::t> {
if !ty::type_contains_vars(fcx.ccx.tcx, typ) { ret some(typ); }
alt ty::unify::fixup_vars(fcx.ccx.tcx, some(sp), fcx.var_bindings,
typ) {
fix_ok(new_type) { ret some(new_type); }
fix_err(vid) {
fcx.ccx.tcx.sess.span_err(sp, "cannot determine a type \
for this expression");
ret none;
}
}
}
fn resolve_type_vars_for_node(wbcx: wb_ctxt, sp: span, id: ast::node_id) {
let fcx = wbcx.fcx, tcx = fcx.ccx.tcx;
alt resolve_type_vars_in_type(fcx, sp, ty::node_id_to_type(tcx, id)) {
some(t) { write_ty(tcx, id, t); }
none { wbcx.success = false; ret }
}
alt tcx.node_type_substs.find(id) {
some(substs) {
let new_substs = [];
for subst: ty::t in substs {
alt resolve_type_vars_in_type(fcx, sp, subst) {
some(t) { new_substs += [t]; }
none { wbcx.success = false; ret; }
}
}
write_substs(tcx, id, new_substs);
}
none {}
}
}
type wb_ctxt =
// As soon as we hit an error we have to stop resolving
// the entire function
{fcx: @fn_ctxt, mutable success: bool};
type wb_vt = visit::vt<wb_ctxt>;
fn visit_stmt(s: @ast::stmt, wbcx: wb_ctxt, v: wb_vt) {
if !wbcx.success { ret; }
resolve_type_vars_for_node(wbcx, s.span, ty::stmt_node_id(s));
visit::visit_stmt(s, wbcx, v);
}
fn visit_expr(e: @ast::expr, wbcx: wb_ctxt, v: wb_vt) {
if !wbcx.success { ret; }
resolve_type_vars_for_node(wbcx, e.span, e.id);
alt e.node {
ast::expr_fn(_, decl, _, _) |
ast::expr_fn_block(decl, _) {
for input in decl.inputs {
resolve_type_vars_for_node(wbcx, e.span, input.id);
}
}
_ { }
}
visit::visit_expr(e, wbcx, v);
}
fn visit_block(b: ast::blk, wbcx: wb_ctxt, v: wb_vt) {
if !wbcx.success { ret; }
resolve_type_vars_for_node(wbcx, b.span, b.node.id);
visit::visit_block(b, wbcx, v);
}