Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions analysis/src/process_cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ let attrs_to_docstring attrs =
| None -> []
| Some docstring -> [docstring]

let map_record_field {Types.ld_id; ld_type; ld_attributes; ld_optional} =
let astamp = Ident.binding_time ld_id in
let map_record_field ~env {Types.ld_id; ld_type; ld_attributes; ld_optional} =
let astamp = Env.fresh_label_stamp env in
let name = Ident.name ld_id in
{
stamp = astamp;
Expand All @@ -35,29 +35,33 @@ let map_record_field {Types.ld_id; ld_type; ld_attributes; ld_optional} =
deprecated = Process_attributes.find_deprecated_attribute ld_attributes;
}

(* A member read from a signature, unless this file already has it: a
structure that shadows one of its members gets an implicit constraint to its
signature, which lists the remaining members of the structure again. Keep
what the structure gave them, in particular the path of an included copy,
and only export them. *)
let add_signature_declared ~(name : string Location.loc) ~extent ~stamp
~(env : Env.t) ~item attributes add_exported find_stamp add_stamp =
match find_stamp env.stamps stamp with
| Some declared ->
ignore (add_exported name.txt stamp : bool);
declared
| None ->
add_declared ~name ~extent ~stamp ~env ~item:(item ()) attributes
add_exported add_stamp

