diff --git a/compiler/compileArg.ml b/compiler/compileArg.ml index 605d0ac53d..540707cdd5 100644 --- a/compiler/compileArg.ml +++ b/compiler/compileArg.ml @@ -17,8 +17,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler -open Js_of_ocaml_compiler.Stdlib open Cmdliner type t = @@ -248,7 +248,7 @@ let options = else None in let source_map = - if source_map <> None && not Source_map_io.enabled + if Option.is_some source_map && not Source_map_io.enabled then ( warn "Warning: '--source-map' flag ignored because js_of_ocaml was compiled \ diff --git a/compiler/js_of_ocaml.ml b/compiler/js_of_ocaml.ml index bcf62e14f0..b0d3778edd 100644 --- a/compiler/js_of_ocaml.ml +++ b/compiler/js_of_ocaml.ml @@ -18,8 +18,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler -open Js_of_ocaml_compiler.Stdlib let times = Debug.find "times" @@ -133,14 +133,16 @@ let f in if times () then Format.eprintf "Start parsing...@."; let need_debug = - if source_map <> None || Config.Flag.debuginfo () || toplevel + if Option.is_some source_map || Config.Flag.debuginfo () || toplevel then `Full else if Config.Flag.pretty () then `Names else `No in let check_debug debug = - if (not runtime_only) && source_map <> None && Parse_bytecode.Debug.is_empty debug + if (not runtime_only) + && Option.is_some source_map + && Parse_bytecode.Debug.is_empty debug then warn "Warning: '--source-map' is enabled but the bytecode program was compiled with \ @@ -260,7 +262,7 @@ let f | (`Stdout, _), false -> `Stdout | (`Name x, _), false -> `Name x | (`Name x, true), true - when String.length x > 0 && x.[String.length x - 1] = '/' -> + when String.length x > 0 && Char.equal x.[String.length x - 1] '/' -> `Name (gen_unit_filename x cmo) | (`Name _, true), true | (`Stdout, true), true -> failwith "use [-o dirname/] or remove [--keep-unit-names]" @@ -277,8 +279,8 @@ let f match output_file with | `Stdout, false -> `Name (gen_unit_filename "./" cmo) | `Name x, false -> `Name (gen_unit_filename (Filename.dirname x) cmo) - | `Name x, true when String.length x > 0 && x.[String.length x - 1] = '/' - -> + | `Name x, true + when String.length x > 0 && Char.equal x.[String.length x - 1] '/' -> `Name (gen_unit_filename x cmo) | `Stdout, true | `Name _, true -> failwith "use [-o dirname/] or remove [--keep-unit-names]" diff --git a/compiler/jsoo_link.ml b/compiler/jsoo_link.ml index fe3ab27867..00a30fe33f 100644 --- a/compiler/jsoo_link.ml +++ b/compiler/jsoo_link.ml @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler let f {LinkerArg.output_file; source_map; resolve_sourcemap_url; js_files} = diff --git a/compiler/jsoo_minify.ml b/compiler/jsoo_minify.ml index 40e512bf96..a25ada4051 100644 --- a/compiler/jsoo_minify.ml +++ b/compiler/jsoo_minify.ml @@ -17,8 +17,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler -open Js_of_ocaml_compiler.Stdlib let error k = Format.ksprintf (fun s -> failwith s) k diff --git a/compiler/lib/base64.ml b/compiler/lib/base64.ml index bec7b68cfe..5d99b74515 100644 --- a/compiler/lib/base64.ml +++ b/compiler/lib/base64.ml @@ -19,6 +19,8 @@ * *) +open! Stdlib + type ('a, 'b) result = | Ok of 'a | Error of 'b @@ -51,9 +53,9 @@ let make_alphabet alphabet = if String.length alphabet <> 64 then invalid_arg "Length of alphabet must be 64"; if String.contains alphabet '=' then invalid_arg "Alphabet can not contain padding character"; - let emap = Array.init (String.length alphabet) (fun i -> Char.code alphabet.[i]) in + let emap = Array.init (String.length alphabet) ~f:(fun i -> Char.code alphabet.[i]) in let dmap = Array.make 256 none in - String.iteri (fun idx chr -> dmap.(Char.code chr) <- idx) alphabet; + String.iteri ~f:(fun idx chr -> dmap.(Char.code chr) <- idx) alphabet; {emap; dmap} let length_alphabet {emap; _} = Array.length emap @@ -146,7 +148,7 @@ let encode_sub pad {emap; _} ?(off = 0) ?len input = let encode ?(pad = true) ?(alphabet = default_alphabet) ?off ?len input = match encode_sub pad alphabet ?off ?len input with - | Ok (res, off, len) -> Ok (String.sub res off len) + | Ok (res, off, len) -> Ok (String.sub res ~pos:off ~len) | Error _ as err -> err let encode_string ?pad ?alphabet input = @@ -284,7 +286,7 @@ let decode_sub ?(pad = true) {dmap; _} ?(off = 0) ?len input = let decode ?pad ?(alphabet = default_alphabet) ?off ?len input = match decode_sub ?pad alphabet ?off ?len input with - | Ok (res, off, len) -> Ok (String.sub res off len) + | Ok (res, off, len) -> Ok (String.sub res ~pos:off ~len) | Error _ as err -> err let decode_sub ?pad ?(alphabet = default_alphabet) ?off ?len input = diff --git a/compiler/lib/build_path_prefix_map.ml b/compiler/lib/build_path_prefix_map.ml index 0803f56873..7d1c139a25 100644 --- a/compiler/lib/build_path_prefix_map.ml +++ b/compiler/lib/build_path_prefix_map.ml @@ -13,7 +13,7 @@ (* *) (**************************************************************************) -open Stdlib +open! Stdlib type path = string @@ -107,11 +107,7 @@ let decode_map str = let rewrite_opt prefix_map path = let is_prefix = function | None -> false - | Some {target = _; source} -> - String.length source <= String.length path - && Stdlib.String.equal - source - (String.sub path ~pos:0 ~len:(String.length source)) + | Some {target = _; source} -> String.is_prefix path ~prefix:source in match List.find diff --git a/compiler/lib/code.ml b/compiler/lib/code.ml index 259dc5f4f0..8709346429 100644 --- a/compiler/lib/code.ml +++ b/compiler/lib/code.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib module Addr = struct type t = int @@ -57,6 +57,8 @@ module Var : sig val print : Format.formatter -> t -> unit + val equal : t -> t -> bool + val idx : t -> int val of_idx : int -> t @@ -128,7 +130,9 @@ end = struct module T = struct type t = int - let compare v1 v2 = v1 - v2 + let compare : t -> t -> int = compare + + let equal (a : t) (b : t) = a = b end include T @@ -564,9 +568,9 @@ let eq (pc1, blocks1, _) (pc2, blocks2, _) = && try let block2 = Addr.Map.find pc blocks2 in - block1.params = block2.params - && block1.branch = block2.branch - && block1.body = block2.body + Poly.(block1.params = block2.params) + && Poly.(block1.branch = block2.branch) + && Poly.(block1.body = block2.body) with Not_found -> false) blocks1 true @@ -586,7 +590,7 @@ let invariant (_, blocks, _) = let define x = if check_defs then ( - assert (defs.(Var.idx x) = false); + assert (not defs.(Var.idx x)); defs.(Var.idx x) <- true) in let check_expr = function diff --git a/compiler/lib/code.mli b/compiler/lib/code.mli index 8b851c6ee2..c143f1a0c2 100644 --- a/compiler/lib/code.mli +++ b/compiler/lib/code.mli @@ -50,6 +50,8 @@ module Var : sig val print : Format.formatter -> t -> unit + val equal : t -> t -> bool + val idx : t -> int val of_idx : int -> t diff --git a/compiler/lib/config.ml b/compiler/lib/config.ml index 0d80309f90..446e97e991 100644 --- a/compiler/lib/config.ml +++ b/compiler/lib/config.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib module Flag = struct let optims = ref [] @@ -137,7 +137,8 @@ module Param = struct let tc_default = TcTrampoline - let _tc_all = tc_default :: List.filter [TcNone; TcTrampoline] ~f:(( <> ) tc_default) + let _tc_all = + tc_default :: List.filter [TcNone; TcTrampoline] ~f:(Poly.( <> ) tc_default) let tailcall_optim = p diff --git a/compiler/lib/constant.ml b/compiler/lib/constant.ml index c31126a502..0215be841c 100644 --- a/compiler/lib/constant.ml +++ b/compiler/lib/constant.ml @@ -16,6 +16,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib + let global_object = "joo_global_object" let extra_js_files = ["+graphics.js"; "+toplevel.js"; "+nat.js"; "+dynlink.js"] diff --git a/compiler/lib/control.ml b/compiler/lib/control.ml index 5e33690d92..d37bef0d3b 100644 --- a/compiler/lib/control.ml +++ b/compiler/lib/control.ml @@ -21,7 +21,7 @@ (* FIX: is there a way to merge this with dead code elimination? *) - +open! Stdlib open Code (****) diff --git a/compiler/lib/deadcode.ml b/compiler/lib/deadcode.ml index 179ac12e09..6b68125580 100644 --- a/compiler/lib/deadcode.ml +++ b/compiler/lib/deadcode.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let debug = Debug.find "deadcode" diff --git a/compiler/lib/debug.ml b/compiler/lib/debug.ml index 679ea3ced5..52f1bc3996 100644 --- a/compiler/lib/debug.ml +++ b/compiler/lib/debug.ml @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let series = ref None @@ -55,7 +55,7 @@ let find s = state in fun () -> - if s = "times" then take_snapshot (); + if String.equal s "times" then take_snapshot (); (not !quiet) && !state let enable s = diff --git a/compiler/lib/dgraph.ml b/compiler/lib/dgraph.ml index bf6f1c6eb7..77ad673cba 100644 --- a/compiler/lib/dgraph.ml +++ b/compiler/lib/dgraph.ml @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib module Make (N : sig type t @@ -187,7 +188,7 @@ struct let invert size g = let h = NTbl.make size [] in NSet.iter (fun x -> g.iter_children (fun y -> add_edge h y x) x) g.domain; - {domain = g.domain; iter_children = (fun f x -> List.iter f (successors h x))} + {domain = g.domain; iter_children = (fun f x -> List.iter ~f (successors h x))} module type DOMAIN = sig type t diff --git a/compiler/lib/driver.ml b/compiler/lib/driver.ml index a7f8ad0f37..65388241b2 100644 --- a/compiler/lib/driver.ml +++ b/compiler/lib/driver.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let debug = Debug.find "main" diff --git a/compiler/lib/eval.ml b/compiler/lib/eval.ml index d57f40230c..6e1be41357 100644 --- a/compiler/lib/eval.ml +++ b/compiler/lib/eval.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code open Flow @@ -75,12 +75,12 @@ let bool b = Some (Int (if b then 1l else 0l)) let eval_prim x = match x with - | Not, [Int i] -> bool (i = 0l) - | Lt, [Int i; Int j] -> bool (i < j) - | Le, [Int i; Int j] -> bool (i <= j) - | Eq, [Int i; Int j] -> bool (i = j) - | Neq, [Int i; Int j] -> bool (i <> j) - | Ult, [Int i; Int j] -> bool (j < 0l || i < j) + | Not, [Int i] -> bool Int32.(i = 0l) + | Lt, [Int i; Int j] -> bool Int32.(i < j) + | Le, [Int i; Int j] -> bool Int32.(i <= j) + | Eq, [Int i; Int j] -> bool Int32.(i = j) + | Neq, [Int i; Int j] -> bool Int32.(i <> j) + | Ult, [Int i; Int j] -> bool (Int32.(j < 0l) || Int32.(i < j)) | Extern name, l -> ( let name = Primitive.resolve name in match name, l with @@ -99,12 +99,12 @@ let eval_prim x = | "%int_asr", _ -> shift l Int.shift_right | "%int_neg", [Int i] -> Some (Int (Int.neg i)) (* float *) - | "caml_eq_float", _ -> float_binop_bool l ( = ) - | "caml_neq_float", _ -> float_binop_bool l ( <> ) - | "caml_ge_float", _ -> float_binop_bool l ( >= ) - | "caml_le_float", _ -> float_binop_bool l ( <= ) - | "caml_gt_float", _ -> float_binop_bool l ( > ) - | "caml_lt_float", _ -> float_binop_bool l ( < ) + | "caml_eq_float", _ -> float_binop_bool l Float.( = ) + | "caml_neq_float", _ -> float_binop_bool l Float.( <> ) + | "caml_ge_float", _ -> float_binop_bool l Float.( >= ) + | "caml_le_float", _ -> float_binop_bool l Float.( <= ) + | "caml_gt_float", _ -> float_binop_bool l Float.( > ) + | "caml_lt_float", _ -> float_binop_bool l Float.( < ) | "caml_add_float", _ -> float_binop l ( +. ) | "caml_sub_float", _ -> float_binop l ( -. ) | "caml_mul_float", _ -> float_binop l ( *. ) @@ -135,8 +135,8 @@ let eval_prim x = if Config.Flag.safe_string () && pos >= 0 && pos < String.length s then Some (Int (Int.of_int (Char.code s.[pos]))) else None - | "caml_string_equal", [String s1; String s2] -> bool (s1 = s2) - | "caml_string_notequal", [String s1; String s2] -> bool (s1 <> s2) + | "caml_string_equal", [String s1; String s2] -> bool (String.equal s1 s2) + | "caml_string_notequal", [String s1; String s2] -> bool (not (String.equal s1 s2)) | "caml_sys_getenv", [String s] -> ( match get_static_env s with | Some env -> Some (String env) @@ -161,7 +161,7 @@ let the_length_of info x = None (fun u v -> match u, v with - | Some l, Some l' when l = l' -> Some l + | Some l, Some l' when Int32.(l = l') -> Some l | _ -> None) x @@ -195,7 +195,7 @@ let eval_instr info i = | Let (x, Prim (Extern ("caml_js_equals" | "caml_equal"), [y; z])) -> ( match the_const_of info y, the_const_of info z with | Some e1, Some e2 -> - let c = if e1 = e2 then 1l else 0l in + let c = if Poly.(e1 = e2) then 1l else 0l in let c = Constant (Int c) in Flow.update_def info x c; Let (x, c) @@ -222,7 +222,7 @@ let eval_instr info i = match is_int info y with | Unknown -> i | (Y | N) as b -> - let b = if b = N then 0l else 1l in + let b = if Poly.(b = N) then 0l else 1l in let c = Constant (Int b) in Flow.update_def info x c; Let (x, c)) @@ -299,10 +299,10 @@ let eval_branch info = function | 0l -> false (* https://github.com/ocaml/ocaml/blob/trunk/byterun/interp.c#L798 *) | _ -> true) - | CEq i -> i = j - | CLt i -> i < j - | CLe i -> i <= j - | CUlt i -> j < 0l || i < j + | CEq i -> Int32.(i = j) + | CLt i -> Int32.(i < j) + | CLe i -> Int32.(i <= j) + | CUlt i -> Int32.(j < 0l) || Int32.(i < j) in match res with | true -> Branch ftrue @@ -371,7 +371,7 @@ let drop_exception_handler blocks = Addr.Set.fold (fun pc2 blocks -> let b = Addr.Map.find pc2 blocks in - assert (b.handler <> parent_hander); + assert (Poly.(b.handler <> parent_hander)); let branch = match b.branch with | Poptrap (cont, pushtrap) -> diff --git a/compiler/lib/findlib.ml b/compiler/lib/findlib.ml index c8d4c82b6f..20777f1dbd 100644 --- a/compiler/lib/findlib.ml +++ b/compiler/lib/findlib.ml @@ -15,6 +15,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib let find_pkg_dir_ref = ref (fun _ -> raise Not_found) @@ -22,10 +23,7 @@ let set_find_pkg_dir f = find_pkg_dir_ref := f let find_pkg_dir pkg = !find_pkg_dir_ref pkg -let path_require_findlib path = - if path <> "" && path.[0] = '+' - then Some (String.sub path 1 (String.length path - 1)) - else None +let path_require_findlib path = String.drop_prefix path ~prefix:"+" let rec find_in_findlib_paths ?(pkg = "stdlib") paths name = match paths with diff --git a/compiler/lib/flow.ml b/compiler/lib/flow.ml index 1903bfc9e2..da2325206e 100644 --- a/compiler/lib/flow.ml +++ b/compiler/lib/flow.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let debug = Debug.find "flow" @@ -66,7 +66,7 @@ let add_assign_def vars defs x y = let add_param_def vars defs x = add_var vars x; let idx = Var.idx x in - assert (is_undefined defs.(idx) || defs.(idx) = Param); + assert (is_undefined defs.(idx) || Poly.(defs.(idx) = Param)); defs.(idx) <- Param (* x depends on y *) @@ -275,7 +275,7 @@ let propagate2 ?(skip_param = false) defs known_origins possibly_mutable st x = module Domain2 = struct type t = bool - let equal (u : bool) v = u = v + let equal = Bool.equal let bot = false end @@ -331,7 +331,7 @@ let the_const_of info x = None (fun u v -> match u, v with - | Some i, Some j when i = j -> u + | Some i, Some j when Poly.(i = j) -> u | _ -> None) x | Pc c -> Some c @@ -377,7 +377,7 @@ let build_subst info vars = then let s = Var.Tbl.get info.info_known_origins x in if Var.Set.cardinal s = 1 then subst.(Var.idx x) <- Some (Var.Set.choose s)); - if subst.(Var.idx x) = None then subst.(Var.idx x) <- direct_approx info x; + if Option.is_none subst.(Var.idx x) then subst.(Var.idx x) <- direct_approx info x; match subst.(Var.idx x) with | None -> () | Some y -> Var.propagate_name x y) diff --git a/compiler/lib/freevars.ml b/compiler/lib/freevars.ml index 5eb1dc6109..5ea521d1ee 100644 --- a/compiler/lib/freevars.ml +++ b/compiler/lib/freevars.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let times = Debug.find "times" diff --git a/compiler/lib/fs.ml b/compiler/lib/fs.ml index a1865822e8..a7876fc403 100644 --- a/compiler/lib/fs.ml +++ b/compiler/lib/fs.ml @@ -15,6 +15,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib let rec find_in_path paths name = match paths with @@ -24,7 +25,7 @@ let rec find_in_path paths name = if Sys.file_exists file then file else find_in_path rem name let find_in_path paths name = - if name = "" || name = "." + if String.is_empty name || String.equal name "." then raise Not_found else if Filename.is_relative name then find_in_path paths name diff --git a/compiler/lib/generate.ml b/compiler/lib/generate.ml index 1c7d2cb1b0..b6857d94fb 100644 --- a/compiler/lib/generate.ml +++ b/compiler/lib/generate.ml @@ -33,7 +33,7 @@ - CLEAN UP!!! *) -open Stdlib +open! Stdlib let debug = Debug.find "gen" @@ -52,7 +52,7 @@ let rec list_group_rec f g l b m n = | [] -> List.rev ((b, List.rev m) :: n) | a :: r -> let fa = f a in - if fa = b + if Poly.(fa = b) then list_group_rec f g r b (g a :: m) n else list_group_rec f g r fa [g a] ((b, List.rev m) :: n) @@ -383,7 +383,7 @@ let access_queue queue x = else ( (elt.prop, elt.ce) , List.map queue ~f:(function - | x', elt when x = x' -> x', {elt with cardinal = pred elt.cardinal} + | x', elt when Var.equal x x' -> x', {elt with cardinal = pred elt.cardinal} | x -> x) ) with Not_found -> (const_p, var x), queue @@ -391,7 +391,7 @@ let access_queue' ~ctx queue x = match x with | Pc c -> let js, instrs = constant ~ctx c (Config.Param.constant_max_depth ()) in - assert (instrs = []); + assert (List.is_empty instrs); (* We only have simple constants here *) (const_p, js), queue | Pv x -> access_queue queue x @@ -482,7 +482,7 @@ module DTree = struct let normalize a = a >> Array.to_list - >> List.stable_sort ~cmp:(fun (cont1, _) (cont2, _) -> compare cont1 cont2) + >> List.stable_sort ~cmp:(fun (cont1, _) (cont2, _) -> Poly.compare cont1 cont2) >> list_group fst snd >> List.map ~f:(fun (cont1, l1) -> cont1, List.flatten l1) >> List.stable_sort ~cmp:(fun (_, l1) (_, l2) -> @@ -1088,7 +1088,9 @@ let rec translate_expr ctx queue loc _x e level : _ * J.statement_list = l ~init:([], mutator_p, queue) in - J.ENew (cc, if args = [] then None else Some args), or_p pc prop, queue + ( J.ENew (cc, if List.is_empty args then None else Some args) + , or_p pc prop + , queue ) | Extern "caml_js_get", [Pv o; Pc (String f | IString f)] when J.is_ident f -> let (po, co), queue = access_queue queue o in J.EDot (co, f), or_p po mutable_p, queue @@ -1139,7 +1141,7 @@ let rec translate_expr ctx queue loc _x e level : _ * J.statement_list = match internal_prim name with | Some f -> f l queue ctx loc | None -> - if name.[0] = '%' + if String.is_prefix name ~prefix:"%" then failwith (Printf.sprintf "Unresolved internal primitive: %s" name); let prim = Share.get_prim (runtime_fun ctx) name ctx.Ctx.share in let prim_kind = kind (Primitive.kind name) in @@ -1190,14 +1192,7 @@ and translate_instr ctx expr_queue loc instr = let keep_name x = match Code.Var.get_name x with | None -> false - | Some s -> - not - (String.length s >= 5 - && s.[0] = 'j' - && s.[1] = 's' - && s.[2] = 'o' - && s.[3] = 'o' - && s.[4] = '_') + | Some s -> not (String.is_prefix s ~prefix:"jsoo_") in match ctx.Ctx.live.(Var.idx x), e with | 0, _ -> @@ -1259,7 +1254,8 @@ and translate_instrs ctx expr_queue loc instr = st @ instrs, expr_queue and compile_block st queue (pc : Addr.t) frontier interm = - if queue <> [] && (Addr.Set.mem pc st.loops || not (Config.Flag.inline ())) + if (not (List.is_empty queue)) + && (Addr.Set.mem pc st.loops || not (Config.Flag.inline ())) then flush_all queue (compile_block st [] pc frontier interm) else ( if pc >= 0 @@ -1318,7 +1314,7 @@ and compile_block st queue (pc : Addr.t) frontier interm = then false else let block = Addr.Map.find pc st.blocks in - block.body <> [] + (not (List.is_empty block.body)) || match block.branch with | Return _ -> false @@ -1509,7 +1505,7 @@ and compile_decision_tree st _queue handler backs frontier interm succs loc cx d | CEq n -> J.EBin (J.EqEqEq, int32 n, cx) | CLt n -> J.EBin (J.Lt, int32 n, cx) | CUlt n -> - let n' = if n < 0l then unsigned (int32 n) else int32 n in + let n' = if Int32.(n < 0l) then unsigned (int32 n) else int32 n in J.EBin (J.Lt, n', unsigned cx) | CLe n -> J.EBin (J.Le, int32 n, cx) in @@ -1677,7 +1673,7 @@ and compile_conditional st queue pc last handler backs frontier interm succs = res and compile_argument_passing ctx queue (pc, args) _backs continuation = - if args = [] + if List.is_empty args then continuation queue else let block = Addr.Map.find pc ctx.Ctx.blocks in @@ -1703,7 +1699,9 @@ and compile_exn_handling ctx queue (pc, args) handler continuation = in (* When an extra block is inserted during code generation, args is [] *) - let m = Subst.build_mapping (if args = [] then [] else block.params) args in + let m = + Subst.build_mapping (if List.is_empty args then [] else block.params) args + in let h_block = Addr.Map.find h_pc ctx.Ctx.blocks in let rec loop continuation old args params queue = match args, params with @@ -1715,7 +1713,7 @@ and compile_exn_handling ctx queue (pc, args) handler continuation = | z :: old -> Some z, old in let x' = try Some (Var.Map.find x m) with Not_found -> Some x in - if Var.compare x x0 = 0 || x' = z + if Var.compare x x0 = 0 || Option.equal Var.equal x' z then loop continuation old args params queue else let (px, cx), queue = access_queue queue x in @@ -1755,7 +1753,7 @@ and compile_branch st queue ((pc, _) as cont) handler backs frontier interm = in if debug () then - if label = None + if Option.is_none label then Format.eprintf "continue;@ " else Format.eprintf "continue (%d);@ " pc; flush_all queue [J.Continue_statement label, J.N]) diff --git a/compiler/lib/generate_closure.ml b/compiler/lib/generate_closure.ml index e1b121d491..5e48ea03e4 100644 --- a/compiler/lib/generate_closure.ml +++ b/compiler/lib/generate_closure.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code let debug_tc = Debug.find "gen_tc" @@ -182,7 +182,7 @@ module Trampoline = struct let free_pc = free_pc + 2 in match List.rev block.body with | Let (x, Apply (f, args, true)) :: rem_rev -> - assert (f = ci.f_name); + assert (Var.equal f ci.f_name); let blocks = Addr.Map.add direct_call_pc @@ -253,7 +253,7 @@ let rewrite_mutable rewrite_list {int = closures_intern; ext = closures_extern} = let internal_and_external = closures_intern @ closures_extern in - assert (closures_extern <> []); + assert (not (List.is_empty closures_extern)); let all_mut, names = List.fold_left internal_and_external @@ -270,7 +270,7 @@ let rewrite_mutable | _ -> assert false) in let vars = Var.Set.elements (Var.Set.diff all_mut names) in - if vars = [] + if List.is_empty vars then free_pc, blocks, internal_and_external else match internal_and_external with diff --git a/compiler/lib/inline.ml b/compiler/lib/inline.ml index 28b9aa50d5..5f1fa7c100 100644 --- a/compiler/lib/inline.ml +++ b/compiler/lib/inline.ml @@ -18,7 +18,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code let optimizable blocks pc _ = @@ -81,7 +81,7 @@ let get_closures (_, blocks, _) = let rewrite_block (pc', handler) pc blocks = let block = Addr.Map.find pc blocks in - assert (block.handler = None); + assert (Option.is_none block.handler); let block = {block with handler} in let block = match block.branch, pc' with @@ -128,7 +128,7 @@ let rec find_mapping mapping x = let simple blocks cont mapping = let map_var mapping x = let x' = find_mapping mapping x in - if x = x' then raise Not_found else x' + if Var.equal x x' then raise Not_found else x' in let map_prim_arg mapping = function | Pc c -> Pc c @@ -196,7 +196,8 @@ let inline closures live_vars outer_optimizable pc (blocks, free_pc) = [], (Branch (free_pc, [arg]), blocks, free_pc + 1)) | `Exp exp -> Let (x, exp) :: rem, state | `Fail -> - if live_vars.(Var.idx f) = 1 && outer_optimizable = f_optimizable + if live_vars.(Var.idx f) = 1 + && Bool.equal outer_optimizable f_optimizable (* inlining the code of an optimizable function could make this code unoptimized. (wrt to Jit compilers) At the moment, V8 doesn't optimize function containing try..catch. diff --git a/compiler/lib/instr.ml b/compiler/lib/instr.ml index fcd5268e98..4ec59917e7 100644 --- a/compiler/lib/instr.ml +++ b/compiler/lib/instr.ml @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib type t = | ACC0 @@ -351,7 +352,7 @@ let ops = ; FIRST_UNIMPLEMENTED_OP, K_will_not_happen, "FIRST_UNIMPLEMENTED_OP" |] in let ops = - Array.mapi (fun i (c, k, n) -> {code = c; kind = k; name = n; opcode = i}) instrs + Array.mapi ~f:(fun i (c, k, n) -> {code = c; kind = k; name = n; opcode = i}) instrs in ops @@ -384,5 +385,5 @@ let get_instr_exn code pc = let i = getu code pc in if i < 0 || i >= Array.length ops then raise (Bad_instruction i); let ins = ops.(i) in - if ins.kind = K_will_not_happen then raise (Bad_instruction i); + if Poly.(ins.kind = K_will_not_happen) then raise (Bad_instruction i); ins diff --git a/compiler/lib/javascript.ml b/compiler/lib/javascript.ml index 25a6c40a64..deb7482495 100644 --- a/compiler/lib/javascript.ml +++ b/compiler/lib/javascript.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib module Num : sig type t @@ -67,35 +67,36 @@ end = struct let of_int32 = Int32.to_string let of_float v = - if v = infinity + if Float.equal v infinity then "Infinity" - else if v = neg_infinity + else if Float.equal v neg_infinity then "-Infinity" - else if v <> v + else if not (Float.equal v v) then "NaN" (* [1/-0] = -inf seems to be the only way to detect -0 in JavaScript *) - else if v = 0. && 1. /. v = neg_infinity + else if Float.equal v 0. && Float.equal (1. /. v) neg_infinity then "-0." else let vint = int_of_float v in - if float_of_int vint = v + if Float.equal (float_of_int vint) v then Printf.sprintf "%d." vint else let s1 = Printf.sprintf "%.12g" v in - if v = float_of_string s1 + if Float.equal v (float_of_string s1) then s1 else let s2 = Printf.sprintf "%.15g" v in - if v = float_of_string s2 then s2 else Printf.sprintf "%.18g" v + if Float.equal v (float_of_string s2) then s2 else Printf.sprintf "%.18g" v let is_zero s = String.equal s "0" let is_one s = String.equal s "1" - let is_neg s = s.[0] = '-' + let is_neg s = Char.equal s.[0] '-' - let drop1 s = String.sub s ~pos:1 ~len:(String.length s - 1) - - let neg s = if is_neg s then drop1 s else "-" ^ s + let neg s = + match String.drop_prefix s ~prefix:"-" with + | None -> "-" ^ s + | Some s -> s let add a b = of_int32 (Int32.add (to_int32 a) (to_int32 b)) end @@ -300,11 +301,10 @@ let is_ident = let l = Array.init 256 ~f:(fun i -> let c = Char.chr i in - if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' - then 1 - else if c >= '0' && c <= '9' - then 2 - else 0) + match c with + | 'a' .. 'z' | 'A' .. 'Z' | '_' | '$' -> 1 + | '0' .. '9' -> 2 + | _ -> 0) in fun s -> (not (StringSet.mem s Reserved.keyword)) diff --git a/compiler/lib/js_assign.ml b/compiler/lib/js_assign.ml index 28c6e8563d..dc1b10051e 100644 --- a/compiler/lib/js_assign.ml +++ b/compiler/lib/js_assign.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Javascript let debug = Debug.find "shortvar" @@ -224,14 +224,14 @@ while compiling the OCaml toplevel: !total; for i = 0 to len - 1 do let l = constr.(idx.(i)) in - if l <> [] && String.length name.(idx.(i)) = 0 + if (not (List.is_empty l)) && String.length name.(idx.(i)) = 0 then ( let n = first_available l in let idx = idx.(i) in nm ~origin:idx n; mark_allocated l n; stats idx n); - if l = [] then assert (weight idx.(i) = 0) + if List.is_empty l then assert (weight idx.(i) = 0) done; if debug () then ( @@ -311,18 +311,18 @@ module Preserve : Strategy = struct S.fold (fun var acc -> let name = names.(Var.idx var) in - if name <> "" then StringSet.add name acc else acc) + if not (String.is_empty name) then StringSet.add name acc else acc) (S.union state.Js_traverse.use state.Js_traverse.def) assigned in let _assigned = S.fold (fun var assigned -> - assert (names.(Var.idx var) = ""); + assert (String.is_empty names.(Var.idx var)); let name = match Var.get_name var with | Some expected_name -> - assert (expected_name <> ""); + assert (not (String.is_empty expected_name)); if not (StringSet.mem expected_name assigned) then expected_name else @@ -368,7 +368,7 @@ let program' (module Strategy : Strategy) p = let color = function | V v -> let name = names.(Var.idx v) in - assert (name <> ""); + assert (not (String.is_empty name)); ident ~var:v name | x -> x in diff --git a/compiler/lib/js_output.ml b/compiler/lib/js_output.ml index 20262f920c..f3115fcb19 100644 --- a/compiler/lib/js_output.ml +++ b/compiler/lib/js_output.ml @@ -28,7 +28,7 @@ XXX Beware automatic semi-colon insertion... the space cannot be replaced by a newline in the following expressions: e ++, e --, continue e, break e, return e, throw e *) -open Stdlib +open! Stdlib let stats = Debug.find "output" @@ -290,7 +290,7 @@ struct for i = 0 to l - 1 do let c = s.[i] in match c with - | '\000' when i = l - 1 || s.[i + 1] < '0' || s.[i + 1] > '9' -> PP.string f "\\0" + | '\000' when i = l - 1 || not (Char.is_num s.[i + 1]) -> PP.string f "\\0" | '\b' -> PP.string f "\\b" | '\t' -> PP.string f "\\t" | '\n' -> PP.string f "\\n" @@ -311,7 +311,7 @@ struct PP.string f (Array.unsafe_get array_conv (c lsr 4)); PP.string f (Array.unsafe_get array_conv (c land 0xf)) | _ -> - if c = quote + if Char.equal c quote then ( PP.string f "\\"; PP.string f (Array.unsafe_get array_str1 (Char.code c))) @@ -378,19 +378,19 @@ struct PP.end_group f) | EStr (s, kind) -> let quote = best_string_quote s in - pp_string f ~utf:(kind = `Utf8) ~quote s + pp_string f ~utf:Poly.(kind = `Utf8) ~quote s | EBool b -> PP.string f (if b then "true" else "false") - | ENum s -> - let s = Num.to_string s in + | ENum num -> + let s = Num.to_string num in let need_parent = - if s.[0] = '-' + if Num.is_neg num then l > 13 (* Negative numbers may need to be parenthesized. *) else l = 15 (* Parenthesize as well when followed by a dot. *) - && s.[0] <> 'I' + && (not (Char.equal s.[0] 'I')) (* Infinity *) - && s.[0] <> 'N' + && not (Char.equal s.[0] 'N') (* NaN *) in if need_parent then PP.string f "("; @@ -443,9 +443,11 @@ struct then ( PP.start_group f 1; PP.string f "("); - if op = IncrA || op = DecrA then expression 13 f e; - if op = IncrA || op = IncrB then PP.string f "++" else PP.string f "--"; - if op = IncrB || op = DecrB then expression 13 f e; + if Poly.(op = IncrA) || Poly.(op = DecrA) then expression 13 f e; + if Poly.(op = IncrA) || Poly.(op = IncrB) + then PP.string f "++" + else PP.string f "--"; + if Poly.(op = IncrB) || Poly.(op = DecrB) then expression 13 f e; if l > 13 then ( PP.string f ")"; @@ -1032,7 +1034,7 @@ struct output_one false x; loop last xs in - loop (def = None && cc' = []) cc; + loop (Option.is_none def && List.is_empty cc') cc; (match def with | None -> () | Some def -> @@ -1040,7 +1042,7 @@ struct PP.string f "default:"; PP.break f; PP.start_group f 0; - statement_list ~skip_last_semi:(cc' = []) f def; + statement_list ~skip_last_semi:(List.is_empty cc') f def; PP.end_group f; PP.end_group f); loop true cc'; @@ -1144,12 +1146,9 @@ end let part_of_ident = let a = Array.init 256 ~f:(fun i -> - let c = Char.chr i in - (c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9') - || c = '_' - || c = '$') + match Char.chr i with + | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '_' | '$' -> true + | _ -> false) in fun c -> Array.unsafe_get a (Char.code c) @@ -1158,10 +1157,14 @@ let need_space a b = (part_of_ident a && part_of_ident b) (* do not generate end_of_line_comment. handle the case of "num / /* comment */ b " *) - || (a = '/' && b = '/') + || + match a, b with + | '/', '/' (* https://github.com/ocsigen/js_of_ocaml/issues/507 *) - || (a = '-' && b = '-') - || (a = '+' && b = '+') + |'-', '-' + |'+', '+' -> + true + | _, _ -> false let program f ?source_map p = let smo = diff --git a/compiler/lib/js_simpl.ml b/compiler/lib/js_simpl.ml index a175d5330b..16e2a82ba7 100644 --- a/compiler/lib/js_simpl.ml +++ b/compiler/lib/js_simpl.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - +open! Stdlib module J = Javascript let rec enot_rec e = @@ -128,8 +128,8 @@ let rec if_statement_2 e loc iftrue truestop iffalse falsestop = (* Generates conditional *) let x1, (e1, _) = assignment_of_statement iftrue in let x2, (e2, _) = assignment_of_statement iffalse in - if x1 <> x2 then raise Not_assignment; - let exp = if e1 = e then J.EBin (J.Or, e, e2) else J.ECond (e, e1, e2) in + if Poly.(x1 <> x2) then raise Not_assignment; + let exp = if Poly.(e1 = e) then J.EBin (J.Or, e, e2) else J.ECond (e, e1, e2) in [J.Variable_statement [x1, Some (exp, loc)], loc] with Not_assignment -> ( try @@ -153,9 +153,10 @@ let if_statement e loc iftrue truestop iffalse falsestop = let e = simplify_condition e in match iftrue, iffalse with (* Shared statements *) - | (J.If_statement (e', iftrue', iffalse'), loc), _ when iffalse = unopt iffalse' -> + | (J.If_statement (e', iftrue', iffalse'), loc), _ when Poly.(iffalse = unopt iffalse') + -> if_statement_2 (J.EBin (J.And, e, e')) loc iftrue' truestop iffalse falsestop - | (J.If_statement (e', iftrue', iffalse'), loc), _ when iffalse = iftrue' -> + | (J.If_statement (e', iftrue', iffalse'), loc), _ when Poly.(iffalse = iftrue') -> if_statement_2 (J.EBin (J.And, e, J.EUn (J.Not, e'))) loc @@ -163,7 +164,7 @@ let if_statement e loc iftrue truestop iffalse falsestop = truestop iffalse falsestop - | _, (J.If_statement (e', iftrue', iffalse'), loc) when iftrue = iftrue' -> + | _, (J.If_statement (e', iftrue', iffalse'), loc) when Poly.(iftrue = iftrue') -> if_statement_2 (J.EBin (J.Or, e, e')) loc @@ -171,7 +172,8 @@ let if_statement e loc iftrue truestop iffalse falsestop = truestop (unopt iffalse') falsestop - | _, (J.If_statement (e', iftrue', iffalse'), loc) when iftrue = unopt iffalse' -> + | _, (J.If_statement (e', iftrue', iffalse'), loc) when Poly.(iftrue = unopt iffalse') + -> if_statement_2 (J.EBin (J.Or, e, J.EUn (J.Not, e'))) loc @@ -187,16 +189,16 @@ let rec get_variable acc = function | J.ECond (e1, e2, e3) -> get_variable (get_variable (get_variable acc e1) e2) e3 | J.EUn (_, e1) | J.EDot (e1, _) | J.ENew (e1, None) -> get_variable acc e1 | J.ECall (e1, el, _) | J.ENew (e1, Some el) -> - List.fold_left get_variable acc (e1 :: el) + List.fold_left ~f:get_variable ~init:acc (e1 :: el) | J.EVar (J.V v) -> Code.Var.Set.add v acc | J.EVar (J.S _) -> acc | J.EFun _ | J.EStr _ | J.EBool _ | J.ENum _ | J.EQuote _ | J.ERegexp _ -> acc | J.EArr a -> List.fold_left - (fun acc i -> + ~f:(fun acc i -> match i with | None -> acc | Some e1 -> get_variable acc e1) - acc + ~init:acc a - | J.EObj l -> List.fold_left (fun acc (_, e1) -> get_variable acc e1) acc l + | J.EObj l -> List.fold_left ~f:(fun acc (_, e1) -> get_variable acc e1) ~init:acc l diff --git a/compiler/lib/js_token.ml b/compiler/lib/js_token.ml index ba60b07a55..864ca1a4ee 100644 --- a/compiler/lib/js_token.ml +++ b/compiler/lib/js_token.ml @@ -17,6 +17,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib + type token = | T_WITH of Parse_info.t | T_WHILE of Parse_info.t diff --git a/compiler/lib/js_traverse.ml b/compiler/lib/js_traverse.ml index 92b2881043..084459d812 100644 --- a/compiler/lib/js_traverse.ml +++ b/compiler/lib/js_traverse.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Javascript class type mapper = @@ -538,7 +538,7 @@ class rename_variable keeps = | Some (S {name; _}, block) -> let v = Code.Var.fresh_n name in let sub = function - | S {name = name'; _} when name' = name -> V v + | S {name = name'; _} when String.equal name' name -> V v | x -> x in let s = new subst sub in @@ -629,7 +629,7 @@ class compact_vardecl = method private pack all sources = let may_flush rem vars s instr = - if vars = [] + if List.is_empty vars then rem, [], s :: instr else rem, [], s :: (Statement (Variable_statement (List.rev vars)), N) :: instr in @@ -727,7 +727,8 @@ class clean = List.rev_append l vars_rev, vars_loc, instr_rev | Empty_statement | Expression_statement (EVar _) -> vars_rev, vars_loc, instr_rev - | _ when vars_rev = [] -> [], vars_loc, rev_append_st (x, loc) instr_rev + | _ when List.is_empty vars_rev -> + [], vars_loc, rev_append_st (x, loc) instr_rev | _ -> ( [] , vars_loc @@ -774,7 +775,7 @@ class clean = List.fold_left l ~init:([], []) ~f:(fun (st_rev, sources_rev) (x, loc) -> match x with | Statement s -> (s, loc) :: st_rev, sources_rev - | Function_declaration _ as x when st_rev = [] -> + | Function_declaration _ as x when List.is_empty st_rev -> [], (m#source x, loc) :: sources_rev | Function_declaration _ as x -> [], (m#source x, loc) :: append_st st_rev sources_rev) @@ -807,7 +808,7 @@ let is_one = function let assign_op = function | exp, EBin (Plus, exp', exp'') -> ( - match exp = exp', exp = exp'' with + match Poly.(exp = exp'), Poly.(exp = exp'') with | false, false -> None | true, false -> if is_one exp'' @@ -816,15 +817,15 @@ let assign_op = function | false, true -> if is_one exp' then Some (EUn (IncrB, exp)) else Some (EBin (PlusEq, exp, exp')) | true, true -> Some (EBin (StarEq, exp, ENum (Num.of_int32 2l)))) - | exp, EBin (Minus, exp', y) when exp = exp' -> + | exp, EBin (Minus, exp', y) when Poly.(exp = exp') -> if is_one y then Some (EUn (DecrB, exp)) else Some (EBin (MinusEq, exp, y)) | exp, EBin (Mul, exp', exp'') -> ( - match exp = exp', exp = exp'' with + match Poly.(exp = exp'), Poly.(exp = exp'') with | false, false -> None | true, _ -> Some (EBin (StarEq, exp, exp'')) | _, true -> Some (EBin (StarEq, exp, exp'))) | exp, EBin (((Div | Mod | Lsl | Asr | Lsr | Band | Bxor | Bor) as unop), exp', y) - when exp = exp' -> + when Poly.(exp = exp') -> Some (EBin (translate_assign_op unop, exp, y)) | _ -> None @@ -873,7 +874,7 @@ class simpl = ( cond , (Expression_statement (EBin (Eq, v1, e1)), _) , Some (Expression_statement (EBin (Eq, v2, e2)), _) ) - when v1 = v2 -> + when Poly.(v1 = v2) -> (Expression_statement (EBin (Eq, v1, ECond (cond, e1, e2))), loc) :: rem | Variable_statement l1 -> let x = @@ -903,7 +904,7 @@ class simpl = List.fold_left l ~init:([], []) ~f:(fun (st_rev, sources_rev) x -> match x with | Statement s, loc -> (s, loc) :: st_rev, sources_rev - | (Function_declaration _ as x), loc when st_rev = [] -> + | (Function_declaration _ as x), loc when List.is_empty st_rev -> [], (m#source x, loc) :: sources_rev | (Function_declaration _ as x), loc -> [], (m#source x, loc) :: append_st st_rev sources_rev) diff --git a/compiler/lib/link_js.ml b/compiler/lib/link_js.ml index a8a234fee3..4f9298c913 100644 --- a/compiler/lib/link_js.ml +++ b/compiler/lib/link_js.ml @@ -16,31 +16,18 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib let sourceMappingURL = "//# sourceMappingURL=" let sourceMappingURL_base64 = "//# sourceMappingURL=data:application/json;base64," -let drop_prefix ~prefix s = - let plen = String.length prefix in - if plen > String.length s - then None - else - try - for i = 0 to String.length prefix - 1 do - if s.[i] <> prefix.[i] then raise Exit - done; - Some (String.sub s plen (String.length s - plen)) - with Exit -> None - -let _ = drop_prefix ~prefix:"qwe:" "qwe" - let kind ~resolve_sourcemap_url file line = let s = - match drop_prefix ~prefix:sourceMappingURL_base64 line with + match String.drop_prefix ~prefix:sourceMappingURL_base64 line with | Some base64 -> `Json_base64 base64 | None -> ( - match drop_prefix ~prefix:sourceMappingURL line with + match String.drop_prefix ~prefix:sourceMappingURL line with | Some url -> `Url url | None -> `Other) in @@ -66,7 +53,7 @@ let link ~output ~files ~resolve_sourcemap_url ~source_map = in let source_offset = ref 0 in List.iter - (fun file -> + ~f:(fun file -> let ic = open_in file in (try output_string output (Printf.sprintf "//# 1 %S" file); diff --git a/compiler/lib/linker.ml b/compiler/lib/linker.ml index 850c7c289c..64b63acaeb 100644 --- a/compiler/lib/linker.ml +++ b/compiler/lib/linker.ml @@ -18,7 +18,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib type fragment = { provides : @@ -156,7 +156,7 @@ class check_and_warn name pi = let use = from#get_use_name in let diff = StringSet.diff def use in let diff = StringSet.remove name diff in - let diff = StringSet.filter (fun s -> String.length s <> 0 && s.[0] <> '_') diff in + let diff = StringSet.filter (fun s -> not (String.is_prefix s ~prefix:"_")) diff in if not (StringSet.is_empty diff) then warn @@ -302,7 +302,7 @@ let add_file f = let rec find = function | [] -> None | (J.Function_declaration (J.S {J.name = n; _}, l, _, _), _) :: _ - when name = n -> + when String.equal name n -> Some (List.length l) | _ :: rem -> find rem in diff --git a/compiler/lib/macro.ml b/compiler/lib/macro.ml index 29de3d3c16..ade9f842eb 100644 --- a/compiler/lib/macro.ml +++ b/compiler/lib/macro.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib class macro_mapper = object (m) diff --git a/compiler/lib/magic_number.ml b/compiler/lib/magic_number.ml index eda92a2462..efa2413f6a 100644 --- a/compiler/lib/magic_number.ml +++ b/compiler/lib/magic_number.ml @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib type t = string * int @@ -58,9 +58,11 @@ let kind (s, _) = let to_string (k, v) = Printf.sprintf "%s%03d" k v let compare (p1, n1) (p2, n2) = - if p1 <> p2 then raise Not_found; + if not (String.equal p1 p2) then raise Not_found; compare n1 n2 +let equal a b = compare a b = 0 + let current_exe = let v = match Ocaml_version.v with diff --git a/compiler/lib/magic_number.mli b/compiler/lib/magic_number.mli index 5bb40f8bfa..93d223c205 100644 --- a/compiler/lib/magic_number.mli +++ b/compiler/lib/magic_number.mli @@ -26,6 +26,8 @@ val size : int val compare : t -> t -> int +val equal : t -> t -> bool + val of_string : string -> t val to_string : t -> string diff --git a/compiler/lib/mlvalue.ml b/compiler/lib/mlvalue.ml index 8d46d0f847..2204dd7284 100644 --- a/compiler/lib/mlvalue.ml +++ b/compiler/lib/mlvalue.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib module J = Javascript let zero = J.ENum (J.Num.of_int32 0l) diff --git a/compiler/lib/ocaml_compiler.cppo.ml b/compiler/lib/ocaml_compiler.cppo.ml index ea36713ec6..509f235c9e 100644 --- a/compiler/lib/ocaml_compiler.cppo.ml +++ b/compiler/lib/ocaml_compiler.cppo.ml @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let rec constant_of_const : _ -> Code.constant = let open Lambda in @@ -50,7 +50,7 @@ let rec constant_of_const : _ -> Code.constant = let rec find_loc_in_summary ident' = function | Env.Env_empty -> None | Env.Env_value (_summary, ident, description) - when ident = ident' -> + when Poly.(ident = ident') -> Some description.Types.val_loc | Env.Env_value (summary,_,_) | Env.Env_type (summary, _, _) @@ -85,6 +85,7 @@ let rec find_loc_in_summary ident' = function #if OCAML_VERSION < (4,8,0) (* Copied from ocaml/utils/tbl.ml *) module Tbl = struct + open Poly type ('a, 'b) t = | Empty | Node of ('a, 'b) t * 'a * 'b * ('a, 'b) t * int @@ -154,7 +155,7 @@ module Symtable = struct module GlobalMap = struct type t = Ident.t numtable - let filter_global_map p gmap = + let filter_global_map (p : Ident.t -> bool) gmap = let newtbl = ref Tbl.empty in Tbl.iter (fun id num -> if p id then newtbl := Tbl.add id num !newtbl) gmap.num_tbl; {num_cnt = gmap.num_cnt; num_tbl = !newtbl} @@ -208,7 +209,7 @@ module Symtable = struct module GlobalMap = Num_tbl(Ident.Map) include GlobalMap - let filter_global_map p (gmap : t) = + let filter_global_map (p : Ident.t -> bool) (gmap : t) = let newtbl = ref Ident.Map.empty in Ident.Map.iter (fun id num -> if p id then newtbl := Ident.Map.add id num !newtbl) diff --git a/compiler/lib/ocaml_version.ml b/compiler/lib/ocaml_version.ml index 0930bde1d7..e8e423ee44 100644 --- a/compiler/lib/ocaml_version.ml +++ b/compiler/lib/ocaml_version.ml @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib type t = int list diff --git a/compiler/lib/parse_bytecode.ml b/compiler/lib/parse_bytecode.ml index 1e9ba2ab1d..b295f70837 100644 --- a/compiler/lib/parse_bytecode.ml +++ b/compiler/lib/parse_bytecode.ml @@ -18,7 +18,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code open Instr @@ -150,7 +150,7 @@ end = struct let set = Hashtbl.fold (fun (_m, p) unit acc -> - if p = pos_fname + if String.equal p pos_fname then match unit.source with | None -> acc @@ -362,7 +362,7 @@ end = struct let i : nativeint = Obj.magic x in let i32 = Nativeint.to_int32 i in let i' = Nativeint.of_int32 i32 in - if i' <> i then warn_overflow (Printf.sprintf "0x%nx (%nd)" i i) i32; + if Poly.(i' <> i) then warn_overflow (Printf.sprintf "0x%nx (%nd)" i i) i32; Int i32) else if tag = Obj.custom_tag && same_custom x 0L then Int64 (Obj.magic x : int64) @@ -1042,7 +1042,7 @@ and compile infos pc state instrs = State.size_globals state (i + 1); let y = State.accu state in let g = State.globals state in - assert (g.vars.(i) = None); + assert (Option.is_none g.vars.(i)); if debug_parser () then Format.printf "(global %d) = %a@." i Var.print y; let instrs = match g.override.(i) with @@ -1410,7 +1410,7 @@ and compile infos pc state instrs = | CHECK_SIGNALS -> compile infos (pc + 1) state instrs | C_CALL1 -> let prim = primitive_name state (getu code (pc + 1)) in - if Primitive.resolve prim = "%identity" + if String.equal (Primitive.resolve prim) "%identity" then (* This is a no-op *) compile infos (pc + 2) state instrs else @@ -1948,7 +1948,7 @@ let match_exn_traps (blocks : 'a Addr.Map.t) = (fun pc conts' blocks -> match Addr.Map.find pc blocks with | {branch = Pushtrap (cont1, x, cont2, conts); _} as block -> - assert (conts = Addr.Set.empty); + assert (Addr.Set.is_empty conts); let branch = Pushtrap (cont1, x, cont2, conts') in Addr.Map.add pc {block with branch} blocks | _ -> assert false) @@ -1966,11 +1966,11 @@ let parse_bytecode ~debug code globals debug_data = let state = State.initial globals in Code.Var.reset (); let blocks = - Blocks.analyse (if debug = `Full then debug_data else Debug.create ()) code + Blocks.analyse (if Poly.(debug = `Full) then debug_data else Debug.create ()) code in let blocks = (* Disabled. [pc] might not be an appropriate place to split blocks *) - if false && debug = `Full + if false && Poly.(debug = `Full) then Debug.fold debug_data (fun pc _ blocks -> Blocks.add blocks pc) blocks else blocks in @@ -2014,7 +2014,7 @@ let seek_section toc ic name = let rec seek_sec curr_ofs = function | [] -> raise Not_found | (n, len) :: rem -> - if n = name + if String.equal n name then ( seek_in ic (curr_ofs - len); len) @@ -2077,7 +2077,7 @@ let from_exe (fun id -> keep (Ident.name id)) orig_symbols in - (if debug = `No + (if Poly.(debug = `No) then () else try @@ -2350,7 +2350,7 @@ let from_cmo ?(includes = []) seek_in ic compunit.Cmo_format.cu_pos; let code = Bytes.create compunit.Cmo_format.cu_codesize in really_input ic code 0 compunit.Cmo_format.cu_codesize; - if debug = `No || compunit.Cmo_format.cu_debug = 0 + if Poly.(debug = `No) || compunit.Cmo_format.cu_debug = 0 then () else ( seek_in ic compunit.Cmo_format.cu_debug; @@ -2373,7 +2373,7 @@ let from_cma ?(includes = []) seek_in ic compunit.Cmo_format.cu_pos; let code = Bytes.create compunit.Cmo_format.cu_codesize in really_input ic code 0 compunit.Cmo_format.cu_codesize; - if debug = `No || compunit.Cmo_format.cu_debug = 0 + if Poly.(debug = `No) || compunit.Cmo_format.cu_debug = 0 then () else ( seek_in ic compunit.Cmo_format.cu_debug; @@ -2400,14 +2400,16 @@ let from_channel ic = | `Pre magic -> ( match Magic_number.kind magic with | `Cmo -> - if Config.Flag.check_magic () && magic <> Magic_number.current_cmo + if Config.Flag.check_magic () + && not (Magic_number.equal magic Magic_number.current_cmo) then raise Magic_number.(Bad_magic_version magic); let compunit_pos = input_binary_int ic in seek_in ic compunit_pos; let compunit : Cmo_format.compilation_unit = input_value ic in `Cmo compunit | `Cma -> - if Config.Flag.check_magic () && magic <> Magic_number.current_cma + if Config.Flag.check_magic () + && not (Magic_number.equal magic Magic_number.current_cma) then raise Magic_number.(Bad_magic_version magic); let pos_toc = input_binary_int ic in (* Go to table of contents *) @@ -2418,7 +2420,8 @@ let from_channel ic = | `Post magic -> ( match Magic_number.kind magic with | `Exe -> - if Config.Flag.check_magic () && magic <> Magic_number.current_exe + if Config.Flag.check_magic () + && not (Magic_number.equal magic Magic_number.current_exe) then raise Magic_number.(Bad_magic_version magic); `Exe | _ -> raise Magic_number.(Bad_magic_number (to_string magic))) diff --git a/compiler/lib/parse_info.ml b/compiler/lib/parse_info.ml index 75cc1bdd0a..8597993756 100644 --- a/compiler/lib/parse_info.ml +++ b/compiler/lib/parse_info.ml @@ -16,6 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib type t = { src : string option diff --git a/compiler/lib/parse_js.ml b/compiler/lib/parse_js.ml index 7981cf30b9..38d7a07b29 100644 --- a/compiler/lib/parse_js.ml +++ b/compiler/lib/parse_js.ml @@ -16,7 +16,9 @@ * license.txt for more details. *) -let strip_comment l = List.filter (fun x -> not (Js_token.is_comment x)) l +open! Stdlib + +let strip_comment l = List.filter ~f:(fun x -> not (Js_token.is_comment x)) l let rec until_non_comment acc = function | [] -> acc, None @@ -92,7 +94,7 @@ let lexer_aux ?(rm_comment = true) lines_info lexbuf = | _ -> let extra = match t with - | Js_token.TComment (ii, cmt) when String.length cmt > 1 && cmt.[0] = '#' -> ( + | Js_token.TComment (ii, cmt) when String.is_prefix cmt ~prefix:"#" -> ( let lexbuf = Lexing.from_string cmt in try let file, line = Js_lexer.pos lexbuf in @@ -128,11 +130,11 @@ let lexer_from_string ?rm_comment ?offset str : lexer = let lexbuf = Lexing.from_string str in lexer_aux ?rm_comment lines_info lexbuf -let lexer_map = List.map +let lexer_map f = List.map ~f -let lexer_fold f acc l = List.fold_left f acc l +let lexer_fold f acc l = List.fold_left ~f ~init:acc l -let lexer_filter f l = List.filter f l +let lexer_filter f l = List.filter ~f l let lexer_from_list l = adjust_tokens l diff --git a/compiler/lib/phisimpl.ml b/compiler/lib/phisimpl.ml index 6434076ec7..25fb24daa4 100644 --- a/compiler/lib/phisimpl.ml +++ b/compiler/lib/phisimpl.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let times = Debug.find "times" @@ -125,7 +125,7 @@ module G = Dgraph.Make_Imperative (Var) (Var.ISet) (Var.Tbl) module Domain1 = struct type t = bool - let equal x y = x = y + let equal = Bool.equal let bot = false end diff --git a/compiler/lib/pretty_print.ml b/compiler/lib/pretty_print.ml index 519e18b728..56754c0411 100644 --- a/compiler/lib/pretty_print.ml +++ b/compiler/lib/pretty_print.ml @@ -18,6 +18,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Stdlib + type pos = { mutable p_line : int ; mutable p_col : int } @@ -54,7 +56,7 @@ let output st (s : string) l = let last = String.rindex_from s (l - 1) '\n' + 1 in let line = ref 0 in for i = 0 to l - 1 do - if s.[i] = '\n' then incr line + if Char.equal s.[i] '\n' then incr line done; st.line <- st.line + !line; st.col <- l - last @@ -120,7 +122,7 @@ let rec push st e = let l = List.rev st.l in st.l <- []; st.n <- 0; - List.iter (fun e -> push st e) l) + List.iter ~f:(fun e -> push st e) l) | Set_pos _ -> () | Start_group _ -> st.n <- st.n + 1 | End_group -> diff --git a/compiler/lib/primitive.ml b/compiler/lib/primitive.ml index 56b3600e94..89c84c2853 100644 --- a/compiler/lib/primitive.ml +++ b/compiler/lib/primitive.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let aliases = Hashtbl.create 17 @@ -57,7 +57,7 @@ let arity nm = Hashtbl.find arities (resolve nm) let has_arity nm a = try Hashtbl.find arities (resolve nm) = a with Not_found -> false -let is_pure nm = kind nm <> `Mutator +let is_pure nm = Poly.(kind nm <> `Mutator) let exists p = Hashtbl.mem kinds p diff --git a/compiler/lib/pseudoFs.ml b/compiler/lib/pseudoFs.ml index b423b00793..1bf41888f3 100644 --- a/compiler/lib/pseudoFs.ml +++ b/compiler/lib/pseudoFs.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let expand_path exts real virt = let rec loop realfile virtfile acc = @@ -35,7 +35,7 @@ let expand_path exts real virt = List.mem e ~set:exts with Not_found -> List.mem "" ~set:exts in - if exts = [] || exmatch then (virtfile, realfile) :: acc else acc + if List.is_empty exts || exmatch then (virtfile, realfile) :: acc else acc with exc -> warn "ignoring %s: %s@." realfile (Printexc.to_string exc); acc @@ -46,11 +46,11 @@ let list_files name paths = let name, virtname = match String.lsplit2 name ~on:':' with | Some (src, dest) -> - if String.length dest > 0 && dest.[0] <> '/' + if String.length dest > 0 && not (Char.equal dest.[0] '/') then failwith (Printf.sprintf "path '%s' for file '%s' must be absolute" dest src); let virtname = - if dest.[String.length dest - 1] = '/' + if Char.equal dest.[String.length dest - 1] '/' then dest ^ Filename.basename src else dest in @@ -106,7 +106,7 @@ let f ~prim ~cmis ~files ~paths = cmis ([], []) in - if missing_cmis <> [] + if not (List.is_empty missing_cmis) then ( warn "Some OCaml interface files were not found.@."; warn "Use [-I dir_of_cmis] option to bring them into scope@."; diff --git a/compiler/lib/pure_fun.ml b/compiler/lib/pure_fun.ml index 84dfbbfb45..4beda6ffac 100644 --- a/compiler/lib/pure_fun.ml +++ b/compiler/lib/pure_fun.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code (****) diff --git a/compiler/lib/reserved.ml b/compiler/lib/reserved.ml index 89a81ecd07..de8b48bf22 100644 --- a/compiler/lib/reserved.ml +++ b/compiler/lib/reserved.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let keyword = List.fold_left diff --git a/compiler/lib/source_map.ml b/compiler/lib/source_map.ml index 4fb1167d30..726b266b25 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -15,7 +15,9 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) +*) + +open! Stdlib type map = { gen_line : int @@ -40,7 +42,7 @@ let string_of_mapping mapping = let a = Array.of_list mapping in let len = Array.length a in Array.stable_sort - (fun t1 t2 -> + ~cmp:(fun t1 t2 -> match compare t1.gen_line t2.gen_line with | 0 -> compare t1.gen_col t2.gen_col | n -> n) @@ -173,7 +175,7 @@ let () = let map = mapping_of_string map_str in let map_str' = string_of_mapping map in (* let map' = mapping_of_string map_str' in *) - assert (map_str = map_str') + assert (String.equal map_str map_str') let merge_sources_content a b = match a, b with @@ -206,7 +208,7 @@ let merge = function ; mappings = acc.mappings @ List.map - (maps ~gen_line_offset ~sources_offset ~names_offset) + ~f:(maps ~gen_line_offset ~sources_offset ~names_offset) sm.mappings } in loop @@ -218,8 +220,9 @@ let merge = function let acc = { x with mappings = - List.map (maps ~gen_line_offset ~sources_offset:0 ~names_offset:0) x.mappings - } + List.map + ~f:(maps ~gen_line_offset ~sources_offset:0 ~names_offset:0) + x.mappings } in Some (loop diff --git a/compiler/lib/specialize.ml b/compiler/lib/specialize.ml index 8242ae453e..efb00b060d 100644 --- a/compiler/lib/specialize.ml +++ b/compiler/lib/specialize.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code open Flow diff --git a/compiler/lib/specialize_js.ml b/compiler/lib/specialize_js.ml index 19804a991e..ef4059aef5 100644 --- a/compiler/lib/specialize_js.ml +++ b/compiler/lib/specialize_js.ml @@ -18,7 +18,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code open Flow @@ -134,20 +134,20 @@ let specialize_instr info i rem = :: rem | Let (x, Prim (Extern "%int_mul", [y; z])) -> (match the_int info y, the_int info z with - | Some j, _ when Int32.abs j < 0x200000l -> + | Some j, _ when Int32.(abs j < 0x200000l) -> Let (x, Prim (Extern "%direct_int_mul", [y; z])) - | _, Some j when Int32.abs j < 0x200000l -> + | _, Some j when Int32.(abs j < 0x200000l) -> Let (x, Prim (Extern "%direct_int_mul", [y; z])) | _ -> i) :: rem | Let (x, Prim (Extern "%int_div", [y; z])) -> (match the_int info z with - | Some j when j <> 0l -> Let (x, Prim (Extern "%direct_int_div", [y; z])) + | Some j when Int32.(j <> 0l) -> Let (x, Prim (Extern "%direct_int_div", [y; z])) | _ -> i) :: rem | Let (x, Prim (Extern "%int_mod", [y; z])) -> (match the_int info z with - | Some j when j <> 0l -> Let (x, Prim (Extern "%direct_int_mod", [y; z])) + | Some j when Int32.(j <> 0l) -> Let (x, Prim (Extern "%direct_int_mod", [y; z])) | _ -> i) :: rem | _ -> i :: rem diff --git a/compiler/lib/stdlib.ml b/compiler/lib/stdlib.ml index 35eceb664d..d7031b11d2 100644 --- a/compiler/lib/stdlib.ml +++ b/compiler/lib/stdlib.ml @@ -14,7 +14,49 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) +*) + +module Poly = struct + external ( < ) : 'a -> 'a -> bool = "%lessthan" + + external ( <= ) : 'a -> 'a -> bool = "%lessequal" + + external ( <> ) : 'a -> 'a -> bool = "%notequal" + + external ( = ) : 'a -> 'a -> bool = "%equal" + + external ( > ) : 'a -> 'a -> bool = "%greaterthan" + + external ( >= ) : 'a -> 'a -> bool = "%greaterequal" + + external compare : 'a -> 'a -> int = "%compare" + + external equal : 'a -> 'a -> bool = "%equal" +end + +module Int_replace_polymorphic_compare = struct + let ( < ) (x : int) y = x < y + + let ( <= ) (x : int) y = x <= y + + let ( <> ) (x : int) y = x <> y + + let ( = ) (x : int) y = x = y + + let ( > ) (x : int) y = x > y + + let ( >= ) (x : int) y = x >= y + + let compare (x : int) y = compare x y + + let equal (x : int) y = x = y + + let max (x : int) y = if x >= y then x else y + + let min (x : int) y = if x <= y then x else y +end + +include Int_replace_polymorphic_compare let quiet = ref false @@ -82,6 +124,30 @@ module List = struct | x :: rest -> prev :: loop x rest in loop x xs + + let is_empty = function + | [] -> true + | _ -> false +end + +module Int32 = struct + include Int32 + + external ( < ) : int32 -> int32 -> bool = "%lessthan" + + external ( <= ) : int32 -> int32 -> bool = "%lessequal" + + external ( <> ) : int32 -> int32 -> bool = "%notequal" + + external ( = ) : int32 -> int32 -> bool = "%equal" + + external ( > ) : int32 -> int32 -> bool = "%greaterthan" + + external ( >= ) : int32 -> int32 -> bool = "%greaterequal" + + external compare : int32 -> int32 -> int = "%compare" + + external equal : int32 -> int32 -> bool = "%equal" end module Option = struct @@ -106,22 +172,84 @@ module Option = struct | None, Some _ -> -1 | Some _, None -> 1 | Some a, Some b -> compare_elt a b + + let equal equal_elt a b = + match a, b with + | None, None -> true + | Some a, Some b -> equal_elt a b + | Some _, None | None, Some _ -> false + + let is_none = function + | None -> true + | Some _ -> false + + let is_some = function + | None -> false + | Some _ -> true end -module Poly = struct - let compare : 'a. 'a -> 'a -> int = compare +module Float = struct + include Float + + external ( < ) : t -> t -> bool = "%lessthan" + + external ( <= ) : t -> t -> bool = "%lessequal" + + external ( <> ) : t -> t -> bool = "%notequal" + + external ( = ) : t -> t -> bool = "%equal" + + external ( > ) : t -> t -> bool = "%greaterthan" + + external ( >= ) : t -> t -> bool = "%greaterequal" +end + +module Bool = struct + external ( <> ) : bool -> bool -> bool = "%notequal" + + external ( = ) : bool -> bool -> bool = "%equal" - let equal : 'a. 'a -> 'a -> bool = ( = ) + external ( > ) : bool -> bool -> bool = "%greaterthan" + + external equal : bool -> bool -> bool = "%equal" end module Char = struct include Char + external ( < ) : char -> char -> bool = "%lessthan" + + external ( <= ) : char -> char -> bool = "%lessequal" + + external ( <> ) : char -> char -> bool = "%notequal" + + external ( = ) : char -> char -> bool = "%equal" + + external ( > ) : char -> char -> bool = "%greaterthan" + + external ( >= ) : char -> char -> bool = "%greaterequal" + + external compare : char -> char -> int = "%compare" + + external equal : char -> char -> bool = "%equal" + + let is_alpha = function + | 'a' .. 'z' | 'A' .. 'Z' -> true + | _ -> false + + let is_num = function + | '0' .. '9' -> true + | _ -> false + let lowercase_ascii c = - if c >= 'A' && c <= 'Z' then Char.unsafe_chr (Char.code c + 32) else c + match c with + | 'A' .. 'Z' as c -> Char.unsafe_chr (Char.code c + 32) + | _ -> c let uppercase_ascii c = - if c >= 'a' && c <= 'z' then Char.unsafe_chr (Char.code c - 32) else c + match c with + | 'a' .. 'z' as c -> Char.unsafe_chr (Char.code c - 32) + | _ -> c end module Bytes = struct @@ -131,12 +259,12 @@ module Bytes = struct end module String = struct - let equal (a : string) (b : string) = a = b - - let _ = equal - include StringLabels + let is_empty = function + | "" -> true + | _ -> false + let is_prefix ~prefix s = let len_a = length prefix in let len_s = length s in @@ -147,12 +275,24 @@ module String = struct let rec loop i = if i > max_idx_a then true - else if unsafe_get prefix i <> unsafe_get s i + else if not (Char.equal (unsafe_get prefix i) (unsafe_get s i)) then false else loop (i + 1) in loop 0 + let drop_prefix ~prefix s = + let plen = String.length prefix in + if plen > String.length s + then None + else + try + for i = 0 to String.length prefix - 1 do + if not (Char.equal s.[i] prefix.[i]) then raise Exit + done; + Some (String.sub s plen (String.length s - plen)) + with Exit -> None + let for_all = let rec loop s ~f ~last i = if i > last @@ -166,14 +306,16 @@ module String = struct let is_ascii s = let res = ref true in for i = 0 to String.length s - 1 do - if s.[i] > '\127' then res := false + match s.[i] with + | '\000' .. '\127' -> () + | '\128' .. '\255' -> res := false done; !res let has_backslash s = let res = ref false in for i = 0 to String.length s - 1 do - if s.[i] = '\\' then res := true + if Char.equal s.[i] '\\' then res := true done; !res @@ -182,7 +324,7 @@ module String = struct let rec split beg cur = if cur >= len then if cur - beg > 0 then [String.sub p beg (cur - beg)] else [] - else if p.[cur] = sep + else if Char.equal p.[cur] sep then String.sub p beg (cur - beg) :: split (cur + 1) (cur + 1) else split beg (cur + 1) in @@ -217,14 +359,15 @@ module String = struct that no separator can be found we exit the loop and make a substring from [sub_start] until the end of the string. *) while !i + sep_max <= s_max do - if String.unsafe_get s !i <> String.unsafe_get sep 0 + if not (Char.equal (String.unsafe_get s !i) (String.unsafe_get sep 0)) then incr i else ( (* Check remaining [sep] chars match, access to unsafe s (!i + !k) is guaranteed by loop invariant. *) k := 1; while - !k <= sep_max && String.unsafe_get s (!i + !k) = String.unsafe_get sep !k + !k <= sep_max + && Char.equal (String.unsafe_get s (!i + !k)) (String.unsafe_get sep !k) do incr k done; diff --git a/compiler/lib/strongly_connected_components.ml b/compiler/lib/strongly_connected_components.ml index 8fd22f2ca2..5f26c6cc7e 100644 --- a/compiler/lib/strongly_connected_components.ml +++ b/compiler/lib/strongly_connected_components.ml @@ -14,6 +14,8 @@ (* *) (**************************************************************************) +open! Stdlib + module IntSet = Set.Make (struct type t = int @@ -31,7 +33,7 @@ end = struct let size = Array.length graph in let transposed = Array.make size [] in let add src dst = transposed.(src) <- dst :: transposed.(src) in - Array.iteri (fun src dsts -> List.iter (fun dst -> add dst src) dsts) graph; + Array.iteri ~f:(fun src dsts -> List.iter ~f:(fun dst -> add dst src) dsts) graph; transposed let depth_first_order (graph : int list array) : int array = @@ -47,7 +49,7 @@ end = struct if not marked.(node) then ( marked.(node) <- true; - List.iter aux graph.(node); + List.iter ~f:aux graph.(node); push node) in for i = 0 to size - 1 do @@ -66,7 +68,7 @@ end = struct then ( marked.(node) <- true; id.(node) <- !count; - List.iter aux graph.(node)) + List.iter ~f:aux graph.(node)) in for i = size - 1 downto 0 do let node = order.(i) in @@ -92,16 +94,19 @@ end = struct let component_graph = Array.make ncomponents IntSet.empty in let add_component_dep node set = let node_deps = graph.(node) in - List.fold_left (fun set dep -> IntSet.add components.(dep) set) set node_deps + List.fold_left + ~f:(fun set dep -> IntSet.add components.(dep) set) + ~init:set + node_deps in Array.iteri - (fun node component -> + ~f:(fun node component -> id_scc.(component) <- node :: id_scc.(component); component_graph.(component) <- add_component_dep node component_graph.(component) ) components; { sorted_connected_components = id_scc - ; component_edges = Array.map IntSet.elements component_graph } + ; component_edges = Array.map ~f:IntSet.elements component_graph } end module type S = sig @@ -148,7 +153,7 @@ struct let size = Id.Map.cardinal graph in let bindings = Id.Map.bindings graph in let a = Array.of_list bindings in - let forth = Array.map fst a in + let forth = Array.map ~f:fst a in let back = let back = ref Id.Map.empty in for i = 0 to size - 1 do @@ -157,7 +162,7 @@ struct !back in let integer_graph = - Array.init size (fun i -> + Array.init size ~f:(fun i -> let _, dests = a.(i) in Id.Set.fold (fun dest acc -> @@ -174,19 +179,19 @@ struct Kosaraju.component_graph integer_graph in Array.mapi - (fun component nodes -> + ~f:(fun component nodes -> match nodes with | [] -> assert false | [node] -> - ( (if List.mem node integer_graph.(node) + ( (if List.mem node ~set:integer_graph.(node) then Has_loop [numbering.forth.(node)] else No_loop numbering.forth.(node)) , component_edges.(component) ) | _ :: _ -> - ( Has_loop (List.map (fun node -> numbering.forth.(node)) nodes) + ( Has_loop (List.map ~f:(fun node -> numbering.forth.(node)) nodes) , component_edges.(component) )) sorted_connected_components let connected_components_sorted_from_roots_to_leaf graph = - Array.map fst (component_graph graph) + Array.map ~f:fst (component_graph graph) end diff --git a/compiler/lib/subst.ml b/compiler/lib/subst.ml index f07b09e4bb..62a30869c0 100644 --- a/compiler/lib/subst.ml +++ b/compiler/lib/subst.ml @@ -18,7 +18,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib open Code let subst_cont s (pc, arg) = pc, List.map arg ~f:(fun x -> s x) diff --git a/compiler/lib/tailcall.ml b/compiler/lib/tailcall.ml index a7693d6466..0df4f9ee9b 100644 --- a/compiler/lib/tailcall.ml +++ b/compiler/lib/tailcall.ml @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib let times = Debug.find "times" diff --git a/compiler/lib/timer.ml b/compiler/lib/timer.ml index 0bf946b999..887c94ff62 100644 --- a/compiler/lib/timer.ml +++ b/compiler/lib/timer.ml @@ -14,7 +14,9 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) +*) + +open! Stdlib type t = float diff --git a/compiler/lib/varPrinter.ml b/compiler/lib/varPrinter.ml index 916aeb1044..5d5824d045 100644 --- a/compiler/lib/varPrinter.ml +++ b/compiler/lib/varPrinter.ml @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Stdlib +open! Stdlib module Alphabet = struct type t = @@ -76,17 +76,13 @@ let propagate_name t v v' = * with _ -> ()) *) with Not_found -> () -let is_alpha c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') - -let is_num c = c >= '0' && c <= '9' - let name t v nm_orig = let len = String.length nm_orig in if len > 0 then ( let buf = Buffer.create (String.length nm_orig) in let idx = ref 0 in - while !idx < len && not (is_alpha nm_orig.[!idx]) do + while !idx < len && not (Char.is_alpha nm_orig.[!idx]) do incr idx done; let pending = ref false in @@ -95,7 +91,7 @@ let name t v nm_orig = pending := true; idx := 0); for i = !idx to len - 1 do - if is_alpha nm_orig.[i] || is_num nm_orig.[i] + if Char.is_alpha nm_orig.[i] || Char.is_num nm_orig.[i] then ( if !pending then Buffer.add_char buf '_'; Buffer.add_char buf nm_orig.[i]; diff --git a/compiler/lib/vlq64.ml b/compiler/lib/vlq64.ml index c10a8680f6..83bb1e502e 100644 --- a/compiler/lib/vlq64.ml +++ b/compiler/lib/vlq64.ml @@ -15,7 +15,9 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - *) +*) + +open! Stdlib let code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" @@ -69,7 +71,7 @@ let encode b x = let vql = toVLQSigned x in encode' b vql -let encode_l b l = List.iter (encode b) l +let encode_l b l = List.iter ~f:(encode b) l let rec decode' acc s start pos = let digit = code_rev.(Char.code s.[pos]) in diff --git a/compiler/linkerArg.ml b/compiler/linkerArg.ml index f85a9b49d8..75897db81b 100644 --- a/compiler/linkerArg.ml +++ b/compiler/linkerArg.ml @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler open Cmdliner diff --git a/compiler/minifyArg.ml b/compiler/minifyArg.ml index 0120ba6714..f76b272f91 100644 --- a/compiler/minifyArg.ml +++ b/compiler/minifyArg.ml @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) +open! Js_of_ocaml_compiler.Stdlib open Js_of_ocaml_compiler open Cmdliner diff --git a/compiler/util.ml b/compiler/util.ml index 936b4c986b..55dff99856 100644 --- a/compiler/util.ml +++ b/compiler/util.ml @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -open Js_of_ocaml_compiler.Stdlib +open! Js_of_ocaml_compiler.Stdlib let normalize_argv ?(warn_ = false) a = let bad = ref [] in @@ -26,7 +26,9 @@ let normalize_argv ?(warn_ = false) a = let size = String.length s in if size <= 2 then s - else if s.[0] = '-' && s.[1] <> '-' && s.[2] <> '=' + else if Char.equal s.[0] '-' + && (not (Char.equal s.[1] '-')) + && not (Char.equal s.[2] '=') then ( bad := s :: !bad; (* long option with one dash lets double the dash *) @@ -34,7 +36,7 @@ let normalize_argv ?(warn_ = false) a = else s) a in - if warn_ && !bad <> [] + if warn_ && not (List.is_empty !bad) then warn "[Warning] long options with a single '-' are now deprecated. Please use '--' for \ diff --git a/toplevel/bin/jsoo_common.ml b/toplevel/bin/jsoo_common.ml index ac31a553fa..79b8dbf3c9 100644 --- a/toplevel/bin/jsoo_common.ml +++ b/toplevel/bin/jsoo_common.ml @@ -29,7 +29,8 @@ let unit_of_cma filename = let ic = open_in_bin filename in let len_magic_number = String.length Config.cma_magic_number in let magic_number = input_s ic len_magic_number in - if magic_number <> Config.cma_magic_number then failwith "not a cma file"; + if not (String.equal magic_number Config.cma_magic_number) + then failwith "not a cma file"; let toc_pos = input_binary_int ic in seek_in ic toc_pos; let lib : Cmo_format.library = input_value ic in @@ -73,7 +74,7 @@ let cmis_of_package pkg : string list = let add filename = fs := filename :: !fs in let archive = try Findlib.package_property ["byte"] pkg "archive" - with exc -> if pkg = "stdlib" then "stdlib.cma" else raise exc + with exc -> if String.equal pkg "stdlib" then "stdlib.cma" else raise exc in let l = String.split_char ~sep:' ' archive in List.iter l ~f:(fun x -> diff --git a/toplevel/lib/jsooTop.ml b/toplevel/lib/jsooTop.ml index dd3e8ac5a2..2e99b5fa5f 100644 --- a/toplevel/lib/jsooTop.ml +++ b/toplevel/lib/jsooTop.ml @@ -26,7 +26,7 @@ let split_primitives p = let rec split beg cur = if cur >= len then [] - else if p.[cur] = '\000' + else if Char.equal p.[cur] '\000' then String.sub p ~pos:beg ~len:(cur - beg) :: split (cur + 1) (cur + 1) else split beg (cur + 1) in