| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type glob_file = { vfile : string; vofile : string } |
| 16 |
|
| 17 |
type glob_output = |
| 18 |
| NoGlob |
| 19 |
| Feedback |
| 20 |
| MultFiles of glob_file |
| 21 |
| File of string |
| 22 |
|
| 23 |
module Cache : |
| 24 |
sig |
| 25 |
type t |
| 26 |
val make : unit -> t |
| 27 |
val with_cache : t -> string -> (string -> unit) -> unit |
| 28 |
end = |
| 29 |
struct |
| 30 |
type t = String.Set.t ref |
| 31 |
let make () = ref String.Set.empty |
| 32 |
let with_cache cache data f = |
| 33 |
if String.Set.mem data !cache then () |
| 34 |
else |
| 35 |
let () = cache := String.Set.add data !cache in |
| 36 |
f data |
| 37 |
end |
| 38 |
|
| 39 |
type glob_state = |
| 40 |
| StNoGlob |
| 41 |
| StFeedback |
| 42 |
| StChannel of { chan : out_channel; cache : Cache.t } |
| 43 |
|
| 44 |
let glob_output = ref [] |
| 45 |
|
| 46 |
let get_output () = match !glob_output with |
| 47 |
| [] -> StNoGlob |
| 48 |
| g::_ -> g |
| 49 |
|
| 50 |
let push_output g = |
| 51 |
let g = match g with |
| 52 |
| NoGlob -> StNoGlob |
| 53 |
| Feedback -> StFeedback |
| 54 |
| MultFiles { vofile; vfile } -> |
| 55 |
let ch = open_out (Filename.chop_extension vofile ^ ".glob") in |
| 56 |
let () = output_string ch "DIGEST " in |
| 57 |
let () = output_string ch (Digest.to_hex (Digest.file vfile)) in |
| 58 |
let () = output_char ch '\n' in |
| 59 |
StChannel { chan = ch; cache = Cache.make () } |
| 60 |
| File f -> |
| 61 |
let ch = open_out f in |
| 62 |
let () = output_string ch "DIGEST NO\n" in |
| 63 |
StChannel { chan = ch; cache = Cache.make () } |
| 64 |
in |
| 65 |
glob_output := g :: !glob_output |
| 66 |
|
| 67 |
let pop_output () = match !glob_output with |
| 68 |
| [] -> CErrors.anomaly (Pp.str "No output left to pop") |
| 69 |
| hd :: ds -> |
| 70 |
let () = match hd with |
| 71 |
| StNoGlob | StFeedback -> () |
| 72 |
| StChannel ch -> close_out ch.chan |
| 73 |
in |
| 74 |
glob_output := ds |
| 75 |
|
| 76 |
let pause () = push_output NoGlob |
| 77 |
let continue = pop_output |
| 78 |
|
| 79 |
let with_glob_output g f () = |
| 80 |
push_output g; |
| 81 |
try |
| 82 |
let res = f () in |
| 83 |
pop_output (); |
| 84 |
res |
| 85 |
with reraise -> |
| 86 |
let reraise = Exninfo.capture reraise in |
| 87 |
pop_output (); |
| 88 |
Exninfo.iraise reraise |
| 89 |
|
| 90 |
let dump () = get_output () <> StNoGlob |
| 91 |
|
| 92 |
let dump_string s = match get_output () with |
| 93 |
| StNoGlob | StFeedback -> () |
| 94 |
| StChannel ch -> output_string ch.chan s |
| 95 |
|
| 96 |
open Decls |
| 97 |
open Declarations |
| 98 |
|
| 99 |
let type_of_logical_kind = function |
| 100 |
| IsDefinition def -> |
| 101 |
(match def with |
| 102 |
| Definition | Let | LetContext -> "def" |
| 103 |
| Coercion -> "coe" |
| 104 |
| SubClass -> "subclass" |
| 105 |
| CanonicalStructure -> "canonstruc" |
| 106 |
| Example -> "ex" |
| 107 |
| Fixpoint -> "def" |
| 108 |
| CoFixpoint -> "def" |
| 109 |
| Scheme -> "scheme" |
| 110 |
| StructureComponent -> "proj" |
| 111 |
| IdentityCoercion -> "coe" |
| 112 |
| Instance -> "inst" |
| 113 |
| Method -> "meth") |
| 114 |
| IsAssumption a -> |
| 115 |
(match a with |
| 116 |
| Definitional -> "defax" |
| 117 |
| Logical -> "prfax" |
| 118 |
| Conjectural -> "prfax" |
| 119 |
| Context -> "prfax") |
| 120 |
| IsProof th -> |
| 121 |
(match th with |
| 122 |
| Theorem |
| 123 |
| Lemma |
| 124 |
| Fact |
| 125 |
| Remark |
| 126 |
| Property |
| 127 |
| Proposition |
| 128 |
| Corollary -> "thm") |
| 129 |
| IsPrimitive -> "prim" |
| 130 |
| IsSymbol -> "symb" |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
let csttab = Summary.ref (Names.Cmap.empty : logical_kind Names.Cmap.t) ~name:"CONSTANT" |
| 136 |
let add_constant_kind kn k = csttab := Names.Cmap.add kn k !csttab |
| 137 |
let constant_kind kn = Names.Cmap.find kn !csttab |
| 138 |
|
| 139 |
let type_of_global_ref gr = |
| 140 |
if Typeclasses.is_class (Global.env ()) gr then |
| 141 |
"class" |
| 142 |
else |
| 143 |
let open Names.GlobRef in |
| 144 |
match gr with |
| 145 |
| ConstRef cst -> |
| 146 |
let knd = try constant_kind cst with Not_found -> IsDefinition Definition in |
| 147 |
type_of_logical_kind knd |
| 148 |
| VarRef v -> |
| 149 |
let knd = try Decls.variable_kind v with Not_found -> IsDefinition Definition in |
| 150 |
"var" ^ type_of_logical_kind knd |
| 151 |
| IndRef ind -> |
| 152 |
let (mib,oib) = Inductive.lookup_mind_specif (Global.env ()) ind in |
| 153 |
if oib.Declarations.mind_record <> Declarations.NotRecord then |
| 154 |
begin match mib.Declarations.mind_finite with |
| 155 |
| Finite -> "indrec" |
| 156 |
| BiFinite -> "rec" |
| 157 |
| CoFinite -> "corec" |
| 158 |
end |
| 159 |
else |
| 160 |
begin match mib.Declarations.mind_finite with |
| 161 |
| Finite -> "ind" |
| 162 |
| BiFinite -> "variant" |
| 163 |
| CoFinite -> "coind" |
| 164 |
end |
| 165 |
| ConstructRef _ -> "constr" |
| 166 |
|
| 167 |
let remove_sections dir = |
| 168 |
let cwd = Libnames.dirpath_of_path @@ Lib.cwd_except_section () in |
| 169 |
if Libnames.is_dirpath_prefix_of cwd dir then |
| 170 |
|
| 171 |
cwd |
| 172 |
else |
| 173 |
|
| 174 |
dir |
| 175 |
|
| 176 |
let interval loc = |
| 177 |
let loc1,loc2 = Loc.unloc loc in |
| 178 |
loc1, loc2-1 |
| 179 |
|
| 180 |
let dump_ref ?loc filepath modpath ident ty = |
| 181 |
match get_output () with |
| 182 |
| StFeedback -> |
| 183 |
Option.iter (fun loc -> |
| 184 |
Feedback.feedback (Feedback.GlobRef (loc, filepath, modpath, ident, ty)) |
| 185 |
) loc |
| 186 |
| StNoGlob -> () |
| 187 |
| StChannel ch -> |
| 188 |
Option.iter (fun loc -> |
| 189 |
let bl, el = interval loc in |
| 190 |
let payload = Printf.sprintf "R%d:%d %s %s %s %s\n" bl el filepath modpath ident ty in |
| 191 |
Cache.with_cache ch.cache payload dump_string |
| 192 |
) loc |
| 193 |
|
| 194 |
let dump_reference ?loc modpath ident ty = |
| 195 |
let filepath = Names.DirPath.to_string (Lib.library_dp ()) in |
| 196 |
dump_ref ?loc filepath modpath ident ty |
| 197 |
|
| 198 |
let dump_secvar ?loc id = |
| 199 |
dump_reference ?loc "<>" (Libnames.string_of_qualid (Decls.variable_secpath id)) "var" |
| 200 |
|
| 201 |
let dump_modref ?loc mp ty = |
| 202 |
let (dp, l) = Lib.split_modpath mp in |
| 203 |
let filepath = Names.DirPath.to_string dp in |
| 204 |
let modpath = Names.DirPath.to_string (Names.DirPath.make l) in |
| 205 |
let ident = "<>" in |
| 206 |
dump_ref ?loc filepath modpath ident ty |
| 207 |
|
| 208 |
let dump_libref ?loc dp ty = |
| 209 |
dump_ref ?loc (Names.DirPath.to_string dp) "<>" "<>" ty |
| 210 |
|
| 211 |
let cook_notation (from,df) sc = |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
let ntn = Bytes.make (String.length df * 5) '_' in |
| 221 |
let j = ref 0 in |
| 222 |
let l = String.length df - 1 in |
| 223 |
let i = ref 0 in |
| 224 |
let open Bytes in |
| 225 |
while !i <= l do |
| 226 |
assert (df.[!i] != ' '); |
| 227 |
if df.[!i] == '_' && (Int.equal !i l || df.[!i+1] == ' ') then |
| 228 |
|
| 229 |
(set ntn !j 'x'; incr j; incr i) |
| 230 |
else begin |
| 231 |
|
| 232 |
set ntn !j '\''; incr j; |
| 233 |
while !i <= l && df.[!i] != ' ' do |
| 234 |
if df.[!i] < ' ' then |
| 235 |
let c = char_of_int (int_of_char 'A' + int_of_char df.[!i] - 1) in |
| 236 |
(String.blit ("'^" ^ String.make 1 c) 0 ntn !j 3; j := !j+3; incr i) |
| 237 |
else begin |
| 238 |
if df.[!i] == '\'' then (set ntn !j '\''; incr j); |
| 239 |
set ntn !j df.[!i]; incr j; incr i |
| 240 |
end |
| 241 |
done; |
| 242 |
set ntn !j '\''; incr j |
| 243 |
end; |
| 244 |
if !i <= l then (set ntn !j '_'; incr j; incr i) |
| 245 |
done; |
| 246 |
let df = Bytes.sub_string ntn 0 !j in |
| 247 |
let df_sc = match sc with Some sc -> ":" ^ sc ^ ":" ^ df | _ -> "::" ^ df in |
| 248 |
let from_df_sc = match from with |
| 249 |
| Constrexpr.InCustomEntry from -> |
| 250 |
let sp = Nametab.CustomEntries.to_path from in |
| 251 |
":" ^ Libnames.string_of_path sp ^ df_sc |
| 252 |
| Constrexpr.InConstrEntry -> ":" ^ df_sc |
| 253 |
in |
| 254 |
from_df_sc |
| 255 |
|
| 256 |
let dump_notation_location posl df (((path,secpath),_),sc) = |
| 257 |
if dump () then |
| 258 |
let path = Names.DirPath.to_string path in |
| 259 |
let secpath = Names.DirPath.to_string secpath in |
| 260 |
let df = cook_notation df sc in |
| 261 |
List.iter (fun l -> |
| 262 |
dump_ref ~loc:(Loc.make_loc l) path secpath df "not") |
| 263 |
posl |
| 264 |
|
| 265 |
let add_glob_gen ?loc sp lib_dp ty = |
| 266 |
if dump () then |
| 267 |
let mod_dp,id = Libnames.repr_path sp in |
| 268 |
let mod_dp = remove_sections mod_dp in |
| 269 |
let mod_dp_trunc = Libnames.drop_dirpath_prefix lib_dp mod_dp in |
| 270 |
let filepath = Names.DirPath.to_string lib_dp in |
| 271 |
let modpath = Names.DirPath.to_string mod_dp_trunc in |
| 272 |
let ident = Names.Id.to_string id in |
| 273 |
dump_ref ?loc filepath modpath ident ty |
| 274 |
|
| 275 |
let add_glob ?loc ref = |
| 276 |
if dump () then |
| 277 |
let sp = Nametab.path_of_global ref in |
| 278 |
let lib_dp = Lib.library_part ref in |
| 279 |
let ty = type_of_global_ref ref in |
| 280 |
add_glob_gen ?loc sp lib_dp ty |
| 281 |
|
| 282 |
let mp_of_kn kn = |
| 283 |
let mp,l = Names.KerName.repr kn in |
| 284 |
Names.MPdot (mp,l) |
| 285 |
|
| 286 |
let add_glob_kn ?loc kn = |
| 287 |
if dump () then |
| 288 |
let sp = Nametab.path_of_abbreviation kn in |
| 289 |
let lib_dp = Names.ModPath.dp (mp_of_kn kn) in |
| 290 |
add_glob_gen ?loc sp lib_dp "abbrev" |
| 291 |
|
| 292 |
let dump_def ?loc ty secpath id = Option.iter (fun loc -> |
| 293 |
if get_output () = StFeedback then |
| 294 |
Feedback.feedback (Feedback.GlobDef (loc, id, secpath, ty)) |
| 295 |
else |
| 296 |
let bl,el = interval loc in |
| 297 |
dump_string (Printf.sprintf "%s %d:%d %s %s\n" ty bl el secpath id) |
| 298 |
) loc |
| 299 |
|
| 300 |
let dump_definition {CAst.loc;v=id} sec s = |
| 301 |
dump_def ?loc s (Names.DirPath.to_string (Lib.current_dirpath sec)) (Names.Id.to_string id) |
| 302 |
|
| 303 |
let dump_constraint { CAst.loc; v = n } sec ty = |
| 304 |
match n with |
| 305 |
| Names.Name id -> dump_definition CAst.(make ?loc id) sec ty |
| 306 |
| Names.Anonymous -> () |
| 307 |
|
| 308 |
let dump_moddef ?loc mp ty = |
| 309 |
let (dp, l) = Lib.split_modpath mp in |
| 310 |
let mp = Names.DirPath.to_string (Names.DirPath.make l) in |
| 311 |
dump_def ?loc ty "<>" mp |
| 312 |
|
| 313 |
let dump_notation {CAst.loc;v=df} sc sec = Option.iter (fun loc -> |
| 314 |
|
| 315 |
let i = fst (Loc.unloc loc) in |
| 316 |
let location = (Loc.make_loc (i, i+1)) in |
| 317 |
dump_def ~loc:location "not" (Names.DirPath.to_string (Lib.current_dirpath sec)) (cook_notation df sc) |
| 318 |
) loc |
| 319 |
|
| 320 |
let dump_binding ?loc uid = |
| 321 |
dump_def ?loc "binder" "<>" uid |