Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ast_polyvar.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* 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. *)openPpxlib(** Note this is okay with enums, for variants,
the underlying representation may change due to
unbox
*)letmap_constructor_declarations_into_ints(row_fields:Parsetree.constructor_declarationlist)=letmark=ref`nothinginlet_,acc=List.fold_left(fun(i,acc)rtag->letattrs=rtag.pcd_attributesinmatchAst_attributes.iter_process_bs_int_asattrswith|Somej->ifj<>ithenifi=0thenmark:=`offsetjelsemark:=`complex;(j+1,j::acc)|None->(i+1,i::acc))(0,[])row_fieldsinmatch!markwith|`nothing->`Offset0|`offsetj->`Offsetj|`complex->`New(List.revacc)letis_enumrow_fields=List.for_all(fun(x:Parsetree.row_field)->matchx.prf_descwithRtag(_label,true,[])->true|_->false)row_fieldsletis_enum_polyvar(ty:Parsetree.type_declaration)=matchty.ptype_manifestwith|Some{ptyp_desc=Ptyp_variant(row_fields,Closed,None);_}whenis_enumrow_fields->Somerow_fields|_->Noneletis_enum_constructors(constructors:Parsetree.constructor_declarationlist)=List.for_all(fun(x:Parsetree.constructor_declaration)->matchxwith|{pcd_args=Pcstr_tuple[](* Note the enum is encoded using [Pcstr_tuple []]*);_;}->true|_->false)constructorsletmap_row_fields_into_intsptyp_loc(row_fields:Parsetree.row_fieldlist)=let_,acc=List.fold_left(fun(i,acc)rtag->matchrtag.prf_descwith|Rtag({txt;_},true,[])->leti=matchAst_attributes.iter_process_bs_int_asrtag.prf_attributeswith|Somei->i|None->iin(i+1,(txt,i)::acc)|_->Error.err~loc:ptyp_locInvalid_mel_int_type)(0,[])row_fieldsinList.revacc(* It also check in-consistency of cases like
{[ [`a | `c of int ] ]} *)letmap_row_fields_into_stringsptyp_loc(row_fields:Parsetree.row_fieldlist):External_arg_spec.attr=lethas_bs_as=reffalseinletcase,result=List.fold_right(funtag(nullary,acc)->match(nullary,tag.prf_desc)with|(`Nothing|`Null),Rtag({txt;_},true,[])->letname=matchAst_attributes.iter_process_bs_string_astag.prf_attributeswith|Somename->has_bs_as:=true;name|None->txtin(`Null,(txt,name)::acc)|(`Nothing|`NonNull),Rtag({txt;_},false,[_])->letname=matchAst_attributes.iter_process_bs_string_astag.prf_attributeswith|Somename->has_bs_as:=true;name|None->txtin(`NonNull,(txt,name)::acc)|_->Error.err~loc:ptyp_locInvalid_mel_string_type)row_fields(`Nothing,[])inmatchcasewith|`Nothing->Error.err~loc:ptyp_locInvalid_mel_string_type|`Null|`NonNull->(lethas_payload=case=`NonNullinletdescr=if!has_bs_asthenSomeresultelseNoneinmatch(has_payload,descr)with|false,None->Bs_ast_invariant.warn~loc:ptyp_locRedundant_mel_string;Nothing|false,Somedescr->Poly_var_string{descr}|true,_->Poly_var{descr})