Skip to content

Commit 1fffef8

Browse files
committed
restore #2540
1 parent bca81e9 commit 1fffef8

27 files changed

+489
-40
lines changed

chamelon/compat.jst.ml

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ open Mode
44

55
let dummy_jkind = Jkind.Primitive.value ~why:(Unknown "dummy_layout")
66
let dummy_value_mode = Value.disallow_right Value.legacy
7+
8+
let dummy_alloc_mode =
9+
{ mode = Alloc.disallow_left Alloc.legacy; closure_context = None }
10+
711
let mkTvar name = Tvar { name; jkind = dummy_jkind }
812

913
let mkTarrow (label, t1, t2, comm) =
@@ -29,21 +33,20 @@ let mkTexp_apply
2933
in
3034
Texp_apply (exp, args, pos, mode, za)
3135

32-
type texp_tuple_identifier = string option list * Alloc.r
36+
type texp_tuple_identifier = string option list * alloc_mode
3337

3438
let mkTexp_tuple ?id exps =
3539
let labels, alloc =
3640
match id with
37-
| None -> (List.map (fun _ -> None) exps, Alloc.disallow_left Alloc.legacy)
41+
| None -> (List.map (fun _ -> None) exps, dummy_alloc_mode)
3842
| Some id -> id
3943
in
4044
let exps = List.combine labels exps in
4145
Texp_tuple (exps, alloc)
4246

43-
type texp_construct_identifier = Alloc.r option
47+
type texp_construct_identifier = alloc_mode option
4448

45-
let mkTexp_construct ?id:(mode = Some (Alloc.disallow_left Alloc.legacy))
46-
(name, desc, args) =
49+
let mkTexp_construct ?id:(mode = Some dummy_alloc_mode) (name, desc, args) =
4750
Texp_construct (name, desc, args, mode)
4851

