Skip to content

Commit 24daa6e

Browse files
panglesdJulow
authored andcommitted
Generate identifiers for exceptions and extensions in ident_env
An identifier was already generated, but kept only in the `loc_to_ident` table. Another one was generated in the (cmt/cmi/cmti) loader. Now it is stored and looked up in the environment, enforcing that the ids correspond. Signed-off-by: Paul-Elliot <[email protected]> Co-authored-by: Jules Aguillon <[email protected]>
1 parent 35fe6a0 commit 24daa6e

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

src/loader/cmi.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,7 @@ let read_type_declaration env parent id decl =
740740

741741
let read_extension_constructor env parent id ext =
742742
let open Extension.Constructor in
743-
let name = Ident.name id in
744-
let id = Identifier.Mk.extension(parent, Odoc_model.Names.ExtensionName.make_std name) in
743+
let id = Env.find_extension_identifier env id in
745744
let locs = None in
746745
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
747746
let doc = Doc_attr.attached_no_tag container ext.ext_attributes in
@@ -774,8 +773,7 @@ let read_type_extension env parent id ext rest =
774773

775774
let read_exception env parent id ext =
776775
let open Exception in
777-
let name = Ident.name id in
778-
let id = Identifier.Mk.exception_(parent, Odoc_model.Names.ExceptionName.make_std name) in
776+
let id = Env.find_exception_identifier env id in
779777
let locs = None in
780778
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
781779
let doc = Doc_attr.attached_no_tag container ext.ext_attributes in

src/loader/cmti.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,7 @@ let read_type_substitutions env parent decls =
292292

293293
let read_extension_constructor env parent ext =
294294
let open Extension.Constructor in
295-
let open Odoc_model.Names in
296-
let name = Ident.name ext.ext_id in
297-
let id = Identifier.Mk.extension(parent, ExtensionName.make_std name) in
295+
let id = Env.find_extension_identifier env ext.ext_id in
298296
let locs = None in
299297
let container = (parent : Identifier.Signature.t :> Identifier.Parent.t) in
300298
let label_container = (container :> Identifier.LabelParent.t) in
@@ -327,9 +325,7 @@ let read_type_extension env parent tyext =
327325

328326
let read_exception env parent (ext : extension_constructor) =
329327
let open Exception in
330-
let open Odoc_model.Names in
331-
let name = Ident.name ext.ext_id in
332-
let id = Identifier.Mk.exception_(parent, ExceptionName.make_std name) in
328+
let id = Env.find_exception_identifier env ext.ext_id in
333329
let locs = None in
334330
let container = (parent : Identifier.Signature.t :> Identifier.Parent.t) in
335331
let label_container = (container :> Identifier.LabelParent.t) in

src/loader/ident_env.cppo.ml

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type t =
3434
module_paths : P.Module.t Ident.tbl;
3535
module_types : Id.ModuleType.t Ident.tbl;
3636
types : Id.DataType.t Ident.tbl;
37+
exceptions: Id.Exception.t Ident.tbl;
38+
extensions: Id.Extension.t Ident.tbl;
3739
values: Id.Value.t Ident.tbl;
3840
classes : Id.Class.t Ident.tbl;
3941
class_types : Id.ClassType.t Ident.tbl;
@@ -46,6 +48,8 @@ let empty () =
4648
module_paths = Ident.empty;
4749
module_types = Ident.empty;
4850
types = Ident.empty;
51+
exceptions = Ident.empty;
52+
extensions = Ident.empty;
4953
values = Ident.empty;
5054
classes = Ident.empty;
5155
class_types = Ident.empty;
@@ -110,13 +114,19 @@ let rec extract_signature_type_items items =
110114
`ClassType (id, obj_id, None, false, None) :: extract_signature_type_items rest
111115
#endif
112116

113-
| Sig_typext _ :: rest ->
114-
extract_signature_type_items rest
117+
| Sig_typext (id, constr, Text_exception, Exported) :: rest ->
118+
`Exception (id, Some constr.ext_loc)
119+
:: extract_signature_type_items rest
120+
121+
| Sig_typext (id, constr, _, Exported) :: rest ->
122+
`Extension (id, Some constr.ext_loc)
123+
:: extract_signature_type_items rest
115124

116125
| Sig_class_type(_, _, _, Hidden) :: Sig_type(_, _, _, _)
117126
:: Sig_type(_, _, _, _) :: rest
118127
| Sig_class(_, _, _, Hidden) :: Sig_class_type(_, _, _, _)
119128
:: Sig_type(_, _, _, _) :: Sig_type(_, _, _, _) :: rest
129+
| Sig_typext (_,_,_,Hidden) :: rest
120130
| Sig_modtype(_, _, Hidden) :: rest
121131
| Sig_module(_, _, _, _, Hidden) :: rest
122132
| Sig_type(_, _, _, Hidden) :: rest
@@ -204,6 +214,18 @@ let rec extract_signature_tree_items : bool -> Typedtree.signature_item list ->
204214
else Some (`Type (decl.typ_id, hide_item, Some decl.typ_loc)))
205215
decls @ extract_signature_tree_items hide_item rest
206216

