Skip to content

Commit ea588cd

Browse files
authored
flambda-backend: Record toplevel attributes in cms files (#2206)
* Record toplevel attributes in cms files * code review * We only need the first attribute * fix build
1 parent ac74282 commit ea588cd

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

driver/compile_common.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ let typecheck_intf info ast =
6363
Profile.(record_call typing) @@ fun () ->
6464
let tsg =
6565
ast
66-
|> Typemod.type_interface info.module_name info.env
66+
|> Typemod.type_interface
67+
~sourcefile:info.source_file
68+
info.module_name
69+
info.env
6770
|> print_if info.ppf_dump Clflags.dump_typedtree Printtyped.interface
6871
in
6972
let sg = tsg.Typedtree.sig_type in
@@ -117,7 +120,7 @@ let typecheck_impl i parsetree =
117120
parsetree
118121
|> Profile.(record typing)
119122
(Typemod.type_implementation
120-
i.source_file i.output_prefix i.module_name i.env)
123+
~sourcefile:i.source_file i.output_prefix i.module_name i.env)
121124
|> print_if i.ppf_dump Clflags.dump_typedtree
122125
Printtyped.implementation_with_coercion
123126
|> print_if i.ppf_dump Clflags.dump_shape

ocamldoc/odoc_analyse.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let process_implementation_file sourcefile =
8888
in
8989
let typedtree =
9090
Typemod.type_implementation
91-
sourcefile prefixname compilation_unit env parsetree
91+
~sourcefile prefixname compilation_unit env parsetree
9292
in
9393
(Some (parsetree, typedtree), inputfile)
9494
with
@@ -122,7 +122,7 @@ let process_interface_file sourcefile =
122122
Pparse.file ~tool_name inputfile
123123
(no_docstring Parse.interface) Pparse.Signature
124124
in
125-
let sg = Typemod.type_interface compilation_unit (initial_env()) ast in
125+
let sg = Typemod.type_interface ~sourcefile compilation_unit (initial_env()) ast in
126126
Warnings.check_fatal ();
127127
(ast, sg, inputfile)
128128

typing/typemod.ml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,32 @@ let gen_annot outputprefix sourcefile annots =
33073307
Cmt2annot.gen_annot (Some (outputprefix ^ ".annot"))
33083308
~sourcefile:(Some sourcefile) ~use_summaries:false annots
33093309

3310-
let type_implementation sourcefile outputprefix modulename initial_env ast =
3310+
let cms_register_toplevel_attributes ~sourcefile ~uid ~f ast =
3311+
(* Cms files do not store the typetree. This can be a problem for Merlin has
3312+
it uses attributes - which is why we manually construct a mapping from uid
3313+
to attributes while typing.
3314+
Generally `Pstr_attribute` and `Psig_attribute` are not needed by Merlin,
3315+
except if it is the first element of the compilation unit structure or
3316+
signature. *)
3317+
let attr =
3318+
match ast with
3319+
| x :: _ -> f x
3320+
| [] -> None
3321+
in
3322+
match attr with
3323+
| None -> ()
3324+
| Some attr ->
3325+
Env.register_uid uid
3326+
~loc:(Location.in_file sourcefile)
3327+
~attributes:[ attr ]
3328+
3329+
let cms_register_toplevel_struct_attributes ~sourcefile ~uid ast =
3330+
cms_register_toplevel_attributes ~sourcefile ~uid ast
3331+
~f:(function
3332+
| { pstr_desc = Pstr_attribute attr; _ } -> Some attr
3333+
| _ -> None)
3334+
3335+
let type_implementation ~sourcefile outputprefix modulename initial_env ast =
33113336
let error e =
33123337
raise (Error (Location.in_file sourcefile, initial_env, e))
33133338
in
@@ -3322,10 +3347,10 @@ let type_implementation sourcefile outputprefix modulename initial_env ast =
33223347
let (str, sg, names, shape, finalenv) =
33233348
Profile.record_call "infer" (fun () ->
33243349
type_structure initial_env ast) in
3325-
let shape =
3326-
Shape.set_uid_if_none shape
3327-
(Uid.of_compilation_unit_id modulename)
3328-
in
3350+
let uid = Uid.of_compilation_unit_id modulename in
3351+
let shape = Shape.set_uid_if_none shape uid in
3352+
if !Clflags.binary_annotations_cms then
3353+
cms_register_toplevel_struct_attributes ~sourcefile ~uid ast;
33293354
let simple_sg = Signature_names.simplify finalenv names sg in
33303355
if !Clflags.print_types then begin
33313356
remove_mode_and_jkind_variables finalenv sg;
@@ -3447,10 +3472,21 @@ let save_signature modname tsg outputprefix source_file initial_env cmi =
34473472
Cms_format.save_cms (outputprefix ^ ".cmsi") modname
34483473
(Some source_file) None
34493474

3450-
let type_interface modulename env ast =
3475+
let cms_register_toplevel_signature_attributes ~sourcefile ~uid ast =
3476+
cms_register_toplevel_attributes ~sourcefile ~uid ast
3477+
~f:(function
3478+
| { psig_desc = Psig_attribute attr; _ } -> Some attr
3479+
| _ -> None)
3480+
3481+
3482+
let type_interface ~sourcefile modulename env ast =
34513483
if !Clflags.as_parameter && Compilation_unit.is_packed modulename then begin
34523484
raise(Error(Location.none, Env.empty, Cannot_pack_parameter))
34533485
end;
3486+
if !Clflags.binary_annotations_cms then begin
3487+
let uid = Shape.Uid.of_compilation_unit_id modulename in
3488+
cms_register_toplevel_signature_attributes ~uid ~sourcefile ast
3489+
end;
34543490
transl_signature env ast
34553491

34563492
(* "Packaging" of several compilation units into one unit

typing/typemod.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ val type_toplevel_phrase:
3939
Typedtree.structure * Types.signature * Signature_names.t * Shape.t *
4040
Env.t
4141
val type_implementation:
42-
string -> string -> Compilation_unit.t -> Env.t ->
42+
sourcefile:string -> string -> Compilation_unit.t -> Env.t ->
4343
Parsetree.structure -> Typedtree.implementation
4444
val type_interface:
45-
Compilation_unit.t -> Env.t -> Parsetree.signature -> Typedtree.signature
45+
sourcefile:string
46+
-> Compilation_unit.t
47+
-> Env.t
48+
-> Parsetree.signature
49+
-> Typedtree.signature
4650
val transl_signature:
4751
Env.t -> Parsetree.signature -> Typedtree.signature
4852
val check_nongen_signature:

0 commit comments

Comments
 (0)