Skip to content

Commit b6233fb

Browse files
freemagmaCharlie Gunngoldfirere
authored
Migrate modality annotations into the parsetree (#2468)
Migrate modality annotations (on label declarations / constructor fields) away from Jane_syntax and into the parsetree proper --------- Co-authored-by: Charlie Gunn <[email protected]> Co-authored-by: Richard Eisenberg <[email protected]>
1 parent 6f8f703 commit b6233fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+36202
-2724
lines changed

ocaml/boot/menhir/parser.ml

Lines changed: 35838 additions & 2560 deletions
Large diffs are not rendered by default.

ocaml/ocamldoc/odoc_sig.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ module Analyser =
461461
let comment_opt = analyze_alerts comment_opt cd_attributes in
462462
let vc_args =
463463
match cd_args with
464-
| Cstr_tuple l -> Cstr_tuple (List.map (fun (ty, _) -> Odoc_env.subst_type env ty) l)
464+
| Cstr_tuple l -> Cstr_tuple (List.map (fun {ca_type=ty; _} -> Odoc_env.subst_type env ty) l)
465465
| Cstr_record l ->
466466
Cstr_record (List.map (get_field env name_comment_list) l)
467467
in
@@ -499,7 +499,7 @@ module Analyser =
499499
let open Typedtree in
500500
function
501501
| Cstr_tuple l ->
502-
Odoc_type.Cstr_tuple (List.map (fun (ty, _) -> tuple ty) l)
502+
Odoc_type.Cstr_tuple (List.map (fun {ca_type=ty; _} -> tuple ty) l)
503503
| Cstr_record l ->
504504
let comments = Record.(doc typedtree) pos_end l in
505505
Odoc_type.Cstr_record (List.map (record comments) l)
@@ -980,7 +980,7 @@ module Analyser =
980980
let xt_args =
981981
match types_ext.ext_args with
982982
| Cstr_tuple l ->
983-
Cstr_tuple (List.map (fun (ty, _) -> Odoc_env.subst_type new_env ty) l)
983+
Cstr_tuple (List.map (fun {ca_type=ty; _} -> Odoc_env.subst_type new_env ty) l)
984984
| Cstr_record l ->
985985
let docs = Record.(doc types ext_loc_end) l in
986986
Cstr_record (List.map (get_field new_env docs) l)
@@ -1026,7 +1026,7 @@ module Analyser =
10261026
let ex_args =
10271027
let pos_end = Loc.end_ types_ext.ext_loc in
10281028
match types_ext.ext_args with
1029-
| Cstr_tuple l -> Cstr_tuple (List.map (fun (ty, _) -> Odoc_env.subst_type env ty) l)
1029+
| Cstr_tuple l -> Cstr_tuple (List.map (fun {ca_type=ty; _} -> Odoc_env.subst_type env ty) l)
10301030
| Cstr_record l ->
10311031
let docs = Record.(doc types) pos_end l in
10321032
Cstr_record (List.map (get_field env docs) l)

ocaml/parsing/ast_helper.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,19 @@ module Type = struct
550550
pcd_attributes = add_info_attrs info attrs;
551551
}
552552

553+
let constructor_arg ?(loc = !default_loc) ?(modalities = []) typ =
554+
{
555+
pca_modalities = modalities;
556+
pca_type = typ;
557+
pca_loc = loc;
558+
}
559+
553560
let field ?(loc = !default_loc) ?(attrs = []) ?(info = empty_info)
554-
?(mut = Immutable) name typ =
561+
?(mut = Immutable) ?(modalities = []) name typ =
555562
{
556563
pld_name = name;
557564
pld_mutable = mut;
565+
pld_modalities = modalities;
558566
pld_type = typ;
559567
pld_loc = loc;
560568
pld_attributes = add_info_attrs info attrs;

ocaml/parsing/ast_helper.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ module Type:
226226
?vars:str list -> ?args:constructor_arguments -> ?res:core_type ->
227227
str ->
228228
constructor_declaration
229+
230+
val constructor_arg: ?loc:loc -> ?modalities:modality with_loc list -> core_type ->
231+
constructor_argument
232+
229233
val field: ?loc:loc -> ?attrs:attrs -> ?info:info ->
230-
?mut:mutable_flag -> str -> core_type -> label_declaration
234+
?mut:mutable_flag -> ?modalities:modality with_loc list -> str -> core_type ->
235+
label_declaration
231236
end
232237

233238
(** Type extensions *)

ocaml/parsing/ast_iterator.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,16 @@ module T = struct
222222
| Ptype_record l -> List.iter (sub.label_declaration sub) l
223223
| Ptype_open -> ()
224224

225+
let iter_modalities sub modalities =
226+
List.iter (iter_loc sub) modalities
227+
228+
let iter_constructor_argument sub {pca_type; pca_loc; pca_modalities} =
229+
sub.typ sub pca_type;
230+
sub.location sub pca_loc;
231+
iter_modalities sub pca_modalities
232+
225233
let iter_constructor_arguments sub = function
226-
| Pcstr_tuple l -> List.iter (sub.typ sub) l
234+
| Pcstr_tuple l -> List.iter (iter_constructor_argument sub) l
227235
| Pcstr_record l ->
228236
List.iter (sub.label_declaration sub) l
229237

@@ -958,11 +966,12 @@ let default_iterator =
958966
);
959967

