Source file ast_util.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
open Ast_helper
type label_exprs = (Longident.t Asttypes.loc * Parsetree.expression) list
let js_property loc obj (name : string) =
Parsetree.Pexp_send
( Ast_compatible.app1 ~loc
(Exp.ident ~loc
{
loc;
txt = Ldot (Ast_literal.Lid.js_oo, Literals.unsafe_downgrade);
})
obj,
{ loc; txt = name } )
let ocaml_obj_as_js_object loc (mapper : Ast_mapper.mapper)
(self_pat : Parsetree.pattern) (clfs : Parsetree.class_field list) =
let generate_val_method_pair loc (mapper : Ast_mapper.mapper)
(val_name : string Asttypes.loc) is_mutable =
let result = Typ.var ~loc val_name.txt in
( result,
Ast_compatible.object_field val_name [] result
::
(if is_mutable then
[
Ast_compatible.object_field
{ val_name with txt = val_name.txt ^ Literals.setter_suffix }
[]
(Ast_typ_uncurry.to_method_type loc mapper Nolabel result
(Ast_literal.type_unit ~loc ()));
]
else []) )
in
let ( (internal_label_attr_types : Parsetree.object_field list),
(public_label_attr_types : Parsetree.object_field list) ) =
Ext_list.fold_right clfs ([], [])
(fun
({ pcf_loc = loc } as x : Parsetree.class_field)
(label_attr_types, public_label_attr_types)
->
match x.pcf_desc with
| Pcf_method (label, public_flag, Cfk_concrete (Fresh, e)) -> (
match e.pexp_desc with
| Pexp_poly ({ pexp_desc = Pexp_fun (lbl, _, pat, e) }, None) ->
let method_type =
Ast_typ_uncurry.generate_arg_type x.pcf_loc mapper label.txt
lbl pat e
in
( Ast_compatible.object_field label [] method_type
:: label_attr_types,
if public_flag = Public then
Ast_compatible.object_field label [] method_type
:: public_label_attr_types
else public_label_attr_types )
| Pexp_poly (_, Some _) ->
Location.raise_errorf ~loc
"polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf ~loc
"Unsupported syntax, expect syntax like `method x () = x ` "
| _ -> Location.raise_errorf ~loc "Unsupported syntax in js object")
| Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, _)) ->
let _, label_attr =
generate_val_method_pair x.pcf_loc mapper label
(mutable_flag = Mutable)
in
( Ext_list.append label_attr label_attr_types,
public_label_attr_types )
| Pcf_val (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently")
in
let internal_obj_type =
Ast_core_type.make_obj ~loc internal_label_attr_types
in
let public_obj_type = Ast_core_type.make_obj ~loc public_label_attr_types in
let labels, label_types, exprs, _ =
Ext_list.fold_right clfs ([], [], [], false)
(fun (x : Parsetree.class_field) (labels, label_types, exprs, aliased) ->
match x.pcf_desc with
| Pcf_method (label, _public_flag, Cfk_concrete (Fresh, e)) -> (
match e.pexp_desc with
| Pexp_poly
(({ pexp_desc = Pexp_fun (ll, None, pat, e) } as f), None) ->
let alias_type =
if aliased then None else Some internal_obj_type
in
let label_type =
Ast_typ_uncurry.generate_method_type ?alias_type x.pcf_loc
mapper label.txt ll pat e
in
( label :: labels,
label_type :: label_types,
{
f with
pexp_desc =
(let f = Ast_pat.is_unit_cont pat ~yes:e ~no:f in
Ast_uncurry_gen.to_method_callback loc mapper Nolabel
self_pat f)
;
}
:: exprs,
true )
| Pexp_poly (_, Some _) ->
Location.raise_errorf ~loc
"polymorphic type annotation not supported yet"
| Pexp_poly (_, None) ->
Location.raise_errorf ~loc
"Unsupported syntax, expect syntax like `method x () = x ` "
| _ -> Location.raise_errorf ~loc "Unsupported syntax in js object")
| Pcf_val (label, mutable_flag, Cfk_concrete (Fresh, val_exp)) ->
let label_type, _ =
generate_val_method_pair x.pcf_loc mapper label
(mutable_flag = Mutable)
in
( label :: labels,
label_type :: label_types,
mapper.expr mapper val_exp :: exprs,
aliased )
| Pcf_val (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not support currently"
| Pcf_val (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtual flag not support currently"
| Pcf_method (_, _, Cfk_concrete (Override, _)) ->
Location.raise_errorf ~loc "override flag not supported"
| Pcf_method (_, _, Cfk_virtual _) ->
Location.raise_errorf ~loc "virtural method not supported"
| Pcf_inherit _ | Pcf_initializer _ | Pcf_attribute _ | Pcf_extension _
| Pcf_constraint _ ->
Location.raise_errorf ~loc "Only method support currently")
in
let pval_type =
Ext_list.fold_right2 labels label_types public_obj_type
(fun label label_type acc ->
Ast_compatible.label_arrow ~loc:label.Asttypes.loc label.Asttypes.txt
label_type acc)
in
Ast_extensions.Make.local_extern_cont_to_obj loc
~pval_prim:(Ast_external_process.pval_prim_of_labels labels)
(fun e ->
Ast_compatible.apply_labels ~loc e
(Ext_list.map2 labels exprs (fun l expr -> (l.txt, expr))))
~pval_type