@@ -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 =
@@ -29,7 +25,8 @@ type ('p, 't) item =
2925 part_env : Env .t ;
3026 part_errors : exn list ;
3127 part_checks : Typecore .delayed_check list ;
32- part_warnings : Warnings .state
28+ part_warnings : Warnings .state ;
29+ part_index : index lazy_t
3330 }
3431
3532type typedtree =
@@ -47,8 +44,7 @@ type 'a cache_result =
4744 snapshot : Types .snapshot ;
4845 ident_stamp : int ;
4946 uid_stamp : int ;
50- value : 'a ;
51- index : (Shape.Uid .t * Longident .t Location .loc , unit ) Stamped_hashtable .t
47+ value : 'a
5248 }
5349
5450let cache : typedtree_items option cache_result option ref = s_ref None
@@ -66,8 +62,7 @@ let get_cache config =
6662 | Some ({ snapshot; _ } as c ) when Types. is_valid snapshot -> c
6763 | Some _ | None ->
6864 let env, snapshot, ident_stamp, uid_stamp = fresh_env config in
69- let index = Stamped_hashtable. create ! index_changelog 256 in
70- { env; snapshot; ident_stamp; uid_stamp; value = None ; index }
65+ { env; snapshot; ident_stamp; uid_stamp; value = None }
7166
7267let return_and_cache status =
7368 cache := Some { status with value = Some status.value };
@@ -81,7 +76,6 @@ type result =
8176 stamp : int ;
8277 initial_uid_stamp : int ;
8378 typedtree : typedtree_items ;
84- index : (Shape.Uid .t * Longident .t Location .loc , unit ) Stamped_hashtable .t ;
8579 cache_stat : typer_cache_stats
8680 }
8781
@@ -105,11 +99,14 @@ let compatible_prefix result_items tree_items =
10599 in
106100 aux [] (result_items, tree_items)
107101
108- let rec type_structure caught env = function
102+ let rec type_structure config caught env index = function
109103 | parsetree_item :: rest ->
110104 let items, _, part_env =
111105 Typemod. merlin_type_structure env [ parsetree_item ]
112106 in
107+ let part_index =
108+ lazy (! index_items (Lazy. force index) config (`Impl items.str_items))
109+ in
113110 let typedtree_items =
114111 (items.Typedtree. str_items, items.Typedtree. str_type)
115112 in
@@ -122,17 +119,21 @@ let rec type_structure caught env = function
122119 part_uid = Shape.Uid. get_current_stamp () ;
123120 part_errors = ! caught;
124121 part_checks = ! Typecore. delayed_checks;
125- part_warnings = Warnings. backup ()
122+ part_warnings = Warnings. backup () ;
123+ part_index
126124 }
127125 in
128- item :: type_structure caught part_env rest
126+ item :: type_structure config caught part_env part_index rest
129127 | [] -> []
130128
131- let rec type_signature caught env = function
129+ let rec type_signature config caught env index = function
132130 | parsetree_item :: rest ->
133131 let { Typedtree. sig_final_env = part_env; sig_items; sig_type } =
134132 Typemod. merlin_transl_signature env [ parsetree_item ]
135133 in
134+ let part_index =
135+ lazy (! index_items (Lazy. force index) config (`Intf sig_items))
136+ in
136137 let item =
137138 { parsetree_item;
138139 typedtree_items = (sig_items, sig_type);
@@ -142,82 +143,87 @@ let rec type_signature caught env = function
142143 part_uid = Shape.Uid. get_current_stamp () ;
143144 part_errors = ! caught;
144145 part_checks = ! Typecore. delayed_checks;
145- part_warnings = Warnings. backup ()
146+ part_warnings = Warnings. backup () ;
147+ part_index
146148 }
147149 in
148- item :: type_signature caught part_env rest
150+ item :: type_signature config caught part_env part_index rest
149151 | [] -> []
150152
151153let type_implementation config caught parsetree =
152- let { env; snapshot; ident_stamp; uid_stamp; value = prefix; index; _ } =
154+ let { env; snapshot; ident_stamp; uid_stamp; value = prefix; _ } =
153155 get_cache config
154156 in
155157 let prefix, parsetree, cache_stats =
156158 match prefix with
157159 | Some (`Implementation items ) -> compatible_prefix items parsetree
158160 | Some (`Interface _ ) | None -> ([] , parsetree, Miss )
159161 in
160- let env', snap', stamp', uid_stamp', warn' =
162+ let env', snap', stamp', uid_stamp', warn', index' =
161163 match prefix with
162- | [] -> (env, snapshot, ident_stamp, uid_stamp, Warnings. backup () )
164+ | [] ->
165+ ( env,
166+ snapshot,
167+ ident_stamp,
168+ uid_stamp,
169+ Warnings. backup () ,
170+ lazy Shape.Uid.Map. empty )
163171 | x :: _ ->
164172 caught := x.part_errors;
165173 Typecore. delayed_checks := x.part_checks;
166- (x.part_env, x.part_snapshot, x.part_stamp, x.part_uid, x.part_warnings)
174+ ( x.part_env,
175+ x.part_snapshot,
176+ x.part_stamp,
177+ x.part_uid,
178+ x.part_warnings,
179+ x.part_index )
167180 in
168181 Btype. backtrack snap';
169182 Warnings. restore warn';
170183 Env. cleanup_functor_caches ~stamp: stamp';
171- let stamp = List. length prefix - 1 in
172- Stamped_hashtable. backtrack ! index_changelog ~stamp ;
173184 Env. cleanup_usage_tables ~stamp: uid_stamp';
174185 Shape.Uid. restore_stamp uid_stamp';
175- let suffix = type_structure caught env' parsetree in
176- let () =
177- List. iteri
178- ~f: (fun i { typedtree_items = items , _ ; _ } ->
179- let stamp = stamp + i + 1 in
180- ! index_items ~index ~stamp config (`Impl items))
181- suffix
182- in
186+ let suffix = type_structure config caught env' index' parsetree in
183187 let value = `Implementation (List. rev_append prefix suffix) in
184- ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
188+ ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
185189 cache_stats )
186190
187191let type_interface config caught parsetree =
188- let { env; snapshot; ident_stamp; uid_stamp; value = prefix; index; _ } =
192+ let { env; snapshot; ident_stamp; uid_stamp; value = prefix; _ } =
189193 get_cache config
190194 in
191195 let prefix, parsetree, cache_stats =
192196 match prefix with
193197 | Some (`Interface items ) -> compatible_prefix items parsetree
194198 | Some (`Implementation _ ) | None -> ([] , parsetree, Miss )
195199 in
196- let env', snap', stamp', uid_stamp', warn' =
200+ let env', snap', stamp', uid_stamp', warn', index' =
197201 match prefix with
198- | [] -> (env, snapshot, ident_stamp, uid_stamp, Warnings. backup () )
202+ | [] ->
203+ ( env,
204+ snapshot,
205+ ident_stamp,
206+ uid_stamp,
207+ Warnings. backup () ,
208+ lazy Shape.Uid.Map. empty )
199209 | x :: _ ->
200210 caught := x.part_errors;
201211 Typecore. delayed_checks := x.part_checks;
202- (x.part_env, x.part_snapshot, x.part_stamp, x.part_uid, x.part_warnings)
212+ ( x.part_env,
213+ x.part_snapshot,
214+ x.part_stamp,
215+ x.part_uid,
216+ x.part_warnings,
217+ x.part_index )
203218 in
204219 Btype. backtrack snap';
205220 Warnings. restore warn';
206221 Env. cleanup_functor_caches ~stamp: stamp';
207- let stamp = List. length prefix in
208- Stamped_hashtable. backtrack ! index_changelog ~stamp ;
209222 Env. cleanup_usage_tables ~stamp: uid_stamp';
210223 Shape.Uid. restore_stamp uid_stamp';
211- let suffix = type_signature caught env' parsetree in
212- let () =
213- List. iteri
214- ~f: (fun i { typedtree_items = items , _ ; _ } ->
215- let stamp = stamp + i + 1 in
216- ! index_items ~index ~stamp config (`Intf items))
217- suffix
218- in
224+ let suffix = type_signature config caught env' index' parsetree in
219225 let value = `Interface (List. rev_append prefix suffix) in
220- ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value; index },
226+ ( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
221227 cache_stats )
222228
223229let run config parsetree =
@@ -246,7 +252,6 @@ let run config parsetree =
246252 stamp;
247253 initial_uid_stamp = cached_result.uid_stamp;
248254 typedtree = cached_result.value;
249- index = cached_result.index;
250255 cache_stat
251256 }
252257
@@ -285,7 +290,15 @@ let get_typedtree t =
285290 let sig_items, sig_type = split_items l in
286291 `Interface { Typedtree. sig_items; sig_type; sig_final_env = get_env t }
287292
288- let get_index t = t.index
293+ let get_index t =
294+ let of_items items =
295+ List. last items
296+ |> Option. value_map ~default: Shape.Uid.Map. empty
297+ ~f: (fun { part_index; _ } -> Lazy. force part_index)
298+ in
299+ match t.typedtree with
300+ | `Implementation items -> of_items items
301+ | `Interface items -> of_items items
289302
290303let get_stamp t = t.stamp
291304
0 commit comments