forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlam_arity_analysis.ml
151 lines (146 loc) · 5.67 KB
/
lam_arity_analysis.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
let arity_of_var (meta : Lam_stats.t) (v : Ident.t) =
(* for functional parameter, if it is a high order function,
if it's not from function parameter, we should warn
*)
match Hash_ident.find_opt meta.ident_tbl v with
| Some (FunctionId {arity; _}) -> arity
| Some _ | None -> Lam_arity.na
(* we need record all aliases -- since not all aliases are eliminated,
mostly are toplevel bindings
We will keep iterating such environment
If not found, we will return [NA]
*)
let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
match lam with
| Lvar v -> arity_of_var meta v
| Lconst _ -> Lam_arity.non_function_arity_info
| Llet (_, _, _, l) -> get_arity meta l
| Lprim
{
primitive = Pfield (_, Fld_module {name});
args = [Lglobal_module (id, dynamic_import)];
_;
} -> (
match
(Lam_compile_env.query_external_id_info ~dynamic_import id name).arity
with
| Single x -> x
| Submodule _ -> Lam_arity.na)
| Lprim
{
primitive = Pfield (m, _);
args =
[
Lprim
{
primitive = Pfield (_, Fld_module {name});
args = [Lglobal_module (id, dynamic_import)];
};
];
_;
} -> (
match
(Lam_compile_env.query_external_id_info ~dynamic_import id name).arity
with
| Submodule subs -> subs.(m) (* TODO: shall we store it as array?*)
| Single _ -> Lam_arity.na)
| Lprim {primitive = Praw_js_code {code_info = Exp (Js_function {arity})}} ->
Lam_arity.info [arity] false
| Lprim {primitive = Praise; _} -> Lam_arity.raise_arity_info
| Lglobal_module _ (* TODO: fix me never going to happen *) | Lprim _ ->
Lam_arity.na (* CHECK*)
(* shall we handle primitive in a direct way,
since we know all the information
Invariant: all primitive application is fully applied,
since this information is already available
-- Check external c functions ?
-- it's not true for primitives
like caml_set_oo_id or Lprim (Pmakeblock , [])
it seems true that primitive is always fully applied, however,
it can return a function
*)
| Lletrec (_, body) -> get_arity meta body
| Lapply {ap_func = app; ap_args = args; _} -> (
(* detect functor application *)
let fn = get_arity meta app in
match fn with
| Arity_na -> Lam_arity.na
| Arity_info (xs, tail) ->
let rec take (arities : _ list) arg_length =
match arities with
| x :: yys ->
if arg_length = x then Lam_arity.info yys tail
else if arg_length > x then take yys (arg_length - x)
else Lam_arity.info ((x - arg_length) :: yys) tail
| [] -> if tail then Lam_arity.raise_arity_info else Lam_arity.na
(* Actually, you can not have truly deministic arities
for example [fun x -> x ]
*)
in
take xs (List.length args))
| Lfunction {arity; body} -> Lam_arity.merge arity (get_arity meta body)
| Lswitch
( _,
{
sw_failaction;
sw_consts;
sw_blocks;
sw_blocks_full = _;
sw_consts_full = _;
} ) ->
all_lambdas meta
(let rest =
Ext_list.map_append sw_consts (Ext_list.map sw_blocks snd) snd
in
match sw_failaction with
| None -> rest
| Some x -> x :: rest)
| Lstringswitch (_, sw, d) -> (
match d with
| None -> all_lambdas meta (Ext_list.map sw snd)
| Some v -> all_lambdas meta (v :: Ext_list.map sw snd))
| Lstaticcatch (_, _, handler) -> get_arity meta handler
| Ltrywith (l1, _, l2) -> all_lambdas meta [l1; l2]
| Lifthenelse (_, l2, l3) -> all_lambdas meta [l2; l3]
| Lsequence (_, l2) -> get_arity meta l2
| Lstaticraise _ (* since it will not be in tail position *) -> Lam_arity.na
| Lwhile _ | Lfor _ | Lassign _ -> Lam_arity.non_function_arity_info
and all_lambdas meta (xs : Lam.t list) =
match xs with
| y :: ys ->
let arity = get_arity meta y in
let rec aux (acc : Lam_arity.t) xs =
match (acc, xs) with
| Arity_na, _ -> acc
| _, [] -> acc
| Arity_info (xxxs, tail), y :: ys -> (
match get_arity meta y with
| Arity_na -> Lam_arity.na
| Arity_info (yyys, tail2) ->
aux (Lam_arity.merge_arities xxxs yyys tail tail2) ys)
in
aux arity ys
| [] -> Lam_arity.na