Skip to content

Commit

Permalink
Merge 2f62301 into 6ac6d63
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Mar 13, 2018
2 parents 6ac6d63 + 2f62301 commit c0d4846
Show file tree
Hide file tree
Showing 13 changed files with 16,492 additions and 15,855 deletions.
2 changes: 2 additions & 0 deletions jscomp/Makefile
Expand Up @@ -255,6 +255,8 @@ SYNTAX_SRCS= \
external_ffi_types\
external_process\
ast_util\
ast_core_type_class_type\
ast_tuple_pattern_flatten\
ppx_entry\
# not a good name ast_util
SYNTAX_CMXS=$(addprefix syntax/, $(addsuffix .cmx, $(SYNTAX_SRCS)))
Expand Down
17 changes: 13 additions & 4 deletions jscomp/all.depend
Expand Up @@ -239,14 +239,21 @@ 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_core_type_class_type.cmx : ext/literals.cmx ext/ext_ref.cmx \
ext/ext_list.cmx syntax/bs_ast_mapper.cmx syntax/ast_util.cmx \
syntax/ast_literal.cmx syntax/ast_comb.cmx syntax/ast_attributes.cmx \
syntax/ast_core_type_class_type.cmi
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_ref.cmx \
ext/ext_list.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_structure.cmx \
syntax/ast_signature.cmx syntax/ast_payload.cmx syntax/ast_literal.cmx \
syntax/ast_derive_projector.cmx syntax/ast_derive_js_mapper.cmx \
syntax/ast_derive_abstract.cmx syntax/ast_derive.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_literal.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_core_type.cmx syntax/ast_comb.cmx syntax/ast_attributes.cmx \
syntax/ppx_entry.cmi
syntax/bs_syntaxerr.cmi :
Expand Down Expand Up @@ -279,6 +286,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_core_type_class_type.cmi : syntax/bs_ast_mapper.cmi
syntax/ast_tuple_pattern_flatten.cmi : syntax/bs_ast_mapper.cmi
syntax/ppx_entry.cmi :
depends/bs_exception.cmi :
depends/ast_extract.cmi : ext/string_map.cmi ext/string_hashtbl.cmi \
Expand Down
217 changes: 217 additions & 0 deletions jscomp/syntax/ast_core_type_class_type.ml
@@ -0,0 +1,217 @@
(* 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 process_getter_setter ~no ~get ~set
loc name
(attrs : Ast_attributes.t)
(ty : Parsetree.core_type) acc =
match Ast_attributes.process_method_attributes_rev attrs with
| {get = None; set = None}, _ -> no ty :: acc
| st , pctf_attributes
->
let get_acc =
match st.set with
| Some `No_get -> acc
| None
| Some `Get ->
let lift txt =
Typ.constr ~loc {txt ; loc} [ty] in
let (null,undefined) =
match st with
| {get = Some (null, undefined) } -> (null, undefined)
| {get = None} -> (false, false ) in
let ty =
match (null,undefined) with
| false, false -> ty
| true, false -> lift Ast_literal.Lid.js_null
| false, true -> lift Ast_literal.Lid.js_undefined
| true , true -> lift Ast_literal.Lid.js_null_undefined in
get ty name pctf_attributes
:: acc
in
if st.set = None then get_acc
else
set ty (name ^ Literals.setter_suffix) pctf_attributes
:: get_acc


let handle_class_type_field self
({pctf_loc = loc } as ctf : Parsetree.class_type_field)
acc =
match ctf.pctf_desc with
| Pctf_method
(name, private_flag, virtual_flag, ty)
->
let no (ty : Parsetree.core_type) =
let ty =
match ty.ptyp_desc with
| Ptyp_arrow (label, args, body)
->
Ast_util.to_method_type
ty.ptyp_loc self label args body

| Ptyp_poly (strs, {ptyp_desc = Ptyp_arrow (label, args, body);
ptyp_loc})
->
{ty with ptyp_desc =
Ptyp_poly(strs,
Ast_util.to_method_type
ptyp_loc self label args body )}
| _ ->
self.typ self ty
in
{ctf with
pctf_desc =
Pctf_method (name , private_flag, virtual_flag, ty)}
in
let get ty name pctf_attributes =
{ctf with
pctf_desc =
Pctf_method (name ,
private_flag,
virtual_flag,
self.typ self ty
);
pctf_attributes} in
let set ty name pctf_attributes =
{ctf with
pctf_desc =
Pctf_method (name,
private_flag,
virtual_flag,
Ast_util.to_method_type
loc self "" ty
(Ast_literal.type_unit ~loc ())
);
pctf_attributes} in
process_getter_setter ~no ~get ~set loc name ctf.pctf_attributes ty acc

| Pctf_inherit _
| Pctf_val _
| Pctf_constraint _
| Pctf_attribute _
| Pctf_extension _ ->
Bs_ast_mapper.default_mapper.class_type_field self ctf :: acc


(*
Attributes are very hard to attribute
(since ptyp_attributes could happen in so many places),
and write ppx extensions correctly,
we can only use it locally
*)

let handle_core_type
~(super : Bs_ast_mapper.mapper)
~(self : Bs_ast_mapper.mapper)
(ty : Parsetree.core_type)
record_as_js_object
=
match ty with
| {ptyp_desc = Ptyp_extension({txt = ("bs.obj"|"obj")}, PTyp ty)}
->
Ext_ref.non_exn_protect record_as_js_object true
(fun _ -> self.typ self ty )
| {ptyp_attributes ;
ptyp_desc = Ptyp_arrow (label, args, body);
(* let it go without regard label names,
it will report error later when the label is not empty
*)
ptyp_loc = loc
} ->
begin match Ast_attributes.process_attributes_rev ptyp_attributes with
| `Uncurry , ptyp_attributes ->
Ast_util.to_uncurry_type loc self label args body
| `Meth_callback, ptyp_attributes ->
Ast_util.to_method_callback_type loc self label args body
| `Method, ptyp_attributes ->
Ast_util.to_method_type loc self label args body
| `Nothing , _ ->
Bs_ast_mapper.default_mapper.typ self ty
end
| {
ptyp_desc = Ptyp_object ( methods, closed_flag) ;
ptyp_loc = loc
} ->
let (+>) attr (typ : Parsetree.core_type) =
{typ with ptyp_attributes = attr :: typ.ptyp_attributes} in
let new_methods =
Ext_list.fold_right (fun (label, ptyp_attrs, core_type) acc ->
let get ty name attrs =
let attrs, core_type =
match Ast_attributes.process_attributes_rev attrs with
| `Nothing, attrs -> attrs, ty (* #1678 *)
| `Uncurry, attrs ->
attrs, Ast_attributes.bs +> ty
| `Method, _
-> Location.raise_errorf ~loc "bs.get/set conflicts with bs.meth"
| `Meth_callback, attrs ->
attrs, Ast_attributes.bs_this +> ty
in
name , attrs, self.typ self core_type in
let set ty name attrs =
let attrs, core_type =
match Ast_attributes.process_attributes_rev attrs with
| `Nothing, attrs -> attrs, ty
| `Uncurry, attrs ->
attrs, Ast_attributes.bs +> ty
| `Method, _
-> Location.raise_errorf ~loc "bs.get/set conflicts with bs.meth"
| `Meth_callback, attrs ->
attrs, Ast_attributes.bs_this +> ty
in
name, attrs, Ast_util.to_method_type loc self "" core_type
(Ast_literal.type_unit ~loc ()) in
let no ty =
let attrs, core_type =
match Ast_attributes.process_attributes_rev ptyp_attrs with
| `Nothing, attrs -> attrs, ty
| `Uncurry, attrs ->
attrs, Ast_attributes.bs +> ty
| `Method, attrs ->
attrs, Ast_attributes.bs_method +> ty
| `Meth_callback, attrs ->
attrs, Ast_attributes.bs_this +> ty in
label, attrs, self.typ self core_type in
process_getter_setter ~no ~get ~set
loc label ptyp_attrs core_type acc
) methods [] in
let inner_type =
{ ty
with ptyp_desc = Ptyp_object(new_methods, closed_flag);
} in
if !record_as_js_object then
Ast_comb.to_js_type loc inner_type
else inner_type
| _ -> super.typ self ty

let handle_class_type_fields self fields =
Ext_list.fold_right
(handle_class_type_field self)
fields []

let handle_core_type self typ record_as_js_object =
handle_core_type
~super:Bs_ast_mapper.default_mapper
~self typ record_as_js_object
36 changes: 36 additions & 0 deletions jscomp/syntax/ast_core_type_class_type.mli
@@ -0,0 +1,36 @@
(* 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 handle_class_type_fields :
Bs_ast_mapper.mapper ->
Parsetree.class_type_field list ->
Parsetree.class_type_field list

val handle_core_type :
Bs_ast_mapper.mapper ->
Parsetree.core_type ->
bool ref ->
Parsetree.core_type

0 comments on commit c0d4846

Please sign in to comment.