Skip to content

Commit

Permalink
refactor: move functions operating on types to the respective modules (
Browse files Browse the repository at this point in the history
…#1210)

* refactor: move functions operating on types to the respective modules

* wip
  • Loading branch information
anmonteiro authored Nov 4, 2024
1 parent dd4433b commit 2492514
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 279 deletions.
20 changes: 18 additions & 2 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@

open Import

let same_vident (x : J.vident) (y : J.vident) =
match (x, y) with
| Id x0, Id y0 -> Ident.same x0 y0
| Qualified (x, str_opt0), Qualified (y, str_opt1) ->
let same_kind (x : Js_op.kind) (y : Js_op.kind) =
match (x, y) with
| Ml, Ml | Runtime, Runtime -> true
| External { name = u; _ }, External { name = v; _ } ->
u = v (* not comparing Default since we will do it later *)
| _, _ -> false
in
Ident.same x.id y.id && same_kind x.kind y.kind
&& Option.equal String.equal str_opt0 str_opt1
| Id _, Qualified _ | Qualified _, Id _ -> false

type idents_stats = {
mutable used_idents : Ident.Set.t;
mutable defined_idents : Ident.Set.t;
Expand Down Expand Up @@ -58,7 +73,8 @@ let free_variables (stats : idents_stats) =
| Fun { env; _ }
(* a optimization to avoid walking into function again
if it's already comuted
*) ->
*)
->
stats.used_idents <-
Ident.Set.union (Js_fun_env.get_unbounded env) stats.used_idents
| _ -> super.expression self exp);
Expand Down Expand Up @@ -188,7 +204,7 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression)
| Call { expr = b0; args = args10; _ } ->
eq_expression a0 b0 && eq_expression_list args00 args10
| _ -> false)
| Var x -> ( match y0 with Var y -> Js_op_util.same_vident x y | _ -> false)
| Var x -> ( match y0 with Var y -> same_vident x y | _ -> false)
| Bin { op = op0; expr1 = a0; expr2 = b0 } -> (
match y0 with
| Bin { op = op1; expr1 = a1; expr2 = b1 } ->
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/js_analyzer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ val rev_toplevel_flatten : J.block -> J.block
*)

val is_okay_to_duplicate : J.expression -> bool
val same_vident : J.vident -> J.vident -> bool
33 changes: 0 additions & 33 deletions jscomp/core/js_ast_util.ml

This file was deleted.

25 changes: 0 additions & 25 deletions jscomp/core/js_ast_util.mli

This file was deleted.

17 changes: 9 additions & 8 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,13 @@ and expression_desc cxt ~(level : int) x : cxt =
{[ 0. - x ]}
{[ 0.00 - x ]}
{[ 0.000 - x ]}
*) ->
*)
->
cond_paren_group cxt (level > 13) 1 (fun () ->
string cxt (match desc with Float _ -> "- " | _ -> "-");
expression ~level:13 cxt e)
| Bin { op; expr1 = e1; expr2 = e2 } ->
let out, lft, rght = Js_op_util.op_prec op in
let out, lft, rght = Js_op.op_prec op in
let need_paren =
level > out || match op with Lsl | Lsr | Asr -> true | _ -> false
in
Expand All @@ -746,12 +747,12 @@ and expression_desc cxt ~(level : int) x : cxt =
cond_paren_group cxt need_paren 1 (fun () ->
let cxt = expression ~level:lft cxt e1 in
space cxt;
string cxt (Js_op_util.op_str op);
string cxt (Js_op.op_str op);
space cxt;
expression ~level:rght cxt e2)
| String_append { prefix = e1; suffix = e2 } ->
let op : Js_op.binop = Plus in
let out, lft, rght = Js_op_util.op_prec op in
let out, lft, rght = Js_op.op_prec op in
let need_paren =
level > out || match op with Lsl | Lsr | Asr -> true | _ -> false
in
Expand Down Expand Up @@ -825,7 +826,7 @@ and expression_desc cxt ~(level : int) x : cxt =
in
expression_desc cxt ~level (Object objs)
| Caml_block { fields = el; tag; tag_info = Blk_constructor p; _ } ->
let is_cons = Js_op_util.is_cons p.name in
let is_cons = Js_op.is_cons p.name in
let objs =
let tails =
List.mapi
Expand Down Expand Up @@ -1156,15 +1157,15 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
let right_prec =
match direction with
| Upto ->
let _, _, right = Js_op_util.op_prec Le in
let _, _, right = Js_op.op_prec Le in
string cxt L.le;
right
| Up ->
let _, _, right = Js_op_util.op_prec Lt in
let _, _, right = Js_op.op_prec Lt in
string cxt L.lt;
right
| Downto ->
let _, _, right = Js_op_util.op_prec Ge in
let _, _, right = Js_op.op_prec Ge in
string cxt L.ge;
right
in
Expand Down
19 changes: 10 additions & 9 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ let cons_pos pos =
match pos with 0l -> L.hd | 1l -> L.tl | _ -> noncons_pos pos

