| 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 | |
| 14 | (** This module contains the tables for globalization. *) |
| 15 | |
| 16 | (** These globalization tables associate internal object references to |
| 17 | qualified names (qualid). There are three classes of names: |
| 18 | |
| 19 | - 1a) internal kernel names: [kernel_name], [constant], [inductive], |
| 20 | [module_path], [DirPath.t] |
| 21 | |
| 22 | - 1b) other internal names: [global_reference], [abbreviation], |
| 23 | [extended_global_reference], [global_dir_reference], ... |
| 24 | |
| 25 | - 2) full, non ambiguous user names: [full_path] |
| 26 | |
| 27 | - 3) non necessarily full, possibly ambiguous user names: [reference] |
| 28 | and [qualid] |
| 29 | *) |
| 30 | |
| 31 | (** Most functions in this module fall into one of the following categories: |
| 32 | {ul {- [push : visibility -> full_user_name -> object_reference -> unit] |
| 33 | |
| 34 | Registers the [object_reference] to be referred to by the |
| 35 | [full_user_name] (and its suffixes according to [visibility]). |
| 36 | [full_user_name] can either be a [full_path] or a [DirPath.t]. |
| 37 | } |
| 38 | {- [exists : full_user_name -> bool] |
| 39 | |
| 40 | Is the [full_user_name] already attributed as an absolute user name |
| 41 | of some object? |
| 42 | } |
| 43 | {- [locate : qualid -> object_reference] |
| 44 | |
| 45 | Finds the object referred to by [qualid] or raises [Not_found] |
| 46 | } |
| 47 | {- [full_name : qualid -> full_user_name] |
| 48 | |
| 49 | Finds the full user name referred to by [qualid] or raises [Not_found] |
| 50 | } |
| 51 | {- [shortest_qualid_of : object_reference -> user_name] |
| 52 | |
| 53 | The [user_name] can be for example the shortest non ambiguous [qualid] or |
| 54 | the [full_user_name] or [Id.t]. Such a function can also have a |
| 55 | local context argument.}} |
| 56 | |
| 57 | *) |
| 58 | |
| 59 | (** to this type are mapped [DirPath.t]'s in the nametab *) |
| 60 | module GlobDirRef : sig |
| 61 | type t = |
| 62 | | DirOpenModule of ModPath.t |
| 63 | | DirOpenModtype of ModPath.t |
| 64 | | DirOpenSection of full_path |
| 65 | val equal : t -> t -> bool |
| 66 | end |
| 67 | |
| 68 | exception GlobalizationError of qualid |
| 69 | |
| 70 | (** Raises a globalization error *) |
| 71 | val error_global_not_found : info:Exninfo.info -> qualid -> 'a |
| 72 | |
| 73 | (** {6 Register visibility of things } *) |
| 74 | |
| 75 | (** The visibility can be registered either |
| 76 | - for all suffixes not shorter then a given int -- when the |
| 77 | object is loaded inside a module -- or |
| 78 | - for a precise suffix, when the module containing (the module |
| 79 | containing ...) the object is opened (imported) |
| 80 | |
| 81 | *) |
| 82 | |
| 83 | type visibility = Until of int | Exactly of int |
| 84 | |
| 85 | val map_visibility : (int -> int) -> visibility -> visibility |
| 86 | |
| 87 | (** {6 Nametab generic APIs } *) |
| 88 | |
| 89 | module type NAMETAB = sig |
| 90 | (** Common APIs on name tables. *) |
| 91 | |
| 92 | type elt |
| 93 | (** The type of the elements of the name table (eg ModPath.t) *) |
| 94 | |
| 95 | val push : visibility -> full_path -> elt -> unit |
| 96 | (** Add an element or modify its visibility. |
| 97 | With visibility [Until], assume the element is new. |
| 98 | With visibility [Exactly], assume the element is not new. |
| 99 | *) |
| 100 | |
| 101 | val remove : full_path -> elt -> unit |
| 102 | (** Remove an element. *) |
| 103 | |
| 104 | val shortest_qualid_gen : ?loc:Loc.t -> ?force_short:bool -> (Id.t -> bool) -> elt -> qualid |
| 105 | (** [shortest_qualid_gen avoid v]: given an object [v] with full |
| 106 | name Mylib.A.B.x, try to find the shortest among x, B.x, A.B.x |
| 107 | and Mylib.A.B.x that denotes the same object. |
| 108 | If [avoid] is [true] on [x] it is assumed to denote something else |
| 109 | (so B.x is the shortest qualid that could be returned). |
| 110 | If [~force_short:true] is passed, always returns the shortest qualid. |
| 111 | Otherwise, respects the "Printing Fully Qualified" flag. |
| 112 | |
| 113 | @raise Not_found for unknown objects. *) |
| 114 | |
| 115 | val shortest_qualid : ?loc:Loc.t -> ?force_short:bool -> Id.Set.t -> elt -> qualid |
| 116 | (** Like [shortest_qualid_gen] but using an id set instead of a closure. *) |
| 117 | |
| 118 | val pr : elt -> Pp.t |
| 119 | (** Print using [shortest_qualid] and empty avoid set. *) |
| 120 | |
| 121 | val locate : qualid -> elt |
| 122 | (** Globalize the given user-level reference. |
| 123 | If any warnings are associated with the produced object, print |
| 124 | them unless [nowarn:true] was given. *) |
| 125 | |
| 126 | val locate_upto : limit:int -> qualid -> (qualid * elt) list |
| 127 | (** Find elements which are [limit] different from the input (dots |
| 128 | are treated rigidly). *) |
| 129 | |
| 130 | val locate_all : qualid -> elt list |
| 131 | (** Locate all elements with a given suffix. If the qualid is a |
| 132 | valid absolute name, that element is first in the list. *) |
| 133 | |
| 134 | val completion_candidates : qualid -> elt list |
| 135 | (** Return the elements that have the qualid as a prefix. |
| 136 | UIs will usually compose with [shortest_qualid]. |
| 137 | Experimental APIs, please tell us if you use it. *) |
| 138 | |
| 139 | val to_path : elt -> full_path |
| 140 | (** Return the absolute name for the given element. *) |
| 141 | |
| 142 | val of_path : full_path -> elt |
| 143 | (** Return an element bound to an absolute path. *) |
| 144 | |
| 145 | val exists : full_path -> bool |
| 146 | (** Returns whether this absolute path is taken. *) |
| 147 | end |
| 148 | |
| 149 | module type WarnedTab = sig |
| 150 | (** NAMETAB extended with warnings associated to some of the elements. *) |
| 151 | |
| 152 | include NAMETAB |
| 153 | |
| 154 | type warning_data |
| 155 | |
| 156 | val push : ?wdata:warning_data -> visibility -> full_path -> elt -> unit |
| 157 | (** When [visibility] is [Until], [wdata] may be [Some _] in which |
| 158 | case the warning data is associated to the element. *) |
| 159 | |
| 160 | val is_warned : elt -> warning_data option |
| 161 | (** Return the warning data associated to the element. *) |
| 162 | |
| 163 | val warn : ?loc:Loc.t -> elt -> warning_data -> unit |
| 164 | (** Emit the given warning data for the given element (if the warning is not disabled). *) |
| 165 | |
| 166 | val locate : ?nowarn:bool -> qualid -> elt |
| 167 | (** Unless [nowarn:false] is given, if the found element is |
| 168 | associated to a warning, that warning is emitted. *) |
| 169 | end |
| 170 | |
| 171 | (** {6 Core nametabs } *) |
| 172 | |
| 173 | module XRefs : WarnedTab |
| 174 | with type elt = Globnames.extended_global_reference |
| 175 | and type warning_data := Globnames.extended_global_reference UserWarn.with_qf |
| 176 | |
| 177 | module Univs : NAMETAB with type elt = Univ.UGlobal.t |
| 178 | |
| 179 | module Quality : NAMETAB with type elt = Sorts.QGlobal.t |
| 180 | |
| 181 | (** Module types, modules and open modules/modtypes/sections form three separate name spaces |
| 182 | (maybe this will change someday) *) |
| 183 | module ModTypes : NAMETAB with type elt = ModPath.t |
| 184 | |
| 185 | module Modules : NAMETAB with type elt = ModPath.t |
| 186 | |
| 187 | module OpenMods : NAMETAB with type elt = GlobDirRef.t |
| 188 | |
| 189 | module CustomEntries : NAMETAB with type elt = Globnames.CustomName.t |
| 190 | (** Do not directly use [locate] with this, instead use the compat |
| 191 | layer [Metasyntax.intern_custom_name]. *) |
| 192 | |
| 193 | (** {6 Specializations for extended references } *) |
| 194 | |
| 195 | (** These functions operate on [XRefs] but are about a subset of |
| 196 | [extended_global_reference] for convenience. *) |
| 197 | |
| 198 | val push : ?user_warns:Globnames.extended_global_reference UserWarn.with_qf -> visibility -> full_path -> GlobRef.t -> unit |
| 199 | |
| 200 | val push_abbreviation : ?user_warns:Globnames.extended_global_reference UserWarn.with_qf -> visibility -> full_path -> Globnames.abbreviation -> unit |
| 201 | |
| 202 | val locate : qualid -> GlobRef.t |
| 203 | |
| 204 | val locate_all : qualid -> GlobRef.t list |
| 205 | |
| 206 | val locate_constant : qualid -> Constant.t |
| 207 | |
| 208 | val locate_abbreviation : qualid -> Globnames.abbreviation |
| 209 | |
| 210 | val remove_abbreviation : full_path -> Globnames.abbreviation -> unit |
| 211 | |
| 212 | val global_of_path : full_path -> GlobRef.t |
| 213 | |
| 214 | val path_of_global : GlobRef.t -> full_path |
| 215 | |
| 216 | val path_of_abbreviation : Globnames.abbreviation -> full_path |
| 217 | |
| 218 | val shortest_qualid_of_global : ?loc:Loc.t -> ?force_short:bool -> Id.Set.t -> GlobRef.t -> qualid |
| 219 | (** Returns a qualid for the given global reference. If [~force_short:true] is |
| 220 | passed, always returns the shortest qualid. Otherwise (default), respects |
| 221 | the "Printing Fully Qualified" flag: when the flag is set, returns the fully |
| 222 | qualified name; when unset, returns the shortest qualid. *) |
| 223 | |
| 224 | val shortest_qualid_of_abbreviation : ?loc:Loc.t -> ?force_short:bool -> Id.Set.t -> Globnames.abbreviation -> qualid |
| 225 | |
| 226 | (** Printing of global references using names as short as possible. |
| 227 | @raise Not_found when the reference is not in the global tables. *) |
| 228 | val pr_global_env : Id.Set.t -> GlobRef.t -> Pp.t |
| 229 | |
| 230 | (** Fully qualified printing control. When enabled, [pr_global_env] uses |
| 231 | fully qualified names instead of shortest names. The flag is controlled |
| 232 | by the "Printing Fully Qualified" option declared in printer.ml. *) |
| 233 | val print_fully_qualified : unit -> bool |
| 234 | val set_print_fully_qualified : bool -> unit |
| 235 | |
| 236 | (** Returns in particular the dirpath or the basename of the full path |
| 237 | associated to global reference *) |
| 238 | |
| 239 | val dirpath_of_global : GlobRef.t -> DirPath.t |
| 240 | val basename_of_global : GlobRef.t -> Id.t |
| 241 | |
| 242 | (** These functions globalize user-level references into global |
| 243 | references, like [locate] and co, but raise [GlobalizationError] |
| 244 | for unbound qualid and [UserError] for type mismatches (eg |
| 245 | [global_inductive] when the qualid is bound to a constructor) *) |
| 246 | |
| 247 | val global : qualid -> GlobRef.t |
| 248 | val global_inductive : qualid -> inductive |
| 249 | |
| 250 | (** {6 These functions declare (resp. return) the source location of the object if known } *) |
| 251 | (* XXX handle as part of the general nametab API? *) |
| 252 | |
| 253 | val set_cci_src_loc : Globnames.extended_global_reference -> Loc.t -> unit |
| 254 | val cci_src_loc : Globnames.extended_global_reference -> Loc.t option |
| 255 | |
| 256 | (** {6 Nametab generators } *) |
| 257 | |
| 258 | module type ValueType = sig |
| 259 | type t |
| 260 | val equal : t -> t -> bool |
| 261 | |
| 262 | val is_var : t -> Id.t option |
| 263 | (** Is this a variable (not handled by name tables)? *) |
| 264 | |
| 265 | module Map : CSig.UMapS with type key = t |
| 266 | end |
| 267 | |
| 268 | module type StateInfo = sig |
| 269 | (** Necessary data to declare the imperative state for a nametab. *) |
| 270 | |
| 271 | val stage : Summary.Stage.t |
| 272 | val summary_name : string |
| 273 | end |
| 274 | |
| 275 | (** Generate a nametab, without warning support. *) |
| 276 | module EasyNoWarn (M:sig include ValueType include StateInfo end) () |
| 277 | : NAMETAB with type elt = M.t |
| 278 | |
| 279 | module type WarnInfo = sig |
| 280 | type elt |
| 281 | type data |
| 282 | |
| 283 | module Map : CSig.UMapS with type key = elt |
| 284 | |
| 285 | val stage : Summary.Stage.t |
| 286 | val summary_name : string |
| 287 | (** NB: This summary name must NOT be the same as the nametab summary name. *) |
| 288 | |
| 289 | val warn : ?loc:Loc.t -> elt -> data -> unit |
| 290 | end |
| 291 | |
| 292 | (** Add warning support to an existing nametab. *) |
| 293 | module MakeWarned(M:NAMETAB)(W:WarnInfo with type elt = M.elt) () |
| 294 | : WarnedTab with type elt = M.elt and type warning_data := W.data |
| 295 | |
| 296 | module type SimpleWarnS = sig |
| 297 | (** Necessary data to declare a simple warning ([UserWarn.create_depr_and_user_warnings_qf]). *) |
| 298 | val object_name : string |
| 299 | val warning_name_base : string |
| 300 | end |
| 301 | |
| 302 | (** Generate a nametab with simple warning support |
| 303 | ([UserWarn.create_depr_and_user_warnings_qf]). *) |
| 304 | module Easy (M:sig include ValueType include StateInfo include SimpleWarnS end) () |
| 305 | : WarnedTab with type elt = M.t and type warning_data := M.t UserWarn.with_qf |
| 306 | |
| 307 | (** {6 legacy APIs } *) |
| 308 | (* XXX deprecate? *) |
| 309 | |
| 310 | val push_modtype : visibility -> full_path -> ModPath.t -> unit |
| 311 | val push_module : visibility -> full_path -> ModPath.t -> unit |
| 312 | val push_dir : visibility -> full_path -> GlobDirRef.t -> unit |
| 313 | |
| 314 | val push_universe : visibility -> full_path -> Univ.UGlobal.t -> unit |
| 315 | |
| 316 | (** Deprecation and user warn info *) |
| 317 | |
| 318 | val is_warned_xref : Globnames.extended_global_reference -> Globnames.extended_global_reference UserWarn.with_qf option |
| 319 | |
| 320 | val warn_user_warn_xref : ?loc:Loc.t -> Globnames.extended_global_reference UserWarn.with_qf -> Globnames.extended_global_reference -> unit |
| 321 | |
| 322 | (** {6 The following functions perform globalization of qualified names } *) |
| 323 | |
| 324 | (** These functions globalize a (partially) qualified name or fail with |
| 325 | [Not_found] *) |
| 326 | |
| 327 | val locate_extended : qualid -> Globnames.extended_global_reference |
| 328 | val locate_modtype : qualid -> ModPath.t |
| 329 | val locate_dir : qualid -> GlobDirRef.t |
| 330 | val locate_module : qualid -> ModPath.t |
| 331 | val locate_section : qualid -> full_path |
| 332 | val locate_universe : qualid -> Univ.UGlobal.t |
| 333 | |
| 334 | val locate_extended_nowarn : qualid -> Globnames.extended_global_reference |
| 335 | |
| 336 | (** These functions locate all global references with a given suffix; |
| 337 | if [qualid] is valid as such, it comes first in the list *) |
| 338 | |
| 339 | val locate_extended_all : qualid -> Globnames.extended_global_reference list |
| 340 | val locate_extended_all_dir : qualid -> GlobDirRef.t list |
| 341 | val locate_extended_all_modtype : qualid -> ModPath.t list |
| 342 | val locate_extended_all_module : qualid -> ModPath.t list |
| 343 | |
| 344 | (** Experimental completion support, API is _unstable_ *) |
| 345 | val completion_canditates : qualid -> Globnames.extended_global_reference list |
| 346 | (** [completion_canditates qualid] will return the list of global |
| 347 | references that have [qualid] as a prefix. UI usually will want to |
| 348 | compose this with [shortest_qualid_of_global] *) |
| 349 | |
| 350 | (** Mapping a full path to a global reference *) |
| 351 | |
| 352 | val extended_global_of_path : full_path -> Globnames.extended_global_reference |
| 353 | |
| 354 | (** {6 These functions tell if the given absolute name is already taken } *) |
| 355 | |
| 356 | val exists_cci : full_path -> bool |
| 357 | val exists_modtype : full_path -> bool |
| 358 | val exists_module : full_path -> bool |
| 359 | val exists_dir : full_path -> bool |
| 360 | val exists_universe : full_path -> bool |
| 361 | |
| 362 | (** {6 These functions locate qualids into full user names } *) |
| 363 | |
| 364 | val full_name_modtype : qualid -> full_path |
| 365 | val full_name_module : qualid -> full_path |
| 366 | val full_name_open_mod : qualid -> full_path |
| 367 | |
| 368 | (** {6 Reverse lookup } |
| 369 | Finding user names corresponding to the given |
| 370 | internal name *) |
| 371 | |
| 372 | (** Returns the full path bound to a global reference or syntactic |
| 373 | definition, and the (full) dirpath associated to a module path *) |
| 374 | |
| 375 | val path_of_module : ModPath.t -> full_path |
| 376 | val path_of_modtype : ModPath.t -> full_path |
| 377 | |
| 378 | (** A universe_id might not be registered with a corresponding user name. |
| 379 | @raise Not_found if the universe was not introduced by the user. *) |
| 380 | val path_of_universe : Univ.UGlobal.t -> full_path |
| 381 | |
| 382 | (** The [shortest_qualid] functions given an object with [user_name] |
| 383 | Mylib.A.B.x, try to find the shortest among x, B.x, A.B.x and |
| 384 | Mylib.A.B.x that denotes the same object. |
| 385 | @raise Not_found for unknown objects. *) |
| 386 | |
| 387 | val shortest_qualid_of_modtype : ?loc:Loc.t -> ?force_short:bool -> ModPath.t -> qualid |
| 388 | val shortest_qualid_of_module : ?loc:Loc.t -> ?force_short:bool -> ModPath.t -> qualid |
| 389 | val shortest_qualid_of_dir : ?loc:Loc.t -> ?force_short:bool -> GlobDirRef.t -> qualid |
| 390 | |
| 391 | (** In general we have a [UnivNames.universe_binders] around rather than a [Id.Set.t] *) |
| 392 | val shortest_qualid_of_universe : ?loc:Loc.t -> ?force_short:bool -> 'u Id.Map.t -> Univ.UGlobal.t -> qualid |