@@ -3,21 +3,17 @@ open Local_store
33
44let { Logger. log } = Logger. for_section " Mtyper"
55
6- let index_changelog = Local_store. s_table Stamped_hashtable. create_changelog ()
7-
8- type index_tbl =
9- (Shape.Uid .t * Longident .t Location .loc , unit ) Stamped_hashtable .t
6+ type index = Longident .t Location .loc list Shape.Uid.Map .t
107
118(* Forward ref to be filled by analysis.Occurrences *)
129let index_items :
13- (index :index_tbl ->
14- stamp:int ->
10+ (index ->
1511 Mconfig. t ->
1612 [ `Impl of Typedtree. structure_item list
1713 | `Intf of Typedtree. signature_item list ] ->
18- unit )
14+ index )
1915 ref =
20- ref (fun ~ index : _ ~ stamp : _ _config _item -> () )
16+ ref (fun acc _config _item -> acc )
2117let set_index_items f = index_items := f
2218
2319type ('p, 't) item =
@@ -30,7 +26,8 @@ type ('p, 't) item =
3026 part_rev_sg : Types .signature_item list ;
3127 part_errors : exn list ;
3228 part_checks : Typecore .delayed_check list ;
33- part_warnings : Warnings .state
29+ part_warnings : Warnings .state ;
30+ part_index : index
3431 }
3532
3633type typedtree =
@@ -53,8 +50,7 @@ type 'a cache_result =
5350 snapshot : Types .snapshot ;
5451 ident_stamp : int ;
5552 uid_stamp : int ;
56- value : 'a ;
57- index : (Shape.Uid .t * Longident .t Location .loc , unit ) Stamped_hashtable .t
53+ value : 'a
5854 }
5955
6056let cache : typedtree_items option cache_result option ref = s_ref None
@@ -72,8 +68,7 @@ let get_cache config =
7268 | Some ({ snapshot; _ } as c ) when Types. is_valid snapshot -> c
7369 | Some _ | None ->
7470 let env, snapshot, ident_stamp, uid_stamp = fresh_env config in
75- let index = Stamped_hashtable. create ! index_changelog 256 in
76- { env; snapshot; ident_stamp; uid_stamp; value = None ; index }
71+ { env; snapshot; ident_stamp; uid_stamp; value = None }
7772
7873let return_and_cache status =
7974 cache := Some { status with value = Some status.value };
@@ -87,7 +82,6 @@ type result =
8782 stamp : int ;
8883 initial_uid_stamp : int ;
8984 typedtree : typedtree_items ;
90- index : (Shape.Uid .t * Longident .t Location .loc , unit ) Stamped_hashtable .t ;
9185 cache_stat : typer_cache_stats
9286 }
9387
@@ -111,11 +105,12 @@ let compatible_prefix_rev result_items tree_items =
111105 in
112106 aux [] (result_items, tree_items)
113107
114- let [@ tail_mod_cons] rec type_structure caught env sg = function
108+ let [@ tail_mod_cons] rec type_structure config caught env index sg = function
115109 | parsetree_item :: rest ->
116110 let items, sg', part_env =
117111 Typemod. merlin_type_structure env sg [ parsetree_item ]
118112 in
113+ let part_index = ! index_items index config (`Impl items.str_items) in
119114 let typedtree_items =
120115 (items.Typedtree. str_items, items.Typedtree. str_type)
121116 in
@@ -130,14 +125,15 @@ let[@tail_mod_cons] rec type_structure caught env sg = function
130125 part_uid = Shape.Uid. get_current_stamp () ;
131126 part_errors = ! caught;
132127 part_checks = ! Typecore. delayed_checks;
133- part_warnings = Warnings. backup ()
128+ part_warnings = Warnings. backup () ;
129+ part_index
134130 }
135131 in
136- item :: type_structure caught part_env part_rev_sg rest
132+ item :: type_structure config caught part_env part_index part_rev_sg rest
137133 | [] -> []
138134
139- let [@ tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
140- function
135+ let [@ tail_mod_cons] rec type_signature config caught env index sg psg_modalities
136+ psg_loc = function
141137 | parsetree_item :: rest ->
142138 let { Typedtree. sig_final_env = part_env;
143139 sig_items;
@@ -150,6 +146,7 @@ let[@tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
150146 [ parsetree_item ])
151147 in
152148 let part_rev_sg = List. rev_append sig_type sg in
149+ let part_index = ! index_items index config (`Intf sig_items) in
153150 let item =
154151 { parsetree_item;
155152 typedtree_items = (sig_items, sig_type);
@@ -160,26 +157,34 @@ let[@tail_mod_cons] rec type_signature caught env sg psg_modalities psg_loc =
160157 part_uid = Shape.Uid. get_current_stamp () ;
161158 part_errors = ! caught;
162159 part_checks = ! Typecore. delayed_checks;
163- part_warnings = Warnings. backup ()
160+ part_warnings = Warnings. backup () ;
161+ part_index
164162 }
165163 in
166164 item
167- :: type_signature caught part_env part_rev_sg psg_modalities psg_loc rest
165+ :: type_signature config caught part_env part_index part_rev_sg
166+ psg_modalities psg_loc rest
168167 | [] -> []
169168
170169let type_implementation config caught parsetree =
171- let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; index; _ }
172- =
170+ let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
173171 get_cache config
174172 in
175173 let rev_prefix, parsetree_suffix, cache_stats =
176174 match cached_value with
177175 | Some (Implementation_items items ) -> compatible_prefix_rev items parsetree
178176 | Some (Interface_items _ ) | None -> ([] , parsetree, Miss )
179177 in
180- let env', sg', snap', stamp', uid_stamp', warn' =
178+ let env', sg', snap', stamp', uid_stamp', warn', index' =
181179 match rev_prefix with
182- | [] -> (env, [] , snapshot, ident_stamp, uid_stamp, Warnings. backup () )
180+ | [] ->
181+ ( env,
182+ [] ,
183+ snapshot,
184+ ident_stamp,
185+ uid_stamp,
186+ Warnings. backup () ,
187+ Shape.Uid.Map. empty )
183188 | x :: _ ->
184189 caught := x.part_errors;
185190 Typecore. delayed_checks := x.part_checks;
@@ -188,30 +193,21 @@ let type_implementation config caught parsetree =
188193 x.part_snapshot,
189194 x.part_stamp,
190195 x.part_uid,
191- x.part_warnings )
196+ x.part_warnings,
197+ x.part_index )
192198 in
193199 Btype. backtrack snap';
194200 Warnings. restore warn';
195201 Env. cleanup_functor_caches ~stamp: stamp';
196- let stamp = List. length rev_prefix - 1 in
197- Stamped_hashtable. backtrack ! index_changelog ~stamp ;
198202 Env. cleanup_usage_tables ~stamp: uid_stamp';
199203 Shape.Uid. restore_stamp uid_stamp';
200- let suffix = type_structure caught env' sg' parsetree_suffix in
201- let () =
202- List. iteri
203- ~f: (fun i { typedtree_items = items , _ ; _ } ->
204- let stamp = stamp + i + 1 in
205- ! index_items ~index ~stamp config (`Impl items))
206- suffix
207- in
204+ let suffix = type_structure config caught env' index' sg' parsetree_suffix in
208205 let value = Implementation_items (List. rev_append rev_prefix suffix) in
209- ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
206+ ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
210207 cache_stats )
211208
212209let type_interface config caught (parsetree : Parsetree.signature ) =
213- let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; index; _ }
214- =
210+ let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
215211 get_cache config
216212 in
217213 let rev_prefix, parsetree_suffix, cache_stats =
@@ -230,9 +226,16 @@ let type_interface config caught (parsetree : Parsetree.signature) =
230226 | Some (Interface_items _ ) | Some (Implementation_items _ ) | None ->
231227 ([] , parsetree.psg_items, Miss )
232228 in
233- let env', sg', snap', stamp', uid_stamp', warn' =
229+ let env', sg', snap', stamp', uid_stamp', warn', index' =
234230 match rev_prefix with
235- | [] -> (env, [] , snapshot, ident_stamp, uid_stamp, Warnings. backup () )
231+ | [] ->
232+ ( env,
233+ [] ,
234+ snapshot,
235+ ident_stamp,
236+ uid_stamp,
237+ Warnings. backup () ,
238+ Shape.Uid.Map. empty )
236239 | x :: _ ->
237240 caught := x.part_errors;
238241 Typecore. delayed_checks := x.part_checks;
@@ -241,25 +244,17 @@ let type_interface config caught (parsetree : Parsetree.signature) =
241244 x.part_snapshot,
242245 x.part_stamp,
243246 x.part_uid,
244- x.part_warnings )
247+ x.part_warnings,
248+ x.part_index )
245249 in
246250 Btype. backtrack snap';
247251 Warnings. restore warn';
248252 Env. cleanup_functor_caches ~stamp: stamp';
249- let stamp = List. length rev_prefix in
250- Stamped_hashtable. backtrack ! index_changelog ~stamp ;
251253 Env. cleanup_usage_tables ~stamp: uid_stamp';
252254 Shape.Uid. restore_stamp uid_stamp';
253255 let suffix =
254- type_signature caught env' sg' parsetree.psg_modalities parsetree.psg_loc
255- parsetree_suffix
256- in
257- let () =
258- List. iteri
259- ~f: (fun i { typedtree_items = items , _ ; _ } ->
260- let stamp = stamp + i + 1 in
261- ! index_items ~index ~stamp config (`Intf items))
262- suffix
256+ type_signature config caught env' index' sg' parsetree.psg_modalities
257+ parsetree.psg_loc parsetree_suffix
263258 in
264259 (* transl an empty signature to get the sig_modalities and sig_sloc *)
265260 let ({ sig_final_env = _;
@@ -281,7 +276,7 @@ let type_interface config caught (parsetree : Parsetree.signature) =
281276 sig_sloc
282277 }
283278 in
284- ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
279+ ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
285280 cache_stats )
286281
287282let run config parsetree =
@@ -313,7 +308,6 @@ let run config parsetree =
313308 stamp;
314309 initial_uid_stamp = cached_result.uid_stamp;
315310 typedtree = cached_result.value;
316- index = cached_result.index;
317311 cache_stat
318312 }
319313
@@ -362,7 +356,15 @@ let get_typedtree t =
362356 sig_sloc
363357 }
364358
365- let get_index t = t.index
359+ let get_index t =
360+ let of_items items =
361+ List. last items
362+ |> Option. value_map ~default: Shape.Uid.Map. empty
363+ ~f: (fun { part_index; _ } -> part_index)
364+ in
365+ match t.typedtree with
366+ | Implementation_items items -> of_items items
367+ | Interface_items { items; _ } -> of_items items
366368
367369let get_stamp t = t.stamp
368370
0 commit comments