Skip to content

Commit 0e3e057

Browse files
authored
flambda-backend: Add identifiers for instantiated functor units (#1092)
The ident `Foo[Bar][Baz]` will stand for the compilation unit `Foo`, given the compilation units `Bar` and `Baz` as arguments.
1 parent 4b75b46 commit 0e3e057

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

typing/ident.ml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type t =
2525
| Predef of { name: string; stamp: int }
2626
(* the stamp is here only for fast comparison, but the name of
2727
predefined identifiers is always unique. *)
28+
| Instance of string * string list
2829

2930
(* A stamp of 0 denotes a persistent identifier *)
3031

@@ -46,11 +47,19 @@ let create_predef s =
4647
let create_persistent s =
4748
Global s
4849

50+
let create_instance f args =
51+
Instance (f, args)
52+
53+
let format_instance f args =
54+
let args_with_brackets = List.map (Format.sprintf "[%s]") args in
55+
String.concat "" (f :: args_with_brackets)
56+
4957
let name = function
5058
| Local { name; _ }
5159
| Scoped { name; _ }
5260
| Global name
5361
| Predef { name; _ } -> name
62+
| Instance (f, args) -> format_instance f args
5463

5564
let rename = function
5665
| Local { name; stamp = _ }
@@ -72,12 +81,14 @@ let unique_name = function
7281
(* we know that none of the predef names (currently) finishes in
7382
"_<some number>", and that their name is unique. *)
7483
name
84+
| Instance _ as i -> name i
7585

7686
let unique_toplevel_name = function
7787
| Local { name; stamp }
7888
| Scoped { name; stamp } -> name ^ "/" ^ Int.to_string stamp
7989
| Global name
8090
| Predef { name; _ } -> name
91+
| Instance _ as i -> name i
8192

8293
let equal i1 i2 =
8394
match i1, i2 with
@@ -88,6 +99,8 @@ let equal i1 i2 =
8899
| Predef { stamp = s1; _ }, Predef { stamp = s2 } ->
89100
(* if they don't have the same stamp, they don't have the same name *)
90101
s1 = s2
102+
| Instance (f1, args1), Instance (f2, args2) ->
103+
String.equal f1 f2 && List.equal String.equal args1 args2
91104
| _ ->
92105
false
93106

@@ -99,6 +112,8 @@ let same i1 i2 =
99112
s1 = s2
100113
| Global name1, Global name2 ->
101114
name1 = name2
115+
| Instance (f1, args1), Instance (f2, args2) ->
116+
String.equal f1 f2 && List.equal String.equal args1 args2
102117
| _ ->
103118
false
104119

@@ -110,7 +125,7 @@ let stamp = function
110125
let scope = function
111126
| Scoped { scope; _ } -> scope
112127
| Local _ -> highest_scope
113-
| Global _ | Predef _ -> lowest_scope
128+
| Global _ | Predef _ | Instance _ -> lowest_scope
114129

115130
let reinit_level = ref (-1)
116131

@@ -121,18 +136,28 @@ let reinit () =
121136

122137
let is_global = function
123138
| Global _ -> true
139+
| Instance _ -> true
124140
| _ -> false
125141

126142
let is_global_or_predef = function
127143
| Local _
128144
| Scoped _ -> false
129145
| Global _
130-
| Predef _ -> true
146+
| Predef _
147+
| Instance _ -> true
131148

132149
let is_predef = function
133150
| Predef _ -> true
134151
| _ -> false
135152

153+
let is_instance = function
154+
| Instance _ -> true
155+
| _ -> false
156+
157+
let split_instance = function
158+
| Instance (f, args) -> Some (f, args)
159+
| _ -> None
160+
136161
let print ~with_scope ppf =
137162
let open Format in
138163
function
@@ -147,6 +172,9 @@ let print ~with_scope ppf =
147172
fprintf ppf "%s%s%s" name
148173
(if !Clflags.unique_ids then sprintf "/%i" n else "")
149174
(if with_scope then sprintf "[%i]" scope else "")
175+
| Instance (f, args) ->
176+
fprintf ppf "%s!" f;
177+
List.iter (fprintf ppf "[%s!]") args
150178

151179
let print_with_scope ppf id = print ~with_scope:true ppf id
152180

@@ -343,6 +371,12 @@ let compare x y =
343371
| Global x, Global y -> compare x y
344372
| Global _, _ -> 1
345373
| _, Global _ -> (-1)
374+
| Instance (f1, args1), Instance (f2, args2) ->
375+
let c = String.compare f1 f2 in
376+
if c <> 0 then c
377+
else List.compare String.compare args1 args2
378+
| Instance _, _ -> 1
379+
| _, Instance _ -> (-1)
346380
| Predef { stamp = s1; _ }, Predef { stamp = s2; _ } -> compare s1 s2
347381

348382
let output oc id = output_string oc (unique_name id)

typing/ident.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ val create_scoped: scope:int -> string -> t
3333
val create_local: string -> t
3434
val create_persistent: string -> t
3535
val create_predef: string -> t
36+
val create_instance: string -> string list -> t
3637

3738
val rename: t -> t
3839
(** Creates an identifier with the same name as the input, a fresh
@@ -54,12 +55,15 @@ val compare: t -> t -> int
5455
val is_global: t -> bool
5556
val is_global_or_predef: t -> bool
5657
val is_predef: t -> bool
58+
val is_instance: t -> bool
5759

5860
val scope: t -> int
5961

6062
val lowest_scope : int
6163
val highest_scope: int
6264

65+
val split_instance : t -> (string * string list) option
66+
6367
val reinit: unit -> unit
6468

6569
type 'a tbl

0 commit comments

Comments
 (0)