4952
type texp_function_param_identifier = {
@@ -86,7 +89,7 @@ type texp_function = {
8689
}
8790

8891
type texp_function_identifier = {
89-
alloc_mode : Alloc.r;
92+
alloc_mode : alloc_mode;
9093
ret_sort : Jkind.sort;
9194
ret_mode : Alloc.l;
9295
zero_alloc : Zero_alloc.t;
@@ -112,7 +115,7 @@ let texp_function_param_identifier_defaults =
112115

113116
let texp_function_defaults =
114117
{
115-
alloc_mode = Alloc.disallow_left Alloc.legacy;
118+
alloc_mode = dummy_alloc_mode;
116119
ret_sort = Jkind.Sort.value;
117120
ret_mode = Alloc.disallow_right Alloc.legacy;
118121
zero_alloc = Zero_alloc.default;

ocaml/boot/menhir/parser.mli

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type token =
1919
| STRUCT
2020
| STRING of (string * Location.t * string option)
2121
| STAR
22+
| STACK
2223
| SIG
2324
| SEMISEMI
2425
| SEMI

ocaml/lambda/translcore.ml

+12-12
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ let fuse_method_arity (parent : fusable_function) : fusable_function =
290290
(function (Texp_poly _, _, _) -> true | _ -> false)
291291
exp_extra
292292
->
293-
begin match transl_alloc_mode_r method_.alloc_mode with
293+
begin match transl_alloc_mode method_.alloc_mode with
294294
| Alloc_heap -> ()
295295
| Alloc_local ->
296296
(* If we support locally-allocated objects, we'll also have to
@@ -506,7 +506,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
506506
Lconst(Const_block(0, List.map extract_constant ll))
507507
with Not_constant ->
508508
Lprim(Pmakeblock(0, Immutable, Some shape,
509-
transl_alloc_mode_r alloc_mode),
509+
transl_alloc_mode alloc_mode),
510510
ll,
511511
(of_location ~scopes e.exp_loc))
512512
end
@@ -552,7 +552,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
552552
begin match constant with
553553
| Some constant -> Lconst constant
554554
| None ->
555-
let alloc_mode = transl_alloc_mode_r (Option.get alloc_mode) in
555+
let alloc_mode = transl_alloc_mode (Option.get alloc_mode) in
556556
let makeblock =
557557
match cstr.cstr_shape with
558558
| Constructor_uniform_value ->
@@ -578,7 +578,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
578578
that out by checking that the sort list is empty *)
579579
lam)
580580
else
581-
let alloc_mode = transl_alloc_mode_r (Option.get alloc_mode) in
581+
let alloc_mode = transl_alloc_mode (Option.get alloc_mode) in
582582
let makeblock =
583583
match cstr.cstr_shape with
584584
| Constructor_uniform_value ->
@@ -613,13 +613,13 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
613613
extract_constant lam]))
614614
with Not_constant ->
615615
Lprim(Pmakeblock(0, Immutable, None,
616-
transl_alloc_mode_r alloc_mode),
616+
transl_alloc_mode alloc_mode),
617617
[Lconst(const_int tag); lam],
618618
of_location ~scopes e.exp_loc)
619619
end
620620
| Texp_record {fields; representation; extended_expression; alloc_mode} ->
621621
transl_record ~scopes e.exp_loc e.exp_env
622-
(Option.map transl_alloc_mode_r alloc_mode)
622+
(Option.map transl_alloc_mode alloc_mode)
623623
fields representation extended_expression
624624
| Texp_field(arg, id, lbl, float) ->
625625
let targ = transl_exp ~scopes Jkind.Sort.for_record arg in
@@ -640,7 +640,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
640640
| Boxing (alloc_mode, _) -> alloc_mode
641641
| Non_boxing _ -> assert false
642642
in
643-
let mode = transl_alloc_mode_r alloc_mode in
643+
let mode = transl_alloc_mode alloc_mode in
644644
Lprim (Pfloatfield (lbl.lbl_pos, sem, mode), [targ],
645645
of_location ~scopes e.exp_loc)
646646
| Record_ufloat ->
@@ -667,7 +667,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
667667
| Float_boxed ->
668668
(match float with
669669
| Boxing (mode, _) ->
670-
flat_read_float_boxed (transl_alloc_mode_r mode)
670+
flat_read_float_boxed (transl_alloc_mode mode)
671671
| Non_boxing _ ->
672672
Misc.fatal_error
673673
"expected typechecking to make [float] boxing mode\
@@ -729,7 +729,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
729729
transl_exp ~scopes lbl_sort newval],
730730
of_location ~scopes e.exp_loc)
731731
| Texp_array (amut, element_sort, expr_list, alloc_mode) ->
732-
let mode = transl_alloc_mode_r alloc_mode in
732+
let mode = transl_alloc_mode alloc_mode in
733733
let kind = array_kind e element_sort in
734734
let ll =
735735
transl_list ~scopes
@@ -1642,7 +1642,7 @@ and transl_function ~in_new_scope ~scopes e params body
16421642
~alloc_mode ~ret_mode:sreturn_mode ~ret_sort:sreturn_sort ~region:sregion
16431643
~zero_alloc =
16441644
let attrs = e.exp_attributes in
1645-
let mode = transl_alloc_mode_r alloc_mode in
1645+
let mode = transl_alloc_mode alloc_mode in
16461646
let zero_alloc = Zero_alloc.get zero_alloc in
16471647
let assume_zero_alloc =
16481648
match zero_alloc with
@@ -2091,7 +2091,7 @@ and transl_match ~scopes ~arg_sort ~return_sort e arg pat_expr_list partial =
20912091
match arg, exn_cases with
20922092
| {exp_desc = Texp_tuple (argl, alloc_mode)}, [] ->
20932093
assert (static_handlers = []);
2094-
let mode = transl_alloc_mode_r alloc_mode in
2094+
let mode = transl_alloc_mode alloc_mode in
20952095
let argl =
20962096
List.map (fun (_, a) -> (a, Jkind.Sort.for_tuple_element)) argl
20972097
in
@@ -2110,7 +2110,7 @@ and transl_match ~scopes ~arg_sort ~return_sort e arg pat_expr_list partial =
21102110
argl
21112111
|> List.split
21122112
in
2113-
let mode = transl_alloc_mode_r alloc_mode in
2113+
let mode = transl_alloc_mode alloc_mode in
21142114
static_catch (transl_list ~scopes argl) val_ids
21152115
(Matching.for_multiple_match ~scopes ~return_layout e.exp_loc
21162116
lvars mode val_cases partial)

