Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file attribute.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581open!Importletpoly_equalab=letmodulePoly=structtypet=T:_->tendinStdppx.Poly.equal(Poly.Ta)(Poly.Tb);;moduleContext=structtype'at=|Label_declaration:label_declarationt|Constructor_declaration:constructor_declarationt|Type_declaration:type_declarationt|Type_exception:type_exceptiont|Type_extension:type_extensiont|Extension_constructor:extension_constructort|Pattern:patternt|Core_type:core_typet|Expression:expressiont|Value_description:value_descriptiont|Class_type:class_typet|Class_type_field:class_type_fieldt|Class_infos:_class_infost|Class_expr:class_exprt|Class_field:class_fieldt|Module_type:module_typet|Module_declaration:module_declarationt|Module_type_declaration:module_type_declarationt|Module_substitution:module_substitutiont|Open_description:open_descriptiont|Open_declaration:open_declarationt|Include_infos:_include_infost|Module_expr:module_exprt|Value_binding:value_bindingt|Module_binding:module_bindingt|Pstr_eval:structure_itemt|Pstr_extension:structure_itemt|Psig_extension:signature_itemt|Rtag:row_fieldt|Object_type_field:object_fieldtletlabel_declaration=Label_declarationletconstructor_declaration=Constructor_declarationlettype_declaration=Type_declarationlettype_extension=Type_extensionlettype_exception=Type_exceptionletextension_constructor=Extension_constructorletpattern=Patternletcore_type=Core_typeletexpression=Expressionletvalue_description=Value_descriptionletclass_type=Class_typeletclass_type_field=Class_type_fieldletclass_infos=Class_infosletclass_expr=Class_exprletclass_field=Class_fieldletmodule_type=Module_typeletmodule_declaration=Module_declarationletmodule_type_declaration=Module_type_declarationletopen_description=Open_descriptionletinclude_infos=Include_infosletmodule_expr=Module_exprletvalue_binding=Value_bindingletmodule_binding=Module_bindingletpstr_eval=Pstr_evalletpstr_extension=Pstr_extensionletpsig_extension=Psig_extensionletrtag=Rtagletobject_type_field=Object_type_fieldletget_pstr_evalst=matchst.pstr_descwith|Pstr_eval(e,l)->(e,l)|_->failwith"Attribute.Context.get_pstr_eval"letget_pstr_extensionst=matchst.pstr_descwith|Pstr_extension(e,l)->(e,l)|_->failwith"Attribute.Context.get_pstr_extension"letget_psig_extensionst=matchst.psig_descwith|Psig_extension(e,l)->(e,l)|_->failwith"Attribute.Context.get_psig_extension"letget_attributes:typea.at->a->attributes=funtx->matchtwith|Label_declaration->x.pld_attributes|Constructor_declaration->x.pcd_attributes|Type_declaration->x.ptype_attributes|Type_extension->x.ptyext_attributes|Type_exception->x.ptyexn_attributes|Extension_constructor->x.pext_attributes|Pattern->x.ppat_attributes|Core_type->x.ptyp_attributes|Expression->x.pexp_attributes|Value_description->x.pval_attributes|Class_type->x.pcty_attributes|Class_type_field->x.pctf_attributes|Class_infos->x.pci_attributes|Class_expr->x.pcl_attributes|Class_field->x.pcf_attributes|Module_type->x.pmty_attributes|Module_declaration->x.pmd_attributes|Module_type_declaration->x.pmtd_attributes|Module_substitution->x.pms_attributes|Open_description->x.popen_attributes|Open_declaration->x.popen_attributes|Include_infos->x.pincl_attributes|Module_expr->x.pmod_attributes|Value_binding->x.pvb_attributes|Module_binding->x.pmb_attributes|Pstr_eval->snd(get_pstr_evalx)|Pstr_extension->snd(get_pstr_extensionx)|Psig_extension->snd(get_psig_extensionx)|Rtag->x.prf_attributes|Object_type_field->x.pof_attributesletset_attributes:typea.at->a->attributes->a=funtxattrs->matchtwith|Label_declaration->{xwithpld_attributes=attrs}|Constructor_declaration->{xwithpcd_attributes=attrs}|Type_declaration->{xwithptype_attributes=attrs}|Type_extension->{xwithptyext_attributes=attrs}|Type_exception->{xwithptyexn_attributes=attrs}|Extension_constructor->{xwithpext_attributes=attrs}|Pattern->{xwithppat_attributes=attrs}|Core_type->{xwithptyp_attributes=attrs}|Expression->{xwithpexp_attributes=attrs}|Value_description->{xwithpval_attributes=attrs}|Class_type->{xwithpcty_attributes=attrs}|Class_type_field->{xwithpctf_attributes=attrs}|Class_infos->{xwithpci_attributes=attrs}|Class_expr->{xwithpcl_attributes=attrs}|Class_field->{xwithpcf_attributes=attrs}|Module_type->{xwithpmty_attributes=attrs}|Module_declaration->{xwithpmd_attributes=attrs}|Module_type_declaration->{xwithpmtd_attributes=attrs}|Module_substitution->{xwithpms_attributes=attrs}|Open_description->{xwithpopen_attributes=attrs}|Open_declaration->{xwithpopen_attributes=attrs}|Include_infos->{xwithpincl_attributes=attrs}|Module_expr->{xwithpmod_attributes=attrs}|Value_binding->{xwithpvb_attributes=attrs}|Module_binding->{xwithpmb_attributes=attrs}|Pstr_eval->{xwithpstr_desc=Pstr_eval(get_pstr_evalx|>fst,attrs)}|Pstr_extension->{xwithpstr_desc=Pstr_extension(get_pstr_extensionx|>fst,attrs)}|Psig_extension->{xwithpsig_desc=Psig_extension(get_psig_extensionx|>fst,attrs)}|Rtag->{xwithprf_attributes=attrs}|Object_type_field->{xwithpof_attributes=attrs}letdesc:typea.at->string=function|Label_declaration->"label declaration"|Constructor_declaration->"constructor declaration"|Type_declaration->"type declaration"|Type_extension->"type extension"|Type_exception->"type exception"|Extension_constructor->"extension constructor"|Pattern->"pattern"|Core_type->"core type"|Expression->"expression"|Value_description->"value"|Class_type->"class type"|Class_type_field->"class type field"|Class_infos->"class declaration"|Class_expr->"class expression"|Class_field->"class field"|Module_type->"module type"|Module_declaration->"module declaration"|Module_type_declaration->"module type declaration"|Module_substitution->"module substitution"|Open_description->"open"|Open_declaration->"open"|Include_infos->"include"|Module_expr->"module expression"|Value_binding->"value binding"|Module_binding->"module binding"|Pstr_eval->"toplevel expression"|Pstr_extension->"toplevel extension"|Psig_extension->"toplevel signature extension"|Rtag->"polymorphic variant tag"|Object_type_field->"object type field"(*
let pattern : type a b c d. a t
-> (attributes, b, c) Ast_pattern.t
-> (a, c, d) Ast_pattern.t
-> (a, b, d) Ast_pattern.t = function
| Label_declaration -> Ast_pattern.pld_attributes
| Constructor_declaration -> Ast_pattern.pcd_attributes
| Type_declaration -> Ast_pattern.ptype_attributes
| Type_extension -> Ast_pattern.ptyext_attributes
| Extension_constructor -> Ast_pattern.pext_attributes
*)letequal:_t->_t->bool=poly_equalendmoduleFloating_context=structtype'at=|Structure_item:structure_itemt|Signature_item:signature_itemt|Class_field:class_fieldt|Class_type_field:class_type_fieldtletstructure_item=Structure_itemletsignature_item=Signature_itemletclass_field=Class_fieldletclass_type_field=Class_type_fieldletget_attribute_if_is_floating_node:typea.at->a->attributeoption=funtx->matcht,xwith|Structure_item,{pstr_desc=Pstr_attributea;_}->Somea|Signature_item,{psig_desc=Psig_attributea;_}->Somea|Class_field,{pcf_desc=Pcf_attributea;_}->Somea|Class_type_field,{pctf_desc=Pctf_attributea;_}->Somea|_->Noneletget_attributetx=matchget_attribute_if_is_floating_nodetxwith|Somea->a|None->failwith"Attribute.Floating.Context.get_attribute"letreplace_by_dummy:typea.at->a->a=letdummy_ext=({txt="";loc=Location.none},PStr[])infuntx->matchtwith|Structure_item->{xwithpstr_desc=Pstr_extension(dummy_ext,[])}|Signature_item->{xwithpsig_desc=Psig_extension(dummy_ext,[])}|Class_field->{xwithpcf_desc=Pcf_extensiondummy_ext}|Class_type_field->{xwithpctf_desc=Pctf_extensiondummy_ext}letdesc:typea.at->string=function|Structure_item->"structure item"|Signature_item->"signature item"|Class_field->"class field"|Class_type_field->"class type field"letequal:_t->_t->bool=poly_equalendtypepacked_context=|On_item:_Context.t->packed_context|Floating:_Floating_context.t->packed_contexttype_payload_parser=Payload_parser:(payload,'a,'b)Ast_pattern.t*(name_loc:Location.t->'a)->'bpayload_parsertype('a,'b)t={name:Name.Pattern.t;context:'aContext.t;payload:'bpayload_parser}typepacked=T:(_,_)t->packedletnamet=Name.Pattern.namet.nameletcontextt=t.contextletregistrar=Name.Registrar.create~kind:"attribute"~current_file:__FILE__~string_of_context:(function|On_itemt->Some(Context.desct)|Floatingt->Some(Floating_context.desct^" (floating)"));;letdeclare_with_name_locnamecontextpatternk=Name.Registrar.register~kind:`Attributeregistrar(On_itemcontext)name;{name=Name.Pattern.makename;context;payload=Payload_parser(pattern,k)};;letdeclarenamecontextpatternk=declare_with_name_locnamecontextpattern(fun~name_loc:_->k);;moduleAttribute_table=Caml.Hashtbl.Make(structtypet=stringloclethash:t->int=Hashtbl.hashletequal:t->t->bool=Poly.equalend)letnot_seen=Attribute_table.create128letmark_as_seen{attr_name;_}=Attribute_table.removenot_seenattr_name;;letmark_as_handled_manually=mark_as_seenletexplicitly_drop=objectinheritAst_traverse.itermethod!attribute=mark_as_seenendletget_internal=letrecfind_best_matchtattributeslongest_match=matchattributeswith|[]->longest_match|{attr_name=name;_}asattr::rest->ifName.Pattern.matchest.namename.txtthenbeginmatchlongest_matchwith|None->find_best_matchtrest(Someattr)|Some{attr_name=name';_}->letlen=String.lengthname.txtinletlen'=String.lengthname'.txtiniflen>len'thenfind_best_matchtrest(Someattr)elseiflen<len'thenfind_best_matchtrestlongest_matchelseLocation.raise_errorf~loc:name.loc"Duplicated attribute"endelsefind_best_matchtrestlongest_matchinfuntattributes->find_best_matchtattributesNone;;letconvert?(do_mark_as_seen=true)patternattr=ifdo_mark_as_seenthenmark_as_seenattr;let(Payload_parser(pattern,k))=patterninAst_pattern.parsepattern(Common.loc_of_payloadattr)attr.attr_payload(k~name_loc:attr.attr_name.loc);;letgett?mark_as_seen:do_mark_as_seenx=letattrs=Context.get_attributest.contextxinmatchget_internaltattrswith|None->None|Someattr->Some(convertt.payloadattr?do_mark_as_seen);;letconsumetx=letattrs=Context.get_attributest.contextxinmatchget_internaltattrswith|None->None|Someattr->letattrs=List.filterattrs~f:(funattr'->not(attr==attr'))inletx=Context.set_attributest.contextxattrsinSome(x,convertt.payloadattr);;letremove_seen(typea)(context:aContext.t)packeds(x:a)=letattrs=Context.get_attributescontextxinletmatched=letrecloopacc=function|[]->acc|Tt::rest->ifContext.equalt.contextcontextthenmatchget_internaltattrswith|None->loopaccrest|Someattr->letname=attr.attr_nameinifAttribute_table.memnot_seennamethenloopaccrestelseloop(attr::acc)restelseloopaccrestinloop[]packedsinletattrs=List.filterattrs~f:(funattr'->not(List.memq~set:matchedattr'))inContext.set_attributescontextxattrs;;letpatterntp=letf=Ast_pattern.to_funcpinAst_pattern.of_func(functxlocxk->matchconsumetxwith|None->fctxlocx(kNone)|Some(x,v)->fctxlocx(k(Somev)));;moduleFloating=structmoduleContext=Floating_contexttype('a,'b)t={name:Name.Pattern.t;context:'aContext.t;payload:'bpayload_parser}letnamet=Name.Pattern.namet.nameletdeclarenamecontextpatternk=Name.Registrar.register~kind:`Attributeregistrar(Floatingcontext)name;{name=Name.Pattern.makename;context;payload=Payload_parser(pattern,fun~name_loc:_->k)};;letconverttsx=matchtswith|[]->None|{context;_}::_->assert(List.for_allts~f:(funt->Context.equalt.contextcontext));letattr=Context.get_attributecontextxinletname=attr.attr_nameinmatchList.filterts~f:(funt->Name.Pattern.matchest.namename.txt)with|[]->None|[t]->Some(convertt.payloadattr)|l->Location.raise_errorf~loc:name.loc"Multiple match for floating attributes: %s"(String.concat~sep:", "(List.mapl~f:(funt->Name.Pattern.namet.name)));;endletcheck_attributeregistrarcontextname=ifnot(Name.Whitelisted.is_whitelisted~kind:`Attributename.txt||Name.ignore_checksname.txt)&&Attribute_table.memnot_seennamethenletwhite_list=Name.Whitelisted.get_attribute_list()inName.Registrar.raise_errorfregistrarcontext~white_list"Attribute `%s' was not used"name;;letcheck_unused=object(self)inheritAst_traverse.iterassupermethod!attribute{attr_name=name;_}=Location.raise_errorf~loc:name.loc"attribute not expected here, Ppxlib.Attribute needs updating!"methodprivatecheck_node:typea.aContext.t->a->a=funcontextnode->letattrs=Context.get_attributescontextnodeinmatchattrswith|[]->node|_->List.iterattrs~f:(fun({attr_name=name;attr_payload=payload;_}asattr)->self#payloadpayload;check_attributeregistrar(On_itemcontext)name;(* If we allow the attribute to pass through, mark it as seen *)mark_as_seenattr);Context.set_attributescontextnode[]methodprivatecheck_floating:typea.aFloating.Context.t->a->a=funcontextnode->matchFloating.Context.get_attribute_if_is_floating_nodecontextnodewith|None->node|Some({attr_name=name;attr_payload=payload;_}asattr)->self#payloadpayload;check_attributeregistrar(Floatingcontext)name;mark_as_seenattr;Floating.Context.replace_by_dummycontextnodemethod!label_declarationx=super#label_declaration(self#check_nodeLabel_declarationx)method!constructor_declarationx=super#constructor_declaration(self#check_nodeConstructor_declarationx)method!type_declarationx=super#type_declaration(self#check_nodeType_declarationx)method!type_extensionx=super#type_extension(self#check_nodeType_extensionx)method!type_exceptionx=super#type_exception(self#check_nodeType_exceptionx)method!extension_constructorx=super#extension_constructor(self#check_nodeExtension_constructorx)method!patternx=super#pattern(self#check_nodePatternx)method!core_typex=super#core_type(self#check_nodeCore_typex)method!expressionx=super#expression(self#check_nodeExpressionx)method!value_descriptionx=super#value_description(self#check_nodeValue_descriptionx)method!class_typex=super#class_type(self#check_nodeClass_typex)method!class_infosfx=super#class_infosf(self#check_nodeClass_infosx)method!class_exprx=super#class_expr(self#check_nodeClass_exprx)method!module_typex=super#module_type(self#check_nodeModule_typex)method!module_declarationx=super#module_declaration(self#check_nodeModule_declarationx)method!module_type_declarationx=super#module_type_declaration(self#check_nodeModule_type_declarationx)method!open_descriptionx=super#open_description(self#check_nodeOpen_descriptionx)method!open_declarationx=super#open_declaration(self#check_nodeOpen_declarationx)method!include_infosfx=super#include_infosf(self#check_nodeInclude_infosx)method!module_exprx=super#module_expr(self#check_nodeModule_exprx)method!value_bindingx=super#value_binding(self#check_nodeValue_bindingx)method!module_bindingx=super#module_binding(self#check_nodeModule_bindingx)method!class_fieldx=letx=self#check_nodeClass_fieldxinletx=self#check_floatingClass_fieldxinsuper#class_fieldxmethod!class_type_fieldx=letx=self#check_nodeClass_type_fieldxinletx=self#check_floatingClass_type_fieldxinsuper#class_type_fieldxmethod!row_fieldx=letx=matchx.prf_descwith|Rtag_->self#check_nodeRtagx|_->xinsuper#row_fieldxmethod!core_type_descx=letx=matchxwith|Ptyp_object(fields,closed_flag)->letfields=List.mapfields~f:(self#check_nodeObject_type_field)inPtyp_object(fields,closed_flag)|_->xinsuper#core_type_descxmethod!structure_itemitem=letitem=self#check_floatingStructure_itemiteminletitem=matchitem.pstr_descwith|Pstr_eval_->self#check_nodePstr_evalitem|Pstr_extension_->self#check_nodePstr_extensionitem|_->iteminsuper#structure_itemitemmethod!signature_itemitem=letitem=self#check_floatingSignature_itemiteminletitem=matchitem.psig_descwith|Psig_extension_->self#check_nodePsig_extensionitem|_->iteminsuper#signature_itemitemendletreset_checks()=Attribute_table.clearnot_seenletcollect=objectinheritAst_traverse.iterassupermethod!attribute({attr_name=name;attr_payload=payload;_}asattr)=letloc=Common.loc_of_attributeattrinsuper#payloadpayload;Attribute_table.addnot_seennamelocendletcheck_all_seen()=letfailnameloc=lettxt=name.txtinifnot(Name.ignore_checkstxt)thenLocation.raise_errorf~loc"Attribute `%s' was silently dropped"txtinAttribute_table.iterfailnot_seen;;letremove_attributes_present_intable=objectinheritAst_traverse.iterassupermethod!attribute{attr_name=name;attr_payload=payload;_}=super#payloadpayload;Attribute_table.removetablenameendletcopy_of_not_seen()=letcopy=Attribute_table.create(Attribute_table.lengthnot_seen)inAttribute_table.iter(Attribute_table.addcopy)not_seen;copy;;letdropped_so_far_structurest=lettable=copy_of_not_seen()in(remove_attributes_present_intable)#structurest;Attribute_table.fold(funnamelocacc->{txt=name.txt;loc}::acc)table[];;letdropped_so_far_signaturesg=lettable=copy_of_not_seen()in(remove_attributes_present_intable)#signaturesg;Attribute_table.fold(funnamelocacc->{txt=name.txt;loc}::acc)table[];;