Skip to content

Commit e46118b

Browse files
authored
Refactor untagged logic (#6215)
* Refactor the logic for dynamic checks in untagged variants. * Refactor compile cases. * Refactor compile_cases * More refactor. * Rename * Refactor block respresentation. * get_literal unused in compile_general_cases * Type rename: use tag * refactor block * rename * Rename and comments.
1 parent cfdfc1e commit e46118b

13 files changed

+145
-150
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ and case_clause = {
244244
comment : string option;
245245
}
246246

247-
and string_clause = Ast_untagged_variants.literal_type * case_clause
247+
and string_clause = Ast_untagged_variants.tag_type * case_clause
248248
and int_clause = int * case_clause
249249

250250
and statement_desc =

jscomp/core/js_dump.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,15 @@ and expression_desc cxt ~(level : int) f x : cxt =
779779
tails
780780
else
781781
(Js_op.Lit tag_name, (* TAG:xx for inline records *)
782-
match Ast_untagged_variants.process_literal_type p.attrs with
782+
match Ast_untagged_variants.process_tag_type p.attrs with
783783
| None -> E.str p.name
784-
| Some literal -> E.literal literal )
784+
| Some t -> E.tag_type t )
785785
:: tails
786786
in
787787
expression_desc cxt ~level f (Object objs)
788788
| Caml_block (el, _, tag, Blk_constructor p) ->
789789
let not_is_cons = p.name <> Literals.cons in
790-
let literal = Ast_untagged_variants.process_literal_type p.attrs in
790+
let tag_type = Ast_untagged_variants.process_tag_type p.attrs in
791791
let untagged = Ast_untagged_variants.process_untagged p.attrs in
792792
let tag_name = match Ast_untagged_variants.process_tag_name p.attrs with
793793
| None -> L.tag
@@ -808,9 +808,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
808808
if untagged || (not_is_cons = false) && p.num_nonconst = 1 then tails
809809
else
810810
( Js_op.Lit tag_name, (* TAG:xx *)
811-
match literal with
811+
match tag_type with
812812
| None -> E.str p.name
813-
| Some literal -> E.literal literal )
813+
| Some t -> E.tag_type t )
814814
:: tails
815815
in
816816
let exp = match objs with
@@ -1210,8 +1210,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
12101210
let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in
12111211
P.space f;
12121212
P.brace_vgroup f 1 (fun _ ->
1213-
let pp_as_value f (literal: Ast_untagged_variants.literal_type) =
1214-
let e = E.literal literal in
1213+
let pp_as_value f (tag_type: Ast_untagged_variants.tag_type) =
1214+
let e = E.tag_type tag_type in
12151215
ignore @@ expression_desc cxt ~level:0 f e.expression_desc in
12161216
let cxt = loop_case_clauses cxt f pp_as_value cc in
12171217
match def with

jscomp/core/js_exp_make.ml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -336,22 +336,6 @@ let zero_float_lit : t =
336336
let float_mod ?comment e1 e2 : J.expression =
337337
{ comment; expression_desc = Bin (Mod, e1, e2) }
338338

339-
let literal = function
340-
| Ast_untagged_variants.String s -> str s ~delim:DStarJ
341-
| Int i -> small_int i
342-
| Float f -> float f
343-
| Bool b -> bool b
344-
| Null -> nil
345-
| Undefined -> undefined
346-
| Block IntType -> str "number"
347-
| Block FloatType -> str "number"
348-
| Block StringType -> str "string"
349-
| Block Array -> str "Array" ~delim:DNoQuotes
350-
| Block Object -> str "object"
351-
| Block Unknown ->
352-
(* TODO: clean up pattern mathing algo whih confuses literal with blocks *)
353-
assert false
354-
355339
let array_index ?comment (e0 : t) (e1 : t) : t =
356340
match (e0.expression_desc, e1.expression_desc) with
357341
| Array (l, _), Number (Int { i; _ })
@@ -776,7 +760,23 @@ let string_equal ?comment (e0 : t) (e1 : t) : t =
776760
let is_type_number ?comment (e : t) : t =
777761
string_equal ?comment (typeof e) (str "number")
778762

779-
let rec is_a_literal_case ~(literal_cases : Ast_untagged_variants.literal_type list) ~block_cases (e:t) : t =
763+
let tag_type = function
764+
| Ast_untagged_variants.String s -> str s ~delim:DStarJ
765+
| Int i -> small_int i
766+
| Float f -> float f
767+
| Bool b -> bool b
768+
| Null -> nil
769+
| Undefined -> undefined
770+
| Untagged IntType -> str "number"
771+
| Untagged FloatType -> str "number"
772+
| Untagged StringType -> str "string"
773+
| Untagged ArrayType -> str "Array" ~delim:DNoQuotes
774+
| Untagged ObjectType -> str "object"
775+
| Untagged UnknownType ->
776+
(* TODO: this should not happen *)
777+
assert false
778+
779+
let rec is_a_literal_case ~(literal_cases : Ast_untagged_variants.tag_type list) ~block_cases (e:t) : t =
780780
let literals_overlaps_with_string () =
781781
Ext_list.exists literal_cases (function
782782
| String _ -> true
@@ -793,24 +793,24 @@ let rec is_a_literal_case ~(literal_cases : Ast_untagged_variants.literal_type l
793793
let (!=) x y = bin NotEqEq x y in
794794
let (||) x y = bin Or x y in
795795
let (&&) x y = bin And x y in
796-
let is_literal_case (l:Ast_untagged_variants.literal_type) : t = e == (literal l) in
797-
let is_not_block_case (c:Ast_untagged_variants.block_type) : t = match c with
796+
let is_literal_case (t: Ast_untagged_variants.tag_type) : t = e == (tag_type t) in
797+
let is_not_block_case (c: Ast_untagged_variants.block_type) : t = match c with
798798
| StringType when literals_overlaps_with_string () = false (* No overlap *) ->
799799
(typeof e) != (str "string")
800800
| IntType when literals_overlaps_with_number () = false ->
801801
(typeof e) != (str "number")
802802
| FloatType when literals_overlaps_with_number () = false ->
803803
(typeof e) != (str "number")
804-
| Array ->
804+
| ArrayType ->
805805
not (is_array e)
806-
| Object when literals_overlaps_with_object () = false ->
806+
| ObjectType when literals_overlaps_with_object () = false ->
807807
(typeof e) != (str "object")
808-
| Object (* overlap *) ->
808+
| ObjectType (* overlap *) ->
809809
e == nil || (typeof e) != (str "object")
810810
| StringType (* overlap *)
811811
| IntType (* overlap *)
812812
| FloatType (* overlap *)
813-
| Unknown ->
813+
| UnknownType ->
814814
(* We don't know the type of unknown, so we need to express:
815815
this is not one of the literals *)
816816
(match literal_cases with

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ val assign_by_exp : t -> t -> t -> t
186186

187187
val assign : ?comment:string -> t -> t -> t
188188

189-
val literal : Ast_untagged_variants.literal_type -> t
189+
val tag_type : Ast_untagged_variants.tag_type -> t
190190

191191
val triple_equal : ?comment:string -> t -> t -> t
192192
(* TODO: reduce [triple_equal] use *)
@@ -205,7 +205,7 @@ val is_type_number : ?comment:string -> t -> t
205205

206206
val is_int_tag : ?has_null_undefined_other:(bool * bool * bool) -> t -> t
207207

208-
val is_a_literal_case : literal_cases:Ast_untagged_variants.literal_type list -> block_cases:Ast_untagged_variants.block_type list -> t -> t
208+
val is_a_literal_case : literal_cases:Ast_untagged_variants.tag_type list -> block_cases:Ast_untagged_variants.block_type list -> t -> t
209209

210210
val is_type_string : ?comment:string -> t -> t
211211

jscomp/core/js_stmt_make.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let int_switch ?(comment : string option)
129129

130130
let string_switch ?(comment : string option)
131131
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
132-
(e : J.expression) (clauses : (Ast_untagged_variants.literal_type * J.case_clause) list) : t =
132+
(e : J.expression) (clauses : (Ast_untagged_variants.tag_type * J.case_clause) list) : t =
133133
match e.expression_desc with
134134
| Str {txt} -> (
135135
let continuation =
@@ -138,9 +138,8 @@ let string_switch ?(comment : string option)
138138
match switch_case with
139139
| String s ->
140140
if s = txt then Some x.switch_body else None
141-
| Int _ | Float _| Bool _ | Null
142-
| Undefined
143-
| Block _ -> None)
141+
| Int _ | Float _| Bool _ | Null | Undefined | Untagged _ ->
142+
None)
144143
with
145144
| Some case -> case
146145
| None -> ( match default with Some x -> x | None -> assert false)

jscomp/core/js_stmt_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ val string_switch :
7777
?declaration:Lam_compat.let_kind * Ident.t ->
7878
?default:J.block ->
7979
J.expression ->
80-
(Ast_untagged_variants.literal_type * J.case_clause) list ->
80+
(Ast_untagged_variants.tag_type * J.case_clause) list ->
8181
t
8282

8383
val declare_variable :

0 commit comments

Comments
 (0)