960968
label_declaration =
961-
(fun this {pld_name; pld_type; pld_loc; pld_mutable = _; pld_attributes}->
969+
(fun this {pld_name; pld_type; pld_loc; pld_mutable = _; pld_modalities; pld_attributes}->
962970
iter_loc this pld_name;
963971
this.typ this pld_type;
964972
this.location this pld_loc;
965-
this.attributes this pld_attributes
973+
this.attributes this pld_attributes;
974+
T.iter_modalities this pld_modalities
966975
);
967976

968977
cases = (fun this l -> List.iter (this.case this) l);

ocaml/parsing/ast_mapper.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,17 @@ module T = struct
263263
| Ptype_record l -> Ptype_record (List.map (sub.label_declaration sub) l)
264264
| Ptype_open -> Ptype_open
265265

266+
let map_modalities sub modalities =
267+
List.map (map_loc sub) modalities
268+
269+
let map_constructor_argument sub x =
270+
let pca_type = sub.typ sub x.pca_type in
271+
let pca_loc = sub.location sub x.pca_loc in
272+
let pca_modalities = map_modalities sub x.pca_modalities in
273+
{ pca_type; pca_loc; pca_modalities }
274+
266275
let map_constructor_arguments sub = function
267-
| Pcstr_tuple l -> Pcstr_tuple (List.map (sub.typ sub) l)
276+
| Pcstr_tuple l -> Pcstr_tuple (List.map (map_constructor_argument sub) l)
268277
| Pcstr_record l ->
269278
Pcstr_record (List.map (sub.label_declaration sub) l)
270279

@@ -1074,11 +1083,12 @@ let default_mapper =
10741083
);
10751084

10761085
label_declaration =
1077-
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
1086+
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_modalities; pld_attributes} ->
10781087
Type.field
10791088
(map_loc this pld_name)
10801089
(this.typ this pld_type)
10811090
~mut:pld_mutable
1091+
~modalities:(T.map_modalities this pld_modalities)
10821092
~loc:(this.location this pld_loc)
10831093
~attrs:(this.attributes this pld_attributes)
10841094
);

ocaml/parsing/depend.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ let add_opt add_fn bv = function
156156
| Some x -> add_fn bv x
157157

158158
let add_constructor_arguments bv = function
159-
| Pcstr_tuple l -> List.iter (add_type bv) l
159+
| Pcstr_tuple l -> List.iter (fun a -> add_type bv a.pca_type) l
160160
| Pcstr_record l -> List.iter (fun l -> add_type bv l.pld_type) l
161161

162162
let add_constructor_decl bv pcd =

