Skip to content

Commit 684b587

Browse files
committed
Unified handling of internal tags
Handle tags as soon as possible instead of passing them around. This ensures that internal tags are always handled.
1 parent 34d95d3 commit 684b587

File tree

14 files changed

+221
-713
lines changed

14 files changed

+221
-713
lines changed

src/loader/cmi.ml

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -332,26 +332,6 @@ let mark_class_declaration cld =
332332
List.iter mark_type_parameter cld.cty_params;
333333
mark_class_type cld.cty_params cld.cty_type
334334

335-
(* Handle internal tags *)
336-
337-
let rec find_tag f = function
338-
| [] -> None
339-
| hd :: tl -> (
340-
match f hd.Odoc_model.Location_.value with Some _ as x -> x | None -> find_tag f tl)
341-
342-
let find_status_tag tags =
343-
match
344-
find_tag
345-
(function (`Inline | `Open | `Closed) as t -> Some t | _ -> None)
346-
tags
347-
with
348-
| Some status -> status
349-
| None -> `Default
350-
351-
let find_canonical_tag tags : [ `Dot of Paths.Path.Module.t * string ] option =
352-
(* Ignore [`Root _] paths *)
353-
find_tag (function `Canonical (`Dot _ as p) -> Some p | _ -> None) tags
354-
355335
let rec read_type_expr env typ =
356336
let open TypeExpr in
357337
let typ = Btype.repr typ in
@@ -525,7 +505,7 @@ let read_value_description env parent id vd =
525505
let open Signature in
526506
let id = Env.find_value_identifier env id in
527507
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
528-
let doc, _ = Doc_attr.attached container vd.val_attributes in
508+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container vd.val_attributes in
529509
mark_value_description vd;
530510
let type_ = read_type_expr env vd.val_type in
531511
match vd.val_kind with
@@ -546,8 +526,8 @@ let read_label_declaration env parent ld =
546526
let open TypeDecl.Field in
547527
let name = Ident.name ld.ld_id in
548528
let id = `Field(parent, Odoc_model.Names.FieldName.make_std name) in
549-
let doc, _ =
550-
Doc_attr.attached
529+
let doc, () =
530+
Doc_attr.attached Odoc_model.Semantics.Expect_none
551531
(parent :> Identifier.LabelParent.t) ld.ld_attributes
552532
in
553533
let mutable_ = (ld.ld_mutable = Mutable) in
@@ -573,7 +553,7 @@ let read_constructor_declaration env parent cd =
573553
let name = Ident.name cd.cd_id in
574554
let id = `Constructor(parent, Odoc_model.Names.ConstructorName.make_std name) in
575555
let container = (parent : Identifier.DataType.t :> Identifier.LabelParent.t) in
576-
let doc, _ = Doc_attr.attached container cd.cd_attributes in
556+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container cd.cd_attributes in
577557
let args =
578558
read_constructor_declaration_arguments env
579559
(parent :> Identifier.Parent.t) cd.cd_args
@@ -634,8 +614,10 @@ let read_type_declaration env parent id decl =
634614
let open TypeDecl in
635615
let id = Env.find_type_identifier env id in
636616
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
637-
let doc, internal_tags = Doc_attr.attached container decl.type_attributes in
638-
let canonical = (find_canonical_tag internal_tags :> Path.Type.t option) in
617+
let doc, canonical =
618+
Doc_attr.attached Odoc_model.Semantics.Expect_canonical container decl.type_attributes
619+
in
620+
let canonical = (canonical :> Path.Type.t option) in
639621
let params = mark_type_declaration decl in
640622
let manifest = opt_map (read_type_expr env) decl.type_manifest in
641623
let constraints = read_type_constraints env params in
@@ -664,7 +646,7 @@ let read_extension_constructor env parent id ext =
664646
let name = Ident.name id in
665647
let id = `Extension(parent, Odoc_model.Names.ExtensionName.make_std name) in
666648
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
667-
let doc, _ = Doc_attr.attached container ext.ext_attributes in
649+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container ext.ext_attributes in
668650
let args =
669651
read_constructor_declaration_arguments env
670652
(parent : Identifier.Signature.t :> Identifier.Parent.t) ext.ext_args
@@ -697,7 +679,7 @@ let read_exception env parent id ext =
697679
let name = Ident.name id in
698680
let id = `Exception(parent, Odoc_model.Names.ExceptionName.make_std name) in
699681
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
700-
let doc, _ = Doc_attr.attached container ext.ext_attributes in
682+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container ext.ext_attributes in
701683
mark_exception ext;
702684
let args =
703685
read_constructor_declaration_arguments env
@@ -795,7 +777,7 @@ let read_class_type_declaration env parent id cltd =
795777
let open ClassType in
796778
let id = Env.find_class_type_identifier env id in
797779
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
798-
let doc, _ = Doc_attr.attached container cltd.clty_attributes in
780+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container cltd.clty_attributes in
799781
mark_class_type_declaration cltd;
800782
let params =
801783
List.map2
@@ -830,7 +812,7 @@ let read_class_declaration env parent id cld =
830812
let open Class in
831813
let id = Env.find_class_identifier env id in
832814
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
833-
let doc, _ = Doc_attr.attached container cld.cty_attributes in
815+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container cld.cty_attributes in
834816
mark_class_declaration cld;
835817
let params =
836818
List.map2
@@ -869,17 +851,17 @@ and read_module_type_declaration env parent id (mtd : Odoc_model.Compat.modtype_
869851
let open ModuleType in
870852
let id = Env.find_module_type env id in
871853
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
872-
let doc, internal_tags = Doc_attr.attached container mtd.mtd_attributes in
873-
let canonical = (find_canonical_tag internal_tags :> Path.ModuleType.t option) in
854+
let doc, canonical = Doc_attr.attached Odoc_model.Semantics.Expect_canonical container mtd.mtd_attributes in
855+
let canonical = (canonical :> Path.ModuleType.t option) in
874856
let expr = opt_map (read_module_type env (id :> Identifier.Signature.t)) mtd.mtd_type in
875857
{id; doc; canonical; expr }
876858

877859
and read_module_declaration env parent ident (md : Odoc_model.Compat.module_declaration) =
878860
let open Module in
879861
let id = (Env.find_module_identifier env ident :> Identifier.Module.t) in
880862
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
881-
let doc, internal_tags = Doc_attr.attached container md.md_attributes in
882-
let canonical = (find_canonical_tag internal_tags :> Path.Module.t option) in
863+
let doc, canonical = Doc_attr.attached Odoc_model.Semantics.Expect_canonical container md.md_attributes in
864+
let canonical = (canonical :> Path.Module.t option) in
883865
let type_ =
884866
match md.md_type with
885867
| Mty_alias p -> Alias (Env.Path.read_module env p, None)

src/loader/cmi.mli

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ val read_interface :
2525
Odoc_model.Compat.signature ->
2626
Paths.Identifier.RootModule.t * Odoc_model.Lang.Signature.t
2727

28-
val find_status_tag :
29-
Odoc_model.Semantics.internal_tags -> [ `Default | `Inline | `Open | `Closed ]
30-
31-
val find_canonical_tag :
32-
Odoc_model.Semantics.internal_tags ->
33-
[ `Dot of Paths.Path.Module.t * string ] option
34-
3528
#if OCAML_MAJOR = 4 && OCAML_MINOR = 02
3629
val read_label : Asttypes.label -> Odoc_model.Lang.TypeExpr.label option
3730
#else

src/loader/cmt.ml

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let rec read_pattern env parent doc pat =
7171

7272
let read_value_binding env parent vb =
7373
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
74-
let doc, _ = Doc_attr.attached container vb.vb_attributes in
74+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container vb.vb_attributes in
7575
read_pattern env parent doc vb.vb_pat
7676

7777
let read_value_bindings env parent vbs =
@@ -93,7 +93,7 @@ let read_type_extension env parent tyext =
9393
let open Extension in
9494
let type_path = Env.Path.read_type env tyext.tyext_path in
9595
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
96-
let doc, _ = Doc_attr.attached container tyext.tyext_attributes in
96+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container tyext.tyext_attributes in
9797
let type_params =
9898
List.map (fun (ctyp, _) -> ctyp.ctyp_type) tyext.tyext_params
9999
in
@@ -123,7 +123,7 @@ let rec read_class_type_field env parent ctf =
123123
let open Odoc_model.Names in
124124

125125
let container = (parent : Identifier.ClassSignature.t :> Identifier.LabelParent.t) in
126-
let doc, _ = Doc_attr.attached container ctf.ctf_attributes in
126+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container ctf.ctf_attributes in
127127
match ctf.ctf_desc with
128128
| Tctf_val(name, mutable_, virtual_, typ) ->
129129
let open InstanceVariable in
@@ -204,7 +204,7 @@ let rec read_class_field env parent cf =
204204
let open ClassSignature in
205205
let open Odoc_model.Names in
206206
let container = (parent : Identifier.ClassSignature.t :> Identifier.LabelParent.t) in
207-
let doc, _ = Doc_attr.attached container (cf.cf_attributes) in
207+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container (cf.cf_attributes) in
208208
match cf.cf_desc with
209209
| Tcf_val({txt = name; _}, mutable_, _, kind, _) ->
210210
let open InstanceVariable in
@@ -306,7 +306,7 @@ let read_class_declaration env parent cld =
306306
let open Class in
307307
let id = Env.find_class_identifier env cld.ci_id_class in
308308
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
309-
let doc, _ = Doc_attr.attached container cld.ci_attributes in
309+
let doc, () = Doc_attr.attached Odoc_model.Semantics.Expect_none container cld.ci_attributes in
310310
Cmi.mark_class_declaration cld.ci_decl;
311311
let virtual_ = (cld.ci_virt = Virtual) in
312312
let clparams =
@@ -338,7 +338,9 @@ let rec read_module_expr env parent label_parent mexpr =
338338
match mexpr.mod_desc with
339339
| Tmod_ident _ ->
340340
Cmi.read_module_type env parent (Odoc_model.Compat.module_type mexpr.mod_type)
341-
| Tmod_structure str -> Signature (fst (read_structure env parent str))
341+
| Tmod_structure str ->
342+
let sg, () = read_structure Odoc_model.Semantics.Expect_none env parent str in
343+
Signature sg
342344
#if OCAML_MAJOR = 4 && OCAML_MINOR >= 10
343345
| Tmod_functor(parameter, res) ->
344346
let f_parameter, env =
@@ -397,8 +399,8 @@ and read_module_binding env parent mb =
397399
#endif
398400
let id = (id :> Identifier.Module.t) in
399401
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
400-
let doc, internal_tags = Doc_attr.attached container mb.mb_attributes in
401-
let canonical = (Cmi.find_canonical_tag internal_tags :> Path.Module.t option) in
402+
let doc, canonical = Doc_attr.attached Odoc_model.Semantics.Expect_canonical container mb.mb_attributes in
403+
let canonical = (canonical :> Path.Module.t option) in
402404
let type_ =
403405
match unwrap_module_expr_desc mb.mb_expr.mod_desc with
404406
| Tmod_ident(p, _) -> Alias (Env.Path.read_module env p, None)
@@ -504,7 +506,7 @@ and read_structure_item env parent item =
504506
and read_include env parent incl =
505507
let open Include in
506508
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
507-
let doc, internal_tags = Doc_attr.attached container incl.incl_attributes in
509+
let doc, status = Doc_attr.attached Odoc_model.Semantics.Expect_status container incl.incl_attributes in
508510
let decl_modty =
509511
match unwrap_module_expr_desc incl.incl_mod.mod_desc with
510512
| Tmod_ident(p, _) ->
@@ -524,7 +526,6 @@ and read_include env parent incl =
524526
| Some m when not (contains_signature m) ->
525527
let decl = ModuleType m in
526528
let expansion = { content; shadowed; } in
527-
let status = Cmi.find_status_tag internal_tags in
528529
[Include {parent; doc; decl; expansion; status }]
529530
| Some (ModuleType.U.Signature { items; _ }) ->
530531
items
@@ -537,16 +538,19 @@ and read_open env parent o =
537538
Open.{expansion}
538539
#endif
539540

540-
and read_structure env parent str =
541+
and read_structure :
542+
'tags. 'tags Odoc_model.Semantics.handle_internal_tags -> _ -> _ -> _ ->
543+
_ * 'tags =
544+
fun internal_tags env parent str ->
541545
let env = Env.add_structure_tree_items parent str env in
542-
let items, doc, internal_tags =
546+
let items, doc, tags =
543547
let classify item =
544548
match item.str_desc with
545549
| Tstr_open _ -> Some `Open
546550
| Tstr_attribute attr -> Some (`Attribute attr)
547551
| _ -> None
548552
in
549-
Doc_attr.extract_top_comment ~classify parent str.str_items
553+
Doc_attr.extract_top_comment internal_tags ~classify parent str.str_items
550554
in
551555
let items =
552556
List.fold_left
@@ -555,11 +559,13 @@ and read_structure env parent str =
555559
[] items
556560
|> List.rev
557561
in
558-
({ items; compiled = false; doc }, internal_tags)
562+
({ Signature.items; compiled = false; doc }, tags)
559563

560564
let read_implementation root name impl =
561565
let id = `Root (root, Odoc_model.Names.ModuleName.make_std name) in
562-
let sg, internal_tags = read_structure Env.empty id impl in
563-
(id, sg, internal_tags)
566+
let sg, canonical =
567+
read_structure Odoc_model.Semantics.Expect_canonical Env.empty id impl
568+
in
569+
(id, sg, (canonical :> Odoc_model.Paths.Path.Module.t option))
564570

565571
let _ = Cmti.read_module_expr := read_module_expr

src/loader/cmt.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ val read_implementation :
2020
Typedtree.structure ->
2121
Odoc_model.Paths.Identifier.RootModule.t
2222
* Odoc_model.Lang.Signature.t
23-
* Odoc_model.Semantics.internal_tags
23+
* Odoc_model.Paths.Path.Module.t option
24+
(** Returns [id, sg, canonical_path]. [canonical_path] is the path set from the
25+
[@canonical] tag. *)

0 commit comments

Comments
 (0)