Source file ident.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
open Local_store
let lowest_scope = 0
let highest_scope = 100_000_000
let currentstamp = s_ref 0
let predefstamp = s_ref 0
module Unscoped = struct
type desc = { name: string; stamp: int }
type state =
| Udesc of desc
| Ulink of t
and t = { mutable state : state }
let create s =
incr currentstamp;
{ state = Udesc { name = s; stamp = !currentstamp } }
let rec get_desc us =
match us.state with
Udesc d -> d
| Ulink u -> get_desc u
let name us = (get_desc us).name
let refresh us = create (get_desc us).name
let equal us1 us2 = (get_desc us1).name = (get_desc us2).name
let stamp us = (get_desc us).stamp
let same us1 us2 = stamp us1 = stamp us2
type change = t * state
let change_log = ref (fun _ -> assert false)
let undo_change (us, us_d) =
us.state <- us_d
let rec repr us =
match us.state with
| Ulink us' ->
repr us'
| Udesc _ -> us
let link us1 us2 =
let us1 = repr us1 in
let us2 = repr us2 in
if us1 == us2 then () else begin
!change_log (us1, us1.state);
us1.state <- Ulink us2
end
let equiv id_pairs us1 us2 =
let s1 = stamp us1 in
let s2 = stamp us2 in
s1 = s2
|| List.exists (fun (i1, i2) -> (stamp i1 = s1 && stamp i2 = s2)
|| stamp i2 = s1 && stamp i1 = s2) id_pairs
module Ops = struct
type nonrec t = t
let compare us1 us2 = stamp us1 - stamp us2
end
module Set = Stdlib.Set.Make(Ops)
end
type t =
| Local of { name: string; stamp: int }
| Scoped of { name: string; stamp: int; scope: int }
| Global of string
| Predef of { name: string; stamp: int }
| Unscoped of Unscoped.t
let create_scoped ~scope s =
incr currentstamp;
Scoped { name = s; stamp = !currentstamp; scope }
let create_local s =
incr currentstamp;
Local { name = s; stamp = !currentstamp }
let of_unscoped u =
Unscoped u
let find_unscoped = function
Unscoped u -> Some u
| _ -> None
let create_predef s =
incr predefstamp;
Predef { name = s; stamp = !predefstamp }
let create_persistent s =
Global s
let name = function
| Local { name; _ }
| Scoped { name; _ }
| Global name
| Predef { name; _ } -> name
| Unscoped us -> Unscoped.name us
let rename = function
| Local { name; stamp = _ }
| Scoped { name; stamp = _; scope = _ } ->
incr currentstamp;
Local { name; stamp = !currentstamp }
| Unscoped us -> Unscoped (Unscoped.refresh us)
| id ->
Misc.fatal_errorf "Ident.rename %s" (name id)
let unique_name = function
| Local { name; stamp }
| Scoped { name; stamp } -> name ^ "_" ^ Int.to_string stamp
| Unscoped us ->
let Unscoped.{ name; stamp } = Unscoped.get_desc us in
name ^ "_" ^ Int.to_string stamp
| Global name ->
name ^ "_0"
| Predef { name; _ } ->
name
let unique_toplevel_name = function
| Local { name; stamp }
| Scoped { name; stamp } -> name ^ "/" ^ Int.to_string stamp
| Unscoped us ->
let Unscoped.{ name; stamp } = Unscoped.get_desc us in
name ^ "/" ^ Int.to_string stamp
| Global name
| Predef { name; _ } -> name
let persistent = function
| Global _ -> true
| _ -> false
let equal i1 i2 =
match i1, i2 with
| Local { name = name1; _ }, Local { name = name2; _ }
| Scoped { name = name1; _ }, Scoped { name = name2; _ }
| Global name1, Global name2 ->
name1 = name2
| Unscoped us1, Unscoped us2 -> Unscoped.equal us1 us2
| Predef { stamp = s1; _ }, Predef { stamp = s2 } ->
s1 = s2
| _ ->
false
let same i1 i2 =
match i1, i2 with
| Local { stamp = s1; _ }, Local { stamp = s2; _ }
| Scoped { stamp = s1; _ }, Scoped { stamp = s2; _ }
| Predef { stamp = s1; _ }, Predef { stamp = s2 } ->
s1 = s2
| Unscoped us1, Unscoped us2 -> Unscoped.same us1 us2
| Global name1, Global name2 ->
name1 = name2
| _ ->
false
let stamp = function
| Local { stamp; _ }
| Scoped { stamp; _ } -> stamp
| Unscoped us -> Unscoped.stamp us
| _ -> 0
let equiv id_pairs i1 i2 =
match i1, i2 with
| Local { stamp = s1; _ }, Local { stamp = s2; _ }
| Scoped { stamp = s1; _ }, Scoped { stamp = s2; _ }
| Predef { stamp = s1; _ }, Predef { stamp = s2 } ->
s1 = s2
| Unscoped us1, Unscoped us2 -> Unscoped.equiv id_pairs us1 us2
| Global name1, Global name2 ->
name1 = name2
| _ ->
false
let compare_stamp id1 id2 =
compare (stamp id1) (stamp id2)
let scope = function
| Scoped { scope; _ } -> scope
| Local _ -> highest_scope
| Global _ | Predef _ -> lowest_scope
| Unscoped _ -> lowest_scope
let reinit_level = ref (-1)
let reinit () =
if !reinit_level < 0
then reinit_level := !currentstamp
else currentstamp := !reinit_level
let global = function
| Local _
| Unscoped _
| Scoped _ -> false
| Global _
| Predef _ -> true
let is_predef = function
| Predef _ -> true
| _ -> false
let is_unscoped = function
| Unscoped _ -> true
| _ -> false
let canonical_stamps = s_table Hashtbl.create 0
let next_canonical_stamp = s_table Hashtbl.create 0
let canonicalize name stamp =
try Hashtbl.find !canonical_stamps (name, stamp)
with Not_found ->
let canonical_stamp =
try Hashtbl.find !next_canonical_stamp name
with Not_found -> 0
in
Hashtbl.replace !next_canonical_stamp name
(canonical_stamp + 1);
Hashtbl.add !canonical_stamps (name, stamp)
canonical_stamp;
canonical_stamp
let pp_stamped ppf (name, stamp) =
let open Format_doc in
if not !Clflags.unique_ids then
fprintf ppf "%s" name
else begin
let stamp =
if not !Clflags.canonical_ids then stamp
else canonicalize name stamp
in
fprintf ppf "%s/%i" name stamp
end
let print ~with_scope ppf =
let open Format_doc in
function
| Global name ->
fprintf ppf "%s!" name
| Predef { name; stamp } ->
fprintf ppf "%a!"
pp_stamped (name, stamp)
| Local { name; stamp } ->
fprintf ppf "%a"
pp_stamped (name, stamp)
| Unscoped us ->
let Unscoped.{ name; stamp } = Unscoped.get_desc us in
fprintf ppf "U%a"
pp_stamped (name, stamp)
| Scoped { name; stamp; scope } ->
fprintf ppf "%a%s"
pp_stamped (name, stamp)
(if with_scope then asprintf "[%i]" scope else "")
let print_with_scope ppf id = print ~with_scope:true ppf id
let doc_print ppf id = print ~with_scope:false ppf id
let print ppf id = Format_doc.compat doc_print ppf id
type 'a tbl =
Empty
| Node of 'a tbl * 'a data * 'a tbl * int
and 'a data =
{ ident: t;
name : string;
stamp : int;
data: 'a;
previous: 'a data option }
let empty = Empty
let get_data k =
assert (stamp k.ident = k.stamp);
k.data
let mknode l d r =
let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h
and hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
Node(l, d, r, (if hl >= hr then hl + 1 else hr + 1))
let balance l d r =
let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h
and hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
if hl > hr + 1 then
match l with
| Node (ll, ld, lr, _)
when (match ll with Empty -> 0 | Node(_,_,_,h) -> h) >=
(match lr with Empty -> 0 | Node(_,_,_,h) -> h) ->
mknode ll ld (mknode lr d r)
| Node (ll, ld, Node(lrl, lrd, lrr, _), _) ->
mknode (mknode ll ld lrl) lrd (mknode lrr d r)
| _ -> assert false
else if hr > hl + 1 then
match r with
| Node (rl, rd, rr, _)
when (match rr with Empty -> 0 | Node(_,_,_,h) -> h) >=
(match rl with Empty -> 0 | Node(_,_,_,h) -> h) ->
mknode (mknode l d rl) rd rr
| Node (Node (rll, rld, rlr, _), rd, rr, _) ->
mknode (mknode l d rll) rld (mknode rlr rd rr)
| _ -> assert false
else
mknode l d r
let rec add id data = function
Empty ->
let k = {
ident = id;
name = name id;
stamp = stamp id;
data = data;
previous = None
} in Node(Empty, k, Empty, 1)
| Node(l, k, r, h) ->
let c = String.compare (name id) k.name in
if c = 0 then
let new_k = {
ident = id;
name = name id;
stamp = stamp id;
data = data;
previous = Some k
} in Node(l, new_k, r, h)
else if c < 0 then
balance (add id data l) k r
else
balance l k (add id data r)
let rec min_binding = function
Empty -> raise Not_found
| Node (Empty, d, _, _) -> d
| Node (l, _, _, _) -> min_binding l
let rec remove_min_binding = function
Empty -> invalid_arg "Map.remove_min_elt"
| Node (Empty, _, r, _) -> r
| Node (l, d, r, _) -> balance (remove_min_binding l) d r
let merge t1 t2 =
match (t1, t2) with
(Empty, t) -> t
| (t, Empty) -> t
| (_, _) ->
let d = min_binding t2 in
balance t1 d (remove_min_binding t2)
let rec remove id = function
Empty ->
Empty
| (Node (l, k, r, h) as m) ->
let c = String.compare (name id) k.name in
if c = 0 then
match k.previous with
| None -> merge l r
| Some k -> Node (l, k, r, h)
else if c < 0 then
let ll = remove id l in if l == ll then m else balance ll k r
else
let rr = remove id r in if r == rr then m else balance l k rr
let rec find_previous id = function
None ->
raise Not_found
| Some k ->
if same id k.ident then get_data k else find_previous id k.previous
let rec find_same id = function
Empty ->
raise Not_found
| Node(l, k, r, _) ->
let c = String.compare (name id) k.name in
if c = 0 then
if same id k.ident
then get_data k
else find_previous id k.previous
else
find_same id (if c < 0 then l else r)
let rec find_name n = function
Empty ->
raise Not_found
| Node(l, k, r, _) ->
let c = String.compare n k.name in
if c = 0 then
k.ident, get_data k
else
find_name n (if c < 0 then l else r)
let rec get_all = function
| None -> []
| Some k -> (k.ident, get_data k) :: get_all k.previous
let rec find_all n = function
Empty ->
[]
| Node(l, k, r, _) ->
let c = String.compare n k.name in
if c = 0 then
(k.ident, get_data k) :: get_all k.previous
else
find_all n (if c < 0 then l else r)
let get_all_seq k () =
Seq.unfold (Option.map (fun k -> (k.ident, get_data k), k.previous))
k ()
let rec find_all_seq n tbl () =
match tbl with
| Empty -> Seq.Nil
| Node(l, k, r, _) ->
let c = String.compare n k.name in
if c = 0 then
Seq.Cons((k.ident, get_data k), get_all_seq k.previous)
else
find_all_seq n (if c < 0 then l else r) ()
let rec fold_aux f stack accu = function
Empty ->
begin match stack with
[] -> accu
| a :: l -> fold_aux f l accu a
end
| Node(l, k, r, _) ->
fold_aux f (l :: stack) (f k accu) r
let fold_name f tbl accu =
fold_aux (fun k -> f k.ident (get_data k)) [] accu tbl
let rec fold_data f d accu =
match d with
None -> accu
| Some k -> f k.ident (get_data k) (fold_data f k.previous accu)
let fold_all f tbl accu =
fold_aux (fun k -> fold_data f (Some k)) [] accu tbl
let rec iter f = function
Empty -> ()
| Node(l, k, r, _) ->
iter f l; f k.ident (get_data k); iter f r
let key_name = ""
let make_key_generator () =
let c = ref 1 in
function
| Local _
| Scoped _ ->
let stamp = !c in
decr c ;
Local { name = key_name; stamp = stamp }
| global_id ->
Misc.fatal_errorf "Ident.make_key_generator () %s" (name global_id)
let compare x y =
match x, y with
| Local x, Local y ->
let c = x.stamp - y.stamp in
if c <> 0 then c
else compare x.name y.name
| Local _, _ -> 1
| _, Local _ -> (-1)
| Scoped x, Scoped y ->
let c = x.stamp - y.stamp in
if c <> 0 then c
else compare x.name y.name
| Scoped _, _ -> 1
| _, Scoped _ -> (-1)
| Global x, Global y -> compare x y
| Global _, _ -> 1
| _, Global _ -> (-1)
| Predef { stamp = s1; _ }, Predef { stamp = s2; _ } -> compare s1 s2
| Predef _, _ -> 1
| _, Predef _ -> (-1)
| Unscoped x, Unscoped y ->
let x = Unscoped.get_desc x in
let y = Unscoped.get_desc y in
let c = x.stamp - y.stamp in
if c <> 0 then c
else compare x.name y.name
let output oc id = output_string oc (unique_name id)
let hash i = (Char.code (name i).[0]) lxor (stamp i)
let original_equal = equal
include Identifiable.Make (struct
type nonrec t = t
let compare = compare
let output = output
let print = print
let hash = hash
let equal = same
end)
let equal = original_equal
let rename_no_exn = function
| Local { name; stamp = _ }
| Scoped { name; stamp = _; scope = _ } ->
incr currentstamp;
Local { name; stamp = !currentstamp }
| id -> id
let get_currentstamp () =
!currentstamp