cp-demangle.c 167 KB

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