2424
2525
2626let arity_of_var (meta : Lam_stats.t ) (v : Ident.t ) =
27- (* * for functional parameter, if it is a high order function,
28- if it's not from function parameter, we should warn
29- *)
30- match Ident_hashtbl. find_opt meta.ident_tbl v with
31- | Some (FunctionId {arity;_} ) -> arity
32- | Some _
33- | None ->
34- Lam_arity. na
27+ (* * for functional parameter, if it is a high order function,
28+ if it's not from function parameter, we should warn
29+ *)
30+ match Ident_hashtbl. find_opt meta.ident_tbl v with
31+ | Some (FunctionId {arity;_} ) -> arity
32+ | Some _
33+ | None ->
34+ Lam_arity. na
3535
3636
3737(* we need record all aliases -- since not all aliases are eliminated,
3838 mostly are toplevel bindings
3939 We will keep iterating such environment
4040 If not found, we will return [NA]
4141*)
42- let rec get_arity
43- (meta : Lam_stats.t )
44- (lam : Lam.t ) :
45- Lam_arity. t =
42+ let rec get_arity (meta : Lam_stats.t ) (lam : Lam.t ) : Lam_arity.t =
4643 match lam with
47- | Lconst _ -> Lam_arity. non_function_arity_info
4844 | Lvar v -> arity_of_var meta v
45+ | Lconst _ -> Lam_arity. non_function_arity_info
4946 | Llet (_ ,_ ,_ , l ) -> get_arity meta l
5047 | Lprim {primitive = Pfield (n,_);
5148 args = [ Lglobal_module id ]; _} ->
5249 begin match (Lam_compile_env. cached_find_ml_id_pos id n meta.env).arity with
53- | Single x -> x
54- | Submodule _ -> Lam_arity. na
50+ | Single x -> x
51+ | Submodule _ -> Lam_arity. na
5552 end
5653 | Lprim {primitive = Pfield (m,_);
5754 args = [ Lprim {primitive = Pfield (n,_);
58- args = [ Lglobal_module id]} ]
59- ; _} ->
55+ args = [ Lglobal_module id]} ]
56+ ; _} ->
6057 begin match (Lam_compile_env. cached_find_ml_id_pos id n meta.env).arity with
6158 | Submodule subs -> subs.(m)
6259 | Single _ -> Lam_arity. na
63- end
60+ end
6461 (* TODO: all information except Pccall is complete, we could
65- get more arity information
62+ get more arity information
6663 *)
6764 | Lprim {primitive = Praise ; _} -> Lam_arity. raise_arity_info
6865 | Lglobal_module _ (* TODO: fix me never going to happen assert false *)
@@ -86,15 +83,15 @@ let rec get_arity
8683 begin match fn with
8784 | Arity_na -> Lam_arity. na
8885 | Arity_info (b , xs , tail ) ->
89- let rec take (xs : _ list ) arg_length =
90- match xs with
86+ let rec take (arities : _ list ) arg_length =
87+ match arities with
9188 | x :: yys ->
9289 if arg_length = x then Lam_arity. info b yys tail
9390 else if arg_length > x then
9491 take yys (arg_length - x)
9592 else Lam_arity. info b
96- ((x - arg_length ) :: yys)
97- tail
93+ ((x - arg_length ) :: yys)
94+ tail
9895 | [] ->
9996 if tail then Lam_arity. info b [] tail
10097 else Lam_arity. na
@@ -115,7 +112,7 @@ let rec get_arity
115112 all_lambdas meta (
116113 let rest =
117114 Ext_list. map_append snd sw_consts
118- (Ext_list. map snd sw_blocks) in
115+ (Ext_list. map snd sw_blocks) in
119116 match sw_failaction with None -> rest | Some x -> x::rest )
120117 | Lstringswitch (l , sw , d ) ->
121118 begin match d with
@@ -139,43 +136,20 @@ and all_lambdas meta (xs : Lam.t list) =
139136 match xs with
140137 | y :: ys ->
141138 let arity = get_arity meta y in
142- List. fold_left (fun exist (v : Lam.t ) ->
143- match (exist : Lam_arity.t ) with
139+ let rec aux (acc : Lam_arity.t ) xs =
140+ match acc, xs with
141+ | Arity_na , _ -> acc
142+ | _ , [] -> acc
143+ | Arity_info (bbb , xxxs , tail ), y ::ys ->
144+ match get_arity meta y with
144145 | Arity_na -> Lam_arity. na
145- | Arity_info (bbb , xxxs , tail ) ->
146- begin
147- match get_arity meta v with
148- | Arity_na -> Lam_arity. na
149- | Arity_info (u ,ys ,tail2 ) ->
150- let rec aux (b ,acc ) xs ys =
151- match xs,ys with
152- | [] , [] -> (b, List. rev acc, tail && tail2)
153- | [] , y ::ys when tail ->
154- aux (b,y::acc) [] ys
155- | x ::xs , [] when tail2 ->
156- aux (b,x::acc) [] xs
157- | x ::xs , y ::ys when x = y -> aux (b, (y :: acc)) xs ys
158- | _ , _ -> (false , List. rev acc, false ) in
159- let (b,acc, tail3) = aux ( u && bbb, [] ) xxxs ys in
160- Lam_arity. info b acc tail3
161- end
162- ) arity ys
163- | _ -> assert false
146+ | Arity_info (u ,yyys ,tail2 ) ->
147+ aux
148+ (Lam_arity. merge_arities ( u && bbb ) xxxs yyys tail tail2)
149+ ys
150+ in aux arity ys
151+
152+ | [] -> Lam_arity. na
153+
164154
165- (*
166- let dump_exports_arities (meta : Lam_stats.t ) =
167- let fmt =
168- if meta.filename <> "" then
169- let cmj_file = Ext_path.chop_extension meta.filename ^ Literals.suffix_cmj in
170- let out = open_out cmj_file in
171- Format.formatter_of_out_channel out
172- else
173- Format.err_formatter in
174- begin
175- List.iter (fun (i : Ident.t) ->
176- pp fmt "@[%s: %s -> %a@]@." meta.filename i.name
177- pp_arities (get_arity meta (Lvar i))
178- ) meta.exports
179- end
180- *)
181155
0 commit comments