Skip to content

Commit 92e38cb

Browse files
committed
Try and pass jsx_element from typed_tree to js_call
1 parent ffc7666 commit 92e38cb

26 files changed

+201
-72
lines changed

compiler/core/js_call_info.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ type call_info =
3333
{[ fun x y -> (f x y) === f ]} when [f] is an atom
3434
*)
3535

36-
type t = {call_info: call_info; arity: arity}
36+
type t = {
37+
call_info: call_info;
38+
arity: arity;
39+
call_transformed_jsx: Parsetree.jsx_element option;
40+
}
3741

38-
let dummy = {arity = NA; call_info = Call_na}
42+
let dummy = {arity = NA; call_info = Call_na; call_transformed_jsx = None}
3943

40-
let builtin_runtime_call = {arity = Full; call_info = Call_builtin_runtime}
44+
let builtin_runtime_call =
45+
{arity = Full; call_info = Call_builtin_runtime; call_transformed_jsx = None}
4146

42-
let ml_full_call = {arity = Full; call_info = Call_ml}
47+
let ml_full_call =
48+
{arity = Full; call_info = Call_ml; call_transformed_jsx = None}

compiler/core/js_call_info.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ type call_info =
3535
{[ fun x y -> f x y === f ]} when [f] is an atom
3636
*)
3737

38-
type t = {call_info: call_info; arity: arity}
38+
type t = {
39+
call_info: call_info;
40+
arity: arity;
41+
call_transformed_jsx: Parsetree.jsx_element option;
42+
}
3943

4044
val dummy : t
4145

compiler/core/js_dump.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
524524
when Ext_list.length_equal el i
525525
]}
526526
*)
527+
| Call (e, el, {call_transformed_jsx = Some jsx_element}) ->
528+
(* The grand point would be to reconstruct the JSX here *)
529+
P.string f "<meh />";
530+
cxt
527531
| Call (e, el, info) ->
528532
P.cond_paren_group f (level > 15) (fun _ ->
529533
P.group f 0 (fun _ ->

compiler/core/lam.ml

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ module Types = struct
8181
*)
8282
and prim_info = {primitive: Lam_primitive.t; args: t list; loc: Location.t}
8383

84-
and apply = {ap_func: t; ap_args: t list; ap_info: ap_info}
84+
and apply = {
85+
ap_func: t;
86+
ap_args: t list;
87+
ap_info: ap_info;
88+
ap_transformed_jsx: Parsetree.jsx_element option;
89+
}
8590

8691
and t =
8792
| Lvar of ident
@@ -121,7 +126,12 @@ module X = struct
121126
loc: Location.t;
122127
}
123128

124-
and apply = Types.apply = {ap_func: t; ap_args: t list; ap_info: ap_info}
129+
and apply = Types.apply = {
130+
ap_func: t;
131+
ap_args: t list;
132+
ap_info: ap_info;
133+
ap_transformed_jsx: Parsetree.jsx_element option;
134+
}
125135

126136
and lfunction = Types.lfunction = {
127137
arity: int;
@@ -159,10 +169,10 @@ include Types
159169
let inner_map (l : t) (f : t -> X.t) : X.t =
160170
match l with
161171
| Lvar (_ : ident) | Lconst (_ : Lam_constant.t) -> ((* Obj.magic *) l : X.t)
162-
| Lapply {ap_func; ap_args; ap_info} ->
172+
| Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx} ->
163173
let ap_func = f ap_func in
164174
let ap_args = Ext_list.map ap_args f in
165-
Lapply {ap_func; ap_args; ap_info}
175+
Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx}
166176
| Lfunction {body; arity; params; attr} ->
167177
let body = f body in
168178
Lfunction {body; arity; params; attr}
@@ -279,7 +289,7 @@ let rec is_eta_conversion_exn params inner_args outer_args : t list =
279289
| _, _, _ -> raise_notrace Not_simple_form
280290

