call.c 301 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894
  1. /* Functions related to invoking methods and overloaded functions.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann (tiemann@cygnus.com) and
  4. modified by Brendan Kehoe (brendan@cygnus.com).
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* High-level class interface. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "stor-layout.h"
  33. #include "trans-mem.h"
  34. #include "stringpool.h"
  35. #include "cp-tree.h"
  36. #include "flags.h"
  37. #include "toplev.h"
  38. #include "diagnostic-core.h"
  39. #include "intl.h"
  40. #include "target.h"
  41. #include "convert.h"
  42. #include "langhooks.h"
  43. #include "c-family/c-objc.h"
  44. #include "timevar.h"
  45. #include "hash-map.h"
  46. #include "is-a.h"
  47. #include "plugin-api.h"
  48. #include "hard-reg-set.h"
  49. #include "input.h"
  50. #include "function.h"
  51. #include "ipa-ref.h"
  52. #include "cgraph.h"
  53. #include "wide-int.h"
  54. #include "internal-fn.h"
  55. /* The various kinds of conversion. */
  56. typedef enum conversion_kind {
  57. ck_identity,
  58. ck_lvalue,
  59. ck_qual,
  60. ck_std,
  61. ck_ptr,
  62. ck_pmem,
  63. ck_base,
  64. ck_ref_bind,
  65. ck_user,
  66. ck_ambig,
  67. ck_list,
  68. ck_aggr,
  69. ck_rvalue
  70. } conversion_kind;
  71. /* The rank of the conversion. Order of the enumerals matters; better
  72. conversions should come earlier in the list. */
  73. typedef enum conversion_rank {
  74. cr_identity,
  75. cr_exact,
  76. cr_promotion,
  77. cr_std,
  78. cr_pbool,
  79. cr_user,
  80. cr_ellipsis,
  81. cr_bad
  82. } conversion_rank;
  83. /* An implicit conversion sequence, in the sense of [over.best.ics].
  84. The first conversion to be performed is at the end of the chain.
  85. That conversion is always a cr_identity conversion. */
  86. typedef struct conversion conversion;
  87. struct conversion {
  88. /* The kind of conversion represented by this step. */
  89. conversion_kind kind;
  90. /* The rank of this conversion. */
  91. conversion_rank rank;
  92. BOOL_BITFIELD user_conv_p : 1;
  93. BOOL_BITFIELD ellipsis_p : 1;
  94. BOOL_BITFIELD this_p : 1;
  95. /* True if this conversion would be permitted with a bending of
  96. language standards, e.g. disregarding pointer qualifiers or
  97. converting integers to pointers. */
  98. BOOL_BITFIELD bad_p : 1;
  99. /* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
  100. temporary should be created to hold the result of the
  101. conversion. */
  102. BOOL_BITFIELD need_temporary_p : 1;
  103. /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
  104. from a pointer-to-derived to pointer-to-base is being performed. */
  105. BOOL_BITFIELD base_p : 1;
  106. /* If KIND is ck_ref_bind, true when either an lvalue reference is
  107. being bound to an lvalue expression or an rvalue reference is
  108. being bound to an rvalue expression. If KIND is ck_rvalue,
  109. true when we should treat an lvalue as an rvalue (12.8p33). If
  110. KIND is ck_base, always false. */
  111. BOOL_BITFIELD rvaluedness_matches_p: 1;
  112. BOOL_BITFIELD check_narrowing: 1;
  113. /* The type of the expression resulting from the conversion. */
  114. tree type;
  115. union {
  116. /* The next conversion in the chain. Since the conversions are
  117. arranged from outermost to innermost, the NEXT conversion will
  118. actually be performed before this conversion. This variant is
  119. used only when KIND is neither ck_identity, ck_ambig nor
  120. ck_list. Please use the next_conversion function instead
  121. of using this field directly. */
  122. conversion *next;
  123. /* The expression at the beginning of the conversion chain. This
  124. variant is used only if KIND is ck_identity or ck_ambig. */
  125. tree expr;
  126. /* The array of conversions for an initializer_list, so this
  127. variant is used only when KIN D is ck_list. */
  128. conversion **list;
  129. } u;
  130. /* The function candidate corresponding to this conversion
  131. sequence. This field is only used if KIND is ck_user. */
  132. struct z_candidate *cand;
  133. };
  134. #define CONVERSION_RANK(NODE) \
  135. ((NODE)->bad_p ? cr_bad \
  136. : (NODE)->ellipsis_p ? cr_ellipsis \
  137. : (NODE)->user_conv_p ? cr_user \
  138. : (NODE)->rank)
  139. #define BAD_CONVERSION_RANK(NODE) \
  140. ((NODE)->ellipsis_p ? cr_ellipsis \
  141. : (NODE)->user_conv_p ? cr_user \
  142. : (NODE)->rank)
  143. static struct obstack conversion_obstack;
  144. static bool conversion_obstack_initialized;
  145. struct rejection_reason;
  146. static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
  147. static int equal_functions (tree, tree);
  148. static int joust (struct z_candidate *, struct z_candidate *, bool,
  149. tsubst_flags_t);
  150. static int compare_ics (conversion *, conversion *);
  151. static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
  152. static tree build_java_interface_fn_ref (tree, tree);
  153. #define convert_like(CONV, EXPR, COMPLAIN) \
  154. convert_like_real ((CONV), (EXPR), NULL_TREE, 0, 0, \
  155. /*issue_conversion_warnings=*/true, \
  156. /*c_cast_p=*/false, (COMPLAIN))
  157. #define convert_like_with_context(CONV, EXPR, FN, ARGNO, COMPLAIN ) \
  158. convert_like_real ((CONV), (EXPR), (FN), (ARGNO), 0, \
  159. /*issue_conversion_warnings=*/true, \
  160. /*c_cast_p=*/false, (COMPLAIN))
  161. static tree convert_like_real (conversion *, tree, tree, int, int, bool,
  162. bool, tsubst_flags_t);
  163. static void op_error (location_t, enum tree_code, enum tree_code, tree,
  164. tree, tree, bool);
  165. static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
  166. tsubst_flags_t);
  167. static void print_z_candidate (location_t, const char *, struct z_candidate *);
  168. static void print_z_candidates (location_t, struct z_candidate *);
  169. static tree build_this (tree);
  170. static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
  171. static bool any_strictly_viable (struct z_candidate *);
  172. static struct z_candidate *add_template_candidate
  173. (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
  174. tree, tree, tree, int, unification_kind_t, tsubst_flags_t);
  175. static struct z_candidate *add_template_candidate_real
  176. (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
  177. tree, tree, tree, int, tree, unification_kind_t, tsubst_flags_t);
  178. static struct z_candidate *add_template_conv_candidate
  179. (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *,
  180. tree, tree, tree, tsubst_flags_t);
  181. static void add_builtin_candidates
  182. (struct z_candidate **, enum tree_code, enum tree_code,
  183. tree, tree *, int, tsubst_flags_t);
  184. static void add_builtin_candidate
  185. (struct z_candidate **, enum tree_code, enum tree_code,
  186. tree, tree, tree, tree *, tree *, int, tsubst_flags_t);
  187. static bool is_complete (tree);
  188. static void build_builtin_candidate
  189. (struct z_candidate **, tree, tree, tree, tree *, tree *,
  190. int, tsubst_flags_t);
  191. static struct z_candidate *add_conv_candidate
  192. (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
  193. tree, tsubst_flags_t);
  194. static struct z_candidate *add_function_candidate
  195. (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
  196. tree, int, tsubst_flags_t);
  197. static conversion *implicit_conversion (tree, tree, tree, bool, int,
  198. tsubst_flags_t);
  199. static conversion *standard_conversion (tree, tree, tree, bool, int);
  200. static conversion *reference_binding (tree, tree, tree, bool, int,
  201. tsubst_flags_t);
  202. static conversion *build_conv (conversion_kind, tree, conversion *);
  203. static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
  204. static conversion *next_conversion (conversion *);
  205. static bool is_subseq (conversion *, conversion *);
  206. static conversion *maybe_handle_ref_bind (conversion **);
  207. static void maybe_handle_implicit_object (conversion **);
  208. static struct z_candidate *add_candidate
  209. (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
  210. conversion **, tree, tree, int, struct rejection_reason *, int);
  211. static tree source_type (conversion *);
  212. static void add_warning (struct z_candidate *, struct z_candidate *);
  213. static bool reference_compatible_p (tree, tree);
  214. static conversion *direct_reference_binding (tree, conversion *);
  215. static bool promoted_arithmetic_type_p (tree);
  216. static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
  217. static char *name_as_c_string (tree, tree, bool *);
  218. static tree prep_operand (tree);
  219. static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
  220. bool, tree, tree, int, struct z_candidate **,
  221. tsubst_flags_t);
  222. static conversion *merge_conversion_sequences (conversion *, conversion *);
  223. static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
  224. /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
  225. NAME can take many forms... */
  226. bool
  227. check_dtor_name (tree basetype, tree name)
  228. {
  229. /* Just accept something we've already complained about. */
  230. if (name == error_mark_node)
  231. return true;
  232. if (TREE_CODE (name) == TYPE_DECL)
  233. name = TREE_TYPE (name);
  234. else if (TYPE_P (name))
  235. /* OK */;
  236. else if (identifier_p (name))
  237. {
  238. if ((MAYBE_CLASS_TYPE_P (basetype)
  239. && name == constructor_name (basetype))
  240. || (TREE_CODE (basetype) == ENUMERAL_TYPE
  241. && name == TYPE_IDENTIFIER (basetype)))
  242. return true;
  243. else
  244. name = get_type_value (name);
  245. }
  246. else
  247. {
  248. /* In the case of:
  249. template <class T> struct S { ~S(); };
  250. int i;
  251. i.~S();
  252. NAME will be a class template. */
  253. gcc_assert (DECL_CLASS_TEMPLATE_P (name));
  254. return false;
  255. }
  256. if (!name || name == error_mark_node)
  257. return false;
  258. return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
  259. }
  260. /* We want the address of a function or method. We avoid creating a
  261. pointer-to-member function. */
  262. tree
  263. build_addr_func (tree function, tsubst_flags_t complain)
  264. {
  265. tree type = TREE_TYPE (function);
  266. /* We have to do these by hand to avoid real pointer to member
  267. functions. */
  268. if (TREE_CODE (type) == METHOD_TYPE)
  269. {
  270. if (TREE_CODE (function) == OFFSET_REF)
  271. {
  272. tree object = build_address (TREE_OPERAND (function, 0));
  273. return get_member_function_from_ptrfunc (&object,
  274. TREE_OPERAND (function, 1),
  275. complain);
  276. }
  277. function = build_address (function);
  278. }
  279. else
  280. function = decay_conversion (function, complain);
  281. return function;
  282. }
  283. /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
  284. POINTER_TYPE to those. Note, pointer to member function types
  285. (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
  286. two variants. build_call_a is the primitive taking an array of
  287. arguments, while build_call_n is a wrapper that handles varargs. */
  288. tree
  289. build_call_n (tree function, int n, ...)
  290. {
  291. if (n == 0)
  292. return build_call_a (function, 0, NULL);
  293. else
  294. {
  295. tree *argarray = XALLOCAVEC (tree, n);
  296. va_list ap;
  297. int i;
  298. va_start (ap, n);
  299. for (i = 0; i < n; i++)
  300. argarray[i] = va_arg (ap, tree);
  301. va_end (ap);
  302. return build_call_a (function, n, argarray);
  303. }
  304. }
  305. /* Update various flags in cfun and the call itself based on what is being
  306. called. Split out of build_call_a so that bot_manip can use it too. */
  307. void
  308. set_flags_from_callee (tree call)
  309. {
  310. bool nothrow;
  311. tree decl = get_callee_fndecl (call);
  312. /* We check both the decl and the type; a function may be known not to
  313. throw without being declared throw(). */
  314. nothrow = decl && TREE_NOTHROW (decl);
  315. if (CALL_EXPR_FN (call))
  316. nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (call))));
  317. else if (internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
  318. nothrow = true;
  319. if (!nothrow && at_function_scope_p () && cfun && cp_function_chain)
  320. cp_function_chain->can_throw = 1;
  321. if (decl && TREE_THIS_VOLATILE (decl) && cfun && cp_function_chain)
  322. current_function_returns_abnormally = 1;
  323. TREE_NOTHROW (call) = nothrow;
  324. }
  325. tree
  326. build_call_a (tree function, int n, tree *argarray)
  327. {
  328. tree decl;
  329. tree result_type;
  330. tree fntype;
  331. int i;
  332. function = build_addr_func (function, tf_warning_or_error);
  333. gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
  334. fntype = TREE_TYPE (TREE_TYPE (function));
  335. gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
  336. || TREE_CODE (fntype) == METHOD_TYPE);
  337. result_type = TREE_TYPE (fntype);
  338. /* An rvalue has no cv-qualifiers. */
  339. if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
  340. result_type = cv_unqualified (result_type);
  341. function = build_call_array_loc (input_location,
  342. result_type, function, n, argarray);
  343. set_flags_from_callee (function);
  344. decl = get_callee_fndecl (function);
  345. if (decl && !TREE_USED (decl))
  346. {
  347. /* We invoke build_call directly for several library
  348. functions. These may have been declared normally if
  349. we're building libgcc, so we can't just check
  350. DECL_ARTIFICIAL. */
  351. gcc_assert (DECL_ARTIFICIAL (decl)
  352. || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
  353. "__", 2));
  354. mark_used (decl);
  355. }
  356. require_complete_eh_spec_types (fntype, decl);
  357. TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
  358. /* Don't pass empty class objects by value. This is useful
  359. for tags in STL, which are used to control overload resolution.
  360. We don't need to handle other cases of copying empty classes. */
  361. if (! decl || ! DECL_BUILT_IN (decl))
  362. for (i = 0; i < n; i++)
  363. {
  364. tree arg = CALL_EXPR_ARG (function, i);
  365. if (is_empty_class (TREE_TYPE (arg))
  366. && ! TREE_ADDRESSABLE (TREE_TYPE (arg)))
  367. {
  368. tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
  369. arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
  370. CALL_EXPR_ARG (function, i) = arg;
  371. }
  372. }
  373. return function;
  374. }
  375. /* Build something of the form ptr->method (args)
  376. or object.method (args). This can also build
  377. calls to constructors, and find friends.
  378. Member functions always take their class variable
  379. as a pointer.
  380. INSTANCE is a class instance.
  381. NAME is the name of the method desired, usually an IDENTIFIER_NODE.
  382. PARMS help to figure out what that NAME really refers to.
  383. BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
  384. down to the real instance type to use for access checking. We need this
  385. information to get protected accesses correct.
  386. FLAGS is the logical disjunction of zero or more LOOKUP_
  387. flags. See cp-tree.h for more info.
  388. If this is all OK, calls build_function_call with the resolved
  389. member function.
  390. This function must also handle being called to perform
  391. initialization, promotion/coercion of arguments, and
  392. instantiation of default parameters.
  393. Note that NAME may refer to an instance variable name. If
  394. `operator()()' is defined for the type of that field, then we return
  395. that result. */
  396. /* New overloading code. */
  397. typedef struct z_candidate z_candidate;
  398. typedef struct candidate_warning candidate_warning;
  399. struct candidate_warning {
  400. z_candidate *loser;
  401. candidate_warning *next;
  402. };
  403. /* Information for providing diagnostics about why overloading failed. */
  404. enum rejection_reason_code {
  405. rr_none,
  406. rr_arity,
  407. rr_explicit_conversion,
  408. rr_template_conversion,
  409. rr_arg_conversion,
  410. rr_bad_arg_conversion,
  411. rr_template_unification,
  412. rr_invalid_copy
  413. };
  414. struct conversion_info {
  415. /* The index of the argument, 0-based. */
  416. int n_arg;
  417. /* The actual argument or its type. */
  418. tree from;
  419. /* The type of the parameter. */
  420. tree to_type;
  421. };
  422. struct rejection_reason {
  423. enum rejection_reason_code code;
  424. union {
  425. /* Information about an arity mismatch. */
  426. struct {
  427. /* The expected number of arguments. */
  428. int expected;
  429. /* The actual number of arguments in the call. */
  430. int actual;
  431. /* Whether the call was a varargs call. */
  432. bool call_varargs_p;
  433. } arity;
  434. /* Information about an argument conversion mismatch. */
  435. struct conversion_info conversion;
  436. /* Same, but for bad argument conversions. */
  437. struct conversion_info bad_conversion;
  438. /* Information about template unification failures. These are the
  439. parameters passed to fn_type_unification. */
  440. struct {
  441. tree tmpl;
  442. tree explicit_targs;
  443. int num_targs;
  444. const tree *args;
  445. unsigned int nargs;
  446. tree return_type;
  447. unification_kind_t strict;
  448. int flags;
  449. } template_unification;
  450. /* Information about template instantiation failures. These are the
  451. parameters passed to instantiate_template. */
  452. struct {
  453. tree tmpl;
  454. tree targs;
  455. } template_instantiation;
  456. } u;
  457. };
  458. struct z_candidate {
  459. /* The FUNCTION_DECL that will be called if this candidate is
  460. selected by overload resolution. */
  461. tree fn;
  462. /* If not NULL_TREE, the first argument to use when calling this
  463. function. */
  464. tree first_arg;
  465. /* The rest of the arguments to use when calling this function. If
  466. there are no further arguments this may be NULL or it may be an
  467. empty vector. */
  468. const vec<tree, va_gc> *args;
  469. /* The implicit conversion sequences for each of the arguments to
  470. FN. */
  471. conversion **convs;
  472. /* The number of implicit conversion sequences. */
  473. size_t num_convs;
  474. /* If FN is a user-defined conversion, the standard conversion
  475. sequence from the type returned by FN to the desired destination
  476. type. */
  477. conversion *second_conv;
  478. struct rejection_reason *reason;
  479. /* If FN is a member function, the binfo indicating the path used to
  480. qualify the name of FN at the call site. This path is used to
  481. determine whether or not FN is accessible if it is selected by
  482. overload resolution. The DECL_CONTEXT of FN will always be a
  483. (possibly improper) base of this binfo. */
  484. tree access_path;
  485. /* If FN is a non-static member function, the binfo indicating the
  486. subobject to which the `this' pointer should be converted if FN
  487. is selected by overload resolution. The type pointed to by
  488. the `this' pointer must correspond to the most derived class
  489. indicated by the CONVERSION_PATH. */
  490. tree conversion_path;
  491. tree template_decl;
  492. tree explicit_targs;
  493. candidate_warning *warnings;
  494. z_candidate *next;
  495. int viable;
  496. /* The flags active in add_candidate. */
  497. int flags;
  498. };
  499. /* Returns true iff T is a null pointer constant in the sense of
  500. [conv.ptr]. */
  501. bool
  502. null_ptr_cst_p (tree t)
  503. {
  504. /* [conv.ptr]
  505. A null pointer constant is an integral constant expression
  506. (_expr.const_) rvalue of integer type that evaluates to zero or
  507. an rvalue of type std::nullptr_t. */
  508. if (NULLPTR_TYPE_P (TREE_TYPE (t)))
  509. return true;
  510. if (CP_INTEGRAL_TYPE_P (TREE_TYPE (t)))
  511. {
  512. /* Core issue 903 says only literal 0 is a null pointer constant. */
  513. if (cxx_dialect < cxx11)
  514. t = fold_non_dependent_expr (t);
  515. STRIP_NOPS (t);
  516. if (integer_zerop (t) && !TREE_OVERFLOW (t))
  517. return true;
  518. }
  519. return false;
  520. }
  521. /* Returns true iff T is a null member pointer value (4.11). */
  522. bool
  523. null_member_pointer_value_p (tree t)
  524. {
  525. tree type = TREE_TYPE (t);
  526. if (!type)
  527. return false;
  528. else if (TYPE_PTRMEMFUNC_P (type))
  529. return (TREE_CODE (t) == CONSTRUCTOR
  530. && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
  531. else if (TYPE_PTRDATAMEM_P (type))
  532. return integer_all_onesp (t);
  533. else
  534. return false;
  535. }
  536. /* Returns nonzero if PARMLIST consists of only default parms,
  537. ellipsis, and/or undeduced parameter packs. */
  538. bool
  539. sufficient_parms_p (const_tree parmlist)
  540. {
  541. for (; parmlist && parmlist != void_list_node;
  542. parmlist = TREE_CHAIN (parmlist))
  543. if (!TREE_PURPOSE (parmlist)
  544. && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
  545. return false;
  546. return true;
  547. }
  548. /* Allocate N bytes of memory from the conversion obstack. The memory
  549. is zeroed before being returned. */
  550. static void *
  551. conversion_obstack_alloc (size_t n)
  552. {
  553. void *p;
  554. if (!conversion_obstack_initialized)
  555. {
  556. gcc_obstack_init (&conversion_obstack);
  557. conversion_obstack_initialized = true;
  558. }
  559. p = obstack_alloc (&conversion_obstack, n);
  560. memset (p, 0, n);
  561. return p;
  562. }
  563. /* Allocate rejection reasons. */
  564. static struct rejection_reason *
  565. alloc_rejection (enum rejection_reason_code code)
  566. {
  567. struct rejection_reason *p;
  568. p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
  569. p->code = code;
  570. return p;
  571. }
  572. static struct rejection_reason *
  573. arity_rejection (tree first_arg, int expected, int actual)
  574. {
  575. struct rejection_reason *r = alloc_rejection (rr_arity);
  576. int adjust = first_arg != NULL_TREE;
  577. r->u.arity.expected = expected - adjust;
  578. r->u.arity.actual = actual - adjust;
  579. return r;
  580. }
  581. static struct rejection_reason *
  582. arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
  583. {
  584. struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
  585. int adjust = first_arg != NULL_TREE;
  586. r->u.conversion.n_arg = n_arg - adjust;
  587. r->u.conversion.from = from;
  588. r->u.conversion.to_type = to;
  589. return r;
  590. }
  591. static struct rejection_reason *
  592. bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to)
  593. {
  594. struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
  595. int adjust = first_arg != NULL_TREE;
  596. r->u.bad_conversion.n_arg = n_arg - adjust;
  597. r->u.bad_conversion.from = from;
  598. r->u.bad_conversion.to_type = to;
  599. return r;
  600. }
  601. static struct rejection_reason *
  602. explicit_conversion_rejection (tree from, tree to)
  603. {
  604. struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
  605. r->u.conversion.n_arg = 0;
  606. r->u.conversion.from = from;
  607. r->u.conversion.to_type = to;
  608. return r;
  609. }
  610. static struct rejection_reason *
  611. template_conversion_rejection (tree from, tree to)
  612. {
  613. struct rejection_reason *r = alloc_rejection (rr_template_conversion);
  614. r->u.conversion.n_arg = 0;
  615. r->u.conversion.from = from;
  616. r->u.conversion.to_type = to;
  617. return r;
  618. }
  619. static struct rejection_reason *
  620. template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
  621. const tree *args, unsigned int nargs,
  622. tree return_type, unification_kind_t strict,
  623. int flags)
  624. {
  625. size_t args_n_bytes = sizeof (*args) * nargs;
  626. tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
  627. struct rejection_reason *r = alloc_rejection (rr_template_unification);
  628. r->u.template_unification.tmpl = tmpl;
  629. r->u.template_unification.explicit_targs = explicit_targs;
  630. r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
  631. /* Copy args to our own storage. */
  632. memcpy (args1, args, args_n_bytes);
  633. r->u.template_unification.args = args1;
  634. r->u.template_unification.nargs = nargs;
  635. r->u.template_unification.return_type = return_type;
  636. r->u.template_unification.strict = strict;
  637. r->u.template_unification.flags = flags;
  638. return r;
  639. }
  640. static struct rejection_reason *
  641. template_unification_error_rejection (void)
  642. {
  643. return alloc_rejection (rr_template_unification);
  644. }
  645. static struct rejection_reason *
  646. invalid_copy_with_fn_template_rejection (void)
  647. {
  648. struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
  649. return r;
  650. }
  651. /* Dynamically allocate a conversion. */
  652. static conversion *
  653. alloc_conversion (conversion_kind kind)
  654. {
  655. conversion *c;
  656. c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
  657. c->kind = kind;
  658. return c;
  659. }
  660. #ifdef ENABLE_CHECKING
  661. /* Make sure that all memory on the conversion obstack has been
  662. freed. */
  663. void
  664. validate_conversion_obstack (void)
  665. {
  666. if (conversion_obstack_initialized)
  667. gcc_assert ((obstack_next_free (&conversion_obstack)
  668. == obstack_base (&conversion_obstack)));
  669. }
  670. #endif /* ENABLE_CHECKING */
  671. /* Dynamically allocate an array of N conversions. */
  672. static conversion **
  673. alloc_conversions (size_t n)
  674. {
  675. return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
  676. }
  677. static conversion *
  678. build_conv (conversion_kind code, tree type, conversion *from)
  679. {
  680. conversion *t;
  681. conversion_rank rank = CONVERSION_RANK (from);
  682. /* Note that the caller is responsible for filling in t->cand for
  683. user-defined conversions. */
  684. t = alloc_conversion (code);
  685. t->type = type;
  686. t->u.next = from;
  687. switch (code)
  688. {
  689. case ck_ptr:
  690. case ck_pmem:
  691. case ck_base:
  692. case ck_std:
  693. if (rank < cr_std)
  694. rank = cr_std;
  695. break;
  696. case ck_qual:
  697. if (rank < cr_exact)
  698. rank = cr_exact;
  699. break;
  700. default:
  701. break;
  702. }
  703. t->rank = rank;
  704. t->user_conv_p = (code == ck_user || from->user_conv_p);
  705. t->bad_p = from->bad_p;
  706. t->base_p = false;
  707. return t;
  708. }
  709. /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
  710. specialization of std::initializer_list<T>, if such a conversion is
  711. possible. */
  712. static conversion *
  713. build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
  714. {
  715. tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
  716. unsigned len = CONSTRUCTOR_NELTS (ctor);
  717. conversion **subconvs = alloc_conversions (len);
  718. conversion *t;
  719. unsigned i;
  720. tree val;
  721. /* Within a list-initialization we can have more user-defined
  722. conversions. */
  723. flags &= ~LOOKUP_NO_CONVERSION;
  724. /* But no narrowing conversions. */
  725. flags |= LOOKUP_NO_NARROWING;
  726. /* Can't make an array of these types. */
  727. if (TREE_CODE (elttype) == REFERENCE_TYPE
  728. || TREE_CODE (elttype) == FUNCTION_TYPE
  729. || VOID_TYPE_P (elttype))
  730. return NULL;
  731. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
  732. {
  733. conversion *sub
  734. = implicit_conversion (elttype, TREE_TYPE (val), val,
  735. false, flags, complain);
  736. if (sub == NULL)
  737. return NULL;
  738. subconvs[i] = sub;
  739. }
  740. t = alloc_conversion (ck_list);
  741. t->type = type;
  742. t->u.list = subconvs;
  743. t->rank = cr_exact;
  744. for (i = 0; i < len; ++i)
  745. {
  746. conversion *sub = subconvs[i];
  747. if (sub->rank > t->rank)
  748. t->rank = sub->rank;
  749. if (sub->user_conv_p)
  750. t->user_conv_p = true;
  751. if (sub->bad_p)
  752. t->bad_p = true;
  753. }
  754. return t;
  755. }
  756. /* Return the next conversion of the conversion chain (if applicable),
  757. or NULL otherwise. Please use this function instead of directly
  758. accessing fields of struct conversion. */
  759. static conversion *
  760. next_conversion (conversion *conv)
  761. {
  762. if (conv == NULL
  763. || conv->kind == ck_identity
  764. || conv->kind == ck_ambig
  765. || conv->kind == ck_list)
  766. return NULL;
  767. return conv->u.next;
  768. }
  769. /* Subroutine of build_aggr_conv: check whether CTOR, a braced-init-list,
  770. is a valid aggregate initializer for array type ATYPE. */
  771. static bool
  772. can_convert_array (tree atype, tree ctor, int flags, tsubst_flags_t complain)
  773. {
  774. unsigned i;
  775. tree elttype = TREE_TYPE (atype);
  776. for (i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
  777. {
  778. tree val = CONSTRUCTOR_ELT (ctor, i)->value;
  779. bool ok;
  780. if (TREE_CODE (elttype) == ARRAY_TYPE
  781. && TREE_CODE (val) == CONSTRUCTOR)
  782. ok = can_convert_array (elttype, val, flags, complain);
  783. else
  784. ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
  785. complain);
  786. if (!ok)
  787. return false;
  788. }
  789. return true;
  790. }
  791. /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
  792. aggregate class, if such a conversion is possible. */
  793. static conversion *
  794. build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
  795. {
  796. unsigned HOST_WIDE_INT i = 0;
  797. conversion *c;
  798. tree field = next_initializable_field (TYPE_FIELDS (type));
  799. tree empty_ctor = NULL_TREE;
  800. /* We already called reshape_init in implicit_conversion. */
  801. /* The conversions within the init-list aren't affected by the enclosing
  802. context; they're always simple copy-initialization. */
  803. flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
  804. for (; field; field = next_initializable_field (DECL_CHAIN (field)))
  805. {
  806. tree ftype = TREE_TYPE (field);
  807. tree val;
  808. bool ok;
  809. if (i < CONSTRUCTOR_NELTS (ctor))
  810. val = CONSTRUCTOR_ELT (ctor, i)->value;
  811. else if (TREE_CODE (ftype) == REFERENCE_TYPE)
  812. /* Value-initialization of reference is ill-formed. */
  813. return NULL;
  814. else
  815. {
  816. if (empty_ctor == NULL_TREE)
  817. empty_ctor = build_constructor (init_list_type_node, NULL);
  818. val = empty_ctor;
  819. }
  820. ++i;
  821. if (TREE_CODE (ftype) == ARRAY_TYPE
  822. && TREE_CODE (val) == CONSTRUCTOR)
  823. ok = can_convert_array (ftype, val, flags, complain);
  824. else
  825. ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
  826. complain);
  827. if (!ok)
  828. return NULL;
  829. if (TREE_CODE (type) == UNION_TYPE)
  830. break;
  831. }
  832. if (i < CONSTRUCTOR_NELTS (ctor))
  833. return NULL;
  834. c = alloc_conversion (ck_aggr);
  835. c->type = type;
  836. c->rank = cr_exact;
  837. c->user_conv_p = true;
  838. c->check_narrowing = true;
  839. c->u.next = NULL;
  840. return c;
  841. }
  842. /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
  843. array type, if such a conversion is possible. */
  844. static conversion *
  845. build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
  846. {
  847. conversion *c;
  848. unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
  849. tree elttype = TREE_TYPE (type);
  850. unsigned i;
  851. tree val;
  852. bool bad = false;
  853. bool user = false;
  854. enum conversion_rank rank = cr_exact;
  855. /* We might need to propagate the size from the element to the array. */
  856. complete_type (type);
  857. if (TYPE_DOMAIN (type)
  858. && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
  859. {
  860. unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
  861. if (alen < len)
  862. return NULL;
  863. }
  864. flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
  865. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
  866. {
  867. conversion *sub
  868. = implicit_conversion (elttype, TREE_TYPE (val), val,
  869. false, flags, complain);
  870. if (sub == NULL)
  871. return NULL;
  872. if (sub->rank > rank)
  873. rank = sub->rank;
  874. if (sub->user_conv_p)
  875. user = true;
  876. if (sub->bad_p)
  877. bad = true;
  878. }
  879. c = alloc_conversion (ck_aggr);
  880. c->type = type;
  881. c->rank = rank;
  882. c->user_conv_p = user;
  883. c->bad_p = bad;
  884. c->u.next = NULL;
  885. return c;
  886. }
  887. /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
  888. complex type, if such a conversion is possible. */
  889. static conversion *
  890. build_complex_conv (tree type, tree ctor, int flags,
  891. tsubst_flags_t complain)
  892. {
  893. conversion *c;
  894. unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
  895. tree elttype = TREE_TYPE (type);
  896. unsigned i;
  897. tree val;
  898. bool bad = false;
  899. bool user = false;
  900. enum conversion_rank rank = cr_exact;
  901. if (len != 2)
  902. return NULL;
  903. flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
  904. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
  905. {
  906. conversion *sub
  907. = implicit_conversion (elttype, TREE_TYPE (val), val,
  908. false, flags, complain);
  909. if (sub == NULL)
  910. return NULL;
  911. if (sub->rank > rank)
  912. rank = sub->rank;
  913. if (sub->user_conv_p)
  914. user = true;
  915. if (sub->bad_p)
  916. bad = true;
  917. }
  918. c = alloc_conversion (ck_aggr);
  919. c->type = type;
  920. c->rank = rank;
  921. c->user_conv_p = user;
  922. c->bad_p = bad;
  923. c->u.next = NULL;
  924. return c;
  925. }
  926. /* Build a representation of the identity conversion from EXPR to
  927. itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
  928. static conversion *
  929. build_identity_conv (tree type, tree expr)
  930. {
  931. conversion *c;
  932. c = alloc_conversion (ck_identity);
  933. c->type = type;
  934. c->u.expr = expr;
  935. return c;
  936. }
  937. /* Converting from EXPR to TYPE was ambiguous in the sense that there
  938. were multiple user-defined conversions to accomplish the job.
  939. Build a conversion that indicates that ambiguity. */
  940. static conversion *
  941. build_ambiguous_conv (tree type, tree expr)
  942. {
  943. conversion *c;
  944. c = alloc_conversion (ck_ambig);
  945. c->type = type;
  946. c->u.expr = expr;
  947. return c;
  948. }
  949. tree
  950. strip_top_quals (tree t)
  951. {
  952. if (TREE_CODE (t) == ARRAY_TYPE)
  953. return t;
  954. return cp_build_qualified_type (t, 0);
  955. }
  956. /* Returns the standard conversion path (see [conv]) from type FROM to type
  957. TO, if any. For proper handling of null pointer constants, you must
  958. also pass the expression EXPR to convert from. If C_CAST_P is true,
  959. this conversion is coming from a C-style cast. */
  960. static conversion *
  961. standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
  962. int flags)
  963. {
  964. enum tree_code fcode, tcode;
  965. conversion *conv;
  966. bool fromref = false;
  967. tree qualified_to;
  968. to = non_reference (to);
  969. if (TREE_CODE (from) == REFERENCE_TYPE)
  970. {
  971. fromref = true;
  972. from = TREE_TYPE (from);
  973. }
  974. qualified_to = to;
  975. to = strip_top_quals (to);
  976. from = strip_top_quals (from);
  977. if (expr && type_unknown_p (expr))
  978. {
  979. if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
  980. {
  981. tsubst_flags_t tflags = tf_conv;
  982. expr = instantiate_type (to, expr, tflags);
  983. if (expr == error_mark_node)
  984. return NULL;
  985. from = TREE_TYPE (expr);
  986. }
  987. else if (TREE_CODE (to) == BOOLEAN_TYPE)
  988. {
  989. /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
  990. expr = resolve_nondeduced_context (expr);
  991. from = TREE_TYPE (expr);
  992. }
  993. }
  994. fcode = TREE_CODE (from);
  995. tcode = TREE_CODE (to);
  996. conv = build_identity_conv (from, expr);
  997. if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
  998. {
  999. from = type_decays_to (from);
  1000. fcode = TREE_CODE (from);
  1001. conv = build_conv (ck_lvalue, from, conv);
  1002. }
  1003. else if (fromref || (expr && lvalue_p (expr)))
  1004. {
  1005. if (expr)
  1006. {
  1007. tree bitfield_type;
  1008. bitfield_type = is_bitfield_expr_with_lowered_type (expr);
  1009. if (bitfield_type)
  1010. {
  1011. from = strip_top_quals (bitfield_type);
  1012. fcode = TREE_CODE (from);
  1013. }
  1014. }
  1015. conv = build_conv (ck_rvalue, from, conv);
  1016. if (flags & LOOKUP_PREFER_RVALUE)
  1017. conv->rvaluedness_matches_p = true;
  1018. }
  1019. /* Allow conversion between `__complex__' data types. */
  1020. if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
  1021. {
  1022. /* The standard conversion sequence to convert FROM to TO is
  1023. the standard conversion sequence to perform componentwise
  1024. conversion. */
  1025. conversion *part_conv = standard_conversion
  1026. (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags);
  1027. if (part_conv)
  1028. {
  1029. conv = build_conv (part_conv->kind, to, conv);
  1030. conv->rank = part_conv->rank;
  1031. }
  1032. else
  1033. conv = NULL;
  1034. return conv;
  1035. }
  1036. if (same_type_p (from, to))
  1037. {
  1038. if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
  1039. conv->type = qualified_to;
  1040. return conv;
  1041. }
  1042. /* [conv.ptr]
  1043. A null pointer constant can be converted to a pointer type; ... A
  1044. null pointer constant of integral type can be converted to an
  1045. rvalue of type std::nullptr_t. */
  1046. if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
  1047. || NULLPTR_TYPE_P (to))
  1048. && ((expr && null_ptr_cst_p (expr))
  1049. || NULLPTR_TYPE_P (from)))
  1050. conv = build_conv (ck_std, to, conv);
  1051. else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
  1052. || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
  1053. {
  1054. /* For backwards brain damage compatibility, allow interconversion of
  1055. pointers and integers with a pedwarn. */
  1056. conv = build_conv (ck_std, to, conv);
  1057. conv->bad_p = true;
  1058. }
  1059. else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
  1060. {
  1061. /* For backwards brain damage compatibility, allow interconversion of
  1062. enums and integers with a pedwarn. */
  1063. conv = build_conv (ck_std, to, conv);
  1064. conv->bad_p = true;
  1065. }
  1066. else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
  1067. || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
  1068. {
  1069. tree to_pointee;
  1070. tree from_pointee;
  1071. if (tcode == POINTER_TYPE
  1072. && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (from),
  1073. TREE_TYPE (to)))
  1074. ;
  1075. else if (VOID_TYPE_P (TREE_TYPE (to))
  1076. && !TYPE_PTRDATAMEM_P (from)
  1077. && TREE_CODE (TREE_TYPE (from)) != FUNCTION_TYPE)
  1078. {
  1079. tree nfrom = TREE_TYPE (from);
  1080. /* Don't try to apply restrict to void. */
  1081. int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
  1082. from = build_pointer_type
  1083. (cp_build_qualified_type (void_type_node, quals));
  1084. conv = build_conv (ck_ptr, from, conv);
  1085. }
  1086. else if (TYPE_PTRDATAMEM_P (from))
  1087. {
  1088. tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
  1089. tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
  1090. if (DERIVED_FROM_P (fbase, tbase)
  1091. && (same_type_ignoring_top_level_qualifiers_p
  1092. (TYPE_PTRMEM_POINTED_TO_TYPE (from),
  1093. TYPE_PTRMEM_POINTED_TO_TYPE (to))))
  1094. {
  1095. from = build_ptrmem_type (tbase,
  1096. TYPE_PTRMEM_POINTED_TO_TYPE (from));
  1097. conv = build_conv (ck_pmem, from, conv);
  1098. }
  1099. else if (!same_type_p (fbase, tbase))
  1100. return NULL;
  1101. }
  1102. else if (CLASS_TYPE_P (TREE_TYPE (from))
  1103. && CLASS_TYPE_P (TREE_TYPE (to))
  1104. /* [conv.ptr]
  1105. An rvalue of type "pointer to cv D," where D is a
  1106. class type, can be converted to an rvalue of type
  1107. "pointer to cv B," where B is a base class (clause
  1108. _class.derived_) of D. If B is an inaccessible
  1109. (clause _class.access_) or ambiguous
  1110. (_class.member.lookup_) base class of D, a program
  1111. that necessitates this conversion is ill-formed.
  1112. Therefore, we use DERIVED_FROM_P, and do not check
  1113. access or uniqueness. */
  1114. && DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
  1115. {
  1116. from =
  1117. cp_build_qualified_type (TREE_TYPE (to),
  1118. cp_type_quals (TREE_TYPE (from)));
  1119. from = build_pointer_type (from);
  1120. conv = build_conv (ck_ptr, from, conv);
  1121. conv->base_p = true;
  1122. }
  1123. if (tcode == POINTER_TYPE)
  1124. {
  1125. to_pointee = TREE_TYPE (to);
  1126. from_pointee = TREE_TYPE (from);
  1127. }
  1128. else
  1129. {
  1130. to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
  1131. from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
  1132. }
  1133. if (same_type_p (from, to))
  1134. /* OK */;
  1135. else if (c_cast_p && comp_ptr_ttypes_const (to, from))
  1136. /* In a C-style cast, we ignore CV-qualification because we
  1137. are allowed to perform a static_cast followed by a
  1138. const_cast. */
  1139. conv = build_conv (ck_qual, to, conv);
  1140. else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
  1141. conv = build_conv (ck_qual, to, conv);
  1142. else if (expr && string_conv_p (to, expr, 0))
  1143. /* converting from string constant to char *. */
  1144. conv = build_conv (ck_qual, to, conv);
  1145. /* Allow conversions among compatible ObjC pointer types (base
  1146. conversions have been already handled above). */
  1147. else if (c_dialect_objc ()
  1148. && objc_compare_types (to, from, -4, NULL_TREE))
  1149. conv = build_conv (ck_ptr, to, conv);
  1150. else if (ptr_reasonably_similar (to_pointee, from_pointee))
  1151. {
  1152. conv = build_conv (ck_ptr, to, conv);
  1153. conv->bad_p = true;
  1154. }
  1155. else
  1156. return NULL;
  1157. from = to;
  1158. }
  1159. else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
  1160. {
  1161. tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
  1162. tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
  1163. tree fbase = class_of_this_parm (fromfn);
  1164. tree tbase = class_of_this_parm (tofn);
  1165. if (!DERIVED_FROM_P (fbase, tbase)
  1166. || !same_type_p (static_fn_type (fromfn),
  1167. static_fn_type (tofn)))
  1168. return NULL;
  1169. from = build_memfn_type (fromfn,
  1170. tbase,
  1171. cp_type_quals (tbase),
  1172. type_memfn_rqual (tofn));
  1173. from = build_ptrmemfunc_type (build_pointer_type (from));
  1174. conv = build_conv (ck_pmem, from, conv);
  1175. conv->base_p = true;
  1176. }
  1177. else if (tcode == BOOLEAN_TYPE)
  1178. {
  1179. /* [conv.bool]
  1180. A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
  1181. to member type can be converted to a prvalue of type bool. ...
  1182. For direct-initialization (8.5 [dcl.init]), a prvalue of type
  1183. std::nullptr_t can be converted to a prvalue of type bool; */
  1184. if (ARITHMETIC_TYPE_P (from)
  1185. || UNSCOPED_ENUM_P (from)
  1186. || fcode == POINTER_TYPE
  1187. || TYPE_PTRMEM_P (from)
  1188. || NULLPTR_TYPE_P (from))
  1189. {
  1190. conv = build_conv (ck_std, to, conv);
  1191. if (fcode == POINTER_TYPE
  1192. || TYPE_PTRDATAMEM_P (from)
  1193. || (TYPE_PTRMEMFUNC_P (from)
  1194. && conv->rank < cr_pbool)
  1195. || NULLPTR_TYPE_P (from))
  1196. conv->rank = cr_pbool;
  1197. if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
  1198. conv->bad_p = true;
  1199. return conv;
  1200. }
  1201. return NULL;
  1202. }
  1203. /* We don't check for ENUMERAL_TYPE here because there are no standard
  1204. conversions to enum type. */
  1205. /* As an extension, allow conversion to complex type. */
  1206. else if (ARITHMETIC_TYPE_P (to))
  1207. {
  1208. if (! (INTEGRAL_CODE_P (fcode)
  1209. || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
  1210. || SCOPED_ENUM_P (from))
  1211. return NULL;
  1212. conv = build_conv (ck_std, to, conv);
  1213. /* Give this a better rank if it's a promotion. */
  1214. if (same_type_p (to, type_promotes_to (from))
  1215. && next_conversion (conv)->rank <= cr_promotion)
  1216. conv->rank = cr_promotion;
  1217. }
  1218. else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
  1219. && vector_types_convertible_p (from, to, false))
  1220. return build_conv (ck_std, to, conv);
  1221. else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
  1222. && is_properly_derived_from (from, to))
  1223. {
  1224. if (conv->kind == ck_rvalue)
  1225. conv = next_conversion (conv);
  1226. conv = build_conv (ck_base, to, conv);
  1227. /* The derived-to-base conversion indicates the initialization
  1228. of a parameter with base type from an object of a derived
  1229. type. A temporary object is created to hold the result of
  1230. the conversion unless we're binding directly to a reference. */
  1231. conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
  1232. }
  1233. else
  1234. return NULL;
  1235. if (flags & LOOKUP_NO_NARROWING)
  1236. conv->check_narrowing = true;
  1237. return conv;
  1238. }
  1239. /* Returns nonzero if T1 is reference-related to T2. */
  1240. bool
  1241. reference_related_p (tree t1, tree t2)
  1242. {
  1243. if (t1 == error_mark_node || t2 == error_mark_node)
  1244. return false;
  1245. t1 = TYPE_MAIN_VARIANT (t1);
  1246. t2 = TYPE_MAIN_VARIANT (t2);
  1247. /* [dcl.init.ref]
  1248. Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
  1249. to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
  1250. of T2. */
  1251. return (same_type_p (t1, t2)
  1252. || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
  1253. && DERIVED_FROM_P (t1, t2)));
  1254. }
  1255. /* Returns nonzero if T1 is reference-compatible with T2. */
  1256. static bool
  1257. reference_compatible_p (tree t1, tree t2)
  1258. {
  1259. /* [dcl.init.ref]
  1260. "cv1 T1" is reference compatible with "cv2 T2" if T1 is
  1261. reference-related to T2 and cv1 is the same cv-qualification as,
  1262. or greater cv-qualification than, cv2. */
  1263. return (reference_related_p (t1, t2)
  1264. && at_least_as_qualified_p (t1, t2));
  1265. }
  1266. /* A reference of the indicated TYPE is being bound directly to the
  1267. expression represented by the implicit conversion sequence CONV.
  1268. Return a conversion sequence for this binding. */
  1269. static conversion *
  1270. direct_reference_binding (tree type, conversion *conv)
  1271. {
  1272. tree t;
  1273. gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
  1274. gcc_assert (TREE_CODE (conv->type) != REFERENCE_TYPE);
  1275. t = TREE_TYPE (type);
  1276. /* [over.ics.rank]
  1277. When a parameter of reference type binds directly
  1278. (_dcl.init.ref_) to an argument expression, the implicit
  1279. conversion sequence is the identity conversion, unless the
  1280. argument expression has a type that is a derived class of the
  1281. parameter type, in which case the implicit conversion sequence is
  1282. a derived-to-base Conversion.
  1283. If the parameter binds directly to the result of applying a
  1284. conversion function to the argument expression, the implicit
  1285. conversion sequence is a user-defined conversion sequence
  1286. (_over.ics.user_), with the second standard conversion sequence
  1287. either an identity conversion or, if the conversion function
  1288. returns an entity of a type that is a derived class of the
  1289. parameter type, a derived-to-base conversion. */
  1290. if (!same_type_ignoring_top_level_qualifiers_p (t, conv->type))
  1291. {
  1292. /* Represent the derived-to-base conversion. */
  1293. conv = build_conv (ck_base, t, conv);
  1294. /* We will actually be binding to the base-class subobject in
  1295. the derived class, so we mark this conversion appropriately.
  1296. That way, convert_like knows not to generate a temporary. */
  1297. conv->need_temporary_p = false;
  1298. }
  1299. return build_conv (ck_ref_bind, type, conv);
  1300. }
  1301. /* Returns the conversion path from type FROM to reference type TO for
  1302. purposes of reference binding. For lvalue binding, either pass a
  1303. reference type to FROM or an lvalue expression to EXPR. If the
  1304. reference will be bound to a temporary, NEED_TEMPORARY_P is set for
  1305. the conversion returned. If C_CAST_P is true, this
  1306. conversion is coming from a C-style cast. */
  1307. static conversion *
  1308. reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
  1309. tsubst_flags_t complain)
  1310. {
  1311. conversion *conv = NULL;
  1312. tree to = TREE_TYPE (rto);
  1313. tree from = rfrom;
  1314. tree tfrom;
  1315. bool related_p;
  1316. bool compatible_p;
  1317. cp_lvalue_kind gl_kind;
  1318. bool is_lvalue;
  1319. if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
  1320. {
  1321. expr = instantiate_type (to, expr, tf_none);
  1322. if (expr == error_mark_node)
  1323. return NULL;
  1324. from = TREE_TYPE (expr);
  1325. }
  1326. if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
  1327. {
  1328. maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
  1329. /* DR 1288: Otherwise, if the initializer list has a single element
  1330. of type E and ... [T's] referenced type is reference-related to E,
  1331. the object or reference is initialized from that element... */
  1332. if (CONSTRUCTOR_NELTS (expr) == 1)
  1333. {
  1334. tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
  1335. if (error_operand_p (elt))
  1336. return NULL;
  1337. tree etype = TREE_TYPE (elt);
  1338. if (reference_related_p (to, etype))
  1339. {
  1340. expr = elt;
  1341. from = etype;
  1342. goto skip;
  1343. }
  1344. }
  1345. /* Otherwise, if T is a reference type, a prvalue temporary of the
  1346. type referenced by T is copy-list-initialized or
  1347. direct-list-initialized, depending on the kind of initialization
  1348. for the reference, and the reference is bound to that temporary. */
  1349. conv = implicit_conversion (to, from, expr, c_cast_p,
  1350. flags|LOOKUP_NO_TEMP_BIND, complain);
  1351. skip:;
  1352. }
  1353. if (TREE_CODE (from) == REFERENCE_TYPE)
  1354. {
  1355. from = TREE_TYPE (from);
  1356. if (!TYPE_REF_IS_RVALUE (rfrom)
  1357. || TREE_CODE (from) == FUNCTION_TYPE)
  1358. gl_kind = clk_ordinary;
  1359. else
  1360. gl_kind = clk_rvalueref;
  1361. }
  1362. else if (expr)
  1363. {
  1364. gl_kind = lvalue_kind (expr);
  1365. if (gl_kind & clk_class)
  1366. /* A class prvalue is not a glvalue. */
  1367. gl_kind = clk_none;
  1368. }
  1369. else
  1370. gl_kind = clk_none;
  1371. is_lvalue = gl_kind && !(gl_kind & clk_rvalueref);
  1372. tfrom = from;
  1373. if ((gl_kind & clk_bitfield) != 0)
  1374. tfrom = unlowered_expr_type (expr);
  1375. /* Figure out whether or not the types are reference-related and
  1376. reference compatible. We have do do this after stripping
  1377. references from FROM. */
  1378. related_p = reference_related_p (to, tfrom);
  1379. /* If this is a C cast, first convert to an appropriately qualified
  1380. type, so that we can later do a const_cast to the desired type. */
  1381. if (related_p && c_cast_p
  1382. && !at_least_as_qualified_p (to, tfrom))
  1383. to = cp_build_qualified_type (to, cp_type_quals (tfrom));
  1384. compatible_p = reference_compatible_p (to, tfrom);
  1385. /* Directly bind reference when target expression's type is compatible with
  1386. the reference and expression is an lvalue. In DR391, the wording in
  1387. [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
  1388. const and rvalue references to rvalues of compatible class type.
  1389. We should also do direct bindings for non-class xvalues. */
  1390. if (related_p
  1391. && (gl_kind
  1392. || (!(flags & LOOKUP_NO_TEMP_BIND)
  1393. && (CLASS_TYPE_P (from)
  1394. || TREE_CODE (from) == ARRAY_TYPE))))
  1395. {
  1396. /* [dcl.init.ref]
  1397. If the initializer expression
  1398. -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
  1399. is reference-compatible with "cv2 T2,"
  1400. the reference is bound directly to the initializer expression
  1401. lvalue.
  1402. [...]
  1403. If the initializer expression is an rvalue, with T2 a class type,
  1404. and "cv1 T1" is reference-compatible with "cv2 T2", the reference
  1405. is bound to the object represented by the rvalue or to a sub-object
  1406. within that object. */
  1407. conv = build_identity_conv (tfrom, expr);
  1408. conv = direct_reference_binding (rto, conv);
  1409. if (flags & LOOKUP_PREFER_RVALUE)
  1410. /* The top-level caller requested that we pretend that the lvalue
  1411. be treated as an rvalue. */
  1412. conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
  1413. else if (TREE_CODE (rfrom) == REFERENCE_TYPE)
  1414. /* Handle rvalue reference to function properly. */
  1415. conv->rvaluedness_matches_p
  1416. = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
  1417. else
  1418. conv->rvaluedness_matches_p
  1419. = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
  1420. if ((gl_kind & clk_bitfield) != 0
  1421. || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
  1422. /* For the purposes of overload resolution, we ignore the fact
  1423. this expression is a bitfield or packed field. (In particular,
  1424. [over.ics.ref] says specifically that a function with a
  1425. non-const reference parameter is viable even if the
  1426. argument is a bitfield.)
  1427. However, when we actually call the function we must create
  1428. a temporary to which to bind the reference. If the
  1429. reference is volatile, or isn't const, then we cannot make
  1430. a temporary, so we just issue an error when the conversion
  1431. actually occurs. */
  1432. conv->need_temporary_p = true;
  1433. /* Don't allow binding of lvalues (other than function lvalues) to
  1434. rvalue references. */
  1435. if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
  1436. && TREE_CODE (to) != FUNCTION_TYPE
  1437. && !(flags & LOOKUP_PREFER_RVALUE))
  1438. conv->bad_p = true;
  1439. /* Nor the reverse. */
  1440. if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
  1441. && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
  1442. || (flags & LOOKUP_NO_RVAL_BIND))
  1443. && TREE_CODE (to) != FUNCTION_TYPE)
  1444. conv->bad_p = true;
  1445. if (!compatible_p)
  1446. conv->bad_p = true;
  1447. return conv;
  1448. }
  1449. /* [class.conv.fct] A conversion function is never used to convert a
  1450. (possibly cv-qualified) object to the (possibly cv-qualified) same
  1451. object type (or a reference to it), to a (possibly cv-qualified) base
  1452. class of that type (or a reference to it).... */
  1453. else if (CLASS_TYPE_P (from) && !related_p
  1454. && !(flags & LOOKUP_NO_CONVERSION))
  1455. {
  1456. /* [dcl.init.ref]
  1457. If the initializer expression
  1458. -- has a class type (i.e., T2 is a class type) can be
  1459. implicitly converted to an lvalue of type "cv3 T3," where
  1460. "cv1 T1" is reference-compatible with "cv3 T3". (this
  1461. conversion is selected by enumerating the applicable
  1462. conversion functions (_over.match.ref_) and choosing the
  1463. best one through overload resolution. (_over.match_).
  1464. the reference is bound to the lvalue result of the conversion
  1465. in the second case. */
  1466. z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
  1467. complain);
  1468. if (cand)
  1469. return cand->second_conv;
  1470. }
  1471. /* From this point on, we conceptually need temporaries, even if we
  1472. elide them. Only the cases above are "direct bindings". */
  1473. if (flags & LOOKUP_NO_TEMP_BIND)
  1474. return NULL;
  1475. /* [over.ics.rank]
  1476. When a parameter of reference type is not bound directly to an
  1477. argument expression, the conversion sequence is the one required
  1478. to convert the argument expression to the underlying type of the
  1479. reference according to _over.best.ics_. Conceptually, this
  1480. conversion sequence corresponds to copy-initializing a temporary
  1481. of the underlying type with the argument expression. Any
  1482. difference in top-level cv-qualification is subsumed by the
  1483. initialization itself and does not constitute a conversion. */
  1484. /* [dcl.init.ref]
  1485. Otherwise, the reference shall be an lvalue reference to a
  1486. non-volatile const type, or the reference shall be an rvalue
  1487. reference.
  1488. We try below to treat this as a bad conversion to improve diagnostics,
  1489. but if TO is an incomplete class, we need to reject this conversion
  1490. now to avoid unnecessary instantiation. */
  1491. if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)
  1492. && !COMPLETE_TYPE_P (to))
  1493. return NULL;
  1494. /* We're generating a temporary now, but don't bind any more in the
  1495. conversion (specifically, don't slice the temporary returned by a
  1496. conversion operator). */
  1497. flags |= LOOKUP_NO_TEMP_BIND;
  1498. /* Core issue 899: When [copy-]initializing a temporary to be bound
  1499. to the first parameter of a copy constructor (12.8) called with
  1500. a single argument in the context of direct-initialization,
  1501. explicit conversion functions are also considered.
  1502. So don't set LOOKUP_ONLYCONVERTING in that case. */
  1503. if (!(flags & LOOKUP_COPY_PARM))
  1504. flags |= LOOKUP_ONLYCONVERTING;
  1505. if (!conv)
  1506. conv = implicit_conversion (to, from, expr, c_cast_p,
  1507. flags, complain);
  1508. if (!conv)
  1509. return NULL;
  1510. if (conv->user_conv_p)
  1511. {
  1512. /* If initializing the temporary used a conversion function,
  1513. recalculate the second conversion sequence. */
  1514. for (conversion *t = conv; t; t = next_conversion (t))
  1515. if (t->kind == ck_user
  1516. && DECL_CONV_FN_P (t->cand->fn))
  1517. {
  1518. tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
  1519. int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
  1520. conversion *new_second
  1521. = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
  1522. sflags, complain);
  1523. if (!new_second)
  1524. return NULL;
  1525. return merge_conversion_sequences (t, new_second);
  1526. }
  1527. }
  1528. conv = build_conv (ck_ref_bind, rto, conv);
  1529. /* This reference binding, unlike those above, requires the
  1530. creation of a temporary. */
  1531. conv->need_temporary_p = true;
  1532. conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
  1533. /* [dcl.init.ref]
  1534. Otherwise, the reference shall be an lvalue reference to a
  1535. non-volatile const type, or the reference shall be an rvalue
  1536. reference. */
  1537. if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
  1538. conv->bad_p = true;
  1539. /* [dcl.init.ref]
  1540. Otherwise, a temporary of type "cv1 T1" is created and
  1541. initialized from the initializer expression using the rules for a
  1542. non-reference copy initialization. If T1 is reference-related to
  1543. T2, cv1 must be the same cv-qualification as, or greater
  1544. cv-qualification than, cv2; otherwise, the program is ill-formed. */
  1545. if (related_p && !at_least_as_qualified_p (to, from))
  1546. conv->bad_p = true;
  1547. return conv;
  1548. }
  1549. /* Returns the implicit conversion sequence (see [over.ics]) from type
  1550. FROM to type TO. The optional expression EXPR may affect the
  1551. conversion. FLAGS are the usual overloading flags. If C_CAST_P is
  1552. true, this conversion is coming from a C-style cast. */
  1553. static conversion *
  1554. implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
  1555. int flags, tsubst_flags_t complain)
  1556. {
  1557. conversion *conv;
  1558. if (from == error_mark_node || to == error_mark_node
  1559. || expr == error_mark_node)
  1560. return NULL;
  1561. /* Other flags only apply to the primary function in overload
  1562. resolution, or after we've chosen one. */
  1563. flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
  1564. |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_PREFER_RVALUE
  1565. |LOOKUP_NO_NARROWING|LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL);
  1566. /* FIXME: actually we don't want warnings either, but we can't just
  1567. have 'complain &= ~(tf_warning|tf_error)' because it would cause
  1568. the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
  1569. We really ought not to issue that warning until we've committed
  1570. to that conversion. */
  1571. complain &= ~tf_error;
  1572. /* Call reshape_init early to remove redundant braces. */
  1573. if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
  1574. && CLASS_TYPE_P (to)
  1575. && COMPLETE_TYPE_P (complete_type (to))
  1576. && !CLASSTYPE_NON_AGGREGATE (to))
  1577. {
  1578. expr = reshape_init (to, expr, complain);
  1579. if (expr == error_mark_node)
  1580. return NULL;
  1581. from = TREE_TYPE (expr);
  1582. }
  1583. if (TREE_CODE (to) == REFERENCE_TYPE)
  1584. conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
  1585. else
  1586. conv = standard_conversion (to, from, expr, c_cast_p, flags);
  1587. if (conv)
  1588. return conv;
  1589. if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
  1590. {
  1591. if (is_std_init_list (to))
  1592. return build_list_conv (to, expr, flags, complain);
  1593. /* As an extension, allow list-initialization of _Complex. */
  1594. if (TREE_CODE (to) == COMPLEX_TYPE)
  1595. {
  1596. conv = build_complex_conv (to, expr, flags, complain);
  1597. if (conv)
  1598. return conv;
  1599. }
  1600. /* Allow conversion from an initializer-list with one element to a
  1601. scalar type. */
  1602. if (SCALAR_TYPE_P (to))
  1603. {
  1604. int nelts = CONSTRUCTOR_NELTS (expr);
  1605. tree elt;
  1606. if (nelts == 0)
  1607. elt = build_value_init (to, tf_none);
  1608. else if (nelts == 1)
  1609. elt = CONSTRUCTOR_ELT (expr, 0)->value;
  1610. else
  1611. elt = error_mark_node;
  1612. conv = implicit_conversion (to, TREE_TYPE (elt), elt,
  1613. c_cast_p, flags, complain);
  1614. if (conv)
  1615. {
  1616. conv->check_narrowing = true;
  1617. if (BRACE_ENCLOSED_INITIALIZER_P (elt))
  1618. /* Too many levels of braces, i.e. '{{1}}'. */
  1619. conv->bad_p = true;
  1620. return conv;
  1621. }
  1622. }
  1623. else if (TREE_CODE (to) == ARRAY_TYPE)
  1624. return build_array_conv (to, expr, flags, complain);
  1625. }
  1626. if (expr != NULL_TREE
  1627. && (MAYBE_CLASS_TYPE_P (from)
  1628. || MAYBE_CLASS_TYPE_P (to))
  1629. && (flags & LOOKUP_NO_CONVERSION) == 0)
  1630. {
  1631. struct z_candidate *cand;
  1632. if (CLASS_TYPE_P (to)
  1633. && BRACE_ENCLOSED_INITIALIZER_P (expr)
  1634. && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
  1635. return build_aggr_conv (to, expr, flags, complain);
  1636. cand = build_user_type_conversion_1 (to, expr, flags, complain);
  1637. if (cand)
  1638. conv = cand->second_conv;
  1639. /* We used to try to bind a reference to a temporary here, but that
  1640. is now handled after the recursive call to this function at the end
  1641. of reference_binding. */
  1642. return conv;
  1643. }
  1644. return NULL;
  1645. }
  1646. /* Add a new entry to the list of candidates. Used by the add_*_candidate
  1647. functions. ARGS will not be changed until a single candidate is
  1648. selected. */
  1649. static struct z_candidate *
  1650. add_candidate (struct z_candidate **candidates,
  1651. tree fn, tree first_arg, const vec<tree, va_gc> *args,
  1652. size_t num_convs, conversion **convs,
  1653. tree access_path, tree conversion_path,
  1654. int viable, struct rejection_reason *reason,
  1655. int flags)
  1656. {
  1657. struct z_candidate *cand = (struct z_candidate *)
  1658. conversion_obstack_alloc (sizeof (struct z_candidate));
  1659. cand->fn = fn;
  1660. cand->first_arg = first_arg;
  1661. cand->args = args;
  1662. cand->convs = convs;
  1663. cand->num_convs = num_convs;
  1664. cand->access_path = access_path;
  1665. cand->conversion_path = conversion_path;
  1666. cand->viable = viable;
  1667. cand->reason = reason;
  1668. cand->next = *candidates;
  1669. cand->flags = flags;
  1670. *candidates = cand;
  1671. return cand;
  1672. }
  1673. /* Return the number of remaining arguments in the parameter list
  1674. beginning with ARG. */
  1675. static int
  1676. remaining_arguments (tree arg)
  1677. {
  1678. int n;
  1679. for (n = 0; arg != NULL_TREE && arg != void_list_node;
  1680. arg = TREE_CHAIN (arg))
  1681. n++;
  1682. return n;
  1683. }
  1684. /* Create an overload candidate for the function or method FN called
  1685. with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
  1686. FLAGS is passed on to implicit_conversion.
  1687. This does not change ARGS.
  1688. CTYPE, if non-NULL, is the type we want to pretend this function
  1689. comes from for purposes of overload resolution. */
  1690. static struct z_candidate *
  1691. add_function_candidate (struct z_candidate **candidates,
  1692. tree fn, tree ctype, tree first_arg,
  1693. const vec<tree, va_gc> *args, tree access_path,
  1694. tree conversion_path, int flags,
  1695. tsubst_flags_t complain)
  1696. {
  1697. tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
  1698. int i, len;
  1699. conversion **convs;
  1700. tree parmnode;
  1701. tree orig_first_arg = first_arg;
  1702. int skip;
  1703. int viable = 1;
  1704. struct rejection_reason *reason = NULL;
  1705. /* At this point we should not see any functions which haven't been
  1706. explicitly declared, except for friend functions which will have
  1707. been found using argument dependent lookup. */
  1708. gcc_assert (!DECL_ANTICIPATED (fn) || DECL_HIDDEN_FRIEND_P (fn));
  1709. /* The `this', `in_chrg' and VTT arguments to constructors are not
  1710. considered in overload resolution. */
  1711. if (DECL_CONSTRUCTOR_P (fn))
  1712. {
  1713. parmlist = skip_artificial_parms_for (fn, parmlist);
  1714. skip = num_artificial_parms_for (fn);
  1715. if (skip > 0 && first_arg != NULL_TREE)
  1716. {
  1717. --skip;
  1718. first_arg = NULL_TREE;
  1719. }
  1720. }
  1721. else
  1722. skip = 0;
  1723. len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
  1724. convs = alloc_conversions (len);
  1725. /* 13.3.2 - Viable functions [over.match.viable]
  1726. First, to be a viable function, a candidate function shall have enough
  1727. parameters to agree in number with the arguments in the list.
  1728. We need to check this first; otherwise, checking the ICSes might cause
  1729. us to produce an ill-formed template instantiation. */
  1730. parmnode = parmlist;
  1731. for (i = 0; i < len; ++i)
  1732. {
  1733. if (parmnode == NULL_TREE || parmnode == void_list_node)
  1734. break;
  1735. parmnode = TREE_CHAIN (parmnode);
  1736. }
  1737. if ((i < len && parmnode)
  1738. || !sufficient_parms_p (parmnode))
  1739. {
  1740. int remaining = remaining_arguments (parmnode);
  1741. viable = 0;
  1742. reason = arity_rejection (first_arg, i + remaining, len);
  1743. }
  1744. /* When looking for a function from a subobject from an implicit
  1745. copy/move constructor/operator=, don't consider anything that takes (a
  1746. reference to) an unrelated type. See c++/44909 and core 1092. */
  1747. else if (parmlist && (flags & LOOKUP_DEFAULTED))
  1748. {
  1749. if (DECL_CONSTRUCTOR_P (fn))
  1750. i = 1;
  1751. else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
  1752. && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
  1753. i = 2;
  1754. else
  1755. i = 0;
  1756. if (i && len == i)
  1757. {
  1758. parmnode = chain_index (i-1, parmlist);
  1759. if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
  1760. ctype))
  1761. viable = 0;
  1762. }
  1763. /* This only applies at the top level. */
  1764. flags &= ~LOOKUP_DEFAULTED;
  1765. }
  1766. if (! viable)
  1767. goto out;
  1768. /* Second, for F to be a viable function, there shall exist for each
  1769. argument an implicit conversion sequence that converts that argument
  1770. to the corresponding parameter of F. */
  1771. parmnode = parmlist;
  1772. for (i = 0; i < len; ++i)
  1773. {
  1774. tree argtype, to_type;
  1775. tree arg;
  1776. conversion *t;
  1777. int is_this;
  1778. if (parmnode == void_list_node)
  1779. break;
  1780. if (i == 0 && first_arg != NULL_TREE)
  1781. arg = first_arg;
  1782. else
  1783. arg = CONST_CAST_TREE (
  1784. (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
  1785. argtype = lvalue_type (arg);
  1786. is_this = (i == 0 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
  1787. && ! DECL_CONSTRUCTOR_P (fn));
  1788. if (parmnode)
  1789. {
  1790. tree parmtype = TREE_VALUE (parmnode);
  1791. int lflags = flags;
  1792. parmnode = TREE_CHAIN (parmnode);
  1793. /* The type of the implicit object parameter ('this') for
  1794. overload resolution is not always the same as for the
  1795. function itself; conversion functions are considered to
  1796. be members of the class being converted, and functions
  1797. introduced by a using-declaration are considered to be
  1798. members of the class that uses them.
  1799. Since build_over_call ignores the ICS for the `this'
  1800. parameter, we can just change the parm type. */
  1801. if (ctype && is_this)
  1802. {
  1803. parmtype = cp_build_qualified_type
  1804. (ctype, cp_type_quals (TREE_TYPE (parmtype)));
  1805. if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
  1806. {
  1807. /* If the function has a ref-qualifier, the implicit
  1808. object parameter has reference type. */
  1809. bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
  1810. parmtype = cp_build_reference_type (parmtype, rv);
  1811. /* The special handling of 'this' conversions in compare_ics
  1812. does not apply if there is a ref-qualifier. */
  1813. is_this = false;
  1814. }
  1815. else
  1816. {
  1817. parmtype = build_pointer_type (parmtype);
  1818. arg = build_this (arg);
  1819. argtype = lvalue_type (arg);
  1820. }
  1821. }
  1822. /* Core issue 899: When [copy-]initializing a temporary to be bound
  1823. to the first parameter of a copy constructor (12.8) called with
  1824. a single argument in the context of direct-initialization,
  1825. explicit conversion functions are also considered.
  1826. So set LOOKUP_COPY_PARM to let reference_binding know that
  1827. it's being called in that context. We generalize the above
  1828. to handle move constructors and template constructors as well;
  1829. the standardese should soon be updated similarly. */
  1830. if (ctype && i == 0 && (len-skip == 1)
  1831. && DECL_CONSTRUCTOR_P (fn)
  1832. && parmtype != error_mark_node
  1833. && (same_type_ignoring_top_level_qualifiers_p
  1834. (non_reference (parmtype), ctype)))
  1835. {
  1836. if (!(flags & LOOKUP_ONLYCONVERTING))
  1837. lflags |= LOOKUP_COPY_PARM;
  1838. /* We allow user-defined conversions within init-lists, but
  1839. don't list-initialize the copy parm, as that would mean
  1840. using two levels of braces for the same type. */
  1841. if ((flags & LOOKUP_LIST_INIT_CTOR)
  1842. && BRACE_ENCLOSED_INITIALIZER_P (arg))
  1843. lflags |= LOOKUP_NO_CONVERSION;
  1844. }
  1845. else
  1846. lflags |= LOOKUP_ONLYCONVERTING;
  1847. t = implicit_conversion (parmtype, argtype, arg,
  1848. /*c_cast_p=*/false, lflags, complain);
  1849. to_type = parmtype;
  1850. }
  1851. else
  1852. {
  1853. t = build_identity_conv (argtype, arg);
  1854. t->ellipsis_p = true;
  1855. to_type = argtype;
  1856. }
  1857. if (t && is_this)
  1858. t->this_p = true;
  1859. convs[i] = t;
  1860. if (! t)
  1861. {
  1862. viable = 0;
  1863. reason = arg_conversion_rejection (first_arg, i, argtype, to_type);
  1864. break;
  1865. }
  1866. if (t->bad_p)
  1867. {
  1868. viable = -1;
  1869. reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type);
  1870. }
  1871. }
  1872. out:
  1873. return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
  1874. access_path, conversion_path, viable, reason, flags);
  1875. }
  1876. /* Create an overload candidate for the conversion function FN which will
  1877. be invoked for expression OBJ, producing a pointer-to-function which
  1878. will in turn be called with the argument list FIRST_ARG/ARGLIST,
  1879. and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
  1880. passed on to implicit_conversion.
  1881. Actually, we don't really care about FN; we care about the type it
  1882. converts to. There may be multiple conversion functions that will
  1883. convert to that type, and we rely on build_user_type_conversion_1 to
  1884. choose the best one; so when we create our candidate, we record the type
  1885. instead of the function. */
  1886. static struct z_candidate *
  1887. add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
  1888. tree first_arg, const vec<tree, va_gc> *arglist,
  1889. tree access_path, tree conversion_path,
  1890. tsubst_flags_t complain)
  1891. {
  1892. tree totype = TREE_TYPE (TREE_TYPE (fn));
  1893. int i, len, viable, flags;
  1894. tree parmlist, parmnode;
  1895. conversion **convs;
  1896. struct rejection_reason *reason;
  1897. for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
  1898. parmlist = TREE_TYPE (parmlist);
  1899. parmlist = TYPE_ARG_TYPES (parmlist);
  1900. len = vec_safe_length (arglist) + (first_arg != NULL_TREE ? 1 : 0) + 1;
  1901. convs = alloc_conversions (len);
  1902. parmnode = parmlist;
  1903. viable = 1;
  1904. flags = LOOKUP_IMPLICIT;
  1905. reason = NULL;
  1906. /* Don't bother looking up the same type twice. */
  1907. if (*candidates && (*candidates)->fn == totype)
  1908. return NULL;
  1909. for (i = 0; i < len; ++i)
  1910. {
  1911. tree arg, argtype, convert_type = NULL_TREE;
  1912. conversion *t;
  1913. if (i == 0)
  1914. arg = obj;
  1915. else if (i == 1 && first_arg != NULL_TREE)
  1916. arg = first_arg;
  1917. else
  1918. arg = (*arglist)[i - (first_arg != NULL_TREE ? 1 : 0) - 1];
  1919. argtype = lvalue_type (arg);
  1920. if (i == 0)
  1921. {
  1922. t = implicit_conversion (totype, argtype, arg, /*c_cast_p=*/false,
  1923. flags, complain);
  1924. convert_type = totype;
  1925. }
  1926. else if (parmnode == void_list_node)
  1927. break;
  1928. else if (parmnode)
  1929. {
  1930. t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
  1931. /*c_cast_p=*/false, flags, complain);
  1932. convert_type = TREE_VALUE (parmnode);
  1933. }
  1934. else
  1935. {
  1936. t = build_identity_conv (argtype, arg);
  1937. t->ellipsis_p = true;
  1938. convert_type = argtype;
  1939. }
  1940. convs[i] = t;
  1941. if (! t)
  1942. break;
  1943. if (t->bad_p)
  1944. {
  1945. viable = -1;
  1946. reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type);
  1947. }
  1948. if (i == 0)
  1949. continue;
  1950. if (parmnode)
  1951. parmnode = TREE_CHAIN (parmnode);
  1952. }
  1953. if (i < len
  1954. || ! sufficient_parms_p (parmnode))
  1955. {
  1956. int remaining = remaining_arguments (parmnode);
  1957. viable = 0;
  1958. reason = arity_rejection (NULL_TREE, i + remaining, len);
  1959. }
  1960. return add_candidate (candidates, totype, first_arg, arglist, len, convs,
  1961. access_path, conversion_path, viable, reason, flags);
  1962. }
  1963. static void
  1964. build_builtin_candidate (struct z_candidate **candidates, tree fnname,
  1965. tree type1, tree type2, tree *args, tree *argtypes,
  1966. int flags, tsubst_flags_t complain)
  1967. {
  1968. conversion *t;
  1969. conversion **convs;
  1970. size_t num_convs;
  1971. int viable = 1, i;
  1972. tree types[2];
  1973. struct rejection_reason *reason = NULL;
  1974. types[0] = type1;
  1975. types[1] = type2;
  1976. num_convs = args[2] ? 3 : (args[1] ? 2 : 1);
  1977. convs = alloc_conversions (num_convs);
  1978. /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
  1979. conversion ops are allowed. We handle that here by just checking for
  1980. boolean_type_node because other operators don't ask for it. COND_EXPR
  1981. also does contextual conversion to bool for the first operand, but we
  1982. handle that in build_conditional_expr, and type1 here is operand 2. */
  1983. if (type1 != boolean_type_node)
  1984. flags |= LOOKUP_ONLYCONVERTING;
  1985. for (i = 0; i < 2; ++i)
  1986. {
  1987. if (! args[i])
  1988. break;
  1989. t = implicit_conversion (types[i], argtypes[i], args[i],
  1990. /*c_cast_p=*/false, flags, complain);
  1991. if (! t)
  1992. {
  1993. viable = 0;
  1994. /* We need something for printing the candidate. */
  1995. t = build_identity_conv (types[i], NULL_TREE);
  1996. reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
  1997. types[i]);
  1998. }
  1999. else if (t->bad_p)
  2000. {
  2001. viable = 0;
  2002. reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
  2003. types[i]);
  2004. }
  2005. convs[i] = t;
  2006. }
  2007. /* For COND_EXPR we rearranged the arguments; undo that now. */
  2008. if (args[2])
  2009. {
  2010. convs[2] = convs[1];
  2011. convs[1] = convs[0];
  2012. t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
  2013. /*c_cast_p=*/false, flags,
  2014. complain);
  2015. if (t)
  2016. convs[0] = t;
  2017. else
  2018. {
  2019. viable = 0;
  2020. reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
  2021. boolean_type_node);
  2022. }
  2023. }
  2024. add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
  2025. num_convs, convs,
  2026. /*access_path=*/NULL_TREE,
  2027. /*conversion_path=*/NULL_TREE,
  2028. viable, reason, flags);
  2029. }
  2030. static bool
  2031. is_complete (tree t)
  2032. {
  2033. return COMPLETE_TYPE_P (complete_type (t));
  2034. }
  2035. /* Returns nonzero if TYPE is a promoted arithmetic type. */
  2036. static bool
  2037. promoted_arithmetic_type_p (tree type)
  2038. {
  2039. /* [over.built]
  2040. In this section, the term promoted integral type is used to refer
  2041. to those integral types which are preserved by integral promotion
  2042. (including e.g. int and long but excluding e.g. char).
  2043. Similarly, the term promoted arithmetic type refers to promoted
  2044. integral types plus floating types. */
  2045. return ((CP_INTEGRAL_TYPE_P (type)
  2046. && same_type_p (type_promotes_to (type), type))
  2047. || TREE_CODE (type) == REAL_TYPE);
  2048. }
  2049. /* Create any builtin operator overload candidates for the operator in
  2050. question given the converted operand types TYPE1 and TYPE2. The other
  2051. args are passed through from add_builtin_candidates to
  2052. build_builtin_candidate.
  2053. TYPE1 and TYPE2 may not be permissible, and we must filter them.
  2054. If CODE is requires candidates operands of the same type of the kind
  2055. of which TYPE1 and TYPE2 are, we add both candidates
  2056. CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
  2057. static void
  2058. add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
  2059. enum tree_code code2, tree fnname, tree type1,
  2060. tree type2, tree *args, tree *argtypes, int flags,
  2061. tsubst_flags_t complain)
  2062. {
  2063. switch (code)
  2064. {
  2065. case POSTINCREMENT_EXPR:
  2066. case POSTDECREMENT_EXPR:
  2067. args[1] = integer_zero_node;
  2068. type2 = integer_type_node;
  2069. break;
  2070. default:
  2071. break;
  2072. }
  2073. switch (code)
  2074. {
  2075. /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
  2076. and VQ is either volatile or empty, there exist candidate operator
  2077. functions of the form
  2078. VQ T& operator++(VQ T&);
  2079. T operator++(VQ T&, int);
  2080. 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
  2081. type other than bool, and VQ is either volatile or empty, there exist
  2082. candidate operator functions of the form
  2083. VQ T& operator--(VQ T&);
  2084. T operator--(VQ T&, int);
  2085. 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
  2086. complete object type, and VQ is either volatile or empty, there exist
  2087. candidate operator functions of the form
  2088. T*VQ& operator++(T*VQ&);
  2089. T*VQ& operator--(T*VQ&);
  2090. T* operator++(T*VQ&, int);
  2091. T* operator--(T*VQ&, int); */
  2092. case POSTDECREMENT_EXPR:
  2093. case PREDECREMENT_EXPR:
  2094. if (TREE_CODE (type1) == BOOLEAN_TYPE)
  2095. return;
  2096. case POSTINCREMENT_EXPR:
  2097. case PREINCREMENT_EXPR:
  2098. if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
  2099. {
  2100. type1 = build_reference_type (type1);
  2101. break;
  2102. }
  2103. return;
  2104. /* 7 For every cv-qualified or cv-unqualified object type T, there
  2105. exist candidate operator functions of the form
  2106. T& operator*(T*);
  2107. 8 For every function type T, there exist candidate operator functions of
  2108. the form
  2109. T& operator*(T*); */
  2110. case INDIRECT_REF:
  2111. if (TYPE_PTR_P (type1)
  2112. && (TYPE_PTROB_P (type1)
  2113. || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
  2114. break;
  2115. return;
  2116. /* 9 For every type T, there exist candidate operator functions of the form
  2117. T* operator+(T*);
  2118. 10For every promoted arithmetic type T, there exist candidate operator
  2119. functions of the form
  2120. T operator+(T);
  2121. T operator-(T); */
  2122. case UNARY_PLUS_EXPR: /* unary + */
  2123. if (TYPE_PTR_P (type1))
  2124. break;
  2125. case NEGATE_EXPR:
  2126. if (ARITHMETIC_TYPE_P (type1))
  2127. break;
  2128. return;
  2129. /* 11For every promoted integral type T, there exist candidate operator
  2130. functions of the form
  2131. T operator~(T); */
  2132. case BIT_NOT_EXPR:
  2133. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
  2134. break;
  2135. return;
  2136. /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
  2137. is the same type as C2 or is a derived class of C2, T is a complete
  2138. object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
  2139. there exist candidate operator functions of the form
  2140. CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
  2141. where CV12 is the union of CV1 and CV2. */
  2142. case MEMBER_REF:
  2143. if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
  2144. {
  2145. tree c1 = TREE_TYPE (type1);
  2146. tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
  2147. if (MAYBE_CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
  2148. && (TYPE_PTRMEMFUNC_P (type2)
  2149. || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
  2150. break;
  2151. }
  2152. return;
  2153. /* 13For every pair of promoted arithmetic types L and R, there exist can-
  2154. didate operator functions of the form
  2155. LR operator*(L, R);
  2156. LR operator/(L, R);
  2157. LR operator+(L, R);
  2158. LR operator-(L, R);
  2159. bool operator<(L, R);
  2160. bool operator>(L, R);
  2161. bool operator<=(L, R);
  2162. bool operator>=(L, R);
  2163. bool operator==(L, R);
  2164. bool operator!=(L, R);
  2165. where LR is the result of the usual arithmetic conversions between
  2166. types L and R.
  2167. 14For every pair of types T and I, where T is a cv-qualified or cv-
  2168. unqualified complete object type and I is a promoted integral type,
  2169. there exist candidate operator functions of the form
  2170. T* operator+(T*, I);
  2171. T& operator[](T*, I);
  2172. T* operator-(T*, I);
  2173. T* operator+(I, T*);
  2174. T& operator[](I, T*);
  2175. 15For every T, where T is a pointer to complete object type, there exist
  2176. candidate operator functions of the form112)
  2177. ptrdiff_t operator-(T, T);
  2178. 16For every pointer or enumeration type T, there exist candidate operator
  2179. functions of the form
  2180. bool operator<(T, T);
  2181. bool operator>(T, T);
  2182. bool operator<=(T, T);
  2183. bool operator>=(T, T);
  2184. bool operator==(T, T);
  2185. bool operator!=(T, T);
  2186. 17For every pointer to member type T, there exist candidate operator
  2187. functions of the form
  2188. bool operator==(T, T);
  2189. bool operator!=(T, T); */
  2190. case MINUS_EXPR:
  2191. if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
  2192. break;
  2193. if (TYPE_PTROB_P (type1)
  2194. && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
  2195. {
  2196. type2 = ptrdiff_type_node;
  2197. break;
  2198. }
  2199. case MULT_EXPR:
  2200. case TRUNC_DIV_EXPR:
  2201. if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
  2202. break;
  2203. return;
  2204. case EQ_EXPR:
  2205. case NE_EXPR:
  2206. if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
  2207. || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
  2208. break;
  2209. if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
  2210. {
  2211. type2 = type1;
  2212. break;
  2213. }
  2214. if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
  2215. {
  2216. type1 = type2;
  2217. break;
  2218. }
  2219. /* Fall through. */
  2220. case LT_EXPR:
  2221. case GT_EXPR:
  2222. case LE_EXPR:
  2223. case GE_EXPR:
  2224. case MAX_EXPR:
  2225. case MIN_EXPR:
  2226. if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
  2227. break;
  2228. if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
  2229. break;
  2230. if (TREE_CODE (type1) == ENUMERAL_TYPE
  2231. && TREE_CODE (type2) == ENUMERAL_TYPE)
  2232. break;
  2233. if (TYPE_PTR_P (type1)
  2234. && null_ptr_cst_p (args[1]))
  2235. {
  2236. type2 = type1;
  2237. break;
  2238. }
  2239. if (null_ptr_cst_p (args[0])
  2240. && TYPE_PTR_P (type2))
  2241. {
  2242. type1 = type2;
  2243. break;
  2244. }
  2245. return;
  2246. case PLUS_EXPR:
  2247. if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
  2248. break;
  2249. case ARRAY_REF:
  2250. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
  2251. {
  2252. type1 = ptrdiff_type_node;
  2253. break;
  2254. }
  2255. if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
  2256. {
  2257. type2 = ptrdiff_type_node;
  2258. break;
  2259. }
  2260. return;
  2261. /* 18For every pair of promoted integral types L and R, there exist candi-
  2262. date operator functions of the form
  2263. LR operator%(L, R);
  2264. LR operator&(L, R);
  2265. LR operator^(L, R);
  2266. LR operator|(L, R);
  2267. L operator<<(L, R);
  2268. L operator>>(L, R);
  2269. where LR is the result of the usual arithmetic conversions between
  2270. types L and R. */
  2271. case TRUNC_MOD_EXPR:
  2272. case BIT_AND_EXPR:
  2273. case BIT_IOR_EXPR:
  2274. case BIT_XOR_EXPR:
  2275. case LSHIFT_EXPR:
  2276. case RSHIFT_EXPR:
  2277. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
  2278. break;
  2279. return;
  2280. /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
  2281. type, VQ is either volatile or empty, and R is a promoted arithmetic
  2282. type, there exist candidate operator functions of the form
  2283. VQ L& operator=(VQ L&, R);
  2284. VQ L& operator*=(VQ L&, R);
  2285. VQ L& operator/=(VQ L&, R);
  2286. VQ L& operator+=(VQ L&, R);
  2287. VQ L& operator-=(VQ L&, R);
  2288. 20For every pair T, VQ), where T is any type and VQ is either volatile
  2289. or empty, there exist candidate operator functions of the form
  2290. T*VQ& operator=(T*VQ&, T*);
  2291. 21For every pair T, VQ), where T is a pointer to member type and VQ is
  2292. either volatile or empty, there exist candidate operator functions of
  2293. the form
  2294. VQ T& operator=(VQ T&, T);
  2295. 22For every triple T, VQ, I), where T is a cv-qualified or cv-
  2296. unqualified complete object type, VQ is either volatile or empty, and
  2297. I is a promoted integral type, there exist candidate operator func-
  2298. tions of the form
  2299. T*VQ& operator+=(T*VQ&, I);
  2300. T*VQ& operator-=(T*VQ&, I);
  2301. 23For every triple L, VQ, R), where L is an integral or enumeration
  2302. type, VQ is either volatile or empty, and R is a promoted integral
  2303. type, there exist candidate operator functions of the form
  2304. VQ L& operator%=(VQ L&, R);
  2305. VQ L& operator<<=(VQ L&, R);
  2306. VQ L& operator>>=(VQ L&, R);
  2307. VQ L& operator&=(VQ L&, R);
  2308. VQ L& operator^=(VQ L&, R);
  2309. VQ L& operator|=(VQ L&, R); */
  2310. case MODIFY_EXPR:
  2311. switch (code2)
  2312. {
  2313. case PLUS_EXPR:
  2314. case MINUS_EXPR:
  2315. if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
  2316. {
  2317. type2 = ptrdiff_type_node;
  2318. break;
  2319. }
  2320. case MULT_EXPR:
  2321. case TRUNC_DIV_EXPR:
  2322. if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
  2323. break;
  2324. return;
  2325. case TRUNC_MOD_EXPR:
  2326. case BIT_AND_EXPR:
  2327. case BIT_IOR_EXPR:
  2328. case BIT_XOR_EXPR:
  2329. case LSHIFT_EXPR:
  2330. case RSHIFT_EXPR:
  2331. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
  2332. break;
  2333. return;
  2334. case NOP_EXPR:
  2335. if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
  2336. break;
  2337. if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
  2338. || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
  2339. || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
  2340. || ((TYPE_PTRMEMFUNC_P (type1)
  2341. || TYPE_PTR_P (type1))
  2342. && null_ptr_cst_p (args[1])))
  2343. {
  2344. type2 = type1;
  2345. break;
  2346. }
  2347. return;
  2348. default:
  2349. gcc_unreachable ();
  2350. }
  2351. type1 = build_reference_type (type1);
  2352. break;
  2353. case COND_EXPR:
  2354. /* [over.built]
  2355. For every pair of promoted arithmetic types L and R, there
  2356. exist candidate operator functions of the form
  2357. LR operator?(bool, L, R);
  2358. where LR is the result of the usual arithmetic conversions
  2359. between types L and R.
  2360. For every type T, where T is a pointer or pointer-to-member
  2361. type, there exist candidate operator functions of the form T
  2362. operator?(bool, T, T); */
  2363. if (promoted_arithmetic_type_p (type1)
  2364. && promoted_arithmetic_type_p (type2))
  2365. /* That's OK. */
  2366. break;
  2367. /* Otherwise, the types should be pointers. */
  2368. if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
  2369. return;
  2370. /* We don't check that the two types are the same; the logic
  2371. below will actually create two candidates; one in which both
  2372. parameter types are TYPE1, and one in which both parameter
  2373. types are TYPE2. */
  2374. break;
  2375. case REALPART_EXPR:
  2376. case IMAGPART_EXPR:
  2377. if (ARITHMETIC_TYPE_P (type1))
  2378. break;
  2379. return;
  2380. default:
  2381. gcc_unreachable ();
  2382. }
  2383. /* Make sure we don't create builtin candidates with dependent types. */
  2384. bool u1 = uses_template_parms (type1);
  2385. bool u2 = type2 ? uses_template_parms (type2) : false;
  2386. if (u1 || u2)
  2387. {
  2388. /* Try to recover if one of the types is non-dependent. But if
  2389. there's only one type, there's nothing we can do. */
  2390. if (!type2)
  2391. return;
  2392. /* And we lose if both are dependent. */
  2393. if (u1 && u2)
  2394. return;
  2395. /* Or if they have different forms. */
  2396. if (TREE_CODE (type1) != TREE_CODE (type2))
  2397. return;
  2398. if (u1 && !u2)
  2399. type1 = type2;
  2400. else if (u2 && !u1)
  2401. type2 = type1;
  2402. }
  2403. /* If we're dealing with two pointer types or two enumeral types,
  2404. we need candidates for both of them. */
  2405. if (type2 && !same_type_p (type1, type2)
  2406. && TREE_CODE (type1) == TREE_CODE (type2)
  2407. && (TREE_CODE (type1) == REFERENCE_TYPE
  2408. || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
  2409. || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
  2410. || TYPE_PTRMEMFUNC_P (type1)
  2411. || MAYBE_CLASS_TYPE_P (type1)
  2412. || TREE_CODE (type1) == ENUMERAL_TYPE))
  2413. {
  2414. if (TYPE_PTR_OR_PTRMEM_P (type1))
  2415. {
  2416. tree cptype = composite_pointer_type (type1, type2,
  2417. error_mark_node,
  2418. error_mark_node,
  2419. CPO_CONVERSION,
  2420. tf_none);
  2421. if (cptype != error_mark_node)
  2422. {
  2423. build_builtin_candidate
  2424. (candidates, fnname, cptype, cptype, args, argtypes,
  2425. flags, complain);
  2426. return;
  2427. }
  2428. }
  2429. build_builtin_candidate
  2430. (candidates, fnname, type1, type1, args, argtypes, flags, complain);
  2431. build_builtin_candidate
  2432. (candidates, fnname, type2, type2, args, argtypes, flags, complain);
  2433. return;
  2434. }
  2435. build_builtin_candidate
  2436. (candidates, fnname, type1, type2, args, argtypes, flags, complain);
  2437. }
  2438. tree
  2439. type_decays_to (tree type)
  2440. {
  2441. if (TREE_CODE (type) == ARRAY_TYPE)
  2442. return build_pointer_type (TREE_TYPE (type));
  2443. if (TREE_CODE (type) == FUNCTION_TYPE)
  2444. return build_pointer_type (type);
  2445. return type;
  2446. }
  2447. /* There are three conditions of builtin candidates:
  2448. 1) bool-taking candidates. These are the same regardless of the input.
  2449. 2) pointer-pair taking candidates. These are generated for each type
  2450. one of the input types converts to.
  2451. 3) arithmetic candidates. According to the standard, we should generate
  2452. all of these, but I'm trying not to...
  2453. Here we generate a superset of the possible candidates for this particular
  2454. case. That is a subset of the full set the standard defines, plus some
  2455. other cases which the standard disallows. add_builtin_candidate will
  2456. filter out the invalid set. */
  2457. static void
  2458. add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
  2459. enum tree_code code2, tree fnname, tree *args,
  2460. int flags, tsubst_flags_t complain)
  2461. {
  2462. int ref1, i;
  2463. int enum_p = 0;
  2464. tree type, argtypes[3], t;
  2465. /* TYPES[i] is the set of possible builtin-operator parameter types
  2466. we will consider for the Ith argument. */
  2467. vec<tree, va_gc> *types[2];
  2468. unsigned ix;
  2469. for (i = 0; i < 3; ++i)
  2470. {
  2471. if (args[i])
  2472. argtypes[i] = unlowered_expr_type (args[i]);
  2473. else
  2474. argtypes[i] = NULL_TREE;
  2475. }
  2476. switch (code)
  2477. {
  2478. /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
  2479. and VQ is either volatile or empty, there exist candidate operator
  2480. functions of the form
  2481. VQ T& operator++(VQ T&); */
  2482. case POSTINCREMENT_EXPR:
  2483. case PREINCREMENT_EXPR:
  2484. case POSTDECREMENT_EXPR:
  2485. case PREDECREMENT_EXPR:
  2486. case MODIFY_EXPR:
  2487. ref1 = 1;
  2488. break;
  2489. /* 24There also exist candidate operator functions of the form
  2490. bool operator!(bool);
  2491. bool operator&&(bool, bool);
  2492. bool operator||(bool, bool); */
  2493. case TRUTH_NOT_EXPR:
  2494. build_builtin_candidate
  2495. (candidates, fnname, boolean_type_node,
  2496. NULL_TREE, args, argtypes, flags, complain);
  2497. return;
  2498. case TRUTH_ORIF_EXPR:
  2499. case TRUTH_ANDIF_EXPR:
  2500. build_builtin_candidate
  2501. (candidates, fnname, boolean_type_node,
  2502. boolean_type_node, args, argtypes, flags, complain);
  2503. return;
  2504. case ADDR_EXPR:
  2505. case COMPOUND_EXPR:
  2506. case COMPONENT_REF:
  2507. return;
  2508. case COND_EXPR:
  2509. case EQ_EXPR:
  2510. case NE_EXPR:
  2511. case LT_EXPR:
  2512. case LE_EXPR:
  2513. case GT_EXPR:
  2514. case GE_EXPR:
  2515. enum_p = 1;
  2516. /* Fall through. */
  2517. default:
  2518. ref1 = 0;
  2519. }
  2520. types[0] = make_tree_vector ();
  2521. types[1] = make_tree_vector ();
  2522. for (i = 0; i < 2; ++i)
  2523. {
  2524. if (! args[i])
  2525. ;
  2526. else if (MAYBE_CLASS_TYPE_P (argtypes[i]))
  2527. {
  2528. tree convs;
  2529. if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
  2530. return;
  2531. convs = lookup_conversions (argtypes[i]);
  2532. if (code == COND_EXPR)
  2533. {
  2534. if (real_lvalue_p (args[i]))
  2535. vec_safe_push (types[i], build_reference_type (argtypes[i]));
  2536. vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
  2537. }
  2538. else if (! convs)
  2539. return;
  2540. for (; convs; convs = TREE_CHAIN (convs))
  2541. {
  2542. type = TREE_TYPE (convs);
  2543. if (i == 0 && ref1
  2544. && (TREE_CODE (type) != REFERENCE_TYPE
  2545. || CP_TYPE_CONST_P (TREE_TYPE (type))))
  2546. continue;
  2547. if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
  2548. vec_safe_push (types[i], type);
  2549. type = non_reference (type);
  2550. if (i != 0 || ! ref1)
  2551. {
  2552. type = cv_unqualified (type_decays_to (type));
  2553. if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
  2554. vec_safe_push (types[i], type);
  2555. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
  2556. type = type_promotes_to (type);
  2557. }
  2558. if (! vec_member (type, types[i]))
  2559. vec_safe_push (types[i], type);
  2560. }
  2561. }
  2562. else
  2563. {
  2564. if (code == COND_EXPR && real_lvalue_p (args[i]))
  2565. vec_safe_push (types[i], build_reference_type (argtypes[i]));
  2566. type = non_reference (argtypes[i]);
  2567. if (i != 0 || ! ref1)
  2568. {
  2569. type = cv_unqualified (type_decays_to (type));
  2570. if (enum_p && UNSCOPED_ENUM_P (type))
  2571. vec_safe_push (types[i], type);
  2572. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
  2573. type = type_promotes_to (type);
  2574. }
  2575. vec_safe_push (types[i], type);
  2576. }
  2577. }
  2578. /* Run through the possible parameter types of both arguments,
  2579. creating candidates with those parameter types. */
  2580. FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
  2581. {
  2582. unsigned jx;
  2583. tree u;
  2584. if (!types[1]->is_empty ())
  2585. FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
  2586. add_builtin_candidate
  2587. (candidates, code, code2, fnname, t,
  2588. u, args, argtypes, flags, complain);
  2589. else
  2590. add_builtin_candidate
  2591. (candidates, code, code2, fnname, t,
  2592. NULL_TREE, args, argtypes, flags, complain);
  2593. }
  2594. release_tree_vector (types[0]);
  2595. release_tree_vector (types[1]);
  2596. }
  2597. /* If TMPL can be successfully instantiated as indicated by
  2598. EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
  2599. TMPL is the template. EXPLICIT_TARGS are any explicit template
  2600. arguments. ARGLIST is the arguments provided at the call-site.
  2601. This does not change ARGLIST. The RETURN_TYPE is the desired type
  2602. for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
  2603. as for add_function_candidate. If an OBJ is supplied, FLAGS and
  2604. CTYPE are ignored, and OBJ is as for add_conv_candidate. */
  2605. static struct z_candidate*
  2606. add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
  2607. tree ctype, tree explicit_targs, tree first_arg,
  2608. const vec<tree, va_gc> *arglist, tree return_type,
  2609. tree access_path, tree conversion_path,
  2610. int flags, tree obj, unification_kind_t strict,
  2611. tsubst_flags_t complain)
  2612. {
  2613. int ntparms = DECL_NTPARMS (tmpl);
  2614. tree targs = make_tree_vec (ntparms);
  2615. unsigned int len = vec_safe_length (arglist);
  2616. unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
  2617. unsigned int skip_without_in_chrg = 0;
  2618. tree first_arg_without_in_chrg = first_arg;
  2619. tree *args_without_in_chrg;
  2620. unsigned int nargs_without_in_chrg;
  2621. unsigned int ia, ix;
  2622. tree arg;
  2623. struct z_candidate *cand;
  2624. tree fn;
  2625. struct rejection_reason *reason = NULL;
  2626. int errs;
  2627. /* We don't do deduction on the in-charge parameter, the VTT
  2628. parameter or 'this'. */
  2629. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
  2630. {
  2631. if (first_arg_without_in_chrg != NULL_TREE)
  2632. first_arg_without_in_chrg = NULL_TREE;
  2633. else
  2634. ++skip_without_in_chrg;
  2635. }
  2636. if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
  2637. || DECL_BASE_CONSTRUCTOR_P (tmpl))
  2638. && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
  2639. {
  2640. if (first_arg_without_in_chrg != NULL_TREE)
  2641. first_arg_without_in_chrg = NULL_TREE;
  2642. else
  2643. ++skip_without_in_chrg;
  2644. }
  2645. if (len < skip_without_in_chrg)
  2646. return NULL;
  2647. nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
  2648. + (len - skip_without_in_chrg));
  2649. args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
  2650. ia = 0;
  2651. if (first_arg_without_in_chrg != NULL_TREE)
  2652. {
  2653. args_without_in_chrg[ia] = first_arg_without_in_chrg;
  2654. ++ia;
  2655. }
  2656. for (ix = skip_without_in_chrg;
  2657. vec_safe_iterate (arglist, ix, &arg);
  2658. ++ix)
  2659. {
  2660. args_without_in_chrg[ia] = arg;
  2661. ++ia;
  2662. }
  2663. gcc_assert (ia == nargs_without_in_chrg);
  2664. errs = errorcount+sorrycount;
  2665. fn = fn_type_unification (tmpl, explicit_targs, targs,
  2666. args_without_in_chrg,
  2667. nargs_without_in_chrg,
  2668. return_type, strict, flags, false,
  2669. complain & tf_decltype);
  2670. if (fn == error_mark_node)
  2671. {
  2672. /* Don't repeat unification later if it already resulted in errors. */
  2673. if (errorcount+sorrycount == errs)
  2674. reason = template_unification_rejection (tmpl, explicit_targs,
  2675. targs, args_without_in_chrg,
  2676. nargs_without_in_chrg,
  2677. return_type, strict, flags);
  2678. else
  2679. reason = template_unification_error_rejection ();
  2680. goto fail;
  2681. }
  2682. /* In [class.copy]:
  2683. A member function template is never instantiated to perform the
  2684. copy of a class object to an object of its class type.
  2685. It's a little unclear what this means; the standard explicitly
  2686. does allow a template to be used to copy a class. For example,
  2687. in:
  2688. struct A {
  2689. A(A&);
  2690. template <class T> A(const T&);
  2691. };
  2692. const A f ();
  2693. void g () { A a (f ()); }
  2694. the member template will be used to make the copy. The section
  2695. quoted above appears in the paragraph that forbids constructors
  2696. whose only parameter is (a possibly cv-qualified variant of) the
  2697. class type, and a logical interpretation is that the intent was
  2698. to forbid the instantiation of member templates which would then
  2699. have that form. */
  2700. if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
  2701. {
  2702. tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
  2703. if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
  2704. ctype))
  2705. {
  2706. reason = invalid_copy_with_fn_template_rejection ();
  2707. goto fail;
  2708. }
  2709. }
  2710. if (obj != NULL_TREE)
  2711. /* Aha, this is a conversion function. */
  2712. cand = add_conv_candidate (candidates, fn, obj, first_arg, arglist,
  2713. access_path, conversion_path, complain);
  2714. else
  2715. cand = add_function_candidate (candidates, fn, ctype,
  2716. first_arg, arglist, access_path,
  2717. conversion_path, flags, complain);
  2718. if (DECL_TI_TEMPLATE (fn) != tmpl)
  2719. /* This situation can occur if a member template of a template
  2720. class is specialized. Then, instantiate_template might return
  2721. an instantiation of the specialization, in which case the
  2722. DECL_TI_TEMPLATE field will point at the original
  2723. specialization. For example:
  2724. template <class T> struct S { template <class U> void f(U);
  2725. template <> void f(int) {}; };
  2726. S<double> sd;
  2727. sd.f(3);
  2728. Here, TMPL will be template <class U> S<double>::f(U).
  2729. And, instantiate template will give us the specialization
  2730. template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
  2731. for this will point at template <class T> template <> S<T>::f(int),
  2732. so that we can find the definition. For the purposes of
  2733. overload resolution, however, we want the original TMPL. */
  2734. cand->template_decl = build_template_info (tmpl, targs);
  2735. else
  2736. cand->template_decl = DECL_TEMPLATE_INFO (fn);
  2737. cand->explicit_targs = explicit_targs;
  2738. return cand;
  2739. fail:
  2740. return add_candidate (candidates, tmpl, first_arg, arglist, nargs, NULL,
  2741. access_path, conversion_path, 0, reason, flags);
  2742. }
  2743. static struct z_candidate *
  2744. add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
  2745. tree explicit_targs, tree first_arg,
  2746. const vec<tree, va_gc> *arglist, tree return_type,
  2747. tree access_path, tree conversion_path, int flags,
  2748. unification_kind_t strict, tsubst_flags_t complain)
  2749. {
  2750. return
  2751. add_template_candidate_real (candidates, tmpl, ctype,
  2752. explicit_targs, first_arg, arglist,
  2753. return_type, access_path, conversion_path,
  2754. flags, NULL_TREE, strict, complain);
  2755. }
  2756. static struct z_candidate *
  2757. add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
  2758. tree obj, tree first_arg,
  2759. const vec<tree, va_gc> *arglist,
  2760. tree return_type, tree access_path,
  2761. tree conversion_path, tsubst_flags_t complain)
  2762. {
  2763. return
  2764. add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
  2765. first_arg, arglist, return_type, access_path,
  2766. conversion_path, 0, obj, DEDUCE_CONV,
  2767. complain);
  2768. }
  2769. /* The CANDS are the set of candidates that were considered for
  2770. overload resolution. Return the set of viable candidates, or CANDS
  2771. if none are viable. If any of the candidates were viable, set
  2772. *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
  2773. considered viable only if it is strictly viable. */
  2774. static struct z_candidate*
  2775. splice_viable (struct z_candidate *cands,
  2776. bool strict_p,
  2777. bool *any_viable_p)
  2778. {
  2779. struct z_candidate *viable;
  2780. struct z_candidate **last_viable;
  2781. struct z_candidate **cand;
  2782. bool found_strictly_viable = false;
  2783. /* Be strict inside templates, since build_over_call won't actually
  2784. do the conversions to get pedwarns. */
  2785. if (processing_template_decl)
  2786. strict_p = true;
  2787. viable = NULL;
  2788. last_viable = &viable;
  2789. *any_viable_p = false;
  2790. cand = &cands;
  2791. while (*cand)
  2792. {
  2793. struct z_candidate *c = *cand;
  2794. if (!strict_p
  2795. && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
  2796. {
  2797. /* Be strict in the presence of a viable candidate. Also if
  2798. there are template candidates, so that we get deduction errors
  2799. for them instead of silently preferring a bad conversion. */
  2800. strict_p = true;
  2801. if (viable && !found_strictly_viable)
  2802. {
  2803. /* Put any spliced near matches back onto the main list so
  2804. that we see them if there is no strict match. */
  2805. *any_viable_p = false;
  2806. *last_viable = cands;
  2807. cands = viable;
  2808. viable = NULL;
  2809. last_viable = &viable;
  2810. }
  2811. }
  2812. if (strict_p ? c->viable == 1 : c->viable)
  2813. {
  2814. *last_viable = c;
  2815. *cand = c->next;
  2816. c->next = NULL;
  2817. last_viable = &c->next;
  2818. *any_viable_p = true;
  2819. if (c->viable == 1)
  2820. found_strictly_viable = true;
  2821. }
  2822. else
  2823. cand = &c->next;
  2824. }
  2825. return viable ? viable : cands;
  2826. }
  2827. static bool
  2828. any_strictly_viable (struct z_candidate *cands)
  2829. {
  2830. for (; cands; cands = cands->next)
  2831. if (cands->viable == 1)
  2832. return true;
  2833. return false;
  2834. }
  2835. /* OBJ is being used in an expression like "OBJ.f (...)". In other
  2836. words, it is about to become the "this" pointer for a member
  2837. function call. Take the address of the object. */
  2838. static tree
  2839. build_this (tree obj)
  2840. {
  2841. /* In a template, we are only concerned about the type of the
  2842. expression, so we can take a shortcut. */
  2843. if (processing_template_decl)
  2844. return build_address (obj);
  2845. return cp_build_addr_expr (obj, tf_warning_or_error);
  2846. }
  2847. /* Returns true iff functions are equivalent. Equivalent functions are
  2848. not '==' only if one is a function-local extern function or if
  2849. both are extern "C". */
  2850. static inline int
  2851. equal_functions (tree fn1, tree fn2)
  2852. {
  2853. if (TREE_CODE (fn1) != TREE_CODE (fn2))
  2854. return 0;
  2855. if (TREE_CODE (fn1) == TEMPLATE_DECL)
  2856. return fn1 == fn2;
  2857. if (DECL_LOCAL_FUNCTION_P (fn1) || DECL_LOCAL_FUNCTION_P (fn2)
  2858. || DECL_EXTERN_C_FUNCTION_P (fn1))
  2859. return decls_match (fn1, fn2);
  2860. return fn1 == fn2;
  2861. }
  2862. /* Print information about a candidate being rejected due to INFO. */
  2863. static void
  2864. print_conversion_rejection (location_t loc, struct conversion_info *info)
  2865. {
  2866. tree from = info->from;
  2867. if (!TYPE_P (from))
  2868. from = lvalue_type (from);
  2869. if (info->n_arg == -1)
  2870. {
  2871. /* Conversion of implicit `this' argument failed. */
  2872. if (!TYPE_P (info->from))
  2873. /* A bad conversion for 'this' must be discarding cv-quals. */
  2874. inform (loc, " passing %qT as %<this%> "
  2875. "argument discards qualifiers",
  2876. from);
  2877. else
  2878. inform (loc, " no known conversion for implicit "
  2879. "%<this%> parameter from %qT to %qT",
  2880. from, info->to_type);
  2881. }
  2882. else if (!TYPE_P (info->from))
  2883. {
  2884. if (info->n_arg >= 0)
  2885. inform (loc, " conversion of argument %d would be ill-formed:",
  2886. info->n_arg + 1);
  2887. perform_implicit_conversion (info->to_type, info->from,
  2888. tf_warning_or_error);
  2889. }
  2890. else if (info->n_arg == -2)
  2891. /* Conversion of conversion function return value failed. */
  2892. inform (loc, " no known conversion from %qT to %qT",
  2893. from, info->to_type);
  2894. else
  2895. inform (loc, " no known conversion for argument %d from %qT to %qT",
  2896. info->n_arg + 1, from, info->to_type);
  2897. }
  2898. /* Print information about a candidate with WANT parameters and we found
  2899. HAVE. */
  2900. static void
  2901. print_arity_information (location_t loc, unsigned int have, unsigned int want)
  2902. {
  2903. inform_n (loc, want,
  2904. " candidate expects %d argument, %d provided",
  2905. " candidate expects %d arguments, %d provided",
  2906. want, have);
  2907. }
  2908. /* Print information about one overload candidate CANDIDATE. MSGSTR
  2909. is the text to print before the candidate itself.
  2910. NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
  2911. to have been run through gettext by the caller. This wart makes
  2912. life simpler in print_z_candidates and for the translators. */
  2913. static void
  2914. print_z_candidate (location_t loc, const char *msgstr,
  2915. struct z_candidate *candidate)
  2916. {
  2917. const char *msg = (msgstr == NULL
  2918. ? ""
  2919. : ACONCAT ((msgstr, " ", NULL)));
  2920. location_t cloc = location_of (candidate->fn);
  2921. if (identifier_p (candidate->fn))
  2922. {
  2923. cloc = loc;
  2924. if (candidate->num_convs == 3)
  2925. inform (cloc, "%s%D(%T, %T, %T) <built-in>", msg, candidate->fn,
  2926. candidate->convs[0]->type,
  2927. candidate->convs[1]->type,
  2928. candidate->convs[2]->type);
  2929. else if (candidate->num_convs == 2)
  2930. inform (cloc, "%s%D(%T, %T) <built-in>", msg, candidate->fn,
  2931. candidate->convs[0]->type,
  2932. candidate->convs[1]->type);
  2933. else
  2934. inform (cloc, "%s%D(%T) <built-in>", msg, candidate->fn,
  2935. candidate->convs[0]->type);
  2936. }
  2937. else if (TYPE_P (candidate->fn))
  2938. inform (cloc, "%s%T <conversion>", msg, candidate->fn);
  2939. else if (candidate->viable == -1)
  2940. inform (cloc, "%s%#D <near match>", msg, candidate->fn);
  2941. else if (DECL_DELETED_FN (candidate->fn))
  2942. inform (cloc, "%s%#D <deleted>", msg, candidate->fn);
  2943. else
  2944. inform (cloc, "%s%#D", msg, candidate->fn);
  2945. /* Give the user some information about why this candidate failed. */
  2946. if (candidate->reason != NULL)
  2947. {
  2948. struct rejection_reason *r = candidate->reason;
  2949. switch (r->code)
  2950. {
  2951. case rr_arity:
  2952. print_arity_information (cloc, r->u.arity.actual,
  2953. r->u.arity.expected);
  2954. break;
  2955. case rr_arg_conversion:
  2956. print_conversion_rejection (cloc, &r->u.conversion);
  2957. break;
  2958. case rr_bad_arg_conversion:
  2959. print_conversion_rejection (cloc, &r->u.bad_conversion);
  2960. break;
  2961. case rr_explicit_conversion:
  2962. inform (cloc, " return type %qT of explicit conversion function "
  2963. "cannot be converted to %qT with a qualification "
  2964. "conversion", r->u.conversion.from,
  2965. r->u.conversion.to_type);
  2966. break;
  2967. case rr_template_conversion:
  2968. inform (cloc, " conversion from return type %qT of template "
  2969. "conversion function specialization to %qT is not an "
  2970. "exact match", r->u.conversion.from,
  2971. r->u.conversion.to_type);
  2972. break;
  2973. case rr_template_unification:
  2974. /* We use template_unification_error_rejection if unification caused
  2975. actual non-SFINAE errors, in which case we don't need to repeat
  2976. them here. */
  2977. if (r->u.template_unification.tmpl == NULL_TREE)
  2978. {
  2979. inform (cloc, " substitution of deduced template arguments "
  2980. "resulted in errors seen above");
  2981. break;
  2982. }
  2983. /* Re-run template unification with diagnostics. */
  2984. inform (cloc, " template argument deduction/substitution failed:");
  2985. fn_type_unification (r->u.template_unification.tmpl,
  2986. r->u.template_unification.explicit_targs,
  2987. (make_tree_vec
  2988. (r->u.template_unification.num_targs)),
  2989. r->u.template_unification.args,
  2990. r->u.template_unification.nargs,
  2991. r->u.template_unification.return_type,
  2992. r->u.template_unification.strict,
  2993. r->u.template_unification.flags,
  2994. true, false);
  2995. break;
  2996. case rr_invalid_copy:
  2997. inform (cloc,
  2998. " a constructor taking a single argument of its own "
  2999. "class type is invalid");
  3000. break;
  3001. case rr_none:
  3002. default:
  3003. /* This candidate didn't have any issues or we failed to
  3004. handle a particular code. Either way... */
  3005. gcc_unreachable ();
  3006. }
  3007. }
  3008. }
  3009. static void
  3010. print_z_candidates (location_t loc, struct z_candidate *candidates)
  3011. {
  3012. struct z_candidate *cand1;
  3013. struct z_candidate **cand2;
  3014. int n_candidates;
  3015. if (!candidates)
  3016. return;
  3017. /* Remove non-viable deleted candidates. */
  3018. cand1 = candidates;
  3019. for (cand2 = &cand1; *cand2; )
  3020. {
  3021. if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
  3022. && !(*cand2)->viable
  3023. && DECL_DELETED_FN ((*cand2)->fn))
  3024. *cand2 = (*cand2)->next;
  3025. else
  3026. cand2 = &(*cand2)->next;
  3027. }
  3028. /* ...if there are any non-deleted ones. */
  3029. if (cand1)
  3030. candidates = cand1;
  3031. /* There may be duplicates in the set of candidates. We put off
  3032. checking this condition as long as possible, since we have no way
  3033. to eliminate duplicates from a set of functions in less than n^2
  3034. time. Now we are about to emit an error message, so it is more
  3035. permissible to go slowly. */
  3036. for (cand1 = candidates; cand1; cand1 = cand1->next)
  3037. {
  3038. tree fn = cand1->fn;
  3039. /* Skip builtin candidates and conversion functions. */
  3040. if (!DECL_P (fn))
  3041. continue;
  3042. cand2 = &cand1->next;
  3043. while (*cand2)
  3044. {
  3045. if (DECL_P ((*cand2)->fn)
  3046. && equal_functions (fn, (*cand2)->fn))
  3047. *cand2 = (*cand2)->next;
  3048. else
  3049. cand2 = &(*cand2)->next;
  3050. }
  3051. }
  3052. for (n_candidates = 0, cand1 = candidates; cand1; cand1 = cand1->next)
  3053. n_candidates++;
  3054. for (; candidates; candidates = candidates->next)
  3055. print_z_candidate (loc, "candidate:", candidates);
  3056. }
  3057. /* USER_SEQ is a user-defined conversion sequence, beginning with a
  3058. USER_CONV. STD_SEQ is the standard conversion sequence applied to
  3059. the result of the conversion function to convert it to the final
  3060. desired type. Merge the two sequences into a single sequence,
  3061. and return the merged sequence. */
  3062. static conversion *
  3063. merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
  3064. {
  3065. conversion **t;
  3066. bool bad = user_seq->bad_p;
  3067. gcc_assert (user_seq->kind == ck_user);
  3068. /* Find the end of the second conversion sequence. */
  3069. for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
  3070. {
  3071. /* The entire sequence is a user-conversion sequence. */
  3072. (*t)->user_conv_p = true;
  3073. if (bad)
  3074. (*t)->bad_p = true;
  3075. }
  3076. /* Replace the identity conversion with the user conversion
  3077. sequence. */
  3078. *t = user_seq;
  3079. return std_seq;
  3080. }
  3081. /* Handle overload resolution for initializing an object of class type from
  3082. an initializer list. First we look for a suitable constructor that
  3083. takes a std::initializer_list; if we don't find one, we then look for a
  3084. non-list constructor.
  3085. Parameters are as for add_candidates, except that the arguments are in
  3086. the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
  3087. the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
  3088. static void
  3089. add_list_candidates (tree fns, tree first_arg,
  3090. tree init_list, tree totype,
  3091. tree explicit_targs, bool template_only,
  3092. tree conversion_path, tree access_path,
  3093. int flags,
  3094. struct z_candidate **candidates,
  3095. tsubst_flags_t complain)
  3096. {
  3097. vec<tree, va_gc> *args;
  3098. gcc_assert (*candidates == NULL);
  3099. /* We're looking for a ctor for list-initialization. */
  3100. flags |= LOOKUP_LIST_INIT_CTOR;
  3101. /* And we don't allow narrowing conversions. We also use this flag to
  3102. avoid the copy constructor call for copy-list-initialization. */
  3103. flags |= LOOKUP_NO_NARROWING;
  3104. /* Always use the default constructor if the list is empty (DR 990). */
  3105. if (CONSTRUCTOR_NELTS (init_list) == 0
  3106. && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
  3107. ;
  3108. /* If the class has a list ctor, try passing the list as a single
  3109. argument first, but only consider list ctors. */
  3110. else if (TYPE_HAS_LIST_CTOR (totype))
  3111. {
  3112. flags |= LOOKUP_LIST_ONLY;
  3113. args = make_tree_vector_single (init_list);
  3114. add_candidates (fns, first_arg, args, NULL_TREE,
  3115. explicit_targs, template_only, conversion_path,
  3116. access_path, flags, candidates, complain);
  3117. if (any_strictly_viable (*candidates))
  3118. return;
  3119. }
  3120. args = ctor_to_vec (init_list);
  3121. /* We aren't looking for list-ctors anymore. */
  3122. flags &= ~LOOKUP_LIST_ONLY;
  3123. /* We allow more user-defined conversions within an init-list. */
  3124. flags &= ~LOOKUP_NO_CONVERSION;
  3125. add_candidates (fns, first_arg, args, NULL_TREE,
  3126. explicit_targs, template_only, conversion_path,
  3127. access_path, flags, candidates, complain);
  3128. }
  3129. /* Returns the best overload candidate to perform the requested
  3130. conversion. This function is used for three the overloading situations
  3131. described in [over.match.copy], [over.match.conv], and [over.match.ref].
  3132. If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
  3133. per [dcl.init.ref], so we ignore temporary bindings. */
  3134. static struct z_candidate *
  3135. build_user_type_conversion_1 (tree totype, tree expr, int flags,
  3136. tsubst_flags_t complain)
  3137. {
  3138. struct z_candidate *candidates, *cand;
  3139. tree fromtype;
  3140. tree ctors = NULL_TREE;
  3141. tree conv_fns = NULL_TREE;
  3142. conversion *conv = NULL;
  3143. tree first_arg = NULL_TREE;
  3144. vec<tree, va_gc> *args = NULL;
  3145. bool any_viable_p;
  3146. int convflags;
  3147. if (!expr)
  3148. return NULL;
  3149. fromtype = TREE_TYPE (expr);
  3150. /* We represent conversion within a hierarchy using RVALUE_CONV and
  3151. BASE_CONV, as specified by [over.best.ics]; these become plain
  3152. constructor calls, as specified in [dcl.init]. */
  3153. gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
  3154. || !DERIVED_FROM_P (totype, fromtype));
  3155. if (MAYBE_CLASS_TYPE_P (totype))
  3156. /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
  3157. creating a garbage BASELINK; constructors can't be inherited. */
  3158. ctors = lookup_fnfields_slot (totype, complete_ctor_identifier);
  3159. if (MAYBE_CLASS_TYPE_P (fromtype))
  3160. {
  3161. tree to_nonref = non_reference (totype);
  3162. if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
  3163. (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
  3164. && DERIVED_FROM_P (to_nonref, fromtype)))
  3165. {
  3166. /* [class.conv.fct] A conversion function is never used to
  3167. convert a (possibly cv-qualified) object to the (possibly
  3168. cv-qualified) same object type (or a reference to it), to a
  3169. (possibly cv-qualified) base class of that type (or a
  3170. reference to it)... */
  3171. }
  3172. else
  3173. conv_fns = lookup_conversions (fromtype);
  3174. }
  3175. candidates = 0;
  3176. flags |= LOOKUP_NO_CONVERSION;
  3177. if (BRACE_ENCLOSED_INITIALIZER_P (expr))
  3178. flags |= LOOKUP_NO_NARROWING;
  3179. /* It's OK to bind a temporary for converting constructor arguments, but
  3180. not in converting the return value of a conversion operator. */
  3181. convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
  3182. | (flags & LOOKUP_NO_NARROWING));
  3183. flags &= ~LOOKUP_NO_TEMP_BIND;
  3184. if (ctors)
  3185. {
  3186. int ctorflags = flags;
  3187. first_arg = build_dummy_object (totype);
  3188. /* We should never try to call the abstract or base constructor
  3189. from here. */
  3190. gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_CURRENT (ctors))
  3191. && !DECL_HAS_VTT_PARM_P (OVL_CURRENT (ctors)));
  3192. if (BRACE_ENCLOSED_INITIALIZER_P (expr))
  3193. {
  3194. /* List-initialization. */
  3195. add_list_candidates (ctors, first_arg, expr, totype, NULL_TREE,
  3196. false, TYPE_BINFO (totype), TYPE_BINFO (totype),
  3197. ctorflags, &candidates, complain);
  3198. }
  3199. else
  3200. {
  3201. args = make_tree_vector_single (expr);
  3202. add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
  3203. TYPE_BINFO (totype), TYPE_BINFO (totype),
  3204. ctorflags, &candidates, complain);
  3205. }
  3206. for (cand = candidates; cand; cand = cand->next)
  3207. {
  3208. cand->second_conv = build_identity_conv (totype, NULL_TREE);
  3209. /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't
  3210. set, then this is copy-initialization. In that case, "The
  3211. result of the call is then used to direct-initialize the
  3212. object that is the destination of the copy-initialization."
  3213. [dcl.init]
  3214. We represent this in the conversion sequence with an
  3215. rvalue conversion, which means a constructor call. */
  3216. if (TREE_CODE (totype) != REFERENCE_TYPE
  3217. && !(convflags & LOOKUP_NO_TEMP_BIND))
  3218. cand->second_conv
  3219. = build_conv (ck_rvalue, totype, cand->second_conv);
  3220. }
  3221. }
  3222. if (conv_fns)
  3223. first_arg = expr;
  3224. for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
  3225. {
  3226. tree conversion_path = TREE_PURPOSE (conv_fns);
  3227. struct z_candidate *old_candidates;
  3228. /* If we are called to convert to a reference type, we are trying to
  3229. find a direct binding, so don't even consider temporaries. If
  3230. we don't find a direct binding, the caller will try again to
  3231. look for a temporary binding. */
  3232. if (TREE_CODE (totype) == REFERENCE_TYPE)
  3233. convflags |= LOOKUP_NO_TEMP_BIND;
  3234. old_candidates = candidates;
  3235. add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
  3236. NULL_TREE, false,
  3237. conversion_path, TYPE_BINFO (fromtype),
  3238. flags, &candidates, complain);
  3239. for (cand = candidates; cand != old_candidates; cand = cand->next)
  3240. {
  3241. tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
  3242. conversion *ics
  3243. = implicit_conversion (totype,
  3244. rettype,
  3245. 0,
  3246. /*c_cast_p=*/false, convflags,
  3247. complain);
  3248. /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
  3249. copy-initialization. In that case, "The result of the
  3250. call is then used to direct-initialize the object that is
  3251. the destination of the copy-initialization." [dcl.init]
  3252. We represent this in the conversion sequence with an
  3253. rvalue conversion, which means a constructor call. But
  3254. don't add a second rvalue conversion if there's already
  3255. one there. Which there really shouldn't be, but it's
  3256. harmless since we'd add it here anyway. */
  3257. if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
  3258. && !(convflags & LOOKUP_NO_TEMP_BIND))
  3259. ics = build_conv (ck_rvalue, totype, ics);
  3260. cand->second_conv = ics;
  3261. if (!ics)
  3262. {
  3263. cand->viable = 0;
  3264. cand->reason = arg_conversion_rejection (NULL_TREE, -2,
  3265. rettype, totype);
  3266. }
  3267. else if (DECL_NONCONVERTING_P (cand->fn)
  3268. && ics->rank > cr_exact)
  3269. {
  3270. /* 13.3.1.5: For direct-initialization, those explicit
  3271. conversion functions that are not hidden within S and
  3272. yield type T or a type that can be converted to type T
  3273. with a qualification conversion (4.4) are also candidate
  3274. functions. */
  3275. /* 13.3.1.6 doesn't have a parallel restriction, but it should;
  3276. I've raised this issue with the committee. --jason 9/2011 */
  3277. cand->viable = -1;
  3278. cand->reason = explicit_conversion_rejection (rettype, totype);
  3279. }
  3280. else if (cand->viable == 1 && ics->bad_p)
  3281. {
  3282. cand->viable = -1;
  3283. cand->reason
  3284. = bad_arg_conversion_rejection (NULL_TREE, -2,
  3285. rettype, totype);
  3286. }
  3287. else if (primary_template_instantiation_p (cand->fn)
  3288. && ics->rank > cr_exact)
  3289. {
  3290. /* 13.3.3.1.2: If the user-defined conversion is specified by
  3291. a specialization of a conversion function template, the
  3292. second standard conversion sequence shall have exact match
  3293. rank. */
  3294. cand->viable = -1;
  3295. cand->reason = template_conversion_rejection (rettype, totype);
  3296. }
  3297. }
  3298. }
  3299. candidates = splice_viable (candidates, false, &any_viable_p);
  3300. if (!any_viable_p)
  3301. {
  3302. if (args)
  3303. release_tree_vector (args);
  3304. return NULL;
  3305. }
  3306. cand = tourney (candidates, complain);
  3307. if (cand == 0)
  3308. {
  3309. if (complain & tf_error)
  3310. {
  3311. error ("conversion from %qT to %qT is ambiguous",
  3312. fromtype, totype);
  3313. print_z_candidates (location_of (expr), candidates);
  3314. }
  3315. cand = candidates; /* any one will do */
  3316. cand->second_conv = build_ambiguous_conv (totype, expr);
  3317. cand->second_conv->user_conv_p = true;
  3318. if (!any_strictly_viable (candidates))
  3319. cand->second_conv->bad_p = true;
  3320. /* If there are viable candidates, don't set ICS_BAD_FLAG; an
  3321. ambiguous conversion is no worse than another user-defined
  3322. conversion. */
  3323. return cand;
  3324. }
  3325. tree convtype;
  3326. if (!DECL_CONSTRUCTOR_P (cand->fn))
  3327. convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
  3328. else if (cand->second_conv->kind == ck_rvalue)
  3329. /* DR 5: [in the first step of copy-initialization]...if the function
  3330. is a constructor, the call initializes a temporary of the
  3331. cv-unqualified version of the destination type. */
  3332. convtype = cv_unqualified (totype);
  3333. else
  3334. convtype = totype;
  3335. /* Build the user conversion sequence. */
  3336. conv = build_conv
  3337. (ck_user,
  3338. convtype,
  3339. build_identity_conv (TREE_TYPE (expr), expr));
  3340. conv->cand = cand;
  3341. if (cand->viable == -1)
  3342. conv->bad_p = true;
  3343. /* Remember that this was a list-initialization. */
  3344. if (flags & LOOKUP_NO_NARROWING)
  3345. conv->check_narrowing = true;
  3346. /* Combine it with the second conversion sequence. */
  3347. cand->second_conv = merge_conversion_sequences (conv,
  3348. cand->second_conv);
  3349. return cand;
  3350. }
  3351. /* Wrapper for above. */
  3352. tree
  3353. build_user_type_conversion (tree totype, tree expr, int flags,
  3354. tsubst_flags_t complain)
  3355. {
  3356. struct z_candidate *cand;
  3357. tree ret;
  3358. bool subtime = timevar_cond_start (TV_OVERLOAD);
  3359. cand = build_user_type_conversion_1 (totype, expr, flags, complain);
  3360. if (cand)
  3361. {
  3362. if (cand->second_conv->kind == ck_ambig)
  3363. ret = error_mark_node;
  3364. else
  3365. {
  3366. expr = convert_like (cand->second_conv, expr, complain);
  3367. ret = convert_from_reference (expr);
  3368. }
  3369. }
  3370. else
  3371. ret = NULL_TREE;
  3372. timevar_cond_stop (TV_OVERLOAD, subtime);
  3373. return ret;
  3374. }
  3375. /* Subroutine of convert_nontype_argument.
  3376. EXPR is an argument for a template non-type parameter of integral or
  3377. enumeration type. Do any necessary conversions (that are permitted for
  3378. non-type arguments) to convert it to the parameter type.
  3379. If conversion is successful, returns the converted expression;
  3380. otherwise, returns error_mark_node. */
  3381. tree
  3382. build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
  3383. {
  3384. conversion *conv;
  3385. void *p;
  3386. tree t;
  3387. location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
  3388. if (error_operand_p (expr))
  3389. return error_mark_node;
  3390. gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
  3391. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  3392. p = conversion_obstack_alloc (0);
  3393. conv = implicit_conversion (type, TREE_TYPE (expr), expr,
  3394. /*c_cast_p=*/false,
  3395. LOOKUP_IMPLICIT, complain);
  3396. /* for a non-type template-parameter of integral or
  3397. enumeration type, integral promotions (4.5) and integral
  3398. conversions (4.7) are applied. */
  3399. /* It should be sufficient to check the outermost conversion step, since
  3400. there are no qualification conversions to integer type. */
  3401. if (conv)
  3402. switch (conv->kind)
  3403. {
  3404. /* A conversion function is OK. If it isn't constexpr, we'll
  3405. complain later that the argument isn't constant. */
  3406. case ck_user:
  3407. /* The lvalue-to-rvalue conversion is OK. */
  3408. case ck_rvalue:
  3409. case ck_identity:
  3410. break;
  3411. case ck_std:
  3412. t = next_conversion (conv)->type;
  3413. if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
  3414. break;
  3415. if (complain & tf_error)
  3416. error_at (loc, "conversion from %qT to %qT not considered for "
  3417. "non-type template argument", t, type);
  3418. /* and fall through. */
  3419. default:
  3420. conv = NULL;
  3421. break;
  3422. }
  3423. if (conv)
  3424. expr = convert_like (conv, expr, complain);
  3425. else
  3426. expr = error_mark_node;
  3427. /* Free all the conversions we allocated. */
  3428. obstack_free (&conversion_obstack, p);
  3429. return expr;
  3430. }
  3431. /* Do any initial processing on the arguments to a function call. */
  3432. static vec<tree, va_gc> *
  3433. resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
  3434. {
  3435. unsigned int ix;
  3436. tree arg;
  3437. FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
  3438. {
  3439. if (error_operand_p (arg))
  3440. return NULL;
  3441. else if (VOID_TYPE_P (TREE_TYPE (arg)))
  3442. {
  3443. if (complain & tf_error)
  3444. error ("invalid use of void expression");
  3445. return NULL;
  3446. }
  3447. else if (invalid_nonstatic_memfn_p (arg, complain))
  3448. return NULL;
  3449. }
  3450. return args;
  3451. }
  3452. /* Perform overload resolution on FN, which is called with the ARGS.
  3453. Return the candidate function selected by overload resolution, or
  3454. NULL if the event that overload resolution failed. In the case
  3455. that overload resolution fails, *CANDIDATES will be the set of
  3456. candidates considered, and ANY_VIABLE_P will be set to true or
  3457. false to indicate whether or not any of the candidates were
  3458. viable.
  3459. The ARGS should already have gone through RESOLVE_ARGS before this
  3460. function is called. */
  3461. static struct z_candidate *
  3462. perform_overload_resolution (tree fn,
  3463. const vec<tree, va_gc> *args,
  3464. struct z_candidate **candidates,
  3465. bool *any_viable_p, tsubst_flags_t complain)
  3466. {
  3467. struct z_candidate *cand;
  3468. tree explicit_targs;
  3469. int template_only;
  3470. bool subtime = timevar_cond_start (TV_OVERLOAD);
  3471. explicit_targs = NULL_TREE;
  3472. template_only = 0;
  3473. *candidates = NULL;
  3474. *any_viable_p = true;
  3475. /* Check FN. */
  3476. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  3477. || TREE_CODE (fn) == TEMPLATE_DECL
  3478. || TREE_CODE (fn) == OVERLOAD
  3479. || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
  3480. if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
  3481. {
  3482. explicit_targs = TREE_OPERAND (fn, 1);
  3483. fn = TREE_OPERAND (fn, 0);
  3484. template_only = 1;
  3485. }
  3486. /* Add the various candidate functions. */
  3487. add_candidates (fn, NULL_TREE, args, NULL_TREE,
  3488. explicit_targs, template_only,
  3489. /*conversion_path=*/NULL_TREE,
  3490. /*access_path=*/NULL_TREE,
  3491. LOOKUP_NORMAL,
  3492. candidates, complain);
  3493. *candidates = splice_viable (*candidates, false, any_viable_p);
  3494. if (*any_viable_p)
  3495. cand = tourney (*candidates, complain);
  3496. else
  3497. cand = NULL;
  3498. timevar_cond_stop (TV_OVERLOAD, subtime);
  3499. return cand;
  3500. }
  3501. /* Print an error message about being unable to build a call to FN with
  3502. ARGS. ANY_VIABLE_P indicates whether any candidate functions could
  3503. be located; CANDIDATES is a possibly empty list of such
  3504. functions. */
  3505. static void
  3506. print_error_for_call_failure (tree fn, vec<tree, va_gc> *args,
  3507. struct z_candidate *candidates)
  3508. {
  3509. tree name = DECL_NAME (OVL_CURRENT (fn));
  3510. location_t loc = location_of (name);
  3511. if (!any_strictly_viable (candidates))
  3512. error_at (loc, "no matching function for call to %<%D(%A)%>",
  3513. name, build_tree_list_vec (args));
  3514. else
  3515. error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
  3516. name, build_tree_list_vec (args));
  3517. if (candidates)
  3518. print_z_candidates (loc, candidates);
  3519. }
  3520. /* Return an expression for a call to FN (a namespace-scope function,
  3521. or a static member function) with the ARGS. This may change
  3522. ARGS. */
  3523. tree
  3524. build_new_function_call (tree fn, vec<tree, va_gc> **args, bool koenig_p,
  3525. tsubst_flags_t complain)
  3526. {
  3527. struct z_candidate *candidates, *cand;
  3528. bool any_viable_p;
  3529. void *p;
  3530. tree result;
  3531. if (args != NULL && *args != NULL)
  3532. {
  3533. *args = resolve_args (*args, complain);
  3534. if (*args == NULL)
  3535. return error_mark_node;
  3536. }
  3537. if (flag_tm)
  3538. tm_malloc_replacement (fn);
  3539. /* If this function was found without using argument dependent
  3540. lookup, then we want to ignore any undeclared friend
  3541. functions. */
  3542. if (!koenig_p)
  3543. {
  3544. tree orig_fn = fn;
  3545. fn = remove_hidden_names (fn);
  3546. if (!fn)
  3547. {
  3548. if (complain & tf_error)
  3549. print_error_for_call_failure (orig_fn, *args, NULL);
  3550. return error_mark_node;
  3551. }
  3552. }
  3553. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  3554. p = conversion_obstack_alloc (0);
  3555. cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
  3556. complain);
  3557. if (!cand)
  3558. {
  3559. if (complain & tf_error)
  3560. {
  3561. if (!any_viable_p && candidates && ! candidates->next
  3562. && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
  3563. return cp_build_function_call_vec (candidates->fn, args, complain);
  3564. if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
  3565. fn = TREE_OPERAND (fn, 0);
  3566. print_error_for_call_failure (fn, *args, candidates);
  3567. }
  3568. result = error_mark_node;
  3569. }
  3570. else
  3571. {
  3572. int flags = LOOKUP_NORMAL;
  3573. /* If fn is template_id_expr, the call has explicit template arguments
  3574. (e.g. func<int>(5)), communicate this info to build_over_call
  3575. through flags so that later we can use it to decide whether to warn
  3576. about peculiar null pointer conversion. */
  3577. if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
  3578. flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
  3579. result = build_over_call (cand, flags, complain);
  3580. }
  3581. /* Free all the conversions we allocated. */
  3582. obstack_free (&conversion_obstack, p);
  3583. return result;
  3584. }
  3585. /* Build a call to a global operator new. FNNAME is the name of the
  3586. operator (either "operator new" or "operator new[]") and ARGS are
  3587. the arguments provided. This may change ARGS. *SIZE points to the
  3588. total number of bytes required by the allocation, and is updated if
  3589. that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
  3590. be used. If this function determines that no cookie should be
  3591. used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
  3592. is not NULL_TREE, it is evaluated before calculating the final
  3593. array size, and if it fails, the array size is replaced with
  3594. (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
  3595. is non-NULL, it will be set, upon return, to the allocation
  3596. function called. */
  3597. tree
  3598. build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
  3599. tree *size, tree *cookie_size, tree size_check,
  3600. tree *fn, tsubst_flags_t complain)
  3601. {
  3602. tree original_size = *size;
  3603. tree fns;
  3604. struct z_candidate *candidates;
  3605. struct z_candidate *cand;
  3606. bool any_viable_p;
  3607. if (fn)
  3608. *fn = NULL_TREE;
  3609. /* Set to (size_t)-1 if the size check fails. */
  3610. if (size_check != NULL_TREE)
  3611. {
  3612. tree errval = TYPE_MAX_VALUE (sizetype);
  3613. if (cxx_dialect >= cxx11 && flag_exceptions)
  3614. errval = throw_bad_array_new_length ();
  3615. *size = fold_build3 (COND_EXPR, sizetype, size_check,
  3616. original_size, errval);
  3617. }
  3618. vec_safe_insert (*args, 0, *size);
  3619. *args = resolve_args (*args, complain);
  3620. if (*args == NULL)
  3621. return error_mark_node;
  3622. /* Based on:
  3623. [expr.new]
  3624. If this lookup fails to find the name, or if the allocated type
  3625. is not a class type, the allocation function's name is looked
  3626. up in the global scope.
  3627. we disregard block-scope declarations of "operator new". */
  3628. fns = lookup_function_nonclass (fnname, *args, /*block_p=*/false);
  3629. /* Figure out what function is being called. */
  3630. cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
  3631. complain);
  3632. /* If no suitable function could be found, issue an error message
  3633. and give up. */
  3634. if (!cand)
  3635. {
  3636. if (complain & tf_error)
  3637. print_error_for_call_failure (fns, *args, candidates);
  3638. return error_mark_node;
  3639. }
  3640. /* If a cookie is required, add some extra space. Whether
  3641. or not a cookie is required cannot be determined until
  3642. after we know which function was called. */
  3643. if (*cookie_size)
  3644. {
  3645. bool use_cookie = true;
  3646. tree arg_types;
  3647. arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
  3648. /* Skip the size_t parameter. */
  3649. arg_types = TREE_CHAIN (arg_types);
  3650. /* Check the remaining parameters (if any). */
  3651. if (arg_types
  3652. && TREE_CHAIN (arg_types) == void_list_node
  3653. && same_type_p (TREE_VALUE (arg_types),
  3654. ptr_type_node))
  3655. use_cookie = false;
  3656. /* If we need a cookie, adjust the number of bytes allocated. */
  3657. if (use_cookie)
  3658. {
  3659. /* Update the total size. */
  3660. *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
  3661. /* Set to (size_t)-1 if the size check fails. */
  3662. gcc_assert (size_check != NULL_TREE);
  3663. *size = fold_build3 (COND_EXPR, sizetype, size_check,
  3664. *size, TYPE_MAX_VALUE (sizetype));
  3665. /* Update the argument list to reflect the adjusted size. */
  3666. (**args)[0] = *size;
  3667. }
  3668. else
  3669. *cookie_size = NULL_TREE;
  3670. }
  3671. /* Tell our caller which function we decided to call. */
  3672. if (fn)
  3673. *fn = cand->fn;
  3674. /* Build the CALL_EXPR. */
  3675. return build_over_call (cand, LOOKUP_NORMAL, complain);
  3676. }
  3677. /* Build a new call to operator(). This may change ARGS. */
  3678. static tree
  3679. build_op_call_1 (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
  3680. {
  3681. struct z_candidate *candidates = 0, *cand;
  3682. tree fns, convs, first_mem_arg = NULL_TREE;
  3683. tree type = TREE_TYPE (obj);
  3684. bool any_viable_p;
  3685. tree result = NULL_TREE;
  3686. void *p;
  3687. if (error_operand_p (obj))
  3688. return error_mark_node;
  3689. obj = prep_operand (obj);
  3690. if (TYPE_PTRMEMFUNC_P (type))
  3691. {
  3692. if (complain & tf_error)
  3693. /* It's no good looking for an overloaded operator() on a
  3694. pointer-to-member-function. */
  3695. error ("pointer-to-member function %E cannot be called without an object; consider using .* or ->*", obj);
  3696. return error_mark_node;
  3697. }
  3698. if (TYPE_BINFO (type))
  3699. {
  3700. fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname (CALL_EXPR), 1);
  3701. if (fns == error_mark_node)
  3702. return error_mark_node;
  3703. }
  3704. else
  3705. fns = NULL_TREE;
  3706. if (args != NULL && *args != NULL)
  3707. {
  3708. *args = resolve_args (*args, complain);
  3709. if (*args == NULL)
  3710. return error_mark_node;
  3711. }
  3712. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  3713. p = conversion_obstack_alloc (0);
  3714. if (fns)
  3715. {
  3716. first_mem_arg = obj;
  3717. add_candidates (BASELINK_FUNCTIONS (fns),
  3718. first_mem_arg, *args, NULL_TREE,
  3719. NULL_TREE, false,
  3720. BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
  3721. LOOKUP_NORMAL, &candidates, complain);
  3722. }
  3723. convs = lookup_conversions (type);
  3724. for (; convs; convs = TREE_CHAIN (convs))
  3725. {
  3726. tree fns = TREE_VALUE (convs);
  3727. tree totype = TREE_TYPE (convs);
  3728. if (TYPE_PTRFN_P (totype)
  3729. || TYPE_REFFN_P (totype)
  3730. || (TREE_CODE (totype) == REFERENCE_TYPE
  3731. && TYPE_PTRFN_P (TREE_TYPE (totype))))
  3732. for (; fns; fns = OVL_NEXT (fns))
  3733. {
  3734. tree fn = OVL_CURRENT (fns);
  3735. if (DECL_NONCONVERTING_P (fn))
  3736. continue;
  3737. if (TREE_CODE (fn) == TEMPLATE_DECL)
  3738. add_template_conv_candidate
  3739. (&candidates, fn, obj, NULL_TREE, *args, totype,
  3740. /*access_path=*/NULL_TREE,
  3741. /*conversion_path=*/NULL_TREE, complain);
  3742. else
  3743. add_conv_candidate (&candidates, fn, obj, NULL_TREE,
  3744. *args, /*conversion_path=*/NULL_TREE,
  3745. /*access_path=*/NULL_TREE, complain);
  3746. }
  3747. }
  3748. /* Be strict here because if we choose a bad conversion candidate, the
  3749. errors we get won't mention the call context. */
  3750. candidates = splice_viable (candidates, true, &any_viable_p);
  3751. if (!any_viable_p)
  3752. {
  3753. if (complain & tf_error)
  3754. {
  3755. error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
  3756. build_tree_list_vec (*args));
  3757. print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
  3758. }
  3759. result = error_mark_node;
  3760. }
  3761. else
  3762. {
  3763. cand = tourney (candidates, complain);
  3764. if (cand == 0)
  3765. {
  3766. if (complain & tf_error)
  3767. {
  3768. error ("call of %<(%T) (%A)%> is ambiguous",
  3769. TREE_TYPE (obj), build_tree_list_vec (*args));
  3770. print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
  3771. }
  3772. result = error_mark_node;
  3773. }
  3774. /* Since cand->fn will be a type, not a function, for a conversion
  3775. function, we must be careful not to unconditionally look at
  3776. DECL_NAME here. */
  3777. else if (TREE_CODE (cand->fn) == FUNCTION_DECL
  3778. && DECL_OVERLOADED_OPERATOR_P (cand->fn) == CALL_EXPR)
  3779. result = build_over_call (cand, LOOKUP_NORMAL, complain);
  3780. else
  3781. {
  3782. obj = convert_like_with_context (cand->convs[0], obj, cand->fn, -1,
  3783. complain);
  3784. obj = convert_from_reference (obj);
  3785. result = cp_build_function_call_vec (obj, args, complain);
  3786. }
  3787. }
  3788. /* Free all the conversions we allocated. */
  3789. obstack_free (&conversion_obstack, p);
  3790. return result;
  3791. }
  3792. /* Wrapper for above. */
  3793. tree
  3794. build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
  3795. {
  3796. tree ret;
  3797. bool subtime = timevar_cond_start (TV_OVERLOAD);
  3798. ret = build_op_call_1 (obj, args, complain);
  3799. timevar_cond_stop (TV_OVERLOAD, subtime);
  3800. return ret;
  3801. }
  3802. /* Called by op_error to prepare format strings suitable for the error
  3803. function. It concatenates a prefix (controlled by MATCH), ERRMSG,
  3804. and a suffix (controlled by NTYPES). */
  3805. static const char *
  3806. op_error_string (const char *errmsg, int ntypes, bool match)
  3807. {
  3808. const char *msg;
  3809. const char *msgp = concat (match ? G_("ambiguous overload for ")
  3810. : G_("no match for "), errmsg, NULL);
  3811. if (ntypes == 3)
  3812. msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
  3813. else if (ntypes == 2)
  3814. msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
  3815. else
  3816. msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
  3817. return msg;
  3818. }
  3819. static void
  3820. op_error (location_t loc, enum tree_code code, enum tree_code code2,
  3821. tree arg1, tree arg2, tree arg3, bool match)
  3822. {
  3823. const char *opname;
  3824. if (code == MODIFY_EXPR)
  3825. opname = assignment_operator_name_info[code2].name;
  3826. else
  3827. opname = operator_name_info[code].name;
  3828. switch (code)
  3829. {
  3830. case COND_EXPR:
  3831. if (flag_diagnostics_show_caret)
  3832. error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
  3833. 3, match),
  3834. TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
  3835. else
  3836. error_at (loc, op_error_string (G_("ternary %<operator?:%> "
  3837. "in %<%E ? %E : %E%>"), 3, match),
  3838. arg1, arg2, arg3,
  3839. TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
  3840. break;
  3841. case POSTINCREMENT_EXPR:
  3842. case POSTDECREMENT_EXPR:
  3843. if (flag_diagnostics_show_caret)
  3844. error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
  3845. opname, TREE_TYPE (arg1));
  3846. else
  3847. error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
  3848. 1, match),
  3849. opname, arg1, opname, TREE_TYPE (arg1));
  3850. break;
  3851. case ARRAY_REF:
  3852. if (flag_diagnostics_show_caret)
  3853. error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
  3854. TREE_TYPE (arg1), TREE_TYPE (arg2));
  3855. else
  3856. error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
  3857. 2, match),
  3858. arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
  3859. break;
  3860. case REALPART_EXPR:
  3861. case IMAGPART_EXPR:
  3862. if (flag_diagnostics_show_caret)
  3863. error_at (loc, op_error_string (G_("%qs"), 1, match),
  3864. opname, TREE_TYPE (arg1));
  3865. else
  3866. error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
  3867. opname, opname, arg1, TREE_TYPE (arg1));
  3868. break;
  3869. default:
  3870. if (arg2)
  3871. if (flag_diagnostics_show_caret)
  3872. error_at (loc, op_error_string (G_("%<operator%s%>"), 2, match),
  3873. opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
  3874. else
  3875. error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
  3876. 2, match),
  3877. opname, arg1, opname, arg2,
  3878. TREE_TYPE (arg1), TREE_TYPE (arg2));
  3879. else
  3880. if (flag_diagnostics_show_caret)
  3881. error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
  3882. opname, TREE_TYPE (arg1));
  3883. else
  3884. error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
  3885. 1, match),
  3886. opname, opname, arg1, TREE_TYPE (arg1));
  3887. break;
  3888. }
  3889. }
  3890. /* Return the implicit conversion sequence that could be used to
  3891. convert E1 to E2 in [expr.cond]. */
  3892. static conversion *
  3893. conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
  3894. {
  3895. tree t1 = non_reference (TREE_TYPE (e1));
  3896. tree t2 = non_reference (TREE_TYPE (e2));
  3897. conversion *conv;
  3898. bool good_base;
  3899. /* [expr.cond]
  3900. If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
  3901. implicitly converted (clause _conv_) to the type "lvalue reference to
  3902. T2", subject to the constraint that in the conversion the
  3903. reference must bind directly (_dcl.init.ref_) to an lvalue.
  3904. If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
  3905. implicitly converted to the type "rvalue reference to T2", subject to
  3906. the constraint that the reference must bind directly. */
  3907. if (lvalue_or_rvalue_with_address_p (e2))
  3908. {
  3909. tree rtype = cp_build_reference_type (t2, !real_lvalue_p (e2));
  3910. conv = implicit_conversion (rtype,
  3911. t1,
  3912. e1,
  3913. /*c_cast_p=*/false,
  3914. LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
  3915. |LOOKUP_ONLYCONVERTING,
  3916. complain);
  3917. if (conv && !conv->bad_p)
  3918. return conv;
  3919. }
  3920. /* If E2 is a prvalue or if neither of the conversions above can be done
  3921. and at least one of the operands has (possibly cv-qualified) class
  3922. type: */
  3923. if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
  3924. return NULL;
  3925. /* [expr.cond]
  3926. If E1 and E2 have class type, and the underlying class types are
  3927. the same or one is a base class of the other: E1 can be converted
  3928. to match E2 if the class of T2 is the same type as, or a base
  3929. class of, the class of T1, and the cv-qualification of T2 is the
  3930. same cv-qualification as, or a greater cv-qualification than, the
  3931. cv-qualification of T1. If the conversion is applied, E1 is
  3932. changed to an rvalue of type T2 that still refers to the original
  3933. source class object (or the appropriate subobject thereof). */
  3934. if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
  3935. && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
  3936. {
  3937. if (good_base && at_least_as_qualified_p (t2, t1))
  3938. {
  3939. conv = build_identity_conv (t1, e1);
  3940. if (!same_type_p (TYPE_MAIN_VARIANT (t1),
  3941. TYPE_MAIN_VARIANT (t2)))
  3942. conv = build_conv (ck_base, t2, conv);
  3943. else
  3944. conv = build_conv (ck_rvalue, t2, conv);
  3945. return conv;
  3946. }
  3947. else
  3948. return NULL;
  3949. }
  3950. else
  3951. /* [expr.cond]
  3952. Otherwise: E1 can be converted to match E2 if E1 can be implicitly
  3953. converted to the type that expression E2 would have if E2 were
  3954. converted to an rvalue (or the type it has, if E2 is an rvalue). */
  3955. return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
  3956. LOOKUP_IMPLICIT, complain);
  3957. }
  3958. /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
  3959. arguments to the conditional expression. */
  3960. static tree
  3961. build_conditional_expr_1 (location_t loc, tree arg1, tree arg2, tree arg3,
  3962. tsubst_flags_t complain)
  3963. {
  3964. tree arg2_type;
  3965. tree arg3_type;
  3966. tree result = NULL_TREE;
  3967. tree result_type = NULL_TREE;
  3968. bool lvalue_p = true;
  3969. struct z_candidate *candidates = 0;
  3970. struct z_candidate *cand;
  3971. void *p;
  3972. tree orig_arg2, orig_arg3;
  3973. /* As a G++ extension, the second argument to the conditional can be
  3974. omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
  3975. c'.) If the second operand is omitted, make sure it is
  3976. calculated only once. */
  3977. if (!arg2)
  3978. {
  3979. if (complain & tf_error)
  3980. pedwarn (loc, OPT_Wpedantic,
  3981. "ISO C++ forbids omitting the middle term of a ?: expression");
  3982. /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
  3983. if (real_lvalue_p (arg1))
  3984. arg2 = arg1 = stabilize_reference (arg1);
  3985. else
  3986. arg2 = arg1 = save_expr (arg1);
  3987. }
  3988. /* If something has already gone wrong, just pass that fact up the
  3989. tree. */
  3990. if (error_operand_p (arg1)
  3991. || error_operand_p (arg2)
  3992. || error_operand_p (arg3))
  3993. return error_mark_node;
  3994. orig_arg2 = arg2;
  3995. orig_arg3 = arg3;
  3996. if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
  3997. {
  3998. arg1 = force_rvalue (arg1, complain);
  3999. arg2 = force_rvalue (arg2, complain);
  4000. arg3 = force_rvalue (arg3, complain);
  4001. /* force_rvalue can return error_mark on valid arguments. */
  4002. if (error_operand_p (arg1)
  4003. || error_operand_p (arg2)
  4004. || error_operand_p (arg3))
  4005. return error_mark_node;
  4006. tree arg1_type = TREE_TYPE (arg1);
  4007. arg2_type = TREE_TYPE (arg2);
  4008. arg3_type = TREE_TYPE (arg3);
  4009. if (TREE_CODE (arg2_type) != VECTOR_TYPE
  4010. && TREE_CODE (arg3_type) != VECTOR_TYPE)
  4011. {
  4012. /* Rely on the error messages of the scalar version. */
  4013. tree scal = build_conditional_expr_1 (loc, integer_one_node,
  4014. orig_arg2, orig_arg3, complain);
  4015. if (scal == error_mark_node)
  4016. return error_mark_node;
  4017. tree stype = TREE_TYPE (scal);
  4018. tree ctype = TREE_TYPE (arg1_type);
  4019. if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
  4020. || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
  4021. {
  4022. if (complain & tf_error)
  4023. error_at (loc, "inferred scalar type %qT is not an integer or "
  4024. "floating point type of the same size as %qT", stype,
  4025. COMPARISON_CLASS_P (arg1)
  4026. ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
  4027. : ctype);
  4028. return error_mark_node;
  4029. }
  4030. tree vtype = build_opaque_vector_type (stype,
  4031. TYPE_VECTOR_SUBPARTS (arg1_type));
  4032. /* We could pass complain & tf_warning to unsafe_conversion_p,
  4033. but the warnings (like Wsign-conversion) have already been
  4034. given by the scalar build_conditional_expr_1. We still check
  4035. unsafe_conversion_p to forbid truncating long long -> float. */
  4036. if (unsafe_conversion_p (loc, stype, arg2, false))
  4037. {
  4038. if (complain & tf_error)
  4039. error_at (loc, "conversion of scalar %qT to vector %qT "
  4040. "involves truncation", arg2_type, vtype);
  4041. return error_mark_node;
  4042. }
  4043. if (unsafe_conversion_p (loc, stype, arg3, false))
  4044. {
  4045. if (complain & tf_error)
  4046. error_at (loc, "conversion of scalar %qT to vector %qT "
  4047. "involves truncation", arg3_type, vtype);
  4048. return error_mark_node;
  4049. }
  4050. arg2 = cp_convert (stype, arg2, complain);
  4051. arg2 = save_expr (arg2);
  4052. arg2 = build_vector_from_val (vtype, arg2);
  4053. arg2_type = vtype;
  4054. arg3 = cp_convert (stype, arg3, complain);
  4055. arg3 = save_expr (arg3);
  4056. arg3 = build_vector_from_val (vtype, arg3);
  4057. arg3_type = vtype;
  4058. }
  4059. if ((TREE_CODE (arg2_type) == VECTOR_TYPE)
  4060. != (TREE_CODE (arg3_type) == VECTOR_TYPE))
  4061. {
  4062. enum stv_conv convert_flag =
  4063. scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
  4064. complain & tf_error);
  4065. switch (convert_flag)
  4066. {
  4067. case stv_error:
  4068. return error_mark_node;
  4069. case stv_firstarg:
  4070. {
  4071. arg2 = save_expr (arg2);
  4072. arg2 = convert (TREE_TYPE (arg3_type), arg2);
  4073. arg2 = build_vector_from_val (arg3_type, arg2);
  4074. arg2_type = TREE_TYPE (arg2);
  4075. break;
  4076. }
  4077. case stv_secondarg:
  4078. {
  4079. arg3 = save_expr (arg3);
  4080. arg3 = convert (TREE_TYPE (arg2_type), arg3);
  4081. arg3 = build_vector_from_val (arg2_type, arg3);
  4082. arg3_type = TREE_TYPE (arg3);
  4083. break;
  4084. }
  4085. default:
  4086. break;
  4087. }
  4088. }
  4089. if (!same_type_p (arg2_type, arg3_type)
  4090. || TYPE_VECTOR_SUBPARTS (arg1_type)
  4091. != TYPE_VECTOR_SUBPARTS (arg2_type)
  4092. || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
  4093. {
  4094. if (complain & tf_error)
  4095. error_at (loc,
  4096. "incompatible vector types in conditional expression: "
  4097. "%qT, %qT and %qT", TREE_TYPE (arg1),
  4098. TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
  4099. return error_mark_node;
  4100. }
  4101. if (!COMPARISON_CLASS_P (arg1))
  4102. arg1 = cp_build_binary_op (loc, NE_EXPR, arg1,
  4103. build_zero_cst (arg1_type), complain);
  4104. return fold_build3 (VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
  4105. }
  4106. /* [expr.cond]
  4107. The first expression is implicitly converted to bool (clause
  4108. _conv_). */
  4109. arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
  4110. LOOKUP_NORMAL);
  4111. if (error_operand_p (arg1))
  4112. return error_mark_node;
  4113. /* [expr.cond]
  4114. If either the second or the third operand has type (possibly
  4115. cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
  4116. array-to-pointer (_conv.array_), and function-to-pointer
  4117. (_conv.func_) standard conversions are performed on the second
  4118. and third operands. */
  4119. arg2_type = unlowered_expr_type (arg2);
  4120. arg3_type = unlowered_expr_type (arg3);
  4121. if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
  4122. {
  4123. /* Do the conversions. We don't these for `void' type arguments
  4124. since it can't have any effect and since decay_conversion
  4125. does not handle that case gracefully. */
  4126. if (!VOID_TYPE_P (arg2_type))
  4127. arg2 = decay_conversion (arg2, complain);
  4128. if (!VOID_TYPE_P (arg3_type))
  4129. arg3 = decay_conversion (arg3, complain);
  4130. arg2_type = TREE_TYPE (arg2);
  4131. arg3_type = TREE_TYPE (arg3);
  4132. /* [expr.cond]
  4133. One of the following shall hold:
  4134. --The second or the third operand (but not both) is a
  4135. throw-expression (_except.throw_); the result is of the
  4136. type of the other and is an rvalue.
  4137. --Both the second and the third operands have type void; the
  4138. result is of type void and is an rvalue.
  4139. We must avoid calling force_rvalue for expressions of type
  4140. "void" because it will complain that their value is being
  4141. used. */
  4142. if (TREE_CODE (arg2) == THROW_EXPR
  4143. && TREE_CODE (arg3) != THROW_EXPR)
  4144. {
  4145. if (!VOID_TYPE_P (arg3_type))
  4146. {
  4147. arg3 = force_rvalue (arg3, complain);
  4148. if (arg3 == error_mark_node)
  4149. return error_mark_node;
  4150. }
  4151. arg3_type = TREE_TYPE (arg3);
  4152. result_type = arg3_type;
  4153. }
  4154. else if (TREE_CODE (arg2) != THROW_EXPR
  4155. && TREE_CODE (arg3) == THROW_EXPR)
  4156. {
  4157. if (!VOID_TYPE_P (arg2_type))
  4158. {
  4159. arg2 = force_rvalue (arg2, complain);
  4160. if (arg2 == error_mark_node)
  4161. return error_mark_node;
  4162. }
  4163. arg2_type = TREE_TYPE (arg2);
  4164. result_type = arg2_type;
  4165. }
  4166. else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
  4167. result_type = void_type_node;
  4168. else
  4169. {
  4170. if (complain & tf_error)
  4171. {
  4172. if (VOID_TYPE_P (arg2_type))
  4173. error_at (EXPR_LOC_OR_LOC (arg3, loc),
  4174. "second operand to the conditional operator "
  4175. "is of type %<void%>, but the third operand is "
  4176. "neither a throw-expression nor of type %<void%>");
  4177. else
  4178. error_at (EXPR_LOC_OR_LOC (arg2, loc),
  4179. "third operand to the conditional operator "
  4180. "is of type %<void%>, but the second operand is "
  4181. "neither a throw-expression nor of type %<void%>");
  4182. }
  4183. return error_mark_node;
  4184. }
  4185. lvalue_p = false;
  4186. goto valid_operands;
  4187. }
  4188. /* [expr.cond]
  4189. Otherwise, if the second and third operand have different types,
  4190. and either has (possibly cv-qualified) class type, or if both are
  4191. glvalues of the same value category and the same type except for
  4192. cv-qualification, an attempt is made to convert each of those operands
  4193. to the type of the other. */
  4194. else if (!same_type_p (arg2_type, arg3_type)
  4195. && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
  4196. || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
  4197. arg3_type)
  4198. && lvalue_or_rvalue_with_address_p (arg2)
  4199. && lvalue_or_rvalue_with_address_p (arg3)
  4200. && real_lvalue_p (arg2) == real_lvalue_p (arg3))))
  4201. {
  4202. conversion *conv2;
  4203. conversion *conv3;
  4204. bool converted = false;
  4205. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  4206. p = conversion_obstack_alloc (0);
  4207. conv2 = conditional_conversion (arg2, arg3, complain);
  4208. conv3 = conditional_conversion (arg3, arg2, complain);
  4209. /* [expr.cond]
  4210. If both can be converted, or one can be converted but the
  4211. conversion is ambiguous, the program is ill-formed. If
  4212. neither can be converted, the operands are left unchanged and
  4213. further checking is performed as described below. If exactly
  4214. one conversion is possible, that conversion is applied to the
  4215. chosen operand and the converted operand is used in place of
  4216. the original operand for the remainder of this section. */
  4217. if ((conv2 && !conv2->bad_p
  4218. && conv3 && !conv3->bad_p)
  4219. || (conv2 && conv2->kind == ck_ambig)
  4220. || (conv3 && conv3->kind == ck_ambig))
  4221. {
  4222. if (complain & tf_error)
  4223. {
  4224. error_at (loc, "operands to ?: have different types %qT and %qT",
  4225. arg2_type, arg3_type);
  4226. if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
  4227. inform (loc, " and each type can be converted to the other");
  4228. else if (conv2 && conv2->kind == ck_ambig)
  4229. convert_like (conv2, arg2, complain);
  4230. else
  4231. convert_like (conv3, arg3, complain);
  4232. }
  4233. result = error_mark_node;
  4234. }
  4235. else if (conv2 && !conv2->bad_p)
  4236. {
  4237. arg2 = convert_like (conv2, arg2, complain);
  4238. arg2 = convert_from_reference (arg2);
  4239. arg2_type = TREE_TYPE (arg2);
  4240. /* Even if CONV2 is a valid conversion, the result of the
  4241. conversion may be invalid. For example, if ARG3 has type
  4242. "volatile X", and X does not have a copy constructor
  4243. accepting a "volatile X&", then even if ARG2 can be
  4244. converted to X, the conversion will fail. */
  4245. if (error_operand_p (arg2))
  4246. result = error_mark_node;
  4247. converted = true;
  4248. }
  4249. else if (conv3 && !conv3->bad_p)
  4250. {
  4251. arg3 = convert_like (conv3, arg3, complain);
  4252. arg3 = convert_from_reference (arg3);
  4253. arg3_type = TREE_TYPE (arg3);
  4254. if (error_operand_p (arg3))
  4255. result = error_mark_node;
  4256. converted = true;
  4257. }
  4258. /* Free all the conversions we allocated. */
  4259. obstack_free (&conversion_obstack, p);
  4260. if (result)
  4261. return result;
  4262. /* If, after the conversion, both operands have class type,
  4263. treat the cv-qualification of both operands as if it were the
  4264. union of the cv-qualification of the operands.
  4265. The standard is not clear about what to do in this
  4266. circumstance. For example, if the first operand has type
  4267. "const X" and the second operand has a user-defined
  4268. conversion to "volatile X", what is the type of the second
  4269. operand after this step? Making it be "const X" (matching
  4270. the first operand) seems wrong, as that discards the
  4271. qualification without actually performing a copy. Leaving it
  4272. as "volatile X" seems wrong as that will result in the
  4273. conditional expression failing altogether, even though,
  4274. according to this step, the one operand could be converted to
  4275. the type of the other. */
  4276. if (converted
  4277. && CLASS_TYPE_P (arg2_type)
  4278. && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
  4279. arg2_type = arg3_type =
  4280. cp_build_qualified_type (arg2_type,
  4281. cp_type_quals (arg2_type)
  4282. | cp_type_quals (arg3_type));
  4283. }
  4284. /* [expr.cond]
  4285. If the second and third operands are glvalues of the same value
  4286. category and have the same type, the result is of that type and
  4287. value category. */
  4288. if (((real_lvalue_p (arg2) && real_lvalue_p (arg3))
  4289. || (xvalue_p (arg2) && xvalue_p (arg3)))
  4290. && same_type_p (arg2_type, arg3_type))
  4291. {
  4292. result_type = arg2_type;
  4293. arg2 = mark_lvalue_use (arg2);
  4294. arg3 = mark_lvalue_use (arg3);
  4295. goto valid_operands;
  4296. }
  4297. /* [expr.cond]
  4298. Otherwise, the result is an rvalue. If the second and third
  4299. operand do not have the same type, and either has (possibly
  4300. cv-qualified) class type, overload resolution is used to
  4301. determine the conversions (if any) to be applied to the operands
  4302. (_over.match.oper_, _over.built_). */
  4303. lvalue_p = false;
  4304. if (!same_type_p (arg2_type, arg3_type)
  4305. && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
  4306. {
  4307. tree args[3];
  4308. conversion *conv;
  4309. bool any_viable_p;
  4310. /* Rearrange the arguments so that add_builtin_candidate only has
  4311. to know about two args. In build_builtin_candidate, the
  4312. arguments are unscrambled. */
  4313. args[0] = arg2;
  4314. args[1] = arg3;
  4315. args[2] = arg1;
  4316. add_builtin_candidates (&candidates,
  4317. COND_EXPR,
  4318. NOP_EXPR,
  4319. ansi_opname (COND_EXPR),
  4320. args,
  4321. LOOKUP_NORMAL, complain);
  4322. /* [expr.cond]
  4323. If the overload resolution fails, the program is
  4324. ill-formed. */
  4325. candidates = splice_viable (candidates, false, &any_viable_p);
  4326. if (!any_viable_p)
  4327. {
  4328. if (complain & tf_error)
  4329. error_at (loc, "operands to ?: have different types %qT and %qT",
  4330. arg2_type, arg3_type);
  4331. return error_mark_node;
  4332. }
  4333. cand = tourney (candidates, complain);
  4334. if (!cand)
  4335. {
  4336. if (complain & tf_error)
  4337. {
  4338. op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
  4339. print_z_candidates (loc, candidates);
  4340. }
  4341. return error_mark_node;
  4342. }
  4343. /* [expr.cond]
  4344. Otherwise, the conversions thus determined are applied, and
  4345. the converted operands are used in place of the original
  4346. operands for the remainder of this section. */
  4347. conv = cand->convs[0];
  4348. arg1 = convert_like (conv, arg1, complain);
  4349. conv = cand->convs[1];
  4350. arg2 = convert_like (conv, arg2, complain);
  4351. arg2_type = TREE_TYPE (arg2);
  4352. conv = cand->convs[2];
  4353. arg3 = convert_like (conv, arg3, complain);
  4354. arg3_type = TREE_TYPE (arg3);
  4355. }
  4356. /* [expr.cond]
  4357. Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
  4358. and function-to-pointer (_conv.func_) standard conversions are
  4359. performed on the second and third operands.
  4360. We need to force the lvalue-to-rvalue conversion here for class types,
  4361. so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
  4362. that isn't wrapped with a TARGET_EXPR plays havoc with exception
  4363. regions. */
  4364. arg2 = force_rvalue (arg2, complain);
  4365. if (!CLASS_TYPE_P (arg2_type))
  4366. arg2_type = TREE_TYPE (arg2);
  4367. arg3 = force_rvalue (arg3, complain);
  4368. if (!CLASS_TYPE_P (arg3_type))
  4369. arg3_type = TREE_TYPE (arg3);
  4370. if (arg2 == error_mark_node || arg3 == error_mark_node)
  4371. return error_mark_node;
  4372. /* [expr.cond]
  4373. After those conversions, one of the following shall hold:
  4374. --The second and third operands have the same type; the result is of
  4375. that type. */
  4376. if (same_type_p (arg2_type, arg3_type))
  4377. result_type = arg2_type;
  4378. /* [expr.cond]
  4379. --The second and third operands have arithmetic or enumeration
  4380. type; the usual arithmetic conversions are performed to bring
  4381. them to a common type, and the result is of that type. */
  4382. else if ((ARITHMETIC_TYPE_P (arg2_type)
  4383. || UNSCOPED_ENUM_P (arg2_type))
  4384. && (ARITHMETIC_TYPE_P (arg3_type)
  4385. || UNSCOPED_ENUM_P (arg3_type)))
  4386. {
  4387. /* In this case, there is always a common type. */
  4388. result_type = type_after_usual_arithmetic_conversions (arg2_type,
  4389. arg3_type);
  4390. if (complain & tf_warning)
  4391. do_warn_double_promotion (result_type, arg2_type, arg3_type,
  4392. "implicit conversion from %qT to %qT to "
  4393. "match other result of conditional",
  4394. loc);
  4395. if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
  4396. && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
  4397. {
  4398. if (TREE_CODE (orig_arg2) == CONST_DECL
  4399. && TREE_CODE (orig_arg3) == CONST_DECL
  4400. && DECL_CONTEXT (orig_arg2) == DECL_CONTEXT (orig_arg3))
  4401. /* Two enumerators from the same enumeration can have different
  4402. types when the enumeration is still being defined. */;
  4403. else if (complain & tf_warning)
  4404. warning_at (loc, OPT_Wenum_compare, "enumeral mismatch in "
  4405. "conditional expression: %qT vs %qT",
  4406. arg2_type, arg3_type);
  4407. }
  4408. else if (extra_warnings
  4409. && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
  4410. && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
  4411. || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
  4412. && !same_type_p (arg2_type,
  4413. type_promotes_to (arg3_type)))))
  4414. {
  4415. if (complain & tf_warning)
  4416. warning_at (loc, OPT_Wextra, "enumeral and non-enumeral type in "
  4417. "conditional expression");
  4418. }
  4419. arg2 = perform_implicit_conversion (result_type, arg2, complain);
  4420. arg3 = perform_implicit_conversion (result_type, arg3, complain);
  4421. }
  4422. /* [expr.cond]
  4423. --The second and third operands have pointer type, or one has
  4424. pointer type and the other is a null pointer constant; pointer
  4425. conversions (_conv.ptr_) and qualification conversions
  4426. (_conv.qual_) are performed to bring them to their composite
  4427. pointer type (_expr.rel_). The result is of the composite
  4428. pointer type.
  4429. --The second and third operands have pointer to member type, or
  4430. one has pointer to member type and the other is a null pointer
  4431. constant; pointer to member conversions (_conv.mem_) and
  4432. qualification conversions (_conv.qual_) are performed to bring
  4433. them to a common type, whose cv-qualification shall match the
  4434. cv-qualification of either the second or the third operand.
  4435. The result is of the common type. */
  4436. else if ((null_ptr_cst_p (arg2)
  4437. && TYPE_PTR_OR_PTRMEM_P (arg3_type))
  4438. || (null_ptr_cst_p (arg3)
  4439. && TYPE_PTR_OR_PTRMEM_P (arg2_type))
  4440. || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
  4441. || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
  4442. || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
  4443. {
  4444. result_type = composite_pointer_type (arg2_type, arg3_type, arg2,
  4445. arg3, CPO_CONDITIONAL_EXPR,
  4446. complain);
  4447. if (result_type == error_mark_node)
  4448. return error_mark_node;
  4449. arg2 = perform_implicit_conversion (result_type, arg2, complain);
  4450. arg3 = perform_implicit_conversion (result_type, arg3, complain);
  4451. }
  4452. if (!result_type)
  4453. {
  4454. if (complain & tf_error)
  4455. error_at (loc, "operands to ?: have different types %qT and %qT",
  4456. arg2_type, arg3_type);
  4457. return error_mark_node;
  4458. }
  4459. if (arg2 == error_mark_node || arg3 == error_mark_node)
  4460. return error_mark_node;
  4461. valid_operands:
  4462. result = build3 (COND_EXPR, result_type, arg1, arg2, arg3);
  4463. if (!cp_unevaluated_operand)
  4464. /* Avoid folding within decltype (c++/42013) and noexcept. */
  4465. result = fold_if_not_in_template (result);
  4466. /* We can't use result_type below, as fold might have returned a
  4467. throw_expr. */
  4468. if (!lvalue_p)
  4469. {
  4470. /* Expand both sides into the same slot, hopefully the target of
  4471. the ?: expression. We used to check for TARGET_EXPRs here,
  4472. but now we sometimes wrap them in NOP_EXPRs so the test would
  4473. fail. */
  4474. if (CLASS_TYPE_P (TREE_TYPE (result)))
  4475. result = get_target_expr_sfinae (result, complain);
  4476. /* If this expression is an rvalue, but might be mistaken for an
  4477. lvalue, we must add a NON_LVALUE_EXPR. */
  4478. result = rvalue (result);
  4479. }
  4480. else
  4481. result = force_paren_expr (result);
  4482. return result;
  4483. }
  4484. /* Wrapper for above. */
  4485. tree
  4486. build_conditional_expr (location_t loc, tree arg1, tree arg2, tree arg3,
  4487. tsubst_flags_t complain)
  4488. {
  4489. tree ret;
  4490. bool subtime = timevar_cond_start (TV_OVERLOAD);
  4491. ret = build_conditional_expr_1 (loc, arg1, arg2, arg3, complain);
  4492. timevar_cond_stop (TV_OVERLOAD, subtime);
  4493. return ret;
  4494. }
  4495. /* OPERAND is an operand to an expression. Perform necessary steps
  4496. required before using it. If OPERAND is NULL_TREE, NULL_TREE is
  4497. returned. */
  4498. static tree
  4499. prep_operand (tree operand)
  4500. {
  4501. if (operand)
  4502. {
  4503. if (CLASS_TYPE_P (TREE_TYPE (operand))
  4504. && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
  4505. /* Make sure the template type is instantiated now. */
  4506. instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
  4507. }
  4508. return operand;
  4509. }
  4510. /* Add each of the viable functions in FNS (a FUNCTION_DECL or
  4511. OVERLOAD) to the CANDIDATES, returning an updated list of
  4512. CANDIDATES. The ARGS are the arguments provided to the call;
  4513. if FIRST_ARG is non-null it is the implicit object argument,
  4514. otherwise the first element of ARGS is used if needed. The
  4515. EXPLICIT_TARGS are explicit template arguments provided.
  4516. TEMPLATE_ONLY is true if only template functions should be
  4517. considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
  4518. add_function_candidate. */
  4519. static void
  4520. add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
  4521. tree return_type,
  4522. tree explicit_targs, bool template_only,
  4523. tree conversion_path, tree access_path,
  4524. int flags,
  4525. struct z_candidate **candidates,
  4526. tsubst_flags_t complain)
  4527. {
  4528. tree ctype;
  4529. const vec<tree, va_gc> *non_static_args;
  4530. bool check_list_ctor;
  4531. bool check_converting;
  4532. unification_kind_t strict;
  4533. tree fn;
  4534. if (!fns)
  4535. return;
  4536. /* Precalculate special handling of constructors and conversion ops. */
  4537. fn = OVL_CURRENT (fns);
  4538. if (DECL_CONV_FN_P (fn))
  4539. {
  4540. check_list_ctor = false;
  4541. check_converting = !!(flags & LOOKUP_ONLYCONVERTING);
  4542. if (flags & LOOKUP_NO_CONVERSION)
  4543. /* We're doing return_type(x). */
  4544. strict = DEDUCE_CONV;
  4545. else
  4546. /* We're doing x.operator return_type(). */
  4547. strict = DEDUCE_EXACT;
  4548. /* [over.match.funcs] For conversion functions, the function
  4549. is considered to be a member of the class of the implicit
  4550. object argument for the purpose of defining the type of
  4551. the implicit object parameter. */
  4552. ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
  4553. }
  4554. else
  4555. {
  4556. if (DECL_CONSTRUCTOR_P (fn))
  4557. {
  4558. check_list_ctor = !!(flags & LOOKUP_LIST_ONLY);
  4559. /* For list-initialization we consider explicit constructors
  4560. and complain if one is chosen. */
  4561. check_converting
  4562. = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
  4563. == LOOKUP_ONLYCONVERTING);
  4564. }
  4565. else
  4566. {
  4567. check_list_ctor = false;
  4568. check_converting = false;
  4569. }
  4570. strict = DEDUCE_CALL;
  4571. ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
  4572. }
  4573. if (first_arg)
  4574. non_static_args = args;
  4575. else
  4576. /* Delay creating the implicit this parameter until it is needed. */
  4577. non_static_args = NULL;
  4578. for (; fns; fns = OVL_NEXT (fns))
  4579. {
  4580. tree fn_first_arg;
  4581. const vec<tree, va_gc> *fn_args;
  4582. fn = OVL_CURRENT (fns);
  4583. if (check_converting && DECL_NONCONVERTING_P (fn))
  4584. continue;
  4585. if (check_list_ctor && !is_list_ctor (fn))
  4586. continue;
  4587. /* Figure out which set of arguments to use. */
  4588. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
  4589. {
  4590. /* If this function is a non-static member and we didn't get an
  4591. implicit object argument, move it out of args. */
  4592. if (first_arg == NULL_TREE)
  4593. {
  4594. unsigned int ix;
  4595. tree arg;
  4596. vec<tree, va_gc> *tempvec;
  4597. vec_alloc (tempvec, args->length () - 1);
  4598. for (ix = 1; args->iterate (ix, &arg); ++ix)
  4599. tempvec->quick_push (arg);
  4600. non_static_args = tempvec;
  4601. first_arg = (*args)[0];
  4602. }
  4603. fn_first_arg = first_arg;
  4604. fn_args = non_static_args;
  4605. }
  4606. else
  4607. {
  4608. /* Otherwise, just use the list of arguments provided. */
  4609. fn_first_arg = NULL_TREE;
  4610. fn_args = args;
  4611. }
  4612. if (TREE_CODE (fn) == TEMPLATE_DECL)
  4613. add_template_candidate (candidates,
  4614. fn,
  4615. ctype,
  4616. explicit_targs,
  4617. fn_first_arg,
  4618. fn_args,
  4619. return_type,
  4620. access_path,
  4621. conversion_path,
  4622. flags,
  4623. strict,
  4624. complain);
  4625. else if (!template_only)
  4626. add_function_candidate (candidates,
  4627. fn,
  4628. ctype,
  4629. fn_first_arg,
  4630. fn_args,
  4631. access_path,
  4632. conversion_path,
  4633. flags,
  4634. complain);
  4635. }
  4636. }
  4637. static tree
  4638. build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
  4639. tree arg2, tree arg3, tree *overload, tsubst_flags_t complain)
  4640. {
  4641. struct z_candidate *candidates = 0, *cand;
  4642. vec<tree, va_gc> *arglist;
  4643. tree fnname;
  4644. tree args[3];
  4645. tree result = NULL_TREE;
  4646. bool result_valid_p = false;
  4647. enum tree_code code2 = NOP_EXPR;
  4648. enum tree_code code_orig_arg1 = ERROR_MARK;
  4649. enum tree_code code_orig_arg2 = ERROR_MARK;
  4650. conversion *conv;
  4651. void *p;
  4652. bool strict_p;
  4653. bool any_viable_p;
  4654. if (error_operand_p (arg1)
  4655. || error_operand_p (arg2)
  4656. || error_operand_p (arg3))
  4657. return error_mark_node;
  4658. if (code == MODIFY_EXPR)
  4659. {
  4660. code2 = TREE_CODE (arg3);
  4661. arg3 = NULL_TREE;
  4662. fnname = ansi_assopname (code2);
  4663. }
  4664. else
  4665. fnname = ansi_opname (code);
  4666. arg1 = prep_operand (arg1);
  4667. bool memonly = false;
  4668. switch (code)
  4669. {
  4670. case NEW_EXPR:
  4671. case VEC_NEW_EXPR:
  4672. case VEC_DELETE_EXPR:
  4673. case DELETE_EXPR:
  4674. /* Use build_op_new_call and build_op_delete_call instead. */
  4675. gcc_unreachable ();
  4676. case CALL_EXPR:
  4677. /* Use build_op_call instead. */
  4678. gcc_unreachable ();
  4679. case TRUTH_ORIF_EXPR:
  4680. case TRUTH_ANDIF_EXPR:
  4681. case TRUTH_AND_EXPR:
  4682. case TRUTH_OR_EXPR:
  4683. /* These are saved for the sake of warn_logical_operator. */
  4684. code_orig_arg1 = TREE_CODE (arg1);
  4685. code_orig_arg2 = TREE_CODE (arg2);
  4686. break;
  4687. case GT_EXPR:
  4688. case LT_EXPR:
  4689. case GE_EXPR:
  4690. case LE_EXPR:
  4691. case EQ_EXPR:
  4692. case NE_EXPR:
  4693. /* These are saved for the sake of maybe_warn_bool_compare. */
  4694. code_orig_arg1 = TREE_CODE (TREE_TYPE (arg1));
  4695. code_orig_arg2 = TREE_CODE (TREE_TYPE (arg2));
  4696. break;
  4697. /* =, ->, [], () must be non-static member functions. */
  4698. case MODIFY_EXPR:
  4699. if (code2 != NOP_EXPR)
  4700. break;
  4701. case COMPONENT_REF:
  4702. case ARRAY_REF:
  4703. memonly = true;
  4704. break;
  4705. default:
  4706. break;
  4707. }
  4708. arg2 = prep_operand (arg2);
  4709. arg3 = prep_operand (arg3);
  4710. if (code == COND_EXPR)
  4711. /* Use build_conditional_expr instead. */
  4712. gcc_unreachable ();
  4713. else if (! OVERLOAD_TYPE_P (TREE_TYPE (arg1))
  4714. && (! arg2 || ! OVERLOAD_TYPE_P (TREE_TYPE (arg2))))
  4715. goto builtin;
  4716. if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
  4717. arg2 = integer_zero_node;
  4718. vec_alloc (arglist, 3);
  4719. arglist->quick_push (arg1);
  4720. if (arg2 != NULL_TREE)
  4721. arglist->quick_push (arg2);
  4722. if (arg3 != NULL_TREE)
  4723. arglist->quick_push (arg3);
  4724. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  4725. p = conversion_obstack_alloc (0);
  4726. /* Add namespace-scope operators to the list of functions to
  4727. consider. */
  4728. if (!memonly)
  4729. add_candidates (lookup_function_nonclass (fnname, arglist,
  4730. /*block_p=*/true),
  4731. NULL_TREE, arglist, NULL_TREE,
  4732. NULL_TREE, false, NULL_TREE, NULL_TREE,
  4733. flags, &candidates, complain);
  4734. args[0] = arg1;
  4735. args[1] = arg2;
  4736. args[2] = NULL_TREE;
  4737. /* Add class-member operators to the candidate set. */
  4738. if (CLASS_TYPE_P (TREE_TYPE (arg1)))
  4739. {
  4740. tree fns;
  4741. fns = lookup_fnfields (TREE_TYPE (arg1), fnname, 1);
  4742. if (fns == error_mark_node)
  4743. {
  4744. result = error_mark_node;
  4745. goto user_defined_result_ready;
  4746. }
  4747. if (fns)
  4748. add_candidates (BASELINK_FUNCTIONS (fns),
  4749. NULL_TREE, arglist, NULL_TREE,
  4750. NULL_TREE, false,
  4751. BASELINK_BINFO (fns),
  4752. BASELINK_ACCESS_BINFO (fns),
  4753. flags, &candidates, complain);
  4754. }
  4755. /* Per 13.3.1.2/3, 2nd bullet, if no operand has a class type, then
  4756. only non-member functions that have type T1 or reference to
  4757. cv-qualified-opt T1 for the first argument, if the first argument
  4758. has an enumeration type, or T2 or reference to cv-qualified-opt
  4759. T2 for the second argument, if the the second argument has an
  4760. enumeration type. Filter out those that don't match. */
  4761. else if (! arg2 || ! CLASS_TYPE_P (TREE_TYPE (arg2)))
  4762. {
  4763. struct z_candidate **candp, **next;
  4764. for (candp = &candidates; *candp; candp = next)
  4765. {
  4766. tree parmlist, parmtype;
  4767. int i, nargs = (arg2 ? 2 : 1);
  4768. cand = *candp;
  4769. next = &cand->next;
  4770. parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
  4771. for (i = 0; i < nargs; ++i)
  4772. {
  4773. parmtype = TREE_VALUE (parmlist);
  4774. if (TREE_CODE (parmtype) == REFERENCE_TYPE)
  4775. parmtype = TREE_TYPE (parmtype);
  4776. if (TREE_CODE (TREE_TYPE (args[i])) == ENUMERAL_TYPE
  4777. && (same_type_ignoring_top_level_qualifiers_p
  4778. (TREE_TYPE (args[i]), parmtype)))
  4779. break;
  4780. parmlist = TREE_CHAIN (parmlist);
  4781. }
  4782. /* No argument has an appropriate type, so remove this
  4783. candidate function from the list. */
  4784. if (i == nargs)
  4785. {
  4786. *candp = cand->next;
  4787. next = candp;
  4788. }
  4789. }
  4790. }
  4791. add_builtin_candidates (&candidates, code, code2, fnname, args,
  4792. flags, complain);
  4793. switch (code)
  4794. {
  4795. case COMPOUND_EXPR:
  4796. case ADDR_EXPR:
  4797. /* For these, the built-in candidates set is empty
  4798. [over.match.oper]/3. We don't want non-strict matches
  4799. because exact matches are always possible with built-in
  4800. operators. The built-in candidate set for COMPONENT_REF
  4801. would be empty too, but since there are no such built-in
  4802. operators, we accept non-strict matches for them. */
  4803. strict_p = true;
  4804. break;
  4805. default:
  4806. strict_p = false;
  4807. break;
  4808. }
  4809. candidates = splice_viable (candidates, strict_p, &any_viable_p);
  4810. if (!any_viable_p)
  4811. {
  4812. switch (code)
  4813. {
  4814. case POSTINCREMENT_EXPR:
  4815. case POSTDECREMENT_EXPR:
  4816. /* Don't try anything fancy if we're not allowed to produce
  4817. errors. */
  4818. if (!(complain & tf_error))
  4819. return error_mark_node;
  4820. /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
  4821. distinguish between prefix and postfix ++ and
  4822. operator++() was used for both, so we allow this with
  4823. -fpermissive. */
  4824. else
  4825. {
  4826. const char *msg = (flag_permissive)
  4827. ? G_("no %<%D(int)%> declared for postfix %qs,"
  4828. " trying prefix operator instead")
  4829. : G_("no %<%D(int)%> declared for postfix %qs");
  4830. permerror (loc, msg, fnname, operator_name_info[code].name);
  4831. }
  4832. if (!flag_permissive)
  4833. return error_mark_node;
  4834. if (code == POSTINCREMENT_EXPR)
  4835. code = PREINCREMENT_EXPR;
  4836. else
  4837. code = PREDECREMENT_EXPR;
  4838. result = build_new_op_1 (loc, code, flags, arg1, NULL_TREE,
  4839. NULL_TREE, overload, complain);
  4840. break;
  4841. /* The caller will deal with these. */
  4842. case ADDR_EXPR:
  4843. case COMPOUND_EXPR:
  4844. case COMPONENT_REF:
  4845. result = NULL_TREE;
  4846. result_valid_p = true;
  4847. break;
  4848. default:
  4849. if (complain & tf_error)
  4850. {
  4851. /* If one of the arguments of the operator represents
  4852. an invalid use of member function pointer, try to report
  4853. a meaningful error ... */
  4854. if (invalid_nonstatic_memfn_p (arg1, tf_error)
  4855. || invalid_nonstatic_memfn_p (arg2, tf_error)
  4856. || invalid_nonstatic_memfn_p (arg3, tf_error))
  4857. /* We displayed the error message. */;
  4858. else
  4859. {
  4860. /* ... Otherwise, report the more generic
  4861. "no matching operator found" error */
  4862. op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
  4863. print_z_candidates (loc, candidates);
  4864. }
  4865. }
  4866. result = error_mark_node;
  4867. break;
  4868. }
  4869. }
  4870. else
  4871. {
  4872. cand = tourney (candidates, complain);
  4873. if (cand == 0)
  4874. {
  4875. if (complain & tf_error)
  4876. {
  4877. op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
  4878. print_z_candidates (loc, candidates);
  4879. }
  4880. result = error_mark_node;
  4881. }
  4882. else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
  4883. {
  4884. if (overload)
  4885. *overload = cand->fn;
  4886. if (resolve_args (arglist, complain) == NULL)
  4887. result = error_mark_node;
  4888. else
  4889. result = build_over_call (cand, LOOKUP_NORMAL, complain);
  4890. }
  4891. else
  4892. {
  4893. /* Give any warnings we noticed during overload resolution. */
  4894. if (cand->warnings && (complain & tf_warning))
  4895. {
  4896. struct candidate_warning *w;
  4897. for (w = cand->warnings; w; w = w->next)
  4898. joust (cand, w->loser, 1, complain);
  4899. }
  4900. /* Check for comparison of different enum types. */
  4901. switch (code)
  4902. {
  4903. case GT_EXPR:
  4904. case LT_EXPR:
  4905. case GE_EXPR:
  4906. case LE_EXPR:
  4907. case EQ_EXPR:
  4908. case NE_EXPR:
  4909. if (TREE_CODE (TREE_TYPE (arg1)) == ENUMERAL_TYPE
  4910. && TREE_CODE (TREE_TYPE (arg2)) == ENUMERAL_TYPE
  4911. && (TYPE_MAIN_VARIANT (TREE_TYPE (arg1))
  4912. != TYPE_MAIN_VARIANT (TREE_TYPE (arg2)))
  4913. && (complain & tf_warning))
  4914. {
  4915. warning (OPT_Wenum_compare,
  4916. "comparison between %q#T and %q#T",
  4917. TREE_TYPE (arg1), TREE_TYPE (arg2));
  4918. }
  4919. break;
  4920. default:
  4921. break;
  4922. }
  4923. /* We need to strip any leading REF_BIND so that bitfields
  4924. don't cause errors. This should not remove any important
  4925. conversions, because builtins don't apply to class
  4926. objects directly. */
  4927. conv = cand->convs[0];
  4928. if (conv->kind == ck_ref_bind)
  4929. conv = next_conversion (conv);
  4930. arg1 = convert_like (conv, arg1, complain);
  4931. if (arg2)
  4932. {
  4933. conv = cand->convs[1];
  4934. if (conv->kind == ck_ref_bind)
  4935. conv = next_conversion (conv);
  4936. else
  4937. arg2 = decay_conversion (arg2, complain);
  4938. /* We need to call warn_logical_operator before
  4939. converting arg2 to a boolean_type, but after
  4940. decaying an enumerator to its value. */
  4941. if (complain & tf_warning)
  4942. warn_logical_operator (loc, code, boolean_type_node,
  4943. code_orig_arg1, arg1,
  4944. code_orig_arg2, arg2);
  4945. arg2 = convert_like (conv, arg2, complain);
  4946. }
  4947. if (arg3)
  4948. {
  4949. conv = cand->convs[2];
  4950. if (conv->kind == ck_ref_bind)
  4951. conv = next_conversion (conv);
  4952. arg3 = convert_like (conv, arg3, complain);
  4953. }
  4954. }
  4955. }
  4956. user_defined_result_ready:
  4957. /* Free all the conversions we allocated. */
  4958. obstack_free (&conversion_obstack, p);
  4959. if (result || result_valid_p)
  4960. return result;
  4961. builtin:
  4962. switch (code)
  4963. {
  4964. case MODIFY_EXPR:
  4965. return cp_build_modify_expr (arg1, code2, arg2, complain);
  4966. case INDIRECT_REF:
  4967. return cp_build_indirect_ref (arg1, RO_UNARY_STAR, complain);
  4968. case TRUTH_ANDIF_EXPR:
  4969. case TRUTH_ORIF_EXPR:
  4970. case TRUTH_AND_EXPR:
  4971. case TRUTH_OR_EXPR:
  4972. if (complain & tf_warning)
  4973. warn_logical_operator (loc, code, boolean_type_node,
  4974. code_orig_arg1, arg1, code_orig_arg2, arg2);
  4975. /* Fall through. */
  4976. case GT_EXPR:
  4977. case LT_EXPR:
  4978. case GE_EXPR:
  4979. case LE_EXPR:
  4980. case EQ_EXPR:
  4981. case NE_EXPR:
  4982. if ((complain & tf_warning)
  4983. && ((code_orig_arg1 == BOOLEAN_TYPE)
  4984. ^ (code_orig_arg2 == BOOLEAN_TYPE)))
  4985. maybe_warn_bool_compare (loc, code, arg1, arg2);
  4986. /* Fall through. */
  4987. case PLUS_EXPR:
  4988. case MINUS_EXPR:
  4989. case MULT_EXPR:
  4990. case TRUNC_DIV_EXPR:
  4991. case MAX_EXPR:
  4992. case MIN_EXPR:
  4993. case LSHIFT_EXPR:
  4994. case RSHIFT_EXPR:
  4995. case TRUNC_MOD_EXPR:
  4996. case BIT_AND_EXPR:
  4997. case BIT_IOR_EXPR:
  4998. case BIT_XOR_EXPR:
  4999. return cp_build_binary_op (loc, code, arg1, arg2, complain);
  5000. case UNARY_PLUS_EXPR:
  5001. case NEGATE_EXPR:
  5002. case BIT_NOT_EXPR:
  5003. case TRUTH_NOT_EXPR:
  5004. case PREINCREMENT_EXPR:
  5005. case POSTINCREMENT_EXPR:
  5006. case PREDECREMENT_EXPR:
  5007. case POSTDECREMENT_EXPR:
  5008. case REALPART_EXPR:
  5009. case IMAGPART_EXPR:
  5010. case ABS_EXPR:
  5011. return cp_build_unary_op (code, arg1, candidates != 0, complain);
  5012. case ARRAY_REF:
  5013. return cp_build_array_ref (input_location, arg1, arg2, complain);
  5014. case MEMBER_REF:
  5015. return build_m_component_ref (cp_build_indirect_ref (arg1, RO_ARROW_STAR,
  5016. complain),
  5017. arg2, complain);
  5018. /* The caller will deal with these. */
  5019. case ADDR_EXPR:
  5020. case COMPONENT_REF:
  5021. case COMPOUND_EXPR:
  5022. return NULL_TREE;
  5023. default:
  5024. gcc_unreachable ();
  5025. }
  5026. return NULL_TREE;
  5027. }
  5028. /* Wrapper for above. */
  5029. tree
  5030. build_new_op (location_t loc, enum tree_code code, int flags,
  5031. tree arg1, tree arg2, tree arg3,
  5032. tree *overload, tsubst_flags_t complain)
  5033. {
  5034. tree ret;
  5035. bool subtime = timevar_cond_start (TV_OVERLOAD);
  5036. ret = build_new_op_1 (loc, code, flags, arg1, arg2, arg3,
  5037. overload, complain);
  5038. timevar_cond_stop (TV_OVERLOAD, subtime);
  5039. return ret;
  5040. }
  5041. /* Returns true iff T, an element of an OVERLOAD chain, is a usual
  5042. deallocation function (3.7.4.2 [basic.stc.dynamic.deallocation]). */
  5043. bool
  5044. non_placement_deallocation_fn_p (tree t)
  5045. {
  5046. /* A template instance is never a usual deallocation function,
  5047. regardless of its signature. */
  5048. if (TREE_CODE (t) == TEMPLATE_DECL
  5049. || primary_template_instantiation_p (t))
  5050. return false;
  5051. /* If a class T has a member deallocation function named operator delete
  5052. with exactly one parameter, then that function is a usual
  5053. (non-placement) deallocation function. If class T does not declare
  5054. such an operator delete but does declare a member deallocation
  5055. function named operator delete with exactly two parameters, the second
  5056. of which has type std::size_t (18.2), then this function is a usual
  5057. deallocation function. */
  5058. bool global = DECL_NAMESPACE_SCOPE_P (t);
  5059. t = FUNCTION_ARG_CHAIN (t);
  5060. if (t == void_list_node
  5061. || (t && same_type_p (TREE_VALUE (t), size_type_node)
  5062. && (!global || flag_sized_deallocation)
  5063. && TREE_CHAIN (t) == void_list_node))
  5064. return true;
  5065. return false;
  5066. }
  5067. /* Build a call to operator delete. This has to be handled very specially,
  5068. because the restrictions on what signatures match are different from all
  5069. other call instances. For a normal delete, only a delete taking (void *)
  5070. or (void *, size_t) is accepted. For a placement delete, only an exact
  5071. match with the placement new is accepted.
  5072. CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
  5073. ADDR is the pointer to be deleted.
  5074. SIZE is the size of the memory block to be deleted.
  5075. GLOBAL_P is true if the delete-expression should not consider
  5076. class-specific delete operators.
  5077. PLACEMENT is the corresponding placement new call, or NULL_TREE.
  5078. If this call to "operator delete" is being generated as part to
  5079. deallocate memory allocated via a new-expression (as per [expr.new]
  5080. which requires that if the initialization throws an exception then
  5081. we call a deallocation function), then ALLOC_FN is the allocation
  5082. function. */
  5083. tree
  5084. build_op_delete_call (enum tree_code code, tree addr, tree size,
  5085. bool global_p, tree placement,
  5086. tree alloc_fn, tsubst_flags_t complain)
  5087. {
  5088. tree fn = NULL_TREE;
  5089. tree fns, fnname, type, t;
  5090. if (addr == error_mark_node)
  5091. return error_mark_node;
  5092. type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
  5093. fnname = ansi_opname (code);
  5094. if (CLASS_TYPE_P (type)
  5095. && COMPLETE_TYPE_P (complete_type (type))
  5096. && !global_p)
  5097. /* In [class.free]
  5098. If the result of the lookup is ambiguous or inaccessible, or if
  5099. the lookup selects a placement deallocation function, the
  5100. program is ill-formed.
  5101. Therefore, we ask lookup_fnfields to complain about ambiguity. */
  5102. {
  5103. fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1);
  5104. if (fns == error_mark_node)
  5105. return error_mark_node;
  5106. }
  5107. else
  5108. fns = NULL_TREE;
  5109. if (fns == NULL_TREE)
  5110. fns = lookup_name_nonclass (fnname);
  5111. /* Strip const and volatile from addr. */
  5112. addr = cp_convert (ptr_type_node, addr, complain);
  5113. if (placement)
  5114. {
  5115. /* "A declaration of a placement deallocation function matches the
  5116. declaration of a placement allocation function if it has the same
  5117. number of parameters and, after parameter transformations (8.3.5),
  5118. all parameter types except the first are identical."
  5119. So we build up the function type we want and ask instantiate_type
  5120. to get it for us. */
  5121. t = FUNCTION_ARG_CHAIN (alloc_fn);
  5122. t = tree_cons (NULL_TREE, ptr_type_node, t);
  5123. t = build_function_type (void_type_node, t);
  5124. fn = instantiate_type (t, fns, tf_none);
  5125. if (fn == error_mark_node)
  5126. return NULL_TREE;
  5127. if (BASELINK_P (fn))
  5128. fn = BASELINK_FUNCTIONS (fn);
  5129. /* "If the lookup finds the two-parameter form of a usual deallocation
  5130. function (3.7.4.2) and that function, considered as a placement
  5131. deallocation function, would have been selected as a match for the
  5132. allocation function, the program is ill-formed." */
  5133. if (non_placement_deallocation_fn_p (fn))
  5134. {
  5135. /* But if the class has an operator delete (void *), then that is
  5136. the usual deallocation function, so we shouldn't complain
  5137. about using the operator delete (void *, size_t). */
  5138. for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
  5139. t; t = OVL_NEXT (t))
  5140. {
  5141. tree elt = OVL_CURRENT (t);
  5142. if (non_placement_deallocation_fn_p (elt)
  5143. && FUNCTION_ARG_CHAIN (elt) == void_list_node)
  5144. goto ok;
  5145. }
  5146. if (complain & tf_error)
  5147. {
  5148. permerror (0, "non-placement deallocation function %q+D", fn);
  5149. permerror (input_location, "selected for placement delete");
  5150. }
  5151. else
  5152. return error_mark_node;
  5153. ok:;
  5154. }
  5155. }
  5156. else
  5157. /* "Any non-placement deallocation function matches a non-placement
  5158. allocation function. If the lookup finds a single matching
  5159. deallocation function, that function will be called; otherwise, no
  5160. deallocation function will be called." */
  5161. for (t = BASELINK_P (fns) ? BASELINK_FUNCTIONS (fns) : fns;
  5162. t; t = OVL_NEXT (t))
  5163. {
  5164. tree elt = OVL_CURRENT (t);
  5165. if (non_placement_deallocation_fn_p (elt))
  5166. {
  5167. fn = elt;
  5168. /* "If a class T has a member deallocation function named
  5169. operator delete with exactly one parameter, then that
  5170. function is a usual (non-placement) deallocation
  5171. function. If class T does not declare such an operator
  5172. delete but does declare a member deallocation function named
  5173. operator delete with exactly two parameters, the second of
  5174. which has type std::size_t (18.2), then this function is a
  5175. usual deallocation function."
  5176. So in a class (void*) beats (void*, size_t). */
  5177. if (DECL_CLASS_SCOPE_P (fn))
  5178. {
  5179. if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
  5180. break;
  5181. }
  5182. /* At global scope (in C++14 and above) the rules are different:
  5183. If deallocation function lookup finds both a usual
  5184. deallocation function with only a pointer parameter and a
  5185. usual deallocation function with both a pointer parameter
  5186. and a size parameter, the function to be called is selected
  5187. as follows:
  5188. * If the type is complete and if, for the second alternative
  5189. (delete array) only, the operand is a pointer to a class
  5190. type with a non-trivial destructor or a (possibly
  5191. multi-dimensional) array thereof, the function with two
  5192. parameters is selected.
  5193. * Otherwise, it is unspecified which of the two deallocation
  5194. functions is selected. */
  5195. else
  5196. {
  5197. bool want_size = COMPLETE_TYPE_P (type);
  5198. if (code == VEC_DELETE_EXPR
  5199. && !TYPE_VEC_NEW_USES_COOKIE (type))
  5200. /* We need a cookie to determine the array size. */
  5201. want_size = false;
  5202. bool have_size = (FUNCTION_ARG_CHAIN (fn) != void_list_node);
  5203. if (want_size == have_size)
  5204. break;
  5205. }
  5206. }
  5207. }
  5208. /* If we have a matching function, call it. */
  5209. if (fn)
  5210. {
  5211. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  5212. /* If the FN is a member function, make sure that it is
  5213. accessible. */
  5214. if (BASELINK_P (fns))
  5215. perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
  5216. complain);
  5217. /* Core issue 901: It's ok to new a type with deleted delete. */
  5218. if (DECL_DELETED_FN (fn) && alloc_fn)
  5219. return NULL_TREE;
  5220. if (placement)
  5221. {
  5222. /* The placement args might not be suitable for overload
  5223. resolution at this point, so build the call directly. */
  5224. int nargs = call_expr_nargs (placement);
  5225. tree *argarray = XALLOCAVEC (tree, nargs);
  5226. int i;
  5227. argarray[0] = addr;
  5228. for (i = 1; i < nargs; i++)
  5229. argarray[i] = CALL_EXPR_ARG (placement, i);
  5230. mark_used (fn);
  5231. return build_cxx_call (fn, nargs, argarray, complain);
  5232. }
  5233. else
  5234. {
  5235. tree ret;
  5236. vec<tree, va_gc> *args = make_tree_vector ();
  5237. args->quick_push (addr);
  5238. if (FUNCTION_ARG_CHAIN (fn) != void_list_node)
  5239. args->quick_push (size);
  5240. ret = cp_build_function_call_vec (fn, &args, complain);
  5241. release_tree_vector (args);
  5242. return ret;
  5243. }
  5244. }
  5245. /* [expr.new]
  5246. If no unambiguous matching deallocation function can be found,
  5247. propagating the exception does not cause the object's memory to
  5248. be freed. */
  5249. if (alloc_fn)
  5250. {
  5251. if ((complain & tf_warning)
  5252. && !placement)
  5253. warning (0, "no corresponding deallocation function for %qD",
  5254. alloc_fn);
  5255. return NULL_TREE;
  5256. }
  5257. if (complain & tf_error)
  5258. error ("no suitable %<operator %s%> for %qT",
  5259. operator_name_info[(int)code].name, type);
  5260. return error_mark_node;
  5261. }
  5262. /* If the current scope isn't allowed to access DECL along
  5263. BASETYPE_PATH, give an error. The most derived class in
  5264. BASETYPE_PATH is the one used to qualify DECL. DIAG_DECL is
  5265. the declaration to use in the error diagnostic. */
  5266. bool
  5267. enforce_access (tree basetype_path, tree decl, tree diag_decl,
  5268. tsubst_flags_t complain)
  5269. {
  5270. gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
  5271. if (!accessible_p (basetype_path, decl, true))
  5272. {
  5273. if (complain & tf_error)
  5274. {
  5275. if (TREE_PRIVATE (decl))
  5276. error ("%q+#D is private", diag_decl);
  5277. else if (TREE_PROTECTED (decl))
  5278. error ("%q+#D is protected", diag_decl);
  5279. else
  5280. error ("%q+#D is inaccessible", diag_decl);
  5281. error ("within this context");
  5282. }
  5283. return false;
  5284. }
  5285. return true;
  5286. }
  5287. /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
  5288. bitwise or of LOOKUP_* values. If any errors are warnings are
  5289. generated, set *DIAGNOSTIC_FN to "error" or "warning",
  5290. respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
  5291. to NULL. */
  5292. static tree
  5293. build_temp (tree expr, tree type, int flags,
  5294. diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
  5295. {
  5296. int savew, savee;
  5297. vec<tree, va_gc> *args;
  5298. savew = warningcount + werrorcount, savee = errorcount;
  5299. args = make_tree_vector_single (expr);
  5300. expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
  5301. &args, type, flags, complain);
  5302. release_tree_vector (args);
  5303. if (warningcount + werrorcount > savew)
  5304. *diagnostic_kind = DK_WARNING;
  5305. else if (errorcount > savee)
  5306. *diagnostic_kind = DK_ERROR;
  5307. else
  5308. *diagnostic_kind = DK_UNSPECIFIED;
  5309. return expr;
  5310. }
  5311. /* Perform warnings about peculiar, but valid, conversions from/to NULL.
  5312. EXPR is implicitly converted to type TOTYPE.
  5313. FN and ARGNUM are used for diagnostics. */
  5314. static void
  5315. conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
  5316. {
  5317. /* Issue warnings about peculiar, but valid, uses of NULL. */
  5318. if (expr == null_node && TREE_CODE (totype) != BOOLEAN_TYPE
  5319. && ARITHMETIC_TYPE_P (totype))
  5320. {
  5321. source_location loc =
  5322. expansion_point_location_if_in_system_header (input_location);
  5323. if (fn)
  5324. warning_at (loc, OPT_Wconversion_null,
  5325. "passing NULL to non-pointer argument %P of %qD",
  5326. argnum, fn);
  5327. else
  5328. warning_at (loc, OPT_Wconversion_null,
  5329. "converting to non-pointer type %qT from NULL", totype);
  5330. }
  5331. /* Issue warnings if "false" is converted to a NULL pointer */
  5332. else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
  5333. && TYPE_PTR_P (totype))
  5334. {
  5335. if (fn)
  5336. warning_at (input_location, OPT_Wconversion_null,
  5337. "converting %<false%> to pointer type for argument %P "
  5338. "of %qD", argnum, fn);
  5339. else
  5340. warning_at (input_location, OPT_Wconversion_null,
  5341. "converting %<false%> to pointer type %qT", totype);
  5342. }
  5343. }
  5344. /* We gave a diagnostic during a conversion. If this was in the second
  5345. standard conversion sequence of a user-defined conversion sequence, say
  5346. which user-defined conversion. */
  5347. static void
  5348. maybe_print_user_conv_context (conversion *convs)
  5349. {
  5350. if (convs->user_conv_p)
  5351. for (conversion *t = convs; t; t = next_conversion (t))
  5352. if (t->kind == ck_user)
  5353. {
  5354. print_z_candidate (0, " after user-defined conversion:",
  5355. t->cand);
  5356. break;
  5357. }
  5358. }
  5359. /* Perform the conversions in CONVS on the expression EXPR. FN and
  5360. ARGNUM are used for diagnostics. ARGNUM is zero based, -1
  5361. indicates the `this' argument of a method. INNER is nonzero when
  5362. being called to continue a conversion chain. It is negative when a
  5363. reference binding will be applied, positive otherwise. If
  5364. ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
  5365. conversions will be emitted if appropriate. If C_CAST_P is true,
  5366. this conversion is coming from a C-style cast; in that case,
  5367. conversions to inaccessible bases are permitted. */
  5368. static tree
  5369. convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
  5370. int inner, bool issue_conversion_warnings,
  5371. bool c_cast_p, tsubst_flags_t complain)
  5372. {
  5373. tree totype = convs->type;
  5374. diagnostic_t diag_kind;
  5375. int flags;
  5376. location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
  5377. if (convs->bad_p && !(complain & tf_error))
  5378. return error_mark_node;
  5379. if (convs->bad_p
  5380. && convs->kind != ck_user
  5381. && convs->kind != ck_list
  5382. && convs->kind != ck_ambig
  5383. && (convs->kind != ck_ref_bind
  5384. || (convs->user_conv_p && next_conversion (convs)->bad_p))
  5385. && (convs->kind != ck_rvalue
  5386. || SCALAR_TYPE_P (totype))
  5387. && convs->kind != ck_base)
  5388. {
  5389. bool complained = false;
  5390. conversion *t = convs;
  5391. /* Give a helpful error if this is bad because of excess braces. */
  5392. if (BRACE_ENCLOSED_INITIALIZER_P (expr)
  5393. && SCALAR_TYPE_P (totype)
  5394. && CONSTRUCTOR_NELTS (expr) > 0
  5395. && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
  5396. {
  5397. complained = permerror (loc, "too many braces around initializer "
  5398. "for %qT", totype);
  5399. while (BRACE_ENCLOSED_INITIALIZER_P (expr)
  5400. && CONSTRUCTOR_NELTS (expr) == 1)
  5401. expr = CONSTRUCTOR_ELT (expr, 0)->value;
  5402. }
  5403. /* Give a helpful error if this is bad because a conversion to bool
  5404. from std::nullptr_t requires direct-initialization. */
  5405. if (NULLPTR_TYPE_P (TREE_TYPE (expr))
  5406. && TREE_CODE (totype) == BOOLEAN_TYPE)
  5407. complained = permerror (loc, "converting to %qT from %qT requires "
  5408. "direct-initialization",
  5409. totype, TREE_TYPE (expr));
  5410. for (; t ; t = next_conversion (t))
  5411. {
  5412. if (t->kind == ck_user && t->cand->reason)
  5413. {
  5414. complained = permerror (loc, "invalid user-defined conversion "
  5415. "from %qT to %qT", TREE_TYPE (expr),
  5416. totype);
  5417. if (complained)
  5418. print_z_candidate (loc, "candidate is:", t->cand);
  5419. expr = convert_like_real (t, expr, fn, argnum, 1,
  5420. /*issue_conversion_warnings=*/false,
  5421. /*c_cast_p=*/false,
  5422. complain);
  5423. if (convs->kind == ck_ref_bind)
  5424. expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
  5425. LOOKUP_NORMAL, NULL_TREE,
  5426. complain);
  5427. else
  5428. expr = cp_convert (totype, expr, complain);
  5429. if (complained && fn)
  5430. inform (DECL_SOURCE_LOCATION (fn),
  5431. " initializing argument %P of %qD", argnum, fn);
  5432. return expr;
  5433. }
  5434. else if (t->kind == ck_user || !t->bad_p)
  5435. {
  5436. expr = convert_like_real (t, expr, fn, argnum, 1,
  5437. /*issue_conversion_warnings=*/false,
  5438. /*c_cast_p=*/false,
  5439. complain);
  5440. break;
  5441. }
  5442. else if (t->kind == ck_ambig)
  5443. return convert_like_real (t, expr, fn, argnum, 1,
  5444. /*issue_conversion_warnings=*/false,
  5445. /*c_cast_p=*/false,
  5446. complain);
  5447. else if (t->kind == ck_identity)
  5448. break;
  5449. }
  5450. if (!complained)
  5451. complained = permerror (loc, "invalid conversion from %qT to %qT",
  5452. TREE_TYPE (expr), totype);
  5453. if (complained && fn)
  5454. inform (DECL_SOURCE_LOCATION (fn),
  5455. " initializing argument %P of %qD", argnum, fn);
  5456. return cp_convert (totype, expr, complain);
  5457. }
  5458. if (issue_conversion_warnings && (complain & tf_warning))
  5459. conversion_null_warnings (totype, expr, fn, argnum);
  5460. switch (convs->kind)
  5461. {
  5462. case ck_user:
  5463. {
  5464. struct z_candidate *cand = convs->cand;
  5465. tree convfn = cand->fn;
  5466. unsigned i;
  5467. /* When converting from an init list we consider explicit
  5468. constructors, but actually trying to call one is an error. */
  5469. if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
  5470. /* Unless this is for direct-list-initialization. */
  5471. && !DIRECT_LIST_INIT_P (expr))
  5472. {
  5473. if (!(complain & tf_error))
  5474. return error_mark_node;
  5475. error ("converting to %qT from initializer list would use "
  5476. "explicit constructor %qD", totype, convfn);
  5477. }
  5478. /* If we're initializing from {}, it's value-initialization. */
  5479. if (BRACE_ENCLOSED_INITIALIZER_P (expr)
  5480. && CONSTRUCTOR_NELTS (expr) == 0
  5481. && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
  5482. {
  5483. bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
  5484. expr = build_value_init (totype, complain);
  5485. expr = get_target_expr_sfinae (expr, complain);
  5486. if (expr != error_mark_node)
  5487. {
  5488. TARGET_EXPR_LIST_INIT_P (expr) = true;
  5489. TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
  5490. }
  5491. return expr;
  5492. }
  5493. expr = mark_rvalue_use (expr);
  5494. /* Set user_conv_p on the argument conversions, so rvalue/base
  5495. handling knows not to allow any more UDCs. */
  5496. for (i = 0; i < cand->num_convs; ++i)
  5497. cand->convs[i]->user_conv_p = true;
  5498. expr = build_over_call (cand, LOOKUP_NORMAL, complain);
  5499. /* If this is a constructor or a function returning an aggr type,
  5500. we need to build up a TARGET_EXPR. */
  5501. if (DECL_CONSTRUCTOR_P (convfn))
  5502. {
  5503. expr = build_cplus_new (totype, expr, complain);
  5504. /* Remember that this was list-initialization. */
  5505. if (convs->check_narrowing && expr != error_mark_node)
  5506. TARGET_EXPR_LIST_INIT_P (expr) = true;
  5507. }
  5508. return expr;
  5509. }
  5510. case ck_identity:
  5511. if (BRACE_ENCLOSED_INITIALIZER_P (expr))
  5512. {
  5513. int nelts = CONSTRUCTOR_NELTS (expr);
  5514. if (nelts == 0)
  5515. expr = build_value_init (totype, complain);
  5516. else if (nelts == 1)
  5517. expr = CONSTRUCTOR_ELT (expr, 0)->value;
  5518. else
  5519. gcc_unreachable ();
  5520. }
  5521. expr = mark_rvalue_use (expr);
  5522. if (type_unknown_p (expr))
  5523. expr = instantiate_type (totype, expr, complain);
  5524. /* Convert a constant to its underlying value, unless we are
  5525. about to bind it to a reference, in which case we need to
  5526. leave it as an lvalue. */
  5527. if (inner >= 0)
  5528. {
  5529. expr = scalar_constant_value (expr);
  5530. if (expr == null_node && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
  5531. /* If __null has been converted to an integer type, we do not
  5532. want to warn about uses of EXPR as an integer, rather than
  5533. as a pointer. */
  5534. expr = build_int_cst (totype, 0);
  5535. }
  5536. return expr;
  5537. case ck_ambig:
  5538. /* We leave bad_p off ck_ambig because overload resolution considers
  5539. it valid, it just fails when we try to perform it. So we need to
  5540. check complain here, too. */
  5541. if (complain & tf_error)
  5542. {
  5543. /* Call build_user_type_conversion again for the error. */
  5544. build_user_type_conversion (totype, convs->u.expr, LOOKUP_NORMAL,
  5545. complain);
  5546. if (fn)
  5547. inform (input_location, " initializing argument %P of %q+D",
  5548. argnum, fn);
  5549. }
  5550. return error_mark_node;
  5551. case ck_list:
  5552. {
  5553. /* Conversion to std::initializer_list<T>. */
  5554. tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
  5555. tree new_ctor = build_constructor (init_list_type_node, NULL);
  5556. unsigned len = CONSTRUCTOR_NELTS (expr);
  5557. tree array, val, field;
  5558. vec<constructor_elt, va_gc> *vec = NULL;
  5559. unsigned ix;
  5560. /* Convert all the elements. */
  5561. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
  5562. {
  5563. tree sub = convert_like_real (convs->u.list[ix], val, fn, argnum,
  5564. 1, false, false, complain);
  5565. if (sub == error_mark_node)
  5566. return sub;
  5567. if (!BRACE_ENCLOSED_INITIALIZER_P (val)
  5568. && !check_narrowing (TREE_TYPE (sub), val, complain))
  5569. return error_mark_node;
  5570. CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), NULL_TREE, sub);
  5571. if (!TREE_CONSTANT (sub))
  5572. TREE_CONSTANT (new_ctor) = false;
  5573. }
  5574. /* Build up the array. */
  5575. elttype = cp_build_qualified_type
  5576. (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
  5577. array = build_array_of_n_type (elttype, len);
  5578. array = finish_compound_literal (array, new_ctor, complain);
  5579. /* Take the address explicitly rather than via decay_conversion
  5580. to avoid the error about taking the address of a temporary. */
  5581. array = cp_build_addr_expr (array, complain);
  5582. array = cp_convert (build_pointer_type (elttype), array, complain);
  5583. if (array == error_mark_node)
  5584. return error_mark_node;
  5585. /* Build up the initializer_list object. */
  5586. totype = complete_type (totype);
  5587. field = next_initializable_field (TYPE_FIELDS (totype));
  5588. CONSTRUCTOR_APPEND_ELT (vec, field, array);
  5589. field = next_initializable_field (DECL_CHAIN (field));
  5590. CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
  5591. new_ctor = build_constructor (totype, vec);
  5592. return get_target_expr_sfinae (new_ctor, complain);
  5593. }
  5594. case ck_aggr:
  5595. if (TREE_CODE (totype) == COMPLEX_TYPE)
  5596. {
  5597. tree real = CONSTRUCTOR_ELT (expr, 0)->value;
  5598. tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
  5599. real = perform_implicit_conversion (TREE_TYPE (totype),
  5600. real, complain);
  5601. imag = perform_implicit_conversion (TREE_TYPE (totype),
  5602. imag, complain);
  5603. expr = build2 (COMPLEX_EXPR, totype, real, imag);
  5604. return fold_if_not_in_template (expr);
  5605. }
  5606. expr = reshape_init (totype, expr, complain);
  5607. expr = get_target_expr_sfinae (digest_init (totype, expr, complain),
  5608. complain);
  5609. if (expr != error_mark_node)
  5610. TARGET_EXPR_LIST_INIT_P (expr) = true;
  5611. return expr;
  5612. default:
  5613. break;
  5614. };
  5615. expr = convert_like_real (next_conversion (convs), expr, fn, argnum,
  5616. convs->kind == ck_ref_bind ? -1 : 1,
  5617. convs->kind == ck_ref_bind ? issue_conversion_warnings : false,
  5618. c_cast_p,
  5619. complain);
  5620. if (expr == error_mark_node)
  5621. return error_mark_node;
  5622. switch (convs->kind)
  5623. {
  5624. case ck_rvalue:
  5625. expr = decay_conversion (expr, complain);
  5626. if (expr == error_mark_node)
  5627. return error_mark_node;
  5628. if (! MAYBE_CLASS_TYPE_P (totype))
  5629. return expr;
  5630. /* Else fall through. */
  5631. case ck_base:
  5632. if (convs->kind == ck_base && !convs->need_temporary_p)
  5633. {
  5634. /* We are going to bind a reference directly to a base-class
  5635. subobject of EXPR. */
  5636. /* Build an expression for `*((base*) &expr)'. */
  5637. expr = convert_to_base (expr, totype,
  5638. !c_cast_p, /*nonnull=*/true, complain);
  5639. return expr;
  5640. }
  5641. /* Copy-initialization where the cv-unqualified version of the source
  5642. type is the same class as, or a derived class of, the class of the
  5643. destination [is treated as direct-initialization]. [dcl.init] */
  5644. flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
  5645. if (convs->user_conv_p)
  5646. /* This conversion is being done in the context of a user-defined
  5647. conversion (i.e. the second step of copy-initialization), so
  5648. don't allow any more. */
  5649. flags |= LOOKUP_NO_CONVERSION;
  5650. if (convs->rvaluedness_matches_p)
  5651. flags |= LOOKUP_PREFER_RVALUE;
  5652. if (TREE_CODE (expr) == TARGET_EXPR
  5653. && TARGET_EXPR_LIST_INIT_P (expr))
  5654. /* Copy-list-initialization doesn't actually involve a copy. */
  5655. return expr;
  5656. expr = build_temp (expr, totype, flags, &diag_kind, complain);
  5657. if (diag_kind && complain)
  5658. {
  5659. maybe_print_user_conv_context (convs);
  5660. if (fn)
  5661. inform (DECL_SOURCE_LOCATION (fn),
  5662. " initializing argument %P of %qD", argnum, fn);
  5663. }
  5664. return build_cplus_new (totype, expr, complain);
  5665. case ck_ref_bind:
  5666. {
  5667. tree ref_type = totype;
  5668. if (convs->bad_p && !next_conversion (convs)->bad_p)
  5669. {
  5670. tree extype = TREE_TYPE (expr);
  5671. if (TYPE_REF_IS_RVALUE (ref_type)
  5672. && real_lvalue_p (expr))
  5673. error_at (loc, "cannot bind %qT lvalue to %qT",
  5674. extype, totype);
  5675. else if (!TYPE_REF_IS_RVALUE (ref_type) && !real_lvalue_p (expr)
  5676. && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
  5677. error_at (loc, "invalid initialization of non-const reference of "
  5678. "type %qT from an rvalue of type %qT", totype, extype);
  5679. else if (!reference_compatible_p (TREE_TYPE (totype), extype))
  5680. error_at (loc, "binding %qT to reference of type %qT "
  5681. "discards qualifiers", extype, totype);
  5682. else
  5683. gcc_unreachable ();
  5684. maybe_print_user_conv_context (convs);
  5685. if (fn)
  5686. inform (input_location,
  5687. " initializing argument %P of %q+D", argnum, fn);
  5688. return error_mark_node;
  5689. }
  5690. /* If necessary, create a temporary.
  5691. VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
  5692. that need temporaries, even when their types are reference
  5693. compatible with the type of reference being bound, so the
  5694. upcoming call to cp_build_addr_expr doesn't fail. */
  5695. if (convs->need_temporary_p
  5696. || TREE_CODE (expr) == CONSTRUCTOR
  5697. || TREE_CODE (expr) == VA_ARG_EXPR)
  5698. {
  5699. /* Otherwise, a temporary of type "cv1 T1" is created and
  5700. initialized from the initializer expression using the rules
  5701. for a non-reference copy-initialization (8.5). */
  5702. tree type = TREE_TYPE (ref_type);
  5703. cp_lvalue_kind lvalue = real_lvalue_p (expr);
  5704. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  5705. (type, next_conversion (convs)->type));
  5706. if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
  5707. && !TYPE_REF_IS_RVALUE (ref_type))
  5708. {
  5709. /* If the reference is volatile or non-const, we
  5710. cannot create a temporary. */
  5711. if (lvalue & clk_bitfield)
  5712. error_at (loc, "cannot bind bitfield %qE to %qT",
  5713. expr, ref_type);
  5714. else if (lvalue & clk_packed)
  5715. error_at (loc, "cannot bind packed field %qE to %qT",
  5716. expr, ref_type);
  5717. else
  5718. error_at (loc, "cannot bind rvalue %qE to %qT",
  5719. expr, ref_type);
  5720. return error_mark_node;
  5721. }
  5722. /* If the source is a packed field, and we must use a copy
  5723. constructor, then building the target expr will require
  5724. binding the field to the reference parameter to the
  5725. copy constructor, and we'll end up with an infinite
  5726. loop. If we can use a bitwise copy, then we'll be
  5727. OK. */
  5728. if ((lvalue & clk_packed)
  5729. && CLASS_TYPE_P (type)
  5730. && type_has_nontrivial_copy_init (type))
  5731. {
  5732. error_at (loc, "cannot bind packed field %qE to %qT",
  5733. expr, ref_type);
  5734. return error_mark_node;
  5735. }
  5736. if (lvalue & clk_bitfield)
  5737. {
  5738. expr = convert_bitfield_to_declared_type (expr);
  5739. expr = fold_convert (type, expr);
  5740. }
  5741. expr = build_target_expr_with_type (expr, type, complain);
  5742. }
  5743. /* Take the address of the thing to which we will bind the
  5744. reference. */
  5745. expr = cp_build_addr_expr (expr, complain);
  5746. if (expr == error_mark_node)
  5747. return error_mark_node;
  5748. /* Convert it to a pointer to the type referred to by the
  5749. reference. This will adjust the pointer if a derived to
  5750. base conversion is being performed. */
  5751. expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
  5752. expr, complain);
  5753. /* Convert the pointer to the desired reference type. */
  5754. return build_nop (ref_type, expr);
  5755. }
  5756. case ck_lvalue:
  5757. return decay_conversion (expr, complain);
  5758. case ck_qual:
  5759. /* Warn about deprecated conversion if appropriate. */
  5760. string_conv_p (totype, expr, 1);
  5761. break;
  5762. case ck_ptr:
  5763. if (convs->base_p)
  5764. expr = convert_to_base (expr, totype, !c_cast_p,
  5765. /*nonnull=*/false, complain);
  5766. return build_nop (totype, expr);
  5767. case ck_pmem:
  5768. return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
  5769. c_cast_p, complain);
  5770. default:
  5771. break;
  5772. }
  5773. if (convs->check_narrowing
  5774. && !check_narrowing (totype, expr, complain))
  5775. return error_mark_node;
  5776. if (issue_conversion_warnings)
  5777. expr = cp_convert_and_check (totype, expr, complain);
  5778. else
  5779. expr = cp_convert (totype, expr, complain);
  5780. return expr;
  5781. }
  5782. /* ARG is being passed to a varargs function. Perform any conversions
  5783. required. Return the converted value. */
  5784. tree
  5785. convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
  5786. {
  5787. tree arg_type;
  5788. location_t loc = EXPR_LOC_OR_LOC (arg, input_location);
  5789. /* [expr.call]
  5790. The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
  5791. standard conversions are performed. */
  5792. arg = decay_conversion (arg, complain);
  5793. arg_type = TREE_TYPE (arg);
  5794. /* [expr.call]
  5795. If the argument has integral or enumeration type that is subject
  5796. to the integral promotions (_conv.prom_), or a floating point
  5797. type that is subject to the floating point promotion
  5798. (_conv.fpprom_), the value of the argument is converted to the
  5799. promoted type before the call. */
  5800. if (TREE_CODE (arg_type) == REAL_TYPE
  5801. && (TYPE_PRECISION (arg_type)
  5802. < TYPE_PRECISION (double_type_node))
  5803. && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)))
  5804. {
  5805. if ((complain & tf_warning)
  5806. && warn_double_promotion && !c_inhibit_evaluation_warnings)
  5807. warning_at (loc, OPT_Wdouble_promotion,
  5808. "implicit conversion from %qT to %qT when passing "
  5809. "argument to function",
  5810. arg_type, double_type_node);
  5811. arg = convert_to_real (double_type_node, arg);
  5812. }
  5813. else if (NULLPTR_TYPE_P (arg_type))
  5814. arg = null_pointer_node;
  5815. else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
  5816. {
  5817. if (SCOPED_ENUM_P (arg_type))
  5818. {
  5819. tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
  5820. complain);
  5821. prom = cp_perform_integral_promotions (prom, complain);
  5822. if (abi_version_crosses (6)
  5823. && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
  5824. && (complain & tf_warning))
  5825. warning_at (loc, OPT_Wabi, "scoped enum %qT passed through ... as "
  5826. "%qT before -fabi-version=6, %qT after", arg_type,
  5827. TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
  5828. if (!abi_version_at_least (6))
  5829. arg = prom;
  5830. }
  5831. else
  5832. arg = cp_perform_integral_promotions (arg, complain);
  5833. }
  5834. arg = require_complete_type_sfinae (arg, complain);
  5835. arg_type = TREE_TYPE (arg);
  5836. if (arg != error_mark_node
  5837. /* In a template (or ill-formed code), we can have an incomplete type
  5838. even after require_complete_type_sfinae, in which case we don't know
  5839. whether it has trivial copy or not. */
  5840. && COMPLETE_TYPE_P (arg_type))
  5841. {
  5842. /* Build up a real lvalue-to-rvalue conversion in case the
  5843. copy constructor is trivial but not callable. */
  5844. if (!cp_unevaluated_operand && CLASS_TYPE_P (arg_type))
  5845. force_rvalue (arg, complain);
  5846. /* [expr.call] 5.2.2/7:
  5847. Passing a potentially-evaluated argument of class type (Clause 9)
  5848. with a non-trivial copy constructor or a non-trivial destructor
  5849. with no corresponding parameter is conditionally-supported, with
  5850. implementation-defined semantics.
  5851. We support it as pass-by-invisible-reference, just like a normal
  5852. value parameter.
  5853. If the call appears in the context of a sizeof expression,
  5854. it is not potentially-evaluated. */
  5855. if (cp_unevaluated_operand == 0
  5856. && (type_has_nontrivial_copy_init (arg_type)
  5857. || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)))
  5858. {
  5859. if (complain & tf_warning)
  5860. warning (OPT_Wconditionally_supported,
  5861. "passing objects of non-trivially-copyable "
  5862. "type %q#T through %<...%> is conditionally supported",
  5863. arg_type);
  5864. return cp_build_addr_expr (arg, complain);
  5865. }
  5866. }
  5867. return arg;
  5868. }
  5869. /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
  5870. tree
  5871. build_x_va_arg (source_location loc, tree expr, tree type)
  5872. {
  5873. if (processing_template_decl)
  5874. {
  5875. tree r = build_min (VA_ARG_EXPR, type, expr);
  5876. SET_EXPR_LOCATION (r, loc);
  5877. return r;
  5878. }
  5879. type = complete_type_or_else (type, NULL_TREE);
  5880. if (expr == error_mark_node || !type)
  5881. return error_mark_node;
  5882. expr = mark_lvalue_use (expr);
  5883. if (TREE_CODE (type) == REFERENCE_TYPE)
  5884. {
  5885. error ("cannot receive reference type %qT through %<...%>", type);
  5886. return error_mark_node;
  5887. }
  5888. if (type_has_nontrivial_copy_init (type)
  5889. || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  5890. {
  5891. /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
  5892. it as pass by invisible reference. */
  5893. warning_at (loc, OPT_Wconditionally_supported,
  5894. "receiving objects of non-trivially-copyable type %q#T "
  5895. "through %<...%> is conditionally-supported", type);
  5896. tree ref = cp_build_reference_type (type, false);
  5897. expr = build_va_arg (loc, expr, ref);
  5898. return convert_from_reference (expr);
  5899. }
  5900. return build_va_arg (loc, expr, type);
  5901. }
  5902. /* TYPE has been given to va_arg. Apply the default conversions which
  5903. would have happened when passed via ellipsis. Return the promoted
  5904. type, or the passed type if there is no change. */
  5905. tree
  5906. cxx_type_promotes_to (tree type)
  5907. {
  5908. tree promote;
  5909. /* Perform the array-to-pointer and function-to-pointer
  5910. conversions. */
  5911. type = type_decays_to (type);
  5912. promote = type_promotes_to (type);
  5913. if (same_type_p (type, promote))
  5914. promote = type;
  5915. return promote;
  5916. }
  5917. /* ARG is a default argument expression being passed to a parameter of
  5918. the indicated TYPE, which is a parameter to FN. PARMNUM is the
  5919. zero-based argument number. Do any required conversions. Return
  5920. the converted value. */
  5921. static GTY(()) vec<tree, va_gc> *default_arg_context;
  5922. void
  5923. push_defarg_context (tree fn)
  5924. { vec_safe_push (default_arg_context, fn); }
  5925. void
  5926. pop_defarg_context (void)
  5927. { default_arg_context->pop (); }
  5928. tree
  5929. convert_default_arg (tree type, tree arg, tree fn, int parmnum,
  5930. tsubst_flags_t complain)
  5931. {
  5932. int i;
  5933. tree t;
  5934. /* See through clones. */
  5935. fn = DECL_ORIGIN (fn);
  5936. /* Detect recursion. */
  5937. FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
  5938. if (t == fn)
  5939. {
  5940. if (complain & tf_error)
  5941. error ("recursive evaluation of default argument for %q#D", fn);
  5942. return error_mark_node;
  5943. }
  5944. /* If the ARG is an unparsed default argument expression, the
  5945. conversion cannot be performed. */
  5946. if (TREE_CODE (arg) == DEFAULT_ARG)
  5947. {
  5948. if (complain & tf_error)
  5949. error ("call to %qD uses the default argument for parameter %P, which "
  5950. "is not yet defined", fn, parmnum);
  5951. return error_mark_node;
  5952. }
  5953. push_defarg_context (fn);
  5954. if (fn && DECL_TEMPLATE_INFO (fn))
  5955. arg = tsubst_default_argument (fn, type, arg, complain);
  5956. /* Due to:
  5957. [dcl.fct.default]
  5958. The names in the expression are bound, and the semantic
  5959. constraints are checked, at the point where the default
  5960. expressions appears.
  5961. we must not perform access checks here. */
  5962. push_deferring_access_checks (dk_no_check);
  5963. /* We must make a copy of ARG, in case subsequent processing
  5964. alters any part of it. */
  5965. arg = break_out_target_exprs (arg);
  5966. arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
  5967. ICR_DEFAULT_ARGUMENT, fn, parmnum,
  5968. complain);
  5969. arg = convert_for_arg_passing (type, arg, complain);
  5970. pop_deferring_access_checks();
  5971. pop_defarg_context ();
  5972. return arg;
  5973. }
  5974. /* Returns the type which will really be used for passing an argument of
  5975. type TYPE. */
  5976. tree
  5977. type_passed_as (tree type)
  5978. {
  5979. /* Pass classes with copy ctors by invisible reference. */
  5980. if (TREE_ADDRESSABLE (type))
  5981. {
  5982. type = build_reference_type (type);
  5983. /* There are no other pointers to this temporary. */
  5984. type = cp_build_qualified_type (type, TYPE_QUAL_RESTRICT);
  5985. }
  5986. else if (targetm.calls.promote_prototypes (type)
  5987. && INTEGRAL_TYPE_P (type)
  5988. && COMPLETE_TYPE_P (type)
  5989. && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
  5990. type = integer_type_node;
  5991. return type;
  5992. }
  5993. /* Actually perform the appropriate conversion. */
  5994. tree
  5995. convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
  5996. {
  5997. tree bitfield_type;
  5998. /* If VAL is a bitfield, then -- since it has already been converted
  5999. to TYPE -- it cannot have a precision greater than TYPE.
  6000. If it has a smaller precision, we must widen it here. For
  6001. example, passing "int f:3;" to a function expecting an "int" will
  6002. not result in any conversion before this point.
  6003. If the precision is the same we must not risk widening. For
  6004. example, the COMPONENT_REF for a 32-bit "long long" bitfield will
  6005. often have type "int", even though the C++ type for the field is
  6006. "long long". If the value is being passed to a function
  6007. expecting an "int", then no conversions will be required. But,
  6008. if we call convert_bitfield_to_declared_type, the bitfield will
  6009. be converted to "long long". */
  6010. bitfield_type = is_bitfield_expr_with_lowered_type (val);
  6011. if (bitfield_type
  6012. && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
  6013. val = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type), val);
  6014. if (val == error_mark_node)
  6015. ;
  6016. /* Pass classes with copy ctors by invisible reference. */
  6017. else if (TREE_ADDRESSABLE (type))
  6018. val = build1 (ADDR_EXPR, build_reference_type (type), val);
  6019. else if (targetm.calls.promote_prototypes (type)
  6020. && INTEGRAL_TYPE_P (type)
  6021. && COMPLETE_TYPE_P (type)
  6022. && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
  6023. val = cp_perform_integral_promotions (val, complain);
  6024. if ((complain & tf_warning)
  6025. && warn_suggest_attribute_format)
  6026. {
  6027. tree rhstype = TREE_TYPE (val);
  6028. const enum tree_code coder = TREE_CODE (rhstype);
  6029. const enum tree_code codel = TREE_CODE (type);
  6030. if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
  6031. && coder == codel
  6032. && check_missing_format_attribute (type, rhstype))
  6033. warning (OPT_Wsuggest_attribute_format,
  6034. "argument of function call might be a candidate for a format attribute");
  6035. }
  6036. return val;
  6037. }
  6038. /* Returns true iff FN is a function with magic varargs, i.e. ones for
  6039. which no conversions at all should be done. This is true for some
  6040. builtins which don't act like normal functions. */
  6041. bool
  6042. magic_varargs_p (tree fn)
  6043. {
  6044. if (flag_cilkplus && is_cilkplus_reduce_builtin (fn) != BUILT_IN_NONE)
  6045. return true;
  6046. if (DECL_BUILT_IN (fn))
  6047. switch (DECL_FUNCTION_CODE (fn))
  6048. {
  6049. case BUILT_IN_CLASSIFY_TYPE:
  6050. case BUILT_IN_CONSTANT_P:
  6051. case BUILT_IN_NEXT_ARG:
  6052. case BUILT_IN_VA_START:
  6053. return true;
  6054. default:;
  6055. return lookup_attribute ("type generic",
  6056. TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
  6057. }
  6058. return false;
  6059. }
  6060. /* Returns the decl of the dispatcher function if FN is a function version. */
  6061. tree
  6062. get_function_version_dispatcher (tree fn)
  6063. {
  6064. tree dispatcher_decl = NULL;
  6065. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
  6066. && DECL_FUNCTION_VERSIONED (fn));
  6067. gcc_assert (targetm.get_function_versions_dispatcher);
  6068. dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
  6069. if (dispatcher_decl == NULL)
  6070. {
  6071. error_at (input_location, "use of multiversioned function "
  6072. "without a default");
  6073. return NULL;
  6074. }
  6075. retrofit_lang_decl (dispatcher_decl);
  6076. gcc_assert (dispatcher_decl != NULL);
  6077. return dispatcher_decl;
  6078. }
  6079. /* fn is a function version dispatcher that is marked used. Mark all the
  6080. semantically identical function versions it will dispatch as used. */
  6081. void
  6082. mark_versions_used (tree fn)
  6083. {
  6084. struct cgraph_node *node;
  6085. struct cgraph_function_version_info *node_v;
  6086. struct cgraph_function_version_info *it_v;
  6087. gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
  6088. node = cgraph_node::get (fn);
  6089. if (node == NULL)
  6090. return;
  6091. gcc_assert (node->dispatcher_function);
  6092. node_v = node->function_version ();
  6093. if (node_v == NULL)
  6094. return;
  6095. /* All semantically identical versions are chained. Traverse and mark each
  6096. one of them as used. */
  6097. it_v = node_v->next;
  6098. while (it_v != NULL)
  6099. {
  6100. mark_used (it_v->this_node->decl);
  6101. it_v = it_v->next;
  6102. }
  6103. }
  6104. /* Build a call to "the copy constructor" for the type of A, even if it
  6105. wouldn't be selected by normal overload resolution. Used for
  6106. diagnostics. */
  6107. static tree
  6108. call_copy_ctor (tree a, tsubst_flags_t complain)
  6109. {
  6110. tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
  6111. tree binfo = TYPE_BINFO (ctype);
  6112. tree copy = get_copy_ctor (ctype, complain);
  6113. copy = build_baselink (binfo, binfo, copy, NULL_TREE);
  6114. tree ob = build_dummy_object (ctype);
  6115. vec<tree, va_gc>* args = make_tree_vector_single (a);
  6116. tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
  6117. LOOKUP_NORMAL, NULL, complain);
  6118. release_tree_vector (args);
  6119. return r;
  6120. }
  6121. /* Subroutine of the various build_*_call functions. Overload resolution
  6122. has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
  6123. ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
  6124. bitmask of various LOOKUP_* flags which apply to the call itself. */
  6125. static tree
  6126. build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
  6127. {
  6128. tree fn = cand->fn;
  6129. const vec<tree, va_gc> *args = cand->args;
  6130. tree first_arg = cand->first_arg;
  6131. conversion **convs = cand->convs;
  6132. conversion *conv;
  6133. tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
  6134. int parmlen;
  6135. tree val;
  6136. int i = 0;
  6137. int j = 0;
  6138. unsigned int arg_index = 0;
  6139. int is_method = 0;
  6140. int nargs;
  6141. tree *argarray;
  6142. bool already_used = false;
  6143. /* In a template, there is no need to perform all of the work that
  6144. is normally done. We are only interested in the type of the call
  6145. expression, i.e., the return type of the function. Any semantic
  6146. errors will be deferred until the template is instantiated. */
  6147. if (processing_template_decl)
  6148. {
  6149. tree expr, addr;
  6150. tree return_type;
  6151. const tree *argarray;
  6152. unsigned int nargs;
  6153. return_type = TREE_TYPE (TREE_TYPE (fn));
  6154. nargs = vec_safe_length (args);
  6155. if (first_arg == NULL_TREE)
  6156. argarray = args->address ();
  6157. else
  6158. {
  6159. tree *alcarray;
  6160. unsigned int ix;
  6161. tree arg;
  6162. ++nargs;
  6163. alcarray = XALLOCAVEC (tree, nargs);
  6164. alcarray[0] = build_this (first_arg);
  6165. FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
  6166. alcarray[ix + 1] = arg;
  6167. argarray = alcarray;
  6168. }
  6169. addr = build_addr_func (fn, complain);
  6170. if (addr == error_mark_node)
  6171. return error_mark_node;
  6172. expr = build_call_array_loc (input_location, return_type,
  6173. addr, nargs, argarray);
  6174. if (TREE_THIS_VOLATILE (fn) && cfun)
  6175. current_function_returns_abnormally = 1;
  6176. return convert_from_reference (expr);
  6177. }
  6178. /* Give any warnings we noticed during overload resolution. */
  6179. if (cand->warnings && (complain & tf_warning))
  6180. {
  6181. struct candidate_warning *w;
  6182. for (w = cand->warnings; w; w = w->next)
  6183. joust (cand, w->loser, 1, complain);
  6184. }
  6185. /* Make =delete work with SFINAE. */
  6186. if (DECL_DELETED_FN (fn) && !(complain & tf_error))
  6187. return error_mark_node;
  6188. if (DECL_FUNCTION_MEMBER_P (fn))
  6189. {
  6190. tree access_fn;
  6191. /* If FN is a template function, two cases must be considered.
  6192. For example:
  6193. struct A {
  6194. protected:
  6195. template <class T> void f();
  6196. };
  6197. template <class T> struct B {
  6198. protected:
  6199. void g();
  6200. };
  6201. struct C : A, B<int> {
  6202. using A::f; // #1
  6203. using B<int>::g; // #2
  6204. };
  6205. In case #1 where `A::f' is a member template, DECL_ACCESS is
  6206. recorded in the primary template but not in its specialization.
  6207. We check access of FN using its primary template.
  6208. In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
  6209. because it is a member of class template B, DECL_ACCESS is
  6210. recorded in the specialization `B<int>::g'. We cannot use its
  6211. primary template because `B<T>::g' and `B<int>::g' may have
  6212. different access. */
  6213. if (DECL_TEMPLATE_INFO (fn)
  6214. && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
  6215. access_fn = DECL_TI_TEMPLATE (fn);
  6216. else
  6217. access_fn = fn;
  6218. if (!perform_or_defer_access_check (cand->access_path, access_fn,
  6219. fn, complain))
  6220. return error_mark_node;
  6221. }
  6222. /* If we're checking for implicit delete, don't bother with argument
  6223. conversions. */
  6224. if (flags & LOOKUP_SPECULATIVE)
  6225. {
  6226. if (DECL_DELETED_FN (fn))
  6227. {
  6228. if (complain & tf_error)
  6229. mark_used (fn);
  6230. return error_mark_node;
  6231. }
  6232. if (cand->viable == 1)
  6233. return fn;
  6234. else if (!(complain & tf_error))
  6235. /* Reject bad conversions now. */
  6236. return error_mark_node;
  6237. /* else continue to get conversion error. */
  6238. }
  6239. /* N3276 magic doesn't apply to nested calls. */
  6240. int decltype_flag = (complain & tf_decltype);
  6241. complain &= ~tf_decltype;
  6242. /* Find maximum size of vector to hold converted arguments. */
  6243. parmlen = list_length (parm);
  6244. nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
  6245. if (parmlen > nargs)
  6246. nargs = parmlen;
  6247. argarray = XALLOCAVEC (tree, nargs);
  6248. /* The implicit parameters to a constructor are not considered by overload
  6249. resolution, and must be of the proper type. */
  6250. if (DECL_CONSTRUCTOR_P (fn))
  6251. {
  6252. tree object_arg;
  6253. if (first_arg != NULL_TREE)
  6254. {
  6255. object_arg = first_arg;
  6256. first_arg = NULL_TREE;
  6257. }
  6258. else
  6259. {
  6260. object_arg = (*args)[arg_index];
  6261. ++arg_index;
  6262. }
  6263. argarray[j++] = build_this (object_arg);
  6264. parm = TREE_CHAIN (parm);
  6265. /* We should never try to call the abstract constructor. */
  6266. gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
  6267. if (DECL_HAS_VTT_PARM_P (fn))
  6268. {
  6269. argarray[j++] = (*args)[arg_index];
  6270. ++arg_index;
  6271. parm = TREE_CHAIN (parm);
  6272. }
  6273. }
  6274. /* Bypass access control for 'this' parameter. */
  6275. else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
  6276. {
  6277. tree parmtype = TREE_VALUE (parm);
  6278. tree arg = build_this (first_arg != NULL_TREE
  6279. ? first_arg
  6280. : (*args)[arg_index]);
  6281. tree argtype = TREE_TYPE (arg);
  6282. tree converted_arg;
  6283. tree base_binfo;
  6284. if (convs[i]->bad_p)
  6285. {
  6286. if (complain & tf_error)
  6287. {
  6288. if (permerror (input_location, "passing %qT as %<this%> "
  6289. "argument discards qualifiers",
  6290. TREE_TYPE (argtype)))
  6291. inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
  6292. }
  6293. else
  6294. return error_mark_node;
  6295. }
  6296. /* See if the function member or the whole class type is declared
  6297. final and the call can be devirtualized. */
  6298. if (DECL_FINAL_P (fn)
  6299. || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
  6300. flags |= LOOKUP_NONVIRTUAL;
  6301. /* [class.mfct.nonstatic]: If a nonstatic member function of a class
  6302. X is called for an object that is not of type X, or of a type
  6303. derived from X, the behavior is undefined.
  6304. So we can assume that anything passed as 'this' is non-null, and
  6305. optimize accordingly. */
  6306. gcc_assert (TYPE_PTR_P (parmtype));
  6307. /* Convert to the base in which the function was declared. */
  6308. gcc_assert (cand->conversion_path != NULL_TREE);
  6309. converted_arg = build_base_path (PLUS_EXPR,
  6310. arg,
  6311. cand->conversion_path,
  6312. 1, complain);
  6313. /* Check that the base class is accessible. */
  6314. if (!accessible_base_p (TREE_TYPE (argtype),
  6315. BINFO_TYPE (cand->conversion_path), true))
  6316. {
  6317. if (complain & tf_error)
  6318. error ("%qT is not an accessible base of %qT",
  6319. BINFO_TYPE (cand->conversion_path),
  6320. TREE_TYPE (argtype));
  6321. else
  6322. return error_mark_node;
  6323. }
  6324. /* If fn was found by a using declaration, the conversion path
  6325. will be to the derived class, not the base declaring fn. We
  6326. must convert from derived to base. */
  6327. base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
  6328. TREE_TYPE (parmtype), ba_unique,
  6329. NULL, complain);
  6330. converted_arg = build_base_path (PLUS_EXPR, converted_arg,
  6331. base_binfo, 1, complain);
  6332. argarray[j++] = converted_arg;
  6333. parm = TREE_CHAIN (parm);
  6334. if (first_arg != NULL_TREE)
  6335. first_arg = NULL_TREE;
  6336. else
  6337. ++arg_index;
  6338. ++i;
  6339. is_method = 1;
  6340. }
  6341. gcc_assert (first_arg == NULL_TREE);
  6342. for (; arg_index < vec_safe_length (args) && parm;
  6343. parm = TREE_CHAIN (parm), ++arg_index, ++i)
  6344. {
  6345. tree type = TREE_VALUE (parm);
  6346. tree arg = (*args)[arg_index];
  6347. bool conversion_warning = true;
  6348. conv = convs[i];
  6349. /* If the argument is NULL and used to (implicitly) instantiate a
  6350. template function (and bind one of the template arguments to
  6351. the type of 'long int'), we don't want to warn about passing NULL
  6352. to non-pointer argument.
  6353. For example, if we have this template function:
  6354. template<typename T> void func(T x) {}
  6355. we want to warn (when -Wconversion is enabled) in this case:
  6356. void foo() {
  6357. func<int>(NULL);
  6358. }
  6359. but not in this case:
  6360. void foo() {
  6361. func(NULL);
  6362. }
  6363. */
  6364. if (arg == null_node
  6365. && DECL_TEMPLATE_INFO (fn)
  6366. && cand->template_decl
  6367. && !(flags & LOOKUP_EXPLICIT_TMPL_ARGS))
  6368. conversion_warning = false;
  6369. /* Warn about initializer_list deduction that isn't currently in the
  6370. working draft. */
  6371. if (cxx_dialect > cxx98
  6372. && flag_deduce_init_list
  6373. && cand->template_decl
  6374. && is_std_init_list (non_reference (type))
  6375. && BRACE_ENCLOSED_INITIALIZER_P (arg))
  6376. {
  6377. tree tmpl = TI_TEMPLATE (cand->template_decl);
  6378. tree realparm = chain_index (j, DECL_ARGUMENTS (cand->fn));
  6379. tree patparm = get_pattern_parm (realparm, tmpl);
  6380. tree pattype = TREE_TYPE (patparm);
  6381. if (PACK_EXPANSION_P (pattype))
  6382. pattype = PACK_EXPANSION_PATTERN (pattype);
  6383. pattype = non_reference (pattype);
  6384. if (TREE_CODE (pattype) == TEMPLATE_TYPE_PARM
  6385. && (cand->explicit_targs == NULL_TREE
  6386. || (TREE_VEC_LENGTH (cand->explicit_targs)
  6387. <= TEMPLATE_TYPE_IDX (pattype))))
  6388. {
  6389. pedwarn (input_location, 0, "deducing %qT as %qT",
  6390. non_reference (TREE_TYPE (patparm)),
  6391. non_reference (type));
  6392. pedwarn (input_location, 0, " in call to %q+D", cand->fn);
  6393. pedwarn (input_location, 0,
  6394. " (you can disable this with -fno-deduce-init-list)");
  6395. }
  6396. }
  6397. val = convert_like_with_context (conv, arg, fn, i - is_method,
  6398. conversion_warning
  6399. ? complain
  6400. : complain & (~tf_warning));
  6401. val = convert_for_arg_passing (type, val, complain);
  6402. if (val == error_mark_node)
  6403. return error_mark_node;
  6404. else
  6405. argarray[j++] = val;
  6406. }
  6407. /* Default arguments */
  6408. for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
  6409. {
  6410. if (TREE_VALUE (parm) == error_mark_node)
  6411. return error_mark_node;
  6412. argarray[j++] = convert_default_arg (TREE_VALUE (parm),
  6413. TREE_PURPOSE (parm),
  6414. fn, i - is_method,
  6415. complain);
  6416. }
  6417. /* Ellipsis */
  6418. for (; arg_index < vec_safe_length (args); ++arg_index)
  6419. {
  6420. tree a = (*args)[arg_index];
  6421. if (magic_varargs_p (fn))
  6422. /* Do no conversions for magic varargs. */
  6423. a = mark_type_use (a);
  6424. else if (DECL_CONSTRUCTOR_P (fn)
  6425. && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
  6426. TREE_TYPE (a)))
  6427. {
  6428. /* Avoid infinite recursion trying to call A(...). */
  6429. if (complain & tf_error)
  6430. /* Try to call the actual copy constructor for a good error. */
  6431. call_copy_ctor (a, complain);
  6432. return error_mark_node;
  6433. }
  6434. else
  6435. a = convert_arg_to_ellipsis (a, complain);
  6436. argarray[j++] = a;
  6437. }
  6438. gcc_assert (j <= nargs);
  6439. nargs = j;
  6440. check_function_arguments (TREE_TYPE (fn), nargs, argarray);
  6441. /* Avoid actually calling copy constructors and copy assignment operators,
  6442. if possible. */
  6443. if (! flag_elide_constructors)
  6444. /* Do things the hard way. */;
  6445. else if (cand->num_convs == 1
  6446. && (DECL_COPY_CONSTRUCTOR_P (fn)
  6447. || DECL_MOVE_CONSTRUCTOR_P (fn))
  6448. /* It's unsafe to elide the constructor when handling
  6449. a noexcept-expression, it may evaluate to the wrong
  6450. value (c++/53025). */
  6451. && cp_noexcept_operand == 0)
  6452. {
  6453. tree targ;
  6454. tree arg = argarray[num_artificial_parms_for (fn)];
  6455. tree fa;
  6456. bool trivial = trivial_fn_p (fn);
  6457. /* Pull out the real argument, disregarding const-correctness. */
  6458. targ = arg;
  6459. while (CONVERT_EXPR_P (targ)
  6460. || TREE_CODE (targ) == NON_LVALUE_EXPR)
  6461. targ = TREE_OPERAND (targ, 0);
  6462. if (TREE_CODE (targ) == ADDR_EXPR)
  6463. {
  6464. targ = TREE_OPERAND (targ, 0);
  6465. if (!same_type_ignoring_top_level_qualifiers_p
  6466. (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
  6467. targ = NULL_TREE;
  6468. }
  6469. else
  6470. targ = NULL_TREE;
  6471. if (targ)
  6472. arg = targ;
  6473. else
  6474. arg = cp_build_indirect_ref (arg, RO_NULL, complain);
  6475. /* [class.copy]: the copy constructor is implicitly defined even if
  6476. the implementation elided its use. */
  6477. if (!trivial || DECL_DELETED_FN (fn))
  6478. {
  6479. mark_used (fn);
  6480. already_used = true;
  6481. }
  6482. /* If we're creating a temp and we already have one, don't create a
  6483. new one. If we're not creating a temp but we get one, use
  6484. INIT_EXPR to collapse the temp into our target. Otherwise, if the
  6485. ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
  6486. temp or an INIT_EXPR otherwise. */
  6487. fa = argarray[0];
  6488. if (is_dummy_object (fa))
  6489. {
  6490. if (TREE_CODE (arg) == TARGET_EXPR)
  6491. return arg;
  6492. else if (trivial)
  6493. return force_target_expr (DECL_CONTEXT (fn), arg, complain);
  6494. }
  6495. else if (TREE_CODE (arg) == TARGET_EXPR || trivial)
  6496. {
  6497. tree to = stabilize_reference (cp_build_indirect_ref (fa, RO_NULL,
  6498. complain));
  6499. val = build2 (INIT_EXPR, DECL_CONTEXT (fn), to, arg);
  6500. return val;
  6501. }
  6502. }
  6503. else if (DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR
  6504. && trivial_fn_p (fn)
  6505. && !DECL_DELETED_FN (fn))
  6506. {
  6507. tree to = stabilize_reference
  6508. (cp_build_indirect_ref (argarray[0], RO_NULL, complain));
  6509. tree type = TREE_TYPE (to);
  6510. tree as_base = CLASSTYPE_AS_BASE (type);
  6511. tree arg = argarray[1];
  6512. if (is_really_empty_class (type))
  6513. {
  6514. /* Avoid copying empty classes. */
  6515. val = build2 (COMPOUND_EXPR, void_type_node, to, arg);
  6516. TREE_NO_WARNING (val) = 1;
  6517. val = build2 (COMPOUND_EXPR, type, val, to);
  6518. TREE_NO_WARNING (val) = 1;
  6519. }
  6520. else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
  6521. {
  6522. arg = cp_build_indirect_ref (arg, RO_NULL, complain);
  6523. val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
  6524. }
  6525. else
  6526. {
  6527. /* We must only copy the non-tail padding parts. */
  6528. tree arg0, arg2, t;
  6529. tree array_type, alias_set;
  6530. arg2 = TYPE_SIZE_UNIT (as_base);
  6531. arg0 = cp_build_addr_expr (to, complain);
  6532. array_type = build_array_type (char_type_node,
  6533. build_index_type
  6534. (size_binop (MINUS_EXPR,
  6535. arg2, size_int (1))));
  6536. alias_set = build_int_cst (build_pointer_type (type), 0);
  6537. t = build2 (MODIFY_EXPR, void_type_node,
  6538. build2 (MEM_REF, array_type, arg0, alias_set),
  6539. build2 (MEM_REF, array_type, arg, alias_set));
  6540. val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
  6541. TREE_NO_WARNING (val) = 1;
  6542. }
  6543. return val;
  6544. }
  6545. else if (DECL_DESTRUCTOR_P (fn)
  6546. && trivial_fn_p (fn)
  6547. && !DECL_DELETED_FN (fn))
  6548. return fold_convert (void_type_node, argarray[0]);
  6549. /* FIXME handle trivial default constructor, too. */
  6550. /* For calls to a multi-versioned function, overload resolution
  6551. returns the function with the highest target priority, that is,
  6552. the version that will checked for dispatching first. If this
  6553. version is inlinable, a direct call to this version can be made
  6554. otherwise the call should go through the dispatcher. */
  6555. if (DECL_FUNCTION_VERSIONED (fn)
  6556. && (current_function_decl == NULL
  6557. || !targetm.target_option.can_inline_p (current_function_decl, fn)))
  6558. {
  6559. fn = get_function_version_dispatcher (fn);
  6560. if (fn == NULL)
  6561. return NULL;
  6562. if (!already_used)
  6563. mark_versions_used (fn);
  6564. }
  6565. if (!already_used
  6566. && !mark_used (fn, complain))
  6567. return error_mark_node;
  6568. if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
  6569. /* Don't mess with virtual lookup in instantiate_non_dependent_expr;
  6570. virtual functions can't be constexpr. */
  6571. && !in_template_function ())
  6572. {
  6573. tree t;
  6574. tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
  6575. DECL_CONTEXT (fn),
  6576. ba_any, NULL, complain);
  6577. gcc_assert (binfo && binfo != error_mark_node);
  6578. argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
  6579. complain);
  6580. if (TREE_SIDE_EFFECTS (argarray[0]))
  6581. argarray[0] = save_expr (argarray[0]);
  6582. t = build_pointer_type (TREE_TYPE (fn));
  6583. if (DECL_CONTEXT (fn) && TYPE_JAVA_INTERFACE (DECL_CONTEXT (fn)))
  6584. fn = build_java_interface_fn_ref (fn, argarray[0]);
  6585. else
  6586. fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
  6587. TREE_TYPE (fn) = t;
  6588. }
  6589. else
  6590. {
  6591. fn = build_addr_func (fn, complain);
  6592. if (fn == error_mark_node)
  6593. return error_mark_node;
  6594. }
  6595. tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
  6596. if (TREE_CODE (call) == CALL_EXPR
  6597. && (cand->flags & LOOKUP_LIST_INIT_CTOR))
  6598. CALL_EXPR_LIST_INIT_P (call) = true;
  6599. return call;
  6600. }
  6601. /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
  6602. This function performs no overload resolution, conversion, or other
  6603. high-level operations. */
  6604. tree
  6605. build_cxx_call (tree fn, int nargs, tree *argarray,
  6606. tsubst_flags_t complain)
  6607. {
  6608. tree fndecl;
  6609. int optimize_sav;
  6610. /* Remember roughly where this call is. */
  6611. location_t loc = EXPR_LOC_OR_LOC (fn, input_location);
  6612. fn = build_call_a (fn, nargs, argarray);
  6613. SET_EXPR_LOCATION (fn, loc);
  6614. fndecl = get_callee_fndecl (fn);
  6615. /* Check that arguments to builtin functions match the expectations. */
  6616. if (fndecl
  6617. && DECL_BUILT_IN (fndecl)
  6618. && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
  6619. && !check_builtin_function_arguments (fndecl, nargs, argarray))
  6620. return error_mark_node;
  6621. /* If it is a built-in array notation function, then the return type of
  6622. the function is the element type of the array passed in as array
  6623. notation (i.e. the first parameter of the function). */
  6624. if (flag_cilkplus && TREE_CODE (fn) == CALL_EXPR)
  6625. {
  6626. enum built_in_function bif =
  6627. is_cilkplus_reduce_builtin (CALL_EXPR_FN (fn));
  6628. if (bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ADD
  6629. || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUL
  6630. || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MAX
  6631. || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MIN
  6632. || bif == BUILT_IN_CILKPLUS_SEC_REDUCE
  6633. || bif == BUILT_IN_CILKPLUS_SEC_REDUCE_MUTATING)
  6634. {
  6635. if (call_expr_nargs (fn) == 0)
  6636. {
  6637. error_at (EXPR_LOCATION (fn), "Invalid builtin arguments");
  6638. return error_mark_node;
  6639. }
  6640. /* for bif == BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_ZERO or
  6641. BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_ZERO or
  6642. BUILT_IN_CILKPLUS_SEC_REDUCE_ANY_NONZERO or
  6643. BUILT_IN_CILKPLUS_SEC_REDUCE_ALL_NONZERO or
  6644. BUILT_IN_CILKPLUS_SEC_REDUCE_MIN_IND or
  6645. BUILT_IN_CILKPLUS_SEC_REDUCE_MAX_IND
  6646. The pre-defined return-type is the correct one. */
  6647. tree array_ntn = CALL_EXPR_ARG (fn, 0);
  6648. TREE_TYPE (fn) = TREE_TYPE (array_ntn);
  6649. return fn;
  6650. }
  6651. }
  6652. /* Some built-in function calls will be evaluated at compile-time in
  6653. fold (). Set optimize to 1 when folding __builtin_constant_p inside
  6654. a constexpr function so that fold_builtin_1 doesn't fold it to 0. */
  6655. optimize_sav = optimize;
  6656. if (!optimize && fndecl && DECL_IS_BUILTIN_CONSTANT_P (fndecl)
  6657. && current_function_decl
  6658. && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
  6659. optimize = 1;
  6660. fn = fold_if_not_in_template (fn);
  6661. optimize = optimize_sav;
  6662. if (VOID_TYPE_P (TREE_TYPE (fn)))
  6663. return fn;
  6664. /* 5.2.2/11: If a function call is a prvalue of object type: if the
  6665. function call is either the operand of a decltype-specifier or the
  6666. right operand of a comma operator that is the operand of a
  6667. decltype-specifier, a temporary object is not introduced for the
  6668. prvalue. The type of the prvalue may be incomplete. */
  6669. if (!(complain & tf_decltype))
  6670. {
  6671. fn = require_complete_type_sfinae (fn, complain);
  6672. if (fn == error_mark_node)
  6673. return error_mark_node;
  6674. if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
  6675. fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
  6676. }
  6677. return convert_from_reference (fn);
  6678. }
  6679. static GTY(()) tree java_iface_lookup_fn;
  6680. /* Make an expression which yields the address of the Java interface
  6681. method FN. This is achieved by generating a call to libjava's
  6682. _Jv_LookupInterfaceMethodIdx(). */
  6683. static tree
  6684. build_java_interface_fn_ref (tree fn, tree instance)
  6685. {
  6686. tree lookup_fn, method, idx;
  6687. tree klass_ref, iface, iface_ref;
  6688. int i;
  6689. if (!java_iface_lookup_fn)
  6690. {
  6691. tree ftype = build_function_type_list (ptr_type_node,
  6692. ptr_type_node, ptr_type_node,
  6693. java_int_type_node, NULL_TREE);
  6694. java_iface_lookup_fn
  6695. = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", ftype,
  6696. 0, NOT_BUILT_IN, NULL, NULL_TREE);
  6697. }
  6698. /* Look up the pointer to the runtime java.lang.Class object for `instance'.
  6699. This is the first entry in the vtable. */
  6700. klass_ref = build_vtbl_ref (cp_build_indirect_ref (instance, RO_NULL,
  6701. tf_warning_or_error),
  6702. integer_zero_node);
  6703. /* Get the java.lang.Class pointer for the interface being called. */
  6704. iface = DECL_CONTEXT (fn);
  6705. iface_ref = lookup_field (iface, get_identifier ("class$"), 0, false);
  6706. if (!iface_ref || !VAR_P (iface_ref)
  6707. || DECL_CONTEXT (iface_ref) != iface)
  6708. {
  6709. error ("could not find class$ field in java interface type %qT",
  6710. iface);
  6711. return error_mark_node;
  6712. }
  6713. iface_ref = build_address (iface_ref);
  6714. iface_ref = convert (build_pointer_type (iface), iface_ref);
  6715. /* Determine the itable index of FN. */
  6716. i = 1;
  6717. for (method = TYPE_METHODS (iface); method; method = DECL_CHAIN (method))
  6718. {
  6719. if (!DECL_VIRTUAL_P (method))
  6720. continue;
  6721. if (fn == method)
  6722. break;
  6723. i++;
  6724. }
  6725. idx = build_int_cst (NULL_TREE, i);
  6726. lookup_fn = build1 (ADDR_EXPR,
  6727. build_pointer_type (TREE_TYPE (java_iface_lookup_fn)),
  6728. java_iface_lookup_fn);
  6729. return build_call_nary (ptr_type_node, lookup_fn,
  6730. 3, klass_ref, iface_ref, idx);
  6731. }
  6732. /* Returns the value to use for the in-charge parameter when making a
  6733. call to a function with the indicated NAME.
  6734. FIXME:Can't we find a neater way to do this mapping? */
  6735. tree
  6736. in_charge_arg_for_name (tree name)
  6737. {
  6738. if (name == base_ctor_identifier
  6739. || name == base_dtor_identifier)
  6740. return integer_zero_node;
  6741. else if (name == complete_ctor_identifier)
  6742. return integer_one_node;
  6743. else if (name == complete_dtor_identifier)
  6744. return integer_two_node;
  6745. else if (name == deleting_dtor_identifier)
  6746. return integer_three_node;
  6747. /* This function should only be called with one of the names listed
  6748. above. */
  6749. gcc_unreachable ();
  6750. return NULL_TREE;
  6751. }
  6752. /* Build a call to a constructor, destructor, or an assignment
  6753. operator for INSTANCE, an expression with class type. NAME
  6754. indicates the special member function to call; *ARGS are the
  6755. arguments. ARGS may be NULL. This may change ARGS. BINFO
  6756. indicates the base of INSTANCE that is to be passed as the `this'
  6757. parameter to the member function called.
  6758. FLAGS are the LOOKUP_* flags to use when processing the call.
  6759. If NAME indicates a complete object constructor, INSTANCE may be
  6760. NULL_TREE. In this case, the caller will call build_cplus_new to
  6761. store the newly constructed object into a VAR_DECL. */
  6762. tree
  6763. build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
  6764. tree binfo, int flags, tsubst_flags_t complain)
  6765. {
  6766. tree fns;
  6767. /* The type of the subobject to be constructed or destroyed. */
  6768. tree class_type;
  6769. vec<tree, va_gc> *allocated = NULL;
  6770. tree ret;
  6771. gcc_assert (name == complete_ctor_identifier
  6772. || name == base_ctor_identifier
  6773. || name == complete_dtor_identifier
  6774. || name == base_dtor_identifier
  6775. || name == deleting_dtor_identifier
  6776. || name == ansi_assopname (NOP_EXPR));
  6777. if (TYPE_P (binfo))
  6778. {
  6779. /* Resolve the name. */
  6780. if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
  6781. return error_mark_node;
  6782. binfo = TYPE_BINFO (binfo);
  6783. }
  6784. gcc_assert (binfo != NULL_TREE);
  6785. class_type = BINFO_TYPE (binfo);
  6786. /* Handle the special case where INSTANCE is NULL_TREE. */
  6787. if (name == complete_ctor_identifier && !instance)
  6788. instance = build_dummy_object (class_type);
  6789. else
  6790. {
  6791. if (name == complete_dtor_identifier
  6792. || name == base_dtor_identifier
  6793. || name == deleting_dtor_identifier)
  6794. gcc_assert (args == NULL || vec_safe_is_empty (*args));
  6795. /* Convert to the base class, if necessary. */
  6796. if (!same_type_ignoring_top_level_qualifiers_p
  6797. (TREE_TYPE (instance), BINFO_TYPE (binfo)))
  6798. {
  6799. if (name != ansi_assopname (NOP_EXPR))
  6800. /* For constructors and destructors, either the base is
  6801. non-virtual, or it is virtual but we are doing the
  6802. conversion from a constructor or destructor for the
  6803. complete object. In either case, we can convert
  6804. statically. */
  6805. instance = convert_to_base_statically (instance, binfo);
  6806. else
  6807. /* However, for assignment operators, we must convert
  6808. dynamically if the base is virtual. */
  6809. instance = build_base_path (PLUS_EXPR, instance,
  6810. binfo, /*nonnull=*/1, complain);
  6811. }
  6812. }
  6813. gcc_assert (instance != NULL_TREE);
  6814. fns = lookup_fnfields (binfo, name, 1);
  6815. /* When making a call to a constructor or destructor for a subobject
  6816. that uses virtual base classes, pass down a pointer to a VTT for
  6817. the subobject. */
  6818. if ((name == base_ctor_identifier
  6819. || name == base_dtor_identifier)
  6820. && CLASSTYPE_VBASECLASSES (class_type))
  6821. {
  6822. tree vtt;
  6823. tree sub_vtt;
  6824. /* If the current function is a complete object constructor
  6825. or destructor, then we fetch the VTT directly.
  6826. Otherwise, we look it up using the VTT we were given. */
  6827. vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
  6828. vtt = decay_conversion (vtt, complain);
  6829. if (vtt == error_mark_node)
  6830. return error_mark_node;
  6831. vtt = build3 (COND_EXPR, TREE_TYPE (vtt),
  6832. build2 (EQ_EXPR, boolean_type_node,
  6833. current_in_charge_parm, integer_zero_node),
  6834. current_vtt_parm,
  6835. vtt);
  6836. if (BINFO_SUBVTT_INDEX (binfo))
  6837. sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
  6838. else
  6839. sub_vtt = vtt;
  6840. if (args == NULL)
  6841. {
  6842. allocated = make_tree_vector ();
  6843. args = &allocated;
  6844. }
  6845. vec_safe_insert (*args, 0, sub_vtt);
  6846. }
  6847. ret = build_new_method_call (instance, fns, args,
  6848. TYPE_BINFO (BINFO_TYPE (binfo)),
  6849. flags, /*fn=*/NULL,
  6850. complain);
  6851. if (allocated != NULL)
  6852. release_tree_vector (allocated);
  6853. if ((complain & tf_error)
  6854. && (flags & LOOKUP_DELEGATING_CONS)
  6855. && name == complete_ctor_identifier
  6856. && TREE_CODE (ret) == CALL_EXPR
  6857. && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0))
  6858. == current_function_decl))
  6859. error ("constructor delegates to itself");
  6860. return ret;
  6861. }
  6862. /* Return the NAME, as a C string. The NAME indicates a function that
  6863. is a member of TYPE. *FREE_P is set to true if the caller must
  6864. free the memory returned.
  6865. Rather than go through all of this, we should simply set the names
  6866. of constructors and destructors appropriately, and dispense with
  6867. ctor_identifier, dtor_identifier, etc. */
  6868. static char *
  6869. name_as_c_string (tree name, tree type, bool *free_p)
  6870. {
  6871. char *pretty_name;
  6872. /* Assume that we will not allocate memory. */
  6873. *free_p = false;
  6874. /* Constructors and destructors are special. */
  6875. if (IDENTIFIER_CTOR_OR_DTOR_P (name))
  6876. {
  6877. pretty_name
  6878. = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))));
  6879. /* For a destructor, add the '~'. */
  6880. if (name == complete_dtor_identifier
  6881. || name == base_dtor_identifier
  6882. || name == deleting_dtor_identifier)
  6883. {
  6884. pretty_name = concat ("~", pretty_name, NULL);
  6885. /* Remember that we need to free the memory allocated. */
  6886. *free_p = true;
  6887. }
  6888. }
  6889. else if (IDENTIFIER_TYPENAME_P (name))
  6890. {
  6891. pretty_name = concat ("operator ",
  6892. type_as_string_translate (TREE_TYPE (name),
  6893. TFF_PLAIN_IDENTIFIER),
  6894. NULL);
  6895. /* Remember that we need to free the memory allocated. */
  6896. *free_p = true;
  6897. }
  6898. else
  6899. pretty_name = CONST_CAST (char *, identifier_to_locale (IDENTIFIER_POINTER (name)));
  6900. return pretty_name;
  6901. }
  6902. /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
  6903. be set, upon return, to the function called. ARGS may be NULL.
  6904. This may change ARGS. */
  6905. static tree
  6906. build_new_method_call_1 (tree instance, tree fns, vec<tree, va_gc> **args,
  6907. tree conversion_path, int flags,
  6908. tree *fn_p, tsubst_flags_t complain)
  6909. {
  6910. struct z_candidate *candidates = 0, *cand;
  6911. tree explicit_targs = NULL_TREE;
  6912. tree basetype = NULL_TREE;
  6913. tree access_binfo, binfo;
  6914. tree optype;
  6915. tree first_mem_arg = NULL_TREE;
  6916. tree name;
  6917. bool skip_first_for_error;
  6918. vec<tree, va_gc> *user_args;
  6919. tree call;
  6920. tree fn;
  6921. int template_only = 0;
  6922. bool any_viable_p;
  6923. tree orig_instance;
  6924. tree orig_fns;
  6925. vec<tree, va_gc> *orig_args = NULL;
  6926. void *p;
  6927. gcc_assert (instance != NULL_TREE);
  6928. /* We don't know what function we're going to call, yet. */
  6929. if (fn_p)
  6930. *fn_p = NULL_TREE;
  6931. if (error_operand_p (instance)
  6932. || !fns || error_operand_p (fns))
  6933. return error_mark_node;
  6934. if (!BASELINK_P (fns))
  6935. {
  6936. if (complain & tf_error)
  6937. error ("call to non-function %qD", fns);
  6938. return error_mark_node;
  6939. }
  6940. orig_instance = instance;
  6941. orig_fns = fns;
  6942. /* Dismantle the baselink to collect all the information we need. */
  6943. if (!conversion_path)
  6944. conversion_path = BASELINK_BINFO (fns);
  6945. access_binfo = BASELINK_ACCESS_BINFO (fns);
  6946. binfo = BASELINK_BINFO (fns);
  6947. optype = BASELINK_OPTYPE (fns);
  6948. fns = BASELINK_FUNCTIONS (fns);
  6949. if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
  6950. {
  6951. explicit_targs = TREE_OPERAND (fns, 1);
  6952. fns = TREE_OPERAND (fns, 0);
  6953. template_only = 1;
  6954. }
  6955. gcc_assert (TREE_CODE (fns) == FUNCTION_DECL
  6956. || TREE_CODE (fns) == TEMPLATE_DECL
  6957. || TREE_CODE (fns) == OVERLOAD);
  6958. fn = get_first_fn (fns);
  6959. name = DECL_NAME (fn);
  6960. basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
  6961. gcc_assert (CLASS_TYPE_P (basetype));
  6962. if (processing_template_decl)
  6963. {
  6964. orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
  6965. instance = build_non_dependent_expr (instance);
  6966. if (args != NULL)
  6967. make_args_non_dependent (*args);
  6968. }
  6969. user_args = args == NULL ? NULL : *args;
  6970. /* Under DR 147 A::A() is an invalid constructor call,
  6971. not a functional cast. */
  6972. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
  6973. {
  6974. if (! (complain & tf_error))
  6975. return error_mark_node;
  6976. if (permerror (input_location,
  6977. "cannot call constructor %<%T::%D%> directly",
  6978. basetype, name))
  6979. inform (input_location, "for a function-style cast, remove the "
  6980. "redundant %<::%D%>", name);
  6981. call = build_functional_cast (basetype, build_tree_list_vec (user_args),
  6982. complain);
  6983. return call;
  6984. }
  6985. /* Figure out whether to skip the first argument for the error
  6986. message we will display to users if an error occurs. We don't
  6987. want to display any compiler-generated arguments. The "this"
  6988. pointer hasn't been added yet. However, we must remove the VTT
  6989. pointer if this is a call to a base-class constructor or
  6990. destructor. */
  6991. skip_first_for_error = false;
  6992. if (IDENTIFIER_CTOR_OR_DTOR_P (name))
  6993. {
  6994. /* Callers should explicitly indicate whether they want to construct
  6995. the complete object or just the part without virtual bases. */
  6996. gcc_assert (name != ctor_identifier);
  6997. /* Similarly for destructors. */
  6998. gcc_assert (name != dtor_identifier);
  6999. /* Remove the VTT pointer, if present. */
  7000. if ((name == base_ctor_identifier || name == base_dtor_identifier)
  7001. && CLASSTYPE_VBASECLASSES (basetype))
  7002. skip_first_for_error = true;
  7003. }
  7004. /* Process the argument list. */
  7005. if (args != NULL && *args != NULL)
  7006. {
  7007. *args = resolve_args (*args, complain);
  7008. if (*args == NULL)
  7009. return error_mark_node;
  7010. }
  7011. /* Consider the object argument to be used even if we end up selecting a
  7012. static member function. */
  7013. instance = mark_type_use (instance);
  7014. /* It's OK to call destructors and constructors on cv-qualified objects.
  7015. Therefore, convert the INSTANCE to the unqualified type, if
  7016. necessary. */
  7017. if (DECL_DESTRUCTOR_P (fn)
  7018. || DECL_CONSTRUCTOR_P (fn))
  7019. {
  7020. if (!same_type_p (basetype, TREE_TYPE (instance)))
  7021. {
  7022. instance = build_this (instance);
  7023. instance = build_nop (build_pointer_type (basetype), instance);
  7024. instance = build_fold_indirect_ref (instance);
  7025. }
  7026. }
  7027. if (DECL_DESTRUCTOR_P (fn))
  7028. name = complete_dtor_identifier;
  7029. /* For the overload resolution we need to find the actual `this`
  7030. that would be captured if the call turns out to be to a
  7031. non-static member function. Do not actually capture it at this
  7032. point. */
  7033. if (DECL_CONSTRUCTOR_P (fn))
  7034. /* Constructors don't use the enclosing 'this'. */
  7035. first_mem_arg = instance;
  7036. else
  7037. first_mem_arg = maybe_resolve_dummy (instance, false);
  7038. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  7039. p = conversion_obstack_alloc (0);
  7040. /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
  7041. initializer, not T({ }). */
  7042. if (DECL_CONSTRUCTOR_P (fn) && args != NULL && !vec_safe_is_empty (*args)
  7043. && DIRECT_LIST_INIT_P ((**args)[0]))
  7044. {
  7045. tree init_list = (**args)[0];
  7046. tree init = NULL_TREE;
  7047. gcc_assert ((*args)->length () == 1
  7048. && !(flags & LOOKUP_ONLYCONVERTING));
  7049. /* If the initializer list has no elements and T is a class type with
  7050. a default constructor, the object is value-initialized. Handle
  7051. this here so we don't need to handle it wherever we use
  7052. build_special_member_call. */
  7053. if (CONSTRUCTOR_NELTS (init_list) == 0
  7054. && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
  7055. /* For a user-provided default constructor, use the normal
  7056. mechanisms so that protected access works. */
  7057. && type_has_non_user_provided_default_constructor (basetype)
  7058. && !processing_template_decl)
  7059. init = build_value_init (basetype, complain);
  7060. /* If BASETYPE is an aggregate, we need to do aggregate
  7061. initialization. */
  7062. else if (CP_AGGREGATE_TYPE_P (basetype))
  7063. init = digest_init (basetype, init_list, complain);
  7064. if (init)
  7065. {
  7066. if (is_dummy_object (instance))
  7067. return get_target_expr_sfinae (init, complain);
  7068. init = build2 (INIT_EXPR, TREE_TYPE (instance), instance, init);
  7069. TREE_SIDE_EFFECTS (init) = true;
  7070. return init;
  7071. }
  7072. /* Otherwise go ahead with overload resolution. */
  7073. add_list_candidates (fns, first_mem_arg, init_list,
  7074. basetype, explicit_targs, template_only,
  7075. conversion_path, access_binfo, flags,
  7076. &candidates, complain);
  7077. }
  7078. else
  7079. {
  7080. add_candidates (fns, first_mem_arg, user_args, optype,
  7081. explicit_targs, template_only, conversion_path,
  7082. access_binfo, flags, &candidates, complain);
  7083. }
  7084. any_viable_p = false;
  7085. candidates = splice_viable (candidates, false, &any_viable_p);
  7086. if (!any_viable_p)
  7087. {
  7088. if (complain & tf_error)
  7089. {
  7090. if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
  7091. cxx_incomplete_type_error (instance, basetype);
  7092. else if (optype)
  7093. error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
  7094. basetype, optype, build_tree_list_vec (user_args),
  7095. TREE_TYPE (instance));
  7096. else
  7097. {
  7098. char *pretty_name;
  7099. bool free_p;
  7100. tree arglist;
  7101. pretty_name = name_as_c_string (name, basetype, &free_p);
  7102. arglist = build_tree_list_vec (user_args);
  7103. if (skip_first_for_error)
  7104. arglist = TREE_CHAIN (arglist);
  7105. error ("no matching function for call to %<%T::%s(%A)%#V%>",
  7106. basetype, pretty_name, arglist,
  7107. TREE_TYPE (instance));
  7108. if (free_p)
  7109. free (pretty_name);
  7110. }
  7111. print_z_candidates (location_of (name), candidates);
  7112. }
  7113. call = error_mark_node;
  7114. }
  7115. else
  7116. {
  7117. cand = tourney (candidates, complain);
  7118. if (cand == 0)
  7119. {
  7120. char *pretty_name;
  7121. bool free_p;
  7122. tree arglist;
  7123. if (complain & tf_error)
  7124. {
  7125. pretty_name = name_as_c_string (name, basetype, &free_p);
  7126. arglist = build_tree_list_vec (user_args);
  7127. if (skip_first_for_error)
  7128. arglist = TREE_CHAIN (arglist);
  7129. if (!any_strictly_viable (candidates))
  7130. error ("no matching function for call to %<%s(%A)%>",
  7131. pretty_name, arglist);
  7132. else
  7133. error ("call of overloaded %<%s(%A)%> is ambiguous",
  7134. pretty_name, arglist);
  7135. print_z_candidates (location_of (name), candidates);
  7136. if (free_p)
  7137. free (pretty_name);
  7138. }
  7139. call = error_mark_node;
  7140. }
  7141. else
  7142. {
  7143. fn = cand->fn;
  7144. call = NULL_TREE;
  7145. if (!(flags & LOOKUP_NONVIRTUAL)
  7146. && DECL_PURE_VIRTUAL_P (fn)
  7147. && instance == current_class_ref
  7148. && (complain & tf_warning))
  7149. {
  7150. /* This is not an error, it is runtime undefined
  7151. behavior. */
  7152. if (!current_function_decl)
  7153. warning (0, "pure virtual %q#D called from "
  7154. "non-static data member initializer", fn);
  7155. else if (DECL_CONSTRUCTOR_P (current_function_decl)
  7156. || DECL_DESTRUCTOR_P (current_function_decl))
  7157. warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
  7158. ? "pure virtual %q#D called from constructor"
  7159. : "pure virtual %q#D called from destructor"),
  7160. fn);
  7161. }
  7162. if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
  7163. && !DECL_CONSTRUCTOR_P (fn)
  7164. && is_dummy_object (instance))
  7165. {
  7166. instance = maybe_resolve_dummy (instance, true);
  7167. if (instance == error_mark_node)
  7168. call = error_mark_node;
  7169. else if (!is_dummy_object (instance))
  7170. {
  7171. /* We captured 'this' in the current lambda now that
  7172. we know we really need it. */
  7173. cand->first_arg = instance;
  7174. }
  7175. else
  7176. {
  7177. if (complain & tf_error)
  7178. error ("cannot call member function %qD without object",
  7179. fn);
  7180. call = error_mark_node;
  7181. }
  7182. }
  7183. if (call != error_mark_node)
  7184. {
  7185. /* Optimize away vtable lookup if we know that this
  7186. function can't be overridden. We need to check if
  7187. the context and the type where we found fn are the same,
  7188. actually FN might be defined in a different class
  7189. type because of a using-declaration. In this case, we
  7190. do not want to perform a non-virtual call. */
  7191. if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
  7192. && same_type_ignoring_top_level_qualifiers_p
  7193. (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
  7194. && resolves_to_fixed_type_p (instance, 0))
  7195. flags |= LOOKUP_NONVIRTUAL;
  7196. if (explicit_targs)
  7197. flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
  7198. /* Now we know what function is being called. */
  7199. if (fn_p)
  7200. *fn_p = fn;
  7201. /* Build the actual CALL_EXPR. */
  7202. call = build_over_call (cand, flags, complain);
  7203. /* In an expression of the form `a->f()' where `f' turns
  7204. out to be a static member function, `a' is
  7205. none-the-less evaluated. */
  7206. if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
  7207. && !is_dummy_object (instance)
  7208. && TREE_SIDE_EFFECTS (instance))
  7209. call = build2 (COMPOUND_EXPR, TREE_TYPE (call),
  7210. instance, call);
  7211. else if (call != error_mark_node
  7212. && DECL_DESTRUCTOR_P (cand->fn)
  7213. && !VOID_TYPE_P (TREE_TYPE (call)))
  7214. /* An explicit call of the form "x->~X()" has type
  7215. "void". However, on platforms where destructors
  7216. return "this" (i.e., those where
  7217. targetm.cxx.cdtor_returns_this is true), such calls
  7218. will appear to have a return value of pointer type
  7219. to the low-level call machinery. We do not want to
  7220. change the low-level machinery, since we want to be
  7221. able to optimize "delete f()" on such platforms as
  7222. "operator delete(~X(f()))" (rather than generating
  7223. "t = f(), ~X(t), operator delete (t)"). */
  7224. call = build_nop (void_type_node, call);
  7225. }
  7226. }
  7227. }
  7228. if (processing_template_decl && call != error_mark_node)
  7229. {
  7230. bool cast_to_void = false;
  7231. if (TREE_CODE (call) == COMPOUND_EXPR)
  7232. call = TREE_OPERAND (call, 1);
  7233. else if (TREE_CODE (call) == NOP_EXPR)
  7234. {
  7235. cast_to_void = true;
  7236. call = TREE_OPERAND (call, 0);
  7237. }
  7238. if (INDIRECT_REF_P (call))
  7239. call = TREE_OPERAND (call, 0);
  7240. call = (build_min_non_dep_call_vec
  7241. (call,
  7242. build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
  7243. orig_instance, orig_fns, NULL_TREE),
  7244. orig_args));
  7245. SET_EXPR_LOCATION (call, input_location);
  7246. call = convert_from_reference (call);
  7247. if (cast_to_void)
  7248. call = build_nop (void_type_node, call);
  7249. }
  7250. /* Free all the conversions we allocated. */
  7251. obstack_free (&conversion_obstack, p);
  7252. if (orig_args != NULL)
  7253. release_tree_vector (orig_args);
  7254. return call;
  7255. }
  7256. /* Wrapper for above. */
  7257. tree
  7258. build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
  7259. tree conversion_path, int flags,
  7260. tree *fn_p, tsubst_flags_t complain)
  7261. {
  7262. tree ret;
  7263. bool subtime = timevar_cond_start (TV_OVERLOAD);
  7264. ret = build_new_method_call_1 (instance, fns, args, conversion_path, flags,
  7265. fn_p, complain);
  7266. timevar_cond_stop (TV_OVERLOAD, subtime);
  7267. return ret;
  7268. }
  7269. /* Returns true iff standard conversion sequence ICS1 is a proper
  7270. subsequence of ICS2. */
  7271. static bool
  7272. is_subseq (conversion *ics1, conversion *ics2)
  7273. {
  7274. /* We can assume that a conversion of the same code
  7275. between the same types indicates a subsequence since we only get
  7276. here if the types we are converting from are the same. */
  7277. while (ics1->kind == ck_rvalue
  7278. || ics1->kind == ck_lvalue)
  7279. ics1 = next_conversion (ics1);
  7280. while (1)
  7281. {
  7282. while (ics2->kind == ck_rvalue
  7283. || ics2->kind == ck_lvalue)
  7284. ics2 = next_conversion (ics2);
  7285. if (ics2->kind == ck_user
  7286. || ics2->kind == ck_ambig
  7287. || ics2->kind == ck_aggr
  7288. || ics2->kind == ck_list
  7289. || ics2->kind == ck_identity)
  7290. /* At this point, ICS1 cannot be a proper subsequence of
  7291. ICS2. We can get a USER_CONV when we are comparing the
  7292. second standard conversion sequence of two user conversion
  7293. sequences. */
  7294. return false;
  7295. ics2 = next_conversion (ics2);
  7296. if (ics2->kind == ics1->kind
  7297. && same_type_p (ics2->type, ics1->type)
  7298. && same_type_p (next_conversion (ics2)->type,
  7299. next_conversion (ics1)->type))
  7300. return true;
  7301. }
  7302. }
  7303. /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
  7304. be any _TYPE nodes. */
  7305. bool
  7306. is_properly_derived_from (tree derived, tree base)
  7307. {
  7308. if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
  7309. return false;
  7310. /* We only allow proper derivation here. The DERIVED_FROM_P macro
  7311. considers every class derived from itself. */
  7312. return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
  7313. && DERIVED_FROM_P (base, derived));
  7314. }
  7315. /* We build the ICS for an implicit object parameter as a pointer
  7316. conversion sequence. However, such a sequence should be compared
  7317. as if it were a reference conversion sequence. If ICS is the
  7318. implicit conversion sequence for an implicit object parameter,
  7319. modify it accordingly. */
  7320. static void
  7321. maybe_handle_implicit_object (conversion **ics)
  7322. {
  7323. if ((*ics)->this_p)
  7324. {
  7325. /* [over.match.funcs]
  7326. For non-static member functions, the type of the
  7327. implicit object parameter is "reference to cv X"
  7328. where X is the class of which the function is a
  7329. member and cv is the cv-qualification on the member
  7330. function declaration. */
  7331. conversion *t = *ics;
  7332. tree reference_type;
  7333. /* The `this' parameter is a pointer to a class type. Make the
  7334. implicit conversion talk about a reference to that same class
  7335. type. */
  7336. reference_type = TREE_TYPE (t->type);
  7337. reference_type = build_reference_type (reference_type);
  7338. if (t->kind == ck_qual)
  7339. t = next_conversion (t);
  7340. if (t->kind == ck_ptr)
  7341. t = next_conversion (t);
  7342. t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
  7343. t = direct_reference_binding (reference_type, t);
  7344. t->this_p = 1;
  7345. t->rvaluedness_matches_p = 0;
  7346. *ics = t;
  7347. }
  7348. }
  7349. /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
  7350. and return the initial reference binding conversion. Otherwise,
  7351. leave *ICS unchanged and return NULL. */
  7352. static conversion *
  7353. maybe_handle_ref_bind (conversion **ics)
  7354. {
  7355. if ((*ics)->kind == ck_ref_bind)
  7356. {
  7357. conversion *old_ics = *ics;
  7358. *ics = next_conversion (old_ics);
  7359. (*ics)->user_conv_p = old_ics->user_conv_p;
  7360. return old_ics;
  7361. }
  7362. return NULL;
  7363. }
  7364. /* Compare two implicit conversion sequences according to the rules set out in
  7365. [over.ics.rank]. Return values:
  7366. 1: ics1 is better than ics2
  7367. -1: ics2 is better than ics1
  7368. 0: ics1 and ics2 are indistinguishable */
  7369. static int
  7370. compare_ics (conversion *ics1, conversion *ics2)
  7371. {
  7372. tree from_type1;
  7373. tree from_type2;
  7374. tree to_type1;
  7375. tree to_type2;
  7376. tree deref_from_type1 = NULL_TREE;
  7377. tree deref_from_type2 = NULL_TREE;
  7378. tree deref_to_type1 = NULL_TREE;
  7379. tree deref_to_type2 = NULL_TREE;
  7380. conversion_rank rank1, rank2;
  7381. /* REF_BINDING is nonzero if the result of the conversion sequence
  7382. is a reference type. In that case REF_CONV is the reference
  7383. binding conversion. */
  7384. conversion *ref_conv1;
  7385. conversion *ref_conv2;
  7386. /* Compare badness before stripping the reference conversion. */
  7387. if (ics1->bad_p > ics2->bad_p)
  7388. return -1;
  7389. else if (ics1->bad_p < ics2->bad_p)
  7390. return 1;
  7391. /* Handle implicit object parameters. */
  7392. maybe_handle_implicit_object (&ics1);
  7393. maybe_handle_implicit_object (&ics2);
  7394. /* Handle reference parameters. */
  7395. ref_conv1 = maybe_handle_ref_bind (&ics1);
  7396. ref_conv2 = maybe_handle_ref_bind (&ics2);
  7397. /* List-initialization sequence L1 is a better conversion sequence than
  7398. list-initialization sequence L2 if L1 converts to
  7399. std::initializer_list<X> for some X and L2 does not. */
  7400. if (ics1->kind == ck_list && ics2->kind != ck_list)
  7401. return 1;
  7402. if (ics2->kind == ck_list && ics1->kind != ck_list)
  7403. return -1;
  7404. /* [over.ics.rank]
  7405. When comparing the basic forms of implicit conversion sequences (as
  7406. defined in _over.best.ics_)
  7407. --a standard conversion sequence (_over.ics.scs_) is a better
  7408. conversion sequence than a user-defined conversion sequence
  7409. or an ellipsis conversion sequence, and
  7410. --a user-defined conversion sequence (_over.ics.user_) is a
  7411. better conversion sequence than an ellipsis conversion sequence
  7412. (_over.ics.ellipsis_). */
  7413. /* Use BAD_CONVERSION_RANK because we already checked for a badness
  7414. mismatch. If both ICS are bad, we try to make a decision based on
  7415. what would have happened if they'd been good. This is not an
  7416. extension, we'll still give an error when we build up the call; this
  7417. just helps us give a more helpful error message. */
  7418. rank1 = BAD_CONVERSION_RANK (ics1);
  7419. rank2 = BAD_CONVERSION_RANK (ics2);
  7420. if (rank1 > rank2)
  7421. return -1;
  7422. else if (rank1 < rank2)
  7423. return 1;
  7424. if (ics1->ellipsis_p)
  7425. /* Both conversions are ellipsis conversions. */
  7426. return 0;
  7427. /* User-defined conversion sequence U1 is a better conversion sequence
  7428. than another user-defined conversion sequence U2 if they contain the
  7429. same user-defined conversion operator or constructor and if the sec-
  7430. ond standard conversion sequence of U1 is better than the second
  7431. standard conversion sequence of U2. */
  7432. /* Handle list-conversion with the same code even though it isn't always
  7433. ranked as a user-defined conversion and it doesn't have a second
  7434. standard conversion sequence; it will still have the desired effect.
  7435. Specifically, we need to do the reference binding comparison at the
  7436. end of this function. */
  7437. if (ics1->user_conv_p || ics1->kind == ck_list || ics1->kind == ck_aggr)
  7438. {
  7439. conversion *t1;
  7440. conversion *t2;
  7441. for (t1 = ics1; t1->kind != ck_user; t1 = next_conversion (t1))
  7442. if (t1->kind == ck_ambig || t1->kind == ck_aggr
  7443. || t1->kind == ck_list)
  7444. break;
  7445. for (t2 = ics2; t2->kind != ck_user; t2 = next_conversion (t2))
  7446. if (t2->kind == ck_ambig || t2->kind == ck_aggr
  7447. || t2->kind == ck_list)
  7448. break;
  7449. if (t1->kind != t2->kind)
  7450. return 0;
  7451. else if (t1->kind == ck_user)
  7452. {
  7453. if (t1->cand->fn != t2->cand->fn)
  7454. return 0;
  7455. }
  7456. else
  7457. {
  7458. /* For ambiguous or aggregate conversions, use the target type as
  7459. a proxy for the conversion function. */
  7460. if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
  7461. return 0;
  7462. }
  7463. /* We can just fall through here, after setting up
  7464. FROM_TYPE1 and FROM_TYPE2. */
  7465. from_type1 = t1->type;
  7466. from_type2 = t2->type;
  7467. }
  7468. else
  7469. {
  7470. conversion *t1;
  7471. conversion *t2;
  7472. /* We're dealing with two standard conversion sequences.
  7473. [over.ics.rank]
  7474. Standard conversion sequence S1 is a better conversion
  7475. sequence than standard conversion sequence S2 if
  7476. --S1 is a proper subsequence of S2 (comparing the conversion
  7477. sequences in the canonical form defined by _over.ics.scs_,
  7478. excluding any Lvalue Transformation; the identity
  7479. conversion sequence is considered to be a subsequence of
  7480. any non-identity conversion sequence */
  7481. t1 = ics1;
  7482. while (t1->kind != ck_identity)
  7483. t1 = next_conversion (t1);
  7484. from_type1 = t1->type;
  7485. t2 = ics2;
  7486. while (t2->kind != ck_identity)
  7487. t2 = next_conversion (t2);
  7488. from_type2 = t2->type;
  7489. }
  7490. /* One sequence can only be a subsequence of the other if they start with
  7491. the same type. They can start with different types when comparing the
  7492. second standard conversion sequence in two user-defined conversion
  7493. sequences. */
  7494. if (same_type_p (from_type1, from_type2))
  7495. {
  7496. if (is_subseq (ics1, ics2))
  7497. return 1;
  7498. if (is_subseq (ics2, ics1))
  7499. return -1;
  7500. }
  7501. /* [over.ics.rank]
  7502. Or, if not that,
  7503. --the rank of S1 is better than the rank of S2 (by the rules
  7504. defined below):
  7505. Standard conversion sequences are ordered by their ranks: an Exact
  7506. Match is a better conversion than a Promotion, which is a better
  7507. conversion than a Conversion.
  7508. Two conversion sequences with the same rank are indistinguishable
  7509. unless one of the following rules applies:
  7510. --A conversion that does not a convert a pointer, pointer to member,
  7511. or std::nullptr_t to bool is better than one that does.
  7512. The ICS_STD_RANK automatically handles the pointer-to-bool rule,
  7513. so that we do not have to check it explicitly. */
  7514. if (ics1->rank < ics2->rank)
  7515. return 1;
  7516. else if (ics2->rank < ics1->rank)
  7517. return -1;
  7518. to_type1 = ics1->type;
  7519. to_type2 = ics2->type;
  7520. /* A conversion from scalar arithmetic type to complex is worse than a
  7521. conversion between scalar arithmetic types. */
  7522. if (same_type_p (from_type1, from_type2)
  7523. && ARITHMETIC_TYPE_P (from_type1)
  7524. && ARITHMETIC_TYPE_P (to_type1)
  7525. && ARITHMETIC_TYPE_P (to_type2)
  7526. && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
  7527. != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
  7528. {
  7529. if (TREE_CODE (to_type1) == COMPLEX_TYPE)
  7530. return -1;
  7531. else
  7532. return 1;
  7533. }
  7534. if (TYPE_PTR_P (from_type1)
  7535. && TYPE_PTR_P (from_type2)
  7536. && TYPE_PTR_P (to_type1)
  7537. && TYPE_PTR_P (to_type2))
  7538. {
  7539. deref_from_type1 = TREE_TYPE (from_type1);
  7540. deref_from_type2 = TREE_TYPE (from_type2);
  7541. deref_to_type1 = TREE_TYPE (to_type1);
  7542. deref_to_type2 = TREE_TYPE (to_type2);
  7543. }
  7544. /* The rules for pointers to members A::* are just like the rules
  7545. for pointers A*, except opposite: if B is derived from A then
  7546. A::* converts to B::*, not vice versa. For that reason, we
  7547. switch the from_ and to_ variables here. */
  7548. else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
  7549. && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
  7550. || (TYPE_PTRMEMFUNC_P (from_type1)
  7551. && TYPE_PTRMEMFUNC_P (from_type2)
  7552. && TYPE_PTRMEMFUNC_P (to_type1)
  7553. && TYPE_PTRMEMFUNC_P (to_type2)))
  7554. {
  7555. deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
  7556. deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
  7557. deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
  7558. deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
  7559. }
  7560. if (deref_from_type1 != NULL_TREE
  7561. && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
  7562. && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
  7563. {
  7564. /* This was one of the pointer or pointer-like conversions.
  7565. [over.ics.rank]
  7566. --If class B is derived directly or indirectly from class A,
  7567. conversion of B* to A* is better than conversion of B* to
  7568. void*, and conversion of A* to void* is better than
  7569. conversion of B* to void*. */
  7570. if (VOID_TYPE_P (deref_to_type1)
  7571. && VOID_TYPE_P (deref_to_type2))
  7572. {
  7573. if (is_properly_derived_from (deref_from_type1,
  7574. deref_from_type2))
  7575. return -1;
  7576. else if (is_properly_derived_from (deref_from_type2,
  7577. deref_from_type1))
  7578. return 1;
  7579. }
  7580. else if (VOID_TYPE_P (deref_to_type1)
  7581. || VOID_TYPE_P (deref_to_type2))
  7582. {
  7583. if (same_type_p (deref_from_type1, deref_from_type2))
  7584. {
  7585. if (VOID_TYPE_P (deref_to_type2))
  7586. {
  7587. if (is_properly_derived_from (deref_from_type1,
  7588. deref_to_type1))
  7589. return 1;
  7590. }
  7591. /* We know that DEREF_TO_TYPE1 is `void' here. */
  7592. else if (is_properly_derived_from (deref_from_type1,
  7593. deref_to_type2))
  7594. return -1;
  7595. }
  7596. }
  7597. else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
  7598. && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
  7599. {
  7600. /* [over.ics.rank]
  7601. --If class B is derived directly or indirectly from class A
  7602. and class C is derived directly or indirectly from B,
  7603. --conversion of C* to B* is better than conversion of C* to
  7604. A*,
  7605. --conversion of B* to A* is better than conversion of C* to
  7606. A* */
  7607. if (same_type_p (deref_from_type1, deref_from_type2))
  7608. {
  7609. if (is_properly_derived_from (deref_to_type1,
  7610. deref_to_type2))
  7611. return 1;
  7612. else if (is_properly_derived_from (deref_to_type2,
  7613. deref_to_type1))
  7614. return -1;
  7615. }
  7616. else if (same_type_p (deref_to_type1, deref_to_type2))
  7617. {
  7618. if (is_properly_derived_from (deref_from_type2,
  7619. deref_from_type1))
  7620. return 1;
  7621. else if (is_properly_derived_from (deref_from_type1,
  7622. deref_from_type2))
  7623. return -1;
  7624. }
  7625. }
  7626. }
  7627. else if (CLASS_TYPE_P (non_reference (from_type1))
  7628. && same_type_p (from_type1, from_type2))
  7629. {
  7630. tree from = non_reference (from_type1);
  7631. /* [over.ics.rank]
  7632. --binding of an expression of type C to a reference of type
  7633. B& is better than binding an expression of type C to a
  7634. reference of type A&
  7635. --conversion of C to B is better than conversion of C to A, */
  7636. if (is_properly_derived_from (from, to_type1)
  7637. && is_properly_derived_from (from, to_type2))
  7638. {
  7639. if (is_properly_derived_from (to_type1, to_type2))
  7640. return 1;
  7641. else if (is_properly_derived_from (to_type2, to_type1))
  7642. return -1;
  7643. }
  7644. }
  7645. else if (CLASS_TYPE_P (non_reference (to_type1))
  7646. && same_type_p (to_type1, to_type2))
  7647. {
  7648. tree to = non_reference (to_type1);
  7649. /* [over.ics.rank]
  7650. --binding of an expression of type B to a reference of type
  7651. A& is better than binding an expression of type C to a
  7652. reference of type A&,
  7653. --conversion of B to A is better than conversion of C to A */
  7654. if (is_properly_derived_from (from_type1, to)
  7655. && is_properly_derived_from (from_type2, to))
  7656. {
  7657. if (is_properly_derived_from (from_type2, from_type1))
  7658. return 1;
  7659. else if (is_properly_derived_from (from_type1, from_type2))
  7660. return -1;
  7661. }
  7662. }
  7663. /* [over.ics.rank]
  7664. --S1 and S2 differ only in their qualification conversion and yield
  7665. similar types T1 and T2 (_conv.qual_), respectively, and the cv-
  7666. qualification signature of type T1 is a proper subset of the cv-
  7667. qualification signature of type T2 */
  7668. if (ics1->kind == ck_qual
  7669. && ics2->kind == ck_qual
  7670. && same_type_p (from_type1, from_type2))
  7671. {
  7672. int result = comp_cv_qual_signature (to_type1, to_type2);
  7673. if (result != 0)
  7674. return result;
  7675. }
  7676. /* [over.ics.rank]
  7677. --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
  7678. to an implicit object parameter of a non-static member function
  7679. declared without a ref-qualifier, and either S1 binds an lvalue
  7680. reference to an lvalue and S2 binds an rvalue reference or S1 binds an
  7681. rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
  7682. draft standard, 13.3.3.2)
  7683. --S1 and S2 are reference bindings (_dcl.init.ref_), and the
  7684. types to which the references refer are the same type except for
  7685. top-level cv-qualifiers, and the type to which the reference
  7686. initialized by S2 refers is more cv-qualified than the type to
  7687. which the reference initialized by S1 refers.
  7688. DR 1328 [over.match.best]: the context is an initialization by
  7689. conversion function for direct reference binding (13.3.1.6) of a
  7690. reference to function type, the return type of F1 is the same kind of
  7691. reference (i.e. lvalue or rvalue) as the reference being initialized,
  7692. and the return type of F2 is not. */
  7693. if (ref_conv1 && ref_conv2)
  7694. {
  7695. if (!ref_conv1->this_p && !ref_conv2->this_p
  7696. && (ref_conv1->rvaluedness_matches_p
  7697. != ref_conv2->rvaluedness_matches_p)
  7698. && (same_type_p (ref_conv1->type, ref_conv2->type)
  7699. || (TYPE_REF_IS_RVALUE (ref_conv1->type)
  7700. != TYPE_REF_IS_RVALUE (ref_conv2->type))))
  7701. {
  7702. if (ref_conv1->bad_p
  7703. && !same_type_p (TREE_TYPE (ref_conv1->type),
  7704. TREE_TYPE (ref_conv2->type)))
  7705. /* Don't prefer a bad conversion that drops cv-quals to a bad
  7706. conversion with the wrong rvalueness. */
  7707. return 0;
  7708. return (ref_conv1->rvaluedness_matches_p
  7709. - ref_conv2->rvaluedness_matches_p);
  7710. }
  7711. if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
  7712. {
  7713. int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
  7714. int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
  7715. if (ref_conv1->bad_p)
  7716. {
  7717. /* Prefer the one that drops fewer cv-quals. */
  7718. tree ftype = next_conversion (ref_conv1)->type;
  7719. int fquals = cp_type_quals (ftype);
  7720. q1 ^= fquals;
  7721. q2 ^= fquals;
  7722. }
  7723. return comp_cv_qualification (q2, q1);
  7724. }
  7725. }
  7726. /* Neither conversion sequence is better than the other. */
  7727. return 0;
  7728. }
  7729. /* The source type for this standard conversion sequence. */
  7730. static tree
  7731. source_type (conversion *t)
  7732. {
  7733. for (;; t = next_conversion (t))
  7734. {
  7735. if (t->kind == ck_user
  7736. || t->kind == ck_ambig
  7737. || t->kind == ck_identity)
  7738. return t->type;
  7739. }
  7740. gcc_unreachable ();
  7741. }
  7742. /* Note a warning about preferring WINNER to LOSER. We do this by storing
  7743. a pointer to LOSER and re-running joust to produce the warning if WINNER
  7744. is actually used. */
  7745. static void
  7746. add_warning (struct z_candidate *winner, struct z_candidate *loser)
  7747. {
  7748. candidate_warning *cw = (candidate_warning *)
  7749. conversion_obstack_alloc (sizeof (candidate_warning));
  7750. cw->loser = loser;
  7751. cw->next = winner->warnings;
  7752. winner->warnings = cw;
  7753. }
  7754. /* Compare two candidates for overloading as described in
  7755. [over.match.best]. Return values:
  7756. 1: cand1 is better than cand2
  7757. -1: cand2 is better than cand1
  7758. 0: cand1 and cand2 are indistinguishable */
  7759. static int
  7760. joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
  7761. tsubst_flags_t complain)
  7762. {
  7763. int winner = 0;
  7764. int off1 = 0, off2 = 0;
  7765. size_t i;
  7766. size_t len;
  7767. /* Candidates that involve bad conversions are always worse than those
  7768. that don't. */
  7769. if (cand1->viable > cand2->viable)
  7770. return 1;
  7771. if (cand1->viable < cand2->viable)
  7772. return -1;
  7773. /* If we have two pseudo-candidates for conversions to the same type,
  7774. or two candidates for the same function, arbitrarily pick one. */
  7775. if (cand1->fn == cand2->fn
  7776. && (IS_TYPE_OR_DECL_P (cand1->fn)))
  7777. return 1;
  7778. /* Prefer a non-deleted function over an implicitly deleted move
  7779. constructor or assignment operator. This differs slightly from the
  7780. wording for issue 1402 (which says the move op is ignored by overload
  7781. resolution), but this way produces better error messages. */
  7782. if (TREE_CODE (cand1->fn) == FUNCTION_DECL
  7783. && TREE_CODE (cand2->fn) == FUNCTION_DECL
  7784. && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
  7785. {
  7786. if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
  7787. && move_fn_p (cand1->fn))
  7788. return -1;
  7789. if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
  7790. && move_fn_p (cand2->fn))
  7791. return 1;
  7792. }
  7793. /* a viable function F1
  7794. is defined to be a better function than another viable function F2 if
  7795. for all arguments i, ICSi(F1) is not a worse conversion sequence than
  7796. ICSi(F2), and then */
  7797. /* for some argument j, ICSj(F1) is a better conversion sequence than
  7798. ICSj(F2) */
  7799. /* For comparing static and non-static member functions, we ignore
  7800. the implicit object parameter of the non-static function. The
  7801. standard says to pretend that the static function has an object
  7802. parm, but that won't work with operator overloading. */
  7803. len = cand1->num_convs;
  7804. if (len != cand2->num_convs)
  7805. {
  7806. int static_1 = DECL_STATIC_FUNCTION_P (cand1->fn);
  7807. int static_2 = DECL_STATIC_FUNCTION_P (cand2->fn);
  7808. if (DECL_CONSTRUCTOR_P (cand1->fn)
  7809. && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
  7810. /* We're comparing a near-match list constructor and a near-match
  7811. non-list constructor. Just treat them as unordered. */
  7812. return 0;
  7813. gcc_assert (static_1 != static_2);
  7814. if (static_1)
  7815. off2 = 1;
  7816. else
  7817. {
  7818. off1 = 1;
  7819. --len;
  7820. }
  7821. }
  7822. for (i = 0; i < len; ++i)
  7823. {
  7824. conversion *t1 = cand1->convs[i + off1];
  7825. conversion *t2 = cand2->convs[i + off2];
  7826. int comp = compare_ics (t1, t2);
  7827. if (comp != 0)
  7828. {
  7829. if ((complain & tf_warning)
  7830. && warn_sign_promo
  7831. && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
  7832. == cr_std + cr_promotion)
  7833. && t1->kind == ck_std
  7834. && t2->kind == ck_std
  7835. && TREE_CODE (t1->type) == INTEGER_TYPE
  7836. && TREE_CODE (t2->type) == INTEGER_TYPE
  7837. && (TYPE_PRECISION (t1->type)
  7838. == TYPE_PRECISION (t2->type))
  7839. && (TYPE_UNSIGNED (next_conversion (t1)->type)
  7840. || (TREE_CODE (next_conversion (t1)->type)
  7841. == ENUMERAL_TYPE)))
  7842. {
  7843. tree type = next_conversion (t1)->type;
  7844. tree type1, type2;
  7845. struct z_candidate *w, *l;
  7846. if (comp > 0)
  7847. type1 = t1->type, type2 = t2->type,
  7848. w = cand1, l = cand2;
  7849. else
  7850. type1 = t2->type, type2 = t1->type,
  7851. w = cand2, l = cand1;
  7852. if (warn)
  7853. {
  7854. warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
  7855. type, type1, type2);
  7856. warning (OPT_Wsign_promo, " in call to %qD", w->fn);
  7857. }
  7858. else
  7859. add_warning (w, l);
  7860. }
  7861. if (winner && comp != winner)
  7862. {
  7863. winner = 0;
  7864. goto tweak;
  7865. }
  7866. winner = comp;
  7867. }
  7868. }
  7869. /* warn about confusing overload resolution for user-defined conversions,
  7870. either between a constructor and a conversion op, or between two
  7871. conversion ops. */
  7872. if ((complain & tf_warning)
  7873. && winner && warn_conversion && cand1->second_conv
  7874. && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
  7875. && winner != compare_ics (cand1->second_conv, cand2->second_conv))
  7876. {
  7877. struct z_candidate *w, *l;
  7878. bool give_warning = false;
  7879. if (winner == 1)
  7880. w = cand1, l = cand2;
  7881. else
  7882. w = cand2, l = cand1;
  7883. /* We don't want to complain about `X::operator T1 ()'
  7884. beating `X::operator T2 () const', when T2 is a no less
  7885. cv-qualified version of T1. */
  7886. if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
  7887. && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
  7888. {
  7889. tree t = TREE_TYPE (TREE_TYPE (l->fn));
  7890. tree f = TREE_TYPE (TREE_TYPE (w->fn));
  7891. if (TREE_CODE (t) == TREE_CODE (f) && POINTER_TYPE_P (t))
  7892. {
  7893. t = TREE_TYPE (t);
  7894. f = TREE_TYPE (f);
  7895. }
  7896. if (!comp_ptr_ttypes (t, f))
  7897. give_warning = true;
  7898. }
  7899. else
  7900. give_warning = true;
  7901. if (!give_warning)
  7902. /*NOP*/;
  7903. else if (warn)
  7904. {
  7905. tree source = source_type (w->convs[0]);
  7906. if (! DECL_CONSTRUCTOR_P (w->fn))
  7907. source = TREE_TYPE (source);
  7908. if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
  7909. && warning (OPT_Wconversion, " for conversion from %qT to %qT",
  7910. source, w->second_conv->type))
  7911. {
  7912. inform (input_location, " because conversion sequence for the argument is better");
  7913. }
  7914. }
  7915. else
  7916. add_warning (w, l);
  7917. }
  7918. if (winner)
  7919. return winner;
  7920. /* DR 495 moved this tiebreaker above the template ones. */
  7921. /* or, if not that,
  7922. the context is an initialization by user-defined conversion (see
  7923. _dcl.init_ and _over.match.user_) and the standard conversion
  7924. sequence from the return type of F1 to the destination type (i.e.,
  7925. the type of the entity being initialized) is a better conversion
  7926. sequence than the standard conversion sequence from the return type
  7927. of F2 to the destination type. */
  7928. if (cand1->second_conv)
  7929. {
  7930. winner = compare_ics (cand1->second_conv, cand2->second_conv);
  7931. if (winner)
  7932. return winner;
  7933. }
  7934. /* or, if not that,
  7935. F1 is a non-template function and F2 is a template function
  7936. specialization. */
  7937. if (!cand1->template_decl && cand2->template_decl)
  7938. return 1;
  7939. else if (cand1->template_decl && !cand2->template_decl)
  7940. return -1;
  7941. /* or, if not that,
  7942. F1 and F2 are template functions and the function template for F1 is
  7943. more specialized than the template for F2 according to the partial
  7944. ordering rules. */
  7945. if (cand1->template_decl && cand2->template_decl)
  7946. {
  7947. winner = more_specialized_fn
  7948. (TI_TEMPLATE (cand1->template_decl),
  7949. TI_TEMPLATE (cand2->template_decl),
  7950. /* [temp.func.order]: The presence of unused ellipsis and default
  7951. arguments has no effect on the partial ordering of function
  7952. templates. add_function_candidate() will not have
  7953. counted the "this" argument for constructors. */
  7954. cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
  7955. if (winner)
  7956. return winner;
  7957. }
  7958. /* Check whether we can discard a builtin candidate, either because we
  7959. have two identical ones or matching builtin and non-builtin candidates.
  7960. (Pedantically in the latter case the builtin which matched the user
  7961. function should not be added to the overload set, but we spot it here.
  7962. [over.match.oper]
  7963. ... the builtin candidates include ...
  7964. - do not have the same parameter type list as any non-template
  7965. non-member candidate. */
  7966. if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
  7967. {
  7968. for (i = 0; i < len; ++i)
  7969. if (!same_type_p (cand1->convs[i]->type,
  7970. cand2->convs[i]->type))
  7971. break;
  7972. if (i == cand1->num_convs)
  7973. {
  7974. if (cand1->fn == cand2->fn)
  7975. /* Two built-in candidates; arbitrarily pick one. */
  7976. return 1;
  7977. else if (identifier_p (cand1->fn))
  7978. /* cand1 is built-in; prefer cand2. */
  7979. return -1;
  7980. else
  7981. /* cand2 is built-in; prefer cand1. */
  7982. return 1;
  7983. }
  7984. }
  7985. /* For candidates of a multi-versioned function, make the version with
  7986. the highest priority win. This version will be checked for dispatching
  7987. first. If this version can be inlined into the caller, the front-end
  7988. will simply make a direct call to this function. */
  7989. if (TREE_CODE (cand1->fn) == FUNCTION_DECL
  7990. && DECL_FUNCTION_VERSIONED (cand1->fn)
  7991. && TREE_CODE (cand2->fn) == FUNCTION_DECL
  7992. && DECL_FUNCTION_VERSIONED (cand2->fn))
  7993. {
  7994. tree f1 = TREE_TYPE (cand1->fn);
  7995. tree f2 = TREE_TYPE (cand2->fn);
  7996. tree p1 = TYPE_ARG_TYPES (f1);
  7997. tree p2 = TYPE_ARG_TYPES (f2);
  7998. /* Check if cand1->fn and cand2->fn are versions of the same function. It
  7999. is possible that cand1->fn and cand2->fn are function versions but of
  8000. different functions. Check types to see if they are versions of the same
  8001. function. */
  8002. if (compparms (p1, p2)
  8003. && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
  8004. {
  8005. /* Always make the version with the higher priority, more
  8006. specialized, win. */
  8007. gcc_assert (targetm.compare_version_priority);
  8008. if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
  8009. return 1;
  8010. else
  8011. return -1;
  8012. }
  8013. }
  8014. /* If the two function declarations represent the same function (this can
  8015. happen with declarations in multiple scopes and arg-dependent lookup),
  8016. arbitrarily choose one. But first make sure the default args we're
  8017. using match. */
  8018. if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
  8019. && equal_functions (cand1->fn, cand2->fn))
  8020. {
  8021. tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
  8022. tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
  8023. gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
  8024. for (i = 0; i < len; ++i)
  8025. {
  8026. /* Don't crash if the fn is variadic. */
  8027. if (!parms1)
  8028. break;
  8029. parms1 = TREE_CHAIN (parms1);
  8030. parms2 = TREE_CHAIN (parms2);
  8031. }
  8032. if (off1)
  8033. parms1 = TREE_CHAIN (parms1);
  8034. else if (off2)
  8035. parms2 = TREE_CHAIN (parms2);
  8036. for (; parms1; ++i)
  8037. {
  8038. if (!cp_tree_equal (TREE_PURPOSE (parms1),
  8039. TREE_PURPOSE (parms2)))
  8040. {
  8041. if (warn)
  8042. {
  8043. if (complain & tf_error)
  8044. {
  8045. if (permerror (input_location,
  8046. "default argument mismatch in "
  8047. "overload resolution"))
  8048. {
  8049. inform (input_location,
  8050. " candidate 1: %q+#F", cand1->fn);
  8051. inform (input_location,
  8052. " candidate 2: %q+#F", cand2->fn);
  8053. }
  8054. }
  8055. else
  8056. return 0;
  8057. }
  8058. else
  8059. add_warning (cand1, cand2);
  8060. break;
  8061. }
  8062. parms1 = TREE_CHAIN (parms1);
  8063. parms2 = TREE_CHAIN (parms2);
  8064. }
  8065. return 1;
  8066. }
  8067. tweak:
  8068. /* Extension: If the worst conversion for one candidate is worse than the
  8069. worst conversion for the other, take the first. */
  8070. if (!pedantic && (complain & tf_warning_or_error))
  8071. {
  8072. conversion_rank rank1 = cr_identity, rank2 = cr_identity;
  8073. struct z_candidate *w = 0, *l = 0;
  8074. for (i = 0; i < len; ++i)
  8075. {
  8076. if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
  8077. rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
  8078. if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
  8079. rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
  8080. }
  8081. if (rank1 < rank2)
  8082. winner = 1, w = cand1, l = cand2;
  8083. if (rank1 > rank2)
  8084. winner = -1, w = cand2, l = cand1;
  8085. if (winner)
  8086. {
  8087. /* Don't choose a deleted function over ambiguity. */
  8088. if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
  8089. return 0;
  8090. if (warn)
  8091. {
  8092. pedwarn (input_location, 0,
  8093. "ISO C++ says that these are ambiguous, even "
  8094. "though the worst conversion for the first is better than "
  8095. "the worst conversion for the second:");
  8096. print_z_candidate (input_location, _("candidate 1:"), w);
  8097. print_z_candidate (input_location, _("candidate 2:"), l);
  8098. }
  8099. else
  8100. add_warning (w, l);
  8101. return winner;
  8102. }
  8103. }
  8104. gcc_assert (!winner);
  8105. return 0;
  8106. }
  8107. /* Given a list of candidates for overloading, find the best one, if any.
  8108. This algorithm has a worst case of O(2n) (winner is last), and a best
  8109. case of O(n/2) (totally ambiguous); much better than a sorting
  8110. algorithm. */
  8111. static struct z_candidate *
  8112. tourney (struct z_candidate *candidates, tsubst_flags_t complain)
  8113. {
  8114. struct z_candidate *champ = candidates, *challenger;
  8115. int fate;
  8116. int champ_compared_to_predecessor = 0;
  8117. /* Walk through the list once, comparing each current champ to the next
  8118. candidate, knocking out a candidate or two with each comparison. */
  8119. for (challenger = champ->next; challenger; )
  8120. {
  8121. fate = joust (champ, challenger, 0, complain);
  8122. if (fate == 1)
  8123. challenger = challenger->next;
  8124. else
  8125. {
  8126. if (fate == 0)
  8127. {
  8128. champ = challenger->next;
  8129. if (champ == 0)
  8130. return NULL;
  8131. champ_compared_to_predecessor = 0;
  8132. }
  8133. else
  8134. {
  8135. champ = challenger;
  8136. champ_compared_to_predecessor = 1;
  8137. }
  8138. challenger = champ->next;
  8139. }
  8140. }
  8141. /* Make sure the champ is better than all the candidates it hasn't yet
  8142. been compared to. */
  8143. for (challenger = candidates;
  8144. challenger != champ
  8145. && !(champ_compared_to_predecessor && challenger->next == champ);
  8146. challenger = challenger->next)
  8147. {
  8148. fate = joust (champ, challenger, 0, complain);
  8149. if (fate != 1)
  8150. return NULL;
  8151. }
  8152. return champ;
  8153. }
  8154. /* Returns nonzero if things of type FROM can be converted to TO. */
  8155. bool
  8156. can_convert (tree to, tree from, tsubst_flags_t complain)
  8157. {
  8158. tree arg = NULL_TREE;
  8159. /* implicit_conversion only considers user-defined conversions
  8160. if it has an expression for the call argument list. */
  8161. if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
  8162. arg = build1 (CAST_EXPR, from, NULL_TREE);
  8163. return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
  8164. }
  8165. /* Returns nonzero if things of type FROM can be converted to TO with a
  8166. standard conversion. */
  8167. bool
  8168. can_convert_standard (tree to, tree from, tsubst_flags_t complain)
  8169. {
  8170. return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
  8171. }
  8172. /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
  8173. bool
  8174. can_convert_arg (tree to, tree from, tree arg, int flags,
  8175. tsubst_flags_t complain)
  8176. {
  8177. conversion *t;
  8178. void *p;
  8179. bool ok_p;
  8180. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  8181. p = conversion_obstack_alloc (0);
  8182. /* We want to discard any access checks done for this test,
  8183. as we might not be in the appropriate access context and
  8184. we'll do the check again when we actually perform the
  8185. conversion. */
  8186. push_deferring_access_checks (dk_deferred);
  8187. t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
  8188. flags, complain);
  8189. ok_p = (t && !t->bad_p);
  8190. /* Discard the access checks now. */
  8191. pop_deferring_access_checks ();
  8192. /* Free all the conversions we allocated. */
  8193. obstack_free (&conversion_obstack, p);
  8194. return ok_p;
  8195. }
  8196. /* Like can_convert_arg, but allows dubious conversions as well. */
  8197. bool
  8198. can_convert_arg_bad (tree to, tree from, tree arg, int flags,
  8199. tsubst_flags_t complain)
  8200. {
  8201. conversion *t;
  8202. void *p;
  8203. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  8204. p = conversion_obstack_alloc (0);
  8205. /* Try to perform the conversion. */
  8206. t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
  8207. flags, complain);
  8208. /* Free all the conversions we allocated. */
  8209. obstack_free (&conversion_obstack, p);
  8210. return t != NULL;
  8211. }
  8212. /* Convert EXPR to TYPE. Return the converted expression.
  8213. Note that we allow bad conversions here because by the time we get to
  8214. this point we are committed to doing the conversion. If we end up
  8215. doing a bad conversion, convert_like will complain. */
  8216. tree
  8217. perform_implicit_conversion_flags (tree type, tree expr,
  8218. tsubst_flags_t complain, int flags)
  8219. {
  8220. conversion *conv;
  8221. void *p;
  8222. location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
  8223. if (error_operand_p (expr))
  8224. return error_mark_node;
  8225. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  8226. p = conversion_obstack_alloc (0);
  8227. conv = implicit_conversion (type, TREE_TYPE (expr), expr,
  8228. /*c_cast_p=*/false,
  8229. flags, complain);
  8230. if (!conv)
  8231. {
  8232. if (complain & tf_error)
  8233. {
  8234. /* If expr has unknown type, then it is an overloaded function.
  8235. Call instantiate_type to get good error messages. */
  8236. if (TREE_TYPE (expr) == unknown_type_node)
  8237. instantiate_type (type, expr, complain);
  8238. else if (invalid_nonstatic_memfn_p (expr, complain))
  8239. /* We gave an error. */;
  8240. else
  8241. error_at (loc, "could not convert %qE from %qT to %qT", expr,
  8242. TREE_TYPE (expr), type);
  8243. }
  8244. expr = error_mark_node;
  8245. }
  8246. else if (processing_template_decl && conv->kind != ck_identity)
  8247. {
  8248. /* In a template, we are only concerned about determining the
  8249. type of non-dependent expressions, so we do not have to
  8250. perform the actual conversion. But for initializers, we
  8251. need to be able to perform it at instantiation
  8252. (or instantiate_non_dependent_expr) time. */
  8253. expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
  8254. if (!(flags & LOOKUP_ONLYCONVERTING))
  8255. IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
  8256. }
  8257. else
  8258. expr = convert_like (conv, expr, complain);
  8259. /* Free all the conversions we allocated. */
  8260. obstack_free (&conversion_obstack, p);
  8261. return expr;
  8262. }
  8263. tree
  8264. perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
  8265. {
  8266. return perform_implicit_conversion_flags (type, expr, complain,
  8267. LOOKUP_IMPLICIT);
  8268. }
  8269. /* Convert EXPR to TYPE (as a direct-initialization) if that is
  8270. permitted. If the conversion is valid, the converted expression is
  8271. returned. Otherwise, NULL_TREE is returned, except in the case
  8272. that TYPE is a class type; in that case, an error is issued. If
  8273. C_CAST_P is true, then this direct-initialization is taking
  8274. place as part of a static_cast being attempted as part of a C-style
  8275. cast. */
  8276. tree
  8277. perform_direct_initialization_if_possible (tree type,
  8278. tree expr,
  8279. bool c_cast_p,
  8280. tsubst_flags_t complain)
  8281. {
  8282. conversion *conv;
  8283. void *p;
  8284. if (type == error_mark_node || error_operand_p (expr))
  8285. return error_mark_node;
  8286. /* [dcl.init]
  8287. If the destination type is a (possibly cv-qualified) class type:
  8288. -- If the initialization is direct-initialization ...,
  8289. constructors are considered. ... If no constructor applies, or
  8290. the overload resolution is ambiguous, the initialization is
  8291. ill-formed. */
  8292. if (CLASS_TYPE_P (type))
  8293. {
  8294. vec<tree, va_gc> *args = make_tree_vector_single (expr);
  8295. expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
  8296. &args, type, LOOKUP_NORMAL, complain);
  8297. release_tree_vector (args);
  8298. return build_cplus_new (type, expr, complain);
  8299. }
  8300. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  8301. p = conversion_obstack_alloc (0);
  8302. conv = implicit_conversion (type, TREE_TYPE (expr), expr,
  8303. c_cast_p,
  8304. LOOKUP_NORMAL, complain);
  8305. if (!conv || conv->bad_p)
  8306. expr = NULL_TREE;
  8307. else
  8308. expr = convert_like_real (conv, expr, NULL_TREE, 0, 0,
  8309. /*issue_conversion_warnings=*/false,
  8310. c_cast_p,
  8311. complain);
  8312. /* Free all the conversions we allocated. */
  8313. obstack_free (&conversion_obstack, p);
  8314. return expr;
  8315. }
  8316. /* When initializing a reference that lasts longer than a full-expression,
  8317. this special rule applies:
  8318. [class.temporary]
  8319. The temporary to which the reference is bound or the temporary
  8320. that is the complete object to which the reference is bound
  8321. persists for the lifetime of the reference.
  8322. The temporaries created during the evaluation of the expression
  8323. initializing the reference, except the temporary to which the
  8324. reference is bound, are destroyed at the end of the
  8325. full-expression in which they are created.
  8326. In that case, we store the converted expression into a new
  8327. VAR_DECL in a new scope.
  8328. However, we want to be careful not to create temporaries when
  8329. they are not required. For example, given:
  8330. struct B {};
  8331. struct D : public B {};
  8332. D f();
  8333. const B& b = f();
  8334. there is no need to copy the return value from "f"; we can just
  8335. extend its lifetime. Similarly, given:
  8336. struct S {};
  8337. struct T { operator S(); };
  8338. T t;
  8339. const S& s = t;
  8340. we can extend the lifetime of the return value of the conversion
  8341. operator.
  8342. The next several functions are involved in this lifetime extension. */
  8343. /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
  8344. reference is being bound to a temporary. Create and return a new
  8345. VAR_DECL with the indicated TYPE; this variable will store the value to
  8346. which the reference is bound. */
  8347. tree
  8348. make_temporary_var_for_ref_to_temp (tree decl, tree type)
  8349. {
  8350. tree var;
  8351. /* Create the variable. */
  8352. var = create_temporary_var (type);
  8353. /* Register the variable. */
  8354. if (VAR_P (decl)
  8355. && (TREE_STATIC (decl) || DECL_THREAD_LOCAL_P (decl)))
  8356. {
  8357. /* Namespace-scope or local static; give it a mangled name. */
  8358. /* FIXME share comdat with decl? */
  8359. tree name;
  8360. TREE_STATIC (var) = TREE_STATIC (decl);
  8361. set_decl_tls_model (var, DECL_TLS_MODEL (decl));
  8362. name = mangle_ref_init_variable (decl);
  8363. DECL_NAME (var) = name;
  8364. SET_DECL_ASSEMBLER_NAME (var, name);
  8365. var = pushdecl_top_level (var);
  8366. }
  8367. else
  8368. /* Create a new cleanup level if necessary. */
  8369. maybe_push_cleanup_level (type);
  8370. return var;
  8371. }
  8372. /* EXPR is the initializer for a variable DECL of reference or
  8373. std::initializer_list type. Create, push and return a new VAR_DECL
  8374. for the initializer so that it will live as long as DECL. Any
  8375. cleanup for the new variable is returned through CLEANUP, and the
  8376. code to initialize the new variable is returned through INITP. */
  8377. static tree
  8378. set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
  8379. tree *initp)
  8380. {
  8381. tree init;
  8382. tree type;
  8383. tree var;
  8384. /* Create the temporary variable. */
  8385. type = TREE_TYPE (expr);
  8386. var = make_temporary_var_for_ref_to_temp (decl, type);
  8387. layout_decl (var, 0);
  8388. /* If the rvalue is the result of a function call it will be
  8389. a TARGET_EXPR. If it is some other construct (such as a
  8390. member access expression where the underlying object is
  8391. itself the result of a function call), turn it into a
  8392. TARGET_EXPR here. It is important that EXPR be a
  8393. TARGET_EXPR below since otherwise the INIT_EXPR will
  8394. attempt to make a bitwise copy of EXPR to initialize
  8395. VAR. */
  8396. if (TREE_CODE (expr) != TARGET_EXPR)
  8397. expr = get_target_expr (expr);
  8398. if (TREE_CODE (decl) == FIELD_DECL
  8399. && extra_warnings && !TREE_NO_WARNING (decl))
  8400. {
  8401. warning (OPT_Wextra, "a temporary bound to %qD only persists "
  8402. "until the constructor exits", decl);
  8403. TREE_NO_WARNING (decl) = true;
  8404. }
  8405. /* Recursively extend temps in this initializer. */
  8406. TARGET_EXPR_INITIAL (expr)
  8407. = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups);
  8408. /* Any reference temp has a non-trivial initializer. */
  8409. DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
  8410. /* If the initializer is constant, put it in DECL_INITIAL so we get
  8411. static initialization and use in constant expressions. */
  8412. init = maybe_constant_init (expr);
  8413. if (TREE_CONSTANT (init))
  8414. {
  8415. if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
  8416. {
  8417. /* 5.19 says that a constant expression can include an
  8418. lvalue-rvalue conversion applied to "a glvalue of literal type
  8419. that refers to a non-volatile temporary object initialized
  8420. with a constant expression". Rather than try to communicate
  8421. that this VAR_DECL is a temporary, just mark it constexpr.
  8422. Currently this is only useful for initializer_list temporaries,
  8423. since reference vars can't appear in constant expressions. */
  8424. DECL_DECLARED_CONSTEXPR_P (var) = true;
  8425. DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
  8426. TREE_CONSTANT (var) = true;
  8427. }
  8428. DECL_INITIAL (var) = init;
  8429. init = NULL_TREE;
  8430. }
  8431. else
  8432. /* Create the INIT_EXPR that will initialize the temporary
  8433. variable. */
  8434. init = split_nonconstant_init (var, expr);
  8435. if (at_function_scope_p ())
  8436. {
  8437. add_decl_expr (var);
  8438. if (TREE_STATIC (var))
  8439. init = add_stmt_to_compound (init, register_dtor_fn (var));
  8440. else
  8441. {
  8442. tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
  8443. if (cleanup)
  8444. vec_safe_push (*cleanups, cleanup);
  8445. }
  8446. /* We must be careful to destroy the temporary only
  8447. after its initialization has taken place. If the
  8448. initialization throws an exception, then the
  8449. destructor should not be run. We cannot simply
  8450. transform INIT into something like:
  8451. (INIT, ({ CLEANUP_STMT; }))
  8452. because emit_local_var always treats the
  8453. initializer as a full-expression. Thus, the
  8454. destructor would run too early; it would run at the
  8455. end of initializing the reference variable, rather
  8456. than at the end of the block enclosing the
  8457. reference variable.
  8458. The solution is to pass back a cleanup expression
  8459. which the caller is responsible for attaching to
  8460. the statement tree. */
  8461. }
  8462. else
  8463. {
  8464. rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
  8465. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  8466. {
  8467. if (DECL_THREAD_LOCAL_P (var))
  8468. tls_aggregates = tree_cons (NULL_TREE, var,
  8469. tls_aggregates);
  8470. else
  8471. static_aggregates = tree_cons (NULL_TREE, var,
  8472. static_aggregates);
  8473. }
  8474. else
  8475. /* Check whether the dtor is callable. */
  8476. cxx_maybe_build_cleanup (var, tf_warning_or_error);
  8477. }
  8478. /* Avoid -Wunused-variable warning (c++/38958). */
  8479. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
  8480. && TREE_CODE (decl) == VAR_DECL)
  8481. TREE_USED (decl) = DECL_READ_P (decl) = true;
  8482. *initp = init;
  8483. return var;
  8484. }
  8485. /* Convert EXPR to the indicated reference TYPE, in a way suitable for
  8486. initializing a variable of that TYPE. */
  8487. tree
  8488. initialize_reference (tree type, tree expr,
  8489. int flags, tsubst_flags_t complain)
  8490. {
  8491. conversion *conv;
  8492. void *p;
  8493. location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
  8494. if (type == error_mark_node || error_operand_p (expr))
  8495. return error_mark_node;
  8496. /* Get the high-water mark for the CONVERSION_OBSTACK. */
  8497. p = conversion_obstack_alloc (0);
  8498. conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
  8499. flags, complain);
  8500. if (!conv || conv->bad_p)
  8501. {
  8502. if (complain & tf_error)
  8503. {
  8504. if (conv)
  8505. convert_like (conv, expr, complain);
  8506. else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
  8507. && !TYPE_REF_IS_RVALUE (type)
  8508. && !real_lvalue_p (expr))
  8509. error_at (loc, "invalid initialization of non-const reference of "
  8510. "type %qT from an rvalue of type %qT",
  8511. type, TREE_TYPE (expr));
  8512. else
  8513. error_at (loc, "invalid initialization of reference of type "
  8514. "%qT from expression of type %qT", type,
  8515. TREE_TYPE (expr));
  8516. }
  8517. return error_mark_node;
  8518. }
  8519. if (conv->kind == ck_ref_bind)
  8520. /* Perform the conversion. */
  8521. expr = convert_like (conv, expr, complain);
  8522. else if (conv->kind == ck_ambig)
  8523. /* We gave an error in build_user_type_conversion_1. */
  8524. expr = error_mark_node;
  8525. else
  8526. gcc_unreachable ();
  8527. /* Free all the conversions we allocated. */
  8528. obstack_free (&conversion_obstack, p);
  8529. return expr;
  8530. }
  8531. /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
  8532. which is bound either to a reference or a std::initializer_list. */
  8533. static tree
  8534. extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups)
  8535. {
  8536. tree sub = init;
  8537. tree *p;
  8538. STRIP_NOPS (sub);
  8539. if (TREE_CODE (sub) == COMPOUND_EXPR)
  8540. {
  8541. TREE_OPERAND (sub, 1)
  8542. = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups);
  8543. return init;
  8544. }
  8545. if (TREE_CODE (sub) != ADDR_EXPR)
  8546. return init;
  8547. /* Deal with binding to a subobject. */
  8548. for (p = &TREE_OPERAND (sub, 0); TREE_CODE (*p) == COMPONENT_REF; )
  8549. p = &TREE_OPERAND (*p, 0);
  8550. if (TREE_CODE (*p) == TARGET_EXPR)
  8551. {
  8552. tree subinit = NULL_TREE;
  8553. *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit);
  8554. recompute_tree_invariant_for_addr_expr (sub);
  8555. if (init != sub)
  8556. init = fold_convert (TREE_TYPE (init), sub);
  8557. if (subinit)
  8558. init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
  8559. }
  8560. return init;
  8561. }
  8562. /* INIT is part of the initializer for DECL. If there are any
  8563. reference or initializer lists being initialized, extend their
  8564. lifetime to match that of DECL. */
  8565. tree
  8566. extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups)
  8567. {
  8568. tree type = TREE_TYPE (init);
  8569. if (processing_template_decl)
  8570. return init;
  8571. if (TREE_CODE (type) == REFERENCE_TYPE)
  8572. init = extend_ref_init_temps_1 (decl, init, cleanups);
  8573. else if (is_std_init_list (type))
  8574. {
  8575. /* The temporary array underlying a std::initializer_list
  8576. is handled like a reference temporary. */
  8577. tree ctor = init;
  8578. if (TREE_CODE (ctor) == TARGET_EXPR)
  8579. ctor = TARGET_EXPR_INITIAL (ctor);
  8580. if (TREE_CODE (ctor) == CONSTRUCTOR)
  8581. {
  8582. tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
  8583. array = extend_ref_init_temps_1 (decl, array, cleanups);
  8584. CONSTRUCTOR_ELT (ctor, 0)->value = array;
  8585. }
  8586. }
  8587. else if (TREE_CODE (init) == CONSTRUCTOR)
  8588. {
  8589. unsigned i;
  8590. constructor_elt *p;
  8591. vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
  8592. FOR_EACH_VEC_SAFE_ELT (elts, i, p)
  8593. p->value = extend_ref_init_temps (decl, p->value, cleanups);
  8594. }
  8595. return init;
  8596. }
  8597. /* Returns true iff an initializer for TYPE could contain temporaries that
  8598. need to be extended because they are bound to references or
  8599. std::initializer_list. */
  8600. bool
  8601. type_has_extended_temps (tree type)
  8602. {
  8603. type = strip_array_types (type);
  8604. if (TREE_CODE (type) == REFERENCE_TYPE)
  8605. return true;
  8606. if (CLASS_TYPE_P (type))
  8607. {
  8608. if (is_std_init_list (type))
  8609. return true;
  8610. for (tree f = next_initializable_field (TYPE_FIELDS (type));
  8611. f; f = next_initializable_field (DECL_CHAIN (f)))
  8612. if (type_has_extended_temps (TREE_TYPE (f)))
  8613. return true;
  8614. }
  8615. return false;
  8616. }
  8617. /* Returns true iff TYPE is some variant of std::initializer_list. */
  8618. bool
  8619. is_std_init_list (tree type)
  8620. {
  8621. /* Look through typedefs. */
  8622. if (!TYPE_P (type))
  8623. return false;
  8624. if (cxx_dialect == cxx98)
  8625. return false;
  8626. type = TYPE_MAIN_VARIANT (type);
  8627. return (CLASS_TYPE_P (type)
  8628. && CP_TYPE_CONTEXT (type) == std_node
  8629. && strcmp (TYPE_NAME_STRING (type), "initializer_list") == 0);
  8630. }
  8631. /* Returns true iff DECL is a list constructor: i.e. a constructor which
  8632. will accept an argument list of a single std::initializer_list<T>. */
  8633. bool
  8634. is_list_ctor (tree decl)
  8635. {
  8636. tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
  8637. tree arg;
  8638. if (!args || args == void_list_node)
  8639. return false;
  8640. arg = non_reference (TREE_VALUE (args));
  8641. if (!is_std_init_list (arg))
  8642. return false;
  8643. args = TREE_CHAIN (args);
  8644. if (args && args != void_list_node && !TREE_PURPOSE (args))
  8645. /* There are more non-defaulted parms. */
  8646. return false;
  8647. return true;
  8648. }
  8649. #include "gt-cp-call.h"