package frama-c

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

Source file api_generator.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  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, version 2.1.                                              *)
(*                                                                        *)
(*  It 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.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

(* -------------------------------------------------------------------------- *)
(* --- Frama-C TypeScript API Generator                                   --- *)
(* -------------------------------------------------------------------------- *)

module Self = Plugin.Register
    (struct
      let name = "Server TypeScript API"
      let shortname = "server-tsc"
      let help = "Generate TypeScript API for Server"
    end)

module TSC = Self.Action
    (struct
      let option_name = "-server-tsc"
      let help = "Generate TypeScript API"
    end)

module OUT = Self.Filepath
    (struct
      let option_name = "-server-tsc-out"
      let arg_name = "path"
      let default = "src"
      let file_kind = "output-dir"
      let existence = Filepath.Indifferent
      let help = Printf.sprintf "Output directory (default is '%s')" default
    end)

module Md = Markdown
module Pkg = Server.Package

(* -------------------------------------------------------------------------- *)
(* --- TS Utils                                                           --- *)
(* -------------------------------------------------------------------------- *)

let keywords = [
  "break"; "case"; "catch"; "class"; "const"; "continue"; "debugger";
  "default"; "delete"; "do"; "else"; "enum"; "export"; "extends"; "false";
  "finally"; "for"; "function"; "if"; "import"; "in"; "instanceof"; "new";
  "null"; "return"; "super"; "switch"; "this"; "throw"; "true"; "try";
  "typeof"; "var"; "void"; "while"; "with"; "as"; "implements"; "interface";
  "let"; "package"; "private"; "protected"; "public"; "static"; "yield"; "any";
  "boolean"; "constructor"; "declare"; "get"; "module"; "require"; "number";
  "set"; "string"; "symbol"; "type"; "from"; "of";
  "Json"; "Compare"; "Server"; "State";
]

let pp_descr = Md.pp_text ?page:None

let name_of_kind = function
  | `GET -> "GET"
  | `SET -> "SET"
  | `EXEC -> "EXEC"

let makeDescr ?(indent="") fmt descr =
  if descr <> [] then
    Format.fprintf fmt "%s/** @[<hov 0>%a@] */@." indent pp_descr descr

let getSelf = function
  | None -> Self.fatal "Unexpected recursive type"
  | Some id -> id

(* -------------------------------------------------------------------------- *)
(* --- Jtype Generator                                                    --- *)
(* -------------------------------------------------------------------------- *)

let makeJtype ?self ~names =
  let open Pkg in
  let pp_ident fmt id =
    match IdMap.find id names with
    | name -> Format.pp_print_string fmt name
    | exception Not_found -> Self.abort "Undefined '%a'" pp_ident id in
  let rec pp fmt = function
    | Jany -> Format.pp_print_string fmt "Json.json"
    | Jself -> Format.pp_print_string fmt (getSelf self).name
    | Jnull -> Format.pp_print_string fmt "null"
    | Jnumber -> Format.pp_print_string fmt "number"
    | Jboolean -> Format.pp_print_string fmt "boolean"
    | Jstring | Jalpha -> Format.pp_print_string fmt "string"
    | Jtag a -> Format.fprintf fmt "\"%s\"" a
    | Jkey kd -> Format.fprintf fmt "Json.key<'#%s'>" kd
    | Jindex kd -> Format.fprintf fmt "Json.index<'#%s'>" kd
    | Jdict js -> Format.fprintf fmt "@[<hov 2>Json.dict<@,%a>@]" pp js
    | Jdata(id,_) | Jenum(id,_) -> pp_ident fmt id
    | Joption js -> Format.fprintf fmt "%a |@ undefined" pp js
    | Jtuple js ->
      Pretty_utils.pp_list ~pre:"@[<hov 2>[ " ~sep:",@ " ~suf:"@ ]@]" pp fmt js
    | Junion js ->
      Pretty_utils.pp_list ~pre:"@[<hov 0>" ~sep:" |@ " ~suf:"@]" protect fmt js
    | Jrecord fjs ->
      Pretty_utils.pp_list ~pre:"@[<hov 2>{ " ~sep:",@ " ~suf:"@ }@]" field fmt fjs
    | Jarray js -> Format.fprintf fmt "%a[]" protect js
  and protect fmt js = match js with
    | Junion _ | Joption _ -> Format.fprintf fmt "@[<hov 2>(%a)@]" pp js
    | _ -> pp fmt js
  and field fmt (fd,js) =
    match js with
    | Joption js ->
      Format.fprintf fmt "@[<hov 4>%s?:@ %a@]" fd pp js
    | _ ->
      Format.fprintf fmt "@[<hov 4>%s:@ %a@]" fd pp js
  in pp

(* -------------------------------------------------------------------------- *)
(* --- Jtype Decoder                                                      --- *)
(* -------------------------------------------------------------------------- *)

let jprim fmt name = Format.fprintf fmt "Json.%s" name
let jkey fmt kd = Format.fprintf fmt "Json.jKey<'#%s'>('#%s')" kd kd
let jindex fmt kd = Format.fprintf fmt "Json.jIndex<'#%s'>('#%s')" kd kd

let jcall names fmt id =
  try Format.pp_print_string fmt (Pkg.IdMap.find id names)
  with Not_found -> Self.abort "Undefined identifier '%a'" Pkg.pp_ident id

let jenum names fmt id = Format.fprintf fmt "Json.jEnum(%a)" (jcall names) id

let junion ~jtype ~make fmt jts =
  begin
    Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.jUnion<%a>("
      jtype (Pkg.Junion jts) ;
    List.iter
      (fun js -> Format.fprintf fmt "@ @[<hov 2>%a@]," make js) jts ;
    Format.fprintf fmt "@]@,)@]" ;
  end

let jrecord ~make fmt jts =
  begin
    Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.jObject({" ;
    List.iter
      (fun (fd,js) ->
         Format.fprintf fmt "@ @[<hov 2>%s: %a@]," fd make js) jts ;
    Format.fprintf fmt "@]@,})@]" ;
  end

let jtuple ~make fmt jts =
  begin
    let name = match List.length jts with
      | 2 -> "jPair"
      | 3 -> "jTriple"
      | 4 -> "jTuple4"
      | 5 -> "jTuple5"
      | n -> Self.fatal "No jTuple%d defined" n
    in
    Format.fprintf fmt "@[<hv 0>@[<hv 2>Json.%s(" name ;
    List.iter
      (fun js -> Format.fprintf fmt "@ @[<hov 2>%a@]," make js) jts ;
    Format.fprintf fmt "@]@,)@]" ;
  end

let rec makeDecoder ?self ~names fmt js =
  let open Pkg in
  let make = makeDecoder ?self ~names in
  match js with
  | Jany -> jprim fmt "jAny"
  | Jnull -> jprim fmt "jNull"
  | Jboolean -> jprim fmt "jBoolean"
  | Jnumber -> jprim fmt "jNumber"
  | Jstring | Jalpha -> jprim fmt "jString"
  | Jtag a -> Format.fprintf fmt "Json.jTag(\"%s\")" a
  | Jkey kd -> jkey fmt kd
  | Jindex kd -> jindex fmt kd
  | Jdata(id,_) -> jcall names fmt (Pkg.Derived.decode id)
  | Jenum(id,_) -> jenum names fmt id
  | Jself -> jcall names fmt (Pkg.Derived.decode (getSelf self))
  | Joption js ->
    Format.fprintf fmt "@[<hov 2>Json.jOption(@,%a)@]" make js
  | Jdict js ->
    Format.fprintf fmt "@[<hov 2>Json.jDict(@,%a)@]" make js
  | Jarray js ->
    Format.fprintf fmt "@[<hov 2>Json.jArray(@,%a)@]" make js
  | Junion jts ->
    let jtype = makeJtype ?self ~names in
    junion ~jtype ~make fmt jts
  | Jrecord jfs -> jrecord ~make fmt jfs
  | Jtuple jts -> jtuple ~make fmt jts


(* -------------------------------------------------------------------------- *)
(* --- Parameter Decoder                                                  --- *)
(* -------------------------------------------------------------------------- *)

let typeOfParam = function
  | Pkg.P_value js -> js
  | Pkg.P_named fjs -> Jrecord (List.map Pkg.field fjs)

(* -------------------------------------------------------------------------- *)
(* --- Jtype Order                                                        --- *)
(* -------------------------------------------------------------------------- *)

let makeOrder ~self ~names fmt js =
  let open Pkg in
  let rec pp fmt = function
    | Jnull -> Format.pp_print_string fmt "Compare.equal"
    | Jalpha -> Format.pp_print_string fmt "Compare.alpha"
    | Jnumber | Jindex _ -> Format.pp_print_string fmt "Compare.number"
    | Jstring | Jkey _ -> Format.pp_print_string fmt "Compare.string"
    | Jboolean -> Format.pp_print_string fmt "Compare.boolean"
    | Jself -> jcall names fmt (Pkg.Derived.order self)
    | Jdata(id,_) -> jcall names fmt (Pkg.Derived.order id)
    | Joption js ->
      Format.fprintf fmt "@[<hov 2>Compare.defined(@,%a)@]" pp js
    | Jenum(id,_) ->
      Format.fprintf fmt "@[<hov 2>Compare.byEnum(@,%a)@]" (jcall names) id
    | Jarray js ->
      Format.fprintf fmt "@[<hov 2>Compare.array(@,%a)@]" pp js
    | Jtuple jts ->
      let name = match List.length jts with
        | 2 -> "pair"
        | 3 -> "triple"
        | 4 -> "tuple4"
        | 5 -> "tuple5"
        | n -> Self.abort "No comparison for %d-tuples" n in
      Format.fprintf fmt "@[<hv 0>@[<hv 2>Compare.%s(" name ;
      List.iter (fun js -> Format.fprintf fmt "@,%a," pp js) jts ;
      Format.fprintf fmt "@]@,)@]" ;
    | Jrecord jfs ->
      Format.fprintf fmt "@[<hv 0>@[<hv 2>Compare.byFields@,<%a>({"
        (makeJtype ~self ~names) (Jrecord jfs) ;
      List.iter
        (fun (fd,js) -> Format.fprintf fmt "@ @[<hov 2>%s: %a,@]" fd pp js) jfs ;
      Format.fprintf fmt "@]@ })@]" ;
    | Jdict js ->
      let jtype fmt js = makeJtype ~names fmt js in
      Format.fprintf fmt
        "@[<hov 2>Compare.dictionary<@,Json.dict<%a>>(@,%a)@]"
        jtype js pp js
    | Jany | Junion _ | Jtag _ ->
      Format.fprintf fmt "Compare.structural"
  in pp fmt js

(* -------------------------------------------------------------------------- *)
(* --- Jtype Order                                                        --- *)
(* -------------------------------------------------------------------------- *)

let rec atomic js =
  let open Pkg in
  match js with
  | Junion js -> List.exists atomic js
  | Jany | Jnull | Jnumber | Jboolean | Jstring | Jalpha
  | Jtag _ | Jkey _ | Jindex _ | Jdict _ | Joption _ | Jarray _ | Jenum _
    -> true
  | Jself | Jtuple _ | Jrecord _ | Jdata _
    -> false

let makeDefault ~names fmt js =
  let open Pkg in
  let rec pp fmt = function
    | Junion [] -> failwith "empty union type"
    | Jenum(_,[]) -> failwith "empty enum type"
    | Jself -> failwith "recursive default value"
    | Jany | Jnull -> Format.pp_print_string fmt "null"
    | Jnumber -> Format.pp_print_string fmt "0"
    | Jboolean -> Format.pp_print_string fmt "false"
    | Jstring | Jalpha -> Format.pp_print_string fmt "''"
    | Jtag a -> Format.fprintf fmt "\"%s\"" a
    | Jkey kd -> Format.fprintf fmt "%a('')" jkey kd
    | Jindex kd -> Format.fprintf fmt "%a(-1)" jindex kd
    | Jdict _ -> Format.fprintf fmt "{}"
    | Jarray _ -> Format.fprintf fmt "[]"
    | Joption _ -> Format.fprintf fmt "undefined"
    | Jtuple js ->
      Pretty_utils.pp_list ~pre:"@[<hov 2>[ " ~sep:",@ " ~suf:"@ ]@]"
        pp fmt js
    | Jrecord js ->
      Pretty_utils.pp_list ~pre:"@[<hov 2>{ " ~sep:",@ " ~suf:"@ }@]"
        pp_field fmt js
    | Junion js ->
      (try pp fmt (List.find atomic js) with Not_found -> pp fmt (List.hd js))
    | Jdata(id,_) -> jcall names fmt (Pkg.Derived.default id)
    | Jenum(id,tag::_) -> Format.fprintf fmt "%a.%s" (jcall names) id tag
  and pp_field fmt (fd,js) = Format.fprintf fmt "%s: %a" fd pp js
  in pp fmt js

(* -------------------------------------------------------------------------- *)
(* --- Declaration Generator                                              --- *)
(* -------------------------------------------------------------------------- *)

let makeRecursive fn fmt js =
  if Pkg.isRecursive js then
    Format.fprintf fmt "(_x: any) => %a(_x)" fn js
  else fn fmt js

let makeRecursive2 fn fmt js =
  if Pkg.isRecursive js then
    Format.fprintf fmt "(_x: any, _y: any) => %a(_x,_y)" fn js
  else fn fmt js

let makeDeclaration fmt names d =
  let open Pkg in
  Format.pp_print_newline fmt () ;
  let self = d.d_ident in
  let jtype = makeJtype ~self ~names in
  match d.d_kind with

  | D_type js ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt "@[<hv 2>export type %s =@ %a;@]@\n" self.name jtype js ;

  | D_record fjs ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt "export interface %s {@\n" self.name ;
    List.iter
      (fun { fd_name = fd ; fd_type = js ; fd_descr = doc } ->
         makeDescr ~indent:"  " fmt doc ;
         match js with
         | Joption js ->
           Format.fprintf fmt "  @[<hov 2>%s?: %a;@]@\n" fd jtype js
         | _ ->
           Format.fprintf fmt "  @[<hov 2>%s: %a;@]@\n" fd jtype js
      ) fjs ;
    Format.fprintf fmt "}@\n"

  | D_enum tgs ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt "export enum %s {@\n" self.name ;
    List.iter
      (fun { tg_name = tag ; tg_descr = doc } ->
         makeDescr ~indent:"  " fmt doc ;
         Format.fprintf fmt "  %s = '%s',@\n" tag tag ;
      ) tgs ;
    Format.fprintf fmt "}@\n"

  | D_signal ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt "export const %s: Server.Signal = {@\n" self.name ;
    Format.fprintf fmt "  name: '%s',@\n" (Pkg.name_of_ident d.d_ident) ;
    Format.fprintf fmt "};@\n"

  | D_request rq ->
    let kind = name_of_kind rq.rq_kind in
    let prefix = String.capitalize_ascii (String.lowercase_ascii kind) in
    let input = typeOfParam rq.rq_input in
    let output = typeOfParam rq.rq_output in
    let makeParam fmt js = makeDecoder ~names fmt js in
    Format.fprintf fmt
      "@[<hv 2>const %s_internal: Server.%sRequest<@,%a,@,%a@,>@] = {@\n"
      self.name prefix jtype input jtype output ;
    Format.fprintf fmt "  kind: Server.RqKind.%s,@\n" kind ;
    Format.fprintf fmt "  name: '%s',@\n" (Pkg.name_of_ident d.d_ident) ;
    Format.fprintf fmt "  input: %a,@\n" makeParam input ;
    Format.fprintf fmt "  output: %a,@\n" makeParam output ;
    Format.fprintf fmt "  signals: %a,@\n"
      (Pretty_utils.pp_list
         ~empty:"[]" ~pre:"@[<hov 2>[ " ~sep:",@ " ~suf:"@ ]@]"
         (fun fmt s -> Format.fprintf fmt "{ name: '%s' }" s))
      rq.rq_signals;
    Format.fprintf fmt "};@\n" ;
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hv 2>export const %s: Server.%sRequest<@,%a,@,%a@,>@]\
       = %s_internal;@\n"
      self.name prefix jtype input jtype output self.name ;

  | D_value js ->
    Format.fprintf fmt
      "@[<hv 2>const %s_internal: State.Value<@,%a@,>@] = {\n"
      self.name jtype js ;
    Format.fprintf fmt "  name: '%s',@\n" (Pkg.name_of_ident self) ;
    Format.fprintf fmt "  signal: %a,@\n"
      (jcall names) (Pkg.Derived.signal self) ;
    Format.fprintf fmt "  getter: %a,@\n"
      (jcall names) (Pkg.Derived.getter self) ;
    Format.fprintf fmt "};@\n" ;
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hv 2>export const %s: State.Value<@,%a@,>@] = %s_internal;\n"
      self.name jtype js self.name ;

  | D_state js ->
    Format.fprintf fmt
      "@[<hv 2>const %s_internal: State.State<@,%a@,>@] = {@\n"
      self.name jtype js ;
    Format.fprintf fmt "  name: '%s',@\n" (Pkg.name_of_ident self) ;
    Format.fprintf fmt "  signal: %a,@\n"
      (jcall names) (Pkg.Derived.signal self) ;
    Format.fprintf fmt "  getter: %a,@\n"
      (jcall names) (Pkg.Derived.getter self) ;
    Format.fprintf fmt "  setter: %a,@\n"
      (jcall names) (Pkg.Derived.setter self) ;
    Format.fprintf fmt "};@\n" ;
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hv 2>export const %s: State.State<@,%a@,>@] = %s_internal;@\n"
      self.name jtype js self.name;


  | D_array { arr_key ; arr_kind = jkey ; arr_rows = jrow } ->
    let data = Pkg.Derived.data self in
    Format.fprintf fmt
      "@[<hv 2>const %s_internal: State.Array<@,%a,@,%a@,>@] = {@\n"
      self.name jtype jkey jtype jrow ;
    Format.fprintf fmt "  name: '%s',@\n" (Pkg.name_of_ident self) ;
    Format.fprintf fmt "  getkey: ((d:%a) => d.%s),@\n" jtype jrow arr_key ;
    Format.fprintf fmt "  signal: %a,@\n"
      (jcall names) (Pkg.Derived.signal self) ;
    Format.fprintf fmt "  fetch: %a,@\n"
      (jcall names) (Pkg.Derived.fetch self) ;
    Format.fprintf fmt "  reload: %a,@\n"
      (jcall names) (Pkg.Derived.reload self) ;
    Format.fprintf fmt "  order: %a,@\n"
      (jcall names) (Pkg.Derived.order data) ;
    Format.fprintf fmt "};@\n" ;
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hv 2>export const %s: State.Array<@,%a,@,%a@,>@] = %s_internal;@\n"
      self.name jtype jkey jtype jrow self.name ;

  | D_decoder(id,js) ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hov 2>@[<hv 0>export const %s: Json.Decoder<@,%a@,>@] =@ %a;@]\n"
      self.name (jcall names) id
      (makeRecursive (makeDecoder ~self:id ~names)) js

  | D_order(id,js) ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hov 2>@[<hv 0>export const %s: Compare.Order<@,%a@,>@] =@ %a;@]\n"
      self.name (jcall names) id
      (makeRecursive2 (makeOrder ~self:id ~names)) js

  | D_default(id,js) ->
    makeDescr fmt d.d_descr ;
    Format.fprintf fmt
      "@[<hov 2>@[<hv 0>export const %s:@ %a@] =@ %a;@]\n"
      self.name (jcall names) id (makeDefault ~names) js

(* -------------------------------------------------------------------------- *)
(* --- Declaration Ranking                                                --- *)
(* -------------------------------------------------------------------------- *)

type ranking = {
  mutable rank : int ;
  mutable mark : int Pkg.IdMap.t ;
  index : Pkg.declInfo Pkg.IdMap.t ;
}

let depends d =
  match d.Pkg.d_kind with
  | D_value _ ->
    let id = d.d_ident in
    [
      Pkg.Derived.signal id;
      Pkg.Derived.getter id;
    ]
  | D_state _ ->
    let id = d.d_ident in
    [
      Pkg.Derived.signal id;
      Pkg.Derived.getter id;
      Pkg.Derived.setter id;
    ]
  | D_array _ ->
    let id = d.d_ident in
    let data = Pkg.Derived.data id in
    [
      data ;
      Pkg.Derived.decode data ;
      Pkg.Derived.order data ;
      Pkg.Derived.signal id ;
      Pkg.Derived.reload id ;
      Pkg.Derived.fetch id ;
    ]
  | _ -> []

let next m id =
  let r = m.rank in
  m.mark <- Pkg.IdMap.add id r m.mark ;
  m.rank <- succ r

let rec mark m d =
  let id = d.Pkg.d_ident in
  if not (Pkg.IdMap.mem id m.mark) then
    ( List.iter (mark_id m) (depends d) ; next m id )

and mark_id m id =
  try mark m (Pkg.IdMap.find id m.index)
  with Not_found -> ()

let ranking ds =
  let index = List.fold_left
      (fun m d -> Pkg.IdMap.add d.Pkg.d_ident d m)
      Pkg.IdMap.empty ds in
  let m = { rank = 0 ; mark = Pkg.IdMap.empty ; index } in
  List.iter (mark m) ds ;
  let rk = m.mark in
  let getRank a = try Pkg.IdMap.find a.Pkg.d_ident rk with Not_found -> 0 in
  List.sort (fun a b -> getRank a - getRank b) ds

(* -------------------------------------------------------------------------- *)
(* --- Package Generator                                                  --- *)
(* -------------------------------------------------------------------------- *)

let pkg_path ~plugin ~package =
  String.concat "/" @@
  let pkg = "api" :: package in
  "frama-c" ::
  match plugin with
  | Pkg.Kernel -> "kernel" :: pkg
  | Pkg.Plugin p -> "plugins" :: p :: pkg

let makeIgnore fmt msg =
  Format.fprintf fmt "//@ts-ignore@\n" ;
  Format.fprintf fmt msg

(* path shall be [pkg_path] of [pkg] *)
let makePackage pkg path fmt =
  begin
    let open Pkg in
    Format.fprintf fmt "/* --- Generated Frama-C Server API --- */@\n@\n" ;
    Format.fprintf fmt "/**@\n   %s@\n" pkg.p_title ;
    if pkg.p_descr <> [] then
      Format.fprintf fmt "@\n   @[<hov 0>%a@]@\n@\n" pp_descr pkg.p_descr ;
    Format.fprintf fmt "   @@packageDocumentation@\n" ;
    Format.fprintf fmt "   @@module %s@\n" path ;
    Format.fprintf fmt "*/@\n@." ;
    let names = Pkg.resolve ~keywords pkg in
    makeIgnore fmt "import * as Json from 'dome/data/json';@\n" ;
    makeIgnore fmt "import * as Compare from 'dome/data/compare';@\n" ;
    makeIgnore fmt "import * as Server from 'frama-c/server';@\n" ;
    makeIgnore fmt "import * as State from 'frama-c/states';@\n" ;
    Format.pp_print_newline fmt () ;
    Pkg.IdMap.iter
      (fun { name = iname ; plugin ; package } name ->
         if plugin <> pkg.p_plugin || package <> pkg.p_package
         then
           let path = pkg_path ~plugin ~package in
           if iname = name then
             makeIgnore fmt "import { %s } from '%s';@\n"
               name path
           else
             makeIgnore fmt "import { %s: %s } from '%s';@\n"
               iname name path
      ) names ;
    List.iter
      (makeDeclaration fmt names)
      (ranking pkg.p_content) ;
    Format.pp_print_newline fmt () ;
    Format.fprintf fmt "/* ------------------------------------- */@." ;
  end

(* -------------------------------------------------------------------------- *)
(* --- Main Generator                                                     --- *)
(* -------------------------------------------------------------------------- *)

let generate () =
  if TSC.get () then
    begin
      Pkg.iter
        begin fun pkg ->
          let path = pkg_path ~plugin:pkg.p_plugin ~package:pkg.p_package in
          Self.feedback "Package %s" path ;
          let out = OUT.get () in
          let dir = Filepath.Normalized.concat out path in
          let file = Filepath.Normalized.concat dir "index.ts" in
          ignore (Extlib.mkdir ~parents:true dir 0o755) ;
          Command.print_file file (makePackage pkg path) ;
        end
    end

let () =
  Boot.Main.extend generate

(* -------------------------------------------------------------------------- *)
OCaml

Innovation. Community. Security.