Skip to content

Commit 14dd477

Browse files
committed
refactor away functor
1 parent d3c1cd4 commit 14dd477

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

analysis/src/ProcessCmt.ml

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,15 @@ let fromCompilerPath ~(env : QueryEnv.t) path =
626626
| Some (`Local (env, name)) -> `Exported (env, name)
627627
| Some (`Global (moduleName, fullPath)) -> `Global (moduleName, fullPath))
628628

629-
module F (Collector : sig
630-
val extra : extra
631-
val file : File.t
632-
end) =
633-
struct
634-
let extra = Collector.extra
635-
629+
let getIterator (extra : extra) (file : File.t) =
636630
let addReference stamp loc =
637631
Hashtbl.replace extra.internalReferences stamp
638632
(loc
639633
::
640634
(if Hashtbl.mem extra.internalReferences stamp then
641635
Hashtbl.find extra.internalReferences stamp
642636
else []))
637+
in
643638

644639
let addExternalReference moduleName path tip loc =
645640
(* TODO need to follow the path, and be able to load the files to follow module references... *)
@@ -649,6 +644,7 @@ struct
649644
(if Hashtbl.mem extra.externalReferences moduleName then
650645
Hashtbl.find extra.externalReferences moduleName
651646
else []))
647+
in
652648

653649
let addFileReference moduleName loc =
654650
let newLocs =
@@ -657,8 +653,9 @@ struct
657653
| None -> LocationSet.singleton loc
658654
in
659655
Hashtbl.replace extra.fileReferences moduleName newLocs
656+
in
660657

661-
let env = QueryEnv.fromFile Collector.file
658+
let env = QueryEnv.fromFile file in
662659

