gdbtypes.c 186 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473
  1. /* Support routines for manipulating internal types for GDB.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support, using pieces from other GDB modules.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "bfd.h"
  17. #include "symtab.h"
  18. #include "symfile.h"
  19. #include "objfiles.h"
  20. #include "gdbtypes.h"
  21. #include "expression.h"
  22. #include "language.h"
  23. #include "target.h"
  24. #include "value.h"
  25. #include "demangle.h"
  26. #include "complaints.h"
  27. #include "gdbcmd.h"
  28. #include "cp-abi.h"
  29. #include "hashtab.h"
  30. #include "cp-support.h"
  31. #include "bcache.h"
  32. #include "dwarf2/loc.h"
  33. #include "dwarf2/read.h"
  34. #include "gdbcore.h"
  35. #include "floatformat.h"
  36. #include "f-lang.h"
  37. #include <algorithm>
  38. #include "gmp-utils.h"
  39. /* The value of an invalid conversion badness. */
  40. #define INVALID_CONVERSION 100
  41. /* Initialize BADNESS constants. */
  42. const struct rank LENGTH_MISMATCH_BADNESS = {INVALID_CONVERSION,0};
  43. const struct rank TOO_FEW_PARAMS_BADNESS = {INVALID_CONVERSION,0};
  44. const struct rank INCOMPATIBLE_TYPE_BADNESS = {INVALID_CONVERSION,0};
  45. const struct rank EXACT_MATCH_BADNESS = {0,0};
  46. const struct rank INTEGER_PROMOTION_BADNESS = {1,0};
  47. const struct rank FLOAT_PROMOTION_BADNESS = {1,0};
  48. const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0};
  49. const struct rank CV_CONVERSION_BADNESS = {1, 0};
  50. const struct rank INTEGER_CONVERSION_BADNESS = {2,0};
  51. const struct rank FLOAT_CONVERSION_BADNESS = {2,0};
  52. const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0};
  53. const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0};
  54. const struct rank BOOL_CONVERSION_BADNESS = {3,0};
  55. const struct rank BASE_CONVERSION_BADNESS = {2,0};
  56. const struct rank REFERENCE_CONVERSION_BADNESS = {2,0};
  57. const struct rank REFERENCE_SEE_THROUGH_BADNESS = {0,1};
  58. const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0};
  59. const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0};
  60. const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0};
  61. /* Floatformat pairs. */
  62. const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = {
  63. &floatformat_ieee_half_big,
  64. &floatformat_ieee_half_little
  65. };
  66. const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = {
  67. &floatformat_ieee_single_big,
  68. &floatformat_ieee_single_little
  69. };
  70. const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = {
  71. &floatformat_ieee_double_big,
  72. &floatformat_ieee_double_little
  73. };
  74. const struct floatformat *floatformats_ieee_quad[BFD_ENDIAN_UNKNOWN] = {
  75. &floatformat_ieee_quad_big,
  76. &floatformat_ieee_quad_little
  77. };
  78. const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = {
  79. &floatformat_ieee_double_big,
  80. &floatformat_ieee_double_littlebyte_bigword
  81. };
  82. const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = {
  83. &floatformat_i387_ext,
  84. &floatformat_i387_ext
  85. };
  86. const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = {
  87. &floatformat_m68881_ext,
  88. &floatformat_m68881_ext
  89. };
  90. const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = {
  91. &floatformat_arm_ext_big,
  92. &floatformat_arm_ext_littlebyte_bigword
  93. };
  94. const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = {
  95. &floatformat_ia64_spill_big,
  96. &floatformat_ia64_spill_little
  97. };
  98. const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = {
  99. &floatformat_vax_f,
  100. &floatformat_vax_f
  101. };
  102. const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = {
  103. &floatformat_vax_d,
  104. &floatformat_vax_d
  105. };
  106. const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = {
  107. &floatformat_ibm_long_double_big,
  108. &floatformat_ibm_long_double_little
  109. };
  110. const struct floatformat *floatformats_bfloat16[BFD_ENDIAN_UNKNOWN] = {
  111. &floatformat_bfloat16_big,
  112. &floatformat_bfloat16_little
  113. };
  114. /* Should opaque types be resolved? */
  115. static bool opaque_type_resolution = true;
  116. /* See gdbtypes.h. */
  117. unsigned int overload_debug = 0;
  118. /* A flag to enable strict type checking. */
  119. static bool strict_type_checking = true;
  120. /* A function to show whether opaque types are resolved. */
  121. static void
  122. show_opaque_type_resolution (struct ui_file *file, int from_tty,
  123. struct cmd_list_element *c,
  124. const char *value)
  125. {
  126. gdb_printf (file, _("Resolution of opaque struct/class/union types "
  127. "(if set before loading symbols) is %s.\n"),
  128. value);
  129. }
  130. /* A function to show whether C++ overload debugging is enabled. */
  131. static void
  132. show_overload_debug (struct ui_file *file, int from_tty,
  133. struct cmd_list_element *c, const char *value)
  134. {
  135. gdb_printf (file, _("Debugging of C++ overloading is %s.\n"),
  136. value);
  137. }
  138. /* A function to show the status of strict type checking. */
  139. static void
  140. show_strict_type_checking (struct ui_file *file, int from_tty,
  141. struct cmd_list_element *c, const char *value)
  142. {
  143. gdb_printf (file, _("Strict type checking is %s.\n"), value);
  144. }
  145. /* Allocate a new OBJFILE-associated type structure and fill it
  146. with some defaults. Space for the type structure is allocated
  147. on the objfile's objfile_obstack. */
  148. struct type *
  149. alloc_type (struct objfile *objfile)
  150. {
  151. struct type *type;
  152. gdb_assert (objfile != NULL);
  153. /* Alloc the structure and start off with all fields zeroed. */
  154. type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type);
  155. TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  156. struct main_type);
  157. OBJSTAT (objfile, n_types++);
  158. type->set_owner (objfile);
  159. /* Initialize the fields that might not be zero. */
  160. type->set_code (TYPE_CODE_UNDEF);
  161. TYPE_CHAIN (type) = type; /* Chain back to itself. */
  162. return type;
  163. }
  164. /* Allocate a new GDBARCH-associated type structure and fill it
  165. with some defaults. Space for the type structure is allocated
  166. on the obstack associated with GDBARCH. */
  167. struct type *
  168. alloc_type_arch (struct gdbarch *gdbarch)
  169. {
  170. struct type *type;
  171. gdb_assert (gdbarch != NULL);
  172. /* Alloc the structure and start off with all fields zeroed. */
  173. type = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct type);
  174. TYPE_MAIN_TYPE (type) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct main_type);
  175. type->set_owner (gdbarch);
  176. /* Initialize the fields that might not be zero. */
  177. type->set_code (TYPE_CODE_UNDEF);
  178. TYPE_CHAIN (type) = type; /* Chain back to itself. */
  179. return type;
  180. }
  181. /* If TYPE is objfile-associated, allocate a new type structure
  182. associated with the same objfile. If TYPE is gdbarch-associated,
  183. allocate a new type structure associated with the same gdbarch. */
  184. struct type *
  185. alloc_type_copy (const struct type *type)
  186. {
  187. if (type->is_objfile_owned ())
  188. return alloc_type (type->objfile_owner ());
  189. else
  190. return alloc_type_arch (type->arch_owner ());
  191. }
  192. /* See gdbtypes.h. */
  193. gdbarch *
  194. type::arch () const
  195. {
  196. struct gdbarch *arch;
  197. if (this->is_objfile_owned ())
  198. arch = this->objfile_owner ()->arch ();
  199. else
  200. arch = this->arch_owner ();
  201. /* The ARCH can be NULL if TYPE is associated with neither an objfile nor
  202. a gdbarch, however, this is very rare, and even then, in most cases
  203. that type::arch is called, we assume that a non-NULL value is
  204. returned. */
  205. gdb_assert (arch != nullptr);
  206. return arch;
  207. }
  208. /* See gdbtypes.h. */
  209. struct type *
  210. get_target_type (struct type *type)
  211. {
  212. if (type != NULL)
  213. {
  214. type = TYPE_TARGET_TYPE (type);
  215. if (type != NULL)
  216. type = check_typedef (type);
  217. }
  218. return type;
  219. }
  220. /* See gdbtypes.h. */
  221. unsigned int
  222. type_length_units (struct type *type)
  223. {
  224. int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
  225. return TYPE_LENGTH (type) / unit_size;
  226. }
  227. /* Alloc a new type instance structure, fill it with some defaults,
  228. and point it at OLDTYPE. Allocate the new type instance from the
  229. same place as OLDTYPE. */
  230. static struct type *
  231. alloc_type_instance (struct type *oldtype)
  232. {
  233. struct type *type;
  234. /* Allocate the structure. */
  235. if (!oldtype->is_objfile_owned ())
  236. type = GDBARCH_OBSTACK_ZALLOC (oldtype->arch_owner (), struct type);
  237. else
  238. type = OBSTACK_ZALLOC (&oldtype->objfile_owner ()->objfile_obstack,
  239. struct type);
  240. TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype);
  241. TYPE_CHAIN (type) = type; /* Chain back to itself for now. */
  242. return type;
  243. }
  244. /* Clear all remnants of the previous type at TYPE, in preparation for
  245. replacing it with something else. Preserve owner information. */
  246. static void
  247. smash_type (struct type *type)
  248. {
  249. bool objfile_owned = type->is_objfile_owned ();
  250. objfile *objfile = type->objfile_owner ();
  251. gdbarch *arch = type->arch_owner ();
  252. memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type));
  253. /* Restore owner information. */
  254. if (objfile_owned)
  255. type->set_owner (objfile);
  256. else
  257. type->set_owner (arch);
  258. /* For now, delete the rings. */
  259. TYPE_CHAIN (type) = type;
  260. /* For now, leave the pointer/reference types alone. */
  261. }
  262. /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
  263. to a pointer to memory where the pointer type should be stored.
  264. If *TYPEPTR is zero, update it to point to the pointer type we return.
  265. We allocate new memory if needed. */
  266. struct type *
  267. make_pointer_type (struct type *type, struct type **typeptr)
  268. {
  269. struct type *ntype; /* New type */
  270. struct type *chain;
  271. ntype = TYPE_POINTER_TYPE (type);
  272. if (ntype)
  273. {
  274. if (typeptr == 0)
  275. return ntype; /* Don't care about alloc,
  276. and have new type. */
  277. else if (*typeptr == 0)
  278. {
  279. *typeptr = ntype; /* Tracking alloc, and have new type. */
  280. return ntype;
  281. }
  282. }
  283. if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
  284. {
  285. ntype = alloc_type_copy (type);
  286. if (typeptr)
  287. *typeptr = ntype;
  288. }
  289. else /* We have storage, but need to reset it. */
  290. {
  291. ntype = *typeptr;
  292. chain = TYPE_CHAIN (ntype);
  293. smash_type (ntype);
  294. TYPE_CHAIN (ntype) = chain;
  295. }
  296. TYPE_TARGET_TYPE (ntype) = type;
  297. TYPE_POINTER_TYPE (type) = ntype;
  298. /* FIXME! Assumes the machine has only one representation for pointers! */
  299. TYPE_LENGTH (ntype) = gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT;
  300. ntype->set_code (TYPE_CODE_PTR);
  301. /* Mark pointers as unsigned. The target converts between pointers
  302. and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and
  303. gdbarch_address_to_pointer. */
  304. ntype->set_is_unsigned (true);
  305. /* Update the length of all the other variants of this type. */
  306. chain = TYPE_CHAIN (ntype);
  307. while (chain != ntype)
  308. {
  309. TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
  310. chain = TYPE_CHAIN (chain);
  311. }
  312. return ntype;
  313. }
  314. /* Given a type TYPE, return a type of pointers to that type.
  315. May need to construct such a type if this is the first use. */
  316. struct type *
  317. lookup_pointer_type (struct type *type)
  318. {
  319. return make_pointer_type (type, (struct type **) 0);
  320. }
  321. /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero,
  322. points to a pointer to memory where the reference type should be
  323. stored. If *TYPEPTR is zero, update it to point to the reference
  324. type we return. We allocate new memory if needed. REFCODE denotes
  325. the kind of reference type to lookup (lvalue or rvalue reference). */
  326. struct type *
  327. make_reference_type (struct type *type, struct type **typeptr,
  328. enum type_code refcode)
  329. {
  330. struct type *ntype; /* New type */
  331. struct type **reftype;
  332. struct type *chain;
  333. gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
  334. ntype = (refcode == TYPE_CODE_REF ? TYPE_REFERENCE_TYPE (type)
  335. : TYPE_RVALUE_REFERENCE_TYPE (type));
  336. if (ntype)
  337. {
  338. if (typeptr == 0)
  339. return ntype; /* Don't care about alloc,
  340. and have new type. */
  341. else if (*typeptr == 0)
  342. {
  343. *typeptr = ntype; /* Tracking alloc, and have new type. */
  344. return ntype;
  345. }
  346. }
  347. if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
  348. {
  349. ntype = alloc_type_copy (type);
  350. if (typeptr)
  351. *typeptr = ntype;
  352. }
  353. else /* We have storage, but need to reset it. */
  354. {
  355. ntype = *typeptr;
  356. chain = TYPE_CHAIN (ntype);
  357. smash_type (ntype);
  358. TYPE_CHAIN (ntype) = chain;
  359. }
  360. TYPE_TARGET_TYPE (ntype) = type;
  361. reftype = (refcode == TYPE_CODE_REF ? &TYPE_REFERENCE_TYPE (type)
  362. : &TYPE_RVALUE_REFERENCE_TYPE (type));
  363. *reftype = ntype;
  364. /* FIXME! Assume the machine has only one representation for
  365. references, and that it matches the (only) representation for
  366. pointers! */
  367. TYPE_LENGTH (ntype) = gdbarch_ptr_bit (type->arch ()) / TARGET_CHAR_BIT;
  368. ntype->set_code (refcode);
  369. *reftype = ntype;
  370. /* Update the length of all the other variants of this type. */
  371. chain = TYPE_CHAIN (ntype);
  372. while (chain != ntype)
  373. {
  374. TYPE_LENGTH (chain) = TYPE_LENGTH (ntype);
  375. chain = TYPE_CHAIN (chain);
  376. }
  377. return ntype;
  378. }
  379. /* Same as above, but caller doesn't care about memory allocation
  380. details. */
  381. struct type *
  382. lookup_reference_type (struct type *type, enum type_code refcode)
  383. {
  384. return make_reference_type (type, (struct type **) 0, refcode);
  385. }
  386. /* Lookup the lvalue reference type for the type TYPE. */
  387. struct type *
  388. lookup_lvalue_reference_type (struct type *type)
  389. {
  390. return lookup_reference_type (type, TYPE_CODE_REF);
  391. }
  392. /* Lookup the rvalue reference type for the type TYPE. */
  393. struct type *
  394. lookup_rvalue_reference_type (struct type *type)
  395. {
  396. return lookup_reference_type (type, TYPE_CODE_RVALUE_REF);
  397. }
  398. /* Lookup a function type that returns type TYPE. TYPEPTR, if
  399. nonzero, points to a pointer to memory where the function type
  400. should be stored. If *TYPEPTR is zero, update it to point to the
  401. function type we return. We allocate new memory if needed. */
  402. struct type *
  403. make_function_type (struct type *type, struct type **typeptr)
  404. {
  405. struct type *ntype; /* New type */
  406. if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
  407. {
  408. ntype = alloc_type_copy (type);
  409. if (typeptr)
  410. *typeptr = ntype;
  411. }
  412. else /* We have storage, but need to reset it. */
  413. {
  414. ntype = *typeptr;
  415. smash_type (ntype);
  416. }
  417. TYPE_TARGET_TYPE (ntype) = type;
  418. TYPE_LENGTH (ntype) = 1;
  419. ntype->set_code (TYPE_CODE_FUNC);
  420. INIT_FUNC_SPECIFIC (ntype);
  421. return ntype;
  422. }
  423. /* Given a type TYPE, return a type of functions that return that type.
  424. May need to construct such a type if this is the first use. */
  425. struct type *
  426. lookup_function_type (struct type *type)
  427. {
  428. return make_function_type (type, (struct type **) 0);
  429. }
  430. /* Given a type TYPE and argument types, return the appropriate
  431. function type. If the final type in PARAM_TYPES is NULL, make a
  432. varargs function. */
  433. struct type *
  434. lookup_function_type_with_arguments (struct type *type,
  435. int nparams,
  436. struct type **param_types)
  437. {
  438. struct type *fn = make_function_type (type, (struct type **) 0);
  439. int i;
  440. if (nparams > 0)
  441. {
  442. if (param_types[nparams - 1] == NULL)
  443. {
  444. --nparams;
  445. fn->set_has_varargs (true);
  446. }
  447. else if (check_typedef (param_types[nparams - 1])->code ()
  448. == TYPE_CODE_VOID)
  449. {
  450. --nparams;
  451. /* Caller should have ensured this. */
  452. gdb_assert (nparams == 0);
  453. fn->set_is_prototyped (true);
  454. }
  455. else
  456. fn->set_is_prototyped (true);
  457. }
  458. fn->set_num_fields (nparams);
  459. fn->set_fields
  460. ((struct field *) TYPE_ZALLOC (fn, nparams * sizeof (struct field)));
  461. for (i = 0; i < nparams; ++i)
  462. fn->field (i).set_type (param_types[i]);
  463. return fn;
  464. }
  465. /* Identify address space identifier by name -- return a
  466. type_instance_flags. */
  467. type_instance_flags
  468. address_space_name_to_type_instance_flags (struct gdbarch *gdbarch,
  469. const char *space_identifier)
  470. {
  471. type_instance_flags type_flags;
  472. /* Check for known address space delimiters. */
  473. if (!strcmp (space_identifier, "code"))
  474. return TYPE_INSTANCE_FLAG_CODE_SPACE;
  475. else if (!strcmp (space_identifier, "data"))
  476. return TYPE_INSTANCE_FLAG_DATA_SPACE;
  477. else if (gdbarch_address_class_name_to_type_flags_p (gdbarch)
  478. && gdbarch_address_class_name_to_type_flags (gdbarch,
  479. space_identifier,
  480. &type_flags))
  481. return type_flags;
  482. else
  483. error (_("Unknown address space specifier: \"%s\""), space_identifier);
  484. }
  485. /* Identify address space identifier by type_instance_flags and return
  486. the string version of the adress space name. */
  487. const char *
  488. address_space_type_instance_flags_to_name (struct gdbarch *gdbarch,
  489. type_instance_flags space_flag)
  490. {
  491. if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE)
  492. return "code";
  493. else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE)
  494. return "data";
  495. else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
  496. && gdbarch_address_class_type_flags_to_name_p (gdbarch))
  497. return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag);
  498. else
  499. return NULL;
  500. }
  501. /* Create a new type with instance flags NEW_FLAGS, based on TYPE.
  502. If STORAGE is non-NULL, create the new type instance there.
  503. STORAGE must be in the same obstack as TYPE. */
  504. static struct type *
  505. make_qualified_type (struct type *type, type_instance_flags new_flags,
  506. struct type *storage)
  507. {
  508. struct type *ntype;
  509. ntype = type;
  510. do
  511. {
  512. if (ntype->instance_flags () == new_flags)
  513. return ntype;
  514. ntype = TYPE_CHAIN (ntype);
  515. }
  516. while (ntype != type);
  517. /* Create a new type instance. */
  518. if (storage == NULL)
  519. ntype = alloc_type_instance (type);
  520. else
  521. {
  522. /* If STORAGE was provided, it had better be in the same objfile
  523. as TYPE. Otherwise, we can't link it into TYPE's cv chain:
  524. if one objfile is freed and the other kept, we'd have
  525. dangling pointers. */
  526. gdb_assert (type->objfile_owner () == storage->objfile_owner ());
  527. ntype = storage;
  528. TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type);
  529. TYPE_CHAIN (ntype) = ntype;
  530. }
  531. /* Pointers or references to the original type are not relevant to
  532. the new type. */
  533. TYPE_POINTER_TYPE (ntype) = (struct type *) 0;
  534. TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0;
  535. /* Chain the new qualified type to the old type. */
  536. TYPE_CHAIN (ntype) = TYPE_CHAIN (type);
  537. TYPE_CHAIN (type) = ntype;
  538. /* Now set the instance flags and return the new type. */
  539. ntype->set_instance_flags (new_flags);
  540. /* Set length of new type to that of the original type. */
  541. TYPE_LENGTH (ntype) = TYPE_LENGTH (type);
  542. return ntype;
  543. }
  544. /* Make an address-space-delimited variant of a type -- a type that
  545. is identical to the one supplied except that it has an address
  546. space attribute attached to it (such as "code" or "data").
  547. The space attributes "code" and "data" are for Harvard
  548. architectures. The address space attributes are for architectures
  549. which have alternately sized pointers or pointers with alternate
  550. representations. */
  551. struct type *
  552. make_type_with_address_space (struct type *type,
  553. type_instance_flags space_flag)
  554. {
  555. type_instance_flags new_flags = ((type->instance_flags ()
  556. & ~(TYPE_INSTANCE_FLAG_CODE_SPACE
  557. | TYPE_INSTANCE_FLAG_DATA_SPACE
  558. | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL))
  559. | space_flag);
  560. return make_qualified_type (type, new_flags, NULL);
  561. }
  562. /* Make a "c-v" variant of a type -- a type that is identical to the
  563. one supplied except that it may have const or volatile attributes
  564. CNST is a flag for setting the const attribute
  565. VOLTL is a flag for setting the volatile attribute
  566. TYPE is the base type whose variant we are creating.
  567. If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to
  568. storage to hold the new qualified type; *TYPEPTR and TYPE must be
  569. in the same objfile. Otherwise, allocate fresh memory for the new
  570. type whereever TYPE lives. If TYPEPTR is non-zero, set it to the
  571. new type we construct. */
  572. struct type *
  573. make_cv_type (int cnst, int voltl,
  574. struct type *type,
  575. struct type **typeptr)
  576. {
  577. struct type *ntype; /* New type */
  578. type_instance_flags new_flags = (type->instance_flags ()
  579. & ~(TYPE_INSTANCE_FLAG_CONST
  580. | TYPE_INSTANCE_FLAG_VOLATILE));
  581. if (cnst)
  582. new_flags |= TYPE_INSTANCE_FLAG_CONST;
  583. if (voltl)
  584. new_flags |= TYPE_INSTANCE_FLAG_VOLATILE;
  585. if (typeptr && *typeptr != NULL)
  586. {
  587. /* TYPE and *TYPEPTR must be in the same objfile. We can't have
  588. a C-V variant chain that threads across objfiles: if one
  589. objfile gets freed, then the other has a broken C-V chain.
  590. This code used to try to copy over the main type from TYPE to
  591. *TYPEPTR if they were in different objfiles, but that's
  592. wrong, too: TYPE may have a field list or member function
  593. lists, which refer to types of their own, etc. etc. The
  594. whole shebang would need to be copied over recursively; you
  595. can't have inter-objfile pointers. The only thing to do is
  596. to leave stub types as stub types, and look them up afresh by
  597. name each time you encounter them. */
  598. gdb_assert ((*typeptr)->objfile_owner () == type->objfile_owner ());
  599. }
  600. ntype = make_qualified_type (type, new_flags,
  601. typeptr ? *typeptr : NULL);
  602. if (typeptr != NULL)
  603. *typeptr = ntype;
  604. return ntype;
  605. }
  606. /* Make a 'restrict'-qualified version of TYPE. */
  607. struct type *
  608. make_restrict_type (struct type *type)
  609. {
  610. return make_qualified_type (type,
  611. (type->instance_flags ()
  612. | TYPE_INSTANCE_FLAG_RESTRICT),
  613. NULL);
  614. }
  615. /* Make a type without const, volatile, or restrict. */
  616. struct type *
  617. make_unqualified_type (struct type *type)
  618. {
  619. return make_qualified_type (type,
  620. (type->instance_flags ()
  621. & ~(TYPE_INSTANCE_FLAG_CONST
  622. | TYPE_INSTANCE_FLAG_VOLATILE
  623. | TYPE_INSTANCE_FLAG_RESTRICT)),
  624. NULL);
  625. }
  626. /* Make a '_Atomic'-qualified version of TYPE. */
  627. struct type *
  628. make_atomic_type (struct type *type)
  629. {
  630. return make_qualified_type (type,
  631. (type->instance_flags ()
  632. | TYPE_INSTANCE_FLAG_ATOMIC),
  633. NULL);
  634. }
  635. /* Replace the contents of ntype with the type *type. This changes the
  636. contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus
  637. the changes are propogated to all types in the TYPE_CHAIN.
  638. In order to build recursive types, it's inevitable that we'll need
  639. to update types in place --- but this sort of indiscriminate
  640. smashing is ugly, and needs to be replaced with something more
  641. controlled. TYPE_MAIN_TYPE is a step in this direction; it's not
  642. clear if more steps are needed. */
  643. void
  644. replace_type (struct type *ntype, struct type *type)
  645. {
  646. struct type *chain;
  647. /* These two types had better be in the same objfile. Otherwise,
  648. the assignment of one type's main type structure to the other
  649. will produce a type with references to objects (names; field
  650. lists; etc.) allocated on an objfile other than its own. */
  651. gdb_assert (ntype->objfile_owner () == type->objfile_owner ());
  652. *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type);
  653. /* The type length is not a part of the main type. Update it for
  654. each type on the variant chain. */
  655. chain = ntype;
  656. do
  657. {
  658. /* Assert that this element of the chain has no address-class bits
  659. set in its flags. Such type variants might have type lengths
  660. which are supposed to be different from the non-address-class
  661. variants. This assertion shouldn't ever be triggered because
  662. symbol readers which do construct address-class variants don't
  663. call replace_type(). */
  664. gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0);
  665. TYPE_LENGTH (chain) = TYPE_LENGTH (type);
  666. chain = TYPE_CHAIN (chain);
  667. }
  668. while (ntype != chain);
  669. /* Assert that the two types have equivalent instance qualifiers.
  670. This should be true for at least all of our debug readers. */
  671. gdb_assert (ntype->instance_flags () == type->instance_flags ());
  672. }
  673. /* Implement direct support for MEMBER_TYPE in GNU C++.
  674. May need to construct such a type if this is the first use.
  675. The TYPE is the type of the member. The DOMAIN is the type
  676. of the aggregate that the member belongs to. */
  677. struct type *
  678. lookup_memberptr_type (struct type *type, struct type *domain)
  679. {
  680. struct type *mtype;
  681. mtype = alloc_type_copy (type);
  682. smash_to_memberptr_type (mtype, domain, type);
  683. return mtype;
  684. }
  685. /* Return a pointer-to-method type, for a method of type TO_TYPE. */
  686. struct type *
  687. lookup_methodptr_type (struct type *to_type)
  688. {
  689. struct type *mtype;
  690. mtype = alloc_type_copy (to_type);
  691. smash_to_methodptr_type (mtype, to_type);
  692. return mtype;
  693. }
  694. /* Allocate a stub method whose return type is TYPE. This apparently
  695. happens for speed of symbol reading, since parsing out the
  696. arguments to the method is cpu-intensive, the way we are doing it.
  697. So, we will fill in arguments later. This always returns a fresh
  698. type. */
  699. struct type *
  700. allocate_stub_method (struct type *type)
  701. {
  702. struct type *mtype;
  703. mtype = alloc_type_copy (type);
  704. mtype->set_code (TYPE_CODE_METHOD);
  705. TYPE_LENGTH (mtype) = 1;
  706. mtype->set_is_stub (true);
  707. TYPE_TARGET_TYPE (mtype) = type;
  708. /* TYPE_SELF_TYPE (mtype) = unknown yet */
  709. return mtype;
  710. }
  711. /* See gdbtypes.h. */
  712. bool
  713. operator== (const dynamic_prop &l, const dynamic_prop &r)
  714. {
  715. if (l.kind () != r.kind ())
  716. return false;
  717. switch (l.kind ())
  718. {
  719. case PROP_UNDEFINED:
  720. return true;
  721. case PROP_CONST:
  722. return l.const_val () == r.const_val ();
  723. case PROP_ADDR_OFFSET:
  724. case PROP_LOCEXPR:
  725. case PROP_LOCLIST:
  726. return l.baton () == r.baton ();
  727. case PROP_VARIANT_PARTS:
  728. return l.variant_parts () == r.variant_parts ();
  729. case PROP_TYPE:
  730. return l.original_type () == r.original_type ();
  731. }
  732. gdb_assert_not_reached ("unhandled dynamic_prop kind");
  733. }
  734. /* See gdbtypes.h. */
  735. bool
  736. operator== (const range_bounds &l, const range_bounds &r)
  737. {
  738. #define FIELD_EQ(FIELD) (l.FIELD == r.FIELD)
  739. return (FIELD_EQ (low)
  740. && FIELD_EQ (high)
  741. && FIELD_EQ (flag_upper_bound_is_count)
  742. && FIELD_EQ (flag_bound_evaluated)
  743. && FIELD_EQ (bias));
  744. #undef FIELD_EQ
  745. }
  746. /* Create a range type with a dynamic range from LOW_BOUND to
  747. HIGH_BOUND, inclusive. See create_range_type for further details. */
  748. struct type *
  749. create_range_type (struct type *result_type, struct type *index_type,
  750. const struct dynamic_prop *low_bound,
  751. const struct dynamic_prop *high_bound,
  752. LONGEST bias)
  753. {
  754. /* The INDEX_TYPE should be a type capable of holding the upper and lower
  755. bounds, as such a zero sized, or void type makes no sense. */
  756. gdb_assert (index_type->code () != TYPE_CODE_VOID);
  757. gdb_assert (TYPE_LENGTH (index_type) > 0);
  758. if (result_type == NULL)
  759. result_type = alloc_type_copy (index_type);
  760. result_type->set_code (TYPE_CODE_RANGE);
  761. TYPE_TARGET_TYPE (result_type) = index_type;
  762. if (index_type->is_stub ())
  763. result_type->set_target_is_stub (true);
  764. else
  765. TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
  766. range_bounds *bounds
  767. = (struct range_bounds *) TYPE_ZALLOC (result_type, sizeof (range_bounds));
  768. bounds->low = *low_bound;
  769. bounds->high = *high_bound;
  770. bounds->bias = bias;
  771. bounds->stride.set_const_val (0);
  772. result_type->set_bounds (bounds);
  773. if (index_type->code () == TYPE_CODE_FIXED_POINT)
  774. result_type->set_is_unsigned (index_type->is_unsigned ());
  775. /* Note that the signed-ness of a range type can't simply be copied
  776. from the underlying type. Consider a case where the underlying
  777. type is 'int', but the range type can hold 0..65535, and where
  778. the range is further specified to fit into 16 bits. In this
  779. case, if we copy the underlying type's sign, then reading some
  780. range values will cause an unwanted sign extension. So, we have
  781. some heuristics here instead. */
  782. else if (low_bound->kind () == PROP_CONST && low_bound->const_val () >= 0)
  783. result_type->set_is_unsigned (true);
  784. /* Ada allows the declaration of range types whose upper bound is
  785. less than the lower bound, so checking the lower bound is not
  786. enough. Make sure we do not mark a range type whose upper bound
  787. is negative as unsigned. */
  788. if (high_bound->kind () == PROP_CONST && high_bound->const_val () < 0)
  789. result_type->set_is_unsigned (false);
  790. result_type->set_endianity_is_not_default
  791. (index_type->endianity_is_not_default ());
  792. return result_type;
  793. }
  794. /* See gdbtypes.h. */
  795. struct type *
  796. create_range_type_with_stride (struct type *result_type,
  797. struct type *index_type,
  798. const struct dynamic_prop *low_bound,
  799. const struct dynamic_prop *high_bound,
  800. LONGEST bias,
  801. const struct dynamic_prop *stride,
  802. bool byte_stride_p)
  803. {
  804. result_type = create_range_type (result_type, index_type, low_bound,
  805. high_bound, bias);
  806. gdb_assert (stride != nullptr);
  807. result_type->bounds ()->stride = *stride;
  808. result_type->bounds ()->flag_is_byte_stride = byte_stride_p;
  809. return result_type;
  810. }
  811. /* Create a range type using either a blank type supplied in
  812. RESULT_TYPE, or creating a new type, inheriting the objfile from
  813. INDEX_TYPE.
  814. Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
  815. to HIGH_BOUND, inclusive.
  816. FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  817. sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
  818. struct type *
  819. create_static_range_type (struct type *result_type, struct type *index_type,
  820. LONGEST low_bound, LONGEST high_bound)
  821. {
  822. struct dynamic_prop low, high;
  823. low.set_const_val (low_bound);
  824. high.set_const_val (high_bound);
  825. result_type = create_range_type (result_type, index_type, &low, &high, 0);
  826. return result_type;
  827. }
  828. /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values
  829. are static, otherwise returns 0. */
  830. static bool
  831. has_static_range (const struct range_bounds *bounds)
  832. {
  833. /* If the range doesn't have a defined stride then its stride field will
  834. be initialized to the constant 0. */
  835. return (bounds->low.kind () == PROP_CONST
  836. && bounds->high.kind () == PROP_CONST
  837. && bounds->stride.kind () == PROP_CONST);
  838. }
  839. /* See gdbtypes.h. */
  840. gdb::optional<LONGEST>
  841. get_discrete_low_bound (struct type *type)
  842. {
  843. type = check_typedef (type);
  844. switch (type->code ())
  845. {
  846. case TYPE_CODE_RANGE:
  847. {
  848. /* This function only works for ranges with a constant low bound. */
  849. if (type->bounds ()->low.kind () != PROP_CONST)
  850. return {};
  851. LONGEST low = type->bounds ()->low.const_val ();
  852. if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM)
  853. {
  854. gdb::optional<LONGEST> low_pos
  855. = discrete_position (TYPE_TARGET_TYPE (type), low);
  856. if (low_pos.has_value ())
  857. low = *low_pos;
  858. }
  859. return low;
  860. }
  861. case TYPE_CODE_ENUM:
  862. {
  863. if (type->num_fields () > 0)
  864. {
  865. /* The enums may not be sorted by value, so search all
  866. entries. */
  867. LONGEST low = type->field (0).loc_enumval ();
  868. for (int i = 0; i < type->num_fields (); i++)
  869. {
  870. if (type->field (i).loc_enumval () < low)
  871. low = type->field (i).loc_enumval ();
  872. }
  873. /* Set unsigned indicator if warranted. */
  874. if (low >= 0)
  875. type->set_is_unsigned (true);
  876. return low;
  877. }
  878. else
  879. return 0;
  880. }
  881. case TYPE_CODE_BOOL:
  882. return 0;
  883. case TYPE_CODE_INT:
  884. if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
  885. return {};
  886. if (!type->is_unsigned ())
  887. return -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
  888. /* fall through */
  889. case TYPE_CODE_CHAR:
  890. return 0;
  891. default:
  892. return {};
  893. }
  894. }
  895. /* See gdbtypes.h. */
  896. gdb::optional<LONGEST>
  897. get_discrete_high_bound (struct type *type)
  898. {
  899. type = check_typedef (type);
  900. switch (type->code ())
  901. {
  902. case TYPE_CODE_RANGE:
  903. {
  904. /* This function only works for ranges with a constant high bound. */
  905. if (type->bounds ()->high.kind () != PROP_CONST)
  906. return {};
  907. LONGEST high = type->bounds ()->high.const_val ();
  908. if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM)
  909. {
  910. gdb::optional<LONGEST> high_pos
  911. = discrete_position (TYPE_TARGET_TYPE (type), high);
  912. if (high_pos.has_value ())
  913. high = *high_pos;
  914. }
  915. return high;
  916. }
  917. case TYPE_CODE_ENUM:
  918. {
  919. if (type->num_fields () > 0)
  920. {
  921. /* The enums may not be sorted by value, so search all
  922. entries. */
  923. LONGEST high = type->field (0).loc_enumval ();
  924. for (int i = 0; i < type->num_fields (); i++)
  925. {
  926. if (type->field (i).loc_enumval () > high)
  927. high = type->field (i).loc_enumval ();
  928. }
  929. return high;
  930. }
  931. else
  932. return -1;
  933. }
  934. case TYPE_CODE_BOOL:
  935. return 1;
  936. case TYPE_CODE_INT:
  937. if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
  938. return {};
  939. if (!type->is_unsigned ())
  940. {
  941. LONGEST low = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
  942. return -low - 1;
  943. }
  944. /* fall through */
  945. case TYPE_CODE_CHAR:
  946. {
  947. /* This round-about calculation is to avoid shifting by
  948. TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
  949. if TYPE_LENGTH (type) == sizeof (LONGEST). */
  950. LONGEST high = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
  951. return (high - 1) | high;
  952. }
  953. default:
  954. return {};
  955. }
  956. }
  957. /* See gdbtypes.h. */
  958. bool
  959. get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
  960. {
  961. gdb::optional<LONGEST> low = get_discrete_low_bound (type);
  962. if (!low.has_value ())
  963. return false;
  964. gdb::optional<LONGEST> high = get_discrete_high_bound (type);
  965. if (!high.has_value ())
  966. return false;
  967. *lowp = *low;
  968. *highp = *high;
  969. return true;
  970. }
  971. /* See gdbtypes.h */
  972. bool
  973. get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
  974. {
  975. struct type *index = type->index_type ();
  976. LONGEST low = 0;
  977. LONGEST high = 0;
  978. if (index == NULL)
  979. return false;
  980. if (!get_discrete_bounds (index, &low, &high))
  981. return false;
  982. if (low_bound)
  983. *low_bound = low;
  984. if (high_bound)
  985. *high_bound = high;
  986. return true;
  987. }
  988. /* Assuming that TYPE is a discrete type and VAL is a valid integer
  989. representation of a value of this type, save the corresponding
  990. position number in POS.
  991. Its differs from VAL only in the case of enumeration types. In
  992. this case, the position number of the value of the first listed
  993. enumeration literal is zero; the position number of the value of
  994. each subsequent enumeration literal is one more than that of its
  995. predecessor in the list.
  996. Return 1 if the operation was successful. Return zero otherwise,
  997. in which case the value of POS is unmodified.
  998. */
  999. gdb::optional<LONGEST>
  1000. discrete_position (struct type *type, LONGEST val)
  1001. {
  1002. if (type->code () == TYPE_CODE_RANGE)
  1003. type = TYPE_TARGET_TYPE (type);
  1004. if (type->code () == TYPE_CODE_ENUM)
  1005. {
  1006. int i;
  1007. for (i = 0; i < type->num_fields (); i += 1)
  1008. {
  1009. if (val == type->field (i).loc_enumval ())
  1010. return i;
  1011. }
  1012. /* Invalid enumeration value. */
  1013. return {};
  1014. }
  1015. else
  1016. return val;
  1017. }
  1018. /* If the array TYPE has static bounds calculate and update its
  1019. size, then return true. Otherwise return false and leave TYPE
  1020. unchanged. */
  1021. static bool
  1022. update_static_array_size (struct type *type)
  1023. {
  1024. gdb_assert (type->code () == TYPE_CODE_ARRAY);
  1025. struct type *range_type = type->index_type ();
  1026. if (type->dyn_prop (DYN_PROP_BYTE_STRIDE) == nullptr
  1027. && has_static_range (range_type->bounds ())
  1028. && (!type_not_associated (type)
  1029. && !type_not_allocated (type)))
  1030. {
  1031. LONGEST low_bound, high_bound;
  1032. int stride;
  1033. struct type *element_type;
  1034. stride = type->bit_stride ();
  1035. if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
  1036. low_bound = high_bound = 0;
  1037. element_type = check_typedef (TYPE_TARGET_TYPE (type));
  1038. /* Be careful when setting the array length. Ada arrays can be
  1039. empty arrays with the high_bound being smaller than the low_bound.
  1040. In such cases, the array length should be zero. */
  1041. if (high_bound < low_bound)
  1042. TYPE_LENGTH (type) = 0;
  1043. else if (stride != 0)
  1044. {
  1045. /* Ensure that the type length is always positive, even in the
  1046. case where (for example in Fortran) we have a negative
  1047. stride. It is possible to have a single element array with a
  1048. negative stride in Fortran (this doesn't mean anything
  1049. special, it's still just a single element array) so do
  1050. consider that case when touching this code. */
  1051. LONGEST element_count = std::abs (high_bound - low_bound + 1);
  1052. TYPE_LENGTH (type)
  1053. = ((std::abs (stride) * element_count) + 7) / 8;
  1054. }
  1055. else
  1056. TYPE_LENGTH (type) =
  1057. TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
  1058. /* If this array's element is itself an array with a bit stride,
  1059. then we want to update this array's bit stride to reflect the
  1060. size of the sub-array. Otherwise, we'll end up using the
  1061. wrong size when trying to find elements of the outer
  1062. array. */
  1063. if (element_type->code () == TYPE_CODE_ARRAY
  1064. && TYPE_LENGTH (element_type) != 0
  1065. && TYPE_FIELD_BITSIZE (element_type, 0) != 0
  1066. && get_array_bounds (element_type, &low_bound, &high_bound)
  1067. && high_bound >= low_bound)
  1068. TYPE_FIELD_BITSIZE (type, 0)
  1069. = ((high_bound - low_bound + 1)
  1070. * TYPE_FIELD_BITSIZE (element_type, 0));
  1071. return true;
  1072. }
  1073. return false;
  1074. }
  1075. /* Create an array type using either a blank type supplied in
  1076. RESULT_TYPE, or creating a new type, inheriting the objfile from
  1077. RANGE_TYPE.
  1078. Elements will be of type ELEMENT_TYPE, the indices will be of type
  1079. RANGE_TYPE.
  1080. BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
  1081. This byte stride property is added to the resulting array type
  1082. as a DYN_PROP_BYTE_STRIDE. As a consequence, the BYTE_STRIDE_PROP
  1083. argument can only be used to create types that are objfile-owned
  1084. (see add_dyn_prop), meaning that either this function must be called
  1085. with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
  1086. BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
  1087. If BIT_STRIDE is not zero, build a packed array type whose element
  1088. size is BIT_STRIDE. Otherwise, ignore this parameter.
  1089. FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  1090. sure it is TYPE_CODE_UNDEF before we bash it into an array
  1091. type? */
  1092. struct type *
  1093. create_array_type_with_stride (struct type *result_type,
  1094. struct type *element_type,
  1095. struct type *range_type,
  1096. struct dynamic_prop *byte_stride_prop,
  1097. unsigned int bit_stride)
  1098. {
  1099. if (byte_stride_prop != NULL
  1100. && byte_stride_prop->kind () == PROP_CONST)
  1101. {
  1102. /* The byte stride is actually not dynamic. Pretend we were
  1103. called with bit_stride set instead of byte_stride_prop.
  1104. This will give us the same result type, while avoiding
  1105. the need to handle this as a special case. */
  1106. bit_stride = byte_stride_prop->const_val () * 8;
  1107. byte_stride_prop = NULL;
  1108. }
  1109. if (result_type == NULL)
  1110. result_type = alloc_type_copy (range_type);
  1111. result_type->set_code (TYPE_CODE_ARRAY);
  1112. TYPE_TARGET_TYPE (result_type) = element_type;
  1113. result_type->set_num_fields (1);
  1114. result_type->set_fields
  1115. ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
  1116. result_type->set_index_type (range_type);
  1117. if (byte_stride_prop != NULL)
  1118. result_type->add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop);
  1119. else if (bit_stride > 0)
  1120. TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride;
  1121. if (!update_static_array_size (result_type))
  1122. {
  1123. /* This type is dynamic and its length needs to be computed
  1124. on demand. In the meantime, avoid leaving the TYPE_LENGTH
  1125. undefined by setting it to zero. Although we are not expected
  1126. to trust TYPE_LENGTH in this case, setting the size to zero
  1127. allows us to avoid allocating objects of random sizes in case
  1128. we accidently do. */
  1129. TYPE_LENGTH (result_type) = 0;
  1130. }
  1131. /* TYPE_TARGET_STUB will take care of zero length arrays. */
  1132. if (TYPE_LENGTH (result_type) == 0)
  1133. result_type->set_target_is_stub (true);
  1134. return result_type;
  1135. }
  1136. /* Same as create_array_type_with_stride but with no bit_stride
  1137. (BIT_STRIDE = 0), thus building an unpacked array. */
  1138. struct type *
  1139. create_array_type (struct type *result_type,
  1140. struct type *element_type,
  1141. struct type *range_type)
  1142. {
  1143. return create_array_type_with_stride (result_type, element_type,
  1144. range_type, NULL, 0);
  1145. }
  1146. struct type *
  1147. lookup_array_range_type (struct type *element_type,
  1148. LONGEST low_bound, LONGEST high_bound)
  1149. {
  1150. struct type *index_type;
  1151. struct type *range_type;
  1152. if (element_type->is_objfile_owned ())
  1153. index_type = objfile_type (element_type->objfile_owner ())->builtin_int;
  1154. else
  1155. index_type = builtin_type (element_type->arch_owner ())->builtin_int;
  1156. range_type = create_static_range_type (NULL, index_type,
  1157. low_bound, high_bound);
  1158. return create_array_type (NULL, element_type, range_type);
  1159. }
  1160. /* Create a string type using either a blank type supplied in
  1161. RESULT_TYPE, or creating a new type. String types are similar
  1162. enough to array of char types that we can use create_array_type to
  1163. build the basic type and then bash it into a string type.
  1164. For fixed length strings, the range type contains 0 as the lower
  1165. bound and the length of the string minus one as the upper bound.
  1166. FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
  1167. sure it is TYPE_CODE_UNDEF before we bash it into a string
  1168. type? */
  1169. struct type *
  1170. create_string_type (struct type *result_type,
  1171. struct type *string_char_type,
  1172. struct type *range_type)
  1173. {
  1174. result_type = create_array_type (result_type,
  1175. string_char_type,
  1176. range_type);
  1177. result_type->set_code (TYPE_CODE_STRING);
  1178. return result_type;
  1179. }
  1180. struct type *
  1181. lookup_string_range_type (struct type *string_char_type,
  1182. LONGEST low_bound, LONGEST high_bound)
  1183. {
  1184. struct type *result_type;
  1185. result_type = lookup_array_range_type (string_char_type,
  1186. low_bound, high_bound);
  1187. result_type->set_code (TYPE_CODE_STRING);
  1188. return result_type;
  1189. }
  1190. struct type *
  1191. create_set_type (struct type *result_type, struct type *domain_type)
  1192. {
  1193. if (result_type == NULL)
  1194. result_type = alloc_type_copy (domain_type);
  1195. result_type->set_code (TYPE_CODE_SET);
  1196. result_type->set_num_fields (1);
  1197. result_type->set_fields
  1198. ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
  1199. if (!domain_type->is_stub ())
  1200. {
  1201. LONGEST low_bound, high_bound, bit_length;
  1202. if (!get_discrete_bounds (domain_type, &low_bound, &high_bound))
  1203. low_bound = high_bound = 0;
  1204. bit_length = high_bound - low_bound + 1;
  1205. TYPE_LENGTH (result_type)
  1206. = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
  1207. if (low_bound >= 0)
  1208. result_type->set_is_unsigned (true);
  1209. }
  1210. result_type->field (0).set_type (domain_type);
  1211. return result_type;
  1212. }
  1213. /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE
  1214. and any array types nested inside it. */
  1215. void
  1216. make_vector_type (struct type *array_type)
  1217. {
  1218. struct type *inner_array, *elt_type;
  1219. /* Find the innermost array type, in case the array is
  1220. multi-dimensional. */
  1221. inner_array = array_type;
  1222. while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
  1223. inner_array = TYPE_TARGET_TYPE (inner_array);
  1224. elt_type = TYPE_TARGET_TYPE (inner_array);
  1225. if (elt_type->code () == TYPE_CODE_INT)
  1226. {
  1227. type_instance_flags flags
  1228. = elt_type->instance_flags () | TYPE_INSTANCE_FLAG_NOTTEXT;
  1229. elt_type = make_qualified_type (elt_type, flags, NULL);
  1230. TYPE_TARGET_TYPE (inner_array) = elt_type;
  1231. }
  1232. array_type->set_is_vector (true);
  1233. }
  1234. struct type *
  1235. init_vector_type (struct type *elt_type, int n)
  1236. {
  1237. struct type *array_type;
  1238. array_type = lookup_array_range_type (elt_type, 0, n - 1);
  1239. make_vector_type (array_type);
  1240. return array_type;
  1241. }
  1242. /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE
  1243. belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too
  1244. confusing. "self" is a common enough replacement for "this".
  1245. TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
  1246. TYPE_CODE_METHOD. */
  1247. struct type *
  1248. internal_type_self_type (struct type *type)
  1249. {
  1250. switch (type->code ())
  1251. {
  1252. case TYPE_CODE_METHODPTR:
  1253. case TYPE_CODE_MEMBERPTR:
  1254. if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
  1255. return NULL;
  1256. gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
  1257. return TYPE_MAIN_TYPE (type)->type_specific.self_type;
  1258. case TYPE_CODE_METHOD:
  1259. if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
  1260. return NULL;
  1261. gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
  1262. return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type;
  1263. default:
  1264. gdb_assert_not_reached ("bad type");
  1265. }
  1266. }
  1267. /* Set the type of the class that TYPE belongs to.
  1268. In c++ this is the class of "this".
  1269. TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or
  1270. TYPE_CODE_METHOD. */
  1271. void
  1272. set_type_self_type (struct type *type, struct type *self_type)
  1273. {
  1274. switch (type->code ())
  1275. {
  1276. case TYPE_CODE_METHODPTR:
  1277. case TYPE_CODE_MEMBERPTR:
  1278. if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
  1279. TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE;
  1280. gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE);
  1281. TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type;
  1282. break;
  1283. case TYPE_CODE_METHOD:
  1284. if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE)
  1285. INIT_FUNC_SPECIFIC (type);
  1286. gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
  1287. TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type;
  1288. break;
  1289. default:
  1290. gdb_assert_not_reached ("bad type");
  1291. }
  1292. }
  1293. /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type
  1294. TO_TYPE. A member pointer is a wierd thing -- it amounts to a
  1295. typed offset into a struct, e.g. "an int at offset 8". A MEMBER
  1296. TYPE doesn't include the offset (that's the value of the MEMBER
  1297. itself), but does include the structure type into which it points
  1298. (for some reason).
  1299. When "smashing" the type, we preserve the objfile that the old type
  1300. pointed to, since we aren't changing where the type is actually
  1301. allocated. */
  1302. void
  1303. smash_to_memberptr_type (struct type *type, struct type *self_type,
  1304. struct type *to_type)
  1305. {
  1306. smash_type (type);
  1307. type->set_code (TYPE_CODE_MEMBERPTR);
  1308. TYPE_TARGET_TYPE (type) = to_type;
  1309. set_type_self_type (type, self_type);
  1310. /* Assume that a data member pointer is the same size as a normal
  1311. pointer. */
  1312. TYPE_LENGTH (type) = gdbarch_ptr_bit (to_type->arch ()) / TARGET_CHAR_BIT;
  1313. }
  1314. /* Smash TYPE to be a type of pointer to methods type TO_TYPE.
  1315. When "smashing" the type, we preserve the objfile that the old type
  1316. pointed to, since we aren't changing where the type is actually
  1317. allocated. */
  1318. void
  1319. smash_to_methodptr_type (struct type *type, struct type *to_type)
  1320. {
  1321. smash_type (type);
  1322. type->set_code (TYPE_CODE_METHODPTR);
  1323. TYPE_TARGET_TYPE (type) = to_type;
  1324. set_type_self_type (type, TYPE_SELF_TYPE (to_type));
  1325. TYPE_LENGTH (type) = cplus_method_ptr_size (to_type);
  1326. }
  1327. /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE.
  1328. METHOD just means `function that gets an extra "this" argument'.
  1329. When "smashing" the type, we preserve the objfile that the old type
  1330. pointed to, since we aren't changing where the type is actually
  1331. allocated. */
  1332. void
  1333. smash_to_method_type (struct type *type, struct type *self_type,
  1334. struct type *to_type, struct field *args,
  1335. int nargs, int varargs)
  1336. {
  1337. smash_type (type);
  1338. type->set_code (TYPE_CODE_METHOD);
  1339. TYPE_TARGET_TYPE (type) = to_type;
  1340. set_type_self_type (type, self_type);
  1341. type->set_fields (args);
  1342. type->set_num_fields (nargs);
  1343. if (varargs)
  1344. type->set_has_varargs (true);
  1345. TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
  1346. }
  1347. /* A wrapper of TYPE_NAME which calls error if the type is anonymous.
  1348. Since GCC PR debug/47510 DWARF provides associated information to detect the
  1349. anonymous class linkage name from its typedef.
  1350. Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will
  1351. apply it itself. */
  1352. const char *
  1353. type_name_or_error (struct type *type)
  1354. {
  1355. struct type *saved_type = type;
  1356. const char *name;
  1357. struct objfile *objfile;
  1358. type = check_typedef (type);
  1359. name = type->name ();
  1360. if (name != NULL)
  1361. return name;
  1362. name = saved_type->name ();
  1363. objfile = saved_type->objfile_owner ();
  1364. error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"),
  1365. name ? name : "<anonymous>",
  1366. objfile ? objfile_name (objfile) : "<arch>");
  1367. }
  1368. /* Lookup a typedef or primitive type named NAME, visible in lexical
  1369. block BLOCK. If NOERR is nonzero, return zero if NAME is not
  1370. suitably defined. */
  1371. struct type *
  1372. lookup_typename (const struct language_defn *language,
  1373. const char *name,
  1374. const struct block *block, int noerr)
  1375. {
  1376. struct symbol *sym;
  1377. sym = lookup_symbol_in_language (name, block, VAR_DOMAIN,
  1378. language->la_language, NULL).symbol;
  1379. if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
  1380. return sym->type ();
  1381. if (noerr)
  1382. return NULL;
  1383. error (_("No type named %s."), name);
  1384. }
  1385. struct type *
  1386. lookup_unsigned_typename (const struct language_defn *language,
  1387. const char *name)
  1388. {
  1389. char *uns = (char *) alloca (strlen (name) + 10);
  1390. strcpy (uns, "unsigned ");
  1391. strcpy (uns + 9, name);
  1392. return lookup_typename (language, uns, NULL, 0);
  1393. }
  1394. struct type *
  1395. lookup_signed_typename (const struct language_defn *language, const char *name)
  1396. {
  1397. struct type *t;
  1398. char *uns = (char *) alloca (strlen (name) + 8);
  1399. strcpy (uns, "signed ");
  1400. strcpy (uns + 7, name);
  1401. t = lookup_typename (language, uns, NULL, 1);
  1402. /* If we don't find "signed FOO" just try again with plain "FOO". */
  1403. if (t != NULL)
  1404. return t;
  1405. return lookup_typename (language, name, NULL, 0);
  1406. }
  1407. /* Lookup a structure type named "struct NAME",
  1408. visible in lexical block BLOCK. */
  1409. struct type *
  1410. lookup_struct (const char *name, const struct block *block)
  1411. {
  1412. struct symbol *sym;
  1413. sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
  1414. if (sym == NULL)
  1415. {
  1416. error (_("No struct type named %s."), name);
  1417. }
  1418. if (sym->type ()->code () != TYPE_CODE_STRUCT)
  1419. {
  1420. error (_("This context has class, union or enum %s, not a struct."),
  1421. name);
  1422. }
  1423. return (sym->type ());
  1424. }
  1425. /* Lookup a union type named "union NAME",
  1426. visible in lexical block BLOCK. */
  1427. struct type *
  1428. lookup_union (const char *name, const struct block *block)
  1429. {
  1430. struct symbol *sym;
  1431. struct type *t;
  1432. sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
  1433. if (sym == NULL)
  1434. error (_("No union type named %s."), name);
  1435. t = sym->type ();
  1436. if (t->code () == TYPE_CODE_UNION)
  1437. return t;
  1438. /* If we get here, it's not a union. */
  1439. error (_("This context has class, struct or enum %s, not a union."),
  1440. name);
  1441. }
  1442. /* Lookup an enum type named "enum NAME",
  1443. visible in lexical block BLOCK. */
  1444. struct type *
  1445. lookup_enum (const char *name, const struct block *block)
  1446. {
  1447. struct symbol *sym;
  1448. sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol;
  1449. if (sym == NULL)
  1450. {
  1451. error (_("No enum type named %s."), name);
  1452. }
  1453. if (sym->type ()->code () != TYPE_CODE_ENUM)
  1454. {
  1455. error (_("This context has class, struct or union %s, not an enum."),
  1456. name);
  1457. }
  1458. return (sym->type ());
  1459. }
  1460. /* Lookup a template type named "template NAME<TYPE>",
  1461. visible in lexical block BLOCK. */
  1462. struct type *
  1463. lookup_template_type (const char *name, struct type *type,
  1464. const struct block *block)
  1465. {
  1466. struct symbol *sym;
  1467. char *nam = (char *)
  1468. alloca (strlen (name) + strlen (type->name ()) + 4);
  1469. strcpy (nam, name);
  1470. strcat (nam, "<");
  1471. strcat (nam, type->name ());
  1472. strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
  1473. sym = lookup_symbol (nam, block, VAR_DOMAIN, 0).symbol;
  1474. if (sym == NULL)
  1475. {
  1476. error (_("No template type named %s."), name);
  1477. }
  1478. if (sym->type ()->code () != TYPE_CODE_STRUCT)
  1479. {
  1480. error (_("This context has class, union or enum %s, not a struct."),
  1481. name);
  1482. }
  1483. return (sym->type ());
  1484. }
  1485. /* See gdbtypes.h. */
  1486. struct_elt
  1487. lookup_struct_elt (struct type *type, const char *name, int noerr)
  1488. {
  1489. int i;
  1490. for (;;)
  1491. {
  1492. type = check_typedef (type);
  1493. if (type->code () != TYPE_CODE_PTR
  1494. && type->code () != TYPE_CODE_REF)
  1495. break;
  1496. type = TYPE_TARGET_TYPE (type);
  1497. }
  1498. if (type->code () != TYPE_CODE_STRUCT
  1499. && type->code () != TYPE_CODE_UNION)
  1500. {
  1501. std::string type_name = type_to_string (type);
  1502. error (_("Type %s is not a structure or union type."),
  1503. type_name.c_str ());
  1504. }
  1505. for (i = type->num_fields () - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1506. {
  1507. const char *t_field_name = type->field (i).name ();
  1508. if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1509. {
  1510. return {&type->field (i), type->field (i).loc_bitpos ()};
  1511. }
  1512. else if (!t_field_name || *t_field_name == '\0')
  1513. {
  1514. struct_elt elt
  1515. = lookup_struct_elt (type->field (i).type (), name, 1);
  1516. if (elt.field != NULL)
  1517. {
  1518. elt.offset += type->field (i).loc_bitpos ();
  1519. return elt;
  1520. }
  1521. }
  1522. }
  1523. /* OK, it's not in this class. Recursively check the baseclasses. */
  1524. for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1525. {
  1526. struct_elt elt = lookup_struct_elt (TYPE_BASECLASS (type, i), name, 1);
  1527. if (elt.field != NULL)
  1528. return elt;
  1529. }
  1530. if (noerr)
  1531. return {nullptr, 0};
  1532. std::string type_name = type_to_string (type);
  1533. error (_("Type %s has no component named %s."), type_name.c_str (), name);
  1534. }
  1535. /* See gdbtypes.h. */
  1536. struct type *
  1537. lookup_struct_elt_type (struct type *type, const char *name, int noerr)
  1538. {
  1539. struct_elt elt = lookup_struct_elt (type, name, noerr);
  1540. if (elt.field != NULL)
  1541. return elt.field->type ();
  1542. else
  1543. return NULL;
  1544. }
  1545. /* Return the largest number representable by unsigned integer type TYPE. */
  1546. ULONGEST
  1547. get_unsigned_type_max (struct type *type)
  1548. {
  1549. unsigned int n;
  1550. type = check_typedef (type);
  1551. gdb_assert (type->code () == TYPE_CODE_INT && type->is_unsigned ());
  1552. gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST));
  1553. /* Written this way to avoid overflow. */
  1554. n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  1555. return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
  1556. }
  1557. /* Store in *MIN, *MAX the smallest and largest numbers representable by
  1558. signed integer type TYPE. */
  1559. void
  1560. get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
  1561. {
  1562. unsigned int n;
  1563. type = check_typedef (type);
  1564. gdb_assert (type->code () == TYPE_CODE_INT && !type->is_unsigned ());
  1565. gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST));
  1566. n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  1567. *min = -((ULONGEST) 1 << (n - 1));
  1568. *max = ((ULONGEST) 1 << (n - 1)) - 1;
  1569. }
  1570. /* Return the largest value representable by pointer type TYPE. */
  1571. CORE_ADDR
  1572. get_pointer_type_max (struct type *type)
  1573. {
  1574. unsigned int n;
  1575. type = check_typedef (type);
  1576. gdb_assert (type->code () == TYPE_CODE_PTR);
  1577. gdb_assert (TYPE_LENGTH (type) <= sizeof (CORE_ADDR));
  1578. n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  1579. return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
  1580. }
  1581. /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
  1582. cplus_stuff.vptr_fieldno.
  1583. cplus_stuff is initialized to cplus_struct_default which does not
  1584. set vptr_fieldno to -1 for portability reasons (IWBN to use C99
  1585. designated initializers). We cope with that here. */
  1586. int
  1587. internal_type_vptr_fieldno (struct type *type)
  1588. {
  1589. type = check_typedef (type);
  1590. gdb_assert (type->code () == TYPE_CODE_STRUCT
  1591. || type->code () == TYPE_CODE_UNION);
  1592. if (!HAVE_CPLUS_STRUCT (type))
  1593. return -1;
  1594. return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno;
  1595. }
  1596. /* Set the value of cplus_stuff.vptr_fieldno. */
  1597. void
  1598. set_type_vptr_fieldno (struct type *type, int fieldno)
  1599. {
  1600. type = check_typedef (type);
  1601. gdb_assert (type->code () == TYPE_CODE_STRUCT
  1602. || type->code () == TYPE_CODE_UNION);
  1603. if (!HAVE_CPLUS_STRUCT (type))
  1604. ALLOCATE_CPLUS_STRUCT_TYPE (type);
  1605. TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
  1606. }
  1607. /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of
  1608. cplus_stuff.vptr_basetype. */
  1609. struct type *
  1610. internal_type_vptr_basetype (struct type *type)
  1611. {
  1612. type = check_typedef (type);
  1613. gdb_assert (type->code () == TYPE_CODE_STRUCT
  1614. || type->code () == TYPE_CODE_UNION);
  1615. gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF);
  1616. return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype;
  1617. }
  1618. /* Set the value of cplus_stuff.vptr_basetype. */
  1619. void
  1620. set_type_vptr_basetype (struct type *type, struct type *basetype)
  1621. {
  1622. type = check_typedef (type);
  1623. gdb_assert (type->code () == TYPE_CODE_STRUCT
  1624. || type->code () == TYPE_CODE_UNION);
  1625. if (!HAVE_CPLUS_STRUCT (type))
  1626. ALLOCATE_CPLUS_STRUCT_TYPE (type);
  1627. TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
  1628. }
  1629. /* Lookup the vptr basetype/fieldno values for TYPE.
  1630. If found store vptr_basetype in *BASETYPEP if non-NULL, and return
  1631. vptr_fieldno. Also, if found and basetype is from the same objfile,
  1632. cache the results.
  1633. If not found, return -1 and ignore BASETYPEP.
  1634. Callers should be aware that in some cases (for example,
  1635. the type or one of its baseclasses is a stub type and we are
  1636. debugging a .o file, or the compiler uses DWARF-2 and is not GCC),
  1637. this function will not be able to find the
  1638. virtual function table pointer, and vptr_fieldno will remain -1 and
  1639. vptr_basetype will remain NULL or incomplete. */
  1640. int
  1641. get_vptr_fieldno (struct type *type, struct type **basetypep)
  1642. {
  1643. type = check_typedef (type);
  1644. if (TYPE_VPTR_FIELDNO (type) < 0)
  1645. {
  1646. int i;
  1647. /* We must start at zero in case the first (and only) baseclass
  1648. is virtual (and hence we cannot share the table pointer). */
  1649. for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
  1650. {
  1651. struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  1652. int fieldno;
  1653. struct type *basetype;
  1654. fieldno = get_vptr_fieldno (baseclass, &basetype);
  1655. if (fieldno >= 0)
  1656. {
  1657. /* If the type comes from a different objfile we can't cache
  1658. it, it may have a different lifetime. PR 2384 */
  1659. if (type->objfile_owner () == basetype->objfile_owner ())
  1660. {
  1661. set_type_vptr_fieldno (type, fieldno);
  1662. set_type_vptr_basetype (type, basetype);
  1663. }
  1664. if (basetypep)
  1665. *basetypep = basetype;
  1666. return fieldno;
  1667. }
  1668. }
  1669. /* Not found. */
  1670. return -1;
  1671. }
  1672. else
  1673. {
  1674. if (basetypep)
  1675. *basetypep = TYPE_VPTR_BASETYPE (type);
  1676. return TYPE_VPTR_FIELDNO (type);
  1677. }
  1678. }
  1679. static void
  1680. stub_noname_complaint (void)
  1681. {
  1682. complaint (_("stub type has NULL name"));
  1683. }
  1684. /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property
  1685. attached to it, and that property has a non-constant value. */
  1686. static int
  1687. array_type_has_dynamic_stride (struct type *type)
  1688. {
  1689. struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
  1690. return (prop != NULL && prop->kind () != PROP_CONST);
  1691. }
  1692. /* Worker for is_dynamic_type. */
  1693. static int
  1694. is_dynamic_type_internal (struct type *type, int top_level)
  1695. {
  1696. type = check_typedef (type);
  1697. /* We only want to recognize references at the outermost level. */
  1698. if (top_level && type->code () == TYPE_CODE_REF)
  1699. type = check_typedef (TYPE_TARGET_TYPE (type));
  1700. /* Types that have a dynamic TYPE_DATA_LOCATION are considered
  1701. dynamic, even if the type itself is statically defined.
  1702. From a user's point of view, this may appear counter-intuitive;
  1703. but it makes sense in this context, because the point is to determine
  1704. whether any part of the type needs to be resolved before it can
  1705. be exploited. */
  1706. if (TYPE_DATA_LOCATION (type) != NULL
  1707. && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR
  1708. || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST))
  1709. return 1;
  1710. if (TYPE_ASSOCIATED_PROP (type))
  1711. return 1;
  1712. if (TYPE_ALLOCATED_PROP (type))
  1713. return 1;
  1714. struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
  1715. if (prop != nullptr && prop->kind () != PROP_TYPE)
  1716. return 1;
  1717. if (TYPE_HAS_DYNAMIC_LENGTH (type))
  1718. return 1;
  1719. switch (type->code ())
  1720. {
  1721. case TYPE_CODE_RANGE:
  1722. {
  1723. /* A range type is obviously dynamic if it has at least one
  1724. dynamic bound. But also consider the range type to be
  1725. dynamic when its subtype is dynamic, even if the bounds
  1726. of the range type are static. It allows us to assume that
  1727. the subtype of a static range type is also static. */
  1728. return (!has_static_range (type->bounds ())
  1729. || is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0));
  1730. }
  1731. case TYPE_CODE_STRING:
  1732. /* Strings are very much like an array of characters, and can be
  1733. treated as one here. */
  1734. case TYPE_CODE_ARRAY:
  1735. {
  1736. gdb_assert (type->num_fields () == 1);
  1737. /* The array is dynamic if either the bounds are dynamic... */
  1738. if (is_dynamic_type_internal (type->index_type (), 0))
  1739. return 1;
  1740. /* ... or the elements it contains have a dynamic contents... */
  1741. if (is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0))
  1742. return 1;
  1743. /* ... or if it has a dynamic stride... */
  1744. if (array_type_has_dynamic_stride (type))
  1745. return 1;
  1746. return 0;
  1747. }
  1748. case TYPE_CODE_STRUCT:
  1749. case TYPE_CODE_UNION:
  1750. {
  1751. int i;
  1752. bool is_cplus = HAVE_CPLUS_STRUCT (type);
  1753. for (i = 0; i < type->num_fields (); ++i)
  1754. {
  1755. /* Static fields can be ignored here. */
  1756. if (field_is_static (&type->field (i)))
  1757. continue;
  1758. /* If the field has dynamic type, then so does TYPE. */
  1759. if (is_dynamic_type_internal (type->field (i).type (), 0))
  1760. return 1;
  1761. /* If the field is at a fixed offset, then it is not
  1762. dynamic. */
  1763. if (type->field (i).loc_kind () != FIELD_LOC_KIND_DWARF_BLOCK)
  1764. continue;
  1765. /* Do not consider C++ virtual base types to be dynamic
  1766. due to the field's offset being dynamic; these are
  1767. handled via other means. */
  1768. if (is_cplus && BASETYPE_VIA_VIRTUAL (type, i))
  1769. continue;
  1770. return 1;
  1771. }
  1772. }
  1773. break;
  1774. }
  1775. return 0;
  1776. }
  1777. /* See gdbtypes.h. */
  1778. int
  1779. is_dynamic_type (struct type *type)
  1780. {
  1781. return is_dynamic_type_internal (type, 1);
  1782. }
  1783. static struct type *resolve_dynamic_type_internal
  1784. (struct type *type, struct property_addr_info *addr_stack, int top_level);
  1785. /* Given a dynamic range type (dyn_range_type) and a stack of
  1786. struct property_addr_info elements, return a static version
  1787. of that type.
  1788. When RESOLVE_P is true then the returned static range is created by
  1789. actually evaluating any dynamic properties within the range type, while
  1790. when RESOLVE_P is false the returned static range has all of the bounds
  1791. and stride information set to undefined. The RESOLVE_P set to false
  1792. case will be used when evaluating a dynamic array that is not
  1793. allocated, or not associated, i.e. the bounds information might not be
  1794. initialized yet.
  1795. RANK is the array rank for which we are resolving this range, and is a
  1796. zero based count. The rank should never be negative.
  1797. */
  1798. static struct type *
  1799. resolve_dynamic_range (struct type *dyn_range_type,
  1800. struct property_addr_info *addr_stack,
  1801. int rank, bool resolve_p = true)
  1802. {
  1803. CORE_ADDR value;
  1804. struct type *static_range_type, *static_target_type;
  1805. struct dynamic_prop low_bound, high_bound, stride;
  1806. gdb_assert (dyn_range_type->code () == TYPE_CODE_RANGE);
  1807. gdb_assert (rank >= 0);
  1808. const struct dynamic_prop *prop = &dyn_range_type->bounds ()->low;
  1809. if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
  1810. { (CORE_ADDR) rank }))
  1811. low_bound.set_const_val (value);
  1812. else
  1813. low_bound.set_undefined ();
  1814. prop = &dyn_range_type->bounds ()->high;
  1815. if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
  1816. { (CORE_ADDR) rank }))
  1817. {
  1818. high_bound.set_const_val (value);
  1819. if (dyn_range_type->bounds ()->flag_upper_bound_is_count)
  1820. high_bound.set_const_val
  1821. (low_bound.const_val () + high_bound.const_val () - 1);
  1822. }
  1823. else
  1824. high_bound.set_undefined ();
  1825. bool byte_stride_p = dyn_range_type->bounds ()->flag_is_byte_stride;
  1826. prop = &dyn_range_type->bounds ()->stride;
  1827. if (resolve_p && dwarf2_evaluate_property (prop, NULL, addr_stack, &value,
  1828. { (CORE_ADDR) rank }))
  1829. {
  1830. stride.set_const_val (value);
  1831. /* If we have a bit stride that is not an exact number of bytes then
  1832. I really don't think this is going to work with current GDB, the
  1833. array indexing code in GDB seems to be pretty heavily tied to byte
  1834. offsets right now. Assuming 8 bits in a byte. */
  1835. struct gdbarch *gdbarch = dyn_range_type->arch ();
  1836. int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
  1837. if (!byte_stride_p && (value % (unit_size * 8)) != 0)
  1838. error (_("bit strides that are not a multiple of the byte size "
  1839. "are currently not supported"));
  1840. }
  1841. else
  1842. {
  1843. stride.set_undefined ();
  1844. byte_stride_p = true;
  1845. }
  1846. static_target_type
  1847. = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type),
  1848. addr_stack, 0);
  1849. LONGEST bias = dyn_range_type->bounds ()->bias;
  1850. static_range_type = create_range_type_with_stride
  1851. (copy_type (dyn_range_type), static_target_type,
  1852. &low_bound, &high_bound, bias, &stride, byte_stride_p);
  1853. static_range_type->bounds ()->flag_bound_evaluated = 1;
  1854. return static_range_type;
  1855. }
  1856. /* Helper function for resolve_dynamic_array_or_string. This function
  1857. resolves the properties for a single array at RANK within a nested array
  1858. of arrays structure. The RANK value is greater than or equal to 0, and
  1859. starts at it's maximum value and goes down by 1 for each recursive call
  1860. to this function. So, for a 3-dimensional array, the first call to this
  1861. function has RANK == 2, then we call ourselves recursively with RANK ==
  1862. 1, than again with RANK == 0, and at that point we should return.
  1863. TYPE is updated as the dynamic properties are resolved, and so, should
  1864. be a copy of the dynamic type, rather than the original dynamic type
  1865. itself.
  1866. ADDR_STACK is a stack of struct property_addr_info to be used if needed
  1867. during the dynamic resolution.
  1868. When RESOLVE_P is true then the dynamic properties of TYPE are
  1869. evaluated, otherwise the dynamic properties of TYPE are not evaluated,
  1870. instead we assume the array is not allocated/associated yet. */
  1871. static struct type *
  1872. resolve_dynamic_array_or_string_1 (struct type *type,
  1873. struct property_addr_info *addr_stack,
  1874. int rank, bool resolve_p)
  1875. {
  1876. CORE_ADDR value;
  1877. struct type *elt_type;
  1878. struct type *range_type;
  1879. struct type *ary_dim;
  1880. struct dynamic_prop *prop;
  1881. unsigned int bit_stride = 0;
  1882. /* For dynamic type resolution strings can be treated like arrays of
  1883. characters. */
  1884. gdb_assert (type->code () == TYPE_CODE_ARRAY
  1885. || type->code () == TYPE_CODE_STRING);
  1886. /* As the rank is a zero based count we expect this to never be
  1887. negative. */
  1888. gdb_assert (rank >= 0);
  1889. /* Resolve the allocated and associated properties before doing anything
  1890. else. If an array is not allocated or not associated then (at least
  1891. for Fortran) there is no guarantee that the data to define the upper
  1892. bound, lower bound, or stride will be correct. If RESOLVE_P is
  1893. already false at this point then this is not the first dimension of
  1894. the array and a more outer dimension has already marked this array as
  1895. not allocated/associated, as such we just ignore this property. This
  1896. is fine as GDB only checks the allocated/associated on the outer most
  1897. dimension of the array. */
  1898. prop = TYPE_ALLOCATED_PROP (type);
  1899. if (prop != NULL && resolve_p
  1900. && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
  1901. {
  1902. prop->set_const_val (value);
  1903. if (value == 0)
  1904. resolve_p = false;
  1905. }
  1906. prop = TYPE_ASSOCIATED_PROP (type);
  1907. if (prop != NULL && resolve_p
  1908. && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
  1909. {
  1910. prop->set_const_val (value);
  1911. if (value == 0)
  1912. resolve_p = false;
  1913. }
  1914. range_type = check_typedef (type->index_type ());
  1915. range_type
  1916. = resolve_dynamic_range (range_type, addr_stack, rank, resolve_p);
  1917. ary_dim = check_typedef (TYPE_TARGET_TYPE (type));
  1918. if (ary_dim != NULL && ary_dim->code () == TYPE_CODE_ARRAY)
  1919. {
  1920. ary_dim = copy_type (ary_dim);
  1921. elt_type = resolve_dynamic_array_or_string_1 (ary_dim, addr_stack,
  1922. rank - 1, resolve_p);
  1923. }
  1924. else
  1925. elt_type = TYPE_TARGET_TYPE (type);
  1926. prop = type->dyn_prop (DYN_PROP_BYTE_STRIDE);
  1927. if (prop != NULL && resolve_p)
  1928. {
  1929. if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
  1930. {
  1931. type->remove_dyn_prop (DYN_PROP_BYTE_STRIDE);
  1932. bit_stride = (unsigned int) (value * 8);
  1933. }
  1934. else
  1935. {
  1936. /* Could be a bug in our code, but it could also happen
  1937. if the DWARF info is not correct. Issue a warning,
  1938. and assume no byte/bit stride (leave bit_stride = 0). */
  1939. warning (_("cannot determine array stride for type %s"),
  1940. type->name () ? type->name () : "<no name>");
  1941. }
  1942. }
  1943. else
  1944. bit_stride = TYPE_FIELD_BITSIZE (type, 0);
  1945. return create_array_type_with_stride (type, elt_type, range_type, NULL,
  1946. bit_stride);
  1947. }
  1948. /* Resolve an array or string type with dynamic properties, return a new
  1949. type with the dynamic properties resolved to actual values. The
  1950. ADDR_STACK represents the location of the object being resolved. */
  1951. static struct type *
  1952. resolve_dynamic_array_or_string (struct type *type,
  1953. struct property_addr_info *addr_stack)
  1954. {
  1955. CORE_ADDR value;
  1956. int rank = 0;
  1957. /* For dynamic type resolution strings can be treated like arrays of
  1958. characters. */
  1959. gdb_assert (type->code () == TYPE_CODE_ARRAY
  1960. || type->code () == TYPE_CODE_STRING);
  1961. type = copy_type (type);
  1962. /* Resolve the rank property to get rank value. */
  1963. struct dynamic_prop *prop = TYPE_RANK_PROP (type);
  1964. if (dwarf2_evaluate_property (prop, nullptr, addr_stack, &value))
  1965. {
  1966. prop->set_const_val (value);
  1967. rank = value;
  1968. if (rank == 0)
  1969. {
  1970. /* The dynamic property list juggling below was from the original
  1971. patch. I don't understand what this is all about, so I've
  1972. commented it out for now and added the following error. */
  1973. error (_("failed to resolve dynamic array rank"));
  1974. }
  1975. else if (type->code () == TYPE_CODE_STRING && rank != 1)
  1976. {
  1977. /* What would this even mean? A string with a dynamic rank
  1978. greater than 1. */
  1979. error (_("unable to handle string with dynamic rank greater than 1"));
  1980. }
  1981. else if (rank > 1)
  1982. {
  1983. /* Arrays with dynamic rank are initially just an array type
  1984. with a target type that is the array element.
  1985. However, now we know the rank of the array we need to build
  1986. the array of arrays structure that GDB expects, that is we
  1987. need an array type that has a target which is an array type,
  1988. and so on, until eventually, we have the element type at the
  1989. end of the chain. Create all the additional array types here
  1990. by copying the top level array type. */
  1991. struct type *element_type = TYPE_TARGET_TYPE (type);
  1992. struct type *rank_type = type;
  1993. for (int i = 1; i < rank; i++)
  1994. {
  1995. TYPE_TARGET_TYPE (rank_type) = copy_type (rank_type);
  1996. rank_type = TYPE_TARGET_TYPE (rank_type);
  1997. }
  1998. TYPE_TARGET_TYPE (rank_type) = element_type;
  1999. }
  2000. }
  2001. else
  2002. {
  2003. rank = 1;
  2004. for (struct type *tmp_type = check_typedef (TYPE_TARGET_TYPE (type));
  2005. tmp_type->code () == TYPE_CODE_ARRAY;
  2006. tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type)))
  2007. ++rank;
  2008. }
  2009. /* The rank that we calculated above is actually a count of the number of
  2010. ranks. However, when we resolve the type of each individual array
  2011. rank we should actually use a rank "offset", e.g. an array with a rank
  2012. count of 1 (calculated above) will use the rank offset 0 in order to
  2013. resolve the details of the first array dimension. As a result, we
  2014. reduce the rank by 1 here. */
  2015. --rank;
  2016. return resolve_dynamic_array_or_string_1 (type, addr_stack, rank, true);
  2017. }
  2018. /* Resolve dynamic bounds of members of the union TYPE to static
  2019. bounds. ADDR_STACK is a stack of struct property_addr_info
  2020. to be used if needed during the dynamic resolution. */
  2021. static struct type *
  2022. resolve_dynamic_union (struct type *type,
  2023. struct property_addr_info *addr_stack)
  2024. {
  2025. struct type *resolved_type;
  2026. int i;
  2027. unsigned int max_len = 0;
  2028. gdb_assert (type->code () == TYPE_CODE_UNION);
  2029. resolved_type = copy_type (type);
  2030. resolved_type->set_fields
  2031. ((struct field *)
  2032. TYPE_ALLOC (resolved_type,
  2033. resolved_type->num_fields () * sizeof (struct field)));
  2034. memcpy (resolved_type->fields (),
  2035. type->fields (),
  2036. resolved_type->num_fields () * sizeof (struct field));
  2037. for (i = 0; i < resolved_type->num_fields (); ++i)
  2038. {
  2039. struct type *t;
  2040. if (field_is_static (&type->field (i)))
  2041. continue;
  2042. t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
  2043. addr_stack, 0);
  2044. resolved_type->field (i).set_type (t);
  2045. struct type *real_type = check_typedef (t);
  2046. if (TYPE_LENGTH (real_type) > max_len)
  2047. max_len = TYPE_LENGTH (real_type);
  2048. }
  2049. TYPE_LENGTH (resolved_type) = max_len;
  2050. return resolved_type;
  2051. }
  2052. /* See gdbtypes.h. */
  2053. bool
  2054. variant::matches (ULONGEST value, bool is_unsigned) const
  2055. {
  2056. for (const discriminant_range &range : discriminants)
  2057. if (range.contains (value, is_unsigned))
  2058. return true;
  2059. return false;
  2060. }
  2061. static void
  2062. compute_variant_fields_inner (struct type *type,
  2063. struct property_addr_info *addr_stack,
  2064. const variant_part &part,
  2065. std::vector<bool> &flags);
  2066. /* A helper function to determine which variant fields will be active.
  2067. This handles both the variant's direct fields, and any variant
  2068. parts embedded in this variant. TYPE is the type we're examining.
  2069. ADDR_STACK holds information about the concrete object. VARIANT is
  2070. the current variant to be handled. FLAGS is where the results are
  2071. stored -- this function sets the Nth element in FLAGS if the
  2072. corresponding field is enabled. ENABLED is whether this variant is
  2073. enabled or not. */
  2074. static void
  2075. compute_variant_fields_recurse (struct type *type,
  2076. struct property_addr_info *addr_stack,
  2077. const variant &variant,
  2078. std::vector<bool> &flags,
  2079. bool enabled)
  2080. {
  2081. for (int field = variant.first_field; field < variant.last_field; ++field)
  2082. flags[field] = enabled;
  2083. for (const variant_part &new_part : variant.parts)
  2084. {
  2085. if (enabled)
  2086. compute_variant_fields_inner (type, addr_stack, new_part, flags);
  2087. else
  2088. {
  2089. for (const auto &sub_variant : new_part.variants)
  2090. compute_variant_fields_recurse (type, addr_stack, sub_variant,
  2091. flags, enabled);
  2092. }
  2093. }
  2094. }
  2095. /* A helper function to determine which variant fields will be active.
  2096. This evaluates the discriminant, decides which variant (if any) is
  2097. active, and then updates FLAGS to reflect which fields should be
  2098. available. TYPE is the type we're examining. ADDR_STACK holds
  2099. information about the concrete object. VARIANT is the current
  2100. variant to be handled. FLAGS is where the results are stored --
  2101. this function sets the Nth element in FLAGS if the corresponding
  2102. field is enabled. */
  2103. static void
  2104. compute_variant_fields_inner (struct type *type,
  2105. struct property_addr_info *addr_stack,
  2106. const variant_part &part,
  2107. std::vector<bool> &flags)
  2108. {
  2109. /* Evaluate the discriminant. */
  2110. gdb::optional<ULONGEST> discr_value;
  2111. if (part.discriminant_index != -1)
  2112. {
  2113. int idx = part.discriminant_index;
  2114. if (type->field (idx).loc_kind () != FIELD_LOC_KIND_BITPOS)
  2115. error (_("Cannot determine struct field location"
  2116. " (invalid location kind)"));
  2117. if (addr_stack->valaddr.data () != NULL)
  2118. discr_value = unpack_field_as_long (type, addr_stack->valaddr.data (),
  2119. idx);
  2120. else
  2121. {
  2122. CORE_ADDR addr = (addr_stack->addr
  2123. + (type->field (idx).loc_bitpos ()
  2124. / TARGET_CHAR_BIT));
  2125. LONGEST bitsize = TYPE_FIELD_BITSIZE (type, idx);
  2126. LONGEST size = bitsize / 8;
  2127. if (size == 0)
  2128. size = TYPE_LENGTH (type->field (idx).type ());
  2129. gdb_byte bits[sizeof (ULONGEST)];
  2130. read_memory (addr, bits, size);
  2131. LONGEST bitpos = (type->field (idx).loc_bitpos ()
  2132. % TARGET_CHAR_BIT);
  2133. discr_value = unpack_bits_as_long (type->field (idx).type (),
  2134. bits, bitpos, bitsize);
  2135. }
  2136. }
  2137. /* Go through each variant and see which applies. */
  2138. const variant *default_variant = nullptr;
  2139. const variant *applied_variant = nullptr;
  2140. for (const auto &variant : part.variants)
  2141. {
  2142. if (variant.is_default ())
  2143. default_variant = &variant;
  2144. else if (discr_value.has_value ()
  2145. && variant.matches (*discr_value, part.is_unsigned))
  2146. {
  2147. applied_variant = &variant;
  2148. break;
  2149. }
  2150. }
  2151. if (applied_variant == nullptr)
  2152. applied_variant = default_variant;
  2153. for (const auto &variant : part.variants)
  2154. compute_variant_fields_recurse (type, addr_stack, variant,
  2155. flags, applied_variant == &variant);
  2156. }
  2157. /* Determine which variant fields are available in TYPE. The enabled
  2158. fields are stored in RESOLVED_TYPE. ADDR_STACK holds information
  2159. about the concrete object. PARTS describes the top-level variant
  2160. parts for this type. */
  2161. static void
  2162. compute_variant_fields (struct type *type,
  2163. struct type *resolved_type,
  2164. struct property_addr_info *addr_stack,
  2165. const gdb::array_view<variant_part> &parts)
  2166. {
  2167. /* Assume all fields are included by default. */
  2168. std::vector<bool> flags (resolved_type->num_fields (), true);
  2169. /* Now disable fields based on the variants that control them. */
  2170. for (const auto &part : parts)
  2171. compute_variant_fields_inner (type, addr_stack, part, flags);
  2172. resolved_type->set_num_fields
  2173. (std::count (flags.begin (), flags.end (), true));
  2174. resolved_type->set_fields
  2175. ((struct field *)
  2176. TYPE_ALLOC (resolved_type,
  2177. resolved_type->num_fields () * sizeof (struct field)));
  2178. int out = 0;
  2179. for (int i = 0; i < type->num_fields (); ++i)
  2180. {
  2181. if (!flags[i])
  2182. continue;
  2183. resolved_type->field (out) = type->field (i);
  2184. ++out;
  2185. }
  2186. }
  2187. /* Resolve dynamic bounds of members of the struct TYPE to static
  2188. bounds. ADDR_STACK is a stack of struct property_addr_info to
  2189. be used if needed during the dynamic resolution. */
  2190. static struct type *
  2191. resolve_dynamic_struct (struct type *type,
  2192. struct property_addr_info *addr_stack)
  2193. {
  2194. struct type *resolved_type;
  2195. int i;
  2196. unsigned resolved_type_bit_length = 0;
  2197. gdb_assert (type->code () == TYPE_CODE_STRUCT);
  2198. resolved_type = copy_type (type);
  2199. dynamic_prop *variant_prop = resolved_type->dyn_prop (DYN_PROP_VARIANT_PARTS);
  2200. if (variant_prop != nullptr && variant_prop->kind () == PROP_VARIANT_PARTS)
  2201. {
  2202. compute_variant_fields (type, resolved_type, addr_stack,
  2203. *variant_prop->variant_parts ());
  2204. /* We want to leave the property attached, so that the Rust code
  2205. can tell whether the type was originally an enum. */
  2206. variant_prop->set_original_type (type);
  2207. }
  2208. else
  2209. {
  2210. resolved_type->set_fields
  2211. ((struct field *)
  2212. TYPE_ALLOC (resolved_type,
  2213. resolved_type->num_fields () * sizeof (struct field)));
  2214. if (type->num_fields () > 0)
  2215. memcpy (resolved_type->fields (),
  2216. type->fields (),
  2217. resolved_type->num_fields () * sizeof (struct field));
  2218. }
  2219. for (i = 0; i < resolved_type->num_fields (); ++i)
  2220. {
  2221. unsigned new_bit_length;
  2222. struct property_addr_info pinfo;
  2223. if (field_is_static (&resolved_type->field (i)))
  2224. continue;
  2225. if (resolved_type->field (i).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
  2226. {
  2227. struct dwarf2_property_baton baton;
  2228. baton.property_type
  2229. = lookup_pointer_type (resolved_type->field (i).type ());
  2230. baton.locexpr = *resolved_type->field (i).loc_dwarf_block ();
  2231. struct dynamic_prop prop;
  2232. prop.set_locexpr (&baton);
  2233. CORE_ADDR addr;
  2234. if (dwarf2_evaluate_property (&prop, nullptr, addr_stack, &addr,
  2235. {addr_stack->addr}))
  2236. resolved_type->field (i).set_loc_bitpos
  2237. (TARGET_CHAR_BIT * (addr - addr_stack->addr));
  2238. }
  2239. /* As we know this field is not a static field, the field's
  2240. field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify
  2241. this is the case, but only trigger a simple error rather
  2242. than an internal error if that fails. While failing
  2243. that verification indicates a bug in our code, the error
  2244. is not severe enough to suggest to the user he stops
  2245. his debugging session because of it. */
  2246. if (resolved_type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
  2247. error (_("Cannot determine struct field location"
  2248. " (invalid location kind)"));
  2249. pinfo.type = check_typedef (resolved_type->field (i).type ());
  2250. size_t offset = resolved_type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
  2251. pinfo.valaddr = addr_stack->valaddr;
  2252. if (!pinfo.valaddr.empty ())
  2253. pinfo.valaddr = pinfo.valaddr.slice (offset);
  2254. pinfo.addr = addr_stack->addr + offset;
  2255. pinfo.next = addr_stack;
  2256. resolved_type->field (i).set_type
  2257. (resolve_dynamic_type_internal (resolved_type->field (i).type (),
  2258. &pinfo, 0));
  2259. gdb_assert (resolved_type->field (i).loc_kind ()
  2260. == FIELD_LOC_KIND_BITPOS);
  2261. new_bit_length = resolved_type->field (i).loc_bitpos ();
  2262. if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
  2263. new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
  2264. else
  2265. {
  2266. struct type *real_type
  2267. = check_typedef (resolved_type->field (i).type ());
  2268. new_bit_length += (TYPE_LENGTH (real_type) * TARGET_CHAR_BIT);
  2269. }
  2270. /* Normally, we would use the position and size of the last field
  2271. to determine the size of the enclosing structure. But GCC seems
  2272. to be encoding the position of some fields incorrectly when
  2273. the struct contains a dynamic field that is not placed last.
  2274. So we compute the struct size based on the field that has
  2275. the highest position + size - probably the best we can do. */
  2276. if (new_bit_length > resolved_type_bit_length)
  2277. resolved_type_bit_length = new_bit_length;
  2278. }
  2279. /* The length of a type won't change for fortran, but it does for C and Ada.
  2280. For fortran the size of dynamic fields might change over time but not the
  2281. type length of the structure. If we adapt it, we run into problems
  2282. when calculating the element offset for arrays of structs. */
  2283. if (current_language->la_language != language_fortran)
  2284. TYPE_LENGTH (resolved_type)
  2285. = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
  2286. /* The Ada language uses this field as a cache for static fixed types: reset
  2287. it as RESOLVED_TYPE must have its own static fixed type. */
  2288. TYPE_TARGET_TYPE (resolved_type) = NULL;
  2289. return resolved_type;
  2290. }
  2291. /* Worker for resolved_dynamic_type. */
  2292. static struct type *
  2293. resolve_dynamic_type_internal (struct type *type,
  2294. struct property_addr_info *addr_stack,
  2295. int top_level)
  2296. {
  2297. struct type *real_type = check_typedef (type);
  2298. struct type *resolved_type = nullptr;
  2299. struct dynamic_prop *prop;
  2300. CORE_ADDR value;
  2301. if (!is_dynamic_type_internal (real_type, top_level))
  2302. return type;
  2303. gdb::optional<CORE_ADDR> type_length;
  2304. prop = TYPE_DYNAMIC_LENGTH (type);
  2305. if (prop != NULL
  2306. && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
  2307. type_length = value;
  2308. if (type->code () == TYPE_CODE_TYPEDEF)
  2309. {
  2310. resolved_type = copy_type (type);
  2311. TYPE_TARGET_TYPE (resolved_type)
  2312. = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack,
  2313. top_level);
  2314. }
  2315. else
  2316. {
  2317. /* Before trying to resolve TYPE, make sure it is not a stub. */
  2318. type = real_type;
  2319. switch (type->code ())
  2320. {
  2321. case TYPE_CODE_REF:
  2322. {
  2323. struct property_addr_info pinfo;
  2324. pinfo.type = check_typedef (TYPE_TARGET_TYPE (type));
  2325. pinfo.valaddr = {};
  2326. if (addr_stack->valaddr.data () != NULL)
  2327. pinfo.addr = extract_typed_address (addr_stack->valaddr.data (),
  2328. type);
  2329. else
  2330. pinfo.addr = read_memory_typed_address (addr_stack->addr, type);
  2331. pinfo.next = addr_stack;
  2332. resolved_type = copy_type (type);
  2333. TYPE_TARGET_TYPE (resolved_type)
  2334. = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type),
  2335. &pinfo, top_level);
  2336. break;
  2337. }
  2338. case TYPE_CODE_STRING:
  2339. /* Strings are very much like an array of characters, and can be
  2340. treated as one here. */
  2341. case TYPE_CODE_ARRAY:
  2342. resolved_type = resolve_dynamic_array_or_string (type, addr_stack);
  2343. break;
  2344. case TYPE_CODE_RANGE:
  2345. /* Pass 0 for the rank value here, which indicates this is a
  2346. range for the first rank of an array. The assumption is that
  2347. this rank value is not actually required for the resolution of
  2348. the dynamic range, otherwise, we'd be resolving this range
  2349. within the context of a dynamic array. */
  2350. resolved_type = resolve_dynamic_range (type, addr_stack, 0);
  2351. break;
  2352. case TYPE_CODE_UNION:
  2353. resolved_type = resolve_dynamic_union (type, addr_stack);
  2354. break;
  2355. case TYPE_CODE_STRUCT:
  2356. resolved_type = resolve_dynamic_struct (type, addr_stack);
  2357. break;
  2358. }
  2359. }
  2360. if (resolved_type == nullptr)
  2361. return type;
  2362. if (type_length.has_value ())
  2363. {
  2364. TYPE_LENGTH (resolved_type) = *type_length;
  2365. resolved_type->remove_dyn_prop (DYN_PROP_BYTE_SIZE);
  2366. }
  2367. /* Resolve data_location attribute. */
  2368. prop = TYPE_DATA_LOCATION (resolved_type);
  2369. if (prop != NULL
  2370. && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
  2371. {
  2372. /* Start of Fortran hack. See comment in f-lang.h for what is going
  2373. on here.*/
  2374. if (current_language->la_language == language_fortran
  2375. && resolved_type->code () == TYPE_CODE_ARRAY)
  2376. value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
  2377. value);
  2378. /* End of Fortran hack. */
  2379. prop->set_const_val (value);
  2380. }
  2381. return resolved_type;
  2382. }
  2383. /* See gdbtypes.h */
  2384. struct type *
  2385. resolve_dynamic_type (struct type *type,
  2386. gdb::array_view<const gdb_byte> valaddr,
  2387. CORE_ADDR addr)
  2388. {
  2389. struct property_addr_info pinfo
  2390. = {check_typedef (type), valaddr, addr, NULL};
  2391. return resolve_dynamic_type_internal (type, &pinfo, 1);
  2392. }
  2393. /* See gdbtypes.h */
  2394. dynamic_prop *
  2395. type::dyn_prop (dynamic_prop_node_kind prop_kind) const
  2396. {
  2397. dynamic_prop_list *node = this->main_type->dyn_prop_list;
  2398. while (node != NULL)
  2399. {
  2400. if (node->prop_kind == prop_kind)
  2401. return &node->prop;
  2402. node = node->next;
  2403. }
  2404. return NULL;
  2405. }
  2406. /* See gdbtypes.h */
  2407. void
  2408. type::add_dyn_prop (dynamic_prop_node_kind prop_kind, dynamic_prop prop)
  2409. {
  2410. struct dynamic_prop_list *temp;
  2411. gdb_assert (this->is_objfile_owned ());
  2412. temp = XOBNEW (&this->objfile_owner ()->objfile_obstack,
  2413. struct dynamic_prop_list);
  2414. temp->prop_kind = prop_kind;
  2415. temp->prop = prop;
  2416. temp->next = this->main_type->dyn_prop_list;
  2417. this->main_type->dyn_prop_list = temp;
  2418. }
  2419. /* See gdbtypes.h. */
  2420. void
  2421. type::remove_dyn_prop (dynamic_prop_node_kind kind)
  2422. {
  2423. struct dynamic_prop_list *prev_node, *curr_node;
  2424. curr_node = this->main_type->dyn_prop_list;
  2425. prev_node = NULL;
  2426. while (NULL != curr_node)
  2427. {
  2428. if (curr_node->prop_kind == kind)
  2429. {
  2430. /* Update the linked list but don't free anything.
  2431. The property was allocated on objstack and it is not known
  2432. if we are on top of it. Nevertheless, everything is released
  2433. when the complete objstack is freed. */
  2434. if (NULL == prev_node)
  2435. this->main_type->dyn_prop_list = curr_node->next;
  2436. else
  2437. prev_node->next = curr_node->next;
  2438. return;
  2439. }
  2440. prev_node = curr_node;
  2441. curr_node = curr_node->next;
  2442. }
  2443. }
  2444. /* Find the real type of TYPE. This function returns the real type,
  2445. after removing all layers of typedefs, and completing opaque or stub
  2446. types. Completion changes the TYPE argument, but stripping of
  2447. typedefs does not.
  2448. Instance flags (e.g. const/volatile) are preserved as typedefs are
  2449. stripped. If necessary a new qualified form of the underlying type
  2450. is created.
  2451. NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has
  2452. not been computed and we're either in the middle of reading symbols, or
  2453. there was no name for the typedef in the debug info.
  2454. NOTE: Lookup of opaque types can throw errors for invalid symbol files.
  2455. QUITs in the symbol reading code can also throw.
  2456. Thus this function can throw an exception.
  2457. If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of
  2458. the target type.
  2459. If this is a stubbed struct (i.e. declared as struct foo *), see if
  2460. we can find a full definition in some other file. If so, copy this
  2461. definition, so we can use it in future. There used to be a comment
  2462. (but not any code) that if we don't find a full definition, we'd
  2463. set a flag so we don't spend time in the future checking the same
  2464. type. That would be a mistake, though--we might load in more
  2465. symbols which contain a full definition for the type. */
  2466. struct type *
  2467. check_typedef (struct type *type)
  2468. {
  2469. struct type *orig_type = type;
  2470. gdb_assert (type);
  2471. /* While we're removing typedefs, we don't want to lose qualifiers.
  2472. E.g., const/volatile. */
  2473. type_instance_flags instance_flags = type->instance_flags ();
  2474. while (type->code () == TYPE_CODE_TYPEDEF)
  2475. {
  2476. if (!TYPE_TARGET_TYPE (type))
  2477. {
  2478. const char *name;
  2479. struct symbol *sym;
  2480. /* It is dangerous to call lookup_symbol if we are currently
  2481. reading a symtab. Infinite recursion is one danger. */
  2482. if (currently_reading_symtab)
  2483. return make_qualified_type (type, instance_flags, NULL);
  2484. name = type->name ();
  2485. /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or
  2486. VAR_DOMAIN as appropriate? */
  2487. if (name == NULL)
  2488. {
  2489. stub_noname_complaint ();
  2490. return make_qualified_type (type, instance_flags, NULL);
  2491. }
  2492. sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
  2493. if (sym)
  2494. TYPE_TARGET_TYPE (type) = sym->type ();
  2495. else /* TYPE_CODE_UNDEF */
  2496. TYPE_TARGET_TYPE (type) = alloc_type_arch (type->arch ());
  2497. }
  2498. type = TYPE_TARGET_TYPE (type);
  2499. /* Preserve the instance flags as we traverse down the typedef chain.
  2500. Handling address spaces/classes is nasty, what do we do if there's a
  2501. conflict?
  2502. E.g., what if an outer typedef marks the type as class_1 and an inner
  2503. typedef marks the type as class_2?
  2504. This is the wrong place to do such error checking. We leave it to
  2505. the code that created the typedef in the first place to flag the
  2506. error. We just pick the outer address space (akin to letting the
  2507. outer cast in a chain of casting win), instead of assuming
  2508. "it can't happen". */
  2509. {
  2510. const type_instance_flags ALL_SPACES
  2511. = (TYPE_INSTANCE_FLAG_CODE_SPACE
  2512. | TYPE_INSTANCE_FLAG_DATA_SPACE);
  2513. const type_instance_flags ALL_CLASSES
  2514. = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL;
  2515. type_instance_flags new_instance_flags = type->instance_flags ();
  2516. /* Treat code vs data spaces and address classes separately. */
  2517. if ((instance_flags & ALL_SPACES) != 0)
  2518. new_instance_flags &= ~ALL_SPACES;
  2519. if ((instance_flags & ALL_CLASSES) != 0)
  2520. new_instance_flags &= ~ALL_CLASSES;
  2521. instance_flags |= new_instance_flags;
  2522. }
  2523. }
  2524. /* If this is a struct/class/union with no fields, then check
  2525. whether a full definition exists somewhere else. This is for
  2526. systems where a type definition with no fields is issued for such
  2527. types, instead of identifying them as stub types in the first
  2528. place. */
  2529. if (TYPE_IS_OPAQUE (type)
  2530. && opaque_type_resolution
  2531. && !currently_reading_symtab)
  2532. {
  2533. const char *name = type->name ();
  2534. struct type *newtype;
  2535. if (name == NULL)
  2536. {
  2537. stub_noname_complaint ();
  2538. return make_qualified_type (type, instance_flags, NULL);
  2539. }
  2540. newtype = lookup_transparent_type (name);
  2541. if (newtype)
  2542. {
  2543. /* If the resolved type and the stub are in the same
  2544. objfile, then replace the stub type with the real deal.
  2545. But if they're in separate objfiles, leave the stub
  2546. alone; we'll just look up the transparent type every time
  2547. we call check_typedef. We can't create pointers between
  2548. types allocated to different objfiles, since they may
  2549. have different lifetimes. Trying to copy NEWTYPE over to
  2550. TYPE's objfile is pointless, too, since you'll have to
  2551. move over any other types NEWTYPE refers to, which could
  2552. be an unbounded amount of stuff. */
  2553. if (newtype->objfile_owner () == type->objfile_owner ())
  2554. type = make_qualified_type (newtype, type->instance_flags (), type);
  2555. else
  2556. type = newtype;
  2557. }
  2558. }
  2559. /* Otherwise, rely on the stub flag being set for opaque/stubbed
  2560. types. */
  2561. else if (type->is_stub () && !currently_reading_symtab)
  2562. {
  2563. const char *name = type->name ();
  2564. /* FIXME: shouldn't we look in STRUCT_DOMAIN and/or VAR_DOMAIN
  2565. as appropriate? */
  2566. struct symbol *sym;
  2567. if (name == NULL)
  2568. {
  2569. stub_noname_complaint ();
  2570. return make_qualified_type (type, instance_flags, NULL);
  2571. }
  2572. sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol;
  2573. if (sym)
  2574. {
  2575. /* Same as above for opaque types, we can replace the stub
  2576. with the complete type only if they are in the same
  2577. objfile. */
  2578. if (sym->type ()->objfile_owner () == type->objfile_owner ())
  2579. type = make_qualified_type (sym->type (),
  2580. type->instance_flags (), type);
  2581. else
  2582. type = sym->type ();
  2583. }
  2584. }
  2585. if (type->target_is_stub ())
  2586. {
  2587. struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
  2588. if (target_type->is_stub () || target_type->target_is_stub ())
  2589. {
  2590. /* Nothing we can do. */
  2591. }
  2592. else if (type->code () == TYPE_CODE_RANGE)
  2593. {
  2594. TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
  2595. type->set_target_is_stub (false);
  2596. }
  2597. else if (type->code () == TYPE_CODE_ARRAY
  2598. && update_static_array_size (type))
  2599. type->set_target_is_stub (false);
  2600. }
  2601. type = make_qualified_type (type, instance_flags, NULL);
  2602. /* Cache TYPE_LENGTH for future use. */
  2603. TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
  2604. return type;
  2605. }
  2606. /* Parse a type expression in the string [P..P+LENGTH). If an error
  2607. occurs, silently return a void type. */
  2608. static struct type *
  2609. safe_parse_type (struct gdbarch *gdbarch, const char *p, int length)
  2610. {
  2611. struct ui_file *saved_gdb_stderr;
  2612. struct type *type = NULL; /* Initialize to keep gcc happy. */
  2613. /* Suppress error messages. */
  2614. saved_gdb_stderr = gdb_stderr;
  2615. gdb_stderr = &null_stream;
  2616. /* Call parse_and_eval_type() without fear of longjmp()s. */
  2617. try
  2618. {
  2619. type = parse_and_eval_type (p, length);
  2620. }
  2621. catch (const gdb_exception_error &except)
  2622. {
  2623. type = builtin_type (gdbarch)->builtin_void;
  2624. }
  2625. /* Stop suppressing error messages. */
  2626. gdb_stderr = saved_gdb_stderr;
  2627. return type;
  2628. }
  2629. /* Ugly hack to convert method stubs into method types.
  2630. He ain't kiddin'. This demangles the name of the method into a
  2631. string including argument types, parses out each argument type,
  2632. generates a string casting a zero to that type, evaluates the
  2633. string, and stuffs the resulting type into an argtype vector!!!
  2634. Then it knows the type of the whole function (including argument
  2635. types for overloading), which info used to be in the stab's but was
  2636. removed to hack back the space required for them. */
  2637. static void
  2638. check_stub_method (struct type *type, int method_id, int signature_id)
  2639. {
  2640. struct gdbarch *gdbarch = type->arch ();
  2641. struct fn_field *f;
  2642. char *mangled_name = gdb_mangle_name (type, method_id, signature_id);
  2643. gdb::unique_xmalloc_ptr<char> demangled_name
  2644. = gdb_demangle (mangled_name, DMGL_PARAMS | DMGL_ANSI);
  2645. char *argtypetext, *p;
  2646. int depth = 0, argcount = 1;
  2647. struct field *argtypes;
  2648. struct type *mtype;
  2649. /* Make sure we got back a function string that we can use. */
  2650. if (demangled_name)
  2651. p = strchr (demangled_name.get (), '(');
  2652. else
  2653. p = NULL;
  2654. if (demangled_name == NULL || p == NULL)
  2655. error (_("Internal: Cannot demangle mangled name `%s'."),
  2656. mangled_name);
  2657. /* Now, read in the parameters that define this type. */
  2658. p += 1;
  2659. argtypetext = p;
  2660. while (*p)
  2661. {
  2662. if (*p == '(' || *p == '<')
  2663. {
  2664. depth += 1;
  2665. }
  2666. else if (*p == ')' || *p == '>')
  2667. {
  2668. depth -= 1;
  2669. }
  2670. else if (*p == ',' && depth == 0)
  2671. {
  2672. argcount += 1;
  2673. }
  2674. p += 1;
  2675. }
  2676. /* If we read one argument and it was ``void'', don't count it. */
  2677. if (startswith (argtypetext, "(void)"))
  2678. argcount -= 1;
  2679. /* We need one extra slot, for the THIS pointer. */
  2680. argtypes = (struct field *)
  2681. TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field));
  2682. p = argtypetext;
  2683. /* Add THIS pointer for non-static methods. */
  2684. f = TYPE_FN_FIELDLIST1 (type, method_id);
  2685. if (TYPE_FN_FIELD_STATIC_P (f, signature_id))
  2686. argcount = 0;
  2687. else
  2688. {
  2689. argtypes[0].set_type (lookup_pointer_type (type));
  2690. argcount = 1;
  2691. }
  2692. if (*p != ')') /* () means no args, skip while. */
  2693. {
  2694. depth = 0;
  2695. while (*p)
  2696. {
  2697. if (depth <= 0 && (*p == ',' || *p == ')'))
  2698. {
  2699. /* Avoid parsing of ellipsis, they will be handled below.
  2700. Also avoid ``void'' as above. */
  2701. if (strncmp (argtypetext, "...", p - argtypetext) != 0
  2702. && strncmp (argtypetext, "void", p - argtypetext) != 0)
  2703. {
  2704. argtypes[argcount].set_type
  2705. (safe_parse_type (gdbarch, argtypetext, p - argtypetext));
  2706. argcount += 1;
  2707. }
  2708. argtypetext = p + 1;
  2709. }
  2710. if (*p == '(' || *p == '<')
  2711. {
  2712. depth += 1;
  2713. }
  2714. else if (*p == ')' || *p == '>')
  2715. {
  2716. depth -= 1;
  2717. }
  2718. p += 1;
  2719. }
  2720. }
  2721. TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name;
  2722. /* Now update the old "stub" type into a real type. */
  2723. mtype = TYPE_FN_FIELD_TYPE (f, signature_id);
  2724. /* MTYPE may currently be a function (TYPE_CODE_FUNC).
  2725. We want a method (TYPE_CODE_METHOD). */
  2726. smash_to_method_type (mtype, type, TYPE_TARGET_TYPE (mtype),
  2727. argtypes, argcount, p[-2] == '.');
  2728. mtype->set_is_stub (false);
  2729. TYPE_FN_FIELD_STUB (f, signature_id) = 0;
  2730. }
  2731. /* This is the external interface to check_stub_method, above. This
  2732. function unstubs all of the signatures for TYPE's METHOD_ID method
  2733. name. After calling this function TYPE_FN_FIELD_STUB will be
  2734. cleared for each signature and TYPE_FN_FIELDLIST_NAME will be
  2735. correct.
  2736. This function unfortunately can not die until stabs do. */
  2737. void
  2738. check_stub_method_group (struct type *type, int method_id)
  2739. {
  2740. int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id);
  2741. struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
  2742. for (int j = 0; j < len; j++)
  2743. {
  2744. if (TYPE_FN_FIELD_STUB (f, j))
  2745. check_stub_method (type, method_id, j);
  2746. }
  2747. }
  2748. /* Ensure it is in .rodata (if available) by working around GCC PR 44690. */
  2749. const struct cplus_struct_type cplus_struct_default = { };
  2750. void
  2751. allocate_cplus_struct_type (struct type *type)
  2752. {
  2753. if (HAVE_CPLUS_STRUCT (type))
  2754. /* Structure was already allocated. Nothing more to do. */
  2755. return;
  2756. TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF;
  2757. TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
  2758. TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
  2759. *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
  2760. set_type_vptr_fieldno (type, -1);
  2761. }
  2762. const struct gnat_aux_type gnat_aux_default =
  2763. { NULL };
  2764. /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF,
  2765. and allocate the associated gnat-specific data. The gnat-specific
  2766. data is also initialized to gnat_aux_default. */
  2767. void
  2768. allocate_gnat_aux_type (struct type *type)
  2769. {
  2770. TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF;
  2771. TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *)
  2772. TYPE_ALLOC (type, sizeof (struct gnat_aux_type));
  2773. *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default;
  2774. }
  2775. /* Helper function to initialize a newly allocated type. Set type code
  2776. to CODE and initialize the type-specific fields accordingly. */
  2777. static void
  2778. set_type_code (struct type *type, enum type_code code)
  2779. {
  2780. type->set_code (code);
  2781. switch (code)
  2782. {
  2783. case TYPE_CODE_STRUCT:
  2784. case TYPE_CODE_UNION:
  2785. case TYPE_CODE_NAMESPACE:
  2786. INIT_CPLUS_SPECIFIC (type);
  2787. break;
  2788. case TYPE_CODE_FLT:
  2789. TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT;
  2790. break;
  2791. case TYPE_CODE_FUNC:
  2792. INIT_FUNC_SPECIFIC (type);
  2793. break;
  2794. case TYPE_CODE_FIXED_POINT:
  2795. INIT_FIXED_POINT_SPECIFIC (type);
  2796. break;
  2797. }
  2798. }
  2799. /* Helper function to verify floating-point format and size.
  2800. BIT is the type size in bits; if BIT equals -1, the size is
  2801. determined by the floatformat. Returns size to be used. */
  2802. static int
  2803. verify_floatformat (int bit, const struct floatformat *floatformat)
  2804. {
  2805. gdb_assert (floatformat != NULL);
  2806. if (bit == -1)
  2807. bit = floatformat->totalsize;
  2808. gdb_assert (bit >= 0);
  2809. gdb_assert (bit >= floatformat->totalsize);
  2810. return bit;
  2811. }
  2812. /* Return the floating-point format for a floating-point variable of
  2813. type TYPE. */
  2814. const struct floatformat *
  2815. floatformat_from_type (const struct type *type)
  2816. {
  2817. gdb_assert (type->code () == TYPE_CODE_FLT);
  2818. gdb_assert (TYPE_FLOATFORMAT (type));
  2819. return TYPE_FLOATFORMAT (type);
  2820. }
  2821. /* Helper function to initialize the standard scalar types.
  2822. If NAME is non-NULL, then it is used to initialize the type name.
  2823. Note that NAME is not copied; it is required to have a lifetime at
  2824. least as long as OBJFILE. */
  2825. struct type *
  2826. init_type (struct objfile *objfile, enum type_code code, int bit,
  2827. const char *name)
  2828. {
  2829. struct type *type;
  2830. type = alloc_type (objfile);
  2831. set_type_code (type, code);
  2832. gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
  2833. TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
  2834. type->set_name (name);
  2835. return type;
  2836. }
  2837. /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE,
  2838. to use with variables that have no debug info. NAME is the type
  2839. name. */
  2840. static struct type *
  2841. init_nodebug_var_type (struct objfile *objfile, const char *name)
  2842. {
  2843. return init_type (objfile, TYPE_CODE_ERROR, 0, name);
  2844. }
  2845. /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE.
  2846. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  2847. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  2848. struct type *
  2849. init_integer_type (struct objfile *objfile,
  2850. int bit, int unsigned_p, const char *name)
  2851. {
  2852. struct type *t;
  2853. t = init_type (objfile, TYPE_CODE_INT, bit, name);
  2854. if (unsigned_p)
  2855. t->set_is_unsigned (true);
  2856. TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
  2857. TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
  2858. TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
  2859. return t;
  2860. }
  2861. /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE.
  2862. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  2863. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  2864. struct type *
  2865. init_character_type (struct objfile *objfile,
  2866. int bit, int unsigned_p, const char *name)
  2867. {
  2868. struct type *t;
  2869. t = init_type (objfile, TYPE_CODE_CHAR, bit, name);
  2870. if (unsigned_p)
  2871. t->set_is_unsigned (true);
  2872. return t;
  2873. }
  2874. /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE.
  2875. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  2876. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  2877. struct type *
  2878. init_boolean_type (struct objfile *objfile,
  2879. int bit, int unsigned_p, const char *name)
  2880. {
  2881. struct type *t;
  2882. t = init_type (objfile, TYPE_CODE_BOOL, bit, name);
  2883. if (unsigned_p)
  2884. t->set_is_unsigned (true);
  2885. TYPE_SPECIFIC_FIELD (t) = TYPE_SPECIFIC_INT;
  2886. TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_size = bit;
  2887. TYPE_MAIN_TYPE (t)->type_specific.int_stuff.bit_offset = 0;
  2888. return t;
  2889. }
  2890. /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE.
  2891. BIT is the type size in bits; if BIT equals -1, the size is
  2892. determined by the floatformat. NAME is the type name. Set the
  2893. TYPE_FLOATFORMAT from FLOATFORMATS. BYTE_ORDER is the byte order
  2894. to use. If it is BFD_ENDIAN_UNKNOWN (the default), then the byte
  2895. order of the objfile's architecture is used. */
  2896. struct type *
  2897. init_float_type (struct objfile *objfile,
  2898. int bit, const char *name,
  2899. const struct floatformat **floatformats,
  2900. enum bfd_endian byte_order)
  2901. {
  2902. if (byte_order == BFD_ENDIAN_UNKNOWN)
  2903. {
  2904. struct gdbarch *gdbarch = objfile->arch ();
  2905. byte_order = gdbarch_byte_order (gdbarch);
  2906. }
  2907. const struct floatformat *fmt = floatformats[byte_order];
  2908. struct type *t;
  2909. bit = verify_floatformat (bit, fmt);
  2910. t = init_type (objfile, TYPE_CODE_FLT, bit, name);
  2911. TYPE_FLOATFORMAT (t) = fmt;
  2912. return t;
  2913. }
  2914. /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE.
  2915. BIT is the type size in bits. NAME is the type name. */
  2916. struct type *
  2917. init_decfloat_type (struct objfile *objfile, int bit, const char *name)
  2918. {
  2919. struct type *t;
  2920. t = init_type (objfile, TYPE_CODE_DECFLOAT, bit, name);
  2921. return t;
  2922. }
  2923. /* Return true if init_complex_type can be called with TARGET_TYPE. */
  2924. bool
  2925. can_create_complex_type (struct type *target_type)
  2926. {
  2927. return (target_type->code () == TYPE_CODE_INT
  2928. || target_type->code () == TYPE_CODE_FLT);
  2929. }
  2930. /* Allocate a TYPE_CODE_COMPLEX type structure. NAME is the type
  2931. name. TARGET_TYPE is the component type. */
  2932. struct type *
  2933. init_complex_type (const char *name, struct type *target_type)
  2934. {
  2935. struct type *t;
  2936. gdb_assert (can_create_complex_type (target_type));
  2937. if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
  2938. {
  2939. if (name == nullptr && target_type->name () != nullptr)
  2940. {
  2941. char *new_name
  2942. = (char *) TYPE_ALLOC (target_type,
  2943. strlen (target_type->name ())
  2944. + strlen ("_Complex ") + 1);
  2945. strcpy (new_name, "_Complex ");
  2946. strcat (new_name, target_type->name ());
  2947. name = new_name;
  2948. }
  2949. t = alloc_type_copy (target_type);
  2950. set_type_code (t, TYPE_CODE_COMPLEX);
  2951. TYPE_LENGTH (t) = 2 * TYPE_LENGTH (target_type);
  2952. t->set_name (name);
  2953. TYPE_TARGET_TYPE (t) = target_type;
  2954. TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type = t;
  2955. }
  2956. return TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type;
  2957. }
  2958. /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE.
  2959. BIT is the pointer type size in bits. NAME is the type name.
  2960. TARGET_TYPE is the pointer target type. Always sets the pointer type's
  2961. TYPE_UNSIGNED flag. */
  2962. struct type *
  2963. init_pointer_type (struct objfile *objfile,
  2964. int bit, const char *name, struct type *target_type)
  2965. {
  2966. struct type *t;
  2967. t = init_type (objfile, TYPE_CODE_PTR, bit, name);
  2968. TYPE_TARGET_TYPE (t) = target_type;
  2969. t->set_is_unsigned (true);
  2970. return t;
  2971. }
  2972. /* Allocate a TYPE_CODE_FIXED_POINT type structure associated with OBJFILE.
  2973. BIT is the pointer type size in bits.
  2974. UNSIGNED_P should be nonzero if the type is unsigned.
  2975. NAME is the type name. */
  2976. struct type *
  2977. init_fixed_point_type (struct objfile *objfile,
  2978. int bit, int unsigned_p, const char *name)
  2979. {
  2980. struct type *t;
  2981. t = init_type (objfile, TYPE_CODE_FIXED_POINT, bit, name);
  2982. if (unsigned_p)
  2983. t->set_is_unsigned (true);
  2984. return t;
  2985. }
  2986. /* See gdbtypes.h. */
  2987. unsigned
  2988. type_raw_align (struct type *type)
  2989. {
  2990. if (type->align_log2 != 0)
  2991. return 1 << (type->align_log2 - 1);
  2992. return 0;
  2993. }
  2994. /* See gdbtypes.h. */
  2995. unsigned
  2996. type_align (struct type *type)
  2997. {
  2998. /* Check alignment provided in the debug information. */
  2999. unsigned raw_align = type_raw_align (type);
  3000. if (raw_align != 0)
  3001. return raw_align;
  3002. /* Allow the architecture to provide an alignment. */
  3003. ULONGEST align = gdbarch_type_align (type->arch (), type);
  3004. if (align != 0)
  3005. return align;
  3006. switch (type->code ())
  3007. {
  3008. case TYPE_CODE_PTR:
  3009. case TYPE_CODE_FUNC:
  3010. case TYPE_CODE_FLAGS:
  3011. case TYPE_CODE_INT:
  3012. case TYPE_CODE_RANGE:
  3013. case TYPE_CODE_FLT:
  3014. case TYPE_CODE_ENUM:
  3015. case TYPE_CODE_REF:
  3016. case TYPE_CODE_RVALUE_REF:
  3017. case TYPE_CODE_CHAR:
  3018. case TYPE_CODE_BOOL:
  3019. case TYPE_CODE_DECFLOAT:
  3020. case TYPE_CODE_METHODPTR:
  3021. case TYPE_CODE_MEMBERPTR:
  3022. align = type_length_units (check_typedef (type));
  3023. break;
  3024. case TYPE_CODE_ARRAY:
  3025. case TYPE_CODE_COMPLEX:
  3026. case TYPE_CODE_TYPEDEF:
  3027. align = type_align (TYPE_TARGET_TYPE (type));
  3028. break;
  3029. case TYPE_CODE_STRUCT:
  3030. case TYPE_CODE_UNION:
  3031. {
  3032. int number_of_non_static_fields = 0;
  3033. for (unsigned i = 0; i < type->num_fields (); ++i)
  3034. {
  3035. if (!field_is_static (&type->field (i)))
  3036. {
  3037. number_of_non_static_fields++;
  3038. ULONGEST f_align = type_align (type->field (i).type ());
  3039. if (f_align == 0)
  3040. {
  3041. /* Don't pretend we know something we don't. */
  3042. align = 0;
  3043. break;
  3044. }
  3045. if (f_align > align)
  3046. align = f_align;
  3047. }
  3048. }
  3049. /* A struct with no fields, or with only static fields has an
  3050. alignment of 1. */
  3051. if (number_of_non_static_fields == 0)
  3052. align = 1;
  3053. }
  3054. break;
  3055. case TYPE_CODE_SET:
  3056. case TYPE_CODE_STRING:
  3057. /* Not sure what to do here, and these can't appear in C or C++
  3058. anyway. */
  3059. break;
  3060. case TYPE_CODE_VOID:
  3061. align = 1;
  3062. break;
  3063. case TYPE_CODE_ERROR:
  3064. case TYPE_CODE_METHOD:
  3065. default:
  3066. break;
  3067. }
  3068. if ((align & (align - 1)) != 0)
  3069. {
  3070. /* Not a power of 2, so pass. */
  3071. align = 0;
  3072. }
  3073. return align;
  3074. }
  3075. /* See gdbtypes.h. */
  3076. bool
  3077. set_type_align (struct type *type, ULONGEST align)
  3078. {
  3079. /* Must be a power of 2. Zero is ok. */
  3080. gdb_assert ((align & (align - 1)) == 0);
  3081. unsigned result = 0;
  3082. while (align != 0)
  3083. {
  3084. ++result;
  3085. align >>= 1;
  3086. }
  3087. if (result >= (1 << TYPE_ALIGN_BITS))
  3088. return false;
  3089. type->align_log2 = result;
  3090. return true;
  3091. }
  3092. /* Queries on types. */
  3093. int
  3094. can_dereference (struct type *t)
  3095. {
  3096. /* FIXME: Should we return true for references as well as
  3097. pointers? */
  3098. t = check_typedef (t);
  3099. return
  3100. (t != NULL
  3101. && t->code () == TYPE_CODE_PTR
  3102. && TYPE_TARGET_TYPE (t)->code () != TYPE_CODE_VOID);
  3103. }
  3104. int
  3105. is_integral_type (struct type *t)
  3106. {
  3107. t = check_typedef (t);
  3108. return
  3109. ((t != NULL)
  3110. && !is_fixed_point_type (t)
  3111. && ((t->code () == TYPE_CODE_INT)
  3112. || (t->code () == TYPE_CODE_ENUM)
  3113. || (t->code () == TYPE_CODE_FLAGS)
  3114. || (t->code () == TYPE_CODE_CHAR)
  3115. || (t->code () == TYPE_CODE_RANGE)
  3116. || (t->code () == TYPE_CODE_BOOL)));
  3117. }
  3118. int
  3119. is_floating_type (struct type *t)
  3120. {
  3121. t = check_typedef (t);
  3122. return
  3123. ((t != NULL)
  3124. && ((t->code () == TYPE_CODE_FLT)
  3125. || (t->code () == TYPE_CODE_DECFLOAT)));
  3126. }
  3127. /* Return true if TYPE is scalar. */
  3128. int
  3129. is_scalar_type (struct type *type)
  3130. {
  3131. type = check_typedef (type);
  3132. if (is_fixed_point_type (type))
  3133. return 0; /* Implemented as a scalar, but more like a floating point. */
  3134. switch (type->code ())
  3135. {
  3136. case TYPE_CODE_ARRAY:
  3137. case TYPE_CODE_STRUCT:
  3138. case TYPE_CODE_UNION:
  3139. case TYPE_CODE_SET:
  3140. case TYPE_CODE_STRING:
  3141. return 0;
  3142. default:
  3143. return 1;
  3144. }
  3145. }
  3146. /* Return true if T is scalar, or a composite type which in practice has
  3147. the memory layout of a scalar type. E.g., an array or struct with only
  3148. one scalar element inside it, or a union with only scalar elements. */
  3149. int
  3150. is_scalar_type_recursive (struct type *t)
  3151. {
  3152. t = check_typedef (t);
  3153. if (is_scalar_type (t))
  3154. return 1;
  3155. /* Are we dealing with an array or string of known dimensions? */
  3156. else if ((t->code () == TYPE_CODE_ARRAY
  3157. || t->code () == TYPE_CODE_STRING) && t->num_fields () == 1
  3158. && t->index_type ()->code () == TYPE_CODE_RANGE)
  3159. {
  3160. LONGEST low_bound, high_bound;
  3161. struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
  3162. if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
  3163. return (high_bound == low_bound
  3164. && is_scalar_type_recursive (elt_type));
  3165. else
  3166. return 0;
  3167. }
  3168. /* Are we dealing with a struct with one element? */
  3169. else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
  3170. return is_scalar_type_recursive (t->field (0).type ());
  3171. else if (t->code () == TYPE_CODE_UNION)
  3172. {
  3173. int i, n = t->num_fields ();
  3174. /* If all elements of the union are scalar, then the union is scalar. */
  3175. for (i = 0; i < n; i++)
  3176. if (!is_scalar_type_recursive (t->field (i).type ()))
  3177. return 0;
  3178. return 1;
  3179. }
  3180. return 0;
  3181. }
  3182. /* Return true is T is a class or a union. False otherwise. */
  3183. int
  3184. class_or_union_p (const struct type *t)
  3185. {
  3186. return (t->code () == TYPE_CODE_STRUCT
  3187. || t->code () == TYPE_CODE_UNION);
  3188. }
  3189. /* A helper function which returns true if types A and B represent the
  3190. "same" class type. This is true if the types have the same main
  3191. type, or the same name. */
  3192. int
  3193. class_types_same_p (const struct type *a, const struct type *b)
  3194. {
  3195. return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b)
  3196. || (a->name () && b->name ()
  3197. && !strcmp (a->name (), b->name ())));
  3198. }
  3199. /* If BASE is an ancestor of DCLASS return the distance between them.
  3200. otherwise return -1;
  3201. eg:
  3202. class A {};
  3203. class B: public A {};
  3204. class C: public B {};
  3205. class D: C {};
  3206. distance_to_ancestor (A, A, 0) = 0
  3207. distance_to_ancestor (A, B, 0) = 1
  3208. distance_to_ancestor (A, C, 0) = 2
  3209. distance_to_ancestor (A, D, 0) = 3
  3210. If PUBLIC is 1 then only public ancestors are considered,
  3211. and the function returns the distance only if BASE is a public ancestor
  3212. of DCLASS.
  3213. Eg:
  3214. distance_to_ancestor (A, D, 1) = -1. */
  3215. static int
  3216. distance_to_ancestor (struct type *base, struct type *dclass, int is_public)
  3217. {
  3218. int i;
  3219. int d;
  3220. base = check_typedef (base);
  3221. dclass = check_typedef (dclass);
  3222. if (class_types_same_p (base, dclass))
  3223. return 0;
  3224. for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++)
  3225. {
  3226. if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i))
  3227. continue;
  3228. d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public);
  3229. if (d >= 0)
  3230. return 1 + d;
  3231. }
  3232. return -1;
  3233. }
  3234. /* Check whether BASE is an ancestor or base class or DCLASS
  3235. Return 1 if so, and 0 if not.
  3236. Note: If BASE and DCLASS are of the same type, this function
  3237. will return 1. So for some class A, is_ancestor (A, A) will
  3238. return 1. */
  3239. int
  3240. is_ancestor (struct type *base, struct type *dclass)
  3241. {
  3242. return distance_to_ancestor (base, dclass, 0) >= 0;
  3243. }
  3244. /* Like is_ancestor, but only returns true when BASE is a public
  3245. ancestor of DCLASS. */
  3246. int
  3247. is_public_ancestor (struct type *base, struct type *dclass)
  3248. {
  3249. return distance_to_ancestor (base, dclass, 1) >= 0;
  3250. }
  3251. /* A helper function for is_unique_ancestor. */
  3252. static int
  3253. is_unique_ancestor_worker (struct type *base, struct type *dclass,
  3254. int *offset,
  3255. const gdb_byte *valaddr, int embedded_offset,
  3256. CORE_ADDR address, struct value *val)
  3257. {
  3258. int i, count = 0;
  3259. base = check_typedef (base);
  3260. dclass = check_typedef (dclass);
  3261. for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i)
  3262. {
  3263. struct type *iter;
  3264. int this_offset;
  3265. iter = check_typedef (TYPE_BASECLASS (dclass, i));
  3266. this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset,
  3267. address, val);
  3268. if (class_types_same_p (base, iter))
  3269. {
  3270. /* If this is the first subclass, set *OFFSET and set count
  3271. to 1. Otherwise, if this is at the same offset as
  3272. previous instances, do nothing. Otherwise, increment
  3273. count. */
  3274. if (*offset == -1)
  3275. {
  3276. *offset = this_offset;
  3277. count = 1;
  3278. }
  3279. else if (this_offset == *offset)
  3280. {
  3281. /* Nothing. */
  3282. }
  3283. else
  3284. ++count;
  3285. }
  3286. else
  3287. count += is_unique_ancestor_worker (base, iter, offset,
  3288. valaddr,
  3289. embedded_offset + this_offset,
  3290. address, val);
  3291. }
  3292. return count;
  3293. }
  3294. /* Like is_ancestor, but only returns true if BASE is a unique base
  3295. class of the type of VAL. */
  3296. int
  3297. is_unique_ancestor (struct type *base, struct value *val)
  3298. {
  3299. int offset = -1;
  3300. return is_unique_ancestor_worker (base, value_type (val), &offset,
  3301. value_contents_for_printing (val).data (),
  3302. value_embedded_offset (val),
  3303. value_address (val), val) == 1;
  3304. }
  3305. /* See gdbtypes.h. */
  3306. enum bfd_endian
  3307. type_byte_order (const struct type *type)
  3308. {
  3309. bfd_endian byteorder = gdbarch_byte_order (type->arch ());
  3310. if (type->endianity_is_not_default ())
  3311. {
  3312. if (byteorder == BFD_ENDIAN_BIG)
  3313. return BFD_ENDIAN_LITTLE;
  3314. else
  3315. {
  3316. gdb_assert (byteorder == BFD_ENDIAN_LITTLE);
  3317. return BFD_ENDIAN_BIG;
  3318. }
  3319. }
  3320. return byteorder;
  3321. }
  3322. /* See gdbtypes.h. */
  3323. bool
  3324. is_nocall_function (const struct type *type)
  3325. {
  3326. gdb_assert (type->code () == TYPE_CODE_FUNC
  3327. || type->code () == TYPE_CODE_METHOD);
  3328. return TYPE_CALLING_CONVENTION (type) == DW_CC_nocall;
  3329. }
  3330. /* Overload resolution. */
  3331. /* Return the sum of the rank of A with the rank of B. */
  3332. struct rank
  3333. sum_ranks (struct rank a, struct rank b)
  3334. {
  3335. struct rank c;
  3336. c.rank = a.rank + b.rank;
  3337. c.subrank = a.subrank + b.subrank;
  3338. return c;
  3339. }
  3340. /* Compare rank A and B and return:
  3341. 0 if a = b
  3342. 1 if a is better than b
  3343. -1 if b is better than a. */
  3344. int
  3345. compare_ranks (struct rank a, struct rank b)
  3346. {
  3347. if (a.rank == b.rank)
  3348. {
  3349. if (a.subrank == b.subrank)
  3350. return 0;
  3351. if (a.subrank < b.subrank)
  3352. return 1;
  3353. if (a.subrank > b.subrank)
  3354. return -1;
  3355. }
  3356. if (a.rank < b.rank)
  3357. return 1;
  3358. /* a.rank > b.rank */
  3359. return -1;
  3360. }
  3361. /* Functions for overload resolution begin here. */
  3362. /* Compare two badness vectors A and B and return the result.
  3363. 0 => A and B are identical
  3364. 1 => A and B are incomparable
  3365. 2 => A is better than B
  3366. 3 => A is worse than B */
  3367. int
  3368. compare_badness (const badness_vector &a, const badness_vector &b)
  3369. {
  3370. int i;
  3371. int tmp;
  3372. /* Any positives in comparison? */
  3373. bool found_pos = false;
  3374. /* Any negatives in comparison? */
  3375. bool found_neg = false;
  3376. /* Did A have any INVALID_CONVERSION entries. */
  3377. bool a_invalid = false;
  3378. /* Did B have any INVALID_CONVERSION entries. */
  3379. bool b_invalid = false;
  3380. /* differing sizes => incomparable */
  3381. if (a.size () != b.size ())
  3382. return 1;
  3383. /* Subtract b from a */
  3384. for (i = 0; i < a.size (); i++)
  3385. {
  3386. tmp = compare_ranks (b[i], a[i]);
  3387. if (tmp > 0)
  3388. found_pos = true;
  3389. else if (tmp < 0)
  3390. found_neg = true;
  3391. if (a[i].rank >= INVALID_CONVERSION)
  3392. a_invalid = true;
  3393. if (b[i].rank >= INVALID_CONVERSION)
  3394. b_invalid = true;
  3395. }
  3396. /* B will only be considered better than or incomparable to A if
  3397. they both have invalid entries, or if neither does. That is, if
  3398. A has only valid entries, and B has an invalid entry, then A will
  3399. be considered better than B, even if B happens to be better for
  3400. some parameter. */
  3401. if (a_invalid != b_invalid)
  3402. {
  3403. if (a_invalid)
  3404. return 3; /* A > B */
  3405. return 2; /* A < B */
  3406. }
  3407. else if (found_pos)
  3408. {
  3409. if (found_neg)
  3410. return 1; /* incomparable */
  3411. else
  3412. return 3; /* A > B */
  3413. }
  3414. else
  3415. /* no positives */
  3416. {
  3417. if (found_neg)
  3418. return 2; /* A < B */
  3419. else
  3420. return 0; /* A == B */
  3421. }
  3422. }
  3423. /* Rank a function by comparing its parameter types (PARMS), to the
  3424. types of an argument list (ARGS). Return the badness vector. This
  3425. has ARGS.size() + 1 entries. */
  3426. badness_vector
  3427. rank_function (gdb::array_view<type *> parms,
  3428. gdb::array_view<value *> args)
  3429. {
  3430. /* add 1 for the length-match rank. */
  3431. badness_vector bv;
  3432. bv.reserve (1 + args.size ());
  3433. /* First compare the lengths of the supplied lists.
  3434. If there is a mismatch, set it to a high value. */
  3435. /* pai/1997-06-03 FIXME: when we have debug info about default
  3436. arguments and ellipsis parameter lists, we should consider those
  3437. and rank the length-match more finely. */
  3438. bv.push_back ((args.size () != parms.size ())
  3439. ? LENGTH_MISMATCH_BADNESS
  3440. : EXACT_MATCH_BADNESS);
  3441. /* Now rank all the parameters of the candidate function. */
  3442. size_t min_len = std::min (parms.size (), args.size ());
  3443. for (size_t i = 0; i < min_len; i++)
  3444. bv.push_back (rank_one_type (parms[i], value_type (args[i]),
  3445. args[i]));
  3446. /* If more arguments than parameters, add dummy entries. */
  3447. for (size_t i = min_len; i < args.size (); i++)
  3448. bv.push_back (TOO_FEW_PARAMS_BADNESS);
  3449. return bv;
  3450. }
  3451. /* Compare the names of two integer types, assuming that any sign
  3452. qualifiers have been checked already. We do it this way because
  3453. there may be an "int" in the name of one of the types. */
  3454. static int
  3455. integer_types_same_name_p (const char *first, const char *second)
  3456. {
  3457. int first_p, second_p;
  3458. /* If both are shorts, return 1; if neither is a short, keep
  3459. checking. */
  3460. first_p = (strstr (first, "short") != NULL);
  3461. second_p = (strstr (second, "short") != NULL);
  3462. if (first_p && second_p)
  3463. return 1;
  3464. if (first_p || second_p)
  3465. return 0;
  3466. /* Likewise for long. */
  3467. first_p = (strstr (first, "long") != NULL);
  3468. second_p = (strstr (second, "long") != NULL);
  3469. if (first_p && second_p)
  3470. return 1;
  3471. if (first_p || second_p)
  3472. return 0;
  3473. /* Likewise for char. */
  3474. first_p = (strstr (first, "char") != NULL);
  3475. second_p = (strstr (second, "char") != NULL);
  3476. if (first_p && second_p)
  3477. return 1;
  3478. if (first_p || second_p)
  3479. return 0;
  3480. /* They must both be ints. */
  3481. return 1;
  3482. }
  3483. /* Compares type A to type B. Returns true if they represent the same
  3484. type, false otherwise. */
  3485. bool
  3486. types_equal (struct type *a, struct type *b)
  3487. {
  3488. /* Identical type pointers. */
  3489. /* However, this still doesn't catch all cases of same type for b
  3490. and a. The reason is that builtin types are different from
  3491. the same ones constructed from the object. */
  3492. if (a == b)
  3493. return true;
  3494. /* Resolve typedefs */
  3495. if (a->code () == TYPE_CODE_TYPEDEF)
  3496. a = check_typedef (a);
  3497. if (b->code () == TYPE_CODE_TYPEDEF)
  3498. b = check_typedef (b);
  3499. /* Check if identical after resolving typedefs. */
  3500. if (a == b)
  3501. return true;
  3502. /* If after resolving typedefs a and b are not of the same type
  3503. code then they are not equal. */
  3504. if (a->code () != b->code ())
  3505. return false;
  3506. /* If a and b are both pointers types or both reference types then
  3507. they are equal of the same type iff the objects they refer to are
  3508. of the same type. */
  3509. if (a->code () == TYPE_CODE_PTR
  3510. || a->code () == TYPE_CODE_REF)
  3511. return types_equal (TYPE_TARGET_TYPE (a),
  3512. TYPE_TARGET_TYPE (b));
  3513. /* Well, damnit, if the names are exactly the same, I'll say they
  3514. are exactly the same. This happens when we generate method
  3515. stubs. The types won't point to the same address, but they
  3516. really are the same. */
  3517. if (a->name () && b->name ()
  3518. && strcmp (a->name (), b->name ()) == 0)
  3519. return true;
  3520. /* Two function types are equal if their argument and return types
  3521. are equal. */
  3522. if (a->code () == TYPE_CODE_FUNC)
  3523. {
  3524. int i;
  3525. if (a->num_fields () != b->num_fields ())
  3526. return false;
  3527. if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b)))
  3528. return false;
  3529. for (i = 0; i < a->num_fields (); ++i)
  3530. if (!types_equal (a->field (i).type (), b->field (i).type ()))
  3531. return false;
  3532. return true;
  3533. }
  3534. return false;
  3535. }
  3536. /* Deep comparison of types. */
  3537. /* An entry in the type-equality bcache. */
  3538. struct type_equality_entry
  3539. {
  3540. type_equality_entry (struct type *t1, struct type *t2)
  3541. : type1 (t1),
  3542. type2 (t2)
  3543. {
  3544. }
  3545. struct type *type1, *type2;
  3546. };
  3547. /* A helper function to compare two strings. Returns true if they are
  3548. the same, false otherwise. Handles NULLs properly. */
  3549. static bool
  3550. compare_maybe_null_strings (const char *s, const char *t)
  3551. {
  3552. if (s == NULL || t == NULL)
  3553. return s == t;
  3554. return strcmp (s, t) == 0;
  3555. }
  3556. /* A helper function for check_types_worklist that checks two types for
  3557. "deep" equality. Returns true if the types are considered the
  3558. same, false otherwise. */
  3559. static bool
  3560. check_types_equal (struct type *type1, struct type *type2,
  3561. std::vector<type_equality_entry> *worklist)
  3562. {
  3563. type1 = check_typedef (type1);
  3564. type2 = check_typedef (type2);
  3565. if (type1 == type2)
  3566. return true;
  3567. if (type1->code () != type2->code ()
  3568. || TYPE_LENGTH (type1) != TYPE_LENGTH (type2)
  3569. || type1->is_unsigned () != type2->is_unsigned ()
  3570. || type1->has_no_signedness () != type2->has_no_signedness ()
  3571. || type1->endianity_is_not_default () != type2->endianity_is_not_default ()
  3572. || type1->has_varargs () != type2->has_varargs ()
  3573. || type1->is_vector () != type2->is_vector ()
  3574. || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2)
  3575. || type1->instance_flags () != type2->instance_flags ()
  3576. || type1->num_fields () != type2->num_fields ())
  3577. return false;
  3578. if (!compare_maybe_null_strings (type1->name (), type2->name ()))
  3579. return false;
  3580. if (!compare_maybe_null_strings (type1->name (), type2->name ()))
  3581. return false;
  3582. if (type1->code () == TYPE_CODE_RANGE)
  3583. {
  3584. if (*type1->bounds () != *type2->bounds ())
  3585. return false;
  3586. }
  3587. else
  3588. {
  3589. int i;
  3590. for (i = 0; i < type1->num_fields (); ++i)
  3591. {
  3592. const struct field *field1 = &type1->field (i);
  3593. const struct field *field2 = &type2->field (i);
  3594. if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2)
  3595. || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2)
  3596. || field1->loc_kind () != field2->loc_kind ())
  3597. return false;
  3598. if (!compare_maybe_null_strings (field1->name (), field2->name ()))
  3599. return false;
  3600. switch (field1->loc_kind ())
  3601. {
  3602. case FIELD_LOC_KIND_BITPOS:
  3603. if (field1->loc_bitpos () != field2->loc_bitpos ())
  3604. return false;
  3605. break;
  3606. case FIELD_LOC_KIND_ENUMVAL:
  3607. if (field1->loc_enumval () != field2->loc_enumval ())
  3608. return false;
  3609. /* Don't compare types of enum fields, because they don't
  3610. have a type. */
  3611. continue;
  3612. case FIELD_LOC_KIND_PHYSADDR:
  3613. if (field1->loc_physaddr () != field2->loc_physaddr ())
  3614. return false;
  3615. break;
  3616. case FIELD_LOC_KIND_PHYSNAME:
  3617. if (!compare_maybe_null_strings (field1->loc_physname (),
  3618. field2->loc_physname ()))
  3619. return false;
  3620. break;
  3621. case FIELD_LOC_KIND_DWARF_BLOCK:
  3622. {
  3623. struct dwarf2_locexpr_baton *block1, *block2;
  3624. block1 = field1->loc_dwarf_block ();
  3625. block2 = field2->loc_dwarf_block ();
  3626. if (block1->per_cu != block2->per_cu
  3627. || block1->size != block2->size
  3628. || memcmp (block1->data, block2->data, block1->size) != 0)
  3629. return false;
  3630. }
  3631. break;
  3632. default:
  3633. internal_error (__FILE__, __LINE__, _("Unsupported field kind "
  3634. "%d by check_types_equal"),
  3635. field1->loc_kind ());
  3636. }
  3637. worklist->emplace_back (field1->type (), field2->type ());
  3638. }
  3639. }
  3640. if (TYPE_TARGET_TYPE (type1) != NULL)
  3641. {
  3642. if (TYPE_TARGET_TYPE (type2) == NULL)
  3643. return false;
  3644. worklist->emplace_back (TYPE_TARGET_TYPE (type1),
  3645. TYPE_TARGET_TYPE (type2));
  3646. }
  3647. else if (TYPE_TARGET_TYPE (type2) != NULL)
  3648. return false;
  3649. return true;
  3650. }
  3651. /* Check types on a worklist for equality. Returns false if any pair
  3652. is not equal, true if they are all considered equal. */
  3653. static bool
  3654. check_types_worklist (std::vector<type_equality_entry> *worklist,
  3655. gdb::bcache *cache)
  3656. {
  3657. while (!worklist->empty ())
  3658. {
  3659. bool added;
  3660. struct type_equality_entry entry = std::move (worklist->back ());
  3661. worklist->pop_back ();
  3662. /* If the type pair has already been visited, we know it is
  3663. ok. */
  3664. cache->insert (&entry, sizeof (entry), &added);
  3665. if (!added)
  3666. continue;
  3667. if (!check_types_equal (entry.type1, entry.type2, worklist))
  3668. return false;
  3669. }
  3670. return true;
  3671. }
  3672. /* Return true if types TYPE1 and TYPE2 are equal, as determined by a
  3673. "deep comparison". Otherwise return false. */
  3674. bool
  3675. types_deeply_equal (struct type *type1, struct type *type2)
  3676. {
  3677. std::vector<type_equality_entry> worklist;
  3678. gdb_assert (type1 != NULL && type2 != NULL);
  3679. /* Early exit for the simple case. */
  3680. if (type1 == type2)
  3681. return true;
  3682. gdb::bcache cache;
  3683. worklist.emplace_back (type1, type2);
  3684. return check_types_worklist (&worklist, &cache);
  3685. }
  3686. /* Allocated status of type TYPE. Return zero if type TYPE is allocated.
  3687. Otherwise return one. */
  3688. int
  3689. type_not_allocated (const struct type *type)
  3690. {
  3691. struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type);
  3692. return (prop != nullptr && prop->kind () == PROP_CONST
  3693. && prop->const_val () == 0);
  3694. }
  3695. /* Associated status of type TYPE. Return zero if type TYPE is associated.
  3696. Otherwise return one. */
  3697. int
  3698. type_not_associated (const struct type *type)
  3699. {
  3700. struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type);
  3701. return (prop != nullptr && prop->kind () == PROP_CONST
  3702. && prop->const_val () == 0);
  3703. }
  3704. /* rank_one_type helper for when PARM's type code is TYPE_CODE_PTR. */
  3705. static struct rank
  3706. rank_one_type_parm_ptr (struct type *parm, struct type *arg, struct value *value)
  3707. {
  3708. struct rank rank = {0,0};
  3709. switch (arg->code ())
  3710. {
  3711. case TYPE_CODE_PTR:
  3712. /* Allowed pointer conversions are:
  3713. (a) pointer to void-pointer conversion. */
  3714. if (TYPE_TARGET_TYPE (parm)->code () == TYPE_CODE_VOID)
  3715. return VOID_PTR_CONVERSION_BADNESS;
  3716. /* (b) pointer to ancestor-pointer conversion. */
  3717. rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm),
  3718. TYPE_TARGET_TYPE (arg),
  3719. 0);
  3720. if (rank.subrank >= 0)
  3721. return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank);
  3722. return INCOMPATIBLE_TYPE_BADNESS;
  3723. case TYPE_CODE_ARRAY:
  3724. {
  3725. struct type *t1 = TYPE_TARGET_TYPE (parm);
  3726. struct type *t2 = TYPE_TARGET_TYPE (arg);
  3727. if (types_equal (t1, t2))
  3728. {
  3729. /* Make sure they are CV equal. */
  3730. if (TYPE_CONST (t1) != TYPE_CONST (t2))
  3731. rank.subrank |= CV_CONVERSION_CONST;
  3732. if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
  3733. rank.subrank |= CV_CONVERSION_VOLATILE;
  3734. if (rank.subrank != 0)
  3735. return sum_ranks (CV_CONVERSION_BADNESS, rank);
  3736. return EXACT_MATCH_BADNESS;
  3737. }
  3738. return INCOMPATIBLE_TYPE_BADNESS;
  3739. }
  3740. case TYPE_CODE_FUNC:
  3741. return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL);
  3742. case TYPE_CODE_INT:
  3743. if (value != NULL && value_type (value)->code () == TYPE_CODE_INT)
  3744. {
  3745. if (value_as_long (value) == 0)
  3746. {
  3747. /* Null pointer conversion: allow it to be cast to a pointer.
  3748. [4.10.1 of C++ standard draft n3290] */
  3749. return NULL_POINTER_CONVERSION_BADNESS;
  3750. }
  3751. else
  3752. {
  3753. /* If type checking is disabled, allow the conversion. */
  3754. if (!strict_type_checking)
  3755. return NS_INTEGER_POINTER_CONVERSION_BADNESS;
  3756. }
  3757. }
  3758. /* fall through */
  3759. case TYPE_CODE_ENUM:
  3760. case TYPE_CODE_FLAGS:
  3761. case TYPE_CODE_CHAR:
  3762. case TYPE_CODE_RANGE:
  3763. case TYPE_CODE_BOOL:
  3764. default:
  3765. return INCOMPATIBLE_TYPE_BADNESS;
  3766. }
  3767. }
  3768. /* rank_one_type helper for when PARM's type code is TYPE_CODE_ARRAY. */
  3769. static struct rank
  3770. rank_one_type_parm_array (struct type *parm, struct type *arg, struct value *value)
  3771. {
  3772. switch (arg->code ())
  3773. {
  3774. case TYPE_CODE_PTR:
  3775. case TYPE_CODE_ARRAY:
  3776. return rank_one_type (TYPE_TARGET_TYPE (parm),
  3777. TYPE_TARGET_TYPE (arg), NULL);
  3778. default:
  3779. return INCOMPATIBLE_TYPE_BADNESS;
  3780. }
  3781. }
  3782. /* rank_one_type helper for when PARM's type code is TYPE_CODE_FUNC. */
  3783. static struct rank
  3784. rank_one_type_parm_func (struct type *parm, struct type *arg, struct value *value)
  3785. {
  3786. switch (arg->code ())
  3787. {
  3788. case TYPE_CODE_PTR: /* funcptr -> func */
  3789. return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL);
  3790. default:
  3791. return INCOMPATIBLE_TYPE_BADNESS;
  3792. }
  3793. }
  3794. /* rank_one_type helper for when PARM's type code is TYPE_CODE_INT. */
  3795. static struct rank
  3796. rank_one_type_parm_int (struct type *parm, struct type *arg, struct value *value)
  3797. {
  3798. switch (arg->code ())
  3799. {
  3800. case TYPE_CODE_INT:
  3801. if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
  3802. {
  3803. /* Deal with signed, unsigned, and plain chars and
  3804. signed and unsigned ints. */
  3805. if (parm->has_no_signedness ())
  3806. {
  3807. /* This case only for character types. */
  3808. if (arg->has_no_signedness ())
  3809. return EXACT_MATCH_BADNESS; /* plain char -> plain char */
  3810. else /* signed/unsigned char -> plain char */
  3811. return INTEGER_CONVERSION_BADNESS;
  3812. }
  3813. else if (parm->is_unsigned ())
  3814. {
  3815. if (arg->is_unsigned ())
  3816. {
  3817. /* unsigned int -> unsigned int, or
  3818. unsigned long -> unsigned long */
  3819. if (integer_types_same_name_p (parm->name (),
  3820. arg->name ()))
  3821. return EXACT_MATCH_BADNESS;
  3822. else if (integer_types_same_name_p (arg->name (),
  3823. "int")
  3824. && integer_types_same_name_p (parm->name (),
  3825. "long"))
  3826. /* unsigned int -> unsigned long */
  3827. return INTEGER_PROMOTION_BADNESS;
  3828. else
  3829. /* unsigned long -> unsigned int */
  3830. return INTEGER_CONVERSION_BADNESS;
  3831. }
  3832. else
  3833. {
  3834. if (integer_types_same_name_p (arg->name (),
  3835. "long")
  3836. && integer_types_same_name_p (parm->name (),
  3837. "int"))
  3838. /* signed long -> unsigned int */
  3839. return INTEGER_CONVERSION_BADNESS;
  3840. else
  3841. /* signed int/long -> unsigned int/long */
  3842. return INTEGER_CONVERSION_BADNESS;
  3843. }
  3844. }
  3845. else if (!arg->has_no_signedness () && !arg->is_unsigned ())
  3846. {
  3847. if (integer_types_same_name_p (parm->name (),
  3848. arg->name ()))
  3849. return EXACT_MATCH_BADNESS;
  3850. else if (integer_types_same_name_p (arg->name (),
  3851. "int")
  3852. && integer_types_same_name_p (parm->name (),
  3853. "long"))
  3854. return INTEGER_PROMOTION_BADNESS;
  3855. else
  3856. return INTEGER_CONVERSION_BADNESS;
  3857. }
  3858. else
  3859. return INTEGER_CONVERSION_BADNESS;
  3860. }
  3861. else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  3862. return INTEGER_PROMOTION_BADNESS;
  3863. else
  3864. return INTEGER_CONVERSION_BADNESS;
  3865. case TYPE_CODE_ENUM:
  3866. case TYPE_CODE_FLAGS:
  3867. case TYPE_CODE_CHAR:
  3868. case TYPE_CODE_RANGE:
  3869. case TYPE_CODE_BOOL:
  3870. if (arg->is_declared_class ())
  3871. return INCOMPATIBLE_TYPE_BADNESS;
  3872. return INTEGER_PROMOTION_BADNESS;
  3873. case TYPE_CODE_FLT:
  3874. return INT_FLOAT_CONVERSION_BADNESS;
  3875. case TYPE_CODE_PTR:
  3876. return NS_POINTER_CONVERSION_BADNESS;
  3877. default:
  3878. return INCOMPATIBLE_TYPE_BADNESS;
  3879. }
  3880. }
  3881. /* rank_one_type helper for when PARM's type code is TYPE_CODE_ENUM. */
  3882. static struct rank
  3883. rank_one_type_parm_enum (struct type *parm, struct type *arg, struct value *value)
  3884. {
  3885. switch (arg->code ())
  3886. {
  3887. case TYPE_CODE_INT:
  3888. case TYPE_CODE_CHAR:
  3889. case TYPE_CODE_RANGE:
  3890. case TYPE_CODE_BOOL:
  3891. case TYPE_CODE_ENUM:
  3892. if (parm->is_declared_class () || arg->is_declared_class ())
  3893. return INCOMPATIBLE_TYPE_BADNESS;
  3894. return INTEGER_CONVERSION_BADNESS;
  3895. case TYPE_CODE_FLT:
  3896. return INT_FLOAT_CONVERSION_BADNESS;
  3897. default:
  3898. return INCOMPATIBLE_TYPE_BADNESS;
  3899. }
  3900. }
  3901. /* rank_one_type helper for when PARM's type code is TYPE_CODE_CHAR. */
  3902. static struct rank
  3903. rank_one_type_parm_char (struct type *parm, struct type *arg, struct value *value)
  3904. {
  3905. switch (arg->code ())
  3906. {
  3907. case TYPE_CODE_RANGE:
  3908. case TYPE_CODE_BOOL:
  3909. case TYPE_CODE_ENUM:
  3910. if (arg->is_declared_class ())
  3911. return INCOMPATIBLE_TYPE_BADNESS;
  3912. return INTEGER_CONVERSION_BADNESS;
  3913. case TYPE_CODE_FLT:
  3914. return INT_FLOAT_CONVERSION_BADNESS;
  3915. case TYPE_CODE_INT:
  3916. if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
  3917. return INTEGER_CONVERSION_BADNESS;
  3918. else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  3919. return INTEGER_PROMOTION_BADNESS;
  3920. /* fall through */
  3921. case TYPE_CODE_CHAR:
  3922. /* Deal with signed, unsigned, and plain chars for C++ and
  3923. with int cases falling through from previous case. */
  3924. if (parm->has_no_signedness ())
  3925. {
  3926. if (arg->has_no_signedness ())
  3927. return EXACT_MATCH_BADNESS;
  3928. else
  3929. return INTEGER_CONVERSION_BADNESS;
  3930. }
  3931. else if (parm->is_unsigned ())
  3932. {
  3933. if (arg->is_unsigned ())
  3934. return EXACT_MATCH_BADNESS;
  3935. else
  3936. return INTEGER_PROMOTION_BADNESS;
  3937. }
  3938. else if (!arg->has_no_signedness () && !arg->is_unsigned ())
  3939. return EXACT_MATCH_BADNESS;
  3940. else
  3941. return INTEGER_CONVERSION_BADNESS;
  3942. default:
  3943. return INCOMPATIBLE_TYPE_BADNESS;
  3944. }
  3945. }
  3946. /* rank_one_type helper for when PARM's type code is TYPE_CODE_RANGE. */
  3947. static struct rank
  3948. rank_one_type_parm_range (struct type *parm, struct type *arg, struct value *value)
  3949. {
  3950. switch (arg->code ())
  3951. {
  3952. case TYPE_CODE_INT:
  3953. case TYPE_CODE_CHAR:
  3954. case TYPE_CODE_RANGE:
  3955. case TYPE_CODE_BOOL:
  3956. case TYPE_CODE_ENUM:
  3957. return INTEGER_CONVERSION_BADNESS;
  3958. case TYPE_CODE_FLT:
  3959. return INT_FLOAT_CONVERSION_BADNESS;
  3960. default:
  3961. return INCOMPATIBLE_TYPE_BADNESS;
  3962. }
  3963. }
  3964. /* rank_one_type helper for when PARM's type code is TYPE_CODE_BOOL. */
  3965. static struct rank
  3966. rank_one_type_parm_bool (struct type *parm, struct type *arg, struct value *value)
  3967. {
  3968. switch (arg->code ())
  3969. {
  3970. /* n3290 draft, section 4.12.1 (conv.bool):
  3971. "A prvalue of arithmetic, unscoped enumeration, pointer, or
  3972. pointer to member type can be converted to a prvalue of type
  3973. bool. A zero value, null pointer value, or null member pointer
  3974. value is converted to false; any other value is converted to
  3975. true. A prvalue of type std::nullptr_t can be converted to a
  3976. prvalue of type bool; the resulting value is false." */
  3977. case TYPE_CODE_INT:
  3978. case TYPE_CODE_CHAR:
  3979. case TYPE_CODE_ENUM:
  3980. case TYPE_CODE_FLT:
  3981. case TYPE_CODE_MEMBERPTR:
  3982. case TYPE_CODE_PTR:
  3983. return BOOL_CONVERSION_BADNESS;
  3984. case TYPE_CODE_RANGE:
  3985. return INCOMPATIBLE_TYPE_BADNESS;
  3986. case TYPE_CODE_BOOL:
  3987. return EXACT_MATCH_BADNESS;
  3988. default:
  3989. return INCOMPATIBLE_TYPE_BADNESS;
  3990. }
  3991. }
  3992. /* rank_one_type helper for when PARM's type code is TYPE_CODE_FLOAT. */
  3993. static struct rank
  3994. rank_one_type_parm_float (struct type *parm, struct type *arg, struct value *value)
  3995. {
  3996. switch (arg->code ())
  3997. {
  3998. case TYPE_CODE_FLT:
  3999. if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
  4000. return FLOAT_PROMOTION_BADNESS;
  4001. else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm))
  4002. return EXACT_MATCH_BADNESS;
  4003. else
  4004. return FLOAT_CONVERSION_BADNESS;
  4005. case TYPE_CODE_INT:
  4006. case TYPE_CODE_BOOL:
  4007. case TYPE_CODE_ENUM:
  4008. case TYPE_CODE_RANGE:
  4009. case TYPE_CODE_CHAR:
  4010. return INT_FLOAT_CONVERSION_BADNESS;
  4011. default:
  4012. return INCOMPATIBLE_TYPE_BADNESS;
  4013. }
  4014. }
  4015. /* rank_one_type helper for when PARM's type code is TYPE_CODE_COMPLEX. */
  4016. static struct rank
  4017. rank_one_type_parm_complex (struct type *parm, struct type *arg, struct value *value)
  4018. {
  4019. switch (arg->code ())
  4020. { /* Strictly not needed for C++, but... */
  4021. case TYPE_CODE_FLT:
  4022. return FLOAT_PROMOTION_BADNESS;
  4023. case TYPE_CODE_COMPLEX:
  4024. return EXACT_MATCH_BADNESS;
  4025. default:
  4026. return INCOMPATIBLE_TYPE_BADNESS;
  4027. }
  4028. }
  4029. /* rank_one_type helper for when PARM's type code is TYPE_CODE_STRUCT. */
  4030. static struct rank
  4031. rank_one_type_parm_struct (struct type *parm, struct type *arg, struct value *value)
  4032. {
  4033. struct rank rank = {0, 0};
  4034. switch (arg->code ())
  4035. {
  4036. case TYPE_CODE_STRUCT:
  4037. /* Check for derivation */
  4038. rank.subrank = distance_to_ancestor (parm, arg, 0);
  4039. if (rank.subrank >= 0)
  4040. return sum_ranks (BASE_CONVERSION_BADNESS, rank);
  4041. /* fall through */
  4042. default:
  4043. return INCOMPATIBLE_TYPE_BADNESS;
  4044. }
  4045. }
  4046. /* rank_one_type helper for when PARM's type code is TYPE_CODE_SET. */
  4047. static struct rank
  4048. rank_one_type_parm_set (struct type *parm, struct type *arg, struct value *value)
  4049. {
  4050. switch (arg->code ())
  4051. {
  4052. /* Not in C++ */
  4053. case TYPE_CODE_SET:
  4054. return rank_one_type (parm->field (0).type (),
  4055. arg->field (0).type (), NULL);
  4056. default:
  4057. return INCOMPATIBLE_TYPE_BADNESS;
  4058. }
  4059. }
  4060. /* Compare one type (PARM) for compatibility with another (ARG).
  4061. * PARM is intended to be the parameter type of a function; and
  4062. * ARG is the supplied argument's type. This function tests if
  4063. * the latter can be converted to the former.
  4064. * VALUE is the argument's value or NULL if none (or called recursively)
  4065. *
  4066. * Return 0 if they are identical types;
  4067. * Otherwise, return an integer which corresponds to how compatible
  4068. * PARM is to ARG. The higher the return value, the worse the match.
  4069. * Generally the "bad" conversions are all uniformly assigned
  4070. * INVALID_CONVERSION. */
  4071. struct rank
  4072. rank_one_type (struct type *parm, struct type *arg, struct value *value)
  4073. {
  4074. struct rank rank = {0,0};
  4075. /* Resolve typedefs */
  4076. if (parm->code () == TYPE_CODE_TYPEDEF)
  4077. parm = check_typedef (parm);
  4078. if (arg->code () == TYPE_CODE_TYPEDEF)
  4079. arg = check_typedef (arg);
  4080. if (TYPE_IS_REFERENCE (parm) && value != NULL)
  4081. {
  4082. if (VALUE_LVAL (value) == not_lval)
  4083. {
  4084. /* Rvalues should preferably bind to rvalue references or const
  4085. lvalue references. */
  4086. if (parm->code () == TYPE_CODE_RVALUE_REF)
  4087. rank.subrank = REFERENCE_CONVERSION_RVALUE;
  4088. else if (TYPE_CONST (TYPE_TARGET_TYPE (parm)))
  4089. rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE;
  4090. else
  4091. return INCOMPATIBLE_TYPE_BADNESS;
  4092. return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS);
  4093. }
  4094. else
  4095. {
  4096. /* It's illegal to pass an lvalue as an rvalue. */
  4097. if (parm->code () == TYPE_CODE_RVALUE_REF)
  4098. return INCOMPATIBLE_TYPE_BADNESS;
  4099. }
  4100. }
  4101. if (types_equal (parm, arg))
  4102. {
  4103. struct type *t1 = parm;
  4104. struct type *t2 = arg;
  4105. /* For pointers and references, compare target type. */
  4106. if (parm->is_pointer_or_reference ())
  4107. {
  4108. t1 = TYPE_TARGET_TYPE (parm);
  4109. t2 = TYPE_TARGET_TYPE (arg);
  4110. }
  4111. /* Make sure they are CV equal, too. */
  4112. if (TYPE_CONST (t1) != TYPE_CONST (t2))
  4113. rank.subrank |= CV_CONVERSION_CONST;
  4114. if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
  4115. rank.subrank |= CV_CONVERSION_VOLATILE;
  4116. if (rank.subrank != 0)
  4117. return sum_ranks (CV_CONVERSION_BADNESS, rank);
  4118. return EXACT_MATCH_BADNESS;
  4119. }
  4120. /* See through references, since we can almost make non-references
  4121. references. */
  4122. if (TYPE_IS_REFERENCE (arg))
  4123. return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL),
  4124. REFERENCE_SEE_THROUGH_BADNESS));
  4125. if (TYPE_IS_REFERENCE (parm))
  4126. return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL),
  4127. REFERENCE_SEE_THROUGH_BADNESS));
  4128. if (overload_debug)
  4129. {
  4130. /* Debugging only. */
  4131. gdb_printf (gdb_stderr,
  4132. "------ Arg is %s [%d], parm is %s [%d]\n",
  4133. arg->name (), arg->code (),
  4134. parm->name (), parm->code ());
  4135. }
  4136. /* x -> y means arg of type x being supplied for parameter of type y. */
  4137. switch (parm->code ())
  4138. {
  4139. case TYPE_CODE_PTR:
  4140. return rank_one_type_parm_ptr (parm, arg, value);
  4141. case TYPE_CODE_ARRAY:
  4142. return rank_one_type_parm_array (parm, arg, value);
  4143. case TYPE_CODE_FUNC:
  4144. return rank_one_type_parm_func (parm, arg, value);
  4145. case TYPE_CODE_INT:
  4146. return rank_one_type_parm_int (parm, arg, value);
  4147. case TYPE_CODE_ENUM:
  4148. return rank_one_type_parm_enum (parm, arg, value);
  4149. case TYPE_CODE_CHAR:
  4150. return rank_one_type_parm_char (parm, arg, value);
  4151. case TYPE_CODE_RANGE:
  4152. return rank_one_type_parm_range (parm, arg, value);
  4153. case TYPE_CODE_BOOL:
  4154. return rank_one_type_parm_bool (parm, arg, value);
  4155. case TYPE_CODE_FLT:
  4156. return rank_one_type_parm_float (parm, arg, value);
  4157. case TYPE_CODE_COMPLEX:
  4158. return rank_one_type_parm_complex (parm, arg, value);
  4159. case TYPE_CODE_STRUCT:
  4160. return rank_one_type_parm_struct (parm, arg, value);
  4161. case TYPE_CODE_SET:
  4162. return rank_one_type_parm_set (parm, arg, value);
  4163. default:
  4164. return INCOMPATIBLE_TYPE_BADNESS;
  4165. } /* switch (arg->code ()) */
  4166. }
  4167. /* End of functions for overload resolution. */
  4168. /* Routines to pretty-print types. */
  4169. static void
  4170. print_bit_vector (B_TYPE *bits, int nbits)
  4171. {
  4172. int bitno;
  4173. for (bitno = 0; bitno < nbits; bitno++)
  4174. {
  4175. if ((bitno % 8) == 0)
  4176. {
  4177. gdb_puts (" ");
  4178. }
  4179. if (B_TST (bits, bitno))
  4180. gdb_printf (("1"));
  4181. else
  4182. gdb_printf (("0"));
  4183. }
  4184. }
  4185. /* Note the first arg should be the "this" pointer, we may not want to
  4186. include it since we may get into a infinitely recursive
  4187. situation. */
  4188. static void
  4189. print_args (struct field *args, int nargs, int spaces)
  4190. {
  4191. if (args != NULL)
  4192. {
  4193. int i;
  4194. for (i = 0; i < nargs; i++)
  4195. {
  4196. gdb_printf
  4197. ("%*s[%d] name '%s'\n", spaces, "", i,
  4198. args[i].name () != NULL ? args[i].name () : "<NULL>");
  4199. recursive_dump_type (args[i].type (), spaces + 2);
  4200. }
  4201. }
  4202. }
  4203. int
  4204. field_is_static (struct field *f)
  4205. {
  4206. /* "static" fields are the fields whose location is not relative
  4207. to the address of the enclosing struct. It would be nice to
  4208. have a dedicated flag that would be set for static fields when
  4209. the type is being created. But in practice, checking the field
  4210. loc_kind should give us an accurate answer. */
  4211. return (f->loc_kind () == FIELD_LOC_KIND_PHYSNAME
  4212. || f->loc_kind () == FIELD_LOC_KIND_PHYSADDR);
  4213. }
  4214. static void
  4215. dump_fn_fieldlists (struct type *type, int spaces)
  4216. {
  4217. int method_idx;
  4218. int overload_idx;
  4219. struct fn_field *f;
  4220. gdb_printf ("%*sfn_fieldlists %s\n", spaces, "",
  4221. host_address_to_string (TYPE_FN_FIELDLISTS (type)));
  4222. for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
  4223. {
  4224. f = TYPE_FN_FIELDLIST1 (type, method_idx);
  4225. gdb_printf
  4226. ("%*s[%d] name '%s' (%s) length %d\n", spaces + 2, "",
  4227. method_idx,
  4228. TYPE_FN_FIELDLIST_NAME (type, method_idx),
  4229. host_address_to_string (TYPE_FN_FIELDLIST_NAME (type, method_idx)),
  4230. TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
  4231. for (overload_idx = 0;
  4232. overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
  4233. overload_idx++)
  4234. {
  4235. gdb_printf
  4236. ("%*s[%d] physname '%s' (%s)\n",
  4237. spaces + 4, "", overload_idx,
  4238. TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
  4239. host_address_to_string (TYPE_FN_FIELD_PHYSNAME (f,
  4240. overload_idx)));
  4241. gdb_printf
  4242. ("%*stype %s\n", spaces + 8, "",
  4243. host_address_to_string (TYPE_FN_FIELD_TYPE (f, overload_idx)));
  4244. recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
  4245. spaces + 8 + 2);
  4246. gdb_printf
  4247. ("%*sargs %s\n", spaces + 8, "",
  4248. host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
  4249. print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
  4250. TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
  4251. spaces + 8 + 2);
  4252. gdb_printf
  4253. ("%*sfcontext %s\n", spaces + 8, "",
  4254. host_address_to_string (TYPE_FN_FIELD_FCONTEXT (f,
  4255. overload_idx)));
  4256. gdb_printf ("%*sis_const %d\n", spaces + 8, "",
  4257. TYPE_FN_FIELD_CONST (f, overload_idx));
  4258. gdb_printf ("%*sis_volatile %d\n", spaces + 8, "",
  4259. TYPE_FN_FIELD_VOLATILE (f, overload_idx));
  4260. gdb_printf ("%*sis_private %d\n", spaces + 8, "",
  4261. TYPE_FN_FIELD_PRIVATE (f, overload_idx));
  4262. gdb_printf ("%*sis_protected %d\n", spaces + 8, "",
  4263. TYPE_FN_FIELD_PROTECTED (f, overload_idx));
  4264. gdb_printf ("%*sis_stub %d\n", spaces + 8, "",
  4265. TYPE_FN_FIELD_STUB (f, overload_idx));
  4266. gdb_printf ("%*sdefaulted %d\n", spaces + 8, "",
  4267. TYPE_FN_FIELD_DEFAULTED (f, overload_idx));
  4268. gdb_printf ("%*sis_deleted %d\n", spaces + 8, "",
  4269. TYPE_FN_FIELD_DELETED (f, overload_idx));
  4270. gdb_printf ("%*svoffset %u\n", spaces + 8, "",
  4271. TYPE_FN_FIELD_VOFFSET (f, overload_idx));
  4272. }
  4273. }
  4274. }
  4275. static void
  4276. print_cplus_stuff (struct type *type, int spaces)
  4277. {
  4278. gdb_printf ("%*svptr_fieldno %d\n", spaces, "",
  4279. TYPE_VPTR_FIELDNO (type));
  4280. gdb_printf ("%*svptr_basetype %s\n", spaces, "",
  4281. host_address_to_string (TYPE_VPTR_BASETYPE (type)));
  4282. if (TYPE_VPTR_BASETYPE (type) != NULL)
  4283. recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
  4284. gdb_printf ("%*sn_baseclasses %d\n", spaces, "",
  4285. TYPE_N_BASECLASSES (type));
  4286. gdb_printf ("%*snfn_fields %d\n", spaces, "",
  4287. TYPE_NFN_FIELDS (type));
  4288. if (TYPE_N_BASECLASSES (type) > 0)
  4289. {
  4290. gdb_printf
  4291. ("%*svirtual_field_bits (%d bits at *%s)",
  4292. spaces, "", TYPE_N_BASECLASSES (type),
  4293. host_address_to_string (TYPE_FIELD_VIRTUAL_BITS (type)));
  4294. print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
  4295. TYPE_N_BASECLASSES (type));
  4296. gdb_puts ("\n");
  4297. }
  4298. if (type->num_fields () > 0)
  4299. {
  4300. if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
  4301. {
  4302. gdb_printf
  4303. ("%*sprivate_field_bits (%d bits at *%s)",
  4304. spaces, "", type->num_fields (),
  4305. host_address_to_string (TYPE_FIELD_PRIVATE_BITS (type)));
  4306. print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
  4307. type->num_fields ());
  4308. gdb_puts ("\n");
  4309. }
  4310. if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
  4311. {
  4312. gdb_printf
  4313. ("%*sprotected_field_bits (%d bits at *%s",
  4314. spaces, "", type->num_fields (),
  4315. host_address_to_string (TYPE_FIELD_PROTECTED_BITS (type)));
  4316. print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
  4317. type->num_fields ());
  4318. gdb_puts ("\n");
  4319. }
  4320. }
  4321. if (TYPE_NFN_FIELDS (type) > 0)
  4322. {
  4323. dump_fn_fieldlists (type, spaces);
  4324. }
  4325. gdb_printf ("%*scalling_convention %d\n", spaces, "",
  4326. TYPE_CPLUS_CALLING_CONVENTION (type));
  4327. }
  4328. /* Print the contents of the TYPE's type_specific union, assuming that
  4329. its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */
  4330. static void
  4331. print_gnat_stuff (struct type *type, int spaces)
  4332. {
  4333. struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type);
  4334. if (descriptive_type == NULL)
  4335. gdb_printf ("%*sno descriptive type\n", spaces + 2, "");
  4336. else
  4337. {
  4338. gdb_printf ("%*sdescriptive type\n", spaces + 2, "");
  4339. recursive_dump_type (descriptive_type, spaces + 4);
  4340. }
  4341. }
  4342. /* Print the contents of the TYPE's type_specific union, assuming that
  4343. its type-specific kind is TYPE_SPECIFIC_FIXED_POINT. */
  4344. static void
  4345. print_fixed_point_type_info (struct type *type, int spaces)
  4346. {
  4347. gdb_printf ("%*sscaling factor: %s\n", spaces + 2, "",
  4348. type->fixed_point_scaling_factor ().str ().c_str ());
  4349. }
  4350. static struct obstack dont_print_type_obstack;
  4351. /* Print the dynamic_prop PROP. */
  4352. static void
  4353. dump_dynamic_prop (dynamic_prop const& prop)
  4354. {
  4355. switch (prop.kind ())
  4356. {
  4357. case PROP_CONST:
  4358. gdb_printf ("%s", plongest (prop.const_val ()));
  4359. break;
  4360. case PROP_UNDEFINED:
  4361. gdb_printf ("(undefined)");
  4362. break;
  4363. case PROP_LOCEXPR:
  4364. case PROP_LOCLIST:
  4365. gdb_printf ("(dynamic)");
  4366. break;
  4367. default:
  4368. gdb_assert_not_reached ("unhandled prop kind");
  4369. break;
  4370. }
  4371. }
  4372. void
  4373. recursive_dump_type (struct type *type, int spaces)
  4374. {
  4375. int idx;
  4376. if (spaces == 0)
  4377. obstack_begin (&dont_print_type_obstack, 0);
  4378. if (type->num_fields () > 0
  4379. || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0))
  4380. {
  4381. struct type **first_dont_print
  4382. = (struct type **) obstack_base (&dont_print_type_obstack);
  4383. int i = (struct type **)
  4384. obstack_next_free (&dont_print_type_obstack) - first_dont_print;
  4385. while (--i >= 0)
  4386. {
  4387. if (type == first_dont_print[i])
  4388. {
  4389. gdb_printf ("%*stype node %s", spaces, "",
  4390. host_address_to_string (type));
  4391. gdb_printf (_(" <same as already seen type>\n"));
  4392. return;
  4393. }
  4394. }
  4395. obstack_ptr_grow (&dont_print_type_obstack, type);
  4396. }
  4397. gdb_printf ("%*stype node %s\n", spaces, "",
  4398. host_address_to_string (type));
  4399. gdb_printf ("%*sname '%s' (%s)\n", spaces, "",
  4400. type->name () ? type->name () : "<NULL>",
  4401. host_address_to_string (type->name ()));
  4402. gdb_printf ("%*scode 0x%x ", spaces, "", type->code ());
  4403. switch (type->code ())
  4404. {
  4405. case TYPE_CODE_UNDEF:
  4406. gdb_printf ("(TYPE_CODE_UNDEF)");
  4407. break;
  4408. case TYPE_CODE_PTR:
  4409. gdb_printf ("(TYPE_CODE_PTR)");
  4410. break;
  4411. case TYPE_CODE_ARRAY:
  4412. gdb_printf ("(TYPE_CODE_ARRAY)");
  4413. break;
  4414. case TYPE_CODE_STRUCT:
  4415. gdb_printf ("(TYPE_CODE_STRUCT)");
  4416. break;
  4417. case TYPE_CODE_UNION:
  4418. gdb_printf ("(TYPE_CODE_UNION)");
  4419. break;
  4420. case TYPE_CODE_ENUM:
  4421. gdb_printf ("(TYPE_CODE_ENUM)");
  4422. break;
  4423. case TYPE_CODE_FLAGS:
  4424. gdb_printf ("(TYPE_CODE_FLAGS)");
  4425. break;
  4426. case TYPE_CODE_FUNC:
  4427. gdb_printf ("(TYPE_CODE_FUNC)");
  4428. break;
  4429. case TYPE_CODE_INT:
  4430. gdb_printf ("(TYPE_CODE_INT)");
  4431. break;
  4432. case TYPE_CODE_FLT:
  4433. gdb_printf ("(TYPE_CODE_FLT)");
  4434. break;
  4435. case TYPE_CODE_VOID:
  4436. gdb_printf ("(TYPE_CODE_VOID)");
  4437. break;
  4438. case TYPE_CODE_SET:
  4439. gdb_printf ("(TYPE_CODE_SET)");
  4440. break;
  4441. case TYPE_CODE_RANGE:
  4442. gdb_printf ("(TYPE_CODE_RANGE)");
  4443. break;
  4444. case TYPE_CODE_STRING:
  4445. gdb_printf ("(TYPE_CODE_STRING)");
  4446. break;
  4447. case TYPE_CODE_ERROR:
  4448. gdb_printf ("(TYPE_CODE_ERROR)");
  4449. break;
  4450. case TYPE_CODE_MEMBERPTR:
  4451. gdb_printf ("(TYPE_CODE_MEMBERPTR)");
  4452. break;
  4453. case TYPE_CODE_METHODPTR:
  4454. gdb_printf ("(TYPE_CODE_METHODPTR)");
  4455. break;
  4456. case TYPE_CODE_METHOD:
  4457. gdb_printf ("(TYPE_CODE_METHOD)");
  4458. break;
  4459. case TYPE_CODE_REF:
  4460. gdb_printf ("(TYPE_CODE_REF)");
  4461. break;
  4462. case TYPE_CODE_CHAR:
  4463. gdb_printf ("(TYPE_CODE_CHAR)");
  4464. break;
  4465. case TYPE_CODE_BOOL:
  4466. gdb_printf ("(TYPE_CODE_BOOL)");
  4467. break;
  4468. case TYPE_CODE_COMPLEX:
  4469. gdb_printf ("(TYPE_CODE_COMPLEX)");
  4470. break;
  4471. case TYPE_CODE_TYPEDEF:
  4472. gdb_printf ("(TYPE_CODE_TYPEDEF)");
  4473. break;
  4474. case TYPE_CODE_NAMESPACE:
  4475. gdb_printf ("(TYPE_CODE_NAMESPACE)");
  4476. break;
  4477. case TYPE_CODE_FIXED_POINT:
  4478. gdb_printf ("(TYPE_CODE_FIXED_POINT)");
  4479. break;
  4480. default:
  4481. gdb_printf ("(UNKNOWN TYPE CODE)");
  4482. break;
  4483. }
  4484. gdb_puts ("\n");
  4485. gdb_printf ("%*slength %s\n", spaces, "",
  4486. pulongest (TYPE_LENGTH (type)));
  4487. if (type->is_objfile_owned ())
  4488. gdb_printf ("%*sobjfile %s\n", spaces, "",
  4489. host_address_to_string (type->objfile_owner ()));
  4490. else
  4491. gdb_printf ("%*sgdbarch %s\n", spaces, "",
  4492. host_address_to_string (type->arch_owner ()));
  4493. gdb_printf ("%*starget_type %s\n", spaces, "",
  4494. host_address_to_string (TYPE_TARGET_TYPE (type)));
  4495. if (TYPE_TARGET_TYPE (type) != NULL)
  4496. {
  4497. recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
  4498. }
  4499. gdb_printf ("%*spointer_type %s\n", spaces, "",
  4500. host_address_to_string (TYPE_POINTER_TYPE (type)));
  4501. gdb_printf ("%*sreference_type %s\n", spaces, "",
  4502. host_address_to_string (TYPE_REFERENCE_TYPE (type)));
  4503. gdb_printf ("%*stype_chain %s\n", spaces, "",
  4504. host_address_to_string (TYPE_CHAIN (type)));
  4505. gdb_printf ("%*sinstance_flags 0x%x", spaces, "",
  4506. (unsigned) type->instance_flags ());
  4507. if (TYPE_CONST (type))
  4508. {
  4509. gdb_puts (" TYPE_CONST");
  4510. }
  4511. if (TYPE_VOLATILE (type))
  4512. {
  4513. gdb_puts (" TYPE_VOLATILE");
  4514. }
  4515. if (TYPE_CODE_SPACE (type))
  4516. {
  4517. gdb_puts (" TYPE_CODE_SPACE");
  4518. }
  4519. if (TYPE_DATA_SPACE (type))
  4520. {
  4521. gdb_puts (" TYPE_DATA_SPACE");
  4522. }
  4523. if (TYPE_ADDRESS_CLASS_1 (type))
  4524. {
  4525. gdb_puts (" TYPE_ADDRESS_CLASS_1");
  4526. }
  4527. if (TYPE_ADDRESS_CLASS_2 (type))
  4528. {
  4529. gdb_puts (" TYPE_ADDRESS_CLASS_2");
  4530. }
  4531. if (TYPE_RESTRICT (type))
  4532. {
  4533. gdb_puts (" TYPE_RESTRICT");
  4534. }
  4535. if (TYPE_ATOMIC (type))
  4536. {
  4537. gdb_puts (" TYPE_ATOMIC");
  4538. }
  4539. gdb_puts ("\n");
  4540. gdb_printf ("%*sflags", spaces, "");
  4541. if (type->is_unsigned ())
  4542. {
  4543. gdb_puts (" TYPE_UNSIGNED");
  4544. }
  4545. if (type->has_no_signedness ())
  4546. {
  4547. gdb_puts (" TYPE_NOSIGN");
  4548. }
  4549. if (type->endianity_is_not_default ())
  4550. {
  4551. gdb_puts (" TYPE_ENDIANITY_NOT_DEFAULT");
  4552. }
  4553. if (type->is_stub ())
  4554. {
  4555. gdb_puts (" TYPE_STUB");
  4556. }
  4557. if (type->target_is_stub ())
  4558. {
  4559. gdb_puts (" TYPE_TARGET_STUB");
  4560. }
  4561. if (type->is_prototyped ())
  4562. {
  4563. gdb_puts (" TYPE_PROTOTYPED");
  4564. }
  4565. if (type->has_varargs ())
  4566. {
  4567. gdb_puts (" TYPE_VARARGS");
  4568. }
  4569. /* This is used for things like AltiVec registers on ppc. Gcc emits
  4570. an attribute for the array type, which tells whether or not we
  4571. have a vector, instead of a regular array. */
  4572. if (type->is_vector ())
  4573. {
  4574. gdb_puts (" TYPE_VECTOR");
  4575. }
  4576. if (type->is_fixed_instance ())
  4577. {
  4578. gdb_puts (" TYPE_FIXED_INSTANCE");
  4579. }
  4580. if (type->stub_is_supported ())
  4581. {
  4582. gdb_puts (" TYPE_STUB_SUPPORTED");
  4583. }
  4584. if (TYPE_NOTTEXT (type))
  4585. {
  4586. gdb_puts (" TYPE_NOTTEXT");
  4587. }
  4588. gdb_puts ("\n");
  4589. gdb_printf ("%*snfields %d ", spaces, "", type->num_fields ());
  4590. if (TYPE_ASSOCIATED_PROP (type) != nullptr
  4591. || TYPE_ALLOCATED_PROP (type) != nullptr)
  4592. {
  4593. gdb_printf ("%*s", spaces, "");
  4594. if (TYPE_ASSOCIATED_PROP (type) != nullptr)
  4595. {
  4596. gdb_printf ("associated ");
  4597. dump_dynamic_prop (*TYPE_ASSOCIATED_PROP (type));
  4598. }
  4599. if (TYPE_ALLOCATED_PROP (type) != nullptr)
  4600. {
  4601. if (TYPE_ASSOCIATED_PROP (type) != nullptr)
  4602. gdb_printf (" ");
  4603. gdb_printf ("allocated ");
  4604. dump_dynamic_prop (*TYPE_ALLOCATED_PROP (type));
  4605. }
  4606. gdb_printf ("\n");
  4607. }
  4608. gdb_printf ("%s\n", host_address_to_string (type->fields ()));
  4609. for (idx = 0; idx < type->num_fields (); idx++)
  4610. {
  4611. if (type->code () == TYPE_CODE_ENUM)
  4612. gdb_printf ("%*s[%d] enumval %s type ", spaces + 2, "",
  4613. idx, plongest (type->field (idx).loc_enumval ()));
  4614. else
  4615. gdb_printf ("%*s[%d] bitpos %s bitsize %d type ", spaces + 2, "",
  4616. idx, plongest (type->field (idx).loc_bitpos ()),
  4617. TYPE_FIELD_BITSIZE (type, idx));
  4618. gdb_printf ("%s name '%s' (%s)\n",
  4619. host_address_to_string (type->field (idx).type ()),
  4620. type->field (idx).name () != NULL
  4621. ? type->field (idx).name ()
  4622. : "<NULL>",
  4623. host_address_to_string (type->field (idx).name ()));
  4624. if (type->field (idx).type () != NULL)
  4625. {
  4626. recursive_dump_type (type->field (idx).type (), spaces + 4);
  4627. }
  4628. }
  4629. if (type->code () == TYPE_CODE_RANGE)
  4630. {
  4631. gdb_printf ("%*slow ", spaces, "");
  4632. dump_dynamic_prop (type->bounds ()->low);
  4633. gdb_printf (" high ");
  4634. dump_dynamic_prop (type->bounds ()->high);
  4635. gdb_printf ("\n");
  4636. }
  4637. switch (TYPE_SPECIFIC_FIELD (type))
  4638. {
  4639. case TYPE_SPECIFIC_CPLUS_STUFF:
  4640. gdb_printf ("%*scplus_stuff %s\n", spaces, "",
  4641. host_address_to_string (TYPE_CPLUS_SPECIFIC (type)));
  4642. print_cplus_stuff (type, spaces);
  4643. break;
  4644. case TYPE_SPECIFIC_GNAT_STUFF:
  4645. gdb_printf ("%*sgnat_stuff %s\n", spaces, "",
  4646. host_address_to_string (TYPE_GNAT_SPECIFIC (type)));
  4647. print_gnat_stuff (type, spaces);
  4648. break;
  4649. case TYPE_SPECIFIC_FLOATFORMAT:
  4650. gdb_printf ("%*sfloatformat ", spaces, "");
  4651. if (TYPE_FLOATFORMAT (type) == NULL
  4652. || TYPE_FLOATFORMAT (type)->name == NULL)
  4653. gdb_puts ("(null)");
  4654. else
  4655. gdb_puts (TYPE_FLOATFORMAT (type)->name);
  4656. gdb_puts ("\n");
  4657. break;
  4658. case TYPE_SPECIFIC_FUNC:
  4659. gdb_printf ("%*scalling_convention %d\n", spaces, "",
  4660. TYPE_CALLING_CONVENTION (type));
  4661. /* tail_call_list is not printed. */
  4662. break;
  4663. case TYPE_SPECIFIC_SELF_TYPE:
  4664. gdb_printf ("%*sself_type %s\n", spaces, "",
  4665. host_address_to_string (TYPE_SELF_TYPE (type)));
  4666. break;
  4667. case TYPE_SPECIFIC_FIXED_POINT:
  4668. gdb_printf ("%*sfixed_point_info ", spaces, "");
  4669. print_fixed_point_type_info (type, spaces);
  4670. gdb_puts ("\n");
  4671. break;
  4672. case TYPE_SPECIFIC_INT:
  4673. if (type->bit_size_differs_p ())
  4674. {
  4675. unsigned bit_size = type->bit_size ();
  4676. unsigned bit_off = type->bit_offset ();
  4677. gdb_printf ("%*s bit size = %u, bit offset = %u\n", spaces, "",
  4678. bit_size, bit_off);
  4679. }
  4680. break;
  4681. }
  4682. if (spaces == 0)
  4683. obstack_free (&dont_print_type_obstack, NULL);
  4684. }
  4685. /* Trivial helpers for the libiberty hash table, for mapping one
  4686. type to another. */
  4687. struct type_pair : public allocate_on_obstack
  4688. {
  4689. type_pair (struct type *old_, struct type *newobj_)
  4690. : old (old_), newobj (newobj_)
  4691. {}
  4692. struct type * const old, * const newobj;
  4693. };
  4694. static hashval_t
  4695. type_pair_hash (const void *item)
  4696. {
  4697. const struct type_pair *pair = (const struct type_pair *) item;
  4698. return htab_hash_pointer (pair->old);
  4699. }
  4700. static int
  4701. type_pair_eq (const void *item_lhs, const void *item_rhs)
  4702. {
  4703. const struct type_pair *lhs = (const struct type_pair *) item_lhs;
  4704. const struct type_pair *rhs = (const struct type_pair *) item_rhs;
  4705. return lhs->old == rhs->old;
  4706. }
  4707. /* Allocate the hash table used by copy_type_recursive to walk
  4708. types without duplicates. We use OBJFILE's obstack, because
  4709. OBJFILE is about to be deleted. */
  4710. htab_up
  4711. create_copied_types_hash (struct objfile *objfile)
  4712. {
  4713. return htab_up (htab_create_alloc_ex (1, type_pair_hash, type_pair_eq,
  4714. NULL, &objfile->objfile_obstack,
  4715. hashtab_obstack_allocate,
  4716. dummy_obstack_deallocate));
  4717. }
  4718. /* Recursively copy (deep copy) a dynamic attribute list of a type. */
  4719. static struct dynamic_prop_list *
  4720. copy_dynamic_prop_list (struct obstack *objfile_obstack,
  4721. struct dynamic_prop_list *list)
  4722. {
  4723. struct dynamic_prop_list *copy = list;
  4724. struct dynamic_prop_list **node_ptr = &copy;
  4725. while (*node_ptr != NULL)
  4726. {
  4727. struct dynamic_prop_list *node_copy;
  4728. node_copy = ((struct dynamic_prop_list *)
  4729. obstack_copy (objfile_obstack, *node_ptr,
  4730. sizeof (struct dynamic_prop_list)));
  4731. node_copy->prop = (*node_ptr)->prop;
  4732. *node_ptr = node_copy;
  4733. node_ptr = &node_copy->next;
  4734. }
  4735. return copy;
  4736. }
  4737. /* Recursively copy (deep copy) TYPE, if it is associated with
  4738. OBJFILE. Return a new type owned by the gdbarch associated with the type, a
  4739. saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if
  4740. it is not associated with OBJFILE. */
  4741. struct type *
  4742. copy_type_recursive (struct objfile *objfile,
  4743. struct type *type,
  4744. htab_t copied_types)
  4745. {
  4746. void **slot;
  4747. struct type *new_type;
  4748. if (!type->is_objfile_owned ())
  4749. return type;
  4750. /* This type shouldn't be pointing to any types in other objfiles;
  4751. if it did, the type might disappear unexpectedly. */
  4752. gdb_assert (type->objfile_owner () == objfile);
  4753. struct type_pair pair (type, nullptr);
  4754. slot = htab_find_slot (copied_types, &pair, INSERT);
  4755. if (*slot != NULL)
  4756. return ((struct type_pair *) *slot)->newobj;
  4757. new_type = alloc_type_arch (type->arch ());
  4758. /* We must add the new type to the hash table immediately, in case
  4759. we encounter this type again during a recursive call below. */
  4760. struct type_pair *stored
  4761. = new (&objfile->objfile_obstack) struct type_pair (type, new_type);
  4762. *slot = stored;
  4763. /* Copy the common fields of types. For the main type, we simply
  4764. copy the entire thing and then update specific fields as needed. */
  4765. *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type);
  4766. new_type->set_owner (type->arch ());
  4767. if (type->name ())
  4768. new_type->set_name (xstrdup (type->name ()));
  4769. new_type->set_instance_flags (type->instance_flags ());
  4770. TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
  4771. /* Copy the fields. */
  4772. if (type->num_fields ())
  4773. {
  4774. int i, nfields;
  4775. nfields = type->num_fields ();
  4776. new_type->set_fields
  4777. ((struct field *)
  4778. TYPE_ZALLOC (new_type, nfields * sizeof (struct field)));
  4779. for (i = 0; i < nfields; i++)
  4780. {
  4781. TYPE_FIELD_ARTIFICIAL (new_type, i) =
  4782. TYPE_FIELD_ARTIFICIAL (type, i);
  4783. TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i);
  4784. if (type->field (i).type ())
  4785. new_type->field (i).set_type
  4786. (copy_type_recursive (objfile, type->field (i).type (),
  4787. copied_types));
  4788. if (type->field (i).name ())
  4789. new_type->field (i).set_name (xstrdup (type->field (i).name ()));
  4790. switch (type->field (i).loc_kind ())
  4791. {
  4792. case FIELD_LOC_KIND_BITPOS:
  4793. new_type->field (i).set_loc_bitpos (type->field (i).loc_bitpos ());
  4794. break;
  4795. case FIELD_LOC_KIND_ENUMVAL:
  4796. new_type->field (i).set_loc_enumval (type->field (i).loc_enumval ());
  4797. break;
  4798. case FIELD_LOC_KIND_PHYSADDR:
  4799. new_type->field (i).set_loc_physaddr
  4800. (type->field (i).loc_physaddr ());
  4801. break;
  4802. case FIELD_LOC_KIND_PHYSNAME:
  4803. new_type->field (i).set_loc_physname
  4804. (xstrdup (type->field (i).loc_physname ()));
  4805. break;
  4806. case FIELD_LOC_KIND_DWARF_BLOCK:
  4807. new_type->field (i).set_loc_dwarf_block
  4808. (type->field (i).loc_dwarf_block ());
  4809. break;
  4810. default:
  4811. internal_error (__FILE__, __LINE__,
  4812. _("Unexpected type field location kind: %d"),
  4813. type->field (i).loc_kind ());
  4814. }
  4815. }
  4816. }
  4817. /* For range types, copy the bounds information. */
  4818. if (type->code () == TYPE_CODE_RANGE)
  4819. {
  4820. range_bounds *bounds
  4821. = ((struct range_bounds *) TYPE_ALLOC
  4822. (new_type, sizeof (struct range_bounds)));
  4823. *bounds = *type->bounds ();
  4824. new_type->set_bounds (bounds);
  4825. }
  4826. if (type->main_type->dyn_prop_list != NULL)
  4827. new_type->main_type->dyn_prop_list
  4828. = copy_dynamic_prop_list (&objfile->objfile_obstack,
  4829. type->main_type->dyn_prop_list);
  4830. /* Copy pointers to other types. */
  4831. if (TYPE_TARGET_TYPE (type))
  4832. TYPE_TARGET_TYPE (new_type) =
  4833. copy_type_recursive (objfile,
  4834. TYPE_TARGET_TYPE (type),
  4835. copied_types);
  4836. /* Maybe copy the type_specific bits.
  4837. NOTE drow/2005-12-09: We do not copy the C++-specific bits like
  4838. base classes and methods. There's no fundamental reason why we
  4839. can't, but at the moment it is not needed. */
  4840. switch (TYPE_SPECIFIC_FIELD (type))
  4841. {
  4842. case TYPE_SPECIFIC_NONE:
  4843. break;
  4844. case TYPE_SPECIFIC_FUNC:
  4845. INIT_FUNC_SPECIFIC (new_type);
  4846. TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type);
  4847. TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type);
  4848. TYPE_TAIL_CALL_LIST (new_type) = NULL;
  4849. break;
  4850. case TYPE_SPECIFIC_FLOATFORMAT:
  4851. TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type);
  4852. break;
  4853. case TYPE_SPECIFIC_CPLUS_STUFF:
  4854. INIT_CPLUS_SPECIFIC (new_type);
  4855. break;
  4856. case TYPE_SPECIFIC_GNAT_STUFF:
  4857. INIT_GNAT_SPECIFIC (new_type);
  4858. break;
  4859. case TYPE_SPECIFIC_SELF_TYPE:
  4860. set_type_self_type (new_type,
  4861. copy_type_recursive (objfile, TYPE_SELF_TYPE (type),
  4862. copied_types));
  4863. break;
  4864. case TYPE_SPECIFIC_FIXED_POINT:
  4865. INIT_FIXED_POINT_SPECIFIC (new_type);
  4866. new_type->fixed_point_info ().scaling_factor
  4867. = type->fixed_point_info ().scaling_factor;
  4868. break;
  4869. case TYPE_SPECIFIC_INT:
  4870. TYPE_SPECIFIC_FIELD (new_type) = TYPE_SPECIFIC_INT;
  4871. TYPE_MAIN_TYPE (new_type)->type_specific.int_stuff
  4872. = TYPE_MAIN_TYPE (type)->type_specific.int_stuff;
  4873. break;
  4874. default:
  4875. gdb_assert_not_reached ("bad type_specific_kind");
  4876. }
  4877. return new_type;
  4878. }
  4879. /* Make a copy of the given TYPE, except that the pointer & reference
  4880. types are not preserved.
  4881. This function assumes that the given type has an associated objfile.
  4882. This objfile is used to allocate the new type. */
  4883. struct type *
  4884. copy_type (const struct type *type)
  4885. {
  4886. struct type *new_type;
  4887. gdb_assert (type->is_objfile_owned ());
  4888. new_type = alloc_type_copy (type);
  4889. new_type->set_instance_flags (type->instance_flags ());
  4890. TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
  4891. memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
  4892. sizeof (struct main_type));
  4893. if (type->main_type->dyn_prop_list != NULL)
  4894. new_type->main_type->dyn_prop_list
  4895. = copy_dynamic_prop_list (&type->objfile_owner ()->objfile_obstack,
  4896. type->main_type->dyn_prop_list);
  4897. return new_type;
  4898. }
  4899. /* Helper functions to initialize architecture-specific types. */
  4900. /* Allocate a type structure associated with GDBARCH and set its
  4901. CODE, LENGTH, and NAME fields. */
  4902. struct type *
  4903. arch_type (struct gdbarch *gdbarch,
  4904. enum type_code code, int bit, const char *name)
  4905. {
  4906. struct type *type;
  4907. type = alloc_type_arch (gdbarch);
  4908. set_type_code (type, code);
  4909. gdb_assert ((bit % TARGET_CHAR_BIT) == 0);
  4910. TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT;
  4911. if (name)
  4912. type->set_name (gdbarch_obstack_strdup (gdbarch, name));
  4913. return type;
  4914. }
  4915. /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH.
  4916. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  4917. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  4918. struct type *
  4919. arch_integer_type (struct gdbarch *gdbarch,
  4920. int bit, int unsigned_p, const char *name)
  4921. {
  4922. struct type *t;
  4923. t = arch_type (gdbarch, TYPE_CODE_INT, bit, name);
  4924. if (unsigned_p)
  4925. t->set_is_unsigned (true);
  4926. return t;
  4927. }
  4928. /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH.
  4929. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  4930. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  4931. struct type *
  4932. arch_character_type (struct gdbarch *gdbarch,
  4933. int bit, int unsigned_p, const char *name)
  4934. {
  4935. struct type *t;
  4936. t = arch_type (gdbarch, TYPE_CODE_CHAR, bit, name);
  4937. if (unsigned_p)
  4938. t->set_is_unsigned (true);
  4939. return t;
  4940. }
  4941. /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH.
  4942. BIT is the type size in bits. If UNSIGNED_P is non-zero, set
  4943. the type's TYPE_UNSIGNED flag. NAME is the type name. */
  4944. struct type *
  4945. arch_boolean_type (struct gdbarch *gdbarch,
  4946. int bit, int unsigned_p, const char *name)
  4947. {
  4948. struct type *t;
  4949. t = arch_type (gdbarch, TYPE_CODE_BOOL, bit, name);
  4950. if (unsigned_p)
  4951. t->set_is_unsigned (true);
  4952. return t;
  4953. }
  4954. /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH.
  4955. BIT is the type size in bits; if BIT equals -1, the size is
  4956. determined by the floatformat. NAME is the type name. Set the
  4957. TYPE_FLOATFORMAT from FLOATFORMATS. */
  4958. struct type *
  4959. arch_float_type (struct gdbarch *gdbarch,
  4960. int bit, const char *name,
  4961. const struct floatformat **floatformats)
  4962. {
  4963. const struct floatformat *fmt = floatformats[gdbarch_byte_order (gdbarch)];
  4964. struct type *t;
  4965. bit = verify_floatformat (bit, fmt);
  4966. t = arch_type (gdbarch, TYPE_CODE_FLT, bit, name);
  4967. TYPE_FLOATFORMAT (t) = fmt;
  4968. return t;
  4969. }
  4970. /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH.
  4971. BIT is the type size in bits. NAME is the type name. */
  4972. struct type *
  4973. arch_decfloat_type (struct gdbarch *gdbarch, int bit, const char *name)
  4974. {
  4975. struct type *t;
  4976. t = arch_type (gdbarch, TYPE_CODE_DECFLOAT, bit, name);
  4977. return t;
  4978. }
  4979. /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH.
  4980. BIT is the pointer type size in bits. NAME is the type name.
  4981. TARGET_TYPE is the pointer target type. Always sets the pointer type's
  4982. TYPE_UNSIGNED flag. */
  4983. struct type *
  4984. arch_pointer_type (struct gdbarch *gdbarch,
  4985. int bit, const char *name, struct type *target_type)
  4986. {
  4987. struct type *t;
  4988. t = arch_type (gdbarch, TYPE_CODE_PTR, bit, name);
  4989. TYPE_TARGET_TYPE (t) = target_type;
  4990. t->set_is_unsigned (true);
  4991. return t;
  4992. }
  4993. /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH.
  4994. NAME is the type name. BIT is the size of the flag word in bits. */
  4995. struct type *
  4996. arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
  4997. {
  4998. struct type *type;
  4999. type = arch_type (gdbarch, TYPE_CODE_FLAGS, bit, name);
  5000. type->set_is_unsigned (true);
  5001. type->set_num_fields (0);
  5002. /* Pre-allocate enough space assuming every field is one bit. */
  5003. type->set_fields
  5004. ((struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field)));
  5005. return type;
  5006. }
  5007. /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
  5008. position BITPOS is called NAME. Pass NAME as "" for fields that
  5009. should not be printed. */
  5010. void
  5011. append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
  5012. struct type *field_type, const char *name)
  5013. {
  5014. int type_bitsize = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  5015. int field_nr = type->num_fields ();
  5016. gdb_assert (type->code () == TYPE_CODE_FLAGS);
  5017. gdb_assert (type->num_fields () + 1 <= type_bitsize);
  5018. gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize);
  5019. gdb_assert (nr_bits >= 1 && (start_bitpos + nr_bits) <= type_bitsize);
  5020. gdb_assert (name != NULL);
  5021. type->set_num_fields (type->num_fields () + 1);
  5022. type->field (field_nr).set_name (xstrdup (name));
  5023. type->field (field_nr).set_type (field_type);
  5024. type->field (field_nr).set_loc_bitpos (start_bitpos);
  5025. TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits;
  5026. }
  5027. /* Special version of append_flags_type_field to add a flag field.
  5028. Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at
  5029. position BITPOS is called NAME. */
  5030. void
  5031. append_flags_type_flag (struct type *type, int bitpos, const char *name)
  5032. {
  5033. append_flags_type_field (type, bitpos, 1,
  5034. builtin_type (type->arch ())->builtin_bool,
  5035. name);
  5036. }
  5037. /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as
  5038. specified by CODE) associated with GDBARCH. NAME is the type name. */
  5039. struct type *
  5040. arch_composite_type (struct gdbarch *gdbarch, const char *name,
  5041. enum type_code code)
  5042. {
  5043. struct type *t;
  5044. gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
  5045. t = arch_type (gdbarch, code, 0, NULL);
  5046. t->set_name (name);
  5047. INIT_CPLUS_SPECIFIC (t);
  5048. return t;
  5049. }
  5050. /* Add new field with name NAME and type FIELD to composite type T.
  5051. Do not set the field's position or adjust the type's length;
  5052. the caller should do so. Return the new field. */
  5053. struct field *
  5054. append_composite_type_field_raw (struct type *t, const char *name,
  5055. struct type *field)
  5056. {
  5057. struct field *f;
  5058. t->set_num_fields (t->num_fields () + 1);
  5059. t->set_fields (XRESIZEVEC (struct field, t->fields (),
  5060. t->num_fields ()));
  5061. f = &t->field (t->num_fields () - 1);
  5062. memset (f, 0, sizeof f[0]);
  5063. f[0].set_type (field);
  5064. f[0].set_name (name);
  5065. return f;
  5066. }
  5067. /* Add new field with name NAME and type FIELD to composite type T.
  5068. ALIGNMENT (if non-zero) specifies the minimum field alignment. */
  5069. void
  5070. append_composite_type_field_aligned (struct type *t, const char *name,
  5071. struct type *field, int alignment)
  5072. {
  5073. struct field *f = append_composite_type_field_raw (t, name, field);
  5074. if (t->code () == TYPE_CODE_UNION)
  5075. {
  5076. if (TYPE_LENGTH (t) < TYPE_LENGTH (field))
  5077. TYPE_LENGTH (t) = TYPE_LENGTH (field);
  5078. }
  5079. else if (t->code () == TYPE_CODE_STRUCT)
  5080. {
  5081. TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field);
  5082. if (t->num_fields () > 1)
  5083. {
  5084. f->set_loc_bitpos
  5085. (f[-1].loc_bitpos () + (TYPE_LENGTH (f[-1].type ()) * TARGET_CHAR_BIT));
  5086. if (alignment)
  5087. {
  5088. int left;
  5089. alignment *= TARGET_CHAR_BIT;
  5090. left = f[0].loc_bitpos () % alignment;
  5091. if (left)
  5092. {
  5093. f->set_loc_bitpos (f[0].loc_bitpos () + (alignment - left));
  5094. TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT;
  5095. }
  5096. }
  5097. }
  5098. }
  5099. }
  5100. /* Add new field with name NAME and type FIELD to composite type T. */
  5101. void
  5102. append_composite_type_field (struct type *t, const char *name,
  5103. struct type *field)
  5104. {
  5105. append_composite_type_field_aligned (t, name, field, 0);
  5106. }
  5107. /* We manage the lifetimes of fixed_point_type_info objects by
  5108. attaching them to the objfile. Currently, these objects are
  5109. modified during construction, and GMP does not provide a way to
  5110. hash the contents of an mpq_t; so it's a bit of a pain to hash-cons
  5111. them. If we did do this, they could be moved to the per-BFD and
  5112. shared across objfiles. */
  5113. typedef std::vector<std::unique_ptr<fixed_point_type_info>>
  5114. fixed_point_type_storage;
  5115. /* Key used for managing the storage of fixed-point type info. */
  5116. static const struct objfile_key<fixed_point_type_storage>
  5117. fixed_point_objfile_key;
  5118. /* See gdbtypes.h. */
  5119. void
  5120. allocate_fixed_point_type_info (struct type *type)
  5121. {
  5122. std::unique_ptr<fixed_point_type_info> up (new fixed_point_type_info);
  5123. fixed_point_type_info *info;
  5124. if (type->is_objfile_owned ())
  5125. {
  5126. fixed_point_type_storage *storage
  5127. = fixed_point_objfile_key.get (type->objfile_owner ());
  5128. if (storage == nullptr)
  5129. storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
  5130. info = up.get ();
  5131. storage->push_back (std::move (up));
  5132. }
  5133. else
  5134. {
  5135. /* We just leak the memory, because that's what we do generally
  5136. for non-objfile-attached types. */
  5137. info = up.release ();
  5138. }
  5139. type->set_fixed_point_info (info);
  5140. }
  5141. /* See gdbtypes.h. */
  5142. bool
  5143. is_fixed_point_type (struct type *type)
  5144. {
  5145. while (check_typedef (type)->code () == TYPE_CODE_RANGE)
  5146. type = TYPE_TARGET_TYPE (check_typedef (type));
  5147. type = check_typedef (type);
  5148. return type->code () == TYPE_CODE_FIXED_POINT;
  5149. }
  5150. /* See gdbtypes.h. */
  5151. struct type *
  5152. type::fixed_point_type_base_type ()
  5153. {
  5154. struct type *type = this;
  5155. while (check_typedef (type)->code () == TYPE_CODE_RANGE)
  5156. type = TYPE_TARGET_TYPE (check_typedef (type));
  5157. type = check_typedef (type);
  5158. gdb_assert (type->code () == TYPE_CODE_FIXED_POINT);
  5159. return type;
  5160. }
  5161. /* See gdbtypes.h. */
  5162. const gdb_mpq &
  5163. type::fixed_point_scaling_factor ()
  5164. {
  5165. struct type *type = this->fixed_point_type_base_type ();
  5166. return type->fixed_point_info ().scaling_factor;
  5167. }
  5168. static struct gdbarch_data *gdbtypes_data;
  5169. const struct builtin_type *
  5170. builtin_type (struct gdbarch *gdbarch)
  5171. {
  5172. return (const struct builtin_type *) gdbarch_data (gdbarch, gdbtypes_data);
  5173. }
  5174. static void *
  5175. gdbtypes_post_init (struct gdbarch *gdbarch)
  5176. {
  5177. struct builtin_type *builtin_type
  5178. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type);
  5179. /* Basic types. */
  5180. builtin_type->builtin_void
  5181. = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  5182. builtin_type->builtin_char
  5183. = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  5184. !gdbarch_char_signed (gdbarch), "char");
  5185. builtin_type->builtin_char->set_has_no_signedness (true);
  5186. builtin_type->builtin_signed_char
  5187. = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  5188. 0, "signed char");
  5189. builtin_type->builtin_unsigned_char
  5190. = arch_integer_type (gdbarch, TARGET_CHAR_BIT,
  5191. 1, "unsigned char");
  5192. builtin_type->builtin_short
  5193. = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
  5194. 0, "short");
  5195. builtin_type->builtin_unsigned_short
  5196. = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
  5197. 1, "unsigned short");
  5198. builtin_type->builtin_int
  5199. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  5200. 0, "int");
  5201. builtin_type->builtin_unsigned_int
  5202. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  5203. 1, "unsigned int");
  5204. builtin_type->builtin_long
  5205. = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  5206. 0, "long");
  5207. builtin_type->builtin_unsigned_long
  5208. = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  5209. 1, "unsigned long");
  5210. builtin_type->builtin_long_long
  5211. = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
  5212. 0, "long long");
  5213. builtin_type->builtin_unsigned_long_long
  5214. = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
  5215. 1, "unsigned long long");
  5216. builtin_type->builtin_half
  5217. = arch_float_type (gdbarch, gdbarch_half_bit (gdbarch),
  5218. "half", gdbarch_half_format (gdbarch));
  5219. builtin_type->builtin_float
  5220. = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  5221. "float", gdbarch_float_format (gdbarch));
  5222. builtin_type->builtin_bfloat16
  5223. = arch_float_type (gdbarch, gdbarch_bfloat16_bit (gdbarch),
  5224. "bfloat16", gdbarch_bfloat16_format (gdbarch));
  5225. builtin_type->builtin_double
  5226. = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  5227. "double", gdbarch_double_format (gdbarch));
  5228. builtin_type->builtin_long_double
  5229. = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  5230. "long double", gdbarch_long_double_format (gdbarch));
  5231. builtin_type->builtin_complex
  5232. = init_complex_type ("complex", builtin_type->builtin_float);
  5233. builtin_type->builtin_double_complex
  5234. = init_complex_type ("double complex", builtin_type->builtin_double);
  5235. builtin_type->builtin_string
  5236. = arch_type (gdbarch, TYPE_CODE_STRING, TARGET_CHAR_BIT, "string");
  5237. builtin_type->builtin_bool
  5238. = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "bool");
  5239. /* The following three are about decimal floating point types, which
  5240. are 32-bits, 64-bits and 128-bits respectively. */
  5241. builtin_type->builtin_decfloat
  5242. = arch_decfloat_type (gdbarch, 32, "_Decimal32");
  5243. builtin_type->builtin_decdouble
  5244. = arch_decfloat_type (gdbarch, 64, "_Decimal64");
  5245. builtin_type->builtin_declong
  5246. = arch_decfloat_type (gdbarch, 128, "_Decimal128");
  5247. /* "True" character types. */
  5248. builtin_type->builtin_true_char
  5249. = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character");
  5250. builtin_type->builtin_true_unsigned_char
  5251. = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character");
  5252. /* Fixed-size integer types. */
  5253. builtin_type->builtin_int0
  5254. = arch_integer_type (gdbarch, 0, 0, "int0_t");
  5255. builtin_type->builtin_int8
  5256. = arch_integer_type (gdbarch, 8, 0, "int8_t");
  5257. builtin_type->builtin_uint8
  5258. = arch_integer_type (gdbarch, 8, 1, "uint8_t");
  5259. builtin_type->builtin_int16
  5260. = arch_integer_type (gdbarch, 16, 0, "int16_t");
  5261. builtin_type->builtin_uint16
  5262. = arch_integer_type (gdbarch, 16, 1, "uint16_t");
  5263. builtin_type->builtin_int24
  5264. = arch_integer_type (gdbarch, 24, 0, "int24_t");
  5265. builtin_type->builtin_uint24
  5266. = arch_integer_type (gdbarch, 24, 1, "uint24_t");
  5267. builtin_type->builtin_int32
  5268. = arch_integer_type (gdbarch, 32, 0, "int32_t");
  5269. builtin_type->builtin_uint32
  5270. = arch_integer_type (gdbarch, 32, 1, "uint32_t");
  5271. builtin_type->builtin_int64
  5272. = arch_integer_type (gdbarch, 64, 0, "int64_t");
  5273. builtin_type->builtin_uint64
  5274. = arch_integer_type (gdbarch, 64, 1, "uint64_t");
  5275. builtin_type->builtin_int128
  5276. = arch_integer_type (gdbarch, 128, 0, "int128_t");
  5277. builtin_type->builtin_uint128
  5278. = arch_integer_type (gdbarch, 128, 1, "uint128_t");
  5279. builtin_type->builtin_int8->set_instance_flags
  5280. (builtin_type->builtin_int8->instance_flags ()
  5281. | TYPE_INSTANCE_FLAG_NOTTEXT);
  5282. builtin_type->builtin_uint8->set_instance_flags
  5283. (builtin_type->builtin_uint8->instance_flags ()
  5284. | TYPE_INSTANCE_FLAG_NOTTEXT);
  5285. /* Wide character types. */
  5286. builtin_type->builtin_char16
  5287. = arch_integer_type (gdbarch, 16, 1, "char16_t");
  5288. builtin_type->builtin_char32
  5289. = arch_integer_type (gdbarch, 32, 1, "char32_t");
  5290. builtin_type->builtin_wchar
  5291. = arch_integer_type (gdbarch, gdbarch_wchar_bit (gdbarch),
  5292. !gdbarch_wchar_signed (gdbarch), "wchar_t");
  5293. /* Default data/code pointer types. */
  5294. builtin_type->builtin_data_ptr
  5295. = lookup_pointer_type (builtin_type->builtin_void);
  5296. builtin_type->builtin_func_ptr
  5297. = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void));
  5298. builtin_type->builtin_func_func
  5299. = lookup_function_type (builtin_type->builtin_func_ptr);
  5300. /* This type represents a GDB internal function. */
  5301. builtin_type->internal_fn
  5302. = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0,
  5303. "<internal function>");
  5304. /* This type represents an xmethod. */
  5305. builtin_type->xmethod
  5306. = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>");
  5307. return builtin_type;
  5308. }
  5309. /* This set of objfile-based types is intended to be used by symbol
  5310. readers as basic types. */
  5311. static const struct objfile_key<struct objfile_type,
  5312. gdb::noop_deleter<struct objfile_type>>
  5313. objfile_type_data;
  5314. const struct objfile_type *
  5315. objfile_type (struct objfile *objfile)
  5316. {
  5317. struct gdbarch *gdbarch;
  5318. struct objfile_type *objfile_type = objfile_type_data.get (objfile);
  5319. if (objfile_type)
  5320. return objfile_type;
  5321. objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack,
  5322. 1, struct objfile_type);
  5323. /* Use the objfile architecture to determine basic type properties. */
  5324. gdbarch = objfile->arch ();
  5325. /* Basic types. */
  5326. objfile_type->builtin_void
  5327. = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  5328. objfile_type->builtin_char
  5329. = init_integer_type (objfile, TARGET_CHAR_BIT,
  5330. !gdbarch_char_signed (gdbarch), "char");
  5331. objfile_type->builtin_char->set_has_no_signedness (true);
  5332. objfile_type->builtin_signed_char
  5333. = init_integer_type (objfile, TARGET_CHAR_BIT,
  5334. 0, "signed char");
  5335. objfile_type->builtin_unsigned_char
  5336. = init_integer_type (objfile, TARGET_CHAR_BIT,
  5337. 1, "unsigned char");
  5338. objfile_type->builtin_short
  5339. = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
  5340. 0, "short");
  5341. objfile_type->builtin_unsigned_short
  5342. = init_integer_type (objfile, gdbarch_short_bit (gdbarch),
  5343. 1, "unsigned short");
  5344. objfile_type->builtin_int
  5345. = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
  5346. 0, "int");
  5347. objfile_type->builtin_unsigned_int
  5348. = init_integer_type (objfile, gdbarch_int_bit (gdbarch),
  5349. 1, "unsigned int");
  5350. objfile_type->builtin_long
  5351. = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
  5352. 0, "long");
  5353. objfile_type->builtin_unsigned_long
  5354. = init_integer_type (objfile, gdbarch_long_bit (gdbarch),
  5355. 1, "unsigned long");
  5356. objfile_type->builtin_long_long
  5357. = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
  5358. 0, "long long");
  5359. objfile_type->builtin_unsigned_long_long
  5360. = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch),
  5361. 1, "unsigned long long");
  5362. objfile_type->builtin_float
  5363. = init_float_type (objfile, gdbarch_float_bit (gdbarch),
  5364. "float", gdbarch_float_format (gdbarch));
  5365. objfile_type->builtin_double
  5366. = init_float_type (objfile, gdbarch_double_bit (gdbarch),
  5367. "double", gdbarch_double_format (gdbarch));
  5368. objfile_type->builtin_long_double
  5369. = init_float_type (objfile, gdbarch_long_double_bit (gdbarch),
  5370. "long double", gdbarch_long_double_format (gdbarch));
  5371. /* This type represents a type that was unrecognized in symbol read-in. */
  5372. objfile_type->builtin_error
  5373. = init_type (objfile, TYPE_CODE_ERROR, 0, "<unknown type>");
  5374. /* The following set of types is used for symbols with no
  5375. debug information. */
  5376. objfile_type->nodebug_text_symbol
  5377. = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
  5378. "<text variable, no debug info>");
  5379. objfile_type->nodebug_text_gnu_ifunc_symbol
  5380. = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT,
  5381. "<text gnu-indirect-function variable, no debug info>");
  5382. objfile_type->nodebug_text_gnu_ifunc_symbol->set_is_gnu_ifunc (true);
  5383. objfile_type->nodebug_got_plt_symbol
  5384. = init_pointer_type (objfile, gdbarch_addr_bit (gdbarch),
  5385. "<text from jump slot in .got.plt, no debug info>",
  5386. objfile_type->nodebug_text_symbol);
  5387. objfile_type->nodebug_data_symbol
  5388. = init_nodebug_var_type (objfile, "<data variable, no debug info>");
  5389. objfile_type->nodebug_unknown_symbol
  5390. = init_nodebug_var_type (objfile, "<variable (not text or data), no debug info>");
  5391. objfile_type->nodebug_tls_symbol
  5392. = init_nodebug_var_type (objfile, "<thread local variable, no debug info>");
  5393. /* NOTE: on some targets, addresses and pointers are not necessarily
  5394. the same.
  5395. The upshot is:
  5396. - gdb's `struct type' always describes the target's
  5397. representation.
  5398. - gdb's `struct value' objects should always hold values in
  5399. target form.
  5400. - gdb's CORE_ADDR values are addresses in the unified virtual
  5401. address space that the assembler and linker work with. Thus,
  5402. since target_read_memory takes a CORE_ADDR as an argument, it
  5403. can access any memory on the target, even if the processor has
  5404. separate code and data address spaces.
  5405. In this context, objfile_type->builtin_core_addr is a bit odd:
  5406. it's a target type for a value the target will never see. It's
  5407. only used to hold the values of (typeless) linker symbols, which
  5408. are indeed in the unified virtual address space. */
  5409. objfile_type->builtin_core_addr
  5410. = init_integer_type (objfile, gdbarch_addr_bit (gdbarch), 1,
  5411. "__CORE_ADDR");
  5412. objfile_type_data.set (objfile, objfile_type);
  5413. return objfile_type;
  5414. }
  5415. /* See gdbtypes.h. */
  5416. CORE_ADDR
  5417. call_site::pc () const
  5418. {
  5419. compunit_symtab *cust = this->per_objfile->get_symtab (this->per_cu);
  5420. CORE_ADDR delta
  5421. = this->per_objfile->objfile->section_offsets[cust->block_line_section ()];
  5422. return m_unrelocated_pc + delta;
  5423. }
  5424. void _initialize_gdbtypes ();
  5425. void
  5426. _initialize_gdbtypes ()
  5427. {
  5428. gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init);
  5429. add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug,
  5430. _("Set debugging of C++ overloading."),
  5431. _("Show debugging of C++ overloading."),
  5432. _("When enabled, ranking of the "
  5433. "functions is displayed."),
  5434. NULL,
  5435. show_overload_debug,
  5436. &setdebuglist, &showdebuglist);
  5437. /* Add user knob for controlling resolution of opaque types. */
  5438. add_setshow_boolean_cmd ("opaque-type-resolution", class_support,
  5439. &opaque_type_resolution,
  5440. _("Set resolution of opaque struct/class/union"
  5441. " types (if set before loading symbols)."),
  5442. _("Show resolution of opaque struct/class/union"
  5443. " types (if set before loading symbols)."),
  5444. NULL, NULL,
  5445. show_opaque_type_resolution,
  5446. &setlist, &showlist);
  5447. /* Add an option to permit non-strict type checking. */
  5448. add_setshow_boolean_cmd ("type", class_support,
  5449. &strict_type_checking,
  5450. _("Set strict type checking."),
  5451. _("Show strict type checking."),
  5452. NULL, NULL,
  5453. show_strict_type_checking,
  5454. &setchecklist, &showchecklist);
  5455. }