cp-demangle.c 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009
  1. /* Demangler for g++ V3 ABI.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@wasabisystems.com>.
  4. This file is part of the libiberty library, which is part of GCC.
  5. This file is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. In addition to the permissions in the GNU General Public License, the
  10. Free Software Foundation gives you unlimited permission to link the
  11. compiled version of this file into combinations with other programs,
  12. and to distribute those combinations without any restriction coming
  13. from the use of this file. (The General Public License restrictions
  14. do apply in other respects; for example, they cover modification of
  15. the file, and distribution when not linked into a combined
  16. executable.)
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. /* This code implements a demangler for the g++ V3 ABI. The ABI is
  26. described on this web page:
  27. https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
  28. This code was written while looking at the demangler written by
  29. Alex Samuel <samuel@codesourcery.com>.
  30. This code first pulls the mangled name apart into a list of
  31. components, and then walks the list generating the demangled
  32. name.
  33. This file will normally define the following functions, q.v.:
  34. char *cplus_demangle_v3(const char *mangled, int options)
  35. char *java_demangle_v3(const char *mangled)
  36. int cplus_demangle_v3_callback(const char *mangled, int options,
  37. demangle_callbackref callback)
  38. int java_demangle_v3_callback(const char *mangled,
  39. demangle_callbackref callback)
  40. enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
  41. enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
  42. Also, the interface to the component list is public, and defined in
  43. demangle.h. The interface consists of these types, which are
  44. defined in demangle.h:
  45. enum demangle_component_type
  46. struct demangle_component
  47. demangle_callbackref
  48. and these functions defined in this file:
  49. cplus_demangle_fill_name
  50. cplus_demangle_fill_extended_operator
  51. cplus_demangle_fill_ctor
  52. cplus_demangle_fill_dtor
  53. cplus_demangle_print
  54. cplus_demangle_print_callback
  55. and other functions defined in the file cp-demint.c.
  56. This file also defines some other functions and variables which are
  57. only to be used by the file cp-demint.c.
  58. Preprocessor macros you can define while compiling this file:
  59. IN_LIBGCC2
  60. If defined, this file defines the following functions, q.v.:
  61. char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
  62. int *status)
  63. int __gcclibcxx_demangle_callback (const char *,
  64. void (*)
  65. (const char *, size_t, void *),
  66. void *)
  67. instead of cplus_demangle_v3[_callback]() and
  68. java_demangle_v3[_callback]().
  69. IN_GLIBCPP_V3
  70. If defined, this file defines only __cxa_demangle() and
  71. __gcclibcxx_demangle_callback(), and no other publically visible
  72. functions or variables.
  73. STANDALONE_DEMANGLER
  74. If defined, this file defines a main() function which demangles
  75. any arguments, or, if none, demangles stdin.
  76. CP_DEMANGLE_DEBUG
  77. If defined, turns on debugging mode, which prints information on
  78. stdout about the mangled string. This is not generally useful.
  79. CHECK_DEMANGLER
  80. If defined, additional sanity checks will be performed. It will
  81. cause some slowdown, but will allow to catch out-of-bound access
  82. errors earlier. This macro is intended for testing and debugging. */
  83. #if defined (_AIX) && !defined (__GNUC__)
  84. #pragma alloca
  85. #endif
  86. #ifdef HAVE_CONFIG_H
  87. #include "config.h"
  88. #endif
  89. #include <stdio.h>
  90. #ifdef HAVE_STDLIB_H
  91. #include <stdlib.h>
  92. #endif
  93. #ifdef HAVE_STRING_H
  94. #include <string.h>
  95. #endif
  96. #ifdef HAVE_ALLOCA_H
  97. # include <alloca.h>
  98. #else
  99. # ifndef alloca
  100. # ifdef __GNUC__
  101. # define alloca __builtin_alloca
  102. # else
  103. extern char *alloca ();
  104. # endif /* __GNUC__ */
  105. # endif /* alloca */
  106. #endif /* HAVE_ALLOCA_H */
  107. #ifdef HAVE_LIMITS_H
  108. #include <limits.h>
  109. #endif
  110. #ifndef INT_MAX
  111. # define INT_MAX (int)(((unsigned int) ~0) >> 1) /* 0x7FFFFFFF */
  112. #endif
  113. #include "ansidecl.h"
  114. #include "libiberty.h"
  115. #include "demangle.h"
  116. #include "cp-demangle.h"
  117. /* If IN_GLIBCPP_V3 is defined, some functions are made static. We
  118. also rename them via #define to avoid compiler errors when the
  119. static definition conflicts with the extern declaration in a header
  120. file. */
  121. #ifdef IN_GLIBCPP_V3
  122. #define CP_STATIC_IF_GLIBCPP_V3 static
  123. #define cplus_demangle_fill_name d_fill_name
  124. static int d_fill_name (struct demangle_component *, const char *, int);
  125. #define cplus_demangle_fill_extended_operator d_fill_extended_operator
  126. static int
  127. d_fill_extended_operator (struct demangle_component *, int,
  128. struct demangle_component *);
  129. #define cplus_demangle_fill_ctor d_fill_ctor
  130. static int
  131. d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
  132. struct demangle_component *);
  133. #define cplus_demangle_fill_dtor d_fill_dtor
  134. static int
  135. d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
  136. struct demangle_component *);
  137. #define cplus_demangle_mangled_name d_mangled_name
  138. static struct demangle_component *d_mangled_name (struct d_info *, int);
  139. #define cplus_demangle_type d_type
  140. static struct demangle_component *d_type (struct d_info *);
  141. #define cplus_demangle_print d_print
  142. static char *d_print (int, struct demangle_component *, int, size_t *);
  143. #define cplus_demangle_print_callback d_print_callback
  144. static int d_print_callback (int, struct demangle_component *,
  145. demangle_callbackref, void *);
  146. #define cplus_demangle_init_info d_init_info
  147. static void d_init_info (const char *, int, size_t, struct d_info *);
  148. #else /* ! defined(IN_GLIBCPP_V3) */
  149. #define CP_STATIC_IF_GLIBCPP_V3
  150. #endif /* ! defined(IN_GLIBCPP_V3) */
  151. /* See if the compiler supports dynamic arrays. */
  152. #ifdef __GNUC__
  153. #define CP_DYNAMIC_ARRAYS
  154. #else
  155. #ifdef __STDC__
  156. #ifdef __STDC_VERSION__
  157. #if __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__
  158. #define CP_DYNAMIC_ARRAYS
  159. #endif /* __STDC_VERSION__ >= 199901L && !__STDC_NO_VLA__ */
  160. #endif /* defined (__STDC_VERSION__) */
  161. #endif /* defined (__STDC__) */
  162. #endif /* ! defined (__GNUC__) */
  163. /* We avoid pulling in the ctype tables, to prevent pulling in
  164. additional unresolved symbols when this code is used in a library.
  165. FIXME: Is this really a valid reason? This comes from the original
  166. V3 demangler code.
  167. As of this writing this file has the following undefined references
  168. when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
  169. strcat, strlen. */
  170. #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
  171. #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
  172. #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
  173. /* The prefix prepended by GCC to an identifier represnting the
  174. anonymous namespace. */
  175. #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
  176. #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
  177. (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
  178. /* Information we keep for the standard substitutions. */
  179. struct d_standard_sub_info
  180. {
  181. /* The code for this substitution. */
  182. char code;
  183. /* The simple string it expands to. */
  184. const char *simple_expansion;
  185. /* The length of the simple expansion. */
  186. int simple_len;
  187. /* The results of a full, verbose, expansion. This is used when
  188. qualifying a constructor/destructor, or when in verbose mode. */
  189. const char *full_expansion;
  190. /* The length of the full expansion. */
  191. int full_len;
  192. /* What to set the last_name field of d_info to; NULL if we should
  193. not set it. This is only relevant when qualifying a
  194. constructor/destructor. */
  195. const char *set_last_name;
  196. /* The length of set_last_name. */
  197. int set_last_name_len;
  198. };
  199. /* Accessors for subtrees of struct demangle_component. */
  200. #define d_left(dc) ((dc)->u.s_binary.left)
  201. #define d_right(dc) ((dc)->u.s_binary.right)
  202. /* A list of templates. This is used while printing. */
  203. struct d_print_template
  204. {
  205. /* Next template on the list. */
  206. struct d_print_template *next;
  207. /* This template. */
  208. const struct demangle_component *template_decl;
  209. };
  210. /* A list of type modifiers. This is used while printing. */
  211. struct d_print_mod
  212. {
  213. /* Next modifier on the list. These are in the reverse of the order
  214. in which they appeared in the mangled string. */
  215. struct d_print_mod *next;
  216. /* The modifier. */
  217. struct demangle_component *mod;
  218. /* Whether this modifier was printed. */
  219. int printed;
  220. /* The list of templates which applies to this modifier. */
  221. struct d_print_template *templates;
  222. };
  223. /* We use these structures to hold information during printing. */
  224. struct d_growable_string
  225. {
  226. /* Buffer holding the result. */
  227. char *buf;
  228. /* Current length of data in buffer. */
  229. size_t len;
  230. /* Allocated size of buffer. */
  231. size_t alc;
  232. /* Set to 1 if we had a memory allocation failure. */
  233. int allocation_failure;
  234. };
  235. /* Stack of components, innermost first, used to avoid loops. */
  236. struct d_component_stack
  237. {
  238. /* This component. */
  239. const struct demangle_component *dc;
  240. /* This component's parent. */
  241. const struct d_component_stack *parent;
  242. };
  243. /* A demangle component and some scope captured when it was first
  244. traversed. */
  245. struct d_saved_scope
  246. {
  247. /* The component whose scope this is. */
  248. const struct demangle_component *container;
  249. /* The list of templates, if any, that was current when this
  250. scope was captured. */
  251. struct d_print_template *templates;
  252. };
  253. /* Checkpoint structure to allow backtracking. This holds copies
  254. of the fields of struct d_info that need to be restored
  255. if a trial parse needs to be backtracked over. */
  256. struct d_info_checkpoint
  257. {
  258. const char *n;
  259. int next_comp;
  260. int next_sub;
  261. int expansion;
  262. };
  263. /* Maximum number of times d_print_comp may be called recursively. */
  264. #define MAX_RECURSION_COUNT 1024
  265. enum { D_PRINT_BUFFER_LENGTH = 256 };
  266. struct d_print_info
  267. {
  268. /* Fixed-length allocated buffer for demangled data, flushed to the
  269. callback with a NUL termination once full. */
  270. char buf[D_PRINT_BUFFER_LENGTH];
  271. /* Current length of data in buffer. */
  272. size_t len;
  273. /* The last character printed, saved individually so that it survives
  274. any buffer flush. */
  275. char last_char;
  276. /* Callback function to handle demangled buffer flush. */
  277. demangle_callbackref callback;
  278. /* Opaque callback argument. */
  279. void *opaque;
  280. /* The current list of templates, if any. */
  281. struct d_print_template *templates;
  282. /* The current list of modifiers (e.g., pointer, reference, etc.),
  283. if any. */
  284. struct d_print_mod *modifiers;
  285. /* Set to 1 if we saw a demangling error. */
  286. int demangle_failure;
  287. /* Number of times d_print_comp was recursively called. Should not
  288. be bigger than MAX_RECURSION_COUNT. */
  289. int recursion;
  290. /* Non-zero if we're printing a lambda argument. A template
  291. parameter reference actually means 'auto'. */
  292. int is_lambda_arg;
  293. /* The current index into any template argument packs we are using
  294. for printing, or -1 to print the whole pack. */
  295. int pack_index;
  296. /* Number of d_print_flush calls so far. */
  297. unsigned long int flush_count;
  298. /* Stack of components, innermost first, used to avoid loops. */
  299. const struct d_component_stack *component_stack;
  300. /* Array of saved scopes for evaluating substitutions. */
  301. struct d_saved_scope *saved_scopes;
  302. /* Index of the next unused saved scope in the above array. */
  303. int next_saved_scope;
  304. /* Number of saved scopes in the above array. */
  305. int num_saved_scopes;
  306. /* Array of templates for saving into scopes. */
  307. struct d_print_template *copy_templates;
  308. /* Index of the next unused copy template in the above array. */
  309. int next_copy_template;
  310. /* Number of copy templates in the above array. */
  311. int num_copy_templates;
  312. /* The nearest enclosing template, if any. */
  313. const struct demangle_component *current_template;
  314. };
  315. #ifdef CP_DEMANGLE_DEBUG
  316. static void d_dump (struct demangle_component *, int);
  317. #endif
  318. static struct demangle_component *
  319. d_make_empty (struct d_info *);
  320. static struct demangle_component *
  321. d_make_comp (struct d_info *, enum demangle_component_type,
  322. struct demangle_component *,
  323. struct demangle_component *);
  324. static struct demangle_component *
  325. d_make_name (struct d_info *, const char *, int);
  326. static struct demangle_component *
  327. d_make_demangle_mangled_name (struct d_info *, const char *);
  328. static struct demangle_component *
  329. d_make_builtin_type (struct d_info *,
  330. const struct demangle_builtin_type_info *);
  331. static struct demangle_component *
  332. d_make_operator (struct d_info *,
  333. const struct demangle_operator_info *);
  334. static struct demangle_component *
  335. d_make_extended_operator (struct d_info *, int,
  336. struct demangle_component *);
  337. static struct demangle_component *
  338. d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
  339. struct demangle_component *);
  340. static struct demangle_component *
  341. d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
  342. struct demangle_component *);
  343. static struct demangle_component *
  344. d_make_template_param (struct d_info *, int);
  345. static struct demangle_component *
  346. d_make_sub (struct d_info *, const char *, int);
  347. static int
  348. has_return_type (struct demangle_component *);
  349. static int
  350. is_ctor_dtor_or_conversion (struct demangle_component *);
  351. static struct demangle_component *d_encoding (struct d_info *, int);
  352. static struct demangle_component *d_name (struct d_info *);
  353. static struct demangle_component *d_nested_name (struct d_info *);
  354. static struct demangle_component *d_prefix (struct d_info *, int);
  355. static struct demangle_component *d_unqualified_name (struct d_info *);
  356. static struct demangle_component *d_source_name (struct d_info *);
  357. static int d_number (struct d_info *);
  358. static struct demangle_component *d_identifier (struct d_info *, int);
  359. static struct demangle_component *d_operator_name (struct d_info *);
  360. static struct demangle_component *d_special_name (struct d_info *);
  361. static struct demangle_component *d_parmlist (struct d_info *);
  362. static int d_call_offset (struct d_info *, int);
  363. static struct demangle_component *d_ctor_dtor_name (struct d_info *);
  364. static struct demangle_component **
  365. d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
  366. static struct demangle_component *
  367. d_ref_qualifier (struct d_info *, struct demangle_component *);
  368. static struct demangle_component *
  369. d_function_type (struct d_info *);
  370. static struct demangle_component *
  371. d_bare_function_type (struct d_info *, int);
  372. static struct demangle_component *
  373. d_class_enum_type (struct d_info *);
  374. static struct demangle_component *d_array_type (struct d_info *);
  375. static struct demangle_component *d_vector_type (struct d_info *);
  376. static struct demangle_component *
  377. d_pointer_to_member_type (struct d_info *);
  378. static struct demangle_component *
  379. d_template_param (struct d_info *);
  380. static struct demangle_component *d_template_args (struct d_info *);
  381. static struct demangle_component *d_template_args_1 (struct d_info *);
  382. static struct demangle_component *
  383. d_template_arg (struct d_info *);
  384. static struct demangle_component *d_expression (struct d_info *);
  385. static struct demangle_component *d_expr_primary (struct d_info *);
  386. static struct demangle_component *d_local_name (struct d_info *);
  387. static int d_discriminator (struct d_info *);
  388. static struct demangle_component *d_lambda (struct d_info *);
  389. static struct demangle_component *d_unnamed_type (struct d_info *);
  390. static struct demangle_component *
  391. d_clone_suffix (struct d_info *, struct demangle_component *);
  392. static int
  393. d_add_substitution (struct d_info *, struct demangle_component *);
  394. static struct demangle_component *d_substitution (struct d_info *, int);
  395. static void d_checkpoint (struct d_info *, struct d_info_checkpoint *);
  396. static void d_backtrack (struct d_info *, struct d_info_checkpoint *);
  397. static void d_growable_string_init (struct d_growable_string *, size_t);
  398. static inline void
  399. d_growable_string_resize (struct d_growable_string *, size_t);
  400. static inline void
  401. d_growable_string_append_buffer (struct d_growable_string *,
  402. const char *, size_t);
  403. static void
  404. d_growable_string_callback_adapter (const char *, size_t, void *);
  405. static void
  406. d_print_init (struct d_print_info *, demangle_callbackref, void *,
  407. struct demangle_component *);
  408. static inline void d_print_error (struct d_print_info *);
  409. static inline int d_print_saw_error (struct d_print_info *);
  410. static inline void d_print_flush (struct d_print_info *);
  411. static inline void d_append_char (struct d_print_info *, char);
  412. static inline void d_append_buffer (struct d_print_info *,
  413. const char *, size_t);
  414. static inline void d_append_string (struct d_print_info *, const char *);
  415. static inline char d_last_char (struct d_print_info *);
  416. static void
  417. d_print_comp (struct d_print_info *, int, struct demangle_component *);
  418. static void
  419. d_print_java_identifier (struct d_print_info *, const char *, int);
  420. static void
  421. d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
  422. static void
  423. d_print_mod (struct d_print_info *, int, struct demangle_component *);
  424. static void
  425. d_print_function_type (struct d_print_info *, int,
  426. struct demangle_component *,
  427. struct d_print_mod *);
  428. static void
  429. d_print_array_type (struct d_print_info *, int,
  430. struct demangle_component *,
  431. struct d_print_mod *);
  432. static void
  433. d_print_expr_op (struct d_print_info *, int, struct demangle_component *);
  434. static void d_print_cast (struct d_print_info *, int,
  435. struct demangle_component *);
  436. static void d_print_conversion (struct d_print_info *, int,
  437. struct demangle_component *);
  438. static int d_demangle_callback (const char *, int,
  439. demangle_callbackref, void *);
  440. static char *d_demangle (const char *, int, size_t *);
  441. #define FNQUAL_COMPONENT_CASE \
  442. case DEMANGLE_COMPONENT_RESTRICT_THIS: \
  443. case DEMANGLE_COMPONENT_VOLATILE_THIS: \
  444. case DEMANGLE_COMPONENT_CONST_THIS: \
  445. case DEMANGLE_COMPONENT_REFERENCE_THIS: \
  446. case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS: \
  447. case DEMANGLE_COMPONENT_TRANSACTION_SAFE: \
  448. case DEMANGLE_COMPONENT_NOEXCEPT: \
  449. case DEMANGLE_COMPONENT_THROW_SPEC
  450. /* True iff TYPE is a demangling component representing a
  451. function-type-qualifier. */
  452. static int
  453. is_fnqual_component_type (enum demangle_component_type type)
  454. {
  455. switch (type)
  456. {
  457. FNQUAL_COMPONENT_CASE:
  458. return 1;
  459. default:
  460. break;
  461. }
  462. return 0;
  463. }
  464. #ifdef CP_DEMANGLE_DEBUG
  465. static void
  466. d_dump (struct demangle_component *dc, int indent)
  467. {
  468. int i;
  469. if (dc == NULL)
  470. {
  471. if (indent == 0)
  472. printf ("failed demangling\n");
  473. return;
  474. }
  475. for (i = 0; i < indent; ++i)
  476. putchar (' ');
  477. switch (dc->type)
  478. {
  479. case DEMANGLE_COMPONENT_NAME:
  480. printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
  481. return;
  482. case DEMANGLE_COMPONENT_TAGGED_NAME:
  483. printf ("tagged name\n");
  484. d_dump (dc->u.s_binary.left, indent + 2);
  485. d_dump (dc->u.s_binary.right, indent + 2);
  486. return;
  487. case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
  488. printf ("template parameter %ld\n", dc->u.s_number.number);
  489. return;
  490. case DEMANGLE_COMPONENT_TPARM_OBJ:
  491. printf ("template parameter object\n");
  492. break;
  493. case DEMANGLE_COMPONENT_FUNCTION_PARAM:
  494. printf ("function parameter %ld\n", dc->u.s_number.number);
  495. return;
  496. case DEMANGLE_COMPONENT_CTOR:
  497. printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
  498. d_dump (dc->u.s_ctor.name, indent + 2);
  499. return;
  500. case DEMANGLE_COMPONENT_DTOR:
  501. printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
  502. d_dump (dc->u.s_dtor.name, indent + 2);
  503. return;
  504. case DEMANGLE_COMPONENT_SUB_STD:
  505. printf ("standard substitution %s\n", dc->u.s_string.string);
  506. return;
  507. case DEMANGLE_COMPONENT_BUILTIN_TYPE:
  508. printf ("builtin type %s\n", dc->u.s_builtin.type->name);
  509. return;
  510. case DEMANGLE_COMPONENT_OPERATOR:
  511. printf ("operator %s\n", dc->u.s_operator.op->name);
  512. return;
  513. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  514. printf ("extended operator with %d args\n",
  515. dc->u.s_extended_operator.args);
  516. d_dump (dc->u.s_extended_operator.name, indent + 2);
  517. return;
  518. case DEMANGLE_COMPONENT_QUAL_NAME:
  519. printf ("qualified name\n");
  520. break;
  521. case DEMANGLE_COMPONENT_LOCAL_NAME:
  522. printf ("local name\n");
  523. break;
  524. case DEMANGLE_COMPONENT_TYPED_NAME:
  525. printf ("typed name\n");
  526. break;
  527. case DEMANGLE_COMPONENT_TEMPLATE:
  528. printf ("template\n");
  529. break;
  530. case DEMANGLE_COMPONENT_VTABLE:
  531. printf ("vtable\n");
  532. break;
  533. case DEMANGLE_COMPONENT_VTT:
  534. printf ("VTT\n");
  535. break;
  536. case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  537. printf ("construction vtable\n");
  538. break;
  539. case DEMANGLE_COMPONENT_TYPEINFO:
  540. printf ("typeinfo\n");
  541. break;
  542. case DEMANGLE_COMPONENT_TYPEINFO_NAME:
  543. printf ("typeinfo name\n");
  544. break;
  545. case DEMANGLE_COMPONENT_TYPEINFO_FN:
  546. printf ("typeinfo function\n");
  547. break;
  548. case DEMANGLE_COMPONENT_THUNK:
  549. printf ("thunk\n");
  550. break;
  551. case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
  552. printf ("virtual thunk\n");
  553. break;
  554. case DEMANGLE_COMPONENT_COVARIANT_THUNK:
  555. printf ("covariant thunk\n");
  556. break;
  557. case DEMANGLE_COMPONENT_JAVA_CLASS:
  558. printf ("java class\n");
  559. break;
  560. case DEMANGLE_COMPONENT_GUARD:
  561. printf ("guard\n");
  562. break;
  563. case DEMANGLE_COMPONENT_REFTEMP:
  564. printf ("reference temporary\n");
  565. break;
  566. case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
  567. printf ("hidden alias\n");
  568. break;
  569. case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
  570. printf ("transaction clone\n");
  571. break;
  572. case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
  573. printf ("non-transaction clone\n");
  574. break;
  575. case DEMANGLE_COMPONENT_RESTRICT:
  576. printf ("restrict\n");
  577. break;
  578. case DEMANGLE_COMPONENT_VOLATILE:
  579. printf ("volatile\n");
  580. break;
  581. case DEMANGLE_COMPONENT_CONST:
  582. printf ("const\n");
  583. break;
  584. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  585. printf ("restrict this\n");
  586. break;
  587. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  588. printf ("volatile this\n");
  589. break;
  590. case DEMANGLE_COMPONENT_CONST_THIS:
  591. printf ("const this\n");
  592. break;
  593. case DEMANGLE_COMPONENT_REFERENCE_THIS:
  594. printf ("reference this\n");
  595. break;
  596. case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
  597. printf ("rvalue reference this\n");
  598. break;
  599. case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
  600. printf ("transaction_safe this\n");
  601. break;
  602. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  603. printf ("vendor type qualifier\n");
  604. break;
  605. case DEMANGLE_COMPONENT_POINTER:
  606. printf ("pointer\n");
  607. break;
  608. case DEMANGLE_COMPONENT_REFERENCE:
  609. printf ("reference\n");
  610. break;
  611. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  612. printf ("rvalue reference\n");
  613. break;
  614. case DEMANGLE_COMPONENT_COMPLEX:
  615. printf ("complex\n");
  616. break;
  617. case DEMANGLE_COMPONENT_IMAGINARY:
  618. printf ("imaginary\n");
  619. break;
  620. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  621. printf ("vendor type\n");
  622. break;
  623. case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  624. printf ("function type\n");
  625. break;
  626. case DEMANGLE_COMPONENT_ARRAY_TYPE:
  627. printf ("array type\n");
  628. break;
  629. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  630. printf ("pointer to member type\n");
  631. break;
  632. case DEMANGLE_COMPONENT_FIXED_TYPE:
  633. printf ("fixed-point type, accum? %d, sat? %d\n",
  634. dc->u.s_fixed.accum, dc->u.s_fixed.sat);
  635. d_dump (dc->u.s_fixed.length, indent + 2);
  636. break;
  637. case DEMANGLE_COMPONENT_ARGLIST:
  638. printf ("argument list\n");
  639. break;
  640. case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  641. printf ("template argument list\n");
  642. break;
  643. case DEMANGLE_COMPONENT_INITIALIZER_LIST:
  644. printf ("initializer list\n");
  645. break;
  646. case DEMANGLE_COMPONENT_CAST:
  647. printf ("cast\n");
  648. break;
  649. case DEMANGLE_COMPONENT_CONVERSION:
  650. printf ("conversion operator\n");
  651. break;
  652. case DEMANGLE_COMPONENT_NULLARY:
  653. printf ("nullary operator\n");
  654. break;
  655. case DEMANGLE_COMPONENT_UNARY:
  656. printf ("unary operator\n");
  657. break;
  658. case DEMANGLE_COMPONENT_BINARY:
  659. printf ("binary operator\n");
  660. break;
  661. case DEMANGLE_COMPONENT_BINARY_ARGS:
  662. printf ("binary operator arguments\n");
  663. break;
  664. case DEMANGLE_COMPONENT_TRINARY:
  665. printf ("trinary operator\n");
  666. break;
  667. case DEMANGLE_COMPONENT_TRINARY_ARG1:
  668. printf ("trinary operator arguments 1\n");
  669. break;
  670. case DEMANGLE_COMPONENT_TRINARY_ARG2:
  671. printf ("trinary operator arguments 1\n");
  672. break;
  673. case DEMANGLE_COMPONENT_LITERAL:
  674. printf ("literal\n");
  675. break;
  676. case DEMANGLE_COMPONENT_LITERAL_NEG:
  677. printf ("negative literal\n");
  678. break;
  679. case DEMANGLE_COMPONENT_VENDOR_EXPR:
  680. printf ("vendor expression\n");
  681. break;
  682. case DEMANGLE_COMPONENT_JAVA_RESOURCE:
  683. printf ("java resource\n");
  684. break;
  685. case DEMANGLE_COMPONENT_COMPOUND_NAME:
  686. printf ("compound name\n");
  687. break;
  688. case DEMANGLE_COMPONENT_CHARACTER:
  689. printf ("character '%c'\n", dc->u.s_character.character);
  690. return;
  691. case DEMANGLE_COMPONENT_NUMBER:
  692. printf ("number %ld\n", dc->u.s_number.number);
  693. return;
  694. case DEMANGLE_COMPONENT_DECLTYPE:
  695. printf ("decltype\n");
  696. break;
  697. case DEMANGLE_COMPONENT_PACK_EXPANSION:
  698. printf ("pack expansion\n");
  699. break;
  700. case DEMANGLE_COMPONENT_TLS_INIT:
  701. printf ("tls init function\n");
  702. break;
  703. case DEMANGLE_COMPONENT_TLS_WRAPPER:
  704. printf ("tls wrapper function\n");
  705. break;
  706. case DEMANGLE_COMPONENT_DEFAULT_ARG:
  707. printf ("default argument %d\n", dc->u.s_unary_num.num);
  708. d_dump (dc->u.s_unary_num.sub, indent+2);
  709. return;
  710. case DEMANGLE_COMPONENT_LAMBDA:
  711. printf ("lambda %d\n", dc->u.s_unary_num.num);
  712. d_dump (dc->u.s_unary_num.sub, indent+2);
  713. return;
  714. }
  715. d_dump (d_left (dc), indent + 2);
  716. d_dump (d_right (dc), indent + 2);
  717. }
  718. #endif /* CP_DEMANGLE_DEBUG */
  719. /* Fill in a DEMANGLE_COMPONENT_NAME. */
  720. CP_STATIC_IF_GLIBCPP_V3
  721. int
  722. cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
  723. {
  724. if (p == NULL || s == NULL || len <= 0)
  725. return 0;
  726. p->d_printing = 0;
  727. p->d_counting = 0;
  728. p->type = DEMANGLE_COMPONENT_NAME;
  729. p->u.s_name.s = s;
  730. p->u.s_name.len = len;
  731. return 1;
  732. }
  733. /* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR. */
  734. CP_STATIC_IF_GLIBCPP_V3
  735. int
  736. cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
  737. struct demangle_component *name)
  738. {
  739. if (p == NULL || args < 0 || name == NULL)
  740. return 0;
  741. p->d_printing = 0;
  742. p->d_counting = 0;
  743. p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
  744. p->u.s_extended_operator.args = args;
  745. p->u.s_extended_operator.name = name;
  746. return 1;
  747. }
  748. /* Fill in a DEMANGLE_COMPONENT_CTOR. */
  749. CP_STATIC_IF_GLIBCPP_V3
  750. int
  751. cplus_demangle_fill_ctor (struct demangle_component *p,
  752. enum gnu_v3_ctor_kinds kind,
  753. struct demangle_component *name)
  754. {
  755. if (p == NULL
  756. || name == NULL
  757. || (int) kind < gnu_v3_complete_object_ctor
  758. || (int) kind > gnu_v3_object_ctor_group)
  759. return 0;
  760. p->d_printing = 0;
  761. p->d_counting = 0;
  762. p->type = DEMANGLE_COMPONENT_CTOR;
  763. p->u.s_ctor.kind = kind;
  764. p->u.s_ctor.name = name;
  765. return 1;
  766. }
  767. /* Fill in a DEMANGLE_COMPONENT_DTOR. */
  768. CP_STATIC_IF_GLIBCPP_V3
  769. int
  770. cplus_demangle_fill_dtor (struct demangle_component *p,
  771. enum gnu_v3_dtor_kinds kind,
  772. struct demangle_component *name)
  773. {
  774. if (p == NULL
  775. || name == NULL
  776. || (int) kind < gnu_v3_deleting_dtor
  777. || (int) kind > gnu_v3_object_dtor_group)
  778. return 0;
  779. p->d_printing = 0;
  780. p->d_counting = 0;
  781. p->type = DEMANGLE_COMPONENT_DTOR;
  782. p->u.s_dtor.kind = kind;
  783. p->u.s_dtor.name = name;
  784. return 1;
  785. }
  786. /* Add a new component. */
  787. static struct demangle_component *
  788. d_make_empty (struct d_info *di)
  789. {
  790. struct demangle_component *p;
  791. if (di->next_comp >= di->num_comps)
  792. return NULL;
  793. p = &di->comps[di->next_comp];
  794. p->d_printing = 0;
  795. p->d_counting = 0;
  796. ++di->next_comp;
  797. return p;
  798. }
  799. /* Add a new generic component. */
  800. static struct demangle_component *
  801. d_make_comp (struct d_info *di, enum demangle_component_type type,
  802. struct demangle_component *left,
  803. struct demangle_component *right)
  804. {
  805. struct demangle_component *p;
  806. /* We check for errors here. A typical error would be a NULL return
  807. from a subroutine. We catch those here, and return NULL
  808. upward. */
  809. switch (type)
  810. {
  811. /* These types require two parameters. */
  812. case DEMANGLE_COMPONENT_QUAL_NAME:
  813. case DEMANGLE_COMPONENT_LOCAL_NAME:
  814. case DEMANGLE_COMPONENT_TYPED_NAME:
  815. case DEMANGLE_COMPONENT_TAGGED_NAME:
  816. case DEMANGLE_COMPONENT_TEMPLATE:
  817. case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  818. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  819. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  820. case DEMANGLE_COMPONENT_UNARY:
  821. case DEMANGLE_COMPONENT_BINARY:
  822. case DEMANGLE_COMPONENT_BINARY_ARGS:
  823. case DEMANGLE_COMPONENT_TRINARY:
  824. case DEMANGLE_COMPONENT_TRINARY_ARG1:
  825. case DEMANGLE_COMPONENT_LITERAL:
  826. case DEMANGLE_COMPONENT_LITERAL_NEG:
  827. case DEMANGLE_COMPONENT_VENDOR_EXPR:
  828. case DEMANGLE_COMPONENT_COMPOUND_NAME:
  829. case DEMANGLE_COMPONENT_VECTOR_TYPE:
  830. case DEMANGLE_COMPONENT_CLONE:
  831. if (left == NULL || right == NULL)
  832. return NULL;
  833. break;
  834. /* These types only require one parameter. */
  835. case DEMANGLE_COMPONENT_VTABLE:
  836. case DEMANGLE_COMPONENT_VTT:
  837. case DEMANGLE_COMPONENT_TYPEINFO:
  838. case DEMANGLE_COMPONENT_TYPEINFO_NAME:
  839. case DEMANGLE_COMPONENT_TYPEINFO_FN:
  840. case DEMANGLE_COMPONENT_THUNK:
  841. case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
  842. case DEMANGLE_COMPONENT_COVARIANT_THUNK:
  843. case DEMANGLE_COMPONENT_JAVA_CLASS:
  844. case DEMANGLE_COMPONENT_GUARD:
  845. case DEMANGLE_COMPONENT_TLS_INIT:
  846. case DEMANGLE_COMPONENT_TLS_WRAPPER:
  847. case DEMANGLE_COMPONENT_REFTEMP:
  848. case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
  849. case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
  850. case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
  851. case DEMANGLE_COMPONENT_POINTER:
  852. case DEMANGLE_COMPONENT_REFERENCE:
  853. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  854. case DEMANGLE_COMPONENT_COMPLEX:
  855. case DEMANGLE_COMPONENT_IMAGINARY:
  856. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  857. case DEMANGLE_COMPONENT_CAST:
  858. case DEMANGLE_COMPONENT_CONVERSION:
  859. case DEMANGLE_COMPONENT_JAVA_RESOURCE:
  860. case DEMANGLE_COMPONENT_DECLTYPE:
  861. case DEMANGLE_COMPONENT_PACK_EXPANSION:
  862. case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
  863. case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
  864. case DEMANGLE_COMPONENT_NULLARY:
  865. case DEMANGLE_COMPONENT_TRINARY_ARG2:
  866. case DEMANGLE_COMPONENT_TPARM_OBJ:
  867. if (left == NULL)
  868. return NULL;
  869. break;
  870. /* This needs a right parameter, but the left parameter can be
  871. empty. */
  872. case DEMANGLE_COMPONENT_ARRAY_TYPE:
  873. case DEMANGLE_COMPONENT_INITIALIZER_LIST:
  874. if (right == NULL)
  875. return NULL;
  876. break;
  877. /* These are allowed to have no parameters--in some cases they
  878. will be filled in later. */
  879. case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  880. case DEMANGLE_COMPONENT_RESTRICT:
  881. case DEMANGLE_COMPONENT_VOLATILE:
  882. case DEMANGLE_COMPONENT_CONST:
  883. case DEMANGLE_COMPONENT_ARGLIST:
  884. case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  885. FNQUAL_COMPONENT_CASE:
  886. break;
  887. /* Other types should not be seen here. */
  888. default:
  889. return NULL;
  890. }
  891. p = d_make_empty (di);
  892. if (p != NULL)
  893. {
  894. p->type = type;
  895. p->u.s_binary.left = left;
  896. p->u.s_binary.right = right;
  897. }
  898. return p;
  899. }
  900. /* Add a new demangle mangled name component. */
  901. static struct demangle_component *
  902. d_make_demangle_mangled_name (struct d_info *di, const char *s)
  903. {
  904. if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
  905. return d_make_name (di, s, strlen (s));
  906. d_advance (di, 2);
  907. return d_encoding (di, 0);
  908. }
  909. /* Add a new name component. */
  910. static struct demangle_component *
  911. d_make_name (struct d_info *di, const char *s, int len)
  912. {
  913. struct demangle_component *p;
  914. p = d_make_empty (di);
  915. if (! cplus_demangle_fill_name (p, s, len))
  916. return NULL;
  917. return p;
  918. }
  919. /* Add a new builtin type component. */
  920. static struct demangle_component *
  921. d_make_builtin_type (struct d_info *di,
  922. const struct demangle_builtin_type_info *type)
  923. {
  924. struct demangle_component *p;
  925. if (type == NULL)
  926. return NULL;
  927. p = d_make_empty (di);
  928. if (p != NULL)
  929. {
  930. p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
  931. p->u.s_builtin.type = type;
  932. }
  933. return p;
  934. }
  935. /* Add a new operator component. */
  936. static struct demangle_component *
  937. d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
  938. {
  939. struct demangle_component *p;
  940. p = d_make_empty (di);
  941. if (p != NULL)
  942. {
  943. p->type = DEMANGLE_COMPONENT_OPERATOR;
  944. p->u.s_operator.op = op;
  945. }
  946. return p;
  947. }
  948. /* Add a new extended operator component. */
  949. static struct demangle_component *
  950. d_make_extended_operator (struct d_info *di, int args,
  951. struct demangle_component *name)
  952. {
  953. struct demangle_component *p;
  954. p = d_make_empty (di);
  955. if (! cplus_demangle_fill_extended_operator (p, args, name))
  956. return NULL;
  957. return p;
  958. }
  959. static struct demangle_component *
  960. d_make_default_arg (struct d_info *di, int num,
  961. struct demangle_component *sub)
  962. {
  963. struct demangle_component *p = d_make_empty (di);
  964. if (p)
  965. {
  966. p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
  967. p->u.s_unary_num.num = num;
  968. p->u.s_unary_num.sub = sub;
  969. }
  970. return p;
  971. }
  972. /* Add a new constructor component. */
  973. static struct demangle_component *
  974. d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
  975. struct demangle_component *name)
  976. {
  977. struct demangle_component *p;
  978. p = d_make_empty (di);
  979. if (! cplus_demangle_fill_ctor (p, kind, name))
  980. return NULL;
  981. return p;
  982. }
  983. /* Add a new destructor component. */
  984. static struct demangle_component *
  985. d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
  986. struct demangle_component *name)
  987. {
  988. struct demangle_component *p;
  989. p = d_make_empty (di);
  990. if (! cplus_demangle_fill_dtor (p, kind, name))
  991. return NULL;
  992. return p;
  993. }
  994. /* Add a new template parameter. */
  995. static struct demangle_component *
  996. d_make_template_param (struct d_info *di, int i)
  997. {
  998. struct demangle_component *p;
  999. p = d_make_empty (di);
  1000. if (p != NULL)
  1001. {
  1002. p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
  1003. p->u.s_number.number = i;
  1004. }
  1005. return p;
  1006. }
  1007. /* Add a new function parameter. */
  1008. static struct demangle_component *
  1009. d_make_function_param (struct d_info *di, int i)
  1010. {
  1011. struct demangle_component *p;
  1012. p = d_make_empty (di);
  1013. if (p != NULL)
  1014. {
  1015. p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
  1016. p->u.s_number.number = i;
  1017. }
  1018. return p;
  1019. }
  1020. /* Add a new standard substitution component. */
  1021. static struct demangle_component *
  1022. d_make_sub (struct d_info *di, const char *name, int len)
  1023. {
  1024. struct demangle_component *p;
  1025. p = d_make_empty (di);
  1026. if (p != NULL)
  1027. {
  1028. p->type = DEMANGLE_COMPONENT_SUB_STD;
  1029. p->u.s_string.string = name;
  1030. p->u.s_string.len = len;
  1031. }
  1032. return p;
  1033. }
  1034. /* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
  1035. TOP_LEVEL is non-zero when called at the top level. */
  1036. CP_STATIC_IF_GLIBCPP_V3
  1037. struct demangle_component *
  1038. cplus_demangle_mangled_name (struct d_info *di, int top_level)
  1039. {
  1040. struct demangle_component *p;
  1041. if (! d_check_char (di, '_')
  1042. /* Allow missing _ if not at toplevel to work around a
  1043. bug in G++ abi-version=2 mangling; see the comment in
  1044. write_template_arg. */
  1045. && top_level)
  1046. return NULL;
  1047. if (! d_check_char (di, 'Z'))
  1048. return NULL;
  1049. p = d_encoding (di, top_level);
  1050. /* If at top level and parsing parameters, check for a clone
  1051. suffix. */
  1052. if (top_level && (di->options & DMGL_PARAMS) != 0)
  1053. while (d_peek_char (di) == '.'
  1054. && (IS_LOWER (d_peek_next_char (di))
  1055. || d_peek_next_char (di) == '_'
  1056. || IS_DIGIT (d_peek_next_char (di))))
  1057. p = d_clone_suffix (di, p);
  1058. return p;
  1059. }
  1060. /* Return whether a function should have a return type. The argument
  1061. is the function name, which may be qualified in various ways. The
  1062. rules are that template functions have return types with some
  1063. exceptions, function types which are not part of a function name
  1064. mangling have return types with some exceptions, and non-template
  1065. function names do not have return types. The exceptions are that
  1066. constructors, destructors, and conversion operators do not have
  1067. return types. */
  1068. static int
  1069. has_return_type (struct demangle_component *dc)
  1070. {
  1071. if (dc == NULL)
  1072. return 0;
  1073. switch (dc->type)
  1074. {
  1075. default:
  1076. return 0;
  1077. case DEMANGLE_COMPONENT_LOCAL_NAME:
  1078. return has_return_type (d_right (dc));
  1079. case DEMANGLE_COMPONENT_TEMPLATE:
  1080. return ! is_ctor_dtor_or_conversion (d_left (dc));
  1081. FNQUAL_COMPONENT_CASE:
  1082. return has_return_type (d_left (dc));
  1083. }
  1084. }
  1085. /* Return whether a name is a constructor, a destructor, or a
  1086. conversion operator. */
  1087. static int
  1088. is_ctor_dtor_or_conversion (struct demangle_component *dc)
  1089. {
  1090. if (dc == NULL)
  1091. return 0;
  1092. switch (dc->type)
  1093. {
  1094. default:
  1095. return 0;
  1096. case DEMANGLE_COMPONENT_QUAL_NAME:
  1097. case DEMANGLE_COMPONENT_LOCAL_NAME:
  1098. return is_ctor_dtor_or_conversion (d_right (dc));
  1099. case DEMANGLE_COMPONENT_CTOR:
  1100. case DEMANGLE_COMPONENT_DTOR:
  1101. case DEMANGLE_COMPONENT_CONVERSION:
  1102. return 1;
  1103. }
  1104. }
  1105. /* <encoding> ::= <(function) name> <bare-function-type>
  1106. ::= <(data) name>
  1107. ::= <special-name>
  1108. TOP_LEVEL is non-zero when called at the top level, in which case
  1109. if DMGL_PARAMS is not set we do not demangle the function
  1110. parameters. We only set this at the top level, because otherwise
  1111. we would not correctly demangle names in local scopes. */
  1112. static struct demangle_component *
  1113. d_encoding (struct d_info *di, int top_level)
  1114. {
  1115. char peek = d_peek_char (di);
  1116. struct demangle_component *dc;
  1117. if (peek == 'G' || peek == 'T')
  1118. dc = d_special_name (di);
  1119. else
  1120. {
  1121. dc = d_name (di);
  1122. if (!dc)
  1123. /* Failed already. */;
  1124. else if (top_level && (di->options & DMGL_PARAMS) == 0)
  1125. {
  1126. /* Strip off any initial CV-qualifiers, as they really apply
  1127. to the `this' parameter, and they were not output by the
  1128. v2 demangler without DMGL_PARAMS. */
  1129. while (is_fnqual_component_type (dc->type))
  1130. dc = d_left (dc);
  1131. /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
  1132. there may be function-qualifiers on its right argument which
  1133. really apply here; this happens when parsing a class
  1134. which is local to a function. */
  1135. if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
  1136. {
  1137. while (d_right (dc) != NULL
  1138. && is_fnqual_component_type (d_right (dc)->type))
  1139. d_right (dc) = d_left (d_right (dc));
  1140. if (d_right (dc) == NULL)
  1141. dc = NULL;
  1142. }
  1143. }
  1144. else
  1145. {
  1146. peek = d_peek_char (di);
  1147. if (peek != '\0' && peek != 'E')
  1148. {
  1149. struct demangle_component *ftype;
  1150. ftype = d_bare_function_type (di, has_return_type (dc));
  1151. if (ftype)
  1152. {
  1153. /* If this is a non-top-level local-name, clear the
  1154. return type, so it doesn't confuse the user by
  1155. being confused with the return type of whaever
  1156. this is nested within. */
  1157. if (!top_level && dc->type == DEMANGLE_COMPONENT_LOCAL_NAME
  1158. && ftype->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
  1159. d_left (ftype) = NULL;
  1160. dc = d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME,
  1161. dc, ftype);
  1162. }
  1163. else
  1164. dc = NULL;
  1165. }
  1166. }
  1167. }
  1168. return dc;
  1169. }
  1170. /* <tagged-name> ::= <name> B <source-name> */
  1171. static struct demangle_component *
  1172. d_abi_tags (struct d_info *di, struct demangle_component *dc)
  1173. {
  1174. struct demangle_component *hold_last_name;
  1175. char peek;
  1176. /* Preserve the last name, so the ABI tag doesn't clobber it. */
  1177. hold_last_name = di->last_name;
  1178. while (peek = d_peek_char (di),
  1179. peek == 'B')
  1180. {
  1181. struct demangle_component *tag;
  1182. d_advance (di, 1);
  1183. tag = d_source_name (di);
  1184. dc = d_make_comp (di, DEMANGLE_COMPONENT_TAGGED_NAME, dc, tag);
  1185. }
  1186. di->last_name = hold_last_name;
  1187. return dc;
  1188. }
  1189. /* <name> ::= <nested-name>
  1190. ::= <unscoped-name>
  1191. ::= <unscoped-template-name> <template-args>
  1192. ::= <local-name>
  1193. <unscoped-name> ::= <unqualified-name>
  1194. ::= St <unqualified-name>
  1195. <unscoped-template-name> ::= <unscoped-name>
  1196. ::= <substitution>
  1197. */
  1198. static struct demangle_component *
  1199. d_name (struct d_info *di)
  1200. {
  1201. char peek = d_peek_char (di);
  1202. struct demangle_component *dc;
  1203. switch (peek)
  1204. {
  1205. case 'N':
  1206. return d_nested_name (di);
  1207. case 'Z':
  1208. return d_local_name (di);
  1209. case 'U':
  1210. return d_unqualified_name (di);
  1211. case 'S':
  1212. {
  1213. int subst;
  1214. if (d_peek_next_char (di) != 't')
  1215. {
  1216. dc = d_substitution (di, 0);
  1217. subst = 1;
  1218. }
  1219. else
  1220. {
  1221. d_advance (di, 2);
  1222. dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
  1223. d_make_name (di, "std", 3),
  1224. d_unqualified_name (di));
  1225. di->expansion += 3;
  1226. subst = 0;
  1227. }
  1228. if (d_peek_char (di) != 'I')
  1229. {
  1230. /* The grammar does not permit this case to occur if we
  1231. called d_substitution() above (i.e., subst == 1). We
  1232. don't bother to check. */
  1233. }
  1234. else
  1235. {
  1236. /* This is <template-args>, which means that we just saw
  1237. <unscoped-template-name>, which is a substitution
  1238. candidate if we didn't just get it from a
  1239. substitution. */
  1240. if (! subst)
  1241. {
  1242. if (! d_add_substitution (di, dc))
  1243. return NULL;
  1244. }
  1245. dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
  1246. d_template_args (di));
  1247. }
  1248. return dc;
  1249. }
  1250. case 'L':
  1251. default:
  1252. dc = d_unqualified_name (di);
  1253. if (d_peek_char (di) == 'I')
  1254. {
  1255. /* This is <template-args>, which means that we just saw
  1256. <unscoped-template-name>, which is a substitution
  1257. candidate. */
  1258. if (! d_add_substitution (di, dc))
  1259. return NULL;
  1260. dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
  1261. d_template_args (di));
  1262. }
  1263. return dc;
  1264. }
  1265. }
  1266. /* <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E
  1267. ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E
  1268. */
  1269. static struct demangle_component *
  1270. d_nested_name (struct d_info *di)
  1271. {
  1272. struct demangle_component *ret;
  1273. struct demangle_component **pret;
  1274. struct demangle_component *rqual;
  1275. if (! d_check_char (di, 'N'))
  1276. return NULL;
  1277. pret = d_cv_qualifiers (di, &ret, 1);
  1278. if (pret == NULL)
  1279. return NULL;
  1280. /* Parse the ref-qualifier now and then attach it
  1281. once we have something to attach it to. */
  1282. rqual = d_ref_qualifier (di, NULL);
  1283. *pret = d_prefix (di, 1);
  1284. if (*pret == NULL)
  1285. return NULL;
  1286. if (rqual)
  1287. {
  1288. d_left (rqual) = ret;
  1289. ret = rqual;
  1290. }
  1291. if (! d_check_char (di, 'E'))
  1292. return NULL;
  1293. return ret;
  1294. }
  1295. /* <prefix> ::= <prefix> <unqualified-name>
  1296. ::= <template-prefix> <template-args>
  1297. ::= <template-param>
  1298. ::= <decltype>
  1299. ::=
  1300. ::= <substitution>
  1301. <template-prefix> ::= <prefix> <(template) unqualified-name>
  1302. ::= <template-param>
  1303. ::= <substitution>
  1304. SUBST is true if we should add substitutions (as normal), false
  1305. if not (in an unresolved-name). */
  1306. static struct demangle_component *
  1307. d_prefix (struct d_info *di, int subst)
  1308. {
  1309. struct demangle_component *ret = NULL;
  1310. while (1)
  1311. {
  1312. char peek;
  1313. enum demangle_component_type comb_type;
  1314. struct demangle_component *dc;
  1315. peek = d_peek_char (di);
  1316. if (peek == '\0')
  1317. return NULL;
  1318. /* The older code accepts a <local-name> here, but I don't see
  1319. that in the grammar. The older code does not accept a
  1320. <template-param> here. */
  1321. comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
  1322. if (peek == 'D')
  1323. {
  1324. char peek2 = d_peek_next_char (di);
  1325. if (peek2 == 'T' || peek2 == 't')
  1326. /* Decltype. */
  1327. dc = cplus_demangle_type (di);
  1328. else
  1329. /* Destructor name. */
  1330. dc = d_unqualified_name (di);
  1331. }
  1332. else if (IS_DIGIT (peek)
  1333. || IS_LOWER (peek)
  1334. || peek == 'C'
  1335. || peek == 'U'
  1336. || peek == 'L')
  1337. dc = d_unqualified_name (di);
  1338. else if (peek == 'S')
  1339. dc = d_substitution (di, 1);
  1340. else if (peek == 'I')
  1341. {
  1342. if (ret == NULL)
  1343. return NULL;
  1344. comb_type = DEMANGLE_COMPONENT_TEMPLATE;
  1345. dc = d_template_args (di);
  1346. }
  1347. else if (peek == 'T')
  1348. dc = d_template_param (di);
  1349. else if (peek == 'E')
  1350. return ret;
  1351. else if (peek == 'M')
  1352. {
  1353. /* Initializer scope for a lambda. We don't need to represent
  1354. this; the normal code will just treat the variable as a type
  1355. scope, which gives appropriate output. */
  1356. if (ret == NULL)
  1357. return NULL;
  1358. d_advance (di, 1);
  1359. continue;
  1360. }
  1361. else
  1362. return NULL;
  1363. if (ret == NULL)
  1364. ret = dc;
  1365. else
  1366. ret = d_make_comp (di, comb_type, ret, dc);
  1367. if (peek != 'S' && d_peek_char (di) != 'E' && subst)
  1368. {
  1369. if (! d_add_substitution (di, ret))
  1370. return NULL;
  1371. }
  1372. }
  1373. }
  1374. /* <unqualified-name> ::= <operator-name>
  1375. ::= <ctor-dtor-name>
  1376. ::= <source-name>
  1377. ::= <local-source-name>
  1378. <local-source-name> ::= L <source-name> <discriminator>
  1379. */
  1380. static struct demangle_component *
  1381. d_unqualified_name (struct d_info *di)
  1382. {
  1383. struct demangle_component *ret;
  1384. char peek;
  1385. peek = d_peek_char (di);
  1386. if (IS_DIGIT (peek))
  1387. ret = d_source_name (di);
  1388. else if (IS_LOWER (peek))
  1389. {
  1390. int was_expr = di->is_expression;
  1391. if (peek == 'o' && d_peek_next_char (di) == 'n')
  1392. {
  1393. d_advance (di, 2);
  1394. /* Treat cv as naming a conversion operator. */
  1395. di->is_expression = 0;
  1396. }
  1397. ret = d_operator_name (di);
  1398. di->is_expression = was_expr;
  1399. if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
  1400. {
  1401. di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
  1402. if (!strcmp (ret->u.s_operator.op->code, "li"))
  1403. ret = d_make_comp (di, DEMANGLE_COMPONENT_UNARY, ret,
  1404. d_source_name (di));
  1405. }
  1406. }
  1407. else if (peek == 'C' || peek == 'D')
  1408. ret = d_ctor_dtor_name (di);
  1409. else if (peek == 'L')
  1410. {
  1411. d_advance (di, 1);
  1412. ret = d_source_name (di);
  1413. if (ret == NULL)
  1414. return NULL;
  1415. if (! d_discriminator (di))
  1416. return NULL;
  1417. }
  1418. else if (peek == 'U')
  1419. {
  1420. switch (d_peek_next_char (di))
  1421. {
  1422. case 'l':
  1423. ret = d_lambda (di);
  1424. break;
  1425. case 't':
  1426. ret = d_unnamed_type (di);
  1427. break;
  1428. default:
  1429. return NULL;
  1430. }
  1431. }
  1432. else
  1433. return NULL;
  1434. if (d_peek_char (di) == 'B')
  1435. ret = d_abi_tags (di, ret);
  1436. return ret;
  1437. }
  1438. /* <source-name> ::= <(positive length) number> <identifier> */
  1439. static struct demangle_component *
  1440. d_source_name (struct d_info *di)
  1441. {
  1442. int len;
  1443. struct demangle_component *ret;
  1444. len = d_number (di);
  1445. if (len <= 0)
  1446. return NULL;
  1447. ret = d_identifier (di, len);
  1448. di->last_name = ret;
  1449. return ret;
  1450. }
  1451. /* number ::= [n] <(non-negative decimal integer)> */
  1452. static int
  1453. d_number (struct d_info *di)
  1454. {
  1455. int negative;
  1456. char peek;
  1457. int ret;
  1458. negative = 0;
  1459. peek = d_peek_char (di);
  1460. if (peek == 'n')
  1461. {
  1462. negative = 1;
  1463. d_advance (di, 1);
  1464. peek = d_peek_char (di);
  1465. }
  1466. ret = 0;
  1467. while (1)
  1468. {
  1469. if (! IS_DIGIT (peek))
  1470. {
  1471. if (negative)
  1472. ret = - ret;
  1473. return ret;
  1474. }
  1475. if (ret > ((INT_MAX - (peek - '0')) / 10))
  1476. return -1;
  1477. ret = ret * 10 + (peek - '0');
  1478. d_advance (di, 1);
  1479. peek = d_peek_char (di);
  1480. }
  1481. }
  1482. /* Like d_number, but returns a demangle_component. */
  1483. static struct demangle_component *
  1484. d_number_component (struct d_info *di)
  1485. {
  1486. struct demangle_component *ret = d_make_empty (di);
  1487. if (ret)
  1488. {
  1489. ret->type = DEMANGLE_COMPONENT_NUMBER;
  1490. ret->u.s_number.number = d_number (di);
  1491. }
  1492. return ret;
  1493. }
  1494. /* identifier ::= <(unqualified source code identifier)> */
  1495. static struct demangle_component *
  1496. d_identifier (struct d_info *di, int len)
  1497. {
  1498. const char *name;
  1499. name = d_str (di);
  1500. if (di->send - name < len)
  1501. return NULL;
  1502. d_advance (di, len);
  1503. /* A Java mangled name may have a trailing '$' if it is a C++
  1504. keyword. This '$' is not included in the length count. We just
  1505. ignore the '$'. */
  1506. if ((di->options & DMGL_JAVA) != 0
  1507. && d_peek_char (di) == '$')
  1508. d_advance (di, 1);
  1509. /* Look for something which looks like a gcc encoding of an
  1510. anonymous namespace, and replace it with a more user friendly
  1511. name. */
  1512. if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
  1513. && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
  1514. ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
  1515. {
  1516. const char *s;
  1517. s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
  1518. if ((*s == '.' || *s == '_' || *s == '$')
  1519. && s[1] == 'N')
  1520. {
  1521. di->expansion -= len - sizeof "(anonymous namespace)";
  1522. return d_make_name (di, "(anonymous namespace)",
  1523. sizeof "(anonymous namespace)" - 1);
  1524. }
  1525. }
  1526. return d_make_name (di, name, len);
  1527. }
  1528. /* operator_name ::= many different two character encodings.
  1529. ::= cv <type>
  1530. ::= v <digit> <source-name>
  1531. This list is sorted for binary search. */
  1532. #define NL(s) s, (sizeof s) - 1
  1533. CP_STATIC_IF_GLIBCPP_V3
  1534. const struct demangle_operator_info cplus_demangle_operators[] =
  1535. {
  1536. { "aN", NL ("&="), 2 },
  1537. { "aS", NL ("="), 2 },
  1538. { "aa", NL ("&&"), 2 },
  1539. { "ad", NL ("&"), 1 },
  1540. { "an", NL ("&"), 2 },
  1541. { "at", NL ("alignof "), 1 },
  1542. { "aw", NL ("co_await "), 1 },
  1543. { "az", NL ("alignof "), 1 },
  1544. { "cc", NL ("const_cast"), 2 },
  1545. { "cl", NL ("()"), 2 },
  1546. { "cm", NL (","), 2 },
  1547. { "co", NL ("~"), 1 },
  1548. { "dV", NL ("/="), 2 },
  1549. { "dX", NL ("[...]="), 3 }, /* [expr...expr] = expr */
  1550. { "da", NL ("delete[] "), 1 },
  1551. { "dc", NL ("dynamic_cast"), 2 },
  1552. { "de", NL ("*"), 1 },
  1553. { "di", NL ("="), 2 }, /* .name = expr */
  1554. { "dl", NL ("delete "), 1 },
  1555. { "ds", NL (".*"), 2 },
  1556. { "dt", NL ("."), 2 },
  1557. { "dv", NL ("/"), 2 },
  1558. { "dx", NL ("]="), 2 }, /* [expr] = expr */
  1559. { "eO", NL ("^="), 2 },
  1560. { "eo", NL ("^"), 2 },
  1561. { "eq", NL ("=="), 2 },
  1562. { "fL", NL ("..."), 3 },
  1563. { "fR", NL ("..."), 3 },
  1564. { "fl", NL ("..."), 2 },
  1565. { "fr", NL ("..."), 2 },
  1566. { "ge", NL (">="), 2 },
  1567. { "gs", NL ("::"), 1 },
  1568. { "gt", NL (">"), 2 },
  1569. { "ix", NL ("[]"), 2 },
  1570. { "lS", NL ("<<="), 2 },
  1571. { "le", NL ("<="), 2 },
  1572. { "li", NL ("operator\"\" "), 1 },
  1573. { "ls", NL ("<<"), 2 },
  1574. { "lt", NL ("<"), 2 },
  1575. { "mI", NL ("-="), 2 },
  1576. { "mL", NL ("*="), 2 },
  1577. { "mi", NL ("-"), 2 },
  1578. { "ml", NL ("*"), 2 },
  1579. { "mm", NL ("--"), 1 },
  1580. { "na", NL ("new[]"), 3 },
  1581. { "ne", NL ("!="), 2 },
  1582. { "ng", NL ("-"), 1 },
  1583. { "nt", NL ("!"), 1 },
  1584. { "nw", NL ("new"), 3 },
  1585. { "oR", NL ("|="), 2 },
  1586. { "oo", NL ("||"), 2 },
  1587. { "or", NL ("|"), 2 },
  1588. { "pL", NL ("+="), 2 },
  1589. { "pl", NL ("+"), 2 },
  1590. { "pm", NL ("->*"), 2 },
  1591. { "pp", NL ("++"), 1 },
  1592. { "ps", NL ("+"), 1 },
  1593. { "pt", NL ("->"), 2 },
  1594. { "qu", NL ("?"), 3 },
  1595. { "rM", NL ("%="), 2 },
  1596. { "rS", NL (">>="), 2 },
  1597. { "rc", NL ("reinterpret_cast"), 2 },
  1598. { "rm", NL ("%"), 2 },
  1599. { "rs", NL (">>"), 2 },
  1600. { "sP", NL ("sizeof..."), 1 },
  1601. { "sZ", NL ("sizeof..."), 1 },
  1602. { "sc", NL ("static_cast"), 2 },
  1603. { "ss", NL ("<=>"), 2 },
  1604. { "st", NL ("sizeof "), 1 },
  1605. { "sz", NL ("sizeof "), 1 },
  1606. { "tr", NL ("throw"), 0 },
  1607. { "tw", NL ("throw "), 1 },
  1608. { NULL, NULL, 0, 0 }
  1609. };
  1610. static struct demangle_component *
  1611. d_operator_name (struct d_info *di)
  1612. {
  1613. char c1;
  1614. char c2;
  1615. c1 = d_next_char (di);
  1616. c2 = d_next_char (di);
  1617. if (c1 == 'v' && IS_DIGIT (c2))
  1618. return d_make_extended_operator (di, c2 - '0', d_source_name (di));
  1619. else if (c1 == 'c' && c2 == 'v')
  1620. {
  1621. struct demangle_component *type;
  1622. int was_conversion = di->is_conversion;
  1623. struct demangle_component *res;
  1624. di->is_conversion = ! di->is_expression;
  1625. type = cplus_demangle_type (di);
  1626. if (di->is_conversion)
  1627. res = d_make_comp (di, DEMANGLE_COMPONENT_CONVERSION, type, NULL);
  1628. else
  1629. res = d_make_comp (di, DEMANGLE_COMPONENT_CAST, type, NULL);
  1630. di->is_conversion = was_conversion;
  1631. return res;
  1632. }
  1633. else
  1634. {
  1635. /* LOW is the inclusive lower bound. */
  1636. int low = 0;
  1637. /* HIGH is the exclusive upper bound. We subtract one to ignore
  1638. the sentinel at the end of the array. */
  1639. int high = ((sizeof (cplus_demangle_operators)
  1640. / sizeof (cplus_demangle_operators[0]))
  1641. - 1);
  1642. while (1)
  1643. {
  1644. int i;
  1645. const struct demangle_operator_info *p;
  1646. i = low + (high - low) / 2;
  1647. p = cplus_demangle_operators + i;
  1648. if (c1 == p->code[0] && c2 == p->code[1])
  1649. return d_make_operator (di, p);
  1650. if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
  1651. high = i;
  1652. else
  1653. low = i + 1;
  1654. if (low == high)
  1655. return NULL;
  1656. }
  1657. }
  1658. }
  1659. static struct demangle_component *
  1660. d_make_character (struct d_info *di, int c)
  1661. {
  1662. struct demangle_component *p;
  1663. p = d_make_empty (di);
  1664. if (p != NULL)
  1665. {
  1666. p->type = DEMANGLE_COMPONENT_CHARACTER;
  1667. p->u.s_character.character = c;
  1668. }
  1669. return p;
  1670. }
  1671. static struct demangle_component *
  1672. d_java_resource (struct d_info *di)
  1673. {
  1674. struct demangle_component *p = NULL;
  1675. struct demangle_component *next = NULL;
  1676. int len, i;
  1677. char c;
  1678. const char *str;
  1679. len = d_number (di);
  1680. if (len <= 1)
  1681. return NULL;
  1682. /* Eat the leading '_'. */
  1683. if (d_next_char (di) != '_')
  1684. return NULL;
  1685. len--;
  1686. str = d_str (di);
  1687. i = 0;
  1688. while (len > 0)
  1689. {
  1690. c = str[i];
  1691. if (!c)
  1692. return NULL;
  1693. /* Each chunk is either a '$' escape... */
  1694. if (c == '$')
  1695. {
  1696. i++;
  1697. switch (str[i++])
  1698. {
  1699. case 'S':
  1700. c = '/';
  1701. break;
  1702. case '_':
  1703. c = '.';
  1704. break;
  1705. case '$':
  1706. c = '$';
  1707. break;
  1708. default:
  1709. return NULL;
  1710. }
  1711. next = d_make_character (di, c);
  1712. d_advance (di, i);
  1713. str = d_str (di);
  1714. len -= i;
  1715. i = 0;
  1716. if (next == NULL)
  1717. return NULL;
  1718. }
  1719. /* ... or a sequence of characters. */
  1720. else
  1721. {
  1722. while (i < len && str[i] && str[i] != '$')
  1723. i++;
  1724. next = d_make_name (di, str, i);
  1725. d_advance (di, i);
  1726. str = d_str (di);
  1727. len -= i;
  1728. i = 0;
  1729. if (next == NULL)
  1730. return NULL;
  1731. }
  1732. if (p == NULL)
  1733. p = next;
  1734. else
  1735. {
  1736. p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
  1737. if (p == NULL)
  1738. return NULL;
  1739. }
  1740. }
  1741. p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
  1742. return p;
  1743. }
  1744. /* <special-name> ::= TV <type>
  1745. ::= TT <type>
  1746. ::= TI <type>
  1747. ::= TS <type>
  1748. ::= TA <template-arg>
  1749. ::= GV <(object) name>
  1750. ::= T <call-offset> <(base) encoding>
  1751. ::= Tc <call-offset> <call-offset> <(base) encoding>
  1752. Also g++ extensions:
  1753. ::= TC <type> <(offset) number> _ <(base) type>
  1754. ::= TF <type>
  1755. ::= TJ <type>
  1756. ::= GR <name>
  1757. ::= GA <encoding>
  1758. ::= Gr <resource name>
  1759. ::= GTt <encoding>
  1760. ::= GTn <encoding>
  1761. */
  1762. static struct demangle_component *
  1763. d_special_name (struct d_info *di)
  1764. {
  1765. di->expansion += 20;
  1766. if (d_check_char (di, 'T'))
  1767. {
  1768. switch (d_next_char (di))
  1769. {
  1770. case 'V':
  1771. di->expansion -= 5;
  1772. return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
  1773. cplus_demangle_type (di), NULL);
  1774. case 'T':
  1775. di->expansion -= 10;
  1776. return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
  1777. cplus_demangle_type (di), NULL);
  1778. case 'I':
  1779. return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
  1780. cplus_demangle_type (di), NULL);
  1781. case 'S':
  1782. return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
  1783. cplus_demangle_type (di), NULL);
  1784. case 'h':
  1785. if (! d_call_offset (di, 'h'))
  1786. return NULL;
  1787. return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
  1788. d_encoding (di, 0), NULL);
  1789. case 'v':
  1790. if (! d_call_offset (di, 'v'))
  1791. return NULL;
  1792. return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
  1793. d_encoding (di, 0), NULL);
  1794. case 'c':
  1795. if (! d_call_offset (di, '\0'))
  1796. return NULL;
  1797. if (! d_call_offset (di, '\0'))
  1798. return NULL;
  1799. return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
  1800. d_encoding (di, 0), NULL);
  1801. case 'C':
  1802. {
  1803. struct demangle_component *derived_type;
  1804. int offset;
  1805. struct demangle_component *base_type;
  1806. derived_type = cplus_demangle_type (di);
  1807. offset = d_number (di);
  1808. if (offset < 0)
  1809. return NULL;
  1810. if (! d_check_char (di, '_'))
  1811. return NULL;
  1812. base_type = cplus_demangle_type (di);
  1813. /* We don't display the offset. FIXME: We should display
  1814. it in verbose mode. */
  1815. di->expansion += 5;
  1816. return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
  1817. base_type, derived_type);
  1818. }
  1819. case 'F':
  1820. return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
  1821. cplus_demangle_type (di), NULL);
  1822. case 'J':
  1823. return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
  1824. cplus_demangle_type (di), NULL);
  1825. case 'H':
  1826. return d_make_comp (di, DEMANGLE_COMPONENT_TLS_INIT,
  1827. d_name (di), NULL);
  1828. case 'W':
  1829. return d_make_comp (di, DEMANGLE_COMPONENT_TLS_WRAPPER,
  1830. d_name (di), NULL);
  1831. case 'A':
  1832. return d_make_comp (di, DEMANGLE_COMPONENT_TPARM_OBJ,
  1833. d_template_arg (di), NULL);
  1834. default:
  1835. return NULL;
  1836. }
  1837. }
  1838. else if (d_check_char (di, 'G'))
  1839. {
  1840. switch (d_next_char (di))
  1841. {
  1842. case 'V':
  1843. return d_make_comp (di, DEMANGLE_COMPONENT_GUARD,
  1844. d_name (di), NULL);
  1845. case 'R':
  1846. {
  1847. struct demangle_component *name = d_name (di);
  1848. return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
  1849. d_number_component (di));
  1850. }
  1851. case 'A':
  1852. return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
  1853. d_encoding (di, 0), NULL);
  1854. case 'T':
  1855. switch (d_next_char (di))
  1856. {
  1857. case 'n':
  1858. return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
  1859. d_encoding (di, 0), NULL);
  1860. default:
  1861. /* ??? The proposal is that other letters (such as 'h') stand
  1862. for different variants of transaction cloning, such as
  1863. compiling directly for hardware transaction support. But
  1864. they still should all be transactional clones of some sort
  1865. so go ahead and call them that. */
  1866. case 't':
  1867. return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
  1868. d_encoding (di, 0), NULL);
  1869. }
  1870. case 'r':
  1871. return d_java_resource (di);
  1872. default:
  1873. return NULL;
  1874. }
  1875. }
  1876. else
  1877. return NULL;
  1878. }
  1879. /* <call-offset> ::= h <nv-offset> _
  1880. ::= v <v-offset> _
  1881. <nv-offset> ::= <(offset) number>
  1882. <v-offset> ::= <(offset) number> _ <(virtual offset) number>
  1883. The C parameter, if not '\0', is a character we just read which is
  1884. the start of the <call-offset>.
  1885. We don't display the offset information anywhere. FIXME: We should
  1886. display it in verbose mode. */
  1887. static int
  1888. d_call_offset (struct d_info *di, int c)
  1889. {
  1890. if (c == '\0')
  1891. c = d_next_char (di);
  1892. if (c == 'h')
  1893. d_number (di);
  1894. else if (c == 'v')
  1895. {
  1896. d_number (di);
  1897. if (! d_check_char (di, '_'))
  1898. return 0;
  1899. d_number (di);
  1900. }
  1901. else
  1902. return 0;
  1903. if (! d_check_char (di, '_'))
  1904. return 0;
  1905. return 1;
  1906. }
  1907. /* <ctor-dtor-name> ::= C1
  1908. ::= C2
  1909. ::= C3
  1910. ::= D0
  1911. ::= D1
  1912. ::= D2
  1913. */
  1914. static struct demangle_component *
  1915. d_ctor_dtor_name (struct d_info *di)
  1916. {
  1917. if (di->last_name != NULL)
  1918. {
  1919. if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
  1920. di->expansion += di->last_name->u.s_name.len;
  1921. else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
  1922. di->expansion += di->last_name->u.s_string.len;
  1923. }
  1924. switch (d_peek_char (di))
  1925. {
  1926. case 'C':
  1927. {
  1928. enum gnu_v3_ctor_kinds kind;
  1929. int inheriting = 0;
  1930. if (d_peek_next_char (di) == 'I')
  1931. {
  1932. inheriting = 1;
  1933. d_advance (di, 1);
  1934. }
  1935. switch (d_peek_next_char (di))
  1936. {
  1937. case '1':
  1938. kind = gnu_v3_complete_object_ctor;
  1939. break;
  1940. case '2':
  1941. kind = gnu_v3_base_object_ctor;
  1942. break;
  1943. case '3':
  1944. kind = gnu_v3_complete_object_allocating_ctor;
  1945. break;
  1946. case '4':
  1947. kind = gnu_v3_unified_ctor;
  1948. break;
  1949. case '5':
  1950. kind = gnu_v3_object_ctor_group;
  1951. break;
  1952. default:
  1953. return NULL;
  1954. }
  1955. d_advance (di, 2);
  1956. if (inheriting)
  1957. cplus_demangle_type (di);
  1958. return d_make_ctor (di, kind, di->last_name);
  1959. }
  1960. case 'D':
  1961. {
  1962. enum gnu_v3_dtor_kinds kind;
  1963. switch (d_peek_next_char (di))
  1964. {
  1965. case '0':
  1966. kind = gnu_v3_deleting_dtor;
  1967. break;
  1968. case '1':
  1969. kind = gnu_v3_complete_object_dtor;
  1970. break;
  1971. case '2':
  1972. kind = gnu_v3_base_object_dtor;
  1973. break;
  1974. /* digit '3' is not used */
  1975. case '4':
  1976. kind = gnu_v3_unified_dtor;
  1977. break;
  1978. case '5':
  1979. kind = gnu_v3_object_dtor_group;
  1980. break;
  1981. default:
  1982. return NULL;
  1983. }
  1984. d_advance (di, 2);
  1985. return d_make_dtor (di, kind, di->last_name);
  1986. }
  1987. default:
  1988. return NULL;
  1989. }
  1990. }
  1991. /* True iff we're looking at an order-insensitive type-qualifier, including
  1992. function-type-qualifiers. */
  1993. static int
  1994. next_is_type_qual (struct d_info *di)
  1995. {
  1996. char peek = d_peek_char (di);
  1997. if (peek == 'r' || peek == 'V' || peek == 'K')
  1998. return 1;
  1999. if (peek == 'D')
  2000. {
  2001. peek = d_peek_next_char (di);
  2002. if (peek == 'x' || peek == 'o' || peek == 'O' || peek == 'w')
  2003. return 1;
  2004. }
  2005. return 0;
  2006. }
  2007. /* <type> ::= <builtin-type>
  2008. ::= <function-type>
  2009. ::= <class-enum-type>
  2010. ::= <array-type>
  2011. ::= <pointer-to-member-type>
  2012. ::= <template-param>
  2013. ::= <template-template-param> <template-args>
  2014. ::= <substitution>
  2015. ::= <CV-qualifiers> <type>
  2016. ::= P <type>
  2017. ::= R <type>
  2018. ::= O <type> (C++0x)
  2019. ::= C <type>
  2020. ::= G <type>
  2021. ::= U <source-name> <type>
  2022. <builtin-type> ::= various one letter codes
  2023. ::= u <source-name>
  2024. */
  2025. CP_STATIC_IF_GLIBCPP_V3
  2026. const struct demangle_builtin_type_info
  2027. cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
  2028. {
  2029. /* a */ { NL ("signed char"), NL ("signed char"), D_PRINT_DEFAULT },
  2030. /* b */ { NL ("bool"), NL ("boolean"), D_PRINT_BOOL },
  2031. /* c */ { NL ("char"), NL ("byte"), D_PRINT_DEFAULT },
  2032. /* d */ { NL ("double"), NL ("double"), D_PRINT_FLOAT },
  2033. /* e */ { NL ("long double"), NL ("long double"), D_PRINT_FLOAT },
  2034. /* f */ { NL ("float"), NL ("float"), D_PRINT_FLOAT },
  2035. /* g */ { NL ("__float128"), NL ("__float128"), D_PRINT_FLOAT },
  2036. /* h */ { NL ("unsigned char"), NL ("unsigned char"), D_PRINT_DEFAULT },
  2037. /* i */ { NL ("int"), NL ("int"), D_PRINT_INT },
  2038. /* j */ { NL ("unsigned int"), NL ("unsigned"), D_PRINT_UNSIGNED },
  2039. /* k */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
  2040. /* l */ { NL ("long"), NL ("long"), D_PRINT_LONG },
  2041. /* m */ { NL ("unsigned long"), NL ("unsigned long"), D_PRINT_UNSIGNED_LONG },
  2042. /* n */ { NL ("__int128"), NL ("__int128"), D_PRINT_DEFAULT },
  2043. /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
  2044. D_PRINT_DEFAULT },
  2045. /* p */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
  2046. /* q */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
  2047. /* r */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
  2048. /* s */ { NL ("short"), NL ("short"), D_PRINT_DEFAULT },
  2049. /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
  2050. /* u */ { NULL, 0, NULL, 0, D_PRINT_DEFAULT },
  2051. /* v */ { NL ("void"), NL ("void"), D_PRINT_VOID },
  2052. /* w */ { NL ("wchar_t"), NL ("char"), D_PRINT_DEFAULT },
  2053. /* x */ { NL ("long long"), NL ("long"), D_PRINT_LONG_LONG },
  2054. /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
  2055. D_PRINT_UNSIGNED_LONG_LONG },
  2056. /* z */ { NL ("..."), NL ("..."), D_PRINT_DEFAULT },
  2057. /* 26 */ { NL ("decimal32"), NL ("decimal32"), D_PRINT_DEFAULT },
  2058. /* 27 */ { NL ("decimal64"), NL ("decimal64"), D_PRINT_DEFAULT },
  2059. /* 28 */ { NL ("decimal128"), NL ("decimal128"), D_PRINT_DEFAULT },
  2060. /* 29 */ { NL ("half"), NL ("half"), D_PRINT_FLOAT },
  2061. /* 30 */ { NL ("char8_t"), NL ("char8_t"), D_PRINT_DEFAULT },
  2062. /* 31 */ { NL ("char16_t"), NL ("char16_t"), D_PRINT_DEFAULT },
  2063. /* 32 */ { NL ("char32_t"), NL ("char32_t"), D_PRINT_DEFAULT },
  2064. /* 33 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
  2065. D_PRINT_DEFAULT },
  2066. };
  2067. CP_STATIC_IF_GLIBCPP_V3
  2068. struct demangle_component *
  2069. cplus_demangle_type (struct d_info *di)
  2070. {
  2071. char peek;
  2072. struct demangle_component *ret;
  2073. int can_subst;
  2074. /* The ABI specifies that when CV-qualifiers are used, the base type
  2075. is substitutable, and the fully qualified type is substitutable,
  2076. but the base type with a strict subset of the CV-qualifiers is
  2077. not substitutable. The natural recursive implementation of the
  2078. CV-qualifiers would cause subsets to be substitutable, so instead
  2079. we pull them all off now.
  2080. FIXME: The ABI says that order-insensitive vendor qualifiers
  2081. should be handled in the same way, but we have no way to tell
  2082. which vendor qualifiers are order-insensitive and which are
  2083. order-sensitive. So we just assume that they are all
  2084. order-sensitive. g++ 3.4 supports only one vendor qualifier,
  2085. __vector, and it treats it as order-sensitive when mangling
  2086. names. */
  2087. if (next_is_type_qual (di))
  2088. {
  2089. struct demangle_component **pret;
  2090. pret = d_cv_qualifiers (di, &ret, 0);
  2091. if (pret == NULL)
  2092. return NULL;
  2093. if (d_peek_char (di) == 'F')
  2094. {
  2095. /* cv-qualifiers before a function type apply to 'this',
  2096. so avoid adding the unqualified function type to
  2097. the substitution list. */
  2098. *pret = d_function_type (di);
  2099. }
  2100. else
  2101. *pret = cplus_demangle_type (di);
  2102. if (!*pret)
  2103. return NULL;
  2104. if ((*pret)->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS
  2105. || (*pret)->type == DEMANGLE_COMPONENT_REFERENCE_THIS)
  2106. {
  2107. /* Move the ref-qualifier outside the cv-qualifiers so that
  2108. they are printed in the right order. */
  2109. struct demangle_component *fn = d_left (*pret);
  2110. d_left (*pret) = ret;
  2111. ret = *pret;
  2112. *pret = fn;
  2113. }
  2114. if (! d_add_substitution (di, ret))
  2115. return NULL;
  2116. return ret;
  2117. }
  2118. can_subst = 1;
  2119. peek = d_peek_char (di);
  2120. switch (peek)
  2121. {
  2122. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
  2123. case 'h': case 'i': case 'j': case 'l': case 'm': case 'n':
  2124. case 'o': case 's': case 't':
  2125. case 'v': case 'w': case 'x': case 'y': case 'z':
  2126. ret = d_make_builtin_type (di,
  2127. &cplus_demangle_builtin_types[peek - 'a']);
  2128. di->expansion += ret->u.s_builtin.type->len;
  2129. can_subst = 0;
  2130. d_advance (di, 1);
  2131. break;
  2132. case 'u':
  2133. d_advance (di, 1);
  2134. ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
  2135. d_source_name (di), NULL);
  2136. break;
  2137. case 'F':
  2138. ret = d_function_type (di);
  2139. break;
  2140. case '0': case '1': case '2': case '3': case '4':
  2141. case '5': case '6': case '7': case '8': case '9':
  2142. case 'N':
  2143. case 'Z':
  2144. ret = d_class_enum_type (di);
  2145. break;
  2146. case 'A':
  2147. ret = d_array_type (di);
  2148. break;
  2149. case 'M':
  2150. ret = d_pointer_to_member_type (di);
  2151. break;
  2152. case 'T':
  2153. ret = d_template_param (di);
  2154. if (d_peek_char (di) == 'I')
  2155. {
  2156. /* This may be <template-template-param> <template-args>.
  2157. If this is the type for a conversion operator, we can
  2158. have a <template-template-param> here only by following
  2159. a derivation like this:
  2160. <nested-name>
  2161. -> <template-prefix> <template-args>
  2162. -> <prefix> <template-unqualified-name> <template-args>
  2163. -> <unqualified-name> <template-unqualified-name> <template-args>
  2164. -> <source-name> <template-unqualified-name> <template-args>
  2165. -> <source-name> <operator-name> <template-args>
  2166. -> <source-name> cv <type> <template-args>
  2167. -> <source-name> cv <template-template-param> <template-args> <template-args>
  2168. where the <template-args> is followed by another.
  2169. Otherwise, we must have a derivation like this:
  2170. <nested-name>
  2171. -> <template-prefix> <template-args>
  2172. -> <prefix> <template-unqualified-name> <template-args>
  2173. -> <unqualified-name> <template-unqualified-name> <template-args>
  2174. -> <source-name> <template-unqualified-name> <template-args>
  2175. -> <source-name> <operator-name> <template-args>
  2176. -> <source-name> cv <type> <template-args>
  2177. -> <source-name> cv <template-param> <template-args>
  2178. where we need to leave the <template-args> to be processed
  2179. by d_prefix (following the <template-prefix>).
  2180. The <template-template-param> part is a substitution
  2181. candidate. */
  2182. if (! di->is_conversion)
  2183. {
  2184. if (! d_add_substitution (di, ret))
  2185. return NULL;
  2186. ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
  2187. d_template_args (di));
  2188. }
  2189. else
  2190. {
  2191. struct demangle_component *args;
  2192. struct d_info_checkpoint checkpoint;
  2193. d_checkpoint (di, &checkpoint);
  2194. args = d_template_args (di);
  2195. if (d_peek_char (di) == 'I')
  2196. {
  2197. if (! d_add_substitution (di, ret))
  2198. return NULL;
  2199. ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
  2200. args);
  2201. }
  2202. else
  2203. d_backtrack (di, &checkpoint);
  2204. }
  2205. }
  2206. break;
  2207. case 'S':
  2208. /* If this is a special substitution, then it is the start of
  2209. <class-enum-type>. */
  2210. {
  2211. char peek_next;
  2212. peek_next = d_peek_next_char (di);
  2213. if (IS_DIGIT (peek_next)
  2214. || peek_next == '_'
  2215. || IS_UPPER (peek_next))
  2216. {
  2217. ret = d_substitution (di, 0);
  2218. /* The substituted name may have been a template name and
  2219. may be followed by tepmlate args. */
  2220. if (d_peek_char (di) == 'I')
  2221. ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
  2222. d_template_args (di));
  2223. else
  2224. can_subst = 0;
  2225. }
  2226. else
  2227. {
  2228. ret = d_class_enum_type (di);
  2229. /* If the substitution was a complete type, then it is not
  2230. a new substitution candidate. However, if the
  2231. substitution was followed by template arguments, then
  2232. the whole thing is a substitution candidate. */
  2233. if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
  2234. can_subst = 0;
  2235. }
  2236. }
  2237. break;
  2238. case 'O':
  2239. d_advance (di, 1);
  2240. ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
  2241. cplus_demangle_type (di), NULL);
  2242. break;
  2243. case 'P':
  2244. d_advance (di, 1);
  2245. ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
  2246. cplus_demangle_type (di), NULL);
  2247. break;
  2248. case 'R':
  2249. d_advance (di, 1);
  2250. ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
  2251. cplus_demangle_type (di), NULL);
  2252. break;
  2253. case 'C':
  2254. d_advance (di, 1);
  2255. ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
  2256. cplus_demangle_type (di), NULL);
  2257. break;
  2258. case 'G':
  2259. d_advance (di, 1);
  2260. ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
  2261. cplus_demangle_type (di), NULL);
  2262. break;
  2263. case 'U':
  2264. d_advance (di, 1);
  2265. ret = d_source_name (di);
  2266. if (d_peek_char (di) == 'I')
  2267. ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
  2268. d_template_args (di));
  2269. ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
  2270. cplus_demangle_type (di), ret);
  2271. break;
  2272. case 'D':
  2273. can_subst = 0;
  2274. d_advance (di, 1);
  2275. peek = d_next_char (di);
  2276. switch (peek)
  2277. {
  2278. case 'T':
  2279. case 't':
  2280. /* decltype (expression) */
  2281. ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
  2282. d_expression (di), NULL);
  2283. if (ret && d_next_char (di) != 'E')
  2284. ret = NULL;
  2285. can_subst = 1;
  2286. break;
  2287. case 'p':
  2288. /* Pack expansion. */
  2289. ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
  2290. cplus_demangle_type (di), NULL);
  2291. can_subst = 1;
  2292. break;
  2293. case 'a':
  2294. /* auto */
  2295. ret = d_make_name (di, "auto", 4);
  2296. break;
  2297. case 'c':
  2298. /* decltype(auto) */
  2299. ret = d_make_name (di, "decltype(auto)", 14);
  2300. break;
  2301. case 'f':
  2302. /* 32-bit decimal floating point */
  2303. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
  2304. di->expansion += ret->u.s_builtin.type->len;
  2305. break;
  2306. case 'd':
  2307. /* 64-bit DFP */
  2308. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
  2309. di->expansion += ret->u.s_builtin.type->len;
  2310. break;
  2311. case 'e':
  2312. /* 128-bit DFP */
  2313. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
  2314. di->expansion += ret->u.s_builtin.type->len;
  2315. break;
  2316. case 'h':
  2317. /* 16-bit half-precision FP */
  2318. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
  2319. di->expansion += ret->u.s_builtin.type->len;
  2320. break;
  2321. case 'u':
  2322. /* char8_t */
  2323. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
  2324. di->expansion += ret->u.s_builtin.type->len;
  2325. break;
  2326. case 's':
  2327. /* char16_t */
  2328. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
  2329. di->expansion += ret->u.s_builtin.type->len;
  2330. break;
  2331. case 'i':
  2332. /* char32_t */
  2333. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
  2334. di->expansion += ret->u.s_builtin.type->len;
  2335. break;
  2336. case 'F':
  2337. /* Fixed point types. DF<int bits><length><fract bits><sat> */
  2338. ret = d_make_empty (di);
  2339. ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
  2340. if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
  2341. /* For demangling we don't care about the bits. */
  2342. d_number (di);
  2343. ret->u.s_fixed.length = cplus_demangle_type (di);
  2344. if (ret->u.s_fixed.length == NULL)
  2345. return NULL;
  2346. d_number (di);
  2347. peek = d_next_char (di);
  2348. ret->u.s_fixed.sat = (peek == 's');
  2349. break;
  2350. case 'v':
  2351. ret = d_vector_type (di);
  2352. can_subst = 1;
  2353. break;
  2354. case 'n':
  2355. /* decltype(nullptr) */
  2356. ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[33]);
  2357. di->expansion += ret->u.s_builtin.type->len;
  2358. break;
  2359. default:
  2360. return NULL;
  2361. }
  2362. break;
  2363. default:
  2364. return NULL;
  2365. }
  2366. if (can_subst)
  2367. {
  2368. if (! d_add_substitution (di, ret))
  2369. return NULL;
  2370. }
  2371. return ret;
  2372. }
  2373. /* <CV-qualifiers> ::= [r] [V] [K] [Dx] */
  2374. static struct demangle_component **
  2375. d_cv_qualifiers (struct d_info *di,
  2376. struct demangle_component **pret, int member_fn)
  2377. {
  2378. struct demangle_component **pstart;
  2379. char peek;
  2380. pstart = pret;
  2381. peek = d_peek_char (di);
  2382. while (next_is_type_qual (di))
  2383. {
  2384. enum demangle_component_type t;
  2385. struct demangle_component *right = NULL;
  2386. d_advance (di, 1);
  2387. if (peek == 'r')
  2388. {
  2389. t = (member_fn
  2390. ? DEMANGLE_COMPONENT_RESTRICT_THIS
  2391. : DEMANGLE_COMPONENT_RESTRICT);
  2392. di->expansion += sizeof "restrict";
  2393. }
  2394. else if (peek == 'V')
  2395. {
  2396. t = (member_fn
  2397. ? DEMANGLE_COMPONENT_VOLATILE_THIS
  2398. : DEMANGLE_COMPONENT_VOLATILE);
  2399. di->expansion += sizeof "volatile";
  2400. }
  2401. else if (peek == 'K')
  2402. {
  2403. t = (member_fn
  2404. ? DEMANGLE_COMPONENT_CONST_THIS
  2405. : DEMANGLE_COMPONENT_CONST);
  2406. di->expansion += sizeof "const";
  2407. }
  2408. else
  2409. {
  2410. peek = d_next_char (di);
  2411. if (peek == 'x')
  2412. {
  2413. t = DEMANGLE_COMPONENT_TRANSACTION_SAFE;
  2414. di->expansion += sizeof "transaction_safe";
  2415. }
  2416. else if (peek == 'o'
  2417. || peek == 'O')
  2418. {
  2419. t = DEMANGLE_COMPONENT_NOEXCEPT;
  2420. di->expansion += sizeof "noexcept";
  2421. if (peek == 'O')
  2422. {
  2423. right = d_expression (di);
  2424. if (right == NULL)
  2425. return NULL;
  2426. if (! d_check_char (di, 'E'))
  2427. return NULL;
  2428. }
  2429. }
  2430. else if (peek == 'w')
  2431. {
  2432. t = DEMANGLE_COMPONENT_THROW_SPEC;
  2433. di->expansion += sizeof "throw";
  2434. right = d_parmlist (di);
  2435. if (right == NULL)
  2436. return NULL;
  2437. if (! d_check_char (di, 'E'))
  2438. return NULL;
  2439. }
  2440. else
  2441. return NULL;
  2442. }
  2443. *pret = d_make_comp (di, t, NULL, right);
  2444. if (*pret == NULL)
  2445. return NULL;
  2446. pret = &d_left (*pret);
  2447. peek = d_peek_char (di);
  2448. }
  2449. if (!member_fn && peek == 'F')
  2450. {
  2451. while (pstart != pret)
  2452. {
  2453. switch ((*pstart)->type)
  2454. {
  2455. case DEMANGLE_COMPONENT_RESTRICT:
  2456. (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
  2457. break;
  2458. case DEMANGLE_COMPONENT_VOLATILE:
  2459. (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
  2460. break;
  2461. case DEMANGLE_COMPONENT_CONST:
  2462. (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
  2463. break;
  2464. default:
  2465. break;
  2466. }
  2467. pstart = &d_left (*pstart);
  2468. }
  2469. }
  2470. return pret;
  2471. }
  2472. /* <ref-qualifier> ::= R
  2473. ::= O */
  2474. static struct demangle_component *
  2475. d_ref_qualifier (struct d_info *di, struct demangle_component *sub)
  2476. {
  2477. struct demangle_component *ret = sub;
  2478. char peek;
  2479. peek = d_peek_char (di);
  2480. if (peek == 'R' || peek == 'O')
  2481. {
  2482. enum demangle_component_type t;
  2483. if (peek == 'R')
  2484. {
  2485. t = DEMANGLE_COMPONENT_REFERENCE_THIS;
  2486. di->expansion += sizeof "&";
  2487. }
  2488. else
  2489. {
  2490. t = DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS;
  2491. di->expansion += sizeof "&&";
  2492. }
  2493. d_advance (di, 1);
  2494. ret = d_make_comp (di, t, ret, NULL);
  2495. }
  2496. return ret;
  2497. }
  2498. /* <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] [T] E */
  2499. static struct demangle_component *
  2500. d_function_type (struct d_info *di)
  2501. {
  2502. struct demangle_component *ret = NULL;
  2503. if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
  2504. {
  2505. if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
  2506. /* FIXME: There ought to be a way to report
  2507. that the recursion limit has been reached. */
  2508. return NULL;
  2509. di->recursion_level ++;
  2510. }
  2511. if (d_check_char (di, 'F'))
  2512. {
  2513. if (d_peek_char (di) == 'Y')
  2514. {
  2515. /* Function has C linkage. We don't print this information.
  2516. FIXME: We should print it in verbose mode. */
  2517. d_advance (di, 1);
  2518. }
  2519. ret = d_bare_function_type (di, 1);
  2520. ret = d_ref_qualifier (di, ret);
  2521. if (! d_check_char (di, 'E'))
  2522. ret = NULL;
  2523. }
  2524. if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
  2525. di->recursion_level --;
  2526. return ret;
  2527. }
  2528. /* <type>+ */
  2529. static struct demangle_component *
  2530. d_parmlist (struct d_info *di)
  2531. {
  2532. struct demangle_component *tl;
  2533. struct demangle_component **ptl;
  2534. tl = NULL;
  2535. ptl = &tl;
  2536. while (1)
  2537. {
  2538. struct demangle_component *type;
  2539. char peek = d_peek_char (di);
  2540. if (peek == '\0' || peek == 'E' || peek == '.')
  2541. break;
  2542. if ((peek == 'R' || peek == 'O')
  2543. && d_peek_next_char (di) == 'E')
  2544. /* Function ref-qualifier, not a ref prefix for a parameter type. */
  2545. break;
  2546. type = cplus_demangle_type (di);
  2547. if (type == NULL)
  2548. return NULL;
  2549. *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
  2550. if (*ptl == NULL)
  2551. return NULL;
  2552. ptl = &d_right (*ptl);
  2553. }
  2554. /* There should be at least one parameter type besides the optional
  2555. return type. A function which takes no arguments will have a
  2556. single parameter type void. */
  2557. if (tl == NULL)
  2558. return NULL;
  2559. /* If we have a single parameter type void, omit it. */
  2560. if (d_right (tl) == NULL
  2561. && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
  2562. && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
  2563. {
  2564. di->expansion -= d_left (tl)->u.s_builtin.type->len;
  2565. d_left (tl) = NULL;
  2566. }
  2567. return tl;
  2568. }
  2569. /* <bare-function-type> ::= [J]<type>+ */
  2570. static struct demangle_component *
  2571. d_bare_function_type (struct d_info *di, int has_return_type)
  2572. {
  2573. struct demangle_component *return_type;
  2574. struct demangle_component *tl;
  2575. char peek;
  2576. /* Detect special qualifier indicating that the first argument
  2577. is the return type. */
  2578. peek = d_peek_char (di);
  2579. if (peek == 'J')
  2580. {
  2581. d_advance (di, 1);
  2582. has_return_type = 1;
  2583. }
  2584. if (has_return_type)
  2585. {
  2586. return_type = cplus_demangle_type (di);
  2587. if (return_type == NULL)
  2588. return NULL;
  2589. }
  2590. else
  2591. return_type = NULL;
  2592. tl = d_parmlist (di);
  2593. if (tl == NULL)
  2594. return NULL;
  2595. return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
  2596. return_type, tl);
  2597. }
  2598. /* <class-enum-type> ::= <name> */
  2599. static struct demangle_component *
  2600. d_class_enum_type (struct d_info *di)
  2601. {
  2602. return d_name (di);
  2603. }
  2604. /* <array-type> ::= A <(positive dimension) number> _ <(element) type>
  2605. ::= A [<(dimension) expression>] _ <(element) type>
  2606. */
  2607. static struct demangle_component *
  2608. d_array_type (struct d_info *di)
  2609. {
  2610. char peek;
  2611. struct demangle_component *dim;
  2612. if (! d_check_char (di, 'A'))
  2613. return NULL;
  2614. peek = d_peek_char (di);
  2615. if (peek == '_')
  2616. dim = NULL;
  2617. else if (IS_DIGIT (peek))
  2618. {
  2619. const char *s;
  2620. s = d_str (di);
  2621. do
  2622. {
  2623. d_advance (di, 1);
  2624. peek = d_peek_char (di);
  2625. }
  2626. while (IS_DIGIT (peek));
  2627. dim = d_make_name (di, s, d_str (di) - s);
  2628. if (dim == NULL)
  2629. return NULL;
  2630. }
  2631. else
  2632. {
  2633. dim = d_expression (di);
  2634. if (dim == NULL)
  2635. return NULL;
  2636. }
  2637. if (! d_check_char (di, '_'))
  2638. return NULL;
  2639. return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
  2640. cplus_demangle_type (di));
  2641. }
  2642. /* <vector-type> ::= Dv <number> _ <type>
  2643. ::= Dv _ <expression> _ <type> */
  2644. static struct demangle_component *
  2645. d_vector_type (struct d_info *di)
  2646. {
  2647. char peek;
  2648. struct demangle_component *dim;
  2649. peek = d_peek_char (di);
  2650. if (peek == '_')
  2651. {
  2652. d_advance (di, 1);
  2653. dim = d_expression (di);
  2654. }
  2655. else
  2656. dim = d_number_component (di);
  2657. if (dim == NULL)
  2658. return NULL;
  2659. if (! d_check_char (di, '_'))
  2660. return NULL;
  2661. return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
  2662. cplus_demangle_type (di));
  2663. }
  2664. /* <pointer-to-member-type> ::= M <(class) type> <(member) type> */
  2665. static struct demangle_component *
  2666. d_pointer_to_member_type (struct d_info *di)
  2667. {
  2668. struct demangle_component *cl;
  2669. struct demangle_component *mem;
  2670. if (! d_check_char (di, 'M'))
  2671. return NULL;
  2672. cl = cplus_demangle_type (di);
  2673. if (cl == NULL)
  2674. return NULL;
  2675. /* The ABI says, "The type of a non-static member function is considered
  2676. to be different, for the purposes of substitution, from the type of a
  2677. namespace-scope or static member function whose type appears
  2678. similar. The types of two non-static member functions are considered
  2679. to be different, for the purposes of substitution, if the functions
  2680. are members of different classes. In other words, for the purposes of
  2681. substitution, the class of which the function is a member is
  2682. considered part of the type of function."
  2683. For a pointer to member function, this call to cplus_demangle_type
  2684. will end up adding a (possibly qualified) non-member function type to
  2685. the substitution table, which is not correct; however, the member
  2686. function type will never be used in a substitution, so putting the
  2687. wrong type in the substitution table is harmless. */
  2688. mem = cplus_demangle_type (di);
  2689. if (mem == NULL)
  2690. return NULL;
  2691. return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
  2692. }
  2693. /* <non-negative number> _ */
  2694. static int
  2695. d_compact_number (struct d_info *di)
  2696. {
  2697. int num;
  2698. if (d_peek_char (di) == '_')
  2699. num = 0;
  2700. else if (d_peek_char (di) == 'n')
  2701. return -1;
  2702. else
  2703. num = d_number (di) + 1;
  2704. if (num < 0 || ! d_check_char (di, '_'))
  2705. return -1;
  2706. return num;
  2707. }
  2708. /* <template-param> ::= T_
  2709. ::= T <(parameter-2 non-negative) number> _
  2710. */
  2711. static struct demangle_component *
  2712. d_template_param (struct d_info *di)
  2713. {
  2714. int param;
  2715. if (! d_check_char (di, 'T'))
  2716. return NULL;
  2717. param = d_compact_number (di);
  2718. if (param < 0)
  2719. return NULL;
  2720. return d_make_template_param (di, param);
  2721. }
  2722. /* <template-args> ::= I <template-arg>+ E */
  2723. static struct demangle_component *
  2724. d_template_args (struct d_info *di)
  2725. {
  2726. if (d_peek_char (di) != 'I'
  2727. && d_peek_char (di) != 'J')
  2728. return NULL;
  2729. d_advance (di, 1);
  2730. return d_template_args_1 (di);
  2731. }
  2732. /* <template-arg>* E */
  2733. static struct demangle_component *
  2734. d_template_args_1 (struct d_info *di)
  2735. {
  2736. struct demangle_component *hold_last_name;
  2737. struct demangle_component *al;
  2738. struct demangle_component **pal;
  2739. /* Preserve the last name we saw--don't let the template arguments
  2740. clobber it, as that would give us the wrong name for a subsequent
  2741. constructor or destructor. */
  2742. hold_last_name = di->last_name;
  2743. if (d_peek_char (di) == 'E')
  2744. {
  2745. /* An argument pack can be empty. */
  2746. d_advance (di, 1);
  2747. return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
  2748. }
  2749. al = NULL;
  2750. pal = &al;
  2751. while (1)
  2752. {
  2753. struct demangle_component *a;
  2754. a = d_template_arg (di);
  2755. if (a == NULL)
  2756. return NULL;
  2757. *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
  2758. if (*pal == NULL)
  2759. return NULL;
  2760. pal = &d_right (*pal);
  2761. if (d_peek_char (di) == 'E')
  2762. {
  2763. d_advance (di, 1);
  2764. break;
  2765. }
  2766. }
  2767. di->last_name = hold_last_name;
  2768. return al;
  2769. }
  2770. /* <template-arg> ::= <type>
  2771. ::= X <expression> E
  2772. ::= <expr-primary>
  2773. */
  2774. static struct demangle_component *
  2775. d_template_arg (struct d_info *di)
  2776. {
  2777. struct demangle_component *ret;
  2778. switch (d_peek_char (di))
  2779. {
  2780. case 'X':
  2781. d_advance (di, 1);
  2782. ret = d_expression (di);
  2783. if (! d_check_char (di, 'E'))
  2784. return NULL;
  2785. return ret;
  2786. case 'L':
  2787. return d_expr_primary (di);
  2788. case 'I':
  2789. case 'J':
  2790. /* An argument pack. */
  2791. return d_template_args (di);
  2792. default:
  2793. return cplus_demangle_type (di);
  2794. }
  2795. }
  2796. /* Parse a sequence of expressions until we hit the terminator
  2797. character. */
  2798. static struct demangle_component *
  2799. d_exprlist (struct d_info *di, char terminator)
  2800. {
  2801. struct demangle_component *list = NULL;
  2802. struct demangle_component **p = &list;
  2803. if (d_peek_char (di) == terminator)
  2804. {
  2805. d_advance (di, 1);
  2806. return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
  2807. }
  2808. while (1)
  2809. {
  2810. struct demangle_component *arg = d_expression (di);
  2811. if (arg == NULL)
  2812. return NULL;
  2813. *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
  2814. if (*p == NULL)
  2815. return NULL;
  2816. p = &d_right (*p);
  2817. if (d_peek_char (di) == terminator)
  2818. {
  2819. d_advance (di, 1);
  2820. break;
  2821. }
  2822. }
  2823. return list;
  2824. }
  2825. /* Returns nonzero iff OP is an operator for a C++ cast: const_cast,
  2826. dynamic_cast, static_cast or reinterpret_cast. */
  2827. static int
  2828. op_is_new_cast (struct demangle_component *op)
  2829. {
  2830. const char *code = op->u.s_operator.op->code;
  2831. return (code[1] == 'c'
  2832. && (code[0] == 's' || code[0] == 'd'
  2833. || code[0] == 'c' || code[0] == 'r'));
  2834. }
  2835. /* <unresolved-name> ::= [gs] <base-unresolved-name> # x or (with "gs") ::x
  2836. ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x
  2837. # T::N::x /decltype(p)::N::x
  2838. ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
  2839. # A::x, N::y, A<T>::z; "gs" means leading "::"
  2840. ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>
  2841. "gs" is handled elsewhere, as a unary operator. */
  2842. static struct demangle_component *
  2843. d_unresolved_name (struct d_info *di)
  2844. {
  2845. struct demangle_component *type;
  2846. struct demangle_component *name;
  2847. char peek;
  2848. /* Consume the "sr". */
  2849. d_advance (di, 2);
  2850. peek = d_peek_char (di);
  2851. if (di->unresolved_name_state
  2852. && (IS_DIGIT (peek)
  2853. || IS_LOWER (peek)
  2854. || peek == 'C'
  2855. || peek == 'U'
  2856. || peek == 'L'))
  2857. {
  2858. /* The third production is ambiguous with the old unresolved-name syntax
  2859. of <type> <base-unresolved-name>; in the old mangling, A::x was mangled
  2860. as sr1A1x, now sr1AE1x. So we first try to demangle using the new
  2861. mangling, then with the old if that fails. */
  2862. di->unresolved_name_state = -1;
  2863. type = d_prefix (di, 0);
  2864. if (d_peek_char (di) == 'E')
  2865. d_advance (di, 1);
  2866. }
  2867. else
  2868. type = cplus_demangle_type (di);
  2869. name = d_unqualified_name (di);
  2870. if (d_peek_char (di) == 'I')
  2871. name = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
  2872. d_template_args (di));
  2873. return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
  2874. }
  2875. /* <expression> ::= <(unary) operator-name> <expression>
  2876. ::= <(binary) operator-name> <expression> <expression>
  2877. ::= <(trinary) operator-name> <expression> <expression> <expression>
  2878. ::= cl <expression>+ E
  2879. ::= st <type>
  2880. ::= <template-param>
  2881. ::= u <source-name> <template-arg>* E # vendor extended expression
  2882. ::= <unresolved-name>
  2883. ::= <expr-primary>
  2884. <braced-expression> ::= <expression>
  2885. ::= di <field source-name> <braced-expression> # .name = expr
  2886. ::= dx <index expression> <braced-expression> # [expr] = expr
  2887. ::= dX <range begin expression> <range end expression> <braced-expression>
  2888. # [expr ... expr] = expr
  2889. */
  2890. static struct demangle_component *
  2891. d_expression_1 (struct d_info *di)
  2892. {
  2893. char peek;
  2894. peek = d_peek_char (di);
  2895. if (peek == 'L')
  2896. return d_expr_primary (di);
  2897. else if (peek == 'T')
  2898. return d_template_param (di);
  2899. else if (peek == 's' && d_peek_next_char (di) == 'r')
  2900. return d_unresolved_name (di);
  2901. else if (peek == 's' && d_peek_next_char (di) == 'p')
  2902. {
  2903. d_advance (di, 2);
  2904. return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
  2905. d_expression_1 (di), NULL);
  2906. }
  2907. else if (peek == 'f' && d_peek_next_char (di) == 'p')
  2908. {
  2909. /* Function parameter used in a late-specified return type. */
  2910. int index;
  2911. d_advance (di, 2);
  2912. if (d_peek_char (di) == 'T')
  2913. {
  2914. /* 'this' parameter. */
  2915. d_advance (di, 1);
  2916. index = 0;
  2917. }
  2918. else
  2919. {
  2920. index = d_compact_number (di);
  2921. if (index == INT_MAX || index == -1)
  2922. return NULL;
  2923. index++;
  2924. }
  2925. return d_make_function_param (di, index);
  2926. }
  2927. else if (IS_DIGIT (peek)
  2928. || (peek == 'o' && d_peek_next_char (di) == 'n'))
  2929. {
  2930. /* We can get an unqualified name as an expression in the case of
  2931. a dependent function call, i.e. decltype(f(t)). */
  2932. struct demangle_component *name;
  2933. if (peek == 'o')
  2934. /* operator-function-id, i.e. operator+(t). */
  2935. d_advance (di, 2);
  2936. name = d_unqualified_name (di);
  2937. if (name == NULL)
  2938. return NULL;
  2939. if (d_peek_char (di) == 'I')
  2940. return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
  2941. d_template_args (di));
  2942. else
  2943. return name;
  2944. }
  2945. else if ((peek == 'i' || peek == 't')
  2946. && d_peek_next_char (di) == 'l')
  2947. {
  2948. /* Brace-enclosed initializer list, untyped or typed. */
  2949. struct demangle_component *type = NULL;
  2950. d_advance (di, 2);
  2951. if (peek == 't')
  2952. type = cplus_demangle_type (di);
  2953. if (!d_peek_char (di) || !d_peek_next_char (di))
  2954. return NULL;
  2955. return d_make_comp (di, DEMANGLE_COMPONENT_INITIALIZER_LIST,
  2956. type, d_exprlist (di, 'E'));
  2957. }
  2958. else if (peek == 'u')
  2959. {
  2960. /* A vendor extended expression. */
  2961. struct demangle_component *name, *args;
  2962. d_advance (di, 1);
  2963. name = d_source_name (di);
  2964. args = d_template_args_1 (di);
  2965. return d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_EXPR, name, args);
  2966. }
  2967. else
  2968. {
  2969. struct demangle_component *op;
  2970. const char *code = NULL;
  2971. int args;
  2972. op = d_operator_name (di);
  2973. if (op == NULL)
  2974. return NULL;
  2975. if (op->type == DEMANGLE_COMPONENT_OPERATOR)
  2976. {
  2977. code = op->u.s_operator.op->code;
  2978. di->expansion += op->u.s_operator.op->len - 2;
  2979. if (strcmp (code, "st") == 0)
  2980. return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
  2981. cplus_demangle_type (di));
  2982. }
  2983. switch (op->type)
  2984. {
  2985. default:
  2986. return NULL;
  2987. case DEMANGLE_COMPONENT_OPERATOR:
  2988. args = op->u.s_operator.op->args;
  2989. break;
  2990. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  2991. args = op->u.s_extended_operator.args;
  2992. break;
  2993. case DEMANGLE_COMPONENT_CAST:
  2994. args = 1;
  2995. break;
  2996. }
  2997. switch (args)
  2998. {
  2999. case 0:
  3000. return d_make_comp (di, DEMANGLE_COMPONENT_NULLARY, op, NULL);
  3001. case 1:
  3002. {
  3003. struct demangle_component *operand;
  3004. int suffix = 0;
  3005. if (code && (code[0] == 'p' || code[0] == 'm')
  3006. && code[1] == code[0])
  3007. /* pp_ and mm_ are the prefix variants. */
  3008. suffix = !d_check_char (di, '_');
  3009. if (op->type == DEMANGLE_COMPONENT_CAST
  3010. && d_check_char (di, '_'))
  3011. operand = d_exprlist (di, 'E');
  3012. else if (code && !strcmp (code, "sP"))
  3013. operand = d_template_args_1 (di);
  3014. else
  3015. operand = d_expression_1 (di);
  3016. if (suffix)
  3017. /* Indicate the suffix variant for d_print_comp. */
  3018. operand = d_make_comp (di, DEMANGLE_COMPONENT_BINARY_ARGS,
  3019. operand, operand);
  3020. return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op, operand);
  3021. }
  3022. case 2:
  3023. {
  3024. struct demangle_component *left;
  3025. struct demangle_component *right;
  3026. if (code == NULL)
  3027. return NULL;
  3028. if (op_is_new_cast (op))
  3029. left = cplus_demangle_type (di);
  3030. else if (code[0] == 'f')
  3031. /* fold-expression. */
  3032. left = d_operator_name (di);
  3033. else if (!strcmp (code, "di"))
  3034. left = d_unqualified_name (di);
  3035. else
  3036. left = d_expression_1 (di);
  3037. if (!strcmp (code, "cl"))
  3038. right = d_exprlist (di, 'E');
  3039. else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
  3040. {
  3041. peek = d_peek_char (di);
  3042. /* These codes start a qualified name. */
  3043. if ((peek == 'g' && d_peek_next_char (di) == 's')
  3044. || (peek == 's' && d_peek_next_char (di) == 'r'))
  3045. right = d_expression_1 (di);
  3046. else
  3047. {
  3048. /* Otherwise it's an unqualified name. We use
  3049. d_unqualified_name rather than d_expression_1 here for
  3050. old mangled names that didn't add 'on' before operator
  3051. names. */
  3052. right = d_unqualified_name (di);
  3053. if (d_peek_char (di) == 'I')
  3054. right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
  3055. right, d_template_args (di));
  3056. }
  3057. }
  3058. else
  3059. right = d_expression_1 (di);
  3060. return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
  3061. d_make_comp (di,
  3062. DEMANGLE_COMPONENT_BINARY_ARGS,
  3063. left, right));
  3064. }
  3065. case 3:
  3066. {
  3067. struct demangle_component *first;
  3068. struct demangle_component *second;
  3069. struct demangle_component *third;
  3070. if (code == NULL)
  3071. return NULL;
  3072. else if (!strcmp (code, "qu")
  3073. || !strcmp (code, "dX"))
  3074. {
  3075. /* ?: expression. */
  3076. first = d_expression_1 (di);
  3077. second = d_expression_1 (di);
  3078. third = d_expression_1 (di);
  3079. if (third == NULL)
  3080. return NULL;
  3081. }
  3082. else if (code[0] == 'f')
  3083. {
  3084. /* fold-expression. */
  3085. first = d_operator_name (di);
  3086. second = d_expression_1 (di);
  3087. third = d_expression_1 (di);
  3088. if (third == NULL)
  3089. return NULL;
  3090. }
  3091. else if (code[0] == 'n')
  3092. {
  3093. /* new-expression. */
  3094. if (code[1] != 'w' && code[1] != 'a')
  3095. return NULL;
  3096. first = d_exprlist (di, '_');
  3097. second = cplus_demangle_type (di);
  3098. if (d_peek_char (di) == 'E')
  3099. {
  3100. d_advance (di, 1);
  3101. third = NULL;
  3102. }
  3103. else if (d_peek_char (di) == 'p'
  3104. && d_peek_next_char (di) == 'i')
  3105. {
  3106. /* Parenthesized initializer. */
  3107. d_advance (di, 2);
  3108. third = d_exprlist (di, 'E');
  3109. }
  3110. else if (d_peek_char (di) == 'i'
  3111. && d_peek_next_char (di) == 'l')
  3112. /* initializer-list. */
  3113. third = d_expression_1 (di);
  3114. else
  3115. return NULL;
  3116. }
  3117. else
  3118. return NULL;
  3119. return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
  3120. d_make_comp (di,
  3121. DEMANGLE_COMPONENT_TRINARY_ARG1,
  3122. first,
  3123. d_make_comp (di,
  3124. DEMANGLE_COMPONENT_TRINARY_ARG2,
  3125. second, third)));
  3126. }
  3127. default:
  3128. return NULL;
  3129. }
  3130. }
  3131. }
  3132. static struct demangle_component *
  3133. d_expression (struct d_info *di)
  3134. {
  3135. struct demangle_component *ret;
  3136. int was_expression = di->is_expression;
  3137. di->is_expression = 1;
  3138. ret = d_expression_1 (di);
  3139. di->is_expression = was_expression;
  3140. return ret;
  3141. }
  3142. /* <expr-primary> ::= L <type> <(value) number> E
  3143. ::= L <type> <(value) float> E
  3144. ::= L <mangled-name> E
  3145. */
  3146. static struct demangle_component *
  3147. d_expr_primary (struct d_info *di)
  3148. {
  3149. struct demangle_component *ret;
  3150. if (! d_check_char (di, 'L'))
  3151. return NULL;
  3152. if (d_peek_char (di) == '_'
  3153. /* Workaround for G++ bug; see comment in write_template_arg. */
  3154. || d_peek_char (di) == 'Z')
  3155. ret = cplus_demangle_mangled_name (di, 0);
  3156. else
  3157. {
  3158. struct demangle_component *type;
  3159. enum demangle_component_type t;
  3160. const char *s;
  3161. type = cplus_demangle_type (di);
  3162. if (type == NULL)
  3163. return NULL;
  3164. /* If we have a type we know how to print, we aren't going to
  3165. print the type name itself. */
  3166. if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
  3167. && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
  3168. di->expansion -= type->u.s_builtin.type->len;
  3169. if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
  3170. && strcmp (type->u.s_builtin.type->name,
  3171. cplus_demangle_builtin_types[33].name) == 0)
  3172. {
  3173. if (d_peek_char (di) == 'E')
  3174. {
  3175. d_advance (di, 1);
  3176. return type;
  3177. }
  3178. }
  3179. /* Rather than try to interpret the literal value, we just
  3180. collect it as a string. Note that it's possible to have a
  3181. floating point literal here. The ABI specifies that the
  3182. format of such literals is machine independent. That's fine,
  3183. but what's not fine is that versions of g++ up to 3.2 with
  3184. -fabi-version=1 used upper case letters in the hex constant,
  3185. and dumped out gcc's internal representation. That makes it
  3186. hard to tell where the constant ends, and hard to dump the
  3187. constant in any readable form anyhow. We don't attempt to
  3188. handle these cases. */
  3189. t = DEMANGLE_COMPONENT_LITERAL;
  3190. if (d_peek_char (di) == 'n')
  3191. {
  3192. t = DEMANGLE_COMPONENT_LITERAL_NEG;
  3193. d_advance (di, 1);
  3194. }
  3195. s = d_str (di);
  3196. while (d_peek_char (di) != 'E')
  3197. {
  3198. if (d_peek_char (di) == '\0')
  3199. return NULL;
  3200. d_advance (di, 1);
  3201. }
  3202. ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
  3203. }
  3204. if (! d_check_char (di, 'E'))
  3205. return NULL;
  3206. return ret;
  3207. }
  3208. /* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
  3209. ::= Z <(function) encoding> E s [<discriminator>]
  3210. ::= Z <(function) encoding> E d [<parameter> number>] _ <entity name>
  3211. */
  3212. static struct demangle_component *
  3213. d_local_name (struct d_info *di)
  3214. {
  3215. struct demangle_component *function;
  3216. struct demangle_component *name;
  3217. if (! d_check_char (di, 'Z'))
  3218. return NULL;
  3219. function = d_encoding (di, 0);
  3220. if (!function)
  3221. return NULL;
  3222. if (! d_check_char (di, 'E'))
  3223. return NULL;
  3224. if (d_peek_char (di) == 's')
  3225. {
  3226. d_advance (di, 1);
  3227. if (! d_discriminator (di))
  3228. return NULL;
  3229. name = d_make_name (di, "string literal", sizeof "string literal" - 1);
  3230. }
  3231. else
  3232. {
  3233. int num = -1;
  3234. if (d_peek_char (di) == 'd')
  3235. {
  3236. /* Default argument scope: d <number> _. */
  3237. d_advance (di, 1);
  3238. num = d_compact_number (di);
  3239. if (num < 0)
  3240. return NULL;
  3241. }
  3242. name = d_name (di);
  3243. if (name
  3244. /* Lambdas and unnamed types have internal discriminators
  3245. and are not functions. */
  3246. && name->type != DEMANGLE_COMPONENT_LAMBDA
  3247. && name->type != DEMANGLE_COMPONENT_UNNAMED_TYPE)
  3248. {
  3249. /* Read and ignore an optional discriminator. */
  3250. if (! d_discriminator (di))
  3251. return NULL;
  3252. }
  3253. if (num >= 0)
  3254. name = d_make_default_arg (di, num, name);
  3255. }
  3256. /* Elide the return type of the containing function so as to not
  3257. confuse the user thinking it is the return type of whatever local
  3258. function we might be containing. */
  3259. if (function->type == DEMANGLE_COMPONENT_TYPED_NAME
  3260. && d_right (function)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
  3261. d_left (d_right (function)) = NULL;
  3262. return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
  3263. }
  3264. /* <discriminator> ::= _ <number> # when number < 10
  3265. ::= __ <number> _ # when number >= 10
  3266. <discriminator> ::= _ <number> # when number >=10
  3267. is also accepted to support gcc versions that wrongly mangled that way.
  3268. We demangle the discriminator, but we don't print it out. FIXME:
  3269. We should print it out in verbose mode. */
  3270. static int
  3271. d_discriminator (struct d_info *di)
  3272. {
  3273. int discrim, num_underscores = 1;
  3274. if (d_peek_char (di) != '_')
  3275. return 1;
  3276. d_advance (di, 1);
  3277. if (d_peek_char (di) == '_')
  3278. {
  3279. ++num_underscores;
  3280. d_advance (di, 1);
  3281. }
  3282. discrim = d_number (di);
  3283. if (discrim < 0)
  3284. return 0;
  3285. if (num_underscores > 1 && discrim >= 10)
  3286. {
  3287. if (d_peek_char (di) == '_')
  3288. d_advance (di, 1);
  3289. else
  3290. return 0;
  3291. }
  3292. return 1;
  3293. }
  3294. /* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
  3295. static struct demangle_component *
  3296. d_lambda (struct d_info *di)
  3297. {
  3298. struct demangle_component *tl;
  3299. struct demangle_component *ret;
  3300. int num;
  3301. if (! d_check_char (di, 'U'))
  3302. return NULL;
  3303. if (! d_check_char (di, 'l'))
  3304. return NULL;
  3305. tl = d_parmlist (di);
  3306. if (tl == NULL)
  3307. return NULL;
  3308. if (! d_check_char (di, 'E'))
  3309. return NULL;
  3310. num = d_compact_number (di);
  3311. if (num < 0)
  3312. return NULL;
  3313. ret = d_make_empty (di);
  3314. if (ret)
  3315. {
  3316. ret->type = DEMANGLE_COMPONENT_LAMBDA;
  3317. ret->u.s_unary_num.sub = tl;
  3318. ret->u.s_unary_num.num = num;
  3319. }
  3320. return ret;
  3321. }
  3322. /* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
  3323. static struct demangle_component *
  3324. d_unnamed_type (struct d_info *di)
  3325. {
  3326. struct demangle_component *ret;
  3327. int num;
  3328. if (! d_check_char (di, 'U'))
  3329. return NULL;
  3330. if (! d_check_char (di, 't'))
  3331. return NULL;
  3332. num = d_compact_number (di);
  3333. if (num < 0)
  3334. return NULL;
  3335. ret = d_make_empty (di);
  3336. if (ret)
  3337. {
  3338. ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
  3339. ret->u.s_number.number = num;
  3340. }
  3341. if (! d_add_substitution (di, ret))
  3342. return NULL;
  3343. return ret;
  3344. }
  3345. /* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
  3346. */
  3347. static struct demangle_component *
  3348. d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
  3349. {
  3350. const char *suffix = d_str (di);
  3351. const char *pend = suffix;
  3352. struct demangle_component *n;
  3353. if (*pend == '.' && (IS_LOWER (pend[1]) || IS_DIGIT (pend[1])
  3354. || pend[1] == '_'))
  3355. {
  3356. pend += 2;
  3357. while (IS_LOWER (*pend) || IS_DIGIT (*pend) || *pend == '_')
  3358. ++pend;
  3359. }
  3360. while (*pend == '.' && IS_DIGIT (pend[1]))
  3361. {
  3362. pend += 2;
  3363. while (IS_DIGIT (*pend))
  3364. ++pend;
  3365. }
  3366. d_advance (di, pend - suffix);
  3367. n = d_make_name (di, suffix, pend - suffix);
  3368. return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
  3369. }
  3370. /* Add a new substitution. */
  3371. static int
  3372. d_add_substitution (struct d_info *di, struct demangle_component *dc)
  3373. {
  3374. if (dc == NULL)
  3375. return 0;
  3376. if (di->next_sub >= di->num_subs)
  3377. return 0;
  3378. di->subs[di->next_sub] = dc;
  3379. ++di->next_sub;
  3380. return 1;
  3381. }
  3382. /* <substitution> ::= S <seq-id> _
  3383. ::= S_
  3384. ::= St
  3385. ::= Sa
  3386. ::= Sb
  3387. ::= Ss
  3388. ::= Si
  3389. ::= So
  3390. ::= Sd
  3391. If PREFIX is non-zero, then this type is being used as a prefix in
  3392. a qualified name. In this case, for the standard substitutions, we
  3393. need to check whether we are being used as a prefix for a
  3394. constructor or destructor, and return a full template name.
  3395. Otherwise we will get something like std::iostream::~iostream()
  3396. which does not correspond particularly well to any function which
  3397. actually appears in the source.
  3398. */
  3399. static const struct d_standard_sub_info standard_subs[] =
  3400. {
  3401. { 't', NL ("std"),
  3402. NL ("std"),
  3403. NULL, 0 },
  3404. { 'a', NL ("std::allocator"),
  3405. NL ("std::allocator"),
  3406. NL ("allocator") },
  3407. { 'b', NL ("std::basic_string"),
  3408. NL ("std::basic_string"),
  3409. NL ("basic_string") },
  3410. { 's', NL ("std::string"),
  3411. NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
  3412. NL ("basic_string") },
  3413. { 'i', NL ("std::istream"),
  3414. NL ("std::basic_istream<char, std::char_traits<char> >"),
  3415. NL ("basic_istream") },
  3416. { 'o', NL ("std::ostream"),
  3417. NL ("std::basic_ostream<char, std::char_traits<char> >"),
  3418. NL ("basic_ostream") },
  3419. { 'd', NL ("std::iostream"),
  3420. NL ("std::basic_iostream<char, std::char_traits<char> >"),
  3421. NL ("basic_iostream") }
  3422. };
  3423. static struct demangle_component *
  3424. d_substitution (struct d_info *di, int prefix)
  3425. {
  3426. char c;
  3427. if (! d_check_char (di, 'S'))
  3428. return NULL;
  3429. c = d_next_char (di);
  3430. if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
  3431. {
  3432. unsigned int id;
  3433. id = 0;
  3434. if (c != '_')
  3435. {
  3436. do
  3437. {
  3438. unsigned int new_id;
  3439. if (IS_DIGIT (c))
  3440. new_id = id * 36 + c - '0';
  3441. else if (IS_UPPER (c))
  3442. new_id = id * 36 + c - 'A' + 10;
  3443. else
  3444. return NULL;
  3445. if (new_id < id)
  3446. return NULL;
  3447. id = new_id;
  3448. c = d_next_char (di);
  3449. }
  3450. while (c != '_');
  3451. ++id;
  3452. }
  3453. if (id >= (unsigned int) di->next_sub)
  3454. return NULL;
  3455. return di->subs[id];
  3456. }
  3457. else
  3458. {
  3459. int verbose;
  3460. const struct d_standard_sub_info *p;
  3461. const struct d_standard_sub_info *pend;
  3462. verbose = (di->options & DMGL_VERBOSE) != 0;
  3463. if (! verbose && prefix)
  3464. {
  3465. char peek;
  3466. peek = d_peek_char (di);
  3467. if (peek == 'C' || peek == 'D')
  3468. verbose = 1;
  3469. }
  3470. pend = (&standard_subs[0]
  3471. + sizeof standard_subs / sizeof standard_subs[0]);
  3472. for (p = &standard_subs[0]; p < pend; ++p)
  3473. {
  3474. if (c == p->code)
  3475. {
  3476. const char *s;
  3477. int len;
  3478. struct demangle_component *dc;
  3479. if (p->set_last_name != NULL)
  3480. di->last_name = d_make_sub (di, p->set_last_name,
  3481. p->set_last_name_len);
  3482. if (verbose)
  3483. {
  3484. s = p->full_expansion;
  3485. len = p->full_len;
  3486. }
  3487. else
  3488. {
  3489. s = p->simple_expansion;
  3490. len = p->simple_len;
  3491. }
  3492. di->expansion += len;
  3493. dc = d_make_sub (di, s, len);
  3494. if (d_peek_char (di) == 'B')
  3495. {
  3496. /* If there are ABI tags on the abbreviation, it becomes
  3497. a substitution candidate. */
  3498. dc = d_abi_tags (di, dc);
  3499. if (! d_add_substitution (di, dc))
  3500. return NULL;
  3501. }
  3502. return dc;
  3503. }
  3504. }
  3505. return NULL;
  3506. }
  3507. }
  3508. static void
  3509. d_checkpoint (struct d_info *di, struct d_info_checkpoint *checkpoint)
  3510. {
  3511. checkpoint->n = di->n;
  3512. checkpoint->next_comp = di->next_comp;
  3513. checkpoint->next_sub = di->next_sub;
  3514. checkpoint->expansion = di->expansion;
  3515. }
  3516. static void
  3517. d_backtrack (struct d_info *di, struct d_info_checkpoint *checkpoint)
  3518. {
  3519. di->n = checkpoint->n;
  3520. di->next_comp = checkpoint->next_comp;
  3521. di->next_sub = checkpoint->next_sub;
  3522. di->expansion = checkpoint->expansion;
  3523. }
  3524. /* Initialize a growable string. */
  3525. static void
  3526. d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
  3527. {
  3528. dgs->buf = NULL;
  3529. dgs->len = 0;
  3530. dgs->alc = 0;
  3531. dgs->allocation_failure = 0;
  3532. if (estimate > 0)
  3533. d_growable_string_resize (dgs, estimate);
  3534. }
  3535. /* Grow a growable string to a given size. */
  3536. static inline void
  3537. d_growable_string_resize (struct d_growable_string *dgs, size_t need)
  3538. {
  3539. size_t newalc;
  3540. char *newbuf;
  3541. if (dgs->allocation_failure)
  3542. return;
  3543. /* Start allocation at two bytes to avoid any possibility of confusion
  3544. with the special value of 1 used as a return in *palc to indicate
  3545. allocation failures. */
  3546. newalc = dgs->alc > 0 ? dgs->alc : 2;
  3547. while (newalc < need)
  3548. newalc <<= 1;
  3549. newbuf = (char *) realloc (dgs->buf, newalc);
  3550. if (newbuf == NULL)
  3551. {
  3552. free (dgs->buf);
  3553. dgs->buf = NULL;
  3554. dgs->len = 0;
  3555. dgs->alc = 0;
  3556. dgs->allocation_failure = 1;
  3557. return;
  3558. }
  3559. dgs->buf = newbuf;
  3560. dgs->alc = newalc;
  3561. }
  3562. /* Append a buffer to a growable string. */
  3563. static inline void
  3564. d_growable_string_append_buffer (struct d_growable_string *dgs,
  3565. const char *s, size_t l)
  3566. {
  3567. size_t need;
  3568. need = dgs->len + l + 1;
  3569. if (need > dgs->alc)
  3570. d_growable_string_resize (dgs, need);
  3571. if (dgs->allocation_failure)
  3572. return;
  3573. memcpy (dgs->buf + dgs->len, s, l);
  3574. dgs->buf[dgs->len + l] = '\0';
  3575. dgs->len += l;
  3576. }
  3577. /* Bridge growable strings to the callback mechanism. */
  3578. static void
  3579. d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
  3580. {
  3581. struct d_growable_string *dgs = (struct d_growable_string*) opaque;
  3582. d_growable_string_append_buffer (dgs, s, l);
  3583. }
  3584. /* Walk the tree, counting the number of templates encountered, and
  3585. the number of times a scope might be saved. These counts will be
  3586. used to allocate data structures for d_print_comp, so the logic
  3587. here must mirror the logic d_print_comp will use. It is not
  3588. important that the resulting numbers are exact, so long as they
  3589. are larger than the actual numbers encountered. */
  3590. static void
  3591. d_count_templates_scopes (struct d_print_info *dpi,
  3592. struct demangle_component *dc)
  3593. {
  3594. if (dc == NULL || dc->d_counting > 1 || dpi->recursion > MAX_RECURSION_COUNT)
  3595. return;
  3596. ++ dc->d_counting;
  3597. switch (dc->type)
  3598. {
  3599. case DEMANGLE_COMPONENT_NAME:
  3600. case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
  3601. case DEMANGLE_COMPONENT_FUNCTION_PARAM:
  3602. case DEMANGLE_COMPONENT_SUB_STD:
  3603. case DEMANGLE_COMPONENT_BUILTIN_TYPE:
  3604. case DEMANGLE_COMPONENT_OPERATOR:
  3605. case DEMANGLE_COMPONENT_CHARACTER:
  3606. case DEMANGLE_COMPONENT_NUMBER:
  3607. case DEMANGLE_COMPONENT_UNNAMED_TYPE:
  3608. break;
  3609. case DEMANGLE_COMPONENT_TEMPLATE:
  3610. dpi->num_copy_templates++;
  3611. goto recurse_left_right;
  3612. case DEMANGLE_COMPONENT_REFERENCE:
  3613. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  3614. if (d_left (dc)->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
  3615. dpi->num_saved_scopes++;
  3616. goto recurse_left_right;
  3617. case DEMANGLE_COMPONENT_QUAL_NAME:
  3618. case DEMANGLE_COMPONENT_LOCAL_NAME:
  3619. case DEMANGLE_COMPONENT_TYPED_NAME:
  3620. case DEMANGLE_COMPONENT_VTABLE:
  3621. case DEMANGLE_COMPONENT_VTT:
  3622. case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  3623. case DEMANGLE_COMPONENT_TYPEINFO:
  3624. case DEMANGLE_COMPONENT_TYPEINFO_NAME:
  3625. case DEMANGLE_COMPONENT_TYPEINFO_FN:
  3626. case DEMANGLE_COMPONENT_THUNK:
  3627. case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
  3628. case DEMANGLE_COMPONENT_COVARIANT_THUNK:
  3629. case DEMANGLE_COMPONENT_JAVA_CLASS:
  3630. case DEMANGLE_COMPONENT_GUARD:
  3631. case DEMANGLE_COMPONENT_TLS_INIT:
  3632. case DEMANGLE_COMPONENT_TLS_WRAPPER:
  3633. case DEMANGLE_COMPONENT_REFTEMP:
  3634. case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
  3635. case DEMANGLE_COMPONENT_RESTRICT:
  3636. case DEMANGLE_COMPONENT_VOLATILE:
  3637. case DEMANGLE_COMPONENT_CONST:
  3638. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  3639. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  3640. case DEMANGLE_COMPONENT_CONST_THIS:
  3641. case DEMANGLE_COMPONENT_REFERENCE_THIS:
  3642. case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
  3643. case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
  3644. case DEMANGLE_COMPONENT_NOEXCEPT:
  3645. case DEMANGLE_COMPONENT_THROW_SPEC:
  3646. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  3647. case DEMANGLE_COMPONENT_POINTER:
  3648. case DEMANGLE_COMPONENT_COMPLEX:
  3649. case DEMANGLE_COMPONENT_IMAGINARY:
  3650. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  3651. case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  3652. case DEMANGLE_COMPONENT_ARRAY_TYPE:
  3653. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  3654. case DEMANGLE_COMPONENT_VECTOR_TYPE:
  3655. case DEMANGLE_COMPONENT_ARGLIST:
  3656. case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  3657. case DEMANGLE_COMPONENT_TPARM_OBJ:
  3658. case DEMANGLE_COMPONENT_INITIALIZER_LIST:
  3659. case DEMANGLE_COMPONENT_CAST:
  3660. case DEMANGLE_COMPONENT_CONVERSION:
  3661. case DEMANGLE_COMPONENT_NULLARY:
  3662. case DEMANGLE_COMPONENT_UNARY:
  3663. case DEMANGLE_COMPONENT_BINARY:
  3664. case DEMANGLE_COMPONENT_BINARY_ARGS:
  3665. case DEMANGLE_COMPONENT_TRINARY:
  3666. case DEMANGLE_COMPONENT_TRINARY_ARG1:
  3667. case DEMANGLE_COMPONENT_TRINARY_ARG2:
  3668. case DEMANGLE_COMPONENT_LITERAL:
  3669. case DEMANGLE_COMPONENT_LITERAL_NEG:
  3670. case DEMANGLE_COMPONENT_VENDOR_EXPR:
  3671. case DEMANGLE_COMPONENT_JAVA_RESOURCE:
  3672. case DEMANGLE_COMPONENT_COMPOUND_NAME:
  3673. case DEMANGLE_COMPONENT_DECLTYPE:
  3674. case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
  3675. case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
  3676. case DEMANGLE_COMPONENT_PACK_EXPANSION:
  3677. case DEMANGLE_COMPONENT_TAGGED_NAME:
  3678. case DEMANGLE_COMPONENT_CLONE:
  3679. recurse_left_right:
  3680. /* PR 89394 - Check for too much recursion. */
  3681. if (dpi->recursion > DEMANGLE_RECURSION_LIMIT)
  3682. /* FIXME: There ought to be a way to report to the
  3683. user that the recursion limit has been reached. */
  3684. return;
  3685. ++ dpi->recursion;
  3686. d_count_templates_scopes (dpi, d_left (dc));
  3687. d_count_templates_scopes (dpi, d_right (dc));
  3688. -- dpi->recursion;
  3689. break;
  3690. case DEMANGLE_COMPONENT_CTOR:
  3691. d_count_templates_scopes (dpi, dc->u.s_ctor.name);
  3692. break;
  3693. case DEMANGLE_COMPONENT_DTOR:
  3694. d_count_templates_scopes (dpi, dc->u.s_dtor.name);
  3695. break;
  3696. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  3697. d_count_templates_scopes (dpi, dc->u.s_extended_operator.name);
  3698. break;
  3699. case DEMANGLE_COMPONENT_FIXED_TYPE:
  3700. d_count_templates_scopes (dpi, dc->u.s_fixed.length);
  3701. break;
  3702. case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
  3703. case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
  3704. d_count_templates_scopes (dpi, d_left (dc));
  3705. break;
  3706. case DEMANGLE_COMPONENT_LAMBDA:
  3707. case DEMANGLE_COMPONENT_DEFAULT_ARG:
  3708. d_count_templates_scopes (dpi, dc->u.s_unary_num.sub);
  3709. break;
  3710. }
  3711. }
  3712. /* Initialize a print information structure. */
  3713. static void
  3714. d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
  3715. void *opaque, struct demangle_component *dc)
  3716. {
  3717. dpi->len = 0;
  3718. dpi->last_char = '\0';
  3719. dpi->templates = NULL;
  3720. dpi->modifiers = NULL;
  3721. dpi->pack_index = 0;
  3722. dpi->flush_count = 0;
  3723. dpi->callback = callback;
  3724. dpi->opaque = opaque;
  3725. dpi->demangle_failure = 0;
  3726. dpi->recursion = 0;
  3727. dpi->is_lambda_arg = 0;
  3728. dpi->component_stack = NULL;
  3729. dpi->saved_scopes = NULL;
  3730. dpi->next_saved_scope = 0;
  3731. dpi->num_saved_scopes = 0;
  3732. dpi->copy_templates = NULL;
  3733. dpi->next_copy_template = 0;
  3734. dpi->num_copy_templates = 0;
  3735. d_count_templates_scopes (dpi, dc);
  3736. /* If we did not reach the recursion limit, then reset the
  3737. current recursion value back to 0, so that we can print
  3738. the templates. */
  3739. if (dpi->recursion < DEMANGLE_RECURSION_LIMIT)
  3740. dpi->recursion = 0;
  3741. dpi->num_copy_templates *= dpi->num_saved_scopes;
  3742. dpi->current_template = NULL;
  3743. }
  3744. /* Indicate that an error occurred during printing, and test for error. */
  3745. static inline void
  3746. d_print_error (struct d_print_info *dpi)
  3747. {
  3748. dpi->demangle_failure = 1;
  3749. }
  3750. static inline int
  3751. d_print_saw_error (struct d_print_info *dpi)
  3752. {
  3753. return dpi->demangle_failure != 0;
  3754. }
  3755. /* Flush buffered characters to the callback. */
  3756. static inline void
  3757. d_print_flush (struct d_print_info *dpi)
  3758. {
  3759. dpi->buf[dpi->len] = '\0';
  3760. dpi->callback (dpi->buf, dpi->len, dpi->opaque);
  3761. dpi->len = 0;
  3762. dpi->flush_count++;
  3763. }
  3764. /* Append characters and buffers for printing. */
  3765. static inline void
  3766. d_append_char (struct d_print_info *dpi, char c)
  3767. {
  3768. if (dpi->len == sizeof (dpi->buf) - 1)
  3769. d_print_flush (dpi);
  3770. dpi->buf[dpi->len++] = c;
  3771. dpi->last_char = c;
  3772. }
  3773. static inline void
  3774. d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
  3775. {
  3776. size_t i;
  3777. for (i = 0; i < l; i++)
  3778. d_append_char (dpi, s[i]);
  3779. }
  3780. static inline void
  3781. d_append_string (struct d_print_info *dpi, const char *s)
  3782. {
  3783. d_append_buffer (dpi, s, strlen (s));
  3784. }
  3785. static inline void
  3786. d_append_num (struct d_print_info *dpi, int l)
  3787. {
  3788. char buf[25];
  3789. sprintf (buf,"%d", l);
  3790. d_append_string (dpi, buf);
  3791. }
  3792. static inline char
  3793. d_last_char (struct d_print_info *dpi)
  3794. {
  3795. return dpi->last_char;
  3796. }
  3797. /* Turn components into a human readable string. OPTIONS is the
  3798. options bits passed to the demangler. DC is the tree to print.
  3799. CALLBACK is a function to call to flush demangled string segments
  3800. as they fill the intermediate buffer, and OPAQUE is a generalized
  3801. callback argument. On success, this returns 1. On failure,
  3802. it returns 0, indicating a bad parse. It does not use heap
  3803. memory to build an output string, so cannot encounter memory
  3804. allocation failure. */
  3805. CP_STATIC_IF_GLIBCPP_V3
  3806. int
  3807. cplus_demangle_print_callback (int options,
  3808. struct demangle_component *dc,
  3809. demangle_callbackref callback, void *opaque)
  3810. {
  3811. struct d_print_info dpi;
  3812. d_print_init (&dpi, callback, opaque, dc);
  3813. {
  3814. #ifdef CP_DYNAMIC_ARRAYS
  3815. /* Avoid zero-length VLAs, which are prohibited by the C99 standard
  3816. and flagged as errors by Address Sanitizer. */
  3817. __extension__ struct d_saved_scope scopes[(dpi.num_saved_scopes > 0)
  3818. ? dpi.num_saved_scopes : 1];
  3819. __extension__ struct d_print_template temps[(dpi.num_copy_templates > 0)
  3820. ? dpi.num_copy_templates : 1];
  3821. dpi.saved_scopes = scopes;
  3822. dpi.copy_templates = temps;
  3823. #else
  3824. dpi.saved_scopes = alloca (dpi.num_saved_scopes
  3825. * sizeof (*dpi.saved_scopes));
  3826. dpi.copy_templates = alloca (dpi.num_copy_templates
  3827. * sizeof (*dpi.copy_templates));
  3828. #endif
  3829. d_print_comp (&dpi, options, dc);
  3830. }
  3831. d_print_flush (&dpi);
  3832. return ! d_print_saw_error (&dpi);
  3833. }
  3834. /* Turn components into a human readable string. OPTIONS is the
  3835. options bits passed to the demangler. DC is the tree to print.
  3836. ESTIMATE is a guess at the length of the result. This returns a
  3837. string allocated by malloc, or NULL on error. On success, this
  3838. sets *PALC to the size of the allocated buffer. On failure, this
  3839. sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
  3840. failure. */
  3841. CP_STATIC_IF_GLIBCPP_V3
  3842. char *
  3843. cplus_demangle_print (int options, struct demangle_component *dc,
  3844. int estimate, size_t *palc)
  3845. {
  3846. struct d_growable_string dgs;
  3847. d_growable_string_init (&dgs, estimate);
  3848. if (! cplus_demangle_print_callback (options, dc,
  3849. d_growable_string_callback_adapter,
  3850. &dgs))
  3851. {
  3852. free (dgs.buf);
  3853. *palc = 0;
  3854. return NULL;
  3855. }
  3856. *palc = dgs.allocation_failure ? 1 : dgs.alc;
  3857. return dgs.buf;
  3858. }
  3859. /* Returns the I'th element of the template arglist ARGS, or NULL on
  3860. failure. If I is negative, return the entire arglist. */
  3861. static struct demangle_component *
  3862. d_index_template_argument (struct demangle_component *args, int i)
  3863. {
  3864. struct demangle_component *a;
  3865. if (i < 0)
  3866. /* Print the whole argument pack. */
  3867. return args;
  3868. for (a = args;
  3869. a != NULL;
  3870. a = d_right (a))
  3871. {
  3872. if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
  3873. return NULL;
  3874. if (i <= 0)
  3875. break;
  3876. --i;
  3877. }
  3878. if (i != 0 || a == NULL)
  3879. return NULL;
  3880. return d_left (a);
  3881. }
  3882. /* Returns the template argument from the current context indicated by DC,
  3883. which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL. */
  3884. static struct demangle_component *
  3885. d_lookup_template_argument (struct d_print_info *dpi,
  3886. const struct demangle_component *dc)
  3887. {
  3888. if (dpi->templates == NULL)
  3889. {
  3890. d_print_error (dpi);
  3891. return NULL;
  3892. }
  3893. return d_index_template_argument
  3894. (d_right (dpi->templates->template_decl),
  3895. dc->u.s_number.number);
  3896. }
  3897. /* Returns a template argument pack used in DC (any will do), or NULL. */
  3898. static struct demangle_component *
  3899. d_find_pack (struct d_print_info *dpi,
  3900. const struct demangle_component *dc)
  3901. {
  3902. struct demangle_component *a;
  3903. if (dc == NULL)
  3904. return NULL;
  3905. switch (dc->type)
  3906. {
  3907. case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
  3908. a = d_lookup_template_argument (dpi, dc);
  3909. if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
  3910. return a;
  3911. return NULL;
  3912. case DEMANGLE_COMPONENT_PACK_EXPANSION:
  3913. return NULL;
  3914. case DEMANGLE_COMPONENT_LAMBDA:
  3915. case DEMANGLE_COMPONENT_NAME:
  3916. case DEMANGLE_COMPONENT_TAGGED_NAME:
  3917. case DEMANGLE_COMPONENT_OPERATOR:
  3918. case DEMANGLE_COMPONENT_BUILTIN_TYPE:
  3919. case DEMANGLE_COMPONENT_SUB_STD:
  3920. case DEMANGLE_COMPONENT_CHARACTER:
  3921. case DEMANGLE_COMPONENT_FUNCTION_PARAM:
  3922. case DEMANGLE_COMPONENT_UNNAMED_TYPE:
  3923. case DEMANGLE_COMPONENT_FIXED_TYPE:
  3924. case DEMANGLE_COMPONENT_DEFAULT_ARG:
  3925. case DEMANGLE_COMPONENT_NUMBER:
  3926. return NULL;
  3927. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  3928. return d_find_pack (dpi, dc->u.s_extended_operator.name);
  3929. case DEMANGLE_COMPONENT_CTOR:
  3930. return d_find_pack (dpi, dc->u.s_ctor.name);
  3931. case DEMANGLE_COMPONENT_DTOR:
  3932. return d_find_pack (dpi, dc->u.s_dtor.name);
  3933. default:
  3934. a = d_find_pack (dpi, d_left (dc));
  3935. if (a)
  3936. return a;
  3937. return d_find_pack (dpi, d_right (dc));
  3938. }
  3939. }
  3940. /* Returns the length of the template argument pack DC. */
  3941. static int
  3942. d_pack_length (const struct demangle_component *dc)
  3943. {
  3944. int count = 0;
  3945. while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
  3946. && d_left (dc) != NULL)
  3947. {
  3948. ++count;
  3949. dc = d_right (dc);
  3950. }
  3951. return count;
  3952. }
  3953. /* Returns the number of template args in DC, expanding any pack expansions
  3954. found there. */
  3955. static int
  3956. d_args_length (struct d_print_info *dpi, const struct demangle_component *dc)
  3957. {
  3958. int count = 0;
  3959. for (; dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST;
  3960. dc = d_right (dc))
  3961. {
  3962. struct demangle_component *elt = d_left (dc);
  3963. if (elt == NULL)
  3964. break;
  3965. if (elt->type == DEMANGLE_COMPONENT_PACK_EXPANSION)
  3966. {
  3967. struct demangle_component *a = d_find_pack (dpi, d_left (elt));
  3968. count += d_pack_length (a);
  3969. }
  3970. else
  3971. ++count;
  3972. }
  3973. return count;
  3974. }
  3975. /* DC is a component of a mangled expression. Print it, wrapped in parens
  3976. if needed. */
  3977. static void
  3978. d_print_subexpr (struct d_print_info *dpi, int options,
  3979. struct demangle_component *dc)
  3980. {
  3981. int simple = 0;
  3982. if (dc->type == DEMANGLE_COMPONENT_NAME
  3983. || dc->type == DEMANGLE_COMPONENT_QUAL_NAME
  3984. || dc->type == DEMANGLE_COMPONENT_INITIALIZER_LIST
  3985. || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
  3986. simple = 1;
  3987. if (!simple)
  3988. d_append_char (dpi, '(');
  3989. d_print_comp (dpi, options, dc);
  3990. if (!simple)
  3991. d_append_char (dpi, ')');
  3992. }
  3993. /* Save the current scope. */
  3994. static void
  3995. d_save_scope (struct d_print_info *dpi,
  3996. const struct demangle_component *container)
  3997. {
  3998. struct d_saved_scope *scope;
  3999. struct d_print_template *src, **link;
  4000. if (dpi->next_saved_scope >= dpi->num_saved_scopes)
  4001. {
  4002. d_print_error (dpi);
  4003. return;
  4004. }
  4005. scope = &dpi->saved_scopes[dpi->next_saved_scope];
  4006. dpi->next_saved_scope++;
  4007. scope->container = container;
  4008. link = &scope->templates;
  4009. for (src = dpi->templates; src != NULL; src = src->next)
  4010. {
  4011. struct d_print_template *dst;
  4012. if (dpi->next_copy_template >= dpi->num_copy_templates)
  4013. {
  4014. d_print_error (dpi);
  4015. return;
  4016. }
  4017. dst = &dpi->copy_templates[dpi->next_copy_template];
  4018. dpi->next_copy_template++;
  4019. dst->template_decl = src->template_decl;
  4020. *link = dst;
  4021. link = &dst->next;
  4022. }
  4023. *link = NULL;
  4024. }
  4025. /* Attempt to locate a previously saved scope. Returns NULL if no
  4026. corresponding saved scope was found. */
  4027. static struct d_saved_scope *
  4028. d_get_saved_scope (struct d_print_info *dpi,
  4029. const struct demangle_component *container)
  4030. {
  4031. int i;
  4032. for (i = 0; i < dpi->next_saved_scope; i++)
  4033. if (dpi->saved_scopes[i].container == container)
  4034. return &dpi->saved_scopes[i];
  4035. return NULL;
  4036. }
  4037. /* If DC is a C++17 fold-expression, print it and return true; otherwise
  4038. return false. */
  4039. static int
  4040. d_maybe_print_fold_expression (struct d_print_info *dpi, int options,
  4041. struct demangle_component *dc)
  4042. {
  4043. struct demangle_component *ops, *operator_, *op1, *op2;
  4044. int save_idx;
  4045. const char *fold_code = d_left (dc)->u.s_operator.op->code;
  4046. if (fold_code[0] != 'f')
  4047. return 0;
  4048. ops = d_right (dc);
  4049. operator_ = d_left (ops);
  4050. op1 = d_right (ops);
  4051. op2 = 0;
  4052. if (op1->type == DEMANGLE_COMPONENT_TRINARY_ARG2)
  4053. {
  4054. op2 = d_right (op1);
  4055. op1 = d_left (op1);
  4056. }
  4057. /* Print the whole pack. */
  4058. save_idx = dpi->pack_index;
  4059. dpi->pack_index = -1;
  4060. switch (fold_code[1])
  4061. {
  4062. /* Unary left fold, (... + X). */
  4063. case 'l':
  4064. d_append_string (dpi, "(...");
  4065. d_print_expr_op (dpi, options, operator_);
  4066. d_print_subexpr (dpi, options, op1);
  4067. d_append_char (dpi, ')');
  4068. break;
  4069. /* Unary right fold, (X + ...). */
  4070. case 'r':
  4071. d_append_char (dpi, '(');
  4072. d_print_subexpr (dpi, options, op1);
  4073. d_print_expr_op (dpi, options, operator_);
  4074. d_append_string (dpi, "...)");
  4075. break;
  4076. /* Binary left fold, (42 + ... + X). */
  4077. case 'L':
  4078. /* Binary right fold, (X + ... + 42). */
  4079. case 'R':
  4080. d_append_char (dpi, '(');
  4081. d_print_subexpr (dpi, options, op1);
  4082. d_print_expr_op (dpi, options, operator_);
  4083. d_append_string (dpi, "...");
  4084. d_print_expr_op (dpi, options, operator_);
  4085. d_print_subexpr (dpi, options, op2);
  4086. d_append_char (dpi, ')');
  4087. break;
  4088. }
  4089. dpi->pack_index = save_idx;
  4090. return 1;
  4091. }
  4092. /* True iff DC represents a C99-style designated initializer. */
  4093. static int
  4094. is_designated_init (struct demangle_component *dc)
  4095. {
  4096. if (dc->type != DEMANGLE_COMPONENT_BINARY
  4097. && dc->type != DEMANGLE_COMPONENT_TRINARY)
  4098. return 0;
  4099. struct demangle_component *op = d_left (dc);
  4100. const char *code = op->u.s_operator.op->code;
  4101. return (code[0] == 'd'
  4102. && (code[1] == 'i' || code[1] == 'x' || code[1] == 'X'));
  4103. }
  4104. /* If DC represents a C99-style designated initializer, print it and return
  4105. true; otherwise, return false. */
  4106. static int
  4107. d_maybe_print_designated_init (struct d_print_info *dpi, int options,
  4108. struct demangle_component *dc)
  4109. {
  4110. if (!is_designated_init (dc))
  4111. return 0;
  4112. const char *code = d_left (dc)->u.s_operator.op->code;
  4113. struct demangle_component *operands = d_right (dc);
  4114. struct demangle_component *op1 = d_left (operands);
  4115. struct demangle_component *op2 = d_right (operands);
  4116. if (code[1] == 'i')
  4117. d_append_char (dpi, '.');
  4118. else
  4119. d_append_char (dpi, '[');
  4120. d_print_comp (dpi, options, op1);
  4121. if (code[1] == 'X')
  4122. {
  4123. d_append_string (dpi, " ... ");
  4124. d_print_comp (dpi, options, d_left (op2));
  4125. op2 = d_right (op2);
  4126. }
  4127. if (code[1] != 'i')
  4128. d_append_char (dpi, ']');
  4129. if (is_designated_init (op2))
  4130. {
  4131. /* Don't put '=' or '(' between chained designators. */
  4132. d_print_comp (dpi, options, op2);
  4133. }
  4134. else
  4135. {
  4136. d_append_char (dpi, '=');
  4137. d_print_subexpr (dpi, options, op2);
  4138. }
  4139. return 1;
  4140. }
  4141. /* Subroutine to handle components. */
  4142. static void
  4143. d_print_comp_inner (struct d_print_info *dpi, int options,
  4144. struct demangle_component *dc)
  4145. {
  4146. /* Magic variable to let reference smashing skip over the next modifier
  4147. without needing to modify *dc. */
  4148. struct demangle_component *mod_inner = NULL;
  4149. /* Variable used to store the current templates while a previously
  4150. captured scope is used. */
  4151. struct d_print_template *saved_templates;
  4152. /* Nonzero if templates have been stored in the above variable. */
  4153. int need_template_restore = 0;
  4154. if (dc == NULL)
  4155. {
  4156. d_print_error (dpi);
  4157. return;
  4158. }
  4159. if (d_print_saw_error (dpi))
  4160. return;
  4161. switch (dc->type)
  4162. {
  4163. case DEMANGLE_COMPONENT_NAME:
  4164. if ((options & DMGL_JAVA) == 0)
  4165. d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
  4166. else
  4167. d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
  4168. return;
  4169. case DEMANGLE_COMPONENT_TAGGED_NAME:
  4170. d_print_comp (dpi, options, d_left (dc));
  4171. d_append_string (dpi, "[abi:");
  4172. d_print_comp (dpi, options, d_right (dc));
  4173. d_append_char (dpi, ']');
  4174. return;
  4175. case DEMANGLE_COMPONENT_QUAL_NAME:
  4176. case DEMANGLE_COMPONENT_LOCAL_NAME:
  4177. d_print_comp (dpi, options, d_left (dc));
  4178. if ((options & DMGL_JAVA) == 0)
  4179. d_append_string (dpi, "::");
  4180. else
  4181. d_append_char (dpi, '.');
  4182. {
  4183. struct demangle_component *local_name = d_right (dc);
  4184. if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
  4185. {
  4186. d_append_string (dpi, "{default arg#");
  4187. d_append_num (dpi, local_name->u.s_unary_num.num + 1);
  4188. d_append_string (dpi, "}::");
  4189. local_name = local_name->u.s_unary_num.sub;
  4190. }
  4191. d_print_comp (dpi, options, local_name);
  4192. }
  4193. return;
  4194. case DEMANGLE_COMPONENT_TYPED_NAME:
  4195. {
  4196. struct d_print_mod *hold_modifiers;
  4197. struct demangle_component *typed_name;
  4198. struct d_print_mod adpm[4];
  4199. unsigned int i;
  4200. struct d_print_template dpt;
  4201. /* Pass the name down to the type so that it can be printed in
  4202. the right place for the type. We also have to pass down
  4203. any CV-qualifiers, which apply to the this parameter. */
  4204. hold_modifiers = dpi->modifiers;
  4205. dpi->modifiers = 0;
  4206. i = 0;
  4207. typed_name = d_left (dc);
  4208. while (typed_name != NULL)
  4209. {
  4210. if (i >= sizeof adpm / sizeof adpm[0])
  4211. {
  4212. d_print_error (dpi);
  4213. return;
  4214. }
  4215. adpm[i].next = dpi->modifiers;
  4216. dpi->modifiers = &adpm[i];
  4217. adpm[i].mod = typed_name;
  4218. adpm[i].printed = 0;
  4219. adpm[i].templates = dpi->templates;
  4220. ++i;
  4221. if (!is_fnqual_component_type (typed_name->type))
  4222. break;
  4223. typed_name = d_left (typed_name);
  4224. }
  4225. if (typed_name == NULL)
  4226. {
  4227. d_print_error (dpi);
  4228. return;
  4229. }
  4230. /* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
  4231. there may be CV-qualifiers on its right argument which
  4232. really apply here; this happens when parsing a class that
  4233. is local to a function. */
  4234. if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
  4235. {
  4236. typed_name = d_right (typed_name);
  4237. if (typed_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
  4238. typed_name = typed_name->u.s_unary_num.sub;
  4239. while (typed_name != NULL
  4240. && is_fnqual_component_type (typed_name->type))
  4241. {
  4242. if (i >= sizeof adpm / sizeof adpm[0])
  4243. {
  4244. d_print_error (dpi);
  4245. return;
  4246. }
  4247. adpm[i] = adpm[i - 1];
  4248. adpm[i].next = &adpm[i - 1];
  4249. dpi->modifiers = &adpm[i];
  4250. adpm[i - 1].mod = typed_name;
  4251. adpm[i - 1].printed = 0;
  4252. adpm[i - 1].templates = dpi->templates;
  4253. ++i;
  4254. typed_name = d_left (typed_name);
  4255. }
  4256. if (typed_name == NULL)
  4257. {
  4258. d_print_error (dpi);
  4259. return;
  4260. }
  4261. }
  4262. /* If typed_name is a template, then it applies to the
  4263. function type as well. */
  4264. if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
  4265. {
  4266. dpt.next = dpi->templates;
  4267. dpi->templates = &dpt;
  4268. dpt.template_decl = typed_name;
  4269. }
  4270. d_print_comp (dpi, options, d_right (dc));
  4271. if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
  4272. dpi->templates = dpt.next;
  4273. /* If the modifiers didn't get printed by the type, print them
  4274. now. */
  4275. while (i > 0)
  4276. {
  4277. --i;
  4278. if (! adpm[i].printed)
  4279. {
  4280. d_append_char (dpi, ' ');
  4281. d_print_mod (dpi, options, adpm[i].mod);
  4282. }
  4283. }
  4284. dpi->modifiers = hold_modifiers;
  4285. return;
  4286. }
  4287. case DEMANGLE_COMPONENT_TEMPLATE:
  4288. {
  4289. struct d_print_mod *hold_dpm;
  4290. struct demangle_component *dcl;
  4291. const struct demangle_component *hold_current;
  4292. /* This template may need to be referenced by a cast operator
  4293. contained in its subtree. */
  4294. hold_current = dpi->current_template;
  4295. dpi->current_template = dc;
  4296. /* Don't push modifiers into a template definition. Doing so
  4297. could give the wrong definition for a template argument.
  4298. Instead, treat the template essentially as a name. */
  4299. hold_dpm = dpi->modifiers;
  4300. dpi->modifiers = NULL;
  4301. dcl = d_left (dc);
  4302. if ((options & DMGL_JAVA) != 0
  4303. && dcl->type == DEMANGLE_COMPONENT_NAME
  4304. && dcl->u.s_name.len == 6
  4305. && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
  4306. {
  4307. /* Special-case Java arrays, so that JArray<TYPE> appears
  4308. instead as TYPE[]. */
  4309. d_print_comp (dpi, options, d_right (dc));
  4310. d_append_string (dpi, "[]");
  4311. }
  4312. else
  4313. {
  4314. d_print_comp (dpi, options, dcl);
  4315. if (d_last_char (dpi) == '<')
  4316. d_append_char (dpi, ' ');
  4317. d_append_char (dpi, '<');
  4318. d_print_comp (dpi, options, d_right (dc));
  4319. /* Avoid generating two consecutive '>' characters, to avoid
  4320. the C++ syntactic ambiguity. */
  4321. if (d_last_char (dpi) == '>')
  4322. d_append_char (dpi, ' ');
  4323. d_append_char (dpi, '>');
  4324. }
  4325. dpi->modifiers = hold_dpm;
  4326. dpi->current_template = hold_current;
  4327. return;
  4328. }
  4329. case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
  4330. if (dpi->is_lambda_arg)
  4331. {
  4332. /* Show the template parm index, as that's how g++ displays
  4333. these, and future proofs us against potential
  4334. '[]<typename T> (T *a, T *b) {...}'. */
  4335. d_append_buffer (dpi, "auto:", 5);
  4336. d_append_num (dpi, dc->u.s_number.number + 1);
  4337. }
  4338. else
  4339. {
  4340. struct d_print_template *hold_dpt;
  4341. struct demangle_component *a = d_lookup_template_argument (dpi, dc);
  4342. if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
  4343. a = d_index_template_argument (a, dpi->pack_index);
  4344. if (a == NULL)
  4345. {
  4346. d_print_error (dpi);
  4347. return;
  4348. }
  4349. /* While processing this parameter, we need to pop the list
  4350. of templates. This is because the template parameter may
  4351. itself be a reference to a parameter of an outer
  4352. template. */
  4353. hold_dpt = dpi->templates;
  4354. dpi->templates = hold_dpt->next;
  4355. d_print_comp (dpi, options, a);
  4356. dpi->templates = hold_dpt;
  4357. }
  4358. return;
  4359. case DEMANGLE_COMPONENT_TPARM_OBJ:
  4360. d_append_string (dpi, "template parameter object for ");
  4361. d_print_comp (dpi, options, d_left (dc));
  4362. return;
  4363. case DEMANGLE_COMPONENT_CTOR:
  4364. d_print_comp (dpi, options, dc->u.s_ctor.name);
  4365. return;
  4366. case DEMANGLE_COMPONENT_DTOR:
  4367. d_append_char (dpi, '~');
  4368. d_print_comp (dpi, options, dc->u.s_dtor.name);
  4369. return;
  4370. case DEMANGLE_COMPONENT_VTABLE:
  4371. d_append_string (dpi, "vtable for ");
  4372. d_print_comp (dpi, options, d_left (dc));
  4373. return;
  4374. case DEMANGLE_COMPONENT_VTT:
  4375. d_append_string (dpi, "VTT for ");
  4376. d_print_comp (dpi, options, d_left (dc));
  4377. return;
  4378. case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  4379. d_append_string (dpi, "construction vtable for ");
  4380. d_print_comp (dpi, options, d_left (dc));
  4381. d_append_string (dpi, "-in-");
  4382. d_print_comp (dpi, options, d_right (dc));
  4383. return;
  4384. case DEMANGLE_COMPONENT_TYPEINFO:
  4385. d_append_string (dpi, "typeinfo for ");
  4386. d_print_comp (dpi, options, d_left (dc));
  4387. return;
  4388. case DEMANGLE_COMPONENT_TYPEINFO_NAME:
  4389. d_append_string (dpi, "typeinfo name for ");
  4390. d_print_comp (dpi, options, d_left (dc));
  4391. return;
  4392. case DEMANGLE_COMPONENT_TYPEINFO_FN:
  4393. d_append_string (dpi, "typeinfo fn for ");
  4394. d_print_comp (dpi, options, d_left (dc));
  4395. return;
  4396. case DEMANGLE_COMPONENT_THUNK:
  4397. d_append_string (dpi, "non-virtual thunk to ");
  4398. d_print_comp (dpi, options, d_left (dc));
  4399. return;
  4400. case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
  4401. d_append_string (dpi, "virtual thunk to ");
  4402. d_print_comp (dpi, options, d_left (dc));
  4403. return;
  4404. case DEMANGLE_COMPONENT_COVARIANT_THUNK:
  4405. d_append_string (dpi, "covariant return thunk to ");
  4406. d_print_comp (dpi, options, d_left (dc));
  4407. return;
  4408. case DEMANGLE_COMPONENT_JAVA_CLASS:
  4409. d_append_string (dpi, "java Class for ");
  4410. d_print_comp (dpi, options, d_left (dc));
  4411. return;
  4412. case DEMANGLE_COMPONENT_GUARD:
  4413. d_append_string (dpi, "guard variable for ");
  4414. d_print_comp (dpi, options, d_left (dc));
  4415. return;
  4416. case DEMANGLE_COMPONENT_TLS_INIT:
  4417. d_append_string (dpi, "TLS init function for ");
  4418. d_print_comp (dpi, options, d_left (dc));
  4419. return;
  4420. case DEMANGLE_COMPONENT_TLS_WRAPPER:
  4421. d_append_string (dpi, "TLS wrapper function for ");
  4422. d_print_comp (dpi, options, d_left (dc));
  4423. return;
  4424. case DEMANGLE_COMPONENT_REFTEMP:
  4425. d_append_string (dpi, "reference temporary #");
  4426. d_print_comp (dpi, options, d_right (dc));
  4427. d_append_string (dpi, " for ");
  4428. d_print_comp (dpi, options, d_left (dc));
  4429. return;
  4430. case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
  4431. d_append_string (dpi, "hidden alias for ");
  4432. d_print_comp (dpi, options, d_left (dc));
  4433. return;
  4434. case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
  4435. d_append_string (dpi, "transaction clone for ");
  4436. d_print_comp (dpi, options, d_left (dc));
  4437. return;
  4438. case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
  4439. d_append_string (dpi, "non-transaction clone for ");
  4440. d_print_comp (dpi, options, d_left (dc));
  4441. return;
  4442. case DEMANGLE_COMPONENT_SUB_STD:
  4443. d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
  4444. return;
  4445. case DEMANGLE_COMPONENT_RESTRICT:
  4446. case DEMANGLE_COMPONENT_VOLATILE:
  4447. case DEMANGLE_COMPONENT_CONST:
  4448. {
  4449. struct d_print_mod *pdpm;
  4450. /* When printing arrays, it's possible to have cases where the
  4451. same CV-qualifier gets pushed on the stack multiple times.
  4452. We only need to print it once. */
  4453. for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
  4454. {
  4455. if (! pdpm->printed)
  4456. {
  4457. if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
  4458. && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
  4459. && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
  4460. break;
  4461. if (pdpm->mod->type == dc->type)
  4462. {
  4463. d_print_comp (dpi, options, d_left (dc));
  4464. return;
  4465. }
  4466. }
  4467. }
  4468. }
  4469. goto modifier;
  4470. case DEMANGLE_COMPONENT_REFERENCE:
  4471. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  4472. {
  4473. /* Handle reference smashing: & + && = &. */
  4474. struct demangle_component *sub = d_left (dc);
  4475. if (!dpi->is_lambda_arg
  4476. && sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
  4477. {
  4478. struct d_saved_scope *scope = d_get_saved_scope (dpi, sub);
  4479. struct demangle_component *a;
  4480. if (scope == NULL)
  4481. {
  4482. /* This is the first time SUB has been traversed.
  4483. We need to capture the current templates so
  4484. they can be restored if SUB is reentered as a
  4485. substitution. */
  4486. d_save_scope (dpi, sub);
  4487. if (d_print_saw_error (dpi))
  4488. return;
  4489. }
  4490. else
  4491. {
  4492. const struct d_component_stack *dcse;
  4493. int found_self_or_parent = 0;
  4494. /* This traversal is reentering SUB as a substition.
  4495. If we are not beneath SUB or DC in the tree then we
  4496. need to restore SUB's template stack temporarily. */
  4497. for (dcse = dpi->component_stack; dcse != NULL;
  4498. dcse = dcse->parent)
  4499. {
  4500. if (dcse->dc == sub
  4501. || (dcse->dc == dc
  4502. && dcse != dpi->component_stack))
  4503. {
  4504. found_self_or_parent = 1;
  4505. break;
  4506. }
  4507. }
  4508. if (!found_self_or_parent)
  4509. {
  4510. saved_templates = dpi->templates;
  4511. dpi->templates = scope->templates;
  4512. need_template_restore = 1;
  4513. }
  4514. }
  4515. a = d_lookup_template_argument (dpi, sub);
  4516. if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
  4517. a = d_index_template_argument (a, dpi->pack_index);
  4518. if (a == NULL)
  4519. {
  4520. if (need_template_restore)
  4521. dpi->templates = saved_templates;
  4522. d_print_error (dpi);
  4523. return;
  4524. }
  4525. sub = a;
  4526. }
  4527. if (sub->type == DEMANGLE_COMPONENT_REFERENCE
  4528. || sub->type == dc->type)
  4529. dc = sub;
  4530. else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
  4531. mod_inner = d_left (sub);
  4532. }
  4533. /* Fall through. */
  4534. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  4535. case DEMANGLE_COMPONENT_POINTER:
  4536. case DEMANGLE_COMPONENT_COMPLEX:
  4537. case DEMANGLE_COMPONENT_IMAGINARY:
  4538. FNQUAL_COMPONENT_CASE:
  4539. modifier:
  4540. {
  4541. /* We keep a list of modifiers on the stack. */
  4542. struct d_print_mod dpm;
  4543. dpm.next = dpi->modifiers;
  4544. dpi->modifiers = &dpm;
  4545. dpm.mod = dc;
  4546. dpm.printed = 0;
  4547. dpm.templates = dpi->templates;
  4548. if (!mod_inner)
  4549. mod_inner = d_left (dc);
  4550. d_print_comp (dpi, options, mod_inner);
  4551. /* If the modifier didn't get printed by the type, print it
  4552. now. */
  4553. if (! dpm.printed)
  4554. d_print_mod (dpi, options, dc);
  4555. dpi->modifiers = dpm.next;
  4556. if (need_template_restore)
  4557. dpi->templates = saved_templates;
  4558. return;
  4559. }
  4560. case DEMANGLE_COMPONENT_BUILTIN_TYPE:
  4561. if ((options & DMGL_JAVA) == 0)
  4562. d_append_buffer (dpi, dc->u.s_builtin.type->name,
  4563. dc->u.s_builtin.type->len);
  4564. else
  4565. d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
  4566. dc->u.s_builtin.type->java_len);
  4567. return;
  4568. case DEMANGLE_COMPONENT_VENDOR_TYPE:
  4569. d_print_comp (dpi, options, d_left (dc));
  4570. return;
  4571. case DEMANGLE_COMPONENT_FUNCTION_TYPE:
  4572. {
  4573. if ((options & DMGL_RET_POSTFIX) != 0)
  4574. d_print_function_type (dpi,
  4575. options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
  4576. dc, dpi->modifiers);
  4577. /* Print return type if present */
  4578. if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
  4579. d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
  4580. d_left (dc));
  4581. else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
  4582. {
  4583. struct d_print_mod dpm;
  4584. /* We must pass this type down as a modifier in order to
  4585. print it in the right location. */
  4586. dpm.next = dpi->modifiers;
  4587. dpi->modifiers = &dpm;
  4588. dpm.mod = dc;
  4589. dpm.printed = 0;
  4590. dpm.templates = dpi->templates;
  4591. d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
  4592. d_left (dc));
  4593. dpi->modifiers = dpm.next;
  4594. if (dpm.printed)
  4595. return;
  4596. /* In standard prefix notation, there is a space between the
  4597. return type and the function signature. */
  4598. if ((options & DMGL_RET_POSTFIX) == 0)
  4599. d_append_char (dpi, ' ');
  4600. }
  4601. if ((options & DMGL_RET_POSTFIX) == 0)
  4602. d_print_function_type (dpi,
  4603. options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
  4604. dc, dpi->modifiers);
  4605. return;
  4606. }
  4607. case DEMANGLE_COMPONENT_ARRAY_TYPE:
  4608. {
  4609. struct d_print_mod *hold_modifiers;
  4610. struct d_print_mod adpm[4];
  4611. unsigned int i;
  4612. struct d_print_mod *pdpm;
  4613. /* We must pass this type down as a modifier in order to print
  4614. multi-dimensional arrays correctly. If the array itself is
  4615. CV-qualified, we act as though the element type were
  4616. CV-qualified. We do this by copying the modifiers down
  4617. rather than fiddling pointers, so that we don't wind up
  4618. with a d_print_mod higher on the stack pointing into our
  4619. stack frame after we return. */
  4620. hold_modifiers = dpi->modifiers;
  4621. adpm[0].next = hold_modifiers;
  4622. dpi->modifiers = &adpm[0];
  4623. adpm[0].mod = dc;
  4624. adpm[0].printed = 0;
  4625. adpm[0].templates = dpi->templates;
  4626. i = 1;
  4627. pdpm = hold_modifiers;
  4628. while (pdpm != NULL
  4629. && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
  4630. || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
  4631. || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
  4632. {
  4633. if (! pdpm->printed)
  4634. {
  4635. if (i >= sizeof adpm / sizeof adpm[0])
  4636. {
  4637. d_print_error (dpi);
  4638. return;
  4639. }
  4640. adpm[i] = *pdpm;
  4641. adpm[i].next = dpi->modifiers;
  4642. dpi->modifiers = &adpm[i];
  4643. pdpm->printed = 1;
  4644. ++i;
  4645. }
  4646. pdpm = pdpm->next;
  4647. }
  4648. d_print_comp (dpi, options, d_right (dc));
  4649. dpi->modifiers = hold_modifiers;
  4650. if (adpm[0].printed)
  4651. return;
  4652. while (i > 1)
  4653. {
  4654. --i;
  4655. d_print_mod (dpi, options, adpm[i].mod);
  4656. }
  4657. d_print_array_type (dpi, options, dc, dpi->modifiers);
  4658. return;
  4659. }
  4660. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  4661. case DEMANGLE_COMPONENT_VECTOR_TYPE:
  4662. {
  4663. struct d_print_mod dpm;
  4664. dpm.next = dpi->modifiers;
  4665. dpi->modifiers = &dpm;
  4666. dpm.mod = dc;
  4667. dpm.printed = 0;
  4668. dpm.templates = dpi->templates;
  4669. d_print_comp (dpi, options, d_right (dc));
  4670. /* If the modifier didn't get printed by the type, print it
  4671. now. */
  4672. if (! dpm.printed)
  4673. d_print_mod (dpi, options, dc);
  4674. dpi->modifiers = dpm.next;
  4675. return;
  4676. }
  4677. case DEMANGLE_COMPONENT_FIXED_TYPE:
  4678. if (dc->u.s_fixed.sat)
  4679. d_append_string (dpi, "_Sat ");
  4680. /* Don't print "int _Accum". */
  4681. if (dc->u.s_fixed.length->u.s_builtin.type
  4682. != &cplus_demangle_builtin_types['i'-'a'])
  4683. {
  4684. d_print_comp (dpi, options, dc->u.s_fixed.length);
  4685. d_append_char (dpi, ' ');
  4686. }
  4687. if (dc->u.s_fixed.accum)
  4688. d_append_string (dpi, "_Accum");
  4689. else
  4690. d_append_string (dpi, "_Fract");
  4691. return;
  4692. case DEMANGLE_COMPONENT_ARGLIST:
  4693. case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
  4694. if (d_left (dc) != NULL)
  4695. d_print_comp (dpi, options, d_left (dc));
  4696. if (d_right (dc) != NULL)
  4697. {
  4698. size_t len;
  4699. unsigned long int flush_count;
  4700. /* Make sure ", " isn't flushed by d_append_string, otherwise
  4701. dpi->len -= 2 wouldn't work. */
  4702. if (dpi->len >= sizeof (dpi->buf) - 2)
  4703. d_print_flush (dpi);
  4704. d_append_string (dpi, ", ");
  4705. len = dpi->len;
  4706. flush_count = dpi->flush_count;
  4707. d_print_comp (dpi, options, d_right (dc));
  4708. /* If that didn't print anything (which can happen with empty
  4709. template argument packs), remove the comma and space. */
  4710. if (dpi->flush_count == flush_count && dpi->len == len)
  4711. dpi->len -= 2;
  4712. }
  4713. return;
  4714. case DEMANGLE_COMPONENT_INITIALIZER_LIST:
  4715. {
  4716. struct demangle_component *type = d_left (dc);
  4717. struct demangle_component *list = d_right (dc);
  4718. if (type)
  4719. d_print_comp (dpi, options, type);
  4720. d_append_char (dpi, '{');
  4721. d_print_comp (dpi, options, list);
  4722. d_append_char (dpi, '}');
  4723. }
  4724. return;
  4725. case DEMANGLE_COMPONENT_OPERATOR:
  4726. {
  4727. const struct demangle_operator_info *op = dc->u.s_operator.op;
  4728. int len = op->len;
  4729. d_append_string (dpi, "operator");
  4730. /* Add a space before new/delete. */
  4731. if (IS_LOWER (op->name[0]))
  4732. d_append_char (dpi, ' ');
  4733. /* Omit a trailing space. */
  4734. if (op->name[len-1] == ' ')
  4735. --len;
  4736. d_append_buffer (dpi, op->name, len);
  4737. return;
  4738. }
  4739. case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  4740. d_append_string (dpi, "operator ");
  4741. d_print_comp (dpi, options, dc->u.s_extended_operator.name);
  4742. return;
  4743. case DEMANGLE_COMPONENT_CONVERSION:
  4744. d_append_string (dpi, "operator ");
  4745. d_print_conversion (dpi, options, dc);
  4746. return;
  4747. case DEMANGLE_COMPONENT_NULLARY:
  4748. d_print_expr_op (dpi, options, d_left (dc));
  4749. return;
  4750. case DEMANGLE_COMPONENT_UNARY:
  4751. {
  4752. struct demangle_component *op = d_left (dc);
  4753. struct demangle_component *operand = d_right (dc);
  4754. const char *code = NULL;
  4755. if (op->type == DEMANGLE_COMPONENT_OPERATOR)
  4756. {
  4757. code = op->u.s_operator.op->code;
  4758. if (!strcmp (code, "ad"))
  4759. {
  4760. /* Don't print the argument list for the address of a
  4761. function. */
  4762. if (operand->type == DEMANGLE_COMPONENT_TYPED_NAME
  4763. && d_left (operand)->type == DEMANGLE_COMPONENT_QUAL_NAME
  4764. && d_right (operand)->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
  4765. operand = d_left (operand);
  4766. }
  4767. if (operand->type == DEMANGLE_COMPONENT_BINARY_ARGS)
  4768. {
  4769. /* This indicates a suffix operator. */
  4770. operand = d_left (operand);
  4771. d_print_subexpr (dpi, options, operand);
  4772. d_print_expr_op (dpi, options, op);
  4773. return;
  4774. }
  4775. }
  4776. /* For sizeof..., just print the pack length. */
  4777. if (code && !strcmp (code, "sZ"))
  4778. {
  4779. struct demangle_component *a = d_find_pack (dpi, operand);
  4780. int len = d_pack_length (a);
  4781. d_append_num (dpi, len);
  4782. return;
  4783. }
  4784. else if (code && !strcmp (code, "sP"))
  4785. {
  4786. int len = d_args_length (dpi, operand);
  4787. d_append_num (dpi, len);
  4788. return;
  4789. }
  4790. if (op->type != DEMANGLE_COMPONENT_CAST)
  4791. d_print_expr_op (dpi, options, op);
  4792. else
  4793. {
  4794. d_append_char (dpi, '(');
  4795. d_print_cast (dpi, options, op);
  4796. d_append_char (dpi, ')');
  4797. }
  4798. if (code && !strcmp (code, "gs"))
  4799. /* Avoid parens after '::'. */
  4800. d_print_comp (dpi, options, operand);
  4801. else if (code && !strcmp (code, "st"))
  4802. /* Always print parens for sizeof (type). */
  4803. {
  4804. d_append_char (dpi, '(');
  4805. d_print_comp (dpi, options, operand);
  4806. d_append_char (dpi, ')');
  4807. }
  4808. else
  4809. d_print_subexpr (dpi, options, operand);
  4810. }
  4811. return;
  4812. case DEMANGLE_COMPONENT_BINARY:
  4813. if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
  4814. {
  4815. d_print_error (dpi);
  4816. return;
  4817. }
  4818. if (op_is_new_cast (d_left (dc)))
  4819. {
  4820. d_print_expr_op (dpi, options, d_left (dc));
  4821. d_append_char (dpi, '<');
  4822. d_print_comp (dpi, options, d_left (d_right (dc)));
  4823. d_append_string (dpi, ">(");
  4824. d_print_comp (dpi, options, d_right (d_right (dc)));
  4825. d_append_char (dpi, ')');
  4826. return;
  4827. }
  4828. if (d_maybe_print_fold_expression (dpi, options, dc))
  4829. return;
  4830. if (d_maybe_print_designated_init (dpi, options, dc))
  4831. return;
  4832. /* We wrap an expression which uses the greater-than operator in
  4833. an extra layer of parens so that it does not get confused
  4834. with the '>' which ends the template parameters. */
  4835. if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
  4836. && d_left (dc)->u.s_operator.op->len == 1
  4837. && d_left (dc)->u.s_operator.op->name[0] == '>')
  4838. d_append_char (dpi, '(');
  4839. if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
  4840. && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
  4841. {
  4842. /* Function call used in an expression should not have printed types
  4843. of the function arguments. Values of the function arguments still
  4844. get printed below. */
  4845. const struct demangle_component *func = d_left (d_right (dc));
  4846. if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
  4847. d_print_error (dpi);
  4848. d_print_subexpr (dpi, options, d_left (func));
  4849. }
  4850. else
  4851. d_print_subexpr (dpi, options, d_left (d_right (dc)));
  4852. if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
  4853. {
  4854. d_append_char (dpi, '[');
  4855. d_print_comp (dpi, options, d_right (d_right (dc)));
  4856. d_append_char (dpi, ']');
  4857. }
  4858. else
  4859. {
  4860. if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
  4861. d_print_expr_op (dpi, options, d_left (dc));
  4862. d_print_subexpr (dpi, options, d_right (d_right (dc)));
  4863. }
  4864. if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
  4865. && d_left (dc)->u.s_operator.op->len == 1
  4866. && d_left (dc)->u.s_operator.op->name[0] == '>')
  4867. d_append_char (dpi, ')');
  4868. return;
  4869. case DEMANGLE_COMPONENT_BINARY_ARGS:
  4870. /* We should only see this as part of DEMANGLE_COMPONENT_BINARY. */
  4871. d_print_error (dpi);
  4872. return;
  4873. case DEMANGLE_COMPONENT_TRINARY:
  4874. if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
  4875. || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
  4876. {
  4877. d_print_error (dpi);
  4878. return;
  4879. }
  4880. if (d_maybe_print_fold_expression (dpi, options, dc))
  4881. return;
  4882. if (d_maybe_print_designated_init (dpi, options, dc))
  4883. return;
  4884. {
  4885. struct demangle_component *op = d_left (dc);
  4886. struct demangle_component *first = d_left (d_right (dc));
  4887. struct demangle_component *second = d_left (d_right (d_right (dc)));
  4888. struct demangle_component *third = d_right (d_right (d_right (dc)));
  4889. if (!strcmp (op->u.s_operator.op->code, "qu"))
  4890. {
  4891. d_print_subexpr (dpi, options, first);
  4892. d_print_expr_op (dpi, options, op);
  4893. d_print_subexpr (dpi, options, second);
  4894. d_append_string (dpi, " : ");
  4895. d_print_subexpr (dpi, options, third);
  4896. }
  4897. else
  4898. {
  4899. d_append_string (dpi, "new ");
  4900. if (d_left (first) != NULL)
  4901. {
  4902. d_print_subexpr (dpi, options, first);
  4903. d_append_char (dpi, ' ');
  4904. }
  4905. d_print_comp (dpi, options, second);
  4906. if (third)
  4907. d_print_subexpr (dpi, options, third);
  4908. }
  4909. }
  4910. return;
  4911. case DEMANGLE_COMPONENT_TRINARY_ARG1:
  4912. case DEMANGLE_COMPONENT_TRINARY_ARG2:
  4913. /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY. */
  4914. d_print_error (dpi);
  4915. return;
  4916. case DEMANGLE_COMPONENT_LITERAL:
  4917. case DEMANGLE_COMPONENT_LITERAL_NEG:
  4918. {
  4919. enum d_builtin_type_print tp;
  4920. /* For some builtin types, produce simpler output. */
  4921. tp = D_PRINT_DEFAULT;
  4922. if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
  4923. {
  4924. tp = d_left (dc)->u.s_builtin.type->print;
  4925. switch (tp)
  4926. {
  4927. case D_PRINT_INT:
  4928. case D_PRINT_UNSIGNED:
  4929. case D_PRINT_LONG:
  4930. case D_PRINT_UNSIGNED_LONG:
  4931. case D_PRINT_LONG_LONG:
  4932. case D_PRINT_UNSIGNED_LONG_LONG:
  4933. if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
  4934. {
  4935. if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
  4936. d_append_char (dpi, '-');
  4937. d_print_comp (dpi, options, d_right (dc));
  4938. switch (tp)
  4939. {
  4940. default:
  4941. break;
  4942. case D_PRINT_UNSIGNED:
  4943. d_append_char (dpi, 'u');
  4944. break;
  4945. case D_PRINT_LONG:
  4946. d_append_char (dpi, 'l');
  4947. break;
  4948. case D_PRINT_UNSIGNED_LONG:
  4949. d_append_string (dpi, "ul");
  4950. break;
  4951. case D_PRINT_LONG_LONG:
  4952. d_append_string (dpi, "ll");
  4953. break;
  4954. case D_PRINT_UNSIGNED_LONG_LONG:
  4955. d_append_string (dpi, "ull");
  4956. break;
  4957. }
  4958. return;
  4959. }
  4960. break;
  4961. case D_PRINT_BOOL:
  4962. if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
  4963. && d_right (dc)->u.s_name.len == 1
  4964. && dc->type == DEMANGLE_COMPONENT_LITERAL)
  4965. {
  4966. switch (d_right (dc)->u.s_name.s[0])
  4967. {
  4968. case '0':
  4969. d_append_string (dpi, "false");
  4970. return;
  4971. case '1':
  4972. d_append_string (dpi, "true");
  4973. return;
  4974. default:
  4975. break;
  4976. }
  4977. }
  4978. break;
  4979. default:
  4980. break;
  4981. }
  4982. }
  4983. d_append_char (dpi, '(');
  4984. d_print_comp (dpi, options, d_left (dc));
  4985. d_append_char (dpi, ')');
  4986. if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
  4987. d_append_char (dpi, '-');
  4988. if (tp == D_PRINT_FLOAT)
  4989. d_append_char (dpi, '[');
  4990. d_print_comp (dpi, options, d_right (dc));
  4991. if (tp == D_PRINT_FLOAT)
  4992. d_append_char (dpi, ']');
  4993. }
  4994. return;
  4995. case DEMANGLE_COMPONENT_VENDOR_EXPR:
  4996. d_print_comp (dpi, options, d_left (dc));
  4997. d_append_char (dpi, '(');
  4998. d_print_comp (dpi, options, d_right (dc));
  4999. d_append_char (dpi, ')');
  5000. return;
  5001. case DEMANGLE_COMPONENT_NUMBER:
  5002. d_append_num (dpi, dc->u.s_number.number);
  5003. return;
  5004. case DEMANGLE_COMPONENT_JAVA_RESOURCE:
  5005. d_append_string (dpi, "java resource ");
  5006. d_print_comp (dpi, options, d_left (dc));
  5007. return;
  5008. case DEMANGLE_COMPONENT_COMPOUND_NAME:
  5009. d_print_comp (dpi, options, d_left (dc));
  5010. d_print_comp (dpi, options, d_right (dc));
  5011. return;
  5012. case DEMANGLE_COMPONENT_CHARACTER:
  5013. d_append_char (dpi, dc->u.s_character.character);
  5014. return;
  5015. case DEMANGLE_COMPONENT_DECLTYPE:
  5016. d_append_string (dpi, "decltype (");
  5017. d_print_comp (dpi, options, d_left (dc));
  5018. d_append_char (dpi, ')');
  5019. return;
  5020. case DEMANGLE_COMPONENT_PACK_EXPANSION:
  5021. {
  5022. int len;
  5023. int i;
  5024. struct demangle_component *a = d_find_pack (dpi, d_left (dc));
  5025. if (a == NULL)
  5026. {
  5027. /* d_find_pack won't find anything if the only packs involved
  5028. in this expansion are function parameter packs; in that
  5029. case, just print the pattern and "...". */
  5030. d_print_subexpr (dpi, options, d_left (dc));
  5031. d_append_string (dpi, "...");
  5032. return;
  5033. }
  5034. len = d_pack_length (a);
  5035. dc = d_left (dc);
  5036. for (i = 0; i < len; ++i)
  5037. {
  5038. dpi->pack_index = i;
  5039. d_print_comp (dpi, options, dc);
  5040. if (i < len-1)
  5041. d_append_string (dpi, ", ");
  5042. }
  5043. }
  5044. return;
  5045. case DEMANGLE_COMPONENT_FUNCTION_PARAM:
  5046. {
  5047. long num = dc->u.s_number.number;
  5048. if (num == 0)
  5049. d_append_string (dpi, "this");
  5050. else
  5051. {
  5052. d_append_string (dpi, "{parm#");
  5053. d_append_num (dpi, num);
  5054. d_append_char (dpi, '}');
  5055. }
  5056. }
  5057. return;
  5058. case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
  5059. d_append_string (dpi, "global constructors keyed to ");
  5060. d_print_comp (dpi, options, dc->u.s_binary.left);
  5061. return;
  5062. case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
  5063. d_append_string (dpi, "global destructors keyed to ");
  5064. d_print_comp (dpi, options, dc->u.s_binary.left);
  5065. return;
  5066. case DEMANGLE_COMPONENT_LAMBDA:
  5067. d_append_string (dpi, "{lambda(");
  5068. /* Generic lambda auto parms are mangled as the template type
  5069. parm they are. */
  5070. dpi->is_lambda_arg++;
  5071. d_print_comp (dpi, options, dc->u.s_unary_num.sub);
  5072. dpi->is_lambda_arg--;
  5073. d_append_string (dpi, ")#");
  5074. d_append_num (dpi, dc->u.s_unary_num.num + 1);
  5075. d_append_char (dpi, '}');
  5076. return;
  5077. case DEMANGLE_COMPONENT_UNNAMED_TYPE:
  5078. d_append_string (dpi, "{unnamed type#");
  5079. d_append_num (dpi, dc->u.s_number.number + 1);
  5080. d_append_char (dpi, '}');
  5081. return;
  5082. case DEMANGLE_COMPONENT_CLONE:
  5083. d_print_comp (dpi, options, d_left (dc));
  5084. d_append_string (dpi, " [clone ");
  5085. d_print_comp (dpi, options, d_right (dc));
  5086. d_append_char (dpi, ']');
  5087. return;
  5088. default:
  5089. d_print_error (dpi);
  5090. return;
  5091. }
  5092. }
  5093. static void
  5094. d_print_comp (struct d_print_info *dpi, int options,
  5095. struct demangle_component *dc)
  5096. {
  5097. struct d_component_stack self;
  5098. if (dc == NULL || dc->d_printing > 1 || dpi->recursion > MAX_RECURSION_COUNT)
  5099. {
  5100. d_print_error (dpi);
  5101. return;
  5102. }
  5103. dc->d_printing++;
  5104. dpi->recursion++;
  5105. self.dc = dc;
  5106. self.parent = dpi->component_stack;
  5107. dpi->component_stack = &self;
  5108. d_print_comp_inner (dpi, options, dc);
  5109. dpi->component_stack = self.parent;
  5110. dc->d_printing--;
  5111. dpi->recursion--;
  5112. }
  5113. /* Print a Java dentifier. For Java we try to handle encoded extended
  5114. Unicode characters. The C++ ABI doesn't mention Unicode encoding,
  5115. so we don't it for C++. Characters are encoded as
  5116. __U<hex-char>+_. */
  5117. static void
  5118. d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
  5119. {
  5120. const char *p;
  5121. const char *end;
  5122. end = name + len;
  5123. for (p = name; p < end; ++p)
  5124. {
  5125. if (end - p > 3
  5126. && p[0] == '_'
  5127. && p[1] == '_'
  5128. && p[2] == 'U')
  5129. {
  5130. unsigned long c;
  5131. const char *q;
  5132. c = 0;
  5133. for (q = p + 3; q < end; ++q)
  5134. {
  5135. int dig;
  5136. if (IS_DIGIT (*q))
  5137. dig = *q - '0';
  5138. else if (*q >= 'A' && *q <= 'F')
  5139. dig = *q - 'A' + 10;
  5140. else if (*q >= 'a' && *q <= 'f')
  5141. dig = *q - 'a' + 10;
  5142. else
  5143. break;
  5144. c = c * 16 + dig;
  5145. }
  5146. /* If the Unicode character is larger than 256, we don't try
  5147. to deal with it here. FIXME. */
  5148. if (q < end && *q == '_' && c < 256)
  5149. {
  5150. d_append_char (dpi, c);
  5151. p = q;
  5152. continue;
  5153. }
  5154. }
  5155. d_append_char (dpi, *p);
  5156. }
  5157. }
  5158. /* Print a list of modifiers. SUFFIX is 1 if we are printing
  5159. qualifiers on this after printing a function. */
  5160. static void
  5161. d_print_mod_list (struct d_print_info *dpi, int options,
  5162. struct d_print_mod *mods, int suffix)
  5163. {
  5164. struct d_print_template *hold_dpt;
  5165. if (mods == NULL || d_print_saw_error (dpi))
  5166. return;
  5167. if (mods->printed
  5168. || (! suffix
  5169. && (is_fnqual_component_type (mods->mod->type))))
  5170. {
  5171. d_print_mod_list (dpi, options, mods->next, suffix);
  5172. return;
  5173. }
  5174. mods->printed = 1;
  5175. hold_dpt = dpi->templates;
  5176. dpi->templates = mods->templates;
  5177. if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
  5178. {
  5179. d_print_function_type (dpi, options, mods->mod, mods->next);
  5180. dpi->templates = hold_dpt;
  5181. return;
  5182. }
  5183. else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
  5184. {
  5185. d_print_array_type (dpi, options, mods->mod, mods->next);
  5186. dpi->templates = hold_dpt;
  5187. return;
  5188. }
  5189. else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
  5190. {
  5191. struct d_print_mod *hold_modifiers;
  5192. struct demangle_component *dc;
  5193. /* When this is on the modifier stack, we have pulled any
  5194. qualifiers off the right argument already. Otherwise, we
  5195. print it as usual, but don't let the left argument see any
  5196. modifiers. */
  5197. hold_modifiers = dpi->modifiers;
  5198. dpi->modifiers = NULL;
  5199. d_print_comp (dpi, options, d_left (mods->mod));
  5200. dpi->modifiers = hold_modifiers;
  5201. if ((options & DMGL_JAVA) == 0)
  5202. d_append_string (dpi, "::");
  5203. else
  5204. d_append_char (dpi, '.');
  5205. dc = d_right (mods->mod);
  5206. if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
  5207. {
  5208. d_append_string (dpi, "{default arg#");
  5209. d_append_num (dpi, dc->u.s_unary_num.num + 1);
  5210. d_append_string (dpi, "}::");
  5211. dc = dc->u.s_unary_num.sub;
  5212. }
  5213. while (is_fnqual_component_type (dc->type))
  5214. dc = d_left (dc);
  5215. d_print_comp (dpi, options, dc);
  5216. dpi->templates = hold_dpt;
  5217. return;
  5218. }
  5219. d_print_mod (dpi, options, mods->mod);
  5220. dpi->templates = hold_dpt;
  5221. d_print_mod_list (dpi, options, mods->next, suffix);
  5222. }
  5223. /* Print a modifier. */
  5224. static void
  5225. d_print_mod (struct d_print_info *dpi, int options,
  5226. struct demangle_component *mod)
  5227. {
  5228. switch (mod->type)
  5229. {
  5230. case DEMANGLE_COMPONENT_RESTRICT:
  5231. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  5232. d_append_string (dpi, " restrict");
  5233. return;
  5234. case DEMANGLE_COMPONENT_VOLATILE:
  5235. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  5236. d_append_string (dpi, " volatile");
  5237. return;
  5238. case DEMANGLE_COMPONENT_CONST:
  5239. case DEMANGLE_COMPONENT_CONST_THIS:
  5240. d_append_string (dpi, " const");
  5241. return;
  5242. case DEMANGLE_COMPONENT_TRANSACTION_SAFE:
  5243. d_append_string (dpi, " transaction_safe");
  5244. return;
  5245. case DEMANGLE_COMPONENT_NOEXCEPT:
  5246. d_append_string (dpi, " noexcept");
  5247. if (d_right (mod))
  5248. {
  5249. d_append_char (dpi, '(');
  5250. d_print_comp (dpi, options, d_right (mod));
  5251. d_append_char (dpi, ')');
  5252. }
  5253. return;
  5254. case DEMANGLE_COMPONENT_THROW_SPEC:
  5255. d_append_string (dpi, " throw");
  5256. if (d_right (mod))
  5257. {
  5258. d_append_char (dpi, '(');
  5259. d_print_comp (dpi, options, d_right (mod));
  5260. d_append_char (dpi, ')');
  5261. }
  5262. return;
  5263. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  5264. d_append_char (dpi, ' ');
  5265. d_print_comp (dpi, options, d_right (mod));
  5266. return;
  5267. case DEMANGLE_COMPONENT_POINTER:
  5268. /* There is no pointer symbol in Java. */
  5269. if ((options & DMGL_JAVA) == 0)
  5270. d_append_char (dpi, '*');
  5271. return;
  5272. case DEMANGLE_COMPONENT_REFERENCE_THIS:
  5273. /* For the ref-qualifier, put a space before the &. */
  5274. d_append_char (dpi, ' ');
  5275. /* FALLTHRU */
  5276. case DEMANGLE_COMPONENT_REFERENCE:
  5277. d_append_char (dpi, '&');
  5278. return;
  5279. case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
  5280. d_append_char (dpi, ' ');
  5281. /* FALLTHRU */
  5282. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  5283. d_append_string (dpi, "&&");
  5284. return;
  5285. case DEMANGLE_COMPONENT_COMPLEX:
  5286. d_append_string (dpi, " _Complex");
  5287. return;
  5288. case DEMANGLE_COMPONENT_IMAGINARY:
  5289. d_append_string (dpi, " _Imaginary");
  5290. return;
  5291. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  5292. if (d_last_char (dpi) != '(')
  5293. d_append_char (dpi, ' ');
  5294. d_print_comp (dpi, options, d_left (mod));
  5295. d_append_string (dpi, "::*");
  5296. return;
  5297. case DEMANGLE_COMPONENT_TYPED_NAME:
  5298. d_print_comp (dpi, options, d_left (mod));
  5299. return;
  5300. case DEMANGLE_COMPONENT_VECTOR_TYPE:
  5301. d_append_string (dpi, " __vector(");
  5302. d_print_comp (dpi, options, d_left (mod));
  5303. d_append_char (dpi, ')');
  5304. return;
  5305. default:
  5306. /* Otherwise, we have something that won't go back on the
  5307. modifier stack, so we can just print it. */
  5308. d_print_comp (dpi, options, mod);
  5309. return;
  5310. }
  5311. }
  5312. /* Print a function type, except for the return type. */
  5313. static void
  5314. d_print_function_type (struct d_print_info *dpi, int options,
  5315. struct demangle_component *dc,
  5316. struct d_print_mod *mods)
  5317. {
  5318. int need_paren;
  5319. int need_space;
  5320. struct d_print_mod *p;
  5321. struct d_print_mod *hold_modifiers;
  5322. need_paren = 0;
  5323. need_space = 0;
  5324. for (p = mods; p != NULL; p = p->next)
  5325. {
  5326. if (p->printed)
  5327. break;
  5328. switch (p->mod->type)
  5329. {
  5330. case DEMANGLE_COMPONENT_POINTER:
  5331. case DEMANGLE_COMPONENT_REFERENCE:
  5332. case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
  5333. need_paren = 1;
  5334. break;
  5335. case DEMANGLE_COMPONENT_RESTRICT:
  5336. case DEMANGLE_COMPONENT_VOLATILE:
  5337. case DEMANGLE_COMPONENT_CONST:
  5338. case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
  5339. case DEMANGLE_COMPONENT_COMPLEX:
  5340. case DEMANGLE_COMPONENT_IMAGINARY:
  5341. case DEMANGLE_COMPONENT_PTRMEM_TYPE:
  5342. need_space = 1;
  5343. need_paren = 1;
  5344. break;
  5345. FNQUAL_COMPONENT_CASE:
  5346. break;
  5347. default:
  5348. break;
  5349. }
  5350. if (need_paren)
  5351. break;
  5352. }
  5353. if (need_paren)
  5354. {
  5355. if (! need_space)
  5356. {
  5357. if (d_last_char (dpi) != '('
  5358. && d_last_char (dpi) != '*')
  5359. need_space = 1;
  5360. }
  5361. if (need_space && d_last_char (dpi) != ' ')
  5362. d_append_char (dpi, ' ');
  5363. d_append_char (dpi, '(');
  5364. }
  5365. hold_modifiers = dpi->modifiers;
  5366. dpi->modifiers = NULL;
  5367. d_print_mod_list (dpi, options, mods, 0);
  5368. if (need_paren)
  5369. d_append_char (dpi, ')');
  5370. d_append_char (dpi, '(');
  5371. if (d_right (dc) != NULL)
  5372. d_print_comp (dpi, options, d_right (dc));
  5373. d_append_char (dpi, ')');
  5374. d_print_mod_list (dpi, options, mods, 1);
  5375. dpi->modifiers = hold_modifiers;
  5376. }
  5377. /* Print an array type, except for the element type. */
  5378. static void
  5379. d_print_array_type (struct d_print_info *dpi, int options,
  5380. struct demangle_component *dc,
  5381. struct d_print_mod *mods)
  5382. {
  5383. int need_space;
  5384. need_space = 1;
  5385. if (mods != NULL)
  5386. {
  5387. int need_paren;
  5388. struct d_print_mod *p;
  5389. need_paren = 0;
  5390. for (p = mods; p != NULL; p = p->next)
  5391. {
  5392. if (! p->printed)
  5393. {
  5394. if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
  5395. {
  5396. need_space = 0;
  5397. break;
  5398. }
  5399. else
  5400. {
  5401. need_paren = 1;
  5402. need_space = 1;
  5403. break;
  5404. }
  5405. }
  5406. }
  5407. if (need_paren)
  5408. d_append_string (dpi, " (");
  5409. d_print_mod_list (dpi, options, mods, 0);
  5410. if (need_paren)
  5411. d_append_char (dpi, ')');
  5412. }
  5413. if (need_space)
  5414. d_append_char (dpi, ' ');
  5415. d_append_char (dpi, '[');
  5416. if (d_left (dc) != NULL)
  5417. d_print_comp (dpi, options, d_left (dc));
  5418. d_append_char (dpi, ']');
  5419. }
  5420. /* Print an operator in an expression. */
  5421. static void
  5422. d_print_expr_op (struct d_print_info *dpi, int options,
  5423. struct demangle_component *dc)
  5424. {
  5425. if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
  5426. d_append_buffer (dpi, dc->u.s_operator.op->name,
  5427. dc->u.s_operator.op->len);
  5428. else
  5429. d_print_comp (dpi, options, dc);
  5430. }
  5431. /* Print a cast. */
  5432. static void
  5433. d_print_cast (struct d_print_info *dpi, int options,
  5434. struct demangle_component *dc)
  5435. {
  5436. d_print_comp (dpi, options, d_left (dc));
  5437. }
  5438. /* Print a conversion operator. */
  5439. static void
  5440. d_print_conversion (struct d_print_info *dpi, int options,
  5441. struct demangle_component *dc)
  5442. {
  5443. struct d_print_template dpt;
  5444. /* For a conversion operator, we need the template parameters from
  5445. the enclosing template in scope for processing the type. */
  5446. if (dpi->current_template != NULL)
  5447. {
  5448. dpt.next = dpi->templates;
  5449. dpi->templates = &dpt;
  5450. dpt.template_decl = dpi->current_template;
  5451. }
  5452. if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
  5453. {
  5454. d_print_comp (dpi, options, d_left (dc));
  5455. if (dpi->current_template != NULL)
  5456. dpi->templates = dpt.next;
  5457. }
  5458. else
  5459. {
  5460. d_print_comp (dpi, options, d_left (d_left (dc)));
  5461. /* For a templated cast operator, we need to remove the template
  5462. parameters from scope after printing the operator name,
  5463. so we need to handle the template printing here. */
  5464. if (dpi->current_template != NULL)
  5465. dpi->templates = dpt.next;
  5466. if (d_last_char (dpi) == '<')
  5467. d_append_char (dpi, ' ');
  5468. d_append_char (dpi, '<');
  5469. d_print_comp (dpi, options, d_right (d_left (dc)));
  5470. /* Avoid generating two consecutive '>' characters, to avoid
  5471. the C++ syntactic ambiguity. */
  5472. if (d_last_char (dpi) == '>')
  5473. d_append_char (dpi, ' ');
  5474. d_append_char (dpi, '>');
  5475. }
  5476. }
  5477. /* Initialize the information structure we use to pass around
  5478. information. */
  5479. CP_STATIC_IF_GLIBCPP_V3
  5480. void
  5481. cplus_demangle_init_info (const char *mangled, int options, size_t len,
  5482. struct d_info *di)
  5483. {
  5484. di->s = mangled;
  5485. di->send = mangled + len;
  5486. di->options = options;
  5487. di->n = mangled;
  5488. /* We cannot need more components than twice the number of chars in
  5489. the mangled string. Most components correspond directly to
  5490. chars, but the ARGLIST types are exceptions. */
  5491. di->num_comps = 2 * len;
  5492. di->next_comp = 0;
  5493. /* Similarly, we cannot need more substitutions than there are
  5494. chars in the mangled string. */
  5495. di->num_subs = len;
  5496. di->next_sub = 0;
  5497. di->last_name = NULL;
  5498. di->expansion = 0;
  5499. di->is_expression = 0;
  5500. di->is_conversion = 0;
  5501. di->recursion_level = 0;
  5502. }
  5503. /* Internal implementation for the demangler. If MANGLED is a g++ v3 ABI
  5504. mangled name, return strings in repeated callback giving the demangled
  5505. name. OPTIONS is the usual libiberty demangler options. On success,
  5506. this returns 1. On failure, returns 0. */
  5507. static int
  5508. d_demangle_callback (const char *mangled, int options,
  5509. demangle_callbackref callback, void *opaque)
  5510. {
  5511. enum
  5512. {
  5513. DCT_TYPE,
  5514. DCT_MANGLED,
  5515. DCT_GLOBAL_CTORS,
  5516. DCT_GLOBAL_DTORS
  5517. }
  5518. type;
  5519. struct d_info di;
  5520. struct demangle_component *dc;
  5521. int status;
  5522. if (mangled[0] == '_' && mangled[1] == 'Z')
  5523. type = DCT_MANGLED;
  5524. else if (strncmp (mangled, "_GLOBAL_", 8) == 0
  5525. && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
  5526. && (mangled[9] == 'D' || mangled[9] == 'I')
  5527. && mangled[10] == '_')
  5528. type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
  5529. else
  5530. {
  5531. if ((options & DMGL_TYPES) == 0)
  5532. return 0;
  5533. type = DCT_TYPE;
  5534. }
  5535. di.unresolved_name_state = 1;
  5536. again:
  5537. cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
  5538. /* PR 87675 - Check for a mangled string that is so long
  5539. that we do not have enough stack space to demangle it. */
  5540. if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
  5541. /* This check is a bit arbitrary, since what we really want to do is to
  5542. compare the sizes of the di.comps and di.subs arrays against the
  5543. amount of stack space remaining. But there is no portable way to do
  5544. this, so instead we use the recursion limit as a guide to the maximum
  5545. size of the arrays. */
  5546. && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
  5547. {
  5548. /* FIXME: We need a way to indicate that a stack limit has been reached. */
  5549. return 0;
  5550. }
  5551. {
  5552. #ifdef CP_DYNAMIC_ARRAYS
  5553. __extension__ struct demangle_component comps[di.num_comps];
  5554. __extension__ struct demangle_component *subs[di.num_subs];
  5555. di.comps = comps;
  5556. di.subs = subs;
  5557. #else
  5558. di.comps = alloca (di.num_comps * sizeof (*di.comps));
  5559. di.subs = alloca (di.num_subs * sizeof (*di.subs));
  5560. #endif
  5561. switch (type)
  5562. {
  5563. case DCT_TYPE:
  5564. dc = cplus_demangle_type (&di);
  5565. break;
  5566. case DCT_MANGLED:
  5567. dc = cplus_demangle_mangled_name (&di, 1);
  5568. break;
  5569. case DCT_GLOBAL_CTORS:
  5570. case DCT_GLOBAL_DTORS:
  5571. d_advance (&di, 11);
  5572. dc = d_make_comp (&di,
  5573. (type == DCT_GLOBAL_CTORS
  5574. ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
  5575. : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
  5576. d_make_demangle_mangled_name (&di, d_str (&di)),
  5577. NULL);
  5578. d_advance (&di, strlen (d_str (&di)));
  5579. break;
  5580. default:
  5581. abort (); /* We have listed all the cases. */
  5582. }
  5583. /* If DMGL_PARAMS is set, then if we didn't consume the entire
  5584. mangled string, then we didn't successfully demangle it. If
  5585. DMGL_PARAMS is not set, we didn't look at the trailing
  5586. parameters. */
  5587. if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
  5588. dc = NULL;
  5589. /* See discussion in d_unresolved_name. */
  5590. if (dc == NULL && di.unresolved_name_state == -1)
  5591. {
  5592. di.unresolved_name_state = 0;
  5593. goto again;
  5594. }
  5595. #ifdef CP_DEMANGLE_DEBUG
  5596. d_dump (dc, 0);
  5597. #endif
  5598. status = (dc != NULL)
  5599. ? cplus_demangle_print_callback (options, dc, callback, opaque)
  5600. : 0;
  5601. }
  5602. return status;
  5603. }
  5604. /* Entry point for the demangler. If MANGLED is a g++ v3 ABI mangled
  5605. name, return a buffer allocated with malloc holding the demangled
  5606. name. OPTIONS is the usual libiberty demangler options. On
  5607. success, this sets *PALC to the allocated size of the returned
  5608. buffer. On failure, this sets *PALC to 0 for a bad name, or 1 for
  5609. a memory allocation failure, and returns NULL. */
  5610. static char *
  5611. d_demangle (const char *mangled, int options, size_t *palc)
  5612. {
  5613. struct d_growable_string dgs;
  5614. int status;
  5615. d_growable_string_init (&dgs, 0);
  5616. status = d_demangle_callback (mangled, options,
  5617. d_growable_string_callback_adapter, &dgs);
  5618. if (status == 0)
  5619. {
  5620. free (dgs.buf);
  5621. *palc = 0;
  5622. return NULL;
  5623. }
  5624. *palc = dgs.allocation_failure ? 1 : dgs.alc;
  5625. return dgs.buf;
  5626. }
  5627. #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
  5628. extern char *__cxa_demangle (const char *, char *, size_t *, int *);
  5629. /* ia64 ABI-mandated entry point in the C++ runtime library for
  5630. performing demangling. MANGLED_NAME is a NUL-terminated character
  5631. string containing the name to be demangled.
  5632. OUTPUT_BUFFER is a region of memory, allocated with malloc, of
  5633. *LENGTH bytes, into which the demangled name is stored. If
  5634. OUTPUT_BUFFER is not long enough, it is expanded using realloc.
  5635. OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
  5636. is placed in a region of memory allocated with malloc.
  5637. If LENGTH is non-NULL, the length of the buffer containing the
  5638. demangled name, is placed in *LENGTH.
  5639. The return value is a pointer to the start of the NUL-terminated
  5640. demangled name, or NULL if the demangling fails. The caller is
  5641. responsible for deallocating this memory using free.
  5642. *STATUS is set to one of the following values:
  5643. 0: The demangling operation succeeded.
  5644. -1: A memory allocation failure occurred.
  5645. -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
  5646. -3: One of the arguments is invalid.
  5647. The demangling is performed using the C++ ABI mangling rules, with
  5648. GNU extensions. */
  5649. char *
  5650. __cxa_demangle (const char *mangled_name, char *output_buffer,
  5651. size_t *length, int *status)
  5652. {
  5653. char *demangled;
  5654. size_t alc;
  5655. if (mangled_name == NULL)
  5656. {
  5657. if (status != NULL)
  5658. *status = -3;
  5659. return NULL;
  5660. }
  5661. if (output_buffer != NULL && length == NULL)
  5662. {
  5663. if (status != NULL)
  5664. *status = -3;
  5665. return NULL;
  5666. }
  5667. demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
  5668. if (demangled == NULL)
  5669. {
  5670. if (status != NULL)
  5671. {
  5672. if (alc == 1)
  5673. *status = -1;
  5674. else
  5675. *status = -2;
  5676. }
  5677. return NULL;
  5678. }
  5679. if (output_buffer == NULL)
  5680. {
  5681. if (length != NULL)
  5682. *length = alc;
  5683. }
  5684. else
  5685. {
  5686. if (strlen (demangled) < *length)
  5687. {
  5688. strcpy (output_buffer, demangled);
  5689. free (demangled);
  5690. demangled = output_buffer;
  5691. }
  5692. else
  5693. {
  5694. free (output_buffer);
  5695. *length = alc;
  5696. }
  5697. }
  5698. if (status != NULL)
  5699. *status = 0;
  5700. return demangled;
  5701. }
  5702. extern int __gcclibcxx_demangle_callback (const char *,
  5703. void (*)
  5704. (const char *, size_t, void *),
  5705. void *);
  5706. /* Alternative, allocationless entry point in the C++ runtime library
  5707. for performing demangling. MANGLED_NAME is a NUL-terminated character
  5708. string containing the name to be demangled.
  5709. CALLBACK is a callback function, called with demangled string
  5710. segments as demangling progresses; it is called at least once,
  5711. but may be called more than once. OPAQUE is a generalized pointer
  5712. used as a callback argument.
  5713. The return code is one of the following values, equivalent to
  5714. the STATUS values of __cxa_demangle() (excluding -1, since this
  5715. function performs no memory allocations):
  5716. 0: The demangling operation succeeded.
  5717. -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
  5718. -3: One of the arguments is invalid.
  5719. The demangling is performed using the C++ ABI mangling rules, with
  5720. GNU extensions. */
  5721. int
  5722. __gcclibcxx_demangle_callback (const char *mangled_name,
  5723. void (*callback) (const char *, size_t, void *),
  5724. void *opaque)
  5725. {
  5726. int status;
  5727. if (mangled_name == NULL || callback == NULL)
  5728. return -3;
  5729. status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
  5730. callback, opaque);
  5731. if (status == 0)
  5732. return -2;
  5733. return 0;
  5734. }
  5735. #else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
  5736. /* Entry point for libiberty demangler. If MANGLED is a g++ v3 ABI
  5737. mangled name, return a buffer allocated with malloc holding the
  5738. demangled name. Otherwise, return NULL. */
  5739. char *
  5740. cplus_demangle_v3 (const char *mangled, int options)
  5741. {
  5742. size_t alc;
  5743. return d_demangle (mangled, options, &alc);
  5744. }
  5745. int
  5746. cplus_demangle_v3_callback (const char *mangled, int options,
  5747. demangle_callbackref callback, void *opaque)
  5748. {
  5749. return d_demangle_callback (mangled, options, callback, opaque);
  5750. }
  5751. /* Demangle a Java symbol. Java uses a subset of the V3 ABI C++ mangling
  5752. conventions, but the output formatting is a little different.
  5753. This instructs the C++ demangler not to emit pointer characters ("*"), to
  5754. use Java's namespace separator symbol ("." instead of "::"), and to output
  5755. JArray<TYPE> as TYPE[]. */
  5756. char *
  5757. java_demangle_v3 (const char *mangled)
  5758. {
  5759. size_t alc;
  5760. return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
  5761. }
  5762. int
  5763. java_demangle_v3_callback (const char *mangled,
  5764. demangle_callbackref callback, void *opaque)
  5765. {
  5766. return d_demangle_callback (mangled,
  5767. DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
  5768. callback, opaque);
  5769. }
  5770. #endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
  5771. #ifndef IN_GLIBCPP_V3
  5772. /* Demangle a string in order to find out whether it is a constructor
  5773. or destructor. Return non-zero on success. Set *CTOR_KIND and
  5774. *DTOR_KIND appropriately. */
  5775. static int
  5776. is_ctor_or_dtor (const char *mangled,
  5777. enum gnu_v3_ctor_kinds *ctor_kind,
  5778. enum gnu_v3_dtor_kinds *dtor_kind)
  5779. {
  5780. struct d_info di;
  5781. struct demangle_component *dc;
  5782. int ret;
  5783. *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
  5784. *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
  5785. cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
  5786. {
  5787. #ifdef CP_DYNAMIC_ARRAYS
  5788. __extension__ struct demangle_component comps[di.num_comps];
  5789. __extension__ struct demangle_component *subs[di.num_subs];
  5790. di.comps = comps;
  5791. di.subs = subs;
  5792. #else
  5793. di.comps = alloca (di.num_comps * sizeof (*di.comps));
  5794. di.subs = alloca (di.num_subs * sizeof (*di.subs));
  5795. #endif
  5796. dc = cplus_demangle_mangled_name (&di, 1);
  5797. /* Note that because we did not pass DMGL_PARAMS, we don't expect
  5798. to demangle the entire string. */
  5799. ret = 0;
  5800. while (dc != NULL)
  5801. {
  5802. switch (dc->type)
  5803. {
  5804. /* These cannot appear on a constructor or destructor. */
  5805. case DEMANGLE_COMPONENT_RESTRICT_THIS:
  5806. case DEMANGLE_COMPONENT_VOLATILE_THIS:
  5807. case DEMANGLE_COMPONENT_CONST_THIS:
  5808. case DEMANGLE_COMPONENT_REFERENCE_THIS:
  5809. case DEMANGLE_COMPONENT_RVALUE_REFERENCE_THIS:
  5810. default:
  5811. dc = NULL;
  5812. break;
  5813. case DEMANGLE_COMPONENT_TYPED_NAME:
  5814. case DEMANGLE_COMPONENT_TEMPLATE:
  5815. dc = d_left (dc);
  5816. break;
  5817. case DEMANGLE_COMPONENT_QUAL_NAME:
  5818. case DEMANGLE_COMPONENT_LOCAL_NAME:
  5819. dc = d_right (dc);
  5820. break;
  5821. case DEMANGLE_COMPONENT_CTOR:
  5822. *ctor_kind = dc->u.s_ctor.kind;
  5823. ret = 1;
  5824. dc = NULL;
  5825. break;
  5826. case DEMANGLE_COMPONENT_DTOR:
  5827. *dtor_kind = dc->u.s_dtor.kind;
  5828. ret = 1;
  5829. dc = NULL;
  5830. break;
  5831. }
  5832. }
  5833. }
  5834. return ret;
  5835. }
  5836. /* Return whether NAME is the mangled form of a g++ V3 ABI constructor
  5837. name. A non-zero return indicates the type of constructor. */
  5838. enum gnu_v3_ctor_kinds
  5839. is_gnu_v3_mangled_ctor (const char *name)
  5840. {
  5841. enum gnu_v3_ctor_kinds ctor_kind;
  5842. enum gnu_v3_dtor_kinds dtor_kind;
  5843. if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
  5844. return (enum gnu_v3_ctor_kinds) 0;
  5845. return ctor_kind;
  5846. }
  5847. /* Return whether NAME is the mangled form of a g++ V3 ABI destructor
  5848. name. A non-zero return indicates the type of destructor. */
  5849. enum gnu_v3_dtor_kinds
  5850. is_gnu_v3_mangled_dtor (const char *name)
  5851. {
  5852. enum gnu_v3_ctor_kinds ctor_kind;
  5853. enum gnu_v3_dtor_kinds dtor_kind;
  5854. if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
  5855. return (enum gnu_v3_dtor_kinds) 0;
  5856. return dtor_kind;
  5857. }
  5858. #endif /* IN_GLIBCPP_V3 */
  5859. #ifdef STANDALONE_DEMANGLER
  5860. #include "getopt.h"
  5861. #include "dyn-string.h"
  5862. static void print_usage (FILE* fp, int exit_value);
  5863. #define IS_ALPHA(CHAR) \
  5864. (((CHAR) >= 'a' && (CHAR) <= 'z') \
  5865. || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
  5866. /* Non-zero if CHAR is a character than can occur in a mangled name. */
  5867. #define is_mangled_char(CHAR) \
  5868. (IS_ALPHA (CHAR) || IS_DIGIT (CHAR) \
  5869. || (CHAR) == '_' || (CHAR) == '.' || (CHAR) == '$')
  5870. /* The name of this program, as invoked. */
  5871. const char* program_name;
  5872. /* Prints usage summary to FP and then exits with EXIT_VALUE. */
  5873. static void
  5874. print_usage (FILE* fp, int exit_value)
  5875. {
  5876. fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
  5877. fprintf (fp, "Options:\n");
  5878. fprintf (fp, " -h,--help Display this message.\n");
  5879. fprintf (fp, " -p,--no-params Don't display function parameters\n");
  5880. fprintf (fp, " -v,--verbose Produce verbose demanglings.\n");
  5881. fprintf (fp, "If names are provided, they are demangled. Otherwise filters standard input.\n");
  5882. exit (exit_value);
  5883. }
  5884. /* Option specification for getopt_long. */
  5885. static const struct option long_options[] =
  5886. {
  5887. { "help", no_argument, NULL, 'h' },
  5888. { "no-params", no_argument, NULL, 'p' },
  5889. { "verbose", no_argument, NULL, 'v' },
  5890. { NULL, no_argument, NULL, 0 },
  5891. };
  5892. /* Main entry for a demangling filter executable. It will demangle
  5893. its command line arguments, if any. If none are provided, it will
  5894. filter stdin to stdout, replacing any recognized mangled C++ names
  5895. with their demangled equivalents. */
  5896. int
  5897. main (int argc, char *argv[])
  5898. {
  5899. int i;
  5900. int opt_char;
  5901. int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
  5902. /* Use the program name of this program, as invoked. */
  5903. program_name = argv[0];
  5904. /* Parse options. */
  5905. do
  5906. {
  5907. opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
  5908. switch (opt_char)
  5909. {
  5910. case '?': /* Unrecognized option. */
  5911. print_usage (stderr, 1);
  5912. break;
  5913. case 'h':
  5914. print_usage (stdout, 0);
  5915. break;
  5916. case 'p':
  5917. options &= ~ DMGL_PARAMS;
  5918. break;
  5919. case 'v':
  5920. options |= DMGL_VERBOSE;
  5921. break;
  5922. }
  5923. }
  5924. while (opt_char != -1);
  5925. if (optind == argc)
  5926. /* No command line arguments were provided. Filter stdin. */
  5927. {
  5928. dyn_string_t mangled = dyn_string_new (3);
  5929. char *s;
  5930. /* Read all of input. */
  5931. while (!feof (stdin))
  5932. {
  5933. char c;
  5934. /* Pile characters into mangled until we hit one that can't
  5935. occur in a mangled name. */
  5936. c = getchar ();
  5937. while (!feof (stdin) && is_mangled_char (c))
  5938. {
  5939. dyn_string_append_char (mangled, c);
  5940. if (feof (stdin))
  5941. break;
  5942. c = getchar ();
  5943. }
  5944. if (dyn_string_length (mangled) > 0)
  5945. {
  5946. #ifdef IN_GLIBCPP_V3
  5947. s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
  5948. #else
  5949. s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
  5950. #endif
  5951. if (s != NULL)
  5952. {
  5953. fputs (s, stdout);
  5954. free (s);
  5955. }
  5956. else
  5957. {
  5958. /* It might not have been a mangled name. Print the
  5959. original text. */
  5960. fputs (dyn_string_buf (mangled), stdout);
  5961. }
  5962. dyn_string_clear (mangled);
  5963. }
  5964. /* If we haven't hit EOF yet, we've read one character that
  5965. can't occur in a mangled name, so print it out. */
  5966. if (!feof (stdin))
  5967. putchar (c);
  5968. }
  5969. dyn_string_delete (mangled);
  5970. }
  5971. else
  5972. /* Demangle command line arguments. */
  5973. {
  5974. /* Loop over command line arguments. */
  5975. for (i = optind; i < argc; ++i)
  5976. {
  5977. char *s;
  5978. #ifdef IN_GLIBCPP_V3
  5979. int status;
  5980. #endif
  5981. /* Attempt to demangle. */
  5982. #ifdef IN_GLIBCPP_V3
  5983. s = __cxa_demangle (argv[i], NULL, NULL, &status);
  5984. #else
  5985. s = cplus_demangle_v3 (argv[i], options);
  5986. #endif
  5987. /* If it worked, print the demangled name. */
  5988. if (s != NULL)
  5989. {
  5990. printf ("%s\n", s);
  5991. free (s);
  5992. }
  5993. else
  5994. {
  5995. #ifdef IN_GLIBCPP_V3
  5996. fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
  5997. #else
  5998. fprintf (stderr, "Failed: %s\n", argv[i]);
  5999. #endif
  6000. }
  6001. }
  6002. }
  6003. return 0;
  6004. }
  6005. #endif /* STANDALONE_DEMANGLER */