Source file compilation_unit.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
[@@@ocaml.warning "+a-9-40-41-42"]
open! Int_replace_polymorphic_compare
module List = Misc.Stdlib.List
module String = Misc.Stdlib.String
module Fmt = Format_doc
type error =
| Invalid_character of char * string
| Bad_compilation_unit_name of string
| Child_of_instance of { parent_name : string }
| Packed_instance of { name : string }
| Already_an_instance of { name : string }
exception Error of error
module Name : sig
type t
include Identifiable.S with type t := t
val dummy : t
val predef_exn : t
val of_string : string -> t
val to_string : t -> string
val of_head_of_global_name : Global_module.Name.t -> t
val of_parameter_name : Global_module.Parameter_name.t -> t
val to_global_name : t -> Global_module.Name.t
val to_parameter_name : t -> Global_module.Parameter_name.t
val check_as_path_component : t -> unit
val print_as_inline_code : Fmt.formatter -> t -> unit
val print : Fmt.formatter -> t -> unit
end = struct
type t = string
let doc_print = Fmt.pp_print_string
include Identifiable.Make (struct
type nonrec t = t
let compare = String.compare
let equal = String.equal
let hash = Hashtbl.hash
let print ppf x = Fmt.compat doc_print ppf x
let output = Misc.output_of_doc_print doc_print
end)
let print = doc_print
let isupper chr = Char.equal (Char.uppercase_ascii chr) chr
let of_string str =
if String.equal str ""
then raise (Error (Bad_compilation_unit_name str))
else str
let of_head_of_global_name (name : Global_module.Name.t) = of_string name.head
let of_parameter_name (name : Global_module.Parameter_name.t) =
of_string (Global_module.Parameter_name.to_string name)
let to_global_name t = Global_module.Name.create_no_args t
let to_parameter_name t = Global_module.Parameter_name.of_string t
let check_as_path_component t =
if
String.length t < 1
|| (not (isupper (String.get t 0)))
|| String.contains t '.'
then raise (Error (Bad_compilation_unit_name t))
let dummy = "*dummy*"
let predef_exn = "*predef*"
let to_string t = t
let print_as_inline_code ppf t = Misc.Style.inline_code ppf (to_string t)
end
module Prefix : sig
type t
include Identifiable.S with type t := t
val parse_for_pack : string -> t
val from_clflags : unit -> t
val of_list : Name.t list -> t
val to_list : t -> Name.t list
val to_string : t -> string
val empty : t
val is_empty : t -> bool
val print : Fmt.formatter -> t -> unit
end = struct
type t = Name.t list
let doc_print ppf p =
Fmt.pp_print_list
~pp_sep:(fun ppf () -> Fmt.pp_print_string ppf ".")
Name.print ppf p
include Identifiable.Make (struct
type nonrec t = t
let equal = List.equal Name.equal
let compare = List.compare Name.compare
let hash = Hashtbl.hash
let print ppf p = Fmt.compat doc_print ppf p
let output = Misc.output_of_doc_print doc_print
end)
let print = doc_print
let is_valid_character first_char c =
match c with
| 'A' .. 'Z' -> true
| '_' | '0' .. '9' | 'a' .. 'z' -> not first_char
| _ -> false
let parse_for_pack pack =
let prefix = String.split_on_char '.' pack in
ListLabels.iter prefix ~f:(fun module_name ->
String.iteri
(fun i c ->
if not (is_valid_character (i = 0) c)
then raise (Error (Invalid_character (c, module_name))))
module_name);
ListLabels.map prefix ~f:Name.of_string
let from_clflags () =
match !Clflags.for_package with
| None -> []
| Some pack -> parse_for_pack pack
let to_string p = Fmt.asprintf "%a" print p
let empty = []
let is_empty t = match t with [] -> true | _ :: _ -> false
let of_list t = t
let to_list t = t
end
module T0 : sig
type descr = private
{ name : Name.t;
for_pack_prefix : Prefix.t;
arguments : argument list
}
and argument =
{ param : Name.t;
value : t
}
and t
val descr : t -> descr
val name : t -> Name.t
val is_plain_name : t -> bool
val for_pack_prefix : t -> Prefix.t
val instance_arguments : t -> argument list
val to_global_name : t -> Global_module.Name.t option
val to_global_name_exn : t -> Global_module.Name.t
val to_global_name_without_prefix : t -> Global_module.Name.t
val create_full : Prefix.t -> Name.t -> argument list -> t
val of_global_name : Global_module.Name.t -> t
val compare : t -> t -> int
end = struct
type descr =
{ name : Name.t;
for_pack_prefix : Prefix.t;
arguments : argument list
}
and argument =
{ param : Name.t;
value : t
}
and full =
| With_prefix of
{ name : Name.t;
for_pack_prefix : Prefix.t
}
| Global of Global_module.Name.t
and t = Obj.t
let is_plain_name t =
let tag = Obj.tag t in
tag = Obj.string_tag
let of_plain_name name : t = Obj.repr (name : Name.t)
let of_full full : t = Obj.repr (full : full)
let of_global_name (glob : Global_module.Name.t) =
match glob with
| { head; args = [] } -> of_plain_name (Name.of_string head)
| _ -> of_full (Global glob)
let convert_arguments l =
ListLabels.map
~f:(fun ({ param; value } : Global_module.Name.argument) ->
{ param = Name.of_parameter_name param; value = of_global_name value })
l
let descr t =
let tag = Obj.tag t in
assert (tag < 2 || tag = Obj.string_tag);
if tag = Obj.string_tag
then
{ name = Sys.opaque_identity (Obj.obj t : Name.t);
for_pack_prefix = Prefix.empty;
arguments = []
}
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix { name; for_pack_prefix } ->
{ name; for_pack_prefix; arguments = [] }
| Global { head; args } ->
let name = Name.of_string head in
let arguments = convert_arguments args in
{ name; arguments; for_pack_prefix = Prefix.empty }
let name t =
let tag = Obj.tag t in
assert (tag < 2 || tag = Obj.string_tag);
if tag = Obj.string_tag
then Sys.opaque_identity (Obj.obj t : Name.t)
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix { name; _ } -> name
| Global { head; _ } -> Name.of_string head
let for_pack_prefix t =
let tag = Obj.tag t in
assert (tag < 2 || tag = Obj.string_tag);
if tag = Obj.string_tag
then Prefix.empty
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix { for_pack_prefix; _ } -> for_pack_prefix
| Global _ -> Prefix.empty
let instance_arguments t =
let tag = Obj.tag t in
assert (tag < 2 || tag = Obj.string_tag);
if tag = Obj.string_tag
then []
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix _ -> []
| Global { args; _ } -> convert_arguments args
let rec compare t1 t2 =
if t1 == t2
then 0
else
let { for_pack_prefix = for_pack_prefix1;
name = name1;
arguments = args1
} =
descr t1
in
let { for_pack_prefix = for_pack_prefix2;
name = name2;
arguments = args2
} =
descr t2
in
let c = Name.compare name1 name2 in
if c <> 0
then c
else
let c = Prefix.compare for_pack_prefix1 for_pack_prefix2 in
if c <> 0 then c else List.compare compare_instance_arg args1 args2
and compare_instance_arg { param = param1; value = value1 }
{ param = param2; value = value2 } =
let c = Name.compare param1 param2 in
if c <> 0 then c else compare value1 value2
let compare_argument_by_name arg1 arg2 = Name.compare arg1.param arg2.param
let to_global_name_exn t =
if is_plain_name t
then
let name = Sys.opaque_identity (Obj.obj t : Name.t) in
Global_module.Name.create_no_args (Name.to_string name)
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix { name; _ } ->
raise (Error (Packed_instance { name = name |> Name.to_string }))
| Global glob -> glob
let to_global_name t =
try Some (to_global_name_exn t) with Error (Packed_instance _) -> None
let to_global_name_without_prefix t =
if is_plain_name t
then
let name = Sys.opaque_identity (Obj.obj t : Name.t) in
Global_module.Name.create_no_args (Name.to_string name)
else
let full = Sys.opaque_identity (Obj.obj t : full) in
match full with
| With_prefix { name; _ } ->
Global_module.Name.create_no_args (Name.to_string name)
| Global glob -> glob
let of_global_name (name : Global_module.Name.t) =
match name with
| { head; args = [] } -> of_plain_name (head |> Name.of_string)
| _ -> of_full (Global name)
let create_full for_pack_prefix name arguments =
let empty_prefix = Prefix.is_empty for_pack_prefix in
let empty_arguments = match arguments with [] -> true | _ -> false in
let () =
if not empty_prefix
then (
if not empty_arguments
then
raise (Error (Packed_instance { name = name |> Name.to_string }));
Name.check_as_path_component name;
ListLabels.iter ~f:Name.check_as_path_component
(for_pack_prefix |> Prefix.to_list))
in
let arguments = ListLabels.sort arguments ~cmp:compare_argument_by_name in
if empty_prefix && empty_arguments
then of_plain_name name
else if empty_prefix
then
let head = Name.to_string name in
let arguments =
ListLabels.map
~f:(fun { param; value } : Global_module.Name.argument ->
{ param = Name.to_parameter_name param;
value = to_global_name_exn value
})
arguments
in
of_full (Global (Global_module.Name.create_exn head arguments))
else of_full (With_prefix { for_pack_prefix; name })
end
include T0
let create prefix name = create_full prefix name []
let to_prefix parent =
if not (Prefix.is_empty (for_pack_prefix parent))
then
raise
(Error (Child_of_instance { parent_name = name parent |> Name.to_string }));
(for_pack_prefix parent |> Prefix.to_list) @ [name parent] |> Prefix.of_list
let create_child parent name_ = create (to_prefix parent) name_
let of_string str =
let for_pack_prefix, name =
if String.equal str ".cinaps" || String.equal str "(.cinaps)"
then Prefix.empty, Name.of_string str
else
match String.rindex_opt str '.' with
| None -> Prefix.empty, Name.of_string str
| Some _ ->
Misc.fatal_errorf "[of_string] does not parse qualified names: %s" str
in
create for_pack_prefix name
let of_complete_global_exn glob =
if not (Global_module.is_complete glob)
then
Misc.fatal_errorf_doc "of_complete_global_exn@ %a" Global_module.print glob;
of_global_name (glob |> Global_module.to_name)
let dummy = create Prefix.empty (Name.of_string "*none*")
let predef_exn = create Prefix.empty Name.predef_exn
let name_as_string t = name t |> Name.to_string
let equal_to_name t other_name =
is_plain_name t && Name.equal other_name (name t)
let with_for_pack_prefix t for_pack_prefix =
create_full for_pack_prefix (name t) (instance_arguments t)
let is_packed t = not (Prefix.is_empty (for_pack_prefix t))
let rec doc_print ppf t =
let { for_pack_prefix; name; arguments } = descr t in
let () =
if Prefix.is_empty for_pack_prefix
then Fmt.fprintf ppf "%a" Name.print name
else Fmt.fprintf ppf "%a.%a" Prefix.print for_pack_prefix Name.print name
in
ListLabels.iter ~f:(print_arg ppf) arguments
and print_arg ppf { param; value } =
Fmt.fprintf ppf "[%a:%a]" Name.print param doc_print value
include Identifiable.Make (struct
type nonrec t = t
let compare = compare
let equal x y = if x == y then true else compare x y = 0
let print ppf t = Fmt.compat doc_print ppf t
let output = Misc.output_of_doc_print doc_print
let rec hash t =
let { for_pack_prefix; name; arguments } = descr t in
Hashtbl.hash
( Name.hash name,
Prefix.hash for_pack_prefix,
ListLabels.map ~f:hash_arg arguments )
and hash_arg { param; value } = Hashtbl.hash (Name.hash param, hash value)
end)
let print = doc_print
let is_instance t =
match instance_arguments t with [] -> false | _ :: _ -> true
let full_path_as_string t =
if Prefix.is_empty (for_pack_prefix t) && not (is_instance t)
then Name.to_string (name t)
else Fmt.asprintf "%a" print t
let create_instance t arguments =
let { for_pack_prefix; name; arguments = existing_arguments } = descr t in
let () =
match existing_arguments with
| [] -> ()
| _ :: _ ->
raise (Error (Already_an_instance { name = full_path_as_string t }))
in
create_full for_pack_prefix name arguments
let split_instance_exn t =
match descr t with
| { arguments = []; _ } ->
Misc.fatal_errorf_doc "@[<hov 1>Not an instance:@ %a@]" print t
| { name; for_pack_prefix; arguments } ->
create_full for_pack_prefix name [], arguments
let full_path t = Prefix.to_list (for_pack_prefix t) @ [name t]
let flatten t =
let rec flatten_arg { param; value } ~depth =
assert (not (is_packed value));
let { name; arguments; _ } = descr value in
(depth, param, name)
:: ListLabels.concat_map ~f:(flatten_arg ~depth:(depth + 1)) arguments
in
let { for_pack_prefix; name; arguments } = descr t in
( for_pack_prefix,
name,
ListLabels.concat_map ~f:(flatten_arg ~depth:0) arguments )
let base_filename t =
let _prefix, name, arguments = flatten t in
let arg_segments =
ListLabels.map arguments ~f:(fun (depth, _param, value) ->
String.make (depth + 1) '-' ^ (value |> Name.to_string))
in
String.concat "" ((name |> Name.to_string) :: arg_segments)
|> String.uncapitalize_ascii
let instance_separator = "____"
let instance_separator_depth_char = '_'
let mangle_for_linkage_name ~pack_separator t =
let for_pack_prefix, name, flattened_instance_args = flatten t in
let name = Name.to_string name in
let suffix =
if not (Prefix.is_empty for_pack_prefix)
then begin
assert (match flattened_instance_args with [] -> true | _ -> false);
let pack_names =
Prefix.to_list for_pack_prefix |> List.map Name.to_string
in
String.concat (pack_separator ()) (pack_names @ [name])
end
else begin
let arg_segments =
List.map
(fun (depth, _param, value) ->
let =
String.make depth instance_separator_depth_char
in
let value = value |> Name.to_string in
String.concat "" [instance_separator; extra_separators; value])
flattened_instance_args
in
String.concat "" arg_segments
end
in
name ^ suffix
let is_parent t ~child =
List.equal Name.equal (full_path t) (Prefix.to_list (for_pack_prefix child))
let is_strict_prefix list1 ~of_:list2 ~equal =
(not (List.equal equal list1 list2)) && List.is_prefix list1 ~of_:list2 ~equal
let can_access_by_name t ~accessed_by:me =
let my_path = full_path me in
let t's_prefix_is_my_ancestor =
List.is_prefix
(for_pack_prefix t |> Prefix.to_list)
~of_:my_path ~equal:Name.equal
in
let t_is_not_my_strict_ancestor =
not (is_strict_prefix (full_path t) ~of_:my_path ~equal:Name.equal)
in
t's_prefix_is_my_ancestor && t_is_not_my_strict_ancestor
let can_access_cmx_file = can_access_by_name
let which_cmx_file desired_comp_unit ~accessed_by : t =
let desired_prefix = for_pack_prefix desired_comp_unit in
if Prefix.is_empty desired_prefix
then
desired_comp_unit
else
let () = assert (not (is_instance desired_comp_unit)) in
let rec match_components ~current ~desired ~acc_rev =
match current, desired with
| current_name :: current, desired_name :: desired ->
if Name.equal current_name desired_name
then
let acc_rev = current_name :: acc_rev in
match_components ~current ~desired ~acc_rev
else
acc_rev, desired_name
| [], desired_name :: _desired ->
acc_rev, desired_name
| [], [] ->
acc_rev, name desired_comp_unit
| _ :: _, [] ->
Misc.fatal_errorf_doc
"Compilation unit@ %a@ is inaccessible when compiling compilation \
unit@ %a"
print desired_comp_unit print accessed_by
in
let prefix_rev, name =
match_components ~current:(full_path accessed_by)
~desired:(full_path desired_comp_unit)
~acc_rev:[]
in
create (ListLabels.rev prefix_rev |> Prefix.of_list) name
let print_name ppf t = Fmt.fprintf ppf "%a" Name.print (name t)
let to_global_ident_for_bytecode t =
Ident.create_persistent (full_path_as_string t)
let print_debug ppf t =
let name = name t in
let for_pack_prefix = for_pack_prefix t in
if Prefix.is_empty for_pack_prefix
then Fmt.fprintf ppf "@[<hov 1>(@[<hov 1>(id@ %a)@])@]" Name.print name
else
Fmt.fprintf ppf
"@[<hov 1>(@[<hov 1>(for_pack_prefix@ %a)@]@;@[<hov 1>(name@ %a)@]"
Prefix.print for_pack_prefix Name.print name
let print_as_inline_code = Misc.Style.as_inline_code print
let fwd_get_current : (unit -> t option) ref = ref (fun () -> assert false)
let get_current () = !fwd_get_current ()
let get_current_or_dummy () = Option.value (get_current ()) ~default:dummy
let get_current_exn () =
match get_current () with
| Some t -> t
| None -> Misc.fatal_error "No compilation unit set"
let is_current t =
match get_current () with None -> false | Some t' -> equal t t'
let name_mangling_scheme_override : Config.name_mangling_scheme option ref =
ref None
let set_name_mangling_scheme_override scheme =
name_mangling_scheme_override := Some scheme
let name_mangling_scheme_for_current_unit () =
match !name_mangling_scheme_override with
| Some scheme -> scheme
| None -> Config.name_mangling_scheme
module Private = struct
let fwd_get_current = fwd_get_current
end