let rec for_type_signature_item ~(env : Shared_types.Env.t)
~(exported : Exported.t) (item : Types.signature_item) =
match item with
| Sig_value (ident, {val_type; val_attributes; val_loc = loc}) ->
let item = val_type in
let stamp = Ident.binding_time ident in
let old_declared = Stamps.find_value env.stamps stamp in
let declared =
add_declared
add_signature_declared
~name:(Location.mkloc (Ident.name ident) loc)
~extent:loc ~stamp ~env ~item val_attributes
~extent:loc ~stamp:(Ident.binding_time ident) ~env
~item:(fun () -> val_type)
val_attributes
(Exported.add exported Exported.Value)
Stamps.add_value
in
let declared =
(* When an id is shadowed, a module constraint without the doc comment is created.
Here the existing doc comment is restored. See https://github.com/rescript-lang/rescript-vscode/issues/621 *)
match old_declared with
| Some old_declared when declared.docstring = [] ->
let new_declared = {declared with docstring = old_declared.docstring} in
Stamps.add_value env.stamps stamp new_declared;
new_declared
| _ -> declared
Stamps.find_value Stamps.add_value
in
[
{
Expand All @@ -74,8 +78,8 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
rec_status ) ->
let declared =
let name = Location.mknoloc (Ident.name ident) in
add_declared ~extent:type_loc
~item:
add_signature_declared ~extent:type_loc
~item:(fun () ->
{
Type.decl;
attributes = type_attributes;
Expand All @@ -98,7 +102,7 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
{Types.cd_loc; cd_id; cd_args; cd_res; cd_attributes}
->
let name = Ident.name cd_id in
let stamp = Ident.binding_time cd_id in
let stamp = Env.fresh_label_stamp env in
let item =
{
Constructor.stamp;
Expand All @@ -111,7 +115,7 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
|> List.map (fun t -> (t, Location.none)))
| Cstr_record fields ->
InlineRecord
(fields |> List.map map_record_field));
(fields |> List.map (map_record_field ~env)));
res = cd_res;
type_decl = (name, decl);
docstring = attrs_to_docstring cd_attributes;
Expand All @@ -129,11 +133,11 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
Stamps.add_constructor env.stamps stamp declared;
item))
| Type_record (fields, _) ->
Record (fields |> List.map map_record_field));
}
Record (fields |> List.map (map_record_field ~env)));
})
~name ~stamp:(Ident.binding_time ident) ~env type_attributes
(Exported.add exported Exported.Type)
Stamps.add_type
Stamps.find_type Stamps.add_type
in
[
{
Expand All @@ -147,12 +151,13 @@ let rec for_type_signature_item ~(env : Shared_types.Env.t)
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
let name = Ident.name ident in
let declared =
add_declared ~extent:md_loc
~item:(for_type_module ~name ~env:(env |> Env.add_module ~name) md_type)
add_signature_declared ~extent:md_loc
~item:(fun () ->
for_type_module ~name ~env:(env |> Env.add_module ~name) md_type)
~name:(Location.mkloc name md_loc)
~stamp:(Ident.binding_time ident) ~env md_attributes
(Exported.add exported Exported.Module)
Stamps.add_module
Stamps.find_module Stamps.add_module
in
[
{
Expand Down Expand Up @@ -185,10 +190,21 @@ and for_type_module ~name ~env module_type =
| Mty_functor (_argIdent, _argType, result_type) ->
for_type_module ~name ~env result_type

let rec get_module_path mod_desc =
match mod_desc with
| Typedtree.Tmod_ident (path, _lident) -> Some path
| Tmod_structure _ -> None
| Tmod_functor (_ident, _argName, _maybeType, _resultExpr) -> None
| Tmod_apply (functor_, _arg, _coercion) -> get_module_path functor_.mod_desc
| Tmod_unpack (_expr, _moduleType) -> None
| Tmod_constraint (expr, _typ, _constraint, _coercion) ->
get_module_path expr.mod_desc

let get_module_type_path mod_desc =
match mod_desc with
| Typedtree.Tmty_ident (path, _) | Tmty_alias (path, _) -> Some path
| Tmty_signature _ | Tmty_functor _ | Tmty_with _ | Tmty_typeof _ -> None
| Tmty_typeof module_expr -> get_module_path module_expr.mod_desc
| Tmty_signature _ | Tmty_functor _ | Tmty_with _ -> None

let for_type_declaration ~env ~(exported : Exported.t)
{
Expand Down Expand Up @@ -466,16 +482,6 @@ let for_signature ~name ~env sig_items =
let deprecated = Process_attributes.find_deprecated_attribute attributes in
{Module.name; docstring; exported; items; deprecated}

let rec get_module_path mod_desc =
match mod_desc with
| Typedtree.Tmod_ident (path, _lident) -> Some path
| Tmod_structure _ -> None
| Tmod_functor (_ident, _argName, _maybeType, _resultExpr) -> None
| Tmod_apply (functor_, _arg, _coercion) -> get_module_path functor_.mod_desc
| Tmod_unpack (_expr, _moduleType) -> None
| Tmod_constraint (expr, _typ, _constraint, _coercion) ->
get_module_path expr.mod_desc

let rec for_structure_item ~(env : Shared_types.Env.t) ~(exported : Exported.t)
item =
match item.Typedtree.str_desc with
Expand Down Expand Up @@ -758,7 +764,11 @@ and for_structure ~name ~env str_items =
let file_for_cmt_infos ~module_name ~uri
({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) =
let env =
{Env.stamps = Stamps.init (); module_path = File (uri, module_name)}
{
Env.stamps = Stamps.init ();
module_path = File (uri, module_name);
last_label_stamp = ref (-1);
}
in
match cmt_annots with
| Partial_implementation parts ->
Expand Down
132 changes: 92 additions & 40 deletions analysis/src/process_extra.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,107 @@ let add_loc_item extra loc loc_type =
if not loc.Warnings.loc_ghost then
extra.loc_items <- {loc; loc_type} :: extra.loc_items

let add_reference ~extra stamp loc =
Hashtbl.replace extra.internal_references stamp
(loc
::
(if Hashtbl.mem extra.internal_references stamp then
Hashtbl.find extra.internal_references stamp
else []))
(* Ghost locations, such as those of members read from a signature, point
nowhere in the source. *)
let add_reference ~extra stamp (loc : Location.t) =
if not loc.loc_ghost then
Hashtbl.replace extra.internal_references stamp
(loc
::
(if Hashtbl.mem extra.internal_references stamp then
Hashtbl.find extra.internal_references stamp
else []))

type include_origin =
| Origin of int
| Other_file of string * string list (** module name and path *)

(* An `include` copies the members of a module under fresh stamps, with module
path [IncludedModule (source, _)] (for a nested member, under its modules)
and the location of the member they copy. Follow a copy back to that
member. When the path leads into another file, the result is
[Other_file] with the member's path there, without loading that file: if
the member is missing, the copy still is no declaration of this file and
lookups in the other file find nothing. Otherwise the result is the
member's stamp, or the copy itself when the member can't be found. *)
let rec include_origin ~(file : File.t) stamp (tip : Tip.t) =
let source_path (d : _ Declared.t) =
Module_path.included_path d.module_path d.name.txt
in
let source, kind =
match tip with
| Value ->
( Option.bind (Stamps.find_value file.stamps stamp) source_path,
Exported.Value )
| Module ->
( Option.bind (Stamps.find_module file.stamps stamp) source_path,
Exported.Module )
| Type | Field _ | Constructor _ ->
( Option.bind (Stamps.find_type file.stamps stamp) source_path,
Exported.Type )
in
match source with
| Some source -> (
let env = Query_env.from_file file in
match Resolve_path.from_compiler_path ~env source with
| Exported (env, name) -> (
match Exported.find env.exported kind name with
| Some source_stamp when source_stamp <> stamp ->
include_origin ~file source_stamp tip
| Some _ | None -> Origin stamp)
| Global (module_name, path) -> Other_file (module_name, path)
| Stamp _ | GlobalMod _ | NotFound -> Origin stamp)
| None -> Origin stamp

(* A copy whose member is found elsewhere is not a declaration of this file. *)
let is_declaration ~file stamp tip =
include_origin ~file stamp tip = Origin stamp

let extra_for_file ~(file : File.t) =
let extra = init_extra () in
file.stamps
|> Stamps.iter_modules (fun stamp (d : Module.t Declared.t) ->
add_loc_item extra d.name.loc (LModule (Definition (stamp, Module)));
add_reference ~extra stamp d.name.loc);
if is_declaration ~file stamp Module then (
add_loc_item extra d.name.loc (LModule (Definition (stamp, Module)));
add_reference ~extra stamp d.name.loc));
file.stamps
|> Stamps.iter_values (fun stamp (d : Types.type_expr Declared.t) ->
add_loc_item extra d.name.loc
(Typed (d.name.txt, d.item, Definition (stamp, Value)));
add_reference ~extra stamp d.name.loc);
if is_declaration ~file stamp Value then (
add_loc_item extra d.name.loc
(Typed (d.name.txt, d.item, Definition (stamp, Value)));
add_reference ~extra stamp d.name.loc));
file.stamps
|> Stamps.iter_types (fun stamp (d : Type.t Declared.t) ->
add_loc_item extra d.name.loc
(TypeDefinition (d.name.txt, d.item.Type.decl, stamp));
add_reference ~extra stamp d.name.loc;
match d.item.Type.kind with
| Record labels ->
labels
|> List.iter (fun {stamp; fname; typ} ->
add_reference ~extra stamp fname.loc;
add_loc_item extra fname.loc
(Typed (d.name.txt, typ, Definition (d.stamp, Field fname.txt))))
| Variant constructors ->
constructors
|> List.iter (fun {Constructor.stamp; cname} ->
add_reference ~extra stamp cname.loc;
let t =
{
Types.id = 0;
level = 0;
desc =
Tconstr
( Path.Pident {Ident.stamp; name = d.name.txt; flags = 0},
[],
ref Types.Mnil );
}
in
add_loc_item extra cname.loc
(Typed (d.name.txt, t, Definition (d.stamp, Constructor cname.txt))))
| _ -> ());
if is_declaration ~file stamp Type then (
add_loc_item extra d.name.loc
(TypeDefinition (d.name.txt, d.item.Type.decl, stamp));
add_reference ~extra stamp d.name.loc;
match d.item.Type.kind with
| Record labels ->
labels
|> List.iter (fun {stamp; fname; typ} ->
add_reference ~extra stamp fname.loc;
add_loc_item extra fname.loc
(Typed (d.name.txt, typ, Definition (d.stamp, Field fname.txt))))
| Variant constructors ->
constructors
|> List.iter (fun {Constructor.stamp; cname} ->
add_reference ~extra stamp cname.loc;
let t =
{
Types.id = 0;
level = 0;
desc =
Tconstr
( Path.Pident {Ident.stamp; name = d.name.txt; flags = 0},
[],
ref Types.Mnil );
}
in
add_loc_item extra cname.loc
(Typed
(d.name.txt, t, Definition (d.stamp, Constructor cname.txt))))
| _ -> ()));
extra

let add_external_reference ~extra module_name path tip loc =
Expand Down
Loading
Loading