Skip to content

Commit aaf840b

Browse files
committed
document js_stmt_make.mli
1 parent e1f4308 commit aaf840b

9 files changed

+122
-64
lines changed

jscomp/core/js_ast_util.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ let rec named_expression (e : J.expression)
3939
else
4040
let obj = Ext_ident.create_tmp () in
4141
let obj_code =
42-
S.define
42+
S.define_variable
4343
~kind:Strict obj e in
4444
Some (obj_code, obj)

jscomp/core/js_output.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ let handle_name_tail
6969
| EffectCall, ReturnTrue _ ->
7070
make [S.return_stmt exp] ~finished:True
7171
| Declare (kind, n), ReturnFalse ->
72-
make [ S.define ~kind n exp]
72+
make [ S.define_variable ~kind n exp]
7373
| Assign n ,ReturnFalse ->
7474
make [S.assign n exp ]
7575
| (Declare _ | Assign _ ), ReturnTrue _ ->
@@ -83,7 +83,7 @@ let handle_block_return
8383
(lam : Lam.t) (block : J.block) exp : t =
8484
match st, should_return with
8585
| Declare (kind,n), ReturnFalse ->
86-
make (block @ [ S.define ~kind n exp])
86+
make (block @ [ S.define_variable ~kind n exp])
8787
| Assign n, ReturnFalse -> make (block @ [S.assign n exp])
8888
| (Declare _ | Assign _), ReturnTrue _ -> make [S.unknown_lambda lam] ~finished:True
8989
| EffectCall, ReturnFalse -> make block ~value:exp

jscomp/core/js_pass_flatten_and_mark_dead.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class rewrite_return ?return_value ()=
4949
let mk_return =
5050
match return_value with
5151
| None -> fun e -> S.exp e
52-
| Some ident -> fun e -> S.define ~kind:Variable ident e in
52+
| Some ident -> fun e ->
53+
S.define_variable ~kind:Variable ident e in
5354
object (self : 'self)
5455
inherit Js_map.map as super
5556
method! statement x =
@@ -229,8 +230,9 @@ let subst_map name = object (self)
229230
| _ ->
230231
(* self#add_substitue ident e ; *)
231232
S.block @@
232-
(Ext_list.rev_map_append (fun (id,v) ->
233-
S.define ~kind:Strict id v) bindings [original_statement] )
233+
(Ext_list.rev_map_append
234+
(fun (id,v) ->
235+
S.define_variable ~kind:Strict id v) bindings [original_statement] )
234236
end
235237
| _ -> super#statement v
236238

jscomp/core/js_pass_tailcall_inline.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ let subst name export_set stats =
211211
(* (Js_dump.string_of_block [st]); *)
212212
Js_op_util.update_used_stats v.ident_info Dead_pure;
213213
let block =
214-
Ext_list.fold_right2 (fun param arg acc -> S.define ~kind:Variable param arg :: acc)
214+
Ext_list.fold_right2
215+
(fun param arg acc ->
216+
S.define_variable ~kind:Variable param arg :: acc)
215217
params args ( self#block block) (* see #278 before changes*)
216218

217219
in

jscomp/core/js_stmt_make.ml

+17-13
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ let declare_variable ?comment ?ident_info ~kind (v:Ident.t) : t=
7474
ident_info ;};
7575
comment}
7676

77-
let define ?comment ?ident_info ~kind (v:Ident.t) exp : t=
77+
let define_variable ?comment ?ident_info
78+
~kind (v:Ident.t) exp : t=
7879
let property : J.property = kind in
7980
let ident_info : J.ident_info =
8081
match ident_info with
@@ -85,6 +86,14 @@ let define ?comment ?ident_info ~kind (v:Ident.t) exp : t=
8586
ident_info ;};
8687
comment}
8788

