forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslateStructure.ml
337 lines (327 loc) · 13.1 KB
/
TranslateStructure.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
open GenTypeCommon
let rec addAnnotationsToTypes_ ~config ~(expr : Typedtree.expression)
(argTypes : argType list) =
match (expr.exp_desc, expr.exp_type.desc, argTypes) with
| _, _, {aName; aType = GroupOfLabeledArgs fields} :: nextTypes ->
let fields1, nextTypes1 =
addAnnotationsToFields ~config expr fields nextTypes
in
{aName; aType = GroupOfLabeledArgs fields1} :: nextTypes1
| Texp_function {param; cases = [{c_rhs}]}, _, {aType} :: nextTypes ->
let nextTypes1 = nextTypes |> addAnnotationsToTypes_ ~config ~expr:c_rhs in
let aName = Ident.name param in
{aName; aType} :: nextTypes1
| Texp_construct ({txt = Lident "Function$"}, _, [funExpr]), _, _ ->
(* let uncurried1: function$<_, _> = Function$(x => x |> string_of_int, [`Has_arity1]) *)
addAnnotationsToTypes_ ~config ~expr:funExpr argTypes
| Texp_apply ({exp_desc = Texp_ident (path, _, _)}, [(_, Some expr1)]), _, _
-> (
match path |> TranslateTypeExprFromTypes.pathToList |> List.rev with
| ["Js"; "Internal"; fn_mk]
when (* Uncurried function definition uses Js.Internal.fn_mkX(...) *)
String.length fn_mk >= 5 && String.sub fn_mk 0 5 = "fn_mk" ->
argTypes |> addAnnotationsToTypes_ ~config ~expr:expr1
| _ -> argTypes)
| _ -> argTypes
and addAnnotationsToTypes ~config ~(expr : Typedtree.expression)
(argTypes : argType list) =
let argTypes = addAnnotationsToTypes_ ~config ~expr argTypes in
if argTypes |> List.filter (fun {aName} -> aName = "param") |> List.length > 1
then
(* Underscore "_" appears as "param", can occur more than once *)
argTypes
|> List.mapi (fun i {aName; aType} ->
{aName = aName ^ "_" ^ string_of_int i; aType})
else argTypes
and addAnnotationsToFields ~config (expr : Typedtree.expression)
(fields : fields) (argTypes : argType list) =
match (expr.exp_desc, fields, argTypes) with
| _, [], _ -> ([], argTypes |> addAnnotationsToTypes ~config ~expr)
| Texp_function {cases = [{c_rhs}]}, field :: nextFields, _ ->
let nextFields1, types1 =
addAnnotationsToFields ~config c_rhs nextFields argTypes
in
let nameJS, nameRE =
TranslateTypeDeclarations.renameRecordField
~attributes:expr.exp_attributes ~nameRE:field.nameRE
in
({field with nameJS; nameRE} :: nextFields1, types1)
| _ -> (fields, argTypes)
(** Recover from expr the renaming annotations on named arguments. *)
let addAnnotationsToFunctionType ~config (expr : Typedtree.expression)
(type_ : type_) =
match type_ with
| Function function_ ->
let argTypes = function_.argTypes |> addAnnotationsToTypes ~config ~expr in
Function {function_ with argTypes}
| _ -> type_
let removeValueBindingDuplicates structureItems =
let rec processBindings (bindings : Typedtree.value_binding list) ~seen =
match bindings with
| ({vb_pat = {pat_desc = Tpat_var (id, _)}} as binding) :: otherBindings ->
let name = Ident.name id in
if !seen |> StringSet.mem name then otherBindings |> processBindings ~seen
else (
seen := !seen |> StringSet.add name;
binding :: (otherBindings |> processBindings ~seen))
| binding :: otherBindings ->
binding :: (otherBindings |> processBindings ~seen)
| [] -> []
in
let rec processItems (items : Typedtree.structure_item list) ~acc ~seen =
match items with
| ({Typedtree.str_desc = Tstr_value (loc, valueBindings)} as item)
:: otherItems ->
let bindings = valueBindings |> processBindings ~seen in
let item = {item with str_desc = Tstr_value (loc, bindings)} in
otherItems |> processItems ~acc:(item :: acc) ~seen
| item :: otherItems -> otherItems |> processItems ~acc:(item :: acc) ~seen
| [] -> acc
in
structureItems |> List.rev |> processItems ~acc:[] ~seen:(ref StringSet.empty)
let translateValueBinding ~config ~outputFileRelative ~resolver ~typeEnv
{Typedtree.vb_attributes; vb_expr; vb_pat} : Translation.t =
match vb_pat.pat_desc with
| Tpat_var (id, _) | Tpat_alias ({pat_desc = Tpat_any}, id, _) ->
let name = id |> Ident.name in
if !Debug.translation then Log_.item "Translate Value Binding %s\n" name;
let moduleItem = Runtime.newModuleItem ~name in
typeEnv |> TypeEnv.updateModuleItem ~moduleItem;
if vb_attributes |> Annotation.fromAttributes ~loc:vb_pat.pat_loc = GenType
then
id |> Ident.name
|> Translation.translateValue ~attributes:vb_attributes ~config
~docString:(Annotation.getDocString vb_attributes)
~outputFileRelative ~resolver ~typeEnv ~typeExpr:vb_pat.pat_type
~addAnnotationsToFunction:
(addAnnotationsToFunctionType ~config vb_expr)
else Translation.empty
| _ -> Translation.empty
let rec removeDuplicateValueBindings
(structureItems : Typedtree.structure_item list) =
match structureItems with
| ({Typedtree.str_desc = Tstr_value (loc, valueBindings)} as structureItem)
:: rest ->
let boundInRest, filteredRest = rest |> removeDuplicateValueBindings in
let valueBindingsFiltered =
valueBindings
|> List.filter (fun valueBinding ->
match valueBinding with
| {Typedtree.vb_pat = {pat_desc = Tpat_var (id, _)}} ->
not (boundInRest |> StringSet.mem (id |> Ident.name))
| _ -> true)
in
let bound =
valueBindings
|> List.fold_left
(fun bound (valueBinding : Typedtree.value_binding) ->
match valueBinding with
| {vb_pat = {pat_desc = Tpat_var (id, _)}} ->
bound |> StringSet.add (id |> Ident.name)
| _ -> bound)
boundInRest
in
( bound,
{structureItem with str_desc = Tstr_value (loc, valueBindingsFiltered)}
:: filteredRest )
| structureItem :: rest ->
let boundInRest, filteredRest = rest |> removeDuplicateValueBindings in
(boundInRest, structureItem :: filteredRest)
| [] -> (StringSet.empty, [])
let rec translateModuleBinding ~config ~outputFileRelative ~resolver ~typeEnv
({mb_id; mb_expr; mb_attributes} : Typedtree.module_binding) : Translation.t
=
let name = mb_id |> Ident.name in
if !Debug.translation then Log_.item "Translate Module Binding %s\n" name;
let moduleItem = Runtime.newModuleItem ~name in
typeEnv |> TypeEnv.updateModuleItem ~moduleItem;
let typeEnv = typeEnv |> TypeEnv.newModule ~name in
match mb_expr.mod_desc with
| Tmod_structure structure ->
let isLetPrivate =
mb_attributes |> Annotation.hasAttribute Annotation.tagIsInternLocal
in
if isLetPrivate then Translation.empty
else
structure
|> translateStructure ~config ~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Tmod_apply _ -> (
(* Only look at the resulting type of the module *)
match mb_expr.mod_type with
| Mty_signature signature ->
signature
|> TranslateSignatureFromTypes.translateSignatureFromTypes ~config
~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Mty_ident _ ->
logNotImplemented ("Mty_ident " ^ __LOC__);
Translation.empty
| Mty_functor _ ->
logNotImplemented ("Mty_functor " ^ __LOC__);
Translation.empty
| Mty_alias _ ->
logNotImplemented ("Mty_alias " ^ __LOC__);
Translation.empty)
| Tmod_unpack (_, moduleType) -> (
match moduleType with
| Mty_signature signature ->
signature
|> TranslateSignatureFromTypes.translateSignatureFromTypes ~config
~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Mty_ident path -> (
match typeEnv |> TypeEnv.lookupModuleTypeSignature ~path with
| None -> Translation.empty
| Some (signature, _) ->
signature
|> TranslateSignature.translateSignature ~config ~outputFileRelative
~resolver ~typeEnv
|> Translation.combine)
| Mty_functor _ ->
logNotImplemented ("Mty_functor " ^ __LOC__);
Translation.empty
| Mty_alias _ ->
logNotImplemented ("Mty_alias " ^ __LOC__);
Translation.empty)
| Tmod_ident (path, _) ->
let dep = path |> Dependencies.fromPath ~config ~typeEnv in
let internal = dep |> Dependencies.isInternal in
typeEnv |> TypeEnv.addModuleEquation ~dep ~internal;
Translation.empty
| Tmod_functor _ ->
logNotImplemented ("Tmod_functor " ^ __LOC__);
Translation.empty
| Tmod_constraint (_, Mty_ident path, Tmodtype_explicit _, Tcoerce_none) -> (
match typeEnv |> TypeEnv.lookupModuleTypeSignature ~path with
| None -> Translation.empty
| Some (signature, _) ->
signature
|> TranslateSignature.translateSignature ~config ~outputFileRelative
~resolver ~typeEnv
|> Translation.combine)
| Tmod_constraint
(_, Mty_signature signature, Tmodtype_explicit _, Tcoerce_none) ->
signature
|> TranslateSignatureFromTypes.translateSignatureFromTypes ~config
~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Tmod_constraint
( {mod_desc = Tmod_structure structure},
_,
Tmodtype_implicit,
Tcoerce_structure _ ) ->
{
structure with
str_items = structure.str_items |> removeDuplicateValueBindings |> snd;
}
|> translateStructure ~config ~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Tmod_constraint
( _,
_,
Tmodtype_explicit {mty_desc = Tmty_signature {sig_type = signature}},
_ ) ->
signature
|> TranslateSignatureFromTypes.translateSignatureFromTypes ~config
~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| Tmod_constraint _ ->
logNotImplemented ("Tmod_constraint " ^ __LOC__);
Translation.empty
and translateStructureItem ~config ~outputFileRelative ~resolver ~typeEnv
structItem : Translation.t =
match structItem with
| {Typedtree.str_desc = Typedtree.Tstr_type (recFlag, typeDeclarations)} ->
{
importTypes = [];
codeItems = [];
typeDeclarations =
typeDeclarations
|> TranslateTypeDeclarations.translateTypeDeclarations ~config
~outputFileRelative ~recursive:(recFlag = Recursive) ~resolver
~typeEnv;
}
| {Typedtree.str_desc = Tstr_value (_loc, valueBindings)} ->
valueBindings
|> List.map
(translateValueBinding ~config ~outputFileRelative ~resolver ~typeEnv)
|> Translation.combine
| {Typedtree.str_desc = Tstr_primitive valueDescription} ->
(* external declaration *)
valueDescription
|> Translation.translatePrimitive ~config ~outputFileRelative ~resolver
~typeEnv
| {Typedtree.str_desc = Tstr_module moduleBinding} ->
moduleBinding
|> translateModuleBinding ~config ~outputFileRelative ~resolver ~typeEnv
| {Typedtree.str_desc = Tstr_modtype moduleTypeDeclaration} ->
moduleTypeDeclaration
|> TranslateSignature.translateModuleTypeDeclaration ~config
~outputFileRelative ~resolver ~typeEnv
| {Typedtree.str_desc = Tstr_recmodule moduleBindings} ->
moduleBindings
|> List.map
(translateModuleBinding ~config ~outputFileRelative ~resolver ~typeEnv)
|> Translation.combine
| {
Typedtree.str_desc =
(* Bucklescript's encoding of bs.module: include with constraint. *)
Tstr_include
{
incl_mod =
{
mod_desc =
Tmod_constraint
( {
mod_desc =
Tmod_structure
{
str_items =
[({str_desc = Tstr_primitive _} as structItem1)];
};
},
_,
_,
_ );
};
_;
};
_;
} ->
structItem1
|> translateStructureItem ~config ~outputFileRelative ~resolver ~typeEnv
| {Typedtree.str_desc = Tstr_include {incl_type = signature}} ->
signature
|> TranslateSignatureFromTypes.translateSignatureFromTypes ~config
~outputFileRelative ~resolver ~typeEnv
|> Translation.combine
| {Typedtree.str_desc = Tstr_eval _} ->
logNotImplemented ("Tstr_eval " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_typext _} ->
logNotImplemented ("Tstr_typext " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_exception _} ->
logNotImplemented ("Tstr_exception " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_open _} ->
logNotImplemented ("Tstr_open " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_class _} ->
logNotImplemented ("Tstr_class " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_class_type _} ->
logNotImplemented ("Tstr_class_type " ^ __LOC__);
Translation.empty
| {Typedtree.str_desc = Tstr_attribute _} ->
logNotImplemented ("Tstr_attribute " ^ __LOC__);
Translation.empty
and translateStructure ~config ~outputFileRelative ~resolver ~typeEnv structure
: Translation.t list =
if !Debug.translation then Log_.item "Translate Structure\n";
structure.Typedtree.str_items |> removeValueBindingDuplicates
|> List.map (fun structItem ->
structItem
|> translateStructureItem ~config ~outputFileRelative ~resolver
~typeEnv)