Skip to content

Commit 067c6b4

Browse files
committed
split bs.deriving into a seperate module
1 parent 5c38c80 commit 067c6b4

File tree

11 files changed

+1848
-1460
lines changed

11 files changed

+1848
-1460
lines changed

jscomp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ SYNTAX_SRCS= \
255255
external_ffi_types\
256256
external_process\
257257
ast_util\
258+
ast_tdcls\
258259
ast_exp_apply\
259260
ast_exp_extension\
260261
ast_core_type_class_type\

jscomp/all.depend

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ syntax/ast_util.cmx : ext/literals.cmx syntax/external_process.cmx \
239239
syntax/ast_literal.cmx syntax/ast_external_mk.cmx syntax/ast_exp.cmx \
240240
syntax/ast_core_type.cmx syntax/ast_comb.cmx syntax/ast_attributes.cmx \
241241
syntax/ast_util.cmi
242+
syntax/ast_tdcls.cmx : ext/ext_list.cmx syntax/bs_ast_mapper.cmx \
243+
syntax/ast_structure.cmx syntax/ast_signature.cmx syntax/ast_payload.cmx \
244+
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
245+
syntax/ast_attributes.cmx syntax/ast_tdcls.cmi
242246
syntax/ast_exp_apply.cmx : ext/literals.cmx ext/ext_list.cmx \
243247
syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
244248
syntax/ast_literal.cmx syntax/ast_attributes.cmx syntax/ast_exp_apply.cmi
@@ -253,16 +257,14 @@ syntax/ast_core_type_class_type.cmx : ext/literals.cmx ext/ext_ref.cmx \
253257
syntax/ast_tuple_pattern_flatten.cmx : ext/ext_list.cmx \
254258
syntax/bs_ast_mapper.cmx syntax/ast_tuple_pattern_flatten.cmi
255259
syntax/ppx_entry.cmx : ext/string_map.cmx ext/literals.cmx \
256-
syntax/external_process.cmx ext/ext_string.cmx ext/ext_list.cmx \
257-
syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
260+
syntax/external_process.cmx ext/ext_string.cmx syntax/bs_ast_mapper.cmx \
261+
syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
258262
syntax/ast_utf8_string_interp.cmx syntax/ast_utf8_string.cmx \
259-
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_structure.cmx \
260-
syntax/ast_signature.cmx syntax/ast_payload.cmx \
261-
syntax/ast_exp_extension.cmx syntax/ast_exp_apply.cmx \
262-
syntax/ast_derive_projector.cmx syntax/ast_derive_js_mapper.cmx \
263-
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
264-
syntax/ast_core_type_class_type.cmx syntax/ast_attributes.cmx \
265-
syntax/ppx_entry.cmi
263+
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_tdcls.cmx \
264+
syntax/ast_payload.cmx syntax/ast_exp_extension.cmx \
265+
syntax/ast_exp_apply.cmx syntax/ast_derive_projector.cmx \
266+
syntax/ast_derive_js_mapper.cmx syntax/ast_core_type_class_type.cmx \
267+
syntax/ast_attributes.cmx syntax/ppx_entry.cmi
266268
syntax/bs_syntaxerr.cmi :
267269
syntax/ast_utf8_string.cmi :
268270
syntax/ast_utf8_string_interp.cmi :
@@ -293,6 +295,8 @@ syntax/external_ffi_types.cmi : syntax/external_arg_spec.cmi
293295
syntax/external_process.cmi : common/bs_loc.cmi syntax/ast_core_type.cmi \
294296
syntax/ast_attributes.cmi
295297
syntax/ast_util.cmi : syntax/bs_ast_mapper.cmi syntax/ast_payload.cmi
298+
syntax/ast_tdcls.cmi : syntax/bs_ast_mapper.cmi syntax/ast_structure.cmi \
299+
syntax/ast_signature.cmi
296300
syntax/ast_exp_apply.cmi : syntax/bs_ast_mapper.cmi
297301
syntax/ast_exp_extension.cmi : syntax/bs_ast_mapper.cmi
298302
syntax/ast_core_type_class_type.cmi : syntax/bs_ast_mapper.cmi

