Debian Codegraph @

library/goptions.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* This module manages customization parameters at the vernacular level *)
12
13 open Util
14 open Summary.Stage
15
16 type option_name = string list
17
18 type _ option_kind =
19 | BoolKind : bool option_kind
20 | IntKind : int option option_kind
21 | StringKind : string option_kind
22 | StringOptKind : string option option_kind
23
24 type option_value =
25 | BoolValue of bool
26 | IntValue of int option
27 | StringValue of string
28 | StringOptValue of string option
29
30 type table_value =
31 | StringRefValue of string
32 | QualidRefValue of Libnames.qualid
33
34 (** Summary of an option status *)
35 type option_state = {
36 opt_depr : Deprecation.t option;
37 opt_value : option_value;
38 }
39
40 (****************************************************************************)
41 (* 0- Common things *)
42
43 let nickname table = String.concat " " table
44
45 let error_no_table_of_this_type ~kind key =
46 CErrors.user_err
47 Pp.(str ("There is no " ^ kind ^ "-valued table with this name: \""
48 ^ nickname key ^ "\"."))
49
50 let error_undeclared_key key =
51 CErrors.user_err
52 Pp.(str ("There is no flag, option or table with this name: \""
53 ^ nickname key ^ "\"."))
54
55 (****************************************************************************)
56 (* 1- Tables *)
57
58 type 'a table_of_A = {
59 add : Environ.env -> Libobject.locality -> 'a -> unit;
60 remove : Environ.env -> Libobject.locality -> 'a -> unit;
61 mem : Environ.env -> 'a -> unit;
62 print : unit -> unit;
63 }
64
65 let opts_cat = Libobject.create_category "options"
66
67 module MakeTable =
68 functor
69 (A : sig
70 type t
71 type key
72 module Set : CSig.USetS with type elt = t
73 val table : (string * key table_of_A) list ref
74 val encode : Environ.env -> key -> t
75 val subst : Mod_subst.substitution -> t -> t
76 val check_local : Libobject.locality -> t -> unit
77 val discharge : t -> t
78 val printer : t -> Pp.t
79 val key : option_name
80 val title : string
81 val member_message : t -> bool -> Pp.t
82 end) ->
83 struct
84 type option_mark =
85 | GOadd
86 | GOrmv
87
88 let nick = nickname A.key
89
90 let () =
91 if String.List.mem_assoc nick !A.table then
92 CErrors.anomaly
93 Pp.(strbrk "Sorry, this table name (" ++ str nick
94 ++ strbrk ") is already used.")
95
96 module MySet = A.Set
97
98 let t = Summary.ref ~stage:Interp MySet.empty ~name:nick
99
100 let inGo : Libobject.locality * (option_mark * A.t) -> Libobject.obj =
101 let cache (f,p) = match f with
102 | GOadd -> t := MySet.add p !t
103 | GOrmv -> t := MySet.remove p !t in
104 let subst (subst,(f,p as obj)) =
105 let p' = A.subst subst p in
106 if p' == p then obj else
107 (f,p')
108 in
109 Libobject.declare_object @@
110 Libobject.object_with_locality ~cat:opts_cat nick
111 ~cache ~subst:(Some subst) ~discharge:(on_snd A.discharge)
112
113 let add_option local c =
114 A.check_local local c;
115 Lib.add_leaf (inGo (local,(GOadd, c)))
116
117 let remove_option local c =
118 A.check_local local c;
119 Lib.add_leaf (inGo (local,(GOrmv, c)))
120
121 let print_table table_name printer table =
122 let open Pp in
123 let pp = if MySet.is_empty table then str table_name ++ str " is empty."
124 else
125 str table_name ++ str ":" ++ spc() ++
126 (hov 0 (prlist_with_sep spc printer (MySet.elements table))) ++
127 str "."
128 in
129 Feedback.msg_notice pp
130
131 let table_of_A = {
132 add = (fun env local x -> add_option local (A.encode env x));
133 remove = (fun env local x -> remove_option local (A.encode env x));
134 mem = (fun env x ->
135 let y = A.encode env x in
136 let answer = MySet.mem y !t in
137 Feedback.msg_info (A.member_message y answer));
138 print = (fun () -> print_table A.title A.printer !t);
139 }
140
141 let () = A.table := (nick, table_of_A)::!A.table
142
143 let v () = !t
144 let active x = A.Set.mem x !t
145 let set local x b = if b then add_option local x else remove_option local x
146 end
147
148 let string_table = ref []
149
150 let get_string_table k = String.List.assoc (nickname k) !string_table
151
152 module type StringConvertArg =
153 sig
154 val key : option_name
155 val title : string
156 val member_message : string -> bool -> Pp.t
157 end
158
159 module StringConvert = functor (A : StringConvertArg) ->
160 struct
161 type t = string
162 type key = string
163 module Set = CString.Set
164 let table = string_table
165 let encode _env x = x
166 let subst _ x = x
167 let check_local _ _ = ()
168 let discharge x = x
169 let printer = Pp.str
170 let key = A.key
171 let title = A.title
172 let member_message = A.member_message
173 end
174
175 module MakeStringTable =
176 functor (A : StringConvertArg) -> MakeTable (StringConvert(A))
177
178 let ref_table = ref []
179
180 let get_ref_table k = String.List.assoc (nickname k) !ref_table
181
182 module type RefConvertArg =
183 sig
184 type t
185 module Set : CSig.USetS with type elt = t
186 val encode : Environ.env -> Libnames.qualid -> t
187 val subst : Mod_subst.substitution -> t -> t
188 val check_local : Libobject.locality -> t -> unit
189 val discharge : t -> t
190 val printer : t -> Pp.t
191 val key : option_name
192 val title : string
193 val member_message : t -> bool -> Pp.t
194 end
195
196 module RefConvert = functor (A : RefConvertArg) ->
197 struct
198 type t = A.t
199 type key = Libnames.qualid
200 module Set = A.Set
201 let table = ref_table
202 let encode = A.encode
203 let subst = A.subst
204 let check_local = A.check_local
205 let discharge = A.discharge
206 let printer = A.printer
207 let key = A.key
208 let title = A.title
209 let member_message = A.member_message
210 end
211
212 module MakeRefTable =
213 functor (A : RefConvertArg) -> MakeTable (RefConvert(A))
214
215 type iter_table_aux = { aux : 'a. 'a table_of_A -> Environ.env -> 'a -> unit }
216
217 let iter_table env f key lv =
218 let aux = function
219 | StringRefValue s ->
220 begin
221 try f.aux (get_string_table key) env s
222 with Not_found -> error_no_table_of_this_type ~kind:"string" key
223 end
224 | QualidRefValue locqid ->
225 begin
226 try f.aux (get_ref_table key) env locqid
227 with Not_found -> error_no_table_of_this_type ~kind:"qualid" key
228 end
229 in
230 List.iter aux lv
231
232 (****************************************************************************)
233 (* 2- Flags. *)
234
235 type option_locality = OptDefault | OptLocal | OptExport | OptGlobal
236
237 module OptionOrd =
238 struct
239 type t = option_name
240 let compare opt1 opt2 = List.compare String.compare opt1 opt2
241 end
242
243 module OptionMap = Map.Make(OptionOrd)
244
245 module RawOpt = struct
246 type 'a t = {
247 kind : 'a option_kind;
248 depr : Deprecation.t option;
249 stage : Summary.Stage.t;
250 read : unit -> 'a;
251 write : option_locality -> 'a -> unit;
252 }
253 end
254
255 type any_opt = AnyOpt : 'a RawOpt.t -> any_opt
256
257 let value_tab = ref OptionMap.empty
258
259 (* This raises Not_found if option of key [key] is unknown *)
260
261 let get_option key = OptionMap.find key !value_tab
262
263 let check_key key = try
264 let _ = get_option key in
265 CErrors.user_err Pp.(str "Sorry, this option name ("
266 ++ str (nickname key) ++ str ") is already used.")
267 with Not_found ->
268 if String.List.mem_assoc (nickname key) !string_table
269 || String.List.mem_assoc (nickname key) !ref_table
270 then CErrors.user_err Pp.(str "Sorry, this option name ("
271 ++ str (nickname key) ++ str ") is already used.")
272
273 let declare_raw name v = value_tab := OptionMap.add name (AnyOpt v) !value_tab
274
275 (* not quite the same as RawOpt.t: write takes a option_locality, optkey field present *)
276 type 'a option_sig = {
277 optstage : Summary.Stage.t;
278 optdepr : Deprecation.t option;
279 optkey : option_name;
280 optread : unit -> 'a;
281 optwrite : 'a -> unit }
282
283 open Libobject
284
285 let warn_deprecated_option =
286 Deprecation.create_warning ~object_name:"Option" ~warning_name_if_no_since:"deprecated-option"
287 (fun key -> Pp.str (nickname key))
288
289 let option_object name stage act =
290 let cache_option (l,v) = act v in
291 let load_option i (l, _ as o) = match l with
292 | OptGlobal -> cache_option o
293 | OptExport -> ()
294 | OptLocal | OptDefault ->
295 (* Ruled out by classify_function *)
296 assert false
297 in
298 let open_option (l, _ as o) = match l with
299 | OptExport -> cache_option o
300 | OptGlobal -> ()
301 | OptLocal | OptDefault ->
302 (* Ruled out by classify_function *)
303 assert false
304 in
305 let discharge_option (l,_ as o) =
306 match l with OptLocal -> None | (OptExport | OptGlobal | OptDefault) -> Some o
307 in
308 let classify_option (l,_) =
309 match l with (OptExport | OptGlobal) -> Substitute | (OptLocal | OptDefault) -> Dispose
310 in
311 { (Libobject.default_object name) with
312 object_stage = stage;
313 cache_function = cache_option;
314 load_function = load_option;
315 open_function = simple_open ~cat:opts_cat open_option;
316 subst_function = (fun (_,o) -> o);
317 discharge_function = discharge_option;
318 classify_function = classify_option;
319 }
320
321 let declare_option ?(preprocess = fun x -> x) ?(no_summary=false) ~kind
322 { optstage=stage; optdepr=depr; optkey=key; optread=read; optwrite=write } =
323 check_key key;
324 let () =
325 if not no_summary then begin
326 let default = read() in
327 Summary.declare_summary (nickname key)
328 { stage;
329 Summary.freeze_function = read;
330 Summary.unfreeze_function = write;
331 Summary.init_function = (fun () -> write default) }
332 end
333 in
334 let change =
335 let options : option_locality * _ -> obj =
336 declare_object (option_object (nickname key) stage write)
337 in
338 (fun l v -> let v = preprocess v in Lib.add_leaf (options (l, v)))
339 in
340 let warn () = depr |> Option.iter (fun depr -> warn_deprecated_option (key,depr)) in
341 let cwrite l v = warn (); change l v in
342 declare_raw key {
343 kind;
344 stage;
345 depr;
346 read;
347 write = cwrite;
348 }
349
350 let declare_append_only_option ?(preprocess= fun x -> x) ~sep
351 { optstage=stage; optdepr=depr; optkey=key; optread=read; optwrite=write } =
352 check_key key;
353 let default = read() in
354 let () = Summary.declare_summary (nickname key)
355 { stage;
356 Summary.freeze_function = read;
357 Summary.unfreeze_function = write;
358 Summary.init_function = (fun () -> write default) }
359 in
360 let append x = write (read()^sep^x) in
361 let change =
362 let options : option_locality * _ -> obj =
363 declare_object (option_object (nickname key) stage append)
364 in
365 (fun l v -> let v = preprocess v in Lib.add_leaf (options (l, v)))
366 in
367 let warn () = depr |> Option.iter (fun depr -> warn_deprecated_option (key,depr)) in
368 let cwrite l v = warn (); change l v in
369 declare_raw key {
370 kind = StringKind;
371 stage;
372 depr;
373 read;
374 write = cwrite;
375 }
376
377 type 'a getter = { get : unit -> 'a }
378
379 type 'a opt_decl = ?stage:Summary.Stage.t -> ?depr:Deprecation.t -> key:option_name -> value:'a -> unit -> 'a getter
380
381 let declare_interpreted_option_and_ref from to_ ~stage ?depr ~key kind ~value () =
382 let r_opt = ref value in
383 let optwrite v = r_opt := from v in
384 let optread () = to_ !r_opt in
385 let () = declare_option ~kind {
386 optstage = stage;
387 optdepr = depr;
388 optkey = key;
389 optread; optwrite
390 } in
391 { get = fun () -> !r_opt }
392
393 let declare_int_option_and_ref ?(stage=Interp) ?depr ~key ~(value:int) () =
394 let from v = Option.default value v in
395 let to_ v = Some v in
396 declare_interpreted_option_and_ref from to_ ~stage ?depr ~key IntKind ~value ()
397
398 let declare_intopt_option_and_ref ?(stage=Interp) ?depr ~key ~value () =
399 declare_interpreted_option_and_ref Fun.id Fun.id ~stage ?depr ~key IntKind ~value ()
400
401 let declare_nat_option_and_ref ?(stage=Interp) ?depr ~key ~(value:int) () =
402 assert (value >= 0);
403 let from v =
404 let v = Option.default value v in
405 if v < 0 then
406 CErrors.user_err Pp.(str "This option expects a non-negative value.");
407 v
408 in
409 let to_ v = Some v in
410 declare_interpreted_option_and_ref from to_ ~stage ?depr ~key IntKind ~value ()
411
412 let declare_bool_option_and_ref ?(stage=Interp) ?depr ~key ~(value:bool) () =
413 declare_interpreted_option_and_ref Fun.id Fun.id ~stage ?depr ~key BoolKind ~value ()
414
415 let declare_string_option_and_ref ?(stage=Interp) ?depr ~key ~(value:string) () =
416 let from v = Option.default value v in
417 let to_ v = Some v in
418 declare_interpreted_option_and_ref from to_ ~stage ?depr ~key StringOptKind ~value ()
419
420 let declare_stringopt_option_and_ref ?(stage=Interp) ?depr ~key ~value () =
421 declare_interpreted_option_and_ref Fun.id Fun.id ~stage ?depr ~key StringOptKind ~value ()
422
423 let declare_interpreted_string_option_and_ref from_string to_string ?(stage=Interp) ?depr ~key ~(value:'a) () =
424 let from v = Option.default value @@ Option.map from_string v in
425 let to_ v = Some (to_string v) in
426 declare_interpreted_option_and_ref from to_ ~stage ?depr ~key StringOptKind ~value ()
427
428 (* 3- User accessible commands *)
429
430 (* Setting values of options *)
431
432 let warn_unknown_option =
433 CWarnings.create
434 ~name:"unknown-option"
435 Pp.(fun key -> strbrk "There is no flag or option with this name: \"" ++
436 str (nickname key) ++ str "\".")
437
438 let to_option_value (type a) (k:a option_kind) (v:a) : option_value =
439 match k with
440 | BoolKind -> BoolValue v
441 | IntKind -> IntValue v
442 | StringKind -> StringValue v
443 | StringOptKind -> StringOptValue v
444
445 let get_option_value key =
446 try
447 let AnyOpt opt = get_option key in
448 Some (fun () -> to_option_value opt.kind (opt.read ()))
449 with Not_found -> None
450
451 let bad_type_error ~expected ~got =
452 CErrors.user_err Pp.(strbrk "Bad type of value for this option:" ++ spc()
453 ++ str "expected " ++ str expected ++ str ", got " ++ str got ++ str ".")
454
455 let error_flag () =
456 CErrors.user_err Pp.(str "This is a flag. It does not take a value.")
457
458 type 'a check_and_cast = { check_and_cast : 'b. 'a -> 'b option_kind -> 'b }
459
460 (** Sets the option only if [stage] matches the option declaration or if [stage]
461 is omitted. If the option is not found, a warning is emitted only if the stage
462 is [Interp] or omitted. *)
463 let set_option_value ?(locality = OptDefault) ?stage { check_and_cast } key v =
464 match get_option key with
465 | exception Not_found -> begin match stage with None | Some Summary.Stage.Interp -> warn_unknown_option key | _ -> () end
466 | AnyOpt opt ->
467 if Option.cata (fun s -> s = opt.stage) true stage then
468 opt.write locality (check_and_cast v opt.kind)
469
470 let check_int_value (type a) (v:int option) (k:a option_kind) : a =
471 match k with
472 | BoolKind -> error_flag ()
473 | IntKind -> v
474 | StringKind | StringOptKind ->
475 bad_type_error ~expected:"string" ~got:"int"
476
477 let check_bool_value (type a) (v:bool) (k:a option_kind) : a =
478 match k with
479 | BoolKind -> v
480 | _ ->
481 CErrors.user_err
482 Pp.(str "This is an option. A value must be provided.")
483
484 let check_string_value (type a) (v:string) (k:a option_kind) : a =
485 match k with
486 | BoolKind -> error_flag ()
487 | IntKind -> bad_type_error ~expected:"int" ~got:"string"
488 | StringKind -> v
489 | StringOptKind -> (Some v)
490
491 let check_unset_value (type a) () (k:a option_kind) : a =
492 match k with
493 | BoolKind -> false
494 | IntKind -> None
495 | StringOptKind -> None
496 | StringKind ->
497 CErrors.user_err
498 Pp.(str "This option does not support the \"Unset\" command.")
499
500 (* Nota: For compatibility reasons, some errors are treated as
501 warnings. This allows a script to refer to an option that doesn't
502 exist anymore *)
503
504 let set_int_option_value_gen ?locality ?stage =
505 set_option_value ?locality ?stage { check_and_cast = check_int_value }
506 let set_bool_option_value_gen ?locality ?stage key v =
507 set_option_value ?locality ?stage { check_and_cast = check_bool_value } key v
508 let set_string_option_value_gen ?locality ?stage =
509 set_option_value ?locality ?stage { check_and_cast = check_string_value }
510 let unset_option_value_gen ?locality ?stage key =
511 set_option_value ?locality ?stage { check_and_cast = check_unset_value } key ()
512
513 let set_int_option_value ?stage opt v = set_int_option_value_gen ?stage opt v
514 let set_bool_option_value ?stage opt v = set_bool_option_value_gen ?stage opt v
515 let set_string_option_value ?stage opt v = set_string_option_value_gen ?stage opt v
516
517 (* Printing options/tables *)
518
519 let msg_option_value = Pp.(function
520 | BoolValue true -> str "on"
521 | BoolValue false -> str "off"
522 | IntValue (Some n) -> int n
523 | IntValue None -> str "undefined"
524 | StringValue s -> quote (str s)
525 | StringOptValue None -> str "undefined"
526 | StringOptValue (Some s) -> quote (str s))
527
528 let print_option_value key =
529 let AnyOpt opt = get_option key in
530 let s = opt.read () in
531 match to_option_value opt.kind s with
532 | BoolValue b ->
533 Feedback.msg_notice Pp.(prlist_with_sep spc str key ++ str " is "
534 ++ str (if b then "on" else "off"))
535 | s ->
536 Feedback.msg_notice Pp.(str "Current value of "
537 ++ prlist_with_sep spc str key ++ str " is " ++ msg_option_value s)
538
539 let get_tables () =
540 let tables = !value_tab in
541 let fold key (AnyOpt opt) accu =
542 let state = {
543 opt_depr = opt.depr;
544 opt_value = to_option_value opt.kind (opt.read ());
545 } in
546 OptionMap.add key state accu
547 in
548 OptionMap.fold fold tables OptionMap.empty
549
550 let print_tables () =
551 let open Pp in
552 let print_option key value depr =
553 let msg = str " " ++ str (nickname key) ++ str ": " ++ msg_option_value value in
554 let depr = pr_opt (fun depr ->
555 hov 2
556 (str "[DEPRECATED" ++
557 pr_opt (fun since -> str "since " ++ str since) depr.Deprecation.since ++
558 pr_opt str depr.Deprecation.note ++
559 str "]"))
560 depr
561 in
562 msg ++ depr ++ fnl()
563 in
564 str "Options:" ++ fnl () ++
565 OptionMap.fold
566 (fun key (AnyOpt opt) p ->
567 p ++ print_option key (to_option_value opt.kind (opt.read ())) opt.depr)
568 !value_tab (mt ()) ++
569 str "Tables:" ++ fnl () ++
570 List.fold_right
571 (fun (nickkey,_) p -> p ++ str " " ++ str nickkey ++ fnl ())
572 !string_table (mt ()) ++
573 List.fold_right
574 (fun (nickkey,_) p -> p ++ str " " ++ str nickkey ++ fnl ())
575 !ref_table (mt ()) ++
576 fnl ()
577
578 (* Compat *)
579
580 let declare_int_option ?preprocess osig =
581 declare_option ?preprocess ~kind:IntKind osig
582
583 let declare_bool_option ?preprocess osig =
584 declare_option ?preprocess ~kind:BoolKind osig
585
586 let declare_string_option ?preprocess osig =
587 declare_option ?preprocess ~kind:StringKind osig
588
589 let declare_stringopt_option ?preprocess osig =
590 declare_option ?preprocess ~kind:StringOptKind osig