Skip to content

Commit a9a9076

Browse files
author
Hongbo Zhang
committed
[refactoring] clean up arity which does not depend on type info anymore
1 parent 5aa9826 commit a9a9076

10 files changed

+132
-66
lines changed

jscomp/js_dump.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ and
643643
pp_string f (* ~utf:(kind = `Utf8) *) ~quote s; cxt
644644
| Raw_js_code (s,info) ->
645645
begin match info with
646-
| Exp _ ->
646+
| Exp ->
647647
P.string f "(";
648648
P.string f s ;
649649
P.string f ")";
@@ -1147,7 +1147,7 @@ and statement_desc top cxt f (s : J.statement_desc) : Ext_pp_scope.t =
11471147
match e.expression_desc with
11481148
| Call ({expression_desc = Fun _; },_,_) -> true
11491149
| Caml_uninitialized_obj _
1150-
| Raw_js_code (_, Exp _)
1150+
| Raw_js_code (_, Exp)
11511151
| Fun _ | Object _ -> true
11521152
| Raw_js_code (_,Stmt)
11531153
| Caml_block_set_tag _

jscomp/js_op.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ type length_object =
247247
| Caml_block
248248

249249
type code_info =
250-
| Exp of int option
250+
| Exp (* of int option *)
251251
| Stmt
252252
(** TODO: define constant - for better constant folding *)
253253
(* type constant = *)

jscomp/lam_compile_external_call.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
module E = Js_exp_make
3333

34-
open Parsetree_util
34+
3535

3636
type external_module_name =
3737
| Single of string
@@ -141,7 +141,7 @@ let handle_attributes ({prim_attributes ; prim_name} as _prim : prim ) : Locati
141141
]}
142142
*)
143143
->
144-
begin match is_single_string pay_load with
144+
begin match Ast_payload.is_single_string pay_load with
145145
| Some name ->
146146
js_val := `Value name
147147
| None ->
@@ -152,7 +152,7 @@ let handle_attributes ({prim_attributes ; prim_name} as _prim : prim ) : Locati
152152
(* {[ [@@bs.val_of_module]]}
153153
*)
154154
->
155-
begin match is_single_string pay_load with
155+
begin match Ast_payload.is_single_string pay_load with
156156
| Some name ->
157157
js_val_of_module := `Value(Bind (name, prim_name))
158158
| None ->
@@ -164,19 +164,19 @@ let handle_attributes ({prim_attributes ; prim_name} as _prim : prim ) : Locati
164164

165165
|"bs.send"
166166
->
167-
begin match is_single_string pay_load with
167+
begin match Ast_payload.is_single_string pay_load with
168168
| Some name -> js_send := `Value name
169169
| None -> js_send := `Value _prim.prim_name
170170
end
171171
| "bs.set"
172172
->
173-
begin match is_single_string pay_load with
173+
begin match Ast_payload.is_single_string pay_load with
174174
| Some name -> js_set := `Value name
175175
| None -> js_set := `Value _prim.prim_name
176176
end
177177
| "bs.get"
178178
->
179-
begin match is_single_string pay_load with
179+
begin match Ast_payload.is_single_string pay_load with
180180
| Some name -> js_get := `Value name
181181
| None -> js_get := `Value _prim.prim_name
182182
end
@@ -186,20 +186,20 @@ let handle_attributes ({prim_attributes ; prim_name} as _prim : prim ) : Locati
186186
[@@bs.call "xx"] [@@bs.call]
187187
*)
188188
->
189-
begin match is_single_string pay_load with
189+
begin match Ast_payload.is_single_string pay_load with
190190
| Some name -> call_name := Some (x.loc, name)
191191
| None -> call_name := Some(x.loc, _prim.prim_name)
192192
end
193193
| "bs.module" ->
194-
begin match is_string_or_strings pay_load with
194+
begin match Ast_payload.is_string_or_strings pay_load with
195195
| `Single name -> external_module_name:= Some (Single name)
196196
| `Some [a;b] -> external_module_name := Some (Bind (a,b))
197197
| `Some _ -> ()
198198
| `None -> () (* should emit a warning instead *)
199199
end
200200

201201
| "bs.new" ->
202-
begin match is_single_string pay_load with
202+
begin match Ast_payload.is_single_string pay_load with
203203
| Some x -> js_new := Some x
204204
| None -> js_new := Some _prim.prim_name
205205
end

jscomp/lam_dispatch_primitive.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ let query (prim : Lam_compile_env.primitive_description)
940940
->
941941
begin match args with
942942
| [ { expression_desc = Str (_,s )}] ->
943-
E.raw_js_code (Exp (Parsetree_util.has_arity prim.prim_attributes)) s
943+
E.raw_js_code Exp s
944944
| _ ->
945945
Ext_log.err __LOC__
946946
"JS.unsafe_js_expr is applied to an non literal string in %s"

jscomp/lam_stats_util.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ let rec get_arity
9292

9393
end
9494
| Llet(_,_,_, l ) -> get_arity meta l
95-
| Lprim (Pccall {prim_name = "js_pure_expr"; prim_attributes},
96-
[Lconst (Const_base (Const_string (_str,_)))])
97-
->
98-
(* Ext_log.dwarn __LOC__ "called %s %d" str (List.length prim_attributes ); *)
99-
begin match Parsetree_util.has_arity prim_attributes with
100-
| Some arity ->
101-
(* Ext_log.dwarn __LOC__ "arity %d" arity; *)
102-
Determin(false, [arity, None], false)
103-
| None -> NA
104-
end
95+
(* | Lprim (Pccall {prim_name = "js_pure_expr"; prim_attributes}, *)
96+
(* [Lconst (Const_base (Const_string (_str,_)))]) *)
97+
(* -> *)
98+
(* (\* Ext_log.dwarn __LOC__ "called %s %d" str (List.length prim_attributes ); *\) *)
99+
(* begin match Parsetree_util.has_arity prim_attributes with *)
100+
(* | Some arity -> *)
101+
(* (\* Ext_log.dwarn __LOC__ "arity %d" arity; *\) *)
102+
(* Determin(false, [arity, None], false) *)
103+
(* | None -> NA *)
104+
(* end *)
105105
| Lprim (Pfield (n,_), [Lprim(Pgetglobal id,[])]) ->
106106
Lam_compile_env.find_and_add_if_not_exist (id, n) meta.env
107107
~not_found:(fun _ -> assert false)

jscomp/syntax/ast_payload.ml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t = Parsetree.payload
26+
27+
let is_single_string (x : t ) =
28+
match x with (** TODO also need detect empty phrase case *)
29+
| PStr [ {
30+
pstr_desc =
31+
Pstr_eval (
32+
{pexp_desc =
33+
Pexp_constant
34+
(Const_string (name,_));
35+
_},_);
36+
_}] -> Some name
37+
| _ -> None
38+
39+
40+
let is_string_or_strings (x : t) :
41+
[ `None | `Single of string | `Some of string list ] =
42+
let module M = struct exception Not_str end in
43+
match x with
44+
| PStr [ {pstr_desc =
45+
Pstr_eval (
46+
{pexp_desc =
47+
Pexp_apply
48+
({pexp_desc = Pexp_constant (Const_string (name,_)); _},
49+
args
50+
);
51+
_},_);
52+
_}] ->
53+
(try
54+
`Some (name :: (args |> List.map (fun (_label,e) ->
55+
match (e : Parsetree.expression) with
56+
| {pexp_desc = Pexp_constant (Const_string (name,_)); _} ->
57+
name
58+
| _ -> raise M.Not_str)))
59+
60+
with M.Not_str -> `None )
61+
| PStr [ {
62+
pstr_desc =
63+
Pstr_eval (
64+
{pexp_desc =
65+
Pexp_constant
66+
(Const_string (name,_));
67+
_},_);
68+
_}] -> `Single name
69+
| _ -> `None

jscomp/syntax/ast_payload.mli

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
27+
(** A utility module used when destructuring parsetree attributes, used for
28+
compiling FFI attributes and built-in ppx *)
29+
30+
type t = Parsetree.payload
31+
32+
val is_single_string : t -> string option
33+
34+
val is_string_or_strings :
35+
t -> [ `None | `Single of string | `Some of string list ]

jscomp/syntax/parsetree_util.ml

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ let is_single_string (x : Parsetree.payload ) =
4141
_}] -> Some name
4242
| _ -> None
4343

44-
let is_string_or_strings (x : Parsetree.payload ) : [ `None | `Single of string | `Some of string list ] =
44+
let is_string_or_strings (x : Parsetree.payload ) :
45+
[ `None | `Single of string | `Some of string list ] =
4546
let module M = struct exception Not_str end in
4647
match x with
4748
| PStr [ {pstr_desc =
@@ -74,40 +75,3 @@ let is_string_or_strings (x : Parsetree.payload ) : [ `None | `Single of string
7475
let lift_int ?loc ?attrs x =
7576
Ast_helper.Exp.constant ?loc ?attrs (Const_int x)
7677

77-
let has_arity (attrs : Parsetree.attributes) =
78-
Ext_list.find_opt (fun (attr : Parsetree.attribute) ->
79-
match attr with
80-
| {txt = "arity"; _ },
81-
PStr [ { pstr_desc = Pstr_eval
82-
( {pexp_desc = Pexp_constant (Const_int i)},_attr);
83-
_}]
84-
->
85-
if i >= 0 then
86-
Some i
87-
else None
88-
| _ -> None
89-
) attrs
90-
91-
92-
93-
let arity_from_core_type (x : Parsetree.core_type) =
94-
let rec aux acc (x : Parsetree.core_type) =
95-
match x.ptyp_desc with
96-
| Ptyp_arrow (_,_,r) ->
97-
(* 'a -> ('b -> ('c -> 'd )) *)
98-
aux (acc + 1) r
99-
| _ -> acc in
100-
aux 0 x
101-
102-
103-
104-
let attr_attribute_from_type (x : Parsetree.core_type) : Parsetree.attribute =
105-
let n = arity_from_core_type x in
106-
let loc = x.ptyp_loc in
107-
{txt = "arity"; loc},
108-
PStr ([ {pstr_desc =
109-
Pstr_eval (lift_int n,[]);
110-
pstr_loc = loc
111-
}])
112-
113-

jscomp/syntax/parsetree_util.mli

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
compiling FFI code
3434
*)
3535

36-
val is_single_string : Parsetree.payload -> string option
36+
(* val is_single_string : Parsetree.payload -> string option *)
3737

38-
val is_string_or_strings : Parsetree.payload -> [ `None | `Single of string | `Some of string list ]
38+
(* val is_string_or_strings : Parsetree.payload -> [ `None | `Single of string | `Some of string list ] *)
3939

40-
val has_arity : Parsetree.attributes -> int option
41-
42-
val attr_attribute_from_type : Parsetree.core_type -> Parsetree.attribute

jscomp/syntax/syntax.mllib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
parsetree_util
2+
ast_payload
23
ppx_entry

0 commit comments

Comments
 (0)