Skip to content

Commit 4d7f461

Browse files
panglesdjonludlam
authored andcommitted
Loader: Add constructors to the environment
This will allow to have correct anchors to constructors, as soon as they are added to the `uid_to_loc` table. Signed-off-by: Paul-Elliot <[email protected]>
1 parent cf468f0 commit 4d7f461

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

src/loader/cmi.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,7 @@ let read_constructor_declaration_arguments env parent arg =
627627

628628
let read_constructor_declaration env parent cd =
629629
let open TypeDecl.Constructor in
630-
let name = Ident.name cd.cd_id in
631-
let id = Identifier.Mk.constructor (parent, Odoc_model.Names.ConstructorName.make_std name) in
630+
let id = Ident_env.find_constructor_identifier env cd.cd_id in
632631
let container = (parent : Identifier.DataType.t :> Identifier.LabelParent.t) in
633632
let doc = Doc_attr.attached_no_tag container cd.cd_attributes in
634633
let args =

src/loader/cmti.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ let read_constructor_declaration_arguments env parent label_parent arg =
213213

214214
let read_constructor_declaration env parent cd =
215215
let open TypeDecl.Constructor in
216-
let open Odoc_model.Names in
217-
let name = Ident.name cd.cd_id in
218-
let id = Identifier.Mk.constructor(parent, ConstructorName.make_std name) in
216+
let id = Ident_env.find_constructor_identifier env cd.cd_id in
219217
let container = (parent : Identifier.DataType.t :> Identifier.Parent.t) in
220218
let label_container = (container :> Identifier.LabelParent.t) in
221219
let doc = Doc_attr.attached_no_tag label_container cd.cd_attributes in

src/loader/ident_env.cppo.ml

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type t =
3737
types : Id.DataType.t Ident.tbl;
3838
exceptions: Id.Exception.t Ident.tbl;
3939
extensions: Id.Extension.t Ident.tbl;
40+
constructors: Id.Constructor.t Ident.tbl;
4041
values: Id.Value.t Ident.tbl;
4142
classes : Id.Class.t Ident.tbl;
4243
class_types : Id.ClassType.t Ident.tbl;
@@ -51,6 +52,7 @@ let empty () =
5152
module_types = Ident.empty;
5253
types = Ident.empty;
5354
exceptions = Ident.empty;
55+
constructors = Ident.empty;
5456
extensions = Ident.empty;
5557
values = Ident.empty;
5658
classes = Ident.empty;
@@ -65,6 +67,8 @@ type item = [
6567
`Module of Ident.t * bool * Location.t option
6668
| `ModuleType of Ident.t * bool * Location.t option
6769
| `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 *)
6872
| `Value of Ident.t * bool * Location.t option
6973
| `Class of Ident.t * Ident.t * Ident.t * Ident.t option * bool * Location.t option
7074
| `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
8690
let rec extract_signature_type_items items =
8791
let open Compat in
8892
match items with
89-
| Sig_type(id, _, _, Exported) :: rest ->
93+
| Sig_type(id, td, _, Exported) :: rest ->
9094
if Btype.is_row_name (Ident.name id)
9195
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
93108

94109
| Sig_module(id, _, _, _, Exported) :: rest ->
95110
`Module (id, false, None) :: extract_signature_type_items rest
@@ -196,11 +211,13 @@ let extract_extended_open o =
196211
#endif
197212

198213

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
204221

205222
let rec extract_signature_tree_items : bool -> Typedtree.signature_item list -> items list = fun hide_item items ->
206223
let open Typedtree in
@@ -210,10 +227,17 @@ let rec extract_signature_tree_items : bool -> Typedtree.signature_item list ->
210227
#else
211228
| { sig_desc = Tsig_type (_, decls); _} :: rest ->
212229
#endif
213-
filter_map (fun decl ->
230+
concat_map (fun decl ->
214231
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+
)
217241
decls @ extract_signature_tree_items hide_item rest
218242

219243
#if OCAML_VERSION < (4,8,0)
@@ -329,8 +353,15 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
329353
#else
330354
| { str_desc = Tstr_type (_, decls); _ } :: rest -> (* TODO: handle rec_flag *)
331355
#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
334365

335366
#if OCAML_VERSION < (4,8,0)
336367
| { str_desc = Tstr_exception tyexn_constructor; _ } :: rest ->
@@ -421,7 +452,8 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
421452

422453
let flatten_includes : items list -> item list = fun items ->
423454
List.map (function
424-
| `Type _
455+
| `Type _
456+
| `Constructor _
425457
| `Module _
426458
| `ModuleType _
427459
| `Value _
@@ -465,6 +497,16 @@ let add_items : Id.Signature.t -> item list -> t -> t = fun parent items env ->
465497
(match loc with | Some l -> LocHashtbl.add env.loc_to_ident l (identifier :> Id.any) | _ -> ());
466498
inner rest { env with types; hidden }
467499

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+
468510
| `Exception (t, loc) :: rest ->
469511
let name = Ident.name t in
470512
let identifier = Mk.exception_(parent, ExceptionName.make_std name) in
@@ -602,6 +644,9 @@ let find_module_type env id =
602644
let find_type_identifier env id =
603645
Ident.find_same id env.types
604646

647+
let find_constructor_identifier env id =
648+
Ident.find_same id env.constructors
649+
605650
let find_exception_identifier env id =
606651
Ident.find_same id env.exceptions
607652

src/loader/ident_env.cppo.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ val find_value_identifier : t -> Ident.t -> Paths.Identifier.Value.t
5555

5656
val find_type : t -> Ident.t -> Paths.Identifier.Path.Type.t
5757

58+
val find_constructor_identifier : t -> Ident.t -> Paths.Identifier.Constructor.t
59+
5860
val find_extension_identifier : t -> Ident.t -> Paths.Identifier.Extension.t
5961

6062
val find_exception_identifier : t -> Ident.t -> Paths.Identifier.Exception.t

0 commit comments

Comments
 (0)