663660
let addForPath path lident loc typ tip =
664661
let identName = Longident.last lident in
@@ -685,6 +682,7 @@ struct
685682
| `GlobalMod _ -> NotFound
686683
in
687684
addLocItem extra loc (Typed (identName, typ, locType))
685+
in
688686

689687
let addForPathParent path loc =
690688
let locType =
@@ -707,6 +705,7 @@ struct
707705
| None -> LModule NotFound)
708706
in
709707
addLocItem extra loc locType
708+
in
710709

711710
let getTypeAtPath ~env path =
712711
match fromCompilerPath ~env path with
@@ -726,12 +725,14 @@ struct
726725
match declaredType with
727726
| Some declaredType -> `Local declaredType
728727
| None -> `Not_found)
728+
in
729729

730730
let handleConstructor txt =
731731
match txt with
732732
| Longident.Lident name -> name
733733
| Ldot (_left, name) -> name
734734
| Lapply (_, _) -> assert false
735+
in
735736

736737
let addForField recordType fieldType {Asttypes.txt; loc} =
737738
match (Shared.dig recordType).desc with
@@ -754,6 +755,7 @@ struct
754755
in
755756
addLocItem extra nameLoc (Typed (name, fieldType, locType))
756757
| _ -> ()
758+
in
757759

758760
let addForRecord recordType items =
759761
match (Shared.dig recordType).desc with
@@ -781,6 +783,7 @@ struct
781783
in
782784
addLocItem extra nameLoc (Typed (name, lbl_res, locType)))
783785
| _ -> ()
786+
in
784787

785788
let addForConstructor constructorType {Asttypes.txt; loc} {Types.cstr_name} =
786789
match (Shared.dig constructorType).desc with
@@ -806,12 +809,14 @@ struct
806809
in
807810
addLocItem extra nameLoc (Typed (name, constructorType, locType))
808811
| _ -> ()
812+
in
809813

810814
let rec lidIsComplex (lid : Longident.t) =
811815
match lid with
812816
| Lapply _ -> true
813817
| Ldot (lid, _) -> lidIsComplex lid
814818
| _ -> false
819+
in
815820

816821
let rec addForLongident top (path : Path.t) (txt : Longident.t) loc =
817822
if (not loc.Location.loc_ghost) && not (lidIsComplex txt) then (
@@ -835,6 +840,7 @@ struct
835840
(Utils.chopLocationEnd loc (String.length name + 1))
836841
| Pident _, Lident _ -> ()
837842
| _ -> ())
843+
in
838844

839845
let rec handle_module_expr expr =
840846
match expr with
@@ -849,6 +855,7 @@ struct
849855
handle_module_expr obj.mod_desc;
850856
handle_module_expr arg.mod_desc
851857
| _ -> ()
858+
in
852859

853860
let enter_structure_item item =
854861
match item.str_desc with
@@ -857,38 +864,41 @@ struct
857864
| Tstr_open {open_path; open_txt = {txt; loc}} ->
858865
(* Log.log("Have an open here"); *)
859866
addForLongident None open_path txt loc;
860-
Hashtbl.replace Collector.extra.opens loc ()
867+
Hashtbl.replace extra.opens loc ()
861868
| _ -> ()
869+
in
862870

863871
let enter_signature_item item =
864872
match item.sig_desc with
865873
| Tsig_value {val_id; val_loc; val_name = name; val_desc; val_attributes} ->
866874
let stamp = Ident.binding_time val_id in
867-
if Stamps.findValue Collector.file.stamps stamp = None then (
875+
if Stamps.findValue file.stamps stamp = None then (
868876
let declared =
869877
ProcessAttributes.newDeclared ~name ~stamp ~extent:val_loc
870878
~modulePath:NotVisible ~item:val_desc.ctyp_type false val_attributes
871879
in
872-
Stamps.addValue Collector.file.stamps stamp declared;
880+
Stamps.addValue file.stamps stamp declared;
873881
addReference stamp name.loc;
874882
addLocItem extra name.loc
875883
(Typed (name.txt, val_desc.ctyp_type, Definition (stamp, Value))))
876884
| _ -> ()
885+
in
877886

878887
let enter_core_type {ctyp_type; ctyp_desc} =
879888
match ctyp_desc with
880889
| Ttyp_constr (path, {txt; loc}, _args) ->
881890
addForLongident (Some (ctyp_type, Type)) path txt loc
882891
| _ -> ()
892+
in
883893

884894
let enter_pattern {pat_desc; pat_loc; pat_type; pat_attributes} =
885895
let addForPattern stamp name =
886-
if Stamps.findValue Collector.file.stamps stamp = None then (
896+
if Stamps.findValue file.stamps stamp = None then (
887897
let declared =
888898
ProcessAttributes.newDeclared ~name ~stamp ~modulePath:NotVisible
889899
~extent:pat_loc ~item:pat_type false pat_attributes
890900
in
891-
Stamps.addValue Collector.file.stamps stamp declared;
901+
Stamps.addValue file.stamps stamp declared;
892902
addReference stamp name.loc;
893903
addLocItem extra name.loc
894904
(Typed (name.txt, pat_type, Definition (stamp, Value))))
@@ -906,6 +916,7 @@ struct
906916
let stamp = Ident.binding_time ident in
907917
addForPattern stamp name
908918
| _ -> ()
919+
in
909920

910921
let enter_expression expression =
911922
expression.exp_extra
@@ -934,47 +945,46 @@ struct
934945
| Texp_field (inner, lident, _label_description) ->
935946
addForField inner.exp_type expression.exp_type lident
936947
| _ -> ()
948+
in
937949

938950
let structure_item (iter : Tast_iterator.iterator) item =
939951
enter_structure_item item;
940952
Tast_iterator.default_iterator.structure_item iter item
953+
in
941954

942955
let signature_item (iter : Tast_iterator.iterator) item =
943956
enter_signature_item item;
944957
Tast_iterator.default_iterator.signature_item iter item
958+
in
945959

946960
let typ (iter : Tast_iterator.iterator) item =
947961
enter_core_type item;
948962
Tast_iterator.default_iterator.typ iter item
963+
in
949964

950965
let pat (iter : Tast_iterator.iterator) item =
951966
enter_pattern item;
952967
Tast_iterator.default_iterator.pat iter item
968+
in
953969

954970
let expr (iter : Tast_iterator.iterator) item =
955971
enter_expression item;
956972
Tast_iterator.default_iterator.expr iter item
973+
in
957974

958-
let iterator =
959-
{
960-
Tast_iterator.default_iterator with
961-
expr;
962-
pat;
963-
signature_item;
964-
structure_item;
965-
typ;
966-
}
967-
end
975+
{
976+
Tast_iterator.default_iterator with
977+
expr;
978+
pat;
979+
signature_item;
980+
structure_item;
981+
typ;
982+
}
968983

969984
let extraForStructureItems ~(file : File.t)
970985
(items : Typedtree.structure_item list) parts =
971986
let extra = extraForFile ~file in
972-
(* TODO look through parts and extend the extent *)
973-
let module FM = F (struct
974-
let extra = extra
975-
let file = file
976-
end) in
977-
let iterator = FM.iterator in
987+
let iterator = getIterator extra file in
978988
items |> List.iter (iterator.structure_item iterator);
979989

980990
(* Log.log("Parts " ++ string_of_int(Array.length(parts))); *)
@@ -994,12 +1004,7 @@ let extraForStructureItems ~(file : File.t)
9941004
let extraForSignatureItems ~(file : File.t)
9951005
(items : Typedtree.signature_item list) parts =
9961006
let extra = extraForFile ~file in
997-
998-
let module FM = F (struct
999-
let extra = extra
1000-
let file = file
1001-
end) in
1002-
let iterator = FM.iterator in
1007+
let iterator = getIterator extra file in
10031008
items |> List.iter (iterator.signature_item iterator);
10041009
(* Log.log("Parts " ++ string_of_int(Array.length(parts))); *)
10051010
parts

0 commit comments

Comments
 (0)