ItaniumDemangle.h 165 KB

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