123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615# 1 "src/loader/ident_env.cppo.ml"(*
* Copyright (c) 2014 Leo White <lpw25@cl.cam.ac.uk>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openOdoc_modelopenPredefinedopenNamesmoduleId=Paths.IdentifiermoduleP=Paths.Pathtypetype_ident=Paths.Identifier.Path.Type.ttypet={modules:Id.Module.tIdent.tbl;module_paths:P.Module.tIdent.tbl;module_types:Id.ModuleType.tIdent.tbl;types:Id.DataType.tIdent.tbl;values:Id.Value.tIdent.tbl;classes:Id.Class.tIdent.tbl;class_types:Id.ClassType.tIdent.tbl;hidden:Ident.tlist;(* we use term hidden to mean shadowed and idents_in_doc_off_mode items*)}letempty={modules=Ident.empty;module_paths=Ident.empty;module_types=Ident.empty;types=Ident.empty;values=Ident.empty;classes=Ident.empty;class_types=Ident.empty;hidden=[];}(* The boolean is an override for whether it should be hidden - true only for
items introduced by extended open *)typeextracted_item=[`ModuleofIdent.t*bool|`ModuleTypeofIdent.t*bool|`TypeofIdent.t*bool|`ValueofIdent.t*bool|`ClassofIdent.t*Ident.t*Ident.t*Ident.t*bool|`ClassTypeofIdent.t*Ident.t*Ident.t*bool]typeextracted_items=[extracted_item|`Includeofextracted_itemlist]letbuiltin_idents=List.mapsndPredef.builtin_identsletrecextract_signature_type_itemsitems=letopenCompatinmatchitemswith|Sig_type(id,_,_,Exported)::rest->ifBtype.is_row_name(Ident.nameid)thenextract_signature_type_itemsrestelse`Type(id,false)::extract_signature_type_itemsrest|Sig_module(id,_,_,_,Exported)::rest->`Module(id,false)::extract_signature_type_itemsrest|Sig_modtype(id,_,Exported)::rest->`ModuleType(id,false)::extract_signature_type_itemsrest|Sig_value(id,_,Exported)::rest->`Value(id,false)::extract_signature_type_itemsrest|Sig_class(id,_,_,Exported)::Sig_class_type(ty_id,_,_,_)::Sig_type(obj_id,_,_,_)::Sig_type(cl_id,_,_,_)::rest->`Class(id,ty_id,obj_id,cl_id,false)::extract_signature_type_itemsrest|Sig_class_type(id,_,_,Exported)::Sig_type(obj_id,_,_,_)::Sig_type(cl_id,_,_,_)::rest->`ClassType(id,obj_id,cl_id,false)::extract_signature_type_itemsrest|Sig_typext_::rest->extract_signature_type_itemsrest|Sig_class_type(_,_,_,Hidden)::Sig_type(_,_,_,_)::Sig_type(_,_,_,_)::rest|Sig_class(_,_,_,Hidden)::Sig_class_type(_,_,_,_)::Sig_type(_,_,_,_)::Sig_type(_,_,_,_)::rest|Sig_modtype(_,_,Hidden)::rest|Sig_module(_,_,_,_,Hidden)::rest|Sig_type(_,_,_,Hidden)::rest|Sig_value(_,_,Hidden)::rest->extract_signature_type_itemsrest|Sig_class_::_|Sig_class_type_::_->assertfalse|[]->[]# 112 "src/loader/ident_env.cppo.ml"letrecunwrap_module_expr_desc=function|Typedtree.Tmod_constraint(mexpr,_,Tmodtype_implicit,_)->unwrap_module_expr_descmexpr.mod_desc|desc->descletrecextract_extended_open_itemsitems=letopenTypesinmatchitemswith|Sig_type(id,_,_,_)::rest->ifBtype.is_row_name(Ident.nameid)thenextract_extended_open_itemsrestelse`Type(id,true)::extract_extended_open_itemsrest|Sig_module(id,_,_,_,_)::rest->`Module(id,true)::extract_extended_open_itemsrest|Sig_modtype(id,_,_)::rest->`ModuleType(id,true)::extract_extended_open_itemsrest|Sig_value(id,_,_)::rest->`Value(id,true)::extract_extended_open_itemsrest|Sig_class(id,_,_,_)::Sig_class_type(ty_id,_,_,_)::Sig_type(obj_id,_,_,_)::Sig_type(cl_id,_,_,_)::rest->`Class(id,ty_id,obj_id,cl_id,true)::extract_extended_open_itemsrest|Sig_class_type(id,_,_,_)::Sig_type(obj_id,_,_,_)::Sig_type(cl_id,_,_,_)::rest->`ClassType(id,obj_id,cl_id,true)::extract_extended_open_itemsrest|Sig_typext_::rest->extract_extended_open_itemsrest|Sig_class_::_|Sig_class_type_::_->assertfalse|[]->[]letextract_extended_openo=letopenTypedtreeinmatchunwrap_module_expr_desco.open_expr.mod_descwith|Tmod_ident(_,_)->[]|_->extract_extended_open_itemso.open_bound_items# 159 "src/loader/ident_env.cppo.ml"letrecextract_signature_tree_itemshide_itemitems=letopenTypedtreeinmatchitemswith# 165 "src/loader/ident_env.cppo.ml"|{sig_desc=Tsig_type(_,decls);_}::rest-># 167 "src/loader/ident_env.cppo.ml"List.map(fundecl->`Type(decl.typ_id,hide_item))decls@extract_signature_tree_itemshide_itemrest# 171 "src/loader/ident_env.cppo.ml"|{sig_desc=Tsig_module{md_id=Someid;_};_}::rest->[`Module(id,hide_item)]@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_module_;_}::rest->extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_recmodulemds;_}::rest->List.fold_right(funmditems->matchmd.md_idwith|Someid->`Module(id,hide_item)::items|None->items)mds[]@extract_signature_tree_itemshide_itemrest# 189 "src/loader/ident_env.cppo.ml"|{sig_desc=Tsig_value{val_id;_};_}::rest->[`Value(val_id,hide_item)]@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_modtypemtd;_}::rest->[`ModuleType(mtd.mtd_id,hide_item)]@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_includeincl;_}::rest->[`Include(extract_signature_type_items(Compat.signatureincl.incl_type))]@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_attributeattr;_}::rest->beginmatchDoc_attr.parse_attributeattrwith|Some("/*",_)->extract_signature_tree_items(nothide_item)rest|_->extract_signature_tree_itemshide_itemrestend|{sig_desc=Tsig_classcls;_}::rest->List.map(funcld->lettypehash=# 207 "src/loader/ident_env.cppo.ml"cld.ci_id_typehash# 209 "src/loader/ident_env.cppo.ml"in`Class(cld.ci_id_class,cld.ci_id_class_type,cld.ci_id_object,typehash,hide_item))cls@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_class_typecltyps;_}::rest->List.map(funclty->lettypehash=# 219 "src/loader/ident_env.cppo.ml"clty.ci_id_typehash# 221 "src/loader/ident_env.cppo.ml"in`ClassType(clty.ci_id_class_type,clty.ci_id_object,typehash,hide_item))cltyps@extract_signature_tree_itemshide_itemrest# 226 "src/loader/ident_env.cppo.ml"|{sig_desc=Tsig_modsubstms;_}::rest->[`Module(ms.ms_id,hide_item)]@extract_signature_tree_itemshide_itemrest|{sig_desc=Tsig_typesubstts;_}::rest->List.map(fundecl->`Type(decl.typ_id,hide_item))ts@extract_signature_tree_itemshide_itemrest# 236 "src/loader/ident_env.cppo.ml"|{sig_desc=Tsig_typext_;_}::rest|{sig_desc=Tsig_exception_;_}::rest|{sig_desc=Tsig_open_;_}::rest->extract_signature_tree_itemshide_itemrest|[]->[]letrecread_patternhide_itempat=letopenTypedtreeinmatchpat.pat_descwith|Tpat_var(id,_)->[`Value(id,hide_item)]|Tpat_alias(pat,id,_)->`Value(id,hide_item)::read_patternhide_itempat|Tpat_record(pats,_)->List.concat(List.map(fun(_,_,pat)->read_patternhide_itempat)pats)# 249 "src/loader/ident_env.cppo.ml"|Tpat_construct(_,_,pats)# 253 "src/loader/ident_env.cppo.ml"|Tpat_arraypats|Tpat_tuplepats->List.concat(List.map(funpat->read_patternhide_itempat)pats)|Tpat_or(pat,_,_)|Tpat_variant(_,Somepat,_)|Tpat_lazypat->read_patternhide_itempat|Tpat_any|Tpat_constant_|Tpat_variant(_,None,_)->[]# 263 "src/loader/ident_env.cppo.ml"letrecextract_structure_tree_itemshide_itemitems=letopenTypedtreeinmatchitemswith# 269 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_type(_,decls);_}::rest->(* TODO: handle rec_flag *)# 271 "src/loader/ident_env.cppo.ml"List.map(fundecl->`Type(decl.typ_id,hide_item))decls@extract_structure_tree_itemshide_itemrest# 278 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_value(_,vbs);_}::rest->(*TODO: handle rec_flag *)# 280 "src/loader/ident_env.cppo.ml"(List.map(funvb->read_patternhide_itemvb.vb_pat)vbs|>List.flatten)@extract_structure_tree_itemshide_itemrest# 284 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_module{mb_id=Someid;_};_}::rest->[`Module(id,hide_item)]@extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_module_;_}::rest->extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_recmodulembs;_}::rest->List.fold_right(funmbitems->matchmb.mb_idwith|Someid->`Module(id,hide_item)::items|None->items)mbs[]@extract_structure_tree_itemshide_itemrest# 300 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_modtypemtd;_}::rest->[`ModuleType(mtd.mtd_id,hide_item)]@extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_includeincl;_}::rest->[`Include(extract_signature_type_items(Compat.signatureincl.incl_type))]@extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_attributeattr;_}::rest->beginmatchDoc_attr.parse_attributeattrwith|Some("/*",_)->extract_structure_tree_items(nothide_item)rest|_->extract_structure_tree_itemshide_itemrestend|{str_desc=Tstr_classcls;_}::rest->List.map# 315 "src/loader/ident_env.cppo.ml"(fun(cld,_)-># 317 "src/loader/ident_env.cppo.ml"`Class(cld.ci_id_class,cld.ci_id_class_type,cld.ci_id_object,# 322 "src/loader/ident_env.cppo.ml"cld.ci_id_typehash,# 324 "src/loader/ident_env.cppo.ml"hide_item))cls@extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_class_typecltyps;_}::rest->List.map(fun(_,_,clty)->`ClassType(clty.ci_id_class_type,clty.ci_id_object,# 334 "src/loader/ident_env.cppo.ml"clty.ci_id_typehash,# 336 "src/loader/ident_env.cppo.ml"hide_item))cltyps@extract_structure_tree_itemshide_itemrest# 341 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_openo;_}::rest->((extract_extended_openo):>extracted_itemslist)@extract_structure_tree_itemshide_itemrest# 344 "src/loader/ident_env.cppo.ml"|{str_desc=Tstr_primitive{val_id;_};_}::rest->[`Value(val_id,false)]@extract_structure_tree_itemshide_itemrest|{str_desc=Tstr_eval_;_}::rest|{str_desc=Tstr_typext_;_}::rest|{str_desc=Tstr_exception_;_}::rest->extract_structure_tree_itemshide_itemrest|[]->[]letflatten_extracted:extracted_itemslist->extracted_itemlist=funitems->List.map(function|`Type_|`Module_|`ModuleType_|`Value_|`Class_|`ClassType_asx->[x]|`Includexs->xs)items|>List.flattenlettype_name_existsnameitems=List.exists(function|`Type(id',_)whenIdent.nameid'=name->true|_->false)itemsletvalue_name_existsnameitems=List.exists(function|`Value(id',_)whenIdent.nameid'=name->true|_->false)itemsletmodule_name_existsnameitems=List.exists(function|`Module(id',_)whenIdent.nameid'=name->true|_->false)itemsletmodule_type_name_existsnameitems=List.exists(function|`ModuleType(id',_)whenIdent.nameid'=name->true|_->false)itemsletclass_name_existsnameitems=List.exists(function|`Class(id',_,_,_,_)whenIdent.nameid'=name->true|_->false)itemsletclass_type_name_existsnameitems=List.exists(function|`ClassType(id',_,_,_)whenIdent.nameid'=name->true|_->false)itemsletenv_of_itemsparentitemsenv=letrecinneritemsenv=matchitemswith|`Type(t,is_hidden_item)::rest->letname=Ident.nametinletis_hidden=is_hidden_item||type_name_existsnamerestinletidentifier,hidden=ifis_hiddenthen`Type(parent,TypeName.internal_of_stringname),t::env.hiddenelse`Type(parent,TypeName.make_stdname),env.hiddeninlettypes=Ident.addtidentifierenv.typesininnerrest{envwithtypes;hidden}|`Value(t,is_hidden_item)::rest->letname=Ident.nametinletis_hidden=is_hidden_item||value_name_existsnamerestinletidentifier,hidden=ifis_hiddenthen`Value(parent,ValueName.internal_of_stringname),t::env.hiddenelse`Value(parent,ValueName.make_stdname),env.hiddeninletvalues=Ident.addtidentifierenv.valuesininnerrest{envwithvalues;hidden}|`ModuleType(t,is_hidden_item)::rest->letname=Ident.nametinletis_hidden=is_hidden_item||module_type_name_existsnamerestinletidentifier,hidden=ifis_hiddenthen`ModuleType(parent,ModuleTypeName.internal_of_stringname),t::env.hiddenelse`ModuleType(parent,ModuleTypeName.make_stdname),env.hiddeninletmodule_types=Ident.addtidentifierenv.module_typesininnerrest{envwithmodule_types;hidden}|`Module(t,is_hidden_item)::rest->letname=Ident.nametinletdouble_underscore=Odoc_model.Root.contains_double_underscorenameinletis_hidden=is_hidden_item||module_name_existsnamerest||double_underscoreinletidentifier,hidden=ifis_hiddenthen`Module(parent,ModuleName.internal_of_stringname),t::env.hiddenelse`Module(parent,ModuleName.make_stdname),env.hiddeninletpath=`Identifier(identifier,is_hidden)inletmodules=Ident.addtidentifierenv.modulesinletmodule_paths=Ident.addtpathenv.module_pathsininnerrest{envwithmodules;module_paths;hidden}|`Class(t,t2,t3,t4,is_hidden_item)::rest->letname=Ident.nametinletis_hidden=is_hidden_item||class_name_existsnamerestinletidentifier,hidden=ifis_hiddenthen`Class(parent,ClassName.internal_of_stringname),t::t2::t3::t4::env.hiddenelse`Class(parent,ClassName.make_stdname),env.hiddeninletclasses=List.fold_right(funidclasses->Ident.addididentifierclasses)[t;t2;t3;t4]env.classesininnerrest{envwithclasses;hidden}|`ClassType(t,t2,t3,is_hidden_item)::rest->letname=Ident.nametinletis_hidden=is_hidden_item||class_type_name_existsnamerestinletidentifier,hidden=ifis_hiddenthen`ClassType(parent,ClassTypeName.internal_of_stringname),t::t2::t3::env.hiddenelse`ClassType(parent,ClassTypeName.make_stdname),env.hiddeninletclass_types=List.fold_right(funidclass_types->Ident.addididentifierclass_types)[t;t2;t3]env.class_typesininnerrest{envwithclass_types;hidden}|[]->envininneritemsenvletadd_signature_tree_items:Paths.Identifier.Signature.t->Typedtree.signature->t->t=funparentsgenv->letitems=extract_signature_tree_itemsfalsesg.sig_items|>flatten_extractedinenv_of_itemsparentitemsenvletadd_structure_tree_items:Paths.Identifier.Signature.t->Typedtree.structure->t->t=funparentsgenv->letitems=extract_structure_tree_itemsfalsesg.str_items|>flatten_extractedinenv_of_itemsparentitemsenvlethandle_signature_type_items:Paths.Identifier.Signature.t->Compat.signature->t->t=funparentsgenv->letitems=extract_signature_type_itemssginenv_of_itemsparentitemsenvletadd_parameterparentidnameenv=lethidden=ParameterName.is_hiddennameinletpath=`Identifier(`Parameter(parent,name),hidden)inletmodule_paths=Ident.addidpathenv.module_pathsin{envwithmodule_paths}letfind_moduleenvid=Ident.find_sameidenv.module_pathsletfind_module_identifierenvid=Ident.find_sameidenv.modulesletfind_module_typeenvid=Ident.find_sameidenv.module_typesletfind_type_identifierenvid=Ident.find_sameidenv.typesletfind_value_identifierenvid=Ident.find_sameidenv.valuesletfind_typeenvid=try(Ident.find_sameidenv.types:>Id.Path.Type.t)withNot_found->try(Ident.find_sameidenv.classes:>Id.Path.Type.t)withNot_found->try(Ident.find_sameidenv.class_types:>Id.Path.Type.t)withNot_found->ifList.memidbuiltin_identsthenmatchcore_type_identifier(Ident.nameid)with|Someid->(id:>type_ident)|None->raiseNot_foundelseraiseNot_foundletfind_class_typeenvid=try(Ident.find_sameidenv.classes:>Id.Path.ClassType.t)withNot_found->(Ident.find_sameidenv.class_types:>Id.Path.ClassType.t)letfind_class_identifierenvid=Ident.find_sameidenv.classesletfind_class_type_identifierenvid=Ident.find_sameidenv.class_typesletis_shadowedenvid=List.memidenv.hiddenmodulePath=structletread_module_identenvid=ifIdent.persistentidthen`Root(Ident.nameid)elsetryfind_moduleenvidwithNot_found->assertfalseletread_module_type_identenvid=try`Identifier(find_module_typeenvid,false)withNot_found->assertfalseletread_type_identenvid=try`Identifier(find_typeenvid,false)withNot_found->assertfalseletread_class_type_identenvid:Paths.Path.ClassType.t=try`Identifier(find_class_typeenvid,false)withNot_found->`Dot(`Root"*",(Ident.nameid))(* TODO remove this hack once the fix for PR#6650
is in the OCaml release *)(* When a type is a classtype path (with a #), the # is stripped off because
each ident is mapped to the identifier named for the ident without a
hash. e.g. in the following, we take the name of the identifier from
cd_id_class, and therefore even [Pident #u/10] will map to identifier
[u].
Typedtree.Tsig_class_type
[{Typedtree.ci_virt = Asttypes.Concrete; ci_params = [];
ci_id_name = {Asttypes.txt = ...; loc = ...}; ci_id_class = u/13[14];
ci_id_class_type = u/12[14]; ci_id_object = u/11[14];
ci_id_typehash = #u/10[14];
For a dotted path though, we have to strip the # off manually here, so
[read_class_type] and [read_type] both need the following function.
*)letstrip_hashs=ifs.[0]='#'thenString.subs1(String.lengths-1)elsesletrecread_module:t->Path.t->Paths.Path.Module.t=funenv->function|Path.Pidentid->read_module_identenvid# 574 "src/loader/ident_env.cppo.ml"|Path.Pdot(p,s)->`Dot(read_moduleenvp,s)# 578 "src/loader/ident_env.cppo.ml"|Path.Papply(p,arg)->`Apply(read_moduleenvp,read_moduleenvarg)letread_module_typeenv=function|Path.Pidentid->read_module_type_identenvid# 583 "src/loader/ident_env.cppo.ml"|Path.Pdot(p,s)->`Dot(read_moduleenvp,s)# 587 "src/loader/ident_env.cppo.ml"|Path.Papply(_,_)->assertfalseletread_class_typeenv=function|Path.Pidentid->read_class_type_identenvid# 592 "src/loader/ident_env.cppo.ml"|Path.Pdot(p,s)->`Dot(read_moduleenvp,strip_hashs)# 596 "src/loader/ident_env.cppo.ml"|Path.Papply(_,_)->assertfalseletread_typeenv=function|Path.Pidentid->read_type_identenvid# 601 "src/loader/ident_env.cppo.ml"|Path.Pdot(p,s)->`Dot(read_moduleenvp,strip_hashs)# 605 "src/loader/ident_env.cppo.ml"|Path.Papply(_,_)->assertfalseendmoduleFragment=structletrecread_module:Longident.t->Paths.Fragment.Module.t=function|Longident.Lidents->`Dot(`Root,s)|Longident.Ldot(p,s)->`Dot((read_modulep:>Paths.Fragment.Signature.t),s)|Longident.Lapply_->assertfalseletread_module_type:Longident.t->Paths.Fragment.ModuleType.t=function|Longident.Lidents->`Dot(`Root,s)|Longident.Ldot(p,s)->`Dot((read_modulep:>Paths.Fragment.Signature.t),s)|Longident.Lapply_->assertfalseletread_type=function|Longident.Lidents->`Dot(`Root,s)|Longident.Ldot(p,s)->`Dot((read_modulep:>Paths.Fragment.Signature.t),s)|Longident.Lapply_->assertfalseend