package melange

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

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
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * In addition to the permissions granted to you by the LGPL, you may combine
 * or link a "work that uses the Library" with a publicly distributed version
 * of this file to produce a combined library or application, then distribute
 * that combined work under the terms of your choosing, with no requirement
 * to comply with the obligations normally placed on you by section 4 of the
 * LGPL version 3 (or the corresponding section of a later version of the LGPL
 * should you choose to use a later version).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Ppxlib

type attr = Parsetree.attribute
type t = attr list
type ('a, 'b) st = { get : 'a option; set : 'b option }

let assert_bool_lit (e : Parsetree.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
        "expect `true` or `false` in this field"

let warn_if_bs ~loc txt =
  match txt with
  | "bs" -> Bs_ast_invariant.warn ~loc Deprecated_uncurry_attribute
  | other ->
      if String.starts_with ~prefix:"bs." other then
        Bs_ast_invariant.warn ~loc Deprecated_attribute_namespace

let process_method_attributes_rev (attrs : t) =
  let exception Local of string in
  try
    let ret =
      List.fold_left
        (fun (st, acc)
             ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
          warn_if_bs ~loc txt;
          match txt with
          | "mel.get" | "bs.get" | "get" (* @bs.get{null; undefined}*) ->
              let result =
                match Ast_payload.ident_or_record_as_config payload with
                | Error s -> raise (Local s)
                | Ok config ->
                    List.fold_left
                      (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)
                      (false, false) config
              in

              ({ st with get = Some result }, acc)
          | "mel.set" | "bs.set" | "set" ->
              let result =
                match Ast_payload.ident_or_record_as_config payload with
                | Error s -> raise (Local s)
                | Ok config ->
                    List.fold_left
                      (fun _st ({ txt; loc }, opt_expr) ->
                        (*FIXME*)
                        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)
                      `Get config
              in
              (* properties -- void
                    [@@set{only}]
              *)
              ({ st with set = Some result }, acc)
          | _ -> (st, attr :: acc))
        ({ get = None; set = None }, [])
        attrs
    in
    Ok ret
  with Local s -> Error s

type attr_kind =
  | Nothing
  | Meth_callback of attr
  | Uncurry of attr
  | Method of attr

let process_attributes_rev (attrs : t) : attr_kind * t =
  List.fold_left
    (fun (st, acc) ({ attr_name = { txt; loc }; _ } as attr) ->
      warn_if_bs ~loc txt;
      match (txt, st) with
      | ("u" | "bs"), (Nothing | Uncurry _) ->
          (Uncurry attr, acc) (* TODO: warn unused/duplicated attribute *)
      | ("mel.this" | "bs.this" | "this"), (Nothing | Meth_callback _) ->
          (Meth_callback attr, acc)
      | ("mel.meth" | "bs.meth" | "meth"), (Nothing | Method _) ->
          (Method attr, acc)
      | ("u" | "bs" | "mel.this" | "bs.this" | "this"), _ ->
          Error.err ~loc Conflict_u_mel_this_mel_meth
      | _, _ -> (st, attr :: acc))
    (Nothing, []) attrs

let process_pexp_fun_attributes_rev (attrs : t) =
  List.fold_left
    (fun (st, acc) ({ attr_name = { txt; loc }; _ } as attr) ->
      warn_if_bs ~loc txt;
      match txt with
      | "mel.open" | "bs.open" -> (true, acc)
      | _ -> (st, attr :: acc))
    (false, []) attrs

let process_bs (attrs : t) =
  List.fold_left
    (fun (st, acc) ({ attr_name = { txt; loc }; _ } as attr) ->
      warn_if_bs ~loc txt;
      match (txt, st) with
      | ("u" | "bs"), _ -> (true, acc)
      | _, _ -> (st, attr :: acc))
    (false, []) attrs

let is_bs (attr : attr) =
  match attr with
  | { attr_name = { Location.txt = "u" | "bs"; _ }; _ } -> true
  | _ -> false

let bs_get : attr =
  {
    attr_name = { txt = "mel.get"; loc = Location.none };
    attr_payload = Parsetree.PStr [];
    attr_loc = Location.none;
  }

let bs_get_index : attr =
  {
    attr_name = { txt = "mel.get_index"; loc = Location.none };
    attr_payload = Parsetree.PStr [];
    attr_loc = Location.none;
  }

let bs_get_arity : attr =
  {
    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 bs_set : attr =
  {
    attr_name = { txt = "mel.set"; loc = Location.none };
    attr_payload = PStr [];
    attr_loc = Location.none;
  }

let internal_expansive : attr =
  {
    attr_name = { txt = "internal.expansive"; loc = Location.none };
    attr_payload = PStr [];
    attr_loc = Location.none;
  }

let bs_return_undefined : attr =
  {
    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_bs_string_or_int_as (attrs : Parsetree.attributes) =
  let st = ref None in
  List.iter
    (fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
      match txt with
      | "mel.as" | "bs.as" | "as" ->
          if !st = None then (
            Bs_ast_invariant.mark_used_bs_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
                         Classify_function.classify
                           ~check:
                             (pexp_loc, Bs_flow_ast_utils.flow_deli_offset dec)
                           s
                       with
                      | Js_literal _ -> ()
                      | _ ->
                          Location.raise_errorf ~loc:pexp_loc
                            "an object literal expected");
                      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

(* duplicated @uncurry @string not allowed,
   it is worse in @uncurry since it will introduce
   inconsistency in arity
*)
let iter_process_bs_string_int_unwrap_uncurry (attrs : t) =
  let st = ref `Nothing in
  let assign v ({ attr_name = { loc; _ }; _ } as attr : attr) =
    if !st = `Nothing then (
      Bs_ast_invariant.mark_used_bs_attribute attr;
      st := v)
    else Error.err ~loc Conflict_attributes
  in
  List.iter
    (fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
      warn_if_bs ~loc txt;
      match txt with
      | "mel.string" | "bs.string" | "string" -> assign `String attr
      | "mel.int" | "bs.int" | "int" -> assign `Int attr
      | "mel.ignore" | "bs.ignore" | "ignore" -> assign `Ignore attr
      | "mel.unwrap" | "bs.unwrap" | "unwrap" -> assign `Unwrap attr
      | "mel.uncurry" | "bs.uncurry" | "uncurry" ->
          assign (`Uncurry (Ast_payload.is_single_int payload)) attr
      | _ -> ())
    attrs;
  !st

