Skip to content

Commit e7dd1e1

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 148b9d0 commit e7dd1e1

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 ?name () = newgenty (Tvar name)
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,14 +239,14 @@ 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
241246
| _ -> assert false
242247

243248
let iter_type_expr f ty =
244-
match ty.desc with
249+
match get_desc ty with
245250
| Tvar _ -> ()
246251
| Tarrow (params, ret) ->
247252
List.iter (fun {typ} -> f typ) params;
@@ -336,7 +341,7 @@ let type_iterators =
336341
and it_type_kind it kind = iter_type_expr_kind (it.it_type_expr it) kind
337342
and it_do_type_expr it ty =
338343
iter_type_expr (it.it_type_expr it) ty;
339-
match ty.desc with
344+
match get_desc ty with
340345
| Tconstr (p, _, _) | Tpackage (p, _, _) -> it.it_path p
341346
| Tvariant row -> may (fun (p, _) -> it.it_path p) (row_repr row).row_name
342347
| _ -> ()
@@ -387,7 +392,7 @@ let copy_row f fixed row keep more =
387392
(* Since univars may be used as row variables, we need to do some
388393
encoding during substitution *)
389394
let rec norm_univar ty =
390-
match ty.desc with
395+
match get_desc ty with
391396
| Tunivar _ | Tsubst _ -> ty
392397
| Tlink ty -> norm_univar ty
393398
| Ttuple (ty :: _) -> norm_univar ty
@@ -442,7 +447,9 @@ let end_type_copy_session () =
442447
let type_copy_sessions = Domain.DLS.get type_copy_sessions in
443448
match !type_copy_sessions with
444449
| session :: rest ->
445-
List.iter (fun (ty, desc) -> ty.desc <- desc) session.saved_desc;
450+
List.iter
451+
(fun (ty, desc) -> Transient_expr.set_desc ty desc)
452+
session.saved_desc;
446453
List.iter (fun (r, v) -> r := v) session.saved_mutabilities;
447454
type_copy_sessions := rest
448455
| [] -> assert false
@@ -479,21 +486,21 @@ let row_terminator_generic rest =
479486
in
480487
let rec go t =
481488
let t = repr t in
482-
match Hashtbl.find_opt copy_policy_memo t.id with
489+
match Hashtbl.find_opt copy_policy_memo (get_id t) with
483490
| Some b -> b
484491
| None ->
485492
let b =
486-
match t.desc with
493+
match get_desc t with
487494
| Tfield {rest} -> go rest
488-
| Tvar _ -> t.level = generic_level
495+
| Tvar _ -> get_level t = generic_level
489496
| Tsubst t' ->
490497
(* Already copied in this session: follow the copy; its fresh
491498
terminator is generic exactly when the row is being
492499
instantiated. *)
493500
go t'
494501
| _ -> false
495502
in
496-
Hashtbl.add copy_policy_memo t.id b;
503+
Hashtbl.add copy_policy_memo (get_id t) b;
497504
b
498505
in
499506
go rest
@@ -524,7 +531,7 @@ let rec copy_type_desc ?(keep_names = false) ?(fresh_mutability = false) f =
524531
in
525532
Tfield {f_ with mutability; typ = f f_.typ; rest = f f_.rest}
526533
| Tnil -> Tnil
527-
| Tlink ty -> copy_type_desc ~keep_names ~fresh_mutability f ty.desc
534+
| Tlink ty -> copy_type_desc ~keep_names ~fresh_mutability f (get_desc ty)
528535
| Tsubst _ -> assert false
529536
| Tunivar _ as ty -> ty (* always keep the name *)
530537
| Tpoly (ty, tyl) ->
@@ -554,18 +561,19 @@ let with_copy_session f =
554561
(* Mark a type. *)
555562
let rec mark_type ty =
556563
let ty = repr ty in
557-
if ty.level >= lowest_level then (
558-
ty.level <- pivot_level - ty.level;
564+
if get_level ty >= lowest_level then (
565+
Transient_expr.set_level ty (pivot_level - get_level ty);
559566
iter_type_expr mark_type ty)
560567

561568
let mark_type_node ty =
562569
let ty = repr ty in
563-
if ty.level >= lowest_level then ty.level <- pivot_level - ty.level
570+
if get_level ty >= lowest_level then
571+
Transient_expr.set_level ty (pivot_level - get_level ty)
564572

565573
let type_iterators =
566574
let it_type_expr it ty =
567575
let ty = repr ty in
568-
if ty.level >= lowest_level then (
576+
if get_level ty >= lowest_level then (
569577
mark_type_node ty;
570578
it.it_do_type_expr it ty)
571579
in
@@ -574,8 +582,8 @@ let type_iterators =
574582
(* Remove marks from a type. *)
575583
let rec unmark_type ty =
576584
let ty = repr ty in
577-
if ty.level < lowest_level then (
578-
ty.level <- pivot_level - ty.level;
585+
if get_level ty < lowest_level then (
586+
Transient_expr.set_level ty (pivot_level - get_level ty);
579587
iter_type_expr unmark_type ty)
580588

581589
let unmark_iterators =
@@ -690,9 +698,9 @@ let rec label_assoc x (args : sargs) =
690698
(**********************************)
691699

692700
let undo_change = function
693-
| Ctype (ty, desc) -> ty.desc <- desc
694-
| Ccompress (ty, desc, _) -> ty.desc <- desc
695-
| Clevel (ty, level) -> ty.level <- level
701+
| Ctype (ty, desc) -> Transient_expr.set_desc ty desc
702+
| Ccompress (ty, desc, _) -> Transient_expr.set_desc ty desc
703+
| Clevel (ty, level) -> Transient_expr.set_level ty level
696704
| Crow (r, v) -> r := v
697705
| Cmutability (r, v) -> r := v
698706
| Cuniv (r, v) -> r := v
@@ -701,34 +709,34 @@ let undo_change = function
701709
type snapshot = changes ref * int
702710

703711
let log_type ty =
704-
if ty.id <= (Compiler_request_state.current ()).type_node_last_snapshot then
705-
log_change (Ctype (ty, ty.desc))
712+
if get_id ty <= (Compiler_request_state.current ()).type_node_last_snapshot
713+
then log_change (Ctype (ty, get_desc ty))
706714
let link_type ty ty' =
707715
log_type ty;
708-
let desc = ty.desc in
709-
ty.desc <- Tlink ty';
716+
let desc = get_desc ty in
717+
Transient_expr.set_desc ty (Tlink ty');
710718
(* Name is a user-supplied name for this unification variable (obtained
711719
* through a type annotation for instance). *)
712-
match (desc, ty'.desc) with
720+
match (desc, get_desc ty') with
713721
| Tvar name, Tvar name' -> (
714722
match (name, name') with
715723
| Some _, None ->
716724
log_type ty';
717-
ty'.desc <- Tvar name
725+
Transient_expr.set_desc ty' (Tvar name)
718726
| None, Some _ -> ()
719727
| Some _, Some _ ->
720-
if ty.level < ty'.level then (
728+
if get_level ty < get_level ty' then (
721729
log_type ty';
722-
ty'.desc <- Tvar name)
730+
Transient_expr.set_desc ty' (Tvar name))
723731
| None, None -> ())
724732
| _ -> ()
725733

726734
(* ; assert (check_memorized_abbrevs ()) *)
727735
(* ; check_expans [] ty' *)
728736
let set_level ty level =
729-
if ty.id <= (Compiler_request_state.current ()).type_node_last_snapshot then
730-
log_change (Clevel (ty, ty.level));
731-
ty.level <- level
737+
if get_id ty <= (Compiler_request_state.current ()).type_node_last_snapshot
738+
then log_change (Clevel (ty, get_level ty));
739+
Transient_expr.set_level ty level
732740
let set_univar rty ty =
733741
log_change (Cuniv (rty, !rty));
734742
rty := Some ty
@@ -790,8 +798,8 @@ let undo_compress (changes, _old) =
790798
List.iter
791799
(fun r ->
792800
match !r with
793-
| Change (Ccompress (ty, desc, d), next) when ty.desc == d ->
794-
ty.desc <- desc;
801+
| Change (Ccompress (ty, desc, d), next) when get_desc ty == d ->
802+
Transient_expr.set_desc ty desc;
795803
r := !next
796804
| _ -> ())
797805
log

0 commit comments

Comments
 (0)