217+
#if OCAML_VERSION < (4,8,0)
218+
| { sig_desc = Tsig_exception tyexn_constructor; _ } :: rest ->
219+
#else
220+
| { sig_desc = Tsig_exception { tyexn_constructor; _ }; _ } :: rest ->
221+
#endif
222+
`Exception (tyexn_constructor.ext_id, Some tyexn_constructor.ext_loc) :: extract_signature_tree_items hide_item rest
223+
224+
| { sig_desc = Tsig_typext { tyext_constructors; _ }; _} :: rest ->
225+
let x = List.map (fun { ext_id; ext_loc; _ } -> `Extension (ext_id, Some ext_loc)) tyext_constructors in
226+
x @ extract_signature_tree_items hide_item rest
227+
228+
207229
#if OCAML_VERSION >= (4,10,0)
208230
| { sig_desc = Tsig_module { md_id = Some id; _ }; sig_loc; _} :: rest ->
209231
[`Module (id, hide_item, Some sig_loc)] @ extract_signature_tree_items hide_item rest
@@ -272,8 +294,6 @@ let rec extract_signature_tree_items : bool -> Typedtree.signature_item list ->
272294
| { sig_desc = Tsig_modtypesubst mtd; sig_loc; _ } :: rest ->
273295
[`ModuleType (mtd.mtd_id, hide_item, Some sig_loc)] @ extract_signature_tree_items hide_item rest
274296
#endif
275-
| { sig_desc = Tsig_typext _; _} :: rest
276-
| { sig_desc = Tsig_exception _; _} :: rest
277297
| { sig_desc = Tsig_open _;_} :: rest -> extract_signature_tree_items hide_item rest
278298
| [] -> []
279299

@@ -310,20 +330,16 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
310330
List.map (fun decl -> `Type (decl.typ_id, hide_item, Some decl.typ_loc))
311331
decls @ extract_structure_tree_items hide_item rest
312332

313-
#if OCAML_VERSION < (4,14,0)
314-
| { str_desc = Tstr_exception _; _ } :: rest -> extract_structure_tree_items hide_item rest
333+
#if OCAML_VERSION < (4,8,0)
334+
| { str_desc = Tstr_exception tyexn_constructor; _ } :: rest ->
315335
#else
316-
| { str_desc = Tstr_exception { tyexn_constructor; tyexn_loc = _; _ }; _ } :: rest ->
317-
`Exception (tyexn_constructor.ext_id, Some tyexn_constructor.ext_loc) :: extract_structure_tree_items hide_item rest
336+
| { str_desc = Tstr_exception { tyexn_constructor; _ }; _ } :: rest ->
318337
#endif
338+
`Exception (tyexn_constructor.ext_id, Some tyexn_constructor.ext_loc) :: extract_structure_tree_items hide_item rest
319339

320-
#if OCAML_VERSION < (4,14,0)
321-
| { str_desc = Tstr_typext _; _} :: rest -> extract_structure_tree_items hide_item rest
322-
#else
323340
| { str_desc = Tstr_typext { tyext_constructors; _ }; _} :: rest ->
324341
let x = List.map (fun { ext_id; ext_loc; _ } -> `Extension (ext_id, Some ext_loc)) tyext_constructors in
325342
x @ extract_structure_tree_items hide_item rest
326-
#endif
327343

