forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_ast_derive_abstract.ml
240 lines (229 loc) · 9.37 KB
/
native_ast_derive_abstract.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
module U = Ast_derive_util
open Ast_helper
type tdcls = Parsetree.type_declaration list
module Ast_attributes = struct
include Ast_attributes
let deprecated s : Ast_attributes.attr =
{txt = "ocaml.deprecated"; loc = Location.none },
PStr
[
{pstr_desc =
Pstr_eval (
Ast_compatible.const_exp_string ~loc:Location.none s,
[])
; pstr_loc = Location.none}]
let is_optional (attr : attr) =
match attr with
| {Location.txt = "bs.optional"; _}, _ -> true
| _ -> false
let is_bs_as (attr : attr) =
match attr with
| {Location.txt = "bs.as"; _}, _ -> true
| _ -> false
end
let deprecated name =
Ast_attributes.deprecated
("use " ^ name ^ "Get instead or use {abstract = light} explicitly")
let strip_option arg_name =
match arg_name with
| Asttypes.Nolabel -> assert false
| Optional s
| Labelled s -> s
[@@@ocaml.warning "-a"]
let handleTdcl light (tdcl : Parsetree.type_declaration) =
let core_type = U.core_type_of_type_declaration tdcl in
let loc = tdcl.ptype_loc in
let type_name = tdcl.ptype_name.txt in
match tdcl.ptype_kind with
| Ptype_record label_declarations ->
let is_private = tdcl.ptype_private = Private in
let (has_optional_field, new_label_declarations) =
Ext_list.fold_right label_declarations (false, []) (fun ({pld_type; pld_loc; pld_attributes} as dcl : Parsetree.label_declaration) (has_optional_field, acc) ->
let has_optional_field_local = Ast_attributes.has_bs_optional pld_attributes in
let acc = if has_optional_field_local then
{ dcl with
pld_type = {
dcl.pld_type with
ptyp_desc = Ptyp_constr({txt = Lident "option"; loc = pld_loc}, [pld_type]);
ptyp_loc = pld_loc;
};
pld_attributes = Ext_list.exclude pld_attributes (fun x -> (Ast_attributes.is_optional x) || (Ast_attributes.is_bs_as x))
} :: acc
else dcl :: acc in
(has_optional_field || has_optional_field_local, acc)
) in
let newTdcl = {
tdcl with
ptype_kind = Ptype_record new_label_declarations;
ptype_attributes = [];
} in
let setter_accessor, makeType, labels =
Ext_list.fold_right
label_declarations
([],
(if has_optional_field then
Ast_compatible.arrow ~loc (Ast_literal.type_unit ()) core_type
else core_type),
[])
(fun
({pld_name =
{txt = label_name; loc = label_loc} as pld_name;
pld_type;
pld_mutable;
pld_attributes;
pld_loc
}:
Parsetree.label_declaration) (acc, maker, labels) ->
let is_optional = Ast_attributes.has_bs_optional pld_attributes in
let newLabel =
if is_optional
then {pld_name with txt = Asttypes.Optional pld_name.Asttypes.txt}
else {pld_name with txt = Asttypes.Labelled pld_name.Asttypes.txt}
in
let maker, getter_type =
if is_optional then
let maker_optional_type = Ast_core_type.lift_option_type pld_type in
let getter_optional_type = {
Parsetree.ptyp_desc =
Ptyp_constr(
{txt = Lident "option";
loc = pld_loc
}, [pld_type]);
ptyp_loc = pld_loc;
ptyp_attributes = [];
} in
Ast_compatible.opt_arrow ~loc:pld_loc label_name
pld_type
maker,
Ast_compatible.arrow ~loc core_type getter_optional_type
else
Ast_compatible.label_arrow ~loc:pld_loc label_name pld_type maker,
Ast_compatible.arrow ~loc core_type pld_type
in
let makeGetter light deprec pld_name =
Str.value Nonrecursive [
Vb.mk
~loc:pld_loc
~attrs:(if deprec then deprecated (pld_name.Asttypes.txt) :: []
else [])
(Pat.var {pld_name with txt = if light then label_name else label_name ^ "Get"})
(Exp.constraint_ (Ast_compatible.fun_ ~loc:pld_loc
(Pat.var {Location.txt = "o"; loc = pld_loc})
(Exp.field (Exp.ident {Location.txt = Longident.Lident "o"; loc = pld_loc}) {txt = Longident.Lident pld_name.Location.txt; loc = pld_loc})) getter_type)]
in
let acc = if not light then
makeGetter true true pld_name :: makeGetter false false pld_name :: acc
else makeGetter true false pld_name :: acc in
let is_current_field_mutable = pld_mutable = Mutable in
let acc =
if is_current_field_mutable then
let setter_type =
(Ast_compatible.arrow core_type
(Ast_compatible.arrow
pld_type (* setter *)
(Ast_literal.type_unit ()))) in
let variable = (Exp.ident {Location.txt = Longident.Lident "v"; loc = pld_loc}) in
let setter = Str.value Nonrecursive [
Vb.mk
(Pat.var {loc = label_loc; txt = label_name ^ "Set"})
(Exp.constraint_ (Ast_compatible.fun_ ~loc:pld_loc
(Pat.var {Location.txt = "o"; loc = pld_loc})
(Ast_compatible.fun_ ~loc:pld_loc
(Pat.var {Location.txt = "v"; loc = pld_loc})
(Exp.setfield
(Exp.ident {Location.txt = Longident.Lident "o"; loc = pld_loc})
{txt = Longident.Lident pld_name.Location.txt; loc = pld_loc}
(if is_optional then Exp.construct {txt=Lident "Some"; loc = pld_loc} (Some variable) else variable))))
setter_type)
]
in
setter :: acc
else acc in
acc,
maker,
newLabel::labels
)
in
newTdcl,
(if is_private then
setter_accessor
else
let my_loc = match labels with
| [] -> !default_loc
| { Asttypes.loc = label_loc } :: _ -> label_loc
in
let maker_body = Exp.record (Ext_list.fold_right labels [] (fun ({ Asttypes.txt; loc = label_loc }) rest ->
let field_name = {Asttypes.txt = Longident.Lident (strip_option txt); loc = label_loc} in
(field_name, Exp.ident field_name) :: rest
)) None in
(* This is to support bs.optional, which makes certain args of the function optional so we
add a unit at the end to prevent auto-currying issues. *)
let body_with_extra_unit_fun = (if has_optional_field then
(Ast_compatible.fun_ ~loc:my_loc
(Pat.var ({txt = "()"; loc = my_loc})) maker_body)
else maker_body) in
let myMaker =
#if BS_NATIVE_PPX then
Str.value Nonrecursive [
Vb.mk
(Pat.var {loc; txt = type_name})
(Exp.constraint_ (
Ext_list.fold_right
labels
body_with_extra_unit_fun
(fun arg_name rest ->
(Ast_compatible.label_fun ~label:arg_name.Asttypes.txt ~loc:my_loc
(Pat.var ({arg_name with txt = strip_option arg_name.Asttypes.txt})) rest))
) makeType)
]
#else assert false
#end
in
(myMaker :: setter_accessor))
| Ptype_abstract
| Ptype_variant _
| Ptype_open ->
(* Looks obvious that it does not make sense to warn *)
(* U.notApplicable tdcl.ptype_loc derivingName; *)
tdcl, []
let code_sig_transform sigi = match sigi with
| {Parsetree.pstr_loc; pstr_desc =
Pstr_value (_, (({
pvb_pat = {ppat_desc = Ppat_var name};
pvb_expr = {pexp_desc = Pexp_constraint (_, typ)}
} as _makerVb) :: []))
} ->
Sig.value (Val.mk ~loc:pstr_loc name typ)
| _ ->
#if BS_NATIVE_PPX then
Sig.type_ Nonrecursive []
#else assert false
#end
let handleTdclsInStr ~light rf tdcls =
let tdcls, tdcls_sig, code, code_sig =
Ext_list.fold_right tdcls ([],[], [], []) (fun tdcl (tdcls, tdcls_sig, sts, code_sig) ->
match handleTdcl light tdcl with
ntdcl, value_descriptions ->
let open Parsetree in
(
ntdcl::tdcls,
{ntdcl with ptype_kind = Ptype_abstract }::tdcls_sig,
Ext_list.map_append value_descriptions sts (fun x -> x),
Ext_list.map_append value_descriptions code_sig code_sig_transform
)
) in
(Ast_compatible.rec_type_str rf tdcls :: code,
Ast_compatible.rec_type_sig rf tdcls_sig :: code_sig)
(* still need perform transformation for non-abstract type*)
let handleTdclsInSig ~light rf tdcls =
let tdcls_sig, code =
Ext_list.fold_right tdcls ([], []) (fun tdcl (tdcls_sig, sts) ->
match handleTdcl light tdcl with
ntdcl, value_descriptions ->
let open Parsetree in
(
{ntdcl with ptype_kind = Ptype_abstract }::tdcls_sig,
Ext_list.map_append value_descriptions sts code_sig_transform
)
) in
Ast_compatible.rec_type_sig rf tdcls_sig :: code