Debian Codegraph @

library/nametab.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 Names
12 open Libnames
13 open Globnames
14
15 (* Fully qualified printing support - checked by shortest_qualid functions *)
16 let print_fully_qualified_ref = ref false
17 let print_fully_qualified () = !print_fully_qualified_ref
18 let set_print_fully_qualified b = print_fully_qualified_ref := b
19
20 (* to this type are mapped DirPath.t's in the nametab *)
21 module GlobDirRef = struct
22 type t =
23 | DirOpenModule of ModPath.t
24 | DirOpenModtype of ModPath.t
25 | DirOpenSection of full_path
26
27 let equal r1 r2 = match r1, r2 with
28 | DirOpenModule op1, DirOpenModule op2 -> ModPath.equal op1 op2
29 | DirOpenModtype op1, DirOpenModtype op2 -> ModPath.equal op1 op2
30 | DirOpenSection op1, DirOpenSection op2 -> eq_full_path op1 op2
31 | (DirOpenModule _ | DirOpenModtype _ | DirOpenSection _), _ -> false
32
33 let compare r1 r2 = match r1, r2 with
34 | DirOpenModule op1, DirOpenModule op2 -> ModPath.compare op1 op2
35 | DirOpenModule _, _ -> -1
36 | _, DirOpenModule _ -> 1
37 | DirOpenModtype op1, DirOpenModtype op2 -> ModPath.compare op1 op2
38 | DirOpenModtype _, _ -> -1
39 | _, DirOpenModtype _ -> 1
40 | DirOpenSection op1, DirOpenSection op2 -> compare_full_path op1 op2
41
42 end
43
44 exception GlobalizationError of qualid
45
46 let error_global_not_found ~info qid =
47 let info = Option.cata (Loc.add_loc info) info qid.CAst.loc in
48 Exninfo.iraise (GlobalizationError qid, info)
49
50 (* The visibility can be registered either
51 - for all suffixes not shorter then a given int - when the object
52 is loaded inside a module
53 or
54 - for a precise suffix, when the module containing (the module
55 containing ...) the object is open (imported)
56 *)
57 type visibility = Until of int | Exactly of int
58
59 let map_visibility f = function
60 | Until i -> Until (f i)
61 | Exactly i -> Exactly (f i)
62
63 (* Data structure for nametabs *******************************************)
64
65 module type EqualityType =
66 sig
67 type t
68 val equal : t -> t -> bool
69 end
70
71 (* A ['a t] is a map from [user_name] to ['a], with possible lookup by
72 partially qualified names of type [qualid]. The mapping of
73 partially qualified names to ['a] is determined by the [visibility]
74 parameter of [push].
75
76 The [shortest_qualid] function given a user_name Mylib.A.B.x, tries
77 to find the shortest among x, B.x, A.B.x and Mylib.A.B.x that denotes
78 the same object.
79 *)
80 module type NAMETREE = sig
81 type elt
82 type t
83
84 val empty : t
85 val push : visibility -> full_path -> elt -> t -> t
86 val locate : qualid -> t -> elt
87 val locate_upto : limit:int -> qualid -> t -> (qualid * elt) list
88 val find : full_path -> t -> elt
89 val remove : full_path -> t -> t
90 val exists : full_path -> t -> bool
91 val shortest_qualid_gen : ?loc:Loc.t -> ?force_short:bool -> (Id.t -> bool) -> full_path -> t -> qualid
92 val find_prefixes : qualid -> t -> elt list
93
94 (** Matches a prefix of [qualid], useful for completion *)
95 val match_prefixes : qualid -> t -> elt list
96 end
97
98 let masking_absolute = CWarnings.create_warning
99 ~from:[Deprecation.Version.v8_8] ~name:"masking-absolute-name" ()
100
101 let coq_id = Id.of_string "Coq"
102 let stdlib_id = Id.of_string "Stdlib"
103 let corelib_id = Id.of_string "Corelib"
104
105 let warn_deprecated_dirpath_Coq =
106 CWarnings.create ~name:"deprecated-dirpath-Coq"
107 ~category:Deprecation.Version.v9_0
108 ~quickfix:(fun ~loc (old_id, new_id) -> [Quickfix.make ~loc new_id])
109 (fun (old_id, new_id) ->
110 Pp.(old_id ++ spc () ++ str "has been replaced by" ++ spc () ++ new_id ++ str "."))
111
112 (* We shadow as to create the quickfix and message at the same time *)
113 let fix_coq_id coq_repl l =
114 (match l with
115 | _coq_id :: l -> coq_repl :: l
116 | _ -> l)
117
118 (* [l] is reversed, thus [Corelib.ssr.bool] for example *)
119 let warn_deprecated_dirpath_Coq ?loc (coq_repl, l, id) =
120 let dp l = DirPath.make (List.rev l) in
121 let old_id = pr_qualid @@ Libnames.make_qualid (DirPath.make l) id in
122 let new_id = pr_qualid @@ Libnames.make_qualid (dp @@ fix_coq_id coq_repl (List.rev l)) id in
123 warn_deprecated_dirpath_Coq ?loc (old_id, new_id)
124
125 module Make (E : EqualityType) : NAMETREE
126 with type elt = E.t =
127 struct
128 type elt = E.t
129
130 (* A name became inaccessible, even with absolute qualification.
131 Example:
132 Module F (X : S). Module X.
133 The argument X of the functor F is masked by the inner module X.
134 *)
135 let warn_masking_absolute =
136 CWarnings.create_in masking_absolute
137 Pp.(fun n -> str "Trying to mask the absolute name \"" ++ pr_path n ++ str "\"!")
138
139 type path_status =
140 | Relative of full_path * elt
141 | Absolute of full_path * elt
142
143 let eq_path_status p q = match p, q with
144 | Relative (u1, o1), Relative (u2, o2) -> eq_full_path u1 u2 && E.equal o1 o2
145 | Absolute (u1, o1), Absolute (u2, o2) -> eq_full_path u1 u2 && E.equal o1 o2
146 | (Absolute _ | Relative _), _ -> false
147
148 (* Dictionaries of short names *)
149 type nametree =
150 { path : path_status list;
151 map : nametree Id.Map.t }
152
153 let push_path arg path = match path with
154 | [] -> [arg]
155 | arg' :: _ -> if eq_path_status arg arg' then path else arg :: path
156
157 let mktree p m = { path=p; map=m }
158 let empty_tree = mktree [] Id.Map.empty
159 let is_empty_tree tree = tree.path = [] && Id.Map.is_empty tree.map
160
161 type t = nametree Id.Map.t
162
163 let empty = Id.Map.empty
164
165 (* [push_until] is used to register [Until vis] visibility.
166
167 Example: [push_until Top.X.Y.t o (Until 1) tree [Y;X;Top]] adds the
168 value [Relative (Top.X.Y.t,o)] to occurrences [Y] and [Y.X] of
169 the tree, and [Absolute (Top.X.Y.t,o)] to occurrence [Y.X.Top] of
170 the tree. In particular, the tree now includes the following shape:
171 { map := Y |-> {map := X |-> {map := Top |-> {map := ...;
172 path := Absolute (Top.X.Y.t,o)::...}
173 ...;
174 path := Relative (Top.X.Y.t,o)::...}
175 ...;
176 path := Relative (Top.X.Y.t,o)::...}
177 ...;
178 path := ...}
179 where ... denotes what was there before.
180
181 [push_exactly] is to register [Exactly vis] and [push] chooses
182 the right one *)
183
184 let rec push_until uname o level tree = function
185 | modid :: path ->
186 let modify _ mc = push_until uname o (level-1) mc path in
187 let map =
188 try Id.Map.modify modid modify tree.map
189 with Not_found ->
190 let ptab = modify () empty_tree in
191 Id.Map.add modid ptab tree.map
192 in
193 let this =
194 if level <= 0 then
195 match tree.path with
196 | Absolute (n,_)::_ ->
197 (* This is an absolute name, we must keep it
198 otherwise it may become unaccessible forever *)
199 warn_masking_absolute n; tree.path
200 | current -> push_path (Relative (uname, o)) current
201 else tree.path
202 in
203 if this == tree.path && map == tree.map then tree
204 else mktree this map
205 | [] ->
206 match tree.path with
207 | Absolute (uname',o') :: _ ->
208 if E.equal o' o then begin
209 assert (eq_full_path uname uname');
210 tree
211 (* we are putting the same thing for the second time :) *)
212 end
213 else
214 (* This is an absolute name, we must keep it otherwise it may
215 become unaccessible forever *)
216 (* But ours is also absolute! This is an error! *)
217 CErrors.user_err
218 Pp.(str "Cannot mask the absolute name \"" ++ pr_path uname' ++ str "\"!")
219 | current ->
220 let this = push_path (Absolute (uname, o)) current in
221 if this == tree.path then tree
222 else mktree this tree.map
223
224 let rec push_exactly uname o level tree = function
225 | [] ->
226 CErrors.anomaly (Pp.str "Prefix longer than path! Impossible!")
227 | modid :: path ->
228 if Int.equal level 0 then
229 let this =
230 match tree.path with
231 | Absolute (n,_) :: _ ->
232 (* This is an absolute name, we must keep it
233 otherwise it may become unaccessible forever *)
234 warn_masking_absolute n; tree.path
235 | current -> push_path (Relative (uname, o)) current
236 in
237 if this == tree.path then tree
238 else mktree this tree.map
239 else (* not right level *)
240 let modify _ mc = push_exactly uname o (level-1) mc path in
241 let map =
242 try Id.Map.modify modid modify tree.map
243 with Not_found ->
244 let ptab = modify () empty_tree in
245 Id.Map.add modid ptab tree.map
246 in
247 if map == tree.map then tree
248 else mktree tree.path map
249
250 let push visibility uname o tab =
251 let dir,id = repr_path uname in
252 let dir = DirPath.repr dir in
253 let modify _ ptab = match visibility with
254 | Until i -> push_until uname o (i-1) ptab dir
255 | Exactly i -> push_exactly uname o (i-1) ptab dir
256 in
257 try Id.Map.modify id modify tab
258 with Not_found ->
259 let ptab = modify () empty_tree in
260 Id.Map.add id ptab tab
261
262 (** [remove_path uname tree dir] removes all bindings pointing to
263 [uname] along the path [dir] in [tree] (i.e. all such bindings are
264 assumed to be on this path) *)
265
266 let rec remove_path uname tree = function
267 | modid :: path ->
268 let update = function
269 | None -> (* The name was actually not here *) None
270 | Some mc ->
271 let mc = remove_path uname mc path in
272 if is_empty_tree mc then None else Some mc
273 in
274 let map = Id.Map.update modid update tree.map in
275 let this =
276 let test = function Relative (uname',_) -> not (eq_full_path uname uname') | _ -> true in
277 List.filter test tree.path
278 in
279 mktree this map
280 | [] ->
281 let this =
282 let test = function Absolute (uname',_) -> not (eq_full_path uname uname') | _ -> true in
283 List.filter test tree.path
284 in
285 mktree this tree.map
286
287 (** Remove all bindings pointing to [uname] in [tab] *)
288
289 let remove uname tab =
290 let dir,id = repr_path uname in
291 let dir = DirPath.repr dir in
292 let modify _ ptab = remove_path uname ptab dir in
293 try Id.Map.modify id modify tab
294 with Not_found -> tab
295
296 let rec search tree = function
297 | modid :: path ->
298 begin match Id.Map.find_opt modid tree.map with
299 | None -> None
300 | Some modid -> search modid path
301 end
302 | [] -> Some tree.path
303
304 let rec search_upto ~limit acc (cur_dist,prefix) tree path =
305 if Int.equal limit cur_dist then begin match search tree path with
306 | None -> acc
307 | Some ((Absolute (_, o) | Relative (_, o)) :: _) -> (cur_dist, List.rev_append path prefix, o) :: acc
308 | Some [] -> acc
309 end
310 else
311 match path with
312 | modid :: path ->
313 Id.Map.fold (fun modid' subtree acc ->
314 let dist = CString.edit_distance ~limit:(limit+1) (Id.to_string modid) (Id.to_string modid') in
315 if dist <= limit - cur_dist then
316 search_upto ~limit acc (cur_dist + dist, modid' :: prefix) subtree path
317 else acc)
318 tree.map acc
319 | [] ->
320 match tree.path with
321 | (Absolute (_, o) | Relative (_, o)) :: _ -> (cur_dist, prefix, o) :: acc
322 | [] -> acc
323
324 let mangle_deprecated_path dir =
325 match List.rev dir with
326 | last :: l when Id.equal last coq_id ->
327 [ (Some stdlib_id, List.rev (stdlib_id :: l));
328 (Some corelib_id, List.rev (corelib_id :: l)) ]
329 | _ -> [ None, dir ]
330
331 let search ?loc id tree dir =
332 let dirs = mangle_deprecated_path dir in
333 let search_one (warn, dir) =
334 match search tree dir with
335 | Some v -> Some (warn, v)
336 | None -> None
337 in
338 match List.find_map search_one dirs with
339 | Some (coq_repl, p) ->
340 Option.iter (fun coq_repl -> warn_deprecated_dirpath_Coq ?loc (coq_repl, dir, id)) coq_repl;
341 p
342 | None -> raise Not_found
343
344 let search_upto ~limit acc prefix tree dir =
345 let dirs = mangle_deprecated_path dir in
346 (* no warning here *)
347 List.fold_left (fun acc (_,dir) -> search_upto ~limit acc prefix tree dir) acc dirs
348
349 let find_node qid tab =
350 let loc = qid.CAst.loc in
351 let (dir,id) = repr_qualid qid in
352 search ?loc id (Id.Map.find id tab) (DirPath.repr dir)
353
354 let locate qid tab =
355 let o = match find_node qid tab with
356 | (Absolute (uname,o) | Relative (uname,o)) :: _ -> o
357 | [] -> raise Not_found
358 in
359 o
360
361 let locate_upto ~limit qid tab =
362 let (dir,id) = repr_qualid qid in
363 let res =
364 Id.Map.fold (fun id' tree acc ->
365 let dist = CString.edit_distance ~limit:(limit+1) (Id.to_string id) (Id.to_string id') in
366 if dist <= limit then
367 search_upto ~limit acc (dist, [id']) tree (DirPath.repr dir)
368 else acc)
369 tab
370 []
371 in
372 let res = List.sort (fun (x,_,_) (y,_,_) -> Int.compare x y) res in
373 List.map (fun (_,x,y) ->
374 let x = match List.rev x with
375 | [] -> assert false
376 | x :: lx -> Libnames.make_qualid (DirPath.make lx) x
377 in
378 x, y)
379 res
380
381 let find uname tab =
382 let l,id = repr_path uname in
383 let l = DirPath.repr l in
384 match search id (Id.Map.find id tab) l with
385 | Absolute (_,o) :: _ -> o
386 | _ -> raise Not_found
387
388 let exists uname tab =
389 try
390 let _ = find uname tab in
391 true
392 with
393 Not_found -> false
394
395 let shortest_qualid_gen ?loc ?(force_short=false) hidden uname tab =
396 if not force_short && !print_fully_qualified_ref then qualid_of_path ?loc uname
397 else
398 let dir,id = repr_path uname in
399 let dir = DirPath.repr dir in
400 let hidden = hidden id in
401 let rec find_uname pos dir tree =
402 let is_empty = match pos with [] -> true | _ -> false in
403 match tree.path with
404 | (Absolute (u,_) | Relative (u,_)) :: _
405 when eq_full_path u uname && not (is_empty && hidden) -> List.rev pos
406 | _ ->
407 match dir with
408 [] -> raise Not_found
409 | id::dir -> find_uname (id::pos) dir (Id.Map.find id tree.map)
410 in
411 let ptab = Id.Map.find id tab in
412 let found_dir = find_uname [] dir ptab in
413 make_qualid ?loc (DirPath.make found_dir) id
414
415 let push_node node l =
416 match node with
417 | (Absolute (_,o) | Relative (_,o)) :: _
418 when not (Util.List.mem_f E.equal o l) -> o::l
419 | _ -> l
420
421 let rec flatten_tree tree l =
422 let f _ tree l = flatten_tree tree l in
423 Id.Map.fold f tree.map (push_node tree.path l)
424
425 let rec search_prefixes tree = function
426 | modid :: path -> search_prefixes (Id.Map.find modid tree.map) path
427 | [] -> List.rev (flatten_tree tree [])
428
429 let find_prefixes qid tab =
430 try
431 let (dir,id) = repr_qualid qid in
432 search_prefixes (Id.Map.find id tab) (DirPath.repr dir)
433 with Not_found -> []
434
435 let match_prefixes =
436 let cprefix x y = CString.(compare x (sub y 0 (min (length x) (length y)))) in
437 fun qid tab ->
438 try
439 let (dir,id) = repr_qualid qid in
440 let id_prefix = cprefix Id.(to_string id) in
441 let matches = Id.Map.filter_range (fun x -> id_prefix Id.(to_string x)) tab in
442 let matches = Id.Map.mapi (fun _key tab -> search_prefixes tab (DirPath.repr dir)) matches in
443 (* Rocq's flatten is "magical", so this is not so bad perf-wise *)
444 CList.flatten @@ Id.Map.(fold (fun _ r l -> r :: l) matches [])
445 with Not_found -> []
446
447 end
448
449 module type NAMETAB_gen = sig
450 type 'a read_tab
451 type write_tab
452
453 type elt
454
455 val push : visibility -> full_path -> elt -> write_tab
456
457 val remove : full_path -> elt -> write_tab
458
459 val shortest_qualid_gen : ?loc:Loc.t -> ?force_short:bool -> (Id.t -> bool) -> elt -> qualid read_tab
460
461 val shortest_qualid : ?loc:Loc.t -> ?force_short:bool -> Id.Set.t -> elt -> qualid read_tab
462
463 val pr : elt -> Pp.t read_tab
464
465 val locate : qualid -> elt read_tab
466
467 val locate_upto : limit:int -> qualid -> (qualid * elt) list read_tab
468
469 val locate_all : qualid -> elt list read_tab
470
471 val completion_candidates : qualid -> elt list read_tab
472
473 val to_path : elt -> full_path read_tab
474
475 val of_path : full_path -> elt read_tab
476
477 val exists : full_path -> bool read_tab
478
479 (* XXX add src_loc table? *)
480 end
481
482 module type ValueType = sig
483 include EqualityType
484
485 val is_var : t -> Id.t option
486
487 module Map : CSig.UMapS with type key = t
488 end
489
490 module type WarnInfo = sig
491 type elt
492 type data
493
494 module Map : CSig.UMapS with type key = elt
495
496 val stage : Summary.Stage.t
497 val summary_name : string
498
499 val warn : ?loc:Loc.t -> elt -> data -> unit
500 end
501
502 module type Functional_NAMETAB = sig
503 type t
504 val empty : t
505 include NAMETAB_gen with type 'a read_tab := t -> 'a and type write_tab := t -> t
506 end
507
508 module MakeTab (E:ValueType) : Functional_NAMETAB with type elt = E.t = struct
509 type elt = E.t
510
511 module Tab = Make(E)
512
513 type t = {
514 tab : Tab.t;
515 revtab : full_path E.Map.t;
516 }
517
518 let empty = {
519 tab = Tab.empty;
520 revtab = E.Map.empty;
521 }
522
523 let push vis sp v { tab; revtab } =
524 match vis with
525 | Until _ ->
526 {
527 tab = Tab.push vis sp v tab;
528 revtab = E.Map.add v sp revtab;
529 }
530 | Exactly _ ->
531 {
532 tab = Tab.push vis sp v tab;
533 revtab;
534 }
535
536 let remove sp v { tab; revtab } =
537 {
538 tab = Tab.remove sp tab;
539 revtab = E.Map.remove v revtab;
540 }
541
542 let shortest_qualid_gen ?loc ?force_short avoid v { tab; revtab } =
543 match E.is_var v with
544 | Some id -> make_qualid ?loc DirPath.empty id
545 | None ->
546 let sp = E.Map.find v revtab in
547 Tab.shortest_qualid_gen ?loc ?force_short avoid sp tab
548
549 let shortest_qualid ?loc ?force_short avoid v tabs =
550 shortest_qualid_gen ?loc ?force_short (fun id -> Id.Set.mem id avoid) v tabs
551
552 let pr v tab = pr_qualid (shortest_qualid Id.Set.empty v tab)
553
554 let locate qid { tab } = Tab.locate qid tab
555
556 let locate_upto ~limit qid { tab } = Tab.locate_upto ~limit qid tab
557
558 let locate_all qid { tab } = Tab.find_prefixes qid tab
559
560 let completion_candidates qid { tab } = Tab.match_prefixes qid tab
561
562 let to_path v { revtab } = match E.is_var v with
563 | Some id -> make_path DirPath.empty id
564 | None -> E.Map.find v revtab
565
566 let of_path sp { tab } = Tab.find sp tab
567
568 let exists sp { tab } = Tab.exists sp tab
569
570 end
571
572 module type NAMETAB = NAMETAB_gen with type 'a read_tab := 'a and type write_tab := unit
573
574 module type StateInfo = sig
575 val stage : Summary.Stage.t
576 val summary_name : string
577 end
578
579 module MakeImperative (Tab:Functional_NAMETAB) (SI:StateInfo) ()
580 : NAMETAB with type elt = Tab.elt
581 = struct
582 type elt = Tab.elt
583
584 let the_tab = Summary.ref ~stage:SI.stage ~name:SI.summary_name Tab.empty
585
586 let push vis sp v = the_tab := Tab.push vis sp v !the_tab
587
588 let remove sp v = the_tab := Tab.remove sp v !the_tab
589
590 let shortest_qualid_gen ?loc ?force_short f v = Tab.shortest_qualid_gen ?loc ?force_short f v !the_tab
591
592 let shortest_qualid ?loc ?force_short avoid v = Tab.shortest_qualid ?loc ?force_short avoid v !the_tab
593
594 let pr v = Tab.pr v !the_tab
595
596 let locate qid = Tab.locate qid !the_tab
597
598 let locate_upto ~limit qid = Tab.locate_upto ~limit qid !the_tab
599
600 let locate_all qid = Tab.locate_all qid !the_tab
601
602 let completion_candidates qid = Tab.completion_candidates qid !the_tab
603
604 let to_path v = Tab.to_path v !the_tab
605
606 let of_path v = Tab.of_path v !the_tab
607
608 let exists v = Tab.exists v !the_tab
609 end
610
611 module type WarnedTab = sig
612 include NAMETAB
613
614 type warning_data
615
616 val push : ?wdata:warning_data -> visibility -> full_path -> elt -> unit
617 val is_warned : elt -> warning_data option
618 val warn : ?loc:Loc.t -> elt -> warning_data -> unit
619 val locate : ?nowarn:bool -> qualid -> elt
620 end
621
622 module MakeWarned (M:NAMETAB)
623 (W:WarnInfo with type elt = M.elt)
624 ()
625 : WarnedTab with type elt = M.elt and type warning_data := W.data
626 = struct
627 include M
628
629 let warntab = Summary.ref ~stage:W.stage ~name:W.summary_name W.Map.empty
630
631 let push ?wdata vis sp elt =
632 M.push vis sp elt;
633 match wdata with
634 | None -> ()
635 | Some wdata ->
636 let () = match vis with
637 | Until _ -> ()
638 | Exactly _ -> assert false
639 in
640 warntab := W.Map.add elt wdata !warntab
641
642 let remove sp elt =
643 M.remove sp elt;
644 warntab := W.Map.remove elt !warntab
645
646 let is_warned elt = W.Map.find_opt elt !warntab
647
648 let warn = W.warn
649
650 let locate ?(nowarn=false) qid =
651 let elt = M.locate qid in
652 let () = if nowarn then ()
653 else match is_warned elt with
654 | None -> ()
655 | Some wdata -> warn ?loc:qid.loc elt wdata
656 in
657 elt
658 end
659
660 module EasyNoWarn (M:sig include ValueType include StateInfo end) () =
661 MakeImperative(MakeTab(M))(M)()
662
663 module type SimpleWarnS = sig
664 val object_name : string
665 val warning_name_base : string
666 end
667
668 module Easy (M:sig include ValueType include StateInfo include SimpleWarnS end) () = struct
669 module I = EasyNoWarn(M)()
670
671 module WInfo = struct
672 type elt = M.t
673 type data = elt UserWarn.with_qf
674
675 module Map = M.Map
676
677 let stage = M.stage
678 let summary_name = M.summary_name ^ "-warnings"
679
680 let warn = UserWarn.create_depr_and_user_warnings_qf
681 ~object_name:M.object_name ~warning_name_base:M.warning_name_base
682 ~pp_qf:I.pr I.pr
683 ()
684 end
685
686 include MakeWarned(I)(WInfo)()
687 end
688
689 (* Global name tables *************************************************)
690
691 module XRefV = struct
692 include ExtRefOrdered
693 let is_var = function
694 | TrueGlobal (VarRef id) -> Some id
695 | _ -> None
696
697 module Map = ExtRefMap
698
699 let stage = Summary.Stage.Interp
700 let summary_name = "xreftab"
701 end
702 module XRefNoWarn = EasyNoWarn(XRefV)()
703
704 module XRefWarn = struct
705 type elt = extended_global_reference
706 type data = elt UserWarn.with_qf
707
708 module Map = ExtRefMap
709
710 let pp = XRefNoWarn.pr
711
712 let depr_ref = Deprecation.create_warning_with_qf ~object_name:"Reference" ~warning_name_if_no_since:"deprecated-reference" ~pp_qf:pp pp
713 let depr_abbrev = Deprecation.create_warning_with_qf ~object_name:"Notation" ~warning_name_if_no_since:"deprecated-syntactic-definition" ~pp_qf:pp pp
714 let user_warn = UserWarn.create_warning ~warning_name_if_no_cats:"warn-reference" ()
715
716 let depr_xref ?loc depr xref =
717 match xref with
718 | TrueGlobal _ -> depr_ref ?loc (xref,depr)
719 | Abbrev _ -> depr_abbrev ?loc (xref,depr)
720
721 let warn ?loc xref uwarns =
722 uwarns.UserWarn.depr_qf |> Option.iter (fun depr -> depr_xref ?loc depr xref);
723 uwarns.UserWarn.warn_qf |> List.iter (user_warn ?loc)
724
725 let stage = Summary.Stage.Interp
726 let summary_name = "xrefwarntab"
727 end
728
729 module XRefs = MakeWarned(XRefNoWarn)(XRefWarn)()
730
731 module UnivsV = struct
732 include Univ.UGlobal
733 let is_var _ = None
734 module Map = HMap.Make(Univ.UGlobal)
735 let stage = Summary.Stage.Interp
736 let summary_name = "univtab"
737 end
738 module Univs = EasyNoWarn(UnivsV)()
739
740 module QualityV = struct
741 include Sorts.QGlobal
742 let is_var _ = None
743 module Map = HMap.Make(Sorts.QGlobal)
744 let stage = Summary.Stage.Interp
745 let summary_name = "sorttab"
746 end
747 module Quality = EasyNoWarn(QualityV)()
748
749 module ModTypeV = struct
750 include ModPath
751 let is_var _ = None
752 module Map = ModPath.Map
753 let stage = Summary.Stage.Synterp
754 let summary_name = "modtypetab"
755 end
756 module ModTypes = EasyNoWarn(ModTypeV)()
757
758 module ModuleV = struct
759 include ModPath
760 let is_var _ = None
761 module Map = ModPath.Map
762 let stage = Summary.Stage.Synterp
763 let summary_name = "moduletab"
764 end
765 module Modules = EasyNoWarn(ModuleV)()
766
767 module OpenModV = struct
768 include GlobDirRef
769 let is_var _ = None
770 module Map = Map.Make(GlobDirRef)
771 let stage = Summary.Stage.Synterp
772 let summary_name = "openmodtab"
773 end
774 module OpenMods = EasyNoWarn(OpenModV)()
775
776 module CustomEntriesV = struct
777 include CustomName
778 let is_var _ = None
779 let stage = Summary.Stage.Synterp
780 let summary_name = "customentrytab"
781 end
782 module CustomEntries = EasyNoWarn(CustomEntriesV)()
783
784 (* Push functions *********************************************************)
785
786 let push_abbreviation ?user_warns visibility sp kn =
787 XRefs.push ?wdata:user_warns visibility sp (Abbrev kn)
788
789 let remove_abbreviation sp kn =
790 XRefs.remove sp (Abbrev kn)
791
792 let push ?user_warns vis sp kn = XRefs.push ?wdata:user_warns vis sp (TrueGlobal kn)
793
794 let push_modtype vis sp kn = ModTypes.push vis sp kn
795
796 let push_module vis dir mp = Modules.push vis dir mp
797
798 let push_dir vis dir dir_ref = OpenMods.push vis dir dir_ref
799
800 let push_universe vis sp univ = Univs.push vis sp univ
801
802 (* Reverse locate functions ***********************************************)
803
804 let path_of_global ref = XRefs.to_path (TrueGlobal ref)
805
806 let dirpath_of_global ref =
807 fst (repr_path (path_of_global ref))
808
809 let basename_of_global ref =
810 snd (repr_path (path_of_global ref))
811
812 let path_of_abbreviation kn = XRefs.to_path (Abbrev kn)
813
814 let path_of_module mp = Modules.to_path mp
815
816 let path_of_modtype mp = ModTypes.to_path mp
817
818 let path_of_universe mp = Univs.to_path mp
819
820 (* Shortest qualid functions **********************************************)
821
822 let shortest_qualid_of_global ?loc ?force_short ctx ref =
823 XRefs.shortest_qualid ?loc ?force_short ctx (TrueGlobal ref)
824
825 let shortest_qualid_of_abbreviation ?loc ?force_short ctx kn =
826 XRefs.shortest_qualid ?loc ?force_short ctx (Abbrev kn)
827
828 let shortest_qualid_of_module ?loc ?force_short mp =
829 Modules.shortest_qualid ?loc ?force_short Id.Set.empty mp
830
831 let shortest_qualid_of_modtype ?loc ?force_short kn =
832 ModTypes.shortest_qualid ?loc ?force_short Id.Set.empty kn
833
834 let shortest_qualid_of_dir ?loc ?force_short sp =
835 OpenMods.shortest_qualid ?loc ?force_short Id.Set.empty sp
836
837 let shortest_qualid_of_universe ?loc ?force_short avoid u =
838 Univs.shortest_qualid_gen ?loc ?force_short (fun id -> Id.Map.mem id avoid) u
839
840 let pr_global_env env ref =
841 try pr_qualid (shortest_qualid_of_global env ref)
842 with Not_found as exn ->
843 let exn, info = Exninfo.capture exn in
844 if !Flags.in_debugger then GlobRef.print ref
845 else begin
846 let () = if CDebug.(get_flag misc)
847 then Feedback.msg_debug (Pp.str "pr_global_env not found")
848 in
849 Exninfo.iraise (exn, info)
850 end
851
852 let is_warned_xref = XRefs.is_warned
853
854 let warn_user_warn_xref ?loc wdata x = XRefs.warn ?loc x wdata
855
856 let locate_extended_nowarn qid = XRefs.locate ~nowarn:true qid
857
858 let locate_extended qid = XRefs.locate qid
859
860 let locate qid = match locate_extended qid with
861 | TrueGlobal ref -> ref
862 | Abbrev _ -> raise Not_found
863
864 let locate_abbreviation qid = match locate_extended qid with
865 | TrueGlobal _ -> raise Not_found
866 | Abbrev kn -> kn
867
868 let locate_modtype qid = ModTypes.locate qid
869 let full_name_modtype qid = ModTypes.to_path (locate_modtype qid)
870
871 let locate_universe qid = Univs.locate qid
872
873 let locate_dir qid = OpenMods.locate qid
874
875 let locate_module qid = Modules.locate qid
876
877 let full_name_module qid = Modules.to_path (locate_module qid)
878
879 let full_name_open_mod qid = OpenMods.to_path (locate_dir qid)
880
881 let locate_section qid =
882 match locate_dir qid with
883 | GlobDirRef.DirOpenSection dir -> dir
884 | _ -> raise Not_found
885
886 let locate_extended_all qid = XRefs.locate_all qid
887
888 let locate_all qid =
889 let l = locate_extended_all qid in
890 CList.filter_map (function TrueGlobal a -> Some a | Abbrev _ -> None) l
891
892 let locate_extended_all_dir qid = OpenMods.locate_all qid
893
894 let locate_extended_all_modtype qid = ModTypes.locate_all qid
895
896 let locate_extended_all_module qid = Modules.locate_all qid
897
898 (* Completion *)
899 let completion_canditates qualid = XRefs.completion_candidates qualid
900
901 (* Derived functions *)
902
903 let locate_constant qid =
904 let open GlobRef in
905 match locate_extended qid with
906 | TrueGlobal (ConstRef kn) -> kn
907 | _ -> raise Not_found
908
909 let global_of_path sp =
910 match XRefs.of_path sp with
911 | TrueGlobal ref -> ref
912 | _ -> raise Not_found
913
914 let extended_global_of_path = XRefs.of_path
915
916 let global qid =
917 try match locate_extended qid with
918 | TrueGlobal ref -> ref
919 | Abbrev _ ->
920 CErrors.user_err ?loc:qid.CAst.loc
921 Pp.(str "Unexpected reference to a notation: " ++
922 pr_qualid qid ++ str ".")
923 with Not_found as exn ->
924 let _, info = Exninfo.capture exn in
925 error_global_not_found ~info qid
926
927 let global_inductive qid =
928 let open GlobRef in
929 match global qid with
930 | IndRef ind -> ind
931 | ref ->
932 CErrors.user_err ?loc:qid.CAst.loc
933 Pp.(pr_qualid qid ++ spc () ++ str "is not an inductive type.")
934
935 (* Exists functions ********************************************************)
936
937 let exists_cci = XRefs.exists
938
939 let exists_dir = OpenMods.exists
940
941 let exists_module = Modules.exists
942
943 let exists_modtype = ModTypes.exists
944
945 let exists_universe = Univs.exists
946
947 (* Source locations *)
948
949 open Globnames
950
951 let cci_loc_table : Loc.t ExtRefMap.t ref = Summary.ref ~name:"constant-loc-table" ExtRefMap.empty
952
953 let set_cci_src_loc kn loc = cci_loc_table := ExtRefMap.add kn loc !cci_loc_table
954
955 let cci_src_loc kn = ExtRefMap.find_opt kn !cci_loc_table