Skip to content

Commit 5797973

Browse files
committed
introducing iter visitor
1 parent 136c320 commit 5797973

12 files changed

+746
-34
lines changed

Diff for: jscomp/core/js_analyzer.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ let add_defined_idents (x : idents_stats) ident =
4444
Note such shaking is done in the toplevel, so that it requires us to
4545
flatten the statement first
4646
*)
47-
let free_variables (stats : idents_stats) : Js_fold.fold =
47+
let free_variables (stats : idents_stats) : Js_iter.iter =
4848
object (self)
49-
inherit Js_fold.fold as super
49+
inherit Js_iter.iter as super
5050
method! variable_declaration st =
5151
add_defined_idents stats st.ident;
5252
match st.value with
5353
| None
54-
-> self
54+
-> ()
5555
| Some v
5656
->
5757
self # expression v
5858
method! ident id =
5959
(if not (Set_ident.mem stats.defined_idents id )then
60-
stats.used_idents <- Set_ident.add stats.used_idents id);
61-
self
60+
stats.used_idents <- Set_ident.add stats.used_idents id)
61+
6262
method! expression exp =
6363
match exp.expression_desc with
6464
| Fun(_, _,_, env)
@@ -67,8 +67,8 @@ let free_variables (stats : idents_stats) : Js_fold.fold =
6767
*)
6868
->
6969
stats.used_idents <-
70-
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
71-
self
70+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents
71+
7272

7373
| _
7474
->

