Skip to content

Commit d318a5f

Browse files
Allow signature to be parent in constructor references
This allows to omit the type the constructor is coming from. The constructor will be fetched from the environment. The commit does this in a similar way to fields, which can have type `parent` for parents of type references. Co-authored-by: Paul-Elliot <[email protected]> Signed-off-by: Paul-Elliot <[email protected]>
1 parent 7b66c13 commit d318a5f

File tree

13 files changed

+133
-104
lines changed

13 files changed

+133
-104
lines changed

src/loader/cmi.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ let read_constructor_declaration_arguments env parent arg =
628628
let read_constructor_declaration env parent cd =
629629
let open TypeDecl.Constructor in
630630
let id = Ident_env.find_constructor_identifier env cd.cd_id in
631-
let container = (parent : Identifier.DataType.t :> Identifier.LabelParent.t) in
631+
let container = (parent :> Identifier.LabelParent.t) in
632632
let doc = Doc_attr.attached_no_tag container cd.cd_attributes in
633633
let args =
634634
read_constructor_declaration_arguments env
@@ -713,7 +713,7 @@ let read_type_declaration env parent id decl =
713713
let params = mark_type_declaration decl in
714714
let manifest = opt_map (read_type_expr env) decl.type_manifest in
715715
let constraints = read_type_constraints env params in
716-
let representation = read_type_kind env id decl.type_kind in
716+
let representation = read_type_kind env (id :> Identifier.Parent.t) decl.type_kind in
717717
let abstr =
718718
match decl.type_kind with
719719
Type_abstract ->

src/loader/cmti.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ let read_constructor_declaration_arguments env parent label_parent arg =
214214
let read_constructor_declaration env parent cd =
215215
let open TypeDecl.Constructor in
216216
let id = Ident_env.find_constructor_identifier env cd.cd_id in
217-
let container = (parent : Identifier.DataType.t :> Identifier.Parent.t) in
217+
let container = parent in
218218
let label_container = (container :> Identifier.LabelParent.t) in
219219
let doc = Doc_attr.attached_no_tag label_container cd.cd_attributes in
220220
let args =
@@ -231,7 +231,7 @@ let read_type_kind env parent =
231231
let cstrs = List.map (read_constructor_declaration env parent) cstrs in
232232
Some (Variant cstrs)
233233
| Ttype_record lbls ->
234-
let parent = (parent : Identifier.DataType.t :> Identifier.Parent.t) in
234+
let parent = (parent ) in
235235
let label_parent = (parent :> Identifier.LabelParent.t) in
236236
let lbls =
237237
List.map (read_label_declaration env parent label_parent) lbls in
@@ -260,7 +260,7 @@ let read_type_declaration env parent decl =
260260
let doc, canonical = Doc_attr.attached Odoc_model.Semantics.Expect_canonical container decl.typ_attributes in
261261
let canonical = (canonical :> Path.Type.t option) in
262262
let equation = read_type_equation env container decl in
263-
let representation = read_type_kind env (id :> Identifier.DataType.t) decl.typ_kind in
263+
let representation = read_type_kind env (id :> Identifier.Parent.t) decl.typ_kind in
264264
{id; locs; doc; canonical; equation; representation}
265265

266266
let read_type_declarations env parent rec_flag decls =

src/model/paths.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module Identifier = struct
165165
| { iv = `Label (p, _); _ } -> p
166166
| { iv = `Method (p, _); _ } | { iv = `InstanceVariable (p, _); _ } ->
167167
(p : class_signature :> label_parent)
168-
| { iv = `Constructor (p, _); _ } -> (p : datatype :> label_parent)
168+
| { iv = `Constructor (p, _); _ } -> (p : parent :> label_parent)
169169
| { iv = `Field (p, _); _ } -> (p : parent :> label_parent)
170170

171171
let label_parent n = label_parent_aux (n :> Id.non_src)
@@ -572,8 +572,8 @@ module Identifier = struct
572572
mk_fresh (fun s -> s) "coret" (fun s -> `CoreType (TypeName.make_std s))
573573

574574
let constructor :
575-
Type.t * ConstructorName.t ->
576-
[> `Constructor of Type.t * ConstructorName.t ] id =
575+
Parent.t * ConstructorName.t ->
576+
[> `Constructor of Parent.t * ConstructorName.t ] id =
577577
mk_parent ConstructorName.to_string "ctor" (fun (p, n) ->
578578
`Constructor (p, n))
579579

