Debian Codegraph @

vernac/declaremods.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 open Pp
12 open CErrors
13 open Util
14 open Names
15 open Declarations
16 open Mod_declarations
17 open Entries
18 open Libnames
19 open Libobject
20 open Mod_subst
21
22 (** {6 Inlining levels} *)
23
24 (** Rigid / flexible module signature *)
25
26 type 'a module_signature =
27 | Enforce of 'a (** ... : T *)
28 | Check of 'a list (** ... <: T1 <: T2, possibly empty *)
29
30 (** Which module inline annotations should we honor,
31 either None or the ones whose level is less or equal
32 to the given integer *)
33
34 type inline =
35 | NoInline
36 | DefaultInline
37 | InlineAt of int
38
39 let default_inline_level = 100
40
41 let { Goptions.get = default_inline_level } =
42 Goptions.declare_int_option_and_ref ~key:["Inline";"Level"] ~value:default_inline_level ()
43
44 let default_inline_level () = Some (default_inline_level())
45
46 let inl2intopt = function
47 | NoInline -> None
48 | InlineAt i -> Some i
49 | DefaultInline -> default_inline_level ()
50
51 (** These functions register the visibility of the module and iterates
52 through its components. They are called by plenty of module functions *)
53
54 let consistency_checks exists dir =
55 if exists then
56 let _ =
57 try Nametab.locate_module (qualid_of_path dir)
58 with Not_found ->
59 user_err
60 (pr_path dir ++ str " should already exist!")
61 in
62 ()
63 else
64 if Nametab.exists_module dir then
65 user_err
66 (pr_path dir ++ str " already exists.")
67
68 let rec get_module_path = function
69 | MEident mp -> mp
70 | MEwith (me,_) -> get_module_path me
71 | MEapply (me,_) -> get_module_path me
72
73 let type_of_mod mp env = function
74 | true -> mod_type (Environ.lookup_module mp env)
75 | false -> mod_type (Environ.lookup_modtype mp env)
76
77 (** {6 Name management}
78
79 Auxiliary functions to transform full_path and kernel_name given
80 by Lib into ModPath.t and DirPath.t needed for modules
81 *)
82
83 let mp_of_kn kn =
84 let mp,l = KerName.repr kn in
85 MPdot (mp,l)
86
87 let pop_path sp =
88 path_pop_suffix sp, basename sp
89
90 let path_of_file dp =
91 match DirPath.repr dp with
92 | x :: dp -> make_path (DirPath.make dp) x
93 | [] -> CErrors.anomaly Pp.(str "Cannot start library with empty dirpath")
94
95 (* Avoid generating a KeepObject for nothing *)
96 let keep_objects id keep = match keep.keep_objects with
97 | [] -> []
98 | _ :: _ -> [KeepObject (id, keep)]
99
100 let escape_objects id escape = match escape.escape_objects with
101 | [] -> []
102 | _ :: _ -> [EscapeObject (id, escape)]
103
104 (** The [ModActions] abstraction represent operations on modules
105 that are specific to a given stage. Two instances are defined below,
106 for Synterp and Interp. *)
107 module type ModActions = sig
108
109 type typexpr
110 type env
111
112 val stage : Summary.Stage.t
113 val substobjs_table_name : string
114 val modobjs_table_name : string
115
116 val enter_module : ModPath.t -> full_path -> int -> unit
117
118 val enter_modtype : ModPath.t -> full_path -> int -> unit
119
120 val open_module : open_filter -> ModPath.t -> full_path -> int -> unit
121
122 module Lib : Lib.StagedLibS
123
124 (** Create the substitution corresponding to some functor applications *)
125
126 val compute_subst : is_mod:bool -> env -> MBId.t list -> ModPath.t -> ModPath.t list -> Entries.inline -> MBId.t list * substitution
127
128 end
129
130 module SynterpActions : ModActions with
131 type env = unit with
132 type typexpr = Constrexpr.universe_decl_expr option * Constrexpr.constr_expr =
133 struct
134 type typexpr = Constrexpr.universe_decl_expr option * Constrexpr.constr_expr
135 type env = unit
136 let stage = Summary.Stage.Synterp
137 let substobjs_table_name = "MODULE-SYNTAX-SUBSTOBJS"
138 let modobjs_table_name = "MODULE-SYNTAX-OBJS"
139
140 let enter_module obj_mp obj_path i =
141 consistency_checks false obj_path;
142 Nametab.push_module (Until i) obj_path obj_mp
143
144 let enter_modtype mp sp i =
145 if Nametab.exists_modtype sp then
146 anomaly (pr_path sp ++ str " already exists.");
147 Nametab.push_modtype (Nametab.Until i) sp mp
148
149 let open_module f obj_mp obj_path i =
150 consistency_checks true obj_path;
151 if in_filter ~cat:None f then Nametab.push_module (Nametab.Exactly i) obj_path obj_mp
152
153 module Lib = Lib.Synterp
154
155 let rec compute_subst () mbids mp_l inl =
156 match mbids,mp_l with
157 | _,[] -> mbids,empty_subst
158 | [],r -> user_err Pp.(str "Application of a functor with too few arguments.")
159 | mbid::mbids,mp::mp_l ->
160 let mbid_left,subst = compute_subst () mbids mp_l inl in
161 mbid_left, join (map_mbid mbid mp (empty_delta_resolver mp)) subst
162
163 let compute_subst ~is_mod () mbids mp1 mp_l inl =
164 compute_subst () mbids mp_l inl
165
166 end
167
168 module InterpActions : ModActions
169 with type env = Environ.env
170 with type typexpr = Constr.t * UVars.AbstractContext.t option =
171 struct
172 type typexpr = Constr.t * UVars.AbstractContext.t option
173 type env = Environ.env
174 let stage = Summary.Stage.Interp
175 let substobjs_table_name = "MODULE-SUBSTOBJS"
176 let modobjs_table_name = "MODULE-OBJS"
177
178 (** {6 Current module type information}
179
180 This information is stored by each [start_modtype] for use
181 in a later [end_modtype]. *)
182
183 let enter_module obj_mp obj_path i = ()
184 let enter_modtype mp sp i = ()
185
186 let open_module f obj_mp obj_path i = ()
187
188 module Lib = Lib.Interp
189
190 let rec compute_subst env mbids sign mp_l inl =
191 match mbids,mp_l with
192 | _,[] -> mbids,empty_subst
193 | [],r -> user_err Pp.(str "Application of a functor with too few arguments.")
194 | mbid::mbids,mp::mp_l ->
195 let farg_id, farg_b, fbody_b = Modops.destr_functor sign in
196 let mb = Environ.lookup_module mp env in
197 let mbid_left,subst = compute_subst env mbids fbody_b mp_l inl in
198 let resolver = match mod_global_delta mb with
199 | None -> empty_delta_resolver mp
200 | Some delta ->
201 Modops.inline_delta_resolver env inl mp farg_id farg_b delta
202 in
203 mbid_left,join (map_mbid mbid mp resolver) subst
204
205 let compute_subst ~is_mod env mbids mp1 mp_l inl =
206 let typ = type_of_mod mp1 env is_mod in
207 compute_subst env mbids typ mp_l inl
208
209 end
210
211 type exp_substituted_object = (substitutive_objects, exp_algebraic_objects, Empty.t, Empty.t) object_view
212
213 and exp_algebraic_objects = { exp_algebraic_objects : exp_substituted_object list }
214
215 (* and exp_substitutive_objects = Names.MBId.t list * exp_algebraic_objects *)
216
217 type module_objects =
218 { module_prefix : object_prefix;
219 module_substituted_objects : exp_substituted_object list;
220 module_keep_objects : keep_objects;
221 module_escape_objects : escape_objects;
222 }
223
224 (** The [StagedModS] abstraction describes module operations at a given stage. *)
225 module type StagedModS = sig
226
227 type typexpr
228 type env
229
230 val get_module_sobjs : bool -> env -> Entries.inline -> typexpr module_alg_expr -> substitutive_objects
231
232 val load_keep : int -> full_path -> ModPath.t -> keep_objects -> unit
233 val load_escape : int -> full_path -> ModPath.t -> escape_objects -> unit
234 val load_module : int -> full_path -> ModPath.t -> substitutive_objects -> unit
235 val import_modules : export:Lib.export_flag -> (open_filter * ModPath.t) list -> unit
236
237 val add_leaf : Libobject.t -> unit
238 val add_leaves : Libobject.t list -> unit
239
240 val expand_aobjs : Libobject.algebraic_objects -> Libobject.t list
241
242 val get_applications : typexpr module_alg_expr -> ModPath.t * ModPath.t list
243 val debug_print_modtab : unit -> Pp.t
244
245 module ModObjs : sig val all : unit -> module_objects ModPath.Map.t end
246
247 val close_section : unit -> unit
248
249 end
250
251 (** Some utilities about substitutive objects :
252 substitution, expansion *)
253
254 let sobjs_no_functor (mbids,_) = List.is_empty mbids
255
256 let subst_filtered sub (f,mp as x) =
257 let mp' = subst_mp sub mp in
258 if mp == mp' then x
259 else f, mp'
260
261 let rec subst_aobjs sub = function
262 | Objs o as objs ->
263 let o' = subst_objects sub o in
264 if o == o' then objs else Objs o'
265 | Ref (mp, sub0) as r ->
266 let sub0' = join sub0 sub in
267 if sub0' == sub0 then r else Ref (mp, sub0')
268
269 and subst_sobjs sub (mbids,aobjs as sobjs) =
270 let aobjs' = subst_aobjs sub aobjs in
271 if aobjs' == aobjs then sobjs else (mbids, aobjs')
272
273 and subst_objects subst seg =
274 let subst_one node =
275 match node with
276 | AtomicObject obj ->
277 let obj' = Libobject.subst_object (subst,obj) in
278 if obj' == obj then node else AtomicObject obj'
279 | ModuleObject (id, sobjs) ->
280 let sobjs' = subst_sobjs subst sobjs in
281 if sobjs' == sobjs then node else ModuleObject (id, sobjs')
282 | ModuleTypeObject (id, sobjs) ->
283 let sobjs' = subst_sobjs subst sobjs in
284 if sobjs' == sobjs then node else ModuleTypeObject (id, sobjs')
285 | IncludeObject aobjs ->
286 let aobjs' = subst_aobjs subst aobjs in
287 if aobjs' == aobjs then node else IncludeObject aobjs'
288 | ExportObject { mpl } ->
289 let mpl' = List.Smart.map (subst_filtered subst) mpl in
290 if mpl'==mpl then node else ExportObject { mpl = mpl' }
291 | KeepObject _ | EscapeObject _ -> assert false
292 in
293 List.Smart.map subst_one seg
294
295 (** The [StagedMod] abstraction factors out the code dealing with modules
296 that is common to all stages. *)
297 module StagedMod(Actions : ModActions) = struct
298
299 type typexpr = Actions.typexpr
300 type env = Actions.env
301
302 (** ModSubstObjs : a cache of module substitutive objects
303
304 This table is common to modules and module types.
305 - For a Module M:=N, the objects of N will be reloaded
306 with M after substitution.
307 - For a Module M:SIG:=..., the module M gets its objects from SIG
308
309 Invariants:
310 - A alias (i.e. a module path inside a Ref constructor) should
311 never lead to another alias, but rather to a concrete Objs
312 constructor.
313
314 We will plug later a handler dealing with missing entries in the
315 cache. Such missing entries may come from inner parts of module
316 types, which aren't registered by the standard libobject machinery.
317 *)
318
319 module ModSubstObjs :
320 sig
321 val set : ModPath.t -> substitutive_objects -> unit
322 val get : ModPath.t -> substitutive_objects
323 val set_missing_handler : (ModPath.t -> substitutive_objects) -> unit
324 end =
325 struct
326 let table =
327 Summary.ref ~stage:Actions.stage (ModPath.Map.empty : substitutive_objects ModPath.Map.t)
328 ~name:Actions.substobjs_table_name
329 let missing_handler = ref (fun mp -> assert false)
330 let set_missing_handler f = (missing_handler := f)
331 let set mp objs = (table := ModPath.Map.add mp objs !table)
332 let get mp =
333 try ModPath.Map.find mp !table with Not_found -> !missing_handler mp
334 end
335
336 let expand_aobjs = function
337 | Objs o -> o
338 | Ref (mp, sub) ->
339 match ModSubstObjs.get mp with
340 | (_,Objs o) -> subst_objects sub o
341 | _ -> assert false (* Invariant : any alias points to concrete objs *)
342
343 let expand_sobjs (_,aobjs) = expand_aobjs aobjs
344
345 module Expand =
346 struct
347
348 type exp_object = (substitutive_objects, exp_algebraic_objects, keep_objects, escape_objects) object_view
349
350 let exp_substituted_view (obj : exp_substituted_object) : exp_object = match obj with
351 | (AtomicObject _ | ModuleObject _ | ModuleTypeObject _ | IncludeObject _ | ExportObject _) as o -> o
352 | EscapeObject (_, o) | KeepObject (_, o) -> Empty.abort o
353
354 let keep_view (obj : Libobject.t) : exp_object = match obj with
355 | (AtomicObject _ | KeepObject _) as o -> o
356 | ModuleObject _ | ModuleTypeObject _ | IncludeObject _ | ExportObject _ | EscapeObject _ ->
357 assert false (** keep objects only contain atomic / keep *)
358
359 let escape_view (obj : Libobject.t) : exp_object = match obj with
360 | (AtomicObject _ | EscapeObject _) as o -> o
361 | ModuleObject _ | ModuleTypeObject _ | IncludeObject _ | ExportObject _ | KeepObject _ ->
362 assert false (** escape objects only contain atomic / escape *)
363
364 let rec substituted_object_view (obj : Libobject.t) : exp_substituted_object = match obj with
365 | (AtomicObject _ | ModuleObject _ | ModuleTypeObject _ | ExportObject _) as o -> o
366 | IncludeObject aobjs ->
367 let aobjs = expand_aobjs aobjs in
368 IncludeObject { exp_algebraic_objects = List.map substituted_object_view aobjs }
369 | EscapeObject _ | KeepObject _ ->
370 (* forbidden in substitutive objects *)
371 assert false
372
373 let object_view (obj : Libobject.t) : exp_object = match obj with
374 | (AtomicObject _ | EscapeObject _ | ModuleObject _ | ModuleTypeObject _ | ExportObject _ | KeepObject _) as o -> o
375 | IncludeObject aobjs ->
376 let aobjs = expand_aobjs aobjs in
377 IncludeObject { exp_algebraic_objects = List.map substituted_object_view aobjs }
378
379 end
380
381
382 (** {6 ModObjs : a cache of module objects}
383
384 For each module, we also store a cache of
385 "prefix", "substituted objects", "keep objects".
386 This is used for instance to implement the "Import" command.
387
388 substituted objects :
389 roughly the objects above after the substitution - we need to
390 keep them to call open_object when the module is opened (imported)
391
392 keep objects :
393 The list of non-substitutive objects - as above, for each of
394 them we will call open_object when the module is opened
395
396 (Some) Invariants:
397 * If the module is a functor, it won't appear in this cache.
398
399 * Module objects in substitutive_objects part have empty substituted
400 objects.
401
402 * Modules which where created with Module M:=mexpr or with
403 Module M:SIG. ... End M. have the keep list empty.
404 *)
405
406 module ModObjs :
407 sig
408 val set : ModPath.t -> module_objects -> unit
409 val get : ModPath.t -> module_objects (* may raise Not_found *)
410 val all : unit -> module_objects ModPath.Map.t
411 end =
412 struct
413 let table =
414 Summary.ref ~stage:Actions.stage (ModPath.Map.empty : module_objects ModPath.Map.t)
415 ~name:Actions.modobjs_table_name
416 let set mp objs = (table := ModPath.Map.add mp objs !table)
417 let get mp = ModPath.Map.find mp !table
418 let all () = !table
419 end
420
421 open Expand
422
423 (** {6 Declaration of module substitutive objects} *)
424
425 (** Nota: Interactive modules and module types cannot be recached!
426 This used to be checked here via a flag along the substobjs. *)
427
428 (** {6 Declaration of module type substitutive objects} *)
429
430 (** Nota: Interactive modules and module types cannot be recached!
431 This used to be checked more properly here. *)
432
433 let load_modtype i sp mp sobjs =
434 Actions.enter_modtype mp sp i;
435 ModSubstObjs.set mp sobjs
436
437 (** {6 Declaration of substitutive objects for Include} *)
438
439 let rec load_object : type a. (a -> exp_object) -> int -> object_prefix * a -> unit = fun view i (prefix, obj) ->
440 match view obj with
441 | AtomicObject o -> Libobject.load_object i (prefix, o)
442 | ModuleObject (id,sobjs) ->
443 let sp, kn = Lib.make_oname prefix id in
444 load_module i sp (mp_of_kn kn) sobjs
445 | ModuleTypeObject (id,sobjs) ->
446 let name = Lib.make_oname prefix id in
447 let (sp,kn) = name in
448 load_modtype i sp (mp_of_kn kn) sobjs
449 | IncludeObject aobjs ->
450 load_include i (prefix, aobjs)
451 | ExportObject _ -> ()
452 | KeepObject (id,objs) ->
453 let sp, kn = Lib.make_oname prefix id in
454 load_keep i sp (mp_of_kn kn) objs
455 | EscapeObject (id,objs) ->
456 let sp, kn = Lib.make_oname prefix id in
457 load_escape i sp (mp_of_kn kn) objs
458
459 and load_objects : type a. (a -> exp_object) -> int -> object_prefix -> a list -> unit = fun view i prefix objs ->
460 List.iter (fun obj -> load_object view i (prefix, obj)) objs
461
462 and load_include i (prefix, aobjs) =
463 load_objects exp_substituted_view i prefix aobjs.exp_algebraic_objects
464
465 and load_keep i obj_path obj_mp kobjs =
466 (* Invariant : seg isn't empty *)
467 let prefix = { obj_path ; obj_mp; } in
468 let modobjs =
469 try ModObjs.get obj_mp
470 with Not_found -> assert false (* a substobjs should already be loaded *)
471 in
472 assert (eq_object_prefix modobjs.module_prefix prefix);
473 assert (List.is_empty modobjs.module_keep_objects.keep_objects);
474 ModObjs.set obj_mp { modobjs with module_keep_objects = kobjs };
475 load_objects keep_view (i+1) prefix kobjs.keep_objects
476
477 and load_escape i obj_path obj_mp eobjs =
478 (* Invariant : seg isn't empty *)
479 let prefix = { obj_path ; obj_mp; } in
480 let modobjs =
481 try ModObjs.get obj_mp
482 with Not_found ->
483 (* escape objects can exist even if there is no corresponding real module *)
484 { module_prefix = prefix;
485 module_substituted_objects = [];
486 module_keep_objects = { keep_objects = [] };
487 module_escape_objects = { escape_objects = [] };
488 }
489 in
490 assert (eq_object_prefix modobjs.module_prefix prefix);
491 assert (List.is_empty modobjs.module_escape_objects.escape_objects);
492 ModObjs.set obj_mp { modobjs with module_escape_objects = eobjs };
493 load_objects escape_view (i+1) prefix eobjs.escape_objects
494
495 and load_module i obj_path obj_mp sobjs =
496 let prefix = { obj_path ; obj_mp; } in
497 Actions.enter_module obj_mp obj_path i;
498 ModSubstObjs.set obj_mp sobjs;
499 (* If we're not a functor, let's iter on the internal components *)
500 if sobjs_no_functor sobjs then begin
501 let objs = expand_sobjs sobjs in
502 let objs = List.map substituted_object_view objs in
503 let module_objects =
504 { module_prefix = prefix;
505 module_substituted_objects = objs;
506 module_keep_objects = { keep_objects = [] };
507 module_escape_objects = { escape_objects = [] };
508 }
509 in
510 ModObjs.set obj_mp module_objects;
511 load_objects exp_substituted_view (i+1) prefix objs
512 end
513
514 (** {6 Implementation of Import and Export commands} *)
515
516 let mark_object f obj (exports,acc) =
517 (exports, (f,obj)::acc)
518
519 let rec collect_module (f,mp) acc =
520 try
521 (* May raise Not_found for unknown module and for functors *)
522 let modobjs = ModObjs.get mp in
523 let prefix = modobjs.module_prefix in
524 let acc = collect_objects escape_view f 1 prefix modobjs.module_escape_objects.escape_objects acc in
525 let acc = collect_objects keep_view f 1 prefix modobjs.module_keep_objects.keep_objects acc in
526 collect_objects exp_substituted_view f 1 prefix modobjs.module_substituted_objects acc
527 with Not_found when Actions.stage = Summary.Stage.Synterp ->
528 acc
529
530 and collect_object : type a. (a -> exp_object) -> _ -> _ -> _ -> a -> _ -> _ = fun view f i prefix obj acc ->
531 match view obj with
532 | ExportObject { mpl } -> collect_exports f i mpl acc
533 | (AtomicObject _ | IncludeObject _ | KeepObject _ | EscapeObject _
534 | ModuleObject _ | ModuleTypeObject _) as obj -> mark_object f (prefix,obj) acc
535
536 and collect_objects : type a. (a -> exp_object) -> _ -> _ -> _ -> a list -> _ = fun view f i prefix objs acc ->
537 List.fold_left (fun acc obj -> collect_object view f i prefix obj acc)
538 acc
539 (List.rev objs)
540
541 and collect_export f (f',mp) (exports,objs as acc) =
542 match filter_and f f' with
543 | None -> acc
544 | Some f ->
545 let exports' = ModPath.Map.update mp (function
546 | None -> Some f
547 | Some f0 ->
548 let f' = filter_or f f0 in
549 if filter_eq f' f0 then Some f0 else Some f')
550 exports
551 in
552 (* If the map doesn't change there is nothing new to export. *)
553 if exports == exports' then acc
554 else
555 collect_module (f,mp) (exports', objs)
556
557
558 and collect_exports f i mpl acc =
559 if Int.equal i 1 then
560 List.fold_left (fun acc fmp -> collect_export f fmp acc) acc (List.rev mpl)
561 else acc
562
563 let collect_modules mpl =
564 List.fold_left (fun acc fmp -> collect_module fmp acc) (ModPath.Map.empty, []) (List.rev mpl)
565
566 let open_modtype i ((sp,kn),_) =
567 let mp = mp_of_kn kn in
568 let mp' =
569 try Nametab.locate_modtype (qualid_of_path sp)
570 with Not_found ->
571 anomaly (pr_path sp ++ str " should already exist!");
572 in
573 assert (ModPath.equal mp mp');
574 Nametab.push_modtype (Nametab.Exactly i) sp mp
575
576 let rec open_object : type a. (a -> exp_object) -> _ -> _ -> _ * a -> _ = fun view f i (prefix, obj) ->
577 match view obj with
578 | AtomicObject o -> Libobject.open_object f i (prefix, o)
579 | ModuleObject (id,sobjs) ->
580 let sp, kn = Lib.make_oname prefix id in
581 let mp = mp_of_kn kn in
582 open_module f i sp mp sobjs
583 | ModuleTypeObject (id,sobjs) ->
584 let name = Lib.make_oname prefix id in
585 open_modtype i (name, sobjs)
586 | IncludeObject aobjs ->
587 open_include f i (prefix, aobjs)
588 | ExportObject { mpl } -> open_export f i mpl
589 | KeepObject (id,objs) ->
590 let name = Lib.make_oname prefix id in
591 open_keep f i (name, objs)
592 | EscapeObject (id,objs) ->
593 let name = Lib.make_oname prefix id in
594 open_escape f i (name, objs)
595
596 and open_module f i obj_path obj_mp sobjs =
597 Actions.open_module f obj_mp obj_path i;
598 (* If we're not a functor, let's iter on the internal components *)
599 if sobjs_no_functor sobjs then begin
600 let modobjs = ModObjs.get obj_mp in
601 open_objects exp_substituted_view f (i+1) modobjs.module_prefix modobjs.module_substituted_objects
602 end
603
604 and open_objects : type a. (a -> exp_object) -> _ -> _ -> _ -> a list -> _ = fun view f i prefix objs ->
605 List.iter (fun obj -> open_object view f i (prefix, obj)) objs
606
607 and open_include f i (prefix, aobjs) =
608 open_objects exp_substituted_view f i prefix aobjs.exp_algebraic_objects
609
610 and open_export f i mpl =
611 let _,objs = collect_exports f i mpl (ModPath.Map.empty, []) in
612 List.iter (fun (f,o) -> open_object (fun x -> x) f 1 o) objs
613
614 and open_keep f i ((sp,kn),kobjs) =
615 let obj_mp = mp_of_kn kn in
616 let prefix = { obj_path=sp; obj_mp; } in
617 open_objects keep_view f (i+1) prefix kobjs.keep_objects
618
619 and open_escape f i ((sp,kn),kobjs) =
620 let obj_mp = mp_of_kn kn in
621 let prefix = { obj_path=sp; obj_mp; } in
622 open_objects escape_view f (i+1) prefix kobjs.escape_objects
623
624 let cache_include (prefix, aobjs) =
625 let o = expand_aobjs aobjs in
626 let o = List.map substituted_object_view o in
627 load_objects exp_substituted_view 1 prefix o;
628 open_objects exp_substituted_view unfiltered 1 prefix o
629
630 let cache_object (prefix, obj) =
631 match obj with
632 | AtomicObject o -> Libobject.cache_object (prefix, o)
633 | ModuleObject _ -> load_object object_view 1 (prefix,obj)
634 | ModuleTypeObject _ -> load_object object_view 0 (prefix,obj)
635 | IncludeObject aobjs -> cache_include (prefix, aobjs)
636 | ExportObject { mpl } -> anomaly Pp.(str "Export should not be cached")
637 | KeepObject _ | EscapeObject _ -> anomaly (Pp.str "This module should not be cached!")
638
639 (* Adding operations with containers *)
640
641 let add_leaf_entry =
642 match Actions.stage with
643 | Summary.Stage.Synterp -> Lib.Synterp.add_leaf_entry
644 | Summary.Stage.Interp -> Lib.Interp.add_leaf_entry
645
646 let add_leaf obj =
647 cache_object (Lib.prefix (),obj);
648 add_leaf_entry obj
649
650 let add_leaves objs =
651 let add_obj obj =
652 add_leaf_entry obj;
653 load_object object_view 1 (Lib.prefix (),obj)
654 in
655 List.iter add_obj objs
656
657 let import_modules ~export mpl =
658 let _,objs = collect_modules mpl in
659 List.iter (fun (f,o) -> open_object (fun x -> x) f 1 o) objs;
660 match export with
661 | Lib.Import -> ()
662 | Lib.Export ->
663 let entry = ExportObject { mpl } in
664 add_leaf_entry entry
665
666 (** {6 Handler for missing entries in ModSubstObjs} *)
667
668 (** Since the inner of Module Types are not added by default to
669 the ModSubstObjs table, we compensate this by explicit traversal
670 of Module Types inner objects when needed. Quite a hack... *)
671
672 let mp_id mp id = MPdot (mp, id)
673
674 let rec register_mod_objs mp obj = match obj with
675 | ModuleObject (id,sobjs) -> ModSubstObjs.set (mp_id mp id) sobjs
676 | ModuleTypeObject (id,sobjs) -> ModSubstObjs.set (mp_id mp id) sobjs
677 | IncludeObject aobjs ->
678 List.iter (register_mod_objs mp) (expand_aobjs aobjs)
679 | _ -> ()
680
681 let handle_missing_substobjs mp = match mp with
682 | MPdot (mp',l) ->
683 let objs = expand_sobjs (ModSubstObjs.get mp') in
684 List.iter (register_mod_objs mp') objs;
685 ModSubstObjs.get mp
686 | _ ->
687 assert false (* Only inner parts of module types should be missing *)
688
689 let () = ModSubstObjs.set_missing_handler handle_missing_substobjs
690
691
692
693 (** {6 From module expression to substitutive objects} *)
694
695 (** Turn a chain of [MSEapply] into the head ModPath.t and the
696 list of ModPath.t parameters (deepest param coming first).
697 The left part of a [MSEapply] must be either [MSEident] or
698 another [MSEapply]. *)
699
700 let get_applications mexpr =
701 let rec get params = function
702 | MEident mp -> mp, params
703 | MEapply (fexpr, mp) -> get (mp::params) fexpr
704 | MEwith _ -> user_err Pp.(str "Non-atomic functor application.")
705 in get [] mexpr
706
707 (** Create the objects of a "with Module" structure. *)
708
709 let rec replace_module_object idl mp0 objs0 mp1 objs1 =
710 match idl, objs0 with
711 | _,[] -> []
712 | id::idl,(ModuleObject (id', sobjs))::tail when Id.equal id id' ->
713 begin
714 let mp_id = MPdot(mp0, id) in
715 let objs = match idl with
716 | [] -> subst_objects (map_mp mp1 mp_id (empty_delta_resolver mp_id)) objs1
717 | _ ->
718 let objs_id = expand_sobjs sobjs in
719 replace_module_object idl mp_id objs_id mp1 objs1
720 in
721 (ModuleObject (id, ([], Objs objs)))::tail
722 end
723 | idl,lobj::tail -> lobj::replace_module_object idl mp0 tail mp1 objs1
724
725 (** Substitutive objects of a module expression (or module type) *)
726
727 let rec get_module_sobjs is_mod env inl = function
728 | MEident mp ->
729 begin match ModSubstObjs.get mp with
730 | (mbids,Objs _) when not (ModPath.is_bound mp) ->
731 (mbids,Ref (mp, empty_subst)) (* we create an alias *)
732 | sobjs -> sobjs
733 end
734 | MEwith (mty, WithDef _) -> get_module_sobjs is_mod env inl mty
735 | MEwith (mty, WithMod (idl,mp1)) ->
736 assert (not is_mod);
737 let sobjs0 = get_module_sobjs is_mod env inl mty in
738 if not (sobjs_no_functor sobjs0) then
739 user_err Pp.(str "Illegal use of a functor.");
740 (* For now, we expand everything, to be safe *)
741 let mp0 = get_module_path mty in
742 let objs0 = expand_sobjs sobjs0 in
743 let objs1 = expand_sobjs (ModSubstObjs.get mp1) in
744 ([], Objs (replace_module_object idl mp0 objs0 mp1 objs1))
745 | MEapply _ as me ->
746 let mp1, mp_l = get_applications me in
747 let mbids, aobjs = get_module_sobjs is_mod env inl (MEident mp1) in
748 let mbids_left,subst = Actions.compute_subst ~is_mod env mbids mp1 mp_l inl in
749 (mbids_left, subst_aobjs subst aobjs)
750
751 let debug_print_modtab () =
752 let pr_seg = function
753 | 0 -> str "[]"
754 | l -> str "[." ++ int l ++ str ".]"
755 in
756 let pr_modinfo mp modobjs s =
757 let objs = List.length modobjs.module_substituted_objects + List.length modobjs.module_keep_objects.keep_objects in
758 s ++ str (ModPath.to_string mp) ++ spc () ++ pr_seg objs
759 in
760 let modules = ModPath.Map.fold pr_modinfo (ModObjs.all ()) (mt ()) in
761 hov 0 modules
762
763 let add_discharged_item : Lib.discharged_item -> unit = function
764 | DischargedExport { mpl } -> import_modules ~export:Export mpl
765 | DischargedLeaf o -> Lib.add_discharged_leaf o
766
767 let close_section () =
768 let objs = Actions.Lib.close_section () in
769 List.iter add_discharged_item objs
770
771 end
772
773 module SynterpVisitor : StagedModS
774 with type env = SynterpActions.env
775 with type typexpr = Constrexpr.universe_decl_expr option * Constrexpr.constr_expr
776 = StagedMod(SynterpActions)
777
778 module InterpVisitor : StagedModS
779 with type env = InterpActions.env
780 with type typexpr = Constr.t * UVars.AbstractContext.t option
781 = StagedMod(InterpActions)
782
783 (** {6 Modules : start, end, declare} *)
784
785 type current_module_syntax_info = {
786 cur_mp : ModPath.t;
787 cur_typ : ((Constrexpr.universe_decl_expr option * Constrexpr.constr_expr) module_alg_expr * int option) option;
788 cur_mbids : MBId.t list;
789 }
790
791 let default_module_syntax_info mp = { cur_mp = mp; cur_typ = None; cur_mbids = [] }
792
793 let openmod_syntax_info =
794 Summary.ref None ~stage:Summary.Stage.Synterp ~name:"MODULE-SYNTAX-INFO"
795
796 (** {6 Current module information}
797
798 This information is stored by each [start_module] for use
799 in a later [end_module]. *)
800
801 type current_module_info = {
802 cur_typ : (module_struct_entry * int option) option; (** type via ":" *)
803 cur_typs : module_type_body list (** types via "<:" *)
804 }
805
806 let default_module_info = { cur_typ = None; cur_typs = [] }
807
808 let openmod_info = Summary.ref default_module_info ~name:"MODULE-INFO"
809
810 let start_library dir =
811 let mp = Global.start_library dir in
812 openmod_info := default_module_info;
813 openmod_syntax_info := Some (default_module_syntax_info mp);
814 Lib.start_compilation dir mp
815
816 let set_openmod_syntax_info info = match !openmod_syntax_info with
817 | None -> anomaly Pp.(str "bad init of openmod_syntax_info")
818 | Some _ -> openmod_syntax_info := Some info
819
820 let openmod_syntax_info () = match !openmod_syntax_info with
821 | None -> anomaly Pp.(str "missing init of openmod_syntax_info")
822 | Some v -> v
823
824 let vm_state =
825 (* VM bytecode is not needed here *)
826 let vm_handler _ _ _ () = (), None in
827 ((), { Mod_typing.vm_handler })
828
829 module RawModOps = struct
830
831 module Synterp = struct
832
833 let build_subtypes mtys =
834 List.map
835 (fun (m,ann) ->
836 let inl = inl2intopt ann in
837 let mte, base, kind = Modintern.intern_module_ast Modintern.ModType m in
838 (mte, base, kind, inl))
839 mtys
840
841 let intern_arg (idl,(typ,ann)) =
842 let inl = inl2intopt ann in
843 let lib_dir = Lib.library_dp() in
844 let (mty, base, kind) = Modintern.intern_module_ast Modintern.ModType typ in
845 let sobjs = SynterpVisitor.get_module_sobjs false () inl mty in
846 let mp0 = get_module_path mty in
847 let map {CAst.v=id} =
848 let sp = Libnames.make_path DirPath.empty id in
849 let mbid = MBId.make lib_dir id in
850 let mp = MPbound mbid in
851 (* We can use an empty delta resolver because we load only syntax objects *)
852 let sobjs = subst_sobjs (map_mp mp0 mp (empty_delta_resolver mp)) sobjs in
853 SynterpVisitor.load_module 1 sp mp sobjs;
854 mbid
855 in
856 List.map map idl, (mty, base, kind, inl)
857
858 let intern_args params =
859 List.map intern_arg params
860
861 let start_module_core id args res =
862 (* Loads the parsing objects in arguments *)
863 let args = intern_args args in
864 let mbids = List.flatten @@ List.map (fun (mbidl,_) -> mbidl) args in
865 let res_entry_o, sign = match res with
866 | Enforce (res,ann) ->
867 let inl = inl2intopt ann in
868 let (mte, base, kind) = Modintern.intern_module_ast Modintern.ModType res in
869 Some (mte, inl), Enforce (mte, base, kind, inl)
870 | Check resl -> None, Check (build_subtypes resl)
871 in
872 let mp = ModPath.MPdot((openmod_syntax_info ()).cur_mp, id) in
873 mp, res_entry_o, mbids, sign, args
874
875 let start_module export id args res =
876 let () = if Option.has_some export && not (CList.is_empty args)
877 then user_err Pp.(str "Cannot import functors.")
878 in
879 let fs = Summary.Synterp.freeze_summaries () in
880 let mp, res_entry_o, mbids, sign, args = start_module_core id args res in
881 set_openmod_syntax_info { cur_mp = mp; cur_typ = res_entry_o; cur_mbids = mbids };
882 let prefix = Lib.Synterp.start_module export id mp fs in
883 Nametab.(push_dir (Until 1) (prefix.obj_path) (GlobDirRef.DirOpenModule prefix.obj_mp));
884 mp, args, sign
885
886 let end_module_core id (m_info : current_module_syntax_info) objects fs =
887 let {Lib.substobjs = substitute; keepobjs = keep; escapeobjs = escape; anticipateobjs = special; } = objects in
888
889 (* For sealed modules, we use the substitutive objects of their signatures *)
890 let sobjs0, keep = match m_info.cur_typ with
891 | None -> ([], Objs substitute), keep
892 | Some (mty, inline) ->
893 SynterpVisitor.get_module_sobjs false () inline mty, { keep_objects = [] }
894 in
895 Summary.Synterp.unfreeze_summaries fs;
896 let sobjs = let (ms,objs) = sobjs0 in (m_info.cur_mbids@ms,objs) in
897
898 (* We substitute objects if the module is sealed by a signature *)
899 let sobjs =
900 match m_info.cur_typ with
901 | None -> sobjs
902 | Some (mty, _) ->
903 subst_sobjs (map_mp (get_module_path mty) m_info.cur_mp (empty_delta_resolver m_info.cur_mp)) sobjs
904 in
905 let node = ModuleObject (id,sobjs) in
906 (* We add the keep objects, if any, and if this isn't a functor *)
907 let keep = if not (CList.is_empty m_info.cur_mbids) then [] else keep_objects id keep in
908 let escape = escape_objects id escape in
909 let objects = special@[node]@keep@escape in
910
911 m_info.cur_mp, objects
912
913 let end_module () =
914 let oldprefix,fs,objects = Lib.Synterp.end_module () in
915 let m_info = openmod_syntax_info () in
916 let olddp, id = pop_path oldprefix.obj_path in
917 let mp,objects = end_module_core id m_info objects fs in
918
919 let () = SynterpVisitor.add_leaves objects in
920
921 assert (eq_full_path (Lib.prefix()).obj_path olddp);
922 mp
923
924 let get_functor_sobjs is_mod inl (mbids,mexpr) =
925 let (mbids0, aobjs) = SynterpVisitor.get_module_sobjs is_mod () inl mexpr in
926 (mbids @ mbids0, aobjs)
927
928 let declare_module id args res mexpr_o =
929 let fs = Summary.Synterp.freeze_summaries () in
930 (* We simulate the beginning of an interactive module,
931 then we adds the module parameters to the global env. *)
932 let mp = ModPath.MPdot((openmod_syntax_info ()).cur_mp, id) in
933 let args = intern_args args in
934 let mbids = List.flatten @@ List.map fst args in
935 let mty_entry_o = match res with
936 | Enforce (mty,ann) ->
937 let inl = inl2intopt ann in
938 let (mte, base, kind) = Modintern.intern_module_ast Modintern.ModType mty in
939 Enforce (mte, base, kind, inl)
940 | Check mtys ->
941 Check (build_subtypes mtys)
942 in
943 let mexpr_entry_o = match mexpr_o with
944 | None -> None
945 | Some (mexpr,ann) ->
946 let (mte, base, kind) = Modintern.intern_module_ast Modintern.Module mexpr in
947 Some (mte, base, kind, inl2intopt ann)
948 in
949 let sobjs, mp0 = match mexpr_entry_o, mty_entry_o with
950 | None, Check _ -> assert false (* No body, no type ... *)
951 | _, Enforce (typ,_,_,inl_res) -> get_functor_sobjs false inl_res (mbids,typ), get_module_path typ
952 | Some (body, _, _, inl_expr), Check _ ->
953 get_functor_sobjs true inl_expr (mbids,body), get_module_path body
954 in
955 (* Undo the simulated interactive building of the module
956 and declare the module as a whole *)
957 Summary.Synterp.unfreeze_summaries fs;
958
959 (* We can use an empty delta resolver on syntax objects *)
960 let sobjs = subst_sobjs (map_mp mp0 mp (empty_delta_resolver mp)) sobjs in
961 ignore (SynterpVisitor.add_leaf (ModuleObject (id,sobjs)));
962 mp, args, mexpr_entry_o, mty_entry_o
963
964 end
965
966 module Interp = struct
967
968 (** {6 Auxiliary functions concerning subtyping checks} *)
969
970 let check_sub env mp sub_mtb_l =
971 let fold sub_mtb (cst, env) =
972 let state = ((Environ.universes env, cst), Reductionops.inferred_universes) in
973 let ugraph, cst = Subtyping.check_subtypes state env mp mp sub_mtb in
974 (cst, Environ.set_universes ugraph env)
975 in
976 let cst, _ = List.fold_right fold sub_mtb_l (Univ.UnivConstraints.empty, env) in
977 Global.add_univ_constraints cst
978
979 (** This function checks if the type calculated for the module [mp] is
980 a "<:"-like subtype of all signatures in [sub_mtb_l]. Uses only
981 the global environment. *)
982
983 let check_subtypes mp sub_mtb_l =
984 check_sub (Global.env ()) mp sub_mtb_l
985
986 (** Same for module type [mp] *)
987
988 let check_subtypes_mt mp sub_mtb_l =
989 let mtb =
990 try Global.lookup_modtype mp with Not_found -> assert false
991 in
992 let env = Modops.add_module mp (module_body_of_type mtb) (Global.env ()) in
993 check_sub env mp sub_mtb_l
994
995 let current_modresolver () =
996 Safe_typing.delta_of_senv @@ Global.safe_env ()
997
998 let current_struct () =
999 let struc = Safe_typing.structure_body_of_safe_env @@ Global.safe_env () in
1000 NoFunctor (List.rev struc)
1001
1002 (** Prepare the module type list for check of subtypes *)
1003
1004 let build_subtypes env mp args mtys =
1005 let (ctx, ans) = List.fold_left_map
1006 (fun ctx (mte,base,kind,inl) ->
1007 let mte, ctx' = Modintern.interp_module_ast env Modintern.ModType base mte in
1008 let env = Environ.push_context_set ~strict:true ctx' env in
1009 let ctx = Univ.ContextSet.union ctx ctx' in
1010 let state = ((Environ.universes env, Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1011 (* functor arguments are already part of the env, we compute the type
1012 and requantify over them *)
1013 let mtb, (_, cst), _ = Mod_typing.translate_modtype state vm_state env mp inl ([], mte) in
1014 let fold (mbid, mtb, _, _) accu =
1015 MoreFunctor (mbid, mtb, accu)
1016 in
1017 (* XXX: parameters will be rechecked for subtyping, even though we
1018 statically know they are the same as the ones of the ambient
1019 module *)
1020 let sign = List.fold_right fold args (mod_type mtb) in
1021 let mtb = make_module_type sign (mod_delta mtb) in
1022 let ctx = Univ.ContextSet.add_constraints cst ctx in
1023 ctx, mtb)
1024 Univ.ContextSet.empty mtys
1025 in
1026 (ans, ctx)
1027
1028 (** Process a declaration of functor parameter(s) (Id1 .. Idn : Typ)
1029 i.e. possibly multiple names with the same module type.
1030 Global environment is updated on the fly.
1031 Objects in these parameters are also loaded.
1032 Output is accumulated on top of [acc] (in reverse order). *)
1033
1034 let intern_arg (acc, cst) (mbidl,(mty, base, kind, inl)) =
1035 let env = Global.env() in
1036 let (mty, cst') = Modintern.interp_module_ast env kind base mty in
1037 let () = Global.push_context_set cst' in
1038 let () =
1039 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1040 let _, (_, cst), _ = Mod_typing.translate_modtype state vm_state (Global.env ()) base inl ([], mty) in
1041 Global.add_univ_constraints cst
1042 in
1043 let env = Global.env () in
1044 let sobjs = InterpVisitor.get_module_sobjs false env inl mty in
1045 let mp0 = get_module_path mty in
1046 let fold acc mbid =
1047 let id = MBId.to_id mbid in
1048 let sp = Libnames.make_path DirPath.empty id in
1049 let mp = MPbound mbid in
1050 let mtb = Global.add_module_parameter mbid mty inl in
1051 let resolver = mod_delta mtb in
1052 let sobjs = subst_sobjs (map_mp mp0 mp resolver) sobjs in
1053 InterpVisitor.load_module 1 sp mp sobjs;
1054 (mbid, mtb, mty, inl) :: acc
1055 in
1056 let acc = List.fold_left fold acc mbidl in
1057 (acc, Univ.ContextSet.union cst cst')
1058
1059
1060 (** Process a list of declarations of functor parameters
1061 (Id11 .. Id1n : Typ1)..(Idk1 .. Idkm : Typk)
1062 Global environment is updated on the fly.
1063 The calls to [interp_modast] should be interleaved with these
1064 env updates, otherwise some "with Definition" could be rejected.
1065 Returns a list of mbids and entries (in reversed order).
1066
1067 This used to be a [List.concat (List.map ...)], but this should
1068 be more efficient and independent of [List.map] eval order.
1069 *)
1070
1071 let intern_args params =
1072 let args, ctx = List.fold_left intern_arg ([], Univ.ContextSet.empty) params in
1073 List.rev args, ctx
1074
1075 let start_module_core id args res =
1076 let mp = Global.start_module id in
1077 let params, ctx = intern_args args in
1078 let () = Global.push_context_set ctx in
1079 let env = Global.env () in
1080 let res_entry_o, subtyps, ctx' = match res with
1081 | Enforce (mte, base, kind, inl) ->
1082 let (mte, ctx) = Modintern.interp_module_ast env kind base mte in
1083 let env = Environ.push_context_set ctx env in
1084 (* We check immediately that mte is well-formed *)
1085 let state = ((Environ.universes env, Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1086 let _, (_, cst), _ = Mod_typing.translate_modtype state vm_state env mp inl ([], mte) in
1087 let ctx = Univ.ContextSet.add_constraints cst ctx in
1088 Some (mte, inl), [], ctx
1089 | Check resl ->
1090 let typs, ctx = build_subtypes env mp params resl in
1091 None, typs, ctx
1092 in
1093 let () = Global.push_context_set ctx' in
1094 mp, res_entry_o, subtyps, params, Univ.ContextSet.union ctx ctx'
1095
1096 let start_module export id args res =
1097 let fs = Summary.Interp.freeze_summaries () in
1098 let mp, res_entry_o, subtyps, _, _ = start_module_core id args res in
1099 openmod_info := { cur_typ = res_entry_o; cur_typs = subtyps };
1100 let _ : object_prefix = Lib.Interp.start_module export id mp fs in
1101 mp
1102
1103 let end_module_core id m_info objects fs =
1104 let {Lib.substobjs = substitute; keepobjs = keep; escapeobjs = escape; anticipateobjs = special; } = objects in
1105
1106 (* For sealed modules, we use the substitutive objects of their signatures *)
1107 let sobjs0, keep = match m_info.cur_typ with
1108 | None -> ([], Objs substitute), keep
1109 | Some (mty, inline) ->
1110 InterpVisitor.get_module_sobjs false (Global.env()) inline mty, { keep_objects = [] }
1111 in
1112
1113 let struc = current_struct () in
1114 let restype' = Option.map (fun (ty,inl) -> (([],ty),inl)) m_info.cur_typ in
1115 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1116 let _, (_, cst), _ =
1117 Mod_typing.finalize_module state vm_state (Global.env ()) (Global.current_modpath ())
1118 (struc, current_modresolver ()) restype'
1119 in
1120 let () = Global.add_univ_constraints cst in
1121
1122 let mp,mbids,resolver = Global.end_module fs id m_info.cur_typ in
1123 let sobjs = let (ms,objs) = sobjs0 in (mbids@ms,objs) in
1124
1125 let () = check_subtypes mp m_info.cur_typs in
1126
1127 (* We substitute objects if the module is sealed by a signature *)
1128 let sobjs =
1129 match m_info.cur_typ with
1130 | None -> sobjs
1131 | Some (mty, _) ->
1132 subst_sobjs (map_mp (get_module_path mty) mp resolver) sobjs
1133 in
1134 let node = ModuleObject (id,sobjs) in
1135 (* We add the keep objects, if any, and if this isn't a functor *)
1136 let keep = if not (CList.is_empty mbids) then [] else keep_objects id keep in
1137 (* NB: escape objects are added even for sealed modules, not sure if want *)
1138 let escape = escape_objects id escape in
1139 let objects = special@[node]@keep@escape in
1140
1141 mp, objects
1142
1143 let end_module () =
1144 let oldprefix,fs,objects = Lib.Interp.end_module () in
1145 let m_info = !openmod_info in
1146 let _olddp, id = pop_path oldprefix.obj_path in
1147 let mp,objects = end_module_core id m_info objects fs in
1148
1149 let () = InterpVisitor.add_leaves objects in
1150
1151 (* Name consistency check : kernel vs. library *)
1152 assert (ModPath.equal oldprefix.obj_mp mp);
1153
1154 mp
1155
1156 let get_functor_sobjs is_mod env inl (params,mexpr) =
1157 let (mbids, aobjs) = InterpVisitor.get_module_sobjs is_mod env inl mexpr in
1158 (List.map pi1 params @ mbids, aobjs)
1159
1160 (* TODO cleanup push universes directly to global env *)
1161 let declare_module id args res mexpr_o =
1162 let fs = Summary.Interp.freeze_summaries () in
1163 (* We simulate the beginning of an interactive module,
1164 then we adds the module parameters to the global env. *)
1165 let mp, mty_entry_o, subs, params, ctx = start_module_core id args res in
1166 let env = Global.env () in
1167 let mexpr_entry_o, inl_expr, ctx' = match mexpr_o with
1168 | None -> None, default_inline_level (), Univ.ContextSet.empty
1169 | Some (mte, base, kind, inl) ->
1170 let (mte, ctx) = Modintern.interp_module_ast env kind base mte in
1171 Some mte, inl, ctx
1172 in
1173 let env = Environ.push_context_set ctx' env in
1174 let ctx = Univ.ContextSet.union ctx ctx' in
1175 let params = List.map (fun (mbid, _, mte, b) -> (mbid, mte, b)) params in
1176 let entry, inl_res = match mexpr_entry_o, mty_entry_o with
1177 | None, None -> assert false (* No body, no type ... *)
1178 | None, Some (typ, inl) -> MType (params, typ), inl
1179 | Some body, otyp -> MExpr (params, body, Option.map fst otyp), Option.cata snd (default_inline_level ()) otyp
1180 in
1181 let sobjs, mp0 = match entry with
1182 | MType (_,mte) | MExpr (_,_,Some mte) ->
1183 get_functor_sobjs false env inl_res (params,mte), get_module_path mte
1184 | MExpr (_,me,None) ->
1185 get_functor_sobjs true env inl_expr (params,me), get_module_path me
1186 in
1187 (* Undo the simulated interactive building of the module
1188 and declare the module as a whole *)
1189 Summary.Interp.unfreeze_summaries fs;
1190 let inl = match inl_expr with
1191 | None -> None
1192 | _ -> inl_res
1193 in
1194 let () = Global.push_context_set ctx in
1195 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1196 let _, (_, cst), _ = Mod_typing.translate_module state vm_state (Global.env ()) mp inl entry in
1197 let () = Global.add_univ_constraints cst in
1198 let mp_env,resolver = Global.add_module id entry inl in
1199
1200 (* Name consistency check : kernel vs. library *)
1201 assert (ModPath.equal mp (mp_of_kn (Lib.make_kn id)));
1202 assert (ModPath.equal mp mp_env);
1203
1204 let () = check_subtypes mp subs in
1205
1206 let sobjs = subst_sobjs (map_mp mp0 mp resolver) sobjs in
1207 InterpVisitor.add_leaf (ModuleObject (id,sobjs));
1208 mp
1209
1210 end
1211
1212 end
1213
1214 (** {6 Module types : start, end, declare} *)
1215
1216 module RawModTypeOps = struct
1217
1218 module Synterp = struct
1219
1220 let start_modtype_core id cur_mp args mtys =
1221 let mp = ModPath.MPdot(cur_mp, id) in
1222 let args = RawModOps.Synterp.intern_args args in
1223 let mbids = List.flatten @@ List.map (fun (mbidl,_) -> mbidl) args in
1224 let sub_mty_l = RawModOps.Synterp.build_subtypes mtys in
1225 mp, mbids, args, sub_mty_l
1226
1227 let start_modtype id args mtys =
1228 let fs = Summary.Synterp.freeze_summaries () in
1229 let mp, mbids, args, sub_mty_l = start_modtype_core id (openmod_syntax_info ()).cur_mp args mtys in
1230 set_openmod_syntax_info { cur_mp = mp; cur_typ = None; cur_mbids = mbids };
1231 let prefix = Lib.Synterp.start_modtype id mp fs in
1232 Nametab.(push_dir (Until 1) (prefix.obj_path) (GlobDirRef.DirOpenModtype prefix.obj_mp));
1233 mp, args, sub_mty_l
1234
1235 let end_modtype_core id mbids objects fs =
1236 let {Lib.substobjs = substitute; keepobjs = _; escapeobjs = escape; anticipateobjs = special; } = objects in
1237 Summary.Synterp.unfreeze_summaries fs;
1238 let modtypeobjs = (mbids, Objs substitute) in
1239 special@[ModuleTypeObject (id,modtypeobjs)]@(escape_objects id escape)
1240
1241 let end_modtype () =
1242 let oldprefix,fs,objects = Lib.Synterp.end_modtype () in
1243 let _olddp, id = pop_path oldprefix.obj_path in
1244 let objects = end_modtype_core id (openmod_syntax_info ()).cur_mbids objects fs in
1245 SynterpVisitor.add_leaves objects;
1246 (openmod_syntax_info ()).cur_mp
1247
1248 let declare_modtype id args mtys (mty,ann) =
1249 let fs = Summary.Synterp.freeze_summaries () in
1250 let inl = inl2intopt ann in
1251 (* We simulate the beginning of an interactive module,
1252 then we adds the module parameters to the global env. *)
1253 let mp, mbids, args, sub_mty_l = start_modtype_core id (openmod_syntax_info ()).cur_mp args mtys in
1254 let mte, base, kind = Modintern.intern_module_ast Modintern.ModType mty in
1255 let entry = mbids, mte in
1256 let sobjs = RawModOps.Synterp.get_functor_sobjs false inl entry in
1257 let subst = map_mp (get_module_path (snd entry)) mp (empty_delta_resolver mp) in
1258 let sobjs = subst_sobjs subst sobjs in
1259
1260 (* Undo the simulated interactive building of the module type
1261 and declare the module type as a whole *)
1262 Summary.Synterp.unfreeze_summaries fs;
1263 ignore (SynterpVisitor.add_leaf (ModuleTypeObject (id,sobjs)));
1264 mp, args, (mte, base, kind, inl), sub_mty_l
1265
1266 end
1267
1268 module Interp = struct
1269
1270 let openmodtype_info =
1271 Summary.ref ([] : module_type_body list) ~name:"MODTYPE-INFO"
1272
1273 let start_modtype_core id args mtys =
1274 let mp = Global.start_modtype id in
1275 let params, params_ctx = RawModOps.Interp.intern_args args in
1276 let () = Global.push_context_set params_ctx in
1277 let env = Global.env () in
1278 let sub_mty_l, sub_mty_ctx = RawModOps.Interp.build_subtypes env mp params mtys in
1279 let () = Global.push_context_set sub_mty_ctx in
1280 mp, params, sub_mty_l, Univ.ContextSet.union params_ctx sub_mty_ctx
1281
1282 let start_modtype id args mtys =
1283 let fs = Summary.Interp.freeze_summaries () in
1284 let mp, _, sub_mty_l, _ = start_modtype_core id args mtys in
1285 openmodtype_info := sub_mty_l;
1286 let prefix = Lib.Interp.start_modtype id mp fs in
1287 Nametab.(push_dir (Until 1) (prefix.obj_path) (GlobDirRef.DirOpenModtype mp));
1288 mp
1289
1290 let end_modtype_core id sub_mty_l objects fs =
1291 let {Lib.substobjs = substitute; keepobjs = _; escapeobjs = escape; anticipateobjs = special; } = objects in
1292 let mp, mbids = Global.end_modtype fs id in
1293 let () = RawModOps.Interp.check_subtypes_mt mp sub_mty_l in
1294 let modtypeobjs = (mbids, Objs substitute) in
1295 let objects = special@[ModuleTypeObject (id,modtypeobjs)]@(escape_objects id escape) in
1296 mp, objects
1297
1298 let end_modtype () =
1299 let oldprefix,fs,objects = Lib.Interp.end_modtype () in
1300 let olddp, id = pop_path oldprefix.obj_path in
1301 let sub_mty_l = !openmodtype_info in
1302 let mp, objects = end_modtype_core id sub_mty_l objects fs in
1303 let () = InterpVisitor.add_leaves objects in
1304 (* Check name consistence : start_ vs. end_modtype, kernel vs. library *)
1305 assert (eq_full_path (Lib.prefix()).obj_path olddp);
1306 assert (ModPath.equal oldprefix.obj_mp mp);
1307 mp
1308
1309 let declare_modtype id args mtys (mte,base,kind,inl) =
1310 let fs = Summary.Interp.freeze_summaries () in
1311 (* We simulate the beginning of an interactive module,
1312 then we adds the module parameters to the global env. *)
1313 let mp, params, sub_mty_l, ctx = start_modtype_core id args mtys in
1314 let env = Global.env () in
1315 let mte, mte_ctx = Modintern.interp_module_ast env kind base mte in
1316 let () = Global.push_context_set mte_ctx in
1317 let env = Global.env () in
1318 (* We check immediately that mte is well-formed *)
1319 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1320 let _, (_, mte_cst), _ = Mod_typing.translate_modtype state vm_state env mp inl ([], mte) in
1321 let () = Global.push_context_set (Univ.Level.Set.empty, mte_cst) in
1322 let params = List.map (fun (mbid, _, mte, b) -> (mbid, mte, b)) params in
1323 let entry = params, mte in
1324 let env = Global.env () in
1325 let sobjs = RawModOps.Interp.get_functor_sobjs false env inl entry in
1326 let subst = map_mp (get_module_path (snd entry)) mp (empty_delta_resolver mp) in
1327 let sobjs = subst_sobjs subst sobjs in
1328
1329 (* Undo the simulated interactive building of the module type
1330 and declare the module type as a whole *)
1331 Summary.Interp.unfreeze_summaries fs;
1332
1333 (* We enrich the global environment *)
1334 let () = Global.push_context_set ctx in
1335 let () = Global.push_context_set mte_ctx in
1336 let () = Global.push_context_set (Univ.Level.Set.empty, mte_cst) in
1337 let mp_env = Global.add_modtype id entry inl in
1338
1339 (* Name consistency check : kernel vs. library *)
1340 assert (ModPath.equal mp_env mp);
1341
1342 (* Subtyping checks *)
1343 let () = RawModOps.Interp.check_subtypes_mt mp sub_mty_l in
1344
1345 InterpVisitor.add_leaf (ModuleTypeObject (id, sobjs));
1346 mp
1347
1348 end
1349
1350 end
1351
1352 (** {6 Include} *)
1353
1354 module RawIncludeOps = struct
1355
1356 exception NoIncludeSelf
1357
1358 module Synterp = struct
1359
1360 let rec include_subst mp mbids = match mbids with
1361 | [] -> empty_subst
1362 | mbid::mbids ->
1363 let subst = include_subst mp mbids in
1364 join (map_mbid mbid mp (empty_delta_resolver mp)) subst
1365
1366 let declare_one_include_core cur_mp (me_ast,annot) =
1367 let me, base, kind = Modintern.intern_module_ast Modintern.ModAny me_ast in
1368 let is_mod = (kind == Modintern.Module) in
1369 let inl = inl2intopt annot in
1370 let mbids,aobjs = SynterpVisitor.get_module_sobjs is_mod () inl me in
1371 let subst_self =
1372 try
1373 if List.is_empty mbids then raise NoIncludeSelf;
1374 include_subst cur_mp mbids
1375 with NoIncludeSelf -> empty_subst
1376 in
1377 let base_mp = get_module_path me in
1378 (* We can use an empty delta resolver on syntax objects *)
1379 let subst = join subst_self (map_mp base_mp cur_mp (empty_delta_resolver cur_mp)) in
1380 let aobjs = subst_aobjs subst aobjs in
1381 (me, base, kind, inl), aobjs
1382
1383 let declare_one_include (me_ast,annot) =
1384 let res, aobjs = declare_one_include_core (openmod_syntax_info ()).cur_mp (me_ast,annot) in
1385 SynterpVisitor.add_leaf (IncludeObject aobjs);
1386 res
1387
1388 let declare_include me_asts = List.map declare_one_include me_asts
1389
1390 end
1391
1392 module Interp = struct
1393
1394 let rec include_subst env mp reso mbids sign inline = match mbids with
1395 | [] -> empty_subst
1396 | mbid::mbids ->
1397 let farg_id, farg_b, fbody_b = Modops.destr_functor sign in
1398 let subst = include_subst env mp reso mbids fbody_b inline in
1399 let mp_delta =
1400 Modops.inline_delta_resolver env inline mp farg_id farg_b reso
1401 in
1402 join (map_mbid mbid mp mp_delta) subst
1403
1404 let rec decompose_functor mpl typ =
1405 match mpl, typ with
1406 | [], _ -> typ
1407 | _::mpl, MoreFunctor(_,_,str) -> decompose_functor mpl str
1408 | _ -> user_err Pp.(str "Application of a functor with too much arguments.")
1409
1410 let type_of_incl env is_mod = function
1411 | MEident mp -> type_of_mod mp env is_mod
1412 | MEapply _ as me ->
1413 let mp0, mp_l = InterpVisitor.get_applications me in
1414 decompose_functor mp_l (type_of_mod mp0 env is_mod)
1415 | MEwith _ -> raise NoIncludeSelf
1416
1417 (** Implements [Include F] where [F] has parameters [mbids] to be
1418 instantiated by fields of the current "self" module, i.e. using
1419 subtyping, by the current module itself. *)
1420
1421 let declare_one_include_core (me,base,kind,inl) =
1422 let env = Global.env() in
1423 let me, cst = Modintern.interp_module_ast env kind base me in
1424 let () = Global.push_context_set cst in
1425 let env = Global.env () in
1426 let is_mod = (kind == Modintern.Module) in
1427 let cur_mp = Global.current_modpath () in
1428 let mbids,aobjs = InterpVisitor.get_module_sobjs is_mod env inl me in
1429 let subst_self =
1430 try
1431 if List.is_empty mbids then raise NoIncludeSelf;
1432 let typ = type_of_incl env is_mod me in
1433 let reso = RawModOps.Interp.current_modresolver () in
1434 include_subst env cur_mp reso mbids typ inl
1435 with NoIncludeSelf -> empty_subst
1436 in
1437 let base_mp = get_module_path me in
1438
1439 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1440 let sign, (), resolver, (_, cst), _ =
1441 Mod_typing.translate_mse_include is_mod state vm_state (Global.env ()) (Global.current_modpath ()) inl me
1442 in
1443 let () = Global.add_univ_constraints cst in
1444 let () = assert (ModPath.equal cur_mp (Global.current_modpath ())) in
1445 (* Include Self support *)
1446 let mb = make_module_body (RawModOps.Interp.current_struct ()) (RawModOps.Interp.current_modresolver ()) [] in
1447 let rec compute_sign sign =
1448 match sign with
1449 | MoreFunctor(mbid,mtb,str) ->
1450 let state = ((Global.universes (), Univ.UnivConstraints.empty), Reductionops.inferred_universes) in
1451 (* Module subcomponents are already part of env at this point *)
1452 let env = Environ.shallow_add_module cur_mp mb (Global.env ()) in
1453 let (_, cst) = Subtyping.check_subtypes state env cur_mp (MPbound mbid) mtb in
1454 let () = Global.add_univ_constraints cst in
1455 let mpsup_delta = match mod_global_delta mb with
1456 | None -> assert false (* mb is guaranteed not to be a functor here *)
1457 | Some delta ->
1458 Modops.inline_delta_resolver (Global.env ()) inl cur_mp mbid mtb delta
1459 in
1460 let subst = Mod_subst.map_mbid mbid cur_mp mpsup_delta in
1461 compute_sign (Modops.subst_signature subst cur_mp str)
1462 | NoFunctor str -> ()
1463 in
1464 let () = compute_sign sign in
1465
1466 let resolver = Global.add_include me is_mod inl in
1467 let subst = join subst_self (map_mp base_mp cur_mp resolver) in
1468 subst_aobjs subst aobjs
1469
1470 let declare_one_include (me,base,kind,inl) =
1471 let aobjs = declare_one_include_core (me,base,kind,inl) in
1472 InterpVisitor.add_leaf (IncludeObject aobjs)
1473
1474 let declare_include me_asts = List.iter declare_one_include me_asts
1475
1476 end
1477
1478 end
1479
1480 (** {6 Libraries} *)
1481
1482 type library_name = DirPath.t
1483
1484 (** A library object is made of some substitutive objects
1485 and some "keep" and "escape" objects. *)
1486
1487 type library_objects = Libobject.t list * keep_objects * escape_objects
1488
1489 module Synterp = struct
1490
1491 let start_module export id args res =
1492 RawModOps.Synterp.start_module export id args res
1493
1494 let end_module = RawModOps.Synterp.end_module
1495
1496 (** Declare a module in terms of a list of module bodies, by including them.
1497 Typically used for `Module M := N <+ P`.
1498 *)
1499 let declare_module_includes id args res mexpr_l =
1500 let fs = Summary.Synterp.freeze_summaries () in
1501 let mp, res_entry_o, mbids, sign, args = RawModOps.Synterp.start_module_core id args res in
1502 let mod_info = { cur_mp = mp; cur_typ = res_entry_o; cur_mbids = mbids } in
1503 let includes = List.map_left (RawIncludeOps.Synterp.declare_one_include_core mp) mexpr_l in
1504 let bodies, incl_objs = List.split includes in
1505 let incl_objs = List.map (fun x -> IncludeObject x) incl_objs in
1506 let objects = {
1507 Lib.substobjs = incl_objs;
1508 keepobjs = { keep_objects = [] };
1509 escapeobjs = { escape_objects = [] };
1510 anticipateobjs = [];
1511 } in
1512 let mp, objects = RawModOps.Synterp.end_module_core id mod_info objects fs in
1513 SynterpVisitor.add_leaves objects;
1514 mp, args, bodies, sign
1515
1516 (** Declare a module type in terms of a list of module bodies, by including them.
1517 Typically used for `Module Type M := N <+ P`.
1518 *)
1519 let declare_modtype_includes id args res mexpr_l =
1520 let fs = Summary.Synterp.freeze_summaries () in
1521 let mp, mbids, args, subtyps = RawModTypeOps.Synterp.start_modtype_core id (openmod_syntax_info ()).cur_mp args res in
1522 let includes = List.map_left (RawIncludeOps.Synterp.declare_one_include_core mp) mexpr_l in
1523 let bodies, incl_objs = List.split includes in
1524 let incl_objs = List.map (fun x -> IncludeObject x) incl_objs in
1525 let objects = {
1526 Lib.substobjs = incl_objs;
1527 keepobjs = { keep_objects = [] };
1528 escapeobjs = { escape_objects = [] };
1529 anticipateobjs = [];
1530 } in
1531 let objects = RawModTypeOps.Synterp.end_modtype_core id mbids objects fs in
1532 SynterpVisitor.add_leaves objects;
1533 mp, args, bodies, subtyps
1534
1535 let declare_module id args mtys me_l =
1536 match me_l with
1537 | [] ->
1538 let mp, args, body, sign = RawModOps.Synterp.declare_module id args mtys None in
1539 assert (Option.is_empty body);
1540 mp, args, [], sign
1541 | [me] ->
1542 let mp, args, body, sign = RawModOps.Synterp.declare_module id args mtys (Some me) in
1543 mp, args, [Option.get body], sign
1544 | me_l -> declare_module_includes id args mtys me_l
1545
1546 let start_modtype id args mtys =
1547 RawModTypeOps.Synterp.start_modtype id args mtys
1548
1549 let end_modtype = RawModTypeOps.Synterp.end_modtype
1550
1551 let declare_modtype id args mtys mty_l =
1552 match mty_l with
1553 | [] -> assert false
1554 | [mty] ->
1555 let mp, args, body, sign = RawModTypeOps.Synterp.declare_modtype id args mtys mty in
1556 mp, args, [body], sign
1557 | mty_l -> declare_modtype_includes id args mtys mty_l
1558
1559 let declare_include = RawIncludeOps.Synterp.declare_include
1560
1561 let register_library dir (objs:library_objects) =
1562 let mp = MPfile dir in
1563 let sp = path_of_file dir in
1564 let sobjs,keepobjs,escapeobjs = objs in
1565 SynterpVisitor.load_module 1 sp mp ([],Objs sobjs);
1566 SynterpVisitor.load_escape 2 sp mp escapeobjs;
1567 SynterpVisitor.load_keep 2 sp mp keepobjs
1568
1569 let import_modules = SynterpVisitor.import_modules
1570
1571 let import_module f ~export mp =
1572 import_modules ~export [f,mp]
1573
1574 let close_section = SynterpVisitor.close_section
1575
1576 end
1577
1578 module Interp = struct
1579
1580 let start_module = RawModOps.Interp.start_module
1581
1582 let end_module = RawModOps.Interp.end_module
1583
1584 (** Declare a module in terms of a list of module bodies, by including them.
1585 Typically used for `Module M := N <+ P`.
1586 *)
1587 let declare_module_includes id args res mexpr_l =
1588 let fs = Summary.Interp.freeze_summaries () in
1589 let mp, res_entry_o, subtyps, _, _ = RawModOps.Interp.start_module_core id args res in
1590 let mod_info = { cur_typ = res_entry_o; cur_typs = subtyps } in
1591 let incl_objs = List.map_left (fun x -> IncludeObject (RawIncludeOps.Interp.declare_one_include_core x)) mexpr_l in
1592 let objects = {
1593 Lib.substobjs = incl_objs;
1594 keepobjs = { keep_objects = [] };
1595 escapeobjs = { escape_objects = [] };
1596 anticipateobjs = [];
1597 } in
1598 let mp, objects = RawModOps.Interp.end_module_core id mod_info objects fs in
1599 InterpVisitor.add_leaves objects;
1600 mp
1601
1602 (** Declare a module type in terms of a list of module bodies, by including them.
1603 Typically used for `Module Type M := N <+ P`.
1604 *)
1605 let declare_modtype_includes id args res mexpr_l =
1606 let fs = Summary.Interp.freeze_summaries () in
1607 let mp, _, subtyps, _ = RawModTypeOps.Interp.start_modtype_core id args res in
1608 let incl_objs = List.map_left (fun x -> IncludeObject (RawIncludeOps.Interp.declare_one_include_core x)) mexpr_l in
1609 let objects = {
1610 Lib.substobjs = incl_objs;
1611 keepobjs = { keep_objects = [] };
1612 escapeobjs = { escape_objects = [] };
1613 anticipateobjs = [];
1614 } in
1615 let mp, objects = RawModTypeOps.Interp.end_modtype_core id subtyps objects fs in
1616 InterpVisitor.add_leaves objects;
1617 mp
1618
1619 let declare_module id args mtys me_l =
1620 match me_l with
1621 | [] -> RawModOps.Interp.declare_module id args mtys None
1622 | [me] -> RawModOps.Interp.declare_module id args mtys (Some me)
1623 | me_l -> declare_module_includes id args mtys me_l
1624
1625 let start_modtype = RawModTypeOps.Interp.start_modtype
1626
1627 let end_modtype = RawModTypeOps.Interp.end_modtype
1628
1629 let declare_modtype id args mtys mty_l =
1630 match mty_l with
1631 | [] -> assert false
1632 | [mty] -> RawModTypeOps.Interp.declare_modtype id args mtys mty
1633 | mty_l -> declare_modtype_includes id args mtys mty_l
1634
1635 let declare_include me_asts =
1636 if Lib.sections_are_opened () then
1637 user_err Pp.(str "Include is not allowed inside sections.");
1638 RawIncludeOps.Interp.declare_include me_asts
1639
1640 let register_library dir cenv (objs:library_objects) digest vmtab =
1641 let mp = MPfile dir in
1642 let sp = path_of_file dir in
1643 let () =
1644 try
1645 (* If the library was loaded inside a module or section, the
1646 end_segment will replay the library object for non-kernel
1647 effects but the kernel did not forget the library. *)
1648 ignore(Global.lookup_module mp);
1649 with Not_found ->
1650 begin
1651 let mp' = Global.import cenv vmtab digest in
1652 if not (ModPath.equal mp mp') then
1653 anomaly (Pp.str "Unexpected disk module name.")
1654 end
1655 in
1656 let sobjs,keepobjs,escapeobjs = objs in
1657 InterpVisitor.load_module 1 sp mp ([],Objs sobjs);
1658 InterpVisitor.load_escape 1 sp mp escapeobjs;
1659 InterpVisitor.load_keep 1 sp mp keepobjs
1660
1661 let import_modules = InterpVisitor.import_modules
1662
1663 let import_module f ~export mp =
1664 import_modules ~export [f,mp]
1665
1666 let close_section = InterpVisitor.close_section
1667
1668 end
1669
1670 let end_library_hook = ref []
1671 let append_end_library_hook f =
1672 end_library_hook := f :: !end_library_hook
1673
1674 let end_library_hook () =
1675 List.iter (fun f -> f ()) (List.rev !end_library_hook)
1676
1677 let end_library ~output_native_objects dir =
1678 end_library_hook();
1679 let { Lib.info; interp_objects = lib_stack; synterp_objects = lib_stack_syntax; } =
1680 Lib.end_compilation dir
1681 in
1682 let mp,cenv,vmlib,ast = Global.export ~output_native_objects dir in
1683 assert (ModPath.equal mp (MPfile dir));
1684 let drop_anticipate {Lib.substobjs; keepobjs; escapeobjs; anticipateobjs=_} =
1685 (substobjs, keepobjs, escapeobjs)
1686 in
1687 cenv,(drop_anticipate lib_stack),(drop_anticipate lib_stack_syntax),vmlib,ast,info
1688
1689 (** {6 Iterators} *)
1690
1691 let iter_all_interp_segments f =
1692 let rec apply_obj prefix obj = match obj with
1693 | IncludeObject aobjs ->
1694 let objs = InterpVisitor.expand_aobjs aobjs in
1695 List.iter (apply_obj prefix) objs
1696 | _ -> f prefix obj
1697 in
1698 let rec subst_view (obj : exp_substituted_object) : Libobject.t = match obj with
1699 | KeepObject (_, o) | EscapeObject (_, o) -> Empty.abort o
1700 | (AtomicObject _ | ExportObject _ | ModuleObject _ | ModuleTypeObject _) as o -> o
1701 | IncludeObject aobjs ->
1702 let aobjs = aobjs.exp_algebraic_objects in
1703 IncludeObject (Objs (List.map subst_view aobjs))
1704 in
1705 let apply_mod_obj _ modobjs =
1706 let prefix = modobjs.module_prefix in
1707 List.iter (fun obj -> apply_obj prefix (subst_view obj)) modobjs.module_substituted_objects;
1708 List.iter (fun obj -> apply_obj prefix obj) modobjs.module_keep_objects.keep_objects
1709 in
1710 let apply_nodes (node, os) = List.iter (fun o -> apply_obj (Lib.node_prefix node) o) os in
1711 ModPath.Map.iter apply_mod_obj (InterpVisitor.ModObjs.all ());
1712 List.iter apply_nodes (Lib.contents ())
1713
1714
1715 (** {6 Some types used to shorten declaremods.mli} *)
1716
1717 type module_params = (lident list * (Constrexpr.module_ast * inline)) list
1718 type module_expr = (Modintern.module_struct_expr * ModPath.t * Modintern.module_kind * Entries.inline)
1719 type module_params_expr = (MBId.t list * module_expr) list
1720
1721 (** {6 Debug} *)
1722
1723 let debug_print_modtab () = InterpVisitor.debug_print_modtab ()
1724
1725 (** For printing modules, [process_module_binding] adds names of
1726 bound module (and its components) to Nametab. It also loads
1727 objects associated to it. *)
1728
1729 let process_module_binding mbid me =
1730 let sp = Libnames.make_path DirPath.empty (MBId.to_id mbid) in
1731 let mp = MPbound mbid in
1732 let sobjs = InterpVisitor.get_module_sobjs false (Global.env()) (default_inline_level ()) me in
1733 let subst = map_mp (get_module_path me) mp (empty_delta_resolver mp) in
1734 let sobjs = subst_sobjs subst sobjs in
1735 SynterpVisitor.load_module 1 sp mp sobjs;
1736 InterpVisitor.load_module 1 sp mp sobjs
1737
1738 let () = append_end_library_hook Profile_tactic.do_print_results_at_close