forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ty.rs
3437 lines (3040 loc) · 105 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
// #[warn(deprecated_mode)];
#[warn(deprecated_pattern)];
import std::{map, smallintmap};
import result::result;
import std::map::hashmap;
import driver::session;
import session::session;
import syntax::{ast, ast_map};
import syntax::ast_util;
import syntax::ast_util::{is_local, local_def, new_def_hash};
import syntax::codemap::span;
import metadata::csearch;
import util::ppaux::{region_to_str, explain_region, vstore_to_str};
import middle::lint;
import middle::lint::{get_lint_level, allow};
import syntax::ast::*;
import syntax::print::pprust::*;
import util::ppaux::{ty_to_str, proto_ty_to_str, tys_to_str};
import std::serialization::{serialize_option,
deserialize_option};
export tv_vid, tvi_vid, region_vid, vid;
export br_hashmap;
export is_instantiable;
export node_id_to_type;
export node_id_to_type_params;
export arg;
export args_eq;
export block_ty;
export class_items_as_fields, class_items_as_mutable_fields;
export ctxt;
export deref, deref_sty;
export index, index_sty;
export def_has_ty_params;
export expr_has_ty_params;
export expr_ty;
export expr_ty_params_and_ty;
export expr_is_lval;
export field_ty;
export fold_ty, fold_sty_to_ty, fold_region, fold_regions;
export fold_regions_and_ty, walk_regions_and_ty;
export field;
export field_idx;
export get_field;
export get_fields;
export get_element_type;
export has_dtor;
export is_binopable;
export is_pred_ty;
export lookup_class_field, lookup_class_fields;
export lookup_class_method_by_name;
export lookup_field_type;
export lookup_item_type;
export lookup_public_fields;
export method;
export method_idx;
export mk_class;
export mk_ctxt;
export mk_with_id, type_def_id;
export mt;
export node_type_table;
export pat_ty;
export sequence_element_type;
export stmt_node_id;
export sty;
export subst, subst_tps, substs_is_noop, substs_to_str, substs;
export t;
export new_ty_hash;
export enum_variants, substd_enum_variants, enum_is_univariant;
export trait_methods, store_trait_methods, impl_traits;
export enum_variant_with_id;
export ty_dtor;
export ty_param_bounds_and_ty;
export ty_bool, mk_bool, type_is_bool;
export ty_bot, mk_bot, type_is_bot;
export ty_box, mk_box, mk_imm_box, type_is_box, type_is_boxed;
export ty_opaque_closure_ptr, mk_opaque_closure_ptr;
export ty_opaque_box, mk_opaque_box;
export ty_float, mk_float, mk_mach_float, type_is_fp;
export ty_fn, fn_ty, mk_fn;
export ty_fn_proto, ty_fn_purity, ty_fn_ret, ty_fn_ret_style, tys_in_fn_ty;
export ty_int, mk_int, mk_mach_int, mk_char;
export mk_i8, mk_u8, mk_i16, mk_u16, mk_i32, mk_u32, mk_i64, mk_u64;
export ty_estr, mk_estr, type_is_str;
export ty_evec, mk_evec, type_is_vec;
export ty_unboxed_vec, mk_unboxed_vec, mk_mut_unboxed_vec;
export vstore, vstore_fixed, vstore_uniq, vstore_box, vstore_slice;
export ty_nil, mk_nil, type_is_nil;
export ty_trait, mk_trait;
export ty_param, mk_param, ty_params_to_tys;
export ty_ptr, mk_ptr, mk_mut_ptr, mk_imm_ptr, mk_nil_ptr, type_is_unsafe_ptr;
export ty_rptr, mk_rptr, mk_mut_rptr, mk_imm_rptr;
export ty_rec, mk_rec;
export ty_enum, mk_enum, type_is_enum;
export ty_tup, mk_tup;
export ty_type, mk_type;
export ty_uint, mk_uint, mk_mach_uint;
export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box;
export ty_var, mk_var, type_is_var;
export ty_var_integral, mk_var_integral, type_is_var_integral;
export ty_self, mk_self, type_has_self;
export ty_class;
export region, bound_region, encl_region;
export re_bound, re_free, re_scope, re_static, re_var;
export br_self, br_anon, br_named, br_cap_avoid;
export get, type_has_params, type_needs_infer, type_has_regions;
export type_is_region_ptr;
export type_id;
export tbox_has_flag;
export ty_var_id;
export ty_to_def_id;
export ty_fn_args;
export ty_region;
export kind, kind_implicitly_copyable, kind_send_copy, kind_copyable;
export kind_noncopyable, kind_const;
export kind_can_be_copied, kind_can_be_sent, kind_can_be_implicitly_copied;
export kind_is_safe_for_default_mode;
export kind_is_owned;
export proto_kind, kind_lteq, type_kind;
export operators;
export type_err, terr_vstore_kind;
export type_err_to_str;
export expected_found;
export type_needs_drop;
export type_is_empty;
export type_is_integral;
export type_is_numeric;
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_slice;
export type_is_unique;
export type_is_c_like_enum;
export type_structurally_contains;
export type_structurally_contains_uniques;
export type_autoderef, deref, deref_sty;
export type_param;
export type_needs_unwind_cleanup;
export canon_mode;
export resolved_mode;
export arg_mode;
export unify_mode;
export set_default_mode;
export variant_info;
export walk_ty, maybe_walk_ty;
export occurs_check;
export closure_kind;
export ck_block;
export ck_box;
export ck_uniq;
export param_bound, param_bounds, bound_copy, bound_owned;
export bound_send, bound_trait;
export param_bounds_to_kind;
export default_arg_mode_for_ty;
export item_path;
export item_path_str;
export ast_ty_to_ty_cache_entry;
export atttce_unresolved, atttce_resolved;
export mach_sty;
export ty_sort_str;
export normalize_ty;
export to_str;
export borrow, serialize_borrow, deserialize_borrow;
export bound_const;
export terr_no_integral_type, terr_ty_param_size, terr_self_substs;
export terr_in_field, terr_record_fields, terr_vstores_differ, terr_arg_count;
export terr_sorts, terr_vec, terr_str, terr_record_size, terr_tuple_size;
export terr_regions_does_not_outlive, terr_mutability, terr_purity_mismatch;
export terr_regions_not_same, terr_regions_no_overlap;
export terr_proto_mismatch;
export terr_ret_style_mismatch;
export terr_fn, terr_trait;
export purity_to_str;
export param_tys_in_type;
export eval_repeat_count;
export fn_proto, proto_bare, proto_vstore;
export ast_proto_to_proto;
export is_blockish;
export method_call_bounds;
export hash_region;
export region_variance, rv_covariant, rv_invariant, rv_contravariant;
export serialize_region_variance, deserialize_region_variance;
export opt_region_variance;
export serialize_opt_region_variance, deserialize_opt_region_variance;
export determine_inherited_purity;
// Data types
// Note: after typeck, you should use resolved_mode() to convert this mode
// into an rmode, which will take into account the results of mode inference.
type arg = {mode: ast::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,
self_ty: ast::self_ty_,
vis: ast::visibility};
type mt = {ty: t, mutbl: ast::mutability};
enum vstore {
vstore_fixed(uint),
vstore_uniq,
vstore_box,
vstore_slice(region)
}
type field_ty = {
ident: ident,
id: def_id,
vis: ast::visibility,
mutability: ast::class_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}, t>;
type intern_key = {struct: sty, o_def_id: option<ast::def_id>};
enum ast_ty_to_ty_cache_entry {
atttce_unresolved, /* not resolved yet */
atttce_resolved(t) /* resolved to a type, irrespective of region */
}
#[auto_serialize]
type opt_region_variance = option<region_variance>;
#[auto_serialize]
enum region_variance { rv_covariant, rv_invariant, rv_contravariant }
// N.B.: Borrows from inlined content are not accurately deserialized. This
// is because we don't need the details in trans, we only care if there is an
// entry in the table or not.
type borrow = {
region: ty::region,
mutbl: ast::mutability
};
type ctxt =
@{diag: syntax::diagnostic::span_handler,
interner: hashmap<intern_key, t_box>,
mut next_id: uint,
vecs_implicitly_copyable: bool,
cstore: metadata::cstore::cstore,
sess: session::session,
def_map: resolve3::DefMap,
region_map: middle::region::region_map,
region_paramd_items: middle::region::region_paramd_items,
// Stores the types for various nodes in the AST. Note that this table
// is not guaranteed to be populated until after typeck. See
// typeck::check::fn_ctxt for details.
node_types: node_type_table,
// Stores the type parameters which were substituted to obtain the type
// of this node. This only applies to nodes that refer to entities
// parameterized by type parameters, such as generic fns, types, or
// other items.
node_type_substs: hashmap<node_id, ~[t]>,
items: ast_map::map,
intrinsic_defs: hashmap<ast::ident, (ast::def_id, t)>,
freevars: freevars::freevar_map,
tcache: type_cache,
rcache: creader_cache,
ccache: constness_cache,
short_names_cache: hashmap<t, @~str>,
needs_drop_cache: hashmap<t, bool>,
needs_unwind_cleanup_cache: hashmap<t, bool>,
kind_cache: hashmap<t, kind>,
ast_ty_to_ty_cache: hashmap<@ast::ty, ast_ty_to_ty_cache_entry>,
enum_var_cache: hashmap<def_id, @~[variant_info]>,
trait_method_cache: hashmap<def_id, @~[method]>,
ty_param_bounds: hashmap<ast::node_id, param_bounds>,
inferred_modes: hashmap<ast::node_id, ast::mode>,
// maps the id of borrowed expr to scope of borrowed ptr
borrowings: hashmap<ast::node_id, borrow>,
normalized_cache: hashmap<t, t>};
enum tbox_flag {
has_params = 1,
has_self = 2,
needs_infer = 4,
has_regions = 8,
// a meta-flag: subst may be required if the type has parameters, a self
// type, or references bound regions
needs_subst = 1 | 2 | 8
}
type t_box = @{struct: sty,
id: uint,
flags: uint,
o_def_id: option<ast::def_id>};
// To reduce refcounting cost, we're representing types as unsafe pointers
// throughout the compiler. These are simply casted t_box values. Use ty::get
// to cast them back to a box. (Without the cast, compiler performance suffers
// ~15%.) This does mean that a t value relies on the ctxt to keep its box
// alive, and using ty::get is unsafe when the ctxt is no longer alive.
enum t_opaque {}
type t = *t_opaque;
pure fn get(t: t) -> t_box unsafe {
let t2 = unsafe::reinterpret_cast::<t, t_box>(t);
let t3 = t2;
unsafe::forget(t2);
t3
}
pure fn tbox_has_flag(tb: t_box, flag: tbox_flag) -> bool {
(tb.flags & (flag as uint)) != 0u
}
pure fn type_has_params(t: t) -> bool { tbox_has_flag(get(t), has_params) }
pure fn type_has_self(t: t) -> bool { tbox_has_flag(get(t), has_self) }
pure fn type_needs_infer(t: t) -> bool { tbox_has_flag(get(t), needs_infer) }
pure fn type_has_regions(t: t) -> bool { tbox_has_flag(get(t), has_regions) }
pure fn type_def_id(t: t) -> option<ast::def_id> { get(t).o_def_id }
pure fn type_id(t: t) -> uint { get(t).id }
enum closure_kind {
ck_block,
ck_box,
ck_uniq,
}
enum fn_proto {
proto_bare, // supertype of all other protocols
proto_vstore(vstore)
}
/// Innards of a function type:
///
/// - `purity` is the function's effect (pure, impure, unsafe).
/// - `proto` is the protocol (fn@, fn~, etc).
/// - `bound` is the parameter bounds on the function's upvars.
/// - `inputs` is the list of arguments and their modes.
/// - `output` is the return type.
/// - `ret_style` indicates whether the function returns a value or fails.
type fn_ty = {purity: ast::purity,
proto: fn_proto,
bounds: @~[param_bound],
inputs: ~[arg],
output: t,
ret_style: ret_style};
type param_ty = {idx: uint, def_id: def_id};
/// Representation of regions:
enum region {
/// Bound regions are found (primarily) in function types. They indicate
/// region parameters that have yet to be replaced with actual regions
/// (analogous to type parameters, except that due to the monomorphic
/// nature of our type system, bound type parameters are always replaced
/// with fresh type variables whenever an item is referenced, so type
/// parameters only appear "free" in types. Regions in contrast can
/// appear free or bound.). When a function is called, all bound regions
/// tied to that function's node-id are replaced with fresh region
/// variables whose value is then inferred.
re_bound(bound_region),
/// When checking a function body, the types of all arguments and so forth
/// that refer to bound region parameters are modified to refer to free
/// region parameters.
re_free(node_id, bound_region),
/// A concrete region naming some expression within the current function.
re_scope(node_id),
/// Static data that has an "infinite" lifetime.
re_static,
/// A region variable. Should not exist after typeck.
re_var(region_vid)
}
enum bound_region {
/// The self region for classes, impls (&T in a type defn or &self/T)
br_self,
/// An anonymous region parameter for a given fn (&T)
br_anon(uint),
/// Named region parameters for functions (a in &a/T)
br_named(ast::ident),
/// Handles capture-avoiding substitution in a rather subtle case. If you
/// have a closure whose argument types are being inferred based on the
/// expected type, and the expected type includes bound regions, then we
/// will wrap those bound regions in a br_cap_avoid() with the id of the
/// fn expression. This ensures that the names are not "captured" by the
/// enclosing scope, which may define the same names. For an example of
/// where this comes up, see src/test/compile-fail/regions-ret-borrowed.rs
/// and regions-ret-borrowed-1.rs.
br_cap_avoid(ast::node_id, @bound_region),
}
type opt_region = option<region>;
/// The type substs represents the kinds of things that can be substituted to
/// convert a polytype into a monotype. Note however that substituting bound
/// regions other than `self` is done through a different mechanism.
///
/// `tps` represents the type parameters in scope. They are indexed according
/// to the order in which they were declared.
///
/// `self_r` indicates the region parameter `self` that is present on nominal
/// types (enums, classes) declared as having a region parameter. `self_r`
/// should always be none for types that are not region-parameterized and
/// some(_) for types that are. The only bound region parameter that should
/// appear within a region-parameterized type is `self`.
///
/// `self_ty` is the type to which `self` should be remapped, if any. The
/// `self` type is rather funny in that it can only appear on traits and
/// is always substituted away to the implementing type for a trait.
type substs = {
self_r: opt_region,
self_ty: option<ty::t>,
tps: ~[t]
};
// NB: If you change this, you'll probably want to change the corresponding
// AST structure in libsyntax/ast.rs as well.
enum sty {
ty_nil,
ty_bot,
ty_bool,
ty_int(ast::int_ty),
ty_uint(ast::uint_ty),
ty_float(ast::float_ty),
ty_estr(vstore),
ty_enum(def_id, substs),
ty_box(mt),
ty_uniq(mt),
ty_evec(mt, vstore),
ty_ptr(mt),
ty_rptr(region, mt),
ty_rec(~[field]),
ty_fn(fn_ty),
ty_trait(def_id, substs, vstore),
ty_class(def_id, substs),
ty_tup(~[t]),
ty_var(tv_vid), // type variable during typechecking
ty_var_integral(tvi_vid), // type variable during typechecking, for
// integral types only
ty_param(param_ty), // type parameter
ty_self, // special, implicit `self` type parameter
// "Fake" types, used for trans purposes
ty_type, // type_desc*
ty_opaque_box, // used by monomorphizer to represent any @ box
ty_opaque_closure_ptr(closure_kind), // ptr to env for fn, fn@, fn~
ty_unboxed_vec(mt),
}
enum terr_vstore_kind {
terr_vec, terr_str, terr_fn, terr_trait
}
struct expected_found<T> {
expected: T;
found: T;
}
// Data structures used in type unification
enum type_err {
terr_mismatch,
terr_ret_style_mismatch(expected_found<ast::ret_style>),
terr_purity_mismatch(expected_found<purity>),
terr_mutability,
terr_proto_mismatch(expected_found<ty::fn_proto>),
terr_box_mutability,
terr_ptr_mutability,
terr_ref_mutability,
terr_vec_mutability,
terr_tuple_size(expected_found<uint>),
terr_ty_param_size(expected_found<uint>),
terr_record_size(expected_found<uint>),
terr_record_mutability,
terr_record_fields(expected_found<ident>),
terr_arg_count,
terr_mode_mismatch(expected_found<mode>),
terr_regions_does_not_outlive(region, region),
terr_regions_not_same(region, region),
terr_regions_no_overlap(region, region),
terr_vstores_differ(terr_vstore_kind, expected_found<vstore>),
terr_in_field(@type_err, ast::ident),
terr_sorts(expected_found<t>),
terr_self_substs,
terr_no_integral_type,
}
enum param_bound {
bound_copy,
bound_owned,
bound_send,
bound_const,
bound_trait(t),
}
enum tv_vid = uint;
enum tvi_vid = uint;
enum region_vid = uint;
trait vid {
pure fn to_uint() -> uint;
pure fn to_str() -> ~str;
}
impl tv_vid: vid {
pure fn to_uint() -> uint { *self }
pure fn to_str() -> ~str { fmt!("<V%u>", self.to_uint()) }
}
impl tvi_vid: vid {
pure fn to_uint() -> uint { *self }
pure fn to_str() -> ~str { fmt!("<VI%u>", self.to_uint()) }
}
impl region_vid: vid {
pure fn to_uint() -> uint { *self }
pure fn to_str() -> ~str { fmt!("%?", self) }
}
trait purity_to_str {
pure fn to_str() -> ~str;
}
impl purity: purity_to_str {
pure fn to_str() -> ~str {
purity_to_str(self)
}
}
fn param_bounds_to_kind(bounds: param_bounds) -> kind {
let mut kind = kind_noncopyable();
for vec::each(*bounds) |bound| {
match bound {
bound_copy => {
kind = raise_kind(kind, kind_implicitly_copyable());
}
bound_owned => {
kind = raise_kind(kind, kind_owned());
}
bound_send => {
kind = raise_kind(kind, kind_send_only() | kind_owned());
}
bound_const => {
kind = raise_kind(kind, kind_const());
}
bound_trait(_) => ()
}
}
kind
}
/// A polytype.
///
/// - `bounds`: The list of bounds for each type parameter. The length of the
/// list also tells you how many type parameters there are.
///
/// - `rp`: true if the type is region-parameterized. Types can have at
/// most one region parameter, always called `&self`.
///
/// - `ty`: the base type. May have reference to the (unsubstituted) bound
/// region `&self` or to (unsubstituted) ty_param types
type ty_param_bounds_and_ty = {bounds: @~[param_bounds],
region_param: option<region_variance>,
ty: t};
type type_cache = hashmap<ast::def_id, ty_param_bounds_and_ty>;
type constness_cache = hashmap<ast::def_id, const_eval::constness>;
type node_type_table = @smallintmap::smallintmap<t>;
fn mk_rcache() -> creader_cache {
type val = {cnum: int, pos: uint, len: uint};
pure fn hash_cache_entry(k: &val) -> uint {
(k.cnum as uint) + k.pos + k.len
}
pure fn eq_cache_entries(a: &val, b: &val) -> bool {
a.cnum == b.cnum && a.pos == b.pos && a.len == b.len
}
return map::hashmap(hash_cache_entry, eq_cache_entries);
}
fn new_ty_hash<V: copy>() -> map::hashmap<t, V> {
map::hashmap(|t: &t| type_id(*t),
|a: &t, b: &t| type_id(*a) == type_id(*b))
}
fn mk_ctxt(s: session::session,
dm: resolve3::DefMap,
amap: ast_map::map,
freevars: freevars::freevar_map,
region_map: middle::region::region_map,
region_paramd_items: middle::region::region_paramd_items) -> ctxt {
pure fn hash_intern_key(k: &intern_key) -> uint {
hash_type_structure(&k.struct) +
option::map_default(k.o_def_id, 0u, |d| ast_util::hash_def(&d))
}
let interner = map::hashmap(hash_intern_key, sys::shape_eq);
let vecs_implicitly_copyable =
get_lint_level(s.lint_settings.default_settings,
lint::vecs_implicitly_copyable) == allow;
@{diag: s.diagnostic(),
interner: interner,
mut next_id: 0u,
vecs_implicitly_copyable: vecs_implicitly_copyable,
cstore: s.cstore,
sess: s,
def_map: dm,
region_map: region_map,
region_paramd_items: region_paramd_items,
node_types: @smallintmap::mk(),
node_type_substs: map::int_hash(),
items: amap,
intrinsic_defs: map::uint_hash(),
freevars: freevars,
tcache: ast_util::new_def_hash(),
rcache: mk_rcache(),
ccache: ast_util::new_def_hash(),
short_names_cache: new_ty_hash(),
needs_drop_cache: new_ty_hash(),
needs_unwind_cleanup_cache: new_ty_hash(),
kind_cache: new_ty_hash(),
ast_ty_to_ty_cache: map::hashmap(
ast_util::hash_ty, ast_util::eq_ty),
enum_var_cache: new_def_hash(),
trait_method_cache: new_def_hash(),
ty_param_bounds: map::int_hash(),
inferred_modes: map::int_hash(),
borrowings: map::int_hash(),
normalized_cache: new_ty_hash()}
}
// Type constructors
fn mk_t(cx: ctxt, +st: sty) -> t { mk_t_with_id(cx, st, none) }
// Interns a type/name combination, stores the resulting box in cx.interner,
// and returns the box as cast to an unsafe ptr (see comments for t above).
fn mk_t_with_id(cx: ctxt, +st: sty, o_def_id: option<ast::def_id>) -> t {
let key = {struct: st, o_def_id: o_def_id};
match cx.interner.find(key) {
some(t) => unsafe { return unsafe::reinterpret_cast(t); },
_ => ()
}
let mut flags = 0u;
fn rflags(r: region) -> uint {
(has_regions as uint) | {
match r {
ty::re_var(_) => needs_infer as uint,
_ => 0u
}
}
}
fn sflags(substs: &substs) -> uint {
let mut f = 0u;
for substs.tps.each |tt| { f |= get(tt).flags; }
substs.self_r.iter(|r| f |= rflags(r));
return f;
}
match st {
ty_estr(vstore_slice(r)) => {
flags |= rflags(r);
}
ty_evec(mt, vstore_slice(r)) => {
flags |= rflags(r);
flags |= get(mt.ty).flags;
}
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
ty_estr(_) | ty_type | ty_opaque_closure_ptr(_) |
ty_opaque_box => (),
ty_param(_) => flags |= has_params as uint,
ty_var(_) | ty_var_integral(_) => flags |= needs_infer as uint,
ty_self => flags |= has_self as uint,
ty_enum(_, ref substs) | ty_class(_, ref substs)
| ty_trait(_, ref substs, _) => {
flags |= sflags(substs);
}
ty_box(m) | ty_uniq(m) | ty_evec(m, _) |
ty_ptr(m) | ty_unboxed_vec(m) => {
flags |= get(m.ty).flags;
}
ty_rptr(r, m) => {
flags |= rflags(r);
flags |= get(m.ty).flags;
}
ty_rec(flds) => for flds.each |f| { flags |= get(f.mt.ty).flags; },
ty_tup(ts) => for ts.each |tt| { flags |= get(tt).flags; },
ty_fn(ref f) => {
match f.proto {
ty::proto_vstore(vstore_slice(r)) => flags |= rflags(r),
ty::proto_bare | ty::proto_vstore(_) => {}
}
for f.inputs.each |a| { flags |= get(a.ty).flags; }
flags |= get(f.output).flags;
}
}
let t = @{struct: st, id: cx.next_id, flags: flags, o_def_id: o_def_id};
cx.interner.insert(key, t);
cx.next_id += 1u;
unsafe { unsafe::reinterpret_cast(t) }
}
fn mk_nil(cx: ctxt) -> t { mk_t(cx, ty_nil) }
fn mk_bot(cx: ctxt) -> t { mk_t(cx, ty_bot) }
fn mk_bool(cx: ctxt) -> t { mk_t(cx, ty_bool) }
fn mk_int(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i)) }
fn mk_i8(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i8)) }
fn mk_i16(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i16)) }
fn mk_i32(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i32)) }
fn mk_i64(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_i64)) }
fn mk_float(cx: ctxt) -> t { mk_t(cx, ty_float(ast::ty_f)) }
fn mk_uint(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u)) }
fn mk_u8(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u8)) }
fn mk_u16(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u16)) }
fn mk_u32(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u32)) }
fn mk_u64(cx: ctxt) -> t { mk_t(cx, ty_uint(ast::ty_u64)) }
fn mk_mach_int(cx: ctxt, tm: ast::int_ty) -> t { mk_t(cx, ty_int(tm)) }
fn mk_mach_uint(cx: ctxt, tm: ast::uint_ty) -> t { mk_t(cx, ty_uint(tm)) }
fn mk_mach_float(cx: ctxt, tm: ast::float_ty) -> t { mk_t(cx, ty_float(tm)) }
fn mk_char(cx: ctxt) -> t { mk_t(cx, ty_int(ast::ty_char)) }
fn mk_estr(cx: ctxt, t: vstore) -> t {
mk_t(cx, ty_estr(t))
}
fn mk_enum(cx: ctxt, did: ast::def_id, +substs: substs) -> t {
// take a copy of substs so that we own the vectors inside
mk_t(cx, ty_enum(did, substs))
}
fn mk_box(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_box(tm)) }
fn mk_imm_box(cx: ctxt, ty: t) -> t { mk_box(cx, {ty: ty,
mutbl: ast::m_imm}) }
fn mk_uniq(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_uniq(tm)) }
fn mk_imm_uniq(cx: ctxt, ty: t) -> t { mk_uniq(cx, {ty: ty,
mutbl: ast::m_imm}) }
fn mk_ptr(cx: ctxt, tm: mt) -> t { mk_t(cx, ty_ptr(tm)) }
fn mk_rptr(cx: ctxt, r: region, tm: mt) -> t { mk_t(cx, ty_rptr(r, tm)) }
fn mk_mut_rptr(cx: ctxt, r: region, ty: t) -> t {
mk_rptr(cx, r, {ty: ty, mutbl: ast::m_mutbl})
}
fn mk_imm_rptr(cx: ctxt, r: region, ty: t) -> t {
mk_rptr(cx, r, {ty: ty, mutbl: ast::m_imm})
}
fn mk_mut_ptr(cx: ctxt, ty: t) -> t { mk_ptr(cx, {ty: ty,
mutbl: ast::m_mutbl}) }
fn mk_imm_ptr(cx: ctxt, ty: t) -> t {
mk_ptr(cx, {ty: ty, mutbl: ast::m_imm})
}
fn mk_nil_ptr(cx: ctxt) -> t {
mk_ptr(cx, {ty: mk_nil(cx), mutbl: ast::m_imm})
}
fn mk_evec(cx: ctxt, tm: mt, t: vstore) -> t {
mk_t(cx, ty_evec(tm, t))
}
fn mk_unboxed_vec(cx: ctxt, tm: mt) -> t {
mk_t(cx, ty_unboxed_vec(tm))
}
fn mk_mut_unboxed_vec(cx: ctxt, ty: t) -> t {
mk_t(cx, ty_unboxed_vec({ty: ty, mutbl: ast::m_imm}))
}
fn mk_rec(cx: ctxt, fs: ~[field]) -> t { mk_t(cx, ty_rec(fs)) }
fn mk_tup(cx: ctxt, ts: ~[t]) -> t { mk_t(cx, ty_tup(ts)) }
// take a copy because we want to own the various vectors inside
fn mk_fn(cx: ctxt, +fty: fn_ty) -> t { mk_t(cx, ty_fn(fty)) }
fn mk_trait(cx: ctxt, did: ast::def_id, +substs: substs, vstore: vstore)
-> t {
// take a copy of substs so that we own the vectors inside
mk_t(cx, ty_trait(did, substs, vstore))
}
fn mk_class(cx: ctxt, class_id: ast::def_id, +substs: substs) -> t {
// take a copy of substs so that we own the vectors inside
mk_t(cx, ty_class(class_id, substs))
}
fn mk_var(cx: ctxt, v: tv_vid) -> t { mk_t(cx, ty_var(v)) }
fn mk_var_integral(cx: ctxt, v: tvi_vid) -> t {
mk_t(cx, ty_var_integral(v))
}
fn mk_self(cx: ctxt) -> t { mk_t(cx, ty_self) }
fn mk_param(cx: ctxt, n: uint, k: def_id) -> t {
mk_t(cx, ty_param({idx: n, def_id: k}))
}
fn mk_type(cx: ctxt) -> t { mk_t(cx, ty_type) }
fn mk_opaque_closure_ptr(cx: ctxt, ck: closure_kind) -> t {
mk_t(cx, ty_opaque_closure_ptr(ck))
}
fn mk_opaque_box(cx: ctxt) -> t { mk_t(cx, ty_opaque_box) }
fn mk_with_id(cx: ctxt, base: t, def_id: ast::def_id) -> t {
mk_t_with_id(cx, get(base).struct, some(def_id))
}
// Converts s to its machine type equivalent
pure fn mach_sty(cfg: @session::config, t: t) -> sty {
match get(t).struct {
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
}
}
fn default_arg_mode_for_ty(ty: ty::t) -> ast::rmode {
if ty::type_is_immediate(ty) { ast::by_val }
else { ast::by_ref }
}
// Returns the narrowest lifetime enclosing the evaluation of the expression
// with id `id`.
fn encl_region(cx: ctxt, id: ast::node_id) -> ty::region {
match cx.region_map.find(id) {
some(encl_scope) => ty::re_scope(encl_scope),
none => ty::re_static
}
}
fn walk_ty(ty: t, f: fn(t)) {
maybe_walk_ty(ty, |t| { f(t); true });
}
fn maybe_walk_ty(ty: t, f: fn(t) -> bool) {
if !f(ty) { return; }
match get(ty).struct {
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
ty_estr(_) | ty_type | ty_opaque_box | ty_self |
ty_opaque_closure_ptr(_) | ty_var(_) | ty_var_integral(_) |
ty_param(_) => {
}
ty_box(tm) | ty_evec(tm, _) | ty_unboxed_vec(tm) |
ty_ptr(tm) | ty_rptr(_, tm) => {
maybe_walk_ty(tm.ty, f);
}
ty_enum(_, substs) | ty_class(_, substs) |
ty_trait(_, substs, _) => {
for substs.tps.each |subty| { maybe_walk_ty(subty, f); }
}
ty_rec(fields) => {
for fields.each |fl| { maybe_walk_ty(fl.mt.ty, f); }
}
ty_tup(ts) => { for ts.each |tt| { maybe_walk_ty(tt, f); } }
ty_fn(ref ft) => {
for ft.inputs.each |a| { maybe_walk_ty(a.ty, f); }
maybe_walk_ty(ft.output, f);
}
ty_uniq(tm) => { maybe_walk_ty(tm.ty, f); }
}
}
fn fold_sty_to_ty(tcx: ty::ctxt, sty: &sty, foldop: fn(t) -> t) -> t {
mk_t(tcx, fold_sty(sty, foldop))
}
fn fold_sty(sty: &sty, fldop: fn(t) -> t) -> sty {
fn fold_substs(substs: &substs, fldop: fn(t) -> t) -> substs {
{self_r: substs.self_r,
self_ty: substs.self_ty.map(|t| fldop(t)),
tps: substs.tps.map(|t| fldop(t))}
}
match *sty {
ty_box(tm) => {
ty_box({ty: fldop(tm.ty), mutbl: tm.mutbl})
}
ty_uniq(tm) => {
ty_uniq({ty: fldop(tm.ty), mutbl: tm.mutbl})
}
ty_ptr(tm) => {
ty_ptr({ty: fldop(tm.ty), mutbl: tm.mutbl})
}
ty_unboxed_vec(tm) => {
ty_unboxed_vec({ty: fldop(tm.ty), mutbl: tm.mutbl})
}
ty_evec(tm, vst) => {
ty_evec({ty: fldop(tm.ty), mutbl: tm.mutbl}, vst)
}
ty_enum(tid, ref substs) => {
ty_enum(tid, fold_substs(substs, fldop))
}
ty_trait(did, ref substs, vst) => {
ty_trait(did, fold_substs(substs, fldop), vst)
}
ty_rec(fields) => {
let new_fields = do vec::map(fields) |fl| {
let new_ty = fldop(fl.mt.ty);
let new_mt = {ty: new_ty, mutbl: fl.mt.mutbl};
{ident: fl.ident, mt: new_mt}
};
ty_rec(new_fields)
}
ty_tup(ts) => {
let new_ts = vec::map(ts, |tt| fldop(tt));
ty_tup(new_ts)
}
ty_fn(ref f) => {
let new_args = vec::map(f.inputs, |a| {
let new_ty = fldop(a.ty);
{mode: a.mode, ty: new_ty}
});
let new_output = fldop(f.output);
ty_fn({inputs: new_args, output: new_output with *f})
}
ty_rptr(r, tm) => {
ty_rptr(r, {ty: fldop(tm.ty), mutbl: tm.mutbl})
}
ty_class(did, ref substs) => {
ty_class(did, fold_substs(substs, fldop))
}
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
ty_estr(_) | ty_type | ty_opaque_closure_ptr(_) |
ty_opaque_box | ty_var(_) | ty_var_integral(_) |
ty_param(*) | ty_self => {
*sty
}
}
}
// Folds types from the bottom up.
fn fold_ty(cx: ctxt, t0: t, fldop: fn(t) -> t) -> t {
let sty = fold_sty(&get(t0).struct, |t| fold_ty(cx, fldop(t), fldop));
fldop(mk_t(cx, sty))
}
fn walk_regions_and_ty(
cx: ctxt,
ty: t,
walkr: fn(r: region),
walkt: fn(t: t) -> bool) {
if (walkt(ty)) {
fold_regions_and_ty(
cx, ty,
|r| { walkr(r); r },
|t| { walkt(t); walk_regions_and_ty(cx, t, walkr, walkt); t },
|t| { walkt(t); walk_regions_and_ty(cx, t, walkr, walkt); t });
}
}
fn fold_regions_and_ty(
cx: ctxt,
ty: t,
fldr: fn(r: region) -> region,
fldfnt: fn(t: t) -> t,