cplus-dem.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886
  1. /* Demangler for GNU C++
  2. Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
  3. 2000, 2001, 2002, 2003, 2004, 2010 Free Software Foundation, Inc.
  4. Written by James Clark (jjc@jclark.uucp)
  5. Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  6. Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
  7. This file is part of the libiberty library.
  8. Libiberty is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12. In addition to the permissions in the GNU Library General Public
  13. License, the Free Software Foundation gives you unlimited permission
  14. to link the compiled version of this file into combinations with other
  15. programs, and to distribute those combinations without any restriction
  16. coming from the use of this file. (The Library Public License
  17. restrictions do apply in other respects; for example, they cover
  18. modification of the file, and distribution when not linked into a
  19. combined executable.)
  20. Libiberty is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. Library General Public License for more details.
  24. You should have received a copy of the GNU Library General Public
  25. License along with libiberty; see the file COPYING.LIB. If
  26. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  27. Boston, MA 02110-1301, USA. */
  28. /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
  29. This file imports xmalloc and xrealloc, which are like malloc and
  30. realloc except that they generate a fatal error if there is no
  31. available memory. */
  32. /* This file lives in both GCC and libiberty. When making changes, please
  33. try not to break either. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include "safe-ctype.h"
  38. #include <sys/types.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #ifdef HAVE_STDLIB_H
  42. #include <stdlib.h>
  43. #else
  44. void * malloc ();
  45. void * realloc ();
  46. #endif
  47. #include <demangle.h>
  48. #undef CURRENT_DEMANGLING_STYLE
  49. #define CURRENT_DEMANGLING_STYLE work->options
  50. #include "libiberty.h"
  51. #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
  52. /* A value at least one greater than the maximum number of characters
  53. that will be output when using the `%d' format with `printf'. */
  54. #define INTBUF_SIZE 32
  55. extern void fancy_abort (void) ATTRIBUTE_NORETURN;
  56. /* In order to allow a single demangler executable to demangle strings
  57. using various common values of CPLUS_MARKER, as well as any specific
  58. one set at compile time, we maintain a string containing all the
  59. commonly used ones, and check to see if the marker we are looking for
  60. is in that string. CPLUS_MARKER is usually '$' on systems where the
  61. assembler can deal with that. Where the assembler can't, it's usually
  62. '.' (but on many systems '.' is used for other things). We put the
  63. current defined CPLUS_MARKER first (which defaults to '$'), followed
  64. by the next most common value, followed by an explicit '$' in case
  65. the value of CPLUS_MARKER is not '$'.
  66. We could avoid this if we could just get g++ to tell us what the actual
  67. cplus marker character is as part of the debug information, perhaps by
  68. ensuring that it is the character that terminates the gcc<n>_compiled
  69. marker symbol (FIXME). */
  70. #if !defined (CPLUS_MARKER)
  71. #define CPLUS_MARKER '$'
  72. #endif
  73. enum demangling_styles current_demangling_style = auto_demangling;
  74. static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
  75. static char char_str[2] = { '\000', '\000' };
  76. void
  77. set_cplus_marker_for_demangling (int ch)
  78. {
  79. cplus_markers[0] = ch;
  80. }
  81. typedef struct string /* Beware: these aren't required to be */
  82. { /* '\0' terminated. */
  83. char *b; /* pointer to start of string */
  84. char *p; /* pointer after last character */
  85. char *e; /* pointer after end of allocated space */
  86. } string;
  87. /* Stuff that is shared between sub-routines.
  88. Using a shared structure allows cplus_demangle to be reentrant. */
  89. struct work_stuff
  90. {
  91. int options;
  92. char **typevec;
  93. char **ktypevec;
  94. char **btypevec;
  95. int numk;
  96. int numb;
  97. int ksize;
  98. int bsize;
  99. int ntypes;
  100. int typevec_size;
  101. int constructor;
  102. int destructor;
  103. int static_type; /* A static member function */
  104. int temp_start; /* index in demangled to start of template args */
  105. int type_quals; /* The type qualifiers. */
  106. int dllimported; /* Symbol imported from a PE DLL */
  107. char **tmpl_argvec; /* Template function arguments. */
  108. int ntmpl_args; /* The number of template function arguments. */
  109. int forgetting_types; /* Nonzero if we are not remembering the types
  110. we see. */
  111. string* previous_argument; /* The last function argument demangled. */
  112. int nrepeats; /* The number of times to repeat the previous
  113. argument. */
  114. };
  115. #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
  116. #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
  117. static const struct optable
  118. {
  119. const char *const in;
  120. const char *const out;
  121. const int flags;
  122. } optable[] = {
  123. {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
  124. {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
  125. {"new", " new", 0}, /* old (1.91, and 1.x) */
  126. {"delete", " delete", 0}, /* old (1.91, and 1.x) */
  127. {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
  128. {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
  129. {"as", "=", DMGL_ANSI}, /* ansi */
  130. {"ne", "!=", DMGL_ANSI}, /* old, ansi */
  131. {"eq", "==", DMGL_ANSI}, /* old, ansi */
  132. {"ge", ">=", DMGL_ANSI}, /* old, ansi */
  133. {"gt", ">", DMGL_ANSI}, /* old, ansi */
  134. {"le", "<=", DMGL_ANSI}, /* old, ansi */
  135. {"lt", "<", DMGL_ANSI}, /* old, ansi */
  136. {"plus", "+", 0}, /* old */
  137. {"pl", "+", DMGL_ANSI}, /* ansi */
  138. {"apl", "+=", DMGL_ANSI}, /* ansi */
  139. {"minus", "-", 0}, /* old */
  140. {"mi", "-", DMGL_ANSI}, /* ansi */
  141. {"ami", "-=", DMGL_ANSI}, /* ansi */
  142. {"mult", "*", 0}, /* old */
  143. {"ml", "*", DMGL_ANSI}, /* ansi */
  144. {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
  145. {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
  146. {"convert", "+", 0}, /* old (unary +) */
  147. {"negate", "-", 0}, /* old (unary -) */
  148. {"trunc_mod", "%", 0}, /* old */
  149. {"md", "%", DMGL_ANSI}, /* ansi */
  150. {"amd", "%=", DMGL_ANSI}, /* ansi */
  151. {"trunc_div", "/", 0}, /* old */
  152. {"dv", "/", DMGL_ANSI}, /* ansi */
  153. {"adv", "/=", DMGL_ANSI}, /* ansi */
  154. {"truth_andif", "&&", 0}, /* old */
  155. {"aa", "&&", DMGL_ANSI}, /* ansi */
  156. {"truth_orif", "||", 0}, /* old */
  157. {"oo", "||", DMGL_ANSI}, /* ansi */
  158. {"truth_not", "!", 0}, /* old */
  159. {"nt", "!", DMGL_ANSI}, /* ansi */
  160. {"postincrement","++", 0}, /* old */
  161. {"pp", "++", DMGL_ANSI}, /* ansi */
  162. {"postdecrement","--", 0}, /* old */
  163. {"mm", "--", DMGL_ANSI}, /* ansi */
  164. {"bit_ior", "|", 0}, /* old */
  165. {"or", "|", DMGL_ANSI}, /* ansi */
  166. {"aor", "|=", DMGL_ANSI}, /* ansi */
  167. {"bit_xor", "^", 0}, /* old */
  168. {"er", "^", DMGL_ANSI}, /* ansi */
  169. {"aer", "^=", DMGL_ANSI}, /* ansi */
  170. {"bit_and", "&", 0}, /* old */
  171. {"ad", "&", DMGL_ANSI}, /* ansi */
  172. {"aad", "&=", DMGL_ANSI}, /* ansi */
  173. {"bit_not", "~", 0}, /* old */
  174. {"co", "~", DMGL_ANSI}, /* ansi */
  175. {"call", "()", 0}, /* old */
  176. {"cl", "()", DMGL_ANSI}, /* ansi */
  177. {"alshift", "<<", 0}, /* old */
  178. {"ls", "<<", DMGL_ANSI}, /* ansi */
  179. {"als", "<<=", DMGL_ANSI}, /* ansi */
  180. {"arshift", ">>", 0}, /* old */
  181. {"rs", ">>", DMGL_ANSI}, /* ansi */
  182. {"ars", ">>=", DMGL_ANSI}, /* ansi */
  183. {"component", "->", 0}, /* old */
  184. {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
  185. {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
  186. {"indirect", "*", 0}, /* old */
  187. {"method_call", "->()", 0}, /* old */
  188. {"addr", "&", 0}, /* old (unary &) */
  189. {"array", "[]", 0}, /* old */
  190. {"vc", "[]", DMGL_ANSI}, /* ansi */
  191. {"compound", ", ", 0}, /* old */
  192. {"cm", ", ", DMGL_ANSI}, /* ansi */
  193. {"cond", "?:", 0}, /* old */
  194. {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
  195. {"max", ">?", 0}, /* old */
  196. {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
  197. {"min", "<?", 0}, /* old */
  198. {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
  199. {"nop", "", 0}, /* old (for operator=) */
  200. {"rm", "->*", DMGL_ANSI}, /* ansi */
  201. {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
  202. };
  203. /* These values are used to indicate the various type varieties.
  204. They are all non-zero so that they can be used as `success'
  205. values. */
  206. typedef enum type_kind_t
  207. {
  208. tk_none,
  209. tk_pointer,
  210. tk_reference,
  211. tk_integral,
  212. tk_bool,
  213. tk_char,
  214. tk_real
  215. } type_kind_t;
  216. const struct demangler_engine libiberty_demanglers[] =
  217. {
  218. {
  219. NO_DEMANGLING_STYLE_STRING,
  220. no_demangling,
  221. "Demangling disabled"
  222. }
  223. ,
  224. {
  225. AUTO_DEMANGLING_STYLE_STRING,
  226. auto_demangling,
  227. "Automatic selection based on executable"
  228. }
  229. ,
  230. {
  231. GNU_DEMANGLING_STYLE_STRING,
  232. gnu_demangling,
  233. "GNU (g++) style demangling"
  234. }
  235. ,
  236. {
  237. LUCID_DEMANGLING_STYLE_STRING,
  238. lucid_demangling,
  239. "Lucid (lcc) style demangling"
  240. }
  241. ,
  242. {
  243. ARM_DEMANGLING_STYLE_STRING,
  244. arm_demangling,
  245. "ARM style demangling"
  246. }
  247. ,
  248. {
  249. HP_DEMANGLING_STYLE_STRING,
  250. hp_demangling,
  251. "HP (aCC) style demangling"
  252. }
  253. ,
  254. {
  255. EDG_DEMANGLING_STYLE_STRING,
  256. edg_demangling,
  257. "EDG style demangling"
  258. }
  259. ,
  260. {
  261. GNU_V3_DEMANGLING_STYLE_STRING,
  262. gnu_v3_demangling,
  263. "GNU (g++) V3 ABI-style demangling"
  264. }
  265. ,
  266. {
  267. JAVA_DEMANGLING_STYLE_STRING,
  268. java_demangling,
  269. "Java style demangling"
  270. }
  271. ,
  272. {
  273. GNAT_DEMANGLING_STYLE_STRING,
  274. gnat_demangling,
  275. "GNAT style demangling"
  276. }
  277. ,
  278. {
  279. DLANG_DEMANGLING_STYLE_STRING,
  280. dlang_demangling,
  281. "DLANG style demangling"
  282. }
  283. ,
  284. {
  285. NULL, unknown_demangling, NULL
  286. }
  287. };
  288. #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
  289. #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
  290. string_append(str, " ");}
  291. #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
  292. /* The scope separator appropriate for the language being demangled. */
  293. #define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")
  294. #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
  295. #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
  296. /* Prototypes for local functions */
  297. static void delete_work_stuff (struct work_stuff *);
  298. static void delete_non_B_K_work_stuff (struct work_stuff *);
  299. static char *mop_up (struct work_stuff *, string *, int);
  300. static void squangle_mop_up (struct work_stuff *);
  301. static void work_stuff_copy_to_from (struct work_stuff *, struct work_stuff *);
  302. #if 0
  303. static int
  304. demangle_method_args (struct work_stuff *, const char **, string *);
  305. #endif
  306. static char *
  307. internal_cplus_demangle (struct work_stuff *, const char *);
  308. static int
  309. demangle_template_template_parm (struct work_stuff *work,
  310. const char **, string *);
  311. static int
  312. demangle_template (struct work_stuff *work, const char **, string *,
  313. string *, int, int);
  314. static int
  315. arm_pt (struct work_stuff *, const char *, int, const char **,
  316. const char **);
  317. static int
  318. demangle_class_name (struct work_stuff *, const char **, string *);
  319. static int
  320. demangle_qualified (struct work_stuff *, const char **, string *,
  321. int, int);
  322. static int demangle_class (struct work_stuff *, const char **, string *);
  323. static int demangle_fund_type (struct work_stuff *, const char **, string *);
  324. static int demangle_signature (struct work_stuff *, const char **, string *);
  325. static int demangle_prefix (struct work_stuff *, const char **, string *);
  326. static int gnu_special (struct work_stuff *, const char **, string *);
  327. static int arm_special (const char **, string *);
  328. static void string_need (string *, int);
  329. static void string_delete (string *);
  330. static void
  331. string_init (string *);
  332. static void string_clear (string *);
  333. #if 0
  334. static int string_empty (string *);
  335. #endif
  336. static void string_append (string *, const char *);
  337. static void string_appends (string *, string *);
  338. static void string_appendn (string *, const char *, int);
  339. static void string_prepend (string *, const char *);
  340. static void string_prependn (string *, const char *, int);
  341. static void string_append_template_idx (string *, int);
  342. static int get_count (const char **, int *);
  343. static int consume_count (const char **);
  344. static int consume_count_with_underscores (const char**);
  345. static int demangle_args (struct work_stuff *, const char **, string *);
  346. static int demangle_nested_args (struct work_stuff*, const char**, string*);
  347. static int do_type (struct work_stuff *, const char **, string *);
  348. static int do_arg (struct work_stuff *, const char **, string *);
  349. static int
  350. demangle_function_name (struct work_stuff *, const char **, string *,
  351. const char *);
  352. static int
  353. iterate_demangle_function (struct work_stuff *,
  354. const char **, string *, const char *);
  355. static void remember_type (struct work_stuff *, const char *, int);
  356. static void remember_Btype (struct work_stuff *, const char *, int, int);
  357. static int register_Btype (struct work_stuff *);
  358. static void remember_Ktype (struct work_stuff *, const char *, int);
  359. static void forget_types (struct work_stuff *);
  360. static void forget_B_and_K_types (struct work_stuff *);
  361. static void string_prepends (string *, string *);
  362. static int
  363. demangle_template_value_parm (struct work_stuff*, const char**,
  364. string*, type_kind_t);
  365. static int
  366. do_hpacc_template_const_value (struct work_stuff *, const char **, string *);
  367. static int
  368. do_hpacc_template_literal (struct work_stuff *, const char **, string *);
  369. static int snarf_numeric_literal (const char **, string *);
  370. /* There is a TYPE_QUAL value for each type qualifier. They can be
  371. combined by bitwise-or to form the complete set of qualifiers for a
  372. type. */
  373. #define TYPE_UNQUALIFIED 0x0
  374. #define TYPE_QUAL_CONST 0x1
  375. #define TYPE_QUAL_VOLATILE 0x2
  376. #define TYPE_QUAL_RESTRICT 0x4
  377. static int code_for_qualifier (int);
  378. static const char* qualifier_string (int);
  379. static const char* demangle_qualifier (int);
  380. static int demangle_expression (struct work_stuff *, const char **, string *,
  381. type_kind_t);
  382. static int
  383. demangle_integral_value (struct work_stuff *, const char **, string *);
  384. static int
  385. demangle_real_value (struct work_stuff *, const char **, string *);
  386. static void
  387. demangle_arm_hp_template (struct work_stuff *, const char **, int, string *);
  388. static void
  389. recursively_demangle (struct work_stuff *, const char **, string *, int);
  390. /* Translate count to integer, consuming tokens in the process.
  391. Conversion terminates on the first non-digit character.
  392. Trying to consume something that isn't a count results in no
  393. consumption of input and a return of -1.
  394. Overflow consumes the rest of the digits, and returns -1. */
  395. static int
  396. consume_count (const char **type)
  397. {
  398. int count = 0;
  399. if (! ISDIGIT ((unsigned char)**type))
  400. return -1;
  401. while (ISDIGIT ((unsigned char)**type))
  402. {
  403. count *= 10;
  404. /* Check for overflow.
  405. We assume that count is represented using two's-complement;
  406. no power of two is divisible by ten, so if an overflow occurs
  407. when multiplying by ten, the result will not be a multiple of
  408. ten. */
  409. if ((count % 10) != 0)
  410. {
  411. while (ISDIGIT ((unsigned char) **type))
  412. (*type)++;
  413. return -1;
  414. }
  415. count += **type - '0';
  416. (*type)++;
  417. }
  418. if (count < 0)
  419. count = -1;
  420. return (count);
  421. }
  422. /* Like consume_count, but for counts that are preceded and followed
  423. by '_' if they are greater than 10. Also, -1 is returned for
  424. failure, since 0 can be a valid value. */
  425. static int
  426. consume_count_with_underscores (const char **mangled)
  427. {
  428. int idx;
  429. if (**mangled == '_')
  430. {
  431. (*mangled)++;
  432. if (!ISDIGIT ((unsigned char)**mangled))
  433. return -1;
  434. idx = consume_count (mangled);
  435. if (**mangled != '_')
  436. /* The trailing underscore was missing. */
  437. return -1;
  438. (*mangled)++;
  439. }
  440. else
  441. {
  442. if (**mangled < '0' || **mangled > '9')
  443. return -1;
  444. idx = **mangled - '0';
  445. (*mangled)++;
  446. }
  447. return idx;
  448. }
  449. /* C is the code for a type-qualifier. Return the TYPE_QUAL
  450. corresponding to this qualifier. */
  451. static int
  452. code_for_qualifier (int c)
  453. {
  454. switch (c)
  455. {
  456. case 'C':
  457. return TYPE_QUAL_CONST;
  458. case 'V':
  459. return TYPE_QUAL_VOLATILE;
  460. case 'u':
  461. return TYPE_QUAL_RESTRICT;
  462. default:
  463. break;
  464. }
  465. /* C was an invalid qualifier. */
  466. abort ();
  467. }
  468. /* Return the string corresponding to the qualifiers given by
  469. TYPE_QUALS. */
  470. static const char*
  471. qualifier_string (int type_quals)
  472. {
  473. switch (type_quals)
  474. {
  475. case TYPE_UNQUALIFIED:
  476. return "";
  477. case TYPE_QUAL_CONST:
  478. return "const";
  479. case TYPE_QUAL_VOLATILE:
  480. return "volatile";
  481. case TYPE_QUAL_RESTRICT:
  482. return "__restrict";
  483. case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
  484. return "const volatile";
  485. case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
  486. return "const __restrict";
  487. case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
  488. return "volatile __restrict";
  489. case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
  490. return "const volatile __restrict";
  491. default:
  492. break;
  493. }
  494. /* TYPE_QUALS was an invalid qualifier set. */
  495. abort ();
  496. }
  497. /* C is the code for a type-qualifier. Return the string
  498. corresponding to this qualifier. This function should only be
  499. called with a valid qualifier code. */
  500. static const char*
  501. demangle_qualifier (int c)
  502. {
  503. return qualifier_string (code_for_qualifier (c));
  504. }
  505. int
  506. cplus_demangle_opname (const char *opname, char *result, int options)
  507. {
  508. int len, len1, ret;
  509. string type;
  510. struct work_stuff work[1];
  511. const char *tem;
  512. len = strlen(opname);
  513. result[0] = '\0';
  514. ret = 0;
  515. memset ((char *) work, 0, sizeof (work));
  516. work->options = options;
  517. if (opname[0] == '_' && opname[1] == '_'
  518. && opname[2] == 'o' && opname[3] == 'p')
  519. {
  520. /* ANSI. */
  521. /* type conversion operator. */
  522. tem = opname + 4;
  523. if (do_type (work, &tem, &type))
  524. {
  525. strcat (result, "operator ");
  526. strncat (result, type.b, type.p - type.b);
  527. string_delete (&type);
  528. ret = 1;
  529. }
  530. }
  531. else if (opname[0] == '_' && opname[1] == '_'
  532. && ISLOWER((unsigned char)opname[2])
  533. && ISLOWER((unsigned char)opname[3]))
  534. {
  535. if (opname[4] == '\0')
  536. {
  537. /* Operator. */
  538. size_t i;
  539. for (i = 0; i < ARRAY_SIZE (optable); i++)
  540. {
  541. if (strlen (optable[i].in) == 2
  542. && memcmp (optable[i].in, opname + 2, 2) == 0)
  543. {
  544. strcat (result, "operator");
  545. strcat (result, optable[i].out);
  546. ret = 1;
  547. break;
  548. }
  549. }
  550. }
  551. else
  552. {
  553. if (opname[2] == 'a' && opname[5] == '\0')
  554. {
  555. /* Assignment. */
  556. size_t i;
  557. for (i = 0; i < ARRAY_SIZE (optable); i++)
  558. {
  559. if (strlen (optable[i].in) == 3
  560. && memcmp (optable[i].in, opname + 2, 3) == 0)
  561. {
  562. strcat (result, "operator");
  563. strcat (result, optable[i].out);
  564. ret = 1;
  565. break;
  566. }
  567. }
  568. }
  569. }
  570. }
  571. else if (len >= 3
  572. && opname[0] == 'o'
  573. && opname[1] == 'p'
  574. && strchr (cplus_markers, opname[2]) != NULL)
  575. {
  576. /* see if it's an assignment expression */
  577. if (len >= 10 /* op$assign_ */
  578. && memcmp (opname + 3, "assign_", 7) == 0)
  579. {
  580. size_t i;
  581. for (i = 0; i < ARRAY_SIZE (optable); i++)
  582. {
  583. len1 = len - 10;
  584. if ((int) strlen (optable[i].in) == len1
  585. && memcmp (optable[i].in, opname + 10, len1) == 0)
  586. {
  587. strcat (result, "operator");
  588. strcat (result, optable[i].out);
  589. strcat (result, "=");
  590. ret = 1;
  591. break;
  592. }
  593. }
  594. }
  595. else
  596. {
  597. size_t i;
  598. for (i = 0; i < ARRAY_SIZE (optable); i++)
  599. {
  600. len1 = len - 3;
  601. if ((int) strlen (optable[i].in) == len1
  602. && memcmp (optable[i].in, opname + 3, len1) == 0)
  603. {
  604. strcat (result, "operator");
  605. strcat (result, optable[i].out);
  606. ret = 1;
  607. break;
  608. }
  609. }
  610. }
  611. }
  612. else if (len >= 5 && memcmp (opname, "type", 4) == 0
  613. && strchr (cplus_markers, opname[4]) != NULL)
  614. {
  615. /* type conversion operator */
  616. tem = opname + 5;
  617. if (do_type (work, &tem, &type))
  618. {
  619. strcat (result, "operator ");
  620. strncat (result, type.b, type.p - type.b);
  621. string_delete (&type);
  622. ret = 1;
  623. }
  624. }
  625. squangle_mop_up (work);
  626. return ret;
  627. }
  628. /* Takes operator name as e.g. "++" and returns mangled
  629. operator name (e.g. "postincrement_expr"), or NULL if not found.
  630. If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
  631. if OPTIONS & DMGL_ANSI == 0, return the old GNU name. */
  632. const char *
  633. cplus_mangle_opname (const char *opname, int options)
  634. {
  635. size_t i;
  636. int len;
  637. len = strlen (opname);
  638. for (i = 0; i < ARRAY_SIZE (optable); i++)
  639. {
  640. if ((int) strlen (optable[i].out) == len
  641. && (options & DMGL_ANSI) == (optable[i].flags & DMGL_ANSI)
  642. && memcmp (optable[i].out, opname, len) == 0)
  643. return optable[i].in;
  644. }
  645. return (0);
  646. }
  647. /* Add a routine to set the demangling style to be sure it is valid and
  648. allow for any demangler initialization that maybe necessary. */
  649. enum demangling_styles
  650. cplus_demangle_set_style (enum demangling_styles style)
  651. {
  652. const struct demangler_engine *demangler = libiberty_demanglers;
  653. for (; demangler->demangling_style != unknown_demangling; ++demangler)
  654. if (style == demangler->demangling_style)
  655. {
  656. current_demangling_style = style;
  657. return current_demangling_style;
  658. }
  659. return unknown_demangling;
  660. }
  661. /* Do string name to style translation */
  662. enum demangling_styles
  663. cplus_demangle_name_to_style (const char *name)
  664. {
  665. const struct demangler_engine *demangler = libiberty_demanglers;
  666. for (; demangler->demangling_style != unknown_demangling; ++demangler)
  667. if (strcmp (name, demangler->demangling_style_name) == 0)
  668. return demangler->demangling_style;
  669. return unknown_demangling;
  670. }
  671. /* char *cplus_demangle (const char *mangled, int options)
  672. If MANGLED is a mangled function name produced by GNU C++, then
  673. a pointer to a @code{malloc}ed string giving a C++ representation
  674. of the name will be returned; otherwise NULL will be returned.
  675. It is the caller's responsibility to free the string which
  676. is returned.
  677. The OPTIONS arg may contain one or more of the following bits:
  678. DMGL_ANSI ANSI qualifiers such as `const' and `void' are
  679. included.
  680. DMGL_PARAMS Function parameters are included.
  681. For example,
  682. cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
  683. cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
  684. cplus_demangle ("foo__1Ai", 0) => "A::foo"
  685. cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
  686. cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
  687. cplus_demangle ("foo__1Afe", 0) => "A::foo"
  688. Note that any leading underscores, or other such characters prepended by
  689. the compilation system, are presumed to have already been stripped from
  690. MANGLED. */
  691. char *
  692. cplus_demangle (const char *mangled, int options)
  693. {
  694. char *ret;
  695. struct work_stuff work[1];
  696. if (current_demangling_style == no_demangling)
  697. return xstrdup (mangled);
  698. memset ((char *) work, 0, sizeof (work));
  699. work->options = options;
  700. if ((work->options & DMGL_STYLE_MASK) == 0)
  701. work->options |= (int) current_demangling_style & DMGL_STYLE_MASK;
  702. /* The V3 ABI demangling is implemented elsewhere. */
  703. if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
  704. {
  705. ret = cplus_demangle_v3 (mangled, work->options);
  706. if (ret || GNU_V3_DEMANGLING)
  707. return ret;
  708. }
  709. if (JAVA_DEMANGLING)
  710. {
  711. ret = java_demangle_v3 (mangled);
  712. if (ret)
  713. return ret;
  714. }
  715. if (GNAT_DEMANGLING)
  716. return ada_demangle (mangled, options);
  717. if (DLANG_DEMANGLING)
  718. {
  719. ret = dlang_demangle (mangled, options);
  720. if (ret)
  721. return ret;
  722. }
  723. ret = internal_cplus_demangle (work, mangled);
  724. squangle_mop_up (work);
  725. return (ret);
  726. }
  727. /* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
  728. char *
  729. ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
  730. {
  731. int len0;
  732. const char* p;
  733. char *d;
  734. char *demangled;
  735. /* Discard leading _ada_, which is used for library level subprograms. */
  736. if (strncmp (mangled, "_ada_", 5) == 0)
  737. mangled += 5;
  738. /* All ada unit names are lower-case. */
  739. if (!ISLOWER (mangled[0]))
  740. goto unknown;
  741. /* Most of the demangling will trivially remove chars. Operator names
  742. may add one char but because they are always preceeded by '__' which is
  743. replaced by '.', they eventually never expand the size.
  744. A few special names such as '___elabs' add a few chars (at most 7), but
  745. they occur only once. */
  746. len0 = strlen (mangled) + 7 + 1;
  747. demangled = XNEWVEC (char, len0);
  748. d = demangled;
  749. p = mangled;
  750. while (1)
  751. {
  752. /* An entity names is expected. */
  753. if (ISLOWER (*p))
  754. {
  755. /* An identifier, which is always lower case. */
  756. do
  757. *d++ = *p++;
  758. while (ISLOWER(*p) || ISDIGIT (*p)
  759. || (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
  760. }
  761. else if (p[0] == 'O')
  762. {
  763. /* An operator name. */
  764. static const char * const operators[][2] =
  765. {{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
  766. {"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
  767. {"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
  768. {"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
  769. {"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
  770. {"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
  771. {"Oexpon", "**"}, {NULL, NULL}};
  772. int k;
  773. for (k = 0; operators[k][0] != NULL; k++)
  774. {
  775. size_t slen = strlen (operators[k][0]);
  776. if (strncmp (p, operators[k][0], slen) == 0)
  777. {
  778. p += slen;
  779. slen = strlen (operators[k][1]);
  780. *d++ = '"';
  781. memcpy (d, operators[k][1], slen);
  782. d += slen;
  783. *d++ = '"';
  784. break;
  785. }
  786. }
  787. /* Operator not found. */
  788. if (operators[k][0] == NULL)
  789. goto unknown;
  790. }
  791. else
  792. {
  793. /* Not a GNAT encoding. */
  794. goto unknown;
  795. }
  796. /* The name can be directly followed by some uppercase letters. */
  797. if (p[0] == 'T' && p[1] == 'K')
  798. {
  799. /* Task stuff. */
  800. if (p[2] == 'B' && p[3] == 0)
  801. {
  802. /* Subprogram for task body. */
  803. break;
  804. }
  805. else if (p[2] == '_' && p[3] == '_')
  806. {
  807. /* Inner declarations in a task. */
  808. p += 4;
  809. *d++ = '.';
  810. continue;
  811. }
  812. else
  813. goto unknown;
  814. }
  815. if (p[0] == 'E' && p[1] == 0)
  816. {
  817. /* Exception name. */
  818. goto unknown;
  819. }
  820. if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
  821. {
  822. /* Protected type subprogram. */
  823. break;
  824. }
  825. if ((*p == 'N' || *p == 'S') && p[1] == 0)
  826. {
  827. /* Enumerated type name table. */
  828. goto unknown;
  829. }
  830. if (p[0] == 'X')
  831. {
  832. /* Body nested. */
  833. p++;
  834. while (p[0] == 'n' || p[0] == 'b')
  835. p++;
  836. }
  837. if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
  838. {
  839. /* Stream operations. */
  840. const char *name;
  841. switch (p[1])
  842. {
  843. case 'R':
  844. name = "'Read";
  845. break;
  846. case 'W':
  847. name = "'Write";
  848. break;
  849. case 'I':
  850. name = "'Input";
  851. break;
  852. case 'O':
  853. name = "'Output";
  854. break;
  855. default:
  856. goto unknown;
  857. }
  858. p += 2;
  859. strcpy (d, name);
  860. d += strlen (name);
  861. }
  862. else if (p[0] == 'D')
  863. {
  864. /* Controlled type operation. */
  865. const char *name;
  866. switch (p[1])
  867. {
  868. case 'F':
  869. name = ".Finalize";
  870. break;
  871. case 'A':
  872. name = ".Adjust";
  873. break;
  874. default:
  875. goto unknown;
  876. }
  877. strcpy (d, name);
  878. d += strlen (name);
  879. break;
  880. }
  881. if (p[0] == '_')
  882. {
  883. /* Separator. */
  884. if (p[1] == '_')
  885. {
  886. /* Standard separator. Handled first. */
  887. p += 2;
  888. if (ISDIGIT (*p))
  889. {
  890. /* Overloading number. */
  891. do
  892. p++;
  893. while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
  894. if (*p == 'X')
  895. {
  896. p++;
  897. while (p[0] == 'n' || p[0] == 'b')
  898. p++;
  899. }
  900. }
  901. else if (p[0] == '_' && p[1] != '_')
  902. {
  903. /* Special names. */
  904. static const char * const special[][2] = {
  905. { "_elabb", "'Elab_Body" },
  906. { "_elabs", "'Elab_Spec" },
  907. { "_size", "'Size" },
  908. { "_alignment", "'Alignment" },
  909. { "_assign", ".\":=\"" },
  910. { NULL, NULL }
  911. };
  912. int k;
  913. for (k = 0; special[k][0] != NULL; k++)
  914. {
  915. size_t slen = strlen (special[k][0]);
  916. if (strncmp (p, special[k][0], slen) == 0)
  917. {
  918. p += slen;
  919. slen = strlen (special[k][1]);
  920. memcpy (d, special[k][1], slen);
  921. d += slen;
  922. break;
  923. }
  924. }
  925. if (special[k][0] != NULL)
  926. break;
  927. else
  928. goto unknown;
  929. }
  930. else
  931. {
  932. *d++ = '.';
  933. continue;
  934. }
  935. }
  936. else if (p[1] == 'B' || p[1] == 'E')
  937. {
  938. /* Entry Body or barrier Evaluation. */
  939. p += 2;
  940. while (ISDIGIT (*p))
  941. p++;
  942. if (p[0] == 's' && p[1] == 0)
  943. break;
  944. else
  945. goto unknown;
  946. }
  947. else
  948. goto unknown;
  949. }
  950. if (p[0] == '.' && ISDIGIT (p[1]))
  951. {
  952. /* Nested subprogram. */
  953. p += 2;
  954. while (ISDIGIT (*p))
  955. p++;
  956. }
  957. if (*p == 0)
  958. {
  959. /* End of mangled name. */
  960. break;
  961. }
  962. else
  963. goto unknown;
  964. }
  965. *d = 0;
  966. return demangled;
  967. unknown:
  968. len0 = strlen (mangled);
  969. demangled = XNEWVEC (char, len0 + 3);
  970. if (mangled[0] == '<')
  971. strcpy (demangled, mangled);
  972. else
  973. sprintf (demangled, "<%s>", mangled);
  974. return demangled;
  975. }
  976. /* This function performs most of what cplus_demangle use to do, but
  977. to be able to demangle a name with a B, K or n code, we need to
  978. have a longer term memory of what types have been seen. The original
  979. now initializes and cleans up the squangle code info, while internal
  980. calls go directly to this routine to avoid resetting that info. */
  981. static char *
  982. internal_cplus_demangle (struct work_stuff *work, const char *mangled)
  983. {
  984. string decl;
  985. int success = 0;
  986. char *demangled = NULL;
  987. int s1, s2, s3, s4;
  988. s1 = work->constructor;
  989. s2 = work->destructor;
  990. s3 = work->static_type;
  991. s4 = work->type_quals;
  992. work->constructor = work->destructor = 0;
  993. work->type_quals = TYPE_UNQUALIFIED;
  994. work->dllimported = 0;
  995. if ((mangled != NULL) && (*mangled != '\0'))
  996. {
  997. string_init (&decl);
  998. /* First check to see if gnu style demangling is active and if the
  999. string to be demangled contains a CPLUS_MARKER. If so, attempt to
  1000. recognize one of the gnu special forms rather than looking for a
  1001. standard prefix. In particular, don't worry about whether there
  1002. is a "__" string in the mangled string. Consider "_$_5__foo" for
  1003. example. */
  1004. if ((AUTO_DEMANGLING || GNU_DEMANGLING))
  1005. {
  1006. success = gnu_special (work, &mangled, &decl);
  1007. if (!success)
  1008. {
  1009. delete_work_stuff (work);
  1010. string_delete (&decl);
  1011. }
  1012. }
  1013. if (!success)
  1014. {
  1015. success = demangle_prefix (work, &mangled, &decl);
  1016. }
  1017. if (success && (*mangled != '\0'))
  1018. {
  1019. success = demangle_signature (work, &mangled, &decl);
  1020. }
  1021. if (work->constructor == 2)
  1022. {
  1023. string_prepend (&decl, "global constructors keyed to ");
  1024. work->constructor = 0;
  1025. }
  1026. else if (work->destructor == 2)
  1027. {
  1028. string_prepend (&decl, "global destructors keyed to ");
  1029. work->destructor = 0;
  1030. }
  1031. else if (work->dllimported == 1)
  1032. {
  1033. string_prepend (&decl, "import stub for ");
  1034. work->dllimported = 0;
  1035. }
  1036. demangled = mop_up (work, &decl, success);
  1037. }
  1038. work->constructor = s1;
  1039. work->destructor = s2;
  1040. work->static_type = s3;
  1041. work->type_quals = s4;
  1042. return demangled;
  1043. }
  1044. /* Clear out and squangling related storage */
  1045. static void
  1046. squangle_mop_up (struct work_stuff *work)
  1047. {
  1048. /* clean up the B and K type mangling types. */
  1049. forget_B_and_K_types (work);
  1050. if (work -> btypevec != NULL)
  1051. {
  1052. free ((char *) work -> btypevec);
  1053. work->btypevec = NULL;
  1054. }
  1055. if (work -> ktypevec != NULL)
  1056. {
  1057. free ((char *) work -> ktypevec);
  1058. work->ktypevec = NULL;
  1059. }
  1060. }
  1061. /* Copy the work state and storage. */
  1062. static void
  1063. work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from)
  1064. {
  1065. int i;
  1066. delete_work_stuff (to);
  1067. /* Shallow-copy scalars. */
  1068. memcpy (to, from, sizeof (*to));
  1069. /* Deep-copy dynamic storage. */
  1070. if (from->typevec_size)
  1071. to->typevec = XNEWVEC (char *, from->typevec_size);
  1072. for (i = 0; i < from->ntypes; i++)
  1073. {
  1074. int len = strlen (from->typevec[i]) + 1;
  1075. to->typevec[i] = XNEWVEC (char, len);
  1076. memcpy (to->typevec[i], from->typevec[i], len);
  1077. }
  1078. if (from->ksize)
  1079. to->ktypevec = XNEWVEC (char *, from->ksize);
  1080. for (i = 0; i < from->numk; i++)
  1081. {
  1082. int len = strlen (from->ktypevec[i]) + 1;
  1083. to->ktypevec[i] = XNEWVEC (char, len);
  1084. memcpy (to->ktypevec[i], from->ktypevec[i], len);
  1085. }
  1086. if (from->bsize)
  1087. to->btypevec = XNEWVEC (char *, from->bsize);
  1088. for (i = 0; i < from->numb; i++)
  1089. {
  1090. int len = strlen (from->btypevec[i]) + 1;
  1091. to->btypevec[i] = XNEWVEC (char , len);
  1092. memcpy (to->btypevec[i], from->btypevec[i], len);
  1093. }
  1094. if (from->ntmpl_args)
  1095. to->tmpl_argvec = XNEWVEC (char *, from->ntmpl_args);
  1096. for (i = 0; i < from->ntmpl_args; i++)
  1097. {
  1098. int len = strlen (from->tmpl_argvec[i]) + 1;
  1099. to->tmpl_argvec[i] = XNEWVEC (char, len);
  1100. memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
  1101. }
  1102. if (from->previous_argument)
  1103. {
  1104. to->previous_argument = XNEW (string);
  1105. string_init (to->previous_argument);
  1106. string_appends (to->previous_argument, from->previous_argument);
  1107. }
  1108. }
  1109. /* Delete dynamic stuff in work_stuff that is not to be re-used. */
  1110. static void
  1111. delete_non_B_K_work_stuff (struct work_stuff *work)
  1112. {
  1113. /* Discard the remembered types, if any. */
  1114. forget_types (work);
  1115. if (work -> typevec != NULL)
  1116. {
  1117. free ((char *) work -> typevec);
  1118. work -> typevec = NULL;
  1119. work -> typevec_size = 0;
  1120. }
  1121. if (work->tmpl_argvec)
  1122. {
  1123. int i;
  1124. for (i = 0; i < work->ntmpl_args; i++)
  1125. free ((char*) work->tmpl_argvec[i]);
  1126. free ((char*) work->tmpl_argvec);
  1127. work->tmpl_argvec = NULL;
  1128. }
  1129. if (work->previous_argument)
  1130. {
  1131. string_delete (work->previous_argument);
  1132. free ((char*) work->previous_argument);
  1133. work->previous_argument = NULL;
  1134. }
  1135. }
  1136. /* Delete all dynamic storage in work_stuff. */
  1137. static void
  1138. delete_work_stuff (struct work_stuff *work)
  1139. {
  1140. delete_non_B_K_work_stuff (work);
  1141. squangle_mop_up (work);
  1142. }
  1143. /* Clear out any mangled storage */
  1144. static char *
  1145. mop_up (struct work_stuff *work, string *declp, int success)
  1146. {
  1147. char *demangled = NULL;
  1148. delete_non_B_K_work_stuff (work);
  1149. /* If demangling was successful, ensure that the demangled string is null
  1150. terminated and return it. Otherwise, free the demangling decl. */
  1151. if (!success)
  1152. {
  1153. string_delete (declp);
  1154. }
  1155. else
  1156. {
  1157. string_appendn (declp, "", 1);
  1158. demangled = declp->b;
  1159. }
  1160. return (demangled);
  1161. }
  1162. /*
  1163. LOCAL FUNCTION
  1164. demangle_signature -- demangle the signature part of a mangled name
  1165. SYNOPSIS
  1166. static int
  1167. demangle_signature (struct work_stuff *work, const char **mangled,
  1168. string *declp);
  1169. DESCRIPTION
  1170. Consume and demangle the signature portion of the mangled name.
  1171. DECLP is the string where demangled output is being built. At
  1172. entry it contains the demangled root name from the mangled name
  1173. prefix. I.E. either a demangled operator name or the root function
  1174. name. In some special cases, it may contain nothing.
  1175. *MANGLED points to the current unconsumed location in the mangled
  1176. name. As tokens are consumed and demangling is performed, the
  1177. pointer is updated to continuously point at the next token to
  1178. be consumed.
  1179. Demangling GNU style mangled names is nasty because there is no
  1180. explicit token that marks the start of the outermost function
  1181. argument list. */
  1182. static int
  1183. demangle_signature (struct work_stuff *work,
  1184. const char **mangled, string *declp)
  1185. {
  1186. int success = 1;
  1187. int func_done = 0;
  1188. int expect_func = 0;
  1189. int expect_return_type = 0;
  1190. const char *oldmangled = NULL;
  1191. string trawname;
  1192. string tname;
  1193. while (success && (**mangled != '\0'))
  1194. {
  1195. switch (**mangled)
  1196. {
  1197. case 'Q':
  1198. oldmangled = *mangled;
  1199. success = demangle_qualified (work, mangled, declp, 1, 0);
  1200. if (success)
  1201. remember_type (work, oldmangled, *mangled - oldmangled);
  1202. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1203. expect_func = 1;
  1204. oldmangled = NULL;
  1205. break;
  1206. case 'K':
  1207. oldmangled = *mangled;
  1208. success = demangle_qualified (work, mangled, declp, 1, 0);
  1209. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1210. {
  1211. expect_func = 1;
  1212. }
  1213. oldmangled = NULL;
  1214. break;
  1215. case 'S':
  1216. /* Static member function */
  1217. if (oldmangled == NULL)
  1218. {
  1219. oldmangled = *mangled;
  1220. }
  1221. (*mangled)++;
  1222. work -> static_type = 1;
  1223. break;
  1224. case 'C':
  1225. case 'V':
  1226. case 'u':
  1227. work->type_quals |= code_for_qualifier (**mangled);
  1228. /* a qualified member function */
  1229. if (oldmangled == NULL)
  1230. oldmangled = *mangled;
  1231. (*mangled)++;
  1232. break;
  1233. case 'L':
  1234. /* Local class name follows after "Lnnn_" */
  1235. if (HP_DEMANGLING)
  1236. {
  1237. while (**mangled && (**mangled != '_'))
  1238. (*mangled)++;
  1239. if (!**mangled)
  1240. success = 0;
  1241. else
  1242. (*mangled)++;
  1243. }
  1244. else
  1245. success = 0;
  1246. break;
  1247. case '0': case '1': case '2': case '3': case '4':
  1248. case '5': case '6': case '7': case '8': case '9':
  1249. if (oldmangled == NULL)
  1250. {
  1251. oldmangled = *mangled;
  1252. }
  1253. work->temp_start = -1; /* uppermost call to demangle_class */
  1254. success = demangle_class (work, mangled, declp);
  1255. if (success)
  1256. {
  1257. remember_type (work, oldmangled, *mangled - oldmangled);
  1258. }
  1259. if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
  1260. {
  1261. /* EDG and others will have the "F", so we let the loop cycle
  1262. if we are looking at one. */
  1263. if (**mangled != 'F')
  1264. expect_func = 1;
  1265. }
  1266. oldmangled = NULL;
  1267. break;
  1268. case 'B':
  1269. {
  1270. string s;
  1271. success = do_type (work, mangled, &s);
  1272. if (success)
  1273. {
  1274. string_append (&s, SCOPE_STRING (work));
  1275. string_prepends (declp, &s);
  1276. string_delete (&s);
  1277. }
  1278. oldmangled = NULL;
  1279. expect_func = 1;
  1280. }
  1281. break;
  1282. case 'F':
  1283. /* Function */
  1284. /* ARM/HP style demangling includes a specific 'F' character after
  1285. the class name. For GNU style, it is just implied. So we can
  1286. safely just consume any 'F' at this point and be compatible
  1287. with either style. */
  1288. oldmangled = NULL;
  1289. func_done = 1;
  1290. (*mangled)++;
  1291. /* For lucid/ARM/HP style we have to forget any types we might
  1292. have remembered up to this point, since they were not argument
  1293. types. GNU style considers all types seen as available for
  1294. back references. See comment in demangle_args() */
  1295. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  1296. {
  1297. forget_types (work);
  1298. }
  1299. success = demangle_args (work, mangled, declp);
  1300. /* After picking off the function args, we expect to either
  1301. find the function return type (preceded by an '_') or the
  1302. end of the string. */
  1303. if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
  1304. {
  1305. ++(*mangled);
  1306. /* At this level, we do not care about the return type. */
  1307. success = do_type (work, mangled, &tname);
  1308. string_delete (&tname);
  1309. }
  1310. break;
  1311. case 't':
  1312. /* G++ Template */
  1313. string_init(&trawname);
  1314. string_init(&tname);
  1315. if (oldmangled == NULL)
  1316. {
  1317. oldmangled = *mangled;
  1318. }
  1319. success = demangle_template (work, mangled, &tname,
  1320. &trawname, 1, 1);
  1321. if (success)
  1322. {
  1323. remember_type (work, oldmangled, *mangled - oldmangled);
  1324. }
  1325. string_append (&tname, SCOPE_STRING (work));
  1326. string_prepends(declp, &tname);
  1327. if (work -> destructor & 1)
  1328. {
  1329. string_prepend (&trawname, "~");
  1330. string_appends (declp, &trawname);
  1331. work->destructor -= 1;
  1332. }
  1333. if ((work->constructor & 1) || (work->destructor & 1))
  1334. {
  1335. string_appends (declp, &trawname);
  1336. work->constructor -= 1;
  1337. }
  1338. string_delete(&trawname);
  1339. string_delete(&tname);
  1340. oldmangled = NULL;
  1341. expect_func = 1;
  1342. break;
  1343. case '_':
  1344. if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
  1345. {
  1346. /* Read the return type. */
  1347. string return_type;
  1348. (*mangled)++;
  1349. success = do_type (work, mangled, &return_type);
  1350. APPEND_BLANK (&return_type);
  1351. string_prepends (declp, &return_type);
  1352. string_delete (&return_type);
  1353. break;
  1354. }
  1355. else
  1356. /* At the outermost level, we cannot have a return type specified,
  1357. so if we run into another '_' at this point we are dealing with
  1358. a mangled name that is either bogus, or has been mangled by
  1359. some algorithm we don't know how to deal with. So just
  1360. reject the entire demangling. */
  1361. /* However, "_nnn" is an expected suffix for alternate entry point
  1362. numbered nnn for a function, with HP aCC, so skip over that
  1363. without reporting failure. pai/1997-09-04 */
  1364. if (HP_DEMANGLING)
  1365. {
  1366. (*mangled)++;
  1367. while (**mangled && ISDIGIT ((unsigned char)**mangled))
  1368. (*mangled)++;
  1369. }
  1370. else
  1371. success = 0;
  1372. break;
  1373. case 'H':
  1374. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1375. {
  1376. /* A G++ template function. Read the template arguments. */
  1377. success = demangle_template (work, mangled, declp, 0, 0,
  1378. 0);
  1379. if (!(work->constructor & 1))
  1380. expect_return_type = 1;
  1381. (*mangled)++;
  1382. break;
  1383. }
  1384. else
  1385. /* fall through */
  1386. {;}
  1387. default:
  1388. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1389. {
  1390. /* Assume we have stumbled onto the first outermost function
  1391. argument token, and start processing args. */
  1392. func_done = 1;
  1393. success = demangle_args (work, mangled, declp);
  1394. }
  1395. else
  1396. {
  1397. /* Non-GNU demanglers use a specific token to mark the start
  1398. of the outermost function argument tokens. Typically 'F',
  1399. for ARM/HP-demangling, for example. So if we find something
  1400. we are not prepared for, it must be an error. */
  1401. success = 0;
  1402. }
  1403. break;
  1404. }
  1405. /*
  1406. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1407. */
  1408. {
  1409. if (success && expect_func)
  1410. {
  1411. func_done = 1;
  1412. if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
  1413. {
  1414. forget_types (work);
  1415. }
  1416. success = demangle_args (work, mangled, declp);
  1417. /* Since template include the mangling of their return types,
  1418. we must set expect_func to 0 so that we don't try do
  1419. demangle more arguments the next time we get here. */
  1420. expect_func = 0;
  1421. }
  1422. }
  1423. }
  1424. if (success && !func_done)
  1425. {
  1426. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1427. {
  1428. /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
  1429. bar__3fooi is 'foo::bar(int)'. We get here when we find the
  1430. first case, and need to ensure that the '(void)' gets added to
  1431. the current declp. Note that with ARM/HP, the first case
  1432. represents the name of a static data member 'foo::bar',
  1433. which is in the current declp, so we leave it alone. */
  1434. success = demangle_args (work, mangled, declp);
  1435. }
  1436. }
  1437. if (success && PRINT_ARG_TYPES)
  1438. {
  1439. if (work->static_type)
  1440. string_append (declp, " static");
  1441. if (work->type_quals != TYPE_UNQUALIFIED)
  1442. {
  1443. APPEND_BLANK (declp);
  1444. string_append (declp, qualifier_string (work->type_quals));
  1445. }
  1446. }
  1447. return (success);
  1448. }
  1449. #if 0
  1450. static int
  1451. demangle_method_args (struct work_stuff *work, const char **mangled,
  1452. string *declp)
  1453. {
  1454. int success = 0;
  1455. if (work -> static_type)
  1456. {
  1457. string_append (declp, *mangled + 1);
  1458. *mangled += strlen (*mangled);
  1459. success = 1;
  1460. }
  1461. else
  1462. {
  1463. success = demangle_args (work, mangled, declp);
  1464. }
  1465. return (success);
  1466. }
  1467. #endif
  1468. static int
  1469. demangle_template_template_parm (struct work_stuff *work,
  1470. const char **mangled, string *tname)
  1471. {
  1472. int i;
  1473. int r;
  1474. int need_comma = 0;
  1475. int success = 1;
  1476. string temp;
  1477. string_append (tname, "template <");
  1478. /* get size of template parameter list */
  1479. if (get_count (mangled, &r))
  1480. {
  1481. for (i = 0; i < r; i++)
  1482. {
  1483. if (need_comma)
  1484. {
  1485. string_append (tname, ", ");
  1486. }
  1487. /* Z for type parameters */
  1488. if (**mangled == 'Z')
  1489. {
  1490. (*mangled)++;
  1491. string_append (tname, "class");
  1492. }
  1493. /* z for template parameters */
  1494. else if (**mangled == 'z')
  1495. {
  1496. (*mangled)++;
  1497. success =
  1498. demangle_template_template_parm (work, mangled, tname);
  1499. if (!success)
  1500. {
  1501. break;
  1502. }
  1503. }
  1504. else
  1505. {
  1506. /* temp is initialized in do_type */
  1507. success = do_type (work, mangled, &temp);
  1508. if (success)
  1509. {
  1510. string_appends (tname, &temp);
  1511. }
  1512. string_delete(&temp);
  1513. if (!success)
  1514. {
  1515. break;
  1516. }
  1517. }
  1518. need_comma = 1;
  1519. }
  1520. }
  1521. if (tname->p[-1] == '>')
  1522. string_append (tname, " ");
  1523. string_append (tname, "> class");
  1524. return (success);
  1525. }
  1526. static int
  1527. demangle_expression (struct work_stuff *work, const char **mangled,
  1528. string *s, type_kind_t tk)
  1529. {
  1530. int need_operator = 0;
  1531. int success;
  1532. success = 1;
  1533. string_appendn (s, "(", 1);
  1534. (*mangled)++;
  1535. while (success && **mangled != 'W' && **mangled != '\0')
  1536. {
  1537. if (need_operator)
  1538. {
  1539. size_t i;
  1540. size_t len;
  1541. success = 0;
  1542. len = strlen (*mangled);
  1543. for (i = 0; i < ARRAY_SIZE (optable); ++i)
  1544. {
  1545. size_t l = strlen (optable[i].in);
  1546. if (l <= len
  1547. && memcmp (optable[i].in, *mangled, l) == 0)
  1548. {
  1549. string_appendn (s, " ", 1);
  1550. string_append (s, optable[i].out);
  1551. string_appendn (s, " ", 1);
  1552. success = 1;
  1553. (*mangled) += l;
  1554. break;
  1555. }
  1556. }
  1557. if (!success)
  1558. break;
  1559. }
  1560. else
  1561. need_operator = 1;
  1562. success = demangle_template_value_parm (work, mangled, s, tk);
  1563. }
  1564. if (**mangled != 'W')
  1565. success = 0;
  1566. else
  1567. {
  1568. string_appendn (s, ")", 1);
  1569. (*mangled)++;
  1570. }
  1571. return success;
  1572. }
  1573. static int
  1574. demangle_integral_value (struct work_stuff *work,
  1575. const char **mangled, string *s)
  1576. {
  1577. int success;
  1578. if (**mangled == 'E')
  1579. success = demangle_expression (work, mangled, s, tk_integral);
  1580. else if (**mangled == 'Q' || **mangled == 'K')
  1581. success = demangle_qualified (work, mangled, s, 0, 1);
  1582. else
  1583. {
  1584. int value;
  1585. /* By default, we let the number decide whether we shall consume an
  1586. underscore. */
  1587. int multidigit_without_leading_underscore = 0;
  1588. int leave_following_underscore = 0;
  1589. success = 0;
  1590. if (**mangled == '_')
  1591. {
  1592. if (mangled[0][1] == 'm')
  1593. {
  1594. /* Since consume_count_with_underscores does not handle the
  1595. `m'-prefix we must do it here, using consume_count and
  1596. adjusting underscores: we have to consume the underscore
  1597. matching the prepended one. */
  1598. multidigit_without_leading_underscore = 1;
  1599. string_appendn (s, "-", 1);
  1600. (*mangled) += 2;
  1601. }
  1602. else
  1603. {
  1604. /* Do not consume a following underscore;
  1605. consume_count_with_underscores will consume what
  1606. should be consumed. */
  1607. leave_following_underscore = 1;
  1608. }
  1609. }
  1610. else
  1611. {
  1612. /* Negative numbers are indicated with a leading `m'. */
  1613. if (**mangled == 'm')
  1614. {
  1615. string_appendn (s, "-", 1);
  1616. (*mangled)++;
  1617. }
  1618. /* Since consume_count_with_underscores does not handle
  1619. multi-digit numbers that do not start with an underscore,
  1620. and this number can be an integer template parameter,
  1621. we have to call consume_count. */
  1622. multidigit_without_leading_underscore = 1;
  1623. /* These multi-digit numbers never end on an underscore,
  1624. so if there is one then don't eat it. */
  1625. leave_following_underscore = 1;
  1626. }
  1627. /* We must call consume_count if we expect to remove a trailing
  1628. underscore, since consume_count_with_underscores expects
  1629. the leading underscore (that we consumed) if it is to handle
  1630. multi-digit numbers. */
  1631. if (multidigit_without_leading_underscore)
  1632. value = consume_count (mangled);
  1633. else
  1634. value = consume_count_with_underscores (mangled);
  1635. if (value != -1)
  1636. {
  1637. char buf[INTBUF_SIZE];
  1638. sprintf (buf, "%d", value);
  1639. string_append (s, buf);
  1640. /* Numbers not otherwise delimited, might have an underscore
  1641. appended as a delimeter, which we should skip.
  1642. ??? This used to always remove a following underscore, which
  1643. is wrong. If other (arbitrary) cases are followed by an
  1644. underscore, we need to do something more radical. */
  1645. if ((value > 9 || multidigit_without_leading_underscore)
  1646. && ! leave_following_underscore
  1647. && **mangled == '_')
  1648. (*mangled)++;
  1649. /* All is well. */
  1650. success = 1;
  1651. }
  1652. }
  1653. return success;
  1654. }
  1655. /* Demangle the real value in MANGLED. */
  1656. static int
  1657. demangle_real_value (struct work_stuff *work,
  1658. const char **mangled, string *s)
  1659. {
  1660. if (**mangled == 'E')
  1661. return demangle_expression (work, mangled, s, tk_real);
  1662. if (**mangled == 'm')
  1663. {
  1664. string_appendn (s, "-", 1);
  1665. (*mangled)++;
  1666. }
  1667. while (ISDIGIT ((unsigned char)**mangled))
  1668. {
  1669. string_appendn (s, *mangled, 1);
  1670. (*mangled)++;
  1671. }
  1672. if (**mangled == '.') /* fraction */
  1673. {
  1674. string_appendn (s, ".", 1);
  1675. (*mangled)++;
  1676. while (ISDIGIT ((unsigned char)**mangled))
  1677. {
  1678. string_appendn (s, *mangled, 1);
  1679. (*mangled)++;
  1680. }
  1681. }
  1682. if (**mangled == 'e') /* exponent */
  1683. {
  1684. string_appendn (s, "e", 1);
  1685. (*mangled)++;
  1686. while (ISDIGIT ((unsigned char)**mangled))
  1687. {
  1688. string_appendn (s, *mangled, 1);
  1689. (*mangled)++;
  1690. }
  1691. }
  1692. return 1;
  1693. }
  1694. static int
  1695. demangle_template_value_parm (struct work_stuff *work, const char **mangled,
  1696. string *s, type_kind_t tk)
  1697. {
  1698. int success = 1;
  1699. if (**mangled == 'Y')
  1700. {
  1701. /* The next argument is a template parameter. */
  1702. int idx;
  1703. (*mangled)++;
  1704. idx = consume_count_with_underscores (mangled);
  1705. if (idx == -1
  1706. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  1707. || consume_count_with_underscores (mangled) == -1)
  1708. return -1;
  1709. if (work->tmpl_argvec)
  1710. string_append (s, work->tmpl_argvec[idx]);
  1711. else
  1712. string_append_template_idx (s, idx);
  1713. }
  1714. else if (tk == tk_integral)
  1715. success = demangle_integral_value (work, mangled, s);
  1716. else if (tk == tk_char)
  1717. {
  1718. char tmp[2];
  1719. int val;
  1720. if (**mangled == 'm')
  1721. {
  1722. string_appendn (s, "-", 1);
  1723. (*mangled)++;
  1724. }
  1725. string_appendn (s, "'", 1);
  1726. val = consume_count(mangled);
  1727. if (val <= 0)
  1728. success = 0;
  1729. else
  1730. {
  1731. tmp[0] = (char)val;
  1732. tmp[1] = '\0';
  1733. string_appendn (s, &tmp[0], 1);
  1734. string_appendn (s, "'", 1);
  1735. }
  1736. }
  1737. else if (tk == tk_bool)
  1738. {
  1739. int val = consume_count (mangled);
  1740. if (val == 0)
  1741. string_appendn (s, "false", 5);
  1742. else if (val == 1)
  1743. string_appendn (s, "true", 4);
  1744. else
  1745. success = 0;
  1746. }
  1747. else if (tk == tk_real)
  1748. success = demangle_real_value (work, mangled, s);
  1749. else if (tk == tk_pointer || tk == tk_reference)
  1750. {
  1751. if (**mangled == 'Q')
  1752. success = demangle_qualified (work, mangled, s,
  1753. /*isfuncname=*/0,
  1754. /*append=*/1);
  1755. else
  1756. {
  1757. int symbol_len = consume_count (mangled);
  1758. if (symbol_len == -1)
  1759. return -1;
  1760. if (symbol_len == 0)
  1761. string_appendn (s, "0", 1);
  1762. else
  1763. {
  1764. char *p = XNEWVEC (char, symbol_len + 1), *q;
  1765. strncpy (p, *mangled, symbol_len);
  1766. p [symbol_len] = '\0';
  1767. /* We use cplus_demangle here, rather than
  1768. internal_cplus_demangle, because the name of the entity
  1769. mangled here does not make use of any of the squangling
  1770. or type-code information we have built up thus far; it is
  1771. mangled independently. */
  1772. q = cplus_demangle (p, work->options);
  1773. if (tk == tk_pointer)
  1774. string_appendn (s, "&", 1);
  1775. /* FIXME: Pointer-to-member constants should get a
  1776. qualifying class name here. */
  1777. if (q)
  1778. {
  1779. string_append (s, q);
  1780. free (q);
  1781. }
  1782. else
  1783. string_append (s, p);
  1784. free (p);
  1785. }
  1786. *mangled += symbol_len;
  1787. }
  1788. }
  1789. return success;
  1790. }
  1791. /* Demangle the template name in MANGLED. The full name of the
  1792. template (e.g., S<int>) is placed in TNAME. The name without the
  1793. template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
  1794. non-NULL. If IS_TYPE is nonzero, this template is a type template,
  1795. not a function template. If both IS_TYPE and REMEMBER are nonzero,
  1796. the template is remembered in the list of back-referenceable
  1797. types. */
  1798. static int
  1799. demangle_template (struct work_stuff *work, const char **mangled,
  1800. string *tname, string *trawname,
  1801. int is_type, int remember)
  1802. {
  1803. int i;
  1804. int r;
  1805. int need_comma = 0;
  1806. int success = 0;
  1807. int is_java_array = 0;
  1808. string temp;
  1809. (*mangled)++;
  1810. if (is_type)
  1811. {
  1812. /* get template name */
  1813. if (**mangled == 'z')
  1814. {
  1815. int idx;
  1816. (*mangled)++;
  1817. (*mangled)++;
  1818. idx = consume_count_with_underscores (mangled);
  1819. if (idx == -1
  1820. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  1821. || consume_count_with_underscores (mangled) == -1)
  1822. return (0);
  1823. if (work->tmpl_argvec)
  1824. {
  1825. string_append (tname, work->tmpl_argvec[idx]);
  1826. if (trawname)
  1827. string_append (trawname, work->tmpl_argvec[idx]);
  1828. }
  1829. else
  1830. {
  1831. string_append_template_idx (tname, idx);
  1832. if (trawname)
  1833. string_append_template_idx (trawname, idx);
  1834. }
  1835. }
  1836. else
  1837. {
  1838. if ((r = consume_count (mangled)) <= 0
  1839. || (int) strlen (*mangled) < r)
  1840. {
  1841. return (0);
  1842. }
  1843. is_java_array = (work -> options & DMGL_JAVA)
  1844. && strncmp (*mangled, "JArray1Z", 8) == 0;
  1845. if (! is_java_array)
  1846. {
  1847. string_appendn (tname, *mangled, r);
  1848. }
  1849. if (trawname)
  1850. string_appendn (trawname, *mangled, r);
  1851. *mangled += r;
  1852. }
  1853. }
  1854. if (!is_java_array)
  1855. string_append (tname, "<");
  1856. /* get size of template parameter list */
  1857. if (!get_count (mangled, &r))
  1858. {
  1859. return (0);
  1860. }
  1861. if (!is_type)
  1862. {
  1863. /* Create an array for saving the template argument values. */
  1864. work->tmpl_argvec = XNEWVEC (char *, r);
  1865. work->ntmpl_args = r;
  1866. for (i = 0; i < r; i++)
  1867. work->tmpl_argvec[i] = 0;
  1868. }
  1869. for (i = 0; i < r; i++)
  1870. {
  1871. if (need_comma)
  1872. {
  1873. string_append (tname, ", ");
  1874. }
  1875. /* Z for type parameters */
  1876. if (**mangled == 'Z')
  1877. {
  1878. (*mangled)++;
  1879. /* temp is initialized in do_type */
  1880. success = do_type (work, mangled, &temp);
  1881. if (success)
  1882. {
  1883. string_appends (tname, &temp);
  1884. if (!is_type)
  1885. {
  1886. /* Save the template argument. */
  1887. int len = temp.p - temp.b;
  1888. work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
  1889. memcpy (work->tmpl_argvec[i], temp.b, len);
  1890. work->tmpl_argvec[i][len] = '\0';
  1891. }
  1892. }
  1893. string_delete(&temp);
  1894. if (!success)
  1895. {
  1896. break;
  1897. }
  1898. }
  1899. /* z for template parameters */
  1900. else if (**mangled == 'z')
  1901. {
  1902. int r2;
  1903. (*mangled)++;
  1904. success = demangle_template_template_parm (work, mangled, tname);
  1905. if (success
  1906. && (r2 = consume_count (mangled)) > 0
  1907. && (int) strlen (*mangled) >= r2)
  1908. {
  1909. string_append (tname, " ");
  1910. string_appendn (tname, *mangled, r2);
  1911. if (!is_type)
  1912. {
  1913. /* Save the template argument. */
  1914. int len = r2;
  1915. work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
  1916. memcpy (work->tmpl_argvec[i], *mangled, len);
  1917. work->tmpl_argvec[i][len] = '\0';
  1918. }
  1919. *mangled += r2;
  1920. }
  1921. if (!success)
  1922. {
  1923. break;
  1924. }
  1925. }
  1926. else
  1927. {
  1928. string param;
  1929. string* s;
  1930. /* otherwise, value parameter */
  1931. /* temp is initialized in do_type */
  1932. success = do_type (work, mangled, &temp);
  1933. string_delete(&temp);
  1934. if (!success)
  1935. break;
  1936. if (!is_type)
  1937. {
  1938. s = &param;
  1939. string_init (s);
  1940. }
  1941. else
  1942. s = tname;
  1943. success = demangle_template_value_parm (work, mangled, s,
  1944. (type_kind_t) success);
  1945. if (!success)
  1946. {
  1947. if (!is_type)
  1948. string_delete (s);
  1949. success = 0;
  1950. break;
  1951. }
  1952. if (!is_type)
  1953. {
  1954. int len = s->p - s->b;
  1955. work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
  1956. memcpy (work->tmpl_argvec[i], s->b, len);
  1957. work->tmpl_argvec[i][len] = '\0';
  1958. string_appends (tname, s);
  1959. string_delete (s);
  1960. }
  1961. }
  1962. need_comma = 1;
  1963. }
  1964. if (is_java_array)
  1965. {
  1966. string_append (tname, "[]");
  1967. }
  1968. else
  1969. {
  1970. if (tname->p[-1] == '>')
  1971. string_append (tname, " ");
  1972. string_append (tname, ">");
  1973. }
  1974. if (is_type && remember)
  1975. {
  1976. const int bindex = register_Btype (work);
  1977. remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
  1978. }
  1979. /*
  1980. if (work -> static_type)
  1981. {
  1982. string_append (declp, *mangled + 1);
  1983. *mangled += strlen (*mangled);
  1984. success = 1;
  1985. }
  1986. else
  1987. {
  1988. success = demangle_args (work, mangled, declp);
  1989. }
  1990. }
  1991. */
  1992. return (success);
  1993. }
  1994. static int
  1995. arm_pt (struct work_stuff *work, const char *mangled,
  1996. int n, const char **anchor, const char **args)
  1997. {
  1998. /* Check if ARM template with "__pt__" in it ("parameterized type") */
  1999. /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
  2000. if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
  2001. {
  2002. int len;
  2003. *args = *anchor + 6;
  2004. len = consume_count (args);
  2005. if (len == -1)
  2006. return 0;
  2007. if (*args + len == mangled + n && **args == '_')
  2008. {
  2009. ++*args;
  2010. return 1;
  2011. }
  2012. }
  2013. if (AUTO_DEMANGLING || EDG_DEMANGLING)
  2014. {
  2015. if ((*anchor = strstr (mangled, "__tm__"))
  2016. || (*anchor = strstr (mangled, "__ps__"))
  2017. || (*anchor = strstr (mangled, "__pt__")))
  2018. {
  2019. int len;
  2020. *args = *anchor + 6;
  2021. len = consume_count (args);
  2022. if (len == -1)
  2023. return 0;
  2024. if (*args + len == mangled + n && **args == '_')
  2025. {
  2026. ++*args;
  2027. return 1;
  2028. }
  2029. }
  2030. else if ((*anchor = strstr (mangled, "__S")))
  2031. {
  2032. int len;
  2033. *args = *anchor + 3;
  2034. len = consume_count (args);
  2035. if (len == -1)
  2036. return 0;
  2037. if (*args + len == mangled + n && **args == '_')
  2038. {
  2039. ++*args;
  2040. return 1;
  2041. }
  2042. }
  2043. }
  2044. return 0;
  2045. }
  2046. static void
  2047. demangle_arm_hp_template (struct work_stuff *work, const char **mangled,
  2048. int n, string *declp)
  2049. {
  2050. const char *p;
  2051. const char *args;
  2052. const char *e = *mangled + n;
  2053. string arg;
  2054. /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
  2055. template args */
  2056. if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
  2057. {
  2058. char *start_spec_args = NULL;
  2059. int hold_options;
  2060. /* First check for and omit template specialization pseudo-arguments,
  2061. such as in "Spec<#1,#1.*>" */
  2062. start_spec_args = strchr (*mangled, '<');
  2063. if (start_spec_args && (start_spec_args - *mangled < n))
  2064. string_appendn (declp, *mangled, start_spec_args - *mangled);
  2065. else
  2066. string_appendn (declp, *mangled, n);
  2067. (*mangled) += n + 1;
  2068. string_init (&arg);
  2069. if (work->temp_start == -1) /* non-recursive call */
  2070. work->temp_start = declp->p - declp->b;
  2071. /* We want to unconditionally demangle parameter types in
  2072. template parameters. */
  2073. hold_options = work->options;
  2074. work->options |= DMGL_PARAMS;
  2075. string_append (declp, "<");
  2076. while (1)
  2077. {
  2078. string_delete (&arg);
  2079. switch (**mangled)
  2080. {
  2081. case 'T':
  2082. /* 'T' signals a type parameter */
  2083. (*mangled)++;
  2084. if (!do_type (work, mangled, &arg))
  2085. goto hpacc_template_args_done;
  2086. break;
  2087. case 'U':
  2088. case 'S':
  2089. /* 'U' or 'S' signals an integral value */
  2090. if (!do_hpacc_template_const_value (work, mangled, &arg))
  2091. goto hpacc_template_args_done;
  2092. break;
  2093. case 'A':
  2094. /* 'A' signals a named constant expression (literal) */
  2095. if (!do_hpacc_template_literal (work, mangled, &arg))
  2096. goto hpacc_template_args_done;
  2097. break;
  2098. default:
  2099. /* Today, 1997-09-03, we have only the above types
  2100. of template parameters */
  2101. /* FIXME: maybe this should fail and return null */
  2102. goto hpacc_template_args_done;
  2103. }
  2104. string_appends (declp, &arg);
  2105. /* Check if we're at the end of template args.
  2106. 0 if at end of static member of template class,
  2107. _ if done with template args for a function */
  2108. if ((**mangled == '\000') || (**mangled == '_'))
  2109. break;
  2110. else
  2111. string_append (declp, ",");
  2112. }
  2113. hpacc_template_args_done:
  2114. string_append (declp, ">");
  2115. string_delete (&arg);
  2116. if (**mangled == '_')
  2117. (*mangled)++;
  2118. work->options = hold_options;
  2119. return;
  2120. }
  2121. /* ARM template? (Also handles HP cfront extensions) */
  2122. else if (arm_pt (work, *mangled, n, &p, &args))
  2123. {
  2124. int hold_options;
  2125. string type_str;
  2126. string_init (&arg);
  2127. string_appendn (declp, *mangled, p - *mangled);
  2128. if (work->temp_start == -1) /* non-recursive call */
  2129. work->temp_start = declp->p - declp->b;
  2130. /* We want to unconditionally demangle parameter types in
  2131. template parameters. */
  2132. hold_options = work->options;
  2133. work->options |= DMGL_PARAMS;
  2134. string_append (declp, "<");
  2135. /* should do error checking here */
  2136. while (args < e) {
  2137. string_delete (&arg);
  2138. /* Check for type or literal here */
  2139. switch (*args)
  2140. {
  2141. /* HP cfront extensions to ARM for template args */
  2142. /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
  2143. /* FIXME: We handle only numeric literals for HP cfront */
  2144. case 'X':
  2145. /* A typed constant value follows */
  2146. args++;
  2147. if (!do_type (work, &args, &type_str))
  2148. goto cfront_template_args_done;
  2149. string_append (&arg, "(");
  2150. string_appends (&arg, &type_str);
  2151. string_delete (&type_str);
  2152. string_append (&arg, ")");
  2153. if (*args != 'L')
  2154. goto cfront_template_args_done;
  2155. args++;
  2156. /* Now snarf a literal value following 'L' */
  2157. if (!snarf_numeric_literal (&args, &arg))
  2158. goto cfront_template_args_done;
  2159. break;
  2160. case 'L':
  2161. /* Snarf a literal following 'L' */
  2162. args++;
  2163. if (!snarf_numeric_literal (&args, &arg))
  2164. goto cfront_template_args_done;
  2165. break;
  2166. default:
  2167. /* Not handling other HP cfront stuff */
  2168. {
  2169. const char* old_args = args;
  2170. if (!do_type (work, &args, &arg))
  2171. goto cfront_template_args_done;
  2172. /* Fail if we didn't make any progress: prevent infinite loop. */
  2173. if (args == old_args)
  2174. {
  2175. work->options = hold_options;
  2176. return;
  2177. }
  2178. }
  2179. }
  2180. string_appends (declp, &arg);
  2181. string_append (declp, ",");
  2182. }
  2183. cfront_template_args_done:
  2184. string_delete (&arg);
  2185. if (args >= e)
  2186. --declp->p; /* remove extra comma */
  2187. string_append (declp, ">");
  2188. work->options = hold_options;
  2189. }
  2190. else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
  2191. && (*mangled)[9] == 'N'
  2192. && (*mangled)[8] == (*mangled)[10]
  2193. && strchr (cplus_markers, (*mangled)[8]))
  2194. {
  2195. /* A member of the anonymous namespace. */
  2196. string_append (declp, "{anonymous}");
  2197. }
  2198. else
  2199. {
  2200. if (work->temp_start == -1) /* non-recursive call only */
  2201. work->temp_start = 0; /* disable in recursive calls */
  2202. string_appendn (declp, *mangled, n);
  2203. }
  2204. *mangled += n;
  2205. }
  2206. /* Extract a class name, possibly a template with arguments, from the
  2207. mangled string; qualifiers, local class indicators, etc. have
  2208. already been dealt with */
  2209. static int
  2210. demangle_class_name (struct work_stuff *work, const char **mangled,
  2211. string *declp)
  2212. {
  2213. int n;
  2214. int success = 0;
  2215. n = consume_count (mangled);
  2216. if (n == -1)
  2217. return 0;
  2218. if ((int) strlen (*mangled) >= n)
  2219. {
  2220. demangle_arm_hp_template (work, mangled, n, declp);
  2221. success = 1;
  2222. }
  2223. return (success);
  2224. }
  2225. /*
  2226. LOCAL FUNCTION
  2227. demangle_class -- demangle a mangled class sequence
  2228. SYNOPSIS
  2229. static int
  2230. demangle_class (struct work_stuff *work, const char **mangled,
  2231. strint *declp)
  2232. DESCRIPTION
  2233. DECLP points to the buffer into which demangling is being done.
  2234. *MANGLED points to the current token to be demangled. On input,
  2235. it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
  2236. On exit, it points to the next token after the mangled class on
  2237. success, or the first unconsumed token on failure.
  2238. If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
  2239. we are demangling a constructor or destructor. In this case
  2240. we prepend "class::class" or "class::~class" to DECLP.
  2241. Otherwise, we prepend "class::" to the current DECLP.
  2242. Reset the constructor/destructor flags once they have been
  2243. "consumed". This allows demangle_class to be called later during
  2244. the same demangling, to do normal class demangling.
  2245. Returns 1 if demangling is successful, 0 otherwise.
  2246. */
  2247. static int
  2248. demangle_class (struct work_stuff *work, const char **mangled, string *declp)
  2249. {
  2250. int success = 0;
  2251. int btype;
  2252. string class_name;
  2253. char *save_class_name_end = 0;
  2254. string_init (&class_name);
  2255. btype = register_Btype (work);
  2256. if (demangle_class_name (work, mangled, &class_name))
  2257. {
  2258. save_class_name_end = class_name.p;
  2259. if ((work->constructor & 1) || (work->destructor & 1))
  2260. {
  2261. /* adjust so we don't include template args */
  2262. if (work->temp_start && (work->temp_start != -1))
  2263. {
  2264. class_name.p = class_name.b + work->temp_start;
  2265. }
  2266. string_prepends (declp, &class_name);
  2267. if (work -> destructor & 1)
  2268. {
  2269. string_prepend (declp, "~");
  2270. work -> destructor -= 1;
  2271. }
  2272. else
  2273. {
  2274. work -> constructor -= 1;
  2275. }
  2276. }
  2277. class_name.p = save_class_name_end;
  2278. remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
  2279. remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
  2280. string_prepend (declp, SCOPE_STRING (work));
  2281. string_prepends (declp, &class_name);
  2282. success = 1;
  2283. }
  2284. string_delete (&class_name);
  2285. return (success);
  2286. }
  2287. /* Called when there's a "__" in the mangled name, with `scan' pointing to
  2288. the rightmost guess.
  2289. Find the correct "__"-sequence where the function name ends and the
  2290. signature starts, which is ambiguous with GNU mangling.
  2291. Call demangle_signature here, so we can make sure we found the right
  2292. one; *mangled will be consumed so caller will not make further calls to
  2293. demangle_signature. */
  2294. static int
  2295. iterate_demangle_function (struct work_stuff *work, const char **mangled,
  2296. string *declp, const char *scan)
  2297. {
  2298. const char *mangle_init = *mangled;
  2299. int success = 0;
  2300. string decl_init;
  2301. struct work_stuff work_init;
  2302. if (*(scan + 2) == '\0')
  2303. return 0;
  2304. /* Do not iterate for some demangling modes, or if there's only one
  2305. "__"-sequence. This is the normal case. */
  2306. if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
  2307. || strstr (scan + 2, "__") == NULL)
  2308. return demangle_function_name (work, mangled, declp, scan);
  2309. /* Save state so we can restart if the guess at the correct "__" was
  2310. wrong. */
  2311. string_init (&decl_init);
  2312. string_appends (&decl_init, declp);
  2313. memset (&work_init, 0, sizeof work_init);
  2314. work_stuff_copy_to_from (&work_init, work);
  2315. /* Iterate over occurrences of __, allowing names and types to have a
  2316. "__" sequence in them. We must start with the first (not the last)
  2317. occurrence, since "__" most often occur between independent mangled
  2318. parts, hence starting at the last occurence inside a signature
  2319. might get us a "successful" demangling of the signature. */
  2320. while (scan[2])
  2321. {
  2322. if (demangle_function_name (work, mangled, declp, scan))
  2323. {
  2324. success = demangle_signature (work, mangled, declp);
  2325. if (success)
  2326. break;
  2327. }
  2328. /* Reset demangle state for the next round. */
  2329. *mangled = mangle_init;
  2330. string_clear (declp);
  2331. string_appends (declp, &decl_init);
  2332. work_stuff_copy_to_from (work, &work_init);
  2333. /* Leave this underscore-sequence. */
  2334. scan += 2;
  2335. /* Scan for the next "__" sequence. */
  2336. while (*scan && (scan[0] != '_' || scan[1] != '_'))
  2337. scan++;
  2338. /* Move to last "__" in this sequence. */
  2339. while (*scan && *scan == '_')
  2340. scan++;
  2341. scan -= 2;
  2342. }
  2343. /* Delete saved state. */
  2344. delete_work_stuff (&work_init);
  2345. string_delete (&decl_init);
  2346. return success;
  2347. }
  2348. /*
  2349. LOCAL FUNCTION
  2350. demangle_prefix -- consume the mangled name prefix and find signature
  2351. SYNOPSIS
  2352. static int
  2353. demangle_prefix (struct work_stuff *work, const char **mangled,
  2354. string *declp);
  2355. DESCRIPTION
  2356. Consume and demangle the prefix of the mangled name.
  2357. While processing the function name root, arrange to call
  2358. demangle_signature if the root is ambiguous.
  2359. DECLP points to the string buffer into which demangled output is
  2360. placed. On entry, the buffer is empty. On exit it contains
  2361. the root function name, the demangled operator name, or in some
  2362. special cases either nothing or the completely demangled result.
  2363. MANGLED points to the current pointer into the mangled name. As each
  2364. token of the mangled name is consumed, it is updated. Upon entry
  2365. the current mangled name pointer points to the first character of
  2366. the mangled name. Upon exit, it should point to the first character
  2367. of the signature if demangling was successful, or to the first
  2368. unconsumed character if demangling of the prefix was unsuccessful.
  2369. Returns 1 on success, 0 otherwise.
  2370. */
  2371. static int
  2372. demangle_prefix (struct work_stuff *work, const char **mangled,
  2373. string *declp)
  2374. {
  2375. int success = 1;
  2376. const char *scan;
  2377. int i;
  2378. if (strlen(*mangled) > 6
  2379. && (strncmp(*mangled, "_imp__", 6) == 0
  2380. || strncmp(*mangled, "__imp_", 6) == 0))
  2381. {
  2382. /* it's a symbol imported from a PE dynamic library. Check for both
  2383. new style prefix _imp__ and legacy __imp_ used by older versions
  2384. of dlltool. */
  2385. (*mangled) += 6;
  2386. work->dllimported = 1;
  2387. }
  2388. else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
  2389. {
  2390. char *marker = strchr (cplus_markers, (*mangled)[8]);
  2391. if (marker != NULL && *marker == (*mangled)[10])
  2392. {
  2393. if ((*mangled)[9] == 'D')
  2394. {
  2395. /* it's a GNU global destructor to be executed at program exit */
  2396. (*mangled) += 11;
  2397. work->destructor = 2;
  2398. if (gnu_special (work, mangled, declp))
  2399. return success;
  2400. }
  2401. else if ((*mangled)[9] == 'I')
  2402. {
  2403. /* it's a GNU global constructor to be executed at program init */
  2404. (*mangled) += 11;
  2405. work->constructor = 2;
  2406. if (gnu_special (work, mangled, declp))
  2407. return success;
  2408. }
  2409. }
  2410. }
  2411. else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
  2412. {
  2413. /* it's a ARM global destructor to be executed at program exit */
  2414. (*mangled) += 7;
  2415. work->destructor = 2;
  2416. }
  2417. else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
  2418. {
  2419. /* it's a ARM global constructor to be executed at program initial */
  2420. (*mangled) += 7;
  2421. work->constructor = 2;
  2422. }
  2423. /* This block of code is a reduction in strength time optimization
  2424. of:
  2425. scan = strstr (*mangled, "__"); */
  2426. {
  2427. scan = *mangled;
  2428. do {
  2429. scan = strchr (scan, '_');
  2430. } while (scan != NULL && *++scan != '_');
  2431. if (scan != NULL) --scan;
  2432. }
  2433. if (scan != NULL)
  2434. {
  2435. /* We found a sequence of two or more '_', ensure that we start at
  2436. the last pair in the sequence. */
  2437. i = strspn (scan, "_");
  2438. if (i > 2)
  2439. {
  2440. scan += (i - 2);
  2441. }
  2442. }
  2443. if (scan == NULL)
  2444. {
  2445. success = 0;
  2446. }
  2447. else if (work -> static_type)
  2448. {
  2449. if (!ISDIGIT ((unsigned char)scan[0]) && (scan[0] != 't'))
  2450. {
  2451. success = 0;
  2452. }
  2453. }
  2454. else if ((scan == *mangled)
  2455. && (ISDIGIT ((unsigned char)scan[2]) || (scan[2] == 'Q')
  2456. || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
  2457. {
  2458. /* The ARM says nothing about the mangling of local variables.
  2459. But cfront mangles local variables by prepending __<nesting_level>
  2460. to them. As an extension to ARM demangling we handle this case. */
  2461. if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
  2462. && ISDIGIT ((unsigned char)scan[2]))
  2463. {
  2464. *mangled = scan + 2;
  2465. consume_count (mangled);
  2466. string_append (declp, *mangled);
  2467. *mangled += strlen (*mangled);
  2468. success = 1;
  2469. }
  2470. else
  2471. {
  2472. /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
  2473. names like __Q2_3foo3bar for nested type names. So don't accept
  2474. this style of constructor for cfront demangling. A GNU
  2475. style member-template constructor starts with 'H'. */
  2476. if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
  2477. work -> constructor += 1;
  2478. *mangled = scan + 2;
  2479. }
  2480. }
  2481. else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
  2482. {
  2483. /* Cfront-style parameterized type. Handled later as a signature. */
  2484. success = 1;
  2485. /* ARM template? */
  2486. demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
  2487. }
  2488. else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
  2489. || (scan[2] == 'p' && scan[3] == 's')
  2490. || (scan[2] == 'p' && scan[3] == 't')))
  2491. {
  2492. /* EDG-style parameterized type. Handled later as a signature. */
  2493. success = 1;
  2494. /* EDG template? */
  2495. demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
  2496. }
  2497. else if ((scan == *mangled) && !ISDIGIT ((unsigned char)scan[2])
  2498. && (scan[2] != 't'))
  2499. {
  2500. /* Mangled name starts with "__". Skip over any leading '_' characters,
  2501. then find the next "__" that separates the prefix from the signature.
  2502. */
  2503. if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  2504. || (arm_special (mangled, declp) == 0))
  2505. {
  2506. while (*scan == '_')
  2507. {
  2508. scan++;
  2509. }
  2510. if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
  2511. {
  2512. /* No separator (I.E. "__not_mangled"), or empty signature
  2513. (I.E. "__not_mangled_either__") */
  2514. success = 0;
  2515. }
  2516. else
  2517. return iterate_demangle_function (work, mangled, declp, scan);
  2518. }
  2519. }
  2520. else if (*(scan + 2) != '\0')
  2521. {
  2522. /* Mangled name does not start with "__" but does have one somewhere
  2523. in there with non empty stuff after it. Looks like a global
  2524. function name. Iterate over all "__":s until the right
  2525. one is found. */
  2526. return iterate_demangle_function (work, mangled, declp, scan);
  2527. }
  2528. else
  2529. {
  2530. /* Doesn't look like a mangled name */
  2531. success = 0;
  2532. }
  2533. if (!success && (work->constructor == 2 || work->destructor == 2))
  2534. {
  2535. string_append (declp, *mangled);
  2536. *mangled += strlen (*mangled);
  2537. success = 1;
  2538. }
  2539. return (success);
  2540. }
  2541. /*
  2542. LOCAL FUNCTION
  2543. gnu_special -- special handling of gnu mangled strings
  2544. SYNOPSIS
  2545. static int
  2546. gnu_special (struct work_stuff *work, const char **mangled,
  2547. string *declp);
  2548. DESCRIPTION
  2549. Process some special GNU style mangling forms that don't fit
  2550. the normal pattern. For example:
  2551. _$_3foo (destructor for class foo)
  2552. _vt$foo (foo virtual table)
  2553. _vt$foo$bar (foo::bar virtual table)
  2554. __vt_foo (foo virtual table, new style with thunks)
  2555. _3foo$varname (static data member)
  2556. _Q22rs2tu$vw (static data member)
  2557. __t6vector1Zii (constructor with template)
  2558. __thunk_4__$_7ostream (virtual function thunk)
  2559. */
  2560. static int
  2561. gnu_special (struct work_stuff *work, const char **mangled, string *declp)
  2562. {
  2563. int n;
  2564. int success = 1;
  2565. const char *p;
  2566. if ((*mangled)[0] == '_'
  2567. && strchr (cplus_markers, (*mangled)[1]) != NULL
  2568. && (*mangled)[2] == '_')
  2569. {
  2570. /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
  2571. (*mangled) += 3;
  2572. work -> destructor += 1;
  2573. }
  2574. else if ((*mangled)[0] == '_'
  2575. && (((*mangled)[1] == '_'
  2576. && (*mangled)[2] == 'v'
  2577. && (*mangled)[3] == 't'
  2578. && (*mangled)[4] == '_')
  2579. || ((*mangled)[1] == 'v'
  2580. && (*mangled)[2] == 't'
  2581. && strchr (cplus_markers, (*mangled)[3]) != NULL)))
  2582. {
  2583. /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
  2584. and create the decl. Note that we consume the entire mangled
  2585. input string, which means that demangle_signature has no work
  2586. to do. */
  2587. if ((*mangled)[2] == 'v')
  2588. (*mangled) += 5; /* New style, with thunks: "__vt_" */
  2589. else
  2590. (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
  2591. while (**mangled != '\0')
  2592. {
  2593. switch (**mangled)
  2594. {
  2595. case 'Q':
  2596. case 'K':
  2597. success = demangle_qualified (work, mangled, declp, 0, 1);
  2598. break;
  2599. case 't':
  2600. success = demangle_template (work, mangled, declp, 0, 1,
  2601. 1);
  2602. break;
  2603. default:
  2604. if (ISDIGIT((unsigned char)*mangled[0]))
  2605. {
  2606. n = consume_count(mangled);
  2607. /* We may be seeing a too-large size, or else a
  2608. ".<digits>" indicating a static local symbol. In
  2609. any case, declare victory and move on; *don't* try
  2610. to use n to allocate. */
  2611. if (n > (int) strlen (*mangled))
  2612. {
  2613. success = 1;
  2614. break;
  2615. }
  2616. }
  2617. else
  2618. {
  2619. n = strcspn (*mangled, cplus_markers);
  2620. }
  2621. string_appendn (declp, *mangled, n);
  2622. (*mangled) += n;
  2623. }
  2624. p = strpbrk (*mangled, cplus_markers);
  2625. if (success && ((p == NULL) || (p == *mangled)))
  2626. {
  2627. if (p != NULL)
  2628. {
  2629. string_append (declp, SCOPE_STRING (work));
  2630. (*mangled)++;
  2631. }
  2632. }
  2633. else
  2634. {
  2635. success = 0;
  2636. break;
  2637. }
  2638. }
  2639. if (success)
  2640. string_append (declp, " virtual table");
  2641. }
  2642. else if ((*mangled)[0] == '_'
  2643. && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
  2644. && (p = strpbrk (*mangled, cplus_markers)) != NULL)
  2645. {
  2646. /* static data member, "_3foo$varname" for example */
  2647. (*mangled)++;
  2648. switch (**mangled)
  2649. {
  2650. case 'Q':
  2651. case 'K':
  2652. success = demangle_qualified (work, mangled, declp, 0, 1);
  2653. break;
  2654. case 't':
  2655. success = demangle_template (work, mangled, declp, 0, 1, 1);
  2656. break;
  2657. default:
  2658. n = consume_count (mangled);
  2659. if (n < 0 || n > (long) strlen (*mangled))
  2660. {
  2661. success = 0;
  2662. break;
  2663. }
  2664. if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
  2665. && (*mangled)[9] == 'N'
  2666. && (*mangled)[8] == (*mangled)[10]
  2667. && strchr (cplus_markers, (*mangled)[8]))
  2668. {
  2669. /* A member of the anonymous namespace. There's information
  2670. about what identifier or filename it was keyed to, but
  2671. it's just there to make the mangled name unique; we just
  2672. step over it. */
  2673. string_append (declp, "{anonymous}");
  2674. (*mangled) += n;
  2675. /* Now p points to the marker before the N, so we need to
  2676. update it to the first marker after what we consumed. */
  2677. p = strpbrk (*mangled, cplus_markers);
  2678. break;
  2679. }
  2680. string_appendn (declp, *mangled, n);
  2681. (*mangled) += n;
  2682. }
  2683. if (success && (p == *mangled))
  2684. {
  2685. /* Consumed everything up to the cplus_marker, append the
  2686. variable name. */
  2687. (*mangled)++;
  2688. string_append (declp, SCOPE_STRING (work));
  2689. n = strlen (*mangled);
  2690. string_appendn (declp, *mangled, n);
  2691. (*mangled) += n;
  2692. }
  2693. else
  2694. {
  2695. success = 0;
  2696. }
  2697. }
  2698. else if (strncmp (*mangled, "__thunk_", 8) == 0)
  2699. {
  2700. int delta;
  2701. (*mangled) += 8;
  2702. delta = consume_count (mangled);
  2703. if (delta == -1)
  2704. success = 0;
  2705. else
  2706. {
  2707. char *method = internal_cplus_demangle (work, ++*mangled);
  2708. if (method)
  2709. {
  2710. char buf[50];
  2711. sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
  2712. string_append (declp, buf);
  2713. string_append (declp, method);
  2714. free (method);
  2715. n = strlen (*mangled);
  2716. (*mangled) += n;
  2717. }
  2718. else
  2719. {
  2720. success = 0;
  2721. }
  2722. }
  2723. }
  2724. else if (strncmp (*mangled, "__t", 3) == 0
  2725. && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
  2726. {
  2727. p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
  2728. (*mangled) += 4;
  2729. switch (**mangled)
  2730. {
  2731. case 'Q':
  2732. case 'K':
  2733. success = demangle_qualified (work, mangled, declp, 0, 1);
  2734. break;
  2735. case 't':
  2736. success = demangle_template (work, mangled, declp, 0, 1, 1);
  2737. break;
  2738. default:
  2739. success = do_type (work, mangled, declp);
  2740. break;
  2741. }
  2742. if (success && **mangled != '\0')
  2743. success = 0;
  2744. if (success)
  2745. string_append (declp, p);
  2746. }
  2747. else
  2748. {
  2749. success = 0;
  2750. }
  2751. return (success);
  2752. }
  2753. static void
  2754. recursively_demangle(struct work_stuff *work, const char **mangled,
  2755. string *result, int namelength)
  2756. {
  2757. char * recurse = (char *)NULL;
  2758. char * recurse_dem = (char *)NULL;
  2759. recurse = XNEWVEC (char, namelength + 1);
  2760. memcpy (recurse, *mangled, namelength);
  2761. recurse[namelength] = '\000';
  2762. recurse_dem = cplus_demangle (recurse, work->options);
  2763. if (recurse_dem)
  2764. {
  2765. string_append (result, recurse_dem);
  2766. free (recurse_dem);
  2767. }
  2768. else
  2769. {
  2770. string_appendn (result, *mangled, namelength);
  2771. }
  2772. free (recurse);
  2773. *mangled += namelength;
  2774. }
  2775. /*
  2776. LOCAL FUNCTION
  2777. arm_special -- special handling of ARM/lucid mangled strings
  2778. SYNOPSIS
  2779. static int
  2780. arm_special (const char **mangled,
  2781. string *declp);
  2782. DESCRIPTION
  2783. Process some special ARM style mangling forms that don't fit
  2784. the normal pattern. For example:
  2785. __vtbl__3foo (foo virtual table)
  2786. __vtbl__3foo__3bar (bar::foo virtual table)
  2787. */
  2788. static int
  2789. arm_special (const char **mangled, string *declp)
  2790. {
  2791. int n;
  2792. int success = 1;
  2793. const char *scan;
  2794. if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
  2795. {
  2796. /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
  2797. and create the decl. Note that we consume the entire mangled
  2798. input string, which means that demangle_signature has no work
  2799. to do. */
  2800. scan = *mangled + ARM_VTABLE_STRLEN;
  2801. while (*scan != '\0') /* first check it can be demangled */
  2802. {
  2803. n = consume_count (&scan);
  2804. if (n == -1)
  2805. {
  2806. return (0); /* no good */
  2807. }
  2808. scan += n;
  2809. if (scan[0] == '_' && scan[1] == '_')
  2810. {
  2811. scan += 2;
  2812. }
  2813. }
  2814. (*mangled) += ARM_VTABLE_STRLEN;
  2815. while (**mangled != '\0')
  2816. {
  2817. n = consume_count (mangled);
  2818. if (n == -1
  2819. || n > (long) strlen (*mangled))
  2820. return 0;
  2821. string_prependn (declp, *mangled, n);
  2822. (*mangled) += n;
  2823. if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
  2824. {
  2825. string_prepend (declp, "::");
  2826. (*mangled) += 2;
  2827. }
  2828. }
  2829. string_append (declp, " virtual table");
  2830. }
  2831. else
  2832. {
  2833. success = 0;
  2834. }
  2835. return (success);
  2836. }
  2837. /*
  2838. LOCAL FUNCTION
  2839. demangle_qualified -- demangle 'Q' qualified name strings
  2840. SYNOPSIS
  2841. static int
  2842. demangle_qualified (struct work_stuff *, const char *mangled,
  2843. string *result, int isfuncname, int append);
  2844. DESCRIPTION
  2845. Demangle a qualified name, such as "Q25Outer5Inner" which is
  2846. the mangled form of "Outer::Inner". The demangled output is
  2847. prepended or appended to the result string according to the
  2848. state of the append flag.
  2849. If isfuncname is nonzero, then the qualified name we are building
  2850. is going to be used as a member function name, so if it is a
  2851. constructor or destructor function, append an appropriate
  2852. constructor or destructor name. I.E. for the above example,
  2853. the result for use as a constructor is "Outer::Inner::Inner"
  2854. and the result for use as a destructor is "Outer::Inner::~Inner".
  2855. BUGS
  2856. Numeric conversion is ASCII dependent (FIXME).
  2857. */
  2858. static int
  2859. demangle_qualified (struct work_stuff *work, const char **mangled,
  2860. string *result, int isfuncname, int append)
  2861. {
  2862. int qualifiers = 0;
  2863. int success = 1;
  2864. char num[2];
  2865. string temp;
  2866. string last_name;
  2867. int bindex = register_Btype (work);
  2868. /* We only make use of ISFUNCNAME if the entity is a constructor or
  2869. destructor. */
  2870. isfuncname = (isfuncname
  2871. && ((work->constructor & 1) || (work->destructor & 1)));
  2872. string_init (&temp);
  2873. string_init (&last_name);
  2874. if ((*mangled)[0] == 'K')
  2875. {
  2876. /* Squangling qualified name reuse */
  2877. int idx;
  2878. (*mangled)++;
  2879. idx = consume_count_with_underscores (mangled);
  2880. if (idx == -1 || idx >= work -> numk)
  2881. success = 0;
  2882. else
  2883. string_append (&temp, work -> ktypevec[idx]);
  2884. }
  2885. else
  2886. switch ((*mangled)[1])
  2887. {
  2888. case '_':
  2889. /* GNU mangled name with more than 9 classes. The count is preceded
  2890. by an underscore (to distinguish it from the <= 9 case) and followed
  2891. by an underscore. */
  2892. (*mangled)++;
  2893. qualifiers = consume_count_with_underscores (mangled);
  2894. if (qualifiers == -1)
  2895. success = 0;
  2896. break;
  2897. case '1':
  2898. case '2':
  2899. case '3':
  2900. case '4':
  2901. case '5':
  2902. case '6':
  2903. case '7':
  2904. case '8':
  2905. case '9':
  2906. /* The count is in a single digit. */
  2907. num[0] = (*mangled)[1];
  2908. num[1] = '\0';
  2909. qualifiers = atoi (num);
  2910. /* If there is an underscore after the digit, skip it. This is
  2911. said to be for ARM-qualified names, but the ARM makes no
  2912. mention of such an underscore. Perhaps cfront uses one. */
  2913. if ((*mangled)[2] == '_')
  2914. {
  2915. (*mangled)++;
  2916. }
  2917. (*mangled) += 2;
  2918. break;
  2919. case '0':
  2920. default:
  2921. success = 0;
  2922. }
  2923. if (!success)
  2924. return success;
  2925. /* Pick off the names and collect them in the temp buffer in the order
  2926. in which they are found, separated by '::'. */
  2927. while (qualifiers-- > 0)
  2928. {
  2929. int remember_K = 1;
  2930. string_clear (&last_name);
  2931. if (*mangled[0] == '_')
  2932. (*mangled)++;
  2933. if (*mangled[0] == 't')
  2934. {
  2935. /* Here we always append to TEMP since we will want to use
  2936. the template name without the template parameters as a
  2937. constructor or destructor name. The appropriate
  2938. (parameter-less) value is returned by demangle_template
  2939. in LAST_NAME. We do not remember the template type here,
  2940. in order to match the G++ mangling algorithm. */
  2941. success = demangle_template(work, mangled, &temp,
  2942. &last_name, 1, 0);
  2943. if (!success)
  2944. break;
  2945. }
  2946. else if (*mangled[0] == 'K')
  2947. {
  2948. int idx;
  2949. (*mangled)++;
  2950. idx = consume_count_with_underscores (mangled);
  2951. if (idx == -1 || idx >= work->numk)
  2952. success = 0;
  2953. else
  2954. string_append (&temp, work->ktypevec[idx]);
  2955. remember_K = 0;
  2956. if (!success) break;
  2957. }
  2958. else
  2959. {
  2960. if (EDG_DEMANGLING)
  2961. {
  2962. int namelength;
  2963. /* Now recursively demangle the qualifier
  2964. * This is necessary to deal with templates in
  2965. * mangling styles like EDG */
  2966. namelength = consume_count (mangled);
  2967. if (namelength == -1)
  2968. {
  2969. success = 0;
  2970. break;
  2971. }
  2972. recursively_demangle(work, mangled, &temp, namelength);
  2973. }
  2974. else
  2975. {
  2976. string_delete (&last_name);
  2977. success = do_type (work, mangled, &last_name);
  2978. if (!success)
  2979. break;
  2980. string_appends (&temp, &last_name);
  2981. }
  2982. }
  2983. if (remember_K)
  2984. remember_Ktype (work, temp.b, LEN_STRING (&temp));
  2985. if (qualifiers > 0)
  2986. string_append (&temp, SCOPE_STRING (work));
  2987. }
  2988. remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
  2989. /* If we are using the result as a function name, we need to append
  2990. the appropriate '::' separated constructor or destructor name.
  2991. We do this here because this is the most convenient place, where
  2992. we already have a pointer to the name and the length of the name. */
  2993. if (isfuncname)
  2994. {
  2995. string_append (&temp, SCOPE_STRING (work));
  2996. if (work -> destructor & 1)
  2997. string_append (&temp, "~");
  2998. string_appends (&temp, &last_name);
  2999. }
  3000. /* Now either prepend the temp buffer to the result, or append it,
  3001. depending upon the state of the append flag. */
  3002. if (append)
  3003. string_appends (result, &temp);
  3004. else
  3005. {
  3006. if (!STRING_EMPTY (result))
  3007. string_append (&temp, SCOPE_STRING (work));
  3008. string_prepends (result, &temp);
  3009. }
  3010. string_delete (&last_name);
  3011. string_delete (&temp);
  3012. return (success);
  3013. }
  3014. /*
  3015. LOCAL FUNCTION
  3016. get_count -- convert an ascii count to integer, consuming tokens
  3017. SYNOPSIS
  3018. static int
  3019. get_count (const char **type, int *count)
  3020. DESCRIPTION
  3021. Assume that *type points at a count in a mangled name; set
  3022. *count to its value, and set *type to the next character after
  3023. the count. There are some weird rules in effect here.
  3024. If *type does not point at a string of digits, return zero.
  3025. If *type points at a string of digits followed by an
  3026. underscore, set *count to their value as an integer, advance
  3027. *type to point *after the underscore, and return 1.
  3028. If *type points at a string of digits not followed by an
  3029. underscore, consume only the first digit. Set *count to its
  3030. value as an integer, leave *type pointing after that digit,
  3031. and return 1.
  3032. The excuse for this odd behavior: in the ARM and HP demangling
  3033. styles, a type can be followed by a repeat count of the form
  3034. `Nxy', where:
  3035. `x' is a single digit specifying how many additional copies
  3036. of the type to append to the argument list, and
  3037. `y' is one or more digits, specifying the zero-based index of
  3038. the first repeated argument in the list. Yes, as you're
  3039. unmangling the name you can figure this out yourself, but
  3040. it's there anyway.
  3041. So, for example, in `bar__3fooFPiN51', the first argument is a
  3042. pointer to an integer (`Pi'), and then the next five arguments
  3043. are the same (`N5'), and the first repeat is the function's
  3044. second argument (`1').
  3045. */
  3046. static int
  3047. get_count (const char **type, int *count)
  3048. {
  3049. const char *p;
  3050. int n;
  3051. if (!ISDIGIT ((unsigned char)**type))
  3052. return (0);
  3053. else
  3054. {
  3055. *count = **type - '0';
  3056. (*type)++;
  3057. if (ISDIGIT ((unsigned char)**type))
  3058. {
  3059. p = *type;
  3060. n = *count;
  3061. do
  3062. {
  3063. n *= 10;
  3064. n += *p - '0';
  3065. p++;
  3066. }
  3067. while (ISDIGIT ((unsigned char)*p));
  3068. if (*p == '_')
  3069. {
  3070. *type = p + 1;
  3071. *count = n;
  3072. }
  3073. }
  3074. }
  3075. return (1);
  3076. }
  3077. /* RESULT will be initialised here; it will be freed on failure. The
  3078. value returned is really a type_kind_t. */
  3079. static int
  3080. do_type (struct work_stuff *work, const char **mangled, string *result)
  3081. {
  3082. int n;
  3083. int done;
  3084. int success;
  3085. string decl;
  3086. const char *remembered_type;
  3087. int type_quals;
  3088. type_kind_t tk = tk_none;
  3089. string_init (&decl);
  3090. string_init (result);
  3091. done = 0;
  3092. success = 1;
  3093. while (success && !done)
  3094. {
  3095. int member;
  3096. switch (**mangled)
  3097. {
  3098. /* A pointer type */
  3099. case 'P':
  3100. case 'p':
  3101. (*mangled)++;
  3102. if (! (work -> options & DMGL_JAVA))
  3103. string_prepend (&decl, "*");
  3104. if (tk == tk_none)
  3105. tk = tk_pointer;
  3106. break;
  3107. /* A reference type */
  3108. case 'R':
  3109. (*mangled)++;
  3110. string_prepend (&decl, "&");
  3111. if (tk == tk_none)
  3112. tk = tk_reference;
  3113. break;
  3114. /* An array */
  3115. case 'A':
  3116. {
  3117. ++(*mangled);
  3118. if (!STRING_EMPTY (&decl)
  3119. && (decl.b[0] == '*' || decl.b[0] == '&'))
  3120. {
  3121. string_prepend (&decl, "(");
  3122. string_append (&decl, ")");
  3123. }
  3124. string_append (&decl, "[");
  3125. if (**mangled != '_')
  3126. success = demangle_template_value_parm (work, mangled, &decl,
  3127. tk_integral);
  3128. if (**mangled == '_')
  3129. ++(*mangled);
  3130. string_append (&decl, "]");
  3131. break;
  3132. }
  3133. /* A back reference to a previously seen type */
  3134. case 'T':
  3135. (*mangled)++;
  3136. if (!get_count (mangled, &n) || n >= work -> ntypes)
  3137. {
  3138. success = 0;
  3139. }
  3140. else
  3141. {
  3142. remembered_type = work -> typevec[n];
  3143. mangled = &remembered_type;
  3144. }
  3145. break;
  3146. /* A function */
  3147. case 'F':
  3148. (*mangled)++;
  3149. if (!STRING_EMPTY (&decl)
  3150. && (decl.b[0] == '*' || decl.b[0] == '&'))
  3151. {
  3152. string_prepend (&decl, "(");
  3153. string_append (&decl, ")");
  3154. }
  3155. /* After picking off the function args, we expect to either find the
  3156. function return type (preceded by an '_') or the end of the
  3157. string. */
  3158. if (!demangle_nested_args (work, mangled, &decl)
  3159. || (**mangled != '_' && **mangled != '\0'))
  3160. {
  3161. success = 0;
  3162. break;
  3163. }
  3164. if (success && (**mangled == '_'))
  3165. (*mangled)++;
  3166. break;
  3167. case 'M':
  3168. case 'O':
  3169. {
  3170. type_quals = TYPE_UNQUALIFIED;
  3171. member = **mangled == 'M';
  3172. (*mangled)++;
  3173. string_append (&decl, ")");
  3174. /* We don't need to prepend `::' for a qualified name;
  3175. demangle_qualified will do that for us. */
  3176. if (**mangled != 'Q')
  3177. string_prepend (&decl, SCOPE_STRING (work));
  3178. if (ISDIGIT ((unsigned char)**mangled))
  3179. {
  3180. n = consume_count (mangled);
  3181. if (n == -1
  3182. || (int) strlen (*mangled) < n)
  3183. {
  3184. success = 0;
  3185. break;
  3186. }
  3187. string_prependn (&decl, *mangled, n);
  3188. *mangled += n;
  3189. }
  3190. else if (**mangled == 'X' || **mangled == 'Y')
  3191. {
  3192. string temp;
  3193. do_type (work, mangled, &temp);
  3194. string_prepends (&decl, &temp);
  3195. string_delete (&temp);
  3196. }
  3197. else if (**mangled == 't')
  3198. {
  3199. string temp;
  3200. string_init (&temp);
  3201. success = demangle_template (work, mangled, &temp,
  3202. NULL, 1, 1);
  3203. if (success)
  3204. {
  3205. string_prependn (&decl, temp.b, temp.p - temp.b);
  3206. string_delete (&temp);
  3207. }
  3208. else
  3209. {
  3210. string_delete (&temp);
  3211. break;
  3212. }
  3213. }
  3214. else if (**mangled == 'Q')
  3215. {
  3216. success = demangle_qualified (work, mangled, &decl,
  3217. /*isfuncnam=*/0,
  3218. /*append=*/0);
  3219. if (!success)
  3220. break;
  3221. }
  3222. else
  3223. {
  3224. success = 0;
  3225. break;
  3226. }
  3227. string_prepend (&decl, "(");
  3228. if (member)
  3229. {
  3230. switch (**mangled)
  3231. {
  3232. case 'C':
  3233. case 'V':
  3234. case 'u':
  3235. type_quals |= code_for_qualifier (**mangled);
  3236. (*mangled)++;
  3237. break;
  3238. default:
  3239. break;
  3240. }
  3241. if (*(*mangled)++ != 'F')
  3242. {
  3243. success = 0;
  3244. break;
  3245. }
  3246. }
  3247. if ((member && !demangle_nested_args (work, mangled, &decl))
  3248. || **mangled != '_')
  3249. {
  3250. success = 0;
  3251. break;
  3252. }
  3253. (*mangled)++;
  3254. if (! PRINT_ANSI_QUALIFIERS)
  3255. {
  3256. break;
  3257. }
  3258. if (type_quals != TYPE_UNQUALIFIED)
  3259. {
  3260. APPEND_BLANK (&decl);
  3261. string_append (&decl, qualifier_string (type_quals));
  3262. }
  3263. break;
  3264. }
  3265. case 'G':
  3266. (*mangled)++;
  3267. break;
  3268. case 'C':
  3269. case 'V':
  3270. case 'u':
  3271. if (PRINT_ANSI_QUALIFIERS)
  3272. {
  3273. if (!STRING_EMPTY (&decl))
  3274. string_prepend (&decl, " ");
  3275. string_prepend (&decl, demangle_qualifier (**mangled));
  3276. }
  3277. (*mangled)++;
  3278. break;
  3279. /*
  3280. }
  3281. */
  3282. /* fall through */
  3283. default:
  3284. done = 1;
  3285. break;
  3286. }
  3287. }
  3288. if (success) switch (**mangled)
  3289. {
  3290. /* A qualified name, such as "Outer::Inner". */
  3291. case 'Q':
  3292. case 'K':
  3293. {
  3294. success = demangle_qualified (work, mangled, result, 0, 1);
  3295. break;
  3296. }
  3297. /* A back reference to a previously seen squangled type */
  3298. case 'B':
  3299. (*mangled)++;
  3300. if (!get_count (mangled, &n) || n >= work -> numb)
  3301. success = 0;
  3302. else
  3303. string_append (result, work->btypevec[n]);
  3304. break;
  3305. case 'X':
  3306. case 'Y':
  3307. /* A template parm. We substitute the corresponding argument. */
  3308. {
  3309. int idx;
  3310. (*mangled)++;
  3311. idx = consume_count_with_underscores (mangled);
  3312. if (idx == -1
  3313. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  3314. || consume_count_with_underscores (mangled) == -1)
  3315. {
  3316. success = 0;
  3317. break;
  3318. }
  3319. if (work->tmpl_argvec)
  3320. string_append (result, work->tmpl_argvec[idx]);
  3321. else
  3322. string_append_template_idx (result, idx);
  3323. success = 1;
  3324. }
  3325. break;
  3326. default:
  3327. success = demangle_fund_type (work, mangled, result);
  3328. if (tk == tk_none)
  3329. tk = (type_kind_t) success;
  3330. break;
  3331. }
  3332. if (success)
  3333. {
  3334. if (!STRING_EMPTY (&decl))
  3335. {
  3336. string_append (result, " ");
  3337. string_appends (result, &decl);
  3338. }
  3339. }
  3340. else
  3341. string_delete (result);
  3342. string_delete (&decl);
  3343. if (success)
  3344. /* Assume an integral type, if we're not sure. */
  3345. return (int) ((tk == tk_none) ? tk_integral : tk);
  3346. else
  3347. return 0;
  3348. }
  3349. /* Given a pointer to a type string that represents a fundamental type
  3350. argument (int, long, unsigned int, etc) in TYPE, a pointer to the
  3351. string in which the demangled output is being built in RESULT, and
  3352. the WORK structure, decode the types and add them to the result.
  3353. For example:
  3354. "Ci" => "const int"
  3355. "Sl" => "signed long"
  3356. "CUs" => "const unsigned short"
  3357. The value returned is really a type_kind_t. */
  3358. static int
  3359. demangle_fund_type (struct work_stuff *work,
  3360. const char **mangled, string *result)
  3361. {
  3362. int done = 0;
  3363. int success = 1;
  3364. char buf[INTBUF_SIZE + 5 /* 'int%u_t' */];
  3365. unsigned int dec = 0;
  3366. type_kind_t tk = tk_integral;
  3367. /* First pick off any type qualifiers. There can be more than one. */
  3368. while (!done)
  3369. {
  3370. switch (**mangled)
  3371. {
  3372. case 'C':
  3373. case 'V':
  3374. case 'u':
  3375. if (PRINT_ANSI_QUALIFIERS)
  3376. {
  3377. if (!STRING_EMPTY (result))
  3378. string_prepend (result, " ");
  3379. string_prepend (result, demangle_qualifier (**mangled));
  3380. }
  3381. (*mangled)++;
  3382. break;
  3383. case 'U':
  3384. (*mangled)++;
  3385. APPEND_BLANK (result);
  3386. string_append (result, "unsigned");
  3387. break;
  3388. case 'S': /* signed char only */
  3389. (*mangled)++;
  3390. APPEND_BLANK (result);
  3391. string_append (result, "signed");
  3392. break;
  3393. case 'J':
  3394. (*mangled)++;
  3395. APPEND_BLANK (result);
  3396. string_append (result, "__complex");
  3397. break;
  3398. default:
  3399. done = 1;
  3400. break;
  3401. }
  3402. }
  3403. /* Now pick off the fundamental type. There can be only one. */
  3404. switch (**mangled)
  3405. {
  3406. case '\0':
  3407. case '_':
  3408. break;
  3409. case 'v':
  3410. (*mangled)++;
  3411. APPEND_BLANK (result);
  3412. string_append (result, "void");
  3413. break;
  3414. case 'x':
  3415. (*mangled)++;
  3416. APPEND_BLANK (result);
  3417. string_append (result, "long long");
  3418. break;
  3419. case 'l':
  3420. (*mangled)++;
  3421. APPEND_BLANK (result);
  3422. string_append (result, "long");
  3423. break;
  3424. case 'i':
  3425. (*mangled)++;
  3426. APPEND_BLANK (result);
  3427. string_append (result, "int");
  3428. break;
  3429. case 's':
  3430. (*mangled)++;
  3431. APPEND_BLANK (result);
  3432. string_append (result, "short");
  3433. break;
  3434. case 'b':
  3435. (*mangled)++;
  3436. APPEND_BLANK (result);
  3437. string_append (result, "bool");
  3438. tk = tk_bool;
  3439. break;
  3440. case 'c':
  3441. (*mangled)++;
  3442. APPEND_BLANK (result);
  3443. string_append (result, "char");
  3444. tk = tk_char;
  3445. break;
  3446. case 'w':
  3447. (*mangled)++;
  3448. APPEND_BLANK (result);
  3449. string_append (result, "wchar_t");
  3450. tk = tk_char;
  3451. break;
  3452. case 'r':
  3453. (*mangled)++;
  3454. APPEND_BLANK (result);
  3455. string_append (result, "long double");
  3456. tk = tk_real;
  3457. break;
  3458. case 'd':
  3459. (*mangled)++;
  3460. APPEND_BLANK (result);
  3461. string_append (result, "double");
  3462. tk = tk_real;
  3463. break;
  3464. case 'f':
  3465. (*mangled)++;
  3466. APPEND_BLANK (result);
  3467. string_append (result, "float");
  3468. tk = tk_real;
  3469. break;
  3470. case 'G':
  3471. (*mangled)++;
  3472. if (!ISDIGIT ((unsigned char)**mangled))
  3473. {
  3474. success = 0;
  3475. break;
  3476. }
  3477. case 'I':
  3478. (*mangled)++;
  3479. if (**mangled == '_')
  3480. {
  3481. int i;
  3482. (*mangled)++;
  3483. for (i = 0;
  3484. i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
  3485. (*mangled)++, i++)
  3486. buf[i] = **mangled;
  3487. if (**mangled != '_')
  3488. {
  3489. success = 0;
  3490. break;
  3491. }
  3492. buf[i] = '\0';
  3493. (*mangled)++;
  3494. }
  3495. else
  3496. {
  3497. strncpy (buf, *mangled, 2);
  3498. buf[2] = '\0';
  3499. *mangled += min (strlen (*mangled), 2);
  3500. }
  3501. sscanf (buf, "%x", &dec);
  3502. sprintf (buf, "int%u_t", dec);
  3503. APPEND_BLANK (result);
  3504. string_append (result, buf);
  3505. break;
  3506. /* fall through */
  3507. /* An explicit type, such as "6mytype" or "7integer" */
  3508. case '0':
  3509. case '1':
  3510. case '2':
  3511. case '3':
  3512. case '4':
  3513. case '5':
  3514. case '6':
  3515. case '7':
  3516. case '8':
  3517. case '9':
  3518. {
  3519. int bindex = register_Btype (work);
  3520. string btype;
  3521. string_init (&btype);
  3522. if (demangle_class_name (work, mangled, &btype)) {
  3523. remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
  3524. APPEND_BLANK (result);
  3525. string_appends (result, &btype);
  3526. }
  3527. else
  3528. success = 0;
  3529. string_delete (&btype);
  3530. break;
  3531. }
  3532. case 't':
  3533. {
  3534. string btype;
  3535. string_init (&btype);
  3536. success = demangle_template (work, mangled, &btype, 0, 1, 1);
  3537. string_appends (result, &btype);
  3538. string_delete (&btype);
  3539. break;
  3540. }
  3541. default:
  3542. success = 0;
  3543. break;
  3544. }
  3545. return success ? ((int) tk) : 0;
  3546. }
  3547. /* Handle a template's value parameter for HP aCC (extension from ARM)
  3548. **mangled points to 'S' or 'U' */
  3549. static int
  3550. do_hpacc_template_const_value (struct work_stuff *work ATTRIBUTE_UNUSED,
  3551. const char **mangled, string *result)
  3552. {
  3553. int unsigned_const;
  3554. if (**mangled != 'U' && **mangled != 'S')
  3555. return 0;
  3556. unsigned_const = (**mangled == 'U');
  3557. (*mangled)++;
  3558. switch (**mangled)
  3559. {
  3560. case 'N':
  3561. string_append (result, "-");
  3562. /* fall through */
  3563. case 'P':
  3564. (*mangled)++;
  3565. break;
  3566. case 'M':
  3567. /* special case for -2^31 */
  3568. string_append (result, "-2147483648");
  3569. (*mangled)++;
  3570. return 1;
  3571. default:
  3572. return 0;
  3573. }
  3574. /* We have to be looking at an integer now */
  3575. if (!(ISDIGIT ((unsigned char)**mangled)))
  3576. return 0;
  3577. /* We only deal with integral values for template
  3578. parameters -- so it's OK to look only for digits */
  3579. while (ISDIGIT ((unsigned char)**mangled))
  3580. {
  3581. char_str[0] = **mangled;
  3582. string_append (result, char_str);
  3583. (*mangled)++;
  3584. }
  3585. if (unsigned_const)
  3586. string_append (result, "U");
  3587. /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
  3588. with L or LL suffixes. pai/1997-09-03 */
  3589. return 1; /* success */
  3590. }
  3591. /* Handle a template's literal parameter for HP aCC (extension from ARM)
  3592. **mangled is pointing to the 'A' */
  3593. static int
  3594. do_hpacc_template_literal (struct work_stuff *work, const char **mangled,
  3595. string *result)
  3596. {
  3597. int literal_len = 0;
  3598. char * recurse;
  3599. char * recurse_dem;
  3600. if (**mangled != 'A')
  3601. return 0;
  3602. (*mangled)++;
  3603. literal_len = consume_count (mangled);
  3604. if (literal_len <= 0)
  3605. return 0;
  3606. /* Literal parameters are names of arrays, functions, etc. and the
  3607. canonical representation uses the address operator */
  3608. string_append (result, "&");
  3609. /* Now recursively demangle the literal name */
  3610. recurse = XNEWVEC (char, literal_len + 1);
  3611. memcpy (recurse, *mangled, literal_len);
  3612. recurse[literal_len] = '\000';
  3613. recurse_dem = cplus_demangle (recurse, work->options);
  3614. if (recurse_dem)
  3615. {
  3616. string_append (result, recurse_dem);
  3617. free (recurse_dem);
  3618. }
  3619. else
  3620. {
  3621. string_appendn (result, *mangled, literal_len);
  3622. }
  3623. (*mangled) += literal_len;
  3624. free (recurse);
  3625. return 1;
  3626. }
  3627. static int
  3628. snarf_numeric_literal (const char **args, string *arg)
  3629. {
  3630. if (**args == '-')
  3631. {
  3632. char_str[0] = '-';
  3633. string_append (arg, char_str);
  3634. (*args)++;
  3635. }
  3636. else if (**args == '+')
  3637. (*args)++;
  3638. if (!ISDIGIT ((unsigned char)**args))
  3639. return 0;
  3640. while (ISDIGIT ((unsigned char)**args))
  3641. {
  3642. char_str[0] = **args;
  3643. string_append (arg, char_str);
  3644. (*args)++;
  3645. }
  3646. return 1;
  3647. }
  3648. /* Demangle the next argument, given by MANGLED into RESULT, which
  3649. *should be an uninitialized* string. It will be initialized here,
  3650. and free'd should anything go wrong. */
  3651. static int
  3652. do_arg (struct work_stuff *work, const char **mangled, string *result)
  3653. {
  3654. /* Remember where we started so that we can record the type, for
  3655. non-squangling type remembering. */
  3656. const char *start = *mangled;
  3657. string_init (result);
  3658. if (work->nrepeats > 0)
  3659. {
  3660. --work->nrepeats;
  3661. if (work->previous_argument == 0)
  3662. return 0;
  3663. /* We want to reissue the previous type in this argument list. */
  3664. string_appends (result, work->previous_argument);
  3665. return 1;
  3666. }
  3667. if (**mangled == 'n')
  3668. {
  3669. /* A squangling-style repeat. */
  3670. (*mangled)++;
  3671. work->nrepeats = consume_count(mangled);
  3672. if (work->nrepeats <= 0)
  3673. /* This was not a repeat count after all. */
  3674. return 0;
  3675. if (work->nrepeats > 9)
  3676. {
  3677. if (**mangled != '_')
  3678. /* The repeat count should be followed by an '_' in this
  3679. case. */
  3680. return 0;
  3681. else
  3682. (*mangled)++;
  3683. }
  3684. /* Now, the repeat is all set up. */
  3685. return do_arg (work, mangled, result);
  3686. }
  3687. /* Save the result in WORK->previous_argument so that we can find it
  3688. if it's repeated. Note that saving START is not good enough: we
  3689. do not want to add additional types to the back-referenceable
  3690. type vector when processing a repeated type. */
  3691. if (work->previous_argument)
  3692. string_delete (work->previous_argument);
  3693. else
  3694. work->previous_argument = XNEW (string);
  3695. if (!do_type (work, mangled, work->previous_argument))
  3696. return 0;
  3697. string_appends (result, work->previous_argument);
  3698. remember_type (work, start, *mangled - start);
  3699. return 1;
  3700. }
  3701. static void
  3702. remember_type (struct work_stuff *work, const char *start, int len)
  3703. {
  3704. char *tem;
  3705. if (work->forgetting_types)
  3706. return;
  3707. if (work -> ntypes >= work -> typevec_size)
  3708. {
  3709. if (work -> typevec_size == 0)
  3710. {
  3711. work -> typevec_size = 3;
  3712. work -> typevec = XNEWVEC (char *, work->typevec_size);
  3713. }
  3714. else
  3715. {
  3716. work -> typevec_size *= 2;
  3717. work -> typevec
  3718. = XRESIZEVEC (char *, work->typevec, work->typevec_size);
  3719. }
  3720. }
  3721. tem = XNEWVEC (char, len + 1);
  3722. memcpy (tem, start, len);
  3723. tem[len] = '\0';
  3724. work -> typevec[work -> ntypes++] = tem;
  3725. }
  3726. /* Remember a K type class qualifier. */
  3727. static void
  3728. remember_Ktype (struct work_stuff *work, const char *start, int len)
  3729. {
  3730. char *tem;
  3731. if (work -> numk >= work -> ksize)
  3732. {
  3733. if (work -> ksize == 0)
  3734. {
  3735. work -> ksize = 5;
  3736. work -> ktypevec = XNEWVEC (char *, work->ksize);
  3737. }
  3738. else
  3739. {
  3740. work -> ksize *= 2;
  3741. work -> ktypevec
  3742. = XRESIZEVEC (char *, work->ktypevec, work->ksize);
  3743. }
  3744. }
  3745. tem = XNEWVEC (char, len + 1);
  3746. memcpy (tem, start, len);
  3747. tem[len] = '\0';
  3748. work -> ktypevec[work -> numk++] = tem;
  3749. }
  3750. /* Register a B code, and get an index for it. B codes are registered
  3751. as they are seen, rather than as they are completed, so map<temp<char> >
  3752. registers map<temp<char> > as B0, and temp<char> as B1 */
  3753. static int
  3754. register_Btype (struct work_stuff *work)
  3755. {
  3756. int ret;
  3757. if (work -> numb >= work -> bsize)
  3758. {
  3759. if (work -> bsize == 0)
  3760. {
  3761. work -> bsize = 5;
  3762. work -> btypevec = XNEWVEC (char *, work->bsize);
  3763. }
  3764. else
  3765. {
  3766. work -> bsize *= 2;
  3767. work -> btypevec
  3768. = XRESIZEVEC (char *, work->btypevec, work->bsize);
  3769. }
  3770. }
  3771. ret = work -> numb++;
  3772. work -> btypevec[ret] = NULL;
  3773. return(ret);
  3774. }
  3775. /* Store a value into a previously registered B code type. */
  3776. static void
  3777. remember_Btype (struct work_stuff *work, const char *start,
  3778. int len, int index)
  3779. {
  3780. char *tem;
  3781. tem = XNEWVEC (char, len + 1);
  3782. memcpy (tem, start, len);
  3783. tem[len] = '\0';
  3784. work -> btypevec[index] = tem;
  3785. }
  3786. /* Lose all the info related to B and K type codes. */
  3787. static void
  3788. forget_B_and_K_types (struct work_stuff *work)
  3789. {
  3790. int i;
  3791. while (work -> numk > 0)
  3792. {
  3793. i = --(work -> numk);
  3794. if (work -> ktypevec[i] != NULL)
  3795. {
  3796. free (work -> ktypevec[i]);
  3797. work -> ktypevec[i] = NULL;
  3798. }
  3799. }
  3800. while (work -> numb > 0)
  3801. {
  3802. i = --(work -> numb);
  3803. if (work -> btypevec[i] != NULL)
  3804. {
  3805. free (work -> btypevec[i]);
  3806. work -> btypevec[i] = NULL;
  3807. }
  3808. }
  3809. }
  3810. /* Forget the remembered types, but not the type vector itself. */
  3811. static void
  3812. forget_types (struct work_stuff *work)
  3813. {
  3814. int i;
  3815. while (work -> ntypes > 0)
  3816. {
  3817. i = --(work -> ntypes);
  3818. if (work -> typevec[i] != NULL)
  3819. {
  3820. free (work -> typevec[i]);
  3821. work -> typevec[i] = NULL;
  3822. }
  3823. }
  3824. }
  3825. /* Process the argument list part of the signature, after any class spec
  3826. has been consumed, as well as the first 'F' character (if any). For
  3827. example:
  3828. "__als__3fooRT0" => process "RT0"
  3829. "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
  3830. DECLP must be already initialised, usually non-empty. It won't be freed
  3831. on failure.
  3832. Note that g++ differs significantly from ARM and lucid style mangling
  3833. with regards to references to previously seen types. For example, given
  3834. the source fragment:
  3835. class foo {
  3836. public:
  3837. foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
  3838. };
  3839. foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  3840. void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  3841. g++ produces the names:
  3842. __3fooiRT0iT2iT2
  3843. foo__FiR3fooiT1iT1
  3844. while lcc (and presumably other ARM style compilers as well) produces:
  3845. foo__FiR3fooT1T2T1T2
  3846. __ct__3fooFiR3fooT1T2T1T2
  3847. Note that g++ bases its type numbers starting at zero and counts all
  3848. previously seen types, while lucid/ARM bases its type numbers starting
  3849. at one and only considers types after it has seen the 'F' character
  3850. indicating the start of the function args. For lucid/ARM style, we
  3851. account for this difference by discarding any previously seen types when
  3852. we see the 'F' character, and subtracting one from the type number
  3853. reference.
  3854. */
  3855. static int
  3856. demangle_args (struct work_stuff *work, const char **mangled,
  3857. string *declp)
  3858. {
  3859. string arg;
  3860. int need_comma = 0;
  3861. int r;
  3862. int t;
  3863. const char *tem;
  3864. char temptype;
  3865. if (PRINT_ARG_TYPES)
  3866. {
  3867. string_append (declp, "(");
  3868. if (**mangled == '\0')
  3869. {
  3870. string_append (declp, "void");
  3871. }
  3872. }
  3873. while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
  3874. || work->nrepeats > 0)
  3875. {
  3876. if ((**mangled == 'N') || (**mangled == 'T'))
  3877. {
  3878. temptype = *(*mangled)++;
  3879. if (temptype == 'N')
  3880. {
  3881. if (!get_count (mangled, &r))
  3882. {
  3883. return (0);
  3884. }
  3885. }
  3886. else
  3887. {
  3888. r = 1;
  3889. }
  3890. if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
  3891. {
  3892. /* If we have 10 or more types we might have more than a 1 digit
  3893. index so we'll have to consume the whole count here. This
  3894. will lose if the next thing is a type name preceded by a
  3895. count but it's impossible to demangle that case properly
  3896. anyway. Eg if we already have 12 types is T12Pc "(..., type1,
  3897. Pc, ...)" or "(..., type12, char *, ...)" */
  3898. if ((t = consume_count(mangled)) <= 0)
  3899. {
  3900. return (0);
  3901. }
  3902. }
  3903. else
  3904. {
  3905. if (!get_count (mangled, &t))
  3906. {
  3907. return (0);
  3908. }
  3909. }
  3910. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  3911. {
  3912. t--;
  3913. }
  3914. /* Validate the type index. Protect against illegal indices from
  3915. malformed type strings. */
  3916. if ((t < 0) || (t >= work -> ntypes))
  3917. {
  3918. return (0);
  3919. }
  3920. while (work->nrepeats > 0 || --r >= 0)
  3921. {
  3922. tem = work -> typevec[t];
  3923. if (need_comma && PRINT_ARG_TYPES)
  3924. {
  3925. string_append (declp, ", ");
  3926. }
  3927. if (!do_arg (work, &tem, &arg))
  3928. {
  3929. return (0);
  3930. }
  3931. if (PRINT_ARG_TYPES)
  3932. {
  3933. string_appends (declp, &arg);
  3934. }
  3935. string_delete (&arg);
  3936. need_comma = 1;
  3937. }
  3938. }
  3939. else
  3940. {
  3941. if (need_comma && PRINT_ARG_TYPES)
  3942. string_append (declp, ", ");
  3943. if (!do_arg (work, mangled, &arg))
  3944. return (0);
  3945. if (PRINT_ARG_TYPES)
  3946. string_appends (declp, &arg);
  3947. string_delete (&arg);
  3948. need_comma = 1;
  3949. }
  3950. }
  3951. if (**mangled == 'e')
  3952. {
  3953. (*mangled)++;
  3954. if (PRINT_ARG_TYPES)
  3955. {
  3956. if (need_comma)
  3957. {
  3958. string_append (declp, ",");
  3959. }
  3960. string_append (declp, "...");
  3961. }
  3962. }
  3963. if (PRINT_ARG_TYPES)
  3964. {
  3965. string_append (declp, ")");
  3966. }
  3967. return (1);
  3968. }
  3969. /* Like demangle_args, but for demangling the argument lists of function
  3970. and method pointers or references, not top-level declarations. */
  3971. static int
  3972. demangle_nested_args (struct work_stuff *work, const char **mangled,
  3973. string *declp)
  3974. {
  3975. string* saved_previous_argument;
  3976. int result;
  3977. int saved_nrepeats;
  3978. /* The G++ name-mangling algorithm does not remember types on nested
  3979. argument lists, unless -fsquangling is used, and in that case the
  3980. type vector updated by remember_type is not used. So, we turn
  3981. off remembering of types here. */
  3982. ++work->forgetting_types;
  3983. /* For the repeat codes used with -fsquangling, we must keep track of
  3984. the last argument. */
  3985. saved_previous_argument = work->previous_argument;
  3986. saved_nrepeats = work->nrepeats;
  3987. work->previous_argument = 0;
  3988. work->nrepeats = 0;
  3989. /* Actually demangle the arguments. */
  3990. result = demangle_args (work, mangled, declp);
  3991. /* Restore the previous_argument field. */
  3992. if (work->previous_argument)
  3993. {
  3994. string_delete (work->previous_argument);
  3995. free ((char *) work->previous_argument);
  3996. }
  3997. work->previous_argument = saved_previous_argument;
  3998. --work->forgetting_types;
  3999. work->nrepeats = saved_nrepeats;
  4000. return result;
  4001. }
  4002. /* Returns 1 if a valid function name was found or 0 otherwise. */
  4003. static int
  4004. demangle_function_name (struct work_stuff *work, const char **mangled,
  4005. string *declp, const char *scan)
  4006. {
  4007. size_t i;
  4008. string type;
  4009. const char *tem;
  4010. string_appendn (declp, (*mangled), scan - (*mangled));
  4011. string_need (declp, 1);
  4012. *(declp -> p) = '\0';
  4013. /* Consume the function name, including the "__" separating the name
  4014. from the signature. We are guaranteed that SCAN points to the
  4015. separator. */
  4016. (*mangled) = scan + 2;
  4017. /* We may be looking at an instantiation of a template function:
  4018. foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
  4019. following _F marks the start of the function arguments. Handle
  4020. the template arguments first. */
  4021. if (HP_DEMANGLING && (**mangled == 'X'))
  4022. {
  4023. demangle_arm_hp_template (work, mangled, 0, declp);
  4024. /* This leaves MANGLED pointing to the 'F' marking func args */
  4025. }
  4026. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  4027. {
  4028. /* See if we have an ARM style constructor or destructor operator.
  4029. If so, then just record it, clear the decl, and return.
  4030. We can't build the actual constructor/destructor decl until later,
  4031. when we recover the class name from the signature. */
  4032. if (strcmp (declp -> b, "__ct") == 0)
  4033. {
  4034. work -> constructor += 1;
  4035. string_clear (declp);
  4036. return 1;
  4037. }
  4038. else if (strcmp (declp -> b, "__dt") == 0)
  4039. {
  4040. work -> destructor += 1;
  4041. string_clear (declp);
  4042. return 1;
  4043. }
  4044. }
  4045. if (declp->p - declp->b >= 3
  4046. && declp->b[0] == 'o'
  4047. && declp->b[1] == 'p'
  4048. && strchr (cplus_markers, declp->b[2]) != NULL)
  4049. {
  4050. /* see if it's an assignment expression */
  4051. if (declp->p - declp->b >= 10 /* op$assign_ */
  4052. && memcmp (declp->b + 3, "assign_", 7) == 0)
  4053. {
  4054. for (i = 0; i < ARRAY_SIZE (optable); i++)
  4055. {
  4056. int len = declp->p - declp->b - 10;
  4057. if ((int) strlen (optable[i].in) == len
  4058. && memcmp (optable[i].in, declp->b + 10, len) == 0)
  4059. {
  4060. string_clear (declp);
  4061. string_append (declp, "operator");
  4062. string_append (declp, optable[i].out);
  4063. string_append (declp, "=");
  4064. break;
  4065. }
  4066. }
  4067. }
  4068. else
  4069. {
  4070. for (i = 0; i < ARRAY_SIZE (optable); i++)
  4071. {
  4072. int len = declp->p - declp->b - 3;
  4073. if ((int) strlen (optable[i].in) == len
  4074. && memcmp (optable[i].in, declp->b + 3, len) == 0)
  4075. {
  4076. string_clear (declp);
  4077. string_append (declp, "operator");
  4078. string_append (declp, optable[i].out);
  4079. break;
  4080. }
  4081. }
  4082. }
  4083. }
  4084. else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
  4085. && strchr (cplus_markers, declp->b[4]) != NULL)
  4086. {
  4087. /* type conversion operator */
  4088. tem = declp->b + 5;
  4089. if (do_type (work, &tem, &type))
  4090. {
  4091. string_clear (declp);
  4092. string_append (declp, "operator ");
  4093. string_appends (declp, &type);
  4094. string_delete (&type);
  4095. }
  4096. }
  4097. else if (declp->b[0] == '_' && declp->b[1] == '_'
  4098. && declp->b[2] == 'o' && declp->b[3] == 'p')
  4099. {
  4100. /* ANSI. */
  4101. /* type conversion operator. */
  4102. tem = declp->b + 4;
  4103. if (do_type (work, &tem, &type))
  4104. {
  4105. string_clear (declp);
  4106. string_append (declp, "operator ");
  4107. string_appends (declp, &type);
  4108. string_delete (&type);
  4109. }
  4110. }
  4111. else if (declp->b[0] == '_' && declp->b[1] == '_'
  4112. && ISLOWER((unsigned char)declp->b[2])
  4113. && ISLOWER((unsigned char)declp->b[3]))
  4114. {
  4115. if (declp->b[4] == '\0')
  4116. {
  4117. /* Operator. */
  4118. for (i = 0; i < ARRAY_SIZE (optable); i++)
  4119. {
  4120. if (strlen (optable[i].in) == 2
  4121. && memcmp (optable[i].in, declp->b + 2, 2) == 0)
  4122. {
  4123. string_clear (declp);
  4124. string_append (declp, "operator");
  4125. string_append (declp, optable[i].out);
  4126. break;
  4127. }
  4128. }
  4129. }
  4130. else
  4131. {
  4132. if (declp->b[2] == 'a' && declp->b[5] == '\0')
  4133. {
  4134. /* Assignment. */
  4135. for (i = 0; i < ARRAY_SIZE (optable); i++)
  4136. {
  4137. if (strlen (optable[i].in) == 3
  4138. && memcmp (optable[i].in, declp->b + 2, 3) == 0)
  4139. {
  4140. string_clear (declp);
  4141. string_append (declp, "operator");
  4142. string_append (declp, optable[i].out);
  4143. break;
  4144. }
  4145. }
  4146. }
  4147. }
  4148. }
  4149. /* If a function name was obtained but it's not valid, we were not
  4150. successful. */
  4151. if (LEN_STRING (declp) == 1 && declp->b[0] == '.')
  4152. return 0;
  4153. else
  4154. return 1;
  4155. }
  4156. /* a mini string-handling package */
  4157. static void
  4158. string_need (string *s, int n)
  4159. {
  4160. int tem;
  4161. if (s->b == NULL)
  4162. {
  4163. if (n < 32)
  4164. {
  4165. n = 32;
  4166. }
  4167. s->p = s->b = XNEWVEC (char, n);
  4168. s->e = s->b + n;
  4169. }
  4170. else if (s->e - s->p < n)
  4171. {
  4172. tem = s->p - s->b;
  4173. n += tem;
  4174. n *= 2;
  4175. s->b = XRESIZEVEC (char, s->b, n);
  4176. s->p = s->b + tem;
  4177. s->e = s->b + n;
  4178. }
  4179. }
  4180. static void
  4181. string_delete (string *s)
  4182. {
  4183. if (s->b != NULL)
  4184. {
  4185. free (s->b);
  4186. s->b = s->e = s->p = NULL;
  4187. }
  4188. }
  4189. static void
  4190. string_init (string *s)
  4191. {
  4192. s->b = s->p = s->e = NULL;
  4193. }
  4194. static void
  4195. string_clear (string *s)
  4196. {
  4197. s->p = s->b;
  4198. }
  4199. #if 0
  4200. static int
  4201. string_empty (string *s)
  4202. {
  4203. return (s->b == s->p);
  4204. }
  4205. #endif
  4206. static void
  4207. string_append (string *p, const char *s)
  4208. {
  4209. int n;
  4210. if (s == NULL || *s == '\0')
  4211. return;
  4212. n = strlen (s);
  4213. string_need (p, n);
  4214. memcpy (p->p, s, n);
  4215. p->p += n;
  4216. }
  4217. static void
  4218. string_appends (string *p, string *s)
  4219. {
  4220. int n;
  4221. if (s->b != s->p)
  4222. {
  4223. n = s->p - s->b;
  4224. string_need (p, n);
  4225. memcpy (p->p, s->b, n);
  4226. p->p += n;
  4227. }
  4228. }
  4229. static void
  4230. string_appendn (string *p, const char *s, int n)
  4231. {
  4232. if (n != 0)
  4233. {
  4234. string_need (p, n);
  4235. memcpy (p->p, s, n);
  4236. p->p += n;
  4237. }
  4238. }
  4239. static void
  4240. string_prepend (string *p, const char *s)
  4241. {
  4242. if (s != NULL && *s != '\0')
  4243. {
  4244. string_prependn (p, s, strlen (s));
  4245. }
  4246. }
  4247. static void
  4248. string_prepends (string *p, string *s)
  4249. {
  4250. if (s->b != s->p)
  4251. {
  4252. string_prependn (p, s->b, s->p - s->b);
  4253. }
  4254. }
  4255. static void
  4256. string_prependn (string *p, const char *s, int n)
  4257. {
  4258. char *q;
  4259. if (n != 0)
  4260. {
  4261. string_need (p, n);
  4262. for (q = p->p - 1; q >= p->b; q--)
  4263. {
  4264. q[n] = q[0];
  4265. }
  4266. memcpy (p->b, s, n);
  4267. p->p += n;
  4268. }
  4269. }
  4270. static void
  4271. string_append_template_idx (string *s, int idx)
  4272. {
  4273. char buf[INTBUF_SIZE + 1 /* 'T' */];
  4274. sprintf(buf, "T%d", idx);
  4275. string_append (s, buf);
  4276. }