Skip to content

Commit 5543e4f

Browse files
committed
continue cleaning up, keep meaningful names
1 parent 0c9fd50 commit 5543e4f

14 files changed

+384
-381
lines changed

jscomp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ CORE_SRCS= js_runtime_modules \
298298
lam_group \
299299
lam_pass_deep_flatten\
300300
js_stmt_make js_pass_flatten\
301-
js_pass_tailcall_inline js_of_lam_variant js_pass_flatten_and_mark_dead js_ast_util lam_dce lam_group_pass lam_compile_env lam_stats_util\
301+
js_pass_tailcall_inline js_of_lam_variant js_pass_flatten_and_mark_dead js_ast_util lam_dce lam_group_pass lam_compile_env lam_arity_analysis\
302302
lam_stats_export lam_pass_alpha_conversion lam_pass_collect\
303303
js_dump_lit\
304304
js_dump_string\

jscomp/all.depend

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ core/lam_dce.cmi : core/lam_group.cmi
387387
core/lam_compile_env.cmi : core/ocaml_types.cmi core/lam_module_ident.cmi \
388388
core/lam.cmi core/js_packages_info.cmi core/js_cmj_format.cmi \
389389
syntax/external_ffi_types.cmi
390-
core/lam_stats_util.cmi : core/lam_stats.cmi core/lam_arity.cmi core/lam.cmi
390+
core/lam_arity_analysis.cmi : core/lam_stats.cmi core/lam_arity.cmi \
391+
core/lam.cmi
391392
core/lam_stats_export.cmi : core/lam_stats.cmi core/lam_module_ident.cmi \
392393
core/lam.cmi core/js_cmj_format.cmi ext/ident_map.cmi
393394
core/lam_pass_alpha_conversion.cmi : core/lam_stats.cmi core/lam.cmi
@@ -565,23 +566,23 @@ core/lam_compile_env.cmx : ext/string_map.cmx core/ocaml_types.cmx \
565566
core/js_stmt_make.cmx core/js_exp_make.cmx common/js_config.cmx \
566567
core/js_cmj_load.cmx core/js_cmj_format.cmx syntax/external_ffi_types.cmx \
567568
ext/ext_string.cmx ext/ext_modulename.cmx core/lam_compile_env.cmi
568-
core/lam_stats_util.cmx : core/lam_stats.cmx core/lam_compile_env.cmx \
569+
core/lam_arity_analysis.cmx : core/lam_stats.cmx core/lam_compile_env.cmx \
569570
core/lam_arity.cmx core/lam.cmx ext/ident_hashtbl.cmx ext/ext_list.cmx \
570-
core/lam_stats_util.cmi
571-
core/lam_stats_export.cmx : ext/string_map.cmx core/lam_stats_util.cmx \
572-
core/lam_stats.cmx core/lam_module_ident.cmx core/lam_inline_util.cmx \
573-
core/lam_id_kind.cmx core/lam_compile_env.cmx core/lam_closure.cmx \
571+
core/lam_arity_analysis.cmi
572+
core/lam_stats_export.cmx : ext/string_map.cmx core/lam_stats.cmx \
573+
core/lam_module_ident.cmx core/lam_inline_util.cmx core/lam_id_kind.cmx \
574+
core/lam_compile_env.cmx core/lam_closure.cmx core/lam_arity_analysis.cmx \
574575
core/lam_arity.cmx core/lam_analysis.cmx core/lam.cmx \
575576
core/js_packages_state.cmx core/js_cmj_format.cmx ext/ident_set.cmx \
576577
ext/ident_map.cmx ext/ident_hashtbl.cmx ext/ext_option.cmx \
577578
common/ext_log.cmx ext/ext_list.cmx ext/ext_array.cmx \
578579
core/lam_stats_export.cmi
579-
core/lam_pass_alpha_conversion.cmx : core/lam_stats_util.cmx \
580-
core/lam_stats.cmx core/lam_eta_conversion.cmx core/lam.cmx \
580+
core/lam_pass_alpha_conversion.cmx : core/lam_stats.cmx \
581+
core/lam_eta_conversion.cmx core/lam_arity_analysis.cmx core/lam.cmx \
581582
ext/ext_list.cmx core/lam_pass_alpha_conversion.cmi
582-
core/lam_pass_collect.cmx : core/lam_util.cmx core/lam_stats_util.cmx \
583-
core/lam_stats.cmx core/lam_arity.cmx core/lam.cmx ext/ident_set.cmx \
584-
ext/ident_hashtbl.cmx core/lam_pass_collect.cmi
583+
core/lam_pass_collect.cmx : core/lam_util.cmx core/lam_stats.cmx \
584+
core/lam_arity_analysis.cmx core/lam_arity.cmx core/lam.cmx \
585+
ext/ident_set.cmx ext/ident_hashtbl.cmx core/lam_pass_collect.cmi
585586
core/js_dump_lit.cmx : ext/literals.cmx ext/ext_string.cmx
586587
core/js_dump_string.cmx : ext/ext_pp.cmx core/js_dump_string.cmi
587588
core/js_dump_property.cmx : core/js_dump_string.cmx core/js_dump_lit.cmx \
@@ -681,10 +682,10 @@ core/lam_pass_remove_alias.cmx : core/lam_util.cmx core/lam_stats.cmx \
681682
ext/ident_set.cmx ext/ident_hashtbl.cmx ext/ext_list.cmx \
682683
core/lam_pass_remove_alias.cmi
683684
core/lam_coercion.cmx : ext/string_hash_set.cmx core/lam_util.cmx \
684-
core/lam_stats_util.cmx core/lam_stats.cmx core/lam_group.cmx \
685-
core/lam_dce.cmx core/lam.cmx ext/ident_set.cmx ext/ident_map.cmx \
686-
ext/ident_hashtbl.cmx ext/ext_list.cmx depends/bs_exception.cmx \
687-
core/lam_coercion.cmi
685+
core/lam_stats.cmx core/lam_group.cmx core/lam_dce.cmx \
686+
core/lam_arity_analysis.cmx core/lam_arity.cmx core/lam.cmx \
687+
ext/ident_set.cmx ext/ident_map.cmx ext/ident_hashtbl.cmx \
688+
ext/ext_list.cmx depends/bs_exception.cmx core/lam_coercion.cmi
688689
core/lam_compile_main.cmx : ext/literals.cmx core/lam_util.cmx \
689690
core/lam_stats_export.cmx core/lam_stats.cmx \
690691
core/lam_pass_remove_alias.cmx core/lam_pass_lets_dce.cmx \

