123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698# 1 "override.cppo.ml"moduleOCaml_version=Migrate_parsetree.OCaml_408moduleFrom=Migrate_parsetree.Convert(OCaml_version)(Migrate_parsetree.OCaml_current)moduleAst_mapper=OCaml_version.Ast.Ast_mappermoduleAst_helper=OCaml_version.Ast.Ast_helpermoduleParsetree=OCaml_version.Ast.Parsetreeletoverride_name="[%%override]"letrecursive_name="[%%recursive]"letattr_remove="remove"letattr_rewrite="rewrite"letattr_from="from"letflatten_mapflist=letrecauxacculist=matchlistwith|[]->List.revaccu|hd::tl->aux(List.rev_append(fhd)accu)tlinaux[]listletrecfind_map_optflist=matchlistwith|[]->None|hd::tl->matchfhdwith|None->find_map_optftl|result->result(*
Adapted from ppx_import
https://github.com/ocaml-ppx/ppx_import/
Copyright (c) 2014 Peter Zotov whitequark@whitequark.org
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)letlazy_env=lazy((* It is important that the typing environment is not evaluated
right away, but only once the ppx-context has been loaded from
the AST, so that Config.load_path and the rest of the environment
context are correctly set.
The environment setting should happen when reading the
ppx-context attribute which is the very first structure/signature
item sent to ppx rewriters. In particular, this happens before
the [%import ] extensions are traversed, which are the places in
this code where 'env' is forced.
We would also have the option to not have a global environment, but
recompute the typing environment on each [%import ] extension. We don't
see any advantage in doing this, given that we compute the global/initial
environment that is the same at all program points.
*)(* We need to set recursive_types manually, because it is not part
of the context automatically saved by Ast_mapper (as of 4.06),
and this prevents loading the interface of recursive-types-using
modules. On the other hand, setting recursive_types more often
than necessary does not seem harmful. *)Clflags.recursive_types:=true;# 84 "override.cppo.ml"Compmisc.init_path();# 88 "override.cppo.ml"Compmisc.initial_env())lettry_find_module~loc:_envlid=(* Note: we are careful to call `Env.lookup_module` and not
`Typetexp.lookup_module`, because we want to reason precisely
about the possible failures: we want to handle the case where
the module path does not exist, but let all the other errors
(invalid .cmi format, etc.) bubble up to the error handler.
`Env.lookup_module` allows to do this easily as it raises
a well-identified `Not_found` exception, while
`Typetexp.lookup_module` wraps the Not_found failure in
user-oriented data and is not meant for catching.
`Env.find_module` can raise `Not_found` again; we suspect that
it will not in the cases where `lookup_module` returned correctly,
but better be safe and bundle them in the same try..with.
*)tryletpath=Env.lookup_module~load:truelidenvinletmodule_decl=Env.find_modulepathenvinSomemodule_decl.md_typewithNot_found->Nonelettry_find_module_type~locenvlid=(* Here again we prefer to handle the `Not_found` case, so we
use `Env.lookup_module` rather than `Typetexp.lookup_module`. *)trylet_path,modtype_decl=Env.lookup_modtype~loclidenvinSome(matchmodtype_decl.mtd_typewith|None->Location.raise_errorf~loc"%s: cannot access the signature of the abstract module %a"override_namePrinttyp.longidentlid|Somemodule_type->module_type)withNot_found->Noneletlocate_sigenv(ident:Longident.tLocation.loc)=matchidentwith{loc;txt=lid}->matchtry_find_module~locenvlidwith|Somemty->mty|None->matchtry_find_module_type~locenvlidwith|Somemty->mty|None->Location.raise_errorf~loc"%s: cannot locate module %a"override_namePrinttyp.longidentlidletrecroot_of_longident(lid:Longident.t)=matchlidwith|Lidentident->ident|Ldot(lid,_)|Lapply(lid,_)->root_of_longidentlidletis_self_reference(lid:Longident.t)=letmn=String.uncapitalize_ascii(root_of_longidentlid)inletfn=!Location.input_name|>Filename.basename|>Filename.chop_extension|>String.uncapitalize_asciiinmn=fnmoduleString_map=Map.Make(String)moduleString_set=Set.Make(String)moduleLongident_map=Map.Make(structtypet=Longident.tletcompare=compareend)exceptionUnsupportedletrecequal_core_type(t0:Parsetree.core_type)(t1:Parsetree.core_type)=Core_type_equiv.equiv_core_typeequal_core_typet0t1letrecmatch_core_typesubst_ref(p:Parsetree.core_type)(t:Parsetree.core_type)=matchp.ptyp_descwith|Ptyp_any->true|Ptyp_varx->beginmatchString_map.find_optx!subst_refwith|Somet'->equal_core_typett'|None->subst_ref:=String_map.addxt!subst_ref;trueend|_->Core_type_equiv.equiv_core_type(match_core_typesubst_ref)ptletsubst_core_typesubstt=lettypmapper(t:Parsetree.core_type)=matchmatcht.ptyp_descwith|Ptyp_varx->beginmatchString_map.find_optxsubstwith|Somet'->Somet'|None->Noneend|_->Nonewith|Somet'->t'|None->Ast_mapper.default_mapper.typmappertinletmapper={Ast_mapper.default_mapperwithtyp}inmapper.typmapperttyperewrite_system=(Parsetree.core_type*Parsetree.core_type)listletrecrewrite_once(ty:Parsetree.core_type)rewrite_system:Parsetree.core_typeoption=matchrewrite_systemwith|[]->None|(pat,res)::tail->letsubst_ref=refString_map.emptyinmatchifmatch_core_typesubst_refpattythenletres=subst_core_type!subst_refresinifequal_core_typetyresthenNoneelseSomereselseNonewith|None->rewrite_oncetytail|res->resletrecrewriterewrite_system(ty:Parsetree.core_type):Parsetree.core_type=matchty.ptyp_descwith|Ptyp_constr(ident,args)->letargs=args|>List.map(rewriterewrite_system)inletnew_ty={tywithptyp_desc=Ptyp_constr(ident,args)}inbeginmatchrewrite_oncenew_tyrewrite_systemwith|None->new_ty|Somerewritten->rewriterewrite_systemrewrittenend|_->tytyperewrite_context={subst_var:Parsetree.core_typeString_map.t;subst_constr:Longident.tLongident_map.t;subst_mod:Longident.tLongident_map.t;rewrite_system:rewrite_system;}letempty_rewrite_context={subst_var=String_map.empty;subst_constr=Longident_map.empty;subst_mod=Longident_map.empty;rewrite_system=[];}letrecrewrite_mod(subst:Longident.tLongident_map.t)(lid:Longident.t)=matchLongident_map.find_optlidsubstwith|Somelid'->lid'|None->matchlidwith|Lident_->lid|Ldot(lid,name)->Ldot(rewrite_modsubstlid,name)|Lapply(u,v)->Lapply(rewrite_modsubstu,rewrite_modsubstv)letmapper_of_rewrite_contextrewrite_context=lettyp(mapper:Ast_mapper.mapper)(core_type:Parsetree.core_type)=letcore_type=matchcore_type.ptyp_descwith|Ptyp_varx->begintryString_map.findxrewrite_context.subst_varwithNot_found->core_typeend|Ptyp_constr(lid,args)->letargs=args|>List.map(mapper.typmapper)inlettxt=matchLongident_map.find_optlid.txtrewrite_context.subst_constrwith|Sometxt->txt|None->rewrite_modrewrite_context.subst_modlid.txtin{core_typewithptyp_desc=Ptyp_constr({lidwithtxt},args)}|_->Ast_mapper.default_mapper.typmappercore_typeinrewriterewrite_context.rewrite_systemcore_typein{Ast_mapper.default_mapperwithtyp}letmap_locf(l:'aLocation.loc):'bLocation.loc={lwithtxt=fl.txt}letident_of_namename=map_loc(funx:Longident.t->Lidentx)nameletqualified_ident_of_namemodidentname=map_loc(funx:Longident.t->Ldot(modident,x))namemoduleSymbol_set=structtypet={types:String_set.t;modules:String_set.t;module_types:String_set.t;}letempty={types=String_set.empty;modules=String_set.empty;module_types=String_set.empty;}letadd_typetype_namesymbol_table={symbol_tablewithtypes=String_set.addtype_namesymbol_table.types}letadd_modulemodule_namesymbol_table={symbol_tablewithmodules=String_set.addmodule_namesymbol_table.modules}letadd_module_typemodule_type_namesymbol_table={symbol_tablewithmodule_types=String_set.addmodule_type_namesymbol_table.module_types}letunionuv={types=String_set.unionu.typesv.types;modules=String_set.unionu.modulesv.modules;module_types=String_set.unionu.module_typesv.module_types;}endmoduleSymbol_table=structtype'agroup={rec_flag:OCaml_version.Ast.Asttypes.rec_flag;decls:'alist;}typetype_decl={decl:Parsetree.type_declaration;mutableimported:bool;mutablerec_group:type_declgroup;}typemodtype_decl={decl:Parsetree.module_type_declaration;mutableimported:bool;}typeitem=|Typeoftype_declgroup|Modtypeofmodtype_decl|ValueofParsetree.value_description|ModuleofParsetree.module_declarationgroupletempty_group={rec_flag=Nonrecursive;decls=[]}typet={types:type_declString_map.t;modules:Parsetree.module_declarationString_map.t;module_types:modtype_declString_map.t;only_types:bool;}letempty={types=String_map.empty;modules=String_map.empty;module_types=String_map.empty;only_types=true;}letadd_typenametype_decltable={tablewithtypes=String_map.addnametype_decltable.types}letadd_modulenamemod_decltable={tablewithmodules=String_map.addnamemod_decltable.modules}letadd_module_typenamemod_decltable={tablewithmodule_types=String_map.addnamemod_decltable.module_types}letnot_only_typestable={tablewithonly_types=false}typesignature={sig_:Parsetree.signature;items:itemlist;table:t;}letadd_item(rev_items,table)(item:Parsetree.signature_item)=matchitem.psig_descwith|Psig_type(rec_flag,decls)->letadd_type(rev_decls,table)decl=lettype_decl={decl;imported=false;rec_group=empty_group}intype_decl::rev_decls,add_typedecl.ptype_name.txttype_decltableinletrev_decls,table=List.fold_leftadd_type([],table)declsinletgroup={rec_flag;decls=List.revrev_decls}inrev_decls|>List.iterbeginfun(decl:type_decl)->decl.rec_group<-group;end;Typegroup::rev_items,table|Psig_moduledecl->letgroup={rec_flag=Nonrecursive;decls=[decl]}inlettable=add_moduledecl.pmd_name.txtdecltableinModulegroup::rev_items,not_only_typestable|Psig_recmoduledecls->letgroup={rec_flag=Recursive;decls=decls}inlettable=List.fold_leftbeginfuntable(decl:Parsetree.module_declaration)->add_moduledecl.pmd_name.txtdecltableendtabledeclsinModulegroup::rev_items,not_only_typestable|Psig_modtypedecl->letmod_decl={decl;imported=false}inlettable=add_module_typedecl.pmtd_name.txtmod_decltableinModtypemod_decl::rev_items,table|Psig_valuedesc->Valuedesc::rev_items,not_only_typestable|_->rev_items,not_only_typestableletof_signaturesig_=letrev_items,table=List.fold_leftadd_item([],empty)sig_in{sig_;items=List.revrev_items;table}letimport~target~source=lettake_source_keytargetsource=Somesourcein{types=String_map.uniontake_sourcetarget.typessource.types;modules=String_map.uniontake_sourcetarget.modulessource.modules;module_types=String_map.uniontake_sourcetarget.module_typessource.module_types;only_types=target.only_types&&source.only_types;}endmoduleZipper=structtype'at={previous:'alist;current:'a;next:'alist;}letrecfindpreviousplist=matchlistwith|[]->None|current::next->ifpcurrentthenSome{previous;current;next}elsefind(current::previous)pnextletfindplist=find[]plistletpopzipper=List.rev_appendzipper.previouszipper.nextendletattr_name_isname({attr_name={txt;_};_}:Parsetree.attribute)=txt=namelethas_attrnameattributes=attributes|>List.exists(attr_name_isname)letfind_attr_type~locnameattributes=matchZipper.find(attr_name_isname)attributeswith|None->None|Somezipper->matchzipper.current.attr_payloadwith|PTypty->Some(zipper,ty)|_->Location.raise_errorf~loc"Type expected"letimport_type_declaration~locrewrite_context?modidentname?params?(attrs=[])(decl:Parsetree.type_declaration):Parsetree.type_declaration=letmapper=mapper_of_rewrite_contextrewrite_contextinletresult=mapper.type_declarationmapperdeclinletparams=matchparamswith|None->decl.ptype_params|>List.mapfst|Someparams->paramsinletfrom_name=decl.ptype_nameinletptype_name=nameinletptype_manifest,ptype_attributes=matchresult.ptype_manifestwith|Sometyp->letattrs:Parsetree.attributes=ifhas_attrattr_rewriteattrs&¬(has_attrattr_fromattrs)thenletimported_type=Ast_helper.Typ.constr(ident_of_namefrom_name)paramsinAst_helper.Attr.mk(Parsetree_of_types.mklocattr_from)(PTypimported_type)::attrselseattrsinSometyp,attrs|None->letmanifest=modident|>Option.mapbeginfunmodident->Ast_helper.Typ.constr(qualified_ident_of_namemodidentfrom_name)paramsendinmanifest,attrsin{resultwithptype_name;ptype_manifest;ptype_attributes}typeimport_type_decl={new_name:stringLocation.loc;attrs:Parsetree.attributes;decl:Symbol_table.type_decl;params:Parsetree.core_typelistoption;loc:Location.t;pdecl:Parsetree.type_declarationoption;}letprepare_import_rewrite_contextrewrite_context{new_name;decl}=letfrom_name=decl.decl.ptype_nameinifCore_type_equiv.equal_loc(=)from_namenew_namethenrewrite_contextelse{rewrite_contextwithsubst_constr=Longident_map.add(Lidentfrom_name.txt)(Longident.Lidentnew_name.txt)rewrite_context.subst_constr}letsubst_params~loc(params:Parsetree.core_typelist)(args:Parsetree.core_typelist)(rewrite_context:rewrite_context)=letpairs=tryList.combineparamsargswithInvalid_argument_->Location.raise_errorf~loc"Imported type has %d parameter(s), but %d are passed"(List.lengthparams)(List.lengthargs)inletadd_substsubst_var((param:Parsetree.core_type),arg)=matchparam.ptyp_descwith|Ptyp_any->subst_var|Ptyp_varx->String_map.addxargsubst_var|_->raiseUnsupportedin{rewrite_contextwithsubst_var=List.fold_leftadd_substrewrite_context.subst_varpairs}letimport_type_decl{new_name;attrs;decl;params;loc}modidentrewrite_contextoverriden_refdefined_ref=Ast_helper.with_default_locloc@@fun()->letrewrite_context=matchparamswith|None->rewrite_context|Someparams->subst_params~loc(decl.decl.ptype_params|>List.mapfst)paramsrewrite_contextinletresult=import_type_declaration~locrewrite_context?modidentnew_name?params~attrsdecl.declindecl.imported<-true;defined_ref:=Symbol_set.add_typenew_name.txt!defined_ref;overriden_ref:=Symbol_set.add_typedecl.decl.ptype_name.txt!overriden_ref;resultletimport_of_decl~loc(decl:Symbol_table.type_decl)attrs=letnew_name=decl.decl.ptype_namein{loc;new_name;attrs;decl;params=None;pdecl=None;}letdecl_of_list~locattrsmodidentrewrite_context(decls:Symbol_table.type_decllist)overriden_refdefined_ref=decls|>List.filter_mapbeginfun(decl:Symbol_table.type_decl)->ifdecl.importedthenNoneelsetrySome(import_type_decl(import_of_decl~locdeclattrs)modidentrewrite_contextoverriden_refdefined_ref)withUnsupported->Noneendtype'aenv={env:Env.t;scope:Symbol_table.t;signature:'a;}letget_alias_target(modtype:Parsetree.module_type):Longident.tLocation.locoption=matchmodtype.pmty_descwith|Pmty_identident|Pmty_aliasident->Someident|_->Noneletget_signature(modtype:Parsetree.module_type):Parsetree.signatureoption=matchmodtype.pmty_descwith|Pmty_signaturesignature->Somesignature|_->Noneletget_functor(modtype:Parsetree.module_type):(stringLocation.loc*Parsetree.module_typeoption*Parsetree.module_type)option=matchmodtype.pmty_descwith|Pmty_functor(x,arg_type,result_type)->Some(x,arg_type,result_type)|_->Noneletrecget_module_type~locenv(ident:Longident.t):Longident.t*Parsetree.module_typeoption=let(_,modtype_opt)asresult=matchidentwith|Lidentname->letmodtype_opt=matchmatchString_map.find_optnameenv.scope.moduleswith|None->None|Somedecl->ifmatchget_alias_targetdecl.pmd_typewith|None->true|Sometarget->target.txt<>identthenSomedecl.pmd_typeelseNonewith|(Some_)asresult->result|None->try_find_module~locenv.envident|>Option.mapParsetree_of_types.module_typeinident,modtype_opt|Ldot(ident,name)->letident,modtype_opt=get_module_type~locenvidentinletmodtype_opt=Option.bindmodtype_optbeginfunmodtype->Option.bind(get_signaturemodtype)beginfuntsig->tsig|>find_map_optbeginfun(item:Parsetree.signature_item)->matchitem.psig_descwith|Psig_moduledeclwhendecl.pmd_name.txt=name->Somedecl.pmd_type|_->NoneendendendinLdot(ident,name),modtype_opt|Lapply(ident,arg)->letident,modtype_opt=get_module_type~locenvidentinletarg,_modtype_opt=get_module_type~locenvidentinletmodtype_opt=Option.bindmodtype_optbeginfunmodtype->get_functormodtype|>Option.mapbeginfun(_x,_arg,result)->resultendendinLapply(ident,arg),modtype_optinmatchmatchOption.bindmodtype_optget_alias_targetwith|None->None|(Sometarget)asresult->ifident=target.txtthenNoneelseresultwith|None->result|Sometarget->get_module_type~locenvtarget.txtletresolve_alias~loc(env:Parsetree.module_typeenv)=matchget_alias_targetenv.signaturewith|None->Someenv.signature|Sometarget->snd(get_module_type~locenvtarget.txt)letextract_functor~locenvlid=matchOption.bind(resolve_alias~locenv)get_functorwith|Some(y,t,signature)->y,t,signature|None->Location.raise_errorf~loc"%s: %a is not a functor"override_namePrinttyp.longidentlidletextract_signature~locenvlid=matchOption.bind(resolve_alias~locenv)get_signaturewith|Somesignature->signature|None->Location.raise_errorf~loc"%s: %a is a functor"override_namePrinttyp.longidentlidtypemodenv={ident:Longident.tLocation.loc;modtype:Parsetree.module_typeenvoption;}letapply_functor~locmodenvname=lety,modtype=matchmodenv.modtypewith|None->None,None|Someenv->lety,_t,signature=extract_functor~locenvmodenv.ident.txtinSomey,Some{envwithsignature}inletmodenv={ident=modenv.ident|>map_loc(funident:Longident.t->Lapply(ident,Lidentname));modtype}iny,modenvletnot_foundkind(name:stringLocation.loc)ident=Location.raise_errorf~loc:name.loc"%s: %s %s not found in %a"override_namekindname.txtPrinttyp.longidentidentletfindkind(name:stringLocation.loc)mapident=tryString_map.findname.txtmapwithNot_found->not_foundkindnameidentletkind_type="type"letfind_typearg=findkind_typeargletfind_modulearg=find"module"argletfind_module_typearg=find"module type"argtypeimport_mode=Include|Not_include|Ignoretypemode={import:import_mode;submodule:bool;}letmode_of_stringname=matchnamewith|"override"->{import=Include;submodule=true;}|"include"->{import=Include;submodule=false;}|"import"->{import=Not_include;submodule=false;}|_->invalid_arg"mode_of_string"letrecremove_prefixprefix(ident:Longident.t)=matchidentwith|Lident_->ident|Ldot(lid,name)->iflid=prefixthenLidentnameelseLdot(remove_prefixprefixlid,name)|Lapply(lid,lid')->Lapply(remove_prefixprefixlid,remove_prefixprefixlid')letmap_typ_constr(p:Longident.tLocation.loc->Parsetree.core_typelist->Parsetree.core_type)t=lettyp(mapper:Ast_mapper.mapper)(t:Parsetree.core_type)=matcht.ptyp_descwith|Ptyp_constr(ident,args)->letresult=pident(args|>List.map(mapper.typmapper))in{twithptyp_desc=result.ptyp_desc}|_->Ast_mapper.default_mapper.typmappertinletmapper={Ast_mapper.default_mapperwithtyp}inmapper.typmappertletmap_typ_constr_ident(p:Longident.tLocation.loc->Longident.tLocation.loc)t=t|>map_typ_constrbeginfunidentargs->Ast_helper.Typ.constr(pident)argsendletrecmap_identmap_name(ident:Longident.t):Longident.t=matchidentwith|Lidentname->map_namename|Ldot(lid,name)->Ldot(map_identmap_namelid,name)|Lapply(lid,lid')->Lapply(map_identmap_namelid,map_identmap_namelid')letrecmap_ident_leafmap_mod_namemap_leaf_name(ident:Longident.t):Longident.t=matchidentwith|Lidentname->map_leaf_namename|Ldot(lid,name)->Ldot(map_identmap_mod_namelid,name)|Lapply_->invalid_arg"map_ident_leaf"(*
let rec canonize_type env (context : rewrite_context)
(ident : Longident.t Location.loc) args =
let loc = ident.loc in
match ident.txt with
| Lident _ -> Ast_helper.Typ.constr ident args
| Lapply _ -> assert false
| Ldot (lid, name) ->
let lid, modtype_opt = get_module_type ~loc env lid in
match
Option.bind modtype_opt begin fun modtype ->
Option.bind (get_signature modtype) begin fun tsig ->
tsig |> find_map_opt begin fun (item : Parsetree.signature_item) ->
match item.psig_desc with
| Psig_type (_, decls) ->
begin match
decls |> List.find_opt begin
fun (decl : Parsetree.type_declaration) ->
decl.ptype_name.txt = name
end
with
| None -> None
| Some decl ->
match decl.ptype_manifest with
| None -> None
| Some manifest ->
Some (decl.ptype_params, manifest)
end
| _ -> None
end
end
end
with
| _ (*None*) -> Ast_helper.Typ.constr { loc; txt = Ldot (lid, name) } args
| Some (params, manifest) ->
let context =
subst_params ~loc (params |> List.map fst) args context in
let mapper = mapper_of_rewrite_context context in
mapper.typ mapper manifest
*)letprefix_if_defined_locallyprefix(defined:Symbol_set.t)(type_pattern:Parsetree.core_type):Parsetree.core_type=type_pattern|>map_typ_constr@@beginfunidentargs->matchident.txt|>map_ident_leaf(funmod_name->ifString_set.memmod_namedefined.modulesthenLdot(prefix,mod_name)elseraiseNot_found)(funtyp_name->ifString_set.memtyp_namedefined.typesthenLdot(prefix,typ_name)elseraiseNot_found)with|exceptionNot_found->Ast_helper.Typ.constridentargs|txt->letident={identwithtxt}inAst_helper.Typ.constridentargsendletpromote_rewrite~locenvrewriterewrite_refprefixrhs_prefixoverridendefinednew_rewrites=letprefixed_rewrites=new_rewrites|>List.rev_mapbeginfun(lhs,rhs)->letlhs=prefix_if_defined_locallyprefixoverridenlhsinletrhs=ifrhs_prefixthenprefix_if_defined_locallyprefixdefinedrhselserhsin(lhs,rhs)endinrewrite_ref:=List.rev_appendprefixed_rewrites!rewrite_reftyperewrite_env={context:rewrite_context;rewrite_system_ref:rewrite_systemref;subst_mod_ref:Longident.tLongident_map.tref;}letcurrent_rewrite_context(env:rewrite_env)={env.contextwithrewrite_system=List.rev_append!(env.rewrite_system_ref)env.context.rewrite_system;subst_mod=Longident_map.union(fun_xy->Somex)!(env.subst_mod_ref)env.context.subst_mod}letmake_rewrite_envcontext={context;rewrite_system_ref=ref[];subst_mod_ref=refLongident_map.empty}letderive_rewrite_env(env:rewrite_env)=make_rewrite_env(current_rewrite_contextenv)letforce_rewrite_envrewrite_env=matchrewrite_envwith|None->make_rewrite_envempty_rewrite_context|Somerewrite_env->rewrite_envtypeoverride_context={modenv:modenv;name:string;mode:mode;manifest:bool;rewrite_env:rewrite_env;overriden_ref:Symbol_set.tref;defined_ref:Symbol_set.tref;override_module_type:override_context->Parsetree.module_type->Parsetree.module_type;}typemapper_context={ocamldep:bool;rewrite_env:rewrite_envoption;override_module_type:override_context->Parsetree.module_type->Parsetree.module_type;}letmake_context?(defined_ref=refSymbol_set.empty)modenvnamemode~manifestrewrite_envoverride_module_type={modenv;name;mode;manifest;rewrite_env;overriden_ref=refSymbol_set.empty;defined_ref;override_module_type;}letwith_constraints(table:Symbol_table.t)(modident:Longident.tLocation.loc)rewrite_context(symbols:Symbol_set.t)=letloc=modident.locinassert(String_set.is_emptysymbols.module_types);lettype_constraints=String_set.foldbeginfuntype_nameaccu:Parsetree.with_constraintlist->matchString_map.find_opttype_nametable.typeswith|None->accu|Somedecl->lettype_name:stringLocation.loc={loc;txt=type_name}inletqual_name=qualified_ident_of_namemodident.txttype_nameinletparams=decl.decl.ptype_paramsinletmanifest=Ast_helper.Typ.constrqual_name(params|>List.mapfst)inletty=Ast_helper.Type.mk~params~manifesttype_nameinPwith_typesubst(ident_of_nametype_name,ty)::accuendsymbols.types[]inletmodule_constraints=String_set.foldbeginfunmod_nameaccu:Parsetree.with_constraintlist->matchString_map.find_optmod_nametable.moduleswith|None->accu|Sometyped_decl->letmod_name:stringLocation.loc={loc;txt=mod_name}inletqual_name=qualified_ident_of_namemodident.txtmod_nameinPwith_modsubst(ident_of_namemod_name,qual_name)::accuendsymbols.modulestype_constraintsinmodule_constraintsletapply_rewrite_attr~loc?modidentrewrite_system_reftype_decls=type_decls|>List.filter_mapbeginfun(decl:Parsetree.type_declaration)->matchZipper.find(attr_name_isattr_rewrite)decl.ptype_attributeswith|Some({current={attr_payload=PStr[];_};_}aszipper)->beginmatchrewrite_system_refwith|None->Location.raise_errorf~loc:decl.ptype_loc"[@@rewrite] should appear in the scope of [%%override] or [%%import] or [%%include] or [%%rewrite]."|Somerewrite_system_ref->letdecl_pattern=Ast_helper.Typ.constr(ident_of_namedecl.ptype_name)(List.mapfstdecl.ptype_params)inifhas_attrattr_removedecl.ptype_attributesthenletrhs=matchfind_attr_type~loc:decl.ptype_locattr_fromdecl.ptype_attributeswith|Some(_zipper,rhs)->rhs|None->assertfalseinletrule=ifhas_attr"rhs_to_lhs"decl.ptype_attributesthenrhs,decl_patternelsedecl_pattern,rhsinrewrite_system_ref:=rule::!rewrite_system_ref;Noneelseletptype_attributes=Zipper.popzipperinletlhs,ptype_attributes=matchfind_attr_type~loc:decl.ptype_locattr_fromptype_attributeswith|Some(zipper,lhs)->lhs,Zipper.popzipper|None->letlhs=matchdecl.ptype_manifestwith|None->Location.raise_errorf~loc:decl.ptype_loc"[@@rewrite] needs a manifest"|Somemanifest->manifestinletlhs=matchmodidentwith|None->lhs|Somemodident->lhs|>map_typ_constr_ident(map_loc(remove_prefixmodident))inlhs,ptype_attributesinrewrite_system_ref:=(lhs,decl_pattern)::!rewrite_system_ref;Some{declwithptype_attributes}end|_->Somedeclendlettype_decls_has_co(type_decls:Parsetree.type_declarationlist)=matchList.revtype_declswith|{ptype_name={txt="co";_};ptype_manifest=None;ptype_attributes;_}::((_::_)asothers)whennot(has_attrattr_fromptype_attributes)->others,Someptype_attributes|_->type_decls,Noneletlist_type_decls_to_importmapmodidenttype_decls=type_decls|>List.mapbeginfun(pdecl:Parsetree.type_declaration)->letloc=pdecl.ptype_locinbeginmatchpdecl.ptype_manifestwith|Some[%type:_]|None->()|_->Location.raise_errorf~loc"Types to import should have no manifest"end;letfrom_name,attrs=matchfind_attr_type~locattr_frompdecl.ptype_attributeswith|None->pdecl.ptype_name,pdecl.ptype_attributes|Some(zipper,{ptyp_desc=Ptyp_constr({txt=Lidentname;loc},[]);_})->{loc;txt=name},ifhas_attrattr_rewritepdecl.ptype_attributesthenpdecl.ptype_attributeselseZipper.popzipper|_->Location.raise_errorf~loc"%s: Type name expected"override_nameinletdecl=find_typefrom_namemapmodidentin{new_name=pdecl.ptype_name;attrs;decl;pdecl=Somepdecl;loc;params=Some(List.mapfstpdecl.ptype_params)}endletinclude_co_in_type_listattrstype_list=lettypes_already_there=List.fold_left(funsetimport->String_set.addimport.decl.decl.ptype_name.txtset)String_set.emptytype_listinlettype_list,_types_already_there=List.fold_leftbeginfunaccuimport->List.fold_leftbeginfunaccu(decl:Symbol_table.type_decl)->lettype_list,types_already_there=accuinifString_set.memdecl.decl.ptype_name.txttypes_already_therethenaccuelseimport_of_decl~loc:import.locdeclattrs::type_list,String_set.adddecl.decl.ptype_name.txttypes_already_thereendaccuimport.decl.rec_group.declsend(type_list,types_already_there)type_listintype_listletdecl_has_attrattr(decl:Parsetree.type_declaration)=has_attrattrdecl.ptype_attributesletmodident_if_manifest_and_not_self_reference~manifestmodident=ifnotmanifest||is_self_referencemodidentthenNoneelseSomemodidentletprepare_type_declsmaptype_declsmodidentmktypeoverriden_refdefined_ref~manifestrewrite_context=letmodident_opt=modident_if_manifest_and_not_self_reference~manifestmodidentinlettype_decls',and_co=type_decls_has_cotype_declsinlettype_decls=iftype_decls|>List.existsbeginfun(decl:Parsetree.type_declaration)->matchdecl.ptype_manifestwith|Some[%type:_]->true|_->falseendthenlettype_list=list_type_decls_to_importmapmodidenttype_decls'inlettype_list=matchand_cowith|None->type_list|Someattrs->include_co_in_type_listattrstype_listinletrewrite_context=List.fold_leftprepare_import_rewrite_contextrewrite_contexttype_listintype_list|>List.mapbeginfunimport->import_type_declimportmodident_optrewrite_contextoverriden_refdefined_refendelseiftype_decls|>List.exists(decl_has_attrattr_remove)thenlettype_list=matchand_cowith|None->type_decls'|>List.mapbeginfun(decl:Parsetree.type_declaration)->decl.ptype_name,String_map.find_optdecl.ptype_name.txtmap,decl.ptype_loc,Somedeclend|Someattrs->list_type_decls_to_importmapmodidenttype_decls'|>include_co_in_type_listattrs|>List.map(fun{decl;loc;pdecl;_}->(decl.decl.ptype_name,Somedecl,loc,pdecl))inbegintype_list|>List.iterbeginfun(_,(decl:Symbol_table.type_decloption),_,_)->matchdeclwith|None->()|Somedecl->decl.imported<-trueendend;iftype_decls|>List.exists(decl_has_attrattr_rewrite)thentype_list|>List.mapbeginfun(name,decl,loc,(pdecl:Parsetree.type_declarationoption))->Ast_helper.with_default_loclocbeginfun()->letfrom_type,params=matchpdeclwith|Some{ptype_manifest=Somemanifest;ptype_params;_}->manifest,ptype_params|_->matchdeclwith|None->not_foundkind_typenamemodident|Some(decl:Symbol_table.type_decl)->matchdecl.decl.ptype_manifestwith|None->Location.raise_errorf~loc"Manifest expected"|Sometyp->typ,decl.decl.ptype_paramsinletfrom_type=letmapper=mapper_of_rewrite_contextrewrite_contextinmapper.typmapperfrom_typeinoverriden_ref:=Symbol_set.add_typename.txt!overriden_ref;defined_ref:=Symbol_set.add_typename.txt!defined_ref;letattrs=matchpdeclwith|None->[]|Somedecl->decl.ptype_attributesinAst_helper.Type.mkname~params~attrs:(([Ast_helper.Attr.mk(Parsetree_of_types.mklocattr_from)(PTypfrom_type);Ast_helper.Attr.mk(Parsetree_of_types.mklocattr_rewrite)(PStr[]);Ast_helper.Attr.mk(Parsetree_of_types.mklocattr_remove)(PStr[])])@attrs)endendelse[]elsebegintype_decls'|>List.iterbeginfun(decl:Parsetree.type_declaration)->beginmatchString_map.find_optdecl.ptype_name.txtmapwith|None->()|Somedecl->decl.imported<-trueend;overriden_ref:=Symbol_set.add_typedecl.ptype_name.txt!overriden_ref;defined_ref:=Symbol_set.add_typedecl.ptype_name.txt!defined_ref;end;type_declsendiniftype_decls=[]then[]else[mktypetype_decls]letsymbols_only_allowed_in_signatures~loc()=Location.raise_errorf~loc"[%%symbols] only allowed in signatures"letfilter_signature(sig_:Parsetree.signature)(symbols:Symbol_set.t):Parsetree.signature=sig_|>List.filter_mapbeginfun(item:Parsetree.signature_item)->matchitem.psig_descwith|Psig_type(rec_flag,decls)->beginmatchdecls|>List.filterbeginfun(decl:Parsetree.type_declaration)->not(String_set.memdecl.ptype_name.txtsymbols.types)endwith|[]->None|decls->Some{itemwithpsig_desc=Psig_type(rec_flag,decls)}end|Psig_moduledecl->ifString_set.memdecl.pmd_name.txtsymbols.modulesthenNoneelseSomeitem|Psig_recmoduledecls->beginmatchdecls|>List.filterbeginfun(decl:Parsetree.module_declaration)->not(String_set.memdecl.pmd_name.txtsymbols.modules)endwith|[]->None|decls->Some{itemwithpsig_desc=Psig_recmoduledecls}end|Psig_modtypedecl->ifString_set.memdecl.pmtd_name.txtsymbols.module_typesthenNoneelseSomeitem|_->SomeitemendmoduleMake_mapper(Wrapper:Ast_wrapper.S)=structletmake_recursive~loccontentsattributes=letrecextract_type_declscontents=contents|>flatten_mapbeginfunitem->letdesc=Wrapper.destructiteminmatchmatchdesc.txtwith|Includeinc->beginmatch(Wrapper.destruct_module_exprinc.pincl_mod).txt.contentswith|Contentscontents->Some(extract_type_declscontents)|_->Noneend|Type(_,type_decls)->Sometype_decls|_->Nonewith|Sometype_decls->type_decls|None->Location.raise_errorf~loc:desc.loc"%s: Only type declaration expected."recursive_nameendinmatchextract_type_declscontentswith|[]->None|hd::tl->letptype_attributes=attributes@hd.ptype_attributesinlettype_decls={hdwithptype_attributes}::tlinSome(Wrapper.build{loc;txt=Type(Recursive,type_decls)})letinclude_module~loc(expr:Wrapper.module_expr):Wrapper.item=Wrapper.build{loc;txt=Include(Ast_helper.Incl.mk~locexpr)}letinclude_module_type~loc(modtype:Parsetree.module_type):Wrapper.item=letmodtype=Wrapper.choose_module_expr(fun()->Location.raise_errorf~loc"Module types can only be included in signatures")(fun()->modtype)inWrapper.build{loc;txt=Include(Ast_helper.Incl.mk~locmodtype)}letstructure_of_contents~loccontents=Wrapper.build_module_expr(Wrapper.mkattr~loc(Wrapper.Contentscontents))letbind_module~locnameexpr=Wrapper.build{loc;txt=Module(Wrapper.build_module_binding(Wrapper.mkattr~loc{Wrapper.name;expr}))}letmodule_of_ident~locident=Wrapper.build_module_expr(Wrapper.mkattr~loc(Wrapper.Identident))typemodule_or_modtype=|ModuleofWrapper.wrapped_module_binding|ModtypeofParsetree.module_type_declarationletmodule_or_modtype_of_payload~locpayload=letpayload=matchWrapper.destruct_payload~locpayloadwith|[item]->Wrapper.destructitem|[]->Location.raise_errorf~loc"No module given"|_::_->Location.raise_errorf~loc"Only one module expected"inmatchpayload.txtwith|Modulebinding->Module(Wrapper.destruct_module_bindingbinding)|Modtypemodtype->Modtypemodtype|_->Location.raise_errorf~loc:payload.loc"Module or module type expected"letabstract_module_types_not_supported~loc=Location.raise_errorf~loc"Abstract module types are not supported."letimport_modtype_decl~locrewrite_context(modtype_decl:Parsetree.module_type_declaration)=letmapper=mapper_of_rewrite_contextrewrite_contextinletmodtype_decl=modtype_decl|>mapper.module_type_declarationmapperinWrapper.build{loc;txt=Modtypemodtype_decl}letoverride~loc(rewrite_env:rewrite_env)(context:override_context)override_itemitembind_iteminclude_item=letitem=override_itemcontextiteminletresult=ifcontext.mode.submodulethenbind_itemitemelseinclude_itemiteminpromote_rewrite~loccontext.modenv.modtyperewrite_env.contextrewrite_env.rewrite_system_ref(Lidentcontext.name)context.mode.submodule!(context.overriden_ref)!(context.defined_ref)!(context.rewrite_env.rewrite_system_ref);resultletmk_type~loccontextrec_flagtype_decls=lettype_decls=apply_rewrite_attr~loc~modident:context.modenv.ident.txt(Somecontext.rewrite_env.rewrite_system_ref)type_declsiniftype_decls=[]thenWrapper.empty~locelseWrapper.build{loc;txt=Type(rec_flag,type_decls)}letimport_symbols_from_signature~loc~only_typescontextattrsenv(signature:Symbol_table.signature)=letmodident=modident_if_manifest_and_not_self_reference~manifest:context.manifestcontext.modenv.ident.txtinletrewrite_context=current_rewrite_contextcontext.rewrite_envinsignature.items|>List.filter_mapbeginfun(item:Symbol_table.item)->beginmatchitemwith|Typegroup->letrev_override_attrs,rev_other_attrs=attrs|>List.fold_leftbeginfun(rev_override_attrs,rev_other_attrs)(attr:Parsetree.attribute)->letattr_name=attr.attr_name.txtinifattr_name=attr_from||attr_name=attr_rewrite||attr_name=attr_removethen(attr::rev_override_attrs,rev_other_attrs)else(rev_override_attrs,attr::rev_other_attrs)end([],[])inletoverride_attrs=List.revrev_override_attrsinletother_attrs=List.revrev_other_attrsinbeginmatchdecl_of_list~locoverride_attrsmodidentrewrite_contextgroup.declscontext.overriden_refcontext.defined_refwith|[]->None|hd::tl->letptype_attributes=other_attrs@hd.ptype_attributesinletdecls={hdwithptype_attributes}::tlinSome(mk_type~loccontextgroup.rec_flagdecls)end|Modtypedecl->ifdecl.importedthenNoneelseletitem=import_modtype_decl~locrewrite_contextdecl.declindecl.imported<-true;Someitem|Valuedecl->ifonly_typesthenNoneelseletitem=Wrapper.choose(symbols_only_allowed_in_signatures~loc)(fun()->letmapper=mapper_of_rewrite_contextrewrite_contextinAst_helper.Sig.value(mapper.value_descriptionmapperdecl))inSomeitem|Modulegroup->ifonly_typesthenNoneelseletitem=Wrapper.choose(symbols_only_allowed_in_signatures~loc)beginfun()->letmapper=mapper_of_rewrite_contextrewrite_contextinletdecls=group.decls|>List.map(mapper.module_declarationmapper)inmatchgroup.rec_flagwith|Recursive->Ast_helper.Sig.rec_moduledecls|Nonrecursive->matchdeclswith|[decl]->Ast_helper.Sig.module_decl|_->assertfalseendinSomeitemendendletrecoverride_module(rewrite_env:rewrite_env)(context:override_context)(desc:Wrapper.wrapped_module_binding)=letloc=desc.locinoverride~locrewrite_envcontextoverride_module_exprdesc.txt.contents.expr(bind_module~locdesc.txt.contents.name)(include_module~loc)andoverride_module_type(rewrite_env:rewrite_env)(context:override_context)(desc:Parsetree.module_type_declaration)=letloc=desc.pmtd_locinletmod_type=matchdesc.pmtd_typewith|None->abstract_module_types_not_supported~loc|Somemod_type->mod_typeinoverride~locrewrite_envcontextcontext.override_module_typemod_type(funitem->letmodtypedecl:Parsetree.module_type_declaration={descwithpmtd_type=Someitem}inWrapper.build{loc;txt=Modtypemodtypedecl})(include_module_type~loc)andoverride_module_expr(context:override_context)(expr:Wrapper.module_expr)=matchWrapper.destruct_module_exprexprwith{loc;txt={attrs;contents}}->matchcontentswith|Contentscontents->letsignature=context.modenv.modtype|>Option.mapbeginfunenv->letsignature=extract_signature~locenvcontext.modenv.ident.txt|>Symbol_table.of_signatureinletscope=Symbol_table.import~target:env.scope~source:signature.tablein{envwithsignature;scope}endinletcontents=override_contentscontextsignaturecontentsinletmodule_expr=module_of_ident~loccontext.modenv.identinletmodident=Ast_wrapper.module_expr_of_longidentcontext.modenv.identinlettype_of()=Ast_helper.Mty.typeof_(Ast_helper.Mod.structure[Ast_helper.Str.include_(Ast_helper.Incl.mkmodident)])inletmake_with_constraintswith_constraints=matchwith_constraintswith|[]->None|_->Some(Ast_helper.Mty.with_(type_of())with_constraints)inletmake_includesig_constraint=letmodule_expr=matchsig_constraintwith|None->Wrapper.choose_module_expr(fun()->modident)(fun()->type_of())|Somesig_constraint->Wrapper.build_module_expr(Wrapper.mkattr~loc(Wrapper.Constraint(Lazy.from_valmodule_expr,sig_constraint)))ininclude_module~locmodule_exprinletcontents=matchcontext.mode.import,signaturewith|(Not_include|Ignore),_->contents|Include,None->make_includeNone::contents(* ocamldep *)|Include,Some{signature;_}->ifsignature.table.only_types&&signature.table.types|>String_map.for_allbeginfun_(decl:Symbol_table.type_decl)->decl.importedend&&signature.table.module_types|>String_map.for_allbeginfun_(decl:Symbol_table.modtype_decl)->decl.importedendthencontentselseletrewrite_context=current_rewrite_contextcontext.rewrite_envinletsymbols=Symbol_set.union!(context.overriden_ref)!(context.defined_ref)inletsig_constraint=ifString_set.is_emptysymbols.module_typesthenwith_constraintssignature.tablecontext.modenv.identrewrite_contextsymbols|>make_with_constraintselseSome(Ast_helper.Mty.signature(filter_signaturesignature.sig_symbols))inmake_includesig_constraint::contentsinstructure_of_contents~loccontents|Functor(x,t,e)->letcontext=matchcontext.mode.importwith|Ignore->context|_->lety,modenv=apply_functor~loccontext.modenvx.txtinletrewrite_env=matchywith|None->context.rewrite_env|Somey->{context.rewrite_envwithcontext={context.rewrite_env.contextwithsubst_mod=Longident_map.add(Longident.Lidenty.txt)(Longident.Lidentx.txt)context.rewrite_env.context.subst_mod}}in{contextwithmodenv;rewrite_env}inlete'=override_module_exprcontexteinifcontext.mode.submodulethenWrapper.build_module_expr(Wrapper.mkattr~loc(Wrapper.Functor(x,t,e')))elsee'|Constraint(e,t)->lete'=lazy(override_module_exprcontext(Lazy.forcee))inWrapper.build_module_expr(Wrapper.mkattr~loc(Wrapper.Constraint(e',t)))|_->Location.raise_errorf~loc"%s: Only functors and structures are supported."override_nameandoverride_contents(context:override_context)(env:Symbol_table.signatureenvoption)(contents:Wrapper.contents)=contents|>flatten_mapbeginfun(item:Wrapper.item)->letitem_desc=Wrapper.destructiteminletloc=item_desc.locinmatchitem_desc.txt,envwith|Type(rec_flag,type_decls),Some{signature;_}->letrewrite_context=current_rewrite_contextcontext.rewrite_envinprepare_type_declssignature.table.typestype_declscontext.modenv.ident.txt(mk_type~loccontextrec_flag)context.overriden_refcontext.defined_refrewrite_context~manifest:context.manifest|Modulebinding,_->letdesc=Wrapper.destruct_module_bindingbindinginletmode={import=Ignore;submodule=true}in[override_submodulecontextenvmode(Moduledesc)[]]|Modtypedeclaration,Some{signature;_}->ifString_map.memdeclaration.pmtd_name.txtsignature.table.module_typesthenbegincontext.overriden_ref:=Symbol_set.add_module_typedeclaration.pmtd_name.txt!(context.overriden_ref)end;[item]|Extension(({txt="rewrite_module";_},PStr[%str[%e?lhs]=[%e?rhs]]),attrs),_->letget_construct(e:Parsetree.expression)=matche.pexp_descwith|Pexp_construct(lid,None)->lid.txt|_->Location.raise_errorf~loc:e.pexp_loc"Module name expected"inletlhs=get_constructlhsinletrhs=get_constructrhsincontext.rewrite_env.subst_mod_ref:=Longident_map.addlhsrhs!(context.rewrite_env.subst_mod_ref);[]|Extension(({txt="types";_},PStr[]),attrs),Some{env;signature;_}->import_symbols_from_signature~loc~only_types:truecontextattrsenvsignature|Extension(({txt="symbols";_},PStr[]),attrs),Some{env;signature;_}->import_symbols_from_signature~loc~only_types:falsecontextattrsenvsignature|Extension(({txt="rewrite";_},payload),attrs),_->letcontext={contextwithrewrite_env=derive_rewrite_envcontext.rewrite_env}inWrapper.destruct_payload~locpayload|>override_contentscontextenv|Extension(({txt="recursive";_},payload),attrs),_->letcontents=Wrapper.destruct_payload~locpayload|>override_contentscontextenvinifcontext.modenv.modtype=Nonethen(* ocamldep *)contentselseOption.to_list(make_recursive~loccontentsattrs)|Extension(({txt="print_rewrite_system";_},_payload),_attrs),_->letrewrite_context=current_rewrite_contextcontext.rewrite_envinrewrite_context.rewrite_system|>List.iter(fun(lhs,rhs)->Format.fprintfFormat.err_formatter"%a -> %a@."Pprintast.core_type(From.copy_core_typelhs)Pprintast.core_type(From.copy_core_typerhs));[]|Extension((extension_name,payload),attrs),_->beginmatchmode_of_stringextension_name.txtwith|exception(Invalid_argument_)->[item]|mode->letsubmodule=module_or_modtype_of_payload~locpayloadinletmanifest=matchsubmodulewith|Module_->true|Modtype_->falseinletcontext={contextwithmanifest}in[override_submodulecontextenvmodesubmoduleattrs]end|_->[item]endandoverride_submodule(context:override_context)(env:Symbol_table.signatureenvoption)(mode:mode)(submodule:module_or_modtype)attrs=letname,add_module,find_module,override_module=matchsubmodulewith|Moduledesc->desc.txt.contents.name,Symbol_set.add_module,(funname(signature:Symbol_table.signature)->(find_modulenamesignature.table.modulescontext.modenv.ident.txt).pmd_type),(funcontext'->override_modulecontext.rewrite_envcontext'desc)|Modtypedecl->decl.pmtd_name,Symbol_set.add_module_type,(funname(signature:Symbol_table.signature)->match(find_module_typenamesignature.table.module_typescontext.modenv.ident.txt).decl.pmtd_typewith|None->abstract_module_types_not_supported~loc:decl.pmtd_loc|Somemodtype->modtype),(funcontext'->override_module_typecontext.rewrite_envcontext'decl)incontext.overriden_ref:=add_modulename.txt!(context.overriden_ref);letsubmodenv=matchmode.importwith|Ignore->context.modenv|Include|Not_include->letmodtype=matchenvwith|None->None|Someenv->Some{envwithsignature=find_modulenameenv.signature}in{ident=context.modenv.ident|>map_loc(funident:Longident.t->Ldot(ident,name.txt));modtype}inletdefined_ref=ifmode.submodulethenNoneelseSomecontext.defined_refinletcontext'=make_context?defined_ref~manifest:context.manifestsubmodenvname.txtmode(derive_rewrite_envcontext.rewrite_env)context.override_module_typeinoverride_modulecontext'letmapper(context:mapper_context)(mapper:mapper_context->Ast_mapper.mapper)(item:Wrapper.item)=letitem_desc=Wrapper.destructiteminletloc=item_desc.locinletresult=matchitem_desc.txtwith|Extension(({txt="rewrite";_},payload),attrs)->letrewrite_env=force_rewrite_envcontext.rewrite_env|>derive_rewrite_envinletmapper=mapper{contextwithrewrite_env=Somerewrite_env}inletcontents=Wrapper.destruct_payload~locpayload|>Wrapper.mapmappermapperininclude_module~loc(structure_of_contents~loccontents)|Extension(({txt="recursive";_},payload),attrs)->letrewrite_env=force_rewrite_envcontext.rewrite_envinletmapper=mapper{contextwithrewrite_env=Somerewrite_env}inletcontents=Wrapper.destruct_payload~locpayload|>Wrapper.mapmappermapperinifcontext.ocamldeptheninclude_module~loc(structure_of_contents~loccontents)elsebeginmatchmake_recursive~loccontentsattrswith|None->Wrapper.empty~loc|Someitem->itemend|Extension(({txt="print_rewrite_system";_},_payload),_attrs)->letrewrite_env=force_rewrite_envcontext.rewrite_envinletrewrite_context=current_rewrite_contextrewrite_envinrewrite_context.rewrite_system|>List.iter(fun(lhs,rhs)->Format.fprintfFormat.err_formatter"%a -> %a@."Pprintast.core_type(From.copy_core_typelhs)Pprintast.core_type(From.copy_core_typerhs));Wrapper.empty~loc|Extension((extension_name,payload),attrs)->beginmatchmode_of_stringextension_name.txtwith|exception(Invalid_argument_)->item|mode->matchmodule_or_modtype_of_payload~locpayloadwith|Moduledesc->letname=desc.txt.contents.nameinletrewrite_env=force_rewrite_envcontext.rewrite_envinletmodenv=letident=ident_of_namenameinletmodtype=ifcontext.ocamldepthenNoneelseletenv=Lazy.forcelazy_envinSome{env;signature=Parsetree_of_types.module_type(locate_sigenvident);scope=Symbol_table.empty}in{ident;modtype}inletrewrite_env'=derive_rewrite_envrewrite_envinletcontext=make_contextmodenvname.txtmoderewrite_env'context.override_module_type~manifest:trueinoverride_modulerewrite_envcontextdesc|Modtypedecl->Location.raise_errorf~loc"Module types cannot be compilation unit"end|Type(rec_flag,type_decls)->letrewrite_system_ref=context.rewrite_env|>Option.mapbeginfunenv->env.rewrite_system_refendinlettype_decls=apply_rewrite_attr~locrewrite_system_reftype_declsinWrapper.build{loc;txt=Type(rec_flag,type_decls)}|_->Wrapper.map_itemAst_mapper.default_mapper(mappercontext)iteminresultendmoduleStructure_mapper=Make_mapper(Ast_wrapper.Structure)moduleSignature_mapper=Make_mapper(Ast_wrapper.Signature)letrecmake_mapper(context:mapper_context):Ast_mapper.mapper={Ast_mapper.default_mapperwithstructure_item=(fun_mapper->Structure_mapper.mappercontextmake_mapper);signature_item=(fun_mapper->Signature_mapper.mappercontextmake_mapper);}let()=Migrate_parsetree.Driver.register~name:"override"~position:(-10)(moduleOCaml_version)(funconfig_->make_mapper{ocamldep=config.tool_name="ocamldep";rewrite_env=None;override_module_type=Signature_mapper.override_module_expr})