cp-demangle.c 167 KB

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