ada-lang.c 416 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048
  1. /* Ada language support routines for GDB, the GNU debugger.
  2. Copyright (C) 1992-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include <ctype.h>
  16. #include "gdbsupport/gdb_regex.h"
  17. #include "frame.h"
  18. #include "symtab.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcmd.h"
  21. #include "expression.h"
  22. #include "parser-defs.h"
  23. #include "language.h"
  24. #include "varobj.h"
  25. #include "inferior.h"
  26. #include "symfile.h"
  27. #include "objfiles.h"
  28. #include "breakpoint.h"
  29. #include "gdbcore.h"
  30. #include "hashtab.h"
  31. #include "gdbsupport/gdb_obstack.h"
  32. #include "ada-lang.h"
  33. #include "completer.h"
  34. #include "ui-out.h"
  35. #include "block.h"
  36. #include "infcall.h"
  37. #include "annotate.h"
  38. #include "valprint.h"
  39. #include "source.h"
  40. #include "observable.h"
  41. #include "stack.h"
  42. #include "typeprint.h"
  43. #include "namespace.h"
  44. #include "cli/cli-style.h"
  45. #include "cli/cli-decode.h"
  46. #include "value.h"
  47. #include "mi/mi-common.h"
  48. #include "arch-utils.h"
  49. #include "cli/cli-utils.h"
  50. #include "gdbsupport/function-view.h"
  51. #include "gdbsupport/byte-vector.h"
  52. #include <algorithm>
  53. #include "ada-exp.h"
  54. #include "charset.h"
  55. /* Define whether or not the C operator '/' truncates towards zero for
  56. differently signed operands (truncation direction is undefined in C).
  57. Copied from valarith.c. */
  58. #ifndef TRUNCATION_TOWARDS_ZERO
  59. #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
  60. #endif
  61. static struct type *desc_base_type (struct type *);
  62. static struct type *desc_bounds_type (struct type *);
  63. static struct value *desc_bounds (struct value *);
  64. static int fat_pntr_bounds_bitpos (struct type *);
  65. static int fat_pntr_bounds_bitsize (struct type *);
  66. static struct type *desc_data_target_type (struct type *);
  67. static struct value *desc_data (struct value *);
  68. static int fat_pntr_data_bitpos (struct type *);
  69. static int fat_pntr_data_bitsize (struct type *);
  70. static struct value *desc_one_bound (struct value *, int, int);
  71. static int desc_bound_bitpos (struct type *, int, int);
  72. static int desc_bound_bitsize (struct type *, int, int);
  73. static struct type *desc_index_type (struct type *, int);
  74. static int desc_arity (struct type *);
  75. static int ada_args_match (struct symbol *, struct value **, int);
  76. static struct value *make_array_descriptor (struct type *, struct value *);
  77. static void ada_add_block_symbols (std::vector<struct block_symbol> &,
  78. const struct block *,
  79. const lookup_name_info &lookup_name,
  80. domain_enum, struct objfile *);
  81. static void ada_add_all_symbols (std::vector<struct block_symbol> &,
  82. const struct block *,
  83. const lookup_name_info &lookup_name,
  84. domain_enum, int, int *);
  85. static int is_nonfunction (const std::vector<struct block_symbol> &);
  86. static void add_defn_to_vec (std::vector<struct block_symbol> &,
  87. struct symbol *,
  88. const struct block *);
  89. static int possible_user_operator_p (enum exp_opcode, struct value **);
  90. static const char *ada_decoded_op_name (enum exp_opcode);
  91. static int numeric_type_p (struct type *);
  92. static int integer_type_p (struct type *);
  93. static int scalar_type_p (struct type *);
  94. static int discrete_type_p (struct type *);
  95. static struct type *ada_lookup_struct_elt_type (struct type *, const char *,
  96. int, int);
  97. static struct type *ada_find_parallel_type_with_name (struct type *,
  98. const char *);
  99. static int is_dynamic_field (struct type *, int);
  100. static struct type *to_fixed_variant_branch_type (struct type *,
  101. const gdb_byte *,
  102. CORE_ADDR, struct value *);
  103. static struct type *to_fixed_array_type (struct type *, struct value *, int);
  104. static struct type *to_fixed_range_type (struct type *, struct value *);
  105. static struct type *to_static_fixed_type (struct type *);
  106. static struct type *static_unwrap_type (struct type *type);
  107. static struct value *unwrap_value (struct value *);
  108. static struct type *constrained_packed_array_type (struct type *, long *);
  109. static struct type *decode_constrained_packed_array_type (struct type *);
  110. static long decode_packed_array_bitsize (struct type *);
  111. static struct value *decode_constrained_packed_array (struct value *);
  112. static int ada_is_unconstrained_packed_array_type (struct type *);
  113. static struct value *value_subscript_packed (struct value *, int,
  114. struct value **);
  115. static struct value *coerce_unspec_val_to_type (struct value *,
  116. struct type *);
  117. static int lesseq_defined_than (struct symbol *, struct symbol *);
  118. static int equiv_types (struct type *, struct type *);
  119. static int is_name_suffix (const char *);
  120. static int advance_wild_match (const char **, const char *, char);
  121. static bool wild_match (const char *name, const char *patn);
  122. static struct value *ada_coerce_ref (struct value *);
  123. static LONGEST pos_atr (struct value *);
  124. static struct value *val_atr (struct type *, LONGEST);
  125. static struct symbol *standard_lookup (const char *, const struct block *,
  126. domain_enum);
  127. static struct value *ada_search_struct_field (const char *, struct value *, int,
  128. struct type *);
  129. static int find_struct_field (const char *, struct type *, int,
  130. struct type **, int *, int *, int *, int *);
  131. static int ada_resolve_function (std::vector<struct block_symbol> &,
  132. struct value **, int, const char *,
  133. struct type *, bool);
  134. static int ada_is_direct_array_type (struct type *);
  135. static struct value *ada_index_struct_field (int, struct value *, int,
  136. struct type *);
  137. static void add_component_interval (LONGEST, LONGEST, std::vector<LONGEST> &);
  138. static struct type *ada_find_any_type (const char *name);
  139. static symbol_name_matcher_ftype *ada_get_symbol_name_matcher
  140. (const lookup_name_info &lookup_name);
  141. /* The character set used for source files. */
  142. static const char *ada_source_charset;
  143. /* The string "UTF-8". This is here so we can check for the UTF-8
  144. charset using == rather than strcmp. */
  145. static const char ada_utf8[] = "UTF-8";
  146. /* Each entry in the UTF-32 case-folding table is of this form. */
  147. struct utf8_entry
  148. {
  149. /* The start and end, inclusive, of this range of codepoints. */
  150. uint32_t start, end;
  151. /* The delta to apply to get the upper-case form. 0 if this is
  152. already upper-case. */
  153. int upper_delta;
  154. /* The delta to apply to get the lower-case form. 0 if this is
  155. already lower-case. */
  156. int lower_delta;
  157. bool operator< (uint32_t val) const
  158. {
  159. return end < val;
  160. }
  161. };
  162. static const utf8_entry ada_case_fold[] =
  163. {
  164. #include "ada-casefold.h"
  165. };
  166. /* The result of a symbol lookup to be stored in our symbol cache. */
  167. struct cache_entry
  168. {
  169. /* The name used to perform the lookup. */
  170. const char *name;
  171. /* The namespace used during the lookup. */
  172. domain_enum domain;
  173. /* The symbol returned by the lookup, or NULL if no matching symbol
  174. was found. */
  175. struct symbol *sym;
  176. /* The block where the symbol was found, or NULL if no matching
  177. symbol was found. */
  178. const struct block *block;
  179. /* A pointer to the next entry with the same hash. */
  180. struct cache_entry *next;
  181. };
  182. /* The Ada symbol cache, used to store the result of Ada-mode symbol
  183. lookups in the course of executing the user's commands.
  184. The cache is implemented using a simple, fixed-sized hash.
  185. The size is fixed on the grounds that there are not likely to be
  186. all that many symbols looked up during any given session, regardless
  187. of the size of the symbol table. If we decide to go to a resizable
  188. table, let's just use the stuff from libiberty instead. */
  189. #define HASH_SIZE 1009
  190. struct ada_symbol_cache
  191. {
  192. /* An obstack used to store the entries in our cache. */
  193. struct auto_obstack cache_space;
  194. /* The root of the hash table used to implement our symbol cache. */
  195. struct cache_entry *root[HASH_SIZE] {};
  196. };
  197. static const char ada_completer_word_break_characters[] =
  198. #ifdef VMS
  199. " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
  200. #else
  201. " \t\n!@#$%^&*()+=|~`}{[]\";:?/,-";
  202. #endif
  203. /* The name of the symbol to use to get the name of the main subprogram. */
  204. static const char ADA_MAIN_PROGRAM_SYMBOL_NAME[]
  205. = "__gnat_ada_main_program_name";
  206. /* Limit on the number of warnings to raise per expression evaluation. */
  207. static int warning_limit = 2;
  208. /* Number of warning messages issued; reset to 0 by cleanups after
  209. expression evaluation. */
  210. static int warnings_issued = 0;
  211. static const char * const known_runtime_file_name_patterns[] = {
  212. ADA_KNOWN_RUNTIME_FILE_NAME_PATTERNS NULL
  213. };
  214. static const char * const known_auxiliary_function_name_patterns[] = {
  215. ADA_KNOWN_AUXILIARY_FUNCTION_NAME_PATTERNS NULL
  216. };
  217. /* Maintenance-related settings for this module. */
  218. static struct cmd_list_element *maint_set_ada_cmdlist;
  219. static struct cmd_list_element *maint_show_ada_cmdlist;
  220. /* The "maintenance ada set/show ignore-descriptive-type" value. */
  221. static bool ada_ignore_descriptive_types_p = false;
  222. /* Inferior-specific data. */
  223. /* Per-inferior data for this module. */
  224. struct ada_inferior_data
  225. {
  226. /* The ada__tags__type_specific_data type, which is used when decoding
  227. tagged types. With older versions of GNAT, this type was directly
  228. accessible through a component ("tsd") in the object tag. But this
  229. is no longer the case, so we cache it for each inferior. */
  230. struct type *tsd_type = nullptr;
  231. /* The exception_support_info data. This data is used to determine
  232. how to implement support for Ada exception catchpoints in a given
  233. inferior. */
  234. const struct exception_support_info *exception_info = nullptr;
  235. };
  236. /* Our key to this module's inferior data. */
  237. static const struct inferior_key<ada_inferior_data> ada_inferior_data;
  238. /* Return our inferior data for the given inferior (INF).
  239. This function always returns a valid pointer to an allocated
  240. ada_inferior_data structure. If INF's inferior data has not
  241. been previously set, this functions creates a new one with all
  242. fields set to zero, sets INF's inferior to it, and then returns
  243. a pointer to that newly allocated ada_inferior_data. */
  244. static struct ada_inferior_data *
  245. get_ada_inferior_data (struct inferior *inf)
  246. {
  247. struct ada_inferior_data *data;
  248. data = ada_inferior_data.get (inf);
  249. if (data == NULL)
  250. data = ada_inferior_data.emplace (inf);
  251. return data;
  252. }
  253. /* Perform all necessary cleanups regarding our module's inferior data
  254. that is required after the inferior INF just exited. */
  255. static void
  256. ada_inferior_exit (struct inferior *inf)
  257. {
  258. ada_inferior_data.clear (inf);
  259. }
  260. /* program-space-specific data. */
  261. /* This module's per-program-space data. */
  262. struct ada_pspace_data
  263. {
  264. /* The Ada symbol cache. */
  265. std::unique_ptr<ada_symbol_cache> sym_cache;
  266. };
  267. /* Key to our per-program-space data. */
  268. static const struct program_space_key<ada_pspace_data> ada_pspace_data_handle;
  269. /* Return this module's data for the given program space (PSPACE).
  270. If not is found, add a zero'ed one now.
  271. This function always returns a valid object. */
  272. static struct ada_pspace_data *
  273. get_ada_pspace_data (struct program_space *pspace)
  274. {
  275. struct ada_pspace_data *data;
  276. data = ada_pspace_data_handle.get (pspace);
  277. if (data == NULL)
  278. data = ada_pspace_data_handle.emplace (pspace);
  279. return data;
  280. }
  281. /* Utilities */
  282. /* If TYPE is a TYPE_CODE_TYPEDEF type, return the target type after
  283. all typedef layers have been peeled. Otherwise, return TYPE.
  284. Normally, we really expect a typedef type to only have 1 typedef layer.
  285. In other words, we really expect the target type of a typedef type to be
  286. a non-typedef type. This is particularly true for Ada units, because
  287. the language does not have a typedef vs not-typedef distinction.
  288. In that respect, the Ada compiler has been trying to eliminate as many
  289. typedef definitions in the debugging information, since they generally
  290. do not bring any extra information (we still use typedef under certain
  291. circumstances related mostly to the GNAT encoding).
  292. Unfortunately, we have seen situations where the debugging information
  293. generated by the compiler leads to such multiple typedef layers. For
  294. instance, consider the following example with stabs:
  295. .stabs "pck__float_array___XUP:Tt(0,46)=s16P_ARRAY:(0,47)=[...]"[...]
  296. .stabs "pck__float_array___XUP:t(0,36)=(0,46)",128,0,6,0
  297. This is an error in the debugging information which causes type
  298. pck__float_array___XUP to be defined twice, and the second time,
  299. it is defined as a typedef of a typedef.
  300. This is on the fringe of legality as far as debugging information is
  301. concerned, and certainly unexpected. But it is easy to handle these
  302. situations correctly, so we can afford to be lenient in this case. */
  303. static struct type *
  304. ada_typedef_target_type (struct type *type)
  305. {
  306. while (type->code () == TYPE_CODE_TYPEDEF)
  307. type = TYPE_TARGET_TYPE (type);
  308. return type;
  309. }
  310. /* Given DECODED_NAME a string holding a symbol name in its
  311. decoded form (ie using the Ada dotted notation), returns
  312. its unqualified name. */
  313. static const char *
  314. ada_unqualified_name (const char *decoded_name)
  315. {
  316. const char *result;
  317. /* If the decoded name starts with '<', it means that the encoded
  318. name does not follow standard naming conventions, and thus that
  319. it is not your typical Ada symbol name. Trying to unqualify it
  320. is therefore pointless and possibly erroneous. */
  321. if (decoded_name[0] == '<')
  322. return decoded_name;
  323. result = strrchr (decoded_name, '.');
  324. if (result != NULL)
  325. result++; /* Skip the dot... */
  326. else
  327. result = decoded_name;
  328. return result;
  329. }
  330. /* Return a string starting with '<', followed by STR, and '>'. */
  331. static std::string
  332. add_angle_brackets (const char *str)
  333. {
  334. return string_printf ("<%s>", str);
  335. }
  336. /* True (non-zero) iff TARGET matches FIELD_NAME up to any trailing
  337. suffix of FIELD_NAME beginning "___". */
  338. static int
  339. field_name_match (const char *field_name, const char *target)
  340. {
  341. int len = strlen (target);
  342. return
  343. (strncmp (field_name, target, len) == 0
  344. && (field_name[len] == '\0'
  345. || (startswith (field_name + len, "___")
  346. && strcmp (field_name + strlen (field_name) - 6,
  347. "___XVN") != 0)));
  348. }
  349. /* Assuming TYPE is a TYPE_CODE_STRUCT or a TYPE_CODE_TYPDEF to
  350. a TYPE_CODE_STRUCT, find the field whose name matches FIELD_NAME,
  351. and return its index. This function also handles fields whose name
  352. have ___ suffixes because the compiler sometimes alters their name
  353. by adding such a suffix to represent fields with certain constraints.
  354. If the field could not be found, return a negative number if
  355. MAYBE_MISSING is set. Otherwise raise an error. */
  356. int
  357. ada_get_field_index (const struct type *type, const char *field_name,
  358. int maybe_missing)
  359. {
  360. int fieldno;
  361. struct type *struct_type = check_typedef ((struct type *) type);
  362. for (fieldno = 0; fieldno < struct_type->num_fields (); fieldno++)
  363. if (field_name_match (struct_type->field (fieldno).name (), field_name))
  364. return fieldno;
  365. if (!maybe_missing)
  366. error (_("Unable to find field %s in struct %s. Aborting"),
  367. field_name, struct_type->name ());
  368. return -1;
  369. }
  370. /* The length of the prefix of NAME prior to any "___" suffix. */
  371. int
  372. ada_name_prefix_len (const char *name)
  373. {
  374. if (name == NULL)
  375. return 0;
  376. else
  377. {
  378. const char *p = strstr (name, "___");
  379. if (p == NULL)
  380. return strlen (name);
  381. else
  382. return p - name;
  383. }
  384. }
  385. /* Return non-zero if SUFFIX is a suffix of STR.
  386. Return zero if STR is null. */
  387. static int
  388. is_suffix (const char *str, const char *suffix)
  389. {
  390. int len1, len2;
  391. if (str == NULL)
  392. return 0;
  393. len1 = strlen (str);
  394. len2 = strlen (suffix);
  395. return (len1 >= len2 && strcmp (str + len1 - len2, suffix) == 0);
  396. }
  397. /* The contents of value VAL, treated as a value of type TYPE. The
  398. result is an lval in memory if VAL is. */
  399. static struct value *
  400. coerce_unspec_val_to_type (struct value *val, struct type *type)
  401. {
  402. type = ada_check_typedef (type);
  403. if (value_type (val) == type)
  404. return val;
  405. else
  406. {
  407. struct value *result;
  408. if (value_optimized_out (val))
  409. result = allocate_optimized_out_value (type);
  410. else if (value_lazy (val)
  411. /* Be careful not to make a lazy not_lval value. */
  412. || (VALUE_LVAL (val) != not_lval
  413. && TYPE_LENGTH (type) > TYPE_LENGTH (value_type (val))))
  414. result = allocate_value_lazy (type);
  415. else
  416. {
  417. result = allocate_value (type);
  418. value_contents_copy (result, 0, val, 0, TYPE_LENGTH (type));
  419. }
  420. set_value_component_location (result, val);
  421. set_value_bitsize (result, value_bitsize (val));
  422. set_value_bitpos (result, value_bitpos (val));
  423. if (VALUE_LVAL (result) == lval_memory)
  424. set_value_address (result, value_address (val));
  425. return result;
  426. }
  427. }
  428. static const gdb_byte *
  429. cond_offset_host (const gdb_byte *valaddr, long offset)
  430. {
  431. if (valaddr == NULL)
  432. return NULL;
  433. else
  434. return valaddr + offset;
  435. }
  436. static CORE_ADDR
  437. cond_offset_target (CORE_ADDR address, long offset)
  438. {
  439. if (address == 0)
  440. return 0;
  441. else
  442. return address + offset;
  443. }
  444. /* Issue a warning (as for the definition of warning in utils.c, but
  445. with exactly one argument rather than ...), unless the limit on the
  446. number of warnings has passed during the evaluation of the current
  447. expression. */
  448. /* FIXME: cagney/2004-10-10: This function is mimicking the behavior
  449. provided by "complaint". */
  450. static void lim_warning (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
  451. static void
  452. lim_warning (const char *format, ...)
  453. {
  454. va_list args;
  455. va_start (args, format);
  456. warnings_issued += 1;
  457. if (warnings_issued <= warning_limit)
  458. vwarning (format, args);
  459. va_end (args);
  460. }
  461. /* Maximum value of a SIZE-byte signed integer type. */
  462. static LONGEST
  463. max_of_size (int size)
  464. {
  465. LONGEST top_bit = (LONGEST) 1 << (size * 8 - 2);
  466. return top_bit | (top_bit - 1);
  467. }
  468. /* Minimum value of a SIZE-byte signed integer type. */
  469. static LONGEST
  470. min_of_size (int size)
  471. {
  472. return -max_of_size (size) - 1;
  473. }
  474. /* Maximum value of a SIZE-byte unsigned integer type. */
  475. static ULONGEST
  476. umax_of_size (int size)
  477. {
  478. ULONGEST top_bit = (ULONGEST) 1 << (size * 8 - 1);
  479. return top_bit | (top_bit - 1);
  480. }
  481. /* Maximum value of integral type T, as a signed quantity. */
  482. static LONGEST
  483. max_of_type (struct type *t)
  484. {
  485. if (t->is_unsigned ())
  486. return (LONGEST) umax_of_size (TYPE_LENGTH (t));
  487. else
  488. return max_of_size (TYPE_LENGTH (t));
  489. }
  490. /* Minimum value of integral type T, as a signed quantity. */
  491. static LONGEST
  492. min_of_type (struct type *t)
  493. {
  494. if (t->is_unsigned ())
  495. return 0;
  496. else
  497. return min_of_size (TYPE_LENGTH (t));
  498. }
  499. /* The largest value in the domain of TYPE, a discrete type, as an integer. */
  500. LONGEST
  501. ada_discrete_type_high_bound (struct type *type)
  502. {
  503. type = resolve_dynamic_type (type, {}, 0);
  504. switch (type->code ())
  505. {
  506. case TYPE_CODE_RANGE:
  507. {
  508. const dynamic_prop &high = type->bounds ()->high;
  509. if (high.kind () == PROP_CONST)
  510. return high.const_val ();
  511. else
  512. {
  513. gdb_assert (high.kind () == PROP_UNDEFINED);
  514. /* This happens when trying to evaluate a type's dynamic bound
  515. without a live target. There is nothing relevant for us to
  516. return here, so return 0. */
  517. return 0;
  518. }
  519. }
  520. case TYPE_CODE_ENUM:
  521. return type->field (type->num_fields () - 1).loc_enumval ();
  522. case TYPE_CODE_BOOL:
  523. return 1;
  524. case TYPE_CODE_CHAR:
  525. case TYPE_CODE_INT:
  526. return max_of_type (type);
  527. default:
  528. error (_("Unexpected type in ada_discrete_type_high_bound."));
  529. }
  530. }
  531. /* The smallest value in the domain of TYPE, a discrete type, as an integer. */
  532. LONGEST
  533. ada_discrete_type_low_bound (struct type *type)
  534. {
  535. type = resolve_dynamic_type (type, {}, 0);
  536. switch (type->code ())
  537. {
  538. case TYPE_CODE_RANGE:
  539. {
  540. const dynamic_prop &low = type->bounds ()->low;
  541. if (low.kind () == PROP_CONST)
  542. return low.const_val ();
  543. else
  544. {
  545. gdb_assert (low.kind () == PROP_UNDEFINED);
  546. /* This happens when trying to evaluate a type's dynamic bound
  547. without a live target. There is nothing relevant for us to
  548. return here, so return 0. */
  549. return 0;
  550. }
  551. }
  552. case TYPE_CODE_ENUM:
  553. return type->field (0).loc_enumval ();
  554. case TYPE_CODE_BOOL:
  555. return 0;
  556. case TYPE_CODE_CHAR:
  557. case TYPE_CODE_INT:
  558. return min_of_type (type);
  559. default:
  560. error (_("Unexpected type in ada_discrete_type_low_bound."));
  561. }
  562. }
  563. /* The identity on non-range types. For range types, the underlying
  564. non-range scalar type. */
  565. static struct type *
  566. get_base_type (struct type *type)
  567. {
  568. while (type != NULL && type->code () == TYPE_CODE_RANGE)
  569. {
  570. if (type == TYPE_TARGET_TYPE (type) || TYPE_TARGET_TYPE (type) == NULL)
  571. return type;
  572. type = TYPE_TARGET_TYPE (type);
  573. }
  574. return type;
  575. }
  576. /* Return a decoded version of the given VALUE. This means returning
  577. a value whose type is obtained by applying all the GNAT-specific
  578. encodings, making the resulting type a static but standard description
  579. of the initial type. */
  580. struct value *
  581. ada_get_decoded_value (struct value *value)
  582. {
  583. struct type *type = ada_check_typedef (value_type (value));
  584. if (ada_is_array_descriptor_type (type)
  585. || (ada_is_constrained_packed_array_type (type)
  586. && type->code () != TYPE_CODE_PTR))
  587. {
  588. if (type->code () == TYPE_CODE_TYPEDEF) /* array access type. */
  589. value = ada_coerce_to_simple_array_ptr (value);
  590. else
  591. value = ada_coerce_to_simple_array (value);
  592. }
  593. else
  594. value = ada_to_fixed_value (value);
  595. return value;
  596. }
  597. /* Same as ada_get_decoded_value, but with the given TYPE.
  598. Because there is no associated actual value for this type,
  599. the resulting type might be a best-effort approximation in
  600. the case of dynamic types. */
  601. struct type *
  602. ada_get_decoded_type (struct type *type)
  603. {
  604. type = to_static_fixed_type (type);
  605. if (ada_is_constrained_packed_array_type (type))
  606. type = ada_coerce_to_simple_array_type (type);
  607. return type;
  608. }
  609. /* Language Selection */
  610. /* If the main program is in Ada, return language_ada, otherwise return LANG
  611. (the main program is in Ada iif the adainit symbol is found). */
  612. static enum language
  613. ada_update_initial_language (enum language lang)
  614. {
  615. if (lookup_minimal_symbol ("adainit", NULL, NULL).minsym != NULL)
  616. return language_ada;
  617. return lang;
  618. }
  619. /* If the main procedure is written in Ada, then return its name.
  620. The result is good until the next call. Return NULL if the main
  621. procedure doesn't appear to be in Ada. */
  622. char *
  623. ada_main_name (void)
  624. {
  625. struct bound_minimal_symbol msym;
  626. static gdb::unique_xmalloc_ptr<char> main_program_name;
  627. /* For Ada, the name of the main procedure is stored in a specific
  628. string constant, generated by the binder. Look for that symbol,
  629. extract its address, and then read that string. If we didn't find
  630. that string, then most probably the main procedure is not written
  631. in Ada. */
  632. msym = lookup_minimal_symbol (ADA_MAIN_PROGRAM_SYMBOL_NAME, NULL, NULL);
  633. if (msym.minsym != NULL)
  634. {
  635. CORE_ADDR main_program_name_addr = BMSYMBOL_VALUE_ADDRESS (msym);
  636. if (main_program_name_addr == 0)
  637. error (_("Invalid address for Ada main program name."));
  638. main_program_name = target_read_string (main_program_name_addr, 1024);
  639. return main_program_name.get ();
  640. }
  641. /* The main procedure doesn't seem to be in Ada. */
  642. return NULL;
  643. }
  644. /* Symbols */
  645. /* Table of Ada operators and their GNAT-encoded names. Last entry is pair
  646. of NULLs. */
  647. const struct ada_opname_map ada_opname_table[] = {
  648. {"Oadd", "\"+\"", BINOP_ADD},
  649. {"Osubtract", "\"-\"", BINOP_SUB},
  650. {"Omultiply", "\"*\"", BINOP_MUL},
  651. {"Odivide", "\"/\"", BINOP_DIV},
  652. {"Omod", "\"mod\"", BINOP_MOD},
  653. {"Orem", "\"rem\"", BINOP_REM},
  654. {"Oexpon", "\"**\"", BINOP_EXP},
  655. {"Olt", "\"<\"", BINOP_LESS},
  656. {"Ole", "\"<=\"", BINOP_LEQ},
  657. {"Ogt", "\">\"", BINOP_GTR},
  658. {"Oge", "\">=\"", BINOP_GEQ},
  659. {"Oeq", "\"=\"", BINOP_EQUAL},
  660. {"One", "\"/=\"", BINOP_NOTEQUAL},
  661. {"Oand", "\"and\"", BINOP_BITWISE_AND},
  662. {"Oor", "\"or\"", BINOP_BITWISE_IOR},
  663. {"Oxor", "\"xor\"", BINOP_BITWISE_XOR},
  664. {"Oconcat", "\"&\"", BINOP_CONCAT},
  665. {"Oabs", "\"abs\"", UNOP_ABS},
  666. {"Onot", "\"not\"", UNOP_LOGICAL_NOT},
  667. {"Oadd", "\"+\"", UNOP_PLUS},
  668. {"Osubtract", "\"-\"", UNOP_NEG},
  669. {NULL, NULL}
  670. };
  671. /* If STR is a decoded version of a compiler-provided suffix (like the
  672. "[cold]" in "symbol[cold]"), return true. Otherwise, return
  673. false. */
  674. static bool
  675. is_compiler_suffix (const char *str)
  676. {
  677. gdb_assert (*str == '[');
  678. ++str;
  679. while (*str != '\0' && isalpha (*str))
  680. ++str;
  681. /* We accept a missing "]" in order to support completion. */
  682. return *str == '\0' || (str[0] == ']' && str[1] == '\0');
  683. }
  684. /* Append a non-ASCII character to RESULT. */
  685. static void
  686. append_hex_encoded (std::string &result, uint32_t one_char)
  687. {
  688. if (one_char <= 0xff)
  689. {
  690. result.append ("U");
  691. result.append (phex (one_char, 1));
  692. }
  693. else if (one_char <= 0xffff)
  694. {
  695. result.append ("W");
  696. result.append (phex (one_char, 2));
  697. }
  698. else
  699. {
  700. result.append ("WW");
  701. result.append (phex (one_char, 4));
  702. }
  703. }
  704. /* Return a string that is a copy of the data in STORAGE, with
  705. non-ASCII characters replaced by the appropriate hex encoding. A
  706. template is used because, for UTF-8, we actually want to work with
  707. UTF-32 codepoints. */
  708. template<typename T>
  709. std::string
  710. copy_and_hex_encode (struct obstack *storage)
  711. {
  712. const T *chars = (T *) obstack_base (storage);
  713. int num_chars = obstack_object_size (storage) / sizeof (T);
  714. std::string result;
  715. for (int i = 0; i < num_chars; ++i)
  716. {
  717. if (chars[i] <= 0x7f)
  718. {
  719. /* The host character set has to be a superset of ASCII, as
  720. are all the other character sets we can use. */
  721. result.push_back (chars[i]);
  722. }
  723. else
  724. append_hex_encoded (result, chars[i]);
  725. }
  726. return result;
  727. }
  728. /* The "encoded" form of DECODED, according to GNAT conventions. If
  729. THROW_ERRORS, throw an error if invalid operator name is found.
  730. Otherwise, return the empty string in that case. */
  731. static std::string
  732. ada_encode_1 (const char *decoded, bool throw_errors)
  733. {
  734. if (decoded == NULL)
  735. return {};
  736. std::string encoding_buffer;
  737. bool saw_non_ascii = false;
  738. for (const char *p = decoded; *p != '\0'; p += 1)
  739. {
  740. if ((*p & 0x80) != 0)
  741. saw_non_ascii = true;
  742. if (*p == '.')
  743. encoding_buffer.append ("__");
  744. else if (*p == '[' && is_compiler_suffix (p))
  745. {
  746. encoding_buffer = encoding_buffer + "." + (p + 1);
  747. if (encoding_buffer.back () == ']')
  748. encoding_buffer.pop_back ();
  749. break;
  750. }
  751. else if (*p == '"')
  752. {
  753. const struct ada_opname_map *mapping;
  754. for (mapping = ada_opname_table;
  755. mapping->encoded != NULL
  756. && !startswith (p, mapping->decoded); mapping += 1)
  757. ;
  758. if (mapping->encoded == NULL)
  759. {
  760. if (throw_errors)
  761. error (_("invalid Ada operator name: %s"), p);
  762. else
  763. return {};
  764. }
  765. encoding_buffer.append (mapping->encoded);
  766. break;
  767. }
  768. else
  769. encoding_buffer.push_back (*p);
  770. }
  771. /* If a non-ASCII character is seen, we must convert it to the
  772. appropriate hex form. As this is more expensive, we keep track
  773. of whether it is even necessary. */
  774. if (saw_non_ascii)
  775. {
  776. auto_obstack storage;
  777. bool is_utf8 = ada_source_charset == ada_utf8;
  778. try
  779. {
  780. convert_between_encodings
  781. (host_charset (),
  782. is_utf8 ? HOST_UTF32 : ada_source_charset,
  783. (const gdb_byte *) encoding_buffer.c_str (),
  784. encoding_buffer.length (), 1,
  785. &storage, translit_none);
  786. }
  787. catch (const gdb_exception &)
  788. {
  789. static bool warned = false;
  790. /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
  791. might like to know why. */
  792. if (!warned)
  793. {
  794. warned = true;
  795. warning (_("charset conversion failure for '%s'.\n"
  796. "You may have the wrong value for 'set ada source-charset'."),
  797. encoding_buffer.c_str ());
  798. }
  799. /* We don't try to recover from errors. */
  800. return encoding_buffer;
  801. }
  802. if (is_utf8)
  803. return copy_and_hex_encode<uint32_t> (&storage);
  804. return copy_and_hex_encode<gdb_byte> (&storage);
  805. }
  806. return encoding_buffer;
  807. }
  808. /* Find the entry for C in the case-folding table. Return nullptr if
  809. the entry does not cover C. */
  810. static const utf8_entry *
  811. find_case_fold_entry (uint32_t c)
  812. {
  813. auto iter = std::lower_bound (std::begin (ada_case_fold),
  814. std::end (ada_case_fold),
  815. c);
  816. if (iter == std::end (ada_case_fold)
  817. || c < iter->start
  818. || c > iter->end)
  819. return nullptr;
  820. return &*iter;
  821. }
  822. /* Return NAME folded to lower case, or, if surrounded by single
  823. quotes, unfolded, but with the quotes stripped away. If
  824. THROW_ON_ERROR is true, encoding failures will throw an exception
  825. rather than emitting a warning. Result good to next call. */
  826. static const char *
  827. ada_fold_name (gdb::string_view name, bool throw_on_error = false)
  828. {
  829. static std::string fold_storage;
  830. if (!name.empty () && name[0] == '\'')
  831. fold_storage = gdb::to_string (name.substr (1, name.size () - 2));
  832. else
  833. {
  834. /* Why convert to UTF-32 and implement our own case-folding,
  835. rather than convert to wchar_t and use the platform's
  836. functions? I'm glad you asked.
  837. The main problem is that GNAT implements an unusual rule for
  838. case folding. For ASCII letters, letters in single-byte
  839. encodings (such as ISO-8859-*), and Unicode letters that fit
  840. in a single byte (i.e., code point is <= 0xff), the letter is
  841. folded to lower case. Other Unicode letters are folded to
  842. upper case.
  843. This rule means that the code must be able to examine the
  844. value of the character. And, some hosts do not use Unicode
  845. for wchar_t, so examining the value of such characters is
  846. forbidden. */
  847. auto_obstack storage;
  848. try
  849. {
  850. convert_between_encodings
  851. (host_charset (), HOST_UTF32,
  852. (const gdb_byte *) name.data (),
  853. name.length (), 1,
  854. &storage, translit_none);
  855. }
  856. catch (const gdb_exception &)
  857. {
  858. if (throw_on_error)
  859. throw;
  860. static bool warned = false;
  861. /* Converting to UTF-32 shouldn't fail, so if it doesn't, we
  862. might like to know why. */
  863. if (!warned)
  864. {
  865. warned = true;
  866. warning (_("could not convert '%s' from the host encoding (%s) to UTF-32.\n"
  867. "This normally should not happen, please file a bug report."),
  868. gdb::to_string (name).c_str (), host_charset ());
  869. }
  870. /* We don't try to recover from errors; just return the
  871. original string. */
  872. fold_storage = gdb::to_string (name);
  873. return fold_storage.c_str ();
  874. }
  875. bool is_utf8 = ada_source_charset == ada_utf8;
  876. uint32_t *chars = (uint32_t *) obstack_base (&storage);
  877. int num_chars = obstack_object_size (&storage) / sizeof (uint32_t);
  878. for (int i = 0; i < num_chars; ++i)
  879. {
  880. const struct utf8_entry *entry = find_case_fold_entry (chars[i]);
  881. if (entry != nullptr)
  882. {
  883. uint32_t low = chars[i] + entry->lower_delta;
  884. if (!is_utf8 || low <= 0xff)
  885. chars[i] = low;
  886. else
  887. chars[i] = chars[i] + entry->upper_delta;
  888. }
  889. }
  890. /* Now convert back to ordinary characters. */
  891. auto_obstack reconverted;
  892. try
  893. {
  894. convert_between_encodings (HOST_UTF32,
  895. host_charset (),
  896. (const gdb_byte *) chars,
  897. num_chars * sizeof (uint32_t),
  898. sizeof (uint32_t),
  899. &reconverted,
  900. translit_none);
  901. obstack_1grow (&reconverted, '\0');
  902. fold_storage = std::string ((const char *) obstack_base (&reconverted));
  903. }
  904. catch (const gdb_exception &)
  905. {
  906. if (throw_on_error)
  907. throw;
  908. static bool warned = false;
  909. /* Converting back from UTF-32 shouldn't normally fail, but
  910. there are some host encodings without upper/lower
  911. equivalence. */
  912. if (!warned)
  913. {
  914. warned = true;
  915. warning (_("could not convert the lower-cased variant of '%s'\n"
  916. "from UTF-32 to the host encoding (%s)."),
  917. gdb::to_string (name).c_str (), host_charset ());
  918. }
  919. /* We don't try to recover from errors; just return the
  920. original string. */
  921. fold_storage = gdb::to_string (name);
  922. }
  923. }
  924. return fold_storage.c_str ();
  925. }
  926. /* The "encoded" form of DECODED, according to GNAT conventions. */
  927. std::string
  928. ada_encode (const char *decoded)
  929. {
  930. if (decoded[0] != '<')
  931. decoded = ada_fold_name (decoded);
  932. return ada_encode_1 (decoded, true);
  933. }
  934. /* Return nonzero if C is either a digit or a lowercase alphabet character. */
  935. static int
  936. is_lower_alphanum (const char c)
  937. {
  938. return (isdigit (c) || (isalpha (c) && islower (c)));
  939. }
  940. /* ENCODED is the linkage name of a symbol and LEN contains its length.
  941. This function saves in LEN the length of that same symbol name but
  942. without either of these suffixes:
  943. . .{DIGIT}+
  944. . ${DIGIT}+
  945. . ___{DIGIT}+
  946. . __{DIGIT}+.
  947. These are suffixes introduced by the compiler for entities such as
  948. nested subprogram for instance, in order to avoid name clashes.
  949. They do not serve any purpose for the debugger. */
  950. static void
  951. ada_remove_trailing_digits (const char *encoded, int *len)
  952. {
  953. if (*len > 1 && isdigit (encoded[*len - 1]))
  954. {
  955. int i = *len - 2;
  956. while (i > 0 && isdigit (encoded[i]))
  957. i--;
  958. if (i >= 0 && encoded[i] == '.')
  959. *len = i;
  960. else if (i >= 0 && encoded[i] == '$')
  961. *len = i;
  962. else if (i >= 2 && startswith (encoded + i - 2, "___"))
  963. *len = i - 2;
  964. else if (i >= 1 && startswith (encoded + i - 1, "__"))
  965. *len = i - 1;
  966. }
  967. }
  968. /* Remove the suffix introduced by the compiler for protected object
  969. subprograms. */
  970. static void
  971. ada_remove_po_subprogram_suffix (const char *encoded, int *len)
  972. {
  973. /* Remove trailing N. */
  974. /* Protected entry subprograms are broken into two
  975. separate subprograms: The first one is unprotected, and has
  976. a 'N' suffix; the second is the protected version, and has
  977. the 'P' suffix. The second calls the first one after handling
  978. the protection. Since the P subprograms are internally generated,
  979. we leave these names undecoded, giving the user a clue that this
  980. entity is internal. */
  981. if (*len > 1
  982. && encoded[*len - 1] == 'N'
  983. && (isdigit (encoded[*len - 2]) || islower (encoded[*len - 2])))
  984. *len = *len - 1;
  985. }
  986. /* If ENCODED ends with a compiler-provided suffix (like ".cold"),
  987. then update *LEN to remove the suffix and return the offset of the
  988. character just past the ".". Otherwise, return -1. */
  989. static int
  990. remove_compiler_suffix (const char *encoded, int *len)
  991. {
  992. int offset = *len - 1;
  993. while (offset > 0 && isalpha (encoded[offset]))
  994. --offset;
  995. if (offset > 0 && encoded[offset] == '.')
  996. {
  997. *len = offset;
  998. return offset + 1;
  999. }
  1000. return -1;
  1001. }
  1002. /* Convert an ASCII hex string to a number. Reads exactly N
  1003. characters from STR. Returns true on success, false if one of the
  1004. digits was not a hex digit. */
  1005. static bool
  1006. convert_hex (const char *str, int n, uint32_t *out)
  1007. {
  1008. uint32_t result = 0;
  1009. for (int i = 0; i < n; ++i)
  1010. {
  1011. if (!isxdigit (str[i]))
  1012. return false;
  1013. result <<= 4;
  1014. result |= fromhex (str[i]);
  1015. }
  1016. *out = result;
  1017. return true;
  1018. }
  1019. /* Convert a wide character from its ASCII hex representation in STR
  1020. (consisting of exactly N characters) to the host encoding,
  1021. appending the resulting bytes to OUT. If N==2 and the Ada source
  1022. charset is not UTF-8, then hex refers to an encoding in the
  1023. ADA_SOURCE_CHARSET; otherwise, use UTF-32. Return true on success.
  1024. Return false and do not modify OUT on conversion failure. */
  1025. static bool
  1026. convert_from_hex_encoded (std::string &out, const char *str, int n)
  1027. {
  1028. uint32_t value;
  1029. if (!convert_hex (str, n, &value))
  1030. return false;
  1031. try
  1032. {
  1033. auto_obstack bytes;
  1034. /* In the 'U' case, the hex digits encode the character in the
  1035. Ada source charset. However, if the source charset is UTF-8,
  1036. this really means it is a single-byte UTF-32 character. */
  1037. if (n == 2 && ada_source_charset != ada_utf8)
  1038. {
  1039. gdb_byte one_char = (gdb_byte) value;
  1040. convert_between_encodings (ada_source_charset, host_charset (),
  1041. &one_char,
  1042. sizeof (one_char), sizeof (one_char),
  1043. &bytes, translit_none);
  1044. }
  1045. else
  1046. convert_between_encodings (HOST_UTF32, host_charset (),
  1047. (const gdb_byte *) &value,
  1048. sizeof (value), sizeof (value),
  1049. &bytes, translit_none);
  1050. obstack_1grow (&bytes, '\0');
  1051. out.append ((const char *) obstack_base (&bytes));
  1052. }
  1053. catch (const gdb_exception &)
  1054. {
  1055. /* On failure, the caller will just let the encoded form
  1056. through, which seems basically reasonable. */
  1057. return false;
  1058. }
  1059. return true;
  1060. }
  1061. /* See ada-lang.h. */
  1062. std::string
  1063. ada_decode (const char *encoded, bool wrap)
  1064. {
  1065. int i;
  1066. int len0;
  1067. const char *p;
  1068. int at_start_name;
  1069. std::string decoded;
  1070. int suffix = -1;
  1071. /* With function descriptors on PPC64, the value of a symbol named
  1072. ".FN", if it exists, is the entry point of the function "FN". */
  1073. if (encoded[0] == '.')
  1074. encoded += 1;
  1075. /* The name of the Ada main procedure starts with "_ada_".
  1076. This prefix is not part of the decoded name, so skip this part
  1077. if we see this prefix. */
  1078. if (startswith (encoded, "_ada_"))
  1079. encoded += 5;
  1080. /* The "___ghost_" prefix is used for ghost entities. Normally
  1081. these aren't preserved but when they are, it's useful to see
  1082. them. */
  1083. if (startswith (encoded, "___ghost_"))
  1084. encoded += 9;
  1085. /* If the name starts with '_', then it is not a properly encoded
  1086. name, so do not attempt to decode it. Similarly, if the name
  1087. starts with '<', the name should not be decoded. */
  1088. if (encoded[0] == '_' || encoded[0] == '<')
  1089. goto Suppress;
  1090. len0 = strlen (encoded);
  1091. suffix = remove_compiler_suffix (encoded, &len0);
  1092. ada_remove_trailing_digits (encoded, &len0);
  1093. ada_remove_po_subprogram_suffix (encoded, &len0);
  1094. /* Remove the ___X.* suffix if present. Do not forget to verify that
  1095. the suffix is located before the current "end" of ENCODED. We want
  1096. to avoid re-matching parts of ENCODED that have previously been
  1097. marked as discarded (by decrementing LEN0). */
  1098. p = strstr (encoded, "___");
  1099. if (p != NULL && p - encoded < len0 - 3)
  1100. {
  1101. if (p[3] == 'X')
  1102. len0 = p - encoded;
  1103. else
  1104. goto Suppress;
  1105. }
  1106. /* Remove any trailing TKB suffix. It tells us that this symbol
  1107. is for the body of a task, but that information does not actually
  1108. appear in the decoded name. */
  1109. if (len0 > 3 && startswith (encoded + len0 - 3, "TKB"))
  1110. len0 -= 3;
  1111. /* Remove any trailing TB suffix. The TB suffix is slightly different
  1112. from the TKB suffix because it is used for non-anonymous task
  1113. bodies. */
  1114. if (len0 > 2 && startswith (encoded + len0 - 2, "TB"))
  1115. len0 -= 2;
  1116. /* Remove trailing "B" suffixes. */
  1117. /* FIXME: brobecker/2006-04-19: Not sure what this are used for... */
  1118. if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
  1119. len0 -= 1;
  1120. /* Remove trailing __{digit}+ or trailing ${digit}+. */
  1121. if (len0 > 1 && isdigit (encoded[len0 - 1]))
  1122. {
  1123. i = len0 - 2;
  1124. while ((i >= 0 && isdigit (encoded[i]))
  1125. || (i >= 1 && encoded[i] == '_' && isdigit (encoded[i - 1])))
  1126. i -= 1;
  1127. if (i > 1 && encoded[i] == '_' && encoded[i - 1] == '_')
  1128. len0 = i - 1;
  1129. else if (encoded[i] == '$')
  1130. len0 = i;
  1131. }
  1132. /* The first few characters that are not alphabetic are not part
  1133. of any encoding we use, so we can copy them over verbatim. */
  1134. for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
  1135. decoded.push_back (encoded[i]);
  1136. at_start_name = 1;
  1137. while (i < len0)
  1138. {
  1139. /* Is this a symbol function? */
  1140. if (at_start_name && encoded[i] == 'O')
  1141. {
  1142. int k;
  1143. for (k = 0; ada_opname_table[k].encoded != NULL; k += 1)
  1144. {
  1145. int op_len = strlen (ada_opname_table[k].encoded);
  1146. if ((strncmp (ada_opname_table[k].encoded + 1, encoded + i + 1,
  1147. op_len - 1) == 0)
  1148. && !isalnum (encoded[i + op_len]))
  1149. {
  1150. decoded.append (ada_opname_table[k].decoded);
  1151. at_start_name = 0;
  1152. i += op_len;
  1153. break;
  1154. }
  1155. }
  1156. if (ada_opname_table[k].encoded != NULL)
  1157. continue;
  1158. }
  1159. at_start_name = 0;
  1160. /* Replace "TK__" with "__", which will eventually be translated
  1161. into "." (just below). */
  1162. if (i < len0 - 4 && startswith (encoded + i, "TK__"))
  1163. i += 2;
  1164. /* Replace "__B_{DIGITS}+__" sequences by "__", which will eventually
  1165. be translated into "." (just below). These are internal names
  1166. generated for anonymous blocks inside which our symbol is nested. */
  1167. if (len0 - i > 5 && encoded [i] == '_' && encoded [i+1] == '_'
  1168. && encoded [i+2] == 'B' && encoded [i+3] == '_'
  1169. && isdigit (encoded [i+4]))
  1170. {
  1171. int k = i + 5;
  1172. while (k < len0 && isdigit (encoded[k]))
  1173. k++; /* Skip any extra digit. */
  1174. /* Double-check that the "__B_{DIGITS}+" sequence we found
  1175. is indeed followed by "__". */
  1176. if (len0 - k > 2 && encoded [k] == '_' && encoded [k+1] == '_')
  1177. i = k;
  1178. }
  1179. /* Remove _E{DIGITS}+[sb] */
  1180. /* Just as for protected object subprograms, there are 2 categories
  1181. of subprograms created by the compiler for each entry. The first
  1182. one implements the actual entry code, and has a suffix following
  1183. the convention above; the second one implements the barrier and
  1184. uses the same convention as above, except that the 'E' is replaced
  1185. by a 'B'.
  1186. Just as above, we do not decode the name of barrier functions
  1187. to give the user a clue that the code he is debugging has been
  1188. internally generated. */
  1189. if (len0 - i > 3 && encoded [i] == '_' && encoded[i+1] == 'E'
  1190. && isdigit (encoded[i+2]))
  1191. {
  1192. int k = i + 3;
  1193. while (k < len0 && isdigit (encoded[k]))
  1194. k++;
  1195. if (k < len0
  1196. && (encoded[k] == 'b' || encoded[k] == 's'))
  1197. {
  1198. k++;
  1199. /* Just as an extra precaution, make sure that if this
  1200. suffix is followed by anything else, it is a '_'.
  1201. Otherwise, we matched this sequence by accident. */
  1202. if (k == len0
  1203. || (k < len0 && encoded[k] == '_'))
  1204. i = k;
  1205. }
  1206. }
  1207. /* Remove trailing "N" in [a-z0-9]+N__. The N is added by
  1208. the GNAT front-end in protected object subprograms. */
  1209. if (i < len0 + 3
  1210. && encoded[i] == 'N' && encoded[i+1] == '_' && encoded[i+2] == '_')
  1211. {
  1212. /* Backtrack a bit up until we reach either the begining of
  1213. the encoded name, or "__". Make sure that we only find
  1214. digits or lowercase characters. */
  1215. const char *ptr = encoded + i - 1;
  1216. while (ptr >= encoded && is_lower_alphanum (ptr[0]))
  1217. ptr--;
  1218. if (ptr < encoded
  1219. || (ptr > encoded && ptr[0] == '_' && ptr[-1] == '_'))
  1220. i++;
  1221. }
  1222. if (i < len0 + 3 && encoded[i] == 'U' && isxdigit (encoded[i + 1]))
  1223. {
  1224. if (convert_from_hex_encoded (decoded, &encoded[i + 1], 2))
  1225. {
  1226. i += 3;
  1227. continue;
  1228. }
  1229. }
  1230. else if (i < len0 + 5 && encoded[i] == 'W' && isxdigit (encoded[i + 1]))
  1231. {
  1232. if (convert_from_hex_encoded (decoded, &encoded[i + 1], 4))
  1233. {
  1234. i += 5;
  1235. continue;
  1236. }
  1237. }
  1238. else if (i < len0 + 10 && encoded[i] == 'W' && encoded[i + 1] == 'W'
  1239. && isxdigit (encoded[i + 2]))
  1240. {
  1241. if (convert_from_hex_encoded (decoded, &encoded[i + 2], 8))
  1242. {
  1243. i += 10;
  1244. continue;
  1245. }
  1246. }
  1247. if (encoded[i] == 'X' && i != 0 && isalnum (encoded[i - 1]))
  1248. {
  1249. /* This is a X[bn]* sequence not separated from the previous
  1250. part of the name with a non-alpha-numeric character (in other
  1251. words, immediately following an alpha-numeric character), then
  1252. verify that it is placed at the end of the encoded name. If
  1253. not, then the encoding is not valid and we should abort the
  1254. decoding. Otherwise, just skip it, it is used in body-nested
  1255. package names. */
  1256. do
  1257. i += 1;
  1258. while (i < len0 && (encoded[i] == 'b' || encoded[i] == 'n'));
  1259. if (i < len0)
  1260. goto Suppress;
  1261. }
  1262. else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
  1263. {
  1264. /* Replace '__' by '.'. */
  1265. decoded.push_back ('.');
  1266. at_start_name = 1;
  1267. i += 2;
  1268. }
  1269. else
  1270. {
  1271. /* It's a character part of the decoded name, so just copy it
  1272. over. */
  1273. decoded.push_back (encoded[i]);
  1274. i += 1;
  1275. }
  1276. }
  1277. /* Decoded names should never contain any uppercase character.
  1278. Double-check this, and abort the decoding if we find one. */
  1279. for (i = 0; i < decoded.length(); ++i)
  1280. if (isupper (decoded[i]) || decoded[i] == ' ')
  1281. goto Suppress;
  1282. /* If the compiler added a suffix, append it now. */
  1283. if (suffix >= 0)
  1284. decoded = decoded + "[" + &encoded[suffix] + "]";
  1285. return decoded;
  1286. Suppress:
  1287. if (!wrap)
  1288. return {};
  1289. if (encoded[0] == '<')
  1290. decoded = encoded;
  1291. else
  1292. decoded = '<' + std::string(encoded) + '>';
  1293. return decoded;
  1294. }
  1295. /* Table for keeping permanent unique copies of decoded names. Once
  1296. allocated, names in this table are never released. While this is a
  1297. storage leak, it should not be significant unless there are massive
  1298. changes in the set of decoded names in successive versions of a
  1299. symbol table loaded during a single session. */
  1300. static struct htab *decoded_names_store;
  1301. /* Returns the decoded name of GSYMBOL, as for ada_decode, caching it
  1302. in the language-specific part of GSYMBOL, if it has not been
  1303. previously computed. Tries to save the decoded name in the same
  1304. obstack as GSYMBOL, if possible, and otherwise on the heap (so that,
  1305. in any case, the decoded symbol has a lifetime at least that of
  1306. GSYMBOL).
  1307. The GSYMBOL parameter is "mutable" in the C++ sense: logically
  1308. const, but nevertheless modified to a semantically equivalent form
  1309. when a decoded name is cached in it. */
  1310. const char *
  1311. ada_decode_symbol (const struct general_symbol_info *arg)
  1312. {
  1313. struct general_symbol_info *gsymbol = (struct general_symbol_info *) arg;
  1314. const char **resultp =
  1315. &gsymbol->language_specific.demangled_name;
  1316. if (!gsymbol->ada_mangled)
  1317. {
  1318. std::string decoded = ada_decode (gsymbol->linkage_name ());
  1319. struct obstack *obstack = gsymbol->language_specific.obstack;
  1320. gsymbol->ada_mangled = 1;
  1321. if (obstack != NULL)
  1322. *resultp = obstack_strdup (obstack, decoded.c_str ());
  1323. else
  1324. {
  1325. /* Sometimes, we can't find a corresponding objfile, in
  1326. which case, we put the result on the heap. Since we only
  1327. decode when needed, we hope this usually does not cause a
  1328. significant memory leak (FIXME). */
  1329. char **slot = (char **) htab_find_slot (decoded_names_store,
  1330. decoded.c_str (), INSERT);
  1331. if (*slot == NULL)
  1332. *slot = xstrdup (decoded.c_str ());
  1333. *resultp = *slot;
  1334. }
  1335. }
  1336. return *resultp;
  1337. }
  1338. /* Arrays */
  1339. /* Assuming that INDEX_DESC_TYPE is an ___XA structure, a structure
  1340. generated by the GNAT compiler to describe the index type used
  1341. for each dimension of an array, check whether it follows the latest
  1342. known encoding. If not, fix it up to conform to the latest encoding.
  1343. Otherwise, do nothing. This function also does nothing if
  1344. INDEX_DESC_TYPE is NULL.
  1345. The GNAT encoding used to describe the array index type evolved a bit.
  1346. Initially, the information would be provided through the name of each
  1347. field of the structure type only, while the type of these fields was
  1348. described as unspecified and irrelevant. The debugger was then expected
  1349. to perform a global type lookup using the name of that field in order
  1350. to get access to the full index type description. Because these global
  1351. lookups can be very expensive, the encoding was later enhanced to make
  1352. the global lookup unnecessary by defining the field type as being
  1353. the full index type description.
  1354. The purpose of this routine is to allow us to support older versions
  1355. of the compiler by detecting the use of the older encoding, and by
  1356. fixing up the INDEX_DESC_TYPE to follow the new one (at this point,
  1357. we essentially replace each field's meaningless type by the associated
  1358. index subtype). */
  1359. void
  1360. ada_fixup_array_indexes_type (struct type *index_desc_type)
  1361. {
  1362. int i;
  1363. if (index_desc_type == NULL)
  1364. return;
  1365. gdb_assert (index_desc_type->num_fields () > 0);
  1366. /* Check if INDEX_DESC_TYPE follows the older encoding (it is sufficient
  1367. to check one field only, no need to check them all). If not, return
  1368. now.
  1369. If our INDEX_DESC_TYPE was generated using the older encoding,
  1370. the field type should be a meaningless integer type whose name
  1371. is not equal to the field name. */
  1372. if (index_desc_type->field (0).type ()->name () != NULL
  1373. && strcmp (index_desc_type->field (0).type ()->name (),
  1374. index_desc_type->field (0).name ()) == 0)
  1375. return;
  1376. /* Fixup each field of INDEX_DESC_TYPE. */
  1377. for (i = 0; i < index_desc_type->num_fields (); i++)
  1378. {
  1379. const char *name = index_desc_type->field (i).name ();
  1380. struct type *raw_type = ada_check_typedef (ada_find_any_type (name));
  1381. if (raw_type)
  1382. index_desc_type->field (i).set_type (raw_type);
  1383. }
  1384. }
  1385. /* The desc_* routines return primitive portions of array descriptors
  1386. (fat pointers). */
  1387. /* The descriptor or array type, if any, indicated by TYPE; removes
  1388. level of indirection, if needed. */
  1389. static struct type *
  1390. desc_base_type (struct type *type)
  1391. {
  1392. if (type == NULL)
  1393. return NULL;
  1394. type = ada_check_typedef (type);
  1395. if (type->code () == TYPE_CODE_TYPEDEF)
  1396. type = ada_typedef_target_type (type);
  1397. if (type != NULL
  1398. && (type->code () == TYPE_CODE_PTR
  1399. || type->code () == TYPE_CODE_REF))
  1400. return ada_check_typedef (TYPE_TARGET_TYPE (type));
  1401. else
  1402. return type;
  1403. }
  1404. /* True iff TYPE indicates a "thin" array pointer type. */
  1405. static int
  1406. is_thin_pntr (struct type *type)
  1407. {
  1408. return
  1409. is_suffix (ada_type_name (desc_base_type (type)), "___XUT")
  1410. || is_suffix (ada_type_name (desc_base_type (type)), "___XUT___XVE");
  1411. }
  1412. /* The descriptor type for thin pointer type TYPE. */
  1413. static struct type *
  1414. thin_descriptor_type (struct type *type)
  1415. {
  1416. struct type *base_type = desc_base_type (type);
  1417. if (base_type == NULL)
  1418. return NULL;
  1419. if (is_suffix (ada_type_name (base_type), "___XVE"))
  1420. return base_type;
  1421. else
  1422. {
  1423. struct type *alt_type = ada_find_parallel_type (base_type, "___XVE");
  1424. if (alt_type == NULL)
  1425. return base_type;
  1426. else
  1427. return alt_type;
  1428. }
  1429. }
  1430. /* A pointer to the array data for thin-pointer value VAL. */
  1431. static struct value *
  1432. thin_data_pntr (struct value *val)
  1433. {
  1434. struct type *type = ada_check_typedef (value_type (val));
  1435. struct type *data_type = desc_data_target_type (thin_descriptor_type (type));
  1436. data_type = lookup_pointer_type (data_type);
  1437. if (type->code () == TYPE_CODE_PTR)
  1438. return value_cast (data_type, value_copy (val));
  1439. else
  1440. return value_from_longest (data_type, value_address (val));
  1441. }
  1442. /* True iff TYPE indicates a "thick" array pointer type. */
  1443. static int
  1444. is_thick_pntr (struct type *type)
  1445. {
  1446. type = desc_base_type (type);
  1447. return (type != NULL && type->code () == TYPE_CODE_STRUCT
  1448. && lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL);
  1449. }
  1450. /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
  1451. pointer to one, the type of its bounds data; otherwise, NULL. */
  1452. static struct type *
  1453. desc_bounds_type (struct type *type)
  1454. {
  1455. struct type *r;
  1456. type = desc_base_type (type);
  1457. if (type == NULL)
  1458. return NULL;
  1459. else if (is_thin_pntr (type))
  1460. {
  1461. type = thin_descriptor_type (type);
  1462. if (type == NULL)
  1463. return NULL;
  1464. r = lookup_struct_elt_type (type, "BOUNDS", 1);
  1465. if (r != NULL)
  1466. return ada_check_typedef (r);
  1467. }
  1468. else if (type->code () == TYPE_CODE_STRUCT)
  1469. {
  1470. r = lookup_struct_elt_type (type, "P_BOUNDS", 1);
  1471. if (r != NULL)
  1472. return ada_check_typedef (TYPE_TARGET_TYPE (ada_check_typedef (r)));
  1473. }
  1474. return NULL;
  1475. }
  1476. /* If ARR is an array descriptor (fat or thin pointer), or pointer to
  1477. one, a pointer to its bounds data. Otherwise NULL. */
  1478. static struct value *
  1479. desc_bounds (struct value *arr)
  1480. {
  1481. struct type *type = ada_check_typedef (value_type (arr));
  1482. if (is_thin_pntr (type))
  1483. {
  1484. struct type *bounds_type =
  1485. desc_bounds_type (thin_descriptor_type (type));
  1486. LONGEST addr;
  1487. if (bounds_type == NULL)
  1488. error (_("Bad GNAT array descriptor"));
  1489. /* NOTE: The following calculation is not really kosher, but
  1490. since desc_type is an XVE-encoded type (and shouldn't be),
  1491. the correct calculation is a real pain. FIXME (and fix GCC). */
  1492. if (type->code () == TYPE_CODE_PTR)
  1493. addr = value_as_long (arr);
  1494. else
  1495. addr = value_address (arr);
  1496. return
  1497. value_from_longest (lookup_pointer_type (bounds_type),
  1498. addr - TYPE_LENGTH (bounds_type));
  1499. }
  1500. else if (is_thick_pntr (type))
  1501. {
  1502. struct value *p_bounds = value_struct_elt (&arr, {}, "P_BOUNDS", NULL,
  1503. _("Bad GNAT array descriptor"));
  1504. struct type *p_bounds_type = value_type (p_bounds);
  1505. if (p_bounds_type
  1506. && p_bounds_type->code () == TYPE_CODE_PTR)
  1507. {
  1508. struct type *target_type = TYPE_TARGET_TYPE (p_bounds_type);
  1509. if (target_type->is_stub ())
  1510. p_bounds = value_cast (lookup_pointer_type
  1511. (ada_check_typedef (target_type)),
  1512. p_bounds);
  1513. }
  1514. else
  1515. error (_("Bad GNAT array descriptor"));
  1516. return p_bounds;
  1517. }
  1518. else
  1519. return NULL;
  1520. }
  1521. /* If TYPE is the type of an array-descriptor (fat pointer), the bit
  1522. position of the field containing the address of the bounds data. */
  1523. static int
  1524. fat_pntr_bounds_bitpos (struct type *type)
  1525. {
  1526. return desc_base_type (type)->field (1).loc_bitpos ();
  1527. }
  1528. /* If TYPE is the type of an array-descriptor (fat pointer), the bit
  1529. size of the field containing the address of the bounds data. */
  1530. static int
  1531. fat_pntr_bounds_bitsize (struct type *type)
  1532. {
  1533. type = desc_base_type (type);
  1534. if (TYPE_FIELD_BITSIZE (type, 1) > 0)
  1535. return TYPE_FIELD_BITSIZE (type, 1);
  1536. else
  1537. return 8 * TYPE_LENGTH (ada_check_typedef (type->field (1).type ()));
  1538. }
  1539. /* If TYPE is the type of an array descriptor (fat or thin pointer) or a
  1540. pointer to one, the type of its array data (a array-with-no-bounds type);
  1541. otherwise, NULL. Use ada_type_of_array to get an array type with bounds
  1542. data. */
  1543. static struct type *
  1544. desc_data_target_type (struct type *type)
  1545. {
  1546. type = desc_base_type (type);
  1547. /* NOTE: The following is bogus; see comment in desc_bounds. */
  1548. if (is_thin_pntr (type))
  1549. return desc_base_type (thin_descriptor_type (type)->field (1).type ());
  1550. else if (is_thick_pntr (type))
  1551. {
  1552. struct type *data_type = lookup_struct_elt_type (type, "P_ARRAY", 1);
  1553. if (data_type
  1554. && ada_check_typedef (data_type)->code () == TYPE_CODE_PTR)
  1555. return ada_check_typedef (TYPE_TARGET_TYPE (data_type));
  1556. }
  1557. return NULL;
  1558. }
  1559. /* If ARR is an array descriptor (fat or thin pointer), a pointer to
  1560. its array data. */
  1561. static struct value *
  1562. desc_data (struct value *arr)
  1563. {
  1564. struct type *type = value_type (arr);
  1565. if (is_thin_pntr (type))
  1566. return thin_data_pntr (arr);
  1567. else if (is_thick_pntr (type))
  1568. return value_struct_elt (&arr, {}, "P_ARRAY", NULL,
  1569. _("Bad GNAT array descriptor"));
  1570. else
  1571. return NULL;
  1572. }
  1573. /* If TYPE is the type of an array-descriptor (fat pointer), the bit
  1574. position of the field containing the address of the data. */
  1575. static int
  1576. fat_pntr_data_bitpos (struct type *type)
  1577. {
  1578. return desc_base_type (type)->field (0).loc_bitpos ();
  1579. }
  1580. /* If TYPE is the type of an array-descriptor (fat pointer), the bit
  1581. size of the field containing the address of the data. */
  1582. static int
  1583. fat_pntr_data_bitsize (struct type *type)
  1584. {
  1585. type = desc_base_type (type);
  1586. if (TYPE_FIELD_BITSIZE (type, 0) > 0)
  1587. return TYPE_FIELD_BITSIZE (type, 0);
  1588. else
  1589. return TARGET_CHAR_BIT * TYPE_LENGTH (type->field (0).type ());
  1590. }
  1591. /* If BOUNDS is an array-bounds structure (or pointer to one), return
  1592. the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
  1593. bound, if WHICH is 1. The first bound is I=1. */
  1594. static struct value *
  1595. desc_one_bound (struct value *bounds, int i, int which)
  1596. {
  1597. char bound_name[20];
  1598. xsnprintf (bound_name, sizeof (bound_name), "%cB%d",
  1599. which ? 'U' : 'L', i - 1);
  1600. return value_struct_elt (&bounds, {}, bound_name, NULL,
  1601. _("Bad GNAT array descriptor bounds"));
  1602. }
  1603. /* If BOUNDS is an array-bounds structure type, return the bit position
  1604. of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
  1605. bound, if WHICH is 1. The first bound is I=1. */
  1606. static int
  1607. desc_bound_bitpos (struct type *type, int i, int which)
  1608. {
  1609. return desc_base_type (type)->field (2 * i + which - 2).loc_bitpos ();
  1610. }
  1611. /* If BOUNDS is an array-bounds structure type, return the bit field size
  1612. of the Ith lower bound stored in it, if WHICH is 0, and the Ith upper
  1613. bound, if WHICH is 1. The first bound is I=1. */
  1614. static int
  1615. desc_bound_bitsize (struct type *type, int i, int which)
  1616. {
  1617. type = desc_base_type (type);
  1618. if (TYPE_FIELD_BITSIZE (type, 2 * i + which - 2) > 0)
  1619. return TYPE_FIELD_BITSIZE (type, 2 * i + which - 2);
  1620. else
  1621. return 8 * TYPE_LENGTH (type->field (2 * i + which - 2).type ());
  1622. }
  1623. /* If TYPE is the type of an array-bounds structure, the type of its
  1624. Ith bound (numbering from 1). Otherwise, NULL. */
  1625. static struct type *
  1626. desc_index_type (struct type *type, int i)
  1627. {
  1628. type = desc_base_type (type);
  1629. if (type->code () == TYPE_CODE_STRUCT)
  1630. {
  1631. char bound_name[20];
  1632. xsnprintf (bound_name, sizeof (bound_name), "LB%d", i - 1);
  1633. return lookup_struct_elt_type (type, bound_name, 1);
  1634. }
  1635. else
  1636. return NULL;
  1637. }
  1638. /* The number of index positions in the array-bounds type TYPE.
  1639. Return 0 if TYPE is NULL. */
  1640. static int
  1641. desc_arity (struct type *type)
  1642. {
  1643. type = desc_base_type (type);
  1644. if (type != NULL)
  1645. return type->num_fields () / 2;
  1646. return 0;
  1647. }
  1648. /* Non-zero iff TYPE is a simple array type (not a pointer to one) or
  1649. an array descriptor type (representing an unconstrained array
  1650. type). */
  1651. static int
  1652. ada_is_direct_array_type (struct type *type)
  1653. {
  1654. if (type == NULL)
  1655. return 0;
  1656. type = ada_check_typedef (type);
  1657. return (type->code () == TYPE_CODE_ARRAY
  1658. || ada_is_array_descriptor_type (type));
  1659. }
  1660. /* Non-zero iff TYPE represents any kind of array in Ada, or a pointer
  1661. * to one. */
  1662. static int
  1663. ada_is_array_type (struct type *type)
  1664. {
  1665. while (type != NULL
  1666. && (type->code () == TYPE_CODE_PTR
  1667. || type->code () == TYPE_CODE_REF))
  1668. type = TYPE_TARGET_TYPE (type);
  1669. return ada_is_direct_array_type (type);
  1670. }
  1671. /* Non-zero iff TYPE is a simple array type or pointer to one. */
  1672. int
  1673. ada_is_simple_array_type (struct type *type)
  1674. {
  1675. if (type == NULL)
  1676. return 0;
  1677. type = ada_check_typedef (type);
  1678. return (type->code () == TYPE_CODE_ARRAY
  1679. || (type->code () == TYPE_CODE_PTR
  1680. && (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ()
  1681. == TYPE_CODE_ARRAY)));
  1682. }
  1683. /* Non-zero iff TYPE belongs to a GNAT array descriptor. */
  1684. int
  1685. ada_is_array_descriptor_type (struct type *type)
  1686. {
  1687. struct type *data_type = desc_data_target_type (type);
  1688. if (type == NULL)
  1689. return 0;
  1690. type = ada_check_typedef (type);
  1691. return (data_type != NULL
  1692. && data_type->code () == TYPE_CODE_ARRAY
  1693. && desc_arity (desc_bounds_type (type)) > 0);
  1694. }
  1695. /* Non-zero iff type is a partially mal-formed GNAT array
  1696. descriptor. FIXME: This is to compensate for some problems with
  1697. debugging output from GNAT. Re-examine periodically to see if it
  1698. is still needed. */
  1699. int
  1700. ada_is_bogus_array_descriptor (struct type *type)
  1701. {
  1702. return
  1703. type != NULL
  1704. && type->code () == TYPE_CODE_STRUCT
  1705. && (lookup_struct_elt_type (type, "P_BOUNDS", 1) != NULL
  1706. || lookup_struct_elt_type (type, "P_ARRAY", 1) != NULL)
  1707. && !ada_is_array_descriptor_type (type);
  1708. }
  1709. /* If ARR has a record type in the form of a standard GNAT array descriptor,
  1710. (fat pointer) returns the type of the array data described---specifically,
  1711. a pointer-to-array type. If BOUNDS is non-zero, the bounds data are filled
  1712. in from the descriptor; otherwise, they are left unspecified. If
  1713. the ARR denotes a null array descriptor and BOUNDS is non-zero,
  1714. returns NULL. The result is simply the type of ARR if ARR is not
  1715. a descriptor. */
  1716. static struct type *
  1717. ada_type_of_array (struct value *arr, int bounds)
  1718. {
  1719. if (ada_is_constrained_packed_array_type (value_type (arr)))
  1720. return decode_constrained_packed_array_type (value_type (arr));
  1721. if (!ada_is_array_descriptor_type (value_type (arr)))
  1722. return value_type (arr);
  1723. if (!bounds)
  1724. {
  1725. struct type *array_type =
  1726. ada_check_typedef (desc_data_target_type (value_type (arr)));
  1727. if (ada_is_unconstrained_packed_array_type (value_type (arr)))
  1728. TYPE_FIELD_BITSIZE (array_type, 0) =
  1729. decode_packed_array_bitsize (value_type (arr));
  1730. return array_type;
  1731. }
  1732. else
  1733. {
  1734. struct type *elt_type;
  1735. int arity;
  1736. struct value *descriptor;
  1737. elt_type = ada_array_element_type (value_type (arr), -1);
  1738. arity = ada_array_arity (value_type (arr));
  1739. if (elt_type == NULL || arity == 0)
  1740. return ada_check_typedef (value_type (arr));
  1741. descriptor = desc_bounds (arr);
  1742. if (value_as_long (descriptor) == 0)
  1743. return NULL;
  1744. while (arity > 0)
  1745. {
  1746. struct type *range_type = alloc_type_copy (value_type (arr));
  1747. struct type *array_type = alloc_type_copy (value_type (arr));
  1748. struct value *low = desc_one_bound (descriptor, arity, 0);
  1749. struct value *high = desc_one_bound (descriptor, arity, 1);
  1750. arity -= 1;
  1751. create_static_range_type (range_type, value_type (low),
  1752. longest_to_int (value_as_long (low)),
  1753. longest_to_int (value_as_long (high)));
  1754. elt_type = create_array_type (array_type, elt_type, range_type);
  1755. if (ada_is_unconstrained_packed_array_type (value_type (arr)))
  1756. {
  1757. /* We need to store the element packed bitsize, as well as
  1758. recompute the array size, because it was previously
  1759. computed based on the unpacked element size. */
  1760. LONGEST lo = value_as_long (low);
  1761. LONGEST hi = value_as_long (high);
  1762. TYPE_FIELD_BITSIZE (elt_type, 0) =
  1763. decode_packed_array_bitsize (value_type (arr));
  1764. /* If the array has no element, then the size is already
  1765. zero, and does not need to be recomputed. */
  1766. if (lo < hi)
  1767. {
  1768. int array_bitsize =
  1769. (hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
  1770. TYPE_LENGTH (array_type) = (array_bitsize + 7) / 8;
  1771. }
  1772. }
  1773. }
  1774. return lookup_pointer_type (elt_type);
  1775. }
  1776. }
  1777. /* If ARR does not represent an array, returns ARR unchanged.
  1778. Otherwise, returns either a standard GDB array with bounds set
  1779. appropriately or, if ARR is a non-null fat pointer, a pointer to a standard
  1780. GDB array. Returns NULL if ARR is a null fat pointer. */
  1781. struct value *
  1782. ada_coerce_to_simple_array_ptr (struct value *arr)
  1783. {
  1784. if (ada_is_array_descriptor_type (value_type (arr)))
  1785. {
  1786. struct type *arrType = ada_type_of_array (arr, 1);
  1787. if (arrType == NULL)
  1788. return NULL;
  1789. return value_cast (arrType, value_copy (desc_data (arr)));
  1790. }
  1791. else if (ada_is_constrained_packed_array_type (value_type (arr)))
  1792. return decode_constrained_packed_array (arr);
  1793. else
  1794. return arr;
  1795. }
  1796. /* If ARR does not represent an array, returns ARR unchanged.
  1797. Otherwise, returns a standard GDB array describing ARR (which may
  1798. be ARR itself if it already is in the proper form). */
  1799. struct value *
  1800. ada_coerce_to_simple_array (struct value *arr)
  1801. {
  1802. if (ada_is_array_descriptor_type (value_type (arr)))
  1803. {
  1804. struct value *arrVal = ada_coerce_to_simple_array_ptr (arr);
  1805. if (arrVal == NULL)
  1806. error (_("Bounds unavailable for null array pointer."));
  1807. return value_ind (arrVal);
  1808. }
  1809. else if (ada_is_constrained_packed_array_type (value_type (arr)))
  1810. return decode_constrained_packed_array (arr);
  1811. else
  1812. return arr;
  1813. }
  1814. /* If TYPE represents a GNAT array type, return it translated to an
  1815. ordinary GDB array type (possibly with BITSIZE fields indicating
  1816. packing). For other types, is the identity. */
  1817. struct type *
  1818. ada_coerce_to_simple_array_type (struct type *type)
  1819. {
  1820. if (ada_is_constrained_packed_array_type (type))
  1821. return decode_constrained_packed_array_type (type);
  1822. if (ada_is_array_descriptor_type (type))
  1823. return ada_check_typedef (desc_data_target_type (type));
  1824. return type;
  1825. }
  1826. /* Non-zero iff TYPE represents a standard GNAT packed-array type. */
  1827. static int
  1828. ada_is_gnat_encoded_packed_array_type (struct type *type)
  1829. {
  1830. if (type == NULL)
  1831. return 0;
  1832. type = desc_base_type (type);
  1833. type = ada_check_typedef (type);
  1834. return
  1835. ada_type_name (type) != NULL
  1836. && strstr (ada_type_name (type), "___XP") != NULL;
  1837. }
  1838. /* Non-zero iff TYPE represents a standard GNAT constrained
  1839. packed-array type. */
  1840. int
  1841. ada_is_constrained_packed_array_type (struct type *type)
  1842. {
  1843. return ada_is_gnat_encoded_packed_array_type (type)
  1844. && !ada_is_array_descriptor_type (type);
  1845. }
  1846. /* Non-zero iff TYPE represents an array descriptor for a
  1847. unconstrained packed-array type. */
  1848. static int
  1849. ada_is_unconstrained_packed_array_type (struct type *type)
  1850. {
  1851. if (!ada_is_array_descriptor_type (type))
  1852. return 0;
  1853. if (ada_is_gnat_encoded_packed_array_type (type))
  1854. return 1;
  1855. /* If we saw GNAT encodings, then the above code is sufficient.
  1856. However, with minimal encodings, we will just have a thick
  1857. pointer instead. */
  1858. if (is_thick_pntr (type))
  1859. {
  1860. type = desc_base_type (type);
  1861. /* The structure's first field is a pointer to an array, so this
  1862. fetches the array type. */
  1863. type = TYPE_TARGET_TYPE (type->field (0).type ());
  1864. if (type->code () == TYPE_CODE_TYPEDEF)
  1865. type = ada_typedef_target_type (type);
  1866. /* Now we can see if the array elements are packed. */
  1867. return TYPE_FIELD_BITSIZE (type, 0) > 0;
  1868. }
  1869. return 0;
  1870. }
  1871. /* Return true if TYPE is a (Gnat-encoded) constrained packed array
  1872. type, or if it is an ordinary (non-Gnat-encoded) packed array. */
  1873. static bool
  1874. ada_is_any_packed_array_type (struct type *type)
  1875. {
  1876. return (ada_is_constrained_packed_array_type (type)
  1877. || (type->code () == TYPE_CODE_ARRAY
  1878. && TYPE_FIELD_BITSIZE (type, 0) % 8 != 0));
  1879. }
  1880. /* Given that TYPE encodes a packed array type (constrained or unconstrained),
  1881. return the size of its elements in bits. */
  1882. static long
  1883. decode_packed_array_bitsize (struct type *type)
  1884. {
  1885. const char *raw_name;
  1886. const char *tail;
  1887. long bits;
  1888. /* Access to arrays implemented as fat pointers are encoded as a typedef
  1889. of the fat pointer type. We need the name of the fat pointer type
  1890. to do the decoding, so strip the typedef layer. */
  1891. if (type->code () == TYPE_CODE_TYPEDEF)
  1892. type = ada_typedef_target_type (type);
  1893. raw_name = ada_type_name (ada_check_typedef (type));
  1894. if (!raw_name)
  1895. raw_name = ada_type_name (desc_base_type (type));
  1896. if (!raw_name)
  1897. return 0;
  1898. tail = strstr (raw_name, "___XP");
  1899. if (tail == nullptr)
  1900. {
  1901. gdb_assert (is_thick_pntr (type));
  1902. /* The structure's first field is a pointer to an array, so this
  1903. fetches the array type. */
  1904. type = TYPE_TARGET_TYPE (type->field (0).type ());
  1905. /* Now we can see if the array elements are packed. */
  1906. return TYPE_FIELD_BITSIZE (type, 0);
  1907. }
  1908. if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
  1909. {
  1910. lim_warning
  1911. (_("could not understand bit size information on packed array"));
  1912. return 0;
  1913. }
  1914. return bits;
  1915. }
  1916. /* Given that TYPE is a standard GDB array type with all bounds filled
  1917. in, and that the element size of its ultimate scalar constituents
  1918. (that is, either its elements, or, if it is an array of arrays, its
  1919. elements' elements, etc.) is *ELT_BITS, return an identical type,
  1920. but with the bit sizes of its elements (and those of any
  1921. constituent arrays) recorded in the BITSIZE components of its
  1922. TYPE_FIELD_BITSIZE values, and with *ELT_BITS set to its total size
  1923. in bits.
  1924. Note that, for arrays whose index type has an XA encoding where
  1925. a bound references a record discriminant, getting that discriminant,
  1926. and therefore the actual value of that bound, is not possible
  1927. because none of the given parameters gives us access to the record.
  1928. This function assumes that it is OK in the context where it is being
  1929. used to return an array whose bounds are still dynamic and where
  1930. the length is arbitrary. */
  1931. static struct type *
  1932. constrained_packed_array_type (struct type *type, long *elt_bits)
  1933. {
  1934. struct type *new_elt_type;
  1935. struct type *new_type;
  1936. struct type *index_type_desc;
  1937. struct type *index_type;
  1938. LONGEST low_bound, high_bound;
  1939. type = ada_check_typedef (type);
  1940. if (type->code () != TYPE_CODE_ARRAY)
  1941. return type;
  1942. index_type_desc = ada_find_parallel_type (type, "___XA");
  1943. if (index_type_desc)
  1944. index_type = to_fixed_range_type (index_type_desc->field (0).type (),
  1945. NULL);
  1946. else
  1947. index_type = type->index_type ();
  1948. new_type = alloc_type_copy (type);
  1949. new_elt_type =
  1950. constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
  1951. elt_bits);
  1952. create_array_type (new_type, new_elt_type, index_type);
  1953. TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
  1954. new_type->set_name (ada_type_name (type));
  1955. if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
  1956. && is_dynamic_type (check_typedef (index_type)))
  1957. || !get_discrete_bounds (index_type, &low_bound, &high_bound))
  1958. low_bound = high_bound = 0;
  1959. if (high_bound < low_bound)
  1960. *elt_bits = TYPE_LENGTH (new_type) = 0;
  1961. else
  1962. {
  1963. *elt_bits *= (high_bound - low_bound + 1);
  1964. TYPE_LENGTH (new_type) =
  1965. (*elt_bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
  1966. }
  1967. new_type->set_is_fixed_instance (true);
  1968. return new_type;
  1969. }
  1970. /* The array type encoded by TYPE, where
  1971. ada_is_constrained_packed_array_type (TYPE). */
  1972. static struct type *
  1973. decode_constrained_packed_array_type (struct type *type)
  1974. {
  1975. const char *raw_name = ada_type_name (ada_check_typedef (type));
  1976. char *name;
  1977. const char *tail;
  1978. struct type *shadow_type;
  1979. long bits;
  1980. if (!raw_name)
  1981. raw_name = ada_type_name (desc_base_type (type));
  1982. if (!raw_name)
  1983. return NULL;
  1984. name = (char *) alloca (strlen (raw_name) + 1);
  1985. tail = strstr (raw_name, "___XP");
  1986. type = desc_base_type (type);
  1987. memcpy (name, raw_name, tail - raw_name);
  1988. name[tail - raw_name] = '\000';
  1989. shadow_type = ada_find_parallel_type_with_name (type, name);
  1990. if (shadow_type == NULL)
  1991. {
  1992. lim_warning (_("could not find bounds information on packed array"));
  1993. return NULL;
  1994. }
  1995. shadow_type = check_typedef (shadow_type);
  1996. if (shadow_type->code () != TYPE_CODE_ARRAY)
  1997. {
  1998. lim_warning (_("could not understand bounds "
  1999. "information on packed array"));
  2000. return NULL;
  2001. }
  2002. bits = decode_packed_array_bitsize (type);
  2003. return constrained_packed_array_type (shadow_type, &bits);
  2004. }
  2005. /* Helper function for decode_constrained_packed_array. Set the field
  2006. bitsize on a series of packed arrays. Returns the number of
  2007. elements in TYPE. */
  2008. static LONGEST
  2009. recursively_update_array_bitsize (struct type *type)
  2010. {
  2011. gdb_assert (type->code () == TYPE_CODE_ARRAY);
  2012. LONGEST low, high;
  2013. if (!get_discrete_bounds (type->index_type (), &low, &high)
  2014. || low > high)
  2015. return 0;
  2016. LONGEST our_len = high - low + 1;
  2017. struct type *elt_type = TYPE_TARGET_TYPE (type);
  2018. if (elt_type->code () == TYPE_CODE_ARRAY)
  2019. {
  2020. LONGEST elt_len = recursively_update_array_bitsize (elt_type);
  2021. LONGEST elt_bitsize = elt_len * TYPE_FIELD_BITSIZE (elt_type, 0);
  2022. TYPE_FIELD_BITSIZE (type, 0) = elt_bitsize;
  2023. TYPE_LENGTH (type) = ((our_len * elt_bitsize + HOST_CHAR_BIT - 1)
  2024. / HOST_CHAR_BIT);
  2025. }
  2026. return our_len;
  2027. }
  2028. /* Given that ARR is a struct value *indicating a GNAT constrained packed
  2029. array, returns a simple array that denotes that array. Its type is a
  2030. standard GDB array type except that the BITSIZEs of the array
  2031. target types are set to the number of bits in each element, and the
  2032. type length is set appropriately. */
  2033. static struct value *
  2034. decode_constrained_packed_array (struct value *arr)
  2035. {
  2036. struct type *type;
  2037. /* If our value is a pointer, then dereference it. Likewise if
  2038. the value is a reference. Make sure that this operation does not
  2039. cause the target type to be fixed, as this would indirectly cause
  2040. this array to be decoded. The rest of the routine assumes that
  2041. the array hasn't been decoded yet, so we use the basic "coerce_ref"
  2042. and "value_ind" routines to perform the dereferencing, as opposed
  2043. to using "ada_coerce_ref" or "ada_value_ind". */
  2044. arr = coerce_ref (arr);
  2045. if (ada_check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
  2046. arr = value_ind (arr);
  2047. type = decode_constrained_packed_array_type (value_type (arr));
  2048. if (type == NULL)
  2049. {
  2050. error (_("can't unpack array"));
  2051. return NULL;
  2052. }
  2053. /* Decoding the packed array type could not correctly set the field
  2054. bitsizes for any dimension except the innermost, because the
  2055. bounds may be variable and were not passed to that function. So,
  2056. we further resolve the array bounds here and then update the
  2057. sizes. */
  2058. const gdb_byte *valaddr = value_contents_for_printing (arr).data ();
  2059. CORE_ADDR address = value_address (arr);
  2060. gdb::array_view<const gdb_byte> view
  2061. = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
  2062. type = resolve_dynamic_type (type, view, address);
  2063. recursively_update_array_bitsize (type);
  2064. if (type_byte_order (value_type (arr)) == BFD_ENDIAN_BIG
  2065. && ada_is_modular_type (value_type (arr)))
  2066. {
  2067. /* This is a (right-justified) modular type representing a packed
  2068. array with no wrapper. In order to interpret the value through
  2069. the (left-justified) packed array type we just built, we must
  2070. first left-justify it. */
  2071. int bit_size, bit_pos;
  2072. ULONGEST mod;
  2073. mod = ada_modulus (value_type (arr)) - 1;
  2074. bit_size = 0;
  2075. while (mod > 0)
  2076. {
  2077. bit_size += 1;
  2078. mod >>= 1;
  2079. }
  2080. bit_pos = HOST_CHAR_BIT * TYPE_LENGTH (value_type (arr)) - bit_size;
  2081. arr = ada_value_primitive_packed_val (arr, NULL,
  2082. bit_pos / HOST_CHAR_BIT,
  2083. bit_pos % HOST_CHAR_BIT,
  2084. bit_size,
  2085. type);
  2086. }
  2087. return coerce_unspec_val_to_type (arr, type);
  2088. }
  2089. /* The value of the element of packed array ARR at the ARITY indices
  2090. given in IND. ARR must be a simple array. */
  2091. static struct value *
  2092. value_subscript_packed (struct value *arr, int arity, struct value **ind)
  2093. {
  2094. int i;
  2095. int bits, elt_off, bit_off;
  2096. long elt_total_bit_offset;
  2097. struct type *elt_type;
  2098. struct value *v;
  2099. bits = 0;
  2100. elt_total_bit_offset = 0;
  2101. elt_type = ada_check_typedef (value_type (arr));
  2102. for (i = 0; i < arity; i += 1)
  2103. {
  2104. if (elt_type->code () != TYPE_CODE_ARRAY
  2105. || TYPE_FIELD_BITSIZE (elt_type, 0) == 0)
  2106. error
  2107. (_("attempt to do packed indexing of "
  2108. "something other than a packed array"));
  2109. else
  2110. {
  2111. struct type *range_type = elt_type->index_type ();
  2112. LONGEST lowerbound, upperbound;
  2113. LONGEST idx;
  2114. if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
  2115. {
  2116. lim_warning (_("don't know bounds of array"));
  2117. lowerbound = upperbound = 0;
  2118. }
  2119. idx = pos_atr (ind[i]);
  2120. if (idx < lowerbound || idx > upperbound)
  2121. lim_warning (_("packed array index %ld out of bounds"),
  2122. (long) idx);
  2123. bits = TYPE_FIELD_BITSIZE (elt_type, 0);
  2124. elt_total_bit_offset += (idx - lowerbound) * bits;
  2125. elt_type = ada_check_typedef (TYPE_TARGET_TYPE (elt_type));
  2126. }
  2127. }
  2128. elt_off = elt_total_bit_offset / HOST_CHAR_BIT;
  2129. bit_off = elt_total_bit_offset % HOST_CHAR_BIT;
  2130. v = ada_value_primitive_packed_val (arr, NULL, elt_off, bit_off,
  2131. bits, elt_type);
  2132. return v;
  2133. }
  2134. /* Non-zero iff TYPE includes negative integer values. */
  2135. static int
  2136. has_negatives (struct type *type)
  2137. {
  2138. switch (type->code ())
  2139. {
  2140. default:
  2141. return 0;
  2142. case TYPE_CODE_INT:
  2143. return !type->is_unsigned ();
  2144. case TYPE_CODE_RANGE:
  2145. return type->bounds ()->low.const_val () - type->bounds ()->bias < 0;
  2146. }
  2147. }
  2148. /* With SRC being a buffer containing BIT_SIZE bits of data at BIT_OFFSET,
  2149. unpack that data into UNPACKED. UNPACKED_LEN is the size in bytes of
  2150. the unpacked buffer.
  2151. The size of the unpacked buffer (UNPACKED_LEN) is expected to be large
  2152. enough to contain at least BIT_OFFSET bits. If not, an error is raised.
  2153. IS_BIG_ENDIAN is nonzero if the data is stored in big endian mode,
  2154. zero otherwise.
  2155. IS_SIGNED_TYPE is nonzero if the data corresponds to a signed type.
  2156. IS_SCALAR is nonzero if the data corresponds to a signed type. */
  2157. static void
  2158. ada_unpack_from_contents (const gdb_byte *src, int bit_offset, int bit_size,
  2159. gdb_byte *unpacked, int unpacked_len,
  2160. int is_big_endian, int is_signed_type,
  2161. int is_scalar)
  2162. {
  2163. int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
  2164. int src_idx; /* Index into the source area */
  2165. int src_bytes_left; /* Number of source bytes left to process. */
  2166. int srcBitsLeft; /* Number of source bits left to move */
  2167. int unusedLS; /* Number of bits in next significant
  2168. byte of source that are unused */
  2169. int unpacked_idx; /* Index into the unpacked buffer */
  2170. int unpacked_bytes_left; /* Number of bytes left to set in unpacked. */
  2171. unsigned long accum; /* Staging area for bits being transferred */
  2172. int accumSize; /* Number of meaningful bits in accum */
  2173. unsigned char sign;
  2174. /* Transmit bytes from least to most significant; delta is the direction
  2175. the indices move. */
  2176. int delta = is_big_endian ? -1 : 1;
  2177. /* Make sure that unpacked is large enough to receive the BIT_SIZE
  2178. bits from SRC. .*/
  2179. if ((bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT > unpacked_len)
  2180. error (_("Cannot unpack %d bits into buffer of %d bytes"),
  2181. bit_size, unpacked_len);
  2182. srcBitsLeft = bit_size;
  2183. src_bytes_left = src_len;
  2184. unpacked_bytes_left = unpacked_len;
  2185. sign = 0;
  2186. if (is_big_endian)
  2187. {
  2188. src_idx = src_len - 1;
  2189. if (is_signed_type
  2190. && ((src[0] << bit_offset) & (1 << (HOST_CHAR_BIT - 1))))
  2191. sign = ~0;
  2192. unusedLS =
  2193. (HOST_CHAR_BIT - (bit_size + bit_offset) % HOST_CHAR_BIT)
  2194. % HOST_CHAR_BIT;
  2195. if (is_scalar)
  2196. {
  2197. accumSize = 0;
  2198. unpacked_idx = unpacked_len - 1;
  2199. }
  2200. else
  2201. {
  2202. /* Non-scalar values must be aligned at a byte boundary... */
  2203. accumSize =
  2204. (HOST_CHAR_BIT - bit_size % HOST_CHAR_BIT) % HOST_CHAR_BIT;
  2205. /* ... And are placed at the beginning (most-significant) bytes
  2206. of the target. */
  2207. unpacked_idx = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT - 1;
  2208. unpacked_bytes_left = unpacked_idx + 1;
  2209. }
  2210. }
  2211. else
  2212. {
  2213. int sign_bit_offset = (bit_size + bit_offset - 1) % 8;
  2214. src_idx = unpacked_idx = 0;
  2215. unusedLS = bit_offset;
  2216. accumSize = 0;
  2217. if (is_signed_type && (src[src_len - 1] & (1 << sign_bit_offset)))
  2218. sign = ~0;
  2219. }
  2220. accum = 0;
  2221. while (src_bytes_left > 0)
  2222. {
  2223. /* Mask for removing bits of the next source byte that are not
  2224. part of the value. */
  2225. unsigned int unusedMSMask =
  2226. (1 << (srcBitsLeft >= HOST_CHAR_BIT ? HOST_CHAR_BIT : srcBitsLeft)) -
  2227. 1;
  2228. /* Sign-extend bits for this byte. */
  2229. unsigned int signMask = sign & ~unusedMSMask;
  2230. accum |=
  2231. (((src[src_idx] >> unusedLS) & unusedMSMask) | signMask) << accumSize;
  2232. accumSize += HOST_CHAR_BIT - unusedLS;
  2233. if (accumSize >= HOST_CHAR_BIT)
  2234. {
  2235. unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
  2236. accumSize -= HOST_CHAR_BIT;
  2237. accum >>= HOST_CHAR_BIT;
  2238. unpacked_bytes_left -= 1;
  2239. unpacked_idx += delta;
  2240. }
  2241. srcBitsLeft -= HOST_CHAR_BIT - unusedLS;
  2242. unusedLS = 0;
  2243. src_bytes_left -= 1;
  2244. src_idx += delta;
  2245. }
  2246. while (unpacked_bytes_left > 0)
  2247. {
  2248. accum |= sign << accumSize;
  2249. unpacked[unpacked_idx] = accum & ~(~0UL << HOST_CHAR_BIT);
  2250. accumSize -= HOST_CHAR_BIT;
  2251. if (accumSize < 0)
  2252. accumSize = 0;
  2253. accum >>= HOST_CHAR_BIT;
  2254. unpacked_bytes_left -= 1;
  2255. unpacked_idx += delta;
  2256. }
  2257. }
  2258. /* Create a new value of type TYPE from the contents of OBJ starting
  2259. at byte OFFSET, and bit offset BIT_OFFSET within that byte,
  2260. proceeding for BIT_SIZE bits. If OBJ is an lval in memory, then
  2261. assigning through the result will set the field fetched from.
  2262. VALADDR is ignored unless OBJ is NULL, in which case,
  2263. VALADDR+OFFSET must address the start of storage containing the
  2264. packed value. The value returned in this case is never an lval.
  2265. Assumes 0 <= BIT_OFFSET < HOST_CHAR_BIT. */
  2266. struct value *
  2267. ada_value_primitive_packed_val (struct value *obj, const gdb_byte *valaddr,
  2268. long offset, int bit_offset, int bit_size,
  2269. struct type *type)
  2270. {
  2271. struct value *v;
  2272. const gdb_byte *src; /* First byte containing data to unpack */
  2273. gdb_byte *unpacked;
  2274. const int is_scalar = is_scalar_type (type);
  2275. const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
  2276. gdb::byte_vector staging;
  2277. type = ada_check_typedef (type);
  2278. if (obj == NULL)
  2279. src = valaddr + offset;
  2280. else
  2281. src = value_contents (obj).data () + offset;
  2282. if (is_dynamic_type (type))
  2283. {
  2284. /* The length of TYPE might by dynamic, so we need to resolve
  2285. TYPE in order to know its actual size, which we then use
  2286. to create the contents buffer of the value we return.
  2287. The difficulty is that the data containing our object is
  2288. packed, and therefore maybe not at a byte boundary. So, what
  2289. we do, is unpack the data into a byte-aligned buffer, and then
  2290. use that buffer as our object's value for resolving the type. */
  2291. int staging_len = (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
  2292. staging.resize (staging_len);
  2293. ada_unpack_from_contents (src, bit_offset, bit_size,
  2294. staging.data (), staging.size (),
  2295. is_big_endian, has_negatives (type),
  2296. is_scalar);
  2297. type = resolve_dynamic_type (type, staging, 0);
  2298. if (TYPE_LENGTH (type) < (bit_size + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT)
  2299. {
  2300. /* This happens when the length of the object is dynamic,
  2301. and is actually smaller than the space reserved for it.
  2302. For instance, in an array of variant records, the bit_size
  2303. we're given is the array stride, which is constant and
  2304. normally equal to the maximum size of its element.
  2305. But, in reality, each element only actually spans a portion
  2306. of that stride. */
  2307. bit_size = TYPE_LENGTH (type) * HOST_CHAR_BIT;
  2308. }
  2309. }
  2310. if (obj == NULL)
  2311. {
  2312. v = allocate_value (type);
  2313. src = valaddr + offset;
  2314. }
  2315. else if (VALUE_LVAL (obj) == lval_memory && value_lazy (obj))
  2316. {
  2317. int src_len = (bit_size + bit_offset + HOST_CHAR_BIT - 1) / 8;
  2318. gdb_byte *buf;
  2319. v = value_at (type, value_address (obj) + offset);
  2320. buf = (gdb_byte *) alloca (src_len);
  2321. read_memory (value_address (v), buf, src_len);
  2322. src = buf;
  2323. }
  2324. else
  2325. {
  2326. v = allocate_value (type);
  2327. src = value_contents (obj).data () + offset;
  2328. }
  2329. if (obj != NULL)
  2330. {
  2331. long new_offset = offset;
  2332. set_value_component_location (v, obj);
  2333. set_value_bitpos (v, bit_offset + value_bitpos (obj));
  2334. set_value_bitsize (v, bit_size);
  2335. if (value_bitpos (v) >= HOST_CHAR_BIT)
  2336. {
  2337. ++new_offset;
  2338. set_value_bitpos (v, value_bitpos (v) - HOST_CHAR_BIT);
  2339. }
  2340. set_value_offset (v, new_offset);
  2341. /* Also set the parent value. This is needed when trying to
  2342. assign a new value (in inferior memory). */
  2343. set_value_parent (v, obj);
  2344. }
  2345. else
  2346. set_value_bitsize (v, bit_size);
  2347. unpacked = value_contents_writeable (v).data ();
  2348. if (bit_size == 0)
  2349. {
  2350. memset (unpacked, 0, TYPE_LENGTH (type));
  2351. return v;
  2352. }
  2353. if (staging.size () == TYPE_LENGTH (type))
  2354. {
  2355. /* Small short-cut: If we've unpacked the data into a buffer
  2356. of the same size as TYPE's length, then we can reuse that,
  2357. instead of doing the unpacking again. */
  2358. memcpy (unpacked, staging.data (), staging.size ());
  2359. }
  2360. else
  2361. ada_unpack_from_contents (src, bit_offset, bit_size,
  2362. unpacked, TYPE_LENGTH (type),
  2363. is_big_endian, has_negatives (type), is_scalar);
  2364. return v;
  2365. }
  2366. /* Store the contents of FROMVAL into the location of TOVAL.
  2367. Return a new value with the location of TOVAL and contents of
  2368. FROMVAL. Handles assignment into packed fields that have
  2369. floating-point or non-scalar types. */
  2370. static struct value *
  2371. ada_value_assign (struct value *toval, struct value *fromval)
  2372. {
  2373. struct type *type = value_type (toval);
  2374. int bits = value_bitsize (toval);
  2375. toval = ada_coerce_ref (toval);
  2376. fromval = ada_coerce_ref (fromval);
  2377. if (ada_is_direct_array_type (value_type (toval)))
  2378. toval = ada_coerce_to_simple_array (toval);
  2379. if (ada_is_direct_array_type (value_type (fromval)))
  2380. fromval = ada_coerce_to_simple_array (fromval);
  2381. if (!deprecated_value_modifiable (toval))
  2382. error (_("Left operand of assignment is not a modifiable lvalue."));
  2383. if (VALUE_LVAL (toval) == lval_memory
  2384. && bits > 0
  2385. && (type->code () == TYPE_CODE_FLT
  2386. || type->code () == TYPE_CODE_STRUCT))
  2387. {
  2388. int len = (value_bitpos (toval)
  2389. + bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
  2390. int from_size;
  2391. gdb_byte *buffer = (gdb_byte *) alloca (len);
  2392. struct value *val;
  2393. CORE_ADDR to_addr = value_address (toval);
  2394. if (type->code () == TYPE_CODE_FLT)
  2395. fromval = value_cast (type, fromval);
  2396. read_memory (to_addr, buffer, len);
  2397. from_size = value_bitsize (fromval);
  2398. if (from_size == 0)
  2399. from_size = TYPE_LENGTH (value_type (fromval)) * TARGET_CHAR_BIT;
  2400. const int is_big_endian = type_byte_order (type) == BFD_ENDIAN_BIG;
  2401. ULONGEST from_offset = 0;
  2402. if (is_big_endian && is_scalar_type (value_type (fromval)))
  2403. from_offset = from_size - bits;
  2404. copy_bitwise (buffer, value_bitpos (toval),
  2405. value_contents (fromval).data (), from_offset,
  2406. bits, is_big_endian);
  2407. write_memory_with_notification (to_addr, buffer, len);
  2408. val = value_copy (toval);
  2409. memcpy (value_contents_raw (val).data (),
  2410. value_contents (fromval).data (),
  2411. TYPE_LENGTH (type));
  2412. deprecated_set_value_type (val, type);
  2413. return val;
  2414. }
  2415. return value_assign (toval, fromval);
  2416. }
  2417. /* Given that COMPONENT is a memory lvalue that is part of the lvalue
  2418. CONTAINER, assign the contents of VAL to COMPONENTS's place in
  2419. CONTAINER. Modifies the VALUE_CONTENTS of CONTAINER only, not
  2420. COMPONENT, and not the inferior's memory. The current contents
  2421. of COMPONENT are ignored.
  2422. Although not part of the initial design, this function also works
  2423. when CONTAINER and COMPONENT are not_lval's: it works as if CONTAINER
  2424. had a null address, and COMPONENT had an address which is equal to
  2425. its offset inside CONTAINER. */
  2426. static void
  2427. value_assign_to_component (struct value *container, struct value *component,
  2428. struct value *val)
  2429. {
  2430. LONGEST offset_in_container =
  2431. (LONGEST) (value_address (component) - value_address (container));
  2432. int bit_offset_in_container =
  2433. value_bitpos (component) - value_bitpos (container);
  2434. int bits;
  2435. val = value_cast (value_type (component), val);
  2436. if (value_bitsize (component) == 0)
  2437. bits = TARGET_CHAR_BIT * TYPE_LENGTH (value_type (component));
  2438. else
  2439. bits = value_bitsize (component);
  2440. if (type_byte_order (value_type (container)) == BFD_ENDIAN_BIG)
  2441. {
  2442. int src_offset;
  2443. if (is_scalar_type (check_typedef (value_type (component))))
  2444. src_offset
  2445. = TYPE_LENGTH (value_type (component)) * TARGET_CHAR_BIT - bits;
  2446. else
  2447. src_offset = 0;
  2448. copy_bitwise ((value_contents_writeable (container).data ()
  2449. + offset_in_container),
  2450. value_bitpos (container) + bit_offset_in_container,
  2451. value_contents (val).data (), src_offset, bits, 1);
  2452. }
  2453. else
  2454. copy_bitwise ((value_contents_writeable (container).data ()
  2455. + offset_in_container),
  2456. value_bitpos (container) + bit_offset_in_container,
  2457. value_contents (val).data (), 0, bits, 0);
  2458. }
  2459. /* Determine if TYPE is an access to an unconstrained array. */
  2460. bool
  2461. ada_is_access_to_unconstrained_array (struct type *type)
  2462. {
  2463. return (type->code () == TYPE_CODE_TYPEDEF
  2464. && is_thick_pntr (ada_typedef_target_type (type)));
  2465. }
  2466. /* The value of the element of array ARR at the ARITY indices given in IND.
  2467. ARR may be either a simple array, GNAT array descriptor, or pointer
  2468. thereto. */
  2469. struct value *
  2470. ada_value_subscript (struct value *arr, int arity, struct value **ind)
  2471. {
  2472. int k;
  2473. struct value *elt;
  2474. struct type *elt_type;
  2475. elt = ada_coerce_to_simple_array (arr);
  2476. elt_type = ada_check_typedef (value_type (elt));
  2477. if (elt_type->code () == TYPE_CODE_ARRAY
  2478. && TYPE_FIELD_BITSIZE (elt_type, 0) > 0)
  2479. return value_subscript_packed (elt, arity, ind);
  2480. for (k = 0; k < arity; k += 1)
  2481. {
  2482. struct type *saved_elt_type = TYPE_TARGET_TYPE (elt_type);
  2483. if (elt_type->code () != TYPE_CODE_ARRAY)
  2484. error (_("too many subscripts (%d expected)"), k);
  2485. elt = value_subscript (elt, pos_atr (ind[k]));
  2486. if (ada_is_access_to_unconstrained_array (saved_elt_type)
  2487. && value_type (elt)->code () != TYPE_CODE_TYPEDEF)
  2488. {
  2489. /* The element is a typedef to an unconstrained array,
  2490. except that the value_subscript call stripped the
  2491. typedef layer. The typedef layer is GNAT's way to
  2492. specify that the element is, at the source level, an
  2493. access to the unconstrained array, rather than the
  2494. unconstrained array. So, we need to restore that
  2495. typedef layer, which we can do by forcing the element's
  2496. type back to its original type. Otherwise, the returned
  2497. value is going to be printed as the array, rather
  2498. than as an access. Another symptom of the same issue
  2499. would be that an expression trying to dereference the
  2500. element would also be improperly rejected. */
  2501. deprecated_set_value_type (elt, saved_elt_type);
  2502. }
  2503. elt_type = ada_check_typedef (value_type (elt));
  2504. }
  2505. return elt;
  2506. }
  2507. /* Assuming ARR is a pointer to a GDB array, the value of the element
  2508. of *ARR at the ARITY indices given in IND.
  2509. Does not read the entire array into memory.
  2510. Note: Unlike what one would expect, this function is used instead of
  2511. ada_value_subscript for basically all non-packed array types. The reason
  2512. for this is that a side effect of doing our own pointer arithmetics instead
  2513. of relying on value_subscript is that there is no implicit typedef peeling.
  2514. This is important for arrays of array accesses, where it allows us to
  2515. preserve the fact that the array's element is an array access, where the
  2516. access part os encoded in a typedef layer. */
  2517. static struct value *
  2518. ada_value_ptr_subscript (struct value *arr, int arity, struct value **ind)
  2519. {
  2520. int k;
  2521. struct value *array_ind = ada_value_ind (arr);
  2522. struct type *type
  2523. = check_typedef (value_enclosing_type (array_ind));
  2524. if (type->code () == TYPE_CODE_ARRAY
  2525. && TYPE_FIELD_BITSIZE (type, 0) > 0)
  2526. return value_subscript_packed (array_ind, arity, ind);
  2527. for (k = 0; k < arity; k += 1)
  2528. {
  2529. LONGEST lwb, upb;
  2530. if (type->code () != TYPE_CODE_ARRAY)
  2531. error (_("too many subscripts (%d expected)"), k);
  2532. arr = value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
  2533. value_copy (arr));
  2534. get_discrete_bounds (type->index_type (), &lwb, &upb);
  2535. arr = value_ptradd (arr, pos_atr (ind[k]) - lwb);
  2536. type = TYPE_TARGET_TYPE (type);
  2537. }
  2538. return value_ind (arr);
  2539. }
  2540. /* Given that ARRAY_PTR is a pointer or reference to an array of type TYPE (the
  2541. actual type of ARRAY_PTR is ignored), returns the Ada slice of
  2542. HIGH'Pos-LOW'Pos+1 elements starting at index LOW. The lower bound of
  2543. this array is LOW, as per Ada rules. */
  2544. static struct value *
  2545. ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
  2546. int low, int high)
  2547. {
  2548. struct type *type0 = ada_check_typedef (type);
  2549. struct type *base_index_type = TYPE_TARGET_TYPE (type0->index_type ());
  2550. struct type *index_type
  2551. = create_static_range_type (NULL, base_index_type, low, high);
  2552. struct type *slice_type = create_array_type_with_stride
  2553. (NULL, TYPE_TARGET_TYPE (type0), index_type,
  2554. type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
  2555. TYPE_FIELD_BITSIZE (type0, 0));
  2556. int base_low = ada_discrete_type_low_bound (type0->index_type ());
  2557. gdb::optional<LONGEST> base_low_pos, low_pos;
  2558. CORE_ADDR base;
  2559. low_pos = discrete_position (base_index_type, low);
  2560. base_low_pos = discrete_position (base_index_type, base_low);
  2561. if (!low_pos.has_value () || !base_low_pos.has_value ())
  2562. {
  2563. warning (_("unable to get positions in slice, use bounds instead"));
  2564. low_pos = low;
  2565. base_low_pos = base_low;
  2566. }
  2567. ULONGEST stride = TYPE_FIELD_BITSIZE (slice_type, 0) / 8;
  2568. if (stride == 0)
  2569. stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
  2570. base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
  2571. return value_at_lazy (slice_type, base);
  2572. }
  2573. static struct value *
  2574. ada_value_slice (struct value *array, int low, int high)
  2575. {
  2576. struct type *type = ada_check_typedef (value_type (array));
  2577. struct type *base_index_type = TYPE_TARGET_TYPE (type->index_type ());
  2578. struct type *index_type
  2579. = create_static_range_type (NULL, type->index_type (), low, high);
  2580. struct type *slice_type = create_array_type_with_stride
  2581. (NULL, TYPE_TARGET_TYPE (type), index_type,
  2582. type->dyn_prop (DYN_PROP_BYTE_STRIDE),
  2583. TYPE_FIELD_BITSIZE (type, 0));
  2584. gdb::optional<LONGEST> low_pos, high_pos;
  2585. low_pos = discrete_position (base_index_type, low);
  2586. high_pos = discrete_position (base_index_type, high);
  2587. if (!low_pos.has_value () || !high_pos.has_value ())
  2588. {
  2589. warning (_("unable to get positions in slice, use bounds instead"));
  2590. low_pos = low;
  2591. high_pos = high;
  2592. }
  2593. return value_cast (slice_type,
  2594. value_slice (array, low, *high_pos - *low_pos + 1));
  2595. }
  2596. /* If type is a record type in the form of a standard GNAT array
  2597. descriptor, returns the number of dimensions for type. If arr is a
  2598. simple array, returns the number of "array of"s that prefix its
  2599. type designation. Otherwise, returns 0. */
  2600. int
  2601. ada_array_arity (struct type *type)
  2602. {
  2603. int arity;
  2604. if (type == NULL)
  2605. return 0;
  2606. type = desc_base_type (type);
  2607. arity = 0;
  2608. if (type->code () == TYPE_CODE_STRUCT)
  2609. return desc_arity (desc_bounds_type (type));
  2610. else
  2611. while (type->code () == TYPE_CODE_ARRAY)
  2612. {
  2613. arity += 1;
  2614. type = ada_check_typedef (TYPE_TARGET_TYPE (type));
  2615. }
  2616. return arity;
  2617. }
  2618. /* If TYPE is a record type in the form of a standard GNAT array
  2619. descriptor or a simple array type, returns the element type for
  2620. TYPE after indexing by NINDICES indices, or by all indices if
  2621. NINDICES is -1. Otherwise, returns NULL. */
  2622. struct type *
  2623. ada_array_element_type (struct type *type, int nindices)
  2624. {
  2625. type = desc_base_type (type);
  2626. if (type->code () == TYPE_CODE_STRUCT)
  2627. {
  2628. int k;
  2629. struct type *p_array_type;
  2630. p_array_type = desc_data_target_type (type);
  2631. k = ada_array_arity (type);
  2632. if (k == 0)
  2633. return NULL;
  2634. /* Initially p_array_type = elt_type(*)[]...(k times)...[]. */
  2635. if (nindices >= 0 && k > nindices)
  2636. k = nindices;
  2637. while (k > 0 && p_array_type != NULL)
  2638. {
  2639. p_array_type = ada_check_typedef (TYPE_TARGET_TYPE (p_array_type));
  2640. k -= 1;
  2641. }
  2642. return p_array_type;
  2643. }
  2644. else if (type->code () == TYPE_CODE_ARRAY)
  2645. {
  2646. while (nindices != 0 && type->code () == TYPE_CODE_ARRAY)
  2647. {
  2648. type = TYPE_TARGET_TYPE (type);
  2649. nindices -= 1;
  2650. }
  2651. return type;
  2652. }
  2653. return NULL;
  2654. }
  2655. /* See ada-lang.h. */
  2656. struct type *
  2657. ada_index_type (struct type *type, int n, const char *name)
  2658. {
  2659. struct type *result_type;
  2660. type = desc_base_type (type);
  2661. if (n < 0 || n > ada_array_arity (type))
  2662. error (_("invalid dimension number to '%s"), name);
  2663. if (ada_is_simple_array_type (type))
  2664. {
  2665. int i;
  2666. for (i = 1; i < n; i += 1)
  2667. {
  2668. type = ada_check_typedef (type);
  2669. type = TYPE_TARGET_TYPE (type);
  2670. }
  2671. result_type = TYPE_TARGET_TYPE (ada_check_typedef (type)->index_type ());
  2672. /* FIXME: The stabs type r(0,0);bound;bound in an array type
  2673. has a target type of TYPE_CODE_UNDEF. We compensate here, but
  2674. perhaps stabsread.c would make more sense. */
  2675. if (result_type && result_type->code () == TYPE_CODE_UNDEF)
  2676. result_type = NULL;
  2677. }
  2678. else
  2679. {
  2680. result_type = desc_index_type (desc_bounds_type (type), n);
  2681. if (result_type == NULL)
  2682. error (_("attempt to take bound of something that is not an array"));
  2683. }
  2684. return result_type;
  2685. }
  2686. /* Given that arr is an array type, returns the lower bound of the
  2687. Nth index (numbering from 1) if WHICH is 0, and the upper bound if
  2688. WHICH is 1. This returns bounds 0 .. -1 if ARR_TYPE is an
  2689. array-descriptor type. It works for other arrays with bounds supplied
  2690. by run-time quantities other than discriminants. */
  2691. static LONGEST
  2692. ada_array_bound_from_type (struct type *arr_type, int n, int which)
  2693. {
  2694. struct type *type, *index_type_desc, *index_type;
  2695. int i;
  2696. gdb_assert (which == 0 || which == 1);
  2697. if (ada_is_constrained_packed_array_type (arr_type))
  2698. arr_type = decode_constrained_packed_array_type (arr_type);
  2699. if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
  2700. return (LONGEST) - which;
  2701. if (arr_type->code () == TYPE_CODE_PTR)
  2702. type = TYPE_TARGET_TYPE (arr_type);
  2703. else
  2704. type = arr_type;
  2705. if (type->is_fixed_instance ())
  2706. {
  2707. /* The array has already been fixed, so we do not need to
  2708. check the parallel ___XA type again. That encoding has
  2709. already been applied, so ignore it now. */
  2710. index_type_desc = NULL;
  2711. }
  2712. else
  2713. {
  2714. index_type_desc = ada_find_parallel_type (type, "___XA");
  2715. ada_fixup_array_indexes_type (index_type_desc);
  2716. }
  2717. if (index_type_desc != NULL)
  2718. index_type = to_fixed_range_type (index_type_desc->field (n - 1).type (),
  2719. NULL);
  2720. else
  2721. {
  2722. struct type *elt_type = check_typedef (type);
  2723. for (i = 1; i < n; i++)
  2724. elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
  2725. index_type = elt_type->index_type ();
  2726. }
  2727. return
  2728. (LONGEST) (which == 0
  2729. ? ada_discrete_type_low_bound (index_type)
  2730. : ada_discrete_type_high_bound (index_type));
  2731. }
  2732. /* Given that arr is an array value, returns the lower bound of the
  2733. nth index (numbering from 1) if WHICH is 0, and the upper bound if
  2734. WHICH is 1. This routine will also work for arrays with bounds
  2735. supplied by run-time quantities other than discriminants. */
  2736. static LONGEST
  2737. ada_array_bound (struct value *arr, int n, int which)
  2738. {
  2739. struct type *arr_type;
  2740. if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
  2741. arr = value_ind (arr);
  2742. arr_type = value_enclosing_type (arr);
  2743. if (ada_is_constrained_packed_array_type (arr_type))
  2744. return ada_array_bound (decode_constrained_packed_array (arr), n, which);
  2745. else if (ada_is_simple_array_type (arr_type))
  2746. return ada_array_bound_from_type (arr_type, n, which);
  2747. else
  2748. return value_as_long (desc_one_bound (desc_bounds (arr), n, which));
  2749. }
  2750. /* Given that arr is an array value, returns the length of the
  2751. nth index. This routine will also work for arrays with bounds
  2752. supplied by run-time quantities other than discriminants.
  2753. Does not work for arrays indexed by enumeration types with representation
  2754. clauses at the moment. */
  2755. static LONGEST
  2756. ada_array_length (struct value *arr, int n)
  2757. {
  2758. struct type *arr_type, *index_type;
  2759. int low, high;
  2760. if (check_typedef (value_type (arr))->code () == TYPE_CODE_PTR)
  2761. arr = value_ind (arr);
  2762. arr_type = value_enclosing_type (arr);
  2763. if (ada_is_constrained_packed_array_type (arr_type))
  2764. return ada_array_length (decode_constrained_packed_array (arr), n);
  2765. if (ada_is_simple_array_type (arr_type))
  2766. {
  2767. low = ada_array_bound_from_type (arr_type, n, 0);
  2768. high = ada_array_bound_from_type (arr_type, n, 1);
  2769. }
  2770. else
  2771. {
  2772. low = value_as_long (desc_one_bound (desc_bounds (arr), n, 0));
  2773. high = value_as_long (desc_one_bound (desc_bounds (arr), n, 1));
  2774. }
  2775. arr_type = check_typedef (arr_type);
  2776. index_type = ada_index_type (arr_type, n, "length");
  2777. if (index_type != NULL)
  2778. {
  2779. struct type *base_type;
  2780. if (index_type->code () == TYPE_CODE_RANGE)
  2781. base_type = TYPE_TARGET_TYPE (index_type);
  2782. else
  2783. base_type = index_type;
  2784. low = pos_atr (value_from_longest (base_type, low));
  2785. high = pos_atr (value_from_longest (base_type, high));
  2786. }
  2787. return high - low + 1;
  2788. }
  2789. /* An array whose type is that of ARR_TYPE (an array type), with
  2790. bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
  2791. less than LOW, then LOW-1 is used. */
  2792. static struct value *
  2793. empty_array (struct type *arr_type, int low, int high)
  2794. {
  2795. struct type *arr_type0 = ada_check_typedef (arr_type);
  2796. struct type *index_type
  2797. = create_static_range_type
  2798. (NULL, TYPE_TARGET_TYPE (arr_type0->index_type ()), low,
  2799. high < low ? low - 1 : high);
  2800. struct type *elt_type = ada_array_element_type (arr_type0, 1);
  2801. return allocate_value (create_array_type (NULL, elt_type, index_type));
  2802. }
  2803. /* Name resolution */
  2804. /* The "decoded" name for the user-definable Ada operator corresponding
  2805. to OP. */
  2806. static const char *
  2807. ada_decoded_op_name (enum exp_opcode op)
  2808. {
  2809. int i;
  2810. for (i = 0; ada_opname_table[i].encoded != NULL; i += 1)
  2811. {
  2812. if (ada_opname_table[i].op == op)
  2813. return ada_opname_table[i].decoded;
  2814. }
  2815. error (_("Could not find operator name for opcode"));
  2816. }
  2817. /* Returns true (non-zero) iff decoded name N0 should appear before N1
  2818. in a listing of choices during disambiguation (see sort_choices, below).
  2819. The idea is that overloadings of a subprogram name from the
  2820. same package should sort in their source order. We settle for ordering
  2821. such symbols by their trailing number (__N or $N). */
  2822. static int
  2823. encoded_ordered_before (const char *N0, const char *N1)
  2824. {
  2825. if (N1 == NULL)
  2826. return 0;
  2827. else if (N0 == NULL)
  2828. return 1;
  2829. else
  2830. {
  2831. int k0, k1;
  2832. for (k0 = strlen (N0) - 1; k0 > 0 && isdigit (N0[k0]); k0 -= 1)
  2833. ;
  2834. for (k1 = strlen (N1) - 1; k1 > 0 && isdigit (N1[k1]); k1 -= 1)
  2835. ;
  2836. if ((N0[k0] == '_' || N0[k0] == '$') && N0[k0 + 1] != '\000'
  2837. && (N1[k1] == '_' || N1[k1] == '$') && N1[k1 + 1] != '\000')
  2838. {
  2839. int n0, n1;
  2840. n0 = k0;
  2841. while (N0[n0] == '_' && n0 > 0 && N0[n0 - 1] == '_')
  2842. n0 -= 1;
  2843. n1 = k1;
  2844. while (N1[n1] == '_' && n1 > 0 && N1[n1 - 1] == '_')
  2845. n1 -= 1;
  2846. if (n0 == n1 && strncmp (N0, N1, n0) == 0)
  2847. return (atoi (N0 + k0 + 1) < atoi (N1 + k1 + 1));
  2848. }
  2849. return (strcmp (N0, N1) < 0);
  2850. }
  2851. }
  2852. /* Sort SYMS[0..NSYMS-1] to put the choices in a canonical order by the
  2853. encoded names. */
  2854. static void
  2855. sort_choices (struct block_symbol syms[], int nsyms)
  2856. {
  2857. int i;
  2858. for (i = 1; i < nsyms; i += 1)
  2859. {
  2860. struct block_symbol sym = syms[i];
  2861. int j;
  2862. for (j = i - 1; j >= 0; j -= 1)
  2863. {
  2864. if (encoded_ordered_before (syms[j].symbol->linkage_name (),
  2865. sym.symbol->linkage_name ()))
  2866. break;
  2867. syms[j + 1] = syms[j];
  2868. }
  2869. syms[j + 1] = sym;
  2870. }
  2871. }
  2872. /* Whether GDB should display formals and return types for functions in the
  2873. overloads selection menu. */
  2874. static bool print_signatures = true;
  2875. /* Print the signature for SYM on STREAM according to the FLAGS options. For
  2876. all but functions, the signature is just the name of the symbol. For
  2877. functions, this is the name of the function, the list of types for formals
  2878. and the return type (if any). */
  2879. static void
  2880. ada_print_symbol_signature (struct ui_file *stream, struct symbol *sym,
  2881. const struct type_print_options *flags)
  2882. {
  2883. struct type *type = sym->type ();
  2884. gdb_printf (stream, "%s", sym->print_name ());
  2885. if (!print_signatures
  2886. || type == NULL
  2887. || type->code () != TYPE_CODE_FUNC)
  2888. return;
  2889. if (type->num_fields () > 0)
  2890. {
  2891. int i;
  2892. gdb_printf (stream, " (");
  2893. for (i = 0; i < type->num_fields (); ++i)
  2894. {
  2895. if (i > 0)
  2896. gdb_printf (stream, "; ");
  2897. ada_print_type (type->field (i).type (), NULL, stream, -1, 0,
  2898. flags);
  2899. }
  2900. gdb_printf (stream, ")");
  2901. }
  2902. if (TYPE_TARGET_TYPE (type) != NULL
  2903. && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
  2904. {
  2905. gdb_printf (stream, " return ");
  2906. ada_print_type (TYPE_TARGET_TYPE (type), NULL, stream, -1, 0, flags);
  2907. }
  2908. }
  2909. /* Read and validate a set of numeric choices from the user in the
  2910. range 0 .. N_CHOICES-1. Place the results in increasing
  2911. order in CHOICES[0 .. N-1], and return N.
  2912. The user types choices as a sequence of numbers on one line
  2913. separated by blanks, encoding them as follows:
  2914. + A choice of 0 means to cancel the selection, throwing an error.
  2915. + If IS_ALL_CHOICE, a choice of 1 selects the entire set 0 .. N_CHOICES-1.
  2916. + The user chooses k by typing k+IS_ALL_CHOICE+1.
  2917. The user is not allowed to choose more than MAX_RESULTS values.
  2918. ANNOTATION_SUFFIX, if present, is used to annotate the input
  2919. prompts (for use with the -f switch). */
  2920. static int
  2921. get_selections (int *choices, int n_choices, int max_results,
  2922. int is_all_choice, const char *annotation_suffix)
  2923. {
  2924. const char *args;
  2925. const char *prompt;
  2926. int n_chosen;
  2927. int first_choice = is_all_choice ? 2 : 1;
  2928. prompt = getenv ("PS2");
  2929. if (prompt == NULL)
  2930. prompt = "> ";
  2931. args = command_line_input (prompt, annotation_suffix);
  2932. if (args == NULL)
  2933. error_no_arg (_("one or more choice numbers"));
  2934. n_chosen = 0;
  2935. /* Set choices[0 .. n_chosen-1] to the users' choices in ascending
  2936. order, as given in args. Choices are validated. */
  2937. while (1)
  2938. {
  2939. char *args2;
  2940. int choice, j;
  2941. args = skip_spaces (args);
  2942. if (*args == '\0' && n_chosen == 0)
  2943. error_no_arg (_("one or more choice numbers"));
  2944. else if (*args == '\0')
  2945. break;
  2946. choice = strtol (args, &args2, 10);
  2947. if (args == args2 || choice < 0
  2948. || choice > n_choices + first_choice - 1)
  2949. error (_("Argument must be choice number"));
  2950. args = args2;
  2951. if (choice == 0)
  2952. error (_("cancelled"));
  2953. if (choice < first_choice)
  2954. {
  2955. n_chosen = n_choices;
  2956. for (j = 0; j < n_choices; j += 1)
  2957. choices[j] = j;
  2958. break;
  2959. }
  2960. choice -= first_choice;
  2961. for (j = n_chosen - 1; j >= 0 && choice < choices[j]; j -= 1)
  2962. {
  2963. }
  2964. if (j < 0 || choice != choices[j])
  2965. {
  2966. int k;
  2967. for (k = n_chosen - 1; k > j; k -= 1)
  2968. choices[k + 1] = choices[k];
  2969. choices[j + 1] = choice;
  2970. n_chosen += 1;
  2971. }
  2972. }
  2973. if (n_chosen > max_results)
  2974. error (_("Select no more than %d of the above"), max_results);
  2975. return n_chosen;
  2976. }
  2977. /* Given a list of NSYMS symbols in SYMS, select up to MAX_RESULTS>0
  2978. by asking the user (if necessary), returning the number selected,
  2979. and setting the first elements of SYMS items. Error if no symbols
  2980. selected. */
  2981. /* NOTE: Adapted from decode_line_2 in symtab.c, with which it ought
  2982. to be re-integrated one of these days. */
  2983. static int
  2984. user_select_syms (struct block_symbol *syms, int nsyms, int max_results)
  2985. {
  2986. int i;
  2987. int *chosen = XALLOCAVEC (int , nsyms);
  2988. int n_chosen;
  2989. int first_choice = (max_results == 1) ? 1 : 2;
  2990. const char *select_mode = multiple_symbols_select_mode ();
  2991. if (max_results < 1)
  2992. error (_("Request to select 0 symbols!"));
  2993. if (nsyms <= 1)
  2994. return nsyms;
  2995. if (select_mode == multiple_symbols_cancel)
  2996. error (_("\
  2997. canceled because the command is ambiguous\n\
  2998. See set/show multiple-symbol."));
  2999. /* If select_mode is "all", then return all possible symbols.
  3000. Only do that if more than one symbol can be selected, of course.
  3001. Otherwise, display the menu as usual. */
  3002. if (select_mode == multiple_symbols_all && max_results > 1)
  3003. return nsyms;
  3004. gdb_printf (_("[0] cancel\n"));
  3005. if (max_results > 1)
  3006. gdb_printf (_("[1] all\n"));
  3007. sort_choices (syms, nsyms);
  3008. for (i = 0; i < nsyms; i += 1)
  3009. {
  3010. if (syms[i].symbol == NULL)
  3011. continue;
  3012. if (syms[i].symbol->aclass () == LOC_BLOCK)
  3013. {
  3014. struct symtab_and_line sal =
  3015. find_function_start_sal (syms[i].symbol, 1);
  3016. gdb_printf ("[%d] ", i + first_choice);
  3017. ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
  3018. &type_print_raw_options);
  3019. if (sal.symtab == NULL)
  3020. gdb_printf (_(" at %p[<no source file available>%p]:%d\n"),
  3021. metadata_style.style ().ptr (), nullptr, sal.line);
  3022. else
  3023. gdb_printf
  3024. (_(" at %ps:%d\n"),
  3025. styled_string (file_name_style.style (),
  3026. symtab_to_filename_for_display (sal.symtab)),
  3027. sal.line);
  3028. continue;
  3029. }
  3030. else
  3031. {
  3032. int is_enumeral =
  3033. (syms[i].symbol->aclass () == LOC_CONST
  3034. && syms[i].symbol->type () != NULL
  3035. && syms[i].symbol->type ()->code () == TYPE_CODE_ENUM);
  3036. struct symtab *symtab = NULL;
  3037. if (syms[i].symbol->is_objfile_owned ())
  3038. symtab = symbol_symtab (syms[i].symbol);
  3039. if (syms[i].symbol->line () != 0 && symtab != NULL)
  3040. {
  3041. gdb_printf ("[%d] ", i + first_choice);
  3042. ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
  3043. &type_print_raw_options);
  3044. gdb_printf (_(" at %s:%d\n"),
  3045. symtab_to_filename_for_display (symtab),
  3046. syms[i].symbol->line ());
  3047. }
  3048. else if (is_enumeral
  3049. && syms[i].symbol->type ()->name () != NULL)
  3050. {
  3051. gdb_printf (("[%d] "), i + first_choice);
  3052. ada_print_type (syms[i].symbol->type (), NULL,
  3053. gdb_stdout, -1, 0, &type_print_raw_options);
  3054. gdb_printf (_("'(%s) (enumeral)\n"),
  3055. syms[i].symbol->print_name ());
  3056. }
  3057. else
  3058. {
  3059. gdb_printf ("[%d] ", i + first_choice);
  3060. ada_print_symbol_signature (gdb_stdout, syms[i].symbol,
  3061. &type_print_raw_options);
  3062. if (symtab != NULL)
  3063. gdb_printf (is_enumeral
  3064. ? _(" in %s (enumeral)\n")
  3065. : _(" at %s:?\n"),
  3066. symtab_to_filename_for_display (symtab));
  3067. else
  3068. gdb_printf (is_enumeral
  3069. ? _(" (enumeral)\n")
  3070. : _(" at ?\n"));
  3071. }
  3072. }
  3073. }
  3074. n_chosen = get_selections (chosen, nsyms, max_results, max_results > 1,
  3075. "overload-choice");
  3076. for (i = 0; i < n_chosen; i += 1)
  3077. syms[i] = syms[chosen[i]];
  3078. return n_chosen;
  3079. }
  3080. /* See ada-lang.h. */
  3081. block_symbol
  3082. ada_find_operator_symbol (enum exp_opcode op, bool parse_completion,
  3083. int nargs, value *argvec[])
  3084. {
  3085. if (possible_user_operator_p (op, argvec))
  3086. {
  3087. std::vector<struct block_symbol> candidates
  3088. = ada_lookup_symbol_list (ada_decoded_op_name (op),
  3089. NULL, VAR_DOMAIN);
  3090. int i = ada_resolve_function (candidates, argvec,
  3091. nargs, ada_decoded_op_name (op), NULL,
  3092. parse_completion);
  3093. if (i >= 0)
  3094. return candidates[i];
  3095. }
  3096. return {};
  3097. }
  3098. /* See ada-lang.h. */
  3099. block_symbol
  3100. ada_resolve_funcall (struct symbol *sym, const struct block *block,
  3101. struct type *context_type,
  3102. bool parse_completion,
  3103. int nargs, value *argvec[],
  3104. innermost_block_tracker *tracker)
  3105. {
  3106. std::vector<struct block_symbol> candidates
  3107. = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
  3108. int i;
  3109. if (candidates.size () == 1)
  3110. i = 0;
  3111. else
  3112. {
  3113. i = ada_resolve_function
  3114. (candidates,
  3115. argvec, nargs,
  3116. sym->linkage_name (),
  3117. context_type, parse_completion);
  3118. if (i < 0)
  3119. error (_("Could not find a match for %s"), sym->print_name ());
  3120. }
  3121. tracker->update (candidates[i]);
  3122. return candidates[i];
  3123. }
  3124. /* Resolve a mention of a name where the context type is an
  3125. enumeration type. */
  3126. static int
  3127. ada_resolve_enum (std::vector<struct block_symbol> &syms,
  3128. const char *name, struct type *context_type,
  3129. bool parse_completion)
  3130. {
  3131. gdb_assert (context_type->code () == TYPE_CODE_ENUM);
  3132. context_type = ada_check_typedef (context_type);
  3133. for (int i = 0; i < syms.size (); ++i)
  3134. {
  3135. /* We already know the name matches, so we're just looking for
  3136. an element of the correct enum type. */
  3137. if (ada_check_typedef (syms[i].symbol->type ()) == context_type)
  3138. return i;
  3139. }
  3140. error (_("No name '%s' in enumeration type '%s'"), name,
  3141. ada_type_name (context_type));
  3142. }
  3143. /* See ada-lang.h. */
  3144. block_symbol
  3145. ada_resolve_variable (struct symbol *sym, const struct block *block,
  3146. struct type *context_type,
  3147. bool parse_completion,
  3148. int deprocedure_p,
  3149. innermost_block_tracker *tracker)
  3150. {
  3151. std::vector<struct block_symbol> candidates
  3152. = ada_lookup_symbol_list (sym->linkage_name (), block, VAR_DOMAIN);
  3153. if (std::any_of (candidates.begin (),
  3154. candidates.end (),
  3155. [] (block_symbol &bsym)
  3156. {
  3157. switch (bsym.symbol->aclass ())
  3158. {
  3159. case LOC_REGISTER:
  3160. case LOC_ARG:
  3161. case LOC_REF_ARG:
  3162. case LOC_REGPARM_ADDR:
  3163. case LOC_LOCAL:
  3164. case LOC_COMPUTED:
  3165. return true;
  3166. default:
  3167. return false;
  3168. }
  3169. }))
  3170. {
  3171. /* Types tend to get re-introduced locally, so if there
  3172. are any local symbols that are not types, first filter
  3173. out all types. */
  3174. candidates.erase
  3175. (std::remove_if
  3176. (candidates.begin (),
  3177. candidates.end (),
  3178. [] (block_symbol &bsym)
  3179. {
  3180. return bsym.symbol->aclass () == LOC_TYPEDEF;
  3181. }),
  3182. candidates.end ());
  3183. }
  3184. /* Filter out artificial symbols. */
  3185. candidates.erase
  3186. (std::remove_if
  3187. (candidates.begin (),
  3188. candidates.end (),
  3189. [] (block_symbol &bsym)
  3190. {
  3191. return bsym.symbol->artificial;
  3192. }),
  3193. candidates.end ());
  3194. int i;
  3195. if (candidates.empty ())
  3196. error (_("No definition found for %s"), sym->print_name ());
  3197. else if (candidates.size () == 1)
  3198. i = 0;
  3199. else if (context_type != nullptr
  3200. && context_type->code () == TYPE_CODE_ENUM)
  3201. i = ada_resolve_enum (candidates, sym->linkage_name (), context_type,
  3202. parse_completion);
  3203. else if (deprocedure_p && !is_nonfunction (candidates))
  3204. {
  3205. i = ada_resolve_function
  3206. (candidates, NULL, 0,
  3207. sym->linkage_name (),
  3208. context_type, parse_completion);
  3209. if (i < 0)
  3210. error (_("Could not find a match for %s"), sym->print_name ());
  3211. }
  3212. else
  3213. {
  3214. gdb_printf (_("Multiple matches for %s\n"), sym->print_name ());
  3215. user_select_syms (candidates.data (), candidates.size (), 1);
  3216. i = 0;
  3217. }
  3218. tracker->update (candidates[i]);
  3219. return candidates[i];
  3220. }
  3221. /* Return non-zero if formal type FTYPE matches actual type ATYPE. */
  3222. /* The term "match" here is rather loose. The match is heuristic and
  3223. liberal. */
  3224. static int
  3225. ada_type_match (struct type *ftype, struct type *atype)
  3226. {
  3227. ftype = ada_check_typedef (ftype);
  3228. atype = ada_check_typedef (atype);
  3229. if (ftype->code () == TYPE_CODE_REF)
  3230. ftype = TYPE_TARGET_TYPE (ftype);
  3231. if (atype->code () == TYPE_CODE_REF)
  3232. atype = TYPE_TARGET_TYPE (atype);
  3233. switch (ftype->code ())
  3234. {
  3235. default:
  3236. return ftype->code () == atype->code ();
  3237. case TYPE_CODE_PTR:
  3238. if (atype->code () != TYPE_CODE_PTR)
  3239. return 0;
  3240. atype = TYPE_TARGET_TYPE (atype);
  3241. /* This can only happen if the actual argument is 'null'. */
  3242. if (atype->code () == TYPE_CODE_INT && TYPE_LENGTH (atype) == 0)
  3243. return 1;
  3244. return ada_type_match (TYPE_TARGET_TYPE (ftype), atype);
  3245. case TYPE_CODE_INT:
  3246. case TYPE_CODE_ENUM:
  3247. case TYPE_CODE_RANGE:
  3248. switch (atype->code ())
  3249. {
  3250. case TYPE_CODE_INT:
  3251. case TYPE_CODE_ENUM:
  3252. case TYPE_CODE_RANGE:
  3253. return 1;
  3254. default:
  3255. return 0;
  3256. }
  3257. case TYPE_CODE_ARRAY:
  3258. return (atype->code () == TYPE_CODE_ARRAY
  3259. || ada_is_array_descriptor_type (atype));
  3260. case TYPE_CODE_STRUCT:
  3261. if (ada_is_array_descriptor_type (ftype))
  3262. return (atype->code () == TYPE_CODE_ARRAY
  3263. || ada_is_array_descriptor_type (atype));
  3264. else
  3265. return (atype->code () == TYPE_CODE_STRUCT
  3266. && !ada_is_array_descriptor_type (atype));
  3267. case TYPE_CODE_UNION:
  3268. case TYPE_CODE_FLT:
  3269. return (atype->code () == ftype->code ());
  3270. }
  3271. }
  3272. /* Return non-zero if the formals of FUNC "sufficiently match" the
  3273. vector of actual argument types ACTUALS of size N_ACTUALS. FUNC
  3274. may also be an enumeral, in which case it is treated as a 0-
  3275. argument function. */
  3276. static int
  3277. ada_args_match (struct symbol *func, struct value **actuals, int n_actuals)
  3278. {
  3279. int i;
  3280. struct type *func_type = func->type ();
  3281. if (func->aclass () == LOC_CONST
  3282. && func_type->code () == TYPE_CODE_ENUM)
  3283. return (n_actuals == 0);
  3284. else if (func_type == NULL || func_type->code () != TYPE_CODE_FUNC)
  3285. return 0;
  3286. if (func_type->num_fields () != n_actuals)
  3287. return 0;
  3288. for (i = 0; i < n_actuals; i += 1)
  3289. {
  3290. if (actuals[i] == NULL)
  3291. return 0;
  3292. else
  3293. {
  3294. struct type *ftype = ada_check_typedef (func_type->field (i).type ());
  3295. struct type *atype = ada_check_typedef (value_type (actuals[i]));
  3296. if (!ada_type_match (ftype, atype))
  3297. return 0;
  3298. }
  3299. }
  3300. return 1;
  3301. }
  3302. /* False iff function type FUNC_TYPE definitely does not produce a value
  3303. compatible with type CONTEXT_TYPE. Conservatively returns 1 if
  3304. FUNC_TYPE is not a valid function type with a non-null return type
  3305. or an enumerated type. A null CONTEXT_TYPE indicates any non-void type. */
  3306. static int
  3307. return_match (struct type *func_type, struct type *context_type)
  3308. {
  3309. struct type *return_type;
  3310. if (func_type == NULL)
  3311. return 1;
  3312. if (func_type->code () == TYPE_CODE_FUNC)
  3313. return_type = get_base_type (TYPE_TARGET_TYPE (func_type));
  3314. else
  3315. return_type = get_base_type (func_type);
  3316. if (return_type == NULL)
  3317. return 1;
  3318. context_type = get_base_type (context_type);
  3319. if (return_type->code () == TYPE_CODE_ENUM)
  3320. return context_type == NULL || return_type == context_type;
  3321. else if (context_type == NULL)
  3322. return return_type->code () != TYPE_CODE_VOID;
  3323. else
  3324. return return_type->code () == context_type->code ();
  3325. }
  3326. /* Returns the index in SYMS that contains the symbol for the
  3327. function (if any) that matches the types of the NARGS arguments in
  3328. ARGS. If CONTEXT_TYPE is non-null and there is at least one match
  3329. that returns that type, then eliminate matches that don't. If
  3330. CONTEXT_TYPE is void and there is at least one match that does not
  3331. return void, eliminate all matches that do.
  3332. Asks the user if there is more than one match remaining. Returns -1
  3333. if there is no such symbol or none is selected. NAME is used
  3334. solely for messages. May re-arrange and modify SYMS in
  3335. the process; the index returned is for the modified vector. */
  3336. static int
  3337. ada_resolve_function (std::vector<struct block_symbol> &syms,
  3338. struct value **args, int nargs,
  3339. const char *name, struct type *context_type,
  3340. bool parse_completion)
  3341. {
  3342. int fallback;
  3343. int k;
  3344. int m; /* Number of hits */
  3345. m = 0;
  3346. /* In the first pass of the loop, we only accept functions matching
  3347. context_type. If none are found, we add a second pass of the loop
  3348. where every function is accepted. */
  3349. for (fallback = 0; m == 0 && fallback < 2; fallback++)
  3350. {
  3351. for (k = 0; k < syms.size (); k += 1)
  3352. {
  3353. struct type *type = ada_check_typedef (syms[k].symbol->type ());
  3354. if (ada_args_match (syms[k].symbol, args, nargs)
  3355. && (fallback || return_match (type, context_type)))
  3356. {
  3357. syms[m] = syms[k];
  3358. m += 1;
  3359. }
  3360. }
  3361. }
  3362. /* If we got multiple matches, ask the user which one to use. Don't do this
  3363. interactive thing during completion, though, as the purpose of the
  3364. completion is providing a list of all possible matches. Prompting the
  3365. user to filter it down would be completely unexpected in this case. */
  3366. if (m == 0)
  3367. return -1;
  3368. else if (m > 1 && !parse_completion)
  3369. {
  3370. gdb_printf (_("Multiple matches for %s\n"), name);
  3371. user_select_syms (syms.data (), m, 1);
  3372. return 0;
  3373. }
  3374. return 0;
  3375. }
  3376. /* Type-class predicates */
  3377. /* True iff TYPE is numeric (i.e., an INT, RANGE (of numeric type),
  3378. or FLOAT). */
  3379. static int
  3380. numeric_type_p (struct type *type)
  3381. {
  3382. if (type == NULL)
  3383. return 0;
  3384. else
  3385. {
  3386. switch (type->code ())
  3387. {
  3388. case TYPE_CODE_INT:
  3389. case TYPE_CODE_FLT:
  3390. case TYPE_CODE_FIXED_POINT:
  3391. return 1;
  3392. case TYPE_CODE_RANGE:
  3393. return (type == TYPE_TARGET_TYPE (type)
  3394. || numeric_type_p (TYPE_TARGET_TYPE (type)));
  3395. default:
  3396. return 0;
  3397. }
  3398. }
  3399. }
  3400. /* True iff TYPE is integral (an INT or RANGE of INTs). */
  3401. static int
  3402. integer_type_p (struct type *type)
  3403. {
  3404. if (type == NULL)
  3405. return 0;
  3406. else
  3407. {
  3408. switch (type->code ())
  3409. {
  3410. case TYPE_CODE_INT:
  3411. return 1;
  3412. case TYPE_CODE_RANGE:
  3413. return (type == TYPE_TARGET_TYPE (type)
  3414. || integer_type_p (TYPE_TARGET_TYPE (type)));
  3415. default:
  3416. return 0;
  3417. }
  3418. }
  3419. }
  3420. /* True iff TYPE is scalar (INT, RANGE, FLOAT, ENUM). */
  3421. static int
  3422. scalar_type_p (struct type *type)
  3423. {
  3424. if (type == NULL)
  3425. return 0;
  3426. else
  3427. {
  3428. switch (type->code ())
  3429. {
  3430. case TYPE_CODE_INT:
  3431. case TYPE_CODE_RANGE:
  3432. case TYPE_CODE_ENUM:
  3433. case TYPE_CODE_FLT:
  3434. case TYPE_CODE_FIXED_POINT:
  3435. return 1;
  3436. default:
  3437. return 0;
  3438. }
  3439. }
  3440. }
  3441. /* True iff TYPE is discrete (INT, RANGE, ENUM). */
  3442. static int
  3443. discrete_type_p (struct type *type)
  3444. {
  3445. if (type == NULL)
  3446. return 0;
  3447. else
  3448. {
  3449. switch (type->code ())
  3450. {
  3451. case TYPE_CODE_INT:
  3452. case TYPE_CODE_RANGE:
  3453. case TYPE_CODE_ENUM:
  3454. case TYPE_CODE_BOOL:
  3455. return 1;
  3456. default:
  3457. return 0;
  3458. }
  3459. }
  3460. }
  3461. /* Returns non-zero if OP with operands in the vector ARGS could be
  3462. a user-defined function. Errs on the side of pre-defined operators
  3463. (i.e., result 0). */
  3464. static int
  3465. possible_user_operator_p (enum exp_opcode op, struct value *args[])
  3466. {
  3467. struct type *type0 =
  3468. (args[0] == NULL) ? NULL : ada_check_typedef (value_type (args[0]));
  3469. struct type *type1 =
  3470. (args[1] == NULL) ? NULL : ada_check_typedef (value_type (args[1]));
  3471. if (type0 == NULL)
  3472. return 0;
  3473. switch (op)
  3474. {
  3475. default:
  3476. return 0;
  3477. case BINOP_ADD:
  3478. case BINOP_SUB:
  3479. case BINOP_MUL:
  3480. case BINOP_DIV:
  3481. return (!(numeric_type_p (type0) && numeric_type_p (type1)));
  3482. case BINOP_REM:
  3483. case BINOP_MOD:
  3484. case BINOP_BITWISE_AND:
  3485. case BINOP_BITWISE_IOR:
  3486. case BINOP_BITWISE_XOR:
  3487. return (!(integer_type_p (type0) && integer_type_p (type1)));
  3488. case BINOP_EQUAL:
  3489. case BINOP_NOTEQUAL:
  3490. case BINOP_LESS:
  3491. case BINOP_GTR:
  3492. case BINOP_LEQ:
  3493. case BINOP_GEQ:
  3494. return (!(scalar_type_p (type0) && scalar_type_p (type1)));
  3495. case BINOP_CONCAT:
  3496. return !ada_is_array_type (type0) || !ada_is_array_type (type1);
  3497. case BINOP_EXP:
  3498. return (!(numeric_type_p (type0) && integer_type_p (type1)));
  3499. case UNOP_NEG:
  3500. case UNOP_PLUS:
  3501. case UNOP_LOGICAL_NOT:
  3502. case UNOP_ABS:
  3503. return (!numeric_type_p (type0));
  3504. }
  3505. }
  3506. /* Renaming */
  3507. /* NOTES:
  3508. 1. In the following, we assume that a renaming type's name may
  3509. have an ___XD suffix. It would be nice if this went away at some
  3510. point.
  3511. 2. We handle both the (old) purely type-based representation of
  3512. renamings and the (new) variable-based encoding. At some point,
  3513. it is devoutly to be hoped that the former goes away
  3514. (FIXME: hilfinger-2007-07-09).
  3515. 3. Subprogram renamings are not implemented, although the XRS
  3516. suffix is recognized (FIXME: hilfinger-2007-07-09). */
  3517. /* If SYM encodes a renaming,
  3518. <renaming> renames <renamed entity>,
  3519. sets *LEN to the length of the renamed entity's name,
  3520. *RENAMED_ENTITY to that name (not null-terminated), and *RENAMING_EXPR to
  3521. the string describing the subcomponent selected from the renamed
  3522. entity. Returns ADA_NOT_RENAMING if SYM does not encode a renaming
  3523. (in which case, the values of *RENAMED_ENTITY, *LEN, and *RENAMING_EXPR
  3524. are undefined). Otherwise, returns a value indicating the category
  3525. of entity renamed: an object (ADA_OBJECT_RENAMING), exception
  3526. (ADA_EXCEPTION_RENAMING), package (ADA_PACKAGE_RENAMING), or
  3527. subprogram (ADA_SUBPROGRAM_RENAMING). Does no allocation; the
  3528. strings returned in *RENAMED_ENTITY and *RENAMING_EXPR should not be
  3529. deallocated. The values of RENAMED_ENTITY, LEN, or RENAMING_EXPR
  3530. may be NULL, in which case they are not assigned.
  3531. [Currently, however, GCC does not generate subprogram renamings.] */
  3532. enum ada_renaming_category
  3533. ada_parse_renaming (struct symbol *sym,
  3534. const char **renamed_entity, int *len,
  3535. const char **renaming_expr)
  3536. {
  3537. enum ada_renaming_category kind;
  3538. const char *info;
  3539. const char *suffix;
  3540. if (sym == NULL)
  3541. return ADA_NOT_RENAMING;
  3542. switch (sym->aclass ())
  3543. {
  3544. default:
  3545. return ADA_NOT_RENAMING;
  3546. case LOC_LOCAL:
  3547. case LOC_STATIC:
  3548. case LOC_COMPUTED:
  3549. case LOC_OPTIMIZED_OUT:
  3550. info = strstr (sym->linkage_name (), "___XR");
  3551. if (info == NULL)
  3552. return ADA_NOT_RENAMING;
  3553. switch (info[5])
  3554. {
  3555. case '_':
  3556. kind = ADA_OBJECT_RENAMING;
  3557. info += 6;
  3558. break;
  3559. case 'E':
  3560. kind = ADA_EXCEPTION_RENAMING;
  3561. info += 7;
  3562. break;
  3563. case 'P':
  3564. kind = ADA_PACKAGE_RENAMING;
  3565. info += 7;
  3566. break;
  3567. case 'S':
  3568. kind = ADA_SUBPROGRAM_RENAMING;
  3569. info += 7;
  3570. break;
  3571. default:
  3572. return ADA_NOT_RENAMING;
  3573. }
  3574. }
  3575. if (renamed_entity != NULL)
  3576. *renamed_entity = info;
  3577. suffix = strstr (info, "___XE");
  3578. if (suffix == NULL || suffix == info)
  3579. return ADA_NOT_RENAMING;
  3580. if (len != NULL)
  3581. *len = strlen (info) - strlen (suffix);
  3582. suffix += 5;
  3583. if (renaming_expr != NULL)
  3584. *renaming_expr = suffix;
  3585. return kind;
  3586. }
  3587. /* Compute the value of the given RENAMING_SYM, which is expected to
  3588. be a symbol encoding a renaming expression. BLOCK is the block
  3589. used to evaluate the renaming. */
  3590. static struct value *
  3591. ada_read_renaming_var_value (struct symbol *renaming_sym,
  3592. const struct block *block)
  3593. {
  3594. const char *sym_name;
  3595. sym_name = renaming_sym->linkage_name ();
  3596. expression_up expr = parse_exp_1 (&sym_name, 0, block, 0);
  3597. return evaluate_expression (expr.get ());
  3598. }
  3599. /* Evaluation: Function Calls */
  3600. /* Return an lvalue containing the value VAL. This is the identity on
  3601. lvalues, and otherwise has the side-effect of allocating memory
  3602. in the inferior where a copy of the value contents is copied. */
  3603. static struct value *
  3604. ensure_lval (struct value *val)
  3605. {
  3606. if (VALUE_LVAL (val) == not_lval
  3607. || VALUE_LVAL (val) == lval_internalvar)
  3608. {
  3609. int len = TYPE_LENGTH (ada_check_typedef (value_type (val)));
  3610. const CORE_ADDR addr =
  3611. value_as_long (value_allocate_space_in_inferior (len));
  3612. VALUE_LVAL (val) = lval_memory;
  3613. set_value_address (val, addr);
  3614. write_memory (addr, value_contents (val).data (), len);
  3615. }
  3616. return val;
  3617. }
  3618. /* Given ARG, a value of type (pointer or reference to a)*
  3619. structure/union, extract the component named NAME from the ultimate
  3620. target structure/union and return it as a value with its
  3621. appropriate type.
  3622. The routine searches for NAME among all members of the structure itself
  3623. and (recursively) among all members of any wrapper members
  3624. (e.g., '_parent').
  3625. If NO_ERR, then simply return NULL in case of error, rather than
  3626. calling error. */
  3627. static struct value *
  3628. ada_value_struct_elt (struct value *arg, const char *name, int no_err)
  3629. {
  3630. struct type *t, *t1;
  3631. struct value *v;
  3632. int check_tag;
  3633. v = NULL;
  3634. t1 = t = ada_check_typedef (value_type (arg));
  3635. if (t->code () == TYPE_CODE_REF)
  3636. {
  3637. t1 = TYPE_TARGET_TYPE (t);
  3638. if (t1 == NULL)
  3639. goto BadValue;
  3640. t1 = ada_check_typedef (t1);
  3641. if (t1->code () == TYPE_CODE_PTR)
  3642. {
  3643. arg = coerce_ref (arg);
  3644. t = t1;
  3645. }
  3646. }
  3647. while (t->code () == TYPE_CODE_PTR)
  3648. {
  3649. t1 = TYPE_TARGET_TYPE (t);
  3650. if (t1 == NULL)
  3651. goto BadValue;
  3652. t1 = ada_check_typedef (t1);
  3653. if (t1->code () == TYPE_CODE_PTR)
  3654. {
  3655. arg = value_ind (arg);
  3656. t = t1;
  3657. }
  3658. else
  3659. break;
  3660. }
  3661. if (t1->code () != TYPE_CODE_STRUCT && t1->code () != TYPE_CODE_UNION)
  3662. goto BadValue;
  3663. if (t1 == t)
  3664. v = ada_search_struct_field (name, arg, 0, t);
  3665. else
  3666. {
  3667. int bit_offset, bit_size, byte_offset;
  3668. struct type *field_type;
  3669. CORE_ADDR address;
  3670. if (t->code () == TYPE_CODE_PTR)
  3671. address = value_address (ada_value_ind (arg));
  3672. else
  3673. address = value_address (ada_coerce_ref (arg));
  3674. /* Check to see if this is a tagged type. We also need to handle
  3675. the case where the type is a reference to a tagged type, but
  3676. we have to be careful to exclude pointers to tagged types.
  3677. The latter should be shown as usual (as a pointer), whereas
  3678. a reference should mostly be transparent to the user. */
  3679. if (ada_is_tagged_type (t1, 0)
  3680. || (t1->code () == TYPE_CODE_REF
  3681. && ada_is_tagged_type (TYPE_TARGET_TYPE (t1), 0)))
  3682. {
  3683. /* We first try to find the searched field in the current type.
  3684. If not found then let's look in the fixed type. */
  3685. if (!find_struct_field (name, t1, 0,
  3686. nullptr, nullptr, nullptr,
  3687. nullptr, nullptr))
  3688. check_tag = 1;
  3689. else
  3690. check_tag = 0;
  3691. }
  3692. else
  3693. check_tag = 0;
  3694. /* Convert to fixed type in all cases, so that we have proper
  3695. offsets to each field in unconstrained record types. */
  3696. t1 = ada_to_fixed_type (ada_get_base_type (t1), NULL,
  3697. address, NULL, check_tag);
  3698. /* Resolve the dynamic type as well. */
  3699. arg = value_from_contents_and_address (t1, nullptr, address);
  3700. t1 = value_type (arg);
  3701. if (find_struct_field (name, t1, 0,
  3702. &field_type, &byte_offset, &bit_offset,
  3703. &bit_size, NULL))
  3704. {
  3705. if (bit_size != 0)
  3706. {
  3707. if (t->code () == TYPE_CODE_REF)
  3708. arg = ada_coerce_ref (arg);
  3709. else
  3710. arg = ada_value_ind (arg);
  3711. v = ada_value_primitive_packed_val (arg, NULL, byte_offset,
  3712. bit_offset, bit_size,
  3713. field_type);
  3714. }
  3715. else
  3716. v = value_at_lazy (field_type, address + byte_offset);
  3717. }
  3718. }
  3719. if (v != NULL || no_err)
  3720. return v;
  3721. else
  3722. error (_("There is no member named %s."), name);
  3723. BadValue:
  3724. if (no_err)
  3725. return NULL;
  3726. else
  3727. error (_("Attempt to extract a component of "
  3728. "a value that is not a record."));
  3729. }
  3730. /* Return the value ACTUAL, converted to be an appropriate value for a
  3731. formal of type FORMAL_TYPE. Use *SP as a stack pointer for
  3732. allocating any necessary descriptors (fat pointers), or copies of
  3733. values not residing in memory, updating it as needed. */
  3734. struct value *
  3735. ada_convert_actual (struct value *actual, struct type *formal_type0)
  3736. {
  3737. struct type *actual_type = ada_check_typedef (value_type (actual));
  3738. struct type *formal_type = ada_check_typedef (formal_type0);
  3739. struct type *formal_target =
  3740. formal_type->code () == TYPE_CODE_PTR
  3741. ? ada_check_typedef (TYPE_TARGET_TYPE (formal_type)) : formal_type;
  3742. struct type *actual_target =
  3743. actual_type->code () == TYPE_CODE_PTR
  3744. ? ada_check_typedef (TYPE_TARGET_TYPE (actual_type)) : actual_type;
  3745. if (ada_is_array_descriptor_type (formal_target)
  3746. && actual_target->code () == TYPE_CODE_ARRAY)
  3747. return make_array_descriptor (formal_type, actual);
  3748. else if (formal_type->code () == TYPE_CODE_PTR
  3749. || formal_type->code () == TYPE_CODE_REF)
  3750. {
  3751. struct value *result;
  3752. if (formal_target->code () == TYPE_CODE_ARRAY
  3753. && ada_is_array_descriptor_type (actual_target))
  3754. result = desc_data (actual);
  3755. else if (formal_type->code () != TYPE_CODE_PTR)
  3756. {
  3757. if (VALUE_LVAL (actual) != lval_memory)
  3758. {
  3759. struct value *val;
  3760. actual_type = ada_check_typedef (value_type (actual));
  3761. val = allocate_value (actual_type);
  3762. copy (value_contents (actual), value_contents_raw (val));
  3763. actual = ensure_lval (val);
  3764. }
  3765. result = value_addr (actual);
  3766. }
  3767. else
  3768. return actual;
  3769. return value_cast_pointers (formal_type, result, 0);
  3770. }
  3771. else if (actual_type->code () == TYPE_CODE_PTR)
  3772. return ada_value_ind (actual);
  3773. else if (ada_is_aligner_type (formal_type))
  3774. {
  3775. /* We need to turn this parameter into an aligner type
  3776. as well. */
  3777. struct value *aligner = allocate_value (formal_type);
  3778. struct value *component = ada_value_struct_elt (aligner, "F", 0);
  3779. value_assign_to_component (aligner, component, actual);
  3780. return aligner;
  3781. }
  3782. return actual;
  3783. }
  3784. /* Convert VALUE (which must be an address) to a CORE_ADDR that is a pointer of
  3785. type TYPE. This is usually an inefficient no-op except on some targets
  3786. (such as AVR) where the representation of a pointer and an address
  3787. differs. */
  3788. static CORE_ADDR
  3789. value_pointer (struct value *value, struct type *type)
  3790. {
  3791. unsigned len = TYPE_LENGTH (type);
  3792. gdb_byte *buf = (gdb_byte *) alloca (len);
  3793. CORE_ADDR addr;
  3794. addr = value_address (value);
  3795. gdbarch_address_to_pointer (type->arch (), type, buf, addr);
  3796. addr = extract_unsigned_integer (buf, len, type_byte_order (type));
  3797. return addr;
  3798. }
  3799. /* Push a descriptor of type TYPE for array value ARR on the stack at
  3800. *SP, updating *SP to reflect the new descriptor. Return either
  3801. an lvalue representing the new descriptor, or (if TYPE is a pointer-
  3802. to-descriptor type rather than a descriptor type), a struct value *
  3803. representing a pointer to this descriptor. */
  3804. static struct value *
  3805. make_array_descriptor (struct type *type, struct value *arr)
  3806. {
  3807. struct type *bounds_type = desc_bounds_type (type);
  3808. struct type *desc_type = desc_base_type (type);
  3809. struct value *descriptor = allocate_value (desc_type);
  3810. struct value *bounds = allocate_value (bounds_type);
  3811. int i;
  3812. for (i = ada_array_arity (ada_check_typedef (value_type (arr)));
  3813. i > 0; i -= 1)
  3814. {
  3815. modify_field (value_type (bounds),
  3816. value_contents_writeable (bounds).data (),
  3817. ada_array_bound (arr, i, 0),
  3818. desc_bound_bitpos (bounds_type, i, 0),
  3819. desc_bound_bitsize (bounds_type, i, 0));
  3820. modify_field (value_type (bounds),
  3821. value_contents_writeable (bounds).data (),
  3822. ada_array_bound (arr, i, 1),
  3823. desc_bound_bitpos (bounds_type, i, 1),
  3824. desc_bound_bitsize (bounds_type, i, 1));
  3825. }
  3826. bounds = ensure_lval (bounds);
  3827. modify_field (value_type (descriptor),
  3828. value_contents_writeable (descriptor).data (),
  3829. value_pointer (ensure_lval (arr),
  3830. desc_type->field (0).type ()),
  3831. fat_pntr_data_bitpos (desc_type),
  3832. fat_pntr_data_bitsize (desc_type));
  3833. modify_field (value_type (descriptor),
  3834. value_contents_writeable (descriptor).data (),
  3835. value_pointer (bounds,
  3836. desc_type->field (1).type ()),
  3837. fat_pntr_bounds_bitpos (desc_type),
  3838. fat_pntr_bounds_bitsize (desc_type));
  3839. descriptor = ensure_lval (descriptor);
  3840. if (type->code () == TYPE_CODE_PTR)
  3841. return value_addr (descriptor);
  3842. else
  3843. return descriptor;
  3844. }
  3845. /* Symbol Cache Module */
  3846. /* Performance measurements made as of 2010-01-15 indicate that
  3847. this cache does bring some noticeable improvements. Depending
  3848. on the type of entity being printed, the cache can make it as much
  3849. as an order of magnitude faster than without it.
  3850. The descriptive type DWARF extension has significantly reduced
  3851. the need for this cache, at least when DWARF is being used. However,
  3852. even in this case, some expensive name-based symbol searches are still
  3853. sometimes necessary - to find an XVZ variable, mostly. */
  3854. /* Return the symbol cache associated to the given program space PSPACE.
  3855. If not allocated for this PSPACE yet, allocate and initialize one. */
  3856. static struct ada_symbol_cache *
  3857. ada_get_symbol_cache (struct program_space *pspace)
  3858. {
  3859. struct ada_pspace_data *pspace_data = get_ada_pspace_data (pspace);
  3860. if (pspace_data->sym_cache == nullptr)
  3861. pspace_data->sym_cache.reset (new ada_symbol_cache);
  3862. return pspace_data->sym_cache.get ();
  3863. }
  3864. /* Clear all entries from the symbol cache. */
  3865. static void
  3866. ada_clear_symbol_cache ()
  3867. {
  3868. struct ada_pspace_data *pspace_data
  3869. = get_ada_pspace_data (current_program_space);
  3870. if (pspace_data->sym_cache != nullptr)
  3871. pspace_data->sym_cache.reset ();
  3872. }
  3873. /* Search our cache for an entry matching NAME and DOMAIN.
  3874. Return it if found, or NULL otherwise. */
  3875. static struct cache_entry **
  3876. find_entry (const char *name, domain_enum domain)
  3877. {
  3878. struct ada_symbol_cache *sym_cache
  3879. = ada_get_symbol_cache (current_program_space);
  3880. int h = msymbol_hash (name) % HASH_SIZE;
  3881. struct cache_entry **e;
  3882. for (e = &sym_cache->root[h]; *e != NULL; e = &(*e)->next)
  3883. {
  3884. if (domain == (*e)->domain && strcmp (name, (*e)->name) == 0)
  3885. return e;
  3886. }
  3887. return NULL;
  3888. }
  3889. /* Search the symbol cache for an entry matching NAME and DOMAIN.
  3890. Return 1 if found, 0 otherwise.
  3891. If an entry was found and SYM is not NULL, set *SYM to the entry's
  3892. SYM. Same principle for BLOCK if not NULL. */
  3893. static int
  3894. lookup_cached_symbol (const char *name, domain_enum domain,
  3895. struct symbol **sym, const struct block **block)
  3896. {
  3897. struct cache_entry **e = find_entry (name, domain);
  3898. if (e == NULL)
  3899. return 0;
  3900. if (sym != NULL)
  3901. *sym = (*e)->sym;
  3902. if (block != NULL)
  3903. *block = (*e)->block;
  3904. return 1;
  3905. }
  3906. /* Assuming that (SYM, BLOCK) is the result of the lookup of NAME
  3907. in domain DOMAIN, save this result in our symbol cache. */
  3908. static void
  3909. cache_symbol (const char *name, domain_enum domain, struct symbol *sym,
  3910. const struct block *block)
  3911. {
  3912. struct ada_symbol_cache *sym_cache
  3913. = ada_get_symbol_cache (current_program_space);
  3914. int h;
  3915. struct cache_entry *e;
  3916. /* Symbols for builtin types don't have a block.
  3917. For now don't cache such symbols. */
  3918. if (sym != NULL && !sym->is_objfile_owned ())
  3919. return;
  3920. /* If the symbol is a local symbol, then do not cache it, as a search
  3921. for that symbol depends on the context. To determine whether
  3922. the symbol is local or not, we check the block where we found it
  3923. against the global and static blocks of its associated symtab. */
  3924. if (sym
  3925. && BLOCKVECTOR_BLOCK (symbol_symtab (sym)->compunit ()->blockvector (),
  3926. GLOBAL_BLOCK) != block
  3927. && BLOCKVECTOR_BLOCK (symbol_symtab (sym)->compunit ()->blockvector (),
  3928. STATIC_BLOCK) != block)
  3929. return;
  3930. h = msymbol_hash (name) % HASH_SIZE;
  3931. e = XOBNEW (&sym_cache->cache_space, cache_entry);
  3932. e->next = sym_cache->root[h];
  3933. sym_cache->root[h] = e;
  3934. e->name = obstack_strdup (&sym_cache->cache_space, name);
  3935. e->sym = sym;
  3936. e->domain = domain;
  3937. e->block = block;
  3938. }
  3939. /* Symbol Lookup */
  3940. /* Return the symbol name match type that should be used used when
  3941. searching for all symbols matching LOOKUP_NAME.
  3942. LOOKUP_NAME is expected to be a symbol name after transformation
  3943. for Ada lookups. */
  3944. static symbol_name_match_type
  3945. name_match_type_from_name (const char *lookup_name)
  3946. {
  3947. return (strstr (lookup_name, "__") == NULL
  3948. ? symbol_name_match_type::WILD
  3949. : symbol_name_match_type::FULL);
  3950. }
  3951. /* Return the result of a standard (literal, C-like) lookup of NAME in
  3952. given DOMAIN, visible from lexical block BLOCK. */
  3953. static struct symbol *
  3954. standard_lookup (const char *name, const struct block *block,
  3955. domain_enum domain)
  3956. {
  3957. /* Initialize it just to avoid a GCC false warning. */
  3958. struct block_symbol sym = {};
  3959. if (lookup_cached_symbol (name, domain, &sym.symbol, NULL))
  3960. return sym.symbol;
  3961. ada_lookup_encoded_symbol (name, block, domain, &sym);
  3962. cache_symbol (name, domain, sym.symbol, sym.block);
  3963. return sym.symbol;
  3964. }
  3965. /* Non-zero iff there is at least one non-function/non-enumeral symbol
  3966. in the symbol fields of SYMS. We treat enumerals as functions,
  3967. since they contend in overloading in the same way. */
  3968. static int
  3969. is_nonfunction (const std::vector<struct block_symbol> &syms)
  3970. {
  3971. for (const block_symbol &sym : syms)
  3972. if (sym.symbol->type ()->code () != TYPE_CODE_FUNC
  3973. && (sym.symbol->type ()->code () != TYPE_CODE_ENUM
  3974. || sym.symbol->aclass () != LOC_CONST))
  3975. return 1;
  3976. return 0;
  3977. }
  3978. /* If true (non-zero), then TYPE0 and TYPE1 represent equivalent
  3979. struct types. Otherwise, they may not. */
  3980. static int
  3981. equiv_types (struct type *type0, struct type *type1)
  3982. {
  3983. if (type0 == type1)
  3984. return 1;
  3985. if (type0 == NULL || type1 == NULL
  3986. || type0->code () != type1->code ())
  3987. return 0;
  3988. if ((type0->code () == TYPE_CODE_STRUCT
  3989. || type0->code () == TYPE_CODE_ENUM)
  3990. && ada_type_name (type0) != NULL && ada_type_name (type1) != NULL
  3991. && strcmp (ada_type_name (type0), ada_type_name (type1)) == 0)
  3992. return 1;
  3993. return 0;
  3994. }
  3995. /* True iff SYM0 represents the same entity as SYM1, or one that is
  3996. no more defined than that of SYM1. */
  3997. static int
  3998. lesseq_defined_than (struct symbol *sym0, struct symbol *sym1)
  3999. {
  4000. if (sym0 == sym1)
  4001. return 1;
  4002. if (sym0->domain () != sym1->domain ()
  4003. || sym0->aclass () != sym1->aclass ())
  4004. return 0;
  4005. switch (sym0->aclass ())
  4006. {
  4007. case LOC_UNDEF:
  4008. return 1;
  4009. case LOC_TYPEDEF:
  4010. {
  4011. struct type *type0 = sym0->type ();
  4012. struct type *type1 = sym1->type ();
  4013. const char *name0 = sym0->linkage_name ();
  4014. const char *name1 = sym1->linkage_name ();
  4015. int len0 = strlen (name0);
  4016. return
  4017. type0->code () == type1->code ()
  4018. && (equiv_types (type0, type1)
  4019. || (len0 < strlen (name1) && strncmp (name0, name1, len0) == 0
  4020. && startswith (name1 + len0, "___XV")));
  4021. }
  4022. case LOC_CONST:
  4023. return SYMBOL_VALUE (sym0) == SYMBOL_VALUE (sym1)
  4024. && equiv_types (sym0->type (), sym1->type ());
  4025. case LOC_STATIC:
  4026. {
  4027. const char *name0 = sym0->linkage_name ();
  4028. const char *name1 = sym1->linkage_name ();
  4029. return (strcmp (name0, name1) == 0
  4030. && SYMBOL_VALUE_ADDRESS (sym0) == SYMBOL_VALUE_ADDRESS (sym1));
  4031. }
  4032. default:
  4033. return 0;
  4034. }
  4035. }
  4036. /* Append (SYM,BLOCK) to the end of the array of struct block_symbol
  4037. records in RESULT. Do nothing if SYM is a duplicate. */
  4038. static void
  4039. add_defn_to_vec (std::vector<struct block_symbol> &result,
  4040. struct symbol *sym,
  4041. const struct block *block)
  4042. {
  4043. /* Do not try to complete stub types, as the debugger is probably
  4044. already scanning all symbols matching a certain name at the
  4045. time when this function is called. Trying to replace the stub
  4046. type by its associated full type will cause us to restart a scan
  4047. which may lead to an infinite recursion. Instead, the client
  4048. collecting the matching symbols will end up collecting several
  4049. matches, with at least one of them complete. It can then filter
  4050. out the stub ones if needed. */
  4051. for (int i = result.size () - 1; i >= 0; i -= 1)
  4052. {
  4053. if (lesseq_defined_than (sym, result[i].symbol))
  4054. return;
  4055. else if (lesseq_defined_than (result[i].symbol, sym))
  4056. {
  4057. result[i].symbol = sym;
  4058. result[i].block = block;
  4059. return;
  4060. }
  4061. }
  4062. struct block_symbol info;
  4063. info.symbol = sym;
  4064. info.block = block;
  4065. result.push_back (info);
  4066. }
  4067. /* Return a bound minimal symbol matching NAME according to Ada
  4068. decoding rules. Returns an invalid symbol if there is no such
  4069. minimal symbol. Names prefixed with "standard__" are handled
  4070. specially: "standard__" is first stripped off, and only static and
  4071. global symbols are searched. */
  4072. struct bound_minimal_symbol
  4073. ada_lookup_simple_minsym (const char *name)
  4074. {
  4075. struct bound_minimal_symbol result;
  4076. symbol_name_match_type match_type = name_match_type_from_name (name);
  4077. lookup_name_info lookup_name (name, match_type);
  4078. symbol_name_matcher_ftype *match_name
  4079. = ada_get_symbol_name_matcher (lookup_name);
  4080. for (objfile *objfile : current_program_space->objfiles ())
  4081. {
  4082. for (minimal_symbol *msymbol : objfile->msymbols ())
  4083. {
  4084. if (match_name (msymbol->linkage_name (), lookup_name, NULL)
  4085. && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
  4086. {
  4087. result.minsym = msymbol;
  4088. result.objfile = objfile;
  4089. break;
  4090. }
  4091. }
  4092. }
  4093. return result;
  4094. }
  4095. /* True if TYPE is definitely an artificial type supplied to a symbol
  4096. for which no debugging information was given in the symbol file. */
  4097. static int
  4098. is_nondebugging_type (struct type *type)
  4099. {
  4100. const char *name = ada_type_name (type);
  4101. return (name != NULL && strcmp (name, "<variable, no debug info>") == 0);
  4102. }
  4103. /* Return nonzero if TYPE1 and TYPE2 are two enumeration types
  4104. that are deemed "identical" for practical purposes.
  4105. This function assumes that TYPE1 and TYPE2 are both TYPE_CODE_ENUM
  4106. types and that their number of enumerals is identical (in other
  4107. words, type1->num_fields () == type2->num_fields ()). */
  4108. static int
  4109. ada_identical_enum_types_p (struct type *type1, struct type *type2)
  4110. {
  4111. int i;
  4112. /* The heuristic we use here is fairly conservative. We consider
  4113. that 2 enumerate types are identical if they have the same
  4114. number of enumerals and that all enumerals have the same
  4115. underlying value and name. */
  4116. /* All enums in the type should have an identical underlying value. */
  4117. for (i = 0; i < type1->num_fields (); i++)
  4118. if (type1->field (i).loc_enumval () != type2->field (i).loc_enumval ())
  4119. return 0;
  4120. /* All enumerals should also have the same name (modulo any numerical
  4121. suffix). */
  4122. for (i = 0; i < type1->num_fields (); i++)
  4123. {
  4124. const char *name_1 = type1->field (i).name ();
  4125. const char *name_2 = type2->field (i).name ();
  4126. int len_1 = strlen (name_1);
  4127. int len_2 = strlen (name_2);
  4128. ada_remove_trailing_digits (type1->field (i).name (), &len_1);
  4129. ada_remove_trailing_digits (type2->field (i).name (), &len_2);
  4130. if (len_1 != len_2
  4131. || strncmp (type1->field (i).name (),
  4132. type2->field (i).name (),
  4133. len_1) != 0)
  4134. return 0;
  4135. }
  4136. return 1;
  4137. }
  4138. /* Return nonzero if all the symbols in SYMS are all enumeral symbols
  4139. that are deemed "identical" for practical purposes. Sometimes,
  4140. enumerals are not strictly identical, but their types are so similar
  4141. that they can be considered identical.
  4142. For instance, consider the following code:
  4143. type Color is (Black, Red, Green, Blue, White);
  4144. type RGB_Color is new Color range Red .. Blue;
  4145. Type RGB_Color is a subrange of an implicit type which is a copy
  4146. of type Color. If we call that implicit type RGB_ColorB ("B" is
  4147. for "Base Type"), then type RGB_ColorB is a copy of type Color.
  4148. As a result, when an expression references any of the enumeral
  4149. by name (Eg. "print green"), the expression is technically
  4150. ambiguous and the user should be asked to disambiguate. But
  4151. doing so would only hinder the user, since it wouldn't matter
  4152. what choice he makes, the outcome would always be the same.
  4153. So, for practical purposes, we consider them as the same. */
  4154. static int
  4155. symbols_are_identical_enums (const std::vector<struct block_symbol> &syms)
  4156. {
  4157. int i;
  4158. /* Before performing a thorough comparison check of each type,
  4159. we perform a series of inexpensive checks. We expect that these
  4160. checks will quickly fail in the vast majority of cases, and thus
  4161. help prevent the unnecessary use of a more expensive comparison.
  4162. Said comparison also expects us to make some of these checks
  4163. (see ada_identical_enum_types_p). */
  4164. /* Quick check: All symbols should have an enum type. */
  4165. for (i = 0; i < syms.size (); i++)
  4166. if (syms[i].symbol->type ()->code () != TYPE_CODE_ENUM)
  4167. return 0;
  4168. /* Quick check: They should all have the same value. */
  4169. for (i = 1; i < syms.size (); i++)
  4170. if (SYMBOL_VALUE (syms[i].symbol) != SYMBOL_VALUE (syms[0].symbol))
  4171. return 0;
  4172. /* Quick check: They should all have the same number of enumerals. */
  4173. for (i = 1; i < syms.size (); i++)
  4174. if (syms[i].symbol->type ()->num_fields ()
  4175. != syms[0].symbol->type ()->num_fields ())
  4176. return 0;
  4177. /* All the sanity checks passed, so we might have a set of
  4178. identical enumeration types. Perform a more complete
  4179. comparison of the type of each symbol. */
  4180. for (i = 1; i < syms.size (); i++)
  4181. if (!ada_identical_enum_types_p (syms[i].symbol->type (),
  4182. syms[0].symbol->type ()))
  4183. return 0;
  4184. return 1;
  4185. }
  4186. /* Remove any non-debugging symbols in SYMS that definitely
  4187. duplicate other symbols in the list (The only case I know of where
  4188. this happens is when object files containing stabs-in-ecoff are
  4189. linked with files containing ordinary ecoff debugging symbols (or no
  4190. debugging symbols)). Modifies SYMS to squeeze out deleted entries. */
  4191. static void
  4192. remove_extra_symbols (std::vector<struct block_symbol> *syms)
  4193. {
  4194. int i, j;
  4195. /* We should never be called with less than 2 symbols, as there
  4196. cannot be any extra symbol in that case. But it's easy to
  4197. handle, since we have nothing to do in that case. */
  4198. if (syms->size () < 2)
  4199. return;
  4200. i = 0;
  4201. while (i < syms->size ())
  4202. {
  4203. int remove_p = 0;
  4204. /* If two symbols have the same name and one of them is a stub type,
  4205. the get rid of the stub. */
  4206. if ((*syms)[i].symbol->type ()->is_stub ()
  4207. && (*syms)[i].symbol->linkage_name () != NULL)
  4208. {
  4209. for (j = 0; j < syms->size (); j++)
  4210. {
  4211. if (j != i
  4212. && !(*syms)[j].symbol->type ()->is_stub ()
  4213. && (*syms)[j].symbol->linkage_name () != NULL
  4214. && strcmp ((*syms)[i].symbol->linkage_name (),
  4215. (*syms)[j].symbol->linkage_name ()) == 0)
  4216. remove_p = 1;
  4217. }
  4218. }
  4219. /* Two symbols with the same name, same class and same address
  4220. should be identical. */
  4221. else if ((*syms)[i].symbol->linkage_name () != NULL
  4222. && (*syms)[i].symbol->aclass () == LOC_STATIC
  4223. && is_nondebugging_type ((*syms)[i].symbol->type ()))
  4224. {
  4225. for (j = 0; j < syms->size (); j += 1)
  4226. {
  4227. if (i != j
  4228. && (*syms)[j].symbol->linkage_name () != NULL
  4229. && strcmp ((*syms)[i].symbol->linkage_name (),
  4230. (*syms)[j].symbol->linkage_name ()) == 0
  4231. && ((*syms)[i].symbol->aclass ()
  4232. == (*syms)[j].symbol->aclass ())
  4233. && SYMBOL_VALUE_ADDRESS ((*syms)[i].symbol)
  4234. == SYMBOL_VALUE_ADDRESS ((*syms)[j].symbol))
  4235. remove_p = 1;
  4236. }
  4237. }
  4238. if (remove_p)
  4239. syms->erase (syms->begin () + i);
  4240. else
  4241. i += 1;
  4242. }
  4243. /* If all the remaining symbols are identical enumerals, then
  4244. just keep the first one and discard the rest.
  4245. Unlike what we did previously, we do not discard any entry
  4246. unless they are ALL identical. This is because the symbol
  4247. comparison is not a strict comparison, but rather a practical
  4248. comparison. If all symbols are considered identical, then
  4249. we can just go ahead and use the first one and discard the rest.
  4250. But if we cannot reduce the list to a single element, we have
  4251. to ask the user to disambiguate anyways. And if we have to
  4252. present a multiple-choice menu, it's less confusing if the list
  4253. isn't missing some choices that were identical and yet distinct. */
  4254. if (symbols_are_identical_enums (*syms))
  4255. syms->resize (1);
  4256. }
  4257. /* Given a type that corresponds to a renaming entity, use the type name
  4258. to extract the scope (package name or function name, fully qualified,
  4259. and following the GNAT encoding convention) where this renaming has been
  4260. defined. */
  4261. static std::string
  4262. xget_renaming_scope (struct type *renaming_type)
  4263. {
  4264. /* The renaming types adhere to the following convention:
  4265. <scope>__<rename>___<XR extension>.
  4266. So, to extract the scope, we search for the "___XR" extension,
  4267. and then backtrack until we find the first "__". */
  4268. const char *name = renaming_type->name ();
  4269. const char *suffix = strstr (name, "___XR");
  4270. const char *last;
  4271. /* Now, backtrack a bit until we find the first "__". Start looking
  4272. at suffix - 3, as the <rename> part is at least one character long. */
  4273. for (last = suffix - 3; last > name; last--)
  4274. if (last[0] == '_' && last[1] == '_')
  4275. break;
  4276. /* Make a copy of scope and return it. */
  4277. return std::string (name, last);
  4278. }
  4279. /* Return nonzero if NAME corresponds to a package name. */
  4280. static int
  4281. is_package_name (const char *name)
  4282. {
  4283. /* Here, We take advantage of the fact that no symbols are generated
  4284. for packages, while symbols are generated for each function.
  4285. So the condition for NAME represent a package becomes equivalent
  4286. to NAME not existing in our list of symbols. There is only one
  4287. small complication with library-level functions (see below). */
  4288. /* If it is a function that has not been defined at library level,
  4289. then we should be able to look it up in the symbols. */
  4290. if (standard_lookup (name, NULL, VAR_DOMAIN) != NULL)
  4291. return 0;
  4292. /* Library-level function names start with "_ada_". See if function
  4293. "_ada_" followed by NAME can be found. */
  4294. /* Do a quick check that NAME does not contain "__", since library-level
  4295. functions names cannot contain "__" in them. */
  4296. if (strstr (name, "__") != NULL)
  4297. return 0;
  4298. std::string fun_name = string_printf ("_ada_%s", name);
  4299. return (standard_lookup (fun_name.c_str (), NULL, VAR_DOMAIN) == NULL);
  4300. }
  4301. /* Return nonzero if SYM corresponds to a renaming entity that is
  4302. not visible from FUNCTION_NAME. */
  4303. static int
  4304. old_renaming_is_invisible (const struct symbol *sym, const char *function_name)
  4305. {
  4306. if (sym->aclass () != LOC_TYPEDEF)
  4307. return 0;
  4308. std::string scope = xget_renaming_scope (sym->type ());
  4309. /* If the rename has been defined in a package, then it is visible. */
  4310. if (is_package_name (scope.c_str ()))
  4311. return 0;
  4312. /* Check that the rename is in the current function scope by checking
  4313. that its name starts with SCOPE. */
  4314. /* If the function name starts with "_ada_", it means that it is
  4315. a library-level function. Strip this prefix before doing the
  4316. comparison, as the encoding for the renaming does not contain
  4317. this prefix. */
  4318. if (startswith (function_name, "_ada_"))
  4319. function_name += 5;
  4320. return !startswith (function_name, scope.c_str ());
  4321. }
  4322. /* Remove entries from SYMS that corresponds to a renaming entity that
  4323. is not visible from the function associated with CURRENT_BLOCK or
  4324. that is superfluous due to the presence of more specific renaming
  4325. information. Places surviving symbols in the initial entries of
  4326. SYMS.
  4327. Rationale:
  4328. First, in cases where an object renaming is implemented as a
  4329. reference variable, GNAT may produce both the actual reference
  4330. variable and the renaming encoding. In this case, we discard the
  4331. latter.
  4332. Second, GNAT emits a type following a specified encoding for each renaming
  4333. entity. Unfortunately, STABS currently does not support the definition
  4334. of types that are local to a given lexical block, so all renamings types
  4335. are emitted at library level. As a consequence, if an application
  4336. contains two renaming entities using the same name, and a user tries to
  4337. print the value of one of these entities, the result of the ada symbol
  4338. lookup will also contain the wrong renaming type.
  4339. This function partially covers for this limitation by attempting to
  4340. remove from the SYMS list renaming symbols that should be visible
  4341. from CURRENT_BLOCK. However, there does not seem be a 100% reliable
  4342. method with the current information available. The implementation
  4343. below has a couple of limitations (FIXME: brobecker-2003-05-12):
  4344. - When the user tries to print a rename in a function while there
  4345. is another rename entity defined in a package: Normally, the
  4346. rename in the function has precedence over the rename in the
  4347. package, so the latter should be removed from the list. This is
  4348. currently not the case.
  4349. - This function will incorrectly remove valid renames if
  4350. the CURRENT_BLOCK corresponds to a function which symbol name
  4351. has been changed by an "Export" pragma. As a consequence,
  4352. the user will be unable to print such rename entities. */
  4353. static void
  4354. remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
  4355. const struct block *current_block)
  4356. {
  4357. struct symbol *current_function;
  4358. const char *current_function_name;
  4359. int i;
  4360. int is_new_style_renaming;
  4361. /* If there is both a renaming foo___XR... encoded as a variable and
  4362. a simple variable foo in the same block, discard the latter.
  4363. First, zero out such symbols, then compress. */
  4364. is_new_style_renaming = 0;
  4365. for (i = 0; i < syms->size (); i += 1)
  4366. {
  4367. struct symbol *sym = (*syms)[i].symbol;
  4368. const struct block *block = (*syms)[i].block;
  4369. const char *name;
  4370. const char *suffix;
  4371. if (sym == NULL || sym->aclass () == LOC_TYPEDEF)
  4372. continue;
  4373. name = sym->linkage_name ();
  4374. suffix = strstr (name, "___XR");
  4375. if (suffix != NULL)
  4376. {
  4377. int name_len = suffix - name;
  4378. int j;
  4379. is_new_style_renaming = 1;
  4380. for (j = 0; j < syms->size (); j += 1)
  4381. if (i != j && (*syms)[j].symbol != NULL
  4382. && strncmp (name, (*syms)[j].symbol->linkage_name (),
  4383. name_len) == 0
  4384. && block == (*syms)[j].block)
  4385. (*syms)[j].symbol = NULL;
  4386. }
  4387. }
  4388. if (is_new_style_renaming)
  4389. {
  4390. int j, k;
  4391. for (j = k = 0; j < syms->size (); j += 1)
  4392. if ((*syms)[j].symbol != NULL)
  4393. {
  4394. (*syms)[k] = (*syms)[j];
  4395. k += 1;
  4396. }
  4397. syms->resize (k);
  4398. return;
  4399. }
  4400. /* Extract the function name associated to CURRENT_BLOCK.
  4401. Abort if unable to do so. */
  4402. if (current_block == NULL)
  4403. return;
  4404. current_function = block_linkage_function (current_block);
  4405. if (current_function == NULL)
  4406. return;
  4407. current_function_name = current_function->linkage_name ();
  4408. if (current_function_name == NULL)
  4409. return;
  4410. /* Check each of the symbols, and remove it from the list if it is
  4411. a type corresponding to a renaming that is out of the scope of
  4412. the current block. */
  4413. i = 0;
  4414. while (i < syms->size ())
  4415. {
  4416. if (ada_parse_renaming ((*syms)[i].symbol, NULL, NULL, NULL)
  4417. == ADA_OBJECT_RENAMING
  4418. && old_renaming_is_invisible ((*syms)[i].symbol,
  4419. current_function_name))
  4420. syms->erase (syms->begin () + i);
  4421. else
  4422. i += 1;
  4423. }
  4424. }
  4425. /* Add to RESULT all symbols from BLOCK (and its super-blocks)
  4426. whose name and domain match LOOKUP_NAME and DOMAIN respectively.
  4427. Note: This function assumes that RESULT is empty. */
  4428. static void
  4429. ada_add_local_symbols (std::vector<struct block_symbol> &result,
  4430. const lookup_name_info &lookup_name,
  4431. const struct block *block, domain_enum domain)
  4432. {
  4433. while (block != NULL)
  4434. {
  4435. ada_add_block_symbols (result, block, lookup_name, domain, NULL);
  4436. /* If we found a non-function match, assume that's the one. We
  4437. only check this when finding a function boundary, so that we
  4438. can accumulate all results from intervening blocks first. */
  4439. if (BLOCK_FUNCTION (block) != nullptr && is_nonfunction (result))
  4440. return;
  4441. block = BLOCK_SUPERBLOCK (block);
  4442. }
  4443. }
  4444. /* An object of this type is used as the callback argument when
  4445. calling the map_matching_symbols method. */
  4446. struct match_data
  4447. {
  4448. explicit match_data (std::vector<struct block_symbol> *rp)
  4449. : resultp (rp)
  4450. {
  4451. }
  4452. DISABLE_COPY_AND_ASSIGN (match_data);
  4453. bool operator() (struct block_symbol *bsym);
  4454. struct objfile *objfile = nullptr;
  4455. std::vector<struct block_symbol> *resultp;
  4456. struct symbol *arg_sym = nullptr;
  4457. bool found_sym = false;
  4458. };
  4459. /* A callback for add_nonlocal_symbols that adds symbol, found in
  4460. BSYM, to a list of symbols. */
  4461. bool
  4462. match_data::operator() (struct block_symbol *bsym)
  4463. {
  4464. const struct block *block = bsym->block;
  4465. struct symbol *sym = bsym->symbol;
  4466. if (sym == NULL)
  4467. {
  4468. if (!found_sym && arg_sym != NULL)
  4469. add_defn_to_vec (*resultp,
  4470. fixup_symbol_section (arg_sym, objfile),
  4471. block);
  4472. found_sym = false;
  4473. arg_sym = NULL;
  4474. }
  4475. else
  4476. {
  4477. if (sym->aclass () == LOC_UNRESOLVED)
  4478. return true;
  4479. else if (sym->is_argument ())
  4480. arg_sym = sym;
  4481. else
  4482. {
  4483. found_sym = true;
  4484. add_defn_to_vec (*resultp,
  4485. fixup_symbol_section (sym, objfile),
  4486. block);
  4487. }
  4488. }
  4489. return true;
  4490. }
  4491. /* Helper for add_nonlocal_symbols. Find symbols in DOMAIN which are
  4492. targeted by renamings matching LOOKUP_NAME in BLOCK. Add these
  4493. symbols to RESULT. Return whether we found such symbols. */
  4494. static int
  4495. ada_add_block_renamings (std::vector<struct block_symbol> &result,
  4496. const struct block *block,
  4497. const lookup_name_info &lookup_name,
  4498. domain_enum domain)
  4499. {
  4500. struct using_direct *renaming;
  4501. int defns_mark = result.size ();
  4502. symbol_name_matcher_ftype *name_match
  4503. = ada_get_symbol_name_matcher (lookup_name);
  4504. for (renaming = block_using (block);
  4505. renaming != NULL;
  4506. renaming = renaming->next)
  4507. {
  4508. const char *r_name;
  4509. /* Avoid infinite recursions: skip this renaming if we are actually
  4510. already traversing it.
  4511. Currently, symbol lookup in Ada don't use the namespace machinery from
  4512. C++/Fortran support: skip namespace imports that use them. */
  4513. if (renaming->searched
  4514. || (renaming->import_src != NULL
  4515. && renaming->import_src[0] != '\0')
  4516. || (renaming->import_dest != NULL
  4517. && renaming->import_dest[0] != '\0'))
  4518. continue;
  4519. renaming->searched = 1;
  4520. /* TODO: here, we perform another name-based symbol lookup, which can
  4521. pull its own multiple overloads. In theory, we should be able to do
  4522. better in this case since, in DWARF, DW_AT_import is a DIE reference,
  4523. not a simple name. But in order to do this, we would need to enhance
  4524. the DWARF reader to associate a symbol to this renaming, instead of a
  4525. name. So, for now, we do something simpler: re-use the C++/Fortran
  4526. namespace machinery. */
  4527. r_name = (renaming->alias != NULL
  4528. ? renaming->alias
  4529. : renaming->declaration);
  4530. if (name_match (r_name, lookup_name, NULL))
  4531. {
  4532. lookup_name_info decl_lookup_name (renaming->declaration,
  4533. lookup_name.match_type ());
  4534. ada_add_all_symbols (result, block, decl_lookup_name, domain,
  4535. 1, NULL);
  4536. }
  4537. renaming->searched = 0;
  4538. }
  4539. return result.size () != defns_mark;
  4540. }
  4541. /* Implements compare_names, but only applying the comparision using
  4542. the given CASING. */
  4543. static int
  4544. compare_names_with_case (const char *string1, const char *string2,
  4545. enum case_sensitivity casing)
  4546. {
  4547. while (*string1 != '\0' && *string2 != '\0')
  4548. {
  4549. char c1, c2;
  4550. if (isspace (*string1) || isspace (*string2))
  4551. return strcmp_iw_ordered (string1, string2);
  4552. if (casing == case_sensitive_off)
  4553. {
  4554. c1 = tolower (*string1);
  4555. c2 = tolower (*string2);
  4556. }
  4557. else
  4558. {
  4559. c1 = *string1;
  4560. c2 = *string2;
  4561. }
  4562. if (c1 != c2)
  4563. break;
  4564. string1 += 1;
  4565. string2 += 1;
  4566. }
  4567. switch (*string1)
  4568. {
  4569. case '(':
  4570. return strcmp_iw_ordered (string1, string2);
  4571. case '_':
  4572. if (*string2 == '\0')
  4573. {
  4574. if (is_name_suffix (string1))
  4575. return 0;
  4576. else
  4577. return 1;
  4578. }
  4579. /* FALLTHROUGH */
  4580. default:
  4581. if (*string2 == '(')
  4582. return strcmp_iw_ordered (string1, string2);
  4583. else
  4584. {
  4585. if (casing == case_sensitive_off)
  4586. return tolower (*string1) - tolower (*string2);
  4587. else
  4588. return *string1 - *string2;
  4589. }
  4590. }
  4591. }
  4592. /* Compare STRING1 to STRING2, with results as for strcmp.
  4593. Compatible with strcmp_iw_ordered in that...
  4594. strcmp_iw_ordered (STRING1, STRING2) <= 0
  4595. ... implies...
  4596. compare_names (STRING1, STRING2) <= 0
  4597. (they may differ as to what symbols compare equal). */
  4598. static int
  4599. compare_names (const char *string1, const char *string2)
  4600. {
  4601. int result;
  4602. /* Similar to what strcmp_iw_ordered does, we need to perform
  4603. a case-insensitive comparison first, and only resort to
  4604. a second, case-sensitive, comparison if the first one was
  4605. not sufficient to differentiate the two strings. */
  4606. result = compare_names_with_case (string1, string2, case_sensitive_off);
  4607. if (result == 0)
  4608. result = compare_names_with_case (string1, string2, case_sensitive_on);
  4609. return result;
  4610. }
  4611. /* Convenience function to get at the Ada encoded lookup name for
  4612. LOOKUP_NAME, as a C string. */
  4613. static const char *
  4614. ada_lookup_name (const lookup_name_info &lookup_name)
  4615. {
  4616. return lookup_name.ada ().lookup_name ().c_str ();
  4617. }
  4618. /* A helper for add_nonlocal_symbols. Call expand_matching_symbols
  4619. for OBJFILE, then walk the objfile's symtabs and update the
  4620. results. */
  4621. static void
  4622. map_matching_symbols (struct objfile *objfile,
  4623. const lookup_name_info &lookup_name,
  4624. bool is_wild_match,
  4625. domain_enum domain,
  4626. int global,
  4627. match_data &data)
  4628. {
  4629. data.objfile = objfile;
  4630. objfile->expand_matching_symbols (lookup_name, domain, global,
  4631. is_wild_match ? nullptr : compare_names);
  4632. const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
  4633. for (compunit_symtab *symtab : objfile->compunits ())
  4634. {
  4635. const struct block *block
  4636. = BLOCKVECTOR_BLOCK (symtab->blockvector (), block_kind);
  4637. if (!iterate_over_symbols_terminated (block, lookup_name,
  4638. domain, data))
  4639. break;
  4640. }
  4641. }
  4642. /* Add to RESULT all non-local symbols whose name and domain match
  4643. LOOKUP_NAME and DOMAIN respectively. The search is performed on
  4644. GLOBAL_BLOCK symbols if GLOBAL is non-zero, or on STATIC_BLOCK
  4645. symbols otherwise. */
  4646. static void
  4647. add_nonlocal_symbols (std::vector<struct block_symbol> &result,
  4648. const lookup_name_info &lookup_name,
  4649. domain_enum domain, int global)
  4650. {
  4651. struct match_data data (&result);
  4652. bool is_wild_match = lookup_name.ada ().wild_match_p ();
  4653. for (objfile *objfile : current_program_space->objfiles ())
  4654. {
  4655. map_matching_symbols (objfile, lookup_name, is_wild_match, domain,
  4656. global, data);
  4657. for (compunit_symtab *cu : objfile->compunits ())
  4658. {
  4659. const struct block *global_block
  4660. = BLOCKVECTOR_BLOCK (cu->blockvector (), GLOBAL_BLOCK);
  4661. if (ada_add_block_renamings (result, global_block, lookup_name,
  4662. domain))
  4663. data.found_sym = true;
  4664. }
  4665. }
  4666. if (result.empty () && global && !is_wild_match)
  4667. {
  4668. const char *name = ada_lookup_name (lookup_name);
  4669. std::string bracket_name = std::string ("<_ada_") + name + '>';
  4670. lookup_name_info name1 (bracket_name, symbol_name_match_type::FULL);
  4671. for (objfile *objfile : current_program_space->objfiles ())
  4672. map_matching_symbols (objfile, name1, false, domain, global, data);
  4673. }
  4674. }
  4675. /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if
  4676. FULL_SEARCH is non-zero, enclosing scope and in global scopes,
  4677. returning the number of matches. Add these to RESULT.
  4678. When FULL_SEARCH is non-zero, any non-function/non-enumeral
  4679. symbol match within the nest of blocks whose innermost member is BLOCK,
  4680. is the one match returned (no other matches in that or
  4681. enclosing blocks is returned). If there are any matches in or
  4682. surrounding BLOCK, then these alone are returned.
  4683. Names prefixed with "standard__" are handled specially:
  4684. "standard__" is first stripped off (by the lookup_name
  4685. constructor), and only static and global symbols are searched.
  4686. If MADE_GLOBAL_LOOKUP_P is non-null, set it before return to whether we had
  4687. to lookup global symbols. */
  4688. static void
  4689. ada_add_all_symbols (std::vector<struct block_symbol> &result,
  4690. const struct block *block,
  4691. const lookup_name_info &lookup_name,
  4692. domain_enum domain,
  4693. int full_search,
  4694. int *made_global_lookup_p)
  4695. {
  4696. struct symbol *sym;
  4697. if (made_global_lookup_p)
  4698. *made_global_lookup_p = 0;
  4699. /* Special case: If the user specifies a symbol name inside package
  4700. Standard, do a non-wild matching of the symbol name without
  4701. the "standard__" prefix. This was primarily introduced in order
  4702. to allow the user to specifically access the standard exceptions
  4703. using, for instance, Standard.Constraint_Error when Constraint_Error
  4704. is ambiguous (due to the user defining its own Constraint_Error
  4705. entity inside its program). */
  4706. if (lookup_name.ada ().standard_p ())
  4707. block = NULL;
  4708. /* Check the non-global symbols. If we have ANY match, then we're done. */
  4709. if (block != NULL)
  4710. {
  4711. if (full_search)
  4712. ada_add_local_symbols (result, lookup_name, block, domain);
  4713. else
  4714. {
  4715. /* In the !full_search case we're are being called by
  4716. iterate_over_symbols, and we don't want to search
  4717. superblocks. */
  4718. ada_add_block_symbols (result, block, lookup_name, domain, NULL);
  4719. }
  4720. if (!result.empty () || !full_search)
  4721. return;
  4722. }
  4723. /* No non-global symbols found. Check our cache to see if we have
  4724. already performed this search before. If we have, then return
  4725. the same result. */
  4726. if (lookup_cached_symbol (ada_lookup_name (lookup_name),
  4727. domain, &sym, &block))
  4728. {
  4729. if (sym != NULL)
  4730. add_defn_to_vec (result, sym, block);
  4731. return;
  4732. }
  4733. if (made_global_lookup_p)
  4734. *made_global_lookup_p = 1;
  4735. /* Search symbols from all global blocks. */
  4736. add_nonlocal_symbols (result, lookup_name, domain, 1);
  4737. /* Now add symbols from all per-file blocks if we've gotten no hits
  4738. (not strictly correct, but perhaps better than an error). */
  4739. if (result.empty ())
  4740. add_nonlocal_symbols (result, lookup_name, domain, 0);
  4741. }
  4742. /* Find symbols in DOMAIN matching LOOKUP_NAME, in BLOCK and, if FULL_SEARCH
  4743. is non-zero, enclosing scope and in global scopes.
  4744. Returns (SYM,BLOCK) tuples, indicating the symbols found and the
  4745. blocks and symbol tables (if any) in which they were found.
  4746. When full_search is non-zero, any non-function/non-enumeral
  4747. symbol match within the nest of blocks whose innermost member is BLOCK,
  4748. is the one match returned (no other matches in that or
  4749. enclosing blocks is returned). If there are any matches in or
  4750. surrounding BLOCK, then these alone are returned.
  4751. Names prefixed with "standard__" are handled specially: "standard__"
  4752. is first stripped off, and only static and global symbols are searched. */
  4753. static std::vector<struct block_symbol>
  4754. ada_lookup_symbol_list_worker (const lookup_name_info &lookup_name,
  4755. const struct block *block,
  4756. domain_enum domain,
  4757. int full_search)
  4758. {
  4759. int syms_from_global_search;
  4760. std::vector<struct block_symbol> results;
  4761. ada_add_all_symbols (results, block, lookup_name,
  4762. domain, full_search, &syms_from_global_search);
  4763. remove_extra_symbols (&results);
  4764. if (results.empty () && full_search && syms_from_global_search)
  4765. cache_symbol (ada_lookup_name (lookup_name), domain, NULL, NULL);
  4766. if (results.size () == 1 && full_search && syms_from_global_search)
  4767. cache_symbol (ada_lookup_name (lookup_name), domain,
  4768. results[0].symbol, results[0].block);
  4769. remove_irrelevant_renamings (&results, block);
  4770. return results;
  4771. }
  4772. /* Find symbols in DOMAIN matching NAME, in BLOCK and enclosing scope and
  4773. in global scopes, returning (SYM,BLOCK) tuples.
  4774. See ada_lookup_symbol_list_worker for further details. */
  4775. std::vector<struct block_symbol>
  4776. ada_lookup_symbol_list (const char *name, const struct block *block,
  4777. domain_enum domain)
  4778. {
  4779. symbol_name_match_type name_match_type = name_match_type_from_name (name);
  4780. lookup_name_info lookup_name (name, name_match_type);
  4781. return ada_lookup_symbol_list_worker (lookup_name, block, domain, 1);
  4782. }
  4783. /* The result is as for ada_lookup_symbol_list with FULL_SEARCH set
  4784. to 1, but choosing the first symbol found if there are multiple
  4785. choices.
  4786. The result is stored in *INFO, which must be non-NULL.
  4787. If no match is found, INFO->SYM is set to NULL. */
  4788. void
  4789. ada_lookup_encoded_symbol (const char *name, const struct block *block,
  4790. domain_enum domain,
  4791. struct block_symbol *info)
  4792. {
  4793. /* Since we already have an encoded name, wrap it in '<>' to force a
  4794. verbatim match. Otherwise, if the name happens to not look like
  4795. an encoded name (because it doesn't include a "__"),
  4796. ada_lookup_name_info would re-encode/fold it again, and that
  4797. would e.g., incorrectly lowercase object renaming names like
  4798. "R28b" -> "r28b". */
  4799. std::string verbatim = add_angle_brackets (name);
  4800. gdb_assert (info != NULL);
  4801. *info = ada_lookup_symbol (verbatim.c_str (), block, domain);
  4802. }
  4803. /* Return a symbol in DOMAIN matching NAME, in BLOCK0 and enclosing
  4804. scope and in global scopes, or NULL if none. NAME is folded and
  4805. encoded first. Otherwise, the result is as for ada_lookup_symbol_list,
  4806. choosing the first symbol if there are multiple choices. */
  4807. struct block_symbol
  4808. ada_lookup_symbol (const char *name, const struct block *block0,
  4809. domain_enum domain)
  4810. {
  4811. std::vector<struct block_symbol> candidates
  4812. = ada_lookup_symbol_list (name, block0, domain);
  4813. if (candidates.empty ())
  4814. return {};
  4815. block_symbol info = candidates[0];
  4816. info.symbol = fixup_symbol_section (info.symbol, NULL);
  4817. return info;
  4818. }
  4819. /* True iff STR is a possible encoded suffix of a normal Ada name
  4820. that is to be ignored for matching purposes. Suffixes of parallel
  4821. names (e.g., XVE) are not included here. Currently, the possible suffixes
  4822. are given by any of the regular expressions:
  4823. [.$][0-9]+ [nested subprogram suffix, on platforms such as GNU/Linux]
  4824. ___[0-9]+ [nested subprogram suffix, on platforms such as HP/UX]
  4825. TKB [subprogram suffix for task bodies]
  4826. _E[0-9]+[bs]$ [protected object entry suffixes]
  4827. (X[nb]*)?((\$|__)[0-9](_?[0-9]+)|___(JM|LJM|X([FDBUP].*|R[^T]?)))?$
  4828. Also, any leading "__[0-9]+" sequence is skipped before the suffix
  4829. match is performed. This sequence is used to differentiate homonyms,
  4830. is an optional part of a valid name suffix. */
  4831. static int
  4832. is_name_suffix (const char *str)
  4833. {
  4834. int k;
  4835. const char *matching;
  4836. const int len = strlen (str);
  4837. /* Skip optional leading __[0-9]+. */
  4838. if (len > 3 && str[0] == '_' && str[1] == '_' && isdigit (str[2]))
  4839. {
  4840. str += 3;
  4841. while (isdigit (str[0]))
  4842. str += 1;
  4843. }
  4844. /* [.$][0-9]+ */
  4845. if (str[0] == '.' || str[0] == '$')
  4846. {
  4847. matching = str + 1;
  4848. while (isdigit (matching[0]))
  4849. matching += 1;
  4850. if (matching[0] == '\0')
  4851. return 1;
  4852. }
  4853. /* ___[0-9]+ */
  4854. if (len > 3 && str[0] == '_' && str[1] == '_' && str[2] == '_')
  4855. {
  4856. matching = str + 3;
  4857. while (isdigit (matching[0]))
  4858. matching += 1;
  4859. if (matching[0] == '\0')
  4860. return 1;
  4861. }
  4862. /* "TKB" suffixes are used for subprograms implementing task bodies. */
  4863. if (strcmp (str, "TKB") == 0)
  4864. return 1;
  4865. #if 0
  4866. /* FIXME: brobecker/2005-09-23: Protected Object subprograms end
  4867. with a N at the end. Unfortunately, the compiler uses the same
  4868. convention for other internal types it creates. So treating
  4869. all entity names that end with an "N" as a name suffix causes
  4870. some regressions. For instance, consider the case of an enumerated
  4871. type. To support the 'Image attribute, it creates an array whose
  4872. name ends with N.
  4873. Having a single character like this as a suffix carrying some
  4874. information is a bit risky. Perhaps we should change the encoding
  4875. to be something like "_N" instead. In the meantime, do not do
  4876. the following check. */
  4877. /* Protected Object Subprograms */
  4878. if (len == 1 && str [0] == 'N')
  4879. return 1;
  4880. #endif
  4881. /* _E[0-9]+[bs]$ */
  4882. if (len > 3 && str[0] == '_' && str [1] == 'E' && isdigit (str[2]))
  4883. {
  4884. matching = str + 3;
  4885. while (isdigit (matching[0]))
  4886. matching += 1;
  4887. if ((matching[0] == 'b' || matching[0] == 's')
  4888. && matching [1] == '\0')
  4889. return 1;
  4890. }
  4891. /* ??? We should not modify STR directly, as we are doing below. This
  4892. is fine in this case, but may become problematic later if we find
  4893. that this alternative did not work, and want to try matching
  4894. another one from the begining of STR. Since we modified it, we
  4895. won't be able to find the begining of the string anymore! */
  4896. if (str[0] == 'X')
  4897. {
  4898. str += 1;
  4899. while (str[0] != '_' && str[0] != '\0')
  4900. {
  4901. if (str[0] != 'n' && str[0] != 'b')
  4902. return 0;
  4903. str += 1;
  4904. }
  4905. }
  4906. if (str[0] == '\000')
  4907. return 1;
  4908. if (str[0] == '_')
  4909. {
  4910. if (str[1] != '_' || str[2] == '\000')
  4911. return 0;
  4912. if (str[2] == '_')
  4913. {
  4914. if (strcmp (str + 3, "JM") == 0)
  4915. return 1;
  4916. /* FIXME: brobecker/2004-09-30: GNAT will soon stop using
  4917. the LJM suffix in favor of the JM one. But we will
  4918. still accept LJM as a valid suffix for a reasonable
  4919. amount of time, just to allow ourselves to debug programs
  4920. compiled using an older version of GNAT. */
  4921. if (strcmp (str + 3, "LJM") == 0)
  4922. return 1;
  4923. if (str[3] != 'X')
  4924. return 0;
  4925. if (str[4] == 'F' || str[4] == 'D' || str[4] == 'B'
  4926. || str[4] == 'U' || str[4] == 'P')
  4927. return 1;
  4928. if (str[4] == 'R' && str[5] != 'T')
  4929. return 1;
  4930. return 0;
  4931. }
  4932. if (!isdigit (str[2]))
  4933. return 0;
  4934. for (k = 3; str[k] != '\0'; k += 1)
  4935. if (!isdigit (str[k]) && str[k] != '_')
  4936. return 0;
  4937. return 1;
  4938. }
  4939. if (str[0] == '$' && isdigit (str[1]))
  4940. {
  4941. for (k = 2; str[k] != '\0'; k += 1)
  4942. if (!isdigit (str[k]) && str[k] != '_')
  4943. return 0;
  4944. return 1;
  4945. }
  4946. return 0;
  4947. }
  4948. /* Return non-zero if the string starting at NAME and ending before
  4949. NAME_END contains no capital letters. */
  4950. static int
  4951. is_valid_name_for_wild_match (const char *name0)
  4952. {
  4953. std::string decoded_name = ada_decode (name0);
  4954. int i;
  4955. /* If the decoded name starts with an angle bracket, it means that
  4956. NAME0 does not follow the GNAT encoding format. It should then
  4957. not be allowed as a possible wild match. */
  4958. if (decoded_name[0] == '<')
  4959. return 0;
  4960. for (i=0; decoded_name[i] != '\0'; i++)
  4961. if (isalpha (decoded_name[i]) && !islower (decoded_name[i]))
  4962. return 0;
  4963. return 1;
  4964. }
  4965. /* Advance *NAMEP to next occurrence in the string NAME0 of the TARGET0
  4966. character which could start a simple name. Assumes that *NAMEP points
  4967. somewhere inside the string beginning at NAME0. */
  4968. static int
  4969. advance_wild_match (const char **namep, const char *name0, char target0)
  4970. {
  4971. const char *name = *namep;
  4972. while (1)
  4973. {
  4974. char t0, t1;
  4975. t0 = *name;
  4976. if (t0 == '_')
  4977. {
  4978. t1 = name[1];
  4979. if ((t1 >= 'a' && t1 <= 'z') || (t1 >= '0' && t1 <= '9'))
  4980. {
  4981. name += 1;
  4982. if (name == name0 + 5 && startswith (name0, "_ada"))
  4983. break;
  4984. else
  4985. name += 1;
  4986. }
  4987. else if (t1 == '_' && ((name[2] >= 'a' && name[2] <= 'z')
  4988. || name[2] == target0))
  4989. {
  4990. name += 2;
  4991. break;
  4992. }
  4993. else if (t1 == '_' && name[2] == 'B' && name[3] == '_')
  4994. {
  4995. /* Names like "pkg__B_N__name", where N is a number, are
  4996. block-local. We can handle these by simply skipping
  4997. the "B_" here. */
  4998. name += 4;
  4999. }
  5000. else
  5001. return 0;
  5002. }
  5003. else if ((t0 >= 'a' && t0 <= 'z') || (t0 >= '0' && t0 <= '9'))
  5004. name += 1;
  5005. else
  5006. return 0;
  5007. }
  5008. *namep = name;
  5009. return 1;
  5010. }
  5011. /* Return true iff NAME encodes a name of the form prefix.PATN.
  5012. Ignores any informational suffixes of NAME (i.e., for which
  5013. is_name_suffix is true). Assumes that PATN is a lower-cased Ada
  5014. simple name. */
  5015. static bool
  5016. wild_match (const char *name, const char *patn)
  5017. {
  5018. const char *p;
  5019. const char *name0 = name;
  5020. if (startswith (name, "___ghost_"))
  5021. name += 9;
  5022. while (1)
  5023. {
  5024. const char *match = name;
  5025. if (*name == *patn)
  5026. {
  5027. for (name += 1, p = patn + 1; *p != '\0'; name += 1, p += 1)
  5028. if (*p != *name)
  5029. break;
  5030. if (*p == '\0' && is_name_suffix (name))
  5031. return match == name0 || is_valid_name_for_wild_match (name0);
  5032. if (name[-1] == '_')
  5033. name -= 1;
  5034. }
  5035. if (!advance_wild_match (&name, name0, *patn))
  5036. return false;
  5037. }
  5038. }
  5039. /* Add symbols from BLOCK matching LOOKUP_NAME in DOMAIN to RESULT (if
  5040. necessary). OBJFILE is the section containing BLOCK. */
  5041. static void
  5042. ada_add_block_symbols (std::vector<struct block_symbol> &result,
  5043. const struct block *block,
  5044. const lookup_name_info &lookup_name,
  5045. domain_enum domain, struct objfile *objfile)
  5046. {
  5047. struct block_iterator iter;
  5048. /* A matching argument symbol, if any. */
  5049. struct symbol *arg_sym;
  5050. /* Set true when we find a matching non-argument symbol. */
  5051. bool found_sym;
  5052. struct symbol *sym;
  5053. arg_sym = NULL;
  5054. found_sym = false;
  5055. for (sym = block_iter_match_first (block, lookup_name, &iter);
  5056. sym != NULL;
  5057. sym = block_iter_match_next (lookup_name, &iter))
  5058. {
  5059. if (symbol_matches_domain (sym->language (), sym->domain (), domain))
  5060. {
  5061. if (sym->aclass () != LOC_UNRESOLVED)
  5062. {
  5063. if (sym->is_argument ())
  5064. arg_sym = sym;
  5065. else
  5066. {
  5067. found_sym = true;
  5068. add_defn_to_vec (result,
  5069. fixup_symbol_section (sym, objfile),
  5070. block);
  5071. }
  5072. }
  5073. }
  5074. }
  5075. /* Handle renamings. */
  5076. if (ada_add_block_renamings (result, block, lookup_name, domain))
  5077. found_sym = true;
  5078. if (!found_sym && arg_sym != NULL)
  5079. {
  5080. add_defn_to_vec (result,
  5081. fixup_symbol_section (arg_sym, objfile),
  5082. block);
  5083. }
  5084. if (!lookup_name.ada ().wild_match_p ())
  5085. {
  5086. arg_sym = NULL;
  5087. found_sym = false;
  5088. const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
  5089. const char *name = ada_lookup_name.c_str ();
  5090. size_t name_len = ada_lookup_name.size ();
  5091. ALL_BLOCK_SYMBOLS (block, iter, sym)
  5092. {
  5093. if (symbol_matches_domain (sym->language (),
  5094. sym->domain (), domain))
  5095. {
  5096. int cmp;
  5097. cmp = (int) '_' - (int) sym->linkage_name ()[0];
  5098. if (cmp == 0)
  5099. {
  5100. cmp = !startswith (sym->linkage_name (), "_ada_");
  5101. if (cmp == 0)
  5102. cmp = strncmp (name, sym->linkage_name () + 5,
  5103. name_len);
  5104. }
  5105. if (cmp == 0
  5106. && is_name_suffix (sym->linkage_name () + name_len + 5))
  5107. {
  5108. if (sym->aclass () != LOC_UNRESOLVED)
  5109. {
  5110. if (sym->is_argument ())
  5111. arg_sym = sym;
  5112. else
  5113. {
  5114. found_sym = true;
  5115. add_defn_to_vec (result,
  5116. fixup_symbol_section (sym, objfile),
  5117. block);
  5118. }
  5119. }
  5120. }
  5121. }
  5122. }
  5123. /* NOTE: This really shouldn't be needed for _ada_ symbols.
  5124. They aren't parameters, right? */
  5125. if (!found_sym && arg_sym != NULL)
  5126. {
  5127. add_defn_to_vec (result,
  5128. fixup_symbol_section (arg_sym, objfile),
  5129. block);
  5130. }
  5131. }
  5132. }
  5133. /* Symbol Completion */
  5134. /* See symtab.h. */
  5135. bool
  5136. ada_lookup_name_info::matches
  5137. (const char *sym_name,
  5138. symbol_name_match_type match_type,
  5139. completion_match_result *comp_match_res) const
  5140. {
  5141. bool match = false;
  5142. const char *text = m_encoded_name.c_str ();
  5143. size_t text_len = m_encoded_name.size ();
  5144. /* First, test against the fully qualified name of the symbol. */
  5145. if (strncmp (sym_name, text, text_len) == 0)
  5146. match = true;
  5147. std::string decoded_name = ada_decode (sym_name);
  5148. if (match && !m_encoded_p)
  5149. {
  5150. /* One needed check before declaring a positive match is to verify
  5151. that iff we are doing a verbatim match, the decoded version
  5152. of the symbol name starts with '<'. Otherwise, this symbol name
  5153. is not a suitable completion. */
  5154. bool has_angle_bracket = (decoded_name[0] == '<');
  5155. match = (has_angle_bracket == m_verbatim_p);
  5156. }
  5157. if (match && !m_verbatim_p)
  5158. {
  5159. /* When doing non-verbatim match, another check that needs to
  5160. be done is to verify that the potentially matching symbol name
  5161. does not include capital letters, because the ada-mode would
  5162. not be able to understand these symbol names without the
  5163. angle bracket notation. */
  5164. const char *tmp;
  5165. for (tmp = sym_name; *tmp != '\0' && !isupper (*tmp); tmp++);
  5166. if (*tmp != '\0')
  5167. match = false;
  5168. }
  5169. /* Second: Try wild matching... */
  5170. if (!match && m_wild_match_p)
  5171. {
  5172. /* Since we are doing wild matching, this means that TEXT
  5173. may represent an unqualified symbol name. We therefore must
  5174. also compare TEXT against the unqualified name of the symbol. */
  5175. sym_name = ada_unqualified_name (decoded_name.c_str ());
  5176. if (strncmp (sym_name, text, text_len) == 0)
  5177. match = true;
  5178. }
  5179. /* Finally: If we found a match, prepare the result to return. */
  5180. if (!match)
  5181. return false;
  5182. if (comp_match_res != NULL)
  5183. {
  5184. std::string &match_str = comp_match_res->match.storage ();
  5185. if (!m_encoded_p)
  5186. match_str = ada_decode (sym_name);
  5187. else
  5188. {
  5189. if (m_verbatim_p)
  5190. match_str = add_angle_brackets (sym_name);
  5191. else
  5192. match_str = sym_name;
  5193. }
  5194. comp_match_res->set_match (match_str.c_str ());
  5195. }
  5196. return true;
  5197. }
  5198. /* Field Access */
  5199. /* Return non-zero if TYPE is a pointer to the GNAT dispatch table used
  5200. for tagged types. */
  5201. static int
  5202. ada_is_dispatch_table_ptr_type (struct type *type)
  5203. {
  5204. const char *name;
  5205. if (type->code () != TYPE_CODE_PTR)
  5206. return 0;
  5207. name = TYPE_TARGET_TYPE (type)->name ();
  5208. if (name == NULL)
  5209. return 0;
  5210. return (strcmp (name, "ada__tags__dispatch_table") == 0);
  5211. }
  5212. /* Return non-zero if TYPE is an interface tag. */
  5213. static int
  5214. ada_is_interface_tag (struct type *type)
  5215. {
  5216. const char *name = type->name ();
  5217. if (name == NULL)
  5218. return 0;
  5219. return (strcmp (name, "ada__tags__interface_tag") == 0);
  5220. }
  5221. /* True if field number FIELD_NUM in struct or union type TYPE is supposed
  5222. to be invisible to users. */
  5223. int
  5224. ada_is_ignored_field (struct type *type, int field_num)
  5225. {
  5226. if (field_num < 0 || field_num > type->num_fields ())
  5227. return 1;
  5228. /* Check the name of that field. */
  5229. {
  5230. const char *name = type->field (field_num).name ();
  5231. /* Anonymous field names should not be printed.
  5232. brobecker/2007-02-20: I don't think this can actually happen
  5233. but we don't want to print the value of anonymous fields anyway. */
  5234. if (name == NULL)
  5235. return 1;
  5236. /* Normally, fields whose name start with an underscore ("_")
  5237. are fields that have been internally generated by the compiler,
  5238. and thus should not be printed. The "_parent" field is special,
  5239. however: This is a field internally generated by the compiler
  5240. for tagged types, and it contains the components inherited from
  5241. the parent type. This field should not be printed as is, but
  5242. should not be ignored either. */
  5243. if (name[0] == '_' && !startswith (name, "_parent"))
  5244. return 1;
  5245. /* The compiler doesn't document this, but sometimes it emits
  5246. a field whose name starts with a capital letter, like 'V148s'.
  5247. These aren't marked as artificial in any way, but we know they
  5248. should be ignored. However, wrapper fields should not be
  5249. ignored. */
  5250. if (name[0] == 'S' || name[0] == 'R' || name[0] == 'O')
  5251. {
  5252. /* Wrapper field. */
  5253. }
  5254. else if (isupper (name[0]))
  5255. return 1;
  5256. }
  5257. /* If this is the dispatch table of a tagged type or an interface tag,
  5258. then ignore. */
  5259. if (ada_is_tagged_type (type, 1)
  5260. && (ada_is_dispatch_table_ptr_type (type->field (field_num).type ())
  5261. || ada_is_interface_tag (type->field (field_num).type ())))
  5262. return 1;
  5263. /* Not a special field, so it should not be ignored. */
  5264. return 0;
  5265. }
  5266. /* True iff TYPE has a tag field. If REFOK, then TYPE may also be a
  5267. pointer or reference type whose ultimate target has a tag field. */
  5268. int
  5269. ada_is_tagged_type (struct type *type, int refok)
  5270. {
  5271. return (ada_lookup_struct_elt_type (type, "_tag", refok, 1) != NULL);
  5272. }
  5273. /* True iff TYPE represents the type of X'Tag */
  5274. int
  5275. ada_is_tag_type (struct type *type)
  5276. {
  5277. type = ada_check_typedef (type);
  5278. if (type == NULL || type->code () != TYPE_CODE_PTR)
  5279. return 0;
  5280. else
  5281. {
  5282. const char *name = ada_type_name (TYPE_TARGET_TYPE (type));
  5283. return (name != NULL
  5284. && strcmp (name, "ada__tags__dispatch_table") == 0);
  5285. }
  5286. }
  5287. /* The type of the tag on VAL. */
  5288. static struct type *
  5289. ada_tag_type (struct value *val)
  5290. {
  5291. return ada_lookup_struct_elt_type (value_type (val), "_tag", 1, 0);
  5292. }
  5293. /* Return 1 if TAG follows the old scheme for Ada tags (used for Ada 95,
  5294. retired at Ada 05). */
  5295. static int
  5296. is_ada95_tag (struct value *tag)
  5297. {
  5298. return ada_value_struct_elt (tag, "tsd", 1) != NULL;
  5299. }
  5300. /* The value of the tag on VAL. */
  5301. static struct value *
  5302. ada_value_tag (struct value *val)
  5303. {
  5304. return ada_value_struct_elt (val, "_tag", 0);
  5305. }
  5306. /* The value of the tag on the object of type TYPE whose contents are
  5307. saved at VALADDR, if it is non-null, or is at memory address
  5308. ADDRESS. */
  5309. static struct value *
  5310. value_tag_from_contents_and_address (struct type *type,
  5311. const gdb_byte *valaddr,
  5312. CORE_ADDR address)
  5313. {
  5314. int tag_byte_offset;
  5315. struct type *tag_type;
  5316. gdb::array_view<const gdb_byte> contents;
  5317. if (valaddr != nullptr)
  5318. contents = gdb::make_array_view (valaddr, TYPE_LENGTH (type));
  5319. struct type *resolved_type = resolve_dynamic_type (type, contents, address);
  5320. if (find_struct_field ("_tag", resolved_type, 0, &tag_type, &tag_byte_offset,
  5321. NULL, NULL, NULL))
  5322. {
  5323. const gdb_byte *valaddr1 = ((valaddr == NULL)
  5324. ? NULL
  5325. : valaddr + tag_byte_offset);
  5326. CORE_ADDR address1 = (address == 0) ? 0 : address + tag_byte_offset;
  5327. return value_from_contents_and_address (tag_type, valaddr1, address1);
  5328. }
  5329. return NULL;
  5330. }
  5331. static struct type *
  5332. type_from_tag (struct value *tag)
  5333. {
  5334. gdb::unique_xmalloc_ptr<char> type_name = ada_tag_name (tag);
  5335. if (type_name != NULL)
  5336. return ada_find_any_type (ada_encode (type_name.get ()).c_str ());
  5337. return NULL;
  5338. }
  5339. /* Given a value OBJ of a tagged type, return a value of this
  5340. type at the base address of the object. The base address, as
  5341. defined in Ada.Tags, it is the address of the primary tag of
  5342. the object, and therefore where the field values of its full
  5343. view can be fetched. */
  5344. struct value *
  5345. ada_tag_value_at_base_address (struct value *obj)
  5346. {
  5347. struct value *val;
  5348. LONGEST offset_to_top = 0;
  5349. struct type *ptr_type, *obj_type;
  5350. struct value *tag;
  5351. CORE_ADDR base_address;
  5352. obj_type = value_type (obj);
  5353. /* It is the responsability of the caller to deref pointers. */
  5354. if (obj_type->code () == TYPE_CODE_PTR || obj_type->code () == TYPE_CODE_REF)
  5355. return obj;
  5356. tag = ada_value_tag (obj);
  5357. if (!tag)
  5358. return obj;
  5359. /* Base addresses only appeared with Ada 05 and multiple inheritance. */
  5360. if (is_ada95_tag (tag))
  5361. return obj;
  5362. struct type *offset_type
  5363. = language_lookup_primitive_type (language_def (language_ada),
  5364. target_gdbarch(), "storage_offset");
  5365. ptr_type = lookup_pointer_type (offset_type);
  5366. val = value_cast (ptr_type, tag);
  5367. if (!val)
  5368. return obj;
  5369. /* It is perfectly possible that an exception be raised while
  5370. trying to determine the base address, just like for the tag;
  5371. see ada_tag_name for more details. We do not print the error
  5372. message for the same reason. */
  5373. try
  5374. {
  5375. offset_to_top = value_as_long (value_ind (value_ptradd (val, -2)));
  5376. }
  5377. catch (const gdb_exception_error &e)
  5378. {
  5379. return obj;
  5380. }
  5381. /* If offset is null, nothing to do. */
  5382. if (offset_to_top == 0)
  5383. return obj;
  5384. /* -1 is a special case in Ada.Tags; however, what should be done
  5385. is not quite clear from the documentation. So do nothing for
  5386. now. */
  5387. if (offset_to_top == -1)
  5388. return obj;
  5389. /* Storage_Offset'Last is used to indicate that a dynamic offset to
  5390. top is used. In this situation the offset is stored just after
  5391. the tag, in the object itself. */
  5392. ULONGEST last = (((ULONGEST) 1) << (8 * TYPE_LENGTH (offset_type) - 1)) - 1;
  5393. if (offset_to_top == last)
  5394. {
  5395. struct value *tem = value_addr (tag);
  5396. tem = value_ptradd (tem, 1);
  5397. tem = value_cast (ptr_type, tem);
  5398. offset_to_top = value_as_long (value_ind (tem));
  5399. }
  5400. else if (offset_to_top > 0)
  5401. {
  5402. /* OFFSET_TO_TOP used to be a positive value to be subtracted
  5403. from the base address. This was however incompatible with
  5404. C++ dispatch table: C++ uses a *negative* value to *add*
  5405. to the base address. Ada's convention has therefore been
  5406. changed in GNAT 19.0w 20171023: since then, C++ and Ada
  5407. use the same convention. Here, we support both cases by
  5408. checking the sign of OFFSET_TO_TOP. */
  5409. offset_to_top = -offset_to_top;
  5410. }
  5411. base_address = value_address (obj) + offset_to_top;
  5412. tag = value_tag_from_contents_and_address (obj_type, NULL, base_address);
  5413. /* Make sure that we have a proper tag at the new address.
  5414. Otherwise, offset_to_top is bogus (which can happen when
  5415. the object is not initialized yet). */
  5416. if (!tag)
  5417. return obj;
  5418. obj_type = type_from_tag (tag);
  5419. if (!obj_type)
  5420. return obj;
  5421. return value_from_contents_and_address (obj_type, NULL, base_address);
  5422. }
  5423. /* Return the "ada__tags__type_specific_data" type. */
  5424. static struct type *
  5425. ada_get_tsd_type (struct inferior *inf)
  5426. {
  5427. struct ada_inferior_data *data = get_ada_inferior_data (inf);
  5428. if (data->tsd_type == 0)
  5429. data->tsd_type = ada_find_any_type ("ada__tags__type_specific_data");
  5430. return data->tsd_type;
  5431. }
  5432. /* Return the TSD (type-specific data) associated to the given TAG.
  5433. TAG is assumed to be the tag of a tagged-type entity.
  5434. May return NULL if we are unable to get the TSD. */
  5435. static struct value *
  5436. ada_get_tsd_from_tag (struct value *tag)
  5437. {
  5438. struct value *val;
  5439. struct type *type;
  5440. /* First option: The TSD is simply stored as a field of our TAG.
  5441. Only older versions of GNAT would use this format, but we have
  5442. to test it first, because there are no visible markers for
  5443. the current approach except the absence of that field. */
  5444. val = ada_value_struct_elt (tag, "tsd", 1);
  5445. if (val)
  5446. return val;
  5447. /* Try the second representation for the dispatch table (in which
  5448. there is no explicit 'tsd' field in the referent of the tag pointer,
  5449. and instead the tsd pointer is stored just before the dispatch
  5450. table. */
  5451. type = ada_get_tsd_type (current_inferior());
  5452. if (type == NULL)
  5453. return NULL;
  5454. type = lookup_pointer_type (lookup_pointer_type (type));
  5455. val = value_cast (type, tag);
  5456. if (val == NULL)
  5457. return NULL;
  5458. return value_ind (value_ptradd (val, -1));
  5459. }
  5460. /* Given the TSD of a tag (type-specific data), return a string
  5461. containing the name of the associated type.
  5462. May return NULL if we are unable to determine the tag name. */
  5463. static gdb::unique_xmalloc_ptr<char>
  5464. ada_tag_name_from_tsd (struct value *tsd)
  5465. {
  5466. struct value *val;
  5467. val = ada_value_struct_elt (tsd, "expanded_name", 1);
  5468. if (val == NULL)
  5469. return NULL;
  5470. gdb::unique_xmalloc_ptr<char> buffer
  5471. = target_read_string (value_as_address (val), INT_MAX);
  5472. if (buffer == nullptr)
  5473. return nullptr;
  5474. try
  5475. {
  5476. /* Let this throw an exception on error. If the data is
  5477. uninitialized, we'd rather not have the user see a
  5478. warning. */
  5479. const char *folded = ada_fold_name (buffer.get (), true);
  5480. return make_unique_xstrdup (folded);
  5481. }
  5482. catch (const gdb_exception &)
  5483. {
  5484. return nullptr;
  5485. }
  5486. }
  5487. /* The type name of the dynamic type denoted by the 'tag value TAG, as
  5488. a C string.
  5489. Return NULL if the TAG is not an Ada tag, or if we were unable to
  5490. determine the name of that tag. */
  5491. gdb::unique_xmalloc_ptr<char>
  5492. ada_tag_name (struct value *tag)
  5493. {
  5494. gdb::unique_xmalloc_ptr<char> name;
  5495. if (!ada_is_tag_type (value_type (tag)))
  5496. return NULL;
  5497. /* It is perfectly possible that an exception be raised while trying
  5498. to determine the TAG's name, even under normal circumstances:
  5499. The associated variable may be uninitialized or corrupted, for
  5500. instance. We do not let any exception propagate past this point.
  5501. instead we return NULL.
  5502. We also do not print the error message either (which often is very
  5503. low-level (Eg: "Cannot read memory at 0x[...]"), but instead let
  5504. the caller print a more meaningful message if necessary. */
  5505. try
  5506. {
  5507. struct value *tsd = ada_get_tsd_from_tag (tag);
  5508. if (tsd != NULL)
  5509. name = ada_tag_name_from_tsd (tsd);
  5510. }
  5511. catch (const gdb_exception_error &e)
  5512. {
  5513. }
  5514. return name;
  5515. }
  5516. /* The parent type of TYPE, or NULL if none. */
  5517. struct type *
  5518. ada_parent_type (struct type *type)
  5519. {
  5520. int i;
  5521. type = ada_check_typedef (type);
  5522. if (type == NULL || type->code () != TYPE_CODE_STRUCT)
  5523. return NULL;
  5524. for (i = 0; i < type->num_fields (); i += 1)
  5525. if (ada_is_parent_field (type, i))
  5526. {
  5527. struct type *parent_type = type->field (i).type ();
  5528. /* If the _parent field is a pointer, then dereference it. */
  5529. if (parent_type->code () == TYPE_CODE_PTR)
  5530. parent_type = TYPE_TARGET_TYPE (parent_type);
  5531. /* If there is a parallel XVS type, get the actual base type. */
  5532. parent_type = ada_get_base_type (parent_type);
  5533. return ada_check_typedef (parent_type);
  5534. }
  5535. return NULL;
  5536. }
  5537. /* True iff field number FIELD_NUM of structure type TYPE contains the
  5538. parent-type (inherited) fields of a derived type. Assumes TYPE is
  5539. a structure type with at least FIELD_NUM+1 fields. */
  5540. int
  5541. ada_is_parent_field (struct type *type, int field_num)
  5542. {
  5543. const char *name = ada_check_typedef (type)->field (field_num).name ();
  5544. return (name != NULL
  5545. && (startswith (name, "PARENT")
  5546. || startswith (name, "_parent")));
  5547. }
  5548. /* True iff field number FIELD_NUM of structure type TYPE is a
  5549. transparent wrapper field (which should be silently traversed when doing
  5550. field selection and flattened when printing). Assumes TYPE is a
  5551. structure type with at least FIELD_NUM+1 fields. Such fields are always
  5552. structures. */
  5553. int
  5554. ada_is_wrapper_field (struct type *type, int field_num)
  5555. {
  5556. const char *name = type->field (field_num).name ();
  5557. if (name != NULL && strcmp (name, "RETVAL") == 0)
  5558. {
  5559. /* This happens in functions with "out" or "in out" parameters
  5560. which are passed by copy. For such functions, GNAT describes
  5561. the function's return type as being a struct where the return
  5562. value is in a field called RETVAL, and where the other "out"
  5563. or "in out" parameters are fields of that struct. This is not
  5564. a wrapper. */
  5565. return 0;
  5566. }
  5567. return (name != NULL
  5568. && (startswith (name, "PARENT")
  5569. || strcmp (name, "REP") == 0
  5570. || startswith (name, "_parent")
  5571. || name[0] == 'S' || name[0] == 'R' || name[0] == 'O'));
  5572. }
  5573. /* True iff field number FIELD_NUM of structure or union type TYPE
  5574. is a variant wrapper. Assumes TYPE is a structure type with at least
  5575. FIELD_NUM+1 fields. */
  5576. int
  5577. ada_is_variant_part (struct type *type, int field_num)
  5578. {
  5579. /* Only Ada types are eligible. */
  5580. if (!ADA_TYPE_P (type))
  5581. return 0;
  5582. struct type *field_type = type->field (field_num).type ();
  5583. return (field_type->code () == TYPE_CODE_UNION
  5584. || (is_dynamic_field (type, field_num)
  5585. && (TYPE_TARGET_TYPE (field_type)->code ()
  5586. == TYPE_CODE_UNION)));
  5587. }
  5588. /* Assuming that VAR_TYPE is a variant wrapper (type of the variant part)
  5589. whose discriminants are contained in the record type OUTER_TYPE,
  5590. returns the type of the controlling discriminant for the variant.
  5591. May return NULL if the type could not be found. */
  5592. struct type *
  5593. ada_variant_discrim_type (struct type *var_type, struct type *outer_type)
  5594. {
  5595. const char *name = ada_variant_discrim_name (var_type);
  5596. return ada_lookup_struct_elt_type (outer_type, name, 1, 1);
  5597. }
  5598. /* Assuming that TYPE is the type of a variant wrapper, and FIELD_NUM is a
  5599. valid field number within it, returns 1 iff field FIELD_NUM of TYPE
  5600. represents a 'when others' clause; otherwise 0. */
  5601. static int
  5602. ada_is_others_clause (struct type *type, int field_num)
  5603. {
  5604. const char *name = type->field (field_num).name ();
  5605. return (name != NULL && name[0] == 'O');
  5606. }
  5607. /* Assuming that TYPE0 is the type of the variant part of a record,
  5608. returns the name of the discriminant controlling the variant.
  5609. The value is valid until the next call to ada_variant_discrim_name. */
  5610. const char *
  5611. ada_variant_discrim_name (struct type *type0)
  5612. {
  5613. static std::string result;
  5614. struct type *type;
  5615. const char *name;
  5616. const char *discrim_end;
  5617. const char *discrim_start;
  5618. if (type0->code () == TYPE_CODE_PTR)
  5619. type = TYPE_TARGET_TYPE (type0);
  5620. else
  5621. type = type0;
  5622. name = ada_type_name (type);
  5623. if (name == NULL || name[0] == '\000')
  5624. return "";
  5625. for (discrim_end = name + strlen (name) - 6; discrim_end != name;
  5626. discrim_end -= 1)
  5627. {
  5628. if (startswith (discrim_end, "___XVN"))
  5629. break;
  5630. }
  5631. if (discrim_end == name)
  5632. return "";
  5633. for (discrim_start = discrim_end; discrim_start != name + 3;
  5634. discrim_start -= 1)
  5635. {
  5636. if (discrim_start == name + 1)
  5637. return "";
  5638. if ((discrim_start > name + 3
  5639. && startswith (discrim_start - 3, "___"))
  5640. || discrim_start[-1] == '.')
  5641. break;
  5642. }
  5643. result = std::string (discrim_start, discrim_end - discrim_start);
  5644. return result.c_str ();
  5645. }
  5646. /* Scan STR for a subtype-encoded number, beginning at position K.
  5647. Put the position of the character just past the number scanned in
  5648. *NEW_K, if NEW_K!=NULL. Put the scanned number in *R, if R!=NULL.
  5649. Return 1 if there was a valid number at the given position, and 0
  5650. otherwise. A "subtype-encoded" number consists of the absolute value
  5651. in decimal, followed by the letter 'm' to indicate a negative number.
  5652. Assumes 0m does not occur. */
  5653. int
  5654. ada_scan_number (const char str[], int k, LONGEST * R, int *new_k)
  5655. {
  5656. ULONGEST RU;
  5657. if (!isdigit (str[k]))
  5658. return 0;
  5659. /* Do it the hard way so as not to make any assumption about
  5660. the relationship of unsigned long (%lu scan format code) and
  5661. LONGEST. */
  5662. RU = 0;
  5663. while (isdigit (str[k]))
  5664. {
  5665. RU = RU * 10 + (str[k] - '0');
  5666. k += 1;
  5667. }
  5668. if (str[k] == 'm')
  5669. {
  5670. if (R != NULL)
  5671. *R = (-(LONGEST) (RU - 1)) - 1;
  5672. k += 1;
  5673. }
  5674. else if (R != NULL)
  5675. *R = (LONGEST) RU;
  5676. /* NOTE on the above: Technically, C does not say what the results of
  5677. - (LONGEST) RU or (LONGEST) -RU are for RU == largest positive
  5678. number representable as a LONGEST (although either would probably work
  5679. in most implementations). When RU>0, the locution in the then branch
  5680. above is always equivalent to the negative of RU. */
  5681. if (new_k != NULL)
  5682. *new_k = k;
  5683. return 1;
  5684. }
  5685. /* Assuming that TYPE is a variant part wrapper type (a VARIANTS field),
  5686. and FIELD_NUM is a valid field number within it, returns 1 iff VAL is
  5687. in the range encoded by field FIELD_NUM of TYPE; otherwise 0. */
  5688. static int
  5689. ada_in_variant (LONGEST val, struct type *type, int field_num)
  5690. {
  5691. const char *name = type->field (field_num).name ();
  5692. int p;
  5693. p = 0;
  5694. while (1)
  5695. {
  5696. switch (name[p])
  5697. {
  5698. case '\0':
  5699. return 0;
  5700. case 'S':
  5701. {
  5702. LONGEST W;
  5703. if (!ada_scan_number (name, p + 1, &W, &p))
  5704. return 0;
  5705. if (val == W)
  5706. return 1;
  5707. break;
  5708. }
  5709. case 'R':
  5710. {
  5711. LONGEST L, U;
  5712. if (!ada_scan_number (name, p + 1, &L, &p)
  5713. || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
  5714. return 0;
  5715. if (val >= L && val <= U)
  5716. return 1;
  5717. break;
  5718. }
  5719. case 'O':
  5720. return 1;
  5721. default:
  5722. return 0;
  5723. }
  5724. }
  5725. }
  5726. /* FIXME: Lots of redundancy below. Try to consolidate. */
  5727. /* Given a value ARG1 (offset by OFFSET bytes) of a struct or union type
  5728. ARG_TYPE, extract and return the value of one of its (non-static)
  5729. fields. FIELDNO says which field. Differs from value_primitive_field
  5730. only in that it can handle packed values of arbitrary type. */
  5731. struct value *
  5732. ada_value_primitive_field (struct value *arg1, int offset, int fieldno,
  5733. struct type *arg_type)
  5734. {
  5735. struct type *type;
  5736. arg_type = ada_check_typedef (arg_type);
  5737. type = arg_type->field (fieldno).type ();
  5738. /* Handle packed fields. It might be that the field is not packed
  5739. relative to its containing structure, but the structure itself is
  5740. packed; in this case we must take the bit-field path. */
  5741. if (TYPE_FIELD_BITSIZE (arg_type, fieldno) != 0 || value_bitpos (arg1) != 0)
  5742. {
  5743. int bit_pos = arg_type->field (fieldno).loc_bitpos ();
  5744. int bit_size = TYPE_FIELD_BITSIZE (arg_type, fieldno);
  5745. return ada_value_primitive_packed_val (arg1,
  5746. value_contents (arg1).data (),
  5747. offset + bit_pos / 8,
  5748. bit_pos % 8, bit_size, type);
  5749. }
  5750. else
  5751. return value_primitive_field (arg1, offset, fieldno, arg_type);
  5752. }
  5753. /* Find field with name NAME in object of type TYPE. If found,
  5754. set the following for each argument that is non-null:
  5755. - *FIELD_TYPE_P to the field's type;
  5756. - *BYTE_OFFSET_P to OFFSET + the byte offset of the field within
  5757. an object of that type;
  5758. - *BIT_OFFSET_P to the bit offset modulo byte size of the field;
  5759. - *BIT_SIZE_P to its size in bits if the field is packed, and
  5760. 0 otherwise;
  5761. If INDEX_P is non-null, increment *INDEX_P by the number of source-visible
  5762. fields up to but not including the desired field, or by the total
  5763. number of fields if not found. A NULL value of NAME never
  5764. matches; the function just counts visible fields in this case.
  5765. Notice that we need to handle when a tagged record hierarchy
  5766. has some components with the same name, like in this scenario:
  5767. type Top_T is tagged record
  5768. N : Integer := 1;
  5769. U : Integer := 974;
  5770. A : Integer := 48;
  5771. end record;
  5772. type Middle_T is new Top.Top_T with record
  5773. N : Character := 'a';
  5774. C : Integer := 3;
  5775. end record;
  5776. type Bottom_T is new Middle.Middle_T with record
  5777. N : Float := 4.0;
  5778. C : Character := '5';
  5779. X : Integer := 6;
  5780. A : Character := 'J';
  5781. end record;
  5782. Let's say we now have a variable declared and initialized as follow:
  5783. TC : Top_A := new Bottom_T;
  5784. And then we use this variable to call this function
  5785. procedure Assign (Obj: in out Top_T; TV : Integer);
  5786. as follow:
  5787. Assign (Top_T (B), 12);
  5788. Now, we're in the debugger, and we're inside that procedure
  5789. then and we want to print the value of obj.c:
  5790. Usually, the tagged record or one of the parent type owns the
  5791. component to print and there's no issue but in this particular
  5792. case, what does it mean to ask for Obj.C? Since the actual
  5793. type for object is type Bottom_T, it could mean two things: type
  5794. component C from the Middle_T view, but also component C from
  5795. Bottom_T. So in that "undefined" case, when the component is
  5796. not found in the non-resolved type (which includes all the
  5797. components of the parent type), then resolve it and see if we
  5798. get better luck once expanded.
  5799. In the case of homonyms in the derived tagged type, we don't
  5800. guaranty anything, and pick the one that's easiest for us
  5801. to program.
  5802. Returns 1 if found, 0 otherwise. */
  5803. static int
  5804. find_struct_field (const char *name, struct type *type, int offset,
  5805. struct type **field_type_p,
  5806. int *byte_offset_p, int *bit_offset_p, int *bit_size_p,
  5807. int *index_p)
  5808. {
  5809. int i;
  5810. int parent_offset = -1;
  5811. type = ada_check_typedef (type);
  5812. if (field_type_p != NULL)
  5813. *field_type_p = NULL;
  5814. if (byte_offset_p != NULL)
  5815. *byte_offset_p = 0;
  5816. if (bit_offset_p != NULL)
  5817. *bit_offset_p = 0;
  5818. if (bit_size_p != NULL)
  5819. *bit_size_p = 0;
  5820. for (i = 0; i < type->num_fields (); i += 1)
  5821. {
  5822. /* These can't be computed using TYPE_FIELD_BITPOS for a dynamic
  5823. type. However, we only need the values to be correct when
  5824. the caller asks for them. */
  5825. int bit_pos = 0, fld_offset = 0;
  5826. if (byte_offset_p != nullptr || bit_offset_p != nullptr)
  5827. {
  5828. bit_pos = type->field (i).loc_bitpos ();
  5829. fld_offset = offset + bit_pos / 8;
  5830. }
  5831. const char *t_field_name = type->field (i).name ();
  5832. if (t_field_name == NULL)
  5833. continue;
  5834. else if (ada_is_parent_field (type, i))
  5835. {
  5836. /* This is a field pointing us to the parent type of a tagged
  5837. type. As hinted in this function's documentation, we give
  5838. preference to fields in the current record first, so what
  5839. we do here is just record the index of this field before
  5840. we skip it. If it turns out we couldn't find our field
  5841. in the current record, then we'll get back to it and search
  5842. inside it whether the field might exist in the parent. */
  5843. parent_offset = i;
  5844. continue;
  5845. }
  5846. else if (name != NULL && field_name_match (t_field_name, name))
  5847. {
  5848. int bit_size = TYPE_FIELD_BITSIZE (type, i);
  5849. if (field_type_p != NULL)
  5850. *field_type_p = type->field (i).type ();
  5851. if (byte_offset_p != NULL)
  5852. *byte_offset_p = fld_offset;
  5853. if (bit_offset_p != NULL)
  5854. *bit_offset_p = bit_pos % 8;
  5855. if (bit_size_p != NULL)
  5856. *bit_size_p = bit_size;
  5857. return 1;
  5858. }
  5859. else if (ada_is_wrapper_field (type, i))
  5860. {
  5861. if (find_struct_field (name, type->field (i).type (), fld_offset,
  5862. field_type_p, byte_offset_p, bit_offset_p,
  5863. bit_size_p, index_p))
  5864. return 1;
  5865. }
  5866. else if (ada_is_variant_part (type, i))
  5867. {
  5868. /* PNH: Wait. Do we ever execute this section, or is ARG always of
  5869. fixed type?? */
  5870. int j;
  5871. struct type *field_type
  5872. = ada_check_typedef (type->field (i).type ());
  5873. for (j = 0; j < field_type->num_fields (); j += 1)
  5874. {
  5875. if (find_struct_field (name, field_type->field (j).type (),
  5876. fld_offset
  5877. + field_type->field (j).loc_bitpos () / 8,
  5878. field_type_p, byte_offset_p,
  5879. bit_offset_p, bit_size_p, index_p))
  5880. return 1;
  5881. }
  5882. }
  5883. else if (index_p != NULL)
  5884. *index_p += 1;
  5885. }
  5886. /* Field not found so far. If this is a tagged type which
  5887. has a parent, try finding that field in the parent now. */
  5888. if (parent_offset != -1)
  5889. {
  5890. /* As above, only compute the offset when truly needed. */
  5891. int fld_offset = offset;
  5892. if (byte_offset_p != nullptr || bit_offset_p != nullptr)
  5893. {
  5894. int bit_pos = type->field (parent_offset).loc_bitpos ();
  5895. fld_offset += bit_pos / 8;
  5896. }
  5897. if (find_struct_field (name, type->field (parent_offset).type (),
  5898. fld_offset, field_type_p, byte_offset_p,
  5899. bit_offset_p, bit_size_p, index_p))
  5900. return 1;
  5901. }
  5902. return 0;
  5903. }
  5904. /* Number of user-visible fields in record type TYPE. */
  5905. static int
  5906. num_visible_fields (struct type *type)
  5907. {
  5908. int n;
  5909. n = 0;
  5910. find_struct_field (NULL, type, 0, NULL, NULL, NULL, NULL, &n);
  5911. return n;
  5912. }
  5913. /* Look for a field NAME in ARG. Adjust the address of ARG by OFFSET bytes,
  5914. and search in it assuming it has (class) type TYPE.
  5915. If found, return value, else return NULL.
  5916. Searches recursively through wrapper fields (e.g., '_parent').
  5917. In the case of homonyms in the tagged types, please refer to the
  5918. long explanation in find_struct_field's function documentation. */
  5919. static struct value *
  5920. ada_search_struct_field (const char *name, struct value *arg, int offset,
  5921. struct type *type)
  5922. {
  5923. int i;
  5924. int parent_offset = -1;
  5925. type = ada_check_typedef (type);
  5926. for (i = 0; i < type->num_fields (); i += 1)
  5927. {
  5928. const char *t_field_name = type->field (i).name ();
  5929. if (t_field_name == NULL)
  5930. continue;
  5931. else if (ada_is_parent_field (type, i))
  5932. {
  5933. /* This is a field pointing us to the parent type of a tagged
  5934. type. As hinted in this function's documentation, we give
  5935. preference to fields in the current record first, so what
  5936. we do here is just record the index of this field before
  5937. we skip it. If it turns out we couldn't find our field
  5938. in the current record, then we'll get back to it and search
  5939. inside it whether the field might exist in the parent. */
  5940. parent_offset = i;
  5941. continue;
  5942. }
  5943. else if (field_name_match (t_field_name, name))
  5944. return ada_value_primitive_field (arg, offset, i, type);
  5945. else if (ada_is_wrapper_field (type, i))
  5946. {
  5947. struct value *v = /* Do not let indent join lines here. */
  5948. ada_search_struct_field (name, arg,
  5949. offset + type->field (i).loc_bitpos () / 8,
  5950. type->field (i).type ());
  5951. if (v != NULL)
  5952. return v;
  5953. }
  5954. else if (ada_is_variant_part (type, i))
  5955. {
  5956. /* PNH: Do we ever get here? See find_struct_field. */
  5957. int j;
  5958. struct type *field_type = ada_check_typedef (type->field (i).type ());
  5959. int var_offset = offset + type->field (i).loc_bitpos () / 8;
  5960. for (j = 0; j < field_type->num_fields (); j += 1)
  5961. {
  5962. struct value *v = ada_search_struct_field /* Force line
  5963. break. */
  5964. (name, arg,
  5965. var_offset + field_type->field (j).loc_bitpos () / 8,
  5966. field_type->field (j).type ());
  5967. if (v != NULL)
  5968. return v;
  5969. }
  5970. }
  5971. }
  5972. /* Field not found so far. If this is a tagged type which
  5973. has a parent, try finding that field in the parent now. */
  5974. if (parent_offset != -1)
  5975. {
  5976. struct value *v = ada_search_struct_field (
  5977. name, arg, offset + type->field (parent_offset).loc_bitpos () / 8,
  5978. type->field (parent_offset).type ());
  5979. if (v != NULL)
  5980. return v;
  5981. }
  5982. return NULL;
  5983. }
  5984. static struct value *ada_index_struct_field_1 (int *, struct value *,
  5985. int, struct type *);
  5986. /* Return field #INDEX in ARG, where the index is that returned by
  5987. * find_struct_field through its INDEX_P argument. Adjust the address
  5988. * of ARG by OFFSET bytes, and search in it assuming it has (class) type TYPE.
  5989. * If found, return value, else return NULL. */
  5990. static struct value *
  5991. ada_index_struct_field (int index, struct value *arg, int offset,
  5992. struct type *type)
  5993. {
  5994. return ada_index_struct_field_1 (&index, arg, offset, type);
  5995. }
  5996. /* Auxiliary function for ada_index_struct_field. Like
  5997. * ada_index_struct_field, but takes index from *INDEX_P and modifies
  5998. * *INDEX_P. */
  5999. static struct value *
  6000. ada_index_struct_field_1 (int *index_p, struct value *arg, int offset,
  6001. struct type *type)
  6002. {
  6003. int i;
  6004. type = ada_check_typedef (type);
  6005. for (i = 0; i < type->num_fields (); i += 1)
  6006. {
  6007. if (type->field (i).name () == NULL)
  6008. continue;
  6009. else if (ada_is_wrapper_field (type, i))
  6010. {
  6011. struct value *v = /* Do not let indent join lines here. */
  6012. ada_index_struct_field_1 (index_p, arg,
  6013. offset + type->field (i).loc_bitpos () / 8,
  6014. type->field (i).type ());
  6015. if (v != NULL)
  6016. return v;
  6017. }
  6018. else if (ada_is_variant_part (type, i))
  6019. {
  6020. /* PNH: Do we ever get here? See ada_search_struct_field,
  6021. find_struct_field. */
  6022. error (_("Cannot assign this kind of variant record"));
  6023. }
  6024. else if (*index_p == 0)
  6025. return ada_value_primitive_field (arg, offset, i, type);
  6026. else
  6027. *index_p -= 1;
  6028. }
  6029. return NULL;
  6030. }
  6031. /* Return a string representation of type TYPE. */
  6032. static std::string
  6033. type_as_string (struct type *type)
  6034. {
  6035. string_file tmp_stream;
  6036. type_print (type, "", &tmp_stream, -1);
  6037. return tmp_stream.release ();
  6038. }
  6039. /* Given a type TYPE, look up the type of the component of type named NAME.
  6040. If DISPP is non-null, add its byte displacement from the beginning of a
  6041. structure (pointed to by a value) of type TYPE to *DISPP (does not
  6042. work for packed fields).
  6043. Matches any field whose name has NAME as a prefix, possibly
  6044. followed by "___".
  6045. TYPE can be either a struct or union. If REFOK, TYPE may also
  6046. be a (pointer or reference)+ to a struct or union, and the
  6047. ultimate target type will be searched.
  6048. Looks recursively into variant clauses and parent types.
  6049. In the case of homonyms in the tagged types, please refer to the
  6050. long explanation in find_struct_field's function documentation.
  6051. If NOERR is nonzero, return NULL if NAME is not suitably defined or
  6052. TYPE is not a type of the right kind. */
  6053. static struct type *
  6054. ada_lookup_struct_elt_type (struct type *type, const char *name, int refok,
  6055. int noerr)
  6056. {
  6057. int i;
  6058. int parent_offset = -1;
  6059. if (name == NULL)
  6060. goto BadName;
  6061. if (refok && type != NULL)
  6062. while (1)
  6063. {
  6064. type = ada_check_typedef (type);
  6065. if (type->code () != TYPE_CODE_PTR && type->code () != TYPE_CODE_REF)
  6066. break;
  6067. type = TYPE_TARGET_TYPE (type);
  6068. }
  6069. if (type == NULL
  6070. || (type->code () != TYPE_CODE_STRUCT
  6071. && type->code () != TYPE_CODE_UNION))
  6072. {
  6073. if (noerr)
  6074. return NULL;
  6075. error (_("Type %s is not a structure or union type"),
  6076. type != NULL ? type_as_string (type).c_str () : _("(null)"));
  6077. }
  6078. type = to_static_fixed_type (type);
  6079. for (i = 0; i < type->num_fields (); i += 1)
  6080. {
  6081. const char *t_field_name = type->field (i).name ();
  6082. struct type *t;
  6083. if (t_field_name == NULL)
  6084. continue;
  6085. else if (ada_is_parent_field (type, i))
  6086. {
  6087. /* This is a field pointing us to the parent type of a tagged
  6088. type. As hinted in this function's documentation, we give
  6089. preference to fields in the current record first, so what
  6090. we do here is just record the index of this field before
  6091. we skip it. If it turns out we couldn't find our field
  6092. in the current record, then we'll get back to it and search
  6093. inside it whether the field might exist in the parent. */
  6094. parent_offset = i;
  6095. continue;
  6096. }
  6097. else if (field_name_match (t_field_name, name))
  6098. return type->field (i).type ();
  6099. else if (ada_is_wrapper_field (type, i))
  6100. {
  6101. t = ada_lookup_struct_elt_type (type->field (i).type (), name,
  6102. 0, 1);
  6103. if (t != NULL)
  6104. return t;
  6105. }
  6106. else if (ada_is_variant_part (type, i))
  6107. {
  6108. int j;
  6109. struct type *field_type = ada_check_typedef (type->field (i).type ());
  6110. for (j = field_type->num_fields () - 1; j >= 0; j -= 1)
  6111. {
  6112. /* FIXME pnh 2008/01/26: We check for a field that is
  6113. NOT wrapped in a struct, since the compiler sometimes
  6114. generates these for unchecked variant types. Revisit
  6115. if the compiler changes this practice. */
  6116. const char *v_field_name = field_type->field (j).name ();
  6117. if (v_field_name != NULL
  6118. && field_name_match (v_field_name, name))
  6119. t = field_type->field (j).type ();
  6120. else
  6121. t = ada_lookup_struct_elt_type (field_type->field (j).type (),
  6122. name, 0, 1);
  6123. if (t != NULL)
  6124. return t;
  6125. }
  6126. }
  6127. }
  6128. /* Field not found so far. If this is a tagged type which
  6129. has a parent, try finding that field in the parent now. */
  6130. if (parent_offset != -1)
  6131. {
  6132. struct type *t;
  6133. t = ada_lookup_struct_elt_type (type->field (parent_offset).type (),
  6134. name, 0, 1);
  6135. if (t != NULL)
  6136. return t;
  6137. }
  6138. BadName:
  6139. if (!noerr)
  6140. {
  6141. const char *name_str = name != NULL ? name : _("<null>");
  6142. error (_("Type %s has no component named %s"),
  6143. type_as_string (type).c_str (), name_str);
  6144. }
  6145. return NULL;
  6146. }
  6147. /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
  6148. within a value of type OUTER_TYPE, return true iff VAR_TYPE
  6149. represents an unchecked union (that is, the variant part of a
  6150. record that is named in an Unchecked_Union pragma). */
  6151. static int
  6152. is_unchecked_variant (struct type *var_type, struct type *outer_type)
  6153. {
  6154. const char *discrim_name = ada_variant_discrim_name (var_type);
  6155. return (ada_lookup_struct_elt_type (outer_type, discrim_name, 0, 1) == NULL);
  6156. }
  6157. /* Assuming that VAR_TYPE is the type of a variant part of a record (a union),
  6158. within OUTER, determine which variant clause (field number in VAR_TYPE,
  6159. numbering from 0) is applicable. Returns -1 if none are. */
  6160. int
  6161. ada_which_variant_applies (struct type *var_type, struct value *outer)
  6162. {
  6163. int others_clause;
  6164. int i;
  6165. const char *discrim_name = ada_variant_discrim_name (var_type);
  6166. struct value *discrim;
  6167. LONGEST discrim_val;
  6168. /* Using plain value_from_contents_and_address here causes problems
  6169. because we will end up trying to resolve a type that is currently
  6170. being constructed. */
  6171. discrim = ada_value_struct_elt (outer, discrim_name, 1);
  6172. if (discrim == NULL)
  6173. return -1;
  6174. discrim_val = value_as_long (discrim);
  6175. others_clause = -1;
  6176. for (i = 0; i < var_type->num_fields (); i += 1)
  6177. {
  6178. if (ada_is_others_clause (var_type, i))
  6179. others_clause = i;
  6180. else if (ada_in_variant (discrim_val, var_type, i))
  6181. return i;
  6182. }
  6183. return others_clause;
  6184. }
  6185. /* Dynamic-Sized Records */
  6186. /* Strategy: The type ostensibly attached to a value with dynamic size
  6187. (i.e., a size that is not statically recorded in the debugging
  6188. data) does not accurately reflect the size or layout of the value.
  6189. Our strategy is to convert these values to values with accurate,
  6190. conventional types that are constructed on the fly. */
  6191. /* There is a subtle and tricky problem here. In general, we cannot
  6192. determine the size of dynamic records without its data. However,
  6193. the 'struct value' data structure, which GDB uses to represent
  6194. quantities in the inferior process (the target), requires the size
  6195. of the type at the time of its allocation in order to reserve space
  6196. for GDB's internal copy of the data. That's why the
  6197. 'to_fixed_xxx_type' routines take (target) addresses as parameters,
  6198. rather than struct value*s.
  6199. However, GDB's internal history variables ($1, $2, etc.) are
  6200. struct value*s containing internal copies of the data that are not, in
  6201. general, the same as the data at their corresponding addresses in
  6202. the target. Fortunately, the types we give to these values are all
  6203. conventional, fixed-size types (as per the strategy described
  6204. above), so that we don't usually have to perform the
  6205. 'to_fixed_xxx_type' conversions to look at their values.
  6206. Unfortunately, there is one exception: if one of the internal
  6207. history variables is an array whose elements are unconstrained
  6208. records, then we will need to create distinct fixed types for each
  6209. element selected. */
  6210. /* The upshot of all of this is that many routines take a (type, host
  6211. address, target address) triple as arguments to represent a value.
  6212. The host address, if non-null, is supposed to contain an internal
  6213. copy of the relevant data; otherwise, the program is to consult the
  6214. target at the target address. */
  6215. /* Assuming that VAL0 represents a pointer value, the result of
  6216. dereferencing it. Differs from value_ind in its treatment of
  6217. dynamic-sized types. */
  6218. struct value *
  6219. ada_value_ind (struct value *val0)
  6220. {
  6221. struct value *val = value_ind (val0);
  6222. if (ada_is_tagged_type (value_type (val), 0))
  6223. val = ada_tag_value_at_base_address (val);
  6224. return ada_to_fixed_value (val);
  6225. }
  6226. /* The value resulting from dereferencing any "reference to"
  6227. qualifiers on VAL0. */
  6228. static struct value *
  6229. ada_coerce_ref (struct value *val0)
  6230. {
  6231. if (value_type (val0)->code () == TYPE_CODE_REF)
  6232. {
  6233. struct value *val = val0;
  6234. val = coerce_ref (val);
  6235. if (ada_is_tagged_type (value_type (val), 0))
  6236. val = ada_tag_value_at_base_address (val);
  6237. return ada_to_fixed_value (val);
  6238. }
  6239. else
  6240. return val0;
  6241. }
  6242. /* Return the bit alignment required for field #F of template type TYPE. */
  6243. static unsigned int
  6244. field_alignment (struct type *type, int f)
  6245. {
  6246. const char *name = type->field (f).name ();
  6247. int len;
  6248. int align_offset;
  6249. /* The field name should never be null, unless the debugging information
  6250. is somehow malformed. In this case, we assume the field does not
  6251. require any alignment. */
  6252. if (name == NULL)
  6253. return 1;
  6254. len = strlen (name);
  6255. if (!isdigit (name[len - 1]))
  6256. return 1;
  6257. if (isdigit (name[len - 2]))
  6258. align_offset = len - 2;
  6259. else
  6260. align_offset = len - 1;
  6261. if (align_offset < 7 || !startswith (name + align_offset - 6, "___XV"))
  6262. return TARGET_CHAR_BIT;
  6263. return atoi (name + align_offset) * TARGET_CHAR_BIT;
  6264. }
  6265. /* Find a typedef or tag symbol named NAME. Ignores ambiguity. */
  6266. static struct symbol *
  6267. ada_find_any_type_symbol (const char *name)
  6268. {
  6269. struct symbol *sym;
  6270. sym = standard_lookup (name, get_selected_block (NULL), VAR_DOMAIN);
  6271. if (sym != NULL && sym->aclass () == LOC_TYPEDEF)
  6272. return sym;
  6273. sym = standard_lookup (name, NULL, STRUCT_DOMAIN);
  6274. return sym;
  6275. }
  6276. /* Find a type named NAME. Ignores ambiguity. This routine will look
  6277. solely for types defined by debug info, it will not search the GDB
  6278. primitive types. */
  6279. static struct type *
  6280. ada_find_any_type (const char *name)
  6281. {
  6282. struct symbol *sym = ada_find_any_type_symbol (name);
  6283. if (sym != NULL)
  6284. return sym->type ();
  6285. return NULL;
  6286. }
  6287. /* Given NAME_SYM and an associated BLOCK, find a "renaming" symbol
  6288. associated with NAME_SYM's name. NAME_SYM may itself be a renaming
  6289. symbol, in which case it is returned. Otherwise, this looks for
  6290. symbols whose name is that of NAME_SYM suffixed with "___XR".
  6291. Return symbol if found, and NULL otherwise. */
  6292. static bool
  6293. ada_is_renaming_symbol (struct symbol *name_sym)
  6294. {
  6295. const char *name = name_sym->linkage_name ();
  6296. return strstr (name, "___XR") != NULL;
  6297. }
  6298. /* Because of GNAT encoding conventions, several GDB symbols may match a
  6299. given type name. If the type denoted by TYPE0 is to be preferred to
  6300. that of TYPE1 for purposes of type printing, return non-zero;
  6301. otherwise return 0. */
  6302. int
  6303. ada_prefer_type (struct type *type0, struct type *type1)
  6304. {
  6305. if (type1 == NULL)
  6306. return 1;
  6307. else if (type0 == NULL)
  6308. return 0;
  6309. else if (type1->code () == TYPE_CODE_VOID)
  6310. return 1;
  6311. else if (type0->code () == TYPE_CODE_VOID)
  6312. return 0;
  6313. else if (type1->name () == NULL && type0->name () != NULL)
  6314. return 1;
  6315. else if (ada_is_constrained_packed_array_type (type0))
  6316. return 1;
  6317. else if (ada_is_array_descriptor_type (type0)
  6318. && !ada_is_array_descriptor_type (type1))
  6319. return 1;
  6320. else
  6321. {
  6322. const char *type0_name = type0->name ();
  6323. const char *type1_name = type1->name ();
  6324. if (type0_name != NULL && strstr (type0_name, "___XR") != NULL
  6325. && (type1_name == NULL || strstr (type1_name, "___XR") == NULL))
  6326. return 1;
  6327. }
  6328. return 0;
  6329. }
  6330. /* The name of TYPE, which is its TYPE_NAME. Null if TYPE is
  6331. null. */
  6332. const char *
  6333. ada_type_name (struct type *type)
  6334. {
  6335. if (type == NULL)
  6336. return NULL;
  6337. return type->name ();
  6338. }
  6339. /* Search the list of "descriptive" types associated to TYPE for a type
  6340. whose name is NAME. */
  6341. static struct type *
  6342. find_parallel_type_by_descriptive_type (struct type *type, const char *name)
  6343. {
  6344. struct type *result, *tmp;
  6345. if (ada_ignore_descriptive_types_p)
  6346. return NULL;
  6347. /* If there no descriptive-type info, then there is no parallel type
  6348. to be found. */
  6349. if (!HAVE_GNAT_AUX_INFO (type))
  6350. return NULL;
  6351. result = TYPE_DESCRIPTIVE_TYPE (type);
  6352. while (result != NULL)
  6353. {
  6354. const char *result_name = ada_type_name (result);
  6355. if (result_name == NULL)
  6356. {
  6357. warning (_("unexpected null name on descriptive type"));
  6358. return NULL;
  6359. }
  6360. /* If the names match, stop. */
  6361. if (strcmp (result_name, name) == 0)
  6362. break;
  6363. /* Otherwise, look at the next item on the list, if any. */
  6364. if (HAVE_GNAT_AUX_INFO (result))
  6365. tmp = TYPE_DESCRIPTIVE_TYPE (result);
  6366. else
  6367. tmp = NULL;
  6368. /* If not found either, try after having resolved the typedef. */
  6369. if (tmp != NULL)
  6370. result = tmp;
  6371. else
  6372. {
  6373. result = check_typedef (result);
  6374. if (HAVE_GNAT_AUX_INFO (result))
  6375. result = TYPE_DESCRIPTIVE_TYPE (result);
  6376. else
  6377. result = NULL;
  6378. }
  6379. }
  6380. /* If we didn't find a match, see whether this is a packed array. With
  6381. older compilers, the descriptive type information is either absent or
  6382. irrelevant when it comes to packed arrays so the above lookup fails.
  6383. Fall back to using a parallel lookup by name in this case. */
  6384. if (result == NULL && ada_is_constrained_packed_array_type (type))
  6385. return ada_find_any_type (name);
  6386. return result;
  6387. }
  6388. /* Find a parallel type to TYPE with the specified NAME, using the
  6389. descriptive type taken from the debugging information, if available,
  6390. and otherwise using the (slower) name-based method. */
  6391. static struct type *
  6392. ada_find_parallel_type_with_name (struct type *type, const char *name)
  6393. {
  6394. struct type *result = NULL;
  6395. if (HAVE_GNAT_AUX_INFO (type))
  6396. result = find_parallel_type_by_descriptive_type (type, name);
  6397. else
  6398. result = ada_find_any_type (name);
  6399. return result;
  6400. }
  6401. /* Same as above, but specify the name of the parallel type by appending
  6402. SUFFIX to the name of TYPE. */
  6403. struct type *
  6404. ada_find_parallel_type (struct type *type, const char *suffix)
  6405. {
  6406. char *name;
  6407. const char *type_name = ada_type_name (type);
  6408. int len;
  6409. if (type_name == NULL)
  6410. return NULL;
  6411. len = strlen (type_name);
  6412. name = (char *) alloca (len + strlen (suffix) + 1);
  6413. strcpy (name, type_name);
  6414. strcpy (name + len, suffix);
  6415. return ada_find_parallel_type_with_name (type, name);
  6416. }
  6417. /* If TYPE is a variable-size record type, return the corresponding template
  6418. type describing its fields. Otherwise, return NULL. */
  6419. static struct type *
  6420. dynamic_template_type (struct type *type)
  6421. {
  6422. type = ada_check_typedef (type);
  6423. if (type == NULL || type->code () != TYPE_CODE_STRUCT
  6424. || ada_type_name (type) == NULL)
  6425. return NULL;
  6426. else
  6427. {
  6428. int len = strlen (ada_type_name (type));
  6429. if (len > 6 && strcmp (ada_type_name (type) + len - 6, "___XVE") == 0)
  6430. return type;
  6431. else
  6432. return ada_find_parallel_type (type, "___XVE");
  6433. }
  6434. }
  6435. /* Assuming that TEMPL_TYPE is a union or struct type, returns
  6436. non-zero iff field FIELD_NUM of TEMPL_TYPE has dynamic size. */
  6437. static int
  6438. is_dynamic_field (struct type *templ_type, int field_num)
  6439. {
  6440. const char *name = templ_type->field (field_num).name ();
  6441. return name != NULL
  6442. && templ_type->field (field_num).type ()->code () == TYPE_CODE_PTR
  6443. && strstr (name, "___XVL") != NULL;
  6444. }
  6445. /* The index of the variant field of TYPE, or -1 if TYPE does not
  6446. represent a variant record type. */
  6447. static int
  6448. variant_field_index (struct type *type)
  6449. {
  6450. int f;
  6451. if (type == NULL || type->code () != TYPE_CODE_STRUCT)
  6452. return -1;
  6453. for (f = 0; f < type->num_fields (); f += 1)
  6454. {
  6455. if (ada_is_variant_part (type, f))
  6456. return f;
  6457. }
  6458. return -1;
  6459. }
  6460. /* A record type with no fields. */
  6461. static struct type *
  6462. empty_record (struct type *templ)
  6463. {
  6464. struct type *type = alloc_type_copy (templ);
  6465. type->set_code (TYPE_CODE_STRUCT);
  6466. INIT_NONE_SPECIFIC (type);
  6467. type->set_name ("<empty>");
  6468. TYPE_LENGTH (type) = 0;
  6469. return type;
  6470. }
  6471. /* An ordinary record type (with fixed-length fields) that describes
  6472. the value of type TYPE at VALADDR or ADDRESS (see comments at
  6473. the beginning of this section) VAL according to GNAT conventions.
  6474. DVAL0 should describe the (portion of a) record that contains any
  6475. necessary discriminants. It should be NULL if value_type (VAL) is
  6476. an outer-level type (i.e., as opposed to a branch of a variant.) A
  6477. variant field (unless unchecked) is replaced by a particular branch
  6478. of the variant.
  6479. If not KEEP_DYNAMIC_FIELDS, then all fields whose position or
  6480. length are not statically known are discarded. As a consequence,
  6481. VALADDR, ADDRESS and DVAL0 are ignored.
  6482. NOTE: Limitations: For now, we assume that dynamic fields and
  6483. variants occupy whole numbers of bytes. However, they need not be
  6484. byte-aligned. */
  6485. struct type *
  6486. ada_template_to_fixed_record_type_1 (struct type *type,
  6487. const gdb_byte *valaddr,
  6488. CORE_ADDR address, struct value *dval0,
  6489. int keep_dynamic_fields)
  6490. {
  6491. struct value *mark = value_mark ();
  6492. struct value *dval;
  6493. struct type *rtype;
  6494. int nfields, bit_len;
  6495. int variant_field;
  6496. long off;
  6497. int fld_bit_len;
  6498. int f;
  6499. /* Compute the number of fields in this record type that are going
  6500. to be processed: unless keep_dynamic_fields, this includes only
  6501. fields whose position and length are static will be processed. */
  6502. if (keep_dynamic_fields)
  6503. nfields = type->num_fields ();
  6504. else
  6505. {
  6506. nfields = 0;
  6507. while (nfields < type->num_fields ()
  6508. && !ada_is_variant_part (type, nfields)
  6509. && !is_dynamic_field (type, nfields))
  6510. nfields++;
  6511. }
  6512. rtype = alloc_type_copy (type);
  6513. rtype->set_code (TYPE_CODE_STRUCT);
  6514. INIT_NONE_SPECIFIC (rtype);
  6515. rtype->set_num_fields (nfields);
  6516. rtype->set_fields
  6517. ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
  6518. rtype->set_name (ada_type_name (type));
  6519. rtype->set_is_fixed_instance (true);
  6520. off = 0;
  6521. bit_len = 0;
  6522. variant_field = -1;
  6523. for (f = 0; f < nfields; f += 1)
  6524. {
  6525. off = align_up (off, field_alignment (type, f))
  6526. + type->field (f).loc_bitpos ();
  6527. rtype->field (f).set_loc_bitpos (off);
  6528. TYPE_FIELD_BITSIZE (rtype, f) = 0;
  6529. if (ada_is_variant_part (type, f))
  6530. {
  6531. variant_field = f;
  6532. fld_bit_len = 0;
  6533. }
  6534. else if (is_dynamic_field (type, f))
  6535. {
  6536. const gdb_byte *field_valaddr = valaddr;
  6537. CORE_ADDR field_address = address;
  6538. struct type *field_type =
  6539. TYPE_TARGET_TYPE (type->field (f).type ());
  6540. if (dval0 == NULL)
  6541. {
  6542. /* Using plain value_from_contents_and_address here
  6543. causes problems because we will end up trying to
  6544. resolve a type that is currently being
  6545. constructed. */
  6546. dval = value_from_contents_and_address_unresolved (rtype,
  6547. valaddr,
  6548. address);
  6549. rtype = value_type (dval);
  6550. }
  6551. else
  6552. dval = dval0;
  6553. /* If the type referenced by this field is an aligner type, we need
  6554. to unwrap that aligner type, because its size might not be set.
  6555. Keeping the aligner type would cause us to compute the wrong
  6556. size for this field, impacting the offset of the all the fields
  6557. that follow this one. */
  6558. if (ada_is_aligner_type (field_type))
  6559. {
  6560. long field_offset = type->field (f).loc_bitpos ();
  6561. field_valaddr = cond_offset_host (field_valaddr, field_offset);
  6562. field_address = cond_offset_target (field_address, field_offset);
  6563. field_type = ada_aligned_type (field_type);
  6564. }
  6565. field_valaddr = cond_offset_host (field_valaddr,
  6566. off / TARGET_CHAR_BIT);
  6567. field_address = cond_offset_target (field_address,
  6568. off / TARGET_CHAR_BIT);
  6569. /* Get the fixed type of the field. Note that, in this case,
  6570. we do not want to get the real type out of the tag: if
  6571. the current field is the parent part of a tagged record,
  6572. we will get the tag of the object. Clearly wrong: the real
  6573. type of the parent is not the real type of the child. We
  6574. would end up in an infinite loop. */
  6575. field_type = ada_get_base_type (field_type);
  6576. field_type = ada_to_fixed_type (field_type, field_valaddr,
  6577. field_address, dval, 0);
  6578. rtype->field (f).set_type (field_type);
  6579. rtype->field (f).set_name (type->field (f).name ());
  6580. /* The multiplication can potentially overflow. But because
  6581. the field length has been size-checked just above, and
  6582. assuming that the maximum size is a reasonable value,
  6583. an overflow should not happen in practice. So rather than
  6584. adding overflow recovery code to this already complex code,
  6585. we just assume that it's not going to happen. */
  6586. fld_bit_len =
  6587. TYPE_LENGTH (rtype->field (f).type ()) * TARGET_CHAR_BIT;
  6588. }
  6589. else
  6590. {
  6591. /* Note: If this field's type is a typedef, it is important
  6592. to preserve the typedef layer.
  6593. Otherwise, we might be transforming a typedef to a fat
  6594. pointer (encoding a pointer to an unconstrained array),
  6595. into a basic fat pointer (encoding an unconstrained
  6596. array). As both types are implemented using the same
  6597. structure, the typedef is the only clue which allows us
  6598. to distinguish between the two options. Stripping it
  6599. would prevent us from printing this field appropriately. */
  6600. rtype->field (f).set_type (type->field (f).type ());
  6601. rtype->field (f).set_name (type->field (f).name ());
  6602. if (TYPE_FIELD_BITSIZE (type, f) > 0)
  6603. fld_bit_len =
  6604. TYPE_FIELD_BITSIZE (rtype, f) = TYPE_FIELD_BITSIZE (type, f);
  6605. else
  6606. {
  6607. struct type *field_type = type->field (f).type ();
  6608. /* We need to be careful of typedefs when computing
  6609. the length of our field. If this is a typedef,
  6610. get the length of the target type, not the length
  6611. of the typedef. */
  6612. if (field_type->code () == TYPE_CODE_TYPEDEF)
  6613. field_type = ada_typedef_target_type (field_type);
  6614. fld_bit_len =
  6615. TYPE_LENGTH (ada_check_typedef (field_type)) * TARGET_CHAR_BIT;
  6616. }
  6617. }
  6618. if (off + fld_bit_len > bit_len)
  6619. bit_len = off + fld_bit_len;
  6620. off += fld_bit_len;
  6621. TYPE_LENGTH (rtype) =
  6622. align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
  6623. }
  6624. /* We handle the variant part, if any, at the end because of certain
  6625. odd cases in which it is re-ordered so as NOT to be the last field of
  6626. the record. This can happen in the presence of representation
  6627. clauses. */
  6628. if (variant_field >= 0)
  6629. {
  6630. struct type *branch_type;
  6631. off = rtype->field (variant_field).loc_bitpos ();
  6632. if (dval0 == NULL)
  6633. {
  6634. /* Using plain value_from_contents_and_address here causes
  6635. problems because we will end up trying to resolve a type
  6636. that is currently being constructed. */
  6637. dval = value_from_contents_and_address_unresolved (rtype, valaddr,
  6638. address);
  6639. rtype = value_type (dval);
  6640. }
  6641. else
  6642. dval = dval0;
  6643. branch_type =
  6644. to_fixed_variant_branch_type
  6645. (type->field (variant_field).type (),
  6646. cond_offset_host (valaddr, off / TARGET_CHAR_BIT),
  6647. cond_offset_target (address, off / TARGET_CHAR_BIT), dval);
  6648. if (branch_type == NULL)
  6649. {
  6650. for (f = variant_field + 1; f < rtype->num_fields (); f += 1)
  6651. rtype->field (f - 1) = rtype->field (f);
  6652. rtype->set_num_fields (rtype->num_fields () - 1);
  6653. }
  6654. else
  6655. {
  6656. rtype->field (variant_field).set_type (branch_type);
  6657. rtype->field (variant_field).set_name ("S");
  6658. fld_bit_len =
  6659. TYPE_LENGTH (rtype->field (variant_field).type ()) *
  6660. TARGET_CHAR_BIT;
  6661. if (off + fld_bit_len > bit_len)
  6662. bit_len = off + fld_bit_len;
  6663. TYPE_LENGTH (rtype) =
  6664. align_up (bit_len, TARGET_CHAR_BIT) / TARGET_CHAR_BIT;
  6665. }
  6666. }
  6667. /* According to exp_dbug.ads, the size of TYPE for variable-size records
  6668. should contain the alignment of that record, which should be a strictly
  6669. positive value. If null or negative, then something is wrong, most
  6670. probably in the debug info. In that case, we don't round up the size
  6671. of the resulting type. If this record is not part of another structure,
  6672. the current RTYPE length might be good enough for our purposes. */
  6673. if (TYPE_LENGTH (type) <= 0)
  6674. {
  6675. if (rtype->name ())
  6676. warning (_("Invalid type size for `%s' detected: %s."),
  6677. rtype->name (), pulongest (TYPE_LENGTH (type)));
  6678. else
  6679. warning (_("Invalid type size for <unnamed> detected: %s."),
  6680. pulongest (TYPE_LENGTH (type)));
  6681. }
  6682. else
  6683. {
  6684. TYPE_LENGTH (rtype) = align_up (TYPE_LENGTH (rtype),
  6685. TYPE_LENGTH (type));
  6686. }
  6687. value_free_to_mark (mark);
  6688. return rtype;
  6689. }
  6690. /* As for ada_template_to_fixed_record_type_1 with KEEP_DYNAMIC_FIELDS
  6691. of 1. */
  6692. static struct type *
  6693. template_to_fixed_record_type (struct type *type, const gdb_byte *valaddr,
  6694. CORE_ADDR address, struct value *dval0)
  6695. {
  6696. return ada_template_to_fixed_record_type_1 (type, valaddr,
  6697. address, dval0, 1);
  6698. }
  6699. /* An ordinary record type in which ___XVL-convention fields and
  6700. ___XVU- and ___XVN-convention field types in TYPE0 are replaced with
  6701. static approximations, containing all possible fields. Uses
  6702. no runtime values. Useless for use in values, but that's OK,
  6703. since the results are used only for type determinations. Works on both
  6704. structs and unions. Representation note: to save space, we memorize
  6705. the result of this function in the TYPE_TARGET_TYPE of the
  6706. template type. */
  6707. static struct type *
  6708. template_to_static_fixed_type (struct type *type0)
  6709. {
  6710. struct type *type;
  6711. int nfields;
  6712. int f;
  6713. /* No need no do anything if the input type is already fixed. */
  6714. if (type0->is_fixed_instance ())
  6715. return type0;
  6716. /* Likewise if we already have computed the static approximation. */
  6717. if (TYPE_TARGET_TYPE (type0) != NULL)
  6718. return TYPE_TARGET_TYPE (type0);
  6719. /* Don't clone TYPE0 until we are sure we are going to need a copy. */
  6720. type = type0;
  6721. nfields = type0->num_fields ();
  6722. /* Whether or not we cloned TYPE0, cache the result so that we don't do
  6723. recompute all over next time. */
  6724. TYPE_TARGET_TYPE (type0) = type;
  6725. for (f = 0; f < nfields; f += 1)
  6726. {
  6727. struct type *field_type = type0->field (f).type ();
  6728. struct type *new_type;
  6729. if (is_dynamic_field (type0, f))
  6730. {
  6731. field_type = ada_check_typedef (field_type);
  6732. new_type = to_static_fixed_type (TYPE_TARGET_TYPE (field_type));
  6733. }
  6734. else
  6735. new_type = static_unwrap_type (field_type);
  6736. if (new_type != field_type)
  6737. {
  6738. /* Clone TYPE0 only the first time we get a new field type. */
  6739. if (type == type0)
  6740. {
  6741. TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
  6742. type->set_code (type0->code ());
  6743. INIT_NONE_SPECIFIC (type);
  6744. type->set_num_fields (nfields);
  6745. field *fields =
  6746. ((struct field *)
  6747. TYPE_ALLOC (type, nfields * sizeof (struct field)));
  6748. memcpy (fields, type0->fields (),
  6749. sizeof (struct field) * nfields);
  6750. type->set_fields (fields);
  6751. type->set_name (ada_type_name (type0));
  6752. type->set_is_fixed_instance (true);
  6753. TYPE_LENGTH (type) = 0;
  6754. }
  6755. type->field (f).set_type (new_type);
  6756. type->field (f).set_name (type0->field (f).name ());
  6757. }
  6758. }
  6759. return type;
  6760. }
  6761. /* Given an object of type TYPE whose contents are at VALADDR and
  6762. whose address in memory is ADDRESS, returns a revision of TYPE,
  6763. which should be a non-dynamic-sized record, in which the variant
  6764. part, if any, is replaced with the appropriate branch. Looks
  6765. for discriminant values in DVAL0, which can be NULL if the record
  6766. contains the necessary discriminant values. */
  6767. static struct type *
  6768. to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
  6769. CORE_ADDR address, struct value *dval0)
  6770. {
  6771. struct value *mark = value_mark ();
  6772. struct value *dval;
  6773. struct type *rtype;
  6774. struct type *branch_type;
  6775. int nfields = type->num_fields ();
  6776. int variant_field = variant_field_index (type);
  6777. if (variant_field == -1)
  6778. return type;
  6779. if (dval0 == NULL)
  6780. {
  6781. dval = value_from_contents_and_address (type, valaddr, address);
  6782. type = value_type (dval);
  6783. }
  6784. else
  6785. dval = dval0;
  6786. rtype = alloc_type_copy (type);
  6787. rtype->set_code (TYPE_CODE_STRUCT);
  6788. INIT_NONE_SPECIFIC (rtype);
  6789. rtype->set_num_fields (nfields);
  6790. field *fields =
  6791. (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
  6792. memcpy (fields, type->fields (), sizeof (struct field) * nfields);
  6793. rtype->set_fields (fields);
  6794. rtype->set_name (ada_type_name (type));
  6795. rtype->set_is_fixed_instance (true);
  6796. TYPE_LENGTH (rtype) = TYPE_LENGTH (type);
  6797. branch_type = to_fixed_variant_branch_type
  6798. (type->field (variant_field).type (),
  6799. cond_offset_host (valaddr,
  6800. type->field (variant_field).loc_bitpos ()
  6801. / TARGET_CHAR_BIT),
  6802. cond_offset_target (address,
  6803. type->field (variant_field).loc_bitpos ()
  6804. / TARGET_CHAR_BIT), dval);
  6805. if (branch_type == NULL)
  6806. {
  6807. int f;
  6808. for (f = variant_field + 1; f < nfields; f += 1)
  6809. rtype->field (f - 1) = rtype->field (f);
  6810. rtype->set_num_fields (rtype->num_fields () - 1);
  6811. }
  6812. else
  6813. {
  6814. rtype->field (variant_field).set_type (branch_type);
  6815. rtype->field (variant_field).set_name ("S");
  6816. TYPE_FIELD_BITSIZE (rtype, variant_field) = 0;
  6817. TYPE_LENGTH (rtype) += TYPE_LENGTH (branch_type);
  6818. }
  6819. TYPE_LENGTH (rtype) -= TYPE_LENGTH (type->field (variant_field).type ());
  6820. value_free_to_mark (mark);
  6821. return rtype;
  6822. }
  6823. /* An ordinary record type (with fixed-length fields) that describes
  6824. the value at (TYPE0, VALADDR, ADDRESS) [see explanation at
  6825. beginning of this section]. Any necessary discriminants' values
  6826. should be in DVAL, a record value; it may be NULL if the object
  6827. at ADDR itself contains any necessary discriminant values.
  6828. Additionally, VALADDR and ADDRESS may also be NULL if no discriminant
  6829. values from the record are needed. Except in the case that DVAL,
  6830. VALADDR, and ADDRESS are all 0 or NULL, a variant field (unless
  6831. unchecked) is replaced by a particular branch of the variant.
  6832. NOTE: the case in which DVAL and VALADDR are NULL and ADDRESS is 0
  6833. is questionable and may be removed. It can arise during the
  6834. processing of an unconstrained-array-of-record type where all the
  6835. variant branches have exactly the same size. This is because in
  6836. such cases, the compiler does not bother to use the XVS convention
  6837. when encoding the record. I am currently dubious of this
  6838. shortcut and suspect the compiler should be altered. FIXME. */
  6839. static struct type *
  6840. to_fixed_record_type (struct type *type0, const gdb_byte *valaddr,
  6841. CORE_ADDR address, struct value *dval)
  6842. {
  6843. struct type *templ_type;
  6844. if (type0->is_fixed_instance ())
  6845. return type0;
  6846. templ_type = dynamic_template_type (type0);
  6847. if (templ_type != NULL)
  6848. return template_to_fixed_record_type (templ_type, valaddr, address, dval);
  6849. else if (variant_field_index (type0) >= 0)
  6850. {
  6851. if (dval == NULL && valaddr == NULL && address == 0)
  6852. return type0;
  6853. return to_record_with_fixed_variant_part (type0, valaddr, address,
  6854. dval);
  6855. }
  6856. else
  6857. {
  6858. type0->set_is_fixed_instance (true);
  6859. return type0;
  6860. }
  6861. }
  6862. /* An ordinary record type (with fixed-length fields) that describes
  6863. the value at (VAR_TYPE0, VALADDR, ADDRESS), where VAR_TYPE0 is a
  6864. union type. Any necessary discriminants' values should be in DVAL,
  6865. a record value. That is, this routine selects the appropriate
  6866. branch of the union at ADDR according to the discriminant value
  6867. indicated in the union's type name. Returns VAR_TYPE0 itself if
  6868. it represents a variant subject to a pragma Unchecked_Union. */
  6869. static struct type *
  6870. to_fixed_variant_branch_type (struct type *var_type0, const gdb_byte *valaddr,
  6871. CORE_ADDR address, struct value *dval)
  6872. {
  6873. int which;
  6874. struct type *templ_type;
  6875. struct type *var_type;
  6876. if (var_type0->code () == TYPE_CODE_PTR)
  6877. var_type = TYPE_TARGET_TYPE (var_type0);
  6878. else
  6879. var_type = var_type0;
  6880. templ_type = ada_find_parallel_type (var_type, "___XVU");
  6881. if (templ_type != NULL)
  6882. var_type = templ_type;
  6883. if (is_unchecked_variant (var_type, value_type (dval)))
  6884. return var_type0;
  6885. which = ada_which_variant_applies (var_type, dval);
  6886. if (which < 0)
  6887. return empty_record (var_type);
  6888. else if (is_dynamic_field (var_type, which))
  6889. return to_fixed_record_type
  6890. (TYPE_TARGET_TYPE (var_type->field (which).type ()),
  6891. valaddr, address, dval);
  6892. else if (variant_field_index (var_type->field (which).type ()) >= 0)
  6893. return
  6894. to_fixed_record_type
  6895. (var_type->field (which).type (), valaddr, address, dval);
  6896. else
  6897. return var_type->field (which).type ();
  6898. }
  6899. /* Assuming RANGE_TYPE is a TYPE_CODE_RANGE, return nonzero if
  6900. ENCODING_TYPE, a type following the GNAT conventions for discrete
  6901. type encodings, only carries redundant information. */
  6902. static int
  6903. ada_is_redundant_range_encoding (struct type *range_type,
  6904. struct type *encoding_type)
  6905. {
  6906. const char *bounds_str;
  6907. int n;
  6908. LONGEST lo, hi;
  6909. gdb_assert (range_type->code () == TYPE_CODE_RANGE);
  6910. if (get_base_type (range_type)->code ()
  6911. != get_base_type (encoding_type)->code ())
  6912. {
  6913. /* The compiler probably used a simple base type to describe
  6914. the range type instead of the range's actual base type,
  6915. expecting us to get the real base type from the encoding
  6916. anyway. In this situation, the encoding cannot be ignored
  6917. as redundant. */
  6918. return 0;
  6919. }
  6920. if (is_dynamic_type (range_type))
  6921. return 0;
  6922. if (encoding_type->name () == NULL)
  6923. return 0;
  6924. bounds_str = strstr (encoding_type->name (), "___XDLU_");
  6925. if (bounds_str == NULL)
  6926. return 0;
  6927. n = 8; /* Skip "___XDLU_". */
  6928. if (!ada_scan_number (bounds_str, n, &lo, &n))
  6929. return 0;
  6930. if (range_type->bounds ()->low.const_val () != lo)
  6931. return 0;
  6932. n += 2; /* Skip the "__" separator between the two bounds. */
  6933. if (!ada_scan_number (bounds_str, n, &hi, &n))
  6934. return 0;
  6935. if (range_type->bounds ()->high.const_val () != hi)
  6936. return 0;
  6937. return 1;
  6938. }
  6939. /* Given the array type ARRAY_TYPE, return nonzero if DESC_TYPE,
  6940. a type following the GNAT encoding for describing array type
  6941. indices, only carries redundant information. */
  6942. static int
  6943. ada_is_redundant_index_type_desc (struct type *array_type,
  6944. struct type *desc_type)
  6945. {
  6946. struct type *this_layer = check_typedef (array_type);
  6947. int i;
  6948. for (i = 0; i < desc_type->num_fields (); i++)
  6949. {
  6950. if (!ada_is_redundant_range_encoding (this_layer->index_type (),
  6951. desc_type->field (i).type ()))
  6952. return 0;
  6953. this_layer = check_typedef (TYPE_TARGET_TYPE (this_layer));
  6954. }
  6955. return 1;
  6956. }
  6957. /* Assuming that TYPE0 is an array type describing the type of a value
  6958. at ADDR, and that DVAL describes a record containing any
  6959. discriminants used in TYPE0, returns a type for the value that
  6960. contains no dynamic components (that is, no components whose sizes
  6961. are determined by run-time quantities). Unless IGNORE_TOO_BIG is
  6962. true, gives an error message if the resulting type's size is over
  6963. varsize_limit. */
  6964. static struct type *
  6965. to_fixed_array_type (struct type *type0, struct value *dval,
  6966. int ignore_too_big)
  6967. {
  6968. struct type *index_type_desc;
  6969. struct type *result;
  6970. int constrained_packed_array_p;
  6971. static const char *xa_suffix = "___XA";
  6972. type0 = ada_check_typedef (type0);
  6973. if (type0->is_fixed_instance ())
  6974. return type0;
  6975. constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
  6976. if (constrained_packed_array_p)
  6977. {
  6978. type0 = decode_constrained_packed_array_type (type0);
  6979. if (type0 == nullptr)
  6980. error (_("could not decode constrained packed array type"));
  6981. }
  6982. index_type_desc = ada_find_parallel_type (type0, xa_suffix);
  6983. /* As mentioned in exp_dbug.ads, for non bit-packed arrays an
  6984. encoding suffixed with 'P' may still be generated. If so,
  6985. it should be used to find the XA type. */
  6986. if (index_type_desc == NULL)
  6987. {
  6988. const char *type_name = ada_type_name (type0);
  6989. if (type_name != NULL)
  6990. {
  6991. const int len = strlen (type_name);
  6992. char *name = (char *) alloca (len + strlen (xa_suffix));
  6993. if (type_name[len - 1] == 'P')
  6994. {
  6995. strcpy (name, type_name);
  6996. strcpy (name + len - 1, xa_suffix);
  6997. index_type_desc = ada_find_parallel_type_with_name (type0, name);
  6998. }
  6999. }
  7000. }
  7001. ada_fixup_array_indexes_type (index_type_desc);
  7002. if (index_type_desc != NULL
  7003. && ada_is_redundant_index_type_desc (type0, index_type_desc))
  7004. {
  7005. /* Ignore this ___XA parallel type, as it does not bring any
  7006. useful information. This allows us to avoid creating fixed
  7007. versions of the array's index types, which would be identical
  7008. to the original ones. This, in turn, can also help avoid
  7009. the creation of fixed versions of the array itself. */
  7010. index_type_desc = NULL;
  7011. }
  7012. if (index_type_desc == NULL)
  7013. {
  7014. struct type *elt_type0 = ada_check_typedef (TYPE_TARGET_TYPE (type0));
  7015. /* NOTE: elt_type---the fixed version of elt_type0---should never
  7016. depend on the contents of the array in properly constructed
  7017. debugging data. */
  7018. /* Create a fixed version of the array element type.
  7019. We're not providing the address of an element here,
  7020. and thus the actual object value cannot be inspected to do
  7021. the conversion. This should not be a problem, since arrays of
  7022. unconstrained objects are not allowed. In particular, all
  7023. the elements of an array of a tagged type should all be of
  7024. the same type specified in the debugging info. No need to
  7025. consult the object tag. */
  7026. struct type *elt_type = ada_to_fixed_type (elt_type0, 0, 0, dval, 1);
  7027. /* Make sure we always create a new array type when dealing with
  7028. packed array types, since we're going to fix-up the array
  7029. type length and element bitsize a little further down. */
  7030. if (elt_type0 == elt_type && !constrained_packed_array_p)
  7031. result = type0;
  7032. else
  7033. result = create_array_type (alloc_type_copy (type0),
  7034. elt_type, type0->index_type ());
  7035. }
  7036. else
  7037. {
  7038. int i;
  7039. struct type *elt_type0;
  7040. elt_type0 = type0;
  7041. for (i = index_type_desc->num_fields (); i > 0; i -= 1)
  7042. elt_type0 = TYPE_TARGET_TYPE (elt_type0);
  7043. /* NOTE: result---the fixed version of elt_type0---should never
  7044. depend on the contents of the array in properly constructed
  7045. debugging data. */
  7046. /* Create a fixed version of the array element type.
  7047. We're not providing the address of an element here,
  7048. and thus the actual object value cannot be inspected to do
  7049. the conversion. This should not be a problem, since arrays of
  7050. unconstrained objects are not allowed. In particular, all
  7051. the elements of an array of a tagged type should all be of
  7052. the same type specified in the debugging info. No need to
  7053. consult the object tag. */
  7054. result =
  7055. ada_to_fixed_type (ada_check_typedef (elt_type0), 0, 0, dval, 1);
  7056. elt_type0 = type0;
  7057. for (i = index_type_desc->num_fields () - 1; i >= 0; i -= 1)
  7058. {
  7059. struct type *range_type =
  7060. to_fixed_range_type (index_type_desc->field (i).type (), dval);
  7061. result = create_array_type (alloc_type_copy (elt_type0),
  7062. result, range_type);
  7063. elt_type0 = TYPE_TARGET_TYPE (elt_type0);
  7064. }
  7065. }
  7066. /* We want to preserve the type name. This can be useful when
  7067. trying to get the type name of a value that has already been
  7068. printed (for instance, if the user did "print VAR; whatis $". */
  7069. result->set_name (type0->name ());
  7070. if (constrained_packed_array_p)
  7071. {
  7072. /* So far, the resulting type has been created as if the original
  7073. type was a regular (non-packed) array type. As a result, the
  7074. bitsize of the array elements needs to be set again, and the array
  7075. length needs to be recomputed based on that bitsize. */
  7076. int len = TYPE_LENGTH (result) / TYPE_LENGTH (TYPE_TARGET_TYPE (result));
  7077. int elt_bitsize = TYPE_FIELD_BITSIZE (type0, 0);
  7078. TYPE_FIELD_BITSIZE (result, 0) = TYPE_FIELD_BITSIZE (type0, 0);
  7079. TYPE_LENGTH (result) = len * elt_bitsize / HOST_CHAR_BIT;
  7080. if (TYPE_LENGTH (result) * HOST_CHAR_BIT < len * elt_bitsize)
  7081. TYPE_LENGTH (result)++;
  7082. }
  7083. result->set_is_fixed_instance (true);
  7084. return result;
  7085. }
  7086. /* A standard type (containing no dynamically sized components)
  7087. corresponding to TYPE for the value (TYPE, VALADDR, ADDRESS)
  7088. DVAL describes a record containing any discriminants used in TYPE0,
  7089. and may be NULL if there are none, or if the object of type TYPE at
  7090. ADDRESS or in VALADDR contains these discriminants.
  7091. If CHECK_TAG is not null, in the case of tagged types, this function
  7092. attempts to locate the object's tag and use it to compute the actual
  7093. type. However, when ADDRESS is null, we cannot use it to determine the
  7094. location of the tag, and therefore compute the tagged type's actual type.
  7095. So we return the tagged type without consulting the tag. */
  7096. static struct type *
  7097. ada_to_fixed_type_1 (struct type *type, const gdb_byte *valaddr,
  7098. CORE_ADDR address, struct value *dval, int check_tag)
  7099. {
  7100. type = ada_check_typedef (type);
  7101. /* Only un-fixed types need to be handled here. */
  7102. if (!HAVE_GNAT_AUX_INFO (type))
  7103. return type;
  7104. switch (type->code ())
  7105. {
  7106. default:
  7107. return type;
  7108. case TYPE_CODE_STRUCT:
  7109. {
  7110. struct type *static_type = to_static_fixed_type (type);
  7111. struct type *fixed_record_type =
  7112. to_fixed_record_type (type, valaddr, address, NULL);
  7113. /* If STATIC_TYPE is a tagged type and we know the object's address,
  7114. then we can determine its tag, and compute the object's actual
  7115. type from there. Note that we have to use the fixed record
  7116. type (the parent part of the record may have dynamic fields
  7117. and the way the location of _tag is expressed may depend on
  7118. them). */
  7119. if (check_tag && address != 0 && ada_is_tagged_type (static_type, 0))
  7120. {
  7121. struct value *tag =
  7122. value_tag_from_contents_and_address
  7123. (fixed_record_type,
  7124. valaddr,
  7125. address);
  7126. struct type *real_type = type_from_tag (tag);
  7127. struct value *obj =
  7128. value_from_contents_and_address (fixed_record_type,
  7129. valaddr,
  7130. address);
  7131. fixed_record_type = value_type (obj);
  7132. if (real_type != NULL)
  7133. return to_fixed_record_type
  7134. (real_type, NULL,
  7135. value_address (ada_tag_value_at_base_address (obj)), NULL);
  7136. }
  7137. /* Check to see if there is a parallel ___XVZ variable.
  7138. If there is, then it provides the actual size of our type. */
  7139. else if (ada_type_name (fixed_record_type) != NULL)
  7140. {
  7141. const char *name = ada_type_name (fixed_record_type);
  7142. char *xvz_name
  7143. = (char *) alloca (strlen (name) + 7 /* "___XVZ\0" */);
  7144. bool xvz_found = false;
  7145. LONGEST size;
  7146. xsnprintf (xvz_name, strlen (name) + 7, "%s___XVZ", name);
  7147. try
  7148. {
  7149. xvz_found = get_int_var_value (xvz_name, size);
  7150. }
  7151. catch (const gdb_exception_error &except)
  7152. {
  7153. /* We found the variable, but somehow failed to read
  7154. its value. Rethrow the same error, but with a little
  7155. bit more information, to help the user understand
  7156. what went wrong (Eg: the variable might have been
  7157. optimized out). */
  7158. throw_error (except.error,
  7159. _("unable to read value of %s (%s)"),
  7160. xvz_name, except.what ());
  7161. }
  7162. if (xvz_found && TYPE_LENGTH (fixed_record_type) != size)
  7163. {
  7164. fixed_record_type = copy_type (fixed_record_type);
  7165. TYPE_LENGTH (fixed_record_type) = size;
  7166. /* The FIXED_RECORD_TYPE may have be a stub. We have
  7167. observed this when the debugging info is STABS, and
  7168. apparently it is something that is hard to fix.
  7169. In practice, we don't need the actual type definition
  7170. at all, because the presence of the XVZ variable allows us
  7171. to assume that there must be a XVS type as well, which we
  7172. should be able to use later, when we need the actual type
  7173. definition.
  7174. In the meantime, pretend that the "fixed" type we are
  7175. returning is NOT a stub, because this can cause trouble
  7176. when using this type to create new types targeting it.
  7177. Indeed, the associated creation routines often check
  7178. whether the target type is a stub and will try to replace
  7179. it, thus using a type with the wrong size. This, in turn,
  7180. might cause the new type to have the wrong size too.
  7181. Consider the case of an array, for instance, where the size
  7182. of the array is computed from the number of elements in
  7183. our array multiplied by the size of its element. */
  7184. fixed_record_type->set_is_stub (false);
  7185. }
  7186. }
  7187. return fixed_record_type;
  7188. }
  7189. case TYPE_CODE_ARRAY:
  7190. return to_fixed_array_type (type, dval, 1);
  7191. case TYPE_CODE_UNION:
  7192. if (dval == NULL)
  7193. return type;
  7194. else
  7195. return to_fixed_variant_branch_type (type, valaddr, address, dval);
  7196. }
  7197. }
  7198. /* The same as ada_to_fixed_type_1, except that it preserves the type
  7199. if it is a TYPE_CODE_TYPEDEF of a type that is already fixed.
  7200. The typedef layer needs be preserved in order to differentiate between
  7201. arrays and array pointers when both types are implemented using the same
  7202. fat pointer. In the array pointer case, the pointer is encoded as
  7203. a typedef of the pointer type. For instance, considering:
  7204. type String_Access is access String;
  7205. S1 : String_Access := null;
  7206. To the debugger, S1 is defined as a typedef of type String. But
  7207. to the user, it is a pointer. So if the user tries to print S1,
  7208. we should not dereference the array, but print the array address
  7209. instead.
  7210. If we didn't preserve the typedef layer, we would lose the fact that
  7211. the type is to be presented as a pointer (needs de-reference before
  7212. being printed). And we would also use the source-level type name. */
  7213. struct type *
  7214. ada_to_fixed_type (struct type *type, const gdb_byte *valaddr,
  7215. CORE_ADDR address, struct value *dval, int check_tag)
  7216. {
  7217. struct type *fixed_type =
  7218. ada_to_fixed_type_1 (type, valaddr, address, dval, check_tag);
  7219. /* If TYPE is a typedef and its target type is the same as the FIXED_TYPE,
  7220. then preserve the typedef layer.
  7221. Implementation note: We can only check the main-type portion of
  7222. the TYPE and FIXED_TYPE, because eliminating the typedef layer
  7223. from TYPE now returns a type that has the same instance flags
  7224. as TYPE. For instance, if TYPE is a "typedef const", and its
  7225. target type is a "struct", then the typedef elimination will return
  7226. a "const" version of the target type. See check_typedef for more
  7227. details about how the typedef layer elimination is done.
  7228. brobecker/2010-11-19: It seems to me that the only case where it is
  7229. useful to preserve the typedef layer is when dealing with fat pointers.
  7230. Perhaps, we could add a check for that and preserve the typedef layer
  7231. only in that situation. But this seems unnecessary so far, probably
  7232. because we call check_typedef/ada_check_typedef pretty much everywhere.
  7233. */
  7234. if (type->code () == TYPE_CODE_TYPEDEF
  7235. && (TYPE_MAIN_TYPE (ada_typedef_target_type (type))
  7236. == TYPE_MAIN_TYPE (fixed_type)))
  7237. return type;
  7238. return fixed_type;
  7239. }
  7240. /* A standard (static-sized) type corresponding as well as possible to
  7241. TYPE0, but based on no runtime data. */
  7242. static struct type *
  7243. to_static_fixed_type (struct type *type0)
  7244. {
  7245. struct type *type;
  7246. if (type0 == NULL)
  7247. return NULL;
  7248. if (type0->is_fixed_instance ())
  7249. return type0;
  7250. type0 = ada_check_typedef (type0);
  7251. switch (type0->code ())
  7252. {
  7253. default:
  7254. return type0;
  7255. case TYPE_CODE_STRUCT:
  7256. type = dynamic_template_type (type0);
  7257. if (type != NULL)
  7258. return template_to_static_fixed_type (type);
  7259. else
  7260. return template_to_static_fixed_type (type0);
  7261. case TYPE_CODE_UNION:
  7262. type = ada_find_parallel_type (type0, "___XVU");
  7263. if (type != NULL)
  7264. return template_to_static_fixed_type (type);
  7265. else
  7266. return template_to_static_fixed_type (type0);
  7267. }
  7268. }
  7269. /* A static approximation of TYPE with all type wrappers removed. */
  7270. static struct type *
  7271. static_unwrap_type (struct type *type)
  7272. {
  7273. if (ada_is_aligner_type (type))
  7274. {
  7275. struct type *type1 = ada_check_typedef (type)->field (0).type ();
  7276. if (ada_type_name (type1) == NULL)
  7277. type1->set_name (ada_type_name (type));
  7278. return static_unwrap_type (type1);
  7279. }
  7280. else
  7281. {
  7282. struct type *raw_real_type = ada_get_base_type (type);
  7283. if (raw_real_type == type)
  7284. return type;
  7285. else
  7286. return to_static_fixed_type (raw_real_type);
  7287. }
  7288. }
  7289. /* In some cases, incomplete and private types require
  7290. cross-references that are not resolved as records (for example,
  7291. type Foo;
  7292. type FooP is access Foo;
  7293. V: FooP;
  7294. type Foo is array ...;
  7295. ). In these cases, since there is no mechanism for producing
  7296. cross-references to such types, we instead substitute for FooP a
  7297. stub enumeration type that is nowhere resolved, and whose tag is
  7298. the name of the actual type. Call these types "non-record stubs". */
  7299. /* A type equivalent to TYPE that is not a non-record stub, if one
  7300. exists, otherwise TYPE. */
  7301. struct type *
  7302. ada_check_typedef (struct type *type)
  7303. {
  7304. if (type == NULL)
  7305. return NULL;
  7306. /* If our type is an access to an unconstrained array, which is encoded
  7307. as a TYPE_CODE_TYPEDEF of a fat pointer, then we're done.
  7308. We don't want to strip the TYPE_CODE_TYPDEF layer, because this is
  7309. what allows us to distinguish between fat pointers that represent
  7310. array types, and fat pointers that represent array access types
  7311. (in both cases, the compiler implements them as fat pointers). */
  7312. if (ada_is_access_to_unconstrained_array (type))
  7313. return type;
  7314. type = check_typedef (type);
  7315. if (type == NULL || type->code () != TYPE_CODE_ENUM
  7316. || !type->is_stub ()
  7317. || type->name () == NULL)
  7318. return type;
  7319. else
  7320. {
  7321. const char *name = type->name ();
  7322. struct type *type1 = ada_find_any_type (name);
  7323. if (type1 == NULL)
  7324. return type;
  7325. /* TYPE1 might itself be a TYPE_CODE_TYPEDEF (this can happen with
  7326. stubs pointing to arrays, as we don't create symbols for array
  7327. types, only for the typedef-to-array types). If that's the case,
  7328. strip the typedef layer. */
  7329. if (type1->code () == TYPE_CODE_TYPEDEF)
  7330. type1 = ada_check_typedef (type1);
  7331. return type1;
  7332. }
  7333. }
  7334. /* A value representing the data at VALADDR/ADDRESS as described by
  7335. type TYPE0, but with a standard (static-sized) type that correctly
  7336. describes it. If VAL0 is not NULL and TYPE0 already is a standard
  7337. type, then return VAL0 [this feature is simply to avoid redundant
  7338. creation of struct values]. */
  7339. static struct value *
  7340. ada_to_fixed_value_create (struct type *type0, CORE_ADDR address,
  7341. struct value *val0)
  7342. {
  7343. struct type *type = ada_to_fixed_type (type0, 0, address, NULL, 1);
  7344. if (type == type0 && val0 != NULL)
  7345. return val0;
  7346. if (VALUE_LVAL (val0) != lval_memory)
  7347. {
  7348. /* Our value does not live in memory; it could be a convenience
  7349. variable, for instance. Create a not_lval value using val0's
  7350. contents. */
  7351. return value_from_contents (type, value_contents (val0).data ());
  7352. }
  7353. return value_from_contents_and_address (type, 0, address);
  7354. }
  7355. /* A value representing VAL, but with a standard (static-sized) type
  7356. that correctly describes it. Does not necessarily create a new
  7357. value. */
  7358. struct value *
  7359. ada_to_fixed_value (struct value *val)
  7360. {
  7361. val = unwrap_value (val);
  7362. val = ada_to_fixed_value_create (value_type (val), value_address (val), val);
  7363. return val;
  7364. }
  7365. /* Attributes */
  7366. /* Table mapping attribute numbers to names.
  7367. NOTE: Keep up to date with enum ada_attribute definition in ada-lang.h. */
  7368. static const char * const attribute_names[] = {
  7369. "<?>",
  7370. "first",
  7371. "last",
  7372. "length",
  7373. "image",
  7374. "max",
  7375. "min",
  7376. "modulus",
  7377. "pos",
  7378. "size",
  7379. "tag",
  7380. "val",
  7381. 0
  7382. };
  7383. static const char *
  7384. ada_attribute_name (enum exp_opcode n)
  7385. {
  7386. if (n >= OP_ATR_FIRST && n <= (int) OP_ATR_VAL)
  7387. return attribute_names[n - OP_ATR_FIRST + 1];
  7388. else
  7389. return attribute_names[0];
  7390. }
  7391. /* Evaluate the 'POS attribute applied to ARG. */
  7392. static LONGEST
  7393. pos_atr (struct value *arg)
  7394. {
  7395. struct value *val = coerce_ref (arg);
  7396. struct type *type = value_type (val);
  7397. if (!discrete_type_p (type))
  7398. error (_("'POS only defined on discrete types"));
  7399. gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
  7400. if (!result.has_value ())
  7401. error (_("enumeration value is invalid: can't find 'POS"));
  7402. return *result;
  7403. }
  7404. struct value *
  7405. ada_pos_atr (struct type *expect_type,
  7406. struct expression *exp,
  7407. enum noside noside, enum exp_opcode op,
  7408. struct value *arg)
  7409. {
  7410. struct type *type = builtin_type (exp->gdbarch)->builtin_int;
  7411. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  7412. return value_zero (type, not_lval);
  7413. return value_from_longest (type, pos_atr (arg));
  7414. }
  7415. /* Evaluate the TYPE'VAL attribute applied to ARG. */
  7416. static struct value *
  7417. val_atr (struct type *type, LONGEST val)
  7418. {
  7419. gdb_assert (discrete_type_p (type));
  7420. if (type->code () == TYPE_CODE_RANGE)
  7421. type = TYPE_TARGET_TYPE (type);
  7422. if (type->code () == TYPE_CODE_ENUM)
  7423. {
  7424. if (val < 0 || val >= type->num_fields ())
  7425. error (_("argument to 'VAL out of range"));
  7426. val = type->field (val).loc_enumval ();
  7427. }
  7428. return value_from_longest (type, val);
  7429. }
  7430. struct value *
  7431. ada_val_atr (enum noside noside, struct type *type, struct value *arg)
  7432. {
  7433. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  7434. return value_zero (type, not_lval);
  7435. if (!discrete_type_p (type))
  7436. error (_("'VAL only defined on discrete types"));
  7437. if (!integer_type_p (value_type (arg)))
  7438. error (_("'VAL requires integral argument"));
  7439. return val_atr (type, value_as_long (arg));
  7440. }
  7441. /* Evaluation */
  7442. /* True if TYPE appears to be an Ada character type.
  7443. [At the moment, this is true only for Character and Wide_Character;
  7444. It is a heuristic test that could stand improvement]. */
  7445. bool
  7446. ada_is_character_type (struct type *type)
  7447. {
  7448. const char *name;
  7449. /* If the type code says it's a character, then assume it really is,
  7450. and don't check any further. */
  7451. if (type->code () == TYPE_CODE_CHAR)
  7452. return true;
  7453. /* Otherwise, assume it's a character type iff it is a discrete type
  7454. with a known character type name. */
  7455. name = ada_type_name (type);
  7456. return (name != NULL
  7457. && (type->code () == TYPE_CODE_INT
  7458. || type->code () == TYPE_CODE_RANGE)
  7459. && (strcmp (name, "character") == 0
  7460. || strcmp (name, "wide_character") == 0
  7461. || strcmp (name, "wide_wide_character") == 0
  7462. || strcmp (name, "unsigned char") == 0));
  7463. }
  7464. /* True if TYPE appears to be an Ada string type. */
  7465. bool
  7466. ada_is_string_type (struct type *type)
  7467. {
  7468. type = ada_check_typedef (type);
  7469. if (type != NULL
  7470. && type->code () != TYPE_CODE_PTR
  7471. && (ada_is_simple_array_type (type)
  7472. || ada_is_array_descriptor_type (type))
  7473. && ada_array_arity (type) == 1)
  7474. {
  7475. struct type *elttype = ada_array_element_type (type, 1);
  7476. return ada_is_character_type (elttype);
  7477. }
  7478. else
  7479. return false;
  7480. }
  7481. /* The compiler sometimes provides a parallel XVS type for a given
  7482. PAD type. Normally, it is safe to follow the PAD type directly,
  7483. but older versions of the compiler have a bug that causes the offset
  7484. of its "F" field to be wrong. Following that field in that case
  7485. would lead to incorrect results, but this can be worked around
  7486. by ignoring the PAD type and using the associated XVS type instead.
  7487. Set to True if the debugger should trust the contents of PAD types.
  7488. Otherwise, ignore the PAD type if there is a parallel XVS type. */
  7489. static bool trust_pad_over_xvs = true;
  7490. /* True if TYPE is a struct type introduced by the compiler to force the
  7491. alignment of a value. Such types have a single field with a
  7492. distinctive name. */
  7493. int
  7494. ada_is_aligner_type (struct type *type)
  7495. {
  7496. type = ada_check_typedef (type);
  7497. if (!trust_pad_over_xvs && ada_find_parallel_type (type, "___XVS") != NULL)
  7498. return 0;
  7499. return (type->code () == TYPE_CODE_STRUCT
  7500. && type->num_fields () == 1
  7501. && strcmp (type->field (0).name (), "F") == 0);
  7502. }
  7503. /* If there is an ___XVS-convention type parallel to SUBTYPE, return
  7504. the parallel type. */
  7505. struct type *
  7506. ada_get_base_type (struct type *raw_type)
  7507. {
  7508. struct type *real_type_namer;
  7509. struct type *raw_real_type;
  7510. if (raw_type == NULL || raw_type->code () != TYPE_CODE_STRUCT)
  7511. return raw_type;
  7512. if (ada_is_aligner_type (raw_type))
  7513. /* The encoding specifies that we should always use the aligner type.
  7514. So, even if this aligner type has an associated XVS type, we should
  7515. simply ignore it.
  7516. According to the compiler gurus, an XVS type parallel to an aligner
  7517. type may exist because of a stabs limitation. In stabs, aligner
  7518. types are empty because the field has a variable-sized type, and
  7519. thus cannot actually be used as an aligner type. As a result,
  7520. we need the associated parallel XVS type to decode the type.
  7521. Since the policy in the compiler is to not change the internal
  7522. representation based on the debugging info format, we sometimes
  7523. end up having a redundant XVS type parallel to the aligner type. */
  7524. return raw_type;
  7525. real_type_namer = ada_find_parallel_type (raw_type, "___XVS");
  7526. if (real_type_namer == NULL
  7527. || real_type_namer->code () != TYPE_CODE_STRUCT
  7528. || real_type_namer->num_fields () != 1)
  7529. return raw_type;
  7530. if (real_type_namer->field (0).type ()->code () != TYPE_CODE_REF)
  7531. {
  7532. /* This is an older encoding form where the base type needs to be
  7533. looked up by name. We prefer the newer encoding because it is
  7534. more efficient. */
  7535. raw_real_type = ada_find_any_type (real_type_namer->field (0).name ());
  7536. if (raw_real_type == NULL)
  7537. return raw_type;
  7538. else
  7539. return raw_real_type;
  7540. }
  7541. /* The field in our XVS type is a reference to the base type. */
  7542. return TYPE_TARGET_TYPE (real_type_namer->field (0).type ());
  7543. }
  7544. /* The type of value designated by TYPE, with all aligners removed. */
  7545. struct type *
  7546. ada_aligned_type (struct type *type)
  7547. {
  7548. if (ada_is_aligner_type (type))
  7549. return ada_aligned_type (type->field (0).type ());
  7550. else
  7551. return ada_get_base_type (type);
  7552. }
  7553. /* The address of the aligned value in an object at address VALADDR
  7554. having type TYPE. Assumes ada_is_aligner_type (TYPE). */
  7555. const gdb_byte *
  7556. ada_aligned_value_addr (struct type *type, const gdb_byte *valaddr)
  7557. {
  7558. if (ada_is_aligner_type (type))
  7559. return ada_aligned_value_addr
  7560. (type->field (0).type (),
  7561. valaddr + type->field (0).loc_bitpos () / TARGET_CHAR_BIT);
  7562. else
  7563. return valaddr;
  7564. }
  7565. /* The printed representation of an enumeration literal with encoded
  7566. name NAME. The value is good to the next call of ada_enum_name. */
  7567. const char *
  7568. ada_enum_name (const char *name)
  7569. {
  7570. static std::string storage;
  7571. const char *tmp;
  7572. /* First, unqualify the enumeration name:
  7573. 1. Search for the last '.' character. If we find one, then skip
  7574. all the preceding characters, the unqualified name starts
  7575. right after that dot.
  7576. 2. Otherwise, we may be debugging on a target where the compiler
  7577. translates dots into "__". Search forward for double underscores,
  7578. but stop searching when we hit an overloading suffix, which is
  7579. of the form "__" followed by digits. */
  7580. tmp = strrchr (name, '.');
  7581. if (tmp != NULL)
  7582. name = tmp + 1;
  7583. else
  7584. {
  7585. while ((tmp = strstr (name, "__")) != NULL)
  7586. {
  7587. if (isdigit (tmp[2]))
  7588. break;
  7589. else
  7590. name = tmp + 2;
  7591. }
  7592. }
  7593. if (name[0] == 'Q')
  7594. {
  7595. int v;
  7596. if (name[1] == 'U' || name[1] == 'W')
  7597. {
  7598. int offset = 2;
  7599. if (name[1] == 'W' && name[2] == 'W')
  7600. {
  7601. /* Also handle the QWW case. */
  7602. ++offset;
  7603. }
  7604. if (sscanf (name + offset, "%x", &v) != 1)
  7605. return name;
  7606. }
  7607. else if (((name[1] >= '0' && name[1] <= '9')
  7608. || (name[1] >= 'a' && name[1] <= 'z'))
  7609. && name[2] == '\0')
  7610. {
  7611. storage = string_printf ("'%c'", name[1]);
  7612. return storage.c_str ();
  7613. }
  7614. else
  7615. return name;
  7616. if (isascii (v) && isprint (v))
  7617. storage = string_printf ("'%c'", v);
  7618. else if (name[1] == 'U')
  7619. storage = string_printf ("'[\"%02x\"]'", v);
  7620. else if (name[2] != 'W')
  7621. storage = string_printf ("'[\"%04x\"]'", v);
  7622. else
  7623. storage = string_printf ("'[\"%06x\"]'", v);
  7624. return storage.c_str ();
  7625. }
  7626. else
  7627. {
  7628. tmp = strstr (name, "__");
  7629. if (tmp == NULL)
  7630. tmp = strstr (name, "$");
  7631. if (tmp != NULL)
  7632. {
  7633. storage = std::string (name, tmp - name);
  7634. return storage.c_str ();
  7635. }
  7636. return name;
  7637. }
  7638. }
  7639. /* If VAL is wrapped in an aligner or subtype wrapper, return the
  7640. value it wraps. */
  7641. static struct value *
  7642. unwrap_value (struct value *val)
  7643. {
  7644. struct type *type = ada_check_typedef (value_type (val));
  7645. if (ada_is_aligner_type (type))
  7646. {
  7647. struct value *v = ada_value_struct_elt (val, "F", 0);
  7648. struct type *val_type = ada_check_typedef (value_type (v));
  7649. if (ada_type_name (val_type) == NULL)
  7650. val_type->set_name (ada_type_name (type));
  7651. return unwrap_value (v);
  7652. }
  7653. else
  7654. {
  7655. struct type *raw_real_type =
  7656. ada_check_typedef (ada_get_base_type (type));
  7657. /* If there is no parallel XVS or XVE type, then the value is
  7658. already unwrapped. Return it without further modification. */
  7659. if ((type == raw_real_type)
  7660. && ada_find_parallel_type (type, "___XVE") == NULL)
  7661. return val;
  7662. return
  7663. coerce_unspec_val_to_type
  7664. (val, ada_to_fixed_type (raw_real_type, 0,
  7665. value_address (val),
  7666. NULL, 1));
  7667. }
  7668. }
  7669. /* Given two array types T1 and T2, return nonzero iff both arrays
  7670. contain the same number of elements. */
  7671. static int
  7672. ada_same_array_size_p (struct type *t1, struct type *t2)
  7673. {
  7674. LONGEST lo1, hi1, lo2, hi2;
  7675. /* Get the array bounds in order to verify that the size of
  7676. the two arrays match. */
  7677. if (!get_array_bounds (t1, &lo1, &hi1)
  7678. || !get_array_bounds (t2, &lo2, &hi2))
  7679. error (_("unable to determine array bounds"));
  7680. /* To make things easier for size comparison, normalize a bit
  7681. the case of empty arrays by making sure that the difference
  7682. between upper bound and lower bound is always -1. */
  7683. if (lo1 > hi1)
  7684. hi1 = lo1 - 1;
  7685. if (lo2 > hi2)
  7686. hi2 = lo2 - 1;
  7687. return (hi1 - lo1 == hi2 - lo2);
  7688. }
  7689. /* Assuming that VAL is an array of integrals, and TYPE represents
  7690. an array with the same number of elements, but with wider integral
  7691. elements, return an array "casted" to TYPE. In practice, this
  7692. means that the returned array is built by casting each element
  7693. of the original array into TYPE's (wider) element type. */
  7694. static struct value *
  7695. ada_promote_array_of_integrals (struct type *type, struct value *val)
  7696. {
  7697. struct type *elt_type = TYPE_TARGET_TYPE (type);
  7698. LONGEST lo, hi;
  7699. LONGEST i;
  7700. /* Verify that both val and type are arrays of scalars, and
  7701. that the size of val's elements is smaller than the size
  7702. of type's element. */
  7703. gdb_assert (type->code () == TYPE_CODE_ARRAY);
  7704. gdb_assert (is_integral_type (TYPE_TARGET_TYPE (type)));
  7705. gdb_assert (value_type (val)->code () == TYPE_CODE_ARRAY);
  7706. gdb_assert (is_integral_type (TYPE_TARGET_TYPE (value_type (val))));
  7707. gdb_assert (TYPE_LENGTH (TYPE_TARGET_TYPE (type))
  7708. > TYPE_LENGTH (TYPE_TARGET_TYPE (value_type (val))));
  7709. if (!get_array_bounds (type, &lo, &hi))
  7710. error (_("unable to determine array bounds"));
  7711. value *res = allocate_value (type);
  7712. gdb::array_view<gdb_byte> res_contents = value_contents_writeable (res);
  7713. /* Promote each array element. */
  7714. for (i = 0; i < hi - lo + 1; i++)
  7715. {
  7716. struct value *elt = value_cast (elt_type, value_subscript (val, lo + i));
  7717. int elt_len = TYPE_LENGTH (elt_type);
  7718. copy (value_contents_all (elt), res_contents.slice (elt_len * i, elt_len));
  7719. }
  7720. return res;
  7721. }
  7722. /* Coerce VAL as necessary for assignment to an lval of type TYPE, and
  7723. return the converted value. */
  7724. static struct value *
  7725. coerce_for_assign (struct type *type, struct value *val)
  7726. {
  7727. struct type *type2 = value_type (val);
  7728. if (type == type2)
  7729. return val;
  7730. type2 = ada_check_typedef (type2);
  7731. type = ada_check_typedef (type);
  7732. if (type2->code () == TYPE_CODE_PTR
  7733. && type->code () == TYPE_CODE_ARRAY)
  7734. {
  7735. val = ada_value_ind (val);
  7736. type2 = value_type (val);
  7737. }
  7738. if (type2->code () == TYPE_CODE_ARRAY
  7739. && type->code () == TYPE_CODE_ARRAY)
  7740. {
  7741. if (!ada_same_array_size_p (type, type2))
  7742. error (_("cannot assign arrays of different length"));
  7743. if (is_integral_type (TYPE_TARGET_TYPE (type))
  7744. && is_integral_type (TYPE_TARGET_TYPE (type2))
  7745. && TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
  7746. < TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
  7747. {
  7748. /* Allow implicit promotion of the array elements to
  7749. a wider type. */
  7750. return ada_promote_array_of_integrals (type, val);
  7751. }
  7752. if (TYPE_LENGTH (TYPE_TARGET_TYPE (type2))
  7753. != TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
  7754. error (_("Incompatible types in assignment"));
  7755. deprecated_set_value_type (val, type);
  7756. }
  7757. return val;
  7758. }
  7759. static struct value *
  7760. ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  7761. {
  7762. struct value *val;
  7763. struct type *type1, *type2;
  7764. LONGEST v, v1, v2;
  7765. arg1 = coerce_ref (arg1);
  7766. arg2 = coerce_ref (arg2);
  7767. type1 = get_base_type (ada_check_typedef (value_type (arg1)));
  7768. type2 = get_base_type (ada_check_typedef (value_type (arg2)));
  7769. if (type1->code () != TYPE_CODE_INT
  7770. || type2->code () != TYPE_CODE_INT)
  7771. return value_binop (arg1, arg2, op);
  7772. switch (op)
  7773. {
  7774. case BINOP_MOD:
  7775. case BINOP_DIV:
  7776. case BINOP_REM:
  7777. break;
  7778. default:
  7779. return value_binop (arg1, arg2, op);
  7780. }
  7781. v2 = value_as_long (arg2);
  7782. if (v2 == 0)
  7783. {
  7784. const char *name;
  7785. if (op == BINOP_MOD)
  7786. name = "mod";
  7787. else if (op == BINOP_DIV)
  7788. name = "/";
  7789. else
  7790. {
  7791. gdb_assert (op == BINOP_REM);
  7792. name = "rem";
  7793. }
  7794. error (_("second operand of %s must not be zero."), name);
  7795. }
  7796. if (type1->is_unsigned () || op == BINOP_MOD)
  7797. return value_binop (arg1, arg2, op);
  7798. v1 = value_as_long (arg1);
  7799. switch (op)
  7800. {
  7801. case BINOP_DIV:
  7802. v = v1 / v2;
  7803. if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
  7804. v += v > 0 ? -1 : 1;
  7805. break;
  7806. case BINOP_REM:
  7807. v = v1 % v2;
  7808. if (v * v1 < 0)
  7809. v -= v2;
  7810. break;
  7811. default:
  7812. /* Should not reach this point. */
  7813. v = 0;
  7814. }
  7815. val = allocate_value (type1);
  7816. store_unsigned_integer (value_contents_raw (val).data (),
  7817. TYPE_LENGTH (value_type (val)),
  7818. type_byte_order (type1), v);
  7819. return val;
  7820. }
  7821. static int
  7822. ada_value_equal (struct value *arg1, struct value *arg2)
  7823. {
  7824. if (ada_is_direct_array_type (value_type (arg1))
  7825. || ada_is_direct_array_type (value_type (arg2)))
  7826. {
  7827. struct type *arg1_type, *arg2_type;
  7828. /* Automatically dereference any array reference before
  7829. we attempt to perform the comparison. */
  7830. arg1 = ada_coerce_ref (arg1);
  7831. arg2 = ada_coerce_ref (arg2);
  7832. arg1 = ada_coerce_to_simple_array (arg1);
  7833. arg2 = ada_coerce_to_simple_array (arg2);
  7834. arg1_type = ada_check_typedef (value_type (arg1));
  7835. arg2_type = ada_check_typedef (value_type (arg2));
  7836. if (arg1_type->code () != TYPE_CODE_ARRAY
  7837. || arg2_type->code () != TYPE_CODE_ARRAY)
  7838. error (_("Attempt to compare array with non-array"));
  7839. /* FIXME: The following works only for types whose
  7840. representations use all bits (no padding or undefined bits)
  7841. and do not have user-defined equality. */
  7842. return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
  7843. && memcmp (value_contents (arg1).data (),
  7844. value_contents (arg2).data (),
  7845. TYPE_LENGTH (arg1_type)) == 0);
  7846. }
  7847. return value_equal (arg1, arg2);
  7848. }
  7849. namespace expr
  7850. {
  7851. bool
  7852. check_objfile (const std::unique_ptr<ada_component> &comp,
  7853. struct objfile *objfile)
  7854. {
  7855. return comp->uses_objfile (objfile);
  7856. }
  7857. /* Assign the result of evaluating ARG starting at *POS to the INDEXth
  7858. component of LHS (a simple array or a record). Does not modify the
  7859. inferior's memory, nor does it modify LHS (unless LHS ==
  7860. CONTAINER). */
  7861. static void
  7862. assign_component (struct value *container, struct value *lhs, LONGEST index,
  7863. struct expression *exp, operation_up &arg)
  7864. {
  7865. scoped_value_mark mark;
  7866. struct value *elt;
  7867. struct type *lhs_type = check_typedef (value_type (lhs));
  7868. if (lhs_type->code () == TYPE_CODE_ARRAY)
  7869. {
  7870. struct type *index_type = builtin_type (exp->gdbarch)->builtin_int;
  7871. struct value *index_val = value_from_longest (index_type, index);
  7872. elt = unwrap_value (ada_value_subscript (lhs, 1, &index_val));
  7873. }
  7874. else
  7875. {
  7876. elt = ada_index_struct_field (index, lhs, 0, value_type (lhs));
  7877. elt = ada_to_fixed_value (elt);
  7878. }
  7879. ada_aggregate_operation *ag_op
  7880. = dynamic_cast<ada_aggregate_operation *> (arg.get ());
  7881. if (ag_op != nullptr)
  7882. ag_op->assign_aggregate (container, elt, exp);
  7883. else
  7884. value_assign_to_component (container, elt,
  7885. arg->evaluate (nullptr, exp,
  7886. EVAL_NORMAL));
  7887. }
  7888. bool
  7889. ada_aggregate_component::uses_objfile (struct objfile *objfile)
  7890. {
  7891. for (const auto &item : m_components)
  7892. if (item->uses_objfile (objfile))
  7893. return true;
  7894. return false;
  7895. }
  7896. void
  7897. ada_aggregate_component::dump (ui_file *stream, int depth)
  7898. {
  7899. gdb_printf (stream, _("%*sAggregate\n"), depth, "");
  7900. for (const auto &item : m_components)
  7901. item->dump (stream, depth + 1);
  7902. }
  7903. void
  7904. ada_aggregate_component::assign (struct value *container,
  7905. struct value *lhs, struct expression *exp,
  7906. std::vector<LONGEST> &indices,
  7907. LONGEST low, LONGEST high)
  7908. {
  7909. for (auto &item : m_components)
  7910. item->assign (container, lhs, exp, indices, low, high);
  7911. }
  7912. /* See ada-exp.h. */
  7913. value *
  7914. ada_aggregate_operation::assign_aggregate (struct value *container,
  7915. struct value *lhs,
  7916. struct expression *exp)
  7917. {
  7918. struct type *lhs_type;
  7919. LONGEST low_index, high_index;
  7920. container = ada_coerce_ref (container);
  7921. if (ada_is_direct_array_type (value_type (container)))
  7922. container = ada_coerce_to_simple_array (container);
  7923. lhs = ada_coerce_ref (lhs);
  7924. if (!deprecated_value_modifiable (lhs))
  7925. error (_("Left operand of assignment is not a modifiable lvalue."));
  7926. lhs_type = check_typedef (value_type (lhs));
  7927. if (ada_is_direct_array_type (lhs_type))
  7928. {
  7929. lhs = ada_coerce_to_simple_array (lhs);
  7930. lhs_type = check_typedef (value_type (lhs));
  7931. low_index = lhs_type->bounds ()->low.const_val ();
  7932. high_index = lhs_type->bounds ()->high.const_val ();
  7933. }
  7934. else if (lhs_type->code () == TYPE_CODE_STRUCT)
  7935. {
  7936. low_index = 0;
  7937. high_index = num_visible_fields (lhs_type) - 1;
  7938. }
  7939. else
  7940. error (_("Left-hand side must be array or record."));
  7941. std::vector<LONGEST> indices (4);
  7942. indices[0] = indices[1] = low_index - 1;
  7943. indices[2] = indices[3] = high_index + 1;
  7944. std::get<0> (m_storage)->assign (container, lhs, exp, indices,
  7945. low_index, high_index);
  7946. return container;
  7947. }
  7948. bool
  7949. ada_positional_component::uses_objfile (struct objfile *objfile)
  7950. {
  7951. return m_op->uses_objfile (objfile);
  7952. }
  7953. void
  7954. ada_positional_component::dump (ui_file *stream, int depth)
  7955. {
  7956. gdb_printf (stream, _("%*sPositional, index = %d\n"),
  7957. depth, "", m_index);
  7958. m_op->dump (stream, depth + 1);
  7959. }
  7960. /* Assign into the component of LHS indexed by the OP_POSITIONAL
  7961. construct, given that the positions are relative to lower bound
  7962. LOW, where HIGH is the upper bound. Record the position in
  7963. INDICES. CONTAINER is as for assign_aggregate. */
  7964. void
  7965. ada_positional_component::assign (struct value *container,
  7966. struct value *lhs, struct expression *exp,
  7967. std::vector<LONGEST> &indices,
  7968. LONGEST low, LONGEST high)
  7969. {
  7970. LONGEST ind = m_index + low;
  7971. if (ind - 1 == high)
  7972. warning (_("Extra components in aggregate ignored."));
  7973. if (ind <= high)
  7974. {
  7975. add_component_interval (ind, ind, indices);
  7976. assign_component (container, lhs, ind, exp, m_op);
  7977. }
  7978. }
  7979. bool
  7980. ada_discrete_range_association::uses_objfile (struct objfile *objfile)
  7981. {
  7982. return m_low->uses_objfile (objfile) || m_high->uses_objfile (objfile);
  7983. }
  7984. void
  7985. ada_discrete_range_association::dump (ui_file *stream, int depth)
  7986. {
  7987. gdb_printf (stream, _("%*sDiscrete range:\n"), depth, "");
  7988. m_low->dump (stream, depth + 1);
  7989. m_high->dump (stream, depth + 1);
  7990. }
  7991. void
  7992. ada_discrete_range_association::assign (struct value *container,
  7993. struct value *lhs,
  7994. struct expression *exp,
  7995. std::vector<LONGEST> &indices,
  7996. LONGEST low, LONGEST high,
  7997. operation_up &op)
  7998. {
  7999. LONGEST lower = value_as_long (m_low->evaluate (nullptr, exp, EVAL_NORMAL));
  8000. LONGEST upper = value_as_long (m_high->evaluate (nullptr, exp, EVAL_NORMAL));
  8001. if (lower <= upper && (lower < low || upper > high))
  8002. error (_("Index in component association out of bounds."));
  8003. add_component_interval (lower, upper, indices);
  8004. while (lower <= upper)
  8005. {
  8006. assign_component (container, lhs, lower, exp, op);
  8007. lower += 1;
  8008. }
  8009. }
  8010. bool
  8011. ada_name_association::uses_objfile (struct objfile *objfile)
  8012. {
  8013. return m_val->uses_objfile (objfile);
  8014. }
  8015. void
  8016. ada_name_association::dump (ui_file *stream, int depth)
  8017. {
  8018. gdb_printf (stream, _("%*sName:\n"), depth, "");
  8019. m_val->dump (stream, depth + 1);
  8020. }
  8021. void
  8022. ada_name_association::assign (struct value *container,
  8023. struct value *lhs,
  8024. struct expression *exp,
  8025. std::vector<LONGEST> &indices,
  8026. LONGEST low, LONGEST high,
  8027. operation_up &op)
  8028. {
  8029. int index;
  8030. if (ada_is_direct_array_type (value_type (lhs)))
  8031. index = longest_to_int (value_as_long (m_val->evaluate (nullptr, exp,
  8032. EVAL_NORMAL)));
  8033. else
  8034. {
  8035. ada_string_operation *strop
  8036. = dynamic_cast<ada_string_operation *> (m_val.get ());
  8037. const char *name;
  8038. if (strop != nullptr)
  8039. name = strop->get_name ();
  8040. else
  8041. {
  8042. ada_var_value_operation *vvo
  8043. = dynamic_cast<ada_var_value_operation *> (m_val.get ());
  8044. if (vvo != nullptr)
  8045. error (_("Invalid record component association."));
  8046. name = vvo->get_symbol ()->natural_name ();
  8047. }
  8048. index = 0;
  8049. if (! find_struct_field (name, value_type (lhs), 0,
  8050. NULL, NULL, NULL, NULL, &index))
  8051. error (_("Unknown component name: %s."), name);
  8052. }
  8053. add_component_interval (index, index, indices);
  8054. assign_component (container, lhs, index, exp, op);
  8055. }
  8056. bool
  8057. ada_choices_component::uses_objfile (struct objfile *objfile)
  8058. {
  8059. if (m_op->uses_objfile (objfile))
  8060. return true;
  8061. for (const auto &item : m_assocs)
  8062. if (item->uses_objfile (objfile))
  8063. return true;
  8064. return false;
  8065. }
  8066. void
  8067. ada_choices_component::dump (ui_file *stream, int depth)
  8068. {
  8069. gdb_printf (stream, _("%*sChoices:\n"), depth, "");
  8070. m_op->dump (stream, depth + 1);
  8071. for (const auto &item : m_assocs)
  8072. item->dump (stream, depth + 1);
  8073. }
  8074. /* Assign into the components of LHS indexed by the OP_CHOICES
  8075. construct at *POS, updating *POS past the construct, given that
  8076. the allowable indices are LOW..HIGH. Record the indices assigned
  8077. to in INDICES. CONTAINER is as for assign_aggregate. */
  8078. void
  8079. ada_choices_component::assign (struct value *container,
  8080. struct value *lhs, struct expression *exp,
  8081. std::vector<LONGEST> &indices,
  8082. LONGEST low, LONGEST high)
  8083. {
  8084. for (auto &item : m_assocs)
  8085. item->assign (container, lhs, exp, indices, low, high, m_op);
  8086. }
  8087. bool
  8088. ada_others_component::uses_objfile (struct objfile *objfile)
  8089. {
  8090. return m_op->uses_objfile (objfile);
  8091. }
  8092. void
  8093. ada_others_component::dump (ui_file *stream, int depth)
  8094. {
  8095. gdb_printf (stream, _("%*sOthers:\n"), depth, "");
  8096. m_op->dump (stream, depth + 1);
  8097. }
  8098. /* Assign the value of the expression in the OP_OTHERS construct in
  8099. EXP at *POS into the components of LHS indexed from LOW .. HIGH that
  8100. have not been previously assigned. The index intervals already assigned
  8101. are in INDICES. CONTAINER is as for assign_aggregate. */
  8102. void
  8103. ada_others_component::assign (struct value *container,
  8104. struct value *lhs, struct expression *exp,
  8105. std::vector<LONGEST> &indices,
  8106. LONGEST low, LONGEST high)
  8107. {
  8108. int num_indices = indices.size ();
  8109. for (int i = 0; i < num_indices - 2; i += 2)
  8110. {
  8111. for (LONGEST ind = indices[i + 1] + 1; ind < indices[i + 2]; ind += 1)
  8112. assign_component (container, lhs, ind, exp, m_op);
  8113. }
  8114. }
  8115. struct value *
  8116. ada_assign_operation::evaluate (struct type *expect_type,
  8117. struct expression *exp,
  8118. enum noside noside)
  8119. {
  8120. value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  8121. ada_aggregate_operation *ag_op
  8122. = dynamic_cast<ada_aggregate_operation *> (std::get<1> (m_storage).get ());
  8123. if (ag_op != nullptr)
  8124. {
  8125. if (noside != EVAL_NORMAL)
  8126. return arg1;
  8127. arg1 = ag_op->assign_aggregate (arg1, arg1, exp);
  8128. return ada_value_assign (arg1, arg1);
  8129. }
  8130. /* Force the evaluation of the rhs ARG2 to the type of the lhs ARG1,
  8131. except if the lhs of our assignment is a convenience variable.
  8132. In the case of assigning to a convenience variable, the lhs
  8133. should be exactly the result of the evaluation of the rhs. */
  8134. struct type *type = value_type (arg1);
  8135. if (VALUE_LVAL (arg1) == lval_internalvar)
  8136. type = NULL;
  8137. value *arg2 = std::get<1> (m_storage)->evaluate (type, exp, noside);
  8138. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8139. return arg1;
  8140. if (VALUE_LVAL (arg1) == lval_internalvar)
  8141. {
  8142. /* Nothing. */
  8143. }
  8144. else
  8145. arg2 = coerce_for_assign (value_type (arg1), arg2);
  8146. return ada_value_assign (arg1, arg2);
  8147. }
  8148. } /* namespace expr */
  8149. /* Add the interval [LOW .. HIGH] to the sorted set of intervals
  8150. [ INDICES[0] .. INDICES[1] ],... The resulting intervals do not
  8151. overlap. */
  8152. static void
  8153. add_component_interval (LONGEST low, LONGEST high,
  8154. std::vector<LONGEST> &indices)
  8155. {
  8156. int i, j;
  8157. int size = indices.size ();
  8158. for (i = 0; i < size; i += 2) {
  8159. if (high >= indices[i] && low <= indices[i + 1])
  8160. {
  8161. int kh;
  8162. for (kh = i + 2; kh < size; kh += 2)
  8163. if (high < indices[kh])
  8164. break;
  8165. if (low < indices[i])
  8166. indices[i] = low;
  8167. indices[i + 1] = indices[kh - 1];
  8168. if (high > indices[i + 1])
  8169. indices[i + 1] = high;
  8170. memcpy (indices.data () + i + 2, indices.data () + kh, size - kh);
  8171. indices.resize (kh - i - 2);
  8172. return;
  8173. }
  8174. else if (high < indices[i])
  8175. break;
  8176. }
  8177. indices.resize (indices.size () + 2);
  8178. for (j = indices.size () - 1; j >= i + 2; j -= 1)
  8179. indices[j] = indices[j - 2];
  8180. indices[i] = low;
  8181. indices[i + 1] = high;
  8182. }
  8183. /* Perform and Ada cast of ARG2 to type TYPE if the type of ARG2
  8184. is different. */
  8185. static struct value *
  8186. ada_value_cast (struct type *type, struct value *arg2)
  8187. {
  8188. if (type == ada_check_typedef (value_type (arg2)))
  8189. return arg2;
  8190. return value_cast (type, arg2);
  8191. }
  8192. /* Evaluating Ada expressions, and printing their result.
  8193. ------------------------------------------------------
  8194. 1. Introduction:
  8195. ----------------
  8196. We usually evaluate an Ada expression in order to print its value.
  8197. We also evaluate an expression in order to print its type, which
  8198. happens during the EVAL_AVOID_SIDE_EFFECTS phase of the evaluation,
  8199. but we'll focus mostly on the EVAL_NORMAL phase. In practice, the
  8200. EVAL_AVOID_SIDE_EFFECTS phase allows us to simplify certain aspects of
  8201. the evaluation compared to the EVAL_NORMAL, but is otherwise very
  8202. similar.
  8203. Evaluating expressions is a little more complicated for Ada entities
  8204. than it is for entities in languages such as C. The main reason for
  8205. this is that Ada provides types whose definition might be dynamic.
  8206. One example of such types is variant records. Or another example
  8207. would be an array whose bounds can only be known at run time.
  8208. The following description is a general guide as to what should be
  8209. done (and what should NOT be done) in order to evaluate an expression
  8210. involving such types, and when. This does not cover how the semantic
  8211. information is encoded by GNAT as this is covered separatly. For the
  8212. document used as the reference for the GNAT encoding, see exp_dbug.ads
  8213. in the GNAT sources.
  8214. Ideally, we should embed each part of this description next to its
  8215. associated code. Unfortunately, the amount of code is so vast right
  8216. now that it's hard to see whether the code handling a particular
  8217. situation might be duplicated or not. One day, when the code is
  8218. cleaned up, this guide might become redundant with the comments
  8219. inserted in the code, and we might want to remove it.
  8220. 2. ``Fixing'' an Entity, the Simple Case:
  8221. -----------------------------------------
  8222. When evaluating Ada expressions, the tricky issue is that they may
  8223. reference entities whose type contents and size are not statically
  8224. known. Consider for instance a variant record:
  8225. type Rec (Empty : Boolean := True) is record
  8226. case Empty is
  8227. when True => null;
  8228. when False => Value : Integer;
  8229. end case;
  8230. end record;
  8231. Yes : Rec := (Empty => False, Value => 1);
  8232. No : Rec := (empty => True);
  8233. The size and contents of that record depends on the value of the
  8234. descriminant (Rec.Empty). At this point, neither the debugging
  8235. information nor the associated type structure in GDB are able to
  8236. express such dynamic types. So what the debugger does is to create
  8237. "fixed" versions of the type that applies to the specific object.
  8238. We also informally refer to this operation as "fixing" an object,
  8239. which means creating its associated fixed type.
  8240. Example: when printing the value of variable "Yes" above, its fixed
  8241. type would look like this:
  8242. type Rec is record
  8243. Empty : Boolean;
  8244. Value : Integer;
  8245. end record;
  8246. On the other hand, if we printed the value of "No", its fixed type
  8247. would become:
  8248. type Rec is record
  8249. Empty : Boolean;
  8250. end record;
  8251. Things become a little more complicated when trying to fix an entity
  8252. with a dynamic type that directly contains another dynamic type,
  8253. such as an array of variant records, for instance. There are
  8254. two possible cases: Arrays, and records.
  8255. 3. ``Fixing'' Arrays:
  8256. ---------------------
  8257. The type structure in GDB describes an array in terms of its bounds,
  8258. and the type of its elements. By design, all elements in the array
  8259. have the same type and we cannot represent an array of variant elements
  8260. using the current type structure in GDB. When fixing an array,
  8261. we cannot fix the array element, as we would potentially need one
  8262. fixed type per element of the array. As a result, the best we can do
  8263. when fixing an array is to produce an array whose bounds and size
  8264. are correct (allowing us to read it from memory), but without having
  8265. touched its element type. Fixing each element will be done later,
  8266. when (if) necessary.
  8267. Arrays are a little simpler to handle than records, because the same
  8268. amount of memory is allocated for each element of the array, even if
  8269. the amount of space actually used by each element differs from element
  8270. to element. Consider for instance the following array of type Rec:
  8271. type Rec_Array is array (1 .. 2) of Rec;
  8272. The actual amount of memory occupied by each element might be different
  8273. from element to element, depending on the value of their discriminant.
  8274. But the amount of space reserved for each element in the array remains
  8275. fixed regardless. So we simply need to compute that size using
  8276. the debugging information available, from which we can then determine
  8277. the array size (we multiply the number of elements of the array by
  8278. the size of each element).
  8279. The simplest case is when we have an array of a constrained element
  8280. type. For instance, consider the following type declarations:
  8281. type Bounded_String (Max_Size : Integer) is
  8282. Length : Integer;
  8283. Buffer : String (1 .. Max_Size);
  8284. end record;
  8285. type Bounded_String_Array is array (1 ..2) of Bounded_String (80);
  8286. In this case, the compiler describes the array as an array of
  8287. variable-size elements (identified by its XVS suffix) for which
  8288. the size can be read in the parallel XVZ variable.
  8289. In the case of an array of an unconstrained element type, the compiler
  8290. wraps the array element inside a private PAD type. This type should not
  8291. be shown to the user, and must be "unwrap"'ed before printing. Note
  8292. that we also use the adjective "aligner" in our code to designate
  8293. these wrapper types.
  8294. In some cases, the size allocated for each element is statically
  8295. known. In that case, the PAD type already has the correct size,
  8296. and the array element should remain unfixed.
  8297. But there are cases when this size is not statically known.
  8298. For instance, assuming that "Five" is an integer variable:
  8299. type Dynamic is array (1 .. Five) of Integer;
  8300. type Wrapper (Has_Length : Boolean := False) is record
  8301. Data : Dynamic;
  8302. case Has_Length is
  8303. when True => Length : Integer;
  8304. when False => null;
  8305. end case;
  8306. end record;
  8307. type Wrapper_Array is array (1 .. 2) of Wrapper;
  8308. Hello : Wrapper_Array := (others => (Has_Length => True,
  8309. Data => (others => 17),
  8310. Length => 1));
  8311. The debugging info would describe variable Hello as being an
  8312. array of a PAD type. The size of that PAD type is not statically
  8313. known, but can be determined using a parallel XVZ variable.
  8314. In that case, a copy of the PAD type with the correct size should
  8315. be used for the fixed array.
  8316. 3. ``Fixing'' record type objects:
  8317. ----------------------------------
  8318. Things are slightly different from arrays in the case of dynamic
  8319. record types. In this case, in order to compute the associated
  8320. fixed type, we need to determine the size and offset of each of
  8321. its components. This, in turn, requires us to compute the fixed
  8322. type of each of these components.
  8323. Consider for instance the example:
  8324. type Bounded_String (Max_Size : Natural) is record
  8325. Str : String (1 .. Max_Size);
  8326. Length : Natural;
  8327. end record;
  8328. My_String : Bounded_String (Max_Size => 10);
  8329. In that case, the position of field "Length" depends on the size
  8330. of field Str, which itself depends on the value of the Max_Size
  8331. discriminant. In order to fix the type of variable My_String,
  8332. we need to fix the type of field Str. Therefore, fixing a variant
  8333. record requires us to fix each of its components.
  8334. However, if a component does not have a dynamic size, the component
  8335. should not be fixed. In particular, fields that use a PAD type
  8336. should not fixed. Here is an example where this might happen
  8337. (assuming type Rec above):
  8338. type Container (Big : Boolean) is record
  8339. First : Rec;
  8340. After : Integer;
  8341. case Big is
  8342. when True => Another : Integer;
  8343. when False => null;
  8344. end case;
  8345. end record;
  8346. My_Container : Container := (Big => False,
  8347. First => (Empty => True),
  8348. After => 42);
  8349. In that example, the compiler creates a PAD type for component First,
  8350. whose size is constant, and then positions the component After just
  8351. right after it. The offset of component After is therefore constant
  8352. in this case.
  8353. The debugger computes the position of each field based on an algorithm
  8354. that uses, among other things, the actual position and size of the field
  8355. preceding it. Let's now imagine that the user is trying to print
  8356. the value of My_Container. If the type fixing was recursive, we would
  8357. end up computing the offset of field After based on the size of the
  8358. fixed version of field First. And since in our example First has
  8359. only one actual field, the size of the fixed type is actually smaller
  8360. than the amount of space allocated to that field, and thus we would
  8361. compute the wrong offset of field After.
  8362. To make things more complicated, we need to watch out for dynamic
  8363. components of variant records (identified by the ___XVL suffix in
  8364. the component name). Even if the target type is a PAD type, the size
  8365. of that type might not be statically known. So the PAD type needs
  8366. to be unwrapped and the resulting type needs to be fixed. Otherwise,
  8367. we might end up with the wrong size for our component. This can be
  8368. observed with the following type declarations:
  8369. type Octal is new Integer range 0 .. 7;
  8370. type Octal_Array is array (Positive range <>) of Octal;
  8371. pragma Pack (Octal_Array);
  8372. type Octal_Buffer (Size : Positive) is record
  8373. Buffer : Octal_Array (1 .. Size);
  8374. Length : Integer;
  8375. end record;
  8376. In that case, Buffer is a PAD type whose size is unset and needs
  8377. to be computed by fixing the unwrapped type.
  8378. 4. When to ``Fix'' un-``Fixed'' sub-elements of an entity:
  8379. ----------------------------------------------------------
  8380. Lastly, when should the sub-elements of an entity that remained unfixed
  8381. thus far, be actually fixed?
  8382. The answer is: Only when referencing that element. For instance
  8383. when selecting one component of a record, this specific component
  8384. should be fixed at that point in time. Or when printing the value
  8385. of a record, each component should be fixed before its value gets
  8386. printed. Similarly for arrays, the element of the array should be
  8387. fixed when printing each element of the array, or when extracting
  8388. one element out of that array. On the other hand, fixing should
  8389. not be performed on the elements when taking a slice of an array!
  8390. Note that one of the side effects of miscomputing the offset and
  8391. size of each field is that we end up also miscomputing the size
  8392. of the containing type. This can have adverse results when computing
  8393. the value of an entity. GDB fetches the value of an entity based
  8394. on the size of its type, and thus a wrong size causes GDB to fetch
  8395. the wrong amount of memory. In the case where the computed size is
  8396. too small, GDB fetches too little data to print the value of our
  8397. entity. Results in this case are unpredictable, as we usually read
  8398. past the buffer containing the data =:-o. */
  8399. /* A helper function for TERNOP_IN_RANGE. */
  8400. static value *
  8401. eval_ternop_in_range (struct type *expect_type, struct expression *exp,
  8402. enum noside noside,
  8403. value *arg1, value *arg2, value *arg3)
  8404. {
  8405. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8406. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
  8407. struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
  8408. return
  8409. value_from_longest (type,
  8410. (value_less (arg1, arg3)
  8411. || value_equal (arg1, arg3))
  8412. && (value_less (arg2, arg1)
  8413. || value_equal (arg2, arg1)));
  8414. }
  8415. /* A helper function for UNOP_NEG. */
  8416. value *
  8417. ada_unop_neg (struct type *expect_type,
  8418. struct expression *exp,
  8419. enum noside noside, enum exp_opcode op,
  8420. struct value *arg1)
  8421. {
  8422. unop_promote (exp->language_defn, exp->gdbarch, &arg1);
  8423. return value_neg (arg1);
  8424. }
  8425. /* A helper function for UNOP_IN_RANGE. */
  8426. value *
  8427. ada_unop_in_range (struct type *expect_type,
  8428. struct expression *exp,
  8429. enum noside noside, enum exp_opcode op,
  8430. struct value *arg1, struct type *type)
  8431. {
  8432. struct value *arg2, *arg3;
  8433. switch (type->code ())
  8434. {
  8435. default:
  8436. lim_warning (_("Membership test incompletely implemented; "
  8437. "always returns true"));
  8438. type = language_bool_type (exp->language_defn, exp->gdbarch);
  8439. return value_from_longest (type, (LONGEST) 1);
  8440. case TYPE_CODE_RANGE:
  8441. arg2 = value_from_longest (type,
  8442. type->bounds ()->low.const_val ());
  8443. arg3 = value_from_longest (type,
  8444. type->bounds ()->high.const_val ());
  8445. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8446. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
  8447. type = language_bool_type (exp->language_defn, exp->gdbarch);
  8448. return
  8449. value_from_longest (type,
  8450. (value_less (arg1, arg3)
  8451. || value_equal (arg1, arg3))
  8452. && (value_less (arg2, arg1)
  8453. || value_equal (arg2, arg1)));
  8454. }
  8455. }
  8456. /* A helper function for OP_ATR_TAG. */
  8457. value *
  8458. ada_atr_tag (struct type *expect_type,
  8459. struct expression *exp,
  8460. enum noside noside, enum exp_opcode op,
  8461. struct value *arg1)
  8462. {
  8463. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8464. return value_zero (ada_tag_type (arg1), not_lval);
  8465. return ada_value_tag (arg1);
  8466. }
  8467. /* A helper function for OP_ATR_SIZE. */
  8468. value *
  8469. ada_atr_size (struct type *expect_type,
  8470. struct expression *exp,
  8471. enum noside noside, enum exp_opcode op,
  8472. struct value *arg1)
  8473. {
  8474. struct type *type = value_type (arg1);
  8475. /* If the argument is a reference, then dereference its type, since
  8476. the user is really asking for the size of the actual object,
  8477. not the size of the pointer. */
  8478. if (type->code () == TYPE_CODE_REF)
  8479. type = TYPE_TARGET_TYPE (type);
  8480. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8481. return value_zero (builtin_type (exp->gdbarch)->builtin_int, not_lval);
  8482. else
  8483. return value_from_longest (builtin_type (exp->gdbarch)->builtin_int,
  8484. TARGET_CHAR_BIT * TYPE_LENGTH (type));
  8485. }
  8486. /* A helper function for UNOP_ABS. */
  8487. value *
  8488. ada_abs (struct type *expect_type,
  8489. struct expression *exp,
  8490. enum noside noside, enum exp_opcode op,
  8491. struct value *arg1)
  8492. {
  8493. unop_promote (exp->language_defn, exp->gdbarch, &arg1);
  8494. if (value_less (arg1, value_zero (value_type (arg1), not_lval)))
  8495. return value_neg (arg1);
  8496. else
  8497. return arg1;
  8498. }
  8499. /* A helper function for BINOP_MUL. */
  8500. value *
  8501. ada_mult_binop (struct type *expect_type,
  8502. struct expression *exp,
  8503. enum noside noside, enum exp_opcode op,
  8504. struct value *arg1, struct value *arg2)
  8505. {
  8506. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8507. {
  8508. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8509. return value_zero (value_type (arg1), not_lval);
  8510. }
  8511. else
  8512. {
  8513. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8514. return ada_value_binop (arg1, arg2, op);
  8515. }
  8516. }
  8517. /* A helper function for BINOP_EQUAL and BINOP_NOTEQUAL. */
  8518. value *
  8519. ada_equal_binop (struct type *expect_type,
  8520. struct expression *exp,
  8521. enum noside noside, enum exp_opcode op,
  8522. struct value *arg1, struct value *arg2)
  8523. {
  8524. int tem;
  8525. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8526. tem = 0;
  8527. else
  8528. {
  8529. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8530. tem = ada_value_equal (arg1, arg2);
  8531. }
  8532. if (op == BINOP_NOTEQUAL)
  8533. tem = !tem;
  8534. struct type *type = language_bool_type (exp->language_defn, exp->gdbarch);
  8535. return value_from_longest (type, (LONGEST) tem);
  8536. }
  8537. /* A helper function for TERNOP_SLICE. */
  8538. value *
  8539. ada_ternop_slice (struct expression *exp,
  8540. enum noside noside,
  8541. struct value *array, struct value *low_bound_val,
  8542. struct value *high_bound_val)
  8543. {
  8544. LONGEST low_bound;
  8545. LONGEST high_bound;
  8546. low_bound_val = coerce_ref (low_bound_val);
  8547. high_bound_val = coerce_ref (high_bound_val);
  8548. low_bound = value_as_long (low_bound_val);
  8549. high_bound = value_as_long (high_bound_val);
  8550. /* If this is a reference to an aligner type, then remove all
  8551. the aligners. */
  8552. if (value_type (array)->code () == TYPE_CODE_REF
  8553. && ada_is_aligner_type (TYPE_TARGET_TYPE (value_type (array))))
  8554. TYPE_TARGET_TYPE (value_type (array)) =
  8555. ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));
  8556. if (ada_is_any_packed_array_type (value_type (array)))
  8557. error (_("cannot slice a packed array"));
  8558. /* If this is a reference to an array or an array lvalue,
  8559. convert to a pointer. */
  8560. if (value_type (array)->code () == TYPE_CODE_REF
  8561. || (value_type (array)->code () == TYPE_CODE_ARRAY
  8562. && VALUE_LVAL (array) == lval_memory))
  8563. array = value_addr (array);
  8564. if (noside == EVAL_AVOID_SIDE_EFFECTS
  8565. && ada_is_array_descriptor_type (ada_check_typedef
  8566. (value_type (array))))
  8567. return empty_array (ada_type_of_array (array, 0), low_bound,
  8568. high_bound);
  8569. array = ada_coerce_to_simple_array_ptr (array);
  8570. /* If we have more than one level of pointer indirection,
  8571. dereference the value until we get only one level. */
  8572. while (value_type (array)->code () == TYPE_CODE_PTR
  8573. && (TYPE_TARGET_TYPE (value_type (array))->code ()
  8574. == TYPE_CODE_PTR))
  8575. array = value_ind (array);
  8576. /* Make sure we really do have an array type before going further,
  8577. to avoid a SEGV when trying to get the index type or the target
  8578. type later down the road if the debug info generated by
  8579. the compiler is incorrect or incomplete. */
  8580. if (!ada_is_simple_array_type (value_type (array)))
  8581. error (_("cannot take slice of non-array"));
  8582. if (ada_check_typedef (value_type (array))->code ()
  8583. == TYPE_CODE_PTR)
  8584. {
  8585. struct type *type0 = ada_check_typedef (value_type (array));
  8586. if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
  8587. return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
  8588. else
  8589. {
  8590. struct type *arr_type0 =
  8591. to_fixed_array_type (TYPE_TARGET_TYPE (type0), NULL, 1);
  8592. return ada_value_slice_from_ptr (array, arr_type0,
  8593. longest_to_int (low_bound),
  8594. longest_to_int (high_bound));
  8595. }
  8596. }
  8597. else if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8598. return array;
  8599. else if (high_bound < low_bound)
  8600. return empty_array (value_type (array), low_bound, high_bound);
  8601. else
  8602. return ada_value_slice (array, longest_to_int (low_bound),
  8603. longest_to_int (high_bound));
  8604. }
  8605. /* A helper function for BINOP_IN_BOUNDS. */
  8606. value *
  8607. ada_binop_in_bounds (struct expression *exp, enum noside noside,
  8608. struct value *arg1, struct value *arg2, int n)
  8609. {
  8610. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8611. {
  8612. struct type *type = language_bool_type (exp->language_defn,
  8613. exp->gdbarch);
  8614. return value_zero (type, not_lval);
  8615. }
  8616. struct type *type = ada_index_type (value_type (arg2), n, "range");
  8617. if (!type)
  8618. type = value_type (arg1);
  8619. value *arg3 = value_from_longest (type, ada_array_bound (arg2, n, 1));
  8620. arg2 = value_from_longest (type, ada_array_bound (arg2, n, 0));
  8621. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8622. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg3);
  8623. type = language_bool_type (exp->language_defn, exp->gdbarch);
  8624. return value_from_longest (type,
  8625. (value_less (arg1, arg3)
  8626. || value_equal (arg1, arg3))
  8627. && (value_less (arg2, arg1)
  8628. || value_equal (arg2, arg1)));
  8629. }
  8630. /* A helper function for some attribute operations. */
  8631. static value *
  8632. ada_unop_atr (struct expression *exp, enum noside noside, enum exp_opcode op,
  8633. struct value *arg1, struct type *type_arg, int tem)
  8634. {
  8635. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8636. {
  8637. if (type_arg == NULL)
  8638. type_arg = value_type (arg1);
  8639. if (ada_is_constrained_packed_array_type (type_arg))
  8640. type_arg = decode_constrained_packed_array_type (type_arg);
  8641. if (!discrete_type_p (type_arg))
  8642. {
  8643. switch (op)
  8644. {
  8645. default: /* Should never happen. */
  8646. error (_("unexpected attribute encountered"));
  8647. case OP_ATR_FIRST:
  8648. case OP_ATR_LAST:
  8649. type_arg = ada_index_type (type_arg, tem,
  8650. ada_attribute_name (op));
  8651. break;
  8652. case OP_ATR_LENGTH:
  8653. type_arg = builtin_type (exp->gdbarch)->builtin_int;
  8654. break;
  8655. }
  8656. }
  8657. return value_zero (type_arg, not_lval);
  8658. }
  8659. else if (type_arg == NULL)
  8660. {
  8661. arg1 = ada_coerce_ref (arg1);
  8662. if (ada_is_constrained_packed_array_type (value_type (arg1)))
  8663. arg1 = ada_coerce_to_simple_array (arg1);
  8664. struct type *type;
  8665. if (op == OP_ATR_LENGTH)
  8666. type = builtin_type (exp->gdbarch)->builtin_int;
  8667. else
  8668. {
  8669. type = ada_index_type (value_type (arg1), tem,
  8670. ada_attribute_name (op));
  8671. if (type == NULL)
  8672. type = builtin_type (exp->gdbarch)->builtin_int;
  8673. }
  8674. switch (op)
  8675. {
  8676. default: /* Should never happen. */
  8677. error (_("unexpected attribute encountered"));
  8678. case OP_ATR_FIRST:
  8679. return value_from_longest
  8680. (type, ada_array_bound (arg1, tem, 0));
  8681. case OP_ATR_LAST:
  8682. return value_from_longest
  8683. (type, ada_array_bound (arg1, tem, 1));
  8684. case OP_ATR_LENGTH:
  8685. return value_from_longest
  8686. (type, ada_array_length (arg1, tem));
  8687. }
  8688. }
  8689. else if (discrete_type_p (type_arg))
  8690. {
  8691. struct type *range_type;
  8692. const char *name = ada_type_name (type_arg);
  8693. range_type = NULL;
  8694. if (name != NULL && type_arg->code () != TYPE_CODE_ENUM)
  8695. range_type = to_fixed_range_type (type_arg, NULL);
  8696. if (range_type == NULL)
  8697. range_type = type_arg;
  8698. switch (op)
  8699. {
  8700. default:
  8701. error (_("unexpected attribute encountered"));
  8702. case OP_ATR_FIRST:
  8703. return value_from_longest
  8704. (range_type, ada_discrete_type_low_bound (range_type));
  8705. case OP_ATR_LAST:
  8706. return value_from_longest
  8707. (range_type, ada_discrete_type_high_bound (range_type));
  8708. case OP_ATR_LENGTH:
  8709. error (_("the 'length attribute applies only to array types"));
  8710. }
  8711. }
  8712. else if (type_arg->code () == TYPE_CODE_FLT)
  8713. error (_("unimplemented type attribute"));
  8714. else
  8715. {
  8716. LONGEST low, high;
  8717. if (ada_is_constrained_packed_array_type (type_arg))
  8718. type_arg = decode_constrained_packed_array_type (type_arg);
  8719. struct type *type;
  8720. if (op == OP_ATR_LENGTH)
  8721. type = builtin_type (exp->gdbarch)->builtin_int;
  8722. else
  8723. {
  8724. type = ada_index_type (type_arg, tem, ada_attribute_name (op));
  8725. if (type == NULL)
  8726. type = builtin_type (exp->gdbarch)->builtin_int;
  8727. }
  8728. switch (op)
  8729. {
  8730. default:
  8731. error (_("unexpected attribute encountered"));
  8732. case OP_ATR_FIRST:
  8733. low = ada_array_bound_from_type (type_arg, tem, 0);
  8734. return value_from_longest (type, low);
  8735. case OP_ATR_LAST:
  8736. high = ada_array_bound_from_type (type_arg, tem, 1);
  8737. return value_from_longest (type, high);
  8738. case OP_ATR_LENGTH:
  8739. low = ada_array_bound_from_type (type_arg, tem, 0);
  8740. high = ada_array_bound_from_type (type_arg, tem, 1);
  8741. return value_from_longest (type, high - low + 1);
  8742. }
  8743. }
  8744. }
  8745. /* A helper function for OP_ATR_MIN and OP_ATR_MAX. */
  8746. struct value *
  8747. ada_binop_minmax (struct type *expect_type,
  8748. struct expression *exp,
  8749. enum noside noside, enum exp_opcode op,
  8750. struct value *arg1, struct value *arg2)
  8751. {
  8752. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8753. return value_zero (value_type (arg1), not_lval);
  8754. else
  8755. {
  8756. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8757. return value_binop (arg1, arg2, op);
  8758. }
  8759. }
  8760. /* A helper function for BINOP_EXP. */
  8761. struct value *
  8762. ada_binop_exp (struct type *expect_type,
  8763. struct expression *exp,
  8764. enum noside noside, enum exp_opcode op,
  8765. struct value *arg1, struct value *arg2)
  8766. {
  8767. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  8768. return value_zero (value_type (arg1), not_lval);
  8769. else
  8770. {
  8771. /* For integer exponentiation operations,
  8772. only promote the first argument. */
  8773. if (is_integral_type (value_type (arg2)))
  8774. unop_promote (exp->language_defn, exp->gdbarch, &arg1);
  8775. else
  8776. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  8777. return value_binop (arg1, arg2, op);
  8778. }
  8779. }
  8780. namespace expr
  8781. {
  8782. /* See ada-exp.h. */
  8783. operation_up
  8784. ada_resolvable::replace (operation_up &&owner,
  8785. struct expression *exp,
  8786. bool deprocedure_p,
  8787. bool parse_completion,
  8788. innermost_block_tracker *tracker,
  8789. struct type *context_type)
  8790. {
  8791. if (resolve (exp, deprocedure_p, parse_completion, tracker, context_type))
  8792. return (make_operation<ada_funcall_operation>
  8793. (std::move (owner),
  8794. std::vector<operation_up> ()));
  8795. return std::move (owner);
  8796. }
  8797. /* Convert the character literal whose value would be VAL to the
  8798. appropriate value of type TYPE, if there is a translation.
  8799. Otherwise return VAL. Hence, in an enumeration type ('A', 'B'),
  8800. the literal 'A' (VAL == 65), returns 0. */
  8801. static LONGEST
  8802. convert_char_literal (struct type *type, LONGEST val)
  8803. {
  8804. char name[12];
  8805. int f;
  8806. if (type == NULL)
  8807. return val;
  8808. type = check_typedef (type);
  8809. if (type->code () != TYPE_CODE_ENUM)
  8810. return val;
  8811. if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
  8812. xsnprintf (name, sizeof (name), "Q%c", (int) val);
  8813. else if (val >= 0 && val < 256)
  8814. xsnprintf (name, sizeof (name), "QU%02x", (unsigned) val);
  8815. else if (val >= 0 && val < 0x10000)
  8816. xsnprintf (name, sizeof (name), "QW%04x", (unsigned) val);
  8817. else
  8818. xsnprintf (name, sizeof (name), "QWW%08lx", (unsigned long) val);
  8819. size_t len = strlen (name);
  8820. for (f = 0; f < type->num_fields (); f += 1)
  8821. {
  8822. /* Check the suffix because an enum constant in a package will
  8823. have a name like "pkg__QUxx". This is safe enough because we
  8824. already have the correct type, and because mangling means
  8825. there can't be clashes. */
  8826. const char *ename = type->field (f).name ();
  8827. size_t elen = strlen (ename);
  8828. if (elen >= len && strcmp (name, ename + elen - len) == 0)
  8829. return type->field (f).loc_enumval ();
  8830. }
  8831. return val;
  8832. }
  8833. value *
  8834. ada_char_operation::evaluate (struct type *expect_type,
  8835. struct expression *exp,
  8836. enum noside noside)
  8837. {
  8838. value *result = long_const_operation::evaluate (expect_type, exp, noside);
  8839. if (expect_type != nullptr)
  8840. result = ada_value_cast (expect_type, result);
  8841. return result;
  8842. }
  8843. /* See ada-exp.h. */
  8844. operation_up
  8845. ada_char_operation::replace (operation_up &&owner,
  8846. struct expression *exp,
  8847. bool deprocedure_p,
  8848. bool parse_completion,
  8849. innermost_block_tracker *tracker,
  8850. struct type *context_type)
  8851. {
  8852. operation_up result = std::move (owner);
  8853. if (context_type != nullptr && context_type->code () == TYPE_CODE_ENUM)
  8854. {
  8855. gdb_assert (result.get () == this);
  8856. std::get<0> (m_storage) = context_type;
  8857. std::get<1> (m_storage)
  8858. = convert_char_literal (context_type, std::get<1> (m_storage));
  8859. }
  8860. return result;
  8861. }
  8862. value *
  8863. ada_wrapped_operation::evaluate (struct type *expect_type,
  8864. struct expression *exp,
  8865. enum noside noside)
  8866. {
  8867. value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
  8868. if (noside == EVAL_NORMAL)
  8869. result = unwrap_value (result);
  8870. /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
  8871. then we need to perform the conversion manually, because
  8872. evaluate_subexp_standard doesn't do it. This conversion is
  8873. necessary in Ada because the different kinds of float/fixed
  8874. types in Ada have different representations.
  8875. Similarly, we need to perform the conversion from OP_LONG
  8876. ourselves. */
  8877. if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
  8878. result = ada_value_cast (expect_type, result);
  8879. return result;
  8880. }
  8881. value *
  8882. ada_string_operation::evaluate (struct type *expect_type,
  8883. struct expression *exp,
  8884. enum noside noside)
  8885. {
  8886. struct type *char_type;
  8887. if (expect_type != nullptr && ada_is_string_type (expect_type))
  8888. char_type = ada_array_element_type (expect_type, 1);
  8889. else
  8890. char_type = language_string_char_type (exp->language_defn, exp->gdbarch);
  8891. const std::string &str = std::get<0> (m_storage);
  8892. const char *encoding;
  8893. switch (TYPE_LENGTH (char_type))
  8894. {
  8895. case 1:
  8896. {
  8897. /* Simply copy over the data -- this isn't perhaps strictly
  8898. correct according to the encodings, but it is gdb's
  8899. historical behavior. */
  8900. struct type *stringtype
  8901. = lookup_array_range_type (char_type, 1, str.length ());
  8902. struct value *val = allocate_value (stringtype);
  8903. memcpy (value_contents_raw (val).data (), str.c_str (),
  8904. str.length ());
  8905. return val;
  8906. }
  8907. case 2:
  8908. if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
  8909. encoding = "UTF-16BE";
  8910. else
  8911. encoding = "UTF-16LE";
  8912. break;
  8913. case 4:
  8914. if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
  8915. encoding = "UTF-32BE";
  8916. else
  8917. encoding = "UTF-32LE";
  8918. break;
  8919. default:
  8920. error (_("unexpected character type size %s"),
  8921. pulongest (TYPE_LENGTH (char_type)));
  8922. }
  8923. auto_obstack converted;
  8924. convert_between_encodings (host_charset (), encoding,
  8925. (const gdb_byte *) str.c_str (),
  8926. str.length (), 1,
  8927. &converted, translit_none);
  8928. struct type *stringtype
  8929. = lookup_array_range_type (char_type, 1,
  8930. obstack_object_size (&converted)
  8931. / TYPE_LENGTH (char_type));
  8932. struct value *val = allocate_value (stringtype);
  8933. memcpy (value_contents_raw (val).data (),
  8934. obstack_base (&converted),
  8935. obstack_object_size (&converted));
  8936. return val;
  8937. }
  8938. value *
  8939. ada_concat_operation::evaluate (struct type *expect_type,
  8940. struct expression *exp,
  8941. enum noside noside)
  8942. {
  8943. /* If one side is a literal, evaluate the other side first so that
  8944. the expected type can be set properly. */
  8945. const operation_up &lhs_expr = std::get<0> (m_storage);
  8946. const operation_up &rhs_expr = std::get<1> (m_storage);
  8947. value *lhs, *rhs;
  8948. if (dynamic_cast<ada_string_operation *> (lhs_expr.get ()) != nullptr)
  8949. {
  8950. rhs = rhs_expr->evaluate (nullptr, exp, noside);
  8951. lhs = lhs_expr->evaluate (value_type (rhs), exp, noside);
  8952. }
  8953. else if (dynamic_cast<ada_char_operation *> (lhs_expr.get ()) != nullptr)
  8954. {
  8955. rhs = rhs_expr->evaluate (nullptr, exp, noside);
  8956. struct type *rhs_type = check_typedef (value_type (rhs));
  8957. struct type *elt_type = nullptr;
  8958. if (rhs_type->code () == TYPE_CODE_ARRAY)
  8959. elt_type = TYPE_TARGET_TYPE (rhs_type);
  8960. lhs = lhs_expr->evaluate (elt_type, exp, noside);
  8961. }
  8962. else if (dynamic_cast<ada_string_operation *> (rhs_expr.get ()) != nullptr)
  8963. {
  8964. lhs = lhs_expr->evaluate (nullptr, exp, noside);
  8965. rhs = rhs_expr->evaluate (value_type (lhs), exp, noside);
  8966. }
  8967. else if (dynamic_cast<ada_char_operation *> (rhs_expr.get ()) != nullptr)
  8968. {
  8969. lhs = lhs_expr->evaluate (nullptr, exp, noside);
  8970. struct type *lhs_type = check_typedef (value_type (lhs));
  8971. struct type *elt_type = nullptr;
  8972. if (lhs_type->code () == TYPE_CODE_ARRAY)
  8973. elt_type = TYPE_TARGET_TYPE (lhs_type);
  8974. rhs = rhs_expr->evaluate (elt_type, exp, noside);
  8975. }
  8976. else
  8977. return concat_operation::evaluate (expect_type, exp, noside);
  8978. return value_concat (lhs, rhs);
  8979. }
  8980. value *
  8981. ada_qual_operation::evaluate (struct type *expect_type,
  8982. struct expression *exp,
  8983. enum noside noside)
  8984. {
  8985. struct type *type = std::get<1> (m_storage);
  8986. return std::get<0> (m_storage)->evaluate (type, exp, noside);
  8987. }
  8988. value *
  8989. ada_ternop_range_operation::evaluate (struct type *expect_type,
  8990. struct expression *exp,
  8991. enum noside noside)
  8992. {
  8993. value *arg0 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  8994. value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  8995. value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
  8996. return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
  8997. }
  8998. value *
  8999. ada_binop_addsub_operation::evaluate (struct type *expect_type,
  9000. struct expression *exp,
  9001. enum noside noside)
  9002. {
  9003. value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
  9004. value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
  9005. auto do_op = [=] (LONGEST x, LONGEST y)
  9006. {
  9007. if (std::get<0> (m_storage) == BINOP_ADD)
  9008. return x + y;
  9009. return x - y;
  9010. };
  9011. if (value_type (arg1)->code () == TYPE_CODE_PTR)
  9012. return (value_from_longest
  9013. (value_type (arg1),
  9014. do_op (value_as_long (arg1), value_as_long (arg2))));
  9015. if (value_type (arg2)->code () == TYPE_CODE_PTR)
  9016. return (value_from_longest
  9017. (value_type (arg2),
  9018. do_op (value_as_long (arg1), value_as_long (arg2))));
  9019. /* Preserve the original type for use by the range case below.
  9020. We cannot cast the result to a reference type, so if ARG1 is
  9021. a reference type, find its underlying type. */
  9022. struct type *type = value_type (arg1);
  9023. while (type->code () == TYPE_CODE_REF)
  9024. type = TYPE_TARGET_TYPE (type);
  9025. binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
  9026. arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
  9027. /* We need to special-case the result with a range.
  9028. This is done for the benefit of "ptype". gdb's Ada support
  9029. historically used the LHS to set the result type here, so
  9030. preserve this behavior. */
  9031. if (type->code () == TYPE_CODE_RANGE)
  9032. arg1 = value_cast (type, arg1);
  9033. return arg1;
  9034. }
  9035. value *
  9036. ada_unop_atr_operation::evaluate (struct type *expect_type,
  9037. struct expression *exp,
  9038. enum noside noside)
  9039. {
  9040. struct type *type_arg = nullptr;
  9041. value *val = nullptr;
  9042. if (std::get<0> (m_storage)->opcode () == OP_TYPE)
  9043. {
  9044. value *tem = std::get<0> (m_storage)->evaluate (nullptr, exp,
  9045. EVAL_AVOID_SIDE_EFFECTS);
  9046. type_arg = value_type (tem);
  9047. }
  9048. else
  9049. val = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  9050. return ada_unop_atr (exp, noside, std::get<1> (m_storage),
  9051. val, type_arg, std::get<2> (m_storage));
  9052. }
  9053. value *
  9054. ada_var_msym_value_operation::evaluate_for_cast (struct type *expect_type,
  9055. struct expression *exp,
  9056. enum noside noside)
  9057. {
  9058. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9059. return value_zero (expect_type, not_lval);
  9060. const bound_minimal_symbol &b = std::get<0> (m_storage);
  9061. value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
  9062. val = ada_value_cast (expect_type, val);
  9063. /* Follow the Ada language semantics that do not allow taking
  9064. an address of the result of a cast (view conversion in Ada). */
  9065. if (VALUE_LVAL (val) == lval_memory)
  9066. {
  9067. if (value_lazy (val))
  9068. value_fetch_lazy (val);
  9069. VALUE_LVAL (val) = not_lval;
  9070. }
  9071. return val;
  9072. }
  9073. value *
  9074. ada_var_value_operation::evaluate_for_cast (struct type *expect_type,
  9075. struct expression *exp,
  9076. enum noside noside)
  9077. {
  9078. value *val = evaluate_var_value (noside,
  9079. std::get<0> (m_storage).block,
  9080. std::get<0> (m_storage).symbol);
  9081. val = ada_value_cast (expect_type, val);
  9082. /* Follow the Ada language semantics that do not allow taking
  9083. an address of the result of a cast (view conversion in Ada). */
  9084. if (VALUE_LVAL (val) == lval_memory)
  9085. {
  9086. if (value_lazy (val))
  9087. value_fetch_lazy (val);
  9088. VALUE_LVAL (val) = not_lval;
  9089. }
  9090. return val;
  9091. }
  9092. value *
  9093. ada_var_value_operation::evaluate (struct type *expect_type,
  9094. struct expression *exp,
  9095. enum noside noside)
  9096. {
  9097. symbol *sym = std::get<0> (m_storage).symbol;
  9098. if (sym->domain () == UNDEF_DOMAIN)
  9099. /* Only encountered when an unresolved symbol occurs in a
  9100. context other than a function call, in which case, it is
  9101. invalid. */
  9102. error (_("Unexpected unresolved symbol, %s, during evaluation"),
  9103. sym->print_name ());
  9104. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9105. {
  9106. struct type *type = static_unwrap_type (sym->type ());
  9107. /* Check to see if this is a tagged type. We also need to handle
  9108. the case where the type is a reference to a tagged type, but
  9109. we have to be careful to exclude pointers to tagged types.
  9110. The latter should be shown as usual (as a pointer), whereas
  9111. a reference should mostly be transparent to the user. */
  9112. if (ada_is_tagged_type (type, 0)
  9113. || (type->code () == TYPE_CODE_REF
  9114. && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0)))
  9115. {
  9116. /* Tagged types are a little special in the fact that the real
  9117. type is dynamic and can only be determined by inspecting the
  9118. object's tag. This means that we need to get the object's
  9119. value first (EVAL_NORMAL) and then extract the actual object
  9120. type from its tag.
  9121. Note that we cannot skip the final step where we extract
  9122. the object type from its tag, because the EVAL_NORMAL phase
  9123. results in dynamic components being resolved into fixed ones.
  9124. This can cause problems when trying to print the type
  9125. description of tagged types whose parent has a dynamic size:
  9126. We use the type name of the "_parent" component in order
  9127. to print the name of the ancestor type in the type description.
  9128. If that component had a dynamic size, the resolution into
  9129. a fixed type would result in the loss of that type name,
  9130. thus preventing us from printing the name of the ancestor
  9131. type in the type description. */
  9132. value *arg1 = evaluate (nullptr, exp, EVAL_NORMAL);
  9133. if (type->code () != TYPE_CODE_REF)
  9134. {
  9135. struct type *actual_type;
  9136. actual_type = type_from_tag (ada_value_tag (arg1));
  9137. if (actual_type == NULL)
  9138. /* If, for some reason, we were unable to determine
  9139. the actual type from the tag, then use the static
  9140. approximation that we just computed as a fallback.
  9141. This can happen if the debugging information is
  9142. incomplete, for instance. */
  9143. actual_type = type;
  9144. return value_zero (actual_type, not_lval);
  9145. }
  9146. else
  9147. {
  9148. /* In the case of a ref, ada_coerce_ref takes care
  9149. of determining the actual type. But the evaluation
  9150. should return a ref as it should be valid to ask
  9151. for its address; so rebuild a ref after coerce. */
  9152. arg1 = ada_coerce_ref (arg1);
  9153. return value_ref (arg1, TYPE_CODE_REF);
  9154. }
  9155. }
  9156. /* Records and unions for which GNAT encodings have been
  9157. generated need to be statically fixed as well.
  9158. Otherwise, non-static fixing produces a type where
  9159. all dynamic properties are removed, which prevents "ptype"
  9160. from being able to completely describe the type.
  9161. For instance, a case statement in a variant record would be
  9162. replaced by the relevant components based on the actual
  9163. value of the discriminants. */
  9164. if ((type->code () == TYPE_CODE_STRUCT
  9165. && dynamic_template_type (type) != NULL)
  9166. || (type->code () == TYPE_CODE_UNION
  9167. && ada_find_parallel_type (type, "___XVU") != NULL))
  9168. return value_zero (to_static_fixed_type (type), not_lval);
  9169. }
  9170. value *arg1 = var_value_operation::evaluate (expect_type, exp, noside);
  9171. return ada_to_fixed_value (arg1);
  9172. }
  9173. bool
  9174. ada_var_value_operation::resolve (struct expression *exp,
  9175. bool deprocedure_p,
  9176. bool parse_completion,
  9177. innermost_block_tracker *tracker,
  9178. struct type *context_type)
  9179. {
  9180. symbol *sym = std::get<0> (m_storage).symbol;
  9181. if (sym->domain () == UNDEF_DOMAIN)
  9182. {
  9183. block_symbol resolved
  9184. = ada_resolve_variable (sym, std::get<0> (m_storage).block,
  9185. context_type, parse_completion,
  9186. deprocedure_p, tracker);
  9187. std::get<0> (m_storage) = resolved;
  9188. }
  9189. if (deprocedure_p
  9190. && (std::get<0> (m_storage).symbol->type ()->code ()
  9191. == TYPE_CODE_FUNC))
  9192. return true;
  9193. return false;
  9194. }
  9195. value *
  9196. ada_atr_val_operation::evaluate (struct type *expect_type,
  9197. struct expression *exp,
  9198. enum noside noside)
  9199. {
  9200. value *arg = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  9201. return ada_val_atr (noside, std::get<0> (m_storage), arg);
  9202. }
  9203. value *
  9204. ada_unop_ind_operation::evaluate (struct type *expect_type,
  9205. struct expression *exp,
  9206. enum noside noside)
  9207. {
  9208. value *arg1 = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
  9209. struct type *type = ada_check_typedef (value_type (arg1));
  9210. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9211. {
  9212. if (ada_is_array_descriptor_type (type))
  9213. /* GDB allows dereferencing GNAT array descriptors. */
  9214. {
  9215. struct type *arrType = ada_type_of_array (arg1, 0);
  9216. if (arrType == NULL)
  9217. error (_("Attempt to dereference null array pointer."));
  9218. return value_at_lazy (arrType, 0);
  9219. }
  9220. else if (type->code () == TYPE_CODE_PTR
  9221. || type->code () == TYPE_CODE_REF
  9222. /* In C you can dereference an array to get the 1st elt. */
  9223. || type->code () == TYPE_CODE_ARRAY)
  9224. {
  9225. /* As mentioned in the OP_VAR_VALUE case, tagged types can
  9226. only be determined by inspecting the object's tag.
  9227. This means that we need to evaluate completely the
  9228. expression in order to get its type. */
  9229. if ((type->code () == TYPE_CODE_REF
  9230. || type->code () == TYPE_CODE_PTR)
  9231. && ada_is_tagged_type (TYPE_TARGET_TYPE (type), 0))
  9232. {
  9233. arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
  9234. EVAL_NORMAL);
  9235. type = value_type (ada_value_ind (arg1));
  9236. }
  9237. else
  9238. {
  9239. type = to_static_fixed_type
  9240. (ada_aligned_type
  9241. (ada_check_typedef (TYPE_TARGET_TYPE (type))));
  9242. }
  9243. return value_zero (type, lval_memory);
  9244. }
  9245. else if (type->code () == TYPE_CODE_INT)
  9246. {
  9247. /* GDB allows dereferencing an int. */
  9248. if (expect_type == NULL)
  9249. return value_zero (builtin_type (exp->gdbarch)->builtin_int,
  9250. lval_memory);
  9251. else
  9252. {
  9253. expect_type =
  9254. to_static_fixed_type (ada_aligned_type (expect_type));
  9255. return value_zero (expect_type, lval_memory);
  9256. }
  9257. }
  9258. else
  9259. error (_("Attempt to take contents of a non-pointer value."));
  9260. }
  9261. arg1 = ada_coerce_ref (arg1); /* FIXME: What is this for?? */
  9262. type = ada_check_typedef (value_type (arg1));
  9263. if (type->code () == TYPE_CODE_INT)
  9264. /* GDB allows dereferencing an int. If we were given
  9265. the expect_type, then use that as the target type.
  9266. Otherwise, assume that the target type is an int. */
  9267. {
  9268. if (expect_type != NULL)
  9269. return ada_value_ind (value_cast (lookup_pointer_type (expect_type),
  9270. arg1));
  9271. else
  9272. return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
  9273. (CORE_ADDR) value_as_address (arg1));
  9274. }
  9275. if (ada_is_array_descriptor_type (type))
  9276. /* GDB allows dereferencing GNAT array descriptors. */
  9277. return ada_coerce_to_simple_array (arg1);
  9278. else
  9279. return ada_value_ind (arg1);
  9280. }
  9281. value *
  9282. ada_structop_operation::evaluate (struct type *expect_type,
  9283. struct expression *exp,
  9284. enum noside noside)
  9285. {
  9286. value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  9287. const char *str = std::get<1> (m_storage).c_str ();
  9288. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9289. {
  9290. struct type *type;
  9291. struct type *type1 = value_type (arg1);
  9292. if (ada_is_tagged_type (type1, 1))
  9293. {
  9294. type = ada_lookup_struct_elt_type (type1, str, 1, 1);
  9295. /* If the field is not found, check if it exists in the
  9296. extension of this object's type. This means that we
  9297. need to evaluate completely the expression. */
  9298. if (type == NULL)
  9299. {
  9300. arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
  9301. EVAL_NORMAL);
  9302. arg1 = ada_value_struct_elt (arg1, str, 0);
  9303. arg1 = unwrap_value (arg1);
  9304. type = value_type (ada_to_fixed_value (arg1));
  9305. }
  9306. }
  9307. else
  9308. type = ada_lookup_struct_elt_type (type1, str, 1, 0);
  9309. return value_zero (ada_aligned_type (type), lval_memory);
  9310. }
  9311. else
  9312. {
  9313. arg1 = ada_value_struct_elt (arg1, str, 0);
  9314. arg1 = unwrap_value (arg1);
  9315. return ada_to_fixed_value (arg1);
  9316. }
  9317. }
  9318. value *
  9319. ada_funcall_operation::evaluate (struct type *expect_type,
  9320. struct expression *exp,
  9321. enum noside noside)
  9322. {
  9323. const std::vector<operation_up> &args_up = std::get<1> (m_storage);
  9324. int nargs = args_up.size ();
  9325. std::vector<value *> argvec (nargs);
  9326. operation_up &callee_op = std::get<0> (m_storage);
  9327. ada_var_value_operation *avv
  9328. = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
  9329. if (avv != nullptr
  9330. && avv->get_symbol ()->domain () == UNDEF_DOMAIN)
  9331. error (_("Unexpected unresolved symbol, %s, during evaluation"),
  9332. avv->get_symbol ()->print_name ());
  9333. value *callee = callee_op->evaluate (nullptr, exp, noside);
  9334. for (int i = 0; i < args_up.size (); ++i)
  9335. argvec[i] = args_up[i]->evaluate (nullptr, exp, noside);
  9336. if (ada_is_constrained_packed_array_type
  9337. (desc_base_type (value_type (callee))))
  9338. callee = ada_coerce_to_simple_array (callee);
  9339. else if (value_type (callee)->code () == TYPE_CODE_ARRAY
  9340. && TYPE_FIELD_BITSIZE (value_type (callee), 0) != 0)
  9341. /* This is a packed array that has already been fixed, and
  9342. therefore already coerced to a simple array. Nothing further
  9343. to do. */
  9344. ;
  9345. else if (value_type (callee)->code () == TYPE_CODE_REF)
  9346. {
  9347. /* Make sure we dereference references so that all the code below
  9348. feels like it's really handling the referenced value. Wrapping
  9349. types (for alignment) may be there, so make sure we strip them as
  9350. well. */
  9351. callee = ada_to_fixed_value (coerce_ref (callee));
  9352. }
  9353. else if (value_type (callee)->code () == TYPE_CODE_ARRAY
  9354. && VALUE_LVAL (callee) == lval_memory)
  9355. callee = value_addr (callee);
  9356. struct type *type = ada_check_typedef (value_type (callee));
  9357. /* Ada allows us to implicitly dereference arrays when subscripting
  9358. them. So, if this is an array typedef (encoding use for array
  9359. access types encoded as fat pointers), strip it now. */
  9360. if (type->code () == TYPE_CODE_TYPEDEF)
  9361. type = ada_typedef_target_type (type);
  9362. if (type->code () == TYPE_CODE_PTR)
  9363. {
  9364. switch (ada_check_typedef (TYPE_TARGET_TYPE (type))->code ())
  9365. {
  9366. case TYPE_CODE_FUNC:
  9367. type = ada_check_typedef (TYPE_TARGET_TYPE (type));
  9368. break;
  9369. case TYPE_CODE_ARRAY:
  9370. break;
  9371. case TYPE_CODE_STRUCT:
  9372. if (noside != EVAL_AVOID_SIDE_EFFECTS)
  9373. callee = ada_value_ind (callee);
  9374. type = ada_check_typedef (TYPE_TARGET_TYPE (type));
  9375. break;
  9376. default:
  9377. error (_("cannot subscript or call something of type `%s'"),
  9378. ada_type_name (value_type (callee)));
  9379. break;
  9380. }
  9381. }
  9382. switch (type->code ())
  9383. {
  9384. case TYPE_CODE_FUNC:
  9385. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9386. {
  9387. if (TYPE_TARGET_TYPE (type) == NULL)
  9388. error_call_unknown_return_type (NULL);
  9389. return allocate_value (TYPE_TARGET_TYPE (type));
  9390. }
  9391. return call_function_by_hand (callee, NULL, argvec);
  9392. case TYPE_CODE_INTERNAL_FUNCTION:
  9393. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9394. /* We don't know anything about what the internal
  9395. function might return, but we have to return
  9396. something. */
  9397. return value_zero (builtin_type (exp->gdbarch)->builtin_int,
  9398. not_lval);
  9399. else
  9400. return call_internal_function (exp->gdbarch, exp->language_defn,
  9401. callee, nargs,
  9402. argvec.data ());
  9403. case TYPE_CODE_STRUCT:
  9404. {
  9405. int arity;
  9406. arity = ada_array_arity (type);
  9407. type = ada_array_element_type (type, nargs);
  9408. if (type == NULL)
  9409. error (_("cannot subscript or call a record"));
  9410. if (arity != nargs)
  9411. error (_("wrong number of subscripts; expecting %d"), arity);
  9412. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9413. return value_zero (ada_aligned_type (type), lval_memory);
  9414. return
  9415. unwrap_value (ada_value_subscript
  9416. (callee, nargs, argvec.data ()));
  9417. }
  9418. case TYPE_CODE_ARRAY:
  9419. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9420. {
  9421. type = ada_array_element_type (type, nargs);
  9422. if (type == NULL)
  9423. error (_("element type of array unknown"));
  9424. else
  9425. return value_zero (ada_aligned_type (type), lval_memory);
  9426. }
  9427. return
  9428. unwrap_value (ada_value_subscript
  9429. (ada_coerce_to_simple_array (callee),
  9430. nargs, argvec.data ()));
  9431. case TYPE_CODE_PTR: /* Pointer to array */
  9432. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  9433. {
  9434. type = to_fixed_array_type (TYPE_TARGET_TYPE (type), NULL, 1);
  9435. type = ada_array_element_type (type, nargs);
  9436. if (type == NULL)
  9437. error (_("element type of array unknown"));
  9438. else
  9439. return value_zero (ada_aligned_type (type), lval_memory);
  9440. }
  9441. return
  9442. unwrap_value (ada_value_ptr_subscript (callee, nargs,
  9443. argvec.data ()));
  9444. default:
  9445. error (_("Attempt to index or call something other than an "
  9446. "array or function"));
  9447. }
  9448. }
  9449. bool
  9450. ada_funcall_operation::resolve (struct expression *exp,
  9451. bool deprocedure_p,
  9452. bool parse_completion,
  9453. innermost_block_tracker *tracker,
  9454. struct type *context_type)
  9455. {
  9456. operation_up &callee_op = std::get<0> (m_storage);
  9457. ada_var_value_operation *avv
  9458. = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
  9459. if (avv == nullptr)
  9460. return false;
  9461. symbol *sym = avv->get_symbol ();
  9462. if (sym->domain () != UNDEF_DOMAIN)
  9463. return false;
  9464. const std::vector<operation_up> &args_up = std::get<1> (m_storage);
  9465. int nargs = args_up.size ();
  9466. std::vector<value *> argvec (nargs);
  9467. for (int i = 0; i < args_up.size (); ++i)
  9468. argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
  9469. const block *block = avv->get_block ();
  9470. block_symbol resolved
  9471. = ada_resolve_funcall (sym, block,
  9472. context_type, parse_completion,
  9473. nargs, argvec.data (),
  9474. tracker);
  9475. std::get<0> (m_storage)
  9476. = make_operation<ada_var_value_operation> (resolved);
  9477. return false;
  9478. }
  9479. bool
  9480. ada_ternop_slice_operation::resolve (struct expression *exp,
  9481. bool deprocedure_p,
  9482. bool parse_completion,
  9483. innermost_block_tracker *tracker,
  9484. struct type *context_type)
  9485. {
  9486. /* Historically this check was done during resolution, so we
  9487. continue that here. */
  9488. value *v = std::get<0> (m_storage)->evaluate (context_type, exp,
  9489. EVAL_AVOID_SIDE_EFFECTS);
  9490. if (ada_is_any_packed_array_type (value_type (v)))
  9491. error (_("cannot slice a packed array"));
  9492. return false;
  9493. }
  9494. }
  9495. /* Return non-zero iff TYPE represents a System.Address type. */
  9496. int
  9497. ada_is_system_address_type (struct type *type)
  9498. {
  9499. return (type->name () && strcmp (type->name (), "system__address") == 0);
  9500. }
  9501. /* Range types */
  9502. /* Scan STR beginning at position K for a discriminant name, and
  9503. return the value of that discriminant field of DVAL in *PX. If
  9504. PNEW_K is not null, put the position of the character beyond the
  9505. name scanned in *PNEW_K. Return 1 if successful; return 0 and do
  9506. not alter *PX and *PNEW_K if unsuccessful. */
  9507. static int
  9508. scan_discrim_bound (const char *str, int k, struct value *dval, LONGEST * px,
  9509. int *pnew_k)
  9510. {
  9511. static std::string storage;
  9512. const char *pstart, *pend, *bound;
  9513. struct value *bound_val;
  9514. if (dval == NULL || str == NULL || str[k] == '\0')
  9515. return 0;
  9516. pstart = str + k;
  9517. pend = strstr (pstart, "__");
  9518. if (pend == NULL)
  9519. {
  9520. bound = pstart;
  9521. k += strlen (bound);
  9522. }
  9523. else
  9524. {
  9525. int len = pend - pstart;
  9526. /* Strip __ and beyond. */
  9527. storage = std::string (pstart, len);
  9528. bound = storage.c_str ();
  9529. k = pend - str;
  9530. }
  9531. bound_val = ada_search_struct_field (bound, dval, 0, value_type (dval));
  9532. if (bound_val == NULL)
  9533. return 0;
  9534. *px = value_as_long (bound_val);
  9535. if (pnew_k != NULL)
  9536. *pnew_k = k;
  9537. return 1;
  9538. }
  9539. /* Value of variable named NAME. Only exact matches are considered.
  9540. If no such variable found, then if ERR_MSG is null, returns 0, and
  9541. otherwise causes an error with message ERR_MSG. */
  9542. static struct value *
  9543. get_var_value (const char *name, const char *err_msg)
  9544. {
  9545. std::string quoted_name = add_angle_brackets (name);
  9546. lookup_name_info lookup_name (quoted_name, symbol_name_match_type::FULL);
  9547. std::vector<struct block_symbol> syms
  9548. = ada_lookup_symbol_list_worker (lookup_name,
  9549. get_selected_block (0),
  9550. VAR_DOMAIN, 1);
  9551. if (syms.size () != 1)
  9552. {
  9553. if (err_msg == NULL)
  9554. return 0;
  9555. else
  9556. error (("%s"), err_msg);
  9557. }
  9558. return value_of_variable (syms[0].symbol, syms[0].block);
  9559. }
  9560. /* Value of integer variable named NAME in the current environment.
  9561. If no such variable is found, returns false. Otherwise, sets VALUE
  9562. to the variable's value and returns true. */
  9563. bool
  9564. get_int_var_value (const char *name, LONGEST &value)
  9565. {
  9566. struct value *var_val = get_var_value (name, 0);
  9567. if (var_val == 0)
  9568. return false;
  9569. value = value_as_long (var_val);
  9570. return true;
  9571. }
  9572. /* Return a range type whose base type is that of the range type named
  9573. NAME in the current environment, and whose bounds are calculated
  9574. from NAME according to the GNAT range encoding conventions.
  9575. Extract discriminant values, if needed, from DVAL. ORIG_TYPE is the
  9576. corresponding range type from debug information; fall back to using it
  9577. if symbol lookup fails. If a new type must be created, allocate it
  9578. like ORIG_TYPE was. The bounds information, in general, is encoded
  9579. in NAME, the base type given in the named range type. */
  9580. static struct type *
  9581. to_fixed_range_type (struct type *raw_type, struct value *dval)
  9582. {
  9583. const char *name;
  9584. struct type *base_type;
  9585. const char *subtype_info;
  9586. gdb_assert (raw_type != NULL);
  9587. gdb_assert (raw_type->name () != NULL);
  9588. if (raw_type->code () == TYPE_CODE_RANGE)
  9589. base_type = TYPE_TARGET_TYPE (raw_type);
  9590. else
  9591. base_type = raw_type;
  9592. name = raw_type->name ();
  9593. subtype_info = strstr (name, "___XD");
  9594. if (subtype_info == NULL)
  9595. {
  9596. LONGEST L = ada_discrete_type_low_bound (raw_type);
  9597. LONGEST U = ada_discrete_type_high_bound (raw_type);
  9598. if (L < INT_MIN || U > INT_MAX)
  9599. return raw_type;
  9600. else
  9601. return create_static_range_type (alloc_type_copy (raw_type), raw_type,
  9602. L, U);
  9603. }
  9604. else
  9605. {
  9606. int prefix_len = subtype_info - name;
  9607. LONGEST L, U;
  9608. struct type *type;
  9609. const char *bounds_str;
  9610. int n;
  9611. subtype_info += 5;
  9612. bounds_str = strchr (subtype_info, '_');
  9613. n = 1;
  9614. if (*subtype_info == 'L')
  9615. {
  9616. if (!ada_scan_number (bounds_str, n, &L, &n)
  9617. && !scan_discrim_bound (bounds_str, n, dval, &L, &n))
  9618. return raw_type;
  9619. if (bounds_str[n] == '_')
  9620. n += 2;
  9621. else if (bounds_str[n] == '.') /* FIXME? SGI Workshop kludge. */
  9622. n += 1;
  9623. subtype_info += 1;
  9624. }
  9625. else
  9626. {
  9627. std::string name_buf = std::string (name, prefix_len) + "___L";
  9628. if (!get_int_var_value (name_buf.c_str (), L))
  9629. {
  9630. lim_warning (_("Unknown lower bound, using 1."));
  9631. L = 1;
  9632. }
  9633. }
  9634. if (*subtype_info == 'U')
  9635. {
  9636. if (!ada_scan_number (bounds_str, n, &U, &n)
  9637. && !scan_discrim_bound (bounds_str, n, dval, &U, &n))
  9638. return raw_type;
  9639. }
  9640. else
  9641. {
  9642. std::string name_buf = std::string (name, prefix_len) + "___U";
  9643. if (!get_int_var_value (name_buf.c_str (), U))
  9644. {
  9645. lim_warning (_("Unknown upper bound, using %ld."), (long) L);
  9646. U = L;
  9647. }
  9648. }
  9649. type = create_static_range_type (alloc_type_copy (raw_type),
  9650. base_type, L, U);
  9651. /* create_static_range_type alters the resulting type's length
  9652. to match the size of the base_type, which is not what we want.
  9653. Set it back to the original range type's length. */
  9654. TYPE_LENGTH (type) = TYPE_LENGTH (raw_type);
  9655. type->set_name (name);
  9656. return type;
  9657. }
  9658. }
  9659. /* True iff NAME is the name of a range type. */
  9660. int
  9661. ada_is_range_type_name (const char *name)
  9662. {
  9663. return (name != NULL && strstr (name, "___XD"));
  9664. }
  9665. /* Modular types */
  9666. /* True iff TYPE is an Ada modular type. */
  9667. int
  9668. ada_is_modular_type (struct type *type)
  9669. {
  9670. struct type *subranged_type = get_base_type (type);
  9671. return (subranged_type != NULL && type->code () == TYPE_CODE_RANGE
  9672. && subranged_type->code () == TYPE_CODE_INT
  9673. && subranged_type->is_unsigned ());
  9674. }
  9675. /* Assuming ada_is_modular_type (TYPE), the modulus of TYPE. */
  9676. ULONGEST
  9677. ada_modulus (struct type *type)
  9678. {
  9679. const dynamic_prop &high = type->bounds ()->high;
  9680. if (high.kind () == PROP_CONST)
  9681. return (ULONGEST) high.const_val () + 1;
  9682. /* If TYPE is unresolved, the high bound might be a location list. Return
  9683. 0, for lack of a better value to return. */
  9684. return 0;
  9685. }
  9686. /* Ada exception catchpoint support:
  9687. ---------------------------------
  9688. We support 3 kinds of exception catchpoints:
  9689. . catchpoints on Ada exceptions
  9690. . catchpoints on unhandled Ada exceptions
  9691. . catchpoints on failed assertions
  9692. Exceptions raised during failed assertions, or unhandled exceptions
  9693. could perfectly be caught with the general catchpoint on Ada exceptions.
  9694. However, we can easily differentiate these two special cases, and having
  9695. the option to distinguish these two cases from the rest can be useful
  9696. to zero-in on certain situations.
  9697. Exception catchpoints are a specialized form of breakpoint,
  9698. since they rely on inserting breakpoints inside known routines
  9699. of the GNAT runtime. The implementation therefore uses a standard
  9700. breakpoint structure of the BP_BREAKPOINT type, but with its own set
  9701. of breakpoint_ops.
  9702. Support in the runtime for exception catchpoints have been changed
  9703. a few times already, and these changes affect the implementation
  9704. of these catchpoints. In order to be able to support several
  9705. variants of the runtime, we use a sniffer that will determine
  9706. the runtime variant used by the program being debugged. */
  9707. /* Ada's standard exceptions.
  9708. The Ada 83 standard also defined Numeric_Error. But there so many
  9709. situations where it was unclear from the Ada 83 Reference Manual
  9710. (RM) whether Constraint_Error or Numeric_Error should be raised,
  9711. that the ARG (Ada Rapporteur Group) eventually issued a Binding
  9712. Interpretation saying that anytime the RM says that Numeric_Error
  9713. should be raised, the implementation may raise Constraint_Error.
  9714. Ada 95 went one step further and pretty much removed Numeric_Error
  9715. from the list of standard exceptions (it made it a renaming of
  9716. Constraint_Error, to help preserve compatibility when compiling
  9717. an Ada83 compiler). As such, we do not include Numeric_Error from
  9718. this list of standard exceptions. */
  9719. static const char * const standard_exc[] = {
  9720. "constraint_error",
  9721. "program_error",
  9722. "storage_error",
  9723. "tasking_error"
  9724. };
  9725. typedef CORE_ADDR (ada_unhandled_exception_name_addr_ftype) (void);
  9726. /* A structure that describes how to support exception catchpoints
  9727. for a given executable. */
  9728. struct exception_support_info
  9729. {
  9730. /* The name of the symbol to break on in order to insert
  9731. a catchpoint on exceptions. */
  9732. const char *catch_exception_sym;
  9733. /* The name of the symbol to break on in order to insert
  9734. a catchpoint on unhandled exceptions. */
  9735. const char *catch_exception_unhandled_sym;
  9736. /* The name of the symbol to break on in order to insert
  9737. a catchpoint on failed assertions. */
  9738. const char *catch_assert_sym;
  9739. /* The name of the symbol to break on in order to insert
  9740. a catchpoint on exception handling. */
  9741. const char *catch_handlers_sym;
  9742. /* Assuming that the inferior just triggered an unhandled exception
  9743. catchpoint, this function is responsible for returning the address
  9744. in inferior memory where the name of that exception is stored.
  9745. Return zero if the address could not be computed. */
  9746. ada_unhandled_exception_name_addr_ftype *unhandled_exception_name_addr;
  9747. };
  9748. static CORE_ADDR ada_unhandled_exception_name_addr (void);
  9749. static CORE_ADDR ada_unhandled_exception_name_addr_from_raise (void);
  9750. /* The following exception support info structure describes how to
  9751. implement exception catchpoints with the latest version of the
  9752. Ada runtime (as of 2019-08-??). */
  9753. static const struct exception_support_info default_exception_support_info =
  9754. {
  9755. "__gnat_debug_raise_exception", /* catch_exception_sym */
  9756. "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
  9757. "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
  9758. "__gnat_begin_handler_v1", /* catch_handlers_sym */
  9759. ada_unhandled_exception_name_addr
  9760. };
  9761. /* The following exception support info structure describes how to
  9762. implement exception catchpoints with an earlier version of the
  9763. Ada runtime (as of 2007-03-06) using v0 of the EH ABI. */
  9764. static const struct exception_support_info exception_support_info_v0 =
  9765. {
  9766. "__gnat_debug_raise_exception", /* catch_exception_sym */
  9767. "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
  9768. "__gnat_debug_raise_assert_failure", /* catch_assert_sym */
  9769. "__gnat_begin_handler", /* catch_handlers_sym */
  9770. ada_unhandled_exception_name_addr
  9771. };
  9772. /* The following exception support info structure describes how to
  9773. implement exception catchpoints with a slightly older version
  9774. of the Ada runtime. */
  9775. static const struct exception_support_info exception_support_info_fallback =
  9776. {
  9777. "__gnat_raise_nodefer_with_msg", /* catch_exception_sym */
  9778. "__gnat_unhandled_exception", /* catch_exception_unhandled_sym */
  9779. "system__assertions__raise_assert_failure", /* catch_assert_sym */
  9780. "__gnat_begin_handler", /* catch_handlers_sym */
  9781. ada_unhandled_exception_name_addr_from_raise
  9782. };
  9783. /* Return nonzero if we can detect the exception support routines
  9784. described in EINFO.
  9785. This function errors out if an abnormal situation is detected
  9786. (for instance, if we find the exception support routines, but
  9787. that support is found to be incomplete). */
  9788. static int
  9789. ada_has_this_exception_support (const struct exception_support_info *einfo)
  9790. {
  9791. struct symbol *sym;
  9792. /* The symbol we're looking up is provided by a unit in the GNAT runtime
  9793. that should be compiled with debugging information. As a result, we
  9794. expect to find that symbol in the symtabs. */
  9795. sym = standard_lookup (einfo->catch_exception_sym, NULL, VAR_DOMAIN);
  9796. if (sym == NULL)
  9797. {
  9798. /* Perhaps we did not find our symbol because the Ada runtime was
  9799. compiled without debugging info, or simply stripped of it.
  9800. It happens on some GNU/Linux distributions for instance, where
  9801. users have to install a separate debug package in order to get
  9802. the runtime's debugging info. In that situation, let the user
  9803. know why we cannot insert an Ada exception catchpoint.
  9804. Note: Just for the purpose of inserting our Ada exception
  9805. catchpoint, we could rely purely on the associated minimal symbol.
  9806. But we would be operating in degraded mode anyway, since we are
  9807. still lacking the debugging info needed later on to extract
  9808. the name of the exception being raised (this name is printed in
  9809. the catchpoint message, and is also used when trying to catch
  9810. a specific exception). We do not handle this case for now. */
  9811. struct bound_minimal_symbol msym
  9812. = lookup_minimal_symbol (einfo->catch_exception_sym, NULL, NULL);
  9813. if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
  9814. error (_("Your Ada runtime appears to be missing some debugging "
  9815. "information.\nCannot insert Ada exception catchpoint "
  9816. "in this configuration."));
  9817. return 0;
  9818. }
  9819. /* Make sure that the symbol we found corresponds to a function. */
  9820. if (sym->aclass () != LOC_BLOCK)
  9821. {
  9822. error (_("Symbol \"%s\" is not a function (class = %d)"),
  9823. sym->linkage_name (), sym->aclass ());
  9824. return 0;
  9825. }
  9826. sym = standard_lookup (einfo->catch_handlers_sym, NULL, VAR_DOMAIN);
  9827. if (sym == NULL)
  9828. {
  9829. struct bound_minimal_symbol msym
  9830. = lookup_minimal_symbol (einfo->catch_handlers_sym, NULL, NULL);
  9831. if (msym.minsym && MSYMBOL_TYPE (msym.minsym) != mst_solib_trampoline)
  9832. error (_("Your Ada runtime appears to be missing some debugging "
  9833. "information.\nCannot insert Ada exception catchpoint "
  9834. "in this configuration."));
  9835. return 0;
  9836. }
  9837. /* Make sure that the symbol we found corresponds to a function. */
  9838. if (sym->aclass () != LOC_BLOCK)
  9839. {
  9840. error (_("Symbol \"%s\" is not a function (class = %d)"),
  9841. sym->linkage_name (), sym->aclass ());
  9842. return 0;
  9843. }
  9844. return 1;
  9845. }
  9846. /* Inspect the Ada runtime and determine which exception info structure
  9847. should be used to provide support for exception catchpoints.
  9848. This function will always set the per-inferior exception_info,
  9849. or raise an error. */
  9850. static void
  9851. ada_exception_support_info_sniffer (void)
  9852. {
  9853. struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
  9854. /* If the exception info is already known, then no need to recompute it. */
  9855. if (data->exception_info != NULL)
  9856. return;
  9857. /* Check the latest (default) exception support info. */
  9858. if (ada_has_this_exception_support (&default_exception_support_info))
  9859. {
  9860. data->exception_info = &default_exception_support_info;
  9861. return;
  9862. }
  9863. /* Try the v0 exception suport info. */
  9864. if (ada_has_this_exception_support (&exception_support_info_v0))
  9865. {
  9866. data->exception_info = &exception_support_info_v0;
  9867. return;
  9868. }
  9869. /* Try our fallback exception suport info. */
  9870. if (ada_has_this_exception_support (&exception_support_info_fallback))
  9871. {
  9872. data->exception_info = &exception_support_info_fallback;
  9873. return;
  9874. }
  9875. /* Sometimes, it is normal for us to not be able to find the routine
  9876. we are looking for. This happens when the program is linked with
  9877. the shared version of the GNAT runtime, and the program has not been
  9878. started yet. Inform the user of these two possible causes if
  9879. applicable. */
  9880. if (ada_update_initial_language (language_unknown) != language_ada)
  9881. error (_("Unable to insert catchpoint. Is this an Ada main program?"));
  9882. /* If the symbol does not exist, then check that the program is
  9883. already started, to make sure that shared libraries have been
  9884. loaded. If it is not started, this may mean that the symbol is
  9885. in a shared library. */
  9886. if (inferior_ptid.pid () == 0)
  9887. error (_("Unable to insert catchpoint. Try to start the program first."));
  9888. /* At this point, we know that we are debugging an Ada program and
  9889. that the inferior has been started, but we still are not able to
  9890. find the run-time symbols. That can mean that we are in
  9891. configurable run time mode, or that a-except as been optimized
  9892. out by the linker... In any case, at this point it is not worth
  9893. supporting this feature. */
  9894. error (_("Cannot insert Ada exception catchpoints in this configuration."));
  9895. }
  9896. /* True iff FRAME is very likely to be that of a function that is
  9897. part of the runtime system. This is all very heuristic, but is
  9898. intended to be used as advice as to what frames are uninteresting
  9899. to most users. */
  9900. static int
  9901. is_known_support_routine (struct frame_info *frame)
  9902. {
  9903. enum language func_lang;
  9904. int i;
  9905. const char *fullname;
  9906. /* If this code does not have any debugging information (no symtab),
  9907. This cannot be any user code. */
  9908. symtab_and_line sal = find_frame_sal (frame);
  9909. if (sal.symtab == NULL)
  9910. return 1;
  9911. /* If there is a symtab, but the associated source file cannot be
  9912. located, then assume this is not user code: Selecting a frame
  9913. for which we cannot display the code would not be very helpful
  9914. for the user. This should also take care of case such as VxWorks
  9915. where the kernel has some debugging info provided for a few units. */
  9916. fullname = symtab_to_fullname (sal.symtab);
  9917. if (access (fullname, R_OK) != 0)
  9918. return 1;
  9919. /* Check the unit filename against the Ada runtime file naming.
  9920. We also check the name of the objfile against the name of some
  9921. known system libraries that sometimes come with debugging info
  9922. too. */
  9923. for (i = 0; known_runtime_file_name_patterns[i] != NULL; i += 1)
  9924. {
  9925. re_comp (known_runtime_file_name_patterns[i]);
  9926. if (re_exec (lbasename (sal.symtab->filename)))
  9927. return 1;
  9928. if (sal.symtab->compunit ()->objfile () != NULL
  9929. && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
  9930. return 1;
  9931. }
  9932. /* Check whether the function is a GNAT-generated entity. */
  9933. gdb::unique_xmalloc_ptr<char> func_name
  9934. = find_frame_funname (frame, &func_lang, NULL);
  9935. if (func_name == NULL)
  9936. return 1;
  9937. for (i = 0; known_auxiliary_function_name_patterns[i] != NULL; i += 1)
  9938. {
  9939. re_comp (known_auxiliary_function_name_patterns[i]);
  9940. if (re_exec (func_name.get ()))
  9941. return 1;
  9942. }
  9943. return 0;
  9944. }
  9945. /* Find the first frame that contains debugging information and that is not
  9946. part of the Ada run-time, starting from FI and moving upward. */
  9947. void
  9948. ada_find_printable_frame (struct frame_info *fi)
  9949. {
  9950. for (; fi != NULL; fi = get_prev_frame (fi))
  9951. {
  9952. if (!is_known_support_routine (fi))
  9953. {
  9954. select_frame (fi);
  9955. break;
  9956. }
  9957. }
  9958. }
  9959. /* Assuming that the inferior just triggered an unhandled exception
  9960. catchpoint, return the address in inferior memory where the name
  9961. of the exception is stored.
  9962. Return zero if the address could not be computed. */
  9963. static CORE_ADDR
  9964. ada_unhandled_exception_name_addr (void)
  9965. {
  9966. return parse_and_eval_address ("e.full_name");
  9967. }
  9968. /* Same as ada_unhandled_exception_name_addr, except that this function
  9969. should be used when the inferior uses an older version of the runtime,
  9970. where the exception name needs to be extracted from a specific frame
  9971. several frames up in the callstack. */
  9972. static CORE_ADDR
  9973. ada_unhandled_exception_name_addr_from_raise (void)
  9974. {
  9975. int frame_level;
  9976. struct frame_info *fi;
  9977. struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
  9978. /* To determine the name of this exception, we need to select
  9979. the frame corresponding to RAISE_SYM_NAME. This frame is
  9980. at least 3 levels up, so we simply skip the first 3 frames
  9981. without checking the name of their associated function. */
  9982. fi = get_current_frame ();
  9983. for (frame_level = 0; frame_level < 3; frame_level += 1)
  9984. if (fi != NULL)
  9985. fi = get_prev_frame (fi);
  9986. while (fi != NULL)
  9987. {
  9988. enum language func_lang;
  9989. gdb::unique_xmalloc_ptr<char> func_name
  9990. = find_frame_funname (fi, &func_lang, NULL);
  9991. if (func_name != NULL)
  9992. {
  9993. if (strcmp (func_name.get (),
  9994. data->exception_info->catch_exception_sym) == 0)
  9995. break; /* We found the frame we were looking for... */
  9996. }
  9997. fi = get_prev_frame (fi);
  9998. }
  9999. if (fi == NULL)
  10000. return 0;
  10001. select_frame (fi);
  10002. return parse_and_eval_address ("id.full_name");
  10003. }
  10004. /* Assuming the inferior just triggered an Ada exception catchpoint
  10005. (of any type), return the address in inferior memory where the name
  10006. of the exception is stored, if applicable.
  10007. Assumes the selected frame is the current frame.
  10008. Return zero if the address could not be computed, or if not relevant. */
  10009. static CORE_ADDR
  10010. ada_exception_name_addr_1 (enum ada_exception_catchpoint_kind ex,
  10011. struct breakpoint *b)
  10012. {
  10013. struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
  10014. switch (ex)
  10015. {
  10016. case ada_catch_exception:
  10017. return (parse_and_eval_address ("e.full_name"));
  10018. break;
  10019. case ada_catch_exception_unhandled:
  10020. return data->exception_info->unhandled_exception_name_addr ();
  10021. break;
  10022. case ada_catch_handlers:
  10023. return 0; /* The runtimes does not provide access to the exception
  10024. name. */
  10025. break;
  10026. case ada_catch_assert:
  10027. return 0; /* Exception name is not relevant in this case. */
  10028. break;
  10029. default:
  10030. internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
  10031. break;
  10032. }
  10033. return 0; /* Should never be reached. */
  10034. }
  10035. /* Assuming the inferior is stopped at an exception catchpoint,
  10036. return the message which was associated to the exception, if
  10037. available. Return NULL if the message could not be retrieved.
  10038. Note: The exception message can be associated to an exception
  10039. either through the use of the Raise_Exception function, or
  10040. more simply (Ada 2005 and later), via:
  10041. raise Exception_Name with "exception message";
  10042. */
  10043. static gdb::unique_xmalloc_ptr<char>
  10044. ada_exception_message_1 (void)
  10045. {
  10046. struct value *e_msg_val;
  10047. int e_msg_len;
  10048. /* For runtimes that support this feature, the exception message
  10049. is passed as an unbounded string argument called "message". */
  10050. e_msg_val = parse_and_eval ("message");
  10051. if (e_msg_val == NULL)
  10052. return NULL; /* Exception message not supported. */
  10053. e_msg_val = ada_coerce_to_simple_array (e_msg_val);
  10054. gdb_assert (e_msg_val != NULL);
  10055. e_msg_len = TYPE_LENGTH (value_type (e_msg_val));
  10056. /* If the message string is empty, then treat it as if there was
  10057. no exception message. */
  10058. if (e_msg_len <= 0)
  10059. return NULL;
  10060. gdb::unique_xmalloc_ptr<char> e_msg ((char *) xmalloc (e_msg_len + 1));
  10061. read_memory (value_address (e_msg_val), (gdb_byte *) e_msg.get (),
  10062. e_msg_len);
  10063. e_msg.get ()[e_msg_len] = '\0';
  10064. return e_msg;
  10065. }
  10066. /* Same as ada_exception_message_1, except that all exceptions are
  10067. contained here (returning NULL instead). */
  10068. static gdb::unique_xmalloc_ptr<char>
  10069. ada_exception_message (void)
  10070. {
  10071. gdb::unique_xmalloc_ptr<char> e_msg;
  10072. try
  10073. {
  10074. e_msg = ada_exception_message_1 ();
  10075. }
  10076. catch (const gdb_exception_error &e)
  10077. {
  10078. e_msg.reset (nullptr);
  10079. }
  10080. return e_msg;
  10081. }
  10082. /* Same as ada_exception_name_addr_1, except that it intercepts and contains
  10083. any error that ada_exception_name_addr_1 might cause to be thrown.
  10084. When an error is intercepted, a warning with the error message is printed,
  10085. and zero is returned. */
  10086. static CORE_ADDR
  10087. ada_exception_name_addr (enum ada_exception_catchpoint_kind ex,
  10088. struct breakpoint *b)
  10089. {
  10090. CORE_ADDR result = 0;
  10091. try
  10092. {
  10093. result = ada_exception_name_addr_1 (ex, b);
  10094. }
  10095. catch (const gdb_exception_error &e)
  10096. {
  10097. warning (_("failed to get exception name: %s"), e.what ());
  10098. return 0;
  10099. }
  10100. return result;
  10101. }
  10102. static std::string ada_exception_catchpoint_cond_string
  10103. (const char *excep_string,
  10104. enum ada_exception_catchpoint_kind ex);
  10105. /* Ada catchpoints.
  10106. In the case of catchpoints on Ada exceptions, the catchpoint will
  10107. stop the target on every exception the program throws. When a user
  10108. specifies the name of a specific exception, we translate this
  10109. request into a condition expression (in text form), and then parse
  10110. it into an expression stored in each of the catchpoint's locations.
  10111. We then use this condition to check whether the exception that was
  10112. raised is the one the user is interested in. If not, then the
  10113. target is resumed again. We store the name of the requested
  10114. exception, in order to be able to re-set the condition expression
  10115. when symbols change. */
  10116. /* An instance of this type is used to represent an Ada catchpoint
  10117. breakpoint location. */
  10118. class ada_catchpoint_location : public bp_location
  10119. {
  10120. public:
  10121. ada_catchpoint_location (breakpoint *owner)
  10122. : bp_location (owner, bp_loc_software_breakpoint)
  10123. {}
  10124. /* The condition that checks whether the exception that was raised
  10125. is the specific exception the user specified on catchpoint
  10126. creation. */
  10127. expression_up excep_cond_expr;
  10128. };
  10129. /* An instance of this type is used to represent an Ada catchpoint. */
  10130. struct ada_catchpoint : public breakpoint
  10131. {
  10132. explicit ada_catchpoint (enum ada_exception_catchpoint_kind kind)
  10133. : m_kind (kind)
  10134. {
  10135. }
  10136. /* The name of the specific exception the user specified. */
  10137. std::string excep_string;
  10138. /* What kind of catchpoint this is. */
  10139. enum ada_exception_catchpoint_kind m_kind;
  10140. };
  10141. /* Parse the exception condition string in the context of each of the
  10142. catchpoint's locations, and store them for later evaluation. */
  10143. static void
  10144. create_excep_cond_exprs (struct ada_catchpoint *c,
  10145. enum ada_exception_catchpoint_kind ex)
  10146. {
  10147. /* Nothing to do if there's no specific exception to catch. */
  10148. if (c->excep_string.empty ())
  10149. return;
  10150. /* Same if there are no locations... */
  10151. if (c->loc == NULL)
  10152. return;
  10153. /* Compute the condition expression in text form, from the specific
  10154. expection we want to catch. */
  10155. std::string cond_string
  10156. = ada_exception_catchpoint_cond_string (c->excep_string.c_str (), ex);
  10157. /* Iterate over all the catchpoint's locations, and parse an
  10158. expression for each. */
  10159. for (bp_location *bl : c->locations ())
  10160. {
  10161. struct ada_catchpoint_location *ada_loc
  10162. = (struct ada_catchpoint_location *) bl;
  10163. expression_up exp;
  10164. if (!bl->shlib_disabled)
  10165. {
  10166. const char *s;
  10167. s = cond_string.c_str ();
  10168. try
  10169. {
  10170. exp = parse_exp_1 (&s, bl->address,
  10171. block_for_pc (bl->address),
  10172. 0);
  10173. }
  10174. catch (const gdb_exception_error &e)
  10175. {
  10176. warning (_("failed to reevaluate internal exception condition "
  10177. "for catchpoint %d: %s"),
  10178. c->number, e.what ());
  10179. }
  10180. }
  10181. ada_loc->excep_cond_expr = std::move (exp);
  10182. }
  10183. }
  10184. /* Implement the ALLOCATE_LOCATION method in the breakpoint_ops
  10185. structure for all exception catchpoint kinds. */
  10186. static struct bp_location *
  10187. allocate_location_exception (struct breakpoint *self)
  10188. {
  10189. return new ada_catchpoint_location (self);
  10190. }
  10191. /* Implement the RE_SET method in the breakpoint_ops structure for all
  10192. exception catchpoint kinds. */
  10193. static void
  10194. re_set_exception (struct breakpoint *b)
  10195. {
  10196. struct ada_catchpoint *c = (struct ada_catchpoint *) b;
  10197. /* Call the base class's method. This updates the catchpoint's
  10198. locations. */
  10199. bkpt_breakpoint_ops.re_set (b);
  10200. /* Reparse the exception conditional expressions. One for each
  10201. location. */
  10202. create_excep_cond_exprs (c, c->m_kind);
  10203. }
  10204. /* Returns true if we should stop for this breakpoint hit. If the
  10205. user specified a specific exception, we only want to cause a stop
  10206. if the program thrown that exception. */
  10207. static bool
  10208. should_stop_exception (const struct bp_location *bl)
  10209. {
  10210. struct ada_catchpoint *c = (struct ada_catchpoint *) bl->owner;
  10211. const struct ada_catchpoint_location *ada_loc
  10212. = (const struct ada_catchpoint_location *) bl;
  10213. bool stop;
  10214. struct internalvar *var = lookup_internalvar ("_ada_exception");
  10215. if (c->m_kind == ada_catch_assert)
  10216. clear_internalvar (var);
  10217. else
  10218. {
  10219. try
  10220. {
  10221. const char *expr;
  10222. if (c->m_kind == ada_catch_handlers)
  10223. expr = ("GNAT_GCC_exception_Access(gcc_exception)"
  10224. ".all.occurrence.id");
  10225. else
  10226. expr = "e";
  10227. struct value *exc = parse_and_eval (expr);
  10228. set_internalvar (var, exc);
  10229. }
  10230. catch (const gdb_exception_error &ex)
  10231. {
  10232. clear_internalvar (var);
  10233. }
  10234. }
  10235. /* With no specific exception, should always stop. */
  10236. if (c->excep_string.empty ())
  10237. return true;
  10238. if (ada_loc->excep_cond_expr == NULL)
  10239. {
  10240. /* We will have a NULL expression if back when we were creating
  10241. the expressions, this location's had failed to parse. */
  10242. return true;
  10243. }
  10244. stop = true;
  10245. try
  10246. {
  10247. struct value *mark;
  10248. mark = value_mark ();
  10249. stop = value_true (evaluate_expression (ada_loc->excep_cond_expr.get ()));
  10250. value_free_to_mark (mark);
  10251. }
  10252. catch (const gdb_exception &ex)
  10253. {
  10254. exception_fprintf (gdb_stderr, ex,
  10255. _("Error in testing exception condition:\n"));
  10256. }
  10257. return stop;
  10258. }
  10259. /* Implement the CHECK_STATUS method in the breakpoint_ops structure
  10260. for all exception catchpoint kinds. */
  10261. static void
  10262. check_status_exception (bpstat *bs)
  10263. {
  10264. bs->stop = should_stop_exception (bs->bp_location_at.get ());
  10265. }
  10266. /* Implement the PRINT_IT method in the breakpoint_ops structure
  10267. for all exception catchpoint kinds. */
  10268. static enum print_stop_action
  10269. print_it_exception (bpstat *bs)
  10270. {
  10271. struct ui_out *uiout = current_uiout;
  10272. struct breakpoint *b = bs->breakpoint_at;
  10273. annotate_catchpoint (b->number);
  10274. if (uiout->is_mi_like_p ())
  10275. {
  10276. uiout->field_string ("reason",
  10277. async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
  10278. uiout->field_string ("disp", bpdisp_text (b->disposition));
  10279. }
  10280. uiout->text (b->disposition == disp_del
  10281. ? "\nTemporary catchpoint " : "\nCatchpoint ");
  10282. uiout->field_signed ("bkptno", b->number);
  10283. uiout->text (", ");
  10284. /* ada_exception_name_addr relies on the selected frame being the
  10285. current frame. Need to do this here because this function may be
  10286. called more than once when printing a stop, and below, we'll
  10287. select the first frame past the Ada run-time (see
  10288. ada_find_printable_frame). */
  10289. select_frame (get_current_frame ());
  10290. struct ada_catchpoint *c = (struct ada_catchpoint *) b;
  10291. switch (c->m_kind)
  10292. {
  10293. case ada_catch_exception:
  10294. case ada_catch_exception_unhandled:
  10295. case ada_catch_handlers:
  10296. {
  10297. const CORE_ADDR addr = ada_exception_name_addr (c->m_kind, b);
  10298. char exception_name[256];
  10299. if (addr != 0)
  10300. {
  10301. read_memory (addr, (gdb_byte *) exception_name,
  10302. sizeof (exception_name) - 1);
  10303. exception_name [sizeof (exception_name) - 1] = '\0';
  10304. }
  10305. else
  10306. {
  10307. /* For some reason, we were unable to read the exception
  10308. name. This could happen if the Runtime was compiled
  10309. without debugging info, for instance. In that case,
  10310. just replace the exception name by the generic string
  10311. "exception" - it will read as "an exception" in the
  10312. notification we are about to print. */
  10313. memcpy (exception_name, "exception", sizeof ("exception"));
  10314. }
  10315. /* In the case of unhandled exception breakpoints, we print
  10316. the exception name as "unhandled EXCEPTION_NAME", to make
  10317. it clearer to the user which kind of catchpoint just got
  10318. hit. We used ui_out_text to make sure that this extra
  10319. info does not pollute the exception name in the MI case. */
  10320. if (c->m_kind == ada_catch_exception_unhandled)
  10321. uiout->text ("unhandled ");
  10322. uiout->field_string ("exception-name", exception_name);
  10323. }
  10324. break;
  10325. case ada_catch_assert:
  10326. /* In this case, the name of the exception is not really
  10327. important. Just print "failed assertion" to make it clearer
  10328. that his program just hit an assertion-failure catchpoint.
  10329. We used ui_out_text because this info does not belong in
  10330. the MI output. */
  10331. uiout->text ("failed assertion");
  10332. break;
  10333. }
  10334. gdb::unique_xmalloc_ptr<char> exception_message = ada_exception_message ();
  10335. if (exception_message != NULL)
  10336. {
  10337. uiout->text (" (");
  10338. uiout->field_string ("exception-message", exception_message.get ());
  10339. uiout->text (")");
  10340. }
  10341. uiout->text (" at ");
  10342. ada_find_printable_frame (get_current_frame ());
  10343. return PRINT_SRC_AND_LOC;
  10344. }
  10345. /* Implement the PRINT_ONE method in the breakpoint_ops structure
  10346. for all exception catchpoint kinds. */
  10347. static void
  10348. print_one_exception (struct breakpoint *b, struct bp_location **last_loc)
  10349. {
  10350. struct ui_out *uiout = current_uiout;
  10351. struct ada_catchpoint *c = (struct ada_catchpoint *) b;
  10352. struct value_print_options opts;
  10353. get_user_print_options (&opts);
  10354. if (opts.addressprint)
  10355. uiout->field_skip ("addr");
  10356. annotate_field (5);
  10357. switch (c->m_kind)
  10358. {
  10359. case ada_catch_exception:
  10360. if (!c->excep_string.empty ())
  10361. {
  10362. std::string msg = string_printf (_("`%s' Ada exception"),
  10363. c->excep_string.c_str ());
  10364. uiout->field_string ("what", msg);
  10365. }
  10366. else
  10367. uiout->field_string ("what", "all Ada exceptions");
  10368. break;
  10369. case ada_catch_exception_unhandled:
  10370. uiout->field_string ("what", "unhandled Ada exceptions");
  10371. break;
  10372. case ada_catch_handlers:
  10373. if (!c->excep_string.empty ())
  10374. {
  10375. uiout->field_fmt ("what",
  10376. _("`%s' Ada exception handlers"),
  10377. c->excep_string.c_str ());
  10378. }
  10379. else
  10380. uiout->field_string ("what", "all Ada exceptions handlers");
  10381. break;
  10382. case ada_catch_assert:
  10383. uiout->field_string ("what", "failed Ada assertions");
  10384. break;
  10385. default:
  10386. internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
  10387. break;
  10388. }
  10389. }
  10390. /* Implement the PRINT_MENTION method in the breakpoint_ops structure
  10391. for all exception catchpoint kinds. */
  10392. static void
  10393. print_mention_exception (struct breakpoint *b)
  10394. {
  10395. struct ada_catchpoint *c = (struct ada_catchpoint *) b;
  10396. struct ui_out *uiout = current_uiout;
  10397. uiout->text (b->disposition == disp_del ? _("Temporary catchpoint ")
  10398. : _("Catchpoint "));
  10399. uiout->field_signed ("bkptno", b->number);
  10400. uiout->text (": ");
  10401. switch (c->m_kind)
  10402. {
  10403. case ada_catch_exception:
  10404. if (!c->excep_string.empty ())
  10405. {
  10406. std::string info = string_printf (_("`%s' Ada exception"),
  10407. c->excep_string.c_str ());
  10408. uiout->text (info);
  10409. }
  10410. else
  10411. uiout->text (_("all Ada exceptions"));
  10412. break;
  10413. case ada_catch_exception_unhandled:
  10414. uiout->text (_("unhandled Ada exceptions"));
  10415. break;
  10416. case ada_catch_handlers:
  10417. if (!c->excep_string.empty ())
  10418. {
  10419. std::string info
  10420. = string_printf (_("`%s' Ada exception handlers"),
  10421. c->excep_string.c_str ());
  10422. uiout->text (info);
  10423. }
  10424. else
  10425. uiout->text (_("all Ada exceptions handlers"));
  10426. break;
  10427. case ada_catch_assert:
  10428. uiout->text (_("failed Ada assertions"));
  10429. break;
  10430. default:
  10431. internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
  10432. break;
  10433. }
  10434. }
  10435. /* Implement the PRINT_RECREATE method in the breakpoint_ops structure
  10436. for all exception catchpoint kinds. */
  10437. static void
  10438. print_recreate_exception (struct breakpoint *b, struct ui_file *fp)
  10439. {
  10440. struct ada_catchpoint *c = (struct ada_catchpoint *) b;
  10441. switch (c->m_kind)
  10442. {
  10443. case ada_catch_exception:
  10444. gdb_printf (fp, "catch exception");
  10445. if (!c->excep_string.empty ())
  10446. gdb_printf (fp, " %s", c->excep_string.c_str ());
  10447. break;
  10448. case ada_catch_exception_unhandled:
  10449. gdb_printf (fp, "catch exception unhandled");
  10450. break;
  10451. case ada_catch_handlers:
  10452. gdb_printf (fp, "catch handlers");
  10453. break;
  10454. case ada_catch_assert:
  10455. gdb_printf (fp, "catch assert");
  10456. break;
  10457. default:
  10458. internal_error (__FILE__, __LINE__, _("unexpected catchpoint type"));
  10459. }
  10460. print_recreate_thread (b, fp);
  10461. }
  10462. /* Virtual table for breakpoint type. */
  10463. static struct breakpoint_ops catch_exception_breakpoint_ops;
  10464. /* See ada-lang.h. */
  10465. bool
  10466. is_ada_exception_catchpoint (breakpoint *bp)
  10467. {
  10468. return bp->ops == &catch_exception_breakpoint_ops;
  10469. }
  10470. /* Split the arguments specified in a "catch exception" command.
  10471. Set EX to the appropriate catchpoint type.
  10472. Set EXCEP_STRING to the name of the specific exception if
  10473. specified by the user.
  10474. IS_CATCH_HANDLERS_CMD: True if the arguments are for a
  10475. "catch handlers" command. False otherwise.
  10476. If a condition is found at the end of the arguments, the condition
  10477. expression is stored in COND_STRING (memory must be deallocated
  10478. after use). Otherwise COND_STRING is set to NULL. */
  10479. static void
  10480. catch_ada_exception_command_split (const char *args,
  10481. bool is_catch_handlers_cmd,
  10482. enum ada_exception_catchpoint_kind *ex,
  10483. std::string *excep_string,
  10484. std::string *cond_string)
  10485. {
  10486. std::string exception_name;
  10487. exception_name = extract_arg (&args);
  10488. if (exception_name == "if")
  10489. {
  10490. /* This is not an exception name; this is the start of a condition
  10491. expression for a catchpoint on all exceptions. So, "un-get"
  10492. this token, and set exception_name to NULL. */
  10493. exception_name.clear ();
  10494. args -= 2;
  10495. }
  10496. /* Check to see if we have a condition. */
  10497. args = skip_spaces (args);
  10498. if (startswith (args, "if")
  10499. && (isspace (args[2]) || args[2] == '\0'))
  10500. {
  10501. args += 2;
  10502. args = skip_spaces (args);
  10503. if (args[0] == '\0')
  10504. error (_("Condition missing after `if' keyword"));
  10505. *cond_string = args;
  10506. args += strlen (args);
  10507. }
  10508. /* Check that we do not have any more arguments. Anything else
  10509. is unexpected. */
  10510. if (args[0] != '\0')
  10511. error (_("Junk at end of expression"));
  10512. if (is_catch_handlers_cmd)
  10513. {
  10514. /* Catch handling of exceptions. */
  10515. *ex = ada_catch_handlers;
  10516. *excep_string = exception_name;
  10517. }
  10518. else if (exception_name.empty ())
  10519. {
  10520. /* Catch all exceptions. */
  10521. *ex = ada_catch_exception;
  10522. excep_string->clear ();
  10523. }
  10524. else if (exception_name == "unhandled")
  10525. {
  10526. /* Catch unhandled exceptions. */
  10527. *ex = ada_catch_exception_unhandled;
  10528. excep_string->clear ();
  10529. }
  10530. else
  10531. {
  10532. /* Catch a specific exception. */
  10533. *ex = ada_catch_exception;
  10534. *excep_string = exception_name;
  10535. }
  10536. }
  10537. /* Return the name of the symbol on which we should break in order to
  10538. implement a catchpoint of the EX kind. */
  10539. static const char *
  10540. ada_exception_sym_name (enum ada_exception_catchpoint_kind ex)
  10541. {
  10542. struct ada_inferior_data *data = get_ada_inferior_data (current_inferior ());
  10543. gdb_assert (data->exception_info != NULL);
  10544. switch (ex)
  10545. {
  10546. case ada_catch_exception:
  10547. return (data->exception_info->catch_exception_sym);
  10548. break;
  10549. case ada_catch_exception_unhandled:
  10550. return (data->exception_info->catch_exception_unhandled_sym);
  10551. break;
  10552. case ada_catch_assert:
  10553. return (data->exception_info->catch_assert_sym);
  10554. break;
  10555. case ada_catch_handlers:
  10556. return (data->exception_info->catch_handlers_sym);
  10557. break;
  10558. default:
  10559. internal_error (__FILE__, __LINE__,
  10560. _("unexpected catchpoint kind (%d)"), ex);
  10561. }
  10562. }
  10563. /* Return the condition that will be used to match the current exception
  10564. being raised with the exception that the user wants to catch. This
  10565. assumes that this condition is used when the inferior just triggered
  10566. an exception catchpoint.
  10567. EX: the type of catchpoints used for catching Ada exceptions. */
  10568. static std::string
  10569. ada_exception_catchpoint_cond_string (const char *excep_string,
  10570. enum ada_exception_catchpoint_kind ex)
  10571. {
  10572. bool is_standard_exc = false;
  10573. std::string result;
  10574. if (ex == ada_catch_handlers)
  10575. {
  10576. /* For exception handlers catchpoints, the condition string does
  10577. not use the same parameter as for the other exceptions. */
  10578. result = ("long_integer (GNAT_GCC_exception_Access"
  10579. "(gcc_exception).all.occurrence.id)");
  10580. }
  10581. else
  10582. result = "long_integer (e)";
  10583. /* The standard exceptions are a special case. They are defined in
  10584. runtime units that have been compiled without debugging info; if
  10585. EXCEP_STRING is the not-fully-qualified name of a standard
  10586. exception (e.g. "constraint_error") then, during the evaluation
  10587. of the condition expression, the symbol lookup on this name would
  10588. *not* return this standard exception. The catchpoint condition
  10589. may then be set only on user-defined exceptions which have the
  10590. same not-fully-qualified name (e.g. my_package.constraint_error).
  10591. To avoid this unexcepted behavior, these standard exceptions are
  10592. systematically prefixed by "standard". This means that "catch
  10593. exception constraint_error" is rewritten into "catch exception
  10594. standard.constraint_error".
  10595. If an exception named constraint_error is defined in another package of
  10596. the inferior program, then the only way to specify this exception as a
  10597. breakpoint condition is to use its fully-qualified named:
  10598. e.g. my_package.constraint_error. */
  10599. for (const char *name : standard_exc)
  10600. {
  10601. if (strcmp (name, excep_string) == 0)
  10602. {
  10603. is_standard_exc = true;
  10604. break;
  10605. }
  10606. }
  10607. result += " = ";
  10608. if (is_standard_exc)
  10609. string_appendf (result, "long_integer (&standard.%s)", excep_string);
  10610. else
  10611. string_appendf (result, "long_integer (&%s)", excep_string);
  10612. return result;
  10613. }
  10614. /* Return the symtab_and_line that should be used to insert an exception
  10615. catchpoint of the TYPE kind.
  10616. ADDR_STRING returns the name of the function where the real
  10617. breakpoint that implements the catchpoints is set, depending on the
  10618. type of catchpoint we need to create. */
  10619. static struct symtab_and_line
  10620. ada_exception_sal (enum ada_exception_catchpoint_kind ex,
  10621. std::string *addr_string, const struct breakpoint_ops **ops)
  10622. {
  10623. const char *sym_name;
  10624. struct symbol *sym;
  10625. /* First, find out which exception support info to use. */
  10626. ada_exception_support_info_sniffer ();
  10627. /* Then lookup the function on which we will break in order to catch
  10628. the Ada exceptions requested by the user. */
  10629. sym_name = ada_exception_sym_name (ex);
  10630. sym = standard_lookup (sym_name, NULL, VAR_DOMAIN);
  10631. if (sym == NULL)
  10632. error (_("Catchpoint symbol not found: %s"), sym_name);
  10633. if (sym->aclass () != LOC_BLOCK)
  10634. error (_("Unable to insert catchpoint. %s is not a function."), sym_name);
  10635. /* Set ADDR_STRING. */
  10636. *addr_string = sym_name;
  10637. /* Set OPS. */
  10638. *ops = &catch_exception_breakpoint_ops;
  10639. return find_function_start_sal (sym, 1);
  10640. }
  10641. /* Create an Ada exception catchpoint.
  10642. EX_KIND is the kind of exception catchpoint to be created.
  10643. If EXCEPT_STRING is empty, this catchpoint is expected to trigger
  10644. for all exceptions. Otherwise, EXCEPT_STRING indicates the name
  10645. of the exception to which this catchpoint applies.
  10646. COND_STRING, if not empty, is the catchpoint condition.
  10647. TEMPFLAG, if nonzero, means that the underlying breakpoint
  10648. should be temporary.
  10649. FROM_TTY is the usual argument passed to all commands implementations. */
  10650. void
  10651. create_ada_exception_catchpoint (struct gdbarch *gdbarch,
  10652. enum ada_exception_catchpoint_kind ex_kind,
  10653. const std::string &excep_string,
  10654. const std::string &cond_string,
  10655. int tempflag,
  10656. int disabled,
  10657. int from_tty)
  10658. {
  10659. std::string addr_string;
  10660. const struct breakpoint_ops *ops = NULL;
  10661. struct symtab_and_line sal = ada_exception_sal (ex_kind, &addr_string, &ops);
  10662. std::unique_ptr<ada_catchpoint> c (new ada_catchpoint (ex_kind));
  10663. init_ada_exception_breakpoint (c.get (), gdbarch, sal, addr_string.c_str (),
  10664. ops, tempflag, disabled, from_tty);
  10665. c->excep_string = excep_string;
  10666. create_excep_cond_exprs (c.get (), ex_kind);
  10667. if (!cond_string.empty ())
  10668. set_breakpoint_condition (c.get (), cond_string.c_str (), from_tty, false);
  10669. install_breakpoint (0, std::move (c), 1);
  10670. }
  10671. /* Implement the "catch exception" command. */
  10672. static void
  10673. catch_ada_exception_command (const char *arg_entry, int from_tty,
  10674. struct cmd_list_element *command)
  10675. {
  10676. const char *arg = arg_entry;
  10677. struct gdbarch *gdbarch = get_current_arch ();
  10678. int tempflag;
  10679. enum ada_exception_catchpoint_kind ex_kind;
  10680. std::string excep_string;
  10681. std::string cond_string;
  10682. tempflag = command->context () == CATCH_TEMPORARY;
  10683. if (!arg)
  10684. arg = "";
  10685. catch_ada_exception_command_split (arg, false, &ex_kind, &excep_string,
  10686. &cond_string);
  10687. create_ada_exception_catchpoint (gdbarch, ex_kind,
  10688. excep_string, cond_string,
  10689. tempflag, 1 /* enabled */,
  10690. from_tty);
  10691. }
  10692. /* Implement the "catch handlers" command. */
  10693. static void
  10694. catch_ada_handlers_command (const char *arg_entry, int from_tty,
  10695. struct cmd_list_element *command)
  10696. {
  10697. const char *arg = arg_entry;
  10698. struct gdbarch *gdbarch = get_current_arch ();
  10699. int tempflag;
  10700. enum ada_exception_catchpoint_kind ex_kind;
  10701. std::string excep_string;
  10702. std::string cond_string;
  10703. tempflag = command->context () == CATCH_TEMPORARY;
  10704. if (!arg)
  10705. arg = "";
  10706. catch_ada_exception_command_split (arg, true, &ex_kind, &excep_string,
  10707. &cond_string);
  10708. create_ada_exception_catchpoint (gdbarch, ex_kind,
  10709. excep_string, cond_string,
  10710. tempflag, 1 /* enabled */,
  10711. from_tty);
  10712. }
  10713. /* Completion function for the Ada "catch" commands. */
  10714. static void
  10715. catch_ada_completer (struct cmd_list_element *cmd, completion_tracker &tracker,
  10716. const char *text, const char *word)
  10717. {
  10718. std::vector<ada_exc_info> exceptions = ada_exceptions_list (NULL);
  10719. for (const ada_exc_info &info : exceptions)
  10720. {
  10721. if (startswith (info.name, word))
  10722. tracker.add_completion (make_unique_xstrdup (info.name));
  10723. }
  10724. }
  10725. /* Split the arguments specified in a "catch assert" command.
  10726. ARGS contains the command's arguments (or the empty string if
  10727. no arguments were passed).
  10728. If ARGS contains a condition, set COND_STRING to that condition
  10729. (the memory needs to be deallocated after use). */
  10730. static void
  10731. catch_ada_assert_command_split (const char *args, std::string &cond_string)
  10732. {
  10733. args = skip_spaces (args);
  10734. /* Check whether a condition was provided. */
  10735. if (startswith (args, "if")
  10736. && (isspace (args[2]) || args[2] == '\0'))
  10737. {
  10738. args += 2;
  10739. args = skip_spaces (args);
  10740. if (args[0] == '\0')
  10741. error (_("condition missing after `if' keyword"));
  10742. cond_string.assign (args);
  10743. }
  10744. /* Otherwise, there should be no other argument at the end of
  10745. the command. */
  10746. else if (args[0] != '\0')
  10747. error (_("Junk at end of arguments."));
  10748. }
  10749. /* Implement the "catch assert" command. */
  10750. static void
  10751. catch_assert_command (const char *arg_entry, int from_tty,
  10752. struct cmd_list_element *command)
  10753. {
  10754. const char *arg = arg_entry;
  10755. struct gdbarch *gdbarch = get_current_arch ();
  10756. int tempflag;
  10757. std::string cond_string;
  10758. tempflag = command->context () == CATCH_TEMPORARY;
  10759. if (!arg)
  10760. arg = "";
  10761. catch_ada_assert_command_split (arg, cond_string);
  10762. create_ada_exception_catchpoint (gdbarch, ada_catch_assert,
  10763. "", cond_string,
  10764. tempflag, 1 /* enabled */,
  10765. from_tty);
  10766. }
  10767. /* Return non-zero if the symbol SYM is an Ada exception object. */
  10768. static int
  10769. ada_is_exception_sym (struct symbol *sym)
  10770. {
  10771. const char *type_name = sym->type ()->name ();
  10772. return (sym->aclass () != LOC_TYPEDEF
  10773. && sym->aclass () != LOC_BLOCK
  10774. && sym->aclass () != LOC_CONST
  10775. && sym->aclass () != LOC_UNRESOLVED
  10776. && type_name != NULL && strcmp (type_name, "exception") == 0);
  10777. }
  10778. /* Given a global symbol SYM, return non-zero iff SYM is a non-standard
  10779. Ada exception object. This matches all exceptions except the ones
  10780. defined by the Ada language. */
  10781. static int
  10782. ada_is_non_standard_exception_sym (struct symbol *sym)
  10783. {
  10784. if (!ada_is_exception_sym (sym))
  10785. return 0;
  10786. for (const char *name : standard_exc)
  10787. if (strcmp (sym->linkage_name (), name) == 0)
  10788. return 0; /* A standard exception. */
  10789. /* Numeric_Error is also a standard exception, so exclude it.
  10790. See the STANDARD_EXC description for more details as to why
  10791. this exception is not listed in that array. */
  10792. if (strcmp (sym->linkage_name (), "numeric_error") == 0)
  10793. return 0;
  10794. return 1;
  10795. }
  10796. /* A helper function for std::sort, comparing two struct ada_exc_info
  10797. objects.
  10798. The comparison is determined first by exception name, and then
  10799. by exception address. */
  10800. bool
  10801. ada_exc_info::operator< (const ada_exc_info &other) const
  10802. {
  10803. int result;
  10804. result = strcmp (name, other.name);
  10805. if (result < 0)
  10806. return true;
  10807. if (result == 0 && addr < other.addr)
  10808. return true;
  10809. return false;
  10810. }
  10811. bool
  10812. ada_exc_info::operator== (const ada_exc_info &other) const
  10813. {
  10814. return addr == other.addr && strcmp (name, other.name) == 0;
  10815. }
  10816. /* Sort EXCEPTIONS using compare_ada_exception_info as the comparison
  10817. routine, but keeping the first SKIP elements untouched.
  10818. All duplicates are also removed. */
  10819. static void
  10820. sort_remove_dups_ada_exceptions_list (std::vector<ada_exc_info> *exceptions,
  10821. int skip)
  10822. {
  10823. std::sort (exceptions->begin () + skip, exceptions->end ());
  10824. exceptions->erase (std::unique (exceptions->begin () + skip, exceptions->end ()),
  10825. exceptions->end ());
  10826. }
  10827. /* Add all exceptions defined by the Ada standard whose name match
  10828. a regular expression.
  10829. If PREG is not NULL, then this regexp_t object is used to
  10830. perform the symbol name matching. Otherwise, no name-based
  10831. filtering is performed.
  10832. EXCEPTIONS is a vector of exceptions to which matching exceptions
  10833. gets pushed. */
  10834. static void
  10835. ada_add_standard_exceptions (compiled_regex *preg,
  10836. std::vector<ada_exc_info> *exceptions)
  10837. {
  10838. for (const char *name : standard_exc)
  10839. {
  10840. if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
  10841. {
  10842. struct bound_minimal_symbol msymbol
  10843. = ada_lookup_simple_minsym (name);
  10844. if (msymbol.minsym != NULL)
  10845. {
  10846. struct ada_exc_info info
  10847. = {name, BMSYMBOL_VALUE_ADDRESS (msymbol)};
  10848. exceptions->push_back (info);
  10849. }
  10850. }
  10851. }
  10852. }
  10853. /* Add all Ada exceptions defined locally and accessible from the given
  10854. FRAME.
  10855. If PREG is not NULL, then this regexp_t object is used to
  10856. perform the symbol name matching. Otherwise, no name-based
  10857. filtering is performed.
  10858. EXCEPTIONS is a vector of exceptions to which matching exceptions
  10859. gets pushed. */
  10860. static void
  10861. ada_add_exceptions_from_frame (compiled_regex *preg,
  10862. struct frame_info *frame,
  10863. std::vector<ada_exc_info> *exceptions)
  10864. {
  10865. const struct block *block = get_frame_block (frame, 0);
  10866. while (block != 0)
  10867. {
  10868. struct block_iterator iter;
  10869. struct symbol *sym;
  10870. ALL_BLOCK_SYMBOLS (block, iter, sym)
  10871. {
  10872. switch (sym->aclass ())
  10873. {
  10874. case LOC_TYPEDEF:
  10875. case LOC_BLOCK:
  10876. case LOC_CONST:
  10877. break;
  10878. default:
  10879. if (ada_is_exception_sym (sym))
  10880. {
  10881. struct ada_exc_info info = {sym->print_name (),
  10882. SYMBOL_VALUE_ADDRESS (sym)};
  10883. exceptions->push_back (info);
  10884. }
  10885. }
  10886. }
  10887. if (BLOCK_FUNCTION (block) != NULL)
  10888. break;
  10889. block = BLOCK_SUPERBLOCK (block);
  10890. }
  10891. }
  10892. /* Return true if NAME matches PREG or if PREG is NULL. */
  10893. static bool
  10894. name_matches_regex (const char *name, compiled_regex *preg)
  10895. {
  10896. return (preg == NULL
  10897. || preg->exec (ada_decode (name).c_str (), 0, NULL, 0) == 0);
  10898. }
  10899. /* Add all exceptions defined globally whose name name match
  10900. a regular expression, excluding standard exceptions.
  10901. The reason we exclude standard exceptions is that they need
  10902. to be handled separately: Standard exceptions are defined inside
  10903. a runtime unit which is normally not compiled with debugging info,
  10904. and thus usually do not show up in our symbol search. However,
  10905. if the unit was in fact built with debugging info, we need to
  10906. exclude them because they would duplicate the entry we found
  10907. during the special loop that specifically searches for those
  10908. standard exceptions.
  10909. If PREG is not NULL, then this regexp_t object is used to
  10910. perform the symbol name matching. Otherwise, no name-based
  10911. filtering is performed.
  10912. EXCEPTIONS is a vector of exceptions to which matching exceptions
  10913. gets pushed. */
  10914. static void
  10915. ada_add_global_exceptions (compiled_regex *preg,
  10916. std::vector<ada_exc_info> *exceptions)
  10917. {
  10918. /* In Ada, the symbol "search name" is a linkage name, whereas the
  10919. regular expression used to do the matching refers to the natural
  10920. name. So match against the decoded name. */
  10921. expand_symtabs_matching (NULL,
  10922. lookup_name_info::match_any (),
  10923. [&] (const char *search_name)
  10924. {
  10925. std::string decoded = ada_decode (search_name);
  10926. return name_matches_regex (decoded.c_str (), preg);
  10927. },
  10928. NULL,
  10929. SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
  10930. VARIABLES_DOMAIN);
  10931. for (objfile *objfile : current_program_space->objfiles ())
  10932. {
  10933. for (compunit_symtab *s : objfile->compunits ())
  10934. {
  10935. const struct blockvector *bv = s->blockvector ();
  10936. int i;
  10937. for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
  10938. {
  10939. const struct block *b = BLOCKVECTOR_BLOCK (bv, i);
  10940. struct block_iterator iter;
  10941. struct symbol *sym;
  10942. ALL_BLOCK_SYMBOLS (b, iter, sym)
  10943. if (ada_is_non_standard_exception_sym (sym)
  10944. && name_matches_regex (sym->natural_name (), preg))
  10945. {
  10946. struct ada_exc_info info
  10947. = {sym->print_name (), SYMBOL_VALUE_ADDRESS (sym)};
  10948. exceptions->push_back (info);
  10949. }
  10950. }
  10951. }
  10952. }
  10953. }
  10954. /* Implements ada_exceptions_list with the regular expression passed
  10955. as a regex_t, rather than a string.
  10956. If not NULL, PREG is used to filter out exceptions whose names
  10957. do not match. Otherwise, all exceptions are listed. */
  10958. static std::vector<ada_exc_info>
  10959. ada_exceptions_list_1 (compiled_regex *preg)
  10960. {
  10961. std::vector<ada_exc_info> result;
  10962. int prev_len;
  10963. /* First, list the known standard exceptions. These exceptions
  10964. need to be handled separately, as they are usually defined in
  10965. runtime units that have been compiled without debugging info. */
  10966. ada_add_standard_exceptions (preg, &result);
  10967. /* Next, find all exceptions whose scope is local and accessible
  10968. from the currently selected frame. */
  10969. if (has_stack_frames ())
  10970. {
  10971. prev_len = result.size ();
  10972. ada_add_exceptions_from_frame (preg, get_selected_frame (NULL),
  10973. &result);
  10974. if (result.size () > prev_len)
  10975. sort_remove_dups_ada_exceptions_list (&result, prev_len);
  10976. }
  10977. /* Add all exceptions whose scope is global. */
  10978. prev_len = result.size ();
  10979. ada_add_global_exceptions (preg, &result);
  10980. if (result.size () > prev_len)
  10981. sort_remove_dups_ada_exceptions_list (&result, prev_len);
  10982. return result;
  10983. }
  10984. /* Return a vector of ada_exc_info.
  10985. If REGEXP is NULL, all exceptions are included in the result.
  10986. Otherwise, it should contain a valid regular expression,
  10987. and only the exceptions whose names match that regular expression
  10988. are included in the result.
  10989. The exceptions are sorted in the following order:
  10990. - Standard exceptions (defined by the Ada language), in
  10991. alphabetical order;
  10992. - Exceptions only visible from the current frame, in
  10993. alphabetical order;
  10994. - Exceptions whose scope is global, in alphabetical order. */
  10995. std::vector<ada_exc_info>
  10996. ada_exceptions_list (const char *regexp)
  10997. {
  10998. if (regexp == NULL)
  10999. return ada_exceptions_list_1 (NULL);
  11000. compiled_regex reg (regexp, REG_NOSUB, _("invalid regular expression"));
  11001. return ada_exceptions_list_1 (&reg);
  11002. }
  11003. /* Implement the "info exceptions" command. */
  11004. static void
  11005. info_exceptions_command (const char *regexp, int from_tty)
  11006. {
  11007. struct gdbarch *gdbarch = get_current_arch ();
  11008. std::vector<ada_exc_info> exceptions = ada_exceptions_list (regexp);
  11009. if (regexp != NULL)
  11010. gdb_printf
  11011. (_("All Ada exceptions matching regular expression \"%s\":\n"), regexp);
  11012. else
  11013. gdb_printf (_("All defined Ada exceptions:\n"));
  11014. for (const ada_exc_info &info : exceptions)
  11015. gdb_printf ("%s: %s\n", info.name, paddress (gdbarch, info.addr));
  11016. }
  11017. /* Language vector */
  11018. /* symbol_name_matcher_ftype adapter for wild_match. */
  11019. static bool
  11020. do_wild_match (const char *symbol_search_name,
  11021. const lookup_name_info &lookup_name,
  11022. completion_match_result *comp_match_res)
  11023. {
  11024. return wild_match (symbol_search_name, ada_lookup_name (lookup_name));
  11025. }
  11026. /* symbol_name_matcher_ftype adapter for full_match. */
  11027. static bool
  11028. do_full_match (const char *symbol_search_name,
  11029. const lookup_name_info &lookup_name,
  11030. completion_match_result *comp_match_res)
  11031. {
  11032. const char *lname = lookup_name.ada ().lookup_name ().c_str ();
  11033. /* If both symbols start with "_ada_", just let the loop below
  11034. handle the comparison. However, if only the symbol name starts
  11035. with "_ada_", skip the prefix and let the match proceed as
  11036. usual. */
  11037. if (startswith (symbol_search_name, "_ada_")
  11038. && !startswith (lname, "_ada"))
  11039. symbol_search_name += 5;
  11040. /* Likewise for ghost entities. */
  11041. if (startswith (symbol_search_name, "___ghost_")
  11042. && !startswith (lname, "___ghost_"))
  11043. symbol_search_name += 9;
  11044. int uscore_count = 0;
  11045. while (*lname != '\0')
  11046. {
  11047. if (*symbol_search_name != *lname)
  11048. {
  11049. if (*symbol_search_name == 'B' && uscore_count == 2
  11050. && symbol_search_name[1] == '_')
  11051. {
  11052. symbol_search_name += 2;
  11053. while (isdigit (*symbol_search_name))
  11054. ++symbol_search_name;
  11055. if (symbol_search_name[0] == '_'
  11056. && symbol_search_name[1] == '_')
  11057. {
  11058. symbol_search_name += 2;
  11059. continue;
  11060. }
  11061. }
  11062. return false;
  11063. }
  11064. if (*symbol_search_name == '_')
  11065. ++uscore_count;
  11066. else
  11067. uscore_count = 0;
  11068. ++symbol_search_name;
  11069. ++lname;
  11070. }
  11071. return is_name_suffix (symbol_search_name);
  11072. }
  11073. /* symbol_name_matcher_ftype for exact (verbatim) matches. */
  11074. static bool
  11075. do_exact_match (const char *symbol_search_name,
  11076. const lookup_name_info &lookup_name,
  11077. completion_match_result *comp_match_res)
  11078. {
  11079. return strcmp (symbol_search_name, ada_lookup_name (lookup_name)) == 0;
  11080. }
  11081. /* Build the Ada lookup name for LOOKUP_NAME. */
  11082. ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name)
  11083. {
  11084. gdb::string_view user_name = lookup_name.name ();
  11085. if (!user_name.empty () && user_name[0] == '<')
  11086. {
  11087. if (user_name.back () == '>')
  11088. m_encoded_name
  11089. = gdb::to_string (user_name.substr (1, user_name.size () - 2));
  11090. else
  11091. m_encoded_name
  11092. = gdb::to_string (user_name.substr (1, user_name.size () - 1));
  11093. m_encoded_p = true;
  11094. m_verbatim_p = true;
  11095. m_wild_match_p = false;
  11096. m_standard_p = false;
  11097. }
  11098. else
  11099. {
  11100. m_verbatim_p = false;
  11101. m_encoded_p = user_name.find ("__") != gdb::string_view::npos;
  11102. if (!m_encoded_p)
  11103. {
  11104. const char *folded = ada_fold_name (user_name);
  11105. m_encoded_name = ada_encode_1 (folded, false);
  11106. if (m_encoded_name.empty ())
  11107. m_encoded_name = gdb::to_string (user_name);
  11108. }
  11109. else
  11110. m_encoded_name = gdb::to_string (user_name);
  11111. /* Handle the 'package Standard' special case. See description
  11112. of m_standard_p. */
  11113. if (startswith (m_encoded_name.c_str (), "standard__"))
  11114. {
  11115. m_encoded_name = m_encoded_name.substr (sizeof ("standard__") - 1);
  11116. m_standard_p = true;
  11117. }
  11118. else
  11119. m_standard_p = false;
  11120. /* If the name contains a ".", then the user is entering a fully
  11121. qualified entity name, and the match must not be done in wild
  11122. mode. Similarly, if the user wants to complete what looks
  11123. like an encoded name, the match must not be done in wild
  11124. mode. Also, in the standard__ special case always do
  11125. non-wild matching. */
  11126. m_wild_match_p
  11127. = (lookup_name.match_type () != symbol_name_match_type::FULL
  11128. && !m_encoded_p
  11129. && !m_standard_p
  11130. && user_name.find ('.') == std::string::npos);
  11131. }
  11132. }
  11133. /* symbol_name_matcher_ftype method for Ada. This only handles
  11134. completion mode. */
  11135. static bool
  11136. ada_symbol_name_matches (const char *symbol_search_name,
  11137. const lookup_name_info &lookup_name,
  11138. completion_match_result *comp_match_res)
  11139. {
  11140. return lookup_name.ada ().matches (symbol_search_name,
  11141. lookup_name.match_type (),
  11142. comp_match_res);
  11143. }
  11144. /* A name matcher that matches the symbol name exactly, with
  11145. strcmp. */
  11146. static bool
  11147. literal_symbol_name_matcher (const char *symbol_search_name,
  11148. const lookup_name_info &lookup_name,
  11149. completion_match_result *comp_match_res)
  11150. {
  11151. gdb::string_view name_view = lookup_name.name ();
  11152. if (lookup_name.completion_mode ()
  11153. ? (strncmp (symbol_search_name, name_view.data (),
  11154. name_view.size ()) == 0)
  11155. : symbol_search_name == name_view)
  11156. {
  11157. if (comp_match_res != NULL)
  11158. comp_match_res->set_match (symbol_search_name);
  11159. return true;
  11160. }
  11161. else
  11162. return false;
  11163. }
  11164. /* Implement the "get_symbol_name_matcher" language_defn method for
  11165. Ada. */
  11166. static symbol_name_matcher_ftype *
  11167. ada_get_symbol_name_matcher (const lookup_name_info &lookup_name)
  11168. {
  11169. if (lookup_name.match_type () == symbol_name_match_type::SEARCH_NAME)
  11170. return literal_symbol_name_matcher;
  11171. if (lookup_name.completion_mode ())
  11172. return ada_symbol_name_matches;
  11173. else
  11174. {
  11175. if (lookup_name.ada ().wild_match_p ())
  11176. return do_wild_match;
  11177. else if (lookup_name.ada ().verbatim_p ())
  11178. return do_exact_match;
  11179. else
  11180. return do_full_match;
  11181. }
  11182. }
  11183. /* Class representing the Ada language. */
  11184. class ada_language : public language_defn
  11185. {
  11186. public:
  11187. ada_language ()
  11188. : language_defn (language_ada)
  11189. { /* Nothing. */ }
  11190. /* See language.h. */
  11191. const char *name () const override
  11192. { return "ada"; }
  11193. /* See language.h. */
  11194. const char *natural_name () const override
  11195. { return "Ada"; }
  11196. /* See language.h. */
  11197. const std::vector<const char *> &filename_extensions () const override
  11198. {
  11199. static const std::vector<const char *> extensions
  11200. = { ".adb", ".ads", ".a", ".ada", ".dg" };
  11201. return extensions;
  11202. }
  11203. /* Print an array element index using the Ada syntax. */
  11204. void print_array_index (struct type *index_type,
  11205. LONGEST index,
  11206. struct ui_file *stream,
  11207. const value_print_options *options) const override
  11208. {
  11209. struct value *index_value = val_atr (index_type, index);
  11210. value_print (index_value, stream, options);
  11211. gdb_printf (stream, " => ");
  11212. }
  11213. /* Implement the "read_var_value" language_defn method for Ada. */
  11214. struct value *read_var_value (struct symbol *var,
  11215. const struct block *var_block,
  11216. struct frame_info *frame) const override
  11217. {
  11218. /* The only case where default_read_var_value is not sufficient
  11219. is when VAR is a renaming... */
  11220. if (frame != nullptr)
  11221. {
  11222. const struct block *frame_block = get_frame_block (frame, NULL);
  11223. if (frame_block != nullptr && ada_is_renaming_symbol (var))
  11224. return ada_read_renaming_var_value (var, frame_block);
  11225. }
  11226. /* This is a typical case where we expect the default_read_var_value
  11227. function to work. */
  11228. return language_defn::read_var_value (var, var_block, frame);
  11229. }
  11230. /* See language.h. */
  11231. virtual bool symbol_printing_suppressed (struct symbol *symbol) const override
  11232. {
  11233. return symbol->artificial;
  11234. }
  11235. /* See language.h. */
  11236. void language_arch_info (struct gdbarch *gdbarch,
  11237. struct language_arch_info *lai) const override
  11238. {
  11239. const struct builtin_type *builtin = builtin_type (gdbarch);
  11240. /* Helper function to allow shorter lines below. */
  11241. auto add = [&] (struct type *t)
  11242. {
  11243. lai->add_primitive_type (t);
  11244. };
  11245. add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  11246. 0, "integer"));
  11247. add (arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
  11248. 0, "long_integer"));
  11249. add (arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch),
  11250. 0, "short_integer"));
  11251. struct type *char_type = arch_character_type (gdbarch, TARGET_CHAR_BIT,
  11252. 1, "character");
  11253. lai->set_string_char_type (char_type);
  11254. add (char_type);
  11255. add (arch_character_type (gdbarch, 16, 1, "wide_character"));
  11256. add (arch_character_type (gdbarch, 32, 1, "wide_wide_character"));
  11257. add (arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
  11258. "float", gdbarch_float_format (gdbarch)));
  11259. add (arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
  11260. "long_float", gdbarch_double_format (gdbarch)));
  11261. add (arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch),
  11262. 0, "long_long_integer"));
  11263. add (arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
  11264. "long_long_float",
  11265. gdbarch_long_double_format (gdbarch)));
  11266. add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  11267. 0, "natural"));
  11268. add (arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
  11269. 0, "positive"));
  11270. add (builtin->builtin_void);
  11271. struct type *system_addr_ptr
  11272. = lookup_pointer_type (arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
  11273. "void"));
  11274. system_addr_ptr->set_name ("system__address");
  11275. add (system_addr_ptr);
  11276. /* Create the equivalent of the System.Storage_Elements.Storage_Offset
  11277. type. This is a signed integral type whose size is the same as
  11278. the size of addresses. */
  11279. unsigned int addr_length = TYPE_LENGTH (system_addr_ptr);
  11280. add (arch_integer_type (gdbarch, addr_length * HOST_CHAR_BIT, 0,
  11281. "storage_offset"));
  11282. lai->set_bool_type (builtin->builtin_bool);
  11283. }
  11284. /* See language.h. */
  11285. bool iterate_over_symbols
  11286. (const struct block *block, const lookup_name_info &name,
  11287. domain_enum domain,
  11288. gdb::function_view<symbol_found_callback_ftype> callback) const override
  11289. {
  11290. std::vector<struct block_symbol> results
  11291. = ada_lookup_symbol_list_worker (name, block, domain, 0);
  11292. for (block_symbol &sym : results)
  11293. {
  11294. if (!callback (&sym))
  11295. return false;
  11296. }
  11297. return true;
  11298. }
  11299. /* See language.h. */
  11300. bool sniff_from_mangled_name
  11301. (const char *mangled,
  11302. gdb::unique_xmalloc_ptr<char> *out) const override
  11303. {
  11304. std::string demangled = ada_decode (mangled);
  11305. *out = NULL;
  11306. if (demangled != mangled && demangled[0] != '<')
  11307. {
  11308. /* Set the gsymbol language to Ada, but still return 0.
  11309. Two reasons for that:
  11310. 1. For Ada, we prefer computing the symbol's decoded name
  11311. on the fly rather than pre-compute it, in order to save
  11312. memory (Ada projects are typically very large).
  11313. 2. There are some areas in the definition of the GNAT
  11314. encoding where, with a bit of bad luck, we might be able
  11315. to decode a non-Ada symbol, generating an incorrect
  11316. demangled name (Eg: names ending with "TB" for instance
  11317. are identified as task bodies and so stripped from
  11318. the decoded name returned).
  11319. Returning true, here, but not setting *DEMANGLED, helps us get
  11320. a little bit of the best of both worlds. Because we're last,
  11321. we should not affect any of the other languages that were
  11322. able to demangle the symbol before us; we get to correctly
  11323. tag Ada symbols as such; and even if we incorrectly tagged a
  11324. non-Ada symbol, which should be rare, any routing through the
  11325. Ada language should be transparent (Ada tries to behave much
  11326. like C/C++ with non-Ada symbols). */
  11327. return true;
  11328. }
  11329. return false;
  11330. }
  11331. /* See language.h. */
  11332. gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
  11333. int options) const override
  11334. {
  11335. return make_unique_xstrdup (ada_decode (mangled).c_str ());
  11336. }
  11337. /* See language.h. */
  11338. void print_type (struct type *type, const char *varstring,
  11339. struct ui_file *stream, int show, int level,
  11340. const struct type_print_options *flags) const override
  11341. {
  11342. ada_print_type (type, varstring, stream, show, level, flags);
  11343. }
  11344. /* See language.h. */
  11345. const char *word_break_characters (void) const override
  11346. {
  11347. return ada_completer_word_break_characters;
  11348. }
  11349. /* See language.h. */
  11350. void collect_symbol_completion_matches (completion_tracker &tracker,
  11351. complete_symbol_mode mode,
  11352. symbol_name_match_type name_match_type,
  11353. const char *text, const char *word,
  11354. enum type_code code) const override
  11355. {
  11356. struct symbol *sym;
  11357. const struct block *b, *surrounding_static_block = 0;
  11358. struct block_iterator iter;
  11359. gdb_assert (code == TYPE_CODE_UNDEF);
  11360. lookup_name_info lookup_name (text, name_match_type, true);
  11361. /* First, look at the partial symtab symbols. */
  11362. expand_symtabs_matching (NULL,
  11363. lookup_name,
  11364. NULL,
  11365. NULL,
  11366. SEARCH_GLOBAL_BLOCK | SEARCH_STATIC_BLOCK,
  11367. ALL_DOMAIN);
  11368. /* At this point scan through the misc symbol vectors and add each
  11369. symbol you find to the list. Eventually we want to ignore
  11370. anything that isn't a text symbol (everything else will be
  11371. handled by the psymtab code above). */
  11372. for (objfile *objfile : current_program_space->objfiles ())
  11373. {
  11374. for (minimal_symbol *msymbol : objfile->msymbols ())
  11375. {
  11376. QUIT;
  11377. if (completion_skip_symbol (mode, msymbol))
  11378. continue;
  11379. language symbol_language = msymbol->language ();
  11380. /* Ada minimal symbols won't have their language set to Ada. If
  11381. we let completion_list_add_name compare using the
  11382. default/C-like matcher, then when completing e.g., symbols in a
  11383. package named "pck", we'd match internal Ada symbols like
  11384. "pckS", which are invalid in an Ada expression, unless you wrap
  11385. them in '<' '>' to request a verbatim match.
  11386. Unfortunately, some Ada encoded names successfully demangle as
  11387. C++ symbols (using an old mangling scheme), such as "name__2Xn"
  11388. -> "Xn::name(void)" and thus some Ada minimal symbols end up
  11389. with the wrong language set. Paper over that issue here. */
  11390. if (symbol_language == language_auto
  11391. || symbol_language == language_cplus)
  11392. symbol_language = language_ada;
  11393. completion_list_add_name (tracker,
  11394. symbol_language,
  11395. msymbol->linkage_name (),
  11396. lookup_name, text, word);
  11397. }
  11398. }
  11399. /* Search upwards from currently selected frame (so that we can
  11400. complete on local vars. */
  11401. for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
  11402. {
  11403. if (!BLOCK_SUPERBLOCK (b))
  11404. surrounding_static_block = b; /* For elmin of dups */
  11405. ALL_BLOCK_SYMBOLS (b, iter, sym)
  11406. {
  11407. if (completion_skip_symbol (mode, sym))
  11408. continue;
  11409. completion_list_add_name (tracker,
  11410. sym->language (),
  11411. sym->linkage_name (),
  11412. lookup_name, text, word);
  11413. }
  11414. }
  11415. /* Go through the symtabs and check the externs and statics for
  11416. symbols which match. */
  11417. for (objfile *objfile : current_program_space->objfiles ())
  11418. {
  11419. for (compunit_symtab *s : objfile->compunits ())
  11420. {
  11421. QUIT;
  11422. b = BLOCKVECTOR_BLOCK (s->blockvector (), GLOBAL_BLOCK);
  11423. ALL_BLOCK_SYMBOLS (b, iter, sym)
  11424. {
  11425. if (completion_skip_symbol (mode, sym))
  11426. continue;
  11427. completion_list_add_name (tracker,
  11428. sym->language (),
  11429. sym->linkage_name (),
  11430. lookup_name, text, word);
  11431. }
  11432. }
  11433. }
  11434. for (objfile *objfile : current_program_space->objfiles ())
  11435. {
  11436. for (compunit_symtab *s : objfile->compunits ())
  11437. {
  11438. QUIT;
  11439. b = BLOCKVECTOR_BLOCK (s->blockvector (), STATIC_BLOCK);
  11440. /* Don't do this block twice. */
  11441. if (b == surrounding_static_block)
  11442. continue;
  11443. ALL_BLOCK_SYMBOLS (b, iter, sym)
  11444. {
  11445. if (completion_skip_symbol (mode, sym))
  11446. continue;
  11447. completion_list_add_name (tracker,
  11448. sym->language (),
  11449. sym->linkage_name (),
  11450. lookup_name, text, word);
  11451. }
  11452. }
  11453. }
  11454. }
  11455. /* See language.h. */
  11456. gdb::unique_xmalloc_ptr<char> watch_location_expression
  11457. (struct type *type, CORE_ADDR addr) const override
  11458. {
  11459. type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
  11460. std::string name = type_to_string (type);
  11461. return xstrprintf ("{%s} %s", name.c_str (), core_addr_to_string (addr));
  11462. }
  11463. /* See language.h. */
  11464. void value_print (struct value *val, struct ui_file *stream,
  11465. const struct value_print_options *options) const override
  11466. {
  11467. return ada_value_print (val, stream, options);
  11468. }
  11469. /* See language.h. */
  11470. void value_print_inner
  11471. (struct value *val, struct ui_file *stream, int recurse,
  11472. const struct value_print_options *options) const override
  11473. {
  11474. return ada_value_print_inner (val, stream, recurse, options);
  11475. }
  11476. /* See language.h. */
  11477. struct block_symbol lookup_symbol_nonlocal
  11478. (const char *name, const struct block *block,
  11479. const domain_enum domain) const override
  11480. {
  11481. struct block_symbol sym;
  11482. sym = ada_lookup_symbol (name, block_static_block (block), domain);
  11483. if (sym.symbol != NULL)
  11484. return sym;
  11485. /* If we haven't found a match at this point, try the primitive
  11486. types. In other languages, this search is performed before
  11487. searching for global symbols in order to short-circuit that
  11488. global-symbol search if it happens that the name corresponds
  11489. to a primitive type. But we cannot do the same in Ada, because
  11490. it is perfectly legitimate for a program to declare a type which
  11491. has the same name as a standard type. If looking up a type in
  11492. that situation, we have traditionally ignored the primitive type
  11493. in favor of user-defined types. This is why, unlike most other
  11494. languages, we search the primitive types this late and only after
  11495. having searched the global symbols without success. */
  11496. if (domain == VAR_DOMAIN)
  11497. {
  11498. struct gdbarch *gdbarch;
  11499. if (block == NULL)
  11500. gdbarch = target_gdbarch ();
  11501. else
  11502. gdbarch = block_gdbarch (block);
  11503. sym.symbol
  11504. = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
  11505. if (sym.symbol != NULL)
  11506. return sym;
  11507. }
  11508. return {};
  11509. }
  11510. /* See language.h. */
  11511. int parser (struct parser_state *ps) const override
  11512. {
  11513. warnings_issued = 0;
  11514. return ada_parse (ps);
  11515. }
  11516. /* See language.h. */
  11517. void emitchar (int ch, struct type *chtype,
  11518. struct ui_file *stream, int quoter) const override
  11519. {
  11520. ada_emit_char (ch, chtype, stream, quoter, 1);
  11521. }
  11522. /* See language.h. */
  11523. void printchar (int ch, struct type *chtype,
  11524. struct ui_file *stream) const override
  11525. {
  11526. ada_printchar (ch, chtype, stream);
  11527. }
  11528. /* See language.h. */
  11529. void printstr (struct ui_file *stream, struct type *elttype,
  11530. const gdb_byte *string, unsigned int length,
  11531. const char *encoding, int force_ellipses,
  11532. const struct value_print_options *options) const override
  11533. {
  11534. ada_printstr (stream, elttype, string, length, encoding,
  11535. force_ellipses, options);
  11536. }
  11537. /* See language.h. */
  11538. void print_typedef (struct type *type, struct symbol *new_symbol,
  11539. struct ui_file *stream) const override
  11540. {
  11541. ada_print_typedef (type, new_symbol, stream);
  11542. }
  11543. /* See language.h. */
  11544. bool is_string_type_p (struct type *type) const override
  11545. {
  11546. return ada_is_string_type (type);
  11547. }
  11548. /* See language.h. */
  11549. const char *struct_too_deep_ellipsis () const override
  11550. { return "(...)"; }
  11551. /* See language.h. */
  11552. bool c_style_arrays_p () const override
  11553. { return false; }
  11554. /* See language.h. */
  11555. bool store_sym_names_in_linkage_form_p () const override
  11556. { return true; }
  11557. /* See language.h. */
  11558. const struct lang_varobj_ops *varobj_ops () const override
  11559. { return &ada_varobj_ops; }
  11560. protected:
  11561. /* See language.h. */
  11562. symbol_name_matcher_ftype *get_symbol_name_matcher_inner
  11563. (const lookup_name_info &lookup_name) const override
  11564. {
  11565. return ada_get_symbol_name_matcher (lookup_name);
  11566. }
  11567. };
  11568. /* Single instance of the Ada language class. */
  11569. static ada_language ada_language_defn;
  11570. /* Command-list for the "set/show ada" prefix command. */
  11571. static struct cmd_list_element *set_ada_list;
  11572. static struct cmd_list_element *show_ada_list;
  11573. static void
  11574. initialize_ada_catchpoint_ops (void)
  11575. {
  11576. struct breakpoint_ops *ops;
  11577. initialize_breakpoint_ops ();
  11578. ops = &catch_exception_breakpoint_ops;
  11579. *ops = bkpt_breakpoint_ops;
  11580. ops->allocate_location = allocate_location_exception;
  11581. ops->re_set = re_set_exception;
  11582. ops->check_status = check_status_exception;
  11583. ops->print_it = print_it_exception;
  11584. ops->print_one = print_one_exception;
  11585. ops->print_mention = print_mention_exception;
  11586. ops->print_recreate = print_recreate_exception;
  11587. }
  11588. /* This module's 'new_objfile' observer. */
  11589. static void
  11590. ada_new_objfile_observer (struct objfile *objfile)
  11591. {
  11592. ada_clear_symbol_cache ();
  11593. }
  11594. /* This module's 'free_objfile' observer. */
  11595. static void
  11596. ada_free_objfile_observer (struct objfile *objfile)
  11597. {
  11598. ada_clear_symbol_cache ();
  11599. }
  11600. /* Charsets known to GNAT. */
  11601. static const char * const gnat_source_charsets[] =
  11602. {
  11603. /* Note that code below assumes that the default comes first.
  11604. Latin-1 is the default here, because that is also GNAT's
  11605. default. */
  11606. "ISO-8859-1",
  11607. "ISO-8859-2",
  11608. "ISO-8859-3",
  11609. "ISO-8859-4",
  11610. "ISO-8859-5",
  11611. "ISO-8859-15",
  11612. "CP437",
  11613. "CP850",
  11614. /* Note that this value is special-cased in the encoder and
  11615. decoder. */
  11616. ada_utf8,
  11617. nullptr
  11618. };
  11619. void _initialize_ada_language ();
  11620. void
  11621. _initialize_ada_language ()
  11622. {
  11623. initialize_ada_catchpoint_ops ();
  11624. add_setshow_prefix_cmd
  11625. ("ada", no_class,
  11626. _("Prefix command for changing Ada-specific settings."),
  11627. _("Generic command for showing Ada-specific settings."),
  11628. &set_ada_list, &show_ada_list,
  11629. &setlist, &showlist);
  11630. add_setshow_boolean_cmd ("trust-PAD-over-XVS", class_obscure,
  11631. &trust_pad_over_xvs, _("\
  11632. Enable or disable an optimization trusting PAD types over XVS types."), _("\
  11633. Show whether an optimization trusting PAD types over XVS types is activated."),
  11634. _("\
  11635. This is related to the encoding used by the GNAT compiler. The debugger\n\
  11636. should normally trust the contents of PAD types, but certain older versions\n\
  11637. of GNAT have a bug that sometimes causes the information in the PAD type\n\
  11638. to be incorrect. Turning this setting \"off\" allows the debugger to\n\
  11639. work around this bug. It is always safe to turn this option \"off\", but\n\
  11640. this incurs a slight performance penalty, so it is recommended to NOT change\n\
  11641. this option to \"off\" unless necessary."),
  11642. NULL, NULL, &set_ada_list, &show_ada_list);
  11643. add_setshow_boolean_cmd ("print-signatures", class_vars,
  11644. &print_signatures, _("\
  11645. Enable or disable the output of formal and return types for functions in the \
  11646. overloads selection menu."), _("\
  11647. Show whether the output of formal and return types for functions in the \
  11648. overloads selection menu is activated."),
  11649. NULL, NULL, NULL, &set_ada_list, &show_ada_list);
  11650. ada_source_charset = gnat_source_charsets[0];
  11651. add_setshow_enum_cmd ("source-charset", class_files,
  11652. gnat_source_charsets,
  11653. &ada_source_charset, _("\
  11654. Set the Ada source character set."), _("\
  11655. Show the Ada source character set."), _("\
  11656. The character set used for Ada source files.\n\
  11657. This must correspond to the '-gnati' or '-gnatW' option passed to GNAT."),
  11658. nullptr, nullptr,
  11659. &set_ada_list, &show_ada_list);
  11660. add_catch_command ("exception", _("\
  11661. Catch Ada exceptions, when raised.\n\
  11662. Usage: catch exception [ARG] [if CONDITION]\n\
  11663. Without any argument, stop when any Ada exception is raised.\n\
  11664. If ARG is \"unhandled\" (without the quotes), only stop when the exception\n\
  11665. being raised does not have a handler (and will therefore lead to the task's\n\
  11666. termination).\n\
  11667. Otherwise, the catchpoint only stops when the name of the exception being\n\
  11668. raised is the same as ARG.\n\
  11669. CONDITION is a boolean expression that is evaluated to see whether the\n\
  11670. exception should cause a stop."),
  11671. catch_ada_exception_command,
  11672. catch_ada_completer,
  11673. CATCH_PERMANENT,
  11674. CATCH_TEMPORARY);
  11675. add_catch_command ("handlers", _("\
  11676. Catch Ada exceptions, when handled.\n\
  11677. Usage: catch handlers [ARG] [if CONDITION]\n\
  11678. Without any argument, stop when any Ada exception is handled.\n\
  11679. With an argument, catch only exceptions with the given name.\n\
  11680. CONDITION is a boolean expression that is evaluated to see whether the\n\
  11681. exception should cause a stop."),
  11682. catch_ada_handlers_command,
  11683. catch_ada_completer,
  11684. CATCH_PERMANENT,
  11685. CATCH_TEMPORARY);
  11686. add_catch_command ("assert", _("\
  11687. Catch failed Ada assertions, when raised.\n\
  11688. Usage: catch assert [if CONDITION]\n\
  11689. CONDITION is a boolean expression that is evaluated to see whether the\n\
  11690. exception should cause a stop."),
  11691. catch_assert_command,
  11692. NULL,
  11693. CATCH_PERMANENT,
  11694. CATCH_TEMPORARY);
  11695. add_info ("exceptions", info_exceptions_command,
  11696. _("\
  11697. List all Ada exception names.\n\
  11698. Usage: info exceptions [REGEXP]\n\
  11699. If a regular expression is passed as an argument, only those matching\n\
  11700. the regular expression are listed."));
  11701. add_setshow_prefix_cmd ("ada", class_maintenance,
  11702. _("Set Ada maintenance-related variables."),
  11703. _("Show Ada maintenance-related variables."),
  11704. &maint_set_ada_cmdlist, &maint_show_ada_cmdlist,
  11705. &maintenance_set_cmdlist, &maintenance_show_cmdlist);
  11706. add_setshow_boolean_cmd
  11707. ("ignore-descriptive-types", class_maintenance,
  11708. &ada_ignore_descriptive_types_p,
  11709. _("Set whether descriptive types generated by GNAT should be ignored."),
  11710. _("Show whether descriptive types generated by GNAT should be ignored."),
  11711. _("\
  11712. When enabled, the debugger will stop using the DW_AT_GNAT_descriptive_type\n\
  11713. DWARF attribute."),
  11714. NULL, NULL, &maint_set_ada_cmdlist, &maint_show_ada_cmdlist);
  11715. decoded_names_store = htab_create_alloc (256, htab_hash_string,
  11716. htab_eq_string,
  11717. NULL, xcalloc, xfree);
  11718. /* The ada-lang observers. */
  11719. gdb::observers::new_objfile.attach (ada_new_objfile_observer, "ada-lang");
  11720. gdb::observers::free_objfile.attach (ada_free_objfile_observer, "ada-lang");
  11721. gdb::observers::inferior_exit.attach (ada_inferior_exit, "ada-lang");
  11722. }