ItaniumDemangle.h 168 KB

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