Skip to content

Commit e30be9c

Browse files
committed
Read and write type nodes through the accessors in Btype
Btype now uses `get_desc`, `get_level` and `get_id` for every read of a type node and `Transient_expr` for creation and its unlogged writes (path compression, the backtracking trail, copy sessions, marking). Record patterns such as `{desc = Tvar _}` become matches on `get_desc`. Review: Do `repr`'s path compression and its trail entries change? No. `repr_link` takes the same arguments, compresses the same nodes and logs the same `Ccompress` entries. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent 8cc9caf commit e30be9c

1 file changed

Lines changed: 65 additions & 57 deletions

File tree

‎compiler/ml/btype.ml‎

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@ let pivot_level = (2 * lowest_level) - 1
4141
let newty2 level desc =
4242
let state = Compiler_request_state.current () in
4343
state.type_node_id <- state.type_node_id + 1;
44-
{desc; level; id = state.type_node_id}
44+
Transient_expr.create desc ~level ~id:state.type_node_id
4545
let newgenty desc = newty2 generic_level desc
4646
let newgenvar () = newgenty (Tvar None)
4747

4848
(**** Check some types ****)
4949

50-
let is_Tvar = function
51-
| {desc = Tvar _} -> true
50+
let is_Tvar ty =
51+
match get_desc ty with
52+
| Tvar _ -> true
5253
| _ -> false
53-
let is_Tunivar = function
54-
| {desc = Tunivar _} -> true
54+
let is_Tunivar ty =
55+
match get_desc ty with
56+
| Tunivar _ -> true
5557
| _ -> false
56-
let is_Tconstr = function
57-
| {desc = Tconstr _} -> true
58+
let is_Tconstr ty =
59+
match get_desc ty with
60+
| Tconstr _ -> true
5861
| _ -> false
5962

6063
let default_mty = function
@@ -96,16 +99,17 @@ let log_change ch =
9699

97100
(**** Representative of a type ****)
98101

99-
let rec repr_link compress t d = function
100-
| {desc = Tlink t' as d'} -> repr_link true t d' t'
101-
| t' ->
102+
let rec repr_link compress t d t' =
103+
match get_desc t' with
104+
| Tlink t'' as d' -> repr_link true t d' t''
105+
| _ ->
102106
if compress then (
103-
log_change (Ccompress (t, t.desc, d));
104-
t.desc <- d);
107+
log_change (Ccompress (t, get_desc t, d));
108+
Transient_expr.set_desc t d);
105109
t'
106110

107111
let repr t =
108-
match t.desc with
112+
match get_desc t with
109113
| Tlink t' as d -> repr_link false t d t'
110114
| _ -> t
111115

@@ -124,7 +128,7 @@ let rec rev_concat l ll =
124128
| l' :: ll -> rev_concat (l' @ l) ll
125129

