123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716(*
Copyright 2012-2020 Codinuum Software Lab <https://codinuum.com>
Copyright 2020-2025 Chiba Institute of Technology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)(* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *)[%%prepare_logger]moduleXlist=Diffast_misc.XlistmoduleBinding=Diffast_misc.BindingmoduleAstloc=Langs_common.AstlocmoduleBID=Binding.IDopenCommonmoduleC=ContextmoduleL=Labelletencode_identi=sprintf"%d%s"(String.lengthi)iletdecode_pat=Str.regexp"[0-9]+"letdecode_ident=Str.replace_firstdecode_pat""typepp_if_cond=PP_IFofstring*Obj.tlist|PP_IFDEFofident|PP_IFNDEFofidenttypepp_if_cond_sub=PP_NONE|PP_CLOSING|PP_STR|PP_EXPR|PP_INITtypepp_compl={mutablec_brace:int;mutablec_paren:int;}typepp_if_section_info={mutablei_line:int;mutablei_context:C.t;mutablei_sub_context:C.sub;mutablei_label:L.t;mutablei_brace_level:int;mutablei_paren_level:int;mutablei_templ_param_arg_level:int;mutablei_cond:pp_if_cond;mutablei_cond_sub:pp_if_cond_sub;mutablei_pp_elif:intlist;mutablei_pp_else:intoption;mutablei_lbraces:int;mutablei_rbraces:int;mutablei_odd:bool;mutablei_odd_canceled:bool;mutablei_broken:bool;mutablei_paren_closing:bool;mutablei_brace_closing:int;mutablei_brace_opening:bool;mutablei_func_head:bool;mutablei_func_body:bool;mutablei_semicolon:bool;mutablei_comma:bool;mutablei_cond_expr:bool;mutablei_cond_expr_:bool;mutablei_asm:bool;mutablei_begin_asm:bool;mutablei_pp_if_compl:pp_compl;mutablei_lack_of_dtor:bool;mutablei_class_brace_opening:bool;mutablei_follows_comma:bool;mutablei_brace_paren_closing:bool;mutablei_broken_func_head:bool;mutablei_templ_closing:bool;}letpp_if_cond_to_string=function|PP_IF(s,_)->sprintf"#if(%s)"s|PP_IFDEFs->sprintf"#ifdef(%s)"s|PP_IFNDEFs->sprintf"#ifndef(%s)"sletpp_if_cond_sub_to_string=function|PP_NONE->""|PP_CLOSING->"closing"|PP_STR->"str"|PP_EXPR->"expr"|PP_INIT->"init"letpp_compl_to_string{c_brace=blv;c_paren=plv;}=ifblv=0&&plv=0then""elsesprintf"{B:%d;P:%d}"blvplvletpp_if_section_info_to_string{i_line=ln;i_context=c;i_sub_context=sc;i_label=lab;i_brace_level=blv;i_paren_level=plv;i_templ_param_arg_level=tlv;i_cond=cond;i_cond_sub=cond_sub;i_pp_elif=ei;i_pp_else=es;i_lbraces=lb;i_rbraces=rb;i_odd=o;i_odd_canceled=oc;i_broken=b;i_paren_closing=pcl;i_brace_closing=bcl;i_brace_opening=bo;i_func_head=fh;i_func_body=fb;i_semicolon=s;i_comma=cm;i_cond_expr=ce;i_cond_expr_=ce_;i_asm=a;i_begin_asm=ba;i_pp_if_compl=cmpl;i_lack_of_dtor=lod;i_class_brace_opening=cbo;i_follows_comma=fcm;i_brace_paren_closing=bpc;i_broken_func_head=bfh;i_templ_closing=tcl;}=letl=["odd",o;"odd_canceled",oc;"broken",b;")",pcl;sprintf"}%d"bcl,bcl>0;"{",bo;"func_head",fh;"func_body",fb;";",s;",",cm;"?",ce;"?_",ce_;"asm",a;"begin_asm",ba;"lack_of_dtor",lod;"C{",cbo;",_",fcm;"})",bpc;"broken_func_head",bfh;">",tcl;]inletflags=String.concat""(List.map(fun(k,_)->"["^k^"]")(List.filter(fun(_,v)->v)l))insprintf"{%s%s@%d;%s,%s;%sLv{:%d;Lv(:%d;Lv<:%d;\"{\":%d;\"}\":%d;%s%s%s%s}"(pp_if_cond_to_stringcond)(pp_if_cond_sub_to_stringcond_sub)ln(C.to_stringc)(C.sub_to_stringsc)(matchlabwithDUMMY->""|_->(L.to_stringlab)^";")blvplvtlvlbrb(matcheiwith|_::_->sprintf"#elif:[%s];"(String.concat";"(List.mapstring_of_intei))|[]->"")(matcheswithSomei->sprintf"#else:%d;"i|None->"")(pp_compl_to_stringcmpl)flagsletmake_pp_if_section_info?(cond_sub=PP_NONE)?(pp_elif=[])?(pp_else=None)lncscblvplvtlvcond={i_line=ln;i_context=c;i_sub_context=sc;i_label=L.DUMMY;i_brace_level=blv;i_paren_level=plv;i_templ_param_arg_level=tlv;i_cond=cond;i_cond_sub=cond_sub;i_pp_elif=pp_elif;i_pp_else=pp_else;i_lbraces=0;i_rbraces=0;i_odd=false;i_odd_canceled=false;i_broken=false;i_paren_closing=false;i_brace_closing=0;i_brace_opening=false;i_func_head=false;i_func_body=false;i_semicolon=false;i_comma=false;i_cond_expr=false;i_cond_expr_=false;i_asm=false;i_begin_asm=false;i_pp_if_compl={c_brace=0;c_paren=0};i_lack_of_dtor=false;i_class_brace_opening=false;i_follows_comma=false;i_brace_paren_closing=false;i_broken_func_head=false;i_templ_closing=false;}letdummy_info=make_pp_if_section_info0C.TOPC.INI000(PP_IF("",[]))moduleElaboratedType=structtypet=|Classofname|Structofname|Unionofname|Enumofname|EnumClassofname|Macroofnameletto_string=function|Classn->"class "^n|Structn->"struct "^n|Unionn->"union "^n|Enumn->"enum "^n|EnumClassn->"enum_class"^n|Macron->nletget_name=function|Classn->n|Structn->n|Unionn->n|Enumn->n|EnumClassn->n|Macron->nletencode=function|Classn|Structn->"Ts"^n|Unionn->"Tu"^n|Enumn->"Te"^n|EnumClassn->"Tes"^n|Macron->nletis_type_type=function|Class_|Struct_|Union_|EnumClass_->true|_->falseend(* module Pinfo.ElaboratedType *)modulePlaceholderType=structtypet=|Auto|Decltypeletto_string=function|Auto->"auto"|Decltype->"decltype(auto)"letencode=function|Auto->"Da"|Decltype->"Dc"end(* module Pinfo.PlaceholderType *)moduleCvQualifier=structtypet=|Const|Volatile|Restrict|Cdecl|Stdcallletto_string=function|Const->"const"|Volatile->"volatile"|Restrict->"restrict"|Cdecl->"__cdecl"|Stdcall->"__stdcall"letencode=function|Const->"K"|Volatile->"V"|Restrict->"r"|Cdecl->"U7__cdecl"|Stdcall->"U9__stdcall"end(* module Pinfo.CvQualifier *)moduleTypeSpec=structtypet=|Simpleofname|Decltypeofstring|PlaceholderofPlaceholderType.t|Char|Char8_t|Char16_t|Char32_t|Wchar_t|Bool|Short|Int|Long|Signed|Unsigned|Float|Double|Void|UnsignedInt|UnsignedLong|ElaboratedofElaboratedType.t|Typenameofstring|CvQualifierofCvQualifier.tletget_cv_qualifier=function|CvQualifiercvq->cvq|_->raiseNot_foundletget_ident_opt=function|Simplen|Decltypen|Typenamen->Somen|Elaboratede->Some(ElaboratedType.get_namee)|_->Noneletget_name=function|Simplen|Typenamen->n|Elaboratede->ElaboratedType.get_namee|_->raiseNot_foundletto_string=function|Simplen->n|Decltypee->sprintf"decltype(%s)"e|Placeholderp->PlaceholderType.to_stringp|Char->"char"|Char8_t->"char8_t"|Char16_t->"char16_t"|Char32_t->"char32_t"|Wchar_t->"wchar_t"|Bool->"bool"|Short->"short"|Int->"int"|Long->"long"|Signed->"signed"|Unsigned->"unsigned"|Float->"float"|Double->"double"|Void->"void"|UnsignedInt->"unsigned int"|UnsignedLong->"unsigned long"|Elaboratede->ElaboratedType.to_stringe|Typenamee->"typename "^e|CvQualifiercvq->CvQualifier.to_stringcvqletis_definite_type=function|Decltype_|Placeholder_|Char|Char8_t|Char16_t|Char32_t|Wchar_t|Bool|Short|Int|Long|Signed|Unsigned|Float|Double|Void|UnsignedInt|UnsignedLong|Elaborated_->true|_->falseletis_basic_type=function|Char|Char8_t|Char16_t|Char32_t|Wchar_t|Bool|Short|Int|Long|Signed|Unsigned|Float|Double|Void|UnsignedInt|UnsignedLong->true|_->falseletis_elaborated_type=function|Elaborated_->true|_->falseletis_elaborated_type_type=function|Elaboratede->ElaboratedType.is_type_typee|_->falseletis_elaborated_type_enum_class=function|Elaborated(ElaboratedType.EnumClass_)->true|_->falseletis_typename=function|Typename_->true|_->falseletencodetl=letsigned=reffalseinletunsigned=reffalseinletlong_count=ref0inletcv_qualifiers=ref[]inletfiltered_tl=List.filter(function|Long->incrlong_count;false|Signed->signed:=true;false|Unsigned->unsigned:=true;false|CvQualifiercvq->begincv_qualifiers:=(CvQualifier.encodecvq)::!cv_qualifiers;falseend|_->true)tlinif!signed&&!unsignedthenbegin[%warn_log"signed and unsigned coexist"];signed:=false;unsigned:=false;end;cv_qualifiers:=List.fast_sort(funxy->compareyx)(!cv_qualifiers);let_encoded=String.concat""(List.map(function|Simplen->n|Decltypee->e|Placeholderp->PlaceholderType.encodep|Charwhen!signed->"a"|Charwhen!unsigned->"h"|Char->"c"|Char8_t->"Du"|Char16_t->"Ds"|Char32_t->"Di"|Wchar_t->"w"|Bool->"b"|Shortwhen!unsigned->"t"|Short->"s"|Intwhen!unsigned->"j"|Int->"i"|Float->"f"|Doublewhen!long_count=1->"e"|Double->"d"|Void->"v"|UnsignedInt->"j"|UnsignedLong->"m"|Elaboratede->ElaboratedType.encodee|Typenamee->e|_->"")filtered_tl)inlet_encoded=if_encoded=""then(match!unsigned,!long_countwith|true,0->"j"|false,0->"i"|true,1->"m"|false,1->"l"|true,2->"y"|false,2->"x"|true,_->"y"|false,_->"x")else_encodedin(String.concat""(!cv_qualifiers))^_encodedend(* module Pinfo.TypeSpec *)classdecl_specs=object(self)valmutablevec=0valstorage_class_static=1valstorage_class_thread_local=1lsl1valstorage_class_extern=1lsl2valstorage_class_mutable=1lsl3valfunction_spec_virtual=1lsl4valfunction_spec_explicit=1lsl5valfriend=1lsl6valtypedef=1lsl7valconstexpr=1lsl8valconsteval=1lsl9valinline=1lsl10valtypedef_mask=1lxor(1lsl7)methodset_storage_class_static()=vec<-veclorstorage_class_staticmethodset_storage_class_thread_local()=vec<-veclorstorage_class_thread_localmethodset_storage_class_extern()=vec<-veclorstorage_class_externmethodset_storage_class_mutable()=vec<-veclorstorage_class_mutablemethodset_function_spec_virtual()=vec<-veclorfunction_spec_virtualmethodset_function_spec_explicit()=vec<-veclorfunction_spec_explicitmethodset_friend()=vec<-veclorfriendmethodset_typedef()=vec<-veclortypedefmethodset_constexpr()=vec<-veclorconstexprmethodset_consteval()=vec<-veclorconstevalmethodset_inline()=vec<-veclorinlinemethodclear_typedef()=vec<-veclandtypedef_maskmethodis_storage_class_static=veclandstorage_class_static=storage_class_staticmethodis_storage_class_thread_local=veclandstorage_class_thread_local=storage_class_thread_localmethodis_storage_class_extern=veclandstorage_class_extern=storage_class_externmethodis_storage_class_mutable=veclandstorage_class_mutable=storage_class_mutablemethodis_function_spec_virtual=veclandfunction_spec_virtual=function_spec_virtualmethodis_function_spec_explicit=veclandfunction_spec_explicit=function_spec_explicitmethodis_friend=veclandfriend=friendmethodis_typedef=veclandtypedef=typedefmethodis_constexpr=veclandconstexpr=constexprmethodis_consteval=veclandconsteval=constevalmethodis_inline=veclandinline=inlinemethodto_string=letl=ref[]inifself#is_inlinethenl:="inline"::!l;ifself#is_constevalthenl:="consteval"::!l;ifself#is_constexprthenl:="constexpr"::!l;ifself#is_typedefthenl:="typedef"::!l;ifself#is_friendthenl:="friend"::!l;ifself#is_function_spec_explicitthenl:="explicit"::!l;ifself#is_function_spec_virtualthenl:="virtual"::!l;ifself#is_storage_class_mutablethenl:="mutable"::!l;ifself#is_storage_class_externthenl:="extern"::!l;ifself#is_storage_class_thread_localthenl:="thread_local"::!l;ifself#is_storage_class_staticthenl:="static"::!l;String.concat" "!lend(* class Pinfo.decl_specs *)classqualifiers=letconst=1inletvolatile=1lsl1inletamp=1lsl2inletamp_amp=1lsl3inletnoexcept_computed=1lsl4inletnoexcept=1lsl5inletthrow=1lsl6inletrestrict=1lsl7inletcdecl=1lsl8inletstdcall=1lsl9inobject(self)valmutablevec=0valtbl=Hashtbl.create0methodset_const()=vec<-veclorconstmethodset_volatile()=vec<-veclorvolatilemethodset_amp()=vec<-veclorampmethodset_amp_amp()=vec<-vecloramp_ampmethodset_noexcept_computede=vec<-veclornoexcept_computed;Hashtbl.addtblnoexcept_computedemethodset_noexcept()=vec<-veclornoexceptmethodset_throw()=vec<-veclorthrowmethodset_restrict()=vec<-veclorrestrictmethodset_cdecl()=vec<-veclorcdeclmethodset_stdcall()=vec<-veclorstdcallmethodis_const=veclandconst=constmethodis_volatile=veclandvolatile=volatilemethodis_amp=veclandamp=ampmethodis_amp_amp=veclandamp_amp=amp_ampmethodis_noexcept_computed=veclandnoexcept_computed=noexcept_computedmethodis_noexcept=veclandnoexcept=noexceptmethodis_throw=veclandthrow=throwmethodis_restrict=veclandrestrict=restrictmethodis_cdecl=veclandcdecl=cdeclmethodis_stdcall=veclandstdcall=stdcallmethodto_string=letl=ref[]inifself#is_constthenl:="const"::!l;ifself#is_volatilethenl:="volatile"::!l;ifself#is_ampthenl:="&"::!l;ifself#is_amp_ampthenl:="&&"::!l;ifself#is_noexcept_computedthenl:="noexcept()"::!l;ifself#is_noexceptthenl:="noexcept"::!l;ifself#is_throwthenl:="throw()"::!l;ifself#is_restrictthenl:="restrict"::!l;ifself#is_cdeclthenl:="__cdecl"::!l;ifself#is_stdcallthenl:="__stdcall"::!l;String.concat" "!lmethodencode_cv=letcvq=ref""inifself#is_restrictthencvq:=!cvq^"r";ifself#is_volatilethencvq:=!cvq^"V";ifself#is_constthencvq:=!cvq^"K";!cvqmethodencode_ref=letrefq=ref""inifself#is_ampthenrefq:="R";ifself#is_amp_ampthenrefq:="O";!refqmethodencode_exc=letexc=ref""inifself#is_noexcept||self#is_throwthenexc:="Do";ifself#is_noexcept_computedthenexc:=sprintf"DO%sE"(Hashtbl.findtblnoexcept_computed);!excmethodencode_ms=letmsq=ref""inifself#is_cdeclthenmsq:="U7__cdecl";ifself#is_stdcallthenmsq:="U9__stdcall";!msqend(* class Pinfo.qualifiers *)classvirt_specs=object(self)valmutablevec=0valfinal=1valoverride=1lsl1methodset_final()=vec<-veclorfinalmethodset_override()=vec<-vecloroverridemethodis_final=veclandfinal=finalmethodis_override=veclandoverride=overridemethodto_string=letl=ref[]inifself#is_finalthenl:="final"::!l;ifself#is_overridethenl:="override"::!l;String.concat" "!lend(* class Pinfo.virt_specs *)moduleType=structtypesimple_ty={st_decl_specs:decl_specs;st_type_specs:TypeSpec.tlist;st_encoded:string;}typepointer_op=|Starofstring*qualifiers|Amp|AmpAmp|Hat|Macroofstringtypet=|SimpleTyofsimple_ty|ArrayTyoft*int|PointerTyofpointer_ty|FunctionTyoffunction_ty|AltTyoftlistandpointer_ty={pt_op:pointer_op;pt_type:t;pt_qualifiers:qualifiers;}andfunction_ty={ft_param_types:tlist;ft_qualifiers:qualifiers;ft_return_type:t;ft_is_vararg:bool;ft_params_macro:string;ft_virt_specs:virt_specs;}typet_={mutablet_typedef:bool;t_desc:t;}letsimple_ty_to_string{st_decl_specs=ds;st_type_specs=ts;st_encoded=_;}=letds_str=ds#to_stringinifds_str=""thenbeginmatchtswith|[]->""|[t]->TypeSpec.to_stringt|_->sprintf"(%s)"(list_to_stringTypeSpec.to_string" "ts)endelsesprintf"(%s %s)"ds_str(list_to_stringTypeSpec.to_string" "ts)letget_name_of_simple_ty{st_decl_specs=_;st_type_specs=ts;st_encoded=_;}=matchtswith|[]->raiseNot_found|[t]->[TypeSpec.get_namet]|_->List.filter_map(funt->trySome(TypeSpec.get_namet)with_->None)tsletsimple_ty_has_definite_ty{st_type_specs=ts;_}=List.existsTypeSpec.is_definite_typetsletsimple_ty_has_basic_ty{st_type_specs=ts;_}=List.existsTypeSpec.is_basic_typetsletsimple_ty_has_elaborated_ty{st_type_specs=ts;_}=List.existsTypeSpec.is_elaborated_typetsletsimple_ty_has_elaborated_ty_enum_class{st_type_specs=ts;_}=List.existsTypeSpec.is_elaborated_type_enum_classtsletsimple_ty_has_typename{st_type_specs=ts;_}=List.existsTypeSpec.is_typenametsletsimple_ty_has_type_type?(name=""){st_type_specs=ts;_}=List.exists(funt->TypeSpec.is_elaborated_type_typet&&(name=""||matchTypeSpec.get_ident_opttwith|Somex->x=name|None->true))ts(* || List.exists TypeSpec.is_typename ts*)letget_cv_qualifiers_of_simple_tysty=List.fold_left(funlt->try(TypeSpec.get_cv_qualifiert)::lwithNot_found->l)[]sty.st_type_specsletpointer_op_to_string=function|Star(n,q)->beginletq_str=q#to_stringinifq_str=""thensprintf"%s*"nelsesprintf"(%s* %s)"nq_strend|Amp->"&"|AmpAmp->"&&"|Hat->"^"|Macroi->iletrecget_name=function|SimpleTysty->get_name_of_simple_tysty|ArrayTy(ty,_)->get_namety|PointerTypty->get_name_of_pointer_typty|FunctionTy_->raiseNot_found|AltTyts->List.flatten(List.filter_map(funt->trySome(get_namet)with_->None)ts)andget_name_of_pointer_ty{pt_op=_;pt_type=ty;pt_qualifiers=_;}=get_nametyletrecto_string=function|SimpleTysty->simple_ty_to_stringsty|ArrayTy(ty,dims)->sprintf"%s[%d]"(to_stringty)dims(*(to_string ty)^(String.concat "" (List.init dims (fun _ -> "[]")))*)|PointerTypty->pointer_ty_to_stringpty|FunctionTyfty->function_ty_to_stringfty|AltTyts->sprintf"(%s)"(list_to_stringto_string" | "ts)andpointer_ty_to_string{pt_op=op;pt_type=ty;pt_qualifiers=q;}=letq_str=q#to_stringinleto_str=pointer_op_to_stringopinifq_str=""then(to_stringty)^o_strelsesprintf"(%s %s)%s"(to_stringty)q_stro_strandfunction_ty_to_string{ft_param_types=ptys;ft_qualifiers=q;ft_return_type=rty;ft_is_vararg=is_va;ft_params_macro=params_macro;ft_virt_specs=v;}=ifparams_macro=""thensprintf"(%s%s->%s%s%s)"(String.concat","(List.mapto_stringptys))(ifis_vathen"..."else"")(to_stringrty)(lets=q#to_stringinifs<>""then" "^selse"")(lets=v#to_stringinifs<>""then" "^selse"")elseparams_macroletget_name_{t_typedef=_;t_desc=x}=get_namexletto_string_{t_typedef=b;t_desc=x}=lets=to_stringxinsprintf"%s%s"(ifbthen"TYPEDEF:"else"")sletwrapx={t_typedef=false;t_desc=x}letunwrapx=x.t_descletis_typedefx=x.t_typedefletrecget_top_level_type=function|SimpleTysty->[sty]|ArrayTy(ty,_)->get_top_level_typety|PointerTypty->get_top_level_typepty.pt_type|FunctionTyfty->get_top_level_typefty.ft_return_type|AltTyts->List.concat_mapget_top_level_typetsletrec_is_type_type?(name="")=function|SimpleTysty->simple_ty_has_type_type~namesty|AltTyts->List.exists(_is_type_type~name)ts|_->falseletis_type_type?(name="")x=_is_type_type~name(unwrapx)let_is_function_type=function|FunctionTy_->true|_->falseletis_function_typex=_is_function_type(unwrapx)lethoist_typedefx=lettl=get_top_level_typex.t_descinletb=List.exists(funt->t.st_decl_specs#is_typedef)tlinx.t_typedef<-bletencode_simple_ty{st_encoded=e;_}=eletmake_simple_typedstse=SimpleTy{st_decl_specs=ds;st_type_specs=ts;st_encoded=e;}letidents_of_simple_type=function|SimpleTy{st_decl_specs=_;st_type_specs=ts;_}->beginXlist.filter_mapTypeSpec.get_ident_opttsend|_->invalid_arg"Cpp.Pinfo.Type.idents_of_simple_type"letmake_array_typetydims=ArrayTy(ty,dims)letmake_pointer_type?(qualifiers=newqualifiers)opty=PointerTy{pt_op=op;pt_type=ty;pt_qualifiers=qualifiers;}letmake_function_type?(qualifiers=newqualifiers)?(params_macro="")?(is_vararg=false)?(virt_specs=newvirt_specs)ptysrty=FunctionTy{ft_param_types=ptys;ft_qualifiers=qualifiers;ft_return_type=rty;ft_is_vararg=is_vararg;ft_params_macro=params_macro;ft_virt_specs=virt_specs;}letmake_alt_typets=AltTytsletint_t=make_simple_type(newdecl_specs)[TypeSpec.Int]"i"end(* module Type *)[%%capture_pathmoduleName=structletmesfmt=_mes"Name"fmtmoduleNamespace=structtypet=|Normalofident|Inlineofidentletto_string=function|Normali->i|Inlinei->i^"?"letencode=function|Normali->encode_identi|Inlinei->encode_identiletmk?(inline=false)i=ifinlinethenInlineielseNormaliend(* Name.Namespace *)moduleNestedNamespace=structtypet=Namespace.tlistletto_stringnl=String.concat"::"(List.rev_mapNamespace.to_stringnl)letencodenl=String.concat""(List.rev_mapNamespace.encodenl)letmk1n=([n]:t)letappendnnn=n::nnend(* Name.NestedNamespace *)moduleScope=structtypet=|Top|NamespaceofNestedNamespace.t|Template|Classofident|Enumofident|EnumClassofident|Params|Blockofint*stringref(* prefix*)*stringref(* qname *)*boolref(*is_body*)letto_string=function|Top->"Top"|Namespacenn->sprintf"Namespece(%s)"(NestedNamespace.to_stringnn)|Template->"Template"|Classi->sprintf"Class(%s)"i|Enumi->sprintf"Enum(%s)"i|EnumClassi->sprintf"EnumClass(%s)"i|Params->"Params"|Block(ln,p,q,b)->sprintf"Block@%d%s%s%s"ln(if!p=""then""else"["^(!p)^"]")(if!q=""then""else"["^(!q)^"]")(if!bthen"[body]"else"")letget_name=function|Namespacenn->NestedNamespace.to_stringnn|Classi|Enumi|EnumClassi->i|_->raiseNot_foundletis_top=(==)Topletis_namespace=function|Namespace_->true|_->falseletis_template=(==)Templateletis_class=function|Class_->true|_->falseletis_enum=function|Enum_->true|EnumClass_->true|_->falseletis_enumclass=function|EnumClass_->true|_->falseletis_params=(==)Paramsletis_block=function|Block_->true|_->falseletis_body=function|Block(_,_,_,b)->!b|_->falseletis_lambda_body=function|Block(_,_,q,b)->!q=""&&!b|_->falseend(* Name.Scope *)moduleSpec=structtypeaccess_spec=|Aprivate|Aprotected|Apublic|Amacroofidentletaccess_spec_to_string=function|Aprivate->"private"|Aprotected->"protected"|Apublic->"public"|Amacroi->itypeparam_spec=|Pclass|Ptypename|PclassPack|PtypenamePack|Pconceptofstring*identletparam_spec_to_string=function|Pclass->"class"|Ptypename->"typename"|PclassPack->"class..."|PtypenamePack->"typename..."|Pconcept(q,c)->"concept:"^q^cletparam_spec_is_concept=function|Pconcept_->true|_->falseclassbase_spec?(access_spec=None)?(is_virtual=false)?(is_pack_expansion=false)ident=objectvalmutableis_pack_expansion=is_pack_expansionmethodident=identmethodaccess_spec=access_specmethodis_virtual=is_virtualmethodis_pack_expansion=is_pack_expansionmethodset_is_pack_expansion()=is_pack_expansion<-truemethodto_string=leta_str=opt_to_stringaccess_spec_to_stringaccess_specinsprintf"%s%s%s%s"(ifa_str=""then""elsea_str^" ")(ifis_virtualthen"virtual "else"")ident(ifis_pack_expansionthen"..."else"")end(* class base_spec *)classclass_spec?(base_specs=[])?(alt_base_specs_list=[])qn=objectvalvirt_specs=newvirt_specsmethodqn=qnmethodbase_specs:base_speclist=base_specsmethodalt_base_specs_list:base_speclistlist=alt_base_specs_listmethodvirt_specs:virt_specs=virt_specsmethodto_string=letv_str=virt_specs#to_stringinletbs_str=list_to_string(funx->x#to_string)", "base_specsinsprintf"%s%s%s"qn(ifv_str=""then""else" "^v_str)(ifbs_str=""then""else" : "^bs_str)end(* class class_spec *)classenum_base?(macro_name="")type_specs=objectmethodmacro_name=macro_namemethodtype_specs:TypeSpec.tlist=type_specsmethodto_string=ifmacro_name=""thenlist_to_stringTypeSpec.to_string" "type_specselsemacro_nameend(* class enum_base *)classenum_spec?(enum_base=None)ident=objectmethodident:ident=identmethodenum_base:enum_baseoption=enum_basemethodto_string=leteb_str=opt_to_string(funx->x#to_string)enum_baseinsprintf"%s%s"ident(ifeb_str=""then""else" : "^eb_str)end(* class enum_spec *)classmem_specaccess_specty=objectmethodaccess_spec=access_specmethodty=tymethodis_public=matchaccess_specwith|SomeApublic->true|_->falsemethodto_string=leta_str=opt_to_stringaccess_spec_to_stringaccess_specinsprintf"%s%s"(ifa_str=""then""elsea_str^" ")(Type.to_string_ty)end(* class mem_spec *)typekind=|Namespaceof(unit->unit)|UsingDecl|UsingEnumDecl|Templateofkind|TypeParamofparam_spec|ParamofType.t_|Classofclass_spec|Structofclass_spec|Unionofclass_spec|Enumofenum_spec|EnumClassofenum_spec|EnumStructofenum_spec|EnumMacroofident*enum_spec|Type|FunctionofType.t_|FParamofType.t_|Memberofmem_spec|VariableofType.t_|EnumeratorofType.t_|MacroObj|MacroFun|IdMacroFun|Labelletreckind_to_string=function|Namespace_->"Namespace"|UsingDecl->"UsingDecl"|UsingEnumDecl->"UsingEnumDecl"|Templatek->sprintf"Template:%s"(kind_to_stringk)|TypeParams->sprintf"TypeParam:%s"(param_spec_to_strings)|Paramty->sprintf"Param:%s"(Type.to_string_ty)|Classs->"Class:"^s#to_string|Structs->"Struct:"^s#to_string|Unions->"Union:"^s#to_string|Enums->"Enum:"^s#to_string|EnumClasss->"EnumClass:"^s#to_string|EnumStructs->"EnumStruct:"^s#to_string|EnumMacro(i,s)->sprintf"EnumStruct:%s:%s"is#to_string|Type->"Type"|Functionty->sprintf"Function:%s"(Type.to_string_ty)|FParamty->sprintf"FParam:%s"(Type.to_string_ty)|Members->sprintf"Member:%s"s#to_string|Variablety->sprintf"Variable:%s"(Type.to_string_ty)|Enumeratorty->sprintf"Enumerator:%s"(Type.to_string_ty)|MacroObj->"MacroObj"|MacroFun->"MacroFun"|IdMacroFun->"IdMacroFun"|Label->"Label"letrectype_of_kind=function|Templatek->type_of_kindk|Paramty|Functionty|FParamty|Variablety->ty|Enumeratorty->ty|_->raiseNot_foundletmake_templk=Templatekletmake_typarams=TypeParamsletmake_paramty=letty_=Type.wraptyinType.hoist_typedefty_;Paramty_letmake_functionty=letty_=Type.wraptyinType.hoist_typedefty_;Functionty_letmake_fparamty=letty_=Type.wraptyinType.hoist_typedefty_;FParamty_letmake_variablety=letty_=Type.wraptyinType.hoist_typedefty_;Variablety_letmake_enumeratorty=letty_=Type.wraptyinType.hoist_typedefty_;Enumeratorty_letmake_memberaspecty=letty_=Type.wraptyinType.hoist_typedefty_;letmspec=newmem_specaspecty_inMembermspecclassc?(bid_opt=None)?(prefix="")?(is_local=false)?(section_info_opt=None)(lod:Astloc.t)(iod:int)(ident:ident)(_kind:kind)=objectvalmutablekind=_kindvalmutableprefix=prefixmethodget_qualified_name()=prefix^identmethodbid_opt:BID.toption=bid_optmethodsection_info_opt:pp_if_section_infooption=section_info_optmethodprefix=prefixmethodappend_prefixp=prefix<-prefix^pmethodlod=lodmethodiod=iodmethodis_local=is_localmethodis_public=matchkindwith|Membermspec->mspec#is_public|_->falsemethodis_fparam=matchkindwith|FParam_->true|_->falsemethodis_variable=matchkindwith|Variable_->true|_->falsemethodident=identmethodkind=kindmethodtemplatize()=matchkindwith|Class_|Struct_|Union_|Enum_|EnumClass_|EnumStruct_|EnumMacro_|Member_|Function_->kind<-(make_templkind)|VariabletwhenType.is_function_typet->kind<-(make_templkind)|_->()methodto_string=sprintf"%s%s [%s]"(ifprefix=""then""elsesprintf"%s "prefix)(kind_to_stringkind)(Astloc.to_stringlod)methoddef_adder=matchkindwith|Namespacef->f|_->failwith"Cpp.Pinfo.Name.Spec.c#def_adder"methodget_type()=type_of_kindkindend(* class Name.Spec.c *)end(* module Name.Spec *)classstack_frame(_scope:Scope.t)=objectval_tbl=(Hashtbl.create0:(ident,Spec.c)Hashtbl.t)valmutablescope=_scopemethodscope=scopemethodchange_scopescp=scope<-scpmethodblock_scope=matchscopewith|Block_->true|_->falsemethoditer(f:ident->Spec.c->unit)=Hashtbl.iterf_tblmethodregister?(templatize=true)?(replace=false)ispec=[%debug_log"@%s: %s => %s"(Scope.to_stringscope)ispec#to_string];iftemplatize&&Scope.is_templatescopethenspec#templatize();ifreplacethenHashtbl.replace_tblispecelseHashtbl.add_tblispecmethodremove_macroi=Hashtbl.filter_map_inplace(funkv->ifk=ithenbeginmatchv#kindwith|Spec.MacroObj|MacroFun|IdMacroFun->None|_->SomevendelseSomev)_tblmethodfind_all?(filt=fun_->true)n=List.filterfilt(Hashtbl.find_all_tbln)methodfind?(filt=fun_->true)n=matchList.filterfilt(Hashtbl.find_all_tbln)with|[]->raiseNot_found|x::_->xmethodto_string=letbuf=Buffer.create0inBuffer.add_stringbuf(sprintf"@%s\n"(Scope.to_stringscope));Hashtbl.iter(funns->Buffer.add_stringbuf(sprintf"%s -> %s\n"ns#to_string);)_tbl;Buffer.contentsbufend(* class Name.stack_frame *)exceptionScope_foundexceptionFoundofSpec.cclassstack=object(self)val_stack=(Stack.create():stack_frameStack.t)valmutablepopped_frame=newstack_frameScope.Topmethodcopy={<_stack=Stack.copy_stack;>}methodget_prefix?(prefix="")?(ns_only=false)?(class_only=false)()=letq=ref""inStack.iter(funfrm->match(frm#scope:Scope.t)with|Namespacennwhennotclass_only->beginq:=(NestedNamespace.encodenn)^(!q);ifprefix=!qthenq:=""end|Classi|Enumiwhennotns_only->beginq:=i^(!q);ifprefix=!qthenq:=""end|Block(_,p,_,_)whennotclass_only&¬ns_only&&!p!=""->beginq:=(!p)^(!q);ifprefix=!qthenq:=""end|_->())_stack;[%debug_log"qualifier=%s"!q];!qmethodtop=Stack.top_stackmethodpeek_nthnth=matchnthwith|1->self#top|nwhenn<0->invalid_arg"Pinfo.Name.stack#peek_nth"|_->letcount=ref0inletres=refNoneinbegintryStack.iter(funx->if!count=nth-1thenbeginres:=Somex;raiseExitend;incrcount)_stackwithExit->()end;match!reswith|Somex->x|_->raiseStack.Emptymethodpushfrm=Stack.pushfrm_stackmethodreset_popped_frame()=popped_frame<-newstack_frameScope.Topmethodreset()=Stack.clear_stack;self#reset_popped_frame()methodafter_params=popped_frame#scope==Scope.Paramsmethodset_popped_frame_scope_from_params_to_top()=ifpopped_frame#scope==Scope.Paramsthenpopped_frame#change_scopeTopmethodenter_scopescope=[%debug_log"entering %s scope"(Scope.to_stringscope)];Stack.push(newstack_framescope)_stackmethodenter_namespacenn=self#enter_scope(Scope.Namespacenn)methodenter_template()=self#enter_scopeScope.Templatemethodenter_classi=self#enter_scope(Scope.Classi)methodenter_enumi=self#enter_scope(Scope.Enumi)methodenter_enumclassi=self#enter_scope(Scope.EnumClassi)methodenter_params()=self#enter_scopeScope.Paramsmethodenter_block?(prefix="")?(qname="")?(no_tweak=false)ln=ifpopped_frame#scope==Scope.Params&¬no_tweakthenbegin[%debug_log"changing last poped frame to Block"];popped_frame#change_scope(Scope.Block(ln,refprefix,refqname,reffalse));[%debug_log"entering %s scope"(Scope.to_stringpopped_frame#scope)];Stack.pushpopped_frame_stack;[%debug_log"%s"self#top#to_string];popped_frame<-newstack_frameScope.Topendelseself#enter_scope(Scope.Block(ln,ref"",ref"",reffalse))methodset_body_flag()=matchself#top#scopewith|Scope.Block(_,_,_,b)->b:=true|_->()methodreset_body_name()=matchself#top#scopewith|Scope.Block(_,p,q,_)->p:="";q:=""|_->()method_exit_scopechk_scp=letfrm=Stack.pop_stackin[%debug_log"%s scope poped"(Scope.to_stringfrm#scope)];assert(chk_scpfrm#scope);[%debug_log"%s scope exited"(Scope.to_stringfrm#scope)];popped_frame<-frmmethodexit_namespace()=[%debug_log"exiting Namespace scope..."];self#_exit_scopeScope.is_namespace;popped_framemethodexit_template()=[%debug_log"exiting Template scope..."];self#_exit_scopeScope.is_template;popped_frame#iter(funi(spec:Spec.c)->[%debug_log"%s %s"ispec#to_string];matchspec#kindwith|Template_->self#top#register~templatize:falseispec|Class_|Struct_|Union_|Enum_|EnumClass_|EnumStruct_|EnumMacro_(*| Member _ *)|Function_->beginspec#templatize();self#top#register~templatize:falseispecend|Member_->beginself#top#register~templatize:falseispecend|VariabletwhenType.is_function_typet->beginspec#templatize();self#top#register~templatize:falseispecend|_->())methodexit_class()=[%debug_log"exiting Class scope..."];self#_exit_scopeScope.is_class;popped_frame#iter(funi(spec:Spec.c)->ifspec#is_publicthenbegin[%debug_log"%s is a public member"i];self#top#register~templatize:falseispecend)methodexit_enum()=[%debug_log"exiting Enum scope..."];self#_exit_scopeScope.is_enum;letscope=popped_frame#scopeinletenumclass_flag=Scope.is_enumclassscopeinifenumclass_flagthenbeginletp=Scope.get_namescopeinpopped_frame#iter(funi(spec:Spec.c)->[%debug_log"%s %s"ispec#to_string];spec#append_prefixp;[%debug_log" -> %s"spec#to_string];self#top#register~templatize:falseispec)endelsepopped_frame#iter(funi(spec:Spec.c)->self#top#register~templatize:falseispec)(*method exit_function () =
[%debug_log "exiting Function scope..."];
self#_exit_scope ((=) Scope.Function)*)(*method exit_function_if_any () =
[%debug_log "exiting Function scope (if any)..."];
let frm = Stack.top _stack in
[%debug_log "stack top is %s scope" (Scope.to_string frm#scope)];
if frm#scope = Scope.Function then begin
let _ = Stack.pop _stack in
[%debug_log "%s scope exited" (Scope.to_string frm#scope)]
end*)methodexit_params()=[%debug_log"exiting Params scope..."];self#_exit_scopeScope.is_paramsmethodexit_block()=[%debug_log"exiting Block scope..."];self#_exit_scopeScope.is_blockmethodin_scopechk_scope=tryStack.iter(funfrm->ifchk_scopefrm#scopethenraiseScope_found)_stack;falsewithScope_found->truemethodat_scopechk_scope=trychk_scopeself#top#scopewith_->falsemethodin_template=self#in_scopeScope.is_templatemethodin_class=self#in_scopeScope.is_classmethodin_block=self#in_scopeScope.is_blockmethodin_body=self#in_scopeScope.is_bodymethodin_lambda_body=self#in_scopeScope.is_lambda_bodymethodin_params=self#in_scopeScope.is_paramsmethodat_top=self#at_scopeScope.is_topmethodat_namespace=self#at_scopeScope.is_namespacemethodat_template=self#at_scopeScope.is_templatemethodat_class=self#at_scopeScope.is_classmethodat_enum=self#at_scopeScope.is_enummethodat_enumclass=self#at_scopeScope.is_enumclassmethodat_block=self#at_scopeScope.is_blockmethodat_body=self#at_scopeScope.is_bodymethodat_lambda_body=self#at_scopeScope.is_lambda_bodymethodat_params=self#at_scopeScope.is_paramsmethodblock_level=letcount=ref0inStack.iter(funfrm->matchfrm#scopewith|Scope.Block_->incrcount|_->())_stack;!countmethodfind_name?(filt=fun_->true)n=[%debug_log"%s"n];tryStack.iter(funfrm->(*[%debug_log "%s" frm#to_string];*)tryraise(Found(frm#find?filt:(Somefilt)n))withNot_found->())_stack;ifpopped_frame#scope==Scope.Paramsthenbeginpopped_frame#find~filtnendelseraiseNot_foundwithFoundx->[%debug_log"found: %s"x#to_string];xmethodfind_namespacen=letfiltx=matchx#kindwith|Spec.Namespace_->true|_->falseinself#find_name~filtnmethodfind_objn=letfiltx=matchx#kindwith|Spec.Variable_|FParam_|Param_|Function_->true|_->falseinself#find_name~filtnmethodfind_typen=letfiltx=matchx#kindwith|Spec.TypeParam_|Class_|Struct_|Union_|Enum_|EnumClass_|EnumStruct_|EnumMacro_|Type->true|_->falseinself#find_name~filtnmethodfind_templn=letfiltx=matchx#kindwith|Spec.Template_->true|_->falseinself#find_name~filtnmethodto_string=letbuf=Buffer.create0inStack.iter(funfrm->Buffer.add_stringbuffrm#to_string;Buffer.add_stringbuf"\n";)_stack;Buffer.contentsbufmethodscopes_to_string=letbuf=Buffer.create0inStack.iter(funfrm->Buffer.add_stringbuf(Scope.to_stringfrm#scope);Buffer.add_stringbuf"\n";)_stack;Buffer.contentsbufmethodmake_def_adder(frm:stack_frame)=letdef_adder()=frm#iter(funispec->[%debug_log"%s -> %s"ispec#to_string];self#top#register~templatize:falseispec)indef_adderend(* class Name.stack *)end(* module Name *)]typet=|NoInfo|NameSpecofName.Spec.c|Externalofnameletfrom_specspec=NameSpecspecletfrom_namename=Externalnameletget_external=function|Externaln->n|_->raiseNot_foundletget_spec=function|NameSpecs->s|_->raiseNot_foundletmergeij=matchiwith|NoInfo->j|_->failwith"Cpp.Pinfo.merge"letto_string=function|NoInfo->""|NameSpecspec->"NameSpec:"^spec#to_string|Externaln->"External:"^n