328344
#if OCAML_VERSION < (4,3,0)
329345
| { str_desc = Tstr_value (_, vbs ); _} :: rest ->
@@ -401,7 +417,7 @@ let rec extract_structure_tree_items : bool -> Typedtree.structure_item list ->
401417
| [] -> []
402418

403419

404-
let flatten_extracted : items list -> item list = fun items ->
420+
let flatten_includes : items list -> item list = fun items ->
405421
List.map (function
406422
| `Type _
407423
| `Module _
@@ -451,13 +467,15 @@ let env_of_items : Id.Signature.t -> item list -> t -> t = fun parent items env
451467
let name = Ident.name t in
452468
let identifier = Mk.exception_(parent, ExceptionName.make_std name) in
453469
(match loc with | Some l -> LocHashtbl.add env.loc_to_ident l (identifier :> Id.any) | _ -> ());
454-
inner rest env
470+
let exceptions = Ident.add t identifier env.exceptions in
471+
inner rest {env with exceptions }
455472

456473
| `Extension (t, loc) :: rest ->
457474
let name = Ident.name t in
458475
let identifier = Mk.extension(parent, ExtensionName.make_std name) in
459476
(match loc with | Some l -> LocHashtbl.add env.loc_to_ident l (identifier :> Id.any) | _ -> ());
460-
inner rest env
477+
let extensions = Ident.add t identifier env.extensions in
478+
inner rest {env with extensions }
461479

462480
| `Value (t, is_hidden_item, loc) :: rest ->
463481
let name = Ident.name t in
@@ -545,12 +563,12 @@ let identifier_of_loc : t -> Warnings.loc -> Odoc_model.Paths.Identifier.t optio
545563

546564
let add_signature_tree_items : Paths.Identifier.Signature.t -> Typedtree.signature -> t -> t =
547565
fun parent sg env ->
548-
let items = extract_signature_tree_items false sg.sig_items |> flatten_extracted in
566+
let items = extract_signature_tree_items false sg.sig_items |> flatten_includes in
549567
env_of_items parent items env
550568

551569
let add_structure_tree_items : Paths.Identifier.Signature.t -> Typedtree.structure -> t -> t =
552570
fun parent sg env ->
553-
let items = extract_structure_tree_items false sg.str_items |> flatten_extracted in
571+
let items = extract_structure_tree_items false sg.str_items |> flatten_includes in
554572
env_of_items parent items env
555573

556574
let handle_signature_type_items : Paths.Identifier.Signature.t -> Compat.signature -> t -> t =
@@ -576,6 +594,12 @@ let find_module_type env id =
576594
let find_type_identifier env id =
577595
Ident.find_same id env.types
578596

597+
let find_exception_identifier env id =
598+
Ident.find_same id env.exceptions
599+
600+
let find_extension_identifier env id =
601+
Ident.find_same id env.extensions
602+
579603
let find_value_identifier env id =
580604
Ident.find_same id env.values
581605

src/loader/ident_env.cppo.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ val find_value_identifier : t -> Ident.t -> Paths.Identifier.Value.t
6666

6767
val find_type : t -> Ident.t -> Paths.Identifier.Path.Type.t
6868

69+
val find_extension_identifier : t -> Ident.t -> Paths.Identifier.Extension.t
70+
71+
val find_exception_identifier : t -> Ident.t -> Paths.Identifier.Exception.t
72+
6973
val find_type_identifier : t -> Ident.t -> Paths.Identifier.Type.t
7074

7175
val find_class_identifier : t -> Ident.t -> Paths.Identifier.Class.t

0 commit comments

Comments
 (0)