Source file component_table.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
open Odoc_model
open Paths
open Odoc_model.Lang
open Components
open Odoc_model.Names
type ('a, 'b) tbl =
{ fresh: int -> ('a, 'b) tbl;
find: 'a -> 'b;
add: 'a -> 'b -> unit; }
let make_tbl (type a) (equal : (a -> a -> bool) option)
(hash : (a -> int) option) size =
let make create find add =
let rec fresh size =
let t = create size in
let find x = find t x in
let add x y = add t x y in
{fresh; find; add}
in
fresh size
in
match equal, hash with
| None, None ->
make (Hashtbl.create ?random:None) Hashtbl.find Hashtbl.add
| _ ->
let equal =
match equal with
| None -> (=)
| Some eq -> eq
in
let hash =
match hash with
| None -> Hashtbl.hash
| Some h -> h
in
let module Hash = struct
type t = a
let equal = equal
let hash = hash
end in
let module Tbl = Hashtbl.Make(Hash) in
make Tbl.create Tbl.find Tbl.add
type lookup_result_found = { root : Odoc_model.Root.t; hidden : bool }
type lookup_unit_result =
| Forward_reference
| Found of lookup_result_found
| Not_found
type t =
{ equal : (Root.t -> Root.t -> bool) option;
hash : (Root.t -> int) option;
lookup_unit : string -> lookup_unit_result;
lookup_page : string -> Root.t option;
fetch_unit : Root.t -> Compilation_unit.t;
fetch_page : Root.t -> Odoc_model.Lang.Page.t;
tbl : (Root.t, Sig.t) tbl;
page_tbl: (Root.t, Page.t) tbl; }
let create ?equal ?hash lookup_unit fetch_unit lookup_page fetch_page =
let tbl = make_tbl equal hash 7 in
let page_tbl = make_tbl equal hash 7 in
{equal; hash; lookup_unit; fetch_unit; lookup_page; fetch_page; tbl; page_tbl}
type local =
{ t : t;
local : (Identifier.Signature.t, Sig.t) tbl option;
base : Identifier.Signature.t option; }
let create_local t base =
let equal =
match t.equal with
| None -> None
| Some _equal -> Some Identifier.Signature.equal
in
let hash =
match t.hash with
| None -> None
| Some _hash -> Some Identifier.Signature.hash
in
let local =
match base with
| None -> None
| Some _ -> Some (make_tbl equal hash 23)
in
{ t; local; base; }
let add_local_module_identifier (local : local) id sg =
match local.local with
| None -> ()
| Some tbl -> tbl.add (id : Identifier.Module.t :> Identifier.Signature.t) sg
let add_local_module_type_identifier (local : local) id sg =
match local.local with
| None -> ()
| Some tbl -> tbl.add (id : Identifier.ModuleType.t :> Identifier.Signature.t) sg
let add_local_modules (local : local) id mds =
match local.local with
| None -> ()
| Some tbl ->
List.iter
(fun (name, sg) -> tbl.add (`Module(id, name)) sg)
mds
let add_local_module_types (local : local) id mtys =
match local.local with
| None -> ()
| Some tbl ->
List.iter
(fun (name, sg) -> tbl.add (`ModuleType(id, name)) sg)
mtys
let equals_signature _eq (base : Identifier.Signature.t) (id : Identifier.t) =
match id with
| `Root _ as id ->
Identifier.Signature.equal base id
| `Module _ as id ->
Identifier.Signature.equal base id
| `Argument _ as id ->
Identifier.Signature.equal base (id :> Identifier.Signature.t)
| `ModuleType _ as id ->
Identifier.Signature.equal base (id :> Identifier.Signature.t)
| `Page _ -> false
| `Type _ -> false
| `CoreType _ -> false
| `Constructor _ -> false
| `Field _ -> false
| `Extension _ -> false
| `Exception _ -> false
| `CoreException _ -> false
| `Value _ -> false
| `Class _ -> false
| `ClassType _ -> false
| `Method _ -> false
| `InstanceVariable _ -> false
| `Label _ -> false
let rec is_parent_local : _ -> _ -> Identifier.t -> bool =
fun eq base id ->
match id with
| `Root _ -> false
| `Page _ -> false
| `Module(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Argument(parent, _, _) -> is_local eq base (parent :> Identifier.t)
| `ModuleType(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Type(parent, _) -> is_local eq base (parent :> Identifier.t)
| `CoreType _ -> false
| `Constructor(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Field(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Extension(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Exception(parent, _) -> is_local eq base (parent :> Identifier.t)
| `CoreException _ -> false
| `Value(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Class(parent, _) -> is_local eq base (parent :> Identifier.t)
| `ClassType(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Method(parent, _) -> is_local eq base (parent :> Identifier.t)
| `InstanceVariable(parent, _) -> is_local eq base (parent :> Identifier.t)
| `Label(parent, _) -> is_local eq base (parent :> Identifier.t)
and is_local : _ -> _ -> Identifier.t -> bool =
fun eq base id ->
is_parent_local eq base id
|| equals_signature eq base id
let is_local local id =
match local.base with
| None -> false
| Some base ->
let eq =
match local.t.equal with
| None -> (=)
| Some eq -> eq
in
is_local eq base id
let local_module_identifier (local : local) id =
match local.local with
| None -> Sig.unresolved
| Some tbl ->
try
tbl.find (id : Identifier.Module.t :> Identifier.Signature.t)
with Not_found -> Sig.unresolved
let local_module_type_identifier (local : local) id =
match local.local with
| None -> Sig.unresolved
| Some tbl ->
try
tbl.find (id : Identifier.ModuleType.t :> Identifier.Signature.t)
with Not_found -> Sig.unresolved
let datatype decl =
let open TypeDecl in
let open Representation in
match decl.representation with
| None -> Datatype.abstract
| Some (Variant constructors) ->
let open Constructor in
let name = Identifier.name decl.id in
let decl =
Datatype.variant name
(List.map (fun cstr -> Identifier.name cstr.id) constructors)
in
let decl =
List.fold_right
(fun cstr decl ->
Datatype.add_documentation cstr.doc decl)
constructors decl
in
decl
| Some (Record fields) ->
let open Field in
let name = Identifier.name decl.id in
let decl =
Datatype.record name
(List.map (fun field -> Identifier.name field.id) fields)
in
let decl =
List.fold_right
(fun field decl ->
Datatype.add_documentation field.doc decl)
fields decl
in
decl
| Some Extensible -> Datatype.extensible
let core_types =
let open TypeDecl in
List.map
(fun decl -> (Identifier.name decl.id, datatype decl))
Predefined.core_types
let page tbl base =
try
tbl.page_tbl.find base
with Not_found ->
let page = tbl.fetch_page base in
let t = Page.of_doc page.Odoc_model.Lang.Page.content in
tbl.page_tbl.add base t;
t
let page_identifier tbl : Identifier.Page.t -> _ = function
| `Page(base, _) -> page tbl base
let rec unit tbl base =
try
tbl.tbl.find base
with Not_found ->
let open Compilation_unit in
let unt = tbl.fetch_unit base in
let id = (unt.id : Identifier.Module.t :> Identifier.Signature.t) in
let local = create_local tbl (Some id) in
let t =
match unt.content with
| Module items ->
Sig.signature
(fun items ->
Sig.add_documentation unt.doc
(signature_items local items))
items
| Pack items ->
Sig.signature
(fun items ->
Sig.add_documentation unt.doc
(packed_items local items))
items
in
let t = Sig.set_hidden t unt.hidden in
tbl.tbl.add base t;
t
and signature_identifier tbl (i : Identifier.Signature.t) =
match i with
| `Root(base, _) -> unit tbl base
| `Module(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_module (ModuleName.to_string name) parent
| `Argument(id, pos, _) ->
let parent = signature_identifier tbl id in
Sig.lookup_argument pos parent
| `ModuleType(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_module_type (ModuleTypeName.to_string name) parent
and module_identifier tbl (i : Identifier.Module.t) =
match i with
| `Root(base, _) -> unit tbl base
| `Module(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_module (ModuleName.to_string name) parent
| `Argument(id, pos, _) ->
let parent = signature_identifier tbl id in
Sig.lookup_argument pos parent
and module_type_identifier tbl (i : Identifier.ModuleType.t) =
match i with
| `ModuleType(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_module_type (ModuleTypeName.to_string name) parent
and datatype_identifier tbl (i : Identifier.DataType.t) =
match i with
| (`Type(id, name) : Identifier.Type.t)->
let parent = signature_identifier tbl id in
Sig.lookup_datatype (TypeName.to_string name) parent
| `CoreType name -> List.assoc (TypeName.to_string name) core_types
and class_signature_identifier tbl (p : Identifier.ClassSignature.t) =
match p with
| `Class(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_class_type (ClassName.to_string name) parent
| `ClassType(id, name) ->
let parent = signature_identifier tbl id in
Sig.lookup_class_type (ClassTypeName.to_string name) parent
and resolved_module_path local (p : Path.Resolved.Module.t) =
match p with
| `Identifier (id : Identifier.Module.t) ->
if is_local local (id :> Identifier.t) then local_module_identifier local id
else module_identifier local.t id
| `Subst(sub, _) -> resolved_module_type_path local sub
| `SubstAlias(sub, _) -> resolved_module_path local sub
| `Hidden p -> resolved_module_path local p
| `Module(p, name) ->
let parent = resolved_module_path local p in
Sig.lookup_module (ModuleName.to_string name) parent
| `Canonical (p, _) -> resolved_module_path local p
| `Apply(p, arg) ->
let parent = resolved_module_path local p in
Sig.lookup_apply (module_path local) arg parent
and resolved_module_type_path local (p : Path.Resolved.ModuleType.t) =
match p with
| `Identifier (id : Identifier.ModuleType.t) ->
if is_local local (id :> Identifier.t) then local_module_type_identifier local id
else module_type_identifier local.t id
| `ModuleType(p, name) ->
let parent = resolved_module_path local p in
Sig.lookup_module_type (ModuleTypeName.to_string name) parent
and resolved_class_type_path local (p : Path.Resolved.ClassType.t) =
match p with
| `Identifier id -> class_signature_identifier local.t id
| `Class(p, name) ->
let parent = resolved_module_path local p in
Sig.lookup_class_type (ClassName.to_string name) parent
| `ClassType(p, name) ->
let parent = resolved_module_path local p in
Sig.lookup_class_type (ClassTypeName.to_string name) parent
and module_path local (p : Path.Module.t) =
match p with
| `Root s -> begin
match local.t.lookup_unit s with
| Not_found ->
let sg = Sig.unresolved in
Sig.set_hidden sg (Root.contains_double_underscore s)
| Found {root;_} -> unit local.t root
| Forward_reference ->
let sg = Sig.abstract in
Sig.set_hidden sg (Root.contains_double_underscore s)
end
| `Forward s -> begin
match local.t.lookup_unit s with
| Not_found ->
let sg = Sig.unresolved in
Sig.set_hidden sg (Root.contains_double_underscore s)
| Found {root; _} -> unit local.t root
| Forward_reference ->
let sg = Sig.abstract in
Sig.set_hidden sg (Root.contains_double_underscore s)
end
| `Resolved r -> resolved_module_path local r
| `Dot(p, name) ->
let parent = module_path local p in
Sig.lookup_module name parent
| `Apply(p, arg) ->
let parent = module_path local p in
Sig.lookup_apply (module_path local) arg parent
and module_type_path local = function
| `Resolved r -> resolved_module_type_path local r
| `Dot(p, name) ->
let parent = module_path local p in
Sig.lookup_module_type name parent
and class_signature_path local = function
| `Resolved p -> resolved_class_type_path local p
| `Dot(p, name) ->
let parent = module_path local p in
Sig.lookup_class_type name parent
and class_signature_items local =
let open ClassSig in
let open ClassSignature in function
| InstanceVariable ivar :: rest ->
let open InstanceVariable in
let csig = class_signature_items local rest in
let csig = add_documentation ivar.doc csig in
let name = Identifier.name ivar.id in
add_element name `InstanceVariable csig
| Method meth :: rest ->
let open Method in
let csig = class_signature_items local rest in
let csig = add_documentation meth.doc csig in
let name = Identifier.name meth.id in
add_element name `Method csig
| Constraint _ :: rest ->
class_signature_items local rest
| Inherit expr :: rest ->
let csig = class_signature_items local rest in
let expr = class_type_expr local expr in
inherit_ expr csig
| Comment comment :: rest ->
let csig = class_signature_items local rest in
add_comment comment csig
| [] -> empty
and class_signature local csig =
let open ClassSignature in
class_signature_items local csig.items
and class_type_expr local =
let open ClassType in function
| Constr(p, _) -> ClassSig.constr (class_signature_path local) p
| Signature csig -> ClassSig.signature (class_signature local) csig
and class_decl local =
let open Class in function
| ClassType expr -> class_type_expr local expr
| Arrow(_, _, decl) -> class_decl local decl
and signature_items local =
let open Sig in
let open Signature in function
| Module (_, md) :: rest ->
let open Module in
let name = Identifier.name md.id in
let decl = module_decl local md.type_ in
let decl = set_canonical decl md.canonical in
let decl = set_hidden decl md.hidden in
add_local_module_identifier local md.id decl;
let sg = signature_items local rest in
let sg = add_documentation md.doc sg in
add_module name decl sg
| ModuleType mty :: rest ->
let open ModuleType in
let name = Identifier.name mty.id in
let expr =
match mty.expr with
| None -> abstract
| Some expr -> module_type_expr local expr
in
add_local_module_type_identifier local mty.id expr;
let sg = signature_items local rest in
let sg = add_documentation mty.doc sg in
add_module_type name expr sg
| Type (_, decl) :: rest ->
let open TypeDecl in
let sg = signature_items local rest in
let sg = add_documentation decl.doc sg in
let name = Identifier.name decl.id in
let decl = datatype decl in
add_datatype name decl sg
| TypExt ext :: rest ->
let open Extension in
let sg = signature_items local rest in
let sg = add_documentation ext.doc sg in
List.fold_right
(fun cstr acc ->
let open Constructor in
let name = Identifier.name cstr.id in
let acc = add_documentation cstr.doc acc in
add_element name `Extension acc)
ext.constructors sg
| Exception exn :: rest ->
let open Exception in
let sg = signature_items local rest in
let sg = add_documentation exn.doc sg in
let name = Identifier.name exn.id in
add_element name `Exception sg
| Value v :: rest ->
let open Value in
let sg = signature_items local rest in
let sg = add_documentation v.doc sg in
let name = Identifier.name v.id in
add_element name `Value sg
| External ev :: rest ->
let open External in
let sg = signature_items local rest in
let sg = add_documentation ev.doc sg in
let name = Identifier.name ev.id in
add_element name `Value sg
| Class (_, cl)::rest ->
let open Class in
let sg = signature_items local rest in
let sg = add_documentation cl.doc sg in
let name = Identifier.name cl.id in
let expr = class_decl local cl.type_ in
add_class name expr sg
| ClassType (_, clty)::rest ->
let open ClassType in
let sg = signature_items local rest in
let sg = add_documentation clty.doc sg in
let name = Identifier.name clty.id in
let expr = class_type_expr local clty.expr in
add_class_type name expr sg
| Include incl :: rest ->
let open Include in
let decl = module_decl local incl.decl in
add_local_modules local incl.parent (modules decl);
add_local_module_types local incl.parent (module_types decl);
let sg = signature_items local rest in
let sg = add_documentation incl.doc sg in
include_ decl sg
| Comment com :: rest ->
let sg = signature_items local rest in
add_comment com sg
| ModuleSubstitution mst :: rest ->
let open ModuleSubstitution in
let name = Identifier.name mst.id in
let decl = module_decl local (Alias mst.manifest) in
add_local_module_identifier local mst.id decl;
let sg = signature_items local rest in
let sg = add_documentation mst.doc sg in
add_module name decl sg
| TypeSubstitution _ :: rest ->
signature_items local rest
| [] -> empty
and module_type_expr local expr =
let open Sig in
let open ModuleType in
let open FunctorParameter in
match expr with
| Path p -> path (module_type_path local) p
| Signature sg -> signature (signature_items local) sg
| Functor(Named { id; expr = arg; _}, res) ->
let res = module_type_expr local res in
let arg = module_type_expr local arg in
functor_ local.t.equal local.t.hash id arg res
| Functor(Unit, res) ->
let res = module_type_expr local res in
generative res
| With(body, subs) ->
let body = module_type_expr local body in
List.fold_left
(fun body sub ->
match sub with
| ModuleEq(frag, decl) ->
let eq = module_decl local decl in
with_module frag eq body
| TypeEq _ -> body
| ModuleSubst(frag, _) ->
with_module_subst frag body
| TypeSubst(frag, _) ->
with_type_subst frag body)
body subs
| TypeOf decl -> module_decl local decl
and module_decl local decl =
let open Sig in
let open Module in
match decl with
| Alias p -> alias (module_path local) p
| ModuleType expr -> module_type_expr local expr
and packed_items local =
let open Sig in
let open Compilation_unit.Packed in function
| {id; path} :: rest ->
let name = Identifier.name id in
let decl = alias (module_path local) path in
add_local_module_identifier local id decl;
let sg = packed_items local rest in
add_module name decl sg
| [] -> empty
let resolved_module_path tbl p =
let local = create_local tbl None in
resolved_module_path local p
let resolved_module_type_path tbl p =
let local = create_local tbl None in
resolved_module_type_path local p
let resolved_class_type_path tbl p =
let local = create_local tbl None in
resolved_class_type_path local p
let module_path tbl p =
let local = create_local tbl None in
module_path local p
type with_ =
{ base: Sig.t;
tbl: t; }
let module_type_expr_with tbl id expr =
let local = create_local tbl (Some id) in
let base = module_type_expr local expr in
{ base; tbl }
let module_type_path_with tbl path =
let local = create_local tbl None in
let base = module_type_path local path in
{ base; tbl }
let rec resolved_signature_fragment wth (f : Fragment.Resolved.Signature.t) =
match f with
| `Root -> wth.base
| `Subst(sub, _) -> resolved_module_type_path wth.tbl sub
| `SubstAlias(sub, _) -> resolved_module_path wth.tbl sub
| `Module(p, name) ->
let parent = resolved_signature_fragment wth p in
Sig.lookup_module (ModuleName.to_string name) parent
let rec resolved_signature_reference tbl (r : Reference.Resolved.Signature.t) =
match r with
| `Identifier id ->
signature_identifier tbl id
| `SubstAlias(sub, _) ->
resolved_module_path tbl sub
| `Module(p, name) ->
let parent = resolved_signature_reference tbl p in
Sig.lookup_module (ModuleName.to_string name) parent
| `Canonical (p, _) ->
resolved_signature_reference tbl (p : Reference.Resolved.Module.t :> Reference.Resolved.Signature.t)
| `ModuleType(p, name) ->
let parent = resolved_signature_reference tbl p in
Sig.lookup_module_type (ModuleTypeName.to_string name) parent
and resolved_class_signature_reference tbl (r : Reference.Resolved.ClassSignature.t) =
match r with
| `Identifier id -> class_signature_identifier tbl id
| `Class(p, name) ->
let parent = resolved_signature_reference tbl p in
Sig.lookup_class_type (ClassName.to_string name) parent
| `ClassType(p, name) ->
let parent = resolved_signature_reference tbl p in
Sig.lookup_class_type (ClassTypeName.to_string name) parent
and resolved_datatype_reference tbl (r : Reference.Resolved.DataType.t) =
match r with
| `Identifier id -> datatype_identifier tbl id
| `Type(p, name) ->
let parent = resolved_signature_reference tbl p in
Sig.lookup_datatype (TypeName.to_string name) parent
and resolved_page_reference tbl : Reference.Resolved.Page.t -> _ = function
| `Identifier id -> page_identifier tbl id
let base tbl s = tbl.lookup_unit s
let page_base tbl s = tbl.lookup_page s