| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module Dyn = Dyn.Make () |
| 12 |
|
| 13 |
module Stage = struct |
| 14 |
|
| 15 |
type t = Synterp | Interp |
| 16 |
|
| 17 |
let equal x y = |
| 18 |
match x, y with |
| 19 |
| Synterp, Synterp -> true |
| 20 |
| Synterp, Interp -> false |
| 21 |
| Interp, Interp -> true |
| 22 |
| Interp, Synterp -> false |
| 23 |
|
| 24 |
end |
| 25 |
|
| 26 |
type 'a summary_declaration = { |
| 27 |
stage : Stage.t; |
| 28 |
freeze_function : unit -> 'a; |
| 29 |
unfreeze_function : 'a -> unit; |
| 30 |
init_function : unit -> unit } |
| 31 |
|
| 32 |
module Decl = struct type 'a t = 'a summary_declaration end |
| 33 |
module DynMap = Dyn.Map(Decl) |
| 34 |
|
| 35 |
module MarshMap = Dyn.Map(struct type 'a t = 'a -> 'a end) |
| 36 |
|
| 37 |
type ml_modules = string list |
| 38 |
|
| 39 |
let sum_mod : ml_modules summary_declaration option ref = ref None |
| 40 |
let sum_map_synterp = ref DynMap.empty |
| 41 |
let sum_map_interp = ref DynMap.empty |
| 42 |
let sum_marsh_synterp = ref MarshMap.empty |
| 43 |
let sum_marsh_interp = ref MarshMap.empty |
| 44 |
|
| 45 |
let mangle id = id ^ "-SUMMARY" |
| 46 |
|
| 47 |
let declare_ml_modules_summary decl = |
| 48 |
sum_mod := Some decl |
| 49 |
|
| 50 |
let check_name sumname = match Dyn.name sumname with |
| 51 |
| None -> () |
| 52 |
| Some (Dyn.Any t) -> |
| 53 |
CErrors.anomaly ~label:"Summary.declare_summary" |
| 54 |
Pp.(str "Colliding summary names: " ++ str sumname |
| 55 |
++ str " vs. " ++ str (Dyn.repr t) ++ str ".") |
| 56 |
|
| 57 |
let declare_summary_tag sumname ?make_marshallable decl = |
| 58 |
let () = check_name (mangle sumname) in |
| 59 |
let tag = Dyn.create (mangle sumname) in |
| 60 |
let sum_map, marsh_map = match decl.stage with |
| 61 |
| Synterp -> sum_map_synterp, sum_marsh_synterp |
| 62 |
| Interp -> sum_map_interp, sum_marsh_interp |
| 63 |
in |
| 64 |
let () = sum_map := DynMap.add tag decl !sum_map in |
| 65 |
let () = make_marshallable |> Option.iter (fun f -> |
| 66 |
marsh_map := MarshMap.add tag f !marsh_map) |
| 67 |
in |
| 68 |
tag |
| 69 |
|
| 70 |
let declare_summary sumname ?make_marshallable decl = |
| 71 |
ignore(declare_summary_tag sumname ?make_marshallable decl) |
| 72 |
|
| 73 |
module ID = struct type 'a t = 'a end |
| 74 |
module Frozen = Dyn.Map(ID) |
| 75 |
module HMap = Dyn.HMap(Decl)(ID) |
| 76 |
|
| 77 |
|
| 78 |
module type FrozenStage = sig |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type frozen |
| 84 |
|
| 85 |
val empty_frozen : frozen |
| 86 |
val freeze_summaries : unit -> frozen |
| 87 |
val make_marshallable : frozen -> frozen |
| 88 |
val unfreeze_summaries : ?partial:bool -> frozen -> unit |
| 89 |
val init_summaries : unit -> unit |
| 90 |
val project_from_summary : frozen -> 'a Dyn.tag -> 'a |
| 91 |
|
| 92 |
end |
| 93 |
|
| 94 |
let freeze_summaries sum_map = |
| 95 |
let map = { HMap.map = fun tag decl -> decl.freeze_function () } in |
| 96 |
HMap.map map sum_map |
| 97 |
|
| 98 |
let make_marshallable marsh_map summaries = |
| 99 |
let map = { Frozen.map = fun tag v -> match MarshMap.find tag marsh_map with |
| 100 |
| exception Not_found -> v |
| 101 |
| f -> f v } |
| 102 |
in |
| 103 |
Frozen.map map summaries |
| 104 |
|
| 105 |
let warn_summary_out_of_scope = |
| 106 |
CWarnings.create ~name:"summary-out-of-scope" ~default:Disabled Pp.(fun name -> |
| 107 |
str "A Rocq plugin was loaded inside a local scope (such as a Section)." ++ spc() ++ |
| 108 |
str "It is recommended to load plugins at the start of the file." ++ spc() ++ |
| 109 |
str "Summary entry: " ++ str name) |
| 110 |
|
| 111 |
let unfreeze_summaries ?(partial=false) sum_map summaries = |
| 112 |
|
| 113 |
let ufz (DynMap.Any (name, decl)) = |
| 114 |
try decl.unfreeze_function Frozen.(find name summaries) |
| 115 |
with Not_found -> |
| 116 |
if not partial then begin |
| 117 |
warn_summary_out_of_scope (Dyn.repr name); |
| 118 |
decl.init_function () |
| 119 |
end |
| 120 |
in |
| 121 |
DynMap.iter ufz sum_map |
| 122 |
|
| 123 |
let init_summaries sum_map = |
| 124 |
DynMap.iter (fun (DynMap.Any (_, decl)) -> decl.init_function ()) sum_map |
| 125 |
|
| 126 |
module Synterp = struct |
| 127 |
|
| 128 |
type frozen = |
| 129 |
{ |
| 130 |
summaries : Frozen.t; |
| 131 |
|
| 132 |
ml_module : ml_modules option; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
let empty_frozen = { summaries = Frozen.empty; ml_module = None } |
| 137 |
|
| 138 |
let freeze_summaries () = |
| 139 |
let summaries = freeze_summaries !sum_map_synterp in |
| 140 |
{ summaries; ml_module = Option.map (fun decl -> decl.freeze_function ()) !sum_mod } |
| 141 |
|
| 142 |
let make_marshallable { summaries; ml_module } = |
| 143 |
{ summaries = make_marshallable !sum_marsh_synterp summaries; |
| 144 |
ml_module } |
| 145 |
|
| 146 |
let unfreeze_summaries ?(partial=false) { summaries; ml_module } = |
| 147 |
|
| 148 |
|
| 149 |
begin match !sum_mod with |
| 150 |
| None -> CErrors.anomaly Pp.(str "Undeclared ML-MODULES summary.") |
| 151 |
| Some decl -> Option.iter decl.unfreeze_function ml_module |
| 152 |
end; |
| 153 |
unfreeze_summaries ~partial !sum_map_synterp summaries |
| 154 |
|
| 155 |
let init_summaries () = |
| 156 |
init_summaries !sum_map_synterp |
| 157 |
|
| 158 |
|
| 159 |
let project_from_summary { summaries; _ } tag = |
| 160 |
Frozen.find tag summaries |
| 161 |
|
| 162 |
end |
| 163 |
|
| 164 |
module Interp = struct |
| 165 |
|
| 166 |
type frozen = Frozen.t |
| 167 |
|
| 168 |
let empty_frozen = Frozen.empty |
| 169 |
|
| 170 |
let freeze_summaries () = |
| 171 |
freeze_summaries !sum_map_interp |
| 172 |
|
| 173 |
let make_marshallable summaries = make_marshallable !sum_marsh_interp summaries |
| 174 |
|
| 175 |
let unfreeze_summaries ?(partial=false) summaries = |
| 176 |
unfreeze_summaries ~partial !sum_map_interp summaries |
| 177 |
|
| 178 |
let init_summaries () = |
| 179 |
init_summaries !sum_map_interp |
| 180 |
|
| 181 |
|
| 182 |
let project_from_summary summaries tag = |
| 183 |
Frozen.find tag summaries |
| 184 |
|
| 185 |
let modify_summary summaries tag v = |
| 186 |
let () = assert (Frozen.mem tag summaries) in |
| 187 |
Frozen.add tag v summaries |
| 188 |
|
| 189 |
let remove_from_summary summaries tag = |
| 190 |
let () = assert (Frozen.mem tag summaries) in |
| 191 |
Frozen.remove tag summaries |
| 192 |
|
| 193 |
end |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
let nop () = () |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
let ref_tag ?(stage=Stage.Interp) ~name x = |
| 203 |
let r = ref x in |
| 204 |
let tag = declare_summary_tag name |
| 205 |
{ stage; |
| 206 |
freeze_function = (fun () -> !r); |
| 207 |
unfreeze_function = ((:=) r); |
| 208 |
init_function = (fun () -> r := x) } in |
| 209 |
r, tag |
| 210 |
|
| 211 |
let ref ?(stage=Stage.Interp) ?(local=false) ~name x = |
| 212 |
if not local then fst @@ ref_tag ~stage ~name x |
| 213 |
else |
| 214 |
let r = ref x in |
| 215 |
let () = declare_summary name |
| 216 |
~make_marshallable:(fun _ -> None) |
| 217 |
{ stage; |
| 218 |
freeze_function = (fun () -> Some !r); |
| 219 |
unfreeze_function = (function Some v -> r := v | None -> r := x); |
| 220 |
init_function = (fun () -> r := x); } |
| 221 |
in |
| 222 |
r |
| 223 |
|
| 224 |
|
| 225 |
module type OBSERVABLE = |
| 226 |
sig |
| 227 |
type token |
| 228 |
type value |
| 229 |
|
| 230 |
val register : name:string -> ?override:bool -> value -> token |
| 231 |
|
| 232 |
val activate : token -> unit |
| 233 |
val deactivate : token -> unit |
| 234 |
|
| 235 |
val is_active : token -> bool |
| 236 |
end |
| 237 |
|
| 238 |
module type OBSERVABLE_USER = |
| 239 |
sig |
| 240 |
include OBSERVABLE |
| 241 |
|
| 242 |
val all_active : unit -> (string * value) list |
| 243 |
end |
| 244 |
|
| 245 |
module MakeObservable |
| 246 |
(Obs : sig |
| 247 |
type value |
| 248 |
val stage : Stage.t |
| 249 |
val local : bool |
| 250 |
val name : string |
| 251 |
end) : OBSERVABLE_USER with type value = Obs.value = |
| 252 |
struct |
| 253 |
type token = string |
| 254 |
type value = Obs.value |
| 255 |
|
| 256 |
let observers = Stdlib.ref CString.Map.empty |
| 257 |
let active_observers : token list ref = ref ~stage:Obs.stage ~local:Obs.local ~name:Obs.name [] |
| 258 |
|
| 259 |
let register ~name ?(override=false) value : token = |
| 260 |
if not override && CString.Map.mem name !observers then |
| 261 |
CErrors.anomaly Pp.(str Obs.name ++ str " observer " ++ |
| 262 |
str name ++ str " already exists") |
| 263 |
else |
| 264 |
observers := CString.Map.add name value !observers ; |
| 265 |
name |
| 266 |
|
| 267 |
let remove name = Util.List.remove String.equal name !active_observers |
| 268 |
|
| 269 |
let activate name : unit = |
| 270 |
assert (CString.Map.mem name !observers); |
| 271 |
active_observers := name :: remove name; |
| 272 |
() |
| 273 |
|
| 274 |
let deactivate name : unit = |
| 275 |
active_observers := remove name; |
| 276 |
() |
| 277 |
|
| 278 |
let is_active tkn = List.mem tkn !active_observers |
| 279 |
|
| 280 |
let all_active () : (token * value) list = |
| 281 |
List.map (fun k -> k, CString.Map.get k !observers) !active_observers |
| 282 |
end |
| 283 |
|
| 284 |
|
| 285 |
let dump = Dyn.dump |