@@ -37,6 +37,7 @@ type t =
37
37
types : Id.DataType .t Ident .tbl ;
38
38
exceptions : Id.Exception .t Ident .tbl ;
39
39
extensions : Id.Extension .t Ident .tbl ;
40
+ constructors : Id.Constructor .t Ident .tbl ;
40
41
values : Id.Value .t Ident .tbl ;
41
42
classes : Id.Class .t Ident .tbl ;
42
43
class_types : Id.ClassType .t Ident .tbl ;
@@ -51,6 +52,7 @@ let empty () =
51
52
module_types = Ident. empty;
52
53
types = Ident. empty;
53
54
exceptions = Ident. empty;
55
+ constructors = Ident. empty;
54
56
extensions = Ident. empty;
55
57
values = Ident. empty;
56
58
classes = Ident. empty;
@@ -65,6 +67,8 @@ type item = [
65
67
`Module of Ident .t * bool * Location .t option
66
68
| `ModuleType of Ident .t * bool * Location .t option
67
69
| `Type of Ident .t * bool * Location .t option
70
+ | `Constructor of Ident .t * Ident .t * Location .t option
71
+ (* Second ident.t is for the type parent *)
68
72
| `Value of Ident .t * bool * Location .t option
69
73
| `Class of Ident .t * Ident .t * Ident .t * Ident .t option * bool * Location .t option
70
74
| `ClassType of Ident .t * Ident .t * Ident .t option * bool * Location .t option
@@ -86,10 +90,21 @@ let builtin_idents = List.map snd Predef.builtin_idents
86
90
let rec extract_signature_type_items items =
87
91
let open Compat in
88
92
match items with
89
- | Sig_type (id , _ , _ , Exported) :: rest ->
93
+ | Sig_type (id , td , _ , Exported) :: rest ->
90
94
if Btype. is_row_name (Ident. name id)
91
95
then extract_signature_type_items rest
92
- else `Type (id, false , None ) :: extract_signature_type_items rest
96
+ else
97
+ let constrs = match td.type_kind with
98
+ | Types. Type_abstract -> []
99
+ | Type_record (_ , _ ) -> []
100
+ #if OCAML_VERSION < (4 ,13 ,0 )
101
+ | Type_variant cstrs ->
102
+ #else
103
+ | Type_variant (cstrs , _ ) ->
104
+ #endif
105
+ List. map (fun c -> `Constructor (c.Types. cd_id, id, Some c.cd_loc)) cstrs
106
+ | Type_open -> [] in
107
+ `Type (id, false , None ) :: constrs @ extract_signature_type_items rest
93
108
94
109
| Sig_module (id , _ , _ , _ , Exported) :: rest ->
95
110
`Module (id, false , None ) :: extract_signature_type_items rest
@@ -196,11 +211,13 @@ let extract_extended_open o =
196
211
#endif
197
212
198
213
199
- let filter_map f x =
200
- List. rev
201
- @@ List. fold_left
202
- (fun acc x -> match f x with Some x -> x :: acc | None -> acc)
203
- [] x
214
+ let concat_map f l =
215
+ let rec aux f acc = function
216
+ | [] -> List. rev acc
217
+ | x :: l ->
218
+ let xs = f x in
219
+ aux f (List. rev_append xs acc) l
220
+ in aux f [] l
204
221
205
222
let rec extract_signature_tree_items : bool -> Typedtree.signature_item list -> items list = fun hide_item items ->
206
223
let open Typedtree in
@@ -210,10 +227,17 @@ let rec extract_signature_tree_items : bool -> Typedtree.signature_item list ->
210
227
#else
211
228
| { sig_desc = Tsig_type (_ , decls ); _} :: rest ->
212
229
#endif
213
- filter_map (fun decl ->
230
+ concat_map (fun decl ->
214
231
if Btype. is_row_name (Ident. name decl.typ_id)
215
- then None
216
- else Some (`Type (decl.typ_id, hide_item, Some decl.typ_loc)))
232
+ then []
233
+ else
234
+ `Type (decl.typ_id, hide_item, Some decl.typ_loc) ::
235
+ match decl.typ_kind with
236
+ Ttype_abstract -> []
237
+ | Ttype_variant constrs -> List. map (fun c -> `Constructor (c.cd_id, decl.typ_id, Some c.cd_loc)) constrs
238
+ | Ttype_record _ -> []
239
+ | Ttype_open -> []
240
+ )
217
241
decls @ extract_signature_tree_items hide_item rest
218
242
219
243
#if OCAML_VERSION < (4 ,8 ,0 )
@@ -329,8 +353,15 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
329
353
#else
330
354
| { str_desc = Tstr_type (_ , decls ); _ } :: rest -> (* TODO: handle rec_flag *)
331
355
#endif
332
- List. map (fun decl -> `Type (decl.typ_id, hide_item, Some decl.typ_loc))
333
- decls @ extract_structure_tree_items hide_item rest
356
+ concat_map (fun decl ->
357
+ `Type (decl.typ_id, hide_item, Some decl.typ_loc) ::
358
+ (match decl.typ_kind with
359
+ Ttype_abstract -> []
360
+ | Ttype_variant constrs -> List. map (fun c -> `Constructor (c.cd_id, decl.typ_id, Some c.cd_loc)) constrs
361
+ | Ttype_record _ -> []
362
+ | Ttype_open -> []
363
+ ))
364
+ decls @ extract_structure_tree_items hide_item rest
334
365
335
366
#if OCAML_VERSION < (4 ,8 ,0 )
336
367
| { str_desc = Tstr_exception tyexn_constructor ; _ } :: rest ->
@@ -421,7 +452,8 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
421
452
422
453
let flatten_includes : items list -> item list = fun items ->
423
454
List. map (function
424
- | `Type _
455
+ | `Type _
456
+ | `Constructor _
425
457
| `Module _
426
458
| `ModuleType _
427
459
| `Value _
@@ -465,6 +497,16 @@ let add_items : Id.Signature.t -> item list -> t -> t = fun parent items env ->
465
497
(match loc with | Some l -> LocHashtbl. add env.loc_to_ident l (identifier :> Id.any ) | _ -> () );
466
498
inner rest { env with types; hidden }
467
499
500
+ | `Constructor (t , t_parent , loc ) :: rest ->
501
+ let name = Ident. name t in
502
+ let identifier =
503
+ let parent = Ident. find_same t_parent env.types in
504
+ Mk. constructor(parent, ConstructorName. make_std name)
505
+ in
506
+ let constructors = Ident. add t identifier env.constructors in
507
+ (match loc with | Some l -> LocHashtbl. add env.loc_to_ident l (identifier :> Id.any ) | _ -> () );
508
+ inner rest { env with constructors }
509
+
468
510
| `Exception (t , loc ) :: rest ->
469
511
let name = Ident. name t in
470
512
let identifier = Mk. exception_(parent, ExceptionName. make_std name) in
@@ -602,6 +644,9 @@ let find_module_type env id =
602
644
let find_type_identifier env id =
603
645
Ident. find_same id env.types
604
646
647
+ let find_constructor_identifier env id =
648
+ Ident. find_same id env.constructors
649
+
605
650
let find_exception_identifier env id =
606
651
Ident. find_same id env.exceptions
607
652
0 commit comments