126130
let rec row_repr_aux ll row =
127-
match (repr row.row_more).desc with
131+
match get_desc (repr row.row_more) with
128132
| Tvariant row' ->
129133
let f = row.row_fields in
130134
row_repr_aux (if f = [] then ll else f :: ll) row'
@@ -139,22 +143,23 @@ let rec row_field tag row =
139143
| (tag', f) :: fields ->
140144
if tag = tag' then row_field_repr f else find fields
141145
| [] -> (
142-
match repr row.row_more with
143-
| {desc = Tvariant row'} -> row_field tag row'
146+
match get_desc (repr row.row_more) with
147+
| Tvariant row' -> row_field tag row'
144148
| _ -> Rabsent)
145149
in
146150
find row.row_fields
147151

148152
let rec row_more row =
149-
match repr row.row_more with
150-
| {desc = Tvariant row'} -> row_more row'
151-
| ty -> ty
153+
let ty = repr row.row_more in
154+
match get_desc ty with
155+
| Tvariant row' -> row_more row'
156+
| _ -> ty
152157

153158
let row_fixed row =
154159
let row = row_repr row in
155160
row.row_fixed
156161
||
157-
match (repr row.row_more).desc with
162+
match get_desc (repr row.row_more) with
158163
| Tvar _ | Tnil -> false
159164
| Tunivar _ | Tconstr _ -> true
160165
| _ -> assert false
@@ -181,11 +186,11 @@ let hash_variant s =
181186

182187
let proxy ty =
183188
let ty0 = repr ty in
184-
match ty0.desc with
189+
match get_desc ty0 with
185190
| Tvariant row when not (static_row row) -> row_more row
186191
| Tobject ty ->
187192
let rec proxy_obj ty =
188-
match ty.desc with
193+
match get_desc ty with
189194
| Tfield {rest = ty} | Tlink ty -> proxy_obj ty
190195
| Tvar _ | Tunivar _ | Tconstr _ -> ty
191196
| Tnil -> ty0
@@ -197,11 +202,11 @@ let proxy ty =
197202
(**** Utilities for fixed row private types ****)
198203

199204
let row_of_type t =
200-
match (repr t).desc with
205+
match get_desc (repr t) with
201206
| Tobject t ->
202207
let rec get_row t =
203208
let t = repr t in
204-
match t.desc with
209+
match get_desc t with
205210
| Tfield {rest = t} -> get_row t
206211
| _ -> t
207212
in
@@ -216,7 +221,7 @@ let is_row_name s =
216221
if l < 4 then false else String.sub s (l - 4) 4 = "#row"
217222

218223
let is_constr_row ~allow_ident t =
219-
match t.desc with
224+
match get_desc t with
220225
| Tconstr (Path.Pident id, _, _) when allow_ident ->
221226
is_row_name (Ident.name id)
222227
| Tconstr (Path.Pdot (_, s, _), _, _) -> is_row_name s
@@ -234,7 +239,7 @@ let rec iter_row f row =
234239
| Reither (_, tl, _, _) -> List.iter f tl
235240
| _ -> ())
236241
row.row_fields;
237-
match (repr row.row_more).desc with
242+
match get_desc (repr row.row_more) with
238243
| Tvariant row -> iter_row f row
239244
| Tvar _ | Tunivar _ | Tsubst _ | Tconstr _ | Tnil ->
240245
Misc.may (fun (_, l) -> List.iter f l) row.row_name
@@ -248,7 +253,7 @@ let rec iter_arg_types f = function
248253
iter_arg_types f params
249254

250255
let iter_type_expr f ty =
251-
match ty.desc with
256+
match get_desc ty with
252257
| Tvar _ -> ()
253258
| Tarrow (params, ret) ->
254259
iter_arg_types f params;
@@ -343,7 +348,7 @@ let type_iterators =
343348
and it_type_kind it kind = iter_type_expr_kind (it.it_type_expr it) kind
344349
and it_do_type_expr it ty =
345350
iter_type_expr (it.it_type_expr it) ty;
346-
match ty.desc with
351+
match get_desc ty with
347352
| Tconstr (p, _, _) | Tpackage (p, _, _) -> it.it_path p
348353
| Tvariant row -> may (fun (p, _) -> it.it_path p) (row_repr row).row_name
349354
| _ -> ()
@@ -394,7 +399,7 @@ let copy_row f fixed row keep more =
394399
(* Since univars may be used as row variables, we need to do some
395400
encoding during substitution *)
396401
let rec norm_univar ty =
397-
match ty.desc with
402+
match get_desc ty with
398403
| Tunivar _ | Tsubst _ -> ty
399404
| Tlink ty -> norm_univar ty
400405
| Ttuple (ty :: _) -> norm_univar ty
@@ -449,7 +454,9 @@ let end_type_copy_session () =
449454
let type_copy_sessions = Domain.DLS.get type_copy_sessions in
450455
match !type_copy_sessions with
451456
| session :: rest ->
452-
List.iter (fun (ty, desc) -> ty.desc <- desc) session.saved_desc;
457+
List.iter
458+
(fun (ty, desc) -> Transient_expr.set_desc ty desc)
459+
session.saved_desc;
453460
List.iter (fun (r, v) -> r := v) session.saved_mutabilities;
454461
type_copy_sessions := rest
455462
| [] -> assert false
@@ -486,21 +493,21 @@ let row_terminator_generic rest =
486493
in
487494
let rec go t =
488495
let t = repr t in
489-
match Hashtbl.find_opt copy_policy_memo t.id with
496+
match Hashtbl.find_opt copy_policy_memo (get_id t) with
490497
| Some b -> b
491498
| None ->
492499
let b =
493-
match t.desc with
500+
match get_desc t with
494501
| Tfield {rest} -> go rest
495-
| Tvar _ -> t.level = generic_level
502+
| Tvar _ -> get_level t = generic_level
496503
| Tsubst t' ->
497504
(* Already copied in this session: follow the copy; its fresh
498505
terminator is generic exactly when the row is being
499506
instantiated. *)
500507
go t'
501508
| _ -> false
502509
in
503-
Hashtbl.add copy_policy_memo t.id b;
510+
Hashtbl.add copy_policy_memo (get_id t) b;
504511
b
505512
in
506513
go rest
@@ -531,7 +538,7 @@ let rec copy_type_desc ?(keep_names = false) ?(fresh_mutability = false) f =
531538
in
532539
Tfield {f_ with mutability; typ = f f_.typ; rest = f f_.rest}
533540
| Tnil -> Tnil
534-
| Tlink ty -> copy_type_desc ~keep_names ~fresh_mutability f ty.desc
541+
| Tlink ty -> copy_type_desc ~keep_names ~fresh_mutability f (get_desc ty)
535542
| Tsubst _ -> assert false
536543
| Tunivar _ as ty -> ty (* always keep the name *)
537544
| Tpoly (ty, tyl) ->
@@ -561,18 +568,19 @@ let with_copy_session f =
561568
(* Mark a type. *)
562569
let rec mark_type ty =
563570
let ty = repr ty in
564-
if ty.level >= lowest_level then (
565-
ty.level <- pivot_level - ty.level;
571+
if get_level ty >= lowest_level then (
572+
Transient_expr.set_level ty (pivot_level - get_level ty);
566573
iter_type_expr mark_type ty)
567574

568575
let mark_type_node ty =
569576
let ty = repr ty in
570-
if ty.level >= lowest_level then ty.level <- pivot_level - ty.level
577+
if get_level ty >= lowest_level then
578+
Transient_expr.set_level ty (pivot_level - get_level ty)
571579

572580
let type_iterators =
573581
let it_type_expr it ty =
574582
let ty = repr ty in
575-
if ty.level >= lowest_level then (
583+
if get_level ty >= lowest_level then (
576584
mark_type_node ty;
577585
it.it_do_type_expr it ty)
578586
in
@@ -581,8 +589,8 @@ let type_iterators =
581589
(* Remove marks from a type. *)
582590
let rec unmark_type ty =
583591
let ty = repr ty in
584-
if ty.level < lowest_level then (
585-
ty.level <- pivot_level - ty.level;
592+
if get_level ty < lowest_level then (
593+
Transient_expr.set_level ty (pivot_level - get_level ty);
586594
iter_type_expr unmark_type ty)
587595

588596
let unmark_iterators =
@@ -697,9 +705,9 @@ let rec label_assoc x (args : sargs) =
697705
(**********************************)
698706

699707
let undo_change = function
700-
| Ctype (ty, desc) -> ty.desc <- desc
701-
| Ccompress (ty, desc, _) -> ty.desc <- desc
702-
| Clevel (ty, level) -> ty.level <- level
708+
| Ctype (ty, desc) -> Transient_expr.set_desc ty desc
709+
| Ccompress (ty, desc, _) -> Transient_expr.set_desc ty desc
710+
| Clevel (ty, level) -> Transient_expr.set_level ty level
703711
| Crow (r, v) -> r := v
704712
| Cmutability (r, v) -> r := v
705713
| Cuniv (r, v) -> r := v
@@ -708,34 +716,34 @@ let undo_change = function
708716
type snapshot = changes ref * int
709717

710718
let log_type ty =
711-
if ty.id <= (Compiler_request_state.current ()).type_node_last_snapshot then
712-
log_change (Ctype (ty, ty.desc))
719+
if get_id ty <= (Compiler_request_state.current ()).type_node_last_snapshot
720+
then log_change (Ctype (ty, get_desc ty))
713721
let link_type ty ty' =
714722
log_type ty;
715-
let desc = ty.desc in
716-
ty.desc <- Tlink ty';
723+
let desc = get_desc ty in
724+
Transient_expr.set_desc ty (Tlink ty');
717725
(* Name is a user-supplied name for this unification variable (obtained
718726
* through a type annotation for instance). *)
719-
match (desc, ty'.desc) with
727+
match (desc, get_desc ty') with
720728
| Tvar name, Tvar name' -> (
721729
match (name, name') with
722730
| Some _, None ->
723731
log_type ty';
724-
ty'.desc <- Tvar name
732+
Transient_expr.set_desc ty' (Tvar name)
725733
| None, Some _ -> ()
726734
| Some _, Some _ ->
727-
if ty.level < ty'.level then (
735+
if get_level ty < get_level ty' then (
728736
log_type ty';
729-
ty'.desc <- Tvar name)
737+
Transient_expr.set_desc ty' (Tvar name))
730738
| None, None -> ())
731739
| _ -> ()
732740

733741
(* ; assert (check_memorized_abbrevs ()) *)
734742
(* ; check_expans [] ty' *)
735743
let set_level ty level =
736-
if ty.id <= (Compiler_request_state.current ()).type_node_last_snapshot then
737-
log_change (Clevel (ty, ty.level));
738-
ty.level <- level
744+
if get_id ty <= (Compiler_request_state.current ()).type_node_last_snapshot
745+
then log_change (Clevel (ty, get_level ty));
746+
Transient_expr.set_level ty level
739747
let set_univar rty ty =
740748
log_change (Cuniv (rty, !rty));
741749
rty := Some ty
@@ -797,8 +805,8 @@ let undo_compress (changes, _old) =
797805
List.iter
798806
(fun r ->
799807
match !r with
800-
| Change (Ccompress (ty, desc, d), next) when ty.desc == d ->
801-
ty.desc <- desc;
808+
| Change (Ccompress (ty, desc, d), next) when get_desc ty == d ->
809+
Transient_expr.set_desc ty desc;
802810
r := !next
803811
| _ -> ())
804812
log

0 commit comments

Comments
 (0)