Source file ast_typ_uncurry.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
open Import
module Typ = Ast_helper.Typ
(** [arity_of_fun pat e] tells the arity of
expression [fun pat -> e] *)
let arity_of_fun =
let rec arity_of_fun =
let arity_aux params ~init =
List.fold_left ~init
~f:(fun acc param ->
match param with
| { pparam_desc = Pparam_newtype _; _ } -> acc
| {
pparam_desc =
Pparam_val
( _,
_,
{
ppat_desc = Ppat_construct ({ txt = Lident "()"; _ }, None);
_;
} );
_;
}
when acc = 0 ->
acc
| { pparam_desc = Pparam_val (_, _, _); _ } -> acc + 1)
params
in
fun acc params body ->
let base = arity_aux params ~init:acc in
match body with
| { pexp_desc = Pexp_function (params', _, Pfunction_body body); _ } ->
arity_of_fun base params' body
| _ -> base
in
fun params body -> arity_of_fun 0 params body
let labels_of_fun =
let rec labels_of_fun =
let lbls_aux params ~init =
List.fold_left ~init
~f:(fun acc param ->
match param with
| { pparam_desc = Pparam_newtype _; _ } -> acc
| { pparam_desc = Pparam_val (l, _, _); _ } -> l :: acc)
params
in
fun acc params body ->
let base = lbls_aux params ~init:acc in
match body with
| { pexp_desc = Pexp_function (params', _, Pfunction_body body); _ } ->
labels_of_fun base params' body
| _ -> List.rev base
in
fun params body -> labels_of_fun [] params body
let to_method_callback_type ~loc (mapper : Ast_traverse.map)
(label : Asttypes.arg_label) (first_arg : core_type) (typ : core_type) =
let meth_type =
let first_arg = mapper#core_type first_arg in
let typ = mapper#core_type typ in
Typ.arrow ~loc label first_arg typ
in
let arity = Option.get (Ast_core_type.get_uncurry_arity meth_type) in
Typ.constr
{
txt = Ldot (Ast_literal.js_meth_callback, Format.sprintf "arity%d" arity);
loc;
}
[ meth_type ]
let generate_method_type =
let self_type_lit = "self_type" in
fun loc (mapper : Ast_traverse.map) ?alias_type method_name params body ->
let result = Typ.var ~loc method_name in
let self_type =
match alias_type with
| None -> Typ.var ~loc self_type_lit
| Some ty ->
Typ.alias
~attrs:[ Ast_attributes.unused_type_declaration ]
~loc ty
{ loc; txt = self_type_lit }
in
match arity_of_fun params body with
| 0 -> to_method_callback_type ~loc mapper Nolabel self_type result
| _n -> (
let tyvars =
List.mapi
~f:(fun i x -> (x, Typ.var ~loc (method_name ^ string_of_int i)))
(labels_of_fun params body)
in
match tyvars with
| (label, x) :: rest ->
let method_rest =
List.fold_right
~f:(fun (label, v) acc -> Typ.arrow ~loc label v acc)
rest ~init:result
in
to_method_callback_type ~loc mapper Nolabel self_type
(Typ.arrow ~loc label x method_rest)
| _ -> assert false)
let to_method_type ~loc ~kind (mapper : Ast_traverse.map)
(label : Asttypes.arg_label) (first_arg : core_type) (typ : core_type) =
let typ = mapper#core_type typ in
let meth_type =
let first_arg = mapper#core_type first_arg in
Typ.arrow ~loc label first_arg typ
in
match Option.get (Ast_core_type.get_uncurry_arity meth_type) with
| 0 ->
Typ.constr
{
txt =
Ldot
( (match kind with
| `uncurry -> Ast_literal.js_fn
| `oo -> Ast_literal.js_meth),
"arity0" );
loc;
}
[ typ ]
| n ->
Typ.constr
{
txt =
Ldot
( (match kind with
| `uncurry -> Ast_literal.js_fn
| `oo -> Ast_literal.js_meth),
"arity" ^ string_of_int n );
loc;
}
[ meth_type ]
let to_uncurry_type ~loc mapper label first_arg typ =
to_method_type ~loc ~kind:`uncurry mapper label first_arg typ
let to_method_type ~loc mapper label first_arg typ =
to_method_type ~loc ~kind:`oo mapper label first_arg typ
let generate_arg_type ~loc (mapper : Ast_traverse.map) method_name params body =
match arity_of_fun params body with
| 0 ->
to_method_type ~loc mapper Nolabel [%type: unit]
(Typ.var ~loc method_name)
| _ -> (
let tyvars =
List.mapi
~f:(fun i x ->
(x, Typ.var ~loc (Format.sprintf "%s%d" method_name i)))
(labels_of_fun params body)
in
match tyvars with
| (label, x) :: rest ->
let method_rest =
let init = Typ.var ~loc method_name in
List.fold_right
~f:(fun (label, v) acc -> Typ.arrow ~loc label v acc)
rest ~init
in
to_method_type ~loc mapper label x method_rest
| [] -> assert false)