typeck.c 283 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312
  1. /* Build expressions with type checking for C++ compiler.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. Hacked by Michael Tiemann (tiemann@cygnus.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This file is part of the C++ front end.
  17. It contains routines to build C++ expressions given their operands,
  18. including computing the types of the result, C and C++ specific error
  19. checks, and some optimization. */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "hash-set.h"
  25. #include "machmode.h"
  26. #include "vec.h"
  27. #include "double-int.h"
  28. #include "input.h"
  29. #include "alias.h"
  30. #include "symtab.h"
  31. #include "wide-int.h"
  32. #include "inchash.h"
  33. #include "tree.h"
  34. #include "fold-const.h"
  35. #include "stor-layout.h"
  36. #include "varasm.h"
  37. #include "cp-tree.h"
  38. #include "flags.h"
  39. #include "diagnostic.h"
  40. #include "intl.h"
  41. #include "target.h"
  42. #include "convert.h"
  43. #include "c-family/c-common.h"
  44. #include "c-family/c-objc.h"
  45. #include "c-family/c-ubsan.h"
  46. #include "params.h"
  47. static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
  48. static tree cp_build_function_call (tree, tree, tsubst_flags_t);
  49. static tree pfn_from_ptrmemfunc (tree);
  50. static tree delta_from_ptrmemfunc (tree);
  51. static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
  52. tsubst_flags_t, int);
  53. static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
  54. static tree rationalize_conditional_expr (enum tree_code, tree,
  55. tsubst_flags_t);
  56. static int comp_ptr_ttypes_real (tree, tree, int);
  57. static bool comp_except_types (tree, tree, bool);
  58. static bool comp_array_types (const_tree, const_tree, bool);
  59. static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
  60. static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
  61. static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
  62. static bool casts_away_constness (tree, tree, tsubst_flags_t);
  63. static bool maybe_warn_about_returning_address_of_local (tree);
  64. static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
  65. static void warn_args_num (location_t, tree, bool);
  66. static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
  67. tsubst_flags_t);
  68. /* Do `exp = require_complete_type (exp);' to make sure exp
  69. does not have an incomplete type. (That includes void types.)
  70. Returns error_mark_node if the VALUE does not have
  71. complete type when this function returns. */
  72. tree
  73. require_complete_type_sfinae (tree value, tsubst_flags_t complain)
  74. {
  75. tree type;
  76. if (processing_template_decl || value == error_mark_node)
  77. return value;
  78. if (TREE_CODE (value) == OVERLOAD)
  79. type = unknown_type_node;
  80. else
  81. type = TREE_TYPE (value);
  82. if (type == error_mark_node)
  83. return error_mark_node;
  84. /* First, detect a valid value with a complete type. */
  85. if (COMPLETE_TYPE_P (type))
  86. return value;
  87. if (complete_type_or_maybe_complain (type, value, complain))
  88. return value;
  89. else
  90. return error_mark_node;
  91. }
  92. tree
  93. require_complete_type (tree value)
  94. {
  95. return require_complete_type_sfinae (value, tf_warning_or_error);
  96. }
  97. /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
  98. a template instantiation, do the instantiation. Returns TYPE,
  99. whether or not it could be completed, unless something goes
  100. horribly wrong, in which case the error_mark_node is returned. */
  101. tree
  102. complete_type (tree type)
  103. {
  104. if (type == NULL_TREE)
  105. /* Rather than crash, we return something sure to cause an error
  106. at some point. */
  107. return error_mark_node;
  108. if (type == error_mark_node || COMPLETE_TYPE_P (type))
  109. ;
  110. else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
  111. {
  112. tree t = complete_type (TREE_TYPE (type));
  113. unsigned int needs_constructing, has_nontrivial_dtor;
  114. if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
  115. layout_type (type);
  116. needs_constructing
  117. = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
  118. has_nontrivial_dtor
  119. = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
  120. for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
  121. {
  122. TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
  123. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
  124. }
  125. }
  126. else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
  127. instantiate_class_template (TYPE_MAIN_VARIANT (type));
  128. return type;
  129. }
  130. /* Like complete_type, but issue an error if the TYPE cannot be completed.
  131. VALUE is used for informative diagnostics.
  132. Returns NULL_TREE if the type cannot be made complete. */
  133. tree
  134. complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
  135. {
  136. type = complete_type (type);
  137. if (type == error_mark_node)
  138. /* We already issued an error. */
  139. return NULL_TREE;
  140. else if (!COMPLETE_TYPE_P (type))
  141. {
  142. if (complain & tf_error)
  143. cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
  144. return NULL_TREE;
  145. }
  146. else
  147. return type;
  148. }
  149. tree
  150. complete_type_or_else (tree type, tree value)
  151. {
  152. return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
  153. }
  154. /* Return truthvalue of whether type of EXP is instantiated. */
  155. int
  156. type_unknown_p (const_tree exp)
  157. {
  158. return (TREE_CODE (exp) == TREE_LIST
  159. || TREE_TYPE (exp) == unknown_type_node);
  160. }
  161. /* Return the common type of two parameter lists.
  162. We assume that comptypes has already been done and returned 1;
  163. if that isn't so, this may crash.
  164. As an optimization, free the space we allocate if the parameter
  165. lists are already common. */
  166. static tree
  167. commonparms (tree p1, tree p2)
  168. {
  169. tree oldargs = p1, newargs, n;
  170. int i, len;
  171. int any_change = 0;
  172. len = list_length (p1);
  173. newargs = tree_last (p1);
  174. if (newargs == void_list_node)
  175. i = 1;
  176. else
  177. {
  178. i = 0;
  179. newargs = 0;
  180. }
  181. for (; i < len; i++)
  182. newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
  183. n = newargs;
  184. for (i = 0; p1;
  185. p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
  186. {
  187. if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
  188. {
  189. TREE_PURPOSE (n) = TREE_PURPOSE (p1);
  190. any_change = 1;
  191. }
  192. else if (! TREE_PURPOSE (p1))
  193. {
  194. if (TREE_PURPOSE (p2))
  195. {
  196. TREE_PURPOSE (n) = TREE_PURPOSE (p2);
  197. any_change = 1;
  198. }
  199. }
  200. else
  201. {
  202. if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
  203. any_change = 1;
  204. TREE_PURPOSE (n) = TREE_PURPOSE (p2);
  205. }
  206. if (TREE_VALUE (p1) != TREE_VALUE (p2))
  207. {
  208. any_change = 1;
  209. TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
  210. }
  211. else
  212. TREE_VALUE (n) = TREE_VALUE (p1);
  213. }
  214. if (! any_change)
  215. return oldargs;
  216. return newargs;
  217. }
  218. /* Given a type, perhaps copied for a typedef,
  219. find the "original" version of it. */
  220. static tree
  221. original_type (tree t)
  222. {
  223. int quals = cp_type_quals (t);
  224. while (t != error_mark_node
  225. && TYPE_NAME (t) != NULL_TREE)
  226. {
  227. tree x = TYPE_NAME (t);
  228. if (TREE_CODE (x) != TYPE_DECL)
  229. break;
  230. x = DECL_ORIGINAL_TYPE (x);
  231. if (x == NULL_TREE)
  232. break;
  233. t = x;
  234. }
  235. return cp_build_qualified_type (t, quals);
  236. }
  237. /* Return the common type for two arithmetic types T1 and T2 under the
  238. usual arithmetic conversions. The default conversions have already
  239. been applied, and enumerated types converted to their compatible
  240. integer types. */
  241. static tree
  242. cp_common_type (tree t1, tree t2)
  243. {
  244. enum tree_code code1 = TREE_CODE (t1);
  245. enum tree_code code2 = TREE_CODE (t2);
  246. tree attributes;
  247. int i;
  248. /* In what follows, we slightly generalize the rules given in [expr] so
  249. as to deal with `long long' and `complex'. First, merge the
  250. attributes. */
  251. attributes = (*targetm.merge_type_attributes) (t1, t2);
  252. if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
  253. {
  254. if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
  255. return build_type_attribute_variant (t1, attributes);
  256. else
  257. return NULL_TREE;
  258. }
  259. /* FIXME: Attributes. */
  260. gcc_assert (ARITHMETIC_TYPE_P (t1)
  261. || TREE_CODE (t1) == VECTOR_TYPE
  262. || UNSCOPED_ENUM_P (t1));
  263. gcc_assert (ARITHMETIC_TYPE_P (t2)
  264. || TREE_CODE (t2) == VECTOR_TYPE
  265. || UNSCOPED_ENUM_P (t2));
  266. /* If one type is complex, form the common type of the non-complex
  267. components, then make that complex. Use T1 or T2 if it is the
  268. required type. */
  269. if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
  270. {
  271. tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
  272. tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
  273. tree subtype
  274. = type_after_usual_arithmetic_conversions (subtype1, subtype2);
  275. if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
  276. return build_type_attribute_variant (t1, attributes);
  277. else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
  278. return build_type_attribute_variant (t2, attributes);
  279. else
  280. return build_type_attribute_variant (build_complex_type (subtype),
  281. attributes);
  282. }
  283. if (code1 == VECTOR_TYPE)
  284. {
  285. /* When we get here we should have two vectors of the same size.
  286. Just prefer the unsigned one if present. */
  287. if (TYPE_UNSIGNED (t1))
  288. return build_type_attribute_variant (t1, attributes);
  289. else
  290. return build_type_attribute_variant (t2, attributes);
  291. }
  292. /* If only one is real, use it as the result. */
  293. if (code1 == REAL_TYPE && code2 != REAL_TYPE)
  294. return build_type_attribute_variant (t1, attributes);
  295. if (code2 == REAL_TYPE && code1 != REAL_TYPE)
  296. return build_type_attribute_variant (t2, attributes);
  297. /* Both real or both integers; use the one with greater precision. */
  298. if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
  299. return build_type_attribute_variant (t1, attributes);
  300. else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
  301. return build_type_attribute_variant (t2, attributes);
  302. /* The types are the same; no need to do anything fancy. */
  303. if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
  304. return build_type_attribute_variant (t1, attributes);
  305. if (code1 != REAL_TYPE)
  306. {
  307. /* If one is unsigned long long, then convert the other to unsigned
  308. long long. */
  309. if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
  310. || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
  311. return build_type_attribute_variant (long_long_unsigned_type_node,
  312. attributes);
  313. /* If one is a long long, and the other is an unsigned long, and
  314. long long can represent all the values of an unsigned long, then
  315. convert to a long long. Otherwise, convert to an unsigned long
  316. long. Otherwise, if either operand is long long, convert the
  317. other to long long.
  318. Since we're here, we know the TYPE_PRECISION is the same;
  319. therefore converting to long long cannot represent all the values
  320. of an unsigned long, so we choose unsigned long long in that
  321. case. */
  322. if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
  323. || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
  324. {
  325. tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
  326. ? long_long_unsigned_type_node
  327. : long_long_integer_type_node);
  328. return build_type_attribute_variant (t, attributes);
  329. }
  330. /* Go through the same procedure, but for longs. */
  331. if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
  332. || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
  333. return build_type_attribute_variant (long_unsigned_type_node,
  334. attributes);
  335. if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
  336. || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
  337. {
  338. tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
  339. ? long_unsigned_type_node : long_integer_type_node);
  340. return build_type_attribute_variant (t, attributes);
  341. }
  342. /* For __intN types, either the type is __int128 (and is lower
  343. priority than the types checked above, but higher than other
  344. 128-bit types) or it's known to not be the same size as other
  345. types (enforced in toplev.c). Prefer the unsigned type. */
  346. for (i = 0; i < NUM_INT_N_ENTS; i ++)
  347. {
  348. if (int_n_enabled_p [i]
  349. && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
  350. || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
  351. || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
  352. || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
  353. {
  354. tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
  355. ? int_n_trees[i].unsigned_type
  356. : int_n_trees[i].signed_type);
  357. return build_type_attribute_variant (t, attributes);
  358. }
  359. }
  360. /* Otherwise prefer the unsigned one. */
  361. if (TYPE_UNSIGNED (t1))
  362. return build_type_attribute_variant (t1, attributes);
  363. else
  364. return build_type_attribute_variant (t2, attributes);
  365. }
  366. else
  367. {
  368. if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
  369. || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
  370. return build_type_attribute_variant (long_double_type_node,
  371. attributes);
  372. if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
  373. || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
  374. return build_type_attribute_variant (double_type_node,
  375. attributes);
  376. if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
  377. || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
  378. return build_type_attribute_variant (float_type_node,
  379. attributes);
  380. /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
  381. the standard C++ floating-point types. Logic earlier in this
  382. function has already eliminated the possibility that
  383. TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
  384. compelling reason to choose one or the other. */
  385. return build_type_attribute_variant (t1, attributes);
  386. }
  387. }
  388. /* T1 and T2 are arithmetic or enumeration types. Return the type
  389. that will result from the "usual arithmetic conversions" on T1 and
  390. T2 as described in [expr]. */
  391. tree
  392. type_after_usual_arithmetic_conversions (tree t1, tree t2)
  393. {
  394. gcc_assert (ARITHMETIC_TYPE_P (t1)
  395. || TREE_CODE (t1) == VECTOR_TYPE
  396. || UNSCOPED_ENUM_P (t1));
  397. gcc_assert (ARITHMETIC_TYPE_P (t2)
  398. || TREE_CODE (t2) == VECTOR_TYPE
  399. || UNSCOPED_ENUM_P (t2));
  400. /* Perform the integral promotions. We do not promote real types here. */
  401. if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
  402. && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
  403. {
  404. t1 = type_promotes_to (t1);
  405. t2 = type_promotes_to (t2);
  406. }
  407. return cp_common_type (t1, t2);
  408. }
  409. static void
  410. composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
  411. composite_pointer_operation operation)
  412. {
  413. switch (operation)
  414. {
  415. case CPO_COMPARISON:
  416. emit_diagnostic (kind, input_location, 0,
  417. "comparison between "
  418. "distinct pointer types %qT and %qT lacks a cast",
  419. t1, t2);
  420. break;
  421. case CPO_CONVERSION:
  422. emit_diagnostic (kind, input_location, 0,
  423. "conversion between "
  424. "distinct pointer types %qT and %qT lacks a cast",
  425. t1, t2);
  426. break;
  427. case CPO_CONDITIONAL_EXPR:
  428. emit_diagnostic (kind, input_location, 0,
  429. "conditional expression between "
  430. "distinct pointer types %qT and %qT lacks a cast",
  431. t1, t2);
  432. break;
  433. default:
  434. gcc_unreachable ();
  435. }
  436. }
  437. /* Subroutine of composite_pointer_type to implement the recursive
  438. case. See that function for documentation of the parameters. */
  439. static tree
  440. composite_pointer_type_r (tree t1, tree t2,
  441. composite_pointer_operation operation,
  442. tsubst_flags_t complain)
  443. {
  444. tree pointee1;
  445. tree pointee2;
  446. tree result_type;
  447. tree attributes;
  448. /* Determine the types pointed to by T1 and T2. */
  449. if (TYPE_PTR_P (t1))
  450. {
  451. pointee1 = TREE_TYPE (t1);
  452. pointee2 = TREE_TYPE (t2);
  453. }
  454. else
  455. {
  456. pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
  457. pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
  458. }
  459. /* [expr.rel]
  460. Otherwise, the composite pointer type is a pointer type
  461. similar (_conv.qual_) to the type of one of the operands,
  462. with a cv-qualification signature (_conv.qual_) that is the
  463. union of the cv-qualification signatures of the operand
  464. types. */
  465. if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
  466. result_type = pointee1;
  467. else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
  468. || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
  469. {
  470. result_type = composite_pointer_type_r (pointee1, pointee2, operation,
  471. complain);
  472. if (result_type == error_mark_node)
  473. return error_mark_node;
  474. }
  475. else
  476. {
  477. if (complain & tf_error)
  478. composite_pointer_error (DK_PERMERROR, t1, t2, operation);
  479. else
  480. return error_mark_node;
  481. result_type = void_type_node;
  482. }
  483. result_type = cp_build_qualified_type (result_type,
  484. (cp_type_quals (pointee1)
  485. | cp_type_quals (pointee2)));
  486. /* If the original types were pointers to members, so is the
  487. result. */
  488. if (TYPE_PTRMEM_P (t1))
  489. {
  490. if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
  491. TYPE_PTRMEM_CLASS_TYPE (t2)))
  492. {
  493. if (complain & tf_error)
  494. composite_pointer_error (DK_PERMERROR, t1, t2, operation);
  495. else
  496. return error_mark_node;
  497. }
  498. result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
  499. result_type);
  500. }
  501. else
  502. result_type = build_pointer_type (result_type);
  503. /* Merge the attributes. */
  504. attributes = (*targetm.merge_type_attributes) (t1, t2);
  505. return build_type_attribute_variant (result_type, attributes);
  506. }
  507. /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
  508. ARG1 and ARG2 are the values with those types. The OPERATION is to
  509. describe the operation between the pointer types,
  510. in case an error occurs.
  511. This routine also implements the computation of a common type for
  512. pointers-to-members as per [expr.eq]. */
  513. tree
  514. composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
  515. composite_pointer_operation operation,
  516. tsubst_flags_t complain)
  517. {
  518. tree class1;
  519. tree class2;
  520. /* [expr.rel]
  521. If one operand is a null pointer constant, the composite pointer
  522. type is the type of the other operand. */
  523. if (null_ptr_cst_p (arg1))
  524. return t2;
  525. if (null_ptr_cst_p (arg2))
  526. return t1;
  527. /* We have:
  528. [expr.rel]
  529. If one of the operands has type "pointer to cv1 void*", then
  530. the other has type "pointer to cv2T", and the composite pointer
  531. type is "pointer to cv12 void", where cv12 is the union of cv1
  532. and cv2.
  533. If either type is a pointer to void, make sure it is T1. */
  534. if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
  535. {
  536. tree t;
  537. t = t1;
  538. t1 = t2;
  539. t2 = t;
  540. }
  541. /* Now, if T1 is a pointer to void, merge the qualifiers. */
  542. if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
  543. {
  544. tree attributes;
  545. tree result_type;
  546. if (TYPE_PTRFN_P (t2))
  547. {
  548. if (complain & tf_error)
  549. {
  550. switch (operation)
  551. {
  552. case CPO_COMPARISON:
  553. pedwarn (input_location, OPT_Wpedantic,
  554. "ISO C++ forbids comparison between pointer "
  555. "of type %<void *%> and pointer-to-function");
  556. break;
  557. case CPO_CONVERSION:
  558. pedwarn (input_location, OPT_Wpedantic,
  559. "ISO C++ forbids conversion between pointer "
  560. "of type %<void *%> and pointer-to-function");
  561. break;
  562. case CPO_CONDITIONAL_EXPR:
  563. pedwarn (input_location, OPT_Wpedantic,
  564. "ISO C++ forbids conditional expression between "
  565. "pointer of type %<void *%> and "
  566. "pointer-to-function");
  567. break;
  568. default:
  569. gcc_unreachable ();
  570. }
  571. }
  572. else
  573. return error_mark_node;
  574. }
  575. result_type
  576. = cp_build_qualified_type (void_type_node,
  577. (cp_type_quals (TREE_TYPE (t1))
  578. | cp_type_quals (TREE_TYPE (t2))));
  579. result_type = build_pointer_type (result_type);
  580. /* Merge the attributes. */
  581. attributes = (*targetm.merge_type_attributes) (t1, t2);
  582. return build_type_attribute_variant (result_type, attributes);
  583. }
  584. if (c_dialect_objc () && TYPE_PTR_P (t1)
  585. && TYPE_PTR_P (t2))
  586. {
  587. if (objc_have_common_type (t1, t2, -3, NULL_TREE))
  588. return objc_common_type (t1, t2);
  589. }
  590. /* [expr.eq] permits the application of a pointer conversion to
  591. bring the pointers to a common type. */
  592. if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
  593. && CLASS_TYPE_P (TREE_TYPE (t1))
  594. && CLASS_TYPE_P (TREE_TYPE (t2))
  595. && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
  596. TREE_TYPE (t2)))
  597. {
  598. class1 = TREE_TYPE (t1);
  599. class2 = TREE_TYPE (t2);
  600. if (DERIVED_FROM_P (class1, class2))
  601. t2 = (build_pointer_type
  602. (cp_build_qualified_type (class1, cp_type_quals (class2))));
  603. else if (DERIVED_FROM_P (class2, class1))
  604. t1 = (build_pointer_type
  605. (cp_build_qualified_type (class2, cp_type_quals (class1))));
  606. else
  607. {
  608. if (complain & tf_error)
  609. composite_pointer_error (DK_ERROR, t1, t2, operation);
  610. return error_mark_node;
  611. }
  612. }
  613. /* [expr.eq] permits the application of a pointer-to-member
  614. conversion to change the class type of one of the types. */
  615. else if (TYPE_PTRMEM_P (t1)
  616. && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
  617. TYPE_PTRMEM_CLASS_TYPE (t2)))
  618. {
  619. class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
  620. class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
  621. if (DERIVED_FROM_P (class1, class2))
  622. t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
  623. else if (DERIVED_FROM_P (class2, class1))
  624. t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
  625. else
  626. {
  627. if (complain & tf_error)
  628. switch (operation)
  629. {
  630. case CPO_COMPARISON:
  631. error ("comparison between distinct "
  632. "pointer-to-member types %qT and %qT lacks a cast",
  633. t1, t2);
  634. break;
  635. case CPO_CONVERSION:
  636. error ("conversion between distinct "
  637. "pointer-to-member types %qT and %qT lacks a cast",
  638. t1, t2);
  639. break;
  640. case CPO_CONDITIONAL_EXPR:
  641. error ("conditional expression between distinct "
  642. "pointer-to-member types %qT and %qT lacks a cast",
  643. t1, t2);
  644. break;
  645. default:
  646. gcc_unreachable ();
  647. }
  648. return error_mark_node;
  649. }
  650. }
  651. return composite_pointer_type_r (t1, t2, operation, complain);
  652. }
  653. /* Return the merged type of two types.
  654. We assume that comptypes has already been done and returned 1;
  655. if that isn't so, this may crash.
  656. This just combines attributes and default arguments; any other
  657. differences would cause the two types to compare unalike. */
  658. tree
  659. merge_types (tree t1, tree t2)
  660. {
  661. enum tree_code code1;
  662. enum tree_code code2;
  663. tree attributes;
  664. /* Save time if the two types are the same. */
  665. if (t1 == t2)
  666. return t1;
  667. if (original_type (t1) == original_type (t2))
  668. return t1;
  669. /* If one type is nonsense, use the other. */
  670. if (t1 == error_mark_node)
  671. return t2;
  672. if (t2 == error_mark_node)
  673. return t1;
  674. /* Handle merging an auto redeclaration with a previous deduced
  675. return type. */
  676. if (is_auto (t1))
  677. return t2;
  678. /* Merge the attributes. */
  679. attributes = (*targetm.merge_type_attributes) (t1, t2);
  680. if (TYPE_PTRMEMFUNC_P (t1))
  681. t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
  682. if (TYPE_PTRMEMFUNC_P (t2))
  683. t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
  684. code1 = TREE_CODE (t1);
  685. code2 = TREE_CODE (t2);
  686. if (code1 != code2)
  687. {
  688. gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
  689. if (code1 == TYPENAME_TYPE)
  690. {
  691. t1 = resolve_typename_type (t1, /*only_current_p=*/true);
  692. code1 = TREE_CODE (t1);
  693. }
  694. else
  695. {
  696. t2 = resolve_typename_type (t2, /*only_current_p=*/true);
  697. code2 = TREE_CODE (t2);
  698. }
  699. }
  700. switch (code1)
  701. {
  702. case POINTER_TYPE:
  703. case REFERENCE_TYPE:
  704. /* For two pointers, do this recursively on the target type. */
  705. {
  706. tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
  707. int quals = cp_type_quals (t1);
  708. if (code1 == POINTER_TYPE)
  709. t1 = build_pointer_type (target);
  710. else
  711. t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
  712. t1 = build_type_attribute_variant (t1, attributes);
  713. t1 = cp_build_qualified_type (t1, quals);
  714. if (TREE_CODE (target) == METHOD_TYPE)
  715. t1 = build_ptrmemfunc_type (t1);
  716. return t1;
  717. }
  718. case OFFSET_TYPE:
  719. {
  720. int quals;
  721. tree pointee;
  722. quals = cp_type_quals (t1);
  723. pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
  724. TYPE_PTRMEM_POINTED_TO_TYPE (t2));
  725. t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
  726. pointee);
  727. t1 = cp_build_qualified_type (t1, quals);
  728. break;
  729. }
  730. case ARRAY_TYPE:
  731. {
  732. tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
  733. /* Save space: see if the result is identical to one of the args. */
  734. if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
  735. return build_type_attribute_variant (t1, attributes);
  736. if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
  737. return build_type_attribute_variant (t2, attributes);
  738. /* Merge the element types, and have a size if either arg has one. */
  739. t1 = build_cplus_array_type
  740. (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
  741. break;
  742. }
  743. case FUNCTION_TYPE:
  744. /* Function types: prefer the one that specified arg types.
  745. If both do, merge the arg types. Also merge the return types. */
  746. {
  747. tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
  748. tree p1 = TYPE_ARG_TYPES (t1);
  749. tree p2 = TYPE_ARG_TYPES (t2);
  750. tree parms;
  751. tree rval, raises;
  752. bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
  753. /* Save space: see if the result is identical to one of the args. */
  754. if (valtype == TREE_TYPE (t1) && ! p2)
  755. return cp_build_type_attribute_variant (t1, attributes);
  756. if (valtype == TREE_TYPE (t2) && ! p1)
  757. return cp_build_type_attribute_variant (t2, attributes);
  758. /* Simple way if one arg fails to specify argument types. */
  759. if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
  760. parms = p2;
  761. else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
  762. parms = p1;
  763. else
  764. parms = commonparms (p1, p2);
  765. rval = build_function_type (valtype, parms);
  766. gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
  767. gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
  768. rval = apply_memfn_quals (rval,
  769. type_memfn_quals (t1),
  770. type_memfn_rqual (t1));
  771. raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
  772. TYPE_RAISES_EXCEPTIONS (t2));
  773. t1 = build_exception_variant (rval, raises);
  774. if (late_return_type_p)
  775. TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
  776. break;
  777. }
  778. case METHOD_TYPE:
  779. {
  780. /* Get this value the long way, since TYPE_METHOD_BASETYPE
  781. is just the main variant of this. */
  782. tree basetype = class_of_this_parm (t2);
  783. tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
  784. TYPE_RAISES_EXCEPTIONS (t2));
  785. cp_ref_qualifier rqual = type_memfn_rqual (t1);
  786. tree t3;
  787. bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
  788. bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
  789. /* If this was a member function type, get back to the
  790. original type of type member function (i.e., without
  791. the class instance variable up front. */
  792. t1 = build_function_type (TREE_TYPE (t1),
  793. TREE_CHAIN (TYPE_ARG_TYPES (t1)));
  794. t2 = build_function_type (TREE_TYPE (t2),
  795. TREE_CHAIN (TYPE_ARG_TYPES (t2)));
  796. t3 = merge_types (t1, t2);
  797. t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
  798. TYPE_ARG_TYPES (t3));
  799. t1 = build_exception_variant (t3, raises);
  800. t1 = build_ref_qualified_type (t1, rqual);
  801. if (late_return_type_1_p)
  802. TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
  803. if (late_return_type_2_p)
  804. TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
  805. break;
  806. }
  807. case TYPENAME_TYPE:
  808. /* There is no need to merge attributes into a TYPENAME_TYPE.
  809. When the type is instantiated it will have whatever
  810. attributes result from the instantiation. */
  811. return t1;
  812. default:;
  813. }
  814. if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
  815. return t1;
  816. else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
  817. return t2;
  818. else
  819. return cp_build_type_attribute_variant (t1, attributes);
  820. }
  821. /* Return the ARRAY_TYPE type without its domain. */
  822. tree
  823. strip_array_domain (tree type)
  824. {
  825. tree t2;
  826. gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
  827. if (TYPE_DOMAIN (type) == NULL_TREE)
  828. return type;
  829. t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
  830. return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
  831. }
  832. /* Wrapper around cp_common_type that is used by c-common.c and other
  833. front end optimizations that remove promotions.
  834. Return the common type for two arithmetic types T1 and T2 under the
  835. usual arithmetic conversions. The default conversions have already
  836. been applied, and enumerated types converted to their compatible
  837. integer types. */
  838. tree
  839. common_type (tree t1, tree t2)
  840. {
  841. /* If one type is nonsense, use the other */
  842. if (t1 == error_mark_node)
  843. return t2;
  844. if (t2 == error_mark_node)
  845. return t1;
  846. return cp_common_type (t1, t2);
  847. }
  848. /* Return the common type of two pointer types T1 and T2. This is the
  849. type for the result of most arithmetic operations if the operands
  850. have the given two types.
  851. We assume that comp_target_types has already been done and returned
  852. nonzero; if that isn't so, this may crash. */
  853. tree
  854. common_pointer_type (tree t1, tree t2)
  855. {
  856. gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
  857. || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
  858. || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
  859. return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
  860. CPO_CONVERSION, tf_warning_or_error);
  861. }
  862. /* Compare two exception specifier types for exactness or subsetness, if
  863. allowed. Returns false for mismatch, true for match (same, or
  864. derived and !exact).
  865. [except.spec] "If a class X ... objects of class X or any class publicly
  866. and unambiguously derived from X. Similarly, if a pointer type Y * ...
  867. exceptions of type Y * or that are pointers to any type publicly and
  868. unambiguously derived from Y. Otherwise a function only allows exceptions
  869. that have the same type ..."
  870. This does not mention cv qualifiers and is different to what throw
  871. [except.throw] and catch [except.catch] will do. They will ignore the
  872. top level cv qualifiers, and allow qualifiers in the pointer to class
  873. example.
  874. We implement the letter of the standard. */
  875. static bool
  876. comp_except_types (tree a, tree b, bool exact)
  877. {
  878. if (same_type_p (a, b))
  879. return true;
  880. else if (!exact)
  881. {
  882. if (cp_type_quals (a) || cp_type_quals (b))
  883. return false;
  884. if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
  885. {
  886. a = TREE_TYPE (a);
  887. b = TREE_TYPE (b);
  888. if (cp_type_quals (a) || cp_type_quals (b))
  889. return false;
  890. }
  891. if (TREE_CODE (a) != RECORD_TYPE
  892. || TREE_CODE (b) != RECORD_TYPE)
  893. return false;
  894. if (publicly_uniquely_derived_p (a, b))
  895. return true;
  896. }
  897. return false;
  898. }
  899. /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
  900. If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
  901. If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
  902. If EXACT is ce_exact, the specs must be exactly the same. Exception lists
  903. are unordered, but we've already filtered out duplicates. Most lists will
  904. be in order, we should try to make use of that. */
  905. bool
  906. comp_except_specs (const_tree t1, const_tree t2, int exact)
  907. {
  908. const_tree probe;
  909. const_tree base;
  910. int length = 0;
  911. if (t1 == t2)
  912. return true;
  913. /* First handle noexcept. */
  914. if (exact < ce_exact)
  915. {
  916. /* noexcept(false) is compatible with no exception-specification,
  917. and stricter than any spec. */
  918. if (t1 == noexcept_false_spec)
  919. return t2 == NULL_TREE || exact == ce_derived;
  920. /* Even a derived noexcept(false) is compatible with no
  921. exception-specification. */
  922. if (t2 == noexcept_false_spec)
  923. return t1 == NULL_TREE;
  924. /* Otherwise, if we aren't looking for an exact match, noexcept is
  925. equivalent to throw(). */
  926. if (t1 == noexcept_true_spec)
  927. t1 = empty_except_spec;
  928. if (t2 == noexcept_true_spec)
  929. t2 = empty_except_spec;
  930. }
  931. /* If any noexcept is left, it is only comparable to itself;
  932. either we're looking for an exact match or we're redeclaring a
  933. template with dependent noexcept. */
  934. if ((t1 && TREE_PURPOSE (t1))
  935. || (t2 && TREE_PURPOSE (t2)))
  936. return (t1 && t2
  937. && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
  938. if (t1 == NULL_TREE) /* T1 is ... */
  939. return t2 == NULL_TREE || exact == ce_derived;
  940. if (!TREE_VALUE (t1)) /* t1 is EMPTY */
  941. return t2 != NULL_TREE && !TREE_VALUE (t2);
  942. if (t2 == NULL_TREE) /* T2 is ... */
  943. return false;
  944. if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
  945. return exact == ce_derived;
  946. /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
  947. Count how many we find, to determine exactness. For exact matching and
  948. ordered T1, T2, this is an O(n) operation, otherwise its worst case is
  949. O(nm). */
  950. for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
  951. {
  952. for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
  953. {
  954. tree a = TREE_VALUE (probe);
  955. tree b = TREE_VALUE (t2);
  956. if (comp_except_types (a, b, exact))
  957. {
  958. if (probe == base && exact > ce_derived)
  959. base = TREE_CHAIN (probe);
  960. length++;
  961. break;
  962. }
  963. }
  964. if (probe == NULL_TREE)
  965. return false;
  966. }
  967. return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
  968. }
  969. /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
  970. [] can match [size]. */
  971. static bool
  972. comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
  973. {
  974. tree d1;
  975. tree d2;
  976. tree max1, max2;
  977. if (t1 == t2)
  978. return true;
  979. /* The type of the array elements must be the same. */
  980. if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  981. return false;
  982. d1 = TYPE_DOMAIN (t1);
  983. d2 = TYPE_DOMAIN (t2);
  984. if (d1 == d2)
  985. return true;
  986. /* If one of the arrays is dimensionless, and the other has a
  987. dimension, they are of different types. However, it is valid to
  988. write:
  989. extern int a[];
  990. int a[3];
  991. by [basic.link]:
  992. declarations for an array object can specify
  993. array types that differ by the presence or absence of a major
  994. array bound (_dcl.array_). */
  995. if (!d1 || !d2)
  996. return allow_redeclaration;
  997. /* Check that the dimensions are the same. */
  998. if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
  999. return false;
  1000. max1 = TYPE_MAX_VALUE (d1);
  1001. max2 = TYPE_MAX_VALUE (d2);
  1002. if (!cp_tree_equal (max1, max2))
  1003. return false;
  1004. return true;
  1005. }
  1006. /* Compare the relative position of T1 and T2 into their respective
  1007. template parameter list.
  1008. T1 and T2 must be template parameter types.
  1009. Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
  1010. static bool
  1011. comp_template_parms_position (tree t1, tree t2)
  1012. {
  1013. tree index1, index2;
  1014. gcc_assert (t1 && t2
  1015. && TREE_CODE (t1) == TREE_CODE (t2)
  1016. && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
  1017. || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
  1018. || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
  1019. index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
  1020. index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
  1021. /* Then compare their relative position. */
  1022. if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
  1023. || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
  1024. || (TEMPLATE_PARM_PARAMETER_PACK (index1)
  1025. != TEMPLATE_PARM_PARAMETER_PACK (index2)))
  1026. return false;
  1027. /* In C++14 we can end up comparing 'auto' to a normal template
  1028. parameter. Don't confuse them. */
  1029. if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
  1030. return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
  1031. return true;
  1032. }
  1033. /* Subroutine in comptypes. */
  1034. static bool
  1035. structural_comptypes (tree t1, tree t2, int strict)
  1036. {
  1037. if (t1 == t2)
  1038. return true;
  1039. /* Suppress errors caused by previously reported errors. */
  1040. if (t1 == error_mark_node || t2 == error_mark_node)
  1041. return false;
  1042. gcc_assert (TYPE_P (t1) && TYPE_P (t2));
  1043. /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
  1044. current instantiation. */
  1045. if (TREE_CODE (t1) == TYPENAME_TYPE)
  1046. t1 = resolve_typename_type (t1, /*only_current_p=*/true);
  1047. if (TREE_CODE (t2) == TYPENAME_TYPE)
  1048. t2 = resolve_typename_type (t2, /*only_current_p=*/true);
  1049. if (TYPE_PTRMEMFUNC_P (t1))
  1050. t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
  1051. if (TYPE_PTRMEMFUNC_P (t2))
  1052. t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
  1053. /* Different classes of types can't be compatible. */
  1054. if (TREE_CODE (t1) != TREE_CODE (t2))
  1055. return false;
  1056. /* Qualifiers must match. For array types, we will check when we
  1057. recur on the array element types. */
  1058. if (TREE_CODE (t1) != ARRAY_TYPE
  1059. && cp_type_quals (t1) != cp_type_quals (t2))
  1060. return false;
  1061. if (TREE_CODE (t1) == FUNCTION_TYPE
  1062. && type_memfn_quals (t1) != type_memfn_quals (t2))
  1063. return false;
  1064. /* Need to check this before TYPE_MAIN_VARIANT.
  1065. FIXME function qualifiers should really change the main variant. */
  1066. if ((TREE_CODE (t1) == FUNCTION_TYPE
  1067. || TREE_CODE (t1) == METHOD_TYPE)
  1068. && type_memfn_rqual (t1) != type_memfn_rqual (t2))
  1069. return false;
  1070. if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
  1071. return false;
  1072. /* Allow for two different type nodes which have essentially the same
  1073. definition. Note that we already checked for equality of the type
  1074. qualifiers (just above). */
  1075. if (TREE_CODE (t1) != ARRAY_TYPE
  1076. && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
  1077. return true;
  1078. /* Compare the types. Break out if they could be the same. */
  1079. switch (TREE_CODE (t1))
  1080. {
  1081. case VOID_TYPE:
  1082. case BOOLEAN_TYPE:
  1083. /* All void and bool types are the same. */
  1084. break;
  1085. case INTEGER_TYPE:
  1086. case FIXED_POINT_TYPE:
  1087. case REAL_TYPE:
  1088. /* With these nodes, we can't determine type equivalence by
  1089. looking at what is stored in the nodes themselves, because
  1090. two nodes might have different TYPE_MAIN_VARIANTs but still
  1091. represent the same type. For example, wchar_t and int could
  1092. have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
  1093. TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
  1094. and are distinct types. On the other hand, int and the
  1095. following typedef
  1096. typedef int INT __attribute((may_alias));
  1097. have identical properties, different TYPE_MAIN_VARIANTs, but
  1098. represent the same type. The canonical type system keeps
  1099. track of equivalence in this case, so we fall back on it. */
  1100. return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
  1101. case TEMPLATE_TEMPLATE_PARM:
  1102. case BOUND_TEMPLATE_TEMPLATE_PARM:
  1103. if (!comp_template_parms_position (t1, t2))
  1104. return false;
  1105. if (!comp_template_parms
  1106. (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
  1107. DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
  1108. return false;
  1109. if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
  1110. break;
  1111. /* Don't check inheritance. */
  1112. strict = COMPARE_STRICT;
  1113. /* Fall through. */
  1114. case RECORD_TYPE:
  1115. case UNION_TYPE:
  1116. if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
  1117. && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
  1118. || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
  1119. && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
  1120. break;
  1121. if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
  1122. break;
  1123. else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
  1124. break;
  1125. return false;
  1126. case OFFSET_TYPE:
  1127. if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
  1128. strict & ~COMPARE_REDECLARATION))
  1129. return false;
  1130. if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  1131. return false;
  1132. break;
  1133. case REFERENCE_TYPE:
  1134. if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
  1135. return false;
  1136. /* fall through to checks for pointer types */
  1137. case POINTER_TYPE:
  1138. if (TYPE_MODE (t1) != TYPE_MODE (t2)
  1139. || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
  1140. || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  1141. return false;
  1142. break;
  1143. case METHOD_TYPE:
  1144. case FUNCTION_TYPE:
  1145. if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  1146. return false;
  1147. if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
  1148. return false;
  1149. break;
  1150. case ARRAY_TYPE:
  1151. /* Target types must match incl. qualifiers. */
  1152. if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
  1153. return false;
  1154. break;
  1155. case TEMPLATE_TYPE_PARM:
  1156. /* If T1 and T2 don't have the same relative position in their
  1157. template parameters set, they can't be equal. */
  1158. if (!comp_template_parms_position (t1, t2))
  1159. return false;
  1160. break;
  1161. case TYPENAME_TYPE:
  1162. if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
  1163. TYPENAME_TYPE_FULLNAME (t2)))
  1164. return false;
  1165. /* Qualifiers don't matter on scopes. */
  1166. if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
  1167. TYPE_CONTEXT (t2)))
  1168. return false;
  1169. break;
  1170. case UNBOUND_CLASS_TEMPLATE:
  1171. if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
  1172. return false;
  1173. if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
  1174. return false;
  1175. break;
  1176. case COMPLEX_TYPE:
  1177. if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  1178. return false;
  1179. break;
  1180. case VECTOR_TYPE:
  1181. if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
  1182. || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
  1183. return false;
  1184. break;
  1185. case TYPE_PACK_EXPANSION:
  1186. return (same_type_p (PACK_EXPANSION_PATTERN (t1),
  1187. PACK_EXPANSION_PATTERN (t2))
  1188. && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
  1189. PACK_EXPANSION_EXTRA_ARGS (t2)));
  1190. case DECLTYPE_TYPE:
  1191. if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
  1192. != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
  1193. || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
  1194. != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
  1195. || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
  1196. != DECLTYPE_FOR_LAMBDA_PROXY (t2))
  1197. || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
  1198. DECLTYPE_TYPE_EXPR (t2)))
  1199. return false;
  1200. break;
  1201. case UNDERLYING_TYPE:
  1202. return same_type_p (UNDERLYING_TYPE_TYPE (t1),
  1203. UNDERLYING_TYPE_TYPE (t2));
  1204. default:
  1205. return false;
  1206. }
  1207. /* If we get here, we know that from a target independent POV the
  1208. types are the same. Make sure the target attributes are also
  1209. the same. */
  1210. return comp_type_attributes (t1, t2);
  1211. }
  1212. /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
  1213. is a bitwise-or of the COMPARE_* flags. */
  1214. bool
  1215. comptypes (tree t1, tree t2, int strict)
  1216. {
  1217. if (strict == COMPARE_STRICT)
  1218. {
  1219. if (t1 == t2)
  1220. return true;
  1221. if (t1 == error_mark_node || t2 == error_mark_node)
  1222. return false;
  1223. if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
  1224. /* At least one of the types requires structural equality, so
  1225. perform a deep check. */
  1226. return structural_comptypes (t1, t2, strict);
  1227. #ifdef ENABLE_CHECKING
  1228. if (USE_CANONICAL_TYPES)
  1229. {
  1230. bool result = structural_comptypes (t1, t2, strict);
  1231. if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
  1232. /* The two types are structurally equivalent, but their
  1233. canonical types were different. This is a failure of the
  1234. canonical type propagation code.*/
  1235. internal_error
  1236. ("canonical types differ for identical types %T and %T",
  1237. t1, t2);
  1238. else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
  1239. /* Two types are structurally different, but the canonical
  1240. types are the same. This means we were over-eager in
  1241. assigning canonical types. */
  1242. internal_error
  1243. ("same canonical type node for different types %T and %T",
  1244. t1, t2);
  1245. return result;
  1246. }
  1247. #else
  1248. if (USE_CANONICAL_TYPES)
  1249. return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
  1250. #endif
  1251. else
  1252. return structural_comptypes (t1, t2, strict);
  1253. }
  1254. else if (strict == COMPARE_STRUCTURAL)
  1255. return structural_comptypes (t1, t2, COMPARE_STRICT);
  1256. else
  1257. return structural_comptypes (t1, t2, strict);
  1258. }
  1259. /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
  1260. top-level qualifiers. */
  1261. bool
  1262. same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
  1263. {
  1264. if (type1 == error_mark_node || type2 == error_mark_node)
  1265. return false;
  1266. return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
  1267. }
  1268. /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
  1269. bool
  1270. at_least_as_qualified_p (const_tree type1, const_tree type2)
  1271. {
  1272. int q1 = cp_type_quals (type1);
  1273. int q2 = cp_type_quals (type2);
  1274. /* All qualifiers for TYPE2 must also appear in TYPE1. */
  1275. return (q1 & q2) == q2;
  1276. }
  1277. /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
  1278. more cv-qualified that TYPE1, and 0 otherwise. */
  1279. int
  1280. comp_cv_qualification (int q1, int q2)
  1281. {
  1282. if (q1 == q2)
  1283. return 0;
  1284. if ((q1 & q2) == q2)
  1285. return 1;
  1286. else if ((q1 & q2) == q1)
  1287. return -1;
  1288. return 0;
  1289. }
  1290. int
  1291. comp_cv_qualification (const_tree type1, const_tree type2)
  1292. {
  1293. int q1 = cp_type_quals (type1);
  1294. int q2 = cp_type_quals (type2);
  1295. return comp_cv_qualification (q1, q2);
  1296. }
  1297. /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
  1298. subset of the cv-qualification signature of TYPE2, and the types
  1299. are similar. Returns -1 if the other way 'round, and 0 otherwise. */
  1300. int
  1301. comp_cv_qual_signature (tree type1, tree type2)
  1302. {
  1303. if (comp_ptr_ttypes_real (type2, type1, -1))
  1304. return 1;
  1305. else if (comp_ptr_ttypes_real (type1, type2, -1))
  1306. return -1;
  1307. else
  1308. return 0;
  1309. }
  1310. /* Subroutines of `comptypes'. */
  1311. /* Return true if two parameter type lists PARMS1 and PARMS2 are
  1312. equivalent in the sense that functions with those parameter types
  1313. can have equivalent types. The two lists must be equivalent,
  1314. element by element. */
  1315. bool
  1316. compparms (const_tree parms1, const_tree parms2)
  1317. {
  1318. const_tree t1, t2;
  1319. /* An unspecified parmlist matches any specified parmlist
  1320. whose argument types don't need default promotions. */
  1321. for (t1 = parms1, t2 = parms2;
  1322. t1 || t2;
  1323. t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
  1324. {
  1325. /* If one parmlist is shorter than the other,
  1326. they fail to match. */
  1327. if (!t1 || !t2)
  1328. return false;
  1329. if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. /* Process a sizeof or alignof expression where the operand is a
  1335. type. */
  1336. tree
  1337. cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
  1338. {
  1339. tree value;
  1340. bool dependent_p;
  1341. gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
  1342. if (type == error_mark_node)
  1343. return error_mark_node;
  1344. type = non_reference (type);
  1345. if (TREE_CODE (type) == METHOD_TYPE)
  1346. {
  1347. if (complain)
  1348. pedwarn (input_location, OPT_Wpointer_arith,
  1349. "invalid application of %qs to a member function",
  1350. operator_name_info[(int) op].name);
  1351. else
  1352. return error_mark_node;
  1353. value = size_one_node;
  1354. }
  1355. dependent_p = dependent_type_p (type);
  1356. if (!dependent_p)
  1357. complete_type (type);
  1358. if (dependent_p
  1359. /* VLA types will have a non-constant size. In the body of an
  1360. uninstantiated template, we don't need to try to compute the
  1361. value, because the sizeof expression is not an integral
  1362. constant expression in that case. And, if we do try to
  1363. compute the value, we'll likely end up with SAVE_EXPRs, which
  1364. the template substitution machinery does not expect to see. */
  1365. || (processing_template_decl
  1366. && COMPLETE_TYPE_P (type)
  1367. && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
  1368. {
  1369. value = build_min (op, size_type_node, type);
  1370. TREE_READONLY (value) = 1;
  1371. return value;
  1372. }
  1373. return c_sizeof_or_alignof_type (input_location, complete_type (type),
  1374. op == SIZEOF_EXPR, false,
  1375. complain);
  1376. }
  1377. /* Return the size of the type, without producing any warnings for
  1378. types whose size cannot be taken. This routine should be used only
  1379. in some other routine that has already produced a diagnostic about
  1380. using the size of such a type. */
  1381. tree
  1382. cxx_sizeof_nowarn (tree type)
  1383. {
  1384. if (TREE_CODE (type) == FUNCTION_TYPE
  1385. || VOID_TYPE_P (type)
  1386. || TREE_CODE (type) == ERROR_MARK)
  1387. return size_one_node;
  1388. else if (!COMPLETE_TYPE_P (type))
  1389. return size_zero_node;
  1390. else
  1391. return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
  1392. }
  1393. /* Process a sizeof expression where the operand is an expression. */
  1394. static tree
  1395. cxx_sizeof_expr (tree e, tsubst_flags_t complain)
  1396. {
  1397. if (e == error_mark_node)
  1398. return error_mark_node;
  1399. if (processing_template_decl)
  1400. {
  1401. e = build_min (SIZEOF_EXPR, size_type_node, e);
  1402. TREE_SIDE_EFFECTS (e) = 0;
  1403. TREE_READONLY (e) = 1;
  1404. return e;
  1405. }
  1406. /* To get the size of a static data member declared as an array of
  1407. unknown bound, we need to instantiate it. */
  1408. if (VAR_P (e)
  1409. && VAR_HAD_UNKNOWN_BOUND (e)
  1410. && DECL_TEMPLATE_INSTANTIATION (e))
  1411. instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
  1412. if (TREE_CODE (e) == PARM_DECL
  1413. && DECL_ARRAY_PARAMETER_P (e)
  1414. && (complain & tf_warning))
  1415. {
  1416. if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
  1417. "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
  1418. inform (DECL_SOURCE_LOCATION (e), "declared here");
  1419. }
  1420. e = mark_type_use (e);
  1421. if (TREE_CODE (e) == COMPONENT_REF
  1422. && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
  1423. && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
  1424. {
  1425. if (complain & tf_error)
  1426. error ("invalid application of %<sizeof%> to a bit-field");
  1427. else
  1428. return error_mark_node;
  1429. e = char_type_node;
  1430. }
  1431. else if (is_overloaded_fn (e))
  1432. {
  1433. if (complain & tf_error)
  1434. permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
  1435. "function type");
  1436. else
  1437. return error_mark_node;
  1438. e = char_type_node;
  1439. }
  1440. else if (type_unknown_p (e))
  1441. {
  1442. if (complain & tf_error)
  1443. cxx_incomplete_type_error (e, TREE_TYPE (e));
  1444. else
  1445. return error_mark_node;
  1446. e = char_type_node;
  1447. }
  1448. else
  1449. e = TREE_TYPE (e);
  1450. return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
  1451. }
  1452. /* Implement the __alignof keyword: Return the minimum required
  1453. alignment of E, measured in bytes. For VAR_DECL's and
  1454. FIELD_DECL's return DECL_ALIGN (which can be set from an
  1455. "aligned" __attribute__ specification). */
  1456. static tree
  1457. cxx_alignof_expr (tree e, tsubst_flags_t complain)
  1458. {
  1459. tree t;
  1460. if (e == error_mark_node)
  1461. return error_mark_node;
  1462. if (processing_template_decl)
  1463. {
  1464. e = build_min (ALIGNOF_EXPR, size_type_node, e);
  1465. TREE_SIDE_EFFECTS (e) = 0;
  1466. TREE_READONLY (e) = 1;
  1467. return e;
  1468. }
  1469. e = mark_type_use (e);
  1470. if (VAR_P (e))
  1471. t = size_int (DECL_ALIGN_UNIT (e));
  1472. else if (TREE_CODE (e) == COMPONENT_REF
  1473. && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
  1474. && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
  1475. {
  1476. if (complain & tf_error)
  1477. error ("invalid application of %<__alignof%> to a bit-field");
  1478. else
  1479. return error_mark_node;
  1480. t = size_one_node;
  1481. }
  1482. else if (TREE_CODE (e) == COMPONENT_REF
  1483. && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
  1484. t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
  1485. else if (is_overloaded_fn (e))
  1486. {
  1487. if (complain & tf_error)
  1488. permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
  1489. "function type");
  1490. else
  1491. return error_mark_node;
  1492. if (TREE_CODE (e) == FUNCTION_DECL)
  1493. t = size_int (DECL_ALIGN_UNIT (e));
  1494. else
  1495. t = size_one_node;
  1496. }
  1497. else if (type_unknown_p (e))
  1498. {
  1499. if (complain & tf_error)
  1500. cxx_incomplete_type_error (e, TREE_TYPE (e));
  1501. else
  1502. return error_mark_node;
  1503. t = size_one_node;
  1504. }
  1505. else
  1506. return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
  1507. complain & tf_error);
  1508. return fold_convert (size_type_node, t);
  1509. }
  1510. /* Process a sizeof or alignof expression E with code OP where the operand
  1511. is an expression. */
  1512. tree
  1513. cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
  1514. {
  1515. if (op == SIZEOF_EXPR)
  1516. return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
  1517. else
  1518. return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
  1519. }
  1520. /* Build a representation of an expression 'alignas(E).' Return the
  1521. folded integer value of E if it is an integral constant expression
  1522. that resolves to a valid alignment. If E depends on a template
  1523. parameter, return a syntactic representation tree of kind
  1524. ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
  1525. expression is ill formed, or NULL_TREE if E is NULL_TREE. */
  1526. tree
  1527. cxx_alignas_expr (tree e)
  1528. {
  1529. if (e == NULL_TREE || e == error_mark_node
  1530. || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
  1531. return e;
  1532. if (TYPE_P (e))
  1533. /* [dcl.align]/3:
  1534. When the alignment-specifier is of the form
  1535. alignas(type-id ), it shall have the same effect as
  1536. alignas(alignof(type-id )). */
  1537. return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
  1538. /* If we reach this point, it means the alignas expression if of
  1539. the form "alignas(assignment-expression)", so we should follow
  1540. what is stated by [dcl.align]/2. */
  1541. if (value_dependent_expression_p (e))
  1542. /* Leave value-dependent expression alone for now. */
  1543. return e;
  1544. e = instantiate_non_dependent_expr (e);
  1545. e = mark_rvalue_use (e);
  1546. /* [dcl.align]/2 says:
  1547. the assignment-expression shall be an integral constant
  1548. expression. */
  1549. return cxx_constant_value (e);
  1550. }
  1551. /* EXPR is being used in a context that is not a function call.
  1552. Enforce:
  1553. [expr.ref]
  1554. The expression can be used only as the left-hand operand of a
  1555. member function call.
  1556. [expr.mptr.operator]
  1557. If the result of .* or ->* is a function, then that result can be
  1558. used only as the operand for the function call operator ().
  1559. by issuing an error message if appropriate. Returns true iff EXPR
  1560. violates these rules. */
  1561. bool
  1562. invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
  1563. {
  1564. if (expr == NULL_TREE)
  1565. return false;
  1566. /* Don't enforce this in MS mode. */
  1567. if (flag_ms_extensions)
  1568. return false;
  1569. if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
  1570. expr = get_first_fn (expr);
  1571. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
  1572. {
  1573. if (complain & tf_error)
  1574. error ("invalid use of non-static member function");
  1575. return true;
  1576. }
  1577. return false;
  1578. }
  1579. /* If EXP is a reference to a bitfield, and the type of EXP does not
  1580. match the declared type of the bitfield, return the declared type
  1581. of the bitfield. Otherwise, return NULL_TREE. */
  1582. tree
  1583. is_bitfield_expr_with_lowered_type (const_tree exp)
  1584. {
  1585. switch (TREE_CODE (exp))
  1586. {
  1587. case COND_EXPR:
  1588. if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
  1589. ? TREE_OPERAND (exp, 1)
  1590. : TREE_OPERAND (exp, 0)))
  1591. return NULL_TREE;
  1592. return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
  1593. case COMPOUND_EXPR:
  1594. return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
  1595. case MODIFY_EXPR:
  1596. case SAVE_EXPR:
  1597. return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
  1598. case COMPONENT_REF:
  1599. {
  1600. tree field;
  1601. field = TREE_OPERAND (exp, 1);
  1602. if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
  1603. return NULL_TREE;
  1604. if (same_type_ignoring_top_level_qualifiers_p
  1605. (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
  1606. return NULL_TREE;
  1607. return DECL_BIT_FIELD_TYPE (field);
  1608. }
  1609. CASE_CONVERT:
  1610. if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
  1611. == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
  1612. return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
  1613. /* Fallthrough. */
  1614. default:
  1615. return NULL_TREE;
  1616. }
  1617. }
  1618. /* Like is_bitfield_with_lowered_type, except that if EXP is not a
  1619. bitfield with a lowered type, the type of EXP is returned, rather
  1620. than NULL_TREE. */
  1621. tree
  1622. unlowered_expr_type (const_tree exp)
  1623. {
  1624. tree type;
  1625. tree etype = TREE_TYPE (exp);
  1626. type = is_bitfield_expr_with_lowered_type (exp);
  1627. if (type)
  1628. type = cp_build_qualified_type (type, cp_type_quals (etype));
  1629. else
  1630. type = etype;
  1631. return type;
  1632. }
  1633. /* Perform the conversions in [expr] that apply when an lvalue appears
  1634. in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
  1635. function-to-pointer conversions. In addition, manifest constants
  1636. are replaced by their values, and bitfield references are converted
  1637. to their declared types. Note that this function does not perform the
  1638. lvalue-to-rvalue conversion for class types. If you need that conversion
  1639. to for class types, then you probably need to use force_rvalue.
  1640. Although the returned value is being used as an rvalue, this
  1641. function does not wrap the returned expression in a
  1642. NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
  1643. that the return value is no longer an lvalue. */
  1644. tree
  1645. decay_conversion (tree exp, tsubst_flags_t complain)
  1646. {
  1647. tree type;
  1648. enum tree_code code;
  1649. location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
  1650. type = TREE_TYPE (exp);
  1651. if (type == error_mark_node)
  1652. return error_mark_node;
  1653. exp = mark_rvalue_use (exp);
  1654. exp = resolve_nondeduced_context (exp);
  1655. if (type_unknown_p (exp))
  1656. {
  1657. if (complain & tf_error)
  1658. cxx_incomplete_type_error (exp, TREE_TYPE (exp));
  1659. return error_mark_node;
  1660. }
  1661. code = TREE_CODE (type);
  1662. /* FIXME remove for delayed folding. */
  1663. exp = scalar_constant_value (exp);
  1664. if (error_operand_p (exp))
  1665. return error_mark_node;
  1666. if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
  1667. return nullptr_node;
  1668. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  1669. Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
  1670. if (code == VOID_TYPE)
  1671. {
  1672. if (complain & tf_error)
  1673. error_at (loc, "void value not ignored as it ought to be");
  1674. return error_mark_node;
  1675. }
  1676. if (invalid_nonstatic_memfn_p (exp, complain))
  1677. return error_mark_node;
  1678. if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
  1679. return cp_build_addr_expr (exp, complain);
  1680. if (code == ARRAY_TYPE)
  1681. {
  1682. tree adr;
  1683. tree ptrtype;
  1684. if (INDIRECT_REF_P (exp))
  1685. return build_nop (build_pointer_type (TREE_TYPE (type)),
  1686. TREE_OPERAND (exp, 0));
  1687. if (TREE_CODE (exp) == COMPOUND_EXPR)
  1688. {
  1689. tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
  1690. if (op1 == error_mark_node)
  1691. return error_mark_node;
  1692. return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
  1693. TREE_OPERAND (exp, 0), op1);
  1694. }
  1695. if (!lvalue_p (exp)
  1696. && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
  1697. {
  1698. if (complain & tf_error)
  1699. error_at (loc, "invalid use of non-lvalue array");
  1700. return error_mark_node;
  1701. }
  1702. /* Don't let an array compound literal decay to a pointer. It can
  1703. still be used to initialize an array or bind to a reference. */
  1704. if (TREE_CODE (exp) == TARGET_EXPR)
  1705. {
  1706. if (complain & tf_error)
  1707. error_at (loc, "taking address of temporary array");
  1708. return error_mark_node;
  1709. }
  1710. ptrtype = build_pointer_type (TREE_TYPE (type));
  1711. if (VAR_P (exp))
  1712. {
  1713. if (!cxx_mark_addressable (exp))
  1714. return error_mark_node;
  1715. adr = build_nop (ptrtype, build_address (exp));
  1716. return adr;
  1717. }
  1718. /* This way is better for a COMPONENT_REF since it can
  1719. simplify the offset for a component. */
  1720. adr = cp_build_addr_expr (exp, complain);
  1721. return cp_convert (ptrtype, adr, complain);
  1722. }
  1723. /* If a bitfield is used in a context where integral promotion
  1724. applies, then the caller is expected to have used
  1725. default_conversion. That function promotes bitfields correctly
  1726. before calling this function. At this point, if we have a
  1727. bitfield referenced, we may assume that is not subject to
  1728. promotion, and that, therefore, the type of the resulting rvalue
  1729. is the declared type of the bitfield. */
  1730. exp = convert_bitfield_to_declared_type (exp);
  1731. /* We do not call rvalue() here because we do not want to wrap EXP
  1732. in a NON_LVALUE_EXPR. */
  1733. /* [basic.lval]
  1734. Non-class rvalues always have cv-unqualified types. */
  1735. type = TREE_TYPE (exp);
  1736. if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
  1737. exp = build_nop (cv_unqualified (type), exp);
  1738. return exp;
  1739. }
  1740. /* Perform preparatory conversions, as part of the "usual arithmetic
  1741. conversions". In particular, as per [expr]:
  1742. Whenever an lvalue expression appears as an operand of an
  1743. operator that expects the rvalue for that operand, the
  1744. lvalue-to-rvalue, array-to-pointer, or function-to-pointer
  1745. standard conversions are applied to convert the expression to an
  1746. rvalue.
  1747. In addition, we perform integral promotions here, as those are
  1748. applied to both operands to a binary operator before determining
  1749. what additional conversions should apply. */
  1750. static tree
  1751. cp_default_conversion (tree exp, tsubst_flags_t complain)
  1752. {
  1753. /* Check for target-specific promotions. */
  1754. tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
  1755. if (promoted_type)
  1756. exp = cp_convert (promoted_type, exp, complain);
  1757. /* Perform the integral promotions first so that bitfield
  1758. expressions (which may promote to "int", even if the bitfield is
  1759. declared "unsigned") are promoted correctly. */
  1760. else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
  1761. exp = cp_perform_integral_promotions (exp, complain);
  1762. /* Perform the other conversions. */
  1763. exp = decay_conversion (exp, complain);
  1764. return exp;
  1765. }
  1766. /* C version. */
  1767. tree
  1768. default_conversion (tree exp)
  1769. {
  1770. return cp_default_conversion (exp, tf_warning_or_error);
  1771. }
  1772. /* EXPR is an expression with an integral or enumeration type.
  1773. Perform the integral promotions in [conv.prom], and return the
  1774. converted value. */
  1775. tree
  1776. cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
  1777. {
  1778. tree type;
  1779. tree promoted_type;
  1780. expr = mark_rvalue_use (expr);
  1781. /* [conv.prom]
  1782. If the bitfield has an enumerated type, it is treated as any
  1783. other value of that type for promotion purposes. */
  1784. type = is_bitfield_expr_with_lowered_type (expr);
  1785. if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
  1786. type = TREE_TYPE (expr);
  1787. gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
  1788. /* Scoped enums don't promote. */
  1789. if (SCOPED_ENUM_P (type))
  1790. return expr;
  1791. promoted_type = type_promotes_to (type);
  1792. if (type != promoted_type)
  1793. expr = cp_convert (promoted_type, expr, complain);
  1794. return expr;
  1795. }
  1796. /* C version. */
  1797. tree
  1798. perform_integral_promotions (tree expr)
  1799. {
  1800. return cp_perform_integral_promotions (expr, tf_warning_or_error);
  1801. }
  1802. /* Returns nonzero iff exp is a STRING_CST or the result of applying
  1803. decay_conversion to one. */
  1804. int
  1805. string_conv_p (const_tree totype, const_tree exp, int warn)
  1806. {
  1807. tree t;
  1808. if (!TYPE_PTR_P (totype))
  1809. return 0;
  1810. t = TREE_TYPE (totype);
  1811. if (!same_type_p (t, char_type_node)
  1812. && !same_type_p (t, char16_type_node)
  1813. && !same_type_p (t, char32_type_node)
  1814. && !same_type_p (t, wchar_type_node))
  1815. return 0;
  1816. if (TREE_CODE (exp) == STRING_CST)
  1817. {
  1818. /* Make sure that we don't try to convert between char and wide chars. */
  1819. if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
  1820. return 0;
  1821. }
  1822. else
  1823. {
  1824. /* Is this a string constant which has decayed to 'const char *'? */
  1825. t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
  1826. if (!same_type_p (TREE_TYPE (exp), t))
  1827. return 0;
  1828. STRIP_NOPS (exp);
  1829. if (TREE_CODE (exp) != ADDR_EXPR
  1830. || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
  1831. return 0;
  1832. }
  1833. if (warn)
  1834. {
  1835. if (cxx_dialect >= cxx11)
  1836. pedwarn (input_location,
  1837. pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
  1838. "ISO C++ forbids converting a string constant to %qT",
  1839. totype);
  1840. else
  1841. warning (OPT_Wwrite_strings,
  1842. "deprecated conversion from string constant to %qT",
  1843. totype);
  1844. }
  1845. return 1;
  1846. }
  1847. /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
  1848. can, for example, use as an lvalue. This code used to be in
  1849. unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
  1850. expressions, where we're dealing with aggregates. But now it's again only
  1851. called from unary_complex_lvalue. The case (in particular) that led to
  1852. this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
  1853. get it there. */
  1854. static tree
  1855. rationalize_conditional_expr (enum tree_code code, tree t,
  1856. tsubst_flags_t complain)
  1857. {
  1858. location_t loc = EXPR_LOC_OR_LOC (t, input_location);
  1859. /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
  1860. the first operand is always the one to be used if both operands
  1861. are equal, so we know what conditional expression this used to be. */
  1862. if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
  1863. {
  1864. tree op0 = TREE_OPERAND (t, 0);
  1865. tree op1 = TREE_OPERAND (t, 1);
  1866. /* The following code is incorrect if either operand side-effects. */
  1867. gcc_assert (!TREE_SIDE_EFFECTS (op0)
  1868. && !TREE_SIDE_EFFECTS (op1));
  1869. return
  1870. build_conditional_expr (loc,
  1871. build_x_binary_op (loc,
  1872. (TREE_CODE (t) == MIN_EXPR
  1873. ? LE_EXPR : GE_EXPR),
  1874. op0, TREE_CODE (op0),
  1875. op1, TREE_CODE (op1),
  1876. /*overload=*/NULL,
  1877. complain),
  1878. cp_build_unary_op (code, op0, 0, complain),
  1879. cp_build_unary_op (code, op1, 0, complain),
  1880. complain);
  1881. }
  1882. return
  1883. build_conditional_expr (loc, TREE_OPERAND (t, 0),
  1884. cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
  1885. complain),
  1886. cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
  1887. complain),
  1888. complain);
  1889. }
  1890. /* Given the TYPE of an anonymous union field inside T, return the
  1891. FIELD_DECL for the field. If not found return NULL_TREE. Because
  1892. anonymous unions can nest, we must also search all anonymous unions
  1893. that are directly reachable. */
  1894. tree
  1895. lookup_anon_field (tree t, tree type)
  1896. {
  1897. tree field;
  1898. t = TYPE_MAIN_VARIANT (t);
  1899. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  1900. {
  1901. if (TREE_STATIC (field))
  1902. continue;
  1903. if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
  1904. continue;
  1905. /* If we find it directly, return the field. */
  1906. if (DECL_NAME (field) == NULL_TREE
  1907. && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
  1908. {
  1909. return field;
  1910. }
  1911. /* Otherwise, it could be nested, search harder. */
  1912. if (DECL_NAME (field) == NULL_TREE
  1913. && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
  1914. {
  1915. tree subfield = lookup_anon_field (TREE_TYPE (field), type);
  1916. if (subfield)
  1917. return subfield;
  1918. }
  1919. }
  1920. return NULL_TREE;
  1921. }
  1922. /* Build an expression representing OBJECT.MEMBER. OBJECT is an
  1923. expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
  1924. non-NULL, it indicates the path to the base used to name MEMBER.
  1925. If PRESERVE_REFERENCE is true, the expression returned will have
  1926. REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
  1927. returned will have the type referred to by the reference.
  1928. This function does not perform access control; that is either done
  1929. earlier by the parser when the name of MEMBER is resolved to MEMBER
  1930. itself, or later when overload resolution selects one of the
  1931. functions indicated by MEMBER. */
  1932. tree
  1933. build_class_member_access_expr (tree object, tree member,
  1934. tree access_path, bool preserve_reference,
  1935. tsubst_flags_t complain)
  1936. {
  1937. tree object_type;
  1938. tree member_scope;
  1939. tree result = NULL_TREE;
  1940. tree using_decl = NULL_TREE;
  1941. if (error_operand_p (object) || error_operand_p (member))
  1942. return error_mark_node;
  1943. gcc_assert (DECL_P (member) || BASELINK_P (member));
  1944. /* [expr.ref]
  1945. The type of the first expression shall be "class object" (of a
  1946. complete type). */
  1947. object_type = TREE_TYPE (object);
  1948. if (!currently_open_class (object_type)
  1949. && !complete_type_or_maybe_complain (object_type, object, complain))
  1950. return error_mark_node;
  1951. if (!CLASS_TYPE_P (object_type))
  1952. {
  1953. if (complain & tf_error)
  1954. {
  1955. if (POINTER_TYPE_P (object_type)
  1956. && CLASS_TYPE_P (TREE_TYPE (object_type)))
  1957. error ("request for member %qD in %qE, which is of pointer "
  1958. "type %qT (maybe you meant to use %<->%> ?)",
  1959. member, object, object_type);
  1960. else
  1961. error ("request for member %qD in %qE, which is of non-class "
  1962. "type %qT", member, object, object_type);
  1963. }
  1964. return error_mark_node;
  1965. }
  1966. /* The standard does not seem to actually say that MEMBER must be a
  1967. member of OBJECT_TYPE. However, that is clearly what is
  1968. intended. */
  1969. if (DECL_P (member))
  1970. {
  1971. member_scope = DECL_CLASS_CONTEXT (member);
  1972. mark_used (member);
  1973. if (TREE_DEPRECATED (member))
  1974. warn_deprecated_use (member, NULL_TREE);
  1975. }
  1976. else
  1977. member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
  1978. /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
  1979. presently be the anonymous union. Go outwards until we find a
  1980. type related to OBJECT_TYPE. */
  1981. while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
  1982. && !same_type_ignoring_top_level_qualifiers_p (member_scope,
  1983. object_type))
  1984. member_scope = TYPE_CONTEXT (member_scope);
  1985. if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
  1986. {
  1987. if (complain & tf_error)
  1988. {
  1989. if (TREE_CODE (member) == FIELD_DECL)
  1990. error ("invalid use of nonstatic data member %qE", member);
  1991. else
  1992. error ("%qD is not a member of %qT", member, object_type);
  1993. }
  1994. return error_mark_node;
  1995. }
  1996. /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
  1997. `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
  1998. in the front end; only _DECLs and _REFs are lvalues in the back end. */
  1999. {
  2000. tree temp = unary_complex_lvalue (ADDR_EXPR, object);
  2001. if (temp)
  2002. object = cp_build_indirect_ref (temp, RO_NULL, complain);
  2003. }
  2004. /* In [expr.ref], there is an explicit list of the valid choices for
  2005. MEMBER. We check for each of those cases here. */
  2006. if (VAR_P (member))
  2007. {
  2008. /* A static data member. */
  2009. result = member;
  2010. mark_exp_read (object);
  2011. /* If OBJECT has side-effects, they are supposed to occur. */
  2012. if (TREE_SIDE_EFFECTS (object))
  2013. result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
  2014. }
  2015. else if (TREE_CODE (member) == FIELD_DECL)
  2016. {
  2017. /* A non-static data member. */
  2018. bool null_object_p;
  2019. int type_quals;
  2020. tree member_type;
  2021. null_object_p = (INDIRECT_REF_P (object)
  2022. && integer_zerop (TREE_OPERAND (object, 0)));
  2023. /* Convert OBJECT to the type of MEMBER. */
  2024. if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
  2025. TYPE_MAIN_VARIANT (member_scope)))
  2026. {
  2027. tree binfo;
  2028. base_kind kind;
  2029. binfo = lookup_base (access_path ? access_path : object_type,
  2030. member_scope, ba_unique, &kind, complain);
  2031. if (binfo == error_mark_node)
  2032. return error_mark_node;
  2033. /* It is invalid to try to get to a virtual base of a
  2034. NULL object. The most common cause is invalid use of
  2035. offsetof macro. */
  2036. if (null_object_p && kind == bk_via_virtual)
  2037. {
  2038. if (complain & tf_error)
  2039. {
  2040. error ("invalid access to non-static data member %qD in "
  2041. "virtual base of NULL object", member);
  2042. }
  2043. return error_mark_node;
  2044. }
  2045. /* Convert to the base. */
  2046. object = build_base_path (PLUS_EXPR, object, binfo,
  2047. /*nonnull=*/1, complain);
  2048. /* If we found the base successfully then we should be able
  2049. to convert to it successfully. */
  2050. gcc_assert (object != error_mark_node);
  2051. }
  2052. /* If MEMBER is from an anonymous aggregate, we have converted
  2053. OBJECT so that it refers to the class containing the
  2054. anonymous union. Generate a reference to the anonymous union
  2055. itself, and recur to find MEMBER. */
  2056. if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
  2057. /* When this code is called from build_field_call, the
  2058. object already has the type of the anonymous union.
  2059. That is because the COMPONENT_REF was already
  2060. constructed, and was then disassembled before calling
  2061. build_field_call. After the function-call code is
  2062. cleaned up, this waste can be eliminated. */
  2063. && (!same_type_ignoring_top_level_qualifiers_p
  2064. (TREE_TYPE (object), DECL_CONTEXT (member))))
  2065. {
  2066. tree anonymous_union;
  2067. anonymous_union = lookup_anon_field (TREE_TYPE (object),
  2068. DECL_CONTEXT (member));
  2069. object = build_class_member_access_expr (object,
  2070. anonymous_union,
  2071. /*access_path=*/NULL_TREE,
  2072. preserve_reference,
  2073. complain);
  2074. }
  2075. /* Compute the type of the field, as described in [expr.ref]. */
  2076. type_quals = TYPE_UNQUALIFIED;
  2077. member_type = TREE_TYPE (member);
  2078. if (TREE_CODE (member_type) != REFERENCE_TYPE)
  2079. {
  2080. type_quals = (cp_type_quals (member_type)
  2081. | cp_type_quals (object_type));
  2082. /* A field is const (volatile) if the enclosing object, or the
  2083. field itself, is const (volatile). But, a mutable field is
  2084. not const, even within a const object. */
  2085. if (DECL_MUTABLE_P (member))
  2086. type_quals &= ~TYPE_QUAL_CONST;
  2087. member_type = cp_build_qualified_type (member_type, type_quals);
  2088. }
  2089. result = build3_loc (input_location, COMPONENT_REF, member_type,
  2090. object, member, NULL_TREE);
  2091. result = fold_if_not_in_template (result);
  2092. /* Mark the expression const or volatile, as appropriate. Even
  2093. though we've dealt with the type above, we still have to mark the
  2094. expression itself. */
  2095. if (type_quals & TYPE_QUAL_CONST)
  2096. TREE_READONLY (result) = 1;
  2097. if (type_quals & TYPE_QUAL_VOLATILE)
  2098. TREE_THIS_VOLATILE (result) = 1;
  2099. }
  2100. else if (BASELINK_P (member))
  2101. {
  2102. /* The member is a (possibly overloaded) member function. */
  2103. tree functions;
  2104. tree type;
  2105. /* If the MEMBER is exactly one static member function, then we
  2106. know the type of the expression. Otherwise, we must wait
  2107. until overload resolution has been performed. */
  2108. functions = BASELINK_FUNCTIONS (member);
  2109. if (TREE_CODE (functions) == FUNCTION_DECL
  2110. && DECL_STATIC_FUNCTION_P (functions))
  2111. type = TREE_TYPE (functions);
  2112. else
  2113. type = unknown_type_node;
  2114. /* Note that we do not convert OBJECT to the BASELINK_BINFO
  2115. base. That will happen when the function is called. */
  2116. result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
  2117. }
  2118. else if (TREE_CODE (member) == CONST_DECL)
  2119. {
  2120. /* The member is an enumerator. */
  2121. result = member;
  2122. /* If OBJECT has side-effects, they are supposed to occur. */
  2123. if (TREE_SIDE_EFFECTS (object))
  2124. result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
  2125. object, result);
  2126. }
  2127. else if ((using_decl = strip_using_decl (member)) != member)
  2128. result = build_class_member_access_expr (object,
  2129. using_decl,
  2130. access_path, preserve_reference,
  2131. complain);
  2132. else
  2133. {
  2134. if (complain & tf_error)
  2135. error ("invalid use of %qD", member);
  2136. return error_mark_node;
  2137. }
  2138. if (!preserve_reference)
  2139. /* [expr.ref]
  2140. If E2 is declared to have type "reference to T", then ... the
  2141. type of E1.E2 is T. */
  2142. result = convert_from_reference (result);
  2143. return result;
  2144. }
  2145. /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
  2146. SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
  2147. static tree
  2148. lookup_destructor (tree object, tree scope, tree dtor_name,
  2149. tsubst_flags_t complain)
  2150. {
  2151. tree object_type = TREE_TYPE (object);
  2152. tree dtor_type = TREE_OPERAND (dtor_name, 0);
  2153. tree expr;
  2154. /* We've already complained about this destructor. */
  2155. if (dtor_type == error_mark_node)
  2156. return error_mark_node;
  2157. if (scope && !check_dtor_name (scope, dtor_type))
  2158. {
  2159. if (complain & tf_error)
  2160. error ("qualified type %qT does not match destructor name ~%qT",
  2161. scope, dtor_type);
  2162. return error_mark_node;
  2163. }
  2164. if (is_auto (dtor_type))
  2165. dtor_type = object_type;
  2166. else if (identifier_p (dtor_type))
  2167. {
  2168. /* In a template, names we can't find a match for are still accepted
  2169. destructor names, and we check them here. */
  2170. if (check_dtor_name (object_type, dtor_type))
  2171. dtor_type = object_type;
  2172. else
  2173. {
  2174. if (complain & tf_error)
  2175. error ("object type %qT does not match destructor name ~%qT",
  2176. object_type, dtor_type);
  2177. return error_mark_node;
  2178. }
  2179. }
  2180. else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
  2181. {
  2182. if (complain & tf_error)
  2183. error ("the type being destroyed is %qT, but the destructor "
  2184. "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
  2185. return error_mark_node;
  2186. }
  2187. expr = lookup_member (dtor_type, complete_dtor_identifier,
  2188. /*protect=*/1, /*want_type=*/false,
  2189. tf_warning_or_error);
  2190. if (!expr)
  2191. {
  2192. if (complain & tf_error)
  2193. cxx_incomplete_type_error (dtor_name, dtor_type);
  2194. return error_mark_node;
  2195. }
  2196. expr = (adjust_result_of_qualified_name_lookup
  2197. (expr, dtor_type, object_type));
  2198. if (scope == NULL_TREE)
  2199. /* We need to call adjust_result_of_qualified_name_lookup in case the
  2200. destructor names a base class, but we unset BASELINK_QUALIFIED_P so
  2201. that we still get virtual function binding. */
  2202. BASELINK_QUALIFIED_P (expr) = false;
  2203. return expr;
  2204. }
  2205. /* An expression of the form "A::template B" has been resolved to
  2206. DECL. Issue a diagnostic if B is not a template or template
  2207. specialization. */
  2208. void
  2209. check_template_keyword (tree decl)
  2210. {
  2211. /* The standard says:
  2212. [temp.names]
  2213. If a name prefixed by the keyword template is not a member
  2214. template, the program is ill-formed.
  2215. DR 228 removed the restriction that the template be a member
  2216. template.
  2217. DR 96, if accepted would add the further restriction that explicit
  2218. template arguments must be provided if the template keyword is
  2219. used, but, as of 2005-10-16, that DR is still in "drafting". If
  2220. this DR is accepted, then the semantic checks here can be
  2221. simplified, as the entity named must in fact be a template
  2222. specialization, rather than, as at present, a set of overloaded
  2223. functions containing at least one template function. */
  2224. if (TREE_CODE (decl) != TEMPLATE_DECL
  2225. && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
  2226. {
  2227. if (!is_overloaded_fn (decl))
  2228. permerror (input_location, "%qD is not a template", decl);
  2229. else
  2230. {
  2231. tree fns;
  2232. fns = decl;
  2233. if (BASELINK_P (fns))
  2234. fns = BASELINK_FUNCTIONS (fns);
  2235. while (fns)
  2236. {
  2237. tree fn = OVL_CURRENT (fns);
  2238. if (TREE_CODE (fn) == TEMPLATE_DECL
  2239. || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
  2240. break;
  2241. if (TREE_CODE (fn) == FUNCTION_DECL
  2242. && DECL_USE_TEMPLATE (fn)
  2243. && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
  2244. break;
  2245. fns = OVL_NEXT (fns);
  2246. }
  2247. if (!fns)
  2248. permerror (input_location, "%qD is not a template", decl);
  2249. }
  2250. }
  2251. }
  2252. /* This function is called by the parser to process a class member
  2253. access expression of the form OBJECT.NAME. NAME is a node used by
  2254. the parser to represent a name; it is not yet a DECL. It may,
  2255. however, be a BASELINK where the BASELINK_FUNCTIONS is a
  2256. TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
  2257. there is no reason to do the lookup twice, so the parser keeps the
  2258. BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
  2259. be a template via the use of the "A::template B" syntax. */
  2260. tree
  2261. finish_class_member_access_expr (tree object, tree name, bool template_p,
  2262. tsubst_flags_t complain)
  2263. {
  2264. tree expr;
  2265. tree object_type;
  2266. tree member;
  2267. tree access_path = NULL_TREE;
  2268. tree orig_object = object;
  2269. tree orig_name = name;
  2270. if (object == error_mark_node || name == error_mark_node)
  2271. return error_mark_node;
  2272. /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
  2273. if (!objc_is_public (object, name))
  2274. return error_mark_node;
  2275. object_type = TREE_TYPE (object);
  2276. if (processing_template_decl)
  2277. {
  2278. if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
  2279. dependent_type_p (object_type)
  2280. /* If NAME is just an IDENTIFIER_NODE, then the expression
  2281. is dependent. */
  2282. || identifier_p (object)
  2283. /* If NAME is "f<args>", where either 'f' or 'args' is
  2284. dependent, then the expression is dependent. */
  2285. || (TREE_CODE (name) == TEMPLATE_ID_EXPR
  2286. && dependent_template_id_p (TREE_OPERAND (name, 0),
  2287. TREE_OPERAND (name, 1)))
  2288. /* If NAME is "T::X" where "T" is dependent, then the
  2289. expression is dependent. */
  2290. || (TREE_CODE (name) == SCOPE_REF
  2291. && TYPE_P (TREE_OPERAND (name, 0))
  2292. && dependent_type_p (TREE_OPERAND (name, 0))))
  2293. return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
  2294. object, name, NULL_TREE);
  2295. object = build_non_dependent_expr (object);
  2296. }
  2297. else if (c_dialect_objc ()
  2298. && identifier_p (name)
  2299. && (expr = objc_maybe_build_component_ref (object, name)))
  2300. return expr;
  2301. /* [expr.ref]
  2302. The type of the first expression shall be "class object" (of a
  2303. complete type). */
  2304. if (!currently_open_class (object_type)
  2305. && !complete_type_or_maybe_complain (object_type, object, complain))
  2306. return error_mark_node;
  2307. if (!CLASS_TYPE_P (object_type))
  2308. {
  2309. if (complain & tf_error)
  2310. {
  2311. if (POINTER_TYPE_P (object_type)
  2312. && CLASS_TYPE_P (TREE_TYPE (object_type)))
  2313. error ("request for member %qD in %qE, which is of pointer "
  2314. "type %qT (maybe you meant to use %<->%> ?)",
  2315. name, object, object_type);
  2316. else
  2317. error ("request for member %qD in %qE, which is of non-class "
  2318. "type %qT", name, object, object_type);
  2319. }
  2320. return error_mark_node;
  2321. }
  2322. if (BASELINK_P (name))
  2323. /* A member function that has already been looked up. */
  2324. member = name;
  2325. else
  2326. {
  2327. bool is_template_id = false;
  2328. tree template_args = NULL_TREE;
  2329. tree scope;
  2330. if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  2331. {
  2332. is_template_id = true;
  2333. template_args = TREE_OPERAND (name, 1);
  2334. name = TREE_OPERAND (name, 0);
  2335. if (TREE_CODE (name) == OVERLOAD)
  2336. name = DECL_NAME (get_first_fn (name));
  2337. else if (DECL_P (name))
  2338. name = DECL_NAME (name);
  2339. }
  2340. if (TREE_CODE (name) == SCOPE_REF)
  2341. {
  2342. /* A qualified name. The qualifying class or namespace `S'
  2343. has already been looked up; it is either a TYPE or a
  2344. NAMESPACE_DECL. */
  2345. scope = TREE_OPERAND (name, 0);
  2346. name = TREE_OPERAND (name, 1);
  2347. /* If SCOPE is a namespace, then the qualified name does not
  2348. name a member of OBJECT_TYPE. */
  2349. if (TREE_CODE (scope) == NAMESPACE_DECL)
  2350. {
  2351. if (complain & tf_error)
  2352. error ("%<%D::%D%> is not a member of %qT",
  2353. scope, name, object_type);
  2354. return error_mark_node;
  2355. }
  2356. if (TREE_CODE (scope) == ENUMERAL_TYPE)
  2357. {
  2358. /* Looking up a member enumerator (c++/56793). */
  2359. if (!TYPE_CLASS_SCOPE_P (scope)
  2360. || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
  2361. {
  2362. if (complain & tf_error)
  2363. error ("%<%D::%D%> is not a member of %qT",
  2364. scope, name, object_type);
  2365. return error_mark_node;
  2366. }
  2367. tree val = lookup_enumerator (scope, name);
  2368. if (TREE_SIDE_EFFECTS (object))
  2369. val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
  2370. return val;
  2371. }
  2372. gcc_assert (CLASS_TYPE_P (scope));
  2373. gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
  2374. if (constructor_name_p (name, scope))
  2375. {
  2376. if (complain & tf_error)
  2377. error ("cannot call constructor %<%T::%D%> directly",
  2378. scope, name);
  2379. return error_mark_node;
  2380. }
  2381. /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
  2382. access_path = lookup_base (object_type, scope, ba_check,
  2383. NULL, complain);
  2384. if (access_path == error_mark_node)
  2385. return error_mark_node;
  2386. if (!access_path)
  2387. {
  2388. if (complain & tf_error)
  2389. error ("%qT is not a base of %qT", scope, object_type);
  2390. return error_mark_node;
  2391. }
  2392. }
  2393. else
  2394. {
  2395. scope = NULL_TREE;
  2396. access_path = object_type;
  2397. }
  2398. if (TREE_CODE (name) == BIT_NOT_EXPR)
  2399. member = lookup_destructor (object, scope, name, complain);
  2400. else
  2401. {
  2402. /* Look up the member. */
  2403. member = lookup_member (access_path, name, /*protect=*/1,
  2404. /*want_type=*/false, complain);
  2405. if (member == NULL_TREE)
  2406. {
  2407. if (complain & tf_error)
  2408. error ("%q#T has no member named %qE",
  2409. TREE_CODE (access_path) == TREE_BINFO
  2410. ? TREE_TYPE (access_path) : object_type, name);
  2411. return error_mark_node;
  2412. }
  2413. if (member == error_mark_node)
  2414. return error_mark_node;
  2415. }
  2416. if (is_template_id)
  2417. {
  2418. tree templ = member;
  2419. if (BASELINK_P (templ))
  2420. templ = lookup_template_function (templ, template_args);
  2421. else
  2422. {
  2423. if (complain & tf_error)
  2424. error ("%qD is not a member template function", name);
  2425. return error_mark_node;
  2426. }
  2427. }
  2428. }
  2429. if (TREE_DEPRECATED (member))
  2430. warn_deprecated_use (member, NULL_TREE);
  2431. if (template_p)
  2432. check_template_keyword (member);
  2433. expr = build_class_member_access_expr (object, member, access_path,
  2434. /*preserve_reference=*/false,
  2435. complain);
  2436. if (processing_template_decl && expr != error_mark_node)
  2437. {
  2438. if (BASELINK_P (member))
  2439. {
  2440. if (TREE_CODE (orig_name) == SCOPE_REF)
  2441. BASELINK_QUALIFIED_P (member) = 1;
  2442. orig_name = member;
  2443. }
  2444. return build_min_non_dep (COMPONENT_REF, expr,
  2445. orig_object, orig_name,
  2446. NULL_TREE);
  2447. }
  2448. return expr;
  2449. }
  2450. /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
  2451. type. */
  2452. tree
  2453. build_simple_component_ref (tree object, tree member)
  2454. {
  2455. tree type = cp_build_qualified_type (TREE_TYPE (member),
  2456. cp_type_quals (TREE_TYPE (object)));
  2457. return fold_build3_loc (input_location,
  2458. COMPONENT_REF, type,
  2459. object, member, NULL_TREE);
  2460. }
  2461. /* Return an expression for the MEMBER_NAME field in the internal
  2462. representation of PTRMEM, a pointer-to-member function. (Each
  2463. pointer-to-member function type gets its own RECORD_TYPE so it is
  2464. more convenient to access the fields by name than by FIELD_DECL.)
  2465. This routine converts the NAME to a FIELD_DECL and then creates the
  2466. node for the complete expression. */
  2467. tree
  2468. build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
  2469. {
  2470. tree ptrmem_type;
  2471. tree member;
  2472. /* This code is a stripped down version of
  2473. build_class_member_access_expr. It does not work to use that
  2474. routine directly because it expects the object to be of class
  2475. type. */
  2476. ptrmem_type = TREE_TYPE (ptrmem);
  2477. gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
  2478. for (member = TYPE_FIELDS (ptrmem_type); member;
  2479. member = DECL_CHAIN (member))
  2480. if (DECL_NAME (member) == member_name)
  2481. break;
  2482. return build_simple_component_ref (ptrmem, member);
  2483. }
  2484. /* Given an expression PTR for a pointer, return an expression
  2485. for the value pointed to.
  2486. ERRORSTRING is the name of the operator to appear in error messages.
  2487. This function may need to overload OPERATOR_FNNAME.
  2488. Must also handle REFERENCE_TYPEs for C++. */
  2489. tree
  2490. build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
  2491. tsubst_flags_t complain)
  2492. {
  2493. tree orig_expr = expr;
  2494. tree rval;
  2495. if (processing_template_decl)
  2496. {
  2497. /* Retain the type if we know the operand is a pointer. */
  2498. if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
  2499. return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
  2500. if (type_dependent_expression_p (expr))
  2501. return build_min_nt_loc (loc, INDIRECT_REF, expr);
  2502. expr = build_non_dependent_expr (expr);
  2503. }
  2504. rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
  2505. NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
  2506. if (!rval)
  2507. rval = cp_build_indirect_ref (expr, errorstring, complain);
  2508. if (processing_template_decl && rval != error_mark_node)
  2509. return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
  2510. else
  2511. return rval;
  2512. }
  2513. /* Helper function called from c-common. */
  2514. tree
  2515. build_indirect_ref (location_t /*loc*/,
  2516. tree ptr, ref_operator errorstring)
  2517. {
  2518. return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
  2519. }
  2520. tree
  2521. cp_build_indirect_ref (tree ptr, ref_operator errorstring,
  2522. tsubst_flags_t complain)
  2523. {
  2524. tree pointer, type;
  2525. if (ptr == current_class_ptr
  2526. || (TREE_CODE (ptr) == NOP_EXPR
  2527. && TREE_OPERAND (ptr, 0) == current_class_ptr
  2528. && (same_type_ignoring_top_level_qualifiers_p
  2529. (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
  2530. return current_class_ref;
  2531. pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
  2532. ? ptr : decay_conversion (ptr, complain));
  2533. if (pointer == error_mark_node)
  2534. return error_mark_node;
  2535. type = TREE_TYPE (pointer);
  2536. if (POINTER_TYPE_P (type))
  2537. {
  2538. /* [expr.unary.op]
  2539. If the type of the expression is "pointer to T," the type
  2540. of the result is "T." */
  2541. tree t = TREE_TYPE (type);
  2542. if ((CONVERT_EXPR_P (ptr)
  2543. || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
  2544. && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
  2545. {
  2546. /* If a warning is issued, mark it to avoid duplicates from
  2547. the backend. This only needs to be done at
  2548. warn_strict_aliasing > 2. */
  2549. if (warn_strict_aliasing > 2)
  2550. if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
  2551. type, TREE_OPERAND (ptr, 0)))
  2552. TREE_NO_WARNING (ptr) = 1;
  2553. }
  2554. if (VOID_TYPE_P (t))
  2555. {
  2556. /* A pointer to incomplete type (other than cv void) can be
  2557. dereferenced [expr.unary.op]/1 */
  2558. if (complain & tf_error)
  2559. error ("%qT is not a pointer-to-object type", type);
  2560. return error_mark_node;
  2561. }
  2562. else if (TREE_CODE (pointer) == ADDR_EXPR
  2563. && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
  2564. /* The POINTER was something like `&x'. We simplify `*&x' to
  2565. `x'. */
  2566. return TREE_OPERAND (pointer, 0);
  2567. else
  2568. {
  2569. tree ref = build1 (INDIRECT_REF, t, pointer);
  2570. /* We *must* set TREE_READONLY when dereferencing a pointer to const,
  2571. so that we get the proper error message if the result is used
  2572. to assign to. Also, &* is supposed to be a no-op. */
  2573. TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
  2574. TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
  2575. TREE_SIDE_EFFECTS (ref)
  2576. = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
  2577. return ref;
  2578. }
  2579. }
  2580. else if (!(complain & tf_error))
  2581. /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
  2582. ;
  2583. /* `pointer' won't be an error_mark_node if we were given a
  2584. pointer to member, so it's cool to check for this here. */
  2585. else if (TYPE_PTRMEM_P (type))
  2586. switch (errorstring)
  2587. {
  2588. case RO_ARRAY_INDEXING:
  2589. error ("invalid use of array indexing on pointer to member");
  2590. break;
  2591. case RO_UNARY_STAR:
  2592. error ("invalid use of unary %<*%> on pointer to member");
  2593. break;
  2594. case RO_IMPLICIT_CONVERSION:
  2595. error ("invalid use of implicit conversion on pointer to member");
  2596. break;
  2597. case RO_ARROW_STAR:
  2598. error ("left hand operand of %<->*%> must be a pointer to class, "
  2599. "but is a pointer to member of type %qT", type);
  2600. break;
  2601. default:
  2602. gcc_unreachable ();
  2603. }
  2604. else if (pointer != error_mark_node)
  2605. invalid_indirection_error (input_location, type, errorstring);
  2606. return error_mark_node;
  2607. }
  2608. /* This handles expressions of the form "a[i]", which denotes
  2609. an array reference.
  2610. This is logically equivalent in C to *(a+i), but we may do it differently.
  2611. If A is a variable or a member, we generate a primitive ARRAY_REF.
  2612. This avoids forcing the array out of registers, and can work on
  2613. arrays that are not lvalues (for example, members of structures returned
  2614. by functions).
  2615. If INDEX is of some user-defined type, it must be converted to
  2616. integer type. Otherwise, to make a compatible PLUS_EXPR, it
  2617. will inherit the type of the array, which will be some pointer type.
  2618. LOC is the location to use in building the array reference. */
  2619. tree
  2620. cp_build_array_ref (location_t loc, tree array, tree idx,
  2621. tsubst_flags_t complain)
  2622. {
  2623. tree ret;
  2624. if (idx == 0)
  2625. {
  2626. if (complain & tf_error)
  2627. error_at (loc, "subscript missing in array reference");
  2628. return error_mark_node;
  2629. }
  2630. /* If an array's index is an array notation, then its rank cannot be
  2631. greater than one. */
  2632. if (flag_cilkplus && contains_array_notation_expr (idx))
  2633. {
  2634. size_t rank = 0;
  2635. /* If find_rank returns false, then it should have reported an error,
  2636. thus it is unnecessary for repetition. */
  2637. if (!find_rank (loc, idx, idx, true, &rank))
  2638. return error_mark_node;
  2639. if (rank > 1)
  2640. {
  2641. error_at (loc, "rank of the array%'s index is greater than 1");
  2642. return error_mark_node;
  2643. }
  2644. }
  2645. if (TREE_TYPE (array) == error_mark_node
  2646. || TREE_TYPE (idx) == error_mark_node)
  2647. return error_mark_node;
  2648. /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
  2649. inside it. */
  2650. switch (TREE_CODE (array))
  2651. {
  2652. case COMPOUND_EXPR:
  2653. {
  2654. tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
  2655. complain);
  2656. ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
  2657. TREE_OPERAND (array, 0), value);
  2658. SET_EXPR_LOCATION (ret, loc);
  2659. return ret;
  2660. }
  2661. case COND_EXPR:
  2662. ret = build_conditional_expr
  2663. (loc, TREE_OPERAND (array, 0),
  2664. cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
  2665. complain),
  2666. cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
  2667. complain),
  2668. complain);
  2669. protected_set_expr_location (ret, loc);
  2670. return ret;
  2671. default:
  2672. break;
  2673. }
  2674. bool non_lvalue
  2675. = convert_vector_to_pointer_for_subscript (loc, &array, idx);
  2676. if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
  2677. {
  2678. tree rval, type;
  2679. warn_array_subscript_with_type_char (loc, idx);
  2680. if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
  2681. {
  2682. if (complain & tf_error)
  2683. error_at (loc, "array subscript is not an integer");
  2684. return error_mark_node;
  2685. }
  2686. /* Apply integral promotions *after* noticing character types.
  2687. (It is unclear why we do these promotions -- the standard
  2688. does not say that we should. In fact, the natural thing would
  2689. seem to be to convert IDX to ptrdiff_t; we're performing
  2690. pointer arithmetic.) */
  2691. idx = cp_perform_integral_promotions (idx, complain);
  2692. /* An array that is indexed by a non-constant
  2693. cannot be stored in a register; we must be able to do
  2694. address arithmetic on its address.
  2695. Likewise an array of elements of variable size. */
  2696. if (TREE_CODE (idx) != INTEGER_CST
  2697. || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
  2698. && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
  2699. != INTEGER_CST)))
  2700. {
  2701. if (!cxx_mark_addressable (array))
  2702. return error_mark_node;
  2703. }
  2704. /* An array that is indexed by a constant value which is not within
  2705. the array bounds cannot be stored in a register either; because we
  2706. would get a crash in store_bit_field/extract_bit_field when trying
  2707. to access a non-existent part of the register. */
  2708. if (TREE_CODE (idx) == INTEGER_CST
  2709. && TYPE_DOMAIN (TREE_TYPE (array))
  2710. && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
  2711. {
  2712. if (!cxx_mark_addressable (array))
  2713. return error_mark_node;
  2714. }
  2715. /* Note in C++ it is valid to subscript a `register' array, since
  2716. it is valid to take the address of something with that
  2717. storage specification. */
  2718. if (extra_warnings)
  2719. {
  2720. tree foo = array;
  2721. while (TREE_CODE (foo) == COMPONENT_REF)
  2722. foo = TREE_OPERAND (foo, 0);
  2723. if (VAR_P (foo) && DECL_REGISTER (foo)
  2724. && (complain & tf_warning))
  2725. warning_at (loc, OPT_Wextra,
  2726. "subscripting array declared %<register%>");
  2727. }
  2728. type = TREE_TYPE (TREE_TYPE (array));
  2729. rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
  2730. /* Array ref is const/volatile if the array elements are
  2731. or if the array is.. */
  2732. TREE_READONLY (rval)
  2733. |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
  2734. TREE_SIDE_EFFECTS (rval)
  2735. |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
  2736. TREE_THIS_VOLATILE (rval)
  2737. |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
  2738. ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
  2739. complain);
  2740. protected_set_expr_location (ret, loc);
  2741. if (non_lvalue)
  2742. ret = non_lvalue_loc (loc, ret);
  2743. return ret;
  2744. }
  2745. {
  2746. tree ar = cp_default_conversion (array, complain);
  2747. tree ind = cp_default_conversion (idx, complain);
  2748. /* Put the integer in IND to simplify error checking. */
  2749. if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
  2750. {
  2751. tree temp = ar;
  2752. ar = ind;
  2753. ind = temp;
  2754. }
  2755. if (ar == error_mark_node || ind == error_mark_node)
  2756. return error_mark_node;
  2757. if (!TYPE_PTR_P (TREE_TYPE (ar)))
  2758. {
  2759. if (complain & tf_error)
  2760. error_at (loc, "subscripted value is neither array nor pointer");
  2761. return error_mark_node;
  2762. }
  2763. if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
  2764. {
  2765. if (complain & tf_error)
  2766. error_at (loc, "array subscript is not an integer");
  2767. return error_mark_node;
  2768. }
  2769. warn_array_subscript_with_type_char (loc, idx);
  2770. ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
  2771. PLUS_EXPR, ar, ind,
  2772. complain),
  2773. RO_ARRAY_INDEXING,
  2774. complain);
  2775. protected_set_expr_location (ret, loc);
  2776. if (non_lvalue)
  2777. ret = non_lvalue_loc (loc, ret);
  2778. return ret;
  2779. }
  2780. }
  2781. /* Entry point for Obj-C++. */
  2782. tree
  2783. build_array_ref (location_t loc, tree array, tree idx)
  2784. {
  2785. return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
  2786. }
  2787. /* Resolve a pointer to member function. INSTANCE is the object
  2788. instance to use, if the member points to a virtual member.
  2789. This used to avoid checking for virtual functions if basetype
  2790. has no virtual functions, according to an earlier ANSI draft.
  2791. With the final ISO C++ rules, such an optimization is
  2792. incorrect: A pointer to a derived member can be static_cast
  2793. to pointer-to-base-member, as long as the dynamic object
  2794. later has the right member. So now we only do this optimization
  2795. when we know the dynamic type of the object. */
  2796. tree
  2797. get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
  2798. tsubst_flags_t complain)
  2799. {
  2800. if (TREE_CODE (function) == OFFSET_REF)
  2801. function = TREE_OPERAND (function, 1);
  2802. if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
  2803. {
  2804. tree idx, delta, e1, e2, e3, vtbl;
  2805. bool nonvirtual;
  2806. tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
  2807. tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
  2808. tree instance_ptr = *instance_ptrptr;
  2809. tree instance_save_expr = 0;
  2810. if (instance_ptr == error_mark_node)
  2811. {
  2812. if (TREE_CODE (function) == PTRMEM_CST)
  2813. {
  2814. /* Extracting the function address from a pmf is only
  2815. allowed with -Wno-pmf-conversions. It only works for
  2816. pmf constants. */
  2817. e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
  2818. e1 = convert (fntype, e1);
  2819. return e1;
  2820. }
  2821. else
  2822. {
  2823. if (complain & tf_error)
  2824. error ("object missing in use of %qE", function);
  2825. return error_mark_node;
  2826. }
  2827. }
  2828. /* True if we know that the dynamic type of the object doesn't have
  2829. virtual functions, so we can assume the PFN field is a pointer. */
  2830. nonvirtual = (COMPLETE_TYPE_P (basetype)
  2831. && !TYPE_POLYMORPHIC_P (basetype)
  2832. && resolves_to_fixed_type_p (instance_ptr, 0));
  2833. /* If we don't really have an object (i.e. in an ill-formed
  2834. conversion from PMF to pointer), we can't resolve virtual
  2835. functions anyway. */
  2836. if (!nonvirtual && is_dummy_object (instance_ptr))
  2837. nonvirtual = true;
  2838. if (TREE_SIDE_EFFECTS (instance_ptr))
  2839. instance_ptr = instance_save_expr = save_expr (instance_ptr);
  2840. if (TREE_SIDE_EFFECTS (function))
  2841. function = save_expr (function);
  2842. /* Start by extracting all the information from the PMF itself. */
  2843. e3 = pfn_from_ptrmemfunc (function);
  2844. delta = delta_from_ptrmemfunc (function);
  2845. idx = build1 (NOP_EXPR, vtable_index_type, e3);
  2846. switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
  2847. {
  2848. case ptrmemfunc_vbit_in_pfn:
  2849. e1 = cp_build_binary_op (input_location,
  2850. BIT_AND_EXPR, idx, integer_one_node,
  2851. complain);
  2852. idx = cp_build_binary_op (input_location,
  2853. MINUS_EXPR, idx, integer_one_node,
  2854. complain);
  2855. if (idx == error_mark_node)
  2856. return error_mark_node;
  2857. break;
  2858. case ptrmemfunc_vbit_in_delta:
  2859. e1 = cp_build_binary_op (input_location,
  2860. BIT_AND_EXPR, delta, integer_one_node,
  2861. complain);
  2862. delta = cp_build_binary_op (input_location,
  2863. RSHIFT_EXPR, delta, integer_one_node,
  2864. complain);
  2865. if (delta == error_mark_node)
  2866. return error_mark_node;
  2867. break;
  2868. default:
  2869. gcc_unreachable ();
  2870. }
  2871. if (e1 == error_mark_node)
  2872. return error_mark_node;
  2873. /* Convert down to the right base before using the instance. A
  2874. special case is that in a pointer to member of class C, C may
  2875. be incomplete. In that case, the function will of course be
  2876. a member of C, and no conversion is required. In fact,
  2877. lookup_base will fail in that case, because incomplete
  2878. classes do not have BINFOs. */
  2879. if (!same_type_ignoring_top_level_qualifiers_p
  2880. (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
  2881. {
  2882. basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
  2883. basetype, ba_check, NULL, complain);
  2884. instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
  2885. 1, complain);
  2886. if (instance_ptr == error_mark_node)
  2887. return error_mark_node;
  2888. }
  2889. /* ...and then the delta in the PMF. */
  2890. instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
  2891. /* Hand back the adjusted 'this' argument to our caller. */
  2892. *instance_ptrptr = instance_ptr;
  2893. if (nonvirtual)
  2894. /* Now just return the pointer. */
  2895. return e3;
  2896. /* Next extract the vtable pointer from the object. */
  2897. vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
  2898. instance_ptr);
  2899. vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
  2900. if (vtbl == error_mark_node)
  2901. return error_mark_node;
  2902. /* Finally, extract the function pointer from the vtable. */
  2903. e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
  2904. e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
  2905. if (e2 == error_mark_node)
  2906. return error_mark_node;
  2907. TREE_CONSTANT (e2) = 1;
  2908. /* When using function descriptors, the address of the
  2909. vtable entry is treated as a function pointer. */
  2910. if (TARGET_VTABLE_USES_DESCRIPTORS)
  2911. e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
  2912. cp_build_addr_expr (e2, complain));
  2913. e2 = fold_convert (TREE_TYPE (e3), e2);
  2914. e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
  2915. if (e1 == error_mark_node)
  2916. return error_mark_node;
  2917. /* Make sure this doesn't get evaluated first inside one of the
  2918. branches of the COND_EXPR. */
  2919. if (instance_save_expr)
  2920. e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
  2921. instance_save_expr, e1);
  2922. function = e1;
  2923. }
  2924. return function;
  2925. }
  2926. /* Used by the C-common bits. */
  2927. tree
  2928. build_function_call (location_t /*loc*/,
  2929. tree function, tree params)
  2930. {
  2931. return cp_build_function_call (function, params, tf_warning_or_error);
  2932. }
  2933. /* Used by the C-common bits. */
  2934. tree
  2935. build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
  2936. tree function, vec<tree, va_gc> *params,
  2937. vec<tree, va_gc> * /*origtypes*/)
  2938. {
  2939. vec<tree, va_gc> *orig_params = params;
  2940. tree ret = cp_build_function_call_vec (function, &params,
  2941. tf_warning_or_error);
  2942. /* cp_build_function_call_vec can reallocate PARAMS by adding
  2943. default arguments. That should never happen here. Verify
  2944. that. */
  2945. gcc_assert (params == orig_params);
  2946. return ret;
  2947. }
  2948. /* Build a function call using a tree list of arguments. */
  2949. static tree
  2950. cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
  2951. {
  2952. vec<tree, va_gc> *vec;
  2953. tree ret;
  2954. vec = make_tree_vector ();
  2955. for (; params != NULL_TREE; params = TREE_CHAIN (params))
  2956. vec_safe_push (vec, TREE_VALUE (params));
  2957. ret = cp_build_function_call_vec (function, &vec, complain);
  2958. release_tree_vector (vec);
  2959. return ret;
  2960. }
  2961. /* Build a function call using varargs. */
  2962. tree
  2963. cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
  2964. {
  2965. vec<tree, va_gc> *vec;
  2966. va_list args;
  2967. tree ret, t;
  2968. vec = make_tree_vector ();
  2969. va_start (args, complain);
  2970. for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
  2971. vec_safe_push (vec, t);
  2972. va_end (args);
  2973. ret = cp_build_function_call_vec (function, &vec, complain);
  2974. release_tree_vector (vec);
  2975. return ret;
  2976. }
  2977. /* Build a function call using a vector of arguments. PARAMS may be
  2978. NULL if there are no parameters. This changes the contents of
  2979. PARAMS. */
  2980. tree
  2981. cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
  2982. tsubst_flags_t complain)
  2983. {
  2984. tree fntype, fndecl;
  2985. int is_method;
  2986. tree original = function;
  2987. int nargs;
  2988. tree *argarray;
  2989. tree parm_types;
  2990. vec<tree, va_gc> *allocated = NULL;
  2991. tree ret;
  2992. /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
  2993. expressions, like those used for ObjC messenger dispatches. */
  2994. if (params != NULL && !vec_safe_is_empty (*params))
  2995. function = objc_rewrite_function_call (function, (**params)[0]);
  2996. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  2997. Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
  2998. if (TREE_CODE (function) == NOP_EXPR
  2999. && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
  3000. function = TREE_OPERAND (function, 0);
  3001. if (TREE_CODE (function) == FUNCTION_DECL)
  3002. {
  3003. mark_used (function);
  3004. fndecl = function;
  3005. /* Convert anything with function type to a pointer-to-function. */
  3006. if (DECL_MAIN_P (function))
  3007. {
  3008. if (complain & tf_error)
  3009. pedwarn (input_location, OPT_Wpedantic,
  3010. "ISO C++ forbids calling %<::main%> from within program");
  3011. else
  3012. return error_mark_node;
  3013. }
  3014. function = build_addr_func (function, complain);
  3015. }
  3016. else
  3017. {
  3018. fndecl = NULL_TREE;
  3019. function = build_addr_func (function, complain);
  3020. }
  3021. if (function == error_mark_node)
  3022. return error_mark_node;
  3023. fntype = TREE_TYPE (function);
  3024. if (TYPE_PTRMEMFUNC_P (fntype))
  3025. {
  3026. if (complain & tf_error)
  3027. error ("must use %<.*%> or %<->*%> to call pointer-to-member "
  3028. "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
  3029. original, original);
  3030. return error_mark_node;
  3031. }
  3032. is_method = (TYPE_PTR_P (fntype)
  3033. && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
  3034. if (!(TYPE_PTRFN_P (fntype)
  3035. || is_method
  3036. || TREE_CODE (function) == TEMPLATE_ID_EXPR))
  3037. {
  3038. if (complain & tf_error)
  3039. {
  3040. if (!flag_diagnostics_show_caret)
  3041. error_at (input_location,
  3042. "%qE cannot be used as a function", original);
  3043. else if (DECL_P (original))
  3044. error_at (input_location,
  3045. "%qD cannot be used as a function", original);
  3046. else
  3047. error_at (input_location,
  3048. "expression cannot be used as a function");
  3049. }
  3050. return error_mark_node;
  3051. }
  3052. /* fntype now gets the type of function pointed to. */
  3053. fntype = TREE_TYPE (fntype);
  3054. parm_types = TYPE_ARG_TYPES (fntype);
  3055. if (params == NULL)
  3056. {
  3057. allocated = make_tree_vector ();
  3058. params = &allocated;
  3059. }
  3060. nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
  3061. complain);
  3062. if (nargs < 0)
  3063. return error_mark_node;
  3064. argarray = (*params)->address ();
  3065. /* Check for errors in format strings and inappropriately
  3066. null parameters. */
  3067. check_function_arguments (fntype, nargs, argarray);
  3068. ret = build_cxx_call (function, nargs, argarray, complain);
  3069. if (allocated != NULL)
  3070. release_tree_vector (allocated);
  3071. return ret;
  3072. }
  3073. /* Subroutine of convert_arguments.
  3074. Warn about wrong number of args are genereted. */
  3075. static void
  3076. warn_args_num (location_t loc, tree fndecl, bool too_many_p)
  3077. {
  3078. if (fndecl)
  3079. {
  3080. if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
  3081. {
  3082. if (DECL_NAME (fndecl) == NULL_TREE
  3083. || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
  3084. error_at (loc,
  3085. too_many_p
  3086. ? G_("too many arguments to constructor %q#D")
  3087. : G_("too few arguments to constructor %q#D"),
  3088. fndecl);
  3089. else
  3090. error_at (loc,
  3091. too_many_p
  3092. ? G_("too many arguments to member function %q#D")
  3093. : G_("too few arguments to member function %q#D"),
  3094. fndecl);
  3095. }
  3096. else
  3097. error_at (loc,
  3098. too_many_p
  3099. ? G_("too many arguments to function %q#D")
  3100. : G_("too few arguments to function %q#D"),
  3101. fndecl);
  3102. inform (DECL_SOURCE_LOCATION (fndecl),
  3103. "declared here");
  3104. }
  3105. else
  3106. {
  3107. if (c_dialect_objc () && objc_message_selector ())
  3108. error_at (loc,
  3109. too_many_p
  3110. ? G_("too many arguments to method %q#D")
  3111. : G_("too few arguments to method %q#D"),
  3112. objc_message_selector ());
  3113. else
  3114. error_at (loc, too_many_p ? G_("too many arguments to function")
  3115. : G_("too few arguments to function"));
  3116. }
  3117. }
  3118. /* Convert the actual parameter expressions in the list VALUES to the
  3119. types in the list TYPELIST. The converted expressions are stored
  3120. back in the VALUES vector.
  3121. If parmdecls is exhausted, or when an element has NULL as its type,
  3122. perform the default conversions.
  3123. NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
  3124. This is also where warnings about wrong number of args are generated.
  3125. Returns the actual number of arguments processed (which might be less
  3126. than the length of the vector), or -1 on error.
  3127. In C++, unspecified trailing parameters can be filled in with their
  3128. default arguments, if such were specified. Do so here. */
  3129. static int
  3130. convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
  3131. int flags, tsubst_flags_t complain)
  3132. {
  3133. tree typetail;
  3134. unsigned int i;
  3135. /* Argument passing is always copy-initialization. */
  3136. flags |= LOOKUP_ONLYCONVERTING;
  3137. for (i = 0, typetail = typelist;
  3138. i < vec_safe_length (*values);
  3139. i++)
  3140. {
  3141. tree type = typetail ? TREE_VALUE (typetail) : 0;
  3142. tree val = (**values)[i];
  3143. if (val == error_mark_node || type == error_mark_node)
  3144. return -1;
  3145. if (type == void_type_node)
  3146. {
  3147. if (complain & tf_error)
  3148. {
  3149. warn_args_num (input_location, fndecl, /*too_many_p=*/true);
  3150. return i;
  3151. }
  3152. else
  3153. return -1;
  3154. }
  3155. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  3156. Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
  3157. if (TREE_CODE (val) == NOP_EXPR
  3158. && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
  3159. && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
  3160. val = TREE_OPERAND (val, 0);
  3161. if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
  3162. {
  3163. if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
  3164. || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
  3165. || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
  3166. val = decay_conversion (val, complain);
  3167. }
  3168. if (val == error_mark_node)
  3169. return -1;
  3170. if (type != 0)
  3171. {
  3172. /* Formal parm type is specified by a function prototype. */
  3173. tree parmval;
  3174. if (!COMPLETE_TYPE_P (complete_type (type)))
  3175. {
  3176. if (complain & tf_error)
  3177. {
  3178. if (fndecl)
  3179. error ("parameter %P of %qD has incomplete type %qT",
  3180. i, fndecl, type);
  3181. else
  3182. error ("parameter %P has incomplete type %qT", i, type);
  3183. }
  3184. parmval = error_mark_node;
  3185. }
  3186. else
  3187. {
  3188. parmval = convert_for_initialization
  3189. (NULL_TREE, type, val, flags,
  3190. ICR_ARGPASS, fndecl, i, complain);
  3191. parmval = convert_for_arg_passing (type, parmval, complain);
  3192. }
  3193. if (parmval == error_mark_node)
  3194. return -1;
  3195. (**values)[i] = parmval;
  3196. }
  3197. else
  3198. {
  3199. if (fndecl && magic_varargs_p (fndecl))
  3200. /* Don't do ellipsis conversion for __built_in_constant_p
  3201. as this will result in spurious errors for non-trivial
  3202. types. */
  3203. val = require_complete_type_sfinae (val, complain);
  3204. else
  3205. val = convert_arg_to_ellipsis (val, complain);
  3206. (**values)[i] = val;
  3207. }
  3208. if (typetail)
  3209. typetail = TREE_CHAIN (typetail);
  3210. }
  3211. if (typetail != 0 && typetail != void_list_node)
  3212. {
  3213. /* See if there are default arguments that can be used. Because
  3214. we hold default arguments in the FUNCTION_TYPE (which is so
  3215. wrong), we can see default parameters here from deduced
  3216. contexts (and via typeof) for indirect function calls.
  3217. Fortunately we know whether we have a function decl to
  3218. provide default arguments in a language conformant
  3219. manner. */
  3220. if (fndecl && TREE_PURPOSE (typetail)
  3221. && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
  3222. {
  3223. for (; typetail != void_list_node; ++i)
  3224. {
  3225. tree parmval
  3226. = convert_default_arg (TREE_VALUE (typetail),
  3227. TREE_PURPOSE (typetail),
  3228. fndecl, i, complain);
  3229. if (parmval == error_mark_node)
  3230. return -1;
  3231. vec_safe_push (*values, parmval);
  3232. typetail = TREE_CHAIN (typetail);
  3233. /* ends with `...'. */
  3234. if (typetail == NULL_TREE)
  3235. break;
  3236. }
  3237. }
  3238. else
  3239. {
  3240. if (complain & tf_error)
  3241. warn_args_num (input_location, fndecl, /*too_many_p=*/false);
  3242. return -1;
  3243. }
  3244. }
  3245. return (int) i;
  3246. }
  3247. /* Build a binary-operation expression, after performing default
  3248. conversions on the operands. CODE is the kind of expression to
  3249. build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
  3250. are the tree codes which correspond to ARG1 and ARG2 when issuing
  3251. warnings about possibly misplaced parentheses. They may differ
  3252. from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
  3253. folding (e.g., if the parser sees "a | 1 + 1", it may call this
  3254. routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
  3255. To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
  3256. ARG2_CODE as ERROR_MARK. */
  3257. tree
  3258. build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
  3259. enum tree_code arg1_code, tree arg2,
  3260. enum tree_code arg2_code, tree *overload,
  3261. tsubst_flags_t complain)
  3262. {
  3263. tree orig_arg1;
  3264. tree orig_arg2;
  3265. tree expr;
  3266. orig_arg1 = arg1;
  3267. orig_arg2 = arg2;
  3268. if (processing_template_decl)
  3269. {
  3270. if (type_dependent_expression_p (arg1)
  3271. || type_dependent_expression_p (arg2))
  3272. return build_min_nt_loc (loc, code, arg1, arg2);
  3273. arg1 = build_non_dependent_expr (arg1);
  3274. arg2 = build_non_dependent_expr (arg2);
  3275. }
  3276. if (code == DOTSTAR_EXPR)
  3277. expr = build_m_component_ref (arg1, arg2, complain);
  3278. else
  3279. expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
  3280. overload, complain);
  3281. /* Check for cases such as x+y<<z which users are likely to
  3282. misinterpret. But don't warn about obj << x + y, since that is a
  3283. common idiom for I/O. */
  3284. if (warn_parentheses
  3285. && (complain & tf_warning)
  3286. && !processing_template_decl
  3287. && !error_operand_p (arg1)
  3288. && !error_operand_p (arg2)
  3289. && (code != LSHIFT_EXPR
  3290. || !CLASS_TYPE_P (TREE_TYPE (arg1))))
  3291. warn_about_parentheses (loc, code, arg1_code, orig_arg1,
  3292. arg2_code, orig_arg2);
  3293. if (processing_template_decl && expr != error_mark_node)
  3294. return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
  3295. return expr;
  3296. }
  3297. /* Build and return an ARRAY_REF expression. */
  3298. tree
  3299. build_x_array_ref (location_t loc, tree arg1, tree arg2,
  3300. tsubst_flags_t complain)
  3301. {
  3302. tree orig_arg1 = arg1;
  3303. tree orig_arg2 = arg2;
  3304. tree expr;
  3305. if (processing_template_decl)
  3306. {
  3307. if (type_dependent_expression_p (arg1)
  3308. || type_dependent_expression_p (arg2))
  3309. return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
  3310. NULL_TREE, NULL_TREE);
  3311. arg1 = build_non_dependent_expr (arg1);
  3312. arg2 = build_non_dependent_expr (arg2);
  3313. }
  3314. expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
  3315. NULL_TREE, /*overload=*/NULL, complain);
  3316. if (processing_template_decl && expr != error_mark_node)
  3317. return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
  3318. NULL_TREE, NULL_TREE);
  3319. return expr;
  3320. }
  3321. /* Return whether OP is an expression of enum type cast to integer
  3322. type. In C++ even unsigned enum types are cast to signed integer
  3323. types. We do not want to issue warnings about comparisons between
  3324. signed and unsigned types when one of the types is an enum type.
  3325. Those warnings are always false positives in practice. */
  3326. static bool
  3327. enum_cast_to_int (tree op)
  3328. {
  3329. if (CONVERT_EXPR_P (op)
  3330. && TREE_TYPE (op) == integer_type_node
  3331. && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
  3332. && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
  3333. return true;
  3334. /* The cast may have been pushed into a COND_EXPR. */
  3335. if (TREE_CODE (op) == COND_EXPR)
  3336. return (enum_cast_to_int (TREE_OPERAND (op, 1))
  3337. || enum_cast_to_int (TREE_OPERAND (op, 2)));
  3338. return false;
  3339. }
  3340. /* For the c-common bits. */
  3341. tree
  3342. build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
  3343. int /*convert_p*/)
  3344. {
  3345. return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
  3346. }
  3347. /* Build a binary-operation expression without default conversions.
  3348. CODE is the kind of expression to build.
  3349. LOCATION is the location_t of the operator in the source code.
  3350. This function differs from `build' in several ways:
  3351. the data type of the result is computed and recorded in it,
  3352. warnings are generated if arg data types are invalid,
  3353. special handling for addition and subtraction of pointers is known,
  3354. and some optimization is done (operations on narrow ints
  3355. are done in the narrower type when that gives the same result).
  3356. Constant folding is also done before the result is returned.
  3357. Note that the operands will never have enumeral types
  3358. because either they have just had the default conversions performed
  3359. or they have both just been converted to some other type in which
  3360. the arithmetic is to be done.
  3361. C++: must do special pointer arithmetic when implementing
  3362. multiple inheritance, and deal with pointer to member functions. */
  3363. tree
  3364. cp_build_binary_op (location_t location,
  3365. enum tree_code code, tree orig_op0, tree orig_op1,
  3366. tsubst_flags_t complain)
  3367. {
  3368. tree op0, op1;
  3369. enum tree_code code0, code1;
  3370. tree type0, type1;
  3371. const char *invalid_op_diag;
  3372. /* Expression code to give to the expression when it is built.
  3373. Normally this is CODE, which is what the caller asked for,
  3374. but in some special cases we change it. */
  3375. enum tree_code resultcode = code;
  3376. /* Data type in which the computation is to be performed.
  3377. In the simplest cases this is the common type of the arguments. */
  3378. tree result_type = NULL;
  3379. /* Nonzero means operands have already been type-converted
  3380. in whatever way is necessary.
  3381. Zero means they need to be converted to RESULT_TYPE. */
  3382. int converted = 0;
  3383. /* Nonzero means create the expression with this type, rather than
  3384. RESULT_TYPE. */
  3385. tree build_type = 0;
  3386. /* Nonzero means after finally constructing the expression
  3387. convert it to this type. */
  3388. tree final_type = 0;
  3389. tree result;
  3390. tree orig_type = NULL;
  3391. /* Nonzero if this is an operation like MIN or MAX which can
  3392. safely be computed in short if both args are promoted shorts.
  3393. Also implies COMMON.
  3394. -1 indicates a bitwise operation; this makes a difference
  3395. in the exact conditions for when it is safe to do the operation
  3396. in a narrower mode. */
  3397. int shorten = 0;
  3398. /* Nonzero if this is a comparison operation;
  3399. if both args are promoted shorts, compare the original shorts.
  3400. Also implies COMMON. */
  3401. int short_compare = 0;
  3402. /* Nonzero means set RESULT_TYPE to the common type of the args. */
  3403. int common = 0;
  3404. /* True if both operands have arithmetic type. */
  3405. bool arithmetic_types_p;
  3406. /* Apply default conversions. */
  3407. op0 = orig_op0;
  3408. op1 = orig_op1;
  3409. /* Remember whether we're doing / or %. */
  3410. bool doing_div_or_mod = false;
  3411. /* Remember whether we're doing << or >>. */
  3412. bool doing_shift = false;
  3413. /* Tree holding instrumentation expression. */
  3414. tree instrument_expr = NULL;
  3415. if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
  3416. || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
  3417. || code == TRUTH_XOR_EXPR)
  3418. {
  3419. if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
  3420. op0 = decay_conversion (op0, complain);
  3421. if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
  3422. op1 = decay_conversion (op1, complain);
  3423. }
  3424. else
  3425. {
  3426. if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
  3427. op0 = cp_default_conversion (op0, complain);
  3428. if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
  3429. op1 = cp_default_conversion (op1, complain);
  3430. }
  3431. /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
  3432. STRIP_TYPE_NOPS (op0);
  3433. STRIP_TYPE_NOPS (op1);
  3434. /* DTRT if one side is an overloaded function, but complain about it. */
  3435. if (type_unknown_p (op0))
  3436. {
  3437. tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
  3438. if (t != error_mark_node)
  3439. {
  3440. if (complain & tf_error)
  3441. permerror (input_location, "assuming cast to type %qT from overloaded function",
  3442. TREE_TYPE (t));
  3443. op0 = t;
  3444. }
  3445. }
  3446. if (type_unknown_p (op1))
  3447. {
  3448. tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
  3449. if (t != error_mark_node)
  3450. {
  3451. if (complain & tf_error)
  3452. permerror (input_location, "assuming cast to type %qT from overloaded function",
  3453. TREE_TYPE (t));
  3454. op1 = t;
  3455. }
  3456. }
  3457. type0 = TREE_TYPE (op0);
  3458. type1 = TREE_TYPE (op1);
  3459. /* The expression codes of the data types of the arguments tell us
  3460. whether the arguments are integers, floating, pointers, etc. */
  3461. code0 = TREE_CODE (type0);
  3462. code1 = TREE_CODE (type1);
  3463. /* If an error was already reported for one of the arguments,
  3464. avoid reporting another error. */
  3465. if (code0 == ERROR_MARK || code1 == ERROR_MARK)
  3466. return error_mark_node;
  3467. if ((invalid_op_diag
  3468. = targetm.invalid_binary_op (code, type0, type1)))
  3469. {
  3470. if (complain & tf_error)
  3471. error (invalid_op_diag);
  3472. return error_mark_node;
  3473. }
  3474. /* Issue warnings about peculiar, but valid, uses of NULL. */
  3475. if ((orig_op0 == null_node || orig_op1 == null_node)
  3476. /* It's reasonable to use pointer values as operands of &&
  3477. and ||, so NULL is no exception. */
  3478. && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
  3479. && ( /* Both are NULL (or 0) and the operation was not a
  3480. comparison or a pointer subtraction. */
  3481. (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
  3482. && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
  3483. /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
  3484. || (!null_ptr_cst_p (orig_op0)
  3485. && !TYPE_PTR_OR_PTRMEM_P (type0))
  3486. || (!null_ptr_cst_p (orig_op1)
  3487. && !TYPE_PTR_OR_PTRMEM_P (type1)))
  3488. && (complain & tf_warning))
  3489. {
  3490. source_location loc =
  3491. expansion_point_location_if_in_system_header (input_location);
  3492. warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
  3493. }
  3494. /* In case when one of the operands of the binary operation is
  3495. a vector and another is a scalar -- convert scalar to vector. */
  3496. if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
  3497. {
  3498. enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
  3499. complain & tf_error);
  3500. switch (convert_flag)
  3501. {
  3502. case stv_error:
  3503. return error_mark_node;
  3504. case stv_firstarg:
  3505. {
  3506. op0 = convert (TREE_TYPE (type1), op0);
  3507. op0 = save_expr (op0);
  3508. op0 = build_vector_from_val (type1, op0);
  3509. type0 = TREE_TYPE (op0);
  3510. code0 = TREE_CODE (type0);
  3511. converted = 1;
  3512. break;
  3513. }
  3514. case stv_secondarg:
  3515. {
  3516. op1 = convert (TREE_TYPE (type0), op1);
  3517. op1 = save_expr (op1);
  3518. op1 = build_vector_from_val (type0, op1);
  3519. type1 = TREE_TYPE (op1);
  3520. code1 = TREE_CODE (type1);
  3521. converted = 1;
  3522. break;
  3523. }
  3524. default:
  3525. break;
  3526. }
  3527. }
  3528. switch (code)
  3529. {
  3530. case MINUS_EXPR:
  3531. /* Subtraction of two similar pointers.
  3532. We must subtract them as integers, then divide by object size. */
  3533. if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
  3534. && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
  3535. TREE_TYPE (type1)))
  3536. return pointer_diff (op0, op1, common_pointer_type (type0, type1),
  3537. complain);
  3538. /* In all other cases except pointer - int, the usual arithmetic
  3539. rules apply. */
  3540. else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
  3541. {
  3542. common = 1;
  3543. break;
  3544. }
  3545. /* The pointer - int case is just like pointer + int; fall
  3546. through. */
  3547. case PLUS_EXPR:
  3548. if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
  3549. && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
  3550. {
  3551. tree ptr_operand;
  3552. tree int_operand;
  3553. ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
  3554. int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
  3555. if (processing_template_decl)
  3556. {
  3557. result_type = TREE_TYPE (ptr_operand);
  3558. break;
  3559. }
  3560. return cp_pointer_int_sum (code,
  3561. ptr_operand,
  3562. int_operand,
  3563. complain);
  3564. }
  3565. common = 1;
  3566. break;
  3567. case MULT_EXPR:
  3568. common = 1;
  3569. break;
  3570. case TRUNC_DIV_EXPR:
  3571. case CEIL_DIV_EXPR:
  3572. case FLOOR_DIV_EXPR:
  3573. case ROUND_DIV_EXPR:
  3574. case EXACT_DIV_EXPR:
  3575. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
  3576. || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
  3577. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
  3578. || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
  3579. {
  3580. enum tree_code tcode0 = code0, tcode1 = code1;
  3581. tree cop1 = fold_non_dependent_expr (op1);
  3582. doing_div_or_mod = true;
  3583. warn_for_div_by_zero (location, cop1);
  3584. if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
  3585. tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
  3586. if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
  3587. tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
  3588. if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
  3589. resultcode = RDIV_EXPR;
  3590. else
  3591. /* When dividing two signed integers, we have to promote to int.
  3592. unless we divide by a constant != -1. Note that default
  3593. conversion will have been performed on the operands at this
  3594. point, so we have to dig out the original type to find out if
  3595. it was unsigned. */
  3596. shorten = ((TREE_CODE (op0) == NOP_EXPR
  3597. && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
  3598. || (TREE_CODE (op1) == INTEGER_CST
  3599. && ! integer_all_onesp (op1)));
  3600. common = 1;
  3601. }
  3602. break;
  3603. case BIT_AND_EXPR:
  3604. case BIT_IOR_EXPR:
  3605. case BIT_XOR_EXPR:
  3606. if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  3607. || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
  3608. && !VECTOR_FLOAT_TYPE_P (type0)
  3609. && !VECTOR_FLOAT_TYPE_P (type1)))
  3610. shorten = -1;
  3611. break;
  3612. case TRUNC_MOD_EXPR:
  3613. case FLOOR_MOD_EXPR:
  3614. {
  3615. tree cop1 = fold_non_dependent_expr (op1);
  3616. doing_div_or_mod = true;
  3617. warn_for_div_by_zero (location, cop1);
  3618. }
  3619. if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
  3620. && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
  3621. && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
  3622. common = 1;
  3623. else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  3624. {
  3625. /* Although it would be tempting to shorten always here, that loses
  3626. on some targets, since the modulo instruction is undefined if the
  3627. quotient can't be represented in the computation mode. We shorten
  3628. only if unsigned or if dividing by something we know != -1. */
  3629. shorten = ((TREE_CODE (op0) == NOP_EXPR
  3630. && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
  3631. || (TREE_CODE (op1) == INTEGER_CST
  3632. && ! integer_all_onesp (op1)));
  3633. common = 1;
  3634. }
  3635. break;
  3636. case TRUTH_ANDIF_EXPR:
  3637. case TRUTH_ORIF_EXPR:
  3638. case TRUTH_AND_EXPR:
  3639. case TRUTH_OR_EXPR:
  3640. if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
  3641. {
  3642. if (!COMPARISON_CLASS_P (op1))
  3643. op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
  3644. build_zero_cst (type1), complain);
  3645. if (code == TRUTH_ANDIF_EXPR)
  3646. {
  3647. tree z = build_zero_cst (TREE_TYPE (op1));
  3648. return build_conditional_expr (location, op0, op1, z, complain);
  3649. }
  3650. else if (code == TRUTH_ORIF_EXPR)
  3651. {
  3652. tree m1 = build_all_ones_cst (TREE_TYPE (op1));
  3653. return build_conditional_expr (location, op0, m1, op1, complain);
  3654. }
  3655. else
  3656. gcc_unreachable ();
  3657. }
  3658. if (VECTOR_TYPE_P (type0))
  3659. {
  3660. if (!COMPARISON_CLASS_P (op0))
  3661. op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
  3662. build_zero_cst (type0), complain);
  3663. if (!VECTOR_TYPE_P (type1))
  3664. {
  3665. tree m1 = build_all_ones_cst (TREE_TYPE (op0));
  3666. tree z = build_zero_cst (TREE_TYPE (op0));
  3667. op1 = build_conditional_expr (location, op1, z, m1, complain);
  3668. }
  3669. else if (!COMPARISON_CLASS_P (op1))
  3670. op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
  3671. build_zero_cst (type1), complain);
  3672. if (code == TRUTH_ANDIF_EXPR)
  3673. code = BIT_AND_EXPR;
  3674. else if (code == TRUTH_ORIF_EXPR)
  3675. code = BIT_IOR_EXPR;
  3676. else
  3677. gcc_unreachable ();
  3678. return cp_build_binary_op (location, code, op0, op1, complain);
  3679. }
  3680. result_type = boolean_type_node;
  3681. break;
  3682. /* Shift operations: result has same type as first operand;
  3683. always convert second operand to int.
  3684. Also set SHORT_SHIFT if shifting rightward. */
  3685. case RSHIFT_EXPR:
  3686. if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
  3687. && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
  3688. {
  3689. result_type = type0;
  3690. converted = 1;
  3691. }
  3692. else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
  3693. && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
  3694. && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
  3695. && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
  3696. {
  3697. result_type = type0;
  3698. converted = 1;
  3699. }
  3700. else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  3701. {
  3702. tree const_op1 = fold_non_dependent_expr (op1);
  3703. if (TREE_CODE (const_op1) != INTEGER_CST)
  3704. const_op1 = op1;
  3705. result_type = type0;
  3706. doing_shift = true;
  3707. if (TREE_CODE (const_op1) == INTEGER_CST)
  3708. {
  3709. if (tree_int_cst_lt (const_op1, integer_zero_node))
  3710. {
  3711. if ((complain & tf_warning)
  3712. && c_inhibit_evaluation_warnings == 0)
  3713. warning (OPT_Wshift_count_negative,
  3714. "right shift count is negative");
  3715. }
  3716. else
  3717. {
  3718. if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
  3719. && (complain & tf_warning)
  3720. && c_inhibit_evaluation_warnings == 0)
  3721. warning (OPT_Wshift_count_overflow,
  3722. "right shift count >= width of type");
  3723. }
  3724. }
  3725. /* Avoid converting op1 to result_type later. */
  3726. converted = 1;
  3727. }
  3728. break;
  3729. case LSHIFT_EXPR:
  3730. if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
  3731. && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
  3732. {
  3733. result_type = type0;
  3734. converted = 1;
  3735. }
  3736. else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
  3737. && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
  3738. && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
  3739. && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
  3740. {
  3741. result_type = type0;
  3742. converted = 1;
  3743. }
  3744. else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  3745. {
  3746. tree const_op1 = fold_non_dependent_expr (op1);
  3747. if (TREE_CODE (const_op1) != INTEGER_CST)
  3748. const_op1 = op1;
  3749. result_type = type0;
  3750. doing_shift = true;
  3751. if (TREE_CODE (const_op1) == INTEGER_CST)
  3752. {
  3753. if (tree_int_cst_lt (const_op1, integer_zero_node))
  3754. {
  3755. if ((complain & tf_warning)
  3756. && c_inhibit_evaluation_warnings == 0)
  3757. warning (OPT_Wshift_count_negative,
  3758. "left shift count is negative");
  3759. }
  3760. else if (compare_tree_int (const_op1,
  3761. TYPE_PRECISION (type0)) >= 0)
  3762. {
  3763. if ((complain & tf_warning)
  3764. && c_inhibit_evaluation_warnings == 0)
  3765. warning (OPT_Wshift_count_overflow,
  3766. "left shift count >= width of type");
  3767. }
  3768. }
  3769. /* Avoid converting op1 to result_type later. */
  3770. converted = 1;
  3771. }
  3772. break;
  3773. case RROTATE_EXPR:
  3774. case LROTATE_EXPR:
  3775. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  3776. {
  3777. result_type = type0;
  3778. if (TREE_CODE (op1) == INTEGER_CST)
  3779. {
  3780. if (tree_int_cst_lt (op1, integer_zero_node))
  3781. {
  3782. if (complain & tf_warning)
  3783. warning (0, (code == LROTATE_EXPR)
  3784. ? G_("left rotate count is negative")
  3785. : G_("right rotate count is negative"));
  3786. }
  3787. else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
  3788. {
  3789. if (complain & tf_warning)
  3790. warning (0, (code == LROTATE_EXPR)
  3791. ? G_("left rotate count >= width of type")
  3792. : G_("right rotate count >= width of type"));
  3793. }
  3794. }
  3795. /* Convert the shift-count to an integer, regardless of
  3796. size of value being shifted. */
  3797. if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
  3798. op1 = cp_convert (integer_type_node, op1, complain);
  3799. }
  3800. break;
  3801. case EQ_EXPR:
  3802. case NE_EXPR:
  3803. if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
  3804. goto vector_compare;
  3805. if ((complain & tf_warning)
  3806. && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
  3807. warning (OPT_Wfloat_equal,
  3808. "comparing floating point with == or != is unsafe");
  3809. if ((complain & tf_warning)
  3810. && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
  3811. || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
  3812. warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
  3813. build_type = boolean_type_node;
  3814. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
  3815. || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
  3816. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
  3817. || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
  3818. short_compare = 1;
  3819. else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
  3820. && null_ptr_cst_p (op1))
  3821. /* Handle, eg, (void*)0 (c++/43906), and more. */
  3822. || (code0 == POINTER_TYPE
  3823. && TYPE_PTR_P (type1) && integer_zerop (op1)))
  3824. {
  3825. if (TYPE_PTR_P (type1))
  3826. result_type = composite_pointer_type (type0, type1, op0, op1,
  3827. CPO_COMPARISON, complain);
  3828. else
  3829. result_type = type0;
  3830. if (TREE_CODE (op0) == ADDR_EXPR
  3831. && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
  3832. {
  3833. if ((complain & tf_warning)
  3834. && c_inhibit_evaluation_warnings == 0
  3835. && !TREE_NO_WARNING (op0))
  3836. warning (OPT_Waddress, "the address of %qD will never be NULL",
  3837. TREE_OPERAND (op0, 0));
  3838. }
  3839. }
  3840. else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
  3841. && null_ptr_cst_p (op0))
  3842. /* Handle, eg, (void*)0 (c++/43906), and more. */
  3843. || (code1 == POINTER_TYPE
  3844. && TYPE_PTR_P (type0) && integer_zerop (op0)))
  3845. {
  3846. if (TYPE_PTR_P (type0))
  3847. result_type = composite_pointer_type (type0, type1, op0, op1,
  3848. CPO_COMPARISON, complain);
  3849. else
  3850. result_type = type1;
  3851. if (TREE_CODE (op1) == ADDR_EXPR
  3852. && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
  3853. {
  3854. if ((complain & tf_warning)
  3855. && c_inhibit_evaluation_warnings == 0
  3856. && !TREE_NO_WARNING (op1))
  3857. warning (OPT_Waddress, "the address of %qD will never be NULL",
  3858. TREE_OPERAND (op1, 0));
  3859. }
  3860. }
  3861. else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  3862. || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
  3863. result_type = composite_pointer_type (type0, type1, op0, op1,
  3864. CPO_COMPARISON, complain);
  3865. else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
  3866. /* One of the operands must be of nullptr_t type. */
  3867. result_type = TREE_TYPE (nullptr_node);
  3868. else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  3869. {
  3870. result_type = type0;
  3871. if (complain & tf_error)
  3872. permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
  3873. else
  3874. return error_mark_node;
  3875. }
  3876. else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
  3877. {
  3878. result_type = type1;
  3879. if (complain & tf_error)
  3880. permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
  3881. else
  3882. return error_mark_node;
  3883. }
  3884. else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
  3885. {
  3886. if (TARGET_PTRMEMFUNC_VBIT_LOCATION
  3887. == ptrmemfunc_vbit_in_delta)
  3888. {
  3889. tree pfn0, delta0, e1, e2;
  3890. if (TREE_SIDE_EFFECTS (op0))
  3891. op0 = save_expr (op0);
  3892. pfn0 = pfn_from_ptrmemfunc (op0);
  3893. delta0 = delta_from_ptrmemfunc (op0);
  3894. e1 = cp_build_binary_op (location,
  3895. EQ_EXPR,
  3896. pfn0,
  3897. build_zero_cst (TREE_TYPE (pfn0)),
  3898. complain);
  3899. e2 = cp_build_binary_op (location,
  3900. BIT_AND_EXPR,
  3901. delta0,
  3902. integer_one_node,
  3903. complain);
  3904. if (complain & tf_warning)
  3905. maybe_warn_zero_as_null_pointer_constant (op1, input_location);
  3906. e2 = cp_build_binary_op (location,
  3907. EQ_EXPR, e2, integer_zero_node,
  3908. complain);
  3909. op0 = cp_build_binary_op (location,
  3910. TRUTH_ANDIF_EXPR, e1, e2,
  3911. complain);
  3912. op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
  3913. }
  3914. else
  3915. {
  3916. op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
  3917. op1 = cp_convert (TREE_TYPE (op0), op1, complain);
  3918. }
  3919. result_type = TREE_TYPE (op0);
  3920. }
  3921. else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
  3922. return cp_build_binary_op (location, code, op1, op0, complain);
  3923. else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
  3924. {
  3925. tree type;
  3926. /* E will be the final comparison. */
  3927. tree e;
  3928. /* E1 and E2 are for scratch. */
  3929. tree e1;
  3930. tree e2;
  3931. tree pfn0;
  3932. tree pfn1;
  3933. tree delta0;
  3934. tree delta1;
  3935. type = composite_pointer_type (type0, type1, op0, op1,
  3936. CPO_COMPARISON, complain);
  3937. if (!same_type_p (TREE_TYPE (op0), type))
  3938. op0 = cp_convert_and_check (type, op0, complain);
  3939. if (!same_type_p (TREE_TYPE (op1), type))
  3940. op1 = cp_convert_and_check (type, op1, complain);
  3941. if (op0 == error_mark_node || op1 == error_mark_node)
  3942. return error_mark_node;
  3943. if (TREE_SIDE_EFFECTS (op0))
  3944. op0 = save_expr (op0);
  3945. if (TREE_SIDE_EFFECTS (op1))
  3946. op1 = save_expr (op1);
  3947. pfn0 = pfn_from_ptrmemfunc (op0);
  3948. /* Avoid -Waddress warnings (c++/64877). */
  3949. if (TREE_CODE (pfn0) == ADDR_EXPR)
  3950. TREE_NO_WARNING (pfn0) = 1;
  3951. pfn1 = pfn_from_ptrmemfunc (op1);
  3952. delta0 = delta_from_ptrmemfunc (op0);
  3953. delta1 = delta_from_ptrmemfunc (op1);
  3954. if (TARGET_PTRMEMFUNC_VBIT_LOCATION
  3955. == ptrmemfunc_vbit_in_delta)
  3956. {
  3957. /* We generate:
  3958. (op0.pfn == op1.pfn
  3959. && ((op0.delta == op1.delta)
  3960. || (!op0.pfn && op0.delta & 1 == 0
  3961. && op1.delta & 1 == 0))
  3962. The reason for the `!op0.pfn' bit is that a NULL
  3963. pointer-to-member is any member with a zero PFN and
  3964. LSB of the DELTA field is 0. */
  3965. e1 = cp_build_binary_op (location, BIT_AND_EXPR,
  3966. delta0,
  3967. integer_one_node,
  3968. complain);
  3969. e1 = cp_build_binary_op (location,
  3970. EQ_EXPR, e1, integer_zero_node,
  3971. complain);
  3972. e2 = cp_build_binary_op (location, BIT_AND_EXPR,
  3973. delta1,
  3974. integer_one_node,
  3975. complain);
  3976. e2 = cp_build_binary_op (location,
  3977. EQ_EXPR, e2, integer_zero_node,
  3978. complain);
  3979. e1 = cp_build_binary_op (location,
  3980. TRUTH_ANDIF_EXPR, e2, e1,
  3981. complain);
  3982. e2 = cp_build_binary_op (location, EQ_EXPR,
  3983. pfn0,
  3984. build_zero_cst (TREE_TYPE (pfn0)),
  3985. complain);
  3986. e2 = cp_build_binary_op (location,
  3987. TRUTH_ANDIF_EXPR, e2, e1, complain);
  3988. e1 = cp_build_binary_op (location,
  3989. EQ_EXPR, delta0, delta1, complain);
  3990. e1 = cp_build_binary_op (location,
  3991. TRUTH_ORIF_EXPR, e1, e2, complain);
  3992. }
  3993. else
  3994. {
  3995. /* We generate:
  3996. (op0.pfn == op1.pfn
  3997. && (!op0.pfn || op0.delta == op1.delta))
  3998. The reason for the `!op0.pfn' bit is that a NULL
  3999. pointer-to-member is any member with a zero PFN; the
  4000. DELTA field is unspecified. */
  4001. e1 = cp_build_binary_op (location,
  4002. EQ_EXPR, delta0, delta1, complain);
  4003. e2 = cp_build_binary_op (location,
  4004. EQ_EXPR,
  4005. pfn0,
  4006. build_zero_cst (TREE_TYPE (pfn0)),
  4007. complain);
  4008. e1 = cp_build_binary_op (location,
  4009. TRUTH_ORIF_EXPR, e1, e2, complain);
  4010. }
  4011. e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
  4012. e = cp_build_binary_op (location,
  4013. TRUTH_ANDIF_EXPR, e2, e1, complain);
  4014. if (code == EQ_EXPR)
  4015. return e;
  4016. return cp_build_binary_op (location,
  4017. EQ_EXPR, e, integer_zero_node, complain);
  4018. }
  4019. else
  4020. {
  4021. gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
  4022. || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
  4023. type1));
  4024. gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
  4025. || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
  4026. type0));
  4027. }
  4028. break;
  4029. case MAX_EXPR:
  4030. case MIN_EXPR:
  4031. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  4032. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  4033. shorten = 1;
  4034. else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  4035. result_type = composite_pointer_type (type0, type1, op0, op1,
  4036. CPO_COMPARISON, complain);
  4037. break;
  4038. case LE_EXPR:
  4039. case GE_EXPR:
  4040. case LT_EXPR:
  4041. case GT_EXPR:
  4042. if (TREE_CODE (orig_op0) == STRING_CST
  4043. || TREE_CODE (orig_op1) == STRING_CST)
  4044. {
  4045. if (complain & tf_warning)
  4046. warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
  4047. }
  4048. if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
  4049. {
  4050. vector_compare:
  4051. tree intt;
  4052. if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
  4053. TREE_TYPE (type1))
  4054. && !vector_types_compatible_elements_p (type0, type1))
  4055. {
  4056. if (complain & tf_error)
  4057. {
  4058. error_at (location, "comparing vectors with different "
  4059. "element types");
  4060. inform (location, "operand types are %qT and %qT",
  4061. type0, type1);
  4062. }
  4063. return error_mark_node;
  4064. }
  4065. if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
  4066. {
  4067. if (complain & tf_error)
  4068. {
  4069. error_at (location, "comparing vectors with different "
  4070. "number of elements");
  4071. inform (location, "operand types are %qT and %qT",
  4072. type0, type1);
  4073. }
  4074. return error_mark_node;
  4075. }
  4076. /* Always construct signed integer vector type. */
  4077. intt = c_common_type_for_size (GET_MODE_BITSIZE
  4078. (TYPE_MODE (TREE_TYPE (type0))), 0);
  4079. if (!intt)
  4080. {
  4081. if (complain & tf_error)
  4082. error_at (location, "could not find an integer type "
  4083. "of the same size as %qT", TREE_TYPE (type0));
  4084. return error_mark_node;
  4085. }
  4086. result_type = build_opaque_vector_type (intt,
  4087. TYPE_VECTOR_SUBPARTS (type0));
  4088. converted = 1;
  4089. break;
  4090. }
  4091. build_type = boolean_type_node;
  4092. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
  4093. || code0 == ENUMERAL_TYPE)
  4094. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
  4095. || code1 == ENUMERAL_TYPE))
  4096. short_compare = 1;
  4097. else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  4098. result_type = composite_pointer_type (type0, type1, op0, op1,
  4099. CPO_COMPARISON, complain);
  4100. else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
  4101. {
  4102. result_type = type0;
  4103. if (extra_warnings && (complain & tf_warning))
  4104. warning (OPT_Wextra,
  4105. "ordered comparison of pointer with integer zero");
  4106. }
  4107. else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
  4108. {
  4109. result_type = type1;
  4110. if (extra_warnings && (complain & tf_warning))
  4111. warning (OPT_Wextra,
  4112. "ordered comparison of pointer with integer zero");
  4113. }
  4114. else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
  4115. /* One of the operands must be of nullptr_t type. */
  4116. result_type = TREE_TYPE (nullptr_node);
  4117. else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  4118. {
  4119. result_type = type0;
  4120. if (complain & tf_error)
  4121. permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
  4122. else
  4123. return error_mark_node;
  4124. }
  4125. else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
  4126. {
  4127. result_type = type1;
  4128. if (complain & tf_error)
  4129. permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
  4130. else
  4131. return error_mark_node;
  4132. }
  4133. break;
  4134. case UNORDERED_EXPR:
  4135. case ORDERED_EXPR:
  4136. case UNLT_EXPR:
  4137. case UNLE_EXPR:
  4138. case UNGT_EXPR:
  4139. case UNGE_EXPR:
  4140. case UNEQ_EXPR:
  4141. build_type = integer_type_node;
  4142. if (code0 != REAL_TYPE || code1 != REAL_TYPE)
  4143. {
  4144. if (complain & tf_error)
  4145. error ("unordered comparison on non-floating point argument");
  4146. return error_mark_node;
  4147. }
  4148. common = 1;
  4149. break;
  4150. default:
  4151. break;
  4152. }
  4153. if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
  4154. || code0 == ENUMERAL_TYPE)
  4155. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
  4156. || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
  4157. arithmetic_types_p = 1;
  4158. else
  4159. {
  4160. arithmetic_types_p = 0;
  4161. /* Vector arithmetic is only allowed when both sides are vectors. */
  4162. if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
  4163. {
  4164. if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
  4165. || !vector_types_compatible_elements_p (type0, type1))
  4166. {
  4167. if (complain & tf_error)
  4168. binary_op_error (location, code, type0, type1);
  4169. return error_mark_node;
  4170. }
  4171. arithmetic_types_p = 1;
  4172. }
  4173. }
  4174. /* Determine the RESULT_TYPE, if it is not already known. */
  4175. if (!result_type
  4176. && arithmetic_types_p
  4177. && (shorten || common || short_compare))
  4178. {
  4179. result_type = cp_common_type (type0, type1);
  4180. if (complain & tf_warning)
  4181. do_warn_double_promotion (result_type, type0, type1,
  4182. "implicit conversion from %qT to %qT "
  4183. "to match other operand of binary "
  4184. "expression",
  4185. location);
  4186. }
  4187. if (!result_type)
  4188. {
  4189. if (complain & tf_error)
  4190. error ("invalid operands of types %qT and %qT to binary %qO",
  4191. TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
  4192. return error_mark_node;
  4193. }
  4194. /* If we're in a template, the only thing we need to know is the
  4195. RESULT_TYPE. */
  4196. if (processing_template_decl)
  4197. {
  4198. /* Since the middle-end checks the type when doing a build2, we
  4199. need to build the tree in pieces. This built tree will never
  4200. get out of the front-end as we replace it when instantiating
  4201. the template. */
  4202. tree tmp = build2 (resultcode,
  4203. build_type ? build_type : result_type,
  4204. NULL_TREE, op1);
  4205. TREE_OPERAND (tmp, 0) = op0;
  4206. return tmp;
  4207. }
  4208. if (arithmetic_types_p)
  4209. {
  4210. bool first_complex = (code0 == COMPLEX_TYPE);
  4211. bool second_complex = (code1 == COMPLEX_TYPE);
  4212. int none_complex = (!first_complex && !second_complex);
  4213. /* Adapted from patch for c/24581. */
  4214. if (first_complex != second_complex
  4215. && (code == PLUS_EXPR
  4216. || code == MINUS_EXPR
  4217. || code == MULT_EXPR
  4218. || (code == TRUNC_DIV_EXPR && first_complex))
  4219. && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
  4220. && flag_signed_zeros)
  4221. {
  4222. /* An operation on mixed real/complex operands must be
  4223. handled specially, but the language-independent code can
  4224. more easily optimize the plain complex arithmetic if
  4225. -fno-signed-zeros. */
  4226. tree real_type = TREE_TYPE (result_type);
  4227. tree real, imag;
  4228. if (first_complex)
  4229. {
  4230. if (TREE_TYPE (op0) != result_type)
  4231. op0 = cp_convert_and_check (result_type, op0, complain);
  4232. if (TREE_TYPE (op1) != real_type)
  4233. op1 = cp_convert_and_check (real_type, op1, complain);
  4234. }
  4235. else
  4236. {
  4237. if (TREE_TYPE (op0) != real_type)
  4238. op0 = cp_convert_and_check (real_type, op0, complain);
  4239. if (TREE_TYPE (op1) != result_type)
  4240. op1 = cp_convert_and_check (result_type, op1, complain);
  4241. }
  4242. if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
  4243. return error_mark_node;
  4244. if (first_complex)
  4245. {
  4246. op0 = save_expr (op0);
  4247. real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
  4248. imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
  4249. switch (code)
  4250. {
  4251. case MULT_EXPR:
  4252. case TRUNC_DIV_EXPR:
  4253. op1 = save_expr (op1);
  4254. imag = build2 (resultcode, real_type, imag, op1);
  4255. /* Fall through. */
  4256. case PLUS_EXPR:
  4257. case MINUS_EXPR:
  4258. real = build2 (resultcode, real_type, real, op1);
  4259. break;
  4260. default:
  4261. gcc_unreachable();
  4262. }
  4263. }
  4264. else
  4265. {
  4266. op1 = save_expr (op1);
  4267. real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
  4268. imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
  4269. switch (code)
  4270. {
  4271. case MULT_EXPR:
  4272. op0 = save_expr (op0);
  4273. imag = build2 (resultcode, real_type, op0, imag);
  4274. /* Fall through. */
  4275. case PLUS_EXPR:
  4276. real = build2 (resultcode, real_type, op0, real);
  4277. break;
  4278. case MINUS_EXPR:
  4279. real = build2 (resultcode, real_type, op0, real);
  4280. imag = build1 (NEGATE_EXPR, real_type, imag);
  4281. break;
  4282. default:
  4283. gcc_unreachable();
  4284. }
  4285. }
  4286. real = fold_if_not_in_template (real);
  4287. imag = fold_if_not_in_template (imag);
  4288. result = build2 (COMPLEX_EXPR, result_type, real, imag);
  4289. result = fold_if_not_in_template (result);
  4290. return result;
  4291. }
  4292. /* For certain operations (which identify themselves by shorten != 0)
  4293. if both args were extended from the same smaller type,
  4294. do the arithmetic in that type and then extend.
  4295. shorten !=0 and !=1 indicates a bitwise operation.
  4296. For them, this optimization is safe only if
  4297. both args are zero-extended or both are sign-extended.
  4298. Otherwise, we might change the result.
  4299. E.g., (short)-1 | (unsigned short)-1 is (int)-1
  4300. but calculated in (unsigned short) it would be (unsigned short)-1. */
  4301. if (shorten && none_complex)
  4302. {
  4303. orig_type = result_type;
  4304. final_type = result_type;
  4305. result_type = shorten_binary_op (result_type, op0, op1,
  4306. shorten == -1);
  4307. }
  4308. /* Comparison operations are shortened too but differently.
  4309. They identify themselves by setting short_compare = 1. */
  4310. if (short_compare)
  4311. {
  4312. /* Don't write &op0, etc., because that would prevent op0
  4313. from being kept in a register.
  4314. Instead, make copies of the our local variables and
  4315. pass the copies by reference, then copy them back afterward. */
  4316. tree xop0 = op0, xop1 = op1, xresult_type = result_type;
  4317. enum tree_code xresultcode = resultcode;
  4318. tree val
  4319. = shorten_compare (location, &xop0, &xop1, &xresult_type,
  4320. &xresultcode);
  4321. if (val != 0)
  4322. return cp_convert (boolean_type_node, val, complain);
  4323. op0 = xop0, op1 = xop1;
  4324. converted = 1;
  4325. resultcode = xresultcode;
  4326. }
  4327. if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
  4328. && warn_sign_compare
  4329. /* Do not warn until the template is instantiated; we cannot
  4330. bound the ranges of the arguments until that point. */
  4331. && !processing_template_decl
  4332. && (complain & tf_warning)
  4333. && c_inhibit_evaluation_warnings == 0
  4334. /* Even unsigned enum types promote to signed int. We don't
  4335. want to issue -Wsign-compare warnings for this case. */
  4336. && !enum_cast_to_int (orig_op0)
  4337. && !enum_cast_to_int (orig_op1))
  4338. {
  4339. tree oop0 = maybe_constant_value (orig_op0);
  4340. tree oop1 = maybe_constant_value (orig_op1);
  4341. if (TREE_CODE (oop0) != INTEGER_CST)
  4342. oop0 = orig_op0;
  4343. if (TREE_CODE (oop1) != INTEGER_CST)
  4344. oop1 = orig_op1;
  4345. warn_for_sign_compare (location, oop0, oop1, op0, op1,
  4346. result_type, resultcode);
  4347. }
  4348. }
  4349. /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
  4350. Then the expression will be built.
  4351. It will be given type FINAL_TYPE if that is nonzero;
  4352. otherwise, it will be given type RESULT_TYPE. */
  4353. if (! converted)
  4354. {
  4355. if (TREE_TYPE (op0) != result_type)
  4356. op0 = cp_convert_and_check (result_type, op0, complain);
  4357. if (TREE_TYPE (op1) != result_type)
  4358. op1 = cp_convert_and_check (result_type, op1, complain);
  4359. if (op0 == error_mark_node || op1 == error_mark_node)
  4360. return error_mark_node;
  4361. }
  4362. if (build_type == NULL_TREE)
  4363. build_type = result_type;
  4364. if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
  4365. | SANITIZE_FLOAT_DIVIDE))
  4366. && !processing_template_decl
  4367. && do_ubsan_in_current_function ()
  4368. && (doing_div_or_mod || doing_shift))
  4369. {
  4370. /* OP0 and/or OP1 might have side-effects. */
  4371. op0 = cp_save_expr (op0);
  4372. op1 = cp_save_expr (op1);
  4373. op0 = fold_non_dependent_expr (op0);
  4374. op1 = fold_non_dependent_expr (op1);
  4375. if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
  4376. | SANITIZE_FLOAT_DIVIDE)))
  4377. {
  4378. /* For diagnostics we want to use the promoted types without
  4379. shorten_binary_op. So convert the arguments to the
  4380. original result_type. */
  4381. tree cop0 = op0;
  4382. tree cop1 = op1;
  4383. if (orig_type != NULL && result_type != orig_type)
  4384. {
  4385. cop0 = cp_convert (orig_type, op0, complain);
  4386. cop1 = cp_convert (orig_type, op1, complain);
  4387. }
  4388. instrument_expr = ubsan_instrument_division (location, cop0, cop1);
  4389. }
  4390. else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
  4391. instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
  4392. }
  4393. result = build2 (resultcode, build_type, op0, op1);
  4394. result = fold_if_not_in_template (result);
  4395. if (final_type != 0)
  4396. result = cp_convert (final_type, result, complain);
  4397. if (TREE_OVERFLOW_P (result)
  4398. && !TREE_OVERFLOW_P (op0)
  4399. && !TREE_OVERFLOW_P (op1))
  4400. overflow_warning (location, result);
  4401. if (instrument_expr != NULL)
  4402. result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
  4403. instrument_expr, result);
  4404. return result;
  4405. }
  4406. /* Build a VEC_PERM_EXPR.
  4407. This is a simple wrapper for c_build_vec_perm_expr. */
  4408. tree
  4409. build_x_vec_perm_expr (location_t loc,
  4410. tree arg0, tree arg1, tree arg2,
  4411. tsubst_flags_t complain)
  4412. {
  4413. tree orig_arg0 = arg0;
  4414. tree orig_arg1 = arg1;
  4415. tree orig_arg2 = arg2;
  4416. if (processing_template_decl)
  4417. {
  4418. if (type_dependent_expression_p (arg0)
  4419. || type_dependent_expression_p (arg1)
  4420. || type_dependent_expression_p (arg2))
  4421. return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
  4422. arg0 = build_non_dependent_expr (arg0);
  4423. if (arg1)
  4424. arg1 = build_non_dependent_expr (arg1);
  4425. arg2 = build_non_dependent_expr (arg2);
  4426. }
  4427. tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
  4428. if (processing_template_decl && exp != error_mark_node)
  4429. return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
  4430. orig_arg1, orig_arg2);
  4431. return exp;
  4432. }
  4433. /* Return a tree for the sum or difference (RESULTCODE says which)
  4434. of pointer PTROP and integer INTOP. */
  4435. static tree
  4436. cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
  4437. tsubst_flags_t complain)
  4438. {
  4439. tree res_type = TREE_TYPE (ptrop);
  4440. /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
  4441. in certain circumstance (when it's valid to do so). So we need
  4442. to make sure it's complete. We don't need to check here, if we
  4443. can actually complete it at all, as those checks will be done in
  4444. pointer_int_sum() anyway. */
  4445. complete_type (TREE_TYPE (res_type));
  4446. return pointer_int_sum (input_location, resultcode, ptrop,
  4447. fold_if_not_in_template (intop),
  4448. complain & tf_warning_or_error);
  4449. }
  4450. /* Return a tree for the difference of pointers OP0 and OP1.
  4451. The resulting tree has type int. */
  4452. static tree
  4453. pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
  4454. {
  4455. tree result;
  4456. tree restype = ptrdiff_type_node;
  4457. tree target_type = TREE_TYPE (ptrtype);
  4458. if (!complete_type_or_else (target_type, NULL_TREE))
  4459. return error_mark_node;
  4460. if (VOID_TYPE_P (target_type))
  4461. {
  4462. if (complain & tf_error)
  4463. permerror (input_location, "ISO C++ forbids using pointer of "
  4464. "type %<void *%> in subtraction");
  4465. else
  4466. return error_mark_node;
  4467. }
  4468. if (TREE_CODE (target_type) == FUNCTION_TYPE)
  4469. {
  4470. if (complain & tf_error)
  4471. permerror (input_location, "ISO C++ forbids using pointer to "
  4472. "a function in subtraction");
  4473. else
  4474. return error_mark_node;
  4475. }
  4476. if (TREE_CODE (target_type) == METHOD_TYPE)
  4477. {
  4478. if (complain & tf_error)
  4479. permerror (input_location, "ISO C++ forbids using pointer to "
  4480. "a method in subtraction");
  4481. else
  4482. return error_mark_node;
  4483. }
  4484. /* First do the subtraction as integers;
  4485. then drop through to build the divide operator. */
  4486. op0 = cp_build_binary_op (input_location,
  4487. MINUS_EXPR,
  4488. cp_convert (restype, op0, complain),
  4489. cp_convert (restype, op1, complain),
  4490. complain);
  4491. /* This generates an error if op1 is a pointer to an incomplete type. */
  4492. if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
  4493. {
  4494. if (complain & tf_error)
  4495. error ("invalid use of a pointer to an incomplete type in "
  4496. "pointer arithmetic");
  4497. else
  4498. return error_mark_node;
  4499. }
  4500. if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
  4501. {
  4502. if (complain & tf_error)
  4503. error ("arithmetic on pointer to an empty aggregate");
  4504. else
  4505. return error_mark_node;
  4506. }
  4507. op1 = (TYPE_PTROB_P (ptrtype)
  4508. ? size_in_bytes (target_type)
  4509. : integer_one_node);
  4510. /* Do the division. */
  4511. result = build2 (EXACT_DIV_EXPR, restype, op0,
  4512. cp_convert (restype, op1, complain));
  4513. return fold_if_not_in_template (result);
  4514. }
  4515. /* Construct and perhaps optimize a tree representation
  4516. for a unary operation. CODE, a tree_code, specifies the operation
  4517. and XARG is the operand. */
  4518. tree
  4519. build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
  4520. tsubst_flags_t complain)
  4521. {
  4522. tree orig_expr = xarg;
  4523. tree exp;
  4524. int ptrmem = 0;
  4525. if (processing_template_decl)
  4526. {
  4527. if (type_dependent_expression_p (xarg))
  4528. return build_min_nt_loc (loc, code, xarg, NULL_TREE);
  4529. xarg = build_non_dependent_expr (xarg);
  4530. }
  4531. exp = NULL_TREE;
  4532. /* [expr.unary.op] says:
  4533. The address of an object of incomplete type can be taken.
  4534. (And is just the ordinary address operator, not an overloaded
  4535. "operator &".) However, if the type is a template
  4536. specialization, we must complete the type at this point so that
  4537. an overloaded "operator &" will be available if required. */
  4538. if (code == ADDR_EXPR
  4539. && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
  4540. && ((CLASS_TYPE_P (TREE_TYPE (xarg))
  4541. && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
  4542. || (TREE_CODE (xarg) == OFFSET_REF)))
  4543. /* Don't look for a function. */;
  4544. else
  4545. exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
  4546. NULL_TREE, /*overload=*/NULL, complain);
  4547. if (!exp && code == ADDR_EXPR)
  4548. {
  4549. if (is_overloaded_fn (xarg))
  4550. {
  4551. tree fn = get_first_fn (xarg);
  4552. if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
  4553. {
  4554. if (complain & tf_error)
  4555. error (DECL_CONSTRUCTOR_P (fn)
  4556. ? G_("taking address of constructor %qE")
  4557. : G_("taking address of destructor %qE"),
  4558. xarg);
  4559. return error_mark_node;
  4560. }
  4561. }
  4562. /* A pointer to member-function can be formed only by saying
  4563. &X::mf. */
  4564. if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
  4565. && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
  4566. {
  4567. if (TREE_CODE (xarg) != OFFSET_REF
  4568. || !TYPE_P (TREE_OPERAND (xarg, 0)))
  4569. {
  4570. if (complain & tf_error)
  4571. {
  4572. error ("invalid use of %qE to form a "
  4573. "pointer-to-member-function", xarg);
  4574. if (TREE_CODE (xarg) != OFFSET_REF)
  4575. inform (input_location, " a qualified-id is required");
  4576. }
  4577. return error_mark_node;
  4578. }
  4579. else
  4580. {
  4581. if (complain & tf_error)
  4582. error ("parentheses around %qE cannot be used to form a"
  4583. " pointer-to-member-function",
  4584. xarg);
  4585. else
  4586. return error_mark_node;
  4587. PTRMEM_OK_P (xarg) = 1;
  4588. }
  4589. }
  4590. if (TREE_CODE (xarg) == OFFSET_REF)
  4591. {
  4592. ptrmem = PTRMEM_OK_P (xarg);
  4593. if (!ptrmem && !flag_ms_extensions
  4594. && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
  4595. {
  4596. /* A single non-static member, make sure we don't allow a
  4597. pointer-to-member. */
  4598. xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
  4599. TREE_OPERAND (xarg, 0),
  4600. ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
  4601. PTRMEM_OK_P (xarg) = ptrmem;
  4602. }
  4603. }
  4604. exp = cp_build_addr_expr_strict (xarg, complain);
  4605. }
  4606. if (processing_template_decl && exp != error_mark_node)
  4607. exp = build_min_non_dep (code, exp, orig_expr,
  4608. /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
  4609. if (TREE_CODE (exp) == ADDR_EXPR)
  4610. PTRMEM_OK_P (exp) = ptrmem;
  4611. return exp;
  4612. }
  4613. /* Like c_common_truthvalue_conversion, but handle pointer-to-member
  4614. constants, where a null value is represented by an INTEGER_CST of
  4615. -1. */
  4616. tree
  4617. cp_truthvalue_conversion (tree expr)
  4618. {
  4619. tree type = TREE_TYPE (expr);
  4620. if (TYPE_PTRDATAMEM_P (type)
  4621. /* Avoid ICE on invalid use of non-static member function. */
  4622. || TREE_CODE (expr) == FUNCTION_DECL)
  4623. return build_binary_op (EXPR_LOCATION (expr),
  4624. NE_EXPR, expr, nullptr_node, 1);
  4625. else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
  4626. {
  4627. /* With -Wzero-as-null-pointer-constant do not warn for an
  4628. 'if (p)' or a 'while (!p)', where p is a pointer. */
  4629. tree ret;
  4630. ++c_inhibit_evaluation_warnings;
  4631. ret = c_common_truthvalue_conversion (input_location, expr);
  4632. --c_inhibit_evaluation_warnings;
  4633. return ret;
  4634. }
  4635. else
  4636. return c_common_truthvalue_conversion (input_location, expr);
  4637. }
  4638. /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
  4639. tree
  4640. condition_conversion (tree expr)
  4641. {
  4642. tree t;
  4643. if (processing_template_decl)
  4644. return expr;
  4645. t = perform_implicit_conversion_flags (boolean_type_node, expr,
  4646. tf_warning_or_error, LOOKUP_NORMAL);
  4647. t = fold_build_cleanup_point_expr (boolean_type_node, t);
  4648. return t;
  4649. }
  4650. /* Returns the address of T. This function will fold away
  4651. ADDR_EXPR of INDIRECT_REF. */
  4652. tree
  4653. build_address (tree t)
  4654. {
  4655. if (error_operand_p (t) || !cxx_mark_addressable (t))
  4656. return error_mark_node;
  4657. gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
  4658. t = build_fold_addr_expr (t);
  4659. if (TREE_CODE (t) != ADDR_EXPR)
  4660. t = rvalue (t);
  4661. return t;
  4662. }
  4663. /* Return a NOP_EXPR converting EXPR to TYPE. */
  4664. tree
  4665. build_nop (tree type, tree expr)
  4666. {
  4667. if (type == error_mark_node || error_operand_p (expr))
  4668. return expr;
  4669. return build1 (NOP_EXPR, type, expr);
  4670. }
  4671. /* Take the address of ARG, whatever that means under C++ semantics.
  4672. If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
  4673. and class rvalues as well.
  4674. Nothing should call this function directly; instead, callers should use
  4675. cp_build_addr_expr or cp_build_addr_expr_strict. */
  4676. static tree
  4677. cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
  4678. {
  4679. tree argtype;
  4680. tree val;
  4681. if (!arg || error_operand_p (arg))
  4682. return error_mark_node;
  4683. arg = mark_lvalue_use (arg);
  4684. argtype = lvalue_type (arg);
  4685. gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
  4686. if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
  4687. && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
  4688. {
  4689. /* They're trying to take the address of a unique non-static
  4690. member function. This is ill-formed (except in MS-land),
  4691. but let's try to DTRT.
  4692. Note: We only handle unique functions here because we don't
  4693. want to complain if there's a static overload; non-unique
  4694. cases will be handled by instantiate_type. But we need to
  4695. handle this case here to allow casts on the resulting PMF.
  4696. We could defer this in non-MS mode, but it's easier to give
  4697. a useful error here. */
  4698. /* Inside constant member functions, the `this' pointer
  4699. contains an extra const qualifier. TYPE_MAIN_VARIANT
  4700. is used here to remove this const from the diagnostics
  4701. and the created OFFSET_REF. */
  4702. tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
  4703. tree fn = get_first_fn (TREE_OPERAND (arg, 1));
  4704. mark_used (fn);
  4705. if (! flag_ms_extensions)
  4706. {
  4707. tree name = DECL_NAME (fn);
  4708. if (!(complain & tf_error))
  4709. return error_mark_node;
  4710. else if (current_class_type
  4711. && TREE_OPERAND (arg, 0) == current_class_ref)
  4712. /* An expression like &memfn. */
  4713. permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
  4714. " or parenthesized non-static member function to form"
  4715. " a pointer to member function. Say %<&%T::%D%>",
  4716. base, name);
  4717. else
  4718. permerror (input_location, "ISO C++ forbids taking the address of a bound member"
  4719. " function to form a pointer to member function."
  4720. " Say %<&%T::%D%>",
  4721. base, name);
  4722. }
  4723. arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
  4724. }
  4725. /* Uninstantiated types are all functions. Taking the
  4726. address of a function is a no-op, so just return the
  4727. argument. */
  4728. if (type_unknown_p (arg))
  4729. return build1 (ADDR_EXPR, unknown_type_node, arg);
  4730. if (TREE_CODE (arg) == OFFSET_REF)
  4731. /* We want a pointer to member; bypass all the code for actually taking
  4732. the address of something. */
  4733. goto offset_ref;
  4734. /* Anything not already handled and not a true memory reference
  4735. is an error. */
  4736. if (TREE_CODE (argtype) != FUNCTION_TYPE
  4737. && TREE_CODE (argtype) != METHOD_TYPE)
  4738. {
  4739. cp_lvalue_kind kind = lvalue_kind (arg);
  4740. if (kind == clk_none)
  4741. {
  4742. if (complain & tf_error)
  4743. lvalue_error (input_location, lv_addressof);
  4744. return error_mark_node;
  4745. }
  4746. if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
  4747. {
  4748. if (!(complain & tf_error))
  4749. return error_mark_node;
  4750. if (kind & clk_class)
  4751. /* Make this a permerror because we used to accept it. */
  4752. permerror (input_location, "taking address of temporary");
  4753. else
  4754. error ("taking address of xvalue (rvalue reference)");
  4755. }
  4756. }
  4757. if (TREE_CODE (argtype) == REFERENCE_TYPE)
  4758. {
  4759. tree type = build_pointer_type (TREE_TYPE (argtype));
  4760. arg = build1 (CONVERT_EXPR, type, arg);
  4761. return arg;
  4762. }
  4763. else if (pedantic && DECL_MAIN_P (arg))
  4764. {
  4765. /* ARM $3.4 */
  4766. /* Apparently a lot of autoconf scripts for C++ packages do this,
  4767. so only complain if -Wpedantic. */
  4768. if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
  4769. pedwarn (input_location, OPT_Wpedantic,
  4770. "ISO C++ forbids taking address of function %<::main%>");
  4771. else if (flag_pedantic_errors)
  4772. return error_mark_node;
  4773. }
  4774. /* Let &* cancel out to simplify resulting code. */
  4775. if (INDIRECT_REF_P (arg))
  4776. {
  4777. /* We don't need to have `current_class_ptr' wrapped in a
  4778. NON_LVALUE_EXPR node. */
  4779. if (arg == current_class_ref)
  4780. return current_class_ptr;
  4781. arg = TREE_OPERAND (arg, 0);
  4782. if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
  4783. {
  4784. tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
  4785. arg = build1 (CONVERT_EXPR, type, arg);
  4786. }
  4787. else
  4788. /* Don't let this be an lvalue. */
  4789. arg = rvalue (arg);
  4790. return arg;
  4791. }
  4792. /* ??? Cope with user tricks that amount to offsetof. */
  4793. if (TREE_CODE (argtype) != FUNCTION_TYPE
  4794. && TREE_CODE (argtype) != METHOD_TYPE
  4795. && argtype != unknown_type_node
  4796. && (val = get_base_address (arg))
  4797. && COMPLETE_TYPE_P (TREE_TYPE (val))
  4798. && INDIRECT_REF_P (val)
  4799. && TREE_CONSTANT (TREE_OPERAND (val, 0)))
  4800. {
  4801. tree type = build_pointer_type (argtype);
  4802. return fold_convert (type, fold_offsetof_1 (arg));
  4803. }
  4804. /* Handle complex lvalues (when permitted)
  4805. by reduction to simpler cases. */
  4806. val = unary_complex_lvalue (ADDR_EXPR, arg);
  4807. if (val != 0)
  4808. return val;
  4809. switch (TREE_CODE (arg))
  4810. {
  4811. CASE_CONVERT:
  4812. case FLOAT_EXPR:
  4813. case FIX_TRUNC_EXPR:
  4814. /* Even if we're not being pedantic, we cannot allow this
  4815. extension when we're instantiating in a SFINAE
  4816. context. */
  4817. if (! lvalue_p (arg) && complain == tf_none)
  4818. {
  4819. if (complain & tf_error)
  4820. permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
  4821. else
  4822. return error_mark_node;
  4823. }
  4824. break;
  4825. case BASELINK:
  4826. arg = BASELINK_FUNCTIONS (arg);
  4827. /* Fall through. */
  4828. case OVERLOAD:
  4829. arg = OVL_CURRENT (arg);
  4830. break;
  4831. case OFFSET_REF:
  4832. offset_ref:
  4833. /* Turn a reference to a non-static data member into a
  4834. pointer-to-member. */
  4835. {
  4836. tree type;
  4837. tree t;
  4838. gcc_assert (PTRMEM_OK_P (arg));
  4839. t = TREE_OPERAND (arg, 1);
  4840. if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
  4841. {
  4842. if (complain & tf_error)
  4843. error ("cannot create pointer to reference member %qD", t);
  4844. return error_mark_node;
  4845. }
  4846. type = build_ptrmem_type (context_for_name_lookup (t),
  4847. TREE_TYPE (t));
  4848. t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
  4849. return t;
  4850. }
  4851. default:
  4852. break;
  4853. }
  4854. if (argtype != error_mark_node)
  4855. argtype = build_pointer_type (argtype);
  4856. /* In a template, we are processing a non-dependent expression
  4857. so we can just form an ADDR_EXPR with the correct type. */
  4858. if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
  4859. {
  4860. val = build_address (arg);
  4861. if (TREE_CODE (arg) == OFFSET_REF)
  4862. PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
  4863. }
  4864. else if (BASELINK_P (TREE_OPERAND (arg, 1)))
  4865. {
  4866. tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
  4867. /* We can only get here with a single static member
  4868. function. */
  4869. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  4870. && DECL_STATIC_FUNCTION_P (fn));
  4871. mark_used (fn);
  4872. val = build_address (fn);
  4873. if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
  4874. /* Do not lose object's side effects. */
  4875. val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
  4876. TREE_OPERAND (arg, 0), val);
  4877. }
  4878. else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
  4879. {
  4880. if (complain & tf_error)
  4881. error ("attempt to take address of bit-field structure member %qD",
  4882. TREE_OPERAND (arg, 1));
  4883. return error_mark_node;
  4884. }
  4885. else
  4886. {
  4887. tree object = TREE_OPERAND (arg, 0);
  4888. tree field = TREE_OPERAND (arg, 1);
  4889. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  4890. (TREE_TYPE (object), decl_type_context (field)));
  4891. val = build_address (arg);
  4892. }
  4893. if (TYPE_PTR_P (argtype)
  4894. && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
  4895. {
  4896. build_ptrmemfunc_type (argtype);
  4897. val = build_ptrmemfunc (argtype, val, 0,
  4898. /*c_cast_p=*/false,
  4899. complain);
  4900. }
  4901. return val;
  4902. }
  4903. /* Take the address of ARG if it has one, even if it's an rvalue. */
  4904. tree
  4905. cp_build_addr_expr (tree arg, tsubst_flags_t complain)
  4906. {
  4907. return cp_build_addr_expr_1 (arg, 0, complain);
  4908. }
  4909. /* Take the address of ARG, but only if it's an lvalue. */
  4910. static tree
  4911. cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
  4912. {
  4913. return cp_build_addr_expr_1 (arg, 1, complain);
  4914. }
  4915. /* C++: Must handle pointers to members.
  4916. Perhaps type instantiation should be extended to handle conversion
  4917. from aggregates to types we don't yet know we want? (Or are those
  4918. cases typically errors which should be reported?)
  4919. NOCONVERT nonzero suppresses the default promotions
  4920. (such as from short to int). */
  4921. tree
  4922. cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
  4923. tsubst_flags_t complain)
  4924. {
  4925. /* No default_conversion here. It causes trouble for ADDR_EXPR. */
  4926. tree arg = xarg;
  4927. tree argtype = 0;
  4928. const char *errstring = NULL;
  4929. tree val;
  4930. const char *invalid_op_diag;
  4931. if (!arg || error_operand_p (arg))
  4932. return error_mark_node;
  4933. if ((invalid_op_diag
  4934. = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
  4935. ? CONVERT_EXPR
  4936. : code),
  4937. TREE_TYPE (xarg))))
  4938. {
  4939. if (complain & tf_error)
  4940. error (invalid_op_diag);
  4941. return error_mark_node;
  4942. }
  4943. switch (code)
  4944. {
  4945. case UNARY_PLUS_EXPR:
  4946. case NEGATE_EXPR:
  4947. {
  4948. int flags = WANT_ARITH | WANT_ENUM;
  4949. /* Unary plus (but not unary minus) is allowed on pointers. */
  4950. if (code == UNARY_PLUS_EXPR)
  4951. flags |= WANT_POINTER;
  4952. arg = build_expr_type_conversion (flags, arg, true);
  4953. if (!arg)
  4954. errstring = (code == NEGATE_EXPR
  4955. ? _("wrong type argument to unary minus")
  4956. : _("wrong type argument to unary plus"));
  4957. else
  4958. {
  4959. if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
  4960. arg = cp_perform_integral_promotions (arg, complain);
  4961. /* Make sure the result is not an lvalue: a unary plus or minus
  4962. expression is always a rvalue. */
  4963. arg = rvalue (arg);
  4964. }
  4965. }
  4966. break;
  4967. case BIT_NOT_EXPR:
  4968. if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
  4969. {
  4970. code = CONJ_EXPR;
  4971. if (!noconvert)
  4972. {
  4973. arg = cp_default_conversion (arg, complain);
  4974. if (arg == error_mark_node)
  4975. return error_mark_node;
  4976. }
  4977. }
  4978. else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
  4979. | WANT_VECTOR_OR_COMPLEX,
  4980. arg, true)))
  4981. errstring = _("wrong type argument to bit-complement");
  4982. else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
  4983. arg = cp_perform_integral_promotions (arg, complain);
  4984. break;
  4985. case ABS_EXPR:
  4986. if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
  4987. errstring = _("wrong type argument to abs");
  4988. else if (!noconvert)
  4989. {
  4990. arg = cp_default_conversion (arg, complain);
  4991. if (arg == error_mark_node)
  4992. return error_mark_node;
  4993. }
  4994. break;
  4995. case CONJ_EXPR:
  4996. /* Conjugating a real value is a no-op, but allow it anyway. */
  4997. if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
  4998. errstring = _("wrong type argument to conjugation");
  4999. else if (!noconvert)
  5000. {
  5001. arg = cp_default_conversion (arg, complain);
  5002. if (arg == error_mark_node)
  5003. return error_mark_node;
  5004. }
  5005. break;
  5006. case TRUTH_NOT_EXPR:
  5007. if (VECTOR_TYPE_P (TREE_TYPE (arg)))
  5008. return cp_build_binary_op (input_location, EQ_EXPR, arg,
  5009. build_zero_cst (TREE_TYPE (arg)), complain);
  5010. arg = perform_implicit_conversion (boolean_type_node, arg,
  5011. complain);
  5012. val = invert_truthvalue_loc (input_location, arg);
  5013. if (arg != error_mark_node)
  5014. return val;
  5015. errstring = _("in argument to unary !");
  5016. break;
  5017. case NOP_EXPR:
  5018. break;
  5019. case REALPART_EXPR:
  5020. case IMAGPART_EXPR:
  5021. arg = build_real_imag_expr (input_location, code, arg);
  5022. if (arg == error_mark_node)
  5023. return arg;
  5024. else
  5025. return fold_if_not_in_template (arg);
  5026. case PREINCREMENT_EXPR:
  5027. case POSTINCREMENT_EXPR:
  5028. case PREDECREMENT_EXPR:
  5029. case POSTDECREMENT_EXPR:
  5030. /* Handle complex lvalues (when permitted)
  5031. by reduction to simpler cases. */
  5032. val = unary_complex_lvalue (code, arg);
  5033. if (val != 0)
  5034. return val;
  5035. arg = mark_lvalue_use (arg);
  5036. /* Increment or decrement the real part of the value,
  5037. and don't change the imaginary part. */
  5038. if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
  5039. {
  5040. tree real, imag;
  5041. arg = stabilize_reference (arg);
  5042. real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
  5043. imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
  5044. real = cp_build_unary_op (code, real, 1, complain);
  5045. if (real == error_mark_node || imag == error_mark_node)
  5046. return error_mark_node;
  5047. return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
  5048. real, imag);
  5049. }
  5050. /* Report invalid types. */
  5051. if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
  5052. arg, true)))
  5053. {
  5054. if (code == PREINCREMENT_EXPR)
  5055. errstring = _("no pre-increment operator for type");
  5056. else if (code == POSTINCREMENT_EXPR)
  5057. errstring = _("no post-increment operator for type");
  5058. else if (code == PREDECREMENT_EXPR)
  5059. errstring = _("no pre-decrement operator for type");
  5060. else
  5061. errstring = _("no post-decrement operator for type");
  5062. break;
  5063. }
  5064. else if (arg == error_mark_node)
  5065. return error_mark_node;
  5066. /* Report something read-only. */
  5067. if (CP_TYPE_CONST_P (TREE_TYPE (arg))
  5068. || TREE_READONLY (arg))
  5069. {
  5070. if (complain & tf_error)
  5071. cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
  5072. || code == POSTINCREMENT_EXPR)
  5073. ? lv_increment : lv_decrement));
  5074. else
  5075. return error_mark_node;
  5076. }
  5077. {
  5078. tree inc;
  5079. tree declared_type = unlowered_expr_type (arg);
  5080. argtype = TREE_TYPE (arg);
  5081. /* ARM $5.2.5 last annotation says this should be forbidden. */
  5082. if (TREE_CODE (argtype) == ENUMERAL_TYPE)
  5083. {
  5084. if (complain & tf_error)
  5085. permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
  5086. ? G_("ISO C++ forbids incrementing an enum")
  5087. : G_("ISO C++ forbids decrementing an enum"));
  5088. else
  5089. return error_mark_node;
  5090. }
  5091. /* Compute the increment. */
  5092. if (TYPE_PTR_P (argtype))
  5093. {
  5094. tree type = complete_type (TREE_TYPE (argtype));
  5095. if (!COMPLETE_OR_VOID_TYPE_P (type))
  5096. {
  5097. if (complain & tf_error)
  5098. error (((code == PREINCREMENT_EXPR
  5099. || code == POSTINCREMENT_EXPR))
  5100. ? G_("cannot increment a pointer to incomplete type %qT")
  5101. : G_("cannot decrement a pointer to incomplete type %qT"),
  5102. TREE_TYPE (argtype));
  5103. else
  5104. return error_mark_node;
  5105. }
  5106. else if (!TYPE_PTROB_P (argtype))
  5107. {
  5108. if (complain & tf_error)
  5109. pedwarn (input_location, OPT_Wpointer_arith,
  5110. (code == PREINCREMENT_EXPR
  5111. || code == POSTINCREMENT_EXPR)
  5112. ? G_("ISO C++ forbids incrementing a pointer of type %qT")
  5113. : G_("ISO C++ forbids decrementing a pointer of type %qT"),
  5114. argtype);
  5115. else
  5116. return error_mark_node;
  5117. }
  5118. inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
  5119. }
  5120. else
  5121. inc = VECTOR_TYPE_P (argtype)
  5122. ? build_one_cst (argtype)
  5123. : integer_one_node;
  5124. inc = cp_convert (argtype, inc, complain);
  5125. /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
  5126. need to ask Objective-C to build the increment or decrement
  5127. expression for it. */
  5128. if (objc_is_property_ref (arg))
  5129. return objc_build_incr_expr_for_property_ref (input_location, code,
  5130. arg, inc);
  5131. /* Complain about anything else that is not a true lvalue. */
  5132. if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
  5133. || code == POSTINCREMENT_EXPR)
  5134. ? lv_increment : lv_decrement),
  5135. complain))
  5136. return error_mark_node;
  5137. /* Forbid using -- on `bool'. */
  5138. if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
  5139. {
  5140. if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
  5141. {
  5142. if (complain & tf_error)
  5143. error ("invalid use of Boolean expression as operand "
  5144. "to %<operator--%>");
  5145. return error_mark_node;
  5146. }
  5147. val = boolean_increment (code, arg);
  5148. }
  5149. else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
  5150. /* An rvalue has no cv-qualifiers. */
  5151. val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
  5152. else
  5153. val = build2 (code, TREE_TYPE (arg), arg, inc);
  5154. TREE_SIDE_EFFECTS (val) = 1;
  5155. return val;
  5156. }
  5157. case ADDR_EXPR:
  5158. /* Note that this operation never does default_conversion
  5159. regardless of NOCONVERT. */
  5160. return cp_build_addr_expr (arg, complain);
  5161. default:
  5162. break;
  5163. }
  5164. if (!errstring)
  5165. {
  5166. if (argtype == 0)
  5167. argtype = TREE_TYPE (arg);
  5168. return fold_if_not_in_template (build1 (code, argtype, arg));
  5169. }
  5170. if (complain & tf_error)
  5171. error ("%s", errstring);
  5172. return error_mark_node;
  5173. }
  5174. /* Hook for the c-common bits that build a unary op. */
  5175. tree
  5176. build_unary_op (location_t /*location*/,
  5177. enum tree_code code, tree xarg, int noconvert)
  5178. {
  5179. return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
  5180. }
  5181. /* Apply unary lvalue-demanding operator CODE to the expression ARG
  5182. for certain kinds of expressions which are not really lvalues
  5183. but which we can accept as lvalues.
  5184. If ARG is not a kind of expression we can handle, return
  5185. NULL_TREE. */
  5186. tree
  5187. unary_complex_lvalue (enum tree_code code, tree arg)
  5188. {
  5189. /* Inside a template, making these kinds of adjustments is
  5190. pointless; we are only concerned with the type of the
  5191. expression. */
  5192. if (processing_template_decl)
  5193. return NULL_TREE;
  5194. /* Handle (a, b) used as an "lvalue". */
  5195. if (TREE_CODE (arg) == COMPOUND_EXPR)
  5196. {
  5197. tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
  5198. tf_warning_or_error);
  5199. return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
  5200. TREE_OPERAND (arg, 0), real_result);
  5201. }
  5202. /* Handle (a ? b : c) used as an "lvalue". */
  5203. if (TREE_CODE (arg) == COND_EXPR
  5204. || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
  5205. return rationalize_conditional_expr (code, arg, tf_warning_or_error);
  5206. /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
  5207. if (TREE_CODE (arg) == MODIFY_EXPR
  5208. || TREE_CODE (arg) == PREINCREMENT_EXPR
  5209. || TREE_CODE (arg) == PREDECREMENT_EXPR)
  5210. {
  5211. tree lvalue = TREE_OPERAND (arg, 0);
  5212. if (TREE_SIDE_EFFECTS (lvalue))
  5213. {
  5214. lvalue = stabilize_reference (lvalue);
  5215. arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
  5216. lvalue, TREE_OPERAND (arg, 1));
  5217. }
  5218. return unary_complex_lvalue
  5219. (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
  5220. }
  5221. if (code != ADDR_EXPR)
  5222. return NULL_TREE;
  5223. /* Handle (a = b) used as an "lvalue" for `&'. */
  5224. if (TREE_CODE (arg) == MODIFY_EXPR
  5225. || TREE_CODE (arg) == INIT_EXPR)
  5226. {
  5227. tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
  5228. tf_warning_or_error);
  5229. arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
  5230. arg, real_result);
  5231. TREE_NO_WARNING (arg) = 1;
  5232. return arg;
  5233. }
  5234. if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
  5235. || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
  5236. || TREE_CODE (arg) == OFFSET_REF)
  5237. return NULL_TREE;
  5238. /* We permit compiler to make function calls returning
  5239. objects of aggregate type look like lvalues. */
  5240. {
  5241. tree targ = arg;
  5242. if (TREE_CODE (targ) == SAVE_EXPR)
  5243. targ = TREE_OPERAND (targ, 0);
  5244. if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
  5245. {
  5246. if (TREE_CODE (arg) == SAVE_EXPR)
  5247. targ = arg;
  5248. else
  5249. targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
  5250. return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
  5251. }
  5252. if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
  5253. return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
  5254. TREE_OPERAND (targ, 0), current_function_decl, NULL);
  5255. }
  5256. /* Don't let anything else be handled specially. */
  5257. return NULL_TREE;
  5258. }
  5259. /* Mark EXP saying that we need to be able to take the
  5260. address of it; it should not be allocated in a register.
  5261. Value is true if successful.
  5262. C++: we do not allow `current_class_ptr' to be addressable. */
  5263. bool
  5264. cxx_mark_addressable (tree exp)
  5265. {
  5266. tree x = exp;
  5267. while (1)
  5268. switch (TREE_CODE (x))
  5269. {
  5270. case ADDR_EXPR:
  5271. case COMPONENT_REF:
  5272. case ARRAY_REF:
  5273. case REALPART_EXPR:
  5274. case IMAGPART_EXPR:
  5275. x = TREE_OPERAND (x, 0);
  5276. break;
  5277. case PARM_DECL:
  5278. if (x == current_class_ptr)
  5279. {
  5280. error ("cannot take the address of %<this%>, which is an rvalue expression");
  5281. TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
  5282. return true;
  5283. }
  5284. /* Fall through. */
  5285. case VAR_DECL:
  5286. /* Caller should not be trying to mark initialized
  5287. constant fields addressable. */
  5288. gcc_assert (DECL_LANG_SPECIFIC (x) == 0
  5289. || DECL_IN_AGGR_P (x) == 0
  5290. || TREE_STATIC (x)
  5291. || DECL_EXTERNAL (x));
  5292. /* Fall through. */
  5293. case RESULT_DECL:
  5294. if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
  5295. && !DECL_ARTIFICIAL (x))
  5296. {
  5297. if (VAR_P (x) && DECL_HARD_REGISTER (x))
  5298. {
  5299. error
  5300. ("address of explicit register variable %qD requested", x);
  5301. return false;
  5302. }
  5303. else if (extra_warnings)
  5304. warning
  5305. (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
  5306. }
  5307. TREE_ADDRESSABLE (x) = 1;
  5308. return true;
  5309. case CONST_DECL:
  5310. case FUNCTION_DECL:
  5311. TREE_ADDRESSABLE (x) = 1;
  5312. return true;
  5313. case CONSTRUCTOR:
  5314. TREE_ADDRESSABLE (x) = 1;
  5315. return true;
  5316. case TARGET_EXPR:
  5317. TREE_ADDRESSABLE (x) = 1;
  5318. cxx_mark_addressable (TREE_OPERAND (x, 0));
  5319. return true;
  5320. default:
  5321. return true;
  5322. }
  5323. }
  5324. /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
  5325. tree
  5326. build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
  5327. tsubst_flags_t complain)
  5328. {
  5329. tree orig_ifexp = ifexp;
  5330. tree orig_op1 = op1;
  5331. tree orig_op2 = op2;
  5332. tree expr;
  5333. if (processing_template_decl)
  5334. {
  5335. /* The standard says that the expression is type-dependent if
  5336. IFEXP is type-dependent, even though the eventual type of the
  5337. expression doesn't dependent on IFEXP. */
  5338. if (type_dependent_expression_p (ifexp)
  5339. /* As a GNU extension, the middle operand may be omitted. */
  5340. || (op1 && type_dependent_expression_p (op1))
  5341. || type_dependent_expression_p (op2))
  5342. return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
  5343. ifexp = build_non_dependent_expr (ifexp);
  5344. if (op1)
  5345. op1 = build_non_dependent_expr (op1);
  5346. op2 = build_non_dependent_expr (op2);
  5347. }
  5348. expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
  5349. if (processing_template_decl && expr != error_mark_node
  5350. && TREE_CODE (expr) != VEC_COND_EXPR)
  5351. {
  5352. tree min = build_min_non_dep (COND_EXPR, expr,
  5353. orig_ifexp, orig_op1, orig_op2);
  5354. /* In C++11, remember that the result is an lvalue or xvalue.
  5355. In C++98, lvalue_kind can just assume lvalue in a template. */
  5356. if (cxx_dialect >= cxx11
  5357. && lvalue_or_rvalue_with_address_p (expr)
  5358. && !lvalue_or_rvalue_with_address_p (min))
  5359. TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
  5360. !real_lvalue_p (expr));
  5361. expr = convert_from_reference (min);
  5362. }
  5363. return expr;
  5364. }
  5365. /* Given a list of expressions, return a compound expression
  5366. that performs them all and returns the value of the last of them. */
  5367. tree
  5368. build_x_compound_expr_from_list (tree list, expr_list_kind exp,
  5369. tsubst_flags_t complain)
  5370. {
  5371. tree expr = TREE_VALUE (list);
  5372. if (BRACE_ENCLOSED_INITIALIZER_P (expr)
  5373. && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
  5374. {
  5375. if (complain & tf_error)
  5376. pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
  5377. "list-initializer for non-class type must not "
  5378. "be parenthesized");
  5379. else
  5380. return error_mark_node;
  5381. }
  5382. if (TREE_CHAIN (list))
  5383. {
  5384. if (complain & tf_error)
  5385. switch (exp)
  5386. {
  5387. case ELK_INIT:
  5388. permerror (input_location, "expression list treated as compound "
  5389. "expression in initializer");
  5390. break;
  5391. case ELK_MEM_INIT:
  5392. permerror (input_location, "expression list treated as compound "
  5393. "expression in mem-initializer");
  5394. break;
  5395. case ELK_FUNC_CAST:
  5396. permerror (input_location, "expression list treated as compound "
  5397. "expression in functional cast");
  5398. break;
  5399. default:
  5400. gcc_unreachable ();
  5401. }
  5402. else
  5403. return error_mark_node;
  5404. for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
  5405. expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
  5406. expr, TREE_VALUE (list), complain);
  5407. }
  5408. return expr;
  5409. }
  5410. /* Like build_x_compound_expr_from_list, but using a VEC. */
  5411. tree
  5412. build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
  5413. tsubst_flags_t complain)
  5414. {
  5415. if (vec_safe_is_empty (vec))
  5416. return NULL_TREE;
  5417. else if (vec->length () == 1)
  5418. return (*vec)[0];
  5419. else
  5420. {
  5421. tree expr;
  5422. unsigned int ix;
  5423. tree t;
  5424. if (msg != NULL)
  5425. {
  5426. if (complain & tf_error)
  5427. permerror (input_location,
  5428. "%s expression list treated as compound expression",
  5429. msg);
  5430. else
  5431. return error_mark_node;
  5432. }
  5433. expr = (*vec)[0];
  5434. for (ix = 1; vec->iterate (ix, &t); ++ix)
  5435. expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
  5436. t, complain);
  5437. return expr;
  5438. }
  5439. }
  5440. /* Handle overloading of the ',' operator when needed. */
  5441. tree
  5442. build_x_compound_expr (location_t loc, tree op1, tree op2,
  5443. tsubst_flags_t complain)
  5444. {
  5445. tree result;
  5446. tree orig_op1 = op1;
  5447. tree orig_op2 = op2;
  5448. if (processing_template_decl)
  5449. {
  5450. if (type_dependent_expression_p (op1)
  5451. || type_dependent_expression_p (op2))
  5452. return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
  5453. op1 = build_non_dependent_expr (op1);
  5454. op2 = build_non_dependent_expr (op2);
  5455. }
  5456. result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
  5457. NULL_TREE, /*overload=*/NULL, complain);
  5458. if (!result)
  5459. result = cp_build_compound_expr (op1, op2, complain);
  5460. if (processing_template_decl && result != error_mark_node)
  5461. return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
  5462. return result;
  5463. }
  5464. /* Like cp_build_compound_expr, but for the c-common bits. */
  5465. tree
  5466. build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
  5467. {
  5468. return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
  5469. }
  5470. /* Build a compound expression. */
  5471. tree
  5472. cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
  5473. {
  5474. lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
  5475. if (lhs == error_mark_node || rhs == error_mark_node)
  5476. return error_mark_node;
  5477. if (flag_cilkplus
  5478. && (TREE_CODE (lhs) == CILK_SPAWN_STMT
  5479. || TREE_CODE (rhs) == CILK_SPAWN_STMT))
  5480. {
  5481. location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
  5482. : EXPR_LOCATION (rhs));
  5483. error_at (loc,
  5484. "spawned function call cannot be part of a comma expression");
  5485. return error_mark_node;
  5486. }
  5487. if (TREE_CODE (rhs) == TARGET_EXPR)
  5488. {
  5489. /* If the rhs is a TARGET_EXPR, then build the compound
  5490. expression inside the target_expr's initializer. This
  5491. helps the compiler to eliminate unnecessary temporaries. */
  5492. tree init = TREE_OPERAND (rhs, 1);
  5493. init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
  5494. TREE_OPERAND (rhs, 1) = init;
  5495. return rhs;
  5496. }
  5497. if (type_unknown_p (rhs))
  5498. {
  5499. if (complain & tf_error)
  5500. error ("no context to resolve type of %qE", rhs);
  5501. return error_mark_node;
  5502. }
  5503. return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
  5504. }
  5505. /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
  5506. casts away constness. CAST gives the type of cast. Returns true
  5507. if the cast is ill-formed, false if it is well-formed.
  5508. ??? This function warns for casting away any qualifier not just
  5509. const. We would like to specify exactly what qualifiers are casted
  5510. away.
  5511. */
  5512. static bool
  5513. check_for_casting_away_constness (tree src_type, tree dest_type,
  5514. enum tree_code cast, tsubst_flags_t complain)
  5515. {
  5516. /* C-style casts are allowed to cast away constness. With
  5517. WARN_CAST_QUAL, we still want to issue a warning. */
  5518. if (cast == CAST_EXPR && !warn_cast_qual)
  5519. return false;
  5520. if (!casts_away_constness (src_type, dest_type, complain))
  5521. return false;
  5522. switch (cast)
  5523. {
  5524. case CAST_EXPR:
  5525. if (complain & tf_warning)
  5526. warning (OPT_Wcast_qual,
  5527. "cast from type %qT to type %qT casts away qualifiers",
  5528. src_type, dest_type);
  5529. return false;
  5530. case STATIC_CAST_EXPR:
  5531. if (complain & tf_error)
  5532. error ("static_cast from type %qT to type %qT casts away qualifiers",
  5533. src_type, dest_type);
  5534. return true;
  5535. case REINTERPRET_CAST_EXPR:
  5536. if (complain & tf_error)
  5537. error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
  5538. src_type, dest_type);
  5539. return true;
  5540. default:
  5541. gcc_unreachable();
  5542. }
  5543. }
  5544. /*
  5545. Warns if the cast from expression EXPR to type TYPE is useless.
  5546. */
  5547. void
  5548. maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
  5549. {
  5550. if (warn_useless_cast
  5551. && complain & tf_warning)
  5552. {
  5553. if ((TREE_CODE (type) == REFERENCE_TYPE
  5554. && (TYPE_REF_IS_RVALUE (type)
  5555. ? xvalue_p (expr) : real_lvalue_p (expr))
  5556. && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
  5557. || same_type_p (TREE_TYPE (expr), type))
  5558. warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
  5559. }
  5560. }
  5561. /* Convert EXPR (an expression with pointer-to-member type) to TYPE
  5562. (another pointer-to-member type in the same hierarchy) and return
  5563. the converted expression. If ALLOW_INVERSE_P is permitted, a
  5564. pointer-to-derived may be converted to pointer-to-base; otherwise,
  5565. only the other direction is permitted. If C_CAST_P is true, this
  5566. conversion is taking place as part of a C-style cast. */
  5567. tree
  5568. convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
  5569. bool c_cast_p, tsubst_flags_t complain)
  5570. {
  5571. if (TYPE_PTRDATAMEM_P (type))
  5572. {
  5573. tree delta;
  5574. if (TREE_CODE (expr) == PTRMEM_CST)
  5575. expr = cplus_expand_constant (expr);
  5576. delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
  5577. TYPE_PTRMEM_CLASS_TYPE (type),
  5578. allow_inverse_p,
  5579. c_cast_p, complain);
  5580. if (delta == error_mark_node)
  5581. return error_mark_node;
  5582. if (!integer_zerop (delta))
  5583. {
  5584. tree cond, op1, op2;
  5585. cond = cp_build_binary_op (input_location,
  5586. EQ_EXPR,
  5587. expr,
  5588. build_int_cst (TREE_TYPE (expr), -1),
  5589. complain);
  5590. op1 = build_nop (ptrdiff_type_node, expr);
  5591. op2 = cp_build_binary_op (input_location,
  5592. PLUS_EXPR, op1, delta,
  5593. complain);
  5594. expr = fold_build3_loc (input_location,
  5595. COND_EXPR, ptrdiff_type_node, cond, op1, op2);
  5596. }
  5597. return build_nop (type, expr);
  5598. }
  5599. else
  5600. return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
  5601. allow_inverse_p, c_cast_p, complain);
  5602. }
  5603. /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
  5604. this static_cast is being attempted as one of the possible casts
  5605. allowed by a C-style cast. (In that case, accessibility of base
  5606. classes is not considered, and it is OK to cast away
  5607. constness.) Return the result of the cast. *VALID_P is set to
  5608. indicate whether or not the cast was valid. */
  5609. static tree
  5610. build_static_cast_1 (tree type, tree expr, bool c_cast_p,
  5611. bool *valid_p, tsubst_flags_t complain)
  5612. {
  5613. tree intype;
  5614. tree result;
  5615. cp_lvalue_kind clk;
  5616. /* Assume the cast is valid. */
  5617. *valid_p = true;
  5618. intype = unlowered_expr_type (expr);
  5619. /* Save casted types in the function's used types hash table. */
  5620. used_types_insert (type);
  5621. /* [expr.static.cast]
  5622. An lvalue of type "cv1 B", where B is a class type, can be cast
  5623. to type "reference to cv2 D", where D is a class derived (clause
  5624. _class.derived_) from B, if a valid standard conversion from
  5625. "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
  5626. same cv-qualification as, or greater cv-qualification than, cv1,
  5627. and B is not a virtual base class of D. */
  5628. /* We check this case before checking the validity of "TYPE t =
  5629. EXPR;" below because for this case:
  5630. struct B {};
  5631. struct D : public B { D(const B&); };
  5632. extern B& b;
  5633. void f() { static_cast<const D&>(b); }
  5634. we want to avoid constructing a new D. The standard is not
  5635. completely clear about this issue, but our interpretation is
  5636. consistent with other compilers. */
  5637. if (TREE_CODE (type) == REFERENCE_TYPE
  5638. && CLASS_TYPE_P (TREE_TYPE (type))
  5639. && CLASS_TYPE_P (intype)
  5640. && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
  5641. && DERIVED_FROM_P (intype, TREE_TYPE (type))
  5642. && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
  5643. build_pointer_type (TYPE_MAIN_VARIANT
  5644. (TREE_TYPE (type))),
  5645. complain)
  5646. && (c_cast_p
  5647. || at_least_as_qualified_p (TREE_TYPE (type), intype)))
  5648. {
  5649. tree base;
  5650. /* There is a standard conversion from "D*" to "B*" even if "B"
  5651. is ambiguous or inaccessible. If this is really a
  5652. static_cast, then we check both for inaccessibility and
  5653. ambiguity. However, if this is a static_cast being performed
  5654. because the user wrote a C-style cast, then accessibility is
  5655. not considered. */
  5656. base = lookup_base (TREE_TYPE (type), intype,
  5657. c_cast_p ? ba_unique : ba_check,
  5658. NULL, complain);
  5659. expr = build_address (expr);
  5660. if (flag_sanitize & SANITIZE_VPTR)
  5661. {
  5662. tree ubsan_check
  5663. = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
  5664. if (ubsan_check)
  5665. expr = ubsan_check;
  5666. }
  5667. /* Convert from "B*" to "D*". This function will check that "B"
  5668. is not a virtual base of "D". */
  5669. expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
  5670. complain);
  5671. /* Convert the pointer to a reference -- but then remember that
  5672. there are no expressions with reference type in C++.
  5673. We call rvalue so that there's an actual tree code
  5674. (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
  5675. is a variable with the same type, the conversion would get folded
  5676. away, leaving just the variable and causing lvalue_kind to give
  5677. the wrong answer. */
  5678. return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
  5679. }
  5680. /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
  5681. cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
  5682. if (TREE_CODE (type) == REFERENCE_TYPE
  5683. && TYPE_REF_IS_RVALUE (type)
  5684. && (clk = real_lvalue_p (expr))
  5685. && reference_related_p (TREE_TYPE (type), intype)
  5686. && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
  5687. {
  5688. if (clk == clk_ordinary)
  5689. {
  5690. /* Handle the (non-bit-field) lvalue case here by casting to
  5691. lvalue reference and then changing it to an rvalue reference.
  5692. Casting an xvalue to rvalue reference will be handled by the
  5693. main code path. */
  5694. tree lref = cp_build_reference_type (TREE_TYPE (type), false);
  5695. result = (perform_direct_initialization_if_possible
  5696. (lref, expr, c_cast_p, complain));
  5697. result = cp_fold_convert (type, result);
  5698. /* Make sure we don't fold back down to a named rvalue reference,
  5699. because that would be an lvalue. */
  5700. if (DECL_P (result))
  5701. result = build1 (NON_LVALUE_EXPR, type, result);
  5702. return convert_from_reference (result);
  5703. }
  5704. else
  5705. /* For a bit-field or packed field, bind to a temporary. */
  5706. expr = rvalue (expr);
  5707. }
  5708. /* Resolve overloaded address here rather than once in
  5709. implicit_conversion and again in the inverse code below. */
  5710. if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
  5711. {
  5712. expr = instantiate_type (type, expr, complain);
  5713. intype = TREE_TYPE (expr);
  5714. }
  5715. /* [expr.static.cast]
  5716. Any expression can be explicitly converted to type cv void. */
  5717. if (VOID_TYPE_P (type))
  5718. return convert_to_void (expr, ICV_CAST, complain);
  5719. /* [class.abstract]
  5720. An abstract class shall not be used ... as the type of an explicit
  5721. conversion. */
  5722. if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
  5723. return error_mark_node;
  5724. /* [expr.static.cast]
  5725. An expression e can be explicitly converted to a type T using a
  5726. static_cast of the form static_cast<T>(e) if the declaration T
  5727. t(e);" is well-formed, for some invented temporary variable
  5728. t. */
  5729. result = perform_direct_initialization_if_possible (type, expr,
  5730. c_cast_p, complain);
  5731. if (result)
  5732. {
  5733. result = convert_from_reference (result);
  5734. /* [expr.static.cast]
  5735. If T is a reference type, the result is an lvalue; otherwise,
  5736. the result is an rvalue. */
  5737. if (TREE_CODE (type) != REFERENCE_TYPE)
  5738. result = rvalue (result);
  5739. return result;
  5740. }
  5741. /* [expr.static.cast]
  5742. The inverse of any standard conversion sequence (clause _conv_),
  5743. other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
  5744. (_conv.array_), function-to-pointer (_conv.func_), and boolean
  5745. (_conv.bool_) conversions, can be performed explicitly using
  5746. static_cast subject to the restriction that the explicit
  5747. conversion does not cast away constness (_expr.const.cast_), and
  5748. the following additional rules for specific cases: */
  5749. /* For reference, the conversions not excluded are: integral
  5750. promotions, floating point promotion, integral conversions,
  5751. floating point conversions, floating-integral conversions,
  5752. pointer conversions, and pointer to member conversions. */
  5753. /* DR 128
  5754. A value of integral _or enumeration_ type can be explicitly
  5755. converted to an enumeration type. */
  5756. /* The effect of all that is that any conversion between any two
  5757. types which are integral, floating, or enumeration types can be
  5758. performed. */
  5759. if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
  5760. || SCALAR_FLOAT_TYPE_P (type))
  5761. && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
  5762. || SCALAR_FLOAT_TYPE_P (intype)))
  5763. return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
  5764. if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
  5765. && CLASS_TYPE_P (TREE_TYPE (type))
  5766. && CLASS_TYPE_P (TREE_TYPE (intype))
  5767. && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
  5768. (TREE_TYPE (intype))),
  5769. build_pointer_type (TYPE_MAIN_VARIANT
  5770. (TREE_TYPE (type))),
  5771. complain))
  5772. {
  5773. tree base;
  5774. if (!c_cast_p
  5775. && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
  5776. complain))
  5777. return error_mark_node;
  5778. base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
  5779. c_cast_p ? ba_unique : ba_check,
  5780. NULL, complain);
  5781. expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
  5782. complain);
  5783. if (flag_sanitize & SANITIZE_VPTR)
  5784. {
  5785. tree ubsan_check
  5786. = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
  5787. if (ubsan_check)
  5788. expr = ubsan_check;
  5789. }
  5790. return cp_fold_convert (type, expr);
  5791. }
  5792. if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
  5793. || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
  5794. {
  5795. tree c1;
  5796. tree c2;
  5797. tree t1;
  5798. tree t2;
  5799. c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
  5800. c2 = TYPE_PTRMEM_CLASS_TYPE (type);
  5801. if (TYPE_PTRDATAMEM_P (type))
  5802. {
  5803. t1 = (build_ptrmem_type
  5804. (c1,
  5805. TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
  5806. t2 = (build_ptrmem_type
  5807. (c2,
  5808. TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
  5809. }
  5810. else
  5811. {
  5812. t1 = intype;
  5813. t2 = type;
  5814. }
  5815. if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
  5816. {
  5817. if (!c_cast_p
  5818. && check_for_casting_away_constness (intype, type,
  5819. STATIC_CAST_EXPR,
  5820. complain))
  5821. return error_mark_node;
  5822. return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
  5823. c_cast_p, complain);
  5824. }
  5825. }
  5826. /* [expr.static.cast]
  5827. An rvalue of type "pointer to cv void" can be explicitly
  5828. converted to a pointer to object type. A value of type pointer
  5829. to object converted to "pointer to cv void" and back to the
  5830. original pointer type will have its original value. */
  5831. if (TYPE_PTR_P (intype)
  5832. && VOID_TYPE_P (TREE_TYPE (intype))
  5833. && TYPE_PTROB_P (type))
  5834. {
  5835. if (!c_cast_p
  5836. && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
  5837. complain))
  5838. return error_mark_node;
  5839. return build_nop (type, expr);
  5840. }
  5841. *valid_p = false;
  5842. return error_mark_node;
  5843. }
  5844. /* Return an expression representing static_cast<TYPE>(EXPR). */
  5845. tree
  5846. build_static_cast (tree type, tree expr, tsubst_flags_t complain)
  5847. {
  5848. tree result;
  5849. bool valid_p;
  5850. if (type == error_mark_node || expr == error_mark_node)
  5851. return error_mark_node;
  5852. if (processing_template_decl)
  5853. {
  5854. expr = build_min (STATIC_CAST_EXPR, type, expr);
  5855. /* We don't know if it will or will not have side effects. */
  5856. TREE_SIDE_EFFECTS (expr) = 1;
  5857. return convert_from_reference (expr);
  5858. }
  5859. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  5860. Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
  5861. if (TREE_CODE (type) != REFERENCE_TYPE
  5862. && TREE_CODE (expr) == NOP_EXPR
  5863. && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
  5864. expr = TREE_OPERAND (expr, 0);
  5865. result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
  5866. complain);
  5867. if (valid_p)
  5868. {
  5869. if (result != error_mark_node)
  5870. maybe_warn_about_useless_cast (type, expr, complain);
  5871. return result;
  5872. }
  5873. if (complain & tf_error)
  5874. error ("invalid static_cast from type %qT to type %qT",
  5875. TREE_TYPE (expr), type);
  5876. return error_mark_node;
  5877. }
  5878. /* EXPR is an expression with member function or pointer-to-member
  5879. function type. TYPE is a pointer type. Converting EXPR to TYPE is
  5880. not permitted by ISO C++, but we accept it in some modes. If we
  5881. are not in one of those modes, issue a diagnostic. Return the
  5882. converted expression. */
  5883. tree
  5884. convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
  5885. {
  5886. tree intype;
  5887. tree decl;
  5888. intype = TREE_TYPE (expr);
  5889. gcc_assert (TYPE_PTRMEMFUNC_P (intype)
  5890. || TREE_CODE (intype) == METHOD_TYPE);
  5891. if (!(complain & tf_warning_or_error))
  5892. return error_mark_node;
  5893. if (pedantic || warn_pmf2ptr)
  5894. pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
  5895. "converting from %qT to %qT", intype, type);
  5896. if (TREE_CODE (intype) == METHOD_TYPE)
  5897. expr = build_addr_func (expr, complain);
  5898. else if (TREE_CODE (expr) == PTRMEM_CST)
  5899. expr = build_address (PTRMEM_CST_MEMBER (expr));
  5900. else
  5901. {
  5902. decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
  5903. decl = build_address (decl);
  5904. expr = get_member_function_from_ptrfunc (&decl, expr, complain);
  5905. }
  5906. if (expr == error_mark_node)
  5907. return error_mark_node;
  5908. return build_nop (type, expr);
  5909. }
  5910. /* Return a representation for a reinterpret_cast from EXPR to TYPE.
  5911. If C_CAST_P is true, this reinterpret cast is being done as part of
  5912. a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
  5913. indicate whether or not reinterpret_cast was valid. */
  5914. static tree
  5915. build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
  5916. bool *valid_p, tsubst_flags_t complain)
  5917. {
  5918. tree intype;
  5919. /* Assume the cast is invalid. */
  5920. if (valid_p)
  5921. *valid_p = true;
  5922. if (type == error_mark_node || error_operand_p (expr))
  5923. return error_mark_node;
  5924. intype = TREE_TYPE (expr);
  5925. /* Save casted types in the function's used types hash table. */
  5926. used_types_insert (type);
  5927. /* [expr.reinterpret.cast]
  5928. An lvalue expression of type T1 can be cast to the type
  5929. "reference to T2" if an expression of type "pointer to T1" can be
  5930. explicitly converted to the type "pointer to T2" using a
  5931. reinterpret_cast. */
  5932. if (TREE_CODE (type) == REFERENCE_TYPE)
  5933. {
  5934. if (! real_lvalue_p (expr))
  5935. {
  5936. if (complain & tf_error)
  5937. error ("invalid cast of an rvalue expression of type "
  5938. "%qT to type %qT",
  5939. intype, type);
  5940. return error_mark_node;
  5941. }
  5942. /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
  5943. "B" are related class types; the reinterpret_cast does not
  5944. adjust the pointer. */
  5945. if (TYPE_PTR_P (intype)
  5946. && (complain & tf_warning)
  5947. && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
  5948. COMPARE_BASE | COMPARE_DERIVED)))
  5949. warning (0, "casting %qT to %qT does not dereference pointer",
  5950. intype, type);
  5951. expr = cp_build_addr_expr (expr, complain);
  5952. if (warn_strict_aliasing > 2)
  5953. strict_aliasing_warning (TREE_TYPE (expr), type, expr);
  5954. if (expr != error_mark_node)
  5955. expr = build_reinterpret_cast_1
  5956. (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
  5957. valid_p, complain);
  5958. if (expr != error_mark_node)
  5959. /* cp_build_indirect_ref isn't right for rvalue refs. */
  5960. expr = convert_from_reference (fold_convert (type, expr));
  5961. return expr;
  5962. }
  5963. /* As a G++ extension, we consider conversions from member
  5964. functions, and pointers to member functions to
  5965. pointer-to-function and pointer-to-void types. If
  5966. -Wno-pmf-conversions has not been specified,
  5967. convert_member_func_to_ptr will issue an error message. */
  5968. if ((TYPE_PTRMEMFUNC_P (intype)
  5969. || TREE_CODE (intype) == METHOD_TYPE)
  5970. && TYPE_PTR_P (type)
  5971. && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
  5972. || VOID_TYPE_P (TREE_TYPE (type))))
  5973. return convert_member_func_to_ptr (type, expr, complain);
  5974. /* If the cast is not to a reference type, the lvalue-to-rvalue,
  5975. array-to-pointer, and function-to-pointer conversions are
  5976. performed. */
  5977. expr = decay_conversion (expr, complain);
  5978. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  5979. Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
  5980. if (TREE_CODE (expr) == NOP_EXPR
  5981. && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
  5982. expr = TREE_OPERAND (expr, 0);
  5983. if (error_operand_p (expr))
  5984. return error_mark_node;
  5985. intype = TREE_TYPE (expr);
  5986. /* [expr.reinterpret.cast]
  5987. A pointer can be converted to any integral type large enough to
  5988. hold it. ... A value of type std::nullptr_t can be converted to
  5989. an integral type; the conversion has the same meaning and
  5990. validity as a conversion of (void*)0 to the integral type. */
  5991. if (CP_INTEGRAL_TYPE_P (type)
  5992. && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
  5993. {
  5994. if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
  5995. {
  5996. if (complain & tf_error)
  5997. permerror (input_location, "cast from %qT to %qT loses precision",
  5998. intype, type);
  5999. else
  6000. return error_mark_node;
  6001. }
  6002. if (NULLPTR_TYPE_P (intype))
  6003. return build_int_cst (type, 0);
  6004. }
  6005. /* [expr.reinterpret.cast]
  6006. A value of integral or enumeration type can be explicitly
  6007. converted to a pointer. */
  6008. else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
  6009. /* OK */
  6010. ;
  6011. else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
  6012. || TYPE_PTR_OR_PTRMEM_P (type))
  6013. && same_type_p (type, intype))
  6014. /* DR 799 */
  6015. return rvalue (expr);
  6016. else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
  6017. || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
  6018. return fold_if_not_in_template (build_nop (type, expr));
  6019. else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
  6020. || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
  6021. {
  6022. tree sexpr = expr;
  6023. if (!c_cast_p
  6024. && check_for_casting_away_constness (intype, type,
  6025. REINTERPRET_CAST_EXPR,
  6026. complain))
  6027. return error_mark_node;
  6028. /* Warn about possible alignment problems. */
  6029. if (STRICT_ALIGNMENT && warn_cast_align
  6030. && (complain & tf_warning)
  6031. && !VOID_TYPE_P (type)
  6032. && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
  6033. && COMPLETE_TYPE_P (TREE_TYPE (type))
  6034. && COMPLETE_TYPE_P (TREE_TYPE (intype))
  6035. && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
  6036. warning (OPT_Wcast_align, "cast from %qT to %qT "
  6037. "increases required alignment of target type", intype, type);
  6038. /* We need to strip nops here, because the front end likes to
  6039. create (int *)&a for array-to-pointer decay, instead of &a[0]. */
  6040. STRIP_NOPS (sexpr);
  6041. if (warn_strict_aliasing <= 2)
  6042. strict_aliasing_warning (intype, type, sexpr);
  6043. return fold_if_not_in_template (build_nop (type, expr));
  6044. }
  6045. else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
  6046. || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
  6047. {
  6048. if (complain & tf_warning)
  6049. /* C++11 5.2.10 p8 says that "Converting a function pointer to an
  6050. object pointer type or vice versa is conditionally-supported." */
  6051. warning (OPT_Wconditionally_supported,
  6052. "casting between pointer-to-function and pointer-to-object "
  6053. "is conditionally-supported");
  6054. return fold_if_not_in_template (build_nop (type, expr));
  6055. }
  6056. else if (TREE_CODE (type) == VECTOR_TYPE)
  6057. return fold_if_not_in_template (convert_to_vector (type, expr));
  6058. else if (TREE_CODE (intype) == VECTOR_TYPE
  6059. && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  6060. return fold_if_not_in_template (convert_to_integer (type, expr));
  6061. else
  6062. {
  6063. if (valid_p)
  6064. *valid_p = false;
  6065. if (complain & tf_error)
  6066. error ("invalid cast from type %qT to type %qT", intype, type);
  6067. return error_mark_node;
  6068. }
  6069. return cp_convert (type, expr, complain);
  6070. }
  6071. tree
  6072. build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
  6073. {
  6074. tree r;
  6075. if (type == error_mark_node || expr == error_mark_node)
  6076. return error_mark_node;
  6077. if (processing_template_decl)
  6078. {
  6079. tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
  6080. if (!TREE_SIDE_EFFECTS (t)
  6081. && type_dependent_expression_p (expr))
  6082. /* There might turn out to be side effects inside expr. */
  6083. TREE_SIDE_EFFECTS (t) = 1;
  6084. return convert_from_reference (t);
  6085. }
  6086. r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
  6087. /*valid_p=*/NULL, complain);
  6088. if (r != error_mark_node)
  6089. maybe_warn_about_useless_cast (type, expr, complain);
  6090. return r;
  6091. }
  6092. /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
  6093. return an appropriate expression. Otherwise, return
  6094. error_mark_node. If the cast is not valid, and COMPLAIN is true,
  6095. then a diagnostic will be issued. If VALID_P is non-NULL, we are
  6096. performing a C-style cast, its value upon return will indicate
  6097. whether or not the conversion succeeded. */
  6098. static tree
  6099. build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
  6100. bool *valid_p)
  6101. {
  6102. tree src_type;
  6103. tree reference_type;
  6104. /* Callers are responsible for handling error_mark_node as a
  6105. destination type. */
  6106. gcc_assert (dst_type != error_mark_node);
  6107. /* In a template, callers should be building syntactic
  6108. representations of casts, not using this machinery. */
  6109. gcc_assert (!processing_template_decl);
  6110. /* Assume the conversion is invalid. */
  6111. if (valid_p)
  6112. *valid_p = false;
  6113. if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
  6114. {
  6115. if (complain & tf_error)
  6116. error ("invalid use of const_cast with type %qT, "
  6117. "which is not a pointer, "
  6118. "reference, nor a pointer-to-data-member type", dst_type);
  6119. return error_mark_node;
  6120. }
  6121. if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
  6122. {
  6123. if (complain & tf_error)
  6124. error ("invalid use of const_cast with type %qT, which is a pointer "
  6125. "or reference to a function type", dst_type);
  6126. return error_mark_node;
  6127. }
  6128. /* Save casted types in the function's used types hash table. */
  6129. used_types_insert (dst_type);
  6130. src_type = TREE_TYPE (expr);
  6131. /* Expressions do not really have reference types. */
  6132. if (TREE_CODE (src_type) == REFERENCE_TYPE)
  6133. src_type = TREE_TYPE (src_type);
  6134. /* [expr.const.cast]
  6135. For two object types T1 and T2, if a pointer to T1 can be explicitly
  6136. converted to the type "pointer to T2" using a const_cast, then the
  6137. following conversions can also be made:
  6138. -- an lvalue of type T1 can be explicitly converted to an lvalue of
  6139. type T2 using the cast const_cast<T2&>;
  6140. -- a glvalue of type T1 can be explicitly converted to an xvalue of
  6141. type T2 using the cast const_cast<T2&&>; and
  6142. -- if T1 is a class type, a prvalue of type T1 can be explicitly
  6143. converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
  6144. if (TREE_CODE (dst_type) == REFERENCE_TYPE)
  6145. {
  6146. reference_type = dst_type;
  6147. if (!TYPE_REF_IS_RVALUE (dst_type)
  6148. ? real_lvalue_p (expr)
  6149. : (CLASS_TYPE_P (TREE_TYPE (dst_type))
  6150. ? lvalue_p (expr)
  6151. : lvalue_or_rvalue_with_address_p (expr)))
  6152. /* OK. */;
  6153. else
  6154. {
  6155. if (complain & tf_error)
  6156. error ("invalid const_cast of an rvalue of type %qT to type %qT",
  6157. src_type, dst_type);
  6158. return error_mark_node;
  6159. }
  6160. dst_type = build_pointer_type (TREE_TYPE (dst_type));
  6161. src_type = build_pointer_type (src_type);
  6162. }
  6163. else
  6164. {
  6165. reference_type = NULL_TREE;
  6166. /* If the destination type is not a reference type, the
  6167. lvalue-to-rvalue, array-to-pointer, and function-to-pointer
  6168. conversions are performed. */
  6169. src_type = type_decays_to (src_type);
  6170. if (src_type == error_mark_node)
  6171. return error_mark_node;
  6172. }
  6173. if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
  6174. {
  6175. if (comp_ptr_ttypes_const (dst_type, src_type))
  6176. {
  6177. if (valid_p)
  6178. {
  6179. *valid_p = true;
  6180. /* This cast is actually a C-style cast. Issue a warning if
  6181. the user is making a potentially unsafe cast. */
  6182. check_for_casting_away_constness (src_type, dst_type,
  6183. CAST_EXPR, complain);
  6184. }
  6185. if (reference_type)
  6186. {
  6187. expr = cp_build_addr_expr (expr, complain);
  6188. if (expr == error_mark_node)
  6189. return error_mark_node;
  6190. expr = build_nop (reference_type, expr);
  6191. return convert_from_reference (expr);
  6192. }
  6193. else
  6194. {
  6195. expr = decay_conversion (expr, complain);
  6196. if (expr == error_mark_node)
  6197. return error_mark_node;
  6198. /* build_c_cast puts on a NOP_EXPR to make the result not an
  6199. lvalue. Strip such NOP_EXPRs if VALUE is being used in
  6200. non-lvalue context. */
  6201. if (TREE_CODE (expr) == NOP_EXPR
  6202. && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
  6203. expr = TREE_OPERAND (expr, 0);
  6204. return build_nop (dst_type, expr);
  6205. }
  6206. }
  6207. else if (valid_p
  6208. && !at_least_as_qualified_p (TREE_TYPE (dst_type),
  6209. TREE_TYPE (src_type)))
  6210. check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
  6211. complain);
  6212. }
  6213. if (complain & tf_error)
  6214. error ("invalid const_cast from type %qT to type %qT",
  6215. src_type, dst_type);
  6216. return error_mark_node;
  6217. }
  6218. tree
  6219. build_const_cast (tree type, tree expr, tsubst_flags_t complain)
  6220. {
  6221. tree r;
  6222. if (type == error_mark_node || error_operand_p (expr))
  6223. return error_mark_node;
  6224. if (processing_template_decl)
  6225. {
  6226. tree t = build_min (CONST_CAST_EXPR, type, expr);
  6227. if (!TREE_SIDE_EFFECTS (t)
  6228. && type_dependent_expression_p (expr))
  6229. /* There might turn out to be side effects inside expr. */
  6230. TREE_SIDE_EFFECTS (t) = 1;
  6231. return convert_from_reference (t);
  6232. }
  6233. r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
  6234. if (r != error_mark_node)
  6235. maybe_warn_about_useless_cast (type, expr, complain);
  6236. return r;
  6237. }
  6238. /* Like cp_build_c_cast, but for the c-common bits. */
  6239. tree
  6240. build_c_cast (location_t /*loc*/, tree type, tree expr)
  6241. {
  6242. return cp_build_c_cast (type, expr, tf_warning_or_error);
  6243. }
  6244. /* Build an expression representing an explicit C-style cast to type
  6245. TYPE of expression EXPR. */
  6246. tree
  6247. cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
  6248. {
  6249. tree value = expr;
  6250. tree result;
  6251. bool valid_p;
  6252. if (type == error_mark_node || error_operand_p (expr))
  6253. return error_mark_node;
  6254. if (processing_template_decl)
  6255. {
  6256. tree t = build_min (CAST_EXPR, type,
  6257. tree_cons (NULL_TREE, value, NULL_TREE));
  6258. /* We don't know if it will or will not have side effects. */
  6259. TREE_SIDE_EFFECTS (t) = 1;
  6260. return convert_from_reference (t);
  6261. }
  6262. /* Casts to a (pointer to a) specific ObjC class (or 'id' or
  6263. 'Class') should always be retained, because this information aids
  6264. in method lookup. */
  6265. if (objc_is_object_ptr (type)
  6266. && objc_is_object_ptr (TREE_TYPE (expr)))
  6267. return build_nop (type, expr);
  6268. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  6269. Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
  6270. if (TREE_CODE (type) != REFERENCE_TYPE
  6271. && TREE_CODE (value) == NOP_EXPR
  6272. && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
  6273. value = TREE_OPERAND (value, 0);
  6274. if (TREE_CODE (type) == ARRAY_TYPE)
  6275. {
  6276. /* Allow casting from T1* to T2[] because Cfront allows it.
  6277. NIHCL uses it. It is not valid ISO C++ however. */
  6278. if (TYPE_PTR_P (TREE_TYPE (expr)))
  6279. {
  6280. if (complain & tf_error)
  6281. permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
  6282. else
  6283. return error_mark_node;
  6284. type = build_pointer_type (TREE_TYPE (type));
  6285. }
  6286. else
  6287. {
  6288. if (complain & tf_error)
  6289. error ("ISO C++ forbids casting to an array type %qT", type);
  6290. return error_mark_node;
  6291. }
  6292. }
  6293. if (TREE_CODE (type) == FUNCTION_TYPE
  6294. || TREE_CODE (type) == METHOD_TYPE)
  6295. {
  6296. if (complain & tf_error)
  6297. error ("invalid cast to function type %qT", type);
  6298. return error_mark_node;
  6299. }
  6300. if (TYPE_PTR_P (type)
  6301. && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
  6302. /* Casting to an integer of smaller size is an error detected elsewhere. */
  6303. && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
  6304. /* Don't warn about converting any constant. */
  6305. && !TREE_CONSTANT (value))
  6306. warning_at (input_location, OPT_Wint_to_pointer_cast,
  6307. "cast to pointer from integer of different size");
  6308. /* A C-style cast can be a const_cast. */
  6309. result = build_const_cast_1 (type, value, complain & tf_warning,
  6310. &valid_p);
  6311. if (valid_p)
  6312. {
  6313. if (result != error_mark_node)
  6314. maybe_warn_about_useless_cast (type, value, complain);
  6315. return result;
  6316. }
  6317. /* Or a static cast. */
  6318. result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
  6319. &valid_p, complain);
  6320. /* Or a reinterpret_cast. */
  6321. if (!valid_p)
  6322. result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
  6323. &valid_p, complain);
  6324. /* The static_cast or reinterpret_cast may be followed by a
  6325. const_cast. */
  6326. if (valid_p
  6327. /* A valid cast may result in errors if, for example, a
  6328. conversion to an ambiguous base class is required. */
  6329. && !error_operand_p (result))
  6330. {
  6331. tree result_type;
  6332. maybe_warn_about_useless_cast (type, value, complain);
  6333. /* Non-class rvalues always have cv-unqualified type. */
  6334. if (!CLASS_TYPE_P (type))
  6335. type = TYPE_MAIN_VARIANT (type);
  6336. result_type = TREE_TYPE (result);
  6337. if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
  6338. result_type = TYPE_MAIN_VARIANT (result_type);
  6339. /* If the type of RESULT does not match TYPE, perform a
  6340. const_cast to make it match. If the static_cast or
  6341. reinterpret_cast succeeded, we will differ by at most
  6342. cv-qualification, so the follow-on const_cast is guaranteed
  6343. to succeed. */
  6344. if (!same_type_p (non_reference (type), non_reference (result_type)))
  6345. {
  6346. result = build_const_cast_1 (type, result, false, &valid_p);
  6347. gcc_assert (valid_p);
  6348. }
  6349. return result;
  6350. }
  6351. return error_mark_node;
  6352. }
  6353. /* For use from the C common bits. */
  6354. tree
  6355. build_modify_expr (location_t /*location*/,
  6356. tree lhs, tree /*lhs_origtype*/,
  6357. enum tree_code modifycode,
  6358. location_t /*rhs_location*/, tree rhs,
  6359. tree /*rhs_origtype*/)
  6360. {
  6361. return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
  6362. }
  6363. /* Build an assignment expression of lvalue LHS from value RHS.
  6364. MODIFYCODE is the code for a binary operator that we use
  6365. to combine the old value of LHS with RHS to get the new value.
  6366. Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
  6367. C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
  6368. tree
  6369. cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
  6370. tsubst_flags_t complain)
  6371. {
  6372. tree result;
  6373. tree newrhs = rhs;
  6374. tree lhstype = TREE_TYPE (lhs);
  6375. tree olhstype = lhstype;
  6376. bool plain_assign = (modifycode == NOP_EXPR);
  6377. /* Avoid duplicate error messages from operands that had errors. */
  6378. if (error_operand_p (lhs) || error_operand_p (rhs))
  6379. return error_mark_node;
  6380. /* Handle control structure constructs used as "lvalues". */
  6381. switch (TREE_CODE (lhs))
  6382. {
  6383. /* Handle --foo = 5; as these are valid constructs in C++. */
  6384. case PREDECREMENT_EXPR:
  6385. case PREINCREMENT_EXPR:
  6386. if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
  6387. lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
  6388. stabilize_reference (TREE_OPERAND (lhs, 0)),
  6389. TREE_OPERAND (lhs, 1));
  6390. newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
  6391. modifycode, rhs, complain);
  6392. if (newrhs == error_mark_node)
  6393. return error_mark_node;
  6394. return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
  6395. /* Handle (a, b) used as an "lvalue". */
  6396. case COMPOUND_EXPR:
  6397. newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
  6398. modifycode, rhs, complain);
  6399. if (newrhs == error_mark_node)
  6400. return error_mark_node;
  6401. return build2 (COMPOUND_EXPR, lhstype,
  6402. TREE_OPERAND (lhs, 0), newrhs);
  6403. case MODIFY_EXPR:
  6404. if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
  6405. lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
  6406. stabilize_reference (TREE_OPERAND (lhs, 0)),
  6407. TREE_OPERAND (lhs, 1));
  6408. newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
  6409. complain);
  6410. if (newrhs == error_mark_node)
  6411. return error_mark_node;
  6412. return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
  6413. case MIN_EXPR:
  6414. case MAX_EXPR:
  6415. /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
  6416. when neither operand has side-effects. */
  6417. if (!lvalue_or_else (lhs, lv_assign, complain))
  6418. return error_mark_node;
  6419. gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
  6420. && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
  6421. lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
  6422. build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
  6423. boolean_type_node,
  6424. TREE_OPERAND (lhs, 0),
  6425. TREE_OPERAND (lhs, 1)),
  6426. TREE_OPERAND (lhs, 0),
  6427. TREE_OPERAND (lhs, 1));
  6428. /* Fall through. */
  6429. /* Handle (a ? b : c) used as an "lvalue". */
  6430. case COND_EXPR:
  6431. {
  6432. /* Produce (a ? (b = rhs) : (c = rhs))
  6433. except that the RHS goes through a save-expr
  6434. so the code to compute it is only emitted once. */
  6435. tree cond;
  6436. tree preeval = NULL_TREE;
  6437. if (VOID_TYPE_P (TREE_TYPE (rhs)))
  6438. {
  6439. if (complain & tf_error)
  6440. error ("void value not ignored as it ought to be");
  6441. return error_mark_node;
  6442. }
  6443. rhs = stabilize_expr (rhs, &preeval);
  6444. /* Check this here to avoid odd errors when trying to convert
  6445. a throw to the type of the COND_EXPR. */
  6446. if (!lvalue_or_else (lhs, lv_assign, complain))
  6447. return error_mark_node;
  6448. cond = build_conditional_expr
  6449. (input_location, TREE_OPERAND (lhs, 0),
  6450. cp_build_modify_expr (TREE_OPERAND (lhs, 1),
  6451. modifycode, rhs, complain),
  6452. cp_build_modify_expr (TREE_OPERAND (lhs, 2),
  6453. modifycode, rhs, complain),
  6454. complain);
  6455. if (cond == error_mark_node)
  6456. return cond;
  6457. /* Make sure the code to compute the rhs comes out
  6458. before the split. */
  6459. if (preeval)
  6460. cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
  6461. return cond;
  6462. }
  6463. default:
  6464. break;
  6465. }
  6466. if (modifycode == INIT_EXPR)
  6467. {
  6468. if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
  6469. /* Do the default thing. */;
  6470. else if (TREE_CODE (rhs) == CONSTRUCTOR)
  6471. {
  6472. /* Compound literal. */
  6473. if (! same_type_p (TREE_TYPE (rhs), lhstype))
  6474. /* Call convert to generate an error; see PR 11063. */
  6475. rhs = convert (lhstype, rhs);
  6476. result = build2 (INIT_EXPR, lhstype, lhs, rhs);
  6477. TREE_SIDE_EFFECTS (result) = 1;
  6478. return result;
  6479. }
  6480. else if (! MAYBE_CLASS_TYPE_P (lhstype))
  6481. /* Do the default thing. */;
  6482. else
  6483. {
  6484. vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
  6485. result = build_special_member_call (lhs, complete_ctor_identifier,
  6486. &rhs_vec, lhstype, LOOKUP_NORMAL,
  6487. complain);
  6488. release_tree_vector (rhs_vec);
  6489. if (result == NULL_TREE)
  6490. return error_mark_node;
  6491. return result;
  6492. }
  6493. }
  6494. else
  6495. {
  6496. lhs = require_complete_type_sfinae (lhs, complain);
  6497. if (lhs == error_mark_node)
  6498. return error_mark_node;
  6499. if (modifycode == NOP_EXPR)
  6500. {
  6501. if (c_dialect_objc ())
  6502. {
  6503. result = objc_maybe_build_modify_expr (lhs, rhs);
  6504. if (result)
  6505. return result;
  6506. }
  6507. /* `operator=' is not an inheritable operator. */
  6508. if (! MAYBE_CLASS_TYPE_P (lhstype))
  6509. /* Do the default thing. */;
  6510. else
  6511. {
  6512. result = build_new_op (input_location, MODIFY_EXPR,
  6513. LOOKUP_NORMAL, lhs, rhs,
  6514. make_node (NOP_EXPR), /*overload=*/NULL,
  6515. complain);
  6516. if (result == NULL_TREE)
  6517. return error_mark_node;
  6518. return result;
  6519. }
  6520. lhstype = olhstype;
  6521. }
  6522. else
  6523. {
  6524. tree init = NULL_TREE;
  6525. /* A binary op has been requested. Combine the old LHS
  6526. value with the RHS producing the value we should actually
  6527. store into the LHS. */
  6528. gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
  6529. && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
  6530. || MAYBE_CLASS_TYPE_P (lhstype)));
  6531. /* Preevaluate the RHS to make sure its evaluation is complete
  6532. before the lvalue-to-rvalue conversion of the LHS:
  6533. [expr.ass] With respect to an indeterminately-sequenced
  6534. function call, the operation of a compound assignment is a
  6535. single evaluation. [ Note: Therefore, a function call shall
  6536. not intervene between the lvalue-to-rvalue conversion and the
  6537. side effect associated with any single compound assignment
  6538. operator. -- end note ] */
  6539. lhs = stabilize_reference (lhs);
  6540. rhs = rvalue (rhs);
  6541. rhs = stabilize_expr (rhs, &init);
  6542. newrhs = cp_build_binary_op (input_location,
  6543. modifycode, lhs, rhs,
  6544. complain);
  6545. if (newrhs == error_mark_node)
  6546. {
  6547. if (complain & tf_error)
  6548. error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
  6549. TREE_TYPE (lhs), TREE_TYPE (rhs));
  6550. return error_mark_node;
  6551. }
  6552. if (init)
  6553. newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
  6554. /* Now it looks like a plain assignment. */
  6555. modifycode = NOP_EXPR;
  6556. if (c_dialect_objc ())
  6557. {
  6558. result = objc_maybe_build_modify_expr (lhs, newrhs);
  6559. if (result)
  6560. return result;
  6561. }
  6562. }
  6563. gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
  6564. gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
  6565. }
  6566. /* The left-hand side must be an lvalue. */
  6567. if (!lvalue_or_else (lhs, lv_assign, complain))
  6568. return error_mark_node;
  6569. /* Warn about modifying something that is `const'. Don't warn if
  6570. this is initialization. */
  6571. if (modifycode != INIT_EXPR
  6572. && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
  6573. /* Functions are not modifiable, even though they are
  6574. lvalues. */
  6575. || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
  6576. || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
  6577. /* If it's an aggregate and any field is const, then it is
  6578. effectively const. */
  6579. || (CLASS_TYPE_P (lhstype)
  6580. && C_TYPE_FIELDS_READONLY (lhstype))))
  6581. {
  6582. if (complain & tf_error)
  6583. cxx_readonly_error (lhs, lv_assign);
  6584. else
  6585. return error_mark_node;
  6586. }
  6587. /* If storing into a structure or union member, it may have been given a
  6588. lowered bitfield type. We need to convert to the declared type first,
  6589. so retrieve it now. */
  6590. olhstype = unlowered_expr_type (lhs);
  6591. /* Convert new value to destination type. */
  6592. if (TREE_CODE (lhstype) == ARRAY_TYPE)
  6593. {
  6594. int from_array;
  6595. if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
  6596. {
  6597. if (modifycode != INIT_EXPR)
  6598. {
  6599. if (complain & tf_error)
  6600. error ("assigning to an array from an initializer list");
  6601. return error_mark_node;
  6602. }
  6603. if (check_array_initializer (lhs, lhstype, newrhs))
  6604. return error_mark_node;
  6605. newrhs = digest_init (lhstype, newrhs, complain);
  6606. if (newrhs == error_mark_node)
  6607. return error_mark_node;
  6608. }
  6609. /* C++11 8.5/17: "If the destination type is an array of characters,
  6610. an array of char16_t, an array of char32_t, or an array of wchar_t,
  6611. and the initializer is a string literal...". */
  6612. else if (TREE_CODE (newrhs) == STRING_CST
  6613. && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
  6614. && modifycode == INIT_EXPR)
  6615. {
  6616. newrhs = digest_init (lhstype, newrhs, complain);
  6617. if (newrhs == error_mark_node)
  6618. return error_mark_node;
  6619. }
  6620. else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
  6621. TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
  6622. {
  6623. if (complain & tf_error)
  6624. error ("incompatible types in assignment of %qT to %qT",
  6625. TREE_TYPE (rhs), lhstype);
  6626. return error_mark_node;
  6627. }
  6628. /* Allow array assignment in compiler-generated code. */
  6629. else if (!current_function_decl
  6630. || !DECL_DEFAULTED_FN (current_function_decl))
  6631. {
  6632. /* This routine is used for both initialization and assignment.
  6633. Make sure the diagnostic message differentiates the context. */
  6634. if (complain & tf_error)
  6635. {
  6636. if (modifycode == INIT_EXPR)
  6637. error ("array used as initializer");
  6638. else
  6639. error ("invalid array assignment");
  6640. }
  6641. return error_mark_node;
  6642. }
  6643. from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
  6644. ? 1 + (modifycode != INIT_EXPR): 0;
  6645. return build_vec_init (lhs, NULL_TREE, newrhs,
  6646. /*explicit_value_init_p=*/false,
  6647. from_array, complain);
  6648. }
  6649. if (modifycode == INIT_EXPR)
  6650. /* Calls with INIT_EXPR are all direct-initialization, so don't set
  6651. LOOKUP_ONLYCONVERTING. */
  6652. newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
  6653. ICR_INIT, NULL_TREE, 0,
  6654. complain);
  6655. else
  6656. newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
  6657. NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
  6658. if (!same_type_p (lhstype, olhstype))
  6659. newrhs = cp_convert_and_check (lhstype, newrhs, complain);
  6660. if (modifycode != INIT_EXPR)
  6661. {
  6662. if (TREE_CODE (newrhs) == CALL_EXPR
  6663. && TYPE_NEEDS_CONSTRUCTING (lhstype))
  6664. newrhs = build_cplus_new (lhstype, newrhs, complain);
  6665. /* Can't initialize directly from a TARGET_EXPR, since that would
  6666. cause the lhs to be constructed twice, and possibly result in
  6667. accidental self-initialization. So we force the TARGET_EXPR to be
  6668. expanded without a target. */
  6669. if (TREE_CODE (newrhs) == TARGET_EXPR)
  6670. newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
  6671. TREE_OPERAND (newrhs, 0));
  6672. }
  6673. if (newrhs == error_mark_node)
  6674. return error_mark_node;
  6675. if (c_dialect_objc () && flag_objc_gc)
  6676. {
  6677. result = objc_generate_write_barrier (lhs, modifycode, newrhs);
  6678. if (result)
  6679. return result;
  6680. }
  6681. result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
  6682. lhstype, lhs, newrhs);
  6683. TREE_SIDE_EFFECTS (result) = 1;
  6684. if (!plain_assign)
  6685. TREE_NO_WARNING (result) = 1;
  6686. return result;
  6687. }
  6688. tree
  6689. build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
  6690. tree rhs, tsubst_flags_t complain)
  6691. {
  6692. if (processing_template_decl)
  6693. return build_min_nt_loc (loc, MODOP_EXPR, lhs,
  6694. build_min_nt_loc (loc, modifycode, NULL_TREE,
  6695. NULL_TREE), rhs);
  6696. if (modifycode != NOP_EXPR)
  6697. {
  6698. tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
  6699. make_node (modifycode), /*overload=*/NULL,
  6700. complain);
  6701. if (rval)
  6702. {
  6703. TREE_NO_WARNING (rval) = 1;
  6704. return rval;
  6705. }
  6706. }
  6707. return cp_build_modify_expr (lhs, modifycode, rhs, complain);
  6708. }
  6709. /* Helper function for get_delta_difference which assumes FROM is a base
  6710. class of TO. Returns a delta for the conversion of pointer-to-member
  6711. of FROM to pointer-to-member of TO. If the conversion is invalid and
  6712. tf_error is not set in COMPLAIN returns error_mark_node, otherwise
  6713. returns zero. If FROM is not a base class of TO, returns NULL_TREE.
  6714. If C_CAST_P is true, this conversion is taking place as part of a
  6715. C-style cast. */
  6716. static tree
  6717. get_delta_difference_1 (tree from, tree to, bool c_cast_p,
  6718. tsubst_flags_t complain)
  6719. {
  6720. tree binfo;
  6721. base_kind kind;
  6722. binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
  6723. &kind, complain);
  6724. if (binfo == error_mark_node)
  6725. {
  6726. if (!(complain & tf_error))
  6727. return error_mark_node;
  6728. error (" in pointer to member function conversion");
  6729. return size_zero_node;
  6730. }
  6731. else if (binfo)
  6732. {
  6733. if (kind != bk_via_virtual)
  6734. return BINFO_OFFSET (binfo);
  6735. else
  6736. /* FROM is a virtual base class of TO. Issue an error or warning
  6737. depending on whether or not this is a reinterpret cast. */
  6738. {
  6739. if (!(complain & tf_error))
  6740. return error_mark_node;
  6741. error ("pointer to member conversion via virtual base %qT",
  6742. BINFO_TYPE (binfo_from_vbase (binfo)));
  6743. return size_zero_node;
  6744. }
  6745. }
  6746. else
  6747. return NULL_TREE;
  6748. }
  6749. /* Get difference in deltas for different pointer to member function
  6750. types. If the conversion is invalid and tf_error is not set in
  6751. COMPLAIN, returns error_mark_node, otherwise returns an integer
  6752. constant of type PTRDIFF_TYPE_NODE and its value is zero if the
  6753. conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
  6754. conversions as well. If C_CAST_P is true this conversion is taking
  6755. place as part of a C-style cast.
  6756. Note that the naming of FROM and TO is kind of backwards; the return
  6757. value is what we add to a TO in order to get a FROM. They are named
  6758. this way because we call this function to find out how to convert from
  6759. a pointer to member of FROM to a pointer to member of TO. */
  6760. static tree
  6761. get_delta_difference (tree from, tree to,
  6762. bool allow_inverse_p,
  6763. bool c_cast_p, tsubst_flags_t complain)
  6764. {
  6765. tree result;
  6766. if (same_type_ignoring_top_level_qualifiers_p (from, to))
  6767. /* Pointer to member of incomplete class is permitted*/
  6768. result = size_zero_node;
  6769. else
  6770. result = get_delta_difference_1 (from, to, c_cast_p, complain);
  6771. if (result == error_mark_node)
  6772. return error_mark_node;
  6773. if (!result)
  6774. {
  6775. if (!allow_inverse_p)
  6776. {
  6777. if (!(complain & tf_error))
  6778. return error_mark_node;
  6779. error_not_base_type (from, to);
  6780. error (" in pointer to member conversion");
  6781. result = size_zero_node;
  6782. }
  6783. else
  6784. {
  6785. result = get_delta_difference_1 (to, from, c_cast_p, complain);
  6786. if (result == error_mark_node)
  6787. return error_mark_node;
  6788. if (result)
  6789. result = size_diffop_loc (input_location,
  6790. size_zero_node, result);
  6791. else
  6792. {
  6793. if (!(complain & tf_error))
  6794. return error_mark_node;
  6795. error_not_base_type (from, to);
  6796. error (" in pointer to member conversion");
  6797. result = size_zero_node;
  6798. }
  6799. }
  6800. }
  6801. return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
  6802. result));
  6803. }
  6804. /* Return a constructor for the pointer-to-member-function TYPE using
  6805. the other components as specified. */
  6806. tree
  6807. build_ptrmemfunc1 (tree type, tree delta, tree pfn)
  6808. {
  6809. tree u = NULL_TREE;
  6810. tree delta_field;
  6811. tree pfn_field;
  6812. vec<constructor_elt, va_gc> *v;
  6813. /* Pull the FIELD_DECLs out of the type. */
  6814. pfn_field = TYPE_FIELDS (type);
  6815. delta_field = DECL_CHAIN (pfn_field);
  6816. /* Make sure DELTA has the type we want. */
  6817. delta = convert_and_check (input_location, delta_type_node, delta);
  6818. /* Convert to the correct target type if necessary. */
  6819. pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
  6820. /* Finish creating the initializer. */
  6821. vec_alloc (v, 2);
  6822. CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
  6823. CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
  6824. u = build_constructor (type, v);
  6825. TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
  6826. TREE_STATIC (u) = (TREE_CONSTANT (u)
  6827. && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
  6828. != NULL_TREE)
  6829. && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
  6830. != NULL_TREE));
  6831. return u;
  6832. }
  6833. /* Build a constructor for a pointer to member function. It can be
  6834. used to initialize global variables, local variable, or used
  6835. as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
  6836. want to be.
  6837. If FORCE is nonzero, then force this conversion, even if
  6838. we would rather not do it. Usually set when using an explicit
  6839. cast. A C-style cast is being processed iff C_CAST_P is true.
  6840. Return error_mark_node, if something goes wrong. */
  6841. tree
  6842. build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
  6843. tsubst_flags_t complain)
  6844. {
  6845. tree fn;
  6846. tree pfn_type;
  6847. tree to_type;
  6848. if (error_operand_p (pfn))
  6849. return error_mark_node;
  6850. pfn_type = TREE_TYPE (pfn);
  6851. to_type = build_ptrmemfunc_type (type);
  6852. /* Handle multiple conversions of pointer to member functions. */
  6853. if (TYPE_PTRMEMFUNC_P (pfn_type))
  6854. {
  6855. tree delta = NULL_TREE;
  6856. tree npfn = NULL_TREE;
  6857. tree n;
  6858. if (!force
  6859. && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
  6860. LOOKUP_NORMAL, complain))
  6861. {
  6862. if (complain & tf_error)
  6863. error ("invalid conversion to type %qT from type %qT",
  6864. to_type, pfn_type);
  6865. else
  6866. return error_mark_node;
  6867. }
  6868. n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
  6869. TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
  6870. force,
  6871. c_cast_p, complain);
  6872. if (n == error_mark_node)
  6873. return error_mark_node;
  6874. /* We don't have to do any conversion to convert a
  6875. pointer-to-member to its own type. But, we don't want to
  6876. just return a PTRMEM_CST if there's an explicit cast; that
  6877. cast should make the expression an invalid template argument. */
  6878. if (TREE_CODE (pfn) != PTRMEM_CST)
  6879. {
  6880. if (same_type_p (to_type, pfn_type))
  6881. return pfn;
  6882. else if (integer_zerop (n))
  6883. return build_reinterpret_cast (to_type, pfn,
  6884. complain);
  6885. }
  6886. if (TREE_SIDE_EFFECTS (pfn))
  6887. pfn = save_expr (pfn);
  6888. /* Obtain the function pointer and the current DELTA. */
  6889. if (TREE_CODE (pfn) == PTRMEM_CST)
  6890. expand_ptrmemfunc_cst (pfn, &delta, &npfn);
  6891. else
  6892. {
  6893. npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
  6894. delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
  6895. }
  6896. /* Just adjust the DELTA field. */
  6897. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  6898. (TREE_TYPE (delta), ptrdiff_type_node));
  6899. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
  6900. n = cp_build_binary_op (input_location,
  6901. LSHIFT_EXPR, n, integer_one_node,
  6902. complain);
  6903. delta = cp_build_binary_op (input_location,
  6904. PLUS_EXPR, delta, n, complain);
  6905. return build_ptrmemfunc1 (to_type, delta, npfn);
  6906. }
  6907. /* Handle null pointer to member function conversions. */
  6908. if (null_ptr_cst_p (pfn))
  6909. {
  6910. pfn = cp_build_c_cast (type, pfn, complain);
  6911. return build_ptrmemfunc1 (to_type,
  6912. integer_zero_node,
  6913. pfn);
  6914. }
  6915. if (type_unknown_p (pfn))
  6916. return instantiate_type (type, pfn, complain);
  6917. fn = TREE_OPERAND (pfn, 0);
  6918. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  6919. /* In a template, we will have preserved the
  6920. OFFSET_REF. */
  6921. || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
  6922. return make_ptrmem_cst (to_type, fn);
  6923. }
  6924. /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
  6925. given by CST.
  6926. ??? There is no consistency as to the types returned for the above
  6927. values. Some code acts as if it were a sizetype and some as if it were
  6928. integer_type_node. */
  6929. void
  6930. expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
  6931. {
  6932. tree type = TREE_TYPE (cst);
  6933. tree fn = PTRMEM_CST_MEMBER (cst);
  6934. tree ptr_class, fn_class;
  6935. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  6936. /* The class that the function belongs to. */
  6937. fn_class = DECL_CONTEXT (fn);
  6938. /* The class that we're creating a pointer to member of. */
  6939. ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
  6940. /* First, calculate the adjustment to the function's class. */
  6941. *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
  6942. /*c_cast_p=*/0, tf_warning_or_error);
  6943. if (!DECL_VIRTUAL_P (fn))
  6944. *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
  6945. build_addr_func (fn, tf_warning_or_error));
  6946. else
  6947. {
  6948. /* If we're dealing with a virtual function, we have to adjust 'this'
  6949. again, to point to the base which provides the vtable entry for
  6950. fn; the call will do the opposite adjustment. */
  6951. tree orig_class = DECL_CONTEXT (fn);
  6952. tree binfo = binfo_or_else (orig_class, fn_class);
  6953. *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
  6954. *delta, BINFO_OFFSET (binfo));
  6955. *delta = fold_if_not_in_template (*delta);
  6956. /* We set PFN to the vtable offset at which the function can be
  6957. found, plus one (unless ptrmemfunc_vbit_in_delta, in which
  6958. case delta is shifted left, and then incremented). */
  6959. *pfn = DECL_VINDEX (fn);
  6960. *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
  6961. TYPE_SIZE_UNIT (vtable_entry_type));
  6962. *pfn = fold_if_not_in_template (*pfn);
  6963. switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
  6964. {
  6965. case ptrmemfunc_vbit_in_pfn:
  6966. *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
  6967. integer_one_node);
  6968. *pfn = fold_if_not_in_template (*pfn);
  6969. break;
  6970. case ptrmemfunc_vbit_in_delta:
  6971. *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
  6972. *delta, integer_one_node);
  6973. *delta = fold_if_not_in_template (*delta);
  6974. *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
  6975. *delta, integer_one_node);
  6976. *delta = fold_if_not_in_template (*delta);
  6977. break;
  6978. default:
  6979. gcc_unreachable ();
  6980. }
  6981. *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
  6982. *pfn = fold_if_not_in_template (*pfn);
  6983. }
  6984. }
  6985. /* Return an expression for PFN from the pointer-to-member function
  6986. given by T. */
  6987. static tree
  6988. pfn_from_ptrmemfunc (tree t)
  6989. {
  6990. if (TREE_CODE (t) == PTRMEM_CST)
  6991. {
  6992. tree delta;
  6993. tree pfn;
  6994. expand_ptrmemfunc_cst (t, &delta, &pfn);
  6995. if (pfn)
  6996. return pfn;
  6997. }
  6998. return build_ptrmemfunc_access_expr (t, pfn_identifier);
  6999. }
  7000. /* Return an expression for DELTA from the pointer-to-member function
  7001. given by T. */
  7002. static tree
  7003. delta_from_ptrmemfunc (tree t)
  7004. {
  7005. if (TREE_CODE (t) == PTRMEM_CST)
  7006. {
  7007. tree delta;
  7008. tree pfn;
  7009. expand_ptrmemfunc_cst (t, &delta, &pfn);
  7010. if (delta)
  7011. return delta;
  7012. }
  7013. return build_ptrmemfunc_access_expr (t, delta_identifier);
  7014. }
  7015. /* Convert value RHS to type TYPE as preparation for an assignment to
  7016. an lvalue of type TYPE. ERRTYPE indicates what kind of error the
  7017. implicit conversion is. If FNDECL is non-NULL, we are doing the
  7018. conversion in order to pass the PARMNUMth argument of FNDECL.
  7019. If FNDECL is NULL, we are doing the conversion in function pointer
  7020. argument passing, conversion in initialization, etc. */
  7021. static tree
  7022. convert_for_assignment (tree type, tree rhs,
  7023. impl_conv_rhs errtype, tree fndecl, int parmnum,
  7024. tsubst_flags_t complain, int flags)
  7025. {
  7026. tree rhstype;
  7027. enum tree_code coder;
  7028. /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
  7029. if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
  7030. rhs = TREE_OPERAND (rhs, 0);
  7031. rhstype = TREE_TYPE (rhs);
  7032. coder = TREE_CODE (rhstype);
  7033. if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
  7034. && vector_types_convertible_p (type, rhstype, true))
  7035. {
  7036. rhs = mark_rvalue_use (rhs);
  7037. return convert (type, rhs);
  7038. }
  7039. if (rhs == error_mark_node || rhstype == error_mark_node)
  7040. return error_mark_node;
  7041. if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
  7042. return error_mark_node;
  7043. /* The RHS of an assignment cannot have void type. */
  7044. if (coder == VOID_TYPE)
  7045. {
  7046. if (complain & tf_error)
  7047. error ("void value not ignored as it ought to be");
  7048. return error_mark_node;
  7049. }
  7050. if (c_dialect_objc ())
  7051. {
  7052. int parmno;
  7053. tree selector;
  7054. tree rname = fndecl;
  7055. switch (errtype)
  7056. {
  7057. case ICR_ASSIGN:
  7058. parmno = -1;
  7059. break;
  7060. case ICR_INIT:
  7061. parmno = -2;
  7062. break;
  7063. default:
  7064. selector = objc_message_selector ();
  7065. parmno = parmnum;
  7066. if (selector && parmno > 1)
  7067. {
  7068. rname = selector;
  7069. parmno -= 1;
  7070. }
  7071. }
  7072. if (objc_compare_types (type, rhstype, parmno, rname))
  7073. {
  7074. rhs = mark_rvalue_use (rhs);
  7075. return convert (type, rhs);
  7076. }
  7077. }
  7078. /* [expr.ass]
  7079. The expression is implicitly converted (clause _conv_) to the
  7080. cv-unqualified type of the left operand.
  7081. We allow bad conversions here because by the time we get to this point
  7082. we are committed to doing the conversion. If we end up doing a bad
  7083. conversion, convert_like will complain. */
  7084. if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
  7085. {
  7086. /* When -Wno-pmf-conversions is use, we just silently allow
  7087. conversions from pointers-to-members to plain pointers. If
  7088. the conversion doesn't work, cp_convert will complain. */
  7089. if (!warn_pmf2ptr
  7090. && TYPE_PTR_P (type)
  7091. && TYPE_PTRMEMFUNC_P (rhstype))
  7092. rhs = cp_convert (strip_top_quals (type), rhs, complain);
  7093. else
  7094. {
  7095. if (complain & tf_error)
  7096. {
  7097. /* If the right-hand side has unknown type, then it is an
  7098. overloaded function. Call instantiate_type to get error
  7099. messages. */
  7100. if (rhstype == unknown_type_node)
  7101. instantiate_type (type, rhs, tf_warning_or_error);
  7102. else if (fndecl)
  7103. error ("cannot convert %qT to %qT for argument %qP to %qD",
  7104. rhstype, type, parmnum, fndecl);
  7105. else
  7106. switch (errtype)
  7107. {
  7108. case ICR_DEFAULT_ARGUMENT:
  7109. error ("cannot convert %qT to %qT in default argument",
  7110. rhstype, type);
  7111. break;
  7112. case ICR_ARGPASS:
  7113. error ("cannot convert %qT to %qT in argument passing",
  7114. rhstype, type);
  7115. break;
  7116. case ICR_CONVERTING:
  7117. error ("cannot convert %qT to %qT",
  7118. rhstype, type);
  7119. break;
  7120. case ICR_INIT:
  7121. error ("cannot convert %qT to %qT in initialization",
  7122. rhstype, type);
  7123. break;
  7124. case ICR_RETURN:
  7125. error ("cannot convert %qT to %qT in return",
  7126. rhstype, type);
  7127. break;
  7128. case ICR_ASSIGN:
  7129. error ("cannot convert %qT to %qT in assignment",
  7130. rhstype, type);
  7131. break;
  7132. default:
  7133. gcc_unreachable();
  7134. }
  7135. if (TYPE_PTR_P (rhstype)
  7136. && TYPE_PTR_P (type)
  7137. && CLASS_TYPE_P (TREE_TYPE (rhstype))
  7138. && CLASS_TYPE_P (TREE_TYPE (type))
  7139. && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
  7140. inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
  7141. (TREE_TYPE (rhstype))),
  7142. "class type %qT is incomplete", TREE_TYPE (rhstype));
  7143. }
  7144. return error_mark_node;
  7145. }
  7146. }
  7147. if (warn_suggest_attribute_format)
  7148. {
  7149. const enum tree_code codel = TREE_CODE (type);
  7150. if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
  7151. && coder == codel
  7152. && check_missing_format_attribute (type, rhstype)
  7153. && (complain & tf_warning))
  7154. switch (errtype)
  7155. {
  7156. case ICR_ARGPASS:
  7157. case ICR_DEFAULT_ARGUMENT:
  7158. if (fndecl)
  7159. warning (OPT_Wsuggest_attribute_format,
  7160. "parameter %qP of %qD might be a candidate "
  7161. "for a format attribute", parmnum, fndecl);
  7162. else
  7163. warning (OPT_Wsuggest_attribute_format,
  7164. "parameter might be a candidate "
  7165. "for a format attribute");
  7166. break;
  7167. case ICR_CONVERTING:
  7168. warning (OPT_Wsuggest_attribute_format,
  7169. "target of conversion might be a candidate "
  7170. "for a format attribute");
  7171. break;
  7172. case ICR_INIT:
  7173. warning (OPT_Wsuggest_attribute_format,
  7174. "target of initialization might be a candidate "
  7175. "for a format attribute");
  7176. break;
  7177. case ICR_RETURN:
  7178. warning (OPT_Wsuggest_attribute_format,
  7179. "return type might be a candidate "
  7180. "for a format attribute");
  7181. break;
  7182. case ICR_ASSIGN:
  7183. warning (OPT_Wsuggest_attribute_format,
  7184. "left-hand side of assignment might be a candidate "
  7185. "for a format attribute");
  7186. break;
  7187. default:
  7188. gcc_unreachable();
  7189. }
  7190. }
  7191. /* If -Wparentheses, warn about a = b = c when a has type bool and b
  7192. does not. */
  7193. if (warn_parentheses
  7194. && TREE_CODE (type) == BOOLEAN_TYPE
  7195. && TREE_CODE (rhs) == MODIFY_EXPR
  7196. && !TREE_NO_WARNING (rhs)
  7197. && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
  7198. && (complain & tf_warning))
  7199. {
  7200. location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
  7201. warning_at (loc, OPT_Wparentheses,
  7202. "suggest parentheses around assignment used as truth value");
  7203. TREE_NO_WARNING (rhs) = 1;
  7204. }
  7205. return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
  7206. complain, flags);
  7207. }
  7208. /* Convert RHS to be of type TYPE.
  7209. If EXP is nonzero, it is the target of the initialization.
  7210. ERRTYPE indicates what kind of error the implicit conversion is.
  7211. Two major differences between the behavior of
  7212. `convert_for_assignment' and `convert_for_initialization'
  7213. are that references are bashed in the former, while
  7214. copied in the latter, and aggregates are assigned in
  7215. the former (operator=) while initialized in the
  7216. latter (X(X&)).
  7217. If using constructor make sure no conversion operator exists, if one does
  7218. exist, an ambiguity exists. */
  7219. tree
  7220. convert_for_initialization (tree exp, tree type, tree rhs, int flags,
  7221. impl_conv_rhs errtype, tree fndecl, int parmnum,
  7222. tsubst_flags_t complain)
  7223. {
  7224. enum tree_code codel = TREE_CODE (type);
  7225. tree rhstype;
  7226. enum tree_code coder;
  7227. /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
  7228. Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
  7229. if (TREE_CODE (rhs) == NOP_EXPR
  7230. && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
  7231. && codel != REFERENCE_TYPE)
  7232. rhs = TREE_OPERAND (rhs, 0);
  7233. if (type == error_mark_node
  7234. || rhs == error_mark_node
  7235. || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
  7236. return error_mark_node;
  7237. if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
  7238. && TREE_CODE (type) != ARRAY_TYPE
  7239. && (TREE_CODE (type) != REFERENCE_TYPE
  7240. || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
  7241. || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
  7242. && !TYPE_REFFN_P (type))
  7243. || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
  7244. rhs = decay_conversion (rhs, complain);
  7245. rhstype = TREE_TYPE (rhs);
  7246. coder = TREE_CODE (rhstype);
  7247. if (coder == ERROR_MARK)
  7248. return error_mark_node;
  7249. /* We accept references to incomplete types, so we can
  7250. return here before checking if RHS is of complete type. */
  7251. if (codel == REFERENCE_TYPE)
  7252. {
  7253. /* This should eventually happen in convert_arguments. */
  7254. int savew = 0, savee = 0;
  7255. if (fndecl)
  7256. savew = warningcount + werrorcount, savee = errorcount;
  7257. rhs = initialize_reference (type, rhs, flags, complain);
  7258. if (fndecl
  7259. && (warningcount + werrorcount > savew || errorcount > savee))
  7260. inform (input_location,
  7261. "in passing argument %P of %q+D", parmnum, fndecl);
  7262. return rhs;
  7263. }
  7264. if (exp != 0)
  7265. exp = require_complete_type_sfinae (exp, complain);
  7266. if (exp == error_mark_node)
  7267. return error_mark_node;
  7268. rhstype = non_reference (rhstype);
  7269. type = complete_type (type);
  7270. if (DIRECT_INIT_EXPR_P (type, rhs))
  7271. /* Don't try to do copy-initialization if we already have
  7272. direct-initialization. */
  7273. return rhs;
  7274. if (MAYBE_CLASS_TYPE_P (type))
  7275. return perform_implicit_conversion_flags (type, rhs, complain, flags);
  7276. return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
  7277. complain, flags);
  7278. }
  7279. /* If RETVAL is the address of, or a reference to, a local variable or
  7280. temporary give an appropriate warning and return true. */
  7281. static bool
  7282. maybe_warn_about_returning_address_of_local (tree retval)
  7283. {
  7284. tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
  7285. tree whats_returned = retval;
  7286. for (;;)
  7287. {
  7288. if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
  7289. whats_returned = TREE_OPERAND (whats_returned, 1);
  7290. else if (CONVERT_EXPR_P (whats_returned)
  7291. || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
  7292. whats_returned = TREE_OPERAND (whats_returned, 0);
  7293. else
  7294. break;
  7295. }
  7296. if (TREE_CODE (whats_returned) != ADDR_EXPR)
  7297. return false;
  7298. whats_returned = TREE_OPERAND (whats_returned, 0);
  7299. while (TREE_CODE (whats_returned) == COMPONENT_REF
  7300. || TREE_CODE (whats_returned) == ARRAY_REF)
  7301. whats_returned = TREE_OPERAND (whats_returned, 0);
  7302. if (TREE_CODE (valtype) == REFERENCE_TYPE)
  7303. {
  7304. if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
  7305. || TREE_CODE (whats_returned) == TARGET_EXPR)
  7306. {
  7307. warning (OPT_Wreturn_local_addr, "returning reference to temporary");
  7308. return true;
  7309. }
  7310. if (VAR_P (whats_returned)
  7311. && DECL_NAME (whats_returned)
  7312. && TEMP_NAME_P (DECL_NAME (whats_returned)))
  7313. {
  7314. warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
  7315. return true;
  7316. }
  7317. }
  7318. if (DECL_P (whats_returned)
  7319. && DECL_NAME (whats_returned)
  7320. && DECL_FUNCTION_SCOPE_P (whats_returned)
  7321. && !is_capture_proxy (whats_returned)
  7322. && !(TREE_STATIC (whats_returned)
  7323. || TREE_PUBLIC (whats_returned)))
  7324. {
  7325. if (TREE_CODE (valtype) == REFERENCE_TYPE)
  7326. warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
  7327. whats_returned);
  7328. else if (TREE_CODE (whats_returned) == LABEL_DECL)
  7329. warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
  7330. whats_returned);
  7331. else
  7332. warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
  7333. "returned", whats_returned);
  7334. return true;
  7335. }
  7336. return false;
  7337. }
  7338. /* Check that returning RETVAL from the current function is valid.
  7339. Return an expression explicitly showing all conversions required to
  7340. change RETVAL into the function return type, and to assign it to
  7341. the DECL_RESULT for the function. Set *NO_WARNING to true if
  7342. code reaches end of non-void function warning shouldn't be issued
  7343. on this RETURN_EXPR. */
  7344. tree
  7345. check_return_expr (tree retval, bool *no_warning)
  7346. {
  7347. tree result;
  7348. /* The type actually returned by the function. */
  7349. tree valtype;
  7350. /* The type the function is declared to return, or void if
  7351. the declared type is incomplete. */
  7352. tree functype;
  7353. int fn_returns_value_p;
  7354. bool named_return_value_okay_p;
  7355. *no_warning = false;
  7356. if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
  7357. {
  7358. error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
  7359. "statement is not allowed");
  7360. return NULL_TREE;
  7361. }
  7362. /* A `volatile' function is one that isn't supposed to return, ever.
  7363. (This is a G++ extension, used to get better code for functions
  7364. that call the `volatile' function.) */
  7365. if (TREE_THIS_VOLATILE (current_function_decl))
  7366. warning (0, "function declared %<noreturn%> has a %<return%> statement");
  7367. /* Check for various simple errors. */
  7368. if (DECL_DESTRUCTOR_P (current_function_decl))
  7369. {
  7370. if (retval)
  7371. error ("returning a value from a destructor");
  7372. return NULL_TREE;
  7373. }
  7374. else if (DECL_CONSTRUCTOR_P (current_function_decl))
  7375. {
  7376. if (in_function_try_handler)
  7377. /* If a return statement appears in a handler of the
  7378. function-try-block of a constructor, the program is ill-formed. */
  7379. error ("cannot return from a handler of a function-try-block of a constructor");
  7380. else if (retval)
  7381. /* You can't return a value from a constructor. */
  7382. error ("returning a value from a constructor");
  7383. return NULL_TREE;
  7384. }
  7385. if (processing_template_decl)
  7386. {
  7387. current_function_returns_value = 1;
  7388. if (check_for_bare_parameter_packs (retval))
  7389. retval = error_mark_node;
  7390. return retval;
  7391. }
  7392. functype = TREE_TYPE (TREE_TYPE (current_function_decl));
  7393. /* Deduce auto return type from a return statement. */
  7394. if (current_function_auto_return_pattern)
  7395. {
  7396. tree auto_node;
  7397. tree type;
  7398. if (!retval && !is_auto (current_function_auto_return_pattern))
  7399. {
  7400. /* Give a helpful error message. */
  7401. error ("return-statement with no value, in function returning %qT",
  7402. current_function_auto_return_pattern);
  7403. inform (input_location, "only plain %<auto%> return type can be "
  7404. "deduced to %<void%>");
  7405. type = error_mark_node;
  7406. }
  7407. else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
  7408. {
  7409. error ("returning initializer list");
  7410. type = error_mark_node;
  7411. }
  7412. else
  7413. {
  7414. if (!retval)
  7415. retval = void_node;
  7416. auto_node = type_uses_auto (current_function_auto_return_pattern);
  7417. type = do_auto_deduction (current_function_auto_return_pattern,
  7418. retval, auto_node);
  7419. }
  7420. if (type == error_mark_node)
  7421. /* Leave it. */;
  7422. else if (functype == current_function_auto_return_pattern)
  7423. apply_deduced_return_type (current_function_decl, type);
  7424. else
  7425. /* A mismatch should have been diagnosed in do_auto_deduction. */
  7426. gcc_assert (same_type_p (type, functype));
  7427. functype = type;
  7428. }
  7429. /* When no explicit return-value is given in a function with a named
  7430. return value, the named return value is used. */
  7431. result = DECL_RESULT (current_function_decl);
  7432. valtype = TREE_TYPE (result);
  7433. gcc_assert (valtype != NULL_TREE);
  7434. fn_returns_value_p = !VOID_TYPE_P (valtype);
  7435. if (!retval && DECL_NAME (result) && fn_returns_value_p)
  7436. retval = result;
  7437. /* Check for a return statement with no return value in a function
  7438. that's supposed to return a value. */
  7439. if (!retval && fn_returns_value_p)
  7440. {
  7441. if (functype != error_mark_node)
  7442. permerror (input_location, "return-statement with no value, in "
  7443. "function returning %qT", valtype);
  7444. /* Remember that this function did return. */
  7445. current_function_returns_value = 1;
  7446. /* And signal caller that TREE_NO_WARNING should be set on the
  7447. RETURN_EXPR to avoid control reaches end of non-void function
  7448. warnings in tree-cfg.c. */
  7449. *no_warning = true;
  7450. }
  7451. /* Check for a return statement with a value in a function that
  7452. isn't supposed to return a value. */
  7453. else if (retval && !fn_returns_value_p)
  7454. {
  7455. if (VOID_TYPE_P (TREE_TYPE (retval)))
  7456. /* You can return a `void' value from a function of `void'
  7457. type. In that case, we have to evaluate the expression for
  7458. its side-effects. */
  7459. finish_expr_stmt (retval);
  7460. else
  7461. permerror (input_location, "return-statement with a value, in function "
  7462. "returning 'void'");
  7463. current_function_returns_null = 1;
  7464. /* There's really no value to return, after all. */
  7465. return NULL_TREE;
  7466. }
  7467. else if (!retval)
  7468. /* Remember that this function can sometimes return without a
  7469. value. */
  7470. current_function_returns_null = 1;
  7471. else
  7472. /* Remember that this function did return a value. */
  7473. current_function_returns_value = 1;
  7474. /* Check for erroneous operands -- but after giving ourselves a
  7475. chance to provide an error about returning a value from a void
  7476. function. */
  7477. if (error_operand_p (retval))
  7478. {
  7479. current_function_return_value = error_mark_node;
  7480. return error_mark_node;
  7481. }
  7482. /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
  7483. if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
  7484. || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
  7485. && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
  7486. && ! flag_check_new
  7487. && retval && null_ptr_cst_p (retval))
  7488. warning (0, "%<operator new%> must not return NULL unless it is "
  7489. "declared %<throw()%> (or -fcheck-new is in effect)");
  7490. /* Effective C++ rule 15. See also start_function. */
  7491. if (warn_ecpp
  7492. && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
  7493. {
  7494. bool warn = true;
  7495. /* The function return type must be a reference to the current
  7496. class. */
  7497. if (TREE_CODE (valtype) == REFERENCE_TYPE
  7498. && same_type_ignoring_top_level_qualifiers_p
  7499. (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
  7500. {
  7501. /* Returning '*this' is obviously OK. */
  7502. if (retval == current_class_ref)
  7503. warn = false;
  7504. /* If we are calling a function whose return type is the same of
  7505. the current class reference, it is ok. */
  7506. else if (INDIRECT_REF_P (retval)
  7507. && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
  7508. warn = false;
  7509. }
  7510. if (warn)
  7511. warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
  7512. }
  7513. /* The fabled Named Return Value optimization, as per [class.copy]/15:
  7514. [...] For a function with a class return type, if the expression
  7515. in the return statement is the name of a local object, and the cv-
  7516. unqualified type of the local object is the same as the function
  7517. return type, an implementation is permitted to omit creating the tem-
  7518. porary object to hold the function return value [...]
  7519. So, if this is a value-returning function that always returns the same
  7520. local variable, remember it.
  7521. It might be nice to be more flexible, and choose the first suitable
  7522. variable even if the function sometimes returns something else, but
  7523. then we run the risk of clobbering the variable we chose if the other
  7524. returned expression uses the chosen variable somehow. And people expect
  7525. this restriction, anyway. (jason 2000-11-19)
  7526. See finish_function and finalize_nrv for the rest of this optimization. */
  7527. named_return_value_okay_p =
  7528. (retval != NULL_TREE
  7529. /* Must be a local, automatic variable. */
  7530. && VAR_P (retval)
  7531. && DECL_CONTEXT (retval) == current_function_decl
  7532. && ! TREE_STATIC (retval)
  7533. /* And not a lambda or anonymous union proxy. */
  7534. && !DECL_HAS_VALUE_EXPR_P (retval)
  7535. && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
  7536. /* The cv-unqualified type of the returned value must be the
  7537. same as the cv-unqualified return type of the
  7538. function. */
  7539. && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
  7540. (TYPE_MAIN_VARIANT (functype)))
  7541. /* And the returned value must be non-volatile. */
  7542. && ! TYPE_VOLATILE (TREE_TYPE (retval)));
  7543. if (fn_returns_value_p && flag_elide_constructors)
  7544. {
  7545. if (named_return_value_okay_p
  7546. && (current_function_return_value == NULL_TREE
  7547. || current_function_return_value == retval))
  7548. current_function_return_value = retval;
  7549. else
  7550. current_function_return_value = error_mark_node;
  7551. }
  7552. /* We don't need to do any conversions when there's nothing being
  7553. returned. */
  7554. if (!retval)
  7555. return NULL_TREE;
  7556. /* Do any required conversions. */
  7557. if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
  7558. /* No conversions are required. */
  7559. ;
  7560. else
  7561. {
  7562. int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
  7563. /* The functype's return type will have been set to void, if it
  7564. was an incomplete type. Just treat this as 'return;' */
  7565. if (VOID_TYPE_P (functype))
  7566. return error_mark_node;
  7567. /* If we had an id-expression obfuscated by force_paren_expr, we need
  7568. to undo it so we can try to treat it as an rvalue below. */
  7569. if (cxx_dialect >= cxx14
  7570. && INDIRECT_REF_P (retval)
  7571. && REF_PARENTHESIZED_P (retval))
  7572. {
  7573. retval = TREE_OPERAND (retval, 0);
  7574. while (TREE_CODE (retval) == NON_LVALUE_EXPR
  7575. || TREE_CODE (retval) == NOP_EXPR)
  7576. retval = TREE_OPERAND (retval, 0);
  7577. gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
  7578. retval = TREE_OPERAND (retval, 0);
  7579. }
  7580. /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
  7581. treated as an rvalue for the purposes of overload resolution to
  7582. favor move constructors over copy constructors.
  7583. Note that these conditions are similar to, but not as strict as,
  7584. the conditions for the named return value optimization. */
  7585. if ((cxx_dialect != cxx98)
  7586. && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
  7587. || TREE_CODE (retval) == PARM_DECL)
  7588. && DECL_CONTEXT (retval) == current_function_decl
  7589. && !TREE_STATIC (retval)
  7590. /* This is only interesting for class type. */
  7591. && CLASS_TYPE_P (functype))
  7592. flags = flags | LOOKUP_PREFER_RVALUE;
  7593. /* First convert the value to the function's return type, then
  7594. to the type of return value's location to handle the
  7595. case that functype is smaller than the valtype. */
  7596. retval = convert_for_initialization
  7597. (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
  7598. tf_warning_or_error);
  7599. retval = convert (valtype, retval);
  7600. /* If the conversion failed, treat this just like `return;'. */
  7601. if (retval == error_mark_node)
  7602. return retval;
  7603. /* We can't initialize a register from a AGGR_INIT_EXPR. */
  7604. else if (! cfun->returns_struct
  7605. && TREE_CODE (retval) == TARGET_EXPR
  7606. && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
  7607. retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
  7608. TREE_OPERAND (retval, 0));
  7609. else if (maybe_warn_about_returning_address_of_local (retval))
  7610. retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
  7611. build_zero_cst (TREE_TYPE (retval)));
  7612. }
  7613. /* Actually copy the value returned into the appropriate location. */
  7614. if (retval && retval != result)
  7615. retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
  7616. return retval;
  7617. }
  7618. /* Returns nonzero if the pointer-type FROM can be converted to the
  7619. pointer-type TO via a qualification conversion. If CONSTP is -1,
  7620. then we return nonzero if the pointers are similar, and the
  7621. cv-qualification signature of FROM is a proper subset of that of TO.
  7622. If CONSTP is positive, then all outer pointers have been
  7623. const-qualified. */
  7624. static int
  7625. comp_ptr_ttypes_real (tree to, tree from, int constp)
  7626. {
  7627. bool to_more_cv_qualified = false;
  7628. bool is_opaque_pointer = false;
  7629. for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
  7630. {
  7631. if (TREE_CODE (to) != TREE_CODE (from))
  7632. return 0;
  7633. if (TREE_CODE (from) == OFFSET_TYPE
  7634. && !same_type_p (TYPE_OFFSET_BASETYPE (from),
  7635. TYPE_OFFSET_BASETYPE (to)))
  7636. return 0;
  7637. /* Const and volatile mean something different for function types,
  7638. so the usual checks are not appropriate. */
  7639. if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
  7640. {
  7641. if (!at_least_as_qualified_p (to, from))
  7642. return 0;
  7643. if (!at_least_as_qualified_p (from, to))
  7644. {
  7645. if (constp == 0)
  7646. return 0;
  7647. to_more_cv_qualified = true;
  7648. }
  7649. if (constp > 0)
  7650. constp &= TYPE_READONLY (to);
  7651. }
  7652. if (TREE_CODE (to) == VECTOR_TYPE)
  7653. is_opaque_pointer = vector_targets_convertible_p (to, from);
  7654. if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
  7655. return ((constp >= 0 || to_more_cv_qualified)
  7656. && (is_opaque_pointer
  7657. || same_type_ignoring_top_level_qualifiers_p (to, from)));
  7658. }
  7659. }
  7660. /* When comparing, say, char ** to char const **, this function takes
  7661. the 'char *' and 'char const *'. Do not pass non-pointer/reference
  7662. types to this function. */
  7663. int
  7664. comp_ptr_ttypes (tree to, tree from)
  7665. {
  7666. return comp_ptr_ttypes_real (to, from, 1);
  7667. }
  7668. /* Returns true iff FNTYPE is a non-class type that involves
  7669. error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
  7670. if a parameter type is ill-formed. */
  7671. bool
  7672. error_type_p (const_tree type)
  7673. {
  7674. tree t;
  7675. switch (TREE_CODE (type))
  7676. {
  7677. case ERROR_MARK:
  7678. return true;
  7679. case POINTER_TYPE:
  7680. case REFERENCE_TYPE:
  7681. case OFFSET_TYPE:
  7682. return error_type_p (TREE_TYPE (type));
  7683. case FUNCTION_TYPE:
  7684. case METHOD_TYPE:
  7685. if (error_type_p (TREE_TYPE (type)))
  7686. return true;
  7687. for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
  7688. if (error_type_p (TREE_VALUE (t)))
  7689. return true;
  7690. return false;
  7691. case RECORD_TYPE:
  7692. if (TYPE_PTRMEMFUNC_P (type))
  7693. return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
  7694. return false;
  7695. default:
  7696. return false;
  7697. }
  7698. }
  7699. /* Returns true if to and from are (possibly multi-level) pointers to the same
  7700. type or inheritance-related types, regardless of cv-quals. */
  7701. bool
  7702. ptr_reasonably_similar (const_tree to, const_tree from)
  7703. {
  7704. for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
  7705. {
  7706. /* Any target type is similar enough to void. */
  7707. if (VOID_TYPE_P (to))
  7708. return !error_type_p (from);
  7709. if (VOID_TYPE_P (from))
  7710. return !error_type_p (to);
  7711. if (TREE_CODE (to) != TREE_CODE (from))
  7712. return false;
  7713. if (TREE_CODE (from) == OFFSET_TYPE
  7714. && comptypes (TYPE_OFFSET_BASETYPE (to),
  7715. TYPE_OFFSET_BASETYPE (from),
  7716. COMPARE_BASE | COMPARE_DERIVED))
  7717. continue;
  7718. if (TREE_CODE (to) == VECTOR_TYPE
  7719. && vector_types_convertible_p (to, from, false))
  7720. return true;
  7721. if (TREE_CODE (to) == INTEGER_TYPE
  7722. && TYPE_PRECISION (to) == TYPE_PRECISION (from))
  7723. return true;
  7724. if (TREE_CODE (to) == FUNCTION_TYPE)
  7725. return !error_type_p (to) && !error_type_p (from);
  7726. if (!TYPE_PTR_P (to))
  7727. {
  7728. /* When either type is incomplete avoid DERIVED_FROM_P,
  7729. which may call complete_type (c++/57942). */
  7730. bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
  7731. return comptypes
  7732. (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
  7733. b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
  7734. }
  7735. }
  7736. }
  7737. /* Return true if TO and FROM (both of which are POINTER_TYPEs or
  7738. pointer-to-member types) are the same, ignoring cv-qualification at
  7739. all levels. */
  7740. bool
  7741. comp_ptr_ttypes_const (tree to, tree from)
  7742. {
  7743. bool is_opaque_pointer = false;
  7744. for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
  7745. {
  7746. if (TREE_CODE (to) != TREE_CODE (from))
  7747. return false;
  7748. if (TREE_CODE (from) == OFFSET_TYPE
  7749. && same_type_p (TYPE_OFFSET_BASETYPE (from),
  7750. TYPE_OFFSET_BASETYPE (to)))
  7751. continue;
  7752. if (TREE_CODE (to) == VECTOR_TYPE)
  7753. is_opaque_pointer = vector_targets_convertible_p (to, from);
  7754. if (!TYPE_PTR_P (to))
  7755. return (is_opaque_pointer
  7756. || same_type_ignoring_top_level_qualifiers_p (to, from));
  7757. }
  7758. }
  7759. /* Returns the type qualifiers for this type, including the qualifiers on the
  7760. elements for an array type. */
  7761. int
  7762. cp_type_quals (const_tree type)
  7763. {
  7764. int quals;
  7765. /* This CONST_CAST is okay because strip_array_types returns its
  7766. argument unmodified and we assign it to a const_tree. */
  7767. type = strip_array_types (CONST_CAST_TREE (type));
  7768. if (type == error_mark_node
  7769. /* Quals on a FUNCTION_TYPE are memfn quals. */
  7770. || TREE_CODE (type) == FUNCTION_TYPE)
  7771. return TYPE_UNQUALIFIED;
  7772. quals = TYPE_QUALS (type);
  7773. /* METHOD and REFERENCE_TYPEs should never have quals. */
  7774. gcc_assert ((TREE_CODE (type) != METHOD_TYPE
  7775. && TREE_CODE (type) != REFERENCE_TYPE)
  7776. || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
  7777. == TYPE_UNQUALIFIED));
  7778. return quals;
  7779. }
  7780. /* Returns the function-ref-qualifier for TYPE */
  7781. cp_ref_qualifier
  7782. type_memfn_rqual (const_tree type)
  7783. {
  7784. gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
  7785. || TREE_CODE (type) == METHOD_TYPE);
  7786. if (!FUNCTION_REF_QUALIFIED (type))
  7787. return REF_QUAL_NONE;
  7788. else if (FUNCTION_RVALUE_QUALIFIED (type))
  7789. return REF_QUAL_RVALUE;
  7790. else
  7791. return REF_QUAL_LVALUE;
  7792. }
  7793. /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
  7794. METHOD_TYPE. */
  7795. int
  7796. type_memfn_quals (const_tree type)
  7797. {
  7798. if (TREE_CODE (type) == FUNCTION_TYPE)
  7799. return TYPE_QUALS (type);
  7800. else if (TREE_CODE (type) == METHOD_TYPE)
  7801. return cp_type_quals (class_of_this_parm (type));
  7802. else
  7803. gcc_unreachable ();
  7804. }
  7805. /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
  7806. MEMFN_QUALS and its ref-qualifier to RQUAL. */
  7807. tree
  7808. apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
  7809. {
  7810. /* Could handle METHOD_TYPE here if necessary. */
  7811. gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
  7812. if (TYPE_QUALS (type) == memfn_quals
  7813. && type_memfn_rqual (type) == rqual)
  7814. return type;
  7815. /* This should really have a different TYPE_MAIN_VARIANT, but that gets
  7816. complex. */
  7817. tree result = build_qualified_type (type, memfn_quals);
  7818. if (tree canon = TYPE_CANONICAL (result))
  7819. if (canon != result)
  7820. /* check_qualified_type doesn't check the ref-qualifier, so make sure
  7821. TYPE_CANONICAL is correct. */
  7822. TYPE_CANONICAL (result)
  7823. = build_ref_qualified_type (canon, type_memfn_rqual (result));
  7824. result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
  7825. return build_ref_qualified_type (result, rqual);
  7826. }
  7827. /* Returns nonzero if TYPE is const or volatile. */
  7828. bool
  7829. cv_qualified_p (const_tree type)
  7830. {
  7831. int quals = cp_type_quals (type);
  7832. return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
  7833. }
  7834. /* Returns nonzero if the TYPE contains a mutable member. */
  7835. bool
  7836. cp_has_mutable_p (const_tree type)
  7837. {
  7838. /* This CONST_CAST is okay because strip_array_types returns its
  7839. argument unmodified and we assign it to a const_tree. */
  7840. type = strip_array_types (CONST_CAST_TREE(type));
  7841. return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
  7842. }
  7843. /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
  7844. TYPE_QUALS. For a VAR_DECL, this may be an optimistic
  7845. approximation. In particular, consider:
  7846. int f();
  7847. struct S { int i; };
  7848. const S s = { f(); }
  7849. Here, we will make "s" as TREE_READONLY (because it is declared
  7850. "const") -- only to reverse ourselves upon seeing that the
  7851. initializer is non-constant. */
  7852. void
  7853. cp_apply_type_quals_to_decl (int type_quals, tree decl)
  7854. {
  7855. tree type = TREE_TYPE (decl);
  7856. if (type == error_mark_node)
  7857. return;
  7858. if (TREE_CODE (decl) == TYPE_DECL)
  7859. return;
  7860. gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
  7861. && type_quals != TYPE_UNQUALIFIED));
  7862. /* Avoid setting TREE_READONLY incorrectly. */
  7863. /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
  7864. constructor can produce constant init, so rely on cp_finish_decl to
  7865. clear TREE_READONLY if the variable has non-constant init. */
  7866. /* If the type has (or might have) a mutable component, that component
  7867. might be modified. */
  7868. if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
  7869. type_quals &= ~TYPE_QUAL_CONST;
  7870. c_apply_type_quals_to_decl (type_quals, decl);
  7871. }
  7872. /* Subroutine of casts_away_constness. Make T1 and T2 point at
  7873. exemplar types such that casting T1 to T2 is casting away constness
  7874. if and only if there is no implicit conversion from T1 to T2. */
  7875. static void
  7876. casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
  7877. {
  7878. int quals1;
  7879. int quals2;
  7880. /* [expr.const.cast]
  7881. For multi-level pointer to members and multi-level mixed pointers
  7882. and pointers to members (conv.qual), the "member" aspect of a
  7883. pointer to member level is ignored when determining if a const
  7884. cv-qualifier has been cast away. */
  7885. /* [expr.const.cast]
  7886. For two pointer types:
  7887. X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
  7888. X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
  7889. K is min(N,M)
  7890. casting from X1 to X2 casts away constness if, for a non-pointer
  7891. type T there does not exist an implicit conversion (clause
  7892. _conv_) from:
  7893. Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
  7894. to
  7895. Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
  7896. if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
  7897. || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
  7898. {
  7899. *t1 = cp_build_qualified_type (void_type_node,
  7900. cp_type_quals (*t1));
  7901. *t2 = cp_build_qualified_type (void_type_node,
  7902. cp_type_quals (*t2));
  7903. return;
  7904. }
  7905. quals1 = cp_type_quals (*t1);
  7906. quals2 = cp_type_quals (*t2);
  7907. if (TYPE_PTRDATAMEM_P (*t1))
  7908. *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
  7909. else
  7910. *t1 = TREE_TYPE (*t1);
  7911. if (TYPE_PTRDATAMEM_P (*t2))
  7912. *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
  7913. else
  7914. *t2 = TREE_TYPE (*t2);
  7915. casts_away_constness_r (t1, t2, complain);
  7916. *t1 = build_pointer_type (*t1);
  7917. *t2 = build_pointer_type (*t2);
  7918. *t1 = cp_build_qualified_type (*t1, quals1);
  7919. *t2 = cp_build_qualified_type (*t2, quals2);
  7920. }
  7921. /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
  7922. constness.
  7923. ??? This function returns non-zero if casting away qualifiers not
  7924. just const. We would like to return to the caller exactly which
  7925. qualifiers are casted away to give more accurate diagnostics.
  7926. */
  7927. static bool
  7928. casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
  7929. {
  7930. if (TREE_CODE (t2) == REFERENCE_TYPE)
  7931. {
  7932. /* [expr.const.cast]
  7933. Casting from an lvalue of type T1 to an lvalue of type T2
  7934. using a reference cast casts away constness if a cast from an
  7935. rvalue of type "pointer to T1" to the type "pointer to T2"
  7936. casts away constness. */
  7937. t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
  7938. return casts_away_constness (build_pointer_type (t1),
  7939. build_pointer_type (TREE_TYPE (t2)),
  7940. complain);
  7941. }
  7942. if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
  7943. /* [expr.const.cast]
  7944. Casting from an rvalue of type "pointer to data member of X
  7945. of type T1" to the type "pointer to data member of Y of type
  7946. T2" casts away constness if a cast from an rvalue of type
  7947. "pointer to T1" to the type "pointer to T2" casts away
  7948. constness. */
  7949. return casts_away_constness
  7950. (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
  7951. build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
  7952. complain);
  7953. /* Casting away constness is only something that makes sense for
  7954. pointer or reference types. */
  7955. if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
  7956. return false;
  7957. /* Top-level qualifiers don't matter. */
  7958. t1 = TYPE_MAIN_VARIANT (t1);
  7959. t2 = TYPE_MAIN_VARIANT (t2);
  7960. casts_away_constness_r (&t1, &t2, complain);
  7961. if (!can_convert (t2, t1, complain))
  7962. return true;
  7963. return false;
  7964. }
  7965. /* If T is a REFERENCE_TYPE return the type to which T refers.
  7966. Otherwise, return T itself. */
  7967. tree
  7968. non_reference (tree t)
  7969. {
  7970. if (t && TREE_CODE (t) == REFERENCE_TYPE)
  7971. t = TREE_TYPE (t);
  7972. return t;
  7973. }
  7974. /* Return nonzero if REF is an lvalue valid for this language;
  7975. otherwise, print an error message and return zero. USE says
  7976. how the lvalue is being used and so selects the error message. */
  7977. int
  7978. lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
  7979. {
  7980. cp_lvalue_kind kind = lvalue_kind (ref);
  7981. if (kind == clk_none)
  7982. {
  7983. if (complain & tf_error)
  7984. lvalue_error (input_location, use);
  7985. return 0;
  7986. }
  7987. else if (kind & (clk_rvalueref|clk_class))
  7988. {
  7989. if (!(complain & tf_error))
  7990. return 0;
  7991. if (kind & clk_class)
  7992. /* Make this a permerror because we used to accept it. */
  7993. permerror (input_location, "using temporary as lvalue");
  7994. else
  7995. error ("using xvalue (rvalue reference) as lvalue");
  7996. }
  7997. return 1;
  7998. }
  7999. /* Return true if a user-defined literal operator is a raw operator. */
  8000. bool
  8001. check_raw_literal_operator (const_tree decl)
  8002. {
  8003. tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
  8004. tree argtype;
  8005. int arity;
  8006. bool maybe_raw_p = false;
  8007. /* Count the number and type of arguments and check for ellipsis. */
  8008. for (argtype = argtypes, arity = 0;
  8009. argtype && argtype != void_list_node;
  8010. ++arity, argtype = TREE_CHAIN (argtype))
  8011. {
  8012. tree t = TREE_VALUE (argtype);
  8013. if (same_type_p (t, const_string_type_node))
  8014. maybe_raw_p = true;
  8015. }
  8016. if (!argtype)
  8017. return false; /* Found ellipsis. */
  8018. if (!maybe_raw_p || arity != 1)
  8019. return false;
  8020. return true;
  8021. }
  8022. /* Return true if a user-defined literal operator has one of the allowed
  8023. argument types. */
  8024. bool
  8025. check_literal_operator_args (const_tree decl,
  8026. bool *long_long_unsigned_p, bool *long_double_p)
  8027. {
  8028. tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
  8029. *long_long_unsigned_p = false;
  8030. *long_double_p = false;
  8031. if (processing_template_decl || processing_specialization)
  8032. return argtypes == void_list_node;
  8033. else
  8034. {
  8035. tree argtype;
  8036. int arity;
  8037. int max_arity = 2;
  8038. /* Count the number and type of arguments and check for ellipsis. */
  8039. for (argtype = argtypes, arity = 0;
  8040. argtype && argtype != void_list_node;
  8041. argtype = TREE_CHAIN (argtype))
  8042. {
  8043. tree t = TREE_VALUE (argtype);
  8044. ++arity;
  8045. if (TYPE_PTR_P (t))
  8046. {
  8047. bool maybe_raw_p = false;
  8048. t = TREE_TYPE (t);
  8049. if (cp_type_quals (t) != TYPE_QUAL_CONST)
  8050. return false;
  8051. t = TYPE_MAIN_VARIANT (t);
  8052. if ((maybe_raw_p = same_type_p (t, char_type_node))
  8053. || same_type_p (t, wchar_type_node)
  8054. || same_type_p (t, char16_type_node)
  8055. || same_type_p (t, char32_type_node))
  8056. {
  8057. argtype = TREE_CHAIN (argtype);
  8058. if (!argtype)
  8059. return false;
  8060. t = TREE_VALUE (argtype);
  8061. if (maybe_raw_p && argtype == void_list_node)
  8062. return true;
  8063. else if (same_type_p (t, size_type_node))
  8064. {
  8065. ++arity;
  8066. continue;
  8067. }
  8068. else
  8069. return false;
  8070. }
  8071. }
  8072. else if (same_type_p (t, long_long_unsigned_type_node))
  8073. {
  8074. max_arity = 1;
  8075. *long_long_unsigned_p = true;
  8076. }
  8077. else if (same_type_p (t, long_double_type_node))
  8078. {
  8079. max_arity = 1;
  8080. *long_double_p = true;
  8081. }
  8082. else if (same_type_p (t, char_type_node))
  8083. max_arity = 1;
  8084. else if (same_type_p (t, wchar_type_node))
  8085. max_arity = 1;
  8086. else if (same_type_p (t, char16_type_node))
  8087. max_arity = 1;
  8088. else if (same_type_p (t, char32_type_node))
  8089. max_arity = 1;
  8090. else
  8091. return false;
  8092. }
  8093. if (!argtype)
  8094. return false; /* Found ellipsis. */
  8095. if (arity != max_arity)
  8096. return false;
  8097. return true;
  8098. }
  8099. }