| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module Dyn = Dyn.Make () |
| 12 |
|
| 13 |
type substitutivity = Dispose | Substitute | Keep | Escape | Anticipate |
| 14 |
|
| 15 |
type object_name = Libnames.full_path * Names.KerName.t |
| 16 |
|
| 17 |
type open_filter = |
| 18 |
| Unfiltered |
| 19 |
| Filtered of CString.Pred.t |
| 20 |
|
| 21 |
type category = string |
| 22 |
|
| 23 |
let known_cats = ref CString.Set.empty |
| 24 |
|
| 25 |
let create_category s = |
| 26 |
let cats' = CString.Set.add s !known_cats in |
| 27 |
if !known_cats == cats' then CErrors.anomaly Pp.(str "create_category called twice on " ++ str s); |
| 28 |
known_cats := cats'; |
| 29 |
s |
| 30 |
|
| 31 |
let unfiltered = Unfiltered |
| 32 |
let make_filter ~finite cats = |
| 33 |
if CList.is_empty cats then CErrors.anomaly Pp.(str "Libobject.make_filter got an empty list."); |
| 34 |
let cats = List.fold_left |
| 35 |
(fun cats CAst.{v=cat;loc} -> |
| 36 |
if not (CString.Set.mem cat !known_cats) |
| 37 |
then CErrors.user_err ?loc Pp.(str "Unknown import category " ++ str cat ++ str"."); |
| 38 |
CString.Pred.add cat cats) |
| 39 |
CString.Pred.empty |
| 40 |
cats |
| 41 |
in |
| 42 |
let cats = if finite then cats else CString.Pred.complement cats in |
| 43 |
Filtered cats |
| 44 |
|
| 45 |
let in_filter ~cat f = |
| 46 |
match cat, f with |
| 47 |
| _, Unfiltered -> true |
| 48 |
| None, Filtered f -> not (CString.Pred.is_finite f) |
| 49 |
| Some cat, Filtered f -> CString.Pred.mem cat f |
| 50 |
|
| 51 |
let filtered_open ?cat f filter i o = if in_filter ~cat filter then f i o |
| 52 |
|
| 53 |
let simple_open ?cat f filter i o = if in_filter ~cat filter && Int.equal i 1 then f o |
| 54 |
|
| 55 |
let filter_eq f1 f2 = match f1, f2 with |
| 56 |
| Unfiltered, Unfiltered -> true |
| 57 |
| Unfiltered, _ | _, Unfiltered -> false |
| 58 |
| Filtered f1, Filtered f2 -> CString.Pred.equal f1 f2 |
| 59 |
|
| 60 |
let filter_and f1 f2 = match f1, f2 with |
| 61 |
| Unfiltered, f | f, Unfiltered -> Some f |
| 62 |
| Filtered f1, Filtered f2 -> |
| 63 |
let f = CString.Pred.inter f1 f2 in |
| 64 |
if CString.Pred.is_empty f then None |
| 65 |
else Some (Filtered f) |
| 66 |
|
| 67 |
let filter_or f1 f2 = match f1, f2 with |
| 68 |
| Unfiltered, f | f, Unfiltered -> Unfiltered |
| 69 |
| Filtered f1, Filtered f2 -> Filtered (CString.Pred.union f1 f2) |
| 70 |
|
| 71 |
type ('a,'b,'discharged) object_declaration = { |
| 72 |
object_name : string; |
| 73 |
object_stage : Summary.Stage.t; |
| 74 |
cache_function : 'b -> unit; |
| 75 |
load_function : int -> 'b -> unit; |
| 76 |
open_function : open_filter -> int -> 'b -> unit; |
| 77 |
classify_function : 'a -> substitutivity; |
| 78 |
subst_function : Mod_subst.substitution * 'a -> 'a; |
| 79 |
discharge_function : 'a -> 'discharged option; |
| 80 |
rebuild_function : 'discharged -> 'a; |
| 81 |
} |
| 82 |
|
| 83 |
let default_object ?(stage=Summary.Stage.Interp) s = { |
| 84 |
object_name = s; |
| 85 |
object_stage = stage; |
| 86 |
cache_function = (fun _ -> ()); |
| 87 |
load_function = (fun _ _ -> ()); |
| 88 |
open_function = (fun _ _ _ -> ()); |
| 89 |
subst_function = (fun _ -> |
| 90 |
CErrors.anomaly Pp.(str "The object " ++ str s |
| 91 |
++ str " does not know how to substitute!")); |
| 92 |
classify_function = (fun _ -> CErrors.anomaly Pp.(str "no classify function for " ++ str s)); |
| 93 |
discharge_function = (fun _ -> None); |
| 94 |
rebuild_function = (fun x -> x); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
let ident_subst_function (_,a) = a |
| 110 |
|
| 111 |
|
| 112 |
type obj = Dyn.t |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
module ExportObj = struct |
| 125 |
type t = { mpl : (open_filter * Names.ModPath.t) list } [@@unboxed] |
| 126 |
end |
| 127 |
|
| 128 |
type ('subs, 'alg, 'keep, 'escape) object_view = |
| 129 |
| ModuleObject of Names.Id.t * 'subs |
| 130 |
| ModuleTypeObject of Names.Id.t * 'subs |
| 131 |
| IncludeObject of 'alg |
| 132 |
| KeepObject of Names.Id.t * 'keep |
| 133 |
| EscapeObject of Names.Id.t * 'escape |
| 134 |
| ExportObject of ExportObj.t |
| 135 |
| AtomicObject of obj |
| 136 |
|
| 137 |
type t = (substitutive_objects, algebraic_objects, keep_objects, escape_objects) object_view |
| 138 |
|
| 139 |
and algebraic_objects = |
| 140 |
| Objs of t list |
| 141 |
| Ref of Names.ModPath.t * Mod_subst.substitution |
| 142 |
|
| 143 |
and substitutive_objects = Names.MBId.t list * algebraic_objects |
| 144 |
|
| 145 |
and keep_objects = { keep_objects : t list } |
| 146 |
|
| 147 |
and escape_objects = { escape_objects : t list } |
| 148 |
|
| 149 |
type object_prefix = { |
| 150 |
obj_path : Libnames.full_path; |
| 151 |
obj_mp : Names.ModPath.t; |
| 152 |
} |
| 153 |
|
| 154 |
let eq_object_prefix op1 op2 = |
| 155 |
Libnames.eq_full_path op1.obj_path op2.obj_path && |
| 156 |
Names.ModPath.equal op1.obj_mp op2.obj_mp |
| 157 |
|
| 158 |
type 'a stored_decl = O : ('a, object_prefix * 'a, 'd) object_declaration -> 'a stored_decl |
| 159 |
|
| 160 |
module DynMap = Dyn.Map (struct type 'a t = 'a stored_decl end) |
| 161 |
|
| 162 |
let cache_tab = ref DynMap.empty |
| 163 |
|
| 164 |
let declare_object_gen odecl = |
| 165 |
let na = odecl.object_name in |
| 166 |
let tag = Dyn.create na in |
| 167 |
let () = cache_tab := DynMap.add tag (O odecl) !cache_tab in |
| 168 |
tag |
| 169 |
|
| 170 |
let make_oname { obj_path; obj_mp } id = |
| 171 |
Libnames.add_path_suffix obj_path id, Names.KerName.make obj_mp id |
| 172 |
|
| 173 |
let declare_named_object_full odecl = |
| 174 |
let odecl = |
| 175 |
let oname = make_oname in |
| 176 |
{ object_name = odecl.object_name; |
| 177 |
object_stage = odecl.object_stage; |
| 178 |
cache_function = (fun (p, (id, o)) -> odecl.cache_function (oname p id, o)); |
| 179 |
load_function = (fun i (p, (id, o)) -> odecl.load_function i (oname p id, o)); |
| 180 |
open_function = (fun f i (p, (id, o)) -> odecl.open_function f i (oname p id, o)); |
| 181 |
classify_function = (fun (id, o) -> odecl.classify_function o); |
| 182 |
subst_function = (fun (subst, (id, o)) -> id, odecl.subst_function (subst, o)); |
| 183 |
discharge_function = (fun (id, o) -> Option.map (fun x -> id, x) (odecl.discharge_function o)); |
| 184 |
rebuild_function = Util.on_snd odecl.rebuild_function; |
| 185 |
} |
| 186 |
in |
| 187 |
declare_object_gen odecl |
| 188 |
|
| 189 |
let declare_named_object odecl = |
| 190 |
let tag = declare_named_object_full odecl in |
| 191 |
let infun id v = Dyn.Dyn (tag, (id, v)) in |
| 192 |
infun |
| 193 |
|
| 194 |
let declare_named_object_gen odecl = |
| 195 |
let tag = declare_object_gen odecl in |
| 196 |
let infun v = Dyn.Dyn (tag, v) in |
| 197 |
infun |
| 198 |
|
| 199 |
let declare_object_full odecl = |
| 200 |
let odecl = |
| 201 |
{ odecl with |
| 202 |
cache_function = (fun (_,o) -> odecl.cache_function o); |
| 203 |
load_function = (fun i (_,o) -> odecl.load_function i o); |
| 204 |
open_function = (fun f i (_,o) -> odecl.open_function f i o); |
| 205 |
} |
| 206 |
in |
| 207 |
declare_object_gen odecl |
| 208 |
|
| 209 |
let declare_object odecl = |
| 210 |
let tag = declare_object_full odecl in |
| 211 |
let infun v = Dyn.Dyn (tag, v) in |
| 212 |
infun |
| 213 |
|
| 214 |
let cache_object (sp, Dyn.Dyn (tag, v)) = |
| 215 |
let O decl = DynMap.find tag !cache_tab in |
| 216 |
decl.cache_function (sp, v) |
| 217 |
|
| 218 |
let load_object i (sp, Dyn.Dyn (tag, v)) = |
| 219 |
let O decl = DynMap.find tag !cache_tab in |
| 220 |
decl.load_function i (sp, v) |
| 221 |
|
| 222 |
let open_object f i (sp, Dyn.Dyn (tag, v)) = |
| 223 |
let O decl = DynMap.find tag !cache_tab in |
| 224 |
decl.open_function f i (sp, v) |
| 225 |
|
| 226 |
let subst_object (subs, Dyn.Dyn (tag, v)) = |
| 227 |
let O decl = DynMap.find tag !cache_tab in |
| 228 |
Dyn.Dyn (tag, decl.subst_function (subs, v)) |
| 229 |
|
| 230 |
let classify_object (Dyn.Dyn (tag, v)) = |
| 231 |
let O decl = DynMap.find tag !cache_tab in |
| 232 |
decl.classify_function v |
| 233 |
|
| 234 |
type discharged_obj = Discharged : 'a Dyn.tag * 'd * ('d -> 'a) -> discharged_obj |
| 235 |
|
| 236 |
let discharge_object (Dyn.Dyn (tag, v)) = |
| 237 |
let O decl = DynMap.find tag !cache_tab in |
| 238 |
match decl.discharge_function v with |
| 239 |
| None -> None |
| 240 |
| Some v -> Some (Discharged (tag, v, decl.rebuild_function)) |
| 241 |
|
| 242 |
let rebuild_object (Discharged (tag, v, rebuild)) = |
| 243 |
Dyn.Dyn (tag, rebuild v) |
| 244 |
|
| 245 |
let object_stage (Dyn.Dyn (tag, v)) = |
| 246 |
let O decl = DynMap.find tag !cache_tab in |
| 247 |
decl.object_stage |
| 248 |
|
| 249 |
let object_name (Dyn.Dyn (tag, v)) = |
| 250 |
let O decl = DynMap.find tag !cache_tab in |
| 251 |
decl.object_name |
| 252 |
|
| 253 |
let dump = Dyn.dump |
| 254 |
|
| 255 |
let local_object_nodischarge ?stage s ~cache = |
| 256 |
{ (default_object ?stage s) with |
| 257 |
cache_function = cache; |
| 258 |
classify_function = (fun _ -> Dispose); |
| 259 |
} |
| 260 |
|
| 261 |
let local_object ?stage s ~cache ~discharge = |
| 262 |
{ (local_object_nodischarge ?stage s ~cache) with |
| 263 |
discharge_function = discharge; |
| 264 |
} |
| 265 |
|
| 266 |
let global_object_nodischarge ?cat ?stage s ~cache ~subst = |
| 267 |
{ (default_object ?stage s) with |
| 268 |
cache_function = cache; |
| 269 |
open_function = simple_open ?cat cache; |
| 270 |
subst_function = (match subst with |
| 271 |
| None -> fun _ -> |
| 272 |
CErrors.anomaly Pp.(str "The object " ++ str s |
| 273 |
++ str " does not know how to substitute!") |
| 274 |
| Some subst -> subst; |
| 275 |
); |
| 276 |
classify_function = |
| 277 |
if Option.has_some subst then (fun _ -> Substitute) else (fun _ -> Keep); |
| 278 |
} |
| 279 |
|
| 280 |
let global_object ?cat ?stage s ~cache ~subst ~discharge = |
| 281 |
{ (global_object_nodischarge ?cat s ~cache ~subst) with |
| 282 |
discharge_function = discharge } |
| 283 |
|
| 284 |
let superglobal_object_nodischarge ?stage s ~cache ~subst = |
| 285 |
{ (default_object ?stage s) with |
| 286 |
load_function = (fun _ x -> cache x); |
| 287 |
cache_function = cache; |
| 288 |
subst_function = (match subst with |
| 289 |
| None -> fun _ -> |
| 290 |
CErrors.anomaly Pp.(str "The object " ++ str s |
| 291 |
++ str " does not know how to substitute!") |
| 292 |
| Some subst -> subst; |
| 293 |
); |
| 294 |
classify_function = |
| 295 |
if Option.has_some subst then (fun _ -> Substitute) else (fun _ -> Keep); |
| 296 |
} |
| 297 |
|
| 298 |
let superglobal_object ?stage s ~cache ~subst ~discharge = |
| 299 |
{ (superglobal_object_nodischarge ?stage s ~cache ~subst) with |
| 300 |
discharge_function = discharge } |
| 301 |
|
| 302 |
type locality = Local | Export | SuperGlobal |
| 303 |
|
| 304 |
let object_with_locality ?stage ?cat s ~cache ~subst ~discharge = |
| 305 |
{ (default_object ?stage s) with |
| 306 |
cache_function = (fun (_,v) -> cache v); |
| 307 |
load_function = (fun _ (locality,v) -> match locality with |
| 308 |
| Local -> assert false |
| 309 |
| Export -> () |
| 310 |
| SuperGlobal -> cache v); |
| 311 |
open_function = simple_open ?cat (fun (locality,v) -> match locality with |
| 312 |
| Local -> assert false |
| 313 |
| Export -> cache v |
| 314 |
| SuperGlobal -> ()); |
| 315 |
subst_function = (match subst with |
| 316 |
| None -> fun _ -> assert false |
| 317 |
| Some subst -> fun (s,(locality,v)) -> locality, subst (s,v); |
| 318 |
); |
| 319 |
classify_function = (fun (locality, _) -> match locality with |
| 320 |
| Local -> Dispose |
| 321 |
| Export | SuperGlobal -> if Option.has_some subst then Substitute else Keep); |
| 322 |
discharge_function = |
| 323 |
(fun (locality,v) -> match locality with |
| 324 |
| Local -> None |
| 325 |
| Export | SuperGlobal -> |
| 326 |
let v = discharge v in |
| 327 |
Some (locality, v)); |
| 328 |
} |