@@ -93,6 +93,38 @@ type default_case =
9393let no_effects_const = lazy true
9494let has_effects_const = lazy false
9595
96+ let names_from_construct_pattern (pat : Typedtree.pattern ) =
97+ let names_from_type_variant cstrs =
98+ let (consts, blocks) = List. fold_left
99+ (fun (consts , blocks ) cstr ->
100+ if cstr.Types. cd_args = []
101+ then (Ident. name cstr.Types. cd_id :: consts, blocks)
102+ else (consts, Ident. name cstr.Types. cd_id :: blocks))
103+ ([] , [] ) cstrs in
104+ Some {Lambda. consts = consts |> List. rev |> Array. of_list;
105+ blocks = blocks |> List. rev |> Array. of_list } in
106+
107+ let rec resolve_path n path =
108+ match Env. find_type path pat.pat_env with
109+ | {type_kind = Type_variant cstrs } ->
110+ names_from_type_variant cstrs
111+ | {type_kind = Type_abstract ; type_manifest = Some t } ->
112+ ( match (Ctype. unalias t).desc with
113+ | Tconstr (pathn , _ , _ ) ->
114+ (* Format.eprintf "XXX path%d:%s path%d:%s@." n (Path.name path) (n+1) (Path.name pathn); *)
115+ resolve_path (n+ 1 ) pathn
116+ | _ -> None )
117+ | {type_kind = Type_abstract ; type_manifest = None } ->
118+ None
119+ | {type_kind = Type_record _ | Type_open (* Exceptions *) } ->
120+ None in
121+
122+ match (Btype. repr pat.pat_type).desc with
123+ | Tconstr (path , _ , _ ) -> resolve_path 0 path
124+ | _ -> assert false
125+
126+ let () = Matching. names_from_construct_pattern := names_from_construct_pattern
127+
96128(* * We drop the ability of cross-compiling
97129 the compiler has to be the same running
98130*)
@@ -412,6 +444,7 @@ and compile_recursive_lets cxt id_args : Js_output.t =
412444and compile_general_cases
413445 :
414446 'a .
447+ ('a -> string option ) ->
415448 ('a -> J. expression ) ->
416449 (J. expression -> J. expression -> J. expression ) ->
417450 Lam_compile_context. t ->
@@ -421,6 +454,7 @@ and compile_general_cases
421454 _ ->
422455 ('a * Lam. t ) list -> default_case -> J. block
423456 = fun
457+ (make_comment : _ -> string option )
424458 (make_exp : _ -> J.expression )
425459 (eq_exp : J.expression -> J.expression -> J.expression )
426460 (cxt : Lam_compile_context.t )
@@ -499,10 +533,11 @@ and compile_general_cases
499533 should_break && Lam_exit_code. has_exit lam in
500534 {J. switch_case ;
501535 switch_body;
502- should_break
536+ should_break;
537+ comment = make_comment switch_case;
503538 }
504539 else
505- { switch_case; switch_body = [] ; should_break = false }
540+ { switch_case; switch_body = [] ; should_break = false ; comment = make_comment switch_case; }
506541 )
507542
508543 (* TODO: we should also group default *)
@@ -512,9 +547,10 @@ and compile_general_cases
512547 [switch ?default ?declaration switch_exp body]
513548 )
514549
515- and compile_cases cxt switch_exp table default =
550+ and compile_cases cxt switch_exp table default get_name =
516551 compile_general_cases
517- E. small_int
552+ get_name
553+ (fun i -> {(E. small_int i) with comment = get_name i})
518554 E. int_equal
519555 cxt
520556 (fun ?default ?declaration e clauses ->
@@ -534,7 +570,8 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
534570 sw_consts;
535571 sw_numblocks;
536572 sw_blocks;
537- sw_failaction } : Lam. switch) = sw in
573+ sw_failaction;
574+ sw_names } : Lam. lambda_switch) = sw in
538575 let sw_num_default =
539576 match sw_failaction with
540577 | None -> Complete
@@ -549,6 +586,11 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
549586 if sw_numblocks
550587 then Complete
551588 else Default x in
589+ let get_name is_const i =
590+ match sw_names with
591+ | None -> None
592+ | Some {blocks; consts} ->
593+ Some (if is_const then consts.(i) else blocks.(i)) in
552594 let compile_whole (cxt : Lam_compile_context.t ) =
553595 match compile_lambda
554596 {cxt with continuation = NeedValue Not_tail }
@@ -558,20 +600,20 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
558600 | { block; value = Some e } ->
559601 block @
560602 (if sw_numconsts && sw_consts = [] then
561- compile_cases cxt (E. tag e) sw_blocks sw_blocks_default
603+ compile_cases cxt (E. tag e) sw_blocks sw_blocks_default (get_name false )
562604 else if sw_numblocks && sw_blocks = [] then
563- compile_cases cxt e sw_consts sw_num_default
605+ compile_cases cxt e sw_consts sw_num_default (get_name true )
564606 else
565607 (* [e] will be used twice *)
566608 let dispatch e =
567609 S. if_
568610 (E. is_type_number e )
569- (compile_cases cxt e sw_consts sw_num_default
611+ (compile_cases cxt e sw_consts sw_num_default (get_name true )
570612 )
571613 (* default still needed, could simplified*)
572614 ~else_:
573- (compile_cases cxt (E. tag e ) sw_blocks
574- sw_blocks_default) in
615+ (compile_cases cxt (E. tag e ) sw_blocks
616+ sw_blocks_default (get_name false ) ) in
575617 match e.expression_desc with
576618 | J. Var _ -> [ dispatch e]
577619 | _ ->
@@ -597,6 +639,7 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
597639
598640and compile_string_cases cxt switch_exp table default =
599641 compile_general_cases
642+ (fun s -> None )
600643 E. str
601644 E. string_equal
602645 cxt
@@ -752,15 +795,15 @@ and compile_staticcatch (lam : Lam.t) (lambda_cxt : Lam_compile_context.t)=
752795 Js_output. append_output
753796 (Js_output. make (S. declare_variable ~kind: Variable v :: declares) )
754797 (Js_output. append_output lbody (Js_output. make (
755- compile_cases new_cxt exit_expr handlers NonComplete ) ~value: (E. var v )))
798+ compile_cases new_cxt exit_expr handlers NonComplete ( fun _ -> None ) ) ~value: (E. var v )))
756799 | Declare (kind, id)
757800 (* declare first this we will do branching*) ->
758801 let declares = S. declare_variable ~kind id :: declares in
759802 let new_cxt = {lambda_cxt with jmp_table = jmp_table; continuation = Assign id } in
760803 let lbody = compile_lambda new_cxt body in
761804 Js_output. append_output (Js_output. make declares)
762805 (Js_output. append_output lbody
763- (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete )))
806+ (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete ( fun _ -> None ) )))
764807 (* place holder -- tell the compiler that
765808 we don't know if it's complete
766809 *)
@@ -769,13 +812,13 @@ and compile_staticcatch (lam : Lam.t) (lambda_cxt : Lam_compile_context.t)=
769812 let lbody = compile_lambda new_cxt body in
770813 Js_output. append_output (Js_output. make declares)
771814 (Js_output. append_output lbody
772- (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete )))
815+ (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete ( fun _ -> None ) )))
773816 | Assign _ ->
774817 let new_cxt = {lambda_cxt with jmp_table = jmp_table } in
775818 let lbody = compile_lambda new_cxt body in
776819 Js_output. append_output (Js_output. make declares)
777820 (Js_output. append_output lbody
778- (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete )))
821+ (Js_output. make (compile_cases new_cxt exit_expr handlers NonComplete ( fun _ -> None ) )))
779822
780823and compile_sequand
781824 (l : Lam.t ) (r : Lam.t ) (lambda_cxt : Lam_compile_context.t ) =
@@ -1588,7 +1631,7 @@ and compile_lambda
15881631 | Lstringswitch (l , cases , default ) ->
15891632 compile_stringswitch l cases default lambda_cxt
15901633 | Lswitch (switch_arg , sw ) ->
1591- compile_switch switch_arg sw lambda_cxt
1634+ compile_switch switch_arg sw lambda_cxt
15921635 | Lstaticraise (i , largs ) ->
15931636 compile_staticraise i largs lambda_cxt
15941637 | Lstaticcatch _ ->
0 commit comments