let variant_pos ~constr pos =
match Js_op_util.is_cons constr with
match Js_op.is_cons constr with
| true -> cons_pos pos
| false -> noncons_pos pos

Expand Down Expand Up @@ -664,13 +664,13 @@ let bin ?loc ?comment (op : J.binop) (e0 : t) (e1 : t) : t =

let and_ ?loc ?comment (e1 : t) (e2 : t) : t =
match (e1.expression_desc, e2.expression_desc) with
| Var i, Var j when Js_op_util.same_vident i j -> e1
| Var i, Var j when Js_analyzer.same_vident i j -> e1
| Var i, Bin { op = And; expr1 = { expression_desc = Var j; _ }; _ }
when Js_op_util.same_vident i j ->
when Js_analyzer.same_vident i j ->
e2
| ( Var i,
Bin { op = And; expr1 = l; expr2 = { expression_desc = Var j; _ } as r } )
when Js_op_util.same_vident i j ->
when Js_analyzer.same_vident i j ->
{ e2 with expression_desc = Bin { op = And; expr1 = r; expr2 = l } }
| ( Bin
{
Expand All @@ -684,20 +684,20 @@ let and_ ?loc ?comment (e1 : t) (e2 : t) : t =
expr1 = { expression_desc = Var j; _ };
expr2 = { expression_desc = Str _ | Number _ | Unicode _; _ };
} )
when Js_op_util.same_vident i j ->
when Js_analyzer.same_vident i j ->
e2
| _, _ ->
make_expression ?loc ?comment (Bin { op = And; expr1 = e1; expr2 = e2 })

let or_ ?loc ?comment (e1 : t) (e2 : t) =
match (e1.expression_desc, e2.expression_desc) with
| Var i, Var j when Js_op_util.same_vident i j -> e1
| Var i, Var j when Js_analyzer.same_vident i j -> e1
| Var i, Bin { op = Or; expr1 = { expression_desc = Var j; _ }; _ }
when Js_op_util.same_vident i j ->
when Js_analyzer.same_vident i j ->
e2
| ( Var i,
Bin { op = Or; expr1 = l; expr2 = { expression_desc = Var j; _ } as r } )
when Js_op_util.same_vident i j ->
when Js_analyzer.same_vident i j ->
{ e2 with expression_desc = Bin { op = Or; expr1 = r; expr2 = l } }
| _, _ ->
make_expression ?loc ?comment (Bin { op = Or; expr1 = e1; expr2 = e2 })
Expand Down Expand Up @@ -1003,7 +1003,8 @@ let rec int_comp (cmp : Lam_compat.integer_comparison) ?loc ?comment (e0 : t)
expression_desc =
Var
(Qualified
(({ id = _; kind = Runtime; _ } as iid), Some "caml_compare"));
( ({ id = _; kind = Runtime; _ } as iid),
Some "caml_compare" ));
_;
} as fn;
args = [ _; _ ] as args;
Expand Down
99 changes: 98 additions & 1 deletion jscomp/core/js_op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open Import

