123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321open!ImportopenCommontype(_,_)equality=Eq:('a,'a)equality|Ne:(_,_)equalitymoduleContext=structtype'at=|Class_expr:class_exprt|Class_field:class_fieldt|Class_type:class_typet|Class_type_field:class_type_fieldt|Core_type:core_typet|Expression:expressiont|Module_expr:module_exprt|Module_type:module_typet|Pattern:patternt|Signature_item:signature_itemt|Structure_item:structure_itemttypepacked=T:_t->packedletclass_expr=Class_exprletclass_field=Class_fieldletclass_type=Class_typeletclass_type_field=Class_type_fieldletcore_type=Core_typeletexpression=Expressionletmodule_expr=Module_exprletmodule_type=Module_typeletpattern=Patternletsignature_item=Signature_itemletstructure_item=Structure_itemletdesc:typea.at->string=function|Class_expr->"class expression"|Class_field->"class field"|Class_type->"class type"|Class_type_field->"class type field"|Core_type->"core type"|Expression->"expression"|Module_expr->"module expression"|Module_type->"module type"|Pattern->"pattern"|Signature_item->"signature item"|Structure_item->"structure item"leteq:typeab.at->bt->(a,b)equality=funab->matcha,bwith|Class_expr,Class_expr->Eq|Class_field,Class_field->Eq|Class_type,Class_type->Eq|Class_type_field,Class_type_field->Eq|Core_type,Core_type->Eq|Expression,Expression->Eq|Module_expr,Module_expr->Eq|Module_type,Module_type->Eq|Pattern,Pattern->Eq|Signature_item,Signature_item->Eq|Structure_item,Structure_item->Eq|_->assert(Poly.(<>)(Ta)(Tb));Neletget_extension:typea.at->a->(extension*attributes)option=funtx->matcht,xwith|Class_expr,{pcl_desc=Pcl_extensione;pcl_attributes=a;_}->Some(e,a)|Class_field,{pcf_desc=Pcf_extensione;pcf_attributes=a;_}->Some(e,a)|Class_type,{pcty_desc=Pcty_extensione;pcty_attributes=a;_}->Some(e,a)|Class_type_field,{pctf_desc=Pctf_extensione;pctf_attributes=a;_}->Some(e,a)|Core_type,{ptyp_desc=Ptyp_extensione;ptyp_attributes=a;_}->Some(e,a)|Expression,{pexp_desc=Pexp_extensione;pexp_attributes=a;_}->Some(e,a)|Module_expr,{pmod_desc=Pmod_extensione;pmod_attributes=a;_}->Some(e,a)|Module_type,{pmty_desc=Pmty_extensione;pmty_attributes=a;_}->Some(e,a)|Pattern,{ppat_desc=Ppat_extensione;ppat_attributes=a;_}->Some(e,a)|Signature_item,{psig_desc=Psig_extension(e,a);_}->Some(e,a)|Structure_item,{pstr_desc=Pstr_extension(e,a);_}->Some(e,a)|_->Noneletmerge_attributes:typea.at->a->attributes->a=funtxattrs->matchtwith|Class_expr->{xwithpcl_attributes=x.pcl_attributes@attrs}|Class_field->{xwithpcf_attributes=x.pcf_attributes@attrs}|Class_type->{xwithpcty_attributes=x.pcty_attributes@attrs}|Class_type_field->{xwithpctf_attributes=x.pctf_attributes@attrs}|Core_type->{xwithptyp_attributes=x.ptyp_attributes@attrs}|Expression->{xwithpexp_attributes=x.pexp_attributes@attrs}|Module_expr->{xwithpmod_attributes=x.pmod_attributes@attrs}|Module_type->{xwithpmty_attributes=x.pmty_attributes@attrs}|Pattern->{xwithppat_attributes=x.ppat_attributes@attrs}|Signature_item->assert_no_attributesattrs;x|Structure_item->assert_no_attributesattrs;xendletregistrar=Name.Registrar.create~kind:"extension"~current_file:__FILE__~string_of_context:(fun(Context.Tctx)->Some(Context.descctx));;moduleMake(Callback:sigtype'atend)=structtype('a,'b)payload_parser=Payload_parser:('a,'b,'c)Ast_pattern.t*'bCallback.t->('a,'c)payload_parsertype('context,'payload)t={name:Name.Pattern.t;context:'contextContext.t;payload:(payload,'payload)payload_parser;with_arg:bool}letdeclare~with_argnamecontextpatternk=Name.Registrar.register~kind:`Extensionregistrar(Context.Tcontext)name;{name=Name.Pattern.makename;context;payload=Payload_parser(pattern,k);with_arg};;letfindts(ext:extension)=let{txt=name;loc}=fstextinletname,arg=Name.split_pathnameinmatchList.filterts~f:(funt->Name.Pattern.matchest.namename)with|[]->None|_::_::_asl->Location.raise_errorf~loc"Multiple match for extensions: %s"(String.concat~sep:", "(List.mapl~f:(funt->Name.Pattern.namet.name)))|[t]->ifnott.with_arg&&Option.is_someargthenLocation.raise_errorf~loc"Extension %s doesn't expect a path argument"name;letarg=Option.maparg~f:(funs->letshift=String.lengthname+1inletstart=loc.loc_startin{txt=Longident.parses;loc={locwithloc_start={startwithpos_cnum=start.pos_cnum+shift}}})inSome(t,arg);;endmoduleExpert=structincludeMake(structtype'at=arg:Longident.tLoc.toption->'aend)letdeclare_with_path_argnamectxpattf=declare~with_arg:truenamectxpattfletdeclarenamectxpattf=declare~with_arg:falsenamectxpatt(fun~arg:_->f)letconvertts~locext=matchfindtsextwith|None->None|Some({payload=Payload_parser(pattern,f);_},arg)->Some(Ast_pattern.parsepatternloc(sndext)(f~arg))endmoduleM=Make(structtype'at=ctxt:Expansion_context.Extension.t->arg:Longident.tLoc.toption->'aend)type'aexpander_result=|Simpleof'a|Inlineof'alistmoduleFor_context=structtype'at=('a,'aexpander_result)M.tletconvertts~ctxtext=letloc=Expansion_context.Extension.extension_point_locctxtinmatchM.findtsextwith|None->None|Some({payload=M.Payload_parser(pattern,f);_},arg)->matchAst_pattern.parsepatternloc(sndext)(f~ctxt~arg)with|Simplex->Somex|Inline_->failwith"Extension.convert";;letconvert_inlinets~ctxtext=letloc=Expansion_context.Extension.extension_point_locctxtinmatchM.findtsextwith|None->None|Some({payload=M.Payload_parser(pattern,f);_},arg)->matchAst_pattern.parsepatternloc(sndext)(f~ctxt~arg)with|Simplex->Some[x]|Inlinel->Somel;;endtypet=T:_For_context.t->tletcheck_context_for_inline:typea.func:string->aContext.t->unit=fun~funcctx->matchctxwith|Context.Class_field->()|Context.Class_type_field->()|Context.Signature_item->()|Context.Structure_item->()|context->Printf.ksprintfinvalid_arg"%s: %s can't be inlined"func(Context.desccontext);;letrecfilter_by_context:typea.aContext.t->tlist->aFor_context.tlist=funcontextexpanders->matchexpanderswith|[]->[]|Tt::rest->matchContext.eqcontextt.contextwith|Eq->t::filter_by_contextcontextrest|Ne->filter_by_contextcontextrest;;letfailctx(name,_)=ifnot(Name.Whitelisted.is_whitelisted~kind:`Extensionname.txt||Name.ignore_checksname.txt)thenName.Registrar.raise_errorfregistrar(Context.Tctx)"Extension `%s' was not translated"name;;letcheck_unused=objectinheritAst_traverse.iterassupermethod!extension(name,_)=Location.raise_errorf~loc:name.loc"extension not expected here, Ppxlib.Extension needs updating!"method!core_type_desc=function|Ptyp_extensionext->failCore_typeext|x->super#core_type_descxmethod!pattern_desc=function|Ppat_extensionext->failPatternext|x->super#pattern_descxmethod!expression_desc=function|Pexp_extensionext->failExpressionext|x->super#expression_descxmethod!class_type_desc=function|Pcty_extensionext->failClass_typeext|x->super#class_type_descxmethod!class_type_field_desc=function|Pctf_extensionext->failClass_type_fieldext|x->super#class_type_field_descxmethod!class_expr_desc=function|Pcl_extensionext->failClass_exprext|x->super#class_expr_descxmethod!class_field_desc=function|Pcf_extensionext->failClass_fieldext|x->super#class_field_descxmethod!module_type_desc=function|Pmty_extensionext->failModule_typeext|x->super#module_type_descxmethod!signature_item_desc=function|Psig_extension(ext,_)->failSignature_itemext|x->super#signature_item_descxmethod!module_expr_desc=function|Pmod_extensionext->failModule_exprext|x->super#module_expr_descxmethod!structure_item_desc=function|Pstr_extension(ext,_)->failStructure_itemext|x->super#structure_item_descxendmoduleV3=structtypenonrect=tletdeclarenamecontextpatternk=letpattern=Ast_pattern.map_resultpattern~f:(funx->Simplex)inT(M.declare~with_arg:falsenamecontextpattern(fun~ctxt~arg:_->k~ctxt))letdeclare_inlinenamecontextpatternk=check_context_for_inlinecontext~func:"Extension.declare_inline";letpattern=Ast_pattern.map_resultpattern~f:(funx->Inlinex)inT(M.declare~with_arg:falsenamecontextpattern(fun~ctxt~arg:_->k~ctxt))endletdeclarenamecontextpatternf=V3.declarenamecontextpattern(Expansion_context.Extension.with_loc_and_pathf)letdeclare_inlinenamecontextpatternf=V3.declare_inlinenamecontextpattern(Expansion_context.Extension.with_loc_and_pathf)letdeclare_with_path_argnamecontextpatternk=letk'=Expansion_context.Extension.with_loc_and_pathkinletpattern=Ast_pattern.map_resultpattern~f:(funx->Simplex)inT(M.declare~with_arg:truenamecontextpatternk');;letdeclare_inline_with_path_argnamecontextpatternk=letk'=Expansion_context.Extension.with_loc_and_pathkincheck_context_for_inlinecontext~func:"Extension.declare_inline_with_path_arg";letpattern=Ast_pattern.map_resultpattern~f:(funx->Inlinex)inT(M.declare~with_arg:truenamecontextpatternk');;moduleV2=structtypenonrect=tletdeclare=declareletdeclare_inline=declare_inlineend