package lambdapi
Source file compile.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132(** High-level compilation functions. *) open Lplib open Timed open Common open Error open Library open Parsing open Core open Sign (** [gen_obj] indicates whether we should generate object files when compiling source files. The default behaviour is not te generate them. *) let gen_obj = Stdlib.ref false (** [source base] returns which file among [base.dk] and [base.lp] to use. *) let source base = let lp_src = base ^ lp_src_extension and dk_src = base ^ dk_src_extension in match Sys.file_exists lp_src, Sys.file_exists dk_src with | false, false -> fatal_no_pos {|File "%s.lp" (or .dk) not found.|} base | true , true -> wrn None {|Both "%s" and "%s" exist. We take "%s".|} lp_src dk_src lp_src; lp_src | true , false -> lp_src | false, true -> dk_src (** [compile ss mp] returns the signature associated to the module path [mp]. The corresponding file is processed only when the corresponding object file does not exist or must be updated. In that case, the signature is stored in the corresponding object file if [!gen_obj] is [true]. The sig_state [ss] is only used to get the "String" builtin. *) let rec compile : Command.compiler = fun ss mp -> if mp = Ghost.path then Ghost.sign else if List.mem mp !loading then begin let base = file_of_path mp in fatal_msg {|Circular dependency detected in "%s".@.|} (source base); fatal_msg "Dependency stack for module:@."; List.iter (fatal_msg "- %a@." Path.pp) !loading; fatal_no_pos "Build aborted." end else match Path.Map.find_opt mp !loaded with | Some sign -> sign | None -> let base = file_of_path mp in let obj = base ^ obj_extension in let src = source base in if Extra.more_recent src obj then begin Console.out 1 (Color.blu {|Start checking "%s"|}) src; loading := mp :: !loading; let sign = Sign.create mp in (* [sign] is added to [loaded] before processing the commands so that it is possible to qualify the symbols of the current modules. *) loaded := Path.Map.add mp sign !loaded; let a = Tactic.reset_admitted() in Sig_state.update_ext_sym_dtrees false ss; let consume = let new_ss = Stdlib.ref (Sig_state.of_sign sign) in fun cmd -> Stdlib.(new_ss := Command.handle compile !new_ss cmd) in try Debug.stream_iter consume (Parser.parse_file src); Sig_state.update_ext_sym_dtrees true ss; Tactic.restore_admitted a; Console.out 1 (Color.blu {|End checking "%s"|}) src; Sign.strip_private sign; if Stdlib.(!gen_obj) then begin Console.out 2 (Color.blu {|Write "%s"|}) obj; Sign.write sign obj end; loading := List.tl !loading; sign with LpParser.UnfinishedProof(log_msg, _, _) -> let pos, msg = match log_msg with | { pos=Some p; elt } -> p, elt | _ -> assert false in Parsing.Parser.parser_fatal pos "Syntax error. %s" msg end else begin Console.out 2 (Color.blu {|Load "%s"|}) obj; let sign = Sign.read obj in if sign.sign_path <> mp then fatal_no_pos {|The file "%s" has path "%a" instead of "%a". \ Remove it and compile it again.|} obj Path.pp sign.sign_path Path.pp mp else begin (* We recursively load every module [mp'] on which [mp] depends. *) Path.Map.iter (fun mp' _ -> ignore (compile ss mp')) !(sign.sign_deps); loaded := Path.Map.add mp sign !loaded; Sign.link sign; (* Since the ghost signature is implicitly loaded but not linked, we need to explicitly update the decision tree of ghost symbols and the type of string literals with the String builtin. *) (* We first search for the symbol corresponding to the builtin "String" among loaded signatures. *) let search_string_type() = let exception Found of Core.Term.sym in try Path.Map.iter (fun _ sign -> match Extra.StrMap.find_opt "String" !(sign.sign_builtins) with | Some s -> raise (Found s) | None -> () ) !loaded; Console.out 0 {|Builtin "String" undefined.|}; assert false with Found s -> Term.mk_Symb s in let string_type = let string = ref None in fun () -> match !string with | None -> let t = search_string_type() in string := Some t; t | Some t -> t in let update s = if String.is_string_literal s.Term.sym_name then s.sym_type := string_type() else Tree.update s in Ghost.iter update; if Stdlib.(!Common.Console.lsp_mod) then Tool.Indexing.index_sign sign; sign end end (** [compile_file f] looks for a package configuration file for [f] and compiles it. *) let compile_file (f:string): Sign.t = if not (Sys.file_exists f) then fatal_no_pos {|"%s" does not exist.|} f; Package.set_root_path (Filename.dirname f); compile Sig_state.dummy (path_of_file LpLexer.escape f)