forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathty.rs
2886 lines (2627 loc) · 89 KB
/
ty.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 vec;
import str;
import uint;
import std::ufind;
import std::map;
import std::map::hashmap;
import option;
import option::none;
import option::some;
import std::smallintmap;
import driver::session;
import syntax::ast;
import syntax::ast::*;
import syntax::ast_util;
import syntax::codemap::span;
import metadata::csearch;
import util::common::*;
import syntax::util::interner;
import util::ppaux::ty_to_str;
import util::ppaux::ty_constr_to_str;
import util::ppaux::mode_str;
import syntax::print::pprust::*;
export node_id_to_monotype;
export node_id_to_type;
export node_id_to_type_params;
export node_id_to_ty_param_substs_opt_and_ty;
export arg;
export args_eq;
export ast_constr_to_constr;
export block_ty;
export constr;
export constr_general;
export constr_table;
export count_ty_params;
export ctxt;
export def_has_ty_params;
export expr_has_ty_params;
export expr_ty;
export expr_ty_params_and_ty;
export expr_is_lval;
export fold_ty;
export field;
export field_idx;
export get_field;
export fm_general;
export get_element_type;
export idx_nil;
export is_binopable;
export is_pred_ty;
export lookup_item_type;
export method;
export method_idx;
export mk_bool;
export mk_bot;
export mk_box;
export mk_char;
export mk_constr;
export mk_ctxt;
export mk_float;
export mk_fn;
export mk_imm_box;
export mk_imm_uniq;
export mk_mut_ptr;
export mk_int;
export mk_str;
export mk_vec;
export mk_mach_int;
export mk_mach_uint;
export mk_mach_float;
export mk_native;
export mk_native_fn;
export mk_nil;
export mk_obj;
export mk_iface;
export mk_res;
export mk_param;
export mk_ptr;
export mk_rec;
export mk_tag;
export mk_tup;
export mk_type;
export mk_send_type;
export mk_uint;
export mk_uniq;
export mk_var;
export mk_opaque_closure_ptr;
export mk_named;
export gen_ty;
export mode;
export mt;
export node_type_table;
export pat_ty;
export ret_ty_of_fn;
export sequence_element_type;
export struct;
export ty_name;
export sort_methods;
export stmt_node_id;
export sty;
export substitute_type_params;
export t;
export new_ty_hash;
export tag_variants;
export iface_methods, store_iface_methods, impl_iface;
export tag_variant_with_id;
export ty_param_substs_opt_and_ty;
export ty_param_bounds_and_ty;
export ty_native_fn;
export ty_bool;
export ty_bot;
export ty_box;
export ty_constr;
export ty_opaque_closure_ptr;
export ty_constr_arg;
export ty_float;
export ty_fn, fn_ty;
export ty_fn_proto;
export ty_fn_ret;
export ty_fn_ret_style;
export ty_int;
export ty_str;
export ty_vec;
export ty_native;
export ty_nil;
export ty_obj;
export ty_iface;
export ty_res;
export ty_param;
export ty_ptr;
export ty_rec;
export ty_tag;
export ty_tup;
export ty_type;
export ty_send_type;
export ty_uint;
export ty_uniq;
export ty_var;
export ty_named;
export same_type, same_method;
export ty_var_id;
export ty_param_substs_opt_and_ty_to_monotype;
export ty_fn_args;
export type_constr;
export type_contains_params;
export type_contains_vars;
export kind, kind_sendable, kind_copyable, kind_noncopyable;
export kind_can_be_copied, kind_can_be_sent, proto_kind, kind_lteq, type_kind;
export type_err;
export type_err_to_str;
export type_has_dynamic_size;
export type_needs_drop;
export type_is_bool;
export type_is_bot;
export type_is_box;
export type_is_boxed;
export type_is_unique_box;
export type_is_unsafe_ptr;
export type_is_vec;
export type_is_fp;
export type_allows_implicit_copy;
export type_is_integral;
export type_is_numeric;
export type_is_native;
export type_is_nil;
export type_is_pod;
export type_is_scalar;
export type_is_immediate;
export type_is_sequence;
export type_is_signed;
export type_is_structural;
export type_is_copyable;
export type_is_tup_like;
export type_is_str;
export type_is_unique;
export type_is_tag;
export type_is_enum_like;
export type_structurally_contains_uniques;
export type_autoderef;
export type_param;
export unify;
export variant_info;
export walk_ty;
export occurs_check_fails;
export closure_kind;
export closure_block;
export closure_shared;
export closure_send;
export param_bound, param_bounds, bound_copy, bound_send, bound_iface;
export param_bounds_to_kind;
// Data types
type arg = {mode: mode, ty: t};
type field = {ident: ast::ident, mt: mt};
type param_bounds = @[param_bound];
type method = {ident: ast::ident, tps: @[param_bounds], fty: fn_ty};
type constr_table = hashmap<ast::node_id, [constr]>;
type mt = {ty: t, mut: ast::mutability};
// Contains information needed to resolve types and (in the future) look up
// the types of AST nodes.
type creader_cache = hashmap<{cnum: int, pos: uint, len: uint}, ty::t>;
type ctxt =
@{ts: @type_store,
sess: session::session,
def_map: resolve::def_map,
node_types: node_type_table,
items: ast_map::map,
freevars: freevars::freevar_map,
tcache: type_cache,
rcache: creader_cache,
short_names_cache: hashmap<t, @str>,
needs_drop_cache: hashmap<t, bool>,
kind_cache: hashmap<t, kind>,
ast_ty_to_ty_cache: hashmap<@ast::ty, option::t<t>>,
tag_var_cache: hashmap<def_id, @[variant_info]>,
iface_method_cache: hashmap<def_id, @[method]>,
ty_param_bounds: hashmap<ast::node_id, param_bounds>};
type ty_ctxt = ctxt;
// Never construct these manually. These are interned.
type raw_t = {struct: sty,
hash: uint,
has_params: bool,
has_vars: bool};
type t = uint;
tag closure_kind {
closure_block;
closure_shared;
closure_send;
}
type fn_ty = {proto: ast::proto,
inputs: [arg],
output: t,
ret_style: ret_style,
constraints: [@constr]};
// NB: If you change this, you'll probably want to change the corresponding
// AST structure in front/ast::rs as well.
tag sty {
ty_nil;
ty_bot;
ty_bool;
ty_int(ast::int_ty);
ty_uint(ast::uint_ty);
ty_float(ast::float_ty);
ty_str;
ty_tag(def_id, [t]);
ty_box(mt);
ty_uniq(mt);
ty_vec(mt);
ty_ptr(mt);
ty_rec([field]);
ty_fn(fn_ty);
ty_native_fn([arg], t);
ty_obj([method]);
ty_iface(def_id, [t]);
ty_res(def_id, t, [t]);
ty_tup([t]);
ty_var(int); // type variable
ty_param(uint, def_id); // fn/tag type param
ty_type; // type_desc*
ty_send_type; // type_desc* that has been cloned into exchange heap
ty_native(def_id);
ty_constr(t, [@type_constr]);
ty_opaque_closure_ptr(closure_kind); // ptr to env for fn, fn@, fn~
ty_named(t, @str);
}
// In the middle end, constraints have a def_id attached, referring
// to the definition of the operator in the constraint.
type constr_general<ARG> = spanned<constr_general_<ARG, def_id>>;
type type_constr = constr_general<@path>;
type constr = constr_general<uint>;
// Data structures used in type unification
tag type_err {
terr_mismatch;
terr_ret_style_mismatch(ast::ret_style, ast::ret_style);
terr_box_mutability;
terr_vec_mutability;
terr_tuple_size(uint, uint);
terr_record_size(uint, uint);
terr_record_mutability;
terr_record_fields(ast::ident, ast::ident);
terr_meth_count;
terr_obj_meths(ast::ident, ast::ident);
terr_arg_count;
terr_mode_mismatch(mode, mode);
terr_constr_len(uint, uint);
terr_constr_mismatch(@type_constr, @type_constr);
}
tag param_bound {
bound_copy;
bound_send;
bound_iface(t);
}
fn param_bounds_to_kind(bounds: param_bounds) -> kind {
let kind = kind_noncopyable;
for bound in *bounds {
alt bound {
bound_copy. {
if kind != kind_sendable { kind = kind_copyable; }
}
bound_send. { kind = kind_sendable; }
_ {}
}
}
kind
}
type ty_param_bounds_and_ty = {bounds: @[param_bounds], ty: t};
type type_cache = hashmap<ast::def_id, ty_param_bounds_and_ty>;
const idx_nil: uint = 0u;
const idx_bool: uint = 1u;
const idx_int: uint = 2u;
const idx_float: uint = 3u;
const idx_uint: uint = 4u;
const idx_i8: uint = 5u;
const idx_i16: uint = 6u;
const idx_i32: uint = 7u;
const idx_i64: uint = 8u;
const idx_u8: uint = 9u;
const idx_u16: uint = 10u;
const idx_u32: uint = 11u;
const idx_u64: uint = 12u;
const idx_f32: uint = 13u;
const idx_f64: uint = 14u;
const idx_char: uint = 15u;
const idx_str: uint = 16u;
const idx_type: uint = 17u;
const idx_send_type: uint = 18u;
const idx_bot: uint = 19u;
const idx_first_others: uint = 20u;
type type_store = interner::interner<@raw_t>;
type ty_param_substs_opt_and_ty = {substs: option::t<[ty::t]>, ty: ty::t};
type node_type_table =
@smallintmap::smallintmap<ty::ty_param_substs_opt_and_ty>;
fn populate_type_store(cx: ctxt) {
intern(cx, ty_nil);
intern(cx, ty_bool);
intern(cx, ty_int(ast::ty_i));
intern(cx, ty_float(ast::ty_f));
intern(cx, ty_uint(ast::ty_u));
intern(cx, ty_int(ast::ty_i8));
intern(cx, ty_int(ast::ty_i16));
intern(cx, ty_int(ast::ty_i32));
intern(cx, ty_int(ast::ty_i64));
intern(cx, ty_uint(ast::ty_u8));
intern(cx, ty_uint(ast::ty_u16));
intern(cx, ty_uint(ast::ty_u32));
intern(cx, ty_uint(ast::ty_u64));
intern(cx, ty_float(ast::ty_f32));
intern(cx, ty_float(ast::ty_f64));
intern(cx, ty_int(ast::ty_char));
intern(cx, ty_str);
intern(cx, ty_type);
intern(cx, ty_send_type);
intern(cx, ty_bot);
assert (vec::len(cx.ts.vect) == idx_first_others);
}
fn mk_rcache() -> creader_cache {
type val = {cnum: int, pos: uint, len: uint};
fn hash_cache_entry(k: val) -> uint {
ret (k.cnum as uint) + k.pos + k.len;
}
fn eq_cache_entries(a: val, b: val) -> bool {
ret a.cnum == b.cnum && a.pos == b.pos && a.len == b.len;
}
ret map::mk_hashmap(hash_cache_entry, eq_cache_entries);
}
fn new_ty_hash<V: copy>() -> map::hashmap<t, V> { map::new_uint_hash() }
fn mk_ctxt(s: session::session, dm: resolve::def_map, amap: ast_map::map,
freevars: freevars::freevar_map) -> ctxt {
let ntt: node_type_table =
@smallintmap::mk::<ty::ty_param_substs_opt_and_ty>();
fn eq_raw_ty(&&a: @raw_t, &&b: @raw_t) -> bool {
ret a.hash == b.hash && a.struct == b.struct;
}
let ts = @interner::mk::<@raw_t>(hash_raw_ty, eq_raw_ty);
let cx =
@{ts: ts,
sess: s,
def_map: dm,
node_types: ntt,
items: amap,
freevars: freevars,
tcache: new_def_hash(),
rcache: mk_rcache(),
short_names_cache: new_ty_hash(),
needs_drop_cache: new_ty_hash(),
kind_cache: new_ty_hash(),
ast_ty_to_ty_cache:
map::mk_hashmap(ast_util::hash_ty, ast_util::eq_ty),
tag_var_cache: new_def_hash(),
iface_method_cache: new_def_hash(),
ty_param_bounds: map::new_int_hash()};
populate_type_store(cx);
ret cx;
}
// Type constructors
fn mk_raw_ty(cx: ctxt, st: sty) -> @raw_t {
let h = hash_type_structure(st);
let has_params: bool = false;
let has_vars: bool = false;
fn derive_flags_t(cx: ctxt, &has_params: bool, &has_vars: bool, tt: t) {
let rt = interner::get::<@raw_t>(*cx.ts, tt);
has_params = has_params || rt.has_params;
has_vars = has_vars || rt.has_vars;
}
fn derive_flags_mt(cx: ctxt, &has_params: bool, &has_vars: bool, m: mt) {
derive_flags_t(cx, has_params, has_vars, m.ty);
}
fn derive_flags_arg(cx: ctxt, &has_params: bool, &has_vars: bool,
a: arg) {
derive_flags_t(cx, has_params, has_vars, a.ty);
}
fn derive_flags_sig(cx: ctxt, &has_params: bool, &has_vars: bool,
args: [arg], tt: t) {
for a: arg in args { derive_flags_arg(cx, has_params, has_vars, a); }
derive_flags_t(cx, has_params, has_vars, tt);
}
alt st {
ty_nil. | ty_bot. | ty_bool. | ty_int(_) | ty_float(_) | ty_uint(_) |
ty_str. | ty_send_type. | ty_type. | ty_native(_) |
ty_opaque_closure_ptr(_) {
/* no-op */
}
ty_param(_, _) { has_params = true; }
ty_var(_) { has_vars = true; }
ty_tag(_, tys) | ty_iface(_, tys) {
for tt: t in tys { derive_flags_t(cx, has_params, has_vars, tt); }
}
ty_box(m) { derive_flags_mt(cx, has_params, has_vars, m); }
ty_uniq(m) { derive_flags_mt(cx, has_params, has_vars, m); }
ty_vec(m) { derive_flags_mt(cx, has_params, has_vars, m); }
ty_ptr(m) { derive_flags_mt(cx, has_params, has_vars, m); }
ty_rec(flds) {
for f: field in flds {
derive_flags_mt(cx, has_params, has_vars, f.mt);
}
}
ty_tup(ts) {
for tt in ts { derive_flags_t(cx, has_params, has_vars, tt); }
}
ty_fn(f) {
derive_flags_sig(cx, has_params, has_vars, f.inputs, f.output);
}
ty_native_fn(args, tt) {
derive_flags_sig(cx, has_params, has_vars, args, tt);
}
ty_obj(meths) {
for m: method in meths {
derive_flags_sig(cx, has_params, has_vars, m.fty.inputs,
m.fty.output);
}
}
ty_res(_, tt, tps) {
derive_flags_t(cx, has_params, has_vars, tt);
for tt: t in tps { derive_flags_t(cx, has_params, has_vars, tt); }
}
ty_constr(tt, _) | ty_named(tt, _) {
derive_flags_t(cx, has_params, has_vars, tt);
}
}
ret @{struct: st,
hash: h,
has_params: has_params,
has_vars: has_vars};
}
fn intern(cx: ctxt, st: sty) {
interner::intern(*cx.ts, mk_raw_ty(cx, st));
}
// These are private constructors to this module. External users should always
// use the mk_foo() functions below.
fn gen_ty(cx: ctxt, st: sty) -> t {
let raw_type = mk_raw_ty(cx, st);
ret interner::intern(*cx.ts, raw_type);
}
fn mk_nil(_cx: ctxt) -> t { ret idx_nil; }
fn mk_bot(_cx: ctxt) -> t { ret idx_bot; }
fn mk_bool(_cx: ctxt) -> t { ret idx_bool; }
fn mk_int(_cx: ctxt) -> t { ret idx_int; }
fn mk_float(_cx: ctxt) -> t { ret idx_float; }
fn mk_uint(_cx: ctxt) -> t { ret idx_uint; }
fn mk_mach_int(_cx: ctxt, tm: ast::int_ty) -> t {
alt tm {
ast::ty_i. { ret idx_int; }
ast::ty_char. { ret idx_char; }
ast::ty_i8. { ret idx_i8; }
ast::ty_i16. { ret idx_i16; }
ast::ty_i32. { ret idx_i32; }
ast::ty_i64. { ret idx_i64; }
}
}
fn mk_mach_uint(_cx: ctxt, tm: ast::uint_ty) -> t {
alt tm {
ast::ty_u. { ret idx_uint; }
ast::ty_u8. { ret idx_u8; }
ast::ty_u16. { ret idx_u16; }
ast::ty_u32. { ret idx_u32; }
ast::ty_u64. { ret idx_u64; }
}
}
fn mk_mach_float(_cx: ctxt, tm: ast::float_ty) -> t {
alt tm {
ast::ty_f. { ret idx_float; }
ast::ty_f32. { ret idx_f32; }
ast::ty_f64. { ret idx_f64; }
}
}
fn mk_char(_cx: ctxt) -> t { ret idx_char; }
fn mk_str(_cx: ctxt) -> t { ret idx_str; }
fn mk_tag(cx: ctxt, did: ast::def_id, tys: [t]) -> t {
ret gen_ty(cx, ty_tag(did, tys));
}
fn mk_box(cx: ctxt, tm: mt) -> t { ret gen_ty(cx, ty_box(tm)); }
fn mk_uniq(cx: ctxt, tm: mt) -> t { ret gen_ty(cx, ty_uniq(tm)); }
fn mk_imm_uniq(cx: ctxt, ty: t) -> t {
ret mk_uniq(cx, {ty: ty, mut: ast::imm});
}
fn mk_ptr(cx: ctxt, tm: mt) -> t { ret gen_ty(cx, ty_ptr(tm)); }
fn mk_imm_box(cx: ctxt, ty: t) -> t {
ret mk_box(cx, {ty: ty, mut: ast::imm});
}
fn mk_mut_ptr(cx: ctxt, ty: t) -> t {
ret mk_ptr(cx, {ty: ty, mut: ast::mut});
}
fn mk_vec(cx: ctxt, tm: mt) -> t { ret gen_ty(cx, ty_vec(tm)); }
fn mk_rec(cx: ctxt, fs: [field]) -> t { ret gen_ty(cx, ty_rec(fs)); }
fn mk_constr(cx: ctxt, t: t, cs: [@type_constr]) -> t {
ret gen_ty(cx, ty_constr(t, cs));
}
fn mk_tup(cx: ctxt, ts: [t]) -> t { ret gen_ty(cx, ty_tup(ts)); }
fn mk_fn(cx: ctxt, fty: fn_ty) -> t {
ret gen_ty(cx, ty_fn(fty));
}
fn mk_native_fn(cx: ctxt, args: [arg], ty: t) -> t {
ret gen_ty(cx, ty_native_fn(args, ty));
}
fn mk_obj(cx: ctxt, meths: [method]) -> t { ret gen_ty(cx, ty_obj(meths)); }
fn mk_iface(cx: ctxt, did: ast::def_id, tys: [t]) -> t {
ret gen_ty(cx, ty_iface(did, tys));
}
fn mk_res(cx: ctxt, did: ast::def_id, inner: t, tps: [t]) -> t {
ret gen_ty(cx, ty_res(did, inner, tps));
}
fn mk_var(cx: ctxt, v: int) -> t { ret gen_ty(cx, ty_var(v)); }
fn mk_param(cx: ctxt, n: uint, k: def_id) -> t {
ret gen_ty(cx, ty_param(n, k));
}
fn mk_type(_cx: ctxt) -> t { ret idx_type; }
fn mk_send_type(_cx: ctxt) -> t { ret idx_send_type; }
fn mk_native(cx: ctxt, did: def_id) -> t { ret gen_ty(cx, ty_native(did)); }
fn mk_opaque_closure_ptr(cx: ctxt, ck: closure_kind) -> t {
ret gen_ty(cx, ty_opaque_closure_ptr(ck));
}
fn mk_named(cx: ctxt, base: t, name: @str) -> t {
gen_ty(cx, ty_named(base, name))
}
// Returns the one-level-deep type structure of the given type.
pure fn struct(cx: ctxt, typ: t) -> sty {
alt interner::get(*cx.ts, typ).struct {
ty_named(t, _) { struct(cx, t) }
s { s }
}
}
// Returns struact(cx, typ) but replaces all occurences of platform
// dependent primitive types with their machine type equivalent
pure fn mach_struct(cx: ctxt, cfg: @session::config, typ: t) -> sty {
alt interner::get(*cx.ts, typ).struct {
ty_named(t, _) { mach_struct(cx, cfg, t) }
s { mach_sty(cfg, s) }
}
}
// Converts s to its machine type equivalent
pure fn mach_sty(cfg: @session::config, s: sty) -> sty {
alt s {
ty_int(ast::ty_i.) { ty_int(cfg.int_type) }
ty_uint(ast::ty_u.) { ty_uint(cfg.uint_type) }
ty_float(ast::ty_f.) { ty_float(cfg.float_type) }
s { s }
}
}
pure fn ty_name(cx: ctxt, typ: t) -> option::t<@str> {
alt interner::get(*cx.ts, typ).struct {
ty_named(_, n) { some(n) }
_ { none }
}
}
// Type folds
type ty_walk = fn@(t);
fn walk_ty(cx: ctxt, walker: ty_walk, ty: t) {
alt struct(cx, ty) {
ty_nil. | ty_bot. | ty_bool. | ty_int(_) | ty_uint(_) | ty_float(_) |
ty_str. | ty_send_type. | ty_type. | ty_native(_) |
ty_opaque_closure_ptr(_) {
/* no-op */
}
ty_box(tm) | ty_vec(tm) | ty_ptr(tm) { walk_ty(cx, walker, tm.ty); }
ty_tag(_, subtys) | ty_iface(_, subtys) {
for subty: t in subtys { walk_ty(cx, walker, subty); }
}
ty_rec(fields) {
for fl: field in fields { walk_ty(cx, walker, fl.mt.ty); }
}
ty_tup(ts) { for tt in ts { walk_ty(cx, walker, tt); } }
ty_fn(f) {
for a: arg in f.inputs { walk_ty(cx, walker, a.ty); }
walk_ty(cx, walker, f.output);
}
ty_native_fn(args, ret_ty) {
for a: arg in args { walk_ty(cx, walker, a.ty); }
walk_ty(cx, walker, ret_ty);
}
ty_obj(methods) {
for m: method in methods {
for a: arg in m.fty.inputs { walk_ty(cx, walker, a.ty); }
walk_ty(cx, walker, m.fty.output);
}
}
ty_res(_, sub, tps) {
walk_ty(cx, walker, sub);
for tp: t in tps { walk_ty(cx, walker, tp); }
}
ty_constr(sub, _) { walk_ty(cx, walker, sub); }
ty_var(_) {/* no-op */ }
ty_param(_, _) {/* no-op */ }
ty_uniq(tm) { walk_ty(cx, walker, tm.ty); }
}
walker(ty);
}
tag fold_mode {
fm_var(fn@(int) -> t);
fm_param(fn@(uint, def_id) -> t);
fm_general(fn@(t) -> t);
}
fn fold_ty(cx: ctxt, fld: fold_mode, ty_0: t) -> t {
let ty = ty_0;
// Fast paths.
alt fld {
fm_var(_) { if !type_contains_vars(cx, ty) { ret ty; } }
fm_param(_) { if !type_contains_params(cx, ty) { ret ty; } }
fm_general(_) {/* no fast path */ }
}
alt interner::get(*cx.ts, ty).struct {
ty_nil. | ty_bot. | ty_bool. | ty_int(_) | ty_uint(_) | ty_float(_) |
ty_str. | ty_send_type. | ty_type. | ty_native(_) |
ty_opaque_closure_ptr(_) {
/* no-op */
}
ty_box(tm) {
ty = mk_box(cx, {ty: fold_ty(cx, fld, tm.ty), mut: tm.mut});
}
ty_uniq(tm) {
ty = mk_uniq(cx, {ty: fold_ty(cx, fld, tm.ty), mut: tm.mut});
}
ty_named(t, nm) {
ty = mk_named(cx, fold_ty(cx, fld, t), nm);
}
ty_ptr(tm) {
ty = mk_ptr(cx, {ty: fold_ty(cx, fld, tm.ty), mut: tm.mut});
}
ty_vec(tm) {
ty = mk_vec(cx, {ty: fold_ty(cx, fld, tm.ty), mut: tm.mut});
}
ty_tag(tid, subtys) {
ty = mk_tag(cx, tid, vec::map(subtys, {|t| fold_ty(cx, fld, t) }));
}
ty_iface(did, subtys) {
ty = mk_iface(cx, did, vec::map(subtys, {|t| fold_ty(cx, fld, t) }));
}
ty_rec(fields) {
let new_fields: [field] = [];
for fl: field in fields {
let new_ty = fold_ty(cx, fld, fl.mt.ty);
let new_mt = {ty: new_ty, mut: fl.mt.mut};
new_fields += [{ident: fl.ident, mt: new_mt}];
}
ty = mk_rec(cx, new_fields);
}
ty_tup(ts) {
let new_ts = [];
for tt in ts { new_ts += [fold_ty(cx, fld, tt)]; }
ty = mk_tup(cx, new_ts);
}
ty_fn(f) {
let new_args: [arg] = [];
for a: arg in f.inputs {
let new_ty = fold_ty(cx, fld, a.ty);
new_args += [{mode: a.mode, ty: new_ty}];
}
ty = mk_fn(cx, {inputs: new_args,
output: fold_ty(cx, fld, f.output)
with f});
}
ty_native_fn(args, ret_ty) {
let new_args: [arg] = [];
for a: arg in args {
let new_ty = fold_ty(cx, fld, a.ty);
new_args += [{mode: a.mode, ty: new_ty}];
}
ty = mk_native_fn(cx, new_args, fold_ty(cx, fld, ret_ty));
}
ty_obj(methods) {
let new_methods = vec::map(methods, {|m|
let new_args = vec::map(m.fty.inputs, {|a|
{mode: a.mode, ty: fold_ty(cx, fld, a.ty)}
});
{ident: m.ident, tps: m.tps,
fty: {inputs: new_args,
output: fold_ty(cx, fld, m.fty.output)
with m.fty}}
});
ty = mk_obj(cx, new_methods);
}
ty_res(did, subty, tps) {
let new_tps = [];
for tp: t in tps { new_tps += [fold_ty(cx, fld, tp)]; }
ty = mk_res(cx, did, fold_ty(cx, fld, subty), new_tps);
}
ty_var(id) {
alt fld { fm_var(folder) { ty = folder(id); } _ {/* no-op */ } }
}
ty_param(id, did) {
alt fld { fm_param(folder) { ty = folder(id, did); } _ {} }
}
ty_constr(subty, cs) {
ty = mk_constr(cx, fold_ty(cx, fld, subty), cs);
}
_ {
cx.sess.fatal("Unsupported sort of type in fold_ty");
}
}
// If this is a general type fold, then we need to run it now.
alt fld { fm_general(folder) { ret folder(ty); } _ { ret ty; } }
}
// Type utilities
fn type_is_nil(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) { ty_nil. { ret true; } _ { ret false; } }
}
fn type_is_bot(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) { ty_bot. { ret true; } _ { ret false; } }
}
fn type_is_bool(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) { ty_bool. { ret true; } _ { ret false; } }
}
fn type_is_structural(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_rec(_) | ty_tup(_) | ty_tag(_, _) | ty_fn(_) |
ty_native_fn(_, _) | ty_obj(_) | ty_res(_, _, _) { true }
_ { false }
}
}
fn type_is_copyable(cx: ctxt, ty: t) -> bool {
ret kind_can_be_copied(type_kind(cx, ty));
}
fn type_is_sequence(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_str. { ret true; }
ty_vec(_) { ret true; }
_ { ret false; }
}
}
fn type_is_str(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) { ty_str. { ret true; } _ { ret false; } }
}
fn sequence_element_type(cx: ctxt, ty: t) -> t {
alt struct(cx, ty) {
ty_str. { ret mk_mach_uint(cx, ast::ty_u8); }
ty_vec(mt) { ret mt.ty; }
_ { cx.sess.bug("sequence_element_type called on non-sequence value"); }
}
}
pure fn type_is_tup_like(cx: ctxt, ty: t) -> bool {
let sty = struct(cx, ty);
alt sty {
ty_ptr(_) | ty_uniq(_) |
ty_box(_) | ty_rec(_) | ty_tup(_) | ty_tag(_,_) { true }
_ { false }
}
}
fn get_element_type(cx: ctxt, ty: t, i: uint) -> t {
alt struct(cx, ty) {
ty_rec(flds) { ret flds[i].mt.ty; }
ty_tup(ts) { ret ts[i]; }
_ {
cx.sess.bug("get_element_type called on type " + ty_to_str(cx, ty) +
" - expected a \
tuple or record");
}
}
// NB: This is not exhaustive -- struct(cx, ty) could be a box or a
// tag.
}
pure fn type_is_box(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_box(_) { ret true; }
_ { ret false; }
}
}
pure fn type_is_boxed(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_box(_) | ty_iface(_, _) { ret true; }
_ { ret false; }
}
}
pure fn type_is_unique_box(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_uniq(_) { ret true; }
_ { ret false; }
}
}
pure fn type_is_unsafe_ptr(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_ptr(_) { ret true; }
_ { ret false; }
}
}
pure fn type_is_vec(cx: ctxt, ty: t) -> bool {
ret alt struct(cx, ty) {
ty_vec(_) { true }
ty_str. { true }
_ { false }
};
}
pure fn type_is_unique(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_uniq(_) { ret true; }
ty_vec(_) { true }
ty_str. { true }
_ { ret false; }
}
}
pure fn type_is_scalar(cx: ctxt, ty: t) -> bool {
alt struct(cx, ty) {
ty_nil. | ty_bool. | ty_int(_) | ty_float(_) | ty_uint(_) |
ty_send_type. | ty_type. | ty_native(_) | ty_ptr(_) { true }
_ { false }
}
}
// FIXME maybe inline this for speed?
fn type_is_immediate(cx: ctxt, ty: t) -> bool {
ret type_is_scalar(cx, ty) || type_is_boxed(cx, ty) ||
type_is_unique(cx, ty) || type_is_native(cx, ty);
}
fn type_needs_drop(cx: ctxt, ty: t) -> bool {
alt cx.needs_drop_cache.find(ty) {
some(result) { ret result; }
none. {/* fall through */ }
}
let accum = false;
let result = alt struct(cx, ty) {
// scalar types
ty_nil. | ty_bot. | ty_bool. | ty_int(_) | ty_float(_) | ty_uint(_) |
ty_type. | ty_native(_) | ty_ptr(_) { false }
ty_rec(flds) {
for f in flds { if type_needs_drop(cx, f.mt.ty) { accum = true; } }
accum
}
ty_tup(elts) {
for m in elts { if type_needs_drop(cx, m) { accum = true; } }
accum
}
ty_tag(did, tps) {
let variants = tag_variants(cx, did);
for variant in *variants {
for aty in variant.args {
// Perform any type parameter substitutions.
let arg_ty = substitute_type_params(cx, tps, aty);
if type_needs_drop(cx, arg_ty) { accum = true; }
}
if accum { break; }
}
accum
}
_ { true }
};
cx.needs_drop_cache.insert(ty, result);
ret result;
}