Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_deriving_make.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145# 1 "ppx_deriving_make.cppo.ml"openPpxlibopenAsttypesopenParsetreeopenAst_helperopenPpx_deriving.Ast_convenienceletderiver="make"letraise_errorf=Ppx_deriving.raise_errorfletparse_optionsoptions=options|>List.iter(fun(name,expr)->matchnamewith|_->raise_errorf~loc:expr.pexp_loc"%s does not support option %s"derivername)letattr_defaultattrs=Ppx_deriving.(attrs|>attr~deriver"default"|>Arg.(get_attr~deriverexpr))letattr_splitattrs=Ppx_deriving.(attrs|>attr~deriver"split"|>Arg.get_flag~deriver)letfind_mainlabels=List.fold_left(fun(main,labels)({pld_type;pld_loc;pld_attributes}aslabel)->ifPpx_deriving.(pld_type.ptyp_attributes@pld_attributes|>attr~deriver"main"|>Arg.get_flag~deriver)thenmatchmainwith|Some_->raise_errorf~loc:pld_loc"Duplicate [@deriving.%s.main] annotation"deriver|None->Somelabel,labelselsemain,label::labels)(None,[])labelsletis_optional{pld_name={txt=name};pld_type;pld_attributes}=letattrs=pld_attributes@pld_type.ptyp_attributesinmatchattr_defaultattrswith|Some_->true|None->attr_splitattrs||(matchPpx_deriving.remove_pervasives~deriverpld_typewith|[%type:[%t?_]list]|[%type:[%t?_]option]->true|_->false)letstr_of_type~options~path({ptype_loc=loc}astype_decl)=parse_optionsoptions;letquoter=Ppx_deriving.create_quoter()inletcreator=matchtype_decl.ptype_kindwith|Ptype_recordlabels->letfields=labels|>List.map(fun{pld_name={txt=name;loc}}->name,evarname)inletmain,labels=find_mainlabelsinlethas_option=List.existsis_optionallabelsinletfn=matchmainwith|Some{pld_name={txt=name}}->Exp.fun_Label.nolabelNone(pvarname)(recordfields)|Nonewhenhas_option->Exp.fun_Label.nolabelNone(punit())(recordfields)|None->recordfieldsinList.fold_left(funaccum{pld_name={txt=name};pld_type;pld_attributes}->letattrs=pld_attributes@pld_type.ptyp_attributesinletpld_type=Ppx_deriving.remove_pervasives~deriverpld_typeinmatchattr_defaultattrswith|Somedefault->Exp.fun_(Label.optionalname)(Some(Ppx_deriving.quote~quoterdefault))(pvarname)accum|None->ifattr_splitattrsthenmatchpld_typewith|[%type:[%t?lhs]*[%t?rhs]list]whenname.[String.lengthname-1]='s'->letname'=String.subname0(String.lengthname-1)inExp.fun_(Label.labelledname')None(pvarname')(Exp.fun_(Label.optionalname)(Some[%expr[]])(pvarname)[%exprlet[%ppvarname]=[%eevarname'],[%eevarname]in[%eaccum]])|_->raise_errorf~loc"[@deriving.%s.split] annotation requires a type of form \
'a * 'b list and label name ending with `s'"deriverelsematchpld_typewith|[%type:[%t?_]list]->Exp.fun_(Label.optionalname)(Some[%expr[]])(pvarname)accum|[%type:[%t?_]option]->Exp.fun_(Label.optionalname)None(pvarname)accum|_->Exp.fun_(Label.labelledname)None(pvarname)accum)fnlabels|_->raise_errorf~loc"%s can be derived only for record types"deriverin[Vb.mk(pvar(Ppx_deriving.mangle_type_decl(`Prefixderiver)type_decl))(Ppx_deriving.sanitize~quotercreator)]letwrap_predef_optiontyp=typletsig_of_type~options~path({ptype_loc=loc}astype_decl)=parse_optionsoptions;lettyp=Ppx_deriving.core_type_of_type_decltype_declinlettyp=matchtype_decl.ptype_kindwith|Ptype_recordlabels->letmain,labels=find_mainlabelsinlethas_option=List.existsis_optionallabelsinlettyp=matchmainwith|Some{pld_name={txt=name};pld_type}->Typ.arrowLabel.nolabelpld_typetyp|Nonewhenhas_option->Typ.arrowLabel.nolabel(tconstr"unit"[])typ|None->typinList.fold_left(funaccum{pld_name={txt=name;loc};pld_type;pld_attributes}->letattrs=pld_type.ptyp_attributes@pld_attributesinletpld_type=Ppx_deriving.remove_pervasives~deriverpld_typeinmatchattr_defaultattrswith|Some_->Typ.arrow(Label.optionalname)(wrap_predef_optionpld_type)accum|None->ifattr_splitattrsthenmatchpld_typewith|[%type:[%t?lhs]*[%t?rhs]list]whenname.[String.lengthname-1]='s'->letname'=String.subname0(String.lengthname-1)inTyp.arrow(Label.labelledname')lhs(Typ.arrow(Label.optionalname)(wrap_predef_option[%type:[%trhs]list])accum)|_->raise_errorf~loc"[@deriving.%s.split] annotation requires a type of form \
'a * 'b list and label name ending with `s'"deriverelsematchpld_typewith|[%type:[%t?_]list]->Typ.arrow(Label.optionalname)(wrap_predef_optionpld_type)accum|[%type:[%t?opt]option]->Typ.arrow(Label.optionalname)(wrap_predef_optionopt)accum|_->Typ.arrow(Label.labelledname)pld_typeaccum)typlabels|_->raise_errorf~loc"%s can only be derived for record types"deriverin[Sig.value(Val.mk(mknoloc(Ppx_deriving.mangle_type_decl(`Prefixderiver)type_decl))typ)]let()=Ppx_deriving.(register(createderiver~type_decl_str:(fun~options~pathtype_decls->[Str.valueNonrecursive(List.concat(List.map(str_of_type~options~path)type_decls))])~type_decl_sig:(fun~options~pathtype_decls->List.concat(List.map(sig_of_type~options~path)type_decls))()))