Source file ast_attributes.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
open Import
type ('a, 'b) st = { get : 'a option; set : 'b option }
let assert_bool_lit (e : expression) =
match e.pexp_desc with
| Pexp_construct ({ txt = Lident "true"; _ }, None) -> true
| Pexp_construct ({ txt = Lident "false"; _ }, None) -> false
| _ ->
Location.raise_errorf ~loc:e.pexp_loc
"expected this expression to be a boolean literal (`true` or `false`)"
let error_if_bs_or_non_namespaced ~loc txt =
match txt with
| "bs" ->
Location.raise_errorf ~loc
"The `[@bs]' attribute has been removed in favor of `[@u]'."
| other ->
if
String.starts_with ~prefix:"bs." other
|| not (Mel_ast_invariant.is_mel_attribute txt)
then
Location.raise_errorf ~loc
"`[@bs.*]' and non-namespaced attributes have been removed in favor \
of `[@mel.*]' attributes."
let process_method_attributes_rev attrs =
let exception Local of Location.t * string in
try
let ret =
List.fold_left
~f:(fun
(st, acc)
({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr)
->
match txt with
| "mel.get" | "bs.get" | "get" ->
error_if_bs_or_non_namespaced ~loc txt;
let result =
match Ast_payload.ident_or_record_as_config payload with
| Error s -> raise (Local (loc, s))
| Ok config ->
List.fold_left
~f:(fun (null, undefined) ({ txt; loc }, opt_expr) ->
match txt with
| "null" ->
( (match opt_expr with
| None -> true
| Some e -> assert_bool_lit e),
undefined )
| "undefined" ->
( null,
match opt_expr with
| None -> true
| Some e -> assert_bool_lit e )
| "nullable" -> (
match opt_expr with
| None -> (true, true)
| Some e ->
let v = assert_bool_lit e in
(v, v))
| _ -> Error.err ~loc Unsupported_predicates)
~init:(false, false) config
in
({ st with get = Some result }, acc)
| "mel.set" | "bs.set" | "set" ->
error_if_bs_or_non_namespaced ~loc txt;
let result =
match Ast_payload.ident_or_record_as_config payload with
| Error s -> raise (Local (loc, s))
| Ok config ->
List.fold_left
~f:(fun _st ({ txt; loc }, opt_expr) ->
if txt = "no_get" then
match opt_expr with
| None -> `No_get
| Some e ->
if assert_bool_lit e then `No_get else `Get
else Error.err ~loc Unsupported_predicates)
~init:`Get config
in
({ st with set = Some result }, acc)
| _ -> (st, attr :: acc))
~init:({ get = None; set = None }, [])
attrs
in
Ok ret
with Local (loc, s) -> Error (loc, s)
type attr_kind =
| Nothing
| Meth_callback of attribute
| Uncurry of attribute
| Method of attribute
let process_attributes_rev attrs : attr_kind * attribute list =
List.fold_left
~f:(fun (st, acc) ({ attr_name = { txt; loc }; _ } as attr) ->
match (txt, st) with
| "u", (Nothing | Uncurry _) ->
(Uncurry attr, acc)
| ("mel.this" | "bs.this" | "this"), (Nothing | Meth_callback _) ->
error_if_bs_or_non_namespaced ~loc txt;
(Meth_callback attr, acc)
| ("mel.meth" | "bs.meth" | "meth"), (Nothing | Method _) ->
error_if_bs_or_non_namespaced ~loc txt;
(Method attr, acc)
| ("u" | "mel.this" | "this"), _ ->
Error.err ~loc Conflict_u_mel_this_mel_meth
| _, _ -> (st, attr :: acc))
~init:(Nothing, []) attrs
let process_pexp_fun_attributes_rev attrs =
List.fold_left
~f:(fun (st, acc) ({ attr_name = { txt; loc }; _ } as attr) ->
match txt with
| "mel.open" | "bs.open" ->
error_if_bs_or_non_namespaced ~loc txt;
(true, acc)
| _ -> (st, attr :: acc))
~init:(false, []) attrs
let process_uncurried attrs =
List.fold_left
~f:(fun (st, acc) ({ attr_name = { txt; _ }; _ } as attr) ->
match (txt, st) with "u", _ -> (true, acc) | _, _ -> (st, attr :: acc))
~init:(false, []) attrs
let is_uncurried attr =
match attr with
| { attr_name = { Location.txt = "u"; _ }; _ } -> true
| _ -> false
let mel_get =
{
attr_name = { txt = "mel.get"; loc = Location.none };
attr_payload = PStr [];
attr_loc = Location.none;
}
let mel_get_index =
{
attr_name = { txt = "mel.get_index"; loc = Location.none };
attr_payload = PStr [];
attr_loc = Location.none;
}
let mel_get_arity =
{
attr_name = { txt = "internal.arity"; loc = Location.none };
attr_payload =
PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_loc = Location.none;
pexp_loc_stack = [];
pexp_attributes = [];
pexp_desc =
Pexp_constant (Pconst_integer (string_of_int 1, None));
},
[] );
pstr_loc = Location.none;
};
];
attr_loc = Location.none;
}
let mel_set =
{
attr_name = { txt = "mel.set"; loc = Location.none };
attr_payload = PStr [];
attr_loc = Location.none;
}
let internal_expansive =
let internal_expansive_label = "internal.expansive" in
{
attr_name = { txt = internal_expansive_label; loc = Location.none };
attr_payload = PStr [];
attr_loc = Location.none;
}
let has_internal_expansive attrs =
List.exists
~f:(fun { attr_name = { txt; _ }; _ } -> txt = "internal.expansive")
attrs
let mel_return_undefined =
{
attr_name = { txt = "mel.return"; loc = Location.none };
attr_payload =
PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_desc =
Pexp_ident
{ txt = Lident "undefined_to_opt"; loc = Location.none };
pexp_loc = Location.none;
pexp_loc_stack = [];
pexp_attributes = [];
},
[] );
pstr_loc = Location.none;
};
];
attr_loc = Location.none;
}
type as_const_payload = Int of int | Str of string | Js_literal_str of string
let iter_process_mel_string_or_int_as (attrs : attributes) =
let st = ref None in
List.iter
~f:(fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
match txt with
| "mel.as" | "bs.as" | "as" ->
error_if_bs_or_non_namespaced ~loc txt;
if !st = None then (
Mel_ast_invariant.mark_used_mel_attribute attr;
match Ast_payload.is_single_int payload with
| None -> (
match payload with
| PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_desc =
Pexp_constant
(Pconst_string
(s, _, ((None | Some "json") as dec)));
pexp_loc;
_;
},
_ );
_;
};
] ->
if dec = None then st := Some (Str s)
else (
(match
Melange_ffi.Classify_function.classify
~check:
( pexp_loc,
Melange_ffi.Flow_ast_utils.flow_deli_offset dec
)
s
with
| Js_literal _ -> ()
| _ ->
Location.raise_errorf ~loc:pexp_loc
"`[@mel.as {json| ... |json}]' only supports \
JavaScript literals");
st := Some (Js_literal_str s))
| _ -> Error.err ~loc Expect_int_or_string_or_json_literal)
| Some v -> st := Some (Int v))
else Error.err ~loc Duplicated_mel_as
| _ -> ())
attrs;
!st
let iter_process_mel_string_int_unwrap_uncurry attrs =
let st = ref `Nothing in
let assign v ({ attr_name = { loc; _ }; _ } as attr) =
if !st = `Nothing then (
Mel_ast_invariant.mark_used_mel_attribute attr;
st := v)
else Error.err ~loc Conflict_attributes
in
List.iter
~f:(fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
match txt with
| "mel.string" | "bs.string" | "string" ->
error_if_bs_or_non_namespaced ~loc txt;
assign `String attr
| "mel.int" | "bs.int" | "int" ->
error_if_bs_or_non_namespaced ~loc txt;
assign `Int attr
| "mel.ignore" | "bs.ignore" | "ignore" ->
error_if_bs_or_non_namespaced ~loc txt;
assign `Ignore attr
| "mel.unwrap" | "bs.unwrap" | "unwrap" ->
error_if_bs_or_non_namespaced ~loc txt;
assign `Unwrap attr
| "mel.uncurry" | "bs.uncurry" | "uncurry" ->
error_if_bs_or_non_namespaced ~loc txt;
assign (`Uncurry (Ast_payload.is_single_int payload)) attr
| _ -> ())
attrs;
!st
let iter_process_mel_string_as attrs : string option =
let st = ref None in
List.iter
~f:(fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
match txt with
| "mel.as" | "bs.as" | "as" ->
error_if_bs_or_non_namespaced ~loc txt;
if !st = None then (
match Ast_payload.is_single_string payload with
| None -> Error.err ~loc Expect_string_literal
| Some (v, _dec) ->
Mel_ast_invariant.mark_used_mel_attribute attr;
st := Some v)
else Error.err ~loc Duplicated_mel_as
| _ -> ())
attrs;
!st
let first_char_special (x : string) =
match x with
| "" -> false
| _ -> (
match String.unsafe_get x 0 with
| '#' | '?' | '%' -> true
| _ ->
String.starts_with x ~prefix:"caml_"
|| String.starts_with x ~prefix:"nativeint_")
let first_marshal_char (x : string) = x <> "" && String.unsafe_get x 0 = '\132'
let prims_to_be_encoded (attrs : string list) =
match attrs with
| [] -> assert false
| x :: _ when first_char_special x -> false
| _ :: x :: _ when first_marshal_char x -> false
| _ -> true
(**
[@@inline]
let a = 3
[@@inline]
let a : 3
They are not considered externals, they are part of the language
*)
let rs_externals attrs pval_prim =
match (attrs, pval_prim) with
| _, [] -> false
| [], _ ->
prims_to_be_encoded pval_prim
| _, _ ->
Melange_ffi.External_ffi_attributes.has_mel_attributes
(List.map ~f:(fun { attr_name = { txt; _ }; _ } -> txt) attrs)
|| prims_to_be_encoded pval_prim
let iter_process_mel_int_as attrs =
let st = ref None in
List.iter
~f:(fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
match txt with
| "mel.as" | "bs.as" | "as" ->
error_if_bs_or_non_namespaced ~loc txt;
if !st = None then (
match Ast_payload.is_single_int payload with
| None -> Error.err ~loc Expect_int_literal
| Some _ as v ->
Mel_ast_invariant.mark_used_mel_attribute attr;
st := v)
else Error.err ~loc Duplicated_mel_as
| _ -> ())
attrs;
!st
let has_mel_optional attrs : bool =
List.exists
~f:(fun ({ attr_name = { txt; loc }; _ } as attr) ->
match txt with
| "mel.optional" | "bs.optional" | "optional" ->
error_if_bs_or_non_namespaced ~loc txt;
Mel_ast_invariant.mark_used_mel_attribute attr;
true
| _ -> false)
attrs
let is_inline : attribute -> bool =
fun { attr_name = { txt; loc }; _ } ->
match txt with
| "mel.inline" -> true
| "bs.inline" ->
error_if_bs_or_non_namespaced ~loc txt;
false
| _ -> false
let has_inline_payload attrs = List.find_opt ~f:is_inline attrs
let is_mel_as { attr_name = { txt; loc }; _ } =
match txt with
| "mel.as" -> true
| "bs.as" | "as" ->
error_if_bs_or_non_namespaced ~loc txt;
false
| _ -> false
let has_mel_as_payload attrs =
List.fold_left
~f:(fun (attrs, found) attr ->
match (is_mel_as attr, found) with
| true, None -> (attrs, Some attr)
| false, Some _ | false, None -> (attr :: attrs, found)
| true, Some _ ->
Location.raise_errorf ~loc:attr.attr_loc
"Duplicate `%@mel.as' attribute found")
~init:([], None) attrs
let ocaml_warning w =
{
attr_name = { txt = "ocaml.warning"; loc = Location.none };
attr_payload =
PStr
Ast_helper.
[ Str.eval (Exp.constant (Pconst_string (w, Location.none, None))) ];
attr_loc = Location.none;
}
let unboxable_type_in_prim_decl = ocaml_warning "-unboxable-type-in-prim-decl"
let = ocaml_warning "-ignored-extra-argument"