@@ -31,7 +31,7 @@ type ap_info = {
3131 ap_status : apply_status ;
3232}
3333
34- module Types = struct
34+ module Lam_types = struct
3535 type lambda_switch = {
3636 sw_consts_full : bool ;
3737 (* TODO: refine its representation *)
@@ -47,6 +47,7 @@ module Types = struct
4747 params : ident list ;
4848 body : t ;
4949 attr : Lambda .function_attribute ;
50+ ty : Types .type_expr option ;
5051 }
5152
5253 (*
@@ -86,6 +87,7 @@ module Types = struct
8687 ap_args : t list ;
8788 ap_info : ap_info ;
8889 ap_transformed_jsx : bool ;
90+ ap_result_type : Types .type_expr option ;
8991 }
9092
9193 and t =
@@ -94,7 +96,7 @@ module Types = struct
9496 | Lconst of Lam_constant. t
9597 | Lapply of apply
9698 | Lfunction of lfunction
97- | Llet of Lam_compat. let_kind * ident * t * t
99+ | Llet of Lam_compat. let_kind * ident * Types. type_expr option * t * t
98100 | Lletrec of (ident * t) list * t
99101 | Lprim of prim_info
100102 | Lswitch of t * lambda_switch
@@ -115,7 +117,7 @@ module Types = struct
115117end
116118
117119module X = struct
118- type lambda_switch = Types .lambda_switch = {
120+ type lambda_switch = Lam_types .lambda_switch = {
119121 sw_consts_full : bool ;
120122 sw_consts : (int * t ) list ;
121123 sw_blocks_full : bool ;
@@ -124,33 +126,35 @@ module X = struct
124126 sw_names : Ast_untagged_variants .switch_names option ;
125127 }
126128
127- and prim_info = Types . prim_info = {
129+ and prim_info = Lam_types . prim_info = {
128130 primitive : Lam_primitive .t ;
129131 args : t list ;
130132 loc : Location .t ;
131133 }
132134
133- and apply = Types . apply = {
135+ and apply = Lam_types . apply = {
134136 ap_func : t ;
135137 ap_args : t list ;
136138 ap_info : ap_info ;
137139 ap_transformed_jsx : bool ;
140+ ap_result_type : Types .type_expr option ;
138141 }
139142
140- and lfunction = Types . lfunction = {
143+ and lfunction = Lam_types . lfunction = {
141144 arity : int ;
142145 params : ident list ;
143146 body : t ;
144147 attr : Lambda .function_attribute ;
148+ ty : Types .type_expr option ;
145149 }
146150
147- and t = Types . t =
151+ and t = Lam_types . t =
148152 | Lvar of ident
149153 | Lglobal_module of ident * bool
150154 | Lconst of Lam_constant. t
151155 | Lapply of apply
152156 | Lfunction of lfunction
153- | Llet of Lam_compat. let_kind * ident * t * t
157+ | Llet of Lam_compat. let_kind * ident * Types. type_expr option * t * t
154158 | Lletrec of (ident * t) list * t
155159 | Lprim of prim_info
156160 | Lswitch of t * lambda_switch
@@ -170,24 +174,24 @@ module X = struct
170174 (* | Lsend of Lam_compat.meth_kind * t * t * t list * Location.t *)
171175end
172176
173- include Types
177+ include Lam_types
174178
175179(* * apply [f] to direct successor which has type [Lam.t] *)
176180
177181let inner_map (l : t ) (f : t -> X.t ) : X.t =
178182 match l with
179183 | Lvar (_ : ident ) | Lconst (_ : Lam_constant.t ) -> ((* Obj.magic *) l : X. t)
180- | Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx} ->
184+ | Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx; ap_result_type } ->
181185 let ap_func = f ap_func in
182186 let ap_args = Ext_list. map ap_args f in
183- Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx}
184- | Lfunction {body; arity; params; attr} ->
187+ Lapply {ap_func; ap_args; ap_info; ap_transformed_jsx; ap_result_type }
188+ | Lfunction {body; arity; params; attr; ty } ->
185189 let body = f body in
186- Lfunction {body; arity; params; attr}
187- | Llet (str , id , arg , body ) ->
190+ Lfunction {body; arity; params; attr; ty }
191+ | Llet (str , id , ty , arg , body ) ->
188192 let arg = f arg in
189193 let body = f body in
190- Llet (str, id, arg, body)
194+ Llet (str, id, ty, arg, body)
191195 | Lletrec (decl , body ) ->
192196 let body = f body in
193197 let decl = Ext_list. map_snd decl f in
@@ -307,7 +311,8 @@ let rec is_eta_conversion_exn params inner_args outer_args : t list =
307311 | _ , _ , _ -> raise_notrace Not_simple_form
308312
309313(* * FIXME: more robust inlining check later, we should inline it before we add stub code*)
310- let rec apply ?(ap_transformed_jsx = false ) fn args (ap_info : ap_info ) : t =
314+ let rec apply ?(ap_transformed_jsx = false )
315+ ~(ap_result_type : Types.type_expr option ) fn args (ap_info : ap_info ) : t =
311316 match fn with
312317 | Lfunction
313318 {
@@ -328,7 +333,14 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
328333 Lprim
329334 {primitive = wrap; args = [Lprim {primitive_call with args; loc}]; loc}
330335 | exception Not_simple_form ->
331- Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
336+ Lapply
337+ {
338+ ap_func = fn;
339+ ap_args = args;
340+ ap_info;
341+ ap_transformed_jsx;
342+ ap_result_type;
343+ })
332344 | Lfunction
333345 {
334346 params;
@@ -337,7 +349,14 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
337349 match is_eta_conversion_exn params inner_args args with
338350 | args -> Lprim {primitive_call with args; loc = ap_info.ap_loc}
339351 | exception _ ->
340- Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx})
352+ Lapply
353+ {
354+ ap_func = fn;
355+ ap_args = args;
356+ ap_info;
357+ ap_transformed_jsx;
358+ ap_result_type;
359+ })
341360 | Lfunction
342361 {
343362 params;
@@ -350,17 +369,37 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
350369 | args ->
351370 Lsequence (Lprim {primitive_call with args; loc = ap_info.ap_loc}, const)
352371 | exception _ ->
353- Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
372+ Lapply
373+ {
374+ ap_func = fn;
375+ ap_args = args;
376+ ap_info;
377+ ap_transformed_jsx;
378+ ap_result_type;
379+ }
354380 (* | Lfunction {params;body} when Ext_list.same_length params args ->
355381 Ext_list.fold_right2 (fun p arg acc ->
356382 Llet(Strict,p,arg,acc)
357383 ) params args body *)
358384 (* TODO: more rigirous analysis on [let_kind] *) )
359- | Llet (kind , id , e , (Lfunction _ as fn )) ->
360- Llet (kind, id, e, apply fn args ap_info ~ap_transformed_jsx )
385+ | Llet (kind , id , ty , e , (Lfunction _ as fn )) ->
386+ Llet
387+ ( kind,
388+ id,
389+ ty,
390+ e,
391+ apply fn args ap_info ~ap_transformed_jsx ~ap_result_type )
361392 (* | Llet (kind0, id0, e0, Llet (kind,id, e, (Lfunction _ as fn))) ->
362393 Llet(kind0,id0,e0,Llet (kind, id, e, apply fn args loc status)) *)
363- | _ -> Lapply {ap_func = fn; ap_args = args; ap_info; ap_transformed_jsx}
394+ | _ ->
395+ Lapply
396+ {
397+ ap_func = fn;
398+ ap_args = args;
399+ ap_info;
400+ ap_transformed_jsx;
401+ ap_result_type;
402+ }
364403
365404let rec eq_approx (l1 : t ) (l2 : t ) =
366405 match l1 with
@@ -419,7 +458,7 @@ let rec eq_approx (l1 : t) (l2 : t) =
419458 (fun ((k : string ), v ) (k2 , v2 ) -> k = k2 && eq_approx v v2)
420459 | _ -> false )
421460 | Lfunction _
422- | Llet (_, _, _, _)
461+ | Llet (_, _, _, _, _ )
423462 | Lletrec _ | Lswitch _ | Lstaticcatch _ | Ltrywith _
424463 | Lfor (_, _, _, _, _)
425464 | Lfor_of (_, _, _)
@@ -475,10 +514,10 @@ let global_module ?(dynamic_import = false) id =
475514 Lglobal_module (id, dynamic_import)
476515let const ct : t = Lconst ct
477516
478- let function_ ~attr ~arity ~params ~body : t =
479- Lfunction {arity; params; body; attr}
517+ let function_ ~attr ~arity ~params ~body ~ ty : t =
518+ Lfunction {arity; params; body; attr; ty }
480519
481- let let_ kind id e body : t = Llet (kind, id, e, body)
520+ let let_ kind id ty e body : t = Llet (kind, id, ty , e, body)
482521let letrec bindings body : t = Lletrec (bindings, body)
483522let while_ a b : t = Lwhile (a, b)
484523let try_ body id handler : t = Ltrywith (body, id, handler)
0 commit comments