type binop =
| Eq
(* acutally assignment ..
(* actually assignment ..
TODO: move it into statement, so that all expressions
are side efffect free (except function calls)
*)
Expand Down Expand Up @@ -179,6 +179,103 @@ type tag_info = Lam.Tag_info.t
type length_object = Array | String | Bytes | Function | Caml_block

(** TODO: define constant - for better constant folding *)

(* type constant = *)
(* | Const_int of int *)
(* | Const_ *)

(* Refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
for precedence
*)

(* val op_prec : Js_op.binop -> int * int * int
val op_str : Js_op.binop -> string
val op_int_prec : Js_op.int_op -> int * int * int
val op_int_str : Js_op.int_op -> string
val str_of_used_stats : Js_op.used_stats -> string
val update_used_stats : J.ident_info -> Js_op.used_stats -> unit
val of_lam_mutable_flag : Asttypes.mutable_flag -> Js_op.mutable_flag
val is_cons : string -> bool
*)
let op_prec (op : binop) =
match op with
| Eq -> (1, 13, 1)
| Or -> (3, 3, 3)
| And -> (4, 4, 4)
| EqEqEq | NotEqEq -> (8, 8, 9)
| Gt | Ge | Lt | Le (* | InstanceOf *) -> (9, 9, 10)
| Bor -> (5, 5, 5)
| Bxor -> (6, 6, 6)
| Band -> (7, 7, 7)
| Lsl | Lsr | Asr -> (10, 10, 11)
| Plus | Minus -> (11, 11, 12)
| Mul | Div | Mod -> (12, 12, 13)

let op_int_prec (op : int_op) =
match op with
| Bor -> (5, 5, 5)
| Bxor -> (6, 6, 6)
| Band -> (7, 7, 7)
| Lsl | Lsr | Asr -> (10, 10, 11)
| Plus | Minus -> (11, 11, 12)
| Mul | Div | Mod -> (12, 12, 13)

let op_str (op : binop) =
match op with
| Bor -> "|"
| Bxor -> "^"
| Band -> "&"
| Lsl -> "<<"
| Lsr -> ">>>"
| Asr -> ">>"
| Plus -> "+"
| Minus -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "%"
| Eq -> "="
| Or -> "||"
| And -> "&&"
| EqEqEq -> "==="
| NotEqEq -> "!=="
| Lt -> "<"
| Le -> "<="
| Gt -> ">"
| Ge -> ">="
(* | InstanceOf -> "instanceof" *)

let op_int_str (op : int_op) =
match op with
| Bor -> "|"
| Bxor -> "^"
| Band -> "&"
| Lsl -> "<<"
| Lsr -> ">>>"
| Asr -> ">>"
| Plus -> "+"
| Minus -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "%"

let str_of_used_stats x =
match (x : used_stats) with
| Dead_pure -> "Dead_pure"
| Dead_non_pure -> "Dead_non_pure"
| Exported -> "Exported"
| Once_pure -> "Once_pure"
| Used -> "Used"
| Scanning_pure -> "Scanning_pure"
| Scanning_non_pure -> "Scanning_non_pure"
| NA -> "NA"

let update_used_stats (ident_info : ident_info) used_stats =
match ident_info.used_stats with
| Dead_pure | Dead_non_pure | Exported -> ()
| Scanning_pure | Scanning_non_pure | Used | Once_pure | NA ->
ident_info.used_stats <- used_stats

let of_lam_mutable_flag (x : Asttypes.mutable_flag) : mutable_flag =
match x with Immutable -> Immutable | Mutable -> Mutable

let is_cons = function "::" -> true | _ -> false
Loading

0 comments on commit 2492514

Please sign in to comment.