123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603openPpxlibopenBaseopenGTCommonopenHelpersBaseopenPpxlib.Ast_builder.Defaultlet(@@)=Caml.(@@)letnolabelizexs=List.map~f:(funx->Asttypes.Nolabel,x)xsletinvariantizetypes=List.maptypes~f:(funx->x,Asttypes.Invariant)typeloc=Location.tletnoloc=Location.noneletloc_from_camll=ltypetype_arg=core_typeletnamed_type_arg~locs=ptyp_var~locslettyp_arg_of_core_typet=tletlid?(loc=Location.none)txt={txt;loc}letmknoloctxt=lidtxtletpexp_pair?(loc=Location.none)ab=pexp_tuple~loc[a;b]letconst_string?wtfs=Pconst_string(s,wtf)typelab_decl=label_declarationletlab_decl~locnamemuttype_=label_declaration~loc~name:(Located.mk~locname)~mutable_:(ifmutthenMutableelseImmutable)~type_typecase=Ppxlib.caseletcase~lhs~rhs=case~lhs~rhs~guard:NonemodulePat=structtypet=patternletany~loc=ppat_any~locletunit~loc=[%pat?()]letof_longident~loclident=lethelper=function|Lidents->ppat_var~loc(Located.mk~locs)|Ldot(Lidentl,r)asp->ppat_construct~loc(Located.mk~locp)None|_->assertfalseinhelperlidentletaccess2~locmn=of_longident~loc(Ldot(Lidentm,n))letconstraint_~loc=ppat_constraint~locletconstr~loclidentps=letlident=Located.lident~loclidentinppat_construct~loclident@@matchpswith|[]->None|_->Some(ppat_tuple~locps)letvariant~loclps=ppat_variant~locl@@matchpswith|[]->None|_->Some(ppat_tuple~locps)lettuple~loc=ppat_tuple~locletvar~locs=ppat_var~loc(Located.mk~locs)letof_string~locs=var~locsletsprintf~locfmt=Printf.ksprintf(of_string~loc)fmtletalias~locps=ppat_alias~locp(lid~locs)lettype_~loclident=ppat_type~loc(Located.mk~loclident)letrecord~locps=ppat_record~loc(List.map~f:(fun(l,t)->(Located.mk~locl,t))ps)Closedletrecord1~locname=matchnamewith|Lidentname->record~loc[(Lidentname,var~locname)]|Ldot(Lidentp,name)asi->record~loc[(i,var~locname)]|_->failwith"not implemented"letconstr_record~loclidentps=constr~loclident[record~loc(List.map~f:(fun(l,x)->(Lidentl,x))ps)]endletuse_new_type~locnamee=pexp_newtype~loc(Located.mk~locname)emoduleExp=structtypet=expressionletfrom_camle=eletident~locs=pexp_ident~loc@@Located.lident~locsletof_longident~locl=pexp_ident~loc(Located.mk~locl)letsprintf~locfmt=Printf.ksprintf(ident~loc)fmtletunit~loc=[%expr()]letuid~loc=assertfalseletlid=identletaccess~locmnameiname=assert(Char.is_uppercasemname.[0]);letlident=Ldot(Lidentmname,iname)inifChar.is_uppercaseiname.[0]thenpexp_construct~loc(Located.mk~loclident)Noneelseof_longident~loclidentletconstant~loc=pexp_constant~locletint_const~locn=constant~loc(Pconst_integer(Int.to_stringn,None))letstring_const~locs=constant~loc(Pconst_string(s,None))letapp~loclr=pexp_apply~locl[(Nolabel,r)]letapp_lab~locllabr=pexp_apply~locl[(Labelledlab,r)]letapp_list~locexs=pexp_apply~loce(nolabelizexs)(* let apply1 ~loc ?(label=Nolabel) f arg = pexp_apply ~loc f [label,arg] *)letfield~loctlident=pexp_field~loct(Located.mk~loclident)letacc~loclr=pexp_field~locl(Located.mk~locr)letacc_list~loclxs=assertfalseletfun_~loc=pexp_fun~locNolabelNoneletfun_list_l~locargse=ifList.is_emptyargstheneelseList.fold_rightargs~init:e~f:(fun(l,opt)->pexp_fun~loc(Optionall)(Someopt)(Pat.var~locl))letfun_list~locargse=ifList.is_emptyargstheneelseList.fold_rightargs~init:e~f:(funarg->pexp_fun~locNolabelNonearg)letcase?guardlhsrhs=Ast_builder.Default.case~lhs~rhs~guardletrecord~locts=pexp_record~loc(List.mapts~f:(fun(l,r)->(Located.mk~locl,r)))Noneletrecord1~loclidentexpr=record~loc[lident,expr]letconstruct~loclidentxs=pexp_construct~loc(Located.mk~loclident)@@matchxswith|[]->None|xs->Some(pexp_tuple~locxs)letvariant~locets=matchtswith|[]->pexp_variant~loceNone|_->pexp_variant~loce(Some(pexp_tuple~locts))letmatch_~loc=pexp_match~locletnew_~locs=pexp_new~loc(Located.mk~locs)letobject_~loc=pexp_object~loclettuple~loc=pexp_tuple~locletmaybe_tuple~locxs=matchxswith|[]->None|[x]->Somex|_->Some(tuple~locxs)letsend~locobjs=pexp_send~locobj(Located.mk~locs)letletmodule~loc=pexp_letmodule~locletpack_with_constraint~locmetypname=pexp_constraint~loc(pexp_pack~locme)@@ptyp_package~loc(typname,[])letlet_one~loc?(rec_=false)patexprewhere=pexp_let~loc(ifrec_thenRecursiveelseNonrecursive)[value_binding~loc~pat~expr]ewhereletlet_~loc?(rec_=false)ps=pexp_let~loc(ifrec_thenRecursiveelseNonrecursive)(List.mapps~f:(fun(pat,expr)->value_binding~loc~pat~expr))letassert_false~loc=[%exprassertfalse]letobjmagic_unit~loc=[%exprObj.magic()]letfailwith_~locs=app~loc[%exprfailwith](string_const~locs)lettrue_~loc=[%exprtrue]letfalse_~loc=[%exprfalse]letlist~locxs=List.fold_rightxs~f:(funeacc->construct~loc(lident"::")[e;acc])~init:(construct~loc(lident"[]")[])letconstraint_~locet=pexp_constraint~locetendmoduleTyp=structtypet=Ppxlib.core_typeletconstr~loclident=ptyp_constr~loc(Located.mk~loclident)letof_type_arg~loctyp={typwithptyp_loc=loc}letfrom_camltyp=typletuse_tdecltdecl=letloc=tdecl.ptype_locinptyp_constr~loc(Located.lident~loctdecl.ptype_name.txt)@@(List.map~f:fsttdecl.ptype_params)letof_longident~loclident=ptyp_constr~loc(Located.mk~loclident)[]letaccess2~locmnametname=assert(Char.is_uppercasemname.[0]);letlident=Ldot(Lidentmname,tname)inof_longident~loclidentletident~locs=ptyp_constr~loc(Located.lident~locs)[]letsprintf~locfmt=Printf.ksprintf(ident~loc)fmtletstring~loc=ptyp_constr~loc(Located.mk~loc@@Lident"string")[]letunit~loc=ptyp_constr~loc(Located.mk~loc@@Lident"unit")[]letpair~loclr=ptyp_tuple~loc[l;r]letvar~locs=ptyp_var~locsletany~loc=ptyp_any~locletunit~loc=[%type:unit](* let ground ~loc s = constr ~loc (Located.mk ~loc s) [] *)letclass_~loc=ptyp_class~locletobject_~locflgxs=ptyp_object~loc(List.mapxs~f:(fun(l,r)->{pof_desc=Otag(Located.mk~locl,r);pof_loc=loc;pof_attributes=[]}))flgletpackage~loclident=ptyp_package~loc(lident,[])letarrow~loclr=ptyp_arrow~locNolabellrlettuple~locts=let()=assert(List.lengthts>1)inptyp_tuple~loctsletclass_~loclidentargs=ptyp_class~loc(Located.mk~loclident)argsletchain_arrow~loc=function|[]->failwith"list can't be empty"|xs->letrevxs=List.revxsinList.fold_left(List.tl_exnrevxs)~init:(List.hd_exnrevxs)~f:(funacct->arrow~loctacc)letvariant~loc?(is_open=false)fields=ptyp_variant~locfields(ifis_openthenOpenelseClosed)Noneletvariant_of_t~loct=[%type:[>[%tt]]]letalias~locts=ptyp_alias~loctsletpoly~locnamest=ptyp_poly~loc(List.mapnames~f:(Located.mk~loc))tletmap~onvart=HelpersBase.map_core_type~onvartletopenize~loc?as_t=letans=variant_of_t~loctinmatchas_with|Somename->alias~locansname|None->ansletto_type_argx=Somexletto_type_arg_exn=Fn.idend[@@warning"-32"]typenonrecclass_declaration=class_declarationletclass_declaration~loc~name?(virt=false)?(wrap=(funx->x))~paramsfields=letopenAst_builder.Defaultinletvirt=ifvirtthenVirtualelseConcreteinletparams=invariantizeparamsinletpat=[%pat?_]inAst_helper.Ci.mk~loc~virt~params(Located.mk~locname)@@wrap(Ast_helper.Cl.structure~loc(Ast_helper.Cstr.mkpatfields))typenonrectype_kind=|Ptype_abstract|Ptype_recordoflab_decllisttypenonrectype_declaration=type_declarationlettype_declaration~loc~name~params~manifest~kind=type_declaration~loc~name:(Located.mk~locname)~params:(List.mapparams~f:(funp->(p,Invariant)))~cstrs:[]~private_:Public~manifest~kind:(matchkindwith|Ptype_abstract->Parsetree.Ptype_abstract|Ptype_recordls->Parsetree.Ptype_recordls)moduleStr=structtypet=structure_itemletsingle_class~loc?(virt=Asttypes.Virtual)?(pat=[%pat?_])?(wrap=(funx->x))~name~paramsbody=pstr_class[Ast_helper.Ci.mk~virt~params(Located.mk~locname)@@wrap(Ast_helper.Cl.structure(Ast_helper.Cstr.mkpatbody))]letof_class_declarations=pstr_classletof_tdecls~locdecl=Ast_helper.Str.type_~locRecursive[decl]lettdecl~loc~name~paramstyp=letparams=List.map~f:(funs->Typ.var~locs,Invariant)paramsinpstr_type~locRecursive@@[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params~manifest:(Sometyp)~kind:Ptype_abstract~cstrs:[]~private_:Public]lettdecl_record~loc~name~paramslabels=letparams=List.map~f:(funs->Typ.var~locs,Invariant)paramsinpstr_type~locNonrecursive@@[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params~manifest:None~kind:(Ptype_recordlabels)~cstrs:[]~private_:Public](* make value have a default re4cursive flag *)letclass_single~loc~name?(virt=false)?(wrap=(funx->x))~paramsfields=letopenAst_builder.Defaultinletvirt=ifvirtthenVirtualelseConcreteinletparams=invariantizeparamsinletpat=[%pat?_]inpstr_class~loc[Ast_helper.Ci.mk~loc~virt~params(Located.mk~locname)@@wrap(Ast_helper.Cl.structure~loc(Ast_helper.Cstr.mkpatfields))]letvalue~loc?(flag=Nonrecursive)decls=pstr_value~locflagdeclsletsingle_value~locpatexpr=letflag=Nonrecursiveinpstr_value~locflag[value_binding~loc~pat~expr]letvalues~loc?(rec_flag=Recursive)vbs=pstr_value~locrec_flagvbsletof_vb~loc?(rec_flag=Recursive)vb=pstr_value~locrec_flag[vb]lettdecl_abstr~locnameparams=pstr_type~locRecursive@@[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params:(List.mapparams~f:(function|None->(ptyp_any~loc,Invariant)|Somes->(ptyp_var~locs,Invariant)))~cstrs:[]~kind:Ptype_abstract~private_:Public~manifest:None]letfunctor1~locname~paramsigsstrs=pstr_module~loc@@module_binding~loc~name:(Located.mk~locname)~expr:(pmod_functor~loc(Located.mk~locparam)(Option.some@@pmty_signature~locsigs)@@pmod_structure~locstrs)letsimple_gadt:loc:loc->name:string->params_count:int->(string*Typ.t)list->t=fun~loc~name~params_countxs->pstr_type~locRecursive[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params:(List.initparams_count~f:(fun_->(ptyp_any~loc,Invariant)))~cstrs:[]~private_:Public~manifest:None~kind:(Ptype_variant(List.mapxs~f:(fun(name,typ)->constructor_declaration~loc~name:(Located.mk~locname)~args:(Pcstr_tuple[])~res:(Sometyp))))]letmodule_~locnameme=pstr_module~loc@@module_binding~loc~name:(Located.mk~locname)~expr:meletmodtype~loc=pstr_modtype~locletinclude_~locme=pstr_include~loc@@include_infos~locmeendmoduleMe=structtypet=module_exprletstructure~locsis=pmod_structure~locsisletident~loclident=pmod_ident~loc(Located.mk~loclident)letapply~loc=pmod_apply~locletfunctor_~locname=pmod_functor~loc(Located.mk~locname)endmoduleMt=structtypet=module_typeletident~loclident=pmty_ident~loc(Located.mk~loclident)letsignature~loc=pmty_signature~locletfunctor_~locargnameargtt=pmty_functor~loc(Located.mk~locargname)argttletwith_~loc=pmty_with~locendtypenonrecmodule_declaration=module_declarationtypenonrecmodule_type_declaration=module_type_declarationletmodule_declaration~loc~nametype_=module_declaration~loc~name:(Located.mk~locname)~type_letmodule_type_declaration~loc~nametype_=module_type_declaration~loc~name:(Located.mk~locname)~type_moduleSig=structtypet=signature_itemletof_tdecls~locdecl=Ast_helper.Sig.type_~locRecursive[decl]letclass_~loc~name~params?(virt=false)?(wrap=(funx->x))fields=letvirt=ifvirtthenVirtualelseConcreteinletparams=invariantizeparamsinpsig_class~loc[class_infos~loc~name:(Located.mk~locname)~virt~params~expr:(wrap(pcty_signature~loc@@class_signature~self:[%type:_]~fields))]letvalue~loc~nametype_=psig_value~loc@@letprim=[]invalue_description~loc~name:(Located.mk~locname)~type_~primlettdecl_abstr~locnameparams=psig_type~locRecursive@@[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params:(List.mapparams~f:(function|None->(ptyp_any~loc,Invariant)|Somes->(ptyp_var~locs,Invariant)))~cstrs:[]~kind:Ptype_abstract~private_:Public~manifest:None]letfunctor1~locname~paramsigsstrs=psig_module~loc@@Ast_builder.Default.module_declaration~loc~name:(Located.mk~locname)~type_:(pmty_functor~loc(Located.mk~locparam)(Option.some@@pmty_signature~locsigs)@@pmty_signature~locstrs)letsimple_gadt:loc:loc->name:string->params_count:int->(string*Typ.t)list->t=fun~loc~name~params_countxs->psig_type~locRecursive[Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params:(List.initparams_count~f:(fun_->(ptyp_any~loc,Invariant)))~cstrs:[]~private_:Public~manifest:None~kind:(Ptype_variant(List.mapxs~f:(fun(name,typ)->constructor_declaration~loc~name:(Located.mk~locname)~args:(Pcstr_tuple[])~res:(Sometyp))))]letmodule_~locmd=psig_module~locmdletmodtype~loc=psig_modtype~locendmoduleWC=structtypet=Ppxlib.with_constraint(* There is no helper functions in Ast_builder *)lettyp~loc~paramsnametyp=Pwith_type(Located.mk~loc(Lidentname),Ast_builder.Default.type_declaration~loc~name:(Located.mk~locname)~params:(List.mapparams~f:(funs->(Typ.var~locs,Invariant)))~private_:Public~cstrs:[]~kind:Ptype_abstract~manifest:(Sometyp))endmoduleVb=structtypet=Ppxlib.value_bindingendletvalue_binding=value_bindingmoduleCf=structtypet=class_fieldletconstraint_~loct1t2=pcf_constraint~loc(t1,t2)letinherit_~loc?(as_=None)cl_expr=let(_:stringoption)=as_inletflg=Freshinpcf_inherit~locflgcl_expr@@Option.map~f:(funs->Located.mk~locs)as_letmethod_~locname?(flg=Public)kind=pcf_method~loc(Located.mk~locname,flg,kind)letmethod_concrete~locname(* ?(flg=Public) ?(over_flg=Fresh) *)e=method_~locname~flg:Public(Cfk_concrete(Fresh,e))letmethod_virtual~locname(* ?(flg=Public) *)typ=method_~locname~flg:Public(Cfk_virtualtyp)endmoduleCtf=structtypet=class_type_fieldletmethod_~loc?(virt=false)namekind=letflg=Publicinletvirt_flg=ifvirtthenVirtualelseConcreteinpctf_method~loc(Located.mk~locname,flg,virt_flg,kind)letinherit_~loc=pctf_inherit~locletconstraint_~loclr=pctf_constraint~loc(l,r)endmoduleCty=struct(* include Ast_helper.Cty *)typet=class_typeletarrow~loclr=Ast_helper.Cty.arrow~locNolabellrletconstr~loclidentts=pcty_constr~loc(Located.mk~loclident)tsendmoduleCl=structopenAst_helperincludeCltypet=class_exprletfun_list~locargse=ifList.is_emptyargstheneelseList.fold_rightargs~init:e~f:(funargacc->Cl.fun_~locAsttypes.NolabelNoneargacc)letapply~loceargs=ifList.is_emptyargstheneelseCl.apply~loce(nolabelizeargs)letfun_~loc=pcl_fun~locNolabelNoneletconstr~loc(lid:longident)ts=pcl_constr~loc(Located.mk~loclid)tsletstructure~loc=pcl_structure~locletlet_~loc?(flg=Nonrecursive)=Cl.let_~locflgend(*
module Cstr = struct
let mk ~self fields = class_structure ~self ~fields
end*)typeclass_structure=Ppxlib.class_structureletclass_structure=Ast_builder.Default.class_structureopenParsetreeletopenize_helper~is_open~loctyp=letloc=typ.ptyp_locinTyp.variant~loc~is_open[{prf_desc=Rinherittyp;prf_loc=loc;prf_attributes=[]}]letopenize_poly~loc=openize_helper~is_open:true~locletclosize_poly~loc=openize_helper~is_open:false~locletmap_type_param_names~fps=List.mapps~f:(fun(t,_)->matcht.ptyp_descwith|Ptyp_varname->fname|_->failwith"bad argument of map_type_param_names")letprepare_param_triples~loc~extra?(inh=fun~locs->Typ.var~loc@@"i"^s)?(syn=fun~locs->Typ.var~loc@@"s"^s)?(default_inh=[%type:'inh])?(default_syn=[%type:'syn])names=letps=List.concat_mapnames~f:(funn->[inh~locn;Typ.var~locn;syn~locn])inps@[default_inh;extra;default_syn](* let params_obj ?(loc=Location.none)
* ?(inh=fun s -> Typ.var @@ "i"^s) ?(syn=fun s -> Typ.var @@ "s"^s) root_type =
* (\* converts 'a, 'b to
* < a: 'ia -> 'a -> 'sa ; b: 'ib -> 'b -> 'sb >
* *\)
* let f (t,_) = arr_of_param ~inh ~syn t in
* ptyp_object ~loc (List.map ~f root_type.ptype_params) Asttypes.Closed *)letinh_syn_ts?(loc=Location.none)()=[[%type:'inh];[%type:'syn]](* Used when we need to check that type we working on references himself in
it's body *)letare_the_same(typ:core_type)(tdecl:type_declaration)=(matchtyp.ptyp_descwith|Ptyp_constr({txt=Longident.Lidentxxx},_)->letb=(String.equalxxxtdecl.ptype_name.txt)inb|_->false)lettyp_vars_of_typt=leto=objectinherit[stringlist]Ast_traverse.foldassupermethod!core_type_desctacc=matchtwith|Ptyp_vars->s::acc|_->super#core_type_desctaccendinList.remove_consecutive_duplicates~equal:String.equal@@o#core_typet[]