ocaml/parsing/jane_syntax.mli

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ module Mode_expr : sig
105105
- let local_ x = ...
106106
- local_ exp
107107
- local string -> string
108-
- {global_ x : int}
109108
110109
Note that in the first two cases, axes other than locality are not specified;
111-
in the second case, other axes are defaulted to legacy. In the last case, we
112-
are specifying modalities.
110+
in the second case, other axes are defaulted to legacy.
113111
114-
In the future the three annotations will be quite different, but for now they
115-
are all lists of modes/modalities. [Typemode] has the three different
112+
In the future the two annotations will be quite different, but for now they
113+
are just lists of modes. [Typemode] has the two different
116114
interpretations of the annotation.
117115
118116
(TODO: in the future we will have mutable(...), which is similar to the second

ocaml/parsing/parser.mly

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,16 +4052,15 @@ generalized_constructor_arguments:
40524052
{ ($2,Pcstr_tuple [],Some $4) }
40534053
;
40544054

4055-
%inline atomic_type_with_modality:
4056-
gbl = global_flag cty = atomic_type m1 = optional_atat_mode_expr {
4057-
let m = Mode.concat gbl m1 in
4058-
mktyp_with_modes m cty
4059-
}
4055+
%inline constructor_argument:
4056+
gbl=global_flag cty=atomic_type m1=optional_atat_modalities_expr {
4057+
let modalities = gbl @ m1 in
4058+
Type.constructor_arg cty ~modalities ~loc:(make_loc $sloc)
4059+
}
40604060
;
40614061

40624062
constructor_arguments:
4063-
| tys = inline_separated_nonempty_llist(STAR, atomic_type_with_modality)
4064-
%prec below_HASH
4063+
| tys = inline_separated_nonempty_llist(STAR, constructor_argument)
40654064
{ Pcstr_tuple tys }
40664065
| LBRACE label_declarations RBRACE
40674066
{ Pcstr_record $2 }
@@ -4072,25 +4071,23 @@ label_declarations:
40724071
| label_declaration_semi label_declarations { $1 :: $2 }
40734072
;
40744073
label_declaration:
4075-
mutable_or_global_flag mkrhs(label) COLON poly_type_no_attr m1=optional_atat_mode_expr attrs=attributes
4074+
mutable_or_global_flag mkrhs(label) COLON poly_type_no_attr m1=optional_atat_modalities_expr attrs=attributes
40764075
{ let info = symbol_info $endpos in
40774076
let mut, m0 = $1 in
4078-
let m = Mode.concat m0 m1 in
4079-
let typ = mktyp_with_modes m $4 in
4080-
Type.field $2 typ ~mut ~attrs ~loc:(make_loc $sloc) ~info}
4077+
let modalities = m0 @ m1 in
4078+
Type.field $2 $4 ~mut ~modalities ~attrs ~loc:(make_loc $sloc) ~info}
40814079
;
40824080
label_declaration_semi:
4083-
mutable_or_global_flag mkrhs(label) COLON poly_type_no_attr m1=optional_atat_mode_expr attrs0=attributes
4081+
mutable_or_global_flag mkrhs(label) COLON poly_type_no_attr m1=optional_atat_modalities_expr attrs0=attributes
40844082
SEMI attrs1=attributes
40854083
{ let info =
40864084
match rhs_info $endpos(attrs0) with
40874085
| Some _ as info_before_semi -> info_before_semi
40884086
| None -> symbol_info $endpos
40894087
in
40904088
let mut, m0 = $1 in
4091-
let m = Mode.concat m0 m1 in
4092-
let typ = mktyp_with_modes m $4 in
4093-
Type.field $2 typ ~mut ~attrs:(attrs0 @ attrs1) ~loc:(make_loc $sloc) ~info}
4089+
let modalities = m0 @ m1 in
4090+
Type.field $2 $4 ~mut ~modalities ~attrs:(attrs0 @ attrs1) ~loc:(make_loc $sloc) ~info}
40944091
;
40954092

40964093
/* Type Extensions */
@@ -4418,6 +4415,21 @@ atat_mode_expr:
44184415
| atat_mode_expr {$1}
44194416
;
44204417