281291
(** FIXME: more robust inlining check later, we should inline it before we add stub code*)
282-
let rec apply fn args (ap_info : ap_info) : t =
292+
let rec apply ?(ap_transformed_jsx = None) fn args (ap_info : ap_info) : t =
283293
match fn with
284294
| Lfunction
285295
{
@@ -300,15 +310,16 @@ let rec apply fn args (ap_info : ap_info) : t =
300310
Lprim
301311
{primitive = wrap; args = [Lprim {primitive_call with args; loc}]; loc}
302312
| exception Not_simple_form ->
303-
Lapply {ap_func = fn; ap_args = args; ap_info})
313+
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
304314
| Lfunction
305315
{
306316
params;
307317
body = Lprim ({primitive = _; args = inner_args} as primitive_call);
308318
} -> (
309319
match is_eta_conversion_exn params inner_args args with
310320
| args -> Lprim {primitive_call with args; loc = ap_info.ap_loc}
311-
| exception _ -> Lapply {ap_func = fn; ap_args = args; ap_info})
321+
| exception _ ->
322+
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
312323
| Lfunction
313324
{
314325
params;
@@ -321,17 +332,17 @@ let rec apply fn args (ap_info : ap_info) : t =
321332
| args ->
322333
Lsequence (Lprim {primitive_call with args; loc = ap_info.ap_loc}, const)
323334
| exception _ ->
324-
Lapply {ap_func = fn; ap_args = args; ap_info}
335+
Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
325336
(* | Lfunction {params;body} when Ext_list.same_length params args ->
326337
Ext_list.fold_right2 (fun p arg acc ->
327338
Llet(Strict,p,arg,acc)
328339
) params args body *)
329340
(* TODO: more rigirous analysis on [let_kind] *))
330341
| Llet (kind, id, e, (Lfunction _ as fn)) ->
331-
Llet (kind, id, e, apply fn args ap_info)
342+
Llet (kind, id, e, apply fn args ap_info ~ap_transformed_jsx)
332343
(* | Llet (kind0, id0, e0, Llet (kind,id, e, (Lfunction _ as fn))) ->
333344
Llet(kind0,id0,e0,Llet (kind, id, e, apply fn args loc status)) *)
334-
| _ -> Lapply {ap_func = fn; ap_args = args; ap_info}
345+
| _ -> Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
335346

336347
let rec eq_approx (l1 : t) (l2 : t) =
337348
match l1 with

compiler/core/lam.mli

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ type lambda_switch = {
4141
sw_names: Ast_untagged_variants.switch_names option;
4242
}
4343

44-
and apply = private {ap_func: t; ap_args: t list; ap_info: ap_info}
44+
and apply = private {
45+
ap_func: t;
46+
ap_args: t list;
47+
ap_info: ap_info;
48+
ap_transformed_jsx: Parsetree.jsx_element option;
49+
}
4550

