Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split bs.deriving into a seperate module #2655

Merged
merged 1 commit into from Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jscomp/Makefile
Expand Up @@ -255,6 +255,7 @@ SYNTAX_SRCS= \
external_ffi_types\
external_process\
ast_util\
ast_tdcls\
ast_exp_apply\
ast_exp_extension\
ast_core_type_class_type\
Expand Down
22 changes: 13 additions & 9 deletions jscomp/all.depend
Expand Up @@ -239,6 +239,10 @@ syntax/ast_util.cmx : ext/literals.cmx syntax/external_process.cmx \
syntax/ast_literal.cmx syntax/ast_external_mk.cmx syntax/ast_exp.cmx \
syntax/ast_core_type.cmx syntax/ast_comb.cmx syntax/ast_attributes.cmx \
syntax/ast_util.cmi
syntax/ast_tdcls.cmx : ext/ext_list.cmx syntax/bs_ast_mapper.cmx \
syntax/ast_structure.cmx syntax/ast_signature.cmx syntax/ast_payload.cmx \
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
syntax/ast_attributes.cmx syntax/ast_tdcls.cmi
syntax/ast_exp_apply.cmx : ext/literals.cmx ext/ext_list.cmx \
syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
syntax/ast_literal.cmx syntax/ast_attributes.cmx syntax/ast_exp_apply.cmi
Expand All @@ -253,16 +257,14 @@ syntax/ast_core_type_class_type.cmx : ext/literals.cmx ext/ext_ref.cmx \
syntax/ast_tuple_pattern_flatten.cmx : ext/ext_list.cmx \
syntax/bs_ast_mapper.cmx syntax/ast_tuple_pattern_flatten.cmi
syntax/ppx_entry.cmx : ext/string_map.cmx ext/literals.cmx \
syntax/external_process.cmx ext/ext_string.cmx ext/ext_list.cmx \
syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
syntax/external_process.cmx ext/ext_string.cmx syntax/bs_ast_mapper.cmx \
syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
syntax/ast_utf8_string_interp.cmx syntax/ast_utf8_string.cmx \
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_structure.cmx \
syntax/ast_signature.cmx syntax/ast_payload.cmx \
syntax/ast_exp_extension.cmx syntax/ast_exp_apply.cmx \
syntax/ast_derive_projector.cmx syntax/ast_derive_js_mapper.cmx \
syntax/ast_derive_abstract.cmx syntax/ast_derive.cmx \
syntax/ast_core_type_class_type.cmx syntax/ast_attributes.cmx \
syntax/ppx_entry.cmi
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_tdcls.cmx \
syntax/ast_payload.cmx syntax/ast_exp_extension.cmx \
syntax/ast_exp_apply.cmx syntax/ast_derive_projector.cmx \
syntax/ast_derive_js_mapper.cmx syntax/ast_core_type_class_type.cmx \
syntax/ast_attributes.cmx syntax/ppx_entry.cmi
syntax/bs_syntaxerr.cmi :
syntax/ast_utf8_string.cmi :
syntax/ast_utf8_string_interp.cmi :
Expand Down Expand Up @@ -293,6 +295,8 @@ syntax/external_ffi_types.cmi : syntax/external_arg_spec.cmi
syntax/external_process.cmi : common/bs_loc.cmi syntax/ast_core_type.cmi \
syntax/ast_attributes.cmi
syntax/ast_util.cmi : syntax/bs_ast_mapper.cmi syntax/ast_payload.cmi
syntax/ast_tdcls.cmi : syntax/bs_ast_mapper.cmi syntax/ast_structure.cmi \
syntax/ast_signature.cmi
syntax/ast_exp_apply.cmi : syntax/bs_ast_mapper.cmi
syntax/ast_exp_extension.cmi : syntax/bs_ast_mapper.cmi
syntax/ast_core_type_class_type.cmi : syntax/bs_ast_mapper.cmi
Expand Down
134 changes: 134 additions & 0 deletions jscomp/syntax/ast_tdcls.ml
@@ -0,0 +1,134 @@
(* Copyright (C) 2018 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

open Ast_helper


let newTdcls
(tdcls : Parsetree.type_declaration list)
(newAttrs : Parsetree.attributes)
: Parsetree.type_declaration list
=
match tdcls with
| [ x ] ->
[{ x with Parsetree.ptype_attributes = newAttrs}]
| _ ->
Ext_list.map_last
(fun last x ->
if last then
{ x with
Parsetree.ptype_attributes = newAttrs}
else x )
tdcls


let handleTdclsInSigi
(self : Bs_ast_mapper.mapper)
(sigi : Parsetree.signature_item)
(tdcls : Parsetree.type_declaration list)
: Ast_signature.item =
begin match Ast_attributes.process_derive_type
(Ext_list.last tdcls).ptype_attributes with
| {bs_deriving = Some actions; explict_nonrec}, newAttrs
->
let loc = sigi.psig_loc in
let newTdcls = newTdcls tdcls newAttrs in
let newSigi =
self.signature_item self {sigi with psig_desc = Psig_type newTdcls} in
if Ast_payload.isAbstract actions then
let codes = Ast_derive_abstract.handleTdclsInSig newTdcls in
Ast_signature.fuseAll ~loc
(
Sig.include_ ~loc
(Incl.mk ~loc
(Mty.typeof_ ~loc
(Mod.constraint_ ~loc
(Mod.structure ~loc [
{ pstr_loc = loc;
pstr_desc =
Pstr_type
(match newSigi.psig_desc with
| Psig_type x -> x
| _ -> assert false)
}] )
(Mty.signature ~loc [])) ) )
::
self.signature self
codes
)
else
Ast_signature.fuseAll ~loc
(newSigi::
self.signature
self
(
Ast_derive.gen_signature tdcls actions explict_nonrec))
| {bs_deriving = None }, _ ->
Bs_ast_mapper.default_mapper.signature_item self sigi

end


let handleTdclsInStru
(self : Bs_ast_mapper.mapper)
(str : Parsetree.structure_item)
(tdcls : Parsetree.type_declaration list)
: Ast_structure.item =
begin match
Ast_attributes.process_derive_type
((Ext_list.last tdcls).ptype_attributes) with
| {bs_deriving = Some actions;
explict_nonrec
}, newAttrs ->
let loc = str.pstr_loc in
let tdcls2 = newTdcls tdcls newAttrs in
let newStr =
self.structure_item self
{str with pstr_desc = Pstr_type tdcls2} in
if Ast_payload.isAbstract actions then
let codes = Ast_derive_abstract.handleTdclsInStr tdcls2 in
(* use [tdcls2] avoid nonterminating *)
Ast_structure.fuseAll ~loc
(
Ast_structure.constraint_ ~loc
[newStr] []::
self.structure self
codes)
else
Ast_structure.fuseAll ~loc
(newStr ::
self.structure self
(
List.map
(fun action ->
Ast_derive.gen_structure_signature
loc
tdcls action explict_nonrec
) actions
))
| {bs_deriving = None }, _ ->
Bs_ast_mapper.default_mapper.structure_item self str
end


35 changes: 35 additions & 0 deletions jscomp/syntax/ast_tdcls.mli
@@ -0,0 +1,35 @@
(* Copyright (C) 2018 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


val handleTdclsInSigi :
Bs_ast_mapper.mapper ->
Parsetree.signature_item ->
Parsetree.type_declaration list -> Ast_signature.item


val handleTdclsInStru :
Bs_ast_mapper.mapper ->
Parsetree.structure_item ->
Parsetree.type_declaration list -> Ast_structure.item