@@ -1013,8 +1013,7 @@ module Reference = struct
10131013
| `Class _ | `ClassType _ | `ModuleType _ ) as r ->
10141014
(label_parent_identifier r :> Identifier.t)
10151015
| `Field (p, n) -> Identifier.Mk.field (parent_identifier p, n)
1016-
| `Constructor (s, n) ->
1017-
Identifier.Mk.constructor (parent_type_identifier s, n)
1016+
| `Constructor (s, n) -> Identifier.Mk.constructor (parent_identifier s, n)
10181017
| `Extension (p, q) ->
10191018
Identifier.Mk.extension (parent_signature_identifier p, q)
10201019
| `ExtensionDecl (p, q, r) ->

src/model/paths.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ module Identifier : sig
290290
val core_type : string -> [> `CoreType of TypeName.t ] id
291291

292292
val constructor :
293-
Type.t * ConstructorName.t ->
294-
[> `Constructor of Type.t * ConstructorName.t ] id
293+
Parent.t * ConstructorName.t ->
294+
[> `Constructor of Parent.t * ConstructorName.t ] id
295295

296296
val field :
297297
Parent.t * FieldName.t -> [> `Field of Parent.t * FieldName.t ] id

src/model/paths_types.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ module Identifier = struct
132132
and type_ = type_pv id
133133
(** @canonical Odoc_model.Paths.Identifier.Type.t *)
134134

135-
type constructor_pv = [ `Constructor of type_ * ConstructorName.t ]
135+
type constructor_pv = [ `Constructor of parent * ConstructorName.t ]
136136
(** @canonical Odoc_model.Paths.Identifier.Constructor.t_pv *)
137137

138138
and constructor = constructor_pv id
@@ -666,7 +666,7 @@ module rec Reference : sig
666666
[ `Resolved of Resolved_reference.constructor
667667
| `Root of string * [ `TConstructor | `TExtension | `TException | `TUnknown ]
668668
| `Dot of label_parent * string
669-
| `Constructor of datatype * ConstructorName.t
669+
| `Constructor of parent * ConstructorName.t
670670
| `Extension of signature * ExtensionName.t
671671
| `Exception of signature * ExceptionName.t ]
672672
(** @canonical Odoc_model.Paths.Reference.Constructor.t *)
@@ -756,7 +756,7 @@ module rec Reference : sig
756756
| `Module of signature * ModuleName.t
757757
| `ModuleType of signature * ModuleTypeName.t
758758
| `Type of signature * TypeName.t
759-
| `Constructor of datatype * ConstructorName.t
759+
| `Constructor of parent * ConstructorName.t
760760
| `Field of parent * FieldName.t
761761
| `Extension of signature * ExtensionName.t
762762
| `ExtensionDecl of signature * ExtensionName.t
@@ -847,7 +847,7 @@ and Resolved_reference : sig
847847

848848
type constructor =
849849
[ `Identifier of Identifier.reference_constructor
850-
| `Constructor of datatype * ConstructorName.t
850+
| `Constructor of parent * ConstructorName.t
851851
| `Extension of signature * ExtensionName.t
852852
| `Exception of signature * ExceptionName.t ]
853853
(** @canonical Odoc_model.Paths.Reference.Resolved.Constructor.t *)
@@ -920,7 +920,7 @@ and Resolved_reference : sig
920920
| `Hidden of module_
921921
| `ModuleType of signature * ModuleTypeName.t
922922
| `Type of signature * TypeName.t
923-
| `Constructor of datatype * ConstructorName.t
923+
| `Constructor of parent * ConstructorName.t
924924
| `Field of parent * FieldName.t
925925
| `Extension of signature * ExtensionName.t
926926
| `ExtensionDecl of signature * ExtensionName.t * ExtensionName.t