4651
and lfunction = {
4752
arity: int;
@@ -103,7 +108,12 @@ val global_module : ?dynamic_import:bool -> ident -> t
103108

104109
val const : Lam_constant.t -> t
105110

106-
val apply : t -> t list -> ap_info -> t
111+
val apply :
112+
?ap_transformed_jsx:Parsetree.jsx_element option ->
113+
t ->
114+
t list ->
115+
ap_info ->
116+
t
107117

108118
val function_ :
109119
attr:Lambda.function_attribute ->

compiler/core/lam_bounded_vars.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ let rewrite (map : _ Hash_ident.t) (lam : Lam.t) : Lam.t =
108108
(* here it makes sure that global vars are not rebound *)
109109
Lam.prim ~primitive ~args:(Ext_list.map args aux) loc
110110
| Lglobal_module _ -> lam
111-
| Lapply {ap_func; ap_args; ap_info} ->
111+
| Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx} ->
112112
let fn = aux ap_func in
113113
let args = Ext_list.map ap_args aux in
114-
Lam.apply fn args ap_info
114+
Lam.apply ~ap_transformed_jsx fn args ap_info
115115
| Lswitch
116116
( l,
117117
{

compiler/core/lam_compile.ml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ let args_either_function_or_const (args : Lam.t list) =
3131
| Lfunction _ | Lconst _ -> true
3232
| _ -> false)
3333

34-
let call_info_of_ap_status (ap_status : Lam.apply_status) : Js_call_info.t =
34+
let call_info_of_ap_status call_transformed_jsx (ap_status : Lam.apply_status) :
35+
Js_call_info.t =
3536
(* XXX *)
3637
match ap_status with
37-
| App_infer_full -> {arity = Full; call_info = Call_ml}
38-
| App_uncurry -> {arity = Full; call_info = Call_na}
39-
| App_na -> {arity = NA; call_info = Call_ml}
38+
| App_infer_full -> {arity = Full; call_info = Call_ml; call_transformed_jsx}
39+
| App_uncurry -> {arity = Full; call_info = Call_na; call_transformed_jsx}
40+
| App_na -> {arity = NA; call_info = Call_ml; call_transformed_jsx}
4041

4142
let rec apply_with_arity_aux (fn : J.expression) (arity : int list)
4243
(args : E.t list) (len : int) : E.t =
@@ -49,7 +50,14 @@ let rec apply_with_arity_aux (fn : J.expression) (arity : int list)
4950
if len >= x then
5051
let first_part, continue = Ext_list.split_at args x in
5152
apply_with_arity_aux
52-
(E.call ~info:{arity = Full; call_info = Call_ml} fn first_part)
53+
(E.call
54+
~info:
55+
{
56+
arity = Full;
57+
call_info = Call_ml;
58+
(* no clue if this is correct *) call_transformed_jsx = None;
59+
}
60+
fn first_part)
5361
rest continue (len - x)
5462
else if
5563
(* GPR #1423 *)
@@ -63,7 +71,13 @@ let rec apply_with_arity_aux (fn : J.expression) (arity : int list)
6371
[
6472
S.return_stmt
6573
(E.call
66-
~info:{arity = Full; call_info = Call_ml}
74+
~info:
75+
{
76+
arity = Full;
77+
call_info = Call_ml;
78+
(* no clue if this is correct *) call_transformed_jsx =
79+
None;
80+
}
6781
fn
6882
(Ext_list.append args @@ Ext_list.map params E.var));
6983
]
@@ -306,7 +320,9 @@ let compile output_prefix =
306320
let expression =
307321
match appinfo.ap_info.ap_status with
308322
| (App_infer_full | App_uncurry) as ap_status ->
309-
E.call ~info:(call_info_of_ap_status ap_status) fn args
323+
E.call
324+
~info:(call_info_of_ap_status appinfo.ap_transformed_jsx ap_status)
325+
fn args
310326
| App_na -> (
311327
match ident_info.arity with
312328
| Submodule _ | Single Arity_na ->
@@ -1439,14 +1455,17 @@ let compile output_prefix =
14391455
ap_func =
14401456
Lapply {ap_func; ap_args; ap_info = {ap_status = App_na; ap_inlined}};
14411457
ap_info = {ap_status = App_na} as outer_ap_info;
1458+
ap_transformed_jsx;
14421459
} ->
14431460
(* After inlining, we can generate such code, see {!Ari_regress_test}*)
14441461
let ap_info =
14451462
if outer_ap_info.ap_inlined = ap_inlined then outer_ap_info
14461463
else {outer_ap_info with ap_inlined}
14471464
in
14481465
compile_lambda lambda_cxt
1449-
(Lam.apply ap_func (Ext_list.append ap_args appinfo.ap_args) ap_info)
1466+
(Lam.apply ap_func
1467+
(Ext_list.append ap_args appinfo.ap_args)
1468+
ap_info ~ap_transformed_jsx)
14501469
(* External function call: it can not be tailcall in this case*)
14511470
| {
14521471
ap_func =
@@ -1529,7 +1548,9 @@ let compile output_prefix =
15291548
Js_output.output_of_block_and_expression lambda_cxt.continuation
15301549
args_code
15311550
(E.call
1532-
~info:(call_info_of_ap_status appinfo.ap_info.ap_status)
1551+
~info:
1552+
(call_info_of_ap_status appinfo.ap_transformed_jsx
1553+
appinfo.ap_info.ap_status)
15331554
fn_code args))
15341555
and compile_prim (prim_info : Lam.prim_info)
15351556
(lambda_cxt : Lam_compile_context.t) =

compiler/core/lam_compile_external_call.ml

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
287287
| _ ->
288288
let args, eff, dynamic = assemble_args_has_splice arg_types args in
289289
let args = if dynamic then E.variadic_args args else args in
290-
add_eff eff (E.call ~info:{arity = Full; call_info = Call_na} fn args))
290+
add_eff eff
291+
(E.call
292+
~info:
293+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
294+
fn args))
291295
| Js_call
292296
{
293297
external_module_name = module_name;
@@ -302,20 +306,36 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
302306
if splice then
303307
let args, eff, dynamic = assemble_args_has_splice arg_types args in
304308
let args = if dynamic then E.variadic_args args else args in
305-
add_eff eff (E.call ~info:{arity = Full; call_info = Call_na} fn args)
309+
add_eff eff
310+
(E.call
311+
~info:
312+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
313+
fn args)
306314
else
307315
let args, eff = assemble_args_no_splice arg_types args in
308-
add_eff eff @@ E.call ~info:{arity = Full; call_info = Call_na} fn args
316+
add_eff eff
317+
@@ E.call
318+
~info:
319+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
320+
fn args
309321
| Js_module_as_fn {external_module_name; splice} ->
310322
let fn = external_var external_module_name ~dynamic_import in
311323
if splice then
312324
let args, eff, dynamic = assemble_args_has_splice arg_types args in
313325
let args = if dynamic then E.variadic_args args else args in
314-
add_eff eff (E.call ~info:{arity = Full; call_info = Call_na} fn args)
326+
add_eff eff
327+
(E.call
328+
~info:
329+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
330+
fn args)
315331
else
316332
let args, eff = assemble_args_no_splice arg_types args in
317333
(* TODO: fix in rest calling convention *)
318-
add_eff eff (E.call ~info:{arity = Full; call_info = Call_na} fn args)
334+
add_eff eff
335+
(E.call
336+
~info:
337+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
338+
fn args)
319339
| Js_new {external_module_name = module_name; name = fn; splice; scopes} ->
320340
(* handle [@@new]*)
321341
(* This has some side effect, it will
@@ -362,14 +382,16 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
362382
add_eff eff
363383
(let self = translate_scoped_access js_send_scopes self in
364384
E.call
365-
~info:{arity = Full; call_info = Call_na}
385+
~info:
386+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
366387
(E.dot self name) args)
367388
else
368389
let args, eff = assemble_args_no_splice arg_types args in
369390
add_eff eff
370391
(let self = translate_scoped_access js_send_scopes self in
371392
E.call
372-
~info:{arity = Full; call_info = Call_na}
393+
~info:
394+
{arity = Full; call_info = Call_na; call_transformed_jsx = None}
373395
(E.dot self name) args)
374396
| _ -> assert false)
375397
| Js_module_as_var module_name -> external_var module_name ~dynamic_import
@@ -384,7 +406,10 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
384406
~dynamic_import
385407
in
386408
if args = [] then e
387-
else E.call ~info:{arity = Full; call_info = Call_na} e args
409+
else
410+
E.call
411+
~info:{arity = Full; call_info = Call_na; call_transformed_jsx = None}
412+
e args
388413
| Js_module_as_class module_name ->
389414
let fn = external_var module_name ~dynamic_import in
390415
let args, eff = assemble_args_no_splice arg_types args in

compiler/core/lam_compile_primitive.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ let get_module_system () =
5656

5757
let import_of_path path =
5858
E.call
59-
~info:{arity = Full; call_info = Call_na}
59+
~info:{arity = Full; call_info = Call_na; call_transformed_jsx = None}
6060
(E.js_global "import")
6161
[E.str path]
6262

6363
let wrap_then import value =
6464
let arg = Ident.create "m" in
6565
E.call
66-
~info:{arity = Full; call_info = Call_na}
66+
~info:{arity = Full; call_info = Call_na; call_transformed_jsx = None}
6767
(E.dot import "then")
6868
[
6969
E.ocaml_fun ~return_unit:false ~async:false ~one_unit_arg:false [arg]
@@ -88,7 +88,10 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
8888
| _ -> assert false)
8989
| Pjs_apply -> (
9090
match args with
91-
| fn :: rest -> E.call ~info:{arity = Full; call_info = Call_na} fn rest
91+
| fn :: rest ->
92+
E.call
93+
~info:{arity = Full; call_info = Call_na; call_transformed_jsx = None}
94+
fn rest
9295
| _ -> assert false)
9396
| Pnull_to_opt -> (
9497
match args with

0 commit comments

Comments
 (0)