c-common.c 385 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322
  1. /* Subroutines shared by all languages that are variants of C.
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #define GCC_C_COMMON_C
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "input.h"
  20. #include "c-common.h"
  21. #include "tm.h"
  22. #include "intl.h"
  23. #include "tree.h"
  24. #include "fold-const.h"
  25. #include "stor-layout.h"
  26. #include "calls.h"
  27. #include "stringpool.h"
  28. #include "attribs.h"
  29. #include "varasm.h"
  30. #include "trans-mem.h"
  31. #include "flags.h"
  32. #include "c-pragma.h"
  33. #include "c-objc.h"
  34. #include "tm_p.h"
  35. #include "obstack.h"
  36. #include "cpplib.h"
  37. #include "target.h"
  38. #include "common/common-target.h"
  39. #include "langhooks.h"
  40. #include "tree-inline.h"
  41. #include "toplev.h"
  42. #include "diagnostic.h"
  43. #include "tree-iterator.h"
  44. #include "hashtab.h"
  45. #include "opts.h"
  46. #include "hash-map.h"
  47. #include "is-a.h"
  48. #include "plugin-api.h"
  49. #include "vec.h"
  50. #include "hash-set.h"
  51. #include "machmode.h"
  52. #include "hard-reg-set.h"
  53. #include "input.h"
  54. #include "function.h"
  55. #include "ipa-ref.h"
  56. #include "cgraph.h"
  57. #include "target-def.h"
  58. #include "gimplify.h"
  59. #include "wide-int-print.h"
  60. #include "gimple-expr.h"
  61. cpp_reader *parse_in; /* Declared in c-pragma.h. */
  62. /* Mode used to build pointers (VOIDmode means ptr_mode). */
  63. machine_mode c_default_pointer_mode = VOIDmode;
  64. /* The following symbols are subsumed in the c_global_trees array, and
  65. listed here individually for documentation purposes.
  66. INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
  67. tree short_integer_type_node;
  68. tree long_integer_type_node;
  69. tree long_long_integer_type_node;
  70. tree short_unsigned_type_node;
  71. tree long_unsigned_type_node;
  72. tree long_long_unsigned_type_node;
  73. tree truthvalue_type_node;
  74. tree truthvalue_false_node;
  75. tree truthvalue_true_node;
  76. tree ptrdiff_type_node;
  77. tree unsigned_char_type_node;
  78. tree signed_char_type_node;
  79. tree wchar_type_node;
  80. tree char16_type_node;
  81. tree char32_type_node;
  82. tree float_type_node;
  83. tree double_type_node;
  84. tree long_double_type_node;
  85. tree complex_integer_type_node;
  86. tree complex_float_type_node;
  87. tree complex_double_type_node;
  88. tree complex_long_double_type_node;
  89. tree dfloat32_type_node;
  90. tree dfloat64_type_node;
  91. tree_dfloat128_type_node;
  92. tree intQI_type_node;
  93. tree intHI_type_node;
  94. tree intSI_type_node;
  95. tree intDI_type_node;
  96. tree intTI_type_node;
  97. tree unsigned_intQI_type_node;
  98. tree unsigned_intHI_type_node;
  99. tree unsigned_intSI_type_node;
  100. tree unsigned_intDI_type_node;
  101. tree unsigned_intTI_type_node;
  102. tree widest_integer_literal_type_node;
  103. tree widest_unsigned_literal_type_node;
  104. Nodes for types `void *' and `const void *'.
  105. tree ptr_type_node, const_ptr_type_node;
  106. Nodes for types `char *' and `const char *'.
  107. tree string_type_node, const_string_type_node;
  108. Type `char[SOMENUMBER]'.
  109. Used when an array of char is needed and the size is irrelevant.
  110. tree char_array_type_node;
  111. Type `wchar_t[SOMENUMBER]' or something like it.
  112. Used when a wide string literal is created.
  113. tree wchar_array_type_node;
  114. Type `char16_t[SOMENUMBER]' or something like it.
  115. Used when a UTF-16 string literal is created.
  116. tree char16_array_type_node;
  117. Type `char32_t[SOMENUMBER]' or something like it.
  118. Used when a UTF-32 string literal is created.
  119. tree char32_array_type_node;
  120. Type `int ()' -- used for implicit declaration of functions.
  121. tree default_function_type;
  122. A VOID_TYPE node, packaged in a TREE_LIST.
  123. tree void_list_node;
  124. The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
  125. and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
  126. VAR_DECLS, but C++ does.)
  127. tree function_name_decl_node;
  128. tree pretty_function_name_decl_node;
  129. tree c99_function_name_decl_node;
  130. Stack of nested function name VAR_DECLs.
  131. tree saved_function_name_decls;
  132. */
  133. tree c_global_trees[CTI_MAX];
  134. /* Switches common to the C front ends. */
  135. /* Nonzero means don't output line number information. */
  136. char flag_no_line_commands;
  137. /* Nonzero causes -E output not to be done, but directives such as
  138. #define that have side effects are still obeyed. */
  139. char flag_no_output;
  140. /* Nonzero means dump macros in some fashion. */
  141. char flag_dump_macros;
  142. /* Nonzero means pass #include lines through to the output. */
  143. char flag_dump_includes;
  144. /* Nonzero means process PCH files while preprocessing. */
  145. bool flag_pch_preprocess;
  146. /* The file name to which we should write a precompiled header, or
  147. NULL if no header will be written in this compile. */
  148. const char *pch_file;
  149. /* Nonzero if an ISO standard was selected. It rejects macros in the
  150. user's namespace. */
  151. int flag_iso;
  152. /* C/ObjC language option variables. */
  153. /* Nonzero means allow type mismatches in conditional expressions;
  154. just make their values `void'. */
  155. int flag_cond_mismatch;
  156. /* Nonzero means enable C89 Amendment 1 features. */
  157. int flag_isoc94;
  158. /* Nonzero means use the ISO C99 (or C11) dialect of C. */
  159. int flag_isoc99;
  160. /* Nonzero means use the ISO C11 dialect of C. */
  161. int flag_isoc11;
  162. /* Nonzero means that we have builtin functions, and main is an int. */
  163. int flag_hosted = 1;
  164. /* ObjC language option variables. */
  165. /* Tells the compiler that this is a special run. Do not perform any
  166. compiling, instead we are to test some platform dependent features
  167. and output a C header file with appropriate definitions. */
  168. int print_struct_values;
  169. /* Tells the compiler what is the constant string class for ObjC. */
  170. const char *constant_string_class_name;
  171. /* C++ language option variables. */
  172. /* Nonzero means generate separate instantiation control files and
  173. juggle them at link time. */
  174. int flag_use_repository;
  175. /* The C++ dialect being used. C++98 is the default. */
  176. enum cxx_dialect cxx_dialect = cxx98;
  177. /* Maximum template instantiation depth. This limit exists to limit the
  178. time it takes to notice excessively recursive template instantiations.
  179. The default is lower than the 1024 recommended by the C++0x standard
  180. because G++ runs out of stack before 1024 with highly recursive template
  181. argument deduction substitution (g++.dg/cpp0x/enum11.C). */
  182. int max_tinst_depth = 900;
  183. /* The elements of `ridpointers' are identifier nodes for the reserved
  184. type names and storage classes. It is indexed by a RID_... value. */
  185. tree *ridpointers;
  186. tree (*make_fname_decl) (location_t, tree, int);
  187. /* Nonzero means don't warn about problems that occur when the code is
  188. executed. */
  189. int c_inhibit_evaluation_warnings;
  190. /* Whether we are building a boolean conversion inside
  191. convert_for_assignment, or some other late binary operation. If
  192. build_binary_op is called for C (from code shared by C and C++) in
  193. this case, then the operands have already been folded and the
  194. result will not be folded again, so C_MAYBE_CONST_EXPR should not
  195. be generated. */
  196. bool in_late_binary_op;
  197. /* Whether lexing has been completed, so subsequent preprocessor
  198. errors should use the compiler's input_location. */
  199. bool done_lexing = false;
  200. /* Information about how a function name is generated. */
  201. struct fname_var_t
  202. {
  203. tree *const decl; /* pointer to the VAR_DECL. */
  204. const unsigned rid; /* RID number for the identifier. */
  205. const int pretty; /* How pretty is it? */
  206. };
  207. /* The three ways of getting then name of the current function. */
  208. const struct fname_var_t fname_vars[] =
  209. {
  210. /* C99 compliant __func__, must be first. */
  211. {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
  212. /* GCC __FUNCTION__ compliant. */
  213. {&function_name_decl_node, RID_FUNCTION_NAME, 0},
  214. /* GCC __PRETTY_FUNCTION__ compliant. */
  215. {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
  216. {NULL, 0, 0},
  217. };
  218. /* Global visibility options. */
  219. struct visibility_flags visibility_options;
  220. static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
  221. static tree check_case_value (location_t, tree);
  222. static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
  223. static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
  224. static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
  225. static tree handle_common_attribute (tree *, tree, tree, int, bool *);
  226. static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
  227. static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
  228. static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
  229. static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
  230. int, bool *);
  231. static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
  232. int, bool *);
  233. static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
  234. bool *);
  235. static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
  236. static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
  237. static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
  238. static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
  239. static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
  240. static tree handle_always_inline_attribute (tree *, tree, tree, int,
  241. bool *);
  242. static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
  243. static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
  244. static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
  245. static tree handle_error_attribute (tree *, tree, tree, int, bool *);
  246. static tree handle_used_attribute (tree *, tree, tree, int, bool *);
  247. static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
  248. static tree handle_externally_visible_attribute (tree *, tree, tree, int,
  249. bool *);
  250. static tree handle_no_reorder_attribute (tree *, tree, tree, int,
  251. bool *);
  252. static tree handle_const_attribute (tree *, tree, tree, int, bool *);
  253. static tree handle_transparent_union_attribute (tree *, tree, tree,
  254. int, bool *);
  255. static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
  256. static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
  257. static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
  258. static tree handle_section_attribute (tree *, tree, tree, int, bool *);
  259. static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
  260. static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
  261. static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
  262. static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
  263. static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
  264. static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
  265. static tree handle_visibility_attribute (tree *, tree, tree, int,
  266. bool *);
  267. static tree handle_tls_model_attribute (tree *, tree, tree, int,
  268. bool *);
  269. static tree handle_no_instrument_function_attribute (tree *, tree,
  270. tree, int, bool *);
  271. static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
  272. static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
  273. static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
  274. bool *);
  275. static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
  276. static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
  277. static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
  278. static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
  279. static tree handle_deprecated_attribute (tree *, tree, tree, int,
  280. bool *);
  281. static tree handle_vector_size_attribute (tree *, tree, tree, int,
  282. bool *);
  283. static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
  284. static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
  285. static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
  286. static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
  287. bool *);
  288. static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
  289. static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
  290. static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
  291. static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
  292. static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
  293. static tree handle_target_attribute (tree *, tree, tree, int, bool *);
  294. static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
  295. static tree ignore_attribute (tree *, tree, tree, int, bool *);
  296. static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
  297. static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
  298. static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
  299. static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
  300. static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
  301. bool *);
  302. static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
  303. bool *);
  304. static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
  305. static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
  306. static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
  307. static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
  308. static void check_function_nonnull (tree, int, tree *);
  309. static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
  310. static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
  311. static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
  312. static int resort_field_decl_cmp (const void *, const void *);
  313. /* Reserved words. The third field is a mask: keywords are disabled
  314. if they match the mask.
  315. Masks for languages:
  316. C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
  317. C --std=c99: D_CXXONLY | D_OBJC
  318. ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
  319. C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
  320. C++ --std=c0x: D_CONLY | D_OBJC
  321. ObjC++ is like C++ except that D_OBJC is not set
  322. If -fno-asm is used, D_ASM is added to the mask. If
  323. -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
  324. C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
  325. In C with -Wc++-compat, we warn if D_CXXWARN is set.
  326. Note the complication of the D_CXX_OBJC keywords. These are
  327. reserved words such as 'class'. In C++, 'class' is a reserved
  328. word. In Objective-C++ it is too. In Objective-C, it is a
  329. reserved word too, but only if it follows an '@' sign.
  330. */
  331. const struct c_common_resword c_common_reswords[] =
  332. {
  333. { "_Alignas", RID_ALIGNAS, D_CONLY },
  334. { "_Alignof", RID_ALIGNOF, D_CONLY },
  335. { "_Atomic", RID_ATOMIC, D_CONLY },
  336. { "_Bool", RID_BOOL, D_CONLY },
  337. { "_Complex", RID_COMPLEX, 0 },
  338. { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
  339. { "_Cilk_sync", RID_CILK_SYNC, 0 },
  340. { "_Cilk_for", RID_CILK_FOR, 0 },
  341. { "_Imaginary", RID_IMAGINARY, D_CONLY },
  342. { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
  343. { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
  344. { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
  345. { "_Fract", RID_FRACT, D_CONLY | D_EXT },
  346. { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
  347. { "_Sat", RID_SAT, D_CONLY | D_EXT },
  348. { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
  349. { "_Noreturn", RID_NORETURN, D_CONLY },
  350. { "_Generic", RID_GENERIC, D_CONLY },
  351. { "_Thread_local", RID_THREAD, D_CONLY },
  352. { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
  353. { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
  354. { "__alignof", RID_ALIGNOF, 0 },
  355. { "__alignof__", RID_ALIGNOF, 0 },
  356. { "__asm", RID_ASM, 0 },
  357. { "__asm__", RID_ASM, 0 },
  358. { "__attribute", RID_ATTRIBUTE, 0 },
  359. { "__attribute__", RID_ATTRIBUTE, 0 },
  360. { "__auto_type", RID_AUTO_TYPE, D_CONLY },
  361. { "__bases", RID_BASES, D_CXXONLY },
  362. { "__builtin_call_with_static_chain",
  363. RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
  364. { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
  365. { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
  366. { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
  367. { "__builtin_offsetof", RID_OFFSETOF, 0 },
  368. { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
  369. { "__builtin_va_arg", RID_VA_ARG, 0 },
  370. { "__complex", RID_COMPLEX, 0 },
  371. { "__complex__", RID_COMPLEX, 0 },
  372. { "__const", RID_CONST, 0 },
  373. { "__const__", RID_CONST, 0 },
  374. { "__decltype", RID_DECLTYPE, D_CXXONLY },
  375. { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
  376. { "__extension__", RID_EXTENSION, 0 },
  377. { "__func__", RID_C99_FUNCTION_NAME, 0 },
  378. { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
  379. { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
  380. { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
  381. { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
  382. { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
  383. { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
  384. { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
  385. { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
  386. { "__imag", RID_IMAGPART, 0 },
  387. { "__imag__", RID_IMAGPART, 0 },
  388. { "__inline", RID_INLINE, 0 },
  389. { "__inline__", RID_INLINE, 0 },
  390. { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
  391. { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
  392. { "__is_class", RID_IS_CLASS, D_CXXONLY },
  393. { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
  394. { "__is_enum", RID_IS_ENUM, D_CXXONLY },
  395. { "__is_final", RID_IS_FINAL, D_CXXONLY },
  396. { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
  397. { "__is_pod", RID_IS_POD, D_CXXONLY },
  398. { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
  399. { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
  400. { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
  401. { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
  402. { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
  403. { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
  404. { "__is_union", RID_IS_UNION, D_CXXONLY },
  405. { "__label__", RID_LABEL, 0 },
  406. { "__null", RID_NULL, 0 },
  407. { "__real", RID_REALPART, 0 },
  408. { "__real__", RID_REALPART, 0 },
  409. { "__restrict", RID_RESTRICT, 0 },
  410. { "__restrict__", RID_RESTRICT, 0 },
  411. { "__signed", RID_SIGNED, 0 },
  412. { "__signed__", RID_SIGNED, 0 },
  413. { "__thread", RID_THREAD, 0 },
  414. { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
  415. { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
  416. { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
  417. { "__typeof", RID_TYPEOF, 0 },
  418. { "__typeof__", RID_TYPEOF, 0 },
  419. { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
  420. { "__volatile", RID_VOLATILE, 0 },
  421. { "__volatile__", RID_VOLATILE, 0 },
  422. { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
  423. { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
  424. { "asm", RID_ASM, D_ASM },
  425. { "auto", RID_AUTO, 0 },
  426. { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
  427. { "break", RID_BREAK, 0 },
  428. { "case", RID_CASE, 0 },
  429. { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
  430. { "char", RID_CHAR, 0 },
  431. { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
  432. { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
  433. { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
  434. { "const", RID_CONST, 0 },
  435. { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
  436. { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
  437. { "continue", RID_CONTINUE, 0 },
  438. { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
  439. { "default", RID_DEFAULT, 0 },
  440. { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
  441. { "do", RID_DO, 0 },
  442. { "double", RID_DOUBLE, 0 },
  443. { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
  444. { "else", RID_ELSE, 0 },
  445. { "enum", RID_ENUM, 0 },
  446. { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
  447. { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
  448. { "extern", RID_EXTERN, 0 },
  449. { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
  450. { "float", RID_FLOAT, 0 },
  451. { "for", RID_FOR, 0 },
  452. { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
  453. { "goto", RID_GOTO, 0 },
  454. { "if", RID_IF, 0 },
  455. { "inline", RID_INLINE, D_EXT89 },
  456. { "int", RID_INT, 0 },
  457. { "long", RID_LONG, 0 },
  458. { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
  459. { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
  460. { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
  461. { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
  462. { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
  463. { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
  464. { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
  465. { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
  466. { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
  467. { "register", RID_REGISTER, 0 },
  468. { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
  469. { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
  470. { "return", RID_RETURN, 0 },
  471. { "short", RID_SHORT, 0 },
  472. { "signed", RID_SIGNED, 0 },
  473. { "sizeof", RID_SIZEOF, 0 },
  474. { "static", RID_STATIC, 0 },
  475. { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
  476. { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
  477. { "struct", RID_STRUCT, 0 },
  478. { "switch", RID_SWITCH, 0 },
  479. { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
  480. { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
  481. { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
  482. { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
  483. { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
  484. { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
  485. { "typedef", RID_TYPEDEF, 0 },
  486. { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
  487. { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
  488. { "typeof", RID_TYPEOF, D_ASM | D_EXT },
  489. { "union", RID_UNION, 0 },
  490. { "unsigned", RID_UNSIGNED, 0 },
  491. { "using", RID_USING, D_CXXONLY | D_CXXWARN },
  492. { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
  493. { "void", RID_VOID, 0 },
  494. { "volatile", RID_VOLATILE, 0 },
  495. { "wchar_t", RID_WCHAR, D_CXXONLY },
  496. { "while", RID_WHILE, 0 },
  497. /* These Objective-C keywords are recognized only immediately after
  498. an '@'. */
  499. { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
  500. { "defs", RID_AT_DEFS, D_OBJC },
  501. { "encode", RID_AT_ENCODE, D_OBJC },
  502. { "end", RID_AT_END, D_OBJC },
  503. { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
  504. { "interface", RID_AT_INTERFACE, D_OBJC },
  505. { "protocol", RID_AT_PROTOCOL, D_OBJC },
  506. { "selector", RID_AT_SELECTOR, D_OBJC },
  507. { "finally", RID_AT_FINALLY, D_OBJC },
  508. { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
  509. { "optional", RID_AT_OPTIONAL, D_OBJC },
  510. { "required", RID_AT_REQUIRED, D_OBJC },
  511. { "property", RID_AT_PROPERTY, D_OBJC },
  512. { "package", RID_AT_PACKAGE, D_OBJC },
  513. { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
  514. { "dynamic", RID_AT_DYNAMIC, D_OBJC },
  515. /* These are recognized only in protocol-qualifier context
  516. (see above) */
  517. { "bycopy", RID_BYCOPY, D_OBJC },
  518. { "byref", RID_BYREF, D_OBJC },
  519. { "in", RID_IN, D_OBJC },
  520. { "inout", RID_INOUT, D_OBJC },
  521. { "oneway", RID_ONEWAY, D_OBJC },
  522. { "out", RID_OUT, D_OBJC },
  523. /* These are recognized inside a property attribute list */
  524. { "assign", RID_ASSIGN, D_OBJC },
  525. { "copy", RID_COPY, D_OBJC },
  526. { "getter", RID_GETTER, D_OBJC },
  527. { "nonatomic", RID_NONATOMIC, D_OBJC },
  528. { "readonly", RID_READONLY, D_OBJC },
  529. { "readwrite", RID_READWRITE, D_OBJC },
  530. { "retain", RID_RETAIN, D_OBJC },
  531. { "setter", RID_SETTER, D_OBJC },
  532. };
  533. const unsigned int num_c_common_reswords =
  534. sizeof c_common_reswords / sizeof (struct c_common_resword);
  535. /* Table of machine-independent attributes common to all C-like languages.
  536. All attributes referencing arguments should be additionally processed
  537. in chkp_copy_function_type_adding_bounds for correct instrumentation
  538. by Pointer Bounds Checker.
  539. Current list of processed common attributes: nonnull. */
  540. const struct attribute_spec c_common_attribute_table[] =
  541. {
  542. /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
  543. affects_type_identity } */
  544. { "packed", 0, 0, false, false, false,
  545. handle_packed_attribute , false},
  546. { "nocommon", 0, 0, true, false, false,
  547. handle_nocommon_attribute, false},
  548. { "common", 0, 0, true, false, false,
  549. handle_common_attribute, false },
  550. /* FIXME: logically, noreturn attributes should be listed as
  551. "false, true, true" and apply to function types. But implementing this
  552. would require all the places in the compiler that use TREE_THIS_VOLATILE
  553. on a decl to identify non-returning functions to be located and fixed
  554. to check the function type instead. */
  555. { "noreturn", 0, 0, true, false, false,
  556. handle_noreturn_attribute, false },
  557. { "volatile", 0, 0, true, false, false,
  558. handle_noreturn_attribute, false },
  559. { "stack_protect", 0, 0, true, false, false,
  560. handle_stack_protect_attribute, false },
  561. { "noinline", 0, 0, true, false, false,
  562. handle_noinline_attribute, false },
  563. { "noclone", 0, 0, true, false, false,
  564. handle_noclone_attribute, false },
  565. { "no_icf", 0, 0, true, false, false,
  566. handle_noicf_attribute, false },
  567. { "leaf", 0, 0, true, false, false,
  568. handle_leaf_attribute, false },
  569. { "always_inline", 0, 0, true, false, false,
  570. handle_always_inline_attribute, false },
  571. { "gnu_inline", 0, 0, true, false, false,
  572. handle_gnu_inline_attribute, false },
  573. { "artificial", 0, 0, true, false, false,
  574. handle_artificial_attribute, false },
  575. { "flatten", 0, 0, true, false, false,
  576. handle_flatten_attribute, false },
  577. { "used", 0, 0, true, false, false,
  578. handle_used_attribute, false },
  579. { "unused", 0, 0, false, false, false,
  580. handle_unused_attribute, false },
  581. { "externally_visible", 0, 0, true, false, false,
  582. handle_externally_visible_attribute, false },
  583. { "no_reorder", 0, 0, true, false, false,
  584. handle_no_reorder_attribute, false },
  585. /* The same comments as for noreturn attributes apply to const ones. */
  586. { "const", 0, 0, true, false, false,
  587. handle_const_attribute, false },
  588. { "transparent_union", 0, 0, false, false, false,
  589. handle_transparent_union_attribute, false },
  590. { "constructor", 0, 1, true, false, false,
  591. handle_constructor_attribute, false },
  592. { "destructor", 0, 1, true, false, false,
  593. handle_destructor_attribute, false },
  594. { "mode", 1, 1, false, true, false,
  595. handle_mode_attribute, false },
  596. { "section", 1, 1, true, false, false,
  597. handle_section_attribute, false },
  598. { "aligned", 0, 1, false, false, false,
  599. handle_aligned_attribute, false },
  600. { "weak", 0, 0, true, false, false,
  601. handle_weak_attribute, false },
  602. { "ifunc", 1, 1, true, false, false,
  603. handle_ifunc_attribute, false },
  604. { "alias", 1, 1, true, false, false,
  605. handle_alias_attribute, false },
  606. { "weakref", 0, 1, true, false, false,
  607. handle_weakref_attribute, false },
  608. { "no_instrument_function", 0, 0, true, false, false,
  609. handle_no_instrument_function_attribute,
  610. false },
  611. { "malloc", 0, 0, true, false, false,
  612. handle_malloc_attribute, false },
  613. { "returns_twice", 0, 0, true, false, false,
  614. handle_returns_twice_attribute, false },
  615. { "no_stack_limit", 0, 0, true, false, false,
  616. handle_no_limit_stack_attribute, false },
  617. { "pure", 0, 0, true, false, false,
  618. handle_pure_attribute, false },
  619. { "transaction_callable", 0, 0, false, true, false,
  620. handle_tm_attribute, false },
  621. { "transaction_unsafe", 0, 0, false, true, false,
  622. handle_tm_attribute, false },
  623. { "transaction_safe", 0, 0, false, true, false,
  624. handle_tm_attribute, false },
  625. { "transaction_may_cancel_outer", 0, 0, false, true, false,
  626. handle_tm_attribute, false },
  627. /* ??? These two attributes didn't make the transition from the
  628. Intel language document to the multi-vendor language document. */
  629. { "transaction_pure", 0, 0, false, true, false,
  630. handle_tm_attribute, false },
  631. { "transaction_wrap", 1, 1, true, false, false,
  632. handle_tm_wrap_attribute, false },
  633. /* For internal use (marking of builtins) only. The name contains space
  634. to prevent its usage in source code. */
  635. { "no vops", 0, 0, true, false, false,
  636. handle_novops_attribute, false },
  637. { "deprecated", 0, 1, false, false, false,
  638. handle_deprecated_attribute, false },
  639. { "vector_size", 1, 1, false, true, false,
  640. handle_vector_size_attribute, false },
  641. { "visibility", 1, 1, false, false, false,
  642. handle_visibility_attribute, false },
  643. { "tls_model", 1, 1, true, false, false,
  644. handle_tls_model_attribute, false },
  645. { "nonnull", 0, -1, false, true, true,
  646. handle_nonnull_attribute, false },
  647. { "nothrow", 0, 0, true, false, false,
  648. handle_nothrow_attribute, false },
  649. { "may_alias", 0, 0, false, true, false, NULL, false },
  650. { "cleanup", 1, 1, true, false, false,
  651. handle_cleanup_attribute, false },
  652. { "warn_unused_result", 0, 0, false, true, true,
  653. handle_warn_unused_result_attribute, false },
  654. { "sentinel", 0, 1, false, true, true,
  655. handle_sentinel_attribute, false },
  656. /* For internal use (marking of builtins) only. The name contains space
  657. to prevent its usage in source code. */
  658. { "type generic", 0, 0, false, true, true,
  659. handle_type_generic_attribute, false },
  660. { "alloc_size", 1, 2, false, true, true,
  661. handle_alloc_size_attribute, false },
  662. { "cold", 0, 0, true, false, false,
  663. handle_cold_attribute, false },
  664. { "hot", 0, 0, true, false, false,
  665. handle_hot_attribute, false },
  666. { "no_address_safety_analysis",
  667. 0, 0, true, false, false,
  668. handle_no_address_safety_analysis_attribute,
  669. false },
  670. { "no_sanitize_address", 0, 0, true, false, false,
  671. handle_no_sanitize_address_attribute,
  672. false },
  673. { "no_sanitize_thread", 0, 0, true, false, false,
  674. handle_no_sanitize_address_attribute,
  675. false },
  676. { "no_sanitize_undefined", 0, 0, true, false, false,
  677. handle_no_sanitize_undefined_attribute,
  678. false },
  679. { "warning", 1, 1, true, false, false,
  680. handle_error_attribute, false },
  681. { "error", 1, 1, true, false, false,
  682. handle_error_attribute, false },
  683. { "target", 1, -1, true, false, false,
  684. handle_target_attribute, false },
  685. { "optimize", 1, -1, true, false, false,
  686. handle_optimize_attribute, false },
  687. /* For internal use only. The leading '*' both prevents its usage in
  688. source code and signals that it may be overridden by machine tables. */
  689. { "*tm regparm", 0, 0, false, true, true,
  690. ignore_attribute, false },
  691. { "no_split_stack", 0, 0, true, false, false,
  692. handle_no_split_stack_attribute, false },
  693. /* For internal use (marking of builtins and runtime functions) only.
  694. The name contains space to prevent its usage in source code. */
  695. { "fn spec", 1, 1, false, true, true,
  696. handle_fnspec_attribute, false },
  697. { "warn_unused", 0, 0, false, false, false,
  698. handle_warn_unused_attribute, false },
  699. { "returns_nonnull", 0, 0, false, true, true,
  700. handle_returns_nonnull_attribute, false },
  701. { "omp declare simd", 0, -1, true, false, false,
  702. handle_omp_declare_simd_attribute, false },
  703. { "cilk simd function", 0, -1, true, false, false,
  704. handle_omp_declare_simd_attribute, false },
  705. { "omp declare target", 0, 0, true, false, false,
  706. handle_omp_declare_target_attribute, false },
  707. { "alloc_align", 1, 1, false, true, true,
  708. handle_alloc_align_attribute, false },
  709. { "assume_aligned", 1, 2, false, true, true,
  710. handle_assume_aligned_attribute, false },
  711. { "designated_init", 0, 0, false, true, false,
  712. handle_designated_init_attribute, false },
  713. { "bnd_variable_size", 0, 0, true, false, false,
  714. handle_bnd_variable_size_attribute, false },
  715. { "bnd_legacy", 0, 0, true, false, false,
  716. handle_bnd_legacy, false },
  717. { "bnd_instrument", 0, 0, true, false, false,
  718. handle_bnd_instrument, false },
  719. { NULL, 0, 0, false, false, false, NULL, false }
  720. };
  721. /* Give the specifications for the format attributes, used by C and all
  722. descendants.
  723. All attributes referencing arguments should be additionally processed
  724. in chkp_copy_function_type_adding_bounds for correct instrumentation
  725. by Pointer Bounds Checker.
  726. Current list of processed format attributes: format, format_arg. */
  727. const struct attribute_spec c_common_format_attribute_table[] =
  728. {
  729. /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
  730. affects_type_identity } */
  731. { "format", 3, 3, false, true, true,
  732. handle_format_attribute, false },
  733. { "format_arg", 1, 1, false, true, true,
  734. handle_format_arg_attribute, false },
  735. { NULL, 0, 0, false, false, false, NULL, false }
  736. };
  737. /* Return identifier for address space AS. */
  738. const char *
  739. c_addr_space_name (addr_space_t as)
  740. {
  741. int rid = RID_FIRST_ADDR_SPACE + as;
  742. gcc_assert (ridpointers [rid]);
  743. return IDENTIFIER_POINTER (ridpointers [rid]);
  744. }
  745. /* Push current bindings for the function name VAR_DECLS. */
  746. void
  747. start_fname_decls (void)
  748. {
  749. unsigned ix;
  750. tree saved = NULL_TREE;
  751. for (ix = 0; fname_vars[ix].decl; ix++)
  752. {
  753. tree decl = *fname_vars[ix].decl;
  754. if (decl)
  755. {
  756. saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
  757. saved);
  758. *fname_vars[ix].decl = NULL_TREE;
  759. }
  760. }
  761. if (saved || saved_function_name_decls)
  762. /* Normally they'll have been NULL, so only push if we've got a
  763. stack, or they are non-NULL. */
  764. saved_function_name_decls = tree_cons (saved, NULL_TREE,
  765. saved_function_name_decls);
  766. }
  767. /* Finish up the current bindings, adding them into the current function's
  768. statement tree. This must be done _before_ finish_stmt_tree is called.
  769. If there is no current function, we must be at file scope and no statements
  770. are involved. Pop the previous bindings. */
  771. void
  772. finish_fname_decls (void)
  773. {
  774. unsigned ix;
  775. tree stmts = NULL_TREE;
  776. tree stack = saved_function_name_decls;
  777. for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
  778. append_to_statement_list (TREE_VALUE (stack), &stmts);
  779. if (stmts)
  780. {
  781. tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
  782. if (TREE_CODE (*bodyp) == BIND_EXPR)
  783. bodyp = &BIND_EXPR_BODY (*bodyp);
  784. append_to_statement_list_force (*bodyp, &stmts);
  785. *bodyp = stmts;
  786. }
  787. for (ix = 0; fname_vars[ix].decl; ix++)
  788. *fname_vars[ix].decl = NULL_TREE;
  789. if (stack)
  790. {
  791. /* We had saved values, restore them. */
  792. tree saved;
  793. for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
  794. {
  795. tree decl = TREE_PURPOSE (saved);
  796. unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
  797. *fname_vars[ix].decl = decl;
  798. }
  799. stack = TREE_CHAIN (stack);
  800. }
  801. saved_function_name_decls = stack;
  802. }
  803. /* Return the text name of the current function, suitably prettified
  804. by PRETTY_P. Return string must be freed by caller. */
  805. const char *
  806. fname_as_string (int pretty_p)
  807. {
  808. const char *name = "top level";
  809. char *namep;
  810. int vrb = 2, len;
  811. cpp_string cstr = { 0, 0 }, strname;
  812. if (!pretty_p)
  813. {
  814. name = "";
  815. vrb = 0;
  816. }
  817. if (current_function_decl)
  818. name = lang_hooks.decl_printable_name (current_function_decl, vrb);
  819. len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
  820. namep = XNEWVEC (char, len);
  821. snprintf (namep, len, "\"%s\"", name);
  822. strname.text = (unsigned char *) namep;
  823. strname.len = len - 1;
  824. if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
  825. {
  826. XDELETEVEC (namep);
  827. return (const char *) cstr.text;
  828. }
  829. return namep;
  830. }
  831. /* Return the VAR_DECL for a const char array naming the current
  832. function. If the VAR_DECL has not yet been created, create it
  833. now. RID indicates how it should be formatted and IDENTIFIER_NODE
  834. ID is its name (unfortunately C and C++ hold the RID values of
  835. keywords in different places, so we can't derive RID from ID in
  836. this language independent code. LOC is the location of the
  837. function. */
  838. tree
  839. fname_decl (location_t loc, unsigned int rid, tree id)
  840. {
  841. unsigned ix;
  842. tree decl = NULL_TREE;
  843. for (ix = 0; fname_vars[ix].decl; ix++)
  844. if (fname_vars[ix].rid == rid)
  845. break;
  846. decl = *fname_vars[ix].decl;
  847. if (!decl)
  848. {
  849. /* If a tree is built here, it would normally have the lineno of
  850. the current statement. Later this tree will be moved to the
  851. beginning of the function and this line number will be wrong.
  852. To avoid this problem set the lineno to 0 here; that prevents
  853. it from appearing in the RTL. */
  854. tree stmts;
  855. location_t saved_location = input_location;
  856. input_location = UNKNOWN_LOCATION;
  857. stmts = push_stmt_list ();
  858. decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
  859. stmts = pop_stmt_list (stmts);
  860. if (!IS_EMPTY_STMT (stmts))
  861. saved_function_name_decls
  862. = tree_cons (decl, stmts, saved_function_name_decls);
  863. *fname_vars[ix].decl = decl;
  864. input_location = saved_location;
  865. }
  866. if (!ix && !current_function_decl)
  867. pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
  868. return decl;
  869. }
  870. /* Given a STRING_CST, give it a suitable array-of-chars data type. */
  871. tree
  872. fix_string_type (tree value)
  873. {
  874. int length = TREE_STRING_LENGTH (value);
  875. int nchars;
  876. tree e_type, i_type, a_type;
  877. /* Compute the number of elements, for the array type. */
  878. if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
  879. {
  880. nchars = length;
  881. e_type = char_type_node;
  882. }
  883. else if (TREE_TYPE (value) == char16_array_type_node)
  884. {
  885. nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
  886. e_type = char16_type_node;
  887. }
  888. else if (TREE_TYPE (value) == char32_array_type_node)
  889. {
  890. nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
  891. e_type = char32_type_node;
  892. }
  893. else
  894. {
  895. nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
  896. e_type = wchar_type_node;
  897. }
  898. /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
  899. limit in C++98 Annex B is very large (65536) and is not normative,
  900. so we do not diagnose it (warn_overlength_strings is forced off
  901. in c_common_post_options). */
  902. if (warn_overlength_strings)
  903. {
  904. const int nchars_max = flag_isoc99 ? 4095 : 509;
  905. const int relevant_std = flag_isoc99 ? 99 : 90;
  906. if (nchars - 1 > nchars_max)
  907. /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
  908. separate the %d from the 'C'. 'ISO' should not be
  909. translated, but it may be moved after 'C%d' in languages
  910. where modifiers follow nouns. */
  911. pedwarn (input_location, OPT_Woverlength_strings,
  912. "string length %qd is greater than the length %qd "
  913. "ISO C%d compilers are required to support",
  914. nchars - 1, nchars_max, relevant_std);
  915. }
  916. /* Create the array type for the string constant. The ISO C++
  917. standard says that a string literal has type `const char[N]' or
  918. `const wchar_t[N]'. We use the same logic when invoked as a C
  919. front-end with -Wwrite-strings.
  920. ??? We should change the type of an expression depending on the
  921. state of a warning flag. We should just be warning -- see how
  922. this is handled in the C++ front-end for the deprecated implicit
  923. conversion from string literals to `char*' or `wchar_t*'.
  924. The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
  925. array type being the unqualified version of that type.
  926. Therefore, if we are constructing an array of const char, we must
  927. construct the matching unqualified array type first. The C front
  928. end does not require this, but it does no harm, so we do it
  929. unconditionally. */
  930. i_type = build_index_type (size_int (nchars - 1));
  931. a_type = build_array_type (e_type, i_type);
  932. if (c_dialect_cxx() || warn_write_strings)
  933. a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
  934. TREE_TYPE (value) = a_type;
  935. TREE_CONSTANT (value) = 1;
  936. TREE_READONLY (value) = 1;
  937. TREE_STATIC (value) = 1;
  938. return value;
  939. }
  940. /* If DISABLE is true, stop issuing warnings. This is used when
  941. parsing code that we know will not be executed. This function may
  942. be called multiple times, and works as a stack. */
  943. static void
  944. c_disable_warnings (bool disable)
  945. {
  946. if (disable)
  947. {
  948. ++c_inhibit_evaluation_warnings;
  949. fold_defer_overflow_warnings ();
  950. }
  951. }
  952. /* If ENABLE is true, reenable issuing warnings. */
  953. static void
  954. c_enable_warnings (bool enable)
  955. {
  956. if (enable)
  957. {
  958. --c_inhibit_evaluation_warnings;
  959. fold_undefer_and_ignore_overflow_warnings ();
  960. }
  961. }
  962. /* Fully fold EXPR, an expression that was not folded (beyond integer
  963. constant expressions and null pointer constants) when being built
  964. up. If IN_INIT, this is in a static initializer and certain
  965. changes are made to the folding done. Clear *MAYBE_CONST if
  966. MAYBE_CONST is not NULL and EXPR is definitely not a constant
  967. expression because it contains an evaluated operator (in C99) or an
  968. operator outside of sizeof returning an integer constant (in C90)
  969. not permitted in constant expressions, or because it contains an
  970. evaluated arithmetic overflow. (*MAYBE_CONST should typically be
  971. set to true by callers before calling this function.) Return the
  972. folded expression. Function arguments have already been folded
  973. before calling this function, as have the contents of SAVE_EXPR,
  974. TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
  975. C_MAYBE_CONST_EXPR. */
  976. tree
  977. c_fully_fold (tree expr, bool in_init, bool *maybe_const)
  978. {
  979. tree ret;
  980. tree eptype = NULL_TREE;
  981. bool dummy = true;
  982. bool maybe_const_itself = true;
  983. location_t loc = EXPR_LOCATION (expr);
  984. /* This function is not relevant to C++ because C++ folds while
  985. parsing, and may need changes to be correct for C++ when C++
  986. stops folding while parsing. */
  987. if (c_dialect_cxx ())
  988. gcc_unreachable ();
  989. if (!maybe_const)
  990. maybe_const = &dummy;
  991. if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
  992. {
  993. eptype = TREE_TYPE (expr);
  994. expr = TREE_OPERAND (expr, 0);
  995. }
  996. ret = c_fully_fold_internal (expr, in_init, maybe_const,
  997. &maybe_const_itself);
  998. if (eptype)
  999. ret = fold_convert_loc (loc, eptype, ret);
  1000. *maybe_const &= maybe_const_itself;
  1001. return ret;
  1002. }
  1003. /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
  1004. c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
  1005. not permitted, while *MAYBE_CONST_ITSELF is cleared because of
  1006. arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
  1007. both evaluated and unevaluated subexpressions while
  1008. *MAYBE_CONST_ITSELF is carried from only evaluated
  1009. subexpressions). */
  1010. static tree
  1011. c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
  1012. bool *maybe_const_itself)
  1013. {
  1014. tree ret = expr;
  1015. enum tree_code code = TREE_CODE (expr);
  1016. enum tree_code_class kind = TREE_CODE_CLASS (code);
  1017. location_t loc = EXPR_LOCATION (expr);
  1018. tree op0, op1, op2, op3;
  1019. tree orig_op0, orig_op1, orig_op2;
  1020. bool op0_const = true, op1_const = true, op2_const = true;
  1021. bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
  1022. bool nowarning = TREE_NO_WARNING (expr);
  1023. bool unused_p;
  1024. /* This function is not relevant to C++ because C++ folds while
  1025. parsing, and may need changes to be correct for C++ when C++
  1026. stops folding while parsing. */
  1027. if (c_dialect_cxx ())
  1028. gcc_unreachable ();
  1029. /* Constants, declarations, statements, errors, SAVE_EXPRs and
  1030. anything else not counted as an expression cannot usefully be
  1031. folded further at this point. */
  1032. if (!IS_EXPR_CODE_CLASS (kind)
  1033. || kind == tcc_statement
  1034. || code == SAVE_EXPR)
  1035. return expr;
  1036. /* Operands of variable-length expressions (function calls) have
  1037. already been folded, as have __builtin_* function calls, and such
  1038. expressions cannot occur in constant expressions. */
  1039. if (kind == tcc_vl_exp)
  1040. {
  1041. *maybe_const_operands = false;
  1042. ret = fold (expr);
  1043. goto out;
  1044. }
  1045. if (code == C_MAYBE_CONST_EXPR)
  1046. {
  1047. tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
  1048. tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
  1049. if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
  1050. *maybe_const_operands = false;
  1051. if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
  1052. *maybe_const_itself = false;
  1053. if (pre && !in_init)
  1054. ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
  1055. else
  1056. ret = inner;
  1057. goto out;
  1058. }
  1059. /* Assignment, increment, decrement, function call and comma
  1060. operators, and statement expressions, cannot occur in constant
  1061. expressions if evaluated / outside of sizeof. (Function calls
  1062. were handled above, though VA_ARG_EXPR is treated like a function
  1063. call here, and statement expressions are handled through
  1064. C_MAYBE_CONST_EXPR to avoid folding inside them.) */
  1065. switch (code)
  1066. {
  1067. case MODIFY_EXPR:
  1068. case PREDECREMENT_EXPR:
  1069. case PREINCREMENT_EXPR:
  1070. case POSTDECREMENT_EXPR:
  1071. case POSTINCREMENT_EXPR:
  1072. case COMPOUND_EXPR:
  1073. *maybe_const_operands = false;
  1074. break;
  1075. case VA_ARG_EXPR:
  1076. case TARGET_EXPR:
  1077. case BIND_EXPR:
  1078. case OBJ_TYPE_REF:
  1079. *maybe_const_operands = false;
  1080. ret = fold (expr);
  1081. goto out;
  1082. default:
  1083. break;
  1084. }
  1085. /* Fold individual tree codes as appropriate. */
  1086. switch (code)
  1087. {
  1088. case COMPOUND_LITERAL_EXPR:
  1089. /* Any non-constancy will have been marked in a containing
  1090. C_MAYBE_CONST_EXPR; there is no more folding to do here. */
  1091. goto out;
  1092. case COMPONENT_REF:
  1093. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1094. op1 = TREE_OPERAND (expr, 1);
  1095. op2 = TREE_OPERAND (expr, 2);
  1096. op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
  1097. maybe_const_itself);
  1098. STRIP_TYPE_NOPS (op0);
  1099. if (op0 != orig_op0)
  1100. ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
  1101. if (ret != expr)
  1102. {
  1103. TREE_READONLY (ret) = TREE_READONLY (expr);
  1104. TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
  1105. }
  1106. goto out;
  1107. case ARRAY_REF:
  1108. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1109. orig_op1 = op1 = TREE_OPERAND (expr, 1);
  1110. op2 = TREE_OPERAND (expr, 2);
  1111. op3 = TREE_OPERAND (expr, 3);
  1112. op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
  1113. maybe_const_itself);
  1114. STRIP_TYPE_NOPS (op0);
  1115. op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
  1116. maybe_const_itself);
  1117. STRIP_TYPE_NOPS (op1);
  1118. op1 = decl_constant_value_for_optimization (op1);
  1119. if (op0 != orig_op0 || op1 != orig_op1)
  1120. ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
  1121. if (ret != expr)
  1122. {
  1123. TREE_READONLY (ret) = TREE_READONLY (expr);
  1124. TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
  1125. TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
  1126. }
  1127. ret = fold (ret);
  1128. goto out;
  1129. case COMPOUND_EXPR:
  1130. case MODIFY_EXPR:
  1131. case PREDECREMENT_EXPR:
  1132. case PREINCREMENT_EXPR:
  1133. case POSTDECREMENT_EXPR:
  1134. case POSTINCREMENT_EXPR:
  1135. case PLUS_EXPR:
  1136. case MINUS_EXPR:
  1137. case MULT_EXPR:
  1138. case POINTER_PLUS_EXPR:
  1139. case TRUNC_DIV_EXPR:
  1140. case CEIL_DIV_EXPR:
  1141. case FLOOR_DIV_EXPR:
  1142. case TRUNC_MOD_EXPR:
  1143. case RDIV_EXPR:
  1144. case EXACT_DIV_EXPR:
  1145. case LSHIFT_EXPR:
  1146. case RSHIFT_EXPR:
  1147. case BIT_IOR_EXPR:
  1148. case BIT_XOR_EXPR:
  1149. case BIT_AND_EXPR:
  1150. case LT_EXPR:
  1151. case LE_EXPR:
  1152. case GT_EXPR:
  1153. case GE_EXPR:
  1154. case EQ_EXPR:
  1155. case NE_EXPR:
  1156. case COMPLEX_EXPR:
  1157. case TRUTH_AND_EXPR:
  1158. case TRUTH_OR_EXPR:
  1159. case TRUTH_XOR_EXPR:
  1160. case UNORDERED_EXPR:
  1161. case ORDERED_EXPR:
  1162. case UNLT_EXPR:
  1163. case UNLE_EXPR:
  1164. case UNGT_EXPR:
  1165. case UNGE_EXPR:
  1166. case UNEQ_EXPR:
  1167. /* Binary operations evaluating both arguments (increment and
  1168. decrement are binary internally in GCC). */
  1169. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1170. orig_op1 = op1 = TREE_OPERAND (expr, 1);
  1171. op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
  1172. maybe_const_itself);
  1173. STRIP_TYPE_NOPS (op0);
  1174. if (code != MODIFY_EXPR
  1175. && code != PREDECREMENT_EXPR
  1176. && code != PREINCREMENT_EXPR
  1177. && code != POSTDECREMENT_EXPR
  1178. && code != POSTINCREMENT_EXPR)
  1179. op0 = decl_constant_value_for_optimization (op0);
  1180. /* The RHS of a MODIFY_EXPR was fully folded when building that
  1181. expression for the sake of conversion warnings. */
  1182. if (code != MODIFY_EXPR)
  1183. op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
  1184. maybe_const_itself);
  1185. STRIP_TYPE_NOPS (op1);
  1186. op1 = decl_constant_value_for_optimization (op1);
  1187. if (op0 != orig_op0 || op1 != orig_op1 || in_init)
  1188. ret = in_init
  1189. ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
  1190. : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
  1191. else
  1192. ret = fold (expr);
  1193. if (TREE_OVERFLOW_P (ret)
  1194. && !TREE_OVERFLOW_P (op0)
  1195. && !TREE_OVERFLOW_P (op1))
  1196. overflow_warning (EXPR_LOCATION (expr), ret);
  1197. if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
  1198. && TREE_CODE (orig_op1) != INTEGER_CST
  1199. && TREE_CODE (op1) == INTEGER_CST
  1200. && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
  1201. || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
  1202. && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
  1203. && c_inhibit_evaluation_warnings == 0)
  1204. {
  1205. if (tree_int_cst_sgn (op1) < 0)
  1206. warning_at (loc, 0, (code == LSHIFT_EXPR
  1207. ? G_("left shift count is negative")
  1208. : G_("right shift count is negative")));
  1209. else if (compare_tree_int (op1,
  1210. TYPE_PRECISION (TREE_TYPE (orig_op0)))
  1211. >= 0)
  1212. warning_at (loc, 0, (code == LSHIFT_EXPR
  1213. ? G_("left shift count >= width of type")
  1214. : G_("right shift count >= width of type")));
  1215. }
  1216. if ((code == TRUNC_DIV_EXPR
  1217. || code == CEIL_DIV_EXPR
  1218. || code == FLOOR_DIV_EXPR
  1219. || code == EXACT_DIV_EXPR
  1220. || code == TRUNC_MOD_EXPR)
  1221. && TREE_CODE (orig_op1) != INTEGER_CST
  1222. && TREE_CODE (op1) == INTEGER_CST
  1223. && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
  1224. || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
  1225. && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
  1226. warn_for_div_by_zero (loc, op1);
  1227. goto out;
  1228. case INDIRECT_REF:
  1229. case FIX_TRUNC_EXPR:
  1230. case FLOAT_EXPR:
  1231. CASE_CONVERT:
  1232. case ADDR_SPACE_CONVERT_EXPR:
  1233. case VIEW_CONVERT_EXPR:
  1234. case NON_LVALUE_EXPR:
  1235. case NEGATE_EXPR:
  1236. case BIT_NOT_EXPR:
  1237. case TRUTH_NOT_EXPR:
  1238. case ADDR_EXPR:
  1239. case CONJ_EXPR:
  1240. case REALPART_EXPR:
  1241. case IMAGPART_EXPR:
  1242. /* Unary operations. */
  1243. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1244. op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
  1245. maybe_const_itself);
  1246. STRIP_TYPE_NOPS (op0);
  1247. if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
  1248. op0 = decl_constant_value_for_optimization (op0);
  1249. /* ??? Cope with user tricks that amount to offsetof. The middle-end is
  1250. not prepared to deal with them if they occur in initializers. */
  1251. if (op0 != orig_op0
  1252. && code == ADDR_EXPR
  1253. && (op1 = get_base_address (op0)) != NULL_TREE
  1254. && TREE_CODE (op1) == INDIRECT_REF
  1255. && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
  1256. ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
  1257. else if (op0 != orig_op0 || in_init)
  1258. ret = in_init
  1259. ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
  1260. : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
  1261. else
  1262. ret = fold (expr);
  1263. if (code == INDIRECT_REF
  1264. && ret != expr
  1265. && TREE_CODE (ret) == INDIRECT_REF)
  1266. {
  1267. TREE_READONLY (ret) = TREE_READONLY (expr);
  1268. TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
  1269. TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
  1270. }
  1271. switch (code)
  1272. {
  1273. case FIX_TRUNC_EXPR:
  1274. case FLOAT_EXPR:
  1275. CASE_CONVERT:
  1276. /* Don't warn about explicit conversions. We will already
  1277. have warned about suspect implicit conversions. */
  1278. break;
  1279. default:
  1280. if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
  1281. overflow_warning (EXPR_LOCATION (expr), ret);
  1282. break;
  1283. }
  1284. goto out;
  1285. case TRUTH_ANDIF_EXPR:
  1286. case TRUTH_ORIF_EXPR:
  1287. /* Binary operations not necessarily evaluating both
  1288. arguments. */
  1289. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1290. orig_op1 = op1 = TREE_OPERAND (expr, 1);
  1291. op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
  1292. STRIP_TYPE_NOPS (op0);
  1293. unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
  1294. ? truthvalue_false_node
  1295. : truthvalue_true_node));
  1296. c_disable_warnings (unused_p);
  1297. op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
  1298. STRIP_TYPE_NOPS (op1);
  1299. c_enable_warnings (unused_p);
  1300. if (op0 != orig_op0 || op1 != orig_op1 || in_init)
  1301. ret = in_init
  1302. ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
  1303. : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
  1304. else
  1305. ret = fold (expr);
  1306. *maybe_const_operands &= op0_const;
  1307. *maybe_const_itself &= op0_const_self;
  1308. if (!(flag_isoc99
  1309. && op0_const
  1310. && op0_const_self
  1311. && (code == TRUTH_ANDIF_EXPR
  1312. ? op0 == truthvalue_false_node
  1313. : op0 == truthvalue_true_node)))
  1314. *maybe_const_operands &= op1_const;
  1315. if (!(op0_const
  1316. && op0_const_self
  1317. && (code == TRUTH_ANDIF_EXPR
  1318. ? op0 == truthvalue_false_node
  1319. : op0 == truthvalue_true_node)))
  1320. *maybe_const_itself &= op1_const_self;
  1321. goto out;
  1322. case COND_EXPR:
  1323. orig_op0 = op0 = TREE_OPERAND (expr, 0);
  1324. orig_op1 = op1 = TREE_OPERAND (expr, 1);
  1325. orig_op2 = op2 = TREE_OPERAND (expr, 2);
  1326. op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
  1327. STRIP_TYPE_NOPS (op0);
  1328. c_disable_warnings (op0 == truthvalue_false_node);
  1329. op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
  1330. STRIP_TYPE_NOPS (op1);
  1331. c_enable_warnings (op0 == truthvalue_false_node);
  1332. c_disable_warnings (op0 == truthvalue_true_node);
  1333. op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
  1334. STRIP_TYPE_NOPS (op2);
  1335. c_enable_warnings (op0 == truthvalue_true_node);
  1336. if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
  1337. ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
  1338. else
  1339. ret = fold (expr);
  1340. *maybe_const_operands &= op0_const;
  1341. *maybe_const_itself &= op0_const_self;
  1342. if (!(flag_isoc99
  1343. && op0_const
  1344. && op0_const_self
  1345. && op0 == truthvalue_false_node))
  1346. *maybe_const_operands &= op1_const;
  1347. if (!(op0_const
  1348. && op0_const_self
  1349. && op0 == truthvalue_false_node))
  1350. *maybe_const_itself &= op1_const_self;
  1351. if (!(flag_isoc99
  1352. && op0_const
  1353. && op0_const_self
  1354. && op0 == truthvalue_true_node))
  1355. *maybe_const_operands &= op2_const;
  1356. if (!(op0_const
  1357. && op0_const_self
  1358. && op0 == truthvalue_true_node))
  1359. *maybe_const_itself &= op2_const_self;
  1360. goto out;
  1361. case EXCESS_PRECISION_EXPR:
  1362. /* Each case where an operand with excess precision may be
  1363. encountered must remove the EXCESS_PRECISION_EXPR around
  1364. inner operands and possibly put one around the whole
  1365. expression or possibly convert to the semantic type (which
  1366. c_fully_fold does); we cannot tell at this stage which is
  1367. appropriate in any particular case. */
  1368. gcc_unreachable ();
  1369. default:
  1370. /* Various codes may appear through folding built-in functions
  1371. and their arguments. */
  1372. goto out;
  1373. }
  1374. out:
  1375. /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
  1376. have been done by this point, so remove them again. */
  1377. nowarning |= TREE_NO_WARNING (ret);
  1378. STRIP_TYPE_NOPS (ret);
  1379. if (nowarning && !TREE_NO_WARNING (ret))
  1380. {
  1381. if (!CAN_HAVE_LOCATION_P (ret))
  1382. ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
  1383. TREE_NO_WARNING (ret) = 1;
  1384. }
  1385. if (ret != expr)
  1386. protected_set_expr_location (ret, loc);
  1387. return ret;
  1388. }
  1389. /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
  1390. return EXP. Otherwise, return either EXP or its known constant
  1391. value (if it has one), but return EXP if EXP has mode BLKmode. ???
  1392. Is the BLKmode test appropriate? */
  1393. tree
  1394. decl_constant_value_for_optimization (tree exp)
  1395. {
  1396. tree ret;
  1397. /* This function is only used by C, for c_fully_fold and other
  1398. optimization, and may not be correct for C++. */
  1399. if (c_dialect_cxx ())
  1400. gcc_unreachable ();
  1401. if (!optimize
  1402. || TREE_CODE (exp) != VAR_DECL
  1403. || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
  1404. || DECL_MODE (exp) == BLKmode)
  1405. return exp;
  1406. ret = decl_constant_value (exp);
  1407. /* Avoid unwanted tree sharing between the initializer and current
  1408. function's body where the tree can be modified e.g. by the
  1409. gimplifier. */
  1410. if (ret != exp && TREE_STATIC (exp))
  1411. ret = unshare_expr (ret);
  1412. return ret;
  1413. }
  1414. /* Print a warning if a constant expression had overflow in folding.
  1415. Invoke this function on every expression that the language
  1416. requires to be a constant expression.
  1417. Note the ANSI C standard says it is erroneous for a
  1418. constant expression to overflow. */
  1419. void
  1420. constant_expression_warning (tree value)
  1421. {
  1422. if (warn_overflow && pedantic
  1423. && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
  1424. || TREE_CODE (value) == FIXED_CST
  1425. || TREE_CODE (value) == VECTOR_CST
  1426. || TREE_CODE (value) == COMPLEX_CST)
  1427. && TREE_OVERFLOW (value))
  1428. pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
  1429. }
  1430. /* The same as above but print an unconditional error. */
  1431. void
  1432. constant_expression_error (tree value)
  1433. {
  1434. if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
  1435. || TREE_CODE (value) == FIXED_CST
  1436. || TREE_CODE (value) == VECTOR_CST
  1437. || TREE_CODE (value) == COMPLEX_CST)
  1438. && TREE_OVERFLOW (value))
  1439. error ("overflow in constant expression");
  1440. }
  1441. /* Print a warning if an expression had overflow in folding and its
  1442. operands hadn't.
  1443. Invoke this function on every expression that
  1444. (1) appears in the source code, and
  1445. (2) is a constant expression that overflowed, and
  1446. (3) is not already checked by convert_and_check;
  1447. however, do not invoke this function on operands of explicit casts
  1448. or when the expression is the result of an operator and any operand
  1449. already overflowed. */
  1450. void
  1451. overflow_warning (location_t loc, tree value)
  1452. {
  1453. if (c_inhibit_evaluation_warnings != 0)
  1454. return;
  1455. switch (TREE_CODE (value))
  1456. {
  1457. case INTEGER_CST:
  1458. warning_at (loc, OPT_Woverflow, "integer overflow in expression");
  1459. break;
  1460. case REAL_CST:
  1461. warning_at (loc, OPT_Woverflow,
  1462. "floating point overflow in expression");
  1463. break;
  1464. case FIXED_CST:
  1465. warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
  1466. break;
  1467. case VECTOR_CST:
  1468. warning_at (loc, OPT_Woverflow, "vector overflow in expression");
  1469. break;
  1470. case COMPLEX_CST:
  1471. if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
  1472. warning_at (loc, OPT_Woverflow,
  1473. "complex integer overflow in expression");
  1474. else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
  1475. warning_at (loc, OPT_Woverflow,
  1476. "complex floating point overflow in expression");
  1477. break;
  1478. default:
  1479. break;
  1480. }
  1481. }
  1482. /* Warn about uses of logical || / && operator in a context where it
  1483. is likely that the bitwise equivalent was intended by the
  1484. programmer. We have seen an expression in which CODE is a binary
  1485. operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
  1486. had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
  1487. void
  1488. warn_logical_operator (location_t location, enum tree_code code, tree type,
  1489. enum tree_code code_left, tree op_left,
  1490. enum tree_code ARG_UNUSED (code_right), tree op_right)
  1491. {
  1492. int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
  1493. int in0_p, in1_p, in_p;
  1494. tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
  1495. bool strict_overflow_p = false;
  1496. if (code != TRUTH_ANDIF_EXPR
  1497. && code != TRUTH_AND_EXPR
  1498. && code != TRUTH_ORIF_EXPR
  1499. && code != TRUTH_OR_EXPR)
  1500. return;
  1501. /* Warn if &&/|| are being used in a context where it is
  1502. likely that the bitwise equivalent was intended by the
  1503. programmer. That is, an expression such as op && MASK
  1504. where op should not be any boolean expression, nor a
  1505. constant, and mask seems to be a non-boolean integer constant. */
  1506. if (!truth_value_p (code_left)
  1507. && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
  1508. && !CONSTANT_CLASS_P (op_left)
  1509. && !TREE_NO_WARNING (op_left)
  1510. && TREE_CODE (op_right) == INTEGER_CST
  1511. && !integer_zerop (op_right)
  1512. && !integer_onep (op_right))
  1513. {
  1514. if (or_op)
  1515. warning_at (location, OPT_Wlogical_op, "logical %<or%>"
  1516. " applied to non-boolean constant");
  1517. else
  1518. warning_at (location, OPT_Wlogical_op, "logical %<and%>"
  1519. " applied to non-boolean constant");
  1520. TREE_NO_WARNING (op_left) = true;
  1521. return;
  1522. }
  1523. /* We do not warn for constants because they are typical of macro
  1524. expansions that test for features. */
  1525. if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
  1526. return;
  1527. /* This warning only makes sense with logical operands. */
  1528. if (!(truth_value_p (TREE_CODE (op_left))
  1529. || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
  1530. || !(truth_value_p (TREE_CODE (op_right))
  1531. || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
  1532. return;
  1533. /* The range computations only work with scalars. */
  1534. if (VECTOR_TYPE_P (TREE_TYPE (op_left))
  1535. || VECTOR_TYPE_P (TREE_TYPE (op_right)))
  1536. return;
  1537. /* We first test whether either side separately is trivially true
  1538. (with OR) or trivially false (with AND). If so, do not warn.
  1539. This is a common idiom for testing ranges of data types in
  1540. portable code. */
  1541. lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
  1542. if (!lhs)
  1543. return;
  1544. if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
  1545. lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
  1546. /* If this is an OR operation, invert both sides; now, the result
  1547. should be always false to get a warning. */
  1548. if (or_op)
  1549. in0_p = !in0_p;
  1550. tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
  1551. if (tem && integer_zerop (tem))
  1552. return;
  1553. rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
  1554. if (!rhs)
  1555. return;
  1556. if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
  1557. rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
  1558. /* If this is an OR operation, invert both sides; now, the result
  1559. should be always false to get a warning. */
  1560. if (or_op)
  1561. in1_p = !in1_p;
  1562. tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
  1563. if (tem && integer_zerop (tem))
  1564. return;
  1565. /* If both expressions have the same operand, if we can merge the
  1566. ranges, and if the range test is always false, then warn. */
  1567. if (operand_equal_p (lhs, rhs, 0)
  1568. && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
  1569. in1_p, low1, high1)
  1570. && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
  1571. type, lhs, in_p, low, high))
  1572. && integer_zerop (tem))
  1573. {
  1574. if (or_op)
  1575. warning_at (location, OPT_Wlogical_op,
  1576. "logical %<or%> "
  1577. "of collectively exhaustive tests is always true");
  1578. else
  1579. warning_at (location, OPT_Wlogical_op,
  1580. "logical %<and%> "
  1581. "of mutually exclusive tests is always false");
  1582. }
  1583. }
  1584. /* Warn about logical not used on the left hand side operand of a comparison.
  1585. This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
  1586. Do not warn if RHS is of a boolean type. */
  1587. void
  1588. warn_logical_not_parentheses (location_t location, enum tree_code code,
  1589. tree rhs)
  1590. {
  1591. if (TREE_CODE_CLASS (code) != tcc_comparison
  1592. || TREE_TYPE (rhs) == NULL_TREE
  1593. || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
  1594. return;
  1595. /* Don't warn for !x == 0 or !y != 0, those are equivalent to
  1596. !(x == 0) or !(y != 0). */
  1597. if ((code == EQ_EXPR || code == NE_EXPR)
  1598. && integer_zerop (rhs))
  1599. return;
  1600. warning_at (location, OPT_Wlogical_not_parentheses,
  1601. "logical not is only applied to the left hand side of "
  1602. "comparison");
  1603. }
  1604. /* Warn if EXP contains any computations whose results are not used.
  1605. Return true if a warning is printed; false otherwise. LOCUS is the
  1606. (potential) location of the expression. */
  1607. bool
  1608. warn_if_unused_value (const_tree exp, location_t locus)
  1609. {
  1610. restart:
  1611. if (TREE_USED (exp) || TREE_NO_WARNING (exp))
  1612. return false;
  1613. /* Don't warn about void constructs. This includes casting to void,
  1614. void function calls, and statement expressions with a final cast
  1615. to void. */
  1616. if (VOID_TYPE_P (TREE_TYPE (exp)))
  1617. return false;
  1618. if (EXPR_HAS_LOCATION (exp))
  1619. locus = EXPR_LOCATION (exp);
  1620. switch (TREE_CODE (exp))
  1621. {
  1622. case PREINCREMENT_EXPR:
  1623. case POSTINCREMENT_EXPR:
  1624. case PREDECREMENT_EXPR:
  1625. case POSTDECREMENT_EXPR:
  1626. case MODIFY_EXPR:
  1627. case INIT_EXPR:
  1628. case TARGET_EXPR:
  1629. case CALL_EXPR:
  1630. case TRY_CATCH_EXPR:
  1631. case WITH_CLEANUP_EXPR:
  1632. case EXIT_EXPR:
  1633. case VA_ARG_EXPR:
  1634. return false;
  1635. case BIND_EXPR:
  1636. /* For a binding, warn if no side effect within it. */
  1637. exp = BIND_EXPR_BODY (exp);
  1638. goto restart;
  1639. case SAVE_EXPR:
  1640. case NON_LVALUE_EXPR:
  1641. case NOP_EXPR:
  1642. exp = TREE_OPERAND (exp, 0);
  1643. goto restart;
  1644. case TRUTH_ORIF_EXPR:
  1645. case TRUTH_ANDIF_EXPR:
  1646. /* In && or ||, warn if 2nd operand has no side effect. */
  1647. exp = TREE_OPERAND (exp, 1);
  1648. goto restart;
  1649. case COMPOUND_EXPR:
  1650. if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
  1651. return true;
  1652. /* Let people do `(foo (), 0)' without a warning. */
  1653. if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
  1654. return false;
  1655. exp = TREE_OPERAND (exp, 1);
  1656. goto restart;
  1657. case COND_EXPR:
  1658. /* If this is an expression with side effects, don't warn; this
  1659. case commonly appears in macro expansions. */
  1660. if (TREE_SIDE_EFFECTS (exp))
  1661. return false;
  1662. goto warn;
  1663. case INDIRECT_REF:
  1664. /* Don't warn about automatic dereferencing of references, since
  1665. the user cannot control it. */
  1666. if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
  1667. {
  1668. exp = TREE_OPERAND (exp, 0);
  1669. goto restart;
  1670. }
  1671. /* Fall through. */
  1672. default:
  1673. /* Referencing a volatile value is a side effect, so don't warn. */
  1674. if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
  1675. && TREE_THIS_VOLATILE (exp))
  1676. return false;
  1677. /* If this is an expression which has no operands, there is no value
  1678. to be unused. There are no such language-independent codes,
  1679. but front ends may define such. */
  1680. if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
  1681. return false;
  1682. warn:
  1683. return warning_at (locus, OPT_Wunused_value, "value computed is not used");
  1684. }
  1685. }
  1686. /* Print a warning about casts that might indicate violation
  1687. of strict aliasing rules if -Wstrict-aliasing is used and
  1688. strict aliasing mode is in effect. OTYPE is the original
  1689. TREE_TYPE of EXPR, and TYPE the type we're casting to. */
  1690. bool
  1691. strict_aliasing_warning (tree otype, tree type, tree expr)
  1692. {
  1693. /* Strip pointer conversion chains and get to the correct original type. */
  1694. STRIP_NOPS (expr);
  1695. otype = TREE_TYPE (expr);
  1696. if (!(flag_strict_aliasing
  1697. && POINTER_TYPE_P (type)
  1698. && POINTER_TYPE_P (otype)
  1699. && !VOID_TYPE_P (TREE_TYPE (type)))
  1700. /* If the type we are casting to is a ref-all pointer
  1701. dereferencing it is always valid. */
  1702. || TYPE_REF_CAN_ALIAS_ALL (type))
  1703. return false;
  1704. if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
  1705. && (DECL_P (TREE_OPERAND (expr, 0))
  1706. || handled_component_p (TREE_OPERAND (expr, 0))))
  1707. {
  1708. /* Casting the address of an object to non void pointer. Warn
  1709. if the cast breaks type based aliasing. */
  1710. if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
  1711. {
  1712. warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
  1713. "might break strict-aliasing rules");
  1714. return true;
  1715. }
  1716. else
  1717. {
  1718. /* warn_strict_aliasing >= 3. This includes the default (3).
  1719. Only warn if the cast is dereferenced immediately. */
  1720. alias_set_type set1 =
  1721. get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
  1722. alias_set_type set2 = get_alias_set (TREE_TYPE (type));
  1723. if (set1 != set2 && set2 != 0
  1724. && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
  1725. {
  1726. warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
  1727. "pointer will break strict-aliasing rules");
  1728. return true;
  1729. }
  1730. else if (warn_strict_aliasing == 2
  1731. && !alias_sets_must_conflict_p (set1, set2))
  1732. {
  1733. warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
  1734. "pointer might break strict-aliasing rules");
  1735. return true;
  1736. }
  1737. }
  1738. }
  1739. else
  1740. if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
  1741. {
  1742. /* At this level, warn for any conversions, even if an address is
  1743. not taken in the same statement. This will likely produce many
  1744. false positives, but could be useful to pinpoint problems that
  1745. are not revealed at higher levels. */
  1746. alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
  1747. alias_set_type set2 = get_alias_set (TREE_TYPE (type));
  1748. if (!COMPLETE_TYPE_P (type)
  1749. || !alias_sets_must_conflict_p (set1, set2))
  1750. {
  1751. warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
  1752. "pointer might break strict-aliasing rules");
  1753. return true;
  1754. }
  1755. }
  1756. return false;
  1757. }
  1758. /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
  1759. sizeof as last operand of certain builtins. */
  1760. void
  1761. sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
  1762. vec<tree, va_gc> *params, tree *sizeof_arg,
  1763. bool (*comp_types) (tree, tree))
  1764. {
  1765. tree type, dest = NULL_TREE, src = NULL_TREE, tem;
  1766. bool strop = false, cmp = false;
  1767. unsigned int idx = ~0;
  1768. location_t loc;
  1769. if (TREE_CODE (callee) != FUNCTION_DECL
  1770. || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
  1771. || vec_safe_length (params) <= 1)
  1772. return;
  1773. switch (DECL_FUNCTION_CODE (callee))
  1774. {
  1775. case BUILT_IN_STRNCMP:
  1776. case BUILT_IN_STRNCASECMP:
  1777. cmp = true;
  1778. /* FALLTHRU */
  1779. case BUILT_IN_STRNCPY:
  1780. case BUILT_IN_STRNCPY_CHK:
  1781. case BUILT_IN_STRNCAT:
  1782. case BUILT_IN_STRNCAT_CHK:
  1783. case BUILT_IN_STPNCPY:
  1784. case BUILT_IN_STPNCPY_CHK:
  1785. strop = true;
  1786. /* FALLTHRU */
  1787. case BUILT_IN_MEMCPY:
  1788. case BUILT_IN_MEMCPY_CHK:
  1789. case BUILT_IN_MEMMOVE:
  1790. case BUILT_IN_MEMMOVE_CHK:
  1791. if (params->length () < 3)
  1792. return;
  1793. src = (*params)[1];
  1794. dest = (*params)[0];
  1795. idx = 2;
  1796. break;
  1797. case BUILT_IN_BCOPY:
  1798. if (params->length () < 3)
  1799. return;
  1800. src = (*params)[0];
  1801. dest = (*params)[1];
  1802. idx = 2;
  1803. break;
  1804. case BUILT_IN_MEMCMP:
  1805. case BUILT_IN_BCMP:
  1806. if (params->length () < 3)
  1807. return;
  1808. src = (*params)[1];
  1809. dest = (*params)[0];
  1810. idx = 2;
  1811. cmp = true;
  1812. break;
  1813. case BUILT_IN_MEMSET:
  1814. case BUILT_IN_MEMSET_CHK:
  1815. if (params->length () < 3)
  1816. return;
  1817. dest = (*params)[0];
  1818. idx = 2;
  1819. break;
  1820. case BUILT_IN_BZERO:
  1821. dest = (*params)[0];
  1822. idx = 1;
  1823. break;
  1824. case BUILT_IN_STRNDUP:
  1825. src = (*params)[0];
  1826. strop = true;
  1827. idx = 1;
  1828. break;
  1829. case BUILT_IN_MEMCHR:
  1830. if (params->length () < 3)
  1831. return;
  1832. src = (*params)[0];
  1833. idx = 2;
  1834. break;
  1835. case BUILT_IN_SNPRINTF:
  1836. case BUILT_IN_SNPRINTF_CHK:
  1837. case BUILT_IN_VSNPRINTF:
  1838. case BUILT_IN_VSNPRINTF_CHK:
  1839. dest = (*params)[0];
  1840. idx = 1;
  1841. strop = true;
  1842. break;
  1843. default:
  1844. break;
  1845. }
  1846. if (idx >= 3)
  1847. return;
  1848. if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
  1849. return;
  1850. type = TYPE_P (sizeof_arg[idx])
  1851. ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
  1852. if (!POINTER_TYPE_P (type))
  1853. return;
  1854. if (dest
  1855. && (tem = tree_strip_nop_conversions (dest))
  1856. && POINTER_TYPE_P (TREE_TYPE (tem))
  1857. && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
  1858. return;
  1859. if (src
  1860. && (tem = tree_strip_nop_conversions (src))
  1861. && POINTER_TYPE_P (TREE_TYPE (tem))
  1862. && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
  1863. return;
  1864. loc = sizeof_arg_loc[idx];
  1865. if (dest && !cmp)
  1866. {
  1867. if (!TYPE_P (sizeof_arg[idx])
  1868. && operand_equal_p (dest, sizeof_arg[idx], 0)
  1869. && comp_types (TREE_TYPE (dest), type))
  1870. {
  1871. if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
  1872. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1873. "argument to %<sizeof%> in %qD call is the same "
  1874. "expression as the destination; did you mean to "
  1875. "remove the addressof?", callee);
  1876. else if ((TYPE_PRECISION (TREE_TYPE (type))
  1877. == TYPE_PRECISION (char_type_node))
  1878. || strop)
  1879. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1880. "argument to %<sizeof%> in %qD call is the same "
  1881. "expression as the destination; did you mean to "
  1882. "provide an explicit length?", callee);
  1883. else
  1884. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1885. "argument to %<sizeof%> in %qD call is the same "
  1886. "expression as the destination; did you mean to "
  1887. "dereference it?", callee);
  1888. return;
  1889. }
  1890. if (POINTER_TYPE_P (TREE_TYPE (dest))
  1891. && !strop
  1892. && comp_types (TREE_TYPE (dest), type)
  1893. && !VOID_TYPE_P (TREE_TYPE (type)))
  1894. {
  1895. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1896. "argument to %<sizeof%> in %qD call is the same "
  1897. "pointer type %qT as the destination; expected %qT "
  1898. "or an explicit length", callee, TREE_TYPE (dest),
  1899. TREE_TYPE (TREE_TYPE (dest)));
  1900. return;
  1901. }
  1902. }
  1903. if (src && !cmp)
  1904. {
  1905. if (!TYPE_P (sizeof_arg[idx])
  1906. && operand_equal_p (src, sizeof_arg[idx], 0)
  1907. && comp_types (TREE_TYPE (src), type))
  1908. {
  1909. if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
  1910. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1911. "argument to %<sizeof%> in %qD call is the same "
  1912. "expression as the source; did you mean to "
  1913. "remove the addressof?", callee);
  1914. else if ((TYPE_PRECISION (TREE_TYPE (type))
  1915. == TYPE_PRECISION (char_type_node))
  1916. || strop)
  1917. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1918. "argument to %<sizeof%> in %qD call is the same "
  1919. "expression as the source; did you mean to "
  1920. "provide an explicit length?", callee);
  1921. else
  1922. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1923. "argument to %<sizeof%> in %qD call is the same "
  1924. "expression as the source; did you mean to "
  1925. "dereference it?", callee);
  1926. return;
  1927. }
  1928. if (POINTER_TYPE_P (TREE_TYPE (src))
  1929. && !strop
  1930. && comp_types (TREE_TYPE (src), type)
  1931. && !VOID_TYPE_P (TREE_TYPE (type)))
  1932. {
  1933. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1934. "argument to %<sizeof%> in %qD call is the same "
  1935. "pointer type %qT as the source; expected %qT "
  1936. "or an explicit length", callee, TREE_TYPE (src),
  1937. TREE_TYPE (TREE_TYPE (src)));
  1938. return;
  1939. }
  1940. }
  1941. if (dest)
  1942. {
  1943. if (!TYPE_P (sizeof_arg[idx])
  1944. && operand_equal_p (dest, sizeof_arg[idx], 0)
  1945. && comp_types (TREE_TYPE (dest), type))
  1946. {
  1947. if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
  1948. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1949. "argument to %<sizeof%> in %qD call is the same "
  1950. "expression as the first source; did you mean to "
  1951. "remove the addressof?", callee);
  1952. else if ((TYPE_PRECISION (TREE_TYPE (type))
  1953. == TYPE_PRECISION (char_type_node))
  1954. || strop)
  1955. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1956. "argument to %<sizeof%> in %qD call is the same "
  1957. "expression as the first source; did you mean to "
  1958. "provide an explicit length?", callee);
  1959. else
  1960. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1961. "argument to %<sizeof%> in %qD call is the same "
  1962. "expression as the first source; did you mean to "
  1963. "dereference it?", callee);
  1964. return;
  1965. }
  1966. if (POINTER_TYPE_P (TREE_TYPE (dest))
  1967. && !strop
  1968. && comp_types (TREE_TYPE (dest), type)
  1969. && !VOID_TYPE_P (TREE_TYPE (type)))
  1970. {
  1971. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1972. "argument to %<sizeof%> in %qD call is the same "
  1973. "pointer type %qT as the first source; expected %qT "
  1974. "or an explicit length", callee, TREE_TYPE (dest),
  1975. TREE_TYPE (TREE_TYPE (dest)));
  1976. return;
  1977. }
  1978. }
  1979. if (src)
  1980. {
  1981. if (!TYPE_P (sizeof_arg[idx])
  1982. && operand_equal_p (src, sizeof_arg[idx], 0)
  1983. && comp_types (TREE_TYPE (src), type))
  1984. {
  1985. if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
  1986. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1987. "argument to %<sizeof%> in %qD call is the same "
  1988. "expression as the second source; did you mean to "
  1989. "remove the addressof?", callee);
  1990. else if ((TYPE_PRECISION (TREE_TYPE (type))
  1991. == TYPE_PRECISION (char_type_node))
  1992. || strop)
  1993. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1994. "argument to %<sizeof%> in %qD call is the same "
  1995. "expression as the second source; did you mean to "
  1996. "provide an explicit length?", callee);
  1997. else
  1998. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  1999. "argument to %<sizeof%> in %qD call is the same "
  2000. "expression as the second source; did you mean to "
  2001. "dereference it?", callee);
  2002. return;
  2003. }
  2004. if (POINTER_TYPE_P (TREE_TYPE (src))
  2005. && !strop
  2006. && comp_types (TREE_TYPE (src), type)
  2007. && !VOID_TYPE_P (TREE_TYPE (type)))
  2008. {
  2009. warning_at (loc, OPT_Wsizeof_pointer_memaccess,
  2010. "argument to %<sizeof%> in %qD call is the same "
  2011. "pointer type %qT as the second source; expected %qT "
  2012. "or an explicit length", callee, TREE_TYPE (src),
  2013. TREE_TYPE (TREE_TYPE (src)));
  2014. return;
  2015. }
  2016. }
  2017. }
  2018. /* Warn for unlikely, improbable, or stupid DECL declarations
  2019. of `main'. */
  2020. void
  2021. check_main_parameter_types (tree decl)
  2022. {
  2023. function_args_iterator iter;
  2024. tree type;
  2025. int argct = 0;
  2026. FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
  2027. {
  2028. /* XXX void_type_node belies the abstraction. */
  2029. if (type == void_type_node || type == error_mark_node )
  2030. break;
  2031. tree t = type;
  2032. if (TYPE_ATOMIC (t))
  2033. pedwarn (input_location, OPT_Wmain,
  2034. "%<_Atomic%>-qualified parameter type %qT of %q+D",
  2035. type, decl);
  2036. while (POINTER_TYPE_P (t))
  2037. {
  2038. t = TREE_TYPE (t);
  2039. if (TYPE_ATOMIC (t))
  2040. pedwarn (input_location, OPT_Wmain,
  2041. "%<_Atomic%>-qualified parameter type %qT of %q+D",
  2042. type, decl);
  2043. }
  2044. ++argct;
  2045. switch (argct)
  2046. {
  2047. case 1:
  2048. if (TYPE_MAIN_VARIANT (type) != integer_type_node)
  2049. pedwarn (input_location, OPT_Wmain,
  2050. "first argument of %q+D should be %<int%>", decl);
  2051. break;
  2052. case 2:
  2053. if (TREE_CODE (type) != POINTER_TYPE
  2054. || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
  2055. || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
  2056. != char_type_node))
  2057. pedwarn (input_location, OPT_Wmain,
  2058. "second argument of %q+D should be %<char **%>", decl);
  2059. break;
  2060. case 3:
  2061. if (TREE_CODE (type) != POINTER_TYPE
  2062. || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
  2063. || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
  2064. != char_type_node))
  2065. pedwarn (input_location, OPT_Wmain,
  2066. "third argument of %q+D should probably be "
  2067. "%<char **%>", decl);
  2068. break;
  2069. }
  2070. }
  2071. /* It is intentional that this message does not mention the third
  2072. argument because it's only mentioned in an appendix of the
  2073. standard. */
  2074. if (argct > 0 && (argct < 2 || argct > 3))
  2075. pedwarn (input_location, OPT_Wmain,
  2076. "%q+D takes only zero or two arguments", decl);
  2077. if (stdarg_p (TREE_TYPE (decl)))
  2078. pedwarn (input_location, OPT_Wmain,
  2079. "%q+D declared as variadic function", decl);
  2080. }
  2081. /* vector_targets_convertible_p is used for vector pointer types. The
  2082. callers perform various checks that the qualifiers are satisfactory,
  2083. while OTOH vector_targets_convertible_p ignores the number of elements
  2084. in the vectors. That's fine with vector pointers as we can consider,
  2085. say, a vector of 8 elements as two consecutive vectors of 4 elements,
  2086. and that does not require and conversion of the pointer values.
  2087. In contrast, vector_types_convertible_p and
  2088. vector_types_compatible_elements_p are used for vector value types. */
  2089. /* True if pointers to distinct types T1 and T2 can be converted to
  2090. each other without an explicit cast. Only returns true for opaque
  2091. vector types. */
  2092. bool
  2093. vector_targets_convertible_p (const_tree t1, const_tree t2)
  2094. {
  2095. if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
  2096. && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
  2097. && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
  2098. return true;
  2099. return false;
  2100. }
  2101. /* vector_types_convertible_p is used for vector value types.
  2102. It could in principle call vector_targets_convertible_p as a subroutine,
  2103. but then the check for vector type would be duplicated with its callers,
  2104. and also the purpose of vector_targets_convertible_p would become
  2105. muddled.
  2106. Where vector_types_convertible_p returns true, a conversion might still be
  2107. needed to make the types match.
  2108. In contrast, vector_targets_convertible_p is used for vector pointer
  2109. values, and vector_types_compatible_elements_p is used specifically
  2110. in the context for binary operators, as a check if use is possible without
  2111. conversion. */
  2112. /* True if vector types T1 and T2 can be converted to each other
  2113. without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
  2114. can only be converted with -flax-vector-conversions yet that is not
  2115. in effect, emit a note telling the user about that option if such
  2116. a note has not previously been emitted. */
  2117. bool
  2118. vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
  2119. {
  2120. static bool emitted_lax_note = false;
  2121. bool convertible_lax;
  2122. if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
  2123. && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
  2124. return true;
  2125. convertible_lax =
  2126. (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
  2127. && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
  2128. TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
  2129. && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
  2130. == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
  2131. if (!convertible_lax || flag_lax_vector_conversions)
  2132. return convertible_lax;
  2133. if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
  2134. && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  2135. return true;
  2136. if (emit_lax_note && !emitted_lax_note)
  2137. {
  2138. emitted_lax_note = true;
  2139. inform (input_location, "use -flax-vector-conversions to permit "
  2140. "conversions between vectors with differing "
  2141. "element types or numbers of subparts");
  2142. }
  2143. return false;
  2144. }
  2145. /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
  2146. and have vector types, V0 has the same type as V1, and the number of
  2147. elements of V0, V1, MASK is the same.
  2148. In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
  2149. called with two arguments. In this case implementation passes the
  2150. first argument twice in order to share the same tree code. This fact
  2151. could enable the mask-values being twice the vector length. This is
  2152. an implementation accident and this semantics is not guaranteed to
  2153. the user. */
  2154. tree
  2155. c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
  2156. bool complain)
  2157. {
  2158. tree ret;
  2159. bool wrap = true;
  2160. bool maybe_const = false;
  2161. bool two_arguments = false;
  2162. if (v1 == NULL_TREE)
  2163. {
  2164. two_arguments = true;
  2165. v1 = v0;
  2166. }
  2167. if (v0 == error_mark_node || v1 == error_mark_node
  2168. || mask == error_mark_node)
  2169. return error_mark_node;
  2170. if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
  2171. || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
  2172. {
  2173. if (complain)
  2174. error_at (loc, "__builtin_shuffle last argument must "
  2175. "be an integer vector");
  2176. return error_mark_node;
  2177. }
  2178. if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
  2179. || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
  2180. {
  2181. if (complain)
  2182. error_at (loc, "__builtin_shuffle arguments must be vectors");
  2183. return error_mark_node;
  2184. }
  2185. if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
  2186. {
  2187. if (complain)
  2188. error_at (loc, "__builtin_shuffle argument vectors must be of "
  2189. "the same type");
  2190. return error_mark_node;
  2191. }
  2192. if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
  2193. != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
  2194. && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
  2195. != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
  2196. {
  2197. if (complain)
  2198. error_at (loc, "__builtin_shuffle number of elements of the "
  2199. "argument vector(s) and the mask vector should "
  2200. "be the same");
  2201. return error_mark_node;
  2202. }
  2203. if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
  2204. != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
  2205. {
  2206. if (complain)
  2207. error_at (loc, "__builtin_shuffle argument vector(s) inner type "
  2208. "must have the same size as inner type of the mask");
  2209. return error_mark_node;
  2210. }
  2211. if (!c_dialect_cxx ())
  2212. {
  2213. /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
  2214. v0 = c_fully_fold (v0, false, &maybe_const);
  2215. wrap &= maybe_const;
  2216. if (two_arguments)
  2217. v1 = v0 = save_expr (v0);
  2218. else
  2219. {
  2220. v1 = c_fully_fold (v1, false, &maybe_const);
  2221. wrap &= maybe_const;
  2222. }
  2223. mask = c_fully_fold (mask, false, &maybe_const);
  2224. wrap &= maybe_const;
  2225. }
  2226. else if (two_arguments)
  2227. v1 = v0 = save_expr (v0);
  2228. ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
  2229. if (!c_dialect_cxx () && !wrap)
  2230. ret = c_wrap_maybe_const (ret, true);
  2231. return ret;
  2232. }
  2233. /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
  2234. to integral type. */
  2235. static tree
  2236. c_common_get_narrower (tree op, int *unsignedp_ptr)
  2237. {
  2238. op = get_narrower (op, unsignedp_ptr);
  2239. if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
  2240. && ENUM_IS_SCOPED (TREE_TYPE (op)))
  2241. {
  2242. /* C++0x scoped enumerations don't implicitly convert to integral
  2243. type; if we stripped an explicit conversion to a larger type we
  2244. need to replace it so common_type will still work. */
  2245. tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
  2246. TYPE_UNSIGNED (TREE_TYPE (op)));
  2247. op = fold_convert (type, op);
  2248. }
  2249. return op;
  2250. }
  2251. /* This is a helper function of build_binary_op.
  2252. For certain operations if both args were extended from the same
  2253. smaller type, do the arithmetic in that type and then extend.
  2254. BITWISE indicates a bitwise operation.
  2255. For them, this optimization is safe only if
  2256. both args are zero-extended or both are sign-extended.
  2257. Otherwise, we might change the result.
  2258. Eg, (short)-1 | (unsigned short)-1 is (int)-1
  2259. but calculated in (unsigned short) it would be (unsigned short)-1.
  2260. */
  2261. tree
  2262. shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
  2263. {
  2264. int unsigned0, unsigned1;
  2265. tree arg0, arg1;
  2266. int uns;
  2267. tree type;
  2268. /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
  2269. excessive narrowing when we call get_narrower below. For
  2270. example, suppose that OP0 is of unsigned int extended
  2271. from signed char and that RESULT_TYPE is long long int.
  2272. If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
  2273. like
  2274. (long long int) (unsigned int) signed_char
  2275. which get_narrower would narrow down to
  2276. (unsigned int) signed char
  2277. If we do not cast OP0 first, get_narrower would return
  2278. signed_char, which is inconsistent with the case of the
  2279. explicit cast. */
  2280. op0 = convert (result_type, op0);
  2281. op1 = convert (result_type, op1);
  2282. arg0 = c_common_get_narrower (op0, &unsigned0);
  2283. arg1 = c_common_get_narrower (op1, &unsigned1);
  2284. /* UNS is 1 if the operation to be done is an unsigned one. */
  2285. uns = TYPE_UNSIGNED (result_type);
  2286. /* Handle the case that OP0 (or OP1) does not *contain* a conversion
  2287. but it *requires* conversion to FINAL_TYPE. */
  2288. if ((TYPE_PRECISION (TREE_TYPE (op0))
  2289. == TYPE_PRECISION (TREE_TYPE (arg0)))
  2290. && TREE_TYPE (op0) != result_type)
  2291. unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
  2292. if ((TYPE_PRECISION (TREE_TYPE (op1))
  2293. == TYPE_PRECISION (TREE_TYPE (arg1)))
  2294. && TREE_TYPE (op1) != result_type)
  2295. unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
  2296. /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
  2297. /* For bitwise operations, signedness of nominal type
  2298. does not matter. Consider only how operands were extended. */
  2299. if (bitwise)
  2300. uns = unsigned0;
  2301. /* Note that in all three cases below we refrain from optimizing
  2302. an unsigned operation on sign-extended args.
  2303. That would not be valid. */
  2304. /* Both args variable: if both extended in same way
  2305. from same width, do it in that width.
  2306. Do it unsigned if args were zero-extended. */
  2307. if ((TYPE_PRECISION (TREE_TYPE (arg0))
  2308. < TYPE_PRECISION (result_type))
  2309. && (TYPE_PRECISION (TREE_TYPE (arg1))
  2310. == TYPE_PRECISION (TREE_TYPE (arg0)))
  2311. && unsigned0 == unsigned1
  2312. && (unsigned0 || !uns))
  2313. return c_common_signed_or_unsigned_type
  2314. (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
  2315. else if (TREE_CODE (arg0) == INTEGER_CST
  2316. && (unsigned1 || !uns)
  2317. && (TYPE_PRECISION (TREE_TYPE (arg1))
  2318. < TYPE_PRECISION (result_type))
  2319. && (type
  2320. = c_common_signed_or_unsigned_type (unsigned1,
  2321. TREE_TYPE (arg1)))
  2322. && !POINTER_TYPE_P (type)
  2323. && int_fits_type_p (arg0, type))
  2324. return type;
  2325. else if (TREE_CODE (arg1) == INTEGER_CST
  2326. && (unsigned0 || !uns)
  2327. && (TYPE_PRECISION (TREE_TYPE (arg0))
  2328. < TYPE_PRECISION (result_type))
  2329. && (type
  2330. = c_common_signed_or_unsigned_type (unsigned0,
  2331. TREE_TYPE (arg0)))
  2332. && !POINTER_TYPE_P (type)
  2333. && int_fits_type_p (arg1, type))
  2334. return type;
  2335. return result_type;
  2336. }
  2337. /* Checks if expression EXPR of real/integer type cannot be converted
  2338. to the real/integer type TYPE. Function returns non-zero when:
  2339. * EXPR is a constant which cannot be exactly converted to TYPE.
  2340. * EXPR is not a constant and size of EXPR's type > than size of TYPE,
  2341. for EXPR type and TYPE being both integers or both real.
  2342. * EXPR is not a constant of real type and TYPE is an integer.
  2343. * EXPR is not a constant of integer type which cannot be
  2344. exactly converted to real type.
  2345. Function allows conversions between types of different signedness and
  2346. can return SAFE_CONVERSION (zero) in that case. Function can produce
  2347. signedness warnings if PRODUCE_WARNS is true. */
  2348. enum conversion_safety
  2349. unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
  2350. {
  2351. enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
  2352. tree expr_type = TREE_TYPE (expr);
  2353. loc = expansion_point_location_if_in_system_header (loc);
  2354. if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
  2355. {
  2356. /* Warn for real constant that is not an exact integer converted
  2357. to integer type. */
  2358. if (TREE_CODE (expr_type) == REAL_TYPE
  2359. && TREE_CODE (type) == INTEGER_TYPE)
  2360. {
  2361. if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
  2362. give_warning = UNSAFE_REAL;
  2363. }
  2364. /* Warn for an integer constant that does not fit into integer type. */
  2365. else if (TREE_CODE (expr_type) == INTEGER_TYPE
  2366. && TREE_CODE (type) == INTEGER_TYPE
  2367. && !int_fits_type_p (expr, type))
  2368. {
  2369. if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
  2370. && tree_int_cst_sgn (expr) < 0)
  2371. {
  2372. if (produce_warns)
  2373. warning_at (loc, OPT_Wsign_conversion, "negative integer"
  2374. " implicitly converted to unsigned type");
  2375. }
  2376. else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
  2377. {
  2378. if (produce_warns)
  2379. warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
  2380. " constant value to negative integer");
  2381. }
  2382. else
  2383. give_warning = UNSAFE_OTHER;
  2384. }
  2385. else if (TREE_CODE (type) == REAL_TYPE)
  2386. {
  2387. /* Warn for an integer constant that does not fit into real type. */
  2388. if (TREE_CODE (expr_type) == INTEGER_TYPE)
  2389. {
  2390. REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
  2391. if (!exact_real_truncate (TYPE_MODE (type), &a))
  2392. give_warning = UNSAFE_REAL;
  2393. }
  2394. /* Warn for a real constant that does not fit into a smaller
  2395. real type. */
  2396. else if (TREE_CODE (expr_type) == REAL_TYPE
  2397. && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
  2398. {
  2399. REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
  2400. if (!exact_real_truncate (TYPE_MODE (type), &a))
  2401. give_warning = UNSAFE_REAL;
  2402. }
  2403. }
  2404. }
  2405. else
  2406. {
  2407. /* Warn for real types converted to integer types. */
  2408. if (TREE_CODE (expr_type) == REAL_TYPE
  2409. && TREE_CODE (type) == INTEGER_TYPE)
  2410. give_warning = UNSAFE_REAL;
  2411. else if (TREE_CODE (expr_type) == INTEGER_TYPE
  2412. && TREE_CODE (type) == INTEGER_TYPE)
  2413. {
  2414. /* Don't warn about unsigned char y = 0xff, x = (int) y; */
  2415. expr = get_unwidened (expr, 0);
  2416. expr_type = TREE_TYPE (expr);
  2417. /* Don't warn for short y; short x = ((int)y & 0xff); */
  2418. if (TREE_CODE (expr) == BIT_AND_EXPR
  2419. || TREE_CODE (expr) == BIT_IOR_EXPR
  2420. || TREE_CODE (expr) == BIT_XOR_EXPR)
  2421. {
  2422. /* If both args were extended from a shortest type,
  2423. use that type if that is safe. */
  2424. expr_type = shorten_binary_op (expr_type,
  2425. TREE_OPERAND (expr, 0),
  2426. TREE_OPERAND (expr, 1),
  2427. /* bitwise */1);
  2428. if (TREE_CODE (expr) == BIT_AND_EXPR)
  2429. {
  2430. tree op0 = TREE_OPERAND (expr, 0);
  2431. tree op1 = TREE_OPERAND (expr, 1);
  2432. bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
  2433. bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
  2434. /* If one of the operands is a non-negative constant
  2435. that fits in the target type, then the type of the
  2436. other operand does not matter. */
  2437. if ((TREE_CODE (op0) == INTEGER_CST
  2438. && int_fits_type_p (op0, c_common_signed_type (type))
  2439. && int_fits_type_p (op0, c_common_unsigned_type (type)))
  2440. || (TREE_CODE (op1) == INTEGER_CST
  2441. && int_fits_type_p (op1, c_common_signed_type (type))
  2442. && int_fits_type_p (op1,
  2443. c_common_unsigned_type (type))))
  2444. return SAFE_CONVERSION;
  2445. /* If constant is unsigned and fits in the target
  2446. type, then the result will also fit. */
  2447. else if ((TREE_CODE (op0) == INTEGER_CST
  2448. && unsigned0
  2449. && int_fits_type_p (op0, type))
  2450. || (TREE_CODE (op1) == INTEGER_CST
  2451. && unsigned1
  2452. && int_fits_type_p (op1, type)))
  2453. return SAFE_CONVERSION;
  2454. }
  2455. }
  2456. /* Warn for integer types converted to smaller integer types. */
  2457. if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
  2458. give_warning = UNSAFE_OTHER;
  2459. /* When they are the same width but different signedness,
  2460. then the value may change. */
  2461. else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
  2462. && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
  2463. /* Even when converted to a bigger type, if the type is
  2464. unsigned but expr is signed, then negative values
  2465. will be changed. */
  2466. || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
  2467. && produce_warns)
  2468. warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
  2469. "may change the sign of the result",
  2470. type, expr_type);
  2471. }
  2472. /* Warn for integer types converted to real types if and only if
  2473. all the range of values of the integer type cannot be
  2474. represented by the real type. */
  2475. else if (TREE_CODE (expr_type) == INTEGER_TYPE
  2476. && TREE_CODE (type) == REAL_TYPE)
  2477. {
  2478. tree type_low_bound, type_high_bound;
  2479. REAL_VALUE_TYPE real_low_bound, real_high_bound;
  2480. /* Don't warn about char y = 0xff; float x = (int) y; */
  2481. expr = get_unwidened (expr, 0);
  2482. expr_type = TREE_TYPE (expr);
  2483. type_low_bound = TYPE_MIN_VALUE (expr_type);
  2484. type_high_bound = TYPE_MAX_VALUE (expr_type);
  2485. real_low_bound = real_value_from_int_cst (0, type_low_bound);
  2486. real_high_bound = real_value_from_int_cst (0, type_high_bound);
  2487. if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
  2488. || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
  2489. give_warning = UNSAFE_OTHER;
  2490. }
  2491. /* Warn for real types converted to smaller real types. */
  2492. else if (TREE_CODE (expr_type) == REAL_TYPE
  2493. && TREE_CODE (type) == REAL_TYPE
  2494. && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
  2495. give_warning = UNSAFE_REAL;
  2496. }
  2497. return give_warning;
  2498. }
  2499. /* Warns if the conversion of EXPR to TYPE may alter a value.
  2500. This is a helper function for warnings_for_convert_and_check. */
  2501. static void
  2502. conversion_warning (location_t loc, tree type, tree expr)
  2503. {
  2504. tree expr_type = TREE_TYPE (expr);
  2505. enum conversion_safety conversion_kind;
  2506. if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
  2507. return;
  2508. /* This may happen, because for LHS op= RHS we preevaluate
  2509. RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
  2510. means we could no longer see the code of the EXPR. */
  2511. if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
  2512. expr = C_MAYBE_CONST_EXPR_EXPR (expr);
  2513. if (TREE_CODE (expr) == SAVE_EXPR)
  2514. expr = TREE_OPERAND (expr, 0);
  2515. switch (TREE_CODE (expr))
  2516. {
  2517. case EQ_EXPR:
  2518. case NE_EXPR:
  2519. case LE_EXPR:
  2520. case GE_EXPR:
  2521. case LT_EXPR:
  2522. case GT_EXPR:
  2523. case TRUTH_ANDIF_EXPR:
  2524. case TRUTH_ORIF_EXPR:
  2525. case TRUTH_AND_EXPR:
  2526. case TRUTH_OR_EXPR:
  2527. case TRUTH_XOR_EXPR:
  2528. case TRUTH_NOT_EXPR:
  2529. /* Conversion from boolean to a signed:1 bit-field (which only
  2530. can hold the values 0 and -1) doesn't lose information - but
  2531. it does change the value. */
  2532. if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
  2533. warning_at (loc, OPT_Wconversion,
  2534. "conversion to %qT from boolean expression", type);
  2535. return;
  2536. case REAL_CST:
  2537. case INTEGER_CST:
  2538. conversion_kind = unsafe_conversion_p (loc, type, expr, true);
  2539. if (conversion_kind == UNSAFE_REAL)
  2540. warning_at (loc, OPT_Wfloat_conversion,
  2541. "conversion to %qT alters %qT constant value",
  2542. type, expr_type);
  2543. else if (conversion_kind)
  2544. warning_at (loc, OPT_Wconversion,
  2545. "conversion to %qT alters %qT constant value",
  2546. type, expr_type);
  2547. return;
  2548. case COND_EXPR:
  2549. {
  2550. /* In case of COND_EXPR, we do not care about the type of
  2551. COND_EXPR, only about the conversion of each operand. */
  2552. tree op1 = TREE_OPERAND (expr, 1);
  2553. tree op2 = TREE_OPERAND (expr, 2);
  2554. conversion_warning (loc, type, op1);
  2555. conversion_warning (loc, type, op2);
  2556. return;
  2557. }
  2558. default: /* 'expr' is not a constant. */
  2559. conversion_kind = unsafe_conversion_p (loc, type, expr, true);
  2560. if (conversion_kind == UNSAFE_REAL)
  2561. warning_at (loc, OPT_Wfloat_conversion,
  2562. "conversion to %qT from %qT may alter its value",
  2563. type, expr_type);
  2564. else if (conversion_kind)
  2565. warning_at (loc, OPT_Wconversion,
  2566. "conversion to %qT from %qT may alter its value",
  2567. type, expr_type);
  2568. }
  2569. }
  2570. /* Produce warnings after a conversion. RESULT is the result of
  2571. converting EXPR to TYPE. This is a helper function for
  2572. convert_and_check and cp_convert_and_check. */
  2573. void
  2574. warnings_for_convert_and_check (location_t loc, tree type, tree expr,
  2575. tree result)
  2576. {
  2577. loc = expansion_point_location_if_in_system_header (loc);
  2578. if (TREE_CODE (expr) == INTEGER_CST
  2579. && (TREE_CODE (type) == INTEGER_TYPE
  2580. || TREE_CODE (type) == ENUMERAL_TYPE)
  2581. && !int_fits_type_p (expr, type))
  2582. {
  2583. /* Do not diagnose overflow in a constant expression merely
  2584. because a conversion overflowed. */
  2585. if (TREE_OVERFLOW (result))
  2586. TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
  2587. if (TYPE_UNSIGNED (type))
  2588. {
  2589. /* This detects cases like converting -129 or 256 to
  2590. unsigned char. */
  2591. if (!int_fits_type_p (expr, c_common_signed_type (type)))
  2592. warning_at (loc, OPT_Woverflow,
  2593. "large integer implicitly truncated to unsigned type");
  2594. else
  2595. conversion_warning (loc, type, expr);
  2596. }
  2597. else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
  2598. warning_at (loc, OPT_Woverflow,
  2599. "overflow in implicit constant conversion");
  2600. /* No warning for converting 0x80000000 to int. */
  2601. else if (pedantic
  2602. && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
  2603. || TYPE_PRECISION (TREE_TYPE (expr))
  2604. != TYPE_PRECISION (type)))
  2605. warning_at (loc, OPT_Woverflow,
  2606. "overflow in implicit constant conversion");
  2607. else
  2608. conversion_warning (loc, type, expr);
  2609. }
  2610. else if ((TREE_CODE (result) == INTEGER_CST
  2611. || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
  2612. warning_at (loc, OPT_Woverflow,
  2613. "overflow in implicit constant conversion");
  2614. else
  2615. conversion_warning (loc, type, expr);
  2616. }
  2617. /* Convert EXPR to TYPE, warning about conversion problems with constants.
  2618. Invoke this function on every expression that is converted implicitly,
  2619. i.e. because of language rules and not because of an explicit cast. */
  2620. tree
  2621. convert_and_check (location_t loc, tree type, tree expr)
  2622. {
  2623. tree result;
  2624. tree expr_for_warning;
  2625. /* Convert from a value with possible excess precision rather than
  2626. via the semantic type, but do not warn about values not fitting
  2627. exactly in the semantic type. */
  2628. if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
  2629. {
  2630. tree orig_type = TREE_TYPE (expr);
  2631. expr = TREE_OPERAND (expr, 0);
  2632. expr_for_warning = convert (orig_type, expr);
  2633. if (orig_type == type)
  2634. return expr_for_warning;
  2635. }
  2636. else
  2637. expr_for_warning = expr;
  2638. if (TREE_TYPE (expr) == type)
  2639. return expr;
  2640. result = convert (type, expr);
  2641. if (c_inhibit_evaluation_warnings == 0
  2642. && !TREE_OVERFLOW_P (expr)
  2643. && result != error_mark_node)
  2644. warnings_for_convert_and_check (loc, type, expr_for_warning, result);
  2645. return result;
  2646. }
  2647. /* A node in a list that describes references to variables (EXPR), which are
  2648. either read accesses if WRITER is zero, or write accesses, in which case
  2649. WRITER is the parent of EXPR. */
  2650. struct tlist
  2651. {
  2652. struct tlist *next;
  2653. tree expr, writer;
  2654. };
  2655. /* Used to implement a cache the results of a call to verify_tree. We only
  2656. use this for SAVE_EXPRs. */
  2657. struct tlist_cache
  2658. {
  2659. struct tlist_cache *next;
  2660. struct tlist *cache_before_sp;
  2661. struct tlist *cache_after_sp;
  2662. tree expr;
  2663. };
  2664. /* Obstack to use when allocating tlist structures, and corresponding
  2665. firstobj. */
  2666. static struct obstack tlist_obstack;
  2667. static char *tlist_firstobj = 0;
  2668. /* Keep track of the identifiers we've warned about, so we can avoid duplicate
  2669. warnings. */
  2670. static struct tlist *warned_ids;
  2671. /* SAVE_EXPRs need special treatment. We process them only once and then
  2672. cache the results. */
  2673. static struct tlist_cache *save_expr_cache;
  2674. static void add_tlist (struct tlist **, struct tlist *, tree, int);
  2675. static void merge_tlist (struct tlist **, struct tlist *, int);
  2676. static void verify_tree (tree, struct tlist **, struct tlist **, tree);
  2677. static int warning_candidate_p (tree);
  2678. static bool candidate_equal_p (const_tree, const_tree);
  2679. static void warn_for_collisions (struct tlist *);
  2680. static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
  2681. static struct tlist *new_tlist (struct tlist *, tree, tree);
  2682. /* Create a new struct tlist and fill in its fields. */
  2683. static struct tlist *
  2684. new_tlist (struct tlist *next, tree t, tree writer)
  2685. {
  2686. struct tlist *l;
  2687. l = XOBNEW (&tlist_obstack, struct tlist);
  2688. l->next = next;
  2689. l->expr = t;
  2690. l->writer = writer;
  2691. return l;
  2692. }
  2693. /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
  2694. is nonnull, we ignore any node we find which has a writer equal to it. */
  2695. static void
  2696. add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
  2697. {
  2698. while (add)
  2699. {
  2700. struct tlist *next = add->next;
  2701. if (!copy)
  2702. add->next = *to;
  2703. if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
  2704. *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
  2705. add = next;
  2706. }
  2707. }
  2708. /* Merge the nodes of ADD into TO. This merging process is done so that for
  2709. each variable that already exists in TO, no new node is added; however if
  2710. there is a write access recorded in ADD, and an occurrence on TO is only
  2711. a read access, then the occurrence in TO will be modified to record the
  2712. write. */
  2713. static void
  2714. merge_tlist (struct tlist **to, struct tlist *add, int copy)
  2715. {
  2716. struct tlist **end = to;
  2717. while (*end)
  2718. end = &(*end)->next;
  2719. while (add)
  2720. {
  2721. int found = 0;
  2722. struct tlist *tmp2;
  2723. struct tlist *next = add->next;
  2724. for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
  2725. if (candidate_equal_p (tmp2->expr, add->expr))
  2726. {
  2727. found = 1;
  2728. if (!tmp2->writer)
  2729. tmp2->writer = add->writer;
  2730. }
  2731. if (!found)
  2732. {
  2733. *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
  2734. end = &(*end)->next;
  2735. *end = 0;
  2736. }
  2737. add = next;
  2738. }
  2739. }
  2740. /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
  2741. references in list LIST conflict with it, excluding reads if ONLY writers
  2742. is nonzero. */
  2743. static void
  2744. warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
  2745. int only_writes)
  2746. {
  2747. struct tlist *tmp;
  2748. /* Avoid duplicate warnings. */
  2749. for (tmp = warned_ids; tmp; tmp = tmp->next)
  2750. if (candidate_equal_p (tmp->expr, written))
  2751. return;
  2752. while (list)
  2753. {
  2754. if (candidate_equal_p (list->expr, written)
  2755. && !candidate_equal_p (list->writer, writer)
  2756. && (!only_writes || list->writer))
  2757. {
  2758. warned_ids = new_tlist (warned_ids, written, NULL_TREE);
  2759. warning_at (EXPR_LOC_OR_LOC (writer, input_location),
  2760. OPT_Wsequence_point, "operation on %qE may be undefined",
  2761. list->expr);
  2762. }
  2763. list = list->next;
  2764. }
  2765. }
  2766. /* Given a list LIST of references to variables, find whether any of these
  2767. can cause conflicts due to missing sequence points. */
  2768. static void
  2769. warn_for_collisions (struct tlist *list)
  2770. {
  2771. struct tlist *tmp;
  2772. for (tmp = list; tmp; tmp = tmp->next)
  2773. {
  2774. if (tmp->writer)
  2775. warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
  2776. }
  2777. }
  2778. /* Return nonzero if X is a tree that can be verified by the sequence point
  2779. warnings. */
  2780. static int
  2781. warning_candidate_p (tree x)
  2782. {
  2783. if (DECL_P (x) && DECL_ARTIFICIAL (x))
  2784. return 0;
  2785. if (TREE_CODE (x) == BLOCK)
  2786. return 0;
  2787. /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
  2788. (lvalue_p) crash on TRY/CATCH. */
  2789. if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
  2790. return 0;
  2791. if (!lvalue_p (x))
  2792. return 0;
  2793. /* No point to track non-const calls, they will never satisfy
  2794. operand_equal_p. */
  2795. if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
  2796. return 0;
  2797. if (TREE_CODE (x) == STRING_CST)
  2798. return 0;
  2799. return 1;
  2800. }
  2801. /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
  2802. static bool
  2803. candidate_equal_p (const_tree x, const_tree y)
  2804. {
  2805. return (x == y) || (x && y && operand_equal_p (x, y, 0));
  2806. }
  2807. /* Walk the tree X, and record accesses to variables. If X is written by the
  2808. parent tree, WRITER is the parent.
  2809. We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
  2810. expression or its only operand forces a sequence point, then everything up
  2811. to the sequence point is stored in PBEFORE_SP. Everything else gets stored
  2812. in PNO_SP.
  2813. Once we return, we will have emitted warnings if any subexpression before
  2814. such a sequence point could be undefined. On a higher level, however, the
  2815. sequence point may not be relevant, and we'll merge the two lists.
  2816. Example: (b++, a) + b;
  2817. The call that processes the COMPOUND_EXPR will store the increment of B
  2818. in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
  2819. processes the PLUS_EXPR will need to merge the two lists so that
  2820. eventually, all accesses end up on the same list (and we'll warn about the
  2821. unordered subexpressions b++ and b.
  2822. A note on merging. If we modify the former example so that our expression
  2823. becomes
  2824. (b++, b) + a
  2825. care must be taken not simply to add all three expressions into the final
  2826. PNO_SP list. The function merge_tlist takes care of that by merging the
  2827. before-SP list of the COMPOUND_EXPR into its after-SP list in a special
  2828. way, so that no more than one access to B is recorded. */
  2829. static void
  2830. verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
  2831. tree writer)
  2832. {
  2833. struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
  2834. enum tree_code code;
  2835. enum tree_code_class cl;
  2836. /* X may be NULL if it is the operand of an empty statement expression
  2837. ({ }). */
  2838. if (x == NULL)
  2839. return;
  2840. restart:
  2841. code = TREE_CODE (x);
  2842. cl = TREE_CODE_CLASS (code);
  2843. if (warning_candidate_p (x))
  2844. *pno_sp = new_tlist (*pno_sp, x, writer);
  2845. switch (code)
  2846. {
  2847. case CONSTRUCTOR:
  2848. case SIZEOF_EXPR:
  2849. return;
  2850. case COMPOUND_EXPR:
  2851. case TRUTH_ANDIF_EXPR:
  2852. case TRUTH_ORIF_EXPR:
  2853. tmp_before = tmp_nosp = tmp_list3 = 0;
  2854. verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
  2855. warn_for_collisions (tmp_nosp);
  2856. merge_tlist (pbefore_sp, tmp_before, 0);
  2857. merge_tlist (pbefore_sp, tmp_nosp, 0);
  2858. verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
  2859. merge_tlist (pbefore_sp, tmp_list3, 0);
  2860. return;
  2861. case COND_EXPR:
  2862. tmp_before = tmp_list2 = 0;
  2863. verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
  2864. warn_for_collisions (tmp_list2);
  2865. merge_tlist (pbefore_sp, tmp_before, 0);
  2866. merge_tlist (pbefore_sp, tmp_list2, 0);
  2867. tmp_list3 = tmp_nosp = 0;
  2868. verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
  2869. warn_for_collisions (tmp_nosp);
  2870. merge_tlist (pbefore_sp, tmp_list3, 0);
  2871. tmp_list3 = tmp_list2 = 0;
  2872. verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
  2873. warn_for_collisions (tmp_list2);
  2874. merge_tlist (pbefore_sp, tmp_list3, 0);
  2875. /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
  2876. two first, to avoid warning for (a ? b++ : b++). */
  2877. merge_tlist (&tmp_nosp, tmp_list2, 0);
  2878. add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
  2879. return;
  2880. case PREDECREMENT_EXPR:
  2881. case PREINCREMENT_EXPR:
  2882. case POSTDECREMENT_EXPR:
  2883. case POSTINCREMENT_EXPR:
  2884. verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
  2885. return;
  2886. case MODIFY_EXPR:
  2887. tmp_before = tmp_nosp = tmp_list3 = 0;
  2888. verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
  2889. verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
  2890. /* Expressions inside the LHS are not ordered wrt. the sequence points
  2891. in the RHS. Example:
  2892. *a = (a++, 2)
  2893. Despite the fact that the modification of "a" is in the before_sp
  2894. list (tmp_before), it conflicts with the use of "a" in the LHS.
  2895. We can handle this by adding the contents of tmp_list3
  2896. to those of tmp_before, and redoing the collision warnings for that
  2897. list. */
  2898. add_tlist (&tmp_before, tmp_list3, x, 1);
  2899. warn_for_collisions (tmp_before);
  2900. /* Exclude the LHS itself here; we first have to merge it into the
  2901. tmp_nosp list. This is done to avoid warning for "a = a"; if we
  2902. didn't exclude the LHS, we'd get it twice, once as a read and once
  2903. as a write. */
  2904. add_tlist (pno_sp, tmp_list3, x, 0);
  2905. warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
  2906. merge_tlist (pbefore_sp, tmp_before, 0);
  2907. if (warning_candidate_p (TREE_OPERAND (x, 0)))
  2908. merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
  2909. add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
  2910. return;
  2911. case CALL_EXPR:
  2912. /* We need to warn about conflicts among arguments and conflicts between
  2913. args and the function address. Side effects of the function address,
  2914. however, are not ordered by the sequence point of the call. */
  2915. {
  2916. call_expr_arg_iterator iter;
  2917. tree arg;
  2918. tmp_before = tmp_nosp = 0;
  2919. verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
  2920. FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
  2921. {
  2922. tmp_list2 = tmp_list3 = 0;
  2923. verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
  2924. merge_tlist (&tmp_list3, tmp_list2, 0);
  2925. add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
  2926. }
  2927. add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
  2928. warn_for_collisions (tmp_before);
  2929. add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
  2930. return;
  2931. }
  2932. case TREE_LIST:
  2933. /* Scan all the list, e.g. indices of multi dimensional array. */
  2934. while (x)
  2935. {
  2936. tmp_before = tmp_nosp = 0;
  2937. verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
  2938. merge_tlist (&tmp_nosp, tmp_before, 0);
  2939. add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
  2940. x = TREE_CHAIN (x);
  2941. }
  2942. return;
  2943. case SAVE_EXPR:
  2944. {
  2945. struct tlist_cache *t;
  2946. for (t = save_expr_cache; t; t = t->next)
  2947. if (candidate_equal_p (t->expr, x))
  2948. break;
  2949. if (!t)
  2950. {
  2951. t = XOBNEW (&tlist_obstack, struct tlist_cache);
  2952. t->next = save_expr_cache;
  2953. t->expr = x;
  2954. save_expr_cache = t;
  2955. tmp_before = tmp_nosp = 0;
  2956. verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
  2957. warn_for_collisions (tmp_nosp);
  2958. tmp_list3 = 0;
  2959. merge_tlist (&tmp_list3, tmp_nosp, 0);
  2960. t->cache_before_sp = tmp_before;
  2961. t->cache_after_sp = tmp_list3;
  2962. }
  2963. merge_tlist (pbefore_sp, t->cache_before_sp, 1);
  2964. add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
  2965. return;
  2966. }
  2967. case ADDR_EXPR:
  2968. x = TREE_OPERAND (x, 0);
  2969. if (DECL_P (x))
  2970. return;
  2971. writer = 0;
  2972. goto restart;
  2973. default:
  2974. /* For other expressions, simply recurse on their operands.
  2975. Manual tail recursion for unary expressions.
  2976. Other non-expressions need not be processed. */
  2977. if (cl == tcc_unary)
  2978. {
  2979. x = TREE_OPERAND (x, 0);
  2980. writer = 0;
  2981. goto restart;
  2982. }
  2983. else if (IS_EXPR_CODE_CLASS (cl))
  2984. {
  2985. int lp;
  2986. int max = TREE_OPERAND_LENGTH (x);
  2987. for (lp = 0; lp < max; lp++)
  2988. {
  2989. tmp_before = tmp_nosp = 0;
  2990. verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
  2991. merge_tlist (&tmp_nosp, tmp_before, 0);
  2992. add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
  2993. }
  2994. }
  2995. return;
  2996. }
  2997. }
  2998. /* Try to warn for undefined behavior in EXPR due to missing sequence
  2999. points. */
  3000. DEBUG_FUNCTION void
  3001. verify_sequence_points (tree expr)
  3002. {
  3003. struct tlist *before_sp = 0, *after_sp = 0;
  3004. warned_ids = 0;
  3005. save_expr_cache = 0;
  3006. if (tlist_firstobj == 0)
  3007. {
  3008. gcc_obstack_init (&tlist_obstack);
  3009. tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
  3010. }
  3011. verify_tree (expr, &before_sp, &after_sp, 0);
  3012. warn_for_collisions (after_sp);
  3013. obstack_free (&tlist_obstack, tlist_firstobj);
  3014. }
  3015. /* Validate the expression after `case' and apply default promotions. */
  3016. static tree
  3017. check_case_value (location_t loc, tree value)
  3018. {
  3019. if (value == NULL_TREE)
  3020. return value;
  3021. if (TREE_CODE (value) == INTEGER_CST)
  3022. /* Promote char or short to int. */
  3023. value = perform_integral_promotions (value);
  3024. else if (value != error_mark_node)
  3025. {
  3026. error_at (loc, "case label does not reduce to an integer constant");
  3027. value = error_mark_node;
  3028. }
  3029. constant_expression_warning (value);
  3030. return value;
  3031. }
  3032. /* See if the case values LOW and HIGH are in the range of the original
  3033. type (i.e. before the default conversion to int) of the switch testing
  3034. expression.
  3035. TYPE is the promoted type of the testing expression, and ORIG_TYPE is
  3036. the type before promoting it. CASE_LOW_P is a pointer to the lower
  3037. bound of the case label, and CASE_HIGH_P is the upper bound or NULL
  3038. if the case is not a case range.
  3039. The caller has to make sure that we are not called with NULL for
  3040. CASE_LOW_P (i.e. the default case).
  3041. Returns true if the case label is in range of ORIG_TYPE (saturated or
  3042. untouched) or false if the label is out of range. */
  3043. static bool
  3044. check_case_bounds (location_t loc, tree type, tree orig_type,
  3045. tree *case_low_p, tree *case_high_p)
  3046. {
  3047. tree min_value, max_value;
  3048. tree case_low = *case_low_p;
  3049. tree case_high = case_high_p ? *case_high_p : case_low;
  3050. /* If there was a problem with the original type, do nothing. */
  3051. if (orig_type == error_mark_node)
  3052. return true;
  3053. min_value = TYPE_MIN_VALUE (orig_type);
  3054. max_value = TYPE_MAX_VALUE (orig_type);
  3055. /* Case label is less than minimum for type. */
  3056. if (tree_int_cst_compare (case_low, min_value) < 0
  3057. && tree_int_cst_compare (case_high, min_value) < 0)
  3058. {
  3059. warning_at (loc, 0, "case label value is less than minimum value "
  3060. "for type");
  3061. return false;
  3062. }
  3063. /* Case value is greater than maximum for type. */
  3064. if (tree_int_cst_compare (case_low, max_value) > 0
  3065. && tree_int_cst_compare (case_high, max_value) > 0)
  3066. {
  3067. warning_at (loc, 0, "case label value exceeds maximum value for type");
  3068. return false;
  3069. }
  3070. /* Saturate lower case label value to minimum. */
  3071. if (tree_int_cst_compare (case_high, min_value) >= 0
  3072. && tree_int_cst_compare (case_low, min_value) < 0)
  3073. {
  3074. warning_at (loc, 0, "lower value in case label range"
  3075. " less than minimum value for type");
  3076. case_low = min_value;
  3077. }
  3078. /* Saturate upper case label value to maximum. */
  3079. if (tree_int_cst_compare (case_low, max_value) <= 0
  3080. && tree_int_cst_compare (case_high, max_value) > 0)
  3081. {
  3082. warning_at (loc, 0, "upper value in case label range"
  3083. " exceeds maximum value for type");
  3084. case_high = max_value;
  3085. }
  3086. if (*case_low_p != case_low)
  3087. *case_low_p = convert (type, case_low);
  3088. if (case_high_p && *case_high_p != case_high)
  3089. *case_high_p = convert (type, case_high);
  3090. return true;
  3091. }
  3092. /* Return an integer type with BITS bits of precision,
  3093. that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
  3094. tree
  3095. c_common_type_for_size (unsigned int bits, int unsignedp)
  3096. {
  3097. int i;
  3098. if (bits == TYPE_PRECISION (integer_type_node))
  3099. return unsignedp ? unsigned_type_node : integer_type_node;
  3100. if (bits == TYPE_PRECISION (signed_char_type_node))
  3101. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  3102. if (bits == TYPE_PRECISION (short_integer_type_node))
  3103. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  3104. if (bits == TYPE_PRECISION (long_integer_type_node))
  3105. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  3106. if (bits == TYPE_PRECISION (long_long_integer_type_node))
  3107. return (unsignedp ? long_long_unsigned_type_node
  3108. : long_long_integer_type_node);
  3109. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  3110. if (int_n_enabled_p[i]
  3111. && bits == int_n_data[i].bitsize)
  3112. return (unsignedp ? int_n_trees[i].unsigned_type
  3113. : int_n_trees[i].signed_type);
  3114. if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
  3115. return (unsignedp ? widest_unsigned_literal_type_node
  3116. : widest_integer_literal_type_node);
  3117. if (bits <= TYPE_PRECISION (intQI_type_node))
  3118. return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
  3119. if (bits <= TYPE_PRECISION (intHI_type_node))
  3120. return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
  3121. if (bits <= TYPE_PRECISION (intSI_type_node))
  3122. return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
  3123. if (bits <= TYPE_PRECISION (intDI_type_node))
  3124. return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
  3125. return 0;
  3126. }
  3127. /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
  3128. that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
  3129. and saturating if SATP is nonzero, otherwise not saturating. */
  3130. tree
  3131. c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
  3132. int unsignedp, int satp)
  3133. {
  3134. machine_mode mode;
  3135. if (ibit == 0)
  3136. mode = unsignedp ? UQQmode : QQmode;
  3137. else
  3138. mode = unsignedp ? UHAmode : HAmode;
  3139. for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
  3140. if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
  3141. break;
  3142. if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
  3143. {
  3144. sorry ("GCC cannot support operators with integer types and "
  3145. "fixed-point types that have too many integral and "
  3146. "fractional bits together");
  3147. return 0;
  3148. }
  3149. return c_common_type_for_mode (mode, satp);
  3150. }
  3151. /* Used for communication between c_common_type_for_mode and
  3152. c_register_builtin_type. */
  3153. tree registered_builtin_types;
  3154. /* Return a data type that has machine mode MODE.
  3155. If the mode is an integer,
  3156. then UNSIGNEDP selects between signed and unsigned types.
  3157. If the mode is a fixed-point mode,
  3158. then UNSIGNEDP selects between saturating and nonsaturating types. */
  3159. tree
  3160. c_common_type_for_mode (machine_mode mode, int unsignedp)
  3161. {
  3162. tree t;
  3163. int i;
  3164. if (mode == TYPE_MODE (integer_type_node))
  3165. return unsignedp ? unsigned_type_node : integer_type_node;
  3166. if (mode == TYPE_MODE (signed_char_type_node))
  3167. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  3168. if (mode == TYPE_MODE (short_integer_type_node))
  3169. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  3170. if (mode == TYPE_MODE (long_integer_type_node))
  3171. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  3172. if (mode == TYPE_MODE (long_long_integer_type_node))
  3173. return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
  3174. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  3175. if (int_n_enabled_p[i]
  3176. && mode == int_n_data[i].m)
  3177. return (unsignedp ? int_n_trees[i].unsigned_type
  3178. : int_n_trees[i].signed_type);
  3179. if (mode == TYPE_MODE (widest_integer_literal_type_node))
  3180. return unsignedp ? widest_unsigned_literal_type_node
  3181. : widest_integer_literal_type_node;
  3182. if (mode == QImode)
  3183. return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
  3184. if (mode == HImode)
  3185. return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
  3186. if (mode == SImode)
  3187. return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
  3188. if (mode == DImode)
  3189. return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
  3190. #if HOST_BITS_PER_WIDE_INT >= 64
  3191. if (mode == TYPE_MODE (intTI_type_node))
  3192. return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
  3193. #endif
  3194. if (mode == TYPE_MODE (float_type_node))
  3195. return float_type_node;
  3196. if (mode == TYPE_MODE (double_type_node))
  3197. return double_type_node;
  3198. if (mode == TYPE_MODE (long_double_type_node))
  3199. return long_double_type_node;
  3200. if (mode == TYPE_MODE (void_type_node))
  3201. return void_type_node;
  3202. if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
  3203. return (unsignedp
  3204. ? make_unsigned_type (GET_MODE_PRECISION (mode))
  3205. : make_signed_type (GET_MODE_PRECISION (mode)));
  3206. if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
  3207. return (unsignedp
  3208. ? make_unsigned_type (GET_MODE_PRECISION (mode))
  3209. : make_signed_type (GET_MODE_PRECISION (mode)));
  3210. if (COMPLEX_MODE_P (mode))
  3211. {
  3212. machine_mode inner_mode;
  3213. tree inner_type;
  3214. if (mode == TYPE_MODE (complex_float_type_node))
  3215. return complex_float_type_node;
  3216. if (mode == TYPE_MODE (complex_double_type_node))
  3217. return complex_double_type_node;
  3218. if (mode == TYPE_MODE (complex_long_double_type_node))
  3219. return complex_long_double_type_node;
  3220. if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
  3221. return complex_integer_type_node;
  3222. inner_mode = GET_MODE_INNER (mode);
  3223. inner_type = c_common_type_for_mode (inner_mode, unsignedp);
  3224. if (inner_type != NULL_TREE)
  3225. return build_complex_type (inner_type);
  3226. }
  3227. else if (VECTOR_MODE_P (mode))
  3228. {
  3229. machine_mode inner_mode = GET_MODE_INNER (mode);
  3230. tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
  3231. if (inner_type != NULL_TREE)
  3232. return build_vector_type_for_mode (inner_type, mode);
  3233. }
  3234. if (mode == TYPE_MODE (dfloat32_type_node))
  3235. return dfloat32_type_node;
  3236. if (mode == TYPE_MODE (dfloat64_type_node))
  3237. return dfloat64_type_node;
  3238. if (mode == TYPE_MODE (dfloat128_type_node))
  3239. return dfloat128_type_node;
  3240. if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
  3241. {
  3242. if (mode == TYPE_MODE (short_fract_type_node))
  3243. return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
  3244. if (mode == TYPE_MODE (fract_type_node))
  3245. return unsignedp ? sat_fract_type_node : fract_type_node;
  3246. if (mode == TYPE_MODE (long_fract_type_node))
  3247. return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
  3248. if (mode == TYPE_MODE (long_long_fract_type_node))
  3249. return unsignedp ? sat_long_long_fract_type_node
  3250. : long_long_fract_type_node;
  3251. if (mode == TYPE_MODE (unsigned_short_fract_type_node))
  3252. return unsignedp ? sat_unsigned_short_fract_type_node
  3253. : unsigned_short_fract_type_node;
  3254. if (mode == TYPE_MODE (unsigned_fract_type_node))
  3255. return unsignedp ? sat_unsigned_fract_type_node
  3256. : unsigned_fract_type_node;
  3257. if (mode == TYPE_MODE (unsigned_long_fract_type_node))
  3258. return unsignedp ? sat_unsigned_long_fract_type_node
  3259. : unsigned_long_fract_type_node;
  3260. if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
  3261. return unsignedp ? sat_unsigned_long_long_fract_type_node
  3262. : unsigned_long_long_fract_type_node;
  3263. if (mode == TYPE_MODE (short_accum_type_node))
  3264. return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
  3265. if (mode == TYPE_MODE (accum_type_node))
  3266. return unsignedp ? sat_accum_type_node : accum_type_node;
  3267. if (mode == TYPE_MODE (long_accum_type_node))
  3268. return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
  3269. if (mode == TYPE_MODE (long_long_accum_type_node))
  3270. return unsignedp ? sat_long_long_accum_type_node
  3271. : long_long_accum_type_node;
  3272. if (mode == TYPE_MODE (unsigned_short_accum_type_node))
  3273. return unsignedp ? sat_unsigned_short_accum_type_node
  3274. : unsigned_short_accum_type_node;
  3275. if (mode == TYPE_MODE (unsigned_accum_type_node))
  3276. return unsignedp ? sat_unsigned_accum_type_node
  3277. : unsigned_accum_type_node;
  3278. if (mode == TYPE_MODE (unsigned_long_accum_type_node))
  3279. return unsignedp ? sat_unsigned_long_accum_type_node
  3280. : unsigned_long_accum_type_node;
  3281. if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
  3282. return unsignedp ? sat_unsigned_long_long_accum_type_node
  3283. : unsigned_long_long_accum_type_node;
  3284. if (mode == QQmode)
  3285. return unsignedp ? sat_qq_type_node : qq_type_node;
  3286. if (mode == HQmode)
  3287. return unsignedp ? sat_hq_type_node : hq_type_node;
  3288. if (mode == SQmode)
  3289. return unsignedp ? sat_sq_type_node : sq_type_node;
  3290. if (mode == DQmode)
  3291. return unsignedp ? sat_dq_type_node : dq_type_node;
  3292. if (mode == TQmode)
  3293. return unsignedp ? sat_tq_type_node : tq_type_node;
  3294. if (mode == UQQmode)
  3295. return unsignedp ? sat_uqq_type_node : uqq_type_node;
  3296. if (mode == UHQmode)
  3297. return unsignedp ? sat_uhq_type_node : uhq_type_node;
  3298. if (mode == USQmode)
  3299. return unsignedp ? sat_usq_type_node : usq_type_node;
  3300. if (mode == UDQmode)
  3301. return unsignedp ? sat_udq_type_node : udq_type_node;
  3302. if (mode == UTQmode)
  3303. return unsignedp ? sat_utq_type_node : utq_type_node;
  3304. if (mode == HAmode)
  3305. return unsignedp ? sat_ha_type_node : ha_type_node;
  3306. if (mode == SAmode)
  3307. return unsignedp ? sat_sa_type_node : sa_type_node;
  3308. if (mode == DAmode)
  3309. return unsignedp ? sat_da_type_node : da_type_node;
  3310. if (mode == TAmode)
  3311. return unsignedp ? sat_ta_type_node : ta_type_node;
  3312. if (mode == UHAmode)
  3313. return unsignedp ? sat_uha_type_node : uha_type_node;
  3314. if (mode == USAmode)
  3315. return unsignedp ? sat_usa_type_node : usa_type_node;
  3316. if (mode == UDAmode)
  3317. return unsignedp ? sat_uda_type_node : uda_type_node;
  3318. if (mode == UTAmode)
  3319. return unsignedp ? sat_uta_type_node : uta_type_node;
  3320. }
  3321. for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
  3322. if (TYPE_MODE (TREE_VALUE (t)) == mode
  3323. && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
  3324. return TREE_VALUE (t);
  3325. return 0;
  3326. }
  3327. tree
  3328. c_common_unsigned_type (tree type)
  3329. {
  3330. return c_common_signed_or_unsigned_type (1, type);
  3331. }
  3332. /* Return a signed type the same as TYPE in other respects. */
  3333. tree
  3334. c_common_signed_type (tree type)
  3335. {
  3336. return c_common_signed_or_unsigned_type (0, type);
  3337. }
  3338. /* Return a type the same as TYPE except unsigned or
  3339. signed according to UNSIGNEDP. */
  3340. tree
  3341. c_common_signed_or_unsigned_type (int unsignedp, tree type)
  3342. {
  3343. tree type1;
  3344. int i;
  3345. /* This block of code emulates the behavior of the old
  3346. c_common_unsigned_type. In particular, it returns
  3347. long_unsigned_type_node if passed a long, even when a int would
  3348. have the same size. This is necessary for warnings to work
  3349. correctly in archs where sizeof(int) == sizeof(long) */
  3350. type1 = TYPE_MAIN_VARIANT (type);
  3351. if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
  3352. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  3353. if (type1 == integer_type_node || type1 == unsigned_type_node)
  3354. return unsignedp ? unsigned_type_node : integer_type_node;
  3355. if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
  3356. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  3357. if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
  3358. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  3359. if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
  3360. return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
  3361. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  3362. if (int_n_enabled_p[i]
  3363. && (type1 == int_n_trees[i].unsigned_type
  3364. || type1 == int_n_trees[i].signed_type))
  3365. return (unsignedp ? int_n_trees[i].unsigned_type
  3366. : int_n_trees[i].signed_type);
  3367. if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
  3368. return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
  3369. #if HOST_BITS_PER_WIDE_INT >= 64
  3370. if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
  3371. return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
  3372. #endif
  3373. if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
  3374. return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
  3375. if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
  3376. return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
  3377. if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
  3378. return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
  3379. if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
  3380. return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
  3381. #define C_COMMON_FIXED_TYPES(NAME) \
  3382. if (type1 == short_ ## NAME ## _type_node \
  3383. || type1 == unsigned_short_ ## NAME ## _type_node) \
  3384. return unsignedp ? unsigned_short_ ## NAME ## _type_node \
  3385. : short_ ## NAME ## _type_node; \
  3386. if (type1 == NAME ## _type_node \
  3387. || type1 == unsigned_ ## NAME ## _type_node) \
  3388. return unsignedp ? unsigned_ ## NAME ## _type_node \
  3389. : NAME ## _type_node; \
  3390. if (type1 == long_ ## NAME ## _type_node \
  3391. || type1 == unsigned_long_ ## NAME ## _type_node) \
  3392. return unsignedp ? unsigned_long_ ## NAME ## _type_node \
  3393. : long_ ## NAME ## _type_node; \
  3394. if (type1 == long_long_ ## NAME ## _type_node \
  3395. || type1 == unsigned_long_long_ ## NAME ## _type_node) \
  3396. return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
  3397. : long_long_ ## NAME ## _type_node;
  3398. #define C_COMMON_FIXED_MODE_TYPES(NAME) \
  3399. if (type1 == NAME ## _type_node \
  3400. || type1 == u ## NAME ## _type_node) \
  3401. return unsignedp ? u ## NAME ## _type_node \
  3402. : NAME ## _type_node;
  3403. #define C_COMMON_FIXED_TYPES_SAT(NAME) \
  3404. if (type1 == sat_ ## short_ ## NAME ## _type_node \
  3405. || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
  3406. return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
  3407. : sat_ ## short_ ## NAME ## _type_node; \
  3408. if (type1 == sat_ ## NAME ## _type_node \
  3409. || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
  3410. return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
  3411. : sat_ ## NAME ## _type_node; \
  3412. if (type1 == sat_ ## long_ ## NAME ## _type_node \
  3413. || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
  3414. return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
  3415. : sat_ ## long_ ## NAME ## _type_node; \
  3416. if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
  3417. || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
  3418. return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
  3419. : sat_ ## long_long_ ## NAME ## _type_node;
  3420. #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
  3421. if (type1 == sat_ ## NAME ## _type_node \
  3422. || type1 == sat_ ## u ## NAME ## _type_node) \
  3423. return unsignedp ? sat_ ## u ## NAME ## _type_node \
  3424. : sat_ ## NAME ## _type_node;
  3425. C_COMMON_FIXED_TYPES (fract);
  3426. C_COMMON_FIXED_TYPES_SAT (fract);
  3427. C_COMMON_FIXED_TYPES (accum);
  3428. C_COMMON_FIXED_TYPES_SAT (accum);
  3429. C_COMMON_FIXED_MODE_TYPES (qq);
  3430. C_COMMON_FIXED_MODE_TYPES (hq);
  3431. C_COMMON_FIXED_MODE_TYPES (sq);
  3432. C_COMMON_FIXED_MODE_TYPES (dq);
  3433. C_COMMON_FIXED_MODE_TYPES (tq);
  3434. C_COMMON_FIXED_MODE_TYPES_SAT (qq);
  3435. C_COMMON_FIXED_MODE_TYPES_SAT (hq);
  3436. C_COMMON_FIXED_MODE_TYPES_SAT (sq);
  3437. C_COMMON_FIXED_MODE_TYPES_SAT (dq);
  3438. C_COMMON_FIXED_MODE_TYPES_SAT (tq);
  3439. C_COMMON_FIXED_MODE_TYPES (ha);
  3440. C_COMMON_FIXED_MODE_TYPES (sa);
  3441. C_COMMON_FIXED_MODE_TYPES (da);
  3442. C_COMMON_FIXED_MODE_TYPES (ta);
  3443. C_COMMON_FIXED_MODE_TYPES_SAT (ha);
  3444. C_COMMON_FIXED_MODE_TYPES_SAT (sa);
  3445. C_COMMON_FIXED_MODE_TYPES_SAT (da);
  3446. C_COMMON_FIXED_MODE_TYPES_SAT (ta);
  3447. /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
  3448. the precision; they have precision set to match their range, but
  3449. may use a wider mode to match an ABI. If we change modes, we may
  3450. wind up with bad conversions. For INTEGER_TYPEs in C, must check
  3451. the precision as well, so as to yield correct results for
  3452. bit-field types. C++ does not have these separate bit-field
  3453. types, and producing a signed or unsigned variant of an
  3454. ENUMERAL_TYPE may cause other problems as well. */
  3455. if (!INTEGRAL_TYPE_P (type)
  3456. || TYPE_UNSIGNED (type) == unsignedp)
  3457. return type;
  3458. #define TYPE_OK(node) \
  3459. (TYPE_MODE (type) == TYPE_MODE (node) \
  3460. && TYPE_PRECISION (type) == TYPE_PRECISION (node))
  3461. if (TYPE_OK (signed_char_type_node))
  3462. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  3463. if (TYPE_OK (integer_type_node))
  3464. return unsignedp ? unsigned_type_node : integer_type_node;
  3465. if (TYPE_OK (short_integer_type_node))
  3466. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  3467. if (TYPE_OK (long_integer_type_node))
  3468. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  3469. if (TYPE_OK (long_long_integer_type_node))
  3470. return (unsignedp ? long_long_unsigned_type_node
  3471. : long_long_integer_type_node);
  3472. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  3473. if (int_n_enabled_p[i]
  3474. && TYPE_MODE (type) == int_n_data[i].m
  3475. && TYPE_PRECISION (type) == int_n_data[i].bitsize)
  3476. return (unsignedp ? int_n_trees[i].unsigned_type
  3477. : int_n_trees[i].signed_type);
  3478. if (TYPE_OK (widest_integer_literal_type_node))
  3479. return (unsignedp ? widest_unsigned_literal_type_node
  3480. : widest_integer_literal_type_node);
  3481. #if HOST_BITS_PER_WIDE_INT >= 64
  3482. if (TYPE_OK (intTI_type_node))
  3483. return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
  3484. #endif
  3485. if (TYPE_OK (intDI_type_node))
  3486. return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
  3487. if (TYPE_OK (intSI_type_node))
  3488. return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
  3489. if (TYPE_OK (intHI_type_node))
  3490. return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
  3491. if (TYPE_OK (intQI_type_node))
  3492. return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
  3493. #undef TYPE_OK
  3494. return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
  3495. }
  3496. /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
  3497. tree
  3498. c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
  3499. {
  3500. int i;
  3501. /* Extended integer types of the same width as a standard type have
  3502. lesser rank, so those of the same width as int promote to int or
  3503. unsigned int and are valid for printf formats expecting int or
  3504. unsigned int. To avoid such special cases, avoid creating
  3505. extended integer types for bit-fields if a standard integer type
  3506. is available. */
  3507. if (width == TYPE_PRECISION (integer_type_node))
  3508. return unsignedp ? unsigned_type_node : integer_type_node;
  3509. if (width == TYPE_PRECISION (signed_char_type_node))
  3510. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  3511. if (width == TYPE_PRECISION (short_integer_type_node))
  3512. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  3513. if (width == TYPE_PRECISION (long_integer_type_node))
  3514. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  3515. if (width == TYPE_PRECISION (long_long_integer_type_node))
  3516. return (unsignedp ? long_long_unsigned_type_node
  3517. : long_long_integer_type_node);
  3518. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  3519. if (int_n_enabled_p[i]
  3520. && width == int_n_data[i].bitsize)
  3521. return (unsignedp ? int_n_trees[i].unsigned_type
  3522. : int_n_trees[i].signed_type);
  3523. return build_nonstandard_integer_type (width, unsignedp);
  3524. }
  3525. /* The C version of the register_builtin_type langhook. */
  3526. void
  3527. c_register_builtin_type (tree type, const char* name)
  3528. {
  3529. tree decl;
  3530. decl = build_decl (UNKNOWN_LOCATION,
  3531. TYPE_DECL, get_identifier (name), type);
  3532. DECL_ARTIFICIAL (decl) = 1;
  3533. if (!TYPE_NAME (type))
  3534. TYPE_NAME (type) = decl;
  3535. pushdecl (decl);
  3536. registered_builtin_types = tree_cons (0, type, registered_builtin_types);
  3537. }
  3538. /* Print an error message for invalid operands to arith operation
  3539. CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
  3540. LOCATION is the location of the message. */
  3541. void
  3542. binary_op_error (location_t location, enum tree_code code,
  3543. tree type0, tree type1)
  3544. {
  3545. const char *opname;
  3546. switch (code)
  3547. {
  3548. case PLUS_EXPR:
  3549. opname = "+"; break;
  3550. case MINUS_EXPR:
  3551. opname = "-"; break;
  3552. case MULT_EXPR:
  3553. opname = "*"; break;
  3554. case MAX_EXPR:
  3555. opname = "max"; break;
  3556. case MIN_EXPR:
  3557. opname = "min"; break;
  3558. case EQ_EXPR:
  3559. opname = "=="; break;
  3560. case NE_EXPR:
  3561. opname = "!="; break;
  3562. case LE_EXPR:
  3563. opname = "<="; break;
  3564. case GE_EXPR:
  3565. opname = ">="; break;
  3566. case LT_EXPR:
  3567. opname = "<"; break;
  3568. case GT_EXPR:
  3569. opname = ">"; break;
  3570. case LSHIFT_EXPR:
  3571. opname = "<<"; break;
  3572. case RSHIFT_EXPR:
  3573. opname = ">>"; break;
  3574. case TRUNC_MOD_EXPR:
  3575. case FLOOR_MOD_EXPR:
  3576. opname = "%"; break;
  3577. case TRUNC_DIV_EXPR:
  3578. case FLOOR_DIV_EXPR:
  3579. opname = "/"; break;
  3580. case BIT_AND_EXPR:
  3581. opname = "&"; break;
  3582. case BIT_IOR_EXPR:
  3583. opname = "|"; break;
  3584. case TRUTH_ANDIF_EXPR:
  3585. opname = "&&"; break;
  3586. case TRUTH_ORIF_EXPR:
  3587. opname = "||"; break;
  3588. case BIT_XOR_EXPR:
  3589. opname = "^"; break;
  3590. default:
  3591. gcc_unreachable ();
  3592. }
  3593. error_at (location,
  3594. "invalid operands to binary %s (have %qT and %qT)", opname,
  3595. type0, type1);
  3596. }
  3597. /* Given an expression as a tree, return its original type. Do this
  3598. by stripping any conversion that preserves the sign and precision. */
  3599. static tree
  3600. expr_original_type (tree expr)
  3601. {
  3602. STRIP_SIGN_NOPS (expr);
  3603. return TREE_TYPE (expr);
  3604. }
  3605. /* Subroutine of build_binary_op, used for comparison operations.
  3606. See if the operands have both been converted from subword integer types
  3607. and, if so, perhaps change them both back to their original type.
  3608. This function is also responsible for converting the two operands
  3609. to the proper common type for comparison.
  3610. The arguments of this function are all pointers to local variables
  3611. of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
  3612. RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
  3613. LOC is the location of the comparison.
  3614. If this function returns nonzero, it means that the comparison has
  3615. a constant value. What this function returns is an expression for
  3616. that value. */
  3617. tree
  3618. shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
  3619. tree *restype_ptr, enum tree_code *rescode_ptr)
  3620. {
  3621. tree type;
  3622. tree op0 = *op0_ptr;
  3623. tree op1 = *op1_ptr;
  3624. int unsignedp0, unsignedp1;
  3625. int real1, real2;
  3626. tree primop0, primop1;
  3627. enum tree_code code = *rescode_ptr;
  3628. /* Throw away any conversions to wider types
  3629. already present in the operands. */
  3630. primop0 = c_common_get_narrower (op0, &unsignedp0);
  3631. primop1 = c_common_get_narrower (op1, &unsignedp1);
  3632. /* If primopN is first sign-extended from primopN's precision to opN's
  3633. precision, then zero-extended from opN's precision to
  3634. *restype_ptr precision, shortenings might be invalid. */
  3635. if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
  3636. && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
  3637. && !unsignedp0
  3638. && TYPE_UNSIGNED (TREE_TYPE (op0)))
  3639. primop0 = op0;
  3640. if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
  3641. && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
  3642. && !unsignedp1
  3643. && TYPE_UNSIGNED (TREE_TYPE (op1)))
  3644. primop1 = op1;
  3645. /* Handle the case that OP0 does not *contain* a conversion
  3646. but it *requires* conversion to FINAL_TYPE. */
  3647. if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
  3648. unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
  3649. if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
  3650. unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
  3651. /* If one of the operands must be floated, we cannot optimize. */
  3652. real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
  3653. real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
  3654. /* If first arg is constant, swap the args (changing operation
  3655. so value is preserved), for canonicalization. Don't do this if
  3656. the second arg is 0. */
  3657. if (TREE_CONSTANT (primop0)
  3658. && !integer_zerop (primop1) && !real_zerop (primop1)
  3659. && !fixed_zerop (primop1))
  3660. {
  3661. tree tem = primop0;
  3662. int temi = unsignedp0;
  3663. primop0 = primop1;
  3664. primop1 = tem;
  3665. tem = op0;
  3666. op0 = op1;
  3667. op1 = tem;
  3668. *op0_ptr = op0;
  3669. *op1_ptr = op1;
  3670. unsignedp0 = unsignedp1;
  3671. unsignedp1 = temi;
  3672. temi = real1;
  3673. real1 = real2;
  3674. real2 = temi;
  3675. switch (code)
  3676. {
  3677. case LT_EXPR:
  3678. code = GT_EXPR;
  3679. break;
  3680. case GT_EXPR:
  3681. code = LT_EXPR;
  3682. break;
  3683. case LE_EXPR:
  3684. code = GE_EXPR;
  3685. break;
  3686. case GE_EXPR:
  3687. code = LE_EXPR;
  3688. break;
  3689. default:
  3690. break;
  3691. }
  3692. *rescode_ptr = code;
  3693. }
  3694. /* If comparing an integer against a constant more bits wide,
  3695. maybe we can deduce a value of 1 or 0 independent of the data.
  3696. Or else truncate the constant now
  3697. rather than extend the variable at run time.
  3698. This is only interesting if the constant is the wider arg.
  3699. Also, it is not safe if the constant is unsigned and the
  3700. variable arg is signed, since in this case the variable
  3701. would be sign-extended and then regarded as unsigned.
  3702. Our technique fails in this case because the lowest/highest
  3703. possible unsigned results don't follow naturally from the
  3704. lowest/highest possible values of the variable operand.
  3705. For just EQ_EXPR and NE_EXPR there is another technique that
  3706. could be used: see if the constant can be faithfully represented
  3707. in the other operand's type, by truncating it and reextending it
  3708. and see if that preserves the constant's value. */
  3709. if (!real1 && !real2
  3710. && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
  3711. && TREE_CODE (primop1) == INTEGER_CST
  3712. && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
  3713. {
  3714. int min_gt, max_gt, min_lt, max_lt;
  3715. tree maxval, minval;
  3716. /* 1 if comparison is nominally unsigned. */
  3717. int unsignedp = TYPE_UNSIGNED (*restype_ptr);
  3718. tree val;
  3719. type = c_common_signed_or_unsigned_type (unsignedp0,
  3720. TREE_TYPE (primop0));
  3721. maxval = TYPE_MAX_VALUE (type);
  3722. minval = TYPE_MIN_VALUE (type);
  3723. if (unsignedp && !unsignedp0)
  3724. *restype_ptr = c_common_signed_type (*restype_ptr);
  3725. if (TREE_TYPE (primop1) != *restype_ptr)
  3726. {
  3727. /* Convert primop1 to target type, but do not introduce
  3728. additional overflow. We know primop1 is an int_cst. */
  3729. primop1 = force_fit_type (*restype_ptr,
  3730. wide_int::from
  3731. (primop1,
  3732. TYPE_PRECISION (*restype_ptr),
  3733. TYPE_SIGN (TREE_TYPE (primop1))),
  3734. 0, TREE_OVERFLOW (primop1));
  3735. }
  3736. if (type != *restype_ptr)
  3737. {
  3738. minval = convert (*restype_ptr, minval);
  3739. maxval = convert (*restype_ptr, maxval);
  3740. }
  3741. min_gt = tree_int_cst_lt (primop1, minval);
  3742. max_gt = tree_int_cst_lt (primop1, maxval);
  3743. min_lt = tree_int_cst_lt (minval, primop1);
  3744. max_lt = tree_int_cst_lt (maxval, primop1);
  3745. val = 0;
  3746. /* This used to be a switch, but Genix compiler can't handle that. */
  3747. if (code == NE_EXPR)
  3748. {
  3749. if (max_lt || min_gt)
  3750. val = truthvalue_true_node;
  3751. }
  3752. else if (code == EQ_EXPR)
  3753. {
  3754. if (max_lt || min_gt)
  3755. val = truthvalue_false_node;
  3756. }
  3757. else if (code == LT_EXPR)
  3758. {
  3759. if (max_lt)
  3760. val = truthvalue_true_node;
  3761. if (!min_lt)
  3762. val = truthvalue_false_node;
  3763. }
  3764. else if (code == GT_EXPR)
  3765. {
  3766. if (min_gt)
  3767. val = truthvalue_true_node;
  3768. if (!max_gt)
  3769. val = truthvalue_false_node;
  3770. }
  3771. else if (code == LE_EXPR)
  3772. {
  3773. if (!max_gt)
  3774. val = truthvalue_true_node;
  3775. if (min_gt)
  3776. val = truthvalue_false_node;
  3777. }
  3778. else if (code == GE_EXPR)
  3779. {
  3780. if (!min_lt)
  3781. val = truthvalue_true_node;
  3782. if (max_lt)
  3783. val = truthvalue_false_node;
  3784. }
  3785. /* If primop0 was sign-extended and unsigned comparison specd,
  3786. we did a signed comparison above using the signed type bounds.
  3787. But the comparison we output must be unsigned.
  3788. Also, for inequalities, VAL is no good; but if the signed
  3789. comparison had *any* fixed result, it follows that the
  3790. unsigned comparison just tests the sign in reverse
  3791. (positive values are LE, negative ones GE).
  3792. So we can generate an unsigned comparison
  3793. against an extreme value of the signed type. */
  3794. if (unsignedp && !unsignedp0)
  3795. {
  3796. if (val != 0)
  3797. switch (code)
  3798. {
  3799. case LT_EXPR:
  3800. case GE_EXPR:
  3801. primop1 = TYPE_MIN_VALUE (type);
  3802. val = 0;
  3803. break;
  3804. case LE_EXPR:
  3805. case GT_EXPR:
  3806. primop1 = TYPE_MAX_VALUE (type);
  3807. val = 0;
  3808. break;
  3809. default:
  3810. break;
  3811. }
  3812. type = c_common_unsigned_type (type);
  3813. }
  3814. if (TREE_CODE (primop0) != INTEGER_CST)
  3815. {
  3816. if (val == truthvalue_false_node)
  3817. warning_at (loc, OPT_Wtype_limits,
  3818. "comparison is always false due to limited range of data type");
  3819. if (val == truthvalue_true_node)
  3820. warning_at (loc, OPT_Wtype_limits,
  3821. "comparison is always true due to limited range of data type");
  3822. }
  3823. if (val != 0)
  3824. {
  3825. /* Don't forget to evaluate PRIMOP0 if it has side effects. */
  3826. if (TREE_SIDE_EFFECTS (primop0))
  3827. return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
  3828. return val;
  3829. }
  3830. /* Value is not predetermined, but do the comparison
  3831. in the type of the operand that is not constant.
  3832. TYPE is already properly set. */
  3833. }
  3834. /* If either arg is decimal float and the other is float, find the
  3835. proper common type to use for comparison. */
  3836. else if (real1 && real2
  3837. && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
  3838. && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
  3839. type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
  3840. /* If either arg is decimal float and the other is float, fail. */
  3841. else if (real1 && real2
  3842. && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
  3843. || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
  3844. return 0;
  3845. else if (real1 && real2
  3846. && (TYPE_PRECISION (TREE_TYPE (primop0))
  3847. == TYPE_PRECISION (TREE_TYPE (primop1))))
  3848. type = TREE_TYPE (primop0);
  3849. /* If args' natural types are both narrower than nominal type
  3850. and both extend in the same manner, compare them
  3851. in the type of the wider arg.
  3852. Otherwise must actually extend both to the nominal
  3853. common type lest different ways of extending
  3854. alter the result.
  3855. (eg, (short)-1 == (unsigned short)-1 should be 0.) */
  3856. else if (unsignedp0 == unsignedp1 && real1 == real2
  3857. && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
  3858. && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
  3859. {
  3860. type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
  3861. type = c_common_signed_or_unsigned_type (unsignedp0
  3862. || TYPE_UNSIGNED (*restype_ptr),
  3863. type);
  3864. /* Make sure shorter operand is extended the right way
  3865. to match the longer operand. */
  3866. primop0
  3867. = convert (c_common_signed_or_unsigned_type (unsignedp0,
  3868. TREE_TYPE (primop0)),
  3869. primop0);
  3870. primop1
  3871. = convert (c_common_signed_or_unsigned_type (unsignedp1,
  3872. TREE_TYPE (primop1)),
  3873. primop1);
  3874. }
  3875. else
  3876. {
  3877. /* Here we must do the comparison on the nominal type
  3878. using the args exactly as we received them. */
  3879. type = *restype_ptr;
  3880. primop0 = op0;
  3881. primop1 = op1;
  3882. if (!real1 && !real2 && integer_zerop (primop1)
  3883. && TYPE_UNSIGNED (*restype_ptr))
  3884. {
  3885. tree value = 0;
  3886. /* All unsigned values are >= 0, so we warn. However,
  3887. if OP0 is a constant that is >= 0, the signedness of
  3888. the comparison isn't an issue, so suppress the
  3889. warning. */
  3890. bool warn =
  3891. warn_type_limits && !in_system_header_at (loc)
  3892. && !(TREE_CODE (primop0) == INTEGER_CST
  3893. && !TREE_OVERFLOW (convert (c_common_signed_type (type),
  3894. primop0)))
  3895. /* Do not warn for enumeration types. */
  3896. && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
  3897. switch (code)
  3898. {
  3899. case GE_EXPR:
  3900. if (warn)
  3901. warning_at (loc, OPT_Wtype_limits,
  3902. "comparison of unsigned expression >= 0 is always true");
  3903. value = truthvalue_true_node;
  3904. break;
  3905. case LT_EXPR:
  3906. if (warn)
  3907. warning_at (loc, OPT_Wtype_limits,
  3908. "comparison of unsigned expression < 0 is always false");
  3909. value = truthvalue_false_node;
  3910. break;
  3911. default:
  3912. break;
  3913. }
  3914. if (value != 0)
  3915. {
  3916. /* Don't forget to evaluate PRIMOP0 if it has side effects. */
  3917. if (TREE_SIDE_EFFECTS (primop0))
  3918. return build2 (COMPOUND_EXPR, TREE_TYPE (value),
  3919. primop0, value);
  3920. return value;
  3921. }
  3922. }
  3923. }
  3924. *op0_ptr = convert (type, primop0);
  3925. *op1_ptr = convert (type, primop1);
  3926. *restype_ptr = truthvalue_type_node;
  3927. return 0;
  3928. }
  3929. /* Return a tree for the sum or difference (RESULTCODE says which)
  3930. of pointer PTROP and integer INTOP. */
  3931. tree
  3932. pointer_int_sum (location_t loc, enum tree_code resultcode,
  3933. tree ptrop, tree intop, bool complain)
  3934. {
  3935. tree size_exp, ret;
  3936. /* The result is a pointer of the same type that is being added. */
  3937. tree result_type = TREE_TYPE (ptrop);
  3938. if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
  3939. {
  3940. if (complain && warn_pointer_arith)
  3941. pedwarn (loc, OPT_Wpointer_arith,
  3942. "pointer of type %<void *%> used in arithmetic");
  3943. else if (!complain)
  3944. return error_mark_node;
  3945. size_exp = integer_one_node;
  3946. }
  3947. else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
  3948. {
  3949. if (complain && warn_pointer_arith)
  3950. pedwarn (loc, OPT_Wpointer_arith,
  3951. "pointer to a function used in arithmetic");
  3952. else if (!complain)
  3953. return error_mark_node;
  3954. size_exp = integer_one_node;
  3955. }
  3956. else
  3957. size_exp = size_in_bytes (TREE_TYPE (result_type));
  3958. /* We are manipulating pointer values, so we don't need to warn
  3959. about relying on undefined signed overflow. We disable the
  3960. warning here because we use integer types so fold won't know that
  3961. they are really pointers. */
  3962. fold_defer_overflow_warnings ();
  3963. /* If what we are about to multiply by the size of the elements
  3964. contains a constant term, apply distributive law
  3965. and multiply that constant term separately.
  3966. This helps produce common subexpressions. */
  3967. if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
  3968. && !TREE_CONSTANT (intop)
  3969. && TREE_CONSTANT (TREE_OPERAND (intop, 1))
  3970. && TREE_CONSTANT (size_exp)
  3971. /* If the constant comes from pointer subtraction,
  3972. skip this optimization--it would cause an error. */
  3973. && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
  3974. /* If the constant is unsigned, and smaller than the pointer size,
  3975. then we must skip this optimization. This is because it could cause
  3976. an overflow error if the constant is negative but INTOP is not. */
  3977. && (!TYPE_UNSIGNED (TREE_TYPE (intop))
  3978. || (TYPE_PRECISION (TREE_TYPE (intop))
  3979. == TYPE_PRECISION (TREE_TYPE (ptrop)))))
  3980. {
  3981. enum tree_code subcode = resultcode;
  3982. tree int_type = TREE_TYPE (intop);
  3983. if (TREE_CODE (intop) == MINUS_EXPR)
  3984. subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
  3985. /* Convert both subexpression types to the type of intop,
  3986. because weird cases involving pointer arithmetic
  3987. can result in a sum or difference with different type args. */
  3988. ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
  3989. subcode, ptrop,
  3990. convert (int_type, TREE_OPERAND (intop, 1)), 1);
  3991. intop = convert (int_type, TREE_OPERAND (intop, 0));
  3992. }
  3993. /* Convert the integer argument to a type the same size as sizetype
  3994. so the multiply won't overflow spuriously. */
  3995. if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
  3996. || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
  3997. intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
  3998. TYPE_UNSIGNED (sizetype)), intop);
  3999. /* Replace the integer argument with a suitable product by the object size.
  4000. Do this multiplication as signed, then convert to the appropriate type
  4001. for the pointer operation and disregard an overflow that occurred only
  4002. because of the sign-extension change in the latter conversion. */
  4003. {
  4004. tree t = build_binary_op (loc,
  4005. MULT_EXPR, intop,
  4006. convert (TREE_TYPE (intop), size_exp), 1);
  4007. intop = convert (sizetype, t);
  4008. if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
  4009. intop = wide_int_to_tree (TREE_TYPE (intop), intop);
  4010. }
  4011. /* Create the sum or difference. */
  4012. if (resultcode == MINUS_EXPR)
  4013. intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
  4014. ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
  4015. fold_undefer_and_ignore_overflow_warnings ();
  4016. return ret;
  4017. }
  4018. /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
  4019. and if NON_CONST is known not to be permitted in an evaluated part
  4020. of a constant expression. */
  4021. tree
  4022. c_wrap_maybe_const (tree expr, bool non_const)
  4023. {
  4024. bool nowarning = TREE_NO_WARNING (expr);
  4025. location_t loc = EXPR_LOCATION (expr);
  4026. /* This should never be called for C++. */
  4027. if (c_dialect_cxx ())
  4028. gcc_unreachable ();
  4029. /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
  4030. STRIP_TYPE_NOPS (expr);
  4031. expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
  4032. C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
  4033. if (nowarning)
  4034. TREE_NO_WARNING (expr) = 1;
  4035. protected_set_expr_location (expr, loc);
  4036. return expr;
  4037. }
  4038. /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
  4039. for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
  4040. around the SAVE_EXPR if needed so that c_fully_fold does not need
  4041. to look inside SAVE_EXPRs. */
  4042. tree
  4043. c_save_expr (tree expr)
  4044. {
  4045. bool maybe_const = true;
  4046. if (c_dialect_cxx ())
  4047. return save_expr (expr);
  4048. expr = c_fully_fold (expr, false, &maybe_const);
  4049. expr = save_expr (expr);
  4050. if (!maybe_const)
  4051. expr = c_wrap_maybe_const (expr, true);
  4052. return expr;
  4053. }
  4054. /* Return whether EXPR is a declaration whose address can never be
  4055. NULL. */
  4056. bool
  4057. decl_with_nonnull_addr_p (const_tree expr)
  4058. {
  4059. return (DECL_P (expr)
  4060. && (TREE_CODE (expr) == PARM_DECL
  4061. || TREE_CODE (expr) == LABEL_DECL
  4062. || !DECL_WEAK (expr)));
  4063. }
  4064. /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
  4065. or for an `if' or `while' statement or ?..: exp. It should already
  4066. have been validated to be of suitable type; otherwise, a bad
  4067. diagnostic may result.
  4068. The EXPR is located at LOCATION.
  4069. This preparation consists of taking the ordinary
  4070. representation of an expression expr and producing a valid tree
  4071. boolean expression describing whether expr is nonzero. We could
  4072. simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
  4073. but we optimize comparisons, &&, ||, and !.
  4074. The resulting type should always be `truthvalue_type_node'. */
  4075. tree
  4076. c_common_truthvalue_conversion (location_t location, tree expr)
  4077. {
  4078. switch (TREE_CODE (expr))
  4079. {
  4080. case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
  4081. case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
  4082. case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
  4083. case ORDERED_EXPR: case UNORDERED_EXPR:
  4084. if (TREE_TYPE (expr) == truthvalue_type_node)
  4085. return expr;
  4086. expr = build2 (TREE_CODE (expr), truthvalue_type_node,
  4087. TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
  4088. goto ret;
  4089. case TRUTH_ANDIF_EXPR:
  4090. case TRUTH_ORIF_EXPR:
  4091. case TRUTH_AND_EXPR:
  4092. case TRUTH_OR_EXPR:
  4093. case TRUTH_XOR_EXPR:
  4094. if (TREE_TYPE (expr) == truthvalue_type_node)
  4095. return expr;
  4096. expr = build2 (TREE_CODE (expr), truthvalue_type_node,
  4097. c_common_truthvalue_conversion (location,
  4098. TREE_OPERAND (expr, 0)),
  4099. c_common_truthvalue_conversion (location,
  4100. TREE_OPERAND (expr, 1)));
  4101. goto ret;
  4102. case TRUTH_NOT_EXPR:
  4103. if (TREE_TYPE (expr) == truthvalue_type_node)
  4104. return expr;
  4105. expr = build1 (TREE_CODE (expr), truthvalue_type_node,
  4106. c_common_truthvalue_conversion (location,
  4107. TREE_OPERAND (expr, 0)));
  4108. goto ret;
  4109. case ERROR_MARK:
  4110. return expr;
  4111. case INTEGER_CST:
  4112. return integer_zerop (expr) ? truthvalue_false_node
  4113. : truthvalue_true_node;
  4114. case REAL_CST:
  4115. return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
  4116. ? truthvalue_true_node
  4117. : truthvalue_false_node;
  4118. case FIXED_CST:
  4119. return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
  4120. &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
  4121. ? truthvalue_true_node
  4122. : truthvalue_false_node;
  4123. case FUNCTION_DECL:
  4124. expr = build_unary_op (location, ADDR_EXPR, expr, 0);
  4125. /* Fall through. */
  4126. case ADDR_EXPR:
  4127. {
  4128. tree inner = TREE_OPERAND (expr, 0);
  4129. if (decl_with_nonnull_addr_p (inner))
  4130. {
  4131. /* Common Ada/Pascal programmer's mistake. */
  4132. warning_at (location,
  4133. OPT_Waddress,
  4134. "the address of %qD will always evaluate as %<true%>",
  4135. inner);
  4136. return truthvalue_true_node;
  4137. }
  4138. break;
  4139. }
  4140. case COMPLEX_EXPR:
  4141. expr = build_binary_op (EXPR_LOCATION (expr),
  4142. (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
  4143. ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
  4144. c_common_truthvalue_conversion (location,
  4145. TREE_OPERAND (expr, 0)),
  4146. c_common_truthvalue_conversion (location,
  4147. TREE_OPERAND (expr, 1)),
  4148. 0);
  4149. goto ret;
  4150. case NEGATE_EXPR:
  4151. case ABS_EXPR:
  4152. case FLOAT_EXPR:
  4153. case EXCESS_PRECISION_EXPR:
  4154. /* These don't change whether an object is nonzero or zero. */
  4155. return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
  4156. case LROTATE_EXPR:
  4157. case RROTATE_EXPR:
  4158. /* These don't change whether an object is zero or nonzero, but
  4159. we can't ignore them if their second arg has side-effects. */
  4160. if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
  4161. {
  4162. expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
  4163. TREE_OPERAND (expr, 1),
  4164. c_common_truthvalue_conversion
  4165. (location, TREE_OPERAND (expr, 0)));
  4166. goto ret;
  4167. }
  4168. else
  4169. return c_common_truthvalue_conversion (location,
  4170. TREE_OPERAND (expr, 0));
  4171. case COND_EXPR:
  4172. /* Distribute the conversion into the arms of a COND_EXPR. */
  4173. if (c_dialect_cxx ())
  4174. {
  4175. tree op1 = TREE_OPERAND (expr, 1);
  4176. tree op2 = TREE_OPERAND (expr, 2);
  4177. /* In C++ one of the arms might have void type if it is throw. */
  4178. if (!VOID_TYPE_P (TREE_TYPE (op1)))
  4179. op1 = c_common_truthvalue_conversion (location, op1);
  4180. if (!VOID_TYPE_P (TREE_TYPE (op2)))
  4181. op2 = c_common_truthvalue_conversion (location, op2);
  4182. expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
  4183. TREE_OPERAND (expr, 0), op1, op2);
  4184. goto ret;
  4185. }
  4186. else
  4187. {
  4188. /* Folding will happen later for C. */
  4189. expr = build3 (COND_EXPR, truthvalue_type_node,
  4190. TREE_OPERAND (expr, 0),
  4191. c_common_truthvalue_conversion (location,
  4192. TREE_OPERAND (expr, 1)),
  4193. c_common_truthvalue_conversion (location,
  4194. TREE_OPERAND (expr, 2)));
  4195. goto ret;
  4196. }
  4197. CASE_CONVERT:
  4198. {
  4199. tree totype = TREE_TYPE (expr);
  4200. tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
  4201. /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
  4202. since that affects how `default_conversion' will behave. */
  4203. if (TREE_CODE (totype) == REFERENCE_TYPE
  4204. || TREE_CODE (fromtype) == REFERENCE_TYPE)
  4205. break;
  4206. /* Don't strip a conversion from C++0x scoped enum, since they
  4207. don't implicitly convert to other types. */
  4208. if (TREE_CODE (fromtype) == ENUMERAL_TYPE
  4209. && ENUM_IS_SCOPED (fromtype))
  4210. break;
  4211. /* If this isn't narrowing the argument, we can ignore it. */
  4212. if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
  4213. return c_common_truthvalue_conversion (location,
  4214. TREE_OPERAND (expr, 0));
  4215. }
  4216. break;
  4217. case MODIFY_EXPR:
  4218. if (!TREE_NO_WARNING (expr)
  4219. && warn_parentheses)
  4220. {
  4221. warning (OPT_Wparentheses,
  4222. "suggest parentheses around assignment used as truth value");
  4223. TREE_NO_WARNING (expr) = 1;
  4224. }
  4225. break;
  4226. default:
  4227. break;
  4228. }
  4229. if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
  4230. {
  4231. tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
  4232. expr = (build_binary_op
  4233. (EXPR_LOCATION (expr),
  4234. (TREE_SIDE_EFFECTS (expr)
  4235. ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
  4236. c_common_truthvalue_conversion
  4237. (location,
  4238. build_unary_op (location, REALPART_EXPR, t, 0)),
  4239. c_common_truthvalue_conversion
  4240. (location,
  4241. build_unary_op (location, IMAGPART_EXPR, t, 0)),
  4242. 0));
  4243. goto ret;
  4244. }
  4245. if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
  4246. {
  4247. tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
  4248. FCONST0 (TYPE_MODE
  4249. (TREE_TYPE (expr))));
  4250. return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
  4251. }
  4252. else
  4253. return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
  4254. ret:
  4255. protected_set_expr_location (expr, location);
  4256. return expr;
  4257. }
  4258. static void def_builtin_1 (enum built_in_function fncode,
  4259. const char *name,
  4260. enum built_in_class fnclass,
  4261. tree fntype, tree libtype,
  4262. bool both_p, bool fallback_p, bool nonansi_p,
  4263. tree fnattrs, bool implicit_p);
  4264. /* Apply the TYPE_QUALS to the new DECL. */
  4265. void
  4266. c_apply_type_quals_to_decl (int type_quals, tree decl)
  4267. {
  4268. tree type = TREE_TYPE (decl);
  4269. if (type == error_mark_node)
  4270. return;
  4271. if ((type_quals & TYPE_QUAL_CONST)
  4272. || (type && TREE_CODE (type) == REFERENCE_TYPE))
  4273. /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
  4274. constructor can produce constant init, so rely on cp_finish_decl to
  4275. clear TREE_READONLY if the variable has non-constant init. */
  4276. TREE_READONLY (decl) = 1;
  4277. if (type_quals & TYPE_QUAL_VOLATILE)
  4278. {
  4279. TREE_SIDE_EFFECTS (decl) = 1;
  4280. TREE_THIS_VOLATILE (decl) = 1;
  4281. }
  4282. if (type_quals & TYPE_QUAL_RESTRICT)
  4283. {
  4284. while (type && TREE_CODE (type) == ARRAY_TYPE)
  4285. /* Allow 'restrict' on arrays of pointers.
  4286. FIXME currently we just ignore it. */
  4287. type = TREE_TYPE (type);
  4288. if (!type
  4289. || !POINTER_TYPE_P (type)
  4290. || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
  4291. error ("invalid use of %<restrict%>");
  4292. }
  4293. }
  4294. struct c_type_hasher : ggc_hasher<tree>
  4295. {
  4296. static hashval_t hash (tree);
  4297. static bool equal (tree, tree);
  4298. };
  4299. /* Hash function for the problem of multiple type definitions in
  4300. different files. This must hash all types that will compare
  4301. equal via comptypes to the same value. In practice it hashes
  4302. on some of the simple stuff and leaves the details to comptypes. */
  4303. hashval_t
  4304. c_type_hasher::hash (tree t)
  4305. {
  4306. int n_elements;
  4307. int shift, size;
  4308. tree t2;
  4309. switch (TREE_CODE (t))
  4310. {
  4311. /* For pointers, hash on pointee type plus some swizzling. */
  4312. case POINTER_TYPE:
  4313. return hash (TREE_TYPE (t)) ^ 0x3003003;
  4314. /* Hash on number of elements and total size. */
  4315. case ENUMERAL_TYPE:
  4316. shift = 3;
  4317. t2 = TYPE_VALUES (t);
  4318. break;
  4319. case RECORD_TYPE:
  4320. shift = 0;
  4321. t2 = TYPE_FIELDS (t);
  4322. break;
  4323. case QUAL_UNION_TYPE:
  4324. shift = 1;
  4325. t2 = TYPE_FIELDS (t);
  4326. break;
  4327. case UNION_TYPE:
  4328. shift = 2;
  4329. t2 = TYPE_FIELDS (t);
  4330. break;
  4331. default:
  4332. gcc_unreachable ();
  4333. }
  4334. /* FIXME: We want to use a DECL_CHAIN iteration method here, but
  4335. TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
  4336. n_elements = list_length (t2);
  4337. /* We might have a VLA here. */
  4338. if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
  4339. size = 0;
  4340. else
  4341. size = TREE_INT_CST_LOW (TYPE_SIZE (t));
  4342. return ((size << 24) | (n_elements << shift));
  4343. }
  4344. bool
  4345. c_type_hasher::equal (tree t1, tree t2)
  4346. {
  4347. return lang_hooks.types_compatible_p (t1, t2);
  4348. }
  4349. static GTY(()) hash_table<c_type_hasher> *type_hash_table;
  4350. /* Return the typed-based alias set for T, which may be an expression
  4351. or a type. Return -1 if we don't do anything special. */
  4352. alias_set_type
  4353. c_common_get_alias_set (tree t)
  4354. {
  4355. tree u;
  4356. /* For VLAs, use the alias set of the element type rather than the
  4357. default of alias set 0 for types compared structurally. */
  4358. if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
  4359. {
  4360. if (TREE_CODE (t) == ARRAY_TYPE)
  4361. return get_alias_set (TREE_TYPE (t));
  4362. return -1;
  4363. }
  4364. /* Permit type-punning when accessing a union, provided the access
  4365. is directly through the union. For example, this code does not
  4366. permit taking the address of a union member and then storing
  4367. through it. Even the type-punning allowed here is a GCC
  4368. extension, albeit a common and useful one; the C standard says
  4369. that such accesses have implementation-defined behavior. */
  4370. for (u = t;
  4371. TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
  4372. u = TREE_OPERAND (u, 0))
  4373. if (TREE_CODE (u) == COMPONENT_REF
  4374. && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
  4375. return 0;
  4376. /* That's all the expressions we handle specially. */
  4377. if (!TYPE_P (t))
  4378. return -1;
  4379. /* The C standard guarantees that any object may be accessed via an
  4380. lvalue that has character type. */
  4381. if (t == char_type_node
  4382. || t == signed_char_type_node
  4383. || t == unsigned_char_type_node)
  4384. return 0;
  4385. /* The C standard specifically allows aliasing between signed and
  4386. unsigned variants of the same type. We treat the signed
  4387. variant as canonical. */
  4388. if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
  4389. {
  4390. tree t1 = c_common_signed_type (t);
  4391. /* t1 == t can happen for boolean nodes which are always unsigned. */
  4392. if (t1 != t)
  4393. return get_alias_set (t1);
  4394. }
  4395. /* Handle the case of multiple type nodes referring to "the same" type,
  4396. which occurs with IMA. These share an alias set. FIXME: Currently only
  4397. C90 is handled. (In C99 type compatibility is not transitive, which
  4398. complicates things mightily. The alias set splay trees can theoretically
  4399. represent this, but insertion is tricky when you consider all the
  4400. different orders things might arrive in.) */
  4401. if (c_language != clk_c || flag_isoc99)
  4402. return -1;
  4403. /* Save time if there's only one input file. */
  4404. if (num_in_fnames == 1)
  4405. return -1;
  4406. /* Pointers need special handling if they point to any type that
  4407. needs special handling (below). */
  4408. if (TREE_CODE (t) == POINTER_TYPE)
  4409. {
  4410. tree t2;
  4411. /* Find bottom type under any nested POINTERs. */
  4412. for (t2 = TREE_TYPE (t);
  4413. TREE_CODE (t2) == POINTER_TYPE;
  4414. t2 = TREE_TYPE (t2))
  4415. ;
  4416. if (TREE_CODE (t2) != RECORD_TYPE
  4417. && TREE_CODE (t2) != ENUMERAL_TYPE
  4418. && TREE_CODE (t2) != QUAL_UNION_TYPE
  4419. && TREE_CODE (t2) != UNION_TYPE)
  4420. return -1;
  4421. if (TYPE_SIZE (t2) == 0)
  4422. return -1;
  4423. }
  4424. /* These are the only cases that need special handling. */
  4425. if (TREE_CODE (t) != RECORD_TYPE
  4426. && TREE_CODE (t) != ENUMERAL_TYPE
  4427. && TREE_CODE (t) != QUAL_UNION_TYPE
  4428. && TREE_CODE (t) != UNION_TYPE
  4429. && TREE_CODE (t) != POINTER_TYPE)
  4430. return -1;
  4431. /* Undefined? */
  4432. if (TYPE_SIZE (t) == 0)
  4433. return -1;
  4434. /* Look up t in hash table. Only one of the compatible types within each
  4435. alias set is recorded in the table. */
  4436. if (!type_hash_table)
  4437. type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
  4438. tree *slot = type_hash_table->find_slot (t, INSERT);
  4439. if (*slot != NULL)
  4440. {
  4441. TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
  4442. return TYPE_ALIAS_SET ((tree)*slot);
  4443. }
  4444. else
  4445. /* Our caller will assign and record (in t) a new alias set; all we need
  4446. to do is remember t in the hash table. */
  4447. *slot = t;
  4448. return -1;
  4449. }
  4450. /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
  4451. the IS_SIZEOF parameter indicates which operator is being applied.
  4452. The COMPLAIN flag controls whether we should diagnose possibly
  4453. ill-formed constructs or not. LOC is the location of the SIZEOF or
  4454. TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
  4455. a type in any context should be returned, rather than the normal
  4456. alignment for that type. */
  4457. tree
  4458. c_sizeof_or_alignof_type (location_t loc,
  4459. tree type, bool is_sizeof, bool min_alignof,
  4460. int complain)
  4461. {
  4462. const char *op_name;
  4463. tree value = NULL;
  4464. enum tree_code type_code = TREE_CODE (type);
  4465. op_name = is_sizeof ? "sizeof" : "__alignof__";
  4466. if (type_code == FUNCTION_TYPE)
  4467. {
  4468. if (is_sizeof)
  4469. {
  4470. if (complain && warn_pointer_arith)
  4471. pedwarn (loc, OPT_Wpointer_arith,
  4472. "invalid application of %<sizeof%> to a function type");
  4473. else if (!complain)
  4474. return error_mark_node;
  4475. value = size_one_node;
  4476. }
  4477. else
  4478. {
  4479. if (complain)
  4480. {
  4481. if (c_dialect_cxx ())
  4482. pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
  4483. "%<alignof%> applied to a function type");
  4484. else
  4485. pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
  4486. "%<_Alignof%> applied to a function type");
  4487. }
  4488. value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
  4489. }
  4490. }
  4491. else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
  4492. {
  4493. if (type_code == VOID_TYPE
  4494. && complain && warn_pointer_arith)
  4495. pedwarn (loc, OPT_Wpointer_arith,
  4496. "invalid application of %qs to a void type", op_name);
  4497. else if (!complain)
  4498. return error_mark_node;
  4499. value = size_one_node;
  4500. }
  4501. else if (!COMPLETE_TYPE_P (type)
  4502. && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
  4503. {
  4504. if (complain)
  4505. error_at (loc, "invalid application of %qs to incomplete type %qT",
  4506. op_name, type);
  4507. return error_mark_node;
  4508. }
  4509. else if (c_dialect_cxx () && type_code == ARRAY_TYPE
  4510. && !COMPLETE_TYPE_P (TREE_TYPE (type)))
  4511. {
  4512. if (complain)
  4513. error_at (loc, "invalid application of %qs to array type %qT of "
  4514. "incomplete element type", op_name, type);
  4515. return error_mark_node;
  4516. }
  4517. else
  4518. {
  4519. if (is_sizeof)
  4520. /* Convert in case a char is more than one unit. */
  4521. value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
  4522. size_int (TYPE_PRECISION (char_type_node)
  4523. / BITS_PER_UNIT));
  4524. else if (min_alignof)
  4525. value = size_int (min_align_of_type (type));
  4526. else
  4527. value = size_int (TYPE_ALIGN_UNIT (type));
  4528. }
  4529. /* VALUE will have the middle-end integer type sizetype.
  4530. However, we should really return a value of type `size_t',
  4531. which is just a typedef for an ordinary integer type. */
  4532. value = fold_convert_loc (loc, size_type_node, value);
  4533. return value;
  4534. }
  4535. /* Implement the __alignof keyword: Return the minimum required
  4536. alignment of EXPR, measured in bytes. For VAR_DECLs,
  4537. FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
  4538. from an "aligned" __attribute__ specification). LOC is the
  4539. location of the ALIGNOF operator. */
  4540. tree
  4541. c_alignof_expr (location_t loc, tree expr)
  4542. {
  4543. tree t;
  4544. if (VAR_OR_FUNCTION_DECL_P (expr))
  4545. t = size_int (DECL_ALIGN_UNIT (expr));
  4546. else if (TREE_CODE (expr) == COMPONENT_REF
  4547. && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
  4548. {
  4549. error_at (loc, "%<__alignof%> applied to a bit-field");
  4550. t = size_one_node;
  4551. }
  4552. else if (TREE_CODE (expr) == COMPONENT_REF
  4553. && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
  4554. t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
  4555. else if (TREE_CODE (expr) == INDIRECT_REF)
  4556. {
  4557. tree t = TREE_OPERAND (expr, 0);
  4558. tree best = t;
  4559. int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
  4560. while (CONVERT_EXPR_P (t)
  4561. && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
  4562. {
  4563. int thisalign;
  4564. t = TREE_OPERAND (t, 0);
  4565. thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
  4566. if (thisalign > bestalign)
  4567. best = t, bestalign = thisalign;
  4568. }
  4569. return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
  4570. }
  4571. else
  4572. return c_alignof (loc, TREE_TYPE (expr));
  4573. return fold_convert_loc (loc, size_type_node, t);
  4574. }
  4575. /* Handle C and C++ default attributes. */
  4576. enum built_in_attribute
  4577. {
  4578. #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
  4579. #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
  4580. #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
  4581. #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
  4582. #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
  4583. #include "builtin-attrs.def"
  4584. #undef DEF_ATTR_NULL_TREE
  4585. #undef DEF_ATTR_INT
  4586. #undef DEF_ATTR_STRING
  4587. #undef DEF_ATTR_IDENT
  4588. #undef DEF_ATTR_TREE_LIST
  4589. ATTR_LAST
  4590. };
  4591. static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
  4592. static void c_init_attributes (void);
  4593. enum c_builtin_type
  4594. {
  4595. #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
  4596. #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
  4597. #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
  4598. #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
  4599. #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
  4600. #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
  4601. #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
  4602. #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4603. ARG6) NAME,
  4604. #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4605. ARG6, ARG7) NAME,
  4606. #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4607. ARG6, ARG7, ARG8) NAME,
  4608. #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
  4609. #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
  4610. #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
  4611. #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
  4612. #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
  4613. #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
  4614. NAME,
  4615. #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4616. ARG6, ARG7) NAME,
  4617. #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4618. ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
  4619. #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
  4620. #include "builtin-types.def"
  4621. #undef DEF_PRIMITIVE_TYPE
  4622. #undef DEF_FUNCTION_TYPE_0
  4623. #undef DEF_FUNCTION_TYPE_1
  4624. #undef DEF_FUNCTION_TYPE_2
  4625. #undef DEF_FUNCTION_TYPE_3
  4626. #undef DEF_FUNCTION_TYPE_4
  4627. #undef DEF_FUNCTION_TYPE_5
  4628. #undef DEF_FUNCTION_TYPE_6
  4629. #undef DEF_FUNCTION_TYPE_7
  4630. #undef DEF_FUNCTION_TYPE_8
  4631. #undef DEF_FUNCTION_TYPE_VAR_0
  4632. #undef DEF_FUNCTION_TYPE_VAR_1
  4633. #undef DEF_FUNCTION_TYPE_VAR_2
  4634. #undef DEF_FUNCTION_TYPE_VAR_3
  4635. #undef DEF_FUNCTION_TYPE_VAR_4
  4636. #undef DEF_FUNCTION_TYPE_VAR_5
  4637. #undef DEF_FUNCTION_TYPE_VAR_7
  4638. #undef DEF_FUNCTION_TYPE_VAR_11
  4639. #undef DEF_POINTER_TYPE
  4640. BT_LAST
  4641. };
  4642. typedef enum c_builtin_type builtin_type;
  4643. /* A temporary array for c_common_nodes_and_builtins. Used in
  4644. communication with def_fn_type. */
  4645. static tree builtin_types[(int) BT_LAST + 1];
  4646. /* A helper function for c_common_nodes_and_builtins. Build function type
  4647. for DEF with return type RET and N arguments. If VAR is true, then the
  4648. function should be variadic after those N arguments.
  4649. Takes special care not to ICE if any of the types involved are
  4650. error_mark_node, which indicates that said type is not in fact available
  4651. (see builtin_type_for_size). In which case the function type as a whole
  4652. should be error_mark_node. */
  4653. static void
  4654. def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
  4655. {
  4656. tree t;
  4657. tree *args = XALLOCAVEC (tree, n);
  4658. va_list list;
  4659. int i;
  4660. va_start (list, n);
  4661. for (i = 0; i < n; ++i)
  4662. {
  4663. builtin_type a = (builtin_type) va_arg (list, int);
  4664. t = builtin_types[a];
  4665. if (t == error_mark_node)
  4666. goto egress;
  4667. args[i] = t;
  4668. }
  4669. t = builtin_types[ret];
  4670. if (t == error_mark_node)
  4671. goto egress;
  4672. if (var)
  4673. t = build_varargs_function_type_array (t, n, args);
  4674. else
  4675. t = build_function_type_array (t, n, args);
  4676. egress:
  4677. builtin_types[def] = t;
  4678. va_end (list);
  4679. }
  4680. /* Build builtin functions common to both C and C++ language
  4681. frontends. */
  4682. static void
  4683. c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
  4684. {
  4685. #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
  4686. builtin_types[ENUM] = VALUE;
  4687. #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
  4688. def_fn_type (ENUM, RETURN, 0, 0);
  4689. #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
  4690. def_fn_type (ENUM, RETURN, 0, 1, ARG1);
  4691. #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
  4692. def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
  4693. #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
  4694. def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
  4695. #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
  4696. def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
  4697. #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
  4698. def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
  4699. #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4700. ARG6) \
  4701. def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
  4702. #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4703. ARG6, ARG7) \
  4704. def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
  4705. #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4706. ARG6, ARG7, ARG8) \
  4707. def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
  4708. ARG7, ARG8);
  4709. #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
  4710. def_fn_type (ENUM, RETURN, 1, 0);
  4711. #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
  4712. def_fn_type (ENUM, RETURN, 1, 1, ARG1);
  4713. #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
  4714. def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
  4715. #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
  4716. def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
  4717. #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
  4718. def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
  4719. #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
  4720. def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
  4721. #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4722. ARG6, ARG7) \
  4723. def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
  4724. #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
  4725. ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
  4726. def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
  4727. ARG7, ARG8, ARG9, ARG10, ARG11);
  4728. #define DEF_POINTER_TYPE(ENUM, TYPE) \
  4729. builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
  4730. #include "builtin-types.def"
  4731. #undef DEF_PRIMITIVE_TYPE
  4732. #undef DEF_FUNCTION_TYPE_0
  4733. #undef DEF_FUNCTION_TYPE_1
  4734. #undef DEF_FUNCTION_TYPE_2
  4735. #undef DEF_FUNCTION_TYPE_3
  4736. #undef DEF_FUNCTION_TYPE_4
  4737. #undef DEF_FUNCTION_TYPE_5
  4738. #undef DEF_FUNCTION_TYPE_6
  4739. #undef DEF_FUNCTION_TYPE_7
  4740. #undef DEF_FUNCTION_TYPE_8
  4741. #undef DEF_FUNCTION_TYPE_VAR_0
  4742. #undef DEF_FUNCTION_TYPE_VAR_1
  4743. #undef DEF_FUNCTION_TYPE_VAR_2
  4744. #undef DEF_FUNCTION_TYPE_VAR_3
  4745. #undef DEF_FUNCTION_TYPE_VAR_4
  4746. #undef DEF_FUNCTION_TYPE_VAR_5
  4747. #undef DEF_FUNCTION_TYPE_VAR_7
  4748. #undef DEF_FUNCTION_TYPE_VAR_11
  4749. #undef DEF_POINTER_TYPE
  4750. builtin_types[(int) BT_LAST] = NULL_TREE;
  4751. c_init_attributes ();
  4752. #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
  4753. NONANSI_P, ATTRS, IMPLICIT, COND) \
  4754. if (NAME && COND) \
  4755. def_builtin_1 (ENUM, NAME, CLASS, \
  4756. builtin_types[(int) TYPE], \
  4757. builtin_types[(int) LIBTYPE], \
  4758. BOTH_P, FALLBACK_P, NONANSI_P, \
  4759. built_in_attributes[(int) ATTRS], IMPLICIT);
  4760. #include "builtins.def"
  4761. #undef DEF_BUILTIN
  4762. targetm.init_builtins ();
  4763. build_common_builtin_nodes ();
  4764. if (flag_cilkplus)
  4765. cilk_init_builtins ();
  4766. }
  4767. /* Like get_identifier, but avoid warnings about null arguments when
  4768. the argument may be NULL for targets where GCC lacks stdint.h type
  4769. information. */
  4770. static inline tree
  4771. c_get_ident (const char *id)
  4772. {
  4773. return get_identifier (id);
  4774. }
  4775. /* Build tree nodes and builtin functions common to both C and C++ language
  4776. frontends. */
  4777. void
  4778. c_common_nodes_and_builtins (void)
  4779. {
  4780. int char16_type_size;
  4781. int char32_type_size;
  4782. int wchar_type_size;
  4783. tree array_domain_type;
  4784. tree va_list_ref_type_node;
  4785. tree va_list_arg_type_node;
  4786. int i;
  4787. build_common_tree_nodes (flag_signed_char, flag_short_double);
  4788. /* Define `int' and `char' first so that dbx will output them first. */
  4789. record_builtin_type (RID_INT, NULL, integer_type_node);
  4790. record_builtin_type (RID_CHAR, "char", char_type_node);
  4791. /* `signed' is the same as `int'. FIXME: the declarations of "signed",
  4792. "unsigned long", "long long unsigned" and "unsigned short" were in C++
  4793. but not C. Are the conditionals here needed? */
  4794. if (c_dialect_cxx ())
  4795. record_builtin_type (RID_SIGNED, NULL, integer_type_node);
  4796. record_builtin_type (RID_LONG, "long int", long_integer_type_node);
  4797. record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
  4798. record_builtin_type (RID_MAX, "long unsigned int",
  4799. long_unsigned_type_node);
  4800. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  4801. {
  4802. char name[25];
  4803. sprintf (name, "__int%d", int_n_data[i].bitsize);
  4804. record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
  4805. int_n_trees[i].signed_type);
  4806. sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
  4807. record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
  4808. }
  4809. if (c_dialect_cxx ())
  4810. record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
  4811. record_builtin_type (RID_MAX, "long long int",
  4812. long_long_integer_type_node);
  4813. record_builtin_type (RID_MAX, "long long unsigned int",
  4814. long_long_unsigned_type_node);
  4815. if (c_dialect_cxx ())
  4816. record_builtin_type (RID_MAX, "long long unsigned",
  4817. long_long_unsigned_type_node);
  4818. record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
  4819. record_builtin_type (RID_MAX, "short unsigned int",
  4820. short_unsigned_type_node);
  4821. if (c_dialect_cxx ())
  4822. record_builtin_type (RID_MAX, "unsigned short",
  4823. short_unsigned_type_node);
  4824. /* Define both `signed char' and `unsigned char'. */
  4825. record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
  4826. record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
  4827. /* These are types that c_common_type_for_size and
  4828. c_common_type_for_mode use. */
  4829. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4830. TYPE_DECL, NULL_TREE,
  4831. intQI_type_node));
  4832. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4833. TYPE_DECL, NULL_TREE,
  4834. intHI_type_node));
  4835. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4836. TYPE_DECL, NULL_TREE,
  4837. intSI_type_node));
  4838. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4839. TYPE_DECL, NULL_TREE,
  4840. intDI_type_node));
  4841. #if HOST_BITS_PER_WIDE_INT >= 64
  4842. /* Note that this is different than the __int128 type that's part of
  4843. the generic __intN support. */
  4844. if (targetm.scalar_mode_supported_p (TImode))
  4845. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4846. TYPE_DECL,
  4847. get_identifier ("__int128_t"),
  4848. intTI_type_node));
  4849. #endif
  4850. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4851. TYPE_DECL, NULL_TREE,
  4852. unsigned_intQI_type_node));
  4853. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4854. TYPE_DECL, NULL_TREE,
  4855. unsigned_intHI_type_node));
  4856. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4857. TYPE_DECL, NULL_TREE,
  4858. unsigned_intSI_type_node));
  4859. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4860. TYPE_DECL, NULL_TREE,
  4861. unsigned_intDI_type_node));
  4862. #if HOST_BITS_PER_WIDE_INT >= 64
  4863. if (targetm.scalar_mode_supported_p (TImode))
  4864. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4865. TYPE_DECL,
  4866. get_identifier ("__uint128_t"),
  4867. unsigned_intTI_type_node));
  4868. #endif
  4869. /* Create the widest literal types. */
  4870. widest_integer_literal_type_node
  4871. = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
  4872. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4873. TYPE_DECL, NULL_TREE,
  4874. widest_integer_literal_type_node));
  4875. widest_unsigned_literal_type_node
  4876. = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
  4877. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4878. TYPE_DECL, NULL_TREE,
  4879. widest_unsigned_literal_type_node));
  4880. signed_size_type_node = c_common_signed_type (size_type_node);
  4881. pid_type_node =
  4882. TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
  4883. record_builtin_type (RID_FLOAT, NULL, float_type_node);
  4884. record_builtin_type (RID_DOUBLE, NULL, double_type_node);
  4885. record_builtin_type (RID_MAX, "long double", long_double_type_node);
  4886. /* Only supported decimal floating point extension if the target
  4887. actually supports underlying modes. */
  4888. if (targetm.scalar_mode_supported_p (SDmode)
  4889. && targetm.scalar_mode_supported_p (DDmode)
  4890. && targetm.scalar_mode_supported_p (TDmode))
  4891. {
  4892. record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
  4893. record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
  4894. record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
  4895. }
  4896. if (targetm.fixed_point_supported_p ())
  4897. {
  4898. record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
  4899. record_builtin_type (RID_FRACT, NULL, fract_type_node);
  4900. record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
  4901. record_builtin_type (RID_MAX, "long long _Fract",
  4902. long_long_fract_type_node);
  4903. record_builtin_type (RID_MAX, "unsigned short _Fract",
  4904. unsigned_short_fract_type_node);
  4905. record_builtin_type (RID_MAX, "unsigned _Fract",
  4906. unsigned_fract_type_node);
  4907. record_builtin_type (RID_MAX, "unsigned long _Fract",
  4908. unsigned_long_fract_type_node);
  4909. record_builtin_type (RID_MAX, "unsigned long long _Fract",
  4910. unsigned_long_long_fract_type_node);
  4911. record_builtin_type (RID_MAX, "_Sat short _Fract",
  4912. sat_short_fract_type_node);
  4913. record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
  4914. record_builtin_type (RID_MAX, "_Sat long _Fract",
  4915. sat_long_fract_type_node);
  4916. record_builtin_type (RID_MAX, "_Sat long long _Fract",
  4917. sat_long_long_fract_type_node);
  4918. record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
  4919. sat_unsigned_short_fract_type_node);
  4920. record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
  4921. sat_unsigned_fract_type_node);
  4922. record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
  4923. sat_unsigned_long_fract_type_node);
  4924. record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
  4925. sat_unsigned_long_long_fract_type_node);
  4926. record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
  4927. record_builtin_type (RID_ACCUM, NULL, accum_type_node);
  4928. record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
  4929. record_builtin_type (RID_MAX, "long long _Accum",
  4930. long_long_accum_type_node);
  4931. record_builtin_type (RID_MAX, "unsigned short _Accum",
  4932. unsigned_short_accum_type_node);
  4933. record_builtin_type (RID_MAX, "unsigned _Accum",
  4934. unsigned_accum_type_node);
  4935. record_builtin_type (RID_MAX, "unsigned long _Accum",
  4936. unsigned_long_accum_type_node);
  4937. record_builtin_type (RID_MAX, "unsigned long long _Accum",
  4938. unsigned_long_long_accum_type_node);
  4939. record_builtin_type (RID_MAX, "_Sat short _Accum",
  4940. sat_short_accum_type_node);
  4941. record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
  4942. record_builtin_type (RID_MAX, "_Sat long _Accum",
  4943. sat_long_accum_type_node);
  4944. record_builtin_type (RID_MAX, "_Sat long long _Accum",
  4945. sat_long_long_accum_type_node);
  4946. record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
  4947. sat_unsigned_short_accum_type_node);
  4948. record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
  4949. sat_unsigned_accum_type_node);
  4950. record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
  4951. sat_unsigned_long_accum_type_node);
  4952. record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
  4953. sat_unsigned_long_long_accum_type_node);
  4954. }
  4955. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4956. TYPE_DECL,
  4957. get_identifier ("complex int"),
  4958. complex_integer_type_node));
  4959. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4960. TYPE_DECL,
  4961. get_identifier ("complex float"),
  4962. complex_float_type_node));
  4963. lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
  4964. TYPE_DECL,
  4965. get_identifier ("complex double"),
  4966. complex_double_type_node));
  4967. lang_hooks.decls.pushdecl
  4968. (build_decl (UNKNOWN_LOCATION,
  4969. TYPE_DECL, get_identifier ("complex long double"),
  4970. complex_long_double_type_node));
  4971. if (c_dialect_cxx ())
  4972. /* For C++, make fileptr_type_node a distinct void * type until
  4973. FILE type is defined. */
  4974. fileptr_type_node = build_variant_type_copy (ptr_type_node);
  4975. record_builtin_type (RID_VOID, NULL, void_type_node);
  4976. /* Set the TYPE_NAME for any variants that were built before
  4977. record_builtin_type gave names to the built-in types. */
  4978. {
  4979. tree void_name = TYPE_NAME (void_type_node);
  4980. TYPE_NAME (void_type_node) = NULL_TREE;
  4981. TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
  4982. = void_name;
  4983. TYPE_NAME (void_type_node) = void_name;
  4984. }
  4985. void_list_node = build_void_list_node ();
  4986. /* Make a type to be the domain of a few array types
  4987. whose domains don't really matter.
  4988. 200 is small enough that it always fits in size_t
  4989. and large enough that it can hold most function names for the
  4990. initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
  4991. array_domain_type = build_index_type (size_int (200));
  4992. /* Make a type for arrays of characters.
  4993. With luck nothing will ever really depend on the length of this
  4994. array type. */
  4995. char_array_type_node
  4996. = build_array_type (char_type_node, array_domain_type);
  4997. string_type_node = build_pointer_type (char_type_node);
  4998. const_string_type_node
  4999. = build_pointer_type (build_qualified_type
  5000. (char_type_node, TYPE_QUAL_CONST));
  5001. /* This is special for C++ so functions can be overloaded. */
  5002. wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
  5003. wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
  5004. wchar_type_size = TYPE_PRECISION (wchar_type_node);
  5005. underlying_wchar_type_node = wchar_type_node;
  5006. if (c_dialect_cxx ())
  5007. {
  5008. if (TYPE_UNSIGNED (wchar_type_node))
  5009. wchar_type_node = make_unsigned_type (wchar_type_size);
  5010. else
  5011. wchar_type_node = make_signed_type (wchar_type_size);
  5012. record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
  5013. }
  5014. /* This is for wide string constants. */
  5015. wchar_array_type_node
  5016. = build_array_type (wchar_type_node, array_domain_type);
  5017. /* Define 'char16_t'. */
  5018. char16_type_node = get_identifier (CHAR16_TYPE);
  5019. char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
  5020. char16_type_size = TYPE_PRECISION (char16_type_node);
  5021. if (c_dialect_cxx ())
  5022. {
  5023. char16_type_node = make_unsigned_type (char16_type_size);
  5024. if (cxx_dialect >= cxx11)
  5025. record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
  5026. }
  5027. /* This is for UTF-16 string constants. */
  5028. char16_array_type_node
  5029. = build_array_type (char16_type_node, array_domain_type);
  5030. /* Define 'char32_t'. */
  5031. char32_type_node = get_identifier (CHAR32_TYPE);
  5032. char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
  5033. char32_type_size = TYPE_PRECISION (char32_type_node);
  5034. if (c_dialect_cxx ())
  5035. {
  5036. char32_type_node = make_unsigned_type (char32_type_size);
  5037. if (cxx_dialect >= cxx11)
  5038. record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
  5039. }
  5040. /* This is for UTF-32 string constants. */
  5041. char32_array_type_node
  5042. = build_array_type (char32_type_node, array_domain_type);
  5043. wint_type_node =
  5044. TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
  5045. intmax_type_node =
  5046. TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
  5047. uintmax_type_node =
  5048. TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
  5049. if (SIG_ATOMIC_TYPE)
  5050. sig_atomic_type_node =
  5051. TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
  5052. if (INT8_TYPE)
  5053. int8_type_node =
  5054. TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
  5055. if (INT16_TYPE)
  5056. int16_type_node =
  5057. TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
  5058. if (INT32_TYPE)
  5059. int32_type_node =
  5060. TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
  5061. if (INT64_TYPE)
  5062. int64_type_node =
  5063. TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
  5064. if (UINT8_TYPE)
  5065. uint8_type_node =
  5066. TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
  5067. if (UINT16_TYPE)
  5068. c_uint16_type_node = uint16_type_node =
  5069. TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
  5070. if (UINT32_TYPE)
  5071. c_uint32_type_node = uint32_type_node =
  5072. TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
  5073. if (UINT64_TYPE)
  5074. c_uint64_type_node = uint64_type_node =
  5075. TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
  5076. if (INT_LEAST8_TYPE)
  5077. int_least8_type_node =
  5078. TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
  5079. if (INT_LEAST16_TYPE)
  5080. int_least16_type_node =
  5081. TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
  5082. if (INT_LEAST32_TYPE)
  5083. int_least32_type_node =
  5084. TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
  5085. if (INT_LEAST64_TYPE)
  5086. int_least64_type_node =
  5087. TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
  5088. if (UINT_LEAST8_TYPE)
  5089. uint_least8_type_node =
  5090. TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
  5091. if (UINT_LEAST16_TYPE)
  5092. uint_least16_type_node =
  5093. TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
  5094. if (UINT_LEAST32_TYPE)
  5095. uint_least32_type_node =
  5096. TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
  5097. if (UINT_LEAST64_TYPE)
  5098. uint_least64_type_node =
  5099. TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
  5100. if (INT_FAST8_TYPE)
  5101. int_fast8_type_node =
  5102. TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
  5103. if (INT_FAST16_TYPE)
  5104. int_fast16_type_node =
  5105. TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
  5106. if (INT_FAST32_TYPE)
  5107. int_fast32_type_node =
  5108. TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
  5109. if (INT_FAST64_TYPE)
  5110. int_fast64_type_node =
  5111. TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
  5112. if (UINT_FAST8_TYPE)
  5113. uint_fast8_type_node =
  5114. TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
  5115. if (UINT_FAST16_TYPE)
  5116. uint_fast16_type_node =
  5117. TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
  5118. if (UINT_FAST32_TYPE)
  5119. uint_fast32_type_node =
  5120. TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
  5121. if (UINT_FAST64_TYPE)
  5122. uint_fast64_type_node =
  5123. TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
  5124. if (INTPTR_TYPE)
  5125. intptr_type_node =
  5126. TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
  5127. if (UINTPTR_TYPE)
  5128. uintptr_type_node =
  5129. TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
  5130. default_function_type
  5131. = build_varargs_function_type_list (integer_type_node, NULL_TREE);
  5132. ptrdiff_type_node
  5133. = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
  5134. unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
  5135. lang_hooks.decls.pushdecl
  5136. (build_decl (UNKNOWN_LOCATION,
  5137. TYPE_DECL, get_identifier ("__builtin_va_list"),
  5138. va_list_type_node));
  5139. if (targetm.enum_va_list_p)
  5140. {
  5141. int l;
  5142. const char *pname;
  5143. tree ptype;
  5144. for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
  5145. {
  5146. lang_hooks.decls.pushdecl
  5147. (build_decl (UNKNOWN_LOCATION,
  5148. TYPE_DECL, get_identifier (pname),
  5149. ptype));
  5150. }
  5151. }
  5152. if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
  5153. {
  5154. va_list_arg_type_node = va_list_ref_type_node =
  5155. build_pointer_type (TREE_TYPE (va_list_type_node));
  5156. }
  5157. else
  5158. {
  5159. va_list_arg_type_node = va_list_type_node;
  5160. va_list_ref_type_node = build_reference_type (va_list_type_node);
  5161. }
  5162. if (!flag_preprocess_only)
  5163. c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
  5164. main_identifier_node = get_identifier ("main");
  5165. /* Create the built-in __null node. It is important that this is
  5166. not shared. */
  5167. null_node = make_int_cst (1, 1);
  5168. TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
  5169. /* Since builtin_types isn't gc'ed, don't export these nodes. */
  5170. memset (builtin_types, 0, sizeof (builtin_types));
  5171. }
  5172. /* The number of named compound-literals generated thus far. */
  5173. static GTY(()) int compound_literal_number;
  5174. /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
  5175. void
  5176. set_compound_literal_name (tree decl)
  5177. {
  5178. char *name;
  5179. ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
  5180. compound_literal_number);
  5181. compound_literal_number++;
  5182. DECL_NAME (decl) = get_identifier (name);
  5183. }
  5184. tree
  5185. build_va_arg (location_t loc, tree expr, tree type)
  5186. {
  5187. expr = build1 (VA_ARG_EXPR, type, expr);
  5188. SET_EXPR_LOCATION (expr, loc);
  5189. return expr;
  5190. }
  5191. /* Linked list of disabled built-in functions. */
  5192. typedef struct disabled_builtin
  5193. {
  5194. const char *name;
  5195. struct disabled_builtin *next;
  5196. } disabled_builtin;
  5197. static disabled_builtin *disabled_builtins = NULL;
  5198. static bool builtin_function_disabled_p (const char *);
  5199. /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
  5200. begins with "__builtin_", give an error. */
  5201. void
  5202. disable_builtin_function (const char *name)
  5203. {
  5204. if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
  5205. error ("cannot disable built-in function %qs", name);
  5206. else
  5207. {
  5208. disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
  5209. new_disabled_builtin->name = name;
  5210. new_disabled_builtin->next = disabled_builtins;
  5211. disabled_builtins = new_disabled_builtin;
  5212. }
  5213. }
  5214. /* Return true if the built-in function NAME has been disabled, false
  5215. otherwise. */
  5216. static bool
  5217. builtin_function_disabled_p (const char *name)
  5218. {
  5219. disabled_builtin *p;
  5220. for (p = disabled_builtins; p != NULL; p = p->next)
  5221. {
  5222. if (strcmp (name, p->name) == 0)
  5223. return true;
  5224. }
  5225. return false;
  5226. }
  5227. /* Worker for DEF_BUILTIN.
  5228. Possibly define a builtin function with one or two names.
  5229. Does not declare a non-__builtin_ function if flag_no_builtin, or if
  5230. nonansi_p and flag_no_nonansi_builtin. */
  5231. static void
  5232. def_builtin_1 (enum built_in_function fncode,
  5233. const char *name,
  5234. enum built_in_class fnclass,
  5235. tree fntype, tree libtype,
  5236. bool both_p, bool fallback_p, bool nonansi_p,
  5237. tree fnattrs, bool implicit_p)
  5238. {
  5239. tree decl;
  5240. const char *libname;
  5241. if (fntype == error_mark_node)
  5242. return;
  5243. gcc_assert ((!both_p && !fallback_p)
  5244. || !strncmp (name, "__builtin_",
  5245. strlen ("__builtin_")));
  5246. libname = name + strlen ("__builtin_");
  5247. decl = add_builtin_function (name, fntype, fncode, fnclass,
  5248. (fallback_p ? libname : NULL),
  5249. fnattrs);
  5250. set_builtin_decl (fncode, decl, implicit_p);
  5251. if (both_p
  5252. && !flag_no_builtin && !builtin_function_disabled_p (libname)
  5253. && !(nonansi_p && flag_no_nonansi_builtin))
  5254. add_builtin_function (libname, libtype, fncode, fnclass,
  5255. NULL, fnattrs);
  5256. }
  5257. /* Nonzero if the type T promotes to int. This is (nearly) the
  5258. integral promotions defined in ISO C99 6.3.1.1/2. */
  5259. bool
  5260. c_promoting_integer_type_p (const_tree t)
  5261. {
  5262. switch (TREE_CODE (t))
  5263. {
  5264. case INTEGER_TYPE:
  5265. return (TYPE_MAIN_VARIANT (t) == char_type_node
  5266. || TYPE_MAIN_VARIANT (t) == signed_char_type_node
  5267. || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
  5268. || TYPE_MAIN_VARIANT (t) == short_integer_type_node
  5269. || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
  5270. || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
  5271. case ENUMERAL_TYPE:
  5272. /* ??? Technically all enumerations not larger than an int
  5273. promote to an int. But this is used along code paths
  5274. that only want to notice a size change. */
  5275. return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
  5276. case BOOLEAN_TYPE:
  5277. return 1;
  5278. default:
  5279. return 0;
  5280. }
  5281. }
  5282. /* Return 1 if PARMS specifies a fixed number of parameters
  5283. and none of their types is affected by default promotions. */
  5284. int
  5285. self_promoting_args_p (const_tree parms)
  5286. {
  5287. const_tree t;
  5288. for (t = parms; t; t = TREE_CHAIN (t))
  5289. {
  5290. tree type = TREE_VALUE (t);
  5291. if (type == error_mark_node)
  5292. continue;
  5293. if (TREE_CHAIN (t) == 0 && type != void_type_node)
  5294. return 0;
  5295. if (type == 0)
  5296. return 0;
  5297. if (TYPE_MAIN_VARIANT (type) == float_type_node)
  5298. return 0;
  5299. if (c_promoting_integer_type_p (type))
  5300. return 0;
  5301. }
  5302. return 1;
  5303. }
  5304. /* Recursively remove any '*' or '&' operator from TYPE. */
  5305. tree
  5306. strip_pointer_operator (tree t)
  5307. {
  5308. while (POINTER_TYPE_P (t))
  5309. t = TREE_TYPE (t);
  5310. return t;
  5311. }
  5312. /* Recursively remove pointer or array type from TYPE. */
  5313. tree
  5314. strip_pointer_or_array_types (tree t)
  5315. {
  5316. while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
  5317. t = TREE_TYPE (t);
  5318. return t;
  5319. }
  5320. /* Used to compare case labels. K1 and K2 are actually tree nodes
  5321. representing case labels, or NULL_TREE for a `default' label.
  5322. Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
  5323. K2, and 0 if K1 and K2 are equal. */
  5324. int
  5325. case_compare (splay_tree_key k1, splay_tree_key k2)
  5326. {
  5327. /* Consider a NULL key (such as arises with a `default' label) to be
  5328. smaller than anything else. */
  5329. if (!k1)
  5330. return k2 ? -1 : 0;
  5331. else if (!k2)
  5332. return k1 ? 1 : 0;
  5333. return tree_int_cst_compare ((tree) k1, (tree) k2);
  5334. }
  5335. /* Process a case label, located at LOC, for the range LOW_VALUE
  5336. ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
  5337. then this case label is actually a `default' label. If only
  5338. HIGH_VALUE is NULL_TREE, then case label was declared using the
  5339. usual C/C++ syntax, rather than the GNU case range extension.
  5340. CASES is a tree containing all the case ranges processed so far;
  5341. COND is the condition for the switch-statement itself. Returns the
  5342. CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
  5343. is created. */
  5344. tree
  5345. c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
  5346. tree low_value, tree high_value)
  5347. {
  5348. tree type;
  5349. tree label;
  5350. tree case_label;
  5351. splay_tree_node node;
  5352. /* Create the LABEL_DECL itself. */
  5353. label = create_artificial_label (loc);
  5354. /* If there was an error processing the switch condition, bail now
  5355. before we get more confused. */
  5356. if (!cond || cond == error_mark_node)
  5357. goto error_out;
  5358. if ((low_value && TREE_TYPE (low_value)
  5359. && POINTER_TYPE_P (TREE_TYPE (low_value)))
  5360. || (high_value && TREE_TYPE (high_value)
  5361. && POINTER_TYPE_P (TREE_TYPE (high_value))))
  5362. {
  5363. error_at (loc, "pointers are not permitted as case values");
  5364. goto error_out;
  5365. }
  5366. /* Case ranges are a GNU extension. */
  5367. if (high_value)
  5368. pedwarn (loc, OPT_Wpedantic,
  5369. "range expressions in switch statements are non-standard");
  5370. type = TREE_TYPE (cond);
  5371. if (low_value)
  5372. {
  5373. low_value = check_case_value (loc, low_value);
  5374. low_value = convert_and_check (loc, type, low_value);
  5375. if (low_value == error_mark_node)
  5376. goto error_out;
  5377. }
  5378. if (high_value)
  5379. {
  5380. high_value = check_case_value (loc, high_value);
  5381. high_value = convert_and_check (loc, type, high_value);
  5382. if (high_value == error_mark_node)
  5383. goto error_out;
  5384. }
  5385. if (low_value && high_value)
  5386. {
  5387. /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
  5388. really a case range, even though it was written that way.
  5389. Remove the HIGH_VALUE to simplify later processing. */
  5390. if (tree_int_cst_equal (low_value, high_value))
  5391. high_value = NULL_TREE;
  5392. else if (!tree_int_cst_lt (low_value, high_value))
  5393. warning_at (loc, 0, "empty range specified");
  5394. }
  5395. /* See if the case is in range of the type of the original testing
  5396. expression. If both low_value and high_value are out of range,
  5397. don't insert the case label and return NULL_TREE. */
  5398. if (low_value
  5399. && !check_case_bounds (loc, type, orig_type,
  5400. &low_value, high_value ? &high_value : NULL))
  5401. return NULL_TREE;
  5402. /* Look up the LOW_VALUE in the table of case labels we already
  5403. have. */
  5404. node = splay_tree_lookup (cases, (splay_tree_key) low_value);
  5405. /* If there was not an exact match, check for overlapping ranges.
  5406. There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
  5407. that's a `default' label and the only overlap is an exact match. */
  5408. if (!node && (low_value || high_value))
  5409. {
  5410. splay_tree_node low_bound;
  5411. splay_tree_node high_bound;
  5412. /* Even though there wasn't an exact match, there might be an
  5413. overlap between this case range and another case range.
  5414. Since we've (inductively) not allowed any overlapping case
  5415. ranges, we simply need to find the greatest low case label
  5416. that is smaller that LOW_VALUE, and the smallest low case
  5417. label that is greater than LOW_VALUE. If there is an overlap
  5418. it will occur in one of these two ranges. */
  5419. low_bound = splay_tree_predecessor (cases,
  5420. (splay_tree_key) low_value);
  5421. high_bound = splay_tree_successor (cases,
  5422. (splay_tree_key) low_value);
  5423. /* Check to see if the LOW_BOUND overlaps. It is smaller than
  5424. the LOW_VALUE, so there is no need to check unless the
  5425. LOW_BOUND is in fact itself a case range. */
  5426. if (low_bound
  5427. && CASE_HIGH ((tree) low_bound->value)
  5428. && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
  5429. low_value) >= 0)
  5430. node = low_bound;
  5431. /* Check to see if the HIGH_BOUND overlaps. The low end of that
  5432. range is bigger than the low end of the current range, so we
  5433. are only interested if the current range is a real range, and
  5434. not an ordinary case label. */
  5435. else if (high_bound
  5436. && high_value
  5437. && (tree_int_cst_compare ((tree) high_bound->key,
  5438. high_value)
  5439. <= 0))
  5440. node = high_bound;
  5441. }
  5442. /* If there was an overlap, issue an error. */
  5443. if (node)
  5444. {
  5445. tree duplicate = CASE_LABEL ((tree) node->value);
  5446. if (high_value)
  5447. {
  5448. error_at (loc, "duplicate (or overlapping) case value");
  5449. error_at (DECL_SOURCE_LOCATION (duplicate),
  5450. "this is the first entry overlapping that value");
  5451. }
  5452. else if (low_value)
  5453. {
  5454. error_at (loc, "duplicate case value") ;
  5455. error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
  5456. }
  5457. else
  5458. {
  5459. error_at (loc, "multiple default labels in one switch");
  5460. error_at (DECL_SOURCE_LOCATION (duplicate),
  5461. "this is the first default label");
  5462. }
  5463. goto error_out;
  5464. }
  5465. /* Add a CASE_LABEL to the statement-tree. */
  5466. case_label = add_stmt (build_case_label (low_value, high_value, label));
  5467. /* Register this case label in the splay tree. */
  5468. splay_tree_insert (cases,
  5469. (splay_tree_key) low_value,
  5470. (splay_tree_value) case_label);
  5471. return case_label;
  5472. error_out:
  5473. /* Add a label so that the back-end doesn't think that the beginning of
  5474. the switch is unreachable. Note that we do not add a case label, as
  5475. that just leads to duplicates and thence to failure later on. */
  5476. if (!cases->root)
  5477. {
  5478. tree t = create_artificial_label (loc);
  5479. add_stmt (build_stmt (loc, LABEL_EXPR, t));
  5480. }
  5481. return error_mark_node;
  5482. }
  5483. /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
  5484. Used to verify that case values match up with enumerator values. */
  5485. static void
  5486. match_case_to_enum_1 (tree key, tree type, tree label)
  5487. {
  5488. char buf[WIDE_INT_PRINT_BUFFER_SIZE];
  5489. if (tree_fits_uhwi_p (key))
  5490. print_dec (key, buf, UNSIGNED);
  5491. else if (tree_fits_shwi_p (key))
  5492. print_dec (key, buf, SIGNED);
  5493. else
  5494. print_hex (key, buf);
  5495. if (TYPE_NAME (type) == 0)
  5496. warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
  5497. warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
  5498. "case value %qs not in enumerated type",
  5499. buf);
  5500. else
  5501. warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
  5502. warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
  5503. "case value %qs not in enumerated type %qT",
  5504. buf, type);
  5505. }
  5506. /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
  5507. Used to verify that case values match up with enumerator values. */
  5508. static int
  5509. match_case_to_enum (splay_tree_node node, void *data)
  5510. {
  5511. tree label = (tree) node->value;
  5512. tree type = (tree) data;
  5513. /* Skip default case. */
  5514. if (!CASE_LOW (label))
  5515. return 0;
  5516. /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
  5517. when we did our enum->case scan. Reset our scratch bit after. */
  5518. if (!CASE_LOW_SEEN (label))
  5519. match_case_to_enum_1 (CASE_LOW (label), type, label);
  5520. else
  5521. CASE_LOW_SEEN (label) = 0;
  5522. /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
  5523. not set, that means that CASE_HIGH did not appear when we did our
  5524. enum->case scan. Reset our scratch bit after. */
  5525. if (CASE_HIGH (label))
  5526. {
  5527. if (!CASE_HIGH_SEEN (label))
  5528. match_case_to_enum_1 (CASE_HIGH (label), type, label);
  5529. else
  5530. CASE_HIGH_SEEN (label) = 0;
  5531. }
  5532. return 0;
  5533. }
  5534. /* Handle -Wswitch*. Called from the front end after parsing the
  5535. switch construct. */
  5536. /* ??? Should probably be somewhere generic, since other languages
  5537. besides C and C++ would want this. At the moment, however, C/C++
  5538. are the only tree-ssa languages that support enumerations at all,
  5539. so the point is moot. */
  5540. void
  5541. c_do_switch_warnings (splay_tree cases, location_t switch_location,
  5542. tree type, tree cond)
  5543. {
  5544. splay_tree_node default_node;
  5545. splay_tree_node node;
  5546. tree chain;
  5547. if (!warn_switch && !warn_switch_enum && !warn_switch_default)
  5548. return;
  5549. default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
  5550. if (!default_node)
  5551. warning_at (switch_location, OPT_Wswitch_default,
  5552. "switch missing default case");
  5553. /* From here on, we only care about about enumerated types. */
  5554. if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
  5555. return;
  5556. /* From here on, we only care about -Wswitch and -Wswitch-enum. */
  5557. if (!warn_switch_enum && !warn_switch)
  5558. return;
  5559. /* Check the cases. Warn about case values which are not members of
  5560. the enumerated type. For -Wswitch-enum, or for -Wswitch when
  5561. there is no default case, check that exactly all enumeration
  5562. literals are covered by the cases. */
  5563. /* Clearing COND if it is not an integer constant simplifies
  5564. the tests inside the loop below. */
  5565. if (TREE_CODE (cond) != INTEGER_CST)
  5566. cond = NULL_TREE;
  5567. /* The time complexity here is O(N*lg(N)) worst case, but for the
  5568. common case of monotonically increasing enumerators, it is
  5569. O(N), since the nature of the splay tree will keep the next
  5570. element adjacent to the root at all times. */
  5571. for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
  5572. {
  5573. tree value = TREE_VALUE (chain);
  5574. if (TREE_CODE (value) == CONST_DECL)
  5575. value = DECL_INITIAL (value);
  5576. node = splay_tree_lookup (cases, (splay_tree_key) value);
  5577. if (node)
  5578. {
  5579. /* Mark the CASE_LOW part of the case entry as seen. */
  5580. tree label = (tree) node->value;
  5581. CASE_LOW_SEEN (label) = 1;
  5582. continue;
  5583. }
  5584. /* Even though there wasn't an exact match, there might be a
  5585. case range which includes the enumerator's value. */
  5586. node = splay_tree_predecessor (cases, (splay_tree_key) value);
  5587. if (node && CASE_HIGH ((tree) node->value))
  5588. {
  5589. tree label = (tree) node->value;
  5590. int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
  5591. if (cmp >= 0)
  5592. {
  5593. /* If we match the upper bound exactly, mark the CASE_HIGH
  5594. part of the case entry as seen. */
  5595. if (cmp == 0)
  5596. CASE_HIGH_SEEN (label) = 1;
  5597. continue;
  5598. }
  5599. }
  5600. /* We've now determined that this enumerated literal isn't
  5601. handled by the case labels of the switch statement. */
  5602. /* If the switch expression is a constant, we only really care
  5603. about whether that constant is handled by the switch. */
  5604. if (cond && tree_int_cst_compare (cond, value))
  5605. continue;
  5606. /* If there is a default_node, the only relevant option is
  5607. Wswitch-enum. Otherwise, if both are enabled then we prefer
  5608. to warn using -Wswitch because -Wswitch is enabled by -Wall
  5609. while -Wswitch-enum is explicit. */
  5610. warning_at (switch_location,
  5611. (default_node || !warn_switch
  5612. ? OPT_Wswitch_enum
  5613. : OPT_Wswitch),
  5614. "enumeration value %qE not handled in switch",
  5615. TREE_PURPOSE (chain));
  5616. }
  5617. /* Warn if there are case expressions that don't correspond to
  5618. enumerators. This can occur since C and C++ don't enforce
  5619. type-checking of assignments to enumeration variables.
  5620. The time complexity here is now always O(N) worst case, since
  5621. we should have marked both the lower bound and upper bound of
  5622. every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
  5623. above. This scan also resets those fields. */
  5624. splay_tree_foreach (cases, match_case_to_enum, type);
  5625. }
  5626. /* Finish an expression taking the address of LABEL (an
  5627. IDENTIFIER_NODE). Returns an expression for the address.
  5628. LOC is the location for the expression returned. */
  5629. tree
  5630. finish_label_address_expr (tree label, location_t loc)
  5631. {
  5632. tree result;
  5633. pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
  5634. if (label == error_mark_node)
  5635. return error_mark_node;
  5636. label = lookup_label (label);
  5637. if (label == NULL_TREE)
  5638. result = null_pointer_node;
  5639. else
  5640. {
  5641. TREE_USED (label) = 1;
  5642. result = build1 (ADDR_EXPR, ptr_type_node, label);
  5643. /* The current function is not necessarily uninlinable.
  5644. Computed gotos are incompatible with inlining, but the value
  5645. here could be used only in a diagnostic, for example. */
  5646. protected_set_expr_location (result, loc);
  5647. }
  5648. return result;
  5649. }
  5650. /* Given a boolean expression ARG, return a tree representing an increment
  5651. or decrement (as indicated by CODE) of ARG. The front end must check for
  5652. invalid cases (e.g., decrement in C++). */
  5653. tree
  5654. boolean_increment (enum tree_code code, tree arg)
  5655. {
  5656. tree val;
  5657. tree true_res = build_int_cst (TREE_TYPE (arg), 1);
  5658. arg = stabilize_reference (arg);
  5659. switch (code)
  5660. {
  5661. case PREINCREMENT_EXPR:
  5662. val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
  5663. break;
  5664. case POSTINCREMENT_EXPR:
  5665. val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
  5666. arg = save_expr (arg);
  5667. val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
  5668. val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
  5669. break;
  5670. case PREDECREMENT_EXPR:
  5671. val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
  5672. invert_truthvalue_loc (input_location, arg));
  5673. break;
  5674. case POSTDECREMENT_EXPR:
  5675. val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
  5676. invert_truthvalue_loc (input_location, arg));
  5677. arg = save_expr (arg);
  5678. val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
  5679. val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
  5680. break;
  5681. default:
  5682. gcc_unreachable ();
  5683. }
  5684. TREE_SIDE_EFFECTS (val) = 1;
  5685. return val;
  5686. }
  5687. /* Built-in macros for stddef.h and stdint.h, that require macros
  5688. defined in this file. */
  5689. void
  5690. c_stddef_cpp_builtins(void)
  5691. {
  5692. builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
  5693. builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
  5694. builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
  5695. builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
  5696. builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
  5697. builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
  5698. builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
  5699. builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
  5700. if (SIG_ATOMIC_TYPE)
  5701. builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
  5702. if (INT8_TYPE)
  5703. builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
  5704. if (INT16_TYPE)
  5705. builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
  5706. if (INT32_TYPE)
  5707. builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
  5708. if (INT64_TYPE)
  5709. builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
  5710. if (UINT8_TYPE)
  5711. builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
  5712. if (UINT16_TYPE)
  5713. builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
  5714. if (UINT32_TYPE)
  5715. builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
  5716. if (UINT64_TYPE)
  5717. builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
  5718. if (INT_LEAST8_TYPE)
  5719. builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
  5720. if (INT_LEAST16_TYPE)
  5721. builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
  5722. if (INT_LEAST32_TYPE)
  5723. builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
  5724. if (INT_LEAST64_TYPE)
  5725. builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
  5726. if (UINT_LEAST8_TYPE)
  5727. builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
  5728. if (UINT_LEAST16_TYPE)
  5729. builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
  5730. if (UINT_LEAST32_TYPE)
  5731. builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
  5732. if (UINT_LEAST64_TYPE)
  5733. builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
  5734. if (INT_FAST8_TYPE)
  5735. builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
  5736. if (INT_FAST16_TYPE)
  5737. builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
  5738. if (INT_FAST32_TYPE)
  5739. builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
  5740. if (INT_FAST64_TYPE)
  5741. builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
  5742. if (UINT_FAST8_TYPE)
  5743. builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
  5744. if (UINT_FAST16_TYPE)
  5745. builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
  5746. if (UINT_FAST32_TYPE)
  5747. builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
  5748. if (UINT_FAST64_TYPE)
  5749. builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
  5750. if (INTPTR_TYPE)
  5751. builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
  5752. if (UINTPTR_TYPE)
  5753. builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
  5754. }
  5755. static void
  5756. c_init_attributes (void)
  5757. {
  5758. /* Fill in the built_in_attributes array. */
  5759. #define DEF_ATTR_NULL_TREE(ENUM) \
  5760. built_in_attributes[(int) ENUM] = NULL_TREE;
  5761. #define DEF_ATTR_INT(ENUM, VALUE) \
  5762. built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
  5763. #define DEF_ATTR_STRING(ENUM, VALUE) \
  5764. built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
  5765. #define DEF_ATTR_IDENT(ENUM, STRING) \
  5766. built_in_attributes[(int) ENUM] = get_identifier (STRING);
  5767. #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
  5768. built_in_attributes[(int) ENUM] \
  5769. = tree_cons (built_in_attributes[(int) PURPOSE], \
  5770. built_in_attributes[(int) VALUE], \
  5771. built_in_attributes[(int) CHAIN]);
  5772. #include "builtin-attrs.def"
  5773. #undef DEF_ATTR_NULL_TREE
  5774. #undef DEF_ATTR_INT
  5775. #undef DEF_ATTR_IDENT
  5776. #undef DEF_ATTR_TREE_LIST
  5777. }
  5778. /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
  5779. identifier as an argument, so the front end shouldn't look it up. */
  5780. bool
  5781. attribute_takes_identifier_p (const_tree attr_id)
  5782. {
  5783. const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
  5784. if (spec == NULL)
  5785. /* Unknown attribute that we'll end up ignoring, return true so we
  5786. don't complain about an identifier argument. */
  5787. return true;
  5788. else if (!strcmp ("mode", spec->name)
  5789. || !strcmp ("format", spec->name)
  5790. || !strcmp ("cleanup", spec->name))
  5791. return true;
  5792. else
  5793. return targetm.attribute_takes_identifier_p (attr_id);
  5794. }
  5795. /* Attribute handlers common to C front ends. */
  5796. /* Handle a "packed" attribute; arguments as in
  5797. struct attribute_spec.handler. */
  5798. static tree
  5799. handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  5800. int flags, bool *no_add_attrs)
  5801. {
  5802. if (TYPE_P (*node))
  5803. {
  5804. if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
  5805. *node = build_variant_type_copy (*node);
  5806. TYPE_PACKED (*node) = 1;
  5807. }
  5808. else if (TREE_CODE (*node) == FIELD_DECL)
  5809. {
  5810. if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
  5811. /* Still pack bitfields. */
  5812. && ! DECL_INITIAL (*node))
  5813. warning (OPT_Wattributes,
  5814. "%qE attribute ignored for field of type %qT",
  5815. name, TREE_TYPE (*node));
  5816. else
  5817. DECL_PACKED (*node) = 1;
  5818. }
  5819. /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
  5820. used for DECL_REGISTER. It wouldn't mean anything anyway.
  5821. We can't set DECL_PACKED on the type of a TYPE_DECL, because
  5822. that changes what the typedef is typing. */
  5823. else
  5824. {
  5825. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5826. *no_add_attrs = true;
  5827. }
  5828. return NULL_TREE;
  5829. }
  5830. /* Handle a "nocommon" attribute; arguments as in
  5831. struct attribute_spec.handler. */
  5832. static tree
  5833. handle_nocommon_attribute (tree *node, tree name,
  5834. tree ARG_UNUSED (args),
  5835. int ARG_UNUSED (flags), bool *no_add_attrs)
  5836. {
  5837. if (TREE_CODE (*node) == VAR_DECL)
  5838. DECL_COMMON (*node) = 0;
  5839. else
  5840. {
  5841. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5842. *no_add_attrs = true;
  5843. }
  5844. return NULL_TREE;
  5845. }
  5846. /* Handle a "common" attribute; arguments as in
  5847. struct attribute_spec.handler. */
  5848. static tree
  5849. handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  5850. int ARG_UNUSED (flags), bool *no_add_attrs)
  5851. {
  5852. if (TREE_CODE (*node) == VAR_DECL)
  5853. DECL_COMMON (*node) = 1;
  5854. else
  5855. {
  5856. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5857. *no_add_attrs = true;
  5858. }
  5859. return NULL_TREE;
  5860. }
  5861. /* Handle a "noreturn" attribute; arguments as in
  5862. struct attribute_spec.handler. */
  5863. static tree
  5864. handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  5865. int ARG_UNUSED (flags), bool *no_add_attrs)
  5866. {
  5867. tree type = TREE_TYPE (*node);
  5868. /* See FIXME comment in c_common_attribute_table. */
  5869. if (TREE_CODE (*node) == FUNCTION_DECL
  5870. || objc_method_decl (TREE_CODE (*node)))
  5871. TREE_THIS_VOLATILE (*node) = 1;
  5872. else if (TREE_CODE (type) == POINTER_TYPE
  5873. && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
  5874. TREE_TYPE (*node)
  5875. = (build_qualified_type
  5876. (build_pointer_type
  5877. (build_type_variant (TREE_TYPE (type),
  5878. TYPE_READONLY (TREE_TYPE (type)), 1)),
  5879. TYPE_QUALS (type)));
  5880. else
  5881. {
  5882. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5883. *no_add_attrs = true;
  5884. }
  5885. return NULL_TREE;
  5886. }
  5887. /* Handle a "hot" and attribute; arguments as in
  5888. struct attribute_spec.handler. */
  5889. static tree
  5890. handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  5891. int ARG_UNUSED (flags), bool *no_add_attrs)
  5892. {
  5893. if (TREE_CODE (*node) == FUNCTION_DECL
  5894. || TREE_CODE (*node) == LABEL_DECL)
  5895. {
  5896. if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
  5897. {
  5898. warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
  5899. "with attribute %qs", name, "cold");
  5900. *no_add_attrs = true;
  5901. }
  5902. /* Most of the rest of the hot processing is done later with
  5903. lookup_attribute. */
  5904. }
  5905. else
  5906. {
  5907. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5908. *no_add_attrs = true;
  5909. }
  5910. return NULL_TREE;
  5911. }
  5912. /* Handle a "cold" and attribute; arguments as in
  5913. struct attribute_spec.handler. */
  5914. static tree
  5915. handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  5916. int ARG_UNUSED (flags), bool *no_add_attrs)
  5917. {
  5918. if (TREE_CODE (*node) == FUNCTION_DECL
  5919. || TREE_CODE (*node) == LABEL_DECL)
  5920. {
  5921. if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
  5922. {
  5923. warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
  5924. "with attribute %qs", name, "hot");
  5925. *no_add_attrs = true;
  5926. }
  5927. /* Most of the rest of the cold processing is done later with
  5928. lookup_attribute. */
  5929. }
  5930. else
  5931. {
  5932. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5933. *no_add_attrs = true;
  5934. }
  5935. return NULL_TREE;
  5936. }
  5937. /* Handle a "no_sanitize_address" attribute; arguments as in
  5938. struct attribute_spec.handler. */
  5939. static tree
  5940. handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
  5941. bool *no_add_attrs)
  5942. {
  5943. if (TREE_CODE (*node) != FUNCTION_DECL)
  5944. {
  5945. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5946. *no_add_attrs = true;
  5947. }
  5948. return NULL_TREE;
  5949. }
  5950. /* Handle a "no_address_safety_analysis" attribute; arguments as in
  5951. struct attribute_spec.handler. */
  5952. static tree
  5953. handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
  5954. bool *no_add_attrs)
  5955. {
  5956. if (TREE_CODE (*node) != FUNCTION_DECL)
  5957. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5958. else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
  5959. DECL_ATTRIBUTES (*node)
  5960. = tree_cons (get_identifier ("no_sanitize_address"),
  5961. NULL_TREE, DECL_ATTRIBUTES (*node));
  5962. *no_add_attrs = true;
  5963. return NULL_TREE;
  5964. }
  5965. /* Handle a "no_sanitize_undefined" attribute; arguments as in
  5966. struct attribute_spec.handler. */
  5967. static tree
  5968. handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
  5969. bool *no_add_attrs)
  5970. {
  5971. if (TREE_CODE (*node) != FUNCTION_DECL)
  5972. {
  5973. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5974. *no_add_attrs = true;
  5975. }
  5976. return NULL_TREE;
  5977. }
  5978. /* Handle a "stack_protect" attribute; arguments as in
  5979. struct attribute_spec.handler. */
  5980. static tree
  5981. handle_stack_protect_attribute (tree *node, tree name, tree, int,
  5982. bool *no_add_attrs)
  5983. {
  5984. if (TREE_CODE (*node) != FUNCTION_DECL)
  5985. {
  5986. warning (OPT_Wattributes, "%qE attribute ignored", name);
  5987. *no_add_attrs = true;
  5988. }
  5989. else
  5990. DECL_ATTRIBUTES (*node)
  5991. = tree_cons (get_identifier ("stack_protect"),
  5992. NULL_TREE, DECL_ATTRIBUTES (*node));
  5993. return NULL_TREE;
  5994. }
  5995. /* Handle a "noinline" attribute; arguments as in
  5996. struct attribute_spec.handler. */
  5997. static tree
  5998. handle_noinline_attribute (tree *node, tree name,
  5999. tree ARG_UNUSED (args),
  6000. int ARG_UNUSED (flags), bool *no_add_attrs)
  6001. {
  6002. if (TREE_CODE (*node) == FUNCTION_DECL)
  6003. {
  6004. if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
  6005. {
  6006. warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
  6007. "with attribute %qs", name, "always_inline");
  6008. *no_add_attrs = true;
  6009. }
  6010. else
  6011. DECL_UNINLINABLE (*node) = 1;
  6012. }
  6013. else
  6014. {
  6015. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6016. *no_add_attrs = true;
  6017. }
  6018. return NULL_TREE;
  6019. }
  6020. /* Handle a "noclone" attribute; arguments as in
  6021. struct attribute_spec.handler. */
  6022. static tree
  6023. handle_noclone_attribute (tree *node, tree name,
  6024. tree ARG_UNUSED (args),
  6025. int ARG_UNUSED (flags), bool *no_add_attrs)
  6026. {
  6027. if (TREE_CODE (*node) != FUNCTION_DECL)
  6028. {
  6029. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6030. *no_add_attrs = true;
  6031. }
  6032. return NULL_TREE;
  6033. }
  6034. /* Handle a "no_icf" attribute; arguments as in
  6035. struct attribute_spec.handler. */
  6036. static tree
  6037. handle_noicf_attribute (tree *node, tree name,
  6038. tree ARG_UNUSED (args),
  6039. int ARG_UNUSED (flags), bool *no_add_attrs)
  6040. {
  6041. if (TREE_CODE (*node) != FUNCTION_DECL)
  6042. {
  6043. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6044. *no_add_attrs = true;
  6045. }
  6046. return NULL_TREE;
  6047. }
  6048. /* Handle a "always_inline" attribute; arguments as in
  6049. struct attribute_spec.handler. */
  6050. static tree
  6051. handle_always_inline_attribute (tree *node, tree name,
  6052. tree ARG_UNUSED (args),
  6053. int ARG_UNUSED (flags),
  6054. bool *no_add_attrs)
  6055. {
  6056. if (TREE_CODE (*node) == FUNCTION_DECL)
  6057. {
  6058. if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
  6059. {
  6060. warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
  6061. "with %qs attribute", name, "noinline");
  6062. *no_add_attrs = true;
  6063. }
  6064. else
  6065. /* Set the attribute and mark it for disregarding inline
  6066. limits. */
  6067. DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
  6068. }
  6069. else
  6070. {
  6071. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6072. *no_add_attrs = true;
  6073. }
  6074. return NULL_TREE;
  6075. }
  6076. /* Handle a "gnu_inline" attribute; arguments as in
  6077. struct attribute_spec.handler. */
  6078. static tree
  6079. handle_gnu_inline_attribute (tree *node, tree name,
  6080. tree ARG_UNUSED (args),
  6081. int ARG_UNUSED (flags),
  6082. bool *no_add_attrs)
  6083. {
  6084. if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
  6085. {
  6086. /* Do nothing else, just set the attribute. We'll get at
  6087. it later with lookup_attribute. */
  6088. }
  6089. else
  6090. {
  6091. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6092. *no_add_attrs = true;
  6093. }
  6094. return NULL_TREE;
  6095. }
  6096. /* Handle a "leaf" attribute; arguments as in
  6097. struct attribute_spec.handler. */
  6098. static tree
  6099. handle_leaf_attribute (tree *node, tree name,
  6100. tree ARG_UNUSED (args),
  6101. int ARG_UNUSED (flags), bool *no_add_attrs)
  6102. {
  6103. if (TREE_CODE (*node) != FUNCTION_DECL)
  6104. {
  6105. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6106. *no_add_attrs = true;
  6107. }
  6108. if (!TREE_PUBLIC (*node))
  6109. {
  6110. warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
  6111. *no_add_attrs = true;
  6112. }
  6113. return NULL_TREE;
  6114. }
  6115. /* Handle an "artificial" attribute; arguments as in
  6116. struct attribute_spec.handler. */
  6117. static tree
  6118. handle_artificial_attribute (tree *node, tree name,
  6119. tree ARG_UNUSED (args),
  6120. int ARG_UNUSED (flags),
  6121. bool *no_add_attrs)
  6122. {
  6123. if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
  6124. {
  6125. /* Do nothing else, just set the attribute. We'll get at
  6126. it later with lookup_attribute. */
  6127. }
  6128. else
  6129. {
  6130. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6131. *no_add_attrs = true;
  6132. }
  6133. return NULL_TREE;
  6134. }
  6135. /* Handle a "flatten" attribute; arguments as in
  6136. struct attribute_spec.handler. */
  6137. static tree
  6138. handle_flatten_attribute (tree *node, tree name,
  6139. tree args ATTRIBUTE_UNUSED,
  6140. int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
  6141. {
  6142. if (TREE_CODE (*node) == FUNCTION_DECL)
  6143. /* Do nothing else, just set the attribute. We'll get at
  6144. it later with lookup_attribute. */
  6145. ;
  6146. else
  6147. {
  6148. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6149. *no_add_attrs = true;
  6150. }
  6151. return NULL_TREE;
  6152. }
  6153. /* Handle a "warning" or "error" attribute; arguments as in
  6154. struct attribute_spec.handler. */
  6155. static tree
  6156. handle_error_attribute (tree *node, tree name, tree args,
  6157. int ARG_UNUSED (flags), bool *no_add_attrs)
  6158. {
  6159. if (TREE_CODE (*node) == FUNCTION_DECL
  6160. && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
  6161. /* Do nothing else, just set the attribute. We'll get at
  6162. it later with lookup_attribute. */
  6163. ;
  6164. else
  6165. {
  6166. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6167. *no_add_attrs = true;
  6168. }
  6169. return NULL_TREE;
  6170. }
  6171. /* Handle a "used" attribute; arguments as in
  6172. struct attribute_spec.handler. */
  6173. static tree
  6174. handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
  6175. int ARG_UNUSED (flags), bool *no_add_attrs)
  6176. {
  6177. tree node = *pnode;
  6178. if (TREE_CODE (node) == FUNCTION_DECL
  6179. || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
  6180. || (TREE_CODE (node) == TYPE_DECL))
  6181. {
  6182. TREE_USED (node) = 1;
  6183. DECL_PRESERVE_P (node) = 1;
  6184. if (TREE_CODE (node) == VAR_DECL)
  6185. DECL_READ_P (node) = 1;
  6186. }
  6187. else
  6188. {
  6189. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6190. *no_add_attrs = true;
  6191. }
  6192. return NULL_TREE;
  6193. }
  6194. /* Handle a "unused" attribute; arguments as in
  6195. struct attribute_spec.handler. */
  6196. static tree
  6197. handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  6198. int flags, bool *no_add_attrs)
  6199. {
  6200. if (DECL_P (*node))
  6201. {
  6202. tree decl = *node;
  6203. if (TREE_CODE (decl) == PARM_DECL
  6204. || TREE_CODE (decl) == VAR_DECL
  6205. || TREE_CODE (decl) == FUNCTION_DECL
  6206. || TREE_CODE (decl) == LABEL_DECL
  6207. || TREE_CODE (decl) == TYPE_DECL)
  6208. {
  6209. TREE_USED (decl) = 1;
  6210. if (TREE_CODE (decl) == VAR_DECL
  6211. || TREE_CODE (decl) == PARM_DECL)
  6212. DECL_READ_P (decl) = 1;
  6213. }
  6214. else
  6215. {
  6216. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6217. *no_add_attrs = true;
  6218. }
  6219. }
  6220. else
  6221. {
  6222. if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
  6223. *node = build_variant_type_copy (*node);
  6224. TREE_USED (*node) = 1;
  6225. }
  6226. return NULL_TREE;
  6227. }
  6228. /* Handle a "externally_visible" attribute; arguments as in
  6229. struct attribute_spec.handler. */
  6230. static tree
  6231. handle_externally_visible_attribute (tree *pnode, tree name,
  6232. tree ARG_UNUSED (args),
  6233. int ARG_UNUSED (flags),
  6234. bool *no_add_attrs)
  6235. {
  6236. tree node = *pnode;
  6237. if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
  6238. {
  6239. if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
  6240. && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
  6241. {
  6242. warning (OPT_Wattributes,
  6243. "%qE attribute have effect only on public objects", name);
  6244. *no_add_attrs = true;
  6245. }
  6246. }
  6247. else
  6248. {
  6249. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6250. *no_add_attrs = true;
  6251. }
  6252. return NULL_TREE;
  6253. }
  6254. /* Handle the "no_reorder" attribute. Arguments as in
  6255. struct attribute_spec.handler. */
  6256. static tree
  6257. handle_no_reorder_attribute (tree *pnode,
  6258. tree name,
  6259. tree,
  6260. int,
  6261. bool *no_add_attrs)
  6262. {
  6263. tree node = *pnode;
  6264. if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL)
  6265. && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
  6266. {
  6267. warning (OPT_Wattributes,
  6268. "%qE attribute only affects top level objects",
  6269. name);
  6270. *no_add_attrs = true;
  6271. }
  6272. return NULL_TREE;
  6273. }
  6274. /* Handle a "const" attribute; arguments as in
  6275. struct attribute_spec.handler. */
  6276. static tree
  6277. handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  6278. int ARG_UNUSED (flags), bool *no_add_attrs)
  6279. {
  6280. tree type = TREE_TYPE (*node);
  6281. /* See FIXME comment on noreturn in c_common_attribute_table. */
  6282. if (TREE_CODE (*node) == FUNCTION_DECL)
  6283. TREE_READONLY (*node) = 1;
  6284. else if (TREE_CODE (type) == POINTER_TYPE
  6285. && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
  6286. TREE_TYPE (*node)
  6287. = (build_qualified_type
  6288. (build_pointer_type
  6289. (build_type_variant (TREE_TYPE (type), 1,
  6290. TREE_THIS_VOLATILE (TREE_TYPE (type)))),
  6291. TYPE_QUALS (type)));
  6292. else
  6293. {
  6294. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6295. *no_add_attrs = true;
  6296. }
  6297. return NULL_TREE;
  6298. }
  6299. /* Handle a "transparent_union" attribute; arguments as in
  6300. struct attribute_spec.handler. */
  6301. static tree
  6302. handle_transparent_union_attribute (tree *node, tree name,
  6303. tree ARG_UNUSED (args), int flags,
  6304. bool *no_add_attrs)
  6305. {
  6306. tree type;
  6307. *no_add_attrs = true;
  6308. if (TREE_CODE (*node) == TYPE_DECL
  6309. && ! (flags & ATTR_FLAG_CXX11))
  6310. node = &TREE_TYPE (*node);
  6311. type = *node;
  6312. if (TREE_CODE (type) == UNION_TYPE)
  6313. {
  6314. /* Make sure that the first field will work for a transparent union.
  6315. If the type isn't complete yet, leave the check to the code in
  6316. finish_struct. */
  6317. if (TYPE_SIZE (type))
  6318. {
  6319. tree first = first_field (type);
  6320. if (first == NULL_TREE
  6321. || DECL_ARTIFICIAL (first)
  6322. || TYPE_MODE (type) != DECL_MODE (first))
  6323. goto ignored;
  6324. }
  6325. if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
  6326. {
  6327. /* If the type isn't complete yet, setting the flag
  6328. on a variant wouldn't ever be checked. */
  6329. if (!TYPE_SIZE (type))
  6330. goto ignored;
  6331. /* build_duplicate_type doesn't work for C++. */
  6332. if (c_dialect_cxx ())
  6333. goto ignored;
  6334. /* A type variant isn't good enough, since we don't a cast
  6335. to such a type removed as a no-op. */
  6336. *node = type = build_duplicate_type (type);
  6337. }
  6338. TYPE_TRANSPARENT_AGGR (type) = 1;
  6339. return NULL_TREE;
  6340. }
  6341. ignored:
  6342. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6343. return NULL_TREE;
  6344. }
  6345. /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
  6346. get the requested priority for a constructor or destructor,
  6347. possibly issuing diagnostics for invalid or reserved
  6348. priorities. */
  6349. static priority_type
  6350. get_priority (tree args, bool is_destructor)
  6351. {
  6352. HOST_WIDE_INT pri;
  6353. tree arg;
  6354. if (!args)
  6355. return DEFAULT_INIT_PRIORITY;
  6356. if (!SUPPORTS_INIT_PRIORITY)
  6357. {
  6358. if (is_destructor)
  6359. error ("destructor priorities are not supported");
  6360. else
  6361. error ("constructor priorities are not supported");
  6362. return DEFAULT_INIT_PRIORITY;
  6363. }
  6364. arg = TREE_VALUE (args);
  6365. if (TREE_CODE (arg) == IDENTIFIER_NODE)
  6366. goto invalid;
  6367. if (arg == error_mark_node)
  6368. return DEFAULT_INIT_PRIORITY;
  6369. arg = default_conversion (arg);
  6370. if (!tree_fits_shwi_p (arg)
  6371. || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
  6372. goto invalid;
  6373. pri = tree_to_shwi (arg);
  6374. if (pri < 0 || pri > MAX_INIT_PRIORITY)
  6375. goto invalid;
  6376. if (pri <= MAX_RESERVED_INIT_PRIORITY)
  6377. {
  6378. if (is_destructor)
  6379. warning (0,
  6380. "destructor priorities from 0 to %d are reserved "
  6381. "for the implementation",
  6382. MAX_RESERVED_INIT_PRIORITY);
  6383. else
  6384. warning (0,
  6385. "constructor priorities from 0 to %d are reserved "
  6386. "for the implementation",
  6387. MAX_RESERVED_INIT_PRIORITY);
  6388. }
  6389. return pri;
  6390. invalid:
  6391. if (is_destructor)
  6392. error ("destructor priorities must be integers from 0 to %d inclusive",
  6393. MAX_INIT_PRIORITY);
  6394. else
  6395. error ("constructor priorities must be integers from 0 to %d inclusive",
  6396. MAX_INIT_PRIORITY);
  6397. return DEFAULT_INIT_PRIORITY;
  6398. }
  6399. /* Handle a "constructor" attribute; arguments as in
  6400. struct attribute_spec.handler. */
  6401. static tree
  6402. handle_constructor_attribute (tree *node, tree name, tree args,
  6403. int ARG_UNUSED (flags),
  6404. bool *no_add_attrs)
  6405. {
  6406. tree decl = *node;
  6407. tree type = TREE_TYPE (decl);
  6408. if (TREE_CODE (decl) == FUNCTION_DECL
  6409. && TREE_CODE (type) == FUNCTION_TYPE
  6410. && decl_function_context (decl) == 0)
  6411. {
  6412. priority_type priority;
  6413. DECL_STATIC_CONSTRUCTOR (decl) = 1;
  6414. priority = get_priority (args, /*is_destructor=*/false);
  6415. SET_DECL_INIT_PRIORITY (decl, priority);
  6416. TREE_USED (decl) = 1;
  6417. }
  6418. else
  6419. {
  6420. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6421. *no_add_attrs = true;
  6422. }
  6423. return NULL_TREE;
  6424. }
  6425. /* Handle a "destructor" attribute; arguments as in
  6426. struct attribute_spec.handler. */
  6427. static tree
  6428. handle_destructor_attribute (tree *node, tree name, tree args,
  6429. int ARG_UNUSED (flags),
  6430. bool *no_add_attrs)
  6431. {
  6432. tree decl = *node;
  6433. tree type = TREE_TYPE (decl);
  6434. if (TREE_CODE (decl) == FUNCTION_DECL
  6435. && TREE_CODE (type) == FUNCTION_TYPE
  6436. && decl_function_context (decl) == 0)
  6437. {
  6438. priority_type priority;
  6439. DECL_STATIC_DESTRUCTOR (decl) = 1;
  6440. priority = get_priority (args, /*is_destructor=*/true);
  6441. SET_DECL_FINI_PRIORITY (decl, priority);
  6442. TREE_USED (decl) = 1;
  6443. }
  6444. else
  6445. {
  6446. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6447. *no_add_attrs = true;
  6448. }
  6449. return NULL_TREE;
  6450. }
  6451. /* Nonzero if the mode is a valid vector mode for this architecture.
  6452. This returns nonzero even if there is no hardware support for the
  6453. vector mode, but we can emulate with narrower modes. */
  6454. static int
  6455. vector_mode_valid_p (machine_mode mode)
  6456. {
  6457. enum mode_class mclass = GET_MODE_CLASS (mode);
  6458. machine_mode innermode;
  6459. /* Doh! What's going on? */
  6460. if (mclass != MODE_VECTOR_INT
  6461. && mclass != MODE_VECTOR_FLOAT
  6462. && mclass != MODE_VECTOR_FRACT
  6463. && mclass != MODE_VECTOR_UFRACT
  6464. && mclass != MODE_VECTOR_ACCUM
  6465. && mclass != MODE_VECTOR_UACCUM)
  6466. return 0;
  6467. /* Hardware support. Woo hoo! */
  6468. if (targetm.vector_mode_supported_p (mode))
  6469. return 1;
  6470. innermode = GET_MODE_INNER (mode);
  6471. /* We should probably return 1 if requesting V4DI and we have no DI,
  6472. but we have V2DI, but this is probably very unlikely. */
  6473. /* If we have support for the inner mode, we can safely emulate it.
  6474. We may not have V2DI, but me can emulate with a pair of DIs. */
  6475. return targetm.scalar_mode_supported_p (innermode);
  6476. }
  6477. /* Handle a "mode" attribute; arguments as in
  6478. struct attribute_spec.handler. */
  6479. static tree
  6480. handle_mode_attribute (tree *node, tree name, tree args,
  6481. int ARG_UNUSED (flags), bool *no_add_attrs)
  6482. {
  6483. tree type = *node;
  6484. tree ident = TREE_VALUE (args);
  6485. *no_add_attrs = true;
  6486. if (TREE_CODE (ident) != IDENTIFIER_NODE)
  6487. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6488. else
  6489. {
  6490. int j;
  6491. const char *p = IDENTIFIER_POINTER (ident);
  6492. int len = strlen (p);
  6493. machine_mode mode = VOIDmode;
  6494. tree typefm;
  6495. bool valid_mode;
  6496. if (len > 4 && p[0] == '_' && p[1] == '_'
  6497. && p[len - 1] == '_' && p[len - 2] == '_')
  6498. {
  6499. char *newp = (char *) alloca (len - 1);
  6500. strcpy (newp, &p[2]);
  6501. newp[len - 4] = '\0';
  6502. p = newp;
  6503. }
  6504. /* Change this type to have a type with the specified mode.
  6505. First check for the special modes. */
  6506. if (!strcmp (p, "byte"))
  6507. mode = byte_mode;
  6508. else if (!strcmp (p, "word"))
  6509. mode = word_mode;
  6510. else if (!strcmp (p, "pointer"))
  6511. mode = ptr_mode;
  6512. else if (!strcmp (p, "libgcc_cmp_return"))
  6513. mode = targetm.libgcc_cmp_return_mode ();
  6514. else if (!strcmp (p, "libgcc_shift_count"))
  6515. mode = targetm.libgcc_shift_count_mode ();
  6516. else if (!strcmp (p, "unwind_word"))
  6517. mode = targetm.unwind_word_mode ();
  6518. else
  6519. for (j = 0; j < NUM_MACHINE_MODES; j++)
  6520. if (!strcmp (p, GET_MODE_NAME (j)))
  6521. {
  6522. mode = (machine_mode) j;
  6523. break;
  6524. }
  6525. if (mode == VOIDmode)
  6526. {
  6527. error ("unknown machine mode %qE", ident);
  6528. return NULL_TREE;
  6529. }
  6530. valid_mode = false;
  6531. switch (GET_MODE_CLASS (mode))
  6532. {
  6533. case MODE_INT:
  6534. case MODE_PARTIAL_INT:
  6535. case MODE_FLOAT:
  6536. case MODE_DECIMAL_FLOAT:
  6537. case MODE_FRACT:
  6538. case MODE_UFRACT:
  6539. case MODE_ACCUM:
  6540. case MODE_UACCUM:
  6541. valid_mode = targetm.scalar_mode_supported_p (mode);
  6542. break;
  6543. case MODE_COMPLEX_INT:
  6544. case MODE_COMPLEX_FLOAT:
  6545. valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
  6546. break;
  6547. case MODE_VECTOR_INT:
  6548. case MODE_VECTOR_FLOAT:
  6549. case MODE_VECTOR_FRACT:
  6550. case MODE_VECTOR_UFRACT:
  6551. case MODE_VECTOR_ACCUM:
  6552. case MODE_VECTOR_UACCUM:
  6553. warning (OPT_Wattributes, "specifying vector types with "
  6554. "__attribute__ ((mode)) is deprecated");
  6555. warning (OPT_Wattributes,
  6556. "use __attribute__ ((vector_size)) instead");
  6557. valid_mode = vector_mode_valid_p (mode);
  6558. break;
  6559. default:
  6560. break;
  6561. }
  6562. if (!valid_mode)
  6563. {
  6564. error ("unable to emulate %qs", p);
  6565. return NULL_TREE;
  6566. }
  6567. if (POINTER_TYPE_P (type))
  6568. {
  6569. addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
  6570. tree (*fn)(tree, machine_mode, bool);
  6571. if (!targetm.addr_space.valid_pointer_mode (mode, as))
  6572. {
  6573. error ("invalid pointer mode %qs", p);
  6574. return NULL_TREE;
  6575. }
  6576. if (TREE_CODE (type) == POINTER_TYPE)
  6577. fn = build_pointer_type_for_mode;
  6578. else
  6579. fn = build_reference_type_for_mode;
  6580. typefm = fn (TREE_TYPE (type), mode, false);
  6581. }
  6582. else
  6583. {
  6584. /* For fixed-point modes, we need to test if the signness of type
  6585. and the machine mode are consistent. */
  6586. if (ALL_FIXED_POINT_MODE_P (mode)
  6587. && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
  6588. {
  6589. error ("signedness of type and machine mode %qs don%'t match", p);
  6590. return NULL_TREE;
  6591. }
  6592. /* For fixed-point modes, we need to pass saturating info. */
  6593. typefm = lang_hooks.types.type_for_mode (mode,
  6594. ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
  6595. : TYPE_UNSIGNED (type));
  6596. }
  6597. if (typefm == NULL_TREE)
  6598. {
  6599. error ("no data type for mode %qs", p);
  6600. return NULL_TREE;
  6601. }
  6602. else if (TREE_CODE (type) == ENUMERAL_TYPE)
  6603. {
  6604. /* For enumeral types, copy the precision from the integer
  6605. type returned above. If not an INTEGER_TYPE, we can't use
  6606. this mode for this type. */
  6607. if (TREE_CODE (typefm) != INTEGER_TYPE)
  6608. {
  6609. error ("cannot use mode %qs for enumeral types", p);
  6610. return NULL_TREE;
  6611. }
  6612. if (flags & ATTR_FLAG_TYPE_IN_PLACE)
  6613. {
  6614. TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
  6615. typefm = type;
  6616. }
  6617. else
  6618. {
  6619. /* We cannot build a type variant, as there's code that assumes
  6620. that TYPE_MAIN_VARIANT has the same mode. This includes the
  6621. debug generators. Instead, create a subrange type. This
  6622. results in all of the enumeral values being emitted only once
  6623. in the original, and the subtype gets them by reference. */
  6624. if (TYPE_UNSIGNED (type))
  6625. typefm = make_unsigned_type (TYPE_PRECISION (typefm));
  6626. else
  6627. typefm = make_signed_type (TYPE_PRECISION (typefm));
  6628. TREE_TYPE (typefm) = type;
  6629. }
  6630. }
  6631. else if (VECTOR_MODE_P (mode)
  6632. ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
  6633. : TREE_CODE (type) != TREE_CODE (typefm))
  6634. {
  6635. error ("mode %qs applied to inappropriate type", p);
  6636. return NULL_TREE;
  6637. }
  6638. *node = typefm;
  6639. }
  6640. return NULL_TREE;
  6641. }
  6642. /* Handle a "section" attribute; arguments as in
  6643. struct attribute_spec.handler. */
  6644. static tree
  6645. handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
  6646. int ARG_UNUSED (flags), bool *no_add_attrs)
  6647. {
  6648. tree decl = *node;
  6649. if (targetm_common.have_named_sections)
  6650. {
  6651. user_defined_section_attribute = true;
  6652. if ((TREE_CODE (decl) == FUNCTION_DECL
  6653. || TREE_CODE (decl) == VAR_DECL)
  6654. && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
  6655. {
  6656. if (TREE_CODE (decl) == VAR_DECL
  6657. && current_function_decl != NULL_TREE
  6658. && !TREE_STATIC (decl))
  6659. {
  6660. error_at (DECL_SOURCE_LOCATION (decl),
  6661. "section attribute cannot be specified for "
  6662. "local variables");
  6663. *no_add_attrs = true;
  6664. }
  6665. /* The decl may have already been given a section attribute
  6666. from a previous declaration. Ensure they match. */
  6667. else if (DECL_SECTION_NAME (decl) != NULL
  6668. && strcmp (DECL_SECTION_NAME (decl),
  6669. TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
  6670. {
  6671. error ("section of %q+D conflicts with previous declaration",
  6672. *node);
  6673. *no_add_attrs = true;
  6674. }
  6675. else if (TREE_CODE (decl) == VAR_DECL
  6676. && !targetm.have_tls && targetm.emutls.tmpl_section
  6677. && DECL_THREAD_LOCAL_P (decl))
  6678. {
  6679. error ("section of %q+D cannot be overridden", *node);
  6680. *no_add_attrs = true;
  6681. }
  6682. else
  6683. set_decl_section_name (decl,
  6684. TREE_STRING_POINTER (TREE_VALUE (args)));
  6685. }
  6686. else
  6687. {
  6688. error ("section attribute not allowed for %q+D", *node);
  6689. *no_add_attrs = true;
  6690. }
  6691. }
  6692. else
  6693. {
  6694. error_at (DECL_SOURCE_LOCATION (*node),
  6695. "section attributes are not supported for this target");
  6696. *no_add_attrs = true;
  6697. }
  6698. return NULL_TREE;
  6699. }
  6700. /* Check whether ALIGN is a valid user-specified alignment. If so,
  6701. return its base-2 log; if not, output an error and return -1. If
  6702. ALLOW_ZERO then 0 is valid and should result in a return of -1 with
  6703. no error. */
  6704. int
  6705. check_user_alignment (const_tree align, bool allow_zero)
  6706. {
  6707. int i;
  6708. if (error_operand_p (align))
  6709. return -1;
  6710. if (TREE_CODE (align) != INTEGER_CST
  6711. || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
  6712. {
  6713. error ("requested alignment is not an integer constant");
  6714. return -1;
  6715. }
  6716. else if (allow_zero && integer_zerop (align))
  6717. return -1;
  6718. else if (tree_int_cst_sgn (align) == -1
  6719. || (i = tree_log2 (align)) == -1)
  6720. {
  6721. error ("requested alignment is not a positive power of 2");
  6722. return -1;
  6723. }
  6724. else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
  6725. {
  6726. error ("requested alignment is too large");
  6727. return -1;
  6728. }
  6729. return i;
  6730. }
  6731. /*
  6732. If in c++-11, check if the c++-11 alignment constraint with respect
  6733. to fundamental alignment (in [dcl.align]) are satisfied. If not in
  6734. c++-11 mode, does nothing.
  6735. [dcl.align]2/ says:
  6736. [* if the constant expression evaluates to a fundamental alignment,
  6737. the alignment requirement of the declared entity shall be the
  6738. specified fundamental alignment.
  6739. * if the constant expression evaluates to an extended alignment
  6740. and the implementation supports that alignment in the context
  6741. of the declaration, the alignment of the declared entity shall
  6742. be that alignment
  6743. * if the constant expression evaluates to an extended alignment
  6744. and the implementation does not support that alignment in the
  6745. context of the declaration, the program is ill-formed]. */
  6746. static bool
  6747. check_cxx_fundamental_alignment_constraints (tree node,
  6748. unsigned align_log,
  6749. int flags)
  6750. {
  6751. bool alignment_too_large_p = false;
  6752. unsigned requested_alignment = 1U << align_log;
  6753. unsigned max_align = 0;
  6754. if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
  6755. || (node == NULL_TREE || node == error_mark_node))
  6756. return true;
  6757. if (cxx_fundamental_alignment_p (requested_alignment))
  6758. return true;
  6759. if (DECL_P (node))
  6760. {
  6761. if (TREE_STATIC (node))
  6762. {
  6763. /* For file scope variables and static members, the target
  6764. supports alignments that are at most
  6765. MAX_OFILE_ALIGNMENT. */
  6766. if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
  6767. alignment_too_large_p = true;
  6768. }
  6769. else
  6770. {
  6771. #ifdef BIGGEST_FIELD_ALIGNMENT
  6772. #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
  6773. #else
  6774. #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
  6775. #endif
  6776. /* For non-static members, the target supports either
  6777. alignments that at most either BIGGEST_FIELD_ALIGNMENT
  6778. if it is defined or BIGGEST_ALIGNMENT. */
  6779. max_align = MAX_TARGET_FIELD_ALIGNMENT;
  6780. if (TREE_CODE (node) == FIELD_DECL
  6781. && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
  6782. alignment_too_large_p = true;
  6783. #undef MAX_TARGET_FIELD_ALIGNMENT
  6784. /* For stack variables, the target supports at most
  6785. MAX_STACK_ALIGNMENT. */
  6786. else if (decl_function_context (node) != NULL
  6787. && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
  6788. alignment_too_large_p = true;
  6789. }
  6790. }
  6791. else if (TYPE_P (node))
  6792. {
  6793. /* Let's be liberal for types. */
  6794. if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
  6795. alignment_too_large_p = true;
  6796. }
  6797. if (alignment_too_large_p)
  6798. pedwarn (input_location, OPT_Wattributes,
  6799. "requested alignment %d is larger than %d",
  6800. requested_alignment, max_align);
  6801. return !alignment_too_large_p;
  6802. }
  6803. /* Handle a "aligned" attribute; arguments as in
  6804. struct attribute_spec.handler. */
  6805. static tree
  6806. handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
  6807. int flags, bool *no_add_attrs)
  6808. {
  6809. tree decl = NULL_TREE;
  6810. tree *type = NULL;
  6811. int is_type = 0;
  6812. tree align_expr;
  6813. int i;
  6814. if (args)
  6815. {
  6816. align_expr = TREE_VALUE (args);
  6817. if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
  6818. && TREE_CODE (align_expr) != FUNCTION_DECL)
  6819. align_expr = default_conversion (align_expr);
  6820. }
  6821. else
  6822. align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
  6823. if (DECL_P (*node))
  6824. {
  6825. decl = *node;
  6826. type = &TREE_TYPE (decl);
  6827. is_type = TREE_CODE (*node) == TYPE_DECL;
  6828. }
  6829. else if (TYPE_P (*node))
  6830. type = node, is_type = 1;
  6831. if ((i = check_user_alignment (align_expr, false)) == -1
  6832. || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
  6833. *no_add_attrs = true;
  6834. else if (is_type)
  6835. {
  6836. if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
  6837. /* OK, modify the type in place. */;
  6838. /* If we have a TYPE_DECL, then copy the type, so that we
  6839. don't accidentally modify a builtin type. See pushdecl. */
  6840. else if (decl && TREE_TYPE (decl) != error_mark_node
  6841. && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
  6842. {
  6843. tree tt = TREE_TYPE (decl);
  6844. *type = build_variant_type_copy (*type);
  6845. DECL_ORIGINAL_TYPE (decl) = tt;
  6846. TYPE_NAME (*type) = decl;
  6847. TREE_USED (*type) = TREE_USED (decl);
  6848. TREE_TYPE (decl) = *type;
  6849. }
  6850. else
  6851. *type = build_variant_type_copy (*type);
  6852. TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
  6853. TYPE_USER_ALIGN (*type) = 1;
  6854. }
  6855. else if (! VAR_OR_FUNCTION_DECL_P (decl)
  6856. && TREE_CODE (decl) != FIELD_DECL)
  6857. {
  6858. error ("alignment may not be specified for %q+D", decl);
  6859. *no_add_attrs = true;
  6860. }
  6861. else if (DECL_USER_ALIGN (decl)
  6862. && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
  6863. /* C++-11 [dcl.align/4]:
  6864. When multiple alignment-specifiers are specified for an
  6865. entity, the alignment requirement shall be set to the
  6866. strictest specified alignment.
  6867. This formally comes from the c++11 specification but we are
  6868. doing it for the GNU attribute syntax as well. */
  6869. *no_add_attrs = true;
  6870. else if (TREE_CODE (decl) == FUNCTION_DECL
  6871. && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
  6872. {
  6873. if (DECL_USER_ALIGN (decl))
  6874. error ("alignment for %q+D was previously specified as %d "
  6875. "and may not be decreased", decl,
  6876. DECL_ALIGN (decl) / BITS_PER_UNIT);
  6877. else
  6878. error ("alignment for %q+D must be at least %d", decl,
  6879. DECL_ALIGN (decl) / BITS_PER_UNIT);
  6880. *no_add_attrs = true;
  6881. }
  6882. else
  6883. {
  6884. DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
  6885. DECL_USER_ALIGN (decl) = 1;
  6886. }
  6887. return NULL_TREE;
  6888. }
  6889. /* Handle a "weak" attribute; arguments as in
  6890. struct attribute_spec.handler. */
  6891. static tree
  6892. handle_weak_attribute (tree *node, tree name,
  6893. tree ARG_UNUSED (args),
  6894. int ARG_UNUSED (flags),
  6895. bool * ARG_UNUSED (no_add_attrs))
  6896. {
  6897. if (TREE_CODE (*node) == FUNCTION_DECL
  6898. && DECL_DECLARED_INLINE_P (*node))
  6899. {
  6900. warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
  6901. *no_add_attrs = true;
  6902. }
  6903. else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
  6904. {
  6905. error ("indirect function %q+D cannot be declared weak", *node);
  6906. *no_add_attrs = true;
  6907. return NULL_TREE;
  6908. }
  6909. else if (TREE_CODE (*node) == FUNCTION_DECL
  6910. || TREE_CODE (*node) == VAR_DECL)
  6911. {
  6912. struct symtab_node *n = symtab_node::get (*node);
  6913. if (n && n->refuse_visibility_changes)
  6914. error ("%+D declared weak after being used", *node);
  6915. declare_weak (*node);
  6916. }
  6917. else
  6918. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6919. return NULL_TREE;
  6920. }
  6921. /* Handle an "alias" or "ifunc" attribute; arguments as in
  6922. struct attribute_spec.handler, except that IS_ALIAS tells us
  6923. whether this is an alias as opposed to ifunc attribute. */
  6924. static tree
  6925. handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
  6926. bool *no_add_attrs)
  6927. {
  6928. tree decl = *node;
  6929. if (TREE_CODE (decl) != FUNCTION_DECL
  6930. && (!is_alias || TREE_CODE (decl) != VAR_DECL))
  6931. {
  6932. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6933. *no_add_attrs = true;
  6934. }
  6935. else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
  6936. || (TREE_CODE (decl) != FUNCTION_DECL
  6937. && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
  6938. /* A static variable declaration is always a tentative definition,
  6939. but the alias is a non-tentative definition which overrides. */
  6940. || (TREE_CODE (decl) != FUNCTION_DECL
  6941. && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
  6942. {
  6943. error ("%q+D defined both normally and as %qE attribute", decl, name);
  6944. *no_add_attrs = true;
  6945. return NULL_TREE;
  6946. }
  6947. else if (!is_alias
  6948. && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
  6949. || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
  6950. {
  6951. error ("weak %q+D cannot be defined %qE", decl, name);
  6952. *no_add_attrs = true;
  6953. return NULL_TREE;
  6954. }
  6955. /* Note that the very first time we process a nested declaration,
  6956. decl_function_context will not be set. Indeed, *would* never
  6957. be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
  6958. we do below. After such frobbery, pushdecl would set the context.
  6959. In any case, this is never what we want. */
  6960. else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
  6961. {
  6962. tree id;
  6963. id = TREE_VALUE (args);
  6964. if (TREE_CODE (id) != STRING_CST)
  6965. {
  6966. error ("attribute %qE argument not a string", name);
  6967. *no_add_attrs = true;
  6968. return NULL_TREE;
  6969. }
  6970. id = get_identifier (TREE_STRING_POINTER (id));
  6971. /* This counts as a use of the object pointed to. */
  6972. TREE_USED (id) = 1;
  6973. if (TREE_CODE (decl) == FUNCTION_DECL)
  6974. DECL_INITIAL (decl) = error_mark_node;
  6975. else
  6976. TREE_STATIC (decl) = 1;
  6977. if (!is_alias)
  6978. /* ifuncs are also aliases, so set that attribute too. */
  6979. DECL_ATTRIBUTES (decl)
  6980. = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
  6981. }
  6982. else
  6983. {
  6984. warning (OPT_Wattributes, "%qE attribute ignored", name);
  6985. *no_add_attrs = true;
  6986. }
  6987. if (decl_in_symtab_p (*node))
  6988. {
  6989. struct symtab_node *n = symtab_node::get (decl);
  6990. if (n && n->refuse_visibility_changes)
  6991. {
  6992. if (is_alias)
  6993. error ("%+D declared alias after being used", decl);
  6994. else
  6995. error ("%+D declared ifunc after being used", decl);
  6996. }
  6997. }
  6998. return NULL_TREE;
  6999. }
  7000. /* Handle an "alias" or "ifunc" attribute; arguments as in
  7001. struct attribute_spec.handler. */
  7002. static tree
  7003. handle_ifunc_attribute (tree *node, tree name, tree args,
  7004. int ARG_UNUSED (flags), bool *no_add_attrs)
  7005. {
  7006. return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
  7007. }
  7008. /* Handle an "alias" or "ifunc" attribute; arguments as in
  7009. struct attribute_spec.handler. */
  7010. static tree
  7011. handle_alias_attribute (tree *node, tree name, tree args,
  7012. int ARG_UNUSED (flags), bool *no_add_attrs)
  7013. {
  7014. return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
  7015. }
  7016. /* Handle a "weakref" attribute; arguments as in struct
  7017. attribute_spec.handler. */
  7018. static tree
  7019. handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
  7020. int flags, bool *no_add_attrs)
  7021. {
  7022. tree attr = NULL_TREE;
  7023. /* We must ignore the attribute when it is associated with
  7024. local-scoped decls, since attribute alias is ignored and many
  7025. such symbols do not even have a DECL_WEAK field. */
  7026. if (decl_function_context (*node)
  7027. || current_function_decl
  7028. || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
  7029. {
  7030. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7031. *no_add_attrs = true;
  7032. return NULL_TREE;
  7033. }
  7034. if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
  7035. {
  7036. error ("indirect function %q+D cannot be declared weakref", *node);
  7037. *no_add_attrs = true;
  7038. return NULL_TREE;
  7039. }
  7040. /* The idea here is that `weakref("name")' mutates into `weakref,
  7041. alias("name")', and weakref without arguments, in turn,
  7042. implicitly adds weak. */
  7043. if (args)
  7044. {
  7045. attr = tree_cons (get_identifier ("alias"), args, attr);
  7046. attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
  7047. *no_add_attrs = true;
  7048. decl_attributes (node, attr, flags);
  7049. }
  7050. else
  7051. {
  7052. if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
  7053. error_at (DECL_SOURCE_LOCATION (*node),
  7054. "weakref attribute must appear before alias attribute");
  7055. /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
  7056. and that isn't supported; and because it wants to add it to
  7057. the list of weak decls, which isn't helpful. */
  7058. DECL_WEAK (*node) = 1;
  7059. }
  7060. if (decl_in_symtab_p (*node))
  7061. {
  7062. struct symtab_node *n = symtab_node::get (*node);
  7063. if (n && n->refuse_visibility_changes)
  7064. error ("%+D declared weakref after being used", *node);
  7065. }
  7066. return NULL_TREE;
  7067. }
  7068. /* Handle an "visibility" attribute; arguments as in
  7069. struct attribute_spec.handler. */
  7070. static tree
  7071. handle_visibility_attribute (tree *node, tree name, tree args,
  7072. int ARG_UNUSED (flags),
  7073. bool *ARG_UNUSED (no_add_attrs))
  7074. {
  7075. tree decl = *node;
  7076. tree id = TREE_VALUE (args);
  7077. enum symbol_visibility vis;
  7078. if (TYPE_P (*node))
  7079. {
  7080. if (TREE_CODE (*node) == ENUMERAL_TYPE)
  7081. /* OK */;
  7082. else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
  7083. {
  7084. warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
  7085. name);
  7086. return NULL_TREE;
  7087. }
  7088. else if (TYPE_FIELDS (*node))
  7089. {
  7090. error ("%qE attribute ignored because %qT is already defined",
  7091. name, *node);
  7092. return NULL_TREE;
  7093. }
  7094. }
  7095. else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
  7096. {
  7097. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7098. return NULL_TREE;
  7099. }
  7100. if (TREE_CODE (id) != STRING_CST)
  7101. {
  7102. error ("visibility argument not a string");
  7103. return NULL_TREE;
  7104. }
  7105. /* If this is a type, set the visibility on the type decl. */
  7106. if (TYPE_P (decl))
  7107. {
  7108. decl = TYPE_NAME (decl);
  7109. if (!decl)
  7110. return NULL_TREE;
  7111. if (TREE_CODE (decl) == IDENTIFIER_NODE)
  7112. {
  7113. warning (OPT_Wattributes, "%qE attribute ignored on types",
  7114. name);
  7115. return NULL_TREE;
  7116. }
  7117. }
  7118. if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
  7119. vis = VISIBILITY_DEFAULT;
  7120. else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
  7121. vis = VISIBILITY_INTERNAL;
  7122. else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
  7123. vis = VISIBILITY_HIDDEN;
  7124. else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
  7125. vis = VISIBILITY_PROTECTED;
  7126. else
  7127. {
  7128. error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
  7129. vis = VISIBILITY_DEFAULT;
  7130. }
  7131. if (DECL_VISIBILITY_SPECIFIED (decl)
  7132. && vis != DECL_VISIBILITY (decl))
  7133. {
  7134. tree attributes = (TYPE_P (*node)
  7135. ? TYPE_ATTRIBUTES (*node)
  7136. : DECL_ATTRIBUTES (decl));
  7137. if (lookup_attribute ("visibility", attributes))
  7138. error ("%qD redeclared with different visibility", decl);
  7139. else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
  7140. && lookup_attribute ("dllimport", attributes))
  7141. error ("%qD was declared %qs which implies default visibility",
  7142. decl, "dllimport");
  7143. else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
  7144. && lookup_attribute ("dllexport", attributes))
  7145. error ("%qD was declared %qs which implies default visibility",
  7146. decl, "dllexport");
  7147. }
  7148. DECL_VISIBILITY (decl) = vis;
  7149. DECL_VISIBILITY_SPECIFIED (decl) = 1;
  7150. /* Go ahead and attach the attribute to the node as well. This is needed
  7151. so we can determine whether we have VISIBILITY_DEFAULT because the
  7152. visibility was not specified, or because it was explicitly overridden
  7153. from the containing scope. */
  7154. return NULL_TREE;
  7155. }
  7156. /* Determine the ELF symbol visibility for DECL, which is either a
  7157. variable or a function. It is an error to use this function if a
  7158. definition of DECL is not available in this translation unit.
  7159. Returns true if the final visibility has been determined by this
  7160. function; false if the caller is free to make additional
  7161. modifications. */
  7162. bool
  7163. c_determine_visibility (tree decl)
  7164. {
  7165. gcc_assert (TREE_CODE (decl) == VAR_DECL
  7166. || TREE_CODE (decl) == FUNCTION_DECL);
  7167. /* If the user explicitly specified the visibility with an
  7168. attribute, honor that. DECL_VISIBILITY will have been set during
  7169. the processing of the attribute. We check for an explicit
  7170. attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
  7171. to distinguish the use of an attribute from the use of a "#pragma
  7172. GCC visibility push(...)"; in the latter case we still want other
  7173. considerations to be able to overrule the #pragma. */
  7174. if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
  7175. || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
  7176. && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
  7177. || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
  7178. return true;
  7179. /* Set default visibility to whatever the user supplied with
  7180. visibility_specified depending on #pragma GCC visibility. */
  7181. if (!DECL_VISIBILITY_SPECIFIED (decl))
  7182. {
  7183. if (visibility_options.inpragma
  7184. || DECL_VISIBILITY (decl) != default_visibility)
  7185. {
  7186. DECL_VISIBILITY (decl) = default_visibility;
  7187. DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
  7188. /* If visibility changed and DECL already has DECL_RTL, ensure
  7189. symbol flags are updated. */
  7190. if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
  7191. || TREE_CODE (decl) == FUNCTION_DECL)
  7192. && DECL_RTL_SET_P (decl))
  7193. make_decl_rtl (decl);
  7194. }
  7195. }
  7196. return false;
  7197. }
  7198. /* Handle an "tls_model" attribute; arguments as in
  7199. struct attribute_spec.handler. */
  7200. static tree
  7201. handle_tls_model_attribute (tree *node, tree name, tree args,
  7202. int ARG_UNUSED (flags), bool *no_add_attrs)
  7203. {
  7204. tree id;
  7205. tree decl = *node;
  7206. enum tls_model kind;
  7207. *no_add_attrs = true;
  7208. if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
  7209. {
  7210. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7211. return NULL_TREE;
  7212. }
  7213. kind = DECL_TLS_MODEL (decl);
  7214. id = TREE_VALUE (args);
  7215. if (TREE_CODE (id) != STRING_CST)
  7216. {
  7217. error ("tls_model argument not a string");
  7218. return NULL_TREE;
  7219. }
  7220. if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
  7221. kind = TLS_MODEL_LOCAL_EXEC;
  7222. else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
  7223. kind = TLS_MODEL_INITIAL_EXEC;
  7224. else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
  7225. kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
  7226. else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
  7227. kind = TLS_MODEL_GLOBAL_DYNAMIC;
  7228. else
  7229. error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
  7230. set_decl_tls_model (decl, kind);
  7231. return NULL_TREE;
  7232. }
  7233. /* Handle a "no_instrument_function" attribute; arguments as in
  7234. struct attribute_spec.handler. */
  7235. static tree
  7236. handle_no_instrument_function_attribute (tree *node, tree name,
  7237. tree ARG_UNUSED (args),
  7238. int ARG_UNUSED (flags),
  7239. bool *no_add_attrs)
  7240. {
  7241. tree decl = *node;
  7242. if (TREE_CODE (decl) != FUNCTION_DECL)
  7243. {
  7244. error_at (DECL_SOURCE_LOCATION (decl),
  7245. "%qE attribute applies only to functions", name);
  7246. *no_add_attrs = true;
  7247. }
  7248. else
  7249. DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
  7250. return NULL_TREE;
  7251. }
  7252. /* Handle a "malloc" attribute; arguments as in
  7253. struct attribute_spec.handler. */
  7254. static tree
  7255. handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  7256. int ARG_UNUSED (flags), bool *no_add_attrs)
  7257. {
  7258. if (TREE_CODE (*node) == FUNCTION_DECL
  7259. && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
  7260. DECL_IS_MALLOC (*node) = 1;
  7261. else
  7262. {
  7263. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7264. *no_add_attrs = true;
  7265. }
  7266. return NULL_TREE;
  7267. }
  7268. /* Handle a "alloc_size" attribute; arguments as in
  7269. struct attribute_spec.handler. */
  7270. static tree
  7271. handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
  7272. int ARG_UNUSED (flags), bool *no_add_attrs)
  7273. {
  7274. unsigned arg_count = type_num_arguments (*node);
  7275. for (; args; args = TREE_CHAIN (args))
  7276. {
  7277. tree position = TREE_VALUE (args);
  7278. if (position && TREE_CODE (position) != IDENTIFIER_NODE
  7279. && TREE_CODE (position) != FUNCTION_DECL)
  7280. position = default_conversion (position);
  7281. if (!tree_fits_uhwi_p (position)
  7282. || !arg_count
  7283. || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
  7284. {
  7285. warning (OPT_Wattributes,
  7286. "alloc_size parameter outside range");
  7287. *no_add_attrs = true;
  7288. return NULL_TREE;
  7289. }
  7290. }
  7291. return NULL_TREE;
  7292. }
  7293. /* Handle a "alloc_align" attribute; arguments as in
  7294. struct attribute_spec.handler. */
  7295. static tree
  7296. handle_alloc_align_attribute (tree *node, tree, tree args, int,
  7297. bool *no_add_attrs)
  7298. {
  7299. unsigned arg_count = type_num_arguments (*node);
  7300. tree position = TREE_VALUE (args);
  7301. if (position && TREE_CODE (position) != IDENTIFIER_NODE)
  7302. position = default_conversion (position);
  7303. if (!tree_fits_uhwi_p (position)
  7304. || !arg_count
  7305. || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
  7306. {
  7307. warning (OPT_Wattributes,
  7308. "alloc_align parameter outside range");
  7309. *no_add_attrs = true;
  7310. return NULL_TREE;
  7311. }
  7312. return NULL_TREE;
  7313. }
  7314. /* Handle a "assume_aligned" attribute; arguments as in
  7315. struct attribute_spec.handler. */
  7316. static tree
  7317. handle_assume_aligned_attribute (tree *, tree, tree args, int,
  7318. bool *no_add_attrs)
  7319. {
  7320. for (; args; args = TREE_CHAIN (args))
  7321. {
  7322. tree position = TREE_VALUE (args);
  7323. if (position && TREE_CODE (position) != IDENTIFIER_NODE
  7324. && TREE_CODE (position) != FUNCTION_DECL)
  7325. position = default_conversion (position);
  7326. if (TREE_CODE (position) != INTEGER_CST)
  7327. {
  7328. warning (OPT_Wattributes,
  7329. "assume_aligned parameter not integer constant");
  7330. *no_add_attrs = true;
  7331. return NULL_TREE;
  7332. }
  7333. }
  7334. return NULL_TREE;
  7335. }
  7336. /* Handle a "fn spec" attribute; arguments as in
  7337. struct attribute_spec.handler. */
  7338. static tree
  7339. handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
  7340. tree args, int ARG_UNUSED (flags),
  7341. bool *no_add_attrs ATTRIBUTE_UNUSED)
  7342. {
  7343. gcc_assert (args
  7344. && TREE_CODE (TREE_VALUE (args)) == STRING_CST
  7345. && !TREE_CHAIN (args));
  7346. return NULL_TREE;
  7347. }
  7348. /* Handle a "bnd_variable_size" attribute; arguments as in
  7349. struct attribute_spec.handler. */
  7350. static tree
  7351. handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  7352. int ARG_UNUSED (flags), bool *no_add_attrs)
  7353. {
  7354. if (TREE_CODE (*node) != FIELD_DECL)
  7355. {
  7356. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7357. *no_add_attrs = true;
  7358. }
  7359. return NULL_TREE;
  7360. }
  7361. /* Handle a "bnd_legacy" attribute; arguments as in
  7362. struct attribute_spec.handler. */
  7363. static tree
  7364. handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
  7365. int ARG_UNUSED (flags), bool *no_add_attrs)
  7366. {
  7367. if (TREE_CODE (*node) != FUNCTION_DECL)
  7368. {
  7369. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7370. *no_add_attrs = true;
  7371. }
  7372. return NULL_TREE;
  7373. }
  7374. /* Handle a "bnd_instrument" attribute; arguments as in
  7375. struct attribute_spec.handler. */
  7376. static tree
  7377. handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
  7378. int ARG_UNUSED (flags), bool *no_add_attrs)
  7379. {
  7380. if (TREE_CODE (*node) != FUNCTION_DECL)
  7381. {
  7382. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7383. *no_add_attrs = true;
  7384. }
  7385. return NULL_TREE;
  7386. }
  7387. /* Handle a "warn_unused" attribute; arguments as in
  7388. struct attribute_spec.handler. */
  7389. static tree
  7390. handle_warn_unused_attribute (tree *node, tree name,
  7391. tree args ATTRIBUTE_UNUSED,
  7392. int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
  7393. {
  7394. if (TYPE_P (*node))
  7395. /* Do nothing else, just set the attribute. We'll get at
  7396. it later with lookup_attribute. */
  7397. ;
  7398. else
  7399. {
  7400. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7401. *no_add_attrs = true;
  7402. }
  7403. return NULL_TREE;
  7404. }
  7405. /* Handle an "omp declare simd" attribute; arguments as in
  7406. struct attribute_spec.handler. */
  7407. static tree
  7408. handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
  7409. {
  7410. return NULL_TREE;
  7411. }
  7412. /* Handle an "omp declare target" attribute; arguments as in
  7413. struct attribute_spec.handler. */
  7414. static tree
  7415. handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
  7416. {
  7417. return NULL_TREE;
  7418. }
  7419. /* Handle a "returns_twice" attribute; arguments as in
  7420. struct attribute_spec.handler. */
  7421. static tree
  7422. handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  7423. int ARG_UNUSED (flags), bool *no_add_attrs)
  7424. {
  7425. if (TREE_CODE (*node) == FUNCTION_DECL)
  7426. DECL_IS_RETURNS_TWICE (*node) = 1;
  7427. else
  7428. {
  7429. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7430. *no_add_attrs = true;
  7431. }
  7432. return NULL_TREE;
  7433. }
  7434. /* Handle a "no_limit_stack" attribute; arguments as in
  7435. struct attribute_spec.handler. */
  7436. static tree
  7437. handle_no_limit_stack_attribute (tree *node, tree name,
  7438. tree ARG_UNUSED (args),
  7439. int ARG_UNUSED (flags),
  7440. bool *no_add_attrs)
  7441. {
  7442. tree decl = *node;
  7443. if (TREE_CODE (decl) != FUNCTION_DECL)
  7444. {
  7445. error_at (DECL_SOURCE_LOCATION (decl),
  7446. "%qE attribute applies only to functions", name);
  7447. *no_add_attrs = true;
  7448. }
  7449. else if (DECL_INITIAL (decl))
  7450. {
  7451. error_at (DECL_SOURCE_LOCATION (decl),
  7452. "can%'t set %qE attribute after definition", name);
  7453. *no_add_attrs = true;
  7454. }
  7455. else
  7456. DECL_NO_LIMIT_STACK (decl) = 1;
  7457. return NULL_TREE;
  7458. }
  7459. /* Handle a "pure" attribute; arguments as in
  7460. struct attribute_spec.handler. */
  7461. static tree
  7462. handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  7463. int ARG_UNUSED (flags), bool *no_add_attrs)
  7464. {
  7465. if (TREE_CODE (*node) == FUNCTION_DECL)
  7466. DECL_PURE_P (*node) = 1;
  7467. /* ??? TODO: Support types. */
  7468. else
  7469. {
  7470. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7471. *no_add_attrs = true;
  7472. }
  7473. return NULL_TREE;
  7474. }
  7475. /* Digest an attribute list destined for a transactional memory statement.
  7476. ALLOWED is the set of attributes that are allowed for this statement;
  7477. return the attribute we parsed. Multiple attributes are never allowed. */
  7478. int
  7479. parse_tm_stmt_attr (tree attrs, int allowed)
  7480. {
  7481. tree a_seen = NULL;
  7482. int m_seen = 0;
  7483. for ( ; attrs ; attrs = TREE_CHAIN (attrs))
  7484. {
  7485. tree a = TREE_PURPOSE (attrs);
  7486. int m = 0;
  7487. if (is_attribute_p ("outer", a))
  7488. m = TM_STMT_ATTR_OUTER;
  7489. if ((m & allowed) == 0)
  7490. {
  7491. warning (OPT_Wattributes, "%qE attribute directive ignored", a);
  7492. continue;
  7493. }
  7494. if (m_seen == 0)
  7495. {
  7496. a_seen = a;
  7497. m_seen = m;
  7498. }
  7499. else if (m_seen == m)
  7500. warning (OPT_Wattributes, "%qE attribute duplicated", a);
  7501. else
  7502. warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
  7503. }
  7504. return m_seen;
  7505. }
  7506. /* Transform a TM attribute name into a maskable integer and back.
  7507. Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
  7508. to how the lack of an attribute is treated. */
  7509. int
  7510. tm_attr_to_mask (tree attr)
  7511. {
  7512. if (attr == NULL)
  7513. return 0;
  7514. if (is_attribute_p ("transaction_safe", attr))
  7515. return TM_ATTR_SAFE;
  7516. if (is_attribute_p ("transaction_callable", attr))
  7517. return TM_ATTR_CALLABLE;
  7518. if (is_attribute_p ("transaction_pure", attr))
  7519. return TM_ATTR_PURE;
  7520. if (is_attribute_p ("transaction_unsafe", attr))
  7521. return TM_ATTR_IRREVOCABLE;
  7522. if (is_attribute_p ("transaction_may_cancel_outer", attr))
  7523. return TM_ATTR_MAY_CANCEL_OUTER;
  7524. return 0;
  7525. }
  7526. tree
  7527. tm_mask_to_attr (int mask)
  7528. {
  7529. const char *str;
  7530. switch (mask)
  7531. {
  7532. case TM_ATTR_SAFE:
  7533. str = "transaction_safe";
  7534. break;
  7535. case TM_ATTR_CALLABLE:
  7536. str = "transaction_callable";
  7537. break;
  7538. case TM_ATTR_PURE:
  7539. str = "transaction_pure";
  7540. break;
  7541. case TM_ATTR_IRREVOCABLE:
  7542. str = "transaction_unsafe";
  7543. break;
  7544. case TM_ATTR_MAY_CANCEL_OUTER:
  7545. str = "transaction_may_cancel_outer";
  7546. break;
  7547. default:
  7548. gcc_unreachable ();
  7549. }
  7550. return get_identifier (str);
  7551. }
  7552. /* Return the first TM attribute seen in LIST. */
  7553. tree
  7554. find_tm_attribute (tree list)
  7555. {
  7556. for (; list ; list = TREE_CHAIN (list))
  7557. {
  7558. tree name = TREE_PURPOSE (list);
  7559. if (tm_attr_to_mask (name) != 0)
  7560. return name;
  7561. }
  7562. return NULL_TREE;
  7563. }
  7564. /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
  7565. Here we accept only function types, and verify that none of the other
  7566. function TM attributes are also applied. */
  7567. /* ??? We need to accept class types for C++, but not C. This greatly
  7568. complicates this function, since we can no longer rely on the extra
  7569. processing given by function_type_required. */
  7570. static tree
  7571. handle_tm_attribute (tree *node, tree name, tree args,
  7572. int flags, bool *no_add_attrs)
  7573. {
  7574. /* Only one path adds the attribute; others don't. */
  7575. *no_add_attrs = true;
  7576. switch (TREE_CODE (*node))
  7577. {
  7578. case RECORD_TYPE:
  7579. case UNION_TYPE:
  7580. /* Only tm_callable and tm_safe apply to classes. */
  7581. if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
  7582. goto ignored;
  7583. /* FALLTHRU */
  7584. case FUNCTION_TYPE:
  7585. case METHOD_TYPE:
  7586. {
  7587. tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
  7588. if (old_name == name)
  7589. ;
  7590. else if (old_name != NULL_TREE)
  7591. error ("type was previously declared %qE", old_name);
  7592. else
  7593. *no_add_attrs = false;
  7594. }
  7595. break;
  7596. case POINTER_TYPE:
  7597. {
  7598. enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
  7599. if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
  7600. {
  7601. tree fn_tmp = TREE_TYPE (*node);
  7602. decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
  7603. *node = build_pointer_type (fn_tmp);
  7604. break;
  7605. }
  7606. }
  7607. /* FALLTHRU */
  7608. default:
  7609. /* If a function is next, pass it on to be tried next. */
  7610. if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
  7611. return tree_cons (name, args, NULL);
  7612. ignored:
  7613. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7614. break;
  7615. }
  7616. return NULL_TREE;
  7617. }
  7618. /* Handle the TM_WRAP attribute; arguments as in
  7619. struct attribute_spec.handler. */
  7620. static tree
  7621. handle_tm_wrap_attribute (tree *node, tree name, tree args,
  7622. int ARG_UNUSED (flags), bool *no_add_attrs)
  7623. {
  7624. tree decl = *node;
  7625. /* We don't need the attribute even on success, since we
  7626. record the entry in an external table. */
  7627. *no_add_attrs = true;
  7628. if (TREE_CODE (decl) != FUNCTION_DECL)
  7629. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7630. else
  7631. {
  7632. tree wrap_decl = TREE_VALUE (args);
  7633. if (error_operand_p (wrap_decl))
  7634. ;
  7635. else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
  7636. && TREE_CODE (wrap_decl) != VAR_DECL
  7637. && TREE_CODE (wrap_decl) != FUNCTION_DECL)
  7638. error ("%qE argument not an identifier", name);
  7639. else
  7640. {
  7641. if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
  7642. wrap_decl = lookup_name (wrap_decl);
  7643. if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
  7644. {
  7645. if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
  7646. TREE_TYPE (wrap_decl)))
  7647. record_tm_replacement (wrap_decl, decl);
  7648. else
  7649. error ("%qD is not compatible with %qD", wrap_decl, decl);
  7650. }
  7651. else
  7652. error ("%qE argument is not a function", name);
  7653. }
  7654. }
  7655. return NULL_TREE;
  7656. }
  7657. /* Ignore the given attribute. Used when this attribute may be usefully
  7658. overridden by the target, but is not used generically. */
  7659. static tree
  7660. ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
  7661. tree ARG_UNUSED (args), int ARG_UNUSED (flags),
  7662. bool *no_add_attrs)
  7663. {
  7664. *no_add_attrs = true;
  7665. return NULL_TREE;
  7666. }
  7667. /* Handle a "no vops" attribute; arguments as in
  7668. struct attribute_spec.handler. */
  7669. static tree
  7670. handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
  7671. tree ARG_UNUSED (args), int ARG_UNUSED (flags),
  7672. bool *ARG_UNUSED (no_add_attrs))
  7673. {
  7674. gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
  7675. DECL_IS_NOVOPS (*node) = 1;
  7676. return NULL_TREE;
  7677. }
  7678. /* Handle a "deprecated" attribute; arguments as in
  7679. struct attribute_spec.handler. */
  7680. static tree
  7681. handle_deprecated_attribute (tree *node, tree name,
  7682. tree args, int flags,
  7683. bool *no_add_attrs)
  7684. {
  7685. tree type = NULL_TREE;
  7686. int warn = 0;
  7687. tree what = NULL_TREE;
  7688. if (!args)
  7689. *no_add_attrs = true;
  7690. else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
  7691. {
  7692. error ("deprecated message is not a string");
  7693. *no_add_attrs = true;
  7694. }
  7695. if (DECL_P (*node))
  7696. {
  7697. tree decl = *node;
  7698. type = TREE_TYPE (decl);
  7699. if (TREE_CODE (decl) == TYPE_DECL
  7700. || TREE_CODE (decl) == PARM_DECL
  7701. || TREE_CODE (decl) == VAR_DECL
  7702. || TREE_CODE (decl) == FUNCTION_DECL
  7703. || TREE_CODE (decl) == FIELD_DECL
  7704. || objc_method_decl (TREE_CODE (decl)))
  7705. TREE_DEPRECATED (decl) = 1;
  7706. else
  7707. warn = 1;
  7708. }
  7709. else if (TYPE_P (*node))
  7710. {
  7711. if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
  7712. *node = build_variant_type_copy (*node);
  7713. TREE_DEPRECATED (*node) = 1;
  7714. type = *node;
  7715. }
  7716. else
  7717. warn = 1;
  7718. if (warn)
  7719. {
  7720. *no_add_attrs = true;
  7721. if (type && TYPE_NAME (type))
  7722. {
  7723. if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
  7724. what = TYPE_NAME (*node);
  7725. else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  7726. && DECL_NAME (TYPE_NAME (type)))
  7727. what = DECL_NAME (TYPE_NAME (type));
  7728. }
  7729. if (what)
  7730. warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
  7731. else
  7732. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7733. }
  7734. return NULL_TREE;
  7735. }
  7736. /* Handle a "vector_size" attribute; arguments as in
  7737. struct attribute_spec.handler. */
  7738. static tree
  7739. handle_vector_size_attribute (tree *node, tree name, tree args,
  7740. int ARG_UNUSED (flags),
  7741. bool *no_add_attrs)
  7742. {
  7743. unsigned HOST_WIDE_INT vecsize, nunits;
  7744. machine_mode orig_mode;
  7745. tree type = *node, new_type, size;
  7746. *no_add_attrs = true;
  7747. size = TREE_VALUE (args);
  7748. if (size && TREE_CODE (size) != IDENTIFIER_NODE
  7749. && TREE_CODE (size) != FUNCTION_DECL)
  7750. size = default_conversion (size);
  7751. if (!tree_fits_uhwi_p (size))
  7752. {
  7753. warning (OPT_Wattributes, "%qE attribute ignored", name);
  7754. return NULL_TREE;
  7755. }
  7756. /* Get the vector size (in bytes). */
  7757. vecsize = tree_to_uhwi (size);
  7758. /* We need to provide for vector pointers, vector arrays, and
  7759. functions returning vectors. For example:
  7760. __attribute__((vector_size(16))) short *foo;
  7761. In this case, the mode is SI, but the type being modified is
  7762. HI, so we need to look further. */
  7763. while (POINTER_TYPE_P (type)
  7764. || TREE_CODE (type) == FUNCTION_TYPE
  7765. || TREE_CODE (type) == METHOD_TYPE
  7766. || TREE_CODE (type) == ARRAY_TYPE
  7767. || TREE_CODE (type) == OFFSET_TYPE)
  7768. type = TREE_TYPE (type);
  7769. /* Get the mode of the type being modified. */
  7770. orig_mode = TYPE_MODE (type);
  7771. if ((!INTEGRAL_TYPE_P (type)
  7772. && !SCALAR_FLOAT_TYPE_P (type)
  7773. && !FIXED_POINT_TYPE_P (type))
  7774. || (!SCALAR_FLOAT_MODE_P (orig_mode)
  7775. && GET_MODE_CLASS (orig_mode) != MODE_INT
  7776. && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
  7777. || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
  7778. || TREE_CODE (type) == BOOLEAN_TYPE)
  7779. {
  7780. error ("invalid vector type for attribute %qE", name);
  7781. return NULL_TREE;
  7782. }
  7783. if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
  7784. {
  7785. error ("vector size not an integral multiple of component size");
  7786. return NULL;
  7787. }
  7788. if (vecsize == 0)
  7789. {
  7790. error ("zero vector size");
  7791. return NULL;
  7792. }
  7793. /* Calculate how many units fit in the vector. */
  7794. nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
  7795. if (nunits & (nunits - 1))
  7796. {
  7797. error ("number of components of the vector not a power of two");
  7798. return NULL_TREE;
  7799. }
  7800. new_type = build_vector_type (type, nunits);
  7801. /* Build back pointers if needed. */
  7802. *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
  7803. return NULL_TREE;
  7804. }
  7805. /* Handle the "nonnull" attribute. */
  7806. static tree
  7807. handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
  7808. tree args, int ARG_UNUSED (flags),
  7809. bool *no_add_attrs)
  7810. {
  7811. tree type = *node;
  7812. unsigned HOST_WIDE_INT attr_arg_num;
  7813. /* If no arguments are specified, all pointer arguments should be
  7814. non-null. Verify a full prototype is given so that the arguments
  7815. will have the correct types when we actually check them later. */
  7816. if (!args)
  7817. {
  7818. if (!prototype_p (type))
  7819. {
  7820. error ("nonnull attribute without arguments on a non-prototype");
  7821. *no_add_attrs = true;
  7822. }
  7823. return NULL_TREE;
  7824. }
  7825. /* Argument list specified. Verify that each argument number references
  7826. a pointer argument. */
  7827. for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
  7828. {
  7829. unsigned HOST_WIDE_INT arg_num = 0, ck_num;
  7830. tree arg = TREE_VALUE (args);
  7831. if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
  7832. && TREE_CODE (arg) != FUNCTION_DECL)
  7833. arg = default_conversion (arg);
  7834. if (!get_nonnull_operand (arg, &arg_num))
  7835. {
  7836. error ("nonnull argument has invalid operand number (argument %lu)",
  7837. (unsigned long) attr_arg_num);
  7838. *no_add_attrs = true;
  7839. return NULL_TREE;
  7840. }
  7841. if (prototype_p (type))
  7842. {
  7843. function_args_iterator iter;
  7844. tree argument;
  7845. function_args_iter_init (&iter, type);
  7846. for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
  7847. {
  7848. argument = function_args_iter_cond (&iter);
  7849. if (argument == NULL_TREE || ck_num == arg_num)
  7850. break;
  7851. }
  7852. if (!argument
  7853. || TREE_CODE (argument) == VOID_TYPE)
  7854. {
  7855. error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
  7856. (unsigned long) attr_arg_num, (unsigned long) arg_num);
  7857. *no_add_attrs = true;
  7858. return NULL_TREE;
  7859. }
  7860. if (TREE_CODE (argument) != POINTER_TYPE)
  7861. {
  7862. error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
  7863. (unsigned long) attr_arg_num, (unsigned long) arg_num);
  7864. *no_add_attrs = true;
  7865. return NULL_TREE;
  7866. }
  7867. }
  7868. }
  7869. return NULL_TREE;
  7870. }
  7871. /* Check the argument list of a function call for null in argument slots
  7872. that are marked as requiring a non-null pointer argument. The NARGS
  7873. arguments are passed in the array ARGARRAY.
  7874. */
  7875. static void
  7876. check_function_nonnull (tree attrs, int nargs, tree *argarray)
  7877. {
  7878. tree a;
  7879. int i;
  7880. attrs = lookup_attribute ("nonnull", attrs);
  7881. if (attrs == NULL_TREE)
  7882. return;
  7883. a = attrs;
  7884. /* See if any of the nonnull attributes has no arguments. If so,
  7885. then every pointer argument is checked (in which case the check
  7886. for pointer type is done in check_nonnull_arg). */
  7887. if (TREE_VALUE (a) != NULL_TREE)
  7888. do
  7889. a = lookup_attribute ("nonnull", TREE_CHAIN (a));
  7890. while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
  7891. if (a != NULL_TREE)
  7892. for (i = 0; i < nargs; i++)
  7893. check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
  7894. i + 1);
  7895. else
  7896. {
  7897. /* Walk the argument list. If we encounter an argument number we
  7898. should check for non-null, do it. */
  7899. for (i = 0; i < nargs; i++)
  7900. {
  7901. for (a = attrs; ; a = TREE_CHAIN (a))
  7902. {
  7903. a = lookup_attribute ("nonnull", a);
  7904. if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
  7905. break;
  7906. }
  7907. if (a != NULL_TREE)
  7908. check_function_arguments_recurse (check_nonnull_arg, NULL,
  7909. argarray[i], i + 1);
  7910. }
  7911. }
  7912. }
  7913. /* Check that the Nth argument of a function call (counting backwards
  7914. from the end) is a (pointer)0. The NARGS arguments are passed in the
  7915. array ARGARRAY. */
  7916. static void
  7917. check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
  7918. {
  7919. tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
  7920. if (attr)
  7921. {
  7922. int len = 0;
  7923. int pos = 0;
  7924. tree sentinel;
  7925. function_args_iterator iter;
  7926. tree t;
  7927. /* Skip over the named arguments. */
  7928. FOREACH_FUNCTION_ARGS (fntype, t, iter)
  7929. {
  7930. if (len == nargs)
  7931. break;
  7932. len++;
  7933. }
  7934. if (TREE_VALUE (attr))
  7935. {
  7936. tree p = TREE_VALUE (TREE_VALUE (attr));
  7937. pos = TREE_INT_CST_LOW (p);
  7938. }
  7939. /* The sentinel must be one of the varargs, i.e.
  7940. in position >= the number of fixed arguments. */
  7941. if ((nargs - 1 - pos) < len)
  7942. {
  7943. warning (OPT_Wformat_,
  7944. "not enough variable arguments to fit a sentinel");
  7945. return;
  7946. }
  7947. /* Validate the sentinel. */
  7948. sentinel = argarray[nargs - 1 - pos];
  7949. if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
  7950. || !integer_zerop (sentinel))
  7951. /* Although __null (in C++) is only an integer we allow it
  7952. nevertheless, as we are guaranteed that it's exactly
  7953. as wide as a pointer, and we don't want to force
  7954. users to cast the NULL they have written there.
  7955. We warn with -Wstrict-null-sentinel, though. */
  7956. && (warn_strict_null_sentinel || null_node != sentinel))
  7957. warning (OPT_Wformat_, "missing sentinel in function call");
  7958. }
  7959. }
  7960. /* Helper for check_function_nonnull; given a list of operands which
  7961. must be non-null in ARGS, determine if operand PARAM_NUM should be
  7962. checked. */
  7963. static bool
  7964. nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
  7965. {
  7966. unsigned HOST_WIDE_INT arg_num = 0;
  7967. for (; args; args = TREE_CHAIN (args))
  7968. {
  7969. bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
  7970. gcc_assert (found);
  7971. if (arg_num == param_num)
  7972. return true;
  7973. }
  7974. return false;
  7975. }
  7976. /* Check that the function argument PARAM (which is operand number
  7977. PARAM_NUM) is non-null. This is called by check_function_nonnull
  7978. via check_function_arguments_recurse. */
  7979. static void
  7980. check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
  7981. unsigned HOST_WIDE_INT param_num)
  7982. {
  7983. /* Just skip checking the argument if it's not a pointer. This can
  7984. happen if the "nonnull" attribute was given without an operand
  7985. list (which means to check every pointer argument). */
  7986. if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
  7987. return;
  7988. if (integer_zerop (param))
  7989. warning (OPT_Wnonnull, "null argument where non-null required "
  7990. "(argument %lu)", (unsigned long) param_num);
  7991. }
  7992. /* Helper for nonnull attribute handling; fetch the operand number
  7993. from the attribute argument list. */
  7994. static bool
  7995. get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
  7996. {
  7997. /* Verify the arg number is a small constant. */
  7998. if (tree_fits_uhwi_p (arg_num_expr))
  7999. {
  8000. *valp = TREE_INT_CST_LOW (arg_num_expr);
  8001. return true;
  8002. }
  8003. else
  8004. return false;
  8005. }
  8006. /* Handle a "nothrow" attribute; arguments as in
  8007. struct attribute_spec.handler. */
  8008. static tree
  8009. handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
  8010. int ARG_UNUSED (flags), bool *no_add_attrs)
  8011. {
  8012. if (TREE_CODE (*node) == FUNCTION_DECL)
  8013. TREE_NOTHROW (*node) = 1;
  8014. /* ??? TODO: Support types. */
  8015. else
  8016. {
  8017. warning (OPT_Wattributes, "%qE attribute ignored", name);
  8018. *no_add_attrs = true;
  8019. }
  8020. return NULL_TREE;
  8021. }
  8022. /* Handle a "cleanup" attribute; arguments as in
  8023. struct attribute_spec.handler. */
  8024. static tree
  8025. handle_cleanup_attribute (tree *node, tree name, tree args,
  8026. int ARG_UNUSED (flags), bool *no_add_attrs)
  8027. {
  8028. tree decl = *node;
  8029. tree cleanup_id, cleanup_decl;
  8030. /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
  8031. for global destructors in C++. This requires infrastructure that
  8032. we don't have generically at the moment. It's also not a feature
  8033. we'd be missing too much, since we do have attribute constructor. */
  8034. if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
  8035. {
  8036. warning (OPT_Wattributes, "%qE attribute ignored", name);
  8037. *no_add_attrs = true;
  8038. return NULL_TREE;
  8039. }
  8040. /* Verify that the argument is a function in scope. */
  8041. /* ??? We could support pointers to functions here as well, if
  8042. that was considered desirable. */
  8043. cleanup_id = TREE_VALUE (args);
  8044. if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
  8045. {
  8046. error ("cleanup argument not an identifier");
  8047. *no_add_attrs = true;
  8048. return NULL_TREE;
  8049. }
  8050. cleanup_decl = lookup_name (cleanup_id);
  8051. if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
  8052. {
  8053. error ("cleanup argument not a function");
  8054. *no_add_attrs = true;
  8055. return NULL_TREE;
  8056. }
  8057. /* That the function has proper type is checked with the
  8058. eventual call to build_function_call. */
  8059. return NULL_TREE;
  8060. }
  8061. /* Handle a "warn_unused_result" attribute. No special handling. */
  8062. static tree
  8063. handle_warn_unused_result_attribute (tree *node, tree name,
  8064. tree ARG_UNUSED (args),
  8065. int ARG_UNUSED (flags), bool *no_add_attrs)
  8066. {
  8067. /* Ignore the attribute for functions not returning any value. */
  8068. if (VOID_TYPE_P (TREE_TYPE (*node)))
  8069. {
  8070. warning (OPT_Wattributes, "%qE attribute ignored", name);
  8071. *no_add_attrs = true;
  8072. }
  8073. return NULL_TREE;
  8074. }
  8075. /* Handle a "sentinel" attribute. */
  8076. static tree
  8077. handle_sentinel_attribute (tree *node, tree name, tree args,
  8078. int ARG_UNUSED (flags), bool *no_add_attrs)
  8079. {
  8080. if (!prototype_p (*node))
  8081. {
  8082. warning (OPT_Wattributes,
  8083. "%qE attribute requires prototypes with named arguments", name);
  8084. *no_add_attrs = true;
  8085. }
  8086. else
  8087. {
  8088. if (!stdarg_p (*node))
  8089. {
  8090. warning (OPT_Wattributes,
  8091. "%qE attribute only applies to variadic functions", name);
  8092. *no_add_attrs = true;
  8093. }
  8094. }
  8095. if (args)
  8096. {
  8097. tree position = TREE_VALUE (args);
  8098. if (position && TREE_CODE (position) != IDENTIFIER_NODE
  8099. && TREE_CODE (position) != FUNCTION_DECL)
  8100. position = default_conversion (position);
  8101. if (TREE_CODE (position) != INTEGER_CST
  8102. || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
  8103. {
  8104. warning (OPT_Wattributes,
  8105. "requested position is not an integer constant");
  8106. *no_add_attrs = true;
  8107. }
  8108. else
  8109. {
  8110. if (tree_int_cst_lt (position, integer_zero_node))
  8111. {
  8112. warning (OPT_Wattributes,
  8113. "requested position is less than zero");
  8114. *no_add_attrs = true;
  8115. }
  8116. }
  8117. }
  8118. return NULL_TREE;
  8119. }
  8120. /* Handle a "type_generic" attribute. */
  8121. static tree
  8122. handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
  8123. tree ARG_UNUSED (args), int ARG_UNUSED (flags),
  8124. bool * ARG_UNUSED (no_add_attrs))
  8125. {
  8126. /* Ensure we have a function type. */
  8127. gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
  8128. /* Ensure we have a variadic function. */
  8129. gcc_assert (!prototype_p (*node) || stdarg_p (*node));
  8130. return NULL_TREE;
  8131. }
  8132. /* Handle a "target" attribute. */
  8133. static tree
  8134. handle_target_attribute (tree *node, tree name, tree args, int flags,
  8135. bool *no_add_attrs)
  8136. {
  8137. /* Ensure we have a function type. */
  8138. if (TREE_CODE (*node) != FUNCTION_DECL)
  8139. {
  8140. warning (OPT_Wattributes, "%qE attribute ignored", name);
  8141. *no_add_attrs = true;
  8142. }
  8143. else if (! targetm.target_option.valid_attribute_p (*node, name, args,
  8144. flags))
  8145. *no_add_attrs = true;
  8146. return NULL_TREE;
  8147. }
  8148. /* Arguments being collected for optimization. */
  8149. typedef const char *const_char_p; /* For DEF_VEC_P. */
  8150. static GTY(()) vec<const_char_p, va_gc> *optimize_args;
  8151. /* Inner function to convert a TREE_LIST to argv string to parse the optimize
  8152. options in ARGS. ATTR_P is true if this is for attribute(optimize), and
  8153. false for #pragma GCC optimize. */
  8154. bool
  8155. parse_optimize_options (tree args, bool attr_p)
  8156. {
  8157. bool ret = true;
  8158. unsigned opt_argc;
  8159. unsigned i;
  8160. int saved_flag_strict_aliasing;
  8161. const char **opt_argv;
  8162. struct cl_decoded_option *decoded_options;
  8163. unsigned int decoded_options_count;
  8164. tree ap;
  8165. /* Build up argv vector. Just in case the string is stored away, use garbage
  8166. collected strings. */
  8167. vec_safe_truncate (optimize_args, 0);
  8168. vec_safe_push (optimize_args, (const char *) NULL);
  8169. for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
  8170. {
  8171. tree value = TREE_VALUE (ap);
  8172. if (TREE_CODE (value) == INTEGER_CST)
  8173. {
  8174. char buffer[20];
  8175. sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
  8176. vec_safe_push (optimize_args, ggc_strdup (buffer));
  8177. }
  8178. else if (TREE_CODE (value) == STRING_CST)
  8179. {
  8180. /* Split string into multiple substrings. */
  8181. size_t len = TREE_STRING_LENGTH (value);
  8182. char *p = ASTRDUP (TREE_STRING_POINTER (value));
  8183. char *end = p + len;
  8184. char *comma;
  8185. char *next_p = p;
  8186. while (next_p != NULL)
  8187. {
  8188. size_t len2;
  8189. char *q, *r;
  8190. p = next_p;
  8191. comma = strchr (p, ',');
  8192. if (comma)
  8193. {
  8194. len2 = comma - p;
  8195. *comma = '\0';
  8196. next_p = comma+1;
  8197. }
  8198. else
  8199. {
  8200. len2 = end - p;
  8201. next_p = NULL;
  8202. }
  8203. r = q = (char *) ggc_alloc_atomic (len2 + 3);
  8204. /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
  8205. options. */
  8206. if (*p == '-' && p[1] != 'O' && p[1] != 'f')
  8207. {
  8208. ret = false;
  8209. if (attr_p)
  8210. warning (OPT_Wattributes,
  8211. "bad option %s to optimize attribute", p);
  8212. else
  8213. warning (OPT_Wpragmas,
  8214. "bad option %s to pragma attribute", p);
  8215. continue;
  8216. }
  8217. if (*p != '-')
  8218. {
  8219. *r++ = '-';
  8220. /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
  8221. itself is -Os, and any other switch begins with a -f. */
  8222. if ((*p >= '0' && *p <= '9')
  8223. || (p[0] == 's' && p[1] == '\0'))
  8224. *r++ = 'O';
  8225. else if (*p != 'O')
  8226. *r++ = 'f';
  8227. }
  8228. memcpy (r, p, len2);
  8229. r[len2] = '\0';
  8230. vec_safe_push (optimize_args, (const char *) q);
  8231. }
  8232. }
  8233. }
  8234. opt_argc = optimize_args->length ();
  8235. opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
  8236. for (i = 1; i < opt_argc; i++)
  8237. opt_argv[i] = (*optimize_args)[i];
  8238. saved_flag_strict_aliasing = flag_strict_aliasing;
  8239. /* Now parse the options. */
  8240. decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
  8241. &decoded_options,
  8242. &decoded_options_count);
  8243. decode_options (&global_options, &global_options_set,
  8244. decoded_options, decoded_options_count,
  8245. input_location, global_dc);
  8246. targetm.override_options_after_change();
  8247. /* Don't allow changing -fstrict-aliasing. */
  8248. flag_strict_aliasing = saved_flag_strict_aliasing;
  8249. optimize_args->truncate (0);
  8250. return ret;
  8251. }
  8252. /* For handling "optimize" attribute. arguments as in
  8253. struct attribute_spec.handler. */
  8254. static tree
  8255. handle_optimize_attribute (tree *node, tree name, tree args,
  8256. int ARG_UNUSED (flags), bool *no_add_attrs)
  8257. {
  8258. /* Ensure we have a function type. */
  8259. if (TREE_CODE (*node) != FUNCTION_DECL)
  8260. {
  8261. warning (OPT_Wattributes, "%qE attribute ignored", name);
  8262. *no_add_attrs = true;
  8263. }
  8264. else
  8265. {
  8266. struct cl_optimization cur_opts;
  8267. tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
  8268. /* Save current options. */
  8269. cl_optimization_save (&cur_opts, &global_options);
  8270. /* If we previously had some optimization options, use them as the
  8271. default. */
  8272. if (old_opts)
  8273. cl_optimization_restore (&global_options,
  8274. TREE_OPTIMIZATION (old_opts));
  8275. /* Parse options, and update the vector. */
  8276. parse_optimize_options (args, true);
  8277. DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
  8278. = build_optimization_node (&global_options);
  8279. /* Restore current options. */
  8280. cl_optimization_restore (&global_options, &cur_opts);
  8281. }
  8282. return NULL_TREE;
  8283. }
  8284. /* Handle a "no_split_stack" attribute. */
  8285. static tree
  8286. handle_no_split_stack_attribute (tree *node, tree name,
  8287. tree ARG_UNUSED (args),
  8288. int ARG_UNUSED (flags),
  8289. bool *no_add_attrs)
  8290. {
  8291. tree decl = *node;
  8292. if (TREE_CODE (decl) != FUNCTION_DECL)
  8293. {
  8294. error_at (DECL_SOURCE_LOCATION (decl),
  8295. "%qE attribute applies only to functions", name);
  8296. *no_add_attrs = true;
  8297. }
  8298. else if (DECL_INITIAL (decl))
  8299. {
  8300. error_at (DECL_SOURCE_LOCATION (decl),
  8301. "can%'t set %qE attribute after definition", name);
  8302. *no_add_attrs = true;
  8303. }
  8304. return NULL_TREE;
  8305. }
  8306. /* Handle a "returns_nonnull" attribute; arguments as in
  8307. struct attribute_spec.handler. */
  8308. static tree
  8309. handle_returns_nonnull_attribute (tree *node, tree, tree, int,
  8310. bool *no_add_attrs)
  8311. {
  8312. // Even without a prototype we still have a return type we can check.
  8313. if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
  8314. {
  8315. error ("returns_nonnull attribute on a function not returning a pointer");
  8316. *no_add_attrs = true;
  8317. }
  8318. return NULL_TREE;
  8319. }
  8320. /* Handle a "designated_init" attribute; arguments as in
  8321. struct attribute_spec.handler. */
  8322. static tree
  8323. handle_designated_init_attribute (tree *node, tree name, tree, int,
  8324. bool *no_add_attrs)
  8325. {
  8326. if (TREE_CODE (*node) != RECORD_TYPE)
  8327. {
  8328. error ("%qE attribute is only valid on %<struct%> type", name);
  8329. *no_add_attrs = true;
  8330. }
  8331. return NULL_TREE;
  8332. }
  8333. /* Check for valid arguments being passed to a function with FNTYPE.
  8334. There are NARGS arguments in the array ARGARRAY. */
  8335. void
  8336. check_function_arguments (const_tree fntype, int nargs, tree *argarray)
  8337. {
  8338. /* Check for null being passed in a pointer argument that must be
  8339. non-null. We also need to do this if format checking is enabled. */
  8340. if (warn_nonnull)
  8341. check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
  8342. /* Check for errors in format strings. */
  8343. if (warn_format || warn_suggest_attribute_format)
  8344. check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
  8345. if (warn_format)
  8346. check_function_sentinel (fntype, nargs, argarray);
  8347. }
  8348. /* Generic argument checking recursion routine. PARAM is the argument to
  8349. be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
  8350. once the argument is resolved. CTX is context for the callback. */
  8351. void
  8352. check_function_arguments_recurse (void (*callback)
  8353. (void *, tree, unsigned HOST_WIDE_INT),
  8354. void *ctx, tree param,
  8355. unsigned HOST_WIDE_INT param_num)
  8356. {
  8357. if (CONVERT_EXPR_P (param)
  8358. && (TYPE_PRECISION (TREE_TYPE (param))
  8359. == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
  8360. {
  8361. /* Strip coercion. */
  8362. check_function_arguments_recurse (callback, ctx,
  8363. TREE_OPERAND (param, 0), param_num);
  8364. return;
  8365. }
  8366. if (TREE_CODE (param) == CALL_EXPR)
  8367. {
  8368. tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
  8369. tree attrs;
  8370. bool found_format_arg = false;
  8371. /* See if this is a call to a known internationalization function
  8372. that modifies a format arg. Such a function may have multiple
  8373. format_arg attributes (for example, ngettext). */
  8374. for (attrs = TYPE_ATTRIBUTES (type);
  8375. attrs;
  8376. attrs = TREE_CHAIN (attrs))
  8377. if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
  8378. {
  8379. tree inner_arg;
  8380. tree format_num_expr;
  8381. int format_num;
  8382. int i;
  8383. call_expr_arg_iterator iter;
  8384. /* Extract the argument number, which was previously checked
  8385. to be valid. */
  8386. format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
  8387. format_num = tree_to_uhwi (format_num_expr);
  8388. for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
  8389. inner_arg != 0;
  8390. inner_arg = next_call_expr_arg (&iter), i++)
  8391. if (i == format_num)
  8392. {
  8393. check_function_arguments_recurse (callback, ctx,
  8394. inner_arg, param_num);
  8395. found_format_arg = true;
  8396. break;
  8397. }
  8398. }
  8399. /* If we found a format_arg attribute and did a recursive check,
  8400. we are done with checking this argument. Otherwise, we continue
  8401. and this will be considered a non-literal. */
  8402. if (found_format_arg)
  8403. return;
  8404. }
  8405. if (TREE_CODE (param) == COND_EXPR)
  8406. {
  8407. /* Check both halves of the conditional expression. */
  8408. check_function_arguments_recurse (callback, ctx,
  8409. TREE_OPERAND (param, 1), param_num);
  8410. check_function_arguments_recurse (callback, ctx,
  8411. TREE_OPERAND (param, 2), param_num);
  8412. return;
  8413. }
  8414. (*callback) (ctx, param, param_num);
  8415. }
  8416. /* Checks for a builtin function FNDECL that the number of arguments
  8417. NARGS against the required number REQUIRED and issues an error if
  8418. there is a mismatch. Returns true if the number of arguments is
  8419. correct, otherwise false. */
  8420. static bool
  8421. builtin_function_validate_nargs (tree fndecl, int nargs, int required)
  8422. {
  8423. if (nargs < required)
  8424. {
  8425. error_at (input_location,
  8426. "not enough arguments to function %qE", fndecl);
  8427. return false;
  8428. }
  8429. else if (nargs > required)
  8430. {
  8431. error_at (input_location,
  8432. "too many arguments to function %qE", fndecl);
  8433. return false;
  8434. }
  8435. return true;
  8436. }
  8437. /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
  8438. Returns false if there was an error, otherwise true. */
  8439. bool
  8440. check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
  8441. {
  8442. if (!DECL_BUILT_IN (fndecl)
  8443. || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
  8444. return true;
  8445. switch (DECL_FUNCTION_CODE (fndecl))
  8446. {
  8447. case BUILT_IN_CONSTANT_P:
  8448. return builtin_function_validate_nargs (fndecl, nargs, 1);
  8449. case BUILT_IN_ISFINITE:
  8450. case BUILT_IN_ISINF:
  8451. case BUILT_IN_ISINF_SIGN:
  8452. case BUILT_IN_ISNAN:
  8453. case BUILT_IN_ISNORMAL:
  8454. if (builtin_function_validate_nargs (fndecl, nargs, 1))
  8455. {
  8456. if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
  8457. {
  8458. error ("non-floating-point argument in call to "
  8459. "function %qE", fndecl);
  8460. return false;
  8461. }
  8462. return true;
  8463. }
  8464. return false;
  8465. case BUILT_IN_ISGREATER:
  8466. case BUILT_IN_ISGREATEREQUAL:
  8467. case BUILT_IN_ISLESS:
  8468. case BUILT_IN_ISLESSEQUAL:
  8469. case BUILT_IN_ISLESSGREATER:
  8470. case BUILT_IN_ISUNORDERED:
  8471. if (builtin_function_validate_nargs (fndecl, nargs, 2))
  8472. {
  8473. enum tree_code code0, code1;
  8474. code0 = TREE_CODE (TREE_TYPE (args[0]));
  8475. code1 = TREE_CODE (TREE_TYPE (args[1]));
  8476. if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
  8477. || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
  8478. || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
  8479. {
  8480. error ("non-floating-point arguments in call to "
  8481. "function %qE", fndecl);
  8482. return false;
  8483. }
  8484. return true;
  8485. }
  8486. return false;
  8487. case BUILT_IN_FPCLASSIFY:
  8488. if (builtin_function_validate_nargs (fndecl, nargs, 6))
  8489. {
  8490. unsigned i;
  8491. for (i=0; i<5; i++)
  8492. if (TREE_CODE (args[i]) != INTEGER_CST)
  8493. {
  8494. error ("non-const integer argument %u in call to function %qE",
  8495. i+1, fndecl);
  8496. return false;
  8497. }
  8498. if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
  8499. {
  8500. error ("non-floating-point argument in call to function %qE",
  8501. fndecl);
  8502. return false;
  8503. }
  8504. return true;
  8505. }
  8506. return false;
  8507. case BUILT_IN_ASSUME_ALIGNED:
  8508. if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
  8509. {
  8510. if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
  8511. {
  8512. error ("non-integer argument 3 in call to function %qE", fndecl);
  8513. return false;
  8514. }
  8515. return true;
  8516. }
  8517. return false;
  8518. case BUILT_IN_ADD_OVERFLOW:
  8519. case BUILT_IN_SUB_OVERFLOW:
  8520. case BUILT_IN_MUL_OVERFLOW:
  8521. if (builtin_function_validate_nargs (fndecl, nargs, 3))
  8522. {
  8523. unsigned i;
  8524. for (i = 0; i < 2; i++)
  8525. if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
  8526. {
  8527. error ("argument %u in call to function %qE does not have "
  8528. "integral type", i + 1, fndecl);
  8529. return false;
  8530. }
  8531. if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
  8532. || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
  8533. {
  8534. error ("argument 3 in call to function %qE does not have "
  8535. "pointer to integer type", fndecl);
  8536. return false;
  8537. }
  8538. return true;
  8539. }
  8540. return false;
  8541. default:
  8542. return true;
  8543. }
  8544. }
  8545. /* Function to help qsort sort FIELD_DECLs by name order. */
  8546. int
  8547. field_decl_cmp (const void *x_p, const void *y_p)
  8548. {
  8549. const tree *const x = (const tree *const) x_p;
  8550. const tree *const y = (const tree *const) y_p;
  8551. if (DECL_NAME (*x) == DECL_NAME (*y))
  8552. /* A nontype is "greater" than a type. */
  8553. return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
  8554. if (DECL_NAME (*x) == NULL_TREE)
  8555. return -1;
  8556. if (DECL_NAME (*y) == NULL_TREE)
  8557. return 1;
  8558. if (DECL_NAME (*x) < DECL_NAME (*y))
  8559. return -1;
  8560. return 1;
  8561. }
  8562. static struct {
  8563. gt_pointer_operator new_value;
  8564. void *cookie;
  8565. } resort_data;
  8566. /* This routine compares two fields like field_decl_cmp but using the
  8567. pointer operator in resort_data. */
  8568. static int
  8569. resort_field_decl_cmp (const void *x_p, const void *y_p)
  8570. {
  8571. const tree *const x = (const tree *const) x_p;
  8572. const tree *const y = (const tree *const) y_p;
  8573. if (DECL_NAME (*x) == DECL_NAME (*y))
  8574. /* A nontype is "greater" than a type. */
  8575. return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
  8576. if (DECL_NAME (*x) == NULL_TREE)
  8577. return -1;
  8578. if (DECL_NAME (*y) == NULL_TREE)
  8579. return 1;
  8580. {
  8581. tree d1 = DECL_NAME (*x);
  8582. tree d2 = DECL_NAME (*y);
  8583. resort_data.new_value (&d1, resort_data.cookie);
  8584. resort_data.new_value (&d2, resort_data.cookie);
  8585. if (d1 < d2)
  8586. return -1;
  8587. }
  8588. return 1;
  8589. }
  8590. /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
  8591. void
  8592. resort_sorted_fields (void *obj,
  8593. void * ARG_UNUSED (orig_obj),
  8594. gt_pointer_operator new_value,
  8595. void *cookie)
  8596. {
  8597. struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
  8598. resort_data.new_value = new_value;
  8599. resort_data.cookie = cookie;
  8600. qsort (&sf->elts[0], sf->len, sizeof (tree),
  8601. resort_field_decl_cmp);
  8602. }
  8603. /* Subroutine of c_parse_error.
  8604. Return the result of concatenating LHS and RHS. RHS is really
  8605. a string literal, its first character is indicated by RHS_START and
  8606. RHS_SIZE is its length (including the terminating NUL character).
  8607. The caller is responsible for deleting the returned pointer. */
  8608. static char *
  8609. catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
  8610. {
  8611. const int lhs_size = strlen (lhs);
  8612. char *result = XNEWVEC (char, lhs_size + rhs_size);
  8613. strncpy (result, lhs, lhs_size);
  8614. strncpy (result + lhs_size, rhs_start, rhs_size);
  8615. return result;
  8616. }
  8617. /* Issue the error given by GMSGID, indicating that it occurred before
  8618. TOKEN, which had the associated VALUE. */
  8619. void
  8620. c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
  8621. tree value, unsigned char token_flags)
  8622. {
  8623. #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
  8624. char *message = NULL;
  8625. if (token_type == CPP_EOF)
  8626. message = catenate_messages (gmsgid, " at end of input");
  8627. else if (token_type == CPP_CHAR
  8628. || token_type == CPP_WCHAR
  8629. || token_type == CPP_CHAR16
  8630. || token_type == CPP_CHAR32)
  8631. {
  8632. unsigned int val = TREE_INT_CST_LOW (value);
  8633. const char *prefix;
  8634. switch (token_type)
  8635. {
  8636. default:
  8637. prefix = "";
  8638. break;
  8639. case CPP_WCHAR:
  8640. prefix = "L";
  8641. break;
  8642. case CPP_CHAR16:
  8643. prefix = "u";
  8644. break;
  8645. case CPP_CHAR32:
  8646. prefix = "U";
  8647. break;
  8648. }
  8649. if (val <= UCHAR_MAX && ISGRAPH (val))
  8650. message = catenate_messages (gmsgid, " before %s'%c'");
  8651. else
  8652. message = catenate_messages (gmsgid, " before %s'\\x%x'");
  8653. error (message, prefix, val);
  8654. free (message);
  8655. message = NULL;
  8656. }
  8657. else if (token_type == CPP_CHAR_USERDEF
  8658. || token_type == CPP_WCHAR_USERDEF
  8659. || token_type == CPP_CHAR16_USERDEF
  8660. || token_type == CPP_CHAR32_USERDEF)
  8661. message = catenate_messages (gmsgid,
  8662. " before user-defined character literal");
  8663. else if (token_type == CPP_STRING_USERDEF
  8664. || token_type == CPP_WSTRING_USERDEF
  8665. || token_type == CPP_STRING16_USERDEF
  8666. || token_type == CPP_STRING32_USERDEF
  8667. || token_type == CPP_UTF8STRING_USERDEF)
  8668. message = catenate_messages (gmsgid, " before user-defined string literal");
  8669. else if (token_type == CPP_STRING
  8670. || token_type == CPP_WSTRING
  8671. || token_type == CPP_STRING16
  8672. || token_type == CPP_STRING32
  8673. || token_type == CPP_UTF8STRING)
  8674. message = catenate_messages (gmsgid, " before string constant");
  8675. else if (token_type == CPP_NUMBER)
  8676. message = catenate_messages (gmsgid, " before numeric constant");
  8677. else if (token_type == CPP_NAME)
  8678. {
  8679. message = catenate_messages (gmsgid, " before %qE");
  8680. error (message, value);
  8681. free (message);
  8682. message = NULL;
  8683. }
  8684. else if (token_type == CPP_PRAGMA)
  8685. message = catenate_messages (gmsgid, " before %<#pragma%>");
  8686. else if (token_type == CPP_PRAGMA_EOL)
  8687. message = catenate_messages (gmsgid, " before end of line");
  8688. else if (token_type == CPP_DECLTYPE)
  8689. message = catenate_messages (gmsgid, " before %<decltype%>");
  8690. else if (token_type < N_TTYPES)
  8691. {
  8692. message = catenate_messages (gmsgid, " before %qs token");
  8693. error (message, cpp_type2name (token_type, token_flags));
  8694. free (message);
  8695. message = NULL;
  8696. }
  8697. else
  8698. error (gmsgid);
  8699. if (message)
  8700. {
  8701. error (message);
  8702. free (message);
  8703. }
  8704. #undef catenate_messages
  8705. }
  8706. /* Return the gcc option code associated with the reason for a cpp
  8707. message, or 0 if none. */
  8708. static int
  8709. c_option_controlling_cpp_error (int reason)
  8710. {
  8711. const struct cpp_reason_option_codes_t *entry;
  8712. for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
  8713. {
  8714. if (entry->reason == reason)
  8715. return entry->option_code;
  8716. }
  8717. return 0;
  8718. }
  8719. /* Callback from cpp_error for PFILE to print diagnostics from the
  8720. preprocessor. The diagnostic is of type LEVEL, with REASON set
  8721. to the reason code if LEVEL is represents a warning, at location
  8722. LOCATION unless this is after lexing and the compiler's location
  8723. should be used instead, with column number possibly overridden by
  8724. COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
  8725. the arguments. Returns true if a diagnostic was emitted, false
  8726. otherwise. */
  8727. bool
  8728. c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
  8729. location_t location, unsigned int column_override,
  8730. const char *msg, va_list *ap)
  8731. {
  8732. diagnostic_info diagnostic;
  8733. diagnostic_t dlevel;
  8734. bool save_warn_system_headers = global_dc->dc_warn_system_headers;
  8735. bool ret;
  8736. switch (level)
  8737. {
  8738. case CPP_DL_WARNING_SYSHDR:
  8739. if (flag_no_output)
  8740. return false;
  8741. global_dc->dc_warn_system_headers = 1;
  8742. /* Fall through. */
  8743. case CPP_DL_WARNING:
  8744. if (flag_no_output)
  8745. return false;
  8746. dlevel = DK_WARNING;
  8747. break;
  8748. case CPP_DL_PEDWARN:
  8749. if (flag_no_output && !flag_pedantic_errors)
  8750. return false;
  8751. dlevel = DK_PEDWARN;
  8752. break;
  8753. case CPP_DL_ERROR:
  8754. dlevel = DK_ERROR;
  8755. break;
  8756. case CPP_DL_ICE:
  8757. dlevel = DK_ICE;
  8758. break;
  8759. case CPP_DL_NOTE:
  8760. dlevel = DK_NOTE;
  8761. break;
  8762. case CPP_DL_FATAL:
  8763. dlevel = DK_FATAL;
  8764. break;
  8765. default:
  8766. gcc_unreachable ();
  8767. }
  8768. if (done_lexing)
  8769. location = input_location;
  8770. diagnostic_set_info_translated (&diagnostic, msg, ap,
  8771. location, dlevel);
  8772. if (column_override)
  8773. diagnostic_override_column (&diagnostic, column_override);
  8774. diagnostic_override_option_index (&diagnostic,
  8775. c_option_controlling_cpp_error (reason));
  8776. ret = report_diagnostic (&diagnostic);
  8777. if (level == CPP_DL_WARNING_SYSHDR)
  8778. global_dc->dc_warn_system_headers = save_warn_system_headers;
  8779. return ret;
  8780. }
  8781. /* Convert a character from the host to the target execution character
  8782. set. cpplib handles this, mostly. */
  8783. HOST_WIDE_INT
  8784. c_common_to_target_charset (HOST_WIDE_INT c)
  8785. {
  8786. /* Character constants in GCC proper are sign-extended under -fsigned-char,
  8787. zero-extended under -fno-signed-char. cpplib insists that characters
  8788. and character constants are always unsigned. Hence we must convert
  8789. back and forth. */
  8790. cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
  8791. uc = cpp_host_to_exec_charset (parse_in, uc);
  8792. if (flag_signed_char)
  8793. return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
  8794. >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
  8795. else
  8796. return uc;
  8797. }
  8798. /* Fold an offsetof-like expression. EXPR is a nested sequence of component
  8799. references with an INDIRECT_REF of a constant at the bottom; much like the
  8800. traditional rendering of offsetof as a macro. Return the folded result. */
  8801. tree
  8802. fold_offsetof_1 (tree expr)
  8803. {
  8804. tree base, off, t;
  8805. switch (TREE_CODE (expr))
  8806. {
  8807. case ERROR_MARK:
  8808. return expr;
  8809. case VAR_DECL:
  8810. error ("cannot apply %<offsetof%> to static data member %qD", expr);
  8811. return error_mark_node;
  8812. case CALL_EXPR:
  8813. case TARGET_EXPR:
  8814. error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
  8815. return error_mark_node;
  8816. case NOP_EXPR:
  8817. case INDIRECT_REF:
  8818. if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
  8819. {
  8820. error ("cannot apply %<offsetof%> to a non constant address");
  8821. return error_mark_node;
  8822. }
  8823. return TREE_OPERAND (expr, 0);
  8824. case COMPONENT_REF:
  8825. base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
  8826. if (base == error_mark_node)
  8827. return base;
  8828. t = TREE_OPERAND (expr, 1);
  8829. if (DECL_C_BIT_FIELD (t))
  8830. {
  8831. error ("attempt to take address of bit-field structure "
  8832. "member %qD", t);
  8833. return error_mark_node;
  8834. }
  8835. off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
  8836. size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
  8837. / BITS_PER_UNIT));
  8838. break;
  8839. case ARRAY_REF:
  8840. base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
  8841. if (base == error_mark_node)
  8842. return base;
  8843. t = TREE_OPERAND (expr, 1);
  8844. /* Check if the offset goes beyond the upper bound of the array. */
  8845. if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
  8846. {
  8847. tree upbound = array_ref_up_bound (expr);
  8848. if (upbound != NULL_TREE
  8849. && TREE_CODE (upbound) == INTEGER_CST
  8850. && !tree_int_cst_equal (upbound,
  8851. TYPE_MAX_VALUE (TREE_TYPE (upbound))))
  8852. {
  8853. upbound = size_binop (PLUS_EXPR, upbound,
  8854. build_int_cst (TREE_TYPE (upbound), 1));
  8855. if (tree_int_cst_lt (upbound, t))
  8856. {
  8857. tree v;
  8858. for (v = TREE_OPERAND (expr, 0);
  8859. TREE_CODE (v) == COMPONENT_REF;
  8860. v = TREE_OPERAND (v, 0))
  8861. if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
  8862. == RECORD_TYPE)
  8863. {
  8864. tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
  8865. for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
  8866. if (TREE_CODE (fld_chain) == FIELD_DECL)
  8867. break;
  8868. if (fld_chain)
  8869. break;
  8870. }
  8871. /* Don't warn if the array might be considered a poor
  8872. man's flexible array member with a very permissive
  8873. definition thereof. */
  8874. if (TREE_CODE (v) == ARRAY_REF
  8875. || TREE_CODE (v) == COMPONENT_REF)
  8876. warning (OPT_Warray_bounds,
  8877. "index %E denotes an offset "
  8878. "greater than size of %qT",
  8879. t, TREE_TYPE (TREE_OPERAND (expr, 0)));
  8880. }
  8881. }
  8882. }
  8883. t = convert (sizetype, t);
  8884. off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
  8885. break;
  8886. case COMPOUND_EXPR:
  8887. /* Handle static members of volatile structs. */
  8888. t = TREE_OPERAND (expr, 1);
  8889. gcc_assert (TREE_CODE (t) == VAR_DECL);
  8890. return fold_offsetof_1 (t);
  8891. default:
  8892. gcc_unreachable ();
  8893. }
  8894. return fold_build_pointer_plus (base, off);
  8895. }
  8896. /* Likewise, but convert it to the return type of offsetof. */
  8897. tree
  8898. fold_offsetof (tree expr)
  8899. {
  8900. return convert (size_type_node, fold_offsetof_1 (expr));
  8901. }
  8902. /* Warn for A ?: C expressions (with B omitted) where A is a boolean
  8903. expression, because B will always be true. */
  8904. void
  8905. warn_for_omitted_condop (location_t location, tree cond)
  8906. {
  8907. if (truth_value_p (TREE_CODE (cond)))
  8908. warning_at (location, OPT_Wparentheses,
  8909. "the omitted middle operand in ?: will always be %<true%>, "
  8910. "suggest explicit middle operand");
  8911. }
  8912. /* Give an error for storing into ARG, which is 'const'. USE indicates
  8913. how ARG was being used. */
  8914. void
  8915. readonly_error (location_t loc, tree arg, enum lvalue_use use)
  8916. {
  8917. gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
  8918. || use == lv_asm);
  8919. /* Using this macro rather than (for example) arrays of messages
  8920. ensures that all the format strings are checked at compile
  8921. time. */
  8922. #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
  8923. : (use == lv_increment ? (I) \
  8924. : (use == lv_decrement ? (D) : (AS))))
  8925. if (TREE_CODE (arg) == COMPONENT_REF)
  8926. {
  8927. if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
  8928. error_at (loc, READONLY_MSG (G_("assignment of member "
  8929. "%qD in read-only object"),
  8930. G_("increment of member "
  8931. "%qD in read-only object"),
  8932. G_("decrement of member "
  8933. "%qD in read-only object"),
  8934. G_("member %qD in read-only object "
  8935. "used as %<asm%> output")),
  8936. TREE_OPERAND (arg, 1));
  8937. else
  8938. error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
  8939. G_("increment of read-only member %qD"),
  8940. G_("decrement of read-only member %qD"),
  8941. G_("read-only member %qD used as %<asm%> output")),
  8942. TREE_OPERAND (arg, 1));
  8943. }
  8944. else if (TREE_CODE (arg) == VAR_DECL)
  8945. error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
  8946. G_("increment of read-only variable %qD"),
  8947. G_("decrement of read-only variable %qD"),
  8948. G_("read-only variable %qD used as %<asm%> output")),
  8949. arg);
  8950. else if (TREE_CODE (arg) == PARM_DECL)
  8951. error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
  8952. G_("increment of read-only parameter %qD"),
  8953. G_("decrement of read-only parameter %qD"),
  8954. G_("read-only parameter %qD use as %<asm%> output")),
  8955. arg);
  8956. else if (TREE_CODE (arg) == RESULT_DECL)
  8957. {
  8958. gcc_assert (c_dialect_cxx ());
  8959. error_at (loc, READONLY_MSG (G_("assignment of "
  8960. "read-only named return value %qD"),
  8961. G_("increment of "
  8962. "read-only named return value %qD"),
  8963. G_("decrement of "
  8964. "read-only named return value %qD"),
  8965. G_("read-only named return value %qD "
  8966. "used as %<asm%>output")),
  8967. arg);
  8968. }
  8969. else if (TREE_CODE (arg) == FUNCTION_DECL)
  8970. error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
  8971. G_("increment of function %qD"),
  8972. G_("decrement of function %qD"),
  8973. G_("function %qD used as %<asm%> output")),
  8974. arg);
  8975. else
  8976. error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
  8977. G_("increment of read-only location %qE"),
  8978. G_("decrement of read-only location %qE"),
  8979. G_("read-only location %qE used as %<asm%> output")),
  8980. arg);
  8981. }
  8982. /* Print an error message for an invalid lvalue. USE says
  8983. how the lvalue is being used and so selects the error message. LOC
  8984. is the location for the error. */
  8985. void
  8986. lvalue_error (location_t loc, enum lvalue_use use)
  8987. {
  8988. switch (use)
  8989. {
  8990. case lv_assign:
  8991. error_at (loc, "lvalue required as left operand of assignment");
  8992. break;
  8993. case lv_increment:
  8994. error_at (loc, "lvalue required as increment operand");
  8995. break;
  8996. case lv_decrement:
  8997. error_at (loc, "lvalue required as decrement operand");
  8998. break;
  8999. case lv_addressof:
  9000. error_at (loc, "lvalue required as unary %<&%> operand");
  9001. break;
  9002. case lv_asm:
  9003. error_at (loc, "lvalue required in asm statement");
  9004. break;
  9005. default:
  9006. gcc_unreachable ();
  9007. }
  9008. }
  9009. /* Print an error message for an invalid indirection of type TYPE.
  9010. ERRSTRING is the name of the operator for the indirection. */
  9011. void
  9012. invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
  9013. {
  9014. switch (errstring)
  9015. {
  9016. case RO_NULL:
  9017. gcc_assert (c_dialect_cxx ());
  9018. error_at (loc, "invalid type argument (have %qT)", type);
  9019. break;
  9020. case RO_ARRAY_INDEXING:
  9021. error_at (loc,
  9022. "invalid type argument of array indexing (have %qT)",
  9023. type);
  9024. break;
  9025. case RO_UNARY_STAR:
  9026. error_at (loc,
  9027. "invalid type argument of unary %<*%> (have %qT)",
  9028. type);
  9029. break;
  9030. case RO_ARROW:
  9031. error_at (loc,
  9032. "invalid type argument of %<->%> (have %qT)",
  9033. type);
  9034. break;
  9035. case RO_ARROW_STAR:
  9036. error_at (loc,
  9037. "invalid type argument of %<->*%> (have %qT)",
  9038. type);
  9039. break;
  9040. case RO_IMPLICIT_CONVERSION:
  9041. error_at (loc,
  9042. "invalid type argument of implicit conversion (have %qT)",
  9043. type);
  9044. break;
  9045. default:
  9046. gcc_unreachable ();
  9047. }
  9048. }
  9049. /* *PTYPE is an incomplete array. Complete it with a domain based on
  9050. INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
  9051. is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
  9052. 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
  9053. int
  9054. complete_array_type (tree *ptype, tree initial_value, bool do_default)
  9055. {
  9056. tree maxindex, type, main_type, elt, unqual_elt;
  9057. int failure = 0, quals;
  9058. hashval_t hashcode = 0;
  9059. bool overflow_p = false;
  9060. maxindex = size_zero_node;
  9061. if (initial_value)
  9062. {
  9063. if (TREE_CODE (initial_value) == STRING_CST)
  9064. {
  9065. int eltsize
  9066. = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
  9067. maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
  9068. }
  9069. else if (TREE_CODE (initial_value) == CONSTRUCTOR)
  9070. {
  9071. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
  9072. if (vec_safe_is_empty (v))
  9073. {
  9074. if (pedantic)
  9075. failure = 3;
  9076. maxindex = ssize_int (-1);
  9077. }
  9078. else
  9079. {
  9080. tree curindex;
  9081. unsigned HOST_WIDE_INT cnt;
  9082. constructor_elt *ce;
  9083. bool fold_p = false;
  9084. if ((*v)[0].index)
  9085. maxindex = (*v)[0].index, fold_p = true;
  9086. curindex = maxindex;
  9087. for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
  9088. {
  9089. bool curfold_p = false;
  9090. if (ce->index)
  9091. curindex = ce->index, curfold_p = true;
  9092. else
  9093. {
  9094. if (fold_p)
  9095. {
  9096. /* Since we treat size types now as ordinary
  9097. unsigned types, we need an explicit overflow
  9098. check. */
  9099. tree orig = curindex;
  9100. curindex = fold_convert (sizetype, curindex);
  9101. overflow_p |= tree_int_cst_lt (curindex, orig);
  9102. }
  9103. curindex = size_binop (PLUS_EXPR, curindex,
  9104. size_one_node);
  9105. }
  9106. if (tree_int_cst_lt (maxindex, curindex))
  9107. maxindex = curindex, fold_p = curfold_p;
  9108. }
  9109. if (fold_p)
  9110. {
  9111. tree orig = maxindex;
  9112. maxindex = fold_convert (sizetype, maxindex);
  9113. overflow_p |= tree_int_cst_lt (maxindex, orig);
  9114. }
  9115. }
  9116. }
  9117. else
  9118. {
  9119. /* Make an error message unless that happened already. */
  9120. if (initial_value != error_mark_node)
  9121. failure = 1;
  9122. }
  9123. }
  9124. else
  9125. {
  9126. failure = 2;
  9127. if (!do_default)
  9128. return failure;
  9129. }
  9130. type = *ptype;
  9131. elt = TREE_TYPE (type);
  9132. quals = TYPE_QUALS (strip_array_types (elt));
  9133. if (quals == 0)
  9134. unqual_elt = elt;
  9135. else
  9136. unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
  9137. /* Using build_distinct_type_copy and modifying things afterward instead
  9138. of using build_array_type to create a new type preserves all of the
  9139. TYPE_LANG_FLAG_? bits that the front end may have set. */
  9140. main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
  9141. TREE_TYPE (main_type) = unqual_elt;
  9142. TYPE_DOMAIN (main_type)
  9143. = build_range_type (TREE_TYPE (maxindex),
  9144. build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
  9145. layout_type (main_type);
  9146. /* Make sure we have the canonical MAIN_TYPE. */
  9147. hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
  9148. hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
  9149. hashcode);
  9150. main_type = type_hash_canon (hashcode, main_type);
  9151. /* Fix the canonical type. */
  9152. if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
  9153. || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
  9154. SET_TYPE_STRUCTURAL_EQUALITY (main_type);
  9155. else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
  9156. || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
  9157. != TYPE_DOMAIN (main_type)))
  9158. TYPE_CANONICAL (main_type)
  9159. = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
  9160. TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
  9161. else
  9162. TYPE_CANONICAL (main_type) = main_type;
  9163. if (quals == 0)
  9164. type = main_type;
  9165. else
  9166. type = c_build_qualified_type (main_type, quals);
  9167. if (COMPLETE_TYPE_P (type)
  9168. && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
  9169. && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
  9170. {
  9171. error ("size of array is too large");
  9172. /* If we proceed with the array type as it is, we'll eventually
  9173. crash in tree_to_[su]hwi(). */
  9174. type = error_mark_node;
  9175. }
  9176. *ptype = type;
  9177. return failure;
  9178. }
  9179. /* Like c_mark_addressable but don't check register qualifier. */
  9180. void
  9181. c_common_mark_addressable_vec (tree t)
  9182. {
  9183. while (handled_component_p (t))
  9184. t = TREE_OPERAND (t, 0);
  9185. if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
  9186. return;
  9187. TREE_ADDRESSABLE (t) = 1;
  9188. }
  9189. /* Used to help initialize the builtin-types.def table. When a type of
  9190. the correct size doesn't exist, use error_mark_node instead of NULL.
  9191. The later results in segfaults even when a decl using the type doesn't
  9192. get invoked. */
  9193. tree
  9194. builtin_type_for_size (int size, bool unsignedp)
  9195. {
  9196. tree type = c_common_type_for_size (size, unsignedp);
  9197. return type ? type : error_mark_node;
  9198. }
  9199. /* A helper function for resolve_overloaded_builtin in resolving the
  9200. overloaded __sync_ builtins. Returns a positive power of 2 if the
  9201. first operand of PARAMS is a pointer to a supported data type.
  9202. Returns 0 if an error is encountered. */
  9203. static int
  9204. sync_resolve_size (tree function, vec<tree, va_gc> *params)
  9205. {
  9206. tree type;
  9207. int size;
  9208. if (!params)
  9209. {
  9210. error ("too few arguments to function %qE", function);
  9211. return 0;
  9212. }
  9213. type = TREE_TYPE ((*params)[0]);
  9214. if (TREE_CODE (type) == ARRAY_TYPE)
  9215. {
  9216. /* Force array-to-pointer decay for C++. */
  9217. gcc_assert (c_dialect_cxx());
  9218. (*params)[0] = default_conversion ((*params)[0]);
  9219. type = TREE_TYPE ((*params)[0]);
  9220. }
  9221. if (TREE_CODE (type) != POINTER_TYPE)
  9222. goto incompatible;
  9223. type = TREE_TYPE (type);
  9224. if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
  9225. goto incompatible;
  9226. size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
  9227. if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
  9228. return size;
  9229. incompatible:
  9230. error ("incompatible type for argument %d of %qE", 1, function);
  9231. return 0;
  9232. }
  9233. /* A helper function for resolve_overloaded_builtin. Adds casts to
  9234. PARAMS to make arguments match up with those of FUNCTION. Drops
  9235. the variadic arguments at the end. Returns false if some error
  9236. was encountered; true on success. */
  9237. static bool
  9238. sync_resolve_params (location_t loc, tree orig_function, tree function,
  9239. vec<tree, va_gc> *params, bool orig_format)
  9240. {
  9241. function_args_iterator iter;
  9242. tree ptype;
  9243. unsigned int parmnum;
  9244. function_args_iter_init (&iter, TREE_TYPE (function));
  9245. /* We've declared the implementation functions to use "volatile void *"
  9246. as the pointer parameter, so we shouldn't get any complaints from the
  9247. call to check_function_arguments what ever type the user used. */
  9248. function_args_iter_next (&iter);
  9249. ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
  9250. ptype = TYPE_MAIN_VARIANT (ptype);
  9251. /* For the rest of the values, we need to cast these to FTYPE, so that we
  9252. don't get warnings for passing pointer types, etc. */
  9253. parmnum = 0;
  9254. while (1)
  9255. {
  9256. tree val, arg_type;
  9257. arg_type = function_args_iter_cond (&iter);
  9258. /* XXX void_type_node belies the abstraction. */
  9259. if (arg_type == void_type_node)
  9260. break;
  9261. ++parmnum;
  9262. if (params->length () <= parmnum)
  9263. {
  9264. error_at (loc, "too few arguments to function %qE", orig_function);
  9265. return false;
  9266. }
  9267. /* Only convert parameters if arg_type is unsigned integer type with
  9268. new format sync routines, i.e. don't attempt to convert pointer
  9269. arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
  9270. bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
  9271. kinds). */
  9272. if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
  9273. {
  9274. /* Ideally for the first conversion we'd use convert_for_assignment
  9275. so that we get warnings for anything that doesn't match the pointer
  9276. type. This isn't portable across the C and C++ front ends atm. */
  9277. val = (*params)[parmnum];
  9278. val = convert (ptype, val);
  9279. val = convert (arg_type, val);
  9280. (*params)[parmnum] = val;
  9281. }
  9282. function_args_iter_next (&iter);
  9283. }
  9284. /* __atomic routines are not variadic. */
  9285. if (!orig_format && params->length () != parmnum + 1)
  9286. {
  9287. error_at (loc, "too many arguments to function %qE", orig_function);
  9288. return false;
  9289. }
  9290. /* The definition of these primitives is variadic, with the remaining
  9291. being "an optional list of variables protected by the memory barrier".
  9292. No clue what that's supposed to mean, precisely, but we consider all
  9293. call-clobbered variables to be protected so we're safe. */
  9294. params->truncate (parmnum + 1);
  9295. return true;
  9296. }
  9297. /* A helper function for resolve_overloaded_builtin. Adds a cast to
  9298. RESULT to make it match the type of the first pointer argument in
  9299. PARAMS. */
  9300. static tree
  9301. sync_resolve_return (tree first_param, tree result, bool orig_format)
  9302. {
  9303. tree ptype = TREE_TYPE (TREE_TYPE (first_param));
  9304. tree rtype = TREE_TYPE (result);
  9305. ptype = TYPE_MAIN_VARIANT (ptype);
  9306. /* New format doesn't require casting unless the types are the same size. */
  9307. if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
  9308. return convert (ptype, result);
  9309. else
  9310. return result;
  9311. }
  9312. /* This function verifies the PARAMS to generic atomic FUNCTION.
  9313. It returns the size if all the parameters are the same size, otherwise
  9314. 0 is returned if the parameters are invalid. */
  9315. static int
  9316. get_atomic_generic_size (location_t loc, tree function,
  9317. vec<tree, va_gc> *params)
  9318. {
  9319. unsigned int n_param;
  9320. unsigned int n_model;
  9321. unsigned int x;
  9322. int size_0;
  9323. tree type_0;
  9324. /* Determine the parameter makeup. */
  9325. switch (DECL_FUNCTION_CODE (function))
  9326. {
  9327. case BUILT_IN_ATOMIC_EXCHANGE:
  9328. n_param = 4;
  9329. n_model = 1;
  9330. break;
  9331. case BUILT_IN_ATOMIC_LOAD:
  9332. case BUILT_IN_ATOMIC_STORE:
  9333. n_param = 3;
  9334. n_model = 1;
  9335. break;
  9336. case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
  9337. n_param = 6;
  9338. n_model = 2;
  9339. break;
  9340. default:
  9341. gcc_unreachable ();
  9342. }
  9343. if (vec_safe_length (params) != n_param)
  9344. {
  9345. error_at (loc, "incorrect number of arguments to function %qE", function);
  9346. return 0;
  9347. }
  9348. /* Get type of first parameter, and determine its size. */
  9349. type_0 = TREE_TYPE ((*params)[0]);
  9350. if (TREE_CODE (type_0) == ARRAY_TYPE)
  9351. {
  9352. /* Force array-to-pointer decay for C++. */
  9353. gcc_assert (c_dialect_cxx());
  9354. (*params)[0] = default_conversion ((*params)[0]);
  9355. type_0 = TREE_TYPE ((*params)[0]);
  9356. }
  9357. if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
  9358. {
  9359. error_at (loc, "argument 1 of %qE must be a non-void pointer type",
  9360. function);
  9361. return 0;
  9362. }
  9363. /* Types must be compile time constant sizes. */
  9364. if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
  9365. {
  9366. error_at (loc,
  9367. "argument 1 of %qE must be a pointer to a constant size type",
  9368. function);
  9369. return 0;
  9370. }
  9371. size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
  9372. /* Zero size objects are not allowed. */
  9373. if (size_0 == 0)
  9374. {
  9375. error_at (loc,
  9376. "argument 1 of %qE must be a pointer to a nonzero size object",
  9377. function);
  9378. return 0;
  9379. }
  9380. /* Check each other parameter is a pointer and the same size. */
  9381. for (x = 0; x < n_param - n_model; x++)
  9382. {
  9383. int size;
  9384. tree type = TREE_TYPE ((*params)[x]);
  9385. /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
  9386. if (n_param == 6 && x == 3)
  9387. continue;
  9388. if (!POINTER_TYPE_P (type))
  9389. {
  9390. error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
  9391. function);
  9392. return 0;
  9393. }
  9394. tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
  9395. size = type_size ? tree_to_uhwi (type_size) : 0;
  9396. if (size != size_0)
  9397. {
  9398. error_at (loc, "size mismatch in argument %d of %qE", x + 1,
  9399. function);
  9400. return 0;
  9401. }
  9402. }
  9403. /* Check memory model parameters for validity. */
  9404. for (x = n_param - n_model ; x < n_param; x++)
  9405. {
  9406. tree p = (*params)[x];
  9407. if (TREE_CODE (p) == INTEGER_CST)
  9408. {
  9409. int i = tree_to_uhwi (p);
  9410. if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
  9411. {
  9412. warning_at (loc, OPT_Winvalid_memory_model,
  9413. "invalid memory model argument %d of %qE", x + 1,
  9414. function);
  9415. }
  9416. }
  9417. else
  9418. if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
  9419. {
  9420. error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
  9421. function);
  9422. return 0;
  9423. }
  9424. }
  9425. return size_0;
  9426. }
  9427. /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
  9428. at the beginning of the parameter list PARAMS representing the size of the
  9429. objects. This is to match the library ABI requirement. LOC is the location
  9430. of the function call.
  9431. The new function is returned if it needed rebuilding, otherwise NULL_TREE is
  9432. returned to allow the external call to be constructed. */
  9433. static tree
  9434. add_atomic_size_parameter (unsigned n, location_t loc, tree function,
  9435. vec<tree, va_gc> *params)
  9436. {
  9437. tree size_node;
  9438. /* Insert a SIZE_T parameter as the first param. If there isn't
  9439. enough space, allocate a new vector and recursively re-build with that. */
  9440. if (!params->space (1))
  9441. {
  9442. unsigned int z, len;
  9443. vec<tree, va_gc> *v;
  9444. tree f;
  9445. len = params->length ();
  9446. vec_alloc (v, len + 1);
  9447. v->quick_push (build_int_cst (size_type_node, n));
  9448. for (z = 0; z < len; z++)
  9449. v->quick_push ((*params)[z]);
  9450. f = build_function_call_vec (loc, vNULL, function, v, NULL);
  9451. vec_free (v);
  9452. return f;
  9453. }
  9454. /* Add the size parameter and leave as a function call for processing. */
  9455. size_node = build_int_cst (size_type_node, n);
  9456. params->quick_insert (0, size_node);
  9457. return NULL_TREE;
  9458. }
  9459. /* Return whether atomic operations for naturally aligned N-byte
  9460. arguments are supported, whether inline or through libatomic. */
  9461. static bool
  9462. atomic_size_supported_p (int n)
  9463. {
  9464. switch (n)
  9465. {
  9466. case 1:
  9467. case 2:
  9468. case 4:
  9469. case 8:
  9470. return true;
  9471. case 16:
  9472. return targetm.scalar_mode_supported_p (TImode);
  9473. default:
  9474. return false;
  9475. }
  9476. }
  9477. /* This will process an __atomic_exchange function call, determine whether it
  9478. needs to be mapped to the _N variation, or turned into a library call.
  9479. LOC is the location of the builtin call.
  9480. FUNCTION is the DECL that has been invoked;
  9481. PARAMS is the argument list for the call. The return value is non-null
  9482. TRUE is returned if it is translated into the proper format for a call to the
  9483. external library, and NEW_RETURN is set the tree for that function.
  9484. FALSE is returned if processing for the _N variation is required, and
  9485. NEW_RETURN is set to the the return value the result is copied into. */
  9486. static bool
  9487. resolve_overloaded_atomic_exchange (location_t loc, tree function,
  9488. vec<tree, va_gc> *params, tree *new_return)
  9489. {
  9490. tree p0, p1, p2, p3;
  9491. tree I_type, I_type_ptr;
  9492. int n = get_atomic_generic_size (loc, function, params);
  9493. /* Size of 0 is an error condition. */
  9494. if (n == 0)
  9495. {
  9496. *new_return = error_mark_node;
  9497. return true;
  9498. }
  9499. /* If not a lock-free size, change to the library generic format. */
  9500. if (!atomic_size_supported_p (n))
  9501. {
  9502. *new_return = add_atomic_size_parameter (n, loc, function, params);
  9503. return true;
  9504. }
  9505. /* Otherwise there is a lockfree match, transform the call from:
  9506. void fn(T* mem, T* desired, T* return, model)
  9507. into
  9508. *return = (T) (fn (In* mem, (In) *desired, model)) */
  9509. p0 = (*params)[0];
  9510. p1 = (*params)[1];
  9511. p2 = (*params)[2];
  9512. p3 = (*params)[3];
  9513. /* Create pointer to appropriate size. */
  9514. I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
  9515. I_type_ptr = build_pointer_type (I_type);
  9516. /* Convert object pointer to required type. */
  9517. p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
  9518. (*params)[0] = p0;
  9519. /* Convert new value to required type, and dereference it. */
  9520. p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
  9521. p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
  9522. (*params)[1] = p1;
  9523. /* Move memory model to the 3rd position, and end param list. */
  9524. (*params)[2] = p3;
  9525. params->truncate (3);
  9526. /* Convert return pointer and dereference it for later assignment. */
  9527. *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
  9528. return false;
  9529. }
  9530. /* This will process an __atomic_compare_exchange function call, determine
  9531. whether it needs to be mapped to the _N variation, or turned into a lib call.
  9532. LOC is the location of the builtin call.
  9533. FUNCTION is the DECL that has been invoked;
  9534. PARAMS is the argument list for the call. The return value is non-null
  9535. TRUE is returned if it is translated into the proper format for a call to the
  9536. external library, and NEW_RETURN is set the tree for that function.
  9537. FALSE is returned if processing for the _N variation is required. */
  9538. static bool
  9539. resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
  9540. vec<tree, va_gc> *params,
  9541. tree *new_return)
  9542. {
  9543. tree p0, p1, p2;
  9544. tree I_type, I_type_ptr;
  9545. int n = get_atomic_generic_size (loc, function, params);
  9546. /* Size of 0 is an error condition. */
  9547. if (n == 0)
  9548. {
  9549. *new_return = error_mark_node;
  9550. return true;
  9551. }
  9552. /* If not a lock-free size, change to the library generic format. */
  9553. if (!atomic_size_supported_p (n))
  9554. {
  9555. /* The library generic format does not have the weak parameter, so
  9556. remove it from the param list. Since a parameter has been removed,
  9557. we can be sure that there is room for the SIZE_T parameter, meaning
  9558. there will not be a recursive rebuilding of the parameter list, so
  9559. there is no danger this will be done twice. */
  9560. if (n > 0)
  9561. {
  9562. (*params)[3] = (*params)[4];
  9563. (*params)[4] = (*params)[5];
  9564. params->truncate (5);
  9565. }
  9566. *new_return = add_atomic_size_parameter (n, loc, function, params);
  9567. return true;
  9568. }
  9569. /* Otherwise, there is a match, so the call needs to be transformed from:
  9570. bool fn(T* mem, T* desired, T* return, weak, success, failure)
  9571. into
  9572. bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
  9573. p0 = (*params)[0];
  9574. p1 = (*params)[1];
  9575. p2 = (*params)[2];
  9576. /* Create pointer to appropriate size. */
  9577. I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
  9578. I_type_ptr = build_pointer_type (I_type);
  9579. /* Convert object pointer to required type. */
  9580. p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
  9581. (*params)[0] = p0;
  9582. /* Convert expected pointer to required type. */
  9583. p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
  9584. (*params)[1] = p1;
  9585. /* Convert desired value to required type, and dereference it. */
  9586. p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
  9587. p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
  9588. (*params)[2] = p2;
  9589. /* The rest of the parameters are fine. NULL means no special return value
  9590. processing.*/
  9591. *new_return = NULL;
  9592. return false;
  9593. }
  9594. /* This will process an __atomic_load function call, determine whether it
  9595. needs to be mapped to the _N variation, or turned into a library call.
  9596. LOC is the location of the builtin call.
  9597. FUNCTION is the DECL that has been invoked;
  9598. PARAMS is the argument list for the call. The return value is non-null
  9599. TRUE is returned if it is translated into the proper format for a call to the
  9600. external library, and NEW_RETURN is set the tree for that function.
  9601. FALSE is returned if processing for the _N variation is required, and
  9602. NEW_RETURN is set to the the return value the result is copied into. */
  9603. static bool
  9604. resolve_overloaded_atomic_load (location_t loc, tree function,
  9605. vec<tree, va_gc> *params, tree *new_return)
  9606. {
  9607. tree p0, p1, p2;
  9608. tree I_type, I_type_ptr;
  9609. int n = get_atomic_generic_size (loc, function, params);
  9610. /* Size of 0 is an error condition. */
  9611. if (n == 0)
  9612. {
  9613. *new_return = error_mark_node;
  9614. return true;
  9615. }
  9616. /* If not a lock-free size, change to the library generic format. */
  9617. if (!atomic_size_supported_p (n))
  9618. {
  9619. *new_return = add_atomic_size_parameter (n, loc, function, params);
  9620. return true;
  9621. }
  9622. /* Otherwise, there is a match, so the call needs to be transformed from:
  9623. void fn(T* mem, T* return, model)
  9624. into
  9625. *return = (T) (fn ((In *) mem, model)) */
  9626. p0 = (*params)[0];
  9627. p1 = (*params)[1];
  9628. p2 = (*params)[2];
  9629. /* Create pointer to appropriate size. */
  9630. I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
  9631. I_type_ptr = build_pointer_type (I_type);
  9632. /* Convert object pointer to required type. */
  9633. p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
  9634. (*params)[0] = p0;
  9635. /* Move memory model to the 2nd position, and end param list. */
  9636. (*params)[1] = p2;
  9637. params->truncate (2);
  9638. /* Convert return pointer and dereference it for later assignment. */
  9639. *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
  9640. return false;
  9641. }
  9642. /* This will process an __atomic_store function call, determine whether it
  9643. needs to be mapped to the _N variation, or turned into a library call.
  9644. LOC is the location of the builtin call.
  9645. FUNCTION is the DECL that has been invoked;
  9646. PARAMS is the argument list for the call. The return value is non-null
  9647. TRUE is returned if it is translated into the proper format for a call to the
  9648. external library, and NEW_RETURN is set the tree for that function.
  9649. FALSE is returned if processing for the _N variation is required, and
  9650. NEW_RETURN is set to the the return value the result is copied into. */
  9651. static bool
  9652. resolve_overloaded_atomic_store (location_t loc, tree function,
  9653. vec<tree, va_gc> *params, tree *new_return)
  9654. {
  9655. tree p0, p1;
  9656. tree I_type, I_type_ptr;
  9657. int n = get_atomic_generic_size (loc, function, params);
  9658. /* Size of 0 is an error condition. */
  9659. if (n == 0)
  9660. {
  9661. *new_return = error_mark_node;
  9662. return true;
  9663. }
  9664. /* If not a lock-free size, change to the library generic format. */
  9665. if (!atomic_size_supported_p (n))
  9666. {
  9667. *new_return = add_atomic_size_parameter (n, loc, function, params);
  9668. return true;
  9669. }
  9670. /* Otherwise, there is a match, so the call needs to be transformed from:
  9671. void fn(T* mem, T* value, model)
  9672. into
  9673. fn ((In *) mem, (In) *value, model) */
  9674. p0 = (*params)[0];
  9675. p1 = (*params)[1];
  9676. /* Create pointer to appropriate size. */
  9677. I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
  9678. I_type_ptr = build_pointer_type (I_type);
  9679. /* Convert object pointer to required type. */
  9680. p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
  9681. (*params)[0] = p0;
  9682. /* Convert new value to required type, and dereference it. */
  9683. p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
  9684. p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
  9685. (*params)[1] = p1;
  9686. /* The memory model is in the right spot already. Return is void. */
  9687. *new_return = NULL_TREE;
  9688. return false;
  9689. }
  9690. /* Some builtin functions are placeholders for other expressions. This
  9691. function should be called immediately after parsing the call expression
  9692. before surrounding code has committed to the type of the expression.
  9693. LOC is the location of the builtin call.
  9694. FUNCTION is the DECL that has been invoked; it is known to be a builtin.
  9695. PARAMS is the argument list for the call. The return value is non-null
  9696. when expansion is complete, and null if normal processing should
  9697. continue. */
  9698. tree
  9699. resolve_overloaded_builtin (location_t loc, tree function,
  9700. vec<tree, va_gc> *params)
  9701. {
  9702. enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
  9703. bool orig_format = true;
  9704. tree new_return = NULL_TREE;
  9705. switch (DECL_BUILT_IN_CLASS (function))
  9706. {
  9707. case BUILT_IN_NORMAL:
  9708. break;
  9709. case BUILT_IN_MD:
  9710. if (targetm.resolve_overloaded_builtin)
  9711. return targetm.resolve_overloaded_builtin (loc, function, params);
  9712. else
  9713. return NULL_TREE;
  9714. default:
  9715. return NULL_TREE;
  9716. }
  9717. /* Handle BUILT_IN_NORMAL here. */
  9718. switch (orig_code)
  9719. {
  9720. case BUILT_IN_ATOMIC_EXCHANGE:
  9721. case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
  9722. case BUILT_IN_ATOMIC_LOAD:
  9723. case BUILT_IN_ATOMIC_STORE:
  9724. {
  9725. /* Handle these 4 together so that they can fall through to the next
  9726. case if the call is transformed to an _N variant. */
  9727. switch (orig_code)
  9728. {
  9729. case BUILT_IN_ATOMIC_EXCHANGE:
  9730. {
  9731. if (resolve_overloaded_atomic_exchange (loc, function, params,
  9732. &new_return))
  9733. return new_return;
  9734. /* Change to the _N variant. */
  9735. orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
  9736. break;
  9737. }
  9738. case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
  9739. {
  9740. if (resolve_overloaded_atomic_compare_exchange (loc, function,
  9741. params,
  9742. &new_return))
  9743. return new_return;
  9744. /* Change to the _N variant. */
  9745. orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
  9746. break;
  9747. }
  9748. case BUILT_IN_ATOMIC_LOAD:
  9749. {
  9750. if (resolve_overloaded_atomic_load (loc, function, params,
  9751. &new_return))
  9752. return new_return;
  9753. /* Change to the _N variant. */
  9754. orig_code = BUILT_IN_ATOMIC_LOAD_N;
  9755. break;
  9756. }
  9757. case BUILT_IN_ATOMIC_STORE:
  9758. {
  9759. if (resolve_overloaded_atomic_store (loc, function, params,
  9760. &new_return))
  9761. return new_return;
  9762. /* Change to the _N variant. */
  9763. orig_code = BUILT_IN_ATOMIC_STORE_N;
  9764. break;
  9765. }
  9766. default:
  9767. gcc_unreachable ();
  9768. }
  9769. /* Fallthrough to the normal processing. */
  9770. }
  9771. case BUILT_IN_ATOMIC_EXCHANGE_N:
  9772. case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
  9773. case BUILT_IN_ATOMIC_LOAD_N:
  9774. case BUILT_IN_ATOMIC_STORE_N:
  9775. case BUILT_IN_ATOMIC_ADD_FETCH_N:
  9776. case BUILT_IN_ATOMIC_SUB_FETCH_N:
  9777. case BUILT_IN_ATOMIC_AND_FETCH_N:
  9778. case BUILT_IN_ATOMIC_NAND_FETCH_N:
  9779. case BUILT_IN_ATOMIC_XOR_FETCH_N:
  9780. case BUILT_IN_ATOMIC_OR_FETCH_N:
  9781. case BUILT_IN_ATOMIC_FETCH_ADD_N:
  9782. case BUILT_IN_ATOMIC_FETCH_SUB_N:
  9783. case BUILT_IN_ATOMIC_FETCH_AND_N:
  9784. case BUILT_IN_ATOMIC_FETCH_NAND_N:
  9785. case BUILT_IN_ATOMIC_FETCH_XOR_N:
  9786. case BUILT_IN_ATOMIC_FETCH_OR_N:
  9787. {
  9788. orig_format = false;
  9789. /* Fallthru for parameter processing. */
  9790. }
  9791. case BUILT_IN_SYNC_FETCH_AND_ADD_N:
  9792. case BUILT_IN_SYNC_FETCH_AND_SUB_N:
  9793. case BUILT_IN_SYNC_FETCH_AND_OR_N:
  9794. case BUILT_IN_SYNC_FETCH_AND_AND_N:
  9795. case BUILT_IN_SYNC_FETCH_AND_XOR_N:
  9796. case BUILT_IN_SYNC_FETCH_AND_NAND_N:
  9797. case BUILT_IN_SYNC_ADD_AND_FETCH_N:
  9798. case BUILT_IN_SYNC_SUB_AND_FETCH_N:
  9799. case BUILT_IN_SYNC_OR_AND_FETCH_N:
  9800. case BUILT_IN_SYNC_AND_AND_FETCH_N:
  9801. case BUILT_IN_SYNC_XOR_AND_FETCH_N:
  9802. case BUILT_IN_SYNC_NAND_AND_FETCH_N:
  9803. case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
  9804. case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
  9805. case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
  9806. case BUILT_IN_SYNC_LOCK_RELEASE_N:
  9807. {
  9808. int n = sync_resolve_size (function, params);
  9809. tree new_function, first_param, result;
  9810. enum built_in_function fncode;
  9811. if (n == 0)
  9812. return error_mark_node;
  9813. fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
  9814. new_function = builtin_decl_explicit (fncode);
  9815. if (!sync_resolve_params (loc, function, new_function, params,
  9816. orig_format))
  9817. return error_mark_node;
  9818. first_param = (*params)[0];
  9819. result = build_function_call_vec (loc, vNULL, new_function, params,
  9820. NULL);
  9821. if (result == error_mark_node)
  9822. return result;
  9823. if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
  9824. && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
  9825. && orig_code != BUILT_IN_ATOMIC_STORE_N)
  9826. result = sync_resolve_return (first_param, result, orig_format);
  9827. /* If new_return is set, assign function to that expr and cast the
  9828. result to void since the generic interface returned void. */
  9829. if (new_return)
  9830. {
  9831. /* Cast function result from I{1,2,4,8,16} to the required type. */
  9832. result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
  9833. result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
  9834. result);
  9835. TREE_SIDE_EFFECTS (result) = 1;
  9836. protected_set_expr_location (result, loc);
  9837. result = convert (void_type_node, result);
  9838. }
  9839. return result;
  9840. }
  9841. default:
  9842. return NULL_TREE;
  9843. }
  9844. }
  9845. /* vector_types_compatible_elements_p is used in type checks of vectors
  9846. values used as operands of binary operators. Where it returns true, and
  9847. the other checks of the caller succeed (being vector types in he first
  9848. place, and matching number of elements), we can just treat the types
  9849. as essentially the same.
  9850. Contrast with vector_targets_convertible_p, which is used for vector
  9851. pointer types, and vector_types_convertible_p, which will allow
  9852. language-specific matches under the control of flag_lax_vector_conversions,
  9853. and might still require a conversion. */
  9854. /* True if vector types T1 and T2 can be inputs to the same binary
  9855. operator without conversion.
  9856. We don't check the overall vector size here because some of our callers
  9857. want to give different error messages when the vectors are compatible
  9858. except for the element count. */
  9859. bool
  9860. vector_types_compatible_elements_p (tree t1, tree t2)
  9861. {
  9862. bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
  9863. t1 = TREE_TYPE (t1);
  9864. t2 = TREE_TYPE (t2);
  9865. enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
  9866. gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
  9867. && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
  9868. || c2 == FIXED_POINT_TYPE));
  9869. t1 = c_common_signed_type (t1);
  9870. t2 = c_common_signed_type (t2);
  9871. /* Equality works here because c_common_signed_type uses
  9872. TYPE_MAIN_VARIANT. */
  9873. if (t1 == t2)
  9874. return true;
  9875. if (opaque && c1 == c2
  9876. && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
  9877. && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
  9878. return true;
  9879. return false;
  9880. }
  9881. /* Check for missing format attributes on function pointers. LTYPE is
  9882. the new type or left-hand side type. RTYPE is the old type or
  9883. right-hand side type. Returns TRUE if LTYPE is missing the desired
  9884. attribute. */
  9885. bool
  9886. check_missing_format_attribute (tree ltype, tree rtype)
  9887. {
  9888. tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
  9889. tree ra;
  9890. for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
  9891. if (is_attribute_p ("format", TREE_PURPOSE (ra)))
  9892. break;
  9893. if (ra)
  9894. {
  9895. tree la;
  9896. for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
  9897. if (is_attribute_p ("format", TREE_PURPOSE (la)))
  9898. break;
  9899. return !la;
  9900. }
  9901. else
  9902. return false;
  9903. }
  9904. /* Subscripting with type char is likely to lose on a machine where
  9905. chars are signed. So warn on any machine, but optionally. Don't
  9906. warn for unsigned char since that type is safe. Don't warn for
  9907. signed char because anyone who uses that must have done so
  9908. deliberately. Furthermore, we reduce the false positive load by
  9909. warning only for non-constant value of type char. */
  9910. void
  9911. warn_array_subscript_with_type_char (location_t loc, tree index)
  9912. {
  9913. if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
  9914. && TREE_CODE (index) != INTEGER_CST)
  9915. warning_at (loc, OPT_Wchar_subscripts,
  9916. "array subscript has type %<char%>");
  9917. }
  9918. /* Implement -Wparentheses for the unexpected C precedence rules, to
  9919. cover cases like x + y << z which readers are likely to
  9920. misinterpret. We have seen an expression in which CODE is a binary
  9921. operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
  9922. before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
  9923. CODE_RIGHT may be ERROR_MARK, which means that that side of the
  9924. expression was not formed using a binary or unary operator, or it
  9925. was enclosed in parentheses. */
  9926. void
  9927. warn_about_parentheses (location_t loc, enum tree_code code,
  9928. enum tree_code code_left, tree arg_left,
  9929. enum tree_code code_right, tree arg_right)
  9930. {
  9931. if (!warn_parentheses)
  9932. return;
  9933. /* This macro tests that the expression ARG with original tree code
  9934. CODE appears to be a boolean expression. or the result of folding a
  9935. boolean expression. */
  9936. #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
  9937. (truth_value_p (TREE_CODE (ARG)) \
  9938. || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
  9939. /* Folding may create 0 or 1 integers from other expressions. */ \
  9940. || ((CODE) != INTEGER_CST \
  9941. && (integer_onep (ARG) || integer_zerop (ARG))))
  9942. switch (code)
  9943. {
  9944. case LSHIFT_EXPR:
  9945. if (code_left == PLUS_EXPR)
  9946. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9947. "suggest parentheses around %<+%> inside %<<<%>");
  9948. else if (code_right == PLUS_EXPR)
  9949. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9950. "suggest parentheses around %<+%> inside %<<<%>");
  9951. else if (code_left == MINUS_EXPR)
  9952. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9953. "suggest parentheses around %<-%> inside %<<<%>");
  9954. else if (code_right == MINUS_EXPR)
  9955. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9956. "suggest parentheses around %<-%> inside %<<<%>");
  9957. return;
  9958. case RSHIFT_EXPR:
  9959. if (code_left == PLUS_EXPR)
  9960. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9961. "suggest parentheses around %<+%> inside %<>>%>");
  9962. else if (code_right == PLUS_EXPR)
  9963. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9964. "suggest parentheses around %<+%> inside %<>>%>");
  9965. else if (code_left == MINUS_EXPR)
  9966. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9967. "suggest parentheses around %<-%> inside %<>>%>");
  9968. else if (code_right == MINUS_EXPR)
  9969. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9970. "suggest parentheses around %<-%> inside %<>>%>");
  9971. return;
  9972. case TRUTH_ORIF_EXPR:
  9973. if (code_left == TRUTH_ANDIF_EXPR)
  9974. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9975. "suggest parentheses around %<&&%> within %<||%>");
  9976. else if (code_right == TRUTH_ANDIF_EXPR)
  9977. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9978. "suggest parentheses around %<&&%> within %<||%>");
  9979. return;
  9980. case BIT_IOR_EXPR:
  9981. if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
  9982. || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
  9983. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9984. "suggest parentheses around arithmetic in operand of %<|%>");
  9985. else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
  9986. || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
  9987. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9988. "suggest parentheses around arithmetic in operand of %<|%>");
  9989. /* Check cases like x|y==z */
  9990. else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
  9991. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  9992. "suggest parentheses around comparison in operand of %<|%>");
  9993. else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
  9994. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  9995. "suggest parentheses around comparison in operand of %<|%>");
  9996. /* Check cases like !x | y */
  9997. else if (code_left == TRUTH_NOT_EXPR
  9998. && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
  9999. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10000. "suggest parentheses around operand of "
  10001. "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
  10002. return;
  10003. case BIT_XOR_EXPR:
  10004. if (code_left == BIT_AND_EXPR
  10005. || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
  10006. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10007. "suggest parentheses around arithmetic in operand of %<^%>");
  10008. else if (code_right == BIT_AND_EXPR
  10009. || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
  10010. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10011. "suggest parentheses around arithmetic in operand of %<^%>");
  10012. /* Check cases like x^y==z */
  10013. else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
  10014. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10015. "suggest parentheses around comparison in operand of %<^%>");
  10016. else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
  10017. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10018. "suggest parentheses around comparison in operand of %<^%>");
  10019. return;
  10020. case BIT_AND_EXPR:
  10021. if (code_left == PLUS_EXPR)
  10022. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10023. "suggest parentheses around %<+%> in operand of %<&%>");
  10024. else if (code_right == PLUS_EXPR)
  10025. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10026. "suggest parentheses around %<+%> in operand of %<&%>");
  10027. else if (code_left == MINUS_EXPR)
  10028. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10029. "suggest parentheses around %<-%> in operand of %<&%>");
  10030. else if (code_right == MINUS_EXPR)
  10031. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10032. "suggest parentheses around %<-%> in operand of %<&%>");
  10033. /* Check cases like x&y==z */
  10034. else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
  10035. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10036. "suggest parentheses around comparison in operand of %<&%>");
  10037. else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
  10038. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10039. "suggest parentheses around comparison in operand of %<&%>");
  10040. /* Check cases like !x & y */
  10041. else if (code_left == TRUTH_NOT_EXPR
  10042. && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
  10043. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10044. "suggest parentheses around operand of "
  10045. "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
  10046. return;
  10047. case EQ_EXPR:
  10048. if (TREE_CODE_CLASS (code_left) == tcc_comparison)
  10049. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10050. "suggest parentheses around comparison in operand of %<==%>");
  10051. else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
  10052. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10053. "suggest parentheses around comparison in operand of %<==%>");
  10054. return;
  10055. case NE_EXPR:
  10056. if (TREE_CODE_CLASS (code_left) == tcc_comparison)
  10057. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10058. "suggest parentheses around comparison in operand of %<!=%>");
  10059. else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
  10060. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10061. "suggest parentheses around comparison in operand of %<!=%>");
  10062. return;
  10063. default:
  10064. if (TREE_CODE_CLASS (code) == tcc_comparison)
  10065. {
  10066. if (TREE_CODE_CLASS (code_left) == tcc_comparison
  10067. && code_left != NE_EXPR && code_left != EQ_EXPR
  10068. && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
  10069. warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
  10070. "comparisons like %<X<=Y<=Z%> do not "
  10071. "have their mathematical meaning");
  10072. else if (TREE_CODE_CLASS (code_right) == tcc_comparison
  10073. && code_right != NE_EXPR && code_right != EQ_EXPR
  10074. && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
  10075. warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
  10076. "comparisons like %<X<=Y<=Z%> do not "
  10077. "have their mathematical meaning");
  10078. }
  10079. return;
  10080. }
  10081. #undef NOT_A_BOOLEAN_EXPR_P
  10082. }
  10083. /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
  10084. void
  10085. warn_for_unused_label (tree label)
  10086. {
  10087. if (!TREE_USED (label))
  10088. {
  10089. if (DECL_INITIAL (label))
  10090. warning (OPT_Wunused_label, "label %q+D defined but not used", label);
  10091. else
  10092. warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
  10093. }
  10094. }
  10095. /* Warn for division by zero according to the value of DIVISOR. LOC
  10096. is the location of the division operator. */
  10097. void
  10098. warn_for_div_by_zero (location_t loc, tree divisor)
  10099. {
  10100. /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
  10101. about division by zero. Do not issue a warning if DIVISOR has a
  10102. floating-point type, since we consider 0.0/0.0 a valid way of
  10103. generating a NaN. */
  10104. if (c_inhibit_evaluation_warnings == 0
  10105. && (integer_zerop (divisor) || fixed_zerop (divisor)))
  10106. warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
  10107. }
  10108. /* Subroutine of build_binary_op. Give warnings for comparisons
  10109. between signed and unsigned quantities that may fail. Do the
  10110. checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
  10111. so that casts will be considered, but default promotions won't
  10112. be.
  10113. LOCATION is the location of the comparison operator.
  10114. The arguments of this function map directly to local variables
  10115. of build_binary_op. */
  10116. void
  10117. warn_for_sign_compare (location_t location,
  10118. tree orig_op0, tree orig_op1,
  10119. tree op0, tree op1,
  10120. tree result_type, enum tree_code resultcode)
  10121. {
  10122. int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
  10123. int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
  10124. int unsignedp0, unsignedp1;
  10125. /* In C++, check for comparison of different enum types. */
  10126. if (c_dialect_cxx()
  10127. && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
  10128. && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
  10129. && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
  10130. != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
  10131. {
  10132. warning_at (location,
  10133. OPT_Wsign_compare, "comparison between types %qT and %qT",
  10134. TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
  10135. }
  10136. /* Do not warn if the comparison is being done in a signed type,
  10137. since the signed type will only be chosen if it can represent
  10138. all the values of the unsigned type. */
  10139. if (!TYPE_UNSIGNED (result_type))
  10140. /* OK */;
  10141. /* Do not warn if both operands are unsigned. */
  10142. else if (op0_signed == op1_signed)
  10143. /* OK */;
  10144. else
  10145. {
  10146. tree sop, uop, base_type;
  10147. bool ovf;
  10148. if (op0_signed)
  10149. sop = orig_op0, uop = orig_op1;
  10150. else
  10151. sop = orig_op1, uop = orig_op0;
  10152. STRIP_TYPE_NOPS (sop);
  10153. STRIP_TYPE_NOPS (uop);
  10154. base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
  10155. ? TREE_TYPE (result_type) : result_type);
  10156. /* Do not warn if the signed quantity is an unsuffixed integer
  10157. literal (or some static constant expression involving such
  10158. literals or a conditional expression involving such literals)
  10159. and it is non-negative. */
  10160. if (tree_expr_nonnegative_warnv_p (sop, &ovf))
  10161. /* OK */;
  10162. /* Do not warn if the comparison is an equality operation, the
  10163. unsigned quantity is an integral constant, and it would fit
  10164. in the result if the result were signed. */
  10165. else if (TREE_CODE (uop) == INTEGER_CST
  10166. && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
  10167. && int_fits_type_p (uop, c_common_signed_type (base_type)))
  10168. /* OK */;
  10169. /* In C, do not warn if the unsigned quantity is an enumeration
  10170. constant and its maximum value would fit in the result if the
  10171. result were signed. */
  10172. else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
  10173. && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
  10174. && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
  10175. c_common_signed_type (base_type)))
  10176. /* OK */;
  10177. else
  10178. warning_at (location,
  10179. OPT_Wsign_compare,
  10180. "comparison between signed and unsigned integer expressions");
  10181. }
  10182. /* Warn if two unsigned values are being compared in a size larger
  10183. than their original size, and one (and only one) is the result of
  10184. a `~' operator. This comparison will always fail.
  10185. Also warn if one operand is a constant, and the constant does not
  10186. have all bits set that are set in the ~ operand when it is
  10187. extended. */
  10188. op0 = c_common_get_narrower (op0, &unsignedp0);
  10189. op1 = c_common_get_narrower (op1, &unsignedp1);
  10190. if ((TREE_CODE (op0) == BIT_NOT_EXPR)
  10191. ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
  10192. {
  10193. if (TREE_CODE (op0) == BIT_NOT_EXPR)
  10194. op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
  10195. if (TREE_CODE (op1) == BIT_NOT_EXPR)
  10196. op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
  10197. if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
  10198. {
  10199. tree primop;
  10200. HOST_WIDE_INT constant, mask;
  10201. int unsignedp;
  10202. unsigned int bits;
  10203. if (tree_fits_shwi_p (op0))
  10204. {
  10205. primop = op1;
  10206. unsignedp = unsignedp1;
  10207. constant = tree_to_shwi (op0);
  10208. }
  10209. else
  10210. {
  10211. primop = op0;
  10212. unsignedp = unsignedp0;
  10213. constant = tree_to_shwi (op1);
  10214. }
  10215. bits = TYPE_PRECISION (TREE_TYPE (primop));
  10216. if (bits < TYPE_PRECISION (result_type)
  10217. && bits < HOST_BITS_PER_LONG && unsignedp)
  10218. {
  10219. mask = (~ (HOST_WIDE_INT) 0) << bits;
  10220. if ((mask & constant) != mask)
  10221. {
  10222. if (constant == 0)
  10223. warning_at (location, OPT_Wsign_compare,
  10224. "promoted ~unsigned is always non-zero");
  10225. else
  10226. warning_at (location, OPT_Wsign_compare,
  10227. "comparison of promoted ~unsigned with constant");
  10228. }
  10229. }
  10230. }
  10231. else if (unsignedp0 && unsignedp1
  10232. && (TYPE_PRECISION (TREE_TYPE (op0))
  10233. < TYPE_PRECISION (result_type))
  10234. && (TYPE_PRECISION (TREE_TYPE (op1))
  10235. < TYPE_PRECISION (result_type)))
  10236. warning_at (location, OPT_Wsign_compare,
  10237. "comparison of promoted ~unsigned with unsigned");
  10238. }
  10239. }
  10240. /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
  10241. type via c_common_type. If -Wdouble-promotion is in use, and the
  10242. conditions for warning have been met, issue a warning. GMSGID is
  10243. the warning message. It must have two %T specifiers for the type
  10244. that was converted (generally "float") and the type to which it was
  10245. converted (generally "double), respectively. LOC is the location
  10246. to which the awrning should refer. */
  10247. void
  10248. do_warn_double_promotion (tree result_type, tree type1, tree type2,
  10249. const char *gmsgid, location_t loc)
  10250. {
  10251. tree source_type;
  10252. if (!warn_double_promotion)
  10253. return;
  10254. /* If the conversion will not occur at run-time, there is no need to
  10255. warn about it. */
  10256. if (c_inhibit_evaluation_warnings)
  10257. return;
  10258. if (TYPE_MAIN_VARIANT (result_type) != double_type_node
  10259. && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
  10260. return;
  10261. if (TYPE_MAIN_VARIANT (type1) == float_type_node
  10262. || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
  10263. source_type = type1;
  10264. else if (TYPE_MAIN_VARIANT (type2) == float_type_node
  10265. || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
  10266. source_type = type2;
  10267. else
  10268. return;
  10269. warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
  10270. }
  10271. /* Setup a TYPE_DECL node as a typedef representation.
  10272. X is a TYPE_DECL for a typedef statement. Create a brand new
  10273. ..._TYPE node (which will be just a variant of the existing
  10274. ..._TYPE node with identical properties) and then install X
  10275. as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
  10276. The whole point here is to end up with a situation where each
  10277. and every ..._TYPE node the compiler creates will be uniquely
  10278. associated with AT MOST one node representing a typedef name.
  10279. This way, even though the compiler substitutes corresponding
  10280. ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
  10281. early on, later parts of the compiler can always do the reverse
  10282. translation and get back the corresponding typedef name. For
  10283. example, given:
  10284. typedef struct S MY_TYPE;
  10285. MY_TYPE object;
  10286. Later parts of the compiler might only know that `object' was of
  10287. type `struct S' if it were not for code just below. With this
  10288. code however, later parts of the compiler see something like:
  10289. struct S' == struct S
  10290. typedef struct S' MY_TYPE;
  10291. struct S' object;
  10292. And they can then deduce (from the node for type struct S') that
  10293. the original object declaration was:
  10294. MY_TYPE object;
  10295. Being able to do this is important for proper support of protoize,
  10296. and also for generating precise symbolic debugging information
  10297. which takes full account of the programmer's (typedef) vocabulary.
  10298. Obviously, we don't want to generate a duplicate ..._TYPE node if
  10299. the TYPE_DECL node that we are now processing really represents a
  10300. standard built-in type. */
  10301. void
  10302. set_underlying_type (tree x)
  10303. {
  10304. if (x == error_mark_node)
  10305. return;
  10306. if (DECL_IS_BUILTIN (x))
  10307. {
  10308. if (TYPE_NAME (TREE_TYPE (x)) == 0)
  10309. TYPE_NAME (TREE_TYPE (x)) = x;
  10310. }
  10311. else if (TREE_TYPE (x) != error_mark_node
  10312. && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
  10313. {
  10314. tree tt = TREE_TYPE (x);
  10315. DECL_ORIGINAL_TYPE (x) = tt;
  10316. tt = build_variant_type_copy (tt);
  10317. TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
  10318. TYPE_NAME (tt) = x;
  10319. TREE_USED (tt) = TREE_USED (x);
  10320. TREE_TYPE (x) = tt;
  10321. }
  10322. }
  10323. /* Record the types used by the current global variable declaration
  10324. being parsed, so that we can decide later to emit their debug info.
  10325. Those types are in types_used_by_cur_var_decl, and we are going to
  10326. store them in the types_used_by_vars_hash hash table.
  10327. DECL is the declaration of the global variable that has been parsed. */
  10328. void
  10329. record_types_used_by_current_var_decl (tree decl)
  10330. {
  10331. gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
  10332. while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
  10333. {
  10334. tree type = types_used_by_cur_var_decl->pop ();
  10335. types_used_by_var_decl_insert (type, decl);
  10336. }
  10337. }
  10338. /* If DECL is a typedef that is declared in the current function,
  10339. record it for the purpose of -Wunused-local-typedefs. */
  10340. void
  10341. record_locally_defined_typedef (tree decl)
  10342. {
  10343. struct c_language_function *l;
  10344. if (!warn_unused_local_typedefs
  10345. || cfun == NULL
  10346. /* if this is not a locally defined typedef then we are not
  10347. interested. */
  10348. || !is_typedef_decl (decl)
  10349. || !decl_function_context (decl))
  10350. return;
  10351. l = (struct c_language_function *) cfun->language;
  10352. vec_safe_push (l->local_typedefs, decl);
  10353. }
  10354. /* If T is a TYPE_DECL declared locally, mark it as used. */
  10355. void
  10356. maybe_record_typedef_use (tree t)
  10357. {
  10358. if (!is_typedef_decl (t))
  10359. return;
  10360. TREE_USED (t) = true;
  10361. }
  10362. /* Warn if there are some unused locally defined typedefs in the
  10363. current function. */
  10364. void
  10365. maybe_warn_unused_local_typedefs (void)
  10366. {
  10367. int i;
  10368. tree decl;
  10369. /* The number of times we have emitted -Wunused-local-typedefs
  10370. warnings. If this is different from errorcount, that means some
  10371. unrelated errors have been issued. In which case, we'll avoid
  10372. emitting "unused-local-typedefs" warnings. */
  10373. static int unused_local_typedefs_warn_count;
  10374. struct c_language_function *l;
  10375. if (cfun == NULL)
  10376. return;
  10377. if ((l = (struct c_language_function *) cfun->language) == NULL)
  10378. return;
  10379. if (warn_unused_local_typedefs
  10380. && errorcount == unused_local_typedefs_warn_count)
  10381. {
  10382. FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
  10383. if (!TREE_USED (decl))
  10384. warning_at (DECL_SOURCE_LOCATION (decl),
  10385. OPT_Wunused_local_typedefs,
  10386. "typedef %qD locally defined but not used", decl);
  10387. unused_local_typedefs_warn_count = errorcount;
  10388. }
  10389. vec_free (l->local_typedefs);
  10390. }
  10391. /* Warn about boolean expression compared with an integer value different
  10392. from true/false. Warns also e.g. about "(i1 == i2) == 2".
  10393. LOC is the location of the comparison, CODE is its code, OP0 and OP1
  10394. are the operands of the comparison. The caller must ensure that
  10395. either operand is a boolean expression. */
  10396. void
  10397. maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
  10398. tree op1)
  10399. {
  10400. if (TREE_CODE_CLASS (code) != tcc_comparison)
  10401. return;
  10402. tree cst = (TREE_CODE (op0) == INTEGER_CST)
  10403. ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
  10404. if (!cst)
  10405. return;
  10406. if (!integer_zerop (cst) && !integer_onep (cst))
  10407. {
  10408. int sign = (TREE_CODE (op0) == INTEGER_CST)
  10409. ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst);
  10410. if (code == EQ_EXPR
  10411. || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
  10412. || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
  10413. warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
  10414. "with boolean expression is always false", cst);
  10415. else
  10416. warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
  10417. "with boolean expression is always true", cst);
  10418. }
  10419. }
  10420. /* The C and C++ parsers both use vectors to hold function arguments.
  10421. For efficiency, we keep a cache of unused vectors. This is the
  10422. cache. */
  10423. typedef vec<tree, va_gc> *tree_gc_vec;
  10424. static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
  10425. /* Return a new vector from the cache. If the cache is empty,
  10426. allocate a new vector. These vectors are GC'ed, so it is OK if the
  10427. pointer is not released.. */
  10428. vec<tree, va_gc> *
  10429. make_tree_vector (void)
  10430. {
  10431. if (tree_vector_cache && !tree_vector_cache->is_empty ())
  10432. return tree_vector_cache->pop ();
  10433. else
  10434. {
  10435. /* Passing 0 to vec::alloc returns NULL, and our callers require
  10436. that we always return a non-NULL value. The vector code uses
  10437. 4 when growing a NULL vector, so we do too. */
  10438. vec<tree, va_gc> *v;
  10439. vec_alloc (v, 4);
  10440. return v;
  10441. }
  10442. }
  10443. /* Release a vector of trees back to the cache. */
  10444. void
  10445. release_tree_vector (vec<tree, va_gc> *vec)
  10446. {
  10447. if (vec != NULL)
  10448. {
  10449. vec->truncate (0);
  10450. vec_safe_push (tree_vector_cache, vec);
  10451. }
  10452. }
  10453. /* Get a new tree vector holding a single tree. */
  10454. vec<tree, va_gc> *
  10455. make_tree_vector_single (tree t)
  10456. {
  10457. vec<tree, va_gc> *ret = make_tree_vector ();
  10458. ret->quick_push (t);
  10459. return ret;
  10460. }
  10461. /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
  10462. vec<tree, va_gc> *
  10463. make_tree_vector_from_list (tree list)
  10464. {
  10465. vec<tree, va_gc> *ret = make_tree_vector ();
  10466. for (; list; list = TREE_CHAIN (list))
  10467. vec_safe_push (ret, TREE_VALUE (list));
  10468. return ret;
  10469. }
  10470. /* Get a new tree vector which is a copy of an existing one. */
  10471. vec<tree, va_gc> *
  10472. make_tree_vector_copy (const vec<tree, va_gc> *orig)
  10473. {
  10474. vec<tree, va_gc> *ret;
  10475. unsigned int ix;
  10476. tree t;
  10477. ret = make_tree_vector ();
  10478. vec_safe_reserve (ret, vec_safe_length (orig));
  10479. FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
  10480. ret->quick_push (t);
  10481. return ret;
  10482. }
  10483. /* Return true if KEYWORD starts a type specifier. */
  10484. bool
  10485. keyword_begins_type_specifier (enum rid keyword)
  10486. {
  10487. switch (keyword)
  10488. {
  10489. case RID_AUTO_TYPE:
  10490. case RID_INT:
  10491. case RID_CHAR:
  10492. case RID_FLOAT:
  10493. case RID_DOUBLE:
  10494. case RID_VOID:
  10495. case RID_UNSIGNED:
  10496. case RID_LONG:
  10497. case RID_SHORT:
  10498. case RID_SIGNED:
  10499. case RID_DFLOAT32:
  10500. case RID_DFLOAT64:
  10501. case RID_DFLOAT128:
  10502. case RID_FRACT:
  10503. case RID_ACCUM:
  10504. case RID_BOOL:
  10505. case RID_WCHAR:
  10506. case RID_CHAR16:
  10507. case RID_CHAR32:
  10508. case RID_SAT:
  10509. case RID_COMPLEX:
  10510. case RID_TYPEOF:
  10511. case RID_STRUCT:
  10512. case RID_CLASS:
  10513. case RID_UNION:
  10514. case RID_ENUM:
  10515. return true;
  10516. default:
  10517. if (keyword >= RID_FIRST_INT_N
  10518. && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
  10519. && int_n_enabled_p[keyword-RID_FIRST_INT_N])
  10520. return true;
  10521. return false;
  10522. }
  10523. }
  10524. /* Return true if KEYWORD names a type qualifier. */
  10525. bool
  10526. keyword_is_type_qualifier (enum rid keyword)
  10527. {
  10528. switch (keyword)
  10529. {
  10530. case RID_CONST:
  10531. case RID_VOLATILE:
  10532. case RID_RESTRICT:
  10533. case RID_ATOMIC:
  10534. return true;
  10535. default:
  10536. return false;
  10537. }
  10538. }
  10539. /* Return true if KEYWORD names a storage class specifier.
  10540. RID_TYPEDEF is not included in this list despite `typedef' being
  10541. listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
  10542. such for syntactic convenience only. */
  10543. bool
  10544. keyword_is_storage_class_specifier (enum rid keyword)
  10545. {
  10546. switch (keyword)
  10547. {
  10548. case RID_STATIC:
  10549. case RID_EXTERN:
  10550. case RID_REGISTER:
  10551. case RID_AUTO:
  10552. case RID_MUTABLE:
  10553. case RID_THREAD:
  10554. return true;
  10555. default:
  10556. return false;
  10557. }
  10558. }
  10559. /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
  10560. static bool
  10561. keyword_is_function_specifier (enum rid keyword)
  10562. {
  10563. switch (keyword)
  10564. {
  10565. case RID_INLINE:
  10566. case RID_NORETURN:
  10567. case RID_VIRTUAL:
  10568. case RID_EXPLICIT:
  10569. return true;
  10570. default:
  10571. return false;
  10572. }
  10573. }
  10574. /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
  10575. declaration-specifier (C99 6.7). */
  10576. bool
  10577. keyword_is_decl_specifier (enum rid keyword)
  10578. {
  10579. if (keyword_is_storage_class_specifier (keyword)
  10580. || keyword_is_type_qualifier (keyword)
  10581. || keyword_is_function_specifier (keyword))
  10582. return true;
  10583. switch (keyword)
  10584. {
  10585. case RID_TYPEDEF:
  10586. case RID_FRIEND:
  10587. case RID_CONSTEXPR:
  10588. return true;
  10589. default:
  10590. return false;
  10591. }
  10592. }
  10593. /* Initialize language-specific-bits of tree_contains_struct. */
  10594. void
  10595. c_common_init_ts (void)
  10596. {
  10597. MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
  10598. MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
  10599. MARK_TS_TYPED (ARRAY_NOTATION_REF);
  10600. }
  10601. /* Build a user-defined numeric literal out of an integer constant type VALUE
  10602. with identifier SUFFIX. */
  10603. tree
  10604. build_userdef_literal (tree suffix_id, tree value,
  10605. enum overflow_type overflow, tree num_string)
  10606. {
  10607. tree literal = make_node (USERDEF_LITERAL);
  10608. USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
  10609. USERDEF_LITERAL_VALUE (literal) = value;
  10610. USERDEF_LITERAL_OVERFLOW (literal) = overflow;
  10611. USERDEF_LITERAL_NUM_STRING (literal) = num_string;
  10612. return literal;
  10613. }
  10614. /* For vector[index], convert the vector to a
  10615. pointer of the underlying type. Return true if the resulting
  10616. ARRAY_REF should not be an lvalue. */
  10617. bool
  10618. convert_vector_to_pointer_for_subscript (location_t loc,
  10619. tree *vecp, tree index)
  10620. {
  10621. bool ret = false;
  10622. if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
  10623. {
  10624. tree type = TREE_TYPE (*vecp);
  10625. tree type1;
  10626. ret = !lvalue_p (*vecp);
  10627. if (TREE_CODE (index) == INTEGER_CST)
  10628. if (!tree_fits_uhwi_p (index)
  10629. || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
  10630. warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
  10631. if (ret)
  10632. {
  10633. tree tmp = create_tmp_var_raw (type);
  10634. DECL_SOURCE_LOCATION (tmp) = loc;
  10635. *vecp = c_save_expr (*vecp);
  10636. if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
  10637. {
  10638. bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
  10639. *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
  10640. *vecp
  10641. = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
  10642. *vecp, NULL_TREE, NULL_TREE),
  10643. non_const);
  10644. }
  10645. else
  10646. *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
  10647. NULL_TREE, NULL_TREE);
  10648. SET_EXPR_LOCATION (*vecp, loc);
  10649. c_common_mark_addressable_vec (tmp);
  10650. }
  10651. else
  10652. c_common_mark_addressable_vec (*vecp);
  10653. type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
  10654. type1 = build_pointer_type (TREE_TYPE (*vecp));
  10655. bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
  10656. if (!ref_all
  10657. && !DECL_P (*vecp))
  10658. {
  10659. /* If the original vector isn't declared may_alias and it
  10660. isn't a bare vector look if the subscripting would
  10661. alias the vector we subscript, and if not, force ref-all. */
  10662. alias_set_type vecset = get_alias_set (*vecp);
  10663. alias_set_type sset = get_alias_set (type);
  10664. if (!alias_sets_must_conflict_p (sset, vecset)
  10665. && !alias_set_subset_of (sset, vecset))
  10666. ref_all = true;
  10667. }
  10668. type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
  10669. *vecp = build1 (ADDR_EXPR, type1, *vecp);
  10670. *vecp = convert (type, *vecp);
  10671. }
  10672. return ret;
  10673. }
  10674. /* Determine which of the operands, if any, is a scalar that needs to be
  10675. converted to a vector, for the range of operations. */
  10676. enum stv_conv
  10677. scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
  10678. bool complain)
  10679. {
  10680. tree type0 = TREE_TYPE (op0);
  10681. tree type1 = TREE_TYPE (op1);
  10682. bool integer_only_op = false;
  10683. enum stv_conv ret = stv_firstarg;
  10684. gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
  10685. || TREE_CODE (type1) == VECTOR_TYPE);
  10686. switch (code)
  10687. {
  10688. /* Most GENERIC binary expressions require homogeneous arguments.
  10689. LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
  10690. argument that is a vector and a second one that is a scalar, so
  10691. we never return stv_secondarg for them. */
  10692. case RSHIFT_EXPR:
  10693. case LSHIFT_EXPR:
  10694. if (TREE_CODE (type0) == INTEGER_TYPE
  10695. && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
  10696. {
  10697. if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
  10698. {
  10699. if (complain)
  10700. error_at (loc, "conversion of scalar %qT to vector %qT "
  10701. "involves truncation", type0, type1);
  10702. return stv_error;
  10703. }
  10704. else
  10705. return stv_firstarg;
  10706. }
  10707. break;
  10708. case BIT_IOR_EXPR:
  10709. case BIT_XOR_EXPR:
  10710. case BIT_AND_EXPR:
  10711. integer_only_op = true;
  10712. /* ... fall through ... */
  10713. case VEC_COND_EXPR:
  10714. case PLUS_EXPR:
  10715. case MINUS_EXPR:
  10716. case MULT_EXPR:
  10717. case TRUNC_DIV_EXPR:
  10718. case CEIL_DIV_EXPR:
  10719. case FLOOR_DIV_EXPR:
  10720. case ROUND_DIV_EXPR:
  10721. case EXACT_DIV_EXPR:
  10722. case TRUNC_MOD_EXPR:
  10723. case FLOOR_MOD_EXPR:
  10724. case RDIV_EXPR:
  10725. case EQ_EXPR:
  10726. case NE_EXPR:
  10727. case LE_EXPR:
  10728. case GE_EXPR:
  10729. case LT_EXPR:
  10730. case GT_EXPR:
  10731. /* What about UNLT_EXPR? */
  10732. if (TREE_CODE (type0) == VECTOR_TYPE)
  10733. {
  10734. tree tmp;
  10735. ret = stv_secondarg;
  10736. /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
  10737. tmp = type0; type0 = type1; type1 = tmp;
  10738. tmp = op0; op0 = op1; op1 = tmp;
  10739. }
  10740. if (TREE_CODE (type0) == INTEGER_TYPE
  10741. && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
  10742. {
  10743. if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
  10744. {
  10745. if (complain)
  10746. error_at (loc, "conversion of scalar %qT to vector %qT "
  10747. "involves truncation", type0, type1);
  10748. return stv_error;
  10749. }
  10750. return ret;
  10751. }
  10752. else if (!integer_only_op
  10753. /* Allow integer --> real conversion if safe. */
  10754. && (TREE_CODE (type0) == REAL_TYPE
  10755. || TREE_CODE (type0) == INTEGER_TYPE)
  10756. && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
  10757. {
  10758. if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
  10759. {
  10760. if (complain)
  10761. error_at (loc, "conversion of scalar %qT to vector %qT "
  10762. "involves truncation", type0, type1);
  10763. return stv_error;
  10764. }
  10765. return ret;
  10766. }
  10767. default:
  10768. break;
  10769. }
  10770. return stv_nothing;
  10771. }
  10772. /* Return true iff ALIGN is an integral constant that is a fundamental
  10773. alignment, as defined by [basic.align] in the c++-11
  10774. specifications.
  10775. That is:
  10776. [A fundamental alignment is represented by an alignment less than or
  10777. equal to the greatest alignment supported by the implementation
  10778. in all contexts, which is equal to
  10779. alignof(max_align_t)]. */
  10780. bool
  10781. cxx_fundamental_alignment_p (unsigned align)
  10782. {
  10783. return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
  10784. TYPE_ALIGN (long_double_type_node)));
  10785. }
  10786. /* Return true if T is a pointer to a zero-sized aggregate. */
  10787. bool
  10788. pointer_to_zero_sized_aggr_p (tree t)
  10789. {
  10790. if (!POINTER_TYPE_P (t))
  10791. return false;
  10792. t = TREE_TYPE (t);
  10793. return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
  10794. }
  10795. #include "gt-c-family-c-common.h"