-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathodoc_sig.ml
1936 lines (1825 loc) · 80.3 KB
/
odoc_sig.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2001 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Analysis of interface files. *)
open Asttypes
open Types
open Odoc_parameter
open Odoc_value
open Odoc_type
open Odoc_extension
open Odoc_exception
open Odoc_class
open Odoc_module
open Odoc_types
module Signature_search =
struct
type ele =
| M of string
| MT of string
| V of string
| T of string
| C of string
| CT of string
| X of string
type tab = (ele, Types.signature_item) Hashtbl.t
let add_to_hash table signat =
match signat with
Types.Sig_value (ident, _, _) ->
Hashtbl.add table (V (Name.from_ident ident)) signat
| Types.Sig_typext (ident, _, _, _) ->
Hashtbl.add table (X (Name.from_ident ident)) signat
| Types.Sig_type (ident, _, _, _) ->
Hashtbl.add table (T (Name.from_ident ident)) signat
| Types.Sig_class (ident, _, _, _) ->
Hashtbl.add table (C (Name.from_ident ident)) signat
| Types.Sig_class_type (ident, _, _, _) ->
Hashtbl.add table (CT (Name.from_ident ident)) signat
| Types.Sig_module (ident, _, _, _, _) ->
Hashtbl.add table (M (Name.from_ident ident)) signat
| Types.Sig_modtype (ident,_,_) ->
Hashtbl.add table (MT (Name.from_ident ident)) signat
let table signat =
let t = Hashtbl.create 13 in
List.iter (add_to_hash t) signat;
t
let search_value table name =
match Hashtbl.find table (V name) with
| (Types.Sig_value (_, val_desc, _)) -> val_desc.Types.val_type
| _ -> assert false
let search_extension table name =
match Hashtbl.find table (X name) with
| (Types.Sig_typext (_, ext, _, _)) -> ext
| _ -> assert false
let search_type table name =
match Hashtbl.find table (T name) with
| (Types.Sig_type (_, type_decl, _, _)) -> type_decl
| _ -> assert false
let search_class table name =
match Hashtbl.find table (C name) with
| (Types.Sig_class (_, class_decl, _, _)) -> class_decl
| _ -> assert false
let search_class_type table name =
match Hashtbl.find table (CT name) with
| (Types.Sig_class_type (_, cltype_decl, _, _)) -> cltype_decl
| _ -> assert false
let search_module table name =
match Hashtbl.find table (M name) with
| (Types.Sig_module (_ident, _, md, _, _)) -> md.Types.md_type
| _ -> assert false
let search_module_type table name =
match Hashtbl.find table (MT name) with
| (Types.Sig_modtype (_, {Types.mtd_type = Some module_type}, _)) ->
Some module_type
| (Types.Sig_modtype (_, {Types.mtd_type = None}, _)) ->
None
| _ -> assert false
let search_attribute_type name class_sig =
let (_, _, type_expr) = Types.Vars.find name class_sig.Types.csig_vars in
type_expr
let search_method_type name class_sig =
let (_, _, type_expr) = Types.Meths.find name class_sig.Types.csig_meths in
type_expr
end
module type Info_retriever =
sig
val all_special : string -> string -> int * (Odoc_types.info list)
val blank_line_outside_simple : string -> string -> bool
val just_after_special : string -> string -> (int * Odoc_types.info option)
val first_special : string -> string -> (int * Odoc_types.info option)
val get_comments :
(Odoc_types.text -> 'a) -> string -> string -> (Odoc_types.info option * 'a list)
end
let alert_of_attribute attr =
let open Parsetree in
let load_constant_string = function
| { pexp_desc = Pexp_constant (Pconst_string (text, _, _)); _ } -> Some text
| _ -> None
in
let load_alert_name name = Longident.last name.Location.txt in
let deprecated_payload = function
| PStr [ { pstr_desc = Pstr_eval (s, _); _ } ] -> load_constant_string s
| _ -> None
in
let alert_payload = function
| PStr [ { pstr_desc = Pstr_eval ({ pexp_desc; _ }, _); _ } ] -> (
match pexp_desc with
| Pexp_apply ({ pexp_desc = Pexp_ident name; _ }, [ (_, payload) ]) ->
Some (load_alert_name name, load_constant_string payload)
| Pexp_ident name -> Some (load_alert_name name, None)
| _ -> None)
| _ -> None
in
match attr.attr_name.txt with
| "deprecated" | "ocaml.deprecated" ->
Some
{
alert_name = "deprecated";
alert_payload = deprecated_payload attr.attr_payload;
}
| "alert" | "ocaml.alert" -> (
match alert_payload attr.attr_payload with
| Some (alert_name, alert_payload) -> Some { alert_name; alert_payload }
| None -> None)
| _ -> None
let analyze_alerts info attrs =
(* Remove any deprecated alert if the tag is present. *)
let unify_deprecated alert (depr_tag, acc) =
match alert.alert_name with
| "deprecated" ->
let depr_tag =
match (depr_tag, alert.alert_payload) with
| Some (_ :: _), _ -> depr_tag
| _, Some text -> Some [ Odoc_types.Raw text ]
| _, None -> Some []
in
(depr_tag, acc)
| _ -> (depr_tag, alert :: acc)
in
match List.filter_map alert_of_attribute attrs with
| [] -> info
| _ :: _ as alerts ->
let info = Option.value ~default:Odoc_types.dummy_info info in
let i_deprecated, i_alerts =
List.fold_right unify_deprecated alerts (info.i_deprecated, [])
in
Some { info with i_deprecated; i_alerts }
(** Look for signature-item alerts ([\[@@@...\]]) at the beginning of a
signature. Stop at any other item. These alerts are considered attached to
the entire signature, similarly to the first comment, which becomes the
preamble. *)
let analyze_toplevel_alerts info ast =
let rec extract_attributes = function
| { Parsetree.psig_desc = Parsetree.Psig_attribute attr; _ } :: tl ->
attr :: extract_attributes tl
| _ :: _ | [] -> []
in
analyze_alerts info (extract_attributes ast)
module Analyser =
functor (My_ir : Info_retriever) ->
struct
(** This variable is used to load a file as a string and retrieve characters from it.*)
let file = ref ""
(** The name of the analysed file. *)
let file_name = ref ""
(** This function takes two indexes (start and end) and returns the string
corresponding to the indexes in the file global variable. The function
prepare_file must have been called to fill the file global variable.*)
let get_string_of_file the_start the_end =
try
String.sub !file the_start (the_end-the_start)
with
Invalid_argument _ ->
""
let just_after_special start stop =
let s = get_string_of_file start stop in
My_ir.just_after_special !file_name s
(** Helper functions for extracting location*)
module Loc = struct
let gen proj =
(fun ct -> (proj ct).Location.loc_start.Lexing.pos_cnum),
(fun ct -> (proj ct).Location.loc_end.Lexing.pos_cnum)
let ptyp' ct = ct.Parsetree.ptyp_loc
let pcd' pcd = pcd.Parsetree.pcd_loc
let loc' loc = loc
let psig' p = p.Parsetree.psig_loc
let start, end_ = gen loc'
let ptyp_start, ptyp_end = gen ptyp'
let pcd_start, pcd_end = gen pcd'
let psig_start, psig_end = gen psig'
end
(** This function loads the given file in the file global variable,
and sets file_name.*)
let prepare_file f input_f =
try
let s = Odoc_misc.input_file_as_string input_f in
file := s;
file_name := f
with
e ->
file := "";
raise e
(** The function used to get the comments in a class. *)
let get_comments_in_class pos_start pos_end =
My_ir.get_comments (fun t -> Class_comment t)
!file_name
(get_string_of_file pos_start pos_end)
(** The function used to get the comments in a module. *)
let get_comments_in_module pos_start pos_end =
My_ir.get_comments (fun t -> Element_module_comment t)
!file_name
(get_string_of_file pos_start pos_end)
let preamble filename file proj ast =
let info = My_ir.first_special filename file in
(* Only use as module preamble documentation comments that occur before
any module elements *)
match ast with
| a :: _ when Loc.start (proj a) < fst info -> (0,None)
| _ -> info
let merge_infos = Odoc_merge.merge_info_opt Odoc_types.all_merge_options
(** Fetch the comment just after an item then merge the info with
[comment_opt]. If [attrs] is passed, alerts will be read from it. *)
let get_info ?(attrs = []) comment_opt start stop =
let maybe_more, info_after_opt = just_after_special start stop in
let comment_opt = merge_infos comment_opt info_after_opt in
maybe_more, analyze_alerts comment_opt attrs
(** Module for extracting documentation comments for record from different
tree types *)
module Record = struct
(** A structure to abstract over the tree type *)
type ('a,'b,'c) projector = {
name:'a -> string;
inline_record: 'b -> 'c option;
inline_end: 'b -> int;
start:'a -> int;
end_: 'a -> int }
(** A function to extract documentation from a list of label declarations *)
let doc p pos_end ld =
let rec f = function
| [] -> []
| ld :: [] ->
let name = p.name ld in
let pos = p.end_ ld in
let (_,comment_opt) = just_after_special pos pos_end in
[name, comment_opt]
| ld :: ele2 :: q ->
let pos = p.end_ ld in
let pos2 = p.start ele2 in
let name = p.name ld in
let (_,comment_opt) = just_after_special pos pos2 in
(name, comment_opt) :: (f (ele2 :: q))
in
f ld
let inline_doc p cstr =
match p.inline_record cstr with
| None -> []
| Some r ->
doc p (p.inline_end cstr) r
(** The three tree types used in the rest of the source: *)
let parsetree =
let open Parsetree in
{ name = (fun ld -> ld.pld_name.txt );
start = (fun ld -> Loc.ptyp_start ld.pld_type);
end_ = (fun ld -> Loc.ptyp_end ld.pld_type);
inline_record = begin
fun c -> match c.pcd_args with
| Pcstr_tuple _ -> None
| Pcstr_record r -> Some r
end;
inline_end = (fun c -> Loc.end_ c.pcd_loc)
}
let types =
let open Types in
{ name = (fun ld -> Ident.name ld.ld_id );
start = (fun ld -> Loc.start ld.ld_loc);
end_ = (fun ld -> Loc.start ld.ld_loc);
(* Beware, Loc.start is correct in the code above:
type_expr's do not hold location information, and ld.ld_loc
ends after the documentation comment, so we use Loc.start as
the least problematic approximation for end_. *)
inline_record = begin
fun c -> match c.cd_args with
| Cstr_tuple _ -> None
| Cstr_record r -> Some r
end;
inline_end = (fun c -> Loc.end_ c.cd_loc)
}
let typedtree =
let open Typedtree in
{ name = (fun ld -> Ident.name ld.ld_id );
start = (fun ld -> Loc.start ld.ld_type.ctyp_loc);
end_ = (fun ld -> Loc.end_ ld.ld_type.ctyp_loc);
inline_record = begin
fun c -> match c.cd_args with
| Cstr_tuple _ -> None
| Cstr_record r -> Some r
end;
inline_end = (fun c -> Loc.end_ c.cd_loc)
}
end
let name_comment_from_type_decl pos_end pos_limit ty_decl =
match ty_decl.Parsetree.ptype_kind with
| Parsetree.Ptype_abstract ->
let open Parsetree in
begin match ty_decl.ptype_manifest with
| None -> (0, [])
| Some core_ty ->
begin match core_ty.ptyp_desc with
| Ptyp_object (fields, _) ->
let fields = List.map (fun {pof_desc; _} -> pof_desc) fields in
let rec f = function
| [] -> []
| Otag ({txt=""},_) :: _ ->
(* Fields with no name have been eliminated previously. *)
assert false
| Otag ({txt=name}, ct) :: [] ->
let pos = Loc.ptyp_end ct in
let (_,comment_opt) = just_after_special pos pos_end in
[name, comment_opt]
| Otag ({txt=name}, ct) ::
((Oinherit ct2 | Otag (_, ct2)) as ele2) :: q ->
let pos = Loc.ptyp_end ct in
let pos2 = Loc.ptyp_start ct2 in
let (_,comment_opt) = just_after_special pos pos2 in
(name, comment_opt) :: (f (ele2 :: q))
| _ :: q -> f q
in
let is_named_field field =
match field with
| Otag ({txt=""},_) -> false
| _ -> true
in
(0, f @@ List.filter is_named_field fields)
| _ -> (0, [])
end
end
| Parsetree.Ptype_variant cons_core_type_list_list ->
let rec f acc cons_core_type_list_list =
let open Parsetree in
match cons_core_type_list_list with
[] ->
(0, acc)
| pcd :: [] ->
let acc = Record.(inline_doc parsetree) pcd @ acc in
let (len, comment_opt) =
just_after_special (Loc.pcd_end pcd) pos_limit in
(len, List.rev @@ (pcd.pcd_name.txt, comment_opt):: acc )
| pcd :: (pcd2 :: _ as q) ->
let acc = Record.(inline_doc parsetree) pcd @ acc in
let pos_end_first = Loc.pcd_end pcd in
let pos_start_second = Loc.pcd_start pcd2 in
let (_,comment_opt) =
just_after_special pos_end_first pos_start_second in
f ((pcd.pcd_name.txt, comment_opt)::acc) q
in
f [] cons_core_type_list_list
| Parsetree.Ptype_record label_declaration_list ->
(0, Record.(doc parsetree) pos_end label_declaration_list)
| Parsetree.Ptype_record_unboxed_product _ -> assert false
| Parsetree.Ptype_open ->
(0, [])
let manifest_structure env name_comment_list type_expr =
match get_desc type_expr with
| Tobject (fields, _) ->
let f (field_name, _, type_expr) =
let comment_opt =
try List.assoc field_name name_comment_list
with Not_found -> None
in {
of_name = field_name ;
of_type = Odoc_env.subst_type env type_expr ;
of_text = comment_opt ;
}
in
Object_type (List.map f @@ fst @@ Ctype.flatten_fields fields)
| _ -> Other (Odoc_env.subst_type env type_expr)
let get_field env name_comment_list {Types.ld_id=field_name;ld_mutable=mutable_flag;ld_type=type_expr;ld_attributes} =
let field_name = Ident.name field_name in
let comment_opt =
try List.assoc field_name name_comment_list
with Not_found -> None
in
let comment_opt = analyze_alerts comment_opt ld_attributes in
{
rf_name = field_name ;
rf_mutable = Types.is_mutable mutable_flag;
rf_type = Odoc_env.subst_type env type_expr ;
rf_text = comment_opt
}
let get_type_kind env name_comment_list type_kind =
match type_kind with
Types.Type_abstract _ ->
Odoc_type.Type_abstract
| Types.Type_variant (l,_,_) ->
let f {Types.cd_id=constructor_name;cd_args;cd_res=ret_type;cd_attributes} =
let constructor_name = Ident.name constructor_name in
let comment_opt =
try match List.assoc constructor_name name_comment_list with
| Some { i_desc = None | Some []; _ } -> None
| x -> x
with Not_found -> None
in
let comment_opt = analyze_alerts comment_opt cd_attributes in
let vc_args =
match cd_args with
| Cstr_tuple l -> Cstr_tuple (List.map (fun {ca_type=ty; _} -> Odoc_env.subst_type env ty) l)
| Cstr_record l ->
Cstr_record (List.map (get_field env name_comment_list) l)
in
let vc_name = match constructor_name with
| "::" ->
(* The only infix constructor is always printed (::) *)
"(::)"
| s -> s
in
{
vc_name;
vc_args;
vc_ret = Option.map (Odoc_env.subst_type env) ret_type;
vc_text = comment_opt
}
in
Odoc_type.Type_variant (List.map f l)
| Types.Type_record (l, _, _) ->
Odoc_type.Type_record (List.map (get_field env name_comment_list) l)
| Types.Type_record_unboxed_product (_, _, _) -> assert false
| Types.Type_open ->
Odoc_type.Type_open
let get_cstr_args env pos_end =
let tuple ct = Odoc_env.subst_type env ct.Typedtree.ctyp_type in
let record comments
{ Typedtree.ld_id; ld_mutable; ld_type; ld_loc; ld_attributes } =
get_field env comments @@
{Types.ld_id; ld_mutable; ld_modalities = Mode.Modality.Value.Const.id;
ld_sort=Jkind.Sort.Const.void (* ignored *);
ld_type=ld_type.Typedtree.ctyp_type;
ld_loc; ld_attributes; ld_uid=Types.Uid.internal_not_actually_unique} in
let open Typedtree in
function
| Cstr_tuple l ->
Odoc_type.Cstr_tuple (List.map (fun {ca_type=ty; _} -> tuple ty) l)
| Cstr_record l ->
let comments = Record.(doc typedtree) pos_end l in
Odoc_type.Cstr_record (List.map (record comments) l)
(* Given a constraint "with type M.N.t := foo", this function adds "M" ->
"with type N.t := foo" to acc, ie it build the constraint to put on the
first element of the path being modified.
When filter_out_erased_items_from_signature finds "M", it applies the
constraint on its module type. *)
let constraint_for_subitem =
let split_longident p =
match Longident.flatten p with
| [] -> assert false
| hd :: tl -> hd, Longident.unflatten tl
in
fun acc s rebuild_constraint ->
match split_longident s.txt with
| hd, None -> Name.Map.add hd `Removed acc
| hd, Some p ->
let constraint_ = rebuild_constraint { s with txt = p } in
match Name.Map.find hd acc with
| exception Not_found ->
Name.Map.add hd (`Constrained [constraint_]) acc
| `Constrained old ->
Name.Map.add hd (`Constrained (constraint_ :: old)) acc
| `Removed -> acc
let erased_names_of_constraints constraints acc =
List.fold_right (fun constraint_ acc ->
match constraint_ with
| Parsetree.Pwith_type _ | Parsetree.Pwith_module _ | Parsetree.Pwith_modtype _ -> acc
| Parsetree.Pwith_typesubst (s, typedecl) ->
constraint_for_subitem acc s (fun s -> Parsetree.Pwith_typesubst (s, typedecl))
| Parsetree.Pwith_modsubst (s, modpath) ->
constraint_for_subitem acc s (fun s -> Parsetree.Pwith_modsubst (s, modpath))
| Parsetree.Pwith_modtypesubst (s, modpath) ->
constraint_for_subitem acc s
(fun s -> Parsetree.Pwith_modtypesubst (s, modpath)))
constraints acc
let is_erased ident map =
match Name.Map.find ident map with
| exception Not_found -> false
| `Removed -> true
| `Constrained _ -> false
let apply_constraint module_type constraints =
match module_type.Parsetree.pmty_desc with
| Parsetree.Pmty_alias _ -> module_type
| _ ->
{ Parsetree.
pmty_desc = Parsetree.Pmty_with (module_type, List.rev constraints);
pmty_loc = module_type.Parsetree.pmty_loc;
pmty_attributes = []
}
let filter_out_erased_items_from_signature erased signature =
if Name.Map.is_empty erased then signature
else List.fold_right (fun sig_item acc ->
let take_item psig_desc = { sig_item with Parsetree.psig_desc } :: acc in
match sig_item.Parsetree.psig_desc with
| Parsetree.Psig_kind_abbrev _ -> Misc.fatal_error "Psig_kind_abbrev"
| Parsetree.Psig_attribute _
| Parsetree.Psig_extension _
| Parsetree.Psig_value _
| Parsetree.Psig_typext _
| Parsetree.Psig_exception _
| Parsetree.Psig_open _
| Parsetree.Psig_include ({pincl_kind=Structure;_}, _)
| Parsetree.Psig_class _
| Parsetree.Psig_class_type _ as tp -> take_item tp
| Parsetree.Psig_include ({pincl_kind=Functor;_}, _)
| Parsetree.Psig_typesubst _ -> acc
| Parsetree.Psig_type (rf, types) ->
(match List.filter (fun td -> not (is_erased td.Parsetree.ptype_name.txt erased)) types with
| [] -> acc
| types -> take_item (Parsetree.Psig_type (rf, types)))
| Parsetree.Psig_modsubst _ -> acc
| Parsetree.Psig_module {Parsetree.pmd_name={ txt = None }} -> acc
| Parsetree.Psig_module ({Parsetree.pmd_name={txt = Some name };
pmd_type=module_type} as r)
as m ->
begin match Name.Map.find name erased with
| exception Not_found -> take_item m
| `Removed -> acc
| `Constrained constraints ->
take_item
(Parsetree.Psig_module
{ r with
Parsetree.pmd_type =
apply_constraint module_type constraints })
end
| Parsetree.Psig_modtype {Parsetree.pmtd_name=name} as m ->
if is_erased name.txt erased then acc else take_item m
| Parsetree.Psig_modtypesubst _ -> acc
| Parsetree.Psig_recmodule mods ->
(match List.filter
(fun pmd ->
match pmd.Parsetree.pmd_name.txt with
| None -> false
| Some name -> not (is_erased name erased))
mods
with
| [] -> acc
| mods -> take_item (Parsetree.Psig_recmodule mods)))
signature []
(** Analysis of the elements of a class, from the information in the parsetree and in the class
signature. @return the couple (inherited_class list, elements).*)
let analyse_class_elements env current_class_name last_pos pos_limit
class_type_field_list class_signature =
let get_pos_limit2 q =
match q with
[] -> pos_limit
| ele2 :: _ ->
let loc = ele2.Parsetree.pctf_loc in
match ele2.Parsetree.pctf_desc with
Parsetree.Pctf_val (_, _, _, _)
| Parsetree.Pctf_method (_, _, _, _)
| Parsetree.Pctf_constraint (_, _)
| Parsetree.Pctf_attribute _ -> Loc.start loc
| Parsetree.Pctf_inherit class_type ->
Loc.start class_type.Parsetree.pcty_loc
| Parsetree.Pctf_extension _ -> assert false
in
let get_method name comment_opt private_flag loc q =
let complete_name = Name.concat current_class_name name in
let typ =
try Signature_search.search_method_type name class_signature
with Not_found ->
raise (Failure (Odoc_messages.method_type_not_found current_class_name name))
in
let subst_typ = Odoc_env.subst_type env typ in
let met =
{
met_value =
{
val_name = complete_name ;
val_info = comment_opt ;
val_type = subst_typ ;
val_recursive = false ;
val_parameters = Odoc_value.dummy_parameter_list subst_typ ;
val_code = None ;
val_loc = { loc_impl = None ; loc_inter = Some loc };
} ;
met_private = private_flag = Asttypes.Private ;
met_virtual = false ;
}
in
let pos_limit2 = get_pos_limit2 q in
let pos_end = Loc.end_ loc in
let (maybe_more, info_after_opt) =
My_ir.just_after_special
!file_name
(get_string_of_file pos_end pos_limit2)
in
met.met_value.val_info <- merge_infos met.met_value.val_info info_after_opt ;
(* update the parameter description *)
Odoc_value.update_value_parameters_text met.met_value;
(met, maybe_more)
in
let rec f last_pos class_type_field_list =
match class_type_field_list with
[] ->
let s = get_string_of_file last_pos pos_limit in
let (_, ele_coms) = My_ir.all_special !file_name s in
let ele_comments =
List.fold_left
(fun acc -> fun sc ->
match sc.Odoc_types.i_desc with
None ->
acc
| Some t ->
acc @ [Class_comment t])
[]
ele_coms
in
([], ele_comments)
| item :: q ->
let loc = item.Parsetree.pctf_loc in
match item.Parsetree.pctf_desc with
| Parsetree.Pctf_val ({txt=name}, mutable_flag, virtual_flag, _) ->
(* of (string * mutable_flag * core_type option * Location.t)*)
let (comment_opt, eles_comments) = get_comments_in_class last_pos
(Loc.start loc) in
let complete_name = Name.concat current_class_name name in
let typ =
try Signature_search.search_attribute_type name class_signature
with Not_found ->
raise (Failure (Odoc_messages.attribute_type_not_found current_class_name name))
in
let subst_typ = Odoc_env.subst_type env typ in
let att =
{
att_value =
{
val_name = complete_name ;
val_info = comment_opt ;
val_type = subst_typ;
val_recursive = false ;
val_parameters = [] ;
val_code = None ;
val_loc = { loc_impl = None ; loc_inter = Some loc} ;
} ;
att_mutable = mutable_flag = Asttypes.Mutable ;
att_virtual = virtual_flag = Asttypes.Virtual ;
}
in
let pos_limit2 = get_pos_limit2 q in
let pos_end = Loc.end_ loc in
let (maybe_more, info_after_opt) =
My_ir.just_after_special
!file_name
(get_string_of_file pos_end pos_limit2)
in
att.att_value.val_info <- merge_infos att.att_value.val_info info_after_opt ;
let (inher_l, eles) = f (pos_end + maybe_more) q in
(inher_l, eles_comments @ ((Class_attribute att) :: eles))
| Parsetree.Pctf_method ({txt=name}, private_flag, virtual_flag, _) ->
(* of (string * private_flag * virtual_flag * core_type) *)
let (comment_opt, eles_comments) =
get_comments_in_class last_pos (Loc.start loc) in
let (met, maybe_more) = get_method name comment_opt private_flag loc q in
let met2 =
match virtual_flag with
| Concrete -> met
| Virtual -> { met with met_virtual = true }
in
let (inher_l, eles) = f (Loc.end_ loc + maybe_more) q in
(inher_l, eles_comments @ ((Class_method met2) :: eles))
| (Parsetree.Pctf_constraint (_, _)) ->
(* of (core_type * core_type) *)
(* FIXME: this corresponds to constraints, isn't it? We don't keep them for now *)
let (_comment_opt, eles_comments) = get_comments_in_class last_pos
(Loc.start loc) in
let (inher_l, eles) = f (Loc.end_ loc) q in
(inher_l, eles_comments @ eles)
| Parsetree.Pctf_inherit class_type ->
let loc = class_type.Parsetree.pcty_loc in
let (comment_opt, eles_comments) =
get_comments_in_class last_pos (Loc.start loc) in
let pos_limit2 = get_pos_limit2 q in
let pos_end = Loc.end_ loc in
let (maybe_more, info_after_opt) =
just_after_special pos_end pos_limit2
in
let comment_opt2 = merge_infos comment_opt info_after_opt in
let text_opt = match comment_opt2 with None -> None | Some i -> i.Odoc_types.i_desc in
let inh =
match class_type.Parsetree.pcty_desc with
Parsetree.Pcty_constr (longident, _) ->
(*of Longident.t * core_type list*)
let name = Name.from_longident longident.txt in
{
ic_name = Odoc_env.full_class_or_class_type_name env name ;
ic_class = None ;
ic_text = text_opt ;
}
| Parsetree.Pcty_open _ (* one could also traverse the open *)
| Parsetree.Pcty_signature _
| Parsetree.Pcty_arrow _ ->
(* we don't have a name for the class signature, so we call it "object ... end" *)
{
ic_name = Odoc_messages.object_end ;
ic_class = None ;
ic_text = text_opt ;
}
| Parsetree.Pcty_extension _ -> assert false
in
let (inher_l, eles) = f (pos_end + maybe_more) q in
(inh :: inher_l , eles_comments @ eles)
| Parsetree.Pctf_attribute _ ->
let (_comment_opt, eles_comments) =
get_comments_in_class last_pos (Loc.start loc) in
let (inher_l, eles) = f (Loc.end_ loc) q in
(inher_l, eles_comments @ eles)
| Parsetree.Pctf_extension _ -> assert false
in
f last_pos class_type_field_list
(** Analyse of a .mli parse tree, to get the corresponding elements.
last_pos is the position of the first character which may be used to look for special comments.
*)
let rec analyse_parsetree env signat current_module_name last_pos pos_limit sig_item_list =
let table = Signature_search.table signat in
(* we look for the comment of each item then analyse the item *)
let rec f acc_eles acc_env last_pos = function
[] ->
let s = get_string_of_file last_pos pos_limit in
let (_, ele_coms) = My_ir.all_special !file_name s in
let ele_comments =
List.fold_left
(fun acc -> fun sc ->
match sc.Odoc_types.i_desc with
None ->
acc
| Some t ->
acc @ [Element_module_comment t])
[]
ele_coms
in
acc_eles @ ele_comments
| ele :: q ->
let (assoc_com, ele_comments) =
get_comments_in_module last_pos (Loc.psig_start ele)
in
let (maybe_more, new_env, elements) = analyse_signature_item
acc_env
signat
table
current_module_name
ele.Parsetree.psig_loc
(Loc.psig_start ele)
(Loc.psig_end ele)
(match q with
[] -> pos_limit
| ele2 :: _ -> Loc.psig_start ele2
)
assoc_com
ele
in
let new_pos = Loc.psig_end ele + maybe_more
(* for the comments of constructors in types,
which are after the constructor definition and can
go beyond ele.Parsetree.psig_loc.Location.loc_end.Lexing.pos_cnum *)
in
f (acc_eles @ (ele_comments @ elements))
new_env
new_pos
q
in
f [] env last_pos sig_item_list
and analyse_signature_item_desc_include ~env ~comment_opt incl =
let rec f = function
Parsetree.Pmty_ident longident ->
Name.from_longident longident.txt
| Parsetree.Pmty_alias longident ->
Name.from_longident longident.txt
| Parsetree.Pmty_signature _ ->
"??"
| Parsetree.Pmty_functor _ ->
"??"
| Parsetree.Pmty_with (mt, _) ->
f mt.Parsetree.pmty_desc
| Parsetree.Pmty_typeof mexpr ->
let open Parsetree in
begin match mexpr.pmod_desc with
Pmod_ident longident -> Name.from_longident longident.txt
| Pmod_structure [
{pstr_desc=Pstr_include
{pincl_mod={pmod_desc=Pmod_ident longident};
pincl_kind=Structure}
}] -> (* include module type of struct include M end*)
Name.from_longident longident.txt
| _ -> "??"
end
| Parsetree.Pmty_extension _ -> assert false
| Parsetree.Pmty_strengthen _ -> failwith "strengthen not implemented yet"
in
let name = f incl.Parsetree.pincl_mod.Parsetree.pmty_desc in
let full_name = Odoc_env.full_module_or_module_type_name env name in
let im =
{
im_name = full_name ;
im_module = None ;
im_info = comment_opt;
}
in
(0, env, [ Element_included_module im ]) (* FIXME : extend the environment? How? *)
(** Analyse the given signature_item_desc to create the corresponding module element
(with the given attached comment).*)
and analyse_signature_item_desc env _signat table current_module_name
sig_item_loc pos_start_ele pos_end_ele pos_limit comment_opt sig_item_desc =
match sig_item_desc with
| Parsetree.Psig_kind_abbrev _ -> Misc.fatal_error "Psig_kind_abbrev"
| Parsetree.Psig_value value_desc ->
let name_pre = value_desc.Parsetree.pval_name in
let type_expr =
try Signature_search.search_value table name_pre.txt
with Not_found ->
raise (Failure (Odoc_messages.value_not_found current_module_name name_pre.txt))
in
let name = Name.parens_if_infix name_pre.txt in
let subst_typ = Odoc_env.subst_type env type_expr in
let (maybe_more, comment_opt) =
get_info ~attrs:value_desc.Parsetree.pval_attributes comment_opt
pos_end_ele pos_limit
in
let v =
{
val_name = Name.concat current_module_name name ;
val_info = comment_opt ;
val_type = subst_typ ;
val_recursive = false ;
val_parameters = Odoc_value.dummy_parameter_list subst_typ ;
val_code = None ;
val_loc = { loc_impl = None ; loc_inter = Some sig_item_loc } ;
}
in
(* update the parameter description *)
Odoc_value.update_value_parameters_text v;
let new_env = Odoc_env.add_value env v.val_name in
(maybe_more, new_env, [ Element_value v ])
| Parsetree.Psig_typext tyext ->
let new_env, types_ext_list, last_ext =
List.fold_left
(fun (env_acc, exts_acc, _) ->
fun {Parsetree.pext_name = { txt = name }; pext_attributes; _} ->
let complete_name = Name.concat current_module_name name in
let env_acc = Odoc_env.add_extension env_acc complete_name in
let types_ext =
try Signature_search.search_extension table name
with Not_found ->
raise (Failure (Odoc_messages.extension_not_found current_module_name name))
in
env_acc, ((name, pext_attributes, types_ext) :: exts_acc), Some types_ext
)
(env, [], None)
tyext.Parsetree.ptyext_constructors
in
let types_ext_list = List.rev types_ext_list in
let ty_path, ty_params, priv =
match last_ext with
None -> assert false
| Some ext -> ext.ext_type_path, ext.ext_type_params, ext.ext_private
in
let new_te =
{
te_info = None;
te_type_name =
Odoc_env.full_type_name new_env (Name.from_path ty_path);
te_type_parameters =
List.map (Odoc_env.subst_type new_env) ty_params;
te_private = priv;
te_constructors = [];
te_loc = { loc_impl = None ; loc_inter = Some sig_item_loc} ;
te_code =
(
if !Odoc_global.keep_code then
Some (get_string_of_file pos_start_ele pos_end_ele)
else
None
) ;
}
in
let rec analyse_extension_constructors maybe_more exts_acc types_ext_list =
match types_ext_list with
[] -> (maybe_more, List.rev exts_acc)
| (name, attrs, types_ext) :: q ->
let ext_loc_end = Loc.end_ types_ext.Types.ext_loc in
let xt_args =
match types_ext.ext_args with
| Cstr_tuple l ->
Cstr_tuple (List.map (fun {ca_type=ty; _} -> Odoc_env.subst_type new_env ty) l)
| Cstr_record l ->
let docs = Record.(doc types ext_loc_end) l in
Cstr_record (List.map (get_field new_env docs) l)
in
let (maybe_more, comment_opt) =
let pos_limit2 =
match q with
[] -> pos_limit
| (_, _, next) :: _ -> Loc.start (next.Types.ext_loc)
in
get_info ~attrs None ext_loc_end pos_limit2
in
let new_x =
{
xt_name = Name.concat current_module_name name ;
xt_args;
xt_ret = Option.map (Odoc_env.subst_type new_env) types_ext.ext_ret_type ;
xt_type_extension = new_te;
xt_alias = None ;
xt_loc = { loc_impl = None ; loc_inter = Some types_ext.Types.ext_loc} ;
xt_text = comment_opt;
}
in
analyse_extension_constructors maybe_more (new_x :: exts_acc) q
in
let (maybe_more, exts) = analyse_extension_constructors 0 [] types_ext_list in
new_te.te_constructors <- exts;
let (maybe_more2, comment_opt) =
get_info ~attrs:tyext.Parsetree.ptyext_attributes comment_opt