89+
let alias_variable ?comment ~exp (v:Ident.t) : t=
90+
{statement_desc =
91+
Variable {
92+
ident = v; value = Some exp; property = Alias;
93+
ident_info = {used_stats = NA } };
94+
comment}
95+
96+
8897
let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t =
8998
match e.expression_desc with
9099
| Number (Int {i; _}) ->
@@ -105,7 +114,7 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
105114
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
106115
when Ident.same did id
107116
->
108-
define ?comment ~kind id e0
117+
define_variable ?comment ~kind id e0
109118
| Some(kind,did), _
110119
->
111120
block (declare_variable ?comment ~kind did :: continuation)
@@ -142,7 +151,7 @@ let string_switch ?comment ?declaration ?default (e : J.expression) clauses :
142151
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id); _}, e0);_} ; _}]
143152
when Ident.same did id
144153
->
145-
define ?comment ~kind id e0
154+
define_variable ?comment ~kind id e0
146155
| Some(kind,did), _
147156
->
148157
block @@ declare_variable ?comment ~kind did :: continuation
@@ -190,7 +199,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
190199
begin match declaration with
191200
| Some (kind,did) when Ident.same did id0 ->
192201
declared := true;
193-
define ~kind id0 (E.econd e a0 b0) :: acc
202+
define_variable ~kind id0 (E.econd e a0 b0) :: acc
194203
(* To hit this branch, we also need [declaration] passed down
195204
TODO: check how we compile [Lifthenelse]
196205
*)
@@ -278,12 +287,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
278287

279288

280289

281-
let alias_variable ?comment ?exp (v:Ident.t) : t=
282-
{statement_desc =
283-
Variable {
284-
ident = v; value = exp; property = Alias;
285-
ident_info = {used_stats = NA } };
286-
comment}
290+
287291

288292
let assign ?comment id e : t =
289293
{
@@ -354,7 +358,7 @@ let continue_stmt ?comment ?(label="") unit : t =
354358
comment;
355359
}
356360

357-
let debugger : t =
358-
{ statement_desc = J.Debugger ;
361+
let debugger_block : t list =
362+
[{ statement_desc = J.Debugger ;
359363
comment = None
360-
}
364+
}]

jscomp/core/js_stmt_make.mli

+23-3
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,27 @@ val string_switch :
9595
string J.case_clause list ->
9696
t
9797

98+
(** Just declaration without initialization *)
9899
val declare_variable :
99100
?comment:string ->
100101
?ident_info:J.ident_info ->
101102
kind:Lam.let_kind ->
102103
Ident.t ->
103104
t
104105

105-
val define :
106+
(*** Declaration with initialization *)
107+
val define_variable :
106108
?comment:string ->
107109
?ident_info:J.ident_info ->
108110
kind:Lam.let_kind ->
109111
Ident.t ->
110112
J.expression ->
111113
t
112114

115+
(** created an alias expression *)
113116
val alias_variable :
114117
?comment:string ->
115-
?exp:J.expression ->
118+
exp:J.expression ->
116119
Ident.t ->
117120
t
118121

@@ -122,11 +125,25 @@ val assign :
122125
J.expression ->
123126
t
124127

128+
(** Used in cases like
129+
{[
130+
let x = while true do
131+
...
132+
done in ..
133+
]}
134+
*)
125135
val assign_unit :
126136
?comment:string ->
127137
J.ident ->
128138
t
129139

140+
(** used in cases like
141+
{[
142+
let x = while true do
143+
...
144+
done in ..
145+
]}
146+
*)
130147
val declare_unit :
131148
?comment:string ->
132149
J.ident ->
@@ -167,6 +184,9 @@ val return_stmt :
167184
J.expression ->
168185
t
169186

187+
(** TODO: this should
188+
be marked as failrue
189+
*)
170190
val unknown_lambda :
171191
?comment:string ->
172192
Lam.t ->
@@ -183,4 +203,4 @@ val continue_stmt :
183203
unit ->
184204
t
185205

186-
val debugger : t
206+
val debugger_block : t list

jscomp/core/lam_compile.ml

