Source file mtyper.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
open Std
open Local_store
let { Logger.log } = Logger.for_section "Mtyper"
type index = Longident.t Location.loc list Shape.Uid.Map.t
let index_items :
(index ->
Mconfig.t ->
[ `Impl of Typedtree.structure_item list
| `Intf of Typedtree.signature_item list ] ->
index)
ref =
ref (fun acc _config _item -> acc)
let set_index_items f = index_items := f
type ('p, 't) item =
{ parsetree_item : 'p;
typedtree_items : 't list * Types.signature_item list;
part_snapshot : Types.snapshot;
part_stamp : int;
part_uid : int;
part_env : Env.t;
part_rev_sg : Types.signature_item list;
part_errors : exn list;
part_checks : Typecore.delayed_check list;
part_warnings : Warnings.state;
part_index : index lazy_t
}
type typedtree =
[ `Interface of Typedtree.signature | `Implementation of Typedtree.structure ]
type typedtree_items =
| Interface_items of
{ items : (Parsetree.signature_item, Typedtree.signature_item) item list;
psig_modalities : Parsetree.modalities;
sig_modalities : Typedtree.modalities;
sig_sloc : Location.t
}
| Implementation_items of
(Parsetree.structure_item, Typedtree.structure_item) item list
type typer_cache_stats = Miss | Hit of { reused : int; typed : int }
type 'a cache_result =
{ env : Env.t;
snapshot : Types.snapshot;
ident_stamp : int;
uid_stamp : int;
value : 'a
}
let cache : typedtree_items option cache_result option ref = s_ref None
let fresh_env config =
let env0 = Typer_raw.fresh_env () in
let env0 = Extension.register Mconfig.(config.merlin.extensions) env0 in
let snap0 = Btype.snapshot () in
let stamp0 = Ident.get_currentstamp () in
let uid0 = Shape.Uid.get_current_stamp () in
(env0, snap0, stamp0, uid0)
let get_cache config =
match !cache with
| Some ({ snapshot; _ } as c) when Types.is_valid snapshot -> c
| Some _ | None ->
let env, snapshot, ident_stamp, uid_stamp = fresh_env config in
{ env; snapshot; ident_stamp; uid_stamp; value = None }
let return_and_cache status =
cache := Some { status with value = Some status.value };
status
type result =
{ config : Mconfig.t;
initial_env : Env.t;
initial_snapshot : Types.snapshot;
initial_stamp : int;
stamp : int;
initial_uid_stamp : int;
typedtree : typedtree_items;
cache_stat : typer_cache_stats
}
let initial_env res = res.initial_env
let get_cache_stat res = res.cache_stat
let compatible_prefix_rev result_items tree_items =
let rec aux acc = function
| ritem :: ritems, pitem :: pitems
when Types.is_valid ritem.part_snapshot
&& compare ritem.parsetree_item pitem = 0 ->
aux (ritem :: acc) (ritems, pitems)
| _, pitems ->
let reused = List.length acc in
let typed = List.length pitems in
let cache_stat = Hit { reused; typed } in
log ~title:"compatible_prefix" "reusing %d items, %d new items to type"
reused typed;
(acc, pitems, cache_stat)
in
aux [] (result_items, tree_items)
let[@tail_mod_cons] rec type_structure config caught env index sg = function
| parsetree_item :: rest ->
let items, sg', part_env =
Typemod.merlin_type_structure env sg [ parsetree_item ]
in
let part_index =
lazy (!index_items (Lazy.force index) config (`Impl items.str_items))
in
let typedtree_items =
(items.Typedtree.str_items, items.Typedtree.str_type)
in
let part_rev_sg = List.rev_append sg' sg in
let item =
{ parsetree_item;
typedtree_items;
part_env;
part_rev_sg;
part_snapshot = Btype.snapshot ();
part_stamp = Ident.get_currentstamp ();
part_uid = Shape.Uid.get_current_stamp ();
part_errors = !caught;
part_checks = !Typecore.delayed_checks;
part_warnings = Warnings.backup ();
part_index
}
in
item :: type_structure config caught part_env part_index part_rev_sg rest
| [] -> []
let[@tail_mod_cons] rec type_signature config caught env index sg psg_modalities
psg_loc = function
| parsetree_item :: rest ->
let { Typedtree.sig_final_env = part_env;
sig_items;
sig_type;
sig_modalities = _;
sig_sloc = _
} =
Typemod.merlin_transl_signature env sg
(Ast_helper.Sg.mk ~loc:psg_loc ~modalities:psg_modalities
[ parsetree_item ])
in
let part_rev_sg = List.rev_append sig_type sg in
let part_index =
lazy (!index_items (Lazy.force index) config (`Intf sig_items))
in
let item =
{ parsetree_item;
typedtree_items = (sig_items, sig_type);
part_env;
part_rev_sg;
part_snapshot = Btype.snapshot ();
part_stamp = Ident.get_currentstamp ();
part_uid = Shape.Uid.get_current_stamp ();
part_errors = !caught;
part_checks = !Typecore.delayed_checks;
part_warnings = Warnings.backup ();
part_index
}
in
item
:: type_signature config caught part_env part_index part_rev_sg
psg_modalities psg_loc rest
| [] -> []
let type_implementation config caught parsetree =
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
get_cache config
in
let rev_prefix, parsetree_suffix, cache_stats =
match cached_value with
| Some (Implementation_items items) -> compatible_prefix_rev items parsetree
| Some (Interface_items _) | None -> ([], parsetree, Miss)
in
let env', sg', snap', stamp', uid_stamp', warn', index' =
match rev_prefix with
| [] ->
( env,
[],
snapshot,
ident_stamp,
uid_stamp,
Warnings.backup (),
lazy Shape.Uid.Map.empty )
| x :: _ ->
caught := x.part_errors;
Typecore.delayed_checks := x.part_checks;
( x.part_env,
x.part_rev_sg,
x.part_snapshot,
x.part_stamp,
x.part_uid,
x.part_warnings,
x.part_index )
in
Btype.backtrack snap';
Warnings.restore warn';
Env.cleanup_functor_caches ~stamp:stamp';
Env.cleanup_usage_tables ~stamp:uid_stamp';
Shape.Uid.restore_stamp uid_stamp';
let suffix = type_structure config caught env' index' sg' parsetree_suffix in
let value = Implementation_items (List.rev_append rev_prefix suffix) in
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
cache_stats )
let type_interface config caught (parsetree : Parsetree.signature) =
let { env; snapshot; ident_stamp; uid_stamp; value = cached_value; _ } =
get_cache config
in
let rev_prefix, parsetree_suffix, cache_stats =
match cached_value with
| Some
(Interface_items
{ items; psig_modalities; sig_modalities = _; sig_sloc = _ })
when List.equal
~eq:(fun
{ Location.txt = Parsetree.Modality a; loc = _ }
{ Location.txt = Parsetree.Modality b; loc = _ }
-> String.equal a b)
psig_modalities parsetree.psg_modalities ->
compatible_prefix_rev items parsetree.psg_items
| Some (Interface_items _) | Some (Implementation_items _) | None ->
([], parsetree.psg_items, Miss)
in
let env', sg', snap', stamp', uid_stamp', warn', index' =
match rev_prefix with
| [] ->
( env,
[],
snapshot,
ident_stamp,
uid_stamp,
Warnings.backup (),
lazy Shape.Uid.Map.empty )
| x :: _ ->
caught := x.part_errors;
Typecore.delayed_checks := x.part_checks;
( x.part_env,
x.part_rev_sg,
x.part_snapshot,
x.part_stamp,
x.part_uid,
x.part_warnings,
x.part_index )
in
Btype.backtrack snap';
Warnings.restore warn';
Env.cleanup_functor_caches ~stamp:stamp';
Env.cleanup_usage_tables ~stamp:uid_stamp';
Shape.Uid.restore_stamp uid_stamp';
let suffix =
type_signature config caught env' index' sg' parsetree.psg_modalities
parsetree.psg_loc parsetree_suffix
in
let ({ sig_final_env = _;
sig_items = _;
sig_type = _;
sig_modalities;
sig_sloc
}
: Typedtree.signature) =
Typemod.merlin_transl_signature Env.empty []
(Ast_helper.Sg.mk ~modalities:parsetree.psg_modalities
~loc:parsetree.psg_loc [])
in
let value =
Interface_items
{ items = List.rev_append rev_prefix suffix;
sig_modalities;
psig_modalities = parsetree.psg_modalities;
sig_sloc
}
in
( return_and_cache { env; snapshot; ident_stamp; uid_stamp; value },
cache_stats )
let run config parsetree =
if not (Env.check_state_consistency ()) then (
let { Load_path.visible; hidden } = Load_path.get_paths () in
let parameters = Env.parameters () in
Mocaml.flush_caches ();
Local_store.reset ();
Load_path.reset ();
Load_path.(init ~auto_include:no_auto_include ~visible ~hidden);
List.iter ~f:Env.register_parameter parameters);
let caught = ref [] in
Msupport.catch_errors Mconfig.(config.ocaml.warnings) caught @@ fun () ->
Typecore.reset_delayed_checks ();
let cached_result, cache_stat =
match parsetree with
| `Implementation parsetree -> type_implementation config caught parsetree
| `Interface parsetree -> type_interface config caught parsetree
in
let stamp = Ident.get_currentstamp () in
Typecore.reset_delayed_checks ();
{ config;
initial_env = cached_result.env;
initial_snapshot = cached_result.snapshot;
initial_stamp = cached_result.ident_stamp;
stamp;
initial_uid_stamp = cached_result.uid_stamp;
typedtree = cached_result.value;
cache_stat
}
let get_env ?pos:_ t =
Option.value ~default:t.initial_env
(match t.typedtree with
| Implementation_items l ->
Option.map ~f:(fun x -> x.part_env) (List.last l)
| Interface_items
{ items = l; sig_modalities = _; psig_modalities = _; sig_sloc = _ } ->
Option.map ~f:(fun x -> x.part_env) (List.last l))
let get_errors t =
let errors, checks =
Option.value ~default:([], [])
(let f x = (x.part_errors, x.part_checks) in
match t.typedtree with
| Implementation_items l -> Option.map ~f (List.last l)
| Interface_items l -> Option.map ~f (List.last l.items))
in
let caught = ref errors in
Typecore.delayed_checks := checks;
Msupport.catch_errors
Mconfig.(t.config.ocaml.warnings)
caught Typecore.force_delayed_checks;
Typecore.reset_delayed_checks ();
!caught
let get_typedtree t =
let split_items l =
let typd, typs = List.split (List.map ~f:(fun x -> x.typedtree_items) l) in
(List.concat typd, List.concat typs)
in
match t.typedtree with
| Implementation_items l ->
let str_items, str_type = split_items l in
`Implementation { Typedtree.str_items; str_type; str_final_env = get_env t }
| Interface_items { items = l; psig_modalities = _; sig_modalities; sig_sloc }
->
let sig_items, sig_type = split_items l in
`Interface
{ Typedtree.sig_items;
sig_type;
sig_final_env = get_env t;
sig_modalities;
sig_sloc
}
let get_index t =
let of_items items =
List.last items
|> Option.value_map ~default:Shape.Uid.Map.empty
~f:(fun { part_index; _ } -> Lazy.force part_index)
in
match t.typedtree with
| Implementation_items items -> of_items items
| Interface_items { items; _ } -> of_items items
let get_stamp t = t.stamp
let node_at ?(skip_recovered = false) ?let_pun_behavior
?record_pattern_pun_behavior t pos_cursor =
let node = Mbrowse.of_typedtree (get_typedtree t) in
log ~title:"node_at" "Node: %s" (Mbrowse.print () node);
let rec select = function
| (_, _) :: ((_, node') :: _ as ancestors) when Mbrowse.is_recovered node'
-> select ancestors
| l -> l
in
match
Mbrowse.deepest_before ?let_pun_behavior ?record_pattern_pun_behavior
pos_cursor [ node ]
with
| [] -> [ (get_env t, Browse_raw.Dummy) ]
| path when skip_recovered -> select path
| path ->
log ~title:"node_at" "Deepest before %s" (Mbrowse.print () path);
path