4418+
/* Modalities */
4419+
4420+
%inline modality:
4421+
| LIDENT { mkloc (Modality $1) (make_loc $sloc) }
4422+
4423+
%inline modalities:
4424+
| modality+ { $1 }
4425+
4426+
optional_atat_modalities_expr:
4427+
| %prec below_HASH
4428+
{ [] }
4429+
| ATAT modalities { $2 }
4430+
| ATAT error { expecting $loc($2) "modality expression" }
4431+
;
4432+
44214433
%inline param_type:
44224434
| mktyp_jane_syntax_ltyp(
44234435
LPAREN bound_vars = typevar_list DOT inner_type = core_type RPAREN
@@ -4869,15 +4881,15 @@ mutable_flag:
48694881
;
48704882
mutable_or_global_flag:
48714883
/* empty */
4872-
{ Immutable, Mode.empty }
4884+
{ Immutable, [] }
48734885
| MUTABLE
4874-
{ Mutable, Mode.empty }
4886+
{ Mutable, [] }
48754887
| GLOBAL
4876-
{ Immutable, Mode.singleton (Mode.Const.mk "global" (make_loc $sloc)) }
4888+
{ Immutable, [ mkloc (Modality "global") (make_loc $sloc)] }
48774889
;
48784890
%inline global_flag:
4879-
{ Mode.empty }
4880-
| GLOBAL { Mode.singleton (Mode.Const.mk "global" (make_loc $sloc)) }
4891+
{ [] }
4892+
| GLOBAL { [ mkloc (Modality "global") (make_loc $sloc)] }
48814893
;
48824894
virtual_flag:
48834895
/* empty */ { Concrete }

ocaml/parsing/parsetree.mli

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type constant =
4545

4646
type location_stack = Location.t list
4747

48+
type modality = | Modality of string [@@unboxed]
49+
4850
(** {1 Extension points} *)
4951

5052
type attribute = {
@@ -530,6 +532,7 @@ and label_declaration =
530532
{
531533
pld_name: string loc;
532534
pld_mutable: mutable_flag;
535+
pld_modalities: modality loc list;
533536
pld_type: core_type;
534537
pld_loc: Location.t;
535538
pld_attributes: attributes; (** [l : T [\@id1] [\@id2]] *)
@@ -555,8 +558,15 @@ and constructor_declaration =
555558
pcd_attributes: attributes; (** [C of ... [\@id1] [\@id2]] *)
556559
}
557560

561+
and constructor_argument =
562+
{
563+
pca_modalities: modality loc list;
564+
pca_type: core_type;
565+
pca_loc: Location.t;
566+
}
567+
558568
and constructor_arguments =
559-
| Pcstr_tuple of core_type list
569+
| Pcstr_tuple of constructor_argument list
560570
| Pcstr_record of label_declaration list
561571
(** Values of type {!constructor_declaration}
562572
represents the constructor arguments of:

ocaml/parsing/pprintast.ml

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ let legacy_mode f m =
298298
| "local" -> "local_"
299299
| "unique" -> "unique_"
300300
| "once" -> "once_"
301-
| "global" -> "global_" (* global modality *)
302301
| s -> Misc.fatal_errorf "Unrecognized mode %s - should not parse" s
303302
in
304303
pp_print_string f s
@@ -313,6 +312,25 @@ let optional_legacy_modes f m =
313312
legacy_modes f m;
314313
pp_print_space f ()
315314

315+
let legacy_modality f m =
316+
let {txt; _} = (m : modality Location.loc) in
317+
let s =
318+
match txt with
319+
| Modality "global" -> "global_"
320+
| Modality s -> Misc.fatal_errorf "Unrecognized modality %s - should not parse" s
321+
in
322+
pp_print_string f s
323+
324+
let legacy_modalities f m =
325+
pp_print_list ~pp_sep:(fun f () -> pp f " ") legacy_modality f m
326+
327+
let optional_legacy_modalities f m =
328+
match m with
329+
| [] -> ()
330+
| m ->
331+
legacy_modalities f m;
332+
pp_print_space f ()
333+
316334
let mode f m =
317335
let {txt; _} = (m : Jane_syntax.Mode_expr.Const.t :> _ Location.loc) in
318336
pp_print_string f txt
@@ -336,6 +354,12 @@ let maybe_type_atat_modes pty ctxt f c =
336354
| Some m -> pp f "%a@ @@@@@ %a" (pty ctxt) c modes m
337355
| None -> pty ctxt f c
338356

357+
let modalities_type pty ctxt f pca =
358+
match pca.pca_modalities with
359+
| [] -> pty ctxt f pca.pca_type
360+
| m ->
361+
pp f "%a %a" legacy_modalities m (pty ctxt) pca.pca_type
362+
339363
(* c ['a,'b] *)
340364
let rec class_params_def ctxt f = function
341365
| [] -> ()
@@ -1865,15 +1889,11 @@ and type_def_list ctxt f (rf, exported, l) =
18651889

18661890
and record_declaration ctxt f lbls =
18671891
let type_record_field f pld =
1868-
let modalities, ptyp_attributes =
1869-
Jane_syntax.Mode_expr.maybe_of_attrs pld.pld_type.ptyp_attributes
1870-
in
1871-
let pld_type = {pld.pld_type with ptyp_attributes} in
18721892
pp f "@[<2>%a%a%s:@;%a@;%a@]"
18731893
mutable_flag pld.pld_mutable
1874-
optional_legacy_modes modalities
1894+
optional_legacy_modalities pld.pld_modalities
18751895
pld.pld_name.txt
1876-
(core_type ctxt) pld_type
1896+
(core_type ctxt) pld.pld_type
18771897
(attributes ctxt) pld.pld_attributes
18781898
in
18791899
pp f "{@\n%a}"
@@ -1964,7 +1984,7 @@ and constructor_declaration ctxt f (name, vars_jkinds, args, res, attrs) =
19641984
(fun f -> function
19651985
| Pcstr_tuple [] -> ()
19661986
| Pcstr_tuple l ->
1967-
pp f "@;of@;%a" (list (maybe_modes_type core_type1 ctxt) ~sep:"@;*@;") l
1987+
pp f "@;of@;%a" (list (modalities_type core_type1 ctxt) ~sep:"@;*@;") l
19681988
| Pcstr_record l -> pp f "@;of@;%a" (record_declaration ctxt) l
19691989
) args
19701990
(attributes ctxt) attrs
@@ -1974,7 +1994,7 @@ and constructor_declaration ctxt f (name, vars_jkinds, args, res, attrs) =
19741994
(fun f -> function
19751995
| Pcstr_tuple [] -> core_type1 ctxt f r
19761996
| Pcstr_tuple l -> pp f "%a@;->@;%a"
1977-
(list (maybe_modes_type core_type1 ctxt) ~sep:"@;*@;") l
1997+
(list (modalities_type core_type1 ctxt) ~sep:"@;*@;") l
19781998
(core_type1 ctxt) r
19791999
| Pcstr_record l ->
19802000
pp f "%a@;->@;%a" (record_declaration ctxt) l (core_type1 ctxt) r

0 commit comments

Comments
 (0)