+9-8
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ and compile_recursive_let ~all_bindings
281281
(
282282
Ident_map.fold
283283
(fun old new_param acc ->
284-
S.define ~kind:Alias old (E.var new_param) :: acc)
284+
S.define_variable ~kind:Alias old (E.var new_param) :: acc)
285285
ret.new_params body_block
286286
)
287287
]
@@ -304,7 +304,7 @@ and compile_recursive_let ~all_bindings
304304
and need to be declared first
305305
*)
306306
Js_output.of_block (
307-
S.define ~kind:Variable id (E.array Mutable []) ::
307+
S.define_variable ~kind:Variable id (E.array Mutable []) ::
308308
(List.mapi (fun i (x : Lam.t) ->
309309
match x with
310310
| Lvar lid
@@ -375,7 +375,7 @@ and compile_recursive_lets_aux cxt id_args : Js_output.t =
375375
| [] -> output_code
376376
| _ ->
377377
(Js_output.of_block @@
378-
Ext_list.map (fun id -> S.define ~kind:Variable id (E.dummy_obj ())) ids )
378+
Ext_list.map (fun id -> S.define_variable ~kind:Variable id (E.dummy_obj ())) ids )
379379
++ output_code
380380
and compile_recursive_lets cxt id_args : Js_output.t =
381381

@@ -691,7 +691,8 @@ and
691691
->
692692
(* [%bs.debugger] guarantees that the expression does not matter
693693
TODO: make it even safer *)
694-
Js_output.handle_block_return st should_return lam [S.debugger] E.unit
694+
Js_output.handle_block_return st should_return lam
695+
S.debugger_block E.unit
695696

696697

697698

@@ -933,7 +934,7 @@ and
933934
(* Invariant: should_return is false*)
934935
Js_output.make @@
935936
Ext_list.append b [
936-
S.define ~kind id (E.econd e out1 out2) ]
937+
S.define_variable ~kind id (E.econd e out1 out2) ]
937938
| _, _ ->
938939
Js_output.make
939940
( Ext_list.append b [
@@ -1140,7 +1141,7 @@ and
11401141
| _ ->
11411142
let v = Ext_ident.create_tmp () in
11421143
(* Necessary avoid duplicated computation*)
1143-
(S.define ~kind:Variable v e ) :: dispatch (E.var v)
1144+
(S.define_variable ~kind:Variable v e ) :: dispatch (E.var v)
11441145
end
11451146
| _, _, {value = None; _} -> assert false
11461147
in
@@ -1235,7 +1236,7 @@ and
12351236
TODO: wait for a bug fix
12361237
*)
12371238
let declares =
1238-
S.define ~kind:Variable exit_id
1239+
S.define_variable ~kind:Variable exit_id
12391240
E.zero_int_literal ::
12401241
(* we should always make it zero here, since [zero] is reserved in our mapping*)
12411242
Ext_list.map (fun x -> S.declare_variable ~kind:Variable x ) bindings in
@@ -1366,7 +1367,7 @@ and
13661367
body) ])
13671368
| _ , _
13681369
->
1369-
Ext_list.append b1 (S.define ~kind:Variable id e1 :: (Ext_list.append b2 [
1370+
Ext_list.append b1 (S.define_variable ~kind:Variable id e1 :: (Ext_list.append b2 [
13701371
S.for_ None e2 id direction
13711372
(Js_output.to_block @@
13721373
compile_lambda {cxt with should_return = ReturnFalse ; st = EffectCall}

jscomp/core/lam_compile_external_obj.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ let assemble_args_obj (labels : External_arg_spec.t list) (args : J.expression
9999
| _ ->
100100
let v = Ext_ident.create_tmp () in
101101
let var_v = E.var v in
102-
S.define ~kind:Variable v
102+
S.define_variable ~kind:Variable v
103103
(begin match eff with
104104
| [] ->
105105
E.obj map

0 commit comments

Comments
 (0)