jscomp/syntax/ast_tdcls.ml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
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+
open Ast_helper
26+
27+
28+
let newTdcls
29+
(tdcls : Parsetree.type_declaration list)
30+
(newAttrs : Parsetree.attributes)
31+
: Parsetree.type_declaration list
32+
=
33+
match tdcls with
34+
| [ x ] ->
35+
[{ x with Parsetree.ptype_attributes = newAttrs}]
36+
| _ ->
37+
Ext_list.map_last
38+
(fun last x ->
39+
if last then
40+
{ x with
41+
Parsetree.ptype_attributes = newAttrs}
42+
else x )
43+
tdcls
44+
45+
46+
let handleTdclsInSigi
47+
(self : Bs_ast_mapper.mapper)
48+
(sigi : Parsetree.signature_item)
49+
(tdcls : Parsetree.type_declaration list)
50+
: Ast_signature.item =
51+
begin match Ast_attributes.process_derive_type
52+
(Ext_list.last tdcls).ptype_attributes with
53+
| {bs_deriving = Some actions; explict_nonrec}, newAttrs
54+
->
55+
let loc = sigi.psig_loc in
56+
let newTdcls = newTdcls tdcls newAttrs in
57+
let newSigi =
58+
self.signature_item self {sigi with psig_desc = Psig_type newTdcls} in
59+
if Ast_payload.isAbstract actions then
60+
let codes = Ast_derive_abstract.handleTdclsInSig newTdcls in
61+
Ast_signature.fuseAll ~loc
62+
(
63+
Sig.include_ ~loc
64+
(Incl.mk ~loc
65+
(Mty.typeof_ ~loc
66+
(Mod.constraint_ ~loc
67+
(Mod.structure ~loc [
68+
{ pstr_loc = loc;
69+
pstr_desc =
70+
Pstr_type
71+
(match newSigi.psig_desc with
72+
| Psig_type x -> x
73+
| _ -> assert false)
74+
}] )
75+
(Mty.signature ~loc [])) ) )
76+
::
77+
self.signature self
78+
codes
79+
)
80+
else
81+
Ast_signature.fuseAll ~loc
82+
(newSigi::
83+
self.signature
84+
self
85+
(
86+
Ast_derive.gen_signature tdcls actions explict_nonrec))
87+
| {bs_deriving = None }, _ ->
88+
Bs_ast_mapper.default_mapper.signature_item self sigi
89+
90+
end
91+
92+
93+
let handleTdclsInStru
94+
(self : Bs_ast_mapper.mapper)
95+
(str : Parsetree.structure_item)
96+
(tdcls : Parsetree.type_declaration list)
97+
: Ast_structure.item =
98+
begin match
99+
Ast_attributes.process_derive_type
100+
((Ext_list.last tdcls).ptype_attributes) with
101+
| {bs_deriving = Some actions;
102+
explict_nonrec
103+
}, newAttrs ->
104+
let loc = str.pstr_loc in
105+
let tdcls2 = newTdcls tdcls newAttrs in
106+
let newStr =
107+
self.structure_item self
108+
{str with pstr_desc = Pstr_type tdcls2} in
109+
if Ast_payload.isAbstract actions then
110+
let codes = Ast_derive_abstract.handleTdclsInStr tdcls2 in
111+
(* use [tdcls2] avoid nonterminating *)
112+
Ast_structure.fuseAll ~loc
113+
(
114+
Ast_structure.constraint_ ~loc
115+
[newStr] []::
116+
self.structure self
117+
codes)
118+
else
119+
Ast_structure.fuseAll ~loc
120+
(newStr ::
121+
self.structure self
122+
(
123+
List.map
124+
(fun action ->
125+
Ast_derive.gen_structure_signature
126+
loc
127+
tdcls action explict_nonrec
128+
) actions
129+
))
130+
| {bs_deriving = None }, _ ->
131+
Bs_ast_mapper.default_mapper.structure_item self str
132+
end
133+
134+

jscomp/syntax/ast_tdcls.mli

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(* Copyright (C) 2018 Authors of BuckleScript
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+
val handleTdclsInSigi :
27+
Bs_ast_mapper.mapper ->
28+
Parsetree.signature_item ->
29+
Parsetree.type_declaration list -> Ast_signature.item
30+
31+
32+
val handleTdclsInStru :
33+
Bs_ast_mapper.mapper ->
34+
Parsetree.structure_item ->
35+
Parsetree.type_declaration list -> Ast_structure.item

0 commit comments

Comments
 (0)