src/model/reference.ml

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,10 @@ let parse whole_reference_location s :
219219
match tokens with
220220
| [] -> (
221221
match kind with
222-
| (`TUnknown | `TModule | `TModuleType | `TType | `TClass | `TClassType)
223-
as kind ->
222+
| (`TUnknown | `TModule | `TModuleType | `TType) as kind ->
224223
`Root (identifier, kind)
225224
| _ ->
226-
expected
227-
[ "module"; "module-type"; "type"; "class"; "class-type" ]
228-
location
225+
expected [ "module"; "module-type"; "type" ] location
229226
|> Error.raise_exception)
230227
| next_token :: tokens -> (
231228
match kind with
@@ -238,15 +235,8 @@ let parse whole_reference_location s :
238235
(signature next_token tokens, ModuleTypeName.make_std identifier)
239236
| `TType ->
240237
`Type (signature next_token tokens, TypeName.make_std identifier)
241-
| `TClass ->
242-
`Class (signature next_token tokens, ClassName.make_std identifier)
243-
| `TClassType ->
244-
`ClassType
245-
(signature next_token tokens, ClassTypeName.make_std identifier)
246238
| _ ->
247-
expected
248-
[ "module"; "module-type"; "type"; "class"; "class-type" ]
249-
location
239+
expected [ "module"; "module-type"; "type" ] location
250240
|> Error.raise_exception)
251241
in
252242

@@ -273,22 +263,6 @@ let parse whole_reference_location s :
273263
)
274264
in
275265

276-
let datatype (kind, identifier, location) tokens : DataType.t =
277-
let kind = match_reference_kind location kind in
278-
match tokens with
279-
| [] -> (
280-
match kind with
281-
| (`TUnknown | `TType) as kind -> `Root (identifier, kind)
282-
| _ -> expected [ "type" ] location |> Error.raise_exception)
283-
| next_token :: tokens -> (
284-
match kind with
285-
| `TUnknown ->
286-
`Dot ((parent next_token tokens :> LabelParent.t), identifier)
287-
| `TType ->
288-
`Type (signature next_token tokens, TypeName.make_std identifier)
289-
| _ -> expected [ "type" ] location |> Error.raise_exception)
290-
in
291-
292266
let rec label_parent (kind, identifier, location) tokens : LabelParent.t =
293267
let kind = match_reference_kind location kind in
294268
match tokens with
@@ -360,7 +334,7 @@ let parse whole_reference_location s :
360334
`Type (signature next_token tokens, TypeName.make_std identifier)
361335
| `TConstructor ->
362336
`Constructor
363-
(datatype next_token tokens, ConstructorName.make_std identifier)
337+
(parent next_token tokens, ConstructorName.make_std identifier)
364338
| `TField ->
365339
`Field (parent next_token tokens, FieldName.make_std identifier)
366340
| `TExtension ->

src/xref2/env.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ let add_module identifier m docs env =
288288
let env' = add_to_elts Kind_Module identifier (`Module (identifier, m)) env in
289289
if env.linking then add_cdocs identifier docs env' else env'
290290

291-
let add_type identifier t env =
291+
let add_type (identifier : Identifier.Type.t) t env =
292292
let open Component in
293293
let open_typedecl cs =
294294
let add_cons env (cons : TypeDecl.Constructor.t) =
295295
let ident =
296296
Paths.Identifier.Mk.constructor
297-
(identifier, ConstructorName.make_std cons.name)
297+
( (identifier :> Identifier.Parent.t),
298+
ConstructorName.make_std cons.name )
298299
in
299300
add_to_elts Kind_Constructor ident (`Constructor (ident, cons)) env
300301
and add_field env (field : TypeDecl.Field.t) =

src/xref2/lang_of.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,9 @@ and type_decl_representation map id (t : Component.TypeDecl.Representation.t) :
940940
Odoc_model.Lang.TypeDecl.Representation.t =
941941
match t with
942942
| Extensible -> Extensible
943-
| Variant cs -> Variant (List.map (type_decl_constructor map id) cs)
943+
| Variant cs ->
944+
Variant
945+
(List.map (type_decl_constructor map (id :> Identifier.Parent.t)) cs)
944946
| Record fs ->
945947
Record
946948
(List.map
@@ -949,7 +951,7 @@ and type_decl_representation map id (t : Component.TypeDecl.Representation.t) :
949951

950952
and type_decl_constructor :
951953
maps ->
952-
Odoc_model.Paths.Identifier.Type.t ->
954+
Odoc_model.Paths.Identifier.Parent.t ->
953955
Component.TypeDecl.Constructor.t ->
954956
Odoc_model.Lang.TypeDecl.Constructor.t =
955957
fun map id t ->
@@ -959,11 +961,8 @@ and type_decl_constructor :
959961
{
960962
id = identifier;
961963
doc = docs (id :> Identifier.LabelParent.t) t.doc;
962-
args =
963-
type_decl_constructor_argument map
964-
(id :> Odoc_model.Paths.Identifier.Parent.t)
965-
t.args;
966-
res = Opt.map (type_expr map (id :> Identifier.Parent.t)) t.res;
964+
args = type_decl_constructor_argument map id t.args;
965+
res = Opt.map (type_expr map id) t.res;
967966
}
968967

969968
and type_expr_package map parent t =

src/xref2/lang_of.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ val type_decl_representation :
217217

218218
val type_decl_constructor :
219219
maps ->
220-
Identifier.Type.t ->
220+
Identifier.Parent.t ->
221221
Component.TypeDecl.Constructor.t ->
222222
Odoc_model.Lang.TypeDecl.Constructor.t
223223

src/xref2/ref_tools.ml

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,6 @@ module DT = struct
279279
let of_component _env t ~parent_ref name = Ok (`Type (parent_ref, name), t)
280280