jscomp/core/js_cmj_format.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type cmj_value = {
4141
type effect = string option
4242

4343

44-
let single_na = Single Arity_na
44+
let single_na = Single Lam_arity.na
4545
(** we don't force people to use package *)
4646
type cmj_case = Ext_namespace.file_kind
4747

jscomp/core/lam_arity.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ let merge
7070
match x with
7171
| Arity_na -> Arity_info (false, [n], false)
7272
| Arity_info (b,xs,tail) -> Arity_info (b, n :: xs, tail)
73+
74+
75+
let non_function_arity_info =
76+
Arity_info (true, [], false)
77+
78+
let raise_arity_info =
79+
Arity_info (true,[],true)
80+
81+
let na = Arity_na
82+
83+
let info b0 args b1 =
84+
Arity_info (b0, args, b1)
85+
86+
87+
let first_arity_na ( x : t ) =
88+
match x with
89+
| Arity_na
90+
| Arity_info (_, [], _) -> true
91+
| _ -> false

jscomp/core/lam_arity.mli

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
type t =
26+
type t = private
2727
| Arity_info of bool * int list * bool
2828
(**
2929
when the first argument is true, it is for sure
@@ -41,4 +41,13 @@ val print_arities_tbl :
4141
(Ident.t, t ref) Hashtbl.t ->
4242
unit
4343

44-
val merge : int -> t -> t
44+
val merge : int -> t -> t
45+
46+
val non_function_arity_info : t
47+
48+
val raise_arity_info : t
49+
50+
val na : t
51+
val info : bool -> int list -> bool -> t
52+
53+
val first_arity_na : t -> bool

jscomp/core/lam_stats_util.ml renamed to jscomp/core/lam_arity_analysis.ml

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@ let arity_of_var (meta : Lam_stats.t) (v : Ident.t) =
2727
(** for functional parameter, if it is a high order function,
2828
if it's not from function parameter, we should warn
2929
*)
30-
begin
3130
match Ident_hashtbl.find_opt meta.ident_tbl v with
3231
| Some (FunctionId {arity;_}) -> arity
3332
| Some _
3433
| None ->
35-
(* Format.fprintf Format.err_formatter *)
36-
(* "@[%s %a is not function/functor@]@." meta.filename Ident.print v ; *)
37-
Arity_na
34+
Lam_arity.na
3835

39-
end
4036

4137
(* we need record all aliases -- since not all aliases are eliminated,
4238
mostly are toplevel bindings
@@ -48,36 +44,29 @@ let rec get_arity
4844
(lam : Lam.t) :
4945
Lam_arity.t =
5046
match lam with
51-
| Lconst _ -> Arity_info (true,[], false)
47+
| Lconst _ -> Lam_arity.non_function_arity_info
5248
| Lvar v -> arity_of_var meta v
5349
| Llet(_,_,_, l ) -> get_arity meta l
54-
55-
(* begin match Parsetree_util.has_arity prim_attributes with *)
56-
(* | Some arity -> *)
57-
(* (\* Ext_log.dwarn __LOC__ "arity %d" arity; *\) *)
58-
(* Determin(false, [arity, None], false) *)
59-
(* | None -> NA *)
60-
(* end *)
6150
| Lprim {primitive = Pfield (n,_);
6251
args = [ Lglobal_module id ]; _} ->
6352
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
6453
| Single x -> x
65-
| Submodule _ -> Arity_na
54+
| Submodule _ -> Lam_arity.na
6655
end
6756
| Lprim {primitive = Pfield (m,_);
6857
args = [ Lprim{primitive = Pfield(n,_);
6958
args = [ Lglobal_module id]} ]
7059
; _} ->
7160
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
7261
| Submodule subs -> subs.(m)
73-
| Single _ -> Arity_na
62+
| Single _ -> Lam_arity.na
7463
end
75-
76-
| Lprim {primitive = Pfield _; _} -> Arity_na (** TODO *)
77-
| Lprim {primitive = Praise ; _} -> Arity_info (true,[], true)
78-
| Lprim {primitive = Pccall _; _} -> Arity_info (false, [], false)
64+
(* TODO: all information except Pccall is complete, we could
65+
get more arity information
66+
*)
67+
| Lprim {primitive = Praise ; _} -> Lam_arity.raise_arity_info
7968
| Lglobal_module _ (* TODO: fix me never going to happen assert false *)
80-
| Lprim _ -> Arity_info (true,[] ,false)
69+
| Lprim _ -> Lam_arity.na (* CHECK*)
8170
(* shall we handle primitive in a direct way,
8271
since we know all the information
8372
Invariant: all primitive application is fully applied,
@@ -90,41 +79,33 @@ let rec get_arity
9079
it seems true that primitive is always fully applied, however,
9180
it can return a function
9281
*)
93-
| Lletrec(_, body) ->
94-
get_arity meta body
95-
(* | Lapply(Lprim( p, _), _args, _info) -> *)
96-
(* Determin(true, [], false) (\** Invariant : primtive application is always complete.. *\) *)
82+
| Lletrec(_, body) -> get_arity meta body
9783

9884
| Lapply{fn = app; args; _ } -> (* detect functor application *)
9985
let fn = get_arity meta app in
10086
begin match fn with
101-
| Arity_na -> Arity_na
87+
| Arity_na -> Lam_arity.na
10288
| Arity_info (b, xs, tail ) ->
10389
let rec take (xs : _ list) arg_length =
10490
match xs with
105-
| (x) :: xs ->
106-
if arg_length = x then Lam_arity.Arity_info (b, xs, tail)
91+
| x :: yys ->
92+
if arg_length = x then Lam_arity.info b yys tail
10793
else if arg_length > x then
108-
take xs (arg_length - x)
109-
else Arity_info (b,
110-
(x - arg_length ) :: xs ,
111-
tail)
94+
take yys (arg_length - x)
95+
else Lam_arity.info b
96+
((x - arg_length ) :: yys)
97+
tail
11298
| [] ->
113-
if tail then Arity_info (b, [], tail)
114-
else if not b then
115-
Arity_na
116-
else Arity_na
99+
if tail then Lam_arity.info b [] tail
100+
else Lam_arity.na
117101
(* Actually, you can not have truly deministic arities
118102
for example [fun x -> x ]
119103
*)
120-
(* Ext_pervasives.failwithf ~loc:__LOC__ "%s %s" *)
121-
(* (Format.asprintf "%a" pp_arities fn) *)
122-
(* (Lam_util.string_of_lambda lam) *)
123104
in
124105
take xs (List.length args)
125106
end
126-
| Lfunction {arity; function_kind; params; body = l} ->
127-
Lam_arity.merge arity (get_arity meta l)
107+
| Lfunction {arity; body} ->
108+
Lam_arity.merge arity (get_arity meta body)
128109
| Lswitch(l, {sw_failaction;
129110
sw_consts;
130111
sw_blocks;
@@ -141,30 +122,30 @@ let rec get_arity
141122
| None -> all_lambdas meta (Ext_list.map snd sw )
142123
| Some v -> all_lambdas meta (v:: Ext_list.map snd sw)
143124
end
144-
| Lstaticraise _ -> Arity_na (* since it will not be in tail position *)
145125
| Lstaticcatch(_, _, handler) -> get_arity meta handler
146126
| Ltrywith(l1, _, l2) ->
147127
all_lambdas meta [l1;l2]
148128
| Lifthenelse(l1, l2, l3) ->
149129
all_lambdas meta [l2;l3]
150130
| Lsequence(_, l2) -> get_arity meta l2
151-
| Lsend(u, m, o, ll, v) -> Arity_na
152-
| Lifused(v, l) -> Arity_na
131+
| Lstaticraise _ (* since it will not be in tail position *)
132+
| Lsend _
133+
| Lifused _ -> Lam_arity.na
153134
| Lwhile _
154135
| Lfor _
155-
| Lassign _ -> Arity_info (true,[], false)
136+
| Lassign _ -> Lam_arity.non_function_arity_info
156137

157138
and all_lambdas meta (xs : Lam.t list) =
158139
match xs with
159140
| y :: ys ->
160141
let arity = get_arity meta y in
161142
List.fold_left (fun exist (v : Lam.t) ->
162143
match (exist : Lam_arity.t) with
163-
| Arity_na -> Arity_na
164-
| Arity_info (b, xs, tail) ->
144+
| Arity_na -> Lam_arity.na
145+
| Arity_info (bbb, xxxs, tail) ->
165146
begin
166147
match get_arity meta v with
167-
| Arity_na -> Arity_na
148+
| Arity_na -> Lam_arity.na
168149
| Arity_info (u,ys,tail2) ->
169150
let rec aux (b,acc) xs ys =
170151
match xs,ys with
@@ -175,8 +156,8 @@ and all_lambdas meta (xs : Lam.t list) =
175156
aux (b,x::acc) [] xs
176157
| x::xs, y::ys when x = y -> aux (b, (y :: acc)) xs ys
177158
| _, _ -> (false, List.rev acc, false) in
178-
let (b,acc, tail3) = aux ( u &&b, []) xs ys in
179-
Arity_info (b,acc, tail3)
159+
let (b,acc, tail3) = aux ( u &&bbb, []) xxxs ys in
160+
Lam_arity.info b acc tail3
180161
end
181162
) arity ys
182163
| _ -> assert false

jscomp/core/lam_stats_util.mli renamed to jscomp/core/lam_arity_analysis.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
(** Utilities for lambda analysis *)
3333

3434

35-
val arity_of_var :
36-
Lam_stats.t ->
37-
Ident.t ->
38-
Lam_arity.t
3935

4036
val get_arity :
4137
Lam_stats.t ->

jscomp/core/lam_coercion.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ let handle_exports (meta : Lam_stats.t)
135135
of size 4 instead of 2
136136
*)
137137
let newid = Ident.rename original_export_id in
138-
(match Lam_stats_util.get_arity meta lam with
139-
| Arity_na
140-
| Arity_info(_,[],_) -> ()
141-
| Arity_info _ as v ->
138+
let v = Lam_arity_analysis.get_arity meta lam in
139+
(if not (Lam_arity.first_arity_na v) then
142140
Ident_hashtbl.add meta.ident_tbl newid
143141
(FunctionId{
144142
arity = v; lambda = lam;

jscomp/core/lam_compile.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ and compile_external_field_apply
181181
match arity, len with
182182
| _, 0 ->
183183
acc (** All arguments consumed so far *)
184-
| Arity_info (a, x :: rest, b), len ->
184+
| Arity_info (aaaaa, x :: rest, bbbbb), len ->
185185
let x =
186186
if x = 0
187187
then 1
@@ -191,7 +191,7 @@ and compile_external_field_apply
191191
let first_part, continue = Ext_list.split_at x args in
192192
aux
193193
(E.call ~info:{arity=Full; call_info = Call_ml} acc first_part)
194-
(Arity_info (a, rest, b))
194+
(Lam_arity.info aaaaa rest bbbbb)
195195
continue (len - x)
196196
else (* GPR #1423 *)
197197
if List.for_all Js_analyzer.is_okay_to_duplicate args then
@@ -218,7 +218,7 @@ and compile_external_field_apply
218218
(
219219
aux
220220
(E.ml_var_dot id name)
221-
(match arity with Single x -> x | Submodule _ -> Arity_na)
221+
(match arity with Single x -> x | Submodule _ -> Lam_arity.na)
222222
args (List.length args ))
223223

224224

jscomp/core/lam_pass_alpha_conversion.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
3737
| Lapply {fn = l1; args = ll; loc ; status}
3838
-> (* detect functor application *)
3939
begin
40-
match Lam_stats_util.get_arity meta l1 with
40+
match Lam_arity_analysis.get_arity meta l1 with
4141
| Arity_na ->
4242
Lam.apply (simpl l1) (Ext_list.map simpl ll) loc status
43-
| Arity_info (b, args, tail) ->
43+
| Arity_info (_, args, _) ->
4444
let len = List.length ll in
4545
let rec take args =
4646
match args with
@@ -76,15 +76,16 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
7676
| Lprim {primitive = (Lam.Pjs_fn_make len) as primitive ; args = [arg]
7777
; loc } ->
7878

79-
begin match Lam_stats_util.get_arity meta arg with
80-
| Arity_info (b, x::_, tail)
79+
begin match Lam_arity_analysis.get_arity meta arg with
80+
| Arity_info (_, x::_, _)
8181
->
8282
let arg = simpl arg in
8383
Lam_eta_conversion.unsafe_adjust_to_arity loc
8484
~to_:len
8585
~from:x
8686
arg
87-
| _ -> Lam.prim ~primitive ~args:[simpl arg] loc
87+
| Arity_info(_,[],_)
88+
| Arity_na -> Lam.prim ~primitive ~args:[simpl arg] loc
8889
end
8990
| Lprim {primitive; args ; loc} ->
9091
Lam.prim ~primitive ~args:(Ext_list.map simpl args) loc

0 commit comments

Comments
 (0)