ItaniumDemangle.h 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- ItaniumDemangle.h -----------*- mode:c++;eval:(read-only-mode) -*-===//
  7. // Do not edit! See README.txt.
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Generic itanium demangler library.
  15. // There are two copies of this file in the source tree. The one under
  16. // libcxxabi is the original and the one under llvm is the copy. Use
  17. // cp-to-llvm.sh to update the copy. See README.txt for more details.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef DEMANGLE_ITANIUMDEMANGLE_H
  21. #define DEMANGLE_ITANIUMDEMANGLE_H
  22. // FIXME: (possibly) incomplete list of features that clang mangles that this
  23. // file does not yet support:
  24. // - C++ modules TS
  25. #include "DemangleConfig.h"
  26. #include "StringView.h"
  27. #include "Utility.h"
  28. #include <algorithm>
  29. #include <cassert>
  30. #include <cctype>
  31. #include <cstdio>
  32. #include <cstdlib>
  33. #include <cstring>
  34. #include <limits>
  35. #include <utility>
  36. #define FOR_EACH_NODE_KIND(X) \
  37. X(NodeArrayNode) \
  38. X(DotSuffix) \
  39. X(VendorExtQualType) \
  40. X(QualType) \
  41. X(ConversionOperatorType) \
  42. X(PostfixQualifiedType) \
  43. X(ElaboratedTypeSpefType) \
  44. X(NameType) \
  45. X(AbiTagAttr) \
  46. X(EnableIfAttr) \
  47. X(ObjCProtoName) \
  48. X(PointerType) \
  49. X(ReferenceType) \
  50. X(PointerToMemberType) \
  51. X(ArrayType) \
  52. X(FunctionType) \
  53. X(NoexceptSpec) \
  54. X(DynamicExceptionSpec) \
  55. X(FunctionEncoding) \
  56. X(LiteralOperator) \
  57. X(SpecialName) \
  58. X(CtorVtableSpecialName) \
  59. X(QualifiedName) \
  60. X(NestedName) \
  61. X(LocalName) \
  62. X(VectorType) \
  63. X(PixelVectorType) \
  64. X(BinaryFPType) \
  65. X(SyntheticTemplateParamName) \
  66. X(TypeTemplateParamDecl) \
  67. X(NonTypeTemplateParamDecl) \
  68. X(TemplateTemplateParamDecl) \
  69. X(TemplateParamPackDecl) \
  70. X(ParameterPack) \
  71. X(TemplateArgumentPack) \
  72. X(ParameterPackExpansion) \
  73. X(TemplateArgs) \
  74. X(ForwardTemplateReference) \
  75. X(NameWithTemplateArgs) \
  76. X(GlobalQualifiedName) \
  77. X(StdQualifiedName) \
  78. X(ExpandedSpecialSubstitution) \
  79. X(SpecialSubstitution) \
  80. X(CtorDtorName) \
  81. X(DtorName) \
  82. X(UnnamedTypeName) \
  83. X(ClosureTypeName) \
  84. X(StructuredBindingName) \
  85. X(BinaryExpr) \
  86. X(ArraySubscriptExpr) \
  87. X(PostfixExpr) \
  88. X(ConditionalExpr) \
  89. X(MemberExpr) \
  90. X(SubobjectExpr) \
  91. X(EnclosingExpr) \
  92. X(CastExpr) \
  93. X(SizeofParamPackExpr) \
  94. X(CallExpr) \
  95. X(NewExpr) \
  96. X(DeleteExpr) \
  97. X(PrefixExpr) \
  98. X(FunctionParam) \
  99. X(ConversionExpr) \
  100. X(PointerToMemberConversionExpr) \
  101. X(InitListExpr) \
  102. X(FoldExpr) \
  103. X(ThrowExpr) \
  104. X(BoolExpr) \
  105. X(StringLiteral) \
  106. X(LambdaExpr) \
  107. X(EnumLiteral) \
  108. X(IntegerLiteral) \
  109. X(FloatLiteral) \
  110. X(DoubleLiteral) \
  111. X(LongDoubleLiteral) \
  112. X(BracedExpr) \
  113. X(BracedRangeExpr)
  114. DEMANGLE_NAMESPACE_BEGIN
  115. template <class T, size_t N> class PODSmallVector {
  116. static_assert(std::is_pod<T>::value,
  117. "T is required to be a plain old data type");
  118. T *First = nullptr;
  119. T *Last = nullptr;
  120. T *Cap = nullptr;
  121. T Inline[N] = {0};
  122. bool isInline() const { return First == Inline; }
  123. void clearInline() {
  124. First = Inline;
  125. Last = Inline;
  126. Cap = Inline + N;
  127. }
  128. void reserve(size_t NewCap) {
  129. size_t S = size();
  130. if (isInline()) {
  131. auto *Tmp = static_cast<T *>(std::malloc(NewCap * sizeof(T)));
  132. if (Tmp == nullptr)
  133. std::terminate();
  134. std::copy(First, Last, Tmp);
  135. First = Tmp;
  136. } else {
  137. First = static_cast<T *>(std::realloc(First, NewCap * sizeof(T)));
  138. if (First == nullptr)
  139. std::terminate();
  140. }
  141. Last = First + S;
  142. Cap = First + NewCap;
  143. }
  144. public:
  145. PODSmallVector() : First(Inline), Last(First), Cap(Inline + N) {}
  146. PODSmallVector(const PODSmallVector &) = delete;
  147. PODSmallVector &operator=(const PODSmallVector &) = delete;
  148. PODSmallVector(PODSmallVector &&Other) : PODSmallVector() {
  149. if (Other.isInline()) {
  150. std::copy(Other.begin(), Other.end(), First);
  151. Last = First + Other.size();
  152. Other.clear();
  153. return;
  154. }
  155. First = Other.First;
  156. Last = Other.Last;
  157. Cap = Other.Cap;
  158. Other.clearInline();
  159. }
  160. PODSmallVector &operator=(PODSmallVector &&Other) {
  161. if (Other.isInline()) {
  162. if (!isInline()) {
  163. std::free(First);
  164. clearInline();
  165. }
  166. std::copy(Other.begin(), Other.end(), First);
  167. Last = First + Other.size();
  168. Other.clear();
  169. return *this;
  170. }
  171. if (isInline()) {
  172. First = Other.First;
  173. Last = Other.Last;
  174. Cap = Other.Cap;
  175. Other.clearInline();
  176. return *this;
  177. }
  178. std::swap(First, Other.First);
  179. std::swap(Last, Other.Last);
  180. std::swap(Cap, Other.Cap);
  181. Other.clear();
  182. return *this;
  183. }
  184. // NOLINTNEXTLINE(readability-identifier-naming)
  185. void push_back(const T &Elem) {
  186. if (Last == Cap)
  187. reserve(size() * 2);
  188. *Last++ = Elem;
  189. }
  190. // NOLINTNEXTLINE(readability-identifier-naming)
  191. void pop_back() {
  192. assert(Last != First && "Popping empty vector!");
  193. --Last;
  194. }
  195. void dropBack(size_t Index) {
  196. assert(Index <= size() && "dropBack() can't expand!");
  197. Last = First + Index;
  198. }
  199. T *begin() { return First; }
  200. T *end() { return Last; }
  201. bool empty() const { return First == Last; }
  202. size_t size() const { return static_cast<size_t>(Last - First); }
  203. T &back() {
  204. assert(Last != First && "Calling back() on empty vector!");
  205. return *(Last - 1);
  206. }
  207. T &operator[](size_t Index) {
  208. assert(Index < size() && "Invalid access!");
  209. return *(begin() + Index);
  210. }
  211. void clear() { Last = First; }
  212. ~PODSmallVector() {
  213. if (!isInline())
  214. std::free(First);
  215. }
  216. };
  217. // Base class of all AST nodes. The AST is built by the parser, then is
  218. // traversed by the printLeft/Right functions to produce a demangled string.
  219. class Node {
  220. public:
  221. enum Kind : unsigned char {
  222. #define ENUMERATOR(NodeKind) K ## NodeKind,
  223. FOR_EACH_NODE_KIND(ENUMERATOR)
  224. #undef ENUMERATOR
  225. };
  226. /// Three-way bool to track a cached value. Unknown is possible if this node
  227. /// has an unexpanded parameter pack below it that may affect this cache.
  228. enum class Cache : unsigned char { Yes, No, Unknown, };
  229. private:
  230. Kind K;
  231. // FIXME: Make these protected.
  232. public:
  233. /// Tracks if this node has a component on its right side, in which case we
  234. /// need to call printRight.
  235. Cache RHSComponentCache;
  236. /// Track if this node is a (possibly qualified) array type. This can affect
  237. /// how we format the output string.
  238. Cache ArrayCache;
  239. /// Track if this node is a (possibly qualified) function type. This can
  240. /// affect how we format the output string.
  241. Cache FunctionCache;
  242. public:
  243. Node(Kind K_, Cache RHSComponentCache_ = Cache::No,
  244. Cache ArrayCache_ = Cache::No, Cache FunctionCache_ = Cache::No)
  245. : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_),
  246. FunctionCache(FunctionCache_) {}
  247. /// Visit the most-derived object corresponding to this object.
  248. template<typename Fn> void visit(Fn F) const;
  249. // The following function is provided by all derived classes:
  250. //
  251. // Call F with arguments that, when passed to the constructor of this node,
  252. // would construct an equivalent node.
  253. //template<typename Fn> void match(Fn F) const;
  254. bool hasRHSComponent(OutputBuffer &OB) const {
  255. if (RHSComponentCache != Cache::Unknown)
  256. return RHSComponentCache == Cache::Yes;
  257. return hasRHSComponentSlow(OB);
  258. }
  259. bool hasArray(OutputBuffer &OB) const {
  260. if (ArrayCache != Cache::Unknown)
  261. return ArrayCache == Cache::Yes;
  262. return hasArraySlow(OB);
  263. }
  264. bool hasFunction(OutputBuffer &OB) const {
  265. if (FunctionCache != Cache::Unknown)
  266. return FunctionCache == Cache::Yes;
  267. return hasFunctionSlow(OB);
  268. }
  269. Kind getKind() const { return K; }
  270. virtual bool hasRHSComponentSlow(OutputBuffer &) const { return false; }
  271. virtual bool hasArraySlow(OutputBuffer &) const { return false; }
  272. virtual bool hasFunctionSlow(OutputBuffer &) const { return false; }
  273. // Dig through "glue" nodes like ParameterPack and ForwardTemplateReference to
  274. // get at a node that actually represents some concrete syntax.
  275. virtual const Node *getSyntaxNode(OutputBuffer &) const { return this; }
  276. void print(OutputBuffer &OB) const {
  277. printLeft(OB);
  278. if (RHSComponentCache != Cache::No)
  279. printRight(OB);
  280. }
  281. // Print the "left" side of this Node into OutputBuffer.
  282. virtual void printLeft(OutputBuffer &) const = 0;
  283. // Print the "right". This distinction is necessary to represent C++ types
  284. // that appear on the RHS of their subtype, such as arrays or functions.
  285. // Since most types don't have such a component, provide a default
  286. // implementation.
  287. virtual void printRight(OutputBuffer &) const {}
  288. virtual StringView getBaseName() const { return StringView(); }
  289. // Silence compiler warnings, this dtor will never be called.
  290. virtual ~Node() = default;
  291. #ifndef NDEBUG
  292. DEMANGLE_DUMP_METHOD void dump() const;
  293. #endif
  294. };
  295. class IASTProcessor {
  296. public:
  297. virtual void Process(const Node*) = 0;
  298. };
  299. void demangleAST(const char *MangledName, IASTProcessor *Processor);
  300. class NodeArray {
  301. Node **Elements;
  302. size_t NumElements;
  303. public:
  304. NodeArray() : Elements(nullptr), NumElements(0) {}
  305. NodeArray(Node **Elements_, size_t NumElements_)
  306. : Elements(Elements_), NumElements(NumElements_) {}
  307. bool empty() const { return NumElements == 0; }
  308. size_t size() const { return NumElements; }
  309. Node **begin() const { return Elements; }
  310. Node **end() const { return Elements + NumElements; }
  311. Node *operator[](size_t Idx) const { return Elements[Idx]; }
  312. void printWithComma(OutputBuffer &OB) const {
  313. bool FirstElement = true;
  314. for (size_t Idx = 0; Idx != NumElements; ++Idx) {
  315. size_t BeforeComma = OB.getCurrentPosition();
  316. if (!FirstElement)
  317. OB += ", ";
  318. size_t AfterComma = OB.getCurrentPosition();
  319. Elements[Idx]->print(OB);
  320. // Elements[Idx] is an empty parameter pack expansion, we should erase the
  321. // comma we just printed.
  322. if (AfterComma == OB.getCurrentPosition()) {
  323. OB.setCurrentPosition(BeforeComma);
  324. continue;
  325. }
  326. FirstElement = false;
  327. }
  328. }
  329. };
  330. struct NodeArrayNode : Node {
  331. NodeArray Array;
  332. NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {}
  333. template<typename Fn> void match(Fn F) const { F(Array); }
  334. void printLeft(OutputBuffer &OB) const override { Array.printWithComma(OB); }
  335. };
  336. class DotSuffix final : public Node {
  337. const Node *Prefix;
  338. const StringView Suffix;
  339. public:
  340. DotSuffix(const Node *Prefix_, StringView Suffix_)
  341. : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {}
  342. template<typename Fn> void match(Fn F) const { F(Prefix, Suffix); }
  343. void printLeft(OutputBuffer &OB) const override {
  344. Prefix->print(OB);
  345. OB += " (";
  346. OB += Suffix;
  347. OB += ")";
  348. }
  349. };
  350. class VendorExtQualType final : public Node {
  351. const Node *Ty;
  352. StringView Ext;
  353. const Node *TA;
  354. public:
  355. VendorExtQualType(const Node *Ty_, StringView Ext_, const Node *TA_)
  356. : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_), TA(TA_) {}
  357. template <typename Fn> void match(Fn F) const { F(Ty, Ext, TA); }
  358. void printLeft(OutputBuffer &OB) const override {
  359. Ty->print(OB);
  360. OB += " ";
  361. OB += Ext;
  362. if (TA != nullptr)
  363. TA->print(OB);
  364. }
  365. };
  366. enum FunctionRefQual : unsigned char {
  367. FrefQualNone,
  368. FrefQualLValue,
  369. FrefQualRValue,
  370. };
  371. enum Qualifiers {
  372. QualNone = 0,
  373. QualConst = 0x1,
  374. QualVolatile = 0x2,
  375. QualRestrict = 0x4,
  376. };
  377. inline Qualifiers operator|=(Qualifiers &Q1, Qualifiers Q2) {
  378. return Q1 = static_cast<Qualifiers>(Q1 | Q2);
  379. }
  380. class QualType final : public Node {
  381. protected:
  382. const Qualifiers Quals;
  383. const Node *Child;
  384. void printQuals(OutputBuffer &OB) const {
  385. if (Quals & QualConst)
  386. OB += " const";
  387. if (Quals & QualVolatile)
  388. OB += " volatile";
  389. if (Quals & QualRestrict)
  390. OB += " restrict";
  391. }
  392. public:
  393. QualType(const Node *Child_, Qualifiers Quals_)
  394. : Node(KQualType, Child_->RHSComponentCache,
  395. Child_->ArrayCache, Child_->FunctionCache),
  396. Quals(Quals_), Child(Child_) {}
  397. template<typename Fn> void match(Fn F) const { F(Child, Quals); }
  398. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  399. return Child->hasRHSComponent(OB);
  400. }
  401. bool hasArraySlow(OutputBuffer &OB) const override {
  402. return Child->hasArray(OB);
  403. }
  404. bool hasFunctionSlow(OutputBuffer &OB) const override {
  405. return Child->hasFunction(OB);
  406. }
  407. void printLeft(OutputBuffer &OB) const override {
  408. Child->printLeft(OB);
  409. printQuals(OB);
  410. }
  411. void printRight(OutputBuffer &OB) const override { Child->printRight(OB); }
  412. };
  413. class ConversionOperatorType final : public Node {
  414. const Node *Ty;
  415. public:
  416. ConversionOperatorType(const Node *Ty_)
  417. : Node(KConversionOperatorType), Ty(Ty_) {}
  418. template<typename Fn> void match(Fn F) const { F(Ty); }
  419. void printLeft(OutputBuffer &OB) const override {
  420. OB += "operator ";
  421. Ty->print(OB);
  422. }
  423. };
  424. class PostfixQualifiedType final : public Node {
  425. const Node *Ty;
  426. const StringView Postfix;
  427. public:
  428. PostfixQualifiedType(Node *Ty_, StringView Postfix_)
  429. : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {}
  430. template<typename Fn> void match(Fn F) const { F(Ty, Postfix); }
  431. void printLeft(OutputBuffer &OB) const override {
  432. Ty->printLeft(OB);
  433. OB += Postfix;
  434. }
  435. };
  436. class NameType final : public Node {
  437. const StringView Name;
  438. public:
  439. NameType(StringView Name_) : Node(KNameType), Name(Name_) {}
  440. template<typename Fn> void match(Fn F) const { F(Name); }
  441. StringView getName() const { return Name; }
  442. StringView getBaseName() const override { return Name; }
  443. void printLeft(OutputBuffer &OB) const override { OB += Name; }
  444. };
  445. class ElaboratedTypeSpefType : public Node {
  446. StringView Kind;
  447. Node *Child;
  448. public:
  449. ElaboratedTypeSpefType(StringView Kind_, Node *Child_)
  450. : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {}
  451. template<typename Fn> void match(Fn F) const { F(Kind, Child); }
  452. void printLeft(OutputBuffer &OB) const override {
  453. OB += Kind;
  454. OB += ' ';
  455. Child->print(OB);
  456. }
  457. };
  458. struct AbiTagAttr : Node {
  459. Node *Base;
  460. StringView Tag;
  461. AbiTagAttr(Node* Base_, StringView Tag_)
  462. : Node(KAbiTagAttr, Base_->RHSComponentCache,
  463. Base_->ArrayCache, Base_->FunctionCache),
  464. Base(Base_), Tag(Tag_) {}
  465. template<typename Fn> void match(Fn F) const { F(Base, Tag); }
  466. void printLeft(OutputBuffer &OB) const override {
  467. Base->printLeft(OB);
  468. OB += "[abi:";
  469. OB += Tag;
  470. OB += "]";
  471. }
  472. };
  473. class EnableIfAttr : public Node {
  474. NodeArray Conditions;
  475. public:
  476. EnableIfAttr(NodeArray Conditions_)
  477. : Node(KEnableIfAttr), Conditions(Conditions_) {}
  478. template<typename Fn> void match(Fn F) const { F(Conditions); }
  479. void printLeft(OutputBuffer &OB) const override {
  480. OB += " [enable_if:";
  481. Conditions.printWithComma(OB);
  482. OB += ']';
  483. }
  484. };
  485. class ObjCProtoName : public Node {
  486. const Node *Ty;
  487. StringView Protocol;
  488. friend class PointerType;
  489. public:
  490. ObjCProtoName(const Node *Ty_, StringView Protocol_)
  491. : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {}
  492. template<typename Fn> void match(Fn F) const { F(Ty, Protocol); }
  493. bool isObjCObject() const {
  494. return Ty->getKind() == KNameType &&
  495. static_cast<const NameType *>(Ty)->getName() == "objc_object";
  496. }
  497. void printLeft(OutputBuffer &OB) const override {
  498. Ty->print(OB);
  499. OB += "<";
  500. OB += Protocol;
  501. OB += ">";
  502. }
  503. };
  504. class PointerType final : public Node {
  505. const Node *Pointee;
  506. public:
  507. PointerType(const Node *Pointee_)
  508. : Node(KPointerType, Pointee_->RHSComponentCache),
  509. Pointee(Pointee_) {}
  510. template<typename Fn> void match(Fn F) const { F(Pointee); }
  511. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  512. return Pointee->hasRHSComponent(OB);
  513. }
  514. void printLeft(OutputBuffer &OB) const override {
  515. // We rewrite objc_object<SomeProtocol>* into id<SomeProtocol>.
  516. if (Pointee->getKind() != KObjCProtoName ||
  517. !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
  518. Pointee->printLeft(OB);
  519. if (Pointee->hasArray(OB))
  520. OB += " ";
  521. if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
  522. OB += "(";
  523. OB += "*";
  524. } else {
  525. const auto *objcProto = static_cast<const ObjCProtoName *>(Pointee);
  526. OB += "id<";
  527. OB += objcProto->Protocol;
  528. OB += ">";
  529. }
  530. }
  531. void printRight(OutputBuffer &OB) const override {
  532. if (Pointee->getKind() != KObjCProtoName ||
  533. !static_cast<const ObjCProtoName *>(Pointee)->isObjCObject()) {
  534. if (Pointee->hasArray(OB) || Pointee->hasFunction(OB))
  535. OB += ")";
  536. Pointee->printRight(OB);
  537. }
  538. }
  539. };
  540. enum class ReferenceKind {
  541. LValue,
  542. RValue,
  543. };
  544. // Represents either a LValue or an RValue reference type.
  545. class ReferenceType : public Node {
  546. const Node *Pointee;
  547. ReferenceKind RK;
  548. mutable bool Printing = false;
  549. // Dig through any refs to refs, collapsing the ReferenceTypes as we go. The
  550. // rule here is rvalue ref to rvalue ref collapses to a rvalue ref, and any
  551. // other combination collapses to a lvalue ref.
  552. //
  553. // A combination of a TemplateForwardReference and a back-ref Substitution
  554. // from an ill-formed string may have created a cycle; use cycle detection to
  555. // avoid looping forever.
  556. std::pair<ReferenceKind, const Node *> collapse(OutputBuffer &OB) const {
  557. auto SoFar = std::make_pair(RK, Pointee);
  558. // Track the chain of nodes for the Floyd's 'tortoise and hare'
  559. // cycle-detection algorithm, since getSyntaxNode(S) is impure
  560. PODSmallVector<const Node *, 8> Prev;
  561. for (;;) {
  562. const Node *SN = SoFar.second->getSyntaxNode(OB);
  563. if (SN->getKind() != KReferenceType)
  564. break;
  565. auto *RT = static_cast<const ReferenceType *>(SN);
  566. SoFar.second = RT->Pointee;
  567. SoFar.first = std::min(SoFar.first, RT->RK);
  568. // The middle of Prev is the 'slow' pointer moving at half speed
  569. Prev.push_back(SoFar.second);
  570. if (Prev.size() > 1 && SoFar.second == Prev[(Prev.size() - 1) / 2]) {
  571. // Cycle detected
  572. SoFar.second = nullptr;
  573. break;
  574. }
  575. }
  576. return SoFar;
  577. }
  578. public:
  579. ReferenceType(const Node *Pointee_, ReferenceKind RK_)
  580. : Node(KReferenceType, Pointee_->RHSComponentCache),
  581. Pointee(Pointee_), RK(RK_) {}
  582. template<typename Fn> void match(Fn F) const { F(Pointee, RK); }
  583. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  584. return Pointee->hasRHSComponent(OB);
  585. }
  586. void printLeft(OutputBuffer &OB) const override {
  587. if (Printing)
  588. return;
  589. SwapAndRestore<bool> SavePrinting(Printing, true);
  590. std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
  591. if (!Collapsed.second)
  592. return;
  593. Collapsed.second->printLeft(OB);
  594. if (Collapsed.second->hasArray(OB))
  595. OB += " ";
  596. if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
  597. OB += "(";
  598. OB += (Collapsed.first == ReferenceKind::LValue ? "&" : "&&");
  599. }
  600. void printRight(OutputBuffer &OB) const override {
  601. if (Printing)
  602. return;
  603. SwapAndRestore<bool> SavePrinting(Printing, true);
  604. std::pair<ReferenceKind, const Node *> Collapsed = collapse(OB);
  605. if (!Collapsed.second)
  606. return;
  607. if (Collapsed.second->hasArray(OB) || Collapsed.second->hasFunction(OB))
  608. OB += ")";
  609. Collapsed.second->printRight(OB);
  610. }
  611. };
  612. class PointerToMemberType final : public Node {
  613. const Node *ClassType;
  614. const Node *MemberType;
  615. public:
  616. PointerToMemberType(const Node *ClassType_, const Node *MemberType_)
  617. : Node(KPointerToMemberType, MemberType_->RHSComponentCache),
  618. ClassType(ClassType_), MemberType(MemberType_) {}
  619. template<typename Fn> void match(Fn F) const { F(ClassType, MemberType); }
  620. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  621. return MemberType->hasRHSComponent(OB);
  622. }
  623. void printLeft(OutputBuffer &OB) const override {
  624. MemberType->printLeft(OB);
  625. if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
  626. OB += "(";
  627. else
  628. OB += " ";
  629. ClassType->print(OB);
  630. OB += "::*";
  631. }
  632. void printRight(OutputBuffer &OB) const override {
  633. if (MemberType->hasArray(OB) || MemberType->hasFunction(OB))
  634. OB += ")";
  635. MemberType->printRight(OB);
  636. }
  637. };
  638. class ArrayType final : public Node {
  639. const Node *Base;
  640. Node *Dimension;
  641. public:
  642. ArrayType(const Node *Base_, Node *Dimension_)
  643. : Node(KArrayType,
  644. /*RHSComponentCache=*/Cache::Yes,
  645. /*ArrayCache=*/Cache::Yes),
  646. Base(Base_), Dimension(Dimension_) {}
  647. template<typename Fn> void match(Fn F) const { F(Base, Dimension); }
  648. bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
  649. bool hasArraySlow(OutputBuffer &) const override { return true; }
  650. void printLeft(OutputBuffer &OB) const override { Base->printLeft(OB); }
  651. void printRight(OutputBuffer &OB) const override {
  652. if (OB.back() != ']')
  653. OB += " ";
  654. OB += "[";
  655. if (Dimension)
  656. Dimension->print(OB);
  657. OB += "]";
  658. Base->printRight(OB);
  659. }
  660. };
  661. class FunctionType final : public Node {
  662. const Node *Ret;
  663. NodeArray Params;
  664. Qualifiers CVQuals;
  665. FunctionRefQual RefQual;
  666. const Node *ExceptionSpec;
  667. public:
  668. FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_,
  669. FunctionRefQual RefQual_, const Node *ExceptionSpec_)
  670. : Node(KFunctionType,
  671. /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
  672. /*FunctionCache=*/Cache::Yes),
  673. Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_),
  674. ExceptionSpec(ExceptionSpec_) {}
  675. template<typename Fn> void match(Fn F) const {
  676. F(Ret, Params, CVQuals, RefQual, ExceptionSpec);
  677. }
  678. bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
  679. bool hasFunctionSlow(OutputBuffer &) const override { return true; }
  680. // Handle C++'s ... quirky decl grammar by using the left & right
  681. // distinction. Consider:
  682. // int (*f(float))(char) {}
  683. // f is a function that takes a float and returns a pointer to a function
  684. // that takes a char and returns an int. If we're trying to print f, start
  685. // by printing out the return types's left, then print our parameters, then
  686. // finally print right of the return type.
  687. void printLeft(OutputBuffer &OB) const override {
  688. Ret->printLeft(OB);
  689. OB += " ";
  690. }
  691. void printRight(OutputBuffer &OB) const override {
  692. OB += "(";
  693. Params.printWithComma(OB);
  694. OB += ")";
  695. Ret->printRight(OB);
  696. if (CVQuals & QualConst)
  697. OB += " const";
  698. if (CVQuals & QualVolatile)
  699. OB += " volatile";
  700. if (CVQuals & QualRestrict)
  701. OB += " restrict";
  702. if (RefQual == FrefQualLValue)
  703. OB += " &";
  704. else if (RefQual == FrefQualRValue)
  705. OB += " &&";
  706. if (ExceptionSpec != nullptr) {
  707. OB += ' ';
  708. ExceptionSpec->print(OB);
  709. }
  710. }
  711. };
  712. class NoexceptSpec : public Node {
  713. const Node *E;
  714. public:
  715. NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {}
  716. template<typename Fn> void match(Fn F) const { F(E); }
  717. void printLeft(OutputBuffer &OB) const override {
  718. OB += "noexcept(";
  719. E->print(OB);
  720. OB += ")";
  721. }
  722. };
  723. class DynamicExceptionSpec : public Node {
  724. NodeArray Types;
  725. public:
  726. DynamicExceptionSpec(NodeArray Types_)
  727. : Node(KDynamicExceptionSpec), Types(Types_) {}
  728. template<typename Fn> void match(Fn F) const { F(Types); }
  729. void printLeft(OutputBuffer &OB) const override {
  730. OB += "throw(";
  731. Types.printWithComma(OB);
  732. OB += ')';
  733. }
  734. };
  735. class FunctionEncoding final : public Node {
  736. const Node *Ret;
  737. const Node *Name;
  738. NodeArray Params;
  739. const Node *Attrs;
  740. Qualifiers CVQuals;
  741. FunctionRefQual RefQual;
  742. public:
  743. FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_,
  744. const Node *Attrs_, Qualifiers CVQuals_,
  745. FunctionRefQual RefQual_)
  746. : Node(KFunctionEncoding,
  747. /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No,
  748. /*FunctionCache=*/Cache::Yes),
  749. Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
  750. CVQuals(CVQuals_), RefQual(RefQual_) {}
  751. template<typename Fn> void match(Fn F) const {
  752. F(Ret, Name, Params, Attrs, CVQuals, RefQual);
  753. }
  754. Qualifiers getCVQuals() const { return CVQuals; }
  755. FunctionRefQual getRefQual() const { return RefQual; }
  756. NodeArray getParams() const { return Params; }
  757. const Node *getReturnType() const { return Ret; }
  758. bool hasRHSComponentSlow(OutputBuffer &) const override { return true; }
  759. bool hasFunctionSlow(OutputBuffer &) const override { return true; }
  760. const Node *getName() const { return Name; }
  761. void printLeft(OutputBuffer &OB) const override {
  762. if (Ret) {
  763. Ret->printLeft(OB);
  764. if (!Ret->hasRHSComponent(OB))
  765. OB += " ";
  766. }
  767. Name->print(OB);
  768. }
  769. void printRight(OutputBuffer &OB) const override {
  770. OB += "(";
  771. Params.printWithComma(OB);
  772. OB += ")";
  773. if (Ret)
  774. Ret->printRight(OB);
  775. if (CVQuals & QualConst)
  776. OB += " const";
  777. if (CVQuals & QualVolatile)
  778. OB += " volatile";
  779. if (CVQuals & QualRestrict)
  780. OB += " restrict";
  781. if (RefQual == FrefQualLValue)
  782. OB += " &";
  783. else if (RefQual == FrefQualRValue)
  784. OB += " &&";
  785. if (Attrs != nullptr)
  786. Attrs->print(OB);
  787. }
  788. };
  789. class LiteralOperator : public Node {
  790. const Node *OpName;
  791. public:
  792. LiteralOperator(const Node *OpName_)
  793. : Node(KLiteralOperator), OpName(OpName_) {}
  794. template<typename Fn> void match(Fn F) const { F(OpName); }
  795. void printLeft(OutputBuffer &OB) const override {
  796. OB += "operator\"\" ";
  797. OpName->print(OB);
  798. }
  799. };
  800. class SpecialName final : public Node {
  801. const StringView Special;
  802. const Node *Child;
  803. public:
  804. SpecialName(StringView Special_, const Node *Child_)
  805. : Node(KSpecialName), Special(Special_), Child(Child_) {}
  806. template<typename Fn> void match(Fn F) const { F(Special, Child); }
  807. void printLeft(OutputBuffer &OB) const override {
  808. OB += Special;
  809. Child->print(OB);
  810. }
  811. };
  812. class CtorVtableSpecialName final : public Node {
  813. const Node *FirstType;
  814. const Node *SecondType;
  815. public:
  816. CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_)
  817. : Node(KCtorVtableSpecialName),
  818. FirstType(FirstType_), SecondType(SecondType_) {}
  819. template<typename Fn> void match(Fn F) const { F(FirstType, SecondType); }
  820. void printLeft(OutputBuffer &OB) const override {
  821. OB += "construction vtable for ";
  822. FirstType->print(OB);
  823. OB += "-in-";
  824. SecondType->print(OB);
  825. }
  826. };
  827. struct NestedName : Node {
  828. Node *Qual;
  829. Node *Name;
  830. NestedName(Node *Qual_, Node *Name_)
  831. : Node(KNestedName), Qual(Qual_), Name(Name_) {}
  832. template<typename Fn> void match(Fn F) const { F(Qual, Name); }
  833. StringView getBaseName() const override { return Name->getBaseName(); }
  834. void printLeft(OutputBuffer &OB) const override {
  835. Qual->print(OB);
  836. OB += "::";
  837. Name->print(OB);
  838. }
  839. };
  840. struct LocalName : Node {
  841. Node *Encoding;
  842. Node *Entity;
  843. LocalName(Node *Encoding_, Node *Entity_)
  844. : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
  845. template<typename Fn> void match(Fn F) const { F(Encoding, Entity); }
  846. void printLeft(OutputBuffer &OB) const override {
  847. Encoding->print(OB);
  848. OB += "::";
  849. Entity->print(OB);
  850. }
  851. };
  852. class QualifiedName final : public Node {
  853. // qualifier::name
  854. const Node *Qualifier;
  855. const Node *Name;
  856. public:
  857. QualifiedName(const Node *Qualifier_, const Node *Name_)
  858. : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {}
  859. template<typename Fn> void match(Fn F) const { F(Qualifier, Name); }
  860. StringView getBaseName() const override { return Name->getBaseName(); }
  861. void printLeft(OutputBuffer &OB) const override {
  862. Qualifier->print(OB);
  863. OB += "::";
  864. Name->print(OB);
  865. }
  866. };
  867. class VectorType final : public Node {
  868. const Node *BaseType;
  869. const Node *Dimension;
  870. public:
  871. VectorType(const Node *BaseType_, Node *Dimension_)
  872. : Node(KVectorType), BaseType(BaseType_),
  873. Dimension(Dimension_) {}
  874. template<typename Fn> void match(Fn F) const { F(BaseType, Dimension); }
  875. void printLeft(OutputBuffer &OB) const override {
  876. BaseType->print(OB);
  877. OB += " vector[";
  878. if (Dimension)
  879. Dimension->print(OB);
  880. OB += "]";
  881. }
  882. };
  883. class PixelVectorType final : public Node {
  884. const Node *Dimension;
  885. public:
  886. PixelVectorType(const Node *Dimension_)
  887. : Node(KPixelVectorType), Dimension(Dimension_) {}
  888. template<typename Fn> void match(Fn F) const { F(Dimension); }
  889. void printLeft(OutputBuffer &OB) const override {
  890. // FIXME: This should demangle as "vector pixel".
  891. OB += "pixel vector[";
  892. Dimension->print(OB);
  893. OB += "]";
  894. }
  895. };
  896. class BinaryFPType final : public Node {
  897. const Node *Dimension;
  898. public:
  899. BinaryFPType(const Node *Dimension_)
  900. : Node(KBinaryFPType), Dimension(Dimension_) {}
  901. template<typename Fn> void match(Fn F) const { F(Dimension); }
  902. void printLeft(OutputBuffer &OB) const override {
  903. OB += "_Float";
  904. Dimension->print(OB);
  905. }
  906. };
  907. enum class TemplateParamKind { Type, NonType, Template };
  908. /// An invented name for a template parameter for which we don't have a
  909. /// corresponding template argument.
  910. ///
  911. /// This node is created when parsing the <lambda-sig> for a lambda with
  912. /// explicit template arguments, which might be referenced in the parameter
  913. /// types appearing later in the <lambda-sig>.
  914. class SyntheticTemplateParamName final : public Node {
  915. TemplateParamKind Kind;
  916. unsigned Index;
  917. public:
  918. SyntheticTemplateParamName(TemplateParamKind Kind_, unsigned Index_)
  919. : Node(KSyntheticTemplateParamName), Kind(Kind_), Index(Index_) {}
  920. template<typename Fn> void match(Fn F) const { F(Kind, Index); }
  921. void printLeft(OutputBuffer &OB) const override {
  922. switch (Kind) {
  923. case TemplateParamKind::Type:
  924. OB += "$T";
  925. break;
  926. case TemplateParamKind::NonType:
  927. OB += "$N";
  928. break;
  929. case TemplateParamKind::Template:
  930. OB += "$TT";
  931. break;
  932. }
  933. if (Index > 0)
  934. OB << Index - 1;
  935. }
  936. };
  937. /// A template type parameter declaration, 'typename T'.
  938. class TypeTemplateParamDecl final : public Node {
  939. Node *Name;
  940. public:
  941. TypeTemplateParamDecl(Node *Name_)
  942. : Node(KTypeTemplateParamDecl, Cache::Yes), Name(Name_) {}
  943. template<typename Fn> void match(Fn F) const { F(Name); }
  944. void printLeft(OutputBuffer &OB) const override { OB += "typename "; }
  945. void printRight(OutputBuffer &OB) const override { Name->print(OB); }
  946. };
  947. /// A non-type template parameter declaration, 'int N'.
  948. class NonTypeTemplateParamDecl final : public Node {
  949. Node *Name;
  950. Node *Type;
  951. public:
  952. NonTypeTemplateParamDecl(Node *Name_, Node *Type_)
  953. : Node(KNonTypeTemplateParamDecl, Cache::Yes), Name(Name_), Type(Type_) {}
  954. template<typename Fn> void match(Fn F) const { F(Name, Type); }
  955. void printLeft(OutputBuffer &OB) const override {
  956. Type->printLeft(OB);
  957. if (!Type->hasRHSComponent(OB))
  958. OB += " ";
  959. }
  960. void printRight(OutputBuffer &OB) const override {
  961. Name->print(OB);
  962. Type->printRight(OB);
  963. }
  964. };
  965. /// A template template parameter declaration,
  966. /// 'template<typename T> typename N'.
  967. class TemplateTemplateParamDecl final : public Node {
  968. Node *Name;
  969. NodeArray Params;
  970. public:
  971. TemplateTemplateParamDecl(Node *Name_, NodeArray Params_)
  972. : Node(KTemplateTemplateParamDecl, Cache::Yes), Name(Name_),
  973. Params(Params_) {}
  974. template<typename Fn> void match(Fn F) const { F(Name, Params); }
  975. void printLeft(OutputBuffer &OB) const override {
  976. OB += "template<";
  977. Params.printWithComma(OB);
  978. OB += "> typename ";
  979. }
  980. void printRight(OutputBuffer &OB) const override { Name->print(OB); }
  981. };
  982. /// A template parameter pack declaration, 'typename ...T'.
  983. class TemplateParamPackDecl final : public Node {
  984. Node *Param;
  985. public:
  986. TemplateParamPackDecl(Node *Param_)
  987. : Node(KTemplateParamPackDecl, Cache::Yes), Param(Param_) {}
  988. template<typename Fn> void match(Fn F) const { F(Param); }
  989. void printLeft(OutputBuffer &OB) const override {
  990. Param->printLeft(OB);
  991. OB += "...";
  992. }
  993. void printRight(OutputBuffer &OB) const override { Param->printRight(OB); }
  994. };
  995. /// An unexpanded parameter pack (either in the expression or type context). If
  996. /// this AST is correct, this node will have a ParameterPackExpansion node above
  997. /// it.
  998. ///
  999. /// This node is created when some <template-args> are found that apply to an
  1000. /// <encoding>, and is stored in the TemplateParams table. In order for this to
  1001. /// appear in the final AST, it has to referenced via a <template-param> (ie,
  1002. /// T_).
  1003. class ParameterPack final : public Node {
  1004. NodeArray Data;
  1005. // Setup OutputBuffer for a pack expansion, unless we're already expanding
  1006. // one.
  1007. void initializePackExpansion(OutputBuffer &OB) const {
  1008. if (OB.CurrentPackMax == std::numeric_limits<unsigned>::max()) {
  1009. OB.CurrentPackMax = static_cast<unsigned>(Data.size());
  1010. OB.CurrentPackIndex = 0;
  1011. }
  1012. }
  1013. public:
  1014. ParameterPack(NodeArray Data_) : Node(KParameterPack), Data(Data_) {
  1015. ArrayCache = FunctionCache = RHSComponentCache = Cache::Unknown;
  1016. if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
  1017. return P->ArrayCache == Cache::No;
  1018. }))
  1019. ArrayCache = Cache::No;
  1020. if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
  1021. return P->FunctionCache == Cache::No;
  1022. }))
  1023. FunctionCache = Cache::No;
  1024. if (std::all_of(Data.begin(), Data.end(), [](Node* P) {
  1025. return P->RHSComponentCache == Cache::No;
  1026. }))
  1027. RHSComponentCache = Cache::No;
  1028. }
  1029. template<typename Fn> void match(Fn F) const { F(Data); }
  1030. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  1031. initializePackExpansion(OB);
  1032. size_t Idx = OB.CurrentPackIndex;
  1033. return Idx < Data.size() && Data[Idx]->hasRHSComponent(OB);
  1034. }
  1035. bool hasArraySlow(OutputBuffer &OB) const override {
  1036. initializePackExpansion(OB);
  1037. size_t Idx = OB.CurrentPackIndex;
  1038. return Idx < Data.size() && Data[Idx]->hasArray(OB);
  1039. }
  1040. bool hasFunctionSlow(OutputBuffer &OB) const override {
  1041. initializePackExpansion(OB);
  1042. size_t Idx = OB.CurrentPackIndex;
  1043. return Idx < Data.size() && Data[Idx]->hasFunction(OB);
  1044. }
  1045. const Node *getSyntaxNode(OutputBuffer &OB) const override {
  1046. initializePackExpansion(OB);
  1047. size_t Idx = OB.CurrentPackIndex;
  1048. return Idx < Data.size() ? Data[Idx]->getSyntaxNode(OB) : this;
  1049. }
  1050. void printLeft(OutputBuffer &OB) const override {
  1051. initializePackExpansion(OB);
  1052. size_t Idx = OB.CurrentPackIndex;
  1053. if (Idx < Data.size())
  1054. Data[Idx]->printLeft(OB);
  1055. }
  1056. void printRight(OutputBuffer &OB) const override {
  1057. initializePackExpansion(OB);
  1058. size_t Idx = OB.CurrentPackIndex;
  1059. if (Idx < Data.size())
  1060. Data[Idx]->printRight(OB);
  1061. }
  1062. };
  1063. /// A variadic template argument. This node represents an occurrence of
  1064. /// J<something>E in some <template-args>. It isn't itself unexpanded, unless
  1065. /// one of it's Elements is. The parser inserts a ParameterPack into the
  1066. /// TemplateParams table if the <template-args> this pack belongs to apply to an
  1067. /// <encoding>.
  1068. class TemplateArgumentPack final : public Node {
  1069. NodeArray Elements;
  1070. public:
  1071. TemplateArgumentPack(NodeArray Elements_)
  1072. : Node(KTemplateArgumentPack), Elements(Elements_) {}
  1073. template<typename Fn> void match(Fn F) const { F(Elements); }
  1074. NodeArray getElements() const { return Elements; }
  1075. void printLeft(OutputBuffer &OB) const override {
  1076. Elements.printWithComma(OB);
  1077. }
  1078. };
  1079. /// A pack expansion. Below this node, there are some unexpanded ParameterPacks
  1080. /// which each have Child->ParameterPackSize elements.
  1081. class ParameterPackExpansion final : public Node {
  1082. const Node *Child;
  1083. public:
  1084. ParameterPackExpansion(const Node *Child_)
  1085. : Node(KParameterPackExpansion), Child(Child_) {}
  1086. template<typename Fn> void match(Fn F) const { F(Child); }
  1087. const Node *getChild() const { return Child; }
  1088. void printLeft(OutputBuffer &OB) const override {
  1089. constexpr unsigned Max = std::numeric_limits<unsigned>::max();
  1090. SwapAndRestore<unsigned> SavePackIdx(OB.CurrentPackIndex, Max);
  1091. SwapAndRestore<unsigned> SavePackMax(OB.CurrentPackMax, Max);
  1092. size_t StreamPos = OB.getCurrentPosition();
  1093. // Print the first element in the pack. If Child contains a ParameterPack,
  1094. // it will set up S.CurrentPackMax and print the first element.
  1095. Child->print(OB);
  1096. // No ParameterPack was found in Child. This can occur if we've found a pack
  1097. // expansion on a <function-param>.
  1098. if (OB.CurrentPackMax == Max) {
  1099. OB += "...";
  1100. return;
  1101. }
  1102. // We found a ParameterPack, but it has no elements. Erase whatever we may
  1103. // of printed.
  1104. if (OB.CurrentPackMax == 0) {
  1105. OB.setCurrentPosition(StreamPos);
  1106. return;
  1107. }
  1108. // Else, iterate through the rest of the elements in the pack.
  1109. for (unsigned I = 1, E = OB.CurrentPackMax; I < E; ++I) {
  1110. OB += ", ";
  1111. OB.CurrentPackIndex = I;
  1112. Child->print(OB);
  1113. }
  1114. }
  1115. };
  1116. class TemplateArgs final : public Node {
  1117. NodeArray Params;
  1118. public:
  1119. TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {}
  1120. template<typename Fn> void match(Fn F) const { F(Params); }
  1121. NodeArray getParams() { return Params; }
  1122. void printLeft(OutputBuffer &OB) const override {
  1123. OB += "<";
  1124. Params.printWithComma(OB);
  1125. if (OB.back() == '>')
  1126. OB += " ";
  1127. OB += ">";
  1128. }
  1129. };
  1130. /// A forward-reference to a template argument that was not known at the point
  1131. /// where the template parameter name was parsed in a mangling.
  1132. ///
  1133. /// This is created when demangling the name of a specialization of a
  1134. /// conversion function template:
  1135. ///
  1136. /// \code
  1137. /// struct A {
  1138. /// template<typename T> operator T*();
  1139. /// };
  1140. /// \endcode
  1141. ///
  1142. /// When demangling a specialization of the conversion function template, we
  1143. /// encounter the name of the template (including the \c T) before we reach
  1144. /// the template argument list, so we cannot substitute the parameter name
  1145. /// for the corresponding argument while parsing. Instead, we create a
  1146. /// \c ForwardTemplateReference node that is resolved after we parse the
  1147. /// template arguments.
  1148. struct ForwardTemplateReference : Node {
  1149. size_t Index;
  1150. Node *Ref = nullptr;
  1151. // If we're currently printing this node. It is possible (though invalid) for
  1152. // a forward template reference to refer to itself via a substitution. This
  1153. // creates a cyclic AST, which will stack overflow printing. To fix this, bail
  1154. // out if more than one print* function is active.
  1155. mutable bool Printing = false;
  1156. ForwardTemplateReference(size_t Index_)
  1157. : Node(KForwardTemplateReference, Cache::Unknown, Cache::Unknown,
  1158. Cache::Unknown),
  1159. Index(Index_) {}
  1160. // We don't provide a matcher for these, because the value of the node is
  1161. // not determined by its construction parameters, and it generally needs
  1162. // special handling.
  1163. template<typename Fn> void match(Fn F) const = delete;
  1164. bool hasRHSComponentSlow(OutputBuffer &OB) const override {
  1165. if (Printing)
  1166. return false;
  1167. SwapAndRestore<bool> SavePrinting(Printing, true);
  1168. return Ref->hasRHSComponent(OB);
  1169. }
  1170. bool hasArraySlow(OutputBuffer &OB) const override {
  1171. if (Printing)
  1172. return false;
  1173. SwapAndRestore<bool> SavePrinting(Printing, true);
  1174. return Ref->hasArray(OB);
  1175. }
  1176. bool hasFunctionSlow(OutputBuffer &OB) const override {
  1177. if (Printing)
  1178. return false;
  1179. SwapAndRestore<bool> SavePrinting(Printing, true);
  1180. return Ref->hasFunction(OB);
  1181. }
  1182. const Node *getSyntaxNode(OutputBuffer &OB) const override {
  1183. if (Printing)
  1184. return this;
  1185. SwapAndRestore<bool> SavePrinting(Printing, true);
  1186. return Ref->getSyntaxNode(OB);
  1187. }
  1188. void printLeft(OutputBuffer &OB) const override {
  1189. if (Printing)
  1190. return;
  1191. SwapAndRestore<bool> SavePrinting(Printing, true);
  1192. Ref->printLeft(OB);
  1193. }
  1194. void printRight(OutputBuffer &OB) const override {
  1195. if (Printing)
  1196. return;
  1197. SwapAndRestore<bool> SavePrinting(Printing, true);
  1198. Ref->printRight(OB);
  1199. }
  1200. };
  1201. struct NameWithTemplateArgs : Node {
  1202. // name<template_args>
  1203. Node *Name;
  1204. Node *TemplateArgs;
  1205. NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
  1206. : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
  1207. template<typename Fn> void match(Fn F) const { F(Name, TemplateArgs); }
  1208. StringView getBaseName() const override { return Name->getBaseName(); }
  1209. void printLeft(OutputBuffer &OB) const override {
  1210. Name->print(OB);
  1211. TemplateArgs->print(OB);
  1212. }
  1213. };
  1214. class GlobalQualifiedName final : public Node {
  1215. Node *Child;
  1216. public:
  1217. GlobalQualifiedName(Node* Child_)
  1218. : Node(KGlobalQualifiedName), Child(Child_) {}
  1219. template<typename Fn> void match(Fn F) const { F(Child); }
  1220. StringView getBaseName() const override { return Child->getBaseName(); }
  1221. void printLeft(OutputBuffer &OB) const override {
  1222. OB += "::";
  1223. Child->print(OB);
  1224. }
  1225. };
  1226. struct StdQualifiedName : Node {
  1227. Node *Child;
  1228. StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
  1229. template<typename Fn> void match(Fn F) const { F(Child); }
  1230. StringView getBaseName() const override { return Child->getBaseName(); }
  1231. void printLeft(OutputBuffer &OB) const override {
  1232. OB += "std::";
  1233. Child->print(OB);
  1234. }
  1235. };
  1236. enum class SpecialSubKind {
  1237. allocator,
  1238. basic_string,
  1239. string,
  1240. istream,
  1241. ostream,
  1242. iostream,
  1243. };
  1244. class ExpandedSpecialSubstitution final : public Node {
  1245. SpecialSubKind SSK;
  1246. public:
  1247. ExpandedSpecialSubstitution(SpecialSubKind SSK_)
  1248. : Node(KExpandedSpecialSubstitution), SSK(SSK_) {}
  1249. template<typename Fn> void match(Fn F) const { F(SSK); }
  1250. StringView getBaseName() const override {
  1251. switch (SSK) {
  1252. case SpecialSubKind::allocator:
  1253. return StringView("allocator");
  1254. case SpecialSubKind::basic_string:
  1255. return StringView("basic_string");
  1256. case SpecialSubKind::string:
  1257. return StringView("basic_string");
  1258. case SpecialSubKind::istream:
  1259. return StringView("basic_istream");
  1260. case SpecialSubKind::ostream:
  1261. return StringView("basic_ostream");
  1262. case SpecialSubKind::iostream:
  1263. return StringView("basic_iostream");
  1264. }
  1265. DEMANGLE_UNREACHABLE;
  1266. }
  1267. void printLeft(OutputBuffer &OB) const override {
  1268. switch (SSK) {
  1269. case SpecialSubKind::allocator:
  1270. OB += "std::allocator";
  1271. break;
  1272. case SpecialSubKind::basic_string:
  1273. OB += "std::basic_string";
  1274. break;
  1275. case SpecialSubKind::string:
  1276. OB += "std::basic_string<char, std::char_traits<char>, "
  1277. "std::allocator<char> >";
  1278. break;
  1279. case SpecialSubKind::istream:
  1280. OB += "std::basic_istream<char, std::char_traits<char> >";
  1281. break;
  1282. case SpecialSubKind::ostream:
  1283. OB += "std::basic_ostream<char, std::char_traits<char> >";
  1284. break;
  1285. case SpecialSubKind::iostream:
  1286. OB += "std::basic_iostream<char, std::char_traits<char> >";
  1287. break;
  1288. }
  1289. }
  1290. };
  1291. class SpecialSubstitution final : public Node {
  1292. public:
  1293. SpecialSubKind SSK;
  1294. SpecialSubstitution(SpecialSubKind SSK_)
  1295. : Node(KSpecialSubstitution), SSK(SSK_) {}
  1296. template<typename Fn> void match(Fn F) const { F(SSK); }
  1297. StringView getBaseName() const override {
  1298. switch (SSK) {
  1299. case SpecialSubKind::allocator:
  1300. return StringView("allocator");
  1301. case SpecialSubKind::basic_string:
  1302. return StringView("basic_string");
  1303. case SpecialSubKind::string:
  1304. return StringView("string");
  1305. case SpecialSubKind::istream:
  1306. return StringView("istream");
  1307. case SpecialSubKind::ostream:
  1308. return StringView("ostream");
  1309. case SpecialSubKind::iostream:
  1310. return StringView("iostream");
  1311. }
  1312. DEMANGLE_UNREACHABLE;
  1313. }
  1314. void printLeft(OutputBuffer &OB) const override {
  1315. switch (SSK) {
  1316. case SpecialSubKind::allocator:
  1317. OB += "std::allocator";
  1318. break;
  1319. case SpecialSubKind::basic_string:
  1320. OB += "std::basic_string";
  1321. break;
  1322. case SpecialSubKind::string:
  1323. OB += "std::string";
  1324. break;
  1325. case SpecialSubKind::istream:
  1326. OB += "std::istream";
  1327. break;
  1328. case SpecialSubKind::ostream:
  1329. OB += "std::ostream";
  1330. break;
  1331. case SpecialSubKind::iostream:
  1332. OB += "std::iostream";
  1333. break;
  1334. }
  1335. }
  1336. };
  1337. class CtorDtorName final : public Node {
  1338. const Node *Basename;
  1339. const bool IsDtor;
  1340. const int Variant;
  1341. public:
  1342. CtorDtorName(const Node *Basename_, bool IsDtor_, int Variant_)
  1343. : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_),
  1344. Variant(Variant_) {}
  1345. template<typename Fn> void match(Fn F) const { F(Basename, IsDtor, Variant); }
  1346. void printLeft(OutputBuffer &OB) const override {
  1347. if (IsDtor)
  1348. OB += "~";
  1349. OB += Basename->getBaseName();
  1350. }
  1351. };
  1352. class DtorName : public Node {
  1353. const Node *Base;
  1354. public:
  1355. DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {}
  1356. template<typename Fn> void match(Fn F) const { F(Base); }
  1357. void printLeft(OutputBuffer &OB) const override {
  1358. OB += "~";
  1359. Base->printLeft(OB);
  1360. }
  1361. };
  1362. class UnnamedTypeName : public Node {
  1363. const StringView Count;
  1364. public:
  1365. UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {}
  1366. template<typename Fn> void match(Fn F) const { F(Count); }
  1367. void printLeft(OutputBuffer &OB) const override {
  1368. OB += "'unnamed";
  1369. OB += Count;
  1370. OB += "\'";
  1371. }
  1372. };
  1373. class ClosureTypeName : public Node {
  1374. NodeArray TemplateParams;
  1375. NodeArray Params;
  1376. StringView Count;
  1377. public:
  1378. ClosureTypeName(NodeArray TemplateParams_, NodeArray Params_,
  1379. StringView Count_)
  1380. : Node(KClosureTypeName), TemplateParams(TemplateParams_),
  1381. Params(Params_), Count(Count_) {}
  1382. template<typename Fn> void match(Fn F) const {
  1383. F(TemplateParams, Params, Count);
  1384. }
  1385. void printDeclarator(OutputBuffer &OB) const {
  1386. if (!TemplateParams.empty()) {
  1387. OB += "<";
  1388. TemplateParams.printWithComma(OB);
  1389. OB += ">";
  1390. }
  1391. OB += "(";
  1392. Params.printWithComma(OB);
  1393. OB += ")";
  1394. }
  1395. void printLeft(OutputBuffer &OB) const override {
  1396. OB += "\'lambda";
  1397. OB += Count;
  1398. OB += "\'";
  1399. printDeclarator(OB);
  1400. }
  1401. };
  1402. class StructuredBindingName : public Node {
  1403. NodeArray Bindings;
  1404. public:
  1405. StructuredBindingName(NodeArray Bindings_)
  1406. : Node(KStructuredBindingName), Bindings(Bindings_) {}
  1407. template<typename Fn> void match(Fn F) const { F(Bindings); }
  1408. void printLeft(OutputBuffer &OB) const override {
  1409. OB += '[';
  1410. Bindings.printWithComma(OB);
  1411. OB += ']';
  1412. }
  1413. };
  1414. // -- Expression Nodes --
  1415. class BinaryExpr : public Node {
  1416. const Node *LHS;
  1417. const StringView InfixOperator;
  1418. const Node *RHS;
  1419. public:
  1420. BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_)
  1421. : Node(KBinaryExpr), LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {
  1422. }
  1423. template<typename Fn> void match(Fn F) const { F(LHS, InfixOperator, RHS); }
  1424. void printLeft(OutputBuffer &OB) const override {
  1425. // might be a template argument expression, then we need to disambiguate
  1426. // with parens.
  1427. if (InfixOperator == ">")
  1428. OB += "(";
  1429. OB += "(";
  1430. LHS->print(OB);
  1431. OB += ") ";
  1432. OB += InfixOperator;
  1433. OB += " (";
  1434. RHS->print(OB);
  1435. OB += ")";
  1436. if (InfixOperator == ">")
  1437. OB += ")";
  1438. }
  1439. };
  1440. class ArraySubscriptExpr : public Node {
  1441. const Node *Op1;
  1442. const Node *Op2;
  1443. public:
  1444. ArraySubscriptExpr(const Node *Op1_, const Node *Op2_)
  1445. : Node(KArraySubscriptExpr), Op1(Op1_), Op2(Op2_) {}
  1446. template<typename Fn> void match(Fn F) const { F(Op1, Op2); }
  1447. void printLeft(OutputBuffer &OB) const override {
  1448. OB += "(";
  1449. Op1->print(OB);
  1450. OB += ")[";
  1451. Op2->print(OB);
  1452. OB += "]";
  1453. }
  1454. };
  1455. class PostfixExpr : public Node {
  1456. const Node *Child;
  1457. const StringView Operator;
  1458. public:
  1459. PostfixExpr(const Node *Child_, StringView Operator_)
  1460. : Node(KPostfixExpr), Child(Child_), Operator(Operator_) {}
  1461. template<typename Fn> void match(Fn F) const { F(Child, Operator); }
  1462. void printLeft(OutputBuffer &OB) const override {
  1463. OB += "(";
  1464. Child->print(OB);
  1465. OB += ")";
  1466. OB += Operator;
  1467. }
  1468. };
  1469. class ConditionalExpr : public Node {
  1470. const Node *Cond;
  1471. const Node *Then;
  1472. const Node *Else;
  1473. public:
  1474. ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_)
  1475. : Node(KConditionalExpr), Cond(Cond_), Then(Then_), Else(Else_) {}
  1476. template<typename Fn> void match(Fn F) const { F(Cond, Then, Else); }
  1477. void printLeft(OutputBuffer &OB) const override {
  1478. OB += "(";
  1479. Cond->print(OB);
  1480. OB += ") ? (";
  1481. Then->print(OB);
  1482. OB += ") : (";
  1483. Else->print(OB);
  1484. OB += ")";
  1485. }
  1486. };
  1487. class MemberExpr : public Node {
  1488. const Node *LHS;
  1489. const StringView Kind;
  1490. const Node *RHS;
  1491. public:
  1492. MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_)
  1493. : Node(KMemberExpr), LHS(LHS_), Kind(Kind_), RHS(RHS_) {}
  1494. template<typename Fn> void match(Fn F) const { F(LHS, Kind, RHS); }
  1495. void printLeft(OutputBuffer &OB) const override {
  1496. LHS->print(OB);
  1497. OB += Kind;
  1498. RHS->print(OB);
  1499. }
  1500. };
  1501. class SubobjectExpr : public Node {
  1502. const Node *Type;
  1503. const Node *SubExpr;
  1504. StringView Offset;
  1505. NodeArray UnionSelectors;
  1506. bool OnePastTheEnd;
  1507. public:
  1508. SubobjectExpr(const Node *Type_, const Node *SubExpr_, StringView Offset_,
  1509. NodeArray UnionSelectors_, bool OnePastTheEnd_)
  1510. : Node(KSubobjectExpr), Type(Type_), SubExpr(SubExpr_), Offset(Offset_),
  1511. UnionSelectors(UnionSelectors_), OnePastTheEnd(OnePastTheEnd_) {}
  1512. template<typename Fn> void match(Fn F) const {
  1513. F(Type, SubExpr, Offset, UnionSelectors, OnePastTheEnd);
  1514. }
  1515. void printLeft(OutputBuffer &OB) const override {
  1516. SubExpr->print(OB);
  1517. OB += ".<";
  1518. Type->print(OB);
  1519. OB += " at offset ";
  1520. if (Offset.empty()) {
  1521. OB += "0";
  1522. } else if (Offset[0] == 'n') {
  1523. OB += "-";
  1524. OB += Offset.dropFront();
  1525. } else {
  1526. OB += Offset;
  1527. }
  1528. OB += ">";
  1529. }
  1530. };
  1531. class EnclosingExpr : public Node {
  1532. const StringView Prefix;
  1533. const Node *Infix;
  1534. const StringView Postfix;
  1535. public:
  1536. EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_)
  1537. : Node(KEnclosingExpr), Prefix(Prefix_), Infix(Infix_),
  1538. Postfix(Postfix_) {}
  1539. template<typename Fn> void match(Fn F) const { F(Prefix, Infix, Postfix); }
  1540. void printLeft(OutputBuffer &OB) const override {
  1541. OB += Prefix;
  1542. Infix->print(OB);
  1543. OB += Postfix;
  1544. }
  1545. };
  1546. class CastExpr : public Node {
  1547. // cast_kind<to>(from)
  1548. const StringView CastKind;
  1549. const Node *To;
  1550. const Node *From;
  1551. public:
  1552. CastExpr(StringView CastKind_, const Node *To_, const Node *From_)
  1553. : Node(KCastExpr), CastKind(CastKind_), To(To_), From(From_) {}
  1554. template<typename Fn> void match(Fn F) const { F(CastKind, To, From); }
  1555. void printLeft(OutputBuffer &OB) const override {
  1556. OB += CastKind;
  1557. OB += "<";
  1558. To->printLeft(OB);
  1559. OB += ">(";
  1560. From->printLeft(OB);
  1561. OB += ")";
  1562. }
  1563. };
  1564. class SizeofParamPackExpr : public Node {
  1565. const Node *Pack;
  1566. public:
  1567. SizeofParamPackExpr(const Node *Pack_)
  1568. : Node(KSizeofParamPackExpr), Pack(Pack_) {}
  1569. template<typename Fn> void match(Fn F) const { F(Pack); }
  1570. void printLeft(OutputBuffer &OB) const override {
  1571. OB += "sizeof...(";
  1572. ParameterPackExpansion PPE(Pack);
  1573. PPE.printLeft(OB);
  1574. OB += ")";
  1575. }
  1576. };
  1577. class CallExpr : public Node {
  1578. const Node *Callee;
  1579. NodeArray Args;
  1580. public:
  1581. CallExpr(const Node *Callee_, NodeArray Args_)
  1582. : Node(KCallExpr), Callee(Callee_), Args(Args_) {}
  1583. template<typename Fn> void match(Fn F) const { F(Callee, Args); }
  1584. void printLeft(OutputBuffer &OB) const override {
  1585. Callee->print(OB);
  1586. OB += "(";
  1587. Args.printWithComma(OB);
  1588. OB += ")";
  1589. }
  1590. };
  1591. class NewExpr : public Node {
  1592. // new (expr_list) type(init_list)
  1593. NodeArray ExprList;
  1594. Node *Type;
  1595. NodeArray InitList;
  1596. bool IsGlobal; // ::operator new ?
  1597. bool IsArray; // new[] ?
  1598. public:
  1599. NewExpr(NodeArray ExprList_, Node *Type_, NodeArray InitList_, bool IsGlobal_,
  1600. bool IsArray_)
  1601. : Node(KNewExpr), ExprList(ExprList_), Type(Type_), InitList(InitList_),
  1602. IsGlobal(IsGlobal_), IsArray(IsArray_) {}
  1603. template<typename Fn> void match(Fn F) const {
  1604. F(ExprList, Type, InitList, IsGlobal, IsArray);
  1605. }
  1606. void printLeft(OutputBuffer &OB) const override {
  1607. if (IsGlobal)
  1608. OB += "::operator ";
  1609. OB += "new";
  1610. if (IsArray)
  1611. OB += "[]";
  1612. OB += ' ';
  1613. if (!ExprList.empty()) {
  1614. OB += "(";
  1615. ExprList.printWithComma(OB);
  1616. OB += ")";
  1617. }
  1618. Type->print(OB);
  1619. if (!InitList.empty()) {
  1620. OB += "(";
  1621. InitList.printWithComma(OB);
  1622. OB += ")";
  1623. }
  1624. }
  1625. };
  1626. class DeleteExpr : public Node {
  1627. Node *Op;
  1628. bool IsGlobal;
  1629. bool IsArray;
  1630. public:
  1631. DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_)
  1632. : Node(KDeleteExpr), Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {}
  1633. template<typename Fn> void match(Fn F) const { F(Op, IsGlobal, IsArray); }
  1634. void printLeft(OutputBuffer &OB) const override {
  1635. if (IsGlobal)
  1636. OB += "::";
  1637. OB += "delete";
  1638. if (IsArray)
  1639. OB += "[] ";
  1640. Op->print(OB);
  1641. }
  1642. };
  1643. class PrefixExpr : public Node {
  1644. StringView Prefix;
  1645. Node *Child;
  1646. public:
  1647. PrefixExpr(StringView Prefix_, Node *Child_)
  1648. : Node(KPrefixExpr), Prefix(Prefix_), Child(Child_) {}
  1649. template<typename Fn> void match(Fn F) const { F(Prefix, Child); }
  1650. void printLeft(OutputBuffer &OB) const override {
  1651. OB += Prefix;
  1652. OB += "(";
  1653. Child->print(OB);
  1654. OB += ")";
  1655. }
  1656. };
  1657. class FunctionParam : public Node {
  1658. StringView Number;
  1659. public:
  1660. FunctionParam(StringView Number_) : Node(KFunctionParam), Number(Number_) {}
  1661. template<typename Fn> void match(Fn F) const { F(Number); }
  1662. void printLeft(OutputBuffer &OB) const override {
  1663. OB += "fp";
  1664. OB += Number;
  1665. }
  1666. };
  1667. class ConversionExpr : public Node {
  1668. const Node *Type;
  1669. NodeArray Expressions;
  1670. public:
  1671. ConversionExpr(const Node *Type_, NodeArray Expressions_)
  1672. : Node(KConversionExpr), Type(Type_), Expressions(Expressions_) {}
  1673. template<typename Fn> void match(Fn F) const { F(Type, Expressions); }
  1674. void printLeft(OutputBuffer &OB) const override {
  1675. OB += "(";
  1676. Type->print(OB);
  1677. OB += ")(";
  1678. Expressions.printWithComma(OB);
  1679. OB += ")";
  1680. }
  1681. };
  1682. class PointerToMemberConversionExpr : public Node {
  1683. const Node *Type;
  1684. const Node *SubExpr;
  1685. StringView Offset;
  1686. public:
  1687. PointerToMemberConversionExpr(const Node *Type_, const Node *SubExpr_,
  1688. StringView Offset_)
  1689. : Node(KPointerToMemberConversionExpr), Type(Type_), SubExpr(SubExpr_),
  1690. Offset(Offset_) {}
  1691. template<typename Fn> void match(Fn F) const { F(Type, SubExpr, Offset); }
  1692. void printLeft(OutputBuffer &OB) const override {
  1693. OB += "(";
  1694. Type->print(OB);
  1695. OB += ")(";
  1696. SubExpr->print(OB);
  1697. OB += ")";
  1698. }
  1699. };
  1700. class InitListExpr : public Node {
  1701. const Node *Ty;
  1702. NodeArray Inits;
  1703. public:
  1704. InitListExpr(const Node *Ty_, NodeArray Inits_)
  1705. : Node(KInitListExpr), Ty(Ty_), Inits(Inits_) {}
  1706. template<typename Fn> void match(Fn F) const { F(Ty, Inits); }
  1707. void printLeft(OutputBuffer &OB) const override {
  1708. if (Ty)
  1709. Ty->print(OB);
  1710. OB += '{';
  1711. Inits.printWithComma(OB);
  1712. OB += '}';
  1713. }
  1714. };
  1715. class BracedExpr : public Node {
  1716. const Node *Elem;
  1717. const Node *Init;
  1718. bool IsArray;
  1719. public:
  1720. BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_)
  1721. : Node(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {}
  1722. template<typename Fn> void match(Fn F) const { F(Elem, Init, IsArray); }
  1723. void printLeft(OutputBuffer &OB) const override {
  1724. if (IsArray) {
  1725. OB += '[';
  1726. Elem->print(OB);
  1727. OB += ']';
  1728. } else {
  1729. OB += '.';
  1730. Elem->print(OB);
  1731. }
  1732. if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
  1733. OB += " = ";
  1734. Init->print(OB);
  1735. }
  1736. };
  1737. class BracedRangeExpr : public Node {
  1738. const Node *First;
  1739. const Node *Last;
  1740. const Node *Init;
  1741. public:
  1742. BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_)
  1743. : Node(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {}
  1744. template<typename Fn> void match(Fn F) const { F(First, Last, Init); }
  1745. void printLeft(OutputBuffer &OB) const override {
  1746. OB += '[';
  1747. First->print(OB);
  1748. OB += " ... ";
  1749. Last->print(OB);
  1750. OB += ']';
  1751. if (Init->getKind() != KBracedExpr && Init->getKind() != KBracedRangeExpr)
  1752. OB += " = ";
  1753. Init->print(OB);
  1754. }
  1755. };
  1756. class FoldExpr : public Node {
  1757. const Node *Pack, *Init;
  1758. StringView OperatorName;
  1759. bool IsLeftFold;
  1760. public:
  1761. FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_,
  1762. const Node *Init_)
  1763. : Node(KFoldExpr), Pack(Pack_), Init(Init_), OperatorName(OperatorName_),
  1764. IsLeftFold(IsLeftFold_) {}
  1765. template<typename Fn> void match(Fn F) const {
  1766. F(IsLeftFold, OperatorName, Pack, Init);
  1767. }
  1768. void printLeft(OutputBuffer &OB) const override {
  1769. auto PrintPack = [&] {
  1770. OB += '(';
  1771. ParameterPackExpansion(Pack).print(OB);
  1772. OB += ')';
  1773. };
  1774. OB += '(';
  1775. if (IsLeftFold) {
  1776. // init op ... op pack
  1777. if (Init != nullptr) {
  1778. Init->print(OB);
  1779. OB += ' ';
  1780. OB += OperatorName;
  1781. OB += ' ';
  1782. }
  1783. // ... op pack
  1784. OB += "... ";
  1785. OB += OperatorName;
  1786. OB += ' ';
  1787. PrintPack();
  1788. } else { // !IsLeftFold
  1789. // pack op ...
  1790. PrintPack();
  1791. OB += ' ';
  1792. OB += OperatorName;
  1793. OB += " ...";
  1794. // pack op ... op init
  1795. if (Init != nullptr) {
  1796. OB += ' ';
  1797. OB += OperatorName;
  1798. OB += ' ';
  1799. Init->print(OB);
  1800. }
  1801. }
  1802. OB += ')';
  1803. }
  1804. };
  1805. class ThrowExpr : public Node {
  1806. const Node *Op;
  1807. public:
  1808. ThrowExpr(const Node *Op_) : Node(KThrowExpr), Op(Op_) {}
  1809. template<typename Fn> void match(Fn F) const { F(Op); }
  1810. void printLeft(OutputBuffer &OB) const override {
  1811. OB += "throw ";
  1812. Op->print(OB);
  1813. }
  1814. };
  1815. class BoolExpr : public Node {
  1816. bool Value;
  1817. public:
  1818. BoolExpr(bool Value_) : Node(KBoolExpr), Value(Value_) {}
  1819. template<typename Fn> void match(Fn F) const { F(Value); }
  1820. void printLeft(OutputBuffer &OB) const override {
  1821. OB += Value ? StringView("true") : StringView("false");
  1822. }
  1823. };
  1824. class StringLiteral : public Node {
  1825. const Node *Type;
  1826. public:
  1827. StringLiteral(const Node *Type_) : Node(KStringLiteral), Type(Type_) {}
  1828. template<typename Fn> void match(Fn F) const { F(Type); }
  1829. void printLeft(OutputBuffer &OB) const override {
  1830. OB += "\"<";
  1831. Type->print(OB);
  1832. OB += ">\"";
  1833. }
  1834. };
  1835. class LambdaExpr : public Node {
  1836. const Node *Type;
  1837. public:
  1838. LambdaExpr(const Node *Type_) : Node(KLambdaExpr), Type(Type_) {}
  1839. template<typename Fn> void match(Fn F) const { F(Type); }
  1840. void printLeft(OutputBuffer &OB) const override {
  1841. OB += "[]";
  1842. if (Type->getKind() == KClosureTypeName)
  1843. static_cast<const ClosureTypeName *>(Type)->printDeclarator(OB);
  1844. OB += "{...}";
  1845. }
  1846. };
  1847. class EnumLiteral : public Node {
  1848. // ty(integer)
  1849. const Node *Ty;
  1850. StringView Integer;
  1851. public:
  1852. EnumLiteral(const Node *Ty_, StringView Integer_)
  1853. : Node(KEnumLiteral), Ty(Ty_), Integer(Integer_) {}
  1854. template<typename Fn> void match(Fn F) const { F(Ty, Integer); }
  1855. void printLeft(OutputBuffer &OB) const override {
  1856. OB << "(";
  1857. Ty->print(OB);
  1858. OB << ")";
  1859. if (Integer[0] == 'n')
  1860. OB << "-" << Integer.dropFront(1);
  1861. else
  1862. OB << Integer;
  1863. }
  1864. };
  1865. class IntegerLiteral : public Node {
  1866. StringView Type;
  1867. StringView Value;
  1868. public:
  1869. IntegerLiteral(StringView Type_, StringView Value_)
  1870. : Node(KIntegerLiteral), Type(Type_), Value(Value_) {}
  1871. template<typename Fn> void match(Fn F) const { F(Type, Value); }
  1872. void printLeft(OutputBuffer &OB) const override {
  1873. if (Type.size() > 3) {
  1874. OB += "(";
  1875. OB += Type;
  1876. OB += ")";
  1877. }
  1878. if (Value[0] == 'n') {
  1879. OB += "-";
  1880. OB += Value.dropFront(1);
  1881. } else
  1882. OB += Value;
  1883. if (Type.size() <= 3)
  1884. OB += Type;
  1885. }
  1886. };
  1887. template <class Float> struct FloatData;
  1888. namespace float_literal_impl {
  1889. constexpr Node::Kind getFloatLiteralKind(float *) {
  1890. return Node::KFloatLiteral;
  1891. }
  1892. constexpr Node::Kind getFloatLiteralKind(double *) {
  1893. return Node::KDoubleLiteral;
  1894. }
  1895. constexpr Node::Kind getFloatLiteralKind(long double *) {
  1896. return Node::KLongDoubleLiteral;
  1897. }
  1898. }
  1899. template <class Float> class FloatLiteralImpl : public Node {
  1900. const StringView Contents;
  1901. static constexpr Kind KindForClass =
  1902. float_literal_impl::getFloatLiteralKind((Float *)nullptr);
  1903. public:
  1904. FloatLiteralImpl(StringView Contents_)
  1905. : Node(KindForClass), Contents(Contents_) {}
  1906. template<typename Fn> void match(Fn F) const { F(Contents); }
  1907. void printLeft(OutputBuffer &OB) const override {
  1908. const char *first = Contents.begin();
  1909. const char *last = Contents.end() + 1;
  1910. const size_t N = FloatData<Float>::mangled_size;
  1911. if (static_cast<std::size_t>(last - first) > N) {
  1912. last = first + N;
  1913. union {
  1914. Float value;
  1915. char buf[sizeof(Float)];
  1916. };
  1917. const char *t = first;
  1918. char *e = buf;
  1919. for (; t != last; ++t, ++e) {
  1920. unsigned d1 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
  1921. : static_cast<unsigned>(*t - 'a' + 10);
  1922. ++t;
  1923. unsigned d0 = isdigit(*t) ? static_cast<unsigned>(*t - '0')
  1924. : static_cast<unsigned>(*t - 'a' + 10);
  1925. *e = static_cast<char>((d1 << 4) + d0);
  1926. }
  1927. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  1928. std::reverse(buf, e);
  1929. #endif
  1930. char num[FloatData<Float>::max_demangled_size] = {0};
  1931. int n = snprintf(num, sizeof(num), FloatData<Float>::spec, value);
  1932. OB += StringView(num, num + n);
  1933. }
  1934. }
  1935. };
  1936. using FloatLiteral = FloatLiteralImpl<float>;
  1937. using DoubleLiteral = FloatLiteralImpl<double>;
  1938. using LongDoubleLiteral = FloatLiteralImpl<long double>;
  1939. /// Visit the node. Calls \c F(P), where \c P is the node cast to the
  1940. /// appropriate derived class.
  1941. template<typename Fn>
  1942. void Node::visit(Fn F) const {
  1943. switch (K) {
  1944. #define CASE(X) case K ## X: return F(static_cast<const X*>(this));
  1945. FOR_EACH_NODE_KIND(CASE)
  1946. #undef CASE
  1947. }
  1948. assert(0 && "unknown mangling node kind");
  1949. }
  1950. /// Determine the kind of a node from its type.
  1951. template<typename NodeT> struct NodeKind;
  1952. #define SPECIALIZATION(X) \
  1953. template<> struct NodeKind<X> { \
  1954. static constexpr Node::Kind Kind = Node::K##X; \
  1955. static constexpr const char *name() { return #X; } \
  1956. };
  1957. FOR_EACH_NODE_KIND(SPECIALIZATION)
  1958. #undef SPECIALIZATION
  1959. #undef FOR_EACH_NODE_KIND
  1960. template <typename Derived, typename Alloc> struct AbstractManglingParser {
  1961. const char *First;
  1962. const char *Last;
  1963. // Name stack, this is used by the parser to hold temporary names that were
  1964. // parsed. The parser collapses multiple names into new nodes to construct
  1965. // the AST. Once the parser is finished, names.size() == 1.
  1966. PODSmallVector<Node *, 32> Names;
  1967. // Substitution table. Itanium supports name substitutions as a means of
  1968. // compression. The string "S42_" refers to the 44nd entry (base-36) in this
  1969. // table.
  1970. PODSmallVector<Node *, 32> Subs;
  1971. using TemplateParamList = PODSmallVector<Node *, 8>;
  1972. class ScopedTemplateParamList {
  1973. AbstractManglingParser *Parser;
  1974. size_t OldNumTemplateParamLists;
  1975. TemplateParamList Params;
  1976. public:
  1977. ScopedTemplateParamList(AbstractManglingParser *TheParser)
  1978. : Parser(TheParser),
  1979. OldNumTemplateParamLists(TheParser->TemplateParams.size()) {
  1980. Parser->TemplateParams.push_back(&Params);
  1981. }
  1982. ~ScopedTemplateParamList() {
  1983. assert(Parser->TemplateParams.size() >= OldNumTemplateParamLists);
  1984. Parser->TemplateParams.dropBack(OldNumTemplateParamLists);
  1985. }
  1986. };
  1987. // Template parameter table. Like the above, but referenced like "T42_".
  1988. // This has a smaller size compared to Subs and Names because it can be
  1989. // stored on the stack.
  1990. TemplateParamList OuterTemplateParams;
  1991. // Lists of template parameters indexed by template parameter depth,
  1992. // referenced like "TL2_4_". If nonempty, element 0 is always
  1993. // OuterTemplateParams; inner elements are always template parameter lists of
  1994. // lambda expressions. For a generic lambda with no explicit template
  1995. // parameter list, the corresponding parameter list pointer will be null.
  1996. PODSmallVector<TemplateParamList *, 4> TemplateParams;
  1997. // Set of unresolved forward <template-param> references. These can occur in a
  1998. // conversion operator's type, and are resolved in the enclosing <encoding>.
  1999. PODSmallVector<ForwardTemplateReference *, 4> ForwardTemplateRefs;
  2000. bool TryToParseTemplateArgs = true;
  2001. bool PermitForwardTemplateReferences = false;
  2002. size_t ParsingLambdaParamsAtLevel = (size_t)-1;
  2003. unsigned NumSyntheticTemplateParameters[3] = {};
  2004. Alloc ASTAllocator;
  2005. AbstractManglingParser(const char *First_, const char *Last_)
  2006. : First(First_), Last(Last_) {}
  2007. Derived &getDerived() { return static_cast<Derived &>(*this); }
  2008. void reset(const char *First_, const char *Last_) {
  2009. First = First_;
  2010. Last = Last_;
  2011. Names.clear();
  2012. Subs.clear();
  2013. TemplateParams.clear();
  2014. ParsingLambdaParamsAtLevel = (size_t)-1;
  2015. TryToParseTemplateArgs = true;
  2016. PermitForwardTemplateReferences = false;
  2017. for (int I = 0; I != 3; ++I)
  2018. NumSyntheticTemplateParameters[I] = 0;
  2019. ASTAllocator.reset();
  2020. }
  2021. template <class T, class... Args> Node *make(Args &&... args) {
  2022. return ASTAllocator.template makeNode<T>(std::forward<Args>(args)...);
  2023. }
  2024. template <class It> NodeArray makeNodeArray(It begin, It end) {
  2025. size_t sz = static_cast<size_t>(end - begin);
  2026. void *mem = ASTAllocator.allocateNodeArray(sz);
  2027. Node **data = new (mem) Node *[sz];
  2028. std::copy(begin, end, data);
  2029. return NodeArray(data, sz);
  2030. }
  2031. NodeArray popTrailingNodeArray(size_t FromPosition) {
  2032. assert(FromPosition <= Names.size());
  2033. NodeArray res =
  2034. makeNodeArray(Names.begin() + (long)FromPosition, Names.end());
  2035. Names.dropBack(FromPosition);
  2036. return res;
  2037. }
  2038. bool consumeIf(StringView S) {
  2039. if (StringView(First, Last).startsWith(S)) {
  2040. First += S.size();
  2041. return true;
  2042. }
  2043. return false;
  2044. }
  2045. bool consumeIf(char C) {
  2046. if (First != Last && *First == C) {
  2047. ++First;
  2048. return true;
  2049. }
  2050. return false;
  2051. }
  2052. char consume() { return First != Last ? *First++ : '\0'; }
  2053. char look(unsigned Lookahead = 0) const {
  2054. if (static_cast<size_t>(Last - First) <= Lookahead)
  2055. return '\0';
  2056. return First[Lookahead];
  2057. }
  2058. size_t numLeft() const { return static_cast<size_t>(Last - First); }
  2059. StringView parseNumber(bool AllowNegative = false);
  2060. Qualifiers parseCVQualifiers();
  2061. bool parsePositiveInteger(size_t *Out);
  2062. StringView parseBareSourceName();
  2063. bool parseSeqId(size_t *Out);
  2064. Node *parseSubstitution();
  2065. Node *parseTemplateParam();
  2066. Node *parseTemplateParamDecl();
  2067. Node *parseTemplateArgs(bool TagTemplates = false);
  2068. Node *parseTemplateArg();
  2069. /// Parse the <expr> production.
  2070. Node *parseExpr();
  2071. Node *parsePrefixExpr(StringView Kind);
  2072. Node *parseBinaryExpr(StringView Kind);
  2073. Node *parseIntegerLiteral(StringView Lit);
  2074. Node *parseExprPrimary();
  2075. template <class Float> Node *parseFloatingLiteral();
  2076. Node *parseFunctionParam();
  2077. Node *parseNewExpr();
  2078. Node *parseConversionExpr();
  2079. Node *parseBracedExpr();
  2080. Node *parseFoldExpr();
  2081. Node *parsePointerToMemberConversionExpr();
  2082. Node *parseSubobjectExpr();
  2083. /// Parse the <type> production.
  2084. Node *parseType();
  2085. Node *parseFunctionType();
  2086. Node *parseVectorType();
  2087. Node *parseDecltype();
  2088. Node *parseArrayType();
  2089. Node *parsePointerToMemberType();
  2090. Node *parseClassEnumType();
  2091. Node *parseQualifiedType();
  2092. Node *parseEncoding();
  2093. bool parseCallOffset();
  2094. Node *parseSpecialName();
  2095. /// Holds some extra information about a <name> that is being parsed. This
  2096. /// information is only pertinent if the <name> refers to an <encoding>.
  2097. struct NameState {
  2098. bool CtorDtorConversion = false;
  2099. bool EndsWithTemplateArgs = false;
  2100. Qualifiers CVQualifiers = QualNone;
  2101. FunctionRefQual ReferenceQualifier = FrefQualNone;
  2102. size_t ForwardTemplateRefsBegin;
  2103. NameState(AbstractManglingParser *Enclosing)
  2104. : ForwardTemplateRefsBegin(Enclosing->ForwardTemplateRefs.size()) {}
  2105. };
  2106. bool resolveForwardTemplateRefs(NameState &State) {
  2107. size_t I = State.ForwardTemplateRefsBegin;
  2108. size_t E = ForwardTemplateRefs.size();
  2109. for (; I < E; ++I) {
  2110. size_t Idx = ForwardTemplateRefs[I]->Index;
  2111. if (TemplateParams.empty() || !TemplateParams[0] ||
  2112. Idx >= TemplateParams[0]->size())
  2113. return true;
  2114. ForwardTemplateRefs[I]->Ref = (*TemplateParams[0])[Idx];
  2115. }
  2116. ForwardTemplateRefs.dropBack(State.ForwardTemplateRefsBegin);
  2117. return false;
  2118. }
  2119. /// Parse the <name> production>
  2120. Node *parseName(NameState *State = nullptr);
  2121. Node *parseLocalName(NameState *State);
  2122. Node *parseOperatorName(NameState *State);
  2123. Node *parseUnqualifiedName(NameState *State);
  2124. Node *parseUnnamedTypeName(NameState *State);
  2125. Node *parseSourceName(NameState *State);
  2126. Node *parseUnscopedName(NameState *State);
  2127. Node *parseNestedName(NameState *State);
  2128. Node *parseCtorDtorName(Node *&SoFar, NameState *State);
  2129. Node *parseAbiTags(Node *N);
  2130. /// Parse the <unresolved-name> production.
  2131. Node *parseUnresolvedName();
  2132. Node *parseSimpleId();
  2133. Node *parseBaseUnresolvedName();
  2134. Node *parseUnresolvedType();
  2135. Node *parseDestructorName();
  2136. /// Top-level entry point into the parser.
  2137. Node *parse();
  2138. };
  2139. const char* parse_discriminator(const char* first, const char* last);
  2140. // <name> ::= <nested-name> // N
  2141. // ::= <local-name> # See Scope Encoding below // Z
  2142. // ::= <unscoped-template-name> <template-args>
  2143. // ::= <unscoped-name>
  2144. //
  2145. // <unscoped-template-name> ::= <unscoped-name>
  2146. // ::= <substitution>
  2147. template <typename Derived, typename Alloc>
  2148. Node *AbstractManglingParser<Derived, Alloc>::parseName(NameState *State) {
  2149. consumeIf('L'); // extension
  2150. if (look() == 'N')
  2151. return getDerived().parseNestedName(State);
  2152. if (look() == 'Z')
  2153. return getDerived().parseLocalName(State);
  2154. Node *Result = nullptr;
  2155. bool IsSubst = look() == 'S' && look(1) != 't';
  2156. if (IsSubst) {
  2157. // A substitution must lead to:
  2158. // ::= <unscoped-template-name> <template-args>
  2159. Result = getDerived().parseSubstitution();
  2160. } else {
  2161. // An unscoped name can be one of:
  2162. // ::= <unscoped-name>
  2163. // ::= <unscoped-template-name> <template-args>
  2164. Result = getDerived().parseUnscopedName(State);
  2165. }
  2166. if (Result == nullptr)
  2167. return nullptr;
  2168. if (look() == 'I') {
  2169. // ::= <unscoped-template-name> <template-args>
  2170. if (!IsSubst)
  2171. // An unscoped-template-name is substitutable.
  2172. Subs.push_back(Result);
  2173. Node *TA = getDerived().parseTemplateArgs(State != nullptr);
  2174. if (TA == nullptr)
  2175. return nullptr;
  2176. if (State)
  2177. State->EndsWithTemplateArgs = true;
  2178. Result = make<NameWithTemplateArgs>(Result, TA);
  2179. } else if (IsSubst) {
  2180. // The substitution case must be followed by <template-args>.
  2181. return nullptr;
  2182. }
  2183. return Result;
  2184. }
  2185. // <local-name> := Z <function encoding> E <entity name> [<discriminator>]
  2186. // := Z <function encoding> E s [<discriminator>]
  2187. // := Z <function encoding> Ed [ <parameter number> ] _ <entity name>
  2188. template <typename Derived, typename Alloc>
  2189. Node *AbstractManglingParser<Derived, Alloc>::parseLocalName(NameState *State) {
  2190. if (!consumeIf('Z'))
  2191. return nullptr;
  2192. Node *Encoding = getDerived().parseEncoding();
  2193. if (Encoding == nullptr || !consumeIf('E'))
  2194. return nullptr;
  2195. if (consumeIf('s')) {
  2196. First = parse_discriminator(First, Last);
  2197. auto *StringLitName = make<NameType>("string literal");
  2198. if (!StringLitName)
  2199. return nullptr;
  2200. return make<LocalName>(Encoding, StringLitName);
  2201. }
  2202. if (consumeIf('d')) {
  2203. parseNumber(true);
  2204. if (!consumeIf('_'))
  2205. return nullptr;
  2206. Node *N = getDerived().parseName(State);
  2207. if (N == nullptr)
  2208. return nullptr;
  2209. return make<LocalName>(Encoding, N);
  2210. }
  2211. Node *Entity = getDerived().parseName(State);
  2212. if (Entity == nullptr)
  2213. return nullptr;
  2214. First = parse_discriminator(First, Last);
  2215. return make<LocalName>(Encoding, Entity);
  2216. }
  2217. // <unscoped-name> ::= <unqualified-name>
  2218. // ::= St <unqualified-name> # ::std::
  2219. // extension ::= StL<unqualified-name>
  2220. template <typename Derived, typename Alloc>
  2221. Node *
  2222. AbstractManglingParser<Derived, Alloc>::parseUnscopedName(NameState *State) {
  2223. bool IsStd = consumeIf("St");
  2224. if (IsStd)
  2225. consumeIf('L');
  2226. Node *Result = getDerived().parseUnqualifiedName(State);
  2227. if (Result == nullptr)
  2228. return nullptr;
  2229. if (IsStd)
  2230. Result = make<StdQualifiedName>(Result);
  2231. return Result;
  2232. }
  2233. // <unqualified-name> ::= <operator-name> [abi-tags]
  2234. // ::= <ctor-dtor-name>
  2235. // ::= <source-name>
  2236. // ::= <unnamed-type-name>
  2237. // ::= DC <source-name>+ E # structured binding declaration
  2238. template <typename Derived, typename Alloc>
  2239. Node *
  2240. AbstractManglingParser<Derived, Alloc>::parseUnqualifiedName(NameState *State) {
  2241. // <ctor-dtor-name>s are special-cased in parseNestedName().
  2242. Node *Result;
  2243. if (look() == 'U')
  2244. Result = getDerived().parseUnnamedTypeName(State);
  2245. else if (look() >= '1' && look() <= '9')
  2246. Result = getDerived().parseSourceName(State);
  2247. else if (consumeIf("DC")) {
  2248. size_t BindingsBegin = Names.size();
  2249. do {
  2250. Node *Binding = getDerived().parseSourceName(State);
  2251. if (Binding == nullptr)
  2252. return nullptr;
  2253. Names.push_back(Binding);
  2254. } while (!consumeIf('E'));
  2255. Result = make<StructuredBindingName>(popTrailingNodeArray(BindingsBegin));
  2256. } else
  2257. Result = getDerived().parseOperatorName(State);
  2258. if (Result != nullptr)
  2259. Result = getDerived().parseAbiTags(Result);
  2260. return Result;
  2261. }
  2262. // <unnamed-type-name> ::= Ut [<nonnegative number>] _
  2263. // ::= <closure-type-name>
  2264. //
  2265. // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _
  2266. //
  2267. // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters
  2268. template <typename Derived, typename Alloc>
  2269. Node *
  2270. AbstractManglingParser<Derived, Alloc>::parseUnnamedTypeName(NameState *State) {
  2271. // <template-params> refer to the innermost <template-args>. Clear out any
  2272. // outer args that we may have inserted into TemplateParams.
  2273. if (State != nullptr)
  2274. TemplateParams.clear();
  2275. if (consumeIf("Ut")) {
  2276. StringView Count = parseNumber();
  2277. if (!consumeIf('_'))
  2278. return nullptr;
  2279. return make<UnnamedTypeName>(Count);
  2280. }
  2281. if (consumeIf("Ul")) {
  2282. SwapAndRestore<size_t> SwapParams(ParsingLambdaParamsAtLevel,
  2283. TemplateParams.size());
  2284. ScopedTemplateParamList LambdaTemplateParams(this);
  2285. size_t ParamsBegin = Names.size();
  2286. while (look() == 'T' &&
  2287. StringView("yptn").find(look(1)) != StringView::npos) {
  2288. Node *T = parseTemplateParamDecl();
  2289. if (!T)
  2290. return nullptr;
  2291. Names.push_back(T);
  2292. }
  2293. NodeArray TempParams = popTrailingNodeArray(ParamsBegin);
  2294. // FIXME: If TempParams is empty and none of the function parameters
  2295. // includes 'auto', we should remove LambdaTemplateParams from the
  2296. // TemplateParams list. Unfortunately, we don't find out whether there are
  2297. // any 'auto' parameters until too late in an example such as:
  2298. //
  2299. // template<typename T> void f(
  2300. // decltype([](decltype([]<typename T>(T v) {}),
  2301. // auto) {})) {}
  2302. // template<typename T> void f(
  2303. // decltype([](decltype([]<typename T>(T w) {}),
  2304. // int) {})) {}
  2305. //
  2306. // Here, the type of v is at level 2 but the type of w is at level 1. We
  2307. // don't find this out until we encounter the type of the next parameter.
  2308. //
  2309. // However, compilers can't actually cope with the former example in
  2310. // practice, and it's likely to be made ill-formed in future, so we don't
  2311. // need to support it here.
  2312. //
  2313. // If we encounter an 'auto' in the function parameter types, we will
  2314. // recreate a template parameter scope for it, but any intervening lambdas
  2315. // will be parsed in the 'wrong' template parameter depth.
  2316. if (TempParams.empty())
  2317. TemplateParams.pop_back();
  2318. if (!consumeIf("vE")) {
  2319. do {
  2320. Node *P = getDerived().parseType();
  2321. if (P == nullptr)
  2322. return nullptr;
  2323. Names.push_back(P);
  2324. } while (!consumeIf('E'));
  2325. }
  2326. NodeArray Params = popTrailingNodeArray(ParamsBegin);
  2327. StringView Count = parseNumber();
  2328. if (!consumeIf('_'))
  2329. return nullptr;
  2330. return make<ClosureTypeName>(TempParams, Params, Count);
  2331. }
  2332. if (consumeIf("Ub")) {
  2333. (void)parseNumber();
  2334. if (!consumeIf('_'))
  2335. return nullptr;
  2336. return make<NameType>("'block-literal'");
  2337. }
  2338. return nullptr;
  2339. }
  2340. // <source-name> ::= <positive length number> <identifier>
  2341. template <typename Derived, typename Alloc>
  2342. Node *AbstractManglingParser<Derived, Alloc>::parseSourceName(NameState *) {
  2343. size_t Length = 0;
  2344. if (parsePositiveInteger(&Length))
  2345. return nullptr;
  2346. if (numLeft() < Length || Length == 0)
  2347. return nullptr;
  2348. StringView Name(First, First + Length);
  2349. First += Length;
  2350. if (Name.startsWith("_GLOBAL__N"))
  2351. return make<NameType>("(anonymous namespace)");
  2352. return make<NameType>(Name);
  2353. }
  2354. // <operator-name> ::= aa # &&
  2355. // ::= ad # & (unary)
  2356. // ::= an # &
  2357. // ::= aN # &=
  2358. // ::= aS # =
  2359. // ::= cl # ()
  2360. // ::= cm # ,
  2361. // ::= co # ~
  2362. // ::= cv <type> # (cast)
  2363. // ::= da # delete[]
  2364. // ::= de # * (unary)
  2365. // ::= dl # delete
  2366. // ::= dv # /
  2367. // ::= dV # /=
  2368. // ::= eo # ^
  2369. // ::= eO # ^=
  2370. // ::= eq # ==
  2371. // ::= ge # >=
  2372. // ::= gt # >
  2373. // ::= ix # []
  2374. // ::= le # <=
  2375. // ::= li <source-name> # operator ""
  2376. // ::= ls # <<
  2377. // ::= lS # <<=
  2378. // ::= lt # <
  2379. // ::= mi # -
  2380. // ::= mI # -=
  2381. // ::= ml # *
  2382. // ::= mL # *=
  2383. // ::= mm # -- (postfix in <expression> context)
  2384. // ::= na # new[]
  2385. // ::= ne # !=
  2386. // ::= ng # - (unary)
  2387. // ::= nt # !
  2388. // ::= nw # new
  2389. // ::= oo # ||
  2390. // ::= or # |
  2391. // ::= oR # |=
  2392. // ::= pm # ->*
  2393. // ::= pl # +
  2394. // ::= pL # +=
  2395. // ::= pp # ++ (postfix in <expression> context)
  2396. // ::= ps # + (unary)
  2397. // ::= pt # ->
  2398. // ::= qu # ?
  2399. // ::= rm # %
  2400. // ::= rM # %=
  2401. // ::= rs # >>
  2402. // ::= rS # >>=
  2403. // ::= ss # <=> C++2a
  2404. // ::= v <digit> <source-name> # vendor extended operator
  2405. template <typename Derived, typename Alloc>
  2406. Node *
  2407. AbstractManglingParser<Derived, Alloc>::parseOperatorName(NameState *State) {
  2408. switch (look()) {
  2409. case 'a':
  2410. switch (look(1)) {
  2411. case 'a':
  2412. First += 2;
  2413. return make<NameType>("operator&&");
  2414. case 'd':
  2415. case 'n':
  2416. First += 2;
  2417. return make<NameType>("operator&");
  2418. case 'N':
  2419. First += 2;
  2420. return make<NameType>("operator&=");
  2421. case 'S':
  2422. First += 2;
  2423. return make<NameType>("operator=");
  2424. }
  2425. return nullptr;
  2426. case 'c':
  2427. switch (look(1)) {
  2428. case 'l':
  2429. First += 2;
  2430. return make<NameType>("operator()");
  2431. case 'm':
  2432. First += 2;
  2433. return make<NameType>("operator,");
  2434. case 'o':
  2435. First += 2;
  2436. return make<NameType>("operator~");
  2437. // ::= cv <type> # (cast)
  2438. case 'v': {
  2439. First += 2;
  2440. SwapAndRestore<bool> SaveTemplate(TryToParseTemplateArgs, false);
  2441. // If we're parsing an encoding, State != nullptr and the conversion
  2442. // operators' <type> could have a <template-param> that refers to some
  2443. // <template-arg>s further ahead in the mangled name.
  2444. SwapAndRestore<bool> SavePermit(PermitForwardTemplateReferences,
  2445. PermitForwardTemplateReferences ||
  2446. State != nullptr);
  2447. Node *Ty = getDerived().parseType();
  2448. if (Ty == nullptr)
  2449. return nullptr;
  2450. if (State) State->CtorDtorConversion = true;
  2451. return make<ConversionOperatorType>(Ty);
  2452. }
  2453. }
  2454. return nullptr;
  2455. case 'd':
  2456. switch (look(1)) {
  2457. case 'a':
  2458. First += 2;
  2459. return make<NameType>("operator delete[]");
  2460. case 'e':
  2461. First += 2;
  2462. return make<NameType>("operator*");
  2463. case 'l':
  2464. First += 2;
  2465. return make<NameType>("operator delete");
  2466. case 'v':
  2467. First += 2;
  2468. return make<NameType>("operator/");
  2469. case 'V':
  2470. First += 2;
  2471. return make<NameType>("operator/=");
  2472. }
  2473. return nullptr;
  2474. case 'e':
  2475. switch (look(1)) {
  2476. case 'o':
  2477. First += 2;
  2478. return make<NameType>("operator^");
  2479. case 'O':
  2480. First += 2;
  2481. return make<NameType>("operator^=");
  2482. case 'q':
  2483. First += 2;
  2484. return make<NameType>("operator==");
  2485. }
  2486. return nullptr;
  2487. case 'g':
  2488. switch (look(1)) {
  2489. case 'e':
  2490. First += 2;
  2491. return make<NameType>("operator>=");
  2492. case 't':
  2493. First += 2;
  2494. return make<NameType>("operator>");
  2495. }
  2496. return nullptr;
  2497. case 'i':
  2498. if (look(1) == 'x') {
  2499. First += 2;
  2500. return make<NameType>("operator[]");
  2501. }
  2502. return nullptr;
  2503. case 'l':
  2504. switch (look(1)) {
  2505. case 'e':
  2506. First += 2;
  2507. return make<NameType>("operator<=");
  2508. // ::= li <source-name> # operator ""
  2509. case 'i': {
  2510. First += 2;
  2511. Node *SN = getDerived().parseSourceName(State);
  2512. if (SN == nullptr)
  2513. return nullptr;
  2514. return make<LiteralOperator>(SN);
  2515. }
  2516. case 's':
  2517. First += 2;
  2518. return make<NameType>("operator<<");
  2519. case 'S':
  2520. First += 2;
  2521. return make<NameType>("operator<<=");
  2522. case 't':
  2523. First += 2;
  2524. return make<NameType>("operator<");
  2525. }
  2526. return nullptr;
  2527. case 'm':
  2528. switch (look(1)) {
  2529. case 'i':
  2530. First += 2;
  2531. return make<NameType>("operator-");
  2532. case 'I':
  2533. First += 2;
  2534. return make<NameType>("operator-=");
  2535. case 'l':
  2536. First += 2;
  2537. return make<NameType>("operator*");
  2538. case 'L':
  2539. First += 2;
  2540. return make<NameType>("operator*=");
  2541. case 'm':
  2542. First += 2;
  2543. return make<NameType>("operator--");
  2544. }
  2545. return nullptr;
  2546. case 'n':
  2547. switch (look(1)) {
  2548. case 'a':
  2549. First += 2;
  2550. return make<NameType>("operator new[]");
  2551. case 'e':
  2552. First += 2;
  2553. return make<NameType>("operator!=");
  2554. case 'g':
  2555. First += 2;
  2556. return make<NameType>("operator-");
  2557. case 't':
  2558. First += 2;
  2559. return make<NameType>("operator!");
  2560. case 'w':
  2561. First += 2;
  2562. return make<NameType>("operator new");
  2563. }
  2564. return nullptr;
  2565. case 'o':
  2566. switch (look(1)) {
  2567. case 'o':
  2568. First += 2;
  2569. return make<NameType>("operator||");
  2570. case 'r':
  2571. First += 2;
  2572. return make<NameType>("operator|");
  2573. case 'R':
  2574. First += 2;
  2575. return make<NameType>("operator|=");
  2576. }
  2577. return nullptr;
  2578. case 'p':
  2579. switch (look(1)) {
  2580. case 'm':
  2581. First += 2;
  2582. return make<NameType>("operator->*");
  2583. case 'l':
  2584. First += 2;
  2585. return make<NameType>("operator+");
  2586. case 'L':
  2587. First += 2;
  2588. return make<NameType>("operator+=");
  2589. case 'p':
  2590. First += 2;
  2591. return make<NameType>("operator++");
  2592. case 's':
  2593. First += 2;
  2594. return make<NameType>("operator+");
  2595. case 't':
  2596. First += 2;
  2597. return make<NameType>("operator->");
  2598. }
  2599. return nullptr;
  2600. case 'q':
  2601. if (look(1) == 'u') {
  2602. First += 2;
  2603. return make<NameType>("operator?");
  2604. }
  2605. return nullptr;
  2606. case 'r':
  2607. switch (look(1)) {
  2608. case 'm':
  2609. First += 2;
  2610. return make<NameType>("operator%");
  2611. case 'M':
  2612. First += 2;
  2613. return make<NameType>("operator%=");
  2614. case 's':
  2615. First += 2;
  2616. return make<NameType>("operator>>");
  2617. case 'S':
  2618. First += 2;
  2619. return make<NameType>("operator>>=");
  2620. }
  2621. return nullptr;
  2622. case 's':
  2623. if (look(1) == 's') {
  2624. First += 2;
  2625. return make<NameType>("operator<=>");
  2626. }
  2627. return nullptr;
  2628. // ::= v <digit> <source-name> # vendor extended operator
  2629. case 'v':
  2630. if (std::isdigit(look(1))) {
  2631. First += 2;
  2632. Node *SN = getDerived().parseSourceName(State);
  2633. if (SN == nullptr)
  2634. return nullptr;
  2635. return make<ConversionOperatorType>(SN);
  2636. }
  2637. return nullptr;
  2638. }
  2639. return nullptr;
  2640. }
  2641. // <ctor-dtor-name> ::= C1 # complete object constructor
  2642. // ::= C2 # base object constructor
  2643. // ::= C3 # complete object allocating constructor
  2644. // extension ::= C4 # gcc old-style "[unified]" constructor
  2645. // extension ::= C5 # the COMDAT used for ctors
  2646. // ::= D0 # deleting destructor
  2647. // ::= D1 # complete object destructor
  2648. // ::= D2 # base object destructor
  2649. // extension ::= D4 # gcc old-style "[unified]" destructor
  2650. // extension ::= D5 # the COMDAT used for dtors
  2651. template <typename Derived, typename Alloc>
  2652. Node *
  2653. AbstractManglingParser<Derived, Alloc>::parseCtorDtorName(Node *&SoFar,
  2654. NameState *State) {
  2655. if (SoFar->getKind() == Node::KSpecialSubstitution) {
  2656. auto SSK = static_cast<SpecialSubstitution *>(SoFar)->SSK;
  2657. switch (SSK) {
  2658. case SpecialSubKind::string:
  2659. case SpecialSubKind::istream:
  2660. case SpecialSubKind::ostream:
  2661. case SpecialSubKind::iostream:
  2662. SoFar = make<ExpandedSpecialSubstitution>(SSK);
  2663. if (!SoFar)
  2664. return nullptr;
  2665. break;
  2666. default:
  2667. break;
  2668. }
  2669. }
  2670. if (consumeIf('C')) {
  2671. bool IsInherited = consumeIf('I');
  2672. if (look() != '1' && look() != '2' && look() != '3' && look() != '4' &&
  2673. look() != '5')
  2674. return nullptr;
  2675. int Variant = look() - '0';
  2676. ++First;
  2677. if (State) State->CtorDtorConversion = true;
  2678. if (IsInherited) {
  2679. if (getDerived().parseName(State) == nullptr)
  2680. return nullptr;
  2681. }
  2682. return make<CtorDtorName>(SoFar, /*IsDtor=*/false, Variant);
  2683. }
  2684. if (look() == 'D' && (look(1) == '0' || look(1) == '1' || look(1) == '2' ||
  2685. look(1) == '4' || look(1) == '5')) {
  2686. int Variant = look(1) - '0';
  2687. First += 2;
  2688. if (State) State->CtorDtorConversion = true;
  2689. return make<CtorDtorName>(SoFar, /*IsDtor=*/true, Variant);
  2690. }
  2691. return nullptr;
  2692. }
  2693. // <nested-name> ::= N [<CV-Qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
  2694. // ::= N [<CV-Qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
  2695. //
  2696. // <prefix> ::= <prefix> <unqualified-name>
  2697. // ::= <template-prefix> <template-args>
  2698. // ::= <template-param>
  2699. // ::= <decltype>
  2700. // ::= # empty
  2701. // ::= <substitution>
  2702. // ::= <prefix> <data-member-prefix>
  2703. // extension ::= L
  2704. //
  2705. // <data-member-prefix> := <member source-name> [<template-args>] M
  2706. //
  2707. // <template-prefix> ::= <prefix> <template unqualified-name>
  2708. // ::= <template-param>
  2709. // ::= <substitution>
  2710. template <typename Derived, typename Alloc>
  2711. Node *
  2712. AbstractManglingParser<Derived, Alloc>::parseNestedName(NameState *State) {
  2713. if (!consumeIf('N'))
  2714. return nullptr;
  2715. Qualifiers CVTmp = parseCVQualifiers();
  2716. if (State) State->CVQualifiers = CVTmp;
  2717. if (consumeIf('O')) {
  2718. if (State) State->ReferenceQualifier = FrefQualRValue;
  2719. } else if (consumeIf('R')) {
  2720. if (State) State->ReferenceQualifier = FrefQualLValue;
  2721. } else
  2722. if (State) State->ReferenceQualifier = FrefQualNone;
  2723. Node *SoFar = nullptr;
  2724. auto PushComponent = [&](Node *Comp) {
  2725. if (!Comp) return false;
  2726. if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
  2727. else SoFar = Comp;
  2728. if (State) State->EndsWithTemplateArgs = false;
  2729. return SoFar != nullptr;
  2730. };
  2731. if (consumeIf("St")) {
  2732. SoFar = make<NameType>("std");
  2733. if (!SoFar)
  2734. return nullptr;
  2735. }
  2736. while (!consumeIf('E')) {
  2737. consumeIf('L'); // extension
  2738. // <data-member-prefix> := <member source-name> [<template-args>] M
  2739. if (consumeIf('M')) {
  2740. if (SoFar == nullptr)
  2741. return nullptr;
  2742. continue;
  2743. }
  2744. // ::= <template-param>
  2745. if (look() == 'T') {
  2746. if (!PushComponent(getDerived().parseTemplateParam()))
  2747. return nullptr;
  2748. Subs.push_back(SoFar);
  2749. continue;
  2750. }
  2751. // ::= <template-prefix> <template-args>
  2752. if (look() == 'I') {
  2753. Node *TA = getDerived().parseTemplateArgs(State != nullptr);
  2754. if (TA == nullptr || SoFar == nullptr)
  2755. return nullptr;
  2756. SoFar = make<NameWithTemplateArgs>(SoFar, TA);
  2757. if (!SoFar)
  2758. return nullptr;
  2759. if (State) State->EndsWithTemplateArgs = true;
  2760. Subs.push_back(SoFar);
  2761. continue;
  2762. }
  2763. // ::= <decltype>
  2764. if (look() == 'D' && (look(1) == 't' || look(1) == 'T')) {
  2765. if (!PushComponent(getDerived().parseDecltype()))
  2766. return nullptr;
  2767. Subs.push_back(SoFar);
  2768. continue;
  2769. }
  2770. // ::= <substitution>
  2771. if (look() == 'S' && look(1) != 't') {
  2772. Node *S = getDerived().parseSubstitution();
  2773. if (!PushComponent(S))
  2774. return nullptr;
  2775. if (SoFar != S)
  2776. Subs.push_back(S);
  2777. continue;
  2778. }
  2779. // Parse an <unqualified-name> thats actually a <ctor-dtor-name>.
  2780. if (look() == 'C' || (look() == 'D' && look(1) != 'C')) {
  2781. if (SoFar == nullptr)
  2782. return nullptr;
  2783. if (!PushComponent(getDerived().parseCtorDtorName(SoFar, State)))
  2784. return nullptr;
  2785. SoFar = getDerived().parseAbiTags(SoFar);
  2786. if (SoFar == nullptr)
  2787. return nullptr;
  2788. Subs.push_back(SoFar);
  2789. continue;
  2790. }
  2791. // ::= <prefix> <unqualified-name>
  2792. if (!PushComponent(getDerived().parseUnqualifiedName(State)))
  2793. return nullptr;
  2794. Subs.push_back(SoFar);
  2795. }
  2796. if (SoFar == nullptr || Subs.empty())
  2797. return nullptr;
  2798. Subs.pop_back();
  2799. return SoFar;
  2800. }
  2801. // <simple-id> ::= <source-name> [ <template-args> ]
  2802. template <typename Derived, typename Alloc>
  2803. Node *AbstractManglingParser<Derived, Alloc>::parseSimpleId() {
  2804. Node *SN = getDerived().parseSourceName(/*NameState=*/nullptr);
  2805. if (SN == nullptr)
  2806. return nullptr;
  2807. if (look() == 'I') {
  2808. Node *TA = getDerived().parseTemplateArgs();
  2809. if (TA == nullptr)
  2810. return nullptr;
  2811. return make<NameWithTemplateArgs>(SN, TA);
  2812. }
  2813. return SN;
  2814. }
  2815. // <destructor-name> ::= <unresolved-type> # e.g., ~T or ~decltype(f())
  2816. // ::= <simple-id> # e.g., ~A<2*N>
  2817. template <typename Derived, typename Alloc>
  2818. Node *AbstractManglingParser<Derived, Alloc>::parseDestructorName() {
  2819. Node *Result;
  2820. if (std::isdigit(look()))
  2821. Result = getDerived().parseSimpleId();
  2822. else
  2823. Result = getDerived().parseUnresolvedType();
  2824. if (Result == nullptr)
  2825. return nullptr;
  2826. return make<DtorName>(Result);
  2827. }
  2828. // <unresolved-type> ::= <template-param>
  2829. // ::= <decltype>
  2830. // ::= <substitution>
  2831. template <typename Derived, typename Alloc>
  2832. Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedType() {
  2833. if (look() == 'T') {
  2834. Node *TP = getDerived().parseTemplateParam();
  2835. if (TP == nullptr)
  2836. return nullptr;
  2837. Subs.push_back(TP);
  2838. return TP;
  2839. }
  2840. if (look() == 'D') {
  2841. Node *DT = getDerived().parseDecltype();
  2842. if (DT == nullptr)
  2843. return nullptr;
  2844. Subs.push_back(DT);
  2845. return DT;
  2846. }
  2847. return getDerived().parseSubstitution();
  2848. }
  2849. // <base-unresolved-name> ::= <simple-id> # unresolved name
  2850. // extension ::= <operator-name> # unresolved operator-function-id
  2851. // extension ::= <operator-name> <template-args> # unresolved operator template-id
  2852. // ::= on <operator-name> # unresolved operator-function-id
  2853. // ::= on <operator-name> <template-args> # unresolved operator template-id
  2854. // ::= dn <destructor-name> # destructor or pseudo-destructor;
  2855. // # e.g. ~X or ~X<N-1>
  2856. template <typename Derived, typename Alloc>
  2857. Node *AbstractManglingParser<Derived, Alloc>::parseBaseUnresolvedName() {
  2858. if (std::isdigit(look()))
  2859. return getDerived().parseSimpleId();
  2860. if (consumeIf("dn"))
  2861. return getDerived().parseDestructorName();
  2862. consumeIf("on");
  2863. Node *Oper = getDerived().parseOperatorName(/*NameState=*/nullptr);
  2864. if (Oper == nullptr)
  2865. return nullptr;
  2866. if (look() == 'I') {
  2867. Node *TA = getDerived().parseTemplateArgs();
  2868. if (TA == nullptr)
  2869. return nullptr;
  2870. return make<NameWithTemplateArgs>(Oper, TA);
  2871. }
  2872. return Oper;
  2873. }
  2874. // <unresolved-name>
  2875. // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
  2876. // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
  2877. // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
  2878. // # A::x, N::y, A<T>::z; "gs" means leading "::"
  2879. // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
  2880. // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name>
  2881. // # T::N::x /decltype(p)::N::x
  2882. // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
  2883. //
  2884. // <unresolved-qualifier-level> ::= <simple-id>
  2885. template <typename Derived, typename Alloc>
  2886. Node *AbstractManglingParser<Derived, Alloc>::parseUnresolvedName() {
  2887. Node *SoFar = nullptr;
  2888. // srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name>
  2889. // srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
  2890. if (consumeIf("srN")) {
  2891. SoFar = getDerived().parseUnresolvedType();
  2892. if (SoFar == nullptr)
  2893. return nullptr;
  2894. if (look() == 'I') {
  2895. Node *TA = getDerived().parseTemplateArgs();
  2896. if (TA == nullptr)
  2897. return nullptr;
  2898. SoFar = make<NameWithTemplateArgs>(SoFar, TA);
  2899. if (!SoFar)
  2900. return nullptr;
  2901. }
  2902. while (!consumeIf('E')) {
  2903. Node *Qual = getDerived().parseSimpleId();
  2904. if (Qual == nullptr)
  2905. return nullptr;
  2906. SoFar = make<QualifiedName>(SoFar, Qual);
  2907. if (!SoFar)
  2908. return nullptr;
  2909. }
  2910. Node *Base = getDerived().parseBaseUnresolvedName();
  2911. if (Base == nullptr)
  2912. return nullptr;
  2913. return make<QualifiedName>(SoFar, Base);
  2914. }
  2915. bool Global = consumeIf("gs");
  2916. // [gs] <base-unresolved-name> # x or (with "gs") ::x
  2917. if (!consumeIf("sr")) {
  2918. SoFar = getDerived().parseBaseUnresolvedName();
  2919. if (SoFar == nullptr)
  2920. return nullptr;
  2921. if (Global)
  2922. SoFar = make<GlobalQualifiedName>(SoFar);
  2923. return SoFar;
  2924. }
  2925. // [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
  2926. if (std::isdigit(look())) {
  2927. do {
  2928. Node *Qual = getDerived().parseSimpleId();
  2929. if (Qual == nullptr)
  2930. return nullptr;
  2931. if (SoFar)
  2932. SoFar = make<QualifiedName>(SoFar, Qual);
  2933. else if (Global)
  2934. SoFar = make<GlobalQualifiedName>(Qual);
  2935. else
  2936. SoFar = Qual;
  2937. if (!SoFar)
  2938. return nullptr;
  2939. } while (!consumeIf('E'));
  2940. }
  2941. // sr <unresolved-type> <base-unresolved-name>
  2942. // sr <unresolved-type> <template-args> <base-unresolved-name>
  2943. else {
  2944. SoFar = getDerived().parseUnresolvedType();
  2945. if (SoFar == nullptr)
  2946. return nullptr;
  2947. if (look() == 'I') {
  2948. Node *TA = getDerived().parseTemplateArgs();
  2949. if (TA == nullptr)
  2950. return nullptr;
  2951. SoFar = make<NameWithTemplateArgs>(SoFar, TA);
  2952. if (!SoFar)
  2953. return nullptr;
  2954. }
  2955. }
  2956. assert(SoFar != nullptr);
  2957. Node *Base = getDerived().parseBaseUnresolvedName();
  2958. if (Base == nullptr)
  2959. return nullptr;
  2960. return make<QualifiedName>(SoFar, Base);
  2961. }
  2962. // <abi-tags> ::= <abi-tag> [<abi-tags>]
  2963. // <abi-tag> ::= B <source-name>
  2964. template <typename Derived, typename Alloc>
  2965. Node *AbstractManglingParser<Derived, Alloc>::parseAbiTags(Node *N) {
  2966. while (consumeIf('B')) {
  2967. StringView SN = parseBareSourceName();
  2968. if (SN.empty())
  2969. return nullptr;
  2970. N = make<AbiTagAttr>(N, SN);
  2971. if (!N)
  2972. return nullptr;
  2973. }
  2974. return N;
  2975. }
  2976. // <number> ::= [n] <non-negative decimal integer>
  2977. template <typename Alloc, typename Derived>
  2978. StringView
  2979. AbstractManglingParser<Alloc, Derived>::parseNumber(bool AllowNegative) {
  2980. const char *Tmp = First;
  2981. if (AllowNegative)
  2982. consumeIf('n');
  2983. if (numLeft() == 0 || !std::isdigit(*First))
  2984. return StringView();
  2985. while (numLeft() != 0 && std::isdigit(*First))
  2986. ++First;
  2987. return StringView(Tmp, First);
  2988. }
  2989. // <positive length number> ::= [0-9]*
  2990. template <typename Alloc, typename Derived>
  2991. bool AbstractManglingParser<Alloc, Derived>::parsePositiveInteger(size_t *Out) {
  2992. *Out = 0;
  2993. if (look() < '0' || look() > '9')
  2994. return true;
  2995. while (look() >= '0' && look() <= '9') {
  2996. *Out *= 10;
  2997. *Out += static_cast<size_t>(consume() - '0');
  2998. }
  2999. return false;
  3000. }
  3001. template <typename Alloc, typename Derived>
  3002. StringView AbstractManglingParser<Alloc, Derived>::parseBareSourceName() {
  3003. size_t Int = 0;
  3004. if (parsePositiveInteger(&Int) || numLeft() < Int)
  3005. return StringView();
  3006. StringView R(First, First + Int);
  3007. First += Int;
  3008. return R;
  3009. }
  3010. // <function-type> ::= [<CV-qualifiers>] [<exception-spec>] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E
  3011. //
  3012. // <exception-spec> ::= Do # non-throwing exception-specification (e.g., noexcept, throw())
  3013. // ::= DO <expression> E # computed (instantiation-dependent) noexcept
  3014. // ::= Dw <type>+ E # dynamic exception specification with instantiation-dependent types
  3015. //
  3016. // <ref-qualifier> ::= R # & ref-qualifier
  3017. // <ref-qualifier> ::= O # && ref-qualifier
  3018. template <typename Derived, typename Alloc>
  3019. Node *AbstractManglingParser<Derived, Alloc>::parseFunctionType() {
  3020. Qualifiers CVQuals = parseCVQualifiers();
  3021. Node *ExceptionSpec = nullptr;
  3022. if (consumeIf("Do")) {
  3023. ExceptionSpec = make<NameType>("noexcept");
  3024. if (!ExceptionSpec)
  3025. return nullptr;
  3026. } else if (consumeIf("DO")) {
  3027. Node *E = getDerived().parseExpr();
  3028. if (E == nullptr || !consumeIf('E'))
  3029. return nullptr;
  3030. ExceptionSpec = make<NoexceptSpec>(E);
  3031. if (!ExceptionSpec)
  3032. return nullptr;
  3033. } else if (consumeIf("Dw")) {
  3034. size_t SpecsBegin = Names.size();
  3035. while (!consumeIf('E')) {
  3036. Node *T = getDerived().parseType();
  3037. if (T == nullptr)
  3038. return nullptr;
  3039. Names.push_back(T);
  3040. }
  3041. ExceptionSpec =
  3042. make<DynamicExceptionSpec>(popTrailingNodeArray(SpecsBegin));
  3043. if (!ExceptionSpec)
  3044. return nullptr;
  3045. }
  3046. consumeIf("Dx"); // transaction safe
  3047. if (!consumeIf('F'))
  3048. return nullptr;
  3049. consumeIf('Y'); // extern "C"
  3050. Node *ReturnType = getDerived().parseType();
  3051. if (ReturnType == nullptr)
  3052. return nullptr;
  3053. FunctionRefQual ReferenceQualifier = FrefQualNone;
  3054. size_t ParamsBegin = Names.size();
  3055. while (true) {
  3056. if (consumeIf('E'))
  3057. break;
  3058. if (consumeIf('v'))
  3059. continue;
  3060. if (consumeIf("RE")) {
  3061. ReferenceQualifier = FrefQualLValue;
  3062. break;
  3063. }
  3064. if (consumeIf("OE")) {
  3065. ReferenceQualifier = FrefQualRValue;
  3066. break;
  3067. }
  3068. Node *T = getDerived().parseType();
  3069. if (T == nullptr)
  3070. return nullptr;
  3071. Names.push_back(T);
  3072. }
  3073. NodeArray Params = popTrailingNodeArray(ParamsBegin);
  3074. return make<FunctionType>(ReturnType, Params, CVQuals,
  3075. ReferenceQualifier, ExceptionSpec);
  3076. }
  3077. // extension:
  3078. // <vector-type> ::= Dv <positive dimension number> _ <extended element type>
  3079. // ::= Dv [<dimension expression>] _ <element type>
  3080. // <extended element type> ::= <element type>
  3081. // ::= p # AltiVec vector pixel
  3082. template <typename Derived, typename Alloc>
  3083. Node *AbstractManglingParser<Derived, Alloc>::parseVectorType() {
  3084. if (!consumeIf("Dv"))
  3085. return nullptr;
  3086. if (look() >= '1' && look() <= '9') {
  3087. Node *DimensionNumber = make<NameType>(parseNumber());
  3088. if (!DimensionNumber)
  3089. return nullptr;
  3090. if (!consumeIf('_'))
  3091. return nullptr;
  3092. if (consumeIf('p'))
  3093. return make<PixelVectorType>(DimensionNumber);
  3094. Node *ElemType = getDerived().parseType();
  3095. if (ElemType == nullptr)
  3096. return nullptr;
  3097. return make<VectorType>(ElemType, DimensionNumber);
  3098. }
  3099. if (!consumeIf('_')) {
  3100. Node *DimExpr = getDerived().parseExpr();
  3101. if (!DimExpr)
  3102. return nullptr;
  3103. if (!consumeIf('_'))
  3104. return nullptr;
  3105. Node *ElemType = getDerived().parseType();
  3106. if (!ElemType)
  3107. return nullptr;
  3108. return make<VectorType>(ElemType, DimExpr);
  3109. }
  3110. Node *ElemType = getDerived().parseType();
  3111. if (!ElemType)
  3112. return nullptr;
  3113. return make<VectorType>(ElemType, /*Dimension=*/nullptr);
  3114. }
  3115. // <decltype> ::= Dt <expression> E # decltype of an id-expression or class member access (C++0x)
  3116. // ::= DT <expression> E # decltype of an expression (C++0x)
  3117. template <typename Derived, typename Alloc>
  3118. Node *AbstractManglingParser<Derived, Alloc>::parseDecltype() {
  3119. if (!consumeIf('D'))
  3120. return nullptr;
  3121. if (!consumeIf('t') && !consumeIf('T'))
  3122. return nullptr;
  3123. Node *E = getDerived().parseExpr();
  3124. if (E == nullptr)
  3125. return nullptr;
  3126. if (!consumeIf('E'))
  3127. return nullptr;
  3128. return make<EnclosingExpr>("decltype(", E, ")");
  3129. }
  3130. // <array-type> ::= A <positive dimension number> _ <element type>
  3131. // ::= A [<dimension expression>] _ <element type>
  3132. template <typename Derived, typename Alloc>
  3133. Node *AbstractManglingParser<Derived, Alloc>::parseArrayType() {
  3134. if (!consumeIf('A'))
  3135. return nullptr;
  3136. Node *Dimension = nullptr;
  3137. if (std::isdigit(look())) {
  3138. Dimension = make<NameType>(parseNumber());
  3139. if (!Dimension)
  3140. return nullptr;
  3141. if (!consumeIf('_'))
  3142. return nullptr;
  3143. } else if (!consumeIf('_')) {
  3144. Node *DimExpr = getDerived().parseExpr();
  3145. if (DimExpr == nullptr)
  3146. return nullptr;
  3147. if (!consumeIf('_'))
  3148. return nullptr;
  3149. Dimension = DimExpr;
  3150. }
  3151. Node *Ty = getDerived().parseType();
  3152. if (Ty == nullptr)
  3153. return nullptr;
  3154. return make<ArrayType>(Ty, Dimension);
  3155. }
  3156. // <pointer-to-member-type> ::= M <class type> <member type>
  3157. template <typename Derived, typename Alloc>
  3158. Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberType() {
  3159. if (!consumeIf('M'))
  3160. return nullptr;
  3161. Node *ClassType = getDerived().parseType();
  3162. if (ClassType == nullptr)
  3163. return nullptr;
  3164. Node *MemberType = getDerived().parseType();
  3165. if (MemberType == nullptr)
  3166. return nullptr;
  3167. return make<PointerToMemberType>(ClassType, MemberType);
  3168. }
  3169. // <class-enum-type> ::= <name> # non-dependent type name, dependent type name, or dependent typename-specifier
  3170. // ::= Ts <name> # dependent elaborated type specifier using 'struct' or 'class'
  3171. // ::= Tu <name> # dependent elaborated type specifier using 'union'
  3172. // ::= Te <name> # dependent elaborated type specifier using 'enum'
  3173. template <typename Derived, typename Alloc>
  3174. Node *AbstractManglingParser<Derived, Alloc>::parseClassEnumType() {
  3175. StringView ElabSpef;
  3176. if (consumeIf("Ts"))
  3177. ElabSpef = "struct";
  3178. else if (consumeIf("Tu"))
  3179. ElabSpef = "union";
  3180. else if (consumeIf("Te"))
  3181. ElabSpef = "enum";
  3182. Node *Name = getDerived().parseName();
  3183. if (Name == nullptr)
  3184. return nullptr;
  3185. if (!ElabSpef.empty())
  3186. return make<ElaboratedTypeSpefType>(ElabSpef, Name);
  3187. return Name;
  3188. }
  3189. // <qualified-type> ::= <qualifiers> <type>
  3190. // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers>
  3191. // <extended-qualifier> ::= U <source-name> [<template-args>] # vendor extended type qualifier
  3192. template <typename Derived, typename Alloc>
  3193. Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
  3194. if (consumeIf('U')) {
  3195. StringView Qual = parseBareSourceName();
  3196. if (Qual.empty())
  3197. return nullptr;
  3198. // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
  3199. if (Qual.startsWith("objcproto")) {
  3200. StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto"));
  3201. StringView Proto;
  3202. {
  3203. SwapAndRestore<const char *> SaveFirst(First, ProtoSourceName.begin()),
  3204. SaveLast(Last, ProtoSourceName.end());
  3205. Proto = parseBareSourceName();
  3206. }
  3207. if (Proto.empty())
  3208. return nullptr;
  3209. Node *Child = getDerived().parseQualifiedType();
  3210. if (Child == nullptr)
  3211. return nullptr;
  3212. return make<ObjCProtoName>(Child, Proto);
  3213. }
  3214. Node *TA = nullptr;
  3215. if (look() == 'I') {
  3216. TA = getDerived().parseTemplateArgs();
  3217. if (TA == nullptr)
  3218. return nullptr;
  3219. }
  3220. Node *Child = getDerived().parseQualifiedType();
  3221. if (Child == nullptr)
  3222. return nullptr;
  3223. return make<VendorExtQualType>(Child, Qual, TA);
  3224. }
  3225. Qualifiers Quals = parseCVQualifiers();
  3226. Node *Ty = getDerived().parseType();
  3227. if (Ty == nullptr)
  3228. return nullptr;
  3229. if (Quals != QualNone)
  3230. Ty = make<QualType>(Ty, Quals);
  3231. return Ty;
  3232. }
  3233. // <type> ::= <builtin-type>
  3234. // ::= <qualified-type>
  3235. // ::= <function-type>
  3236. // ::= <class-enum-type>
  3237. // ::= <array-type>
  3238. // ::= <pointer-to-member-type>
  3239. // ::= <template-param>
  3240. // ::= <template-template-param> <template-args>
  3241. // ::= <decltype>
  3242. // ::= P <type> # pointer
  3243. // ::= R <type> # l-value reference
  3244. // ::= O <type> # r-value reference (C++11)
  3245. // ::= C <type> # complex pair (C99)
  3246. // ::= G <type> # imaginary (C99)
  3247. // ::= <substitution> # See Compression below
  3248. // extension ::= U <objc-name> <objc-type> # objc-type<identifier>
  3249. // extension ::= <vector-type> # <vector-type> starts with Dv
  3250. //
  3251. // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1
  3252. // <objc-type> ::= <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name>
  3253. template <typename Derived, typename Alloc>
  3254. Node *AbstractManglingParser<Derived, Alloc>::parseType() {
  3255. Node *Result = nullptr;
  3256. switch (look()) {
  3257. // ::= <qualified-type>
  3258. case 'r':
  3259. case 'V':
  3260. case 'K': {
  3261. unsigned AfterQuals = 0;
  3262. if (look(AfterQuals) == 'r') ++AfterQuals;
  3263. if (look(AfterQuals) == 'V') ++AfterQuals;
  3264. if (look(AfterQuals) == 'K') ++AfterQuals;
  3265. if (look(AfterQuals) == 'F' ||
  3266. (look(AfterQuals) == 'D' &&
  3267. (look(AfterQuals + 1) == 'o' || look(AfterQuals + 1) == 'O' ||
  3268. look(AfterQuals + 1) == 'w' || look(AfterQuals + 1) == 'x'))) {
  3269. Result = getDerived().parseFunctionType();
  3270. break;
  3271. }
  3272. DEMANGLE_FALLTHROUGH;
  3273. }
  3274. case 'U': {
  3275. Result = getDerived().parseQualifiedType();
  3276. break;
  3277. }
  3278. // <builtin-type> ::= v # void
  3279. case 'v':
  3280. ++First;
  3281. return make<NameType>("void");
  3282. // ::= w # wchar_t
  3283. case 'w':
  3284. ++First;
  3285. return make<NameType>("wchar_t");
  3286. // ::= b # bool
  3287. case 'b':
  3288. ++First;
  3289. return make<NameType>("bool");
  3290. // ::= c # char
  3291. case 'c':
  3292. ++First;
  3293. return make<NameType>("char");
  3294. // ::= a # signed char
  3295. case 'a':
  3296. ++First;
  3297. return make<NameType>("signed char");
  3298. // ::= h # unsigned char
  3299. case 'h':
  3300. ++First;
  3301. return make<NameType>("unsigned char");
  3302. // ::= s # short
  3303. case 's':
  3304. ++First;
  3305. return make<NameType>("short");
  3306. // ::= t # unsigned short
  3307. case 't':
  3308. ++First;
  3309. return make<NameType>("unsigned short");
  3310. // ::= i # int
  3311. case 'i':
  3312. ++First;
  3313. return make<NameType>("int");
  3314. // ::= j # unsigned int
  3315. case 'j':
  3316. ++First;
  3317. return make<NameType>("unsigned int");
  3318. // ::= l # long
  3319. case 'l':
  3320. ++First;
  3321. return make<NameType>("long");
  3322. // ::= m # unsigned long
  3323. case 'm':
  3324. ++First;
  3325. return make<NameType>("unsigned long");
  3326. // ::= x # long long, __int64
  3327. case 'x':
  3328. ++First;
  3329. return make<NameType>("long long");
  3330. // ::= y # unsigned long long, __int64
  3331. case 'y':
  3332. ++First;
  3333. return make<NameType>("unsigned long long");
  3334. // ::= n # __int128
  3335. case 'n':
  3336. ++First;
  3337. return make<NameType>("__int128");
  3338. // ::= o # unsigned __int128
  3339. case 'o':
  3340. ++First;
  3341. return make<NameType>("unsigned __int128");
  3342. // ::= f # float
  3343. case 'f':
  3344. ++First;
  3345. return make<NameType>("float");
  3346. // ::= d # double
  3347. case 'd':
  3348. ++First;
  3349. return make<NameType>("double");
  3350. // ::= e # long double, __float80
  3351. case 'e':
  3352. ++First;
  3353. return make<NameType>("long double");
  3354. // ::= g # __float128
  3355. case 'g':
  3356. ++First;
  3357. return make<NameType>("__float128");
  3358. // ::= z # ellipsis
  3359. case 'z':
  3360. ++First;
  3361. return make<NameType>("...");
  3362. // <builtin-type> ::= u <source-name> # vendor extended type
  3363. case 'u': {
  3364. ++First;
  3365. StringView Res = parseBareSourceName();
  3366. if (Res.empty())
  3367. return nullptr;
  3368. // Typically, <builtin-type>s are not considered substitution candidates,
  3369. // but the exception to that exception is vendor extended types (Itanium C++
  3370. // ABI 5.9.1).
  3371. Result = make<NameType>(Res);
  3372. break;
  3373. }
  3374. case 'D':
  3375. switch (look(1)) {
  3376. // ::= Dd # IEEE 754r decimal floating point (64 bits)
  3377. case 'd':
  3378. First += 2;
  3379. return make<NameType>("decimal64");
  3380. // ::= De # IEEE 754r decimal floating point (128 bits)
  3381. case 'e':
  3382. First += 2;
  3383. return make<NameType>("decimal128");
  3384. // ::= Df # IEEE 754r decimal floating point (32 bits)
  3385. case 'f':
  3386. First += 2;
  3387. return make<NameType>("decimal32");
  3388. // ::= Dh # IEEE 754r half-precision floating point (16 bits)
  3389. case 'h':
  3390. First += 2;
  3391. return make<NameType>("half");
  3392. // ::= DF <number> _ # ISO/IEC TS 18661 binary floating point (N bits)
  3393. case 'F': {
  3394. First += 2;
  3395. Node *DimensionNumber = make<NameType>(parseNumber());
  3396. if (!DimensionNumber)
  3397. return nullptr;
  3398. if (!consumeIf('_'))
  3399. return nullptr;
  3400. return make<BinaryFPType>(DimensionNumber);
  3401. }
  3402. // ::= Di # char32_t
  3403. case 'i':
  3404. First += 2;
  3405. return make<NameType>("char32_t");
  3406. // ::= Ds # char16_t
  3407. case 's':
  3408. First += 2;
  3409. return make<NameType>("char16_t");
  3410. // ::= Du # char8_t (C++2a, not yet in the Itanium spec)
  3411. case 'u':
  3412. First += 2;
  3413. return make<NameType>("char8_t");
  3414. // ::= Da # auto (in dependent new-expressions)
  3415. case 'a':
  3416. First += 2;
  3417. return make<NameType>("auto");
  3418. // ::= Dc # decltype(auto)
  3419. case 'c':
  3420. First += 2;
  3421. return make<NameType>("decltype(auto)");
  3422. // ::= Dn # std::nullptr_t (i.e., decltype(nullptr))
  3423. case 'n':
  3424. First += 2;
  3425. return make<NameType>("std::nullptr_t");
  3426. // ::= <decltype>
  3427. case 't':
  3428. case 'T': {
  3429. Result = getDerived().parseDecltype();
  3430. break;
  3431. }
  3432. // extension ::= <vector-type> # <vector-type> starts with Dv
  3433. case 'v': {
  3434. Result = getDerived().parseVectorType();
  3435. break;
  3436. }
  3437. // ::= Dp <type> # pack expansion (C++0x)
  3438. case 'p': {
  3439. First += 2;
  3440. Node *Child = getDerived().parseType();
  3441. if (!Child)
  3442. return nullptr;
  3443. Result = make<ParameterPackExpansion>(Child);
  3444. break;
  3445. }
  3446. // Exception specifier on a function type.
  3447. case 'o':
  3448. case 'O':
  3449. case 'w':
  3450. // Transaction safe function type.
  3451. case 'x':
  3452. Result = getDerived().parseFunctionType();
  3453. break;
  3454. }
  3455. break;
  3456. // ::= <function-type>
  3457. case 'F': {
  3458. Result = getDerived().parseFunctionType();
  3459. break;
  3460. }
  3461. // ::= <array-type>
  3462. case 'A': {
  3463. Result = getDerived().parseArrayType();
  3464. break;
  3465. }
  3466. // ::= <pointer-to-member-type>
  3467. case 'M': {
  3468. Result = getDerived().parsePointerToMemberType();
  3469. break;
  3470. }
  3471. // ::= <template-param>
  3472. case 'T': {
  3473. // This could be an elaborate type specifier on a <class-enum-type>.
  3474. if (look(1) == 's' || look(1) == 'u' || look(1) == 'e') {
  3475. Result = getDerived().parseClassEnumType();
  3476. break;
  3477. }
  3478. Result = getDerived().parseTemplateParam();
  3479. if (Result == nullptr)
  3480. return nullptr;
  3481. // Result could be either of:
  3482. // <type> ::= <template-param>
  3483. // <type> ::= <template-template-param> <template-args>
  3484. //
  3485. // <template-template-param> ::= <template-param>
  3486. // ::= <substitution>
  3487. //
  3488. // If this is followed by some <template-args>, and we're permitted to
  3489. // parse them, take the second production.
  3490. if (TryToParseTemplateArgs && look() == 'I') {
  3491. Node *TA = getDerived().parseTemplateArgs();
  3492. if (TA == nullptr)
  3493. return nullptr;
  3494. Result = make<NameWithTemplateArgs>(Result, TA);
  3495. }
  3496. break;
  3497. }
  3498. // ::= P <type> # pointer
  3499. case 'P': {
  3500. ++First;
  3501. Node *Ptr = getDerived().parseType();
  3502. if (Ptr == nullptr)
  3503. return nullptr;
  3504. Result = make<PointerType>(Ptr);
  3505. break;
  3506. }
  3507. // ::= R <type> # l-value reference
  3508. case 'R': {
  3509. ++First;
  3510. Node *Ref = getDerived().parseType();
  3511. if (Ref == nullptr)
  3512. return nullptr;
  3513. Result = make<ReferenceType>(Ref, ReferenceKind::LValue);
  3514. break;
  3515. }
  3516. // ::= O <type> # r-value reference (C++11)
  3517. case 'O': {
  3518. ++First;
  3519. Node *Ref = getDerived().parseType();
  3520. if (Ref == nullptr)
  3521. return nullptr;
  3522. Result = make<ReferenceType>(Ref, ReferenceKind::RValue);
  3523. break;
  3524. }
  3525. // ::= C <type> # complex pair (C99)
  3526. case 'C': {
  3527. ++First;
  3528. Node *P = getDerived().parseType();
  3529. if (P == nullptr)
  3530. return nullptr;
  3531. Result = make<PostfixQualifiedType>(P, " complex");
  3532. break;
  3533. }
  3534. // ::= G <type> # imaginary (C99)
  3535. case 'G': {
  3536. ++First;
  3537. Node *P = getDerived().parseType();
  3538. if (P == nullptr)
  3539. return P;
  3540. Result = make<PostfixQualifiedType>(P, " imaginary");
  3541. break;
  3542. }
  3543. // ::= <substitution> # See Compression below
  3544. case 'S': {
  3545. if (look(1) != 't') {
  3546. Result = getDerived().parseSubstitution();
  3547. if (Result == nullptr)
  3548. return nullptr;
  3549. // Sub could be either of:
  3550. // <type> ::= <substitution>
  3551. // <type> ::= <template-template-param> <template-args>
  3552. //
  3553. // <template-template-param> ::= <template-param>
  3554. // ::= <substitution>
  3555. //
  3556. // If this is followed by some <template-args>, and we're permitted to
  3557. // parse them, take the second production.
  3558. if (TryToParseTemplateArgs && look() == 'I') {
  3559. Node *TA = getDerived().parseTemplateArgs();
  3560. if (TA == nullptr)
  3561. return nullptr;
  3562. Result = make<NameWithTemplateArgs>(Result, TA);
  3563. } else {
  3564. // If all we parsed was a substitution, don't re-insert into the
  3565. // substitution table.
  3566. return Result;
  3567. }
  3568. break;
  3569. }
  3570. DEMANGLE_FALLTHROUGH;
  3571. }
  3572. // ::= <class-enum-type>
  3573. default: {
  3574. Result = getDerived().parseClassEnumType();
  3575. break;
  3576. }
  3577. }
  3578. // If we parsed a type, insert it into the substitution table. Note that all
  3579. // <builtin-type>s and <substitution>s have already bailed out, because they
  3580. // don't get substitutions.
  3581. if (Result != nullptr)
  3582. Subs.push_back(Result);
  3583. return Result;
  3584. }
  3585. template <typename Derived, typename Alloc>
  3586. Node *AbstractManglingParser<Derived, Alloc>::parsePrefixExpr(StringView Kind) {
  3587. Node *E = getDerived().parseExpr();
  3588. if (E == nullptr)
  3589. return nullptr;
  3590. return make<PrefixExpr>(Kind, E);
  3591. }
  3592. template <typename Derived, typename Alloc>
  3593. Node *AbstractManglingParser<Derived, Alloc>::parseBinaryExpr(StringView Kind) {
  3594. Node *LHS = getDerived().parseExpr();
  3595. if (LHS == nullptr)
  3596. return nullptr;
  3597. Node *RHS = getDerived().parseExpr();
  3598. if (RHS == nullptr)
  3599. return nullptr;
  3600. return make<BinaryExpr>(LHS, Kind, RHS);
  3601. }
  3602. template <typename Derived, typename Alloc>
  3603. Node *
  3604. AbstractManglingParser<Derived, Alloc>::parseIntegerLiteral(StringView Lit) {
  3605. StringView Tmp = parseNumber(true);
  3606. if (!Tmp.empty() && consumeIf('E'))
  3607. return make<IntegerLiteral>(Lit, Tmp);
  3608. return nullptr;
  3609. }
  3610. // <CV-Qualifiers> ::= [r] [V] [K]
  3611. template <typename Alloc, typename Derived>
  3612. Qualifiers AbstractManglingParser<Alloc, Derived>::parseCVQualifiers() {
  3613. Qualifiers CVR = QualNone;
  3614. if (consumeIf('r'))
  3615. CVR |= QualRestrict;
  3616. if (consumeIf('V'))
  3617. CVR |= QualVolatile;
  3618. if (consumeIf('K'))
  3619. CVR |= QualConst;
  3620. return CVR;
  3621. }
  3622. // <function-param> ::= fp <top-level CV-Qualifiers> _ # L == 0, first parameter
  3623. // ::= fp <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L == 0, second and later parameters
  3624. // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> _ # L > 0, first parameter
  3625. // ::= fL <L-1 non-negative number> p <top-level CV-Qualifiers> <parameter-2 non-negative number> _ # L > 0, second and later parameters
  3626. // ::= fpT # 'this' expression (not part of standard?)
  3627. template <typename Derived, typename Alloc>
  3628. Node *AbstractManglingParser<Derived, Alloc>::parseFunctionParam() {
  3629. if (consumeIf("fpT"))
  3630. return make<NameType>("this");
  3631. if (consumeIf("fp")) {
  3632. parseCVQualifiers();
  3633. StringView Num = parseNumber();
  3634. if (!consumeIf('_'))
  3635. return nullptr;
  3636. return make<FunctionParam>(Num);
  3637. }
  3638. if (consumeIf("fL")) {
  3639. if (parseNumber().empty())
  3640. return nullptr;
  3641. if (!consumeIf('p'))
  3642. return nullptr;
  3643. parseCVQualifiers();
  3644. StringView Num = parseNumber();
  3645. if (!consumeIf('_'))
  3646. return nullptr;
  3647. return make<FunctionParam>(Num);
  3648. }
  3649. return nullptr;
  3650. }
  3651. // [gs] nw <expression>* _ <type> E # new (expr-list) type
  3652. // [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
  3653. // [gs] na <expression>* _ <type> E # new[] (expr-list) type
  3654. // [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
  3655. // <initializer> ::= pi <expression>* E # parenthesized initialization
  3656. template <typename Derived, typename Alloc>
  3657. Node *AbstractManglingParser<Derived, Alloc>::parseNewExpr() {
  3658. bool Global = consumeIf("gs");
  3659. bool IsArray = look(1) == 'a';
  3660. if (!consumeIf("nw") && !consumeIf("na"))
  3661. return nullptr;
  3662. size_t Exprs = Names.size();
  3663. while (!consumeIf('_')) {
  3664. Node *Ex = getDerived().parseExpr();
  3665. if (Ex == nullptr)
  3666. return nullptr;
  3667. Names.push_back(Ex);
  3668. }
  3669. NodeArray ExprList = popTrailingNodeArray(Exprs);
  3670. Node *Ty = getDerived().parseType();
  3671. if (Ty == nullptr)
  3672. return Ty;
  3673. if (consumeIf("pi")) {
  3674. size_t InitsBegin = Names.size();
  3675. while (!consumeIf('E')) {
  3676. Node *Init = getDerived().parseExpr();
  3677. if (Init == nullptr)
  3678. return Init;
  3679. Names.push_back(Init);
  3680. }
  3681. NodeArray Inits = popTrailingNodeArray(InitsBegin);
  3682. return make<NewExpr>(ExprList, Ty, Inits, Global, IsArray);
  3683. } else if (!consumeIf('E'))
  3684. return nullptr;
  3685. return make<NewExpr>(ExprList, Ty, NodeArray(), Global, IsArray);
  3686. }
  3687. // cv <type> <expression> # conversion with one argument
  3688. // cv <type> _ <expression>* E # conversion with a different number of arguments
  3689. template <typename Derived, typename Alloc>
  3690. Node *AbstractManglingParser<Derived, Alloc>::parseConversionExpr() {
  3691. if (!consumeIf("cv"))
  3692. return nullptr;
  3693. Node *Ty;
  3694. {
  3695. SwapAndRestore<bool> SaveTemp(TryToParseTemplateArgs, false);
  3696. Ty = getDerived().parseType();
  3697. }
  3698. if (Ty == nullptr)
  3699. return nullptr;
  3700. if (consumeIf('_')) {
  3701. size_t ExprsBegin = Names.size();
  3702. while (!consumeIf('E')) {
  3703. Node *E = getDerived().parseExpr();
  3704. if (E == nullptr)
  3705. return E;
  3706. Names.push_back(E);
  3707. }
  3708. NodeArray Exprs = popTrailingNodeArray(ExprsBegin);
  3709. return make<ConversionExpr>(Ty, Exprs);
  3710. }
  3711. Node *E[1] = {getDerived().parseExpr()};
  3712. if (E[0] == nullptr)
  3713. return nullptr;
  3714. return make<ConversionExpr>(Ty, makeNodeArray(E, E + 1));
  3715. }
  3716. // <expr-primary> ::= L <type> <value number> E # integer literal
  3717. // ::= L <type> <value float> E # floating literal
  3718. // ::= L <string type> E # string literal
  3719. // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE")
  3720. // ::= L <lambda type> E # lambda expression
  3721. // FIXME: ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000)
  3722. // ::= L <mangled-name> E # external name
  3723. template <typename Derived, typename Alloc>
  3724. Node *AbstractManglingParser<Derived, Alloc>::parseExprPrimary() {
  3725. if (!consumeIf('L'))
  3726. return nullptr;
  3727. switch (look()) {
  3728. case 'w':
  3729. ++First;
  3730. return getDerived().parseIntegerLiteral("wchar_t");
  3731. case 'b':
  3732. if (consumeIf("b0E"))
  3733. return make<BoolExpr>(0);
  3734. if (consumeIf("b1E"))
  3735. return make<BoolExpr>(1);
  3736. return nullptr;
  3737. case 'c':
  3738. ++First;
  3739. return getDerived().parseIntegerLiteral("char");
  3740. case 'a':
  3741. ++First;
  3742. return getDerived().parseIntegerLiteral("signed char");
  3743. case 'h':
  3744. ++First;
  3745. return getDerived().parseIntegerLiteral("unsigned char");
  3746. case 's':
  3747. ++First;
  3748. return getDerived().parseIntegerLiteral("short");
  3749. case 't':
  3750. ++First;
  3751. return getDerived().parseIntegerLiteral("unsigned short");
  3752. case 'i':
  3753. ++First;
  3754. return getDerived().parseIntegerLiteral("");
  3755. case 'j':
  3756. ++First;
  3757. return getDerived().parseIntegerLiteral("u");
  3758. case 'l':
  3759. ++First;
  3760. return getDerived().parseIntegerLiteral("l");
  3761. case 'm':
  3762. ++First;
  3763. return getDerived().parseIntegerLiteral("ul");
  3764. case 'x':
  3765. ++First;
  3766. return getDerived().parseIntegerLiteral("ll");
  3767. case 'y':
  3768. ++First;
  3769. return getDerived().parseIntegerLiteral("ull");
  3770. case 'n':
  3771. ++First;
  3772. return getDerived().parseIntegerLiteral("__int128");
  3773. case 'o':
  3774. ++First;
  3775. return getDerived().parseIntegerLiteral("unsigned __int128");
  3776. case 'f':
  3777. ++First;
  3778. return getDerived().template parseFloatingLiteral<float>();
  3779. case 'd':
  3780. ++First;
  3781. return getDerived().template parseFloatingLiteral<double>();
  3782. case 'e':
  3783. ++First;
  3784. #if defined(__powerpc__) || defined(__s390__)
  3785. // Handle cases where long doubles encoded with e have the same size
  3786. // and representation as doubles.
  3787. return getDerived().template parseFloatingLiteral<double>();
  3788. #else
  3789. return getDerived().template parseFloatingLiteral<long double>();
  3790. #endif
  3791. case '_':
  3792. if (consumeIf("_Z")) {
  3793. Node *R = getDerived().parseEncoding();
  3794. if (R != nullptr && consumeIf('E'))
  3795. return R;
  3796. }
  3797. return nullptr;
  3798. case 'A': {
  3799. Node *T = getDerived().parseType();
  3800. if (T == nullptr)
  3801. return nullptr;
  3802. // FIXME: We need to include the string contents in the mangling.
  3803. if (consumeIf('E'))
  3804. return make<StringLiteral>(T);
  3805. return nullptr;
  3806. }
  3807. case 'D':
  3808. if (consumeIf("DnE"))
  3809. return make<NameType>("nullptr");
  3810. return nullptr;
  3811. case 'T':
  3812. // Invalid mangled name per
  3813. // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html
  3814. return nullptr;
  3815. case 'U': {
  3816. // FIXME: Should we support LUb... for block literals?
  3817. if (look(1) != 'l')
  3818. return nullptr;
  3819. Node *T = parseUnnamedTypeName(nullptr);
  3820. if (!T || !consumeIf('E'))
  3821. return nullptr;
  3822. return make<LambdaExpr>(T);
  3823. }
  3824. default: {
  3825. // might be named type
  3826. Node *T = getDerived().parseType();
  3827. if (T == nullptr)
  3828. return nullptr;
  3829. StringView N = parseNumber(/*AllowNegative=*/true);
  3830. if (N.empty())
  3831. return nullptr;
  3832. if (!consumeIf('E'))
  3833. return nullptr;
  3834. return make<EnumLiteral>(T, N);
  3835. }
  3836. }
  3837. }
  3838. // <braced-expression> ::= <expression>
  3839. // ::= di <field source-name> <braced-expression> # .name = expr
  3840. // ::= dx <index expression> <braced-expression> # [expr] = expr
  3841. // ::= dX <range begin expression> <range end expression> <braced-expression>
  3842. template <typename Derived, typename Alloc>
  3843. Node *AbstractManglingParser<Derived, Alloc>::parseBracedExpr() {
  3844. if (look() == 'd') {
  3845. switch (look(1)) {
  3846. case 'i': {
  3847. First += 2;
  3848. Node *Field = getDerived().parseSourceName(/*NameState=*/nullptr);
  3849. if (Field == nullptr)
  3850. return nullptr;
  3851. Node *Init = getDerived().parseBracedExpr();
  3852. if (Init == nullptr)
  3853. return nullptr;
  3854. return make<BracedExpr>(Field, Init, /*isArray=*/false);
  3855. }
  3856. case 'x': {
  3857. First += 2;
  3858. Node *Index = getDerived().parseExpr();
  3859. if (Index == nullptr)
  3860. return nullptr;
  3861. Node *Init = getDerived().parseBracedExpr();
  3862. if (Init == nullptr)
  3863. return nullptr;
  3864. return make<BracedExpr>(Index, Init, /*isArray=*/true);
  3865. }
  3866. case 'X': {
  3867. First += 2;
  3868. Node *RangeBegin = getDerived().parseExpr();
  3869. if (RangeBegin == nullptr)
  3870. return nullptr;
  3871. Node *RangeEnd = getDerived().parseExpr();
  3872. if (RangeEnd == nullptr)
  3873. return nullptr;
  3874. Node *Init = getDerived().parseBracedExpr();
  3875. if (Init == nullptr)
  3876. return nullptr;
  3877. return make<BracedRangeExpr>(RangeBegin, RangeEnd, Init);
  3878. }
  3879. }
  3880. }
  3881. return getDerived().parseExpr();
  3882. }
  3883. // (not yet in the spec)
  3884. // <fold-expr> ::= fL <binary-operator-name> <expression> <expression>
  3885. // ::= fR <binary-operator-name> <expression> <expression>
  3886. // ::= fl <binary-operator-name> <expression>
  3887. // ::= fr <binary-operator-name> <expression>
  3888. template <typename Derived, typename Alloc>
  3889. Node *AbstractManglingParser<Derived, Alloc>::parseFoldExpr() {
  3890. if (!consumeIf('f'))
  3891. return nullptr;
  3892. char FoldKind = look();
  3893. bool IsLeftFold, HasInitializer;
  3894. HasInitializer = FoldKind == 'L' || FoldKind == 'R';
  3895. if (FoldKind == 'l' || FoldKind == 'L')
  3896. IsLeftFold = true;
  3897. else if (FoldKind == 'r' || FoldKind == 'R')
  3898. IsLeftFold = false;
  3899. else
  3900. return nullptr;
  3901. ++First;
  3902. // FIXME: This map is duplicated in parseOperatorName and parseExpr.
  3903. StringView OperatorName;
  3904. if (consumeIf("aa")) OperatorName = "&&";
  3905. else if (consumeIf("an")) OperatorName = "&";
  3906. else if (consumeIf("aN")) OperatorName = "&=";
  3907. else if (consumeIf("aS")) OperatorName = "=";
  3908. else if (consumeIf("cm")) OperatorName = ",";
  3909. else if (consumeIf("ds")) OperatorName = ".*";
  3910. else if (consumeIf("dv")) OperatorName = "/";
  3911. else if (consumeIf("dV")) OperatorName = "/=";
  3912. else if (consumeIf("eo")) OperatorName = "^";
  3913. else if (consumeIf("eO")) OperatorName = "^=";
  3914. else if (consumeIf("eq")) OperatorName = "==";
  3915. else if (consumeIf("ge")) OperatorName = ">=";
  3916. else if (consumeIf("gt")) OperatorName = ">";
  3917. else if (consumeIf("le")) OperatorName = "<=";
  3918. else if (consumeIf("ls")) OperatorName = "<<";
  3919. else if (consumeIf("lS")) OperatorName = "<<=";
  3920. else if (consumeIf("lt")) OperatorName = "<";
  3921. else if (consumeIf("mi")) OperatorName = "-";
  3922. else if (consumeIf("mI")) OperatorName = "-=";
  3923. else if (consumeIf("ml")) OperatorName = "*";
  3924. else if (consumeIf("mL")) OperatorName = "*=";
  3925. else if (consumeIf("ne")) OperatorName = "!=";
  3926. else if (consumeIf("oo")) OperatorName = "||";
  3927. else if (consumeIf("or")) OperatorName = "|";
  3928. else if (consumeIf("oR")) OperatorName = "|=";
  3929. else if (consumeIf("pl")) OperatorName = "+";
  3930. else if (consumeIf("pL")) OperatorName = "+=";
  3931. else if (consumeIf("rm")) OperatorName = "%";
  3932. else if (consumeIf("rM")) OperatorName = "%=";
  3933. else if (consumeIf("rs")) OperatorName = ">>";
  3934. else if (consumeIf("rS")) OperatorName = ">>=";
  3935. else return nullptr;
  3936. Node *Pack = getDerived().parseExpr(), *Init = nullptr;
  3937. if (Pack == nullptr)
  3938. return nullptr;
  3939. if (HasInitializer) {
  3940. Init = getDerived().parseExpr();
  3941. if (Init == nullptr)
  3942. return nullptr;
  3943. }
  3944. if (IsLeftFold && Init)
  3945. std::swap(Pack, Init);
  3946. return make<FoldExpr>(IsLeftFold, OperatorName, Pack, Init);
  3947. }
  3948. // <expression> ::= mc <parameter type> <expr> [<offset number>] E
  3949. //
  3950. // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
  3951. template <typename Derived, typename Alloc>
  3952. Node *AbstractManglingParser<Derived, Alloc>::parsePointerToMemberConversionExpr() {
  3953. Node *Ty = getDerived().parseType();
  3954. if (!Ty)
  3955. return nullptr;
  3956. Node *Expr = getDerived().parseExpr();
  3957. if (!Expr)
  3958. return nullptr;
  3959. StringView Offset = getDerived().parseNumber(true);
  3960. if (!consumeIf('E'))
  3961. return nullptr;
  3962. return make<PointerToMemberConversionExpr>(Ty, Expr, Offset);
  3963. }
  3964. // <expression> ::= so <referent type> <expr> [<offset number>] <union-selector>* [p] E
  3965. // <union-selector> ::= _ [<number>]
  3966. //
  3967. // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/47
  3968. template <typename Derived, typename Alloc>
  3969. Node *AbstractManglingParser<Derived, Alloc>::parseSubobjectExpr() {
  3970. Node *Ty = getDerived().parseType();
  3971. if (!Ty)
  3972. return nullptr;
  3973. Node *Expr = getDerived().parseExpr();
  3974. if (!Expr)
  3975. return nullptr;
  3976. StringView Offset = getDerived().parseNumber(true);
  3977. size_t SelectorsBegin = Names.size();
  3978. while (consumeIf('_')) {
  3979. Node *Selector = make<NameType>(parseNumber());
  3980. if (!Selector)
  3981. return nullptr;
  3982. Names.push_back(Selector);
  3983. }
  3984. bool OnePastTheEnd = consumeIf('p');
  3985. if (!consumeIf('E'))
  3986. return nullptr;
  3987. return make<SubobjectExpr>(
  3988. Ty, Expr, Offset, popTrailingNodeArray(SelectorsBegin), OnePastTheEnd);
  3989. }
  3990. // <expression> ::= <unary operator-name> <expression>
  3991. // ::= <binary operator-name> <expression> <expression>
  3992. // ::= <ternary operator-name> <expression> <expression> <expression>
  3993. // ::= cl <expression>+ E # call
  3994. // ::= cv <type> <expression> # conversion with one argument
  3995. // ::= cv <type> _ <expression>* E # conversion with a different number of arguments
  3996. // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type
  3997. // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init)
  3998. // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type
  3999. // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init)
  4000. // ::= [gs] dl <expression> # delete expression
  4001. // ::= [gs] da <expression> # delete[] expression
  4002. // ::= pp_ <expression> # prefix ++
  4003. // ::= mm_ <expression> # prefix --
  4004. // ::= ti <type> # typeid (type)
  4005. // ::= te <expression> # typeid (expression)
  4006. // ::= dc <type> <expression> # dynamic_cast<type> (expression)
  4007. // ::= sc <type> <expression> # static_cast<type> (expression)
  4008. // ::= cc <type> <expression> # const_cast<type> (expression)
  4009. // ::= rc <type> <expression> # reinterpret_cast<type> (expression)
  4010. // ::= st <type> # sizeof (a type)
  4011. // ::= sz <expression> # sizeof (an expression)
  4012. // ::= at <type> # alignof (a type)
  4013. // ::= az <expression> # alignof (an expression)
  4014. // ::= nx <expression> # noexcept (expression)
  4015. // ::= <template-param>
  4016. // ::= <function-param>
  4017. // ::= dt <expression> <unresolved-name> # expr.name
  4018. // ::= pt <expression> <unresolved-name> # expr->name
  4019. // ::= ds <expression> <expression> # expr.*expr
  4020. // ::= sZ <template-param> # size of a parameter pack
  4021. // ::= sZ <function-param> # size of a function parameter pack
  4022. // ::= sP <template-arg>* E # sizeof...(T), size of a captured template parameter pack from an alias template
  4023. // ::= sp <expression> # pack expansion
  4024. // ::= tw <expression> # throw expression
  4025. // ::= tr # throw with no operand (rethrow)
  4026. // ::= <unresolved-name> # f(p), N::f(p), ::f(p),
  4027. // # freestanding dependent name (e.g., T::x),
  4028. // # objectless nonstatic member reference
  4029. // ::= fL <binary-operator-name> <expression> <expression>
  4030. // ::= fR <binary-operator-name> <expression> <expression>
  4031. // ::= fl <binary-operator-name> <expression>
  4032. // ::= fr <binary-operator-name> <expression>
  4033. // ::= <expr-primary>
  4034. template <typename Derived, typename Alloc>
  4035. Node *AbstractManglingParser<Derived, Alloc>::parseExpr() {
  4036. bool Global = consumeIf("gs");
  4037. if (numLeft() < 2)
  4038. return nullptr;
  4039. switch (*First) {
  4040. case 'L':
  4041. return getDerived().parseExprPrimary();
  4042. case 'T':
  4043. return getDerived().parseTemplateParam();
  4044. case 'f': {
  4045. // Disambiguate a fold expression from a <function-param>.
  4046. if (look(1) == 'p' || (look(1) == 'L' && std::isdigit(look(2))))
  4047. return getDerived().parseFunctionParam();
  4048. return getDerived().parseFoldExpr();
  4049. }
  4050. case 'a':
  4051. switch (First[1]) {
  4052. case 'a':
  4053. First += 2;
  4054. return getDerived().parseBinaryExpr("&&");
  4055. case 'd':
  4056. First += 2;
  4057. return getDerived().parsePrefixExpr("&");
  4058. case 'n':
  4059. First += 2;
  4060. return getDerived().parseBinaryExpr("&");
  4061. case 'N':
  4062. First += 2;
  4063. return getDerived().parseBinaryExpr("&=");
  4064. case 'S':
  4065. First += 2;
  4066. return getDerived().parseBinaryExpr("=");
  4067. case 't': {
  4068. First += 2;
  4069. Node *Ty = getDerived().parseType();
  4070. if (Ty == nullptr)
  4071. return nullptr;
  4072. return make<EnclosingExpr>("alignof (", Ty, ")");
  4073. }
  4074. case 'z': {
  4075. First += 2;
  4076. Node *Ty = getDerived().parseExpr();
  4077. if (Ty == nullptr)
  4078. return nullptr;
  4079. return make<EnclosingExpr>("alignof (", Ty, ")");
  4080. }
  4081. }
  4082. return nullptr;
  4083. case 'c':
  4084. switch (First[1]) {
  4085. // cc <type> <expression> # const_cast<type>(expression)
  4086. case 'c': {
  4087. First += 2;
  4088. Node *Ty = getDerived().parseType();
  4089. if (Ty == nullptr)
  4090. return Ty;
  4091. Node *Ex = getDerived().parseExpr();
  4092. if (Ex == nullptr)
  4093. return Ex;
  4094. return make<CastExpr>("const_cast", Ty, Ex);
  4095. }
  4096. // cl <expression>+ E # call
  4097. case 'l': {
  4098. First += 2;
  4099. Node *Callee = getDerived().parseExpr();
  4100. if (Callee == nullptr)
  4101. return Callee;
  4102. size_t ExprsBegin = Names.size();
  4103. while (!consumeIf('E')) {
  4104. Node *E = getDerived().parseExpr();
  4105. if (E == nullptr)
  4106. return E;
  4107. Names.push_back(E);
  4108. }
  4109. return make<CallExpr>(Callee, popTrailingNodeArray(ExprsBegin));
  4110. }
  4111. case 'm':
  4112. First += 2;
  4113. return getDerived().parseBinaryExpr(",");
  4114. case 'o':
  4115. First += 2;
  4116. return getDerived().parsePrefixExpr("~");
  4117. case 'v':
  4118. return getDerived().parseConversionExpr();
  4119. }
  4120. return nullptr;
  4121. case 'd':
  4122. switch (First[1]) {
  4123. case 'a': {
  4124. First += 2;
  4125. Node *Ex = getDerived().parseExpr();
  4126. if (Ex == nullptr)
  4127. return Ex;
  4128. return make<DeleteExpr>(Ex, Global, /*is_array=*/true);
  4129. }
  4130. case 'c': {
  4131. First += 2;
  4132. Node *T = getDerived().parseType();
  4133. if (T == nullptr)
  4134. return T;
  4135. Node *Ex = getDerived().parseExpr();
  4136. if (Ex == nullptr)
  4137. return Ex;
  4138. return make<CastExpr>("dynamic_cast", T, Ex);
  4139. }
  4140. case 'e':
  4141. First += 2;
  4142. return getDerived().parsePrefixExpr("*");
  4143. case 'l': {
  4144. First += 2;
  4145. Node *E = getDerived().parseExpr();
  4146. if (E == nullptr)
  4147. return E;
  4148. return make<DeleteExpr>(E, Global, /*is_array=*/false);
  4149. }
  4150. case 'n':
  4151. return getDerived().parseUnresolvedName();
  4152. case 's': {
  4153. First += 2;
  4154. Node *LHS = getDerived().parseExpr();
  4155. if (LHS == nullptr)
  4156. return nullptr;
  4157. Node *RHS = getDerived().parseExpr();
  4158. if (RHS == nullptr)
  4159. return nullptr;
  4160. return make<MemberExpr>(LHS, ".*", RHS);
  4161. }
  4162. case 't': {
  4163. First += 2;
  4164. Node *LHS = getDerived().parseExpr();
  4165. if (LHS == nullptr)
  4166. return LHS;
  4167. Node *RHS = getDerived().parseExpr();
  4168. if (RHS == nullptr)
  4169. return nullptr;
  4170. return make<MemberExpr>(LHS, ".", RHS);
  4171. }
  4172. case 'v':
  4173. First += 2;
  4174. return getDerived().parseBinaryExpr("/");
  4175. case 'V':
  4176. First += 2;
  4177. return getDerived().parseBinaryExpr("/=");
  4178. }
  4179. return nullptr;
  4180. case 'e':
  4181. switch (First[1]) {
  4182. case 'o':
  4183. First += 2;
  4184. return getDerived().parseBinaryExpr("^");
  4185. case 'O':
  4186. First += 2;
  4187. return getDerived().parseBinaryExpr("^=");
  4188. case 'q':
  4189. First += 2;
  4190. return getDerived().parseBinaryExpr("==");
  4191. }
  4192. return nullptr;
  4193. case 'g':
  4194. switch (First[1]) {
  4195. case 'e':
  4196. First += 2;
  4197. return getDerived().parseBinaryExpr(">=");
  4198. case 't':
  4199. First += 2;
  4200. return getDerived().parseBinaryExpr(">");
  4201. }
  4202. return nullptr;
  4203. case 'i':
  4204. switch (First[1]) {
  4205. case 'x': {
  4206. First += 2;
  4207. Node *Base = getDerived().parseExpr();
  4208. if (Base == nullptr)
  4209. return nullptr;
  4210. Node *Index = getDerived().parseExpr();
  4211. if (Index == nullptr)
  4212. return Index;
  4213. return make<ArraySubscriptExpr>(Base, Index);
  4214. }
  4215. case 'l': {
  4216. First += 2;
  4217. size_t InitsBegin = Names.size();
  4218. while (!consumeIf('E')) {
  4219. Node *E = getDerived().parseBracedExpr();
  4220. if (E == nullptr)
  4221. return nullptr;
  4222. Names.push_back(E);
  4223. }
  4224. return make<InitListExpr>(nullptr, popTrailingNodeArray(InitsBegin));
  4225. }
  4226. }
  4227. return nullptr;
  4228. case 'l':
  4229. switch (First[1]) {
  4230. case 'e':
  4231. First += 2;
  4232. return getDerived().parseBinaryExpr("<=");
  4233. case 's':
  4234. First += 2;
  4235. return getDerived().parseBinaryExpr("<<");
  4236. case 'S':
  4237. First += 2;
  4238. return getDerived().parseBinaryExpr("<<=");
  4239. case 't':
  4240. First += 2;
  4241. return getDerived().parseBinaryExpr("<");
  4242. }
  4243. return nullptr;
  4244. case 'm':
  4245. switch (First[1]) {
  4246. case 'c':
  4247. First += 2;
  4248. return parsePointerToMemberConversionExpr();
  4249. case 'i':
  4250. First += 2;
  4251. return getDerived().parseBinaryExpr("-");
  4252. case 'I':
  4253. First += 2;
  4254. return getDerived().parseBinaryExpr("-=");
  4255. case 'l':
  4256. First += 2;
  4257. return getDerived().parseBinaryExpr("*");
  4258. case 'L':
  4259. First += 2;
  4260. return getDerived().parseBinaryExpr("*=");
  4261. case 'm':
  4262. First += 2;
  4263. if (consumeIf('_'))
  4264. return getDerived().parsePrefixExpr("--");
  4265. Node *Ex = getDerived().parseExpr();
  4266. if (Ex == nullptr)
  4267. return nullptr;
  4268. return make<PostfixExpr>(Ex, "--");
  4269. }
  4270. return nullptr;
  4271. case 'n':
  4272. switch (First[1]) {
  4273. case 'a':
  4274. case 'w':
  4275. return getDerived().parseNewExpr();
  4276. case 'e':
  4277. First += 2;
  4278. return getDerived().parseBinaryExpr("!=");
  4279. case 'g':
  4280. First += 2;
  4281. return getDerived().parsePrefixExpr("-");
  4282. case 't':
  4283. First += 2;
  4284. return getDerived().parsePrefixExpr("!");
  4285. case 'x':
  4286. First += 2;
  4287. Node *Ex = getDerived().parseExpr();
  4288. if (Ex == nullptr)
  4289. return Ex;
  4290. return make<EnclosingExpr>("noexcept (", Ex, ")");
  4291. }
  4292. return nullptr;
  4293. case 'o':
  4294. switch (First[1]) {
  4295. case 'n':
  4296. return getDerived().parseUnresolvedName();
  4297. case 'o':
  4298. First += 2;
  4299. return getDerived().parseBinaryExpr("||");
  4300. case 'r':
  4301. First += 2;
  4302. return getDerived().parseBinaryExpr("|");
  4303. case 'R':
  4304. First += 2;
  4305. return getDerived().parseBinaryExpr("|=");
  4306. }
  4307. return nullptr;
  4308. case 'p':
  4309. switch (First[1]) {
  4310. case 'm':
  4311. First += 2;
  4312. return getDerived().parseBinaryExpr("->*");
  4313. case 'l':
  4314. First += 2;
  4315. return getDerived().parseBinaryExpr("+");
  4316. case 'L':
  4317. First += 2;
  4318. return getDerived().parseBinaryExpr("+=");
  4319. case 'p': {
  4320. First += 2;
  4321. if (consumeIf('_'))
  4322. return getDerived().parsePrefixExpr("++");
  4323. Node *Ex = getDerived().parseExpr();
  4324. if (Ex == nullptr)
  4325. return Ex;
  4326. return make<PostfixExpr>(Ex, "++");
  4327. }
  4328. case 's':
  4329. First += 2;
  4330. return getDerived().parsePrefixExpr("+");
  4331. case 't': {
  4332. First += 2;
  4333. Node *L = getDerived().parseExpr();
  4334. if (L == nullptr)
  4335. return nullptr;
  4336. Node *R = getDerived().parseExpr();
  4337. if (R == nullptr)
  4338. return nullptr;
  4339. return make<MemberExpr>(L, "->", R);
  4340. }
  4341. }
  4342. return nullptr;
  4343. case 'q':
  4344. if (First[1] == 'u') {
  4345. First += 2;
  4346. Node *Cond = getDerived().parseExpr();
  4347. if (Cond == nullptr)
  4348. return nullptr;
  4349. Node *LHS = getDerived().parseExpr();
  4350. if (LHS == nullptr)
  4351. return nullptr;
  4352. Node *RHS = getDerived().parseExpr();
  4353. if (RHS == nullptr)
  4354. return nullptr;
  4355. return make<ConditionalExpr>(Cond, LHS, RHS);
  4356. }
  4357. return nullptr;
  4358. case 'r':
  4359. switch (First[1]) {
  4360. case 'c': {
  4361. First += 2;
  4362. Node *T = getDerived().parseType();
  4363. if (T == nullptr)
  4364. return T;
  4365. Node *Ex = getDerived().parseExpr();
  4366. if (Ex == nullptr)
  4367. return Ex;
  4368. return make<CastExpr>("reinterpret_cast", T, Ex);
  4369. }
  4370. case 'm':
  4371. First += 2;
  4372. return getDerived().parseBinaryExpr("%");
  4373. case 'M':
  4374. First += 2;
  4375. return getDerived().parseBinaryExpr("%=");
  4376. case 's':
  4377. First += 2;
  4378. return getDerived().parseBinaryExpr(">>");
  4379. case 'S':
  4380. First += 2;
  4381. return getDerived().parseBinaryExpr(">>=");
  4382. }
  4383. return nullptr;
  4384. case 's':
  4385. switch (First[1]) {
  4386. case 'c': {
  4387. First += 2;
  4388. Node *T = getDerived().parseType();
  4389. if (T == nullptr)
  4390. return T;
  4391. Node *Ex = getDerived().parseExpr();
  4392. if (Ex == nullptr)
  4393. return Ex;
  4394. return make<CastExpr>("static_cast", T, Ex);
  4395. }
  4396. case 'o':
  4397. First += 2;
  4398. return parseSubobjectExpr();
  4399. case 'p': {
  4400. First += 2;
  4401. Node *Child = getDerived().parseExpr();
  4402. if (Child == nullptr)
  4403. return nullptr;
  4404. return make<ParameterPackExpansion>(Child);
  4405. }
  4406. case 'r':
  4407. return getDerived().parseUnresolvedName();
  4408. case 't': {
  4409. First += 2;
  4410. Node *Ty = getDerived().parseType();
  4411. if (Ty == nullptr)
  4412. return Ty;
  4413. return make<EnclosingExpr>("sizeof (", Ty, ")");
  4414. }
  4415. case 'z': {
  4416. First += 2;
  4417. Node *Ex = getDerived().parseExpr();
  4418. if (Ex == nullptr)
  4419. return Ex;
  4420. return make<EnclosingExpr>("sizeof (", Ex, ")");
  4421. }
  4422. case 'Z':
  4423. First += 2;
  4424. if (look() == 'T') {
  4425. Node *R = getDerived().parseTemplateParam();
  4426. if (R == nullptr)
  4427. return nullptr;
  4428. return make<SizeofParamPackExpr>(R);
  4429. } else if (look() == 'f') {
  4430. Node *FP = getDerived().parseFunctionParam();
  4431. if (FP == nullptr)
  4432. return nullptr;
  4433. return make<EnclosingExpr>("sizeof... (", FP, ")");
  4434. }
  4435. return nullptr;
  4436. case 'P': {
  4437. First += 2;
  4438. size_t ArgsBegin = Names.size();
  4439. while (!consumeIf('E')) {
  4440. Node *Arg = getDerived().parseTemplateArg();
  4441. if (Arg == nullptr)
  4442. return nullptr;
  4443. Names.push_back(Arg);
  4444. }
  4445. auto *Pack = make<NodeArrayNode>(popTrailingNodeArray(ArgsBegin));
  4446. if (!Pack)
  4447. return nullptr;
  4448. return make<EnclosingExpr>("sizeof... (", Pack, ")");
  4449. }
  4450. }
  4451. return nullptr;
  4452. case 't':
  4453. switch (First[1]) {
  4454. case 'e': {
  4455. First += 2;
  4456. Node *Ex = getDerived().parseExpr();
  4457. if (Ex == nullptr)
  4458. return Ex;
  4459. return make<EnclosingExpr>("typeid (", Ex, ")");
  4460. }
  4461. case 'i': {
  4462. First += 2;
  4463. Node *Ty = getDerived().parseType();
  4464. if (Ty == nullptr)
  4465. return Ty;
  4466. return make<EnclosingExpr>("typeid (", Ty, ")");
  4467. }
  4468. case 'l': {
  4469. First += 2;
  4470. Node *Ty = getDerived().parseType();
  4471. if (Ty == nullptr)
  4472. return nullptr;
  4473. size_t InitsBegin = Names.size();
  4474. while (!consumeIf('E')) {
  4475. Node *E = getDerived().parseBracedExpr();
  4476. if (E == nullptr)
  4477. return nullptr;
  4478. Names.push_back(E);
  4479. }
  4480. return make<InitListExpr>(Ty, popTrailingNodeArray(InitsBegin));
  4481. }
  4482. case 'r':
  4483. First += 2;
  4484. return make<NameType>("throw");
  4485. case 'w': {
  4486. First += 2;
  4487. Node *Ex = getDerived().parseExpr();
  4488. if (Ex == nullptr)
  4489. return nullptr;
  4490. return make<ThrowExpr>(Ex);
  4491. }
  4492. }
  4493. return nullptr;
  4494. case 'u': {
  4495. ++First;
  4496. Node *Name = getDerived().parseSourceName(/*NameState=*/nullptr);
  4497. if (!Name)
  4498. return nullptr;
  4499. // Special case legacy __uuidof mangling. The 't' and 'z' appear where the
  4500. // standard encoding expects a <template-arg>, and would be otherwise be
  4501. // interpreted as <type> node 'short' or 'ellipsis'. However, neither
  4502. // __uuidof(short) nor __uuidof(...) can actually appear, so there is no
  4503. // actual conflict here.
  4504. if (Name->getBaseName() == "__uuidof") {
  4505. if (numLeft() < 2)
  4506. return nullptr;
  4507. if (*First == 't') {
  4508. ++First;
  4509. Node *Ty = getDerived().parseType();
  4510. if (!Ty)
  4511. return nullptr;
  4512. return make<CallExpr>(Name, makeNodeArray(&Ty, &Ty + 1));
  4513. }
  4514. if (*First == 'z') {
  4515. ++First;
  4516. Node *Ex = getDerived().parseExpr();
  4517. if (!Ex)
  4518. return nullptr;
  4519. return make<CallExpr>(Name, makeNodeArray(&Ex, &Ex + 1));
  4520. }
  4521. }
  4522. size_t ExprsBegin = Names.size();
  4523. while (!consumeIf('E')) {
  4524. Node *E = getDerived().parseTemplateArg();
  4525. if (E == nullptr)
  4526. return E;
  4527. Names.push_back(E);
  4528. }
  4529. return make<CallExpr>(Name, popTrailingNodeArray(ExprsBegin));
  4530. }
  4531. case '1':
  4532. case '2':
  4533. case '3':
  4534. case '4':
  4535. case '5':
  4536. case '6':
  4537. case '7':
  4538. case '8':
  4539. case '9':
  4540. return getDerived().parseUnresolvedName();
  4541. }
  4542. return nullptr;
  4543. }
  4544. // <call-offset> ::= h <nv-offset> _
  4545. // ::= v <v-offset> _
  4546. //
  4547. // <nv-offset> ::= <offset number>
  4548. // # non-virtual base override
  4549. //
  4550. // <v-offset> ::= <offset number> _ <virtual offset number>
  4551. // # virtual base override, with vcall offset
  4552. template <typename Alloc, typename Derived>
  4553. bool AbstractManglingParser<Alloc, Derived>::parseCallOffset() {
  4554. // Just scan through the call offset, we never add this information into the
  4555. // output.
  4556. if (consumeIf('h'))
  4557. return parseNumber(true).empty() || !consumeIf('_');
  4558. if (consumeIf('v'))
  4559. return parseNumber(true).empty() || !consumeIf('_') ||
  4560. parseNumber(true).empty() || !consumeIf('_');
  4561. return true;
  4562. }
  4563. // <special-name> ::= TV <type> # virtual table
  4564. // ::= TT <type> # VTT structure (construction vtable index)
  4565. // ::= TI <type> # typeinfo structure
  4566. // ::= TS <type> # typeinfo name (null-terminated byte string)
  4567. // ::= Tc <call-offset> <call-offset> <base encoding>
  4568. // # base is the nominal target function of thunk
  4569. // # first call-offset is 'this' adjustment
  4570. // # second call-offset is result adjustment
  4571. // ::= T <call-offset> <base encoding>
  4572. // # base is the nominal target function of thunk
  4573. // ::= GV <object name> # Guard variable for one-time initialization
  4574. // # No <type>
  4575. // ::= TW <object name> # Thread-local wrapper
  4576. // ::= TH <object name> # Thread-local initialization
  4577. // ::= GR <object name> _ # First temporary
  4578. // ::= GR <object name> <seq-id> _ # Subsequent temporaries
  4579. // extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first
  4580. // extension ::= GR <object name> # reference temporary for object
  4581. template <typename Derived, typename Alloc>
  4582. Node *AbstractManglingParser<Derived, Alloc>::parseSpecialName() {
  4583. switch (look()) {
  4584. case 'T':
  4585. switch (look(1)) {
  4586. // TA <template-arg> # template parameter object
  4587. //
  4588. // Not yet in the spec: https://github.com/itanium-cxx-abi/cxx-abi/issues/63
  4589. case 'A': {
  4590. First += 2;
  4591. Node *Arg = getDerived().parseTemplateArg();
  4592. if (Arg == nullptr)
  4593. return nullptr;
  4594. return make<SpecialName>("template parameter object for ", Arg);
  4595. }
  4596. // TV <type> # virtual table
  4597. case 'V': {
  4598. First += 2;
  4599. Node *Ty = getDerived().parseType();
  4600. if (Ty == nullptr)
  4601. return nullptr;
  4602. return make<SpecialName>("vtable for ", Ty);
  4603. }
  4604. // TT <type> # VTT structure (construction vtable index)
  4605. case 'T': {
  4606. First += 2;
  4607. Node *Ty = getDerived().parseType();
  4608. if (Ty == nullptr)
  4609. return nullptr;
  4610. return make<SpecialName>("VTT for ", Ty);
  4611. }
  4612. // TI <type> # typeinfo structure
  4613. case 'I': {
  4614. First += 2;
  4615. Node *Ty = getDerived().parseType();
  4616. if (Ty == nullptr)
  4617. return nullptr;
  4618. return make<SpecialName>("typeinfo for ", Ty);
  4619. }
  4620. // TS <type> # typeinfo name (null-terminated byte string)
  4621. case 'S': {
  4622. First += 2;
  4623. Node *Ty = getDerived().parseType();
  4624. if (Ty == nullptr)
  4625. return nullptr;
  4626. return make<SpecialName>("typeinfo name for ", Ty);
  4627. }
  4628. // Tc <call-offset> <call-offset> <base encoding>
  4629. case 'c': {
  4630. First += 2;
  4631. if (parseCallOffset() || parseCallOffset())
  4632. return nullptr;
  4633. Node *Encoding = getDerived().parseEncoding();
  4634. if (Encoding == nullptr)
  4635. return nullptr;
  4636. return make<SpecialName>("covariant return thunk to ", Encoding);
  4637. }
  4638. // extension ::= TC <first type> <number> _ <second type>
  4639. // # construction vtable for second-in-first
  4640. case 'C': {
  4641. First += 2;
  4642. Node *FirstType = getDerived().parseType();
  4643. if (FirstType == nullptr)
  4644. return nullptr;
  4645. if (parseNumber(true).empty() || !consumeIf('_'))
  4646. return nullptr;
  4647. Node *SecondType = getDerived().parseType();
  4648. if (SecondType == nullptr)
  4649. return nullptr;
  4650. return make<CtorVtableSpecialName>(SecondType, FirstType);
  4651. }
  4652. // TW <object name> # Thread-local wrapper
  4653. case 'W': {
  4654. First += 2;
  4655. Node *Name = getDerived().parseName();
  4656. if (Name == nullptr)
  4657. return nullptr;
  4658. return make<SpecialName>("thread-local wrapper routine for ", Name);
  4659. }
  4660. // TH <object name> # Thread-local initialization
  4661. case 'H': {
  4662. First += 2;
  4663. Node *Name = getDerived().parseName();
  4664. if (Name == nullptr)
  4665. return nullptr;
  4666. return make<SpecialName>("thread-local initialization routine for ", Name);
  4667. }
  4668. // T <call-offset> <base encoding>
  4669. default: {
  4670. ++First;
  4671. bool IsVirt = look() == 'v';
  4672. if (parseCallOffset())
  4673. return nullptr;
  4674. Node *BaseEncoding = getDerived().parseEncoding();
  4675. if (BaseEncoding == nullptr)
  4676. return nullptr;
  4677. if (IsVirt)
  4678. return make<SpecialName>("virtual thunk to ", BaseEncoding);
  4679. else
  4680. return make<SpecialName>("non-virtual thunk to ", BaseEncoding);
  4681. }
  4682. }
  4683. case 'G':
  4684. switch (look(1)) {
  4685. // GV <object name> # Guard variable for one-time initialization
  4686. case 'V': {
  4687. First += 2;
  4688. Node *Name = getDerived().parseName();
  4689. if (Name == nullptr)
  4690. return nullptr;
  4691. return make<SpecialName>("guard variable for ", Name);
  4692. }
  4693. // GR <object name> # reference temporary for object
  4694. // GR <object name> _ # First temporary
  4695. // GR <object name> <seq-id> _ # Subsequent temporaries
  4696. case 'R': {
  4697. First += 2;
  4698. Node *Name = getDerived().parseName();
  4699. if (Name == nullptr)
  4700. return nullptr;
  4701. size_t Count;
  4702. bool ParsedSeqId = !parseSeqId(&Count);
  4703. if (!consumeIf('_') && ParsedSeqId)
  4704. return nullptr;
  4705. return make<SpecialName>("reference temporary for ", Name);
  4706. }
  4707. }
  4708. }
  4709. return nullptr;
  4710. }
  4711. // <encoding> ::= <function name> <bare-function-type>
  4712. // ::= <data name>
  4713. // ::= <special-name>
  4714. template <typename Derived, typename Alloc>
  4715. Node *AbstractManglingParser<Derived, Alloc>::parseEncoding() {
  4716. // The template parameters of an encoding are unrelated to those of the
  4717. // enclosing context.
  4718. class SaveTemplateParams {
  4719. AbstractManglingParser *Parser;
  4720. decltype(TemplateParams) OldParams;
  4721. decltype(OuterTemplateParams) OldOuterParams;
  4722. public:
  4723. SaveTemplateParams(AbstractManglingParser *TheParser) : Parser(TheParser) {
  4724. OldParams = std::move(Parser->TemplateParams);
  4725. OldOuterParams = std::move(Parser->OuterTemplateParams);
  4726. Parser->TemplateParams.clear();
  4727. Parser->OuterTemplateParams.clear();
  4728. }
  4729. ~SaveTemplateParams() {
  4730. Parser->TemplateParams = std::move(OldParams);
  4731. Parser->OuterTemplateParams = std::move(OldOuterParams);
  4732. }
  4733. } SaveTemplateParams(this);
  4734. if (look() == 'G' || look() == 'T')
  4735. return getDerived().parseSpecialName();
  4736. auto IsEndOfEncoding = [&] {
  4737. // The set of chars that can potentially follow an <encoding> (none of which
  4738. // can start a <type>). Enumerating these allows us to avoid speculative
  4739. // parsing.
  4740. return numLeft() == 0 || look() == 'E' || look() == '.' || look() == '_';
  4741. };
  4742. NameState NameInfo(this);
  4743. Node *Name = getDerived().parseName(&NameInfo);
  4744. if (Name == nullptr)
  4745. return nullptr;
  4746. if (resolveForwardTemplateRefs(NameInfo))
  4747. return nullptr;
  4748. if (IsEndOfEncoding())
  4749. return Name;
  4750. Node *Attrs = nullptr;
  4751. if (consumeIf("Ua9enable_ifI")) {
  4752. size_t BeforeArgs = Names.size();
  4753. while (!consumeIf('E')) {
  4754. Node *Arg = getDerived().parseTemplateArg();
  4755. if (Arg == nullptr)
  4756. return nullptr;
  4757. Names.push_back(Arg);
  4758. }
  4759. Attrs = make<EnableIfAttr>(popTrailingNodeArray(BeforeArgs));
  4760. if (!Attrs)
  4761. return nullptr;
  4762. }
  4763. Node *ReturnType = nullptr;
  4764. if (!NameInfo.CtorDtorConversion && NameInfo.EndsWithTemplateArgs) {
  4765. ReturnType = getDerived().parseType();
  4766. if (ReturnType == nullptr)
  4767. return nullptr;
  4768. }
  4769. if (consumeIf('v'))
  4770. return make<FunctionEncoding>(ReturnType, Name, NodeArray(),
  4771. Attrs, NameInfo.CVQualifiers,
  4772. NameInfo.ReferenceQualifier);
  4773. size_t ParamsBegin = Names.size();
  4774. do {
  4775. Node *Ty = getDerived().parseType();
  4776. if (Ty == nullptr)
  4777. return nullptr;
  4778. Names.push_back(Ty);
  4779. } while (!IsEndOfEncoding());
  4780. return make<FunctionEncoding>(ReturnType, Name,
  4781. popTrailingNodeArray(ParamsBegin),
  4782. Attrs, NameInfo.CVQualifiers,
  4783. NameInfo.ReferenceQualifier);
  4784. }
  4785. template <class Float>
  4786. struct FloatData;
  4787. template <>
  4788. struct FloatData<float>
  4789. {
  4790. static const size_t mangled_size = 8;
  4791. static const size_t max_demangled_size = 24;
  4792. static constexpr const char* spec = "%af";
  4793. };
  4794. template <>
  4795. struct FloatData<double>
  4796. {
  4797. static const size_t mangled_size = 16;
  4798. static const size_t max_demangled_size = 32;
  4799. static constexpr const char* spec = "%a";
  4800. };
  4801. template <>
  4802. struct FloatData<long double>
  4803. {
  4804. #if defined(__mips__) && defined(__mips_n64) || defined(__aarch64__) || \
  4805. defined(__wasm__)
  4806. static const size_t mangled_size = 32;
  4807. #elif defined(__arm__) || defined(__mips__) || defined(__hexagon__)
  4808. static const size_t mangled_size = 16;
  4809. #else
  4810. static const size_t mangled_size = 20; // May need to be adjusted to 16 or 24 on other platforms
  4811. #endif
  4812. // `-0x1.ffffffffffffffffffffffffffffp+16383` + 'L' + '\0' == 42 bytes.
  4813. // 28 'f's * 4 bits == 112 bits, which is the number of mantissa bits.
  4814. // Negatives are one character longer than positives.
  4815. // `0x1.` and `p` are constant, and exponents `+16383` and `-16382` are the
  4816. // same length. 1 sign bit, 112 mantissa bits, and 15 exponent bits == 128.
  4817. static const size_t max_demangled_size = 42;
  4818. static constexpr const char *spec = "%LaL";
  4819. };
  4820. template <typename Alloc, typename Derived>
  4821. template <class Float>
  4822. Node *AbstractManglingParser<Alloc, Derived>::parseFloatingLiteral() {
  4823. const size_t N = FloatData<Float>::mangled_size;
  4824. if (numLeft() <= N)
  4825. return nullptr;
  4826. StringView Data(First, First + N);
  4827. for (char C : Data)
  4828. if (!std::isxdigit(C))
  4829. return nullptr;
  4830. First += N;
  4831. if (!consumeIf('E'))
  4832. return nullptr;
  4833. return make<FloatLiteralImpl<Float>>(Data);
  4834. }
  4835. // <seq-id> ::= <0-9A-Z>+
  4836. template <typename Alloc, typename Derived>
  4837. bool AbstractManglingParser<Alloc, Derived>::parseSeqId(size_t *Out) {
  4838. if (!(look() >= '0' && look() <= '9') &&
  4839. !(look() >= 'A' && look() <= 'Z'))
  4840. return true;
  4841. size_t Id = 0;
  4842. while (true) {
  4843. if (look() >= '0' && look() <= '9') {
  4844. Id *= 36;
  4845. Id += static_cast<size_t>(look() - '0');
  4846. } else if (look() >= 'A' && look() <= 'Z') {
  4847. Id *= 36;
  4848. Id += static_cast<size_t>(look() - 'A') + 10;
  4849. } else {
  4850. *Out = Id;
  4851. return false;
  4852. }
  4853. ++First;
  4854. }
  4855. }
  4856. // <substitution> ::= S <seq-id> _
  4857. // ::= S_
  4858. // <substitution> ::= Sa # ::std::allocator
  4859. // <substitution> ::= Sb # ::std::basic_string
  4860. // <substitution> ::= Ss # ::std::basic_string < char,
  4861. // ::std::char_traits<char>,
  4862. // ::std::allocator<char> >
  4863. // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> >
  4864. // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> >
  4865. // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> >
  4866. template <typename Derived, typename Alloc>
  4867. Node *AbstractManglingParser<Derived, Alloc>::parseSubstitution() {
  4868. if (!consumeIf('S'))
  4869. return nullptr;
  4870. if (look() >= 'a' && look() <= 'z') {
  4871. SpecialSubKind Kind;
  4872. switch (look()) {
  4873. case 'a':
  4874. Kind = SpecialSubKind::allocator;
  4875. break;
  4876. case 'b':
  4877. Kind = SpecialSubKind::basic_string;
  4878. break;
  4879. case 'd':
  4880. Kind = SpecialSubKind::iostream;
  4881. break;
  4882. case 'i':
  4883. Kind = SpecialSubKind::istream;
  4884. break;
  4885. case 'o':
  4886. Kind = SpecialSubKind::ostream;
  4887. break;
  4888. case 's':
  4889. Kind = SpecialSubKind::string;
  4890. break;
  4891. default:
  4892. return nullptr;
  4893. }
  4894. ++First;
  4895. auto *SpecialSub = make<SpecialSubstitution>(Kind);
  4896. if (!SpecialSub)
  4897. return nullptr;
  4898. // Itanium C++ ABI 5.1.2: If a name that would use a built-in <substitution>
  4899. // has ABI tags, the tags are appended to the substitution; the result is a
  4900. // substitutable component.
  4901. Node *WithTags = getDerived().parseAbiTags(SpecialSub);
  4902. if (WithTags != SpecialSub) {
  4903. Subs.push_back(WithTags);
  4904. SpecialSub = WithTags;
  4905. }
  4906. return SpecialSub;
  4907. }
  4908. // ::= S_
  4909. if (consumeIf('_')) {
  4910. if (Subs.empty())
  4911. return nullptr;
  4912. return Subs[0];
  4913. }
  4914. // ::= S <seq-id> _
  4915. size_t Index = 0;
  4916. if (parseSeqId(&Index))
  4917. return nullptr;
  4918. ++Index;
  4919. if (!consumeIf('_') || Index >= Subs.size())
  4920. return nullptr;
  4921. return Subs[Index];
  4922. }
  4923. // <template-param> ::= T_ # first template parameter
  4924. // ::= T <parameter-2 non-negative number> _
  4925. // ::= TL <level-1> __
  4926. // ::= TL <level-1> _ <parameter-2 non-negative number> _
  4927. template <typename Derived, typename Alloc>
  4928. Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParam() {
  4929. if (!consumeIf('T'))
  4930. return nullptr;
  4931. size_t Level = 0;
  4932. if (consumeIf('L')) {
  4933. if (parsePositiveInteger(&Level))
  4934. return nullptr;
  4935. ++Level;
  4936. if (!consumeIf('_'))
  4937. return nullptr;
  4938. }
  4939. size_t Index = 0;
  4940. if (!consumeIf('_')) {
  4941. if (parsePositiveInteger(&Index))
  4942. return nullptr;
  4943. ++Index;
  4944. if (!consumeIf('_'))
  4945. return nullptr;
  4946. }
  4947. // If we're in a context where this <template-param> refers to a
  4948. // <template-arg> further ahead in the mangled name (currently just conversion
  4949. // operator types), then we should only look it up in the right context.
  4950. // This can only happen at the outermost level.
  4951. if (PermitForwardTemplateReferences && Level == 0) {
  4952. Node *ForwardRef = make<ForwardTemplateReference>(Index);
  4953. if (!ForwardRef)
  4954. return nullptr;
  4955. assert(ForwardRef->getKind() == Node::KForwardTemplateReference);
  4956. ForwardTemplateRefs.push_back(
  4957. static_cast<ForwardTemplateReference *>(ForwardRef));
  4958. return ForwardRef;
  4959. }
  4960. if (Level >= TemplateParams.size() || !TemplateParams[Level] ||
  4961. Index >= TemplateParams[Level]->size()) {
  4962. // Itanium ABI 5.1.8: In a generic lambda, uses of auto in the parameter
  4963. // list are mangled as the corresponding artificial template type parameter.
  4964. if (ParsingLambdaParamsAtLevel == Level && Level <= TemplateParams.size()) {
  4965. // This will be popped by the ScopedTemplateParamList in
  4966. // parseUnnamedTypeName.
  4967. if (Level == TemplateParams.size())
  4968. TemplateParams.push_back(nullptr);
  4969. return make<NameType>("auto");
  4970. }
  4971. return nullptr;
  4972. }
  4973. return (*TemplateParams[Level])[Index];
  4974. }
  4975. // <template-param-decl> ::= Ty # type parameter
  4976. // ::= Tn <type> # non-type parameter
  4977. // ::= Tt <template-param-decl>* E # template parameter
  4978. // ::= Tp <template-param-decl> # parameter pack
  4979. template <typename Derived, typename Alloc>
  4980. Node *AbstractManglingParser<Derived, Alloc>::parseTemplateParamDecl() {
  4981. auto InventTemplateParamName = [&](TemplateParamKind Kind) {
  4982. unsigned Index = NumSyntheticTemplateParameters[(int)Kind]++;
  4983. Node *N = make<SyntheticTemplateParamName>(Kind, Index);
  4984. if (N) TemplateParams.back()->push_back(N);
  4985. return N;
  4986. };
  4987. if (consumeIf("Ty")) {
  4988. Node *Name = InventTemplateParamName(TemplateParamKind::Type);
  4989. if (!Name)
  4990. return nullptr;
  4991. return make<TypeTemplateParamDecl>(Name);
  4992. }
  4993. if (consumeIf("Tn")) {
  4994. Node *Name = InventTemplateParamName(TemplateParamKind::NonType);
  4995. if (!Name)
  4996. return nullptr;
  4997. Node *Type = parseType();
  4998. if (!Type)
  4999. return nullptr;
  5000. return make<NonTypeTemplateParamDecl>(Name, Type);
  5001. }
  5002. if (consumeIf("Tt")) {
  5003. Node *Name = InventTemplateParamName(TemplateParamKind::Template);
  5004. if (!Name)
  5005. return nullptr;
  5006. size_t ParamsBegin = Names.size();
  5007. ScopedTemplateParamList TemplateTemplateParamParams(this);
  5008. while (!consumeIf("E")) {
  5009. Node *P = parseTemplateParamDecl();
  5010. if (!P)
  5011. return nullptr;
  5012. Names.push_back(P);
  5013. }
  5014. NodeArray Params = popTrailingNodeArray(ParamsBegin);
  5015. return make<TemplateTemplateParamDecl>(Name, Params);
  5016. }
  5017. if (consumeIf("Tp")) {
  5018. Node *P = parseTemplateParamDecl();
  5019. if (!P)
  5020. return nullptr;
  5021. return make<TemplateParamPackDecl>(P);
  5022. }
  5023. return nullptr;
  5024. }
  5025. // <template-arg> ::= <type> # type or template
  5026. // ::= X <expression> E # expression
  5027. // ::= <expr-primary> # simple expressions
  5028. // ::= J <template-arg>* E # argument pack
  5029. // ::= LZ <encoding> E # extension
  5030. template <typename Derived, typename Alloc>
  5031. Node *AbstractManglingParser<Derived, Alloc>::parseTemplateArg() {
  5032. switch (look()) {
  5033. case 'X': {
  5034. ++First;
  5035. Node *Arg = getDerived().parseExpr();
  5036. if (Arg == nullptr || !consumeIf('E'))
  5037. return nullptr;
  5038. return Arg;
  5039. }
  5040. case 'I': // 'I' here is an old GCC mangling ABI. It is common in outside binaries.
  5041. case 'J': {
  5042. ++First;
  5043. size_t ArgsBegin = Names.size();
  5044. while (!consumeIf('E')) {
  5045. Node *Arg = getDerived().parseTemplateArg();
  5046. if (Arg == nullptr)
  5047. return nullptr;
  5048. Names.push_back(Arg);
  5049. }
  5050. NodeArray Args = popTrailingNodeArray(ArgsBegin);
  5051. return make<TemplateArgumentPack>(Args);
  5052. }
  5053. case 'L': {
  5054. // ::= LZ <encoding> E # extension
  5055. if (look(1) == 'Z') {
  5056. First += 2;
  5057. Node *Arg = getDerived().parseEncoding();
  5058. if (Arg == nullptr || !consumeIf('E'))
  5059. return nullptr;
  5060. return Arg;
  5061. }
  5062. // ::= <expr-primary> # simple expressions
  5063. return getDerived().parseExprPrimary();
  5064. }
  5065. default:
  5066. return getDerived().parseType();
  5067. }
  5068. }
  5069. // <template-args> ::= I <template-arg>* E
  5070. // extension, the abi says <template-arg>+
  5071. template <typename Derived, typename Alloc>
  5072. Node *
  5073. AbstractManglingParser<Derived, Alloc>::parseTemplateArgs(bool TagTemplates) {
  5074. if (!consumeIf('I'))
  5075. return nullptr;
  5076. // <template-params> refer to the innermost <template-args>. Clear out any
  5077. // outer args that we may have inserted into TemplateParams.
  5078. if (TagTemplates) {
  5079. TemplateParams.clear();
  5080. TemplateParams.push_back(&OuterTemplateParams);
  5081. OuterTemplateParams.clear();
  5082. }
  5083. size_t ArgsBegin = Names.size();
  5084. while (!consumeIf('E')) {
  5085. if (TagTemplates) {
  5086. auto OldParams = std::move(TemplateParams);
  5087. Node *Arg = getDerived().parseTemplateArg();
  5088. TemplateParams = std::move(OldParams);
  5089. if (Arg == nullptr)
  5090. return nullptr;
  5091. Names.push_back(Arg);
  5092. Node *TableEntry = Arg;
  5093. if (Arg->getKind() == Node::KTemplateArgumentPack) {
  5094. TableEntry = make<ParameterPack>(
  5095. static_cast<TemplateArgumentPack*>(TableEntry)->getElements());
  5096. if (!TableEntry)
  5097. return nullptr;
  5098. }
  5099. TemplateParams.back()->push_back(TableEntry);
  5100. } else {
  5101. Node *Arg = getDerived().parseTemplateArg();
  5102. if (Arg == nullptr)
  5103. return nullptr;
  5104. Names.push_back(Arg);
  5105. }
  5106. }
  5107. return make<TemplateArgs>(popTrailingNodeArray(ArgsBegin));
  5108. }
  5109. // <mangled-name> ::= _Z <encoding>
  5110. // ::= <type>
  5111. // extension ::= ___Z <encoding> _block_invoke
  5112. // extension ::= ___Z <encoding> _block_invoke<decimal-digit>+
  5113. // extension ::= ___Z <encoding> _block_invoke_<decimal-digit>+
  5114. template <typename Derived, typename Alloc>
  5115. Node *AbstractManglingParser<Derived, Alloc>::parse() {
  5116. if (consumeIf("_Z") || consumeIf("__Z")) {
  5117. Node *Encoding = getDerived().parseEncoding();
  5118. if (Encoding == nullptr)
  5119. return nullptr;
  5120. if (look() == '.') {
  5121. Encoding = make<DotSuffix>(Encoding, StringView(First, Last));
  5122. First = Last;
  5123. }
  5124. if (numLeft() != 0)
  5125. return nullptr;
  5126. return Encoding;
  5127. }
  5128. if (consumeIf("___Z") || consumeIf("____Z")) {
  5129. Node *Encoding = getDerived().parseEncoding();
  5130. if (Encoding == nullptr || !consumeIf("_block_invoke"))
  5131. return nullptr;
  5132. bool RequireNumber = consumeIf('_');
  5133. if (parseNumber().empty() && RequireNumber)
  5134. return nullptr;
  5135. if (look() == '.')
  5136. First = Last;
  5137. if (numLeft() != 0)
  5138. return nullptr;
  5139. return make<SpecialName>("invocation function for block in ", Encoding);
  5140. }
  5141. Node *Ty = getDerived().parseType();
  5142. if (numLeft() != 0)
  5143. return nullptr;
  5144. return Ty;
  5145. }
  5146. template <typename Alloc>
  5147. struct ManglingParser : AbstractManglingParser<ManglingParser<Alloc>, Alloc> {
  5148. using AbstractManglingParser<ManglingParser<Alloc>,
  5149. Alloc>::AbstractManglingParser;
  5150. };
  5151. DEMANGLE_NAMESPACE_END
  5152. #endif // DEMANGLE_ITANIUMDEMANGLE_H
  5153. #ifdef __GNUC__
  5154. #pragma GCC diagnostic pop
  5155. #endif