| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
module API = Elpi.API |
| 6 |
module EC = API.Compile |
| 7 |
module EP = API.Parse |
| 8 |
|
| 9 |
open Rocq_elpi_utils |
| 10 |
type program_name = Loc.t * qualified_name |
| 11 |
type cunit = Full of Names.KerName.t * EC.compilation_unit | Signature of Names.KerName.t * EC.compilation_unit_signature |
| 12 |
let name_of_cunit = function Full(_,u) -> EC.compilation_unit_name u | Signature(_,s) -> EC.compilation_unit_signature_name s |
| 13 |
let kn_of_cunit = function Full(kn,_) -> kn | Signature(kn,_) -> kn |
| 14 |
type what = Code | SignatureOnly |
| 15 |
let pp_cunit fmt = function |
| 16 |
| Full (kn,u) -> Format.fprintf fmt "Full(%s,%s)" Names.KerName.(debug_to_string kn) (EC.compilation_unit_name u) |
| 17 |
| Signature(kn,s) -> Format.fprintf fmt "Signature(%s,%s)" Names.KerName.(debug_to_string kn) (EC.compilation_unit_signature_name s) |
| 18 |
let eq_cunit x y = |
| 19 |
match x,y with |
| 20 |
| Full(k1,_), Full(k2,_) -> Names.KerName.equal k1 k2 |
| 21 |
| Signature(k1,_), Signature(k2,_) -> Names.KerName.equal k1 k2 |
| 22 |
| _ -> false |
| 23 |
|
| 24 |
[%%if coq = "9.0"] |
| 25 |
let my_filtered_open = Libobject.simple_open |
| 26 |
let my_simple_open ?cat f = my_filtered_open ?cat (fun i v -> if Int.equal i 1 then f v) |
| 27 |
let is_in_section = Lib.is_in_section |
| 28 |
[%%else] |
| 29 |
let my_filtered_open = Libobject.filtered_open |
| 30 |
let my_simple_open = Libobject.simple_open |
| 31 |
let is_in_section = Global.is_in_section |
| 32 |
[%%endif] |
| 33 |
|
| 34 |
|
| 35 |
module SLMap = Map.Make(struct type t = qualified_name let compare = compare_qualified_name end) |
| 36 |
module SLSet = Set.Make(struct type t = qualified_name let compare = compare_qualified_name end) |
| 37 |
|
| 38 |
type src = |
| 39 |
| File of src_file |
| 40 |
| DatabaseBody of qualified_name |
| 41 |
| DatabaseHeader of src_db_header |
| 42 |
and src_file = { |
| 43 |
fname : string; |
| 44 |
fast : cunit; |
| 45 |
} |
| 46 |
and src_db_header = { |
| 47 |
dast : cunit; |
| 48 |
} |
| 49 |
|
| 50 |
let subst_cunit subst = function |
| 51 |
| Full (kn, cu) as orig -> |
| 52 |
let kn' = Mod_subst.subst_kn subst kn in |
| 53 |
let cu' = EC.map_compilation_unit (Rocq_elpi_HOAS.subst_cdata subst) cu in |
| 54 |
if kn == kn' && cu == cu' then orig else Full (kn', cu') |
| 55 |
| Signature (kn, cu) as orig -> |
| 56 |
let kn' = Mod_subst.subst_kn subst kn in |
| 57 |
if kn == kn' then orig else Signature (kn', cu) |
| 58 |
|
| 59 |
let subst_src subst = function |
| 60 |
| File ({ fname; fast } as f) -> |
| 61 |
let fast' = subst_cunit subst fast in |
| 62 |
if fast == fast' then File f else File { fname; fast = fast' } |
| 63 |
| DatabaseBody n -> DatabaseBody n |
| 64 |
| DatabaseHeader ({ dast } as h) -> |
| 65 |
let dast' = subst_cunit subst dast in |
| 66 |
if dast == dast' then DatabaseHeader h else DatabaseHeader { dast = dast' } |
| 67 |
|
| 68 |
let alpha = 65599 |
| 69 |
let combine_hash h1 h2 = h1 * alpha + h2 |
| 70 |
|
| 71 |
let hash_cunit = function |
| 72 |
| Full (kn,_) -> Names.KerName.hash kn |
| 73 |
| Signature (kn,_) -> Names.KerName.hash kn + 1 |
| 74 |
let compare_cunit a b = |
| 75 |
match a,b with |
| 76 |
| Full(kn1,_), Full(kn2,_) -> Names.KerName.compare kn1 kn2 |
| 77 |
| Signature(kn1,_), Signature(kn2,_) -> Names.KerName.compare kn1 kn2 |
| 78 |
| Full _, Signature _ -> -1 |
| 79 |
| Signature _, Full _ -> +1 |
| 80 |
|
| 81 |
let hash_list f z l = List.fold_left (fun acc x -> combine_hash (f x) acc) z l |
| 82 |
|
| 83 |
module Chunk = struct |
| 84 |
type t = |
| 85 |
| Base of { |
| 86 |
hash : int; |
| 87 |
base : cunit list; |
| 88 |
} |
| 89 |
| Snoc of { |
| 90 |
source_rev : cunit list; |
| 91 |
prev : t; |
| 92 |
hash : int |
| 93 |
} |
| 94 |
[@@deriving show, ord] |
| 95 |
let hash = function |
| 96 |
| Base { hash } -> hash |
| 97 |
| Snoc { hash } -> hash |
| 98 |
|
| 99 |
let snoc source_rev prev = |
| 100 |
let hash = (hash_list hash_cunit (hash prev) source_rev) in |
| 101 |
Snoc { hash; prev; source_rev } |
| 102 |
|
| 103 |
let rec base = function |
| 104 |
| Base { base } -> base |
| 105 |
| Snoc { prev } -> base prev |
| 106 |
|
| 107 |
let eq x y = compare x y == 0 |
| 108 |
end |
| 109 |
|
| 110 |
module Code = struct |
| 111 |
type 'db t = |
| 112 |
| Base of { |
| 113 |
hash : int; |
| 114 |
base : cunit; |
| 115 |
} |
| 116 |
| Snoc of { |
| 117 |
source : cunit; |
| 118 |
prev : 'db t; |
| 119 |
hash : int; |
| 120 |
cacheme: bool; |
| 121 |
} |
| 122 |
| Snoc_db of { |
| 123 |
chunks : 'db; |
| 124 |
prev : 'db t; |
| 125 |
hash : int |
| 126 |
} |
| 127 |
[@@deriving show] |
| 128 |
let hash = function |
| 129 |
| Base { hash } -> hash |
| 130 |
| Snoc { hash } -> hash |
| 131 |
| Snoc_db { hash } -> hash |
| 132 |
|
| 133 |
let cache = function |
| 134 |
| Base _ -> true |
| 135 |
| Snoc { cacheme } -> cacheme |
| 136 |
| Snoc_db _ -> false |
| 137 |
|
| 138 |
let snoc source prev = |
| 139 |
let hash = combine_hash (hash prev) (hash_cunit source) in |
| 140 |
Snoc { hash; prev; source; cacheme = cache prev } |
| 141 |
|
| 142 |
let snoc_opt source prev = |
| 143 |
match prev with |
| 144 |
| Some prev -> snoc source prev |
| 145 |
| None -> Base { hash = (hash_cunit source); base = source } |
| 146 |
|
| 147 |
let snoc_db f chunks prev = |
| 148 |
let hash = hash prev in |
| 149 |
let hash = combine_hash hash (f chunks) in |
| 150 |
Snoc_db { hash; prev; chunks } |
| 151 |
|
| 152 |
let snoc_db_opt f chunks prev = |
| 153 |
match prev with |
| 154 |
| Some prev -> snoc_db f chunks prev |
| 155 |
| None -> assert false |
| 156 |
|
| 157 |
let rec map f = function |
| 158 |
| Base x -> Base x |
| 159 |
| Snoc { prev; source; cacheme = true } -> |
| 160 |
|
| 161 |
let prev = Obj.magic prev in |
| 162 |
snoc source prev |
| 163 |
| Snoc { prev; source } -> |
| 164 |
let prev = map f prev in |
| 165 |
snoc source prev |
| 166 |
| Snoc_db { prev; chunks } -> |
| 167 |
let prev = map f prev in |
| 168 |
let chunks = f chunks in |
| 169 |
snoc_db Chunk.hash chunks prev |
| 170 |
|
| 171 |
let rec eq c x y = |
| 172 |
x == y || |
| 173 |
match x,y with |
| 174 |
| Base x, Base y -> eq_cunit x.base y.base |
| 175 |
| Snoc x, Snoc y -> eq_cunit x.source y.source && eq c x.prev y.prev |
| 176 |
| Snoc_db x, Snoc_db y -> c x.chunks y.chunks && eq c x.prev y.prev |
| 177 |
| _ -> false |
| 178 |
|
| 179 |
end |
| 180 |
|
| 181 |
type db = { |
| 182 |
sources_rev : Chunk.t; |
| 183 |
units : Names.KNset.t; |
| 184 |
} |
| 185 |
|
| 186 |
type nature = Command of { raw_args : bool } | Tactic | Program of { raw_args : bool } |
| 187 |
|
| 188 |
type program = { |
| 189 |
sources_rev : qualified_name Code.t; |
| 190 |
files : CString.Set.t; |
| 191 |
units : Names.KNset.t; |
| 192 |
dbs : SLSet.t; |
| 193 |
empty : bool; |
| 194 |
} |
| 195 |
|
| 196 |
type from = Initialization | User |
| 197 |
|
| 198 |
type snippet = { |
| 199 |
program : qualified_name; |
| 200 |
code :cunit list; |
| 201 |
scope : Rocq_elpi_utils.clause_scope; |
| 202 |
vars : Names.Id.t list; |
| 203 |
} |
| 204 |
|
| 205 |
let group_clauses l = |
| 206 |
let rec aux acc l = |
| 207 |
match acc, l with |
| 208 |
| _, [] -> List.rev acc |
| 209 |
| [], (dbname,ast,vs,scope) :: xs -> aux [dbname,[ast],vs,scope] xs |
| 210 |
| (dbname,asts,vs,scope) :: tl as acc, (dbname1,ast,vs1,scope1) :: xs -> |
| 211 |
if dbname = dbname1 && vs = vs1 && scope = scope1 then |
| 212 |
aux ((dbname,asts@[ast],vs,scope) :: tl) xs |
| 213 |
else |
| 214 |
aux ((dbname1,[ast],vs1,scope1) :: acc) xs |
| 215 |
in |
| 216 |
aux [] l |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
module type Programs = sig |
| 222 |
|
| 223 |
open Elpi.API |
| 224 |
|
| 225 |
val debug_vars : Compile.StrSet.t ref |
| 226 |
val cc_flags : unit -> Compile.flags |
| 227 |
val unit_from_ast : ?error_header:string -> elpi:Setup.elpi -> base:Compile.program -> loc:Loc.t -> Compile.scoped_program -> cunit |
| 228 |
val parse_goal : elpi:Setup.elpi -> loc:Loc.t -> Ast.Loc.t -> string -> Ast.query |
| 229 |
|
| 230 |
val db_exists : qualified_name -> bool |
| 231 |
val program_exists : qualified_name -> bool |
| 232 |
val declare_db : program_name -> unit |
| 233 |
val declare_program : program_name -> nature -> unit |
| 234 |
val declare_file : program_name -> unit |
| 235 |
val get_nature : qualified_name -> nature |
| 236 |
|
| 237 |
val init_program : program_name -> loc:Loc.t -> (Ast.Loc.t * string) -> unit |
| 238 |
val init_command : program_name -> loc:Loc.t -> unit |
| 239 |
val init_tactic : program_name -> loc:Loc.t -> unit |
| 240 |
val init_db : program_name -> loc:Loc.t -> (Ast.Loc.t * string) -> unit |
| 241 |
val init_file : program_name -> loc:Loc.t -> (Ast.Loc.t * string) -> unit |
| 242 |
|
| 243 |
val ast_of_elpifile : qualified_name -> Compile.scoped_program list |
| 244 |
|
| 245 |
type db_header |
| 246 |
val header_of_db : qualified_name -> db_header |
| 247 |
|
| 248 |
val accumulate_file_to_program : loc:Loc.t -> program:qualified_name -> what:what -> file:string -> unit |
| 249 |
val accumulate_ast_to_program : loc:Loc.t -> program:qualified_name -> what:what -> ast:Compile.scoped_program -> unit |
| 250 |
val accumulate_string_to_program : loc:Loc.t -> program:qualified_name -> code:(Ast.Loc.t * string) -> unit |
| 251 |
val accumulate_plugin_to_program : loc:Loc.t -> program:qualified_name -> plugin:Elpi.API.Setup.builtins -> unit |
| 252 |
val accumulate_db_to_program : loc:Loc.t -> program:qualified_name -> db:qualified_name -> unit |
| 253 |
val accumulate_header_to_program : loc:Loc.t -> program:qualified_name -> header:db_header -> unit |
| 254 |
|
| 255 |
val accumulate_file_to_db : loc:Loc.t -> db:qualified_name -> what:what -> file:string -> scope:Rocq_elpi_utils.clause_scope -> unit |
| 256 |
val accumulate_ast_to_db : loc:Loc.t -> db:qualified_name -> what:what -> ast:Compile.scoped_program -> scope:Rocq_elpi_utils.clause_scope -> unit |
| 257 |
val accumulate_string_to_db : loc:Loc.t -> db:qualified_name -> code:(Ast.Loc.t * string) -> scope:Rocq_elpi_utils.clause_scope -> unit |
| 258 |
val accumulate_units_to_db : loc:Loc.t -> db:qualified_name -> units:cunit list -> secvars:Names.Id.t list -> scope:Rocq_elpi_utils.clause_scope -> unit |
| 259 |
val accumulate_header_to_db : loc:Loc.t -> db:qualified_name -> header:db_header -> scope:Rocq_elpi_utils.clause_scope -> unit |
| 260 |
|
| 261 |
val load_command : loc:Loc.t -> string -> unit |
| 262 |
val load_tactic : loc:Loc.t -> string -> unit |
| 263 |
|
| 264 |
val ensure_initialized : unit -> Setup.elpi |
| 265 |
|
| 266 |
val code : ?even_if_empty:bool -> qualified_name -> Chunk.t Code.t option |
| 267 |
|
| 268 |
val in_stage : string -> string |
| 269 |
val stage : Summary.Stage.t |
| 270 |
val db_inspect : qualified_name -> int |
| 271 |
|
| 272 |
val get_and_compile_existing_db : loc:Loc.t -> qualified_name -> Compile.program |
| 273 |
val get_and_compile : loc:Loc.t -> ?even_if_empty:bool -> qualified_name -> (Compile.program * bool) option |
| 274 |
|
| 275 |
|
| 276 |
end |
| 277 |
|
| 278 |
|
| 279 |
module type Stage = sig |
| 280 |
val stage : Summary.Stage.t |
| 281 |
val in_stage : string -> string |
| 282 |
val init : unit -> API.Setup.elpi |
| 283 |
end |
| 284 |
|
| 285 |
module SourcesStorage(S : Stage) = struct |
| 286 |
open S |
| 287 |
|
| 288 |
module Libobject = struct |
| 289 |
include Libobject |
| 290 |
let declare_named_object o = Libobject.declare_named_object { o with |
| 291 |
Libobject.object_stage = stage; |
| 292 |
Libobject.object_name = in_stage (o.Libobject.object_name) |
| 293 |
} |
| 294 |
let declare_object o = Libobject.declare_object { o with |
| 295 |
Libobject.object_stage = stage; |
| 296 |
Libobject.object_name = in_stage (o.Libobject.object_name) |
| 297 |
} |
| 298 |
end |
| 299 |
|
| 300 |
module Summary = struct |
| 301 |
include Summary |
| 302 |
let ref ?local ~name x = Summary.ref ~stage ?local ~name:(in_stage name) x |
| 303 |
end |
| 304 |
|
| 305 |
let program_name : nature SLMap.t ref = |
| 306 |
Summary.ref ~name:("elpi-programs") SLMap.empty |
| 307 |
let program_exists name = SLMap.mem name !program_name |
| 308 |
|
| 309 |
let get_nature p = |
| 310 |
try SLMap.find p !program_name |
| 311 |
with Not_found -> |
| 312 |
CErrors.user_err |
| 313 |
Pp.(str "No Elpi Program named " ++ pr_qualified_name p) |
| 314 |
|
| 315 |
let in_program_name : qualified_name * nature -> Libobject.obj = |
| 316 |
let open Libobject in |
| 317 |
declare_object @@ (superglobal_object_nodischarge "elpi-programs-names" |
| 318 |
~cache:(fun (name,nature) -> |
| 319 |
program_name := SLMap.add name nature !program_name) |
| 320 |
~subst:(Some (fun (_,x) -> x))) |
| 321 |
|
| 322 |
let declare_program name nature = |
| 323 |
let obj = in_program_name (name,nature) in |
| 324 |
Lib.add_leaf obj |
| 325 |
|
| 326 |
let db_name : SLSet.t ref = Summary.ref ~name:("elpi-dbs") SLSet.empty |
| 327 |
let file_name : SLSet.t ref = Summary.ref ~name:("elpi-files") SLSet.empty |
| 328 |
let db_exists name = SLSet.mem name !db_name |
| 329 |
let file_exists name = SLSet.mem name !file_name |
| 330 |
|
| 331 |
let in_db_name : qualified_name -> Libobject.obj = |
| 332 |
let open Libobject in |
| 333 |
declare_object @@ |
| 334 |
(superglobal_object_nodischarge "elpi-db-names" |
| 335 |
~cache:(fun name -> db_name := SLSet.add name !db_name) |
| 336 |
~subst:(Some (fun (_,x) -> x))) |
| 337 |
|
| 338 |
let in_file_name : qualified_name -> Libobject.obj = |
| 339 |
let open Libobject in |
| 340 |
declare_object @@ |
| 341 |
(superglobal_object_nodischarge "elpi-file-names" |
| 342 |
~cache:(fun name -> file_name := SLSet.add name !file_name) |
| 343 |
~subst:(Some (fun (_,x) -> x))) |
| 344 |
|
| 345 |
let declare_db program = |
| 346 |
ignore @@ Lib.add_leaf (in_db_name program) |
| 347 |
|
| 348 |
let declare_file program = |
| 349 |
ignore @@ Lib.add_leaf (in_file_name program) |
| 350 |
|
| 351 |
let declare_program (loc,qualid) nature = |
| 352 |
if program_exists qualid || db_exists qualid || file_exists qualid then |
| 353 |
CErrors.user_err Pp.(str "Program/Tactic/Db/File " ++ pr_qualified_name qualid ++ str " already exists") |
| 354 |
else |
| 355 |
declare_program qualid nature |
| 356 |
|
| 357 |
let declare_db (loc,qualid) = |
| 358 |
if program_exists qualid || db_exists qualid || file_exists qualid then |
| 359 |
CErrors.user_err Pp.(str "Program/Tactic/Db/File " ++ pr_qualified_name qualid ++ str " already exists") |
| 360 |
else |
| 361 |
declare_db qualid |
| 362 |
|
| 363 |
|
| 364 |
let declare_file (loc,qualid) = |
| 365 |
if program_exists qualid || db_exists qualid || file_exists qualid then |
| 366 |
CErrors.user_err Pp.(str "Program/Tactic/Db/File " ++ pr_qualified_name qualid ++ str " already exists") |
| 367 |
else |
| 368 |
declare_file qualid |
| 369 |
|
| 370 |
let debug_vars = Summary.ref ~name:"elpi-debug" EC.StrSet.empty |
| 371 |
|
| 372 |
let cc_flags () = |
| 373 |
{ EC.default_flags with EC.defined_variables = !debug_vars } |
| 374 |
|
| 375 |
let source_cache1 = Summary.ref ~local:true ~name:"elpi-units1" Names.KNmap.empty |
| 376 |
let source_cache2 = Summary.ref ~local:true ~name:"elpi-units2" CString.Map.empty |
| 377 |
|
| 378 |
let source_cache_lookup flags hash = |
| 379 |
let kn = CString.Map.find hash !source_cache2 in |
| 380 |
let u,cf = Names.KNmap.find kn !source_cache1 in |
| 381 |
if flags <> cf then raise Not_found |
| 382 |
else kn, u |
| 383 |
|
| 384 |
let last_kn = ref None |
| 385 |
|
| 386 |
let in_source : Names.Id.t -> EC.compilation_unit * EC.flags -> Libobject.obj = |
| 387 |
let open Libobject in |
| 388 |
let cache ((_,kn), (u,cf)) = |
| 389 |
last_kn := Some kn; |
| 390 |
source_cache1 := Names.KNmap.add kn (u,cf) !source_cache1; |
| 391 |
source_cache2 := CString.Map.add (EC.compilation_unit_digest u) kn !source_cache2; |
| 392 |
in |
| 393 |
let import _ (_, s as o) = cache o in |
| 394 |
declare_named_object @@ { (default_object "ELPI-UNITS") with |
| 395 |
subst_function = (fun (_,o) -> o); |
| 396 |
load_function = import; |
| 397 |
cache_function = cache; |
| 398 |
open_function = my_filtered_open import; |
| 399 |
discharge_function = (fun o -> Some o); |
| 400 |
classify_function = (fun _ -> Keep); |
| 401 |
} |
| 402 |
;; |
| 403 |
|
| 404 |
let dig = ref 0 |
| 405 |
|
| 406 |
let intern x cc = |
| 407 |
let id = Names.Id.of_string_soft |
| 408 |
@@ Printf.sprintf "%s:%s" (EC.compilation_unit_name x) (Digest.to_hex (EC.compilation_unit_digest x)) in |
| 409 |
Lib.add_leaf (in_source id (x,cc)); |
| 410 |
let kn = Option.get !last_kn in |
| 411 |
let u,_ = Names.KNmap.find kn !source_cache1 in |
| 412 |
kn, u |
| 413 |
|
| 414 |
let unit_from_ast ?error_header ~elpi ~flags ~base ~loc ast = |
| 415 |
try |
| 416 |
let kn, u = source_cache_lookup flags (EC.scoped_program_digest ast) in |
| 417 |
Full(kn,u) |
| 418 |
with Not_found -> |
| 419 |
handle_elpi_compiler_errors ~loc ?error_header (fun () -> |
| 420 |
let u = EC.unit ~elpi ~flags ~base ast in |
| 421 |
let kn, u = intern u flags in |
| 422 |
Full(kn,u)) |
| 423 |
|
| 424 |
let unit_signature_from_ast ~elpi ~flags ~base ~loc ast = |
| 425 |
try |
| 426 |
let kn, u = source_cache_lookup (cc_flags ()) (EC.scoped_program_digest ast) in |
| 427 |
Signature (kn,EC.signature u) |
| 428 |
with Not_found -> |
| 429 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 430 |
let u = EC.unit ~elpi ~flags ~base ast in |
| 431 |
let kn, u = intern u flags in |
| 432 |
Signature (kn,EC.signature u)) |
| 433 |
|
| 434 |
let unit_from_plugin ?error_header ~elpi ~base ~loc builtins ~flags : cunit = |
| 435 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 436 |
let ast = EC.scope_builtins ~elpi builtins in |
| 437 |
let loc = Loc.initial Loc.ToplevelInput in |
| 438 |
unit_from_ast ~elpi ~flags ~base ~loc ast) |
| 439 |
|
| 440 |
let assemble_unit ~loc base u = |
| 441 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 442 |
match u with |
| 443 |
| Full(_, u) -> EC.extend ~base ~flags:(cc_flags ()) u |
| 444 |
| Signature(_, s) -> EC.extend_signature ~base ~flags:(cc_flags ()) s) |
| 445 |
|
| 446 |
let assemble_units ~base ~loc units = |
| 447 |
List.fold_left (assemble_unit ~loc) base units |
| 448 |
|
| 449 |
let extend_with_idependent_units = assemble_units |
| 450 |
let extend_with_unit ~base ~loc u = assemble_units ~base ~loc [u] |
| 451 |
|
| 452 |
|
| 453 |
let private__units_from_file ~elpi ~base ~loc x : cunit list = |
| 454 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 455 |
let flags = cc_flags () in |
| 456 |
let ast = EP.program ~elpi ~file:x in |
| 457 |
let ast = EC.scope_ast ~elpi ast in |
| 458 |
let _, units_rev = |
| 459 |
List.fold_left (fun (base,acc) ast -> |
| 460 |
let u = unit_from_ast ~elpi ~flags ~base ~loc ast in |
| 461 |
let base = assemble_unit ~loc base u in |
| 462 |
base, u :: acc |
| 463 |
) (base,[]) ast in |
| 464 |
List.rev units_rev) |
| 465 |
|
| 466 |
let private__units_from_string ~elpi ~base ~loc xloc x : cunit list = |
| 467 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 468 |
let flags = cc_flags () in |
| 469 |
let ast = EP.program_from ~elpi ~loc:xloc ~digest:(Digest.string x) (Lexing.from_string x) in |
| 470 |
let ast = EC.scope_ast ~elpi ast in |
| 471 |
let _, units_rev = |
| 472 |
List.fold_left (fun (base,acc) ast -> |
| 473 |
let u = unit_from_ast ~elpi ~flags ~base ~loc ast in |
| 474 |
let base = assemble_unit ~loc base u in |
| 475 |
base, u :: acc |
| 476 |
) (base,[]) ast in |
| 477 |
List.rev units_rev) |
| 478 |
|
| 479 |
let parse_goal ~elpi ~loc tloc text = |
| 480 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 481 |
EP.goal ~elpi ~loc:tloc ~text) |
| 482 |
|
| 483 |
let program_src : program SLMap.t ref = |
| 484 |
Summary.ref ~name:("elpi-programs-src") SLMap.empty |
| 485 |
|
| 486 |
let db_name_src : db SLMap.t ref = |
| 487 |
Summary.ref ~name:("elpi-db-src") SLMap.empty |
| 488 |
|
| 489 |
let file_name_src : API.Compile.scoped_program list SLMap.t ref = |
| 490 |
Summary.ref ~name:("elpi-file-src") SLMap.empty |
| 491 |
|
| 492 |
|
| 493 |
let elpi = Stdlib.ref None |
| 494 |
|
| 495 |
let ensure_initialized = |
| 496 |
fun () -> |
| 497 |
match !elpi with |
| 498 |
| Some x -> x |
| 499 |
| None -> |
| 500 |
let e = S.init () in |
| 501 |
elpi := Some e; |
| 502 |
e |
| 503 |
|
| 504 |
let get ?(fail_if_not_exists=false) p = |
| 505 |
let _elpi = ensure_initialized () in |
| 506 |
let nature = get_nature p in |
| 507 |
try |
| 508 |
let { sources_rev; files; units; dbs; empty } = SLMap.find p !program_src in |
| 509 |
files, units, dbs, Some nature, Some sources_rev, empty |
| 510 |
with Not_found -> |
| 511 |
if fail_if_not_exists then |
| 512 |
CErrors.user_err |
| 513 |
Pp.(str "No Elpi Program named " ++ pr_qualified_name p) |
| 514 |
else |
| 515 |
CString.Set.empty, Names.KNset.empty, SLSet.empty, None, None, true |
| 516 |
|
| 517 |
let append_to_prog from name src = |
| 518 |
let files, units, dbs, _, prog, empty = get name in |
| 519 |
let files, units, dbs, prog = |
| 520 |
match src with |
| 521 |
|
| 522 |
| File { fname; fast = Full (kn,_) } when CString.Set.mem fname files || Names.KNset.mem kn units -> files, units, dbs, prog |
| 523 |
| DatabaseHeader { dast = Full(kn,_) } when Names.KNset.mem kn units -> files, units, dbs, prog |
| 524 |
| DatabaseBody n when SLSet.mem n dbs -> files, units, dbs, prog |
| 525 |
|
| 526 |
| File { fname; fast = Full (kn,_) as u } -> CString.Set.add fname files, (Names.KNset.add kn units), dbs, Some (Code.snoc_opt u prog) |
| 527 |
| DatabaseHeader { dast = Full(kn,_) as u } -> files, (Names.KNset.add kn units), dbs, Some (Code.snoc_opt u prog) |
| 528 |
| DatabaseBody n -> files, units, SLSet.add n dbs, Some (Code.snoc_db_opt Hashtbl.hash n prog) |
| 529 |
| File { fast = Signature _ as u } -> files, units, dbs, Some (Code.snoc_opt u prog) |
| 530 |
| DatabaseHeader { dast = Signature _ as u } -> |
| 531 |
files, units, dbs, Some (Code.snoc_opt u prog) |
| 532 |
in |
| 533 |
let prog = Option.get prog in |
| 534 |
{ files; units; dbs; sources_rev = prog; empty = empty && from = Initialization } |
| 535 |
|
| 536 |
let in_program : qualified_name * src * from -> Libobject.obj = |
| 537 |
let open Libobject in |
| 538 |
declare_object @@ superglobal_object_nodischarge "ELPI" |
| 539 |
~cache:(fun (name,src_ast,from) -> |
| 540 |
program_src := |
| 541 |
SLMap.add name (append_to_prog from name src_ast) !program_src) |
| 542 |
~subst:(Some (fun (subst, (name, src_ast, from)) -> |
| 543 |
(name, subst_src subst src_ast, from))) |
| 544 |
|
| 545 |
|
| 546 |
let init_program name ~loc:_ ul = |
| 547 |
ul |> List.iter (fun u -> |
| 548 |
let obj = in_program (name, u, Initialization) in |
| 549 |
Lib.add_leaf obj) |
| 550 |
;; |
| 551 |
|
| 552 |
let add_to_program name v = |
| 553 |
let obj = in_program (name, v, User) in |
| 554 |
Lib.add_leaf obj |
| 555 |
|
| 556 |
let code ?(even_if_empty=false) n : Chunk.t Code.t option = |
| 557 |
let _,_,_,_,sources, empty = get n in |
| 558 |
if empty && not even_if_empty then None else |
| 559 |
sources |> Option.map (fun sources -> sources |> Code.map (fun name -> |
| 560 |
try |
| 561 |
let { sources_rev } : db = SLMap.find name !db_name_src in |
| 562 |
sources_rev |
| 563 |
with |
| 564 |
Not_found -> |
| 565 |
CErrors.user_err Pp.(str "Unknown Db " ++ str (show_qualified_name name)))) |
| 566 |
|
| 567 |
let db_code n : Chunk.t option = |
| 568 |
SLMap.find_opt n !db_name_src |> Option.map (fun ({ sources_rev } : db) -> sources_rev) |
| 569 |
|
| 570 |
let append_to_db name c = |
| 571 |
try |
| 572 |
let (db : db) = SLMap.find name !db_name_src in |
| 573 |
let c = List.filter (fun u -> not (Names.KNset.mem (kn_of_cunit u) db.units)) c in |
| 574 |
if c = [] then db |
| 575 |
else |
| 576 |
{ sources_rev = Chunk.snoc c db.sources_rev; |
| 577 |
units = List.fold_left (fun s u -> Names.KNset.add (kn_of_cunit u) s) db.units c } |
| 578 |
with Not_found -> |
| 579 |
{ sources_rev = Chunk.Base { hash = hash_list hash_cunit 0 c; base = List.rev c }; |
| 580 |
units = List.fold_left (fun s u -> Names.KNset.add (kn_of_cunit u) s) Names.KNset.empty c } |
| 581 |
|
| 582 |
let is_inside_current_library kn = |
| 583 |
Names.DirPath.equal |
| 584 |
(Lib.library_dp ()) |
| 585 |
(Names.KerName.modpath kn |> Names.ModPath.dp) |
| 586 |
|
| 587 |
let in_db : Names.Id.t -> snippet -> Libobject.obj = |
| 588 |
let open Libobject in |
| 589 |
let cache ((_,kn),{ program = name; code = p; _ }) = |
| 590 |
db_name_src := SLMap.add name (append_to_db name p) !db_name_src in |
| 591 |
let load i ((_,kn),s as o) = |
| 592 |
if Int.equal i 1 || |
| 593 |
(s.scope = Rocq_elpi_utils.Global && is_inside_current_library kn) || |
| 594 |
s.scope = Rocq_elpi_utils.SuperGlobal then cache o in |
| 595 |
declare_named_object @@ { (default_object "ELPI-DB") with |
| 596 |
classify_function = (fun { scope; _ } -> |
| 597 |
match scope with |
| 598 |
| Rocq_elpi_utils.Local -> Dispose |
| 599 |
| Rocq_elpi_utils.Regular -> Substitute |
| 600 |
| Rocq_elpi_utils.Global -> Keep |
| 601 |
| Rocq_elpi_utils.SuperGlobal -> Keep); |
| 602 |
load_function = load; |
| 603 |
cache_function = cache; |
| 604 |
subst_function = (fun (subst, ({ code; _ } as o)) -> |
| 605 |
{ o with code = List.map (subst_cunit subst) code }); |
| 606 |
open_function = my_simple_open cache; |
| 607 |
discharge_function = (fun (({ scope; program; vars; } as o)) -> |
| 608 |
if scope = Rocq_elpi_utils.Local || (List.exists (fun x -> is_in_section (Names.GlobRef.VarRef x)) vars) then None |
| 609 |
else Some o); |
| 610 |
} |
| 611 |
|
| 612 |
let in_file : Names.Id.t -> (program_name * API.Compile.scoped_program list) -> Libobject.obj = |
| 613 |
let open Libobject in |
| 614 |
let cache ((_,kn),((_,name),p)) = |
| 615 |
file_name_src := SLMap.add name p !file_name_src in |
| 616 |
let load i ((_,kn),_ as o) = cache o in |
| 617 |
declare_named_object @@ { (default_object "ELPI-FILE") with |
| 618 |
classify_function = (fun _ -> Keep); |
| 619 |
load_function = load; |
| 620 |
cache_function = cache; |
| 621 |
subst_function = (fun (_,o) -> o); |
| 622 |
open_function = my_simple_open cache; |
| 623 |
discharge_function = (fun o -> Some o); |
| 624 |
} |
| 625 |
|
| 626 |
let accum = ref 0 |
| 627 |
let add_to_db program code vars scope = |
| 628 |
ignore @@ Lib.add_leaf |
| 629 |
(in_db (Names.Id.of_string (incr accum; Printf.sprintf "_ELPI_%d" !accum)) { program; code; scope; vars }) |
| 630 |
|
| 631 |
let add_to_file program code = |
| 632 |
ignore @@ Lib.add_leaf |
| 633 |
(in_file (Names.Id.of_string (incr accum; Printf.sprintf "_ELPI_%d" !accum)) (program, code)) |
| 634 |
|
| 635 |
|
| 636 |
let init_file qualid init = |
| 637 |
add_to_file qualid init |
| 638 |
|
| 639 |
let init_file p ~loc (sloc, s) = |
| 640 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 641 |
let elpi = ensure_initialized () in |
| 642 |
let ast = EP.program_from ~elpi ~loc:sloc ~digest:(Digest.string s) (Lexing.from_string s) in |
| 643 |
let asts = EC.scope_ast ~elpi ast in |
| 644 |
init_file p asts |
| 645 |
end |
| 646 |
|
| 647 |
|
| 648 |
type db_header = cunit list |
| 649 |
let header_of_db qulid : db_header = |
| 650 |
(Chunk.base @@ (SLMap.find qulid !db_name_src).sources_rev) |
| 651 |
|
| 652 |
let ast_of_elpifile qulid = SLMap.find qulid !file_name_src |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
let lp_command_base = Summary.ref ~name:("elpi-lp-command") None |
| 657 |
let in_lp_command_src : src list -> Libobject.obj = |
| 658 |
let open Libobject in |
| 659 |
declare_object { (default_object "ELPI-LP-COMMAND") with |
| 660 |
load_function = (fun _ x -> lp_command_base := Some x); |
| 661 |
classify_function = (fun _ -> Keep); |
| 662 |
} |
| 663 |
let load_command ~loc s = |
| 664 |
let elpi = ensure_initialized () in |
| 665 |
let units = private__units_from_file ~elpi ~loc ~base:EC.(empty_base ~elpi) s in |
| 666 |
let base = List.map (fun u -> File { fname = name_of_cunit u; fast = u }) units in |
| 667 |
lp_command_base := Some base; |
| 668 |
Lib.add_leaf (in_lp_command_src base) |
| 669 |
let command_init () = |
| 670 |
match !lp_command_base with |
| 671 |
| None -> CErrors.user_err Pp.(str "Elpi CommandTemplate was not called") |
| 672 |
| Some ast -> ast |
| 673 |
|
| 674 |
let command_base_signature ~loc = |
| 675 |
command_init () |> List.map (function |
| 676 |
| File { fast = Full(kn,u) } -> Signature (kn,EC.signature u) |
| 677 |
| _ -> assert false) |
| 678 |
|
| 679 |
let init_db qualid ~loc (sloc,s) = |
| 680 |
let elpi = ensure_initialized () in |
| 681 |
let base = EC.(empty_base ~elpi) in |
| 682 |
let cmd_sig_base = command_base_signature ~loc in |
| 683 |
let base = extend_with_idependent_units ~base ~loc cmd_sig_base in |
| 684 |
let units = private__units_from_string ~elpi ~base ~loc sloc s in |
| 685 |
add_to_db qualid (cmd_sig_base @ units) [] Rocq_elpi_utils.SuperGlobal |
| 686 |
|
| 687 |
let lp_tactic_ast = Summary.ref ~name:("elpi-lp-tactic") None |
| 688 |
let in_lp_tactic_ast : src list -> Libobject.obj = |
| 689 |
let open Libobject in |
| 690 |
declare_object { (default_object "ELPI-LP-TACTIC") with |
| 691 |
load_function = (fun _ x -> lp_tactic_ast := Some x); |
| 692 |
classify_function = (fun _ -> Keep); |
| 693 |
} |
| 694 |
let load_tactic ~loc s = |
| 695 |
let elpi = ensure_initialized () in |
| 696 |
let units = private__units_from_file ~elpi ~loc ~base:(EC.empty_base ~elpi) s in |
| 697 |
let base = List.map (fun u -> File { fname = name_of_cunit u; fast = u }) units in |
| 698 |
lp_tactic_ast := Some base; |
| 699 |
Lib.add_leaf (in_lp_tactic_ast base) |
| 700 |
let tactic_init () = |
| 701 |
match !lp_tactic_ast with |
| 702 |
| None -> CErrors.user_err Pp.(str "Elpi TacticTemplate was not called") |
| 703 |
| Some ast -> ast |
| 704 |
|
| 705 |
let init_program_units (loc,qualid) ~loc (init : src list) = |
| 706 |
if stage = Summary.Stage.Interp && Global.sections_are_opened () then |
| 707 |
CErrors.user_err Pp.(str "Program/Tactic/Db cannot be declared inside sections") |
| 708 |
else |
| 709 |
init_program qualid ~loc init |
| 710 |
|
| 711 |
let init_tactic n ~loc = init_program_units n loc (command_init () @ tactic_init ()) |
| 712 |
let init_command n ~loc = init_program_units n loc (command_init ()) |
| 713 |
|
| 714 |
let init_program p ~loc (sloc, s) = |
| 715 |
let elpi = ensure_initialized () in |
| 716 |
let base = EC.(empty_base ~elpi) in |
| 717 |
let units = private__units_from_string ~elpi ~base ~loc sloc s in |
| 718 |
init_program_units p ~loc (List.map (fun u -> File { fname = name_of_cunit u; fast = u}) units) |
| 719 |
|
| 720 |
|
| 721 |
let init_db (_,qualid) ~loc header = |
| 722 |
if stage = Summary.Stage.Interp && Global.sections_are_opened () then |
| 723 |
CErrors.user_err Pp.(str "Program/Tactic/Db cannot be declared inside sections") |
| 724 |
else if stage = Summary.Stage.Interp && match Global.current_modpath () with Names.ModPath.MPdot (Names.ModPath.MPfile _,_) -> true | _ -> false then |
| 725 |
CErrors.user_err Pp.(str "Program/Tactic/Db cannot be declared inside modules") |
| 726 |
else |
| 727 |
init_db qualid ~loc header |
| 728 |
;; |
| 729 |
|
| 730 |
let accumulate_to_program p (v : src) = |
| 731 |
if not (program_exists p) then |
| 732 |
CErrors.user_err Pp.(str "No Elpi Program named " ++ pr_qualified_name p); |
| 733 |
add_to_program p v |
| 734 |
|
| 735 |
let accumulate_to_db p v ~vs ~scope = |
| 736 |
if not (db_exists p) then |
| 737 |
CErrors.user_err Pp.(str "No Elpi Db " ++ pr_qualified_name p); |
| 738 |
add_to_db p v vs scope |
| 739 |
|
| 740 |
let in_stage = in_stage |
| 741 |
let stage = stage |
| 742 |
|
| 743 |
let db_inspect name = |
| 744 |
try Names.KNset.cardinal (SLMap.find name !db_name_src).units |
| 745 |
with Not_found -> -1 |
| 746 |
|
| 747 |
let unit_from_ast ?error_header ~elpi ~base ~loc ast = |
| 748 |
unit_from_ast ?error_header ~elpi ~base ~loc ast ~flags:(cc_flags ()) |
| 749 |
|
| 750 |
let unit_signature_from_ast ~elpi ~base ~loc ast = |
| 751 |
unit_signature_from_ast ~elpi ~base ~loc ast ~flags:(cc_flags ()) |
| 752 |
|
| 753 |
let unit_from_plugin ?error_header ~elpi ~base ~loc builtins = |
| 754 |
unit_from_plugin ?error_header ~elpi ~base ~loc builtins ~flags:(cc_flags ()) |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
let compiler_cache_code = Summary.ref ~local:true |
| 759 |
~name:("elpi-compiler-cache-code") |
| 760 |
Int.Map.empty |
| 761 |
let compiler_cache_chunk = Summary.ref ~local:true |
| 762 |
~name:("elpi-compiler-cache-chunk") |
| 763 |
Int.Map.empty |
| 764 |
|
| 765 |
let programs_tip = Summary.ref ~local:true |
| 766 |
~name:("elpi-compiler-cache-gc") |
| 767 |
SLMap.empty |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
type cache_stats = { lookups : int; hits : int } |
| 772 |
let cache_stats = ref { lookups = 0; hits = 0 } |
| 773 |
|
| 774 |
let find_with_stats k t = |
| 775 |
let { lookups; hits } = !cache_stats in |
| 776 |
try |
| 777 |
let rc = Int.Map.find k t in |
| 778 |
cache_stats := { lookups = lookups + 1; hits = hits + 1 }; |
| 779 |
rc |
| 780 |
with Not_found -> |
| 781 |
cache_stats := { lookups = lookups + 1; hits }; |
| 782 |
raise Not_found |
| 783 |
|
| 784 |
let () = at_exit (fun () -> |
| 785 |
let { lookups; hits } = !cache_stats in |
| 786 |
Rocq_elpi_utils.debug Pp.(fun () -> |
| 787 |
str(Format.asprintf "Compiler cache: lookups=%d, hits=%d\n" lookups hits)) |
| 788 |
) |
| 789 |
|
| 790 |
let lookup b h src r cmp pp = |
| 791 |
let h = combine_hash b h in |
| 792 |
let p, src' = find_with_stats h !r in |
| 793 |
if cmp src src' then p else |
| 794 |
let () = Format.eprintf "@[%a@]%!" pp src in |
| 795 |
let () = Format.eprintf "@[%a@]%!" pp src' in |
| 796 |
assert false |
| 797 |
|
| 798 |
let cache b h prog src r = |
| 799 |
let h = combine_hash b h in |
| 800 |
r := Int.Map.add h (prog,src) !r; |
| 801 |
prog |
| 802 |
|
| 803 |
let uncache b h r = |
| 804 |
let h = combine_hash b h in |
| 805 |
r := Int.Map.remove h !r |
| 806 |
|
| 807 |
let lookup_code b h src = lookup b h src compiler_cache_code (Code.eq Chunk.eq) (Code.pp Chunk.pp) |
| 808 |
let lookup_chunk b h src = lookup b h src compiler_cache_chunk Chunk.eq Chunk.pp |
| 809 |
|
| 810 |
let cache_code b h prog src = cache b h prog src compiler_cache_code |
| 811 |
let cache_chunk b h prog src = cache b h prog src compiler_cache_chunk |
| 812 |
|
| 813 |
let recache_code b h1 h2 p src = |
| 814 |
uncache b h1 compiler_cache_code; |
| 815 |
cache_code b h2 p src |
| 816 |
|
| 817 |
let recache_chunk b h1 h2 p src = |
| 818 |
uncache b h1 compiler_cache_chunk; |
| 819 |
cache_chunk b h2 p src |
| 820 |
|
| 821 |
let compile ~loc src = |
| 822 |
let rec compile_code src = |
| 823 |
let h = Code.hash src in |
| 824 |
try |
| 825 |
lookup_code 0 h src |
| 826 |
with Not_found -> |
| 827 |
match src with |
| 828 |
| Code.Base { base = u } -> |
| 829 |
let elpi = ensure_initialized () in |
| 830 |
let prog = extend_with_unit ~base:(EC.empty_base ~elpi) ~loc u in |
| 831 |
cache_code 0 h prog src |
| 832 |
| Code.Snoc { prev; source } -> |
| 833 |
let base = compile_code prev in |
| 834 |
let prog = extend_with_unit ~base ~loc source in |
| 835 |
if Code.cache src then cache_code 0 h prog src else prog |
| 836 |
| Code.Snoc_db { prev; chunks } -> |
| 837 |
let base = compile_code prev in |
| 838 |
let prog = compile_chunk (Code.hash prev) base chunks in |
| 839 |
prog |
| 840 |
and compile_chunk bh base src = |
| 841 |
|
| 842 |
let h = Chunk.hash src in |
| 843 |
try |
| 844 |
lookup_chunk bh h src |
| 845 |
with Not_found -> |
| 846 |
match src with |
| 847 |
| Chunk.Base { base = _ } -> |
| 848 |
base |
| 849 |
| Chunk.Snoc { prev; source_rev } -> |
| 850 |
let base = compile_chunk bh base prev in |
| 851 |
let prog = extend_with_idependent_units ~base ~loc (List.rev source_rev) in |
| 852 |
recache_chunk bh (Chunk.hash prev) h prog src |
| 853 |
in |
| 854 |
compile_code src |
| 855 |
|
| 856 |
let get_and_compile ~loc ?even_if_empty name : (EC.program * bool) option = |
| 857 |
let t = Unix.gettimeofday () in |
| 858 |
let res = code ?even_if_empty name |> Option.map (fun src -> |
| 859 |
|
| 860 |
let prog = compile ~loc src in |
| 861 |
let new_hash = Code.hash src in |
| 862 |
let old_hash = try SLMap.find name !programs_tip with Not_found -> 0 in |
| 863 |
programs_tip := SLMap.add name new_hash !programs_tip; |
| 864 |
let prog = recache_code 0 old_hash new_hash prog src in |
| 865 |
let raw_args = |
| 866 |
match get_nature name with |
| 867 |
| Command { raw_args } -> raw_args |
| 868 |
| Program { raw_args } -> raw_args |
| 869 |
| Tactic -> true in |
| 870 |
(prog, raw_args)) in |
| 871 |
Rocq_elpi_utils.elpitime (fun _ -> Pp.(str(Printf.sprintf "Elpi: get_and_compile %1.4f" (Unix.gettimeofday () -. t)))); |
| 872 |
res |
| 873 |
|
| 874 |
let rec compile_db ~loc src = |
| 875 |
let h = Chunk.hash src in |
| 876 |
try |
| 877 |
lookup_chunk 0 h src |
| 878 |
with Not_found -> |
| 879 |
match src with |
| 880 |
| Chunk.Base { base } -> |
| 881 |
let elpi = ensure_initialized () in |
| 882 |
let prog = extend_with_idependent_units ~loc ~base:(EC.empty_base ~elpi) base in |
| 883 |
cache_chunk 0 h prog src |
| 884 |
| Chunk.Snoc { prev; source_rev } -> |
| 885 |
let base = compile_db ~loc prev in |
| 886 |
let prog = extend_with_idependent_units ~loc ~base (List.rev source_rev) in |
| 887 |
cache_chunk 0 h prog src |
| 888 |
|
| 889 |
let get_and_compile_existing_db ~loc name : EC.program = |
| 890 |
match db_code name with |
| 891 |
| None -> err Pp.(str "Unknown Db " ++ pr_qualified_name name) |
| 892 |
| Some db -> compile_db ~loc db |
| 893 |
|
| 894 |
|
| 895 |
let get_and_compile_existing_program ~loc program : EC.program = |
| 896 |
match get_and_compile ~loc ~even_if_empty:true program with |
| 897 |
| None -> CErrors.user_err ~loc Pp.(str "Unknown program " ++ pr_qualified_name program) |
| 898 |
| Some (base, _) -> base |
| 899 |
|
| 900 |
let asts_from_file ~elpi ~base ~loc x : EC.scoped_program list = |
| 901 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 902 |
let ast = EP.program ~elpi ~file:x in |
| 903 |
EC.scope_ast ~elpi ast) |
| 904 |
|
| 905 |
let asts_from_string ~loc (sloc,s) : EC.scoped_program list = |
| 906 |
handle_elpi_compiler_errors ~loc (fun () -> |
| 907 |
let elpi = ensure_initialized () in |
| 908 |
let digest = Digest.(string s) in |
| 909 |
let ast = EP.program_from ~elpi ~loc:sloc ~digest (Lexing.from_string s) in |
| 910 |
EC.scope_ast ~elpi ast) |
| 911 |
|
| 912 |
let accumulate_ast_to_X ~get_and_compile ~accumulate ~loc ~x ~what ast = |
| 913 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 914 |
let elpi = ensure_initialized () in |
| 915 |
let base = get_and_compile ~loc x in |
| 916 |
let u = |
| 917 |
match what with |
| 918 |
| Code -> unit_from_ast ~elpi ~base ~loc ast |
| 919 |
| SignatureOnly -> unit_signature_from_ast ~elpi ~base ~loc ast in |
| 920 |
accumulate x u |
| 921 |
end |
| 922 |
|
| 923 |
let accumulate_ast_to_program ~loc ~program ~what ~ast = |
| 924 |
accumulate_ast_to_X ~loc ~x:program ~what ast |
| 925 |
~get_and_compile:get_and_compile_existing_program |
| 926 |
~accumulate:(fun n u -> accumulate_to_program n (File { fname = name_of_cunit u; fast = u })) |
| 927 |
|
| 928 |
let accumulate_ast_to_db ~loc ~db ~what ~ast ~scope = |
| 929 |
accumulate_ast_to_X ~loc ~x:db ~what ast |
| 930 |
~get_and_compile:get_and_compile_existing_db |
| 931 |
~accumulate:(fun n u -> |
| 932 |
accumulate_to_db ~scope ~vs:[] n [u]) |
| 933 |
|
| 934 |
let accumulate_file_to_X ~get_and_compile ~accumulate ~loc ~x ~what ~file = |
| 935 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 936 |
let elpi = ensure_initialized () in |
| 937 |
let base = get_and_compile ~loc x in |
| 938 |
let asts = asts_from_file ~loc ~elpi ~base file in |
| 939 |
List.iter (accumulate_ast_to_X ~get_and_compile ~accumulate ~loc ~x ~what) asts |
| 940 |
end |
| 941 |
|
| 942 |
let accumulate_file_to_program ~loc ~program ~what ~file = |
| 943 |
accumulate_file_to_X ~loc ~x:program ~what ~file |
| 944 |
~get_and_compile:get_and_compile_existing_program |
| 945 |
~accumulate:(fun n u -> accumulate_to_program n (File { fname = name_of_cunit u; fast = u })) |
| 946 |
|
| 947 |
let accumulate_file_to_db ~loc ~db ~what ~file ~scope = |
| 948 |
accumulate_file_to_X ~loc ~x:db ~what ~file |
| 949 |
~get_and_compile:get_and_compile_existing_db |
| 950 |
~accumulate:(fun n u -> accumulate_to_db ~scope ~vs:[] n [u]) |
| 951 |
|
| 952 |
let accumulate_string_to_program ~loc ~program ~code = |
| 953 |
asts_from_string ~loc code |
| 954 |
|> List.iter (fun ast -> accumulate_ast_to_program ~loc ~program ~what:Code ~ast) |
| 955 |
|
| 956 |
let accumulate_string_to_db ~loc ~db ~code ~scope = |
| 957 |
asts_from_string ~loc code |
| 958 |
|> List.iter (fun ast -> accumulate_ast_to_db ~loc ~db ~what:Code ~ast ~scope) |
| 959 |
|
| 960 |
let accumulate_plugin_to_program ~loc ~program ~plugin = |
| 961 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 962 |
let elpi = ensure_initialized () in |
| 963 |
let base = get_and_compile_existing_program ~loc program in |
| 964 |
let u = unit_from_plugin ~loc ~elpi ~base plugin in |
| 965 |
accumulate_to_program program (File { fname = name_of_cunit u; fast = u }) |
| 966 |
end |
| 967 |
|
| 968 |
let accumulate_db_to_program ~loc ~program ~db = |
| 969 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 970 |
if db_exists db then |
| 971 |
let header = header_of_db db |> List.map (fun dast -> DatabaseHeader { dast }) in |
| 972 |
(header @ [DatabaseBody db]) |> List.iter (fun src -> |
| 973 |
accumulate_to_program program src) |
| 974 |
else |
| 975 |
CErrors.user_err Pp.(str "Db " ++ pr_qualified_name db ++ str" not found") |
| 976 |
end |
| 977 |
|
| 978 |
|
| 979 |
let accumulate_units_to_db ~loc ~db ~units ~secvars ~scope = |
| 980 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 981 |
accumulate_to_db db ~scope ~vs:secvars units |
| 982 |
end |
| 983 |
|
| 984 |
|
| 985 |
let accumulate_header_to_program ~loc ~program ~header = |
| 986 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 987 |
List.iter (fun u -> accumulate_to_program program (File { fname = name_of_cunit u; fast = u })) header |
| 988 |
end |
| 989 |
|
| 990 |
let accumulate_header_to_db ~loc ~db ~header ~scope = |
| 991 |
handle_elpi_compiler_errors ~loc begin fun () -> |
| 992 |
accumulate_to_db db ~scope ~vs:[] header |
| 993 |
end |
| 994 |
|
| 995 |
|
| 996 |
let check ~loc ~program = handle_elpi_compiler_errors ~loc (fun () -> get_and_compile_existing_program ~loc program |> ignore) |
| 997 |
let check_db ~loc ~db = handle_elpi_compiler_errors ~loc (fun () -> get_and_compile_existing_db ~loc db |> ignore) |
| 998 |
let accumulate_file_to_program ~loc ~program ~what ~file = accumulate_file_to_program ~loc ~program ~what ~file; check ~loc ~program |
| 999 |
let accumulate_ast_to_program ~loc ~program ~what ~ast = accumulate_ast_to_program ~loc ~program ~what ~ast; check ~loc ~program |
| 1000 |
let accumulate_string_to_program ~loc ~program ~code = accumulate_string_to_program ~loc ~program ~code; check ~loc ~program |
| 1001 |
let accumulate_plugin_to_program ~loc ~program ~plugin = accumulate_plugin_to_program ~loc ~program ~plugin; check ~loc ~program |
| 1002 |
let accumulate_db_to_program ~loc ~program ~db = accumulate_db_to_program ~loc ~program ~db; check ~loc ~program |
| 1003 |
let accumulate_header_to_program ~loc ~program ~header = accumulate_header_to_program ~loc ~program ~header; check ~loc ~program |
| 1004 |
let accumulate_file_to_db ~loc ~db ~what ~file ~scope = accumulate_file_to_db ~loc ~db ~what ~file ~scope; check_db ~loc ~db |
| 1005 |
let accumulate_ast_to_db ~loc ~db ~what ~ast ~scope = accumulate_ast_to_db ~loc ~db ~what ~ast ~scope; check_db ~loc ~db |
| 1006 |
let accumulate_string_to_db ~loc ~db ~code ~scope = accumulate_string_to_db ~loc ~db ~code ~scope; check_db ~loc ~db |
| 1007 |
let accumulate_units_to_db ~loc ~db ~units ~secvars ~scope = accumulate_units_to_db ~loc ~db ~units ~secvars ~scope; check_db ~loc ~db |
| 1008 |
let accumulate_header_to_db ~loc ~db ~header ~scope = accumulate_header_to_db ~loc ~db ~header ~scope; check_db ~loc ~db |
| 1009 |
|
| 1010 |
let init_program name ~loc sources = init_program name ~loc sources; check ~loc ~program:(snd name) |
| 1011 |
|
| 1012 |
end |
| 1013 |
|
| 1014 |
|
| 1015 |
let elpi_builtins = |
| 1016 |
API.BuiltIn.declare |
| 1017 |
~file_name:"elpi-builtin.elpi" |
| 1018 |
Elpi.Builtin.( |
| 1019 |
core_builtins @ |
| 1020 |
elpi_builtins @ |
| 1021 |
elpi_nonlogical_builtins @ |
| 1022 |
elpi_stdlib @ |
| 1023 |
elpi_map @ |
| 1024 |
elpi_set @ |
| 1025 |
io_builtins @ |
| 1026 |
ocaml_runtime @ |
| 1027 |
lp_builtins @ |
| 1028 |
[] |
| 1029 |
) |
| 1030 |
|
| 1031 |
let coq_interp_builtins = |
| 1032 |
API.BuiltIn.declare |
| 1033 |
~file_name:"coq-builtin.elpi" |
| 1034 |
begin |
| 1035 |
Rocq_elpi_builtins.coq_header_builtins @ |
| 1036 |
Rocq_elpi_builtins.coq_misc_builtins @ |
| 1037 |
Rocq_elpi_builtins.coq_locate_builtins @ |
| 1038 |
Rocq_elpi_builtins.coq_rest_builtins |
| 1039 |
end |
| 1040 |
|
| 1041 |
let coq_synterp_builtins = |
| 1042 |
API.BuiltIn.declare |
| 1043 |
~file_name:"coq-builtin-synterp.elpi" |
| 1044 |
begin |
| 1045 |
[ |
| 1046 |
API.BuiltIn.LPCode {| |
| 1047 |
kind implicit_kind type. |
| 1048 |
kind field-attribute type. |
| 1049 |
kind upoly-decl type. |
| 1050 |
kind upoly-decl-cumul type. |
| 1051 |
|
| 1052 |
|} |
| 1053 |
] @ |
| 1054 |
Rocq_elpi_builtins.coq_misc_builtins @ |
| 1055 |
Rocq_elpi_builtins_synterp.coq_synterp_builtins |
| 1056 |
end |
| 1057 |
|
| 1058 |
let warn_mul_paths = CWarnings.create ~name:"elpi.multiple-paths" (fun x -> x) |
| 1059 |
|
| 1060 |
|
| 1061 |
let resolve_file_path ~must_exist ~allow_absolute ~only_elpi file = |
| 1062 |
|
| 1063 |
if allow_absolute && not (Filename.is_relative file) then |
| 1064 |
if not must_exist || Sys.file_exists file then file else |
| 1065 |
let msg = "The referenced absolute path " ^ file ^ " does not exist." in |
| 1066 |
CErrors.user_err (Pp.str msg) |
| 1067 |
else |
| 1068 |
|
| 1069 |
let (logpath, relpath) = |
| 1070 |
|
| 1071 |
let path = |
| 1072 |
if String.length file >= 6 && String.sub file 0 6 = "coq://" then |
| 1073 |
String.sub file 6 (String.length file - 6) |
| 1074 |
else file |
| 1075 |
in |
| 1076 |
|
| 1077 |
let path = |
| 1078 |
if only_elpi && Filename.check_suffix path ".elpi" then |
| 1079 |
String.sub path 0 (String.length path - 5) |
| 1080 |
else path |
| 1081 |
in |
| 1082 |
match String.split_on_char '/' path with |
| 1083 |
| logpath :: ((_ :: _) as relpath) -> |
| 1084 |
let logpath = |
| 1085 |
let logpath = String.split_on_char '.' logpath in |
| 1086 |
let logpath = List.rev_map Names.Id.of_string logpath in |
| 1087 |
Names.DirPath.make logpath |
| 1088 |
in |
| 1089 |
let relpath = |
| 1090 |
let relpath = String.concat "/" relpath in |
| 1091 |
if only_elpi then relpath ^ ".elpi" else relpath |
| 1092 |
in |
| 1093 |
(logpath, relpath) |
| 1094 |
| _ -> |
| 1095 |
let msg = |
| 1096 |
"File reference " ^ file ^ " is ill-formed, and cannot be resolved." |
| 1097 |
in |
| 1098 |
CErrors.user_err (Pp.str msg) |
| 1099 |
in |
| 1100 |
|
| 1101 |
let mk_file_path p = Filename.concat (Loadpath.physical p) relpath in |
| 1102 |
let loadpath = Loadpath.find_with_logical_path logpath in |
| 1103 |
let loadpath = |
| 1104 |
if not must_exist then loadpath |
| 1105 |
else List.filter (fun p -> Sys.file_exists (mk_file_path p)) loadpath in |
| 1106 |
let loadpath = |
| 1107 |
match loadpath with |
| 1108 |
| [] -> |
| 1109 |
let msg = |
| 1110 |
"No loadpath found with logical name " ^ |
| 1111 |
Names.DirPath.to_string logpath ^ |
| 1112 |
", cannot resolve file reference " ^ file ^ "." |
| 1113 |
in |
| 1114 |
CErrors.user_err (Pp.str msg) |
| 1115 |
| (p :: xs) as ps -> |
| 1116 |
(if xs <> [] then |
| 1117 |
let msg = |
| 1118 |
Format.sprintf |
| 1119 |
"Multiple loadpaths found with logical name %s, while resolving file reference %s:\n%s\nThe chosen path is the first in the list" |
| 1120 |
(Names.DirPath.to_string logpath) |
| 1121 |
file |
| 1122 |
(String.concat "\n" (List.map (fun lp -> |
| 1123 |
Printf.sprintf "- %s -> %s" |
| 1124 |
(Names.DirPath.to_string (Loadpath.logical lp)) |
| 1125 |
(Loadpath.physical lp) |
| 1126 |
) ps)) |
| 1127 |
in |
| 1128 |
warn_mul_paths (Pp.str msg)); |
| 1129 |
p |
| 1130 |
in |
| 1131 |
|
| 1132 |
let resolved = mk_file_path loadpath in |
| 1133 |
if not must_exist || Sys.file_exists resolved then resolved else |
| 1134 |
let msg = |
| 1135 |
"File reference " ^ file ^ " was resolved to " ^ resolved ^ |
| 1136 |
" but the file does not exist." |
| 1137 |
in |
| 1138 |
CErrors.user_err (Pp.str msg) |
| 1139 |
|
| 1140 |
let file_resolver ?cwd:_ ~unit:file () = |
| 1141 |
resolve_file_path ~must_exist:true ~allow_absolute:true ~only_elpi:true file |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
let versions = |
| 1146 |
let open API.Setup.StrMap in |
| 1147 |
empty |
| 1148 |
|> add "coq-elpi" (API.Utils.version_parser ~what:"coq-elpi" "%%VERSION_NUM%%") |
| 1149 |
|> add "rocq-elpi" (API.Utils.version_parser ~what:"rocq-elpi" "%%VERSION_NUM%%") |
| 1150 |
|> add "coq" (API.Utils.version_parser ~what:"coq" Coq_config.version) |
| 1151 |
|> add "rocq" (API.Utils.version_parser ~what:"rocq" Coq_config.version) |
| 1152 |
|
| 1153 |
module Synterp : Programs = struct |
| 1154 |
module S = struct |
| 1155 |
let stage = Summary.Stage.Synterp |
| 1156 |
let in_stage x = x ^ "-synterp" |
| 1157 |
let init () = |
| 1158 |
API.Setup.init ~versions |
| 1159 |
~state:synterp_state ~hoas:synterp_hoas |
| 1160 |
~quotations:synterp_quotations ~builtins:[elpi_builtins;coq_synterp_builtins] ~file_resolver () |
| 1161 |
end |
| 1162 |
include SourcesStorage(S) |
| 1163 |
|
| 1164 |
let () = Rocq_elpi_builtins_synterp.set_accumulate_to_db_synterp (fun ~loc clauses_to_add -> |
| 1165 |
let elpi = ensure_initialized () in |
| 1166 |
let clauses_to_add = clauses_to_add |> group_clauses |> |
| 1167 |
List.map (fun (dbname,asts,vs,scope) -> |
| 1168 |
let base = get_and_compile_existing_db ~loc dbname in |
| 1169 |
let scoped_asts = List.concat (List.map (EC.scope_ast ~elpi) asts) in |
| 1170 |
let units = List.map (fun x -> |
| 1171 |
unit_from_ast ~error_header:(Format.asprintf "accumulating clause to %s" (String.concat "." dbname)) ~elpi ~base ~loc x) scoped_asts in |
| 1172 |
dbname,units,vs,scope) in |
| 1173 |
clauses_to_add |> List.iter (fun (dbname,units,vs,scope) -> |
| 1174 |
accumulate_to_db dbname units ~vs ~scope)) |
| 1175 |
|
| 1176 |
end |
| 1177 |
module Interp : Programs = struct |
| 1178 |
include SourcesStorage(struct |
| 1179 |
let stage = Summary.Stage.Interp |
| 1180 |
let in_stage x = x ^ "-interp" |
| 1181 |
let init () = |
| 1182 |
API.Setup.init ~versions |
| 1183 |
~state:interp_state ~hoas:interp_hoas |
| 1184 |
~quotations:interp_quotations ~builtins:[elpi_builtins;coq_interp_builtins] ~file_resolver () |
| 1185 |
end) |
| 1186 |
|
| 1187 |
let () = Rocq_elpi_builtins.set_accumulate_to_db_interp (fun ~loc clauses_to_add -> |
| 1188 |
let elpi = ensure_initialized () in |
| 1189 |
let clauses_to_add = clauses_to_add |> group_clauses |> |
| 1190 |
List.map (fun (dbname,asts,vs,scope) -> |
| 1191 |
let base = get_and_compile_existing_db ~loc dbname in |
| 1192 |
let scoped_asts = List.concat (List.map (EC.scope_ast ~elpi) asts) in |
| 1193 |
let units = List.map (fun x -> unit_from_ast ~error_header:(Format.asprintf "accumulating clause to %s" (String.concat "." dbname)) ~elpi ~base ~loc x) scoped_asts in |
| 1194 |
dbname,units,vs,scope) in |
| 1195 |
clauses_to_add |> List.iter (fun (dbname,units,vs,scope) -> |
| 1196 |
accumulate_to_db dbname units ~vs ~scope)) |
| 1197 |
|
| 1198 |
let () = Rocq_elpi_builtins.set_accumulate_text_to_db_interp (fun ~loc n txt scope -> |
| 1199 |
let elpi = ensure_initialized () in |
| 1200 |
let base = get_and_compile_existing_db ~loc n in |
| 1201 |
let u = private__units_from_string ~elpi ~base ~loc (of_coq_loc loc) txt in |
| 1202 |
accumulate_to_db n u ~vs:[] ~scope) |
| 1203 |
|
| 1204 |
end |
| 1205 |
|
| 1206 |
let document_builtins () = |
| 1207 |
API.BuiltIn.document_file coq_interp_builtins; |
| 1208 |
API.BuiltIn.document_file coq_synterp_builtins; |
| 1209 |
API.BuiltIn.document_file ~header:"% Generated file, do not edit" elpi_builtins |
| 1210 |
|