123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320(**************************************************************************)(* *)(* Copyright 2013 OCamlPro *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the Lesser GNU Public License version 3.0. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* Lesser GNU General Public License for more details. *)(* *)(**************************************************************************)(* - Types - *)openIndexTypes(* This type is used to pass the full structure to functions building sub-trees,
so that they can (lazily) lookup type or module type indirections in scope.
Values of this type should have the form {[
[ [Module;Submodule], lazy subtrie_at_Module.Submodule;
[Module], lazy subtrie_at_Module;
[], lazy subtrie_at_Root ]
]}
*)typeparents=(stringlist*tLazy.t)list(* - Utility functions - *)openIndexMiscletorig_file_name=function|Cmtf|Cmtif|Cmif->fletequal_kindk1k2=matchk1,k2with|Type,Type|Value,Value|Exception,Exception|OpenType,OpenType|Field_,Field_|Variant_,Variant_|Method_,Method_|Module,Module|ModuleType,ModuleType|Class,Class|ClassType,ClassType|Keyword,Keyword->true|Type,_|Value,_|Exception,_|OpenType,_|Field_,_|Variant_,_|Method_,_|Module,_|ModuleType,_|Class,_|ClassType,_|Keyword,_->falselethas_kindkinfo=equal_kindkinfo.kind(* - Trie loading and manipulation functions - *)(* Used as path separator *)letdot=char_of_int0letfix_path_prefixstripnew_pfx=letrectlnn=function|[]->[]|_::tlasl->ifn>0thentln(n-1)tlelselinletrev_pfx=List.revnew_pfxinfunid->{idwithpath=List.rev_appendrev_pfx(tlnstripid.path)}letoverriding_merget1t2=letf=(Trie.filter_keys((<>)dot)t2)inTrie.fold0(funtpathvalues->lett=List.fold_left(funtv->Trie.addtpathv)(Trie.unsettpath)valuesinifList.exists(function|{kind=Module|ModuleType|Class|ClassType}->true|_->false)valuesthenletsubpath=path@[dot]inTrie.graft_lazytsubpath(lazy(Trie.subt2subpath))elset)ft1letopen_module?(cleanup_path=false)tpath=letstrip_path=fix_path_prefix(List.lengthpath)[]inletsubmodule=Trie.subt(modpath_to_keypath)inletsubmodule=ifcleanup_paththenTrie.map(fun_key->strip_path)submoduleelsesubmodulein(* Trie.merge ~values:(fun _ v -> v) t submodule -- keeps hidden values in subtrees *)overriding_mergetsubmoduleletalias?(cleanup_path=false)toriginalias=letsubtree=Trie.subt(modpath_to_keyorigin)inletsubtree=letstrip_path=fix_path_prefix(List.lengthorigin)aliasinifcleanup_paththenTrie.map(fun_key->strip_path)subtreeelsesubtreeinTrie.graftt(modpath_to_keyalias)subtree(* Pops comments from a list of comments (string * loc) to find the ones that
are associated to a given location. Also returns the remaining comments after
the location. *)letassociate_comment?(after_only=false)commentslocnextloc=ifloc=Location.nonethenNone,commentselseletlstart=loc.Location.loc_start.Lexing.pos_lnumandlend=loc.Location.loc_end.Lexing.pos_lnuminletisnextc=nextloc<>Location.none&&nextloc.Location.loc_start.Lexing.pos_cnum<c.Location.loc_end.Lexing.pos_cnuminletrecaux=function|[]->None,[]|(comment,cloc)::comments->letcstart=cloc.Location.loc_start.Lexing.pos_lnumandcend=cloc.Location.loc_end.Lexing.pos_lnuminifcend<lstart-1||cstart<lend&&after_onlythenauxcommentselseifcstart>lend+1||isnextcloc||cstart>lstart&&cend<lend(* keep inner comments *)thenNone,(comment,cloc)::commentselseifString.lengthcomment<2||comment.[0]<>'*'||comment.[1]='*'thenauxcommentselseletcomment=String.trim(String.subcomment1(String.lengthcomment-1))inmatchauxcommentswith|None,comments->Somecomment,comments|Somec,comments->Some(String.concat"\n"[comment;c]),commentsinauxcommentsletty_of_sig_item=letopenPrinttypinfunction#ifOCAML_VERSION<"4.08"|Types.Sig_value(id,decl)->tree_of_value_descriptioniddecl|Types.Sig_type(id,decl,rs)->tree_of_type_declarationiddeclrs|Types.Sig_typext(id,decl,es)->tree_of_extension_constructoriddecles|Types.Sig_module(id,{Types.md_type},rs)->tree_of_moduleidmd_typers|Types.Sig_modtype(id,decl)->tree_of_modtype_declarationiddecl|Types.Sig_class(id,decl,rs)->tree_of_class_declarationiddeclrs|Types.Sig_class_type(id,decl,rs)->tree_of_cltype_declarationiddeclrs#else|Types.Sig_value(id,decl,_)->tree_of_value_descriptioniddecl|Types.Sig_type(id,decl,rs,_)->tree_of_type_declarationiddeclrs|Types.Sig_typext(id,decl,es,_)->tree_of_extension_constructoriddecles|Types.Sig_module(id,_,{Types.md_type},rs,_)->tree_of_moduleidmd_typers|Types.Sig_modtype(id,decl,_)->tree_of_modtype_declarationiddecl|Types.Sig_class(id,decl,rs,_)->tree_of_class_declarationiddeclrs|Types.Sig_class_type(id,decl,rs,_)->tree_of_cltype_declarationiddeclrs#endif(* -- Qualifying types -- *)#ifOCAML_VERSION>="4.08"letns={Outcometree.printed_name=s}letnn{Outcometree.printed_name}=printed_name#elseletns=sletnns=s#endif(* The types may contain unqualified identifiers.
We need to do some (lazy) lookup in the trie to qualify them, so that
for example [M.empty] shows up as [M.t] and not just [t] *)letqualify_ty(parents:parents)ty=letqualifyident=letpath=letrecget_path=function|Outcometree.Oide_identname->[nnname]|Outcometree.Oide_dot(path,s)->get_pathpath@[s]|Outcometree.Oide_apply(p1,_p2)->get_pathp1inget_pathidentinletkey=modpath_to_key~enddot:falsepathinletreclookup=function|[]|([],_)::_->ident|((path1::pathn),lazyt)::parents->ifnot(List.exists(has_kindType)(Trie.find_alltkey))thenlookupparentselseletrecadd_pfx=function|Outcometree.Oide_dot(idp,s)->Outcometree.Oide_dot(add_pfxidp,s)|Outcometree.Oide_apply(idp,idp2)->Outcometree.Oide_apply(add_pfxidp,idp2)|Outcometree.Oide_idents->letparentpath=List.fold_left(funaccmodl->Outcometree.Oide_dot(acc,modl))(Outcometree.Oide_ident(npath1))pathninOutcometree.Oide_dot(parentpath,nns)inadd_pfxidentinlookupparentsinletrecaux=(* Some kind of Outcometree.map_ty *)letopenOutcometreeinfunction|Otyp_abstract->Otyp_abstract|Otyp_alias(ty,str)->Otyp_alias(auxty,str)|Otyp_arrow(str,ty1,ty2)->Otyp_arrow(str,auxty1,auxty2)|Otyp_class(bl,id,tylist)->Otyp_class(bl,qualifyid,List.mapauxtylist)|Otyp_constr(id,tylist)->Otyp_constr(qualifyid,List.mapauxtylist)|Otyp_manifest(ty1,ty2)->Otyp_manifest(auxty1,auxty2)|Otyp_object(strtylist,blopt)->Otyp_object(List.map(fun(str,ty)->str,auxty)strtylist,blopt)|Otyp_record(strbltylist)->Otyp_record(List.map(fun(str,bl,ty)->str,bl,auxty)strbltylist)|Otyp_stuffstr->Otyp_stuffstr|Otyp_sum(strtylisttyoptlist)->Otyp_sum(List.map(fun(str,tylist,tyopt)->str,List.mapauxtylist,matchtyoptwithSomety->Some(auxty)|None->None)strtylisttyoptlist)|Otyp_tuple(tylist)->Otyp_tuple(List.mapauxtylist)|Otyp_var(bl,str)->Otyp_var(bl,str)|Otyp_variant(bl,var,bl2,strlistopt)->Otyp_variant(bl,var,bl2,strlistopt)|Otyp_poly(str,ty)->Otyp_poly(str,auxty)|Otyp_module(str,strl,tylist)->Otyp_module(str,strl,List.mapauxtylist)|Otyp_open->Otyp_open#ifOCAML_VERSION>="4.03"|Otyp_attribute(ty,attr)->Otyp_attribute(auxty,attr)#endifinauxtyletqualify_ty_in_sig_item(parents:parents)=letqual=qualify_typarentsinletopenOutcometreeinfunction|Osig_type(out_type_decl,rc)->Osig_type({out_type_declwithotype_type=qualout_type_decl.otype_type;otype_cstrs=List.map(fun(ty1,ty2)->qualty1,qualty2)out_type_decl.otype_cstrs},rc)#ifOCAML_VERSION>="4.03"|Osig_valueo->Osig_value{owithoval_type=qualo.oval_type}#else|Osig_value(str,ty,str2)->Osig_value(str,qualty,str2)#endif|Osig_typext(constr,es)->Osig_typext({constrwithoext_args=List.mapqualconstr.oext_args},es)|out_sig->out_sig(* don't get down in modules, classes and their types *)(* -- end -- *)letwith_path_loc?srcpathloc=matchsrcpathwith|None->loc|Somepath->letpath=(* Some magic to get the real source when using jbuilder or
ocamlbuild *)letlpath=string_splitFilename.dir_sep.[0]pathinletrecaux=function|"_build"::"default"::r->r|"_build"::r->r|p::r->p::auxr|[]->[]inString.concatFilename.dir_sep(auxlpath)inletwith_path_pospos=letopenLexinginifnot(Filename.is_relativepos.pos_fname)thenposelse{poswithpos_fname=Filename.concatpathpos.pos_fname}inletopenLocationin{locwithloc_start=with_path_posloc.loc_start;loc_end=with_path_posloc.loc_end}letloc_of_sig_item=function#ifOCAML_VERSION<"4.08"|Types.Sig_value(_,descr)->descr.Types.val_loc|Types.Sig_type(_,descr,_)->descr.Types.type_loc|Types.Sig_typext(_,descr,_)->descr.Types.ext_loc|Types.Sig_module(_,descr,_)->descr.Types.md_loc|Types.Sig_modtype(_,descr)->descr.Types.mtd_loc|Types.Sig_class(_,descr,_)->descr.Types.cty_loc|Types.Sig_class_type(_,descr,_)->descr.Types.clty_loc#else|Types.Sig_value(_,descr,_)->descr.Types.val_loc|Types.Sig_type(_,descr,_,_)->descr.Types.type_loc|Types.Sig_typext(_,descr,_,_)->descr.Types.ext_loc|Types.Sig_module(_,_,descr,_,_)->descr.Types.md_loc|Types.Sig_modtype(_,descr,_)->descr.Types.mtd_loc|Types.Sig_class(_,descr,_,_)->descr.Types.cty_loc|Types.Sig_class_type(_,descr,_,_)->descr.Types.clty_loc#endifletid_of_sig_item=function#ifOCAML_VERSION<"4.08"|Types.Sig_value(id,_)|Types.Sig_type(id,_,_)|Types.Sig_typext(id,_,_)|Types.Sig_module(id,_,_)|Types.Sig_modtype(id,_)|Types.Sig_class(id,_,_)|Types.Sig_class_type(id,_,_)#else|Types.Sig_value(id,_,_)|Types.Sig_type(id,_,_,_)|Types.Sig_typext(id,_,_,_)|Types.Sig_module(id,_,_,_,_)|Types.Sig_modtype(id,_,_)|Types.Sig_class(id,_,_,_)|Types.Sig_class_type(id,_,_,_)#endif->idletkind_of_sig_item=function|Types.Sig_value_->Value|Types.Sig_type_->Type#ifOCAML_VERSION<"4.08"|Types.Sig_typext(_,_,Types.Text_exception)->Exception#else|Types.Sig_typext(_,_,Types.Text_exception,_)->Exception#endif|Types.Sig_typext_->OpenType|Types.Sig_module_->Module|Types.Sig_modtype_->ModuleType|Types.Sig_class_->Class|Types.Sig_class_type_->ClassTypeletattrs_of_sig_item=function#ifOCAML_VERSION<"4.08"|Types.Sig_value(_,descr)->descr.Types.val_attributes|Types.Sig_type(_,descr,_)->descr.Types.type_attributes|Types.Sig_typext(_,descr,_)->descr.Types.ext_attributes|Types.Sig_module(_,descr,_)->descr.Types.md_attributes|Types.Sig_modtype(_,descr)->descr.Types.mtd_attributes|Types.Sig_class(_,descr,_)->descr.Types.cty_attributes|Types.Sig_class_type(_,descr,_)->descr.Types.clty_attributes#else|Types.Sig_value(_,descr,_)->descr.Types.val_attributes|Types.Sig_type(_,descr,_,_)->descr.Types.type_attributes|Types.Sig_typext(_,descr,_,_)->descr.Types.ext_attributes|Types.Sig_module(_,_,descr,_,_)->descr.Types.md_attributes|Types.Sig_modtype(_,descr,_)->descr.Types.mtd_attributes|Types.Sig_class(_,descr,_,_)->descr.Types.cty_attributes|Types.Sig_class_type(_,descr,_,_)->descr.Types.clty_attributes#endifletdoc_of_attributesattrs=letdoc_loc_id="ocaml.doc"in(* not exported ! *)letopenParsetreeintry#ifOCAML_VERSION>="4.08"matchList.find(fun{attr_name={Location.txt}}->txt=doc_loc_id)attrswith|{attr_payload=PStr[{pstr_desc=Pstr_eval({pexp_desc},_)}]}->#elsematchList.find(fun({Location.txt},_)->txt=doc_loc_id)attrswith|_,PStr[{pstr_desc=Pstr_eval({pexp_desc},_)}]->#endif(matchpexp_descwith#ifOCAML_VERSION>="4.03"|Pexp_constant(Pconst_string(s,_))->Somes#else|Pexp_constant(Const_string(s,_))->Somes#endif|_->debug"Unexpected ocaml.doc docstring format";None)|_->NonewithNot_found->Nonelettrie_of_type_decl?commentsinfoty_decl=matchty_decl.Types.type_kindwith|Types.Type_abstract->[],comments|Types.Type_open->[],comments|Types.Type_record(fields,_repr)->List.map(fun{Types.ld_id;ld_type;ld_attributes}->letty=Printtyp.tree_of_typexpfalseld_typeinletty=Outcometree.Osig_type(Outcometree.({otype_name="";otype_params=[];otype_type=ty;otype_private=Asttypes.Public;#ifOCAML_VERSION>="4.03"#ifOCAML_VERSION>="4.10"otype_immediate=Type_immediacy.Unknown;#elseotype_immediate=false;#endif#ifOCAML_VERSION>="4.04"otype_unboxed=false;#endif#endifotype_cstrs=[];}),Outcometree.Orec_not)inletdoc=doc_of_attributesld_attributesinletid_name=Ident.nameld_idinstring_to_keyid_name,Trie.create~value:{path=info.path;orig_path=info.path;kind=Fieldinfo;name=id_name;ty=Somety;loc_sig=info.loc_sig;loc_impl=info.loc_impl;doc=lazydoc;file=info.file;}())fields,comments|Types.Type_variantvariants->List.map(fun{Types.cd_id;cd_args;cd_attributes}->letty=letparams=matchcd_argswith#ifOCAML_VERSION>="4.03"|Cstr_tuple[]->Outcometree.Otyp_sum[]|Cstr_tuple(param::_asl)->Printtyp.tree_of_typexpfalse{Types.desc=Types.Ttuplel;level=param.Types.level;#ifOCAML_VERSION>="4.08"scope=0;#elifOCAML_VERSION>="4.07"scope=None;#endifid=param.Types.id}|Cstr_recordparams->Outcometree.Otyp_record(List.map(funl->(Ident.namel.Types.ld_id,l.ld_mutable=Mutable,Printtyp.tree_of_typexpfalsel.ld_type))params)#else|[]->Outcometree.Otyp_sum[]|param::_asl->Printtyp.tree_of_typexpfalse{Types.desc=Types.Ttuplel;level=param.Types.level;id=param.Types.id}#endifinOutcometree.Osig_type(Outcometree.({otype_name="";otype_params=[];otype_type=params;otype_private=Asttypes.Public;#ifOCAML_VERSION>="4.03"#ifOCAML_VERSION>="4.10"otype_immediate=Type_immediacy.Unknown;#elseotype_immediate=false;#endif#ifOCAML_VERSION>="4.04"otype_unboxed=false;#endif#endifotype_cstrs=[];}),Outcometree.Orec_not)inletdoc=doc_of_attributescd_attributesinletid_name=Ident.namecd_idinstring_to_keyid_name,Trie.create~value:{path=info.path;orig_path=info.path;kind=Variantinfo;name=id_name;ty=Somety;loc_sig=info.loc_sig;loc_impl=info.loc_impl;doc=lazydoc;file=info.file;}())variants,comments(** Implements looking up a module path in the parents list *)letlookup_parents(parents:parents)pathsig_path=letsig_key,path_key=matchsig_pathwith|hd::tl->modpath_to_key[hd],modpath_to_keytl|[]->assertfalseinletreclookup=function|[]->ifdebug_enabledthendebug"WARN: Module or sig reference %s not found a %s\n"(modpath_to_stringsig_path)(modpath_to_stringpath);Trie.empty|(parentpath,lazyt)::parents->lets=Trie.subtsig_keyinifs=Trie.emptythenlookupparentselselets=Trie.subspath_keyinletrewrite_path=fix_path_prefix(List.lengthparentpath+List.lengthsig_path)pathinTrie.map(fun_kv->rewrite_pathv)sinlookupparentsletrecpath_of_ocaml=function|Path.Pidentid->[Ident.nameid]#ifOCAML_VERSION>="4.08"|Path.Pdot(path,s)->path_of_ocamlpath@[s]#else|Path.Pdot(path,s,_)->path_of_ocamlpath@[s]#endif|Path.Papply(p1,_p2)->path_of_ocamlp1letrectrie_of_sig_item?comments?srcpathimplloc_trie(parents:parents)(orig_file:orig_file)pathsig_itemnext=letid=id_of_sig_itemsig_iteminletloc=with_path_loc?srcpath(loc_of_sig_itemsig_item)inletnextloc=matchnextwith|None->Location.none|Somen->with_path_loc?srcpath(loc_of_sig_itemn)inletdoc,comments=matchdoc_of_attributes(attrs_of_sig_itemsig_item),commentswith|Somes,_->lazy(Somes),comments|None,None->lazyNone,None|None,Somecomments->letassoc=lazy(associate_comment(Lazy.forcecomments)locnextloc)inlazy(fst(Lazy.forceassoc)),Some(lazy(snd(Lazy.forceassoc)))inletty=Some(ty_of_sig_itemsig_item)inletkind=kind_of_sig_itemsig_iteminletloc_sig=lazylocinletloc_impl=lazy(matchimplloc_triewith|lazyNone->loc|lazy(Somet)->tryletpath=List.tlpath@[Ident.nameid]inletkey=modpath_to_key~enddot:falsepathinletc=List.find(has_kindkind)(Trie.find_alltkey)inLazy.forcec.loc_implwithNot_found->Location.none)inletinfo={path;orig_path=path;kind;name=Ident.nameid;ty;loc_sig;loc_impl;doc;file=orig_file}inletsiblings,comments=(* read fields / variants ... *)matchsig_itemwith#ifOCAML_VERSION>="4.08"|Types.Sig_type(_id,descr,_is_rec,_)->#else|Types.Sig_type(_id,descr,_is_rec)->#endiftrie_of_type_decl?commentsinfodescr|_->[],commentsin(* ignore functor arguments *)letrecsig_item_contents=function|Types.Sig_module#ifOCAML_VERSION>="4.08"(id,presence,#ifOCAML_VERSION>="4.10"({Types.md_type=Types.Mty_functor(_,s)}asfunct),#else({Types.md_type=Types.Mty_functor(_,_,s)}asfunct),#endifis_rec,visibility)->letfunct={functwithTypes.md_type=s}insig_item_contents(Types.Sig_module(id,presence,funct,is_rec,visibility))#else(id,({Types.md_type=Types.Mty_functor(_,_,s)}asfunct),is_rec)->letfunct={functwithTypes.md_type=s}insig_item_contents(Types.Sig_module(id,funct,is_rec))#endif|Types.Sig_modtype#ifOCAML_VERSION>="4.08"#ifOCAML_VERSION>="4.10"(id,({Types.mtd_type=Some(Types.Mty_functor(_,s))}asfunct),visibility)#else(id,({Types.mtd_type=Some(Types.Mty_functor(_,_,s))}asfunct),visibility)#endif->letfunct={functwithTypes.mtd_type=Somes}insig_item_contents(Types.Sig_modtype(id,funct,visibility))#else(id,({Types.mtd_type=Some(Types.Mty_functor(_,_,s))}asfunct))->letfunct={functwithTypes.mtd_type=Somes}insig_item_contents(Types.Sig_modtype(id,funct))#endif|si->siin(* read module / class contents *)letchildren,comments=matchsig_item_contentssig_itemwith#ifOCAML_VERSION>="4.08"|Types.Sig_module(id,_,{Types.md_type=Types.Mty_signaturesign},_,_)|Types.Sig_modtype(id,{Types.mtd_type=Some(Types.Mty_signaturesign)},_)#else|Types.Sig_module(id,{Types.md_type=Types.Mty_signaturesign},_)|Types.Sig_modtype(id,{Types.mtd_type=Some(Types.Mty_signaturesign)})#endif->letpath=path@[Ident.nameid]inletchildren_comments=lazy(foldl_next(fun(t,comments)signnext->letchlds,comments=trie_of_sig_item?comments?srcpathimplloc_trie((path,lazyt)::parents)orig_filepathsignnextinList.fold_leftTrie.appendtchlds,comments)(Trie.empty,comments)sign)inletchildren=lazy(fst(Lazy.forcechildren_comments))inletcomments=matchcomments,children_commentswith|None,_->None|Some_,lazy(_,comments)->commentsinchildren,comments|Types.Sig_module(_,#ifOCAML_VERSION>="4.08"_,#endif{Types.md_type=Types.Mty_identsig_ident#ifOCAML_VERSION>="4.04"&&OCAML_VERSION<"4.08"|Types.Mty_alias(_,sig_ident)#else|Types.Mty_aliassig_ident#endif},_#ifOCAML_VERSION>="4.08",_#endif)|Types.Sig_modtype(_,{Types.mtd_type=Some(Types.Mty_identsig_ident#ifOCAML_VERSION>="4.04"&&OCAML_VERSION<"4.08"|Types.Mty_alias(_,sig_ident)#else|Types.Mty_aliassig_ident#endif)}#ifOCAML_VERSION>="4.08",_#endif)->letsig_path=path_of_ocamlsig_identinletchildren=lazy((* Only keep the children, don't override the module reference *)Trie.graft_lazyTrie.empty[](lazy(lookup_parentsparents(path@[Ident.nameid])sig_path)))inchildren,comments#ifOCAML_VERSION>="4.08"|Types.Sig_class(id,{Types.cty_type=cty},_,_)|Types.Sig_class_type(id,{Types.clty_type=cty},_,_)#else|Types.Sig_class(id,{Types.cty_type=cty},_)|Types.Sig_class_type(id,{Types.clty_type=cty},_)#endif->letrecget_clsig=function|Types.Cty_constr(_,_,cty)|Types.Cty_arrow(_,_,cty)->get_clsigcty|Types.Cty_signatureclsig->clsiginletclsig=get_clsigctyinletpath=path@[Ident.nameid]inlet(fields,_)=Ctype.flatten_fields(Ctype.object_fieldsclsig.Types.csig_self)inlazy(List.fold_left(funt(lbl,_,ty_expr)->iflbl="*dummy method*"thentelselet_=Printtyp.reset_and_mark_loopsty_exprinletty=Printtyp.tree_of_typexpfalsety_exprinletty=Outcometree.Osig_type(Outcometree.({otype_name="";otype_params=[];otype_type=ty;otype_private=Asttypes.Public;#ifOCAML_VERSION>="4.03"#ifOCAML_VERSION>="4.10"otype_immediate=Type_immediacy.Unknown;#elseotype_immediate=false;#endif#ifOCAML_VERSION>="4.04"otype_unboxed=false;#endif#endifotype_cstrs=[];}),Outcometree.Orec_not)inTrie.addt(string_to_keylbl){path=path;orig_path=path;kind=Methodinfo;name=lbl;ty=Somety;loc_sig=loc_sig;loc_impl=loc_impl;doc=lazyNone;file=info.file})Trie.emptyfields),comments|_->lazyTrie.empty,commentsinletname=Ident.nameidinifString.lengthname>0&&name.[0]='#'then[],commentselse(string_to_keyname,Trie.create~value:info~children:(lazy[dot,Lazy.forcechildren])())::siblings,comments(* These four functions go through the typedtree to extract includes *)letreclookup_trie_of_module_exprparentstpath=function|Typedtree.Tmod_ident(incpath,{Location.txt=_lid})->letincpath=path_of_ocamlincpathindebug"Including %s impl at %s\n"(modpath_to_stringincpath)(modpath_to_stringpath);letparents=(path,lazyt)::parentsinletsub=lookup_parentsparentspathincpathinoverriding_mergetsub|Typedtree.Tmod_constraint(e,_,_,_)(* | Typedtree.Tmod_apply (e,_,_) *)->lookup_trie_of_module_exprparentstpathe.mod_desc#ifOCAML_VERSION>="4.10"|Typedtree.Tmod_apply({mod_desc=Typedtree.Tmod_functor(Typedtree.Named(Someid,_,_),f)},#else|Typedtree.Tmod_apply({mod_desc=Typedtree.Tmod_functor(id,_,_,f)},#endif{mod_desc=Typedtree.Tmod_ident(arg,_)|Typedtree.Tmod_constraint({mod_desc=Typedtree.Tmod_ident(arg,_)},_,_,_)},_)->letid_name=Ident.nameidinlett=lookup_trie_of_module_exprparentstpathf.Typedtree.mod_descindebug"Grafting %s at %s\n"id_name(modpath_to_string(path_of_ocamlarg));letfunctor_arg=lazy(lookup_parentsparents(path_of_ocamlarg)path)inTrie.graft_lazyt(modpath_to_key[id_name])functor_arg|_->tletrecextract_includes_from_submodule_sigparentstpathname=function|Typedtree.Tmty_signaturesign->letpath=path@[name]inletsub_includes=lazy(get_includes_sig((path,lazyt)::parents)(Trie.subt(modpath_to_key[name]))pathsign)inTrie.graft_lazyt(modpath_to_key[name])sub_includes#ifOCAML_VERSION>="4.10"|Typedtree.Tmty_functor(_,e)#else|Typedtree.Tmty_functor(_,_,_,e)#endif|Typedtree.Tmty_with(e,_)->extract_includes_from_submodule_sigparentstpathnamee.Typedtree.mty_desc|_->tandextract_includes_from_submodule_sig_optparentstpathidmty=#ifOCAML_VERSION>="4.10"matchidwithNone->t|Someid->#endifextract_includes_from_submodule_sigparentstpath(Ident.nameid)mtyandget_includes_implparentstpathttree_struct=letrecextract_submodule_impltname=function|Typedtree.Tmod_structurestr->letpath=path@[name]inletsub_includes=lazy(get_includes_impl((path,lazyt)::parents)(Trie.subt(modpath_to_key[name]))pathstr)inTrie.graft_lazyt(modpath_to_key[name])sub_includes(* | Typedtree.Tmod_functor (arg_id,_,arg_t,e) *)#ifOCAML_VERSION>="4.10"|Typedtree.Tmod_apply({mod_desc=Typedtree.Tmod_functor(Typedtree.Named(Someid,_,_),f)},#else|Typedtree.Tmod_apply({mod_desc=Typedtree.Tmod_functor(id,_,_,f)},#endif{mod_desc=Typedtree.Tmod_ident(arg,_)|Typedtree.Tmod_constraint({mod_desc=Typedtree.Tmod_ident(arg,_)},_,_,_)},_)->letid_name=Ident.nameidindebug"Grafting %s at %s\n"id_name(modpath_to_string(path_of_ocamlarg));letfunctor_arg=lazy(lookup_parents((path,lazyt)::parents)(path_of_ocamlarg)(path@[name]))inextract_submodule_impl(Trie.graft_lazyt(modpath_to_key[id_name])functor_arg)namef.Typedtree.mod_desc#ifOCAML_VERSION>="4.10"|Typedtree.Tmod_functor(_,e)#else|Typedtree.Tmod_functor(_,_,_,e)#endif|Typedtree.Tmod_constraint(e,_,_,_)->extract_submodule_impltnamee.Typedtree.mod_desc|_->tinletextract_submodule_impl_opttidmty=#ifOCAML_VERSION>="4.10"matchidwithNone->t|Someid->#endifextract_submodule_implt(Ident.nameid)mtyinList.fold_left(funtstruc_item->matchstruc_item.Typedtree.str_descwith#ifOCAML_VERSION>="4.08"|Typedtree.Tstr_include{Typedtree.incl_mod={Typedtree.mod_desc=e}}->#else|Typedtree.Tstr_include{Typedtree.incl_mod={Typedtree.mod_desc=e}}->#endiflookup_trie_of_module_exprparentstpathe#ifOCAML_VERSION>="4.08"|Typedtree.Tstr_openTypedtree.{open_expr={mod_desc=Tmod_ident(p,_loc)}}(* TODO: handle the other new open cases *)#else|Typedtree.Tstr_open{Typedtree.open_path=p}#endif->letsub=lookup_parents((path,lazyt)::parents)path(path_of_ocamlp)inoverriding_mergetsub|Typedtree.Tstr_module{Typedtree.mb_id;mb_expr={Typedtree.mod_desc}}->extract_submodule_impl_opttmb_idmod_desc|Typedtree.Tstr_recmodulel->List.fold_left(funt{Typedtree.mb_id;mb_expr={Typedtree.mod_desc}}->extract_submodule_impl_opttmb_idmod_desc)tl|Typedtree.Tstr_modtype{Typedtree.mtd_id=id;mtd_type=Some{Typedtree.mty_desc=e}}->extract_includes_from_submodule_sigparentstpath(Ident.nameid)e|_->t)tttree_struct.Typedtree.str_itemsandget_includes_sigparentstpathttree_sig=letrecextract_includest=function|Typedtree.Tmty_ident(incpath,_)->letincpath=path_of_ocamlincpathindebug"Including %s sig at %s\n"(modpath_to_stringincpath)(modpath_to_stringpath);letparents=(path,lazyt)::parentsinletsub=lookup_parentsparentspathincpathinoverriding_mergetsub|Typedtree.Tmty_with(e,_)->extract_includeste.Typedtree.mty_desc|Typedtree.Tmty_typeofe->lookup_trie_of_module_exprparentstpathe.Typedtree.mod_desc|_->tinList.fold_left(funtsig_item->matchsig_item.Typedtree.sig_descwith|Typedtree.Tsig_include{Typedtree.incl_mod={Typedtree.mty_desc=e}}->extract_includeste|Typedtree.Tsig_module{Typedtree.md_id;md_type={Typedtree.mty_desc}}->extract_includes_from_submodule_sig_optparentstpathmd_idmty_desc|Typedtree.Tsig_modtype{Typedtree.mtd_id=id;mtd_type=Some{Typedtree.mty_desc}}->extract_includes_from_submodule_sigparentstpath(Ident.nameid)mty_desc|Typedtree.Tsig_recmodulel->List.fold_left(funt{Typedtree.md_id;md_type={Typedtree.mty_desc}}->extract_includes_from_submodule_sig_optparentstpathmd_idmty_desc)tl|_->t)tttree_sig.Typedtree.sig_itemsletadd_locs~locst=Trie.map(funpathinfo->letloc_info=lazy(List.find(has_kindinfo.kind)(Trie.find_alllocspath))inletlookupfldnone=letloc=Lazy.force(fldinfo)inifloc=nonethentryLazy.force(fld(Lazy.forceloc_info))withNot_found->noneelselocin{infowithloc_sig=lazy(lookup(funi->i.loc_sig)Location.none);loc_impl=lazy(lookup(funi->i.loc_impl)Location.none);doc=lazy(lookup(funi->i.doc)None);})tletcmt_includesparentstpathcmt_contents=matchcmt_contents.Cmt_format.cmt_annotswith|Cmt_format.Implementationimpl->get_includes_implparentstpathimpl|Cmt_format.Interfacesign->get_includes_sigparentstpathsign|_->Trie.empty(* Can work in a subtree (t doesn't have to be the root) *)letqualify_type_identsparentst=letqualify_keyid=letrel_path=letrecrm_pfxparentspath=matchparents,pathwith|[_root],path->path|_::parents,_::path->rm_pfxparentspath|_->assertfalseinrm_pfxparentsid.pathinletqualify_tyty=letparents=letrecauxaccpath=matchacc,pathwith|((pfx,parent)::_),modl::r->lett=lazy(Trie.sub(Lazy.forceparent)(string_to_key(modl)@[dot]))inaux((pfx@[modl],t)::acc)r|_->accinauxparentsrel_pathinqualify_ty_in_sig_itemparentstyin{idwithty=matchid.tywithSomety->Some(qualify_tyty)|None->None}inTrie.mapqualifytletcmt_signcmt_contents=matchcmt_contents.Cmt_format.cmt_annotswith|Cmt_format.Implementation{Typedtree.str_type=sign;_}|Cmt_format.Interface{Typedtree.sig_type=sign;_}|Cmt_format.Packed(sign,_)->Somesign|_->Noneletprotect_readreaderf=tryreaderfwith|Cmt_format.Error_|Cmi_format.Error_->raise(Bad_formatf)(* Look for a cmt file for the purpose of loading implementation locations.
(assuming other information is already loaded eg. from the cmti). *)letlookup_loc_implorig_file=matchorig_filewith|Cmt_->None|Cmif|Cmtif->letcmt=Filename.chop_extensionf^".cmt"inifnot(Sys.file_existscmt)thenNoneelseSomecmtletload_loc_implparentsfilenamecmt_contents=debug" -Registering %s (for implementation locations)..."filename;letchrono=timer()inmatchcmt_signcmt_contentswith|Somesign->letsrcpath=cmt_contents.Cmt_format.cmt_builddirinlett=foldl_next(funtsig_itemnext->letchld,_comments=trie_of_sig_item~srcpath(lazyNone)parents(Cmtfilename)[]sig_itemnextinList.fold_leftTrie.appendtchld)Trie.emptysignindebug" %.3fs\n%!"(chrono());letincludes=cmt_includesparentst[]cmt_contentsinlett=add_locs~locs:includestinSomet|_->debug" %.3fs\n%!"(chrono());Noneletload_cmi?(qualify=false)roottmodulorig_file=Trie.map_subtreet(string_to_keymodul)(funt->letfile=orig_file_nameorig_fileinletinfo=lazy(letchrono=timer()inletinfo=protect_readCmi_format.read_cmifileindebug" %.3fs\n"(chrono());info)inletimpl_cmt=lazy(matchlookup_loc_implorig_filewith|Somecmt->debug"Loading %s (for implementation locations)..."cmt;letchrono=timer()inletcmt_contents=protect_readCmt_format.read_cmtcmtindebug" %.3fs\n"(chrono());Some(cmt,cmt_contents)|None->None)inletchildren=lazy(letinfo=Lazy.forceinfoindebug" -Registering %s..."file;letchrono=timer()inletrecimplloc_trie=lazy(matchLazy.forceimpl_cmtwith|Some(file,info)->load_loc_impl[[modul],lazy_t;[],root]fileinfo|None->None)andlazy_t=lazy(foldl_next(funtsig_itemnext->letparents=[[modul],lazyt;[],root]inletchld,_comments=trie_of_sig_itemimplloc_trieparentsorig_file[modul]sig_itemnextinList.fold_leftTrie.appendtchld)Trie.emptyinfo.Cmi_format.cmi_sign)inlett=Lazy.forcelazy_tindebug" %.3fs ; done\n%!"(chrono());t)inlett=Trie.addt[]{path=[];orig_path=[];kind=Module;name=modul;ty=None;loc_sig=Lazy.from_valLocation.none;loc_impl=Lazy.from_valLocation.none;doc=lazyNone;file=orig_file;}inletchildren=ifqualifythenlazy(qualify_type_idents[[modul],children;[],root](Lazy.forcechildren))elsechildreninTrie.graft_lazyt[dot]children)letload_cmt?(qualify=false)roottmodulorig_file=Trie.map_subtreet(string_to_keymodul)(funt->letcmt_file=orig_file_nameorig_fileinletinfo=lazy(debug"Loading %s..."cmt_file;letchrono=timer()inletinfo=protect_readCmt_format.read_cmtcmt_fileindebug" %.3fs\n"(chrono());info)inletimpl_cmt=lazy(matchlookup_loc_implorig_filewith|Somecmt->debug"Loading %s (for implementation locations)..."cmt;letchrono=timer()inletcmt_contents=protect_readCmt_format.read_cmtcmtindebug" %.3fs\n"(chrono());Some(cmt,cmt_contents)|None->None)inletchildren=lazy(letinfo=Lazy.forceinfoindebug" -Registering %s..."cmt_file;letchrono=timer()inletcomments=Some(Lazy.from_valinfo.Cmt_format.cmt_comments)inletrecimplloc_trie=lazy(matchLazy.forceimpl_cmtwith|Some(file,info)->load_loc_impl[[modul],lazy_t;[],root]fileinfo|None->None)andlazy_t=lazy(matchcmt_signinfowith|Somesign->letsrcpath=info.Cmt_format.cmt_builddirinlett,_trailing_comments=foldl_next(fun(t,comments)sig_itemnext->letparents=[[modul],lazyt;[],root]inletchld,comments=trie_of_sig_item?comments~srcpathimplloc_trieparentsorig_file[modul]sig_itemnextinList.fold_leftTrie.appendtchld,comments)(Trie.empty,comments)signint|None->Trie.empty)inlett=Lazy.forcelazy_tindebug" %.3fs\n%!"(chrono());t)inletchildren=lazy(letincludes=cmt_includes[[modul],children;[],root]t[](Lazy.forceinfo)inadd_locs~locs:includes(Lazy.forcechildren))inletloc_sig,loc_impl=letof_infoi=matchi.Cmt_format.cmt_sourcefilewith|Somef->Location.in_filef|None->Location.noneinmatchorig_filewith|Cmi_|Cmti_->lazy(of_info(Lazy.forceinfo)),lazy(matchLazy.forceimpl_cmtwith|Some(_,i)->of_infoi|None->Location.none)|Cmt_->letl=lazy(of_info(Lazy.forceinfo))inl,linlett=Trie.addt[]{path=[];orig_path=[];kind=Module;name=modul;ty=None;loc_sig;loc_impl;doc=lazyNone;file=orig_file;}inletchildren=ifqualifythenlazy(qualify_type_idents[[modul],children;[],root](Lazy.forcechildren))elsechildreninTrie.graft_lazyt[dot]children)letdebug_file_counter=ref0letdebug_dir_counter=ref0letload_fileroottmodulf=incrdebug_file_counter;matchfwith|Cmi_->load_cmiroottmodulf|Cmt_|Cmti_->load_cmtroottmodulfletload_filestdirfiles=letsplit_filenamefile=tryleti=String.rindexfile'.'inletlen=String.lengthfilein#ifOCAML_VERSION>="4.03"letmodul=String.capitalize_ascii(String.subfile0i)inletext=String.lowercase_ascii(String.subfile(i+1)(len-i-1))in#elseletmodul=String.capitalize(String.subfile0i)inletext=String.lowercase(String.subfile(i+1)(len-i-1))in#endifmodul,extwithNot_found->file,""inletsort_modulesacc(dir,file)=letregbase=Trie.addacc(string_to_keybase)inmatchsplit_filenamefilewith|base,"cmi"->regbase(Cmi(Filename.concatdirfile))|base,"cmt"->regbase(Cmt(Filename.concatdirfile))|base,"cmti"->regbase(Cmti(Filename.concatdirfile))|_->accinletmodules=List.fold_leftsort_modulesTrie.emptydirfilesinletrecroot=lazy(Trie.fold0(funtmodulfiles->matchfileswith|[]->t|f1::fs->(* Load by order of priority:
- first cmti, more info than cmi, ocamldocs, and doesn't expose
private values
- then cmt, it means there is no mli, everything is exposed
- then cmi, has the interface if not much more *)letchoose_filef1f2=matchf1,f2with|(Cmti_asf),_|_,(Cmti_asf)|(Cmt_asf),_|_,(Cmt_asf)|(Cmi_asf),_->finletfile=List.fold_leftchoose_filef1fsinletmodul=key_to_stringmodulinload_fileroottmodulfile)modulest)inLazy.forcerootletload_dirstdirs=letdirfiles=List.fold_left(funaccdir->incrdebug_dir_counter;letfiles=List.rev_map(funf->dir,f)(Array.to_list(Sys.readdirdir))inList.rev_appendfilesacc)[](List.revdirs)inload_filestdirfilesletloadpaths=lett=Trie.create()inlett=List.fold_left(funtinfo->Trie.addt(string_to_keyinfo.name)info)tIndexPredefined.allinletchrono=timer()inlett=load_dirstpathsindebug"Modules directory loaded in %.3fs (%d files in %d directories)...\n"(chrono())!debug_file_counter!debug_dir_counter;#ifOCAML_VERSION>="4.07"open_module~cleanup_path:truet["Stdlib"]#elseopen_module~cleanup_path:truet["Pervasives"]#endifletfully_open_module?(cleanup_path=false)tpath=letbase_path=matchpathwith|m::_->string_to_keym|[]->[]in(* Merge trying to keep the documentation if the new trie has none *)letmergeintfsimpls=letkeep_intfinfo=tryletintf=List.find(has_kindinfo.kind)intfsinletdoc=lazy(matchinfo.docwith|lazyNone->Lazy.forceintf.doc|lazysome->some)inletloc_sig=intf.loc_sigin{infowithdoc;loc_sig}withNot_found->infoinList.mapkeep_intfimplsinlettpath=modpath_to_keypathinletmod_trie=Trie.subttpathinletmod_trie=trymatch(Trie.findtbase_path).filewith|Cmtif|Cmif->letf=Filename.chop_extensionf^".cmt"inifnot(Sys.file_existsf)thenmod_trieelseletdir,base=Filename.dirnamef,Filename.basenamefinlett=load_filesTrie.empty[dir,base]inlett=Trie.subttpathinTrie.merge~values:mergemod_triet|Cmt_->mod_triewithNot_found->mod_triein(* cleanup and merge at root (cf. open_module) *)letmod_trie=ifcleanup_paththenletpathlen=List.lengthpathinTrie.map(fun_key->fix_path_prefixpathlen[])mod_trieelsemod_trieinoverriding_mergetmod_trieletadd_filetfile=letdir,file=Filename.dirnamefile,Filename.basenamefileinload_filest[dir,file]