Diff for: jscomp/core/js_iter.ml

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
open J
3+
class virtual iter =
4+
object ((o : 'self_type))
5+
method unknown : 'a. 'a -> unit = ignore
6+
method string : string -> unit = ignore
7+
method option :
8+
'a. ('self_type -> 'a -> unit) -> 'a option -> unit =
9+
fun _f_a -> function | None -> () | Some _x -> _f_a o _x
10+
method list :
11+
'a. ('self_type -> 'a -> unit) -> 'a list -> unit =
12+
fun _f_a ->
13+
function
14+
| [] -> ()
15+
| _x :: _x_i1 -> _f_a o _x ; o#list _f_a _x_i1
16+
method int32 : int32 -> unit = ignore
17+
method int : int -> unit = ignore
18+
method bool : bool -> unit = ignore
19+
method label : label -> unit = o#string
20+
method binop : binop -> unit = o#unknown
21+
method int_op : int_op -> unit = o#unknown
22+
method kind : kind -> unit = o#unknown
23+
method property : property -> unit = o#unknown
24+
method number : number -> unit = o#unknown
25+
method mutable_flag : mutable_flag -> unit = o#unknown
26+
method ident_info : ident_info -> unit = o#unknown
27+
method exports : exports -> unit = o#unknown
28+
method tag_info : tag_info -> unit = o#unknown
29+
method required_modules : required_modules -> unit = o#list (fun o -> o#module_id)
30+
method property_name : property_name -> unit = o#unknown
31+
method ident : ident -> unit = o#unknown
32+
method module_id : module_id -> unit = fun { id = _x0;kind = _x1} -> begin o#ident _x0;o#unknown _x1 end
33+
method vident : vident -> unit = function
34+
| Id ( _x0) ->
35+
begin o#ident _x0 end
36+
|Qualified ( _x0,_x1) ->
37+
begin o#module_id _x0 ;o#option (fun o -> o#string) _x1 end
38+
method exception_ident : exception_ident -> unit = o#ident
39+
method for_ident : for_ident -> unit = o#ident
40+
method for_direction : for_direction -> unit = o#unknown
41+
method property_map : property_map -> unit = o#list (fun o -> fun ( _x0,_x1) -> begin o#property_name _x0;o#expression _x1 end)
42+
method length_object : length_object -> unit = o#unknown
43+
method expression_desc : expression_desc -> unit = function
44+
| Length ( _x0,_x1) ->
45+
begin o#expression _x0 ;o#length_object _x1 end
46+
|Char_of_int ( _x0) ->
47+
begin o#expression _x0 end
48+
|Char_to_int ( _x0) ->
49+
begin o#expression _x0 end
50+
|Is_null_or_undefined ( _x0) ->
51+
begin o#expression _x0 end
52+
|String_append ( _x0,_x1) ->
53+
begin o#expression _x0 ;o#expression _x1 end
54+
|Bool ( _x0) ->
55+
begin o#bool _x0 end
56+
|Typeof ( _x0) ->
57+
begin o#expression _x0 end
58+
|Js_not ( _x0) ->
59+
begin o#expression _x0 end
60+
|Seq ( _x0,_x1) ->
61+
begin o#expression _x0 ;o#expression _x1 end
62+
|Cond ( _x0,_x1,_x2) ->
63+
begin o#expression _x0 ;o#expression _x1 ;o#expression _x2 end
64+
|Bin ( _x0,_x1,_x2) ->
65+
begin o#binop _x0 ;o#expression _x1 ;o#expression _x2 end
66+
|FlatCall ( _x0,_x1) ->
67+
begin o#expression _x0 ;o#expression _x1 end
68+
|Call ( _x0,_x1,_x2) ->
69+
begin o#expression _x0 ;o#list (fun o -> o#expression) _x1 ;o#unknown _x2 end
70+
|String_index ( _x0,_x1) ->
71+
begin o#expression _x0 ;o#expression _x1 end
72+
|Array_index ( _x0,_x1) ->
73+
begin o#expression _x0 ;o#expression _x1 end
74+
|Static_index ( _x0,_x1,_x2) ->
75+
begin o#expression _x0 ;o#string _x1 ;o#option (fun o -> o#int32) _x2 end
76+
|New ( _x0,_x1) ->
77+
begin o#expression _x0 ;o#option (fun o -> o#list (fun o -> o#expression)) _x1 end
78+
|Var ( _x0) ->
79+
begin o#vident _x0 end
80+
|Fun ( _x0,_x1,_x2,_x3) ->
81+
begin o#bool _x0 ;o#list (fun o -> o#ident) _x1 ;o#block _x2 ;o#unknown _x3 end
82+
|Str ( _x0,_x1) ->
83+
begin o#bool _x0 ;o#string _x1 end
84+
|Unicode ( _x0) ->
85+
begin o#string _x0 end
86+
|Raw_js_code ( _x0) ->
87+
begin o#unknown _x0 end
88+
|Array ( _x0,_x1) ->
89+
begin o#list (fun o -> o#expression) _x0 ;o#mutable_flag _x1 end
90+
|Optional_block ( _x0,_x1) ->
91+
begin o#expression _x0 ;o#bool _x1 end
92+
|Caml_block ( _x0,_x1,_x2,_x3) ->
93+
begin o#list (fun o -> o#expression) _x0 ;o#mutable_flag _x1 ;o#expression _x2 ;o#tag_info _x3 end
94+
|Caml_block_tag ( _x0) ->
95+
begin o#expression _x0 end
96+
|Number ( _x0) ->
97+
begin o#number _x0 end
98+
|Object ( _x0) ->
99+
begin o#property_map _x0 end
100+
|Undefined -> ()
101+
|Null -> ()
102+
method for_ident_expression : for_ident_expression -> unit = o#expression
103+
method finish_ident_expression : finish_ident_expression -> unit = o#expression
104+
method statement_desc : statement_desc -> unit = function
105+
| Block ( _x0) ->
106+
begin o#block _x0 end
107+
|Variable ( _x0) ->
108+
begin o#variable_declaration _x0 end
109+
|Exp ( _x0) ->
110+
begin o#expression _x0 end
111+
|If ( _x0,_x1,_x2) ->
112+
begin o#expression _x0 ;o#block _x1 ;o#block _x2 end
113+
|While ( _x0,_x1,_x2,_x3) ->
114+
begin o#option (fun o -> o#label) _x0 ;o#expression _x1 ;o#block _x2 ;o#unknown _x3 end
115+
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
116+
begin o#option (fun o -> o#for_ident_expression) _x0 ;o#finish_ident_expression _x1 ;o#for_ident _x2 ;o#for_direction _x3 ;o#block _x4 ;o#unknown _x5 end
117+
|Continue ( _x0) ->
118+
begin o#label _x0 end
119+
|Break -> ()
120+
|Return ( _x0) ->
121+
begin o#expression _x0 end
122+
|Int_switch ( _x0,_x1,_x2) ->
123+
begin o#expression _x0 ;o#list (fun o -> o#int_clause) _x1 ;o#option (fun o -> o#block) _x2 end
124+
|String_switch ( _x0,_x1,_x2) ->
125+
begin o#expression _x0 ;o#list (fun o -> o#string_clause) _x1 ;o#option (fun o -> o#block) _x2 end
126+
|Throw ( _x0) ->
127+
begin o#expression _x0 end
128+
|Try ( _x0,_x1,_x2) ->
129+
begin o#block _x0 ;o#option (fun o -> fun ( _x0,_x1) -> begin o#exception_ident _x0;o#block _x1 end) _x1 ;o#option (fun o -> o#block) _x2 end
130+
|Debugger -> ()
131+
method expression : expression -> unit = fun { expression_desc = _x0;comment = _x1} -> begin o#expression_desc _x0;o#option (fun o -> o#string) _x1 end
132+
method statement : statement -> unit = fun { statement_desc = _x0;comment = _x1} -> begin o#statement_desc _x0;o#option (fun o -> o#string) _x1 end
133+
method variable_declaration : variable_declaration -> unit = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> begin o#ident _x0;o#option (fun o -> o#expression) _x1;o#property _x2;o#ident_info _x3 end
134+
method string_clause : string_clause -> unit = fun ( _x0,_x1) -> begin o#string _x0;o#case_clause _x1 end
135+
method int_clause : int_clause -> unit = fun ( _x0,_x1) -> begin o#int _x0;o#case_clause _x1 end
136+
method case_clause : case_clause -> unit = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> begin o#block _x0;o#bool _x1;o#option (fun o -> o#string) _x2 end
137+
method block : block -> unit = o#list (fun o -> o#statement)
138+
method program : program -> unit = fun { block = _x0;exports = _x1;export_set = _x2} -> begin o#block _x0;o#exports _x1;o#unknown _x2 end
139+
method deps_program : deps_program -> unit = fun { program = _x0;modules = _x1;side_effect = _x2} -> begin o#program _x0;o#required_modules _x1;o#option (fun o -> o#string) _x2 end
140+
end
141+

Diff for: lib/4.06.1/unstable/js_compiler.ml

+152-8
Original file line numberDiff line numberDiff line change
@@ -86740,6 +86740,151 @@ let o = o#required_modules _x1 in
8674086740
let o = o#option (fun o -> o#string) _x2 in o
8674186741
end
8674286742

86743+
end
86744+
module Js_iter
86745+
= struct
86746+
#1 "js_iter.ml"
86747+
86748+
open J
86749+
class virtual iter =
86750+
object ((o : 'self_type))
86751+
method unknown : 'a. 'a -> unit = ignore
86752+
method string : string -> unit = ignore
86753+
method option :
86754+
'a. ('self_type -> 'a -> unit) -> 'a option -> unit =
86755+
fun _f_a -> function | None -> () | Some _x -> _f_a o _x
86756+
method list :
86757+
'a. ('self_type -> 'a -> unit) -> 'a list -> unit =
86758+
fun _f_a ->
86759+
function
86760+
| [] -> ()
86761+
| _x :: _x_i1 -> _f_a o _x ; o#list _f_a _x_i1
86762+
method int32 : int32 -> unit = ignore
86763+
method int : int -> unit = ignore
86764+
method bool : bool -> unit = ignore
86765+
method label : label -> unit = o#string
86766+
method binop : binop -> unit = o#unknown
86767+
method int_op : int_op -> unit = o#unknown
86768+
method kind : kind -> unit = o#unknown
86769+
method property : property -> unit = o#unknown
86770+
method number : number -> unit = o#unknown
86771+
method mutable_flag : mutable_flag -> unit = o#unknown
86772+
method ident_info : ident_info -> unit = o#unknown
86773+
method exports : exports -> unit = o#unknown
86774+
method tag_info : tag_info -> unit = o#unknown
86775+
method required_modules : required_modules -> unit = o#list (fun o -> o#module_id)
86776+
method property_name : property_name -> unit = o#unknown
86777+
method ident : ident -> unit = o#unknown
86778+
method module_id : module_id -> unit = fun { id = _x0;kind = _x1} -> begin o#ident _x0;o#unknown _x1 end
86779+
method vident : vident -> unit = function
86780+
| Id ( _x0) ->
86781+
begin o#ident _x0 end
86782+
|Qualified ( _x0,_x1) ->
86783+
begin o#module_id _x0 ;o#option (fun o -> o#string) _x1 end
86784+
method exception_ident : exception_ident -> unit = o#ident
86785+
method for_ident : for_ident -> unit = o#ident
86786+
method for_direction : for_direction -> unit = o#unknown
86787+
method property_map : property_map -> unit = o#list (fun o -> fun ( _x0,_x1) -> begin o#property_name _x0;o#expression _x1 end)
86788+
method length_object : length_object -> unit = o#unknown
86789+
method expression_desc : expression_desc -> unit = function
86790+
| Length ( _x0,_x1) ->
86791+
begin o#expression _x0 ;o#length_object _x1 end
86792+
|Char_of_int ( _x0) ->
86793+
begin o#expression _x0 end
86794+
|Char_to_int ( _x0) ->
86795+
begin o#expression _x0 end
86796+
|Is_null_or_undefined ( _x0) ->
86797+
begin o#expression _x0 end
86798+
|String_append ( _x0,_x1) ->
86799+
begin o#expression _x0 ;o#expression _x1 end
86800+
|Bool ( _x0) ->
86801+
begin o#bool _x0 end
86802+
|Typeof ( _x0) ->
86803+
begin o#expression _x0 end
86804+
|Js_not ( _x0) ->
86805+
begin o#expression _x0 end
86806+
|Seq ( _x0,_x1) ->
86807+
begin o#expression _x0 ;o#expression _x1 end
86808+
|Cond ( _x0,_x1,_x2) ->
86809+
begin o#expression _x0 ;o#expression _x1 ;o#expression _x2 end
86810+
|Bin ( _x0,_x1,_x2) ->
86811+
begin o#binop _x0 ;o#expression _x1 ;o#expression _x2 end
86812+
|FlatCall ( _x0,_x1) ->
86813+
begin o#expression _x0 ;o#expression _x1 end
86814+
|Call ( _x0,_x1,_x2) ->
86815+
begin o#expression _x0 ;o#list (fun o -> o#expression) _x1 ;o#unknown _x2 end
86816+
|String_index ( _x0,_x1) ->
86817+
begin o#expression _x0 ;o#expression _x1 end
86818+
|Array_index ( _x0,_x1) ->
86819+
begin o#expression _x0 ;o#expression _x1 end
86820+
|Static_index ( _x0,_x1,_x2) ->
86821+
begin o#expression _x0 ;o#string _x1 ;o#option (fun o -> o#int32) _x2 end
86822+
|New ( _x0,_x1) ->
86823+
begin o#expression _x0 ;o#option (fun o -> o#list (fun o -> o#expression)) _x1 end
86824+
|Var ( _x0) ->
86825+
begin o#vident _x0 end
86826+
|Fun ( _x0,_x1,_x2,_x3) ->
86827+
begin o#bool _x0 ;o#list (fun o -> o#ident) _x1 ;o#block _x2 ;o#unknown _x3 end
86828+
|Str ( _x0,_x1) ->
86829+
begin o#bool _x0 ;o#string _x1 end
86830+
|Unicode ( _x0) ->
86831+
begin o#string _x0 end
86832+
|Raw_js_code ( _x0) ->
86833+
begin o#unknown _x0 end
86834+
|Array ( _x0,_x1) ->
86835+
begin o#list (fun o -> o#expression) _x0 ;o#mutable_flag _x1 end
86836+
|Optional_block ( _x0,_x1) ->
86837+
begin o#expression _x0 ;o#bool _x1 end
86838+
|Caml_block ( _x0,_x1,_x2,_x3) ->
86839+
begin o#list (fun o -> o#expression) _x0 ;o#mutable_flag _x1 ;o#expression _x2 ;o#tag_info _x3 end
86840+
|Caml_block_tag ( _x0) ->
86841+
begin o#expression _x0 end
86842+
|Number ( _x0) ->
86843+
begin o#number _x0 end
86844+
|Object ( _x0) ->
86845+
begin o#property_map _x0 end
86846+
|Undefined -> ()
86847+
|Null -> ()
86848+
method for_ident_expression : for_ident_expression -> unit = o#expression
86849+
method finish_ident_expression : finish_ident_expression -> unit = o#expression
86850+
method statement_desc : statement_desc -> unit = function
86851+
| Block ( _x0) ->
86852+
begin o#block _x0 end
86853+
|Variable ( _x0) ->
86854+
begin o#variable_declaration _x0 end
86855+
|Exp ( _x0) ->
86856+
begin o#expression _x0 end
86857+
|If ( _x0,_x1,_x2) ->
86858+
begin o#expression _x0 ;o#block _x1 ;o#block _x2 end
86859+
|While ( _x0,_x1,_x2,_x3) ->
86860+
begin o#option (fun o -> o#label) _x0 ;o#expression _x1 ;o#block _x2 ;o#unknown _x3 end
86861+
|ForRange ( _x0,_x1,_x2,_x3,_x4,_x5) ->
86862+
begin o#option (fun o -> o#for_ident_expression) _x0 ;o#finish_ident_expression _x1 ;o#for_ident _x2 ;o#for_direction _x3 ;o#block _x4 ;o#unknown _x5 end
86863+
|Continue ( _x0) ->
86864+
begin o#label _x0 end
86865+
|Break -> ()
86866+
|Return ( _x0) ->
86867+
begin o#expression _x0 end
86868+
|Int_switch ( _x0,_x1,_x2) ->
86869+
begin o#expression _x0 ;o#list (fun o -> o#int_clause) _x1 ;o#option (fun o -> o#block) _x2 end
86870+
|String_switch ( _x0,_x1,_x2) ->
86871+
begin o#expression _x0 ;o#list (fun o -> o#string_clause) _x1 ;o#option (fun o -> o#block) _x2 end
86872+
|Throw ( _x0) ->
86873+
begin o#expression _x0 end
86874+
|Try ( _x0,_x1,_x2) ->
86875+
begin o#block _x0 ;o#option (fun o -> fun ( _x0,_x1) -> begin o#exception_ident _x0;o#block _x1 end) _x1 ;o#option (fun o -> o#block) _x2 end
86876+
|Debugger -> ()
86877+
method expression : expression -> unit = fun { expression_desc = _x0;comment = _x1} -> begin o#expression_desc _x0;o#option (fun o -> o#string) _x1 end
86878+
method statement : statement -> unit = fun { statement_desc = _x0;comment = _x1} -> begin o#statement_desc _x0;o#option (fun o -> o#string) _x1 end
86879+
method variable_declaration : variable_declaration -> unit = fun { ident = _x0;value = _x1;property = _x2;ident_info = _x3} -> begin o#ident _x0;o#option (fun o -> o#expression) _x1;o#property _x2;o#ident_info _x3 end
86880+
method string_clause : string_clause -> unit = fun ( _x0,_x1) -> begin o#string _x0;o#case_clause _x1 end
86881+
method int_clause : int_clause -> unit = fun ( _x0,_x1) -> begin o#int _x0;o#case_clause _x1 end
86882+
method case_clause : case_clause -> unit = fun { switch_body = _x0;should_break = _x1;comment = _x2} -> begin o#block _x0;o#bool _x1;o#option (fun o -> o#string) _x2 end
86883+
method block : block -> unit = o#list (fun o -> o#statement)
86884+
method program : program -> unit = fun { block = _x0;exports = _x1;export_set = _x2} -> begin o#block _x0;o#exports _x1;o#unknown _x2 end
86885+
method deps_program : deps_program -> unit = fun { program = _x0;modules = _x1;side_effect = _x2} -> begin o#program _x0;o#required_modules _x1;o#option (fun o -> o#string) _x2 end
86886+
end
86887+
8674386888
end
8674486889
module Js_op_util : sig
8674586890
#1 "js_op_util.mli"
@@ -87083,32 +87228,31 @@ let add_defined_idents (x : idents_stats) ident =
8708387228
Note such shaking is done in the toplevel, so that it requires us to
8708487229
flatten the statement first
8708587230
*)
87086-
let free_variables (stats : idents_stats) : Js_fold.fold =
87231+
let free_variables (stats : idents_stats) : Js_iter.iter =
8708787232
object (self)
87088-
inherit Js_fold.fold as super
87233+
inherit Js_iter.iter as super
8708987234
method! variable_declaration st =
8709087235
add_defined_idents stats st.ident;
8709187236
match st.value with
8709287237
| None
87093-
-> self
87238+
-> ()
8709487239
| Some v
8709587240
->
8709687241
self # expression v
8709787242
method! ident id =
8709887243
(if not (Set_ident.mem stats.defined_idents id )then
87099-
stats.used_idents <- Set_ident.add stats.used_idents id);
87100-
self
87101-
method! expression exp =
87244+
stats.used_idents <- Set_ident.add stats.used_idents id)
8710287245

87246+
method! expression exp =
8710387247
match exp.expression_desc with
8710487248
| Fun(_, _,_, env)
8710587249
(** a optimization to avoid walking into funciton again
8710687250
if it's already comuted
8710787251
*)
8710887252
->
8710987253
stats.used_idents <-
87110-
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
87111-
self
87254+
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents
87255+
8711287256

8711387257
| _
8711487258
->

Diff for: lib/4.06.1/unstable/js_compiler.ml.d

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_fold_basic.mli
181181
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_fun_env.ml
182182
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_fun_env.mli
183+
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_iter.ml
183184
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_long.ml
184185
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_long.mli
185186
../lib/4.06.1/unstable/js_compiler.ml: ./core/js_map.ml

0 commit comments

Comments
 (0)