1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162(* {{{ Copying *(
This file is part of Merlin, an helper for ocaml editors
Copyright (C) 2013 - 2017 Frédéric Bour <frederic.bour(_)lakaban.net>
Thomas Refis <refis.thomas(_)gmail.com>
Simon Castellan <simon.castellan(_)iuwt.fr>
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.
)* }}} *)[@@@ocaml.warning"-9"]openStdtypeconstructor_declaration=Typedtree.constructor_declarationopenTypedtreetypenode=|Dummy|Pattern:_general_pattern->node|Expressionofexpression|Case:_case->node|Class_exprofclass_expr|Class_structureofclass_structure|Class_fieldofclass_field|Class_field_kindofclass_field_kind|Module_exprofmodule_expr|Module_type_constraintofmodule_type_constraint|Structureofstructure|Signatureofsignature|Structure_itemofstructure_item*Env.t|Signature_itemofsignature_item*Env.t|Module_bindingofmodule_binding|Value_bindingofvalue_binding|Module_typeofmodule_type|Module_declarationofmodule_declaration|Module_type_declarationofmodule_type_declaration|With_constraintofwith_constraint|Core_typeofcore_type|Package_typeofpackage_type|Row_fieldofrow_field|Value_descriptionofvalue_description|Type_declarationoftype_declaration|Type_kindoftype_kind|Type_extensionoftype_extension|Extension_constructorofextension_constructor|Label_declarationoflabel_declaration|Constructor_declarationofconstructor_declaration|Class_typeofclass_type|Class_signatureofclass_signature|Class_type_fieldofclass_type_field|Class_declarationofclass_declaration|Class_descriptionofclass_description|Class_type_declarationofclass_type_declaration|Binding_opofbinding_op|Include_descriptionofinclude_description|Include_declarationofinclude_declaration|Open_descriptionofopen_description|Open_declarationofopen_declaration|Method_callofexpression*meth*Location.t|Record_field:[`Expressionofexpression|`Patternofpattern]*'repTypes.gen_label_description*'repTypes.record_form*Longident.tLocation.loc->node|Module_binding_nameofmodule_binding|Module_declaration_nameofmodule_declaration|Module_type_declaration_nameofmodule_type_declaration|ModeofMode.Alloc.atomLocation.loc|ModalityofMode.Modality.atomLocation.loc|Jkind_annotationofParsetree.jkind_annotation|Jkind_declarationofTypedtree.jkind_declaration|Mod_boundofParsetree.modeLocation.loc|Scannable_axis_annotationofstringLocation.loc|Attributeofattributeletnode_update_envenv0=function|Pattern{pat_env=env}|Expression{exp_env=env}|Class_expr{cl_env=env}|Method_call({exp_env=env},_,_)|Record_field(`Expression{exp_env=env},_,_,_)|Record_field(`Pattern{pat_env=env},_,_,_)|Module_expr{mod_env=env}|Module_type{mty_env=env}|Structure_item(_,env)|Signature_item(_,env)|Core_type{ctyp_env=env}|Class_type{cltyp_env=env}->env|Dummy|Case_|Class_structure_|Class_signature_|Class_field_|Class_field_kind_|Type_extension_|Extension_constructor_|Package_type_|Row_field_|Type_declaration_|Type_kind_|Module_binding_|Module_declaration_|Module_binding_name_|Module_declaration_name_|Module_type_declaration_|Module_type_constraint_|Module_type_declaration_name_|With_constraint_|Structure_|Signature_|Value_description_|Value_binding_|Constructor_declaration_|Label_declaration_|Class_declaration_|Class_description_|Class_type_declaration_|Class_type_field_|Include_description_|Include_declaration_|Open_description_|Open_declaration_|Binding_op_|Mode_|Modality_|Jkind_annotation_|Jkind_declaration_|Scannable_axis_annotation_|Mod_bound_|Attribute_->env0letnode_real_locloc0=function|Expression{exp_loc=loc}|Pattern{pat_loc=loc}|Method_call(_,_,loc)|Record_field(_,_,_,{loc})|Class_expr{cl_loc=loc}|Module_expr{mod_loc=loc}|Structure_item({str_loc=loc},_)|Signature_item({sig_loc=loc},_)|Module_type{mty_loc=loc}|Core_type{ctyp_loc=loc}|Class_type{cltyp_loc=loc}|Class_field{cf_loc=loc}|Module_binding{mb_loc=loc}|Module_declaration{md_loc=loc}|Module_type_declaration{mtd_loc=loc}|Value_description{val_loc=loc}|Value_binding{vb_loc=loc}|Type_declaration{typ_loc=loc}|Label_declaration{ld_loc=loc}|Constructor_declaration{cd_loc=loc}|Class_type_field{ctf_loc=loc}|Class_declaration{ci_loc=loc}|Class_description{ci_loc=loc}|Class_type_declaration{ci_loc=loc}|Extension_constructor{ext_loc=loc}|Include_description{incl_loc=loc}|Include_declaration{incl_loc=loc}|Open_description{open_loc=loc}|Open_declaration{open_loc=loc}|Binding_op{bop_op_name={loc}}|Mode{loc}|Modality{loc}|Jkind_annotation{pjka_loc=loc}|Jkind_declaration{jkind_loc=loc}|Mod_bound{loc}|Scannable_axis_annotation{loc}|Attribute{attr_name={loc}}->loc|Module_type_declaration_name{mtd_name=loc}->loc.Location.loc|Module_declaration_name{md_name=loc}|Module_binding_name{mb_name=loc}->loc.Location.loc|Structure_|Signature_|Case_|Class_structure_|Type_extension_|Class_field_kind_|Module_type_constraint_|With_constraint_|Row_field_|Type_kind_|Class_signature_|Package_type_|Dummy->loc0letnode_attributes=function|Expressionexp->exp.exp_attributes|Patternpat->pat.pat_attributes|Class_exprcl->cl.cl_attributes|Class_fieldcf->cf.cf_attributes|Module_exprme->me.mod_attributes|Structure_item({str_desc=Tstr_eval(_,_,attr)},_)->attr|Structure_item({str_desc=Tstr_attributea},_)->[a]|Signature_item({sig_desc=Tsig_attributea},_)->[a]|Module_bindingmb->mb.mb_attributes|Value_bindingvb->vb.vb_attributes|Module_typemt->mt.mty_attributes|Module_declarationmd->md.md_attributes|Module_type_declarationmtd->mtd.mtd_attributes|Open_descriptiono->o.open_attributes|Include_declarationi->i.incl_attributes|Include_descriptioni->i.incl_attributes|Core_typect->ct.ctyp_attributes|Row_fieldrf->rf.rf_attributes|Value_descriptionvd->vd.val_attributes|Type_declarationtd->td.typ_attributes|Label_declarationld->ld.ld_attributes|Constructor_declarationcd->cd.cd_attributes|Type_extensionte->te.tyext_attributes|Extension_constructorec->ec.ext_attributes|Class_typect->ct.cltyp_attributes|Class_type_fieldctf->ctf.ctf_attributes|Class_declarationci->ci.ci_attributes|Class_descriptionci->ci.ci_attributes|Class_type_declarationci->ci.ci_attributes|Method_call(obj,_,_)->obj.exp_attributes|Record_field(`Expressionobj,_,_,_)->obj.exp_attributes|Record_field(`Patternobj,_,_,_)->obj.pat_attributes|_->[]lethas_attr~namenode=letattrs=node_attributesnodeinList.exists~f:(funa->letstr,_=Ast_helper.Attr.as_tupleainstr.Location.txt=name)attrsletnode_merlin_locloc0node=letattributes=node_attributesnodeinletloc=letopenParsetreeinletpred{attr_name=loc;_}=Location_aux.is_relaxed_locationlocinmatchList.findattributes~f:predwith|{attr_name;_}->attr_name.Location.loc|exceptionNot_found->node_real_locloc0nodeinletloc=matchnodewith|Expression{exp_extra;_}->List.fold_left~f:(funloc0(_,loc,_)->Location_aux.unionloc0loc)~init:locexp_extra|Pattern{pat_extra;_}->List.fold_left~f:(funloc0(_,loc,_)->Location_aux.unionloc0loc)~init:locpat_extra|_->locinlocletappnodeenvfacc=f(node_update_envenvnode)nodeacctype'af0=Env.t->node->'a->'atype('b,'a)f1='b->Env.t->'af0->'a->'aletid_fold_env(_f:_f0)acc=acclet(**)f1f2env(f:_f0)acc=f2envf(f1envfacc)letreclist_fold(f':_f1)xsenvfacc=matchxswith|x::xs->list_foldf'xsenvf(f'xenvfacc)|[]->accletarray_fold(f':_f1)arrenvfacc=letacc=refaccinfori=0toArray.lengtharr-1doacc:=f'arr.(i)envf!accdone;!accletreclist_fold_with_next(f':_->_f1)xsenvfacc=matchxswith|x::(y::_asxs)->list_fold_with_nextf'xsenvf(f'(Somey)xenvfacc)|[x]->f'Nonexenvfacc|[]->accletoption_foldf'oenv(f:_f0)acc=matchowith|None->acc|Somex->f'xenvfaccletof_core_typect=app(Core_typect)letof_mod_boundmod_bound=app(Mod_boundmod_bound)letof_scannable_axis_annotationscannable_axis_annotation=app(Scannable_axis_annotationscannable_axis_annotation)letof_modemode=app(Modemode)letof_modesmodes=list_foldof_modemodes.mode_descletof_modalitymodality=app(Modalitymodality)letof_modalitiesmodalities=list_foldof_modalitymodalities.moda_descletof_value_description_modal_info=function|Valmi_sig_valuemodalities->of_modalitiesmodalities|Valmi_str_primitivemodes->of_modesmodesletof_jkind_annotationjkind=app(Jkind_annotationjkind)letof_jkind_annotation_optjkind=option_foldof_jkind_annotationjkindletof_exp_extra(exp,_,_)=matchexpwith|Texp_constraintct->of_core_typect|Texp_coerce(cto,ct)->of_core_typect**option_foldof_core_typecto|Texp_polycto->option_foldof_core_typecto|Texp_newtype(_,_,jkind,_)->of_jkind_annotation_optjkind|Texp_modemodes->of_modesmodes|Texp_stack|Texp_inspected_type_|Texp_borrowed|Texp_ghost_region->id_foldletof_expressione=app(Expressione)**list_foldof_exp_extrae.exp_extraletof_pat_extra(pat,_,_)=matchpatwith|Tpat_constraint(ct,modes)->of_core_typect**of_modesmodes|Tpat_type_|Tpat_unpack|Tpat_open_|Tpat_inspected_type_->id_foldletof_pattern(typek)(p:kgeneral_pattern)=app(Patternp)**list_foldof_pat_extrap.pat_extraletof_casec=app(Casec)letof_label_declarationct=app(Label_declarationct)letof_value_bindingvb=app(Value_bindingvb)letof_module_typemt=app(Module_typemt)letof_module_exprme=app(Module_exprme)letof_typ_param(ct,_)=of_core_typectletof_constructor_argarg=of_core_typearg.ca_type**of_modalitiesarg.ca_modalitiesletof_constructor_arguments=function|Cstr_tuplects->list_foldof_constructor_argcts|Cstr_recordlbls->list_foldof_label_declarationlblsletof_bop({bop_exp;_}asbop)=app(Binding_opbop)**of_expressionbop_expletof_record_fieldobjloclblformenv(f:_f0)acc=app(Record_field(obj,lbl,form,loc))envfaccletof_exp_record_fieldobjlid_loclblform=of_record_field(`Expressionobj)lid_loclblformletof_pat_record_fieldobjloclblform=of_record_field(`Patternobj)loclblformletof_comprehension_clause_binding{comp_cb_iterator;_}=matchcomp_cb_iteratorwith|Texp_comp_range{start;stop;_}->of_expressionstart**of_expressionstop|Texp_comp_in{pattern;sequence}->of_patternpattern**of_expressionsequenceletof_comprehension_clause=function|Texp_comp_forbindings->list_foldof_comprehension_clause_bindingbindings|Texp_comp_whene->of_expressioneletof_comprehension{comp_body;comp_clauses}=of_expressioncomp_body**list_foldof_comprehension_clausecomp_clausesletof_pattern_desc(typek)(desc:kpattern_desc)=matchdescwith|Tpat_any|Tpat_var_|Tpat_constant_|Tpat_variant(_,None,_)|Tpat_unboxed_bool_|Tpat_unboxed_unit|Tpat_fun_layout_->id_fold|Tpat_alias{pattern=p;_}|Tpat_variant(_,Somep,_)|Tpat_lazyp|Tpat_exceptionp->of_patternp|Tpat_valuep->of_pattern(p:>valuegeneral_pattern)|Tpat_tupleps->list_fold(fun(_lbl,p)->of_patternp)ps|Tpat_unboxed_tupleps->list_fold(fun(_lbl,p,_sort)->of_patternp)ps|Tpat_construct(_,_,ps,None)|Tpat_array(_,_,ps)->list_foldof_patternps|Tpat_construct(_,_,ps,Some(jkinds,ct))->list_foldof_patternps**of_core_typect**list_fold(fun(_,jkind)->of_jkind_annotation_optjkind)jkinds|Tpat_record(ls,_)->list_fold(fun(lid_loc,desc,p)->of_pat_record_fieldplid_locdescLegacy**of_patternp)ls|Tpat_record_unboxed_product(ls,_)->list_fold(fun(lid_loc,desc,p)->of_pat_record_fieldplid_locdescUnboxed_product**of_patternp)ls|Tpat_or(p1,p2,_)->of_patternp1**of_patternp2letof_method_callobjmethlocenv(f:_f0)acc=letloc_start=obj.exp_loc.Location.loc_endinletloc_end=loc.Location.loc_endinletloc={locwithLocation.loc_start;loc_end}inapp(Method_call(obj,meth,loc))envfaccletof_block_access=function|Baccess_field(_,_)->id_fold|Baccess_block(_,exp)->of_expressionexpletof_unboxed_access=function|Uaccess_unboxed_field(_,_)->id_foldletrecof_expression_descloc=function|Texp_ident_|Texp_constant_|Texp_instvar_|Texp_mutvar_|Texp_variant(_,None)|Texp_unboxed_bool_|Texp_unboxed_unit|Texp_new_|Texp_src_pos|Texp_typed_hole->id_fold|Texp_let(_,vbs,e)->of_expressione**list_foldof_value_bindingvbs|Texp_letmutable(vb,e)->of_expressione**of_value_bindingvb|Texp_function{params;body;ret_mode;_}->list_foldof_function_paramparams**of_function_bodybody**of_modesret_mode|Texp_apply(e,ls,_,_,_)->of_expressione**list_fold(function|_,Omitted_->id_fold|_,Arg(e,_)->of_expressione)ls|Texp_match(e,_,cs,_)->of_expressione**list_foldof_casecs|Texp_try(e,cs)->of_expressione**list_foldof_casecs|Texp_tuple(es,_)->list_fold(fun(_lbl,e)->of_expressione)es|Texp_unboxed_tuplees->list_fold(fun(_lbl,e,_sort)->of_expressione)es|Texp_construct(_,_,es,_)|Texp_array(_,_,es,_)->list_foldof_expressiones|Texp_variant(_,Some(e,_))|Texp_assert(e,_)|Texp_lazye|Texp_setinstvar(_,_,_,e)->of_expressione|Texp_setmutvar(_,_,e)->of_expressione|Texp_record{fields;extended_expression}->option_fold(fun(e,_,_)->of_expressione)extended_expression**letfold_field=function|_,Typedtree.Kept_->id_fold|desc,Typedtree.Overridden(lid_loc,e)->of_exp_record_fieldelid_locdescLegacy**of_expressioneinarray_foldfold_fieldfields|Texp_record_unboxed_product{fields;extended_expression}->option_fold(fun(e,_)->of_expressione)extended_expression**letfold_field=function|_,Typedtree.Kept_->id_fold|desc,Typedtree.Overridden(lid_loc,e)->of_exp_record_fieldelid_locdescUnboxed_product**of_expressioneinarray_foldfold_fieldfields|Texp_field(e,_,lid_loc,lbl,_,_)->of_expressione**of_exp_record_fieldelid_loclblLegacy|Texp_unboxed_field(e,_,lid_loc,lbl,_)->of_expressione**of_exp_record_fieldelid_loclblUnboxed_product|Texp_setfield(e1,_,lid_loc,lbl,e2)->of_expressione1**of_expressione2**of_exp_record_fielde1lid_loclblLegacy|Texp_ifthenelse(e1,e2,None)|Texp_sequence(e1,_,e2)|Texp_while{wh_cond=e1;wh_body=e2;_}|Texp_overwrite(e1,e2)->of_expressione1**of_expressione2|Texp_ifthenelse(e1,e2,Somee3)|Texp_for{for_from=e1;for_to=e2;for_body=e3;_}->of_expressione1**of_expressione2**of_expressione3|Texp_list_comprehensioncs|Texp_array_comprehension(_,_,cs)->of_comprehensioncs|Texp_send(e,meth,_)->of_expressione**of_method_callemethloc(* TODO ulysse CHECK*)|Texp_override(_,ls)->list_fold(fun(_,_,e)->of_expressione)ls|Texp_letmodule(mb_id,mb_name,mb_presence,mb_expr,e)->letmb={mb_id;mb_name;mb_expr;mb_loc=Location.none;mb_attributes=[];mb_presence;mb_uid=Shape.Uid.internal_not_actually_unique}inapp(Module_bindingmb)**of_expressione|Texp_letexception(ec,e)->app(Extension_constructorec)**of_expressione|Texp_object(cs,_)->app(Class_structurecs)|Texp_packme->of_module_exprme|Texp_unreachable|Texp_extension_constructor_->id_fold|Texp_letop{let_;ands;body;_}->(* let+ ..pat1 and pat2 and ... are represented as pattern couples:
[pat1; [pat2; ...]]. The following function flattens these couples.
Keeping track of the known size of the tuple prevent wrongly flattening
the patterns patN when they are tuples themselves. *)letrecflatten_patterns~sizeaccpat=matchpat.pat_descwith|Tpat_tuple[(None,tuple);(None,pat)]whensize>0->flatten_patterns~size:(size-1)(pat::acc)tuple|_->List.rev(pat::acc)inletbindops=let_::andsinletpatterns=flatten_patterns~size:(List.lengthands)[]body.c_lhsinletof_letop(pat,bindop)=of_bopbindop**of_patternpatinlist_foldof_letop(List.combinepatternsbindops)**of_expressionbody.c_rhs|Texp_open(od,e)->app(Module_exprod.open_expr)**of_expressione|Texp_probep->of_expressionp.handler|Texp_probe_is_enabled_->id_fold|Texp_exclavee->of_expressione|Texp_idx(block_access,unboxed_access)->of_block_accessblock_access**list_foldof_unboxed_accessunboxed_access|Texp_atomic_loc(exp,_,_,_,_)->of_expressionexp|Texp_hole_->id_fold|Texp_quotationexp->of_expressionexp|Texp_antiquotationexp->of_expressionexp|Texp_apply_layout(exp,_)->of_expressionexp(* We should consider taking into account param.fp_loc at some point, as it
allows us to respond with the *parameter*'s type (as opposed to the
function's type) when the user queries the label:
let f ?y:(x = 3) () = x
^
*)andof_function_paramfp=of_function_param_kindfp.fp_kind**list_fold(fun(_,_,jkind,_)->of_jkind_annotation_optjkind)fp.fp_newtypes**of_modesfp.fp_modeandof_function_param_kind=function|Tparam_patpat->of_patternpat|Tparam_optional_default(pat,exp,_)->of_patternpat**of_expressionexpandof_function_body=function|Tfunction_bodyexp->of_expressionexp|Tfunction_casesfc->list_foldof_casefc.fc_casesandof_class_expr_desc=function|Tcl_ident(_,_,cts)->list_foldof_core_typects|Tcl_structurecs->app(Class_structurecs)|Tcl_fun(_,p,es,ce,_)->list_fold(fun(_,e)->of_expressione)es**of_patternp**app(Class_exprce)|Tcl_apply(ce,es)->list_fold(function|_,Omitted_->id_fold|_,Arg(e,_)->of_expressione)es**app(Class_exprce)|Tcl_let(_,vbs,es,ce)->list_foldof_value_bindingvbs**list_fold(fun(_,e)->of_expressione)es**app(Class_exprce)|Tcl_constraint(ce,cto,_,_,_)->option_fold(funct->app(Class_typect))cto**app(Class_exprce)|Tcl_open(_,ce)->app(Class_exprce)andof_class_field_desc=function|Tcf_inherit(_,ce,_,_,_)->app(Class_exprce)|Tcf_val(_,_,_,cfk,_)|Tcf_method(_,_,cfk)->app(Class_field_kindcfk)|Tcf_constraint(ct1,ct2)->of_core_typect1**of_core_typect2|Tcf_initializere->of_expressione|Tcf_attribute_->id_fold(*TODO*)andof_module_expr_desc=function|Tmod_ident_->id_fold|Tmod_structurestr->app(Structurestr)|Tmod_functor(Unit,me)->of_module_exprme|Tmod_functor(Named(_,_,mt,modes),me)->of_module_typemt**of_module_exprme**of_modesmodes|Tmod_apply(me1,me2,_)->of_module_exprme1**of_module_exprme2|Tmod_apply_unitme1->of_module_exprme1|Tmod_constraint(me,_,mtc,_)->of_module_exprme**app(Module_type_constraintmtc)|Tmod_unpack(e,_)->of_expressione|Tmod_typed_hole->id_foldandof_structure_item_desc=function|Tstr_eval(e,_,_)->of_expressione|Tstr_value(_,vbs)->list_foldof_value_bindingvbs|Tstr_primitivevd->app(Value_descriptionvd)|Tstr_type(_,tds)->list_fold(funtd->app(Type_declarationtd))tds|Tstr_typexttext->app(Type_extensiontext)|Tstr_exceptiontexn->app(Extension_constructortexn.tyexn_constructor)|Tstr_modulemb->app(Module_bindingmb)|Tstr_recmodulembs->list_fold(funx->app(Module_bindingx))mbs|Tstr_modtypemtd->app(Module_type_declarationmtd)|Tstr_classcds->list_fold(fun(cd,_)->app(Class_declarationcd))cds|Tstr_class_typectds->list_fold(fun(_,_,ctd)->app(Class_type_declarationctd))ctds|Tstr_includei->app(Include_declarationi)|Tstr_opend->app(Open_declarationd)|Tstr_attribute_->id_fold|Tstr_jkindjkind->app(Jkind_declarationjkind)andof_module_type_desc=function|Tmty_ident_|Tmty_alias_->id_fold(* CR module strengthening: need to also fold on the module expression *)|Tmty_strengthen(mty,_,_)->of_module_typemty|Tmty_signaturesg->app(Signaturesg)|Tmty_functor(Named(_,_,mt1,modes1),mt2,modes2)->of_module_typemt1**of_module_typemt2**of_modesmodes1**of_modesmodes2|Tmty_functor(Unit,mt,modes)->of_module_typemt**of_modesmodes|Tmty_with(mt,wcs)->list_fold(fun(_,_,wc)->app(With_constraintwc))wcs**of_module_typemt|Tmty_typeofme->of_module_exprmeandof_signature_item_desc=function|Tsig_attribute_->id_fold|Tsig_opend->app(Open_descriptiond)|Tsig_valuevd->app(Value_descriptionvd)|Tsig_type(_,tds)->list_fold(funtd->app(Type_declarationtd))tds|Tsig_typexttext->app(Type_extensiontext)|Tsig_exceptiontexn->app(Extension_constructortexn.tyexn_constructor)|Tsig_modulemd->app(Module_declarationmd)|Tsig_recmodulemds->list_fold(funmd->app(Module_declarationmd))mds|Tsig_modtypemtd->app(Module_type_declarationmtd)|Tsig_include(i,modalities)->app(Include_descriptioni)**of_modalitiesmodalities|Tsig_classcds->list_fold(funcd->app(Class_descriptioncd))cds|Tsig_class_typectds->list_fold(functd->app(Class_type_declarationctd))ctds|Tsig_typesubsttds->(* FIXME: shitty approximation *)list_fold(funtd->app(Type_declarationtd))tds|Tsig_modsubst_ms->(* TODO. *)id_fold|Tsig_modtypesubst_mts->(* TODO. *)id_fold|Tsig_jkindjkind->app(Jkind_declarationjkind)andof_core_type_desc=function|Ttyp_var(_,jkind)->of_jkind_annotation_optjkind|Ttyp_call_pos->id_fold|Ttyp_of_kindjkind->of_jkind_annotationjkind|Ttyp_open(_,_,ct)->of_core_typect|Ttyp_arrow(_,ct1,modes1,ct2,modes2)->of_core_typect1**of_core_typect2**of_modesmodes1**of_modesmodes2|Ttyp_tuplects->list_fold(fun(_,ty)->of_core_typety)cts|Ttyp_unboxed_tuplects->list_fold(fun(_,ty)->of_core_typety)cts|Ttyp_constr(_,_,cts)|Ttyp_class(_,_,cts)->list_foldof_core_typects|Ttyp_object(cts,_)->list_fold(funof_->matchof_.of_descwith|OTtag(_,ct)|OTinheritct->of_core_typect)cts|Ttyp_poly(bindings,ct)->list_fold(fun(_,jkind)->of_jkind_annotation_optjkind)bindings**of_core_typect|Ttyp_alias(ct,_,jkind)->of_core_typect**of_jkind_annotation_optjkind|Ttyp_variant(rfs,_,_)->list_fold(funrf->app(Row_fieldrf))rfs|Ttyp_packagept->app(Package_typept)|Ttyp_quotect|Ttyp_splicect|Ttyp_repr(_,ct)|Ttyp_newlayout(_,ct)->of_core_typectandof_class_type_desc=function|Tcty_constr(_,_,cts)->list_foldof_core_typects|Tcty_signaturecs->app(Class_signaturecs)|Tcty_arrow(_,ct,clt)->of_core_typect**app(Class_typeclt)|Tcty_open(_,ct)->app(Class_typect)andof_class_type_field_desc=function|Tctf_inheritct->app(Class_typect)|Tctf_val(_,_,_,ct)|Tctf_method(_,_,_,ct)->of_core_typect|Tctf_constraint(ct1,ct2)->of_core_typect1**of_core_typect2|Tctf_attribute_->id_foldletof_jkind_annotation_desc:Parsetree.jkind_annotation_desc->_=letof_core_type(_:Parsetree.core_type)=(* CR-someday: Replace [Parsetree.jkind_annotation] with a version where types are
[Typedtree.core_type]s rather than [Parsetree.core_type]s. Then use the proper
[of_core_type] that's defined in this module above.
*)id_foldinfunction|Pjk_default->id_fold|Pjk_abbreviation(_,scannable_axis_annotations)->list_foldof_scannable_axis_annotationscannable_axis_annotations|Pjk_mod(jkind,mod_bounds)->of_jkind_annotationjkind**list_foldof_mod_boundmod_bounds|Pjk_with(jkind,ct,modalities)->(* Todo: The compiler should get updated so that the translated modalities are
included in the typed tree. *)letmodalities=Typemode.transl_modalities~maturity:StableImmutablemodalitiesinof_jkind_annotationjkind**of_core_typect**of_modalitiesmodalities|Pjk_kind_ofct->of_core_typect|Pjk_productjkinds->list_foldof_jkind_annotationjkindsletof_attribute(attr:attribute)=letname=attr.attr_name.txtin(* There are a number of attributes that start with "merlin." that either modify Merlin
behavior (ex: merlin.loc, merlin.hide, merlin.focus) or deal with the type-checker's
error recovery (ex: merlin.incorrect). Including these attributes in the browse tree
causes Merlin to sometimes choose an incorrect node. These attributes are also
uninteresting - in practice they don't appear in user-written code. *)matchString.is_prefixedname~by:"merlin."with|true->id_fold|false->app(Attributeattr)letof_nodenode=letwithout_attributes=matchnodewith|Dummy->id_fold|Pattern{pat_desc;pat_extra=_}->of_pattern_descpat_desc|Expression{exp_desc;exp_extra=_;exp_loc}->of_expression_descexp_locexp_desc|Case{c_lhs;c_guard;c_rhs}->of_patternc_lhs**of_expressionc_rhs**option_foldof_expressionc_guard|Class_expr{cl_desc}->of_class_expr_desccl_desc|Class_structure{cstr_self;cstr_fields}->of_patterncstr_self**list_fold(funf->app(Class_fieldf))cstr_fields|Class_field{cf_desc}->of_class_field_desccf_desc|Class_field_kind(Tcfk_virtualct)->of_core_typect|Class_field_kind(Tcfk_concrete(_,e))->of_expressione|Module_expr{mod_desc}->of_module_expr_descmod_desc|Module_type_constraintTmodtype_implicit->id_fold|Module_type_constraint(Tmodtype_explicit(mt,modes))->of_module_typemt**of_modesmodes|Structure{str_items;str_final_env}->list_fold_with_next(funnextitem->matchnextwith|None->app(Structure_item(item,str_final_env))|Someitem'->app(Structure_item(item,item'.str_env)))str_items|Structure_item({str_desc},_)->of_structure_item_descstr_desc|Module_bindingmb->app(Module_exprmb.mb_expr)**app(Module_binding_namemb)|Value_binding{vb_pat;vb_expr}->of_patternvb_pat**of_expressionvb_expr|Module_type{mty_desc}->of_module_type_descmty_desc|Signature{sig_items;sig_final_env;sig_modalities}->list_fold_with_next(funnextitem->matchnextwith|None->app(Signature_item(item,sig_final_env))|Someitem'->app(Signature_item(item,item'.sig_env)))sig_items**of_modalitiessig_modalities|Signature_item({sig_desc},_)->of_signature_item_descsig_desc|Module_declarationmd->of_module_typemd.md_type**app(Module_declaration_namemd)**of_modalitiesmd.md_modalities|Module_type_declarationmtd->option_foldof_module_typemtd.mtd_type**app(Module_type_declaration_namemtd)|With_constraint(Twith_typetd|Twith_typesubsttd)->app(Type_declarationtd)|With_constraint(Twith_module_|Twith_modsubst_)->id_fold|With_constraint(Twith_modtypemt|Twith_modtypesubstmt)->of_module_typemt|With_constraint(Twith_jkindjk|Twith_jkindsubstjk)->app(Jkind_declarationjk)|Core_type{ctyp_desc}->of_core_type_descctyp_desc|Package_type{pack_fields}->list_fold(fun(_,ct)->of_core_typect)pack_fields|Row_fieldrf->beginmatchrf.rf_descwith|Ttag(_,_,cts)->list_foldof_core_typects|Tinheritct->of_core_typectend|Value_description{val_desc;val_modal_info}->of_core_typeval_desc**of_value_description_modal_infoval_modal_info|Type_declaration{typ_params;typ_cstrs;typ_kind;typ_manifest;typ_jkind_annotation}->letof_typ_cstrs(ct1,ct2,_)=of_core_typect1**of_core_typect2inoption_foldof_core_typetyp_manifest**list_foldof_typ_paramtyp_params**app(Type_kindtyp_kind)**list_foldof_typ_cstrstyp_cstrs**of_jkind_annotation_opttyp_jkind_annotation|Type_kind(Ttype_abstract|Ttype_open)->id_fold|Type_kind(Ttype_variantcds)->list_fold(funcd->app(Constructor_declarationcd))cds|Type_kind(Ttype_recordlds)|Type_kind(Ttype_record_unboxed_productlds)->list_foldof_label_declarationlds|Type_extension{tyext_params;tyext_constructors}->list_foldof_typ_paramtyext_params**list_fold(funec->app(Extension_constructorec))tyext_constructors|Extension_constructor{ext_kind=Text_decl(cvars,carg,cto)}->option_foldof_core_typecto**of_constructor_argumentscarg**list_fold(fun(_,jkind)->of_jkind_annotation_optjkind)cvars|Extension_constructor{ext_kind=Text_rebind_}->id_fold|Label_declaration{ld_type;ld_modalities}->of_core_typeld_type**of_modalitiesld_modalities|Constructor_declaration{cd_args;cd_res;cd_vars}->option_foldof_core_typecd_res**of_constructor_argumentscd_args**list_fold(fun(_,jkind)->of_jkind_annotation_optjkind)cd_vars|Class_type{cltyp_desc}->of_class_type_desccltyp_desc|Class_signature{csig_self;csig_fields}->of_core_typecsig_self**list_fold(funx->app(Class_type_fieldx))csig_fields|Class_type_field{ctf_desc}->of_class_type_field_descctf_desc|Class_declaration{ci_params;ci_expr}->app(Class_exprci_expr)**list_foldof_typ_paramci_params|Class_description{ci_params;ci_expr}->app(Class_typeci_expr)**list_foldof_typ_paramci_params|Class_type_declaration{ci_params;ci_expr}->app(Class_typeci_expr)**list_foldof_typ_paramci_params|Method_call_->id_fold|Record_field_->id_fold|Module_binding_name_->id_fold|Module_declaration_name_->id_fold|Module_type_declaration_name_->id_fold|Open_description_->id_fold|Open_declarationod->app(Module_exprod.open_expr)|Include_declarationi->of_module_expri.incl_mod|Include_descriptioni->of_module_typei.incl_mod|Binding_op{bop_exp=_}->id_fold|Mode_->id_fold|Modality_->id_fold|Jkind_annotation{pjka_desc}->of_jkind_annotation_descpjka_desc|Jkind_declaration{jkind_annotation}->option_foldof_jkind_annotationjkind_annotation|Mod_bound_->id_fold|Scannable_axis_annotation_->id_fold|Attribute_->id_foldinwithout_attributes**list_foldof_attribute(node_attributesnode)letfold_nodefenvnodeacc=of_nodenodeenvfacc(** Accessors for information specific to a node *)letstring_of_node=function|Dummy->"dummy"|Patternp->letfmt,printer=Format.to_string()inPrinttyped.pattern0fmtp;printer()|Expression_->"expression"|Case_->"case"|Class_expr_->"class_expr"|Class_structure_->"class_structure"|Class_field_->"class_field"|Class_field_kind_->"class_field_kind"|Module_expr_->"module_expr"|Module_type_constraint_->"module_type_constraint"|Structure_->"structure"|Structure_item_->"structure_item"|Module_binding_->"module_binding"|Value_binding_->"value_binding"|Module_type_->"module_type"|Signature_->"signature"|Signature_item_->"signature_item"|Module_declaration_->"module_declaration"|Module_type_declaration_->"module_type_declaration"|With_constraint_->"with_constraint"|Core_type_->"core_type"|Package_type_->"package_type"|Row_field_->"row_field"|Value_description_->"value_description"|Type_declaration_->"type_declaration"|Type_kind_->"type_kind"|Type_extension_->"type_extension"|Extension_constructor_->"extension_constructor"|Label_declaration_->"label_declaration"|Constructor_declaration_->"constructor_declaration"|Class_type_->"class_type"|Class_signature_->"class_signature"|Class_type_field_->"class_type_field"|Class_declaration_->"class_declaration"|Class_description_->"class_description"|Class_type_declaration_->"class_type_declaration"|Binding_op_->"binding_op"|Method_call_->"method_call"|Record_field_->"record_field"|Module_binding_name_->"module_binding_name"|Module_declaration_name_->"module_declaration_name"|Module_type_declaration_name_->"module_type_declaration_name"|Open_description_->"open_description"|Open_declaration_->"open_declaration"|Include_description_->"include_description"|Include_declaration_->"include_declaration"|Mode_->"mode"|Modality_->"modality"|Jkind_annotation_->"jkind_annotation"|Jkind_declaration_->"jkind_declaration"|Mod_bound_->"mod_bound"|Scannable_axis_annotation_->"scannable_axis_annotation"|Attribute_->"attribute"letmkloc=Location.mklocletreloctxtloc={locwithLocation.txt}letmk_lidentx=Longident.Lidentxlettype_constructor_pathtyp=matchTypes.get_desctypwith|Types.Tconstr(p,_,_)->p|_->raiseNot_found(* Build a fake path for value constructors and labels *)letfake_path{Location.loc;txt=lid}typname=matchtype_constructor_pathtypwith|Path.Pdot(p,_)->[(mkloc(Path.Pdot(p,name))loc,Somelid)]|Path.Pident_->[(mkloc(Path.Pident(Ident.create_persistentname))loc,Somelid)]|_|(exceptionNot_found)->[]letpattern_paths(typek){Typedtree.pat_desc;pat_extra;_}=letinit=match(pat_desc:kpattern_desc)with|Tpat_construct(lid_loc,{Types.cstr_name;cstr_res;_},_,_)->fake_pathlid_loccstr_rescstr_name|Tpat_var{id;name={loc;txt};_}->[(mkloc(Path.Pidentid)loc,Some(Longident.Lidenttxt))]|Tpat_alias{id;name;_}->[(reloc(Path.Pidentid)name,Some(Longident.Lidentname.txt))]|_->[]inList.fold_left~initpat_extra~f:(funacc(extra,_,_)->matchextrawith|Tpat_open(path,loc,_)|Tpat_type(path,loc)->(relocpathloc,Someloc.txt)::acc|_->acc)letmodule_expr_paths{Typedtree.mod_desc}=matchmod_descwith|Tmod_ident(path,loc)->[(relocpathloc,Someloc.txt)]|Tmod_functor(Named(Someid,loc,_,_),_)->[(reloc(Path.Pidentid)loc,Option.map~f:mk_lidentloc.txt)]|_->[]letbindop_path{bop_op_name;bop_op_path}=letloc=bop_op_nameinletpath=bop_op_pathin(relocpathloc,Some(Longident.Lidentloc.txt))letexpression_paths{Typedtree.exp_desc;exp_extra;_}=letinit=matchexp_descwith|Texp_ident{path;lid=loc;_}->[(relocpathloc,Someloc.txt)]|Texp_letop{let_;ands}->bindop_pathlet_::List.map~f:bindop_pathands|Texp_new(path,loc,_,_)->[(relocpathloc,Someloc.txt)]|Texp_instvar(_,path,loc)->[(relocpathloc,Some(Lidentloc.txt))]|Texp_setinstvar(_,path,loc,_)->[(relocpathloc,Some(Lidentloc.txt))]|Texp_override(_,ps)->List.map~f:(fun(id,loc,_)->(reloc(Path.Pidentid)loc,Some(Longident.Lidentloc.txt)))ps|Texp_letmodule(Someid,loc,_,_,_)->[(reloc(Path.Pidentid)loc,Option.map~f:mk_lidentloc.txt)]|Texp_for{for_id=id;for_pat={Parsetree.ppat_loc=loc;ppat_desc};_}->letlid=matchppat_descwith|Ppat_any->None|Ppat_var{txt}->Some(Longident.Lidenttxt)|_->assertfalsein[(mkloc(Path.Pidentid)loc,lid)]|Texp_construct(lid_loc,{Types.cstr_name;cstr_res;_},_,_)->fake_pathlid_loccstr_rescstr_name|Texp_open(od,_)->module_expr_pathsod.open_expr(* Normally, [expression_paths] just works at top-level, without
going deep into an expression. But freshly bound newtypes are
not part of any other expression or pattern or type, so they
need to be retrieved here. *)|Texp_function{params;_}->List.concat_mapparams~f:(fun{fp_newtypes;_}->List.concat_mapfp_newtypes~f:(fun(id,(label_loc:_Location.loc),_,_)->letpath=Path.Pidentidinletlid=Longident.Lidentlabel_loc.txtin[(mklocpathlabel_loc.loc,Somelid)]))|_->[]inList.fold_left~initexp_extra~f:(funacc(extra,_,_)->matchextrawith|Texp_newtype(id,label_loc,_,_)->letpath=Path.Pidentidinletlid=Longident.Lidentlabel_loc.txtin(mklocpathlabel_loc.loc,Somelid)::acc|_->acc)letcore_type_paths{Typedtree.ctyp_desc}=matchctyp_descwith|Ttyp_constr(path,loc,_)->[(relocpathloc,Someloc.txt)]|Ttyp_class(path,loc,_)->[(relocpathloc,Someloc.txt)]|_->[]letclass_expr_paths{Typedtree.cl_desc}=matchcl_descwith|Tcl_ident(path,loc,_)->[(relocpathloc,Someloc.txt)]|_->[]letclass_field_paths{Typedtree.cf_desc}=matchcf_descwith|Tcf_val(loc,_,id,_,_)->[(reloc(Path.Pidentid)loc,Some(Longident.Lidentloc.txt))]|_->[]letstructure_item_paths{Typedtree.str_desc}=matchstr_descwith|Tstr_class_typecls->List.map~f:(fun(id,loc,_)->(reloc(Path.Pidentid)loc,Some(Longident.Lidentloc.txt)))cls|Tstr_openod->module_expr_pathsod.open_expr|_->[]letmodule_type_paths{Typedtree.mty_desc}=matchmty_descwith|Tmty_ident(path,loc)|Tmty_alias(path,loc)->[(relocpathloc,Someloc.txt)]|Tmty_functor(Named(Someid,loc,_,_),_,_)->[(reloc(Path.Pidentid)loc,Option.map~f:mk_lidentloc.txt)]|Tmty_with(_,ls)->List.map~f:(fun(p,l,_)->(relocpl,Somel.txt))ls|_->[]letsignature_item_paths{Typedtree.sig_desc}=matchsig_descwith|Tsig_open{Typedtree.open_expr=open_path,open_txt;_}->[(relocopen_pathopen_txt,Someopen_txt.txt)]|_->[]letwith_constraint_paths=function|Twith_module(path,loc)|Twith_modsubst(path,loc)->[(relocpathloc,Someloc.txt)]|_->[]letci_paths{Typedtree.ci_id_name;ci_id_class}=[(reloc(Path.Pidentci_id_class)ci_id_name,Some(Longident.Lidentci_id_name.txt))]letnode_paths_full=letopenTypedtreeinfunction|Patternp->pattern_pathsp|Expressione->expression_pathse|Class_expre->class_expr_pathse|Class_fieldf->class_field_pathsf|Module_exprme->module_expr_pathsme|Structure_item(i,_)->structure_item_pathsi|Module_binding_name{mb_id=Somemb_id;mb_name}->[(reloc(Path.Pidentmb_id)mb_name,Option.map~f:mk_lidentmb_name.txt)]|Module_typemt->module_type_pathsmt|Signature_item(i,_)->signature_item_pathsi|Module_declaration_name{md_id=Somemd_id;md_name}->[(reloc(Path.Pidentmd_id)md_name,Option.map~f:mk_lidentmd_name.txt)]|Module_type_declaration_name{mtd_id;mtd_name}->[(reloc(Path.Pidentmtd_id)mtd_name,Some(Lidentmtd_name.txt))]|With_constraintc->with_constraint_pathsc|Core_typect->core_type_pathsct|Package_type{pack_path;pack_txt}->[(relocpack_pathpack_txt,Somepack_txt.txt)]|Value_description{val_id;val_name}->[(reloc(Path.Pidentval_id)val_name,Some(Lidentval_name.txt))]|Type_declaration{typ_id;typ_name}->[(reloc(Path.Pidenttyp_id)typ_name,Some(Lidenttyp_name.txt))]|Type_extension{tyext_path;tyext_txt}->[(reloctyext_pathtyext_txt,Sometyext_txt.txt)]|Extension_constructor{ext_id;ext_name}->[(reloc(Path.Pidentext_id)ext_name,Some(Lidentext_name.txt))]|Label_declaration{ld_id;ld_name}->[(reloc(Path.Pidentld_id)ld_name,Some(Lidentld_name.txt))]|Constructor_declaration{cd_id;cd_name}->[(reloc(Path.Pidentcd_id)cd_name,Some(Lidentcd_name.txt))]|Class_declarationci->ci_pathsci|Class_descriptionci->ci_pathsci|Class_type_declarationci->ci_pathsci|Record_field(_,{Types.lbl_res;lbl_name;_},_,lid_loc)->fake_pathlid_loclbl_reslbl_name|_->[]letnode_pathst=List.map(node_paths_fullt)~f:fstletnode_paths_and_longidentt=List.filter_map(node_paths_fullt)~f:(function|_,None->None|p,Somelid->Some(p,lid))letnode_is_constructor=function|Constructor_declarationdecl->Some{decl.cd_namewithLocation.txt=`Declarationdecl}|Expression{exp_desc=Texp_construct(loc,desc,_,_)}->Some{locwithLocation.txt=`Descriptiondesc}|Pattern{pat_desc=Tpat_construct(loc,desc,_,_)}->Some{locwithLocation.txt=`Descriptiondesc}|Extension_constructorext_cons->Some{Location.loc=ext_cons.ext_loc;txt=`Extension_constructorext_cons}|_->Noneletnode_of_binary_partenvpart=letopenCmt_formatinmatchpartwith|Partial_structurex->Structurex|Partial_structure_itemx->Structure_item(x,env)|Partial_expressionx->Expressionx|Partial_pattern(_,x)->Patternx|Partial_class_exprx->Class_exprx|Partial_signaturex->Signaturex|Partial_signature_itemx->Signature_item(x,env)|Partial_module_typex->Module_typexletall_holes(env,node)=letrecauxacc(env,node)=letfenvnodeacc=matchnodewith|Expression{exp_desc=Texp_typed_hole;exp_loc;exp_type;exp_env;_}->(exp_loc,exp_env,`Expexp_type)::acc|Module_expr{mod_desc=Tmod_typed_hole;mod_loc;mod_type;mod_env;_}->(mod_loc,mod_env,`Modmod_type)::acc|_->auxacc(env,node)infold_nodefenvnodeaccinaux[](env,node)|>List.rev