123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211(*
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.AstlocmoduleLayeredloc=Langs_common.LayeredlocmoduleAst_base=Langs_common.Ast_basemoduleLoc=AstlocmoduleLLoc=LayeredlocmoduleI=PinfomoduleB=BindingmoduleBID=Binding.IDmoduleL=LabelmoduleN=Pinfo.NamemoduleNS=Pinfo.Name.NamespacemoduleNestedNS=Pinfo.Name.NestedNamespacemoduleType=Pinfo.TypeopenCommonletmk_macro_idi="`"^iletmk_macro_call_id?(args="")i=sprintf"`%s(%s)"iargsletargs_to_simple_stringnl=String.concat","(List.map(funx->L.to_simple_stringx#label)nl)letdummy_node_id=-1[%%capture_pathclassnode?(lloc=LLoc.dummy)?(children=[])?(info=I.NoInfo)?(pvec=[])lab=object(self:'self)valmutableencoded=""valmutableparent=Nonevalmutablelloc=llocvalmutablelabel=(lab:L.t)valmutablechildren=(children:'selflist)valmutableinfo=infovalmutablebinding=B.NoBindingvalmutablepvec=pvecvalmutableid_opt=Nonemethodid=matchid_optwith|None->beginleti=Oo.idselfinid_opt<-Somei;iend|Somei->ivalmutableprefix=""methodadd_prefixs=prefix<-s^prefixmethodget_prefix=prefixvalmutablesuffix=""methodadd_suffixs=suffix<-suffix^smethodget_suffix=suffixmethodset_parentp=parent<-Somepmethodparent:'self=matchparentwith|Somep->p|_->raiseNot_foundmethoditer_parents?(upto=None)(f:'self->unit)=tryletp=self#parentinletmoveon=matchuptowith|Somex->p!=x|_->trueinifmoveonthenbeginfp;p#iter_parents~uptofendwithNot_found->()methodencoded=encodedmethodset_encodede=[%debug_log"%s"e];encoded<-emethodpvec:intlist=matchchildren,pvecwith|[],[]->[]|_,[]->[self#nchildren]|_->pvecmethodset_pvecpv=pvec<-pvmethodnth_childn=[%debug_log"n=%d"n];trymatchnwith|0->List.hdchildren|_->List.nthchildrennwith_->failwith(sprintf"Cpp.Ast.node#nth_child: %s"(L.to_stringlabel))methodnth_childrenn=[%debug_log"n=%d"n];letpv=self#pvecinmatchpvwith|[]->failwith(sprintf"Cpp.Ast.node#nth_children: %s"(L.to_stringlabel))|[_]whenn=0->children|[_]->failwith(sprintf"Cpp.Ast.node#nth_children: %s"(L.to_stringlabel))|_whenn>=0->beginletlen=tryList.nthpvnwith_->failwith(sprintf"Cpp.Ast.node#nth_children: %s"(L.to_stringlabel))iniflen=0then[]elsetrylet_,f,t=List.fold_left(fun(i,f,t)x->[%debug_log" (%d,%d,%d) %d"iftx];ifi<nthen(i+1,f+x,f+x)elseifi=nthenifx=0thenraiseNot_foundelse(i+1,f,f+x-1)else(i,f,t))(0,0,0)pvin[%debug_log"from=%d, to=%d"ft];letl=ref[]intryfori=ftotdol:=(List.nthchildreni)::!ldone;!lwith_->invalid_arg(sprintf"Cpp.Ast.node#nth_children: %s"(L.to_stringlabel))withNot_found->[]end|_->invalid_arg(sprintf"Cpp.Ast.node#nth_children: %s"(L.to_stringlabel))methodbinding=bindingmethodset_bindingb=binding<-bmethodlabel=labelmethodlloc=llocmethodloc=lloc#get_locmethodorig_loc=lloc#get_orig_locmethodchildren=childrenmethodinfo=infomethodset_infoi=info<-imethodadd_infoi=matchinfowith|I.NoInfo->info<-i|_->info<-I.mergeinfoimethodchildren_labels=List.map(funn->n,n#label)childrenmethodnchildren=List.lengthchildrenmethodset_llocl=lloc<-lmethodset_childrenc=children<-cmethodrelablab=label<-labmethodadd_children_lc=children<-c@childrenmethodadd_children_rc=children<-children@cmethodremove_rightmost_child=matchchildrenwith|[]->()|[_]->children<-[]|_->match(List.revchildren)with|_::t->children<-(List.revt)|_->assertfalsemethodremove_leftmost_child=matchchildrenwith|[]->()|[_]->children<-[]|_::t->children<-tmethodto_string=sprintf"<%s>%s%s%s"(L.to_stringlabel)(lloc#to_string?short:(Sometrue)())(matchinfowith|I.NoInfo->""|_->sprintf": <<%s>>"(I.to_stringinfo))(matchbindingwith|B.NoBinding->""|_->sprintf": %a"BID.ps(B.get_bidbinding))methodto_tag=L.to_taglabelmethodget_name=L.get_namelabelmethodhas_name=trylet_=self#get_nameintruewith_->falsemethodget_name_opt=L.get_name_optlabelmethodget_names=L.get_nameslabelmethodget_label=L.get_labellabelmethodget_var=L.get_varlabelmethodget_var_opt=L.get_var_optlabelend(* of class Ast.node *)]letnode_opt_to_name_opt=function|Somend->Somend#get_name|None->Noneletnode_list_to_name_listnds=Xlist.filter_map(funn->n#get_name_opt)ndsletlloc_of_locsloc0loc1=letlloc0=LLoc.of_locloc0inletlloc1=LLoc.of_locloc1inLLoc.mergelloc0lloc1letlloc_of_lexposspos0pos1=letloc0=Loc.of_lexpospos0inletloc1=Loc.of_lexpospos1inlloc_of_locsloc0loc1[%%capture_pathletmknode(*env*)_start_posend_pos?(info=I.NoInfo)?(pvec=[])labelchildren=[%debug_log"%s (nchildren=%d)"(L.to_stringlabel)(List.lengthchildren)];letlloc=lloc_of_lexpossstart_posend_posinletnd=newnode~lloc~children~info~pveclabelinList.iter(funx->x#set_parentnd)children;nd]letmkleafenvstart_posend_pos?(info=I.NoInfo)?(pvec=[])label=mknodeenvstart_posend_pos~info~pveclabel[][%%capture_pathletreloc(*env*)_start_posend_posnode=letlloc=lloc_of_lexpossstart_posend_posin[%debug_log"relocating %s: %s -> %s"(L.to_stringnode#label)(node#lloc#to_string?short:(Sometrue)())(lloc#to_string?short:(Sometrue)())];node#set_lloclloc][%%capture_pathletreloc_end(*env*)_end_posnode=letlloc=lloc_of_lexpossend_posend_posin[%debug_log"relocating %s: %s -> %s"(L.to_stringnode#label)(node#lloc#to_string?short:(Sometrue)())(lloc#to_string?short:(Sometrue)())];node#set_lloc(LLoc.mergenode#lloclloc)](* *)exceptionNode_foundofnodeexceptionType_not_found[%%capture_pathletfind_nodepred(nd:node)=letrec_find1pred(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];ifprednd#labelthenraise(Node_foundnd)elseList.iter(_find1pred)nd#childrenintry_find1prednd;raiseNot_foundwithNode_foundx->x]letnode_existspred(nd:node)=trylet_=find_nodepredndintruewithNot_found->false[%%capture_pathletfind_nodespred(nd:node)=letrec_findpred(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];letnl=List.concat_map(_findpred)nd#childreninifprednd#labelthennd::nlelsenlin_findprednd]letint_pat=Str.regexp"^\\(-?\\)\\(0[bB][01']+\\|0[0-7']+\\|[0-9']+\\|0[xX][0-9a-fA-F']+\\)\\([uUlL]*\\)$"letchar_pat=Str.regexp"^\\(u8\\|[uUL@]\\)?'.+'$"letstr_pat=Str.regexp"^\\(u8\\|[uUL@]\\)?\".+\"$"letget_char_ty_vpats=ifStr.string_matchpats0thenbeginletst=ref1inletlen=ref((String.lengths)-2)inletaffix=tryStr.matched_group1swith_->""inlett=matchaffixwith|""->"c"|"@"->"c"|"u"->incrst;decrlen;"Ds"|"U"->incrst;decrlen;"Di"|"L"->incrst;decrlen;"w"|"u8"->st:=!st+2;len:=!len-2;"Du"|_->[%warn_log"unknown affix: %s"affix];"c"int,String.subs!st!lenendelsebegin[%warn_log"unknown char pattern: %s"s];assertfalseendletencode_ident=I.encode_identletdecode_ident=I.decode_ident[%%capture_pathletprefix_of_encodeds=[%debug_log"s=%s"s];ifs=""thenselseletlen=String.lengthsinletlast_pos=ref0inletpos=ref0inletlen_s=ref""inletskipped=reftrueinbegintrywhiletruedoletc=s.[!pos]inletci=Char.codecinif48<=ci&&ci<=57thenbeginskipped:=false;len_s:=!len_s^(Char.escapedc);incrposendelseif!skippedthenbegin[%debug_log"skipped: %c"c];incrpos;if!pos>=lenthenraiseExitendelsebeginleti=int_of_string!len_sin[%debug_log"i=%d"i];len_s:="";pos:=!pos+i;if!pos>=lenthenraiseExitelsebegin[%debug_log"last_pos: %d -> %d"!last_pos!pos];last_pos:=!pos;skipped:=trueendenddonewithExit->()end;if!last_pos=0thenbegin[%debug_log"s'=\"\""];""endelsebeginlets'=String.subs0!last_posin[%debug_log"s'=%s"s'];s'end][%%capture_pathletrecencode_name=letpat=Str.regexp"::"inletfname=letil=Str.splitpatnameinString.concat""(List.mapencode_identil)infandencode_pointer_op:Type.pointer_op->string=function|Star("",_)->"P"|Star(n,_)->sprintf"M%s"n|Amp->"R"|AmpAmp->"O"|Hat->"U13block_pointer"|Macroi->"U"^(encode_identi)andencode_type_t=encode_type(Type.unwrapt)andencode_type:Type.t->string=function|SimpleTysty->Type.encode_simple_tysty|ArrayTy(ty,dims)->sprintf"A%d_%s"dims(encode_typety)|PointerTypty->encode_pointer_typty|FunctionTyfty->encode_function_tyfty|AltTyts->sprintf"(%s)"(list_to_stringencode_type" | "ts)andencode_pointer_ty{Type.pt_op=op;Type.pt_type=ty;Type.pt_qualifiers=q;}=letq_str=q#to_stringinleto_str=encode_pointer_opopinsprintf"%s%s%s"o_strq_str(encode_typety)andencode_function_ty{Type.ft_param_types=ptys;Type.ft_qualifiers=q;Type.ft_return_type=rty;Type.ft_is_vararg=_;Type.ft_params_macro=params_macro;Type.ft_virt_specs=_;}=ifparams_macro=""thensprintf"%s%sF%s%s%sE"q#encode_cvq#encode_exc(encode_typerty)(String.concat""(List.mapencode_typeptys))q#encode_refelseparams_macroandencode_expr(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];letencode_childrencode=sprintf"%s%s"code(String.concat""(List.mapencode_exprnd#children))inmatchnd#labelwith|IntegerLiterali->beginifStr.string_matchint_pati0thenbeginletst=ref0inletlen=ref(String.lengthi)inletprefix=if(Str.matched_group1i)="-"thenbeginst:=1;decrlen;"n"endelse""inletsuffix=String.lowercase_ascii(Str.matched_group3i)inlett=matchsuffixwith|""->"i"|"l"->decrlen;"l"|"ll"->len:=!len-2;"x"|"u"->decrlen;"j"|"ul"|"lu"->len:=!len-2;"m"|"ull"|"llu"->len:=!len-3;"y"|_->[%warn_log"unknown suffix: %s (%s)"suffixi];"i"inletnum=String.subi!st!leninsprintf"L%s%s%s"tprefixnumendelseassertfalseend|FloatingLiteralf->beginletlen=String.lengthfinlett=matchf.[len-1]with|'f'|'F'->"f"|'l'|'L'->"e"|_->"d"inletrep="<not yet>"insprintf"L%s%sE"trepend|CharacterLiteralc->beginlett,v=get_char_ty_vchar_patcinsprintf"L%s%dE"t(Char.codev.[0])end|ConcatenatedString->String.concat""(List.mapencode_string_literalnd#children)|BooleanLiteralb->beginlete=matchbwith|"true"->"1"|"false"->"0"|_->assertfalseinsprintf"Lb%sE"eend|Nullptr->"LDnE"|This->"!!!This"|ParenthesizedExpression->encode_expr(nd#nth_child0)|Identifieri->encode_identi|PpConcatenatedIdentifier->String.concat"##"(List.mapencode_exprnd#children)|SimpleTemplateIdi->(encode_identi)^(encode_template_argumentsnd#children)|TypeId->encode_type_idnd|OperatorFunctionId->sprintf"on%s"(encode_op(nd#nth_child0))|TemplateIdOp->(encode_expr(nd#nth_child0))^(encode_template_arguments(nd#nth_children1))|LiteralOperatorIdi->sprintf"li%s"(encode_identi)|TemplateIdLit->(encode_expr(nd#nth_child0))^(encode_template_arguments(nd#nth_children1))|ConversionFunctionId->"cv"^(encode_type_id(nd#nth_child0))|Destructor->beginletn0=nd#nth_child0inlett=matchn0#labelwith|DecltypeSpecifier->(encode_decltypen0)|_->(encode_type_namen0)in"dn"^tend|QualifiedId->(encode_nested_name_spec(nd#nth_child0))^(encode_expr(nd#nth_child1))|UnaryExpressionIncr->"pp"^(encode_expr(nd#nth_child0))|UnaryExpressionDecr->"mm"^(encode_expr(nd#nth_child0))|UnaryExpressionInd->"de"^(encode_expr(nd#nth_child0))|UnaryExpressionAddr->"ad"^(encode_expr(nd#nth_child0))|UnaryExpressionLabelAddr->"la"^(encode_expr(nd#nth_child0))|UnaryExpressionPlus->"ps"^(encode_expr(nd#nth_child0))|UnaryExpressionMinus->"ng"^(encode_expr(nd#nth_child0))|UnaryExpressionNeg_->"nt"^(encode_expr(nd#nth_child0))|UnaryExpressionCompl_->"co"^(encode_expr(nd#nth_child0))|UnaryExpressionSizeof->beginmatchnd#childrenwith|[n0]->beginmatchn0#labelwith|TypeId->sprintf"st%s"(encode_type_idn0)|_->sprintf"sz%s"(encode_exprn0)end|_->assertfalseend|UnaryExpressionSizeofPack_->"!!!UnaryExpressionSizeofPack"|UnaryExpressionAlignof->""^(encode_expr(nd#nth_child0))|NoexceptExpression->"nx"^(encode_expr(nd#nth_child0))|NewExpression->beginletprefix=ifnode_exists(functionL.NoptrNewDeclarator->true|_->false)ndthen"na"else"nw"inletgs=matchnd#nth_children0with|[_]->"gs"|_->""inlete=matchnd#nth_children1with|[]->""|[n0]->String.concat""(List.mapencode_exprn0#children)|_->assertfalseinlett=matchnd#nth_children2with|[n0]->beginmatchn0#labelwith|TypeId->encode_type_idn0|_->encode_type_idn0end|_->assertfalseinletini=matchnd#nth_children3with|[]->"E"|[n0]->beginmatchn0#labelwith|NewInitializer->sprintf"pi%sE"(String.concat""(List.mapencode_exprn0#children))|_->"E"(* ??? *)end|_->assertfalseinsprintf"%s%s%s_%s%s"gsprefixetiniend|DeleteExpression->beginletgs=matchnd#nth_children0with|[_]->"gs"|_->""inlete=matchnd#nth_children1with|[n0]->encode_exprn0|_->assertfalseinsprintf"%sdl%s"gseend|DeleteExpressionBracket->beginletgs=matchnd#nth_children0with|[_]->"gs"|_->""inlete=matchnd#nth_children1with|[n0]->encode_exprn0|_->assertfalseinsprintf"%sda%s"gseend|PostfixExpressionSubscr->encode_children"ix"|PostfixExpressionFunCall->(encode_children"cl")^"E"|PostfixExpressionExplicitTypeConvExpr->beginlete0=encode_type(simple_type_of_decl_spec_seq(nd#nth_children0))inmatchnd#childrenwith|[_]->sprintf"cv%s_E"e0|[_;n1]->sprintf"cv%s%s"e0(encode_exprn1)|_::rest->sprintf"cv%s_%sE"e0(String.concat""(List.mapencode_exprrest))|[]->assertfalseend|PostfixExpressionExplicitTypeConvBraced->beginlete0=encode_type(simple_type_of_decl_spec_seq(nd#nth_children0))insprintf"tl%s%s"e0(String.concat""(List.mapencode_expr(nd#nth_child1)#children))end|PostfixExpressionDot->beginletnm=matchnd#nth_children2with|[n2]->encode_exprn2|_->assertfalsein"dt"^(encode_expr(nd#nth_child0))^nmend|PostfixExpressionArrow->beginletnm=matchnd#nth_children2with|[n2]->encode_exprn2|_->assertfalsein"pt"^(encode_expr(nd#nth_child0))^nmend|PostfixExpressionIncr->"pp_"^(encode_expr(nd#nth_child0))|PostfixExpressionDecr->"mm_"^(encode_expr(nd#nth_child0))|PostfixExpressionDynamic_cast->beginlett=encode_type_id(nd#nth_child0)insprintf"dc%s%s"t(encode_expr(nd#nth_child1))end|PostfixExpressionStatic_cast->beginlett=encode_type_id(nd#nth_child0)insprintf"sc%s%s"t(encode_expr(nd#nth_child1))end|PostfixExpressionReinterpret_cast->beginlett=encode_type_id(nd#nth_child0)insprintf"rc%s%s"t(encode_expr(nd#nth_child1))end|PostfixExpressionConst_cast->beginlett=encode_type_id(nd#nth_child0)insprintf"cc%s%s"t(encode_expr(nd#nth_child1))end|PostfixExpressionTypeidExpr->sprintf"te%s"(encode_expr(nd#nth_child0))|PostfixExpressionTypeidTy->sprintf"ti%s"(encode_type_id(nd#nth_child0))|BracedInitList->sprintf"il%sE"(String.concat""(List.mapencode_exprnd#children))|DesignatedInitializerClause->beginleti=match(nd#nth_child0)#labelwith|DesignatorFieldi->i|_->assertfalseinsprintf"di%s%s"(encode_identi)(encode_expr(nd#nth_child1))end|TypenameSpecifieri->beginmatchnd#nth_children0,nd#nth_children1with|[],[]->encode_identi|[],[s]->encode_exprs|[n],[]->sprintf"N%s%sE"(encode_nested_name_specn)(encode_identi)|[n],[s]->sprintf"N%s%sE"(encode_nested_name_specn)(encode_exprs)|_->assertfalseend|CastExpression->encode_children"cv"|PmExpressionClass->encode_children"ds"|PmExpressionPtr->encode_children"pm"|MultiplicativeExpressionMult->encode_children"ml"|MultiplicativeExpressionDiv->encode_children"dv"|MultiplicativeExpressionMod->encode_children"rm"|AdditiveExpressionAdd->encode_children"pl"|AdditiveExpressionSubt->encode_children"mi"|ShiftExpressionLeft->encode_children"ls"|ShiftExpressionRight->encode_children"rs"|CompareExpression->encode_children"ss"|RelationalExpressionLt->encode_children"lt"|RelationalExpressionGt->encode_children"gt"|RelationalExpressionLe->encode_children"le"|RelationalExpressionGe->encode_children"ge"|EqualityExpressionEq->encode_children"eq"|EqualityExpressionNeq_->encode_children"ne"|AndExpression_->encode_children"an"|ExclusiveOrExpression_->encode_children"eo"|InclusiveOrExpression_->encode_children"or"|LogicalAndExpression_->encode_children"aa"|LogicalOrExpression_->encode_children"oo"|ConditionalExpression->encode_children"qu"|AssignmentExpressionEq->encode_children"aS"|AssignmentExpressionPlus->encode_children"pL"|AssignmentExpressionMinus->encode_children"mI"|AssignmentExpressionMult->encode_children"mL"|AssignmentExpressionDiv->encode_children"dV"|AssignmentExpressionMod->encode_children"rM"|AssignmentExpressionShiftLeft->encode_children"lS"|AssignmentExpressionShiftRight->encode_children"rS"|AssignmentExpressionAnd_->encode_children"aN"|AssignmentExpressionXor_->encode_children"eO"|AssignmentExpressionOr_->encode_children"oR"|x->sprintf"!!!%s"(L.to_stringx)andencode_type_id(nd:node)=encode_type(type_of_type_idnd)andencode_nested_name_spec(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|NestedNameSpecifierHead->"gs"|NestedNameSpecifierIdentuqn->beginmatchnd#nth_children0with|[]->uqn|[n0]->(encode_nested_name_specn0)^uqn|_->assertfalseend|NestedNameSpecifierTempluqn->beginmatchnd#nth_children0with|[]->uqn|[n0]->(encode_nested_name_specn0)^uqn|_->assertfalseend|NestedNameSpecifierDeclty->encode_decltype(nd#nth_child0)|_->invalid_arg"Cpp.Ast.encode_nested_name_spec"andencode_decltype(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|DecltypeSpecifier->beginletn0=nd#nth_child0inletprefix=matchn0#labelwith|Identifier_|OperatorFunctionId|ConversionFunctionId|LiteralOperatorId_|Destructor|SimpleTemplateId_|TemplateIdOp|TemplateIdLit|IdentifierMacroInvocation_|PostfixExpressionDot|PostfixExpressionArrow->"Dt"|_->"DT"insprintf"%s%sE"prefix(encode_exprn0)end|_->invalid_arg"Cpp.Ast.encode_decltype"andencode_type_name(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TypeNamei->encode_identi|_->encode_exprndandencode_op?(unary=false)(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|Pluswhenunary->"ps"|Minuswhenunary->"ng"|Amp_whenunary->"ad"|Starwhenunary->"de"(* | PlusPlus when unary -> "pp"
| MinusMinus when unary -> "mm"*)|New->"nw"|Delete->"dl"|NewBracket->"na"|DeleteBracket->"da"|Parentheses->"cl"|Brackets->"ix"|MinusGt->"pt"|MinusGtStar->"pm"|Tilde_->"co"|Exclam_->"nt"|Plus->"pl"|Minus->"mi"|Star->"ml"|Slash->"dv"|Perc->"rm"|Hat_->"eo"|Amp_->"an"|Bar_->"or"|Eq->"aS"|PlusEq->"pL"|MinusEq->"mI"|StarEq->"mL"|SlashEq->"dV"|PercEq->"rM"|HatEq_->"eO"|AmpEq_->"aN"|BarEq_->"oR"|EqEq->"eq"|ExclamEq_->"ne"|Lt->"lt"|Gt->"gt"|LtEq->"le"|GtEq->"ge"|LtEqGt->"ss"|AmpAmp_->"aa"|BarBar_->"oo"|LtLt->"ls"|GtGt->"rs"|LtLtEq->"lS"|GtGtEq->"rS"|PlusPlus->"pp"|MinusMinus->"mm"|Comma->"cm"|OperatorMacroi->"v"^(encode_identi)|_->invalid_arg"Cpp.Ast.encode_op"andencode_template_arguments(nds:nodelist)=letel=List.map(funx->matchx#labelwith|L.PackExpansion->sprintf"J%sE"(String.concat""(List.mapencode_exprnds))|_->beginlete=encode_exprxinife.[0]='L'theneelsesprintf"X%sE"eend)ndsinsprintf"I%sE"(String.concat""el)andencode_string_literal(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|StringLiterals->beginletty,_=get_char_ty_vstr_patsinsprintf"LA%d_K%sE"(String.lengths)tyend|StringMacro_|PpStringized_->""|_->invalid_arg"Cpp.Ast.encode_string_literal"anduqn_of_ident_macro_invocation(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|IdentifierMacroInvocationi->letargs=args_to_simple_stringnd#childreninmk_macro_call_id~argsi|_->invalid_arg"Cpp.Ast.uqn_of_ident_macro_invocation"anduqn_of_simple_template_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|SimpleTemplateIdi->(encode_identi)^(encode_template_argumentsnd#children)|SimpleTemplateIdM->(uqn_of_ident_macro_invocation(nd#nth_child0))^(encode_template_arguments(nd#nth_children1))|IdentifierMacroInvocation_->uqn_of_ident_macro_invocationnd|_->invalid_arg"Cpp.Ast.uqn_of_simple_template_id"andqn_of_elaborated_type_specifier(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ElaboratedTypeSpecifierClassuqn|ElaboratedTypeSpecifierStructuqn|ElaboratedTypeSpecifierUnionuqn->beginmatchnd#nth_children1with|[]->uqn|[n]->(encode_nested_name_specn)^uqn|_->assertfalseend|ElaboratedTypeSpecifierEnumuqn->beginmatchnd#childrenwith|[]->uqn|[n]->(encode_nested_name_specn)^uqn|_->assertfalseend|_->invalid_arg"Cpp.Ast.qn_of_elaborated_type_specifier"andqn_of_simple_type_specifier(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|SimpleTypeSpecifieruqn->beginmatchnd#nth_children0with|[]->uqn|[n]->(encode_nested_name_specn)^uqn|_->assertfalseend|DecltypeSpecifier->encode_decltypend|PlaceholderTypeSpecifierAuto|PlaceholderTypeSpecifierDecltype|Char|Char8_t|Char16_t|Char32_t|Wchar_t|Bool|Short|Int|Long|Signed|Unsigned|Float|Double|Void->raiseNot_found|_->invalid_arg"Cpp.Ast.qn_of_simple_type_specifier"andqn_of_typename_specifier(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TypenameSpecifieruqn->beginmatchnd#nth_children0with|[]->uqn|[n]->(encode_nested_name_specn)^uqn|_->[%debug_log"@"];assertfalseend|_->invalid_arg"Cpp.Ast.qn_of_typename_specifier"anduqn_of_type_name(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TypeNamei->encode_identi|_->uqn_of_simple_template_idndanduqn_of_unqualified_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|Identifieri->encode_identi|SimpleTemplateId_|SimpleTemplateIdM->uqn_of_simple_template_idnd|Destructor->beginletc=nd#nth_child0inmatchc#labelwith|TypeName_|SimpleTemplateId_->"D"^(uqn_of_type_namec)(* ! *)|DecltypeSpecifier->"D"^(encode_decltypec)(* ! *)|IdentifierMacroInvocation_->"D"^(uqn_of_ident_macro_invocationc)(* ! *)|lab->let_=labin[%debug_log"%s"(L.to_stringlab)];assertfalseend|IdentifierMacroInvocation_->uqn_of_ident_macro_invocationnd|OperatorFunctionId|ConversionFunctionId|LiteralOperatorId_|TemplateIdOp|TemplateIdLit|PpConcatenatedIdentifier->encode_exprnd|_->invalid_arg"Cpp.Ast.uqn_of_unqualified_id"anduqn_of_conversion_function_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ConversionFunctionId->encode_exprnd|_->invalid_arg"Cpp.Ast.uqn_of_conversion_function_id"anduqn_of_operator_function_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|OperatorFunctionId->encode_exprnd|_->invalid_arg"Cpp.Ast.uqn_of_operator_function_id"anduqn_of_literal_operator_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|LiteralOperatorId_->encode_exprnd|_->invalid_arg"Cpp.Ast.uqn_of_literal_operator_id"anduqn_of_template_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TemplateIdOp|TemplateIdLit->encode_expr(nd#nth_child0)|_->uqn_of_simple_template_idndandtype_spec_list_of_node_list?(ns="")(nds:nodelist)=matchndswith|[nd]whenbeginmatchnd#labelwith|TypeSpecifierSeq->true|_->falseend->List.map(type_spec_of_node~ns)(nd#nth_children0)|_->List.map(type_spec_of_node~ns)ndsandqn_of_class_or_decltype(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|QualifiedTypeName->beginletprefix=encode_nested_name_spec(nd#nth_child0)inletuqn=uqn_of_type_name(nd#nth_child1)inprefix^uqnend|DecltypeSpecifier->encode_decltypend|_->uqn_of_type_namendanduqn_of_class_name(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ClassNameuqn->uqn|_->invalid_arg"Cpp.Ast.uqn_of_class_name"andqn_of_class_head_name(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ClassHeadNameqn->qn|IdentifierMacroInvocationi->beginletargs=args_to_simple_stringnd#childrenin(mk_macro_call_id~argsi)end|_->uqn_of_class_namendandqn_of_enum_head_name(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|EnumHeadNameuqn->beginmatchnd#nth_children0with|[]->uqn|[n]->beginletp=encode_nested_name_specninp^uqnend|_->assertfalseend|_->invalid_arg"Cpp.Ast.qn_of_enum_head_name"andqn_list_of_using_declaration(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|UsingDeclaration->List.mapqn_of_using_declaratornd#children|_->invalid_arg"Cpp.Ast.qn_list_of_using_declaration"andqn_of_using_declarator(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|UsingDeclarator->beginmatchnd#nth_children1with|[n]->beginletp=encode_nested_name_specninmatchnd#nth_children2with|[n]->p,uqn_of_unqualified_idn|_->assertfalseend|_->assertfalseend|PackExpansion->qn_of_using_declarator(nd#nth_child0)|_->invalid_arg"Cpp.Ast.qn_of_using_declarator"andqn_list_of_using_enum_declaration(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|UsingEnumDeclaration->List.mapqn_of_using_enum_declaratornd#children|_->invalid_arg"Cpp.Ast.qn_list_of_using_enum_declaration"andqn_of_using_enum_declarator(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|UsingEnumDeclarator->beginmatchnd#nth_children0with|[]->beginmatchnd#nth_children1with|[n]->"",uqn_of_unqualified_idn|_->assertfalseend|[n]->beginletp=encode_nested_name_specninmatchnd#nth_children1with|[n]->p,uqn_of_unqualified_idn|_->assertfalseend|_->assertfalseend|PackExpansion->qn_of_using_enum_declarator(nd#nth_child0)|_->invalid_arg"Cpp.Ast.qn_of_using_enum_declarator"andqn_of_qualified_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|QualifiedId->begintryletprefix=encode_nested_name_spec(nd#nth_child0)inprefix^(uqn_of_unqualified_id(nd#nth_child1))withFailure_->assertfalseend|_->invalid_arg"Cpp.Ast.qn_of_qualified_id"andqn_of_id_expression(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|QualifiedId->qn_of_qualified_idnd|_->uqn_of_unqualified_idndandqn_of_declarator_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|PackExpansion->begintry"..."^(qn_of_id_expression(nd#nth_child0))withFailure_->assertfalseend|_->qn_of_id_expressionndandqualifiers_of_node_list(nds:nodelist)=letq=newI.qualifiersinList.iter(fun(nd:node)->matchnd#labelwith|Const->q#set_const()|Volatile->q#set_volatile()|RefQualifierAmp->q#set_amp()|RefQualifierAmpAmp->q#set_amp_amp()|NoexceptSpecifierwhennd#children<>[]->beginmatchnd#childrenwith|[n]->q#set_noexcept_computed(encode_exprn)|_->assertfalseend|NoexceptSpecifier->q#set_noexcept()|NoexceptSpecifierThrow->q#set_throw()|Restrict_->q#set_restrict()|MsStdcall_->q#set_stdcall()|MsCdecl_->q#set_cdecl()|_->())nds;qandvirt_specs_of_node_list(nds:nodelist)=letv=newI.virt_specsinList.iter(fun(nd:node)->matchnd#labelwith|VirtSpecifierFinal->v#set_final()|VirtSpecifierOverride->v#set_override()|ClassVirtSpecifierFinal->v#set_final()|_->())nds;vandnested_namespace_of_node(nd:node)=matchnd#labelwith|EnclosingNamespaceSpecifieri->beginmatchnd#childrenwith|[]->NestedNS.mk1(NS.mki)|[nd0]->NestedNS.append(nested_namespace_of_nodend0)(NS.mki)|[nd0;_]->NestedNS.append(nested_namespace_of_nodend0)(NS.mk~inline:truei)|_->assertfalseend|_->invalid_arg"Cpp.Ast.nested_namespace_of_node"andaccess_spec_of_node(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|Private->N.Spec.Aprivate|Protected->N.Spec.Aprotected|Public->N.Spec.Apublic|AccessSpecMacroi->N.Spec.Amacroi|_->invalid_arg"Cpp.Ast.access_spec_of_node"andbase_spec_of_nodens(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|BaseSpecifier->beginletaccess_spec=match(nd#nth_children1)@(nd#nth_children3)with|[]->None|[a]->Some(access_spec_of_nodea)|_->assertfalseinletis_virtual=matchnd#nth_children2with|[]->false|[_]->true|_->assertfalseinletident=matchnd#nth_children4with|[c]->ns^(qn_of_class_or_decltypec)|_->assertfalseinletspec=newN.Spec.base_spec~access_spec~is_virtualidentinspecend|PackExpansion->beginletspec=base_spec_of_nodens(nd#nth_child0)inspec#set_is_pack_expansion();specend|BaseSpecMacroi->beginnewN.Spec.base_spec(mk_macro_idi)end|BaseSpecMacroInvocationi->beginnewN.Spec.base_spec(mk_macro_call_idi)end|_->invalid_arg"Cpp.Ast.base_spec_of_node"andalt_base_specs_list_of_nodens(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|PpIfSection_->beginletifg=try[fst(base_specs_of_base_clausens((nd#nth_child0)#nth_child1))]with_->[]inletelifg=tryList.map(funx->fst(base_specs_of_base_clausens(x#nth_child1)))(nd#nth_children1)with_->[]inletelsg=tryList.map(funx->fst(base_specs_of_base_clausens(x#nth_child1)))(nd#nth_children2)with_->[]inifg@elifg@elsgend|_->invalid_arg"Cpp.Ast.alt_base_specs_list_of_node"andbase_specs_of_base_clausens(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|BaseClause->beginList.fold_left(fun(bss,abssl)x->matchx#labelwith|L.PpIfSection_->beginletalt_base_specs_list=alt_base_specs_list_of_nodensxinmatchalt_base_specs_listwith|[]->bss,abssl|ys::yss->ys@bss,List.concat_map(funabss->List.map(funys->ys@abss)yss)absslend|_->bss@[(base_spec_of_nodensx)],abssl)([],[])nd#childrenend|ClassVirtSpecifierFinal|ClassVirtSpecifierMsSealed|VirtSpecifierMacro_->[],[]|L.PpIfSection_->beginletalt_base_specs_list=alt_base_specs_list_of_nodensndinmatchalt_base_specs_listwith|[]->[],[]|ys::_->ys,[]end|_->invalid_arg"Cpp.Ast.base_specs_of_base_clause"andqn_class_spec_of_classns(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ClassHeadClass|ClassHeadStruct|ClassHeadUnion|ClassHeadMsRefClass->beginletqn=matchnd#nth_children1with|[]->""|[n]->qn_of_class_head_namen|_->assertfalseinletbase_specs,alt_base_specs_list=matchnd#nth_children3with|[]->[],[]|[b]->base_specs_of_base_clausensb|_->assertfalseinletspec=newN.Spec.class_spec~base_specs~alt_base_specs_listqninbeginmatchnd#nth_children2with|[v]->beginmatchv#labelwith|ClassVirtSpecifierFinal->spec#virt_specs#set_final()|ClassVirtSpecifierMsSealed->spec#virt_specs#set_final()|VirtSpecifierMacro_->()|_->assertfalseend|_->()end;qn,specend|ClassHeadMacroInvocationi->beginletqn=matchnd#nth_children1with|[]->mk_macro_call_idi|[n]->qn_of_class_head_namen|_->assertfalseinletspec=newN.Spec.class_specqninqn,specend|ClassHeadMacroi->beginletqn=mk_macro_idiinletspec=newN.Spec.class_specqninqn,specend|_->invalid_arg"Cpp.Ast.qn_class_spec_of_class"andqn_enum_spec_of_enumns(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|EnumHeadEnum|EnumHeadEnumClass|EnumHeadEnumStruct|OpaqueEnumDeclaration|OpaqueEnumDeclarationClass|OpaqueEnumDeclarationStruct->beginletqn=matchnd#nth_children1with|[]->""|[n]->qn_of_enum_head_namen|_->assertfalseinletenum_base=matchnd#nth_children2with|[]->None|[b]->Some(enum_base_of_nodensb)|_->assertfalseinletspec=newN.Spec.enum_spec~enum_baseqninqn,specend|EnumHeadEnumMacroi|OpaqueEnumDeclarationMacroiwhenbegini="NS_ENUM"||i="NS_OPTIONS"end->beginletqn=(nd#nth_child1)#get_nameinletspec=newN.Spec.enum_specqninqn,specend|_->invalid_arg"Cpp.Ast.qn_enum_spec_of_enum"andident_type_param_spec_of_type_param(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TypeParameteri->beginmatchnd#nth_children2with|[n]->beginmatchn#labelwith|TypeParameterKeyClass->i,N.Spec.Pclass|TypeParameterKeyTypename->i,N.Spec.Ptypename|PackExpansion->beginmatch(n#nth_child0)#labelwith|TypeParameterKeyClass->i,N.Spec.PclassPack|TypeParameterKeyTypename->i,N.Spec.PtypenamePack|_->assertfalseend|_->assertfalseend|_->beginmatchnd#nth_children0with|[n]->beginmatchn#labelwith|TypeConstraintuqn->beginletp=matchn#nth_children0with|[n]->encode_nested_name_specn|_->""ini,N.Spec.Pconcept(p,uqn)end|PackExpansion->beginletn0=n#nth_child0inmatchn0#labelwith|TypeConstraintuqn->beginletp=matchn0#nth_children0with|[n]->encode_nested_name_specn|_->""ini,N.Spec.Pconcept(p,uqn)end|_->assertfalseend|_->assertfalseend|_->assertfalseendend|_->invalid_arg"Cpp.Ast.ident_type_param_spec_of_type_param"andpointer_op_of_node(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|PtrOperatorStar->beginletprefix=matchnd#nth_children0with|[]->""|[n]->encode_nested_name_specn|_->assertfalseinletq=qualifiers_of_node_list(nd#nth_children2)inType.Star(prefix,q)end|PtrOperatorAmp->Type.Amp|PtrOperatorAmpAmp->Type.AmpAmp|PtrOperatorHat->Type.Hat|PtrMacroi->Type.Macroi|_->invalid_arg"Cpp.Ast.pointer_op_of_node"andqn_type_list_of_simple_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|SimpleDeclaration->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children1)in[%debug_log"sty=%s"(Type.to_stringsty)];matchnd#nth_children2with|[]->beginmatchType.idents_of_simple_typestywith|[i]->[nd,i,sty](* typedef? *)|_->[]end|l->beginList.filter_map(funx->matchx#labelwith|L.InitDeclarator_|PpIfSection_->beginleti,w=qn_wrap_of_init_declaratorxinSome(x,i,wsty)end|_->None)lendend|PpIfSection_->beginletif_itl=tryqn_type_list_of_simple_decl((nd#nth_child0)#nth_child1)with_->[]inletelif_itl=List.flatten(Xlist.filter_map(funx->trySome(qn_type_list_of_simple_decl(x#nth_child1))with_->None)(nd#nth_children1))inletelse_itl=List.flatten(Xlist.filter_map(funx->trySome(qn_type_list_of_simple_decl(x#nth_child1))with_->None)(nd#nth_children2))inif_itl@elif_itl@else_itlend|DeclarationMacro_->[]|_->invalid_arg"Cpp.Ast.qn_type_list_of_simple_decl"andqn_type_list_of_enumeratorscope(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];letrecget_qn(n:node)=matchn#labelwith|Enumeratori->encode_identi|EnumeratorMacroInvocationi->mk_macro_call_idi|EnumeratorDefinition->get_qn(n#nth_child0)|EnumeratorDefinitionMacroi->mk_macro_idi|_->invalid_arg"Cpp.Ast.qn_type_list_of_enumerator"inletqn=get_qnndinletet=letn=N.Scope.get_namescopeinifN.Scope.is_enumclassscopethenI.ElaboratedType.EnumClassnelseI.ElaboratedType.Enumninletts=I.TypeSpec.Elaboratedetinletty=I.Type.make_simple_type(newI.decl_specs)[ts](I.TypeSpec.encode[ts])in[nd,qn,ty]andqn_type_list_of_mem_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|MemberDeclarationDecl->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children1)in[%debug_log"sty=%s"(Type.to_stringsty)];matchnd#nth_children2with|[]->beginmatchType.idents_of_simple_typestywith|[i]->[nd,i,sty]|_->[]end|l->beginList.map(funx->leti,w=qn_wrap_of_mem_declaratorxinx,i,wsty)lendend|_->invalid_arg"Cpp.Ast.qn_type_list_of_mem_decl"andqn_type_of_func_def(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|FunctionDefinition_|FunctionHead_->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children1)in[%debug_log"sty=%s"(Type.to_stringsty)];matchnd#nth_children2with|[d]->beginleti,w=qn_wrap_of_declaratordinletfty=wstyin[%debug_log"fty=%s"(Type.to_stringfty)];letrecproc_ty=function|Type.FunctionTyx->beginletv=x.Type.ft_virt_specsintryList.iter(fun(nd:node)->matchnd#labelwith|VirtSpecifierFinal->v#set_final()|VirtSpecifierOverride->v#set_override()|_->())(nd#nth_children3)with_->()end|Type.PointerTyx->proc_tyx.Type.pt_type|Type.AltTy_->()|_->()inproc_tyfty;i,ftyend|_->assertfalseend|_->invalid_arg"Cpp.Ast.qn_type_of_func_def"andqn_wrap_of_init_declarator(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|InitDeclarator_->beginmatchnd#nth_children1with|[d]->qn_wrap_of_declaratord|_->assertfalseend|PpIfSection_->beginletns=((nd#nth_child0)#nth_child1)::(List.map(funx->x#nth_child1)(nd#nth_children1))@(List.map(funx->x#nth_child1)(nd#nth_children2))inletil,wl=List.split(List.filter_map(funx->matchx#labelwith|L.InitDeclarator_|PpIfSection_->Some(qn_wrap_of_init_declaratorx)|_->None)ns)inleti=String.concat"|"ilinletwx=Type.make_alt_type(List.map(funw->wx)wl)ini,wend|_->invalid_arg"Cpp.Ast.qn_wrap_of_init_declarator"andqn_wrap_of_mem_declarator(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|MemberDeclaratorDecl->qn_wrap_of_declarator(nd#nth_child0)|MemberDeclaratorBitFieldi->i,funx->x|PpDefine_->"",funx->x|_->invalid_arg"Cpp.Ast.qn_wrap_of_mem_declarator"andqn_wrap_of_declarator?(ty_opt=None)(nd:node)=begin%debug_block[%debug_log"%s"(L.to_stringnd#label)];matchty_optwith|Somety->[%debug_log"%s"(I.Type.to_stringty)];|None->()end;matchnd#labelwith|DummyDtor->"",funx->x|DtorMacroi->mk_macro_idi,funx->x|PpIfSection_->beginletns=((nd#nth_child0)#nth_child1)::(List.map(funx->x#nth_child1)(nd#nth_children1))@(List.map(funx->x#nth_child1)(nd#nth_children2))inletil,wl=List.split(List.mapqn_wrap_of_declaratorns)inleti=String.concat"|"ilinletwx=Type.make_alt_type(List.map(funw->wx)wl)ini,wend|DeclaratorFunc->beginletw1=wrap_of_params_and_quals(nd#nth_child1)inleti,w0=qn_wrap_of_declarator(nd#nth_child0)inlett=type_of_trailing_ret_type(nd#nth_child2)ini,fun_->w0(w1t)end|PtrDeclaratorPtr->beginletqualifiers=qualifiers_of_node_list(nd#nth_children0)inmatchnd#nth_children3with|[n2]->beginmatchnd#nth_children1with|[]->beginqn_wrap_of_declaratorn2end|[n1]->beginletop=pointer_op_of_noden1inleti,w=qn_wrap_of_declaratorn2ini,funx->w(Type.make_pointer_type~qualifiersopx)end|_->assertfalseend|_->assertfalseend|NoptrDeclarator->beginlet_,w1=tryqn_wrap_of_declarator(nd#nth_child1)with_->"",funx->xinletn0=nd#nth_child0inleti,w0=qn_wrap_of_declaratorn0inletw=matchn0#labelwith|NoptrDeclaratorParen|NoptrAbstractDeclaratorParen->funx->w0(w1x)|_->funx->w1(w0x)ini,wend|NoptrDeclaratorId->(qn_of_declarator_id(nd#nth_child0)),funx->x|NoptrDeclaratorFunc->beginletw1=trywrap_of_params_and_quals(nd#nth_child1)with_->funx->xinletn0=nd#nth_child0inleti,w0=qn_wrap_of_declaratorn0inletw=matchn0#labelwith|NoptrDeclaratorParen|NoptrAbstractDeclaratorParen->funx->w0(w1x)|_->funx->w1(w0x)ini,wend|PpIfSectionBrokenDtorFunc->beginletw1=matchnd#nth_children4with|[pdc]->beginletptys,is_vararg=param_tys_and_is_vararg_of_param_decl_clausepdcinfunrty->Type.make_function_type~is_varargptysrtyend|_->assertfalseinleti_w_list=List.map(fung->[%debug_log"%s"(L.to_stringg#label)];letn0=matchg#nth_children2with|[x]->x|_->assertfalseinleti,w0=qn_wrap_of_declaratorn0inletw=matchn0#labelwith|NoptrDeclaratorParen|NoptrAbstractDeclaratorParen->funx->w0(w1x)|_->funx->w1(w0x)ini,w)((nd#nth_child0)::(nd#nth_children1)@(nd#nth_children2))inletil,wl=List.spliti_w_listinleti=String.concat"|"ilinletwx=Type.make_alt_type(List.map(funw->wx)wl)ini,wend|NoptrDeclaratorOldFunc->beginletids=List.map(funx->matchx#labelwith|L.Identifiers->s|L.ParamDeclMacroi->mk_macro_idi|L.PpIfSection_->beginmatch((x#nth_child0)#nth_child1)#labelwith|L.Identifiers->s|_->assertfalseend|_->assertfalse)(nd#nth_children1)inletfind=lettbl=Hashtbl.create0inList.iter(funx->List.iter(fun(_,i,ty)->Hashtbl.addtblity)(qn_type_list_of_simple_declx))(nd#nth_children2);Hashtbl.findtblinletptys=List.map(funi->tryfindiwithNot_found->Type.int_t)idsinletw1=funrty->Type.make_function_typeptysrtyinletn0=nd#nth_child0inleti,w0=qn_wrap_of_declaratorn0inletw=matchn0#labelwith|NoptrDeclaratorParen|NoptrAbstractDeclaratorParen->funx->w0(w1x)|_->funx->w1(w0x)ini,wend|NoptrDeclaratorArray->beginleti,w=qn_wrap_of_declarator(nd#nth_child0)ini,funty->Type.make_array_type(wty)1end|NoptrDeclaratorParen->qn_wrap_of_declarator(nd#nth_child0)|AbstractDeclaratorFunc->beginleti,w0=matchnd#nth_children0with|[]->"",funx->x|[n0]->qn_wrap_of_declaratorn0|_->assertfalseinletw1=matchnd#nth_children1with|[n1]->wrap_of_params_and_qualsn1|_->assertfalseinlett=matchnd#nth_children2with|[n2]->type_of_trailing_ret_typen2|_->assertfalseini,fun_->w0(w1t)end|PtrAbstractDeclaratorPtr->beginletop=matchnd#nth_children1with|[x]->pointer_op_of_nodex|_->assertfalseinmatchty_optwith|Somety->beginmatchnd#nth_children2with|[]->"",letx=Type.make_pointer_typeoptyinfun_->x|[n]->beginleti,w=qn_wrap_of_declarator~ty_optnini,letx=Type.make_pointer_typeop(wty)infun_->wxend|_->assertfalseend|None->beginmatchnd#nth_children2with|[]->"",Type.make_pointer_typeop|[n]->beginleti,w=qn_wrap_of_declaratornini,funx->w(Type.make_pointer_typeop(wx))end|_->assertfalseendend|NewDeclaratorPtr|ConversionDeclarator|AbstractPackDeclarator->beginletop=pointer_op_of_node(nd#nth_child0)inmatchnd#nth_children1with|[]->"",Type.make_pointer_typeop|[n]->beginleti,w=qn_wrap_of_declaratornini,funx->w(Type.make_pointer_typeop(wx))end|_->assertfalseend|NoptrAbstractDeclaratorFunc->beginletw1=matchnd#nth_children1with|[n1]->wrap_of_params_and_qualsn1|_->assertfalseinmatchnd#nth_children0with|[]->"",w1|[n0]->beginleti,w0=qn_wrap_of_declaratorn0inletw=matchn0#labelwith|NoptrDeclaratorParen|NoptrAbstractDeclaratorParen->funx->w0(w1x)|_->funx->w1(w0x)ini,wend|_->assertfalseend|NoptrAbstractDeclaratorArray->beginleti,w=matchnd#nth_children0with|[]->"",funx->x|[n]->qn_wrap_of_declaratorn|_->assertfalseini,funx->Type.make_array_type(wx)1end|NoptrAbstractDeclaratorParen->qn_wrap_of_declarator(nd#nth_child0)|NoptrNewDeclarator->beginleti,w=matchnd#nth_children0with|[]->"",funx->x|[n]->qn_wrap_of_declaratorn|_->assertfalseini,funx->Type.make_array_type(wx)1end|NoptrAbstractPackDeclaratorFunc->beginletw1=wrap_of_params_and_quals(nd#nth_child1)inlet_,w0=qn_wrap_of_declarator(nd#nth_child0)in"",funx->w1(w0x)end|NoptrAbstractPackDeclaratorArray->beginlet_,w=qn_wrap_of_declarator(nd#nth_child0)in"",funx->Type.make_array_type(wx)1end|AbstractPack->"",funx->x|InitDeclarator_->qn_wrap_of_declarator(nd#nth_child0)|_->invalid_arg"Cpp.Ast.qn_wrap_of_declarator"andtype_of_trailing_ret_type(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TrailingReturnType->type_of_type_id(nd#nth_child0)|_->invalid_arg"Cpp.Ast.type_of_trailing_return_type"andtype_of_type_id(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|TypeId|NewTypeId|ConversionTypeId->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children0)inmatchnd#nth_children1with|[]->sty|[n]->beginlet_,w=qn_wrap_of_declaratorninwstyend|_->assertfalseend|_->invalid_arg"Cpp.Ast.type_of_type_id"andwrap_of_params_and_quals(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ParametersAndQualifiers->beginletptys,is_vararg=param_tys_and_is_vararg_of_param_decl_clause(nd#nth_child0)inletqs=(nd#nth_children1)@(nd#nth_children2)@(nd#nth_children3)inletqualifiers=qualifiers_of_node_listqsinfunrty->Type.make_function_type~qualifiers~is_varargptysrtyend|ParametersMacrop->funrty->Type.make_function_type~params_macro:p[]rty|ParametersMacroInvocationp->beginmatchnd#childrenwith|[n0]->beginmatchn0#labelwith|MacroArgument->beginmatchn0#childrenwith|[n00]->beginmatchn00#labelwith|ParametersAndQualifiers->wrap_of_params_and_qualsn00|_->funrty->Type.make_function_type~params_macro:p[]rtyend|_->funrty->Type.make_function_type~params_macro:p[]rtyend|_->assertfalseend|_->funrty->Type.make_function_type~params_macro:p[]rtyend|_->invalid_arg"Cpp.Ast.wrap_of_params_and_quals"andparam_tys_and_is_vararg_of_param_decl_clause(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ParameterDeclarationClauseb->beginmatchnd#childrenwith|[]->[],b|[n]->beginmatchn#labelwith|ParameterDeclaration_->begintryletty=type_of_param_declnin[ty],bwith_->[],bend|_->assertfalseend|l->(Xlist.filter_map(funx->trySome(type_of_param_declx)with_->None)l),bend|_->invalid_arg"Cpp.Ast.param_tys_of_param_decl_clause"andtype_of_param_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];let_,_,ty=qn_type_of_param_declndintyandqn_type_of_param_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];letil,tl=List.fold_left(fun(il,tl)(_,i,t)->i::il,t::tl)([],[])(qn_type_list_of_param_declnd)inleti=String.concat"|"ilinlett=matchtlwith|[]->raiseNot_found|[t]->t|_->Type.make_alt_typetlinnd,i,tandqn_type_list_of_param_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ParameterDeclaration_->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children1)inletqn,wrap=matchnd#nth_children2with|[n]->qn_wrap_of_declarator~ty_opt:(Somesty)n|_->"",funx->xin[nd,qn,wrapsty]end|PpIfSection_->beginletif_itl=try[qn_type_of_param_decl((nd#nth_child0)#nth_child1)]with_->[]inletelif_itl=Xlist.filter_map(funx->trySome(qn_type_of_param_decl(x#nth_child1))with_->None)(nd#nth_children1)inletelse_itl=Xlist.filter_map(funx->trySome(qn_type_of_param_decl(x#nth_child1))with_->None)(nd#nth_children2)inif_itl@elif_itl@else_itlend|ParamDeclMacro_|ParamDeclMacroInvocation_->[]|_->[](*| _ -> invalid_arg "Cpp.Ast.qn_type_list_of_param_decl"*)andqn_type_of_exc_decl(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|ExceptionDeclaration->beginletsty=simple_type_of_decl_spec_seq(nd#nth_children1)inletqn,wrap=matchnd#nth_children2with|[n]->qn_wrap_of_declaratorn|_->"",funx->xinnd,qn,wrapstyend|Ellipsis->raiseType_not_found|_->invalid_arg"Cpp.Ast.qn_type_of_exc_decl"andenum_base_of_node(*ns*)_(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|EnumBase->newN.Spec.enum_base(type_spec_list_of_node_listnd#children)|BaseMacroi->newN.Spec.enum_base~macro_name:i[]|_->invalid_arg"Cpp.Ast.enum_base_of_node"andtype_spec_of_node?(ns="")(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];matchnd#labelwith|SimpleTypeSpecifieruqn->beginletprefix=matchnd#nth_children0with|[]->ns|[n0]->ns^(encode_nested_name_specn0)|_->assertfalseinletfqn=prefix^uqnin[%debug_log"fqn=%s"fqn];I.TypeSpec.Simplefqnend|DecltypeSpecifier->I.TypeSpec.Decltype(encode_decltypend)|PlaceholderTypeSpecifierAuto->I.TypeSpec.PlaceholderI.PlaceholderType.Auto|PlaceholderTypeSpecifierDecltype->I.TypeSpec.PlaceholderI.PlaceholderType.Decltype|Char->I.TypeSpec.Char|Char8_t->I.TypeSpec.Char8_t|Char16_t->I.TypeSpec.Char16_t|Char32_t->I.TypeSpec.Char32_t|Wchar_t->I.TypeSpec.Wchar_t|Bool->I.TypeSpec.Bool|Short->I.TypeSpec.Short|Int->I.TypeSpec.Int|Long->I.TypeSpec.Long|Signed->I.TypeSpec.Signed|Unsigned->I.TypeSpec.Unsigned|Float->I.TypeSpec.Float|Double->I.TypeSpec.Double|Void->I.TypeSpec.Void|UnsignedInt->I.TypeSpec.UnsignedInt|UnsignedLong->I.TypeSpec.UnsignedLong|ElaboratedTypeSpecifierClassu->I.TypeSpec.Elaborated(I.ElaboratedType.Class(ns^u))|ElaboratedTypeSpecifierStructu->I.TypeSpec.Elaborated(I.ElaboratedType.Struct(ns^u))|ElaboratedTypeSpecifierUnionu->I.TypeSpec.Elaborated(I.ElaboratedType.Union(ns^u))|ElaboratedTypeSpecifierEnumu->I.TypeSpec.Elaborated(I.ElaboratedType.Enum(ns^u))|TypenameSpecifieruqn->I.TypeSpec.Typename(ns^uqn)|Const->I.TypeSpec.CvQualifierI.CvQualifier.Const|Volatile->I.TypeSpec.CvQualifierI.CvQualifier.Volatile|Restrict_->I.TypeSpec.CvQualifierI.CvQualifier.Restrict|MsCdecl_->I.TypeSpec.CvQualifierI.CvQualifier.Cdecl|MsStdcall_->I.TypeSpec.CvQualifierI.CvQualifier.Stdcall|_->invalid_arg"Cpp.Ast.type_spec_of_node"andsimple_type_of_class_head(nd:node)=[%debug_log"%s"(L.to_stringnd#label)];tryletis_macro=reffalseinletk=matchnd#labelwith|ClassHeadClass->funx->I.ElaboratedType.Classx|ClassHeadStruct->funx->I.ElaboratedType.Structx|ClassHeadUnion->funx->I.ElaboratedType.Unionx|ClassHeadMacroi->is_macro:=true;fun_->I.ElaboratedType.Macro(mk_macro_idi)|ClassHeadMacroInvocationi->is_macro:=true;fun_->I.ElaboratedType.Macro(mk_macro_call_idi)|PpIfSection_->raiseExit(*| _ -> raise Not_found*)|_->assertfalseinletn=matchnd#nth_children1with|_when!is_macro->""|[]->""|[x]->beginmatchx#labelwith|ClassNameuqn->uqn|ClassHeadNameqn->(encode_nested_name_spec(x#nth_child0))^qn|IdentifierMacroInvocationi->letargs=args_to_simple_stringx#childreninmk_macro_call_id~argsi|_->assertfalseend|_->assertfalsein[I.TypeSpec.Elaborated(kn)]with(*| Not_found -> []*)|Exit->letgx=x#nth_child1inletchs=(g(nd#nth_child0))::(List.mapg(nd#nth_children1))@(List.mapg(nd#nth_children2))inList.concat_mapsimple_type_of_class_headchsandsimple_type_of_decl_spec_seq(nds:nodelist)=[%debug_log"nds=[%s]"(String.concat"; "(List.map(funn->L.to_stringn#label)nds))];matchndswith|[n]whenbeginmatchn#labelwith|DeclSpecifierSeq->true|_->falseend->simple_type_of_decl_spec_seq(n#nth_children0)|_->letds=newI.decl_specsinletts=List.fold_left(funts(x:node)->[%debug_log"%s"(L.to_stringx#label)];matchx#labelwith|StorageClassSpecifierStatic->ds#set_storage_class_static();ts|StorageClassSpecifierThread_local->ds#set_storage_class_thread_local();ts|StorageClassSpecifierExtern->ds#set_storage_class_extern();ts|StorageClassSpecifierMutable->ds#set_storage_class_mutable();ts|FunctionSpecifierVirtual->ds#set_function_spec_virtual();ts|ExplicitSpecifier->ds#set_function_spec_explicit();ts|DeclSpecifierFriend->ds#set_friend();ts|DeclSpecifierTypedef->ds#set_typedef();ts|DeclSpecifierConstexpr->ds#set_constexpr();ts|DeclSpecifierConsteval->ds#set_consteval();ts|DeclSpecifierInline->ds#set_inline();ts|SimpleTypeSpecifier_|DecltypeSpecifier|PlaceholderTypeSpecifierAuto|PlaceholderTypeSpecifierDecltype|Char|Char8_t|Char16_t|Char32_t|Wchar_t|Bool|Short|Int|Long|Signed|Unsigned|Float|Double|Void|Const|Volatile->(type_spec_of_nodex)::ts|ElaboratedTypeSpecifierClass_|ElaboratedTypeSpecifierStruct_|ElaboratedTypeSpecifierUnion_|ElaboratedTypeSpecifierEnum_->letns=tryencode_nested_name_spec(List.hd(x#nth_children1))with_->""in(type_spec_of_node~nsx)::ts|TypenameSpecifier_->letns=tryencode_nested_name_spec(List.hd(x#nth_children0))with_->""in(type_spec_of_node~nsx)::ts|ClassSpecifier->beginmatchx#nth_children1with|[y]->(simple_type_of_class_heady)@ts|_->assertfalseend|EnumSpecifier->beginmatchx#nth_children1with|[y]->beginletk=matchy#labelwith|EnumHeadEnum|EnumHeadEnumMacro_->funx->I.ElaboratedType.Enumx|EnumHeadEnumClass|EnumHeadEnumStruct->funx->I.ElaboratedType.EnumClassx|lab->let_=labin[%debug_log"%s"(L.to_stringlab)];assertfalseinletn=matchy#nth_children1with|[]->""|[z]->beginmatchz#labelwith|EnumHeadNameqnwhenz#children=[]->qn|EnumHeadNameqn->(encode_nested_name_spec(List.hdz#children))^qn|_->assertfalseend|_->[%debug_log"@"];assertfalsein(I.TypeSpec.Elaborated(kn))::tsend|_->assertfalseend|_->ts)[]ndsin[%debug_log"ts=[%s]"(String.concat"; "(List.mapI.TypeSpec.to_stringts))];letencoded=I.TypeSpec.encodetsinType.make_simple_typedstsencoded]classc(root:node)=objectinheritAst_base.cmethodroot=rootend(* class Ast.c *)letdummy_node=newnodeL.DUMMYletempty_node=newnodeL.EMPTYletsubtree_to_stringroot=letbuf=Buffer.create0inletrecdoitindnd=Buffer.add_stringbuf(sprintf"%s%s\n"indnd#to_string);List.iter(doit(ind^" "))nd#childrenindoit""root;Buffer.contentsbufletto_stringast=subtree_to_stringast#rootletdumpast=letrecdoitindnd=printf"%s%s\n"indnd#to_string;List.iter(doit(ind^" "))nd#childrenindoit""ast#rootletiterf(ast:c)=letrecdoitnd=fnd;List.iterdoitnd#childrenindoitast#root