let iter_process_bs_string_as (attrs : t) : string option =
  let st = ref None in
  List.iter
    (fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
      match txt with
      | "mel.as" | "bs.as" | "as" ->
          if !st = None then (
            match Ast_payload.is_single_string payload with
            | None -> Error.err ~loc Expect_string_literal
            | Some (v, _dec) ->
                Bs_ast_invariant.mark_used_bs_attribute attr;
                st := Some v)
          else Error.err ~loc Duplicated_mel_as
      | _ -> ())
    attrs;
  !st

let external_attrs =
  [|
    "get";
    "set";
    "get_index";
    "return";
    "obj";
    "val";
    "module";
    "scope";
    "variadic";
    "send";
    "new";
    "set_index";
    Literals.gentype_import;
  |]

let first_char_special (x : string) =
  match String.unsafe_get x 0 with
  | '#' | '?' | '%' -> true
  | _ ->
      (* XXX(anmonteiro): Upstream considers "builtin" attributes ones that
         start with `?`. We keep the original terminology of `caml_` (and,
         incidentally, `nativeint_`). *)
      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 (* normal val declaration *)
  | 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 : t) pval_prim =
  match (attrs, pval_prim) with
  | _, [] -> false
  (* This is  val *)
  | [], _ ->
      (* No attributes found *)
      prims_to_be_encoded pval_prim
  | _, _ ->
      List.exists
        (fun { attr_name = { txt; loc = _ }; _ } ->
          String.starts_with txt ~prefix:"bs."
          || String.starts_with txt ~prefix:"mel."
          || Array.exists (fun (x : string) -> txt = x) external_attrs)
        attrs
      || prims_to_be_encoded pval_prim

let iter_process_bs_int_as (attrs : t) =
  let st = ref None in
  List.iter
    (fun ({ attr_name = { txt; loc }; attr_payload = payload; _ } as attr) ->
      warn_if_bs ~loc txt;
      match txt with
      | "mel.as" | "bs.as" | "as" ->
          if !st = None then (
            match Ast_payload.is_single_int payload with
            | None -> Error.err ~loc Expect_int_literal
            | Some _ as v ->
                Bs_ast_invariant.mark_used_bs_attribute attr;
                st := v)
          else Error.err ~loc Duplicated_mel_as
      | _ -> ())
    attrs;
  !st

let has_bs_optional (attrs : t) : bool =
  List.exists
    (fun ({ attr_name = { txt; loc }; _ } as attr) ->
      warn_if_bs ~loc txt;
      match txt with
      | "mel.optional" | "bs.optional" | "optional" ->
          Bs_ast_invariant.mark_used_bs_attribute attr;
          true
      | _ -> false)
    attrs

let is_inline : attr -> bool =
 fun { attr_name = { txt; loc }; _ } ->
  warn_if_bs ~loc txt;
  txt = "mel.inline" || txt = "bs.inline" || txt = "inline"

let has_inline_payload (attrs : t) = List.find_opt is_inline attrs

let is_mel_as : attr -> bool =
 fun { attr_name = { txt; loc }; _ } ->
  warn_if_bs ~loc txt;
  txt = "mel.as" || txt = "bs.as" || txt = "as"

let has_mel_as_payload (attrs : t) = List.find_opt is_mel_as attrs

(* We disable warning 61 in Melange externals since they're substantially
   different from OCaml externals. This warning doesn't make sense for a JS
   runtime *)
let unboxable_type_in_prim_decl : Parsetree.attribute =
  let open Ast_helper in
  {
    attr_name = { txt = "ocaml.warning"; loc = Location.none };
    attr_payload =
      PStr
        [
          Str.eval
            (Exp.constant
               (Pconst_string
                  ("-unboxable-type-in-prim-decl", Location.none, None)));
        ];
    attr_loc = Location.none;
  }
OCaml

Innovation. Community. Security.