281281
let of_element _env (`Type (id, t)) : t = (`Identifier id, t)
282-
283-
let in_env env name =
284-
env_lookup_by_name Env.s_type name env >>= fun e -> Ok (of_element env e)
285-
286-
let in_signature _env ((parent', parent_cp, sg) : signature_lookup_result)
287-
name =
288-
let sg = Tools.prefix_signature (parent_cp, sg) in
289-
find Find.datatype_in_sig sg name >>= fun (`FType (name, t)) ->
290-
Ok (`Type (parent', name), t)
291282
end
292283

293284
module T = struct
@@ -448,14 +439,33 @@ module CS = struct
448439
env_lookup_by_name Env.s_constructor name env
449440
>>= fun (`Constructor (id, _)) -> Ok (`Identifier id :> t)
450441

451-
let in_datatype _env ((parent', t) : datatype_lookup_result) name =
442+
let got_a_field name =
443+
(* Let's pretend we didn't see the field and say we didn't find anything. *)
444+
Error (`Find_by_name (`Cons, name))
445+
446+
let in_parent _env (parent : label_parent_lookup_result) name =
452447
let name_s = ConstructorName.to_string name in
453-
find Find.any_in_type t name_s >>= function
454-
| `FConstructor _ -> Ok (`Constructor (parent', name))
455-
| `FField _ -> Error (`Find_by_name (`Cons, name_s))
448+
match parent with
449+
| `S (parent', parent_cp, sg) -> (
450+
let sg = Tools.prefix_signature (parent_cp, sg) in
451+
find_ambiguous Find.any_in_type_in_sig sg name_s >>= function
452+
| `In_type (_, _, `FField _) -> got_a_field name_s
453+
| `In_type (typ_name, _, `FConstructor _) ->
454+
Ok (`Constructor (`Type (parent', typ_name), name)))
455+
| `T (parent', t) -> (
456+
find Find.any_in_type t name_s >>= function
457+
| `FField _ -> got_a_field name_s
458+
| `FConstructor _ ->
459+
Ok
460+
(`Constructor
461+
((parent' : Resolved.DataType.t :> Resolved.Parent.t), name)))
462+
| (`C _ | `CT _ | `P _) as r -> wrong_kind_error [ `S; `T ] r
456463

457464
let of_component _env parent name =
458-
Ok (`Constructor (parent, ConstructorName.make_std name))
465+
Ok
466+
(`Constructor
467+
( (parent : Resolved.DataType.t :> Resolved.Parent.t),
468+
ConstructorName.make_std name ))
459469
end
460470

461471
module F = struct
@@ -479,7 +489,8 @@ module F = struct
479489
find_ambiguous Find.any_in_type_in_sig sg name_s >>= function
480490
| `In_type (_, _, `FConstructor _) -> got_a_constructor name_s
481491
| `In_type (typ_name, _, `FField _) ->
482-
Ok (`Field (`Type (parent', typ_name), name)))
492+
Ok (`Field ((`Type (parent', typ_name) :> Resolved.Parent.t), name))
493+
)
483494
| `T (parent', t) -> (
484495
find Find.any_in_type t name_s >>= function
485496
| `FConstructor _ -> got_a_constructor name_s
@@ -662,20 +673,6 @@ and resolve_signature_reference :
662673
in
663674
resolve env'
664675

665-
and resolve_datatype_reference :
666-
Env.t -> DataType.t -> datatype_lookup_result ref_result =
667-
fun env r ->
668-
match r with
669-
| `Resolved _ -> failwith "TODO"
670-
| `Root (name, (`TType | `TUnknown)) -> DT.in_env env name
671-
| `Type (parent, name) ->
672-
resolve_signature_reference env parent >>= fun p ->
673-
DT.in_signature env p (TypeName.to_string name)
674-
| `Dot (parent, name) ->
675-
resolve_label_parent_reference env parent
676-
>>= signature_lookup_result_of_label_parent
677-
>>= fun p -> DT.in_signature env p name
678-
679676
and resolve_module_reference env (r : Module.t) : M.t ref_result =
680677
match r with
681678
| `Resolved _r -> failwith "What's going on!?"
@@ -818,8 +815,8 @@ let resolve_reference =
818815
| `Dot (parent, name) -> resolve_reference_dot env parent name
819816
| `Root (name, `TConstructor) -> CS.in_env env name >>= resolved1
820817
| `Constructor (parent, name) ->
821-
resolve_datatype_reference env parent >>= fun p ->
822-
CS.in_datatype env p name >>= resolved1
818+
resolve_label_parent_reference env (parent : Parent.t :> LabelParent.t)
819+
>>= fun p -> CS.in_parent env p name >>= resolved1
823820
| `Root (name, `TException) -> EX.in_env env name >>= resolved1
824821
| `Exception (parent, name) ->
825822
resolve_signature_reference env parent >>= fun p ->

0 commit comments

Comments
 (0)