ocaml/lambda/translmode.ml

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ let transl_alloc_mode_r mode =
3535
(* we only take the locality axis *)
3636
Alloc.proj (Comonadic Areality) mode |> transl_locality_mode_r
3737

38+
let transl_alloc_mode (mode : Typedtree.alloc_mode) =
39+
transl_alloc_mode_r mode.mode
40+
3841
let transl_modify_mode locality =
3942
match Locality.zap_to_floor locality with
4043
| Global -> modify_heap

ocaml/lambda/translmode.mli

+2
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ val transl_alloc_mode_l : (allowed * 'r) Alloc.t -> Lambda.alloc_mode
2020

2121
val transl_alloc_mode_r : ('l * allowed) Alloc.t -> Lambda.alloc_mode
2222

23+
val transl_alloc_mode : Typedtree.alloc_mode -> Lambda.alloc_mode
24+
2325
val transl_modify_mode : (allowed * 'r) Locality.t -> Lambda.modify_mode

ocaml/parsing/ast_helper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ module Exp = struct
222222
mk ?loc ?attrs (Pexp_letop {let_; ands; body})
223223
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
224224
let unreachable ?loc ?attrs () = mk ?loc ?attrs Pexp_unreachable
225+
let stack ?loc ?attrs e = mk ?loc ?attrs (Pexp_stack e)
225226

226227
let case lhs ?guard rhs =
227228
{

ocaml/parsing/ast_helper.mli

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ module Exp:
191191
-> binding_op list -> expression -> expression
192192
val extension: ?loc:loc -> ?attrs:attrs -> extension -> expression
193193
val unreachable: ?loc:loc -> ?attrs:attrs -> unit -> expression
194+
val stack : ?loc:loc -> ?attrs:attrs -> expression -> expression
194195

195196
val case: pattern -> ?guard:expression -> expression -> case
196197
val binding_op: str -> pattern -> expression -> loc -> binding_op

ocaml/parsing/ast_iterator.ml

+1
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ module E = struct
632632
sub.expr sub body
633633
| Pexp_extension x -> sub.extension sub x
634634
| Pexp_unreachable -> ()
635+
| Pexp_stack e -> sub.expr sub e
635636

636637
let iter_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_loc} =
637638
iter_loc sub pbop_op;

ocaml/parsing/ast_mapper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ module E = struct
746746
(List.map (sub.binding_op sub) ands) (sub.expr sub body)
747747
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
748748
| Pexp_unreachable -> unreachable ~loc ~attrs ()
749+
| Pexp_stack e -> stack ~loc ~attrs (sub.expr sub e)
749750

750751
let map_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_loc} =
751752
let open Exp in

ocaml/parsing/depend.ml

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ let rec add_expr bv exp =
330330
| Ok { arg; _ } -> add_expr bv arg
331331
end
332332
| Pexp_extension e -> handle_extension e
333+
| Pexp_stack e -> add_expr bv e
333334
| Pexp_unreachable -> ()
334335

335336
and add_expr_jane_syntax bv : Jane_syntax.Expression.t -> _ = function

ocaml/parsing/lexer.mll

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let keyword_table =
8484
"private", PRIVATE;
8585
"rec", REC;
8686
"sig", SIG;
87+
"stack_", STACK;
8788
"struct", STRUCT;
8889
"then", THEN;
8990
"to", TO;

ocaml/parsing/parser.mly

+4-1
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ let unboxed_type sloc lident tys =
10471047
%token HASH_SUFFIX "# "
10481048
%token <string> HASHOP "##" (* just an example *)
10491049
%token SIG "sig"
1050+
%token STACK "stack_"
10501051
%token STAR "*"
10511052
%token <string * Location.t * string option>
10521053
STRING "\"hello\"" (* just an example *)
@@ -1138,7 +1139,7 @@ The precedences must be listed from low to high.
11381139
%nonassoc BACKQUOTE BANG BEGIN CHAR FALSE FLOAT HASH_FLOAT INT HASH_INT OBJECT
11391140
LBRACE LBRACELESS LBRACKET LBRACKETBAR LBRACKETCOLON LIDENT LPAREN
11401141
NEW PREFIXOP STRING TRUE UIDENT
1141-
LBRACKETPERCENT QUOTED_STRING_EXPR
1142+
LBRACKETPERCENT QUOTED_STRING_EXPR STACK
11421143

11431144

11441145
/* Entry points */
@@ -2867,6 +2868,8 @@ fun_expr:
28672868
%inline expr_:
28682869
| simple_expr nonempty_llist(labeled_simple_expr)
28692870
{ mkexp ~loc:$sloc (Pexp_apply($1, $2)) }
2871+
| STACK simple_expr
2872+
{ mkexp ~loc:$sloc (Pexp_stack $2) }
28702873
| labeled_tuple %prec below_COMMA
28712874
{ pexp_ltuple $sloc $1 }
28722875
| mkrhs(constr_longident) simple_expr %prec below_HASH

ocaml/parsing/parsetree.mli

+1
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ and expression_desc =
436436
- [let* P0 = E00 and* P1 = E01 in E1] *)
437437
| Pexp_extension of extension (** [[%id]] *)
438438
| Pexp_unreachable (** [.] *)
439+
| Pexp_stack of expression (** stack_ exp *)
439440

440441
and case =
441442
{

ocaml/parsing/pprintast.ml

+3
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ and expression ?(jane_syntax_parens = false) ctxt f x =
995995
end (e,l)
996996
end
997997

998+
| Pexp_stack e ->
999+
(* Similar to the common case of [Pexp_apply] *)
1000+
pp f "@[<hov2>stack_@ %a@]" (expression2 reset_ctxt) e
9981001
| Pexp_construct (li, Some eo)
9991002
when not (is_simple_construct (view_expr x))-> (* Not efficient FIXME*)
10001003
(match view_expr x with

ocaml/parsing/printast.ml

+3
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ and expression i ppf x =
398398
payload i ppf arg
399399
| Pexp_unreachable ->
400400
line i ppf "Pexp_unreachable"
401+
| Pexp_stack e ->
402+
line i ppf "Pexp_stack\n";
403+
expression i ppf e
401404

402405
and jkind_annotation i ppf (jkind : jkind_annotation) =
403406
match jkind with

ocaml/testsuite/tests/language-extensions/pprintast_unconditional.ml

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module Example = struct
3737
end"
3838

3939
let local_exp = parse expression "let x = foo (local_ x) in local_ y"
40+
let stack_exp = parse expression
41+
"let x = stack_ 42 in \
42+
let y = stack_ (f x) in \
43+
let z = foo (stack_ 42) in \
44+
foo (stack_ (f x))"
4045
let fun_with_modes_on_arg = parse expression
4146
"let f (a @ local) ~(b @ local) ?(c @ local) \
4247
?(d @ local = 1) ~e:(e @ local) ?f:(f @ local = 2) \
@@ -186,6 +191,7 @@ end = struct
186191
let modality_val = test "modality_val" module_type Example.modality_val
187192

188193
let local_exp = test "local_exp" expression Example.local_exp
194+
let stack_exp = test "stack_exp" expression Example.stack_exp
189195
let fun_with_modes_on_arg = test "fun_with_modes_on_arg" expression Example.fun_with_modes_on_arg
190196

191197
let longident = test "longident" longident Example.longident

ocaml/testsuite/tests/language-extensions/pprintast_unconditional.reference

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ fun_with_modes_on_arg:
3030
?f:(local_ f= 2) () = () in
3131
f
3232

33+
stack_exp:
34+
let x = stack_ 42 in
35+
let y = stack_ (f x) in let z = foo (stack_ 42) in foo (stack_ (f x))
36+
3337
longident: No.Longidents.Require.extensions
3438

3539
expression: [x for x = 1 to 10]
@@ -144,6 +148,10 @@ fun_with_modes_on_arg:
144148
?f:(local_ f= 2) () = () in
145149
f
146150

151+
stack_exp:
152+
let x = stack_ 42 in
153+
let y = stack_ (f x) in let z = foo (stack_ 42) in foo (stack_ (f x))
154+
147155
longident: No.Longidents.Require.extensions
148156

149157
expression: [x for x = 1 to 10]

0 commit comments

Comments
 (0)