123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985# 1 "ppx/tools/ppx_deriving_tools.ml"openPrintfopenPpxlibopenAst_builder.DefaultopenStdLabelsopenExpansion_helpersexceptionErroroflocation*stringleterror~locwhat=raise(Error(loc,what))letnot_supported~locwhat=raise(Error(loc,sprintf"%s are not supported"what))letpexp_error~locmsg=pexp_extension~loc(Location.error_extensionf~loc"%s"msg)letstri_error~locmsg=[%stri[%%ocaml.error[%eestring~locmsg]]]letmap_locfa_loc={a_locwithtxt=fa_loc.txt}letgen_bindings~locprefixn=List.split(List.init~len:n~f:(funi->letid=sprintf"%s_%i"prefixiinletpatt=ppat_var~loc{loc;txt=id}inletexpr=pexp_ident~loc{loc;txt=lidentid}inpatt,expr))letgen_tuple~locprefixn=letps,es=gen_bindings~locprefixninps,pexp_tuple~locesletgen_record~locprefixfs=letps,es=List.split(List.mapfs~f:(fun(n,_attrs,_t)->letid=sprintf"%s_%s"prefixn.txtinletpatt=ppat_var~loc{loc=n.loc;txt=id}inletexpr=pexp_ident~loc{loc=n.loc;txt=lidentid}in(map_loclidentn,patt),expr))inletns,ps=List.splitpsinps,pexp_record~loc(List.combinenses)Noneletgen_pat_tuple~locprefixn=letpatts,exprs=gen_bindings~locprefixninppat_tuple~locpatts,exprsletgen_pat_list~locprefixn=letpatts,exprs=gen_bindings~locprefixninletpatt=List.fold_left(List.revpatts)~init:[%pat?[]]~f:(funprevpatt->[%pat?[%ppatt]::[%pprev]])inpatt,exprsletgen_pat_record~locprefixns=letxs=List.mapns~f:(funn->letid=sprintf"%s_%s"prefixn.txtinletpatt=ppat_var~loc{loc=n.loc;txt=id}inletexpr=pexp_ident~loc{loc=n.loc;txt=lidentid}in(map_loclidentn,patt),expr)inppat_record~loc(List.mapxs~f:fst)Closed,List.mapxs~f:sndlet(-->)pc_lhspc_rhs={pc_lhs;pc_rhs;pc_guard=None}letderive_of_labelname=mangle(Suffixname)letderive_of_longidentname=mangle_lid(Suffixname)letederivername(lid:Longident.tloc)=pexp_ident~loc:lid.loc(map_loc(derive_of_longidentname)lid)typederiver=|As_funof(expression->expression)|As_valofexpressionletas_val~locderiverx=matchderiverwithAs_funf->fx|As_valf->[%expr[%ef][%ex]]letas_fun~locderiver=matchderiverwith|As_funf->[%exprfunx->[%ef[%exprx]]]|As_valf->fclassvirtualderiving=objectmethodvirtualname:labelmethodvirtualextension:loc:location->path:label->core_type->expressionmethodvirtualgenerator:ctxt:Expansion_context.Deriver.t->rec_flag*type_declarationlist->structureendletregister?depsderiving=Deriving.addderiving#name~str_type_decl:(Deriving.Generator.V2.make?depsDeriving.Args.emptyderiving#generator)~extension:deriving#extensionletregister_combined?depsnamederivings=letgenerator~ctxtbindings=List.fold_leftderivings~init:[]~f:(funstrd->d#generator~ctxtbindings@str)inDeriving.addname~str_type_decl:(Deriving.Generator.V2.make?depsDeriving.Args.emptygenerator)moduleSchema=structletrepr_row_fieldfield=matchfield.prf_descwith|Rtag(id,_,ts)->`Rtag(id,ts)|Rinherit{ptyp_desc=Ptyp_constr(id,ts);_}->`Rinherit(id,ts)|Rinherit_->not_supported~loc:field.prf_loc"this polyvariant inherit"letrepr_core_typety=letloc=ty.ptyp_locinmatchty.ptyp_descwith|Ptyp_tuplets->`Ptyp_tuplets|Ptyp_constr(id,ts)->`Ptyp_constr(id,ts)|Ptyp_vartxt->`Ptyp_var{txt;loc=ty.ptyp_loc}|Ptyp_variant(fs,Closed,None)->`Ptyp_variantfs|Ptyp_variant_->not_supported~loc"non closed polyvariants"|Ptyp_arrow_->not_supported~loc"function types"|Ptyp_any->not_supported~loc"type placeholders"|Ptyp_object_->not_supported~loc"object types"|Ptyp_class_->not_supported~loc"class types"|Ptyp_poly_->not_supported~loc"polymorphic type expressions"|Ptyp_package_->not_supported~loc"packaged module types"|Ptyp_extension_->not_supported~loc"extension nodes"|Ptyp_alias_->not_supported~loc"type aliases"letrepr_type_declarationtd=letloc=td.ptype_locinmatchtd.ptype_kind,td.ptype_manifestwith|Ptype_abstract,None->not_supported~loc"abstract types"|Ptype_abstract,Somet->`Ptype_core_typet|Ptype_variantctors,_->`Ptype_variantctors|Ptype_recordfs,_->`Ptype_recordfs|Ptype_open,_->not_supported~loc"open types"letrepr_type_declaration_is_polytd=matchrepr_type_declarationtdwith|`Ptype_core_type({ptyp_desc=Ptyp_variant_;_}ast)->`Ptyp_variantt|_->`Otherletgen_type_ascription(td:type_declaration)=letloc=td.ptype_locinptyp_constr~loc{loc;txt=lidenttd.ptype_name.txt}(List.maptd.ptype_params~f:(fun(p,_)->matchp.ptyp_descwith|Ptyp_varname->ptyp_var~locname|_->failwith"this cannot be a type parameter"))classvirtualderiving0=object(self)inheritderivingmethodvirtualt:loc:location->labelloc->core_type->core_typemethodderive_of_tuple:loc:location->core_typelist->expression=not_supported"tuple types"methodderive_of_record:loc:location->label_declarationlist->expression=not_supported"record types"methodderive_of_variant:loc:location->constructor_declarationlist->expression=not_supported"variant types"methodderive_of_polyvariant:loc:location->row_fieldlist->expression=not_supported"polyvariant types"methodprivatederive_type_ref_name:label->longidentloc->expression=funnamen->ederivernamenmethodderive_type_ref~locnaments=letf=self#derive_type_ref_namenameninletargs=List.fold_left(List.revts)~init:[]~f:(funargsa->leta=self#derive_of_core_typeain(Nolabel,a)::args)inpexp_apply~locfargsmethodderive_of_core_typety=letloc=ty.ptyp_locinmatchrepr_core_typetywith|`Ptyp_tuplets->self#derive_of_tuple~locts|`Ptyp_constr(id,ts)->self#derive_type_refself#name~locidts|`Ptyp_varlabel->ederiverself#name(map_loclidentlabel)|`Ptyp_variantfs->self#derive_of_polyvariant~locfsmethodderive_of_type_declarationtd=letloc=td.ptype_locinletname=td.ptype_nameinletparams=List.maptd.ptype_params~f:(fun(t,_)->matcht.ptyp_descwith|Ptyp_vartxt->{txt;loc=t.ptyp_loc}|_->failwith"type variable is not a variable")inletexpr=matchrepr_type_declarationtdwith|`Ptype_core_typet->self#derive_of_core_typet|`Ptype_variantctors->self#derive_of_variant~locctors|`Ptype_recordfs->self#derive_of_record~locfsinlett=gen_type_ascriptiontdinletexpr=[%expr([%eexpr]:[%tself#t~locnamet])]inletexpr=List.fold_leftparams~init:expr~f:(funbodyname->pexp_fun~locNolabelNone(ppat_var~loc(map_loc(derive_of_labelself#name)name))body)in[value_binding~loc~pat:(ppat_var~loc(self#derive_type_decl_labelname))~expr;]methodprivatederive_type_decl_labelname=map_loc(derive_of_labelself#name)namemethodextension:loc:location->path:label->core_type->expression=fun~loc:_~path:_ty->self#derive_of_core_typetymethodgenerator:ctxt:Expansion_context.Deriver.t->rec_flag*type_declarationlist->structure=fun~ctxt(_rec_flag,type_decls)->letloc=Expansion_context.Deriver.derived_item_locctxtinletbindings=List.concat_maptype_decls~f:(fundecl->self#derive_of_type_declarationdecl)in[%str[@@@ocaml.warning"-39-11-27"][%%ipstr_value~locRecursivebindings]]endclassvirtualderiving1=object(self)inheritderivingmethodvirtualt:loc:location->labelloc->core_type->core_typemethodderive_of_tuple:core_type->core_typelist->expression->expression=funt__->letloc=t.ptyp_locinnot_supported"tuple types"~locmethodderive_of_record:type_declaration->label_declarationlist->expression->expression=funtd__->letloc=td.ptype_locinnot_supported"record types"~locmethodderive_of_variant:type_declaration->constructor_declarationlist->expression->expression=funtd__->letloc=td.ptype_locinnot_supported"variant types"~locmethodderive_of_polyvariant:core_type->row_fieldlist->expression->expression=funt__->letloc=t.ptyp_locinnot_supported"polyvariant types"~locmethodprivatederive_type_ref_name:label->longidentloc->expression=funnamen->ederivernamenmethodprivatederive_type_ref'~locnaments=letf=self#derive_type_ref_namenameninletargs=List.fold_left(List.revts)~init:[]~f:(funargsa->leta=as_fun~loc(self#derive_of_core_type'a)in(Nolabel,a)::args)inAs_val(pexp_apply~locfargs)methodderive_type_ref~locnamentsx=as_val~loc(self#derive_type_ref'~locnaments)xmethodprivatederive_of_core_type't=letloc=t.ptyp_locinmatchrepr_core_typetwith|`Ptyp_tuplets->As_fun(self#derive_of_tupletts)|`Ptyp_varlabel->As_val(ederiverself#name(map_loclidentlabel))|`Ptyp_constr(id,ts)->self#derive_type_ref'self#name~locidts|`Ptyp_variantfs->As_fun(self#derive_of_polyvarianttfs)methodderive_of_core_typetx=letloc=x.pexp_locinas_val~loc(self#derive_of_core_type't)xmethodprivatederive_type_decl_labelname=map_loc(derive_of_labelself#name)namemethodderive_of_type_declarationtd=letloc=td.ptype_locinletname=td.ptype_nameinletrev_params=List.rev_maptd.ptype_params~f:(fun(t,_)->matcht.ptyp_descwith|Ptyp_vartxt->{txt;loc=t.ptyp_loc}|_->failwith"type variable is not a variable")inletx=[%exprx]inletexpr=matchrepr_type_declarationtdwith|`Ptype_core_typet->self#derive_of_core_typetx|`Ptype_variantctors->self#derive_of_varianttdctorsx|`Ptype_recordfs->self#derive_of_recordtdfsxinletexpr=[%expr(funx->[%eexpr]:[%tself#t~locname(gen_type_ascriptiontd)])]inletexpr=List.fold_leftrev_params~init:expr~f:(funbodyparam->pexp_fun~locNolabelNone(ppat_var~loc(map_loc(derive_of_labelself#name)param))body)in[value_binding~loc~pat:(ppat_var~loc(self#derive_type_decl_labelname))~expr;]methodextension:loc:location->path:label->core_type->expression=fun~loc:_~path:_ty->letloc=ty.ptyp_locinas_fun~loc(self#derive_of_core_type'ty)methodgenerator:ctxt:Expansion_context.Deriver.t->rec_flag*type_declarationlist->structure=fun~ctxt(_rec_flag,tds)->letloc=Expansion_context.Deriver.derived_item_locctxtinletbindings=List.concat_maptds~f:self#derive_of_type_declarationin[%str[@@@ocaml.warning"-39-11-27"][%%ipstr_value~locRecursivebindings]]endendmoduleConv=structtype'ctxtuple={tpl_loc:location;tpl_types:core_typelist;tpl_ctx:'ctx;}type'ctxrecord={rcd_loc:location;rcd_fields:label_declarationlist;rcd_ctx:'ctx;}typevariant_case=|Vcs_tupleoflabelloc*variant_case_ctxtuple|Vcs_recordoflabelloc*variant_case_ctxrecord|Vcs_enumoflabelloc*variant_case_ctxandvariant_case_ctx=|Vcs_ctx_variantofconstructor_declaration|Vcs_ctx_polyvariantofrow_fieldtypevariant={vrt_loc:location;vrt_cases:variant_caselist;vrt_ctx:variant_ctx;}andvariant_ctx=|Vrt_ctx_variantoftype_declaration|Vrt_ctx_polyvariantofcore_typeletrepr_polyvariant_casescs=letcases=List.revcs|>List.map~f:(func->c,Schema.repr_row_fieldc)inletis_enum=List.for_allcases~f:(fun(_,r)->matchrwith|`Rtag(_,ts)->(matchtswith[]->true|_::_->false)|`Rinherit_->false)inis_enum,casesletrepr_variant_casescs=letcs=List.revcsinletis_enum=List.for_allcs~f:(fun(c:constructor_declaration)->matchc.pcd_argswith|Pcstr_record[]->true|Pcstr_tuple[]->true|Pcstr_record_|Pcstr_tuple_->false)inis_enum,csletderiving_of~name~of_t~error~derive_of_tuple~derive_of_record~derive_of_variant~derive_of_variant_case()=letpoly_name=sprintf"%s_poly"nameinletpoly=object(self)inheritSchema.deriving1methodname=namemethodt~loc_namet=[%type:[%tof_t~loc]->[%tt]option]method!derive_type_decl_labelname=map_loc(derive_of_labelpoly_name)namemethod!derive_of_tuplettsx=lett={tpl_loc=t.ptyp_loc;tpl_types=ts;tpl_ctx=t}inderive_of_tupleself#derive_of_core_typetxmethod!derive_of_record___=assertfalsemethod!derive_of_variant___=assertfalsemethod!derive_of_polyvariantt(cs:row_fieldlist)x=letloc=t.ptyp_locinletis_enum,cases=repr_polyvariant_casescsinletbody,cases=List.fold_leftcases~init:([%exprNone],[])~f:(fun(next,cases)(c,r)->matchrwith|`Rtag(n,ts)->letmakearg=[%exprSome[%epexp_variant~loc:n.locn.txtarg]]inletctx=Vcs_ctx_polyvariantcinletcase=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inVcs_tuple(n,t)inletnext=derive_of_variant_caseself#derive_of_core_typemakecasenextinnext,case::cases|`Rinherit(id,ts)->letx=self#derive_type_ref~locpoly_nameidtsxinlett=ptyp_variant~loccsClosedNoneinletnext=[%exprmatch[%ex]with|Somex->(Somex:>[%tt]option)|None->[%enext]]innext,cases)inlett={vrt_loc=loc;vrt_cases=cases;vrt_ctx=Vrt_ctx_polyvariantt;}inderive_of_variantself#derive_of_core_typetbodyxendin(object(self)inheritSchema.deriving1assupermethodname=namemethodt~loc_namet=[%type:[%tof_t~loc]->[%tt]]method!derive_of_tuplettsx=lett={tpl_loc=t.ptyp_loc;tpl_types=ts;tpl_ctx=t}inderive_of_tupleself#derive_of_core_typetxmethod!derive_of_recordtdfsx=lett={rcd_loc=td.ptype_loc;rcd_fields=fs;rcd_ctx=td}inderive_of_recordself#derive_of_core_typetxmethod!derive_of_varianttdcsx=letloc=td.ptype_locinletis_enum,cs=repr_variant_casescsinletbody,cases=List.fold_leftcs~init:(error~loc,[])~f:(fun(next,cases)c->letmake(n:labelloc)arg=pexp_construct(map_loclidentn)~loc:n.locarginletctx=Vcs_ctx_variantcinletn=c.pcd_nameinmatchc.pcd_argswith|Pcstr_recordfs->lett=ifis_enumthenVcs_enum(n,ctx)elselett={rcd_loc=loc;rcd_fields=fs;rcd_ctx=ctx}inVcs_record(n,t)inletnext=derive_of_variant_caseself#derive_of_core_type(maken)tnextinnext,t::cases|Pcstr_tuplets->letcase=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inVcs_tuple(n,t)inletnext=derive_of_variant_caseself#derive_of_core_type(maken)casenextinnext,case::cases)inlett={vrt_loc=loc;vrt_cases=cases;vrt_ctx=Vrt_ctx_varianttd;}inderive_of_variantself#derive_of_core_typetbodyxmethod!derive_of_polyvariantt(cs:row_fieldlist)x=letloc=t.ptyp_locinletis_enum,cases=repr_polyvariant_casescsinletbody,cases=List.fold_leftcases~init:(error~loc,[])~f:(fun(next,cases)(c,r)->letctx=Vcs_ctx_polyvariantcinmatchrwith|`Rtag(n,ts)->letmakearg=pexp_variant~loc:n.locn.txtarginletcase=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inVcs_tuple(n,t)inletnext=derive_of_variant_caseself#derive_of_core_typemakecasenextinnext,case::cases|`Rinherit(n,ts)->letmaybe_e=poly#derive_type_ref~locpoly_namentsxinlett=ptyp_variant~loccsClosedNoneinletnext=[%exprmatch[%emaybe_e]with|Somee->(e:>[%tt])|None->[%enext]]innext,cases)inlett={vrt_loc=loc;vrt_cases=cases;vrt_ctx=Vrt_ctx_polyvariantt;}inderive_of_variantself#derive_of_core_typetbodyxmethod!derive_of_type_declarationtd=matchSchema.repr_type_declaration_is_polytdwith|`Ptyp_variant_->letstr=letloc=td.ptype_locinletdecl_name=td.ptype_nameinletparams=List.maptd.ptype_params~f:(fun(t,_)->matcht.ptyp_descwith|Ptyp_vartxt->t,{txt;loc=t.ptyp_loc}|_->assertfalse)inletexpr=letx=[%exprx]inletinit=poly#derive_type_ref~locpoly_name(map_loclidentdecl_name)(List.mapparams~f:fst)xinletinit=[%expr(funx->match[%einit]with|Somex->x|None->[%eerror~loc]:[%tself#t~locdecl_name(Schema.gen_type_ascriptiontd)])]inList.fold_leftparams~init~f:(funbody(_,param)->pexp_fun~locNolabelNone(ppat_var~loc(map_loc(derive_of_labelname)param))body)in[value_binding~loc~pat:(ppat_var~loc(map_loc(derive_of_labelself#name)decl_name))~expr;]inpoly#derive_of_type_declarationtd@str|`Other->super#derive_of_type_declarationtdend:>deriving)letderiving_of_match~name~of_t~error~derive_of_tuple~derive_of_record~derive_of_variant_case()=letpoly_name=sprintf"%s_poly"nameinletpoly=object(self)inheritSchema.deriving1methodname=namemethodt~loc_namet=[%type:[%tof_t~loc]->[%tt]option]method!derive_type_decl_labelname=map_loc(derive_of_labelpoly_name)namemethod!derive_of_tuplettsx=lett={tpl_loc=t.ptyp_loc;tpl_types=ts;tpl_ctx=t}inderive_of_tupleself#derive_of_core_typetxmethod!derive_of_record___=assertfalsemethod!derive_of_variant___=assertfalsemethod!derive_of_polyvariantt(cs:row_fieldlist)x=letloc=t.ptyp_locinletis_enum,cases=repr_polyvariant_casescsinletctors,inherits=List.partition_mapcases~f:(fun(c,r)->letctx=Vcs_ctx_polyvariantcinmatchrwith|`Rtag(n,ts)->ifis_enumthenLeft(n,Vcs_enum(n,ctx))elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inLeft(n,Vcs_tuple(n,t))|`Rinherit(n,ts)->Right(n,ts))inletcatch_all=[%pat?x]-->List.fold_left(List.revinherits)~init:[%exprNone]~f:(funnext(n,ts)->letmaybe=self#derive_type_ref~locpoly_naments[%exprx]inlett=ptyp_variant~loccsClosedNonein[%exprmatch[%emaybe]with|Somex->(Somex:>[%tt]option)|None->[%enext]])inletcases=List.fold_leftctors~init:[catch_all]~f:(funnext(n,case)->letmakearg=[%exprSome[%epexp_variant~loc:n.locn.txtarg]]inderive_of_variant_caseself#derive_of_core_typemakecase::next)inpexp_match~locxcasesendin(object(self)inheritSchema.deriving1assupermethodname=namemethodt~loc_namet=[%type:[%tof_t~loc]->[%tt]]method!derive_of_tuplettsx=lett={tpl_loc=t.ptyp_loc;tpl_types=ts;tpl_ctx=t}inderive_of_tupleself#derive_of_core_typetxmethod!derive_of_recordtdfsx=lett={rcd_loc=td.ptype_loc;rcd_fields=fs;rcd_ctx=td}inderive_of_recordself#derive_of_core_typetxmethod!derive_of_varianttdcsx=letloc=td.ptype_locinletis_enum,cs=repr_variant_casescsinletcases=List.fold_leftcs~init:[[%pat?_]-->error~loc]~f:(funnext(c:constructor_declaration)->letctx=Vcs_ctx_variantcinletmake(n:labelloc)arg=pexp_construct(map_loclidentn)~loc:n.locarginletn=c.pcd_nameinmatchc.pcd_argswith|Pcstr_recordfs->lett=ifis_enumthenVcs_enum(n,ctx)elseletr={rcd_loc=loc;rcd_fields=fs;rcd_ctx=ctx}inVcs_record(n,r)inderive_of_variant_caseself#derive_of_core_type(maken)t::next|Pcstr_tuplets->lett=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inVcs_tuple(n,t)inderive_of_variant_caseself#derive_of_core_type(maken)t::next)inpexp_match~locxcasesmethod!derive_of_polyvariantt(cs:row_fieldlist)x=letloc=t.ptyp_locinletis_enum,cases=repr_polyvariant_casescsinletctors,inherits=List.partition_mapcases~f:(fun(c,r)->letctx=Vcs_ctx_polyvariantcinmatchrwith|`Rtag(n,ts)->ifis_enumthenLeft(n,Vcs_enum(n,ctx))elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inLeft(n,Vcs_tuple(n,t))|`Rinherit(n,ts)->Right(n,ts))inletcatch_all=[%pat?x]-->List.fold_left(List.revinherits)~init:(error~loc)~f:(funnext(n,ts)->letmaybe=poly#derive_type_ref~locpoly_namentsxinlett=ptyp_variant~loccsClosedNonein[%exprmatch[%emaybe]with|Somex->(x:>[%tt])|None->[%enext]])inletcases=List.fold_leftctors~init:[catch_all]~f:(funnext((n:labelloc),t)->letmakearg=pexp_variant~loc:n.locn.txtarginderive_of_variant_caseself#derive_of_core_typemaket::next)inpexp_match~locxcasesmethod!derive_of_type_declarationtd=matchSchema.repr_type_declaration_is_polytdwith|`Ptyp_variant_->letstr=letloc=td.ptype_locinletdecl_name=td.ptype_nameinletparams=List.maptd.ptype_params~f:(fun(t,_)->matcht.ptyp_descwith|Ptyp_vartxt->t,{txt;loc=t.ptyp_loc}|_->assertfalse)inletexpr=letx=[%exprx]inletinit=poly#derive_type_ref~locpoly_name(map_loclidentdecl_name)(List.mapparams~f:fst)xinletinit=[%expr(funx->match[%einit]with|Somex->x|None->[%eerror~loc]:[%tself#t~locdecl_name(Schema.gen_type_ascriptiontd)])]inList.fold_leftparams~init~f:(funbody(_,param)->pexp_fun~locNolabelNone(ppat_var~loc(map_loc(derive_of_labelname)param))body)in[value_binding~loc~pat:(ppat_var~loc(map_loc(derive_of_labelself#name)decl_name))~expr;]inpoly#derive_of_type_declarationtd@str|`Other->super#derive_of_type_declarationtdend:>deriving)letderiving_to~name~t_to~derive_of_tuple~derive_of_record~derive_of_variant_case()=(object(self)inheritSchema.deriving1methodname=namemethodt~loc_namet=[%type:[%tt]->[%tt_to~loc]]method!derive_of_tuplettsx=letloc=t.ptyp_locinlett={tpl_loc=loc;tpl_types=ts;tpl_ctx=t}inletn=List.lengthtsinletp,es=gen_pat_tuple~loc"x"ninpexp_match~locx[p-->derive_of_tupleself#derive_of_core_typetes]method!derive_of_recordtdfsx=lett={rcd_loc=td.ptype_loc;rcd_fields=fs;rcd_ctx=td}inletloc=td.ptype_locinletp,es=gen_pat_record~loc"x"(List.mapfs~f:(funf->f.pld_name))inpexp_match~locx[p-->derive_of_recordself#derive_of_core_typetes]method!derive_of_varianttdcsx=letloc=td.ptype_locinletctor_pat(n:labelloc)pat=ppat_construct~loc:n.loc(map_loclidentn)patinletis_enum,cs=repr_variant_casescsinpexp_match~locx(List.rev_mapcs~f:(func->letn=c.pcd_nameinletctx=Vcs_ctx_variantcinmatchc.pcd_argswith|Pcstr_recordfs->letp,es=gen_pat_record~loc"x"(List.mapfs~f:(funf->f.pld_name))inlett=ifis_enumthenVcs_enum(n,ctx)elselett={rcd_loc=loc;rcd_fields=fs;rcd_ctx=ctx;}inVcs_record(n,t)inctor_patn(Somep)-->derive_of_variant_caseself#derive_of_core_typetes|Pcstr_tuplets->letarity=List.lengthtsinlett=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inVcs_tuple(n,t)inletp,es=gen_pat_tuple~loc"x"arityinctor_patn(ifarity=0thenNoneelseSomep)-->derive_of_variant_caseself#derive_of_core_typetes))method!derive_of_polyvariantt(cs:row_fieldlist)x=letloc=t.ptyp_locinletis_enum,cases=repr_polyvariant_casescsinletcases=List.rev_mapcases~f:(fun(c,r)->letctx=Vcs_ctx_polyvariantcinmatchrwith|`Rtag(n,[])->lett=ifis_enumthenVcs_enum(n,ctx)elselett={tpl_loc=loc;tpl_types=[];tpl_ctx=ctx}inVcs_tuple(n,t)inppat_variant~locn.txtNone-->derive_of_variant_caseself#derive_of_core_typet[]|`Rtag(n,ts)->assert(notis_enum);lett={tpl_loc=loc;tpl_types=ts;tpl_ctx=ctx}inletps,es=gen_pat_tuple~loc"x"(List.lengthts)inppat_variant~locn.txt(Someps)-->derive_of_variant_caseself#derive_of_core_type(Vcs_tuple(n,t))es|`Rinherit(n,ts)->assert(notis_enum);[%pat?[%pppat_type~locn]asx]-->self#derive_of_core_type(ptyp_constr~loc:n.locnts)[%exprx])inpexp_match~locxcasesend:>deriving)endincludeSchema