decl.c 457 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796
  1. /* Process declarations and variables for C++ compiler.
  2. Copyright (C) 1988-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann (tiemann@cygnus.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Process declarations and symbol lookup for C++ front end.
  17. Also constructs types; the standard scalar types at initialization,
  18. and structure, union, array and enum types when they are declared. */
  19. /* ??? not all decl nodes are given the most useful possible
  20. line numbers. For example, the CONST_DECLs for enum values. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "tm.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "tree-hasher.h"
  36. #include "stringpool.h"
  37. #include "stor-layout.h"
  38. #include "varasm.h"
  39. #include "attribs.h"
  40. #include "calls.h"
  41. #include "flags.h"
  42. #include "cp-tree.h"
  43. #include "tree-iterator.h"
  44. #include "tree-inline.h"
  45. #include "decl.h"
  46. #include "intl.h"
  47. #include "toplev.h"
  48. #include "hashtab.h"
  49. #include "tm_p.h"
  50. #include "target.h"
  51. #include "c-family/c-common.h"
  52. #include "c-family/c-objc.h"
  53. #include "c-family/c-pragma.h"
  54. #include "c-family/c-target.h"
  55. #include "c-family/c-ubsan.h"
  56. #include "diagnostic.h"
  57. #include "intl.h"
  58. #include "debug.h"
  59. #include "timevar.h"
  60. #include "splay-tree.h"
  61. #include "plugin.h"
  62. #include "hash-map.h"
  63. #include "is-a.h"
  64. #include "plugin-api.h"
  65. #include "hard-reg-set.h"
  66. #include "input.h"
  67. #include "function.h"
  68. #include "ipa-ref.h"
  69. #include "cgraph.h"
  70. #include "cilk.h"
  71. #include "wide-int.h"
  72. #include "builtins.h"
  73. /* Possible cases of bad specifiers type used by bad_specifiers. */
  74. enum bad_spec_place {
  75. BSP_VAR, /* variable */
  76. BSP_PARM, /* parameter */
  77. BSP_TYPE, /* type */
  78. BSP_FIELD /* field */
  79. };
  80. static tree grokparms (tree parmlist, tree *);
  81. static const char *redeclaration_error_message (tree, tree);
  82. static int decl_jump_unsafe (tree);
  83. static void require_complete_types_for_parms (tree);
  84. static int ambi_op_p (enum tree_code);
  85. static int unary_op_p (enum tree_code);
  86. static void push_local_name (tree);
  87. static tree grok_reference_init (tree, tree, tree, int);
  88. static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
  89. int, int, int, tree);
  90. static int check_static_variable_definition (tree, tree);
  91. static void record_unknown_type (tree, const char *);
  92. static tree builtin_function_1 (tree, tree, bool);
  93. static int member_function_or_else (tree, tree, enum overload_flags);
  94. static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
  95. int);
  96. static void check_for_uninitialized_const_var (tree);
  97. static tree local_variable_p_walkfn (tree *, int *, void *);
  98. static tree record_builtin_java_type (const char *, int);
  99. static const char *tag_name (enum tag_types);
  100. static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
  101. static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
  102. static void maybe_deduce_size_from_array_init (tree, tree);
  103. static void layout_var_decl (tree);
  104. static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
  105. static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
  106. static void save_function_data (tree);
  107. static void copy_type_enum (tree , tree);
  108. static void check_function_type (tree, tree);
  109. static void finish_constructor_body (void);
  110. static void begin_destructor_body (void);
  111. static void finish_destructor_body (void);
  112. static void record_key_method_defined (tree);
  113. static tree create_array_type_for_decl (tree, tree, tree);
  114. static tree get_atexit_node (void);
  115. static tree get_dso_handle_node (void);
  116. static tree start_cleanup_fn (void);
  117. static void end_cleanup_fn (void);
  118. static tree cp_make_fname_decl (location_t, tree, int);
  119. static void initialize_predefined_identifiers (void);
  120. static tree check_special_function_return_type
  121. (special_function_kind, tree, tree);
  122. static tree push_cp_library_fn (enum tree_code, tree, int);
  123. static tree build_cp_library_fn (tree, enum tree_code, tree, int);
  124. static void store_parm_decls (tree);
  125. static void initialize_local_var (tree, tree);
  126. static void expand_static_init (tree, tree);
  127. /* The following symbols are subsumed in the cp_global_trees array, and
  128. listed here individually for documentation purposes.
  129. C++ extensions
  130. tree wchar_decl_node;
  131. tree vtable_entry_type;
  132. tree delta_type_node;
  133. tree __t_desc_type_node;
  134. tree class_type_node;
  135. tree unknown_type_node;
  136. Array type `vtable_entry_type[]'
  137. tree vtbl_type_node;
  138. tree vtbl_ptr_type_node;
  139. Namespaces,
  140. tree std_node;
  141. tree abi_node;
  142. A FUNCTION_DECL which can call `abort'. Not necessarily the
  143. one that the user will declare, but sufficient to be called
  144. by routines that want to abort the program.
  145. tree abort_fndecl;
  146. Used by RTTI
  147. tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
  148. tree tinfo_var_id; */
  149. tree cp_global_trees[CPTI_MAX];
  150. /* Indicates that there is a type value in some namespace, although
  151. that is not necessarily in scope at the moment. */
  152. tree global_type_node;
  153. /* The node that holds the "name" of the global scope. */
  154. tree global_scope_name;
  155. #define local_names cp_function_chain->x_local_names
  156. /* A list of objects which have constructors or destructors
  157. which reside in the global scope. The decl is stored in
  158. the TREE_VALUE slot and the initializer is stored
  159. in the TREE_PURPOSE slot. */
  160. tree static_aggregates;
  161. /* Like static_aggregates, but for thread_local variables. */
  162. tree tls_aggregates;
  163. /* -- end of C++ */
  164. /* A node for the integer constant 2. */
  165. tree integer_two_node;
  166. /* Used only for jumps to as-yet undefined labels, since jumps to
  167. defined labels can have their validity checked immediately. */
  168. struct GTY((chain_next ("%h.next"))) named_label_use_entry {
  169. struct named_label_use_entry *next;
  170. /* The binding level to which this entry is *currently* attached.
  171. This is initially the binding level in which the goto appeared,
  172. but is modified as scopes are closed. */
  173. cp_binding_level *binding_level;
  174. /* The head of the names list that was current when the goto appeared,
  175. or the inner scope popped. These are the decls that will *not* be
  176. skipped when jumping to the label. */
  177. tree names_in_scope;
  178. /* The location of the goto, for error reporting. */
  179. location_t o_goto_locus;
  180. /* True if an OpenMP structured block scope has been closed since
  181. the goto appeared. This means that the branch from the label will
  182. illegally exit an OpenMP scope. */
  183. bool in_omp_scope;
  184. };
  185. /* A list of all LABEL_DECLs in the function that have names. Here so
  186. we can clear out their names' definitions at the end of the
  187. function, and so we can check the validity of jumps to these labels. */
  188. struct GTY((for_user)) named_label_entry {
  189. /* The decl itself. */
  190. tree label_decl;
  191. /* The binding level to which the label is *currently* attached.
  192. This is initially set to the binding level in which the label
  193. is defined, but is modified as scopes are closed. */
  194. cp_binding_level *binding_level;
  195. /* The head of the names list that was current when the label was
  196. defined, or the inner scope popped. These are the decls that will
  197. be skipped when jumping to the label. */
  198. tree names_in_scope;
  199. /* A vector of all decls from all binding levels that would be
  200. crossed by a backward branch to the label. */
  201. vec<tree, va_gc> *bad_decls;
  202. /* A list of uses of the label, before the label is defined. */
  203. struct named_label_use_entry *uses;
  204. /* The following bits are set after the label is defined, and are
  205. updated as scopes are popped. They indicate that a backward jump
  206. to the label will illegally enter a scope of the given flavor. */
  207. bool in_try_scope;
  208. bool in_catch_scope;
  209. bool in_omp_scope;
  210. };
  211. #define named_labels cp_function_chain->x_named_labels
  212. /* The number of function bodies which we are currently processing.
  213. (Zero if we are at namespace scope, one inside the body of a
  214. function, two inside the body of a function in a local class, etc.) */
  215. int function_depth;
  216. /* To avoid unwanted recursion, finish_function defers all mark_used calls
  217. encountered during its execution until it finishes. */
  218. bool defer_mark_used_calls;
  219. vec<tree, va_gc> *deferred_mark_used_calls;
  220. /* States indicating how grokdeclarator() should handle declspecs marked
  221. with __attribute__((deprecated)). An object declared as
  222. __attribute__((deprecated)) suppresses warnings of uses of other
  223. deprecated items. */
  224. enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
  225. /* A list of VAR_DECLs whose type was incomplete at the time the
  226. variable was declared. */
  227. typedef struct GTY(()) incomplete_var_d {
  228. tree decl;
  229. tree incomplete_type;
  230. } incomplete_var;
  231. static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
  232. /* Returns the kind of template specialization we are currently
  233. processing, given that it's declaration contained N_CLASS_SCOPES
  234. explicit scope qualifications. */
  235. tmpl_spec_kind
  236. current_tmpl_spec_kind (int n_class_scopes)
  237. {
  238. int n_template_parm_scopes = 0;
  239. int seen_specialization_p = 0;
  240. int innermost_specialization_p = 0;
  241. cp_binding_level *b;
  242. /* Scan through the template parameter scopes. */
  243. for (b = current_binding_level;
  244. b->kind == sk_template_parms;
  245. b = b->level_chain)
  246. {
  247. /* If we see a specialization scope inside a parameter scope,
  248. then something is wrong. That corresponds to a declaration
  249. like:
  250. template <class T> template <> ...
  251. which is always invalid since [temp.expl.spec] forbids the
  252. specialization of a class member template if the enclosing
  253. class templates are not explicitly specialized as well. */
  254. if (b->explicit_spec_p)
  255. {
  256. if (n_template_parm_scopes == 0)
  257. innermost_specialization_p = 1;
  258. else
  259. seen_specialization_p = 1;
  260. }
  261. else if (seen_specialization_p == 1)
  262. return tsk_invalid_member_spec;
  263. ++n_template_parm_scopes;
  264. }
  265. /* Handle explicit instantiations. */
  266. if (processing_explicit_instantiation)
  267. {
  268. if (n_template_parm_scopes != 0)
  269. /* We've seen a template parameter list during an explicit
  270. instantiation. For example:
  271. template <class T> template void f(int);
  272. This is erroneous. */
  273. return tsk_invalid_expl_inst;
  274. else
  275. return tsk_expl_inst;
  276. }
  277. if (n_template_parm_scopes < n_class_scopes)
  278. /* We've not seen enough template headers to match all the
  279. specialized classes present. For example:
  280. template <class T> void R<T>::S<T>::f(int);
  281. This is invalid; there needs to be one set of template
  282. parameters for each class. */
  283. return tsk_insufficient_parms;
  284. else if (n_template_parm_scopes == n_class_scopes)
  285. /* We're processing a non-template declaration (even though it may
  286. be a member of a template class.) For example:
  287. template <class T> void S<T>::f(int);
  288. The `class T' matches the `S<T>', leaving no template headers
  289. corresponding to the `f'. */
  290. return tsk_none;
  291. else if (n_template_parm_scopes > n_class_scopes + 1)
  292. /* We've got too many template headers. For example:
  293. template <> template <class T> void f (T);
  294. There need to be more enclosing classes. */
  295. return tsk_excessive_parms;
  296. else
  297. /* This must be a template. It's of the form:
  298. template <class T> template <class U> void S<T>::f(U);
  299. This is a specialization if the innermost level was a
  300. specialization; otherwise it's just a definition of the
  301. template. */
  302. return innermost_specialization_p ? tsk_expl_spec : tsk_template;
  303. }
  304. /* Exit the current scope. */
  305. void
  306. finish_scope (void)
  307. {
  308. poplevel (0, 0, 0);
  309. }
  310. /* When a label goes out of scope, check to see if that label was used
  311. in a valid manner, and issue any appropriate warnings or errors. */
  312. static void
  313. pop_label (tree label, tree old_value)
  314. {
  315. if (!processing_template_decl)
  316. {
  317. if (DECL_INITIAL (label) == NULL_TREE)
  318. {
  319. location_t location;
  320. error ("label %q+D used but not defined", label);
  321. location = input_location;
  322. /* FIXME want (LOCATION_FILE (input_location), (line)0) */
  323. /* Avoid crashing later. */
  324. define_label (location, DECL_NAME (label));
  325. }
  326. else
  327. warn_for_unused_label (label);
  328. }
  329. SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
  330. }
  331. /* At the end of a function, all labels declared within the function
  332. go out of scope. BLOCK is the top-level block for the
  333. function. */
  334. int
  335. pop_labels_1 (named_label_entry **slot, tree block)
  336. {
  337. struct named_label_entry *ent = *slot;
  338. pop_label (ent->label_decl, NULL_TREE);
  339. /* Put the labels into the "variables" of the top-level block,
  340. so debugger can see them. */
  341. DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
  342. BLOCK_VARS (block) = ent->label_decl;
  343. named_labels->clear_slot (slot);
  344. return 1;
  345. }
  346. static void
  347. pop_labels (tree block)
  348. {
  349. if (named_labels)
  350. {
  351. named_labels->traverse<tree, pop_labels_1> (block);
  352. named_labels = NULL;
  353. }
  354. }
  355. /* At the end of a block with local labels, restore the outer definition. */
  356. static void
  357. pop_local_label (tree label, tree old_value)
  358. {
  359. struct named_label_entry dummy;
  360. pop_label (label, old_value);
  361. dummy.label_decl = label;
  362. named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
  363. named_labels->clear_slot (slot);
  364. }
  365. /* The following two routines are used to interface to Objective-C++.
  366. The binding level is purposely treated as an opaque type. */
  367. void *
  368. objc_get_current_scope (void)
  369. {
  370. return current_binding_level;
  371. }
  372. /* The following routine is used by the NeXT-style SJLJ exceptions;
  373. variables get marked 'volatile' so as to not be clobbered by
  374. _setjmp()/_longjmp() calls. All variables in the current scope,
  375. as well as parent scopes up to (but not including) ENCLOSING_BLK
  376. shall be thusly marked. */
  377. void
  378. objc_mark_locals_volatile (void *enclosing_blk)
  379. {
  380. cp_binding_level *scope;
  381. for (scope = current_binding_level;
  382. scope && scope != enclosing_blk;
  383. scope = scope->level_chain)
  384. {
  385. tree decl;
  386. for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
  387. objc_volatilize_decl (decl);
  388. /* Do not climb up past the current function. */
  389. if (scope->kind == sk_function_parms)
  390. break;
  391. }
  392. }
  393. /* Update data for defined and undefined labels when leaving a scope. */
  394. int
  395. poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
  396. {
  397. named_label_entry *ent = *slot;
  398. cp_binding_level *obl = bl->level_chain;
  399. if (ent->binding_level == bl)
  400. {
  401. tree decl;
  402. /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
  403. TREE_LISTs representing OVERLOADs, so be careful. */
  404. for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
  405. ? DECL_CHAIN (decl)
  406. : TREE_CHAIN (decl)))
  407. if (decl_jump_unsafe (decl))
  408. vec_safe_push (ent->bad_decls, decl);
  409. ent->binding_level = obl;
  410. ent->names_in_scope = obl->names;
  411. switch (bl->kind)
  412. {
  413. case sk_try:
  414. ent->in_try_scope = true;
  415. break;
  416. case sk_catch:
  417. ent->in_catch_scope = true;
  418. break;
  419. case sk_omp:
  420. ent->in_omp_scope = true;
  421. break;
  422. default:
  423. break;
  424. }
  425. }
  426. else if (ent->uses)
  427. {
  428. struct named_label_use_entry *use;
  429. for (use = ent->uses; use ; use = use->next)
  430. if (use->binding_level == bl)
  431. {
  432. use->binding_level = obl;
  433. use->names_in_scope = obl->names;
  434. if (bl->kind == sk_omp)
  435. use->in_omp_scope = true;
  436. }
  437. }
  438. return 1;
  439. }
  440. /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
  441. when errors were reported, except for -Werror-unused-but-set-*. */
  442. static int unused_but_set_errorcount;
  443. /* Exit a binding level.
  444. Pop the level off, and restore the state of the identifier-decl mappings
  445. that were in effect when this level was entered.
  446. If KEEP == 1, this level had explicit declarations, so
  447. and create a "block" (a BLOCK node) for the level
  448. to record its declarations and subblocks for symbol table output.
  449. If FUNCTIONBODY is nonzero, this level is the body of a function,
  450. so create a block as if KEEP were set and also clear out all
  451. label names.
  452. If REVERSE is nonzero, reverse the order of decls before putting
  453. them into the BLOCK. */
  454. tree
  455. poplevel (int keep, int reverse, int functionbody)
  456. {
  457. tree link;
  458. /* The chain of decls was accumulated in reverse order.
  459. Put it into forward order, just for cleanliness. */
  460. tree decls;
  461. tree subblocks;
  462. tree block;
  463. tree decl;
  464. int leaving_for_scope;
  465. scope_kind kind;
  466. unsigned ix;
  467. cp_label_binding *label_bind;
  468. bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
  469. restart:
  470. block = NULL_TREE;
  471. gcc_assert (current_binding_level->kind != sk_class);
  472. if (current_binding_level->kind == sk_cleanup)
  473. functionbody = 0;
  474. subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
  475. gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
  476. /* We used to use KEEP == 2 to indicate that the new block should go
  477. at the beginning of the list of blocks at this binding level,
  478. rather than the end. This hack is no longer used. */
  479. gcc_assert (keep == 0 || keep == 1);
  480. if (current_binding_level->keep)
  481. keep = 1;
  482. /* Any uses of undefined labels, and any defined labels, now operate
  483. under constraints of next binding contour. */
  484. if (cfun && !functionbody && named_labels)
  485. named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
  486. (current_binding_level);
  487. /* Get the decls in the order they were written.
  488. Usually current_binding_level->names is in reverse order.
  489. But parameter decls were previously put in forward order. */
  490. if (reverse)
  491. current_binding_level->names
  492. = decls = nreverse (current_binding_level->names);
  493. else
  494. decls = current_binding_level->names;
  495. /* If there were any declarations or structure tags in that level,
  496. or if this level is a function body,
  497. create a BLOCK to record them for the life of this function. */
  498. block = NULL_TREE;
  499. /* Avoid function body block if possible. */
  500. if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
  501. keep = 0;
  502. else if (keep == 1 || functionbody)
  503. block = make_node (BLOCK);
  504. if (block != NULL_TREE)
  505. {
  506. BLOCK_VARS (block) = decls;
  507. BLOCK_SUBBLOCKS (block) = subblocks;
  508. }
  509. /* In each subblock, record that this is its superior. */
  510. if (keep >= 0)
  511. for (link = subblocks; link; link = BLOCK_CHAIN (link))
  512. BLOCK_SUPERCONTEXT (link) = block;
  513. /* We still support the old for-scope rules, whereby the variables
  514. in a for-init statement were in scope after the for-statement
  515. ended. We only use the new rules if flag_new_for_scope is
  516. nonzero. */
  517. leaving_for_scope
  518. = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
  519. /* Before we remove the declarations first check for unused variables. */
  520. if ((warn_unused_variable || warn_unused_but_set_variable)
  521. && current_binding_level->kind != sk_template_parms
  522. && !processing_template_decl)
  523. for (tree d = getdecls (); d; d = TREE_CHAIN (d))
  524. {
  525. /* There are cases where D itself is a TREE_LIST. See in
  526. push_local_binding where the list of decls returned by
  527. getdecls is built. */
  528. decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
  529. tree type = TREE_TYPE (decl);
  530. if (VAR_P (decl)
  531. && (! TREE_USED (decl) || !DECL_READ_P (decl))
  532. && ! DECL_IN_SYSTEM_HEADER (decl)
  533. && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
  534. && type != error_mark_node
  535. && (!CLASS_TYPE_P (type)
  536. || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
  537. || lookup_attribute ("warn_unused",
  538. TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
  539. {
  540. if (! TREE_USED (decl))
  541. warning (OPT_Wunused_variable, "unused variable %q+D", decl);
  542. else if (DECL_CONTEXT (decl) == current_function_decl
  543. // For -Wunused-but-set-variable leave references alone.
  544. && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
  545. && errorcount == unused_but_set_errorcount)
  546. {
  547. warning (OPT_Wunused_but_set_variable,
  548. "variable %q+D set but not used", decl);
  549. unused_but_set_errorcount = errorcount;
  550. }
  551. }
  552. }
  553. /* Remove declarations for all the DECLs in this level. */
  554. for (link = decls; link; link = TREE_CHAIN (link))
  555. {
  556. if (leaving_for_scope && VAR_P (link)
  557. /* It's hard to make this ARM compatibility hack play nicely with
  558. lambdas, and it really isn't necessary in C++11 mode. */
  559. && cxx_dialect < cxx11
  560. && DECL_NAME (link))
  561. {
  562. tree name = DECL_NAME (link);
  563. cxx_binding *ob;
  564. tree ns_binding;
  565. ob = outer_binding (name,
  566. IDENTIFIER_BINDING (name),
  567. /*class_p=*/true);
  568. if (!ob)
  569. ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
  570. else
  571. ns_binding = NULL_TREE;
  572. if (ob && ob->scope == current_binding_level->level_chain)
  573. /* We have something like:
  574. int i;
  575. for (int i; ;);
  576. and we are leaving the `for' scope. There's no reason to
  577. keep the binding of the inner `i' in this case. */
  578. pop_binding (name, link);
  579. else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
  580. || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
  581. /* Here, we have something like:
  582. typedef int I;
  583. void f () {
  584. for (int I; ;);
  585. }
  586. We must pop the for-scope binding so we know what's a
  587. type and what isn't. */
  588. pop_binding (name, link);
  589. else
  590. {
  591. /* Mark this VAR_DECL as dead so that we can tell we left it
  592. there only for backward compatibility. */
  593. DECL_DEAD_FOR_LOCAL (link) = 1;
  594. /* Keep track of what should have happened when we
  595. popped the binding. */
  596. if (ob && ob->value)
  597. {
  598. SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
  599. DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
  600. }
  601. /* Add it to the list of dead variables in the next
  602. outermost binding to that we can remove these when we
  603. leave that binding. */
  604. vec_safe_push (
  605. current_binding_level->level_chain->dead_vars_from_for,
  606. link);
  607. /* Although we don't pop the cxx_binding, we do clear
  608. its SCOPE since the scope is going away now. */
  609. IDENTIFIER_BINDING (name)->scope
  610. = current_binding_level->level_chain;
  611. }
  612. }
  613. else
  614. {
  615. tree name;
  616. /* Remove the binding. */
  617. decl = link;
  618. if (TREE_CODE (decl) == TREE_LIST)
  619. decl = TREE_VALUE (decl);
  620. name = decl;
  621. if (TREE_CODE (name) == OVERLOAD)
  622. name = OVL_FUNCTION (name);
  623. gcc_assert (DECL_P (name));
  624. pop_binding (DECL_NAME (name), decl);
  625. }
  626. }
  627. /* Remove declarations for any `for' variables from inner scopes
  628. that we kept around. */
  629. FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
  630. ix, decl)
  631. pop_binding (DECL_NAME (decl), decl);
  632. /* Restore the IDENTIFIER_TYPE_VALUEs. */
  633. for (link = current_binding_level->type_shadowed;
  634. link; link = TREE_CHAIN (link))
  635. SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
  636. /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
  637. FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
  638. ix, label_bind)
  639. pop_local_label (label_bind->label, label_bind->prev_value);
  640. /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
  641. list if a `using' declaration put them there. The debugging
  642. back ends won't understand OVERLOAD, so we remove them here.
  643. Because the BLOCK_VARS are (temporarily) shared with
  644. CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
  645. popped all the bindings. */
  646. if (block)
  647. {
  648. tree* d;
  649. for (d = &BLOCK_VARS (block); *d; )
  650. {
  651. if (TREE_CODE (*d) == TREE_LIST)
  652. *d = TREE_CHAIN (*d);
  653. else
  654. d = &DECL_CHAIN (*d);
  655. }
  656. }
  657. /* If the level being exited is the top level of a function,
  658. check over all the labels. */
  659. if (functionbody)
  660. {
  661. if (block)
  662. {
  663. /* Since this is the top level block of a function, the vars are
  664. the function's parameters. Don't leave them in the BLOCK
  665. because they are found in the FUNCTION_DECL instead. */
  666. BLOCK_VARS (block) = 0;
  667. pop_labels (block);
  668. }
  669. else
  670. pop_labels (subblocks);
  671. }
  672. kind = current_binding_level->kind;
  673. if (kind == sk_cleanup)
  674. {
  675. tree stmt;
  676. /* If this is a temporary binding created for a cleanup, then we'll
  677. have pushed a statement list level. Pop that, create a new
  678. BIND_EXPR for the block, and insert it into the stream. */
  679. stmt = pop_stmt_list (current_binding_level->statement_list);
  680. stmt = c_build_bind_expr (input_location, block, stmt);
  681. add_stmt (stmt);
  682. }
  683. leave_scope ();
  684. if (functionbody)
  685. {
  686. /* The current function is being defined, so its DECL_INITIAL
  687. should be error_mark_node. */
  688. gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
  689. DECL_INITIAL (current_function_decl) = block ? block : subblocks;
  690. if (subblocks)
  691. {
  692. if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
  693. {
  694. if (BLOCK_SUBBLOCKS (subblocks))
  695. BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
  696. }
  697. else
  698. BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
  699. }
  700. }
  701. else if (block)
  702. current_binding_level->blocks
  703. = block_chainon (current_binding_level->blocks, block);
  704. /* If we did not make a block for the level just exited,
  705. any blocks made for inner levels
  706. (since they cannot be recorded as subblocks in that level)
  707. must be carried forward so they will later become subblocks
  708. of something else. */
  709. else if (subblocks)
  710. current_binding_level->blocks
  711. = block_chainon (current_binding_level->blocks, subblocks);
  712. /* Each and every BLOCK node created here in `poplevel' is important
  713. (e.g. for proper debugging information) so if we created one
  714. earlier, mark it as "used". */
  715. if (block)
  716. TREE_USED (block) = 1;
  717. /* All temporary bindings created for cleanups are popped silently. */
  718. if (kind == sk_cleanup)
  719. goto restart;
  720. timevar_cond_stop (TV_NAME_LOOKUP, subtime);
  721. return block;
  722. }
  723. /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
  724. itself, calling F for each. The DATA is passed to F as well. */
  725. static int
  726. walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
  727. {
  728. int result = 0;
  729. tree current = NAMESPACE_LEVEL (name_space)->namespaces;
  730. result |= (*f) (name_space, data);
  731. for (; current; current = DECL_CHAIN (current))
  732. result |= walk_namespaces_r (current, f, data);
  733. return result;
  734. }
  735. /* Walk all the namespaces, calling F for each. The DATA is passed to
  736. F as well. */
  737. int
  738. walk_namespaces (walk_namespaces_fn f, void* data)
  739. {
  740. return walk_namespaces_r (global_namespace, f, data);
  741. }
  742. /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
  743. DATA is non-NULL, this is the last time we will call
  744. wrapup_global_declarations for this NAMESPACE. */
  745. int
  746. wrapup_globals_for_namespace (tree name_space, void* data)
  747. {
  748. cp_binding_level *level = NAMESPACE_LEVEL (name_space);
  749. vec<tree, va_gc> *statics = level->static_decls;
  750. tree *vec = statics->address ();
  751. int len = statics->length ();
  752. int last_time = (data != 0);
  753. if (last_time)
  754. {
  755. check_global_declarations (vec, len);
  756. emit_debug_global_declarations (vec, len);
  757. return 0;
  758. }
  759. /* Write out any globals that need to be output. */
  760. return wrapup_global_declarations (vec, len);
  761. }
  762. /* In C++, you don't have to write `struct S' to refer to `S'; you
  763. can just use `S'. We accomplish this by creating a TYPE_DECL as
  764. if the user had written `typedef struct S S'. Create and return
  765. the TYPE_DECL for TYPE. */
  766. tree
  767. create_implicit_typedef (tree name, tree type)
  768. {
  769. tree decl;
  770. decl = build_decl (input_location, TYPE_DECL, name, type);
  771. DECL_ARTIFICIAL (decl) = 1;
  772. /* There are other implicit type declarations, like the one *within*
  773. a class that allows you to write `S::S'. We must distinguish
  774. amongst these. */
  775. SET_DECL_IMPLICIT_TYPEDEF_P (decl);
  776. TYPE_NAME (type) = decl;
  777. TYPE_STUB_DECL (type) = decl;
  778. return decl;
  779. }
  780. /* Remember a local name for name-mangling purposes. */
  781. static void
  782. push_local_name (tree decl)
  783. {
  784. size_t i, nelts;
  785. tree t, name;
  786. timevar_start (TV_NAME_LOOKUP);
  787. name = DECL_NAME (decl);
  788. nelts = vec_safe_length (local_names);
  789. for (i = 0; i < nelts; i++)
  790. {
  791. t = (*local_names)[i];
  792. if (DECL_NAME (t) == name)
  793. {
  794. if (!DECL_LANG_SPECIFIC (decl))
  795. retrofit_lang_decl (decl);
  796. DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
  797. if (DECL_DISCRIMINATOR_SET_P (t))
  798. DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
  799. else
  800. DECL_DISCRIMINATOR (decl) = 1;
  801. (*local_names)[i] = decl;
  802. timevar_stop (TV_NAME_LOOKUP);
  803. return;
  804. }
  805. }
  806. vec_safe_push (local_names, decl);
  807. timevar_stop (TV_NAME_LOOKUP);
  808. }
  809. /* Subroutine of duplicate_decls: return truthvalue of whether
  810. or not types of these decls match.
  811. For C++, we must compare the parameter list so that `int' can match
  812. `int&' in a parameter position, but `int&' is not confused with
  813. `const int&'. */
  814. int
  815. decls_match (tree newdecl, tree olddecl)
  816. {
  817. int types_match;
  818. if (newdecl == olddecl)
  819. return 1;
  820. if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
  821. /* If the two DECLs are not even the same kind of thing, we're not
  822. interested in their types. */
  823. return 0;
  824. gcc_assert (DECL_P (newdecl));
  825. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  826. {
  827. tree f1 = TREE_TYPE (newdecl);
  828. tree f2 = TREE_TYPE (olddecl);
  829. tree p1 = TYPE_ARG_TYPES (f1);
  830. tree p2 = TYPE_ARG_TYPES (f2);
  831. tree r2;
  832. /* Specializations of different templates are different functions
  833. even if they have the same type. */
  834. tree t1 = (DECL_USE_TEMPLATE (newdecl)
  835. ? DECL_TI_TEMPLATE (newdecl)
  836. : NULL_TREE);
  837. tree t2 = (DECL_USE_TEMPLATE (olddecl)
  838. ? DECL_TI_TEMPLATE (olddecl)
  839. : NULL_TREE);
  840. if (t1 != t2)
  841. return 0;
  842. if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
  843. && ! (DECL_EXTERN_C_P (newdecl)
  844. && DECL_EXTERN_C_P (olddecl)))
  845. return 0;
  846. /* A new declaration doesn't match a built-in one unless it
  847. is also extern "C". */
  848. if (DECL_IS_BUILTIN (olddecl)
  849. && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
  850. return 0;
  851. if (TREE_CODE (f1) != TREE_CODE (f2))
  852. return 0;
  853. /* A declaration with deduced return type should use its pre-deduction
  854. type for declaration matching. */
  855. r2 = fndecl_declared_return_type (olddecl);
  856. if (same_type_p (TREE_TYPE (f1), r2))
  857. {
  858. if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
  859. && (DECL_BUILT_IN (olddecl)
  860. #ifndef NO_IMPLICIT_EXTERN_C
  861. || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
  862. || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
  863. #endif
  864. ))
  865. {
  866. types_match = self_promoting_args_p (p1);
  867. if (p1 == void_list_node)
  868. TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
  869. }
  870. #ifndef NO_IMPLICIT_EXTERN_C
  871. else if (!prototype_p (f1)
  872. && (DECL_EXTERN_C_P (olddecl)
  873. && DECL_IN_SYSTEM_HEADER (olddecl)
  874. && !DECL_CLASS_SCOPE_P (olddecl))
  875. && (DECL_EXTERN_C_P (newdecl)
  876. && DECL_IN_SYSTEM_HEADER (newdecl)
  877. && !DECL_CLASS_SCOPE_P (newdecl)))
  878. {
  879. types_match = self_promoting_args_p (p2);
  880. TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
  881. }
  882. #endif
  883. else
  884. types_match =
  885. compparms (p1, p2)
  886. && type_memfn_rqual (f1) == type_memfn_rqual (f2)
  887. && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
  888. || comp_type_attributes (TREE_TYPE (newdecl),
  889. TREE_TYPE (olddecl)) != 0);
  890. }
  891. else
  892. types_match = 0;
  893. /* The decls dont match if they correspond to two different versions
  894. of the same function. Disallow extern "C" functions to be
  895. versions for now. */
  896. if (types_match
  897. && !DECL_EXTERN_C_P (newdecl)
  898. && !DECL_EXTERN_C_P (olddecl)
  899. && targetm.target_option.function_versions (newdecl, olddecl))
  900. {
  901. /* Mark functions as versions if necessary. Modify the mangled decl
  902. name if necessary. */
  903. if (DECL_FUNCTION_VERSIONED (newdecl)
  904. && DECL_FUNCTION_VERSIONED (olddecl))
  905. return 0;
  906. if (!DECL_FUNCTION_VERSIONED (newdecl))
  907. {
  908. DECL_FUNCTION_VERSIONED (newdecl) = 1;
  909. if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
  910. mangle_decl (newdecl);
  911. }
  912. if (!DECL_FUNCTION_VERSIONED (olddecl))
  913. {
  914. DECL_FUNCTION_VERSIONED (olddecl) = 1;
  915. if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
  916. mangle_decl (olddecl);
  917. }
  918. cgraph_node::record_function_versions (olddecl, newdecl);
  919. return 0;
  920. }
  921. }
  922. else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
  923. {
  924. if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
  925. != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
  926. return 0;
  927. if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
  928. DECL_TEMPLATE_PARMS (olddecl)))
  929. return 0;
  930. if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
  931. types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
  932. TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
  933. else
  934. types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
  935. DECL_TEMPLATE_RESULT (newdecl));
  936. }
  937. else
  938. {
  939. /* Need to check scope for variable declaration (VAR_DECL).
  940. For typedef (TYPE_DECL), scope is ignored. */
  941. if (VAR_P (newdecl)
  942. && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
  943. /* [dcl.link]
  944. Two declarations for an object with C language linkage
  945. with the same name (ignoring the namespace that qualify
  946. it) that appear in different namespace scopes refer to
  947. the same object. */
  948. && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
  949. return 0;
  950. if (TREE_TYPE (newdecl) == error_mark_node)
  951. types_match = TREE_TYPE (olddecl) == error_mark_node;
  952. else if (TREE_TYPE (olddecl) == NULL_TREE)
  953. types_match = TREE_TYPE (newdecl) == NULL_TREE;
  954. else if (TREE_TYPE (newdecl) == NULL_TREE)
  955. types_match = 0;
  956. else
  957. types_match = comptypes (TREE_TYPE (newdecl),
  958. TREE_TYPE (olddecl),
  959. COMPARE_REDECLARATION);
  960. }
  961. return types_match;
  962. }
  963. /* If NEWDECL is `static' and an `extern' was seen previously,
  964. warn about it. OLDDECL is the previous declaration.
  965. Note that this does not apply to the C++ case of declaring
  966. a variable `extern const' and then later `const'.
  967. Don't complain about built-in functions, since they are beyond
  968. the user's control. */
  969. void
  970. warn_extern_redeclared_static (tree newdecl, tree olddecl)
  971. {
  972. if (TREE_CODE (newdecl) == TYPE_DECL
  973. || TREE_CODE (newdecl) == TEMPLATE_DECL
  974. || TREE_CODE (newdecl) == CONST_DECL
  975. || TREE_CODE (newdecl) == NAMESPACE_DECL)
  976. return;
  977. /* Don't get confused by static member functions; that's a different
  978. use of `static'. */
  979. if (TREE_CODE (newdecl) == FUNCTION_DECL
  980. && DECL_STATIC_FUNCTION_P (newdecl))
  981. return;
  982. /* If the old declaration was `static', or the new one isn't, then
  983. everything is OK. */
  984. if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
  985. return;
  986. /* It's OK to declare a builtin function as `static'. */
  987. if (TREE_CODE (olddecl) == FUNCTION_DECL
  988. && DECL_ARTIFICIAL (olddecl))
  989. return;
  990. if (permerror (input_location,
  991. "%qD was declared %<extern%> and later %<static%>", newdecl))
  992. inform (input_location, "previous declaration of %q+D", olddecl);
  993. }
  994. /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
  995. function templates. If their exception specifications do not
  996. match, issue a diagnostic. */
  997. static void
  998. check_redeclaration_exception_specification (tree new_decl,
  999. tree old_decl)
  1000. {
  1001. tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
  1002. tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
  1003. /* Two default specs are equivalent, don't force evaluation. */
  1004. if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
  1005. && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
  1006. return;
  1007. maybe_instantiate_noexcept (new_decl);
  1008. maybe_instantiate_noexcept (old_decl);
  1009. new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
  1010. old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
  1011. /* [except.spec]
  1012. If any declaration of a function has an exception-specification,
  1013. all declarations, including the definition and an explicit
  1014. specialization, of that function shall have an
  1015. exception-specification with the same set of type-ids. */
  1016. if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
  1017. && ! DECL_IS_BUILTIN (old_decl)
  1018. && flag_exceptions
  1019. && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
  1020. {
  1021. error ("declaration of %qF has a different exception specifier",
  1022. new_decl);
  1023. error ("from previous declaration %q+F", old_decl);
  1024. }
  1025. }
  1026. /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
  1027. Otherwise issue diagnostics. */
  1028. static bool
  1029. validate_constexpr_redeclaration (tree old_decl, tree new_decl)
  1030. {
  1031. old_decl = STRIP_TEMPLATE (old_decl);
  1032. new_decl = STRIP_TEMPLATE (new_decl);
  1033. if (!VAR_OR_FUNCTION_DECL_P (old_decl)
  1034. || !VAR_OR_FUNCTION_DECL_P (new_decl))
  1035. return true;
  1036. if (DECL_DECLARED_CONSTEXPR_P (old_decl)
  1037. == DECL_DECLARED_CONSTEXPR_P (new_decl))
  1038. return true;
  1039. if (TREE_CODE (old_decl) == FUNCTION_DECL)
  1040. {
  1041. if (DECL_BUILT_IN (old_decl))
  1042. {
  1043. /* Hide a built-in declaration. */
  1044. DECL_DECLARED_CONSTEXPR_P (old_decl)
  1045. = DECL_DECLARED_CONSTEXPR_P (new_decl);
  1046. return true;
  1047. }
  1048. /* 7.1.5 [dcl.constexpr]
  1049. Note: An explicit specialization can differ from the template
  1050. declaration with respect to the constexpr specifier. */
  1051. if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
  1052. && DECL_TEMPLATE_SPECIALIZATION (new_decl))
  1053. return true;
  1054. error ("redeclaration %qD differs in %<constexpr%>", new_decl);
  1055. error ("from previous declaration %q+D", old_decl);
  1056. return false;
  1057. }
  1058. return true;
  1059. }
  1060. /* DECL is a redeclaration of a function or function template. If
  1061. it does have default arguments issue a diagnostic. Note: this
  1062. function is used to enforce the requirements in C++11 8.3.6 about
  1063. no default arguments in redeclarations. */
  1064. static void
  1065. check_redeclaration_no_default_args (tree decl)
  1066. {
  1067. gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
  1068. for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
  1069. t && t != void_list_node; t = TREE_CHAIN (t))
  1070. if (TREE_PURPOSE (t))
  1071. {
  1072. permerror (input_location,
  1073. "redeclaration of %q#D may not have default "
  1074. "arguments", decl);
  1075. return;
  1076. }
  1077. }
  1078. #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
  1079. && lookup_attribute ("gnu_inline", \
  1080. DECL_ATTRIBUTES (fn)))
  1081. /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
  1082. If the redeclaration is invalid, a diagnostic is issued, and the
  1083. error_mark_node is returned. Otherwise, OLDDECL is returned.
  1084. If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
  1085. returned.
  1086. NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
  1087. tree
  1088. duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
  1089. {
  1090. unsigned olddecl_uid = DECL_UID (olddecl);
  1091. int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
  1092. int new_defines_function = 0;
  1093. tree new_template_info;
  1094. if (newdecl == olddecl)
  1095. return olddecl;
  1096. types_match = decls_match (newdecl, olddecl);
  1097. /* If either the type of the new decl or the type of the old decl is an
  1098. error_mark_node, then that implies that we have already issued an
  1099. error (earlier) for some bogus type specification, and in that case,
  1100. it is rather pointless to harass the user with yet more error message
  1101. about the same declaration, so just pretend the types match here. */
  1102. if (TREE_TYPE (newdecl) == error_mark_node
  1103. || TREE_TYPE (olddecl) == error_mark_node)
  1104. return error_mark_node;
  1105. if (UDLIT_OPER_P (DECL_NAME (newdecl))
  1106. && UDLIT_OPER_P (DECL_NAME (olddecl)))
  1107. {
  1108. if (TREE_CODE (newdecl) == TEMPLATE_DECL
  1109. && TREE_CODE (olddecl) != TEMPLATE_DECL
  1110. && check_raw_literal_operator (olddecl))
  1111. error ("literal operator template %q+D conflicts with"
  1112. " raw literal operator %qD", newdecl, olddecl);
  1113. else if (TREE_CODE (newdecl) != TEMPLATE_DECL
  1114. && TREE_CODE (olddecl) == TEMPLATE_DECL
  1115. && check_raw_literal_operator (newdecl))
  1116. error ("raw literal operator %q+D conflicts with"
  1117. " literal operator template %qD", newdecl, olddecl);
  1118. }
  1119. if (DECL_P (olddecl)
  1120. && TREE_CODE (newdecl) == FUNCTION_DECL
  1121. && TREE_CODE (olddecl) == FUNCTION_DECL
  1122. && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
  1123. {
  1124. if (DECL_DECLARED_INLINE_P (newdecl)
  1125. && DECL_UNINLINABLE (newdecl)
  1126. && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
  1127. /* Already warned elsewhere. */;
  1128. else if (DECL_DECLARED_INLINE_P (olddecl)
  1129. && DECL_UNINLINABLE (olddecl)
  1130. && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
  1131. /* Already warned. */;
  1132. else if (DECL_DECLARED_INLINE_P (newdecl)
  1133. && DECL_UNINLINABLE (olddecl)
  1134. && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
  1135. {
  1136. if (warning (OPT_Wattributes, "function %q+D redeclared as inline",
  1137. newdecl))
  1138. inform (DECL_SOURCE_LOCATION (olddecl),
  1139. "previous declaration of %qD with attribute noinline",
  1140. olddecl);
  1141. }
  1142. else if (DECL_DECLARED_INLINE_P (olddecl)
  1143. && DECL_UNINLINABLE (newdecl)
  1144. && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
  1145. {
  1146. if (warning (OPT_Wattributes, "function %q+D redeclared with "
  1147. "attribute noinline", newdecl))
  1148. inform (DECL_SOURCE_LOCATION (olddecl),
  1149. "previous declaration of %qD was inline",
  1150. olddecl);
  1151. }
  1152. }
  1153. /* Check for redeclaration and other discrepancies. */
  1154. if (TREE_CODE (olddecl) == FUNCTION_DECL
  1155. && DECL_ARTIFICIAL (olddecl))
  1156. {
  1157. gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
  1158. if (TREE_CODE (newdecl) != FUNCTION_DECL)
  1159. {
  1160. /* Avoid warnings redeclaring built-ins which have not been
  1161. explicitly declared. */
  1162. if (DECL_ANTICIPATED (olddecl))
  1163. return NULL_TREE;
  1164. /* If you declare a built-in or predefined function name as static,
  1165. the old definition is overridden, but optionally warn this was a
  1166. bad choice of name. */
  1167. if (! TREE_PUBLIC (newdecl))
  1168. {
  1169. warning (OPT_Wshadow,
  1170. DECL_BUILT_IN (olddecl)
  1171. ? G_("shadowing built-in function %q#D")
  1172. : G_("shadowing library function %q#D"), olddecl);
  1173. /* Discard the old built-in function. */
  1174. return NULL_TREE;
  1175. }
  1176. /* If the built-in is not ansi, then programs can override
  1177. it even globally without an error. */
  1178. else if (! DECL_BUILT_IN (olddecl))
  1179. warning (0, "library function %q#D redeclared as non-function %q#D",
  1180. olddecl, newdecl);
  1181. else
  1182. error ("declaration of %q#D conflicts with built-in "
  1183. "declaration %q#D", newdecl, olddecl);
  1184. return NULL_TREE;
  1185. }
  1186. else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
  1187. {
  1188. gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
  1189. error_at (DECL_SOURCE_LOCATION (newdecl),
  1190. "redeclaration of %<pragma omp declare reduction%>");
  1191. inform (DECL_SOURCE_LOCATION (olddecl),
  1192. "previous %<pragma omp declare reduction%> declaration");
  1193. return error_mark_node;
  1194. }
  1195. else if (!types_match)
  1196. {
  1197. /* Avoid warnings redeclaring built-ins which have not been
  1198. explicitly declared. */
  1199. if (DECL_ANTICIPATED (olddecl))
  1200. {
  1201. /* Deal with fileptr_type_node. FILE type is not known
  1202. at the time we create the builtins. */
  1203. tree t1, t2;
  1204. for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
  1205. t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
  1206. t1 || t2;
  1207. t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
  1208. if (!t1 || !t2)
  1209. break;
  1210. else if (TREE_VALUE (t2) == fileptr_type_node)
  1211. {
  1212. tree t = TREE_VALUE (t1);
  1213. if (TYPE_PTR_P (t)
  1214. && TYPE_IDENTIFIER (TREE_TYPE (t))
  1215. == get_identifier ("FILE")
  1216. && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
  1217. {
  1218. tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
  1219. TYPE_ARG_TYPES (TREE_TYPE (olddecl))
  1220. = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
  1221. types_match = decls_match (newdecl, olddecl);
  1222. if (types_match)
  1223. return duplicate_decls (newdecl, olddecl,
  1224. newdecl_is_friend);
  1225. TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
  1226. }
  1227. }
  1228. else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
  1229. break;
  1230. }
  1231. else if ((DECL_EXTERN_C_P (newdecl)
  1232. && DECL_EXTERN_C_P (olddecl))
  1233. || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
  1234. TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
  1235. {
  1236. /* A near match; override the builtin. */
  1237. if (TREE_PUBLIC (newdecl))
  1238. warning (0, "new declaration %q#D ambiguates built-in "
  1239. "declaration %q#D", newdecl, olddecl);
  1240. else
  1241. warning (OPT_Wshadow,
  1242. DECL_BUILT_IN (olddecl)
  1243. ? G_("shadowing built-in function %q#D")
  1244. : G_("shadowing library function %q#D"), olddecl);
  1245. }
  1246. else
  1247. /* Discard the old built-in function. */
  1248. return NULL_TREE;
  1249. /* Replace the old RTL to avoid problems with inlining. */
  1250. COPY_DECL_RTL (newdecl, olddecl);
  1251. }
  1252. /* Even if the types match, prefer the new declarations type for
  1253. built-ins which have not been explicitly declared, for
  1254. exception lists, etc... */
  1255. else if (DECL_IS_BUILTIN (olddecl))
  1256. {
  1257. tree type = TREE_TYPE (newdecl);
  1258. tree attribs = (*targetm.merge_type_attributes)
  1259. (TREE_TYPE (olddecl), type);
  1260. type = cp_build_type_attribute_variant (type, attribs);
  1261. TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
  1262. }
  1263. /* If a function is explicitly declared "throw ()", propagate that to
  1264. the corresponding builtin. */
  1265. if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
  1266. && DECL_ANTICIPATED (olddecl)
  1267. && TREE_NOTHROW (newdecl)
  1268. && !TREE_NOTHROW (olddecl))
  1269. {
  1270. enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
  1271. tree tmpdecl = builtin_decl_explicit (fncode);
  1272. if (tmpdecl && tmpdecl != olddecl && types_match)
  1273. TREE_NOTHROW (tmpdecl) = 1;
  1274. }
  1275. /* Whether or not the builtin can throw exceptions has no
  1276. bearing on this declarator. */
  1277. TREE_NOTHROW (olddecl) = 0;
  1278. if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
  1279. {
  1280. /* If a builtin function is redeclared as `static', merge
  1281. the declarations, but make the original one static. */
  1282. DECL_THIS_STATIC (olddecl) = 1;
  1283. TREE_PUBLIC (olddecl) = 0;
  1284. /* Make the old declaration consistent with the new one so
  1285. that all remnants of the builtin-ness of this function
  1286. will be banished. */
  1287. SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
  1288. COPY_DECL_RTL (newdecl, olddecl);
  1289. }
  1290. }
  1291. else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
  1292. {
  1293. /* C++ Standard, 3.3, clause 4:
  1294. "[Note: a namespace name or a class template name must be unique
  1295. in its declarative region (7.3.2, clause 14). ]" */
  1296. if (TREE_CODE (olddecl) != NAMESPACE_DECL
  1297. && TREE_CODE (newdecl) != NAMESPACE_DECL
  1298. && (TREE_CODE (olddecl) != TEMPLATE_DECL
  1299. || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
  1300. && (TREE_CODE (newdecl) != TEMPLATE_DECL
  1301. || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
  1302. {
  1303. if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
  1304. && TREE_CODE (newdecl) != TYPE_DECL)
  1305. || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
  1306. && TREE_CODE (olddecl) != TYPE_DECL))
  1307. {
  1308. /* We do nothing special here, because C++ does such nasty
  1309. things with TYPE_DECLs. Instead, just let the TYPE_DECL
  1310. get shadowed, and know that if we need to find a TYPE_DECL
  1311. for a given name, we can look in the IDENTIFIER_TYPE_VALUE
  1312. slot of the identifier. */
  1313. return NULL_TREE;
  1314. }
  1315. if ((TREE_CODE (newdecl) == FUNCTION_DECL
  1316. && DECL_FUNCTION_TEMPLATE_P (olddecl))
  1317. || (TREE_CODE (olddecl) == FUNCTION_DECL
  1318. && DECL_FUNCTION_TEMPLATE_P (newdecl)))
  1319. return NULL_TREE;
  1320. }
  1321. error ("%q#D redeclared as different kind of symbol", newdecl);
  1322. if (TREE_CODE (olddecl) == TREE_LIST)
  1323. olddecl = TREE_VALUE (olddecl);
  1324. inform (DECL_SOURCE_LOCATION (olddecl),
  1325. "previous declaration %q#D", olddecl);
  1326. return error_mark_node;
  1327. }
  1328. else if (!types_match)
  1329. {
  1330. if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
  1331. /* These are certainly not duplicate declarations; they're
  1332. from different scopes. */
  1333. return NULL_TREE;
  1334. if (TREE_CODE (newdecl) == TEMPLATE_DECL)
  1335. {
  1336. /* The name of a class template may not be declared to refer to
  1337. any other template, class, function, object, namespace, value,
  1338. or type in the same scope. */
  1339. if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
  1340. || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
  1341. {
  1342. error ("conflicting declaration of template %q#D", newdecl);
  1343. inform (DECL_SOURCE_LOCATION (olddecl),
  1344. "previous declaration %q#D", olddecl);
  1345. return error_mark_node;
  1346. }
  1347. else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
  1348. && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
  1349. && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
  1350. TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
  1351. && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
  1352. DECL_TEMPLATE_PARMS (olddecl))
  1353. /* Template functions can be disambiguated by
  1354. return type. */
  1355. && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
  1356. TREE_TYPE (TREE_TYPE (olddecl))))
  1357. {
  1358. error ("ambiguating new declaration %q#D", newdecl);
  1359. inform (DECL_SOURCE_LOCATION (olddecl),
  1360. "old declaration %q#D", olddecl);
  1361. }
  1362. return NULL_TREE;
  1363. }
  1364. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1365. {
  1366. if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
  1367. {
  1368. error ("conflicting declaration of C function %q#D",
  1369. newdecl);
  1370. inform (DECL_SOURCE_LOCATION (olddecl),
  1371. "previous declaration %q#D", olddecl);
  1372. return NULL_TREE;
  1373. }
  1374. /* For function versions, params and types match, but they
  1375. are not ambiguous. */
  1376. else if ((!DECL_FUNCTION_VERSIONED (newdecl)
  1377. && !DECL_FUNCTION_VERSIONED (olddecl))
  1378. && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
  1379. TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
  1380. {
  1381. error ("ambiguating new declaration of %q#D", newdecl);
  1382. inform (DECL_SOURCE_LOCATION (olddecl),
  1383. "old declaration %q#D", olddecl);
  1384. return error_mark_node;
  1385. }
  1386. else
  1387. return NULL_TREE;
  1388. }
  1389. else
  1390. {
  1391. error ("conflicting declaration %q#D", newdecl);
  1392. inform (DECL_SOURCE_LOCATION (olddecl),
  1393. "previous declaration as %q#D", olddecl);
  1394. return error_mark_node;
  1395. }
  1396. }
  1397. else if (TREE_CODE (newdecl) == FUNCTION_DECL
  1398. && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
  1399. && (!DECL_TEMPLATE_INFO (newdecl)
  1400. || (DECL_TI_TEMPLATE (newdecl)
  1401. != DECL_TI_TEMPLATE (olddecl))))
  1402. || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
  1403. && (!DECL_TEMPLATE_INFO (olddecl)
  1404. || (DECL_TI_TEMPLATE (olddecl)
  1405. != DECL_TI_TEMPLATE (newdecl))))))
  1406. /* It's OK to have a template specialization and a non-template
  1407. with the same type, or to have specializations of two
  1408. different templates with the same type. Note that if one is a
  1409. specialization, and the other is an instantiation of the same
  1410. template, that we do not exit at this point. That situation
  1411. can occur if we instantiate a template class, and then
  1412. specialize one of its methods. This situation is valid, but
  1413. the declarations must be merged in the usual way. */
  1414. return NULL_TREE;
  1415. else if (TREE_CODE (newdecl) == FUNCTION_DECL
  1416. && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
  1417. && !DECL_USE_TEMPLATE (newdecl))
  1418. || (DECL_TEMPLATE_INSTANTIATION (newdecl)
  1419. && !DECL_USE_TEMPLATE (olddecl))))
  1420. /* One of the declarations is a template instantiation, and the
  1421. other is not a template at all. That's OK. */
  1422. return NULL_TREE;
  1423. else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
  1424. {
  1425. /* In [namespace.alias] we have:
  1426. In a declarative region, a namespace-alias-definition can be
  1427. used to redefine a namespace-alias declared in that declarative
  1428. region to refer only to the namespace to which it already
  1429. refers.
  1430. Therefore, if we encounter a second alias directive for the same
  1431. alias, we can just ignore the second directive. */
  1432. if (DECL_NAMESPACE_ALIAS (newdecl)
  1433. && (DECL_NAMESPACE_ALIAS (newdecl)
  1434. == DECL_NAMESPACE_ALIAS (olddecl)))
  1435. return olddecl;
  1436. /* [namespace.alias]
  1437. A namespace-name or namespace-alias shall not be declared as
  1438. the name of any other entity in the same declarative region.
  1439. A namespace-name defined at global scope shall not be
  1440. declared as the name of any other entity in any global scope
  1441. of the program. */
  1442. error ("conflicting declaration of namespace %qD", newdecl);
  1443. inform (DECL_SOURCE_LOCATION (olddecl),
  1444. "previous declaration of namespace %qD here", olddecl);
  1445. return error_mark_node;
  1446. }
  1447. else
  1448. {
  1449. const char *errmsg = redeclaration_error_message (newdecl, olddecl);
  1450. if (errmsg)
  1451. {
  1452. error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
  1453. if (DECL_NAME (olddecl) != NULL_TREE)
  1454. inform (input_location,
  1455. (DECL_INITIAL (olddecl) && namespace_bindings_p ())
  1456. ? G_("%q+#D previously defined here")
  1457. : G_("%q+#D previously declared here"), olddecl);
  1458. return error_mark_node;
  1459. }
  1460. else if (TREE_CODE (olddecl) == FUNCTION_DECL
  1461. && DECL_INITIAL (olddecl) != NULL_TREE
  1462. && !prototype_p (TREE_TYPE (olddecl))
  1463. && prototype_p (TREE_TYPE (newdecl)))
  1464. {
  1465. /* Prototype decl follows defn w/o prototype. */
  1466. if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
  1467. "prototype specified for %q#D", newdecl))
  1468. inform (DECL_SOURCE_LOCATION (olddecl),
  1469. "previous non-prototype definition here");
  1470. }
  1471. else if (VAR_OR_FUNCTION_DECL_P (olddecl)
  1472. && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
  1473. {
  1474. /* [dcl.link]
  1475. If two declarations of the same function or object
  1476. specify different linkage-specifications ..., the program
  1477. is ill-formed.... Except for functions with C++ linkage,
  1478. a function declaration without a linkage specification
  1479. shall not precede the first linkage specification for
  1480. that function. A function can be declared without a
  1481. linkage specification after an explicit linkage
  1482. specification has been seen; the linkage explicitly
  1483. specified in the earlier declaration is not affected by
  1484. such a function declaration.
  1485. DR 563 raises the question why the restrictions on
  1486. functions should not also apply to objects. Older
  1487. versions of G++ silently ignore the linkage-specification
  1488. for this example:
  1489. namespace N {
  1490. extern int i;
  1491. extern "C" int i;
  1492. }
  1493. which is clearly wrong. Therefore, we now treat objects
  1494. like functions. */
  1495. if (current_lang_depth () == 0)
  1496. {
  1497. /* There is no explicit linkage-specification, so we use
  1498. the linkage from the previous declaration. */
  1499. if (!DECL_LANG_SPECIFIC (newdecl))
  1500. retrofit_lang_decl (newdecl);
  1501. SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
  1502. }
  1503. else
  1504. {
  1505. error ("conflicting declaration of %q#D with %qL linkage",
  1506. newdecl, DECL_LANGUAGE (newdecl));
  1507. inform (DECL_SOURCE_LOCATION (olddecl),
  1508. "previous declaration with %qL linkage",
  1509. DECL_LANGUAGE (olddecl));
  1510. }
  1511. }
  1512. if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
  1513. ;
  1514. else if (TREE_CODE (olddecl) == FUNCTION_DECL)
  1515. {
  1516. /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
  1517. if (DECL_FUNCTION_MEMBER_P (olddecl)
  1518. && (/* grokfndecl passes member function templates too
  1519. as FUNCTION_DECLs. */
  1520. DECL_TEMPLATE_INFO (olddecl)
  1521. /* C++11 8.3.6/6.
  1522. Default arguments for a member function of a class
  1523. template shall be specified on the initial declaration
  1524. of the member function within the class template. */
  1525. || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
  1526. check_redeclaration_no_default_args (newdecl);
  1527. else
  1528. {
  1529. tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
  1530. tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
  1531. int i = 1;
  1532. for (; t1 && t1 != void_list_node;
  1533. t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
  1534. if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
  1535. {
  1536. if (1 == simple_cst_equal (TREE_PURPOSE (t1),
  1537. TREE_PURPOSE (t2)))
  1538. {
  1539. if (permerror (input_location,
  1540. "default argument given for parameter "
  1541. "%d of %q#D", i, newdecl))
  1542. inform (DECL_SOURCE_LOCATION (olddecl),
  1543. "previous specification in %q#D here",
  1544. olddecl);
  1545. }
  1546. else
  1547. {
  1548. error ("default argument given for parameter %d "
  1549. "of %q#D", i, newdecl);
  1550. inform (DECL_SOURCE_LOCATION (olddecl),
  1551. "previous specification in %q#D here",
  1552. olddecl);
  1553. }
  1554. }
  1555. }
  1556. }
  1557. }
  1558. /* Do not merge an implicit typedef with an explicit one. In:
  1559. class A;
  1560. ...
  1561. typedef class A A __attribute__ ((foo));
  1562. the attribute should apply only to the typedef. */
  1563. if (TREE_CODE (olddecl) == TYPE_DECL
  1564. && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
  1565. || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
  1566. return NULL_TREE;
  1567. /* If new decl is `static' and an `extern' was seen previously,
  1568. warn about it. */
  1569. warn_extern_redeclared_static (newdecl, olddecl);
  1570. if (!validate_constexpr_redeclaration (olddecl, newdecl))
  1571. return error_mark_node;
  1572. /* We have committed to returning 1 at this point. */
  1573. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1574. {
  1575. /* Now that functions must hold information normally held
  1576. by field decls, there is extra work to do so that
  1577. declaration information does not get destroyed during
  1578. definition. */
  1579. if (DECL_VINDEX (olddecl))
  1580. DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
  1581. if (DECL_CONTEXT (olddecl))
  1582. DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
  1583. DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
  1584. DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
  1585. DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
  1586. DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
  1587. DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
  1588. DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
  1589. DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
  1590. DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
  1591. if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
  1592. SET_OVERLOADED_OPERATOR_CODE
  1593. (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
  1594. new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
  1595. /* Optionally warn about more than one declaration for the same
  1596. name, but don't warn about a function declaration followed by a
  1597. definition. */
  1598. if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
  1599. && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
  1600. /* Don't warn about extern decl followed by definition. */
  1601. && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
  1602. /* Don't warn about friends, let add_friend take care of it. */
  1603. && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
  1604. /* Don't warn about declaration followed by specialization. */
  1605. && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
  1606. || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
  1607. {
  1608. if (warning (OPT_Wredundant_decls,
  1609. "redundant redeclaration of %qD in same scope",
  1610. newdecl))
  1611. inform (DECL_SOURCE_LOCATION (olddecl),
  1612. "previous declaration of %qD", olddecl);
  1613. }
  1614. if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
  1615. && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
  1616. {
  1617. if (DECL_DELETED_FN (newdecl))
  1618. {
  1619. error ("deleted definition of %qD", newdecl);
  1620. inform (DECL_SOURCE_LOCATION (olddecl),
  1621. "previous declaration of %qD", olddecl);
  1622. }
  1623. DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
  1624. }
  1625. }
  1626. /* Deal with C++: must preserve virtual function table size. */
  1627. if (TREE_CODE (olddecl) == TYPE_DECL)
  1628. {
  1629. tree newtype = TREE_TYPE (newdecl);
  1630. tree oldtype = TREE_TYPE (olddecl);
  1631. if (newtype != error_mark_node && oldtype != error_mark_node
  1632. && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
  1633. CLASSTYPE_FRIEND_CLASSES (newtype)
  1634. = CLASSTYPE_FRIEND_CLASSES (oldtype);
  1635. DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
  1636. }
  1637. /* Copy all the DECL_... slots specified in the new decl
  1638. except for any that we copy here from the old type. */
  1639. DECL_ATTRIBUTES (newdecl)
  1640. = (*targetm.merge_decl_attributes) (olddecl, newdecl);
  1641. if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
  1642. {
  1643. olddecl_friend = DECL_FRIEND_P (olddecl);
  1644. hidden_friend = (DECL_ANTICIPATED (olddecl)
  1645. && DECL_HIDDEN_FRIEND_P (olddecl)
  1646. && newdecl_is_friend);
  1647. if (!hidden_friend)
  1648. {
  1649. DECL_ANTICIPATED (olddecl) = 0;
  1650. DECL_HIDDEN_FRIEND_P (olddecl) = 0;
  1651. }
  1652. }
  1653. if (TREE_CODE (newdecl) == TEMPLATE_DECL)
  1654. {
  1655. tree old_result;
  1656. tree new_result;
  1657. old_result = DECL_TEMPLATE_RESULT (olddecl);
  1658. new_result = DECL_TEMPLATE_RESULT (newdecl);
  1659. TREE_TYPE (olddecl) = TREE_TYPE (old_result);
  1660. DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
  1661. = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
  1662. DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
  1663. DECL_ATTRIBUTES (old_result)
  1664. = (*targetm.merge_decl_attributes) (old_result, new_result);
  1665. if (DECL_FUNCTION_TEMPLATE_P (newdecl))
  1666. {
  1667. /* Per C++11 8.3.6/4, default arguments cannot be added in later
  1668. declarations of a function template. */
  1669. if (DECL_SOURCE_LOCATION (newdecl)
  1670. != DECL_SOURCE_LOCATION (olddecl))
  1671. check_redeclaration_no_default_args (newdecl);
  1672. check_default_args (newdecl);
  1673. if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
  1674. && DECL_INITIAL (new_result))
  1675. {
  1676. if (DECL_INITIAL (old_result))
  1677. DECL_UNINLINABLE (old_result) = 1;
  1678. else
  1679. DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
  1680. DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
  1681. DECL_NOT_REALLY_EXTERN (old_result)
  1682. = DECL_NOT_REALLY_EXTERN (new_result);
  1683. DECL_INTERFACE_KNOWN (old_result)
  1684. = DECL_INTERFACE_KNOWN (new_result);
  1685. DECL_DECLARED_INLINE_P (old_result)
  1686. = DECL_DECLARED_INLINE_P (new_result);
  1687. DECL_DISREGARD_INLINE_LIMITS (old_result)
  1688. |= DECL_DISREGARD_INLINE_LIMITS (new_result);
  1689. }
  1690. else
  1691. {
  1692. DECL_DECLARED_INLINE_P (old_result)
  1693. |= DECL_DECLARED_INLINE_P (new_result);
  1694. DECL_DISREGARD_INLINE_LIMITS (old_result)
  1695. |= DECL_DISREGARD_INLINE_LIMITS (new_result);
  1696. check_redeclaration_exception_specification (newdecl, olddecl);
  1697. }
  1698. }
  1699. /* If the new declaration is a definition, update the file and
  1700. line information on the declaration, and also make
  1701. the old declaration the same definition. */
  1702. if (DECL_INITIAL (new_result) != NULL_TREE)
  1703. {
  1704. DECL_SOURCE_LOCATION (olddecl)
  1705. = DECL_SOURCE_LOCATION (old_result)
  1706. = DECL_SOURCE_LOCATION (newdecl);
  1707. DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
  1708. if (DECL_FUNCTION_TEMPLATE_P (newdecl))
  1709. {
  1710. tree parm;
  1711. DECL_ARGUMENTS (old_result)
  1712. = DECL_ARGUMENTS (new_result);
  1713. for (parm = DECL_ARGUMENTS (old_result); parm;
  1714. parm = DECL_CHAIN (parm))
  1715. DECL_CONTEXT (parm) = old_result;
  1716. }
  1717. }
  1718. return olddecl;
  1719. }
  1720. if (types_match)
  1721. {
  1722. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1723. check_redeclaration_exception_specification (newdecl, olddecl);
  1724. /* Automatically handles default parameters. */
  1725. tree oldtype = TREE_TYPE (olddecl);
  1726. tree newtype;
  1727. /* For typedefs use the old type, as the new type's DECL_NAME points
  1728. at newdecl, which will be ggc_freed. */
  1729. if (TREE_CODE (newdecl) == TYPE_DECL)
  1730. newtype = oldtype;
  1731. else
  1732. /* Merge the data types specified in the two decls. */
  1733. newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
  1734. if (VAR_P (newdecl))
  1735. {
  1736. DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
  1737. DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
  1738. DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
  1739. |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
  1740. DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
  1741. |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
  1742. /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
  1743. if (DECL_LANG_SPECIFIC (olddecl)
  1744. && CP_DECL_THREADPRIVATE_P (olddecl))
  1745. {
  1746. /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
  1747. if (!DECL_LANG_SPECIFIC (newdecl))
  1748. retrofit_lang_decl (newdecl);
  1749. CP_DECL_THREADPRIVATE_P (newdecl) = 1;
  1750. }
  1751. }
  1752. TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
  1753. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1754. check_default_args (newdecl);
  1755. /* Lay the type out, unless already done. */
  1756. if (! same_type_p (newtype, oldtype)
  1757. && TREE_TYPE (newdecl) != error_mark_node
  1758. && !(processing_template_decl && uses_template_parms (newdecl)))
  1759. layout_type (TREE_TYPE (newdecl));
  1760. if ((VAR_P (newdecl)
  1761. || TREE_CODE (newdecl) == PARM_DECL
  1762. || TREE_CODE (newdecl) == RESULT_DECL
  1763. || TREE_CODE (newdecl) == FIELD_DECL
  1764. || TREE_CODE (newdecl) == TYPE_DECL)
  1765. && !(processing_template_decl && uses_template_parms (newdecl)))
  1766. layout_decl (newdecl, 0);
  1767. /* Merge the type qualifiers. */
  1768. if (TREE_READONLY (newdecl))
  1769. TREE_READONLY (olddecl) = 1;
  1770. if (TREE_THIS_VOLATILE (newdecl))
  1771. TREE_THIS_VOLATILE (olddecl) = 1;
  1772. if (TREE_NOTHROW (newdecl))
  1773. TREE_NOTHROW (olddecl) = 1;
  1774. /* Merge deprecatedness. */
  1775. if (TREE_DEPRECATED (newdecl))
  1776. TREE_DEPRECATED (olddecl) = 1;
  1777. /* Preserve function specific target and optimization options */
  1778. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1779. {
  1780. if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
  1781. && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
  1782. DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
  1783. = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
  1784. if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
  1785. && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
  1786. DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
  1787. = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
  1788. }
  1789. /* Merge the initialization information. */
  1790. if (DECL_INITIAL (newdecl) == NULL_TREE
  1791. && DECL_INITIAL (olddecl) != NULL_TREE)
  1792. {
  1793. DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
  1794. DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
  1795. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1796. {
  1797. DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
  1798. DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
  1799. }
  1800. }
  1801. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1802. {
  1803. DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
  1804. |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
  1805. DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
  1806. TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
  1807. TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
  1808. DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
  1809. DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
  1810. DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
  1811. TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
  1812. DECL_LOOPING_CONST_OR_PURE_P (newdecl)
  1813. |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
  1814. /* Keep the old RTL. */
  1815. COPY_DECL_RTL (olddecl, newdecl);
  1816. }
  1817. else if (VAR_P (newdecl)
  1818. && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
  1819. {
  1820. /* Keep the old RTL. We cannot keep the old RTL if the old
  1821. declaration was for an incomplete object and the new
  1822. declaration is not since many attributes of the RTL will
  1823. change. */
  1824. COPY_DECL_RTL (olddecl, newdecl);
  1825. }
  1826. }
  1827. /* If cannot merge, then use the new type and qualifiers,
  1828. and don't preserve the old rtl. */
  1829. else
  1830. {
  1831. /* Clean out any memory we had of the old declaration. */
  1832. tree oldstatic = value_member (olddecl, static_aggregates);
  1833. if (oldstatic)
  1834. TREE_VALUE (oldstatic) = error_mark_node;
  1835. TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
  1836. TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
  1837. TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
  1838. TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
  1839. }
  1840. /* Merge the storage class information. */
  1841. merge_weak (newdecl, olddecl);
  1842. DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
  1843. TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
  1844. TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
  1845. if (! DECL_EXTERNAL (olddecl))
  1846. DECL_EXTERNAL (newdecl) = 0;
  1847. new_template_info = NULL_TREE;
  1848. if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
  1849. {
  1850. bool new_redefines_gnu_inline = false;
  1851. if (new_defines_function
  1852. && ((DECL_INTERFACE_KNOWN (olddecl)
  1853. && TREE_CODE (olddecl) == FUNCTION_DECL)
  1854. || (TREE_CODE (olddecl) == TEMPLATE_DECL
  1855. && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
  1856. == FUNCTION_DECL))))
  1857. {
  1858. tree fn = olddecl;
  1859. if (TREE_CODE (fn) == TEMPLATE_DECL)
  1860. fn = DECL_TEMPLATE_RESULT (olddecl);
  1861. new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
  1862. }
  1863. if (!new_redefines_gnu_inline)
  1864. {
  1865. DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
  1866. DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
  1867. DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
  1868. }
  1869. DECL_TEMPLATE_INSTANTIATED (newdecl)
  1870. |= DECL_TEMPLATE_INSTANTIATED (olddecl);
  1871. DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
  1872. /* If the OLDDECL is an instantiation and/or specialization,
  1873. then the NEWDECL must be too. But, it may not yet be marked
  1874. as such if the caller has created NEWDECL, but has not yet
  1875. figured out that it is a redeclaration. */
  1876. if (!DECL_USE_TEMPLATE (newdecl))
  1877. DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
  1878. /* Don't really know how much of the language-specific
  1879. values we should copy from old to new. */
  1880. DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
  1881. DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
  1882. DECL_INITIALIZED_IN_CLASS_P (newdecl)
  1883. |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
  1884. if (LANG_DECL_HAS_MIN (newdecl))
  1885. {
  1886. DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
  1887. DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
  1888. if (DECL_TEMPLATE_INFO (newdecl))
  1889. {
  1890. new_template_info = DECL_TEMPLATE_INFO (newdecl);
  1891. if (DECL_TEMPLATE_INSTANTIATION (olddecl)
  1892. && DECL_TEMPLATE_SPECIALIZATION (newdecl))
  1893. /* Remember the presence of explicit specialization args. */
  1894. TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
  1895. = TINFO_USED_TEMPLATE_ID (new_template_info);
  1896. }
  1897. DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
  1898. }
  1899. /* Only functions have these fields. */
  1900. if (DECL_DECLARES_FUNCTION_P (newdecl))
  1901. {
  1902. DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
  1903. DECL_BEFRIENDING_CLASSES (newdecl)
  1904. = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
  1905. DECL_BEFRIENDING_CLASSES (olddecl));
  1906. /* DECL_THUNKS is only valid for virtual functions,
  1907. otherwise it is a DECL_FRIEND_CONTEXT. */
  1908. if (DECL_VIRTUAL_P (newdecl))
  1909. SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
  1910. }
  1911. /* Only variables have this field. */
  1912. else if (VAR_P (newdecl)
  1913. && VAR_HAD_UNKNOWN_BOUND (olddecl))
  1914. SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
  1915. }
  1916. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  1917. {
  1918. tree parm;
  1919. /* Merge parameter attributes. */
  1920. tree oldarg, newarg;
  1921. for (oldarg = DECL_ARGUMENTS(olddecl),
  1922. newarg = DECL_ARGUMENTS(newdecl);
  1923. oldarg && newarg;
  1924. oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
  1925. DECL_ATTRIBUTES (newarg)
  1926. = (*targetm.merge_decl_attributes) (oldarg, newarg);
  1927. DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
  1928. }
  1929. if (DECL_TEMPLATE_INSTANTIATION (olddecl)
  1930. && !DECL_TEMPLATE_INSTANTIATION (newdecl))
  1931. {
  1932. /* If newdecl is not a specialization, then it is not a
  1933. template-related function at all. And that means that we
  1934. should have exited above, returning 0. */
  1935. gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
  1936. if (DECL_ODR_USED (olddecl))
  1937. /* From [temp.expl.spec]:
  1938. If a template, a member template or the member of a class
  1939. template is explicitly specialized then that
  1940. specialization shall be declared before the first use of
  1941. that specialization that would cause an implicit
  1942. instantiation to take place, in every translation unit in
  1943. which such a use occurs. */
  1944. error ("explicit specialization of %qD after first use",
  1945. olddecl);
  1946. SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
  1947. DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
  1948. && DECL_DECLARED_INLINE_P (newdecl));
  1949. /* Don't propagate visibility from the template to the
  1950. specialization here. We'll do that in determine_visibility if
  1951. appropriate. */
  1952. DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
  1953. /* [temp.expl.spec/14] We don't inline explicit specialization
  1954. just because the primary template says so. */
  1955. /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
  1956. the always_inline attribute. */
  1957. if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
  1958. && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
  1959. {
  1960. if (DECL_DECLARED_INLINE_P (newdecl))
  1961. DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
  1962. else
  1963. DECL_ATTRIBUTES (newdecl)
  1964. = remove_attribute ("always_inline",
  1965. DECL_ATTRIBUTES (newdecl));
  1966. }
  1967. }
  1968. else if (new_defines_function && DECL_INITIAL (olddecl))
  1969. {
  1970. /* Never inline re-defined extern inline functions.
  1971. FIXME: this could be better handled by keeping both
  1972. function as separate declarations. */
  1973. DECL_UNINLINABLE (newdecl) = 1;
  1974. }
  1975. else
  1976. {
  1977. if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
  1978. DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
  1979. DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
  1980. DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
  1981. = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
  1982. DECL_DISREGARD_INLINE_LIMITS (newdecl)
  1983. = DECL_DISREGARD_INLINE_LIMITS (olddecl)
  1984. = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
  1985. || DECL_DISREGARD_INLINE_LIMITS (olddecl));
  1986. }
  1987. /* Preserve abstractness on cloned [cd]tors. */
  1988. DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
  1989. /* Update newdecl's parms to point at olddecl. */
  1990. for (parm = DECL_ARGUMENTS (newdecl); parm;
  1991. parm = DECL_CHAIN (parm))
  1992. DECL_CONTEXT (parm) = olddecl;
  1993. if (! types_match)
  1994. {
  1995. SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
  1996. COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
  1997. COPY_DECL_RTL (newdecl, olddecl);
  1998. }
  1999. if (! types_match || new_defines_function)
  2000. {
  2001. /* These need to be copied so that the names are available.
  2002. Note that if the types do match, we'll preserve inline
  2003. info and other bits, but if not, we won't. */
  2004. DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
  2005. DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
  2006. }
  2007. /* If redeclaring a builtin function, it stays built in
  2008. if newdecl is a gnu_inline definition, or if newdecl is just
  2009. a declaration. */
  2010. if (DECL_BUILT_IN (olddecl)
  2011. && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
  2012. {
  2013. DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
  2014. DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
  2015. /* If we're keeping the built-in definition, keep the rtl,
  2016. regardless of declaration matches. */
  2017. COPY_DECL_RTL (olddecl, newdecl);
  2018. if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
  2019. {
  2020. enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
  2021. switch (fncode)
  2022. {
  2023. /* If a compatible prototype of these builtin functions
  2024. is seen, assume the runtime implements it with the
  2025. expected semantics. */
  2026. case BUILT_IN_STPCPY:
  2027. if (builtin_decl_explicit_p (fncode))
  2028. set_builtin_decl_implicit_p (fncode, true);
  2029. break;
  2030. default:
  2031. if (builtin_decl_explicit_p (fncode))
  2032. set_builtin_decl_declared_p (fncode, true);
  2033. break;
  2034. }
  2035. }
  2036. }
  2037. if (new_defines_function)
  2038. /* If defining a function declared with other language
  2039. linkage, use the previously declared language linkage. */
  2040. SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
  2041. else if (types_match)
  2042. {
  2043. DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
  2044. /* Don't clear out the arguments if we're just redeclaring a
  2045. function. */
  2046. if (DECL_ARGUMENTS (olddecl))
  2047. DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
  2048. }
  2049. }
  2050. else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
  2051. NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
  2052. /* Now preserve various other info from the definition. */
  2053. TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
  2054. TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
  2055. DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
  2056. COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
  2057. /* Warn about conflicting visibility specifications. */
  2058. if (DECL_VISIBILITY_SPECIFIED (olddecl)
  2059. && DECL_VISIBILITY_SPECIFIED (newdecl)
  2060. && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
  2061. {
  2062. if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
  2063. "%qD: visibility attribute ignored because it "
  2064. "conflicts with previous declaration", newdecl))
  2065. inform (DECL_SOURCE_LOCATION (olddecl),
  2066. "previous declaration of %qD", olddecl);
  2067. }
  2068. /* Choose the declaration which specified visibility. */
  2069. if (DECL_VISIBILITY_SPECIFIED (olddecl))
  2070. {
  2071. DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
  2072. DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
  2073. }
  2074. /* Init priority used to be merged from newdecl to olddecl by the memcpy,
  2075. so keep this behavior. */
  2076. if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
  2077. {
  2078. SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
  2079. DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
  2080. }
  2081. /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
  2082. if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
  2083. {
  2084. DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
  2085. DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
  2086. }
  2087. DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
  2088. if (TREE_CODE (newdecl) == FIELD_DECL)
  2089. DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
  2090. /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
  2091. with that from NEWDECL below. */
  2092. if (DECL_LANG_SPECIFIC (olddecl))
  2093. {
  2094. gcc_assert (DECL_LANG_SPECIFIC (olddecl)
  2095. != DECL_LANG_SPECIFIC (newdecl));
  2096. ggc_free (DECL_LANG_SPECIFIC (olddecl));
  2097. }
  2098. /* Merge the USED information. */
  2099. if (TREE_USED (olddecl))
  2100. TREE_USED (newdecl) = 1;
  2101. else if (TREE_USED (newdecl))
  2102. TREE_USED (olddecl) = 1;
  2103. if (VAR_P (newdecl))
  2104. {
  2105. if (DECL_READ_P (olddecl))
  2106. DECL_READ_P (newdecl) = 1;
  2107. else if (DECL_READ_P (newdecl))
  2108. DECL_READ_P (olddecl) = 1;
  2109. }
  2110. if (DECL_PRESERVE_P (olddecl))
  2111. DECL_PRESERVE_P (newdecl) = 1;
  2112. else if (DECL_PRESERVE_P (newdecl))
  2113. DECL_PRESERVE_P (olddecl) = 1;
  2114. /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
  2115. to olddecl and deleted. */
  2116. if (TREE_CODE (newdecl) == FUNCTION_DECL
  2117. && DECL_FUNCTION_VERSIONED (olddecl))
  2118. {
  2119. /* Set the flag for newdecl so that it gets copied to olddecl. */
  2120. DECL_FUNCTION_VERSIONED (newdecl) = 1;
  2121. /* newdecl will be purged after copying to olddecl and is no longer
  2122. a version. */
  2123. cgraph_node::delete_function_version (newdecl);
  2124. }
  2125. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  2126. {
  2127. int function_size;
  2128. struct symtab_node *snode = symtab_node::get (olddecl);
  2129. function_size = sizeof (struct tree_decl_common);
  2130. memcpy ((char *) olddecl + sizeof (struct tree_common),
  2131. (char *) newdecl + sizeof (struct tree_common),
  2132. function_size - sizeof (struct tree_common));
  2133. memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
  2134. (char *) newdecl + sizeof (struct tree_decl_common),
  2135. sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
  2136. /* Preserve symtab node mapping. */
  2137. olddecl->decl_with_vis.symtab_node = snode;
  2138. if (new_template_info)
  2139. /* If newdecl is a template instantiation, it is possible that
  2140. the following sequence of events has occurred:
  2141. o A friend function was declared in a class template. The
  2142. class template was instantiated.
  2143. o The instantiation of the friend declaration was
  2144. recorded on the instantiation list, and is newdecl.
  2145. o Later, however, instantiate_class_template called pushdecl
  2146. on the newdecl to perform name injection. But, pushdecl in
  2147. turn called duplicate_decls when it discovered that another
  2148. declaration of a global function with the same name already
  2149. existed.
  2150. o Here, in duplicate_decls, we decided to clobber newdecl.
  2151. If we're going to do that, we'd better make sure that
  2152. olddecl, and not newdecl, is on the list of
  2153. instantiations so that if we try to do the instantiation
  2154. again we won't get the clobbered declaration. */
  2155. reregister_specialization (newdecl,
  2156. new_template_info,
  2157. olddecl);
  2158. }
  2159. else
  2160. {
  2161. size_t size = tree_code_size (TREE_CODE (newdecl));
  2162. memcpy ((char *) olddecl + sizeof (struct tree_common),
  2163. (char *) newdecl + sizeof (struct tree_common),
  2164. sizeof (struct tree_decl_common) - sizeof (struct tree_common));
  2165. switch (TREE_CODE (newdecl))
  2166. {
  2167. case LABEL_DECL:
  2168. case VAR_DECL:
  2169. case RESULT_DECL:
  2170. case PARM_DECL:
  2171. case FIELD_DECL:
  2172. case TYPE_DECL:
  2173. case CONST_DECL:
  2174. {
  2175. struct symtab_node *snode = NULL;
  2176. if (TREE_CODE (newdecl) == VAR_DECL
  2177. && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) || DECL_EXTERNAL (olddecl)))
  2178. snode = symtab_node::get (olddecl);
  2179. memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
  2180. (char *) newdecl + sizeof (struct tree_decl_common),
  2181. size - sizeof (struct tree_decl_common)
  2182. + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
  2183. if (TREE_CODE (newdecl) == VAR_DECL)
  2184. olddecl->decl_with_vis.symtab_node = snode;
  2185. }
  2186. break;
  2187. default:
  2188. memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
  2189. (char *) newdecl + sizeof (struct tree_decl_common),
  2190. sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
  2191. + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
  2192. break;
  2193. }
  2194. }
  2195. if (TREE_CODE (newdecl) == FUNCTION_DECL
  2196. || TREE_CODE (newdecl) == VAR_DECL)
  2197. {
  2198. if (DECL_EXTERNAL (olddecl)
  2199. || TREE_PUBLIC (olddecl)
  2200. || TREE_STATIC (olddecl))
  2201. {
  2202. /* Merge the section attribute.
  2203. We want to issue an error if the sections conflict but that must be
  2204. done later in decl_attributes since we are called before attributes
  2205. are assigned. */
  2206. if (DECL_SECTION_NAME (newdecl) != NULL)
  2207. set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
  2208. if (DECL_ONE_ONLY (newdecl))
  2209. {
  2210. struct symtab_node *oldsym, *newsym;
  2211. if (TREE_CODE (olddecl) == FUNCTION_DECL)
  2212. oldsym = cgraph_node::get_create (olddecl);
  2213. else
  2214. oldsym = varpool_node::get_create (olddecl);
  2215. newsym = symtab_node::get (newdecl);
  2216. oldsym->set_comdat_group (newsym->get_comdat_group ());
  2217. }
  2218. }
  2219. if (TREE_CODE (newdecl) == VAR_DECL
  2220. && DECL_THREAD_LOCAL_P (newdecl))
  2221. set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
  2222. }
  2223. DECL_UID (olddecl) = olddecl_uid;
  2224. if (olddecl_friend)
  2225. DECL_FRIEND_P (olddecl) = 1;
  2226. if (hidden_friend)
  2227. {
  2228. DECL_ANTICIPATED (olddecl) = 1;
  2229. DECL_HIDDEN_FRIEND_P (olddecl) = 1;
  2230. }
  2231. /* NEWDECL contains the merged attribute lists.
  2232. Update OLDDECL to be the same. */
  2233. DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
  2234. /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
  2235. so that encode_section_info has a chance to look at the new decl
  2236. flags and attributes. */
  2237. if (DECL_RTL_SET_P (olddecl)
  2238. && (TREE_CODE (olddecl) == FUNCTION_DECL
  2239. || (VAR_P (olddecl)
  2240. && TREE_STATIC (olddecl))))
  2241. make_decl_rtl (olddecl);
  2242. /* The NEWDECL will no longer be needed. Because every out-of-class
  2243. declaration of a member results in a call to duplicate_decls,
  2244. freeing these nodes represents in a significant savings.
  2245. Before releasing the node, be sore to remove function from symbol
  2246. table that might have been inserted there to record comdat group.
  2247. Be sure to however do not free DECL_STRUCT_FUNCTION becuase this
  2248. structure is shared in between newdecl and oldecl. */
  2249. if (TREE_CODE (newdecl) == FUNCTION_DECL)
  2250. DECL_STRUCT_FUNCTION (newdecl) = NULL;
  2251. if (TREE_CODE (newdecl) == FUNCTION_DECL
  2252. || TREE_CODE (newdecl) == VAR_DECL)
  2253. {
  2254. struct symtab_node *snode = symtab_node::get (newdecl);
  2255. if (snode)
  2256. snode->remove ();
  2257. }
  2258. ggc_free (newdecl);
  2259. return olddecl;
  2260. }
  2261. /* Return zero if the declaration NEWDECL is valid
  2262. when the declaration OLDDECL (assumed to be for the same name)
  2263. has already been seen.
  2264. Otherwise return an error message format string with a %s
  2265. where the identifier should go. */
  2266. static const char *
  2267. redeclaration_error_message (tree newdecl, tree olddecl)
  2268. {
  2269. if (TREE_CODE (newdecl) == TYPE_DECL)
  2270. {
  2271. /* Because C++ can put things into name space for free,
  2272. constructs like "typedef struct foo { ... } foo"
  2273. would look like an erroneous redeclaration. */
  2274. if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
  2275. return NULL;
  2276. else
  2277. return G_("redefinition of %q#D");
  2278. }
  2279. else if (TREE_CODE (newdecl) == FUNCTION_DECL)
  2280. {
  2281. /* If this is a pure function, its olddecl will actually be
  2282. the original initialization to `0' (which we force to call
  2283. abort()). Don't complain about redefinition in this case. */
  2284. if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
  2285. && DECL_INITIAL (olddecl) == NULL_TREE)
  2286. return NULL;
  2287. /* If both functions come from different namespaces, this is not
  2288. a redeclaration - this is a conflict with a used function. */
  2289. if (DECL_NAMESPACE_SCOPE_P (olddecl)
  2290. && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
  2291. && ! decls_match (olddecl, newdecl))
  2292. return G_("%qD conflicts with used function");
  2293. /* We'll complain about linkage mismatches in
  2294. warn_extern_redeclared_static. */
  2295. /* Defining the same name twice is no good. */
  2296. if (DECL_INITIAL (olddecl) != NULL_TREE
  2297. && DECL_INITIAL (newdecl) != NULL_TREE)
  2298. {
  2299. if (DECL_NAME (olddecl) == NULL_TREE)
  2300. return G_("%q#D not declared in class");
  2301. else if (!GNU_INLINE_P (olddecl)
  2302. || GNU_INLINE_P (newdecl))
  2303. return G_("redefinition of %q#D");
  2304. }
  2305. if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
  2306. {
  2307. bool olda = GNU_INLINE_P (olddecl);
  2308. bool newa = GNU_INLINE_P (newdecl);
  2309. if (olda != newa)
  2310. {
  2311. if (newa)
  2312. return G_("%q+D redeclared inline with "
  2313. "%<gnu_inline%> attribute");
  2314. else
  2315. return G_("%q+D redeclared inline without "
  2316. "%<gnu_inline%> attribute");
  2317. }
  2318. }
  2319. check_abi_tag_redeclaration
  2320. (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
  2321. lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
  2322. return NULL;
  2323. }
  2324. else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
  2325. {
  2326. tree nt, ot;
  2327. if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
  2328. {
  2329. if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
  2330. && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
  2331. return G_("redefinition of %q#D");
  2332. return NULL;
  2333. }
  2334. if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
  2335. || (DECL_TEMPLATE_RESULT (newdecl)
  2336. == DECL_TEMPLATE_RESULT (olddecl)))
  2337. return NULL;
  2338. nt = DECL_TEMPLATE_RESULT (newdecl);
  2339. if (DECL_TEMPLATE_INFO (nt))
  2340. nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
  2341. ot = DECL_TEMPLATE_RESULT (olddecl);
  2342. if (DECL_TEMPLATE_INFO (ot))
  2343. ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
  2344. if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
  2345. && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
  2346. return G_("redefinition of %q#D");
  2347. if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
  2348. {
  2349. bool olda = GNU_INLINE_P (ot);
  2350. bool newa = GNU_INLINE_P (nt);
  2351. if (olda != newa)
  2352. {
  2353. if (newa)
  2354. return G_("%q+D redeclared inline with "
  2355. "%<gnu_inline%> attribute");
  2356. else
  2357. return G_("%q+D redeclared inline without "
  2358. "%<gnu_inline%> attribute");
  2359. }
  2360. }
  2361. /* Core issue #226 (C++0x):
  2362. If a friend function template declaration specifies a
  2363. default template-argument, that declaration shall be a
  2364. definition and shall be the only declaration of the
  2365. function template in the translation unit. */
  2366. if ((cxx_dialect != cxx98)
  2367. && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
  2368. && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
  2369. /*is_primary=*/true,
  2370. /*is_partial=*/false,
  2371. /*is_friend_decl=*/2))
  2372. return G_("redeclaration of friend %q#D "
  2373. "may not have default template arguments");
  2374. return NULL;
  2375. }
  2376. else if (VAR_P (newdecl)
  2377. && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
  2378. && (! DECL_LANG_SPECIFIC (olddecl)
  2379. || ! CP_DECL_THREADPRIVATE_P (olddecl)
  2380. || DECL_THREAD_LOCAL_P (newdecl)))
  2381. {
  2382. /* Only variables can be thread-local, and all declarations must
  2383. agree on this property. */
  2384. if (DECL_THREAD_LOCAL_P (newdecl))
  2385. return G_("thread-local declaration of %q#D follows "
  2386. "non-thread-local declaration");
  2387. else
  2388. return G_("non-thread-local declaration of %q#D follows "
  2389. "thread-local declaration");
  2390. }
  2391. else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
  2392. {
  2393. /* The objects have been declared at namespace scope. If either
  2394. is a member of an anonymous union, then this is an invalid
  2395. redeclaration. For example:
  2396. int i;
  2397. union { int i; };
  2398. is invalid. */
  2399. if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
  2400. || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
  2401. return G_("redeclaration of %q#D");
  2402. /* If at least one declaration is a reference, there is no
  2403. conflict. For example:
  2404. int i = 3;
  2405. extern int i;
  2406. is valid. */
  2407. if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
  2408. return NULL;
  2409. /* Reject two definitions. */
  2410. return G_("redefinition of %q#D");
  2411. }
  2412. else
  2413. {
  2414. /* Objects declared with block scope: */
  2415. /* Reject two definitions, and reject a definition
  2416. together with an external reference. */
  2417. if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
  2418. return G_("redeclaration of %q#D");
  2419. return NULL;
  2420. }
  2421. }
  2422. /* Hash and equality functions for the named_label table. */
  2423. hashval_t
  2424. named_label_hasher::hash (named_label_entry *ent)
  2425. {
  2426. return DECL_UID (ent->label_decl);
  2427. }
  2428. bool
  2429. named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
  2430. {
  2431. return a->label_decl == b->label_decl;
  2432. }
  2433. /* Create a new label, named ID. */
  2434. static tree
  2435. make_label_decl (tree id, int local_p)
  2436. {
  2437. struct named_label_entry *ent;
  2438. tree decl;
  2439. decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
  2440. DECL_CONTEXT (decl) = current_function_decl;
  2441. DECL_MODE (decl) = VOIDmode;
  2442. C_DECLARED_LABEL_FLAG (decl) = local_p;
  2443. /* Say where one reference is to the label, for the sake of the
  2444. error if it is not defined. */
  2445. DECL_SOURCE_LOCATION (decl) = input_location;
  2446. /* Record the fact that this identifier is bound to this label. */
  2447. SET_IDENTIFIER_LABEL_VALUE (id, decl);
  2448. /* Create the label htab for the function on demand. */
  2449. if (!named_labels)
  2450. named_labels = hash_table<named_label_hasher>::create_ggc (13);
  2451. /* Record this label on the list of labels used in this function.
  2452. We do this before calling make_label_decl so that we get the
  2453. IDENTIFIER_LABEL_VALUE before the new label is declared. */
  2454. ent = ggc_cleared_alloc<named_label_entry> ();
  2455. ent->label_decl = decl;
  2456. named_label_entry **slot = named_labels->find_slot (ent, INSERT);
  2457. gcc_assert (*slot == NULL);
  2458. *slot = ent;
  2459. return decl;
  2460. }
  2461. /* Look for a label named ID in the current function. If one cannot
  2462. be found, create one. (We keep track of used, but undefined,
  2463. labels, and complain about them at the end of a function.) */
  2464. static tree
  2465. lookup_label_1 (tree id)
  2466. {
  2467. tree decl;
  2468. /* You can't use labels at global scope. */
  2469. if (current_function_decl == NULL_TREE)
  2470. {
  2471. error ("label %qE referenced outside of any function", id);
  2472. return NULL_TREE;
  2473. }
  2474. /* See if we've already got this label. */
  2475. decl = IDENTIFIER_LABEL_VALUE (id);
  2476. if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
  2477. return decl;
  2478. decl = make_label_decl (id, /*local_p=*/0);
  2479. return decl;
  2480. }
  2481. /* Wrapper for lookup_label_1. */
  2482. tree
  2483. lookup_label (tree id)
  2484. {
  2485. tree ret;
  2486. bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
  2487. ret = lookup_label_1 (id);
  2488. timevar_cond_stop (TV_NAME_LOOKUP, subtime);
  2489. return ret;
  2490. }
  2491. /* Declare a local label named ID. */
  2492. tree
  2493. declare_local_label (tree id)
  2494. {
  2495. tree decl;
  2496. cp_label_binding bind;
  2497. /* Add a new entry to the SHADOWED_LABELS list so that when we leave
  2498. this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
  2499. bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
  2500. decl = make_label_decl (id, /*local_p=*/1);
  2501. bind.label = decl;
  2502. vec_safe_push (current_binding_level->shadowed_labels, bind);
  2503. return decl;
  2504. }
  2505. /* Returns nonzero if it is ill-formed to jump past the declaration of
  2506. DECL. Returns 2 if it's also a real problem. */
  2507. static int
  2508. decl_jump_unsafe (tree decl)
  2509. {
  2510. /* [stmt.dcl]/3: A program that jumps from a point where a local variable
  2511. with automatic storage duration is not in scope to a point where it is
  2512. in scope is ill-formed unless the variable has scalar type, class type
  2513. with a trivial default constructor and a trivial destructor, a
  2514. cv-qualified version of one of these types, or an array of one of the
  2515. preceding types and is declared without an initializer (8.5). */
  2516. tree type = TREE_TYPE (decl);
  2517. if (!VAR_P (decl) || TREE_STATIC (decl)
  2518. || type == error_mark_node)
  2519. return 0;
  2520. if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
  2521. || variably_modified_type_p (type, NULL_TREE))
  2522. return 2;
  2523. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  2524. return 1;
  2525. return 0;
  2526. }
  2527. /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
  2528. static bool
  2529. identify_goto (tree decl, const location_t *locus)
  2530. {
  2531. bool complained = (decl
  2532. ? permerror (input_location, "jump to label %qD", decl)
  2533. : permerror (input_location, "jump to case label"));
  2534. if (complained && locus)
  2535. inform (*locus, " from here");
  2536. return complained;
  2537. }
  2538. /* Check that a single previously seen jump to a newly defined label
  2539. is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
  2540. the jump context; NAMES are the names in scope in LEVEL at the jump
  2541. context; LOCUS is the source position of the jump or 0. Returns
  2542. true if all is well. */
  2543. static bool
  2544. check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
  2545. bool exited_omp, const location_t *locus)
  2546. {
  2547. cp_binding_level *b;
  2548. bool identified = false, complained = false;
  2549. bool saw_eh = false, saw_omp = false;
  2550. if (exited_omp)
  2551. {
  2552. complained = identify_goto (decl, locus);
  2553. if (complained)
  2554. inform (input_location, " exits OpenMP structured block");
  2555. identified = saw_omp = true;
  2556. }
  2557. for (b = current_binding_level; b ; b = b->level_chain)
  2558. {
  2559. tree new_decls, old_decls = (b == level ? names : NULL_TREE);
  2560. for (new_decls = b->names; new_decls != old_decls;
  2561. new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
  2562. : TREE_CHAIN (new_decls)))
  2563. {
  2564. int problem = decl_jump_unsafe (new_decls);
  2565. if (! problem)
  2566. continue;
  2567. if (!identified)
  2568. {
  2569. complained = identify_goto (decl, locus);
  2570. identified = true;
  2571. }
  2572. if (complained)
  2573. {
  2574. if (problem > 1)
  2575. inform (input_location,
  2576. " crosses initialization of %q+#D", new_decls);
  2577. else
  2578. inform (input_location, " enters scope of %q+#D which has "
  2579. "non-trivial destructor", new_decls);
  2580. }
  2581. }
  2582. if (b == level)
  2583. break;
  2584. if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
  2585. {
  2586. if (!identified)
  2587. {
  2588. complained = identify_goto (decl, locus);
  2589. identified = true;
  2590. }
  2591. if (complained)
  2592. {
  2593. if (b->kind == sk_try)
  2594. inform (input_location, " enters try block");
  2595. else
  2596. inform (input_location, " enters catch block");
  2597. }
  2598. saw_eh = true;
  2599. }
  2600. if (b->kind == sk_omp && !saw_omp)
  2601. {
  2602. if (!identified)
  2603. {
  2604. complained = identify_goto (decl, locus);
  2605. identified = true;
  2606. }
  2607. if (complained)
  2608. inform (input_location, " enters OpenMP structured block");
  2609. saw_omp = true;
  2610. }
  2611. }
  2612. return !identified;
  2613. }
  2614. static void
  2615. check_previous_goto (tree decl, struct named_label_use_entry *use)
  2616. {
  2617. check_previous_goto_1 (decl, use->binding_level,
  2618. use->names_in_scope, use->in_omp_scope,
  2619. &use->o_goto_locus);
  2620. }
  2621. static bool
  2622. check_switch_goto (cp_binding_level* level)
  2623. {
  2624. return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
  2625. }
  2626. /* Check that a new jump to a label DECL is OK. Called by
  2627. finish_goto_stmt. */
  2628. void
  2629. check_goto (tree decl)
  2630. {
  2631. struct named_label_entry *ent, dummy;
  2632. bool saw_catch = false, identified = false, complained = false;
  2633. tree bad;
  2634. unsigned ix;
  2635. /* We can't know where a computed goto is jumping.
  2636. So we assume that it's OK. */
  2637. if (TREE_CODE (decl) != LABEL_DECL)
  2638. return;
  2639. /* We didn't record any information about this label when we created it,
  2640. and there's not much point since it's trivial to analyze as a return. */
  2641. if (decl == cdtor_label)
  2642. return;
  2643. dummy.label_decl = decl;
  2644. ent = named_labels->find (&dummy);
  2645. gcc_assert (ent != NULL);
  2646. /* If the label hasn't been defined yet, defer checking. */
  2647. if (! DECL_INITIAL (decl))
  2648. {
  2649. struct named_label_use_entry *new_use;
  2650. /* Don't bother creating another use if the last goto had the
  2651. same data, and will therefore create the same set of errors. */
  2652. if (ent->uses
  2653. && ent->uses->names_in_scope == current_binding_level->names)
  2654. return;
  2655. new_use = ggc_alloc<named_label_use_entry> ();
  2656. new_use->binding_level = current_binding_level;
  2657. new_use->names_in_scope = current_binding_level->names;
  2658. new_use->o_goto_locus = input_location;
  2659. new_use->in_omp_scope = false;
  2660. new_use->next = ent->uses;
  2661. ent->uses = new_use;
  2662. return;
  2663. }
  2664. if (ent->in_try_scope || ent->in_catch_scope
  2665. || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
  2666. {
  2667. complained = permerror (input_location, "jump to label %q+D", decl);
  2668. if (complained)
  2669. inform (input_location, " from here");
  2670. identified = true;
  2671. }
  2672. FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
  2673. {
  2674. int u = decl_jump_unsafe (bad);
  2675. if (u > 1 && DECL_ARTIFICIAL (bad))
  2676. {
  2677. /* Can't skip init of __exception_info. */
  2678. if (complained)
  2679. inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
  2680. saw_catch = true;
  2681. }
  2682. else if (complained)
  2683. {
  2684. if (u > 1)
  2685. inform (input_location, " skips initialization of %q+#D", bad);
  2686. else
  2687. inform (input_location, " enters scope of %q+#D which has "
  2688. "non-trivial destructor", bad);
  2689. }
  2690. }
  2691. if (complained)
  2692. {
  2693. if (ent->in_try_scope)
  2694. inform (input_location, " enters try block");
  2695. else if (ent->in_catch_scope && !saw_catch)
  2696. inform (input_location, " enters catch block");
  2697. }
  2698. if (ent->in_omp_scope)
  2699. {
  2700. if (complained)
  2701. inform (input_location, " enters OpenMP structured block");
  2702. }
  2703. else if (flag_openmp)
  2704. {
  2705. cp_binding_level *b;
  2706. for (b = current_binding_level; b ; b = b->level_chain)
  2707. {
  2708. if (b == ent->binding_level)
  2709. break;
  2710. if (b->kind == sk_omp)
  2711. {
  2712. if (!identified)
  2713. {
  2714. complained = permerror (input_location,
  2715. "jump to label %q+D", decl);
  2716. if (complained)
  2717. inform (input_location, " from here");
  2718. identified = true;
  2719. }
  2720. if (complained)
  2721. inform (input_location, " exits OpenMP structured block");
  2722. break;
  2723. }
  2724. }
  2725. }
  2726. }
  2727. /* Check that a return is ok wrt OpenMP structured blocks.
  2728. Called by finish_return_stmt. Returns true if all is well. */
  2729. bool
  2730. check_omp_return (void)
  2731. {
  2732. cp_binding_level *b;
  2733. for (b = current_binding_level; b ; b = b->level_chain)
  2734. if (b->kind == sk_omp)
  2735. {
  2736. error ("invalid exit from OpenMP structured block");
  2737. return false;
  2738. }
  2739. else if (b->kind == sk_function_parms)
  2740. break;
  2741. return true;
  2742. }
  2743. /* Define a label, specifying the location in the source file.
  2744. Return the LABEL_DECL node for the label. */
  2745. static tree
  2746. define_label_1 (location_t location, tree name)
  2747. {
  2748. struct named_label_entry *ent, dummy;
  2749. cp_binding_level *p;
  2750. tree decl;
  2751. decl = lookup_label (name);
  2752. dummy.label_decl = decl;
  2753. ent = named_labels->find (&dummy);
  2754. gcc_assert (ent != NULL);
  2755. /* After labels, make any new cleanups in the function go into their
  2756. own new (temporary) binding contour. */
  2757. for (p = current_binding_level;
  2758. p->kind != sk_function_parms;
  2759. p = p->level_chain)
  2760. p->more_cleanups_ok = 0;
  2761. if (name == get_identifier ("wchar_t"))
  2762. permerror (input_location, "label named wchar_t");
  2763. if (DECL_INITIAL (decl) != NULL_TREE)
  2764. {
  2765. error ("duplicate label %qD", decl);
  2766. return error_mark_node;
  2767. }
  2768. else
  2769. {
  2770. struct named_label_use_entry *use;
  2771. /* Mark label as having been defined. */
  2772. DECL_INITIAL (decl) = error_mark_node;
  2773. /* Say where in the source. */
  2774. DECL_SOURCE_LOCATION (decl) = location;
  2775. ent->binding_level = current_binding_level;
  2776. ent->names_in_scope = current_binding_level->names;
  2777. for (use = ent->uses; use ; use = use->next)
  2778. check_previous_goto (decl, use);
  2779. ent->uses = NULL;
  2780. }
  2781. return decl;
  2782. }
  2783. /* Wrapper for define_label_1. */
  2784. tree
  2785. define_label (location_t location, tree name)
  2786. {
  2787. tree ret;
  2788. bool running = timevar_cond_start (TV_NAME_LOOKUP);
  2789. ret = define_label_1 (location, name);
  2790. timevar_cond_stop (TV_NAME_LOOKUP, running);
  2791. return ret;
  2792. }
  2793. struct cp_switch
  2794. {
  2795. cp_binding_level *level;
  2796. struct cp_switch *next;
  2797. /* The SWITCH_STMT being built. */
  2798. tree switch_stmt;
  2799. /* A splay-tree mapping the low element of a case range to the high
  2800. element, or NULL_TREE if there is no high element. Used to
  2801. determine whether or not a new case label duplicates an old case
  2802. label. We need a tree, rather than simply a hash table, because
  2803. of the GNU case range extension. */
  2804. splay_tree cases;
  2805. };
  2806. /* A stack of the currently active switch statements. The innermost
  2807. switch statement is on the top of the stack. There is no need to
  2808. mark the stack for garbage collection because it is only active
  2809. during the processing of the body of a function, and we never
  2810. collect at that point. */
  2811. static struct cp_switch *switch_stack;
  2812. /* Called right after a switch-statement condition is parsed.
  2813. SWITCH_STMT is the switch statement being parsed. */
  2814. void
  2815. push_switch (tree switch_stmt)
  2816. {
  2817. struct cp_switch *p = XNEW (struct cp_switch);
  2818. p->level = current_binding_level;
  2819. p->next = switch_stack;
  2820. p->switch_stmt = switch_stmt;
  2821. p->cases = splay_tree_new (case_compare, NULL, NULL);
  2822. switch_stack = p;
  2823. }
  2824. void
  2825. pop_switch (void)
  2826. {
  2827. struct cp_switch *cs = switch_stack;
  2828. location_t switch_location;
  2829. /* Emit warnings as needed. */
  2830. switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
  2831. if (!processing_template_decl)
  2832. c_do_switch_warnings (cs->cases, switch_location,
  2833. SWITCH_STMT_TYPE (cs->switch_stmt),
  2834. SWITCH_STMT_COND (cs->switch_stmt));
  2835. splay_tree_delete (cs->cases);
  2836. switch_stack = switch_stack->next;
  2837. free (cs);
  2838. }
  2839. /* Convert a case constant VALUE in a switch to the type TYPE of the switch
  2840. condition. Note that if TYPE and VALUE are already integral we don't
  2841. really do the conversion because the language-independent
  2842. warning/optimization code will work better that way. */
  2843. static tree
  2844. case_conversion (tree type, tree value)
  2845. {
  2846. if (value == NULL_TREE)
  2847. return value;
  2848. if (cxx_dialect >= cxx11
  2849. && (SCOPED_ENUM_P (type)
  2850. || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
  2851. {
  2852. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
  2853. type = type_promotes_to (type);
  2854. value = (perform_implicit_conversion_flags
  2855. (type, value, tf_warning_or_error,
  2856. LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
  2857. }
  2858. return cxx_constant_value (value);
  2859. }
  2860. /* Note that we've seen a definition of a case label, and complain if this
  2861. is a bad place for one. */
  2862. tree
  2863. finish_case_label (location_t loc, tree low_value, tree high_value)
  2864. {
  2865. tree cond, r;
  2866. cp_binding_level *p;
  2867. tree type;
  2868. if (processing_template_decl)
  2869. {
  2870. tree label;
  2871. /* For templates, just add the case label; we'll do semantic
  2872. analysis at instantiation-time. */
  2873. label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
  2874. return add_stmt (build_case_label (low_value, high_value, label));
  2875. }
  2876. /* Find the condition on which this switch statement depends. */
  2877. cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
  2878. if (cond && TREE_CODE (cond) == TREE_LIST)
  2879. cond = TREE_VALUE (cond);
  2880. if (!check_switch_goto (switch_stack->level))
  2881. return error_mark_node;
  2882. type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
  2883. low_value = case_conversion (type, low_value);
  2884. high_value = case_conversion (type, high_value);
  2885. r = c_add_case_label (loc, switch_stack->cases, cond, type,
  2886. low_value, high_value);
  2887. /* After labels, make any new cleanups in the function go into their
  2888. own new (temporary) binding contour. */
  2889. for (p = current_binding_level;
  2890. p->kind != sk_function_parms;
  2891. p = p->level_chain)
  2892. p->more_cleanups_ok = 0;
  2893. return r;
  2894. }
  2895. struct typename_info {
  2896. tree scope;
  2897. tree name;
  2898. tree template_id;
  2899. bool enum_p;
  2900. bool class_p;
  2901. };
  2902. struct typename_hasher : ggc_hasher<tree>
  2903. {
  2904. typedef typename_info *compare_type;
  2905. /* Hash a TYPENAME_TYPE. */
  2906. static hashval_t
  2907. hash (tree t)
  2908. {
  2909. hashval_t hash;
  2910. hash = (htab_hash_pointer (TYPE_CONTEXT (t))
  2911. ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
  2912. return hash;
  2913. }
  2914. /* Compare two TYPENAME_TYPEs. */
  2915. static bool
  2916. equal (tree t1, const typename_info *t2)
  2917. {
  2918. return (TYPE_IDENTIFIER (t1) == t2->name
  2919. && TYPE_CONTEXT (t1) == t2->scope
  2920. && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
  2921. && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
  2922. && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
  2923. }
  2924. };
  2925. /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
  2926. the type of `T', NAME is the IDENTIFIER_NODE for `t'.
  2927. Returns the new TYPENAME_TYPE. */
  2928. static GTY (()) hash_table<typename_hasher> *typename_htab;
  2929. static tree
  2930. build_typename_type (tree context, tree name, tree fullname,
  2931. enum tag_types tag_type)
  2932. {
  2933. tree t;
  2934. tree d;
  2935. typename_info ti;
  2936. tree *e;
  2937. hashval_t hash;
  2938. if (typename_htab == NULL)
  2939. typename_htab = hash_table<typename_hasher>::create_ggc (61);
  2940. ti.scope = FROB_CONTEXT (context);
  2941. ti.name = name;
  2942. ti.template_id = fullname;
  2943. ti.enum_p = tag_type == enum_type;
  2944. ti.class_p = (tag_type == class_type
  2945. || tag_type == record_type
  2946. || tag_type == union_type);
  2947. hash = (htab_hash_pointer (ti.scope)
  2948. ^ htab_hash_pointer (ti.name));
  2949. /* See if we already have this type. */
  2950. e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
  2951. if (*e)
  2952. t = *e;
  2953. else
  2954. {
  2955. /* Build the TYPENAME_TYPE. */
  2956. t = cxx_make_type (TYPENAME_TYPE);
  2957. TYPE_CONTEXT (t) = ti.scope;
  2958. TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
  2959. TYPENAME_IS_ENUM_P (t) = ti.enum_p;
  2960. TYPENAME_IS_CLASS_P (t) = ti.class_p;
  2961. /* Build the corresponding TYPE_DECL. */
  2962. d = build_decl (input_location, TYPE_DECL, name, t);
  2963. TYPE_NAME (TREE_TYPE (d)) = d;
  2964. TYPE_STUB_DECL (TREE_TYPE (d)) = d;
  2965. DECL_CONTEXT (d) = FROB_CONTEXT (context);
  2966. DECL_ARTIFICIAL (d) = 1;
  2967. /* Store it in the hash table. */
  2968. *e = t;
  2969. /* TYPENAME_TYPEs must always be compared structurally, because
  2970. they may or may not resolve down to another type depending on
  2971. the currently open classes. */
  2972. SET_TYPE_STRUCTURAL_EQUALITY (t);
  2973. }
  2974. return t;
  2975. }
  2976. /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
  2977. provided to name the type. Returns an appropriate type, unless an
  2978. error occurs, in which case error_mark_node is returned. If we
  2979. locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
  2980. return that, rather than the _TYPE it corresponds to, in other
  2981. cases we look through the type decl. If TF_ERROR is set, complain
  2982. about errors, otherwise be quiet. */
  2983. tree
  2984. make_typename_type (tree context, tree name, enum tag_types tag_type,
  2985. tsubst_flags_t complain)
  2986. {
  2987. tree fullname;
  2988. tree t;
  2989. bool want_template;
  2990. if (name == error_mark_node
  2991. || context == NULL_TREE
  2992. || context == error_mark_node)
  2993. return error_mark_node;
  2994. if (TYPE_P (name))
  2995. {
  2996. if (!(TYPE_LANG_SPECIFIC (name)
  2997. && (CLASSTYPE_IS_TEMPLATE (name)
  2998. || CLASSTYPE_USE_TEMPLATE (name))))
  2999. name = TYPE_IDENTIFIER (name);
  3000. else
  3001. /* Create a TEMPLATE_ID_EXPR for the type. */
  3002. name = build_nt (TEMPLATE_ID_EXPR,
  3003. CLASSTYPE_TI_TEMPLATE (name),
  3004. CLASSTYPE_TI_ARGS (name));
  3005. }
  3006. else if (TREE_CODE (name) == TYPE_DECL)
  3007. name = DECL_NAME (name);
  3008. fullname = name;
  3009. if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  3010. {
  3011. name = TREE_OPERAND (name, 0);
  3012. if (DECL_TYPE_TEMPLATE_P (name))
  3013. name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
  3014. if (TREE_CODE (name) != IDENTIFIER_NODE)
  3015. {
  3016. if (complain & tf_error)
  3017. error ("%qD is not a type", name);
  3018. return error_mark_node;
  3019. }
  3020. }
  3021. if (TREE_CODE (name) == TEMPLATE_DECL)
  3022. {
  3023. if (complain & tf_error)
  3024. error ("%qD used without template parameters", name);
  3025. return error_mark_node;
  3026. }
  3027. gcc_assert (identifier_p (name));
  3028. gcc_assert (TYPE_P (context));
  3029. if (!MAYBE_CLASS_TYPE_P (context))
  3030. {
  3031. if (complain & tf_error)
  3032. error ("%q#T is not a class", context);
  3033. return error_mark_node;
  3034. }
  3035. /* When the CONTEXT is a dependent type, NAME could refer to a
  3036. dependent base class of CONTEXT. But look inside it anyway
  3037. if CONTEXT is a currently open scope, in case it refers to a
  3038. member of the current instantiation or a non-dependent base;
  3039. lookup will stop when we hit a dependent base. */
  3040. if (!dependent_scope_p (context))
  3041. /* We should only set WANT_TYPE when we're a nested typename type.
  3042. Then we can give better diagnostics if we find a non-type. */
  3043. t = lookup_field (context, name, 2, /*want_type=*/true);
  3044. else
  3045. t = NULL_TREE;
  3046. if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
  3047. return build_typename_type (context, name, fullname, tag_type);
  3048. want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
  3049. if (!t)
  3050. {
  3051. if (complain & tf_error)
  3052. error (want_template ? G_("no class template named %q#T in %q#T")
  3053. : G_("no type named %q#T in %q#T"), name, context);
  3054. return error_mark_node;
  3055. }
  3056. /* Pull out the template from an injected-class-name (or multiple). */
  3057. if (want_template)
  3058. t = maybe_get_template_decl_from_type_decl (t);
  3059. if (TREE_CODE (t) == TREE_LIST)
  3060. {
  3061. if (complain & tf_error)
  3062. {
  3063. error ("lookup of %qT in %qT is ambiguous", name, context);
  3064. print_candidates (t);
  3065. }
  3066. return error_mark_node;
  3067. }
  3068. if (want_template && !DECL_TYPE_TEMPLATE_P (t))
  3069. {
  3070. if (complain & tf_error)
  3071. error ("%<typename %T::%D%> names %q#T, which is not a class template",
  3072. context, name, t);
  3073. return error_mark_node;
  3074. }
  3075. if (!want_template && TREE_CODE (t) != TYPE_DECL)
  3076. {
  3077. if (complain & tf_error)
  3078. error ("%<typename %T::%D%> names %q#T, which is not a type",
  3079. context, name, t);
  3080. return error_mark_node;
  3081. }
  3082. if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
  3083. return error_mark_node;
  3084. /* If we are currently parsing a template and if T is a typedef accessed
  3085. through CONTEXT then we need to remember and check access of T at
  3086. template instantiation time. */
  3087. add_typedef_to_current_template_for_access_check (t, context, input_location);
  3088. if (want_template)
  3089. return lookup_template_class (t, TREE_OPERAND (fullname, 1),
  3090. NULL_TREE, context,
  3091. /*entering_scope=*/0,
  3092. complain | tf_user);
  3093. if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
  3094. t = TREE_TYPE (t);
  3095. maybe_record_typedef_use (t);
  3096. return t;
  3097. }
  3098. /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
  3099. can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
  3100. in which case error_mark_node is returned.
  3101. If PARM_LIST is non-NULL, also make sure that the template parameter
  3102. list of TEMPLATE_DECL matches.
  3103. If COMPLAIN zero, don't complain about any errors that occur. */
  3104. tree
  3105. make_unbound_class_template (tree context, tree name, tree parm_list,
  3106. tsubst_flags_t complain)
  3107. {
  3108. tree t;
  3109. tree d;
  3110. if (TYPE_P (name))
  3111. name = TYPE_IDENTIFIER (name);
  3112. else if (DECL_P (name))
  3113. name = DECL_NAME (name);
  3114. gcc_assert (identifier_p (name));
  3115. if (!dependent_type_p (context)
  3116. || currently_open_class (context))
  3117. {
  3118. tree tmpl = NULL_TREE;
  3119. if (MAYBE_CLASS_TYPE_P (context))
  3120. tmpl = lookup_field (context, name, 0, false);
  3121. if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
  3122. tmpl = maybe_get_template_decl_from_type_decl (tmpl);
  3123. if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
  3124. {
  3125. if (complain & tf_error)
  3126. error ("no class template named %q#T in %q#T", name, context);
  3127. return error_mark_node;
  3128. }
  3129. if (parm_list
  3130. && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
  3131. {
  3132. if (complain & tf_error)
  3133. {
  3134. error ("template parameters do not match template %qD", tmpl);
  3135. inform (DECL_SOURCE_LOCATION (tmpl),
  3136. "%qD declared here", tmpl);
  3137. }
  3138. return error_mark_node;
  3139. }
  3140. if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
  3141. complain))
  3142. return error_mark_node;
  3143. return tmpl;
  3144. }
  3145. /* Build the UNBOUND_CLASS_TEMPLATE. */
  3146. t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
  3147. TYPE_CONTEXT (t) = FROB_CONTEXT (context);
  3148. TREE_TYPE (t) = NULL_TREE;
  3149. SET_TYPE_STRUCTURAL_EQUALITY (t);
  3150. /* Build the corresponding TEMPLATE_DECL. */
  3151. d = build_decl (input_location, TEMPLATE_DECL, name, t);
  3152. TYPE_NAME (TREE_TYPE (d)) = d;
  3153. TYPE_STUB_DECL (TREE_TYPE (d)) = d;
  3154. DECL_CONTEXT (d) = FROB_CONTEXT (context);
  3155. DECL_ARTIFICIAL (d) = 1;
  3156. DECL_TEMPLATE_PARMS (d) = parm_list;
  3157. return t;
  3158. }
  3159. /* Push the declarations of builtin types into the namespace.
  3160. RID_INDEX is the index of the builtin type in the array
  3161. RID_POINTERS. NAME is the name used when looking up the builtin
  3162. type. TYPE is the _TYPE node for the builtin type. */
  3163. void
  3164. record_builtin_type (enum rid rid_index,
  3165. const char* name,
  3166. tree type)
  3167. {
  3168. tree rname = NULL_TREE, tname = NULL_TREE;
  3169. tree tdecl = NULL_TREE;
  3170. if ((int) rid_index < (int) RID_MAX)
  3171. rname = ridpointers[(int) rid_index];
  3172. if (name)
  3173. tname = get_identifier (name);
  3174. /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
  3175. eliminated. Built-in types should not be looked up name; their
  3176. names are keywords that the parser can recognize. However, there
  3177. is code in c-common.c that uses identifier_global_value to look
  3178. up built-in types by name. */
  3179. if (tname)
  3180. {
  3181. tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
  3182. DECL_ARTIFICIAL (tdecl) = 1;
  3183. SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
  3184. }
  3185. if (rname)
  3186. {
  3187. if (!tdecl)
  3188. {
  3189. tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
  3190. DECL_ARTIFICIAL (tdecl) = 1;
  3191. }
  3192. SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
  3193. }
  3194. if (!TYPE_NAME (type))
  3195. TYPE_NAME (type) = tdecl;
  3196. if (tdecl)
  3197. debug_hooks->type_decl (tdecl, 0);
  3198. }
  3199. /* Record one of the standard Java types.
  3200. * Declare it as having the given NAME.
  3201. * If SIZE > 0, it is the size of one of the integral types;
  3202. * otherwise it is the negative of the size of one of the other types. */
  3203. static tree
  3204. record_builtin_java_type (const char* name, int size)
  3205. {
  3206. tree type, decl;
  3207. if (size > 0)
  3208. {
  3209. type = build_nonstandard_integer_type (size, 0);
  3210. type = build_distinct_type_copy (type);
  3211. }
  3212. else if (size > -32)
  3213. {
  3214. tree stype;
  3215. /* "__java_char" or ""__java_boolean". */
  3216. type = build_nonstandard_integer_type (-size, 1);
  3217. type = build_distinct_type_copy (type);
  3218. /* Get the signed type cached and attached to the unsigned type,
  3219. so it doesn't get garbage-collected at "random" times,
  3220. causing potential codegen differences out of different UIDs
  3221. and different alias set numbers. */
  3222. stype = build_nonstandard_integer_type (-size, 0);
  3223. stype = build_distinct_type_copy (stype);
  3224. TREE_CHAIN (type) = stype;
  3225. /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
  3226. }
  3227. else
  3228. { /* "__java_float" or ""__java_double". */
  3229. type = make_node (REAL_TYPE);
  3230. TYPE_PRECISION (type) = - size;
  3231. layout_type (type);
  3232. }
  3233. record_builtin_type (RID_MAX, name, type);
  3234. decl = TYPE_NAME (type);
  3235. /* Suppress generate debug symbol entries for these types,
  3236. since for normal C++ they are just clutter.
  3237. However, push_lang_context undoes this if extern "Java" is seen. */
  3238. DECL_IGNORED_P (decl) = 1;
  3239. TYPE_FOR_JAVA (type) = 1;
  3240. return type;
  3241. }
  3242. /* Push a type into the namespace so that the back ends ignore it. */
  3243. static void
  3244. record_unknown_type (tree type, const char* name)
  3245. {
  3246. tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
  3247. TYPE_DECL, get_identifier (name), type));
  3248. /* Make sure the "unknown type" typedecl gets ignored for debug info. */
  3249. DECL_IGNORED_P (decl) = 1;
  3250. TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
  3251. TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
  3252. TYPE_ALIGN (type) = 1;
  3253. TYPE_USER_ALIGN (type) = 0;
  3254. SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
  3255. }
  3256. /* A string for which we should create an IDENTIFIER_NODE at
  3257. startup. */
  3258. typedef struct predefined_identifier
  3259. {
  3260. /* The name of the identifier. */
  3261. const char *const name;
  3262. /* The place where the IDENTIFIER_NODE should be stored. */
  3263. tree *const node;
  3264. /* Nonzero if this is the name of a constructor or destructor. */
  3265. const int ctor_or_dtor_p;
  3266. } predefined_identifier;
  3267. /* Create all the predefined identifiers. */
  3268. static void
  3269. initialize_predefined_identifiers (void)
  3270. {
  3271. const predefined_identifier *pid;
  3272. /* A table of identifiers to create at startup. */
  3273. static const predefined_identifier predefined_identifiers[] = {
  3274. { "C++", &lang_name_cplusplus, 0 },
  3275. { "C", &lang_name_c, 0 },
  3276. { "Java", &lang_name_java, 0 },
  3277. /* Some of these names have a trailing space so that it is
  3278. impossible for them to conflict with names written by users. */
  3279. { "__ct ", &ctor_identifier, 1 },
  3280. { "__base_ctor ", &base_ctor_identifier, 1 },
  3281. { "__comp_ctor ", &complete_ctor_identifier, 1 },
  3282. { "__dt ", &dtor_identifier, 1 },
  3283. { "__comp_dtor ", &complete_dtor_identifier, 1 },
  3284. { "__base_dtor ", &base_dtor_identifier, 1 },
  3285. { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
  3286. { IN_CHARGE_NAME, &in_charge_identifier, 0 },
  3287. { "nelts", &nelts_identifier, 0 },
  3288. { THIS_NAME, &this_identifier, 0 },
  3289. { VTABLE_DELTA_NAME, &delta_identifier, 0 },
  3290. { VTABLE_PFN_NAME, &pfn_identifier, 0 },
  3291. { "_vptr", &vptr_identifier, 0 },
  3292. { "__vtt_parm", &vtt_parm_identifier, 0 },
  3293. { "::", &global_scope_name, 0 },
  3294. { "std", &std_identifier, 0 },
  3295. { NULL, NULL, 0 }
  3296. };
  3297. for (pid = predefined_identifiers; pid->name; ++pid)
  3298. {
  3299. *pid->node = get_identifier (pid->name);
  3300. if (pid->ctor_or_dtor_p)
  3301. IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
  3302. }
  3303. }
  3304. /* Create the predefined scalar types of C,
  3305. and some nodes representing standard constants (0, 1, (void *)0).
  3306. Initialize the global binding level.
  3307. Make definitions for built-in primitive functions. */
  3308. void
  3309. cxx_init_decl_processing (void)
  3310. {
  3311. tree void_ftype;
  3312. tree void_ftype_ptr;
  3313. /* Create all the identifiers we need. */
  3314. initialize_predefined_identifiers ();
  3315. /* Create the global variables. */
  3316. push_to_top_level ();
  3317. current_function_decl = NULL_TREE;
  3318. current_binding_level = NULL;
  3319. /* Enter the global namespace. */
  3320. gcc_assert (global_namespace == NULL_TREE);
  3321. global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
  3322. void_type_node);
  3323. DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
  3324. debug_hooks->register_main_translation_unit
  3325. (DECL_CONTEXT (global_namespace));
  3326. TREE_PUBLIC (global_namespace) = 1;
  3327. begin_scope (sk_namespace, global_namespace);
  3328. if (flag_visibility_ms_compat)
  3329. default_visibility = VISIBILITY_HIDDEN;
  3330. /* Initially, C. */
  3331. current_lang_name = lang_name_c;
  3332. /* Create the `std' namespace. */
  3333. push_namespace (std_identifier);
  3334. std_node = current_namespace;
  3335. pop_namespace ();
  3336. c_common_nodes_and_builtins ();
  3337. java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
  3338. java_short_type_node = record_builtin_java_type ("__java_short", 16);
  3339. java_int_type_node = record_builtin_java_type ("__java_int", 32);
  3340. java_long_type_node = record_builtin_java_type ("__java_long", 64);
  3341. java_float_type_node = record_builtin_java_type ("__java_float", -32);
  3342. java_double_type_node = record_builtin_java_type ("__java_double", -64);
  3343. java_char_type_node = record_builtin_java_type ("__java_char", -16);
  3344. java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
  3345. integer_two_node = build_int_cst (NULL_TREE, 2);
  3346. record_builtin_type (RID_BOOL, "bool", boolean_type_node);
  3347. truthvalue_type_node = boolean_type_node;
  3348. truthvalue_false_node = boolean_false_node;
  3349. truthvalue_true_node = boolean_true_node;
  3350. empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
  3351. noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
  3352. noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
  3353. #if 0
  3354. record_builtin_type (RID_MAX, NULL, string_type_node);
  3355. #endif
  3356. delta_type_node = ptrdiff_type_node;
  3357. vtable_index_type = ptrdiff_type_node;
  3358. vtt_parm_type = build_pointer_type (const_ptr_type_node);
  3359. void_ftype = build_function_type_list (void_type_node, NULL_TREE);
  3360. void_ftype_ptr = build_function_type_list (void_type_node,
  3361. ptr_type_node, NULL_TREE);
  3362. void_ftype_ptr
  3363. = build_exception_variant (void_ftype_ptr, empty_except_spec);
  3364. /* C++ extensions */
  3365. unknown_type_node = make_node (LANG_TYPE);
  3366. record_unknown_type (unknown_type_node, "unknown type");
  3367. /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
  3368. TREE_TYPE (unknown_type_node) = unknown_type_node;
  3369. /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
  3370. result. */
  3371. TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
  3372. TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
  3373. init_list_type_node = make_node (LANG_TYPE);
  3374. record_unknown_type (init_list_type_node, "init list");
  3375. {
  3376. /* Make sure we get a unique function type, so we can give
  3377. its pointer type a name. (This wins for gdb.) */
  3378. tree vfunc_type = make_node (FUNCTION_TYPE);
  3379. TREE_TYPE (vfunc_type) = integer_type_node;
  3380. TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
  3381. layout_type (vfunc_type);
  3382. vtable_entry_type = build_pointer_type (vfunc_type);
  3383. }
  3384. record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
  3385. vtbl_type_node
  3386. = build_cplus_array_type (vtable_entry_type, NULL_TREE);
  3387. layout_type (vtbl_type_node);
  3388. vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
  3389. record_builtin_type (RID_MAX, NULL, vtbl_type_node);
  3390. vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
  3391. layout_type (vtbl_ptr_type_node);
  3392. record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
  3393. push_namespace (get_identifier ("__cxxabiv1"));
  3394. abi_node = current_namespace;
  3395. pop_namespace ();
  3396. global_type_node = make_node (LANG_TYPE);
  3397. record_unknown_type (global_type_node, "global type");
  3398. /* Now, C++. */
  3399. current_lang_name = lang_name_cplusplus;
  3400. {
  3401. tree newattrs, extvisattr;
  3402. tree newtype, deltype;
  3403. tree ptr_ftype_sizetype;
  3404. tree new_eh_spec;
  3405. ptr_ftype_sizetype
  3406. = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
  3407. if (cxx_dialect == cxx98)
  3408. {
  3409. tree bad_alloc_id;
  3410. tree bad_alloc_type_node;
  3411. tree bad_alloc_decl;
  3412. push_namespace (std_identifier);
  3413. bad_alloc_id = get_identifier ("bad_alloc");
  3414. bad_alloc_type_node = make_class_type (RECORD_TYPE);
  3415. TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
  3416. bad_alloc_decl
  3417. = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
  3418. DECL_CONTEXT (bad_alloc_decl) = current_namespace;
  3419. pop_namespace ();
  3420. new_eh_spec
  3421. = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
  3422. }
  3423. else
  3424. new_eh_spec = noexcept_false_spec;
  3425. /* Ensure attribs.c is initialized. */
  3426. init_attributes ();
  3427. extvisattr = build_tree_list (get_identifier ("externally_visible"),
  3428. NULL_TREE);
  3429. newattrs = tree_cons (get_identifier ("alloc_size"),
  3430. build_tree_list (NULL_TREE, integer_one_node),
  3431. extvisattr);
  3432. newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
  3433. newtype = build_exception_variant (newtype, new_eh_spec);
  3434. deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
  3435. deltype = build_exception_variant (deltype, empty_except_spec);
  3436. tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
  3437. DECL_IS_MALLOC (opnew) = 1;
  3438. DECL_IS_OPERATOR_NEW (opnew) = 1;
  3439. opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
  3440. DECL_IS_MALLOC (opnew) = 1;
  3441. DECL_IS_OPERATOR_NEW (opnew) = 1;
  3442. push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
  3443. push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
  3444. if (flag_sized_deallocation)
  3445. {
  3446. /* Also push the sized deallocation variants:
  3447. void operator delete(void*, std::size_t) throw();
  3448. void operator delete[](void*, std::size_t) throw(); */
  3449. tree void_ftype_ptr_size
  3450. = build_function_type_list (void_type_node, ptr_type_node,
  3451. size_type_node, NULL_TREE);
  3452. deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
  3453. extvisattr);
  3454. deltype = build_exception_variant (deltype, empty_except_spec);
  3455. push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
  3456. push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
  3457. }
  3458. nullptr_type_node = make_node (NULLPTR_TYPE);
  3459. TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
  3460. TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
  3461. TYPE_UNSIGNED (nullptr_type_node) = 1;
  3462. TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
  3463. if (abi_version_at_least (9))
  3464. TYPE_ALIGN (nullptr_type_node) = GET_MODE_ALIGNMENT (ptr_mode);
  3465. SET_TYPE_MODE (nullptr_type_node, ptr_mode);
  3466. record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
  3467. nullptr_node = build_int_cst (nullptr_type_node, 0);
  3468. }
  3469. abort_fndecl
  3470. = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
  3471. ECF_NORETURN | ECF_NOTHROW);
  3472. /* Perform other language dependent initializations. */
  3473. init_class_processing ();
  3474. init_rtti_processing ();
  3475. init_template_processing ();
  3476. if (flag_exceptions)
  3477. init_exception_processing ();
  3478. if (! supports_one_only ())
  3479. flag_weak = 0;
  3480. make_fname_decl = cp_make_fname_decl;
  3481. start_fname_decls ();
  3482. /* Show we use EH for cleanups. */
  3483. if (flag_exceptions)
  3484. using_eh_for_cleanups ();
  3485. }
  3486. /* Generate an initializer for a function naming variable from
  3487. NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
  3488. filled in with the type of the init. */
  3489. tree
  3490. cp_fname_init (const char* name, tree *type_p)
  3491. {
  3492. tree domain = NULL_TREE;
  3493. tree type;
  3494. tree init = NULL_TREE;
  3495. size_t length = 0;
  3496. if (name)
  3497. {
  3498. length = strlen (name);
  3499. domain = build_index_type (size_int (length));
  3500. init = build_string (length + 1, name);
  3501. }
  3502. type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
  3503. type = build_cplus_array_type (type, domain);
  3504. *type_p = type;
  3505. if (init)
  3506. TREE_TYPE (init) = type;
  3507. else
  3508. init = error_mark_node;
  3509. return init;
  3510. }
  3511. /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
  3512. the decl, LOC is the location to give the decl, NAME is the
  3513. initialization string and TYPE_DEP indicates whether NAME depended
  3514. on the type of the function. We make use of that to detect
  3515. __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
  3516. at the point of first use, so we mustn't push the decl now. */
  3517. static tree
  3518. cp_make_fname_decl (location_t loc, tree id, int type_dep)
  3519. {
  3520. const char *const name = (type_dep && processing_template_decl
  3521. ? NULL : fname_as_string (type_dep));
  3522. tree type;
  3523. tree init = cp_fname_init (name, &type);
  3524. tree decl = build_decl (loc, VAR_DECL, id, type);
  3525. if (name)
  3526. free (CONST_CAST (char *, name));
  3527. /* As we're using pushdecl_with_scope, we must set the context. */
  3528. DECL_CONTEXT (decl) = current_function_decl;
  3529. TREE_STATIC (decl) = 1;
  3530. TREE_READONLY (decl) = 1;
  3531. DECL_ARTIFICIAL (decl) = 1;
  3532. TREE_USED (decl) = 1;
  3533. if (current_function_decl)
  3534. {
  3535. cp_binding_level *b = current_binding_level;
  3536. if (b->kind == sk_function_parms)
  3537. return error_mark_node;
  3538. while (b->level_chain->kind != sk_function_parms)
  3539. b = b->level_chain;
  3540. pushdecl_with_scope (decl, b, /*is_friend=*/false);
  3541. cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
  3542. LOOKUP_ONLYCONVERTING);
  3543. }
  3544. else
  3545. {
  3546. DECL_THIS_STATIC (decl) = true;
  3547. pushdecl_top_level_and_finish (decl, init);
  3548. }
  3549. return decl;
  3550. }
  3551. static tree
  3552. builtin_function_1 (tree decl, tree context, bool is_global)
  3553. {
  3554. tree id = DECL_NAME (decl);
  3555. const char *name = IDENTIFIER_POINTER (id);
  3556. retrofit_lang_decl (decl);
  3557. DECL_ARTIFICIAL (decl) = 1;
  3558. SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
  3559. SET_DECL_LANGUAGE (decl, lang_c);
  3560. /* Runtime library routines are, by definition, available in an
  3561. external shared object. */
  3562. DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
  3563. DECL_VISIBILITY_SPECIFIED (decl) = 1;
  3564. DECL_CONTEXT (decl) = context;
  3565. if (is_global)
  3566. pushdecl_top_level (decl);
  3567. else
  3568. pushdecl (decl);
  3569. /* A function in the user's namespace should have an explicit
  3570. declaration before it is used. Mark the built-in function as
  3571. anticipated but not actually declared. */
  3572. if (name[0] != '_' || name[1] != '_')
  3573. DECL_ANTICIPATED (decl) = 1;
  3574. else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
  3575. {
  3576. size_t len = strlen (name);
  3577. /* Treat __*_chk fortification functions as anticipated as well,
  3578. unless they are __builtin_*. */
  3579. if (len > strlen ("___chk")
  3580. && memcmp (name + len - strlen ("_chk"),
  3581. "_chk", strlen ("_chk") + 1) == 0)
  3582. DECL_ANTICIPATED (decl) = 1;
  3583. }
  3584. return decl;
  3585. }
  3586. tree
  3587. cxx_builtin_function (tree decl)
  3588. {
  3589. tree id = DECL_NAME (decl);
  3590. const char *name = IDENTIFIER_POINTER (id);
  3591. /* All builtins that don't begin with an '_' should additionally
  3592. go in the 'std' namespace. */
  3593. if (name[0] != '_')
  3594. {
  3595. tree decl2 = copy_node(decl);
  3596. push_namespace (std_identifier);
  3597. builtin_function_1 (decl2, std_node, false);
  3598. pop_namespace ();
  3599. }
  3600. return builtin_function_1 (decl, NULL_TREE, false);
  3601. }
  3602. /* Like cxx_builtin_function, but guarantee the function is added to the global
  3603. scope. This is to allow function specific options to add new machine
  3604. dependent builtins when the target ISA changes via attribute((target(...)))
  3605. which saves space on program startup if the program does not use non-generic
  3606. ISAs. */
  3607. tree
  3608. cxx_builtin_function_ext_scope (tree decl)
  3609. {
  3610. tree id = DECL_NAME (decl);
  3611. const char *name = IDENTIFIER_POINTER (id);
  3612. /* All builtins that don't begin with an '_' should additionally
  3613. go in the 'std' namespace. */
  3614. if (name[0] != '_')
  3615. {
  3616. tree decl2 = copy_node(decl);
  3617. push_namespace (std_identifier);
  3618. builtin_function_1 (decl2, std_node, true);
  3619. pop_namespace ();
  3620. }
  3621. return builtin_function_1 (decl, NULL_TREE, true);
  3622. }
  3623. /* Generate a FUNCTION_DECL with the typical flags for a runtime library
  3624. function. Not called directly. */
  3625. static tree
  3626. build_library_fn (tree name, enum tree_code operator_code, tree type,
  3627. int ecf_flags)
  3628. {
  3629. tree fn = build_lang_decl (FUNCTION_DECL, name, type);
  3630. DECL_EXTERNAL (fn) = 1;
  3631. TREE_PUBLIC (fn) = 1;
  3632. DECL_ARTIFICIAL (fn) = 1;
  3633. SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
  3634. SET_DECL_LANGUAGE (fn, lang_c);
  3635. /* Runtime library routines are, by definition, available in an
  3636. external shared object. */
  3637. DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
  3638. DECL_VISIBILITY_SPECIFIED (fn) = 1;
  3639. set_call_expr_flags (fn, ecf_flags);
  3640. return fn;
  3641. }
  3642. /* Returns the _DECL for a library function with C++ linkage. */
  3643. static tree
  3644. build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
  3645. int ecf_flags)
  3646. {
  3647. tree fn = build_library_fn (name, operator_code, type, ecf_flags);
  3648. DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
  3649. SET_DECL_LANGUAGE (fn, lang_cplusplus);
  3650. return fn;
  3651. }
  3652. /* Like build_library_fn, but takes a C string instead of an
  3653. IDENTIFIER_NODE. */
  3654. tree
  3655. build_library_fn_ptr (const char* name, tree type, int ecf_flags)
  3656. {
  3657. return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
  3658. }
  3659. /* Like build_cp_library_fn, but takes a C string instead of an
  3660. IDENTIFIER_NODE. */
  3661. tree
  3662. build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
  3663. {
  3664. return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
  3665. ecf_flags);
  3666. }
  3667. /* Like build_library_fn, but also pushes the function so that we will
  3668. be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
  3669. may throw exceptions listed in RAISES. */
  3670. tree
  3671. push_library_fn (tree name, tree type, tree raises, int ecf_flags)
  3672. {
  3673. tree fn;
  3674. if (raises)
  3675. type = build_exception_variant (type, raises);
  3676. fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
  3677. pushdecl_top_level (fn);
  3678. return fn;
  3679. }
  3680. /* Like build_cp_library_fn, but also pushes the function so that it
  3681. will be found by normal lookup. */
  3682. static tree
  3683. push_cp_library_fn (enum tree_code operator_code, tree type,
  3684. int ecf_flags)
  3685. {
  3686. tree fn = build_cp_library_fn (ansi_opname (operator_code),
  3687. operator_code,
  3688. type, ecf_flags);
  3689. pushdecl (fn);
  3690. if (flag_tm)
  3691. apply_tm_attr (fn, get_identifier ("transaction_safe"));
  3692. return fn;
  3693. }
  3694. /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
  3695. a FUNCTION_TYPE. */
  3696. tree
  3697. push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
  3698. {
  3699. tree type = build_function_type (void_type_node, parmtypes);
  3700. return push_library_fn (name, type, NULL_TREE, ecf_flags);
  3701. }
  3702. /* Like push_library_fn, but also note that this function throws
  3703. and does not return. Used for __throw_foo and the like. */
  3704. tree
  3705. push_throw_library_fn (tree name, tree type)
  3706. {
  3707. tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
  3708. return fn;
  3709. }
  3710. /* When we call finish_struct for an anonymous union, we create
  3711. default copy constructors and such. But, an anonymous union
  3712. shouldn't have such things; this function undoes the damage to the
  3713. anonymous union type T.
  3714. (The reason that we create the synthesized methods is that we don't
  3715. distinguish `union { int i; }' from `typedef union { int i; } U'.
  3716. The first is an anonymous union; the second is just an ordinary
  3717. union type.) */
  3718. void
  3719. fixup_anonymous_aggr (tree t)
  3720. {
  3721. tree *q;
  3722. /* Wipe out memory of synthesized methods. */
  3723. TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
  3724. TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
  3725. TYPE_HAS_COPY_CTOR (t) = 0;
  3726. TYPE_HAS_CONST_COPY_CTOR (t) = 0;
  3727. TYPE_HAS_COPY_ASSIGN (t) = 0;
  3728. TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
  3729. /* Splice the implicitly generated functions out of the TYPE_METHODS
  3730. list. */
  3731. q = &TYPE_METHODS (t);
  3732. while (*q)
  3733. {
  3734. if (DECL_ARTIFICIAL (*q))
  3735. *q = TREE_CHAIN (*q);
  3736. else
  3737. q = &DECL_CHAIN (*q);
  3738. }
  3739. /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
  3740. if (TYPE_METHODS (t))
  3741. {
  3742. tree decl = TYPE_MAIN_DECL (t);
  3743. if (TREE_CODE (t) != UNION_TYPE)
  3744. error_at (DECL_SOURCE_LOCATION (decl),
  3745. "an anonymous struct cannot have function members");
  3746. else
  3747. error_at (DECL_SOURCE_LOCATION (decl),
  3748. "an anonymous union cannot have function members");
  3749. }
  3750. /* Anonymous aggregates cannot have fields with ctors, dtors or complex
  3751. assignment operators (because they cannot have these methods themselves).
  3752. For anonymous unions this is already checked because they are not allowed
  3753. in any union, otherwise we have to check it. */
  3754. if (TREE_CODE (t) != UNION_TYPE)
  3755. {
  3756. tree field, type;
  3757. for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
  3758. if (TREE_CODE (field) == FIELD_DECL)
  3759. {
  3760. type = TREE_TYPE (field);
  3761. if (CLASS_TYPE_P (type))
  3762. {
  3763. if (TYPE_NEEDS_CONSTRUCTING (type))
  3764. error ("member %q+#D with constructor not allowed "
  3765. "in anonymous aggregate", field);
  3766. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  3767. error ("member %q+#D with destructor not allowed "
  3768. "in anonymous aggregate", field);
  3769. if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
  3770. error ("member %q+#D with copy assignment operator "
  3771. "not allowed in anonymous aggregate", field);
  3772. }
  3773. }
  3774. }
  3775. }
  3776. /* Warn for an attribute located at LOCATION that appertains to the
  3777. class type CLASS_TYPE that has not been properly placed after its
  3778. class-key, in it class-specifier. */
  3779. void
  3780. warn_misplaced_attr_for_class_type (source_location location,
  3781. tree class_type)
  3782. {
  3783. gcc_assert (OVERLOAD_TYPE_P (class_type));
  3784. if (warning_at (location, OPT_Wattributes,
  3785. "attribute ignored in declaration "
  3786. "of %q#T", class_type))
  3787. inform (location,
  3788. "attribute for %q#T must follow the %qs keyword",
  3789. class_type, class_key_or_enum_as_string (class_type));
  3790. }
  3791. /* Make sure that a declaration with no declarator is well-formed, i.e.
  3792. just declares a tagged type or anonymous union.
  3793. Returns the type declared; or NULL_TREE if none. */
  3794. tree
  3795. check_tag_decl (cp_decl_specifier_seq *declspecs,
  3796. bool explicit_type_instantiation_p)
  3797. {
  3798. int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
  3799. int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
  3800. /* If a class, struct, or enum type is declared by the DECLSPECS
  3801. (i.e, if a class-specifier, enum-specifier, or non-typename
  3802. elaborated-type-specifier appears in the DECLSPECS),
  3803. DECLARED_TYPE is set to the corresponding type. */
  3804. tree declared_type = NULL_TREE;
  3805. bool error_p = false;
  3806. if (declspecs->multiple_types_p)
  3807. error ("multiple types in one declaration");
  3808. else if (declspecs->redefined_builtin_type)
  3809. {
  3810. if (!in_system_header_at (input_location))
  3811. permerror (declspecs->locations[ds_redefined_builtin_type_spec],
  3812. "redeclaration of C++ built-in type %qT",
  3813. declspecs->redefined_builtin_type);
  3814. return NULL_TREE;
  3815. }
  3816. if (declspecs->type
  3817. && TYPE_P (declspecs->type)
  3818. && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
  3819. && MAYBE_CLASS_TYPE_P (declspecs->type))
  3820. || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
  3821. declared_type = declspecs->type;
  3822. else if (declspecs->type == error_mark_node)
  3823. error_p = true;
  3824. if (declared_type == NULL_TREE && ! saw_friend && !error_p)
  3825. permerror (input_location, "declaration does not declare anything");
  3826. else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
  3827. {
  3828. error ("%<auto%> can only be specified for variables "
  3829. "or function declarations");
  3830. return error_mark_node;
  3831. }
  3832. /* Check for an anonymous union. */
  3833. else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
  3834. && TYPE_ANONYMOUS_P (declared_type))
  3835. {
  3836. /* 7/3 In a simple-declaration, the optional init-declarator-list
  3837. can be omitted only when declaring a class (clause 9) or
  3838. enumeration (7.2), that is, when the decl-specifier-seq contains
  3839. either a class-specifier, an elaborated-type-specifier with
  3840. a class-key (9.1), or an enum-specifier. In these cases and
  3841. whenever a class-specifier or enum-specifier is present in the
  3842. decl-specifier-seq, the identifiers in these specifiers are among
  3843. the names being declared by the declaration (as class-name,
  3844. enum-names, or enumerators, depending on the syntax). In such
  3845. cases, and except for the declaration of an unnamed bit-field (9.6),
  3846. the decl-specifier-seq shall introduce one or more names into the
  3847. program, or shall redeclare a name introduced by a previous
  3848. declaration. [Example:
  3849. enum { }; // ill-formed
  3850. typedef class { }; // ill-formed
  3851. --end example] */
  3852. if (saw_typedef)
  3853. {
  3854. error ("missing type-name in typedef-declaration");
  3855. return NULL_TREE;
  3856. }
  3857. /* Anonymous unions are objects, so they can have specifiers. */;
  3858. SET_ANON_AGGR_TYPE_P (declared_type);
  3859. if (TREE_CODE (declared_type) != UNION_TYPE
  3860. && !in_system_header_at (input_location))
  3861. pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
  3862. }
  3863. else
  3864. {
  3865. if (decl_spec_seq_has_spec_p (declspecs, ds_inline)
  3866. || decl_spec_seq_has_spec_p (declspecs, ds_virtual))
  3867. error ("%qs can only be specified for functions",
  3868. decl_spec_seq_has_spec_p (declspecs, ds_inline)
  3869. ? "inline" : "virtual");
  3870. else if (saw_friend
  3871. && (!current_class_type
  3872. || current_scope () != current_class_type))
  3873. error ("%<friend%> can only be specified inside a class");
  3874. else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
  3875. error ("%<explicit%> can only be specified for constructors");
  3876. else if (declspecs->storage_class)
  3877. error ("a storage class can only be specified for objects "
  3878. "and functions");
  3879. else if (decl_spec_seq_has_spec_p (declspecs, ds_const)
  3880. || decl_spec_seq_has_spec_p (declspecs, ds_volatile)
  3881. || decl_spec_seq_has_spec_p (declspecs, ds_restrict)
  3882. || decl_spec_seq_has_spec_p (declspecs, ds_thread))
  3883. error ("qualifiers can only be specified for objects "
  3884. "and functions");
  3885. else if (saw_typedef)
  3886. warning (0, "%<typedef%> was ignored in this declaration");
  3887. else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
  3888. error ("%<constexpr%> cannot be used for type declarations");
  3889. }
  3890. if (declspecs->attributes && warn_attributes && declared_type)
  3891. {
  3892. location_t loc;
  3893. if (!CLASS_TYPE_P (declared_type)
  3894. || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
  3895. /* For a non-template class, use the name location. */
  3896. loc = location_of (declared_type);
  3897. else
  3898. /* For a template class (an explicit instantiation), use the
  3899. current location. */
  3900. loc = input_location;
  3901. if (explicit_type_instantiation_p)
  3902. /* [dcl.attr.grammar]/4:
  3903. No attribute-specifier-seq shall appertain to an explicit
  3904. instantiation. */
  3905. {
  3906. if (warning_at (loc, OPT_Wattributes,
  3907. "attribute ignored in explicit instantiation %q#T",
  3908. declared_type))
  3909. inform (loc,
  3910. "no attribute can be applied to "
  3911. "an explicit instantiation");
  3912. }
  3913. else
  3914. warn_misplaced_attr_for_class_type (loc, declared_type);
  3915. }
  3916. return declared_type;
  3917. }
  3918. /* Called when a declaration is seen that contains no names to declare.
  3919. If its type is a reference to a structure, union or enum inherited
  3920. from a containing scope, shadow that tag name for the current scope
  3921. with a forward reference.
  3922. If its type defines a new named structure or union
  3923. or defines an enum, it is valid but we need not do anything here.
  3924. Otherwise, it is an error.
  3925. C++: may have to grok the declspecs to learn about static,
  3926. complain for anonymous unions.
  3927. Returns the TYPE declared -- or NULL_TREE if none. */
  3928. tree
  3929. shadow_tag (cp_decl_specifier_seq *declspecs)
  3930. {
  3931. tree t = check_tag_decl (declspecs,
  3932. /*explicit_type_instantiation_p=*/false);
  3933. if (!t)
  3934. return NULL_TREE;
  3935. if (maybe_process_partial_specialization (t) == error_mark_node)
  3936. return NULL_TREE;
  3937. /* This is where the variables in an anonymous union are
  3938. declared. An anonymous union declaration looks like:
  3939. union { ... } ;
  3940. because there is no declarator after the union, the parser
  3941. sends that declaration here. */
  3942. if (ANON_AGGR_TYPE_P (t))
  3943. {
  3944. fixup_anonymous_aggr (t);
  3945. if (TYPE_FIELDS (t))
  3946. {
  3947. tree decl = grokdeclarator (/*declarator=*/NULL,
  3948. declspecs, NORMAL, 0, NULL);
  3949. finish_anon_union (decl);
  3950. }
  3951. }
  3952. return t;
  3953. }
  3954. /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
  3955. tree
  3956. groktypename (cp_decl_specifier_seq *type_specifiers,
  3957. const cp_declarator *declarator,
  3958. bool is_template_arg)
  3959. {
  3960. tree attrs;
  3961. tree type;
  3962. enum decl_context context
  3963. = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
  3964. attrs = type_specifiers->attributes;
  3965. type_specifiers->attributes = NULL_TREE;
  3966. type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
  3967. if (attrs && type != error_mark_node)
  3968. {
  3969. if (CLASS_TYPE_P (type))
  3970. warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
  3971. "outside of definition", type);
  3972. else if (MAYBE_CLASS_TYPE_P (type))
  3973. /* A template type parameter or other dependent type. */
  3974. warning (OPT_Wattributes, "ignoring attributes applied to dependent "
  3975. "type %qT without an associated declaration", type);
  3976. else
  3977. cplus_decl_attributes (&type, attrs, 0);
  3978. }
  3979. return type;
  3980. }
  3981. /* Process a DECLARATOR for a function-scope variable declaration,
  3982. namespace-scope variable declaration, or function declaration.
  3983. (Function definitions go through start_function; class member
  3984. declarations appearing in the body of the class go through
  3985. grokfield.) The DECL corresponding to the DECLARATOR is returned.
  3986. If an error occurs, the error_mark_node is returned instead.
  3987. DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
  3988. SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
  3989. for an explicitly defaulted function, or SD_DELETED for an explicitly
  3990. deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
  3991. implicitly initialized via a default constructor. ATTRIBUTES and
  3992. PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
  3993. The scope represented by the context of the returned DECL is pushed
  3994. (if it is not the global namespace) and is assigned to
  3995. *PUSHED_SCOPE_P. The caller is then responsible for calling
  3996. pop_scope on *PUSHED_SCOPE_P if it is set. */
  3997. tree
  3998. start_decl (const cp_declarator *declarator,
  3999. cp_decl_specifier_seq *declspecs,
  4000. int initialized,
  4001. tree attributes,
  4002. tree prefix_attributes,
  4003. tree *pushed_scope_p)
  4004. {
  4005. tree decl;
  4006. tree context;
  4007. bool was_public;
  4008. int flags;
  4009. bool alias;
  4010. *pushed_scope_p = NULL_TREE;
  4011. /* An object declared as __attribute__((deprecated)) suppresses
  4012. warnings of uses of other deprecated items. */
  4013. if (lookup_attribute ("deprecated", attributes))
  4014. deprecated_state = DEPRECATED_SUPPRESS;
  4015. attributes = chainon (attributes, prefix_attributes);
  4016. decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
  4017. &attributes);
  4018. deprecated_state = DEPRECATED_NORMAL;
  4019. if (decl == NULL_TREE || VOID_TYPE_P (decl)
  4020. || decl == error_mark_node)
  4021. return error_mark_node;
  4022. context = CP_DECL_CONTEXT (decl);
  4023. if (context != global_namespace)
  4024. *pushed_scope_p = push_scope (context);
  4025. /* Is it valid for this decl to have an initializer at all?
  4026. If not, set INITIALIZED to zero, which will indirectly
  4027. tell `cp_finish_decl' to ignore the initializer once it is parsed. */
  4028. if (initialized
  4029. && TREE_CODE (decl) == TYPE_DECL)
  4030. {
  4031. error ("typedef %qD is initialized (use decltype instead)", decl);
  4032. return error_mark_node;
  4033. }
  4034. if (initialized)
  4035. {
  4036. if (! toplevel_bindings_p ()
  4037. && DECL_EXTERNAL (decl))
  4038. warning (0, "declaration of %q#D has %<extern%> and is initialized",
  4039. decl);
  4040. DECL_EXTERNAL (decl) = 0;
  4041. if (toplevel_bindings_p ())
  4042. TREE_STATIC (decl) = 1;
  4043. }
  4044. alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
  4045. if (alias && TREE_CODE (decl) == FUNCTION_DECL)
  4046. record_key_method_defined (decl);
  4047. /* If this is a typedef that names the class for linkage purposes
  4048. (7.1.3p8), apply any attributes directly to the type. */
  4049. if (TREE_CODE (decl) == TYPE_DECL
  4050. && OVERLOAD_TYPE_P (TREE_TYPE (decl))
  4051. && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
  4052. flags = ATTR_FLAG_TYPE_IN_PLACE;
  4053. else
  4054. flags = 0;
  4055. /* Set attributes here so if duplicate decl, will have proper attributes. */
  4056. cplus_decl_attributes (&decl, attributes, flags);
  4057. /* Dllimported symbols cannot be defined. Static data members (which
  4058. can be initialized in-class and dllimported) go through grokfield,
  4059. not here, so we don't need to exclude those decls when checking for
  4060. a definition. */
  4061. if (initialized && DECL_DLLIMPORT_P (decl))
  4062. {
  4063. error ("definition of %q#D is marked %<dllimport%>", decl);
  4064. DECL_DLLIMPORT_P (decl) = 0;
  4065. }
  4066. /* If #pragma weak was used, mark the decl weak now. */
  4067. if (!processing_template_decl)
  4068. maybe_apply_pragma_weak (decl);
  4069. if (TREE_CODE (decl) == FUNCTION_DECL
  4070. && DECL_DECLARED_INLINE_P (decl)
  4071. && DECL_UNINLINABLE (decl)
  4072. && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
  4073. warning (0, "inline function %q+D given attribute noinline", decl);
  4074. if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
  4075. {
  4076. bool this_tmpl = (processing_template_decl
  4077. > template_class_depth (context));
  4078. if (VAR_P (decl))
  4079. {
  4080. tree field = lookup_field (context, DECL_NAME (decl), 0, false);
  4081. if (field == NULL_TREE
  4082. || !(VAR_P (field) || variable_template_p (field)))
  4083. error ("%q+#D is not a static data member of %q#T", decl, context);
  4084. else if (variable_template_p (field) && !this_tmpl)
  4085. {
  4086. if (DECL_LANG_SPECIFIC (decl)
  4087. && DECL_TEMPLATE_SPECIALIZATION (decl))
  4088. /* OK, specialization was already checked. */;
  4089. else
  4090. {
  4091. error_at (DECL_SOURCE_LOCATION (decl),
  4092. "non-member-template declaration of %qD", decl);
  4093. inform (DECL_SOURCE_LOCATION (field), "does not match "
  4094. "member template declaration here");
  4095. return error_mark_node;
  4096. }
  4097. }
  4098. else
  4099. {
  4100. if (variable_template_p (field))
  4101. field = DECL_TEMPLATE_RESULT (field);
  4102. if (DECL_CONTEXT (field) != context)
  4103. {
  4104. if (!same_type_p (DECL_CONTEXT (field), context))
  4105. permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
  4106. "to be defined as %<%T::%D%>",
  4107. DECL_CONTEXT (field), DECL_NAME (decl),
  4108. context, DECL_NAME (decl));
  4109. DECL_CONTEXT (decl) = DECL_CONTEXT (field);
  4110. }
  4111. /* Static data member are tricky; an in-class initialization
  4112. still doesn't provide a definition, so the in-class
  4113. declaration will have DECL_EXTERNAL set, but will have an
  4114. initialization. Thus, duplicate_decls won't warn
  4115. about this situation, and so we check here. */
  4116. if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
  4117. error ("duplicate initialization of %qD", decl);
  4118. if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
  4119. decl = field;
  4120. if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
  4121. && !DECL_DECLARED_CONSTEXPR_P (field))
  4122. error ("%qD declared %<constexpr%> outside its class", field);
  4123. }
  4124. }
  4125. else
  4126. {
  4127. tree field = check_classfn (context, decl,
  4128. this_tmpl
  4129. ? current_template_parms
  4130. : NULL_TREE);
  4131. if (field && field != error_mark_node
  4132. && duplicate_decls (decl, field,
  4133. /*newdecl_is_friend=*/false))
  4134. decl = field;
  4135. }
  4136. /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
  4137. DECL_IN_AGGR_P (decl) = 0;
  4138. /* Do not mark DECL as an explicit specialization if it was not
  4139. already marked as an instantiation; a declaration should
  4140. never be marked as a specialization unless we know what
  4141. template is being specialized. */
  4142. if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
  4143. {
  4144. SET_DECL_TEMPLATE_SPECIALIZATION (decl);
  4145. if (TREE_CODE (decl) == FUNCTION_DECL)
  4146. DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
  4147. && DECL_DECLARED_INLINE_P (decl));
  4148. else
  4149. DECL_COMDAT (decl) = false;
  4150. /* [temp.expl.spec] An explicit specialization of a static data
  4151. member of a template is a definition if the declaration
  4152. includes an initializer; otherwise, it is a declaration.
  4153. We check for processing_specialization so this only applies
  4154. to the new specialization syntax. */
  4155. if (!initialized && processing_specialization)
  4156. DECL_EXTERNAL (decl) = 1;
  4157. }
  4158. if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
  4159. /* Aliases are definitions. */
  4160. && !alias)
  4161. permerror (input_location, "declaration of %q#D outside of class is not definition",
  4162. decl);
  4163. }
  4164. was_public = TREE_PUBLIC (decl);
  4165. /* Enter this declaration into the symbol table. Don't push the plain
  4166. VAR_DECL for a variable template. */
  4167. if (!template_parm_scope_p ()
  4168. || TREE_CODE (decl) != VAR_DECL)
  4169. decl = maybe_push_decl (decl);
  4170. if (processing_template_decl)
  4171. decl = push_template_decl (decl);
  4172. if (decl == error_mark_node)
  4173. return error_mark_node;
  4174. if (VAR_P (decl)
  4175. && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
  4176. && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
  4177. {
  4178. /* This is a const variable with implicit 'static'. Set
  4179. DECL_THIS_STATIC so we can tell it from variables that are
  4180. !TREE_PUBLIC because of the anonymous namespace. */
  4181. gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
  4182. DECL_THIS_STATIC (decl) = 1;
  4183. }
  4184. if (current_function_decl && VAR_P (decl)
  4185. && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
  4186. {
  4187. bool ok = false;
  4188. if (DECL_THREAD_LOCAL_P (decl))
  4189. error ("%qD declared %<thread_local%> in %<constexpr%> function",
  4190. decl);
  4191. else if (TREE_STATIC (decl))
  4192. error ("%qD declared %<static%> in %<constexpr%> function", decl);
  4193. else
  4194. ok = true;
  4195. if (!ok)
  4196. cp_function_chain->invalid_constexpr = true;
  4197. }
  4198. if (!processing_template_decl && VAR_P (decl))
  4199. start_decl_1 (decl, initialized);
  4200. return decl;
  4201. }
  4202. /* Process the declaration of a variable DECL. INITIALIZED is true
  4203. iff DECL is explicitly initialized. (INITIALIZED is false if the
  4204. variable is initialized via an implicitly-called constructor.)
  4205. This function must be called for ordinary variables (including, for
  4206. example, implicit instantiations of templates), but must not be
  4207. called for template declarations. */
  4208. void
  4209. start_decl_1 (tree decl, bool initialized)
  4210. {
  4211. tree type;
  4212. bool complete_p;
  4213. bool aggregate_definition_p;
  4214. gcc_assert (!processing_template_decl);
  4215. if (error_operand_p (decl))
  4216. return;
  4217. gcc_assert (VAR_P (decl));
  4218. type = TREE_TYPE (decl);
  4219. complete_p = COMPLETE_TYPE_P (type);
  4220. aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
  4221. /* If an explicit initializer is present, or if this is a definition
  4222. of an aggregate, then we need a complete type at this point.
  4223. (Scalars are always complete types, so there is nothing to
  4224. check.) This code just sets COMPLETE_P; errors (if necessary)
  4225. are issued below. */
  4226. if ((initialized || aggregate_definition_p)
  4227. && !complete_p
  4228. && COMPLETE_TYPE_P (complete_type (type)))
  4229. {
  4230. complete_p = true;
  4231. /* We will not yet have set TREE_READONLY on DECL if the type
  4232. was "const", but incomplete, before this point. But, now, we
  4233. have a complete type, so we can try again. */
  4234. cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
  4235. }
  4236. if (initialized)
  4237. /* Is it valid for this decl to have an initializer at all? */
  4238. {
  4239. /* Don't allow initializations for incomplete types except for
  4240. arrays which might be completed by the initialization. */
  4241. if (complete_p)
  4242. ; /* A complete type is ok. */
  4243. else if (type_uses_auto (type))
  4244. ; /* An auto type is ok. */
  4245. else if (TREE_CODE (type) != ARRAY_TYPE)
  4246. {
  4247. error ("variable %q#D has initializer but incomplete type", decl);
  4248. type = TREE_TYPE (decl) = error_mark_node;
  4249. }
  4250. else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
  4251. {
  4252. if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
  4253. error ("elements of array %q#D have incomplete type", decl);
  4254. /* else we already gave an error in start_decl. */
  4255. }
  4256. }
  4257. else if (aggregate_definition_p && !complete_p)
  4258. {
  4259. if (type_uses_auto (type))
  4260. error ("declaration of %q#D has no initializer", decl);
  4261. else
  4262. error ("aggregate %q#D has incomplete type and cannot be defined",
  4263. decl);
  4264. /* Change the type so that assemble_variable will give
  4265. DECL an rtl we can live with: (mem (const_int 0)). */
  4266. type = TREE_TYPE (decl) = error_mark_node;
  4267. }
  4268. /* Create a new scope to hold this declaration if necessary.
  4269. Whether or not a new scope is necessary cannot be determined
  4270. until after the type has been completed; if the type is a
  4271. specialization of a class template it is not until after
  4272. instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
  4273. will be set correctly. */
  4274. maybe_push_cleanup_level (type);
  4275. }
  4276. /* Handle initialization of references. DECL, TYPE, and INIT have the
  4277. same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
  4278. but will be set to a new CLEANUP_STMT if a temporary is created
  4279. that must be destroyed subsequently.
  4280. Returns an initializer expression to use to initialize DECL, or
  4281. NULL if the initialization can be performed statically.
  4282. Quotes on semantics can be found in ARM 8.4.3. */
  4283. static tree
  4284. grok_reference_init (tree decl, tree type, tree init, int flags)
  4285. {
  4286. if (init == NULL_TREE)
  4287. {
  4288. if ((DECL_LANG_SPECIFIC (decl) == 0
  4289. || DECL_IN_AGGR_P (decl) == 0)
  4290. && ! DECL_THIS_EXTERN (decl))
  4291. error ("%qD declared as reference but not initialized", decl);
  4292. return NULL_TREE;
  4293. }
  4294. if (TREE_CODE (init) == TREE_LIST)
  4295. init = build_x_compound_expr_from_list (init, ELK_INIT,
  4296. tf_warning_or_error);
  4297. tree ttype = TREE_TYPE (type);
  4298. if (TREE_CODE (ttype) != ARRAY_TYPE
  4299. && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
  4300. /* Note: default conversion is only called in very special cases. */
  4301. init = decay_conversion (init, tf_warning_or_error);
  4302. /* check_initializer handles this for non-reference variables, but for
  4303. references we need to do it here or the initializer will get the
  4304. incomplete array type and confuse later calls to
  4305. cp_complete_array_type. */
  4306. if (TREE_CODE (ttype) == ARRAY_TYPE
  4307. && TYPE_DOMAIN (ttype) == NULL_TREE
  4308. && (BRACE_ENCLOSED_INITIALIZER_P (init)
  4309. || TREE_CODE (init) == STRING_CST))
  4310. {
  4311. cp_complete_array_type (&ttype, init, false);
  4312. if (ttype != TREE_TYPE (type))
  4313. type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
  4314. }
  4315. /* Convert INIT to the reference type TYPE. This may involve the
  4316. creation of a temporary, whose lifetime must be the same as that
  4317. of the reference. If so, a DECL_EXPR for the temporary will be
  4318. added just after the DECL_EXPR for DECL. That's why we don't set
  4319. DECL_INITIAL for local references (instead assigning to them
  4320. explicitly); we need to allow the temporary to be initialized
  4321. first. */
  4322. return initialize_reference (type, init, flags,
  4323. tf_warning_or_error);
  4324. }
  4325. /* Designated initializers in arrays are not supported in GNU C++.
  4326. The parser cannot detect this error since it does not know whether
  4327. a given brace-enclosed initializer is for a class type or for an
  4328. array. This function checks that CE does not use a designated
  4329. initializer. If it does, an error is issued. Returns true if CE
  4330. is valid, i.e., does not have a designated initializer. */
  4331. static bool
  4332. check_array_designated_initializer (constructor_elt *ce,
  4333. unsigned HOST_WIDE_INT index)
  4334. {
  4335. /* Designated initializers for array elements are not supported. */
  4336. if (ce->index)
  4337. {
  4338. /* The parser only allows identifiers as designated
  4339. initializers. */
  4340. if (ce->index == error_mark_node)
  4341. {
  4342. error ("name used in a GNU-style designated "
  4343. "initializer for an array");
  4344. return false;
  4345. }
  4346. else if (identifier_p (ce->index))
  4347. {
  4348. error ("name %qD used in a GNU-style designated "
  4349. "initializer for an array", ce->index);
  4350. return false;
  4351. }
  4352. tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
  4353. ce->index, true);
  4354. if (ce_index
  4355. && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
  4356. && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
  4357. == INTEGER_CST))
  4358. {
  4359. /* A C99 designator is OK if it matches the current index. */
  4360. if (wi::eq_p (ce_index, index))
  4361. return true;
  4362. else
  4363. sorry ("non-trivial designated initializers not supported");
  4364. }
  4365. else
  4366. error ("C99 designator %qE is not an integral constant-expression",
  4367. ce->index);
  4368. return false;
  4369. }
  4370. return true;
  4371. }
  4372. /* When parsing `int a[] = {1, 2};' we don't know the size of the
  4373. array until we finish parsing the initializer. If that's the
  4374. situation we're in, update DECL accordingly. */
  4375. static void
  4376. maybe_deduce_size_from_array_init (tree decl, tree init)
  4377. {
  4378. tree type = TREE_TYPE (decl);
  4379. if (TREE_CODE (type) == ARRAY_TYPE
  4380. && TYPE_DOMAIN (type) == NULL_TREE
  4381. && TREE_CODE (decl) != TYPE_DECL)
  4382. {
  4383. /* do_default is really a C-ism to deal with tentative definitions.
  4384. But let's leave it here to ease the eventual merge. */
  4385. int do_default = !DECL_EXTERNAL (decl);
  4386. tree initializer = init ? init : DECL_INITIAL (decl);
  4387. int failure = 0;
  4388. /* Check that there are no designated initializers in INIT, as
  4389. those are not supported in GNU C++, and as the middle-end
  4390. will crash if presented with a non-numeric designated
  4391. initializer. */
  4392. if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
  4393. {
  4394. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
  4395. constructor_elt *ce;
  4396. HOST_WIDE_INT i;
  4397. FOR_EACH_VEC_SAFE_ELT (v, i, ce)
  4398. if (!check_array_designated_initializer (ce, i))
  4399. failure = 1;
  4400. }
  4401. if (!failure)
  4402. {
  4403. failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
  4404. do_default);
  4405. if (failure == 1)
  4406. {
  4407. error ("initializer fails to determine size of %qD", decl);
  4408. }
  4409. else if (failure == 2)
  4410. {
  4411. if (do_default)
  4412. {
  4413. error ("array size missing in %qD", decl);
  4414. }
  4415. /* If a `static' var's size isn't known, make it extern as
  4416. well as static, so it does not get allocated. If it's not
  4417. `static', then don't mark it extern; finish_incomplete_decl
  4418. will give it a default size and it will get allocated. */
  4419. else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
  4420. DECL_EXTERNAL (decl) = 1;
  4421. }
  4422. else if (failure == 3)
  4423. {
  4424. error ("zero-size array %qD", decl);
  4425. }
  4426. }
  4427. cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
  4428. relayout_decl (decl);
  4429. }
  4430. }
  4431. /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
  4432. any appropriate error messages regarding the layout. */
  4433. static void
  4434. layout_var_decl (tree decl)
  4435. {
  4436. tree type;
  4437. type = TREE_TYPE (decl);
  4438. if (type == error_mark_node)
  4439. return;
  4440. /* If we haven't already laid out this declaration, do so now.
  4441. Note that we must not call complete type for an external object
  4442. because it's type might involve templates that we are not
  4443. supposed to instantiate yet. (And it's perfectly valid to say
  4444. `extern X x' for some incomplete type `X'.) */
  4445. if (!DECL_EXTERNAL (decl))
  4446. complete_type (type);
  4447. if (!DECL_SIZE (decl)
  4448. && TREE_TYPE (decl) != error_mark_node
  4449. && (COMPLETE_TYPE_P (type)
  4450. || (TREE_CODE (type) == ARRAY_TYPE
  4451. && !TYPE_DOMAIN (type)
  4452. && COMPLETE_TYPE_P (TREE_TYPE (type)))))
  4453. layout_decl (decl, 0);
  4454. if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
  4455. {
  4456. /* An automatic variable with an incomplete type: that is an error.
  4457. Don't talk about array types here, since we took care of that
  4458. message in grokdeclarator. */
  4459. error ("storage size of %qD isn%'t known", decl);
  4460. TREE_TYPE (decl) = error_mark_node;
  4461. }
  4462. #if 0
  4463. /* Keep this code around in case we later want to control debug info
  4464. based on whether a type is "used". (jason 1999-11-11) */
  4465. else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
  4466. /* Let debugger know it should output info for this type. */
  4467. note_debug_info_needed (ttype);
  4468. if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
  4469. note_debug_info_needed (DECL_CONTEXT (decl));
  4470. #endif
  4471. if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
  4472. && DECL_SIZE (decl) != NULL_TREE
  4473. && ! TREE_CONSTANT (DECL_SIZE (decl)))
  4474. {
  4475. if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
  4476. constant_expression_warning (DECL_SIZE (decl));
  4477. else
  4478. {
  4479. error ("storage size of %qD isn%'t constant", decl);
  4480. TREE_TYPE (decl) = error_mark_node;
  4481. }
  4482. }
  4483. }
  4484. /* If a local static variable is declared in an inline function, or if
  4485. we have a weak definition, we must endeavor to create only one
  4486. instance of the variable at link-time. */
  4487. void
  4488. maybe_commonize_var (tree decl)
  4489. {
  4490. /* Static data in a function with comdat linkage also has comdat
  4491. linkage. */
  4492. if (TREE_STATIC (decl)
  4493. /* Don't mess with __FUNCTION__. */
  4494. && ! DECL_ARTIFICIAL (decl)
  4495. && DECL_FUNCTION_SCOPE_P (decl)
  4496. && vague_linkage_p (DECL_CONTEXT (decl)))
  4497. {
  4498. if (flag_weak)
  4499. {
  4500. /* With weak symbols, we simply make the variable COMDAT;
  4501. that will cause copies in multiple translations units to
  4502. be merged. */
  4503. comdat_linkage (decl);
  4504. }
  4505. else
  4506. {
  4507. if (DECL_INITIAL (decl) == NULL_TREE
  4508. || DECL_INITIAL (decl) == error_mark_node)
  4509. {
  4510. /* Without weak symbols, we can use COMMON to merge
  4511. uninitialized variables. */
  4512. TREE_PUBLIC (decl) = 1;
  4513. DECL_COMMON (decl) = 1;
  4514. }
  4515. else
  4516. {
  4517. /* While for initialized variables, we must use internal
  4518. linkage -- which means that multiple copies will not
  4519. be merged. */
  4520. TREE_PUBLIC (decl) = 0;
  4521. DECL_COMMON (decl) = 0;
  4522. if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
  4523. "sorry: semantics of inline function static "
  4524. "data %q#D are wrong (you%'ll wind up "
  4525. "with multiple copies)", decl))
  4526. inform (DECL_SOURCE_LOCATION (decl),
  4527. "you can work around this by removing the initializer");
  4528. }
  4529. }
  4530. }
  4531. }
  4532. /* Issue an error message if DECL is an uninitialized const variable. */
  4533. static void
  4534. check_for_uninitialized_const_var (tree decl)
  4535. {
  4536. tree type = strip_array_types (TREE_TYPE (decl));
  4537. /* ``Unless explicitly declared extern, a const object does not have
  4538. external linkage and must be initialized. ($8.4; $12.1)'' ARM
  4539. 7.1.6 */
  4540. if (VAR_P (decl)
  4541. && TREE_CODE (type) != REFERENCE_TYPE
  4542. && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
  4543. && !DECL_INITIAL (decl))
  4544. {
  4545. tree field = default_init_uninitialized_part (type);
  4546. if (!field)
  4547. return;
  4548. if (CP_TYPE_CONST_P (type))
  4549. permerror (DECL_SOURCE_LOCATION (decl),
  4550. "uninitialized const %qD", decl);
  4551. else
  4552. {
  4553. error_at (DECL_SOURCE_LOCATION (decl),
  4554. "uninitialized variable %qD in %<constexpr%> function",
  4555. decl);
  4556. cp_function_chain->invalid_constexpr = true;
  4557. }
  4558. if (CLASS_TYPE_P (type))
  4559. {
  4560. tree defaulted_ctor;
  4561. inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
  4562. "%q#T has no user-provided default constructor", type);
  4563. defaulted_ctor = in_class_defaulted_default_constructor (type);
  4564. if (defaulted_ctor)
  4565. inform (DECL_SOURCE_LOCATION (defaulted_ctor),
  4566. "constructor is not user-provided because it is "
  4567. "explicitly defaulted in the class body");
  4568. inform (0, "and the implicitly-defined constructor does not "
  4569. "initialize %q+#D", field);
  4570. }
  4571. }
  4572. }
  4573. /* Structure holding the current initializer being processed by reshape_init.
  4574. CUR is a pointer to the current element being processed, END is a pointer
  4575. after the last element present in the initializer. */
  4576. typedef struct reshape_iterator_t
  4577. {
  4578. constructor_elt *cur;
  4579. constructor_elt *end;
  4580. } reshape_iter;
  4581. static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
  4582. /* FIELD is a FIELD_DECL or NULL. In the former case, the value
  4583. returned is the next FIELD_DECL (possibly FIELD itself) that can be
  4584. initialized. If there are no more such fields, the return value
  4585. will be NULL. */
  4586. tree
  4587. next_initializable_field (tree field)
  4588. {
  4589. while (field
  4590. && (TREE_CODE (field) != FIELD_DECL
  4591. || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
  4592. || DECL_ARTIFICIAL (field)))
  4593. field = DECL_CHAIN (field);
  4594. return field;
  4595. }
  4596. /* Subroutine of reshape_init_array and reshape_init_vector, which does
  4597. the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
  4598. INTEGER_CST representing the size of the array minus one (the maximum index),
  4599. or NULL_TREE if the array was declared without specifying the size. D is
  4600. the iterator within the constructor. */
  4601. static tree
  4602. reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
  4603. tsubst_flags_t complain)
  4604. {
  4605. tree new_init;
  4606. bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
  4607. unsigned HOST_WIDE_INT max_index_cst = 0;
  4608. unsigned HOST_WIDE_INT index;
  4609. /* The initializer for an array is always a CONSTRUCTOR. */
  4610. new_init = build_constructor (init_list_type_node, NULL);
  4611. if (sized_array_p)
  4612. {
  4613. /* Minus 1 is used for zero sized arrays. */
  4614. if (integer_all_onesp (max_index))
  4615. return new_init;
  4616. if (tree_fits_uhwi_p (max_index))
  4617. max_index_cst = tree_to_uhwi (max_index);
  4618. /* sizetype is sign extended, not zero extended. */
  4619. else
  4620. max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
  4621. }
  4622. /* Loop until there are no more initializers. */
  4623. for (index = 0;
  4624. d->cur != d->end && (!sized_array_p || index <= max_index_cst);
  4625. ++index)
  4626. {
  4627. tree elt_init;
  4628. constructor_elt *old_cur = d->cur;
  4629. check_array_designated_initializer (d->cur, index);
  4630. elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
  4631. complain);
  4632. if (elt_init == error_mark_node)
  4633. return error_mark_node;
  4634. CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
  4635. size_int (index), elt_init);
  4636. if (!TREE_CONSTANT (elt_init))
  4637. TREE_CONSTANT (new_init) = false;
  4638. /* This can happen with an invalid initializer (c++/54501). */
  4639. if (d->cur == old_cur && !sized_array_p)
  4640. break;
  4641. }
  4642. return new_init;
  4643. }
  4644. /* Subroutine of reshape_init_r, processes the initializers for arrays.
  4645. Parameters are the same of reshape_init_r. */
  4646. static tree
  4647. reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
  4648. {
  4649. tree max_index = NULL_TREE;
  4650. gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
  4651. if (TYPE_DOMAIN (type))
  4652. max_index = array_type_nelts (type);
  4653. return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
  4654. }
  4655. /* Subroutine of reshape_init_r, processes the initializers for vectors.
  4656. Parameters are the same of reshape_init_r. */
  4657. static tree
  4658. reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
  4659. {
  4660. tree max_index = NULL_TREE;
  4661. gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
  4662. if (COMPOUND_LITERAL_P (d->cur->value))
  4663. {
  4664. tree value = d->cur->value;
  4665. if (!same_type_p (TREE_TYPE (value), type))
  4666. {
  4667. if (complain & tf_error)
  4668. error ("invalid type %qT as initializer for a vector of type %qT",
  4669. TREE_TYPE (d->cur->value), type);
  4670. value = error_mark_node;
  4671. }
  4672. ++d->cur;
  4673. return value;
  4674. }
  4675. /* For a vector, we initialize it as an array of the appropriate size. */
  4676. if (TREE_CODE (type) == VECTOR_TYPE)
  4677. max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
  4678. return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
  4679. }
  4680. /* Subroutine of reshape_init_r, processes the initializers for classes
  4681. or union. Parameters are the same of reshape_init_r. */
  4682. static tree
  4683. reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
  4684. tsubst_flags_t complain)
  4685. {
  4686. tree field;
  4687. tree new_init;
  4688. gcc_assert (CLASS_TYPE_P (type));
  4689. /* The initializer for a class is always a CONSTRUCTOR. */
  4690. new_init = build_constructor (init_list_type_node, NULL);
  4691. field = next_initializable_field (TYPE_FIELDS (type));
  4692. if (!field)
  4693. {
  4694. /* [dcl.init.aggr]
  4695. An initializer for an aggregate member that is an
  4696. empty class shall have the form of an empty
  4697. initializer-list {}. */
  4698. if (!first_initializer_p)
  4699. {
  4700. if (complain & tf_error)
  4701. error ("initializer for %qT must be brace-enclosed", type);
  4702. return error_mark_node;
  4703. }
  4704. return new_init;
  4705. }
  4706. /* Loop through the initializable fields, gathering initializers. */
  4707. while (d->cur != d->end)
  4708. {
  4709. tree field_init;
  4710. constructor_elt *old_cur = d->cur;
  4711. /* Handle designated initializers, as an extension. */
  4712. if (d->cur->index)
  4713. {
  4714. if (d->cur->index == error_mark_node)
  4715. return error_mark_node;
  4716. if (TREE_CODE (d->cur->index) == FIELD_DECL)
  4717. /* We already reshaped this. */
  4718. gcc_assert (d->cur->index == field);
  4719. else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
  4720. field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
  4721. else
  4722. {
  4723. if (complain & tf_error)
  4724. error ("%<[%E] =%> used in a GNU-style designated initializer"
  4725. " for class %qT", d->cur->index, type);
  4726. return error_mark_node;
  4727. }
  4728. if (!field || TREE_CODE (field) != FIELD_DECL)
  4729. {
  4730. if (complain & tf_error)
  4731. error ("%qT has no non-static data member named %qD", type,
  4732. d->cur->index);
  4733. return error_mark_node;
  4734. }
  4735. }
  4736. /* If we processed all the member of the class, we are done. */
  4737. if (!field)
  4738. break;
  4739. field_init = reshape_init_r (TREE_TYPE (field), d,
  4740. /*first_initializer_p=*/false, complain);
  4741. if (field_init == error_mark_node)
  4742. return error_mark_node;
  4743. if (d->cur == old_cur && d->cur->index)
  4744. {
  4745. /* This can happen with an invalid initializer for a flexible
  4746. array member (c++/54441). */
  4747. if (complain & tf_error)
  4748. error ("invalid initializer for %q#D", field);
  4749. return error_mark_node;
  4750. }
  4751. CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
  4752. /* [dcl.init.aggr]
  4753. When a union is initialized with a brace-enclosed
  4754. initializer, the braces shall only contain an
  4755. initializer for the first member of the union. */
  4756. if (TREE_CODE (type) == UNION_TYPE)
  4757. break;
  4758. field = next_initializable_field (DECL_CHAIN (field));
  4759. }
  4760. return new_init;
  4761. }
  4762. /* Subroutine of reshape_init_r. We're in a context where C99 initializer
  4763. designators are not valid; either complain or return true to indicate
  4764. that reshape_init_r should return error_mark_node. */
  4765. static bool
  4766. has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
  4767. {
  4768. if (d->cur->index)
  4769. {
  4770. if (complain & tf_error)
  4771. error ("C99 designator %qE outside aggregate initializer",
  4772. d->cur->index);
  4773. else
  4774. return true;
  4775. }
  4776. return false;
  4777. }
  4778. /* Subroutine of reshape_init, which processes a single initializer (part of
  4779. a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
  4780. iterator within the CONSTRUCTOR which points to the initializer to process.
  4781. FIRST_INITIALIZER_P is true if this is the first initializer of the
  4782. outermost CONSTRUCTOR node. */
  4783. static tree
  4784. reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
  4785. tsubst_flags_t complain)
  4786. {
  4787. tree init = d->cur->value;
  4788. if (error_operand_p (init))
  4789. return error_mark_node;
  4790. if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
  4791. && has_designator_problem (d, complain))
  4792. return error_mark_node;
  4793. if (TREE_CODE (type) == COMPLEX_TYPE)
  4794. {
  4795. /* A complex type can be initialized from one or two initializers,
  4796. but braces are not elided. */
  4797. d->cur++;
  4798. if (BRACE_ENCLOSED_INITIALIZER_P (init))
  4799. {
  4800. if (CONSTRUCTOR_NELTS (init) > 2)
  4801. {
  4802. if (complain & tf_error)
  4803. error ("too many initializers for %qT", type);
  4804. else
  4805. return error_mark_node;
  4806. }
  4807. }
  4808. else if (first_initializer_p && d->cur != d->end)
  4809. {
  4810. vec<constructor_elt, va_gc> *v = 0;
  4811. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
  4812. CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
  4813. if (has_designator_problem (d, complain))
  4814. return error_mark_node;
  4815. d->cur++;
  4816. init = build_constructor (init_list_type_node, v);
  4817. }
  4818. return init;
  4819. }
  4820. /* A non-aggregate type is always initialized with a single
  4821. initializer. */
  4822. if (!CP_AGGREGATE_TYPE_P (type))
  4823. {
  4824. /* It is invalid to initialize a non-aggregate type with a
  4825. brace-enclosed initializer before C++0x.
  4826. We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
  4827. of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
  4828. a CONSTRUCTOR (with a record type). */
  4829. if (TREE_CODE (init) == CONSTRUCTOR
  4830. /* Don't complain about a capture-init. */
  4831. && !CONSTRUCTOR_IS_DIRECT_INIT (init)
  4832. && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
  4833. {
  4834. if (SCALAR_TYPE_P (type))
  4835. {
  4836. if (cxx_dialect < cxx11
  4837. /* Isn't value-initialization. */
  4838. || CONSTRUCTOR_NELTS (init) > 0)
  4839. {
  4840. if (complain & tf_error)
  4841. error ("braces around scalar initializer for type %qT",
  4842. type);
  4843. init = error_mark_node;
  4844. }
  4845. }
  4846. else
  4847. maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
  4848. }
  4849. d->cur++;
  4850. return init;
  4851. }
  4852. /* "If T is a class type and the initializer list has a single element of
  4853. type cv U, where U is T or a class derived from T, the object is
  4854. initialized from that element." Even if T is an aggregate. */
  4855. if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
  4856. && first_initializer_p
  4857. && d->end - d->cur == 1
  4858. && reference_related_p (type, TREE_TYPE (init)))
  4859. {
  4860. d->cur++;
  4861. return init;
  4862. }
  4863. /* [dcl.init.aggr]
  4864. All implicit type conversions (clause _conv_) are considered when
  4865. initializing the aggregate member with an initializer from an
  4866. initializer-list. If the initializer can initialize a member,
  4867. the member is initialized. Otherwise, if the member is itself a
  4868. non-empty subaggregate, brace elision is assumed and the
  4869. initializer is considered for the initialization of the first
  4870. member of the subaggregate. */
  4871. if (TREE_CODE (init) != CONSTRUCTOR
  4872. /* But don't try this for the first initializer, since that would be
  4873. looking through the outermost braces; A a2 = { a1 }; is not a
  4874. valid aggregate initialization. */
  4875. && !first_initializer_p
  4876. && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
  4877. || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
  4878. complain)))
  4879. {
  4880. d->cur++;
  4881. return init;
  4882. }
  4883. /* [dcl.init.string]
  4884. A char array (whether plain char, signed char, or unsigned char)
  4885. can be initialized by a string-literal (optionally enclosed in
  4886. braces); a wchar_t array can be initialized by a wide
  4887. string-literal (optionally enclosed in braces). */
  4888. if (TREE_CODE (type) == ARRAY_TYPE
  4889. && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
  4890. {
  4891. tree str_init = init;
  4892. /* Strip one level of braces if and only if they enclose a single
  4893. element (as allowed by [dcl.init.string]). */
  4894. if (!first_initializer_p
  4895. && TREE_CODE (str_init) == CONSTRUCTOR
  4896. && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
  4897. {
  4898. str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
  4899. }
  4900. /* If it's a string literal, then it's the initializer for the array
  4901. as a whole. Otherwise, continue with normal initialization for
  4902. array types (one value per array element). */
  4903. if (TREE_CODE (str_init) == STRING_CST)
  4904. {
  4905. if (has_designator_problem (d, complain))
  4906. return error_mark_node;
  4907. d->cur++;
  4908. return str_init;
  4909. }
  4910. }
  4911. /* The following cases are about aggregates. If we are not within a full
  4912. initializer already, and there is not a CONSTRUCTOR, it means that there
  4913. is a missing set of braces (that is, we are processing the case for
  4914. which reshape_init exists). */
  4915. if (!first_initializer_p)
  4916. {
  4917. if (TREE_CODE (init) == CONSTRUCTOR)
  4918. {
  4919. if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
  4920. /* There is no need to reshape pointer-to-member function
  4921. initializers, as they are always constructed correctly
  4922. by the front end. */
  4923. ;
  4924. else if (COMPOUND_LITERAL_P (init))
  4925. /* For a nested compound literal, there is no need to reshape since
  4926. brace elision is not allowed. Even if we decided to allow it,
  4927. we should add a call to reshape_init in finish_compound_literal,
  4928. before calling digest_init, so changing this code would still
  4929. not be necessary. */
  4930. gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
  4931. else
  4932. {
  4933. ++d->cur;
  4934. gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
  4935. return reshape_init (type, init, complain);
  4936. }
  4937. }
  4938. warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
  4939. type);
  4940. }
  4941. /* Dispatch to specialized routines. */
  4942. if (CLASS_TYPE_P (type))
  4943. return reshape_init_class (type, d, first_initializer_p, complain);
  4944. else if (TREE_CODE (type) == ARRAY_TYPE)
  4945. return reshape_init_array (type, d, complain);
  4946. else if (TREE_CODE (type) == VECTOR_TYPE)
  4947. return reshape_init_vector (type, d, complain);
  4948. else
  4949. gcc_unreachable();
  4950. }
  4951. /* Undo the brace-elision allowed by [dcl.init.aggr] in a
  4952. brace-enclosed aggregate initializer.
  4953. INIT is the CONSTRUCTOR containing the list of initializers describing
  4954. a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
  4955. It may not presently match the shape of the TYPE; for example:
  4956. struct S { int a; int b; };
  4957. struct S a[] = { 1, 2, 3, 4 };
  4958. Here INIT will hold a vector of four elements, rather than a
  4959. vector of two elements, each itself a vector of two elements. This
  4960. routine transforms INIT from the former form into the latter. The
  4961. revised CONSTRUCTOR node is returned. */
  4962. tree
  4963. reshape_init (tree type, tree init, tsubst_flags_t complain)
  4964. {
  4965. vec<constructor_elt, va_gc> *v;
  4966. reshape_iter d;
  4967. tree new_init;
  4968. gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
  4969. v = CONSTRUCTOR_ELTS (init);
  4970. /* An empty constructor does not need reshaping, and it is always a valid
  4971. initializer. */
  4972. if (vec_safe_is_empty (v))
  4973. return init;
  4974. /* Recurse on this CONSTRUCTOR. */
  4975. d.cur = &(*v)[0];
  4976. d.end = d.cur + v->length ();
  4977. new_init = reshape_init_r (type, &d, true, complain);
  4978. if (new_init == error_mark_node)
  4979. return error_mark_node;
  4980. /* Make sure all the element of the constructor were used. Otherwise,
  4981. issue an error about exceeding initializers. */
  4982. if (d.cur != d.end)
  4983. {
  4984. if (complain & tf_error)
  4985. error ("too many initializers for %qT", type);
  4986. else
  4987. return error_mark_node;
  4988. }
  4989. return new_init;
  4990. }
  4991. /* Verify array initializer. Returns true if errors have been reported. */
  4992. bool
  4993. check_array_initializer (tree decl, tree type, tree init)
  4994. {
  4995. tree element_type = TREE_TYPE (type);
  4996. /* The array type itself need not be complete, because the
  4997. initializer may tell us how many elements are in the array.
  4998. But, the elements of the array must be complete. */
  4999. if (!COMPLETE_TYPE_P (complete_type (element_type)))
  5000. {
  5001. if (decl)
  5002. error ("elements of array %q#D have incomplete type", decl);
  5003. else
  5004. error ("elements of array %q#T have incomplete type", type);
  5005. return true;
  5006. }
  5007. /* A compound literal can't have variable size. */
  5008. if (init && !decl
  5009. && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
  5010. || !TREE_CONSTANT (TYPE_SIZE (element_type))))
  5011. {
  5012. error ("variable-sized compound literal");
  5013. return true;
  5014. }
  5015. return false;
  5016. }
  5017. /* Subroutine of check_initializer; args are passed down from that function.
  5018. Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
  5019. static tree
  5020. build_aggr_init_full_exprs (tree decl, tree init, int flags)
  5021. {
  5022. gcc_assert (stmts_are_full_exprs_p ());
  5023. return build_aggr_init (decl, init, flags, tf_warning_or_error);
  5024. }
  5025. /* Verify INIT (the initializer for DECL), and record the
  5026. initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
  5027. grok_reference_init.
  5028. If the return value is non-NULL, it is an expression that must be
  5029. evaluated dynamically to initialize DECL. */
  5030. static tree
  5031. check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
  5032. {
  5033. tree type = TREE_TYPE (decl);
  5034. tree init_code = NULL;
  5035. tree core_type;
  5036. /* Things that are going to be initialized need to have complete
  5037. type. */
  5038. TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
  5039. if (DECL_HAS_VALUE_EXPR_P (decl))
  5040. {
  5041. /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
  5042. it doesn't have storage to be initialized. */
  5043. gcc_assert (init == NULL_TREE);
  5044. return NULL_TREE;
  5045. }
  5046. if (type == error_mark_node)
  5047. /* We will have already complained. */
  5048. return NULL_TREE;
  5049. if (TREE_CODE (type) == ARRAY_TYPE)
  5050. {
  5051. if (check_array_initializer (decl, type, init))
  5052. return NULL_TREE;
  5053. }
  5054. else if (!COMPLETE_TYPE_P (type))
  5055. {
  5056. error ("%q#D has incomplete type", decl);
  5057. TREE_TYPE (decl) = error_mark_node;
  5058. return NULL_TREE;
  5059. }
  5060. else
  5061. /* There is no way to make a variable-sized class type in GNU C++. */
  5062. gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
  5063. if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
  5064. {
  5065. int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
  5066. if (SCALAR_TYPE_P (type))
  5067. {
  5068. if (init_len == 0)
  5069. {
  5070. maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
  5071. init = build_zero_init (type, NULL_TREE, false);
  5072. }
  5073. else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
  5074. {
  5075. error ("scalar object %qD requires one element in initializer",
  5076. decl);
  5077. TREE_TYPE (decl) = error_mark_node;
  5078. return NULL_TREE;
  5079. }
  5080. }
  5081. }
  5082. if (TREE_CODE (decl) == CONST_DECL)
  5083. {
  5084. gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
  5085. DECL_INITIAL (decl) = init;
  5086. gcc_assert (init != NULL_TREE);
  5087. init = NULL_TREE;
  5088. }
  5089. else if (!init && DECL_REALLY_EXTERN (decl))
  5090. ;
  5091. else if (init || type_build_ctor_call (type)
  5092. || TREE_CODE (type) == REFERENCE_TYPE)
  5093. {
  5094. if (TREE_CODE (type) == REFERENCE_TYPE)
  5095. {
  5096. init = grok_reference_init (decl, type, init, flags);
  5097. flags |= LOOKUP_ALREADY_DIGESTED;
  5098. }
  5099. else if (!init)
  5100. check_for_uninitialized_const_var (decl);
  5101. /* Do not reshape constructors of vectors (they don't need to be
  5102. reshaped. */
  5103. else if (BRACE_ENCLOSED_INITIALIZER_P (init))
  5104. {
  5105. if (is_std_init_list (type))
  5106. {
  5107. init = perform_implicit_conversion (type, init,
  5108. tf_warning_or_error);
  5109. flags |= LOOKUP_ALREADY_DIGESTED;
  5110. }
  5111. else if (TYPE_NON_AGGREGATE_CLASS (type))
  5112. {
  5113. /* Don't reshape if the class has constructors. */
  5114. if (cxx_dialect == cxx98)
  5115. error ("in C++98 %qD must be initialized by constructor, "
  5116. "not by %<{...}%>",
  5117. decl);
  5118. }
  5119. else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_VECTOR_OPAQUE (type))
  5120. {
  5121. error ("opaque vector types cannot be initialized");
  5122. init = error_mark_node;
  5123. }
  5124. else
  5125. {
  5126. init = reshape_init (type, init, tf_warning_or_error);
  5127. flags |= LOOKUP_NO_NARROWING;
  5128. }
  5129. }
  5130. else if (TREE_CODE (init) == TREE_LIST
  5131. && TREE_TYPE (init) != unknown_type_node
  5132. && !MAYBE_CLASS_TYPE_P (type))
  5133. {
  5134. gcc_assert (TREE_CODE (decl) != RESULT_DECL);
  5135. /* We get here with code like `int a (2);' */
  5136. init = build_x_compound_expr_from_list (init, ELK_INIT,
  5137. tf_warning_or_error);
  5138. }
  5139. /* If DECL has an array type without a specific bound, deduce the
  5140. array size from the initializer. */
  5141. maybe_deduce_size_from_array_init (decl, init);
  5142. type = TREE_TYPE (decl);
  5143. if (type == error_mark_node)
  5144. return NULL_TREE;
  5145. if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
  5146. && !(flags & LOOKUP_ALREADY_DIGESTED)
  5147. && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
  5148. && CP_AGGREGATE_TYPE_P (type)
  5149. && (CLASS_TYPE_P (type)
  5150. || !TYPE_NEEDS_CONSTRUCTING (type)
  5151. || type_has_extended_temps (type))))
  5152. {
  5153. init_code = build_aggr_init_full_exprs (decl, init, flags);
  5154. /* A constructor call is a non-trivial initializer even if
  5155. it isn't explicitly written. */
  5156. if (TREE_SIDE_EFFECTS (init_code))
  5157. DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
  5158. /* If this is a constexpr initializer, expand_default_init will
  5159. have returned an INIT_EXPR rather than a CALL_EXPR. In that
  5160. case, pull the initializer back out and pass it down into
  5161. store_init_value. */
  5162. while (TREE_CODE (init_code) == EXPR_STMT
  5163. || TREE_CODE (init_code) == CONVERT_EXPR)
  5164. init_code = TREE_OPERAND (init_code, 0);
  5165. if (TREE_CODE (init_code) == INIT_EXPR)
  5166. {
  5167. init = TREE_OPERAND (init_code, 1);
  5168. init_code = NULL_TREE;
  5169. /* Don't call digest_init; it's unnecessary and will complain
  5170. about aggregate initialization of non-aggregate classes. */
  5171. flags |= LOOKUP_ALREADY_DIGESTED;
  5172. }
  5173. else if (DECL_DECLARED_CONSTEXPR_P (decl))
  5174. {
  5175. /* Declared constexpr, but no suitable initializer; massage
  5176. init appropriately so we can pass it into store_init_value
  5177. for the error. */
  5178. if (CLASS_TYPE_P (type)
  5179. && (!init || TREE_CODE (init) == TREE_LIST))
  5180. {
  5181. init = build_functional_cast (type, init, tf_none);
  5182. if (TREE_CODE (init) == TARGET_EXPR)
  5183. TARGET_EXPR_DIRECT_INIT_P (init) = true;
  5184. }
  5185. init_code = NULL_TREE;
  5186. }
  5187. else
  5188. init = NULL_TREE;
  5189. }
  5190. if (init && TREE_CODE (init) != TREE_VEC)
  5191. {
  5192. /* In aggregate initialization of a variable, each element
  5193. initialization is a full-expression because there is no
  5194. enclosing expression. */
  5195. gcc_assert (stmts_are_full_exprs_p ());
  5196. init_code = store_init_value (decl, init, cleanups, flags);
  5197. if (pedantic && TREE_CODE (type) == ARRAY_TYPE
  5198. && DECL_INITIAL (decl)
  5199. && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
  5200. && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
  5201. warning (0, "array %qD initialized by parenthesized string literal %qE",
  5202. decl, DECL_INITIAL (decl));
  5203. init = NULL;
  5204. }
  5205. }
  5206. else
  5207. {
  5208. if (CLASS_TYPE_P (core_type = strip_array_types (type))
  5209. && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
  5210. || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
  5211. diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
  5212. /*complain=*/true);
  5213. check_for_uninitialized_const_var (decl);
  5214. }
  5215. if (init && init != error_mark_node)
  5216. init_code = build2 (INIT_EXPR, type, decl, init);
  5217. if (init_code)
  5218. {
  5219. /* We might have set these in cp_finish_decl. */
  5220. DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
  5221. TREE_CONSTANT (decl) = false;
  5222. }
  5223. if (init_code && DECL_IN_AGGR_P (decl))
  5224. {
  5225. static int explained = 0;
  5226. if (cxx_dialect < cxx11)
  5227. error ("initializer invalid for static member with constructor");
  5228. else
  5229. error ("non-constant in-class initialization invalid for static "
  5230. "member %qD", decl);
  5231. if (!explained)
  5232. {
  5233. inform (input_location,
  5234. "(an out of class initialization is required)");
  5235. explained = 1;
  5236. }
  5237. return NULL_TREE;
  5238. }
  5239. return init_code;
  5240. }
  5241. /* If DECL is not a local variable, give it RTL. */
  5242. static void
  5243. make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
  5244. {
  5245. int toplev = toplevel_bindings_p ();
  5246. int defer_p;
  5247. /* Set the DECL_ASSEMBLER_NAME for the object. */
  5248. if (asmspec)
  5249. {
  5250. /* The `register' keyword, when used together with an
  5251. asm-specification, indicates that the variable should be
  5252. placed in a particular register. */
  5253. if (VAR_P (decl) && DECL_REGISTER (decl))
  5254. {
  5255. set_user_assembler_name (decl, asmspec);
  5256. DECL_HARD_REGISTER (decl) = 1;
  5257. }
  5258. else
  5259. {
  5260. if (TREE_CODE (decl) == FUNCTION_DECL
  5261. && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
  5262. set_builtin_user_assembler_name (decl, asmspec);
  5263. set_user_assembler_name (decl, asmspec);
  5264. }
  5265. }
  5266. /* Handle non-variables up front. */
  5267. if (!VAR_P (decl))
  5268. {
  5269. rest_of_decl_compilation (decl, toplev, at_eof);
  5270. return;
  5271. }
  5272. /* If we see a class member here, it should be a static data
  5273. member. */
  5274. if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
  5275. {
  5276. gcc_assert (TREE_STATIC (decl));
  5277. /* An in-class declaration of a static data member should be
  5278. external; it is only a declaration, and not a definition. */
  5279. if (init == NULL_TREE)
  5280. gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
  5281. }
  5282. /* We don't create any RTL for local variables. */
  5283. if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
  5284. return;
  5285. /* We defer emission of local statics until the corresponding
  5286. DECL_EXPR is expanded. */
  5287. defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
  5288. /* Defer template instantiations. */
  5289. if (DECL_LANG_SPECIFIC (decl)
  5290. && DECL_IMPLICIT_INSTANTIATION (decl))
  5291. defer_p = 1;
  5292. /* If we're not deferring, go ahead and assemble the variable. */
  5293. if (!defer_p)
  5294. rest_of_decl_compilation (decl, toplev, at_eof);
  5295. }
  5296. /* walk_tree helper for wrap_temporary_cleanups, below. */
  5297. static tree
  5298. wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
  5299. {
  5300. /* Stop at types or full-expression boundaries. */
  5301. if (TYPE_P (*stmt_p)
  5302. || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
  5303. {
  5304. *walk_subtrees = 0;
  5305. return NULL_TREE;
  5306. }
  5307. if (TREE_CODE (*stmt_p) == TARGET_EXPR)
  5308. {
  5309. tree guard = (tree)data;
  5310. tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
  5311. tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
  5312. /* Tell honor_protect_cleanup_actions to handle this as a separate
  5313. cleanup. */
  5314. TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
  5315. TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
  5316. }
  5317. return NULL_TREE;
  5318. }
  5319. /* We're initializing a local variable which has a cleanup GUARD. If there
  5320. are any temporaries used in the initializer INIT of this variable, we
  5321. need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
  5322. variable will be cleaned up properly if one of them throws.
  5323. Unfortunately, there's no way to express this properly in terms of
  5324. nesting, as the regions for the temporaries overlap the region for the
  5325. variable itself; if there are two temporaries, the variable needs to be
  5326. the first thing destroyed if either of them throws. However, we only
  5327. want to run the variable's cleanup if it actually got constructed. So
  5328. we need to guard the temporary cleanups with the variable's cleanup if
  5329. they are run on the normal path, but not if they are run on the
  5330. exceptional path. We implement this by telling
  5331. honor_protect_cleanup_actions to strip the variable cleanup from the
  5332. exceptional path. */
  5333. static void
  5334. wrap_temporary_cleanups (tree init, tree guard)
  5335. {
  5336. cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
  5337. }
  5338. /* Generate code to initialize DECL (a local variable). */
  5339. static void
  5340. initialize_local_var (tree decl, tree init)
  5341. {
  5342. tree type = TREE_TYPE (decl);
  5343. tree cleanup;
  5344. int already_used;
  5345. gcc_assert (VAR_P (decl)
  5346. || TREE_CODE (decl) == RESULT_DECL);
  5347. gcc_assert (!TREE_STATIC (decl));
  5348. if (DECL_SIZE (decl) == NULL_TREE)
  5349. {
  5350. /* If we used it already as memory, it must stay in memory. */
  5351. DECL_INITIAL (decl) = NULL_TREE;
  5352. TREE_ADDRESSABLE (decl) = TREE_USED (decl);
  5353. return;
  5354. }
  5355. if (type == error_mark_node)
  5356. return;
  5357. /* Compute and store the initial value. */
  5358. already_used = TREE_USED (decl) || TREE_USED (type);
  5359. if (TREE_USED (type))
  5360. DECL_READ_P (decl) = 1;
  5361. /* Generate a cleanup, if necessary. */
  5362. cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
  5363. /* Perform the initialization. */
  5364. if (init)
  5365. {
  5366. tree rinit = (TREE_CODE (init) == INIT_EXPR
  5367. ? TREE_OPERAND (init, 1) : NULL_TREE);
  5368. if (rinit && !TREE_SIDE_EFFECTS (rinit))
  5369. {
  5370. /* Stick simple initializers in DECL_INITIAL so that
  5371. -Wno-init-self works (c++/34772). */
  5372. gcc_assert (TREE_OPERAND (init, 0) == decl);
  5373. DECL_INITIAL (decl) = rinit;
  5374. if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
  5375. {
  5376. STRIP_NOPS (rinit);
  5377. if (rinit == decl)
  5378. warning_at (DECL_SOURCE_LOCATION (decl),
  5379. OPT_Winit_self,
  5380. "reference %qD is initialized with itself", decl);
  5381. }
  5382. }
  5383. else
  5384. {
  5385. int saved_stmts_are_full_exprs_p;
  5386. /* If we're only initializing a single object, guard the
  5387. destructors of any temporaries used in its initializer with
  5388. its destructor. This isn't right for arrays because each
  5389. element initialization is a full-expression. */
  5390. if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
  5391. wrap_temporary_cleanups (init, cleanup);
  5392. gcc_assert (building_stmt_list_p ());
  5393. saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
  5394. current_stmt_tree ()->stmts_are_full_exprs_p = 1;
  5395. finish_expr_stmt (init);
  5396. current_stmt_tree ()->stmts_are_full_exprs_p =
  5397. saved_stmts_are_full_exprs_p;
  5398. }
  5399. }
  5400. /* Set this to 0 so we can tell whether an aggregate which was
  5401. initialized was ever used. Don't do this if it has a
  5402. destructor, so we don't complain about the 'resource
  5403. allocation is initialization' idiom. Now set
  5404. attribute((unused)) on types so decls of that type will be
  5405. marked used. (see TREE_USED, above.) */
  5406. if (TYPE_NEEDS_CONSTRUCTING (type)
  5407. && ! already_used
  5408. && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
  5409. && DECL_NAME (decl))
  5410. TREE_USED (decl) = 0;
  5411. else if (already_used)
  5412. TREE_USED (decl) = 1;
  5413. if (cleanup)
  5414. finish_decl_cleanup (decl, cleanup);
  5415. }
  5416. /* DECL is a VAR_DECL for a compiler-generated variable with static
  5417. storage duration (like a virtual table) whose initializer is a
  5418. compile-time constant. Initialize the variable and provide it to the
  5419. back end. */
  5420. void
  5421. initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
  5422. {
  5423. tree init;
  5424. gcc_assert (DECL_ARTIFICIAL (decl));
  5425. init = build_constructor (TREE_TYPE (decl), v);
  5426. gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
  5427. DECL_INITIAL (decl) = init;
  5428. DECL_INITIALIZED_P (decl) = 1;
  5429. determine_visibility (decl);
  5430. layout_var_decl (decl);
  5431. maybe_commonize_var (decl);
  5432. make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
  5433. }
  5434. /* INIT is the initializer for a variable, as represented by the
  5435. parser. Returns true iff INIT is type-dependent. */
  5436. static bool
  5437. type_dependent_init_p (tree init)
  5438. {
  5439. if (TREE_CODE (init) == TREE_LIST)
  5440. /* A parenthesized initializer, e.g.: int i (3, 2); ? */
  5441. return any_type_dependent_elements_p (init);
  5442. else if (TREE_CODE (init) == CONSTRUCTOR)
  5443. /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
  5444. {
  5445. vec<constructor_elt, va_gc> *elts;
  5446. size_t nelts;
  5447. size_t i;
  5448. elts = CONSTRUCTOR_ELTS (init);
  5449. nelts = vec_safe_length (elts);
  5450. for (i = 0; i < nelts; ++i)
  5451. if (type_dependent_init_p ((*elts)[i].value))
  5452. return true;
  5453. }
  5454. else
  5455. /* It must be a simple expression, e.g., int i = 3; */
  5456. return type_dependent_expression_p (init);
  5457. return false;
  5458. }
  5459. /* INIT is the initializer for a variable, as represented by the
  5460. parser. Returns true iff INIT is value-dependent. */
  5461. static bool
  5462. value_dependent_init_p (tree init)
  5463. {
  5464. if (TREE_CODE (init) == TREE_LIST)
  5465. /* A parenthesized initializer, e.g.: int i (3, 2); ? */
  5466. return any_value_dependent_elements_p (init);
  5467. else if (TREE_CODE (init) == CONSTRUCTOR)
  5468. /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
  5469. {
  5470. vec<constructor_elt, va_gc> *elts;
  5471. size_t nelts;
  5472. size_t i;
  5473. elts = CONSTRUCTOR_ELTS (init);
  5474. nelts = vec_safe_length (elts);
  5475. for (i = 0; i < nelts; ++i)
  5476. if (value_dependent_init_p ((*elts)[i].value))
  5477. return true;
  5478. }
  5479. else
  5480. /* It must be a simple expression, e.g., int i = 3; */
  5481. return value_dependent_expression_p (init);
  5482. return false;
  5483. }
  5484. /* Finish processing of a declaration;
  5485. install its line number and initial value.
  5486. If the length of an array type is not known before,
  5487. it must be determined now, from the initial value, or it is an error.
  5488. INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
  5489. true, then INIT is an integral constant expression.
  5490. FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
  5491. if the (init) syntax was used. */
  5492. void
  5493. cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
  5494. tree asmspec_tree, int flags)
  5495. {
  5496. tree type;
  5497. vec<tree, va_gc> *cleanups = NULL;
  5498. const char *asmspec = NULL;
  5499. int was_readonly = 0;
  5500. bool var_definition_p = false;
  5501. tree auto_node;
  5502. if (decl == error_mark_node)
  5503. return;
  5504. else if (! decl)
  5505. {
  5506. if (init)
  5507. error ("assignment (not initialization) in declaration");
  5508. return;
  5509. }
  5510. gcc_assert (TREE_CODE (decl) != RESULT_DECL);
  5511. /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
  5512. gcc_assert (TREE_CODE (decl) != PARM_DECL);
  5513. type = TREE_TYPE (decl);
  5514. if (type == error_mark_node)
  5515. return;
  5516. /* If a name was specified, get the string. */
  5517. if (at_namespace_scope_p ())
  5518. asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
  5519. if (asmspec_tree && asmspec_tree != error_mark_node)
  5520. asmspec = TREE_STRING_POINTER (asmspec_tree);
  5521. if (current_class_type
  5522. && CP_DECL_CONTEXT (decl) == current_class_type
  5523. && TYPE_BEING_DEFINED (current_class_type)
  5524. && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
  5525. && (DECL_INITIAL (decl) || init))
  5526. DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
  5527. if (TREE_CODE (decl) != FUNCTION_DECL
  5528. && (auto_node = type_uses_auto (type)))
  5529. {
  5530. tree d_init;
  5531. if (init == NULL_TREE)
  5532. {
  5533. if (DECL_LANG_SPECIFIC (decl)
  5534. && DECL_TEMPLATE_INSTANTIATION (decl)
  5535. && !DECL_TEMPLATE_INSTANTIATED (decl))
  5536. {
  5537. /* init is null because we're deferring instantiating the
  5538. initializer until we need it. Well, we need it now. */
  5539. instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
  5540. return;
  5541. }
  5542. error ("declaration of %q#D has no initializer", decl);
  5543. TREE_TYPE (decl) = error_mark_node;
  5544. return;
  5545. }
  5546. d_init = init;
  5547. if (TREE_CODE (d_init) == TREE_LIST)
  5548. d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
  5549. tf_warning_or_error);
  5550. d_init = resolve_nondeduced_context (d_init);
  5551. type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
  5552. auto_node);
  5553. if (type == error_mark_node)
  5554. return;
  5555. cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
  5556. }
  5557. if (!ensure_literal_type_for_constexpr_object (decl))
  5558. DECL_DECLARED_CONSTEXPR_P (decl) = 0;
  5559. if (VAR_P (decl)
  5560. && DECL_CLASS_SCOPE_P (decl)
  5561. && DECL_INITIALIZED_IN_CLASS_P (decl))
  5562. check_static_variable_definition (decl, type);
  5563. if (init && TREE_CODE (decl) == FUNCTION_DECL)
  5564. {
  5565. tree clone;
  5566. if (init == ridpointers[(int)RID_DELETE])
  5567. {
  5568. /* FIXME check this is 1st decl. */
  5569. DECL_DELETED_FN (decl) = 1;
  5570. DECL_DECLARED_INLINE_P (decl) = 1;
  5571. DECL_INITIAL (decl) = error_mark_node;
  5572. FOR_EACH_CLONE (clone, decl)
  5573. {
  5574. DECL_DELETED_FN (clone) = 1;
  5575. DECL_DECLARED_INLINE_P (clone) = 1;
  5576. DECL_INITIAL (clone) = error_mark_node;
  5577. }
  5578. init = NULL_TREE;
  5579. }
  5580. else if (init == ridpointers[(int)RID_DEFAULT])
  5581. {
  5582. if (defaultable_fn_check (decl))
  5583. DECL_DEFAULTED_FN (decl) = 1;
  5584. else
  5585. DECL_INITIAL (decl) = NULL_TREE;
  5586. }
  5587. }
  5588. if (init && VAR_P (decl))
  5589. {
  5590. DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
  5591. /* If DECL is a reference, then we want to know whether init is a
  5592. reference constant; init_const_expr_p as passed tells us whether
  5593. it's an rvalue constant. */
  5594. if (TREE_CODE (type) == REFERENCE_TYPE)
  5595. init_const_expr_p = potential_constant_expression (init);
  5596. if (init_const_expr_p)
  5597. {
  5598. /* Set these flags now for templates. We'll update the flags in
  5599. store_init_value for instantiations. */
  5600. DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
  5601. if (decl_maybe_constant_var_p (decl))
  5602. TREE_CONSTANT (decl) = 1;
  5603. }
  5604. }
  5605. if (processing_template_decl)
  5606. {
  5607. bool type_dependent_p;
  5608. /* Add this declaration to the statement-tree. */
  5609. if (at_function_scope_p ())
  5610. add_decl_expr (decl);
  5611. type_dependent_p = dependent_type_p (type);
  5612. if (check_for_bare_parameter_packs (init))
  5613. {
  5614. init = NULL_TREE;
  5615. DECL_INITIAL (decl) = NULL_TREE;
  5616. }
  5617. /* Generally, initializers in templates are expanded when the
  5618. template is instantiated. But, if DECL is a variable constant
  5619. then it can be used in future constant expressions, so its value
  5620. must be available. */
  5621. if (!VAR_P (decl) || dependent_type_p (type))
  5622. /* We can't do anything if the decl has dependent type. */;
  5623. else if (init
  5624. && init_const_expr_p
  5625. && !type_dependent_p
  5626. && TREE_CODE (type) != REFERENCE_TYPE
  5627. && decl_maybe_constant_var_p (decl)
  5628. && !type_dependent_init_p (init)
  5629. && !value_dependent_init_p (init))
  5630. {
  5631. /* This variable seems to be a non-dependent constant, so process
  5632. its initializer. If check_initializer returns non-null the
  5633. initialization wasn't constant after all. */
  5634. tree init_code;
  5635. cleanups = make_tree_vector ();
  5636. init_code = check_initializer (decl, init, flags, &cleanups);
  5637. if (init_code == NULL_TREE)
  5638. init = NULL_TREE;
  5639. release_tree_vector (cleanups);
  5640. }
  5641. else if (!DECL_PRETTY_FUNCTION_P (decl))
  5642. {
  5643. /* Deduce array size even if the initializer is dependent. */
  5644. maybe_deduce_size_from_array_init (decl, init);
  5645. /* And complain about multiple initializers. */
  5646. if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
  5647. && !MAYBE_CLASS_TYPE_P (type))
  5648. init = build_x_compound_expr_from_list (init, ELK_INIT,
  5649. tf_warning_or_error);
  5650. }
  5651. if (init)
  5652. DECL_INITIAL (decl) = init;
  5653. return;
  5654. }
  5655. /* Just store non-static data member initializers for later. */
  5656. if (init && TREE_CODE (decl) == FIELD_DECL)
  5657. DECL_INITIAL (decl) = init;
  5658. /* Take care of TYPE_DECLs up front. */
  5659. if (TREE_CODE (decl) == TYPE_DECL)
  5660. {
  5661. if (type != error_mark_node
  5662. && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
  5663. {
  5664. if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
  5665. warning (0, "shadowing previous type declaration of %q#D", decl);
  5666. set_identifier_type_value (DECL_NAME (decl), decl);
  5667. }
  5668. /* If we have installed this as the canonical typedef for this
  5669. type, and that type has not been defined yet, delay emitting
  5670. the debug information for it, as we will emit it later. */
  5671. if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
  5672. && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
  5673. TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
  5674. rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
  5675. at_eof);
  5676. return;
  5677. }
  5678. /* A reference will be modified here, as it is initialized. */
  5679. if (! DECL_EXTERNAL (decl)
  5680. && TREE_READONLY (decl)
  5681. && TREE_CODE (type) == REFERENCE_TYPE)
  5682. {
  5683. was_readonly = 1;
  5684. TREE_READONLY (decl) = 0;
  5685. }
  5686. if (VAR_P (decl))
  5687. {
  5688. /* If this is a local variable that will need a mangled name,
  5689. register it now. We must do this before processing the
  5690. initializer for the variable, since the initialization might
  5691. require a guard variable, and since the mangled name of the
  5692. guard variable will depend on the mangled name of this
  5693. variable. */
  5694. if (DECL_FUNCTION_SCOPE_P (decl)
  5695. && TREE_STATIC (decl)
  5696. && !DECL_ARTIFICIAL (decl))
  5697. {
  5698. push_local_name (decl);
  5699. if (DECL_CONSTRUCTOR_P (current_function_decl)
  5700. || DECL_DESTRUCTOR_P (current_function_decl))
  5701. /* Normally local_decls is populated during GIMPLE lowering,
  5702. but [cd]tors are never actually compiled directly. We need
  5703. to put statics on the list so we can deal with the label
  5704. address extension. FIXME. */
  5705. add_local_decl (cfun, decl);
  5706. }
  5707. /* Convert the initializer to the type of DECL, if we have not
  5708. already initialized DECL. */
  5709. if (!DECL_INITIALIZED_P (decl)
  5710. /* If !DECL_EXTERNAL then DECL is being defined. In the
  5711. case of a static data member initialized inside the
  5712. class-specifier, there can be an initializer even if DECL
  5713. is *not* defined. */
  5714. && (!DECL_EXTERNAL (decl) || init))
  5715. {
  5716. if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
  5717. {
  5718. tree jclass
  5719. = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
  5720. /* Allow libjava/prims.cc define primitive classes. */
  5721. if (init != NULL_TREE
  5722. || jclass == NULL_TREE
  5723. || TREE_CODE (jclass) != TYPE_DECL
  5724. || !POINTER_TYPE_P (TREE_TYPE (jclass))
  5725. || !same_type_ignoring_top_level_qualifiers_p
  5726. (type, TREE_TYPE (TREE_TYPE (jclass))))
  5727. error ("Java object %qD not allocated with %<new%>", decl);
  5728. init = NULL_TREE;
  5729. }
  5730. cleanups = make_tree_vector ();
  5731. init = check_initializer (decl, init, flags, &cleanups);
  5732. /* Handle:
  5733. [dcl.init]
  5734. The memory occupied by any object of static storage
  5735. duration is zero-initialized at program startup before
  5736. any other initialization takes place.
  5737. We cannot create an appropriate initializer until after
  5738. the type of DECL is finalized. If DECL_INITIAL is set,
  5739. then the DECL is statically initialized, and any
  5740. necessary zero-initialization has already been performed. */
  5741. if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
  5742. DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
  5743. /*nelts=*/NULL_TREE,
  5744. /*static_storage_p=*/true);
  5745. /* Remember that the initialization for this variable has
  5746. taken place. */
  5747. DECL_INITIALIZED_P (decl) = 1;
  5748. /* This declaration is the definition of this variable,
  5749. unless we are initializing a static data member within
  5750. the class specifier. */
  5751. if (!DECL_EXTERNAL (decl))
  5752. var_definition_p = true;
  5753. }
  5754. /* If the variable has an array type, lay out the type, even if
  5755. there is no initializer. It is valid to index through the
  5756. array, and we must get TYPE_ALIGN set correctly on the array
  5757. type. */
  5758. else if (TREE_CODE (type) == ARRAY_TYPE)
  5759. layout_type (type);
  5760. if (TREE_STATIC (decl)
  5761. && !at_function_scope_p ()
  5762. && current_function_decl == NULL)
  5763. /* So decl is a global variable or a static member of a
  5764. non local class. Record the types it uses
  5765. so that we can decide later to emit debug info for them. */
  5766. record_types_used_by_current_var_decl (decl);
  5767. }
  5768. else if (TREE_CODE (decl) == FIELD_DECL
  5769. && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
  5770. error ("non-static data member %qD has Java class type", decl);
  5771. /* Add this declaration to the statement-tree. This needs to happen
  5772. after the call to check_initializer so that the DECL_EXPR for a
  5773. reference temp is added before the DECL_EXPR for the reference itself. */
  5774. if (DECL_FUNCTION_SCOPE_P (decl))
  5775. {
  5776. /* If we're building a variable sized type, and we might be
  5777. reachable other than via the top of the current binding
  5778. level, then create a new BIND_EXPR so that we deallocate
  5779. the object at the right time. */
  5780. if (VAR_P (decl)
  5781. && DECL_SIZE (decl)
  5782. && !TREE_CONSTANT (DECL_SIZE (decl))
  5783. && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
  5784. {
  5785. tree bind;
  5786. bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
  5787. TREE_SIDE_EFFECTS (bind) = 1;
  5788. add_stmt (bind);
  5789. BIND_EXPR_BODY (bind) = push_stmt_list ();
  5790. }
  5791. add_decl_expr (decl);
  5792. }
  5793. /* Let the middle end know about variables and functions -- but not
  5794. static data members in uninstantiated class templates. */
  5795. if (VAR_OR_FUNCTION_DECL_P (decl))
  5796. {
  5797. if (VAR_P (decl))
  5798. {
  5799. layout_var_decl (decl);
  5800. maybe_commonize_var (decl);
  5801. }
  5802. /* This needs to happen after the linkage is set. */
  5803. determine_visibility (decl);
  5804. if (var_definition_p && TREE_STATIC (decl))
  5805. {
  5806. /* If a TREE_READONLY variable needs initialization
  5807. at runtime, it is no longer readonly and we need to
  5808. avoid MEM_READONLY_P being set on RTL created for it. */
  5809. if (init)
  5810. {
  5811. if (TREE_READONLY (decl))
  5812. TREE_READONLY (decl) = 0;
  5813. was_readonly = 0;
  5814. }
  5815. else if (was_readonly)
  5816. TREE_READONLY (decl) = 1;
  5817. /* Likewise if it needs destruction. */
  5818. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  5819. TREE_READONLY (decl) = 0;
  5820. }
  5821. make_rtl_for_nonlocal_decl (decl, init, asmspec);
  5822. /* Check for abstractness of the type. Notice that there is no
  5823. need to strip array types here since the check for those types
  5824. is already done within create_array_type_for_decl. */
  5825. abstract_virtuals_error (decl, type);
  5826. if (TREE_TYPE (decl) == error_mark_node)
  5827. /* No initialization required. */
  5828. ;
  5829. else if (TREE_CODE (decl) == FUNCTION_DECL)
  5830. {
  5831. if (init)
  5832. {
  5833. if (init == ridpointers[(int)RID_DEFAULT])
  5834. {
  5835. /* An out-of-class default definition is defined at
  5836. the point where it is explicitly defaulted. */
  5837. if (DECL_DELETED_FN (decl))
  5838. maybe_explain_implicit_delete (decl);
  5839. else if (DECL_INITIAL (decl) == error_mark_node)
  5840. synthesize_method (decl);
  5841. }
  5842. else
  5843. error ("function %q#D is initialized like a variable", decl);
  5844. }
  5845. /* else no initialization required. */
  5846. }
  5847. else if (DECL_EXTERNAL (decl)
  5848. && ! (DECL_LANG_SPECIFIC (decl)
  5849. && DECL_NOT_REALLY_EXTERN (decl)))
  5850. {
  5851. if (init)
  5852. DECL_INITIAL (decl) = init;
  5853. }
  5854. /* A variable definition. */
  5855. else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
  5856. /* Initialize the local variable. */
  5857. initialize_local_var (decl, init);
  5858. /* If a variable is defined, and then a subsequent
  5859. definition with external linkage is encountered, we will
  5860. get here twice for the same variable. We want to avoid
  5861. calling expand_static_init more than once. For variables
  5862. that are not static data members, we can call
  5863. expand_static_init only when we actually process the
  5864. initializer. It is not legal to redeclare a static data
  5865. member, so this issue does not arise in that case. */
  5866. else if (var_definition_p && TREE_STATIC (decl))
  5867. expand_static_init (decl, init);
  5868. }
  5869. /* If a CLEANUP_STMT was created to destroy a temporary bound to a
  5870. reference, insert it in the statement-tree now. */
  5871. if (cleanups)
  5872. {
  5873. unsigned i; tree t;
  5874. FOR_EACH_VEC_ELT (*cleanups, i, t)
  5875. push_cleanup (decl, t, false);
  5876. release_tree_vector (cleanups);
  5877. }
  5878. if (was_readonly)
  5879. TREE_READONLY (decl) = 1;
  5880. invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
  5881. }
  5882. /* Returns a declaration for a VAR_DECL as if:
  5883. extern "C" TYPE NAME;
  5884. had been seen. Used to create compiler-generated global
  5885. variables. */
  5886. static tree
  5887. declare_global_var (tree name, tree type)
  5888. {
  5889. tree decl;
  5890. push_to_top_level ();
  5891. decl = build_decl (input_location, VAR_DECL, name, type);
  5892. TREE_PUBLIC (decl) = 1;
  5893. DECL_EXTERNAL (decl) = 1;
  5894. DECL_ARTIFICIAL (decl) = 1;
  5895. /* If the user has explicitly declared this variable (perhaps
  5896. because the code we are compiling is part of a low-level runtime
  5897. library), then it is possible that our declaration will be merged
  5898. with theirs by pushdecl. */
  5899. decl = pushdecl (decl);
  5900. cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
  5901. pop_from_top_level ();
  5902. return decl;
  5903. }
  5904. /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
  5905. if "__cxa_atexit" is not being used) corresponding to the function
  5906. to be called when the program exits. */
  5907. static tree
  5908. get_atexit_fn_ptr_type (void)
  5909. {
  5910. tree fn_type;
  5911. if (!atexit_fn_ptr_type_node)
  5912. {
  5913. tree arg_type;
  5914. if (flag_use_cxa_atexit
  5915. && !targetm.cxx.use_atexit_for_cxa_atexit ())
  5916. /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
  5917. arg_type = ptr_type_node;
  5918. else
  5919. /* The parameter to "atexit" is "void (*)(void)". */
  5920. arg_type = NULL_TREE;
  5921. fn_type = build_function_type_list (void_type_node,
  5922. arg_type, NULL_TREE);
  5923. atexit_fn_ptr_type_node = build_pointer_type (fn_type);
  5924. }
  5925. return atexit_fn_ptr_type_node;
  5926. }
  5927. /* Returns a pointer to the `atexit' function. Note that if
  5928. FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
  5929. `__cxa_atexit' function specified in the IA64 C++ ABI. */
  5930. static tree
  5931. get_atexit_node (void)
  5932. {
  5933. tree atexit_fndecl;
  5934. tree fn_type;
  5935. tree fn_ptr_type;
  5936. const char *name;
  5937. bool use_aeabi_atexit;
  5938. if (atexit_node)
  5939. return atexit_node;
  5940. if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
  5941. {
  5942. /* The declaration for `__cxa_atexit' is:
  5943. int __cxa_atexit (void (*)(void *), void *, void *)
  5944. We build up the argument types and then the function type
  5945. itself. */
  5946. tree argtype0, argtype1, argtype2;
  5947. use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
  5948. /* First, build the pointer-to-function type for the first
  5949. argument. */
  5950. fn_ptr_type = get_atexit_fn_ptr_type ();
  5951. /* Then, build the rest of the argument types. */
  5952. argtype2 = ptr_type_node;
  5953. if (use_aeabi_atexit)
  5954. {
  5955. argtype1 = fn_ptr_type;
  5956. argtype0 = ptr_type_node;
  5957. }
  5958. else
  5959. {
  5960. argtype1 = ptr_type_node;
  5961. argtype0 = fn_ptr_type;
  5962. }
  5963. /* And the final __cxa_atexit type. */
  5964. fn_type = build_function_type_list (integer_type_node,
  5965. argtype0, argtype1, argtype2,
  5966. NULL_TREE);
  5967. if (use_aeabi_atexit)
  5968. name = "__aeabi_atexit";
  5969. else
  5970. name = "__cxa_atexit";
  5971. }
  5972. else
  5973. {
  5974. /* The declaration for `atexit' is:
  5975. int atexit (void (*)());
  5976. We build up the argument types and then the function type
  5977. itself. */
  5978. fn_ptr_type = get_atexit_fn_ptr_type ();
  5979. /* Build the final atexit type. */
  5980. fn_type = build_function_type_list (integer_type_node,
  5981. fn_ptr_type, NULL_TREE);
  5982. name = "atexit";
  5983. }
  5984. /* Now, build the function declaration. */
  5985. push_lang_context (lang_name_c);
  5986. atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
  5987. mark_used (atexit_fndecl);
  5988. pop_lang_context ();
  5989. atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
  5990. return atexit_node;
  5991. }
  5992. /* Like get_atexit_node, but for thread-local cleanups. */
  5993. static tree
  5994. get_thread_atexit_node (void)
  5995. {
  5996. /* The declaration for `__cxa_thread_atexit' is:
  5997. int __cxa_thread_atexit (void (*)(void *), void *, void *) */
  5998. tree fn_type = build_function_type_list (integer_type_node,
  5999. get_atexit_fn_ptr_type (),
  6000. ptr_type_node, ptr_type_node,
  6001. NULL_TREE);
  6002. /* Now, build the function declaration. */
  6003. tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
  6004. ECF_LEAF | ECF_NOTHROW);
  6005. return decay_conversion (atexit_fndecl, tf_warning_or_error);
  6006. }
  6007. /* Returns the __dso_handle VAR_DECL. */
  6008. static tree
  6009. get_dso_handle_node (void)
  6010. {
  6011. if (dso_handle_node)
  6012. return dso_handle_node;
  6013. /* Declare the variable. */
  6014. dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
  6015. ptr_type_node);
  6016. #ifdef HAVE_GAS_HIDDEN
  6017. if (dso_handle_node != error_mark_node)
  6018. {
  6019. DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
  6020. DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
  6021. }
  6022. #endif
  6023. return dso_handle_node;
  6024. }
  6025. /* Begin a new function with internal linkage whose job will be simply
  6026. to destroy some particular variable. */
  6027. static GTY(()) int start_cleanup_cnt;
  6028. static tree
  6029. start_cleanup_fn (void)
  6030. {
  6031. char name[32];
  6032. tree fntype;
  6033. tree fndecl;
  6034. bool use_cxa_atexit = flag_use_cxa_atexit
  6035. && !targetm.cxx.use_atexit_for_cxa_atexit ();
  6036. push_to_top_level ();
  6037. /* No need to mangle this. */
  6038. push_lang_context (lang_name_c);
  6039. /* Build the name of the function. */
  6040. sprintf (name, "__tcf_%d", start_cleanup_cnt++);
  6041. /* Build the function declaration. */
  6042. fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
  6043. fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
  6044. /* It's a function with internal linkage, generated by the
  6045. compiler. */
  6046. TREE_PUBLIC (fndecl) = 0;
  6047. DECL_ARTIFICIAL (fndecl) = 1;
  6048. /* Make the function `inline' so that it is only emitted if it is
  6049. actually needed. It is unlikely that it will be inlined, since
  6050. it is only called via a function pointer, but we avoid unnecessary
  6051. emissions this way. */
  6052. DECL_DECLARED_INLINE_P (fndecl) = 1;
  6053. DECL_INTERFACE_KNOWN (fndecl) = 1;
  6054. /* Build the parameter. */
  6055. if (use_cxa_atexit)
  6056. {
  6057. tree parmdecl;
  6058. parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
  6059. DECL_CONTEXT (parmdecl) = fndecl;
  6060. TREE_USED (parmdecl) = 1;
  6061. DECL_READ_P (parmdecl) = 1;
  6062. DECL_ARGUMENTS (fndecl) = parmdecl;
  6063. }
  6064. pushdecl (fndecl);
  6065. start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
  6066. pop_lang_context ();
  6067. return current_function_decl;
  6068. }
  6069. /* Finish the cleanup function begun by start_cleanup_fn. */
  6070. static void
  6071. end_cleanup_fn (void)
  6072. {
  6073. expand_or_defer_fn (finish_function (0));
  6074. pop_from_top_level ();
  6075. }
  6076. /* Generate code to handle the destruction of DECL, an object with
  6077. static storage duration. */
  6078. tree
  6079. register_dtor_fn (tree decl)
  6080. {
  6081. tree cleanup;
  6082. tree addr;
  6083. tree compound_stmt;
  6084. tree fcall;
  6085. tree type;
  6086. bool ob_parm, dso_parm, use_dtor;
  6087. tree arg0, arg1, arg2;
  6088. tree atex_node;
  6089. type = TREE_TYPE (decl);
  6090. if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  6091. return void_node;
  6092. /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
  6093. "__aeabi_atexit"), and DECL is a class object, we can just pass the
  6094. destructor to "__cxa_atexit"; we don't have to build a temporary
  6095. function to do the cleanup. */
  6096. dso_parm = (flag_use_cxa_atexit
  6097. && !targetm.cxx.use_atexit_for_cxa_atexit ());
  6098. ob_parm = (DECL_THREAD_LOCAL_P (decl) || dso_parm);
  6099. use_dtor = ob_parm && CLASS_TYPE_P (type);
  6100. if (use_dtor)
  6101. {
  6102. int idx;
  6103. /* Find the destructor. */
  6104. idx = lookup_fnfields_1 (type, complete_dtor_identifier);
  6105. gcc_assert (idx >= 0);
  6106. cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
  6107. /* Make sure it is accessible. */
  6108. perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
  6109. tf_warning_or_error);
  6110. }
  6111. else
  6112. {
  6113. /* Call build_cleanup before we enter the anonymous function so
  6114. that any access checks will be done relative to the current
  6115. scope, rather than the scope of the anonymous function. */
  6116. build_cleanup (decl);
  6117. /* Now start the function. */
  6118. cleanup = start_cleanup_fn ();
  6119. /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
  6120. to the original function, rather than the anonymous one. That
  6121. will make the back end think that nested functions are in use,
  6122. which causes confusion. */
  6123. push_deferring_access_checks (dk_no_check);
  6124. fcall = build_cleanup (decl);
  6125. pop_deferring_access_checks ();
  6126. /* Create the body of the anonymous function. */
  6127. compound_stmt = begin_compound_stmt (BCS_FN_BODY);
  6128. finish_expr_stmt (fcall);
  6129. finish_compound_stmt (compound_stmt);
  6130. end_cleanup_fn ();
  6131. }
  6132. /* Call atexit with the cleanup function. */
  6133. mark_used (cleanup);
  6134. cleanup = build_address (cleanup);
  6135. if (DECL_THREAD_LOCAL_P (decl))
  6136. atex_node = get_thread_atexit_node ();
  6137. else
  6138. atex_node = get_atexit_node ();
  6139. if (use_dtor)
  6140. {
  6141. /* We must convert CLEANUP to the type that "__cxa_atexit"
  6142. expects. */
  6143. cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
  6144. /* "__cxa_atexit" will pass the address of DECL to the
  6145. cleanup function. */
  6146. mark_used (decl);
  6147. addr = build_address (decl);
  6148. /* The declared type of the parameter to "__cxa_atexit" is
  6149. "void *". For plain "T*", we could just let the
  6150. machinery in cp_build_function_call convert it -- but if the
  6151. type is "cv-qualified T *", then we need to convert it
  6152. before passing it in, to avoid spurious errors. */
  6153. addr = build_nop (ptr_type_node, addr);
  6154. }
  6155. else
  6156. /* Since the cleanup functions we build ignore the address
  6157. they're given, there's no reason to pass the actual address
  6158. in, and, in general, it's cheaper to pass NULL than any
  6159. other value. */
  6160. addr = null_pointer_node;
  6161. if (dso_parm)
  6162. arg2 = cp_build_addr_expr (get_dso_handle_node (),
  6163. tf_warning_or_error);
  6164. else if (ob_parm)
  6165. /* Just pass NULL to the dso handle parm if we don't actually
  6166. have a DSO handle on this target. */
  6167. arg2 = null_pointer_node;
  6168. else
  6169. arg2 = NULL_TREE;
  6170. if (ob_parm)
  6171. {
  6172. if (!DECL_THREAD_LOCAL_P (decl)
  6173. && targetm.cxx.use_aeabi_atexit ())
  6174. {
  6175. arg1 = cleanup;
  6176. arg0 = addr;
  6177. }
  6178. else
  6179. {
  6180. arg1 = addr;
  6181. arg0 = cleanup;
  6182. }
  6183. }
  6184. else
  6185. {
  6186. arg0 = cleanup;
  6187. arg1 = NULL_TREE;
  6188. }
  6189. return cp_build_function_call_nary (atex_node, tf_warning_or_error,
  6190. arg0, arg1, arg2, NULL_TREE);
  6191. }
  6192. /* DECL is a VAR_DECL with static storage duration. INIT, if present,
  6193. is its initializer. Generate code to handle the construction
  6194. and destruction of DECL. */
  6195. static void
  6196. expand_static_init (tree decl, tree init)
  6197. {
  6198. gcc_assert (VAR_P (decl));
  6199. gcc_assert (TREE_STATIC (decl));
  6200. /* Some variables require no dynamic initialization. */
  6201. if (!init
  6202. && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
  6203. {
  6204. /* Make sure the destructor is callable. */
  6205. cxx_maybe_build_cleanup (decl, tf_warning_or_error);
  6206. return;
  6207. }
  6208. if (DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
  6209. && !DECL_FUNCTION_SCOPE_P (decl))
  6210. {
  6211. if (init)
  6212. error ("non-local variable %qD declared %<__thread%> "
  6213. "needs dynamic initialization", decl);
  6214. else
  6215. error ("non-local variable %qD declared %<__thread%> "
  6216. "has a non-trivial destructor", decl);
  6217. static bool informed;
  6218. if (!informed)
  6219. {
  6220. inform (DECL_SOURCE_LOCATION (decl),
  6221. "C++11 %<thread_local%> allows dynamic initialization "
  6222. "and destruction");
  6223. informed = true;
  6224. }
  6225. return;
  6226. }
  6227. if (DECL_FUNCTION_SCOPE_P (decl))
  6228. {
  6229. /* Emit code to perform this initialization but once. */
  6230. tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
  6231. tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
  6232. tree guard, guard_addr;
  6233. tree flag, begin;
  6234. /* We don't need thread-safety code for thread-local vars. */
  6235. bool thread_guard = (flag_threadsafe_statics
  6236. && !DECL_THREAD_LOCAL_P (decl));
  6237. /* Emit code to perform this initialization but once. This code
  6238. looks like:
  6239. static <type> guard;
  6240. if (!guard.first_byte) {
  6241. if (__cxa_guard_acquire (&guard)) {
  6242. bool flag = false;
  6243. try {
  6244. // Do initialization.
  6245. flag = true; __cxa_guard_release (&guard);
  6246. // Register variable for destruction at end of program.
  6247. } catch {
  6248. if (!flag) __cxa_guard_abort (&guard);
  6249. }
  6250. }
  6251. Note that the `flag' variable is only set to 1 *after* the
  6252. initialization is complete. This ensures that an exception,
  6253. thrown during the construction, will cause the variable to
  6254. reinitialized when we pass through this code again, as per:
  6255. [stmt.dcl]
  6256. If the initialization exits by throwing an exception, the
  6257. initialization is not complete, so it will be tried again
  6258. the next time control enters the declaration.
  6259. This process should be thread-safe, too; multiple threads
  6260. should not be able to initialize the variable more than
  6261. once. */
  6262. /* Create the guard variable. */
  6263. guard = get_guard (decl);
  6264. /* This optimization isn't safe on targets with relaxed memory
  6265. consistency. On such targets we force synchronization in
  6266. __cxa_guard_acquire. */
  6267. if (!targetm.relaxed_ordering || !thread_guard)
  6268. {
  6269. /* Begin the conditional initialization. */
  6270. if_stmt = begin_if_stmt ();
  6271. finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
  6272. then_clause = begin_compound_stmt (BCS_NO_SCOPE);
  6273. }
  6274. if (thread_guard)
  6275. {
  6276. tree vfntype = NULL_TREE;
  6277. tree acquire_name, release_name, abort_name;
  6278. tree acquire_fn, release_fn, abort_fn;
  6279. guard_addr = build_address (guard);
  6280. acquire_name = get_identifier ("__cxa_guard_acquire");
  6281. release_name = get_identifier ("__cxa_guard_release");
  6282. abort_name = get_identifier ("__cxa_guard_abort");
  6283. acquire_fn = identifier_global_value (acquire_name);
  6284. release_fn = identifier_global_value (release_name);
  6285. abort_fn = identifier_global_value (abort_name);
  6286. if (!acquire_fn)
  6287. acquire_fn = push_library_fn
  6288. (acquire_name, build_function_type_list (integer_type_node,
  6289. TREE_TYPE (guard_addr),
  6290. NULL_TREE),
  6291. NULL_TREE, ECF_NOTHROW | ECF_LEAF);
  6292. if (!release_fn || !abort_fn)
  6293. vfntype = build_function_type_list (void_type_node,
  6294. TREE_TYPE (guard_addr),
  6295. NULL_TREE);
  6296. if (!release_fn)
  6297. release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
  6298. ECF_NOTHROW | ECF_LEAF);
  6299. if (!abort_fn)
  6300. abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
  6301. ECF_NOTHROW | ECF_LEAF);
  6302. inner_if_stmt = begin_if_stmt ();
  6303. finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
  6304. inner_if_stmt);
  6305. inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
  6306. begin = get_target_expr (boolean_false_node);
  6307. flag = TARGET_EXPR_SLOT (begin);
  6308. TARGET_EXPR_CLEANUP (begin)
  6309. = build3 (COND_EXPR, void_type_node, flag,
  6310. void_node,
  6311. build_call_n (abort_fn, 1, guard_addr));
  6312. CLEANUP_EH_ONLY (begin) = 1;
  6313. /* Do the initialization itself. */
  6314. init = add_stmt_to_compound (begin, init);
  6315. init = add_stmt_to_compound
  6316. (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
  6317. init = add_stmt_to_compound
  6318. (init, build_call_n (release_fn, 1, guard_addr));
  6319. }
  6320. else
  6321. init = add_stmt_to_compound (init, set_guard (guard));
  6322. /* Use atexit to register a function for destroying this static
  6323. variable. */
  6324. init = add_stmt_to_compound (init, register_dtor_fn (decl));
  6325. finish_expr_stmt (init);
  6326. if (thread_guard)
  6327. {
  6328. finish_compound_stmt (inner_then_clause);
  6329. finish_then_clause (inner_if_stmt);
  6330. finish_if_stmt (inner_if_stmt);
  6331. }
  6332. if (!targetm.relaxed_ordering || !thread_guard)
  6333. {
  6334. finish_compound_stmt (then_clause);
  6335. finish_then_clause (if_stmt);
  6336. finish_if_stmt (if_stmt);
  6337. }
  6338. }
  6339. else if (DECL_THREAD_LOCAL_P (decl))
  6340. tls_aggregates = tree_cons (init, decl, tls_aggregates);
  6341. else
  6342. static_aggregates = tree_cons (init, decl, static_aggregates);
  6343. }
  6344. /* Make TYPE a complete type based on INITIAL_VALUE.
  6345. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
  6346. 2 if there was no information (in which case assume 0 if DO_DEFAULT),
  6347. 3 if the initializer list is empty (in pedantic mode). */
  6348. int
  6349. cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
  6350. {
  6351. int failure;
  6352. tree type, elt_type;
  6353. /* Don't get confused by a CONSTRUCTOR for some other type. */
  6354. if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
  6355. && !BRACE_ENCLOSED_INITIALIZER_P (initial_value))
  6356. return 1;
  6357. if (initial_value)
  6358. {
  6359. unsigned HOST_WIDE_INT i;
  6360. tree value;
  6361. /* An array of character type can be initialized from a
  6362. brace-enclosed string constant.
  6363. FIXME: this code is duplicated from reshape_init. Probably
  6364. we should just call reshape_init here? */
  6365. if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
  6366. && TREE_CODE (initial_value) == CONSTRUCTOR
  6367. && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
  6368. {
  6369. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
  6370. tree value = (*v)[0].value;
  6371. if (TREE_CODE (value) == STRING_CST
  6372. && v->length () == 1)
  6373. initial_value = value;
  6374. }
  6375. /* If any of the elements are parameter packs, we can't actually
  6376. complete this type now because the array size is dependent. */
  6377. if (TREE_CODE (initial_value) == CONSTRUCTOR)
  6378. {
  6379. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
  6380. i, value)
  6381. {
  6382. if (PACK_EXPANSION_P (value))
  6383. return 0;
  6384. }
  6385. }
  6386. }
  6387. failure = complete_array_type (ptype, initial_value, do_default);
  6388. /* We can create the array before the element type is complete, which
  6389. means that we didn't have these two bits set in the original type
  6390. either. In completing the type, we are expected to propagate these
  6391. bits. See also complete_type which does the same thing for arrays
  6392. of fixed size. */
  6393. type = *ptype;
  6394. if (TYPE_DOMAIN (type))
  6395. {
  6396. elt_type = TREE_TYPE (type);
  6397. TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
  6398. TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
  6399. = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
  6400. }
  6401. return failure;
  6402. }
  6403. /* As above, but either give an error or reject zero-size arrays, depending
  6404. on COMPLAIN. */
  6405. int
  6406. cp_complete_array_type_or_error (tree *ptype, tree initial_value,
  6407. bool do_default, tsubst_flags_t complain)
  6408. {
  6409. int failure;
  6410. bool sfinae = !(complain & tf_error);
  6411. /* In SFINAE context we can't be lenient about zero-size arrays. */
  6412. if (sfinae)
  6413. ++pedantic;
  6414. failure = cp_complete_array_type (ptype, initial_value, do_default);
  6415. if (sfinae)
  6416. --pedantic;
  6417. if (failure)
  6418. {
  6419. if (sfinae)
  6420. /* Not an error. */;
  6421. else if (failure == 1)
  6422. error ("initializer fails to determine size of %qT", *ptype);
  6423. else if (failure == 2)
  6424. {
  6425. if (do_default)
  6426. error ("array size missing in %qT", *ptype);
  6427. }
  6428. else if (failure == 3)
  6429. error ("zero-size array %qT", *ptype);
  6430. *ptype = error_mark_node;
  6431. }
  6432. return failure;
  6433. }
  6434. /* Return zero if something is declared to be a member of type
  6435. CTYPE when in the context of CUR_TYPE. STRING is the error
  6436. message to print in that case. Otherwise, quietly return 1. */
  6437. static int
  6438. member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
  6439. {
  6440. if (ctype && ctype != cur_type)
  6441. {
  6442. if (flags == DTOR_FLAG)
  6443. error ("destructor for alien class %qT cannot be a member", ctype);
  6444. else
  6445. error ("constructor for alien class %qT cannot be a member", ctype);
  6446. return 0;
  6447. }
  6448. return 1;
  6449. }
  6450. /* Subroutine of `grokdeclarator'. */
  6451. /* Generate errors possibly applicable for a given set of specifiers.
  6452. This is for ARM $7.1.2. */
  6453. static void
  6454. bad_specifiers (tree object,
  6455. enum bad_spec_place type,
  6456. int virtualp,
  6457. int quals,
  6458. int inlinep,
  6459. int friendp,
  6460. int raises)
  6461. {
  6462. switch (type)
  6463. {
  6464. case BSP_VAR:
  6465. if (virtualp)
  6466. error ("%qD declared as a %<virtual%> variable", object);
  6467. if (inlinep)
  6468. error ("%qD declared as an %<inline%> variable", object);
  6469. if (quals)
  6470. error ("%<const%> and %<volatile%> function specifiers on "
  6471. "%qD invalid in variable declaration", object);
  6472. break;
  6473. case BSP_PARM:
  6474. if (virtualp)
  6475. error ("%qD declared as a %<virtual%> parameter", object);
  6476. if (inlinep)
  6477. error ("%qD declared as an %<inline%> parameter", object);
  6478. if (quals)
  6479. error ("%<const%> and %<volatile%> function specifiers on "
  6480. "%qD invalid in parameter declaration", object);
  6481. break;
  6482. case BSP_TYPE:
  6483. if (virtualp)
  6484. error ("%qD declared as a %<virtual%> type", object);
  6485. if (inlinep)
  6486. error ("%qD declared as an %<inline%> type", object);
  6487. if (quals)
  6488. error ("%<const%> and %<volatile%> function specifiers on "
  6489. "%qD invalid in type declaration", object);
  6490. break;
  6491. case BSP_FIELD:
  6492. if (virtualp)
  6493. error ("%qD declared as a %<virtual%> field", object);
  6494. if (inlinep)
  6495. error ("%qD declared as an %<inline%> field", object);
  6496. if (quals)
  6497. error ("%<const%> and %<volatile%> function specifiers on "
  6498. "%qD invalid in field declaration", object);
  6499. break;
  6500. default:
  6501. gcc_unreachable();
  6502. }
  6503. if (friendp)
  6504. error ("%q+D declared as a friend", object);
  6505. if (raises
  6506. && (TREE_CODE (object) == TYPE_DECL
  6507. || (!TYPE_PTRFN_P (TREE_TYPE (object))
  6508. && !TYPE_REFFN_P (TREE_TYPE (object))
  6509. && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
  6510. error ("%q+D declared with an exception specification", object);
  6511. }
  6512. /* DECL is a member function or static data member and is presently
  6513. being defined. Check that the definition is taking place in a
  6514. valid namespace. */
  6515. static void
  6516. check_class_member_definition_namespace (tree decl)
  6517. {
  6518. /* These checks only apply to member functions and static data
  6519. members. */
  6520. gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
  6521. /* We check for problems with specializations in pt.c in
  6522. check_specialization_namespace, where we can issue better
  6523. diagnostics. */
  6524. if (processing_specialization)
  6525. return;
  6526. /* There are no restrictions on the placement of
  6527. explicit instantiations. */
  6528. if (processing_explicit_instantiation)
  6529. return;
  6530. /* [class.mfct]
  6531. A member function definition that appears outside of the
  6532. class definition shall appear in a namespace scope enclosing
  6533. the class definition.
  6534. [class.static.data]
  6535. The definition for a static data member shall appear in a
  6536. namespace scope enclosing the member's class definition. */
  6537. if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
  6538. permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
  6539. decl, DECL_CONTEXT (decl));
  6540. }
  6541. /* Build a PARM_DECL for the "this" parameter. TYPE is the
  6542. METHOD_TYPE for a non-static member function; QUALS are the
  6543. cv-qualifiers that apply to the function. */
  6544. tree
  6545. build_this_parm (tree type, cp_cv_quals quals)
  6546. {
  6547. tree this_type;
  6548. tree qual_type;
  6549. tree parm;
  6550. cp_cv_quals this_quals;
  6551. if (CLASS_TYPE_P (type))
  6552. {
  6553. this_type
  6554. = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
  6555. this_type = build_pointer_type (this_type);
  6556. }
  6557. else
  6558. this_type = type_of_this_parm (type);
  6559. /* The `this' parameter is implicitly `const'; it cannot be
  6560. assigned to. */
  6561. this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
  6562. qual_type = cp_build_qualified_type (this_type, this_quals);
  6563. parm = build_artificial_parm (this_identifier, qual_type);
  6564. cp_apply_type_quals_to_decl (this_quals, parm);
  6565. return parm;
  6566. }
  6567. /* DECL is a static member function. Complain if it was declared
  6568. with function-cv-quals. */
  6569. static void
  6570. check_static_quals (tree decl, cp_cv_quals quals)
  6571. {
  6572. if (quals != TYPE_UNQUALIFIED)
  6573. error ("static member function %q#D declared with type qualifiers",
  6574. decl);
  6575. }
  6576. /* Helper function. Replace the temporary this parameter injected
  6577. during cp_finish_omp_declare_simd with the real this parameter. */
  6578. static tree
  6579. declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
  6580. {
  6581. tree this_parm = (tree) data;
  6582. if (TREE_CODE (*tp) == PARM_DECL
  6583. && DECL_NAME (*tp) == this_identifier
  6584. && *tp != this_parm)
  6585. *tp = this_parm;
  6586. else if (TYPE_P (*tp))
  6587. *walk_subtrees = 0;
  6588. return NULL_TREE;
  6589. }
  6590. /* CTYPE is class type, or null if non-class.
  6591. TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
  6592. or METHOD_TYPE.
  6593. DECLARATOR is the function's name.
  6594. PARMS is a chain of PARM_DECLs for the function.
  6595. VIRTUALP is truthvalue of whether the function is virtual or not.
  6596. FLAGS are to be passed through to `grokclassfn'.
  6597. QUALS are qualifiers indicating whether the function is `const'
  6598. or `volatile'.
  6599. RAISES is a list of exceptions that this function can raise.
  6600. CHECK is 1 if we must find this method in CTYPE, 0 if we should
  6601. not look, and -1 if we should not call `grokclassfn' at all.
  6602. SFK is the kind of special function (if any) for the new function.
  6603. Returns `NULL_TREE' if something goes wrong, after issuing
  6604. applicable error messages. */
  6605. static tree
  6606. grokfndecl (tree ctype,
  6607. tree type,
  6608. tree declarator,
  6609. tree parms,
  6610. tree orig_declarator,
  6611. int virtualp,
  6612. enum overload_flags flags,
  6613. cp_cv_quals quals,
  6614. cp_ref_qualifier rqual,
  6615. tree raises,
  6616. int check,
  6617. int friendp,
  6618. int publicp,
  6619. int inlinep,
  6620. bool deletedp,
  6621. special_function_kind sfk,
  6622. bool funcdef_flag,
  6623. int template_count,
  6624. tree in_namespace,
  6625. tree* attrlist,
  6626. location_t location)
  6627. {
  6628. tree decl;
  6629. int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
  6630. tree t;
  6631. if (rqual)
  6632. type = build_ref_qualified_type (type, rqual);
  6633. if (raises)
  6634. type = build_exception_variant (type, raises);
  6635. decl = build_lang_decl (FUNCTION_DECL, declarator, type);
  6636. /* If we have an explicit location, use it, otherwise use whatever
  6637. build_lang_decl used (probably input_location). */
  6638. if (location != UNKNOWN_LOCATION)
  6639. DECL_SOURCE_LOCATION (decl) = location;
  6640. if (TREE_CODE (type) == METHOD_TYPE)
  6641. {
  6642. tree parm;
  6643. parm = build_this_parm (type, quals);
  6644. DECL_CHAIN (parm) = parms;
  6645. parms = parm;
  6646. }
  6647. DECL_ARGUMENTS (decl) = parms;
  6648. for (t = parms; t; t = DECL_CHAIN (t))
  6649. DECL_CONTEXT (t) = decl;
  6650. /* Propagate volatile out from type to decl. */
  6651. if (TYPE_VOLATILE (type))
  6652. TREE_THIS_VOLATILE (decl) = 1;
  6653. /* Setup decl according to sfk. */
  6654. switch (sfk)
  6655. {
  6656. case sfk_constructor:
  6657. case sfk_copy_constructor:
  6658. case sfk_move_constructor:
  6659. DECL_CONSTRUCTOR_P (decl) = 1;
  6660. break;
  6661. case sfk_destructor:
  6662. DECL_DESTRUCTOR_P (decl) = 1;
  6663. break;
  6664. default:
  6665. break;
  6666. }
  6667. /* If pointers to member functions use the least significant bit to
  6668. indicate whether a function is virtual, ensure a pointer
  6669. to this function will have that bit clear. */
  6670. if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
  6671. && TREE_CODE (type) == METHOD_TYPE
  6672. && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
  6673. DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
  6674. if (friendp
  6675. && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
  6676. {
  6677. if (funcdef_flag)
  6678. error
  6679. ("defining explicit specialization %qD in friend declaration",
  6680. orig_declarator);
  6681. else
  6682. {
  6683. tree fns = TREE_OPERAND (orig_declarator, 0);
  6684. tree args = TREE_OPERAND (orig_declarator, 1);
  6685. if (PROCESSING_REAL_TEMPLATE_DECL_P ())
  6686. {
  6687. /* Something like `template <class T> friend void f<T>()'. */
  6688. error ("invalid use of template-id %qD in declaration "
  6689. "of primary template",
  6690. orig_declarator);
  6691. return NULL_TREE;
  6692. }
  6693. /* A friend declaration of the form friend void f<>(). Record
  6694. the information in the TEMPLATE_ID_EXPR. */
  6695. SET_DECL_IMPLICIT_INSTANTIATION (decl);
  6696. gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
  6697. DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
  6698. for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
  6699. if (TREE_PURPOSE (t)
  6700. && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
  6701. {
  6702. error ("default arguments are not allowed in declaration "
  6703. "of friend template specialization %qD",
  6704. decl);
  6705. return NULL_TREE;
  6706. }
  6707. if (inlinep & 1)
  6708. error ("%<inline%> is not allowed in declaration of friend "
  6709. "template specialization %qD",
  6710. decl);
  6711. if (inlinep & 2)
  6712. error ("%<constexpr%> is not allowed in declaration of friend "
  6713. "template specialization %qD",
  6714. decl);
  6715. if (inlinep)
  6716. return NULL_TREE;
  6717. }
  6718. }
  6719. /* If this decl has namespace scope, set that up. */
  6720. if (in_namespace)
  6721. set_decl_namespace (decl, in_namespace, friendp);
  6722. else if (!ctype)
  6723. DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
  6724. /* `main' and builtins have implicit 'C' linkage. */
  6725. if ((MAIN_NAME_P (declarator)
  6726. || (IDENTIFIER_LENGTH (declarator) > 10
  6727. && IDENTIFIER_POINTER (declarator)[0] == '_'
  6728. && IDENTIFIER_POINTER (declarator)[1] == '_'
  6729. && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
  6730. || (targetcm.cxx_implicit_extern_c
  6731. && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
  6732. && current_lang_name == lang_name_cplusplus
  6733. && ctype == NULL_TREE
  6734. && DECL_FILE_SCOPE_P (decl))
  6735. SET_DECL_LANGUAGE (decl, lang_c);
  6736. /* Should probably propagate const out from type to decl I bet (mrs). */
  6737. if (staticp)
  6738. {
  6739. DECL_STATIC_FUNCTION_P (decl) = 1;
  6740. DECL_CONTEXT (decl) = ctype;
  6741. }
  6742. if (deletedp)
  6743. DECL_DELETED_FN (decl) = 1;
  6744. if (ctype)
  6745. {
  6746. DECL_CONTEXT (decl) = ctype;
  6747. if (funcdef_flag)
  6748. check_class_member_definition_namespace (decl);
  6749. }
  6750. if (ctype == NULL_TREE && DECL_MAIN_P (decl))
  6751. {
  6752. if (PROCESSING_REAL_TEMPLATE_DECL_P())
  6753. error ("cannot declare %<::main%> to be a template");
  6754. if (inlinep & 1)
  6755. error ("cannot declare %<::main%> to be inline");
  6756. if (inlinep & 2)
  6757. error ("cannot declare %<::main%> to be constexpr");
  6758. if (!publicp)
  6759. error ("cannot declare %<::main%> to be static");
  6760. inlinep = 0;
  6761. publicp = 1;
  6762. }
  6763. /* Members of anonymous types and local classes have no linkage; make
  6764. them internal. If a typedef is made later, this will be changed. */
  6765. if (ctype && (TYPE_ANONYMOUS_P (ctype)
  6766. || decl_function_context (TYPE_MAIN_DECL (ctype))))
  6767. publicp = 0;
  6768. if (publicp && cxx_dialect == cxx98)
  6769. {
  6770. /* [basic.link]: A name with no linkage (notably, the name of a class
  6771. or enumeration declared in a local scope) shall not be used to
  6772. declare an entity with linkage.
  6773. DR 757 relaxes this restriction for C++0x. */
  6774. no_linkage_error (decl);
  6775. }
  6776. TREE_PUBLIC (decl) = publicp;
  6777. if (! publicp)
  6778. {
  6779. DECL_INTERFACE_KNOWN (decl) = 1;
  6780. DECL_NOT_REALLY_EXTERN (decl) = 1;
  6781. }
  6782. /* If the declaration was declared inline, mark it as such. */
  6783. if (inlinep)
  6784. {
  6785. DECL_DECLARED_INLINE_P (decl) = 1;
  6786. if (publicp)
  6787. DECL_COMDAT (decl) = 1;
  6788. }
  6789. if (inlinep & 2)
  6790. DECL_DECLARED_CONSTEXPR_P (decl) = true;
  6791. DECL_EXTERNAL (decl) = 1;
  6792. if (TREE_CODE (type) == FUNCTION_TYPE)
  6793. {
  6794. if (quals)
  6795. {
  6796. error (ctype
  6797. ? G_("static member function %qD cannot have cv-qualifier")
  6798. : G_("non-member function %qD cannot have cv-qualifier"),
  6799. decl);
  6800. quals = TYPE_UNQUALIFIED;
  6801. }
  6802. if (rqual)
  6803. {
  6804. error (ctype
  6805. ? G_("static member function %qD cannot have ref-qualifier")
  6806. : G_("non-member function %qD cannot have ref-qualifier"),
  6807. decl);
  6808. rqual = REF_QUAL_NONE;
  6809. }
  6810. }
  6811. if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
  6812. && !grok_op_properties (decl, /*complain=*/true))
  6813. return NULL_TREE;
  6814. else if (UDLIT_OPER_P (DECL_NAME (decl)))
  6815. {
  6816. bool long_long_unsigned_p;
  6817. bool long_double_p;
  6818. const char *suffix = NULL;
  6819. /* [over.literal]/6: Literal operators shall not have C linkage. */
  6820. if (DECL_LANGUAGE (decl) == lang_c)
  6821. {
  6822. error ("literal operator with C linkage");
  6823. return NULL_TREE;
  6824. }
  6825. if (DECL_NAMESPACE_SCOPE_P (decl))
  6826. {
  6827. if (!check_literal_operator_args (decl, &long_long_unsigned_p,
  6828. &long_double_p))
  6829. {
  6830. error ("%qD has invalid argument list", decl);
  6831. return NULL_TREE;
  6832. }
  6833. suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
  6834. if (long_long_unsigned_p)
  6835. {
  6836. if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
  6837. warning (0, "integer suffix %<%s%>"
  6838. " shadowed by implementation", suffix);
  6839. }
  6840. else if (long_double_p)
  6841. {
  6842. if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
  6843. warning (0, "floating point suffix %<%s%>"
  6844. " shadowed by implementation", suffix);
  6845. }
  6846. }
  6847. else
  6848. {
  6849. error ("%qD must be a non-member function", decl);
  6850. return NULL_TREE;
  6851. }
  6852. }
  6853. if (funcdef_flag)
  6854. /* Make the init_value nonzero so pushdecl knows this is not
  6855. tentative. error_mark_node is replaced later with the BLOCK. */
  6856. DECL_INITIAL (decl) = error_mark_node;
  6857. if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
  6858. TREE_NOTHROW (decl) = 1;
  6859. if (flag_openmp || flag_openmp_simd || flag_cilkplus)
  6860. {
  6861. /* Adjust "omp declare simd" attributes. */
  6862. tree ods = lookup_attribute ("omp declare simd", *attrlist);
  6863. if (ods)
  6864. {
  6865. tree attr;
  6866. for (attr = ods; attr;
  6867. attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
  6868. {
  6869. if (TREE_CODE (type) == METHOD_TYPE)
  6870. walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
  6871. DECL_ARGUMENTS (decl), NULL);
  6872. if (TREE_VALUE (attr) != NULL_TREE)
  6873. {
  6874. tree cl = TREE_VALUE (TREE_VALUE (attr));
  6875. cl = c_omp_declare_simd_clauses_to_numbers
  6876. (DECL_ARGUMENTS (decl), cl);
  6877. if (cl)
  6878. TREE_VALUE (TREE_VALUE (attr)) = cl;
  6879. else
  6880. TREE_VALUE (attr) = NULL_TREE;
  6881. }
  6882. }
  6883. }
  6884. }
  6885. /* Caller will do the rest of this. */
  6886. if (check < 0)
  6887. return decl;
  6888. if (ctype != NULL_TREE)
  6889. grokclassfn (ctype, decl, flags);
  6890. /* 12.4/3 */
  6891. if (cxx_dialect >= cxx11
  6892. && DECL_DESTRUCTOR_P (decl)
  6893. && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
  6894. && !processing_template_decl)
  6895. deduce_noexcept_on_destructor (decl);
  6896. decl = check_explicit_specialization (orig_declarator, decl,
  6897. template_count,
  6898. 2 * funcdef_flag +
  6899. 4 * (friendp != 0));
  6900. if (decl == error_mark_node)
  6901. return NULL_TREE;
  6902. if (DECL_STATIC_FUNCTION_P (decl))
  6903. check_static_quals (decl, quals);
  6904. if (attrlist)
  6905. {
  6906. cplus_decl_attributes (&decl, *attrlist, 0);
  6907. *attrlist = NULL_TREE;
  6908. }
  6909. /* Check main's type after attributes have been applied. */
  6910. if (ctype == NULL_TREE && DECL_MAIN_P (decl))
  6911. {
  6912. if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
  6913. integer_type_node))
  6914. {
  6915. tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
  6916. tree newtype;
  6917. error ("%<::main%> must return %<int%>");
  6918. newtype = build_function_type (integer_type_node, oldtypeargs);
  6919. TREE_TYPE (decl) = newtype;
  6920. }
  6921. if (warn_main)
  6922. check_main_parameter_types (decl);
  6923. }
  6924. if (ctype != NULL_TREE
  6925. && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
  6926. && check)
  6927. {
  6928. tree old_decl = check_classfn (ctype, decl,
  6929. (processing_template_decl
  6930. > template_class_depth (ctype))
  6931. ? current_template_parms
  6932. : NULL_TREE);
  6933. if (old_decl == error_mark_node)
  6934. return NULL_TREE;
  6935. if (old_decl)
  6936. {
  6937. tree ok;
  6938. tree pushed_scope;
  6939. if (TREE_CODE (old_decl) == TEMPLATE_DECL)
  6940. /* Because grokfndecl is always supposed to return a
  6941. FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
  6942. here. We depend on our callers to figure out that its
  6943. really a template that's being returned. */
  6944. old_decl = DECL_TEMPLATE_RESULT (old_decl);
  6945. if (DECL_STATIC_FUNCTION_P (old_decl)
  6946. && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
  6947. {
  6948. /* Remove the `this' parm added by grokclassfn. */
  6949. revert_static_member_fn (decl);
  6950. check_static_quals (decl, quals);
  6951. }
  6952. if (DECL_ARTIFICIAL (old_decl))
  6953. {
  6954. error ("definition of implicitly-declared %qD", old_decl);
  6955. return NULL_TREE;
  6956. }
  6957. else if (DECL_DEFAULTED_FN (old_decl))
  6958. {
  6959. error ("definition of explicitly-defaulted %q+D", decl);
  6960. error ("%q+#D explicitly defaulted here", old_decl);
  6961. return NULL_TREE;
  6962. }
  6963. /* Since we've smashed OLD_DECL to its
  6964. DECL_TEMPLATE_RESULT, we must do the same to DECL. */
  6965. if (TREE_CODE (decl) == TEMPLATE_DECL)
  6966. decl = DECL_TEMPLATE_RESULT (decl);
  6967. /* Attempt to merge the declarations. This can fail, in
  6968. the case of some invalid specialization declarations. */
  6969. pushed_scope = push_scope (ctype);
  6970. ok = duplicate_decls (decl, old_decl, friendp);
  6971. if (pushed_scope)
  6972. pop_scope (pushed_scope);
  6973. if (!ok)
  6974. {
  6975. error ("no %q#D member function declared in class %qT",
  6976. decl, ctype);
  6977. return NULL_TREE;
  6978. }
  6979. if (ok == error_mark_node)
  6980. return NULL_TREE;
  6981. return old_decl;
  6982. }
  6983. }
  6984. if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
  6985. return NULL_TREE;
  6986. if (ctype == NULL_TREE || check)
  6987. return decl;
  6988. if (virtualp)
  6989. DECL_VIRTUAL_P (decl) = 1;
  6990. return decl;
  6991. }
  6992. /* decl is a FUNCTION_DECL.
  6993. specifiers are the parsed virt-specifiers.
  6994. Set flags to reflect the virt-specifiers.
  6995. Returns decl. */
  6996. static tree
  6997. set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
  6998. {
  6999. if (decl == NULL_TREE)
  7000. return decl;
  7001. if (specifiers & VIRT_SPEC_OVERRIDE)
  7002. DECL_OVERRIDE_P (decl) = 1;
  7003. if (specifiers & VIRT_SPEC_FINAL)
  7004. DECL_FINAL_P (decl) = 1;
  7005. return decl;
  7006. }
  7007. /* DECL is a VAR_DECL for a static data member. Set flags to reflect
  7008. the linkage that DECL will receive in the object file. */
  7009. static void
  7010. set_linkage_for_static_data_member (tree decl)
  7011. {
  7012. /* A static data member always has static storage duration and
  7013. external linkage. Note that static data members are forbidden in
  7014. local classes -- the only situation in which a class has
  7015. non-external linkage. */
  7016. TREE_PUBLIC (decl) = 1;
  7017. TREE_STATIC (decl) = 1;
  7018. /* For non-template classes, static data members are always put
  7019. out in exactly those files where they are defined, just as
  7020. with ordinary namespace-scope variables. */
  7021. if (!processing_template_decl)
  7022. DECL_INTERFACE_KNOWN (decl) = 1;
  7023. }
  7024. /* Create a VAR_DECL named NAME with the indicated TYPE.
  7025. If SCOPE is non-NULL, it is the class type or namespace containing
  7026. the variable. If SCOPE is NULL, the variable should is created in
  7027. the innermost enclosing scope. */
  7028. static tree
  7029. grokvardecl (tree type,
  7030. tree name,
  7031. tree orig_declarator,
  7032. const cp_decl_specifier_seq *declspecs,
  7033. int initialized,
  7034. int constp,
  7035. int template_count,
  7036. tree scope)
  7037. {
  7038. tree decl;
  7039. tree explicit_scope;
  7040. gcc_assert (!name || identifier_p (name));
  7041. /* Compute the scope in which to place the variable, but remember
  7042. whether or not that scope was explicitly specified by the user. */
  7043. explicit_scope = scope;
  7044. if (!scope)
  7045. {
  7046. /* An explicit "extern" specifier indicates a namespace-scope
  7047. variable. */
  7048. if (declspecs->storage_class == sc_extern)
  7049. scope = current_decl_namespace ();
  7050. else if (!at_function_scope_p ())
  7051. scope = current_scope ();
  7052. }
  7053. if (scope
  7054. && (/* If the variable is a namespace-scope variable declared in a
  7055. template, we need DECL_LANG_SPECIFIC. */
  7056. (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
  7057. /* Similarly for namespace-scope variables with language linkage
  7058. other than C++. */
  7059. || (TREE_CODE (scope) == NAMESPACE_DECL
  7060. && current_lang_name != lang_name_cplusplus)
  7061. /* Similarly for static data members. */
  7062. || TYPE_P (scope)
  7063. /* Similarly for explicit specializations. */
  7064. || (orig_declarator
  7065. && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
  7066. decl = build_lang_decl (VAR_DECL, name, type);
  7067. else
  7068. decl = build_decl (input_location, VAR_DECL, name, type);
  7069. if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
  7070. set_decl_namespace (decl, explicit_scope, 0);
  7071. else
  7072. DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
  7073. if (declspecs->storage_class == sc_extern)
  7074. {
  7075. DECL_THIS_EXTERN (decl) = 1;
  7076. DECL_EXTERNAL (decl) = !initialized;
  7077. }
  7078. if (DECL_CLASS_SCOPE_P (decl))
  7079. {
  7080. set_linkage_for_static_data_member (decl);
  7081. /* This function is only called with out-of-class definitions. */
  7082. DECL_EXTERNAL (decl) = 0;
  7083. check_class_member_definition_namespace (decl);
  7084. }
  7085. /* At top level, either `static' or no s.c. makes a definition
  7086. (perhaps tentative), and absence of `static' makes it public. */
  7087. else if (toplevel_bindings_p ())
  7088. {
  7089. TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
  7090. && (DECL_THIS_EXTERN (decl) || ! constp));
  7091. TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
  7092. }
  7093. /* Not at top level, only `static' makes a static definition. */
  7094. else
  7095. {
  7096. TREE_STATIC (decl) = declspecs->storage_class == sc_static;
  7097. TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
  7098. }
  7099. if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
  7100. {
  7101. if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
  7102. set_decl_tls_model (decl, decl_default_tls_model (decl));
  7103. if (declspecs->gnu_thread_keyword_p)
  7104. DECL_GNU_TLS_P (decl) = true;
  7105. }
  7106. /* If the type of the decl has no linkage, make sure that we'll
  7107. notice that in mark_used. */
  7108. if (cxx_dialect > cxx98
  7109. && decl_linkage (decl) != lk_none
  7110. && DECL_LANG_SPECIFIC (decl) == NULL
  7111. && !DECL_EXTERN_C_P (decl)
  7112. && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
  7113. retrofit_lang_decl (decl);
  7114. if (TREE_PUBLIC (decl))
  7115. {
  7116. /* [basic.link]: A name with no linkage (notably, the name of a class
  7117. or enumeration declared in a local scope) shall not be used to
  7118. declare an entity with linkage.
  7119. DR 757 relaxes this restriction for C++0x. */
  7120. if (cxx_dialect < cxx11)
  7121. no_linkage_error (decl);
  7122. }
  7123. else
  7124. DECL_INTERFACE_KNOWN (decl) = 1;
  7125. // Handle explicit specializations and instantiations of variable templates.
  7126. if (orig_declarator
  7127. /* For GCC 5 fix 65646 this way. */
  7128. && current_tmpl_spec_kind (template_count) != tsk_none)
  7129. decl = check_explicit_specialization (orig_declarator, decl,
  7130. template_count, 0);
  7131. return decl != error_mark_node ? decl : NULL_TREE;
  7132. }
  7133. /* Create and return a canonical pointer to member function type, for
  7134. TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
  7135. tree
  7136. build_ptrmemfunc_type (tree type)
  7137. {
  7138. tree field, fields;
  7139. tree t;
  7140. if (type == error_mark_node)
  7141. return type;
  7142. /* Make sure that we always have the unqualified pointer-to-member
  7143. type first. */
  7144. if (cp_cv_quals quals = cp_type_quals (type))
  7145. {
  7146. tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
  7147. return cp_build_qualified_type (unqual, quals);
  7148. }
  7149. /* If a canonical type already exists for this type, use it. We use
  7150. this method instead of type_hash_canon, because it only does a
  7151. simple equality check on the list of field members. */
  7152. if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
  7153. return t;
  7154. t = make_node (RECORD_TYPE);
  7155. /* Let the front end know this is a pointer to member function. */
  7156. TYPE_PTRMEMFUNC_FLAG (t) = 1;
  7157. field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
  7158. fields = field;
  7159. field = build_decl (input_location, FIELD_DECL, delta_identifier,
  7160. delta_type_node);
  7161. DECL_CHAIN (field) = fields;
  7162. fields = field;
  7163. finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
  7164. /* Zap out the name so that the back end will give us the debugging
  7165. information for this anonymous RECORD_TYPE. */
  7166. TYPE_NAME (t) = NULL_TREE;
  7167. /* Cache this pointer-to-member type so that we can find it again
  7168. later. */
  7169. TYPE_SET_PTRMEMFUNC_TYPE (type, t);
  7170. if (TYPE_STRUCTURAL_EQUALITY_P (type))
  7171. SET_TYPE_STRUCTURAL_EQUALITY (t);
  7172. else if (TYPE_CANONICAL (type) != type)
  7173. TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
  7174. return t;
  7175. }
  7176. /* Create and return a pointer to data member type. */
  7177. tree
  7178. build_ptrmem_type (tree class_type, tree member_type)
  7179. {
  7180. if (TREE_CODE (member_type) == METHOD_TYPE)
  7181. {
  7182. cp_cv_quals quals = type_memfn_quals (member_type);
  7183. cp_ref_qualifier rqual = type_memfn_rqual (member_type);
  7184. member_type = build_memfn_type (member_type, class_type, quals, rqual);
  7185. return build_ptrmemfunc_type (build_pointer_type (member_type));
  7186. }
  7187. else
  7188. {
  7189. gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
  7190. return build_offset_type (class_type, member_type);
  7191. }
  7192. }
  7193. /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
  7194. Check to see that the definition is valid. Issue appropriate error
  7195. messages. Return 1 if the definition is particularly bad, or 0
  7196. otherwise. */
  7197. static int
  7198. check_static_variable_definition (tree decl, tree type)
  7199. {
  7200. /* Can't check yet if we don't know the type. */
  7201. if (dependent_type_p (type))
  7202. return 0;
  7203. /* If DECL is declared constexpr, we'll do the appropriate checks
  7204. in check_initializer. */
  7205. if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
  7206. return 0;
  7207. else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  7208. {
  7209. if (!COMPLETE_TYPE_P (type))
  7210. error ("in-class initialization of static data member %q#D of "
  7211. "incomplete type", decl);
  7212. else if (literal_type_p (type))
  7213. permerror (input_location,
  7214. "%<constexpr%> needed for in-class initialization of "
  7215. "static data member %q#D of non-integral type", decl);
  7216. else
  7217. error ("in-class initialization of static data member %q#D of "
  7218. "non-literal type", decl);
  7219. return 1;
  7220. }
  7221. /* Motion 10 at San Diego: If a static const integral data member is
  7222. initialized with an integral constant expression, the initializer
  7223. may appear either in the declaration (within the class), or in
  7224. the definition, but not both. If it appears in the class, the
  7225. member is a member constant. The file-scope definition is always
  7226. required. */
  7227. if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
  7228. {
  7229. error ("invalid in-class initialization of static data member "
  7230. "of non-integral type %qT",
  7231. type);
  7232. return 1;
  7233. }
  7234. else if (!CP_TYPE_CONST_P (type))
  7235. error ("ISO C++ forbids in-class initialization of non-const "
  7236. "static member %qD",
  7237. decl);
  7238. else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  7239. pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
  7240. "%qD of non-integral type %qT", decl, type);
  7241. return 0;
  7242. }
  7243. /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
  7244. SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
  7245. expressions out into temporary variables so that walk_tree doesn't
  7246. step into them (c++/15764). */
  7247. static tree
  7248. stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
  7249. {
  7250. hash_set<tree> *pset = (hash_set<tree> *)data;
  7251. tree expr = *expr_p;
  7252. if (TREE_CODE (expr) == SAVE_EXPR)
  7253. {
  7254. tree op = TREE_OPERAND (expr, 0);
  7255. cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
  7256. if (TREE_SIDE_EFFECTS (op))
  7257. TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
  7258. *walk_subtrees = 0;
  7259. }
  7260. else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
  7261. *walk_subtrees = 0;
  7262. return NULL;
  7263. }
  7264. /* Entry point for the above. */
  7265. static void
  7266. stabilize_vla_size (tree size)
  7267. {
  7268. hash_set<tree> pset;
  7269. /* Break out any function calls into temporary variables. */
  7270. cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
  7271. }
  7272. /* Helper function for compute_array_index_type. Look for SIZEOF_EXPR
  7273. not inside of SAVE_EXPR and fold them. */
  7274. static tree
  7275. fold_sizeof_expr_r (tree *expr_p, int *walk_subtrees, void *data)
  7276. {
  7277. tree expr = *expr_p;
  7278. if (TREE_CODE (expr) == SAVE_EXPR || TYPE_P (expr))
  7279. *walk_subtrees = 0;
  7280. else if (TREE_CODE (expr) == SIZEOF_EXPR)
  7281. {
  7282. *(bool *)data = true;
  7283. if (SIZEOF_EXPR_TYPE_P (expr))
  7284. expr = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (expr, 0)),
  7285. SIZEOF_EXPR, false);
  7286. else if (TYPE_P (TREE_OPERAND (expr, 0)))
  7287. expr = cxx_sizeof_or_alignof_type (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
  7288. false);
  7289. else
  7290. expr = cxx_sizeof_or_alignof_expr (TREE_OPERAND (expr, 0), SIZEOF_EXPR,
  7291. false);
  7292. if (expr == error_mark_node)
  7293. expr = size_one_node;
  7294. *expr_p = expr;
  7295. *walk_subtrees = 0;
  7296. }
  7297. return NULL;
  7298. }
  7299. /* Given the SIZE (i.e., number of elements) in an array, compute an
  7300. appropriate index type for the array. If non-NULL, NAME is the
  7301. name of the thing being declared. */
  7302. tree
  7303. compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
  7304. {
  7305. tree itype;
  7306. tree osize = size;
  7307. if (error_operand_p (size))
  7308. return error_mark_node;
  7309. if (!type_dependent_expression_p (size))
  7310. {
  7311. tree type = TREE_TYPE (size);
  7312. mark_rvalue_use (size);
  7313. if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
  7314. && TREE_SIDE_EFFECTS (size))
  7315. /* In C++98, we mark a non-constant array bound with a magic
  7316. NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
  7317. else
  7318. {
  7319. size = instantiate_non_dependent_expr_sfinae (size, complain);
  7320. if (CLASS_TYPE_P (type)
  7321. && CLASSTYPE_LITERAL_P (type))
  7322. {
  7323. size = build_expr_type_conversion (WANT_INT, size, true);
  7324. if (!size)
  7325. {
  7326. if (!(complain & tf_error))
  7327. return error_mark_node;
  7328. if (name)
  7329. error ("size of array %qD has non-integral type %qT",
  7330. name, type);
  7331. else
  7332. error ("size of array has non-integral type %qT", type);
  7333. size = integer_one_node;
  7334. }
  7335. if (size == error_mark_node)
  7336. return error_mark_node;
  7337. type = TREE_TYPE (size);
  7338. }
  7339. if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
  7340. size = maybe_constant_value (size);
  7341. if (!TREE_CONSTANT (size))
  7342. size = osize;
  7343. }
  7344. if (error_operand_p (size))
  7345. return error_mark_node;
  7346. /* The array bound must be an integer type. */
  7347. if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
  7348. {
  7349. if (!(complain & tf_error))
  7350. return error_mark_node;
  7351. if (name)
  7352. error ("size of array %qD has non-integral type %qT", name, type);
  7353. else
  7354. error ("size of array has non-integral type %qT", type);
  7355. size = integer_one_node;
  7356. type = TREE_TYPE (size);
  7357. }
  7358. }
  7359. /* A type is dependent if it is...an array type constructed from any
  7360. dependent type or whose size is specified by a constant expression
  7361. that is value-dependent. */
  7362. /* We can only call value_dependent_expression_p on integral constant
  7363. expressions; treat non-constant expressions as dependent, too. */
  7364. if (processing_template_decl
  7365. && (type_dependent_expression_p (size)
  7366. || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
  7367. {
  7368. /* We cannot do any checking for a SIZE that isn't known to be
  7369. constant. Just build the index type and mark that it requires
  7370. structural equality checks. */
  7371. itype = build_index_type (build_min (MINUS_EXPR, sizetype,
  7372. size, size_one_node));
  7373. TYPE_DEPENDENT_P (itype) = 1;
  7374. TYPE_DEPENDENT_P_VALID (itype) = 1;
  7375. SET_TYPE_STRUCTURAL_EQUALITY (itype);
  7376. return itype;
  7377. }
  7378. /* Normally, the array-bound will be a constant. */
  7379. if (TREE_CODE (size) == INTEGER_CST)
  7380. {
  7381. /* Check to see if the array bound overflowed. Make that an
  7382. error, no matter how generous we're being. */
  7383. constant_expression_error (size);
  7384. /* An array must have a positive number of elements. */
  7385. if (tree_int_cst_lt (size, integer_zero_node))
  7386. {
  7387. if (!(complain & tf_error))
  7388. return error_mark_node;
  7389. if (name)
  7390. error ("size of array %qD is negative", name);
  7391. else
  7392. error ("size of array is negative");
  7393. size = integer_one_node;
  7394. }
  7395. /* As an extension we allow zero-sized arrays. */
  7396. else if (integer_zerop (size))
  7397. {
  7398. if (!(complain & tf_error))
  7399. /* We must fail if performing argument deduction (as
  7400. indicated by the state of complain), so that
  7401. another substitution can be found. */
  7402. return error_mark_node;
  7403. else if (in_system_header_at (input_location))
  7404. /* Allow them in system headers because glibc uses them. */;
  7405. else if (name)
  7406. pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
  7407. else
  7408. pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
  7409. }
  7410. }
  7411. else if (TREE_CONSTANT (size)
  7412. /* We don't allow VLAs at non-function scopes, or during
  7413. tentative template substitution. */
  7414. || !at_function_scope_p ()
  7415. || !(complain & tf_error))
  7416. {
  7417. if (!(complain & tf_error))
  7418. return error_mark_node;
  7419. /* `(int) &fn' is not a valid array bound. */
  7420. if (name)
  7421. error ("size of array %qD is not an integral constant-expression",
  7422. name);
  7423. else
  7424. error ("size of array is not an integral constant-expression");
  7425. size = integer_one_node;
  7426. }
  7427. else if (pedantic && warn_vla != 0)
  7428. {
  7429. if (name)
  7430. pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
  7431. else
  7432. pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
  7433. }
  7434. else if (warn_vla > 0)
  7435. {
  7436. if (name)
  7437. warning (OPT_Wvla,
  7438. "variable length array %qD is used", name);
  7439. else
  7440. warning (OPT_Wvla,
  7441. "variable length array is used");
  7442. }
  7443. if (processing_template_decl && !TREE_CONSTANT (size))
  7444. /* A variable sized array. */
  7445. itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
  7446. else
  7447. {
  7448. HOST_WIDE_INT saved_processing_template_decl;
  7449. /* Compute the index of the largest element in the array. It is
  7450. one less than the number of elements in the array. We save
  7451. and restore PROCESSING_TEMPLATE_DECL so that computations in
  7452. cp_build_binary_op will be appropriately folded. */
  7453. saved_processing_template_decl = processing_template_decl;
  7454. processing_template_decl = 0;
  7455. itype = cp_build_binary_op (input_location,
  7456. MINUS_EXPR,
  7457. cp_convert (ssizetype, size, complain),
  7458. cp_convert (ssizetype, integer_one_node,
  7459. complain),
  7460. complain);
  7461. itype = fold (itype);
  7462. processing_template_decl = saved_processing_template_decl;
  7463. if (!TREE_CONSTANT (itype))
  7464. {
  7465. /* A variable sized array. */
  7466. itype = variable_size (itype);
  7467. if (TREE_CODE (itype) != SAVE_EXPR)
  7468. {
  7469. /* Look for SIZEOF_EXPRs in itype and fold them, otherwise
  7470. they might survive till gimplification. */
  7471. tree newitype = itype;
  7472. bool found = false;
  7473. cp_walk_tree_without_duplicates (&newitype,
  7474. fold_sizeof_expr_r, &found);
  7475. if (found)
  7476. itype = variable_size (fold (newitype));
  7477. }
  7478. stabilize_vla_size (itype);
  7479. if (flag_sanitize & SANITIZE_VLA
  7480. && do_ubsan_in_current_function ())
  7481. {
  7482. /* We have to add 1 -- in the ubsan routine we generate
  7483. LE_EXPR rather than LT_EXPR. */
  7484. tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
  7485. build_one_cst (TREE_TYPE (itype)));
  7486. t = ubsan_instrument_vla (input_location, t);
  7487. finish_expr_stmt (t);
  7488. }
  7489. }
  7490. /* Make sure that there was no overflow when creating to a signed
  7491. index type. (For example, on a 32-bit machine, an array with
  7492. size 2^32 - 1 is too big.) */
  7493. else if (TREE_CODE (itype) == INTEGER_CST
  7494. && TREE_OVERFLOW (itype))
  7495. {
  7496. if (!(complain & tf_error))
  7497. return error_mark_node;
  7498. error ("overflow in array dimension");
  7499. TREE_OVERFLOW (itype) = 0;
  7500. }
  7501. }
  7502. /* Create and return the appropriate index type. */
  7503. itype = build_index_type (itype);
  7504. /* If the index type were dependent, we would have returned early, so
  7505. remember that it isn't. */
  7506. TYPE_DEPENDENT_P (itype) = 0;
  7507. TYPE_DEPENDENT_P_VALID (itype) = 1;
  7508. return itype;
  7509. }
  7510. /* Returns the scope (if any) in which the entity declared by
  7511. DECLARATOR will be located. If the entity was declared with an
  7512. unqualified name, NULL_TREE is returned. */
  7513. tree
  7514. get_scope_of_declarator (const cp_declarator *declarator)
  7515. {
  7516. while (declarator && declarator->kind != cdk_id)
  7517. declarator = declarator->declarator;
  7518. /* If the declarator-id is a SCOPE_REF, the scope in which the
  7519. declaration occurs is the first operand. */
  7520. if (declarator
  7521. && declarator->u.id.qualifying_scope)
  7522. return declarator->u.id.qualifying_scope;
  7523. /* Otherwise, the declarator is not a qualified name; the entity will
  7524. be declared in the current scope. */
  7525. return NULL_TREE;
  7526. }
  7527. /* Returns an ARRAY_TYPE for an array with SIZE elements of the
  7528. indicated TYPE. If non-NULL, NAME is the NAME of the declaration
  7529. with this type. */
  7530. static tree
  7531. create_array_type_for_decl (tree name, tree type, tree size)
  7532. {
  7533. tree itype = NULL_TREE;
  7534. /* If things have already gone awry, bail now. */
  7535. if (type == error_mark_node || size == error_mark_node)
  7536. return error_mark_node;
  7537. /* 8.3.4/1: If the type of the identifier of D contains the auto
  7538. type-specifier, the program is ill-formed. */
  7539. if (type_uses_auto (type))
  7540. {
  7541. error ("%qD declared as array of %qT", name, type);
  7542. return error_mark_node;
  7543. }
  7544. /* If there are some types which cannot be array elements,
  7545. issue an error-message and return. */
  7546. switch (TREE_CODE (type))
  7547. {
  7548. case VOID_TYPE:
  7549. if (name)
  7550. error ("declaration of %qD as array of void", name);
  7551. else
  7552. error ("creating array of void");
  7553. return error_mark_node;
  7554. case FUNCTION_TYPE:
  7555. if (name)
  7556. error ("declaration of %qD as array of functions", name);
  7557. else
  7558. error ("creating array of functions");
  7559. return error_mark_node;
  7560. case REFERENCE_TYPE:
  7561. if (name)
  7562. error ("declaration of %qD as array of references", name);
  7563. else
  7564. error ("creating array of references");
  7565. return error_mark_node;
  7566. case METHOD_TYPE:
  7567. if (name)
  7568. error ("declaration of %qD as array of function members", name);
  7569. else
  7570. error ("creating array of function members");
  7571. return error_mark_node;
  7572. default:
  7573. break;
  7574. }
  7575. /* [dcl.array]
  7576. The constant expressions that specify the bounds of the arrays
  7577. can be omitted only for the first member of the sequence. */
  7578. if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
  7579. {
  7580. if (name)
  7581. error ("declaration of %qD as multidimensional array must "
  7582. "have bounds for all dimensions except the first",
  7583. name);
  7584. else
  7585. error ("multidimensional array must have bounds for all "
  7586. "dimensions except the first");
  7587. return error_mark_node;
  7588. }
  7589. /* Figure out the index type for the array. */
  7590. if (size)
  7591. itype = compute_array_index_type (name, size, tf_warning_or_error);
  7592. /* [dcl.array]
  7593. T is called the array element type; this type shall not be [...] an
  7594. abstract class type. */
  7595. abstract_virtuals_error (name, type);
  7596. return build_cplus_array_type (type, itype);
  7597. }
  7598. /* Check that it's OK to declare a function with the indicated TYPE.
  7599. SFK indicates the kind of special function (if any) that this
  7600. function is. OPTYPE is the type given in a conversion operator
  7601. declaration, or the class type for a constructor/destructor.
  7602. Returns the actual return type of the function; that
  7603. may be different than TYPE if an error occurs, or for certain
  7604. special functions. */
  7605. static tree
  7606. check_special_function_return_type (special_function_kind sfk,
  7607. tree type,
  7608. tree optype)
  7609. {
  7610. switch (sfk)
  7611. {
  7612. case sfk_constructor:
  7613. if (type)
  7614. error ("return type specification for constructor invalid");
  7615. if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
  7616. type = build_pointer_type (optype);
  7617. else
  7618. type = void_type_node;
  7619. break;
  7620. case sfk_destructor:
  7621. if (type)
  7622. error ("return type specification for destructor invalid");
  7623. /* We can't use the proper return type here because we run into
  7624. problems with ambiguous bases and covariant returns.
  7625. Java classes are left unchanged because (void *) isn't a valid
  7626. Java type, and we don't want to change the Java ABI. */
  7627. if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
  7628. type = build_pointer_type (void_type_node);
  7629. else
  7630. type = void_type_node;
  7631. break;
  7632. case sfk_conversion:
  7633. if (type)
  7634. error ("return type specified for %<operator %T%>", optype);
  7635. type = optype;
  7636. break;
  7637. default:
  7638. gcc_unreachable ();
  7639. }
  7640. return type;
  7641. }
  7642. /* A variable or data member (whose unqualified name is IDENTIFIER)
  7643. has been declared with the indicated TYPE. If the TYPE is not
  7644. acceptable, issue an error message and return a type to use for
  7645. error-recovery purposes. */
  7646. tree
  7647. check_var_type (tree identifier, tree type)
  7648. {
  7649. if (VOID_TYPE_P (type))
  7650. {
  7651. if (!identifier)
  7652. error ("unnamed variable or field declared void");
  7653. else if (identifier_p (identifier))
  7654. {
  7655. gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
  7656. error ("variable or field %qE declared void", identifier);
  7657. }
  7658. else
  7659. error ("variable or field declared void");
  7660. type = error_mark_node;
  7661. }
  7662. return type;
  7663. }
  7664. /* Given declspecs and a declarator (abstract or otherwise), determine
  7665. the name and type of the object declared and construct a DECL node
  7666. for it.
  7667. DECLSPECS points to the representation of declaration-specifier
  7668. sequence that precedes declarator.
  7669. DECL_CONTEXT says which syntactic context this declaration is in:
  7670. NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
  7671. FUNCDEF for a function definition. Like NORMAL but a few different
  7672. error messages in each case. Return value may be zero meaning
  7673. this definition is too screwy to try to parse.
  7674. MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
  7675. handle member functions (which have FIELD context).
  7676. Return value may be zero meaning this definition is too screwy to
  7677. try to parse.
  7678. PARM for a parameter declaration (either within a function prototype
  7679. or before a function body). Make a PARM_DECL, or return void_type_node.
  7680. TPARM for a template parameter declaration.
  7681. CATCHPARM for a parameter declaration before a catch clause.
  7682. TYPENAME if for a typename (in a cast or sizeof).
  7683. Don't make a DECL node; just return the ..._TYPE node.
  7684. FIELD for a struct or union field; make a FIELD_DECL.
  7685. BITFIELD for a field with specified width.
  7686. INITIALIZED is as for start_decl.
  7687. ATTRLIST is a pointer to the list of attributes, which may be NULL
  7688. if there are none; *ATTRLIST may be modified if attributes from inside
  7689. the declarator should be applied to the declaration.
  7690. When this function is called, scoping variables (such as
  7691. CURRENT_CLASS_TYPE) should reflect the scope in which the
  7692. declaration occurs, not the scope in which the new declaration will
  7693. be placed. For example, on:
  7694. void S::f() { ... }
  7695. when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
  7696. should not be `S'.
  7697. Returns a DECL (if a declarator is present), a TYPE (if there is no
  7698. declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
  7699. error occurs. */
  7700. tree
  7701. grokdeclarator (const cp_declarator *declarator,
  7702. cp_decl_specifier_seq *declspecs,
  7703. enum decl_context decl_context,
  7704. int initialized,
  7705. tree* attrlist)
  7706. {
  7707. tree type = NULL_TREE;
  7708. int longlong = 0;
  7709. int explicit_intN = 0;
  7710. int virtualp, explicitp, friendp, inlinep, staticp;
  7711. int explicit_int = 0;
  7712. int explicit_char = 0;
  7713. int defaulted_int = 0;
  7714. tree typedef_decl = NULL_TREE;
  7715. const char *name = NULL;
  7716. tree typedef_type = NULL_TREE;
  7717. /* True if this declarator is a function definition. */
  7718. bool funcdef_flag = false;
  7719. cp_declarator_kind innermost_code = cdk_error;
  7720. int bitfield = 0;
  7721. #if 0
  7722. /* See the code below that used this. */
  7723. tree decl_attr = NULL_TREE;
  7724. #endif
  7725. /* Keep track of what sort of function is being processed
  7726. so that we can warn about default return values, or explicit
  7727. return values which do not match prescribed defaults. */
  7728. special_function_kind sfk = sfk_none;
  7729. tree dname = NULL_TREE;
  7730. tree ctor_return_type = NULL_TREE;
  7731. enum overload_flags flags = NO_SPECIAL;
  7732. /* cv-qualifiers that apply to the declarator, for a declaration of
  7733. a member function. */
  7734. cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
  7735. /* virt-specifiers that apply to the declarator, for a declaration of
  7736. a member function. */
  7737. cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
  7738. /* ref-qualifier that applies to the declarator, for a declaration of
  7739. a member function. */
  7740. cp_ref_qualifier rqual = REF_QUAL_NONE;
  7741. /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
  7742. int type_quals;
  7743. tree raises = NULL_TREE;
  7744. int template_count = 0;
  7745. tree returned_attrs = NULL_TREE;
  7746. tree parms = NULL_TREE;
  7747. const cp_declarator *id_declarator;
  7748. /* The unqualified name of the declarator; either an
  7749. IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
  7750. tree unqualified_id;
  7751. /* The class type, if any, in which this entity is located,
  7752. or NULL_TREE if none. Note that this value may be different from
  7753. the current class type; for example if an attempt is made to declare
  7754. "A::f" inside "B", this value will be "A". */
  7755. tree ctype = current_class_type;
  7756. /* The NAMESPACE_DECL for the namespace in which this entity is
  7757. located. If an unqualified name is used to declare the entity,
  7758. this value will be NULL_TREE, even if the entity is located at
  7759. namespace scope. */
  7760. tree in_namespace = NULL_TREE;
  7761. cp_storage_class storage_class;
  7762. bool unsigned_p, signed_p, short_p, long_p, thread_p;
  7763. bool type_was_error_mark_node = false;
  7764. bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
  7765. bool template_type_arg = false;
  7766. bool template_parm_flag = false;
  7767. bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
  7768. bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
  7769. bool late_return_type_p = false;
  7770. bool array_parameter_p = false;
  7771. source_location saved_loc = input_location;
  7772. const char *errmsg;
  7773. signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
  7774. unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
  7775. short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
  7776. long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
  7777. longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
  7778. explicit_intN = declspecs->explicit_intN_p;
  7779. thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
  7780. if (decl_context == FUNCDEF)
  7781. funcdef_flag = true, decl_context = NORMAL;
  7782. else if (decl_context == MEMFUNCDEF)
  7783. funcdef_flag = true, decl_context = FIELD;
  7784. else if (decl_context == BITFIELD)
  7785. bitfield = 1, decl_context = FIELD;
  7786. else if (decl_context == TEMPLATE_TYPE_ARG)
  7787. template_type_arg = true, decl_context = TYPENAME;
  7788. else if (decl_context == TPARM)
  7789. template_parm_flag = true, decl_context = PARM;
  7790. if (initialized > 1)
  7791. funcdef_flag = true;
  7792. /* Look inside a declarator for the name being declared
  7793. and get it as a string, for an error message. */
  7794. for (id_declarator = declarator;
  7795. id_declarator;
  7796. id_declarator = id_declarator->declarator)
  7797. {
  7798. if (id_declarator->kind != cdk_id)
  7799. innermost_code = id_declarator->kind;
  7800. switch (id_declarator->kind)
  7801. {
  7802. case cdk_function:
  7803. if (id_declarator->declarator
  7804. && id_declarator->declarator->kind == cdk_id)
  7805. {
  7806. sfk = id_declarator->declarator->u.id.sfk;
  7807. if (sfk == sfk_destructor)
  7808. flags = DTOR_FLAG;
  7809. }
  7810. break;
  7811. case cdk_id:
  7812. {
  7813. tree qualifying_scope = id_declarator->u.id.qualifying_scope;
  7814. tree decl = id_declarator->u.id.unqualified_name;
  7815. if (!decl)
  7816. break;
  7817. if (qualifying_scope)
  7818. {
  7819. if (at_function_scope_p ())
  7820. {
  7821. /* [dcl.meaning]
  7822. A declarator-id shall not be qualified except
  7823. for ...
  7824. None of the cases are permitted in block
  7825. scope. */
  7826. if (qualifying_scope == global_namespace)
  7827. error ("invalid use of qualified-name %<::%D%>",
  7828. decl);
  7829. else if (TYPE_P (qualifying_scope))
  7830. error ("invalid use of qualified-name %<%T::%D%>",
  7831. qualifying_scope, decl);
  7832. else
  7833. error ("invalid use of qualified-name %<%D::%D%>",
  7834. qualifying_scope, decl);
  7835. return error_mark_node;
  7836. }
  7837. else if (TYPE_P (qualifying_scope))
  7838. {
  7839. ctype = qualifying_scope;
  7840. if (!MAYBE_CLASS_TYPE_P (ctype))
  7841. {
  7842. error ("%q#T is not a class or a namespace", ctype);
  7843. ctype = NULL_TREE;
  7844. }
  7845. else if (innermost_code != cdk_function
  7846. && current_class_type
  7847. && !uniquely_derived_from_p (ctype,
  7848. current_class_type))
  7849. {
  7850. error ("invalid use of qualified-name %<%T::%D%>",
  7851. qualifying_scope, decl);
  7852. return error_mark_node;
  7853. }
  7854. }
  7855. else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
  7856. in_namespace = qualifying_scope;
  7857. }
  7858. switch (TREE_CODE (decl))
  7859. {
  7860. case BIT_NOT_EXPR:
  7861. {
  7862. tree type;
  7863. if (innermost_code != cdk_function)
  7864. {
  7865. error ("declaration of %qD as non-function", decl);
  7866. return error_mark_node;
  7867. }
  7868. else if (!qualifying_scope
  7869. && !(current_class_type && at_class_scope_p ()))
  7870. {
  7871. error ("declaration of %qD as non-member", decl);
  7872. return error_mark_node;
  7873. }
  7874. type = TREE_OPERAND (decl, 0);
  7875. if (TYPE_P (type))
  7876. type = constructor_name (type);
  7877. name = identifier_to_locale (IDENTIFIER_POINTER (type));
  7878. dname = decl;
  7879. }
  7880. break;
  7881. case TEMPLATE_ID_EXPR:
  7882. {
  7883. tree fns = TREE_OPERAND (decl, 0);
  7884. dname = fns;
  7885. if (!identifier_p (dname))
  7886. {
  7887. if (variable_template_p (dname))
  7888. dname = DECL_NAME (dname);
  7889. else
  7890. {
  7891. gcc_assert (is_overloaded_fn (dname));
  7892. dname = DECL_NAME (get_first_fn (dname));
  7893. }
  7894. }
  7895. }
  7896. /* Fall through. */
  7897. case IDENTIFIER_NODE:
  7898. if (identifier_p (decl))
  7899. dname = decl;
  7900. if (C_IS_RESERVED_WORD (dname))
  7901. {
  7902. error ("declarator-id missing; using reserved word %qD",
  7903. dname);
  7904. name = identifier_to_locale (IDENTIFIER_POINTER (dname));
  7905. }
  7906. else if (!IDENTIFIER_TYPENAME_P (dname))
  7907. name = identifier_to_locale (IDENTIFIER_POINTER (dname));
  7908. else
  7909. {
  7910. gcc_assert (flags == NO_SPECIAL);
  7911. flags = TYPENAME_FLAG;
  7912. ctor_return_type = TREE_TYPE (dname);
  7913. sfk = sfk_conversion;
  7914. if (is_typename_at_global_scope (dname))
  7915. name = identifier_to_locale (IDENTIFIER_POINTER (dname));
  7916. else
  7917. name = "<invalid operator>";
  7918. }
  7919. break;
  7920. default:
  7921. gcc_unreachable ();
  7922. }
  7923. break;
  7924. }
  7925. case cdk_array:
  7926. case cdk_pointer:
  7927. case cdk_reference:
  7928. case cdk_ptrmem:
  7929. break;
  7930. case cdk_error:
  7931. return error_mark_node;
  7932. default:
  7933. gcc_unreachable ();
  7934. }
  7935. if (id_declarator->kind == cdk_id)
  7936. break;
  7937. }
  7938. /* [dcl.fct.edf]
  7939. The declarator in a function-definition shall have the form
  7940. D1 ( parameter-declaration-clause) ... */
  7941. if (funcdef_flag && innermost_code != cdk_function)
  7942. {
  7943. error ("function definition does not declare parameters");
  7944. return error_mark_node;
  7945. }
  7946. if (flags == TYPENAME_FLAG
  7947. && innermost_code != cdk_function
  7948. && ! (ctype && !declspecs->any_specifiers_p))
  7949. {
  7950. error ("declaration of %qD as non-function", dname);
  7951. return error_mark_node;
  7952. }
  7953. if (dname
  7954. && identifier_p (dname)
  7955. && UDLIT_OPER_P (dname)
  7956. && innermost_code != cdk_function)
  7957. {
  7958. error ("declaration of %qD as non-function", dname);
  7959. return error_mark_node;
  7960. }
  7961. if (dname && IDENTIFIER_OPNAME_P (dname))
  7962. {
  7963. if (typedef_p)
  7964. {
  7965. error ("declaration of %qD as %<typedef%>", dname);
  7966. return error_mark_node;
  7967. }
  7968. else if (decl_context == PARM || decl_context == CATCHPARM)
  7969. {
  7970. error ("declaration of %qD as parameter", dname);
  7971. return error_mark_node;
  7972. }
  7973. }
  7974. /* Anything declared one level down from the top level
  7975. must be one of the parameters of a function
  7976. (because the body is at least two levels down). */
  7977. /* This heuristic cannot be applied to C++ nodes! Fixed, however,
  7978. by not allowing C++ class definitions to specify their parameters
  7979. with xdecls (must be spec.d in the parmlist).
  7980. Since we now wait to push a class scope until we are sure that
  7981. we are in a legitimate method context, we must set oldcname
  7982. explicitly (since current_class_name is not yet alive).
  7983. We also want to avoid calling this a PARM if it is in a namespace. */
  7984. if (decl_context == NORMAL && !toplevel_bindings_p ())
  7985. {
  7986. cp_binding_level *b = current_binding_level;
  7987. current_binding_level = b->level_chain;
  7988. if (current_binding_level != 0 && toplevel_bindings_p ())
  7989. decl_context = PARM;
  7990. current_binding_level = b;
  7991. }
  7992. if (name == NULL)
  7993. name = decl_context == PARM ? "parameter" : "type name";
  7994. if (constexpr_p && typedef_p)
  7995. {
  7996. error ("%<constexpr%> cannot appear in a typedef declaration");
  7997. return error_mark_node;
  7998. }
  7999. /* If there were multiple types specified in the decl-specifier-seq,
  8000. issue an error message. */
  8001. if (declspecs->multiple_types_p)
  8002. {
  8003. error ("two or more data types in declaration of %qs", name);
  8004. return error_mark_node;
  8005. }
  8006. if (declspecs->conflicting_specifiers_p)
  8007. {
  8008. error ("conflicting specifiers in declaration of %qs", name);
  8009. return error_mark_node;
  8010. }
  8011. /* Extract the basic type from the decl-specifier-seq. */
  8012. type = declspecs->type;
  8013. if (type == error_mark_node)
  8014. {
  8015. type = NULL_TREE;
  8016. type_was_error_mark_node = true;
  8017. }
  8018. /* If the entire declaration is itself tagged as deprecated then
  8019. suppress reports of deprecated items. */
  8020. if (type && TREE_DEPRECATED (type)
  8021. && deprecated_state != DEPRECATED_SUPPRESS)
  8022. warn_deprecated_use (type, NULL_TREE);
  8023. if (type && TREE_CODE (type) == TYPE_DECL)
  8024. {
  8025. typedef_decl = type;
  8026. type = TREE_TYPE (typedef_decl);
  8027. if (TREE_DEPRECATED (type)
  8028. && DECL_ARTIFICIAL (typedef_decl)
  8029. && deprecated_state != DEPRECATED_SUPPRESS)
  8030. warn_deprecated_use (type, NULL_TREE);
  8031. }
  8032. /* No type at all: default to `int', and set DEFAULTED_INT
  8033. because it was not a user-defined typedef. */
  8034. if (type == NULL_TREE)
  8035. {
  8036. if (signed_p || unsigned_p || long_p || short_p)
  8037. {
  8038. /* These imply 'int'. */
  8039. type = integer_type_node;
  8040. defaulted_int = 1;
  8041. }
  8042. /* If we just have "complex", it is equivalent to "complex double". */
  8043. else if (!longlong && !explicit_intN
  8044. && decl_spec_seq_has_spec_p (declspecs, ds_complex))
  8045. {
  8046. type = double_type_node;
  8047. pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
  8048. "ISO C++ does not support plain %<complex%> meaning "
  8049. "%<double complex%>");
  8050. }
  8051. }
  8052. /* Gather flags. */
  8053. explicit_int = declspecs->explicit_int_p;
  8054. explicit_char = declspecs->explicit_char_p;
  8055. #if 0
  8056. /* See the code below that used this. */
  8057. if (typedef_decl)
  8058. decl_attr = DECL_ATTRIBUTES (typedef_decl);
  8059. #endif
  8060. typedef_type = type;
  8061. if (sfk != sfk_conversion)
  8062. ctor_return_type = ctype;
  8063. if (sfk != sfk_none)
  8064. type = check_special_function_return_type (sfk, type,
  8065. ctor_return_type);
  8066. else if (type == NULL_TREE)
  8067. {
  8068. int is_main;
  8069. explicit_int = -1;
  8070. /* We handle `main' specially here, because 'main () { }' is so
  8071. common. With no options, it is allowed. With -Wreturn-type,
  8072. it is a warning. It is only an error with -pedantic-errors. */
  8073. is_main = (funcdef_flag
  8074. && dname && identifier_p (dname)
  8075. && MAIN_NAME_P (dname)
  8076. && ctype == NULL_TREE
  8077. && in_namespace == NULL_TREE
  8078. && current_namespace == global_namespace);
  8079. if (type_was_error_mark_node)
  8080. /* We've already issued an error, don't complain more. */;
  8081. else if (in_system_header_at (input_location) || flag_ms_extensions)
  8082. /* Allow it, sigh. */;
  8083. else if (! is_main)
  8084. permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
  8085. else if (pedantic)
  8086. pedwarn (input_location, OPT_Wpedantic,
  8087. "ISO C++ forbids declaration of %qs with no type", name);
  8088. else
  8089. warning (OPT_Wreturn_type,
  8090. "ISO C++ forbids declaration of %qs with no type", name);
  8091. type = integer_type_node;
  8092. }
  8093. ctype = NULL_TREE;
  8094. if (explicit_intN)
  8095. {
  8096. if (! int_n_enabled_p[declspecs->int_n_idx])
  8097. {
  8098. error ("%<__int%d%> is not supported by this target",
  8099. int_n_data[declspecs->int_n_idx].bitsize);
  8100. explicit_intN = false;
  8101. }
  8102. else if (pedantic && ! in_system_header_at (input_location))
  8103. pedwarn (input_location, OPT_Wpedantic,
  8104. "ISO C++ does not support %<__int%d%> for %qs",
  8105. int_n_data[declspecs->int_n_idx].bitsize, name);
  8106. }
  8107. /* Now process the modifiers that were specified
  8108. and check for invalid combinations. */
  8109. /* Long double is a special combination. */
  8110. if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
  8111. {
  8112. long_p = false;
  8113. type = cp_build_qualified_type (long_double_type_node,
  8114. cp_type_quals (type));
  8115. }
  8116. /* Check all other uses of type modifiers. */
  8117. if (unsigned_p || signed_p || long_p || short_p)
  8118. {
  8119. int ok = 0;
  8120. if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
  8121. error ("%<signed%> or %<unsigned%> invalid for %qs", name);
  8122. else if (signed_p && unsigned_p)
  8123. error ("%<signed%> and %<unsigned%> specified together for %qs", name);
  8124. else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
  8125. error ("%<long long%> invalid for %qs", name);
  8126. else if (long_p && TREE_CODE (type) == REAL_TYPE)
  8127. error ("%<long%> invalid for %qs", name);
  8128. else if (short_p && TREE_CODE (type) == REAL_TYPE)
  8129. error ("%<short%> invalid for %qs", name);
  8130. else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
  8131. error ("%<long%> or %<short%> invalid for %qs", name);
  8132. else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
  8133. error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
  8134. else if ((long_p || short_p) && explicit_char)
  8135. error ("%<long%> or %<short%> specified with char for %qs", name);
  8136. else if (long_p && short_p)
  8137. error ("%<long%> and %<short%> specified together for %qs", name);
  8138. else if (type == char16_type_node || type == char32_type_node)
  8139. {
  8140. if (signed_p || unsigned_p)
  8141. error ("%<signed%> or %<unsigned%> invalid for %qs", name);
  8142. else if (short_p || long_p)
  8143. error ("%<short%> or %<long%> invalid for %qs", name);
  8144. }
  8145. else
  8146. {
  8147. ok = 1;
  8148. if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
  8149. {
  8150. pedwarn (input_location, OPT_Wpedantic,
  8151. "long, short, signed or unsigned used invalidly for %qs",
  8152. name);
  8153. if (flag_pedantic_errors)
  8154. ok = 0;
  8155. }
  8156. }
  8157. /* Discard the type modifiers if they are invalid. */
  8158. if (! ok)
  8159. {
  8160. unsigned_p = false;
  8161. signed_p = false;
  8162. long_p = false;
  8163. short_p = false;
  8164. longlong = 0;
  8165. }
  8166. }
  8167. /* Decide whether an integer type is signed or not.
  8168. Optionally treat bitfields as signed by default. */
  8169. if (unsigned_p
  8170. /* [class.bit]
  8171. It is implementation-defined whether a plain (neither
  8172. explicitly signed or unsigned) char, short, int, or long
  8173. bit-field is signed or unsigned.
  8174. Naturally, we extend this to long long as well. Note that
  8175. this does not include wchar_t. */
  8176. || (bitfield && !flag_signed_bitfields
  8177. && !signed_p
  8178. /* A typedef for plain `int' without `signed' can be
  8179. controlled just like plain `int', but a typedef for
  8180. `signed int' cannot be so controlled. */
  8181. && !(typedef_decl
  8182. && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
  8183. && TREE_CODE (type) == INTEGER_TYPE
  8184. && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
  8185. {
  8186. if (explicit_intN)
  8187. type = int_n_trees[declspecs->int_n_idx].unsigned_type;
  8188. else if (longlong)
  8189. type = long_long_unsigned_type_node;
  8190. else if (long_p)
  8191. type = long_unsigned_type_node;
  8192. else if (short_p)
  8193. type = short_unsigned_type_node;
  8194. else if (type == char_type_node)
  8195. type = unsigned_char_type_node;
  8196. else if (typedef_decl)
  8197. type = unsigned_type_for (type);
  8198. else
  8199. type = unsigned_type_node;
  8200. }
  8201. else if (signed_p && type == char_type_node)
  8202. type = signed_char_type_node;
  8203. else if (explicit_intN)
  8204. type = int_n_trees[declspecs->int_n_idx].signed_type;
  8205. else if (longlong)
  8206. type = long_long_integer_type_node;
  8207. else if (long_p)
  8208. type = long_integer_type_node;
  8209. else if (short_p)
  8210. type = short_integer_type_node;
  8211. if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
  8212. {
  8213. if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
  8214. error ("complex invalid for %qs", name);
  8215. /* If a modifier is specified, the resulting complex is the complex
  8216. form of TYPE. E.g, "complex short" is "complex short int". */
  8217. else if (type == integer_type_node)
  8218. type = complex_integer_type_node;
  8219. else if (type == float_type_node)
  8220. type = complex_float_type_node;
  8221. else if (type == double_type_node)
  8222. type = complex_double_type_node;
  8223. else if (type == long_double_type_node)
  8224. type = complex_long_double_type_node;
  8225. else
  8226. type = build_complex_type (type);
  8227. }
  8228. type_quals = TYPE_UNQUALIFIED;
  8229. if (decl_spec_seq_has_spec_p (declspecs, ds_const))
  8230. type_quals |= TYPE_QUAL_CONST;
  8231. if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
  8232. type_quals |= TYPE_QUAL_VOLATILE;
  8233. if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
  8234. type_quals |= TYPE_QUAL_RESTRICT;
  8235. if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
  8236. error ("qualifiers are not allowed on declaration of %<operator %T%>",
  8237. ctor_return_type);
  8238. /* If we're using the injected-class-name to form a compound type or a
  8239. declaration, replace it with the underlying class so we don't get
  8240. redundant typedefs in the debug output. But if we are returning the
  8241. type unchanged, leave it alone so that it's available to
  8242. maybe_get_template_decl_from_type_decl. */
  8243. if (CLASS_TYPE_P (type)
  8244. && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
  8245. && type == TREE_TYPE (TYPE_NAME (type))
  8246. && (declarator || type_quals))
  8247. type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
  8248. type_quals |= cp_type_quals (type);
  8249. type = cp_build_qualified_type_real
  8250. (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
  8251. || declspecs->decltype_p)
  8252. ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
  8253. /* We might have ignored or rejected some of the qualifiers. */
  8254. type_quals = cp_type_quals (type);
  8255. staticp = 0;
  8256. inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
  8257. virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
  8258. explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
  8259. storage_class = declspecs->storage_class;
  8260. if (storage_class == sc_static)
  8261. staticp = 1 + (decl_context == FIELD);
  8262. if (virtualp && staticp == 2)
  8263. {
  8264. error ("member %qD cannot be declared both virtual and static", dname);
  8265. storage_class = sc_none;
  8266. staticp = 0;
  8267. }
  8268. friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
  8269. /* Issue errors about use of storage classes for parameters. */
  8270. if (decl_context == PARM)
  8271. {
  8272. if (typedef_p)
  8273. {
  8274. error ("typedef declaration invalid in parameter declaration");
  8275. return error_mark_node;
  8276. }
  8277. else if (template_parm_flag && storage_class != sc_none)
  8278. {
  8279. error ("storage class specified for template parameter %qs", name);
  8280. return error_mark_node;
  8281. }
  8282. else if (storage_class == sc_static
  8283. || storage_class == sc_extern
  8284. || thread_p)
  8285. error ("storage class specifiers invalid in parameter declarations");
  8286. /* Function parameters cannot be constexpr. If we saw one, moan
  8287. and pretend it wasn't there. */
  8288. if (constexpr_p)
  8289. {
  8290. error ("a parameter cannot be declared %<constexpr%>");
  8291. constexpr_p = 0;
  8292. }
  8293. }
  8294. /* Give error if `virtual' is used outside of class declaration. */
  8295. if (virtualp
  8296. && (current_class_name == NULL_TREE || decl_context != FIELD))
  8297. {
  8298. error ("%<virtual%> outside class declaration");
  8299. virtualp = 0;
  8300. }
  8301. /* Static anonymous unions are dealt with here. */
  8302. if (staticp && decl_context == TYPENAME
  8303. && declspecs->type
  8304. && ANON_AGGR_TYPE_P (declspecs->type))
  8305. decl_context = FIELD;
  8306. /* Warn about storage classes that are invalid for certain
  8307. kinds of declarations (parameters, typenames, etc.). */
  8308. if (thread_p
  8309. && ((storage_class
  8310. && storage_class != sc_extern
  8311. && storage_class != sc_static)
  8312. || typedef_p))
  8313. {
  8314. error ("multiple storage classes in declaration of %qs", name);
  8315. thread_p = false;
  8316. }
  8317. if (decl_context != NORMAL
  8318. && ((storage_class != sc_none
  8319. && storage_class != sc_mutable)
  8320. || thread_p))
  8321. {
  8322. if ((decl_context == PARM || decl_context == CATCHPARM)
  8323. && (storage_class == sc_register
  8324. || storage_class == sc_auto))
  8325. ;
  8326. else if (typedef_p)
  8327. ;
  8328. else if (decl_context == FIELD
  8329. /* C++ allows static class elements. */
  8330. && storage_class == sc_static)
  8331. /* C++ also allows inlines and signed and unsigned elements,
  8332. but in those cases we don't come in here. */
  8333. ;
  8334. else
  8335. {
  8336. if (decl_context == FIELD)
  8337. error ("storage class specified for %qs", name);
  8338. else
  8339. {
  8340. if (decl_context == PARM || decl_context == CATCHPARM)
  8341. error ("storage class specified for parameter %qs", name);
  8342. else
  8343. error ("storage class specified for typename");
  8344. }
  8345. if (storage_class == sc_register
  8346. || storage_class == sc_auto
  8347. || storage_class == sc_extern
  8348. || thread_p)
  8349. storage_class = sc_none;
  8350. }
  8351. }
  8352. else if (storage_class == sc_extern && funcdef_flag
  8353. && ! toplevel_bindings_p ())
  8354. error ("nested function %qs declared %<extern%>", name);
  8355. else if (toplevel_bindings_p ())
  8356. {
  8357. if (storage_class == sc_auto)
  8358. error ("top-level declaration of %qs specifies %<auto%>", name);
  8359. }
  8360. else if (thread_p
  8361. && storage_class != sc_extern
  8362. && storage_class != sc_static)
  8363. {
  8364. if (declspecs->gnu_thread_keyword_p)
  8365. pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
  8366. "declared %<__thread%>", name);
  8367. /* When thread_local is applied to a variable of block scope the
  8368. storage-class-specifier static is implied if it does not appear
  8369. explicitly. */
  8370. storage_class = declspecs->storage_class = sc_static;
  8371. staticp = 1;
  8372. }
  8373. if (storage_class && friendp)
  8374. {
  8375. error ("storage class specifiers invalid in friend function declarations");
  8376. storage_class = sc_none;
  8377. staticp = 0;
  8378. }
  8379. if (!id_declarator)
  8380. unqualified_id = NULL_TREE;
  8381. else
  8382. {
  8383. unqualified_id = id_declarator->u.id.unqualified_name;
  8384. switch (TREE_CODE (unqualified_id))
  8385. {
  8386. case BIT_NOT_EXPR:
  8387. unqualified_id = TREE_OPERAND (unqualified_id, 0);
  8388. if (TYPE_P (unqualified_id))
  8389. unqualified_id = constructor_name (unqualified_id);
  8390. break;
  8391. case IDENTIFIER_NODE:
  8392. case TEMPLATE_ID_EXPR:
  8393. break;
  8394. default:
  8395. gcc_unreachable ();
  8396. }
  8397. }
  8398. if (declspecs->std_attributes)
  8399. {
  8400. /* Apply the c++11 attributes to the type preceding them. */
  8401. input_location = declspecs->locations[ds_std_attribute];
  8402. decl_attributes (&type, declspecs->std_attributes, 0);
  8403. input_location = saved_loc;
  8404. }
  8405. /* Determine the type of the entity declared by recurring on the
  8406. declarator. */
  8407. for (; declarator; declarator = declarator->declarator)
  8408. {
  8409. const cp_declarator *inner_declarator;
  8410. tree attrs;
  8411. if (type == error_mark_node)
  8412. return error_mark_node;
  8413. attrs = declarator->attributes;
  8414. if (attrs)
  8415. {
  8416. int attr_flags;
  8417. attr_flags = 0;
  8418. if (declarator == NULL || declarator->kind == cdk_id)
  8419. attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
  8420. if (declarator->kind == cdk_function)
  8421. attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
  8422. if (declarator->kind == cdk_array)
  8423. attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
  8424. returned_attrs = decl_attributes (&type,
  8425. chainon (returned_attrs, attrs),
  8426. attr_flags);
  8427. }
  8428. if (declarator->kind == cdk_id)
  8429. break;
  8430. inner_declarator = declarator->declarator;
  8431. switch (declarator->kind)
  8432. {
  8433. case cdk_array:
  8434. type = create_array_type_for_decl (dname, type,
  8435. declarator->u.array.bounds);
  8436. if (declarator->std_attributes)
  8437. /* [dcl.array]/1:
  8438. The optional attribute-specifier-seq appertains to the
  8439. array. */
  8440. returned_attrs = chainon (returned_attrs,
  8441. declarator->std_attributes);
  8442. break;
  8443. case cdk_function:
  8444. {
  8445. tree arg_types;
  8446. int funcdecl_p;
  8447. /* Declaring a function type.
  8448. Make sure we have a valid type for the function to return. */
  8449. if (type_quals != TYPE_UNQUALIFIED)
  8450. {
  8451. if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
  8452. warning (OPT_Wignored_qualifiers,
  8453. "type qualifiers ignored on function return type");
  8454. /* We now know that the TYPE_QUALS don't apply to the
  8455. decl, but to its return type. */
  8456. type_quals = TYPE_UNQUALIFIED;
  8457. }
  8458. errmsg = targetm.invalid_return_type (type);
  8459. if (errmsg)
  8460. {
  8461. error (errmsg);
  8462. type = integer_type_node;
  8463. }
  8464. /* Error about some types functions can't return. */
  8465. if (TREE_CODE (type) == FUNCTION_TYPE)
  8466. {
  8467. error ("%qs declared as function returning a function", name);
  8468. return error_mark_node;
  8469. }
  8470. if (TREE_CODE (type) == ARRAY_TYPE)
  8471. {
  8472. error ("%qs declared as function returning an array", name);
  8473. return error_mark_node;
  8474. }
  8475. input_location = declspecs->locations[ds_type_spec];
  8476. abstract_virtuals_error (ACU_RETURN, type);
  8477. input_location = saved_loc;
  8478. /* Pick up type qualifiers which should be applied to `this'. */
  8479. memfn_quals = declarator->u.function.qualifiers;
  8480. /* Pick up virt-specifiers. */
  8481. virt_specifiers = declarator->u.function.virt_specifiers;
  8482. /* And ref-qualifier, too */
  8483. rqual = declarator->u.function.ref_qualifier;
  8484. /* Pick up the exception specifications. */
  8485. raises = declarator->u.function.exception_specification;
  8486. /* If the exception-specification is ill-formed, let's pretend
  8487. there wasn't one. */
  8488. if (raises == error_mark_node)
  8489. raises = NULL_TREE;
  8490. /* Say it's a definition only for the CALL_EXPR
  8491. closest to the identifier. */
  8492. funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
  8493. /* Handle a late-specified return type. */
  8494. if (funcdecl_p)
  8495. {
  8496. if (type_uses_auto (type))
  8497. {
  8498. if (!declarator->u.function.late_return_type)
  8499. {
  8500. if (current_class_type
  8501. && LAMBDA_TYPE_P (current_class_type))
  8502. /* OK for C++11 lambdas. */;
  8503. else if (cxx_dialect < cxx14)
  8504. {
  8505. error ("%qs function uses "
  8506. "%<auto%> type specifier without trailing "
  8507. "return type", name);
  8508. inform (input_location, "deduced return type "
  8509. "only available with -std=c++14 or "
  8510. "-std=gnu++14");
  8511. }
  8512. else if (virtualp)
  8513. {
  8514. error ("virtual function cannot "
  8515. "have deduced return type");
  8516. virtualp = false;
  8517. }
  8518. }
  8519. else if (!is_auto (type))
  8520. {
  8521. error ("%qs function with trailing return type has"
  8522. " %qT as its type rather than plain %<auto%>",
  8523. name, type);
  8524. return error_mark_node;
  8525. }
  8526. }
  8527. else if (declarator->u.function.late_return_type)
  8528. {
  8529. if (cxx_dialect < cxx11)
  8530. /* Not using maybe_warn_cpp0x because this should
  8531. always be an error. */
  8532. error ("trailing return type only available with "
  8533. "-std=c++11 or -std=gnu++11");
  8534. else
  8535. error ("%qs function with trailing return type not "
  8536. "declared with %<auto%> type specifier", name);
  8537. return error_mark_node;
  8538. }
  8539. }
  8540. type = splice_late_return_type
  8541. (type, declarator->u.function.late_return_type);
  8542. if (type == error_mark_node)
  8543. return error_mark_node;
  8544. if (declarator->u.function.late_return_type)
  8545. late_return_type_p = true;
  8546. if (ctype == NULL_TREE
  8547. && decl_context == FIELD
  8548. && funcdecl_p
  8549. && friendp == 0)
  8550. ctype = current_class_type;
  8551. if (ctype && (sfk == sfk_constructor
  8552. || sfk == sfk_destructor))
  8553. {
  8554. /* We are within a class's scope. If our declarator name
  8555. is the same as the class name, and we are defining
  8556. a function, then it is a constructor/destructor, and
  8557. therefore returns a void type. */
  8558. /* ISO C++ 12.4/2. A destructor may not be declared
  8559. const or volatile. A destructor may not be static.
  8560. A destructor may not be declared with ref-qualifier.
  8561. ISO C++ 12.1. A constructor may not be declared
  8562. const or volatile. A constructor may not be
  8563. virtual. A constructor may not be static.
  8564. A constructor may not be declared with ref-qualifier. */
  8565. if (staticp == 2)
  8566. error ((flags == DTOR_FLAG)
  8567. ? G_("destructor cannot be static member function")
  8568. : G_("constructor cannot be static member function"));
  8569. if (memfn_quals)
  8570. {
  8571. error ((flags == DTOR_FLAG)
  8572. ? G_("destructors may not be cv-qualified")
  8573. : G_("constructors may not be cv-qualified"));
  8574. memfn_quals = TYPE_UNQUALIFIED;
  8575. }
  8576. if (rqual)
  8577. {
  8578. maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
  8579. error ((flags == DTOR_FLAG)
  8580. ? "destructors may not be ref-qualified"
  8581. : "constructors may not be ref-qualified");
  8582. rqual = REF_QUAL_NONE;
  8583. }
  8584. if (decl_context == FIELD
  8585. && !member_function_or_else (ctype,
  8586. current_class_type,
  8587. flags))
  8588. return error_mark_node;
  8589. if (flags != DTOR_FLAG)
  8590. {
  8591. /* It's a constructor. */
  8592. if (explicitp == 1)
  8593. explicitp = 2;
  8594. if (virtualp)
  8595. {
  8596. permerror (input_location, "constructors cannot be declared virtual");
  8597. virtualp = 0;
  8598. }
  8599. if (decl_context == FIELD
  8600. && sfk != sfk_constructor)
  8601. return error_mark_node;
  8602. }
  8603. if (decl_context == FIELD)
  8604. staticp = 0;
  8605. }
  8606. else if (friendp)
  8607. {
  8608. if (virtualp)
  8609. {
  8610. /* Cannot be both friend and virtual. */
  8611. error ("virtual functions cannot be friends");
  8612. friendp = 0;
  8613. }
  8614. if (decl_context == NORMAL)
  8615. error ("friend declaration not in class definition");
  8616. if (current_function_decl && funcdef_flag)
  8617. error ("can%'t define friend function %qs in a local "
  8618. "class definition",
  8619. name);
  8620. }
  8621. else if (ctype && sfk == sfk_conversion)
  8622. {
  8623. if (explicitp == 1)
  8624. {
  8625. maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
  8626. explicitp = 2;
  8627. }
  8628. }
  8629. arg_types = grokparms (declarator->u.function.parameters,
  8630. &parms);
  8631. if (inner_declarator
  8632. && inner_declarator->kind == cdk_id
  8633. && inner_declarator->u.id.sfk == sfk_destructor
  8634. && arg_types != void_list_node)
  8635. {
  8636. error ("destructors may not have parameters");
  8637. arg_types = void_list_node;
  8638. parms = NULL_TREE;
  8639. }
  8640. type = build_function_type (type, arg_types);
  8641. if (declarator->std_attributes)
  8642. /* [dcl.fct]/2:
  8643. The optional attribute-specifier-seq appertains to
  8644. the function type. */
  8645. decl_attributes (&type, declarator->std_attributes,
  8646. 0);
  8647. }
  8648. break;
  8649. case cdk_pointer:
  8650. case cdk_reference:
  8651. case cdk_ptrmem:
  8652. /* Filter out pointers-to-references and references-to-references.
  8653. We can get these if a TYPE_DECL is used. */
  8654. if (TREE_CODE (type) == REFERENCE_TYPE)
  8655. {
  8656. if (declarator->kind != cdk_reference)
  8657. {
  8658. error ("cannot declare pointer to %q#T", type);
  8659. type = TREE_TYPE (type);
  8660. }
  8661. /* In C++0x, we allow reference to reference declarations
  8662. that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
  8663. and template type arguments [14.3.1/4 temp.arg.type]. The
  8664. check for direct reference to reference declarations, which
  8665. are still forbidden, occurs below. Reasoning behind the change
  8666. can be found in DR106, DR540, and the rvalue reference
  8667. proposals. */
  8668. else if (cxx_dialect == cxx98)
  8669. {
  8670. error ("cannot declare reference to %q#T", type);
  8671. type = TREE_TYPE (type);
  8672. }
  8673. }
  8674. else if (VOID_TYPE_P (type))
  8675. {
  8676. if (declarator->kind == cdk_reference)
  8677. error ("cannot declare reference to %q#T", type);
  8678. else if (declarator->kind == cdk_ptrmem)
  8679. error ("cannot declare pointer to %q#T member", type);
  8680. }
  8681. /* We now know that the TYPE_QUALS don't apply to the decl,
  8682. but to the target of the pointer. */
  8683. type_quals = TYPE_UNQUALIFIED;
  8684. /* This code used to handle METHOD_TYPE, but I don't think it's
  8685. possible to get it here anymore. */
  8686. gcc_assert (TREE_CODE (type) != METHOD_TYPE);
  8687. if (declarator->kind == cdk_ptrmem
  8688. && TREE_CODE (type) == FUNCTION_TYPE)
  8689. {
  8690. memfn_quals |= type_memfn_quals (type);
  8691. type = build_memfn_type (type,
  8692. declarator->u.pointer.class_type,
  8693. memfn_quals,
  8694. rqual);
  8695. if (type == error_mark_node)
  8696. return error_mark_node;
  8697. rqual = REF_QUAL_NONE;
  8698. memfn_quals = TYPE_UNQUALIFIED;
  8699. }
  8700. if (TREE_CODE (type) == FUNCTION_TYPE
  8701. && (type_memfn_quals (type) != TYPE_UNQUALIFIED
  8702. || type_memfn_rqual (type) != REF_QUAL_NONE))
  8703. error (declarator->kind == cdk_reference
  8704. ? G_("cannot declare reference to qualified function type %qT")
  8705. : G_("cannot declare pointer to qualified function type %qT"),
  8706. type);
  8707. /* When the pointed-to type involves components of variable size,
  8708. care must be taken to ensure that the size evaluation code is
  8709. emitted early enough to dominate all the possible later uses
  8710. and late enough for the variables on which it depends to have
  8711. been assigned.
  8712. This is expected to happen automatically when the pointed-to
  8713. type has a name/declaration of it's own, but special attention
  8714. is required if the type is anonymous.
  8715. We handle the NORMAL and FIELD contexts here by inserting a
  8716. dummy statement that just evaluates the size at a safe point
  8717. and ensures it is not deferred until e.g. within a deeper
  8718. conditional context (c++/43555).
  8719. We expect nothing to be needed here for PARM or TYPENAME.
  8720. Evaluating the size at this point for TYPENAME would
  8721. actually be incorrect, as we might be in the middle of an
  8722. expression with side effects on the pointed-to type size
  8723. "arguments" prior to the pointer declaration point and the
  8724. size evaluation could end up prior to the side effects. */
  8725. if (!TYPE_NAME (type)
  8726. && (decl_context == NORMAL || decl_context == FIELD)
  8727. && at_function_scope_p ()
  8728. && variably_modified_type_p (type, NULL_TREE))
  8729. /* Force evaluation of the SAVE_EXPR. */
  8730. finish_expr_stmt (TYPE_SIZE (type));
  8731. if (declarator->kind == cdk_reference)
  8732. {
  8733. /* In C++0x, the type we are creating a reference to might be
  8734. a typedef which is itself a reference type. In that case,
  8735. we follow the reference collapsing rules in
  8736. [7.1.3/8 dcl.typedef] to create the final reference type:
  8737. "If a typedef TD names a type that is a reference to a type
  8738. T, an attempt to create the type 'lvalue reference to cv TD'
  8739. creates the type 'lvalue reference to T,' while an attempt
  8740. to create the type "rvalue reference to cv TD' creates the
  8741. type TD."
  8742. */
  8743. if (VOID_TYPE_P (type))
  8744. /* We already gave an error. */;
  8745. else if (TREE_CODE (type) == REFERENCE_TYPE)
  8746. {
  8747. if (declarator->u.reference.rvalue_ref)
  8748. /* Leave type alone. */;
  8749. else
  8750. type = cp_build_reference_type (TREE_TYPE (type), false);
  8751. }
  8752. else
  8753. type = cp_build_reference_type
  8754. (type, declarator->u.reference.rvalue_ref);
  8755. /* In C++0x, we need this check for direct reference to
  8756. reference declarations, which are forbidden by
  8757. [8.3.2/5 dcl.ref]. Reference to reference declarations
  8758. are only allowed indirectly through typedefs and template
  8759. type arguments. Example:
  8760. void foo(int & &); // invalid ref-to-ref decl
  8761. typedef int & int_ref;
  8762. void foo(int_ref &); // valid ref-to-ref decl
  8763. */
  8764. if (inner_declarator && inner_declarator->kind == cdk_reference)
  8765. error ("cannot declare reference to %q#T, which is not "
  8766. "a typedef or a template type argument", type);
  8767. }
  8768. else if (TREE_CODE (type) == METHOD_TYPE)
  8769. type = build_ptrmemfunc_type (build_pointer_type (type));
  8770. else if (declarator->kind == cdk_ptrmem)
  8771. {
  8772. gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
  8773. != NAMESPACE_DECL);
  8774. if (declarator->u.pointer.class_type == error_mark_node)
  8775. /* We will already have complained. */
  8776. type = error_mark_node;
  8777. else
  8778. type = build_ptrmem_type (declarator->u.pointer.class_type,
  8779. type);
  8780. }
  8781. else
  8782. type = build_pointer_type (type);
  8783. /* Process a list of type modifier keywords (such as
  8784. const or volatile) that were given inside the `*' or `&'. */
  8785. if (declarator->u.pointer.qualifiers)
  8786. {
  8787. type
  8788. = cp_build_qualified_type (type,
  8789. declarator->u.pointer.qualifiers);
  8790. type_quals = cp_type_quals (type);
  8791. }
  8792. /* Apply C++11 attributes to the pointer, and not to the
  8793. type pointed to. This is unlike what is done for GNU
  8794. attributes above. It is to comply with [dcl.ptr]/1:
  8795. [the optional attribute-specifier-seq (7.6.1) appertains
  8796. to the pointer and not to the object pointed to]. */
  8797. if (declarator->std_attributes)
  8798. decl_attributes (&type, declarator->std_attributes,
  8799. 0);
  8800. ctype = NULL_TREE;
  8801. break;
  8802. case cdk_error:
  8803. break;
  8804. default:
  8805. gcc_unreachable ();
  8806. }
  8807. }
  8808. /* A `constexpr' specifier used in an object declaration declares
  8809. the object as `const'. */
  8810. if (constexpr_p && innermost_code != cdk_function)
  8811. {
  8812. /* DR1688 says that a `constexpr' specifier in combination with
  8813. `volatile' is valid. */
  8814. if (TREE_CODE (type) != REFERENCE_TYPE)
  8815. {
  8816. type_quals |= TYPE_QUAL_CONST;
  8817. type = cp_build_qualified_type (type, type_quals);
  8818. }
  8819. }
  8820. if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
  8821. && TREE_CODE (type) != FUNCTION_TYPE
  8822. && TREE_CODE (type) != METHOD_TYPE
  8823. && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
  8824. {
  8825. error ("template-id %qD used as a declarator",
  8826. unqualified_id);
  8827. unqualified_id = dname;
  8828. }
  8829. /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
  8830. qualified with a class-name, turn it into a METHOD_TYPE, unless
  8831. we know that the function is static. We take advantage of this
  8832. opportunity to do other processing that pertains to entities
  8833. explicitly declared to be class members. Note that if DECLARATOR
  8834. is non-NULL, we know it is a cdk_id declarator; otherwise, we
  8835. would not have exited the loop above. */
  8836. if (declarator
  8837. && declarator->u.id.qualifying_scope
  8838. && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
  8839. {
  8840. ctype = declarator->u.id.qualifying_scope;
  8841. ctype = TYPE_MAIN_VARIANT (ctype);
  8842. template_count = num_template_headers_for_class (ctype);
  8843. if (ctype == current_class_type)
  8844. {
  8845. if (friendp)
  8846. {
  8847. permerror (input_location, "member functions are implicitly friends of their class");
  8848. friendp = 0;
  8849. }
  8850. else
  8851. permerror (declarator->id_loc,
  8852. "extra qualification %<%T::%> on member %qs",
  8853. ctype, name);
  8854. }
  8855. else if (/* If the qualifying type is already complete, then we
  8856. can skip the following checks. */
  8857. !COMPLETE_TYPE_P (ctype)
  8858. && (/* If the function is being defined, then
  8859. qualifying type must certainly be complete. */
  8860. funcdef_flag
  8861. /* A friend declaration of "T::f" is OK, even if
  8862. "T" is a template parameter. But, if this
  8863. function is not a friend, the qualifying type
  8864. must be a class. */
  8865. || (!friendp && !CLASS_TYPE_P (ctype))
  8866. /* For a declaration, the type need not be
  8867. complete, if either it is dependent (since there
  8868. is no meaningful definition of complete in that
  8869. case) or the qualifying class is currently being
  8870. defined. */
  8871. || !(dependent_type_p (ctype)
  8872. || currently_open_class (ctype)))
  8873. /* Check that the qualifying type is complete. */
  8874. && !complete_type_or_else (ctype, NULL_TREE))
  8875. return error_mark_node;
  8876. else if (TREE_CODE (type) == FUNCTION_TYPE)
  8877. {
  8878. if (current_class_type
  8879. && (!friendp || funcdef_flag))
  8880. {
  8881. error (funcdef_flag
  8882. ? G_("cannot define member function %<%T::%s%> "
  8883. "within %<%T%>")
  8884. : G_("cannot declare member function %<%T::%s%> "
  8885. "within %<%T%>"),
  8886. ctype, name, current_class_type);
  8887. return error_mark_node;
  8888. }
  8889. }
  8890. else if (typedef_p && current_class_type)
  8891. {
  8892. error ("cannot declare member %<%T::%s%> within %qT",
  8893. ctype, name, current_class_type);
  8894. return error_mark_node;
  8895. }
  8896. }
  8897. if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
  8898. ctype = current_class_type;
  8899. /* Now TYPE has the actual type. */
  8900. if (returned_attrs)
  8901. {
  8902. if (attrlist)
  8903. *attrlist = chainon (returned_attrs, *attrlist);
  8904. else
  8905. attrlist = &returned_attrs;
  8906. }
  8907. if (declarator
  8908. && declarator->kind == cdk_id
  8909. && declarator->std_attributes)
  8910. /* [dcl.meaning]/1: The optional attribute-specifier-seq following
  8911. a declarator-id appertains to the entity that is declared. */
  8912. *attrlist = chainon (*attrlist, declarator->std_attributes);
  8913. /* Handle parameter packs. */
  8914. if (parameter_pack_p)
  8915. {
  8916. if (decl_context == PARM)
  8917. /* Turn the type into a pack expansion.*/
  8918. type = make_pack_expansion (type);
  8919. else
  8920. error ("non-parameter %qs cannot be a parameter pack", name);
  8921. }
  8922. /* Did array size calculations overflow or does the array cover more
  8923. than half of the address-space? */
  8924. if (TREE_CODE (type) == ARRAY_TYPE
  8925. && COMPLETE_TYPE_P (type)
  8926. && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
  8927. && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
  8928. {
  8929. error ("size of array %qs is too large", name);
  8930. /* If we proceed with the array type as it is, we'll eventually
  8931. crash in tree_to_[su]hwi(). */
  8932. type = error_mark_node;
  8933. }
  8934. if ((decl_context == FIELD || decl_context == PARM)
  8935. && !processing_template_decl
  8936. && variably_modified_type_p (type, NULL_TREE))
  8937. {
  8938. if (decl_context == FIELD)
  8939. error ("data member may not have variably modified type %qT", type);
  8940. else
  8941. error ("parameter may not have variably modified type %qT", type);
  8942. type = error_mark_node;
  8943. }
  8944. if (explicitp == 1 || (explicitp && friendp))
  8945. {
  8946. /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
  8947. in the declaration of a constructor or conversion function within
  8948. a class definition. */
  8949. if (!current_class_type)
  8950. error ("%<explicit%> outside class declaration");
  8951. else if (friendp)
  8952. error ("%<explicit%> in friend declaration");
  8953. else
  8954. error ("only declarations of constructors and conversion operators "
  8955. "can be %<explicit%>");
  8956. explicitp = 0;
  8957. }
  8958. if (storage_class == sc_mutable)
  8959. {
  8960. if (decl_context != FIELD || friendp)
  8961. {
  8962. error ("non-member %qs cannot be declared %<mutable%>", name);
  8963. storage_class = sc_none;
  8964. }
  8965. else if (decl_context == TYPENAME || typedef_p)
  8966. {
  8967. error ("non-object member %qs cannot be declared %<mutable%>", name);
  8968. storage_class = sc_none;
  8969. }
  8970. else if (TREE_CODE (type) == FUNCTION_TYPE
  8971. || TREE_CODE (type) == METHOD_TYPE)
  8972. {
  8973. error ("function %qs cannot be declared %<mutable%>", name);
  8974. storage_class = sc_none;
  8975. }
  8976. else if (staticp)
  8977. {
  8978. error ("static %qs cannot be declared %<mutable%>", name);
  8979. storage_class = sc_none;
  8980. }
  8981. else if (type_quals & TYPE_QUAL_CONST)
  8982. {
  8983. error ("const %qs cannot be declared %<mutable%>", name);
  8984. storage_class = sc_none;
  8985. }
  8986. else if (TREE_CODE (type) == REFERENCE_TYPE)
  8987. {
  8988. permerror (input_location, "reference %qs cannot be declared "
  8989. "%<mutable%>", name);
  8990. storage_class = sc_none;
  8991. }
  8992. }
  8993. /* If this is declaring a typedef name, return a TYPE_DECL. */
  8994. if (typedef_p && decl_context != TYPENAME)
  8995. {
  8996. tree decl;
  8997. /* Note that the grammar rejects storage classes
  8998. in typenames, fields or parameters. */
  8999. if (current_lang_name == lang_name_java)
  9000. TYPE_FOR_JAVA (type) = 1;
  9001. /* This declaration:
  9002. typedef void f(int) const;
  9003. declares a function type which is not a member of any
  9004. particular class, but which is cv-qualified; for
  9005. example "f S::*" declares a pointer to a const-qualified
  9006. member function of S. We record the cv-qualification in the
  9007. function type. */
  9008. if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
  9009. {
  9010. type = apply_memfn_quals (type, memfn_quals, rqual);
  9011. /* We have now dealt with these qualifiers. */
  9012. memfn_quals = TYPE_UNQUALIFIED;
  9013. rqual = REF_QUAL_NONE;
  9014. }
  9015. if (type_uses_auto (type))
  9016. {
  9017. error ("typedef declared %<auto%>");
  9018. type = error_mark_node;
  9019. }
  9020. if (decl_context == FIELD)
  9021. decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
  9022. else
  9023. decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
  9024. if (id_declarator && declarator->u.id.qualifying_scope) {
  9025. error_at (DECL_SOURCE_LOCATION (decl),
  9026. "typedef name may not be a nested-name-specifier");
  9027. TREE_TYPE (decl) = error_mark_node;
  9028. }
  9029. if (decl_context != FIELD)
  9030. {
  9031. if (!current_function_decl)
  9032. DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
  9033. else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
  9034. || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
  9035. (current_function_decl)))
  9036. /* The TYPE_DECL is "abstract" because there will be
  9037. clones of this constructor/destructor, and there will
  9038. be copies of this TYPE_DECL generated in those
  9039. clones. The decloning optimization (for space) may
  9040. revert this subsequently if it determines that
  9041. the clones should share a common implementation. */
  9042. DECL_ABSTRACT_P (decl) = true;
  9043. }
  9044. else if (current_class_type
  9045. && constructor_name_p (unqualified_id, current_class_type))
  9046. permerror (input_location, "ISO C++ forbids nested type %qD with same name "
  9047. "as enclosing class",
  9048. unqualified_id);
  9049. /* If the user declares "typedef struct {...} foo" then the
  9050. struct will have an anonymous name. Fill that name in now.
  9051. Nothing can refer to it, so nothing needs know about the name
  9052. change. */
  9053. if (type != error_mark_node
  9054. && unqualified_id
  9055. && TYPE_NAME (type)
  9056. && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  9057. && TYPE_ANONYMOUS_P (type)
  9058. && declspecs->type_definition_p
  9059. && attributes_naming_typedef_ok (*attrlist)
  9060. && cp_type_quals (type) == TYPE_UNQUALIFIED)
  9061. {
  9062. tree t;
  9063. /* Replace the anonymous name with the real name everywhere. */
  9064. for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
  9065. {
  9066. if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
  9067. /* We do not rename the debug info representing the
  9068. anonymous tagged type because the standard says in
  9069. [dcl.typedef] that the naming applies only for
  9070. linkage purposes. */
  9071. /*debug_hooks->set_name (t, decl);*/
  9072. TYPE_NAME (t) = decl;
  9073. }
  9074. if (TYPE_LANG_SPECIFIC (type))
  9075. TYPE_WAS_ANONYMOUS (type) = 1;
  9076. /* If this is a typedef within a template class, the nested
  9077. type is a (non-primary) template. The name for the
  9078. template needs updating as well. */
  9079. if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
  9080. DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
  9081. = TYPE_IDENTIFIER (type);
  9082. /* Adjust linkage now that we aren't anonymous anymore. */
  9083. reset_type_linkage (type);
  9084. /* FIXME remangle member functions; member functions of a
  9085. type with external linkage have external linkage. */
  9086. }
  9087. if (signed_p
  9088. || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
  9089. C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
  9090. bad_specifiers (decl, BSP_TYPE, virtualp,
  9091. memfn_quals != TYPE_UNQUALIFIED,
  9092. inlinep, friendp, raises != NULL_TREE);
  9093. if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
  9094. /* Acknowledge that this was written:
  9095. `using analias = atype;'. */
  9096. TYPE_DECL_ALIAS_P (decl) = 1;
  9097. return decl;
  9098. }
  9099. /* Detect the case of an array type of unspecified size
  9100. which came, as such, direct from a typedef name.
  9101. We must copy the type, so that the array's domain can be
  9102. individually set by the object's initializer. */
  9103. if (type && typedef_type
  9104. && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
  9105. && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
  9106. type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
  9107. /* Detect where we're using a typedef of function type to declare a
  9108. function. PARMS will not be set, so we must create it now. */
  9109. if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
  9110. {
  9111. tree decls = NULL_TREE;
  9112. tree args;
  9113. for (args = TYPE_ARG_TYPES (type);
  9114. args && args != void_list_node;
  9115. args = TREE_CHAIN (args))
  9116. {
  9117. tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
  9118. DECL_CHAIN (decl) = decls;
  9119. decls = decl;
  9120. }
  9121. parms = nreverse (decls);
  9122. if (decl_context != TYPENAME)
  9123. {
  9124. /* The qualifiers on the function type become the qualifiers on
  9125. the non-static member function. */
  9126. memfn_quals |= type_memfn_quals (type);
  9127. rqual = type_memfn_rqual (type);
  9128. type_quals = TYPE_UNQUALIFIED;
  9129. }
  9130. }
  9131. /* If this is a type name (such as, in a cast or sizeof),
  9132. compute the type and return it now. */
  9133. if (decl_context == TYPENAME)
  9134. {
  9135. /* Note that the grammar rejects storage classes
  9136. in typenames, fields or parameters. */
  9137. if (type_quals != TYPE_UNQUALIFIED)
  9138. type_quals = TYPE_UNQUALIFIED;
  9139. /* Special case: "friend class foo" looks like a TYPENAME context. */
  9140. if (friendp)
  9141. {
  9142. if (type_quals != TYPE_UNQUALIFIED)
  9143. {
  9144. error ("type qualifiers specified for friend class declaration");
  9145. type_quals = TYPE_UNQUALIFIED;
  9146. }
  9147. if (inlinep)
  9148. {
  9149. error ("%<inline%> specified for friend class declaration");
  9150. inlinep = 0;
  9151. }
  9152. if (!current_aggr)
  9153. {
  9154. /* Don't allow friend declaration without a class-key. */
  9155. if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
  9156. permerror (input_location, "template parameters cannot be friends");
  9157. else if (TREE_CODE (type) == TYPENAME_TYPE)
  9158. permerror (input_location, "friend declaration requires class-key, "
  9159. "i.e. %<friend class %T::%D%>",
  9160. TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
  9161. else
  9162. permerror (input_location, "friend declaration requires class-key, "
  9163. "i.e. %<friend %#T%>",
  9164. type);
  9165. }
  9166. /* Only try to do this stuff if we didn't already give up. */
  9167. if (type != integer_type_node)
  9168. {
  9169. /* A friendly class? */
  9170. if (current_class_type)
  9171. make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
  9172. /*complain=*/true);
  9173. else
  9174. error ("trying to make class %qT a friend of global scope",
  9175. type);
  9176. type = void_type_node;
  9177. }
  9178. }
  9179. else if (memfn_quals || rqual)
  9180. {
  9181. if (ctype == NULL_TREE
  9182. && TREE_CODE (type) == METHOD_TYPE)
  9183. ctype = TYPE_METHOD_BASETYPE (type);
  9184. if (ctype)
  9185. type = build_memfn_type (type, ctype, memfn_quals, rqual);
  9186. /* Core issue #547: need to allow this in template type args.
  9187. Allow it in general in C++11 for alias-declarations. */
  9188. else if ((template_type_arg || cxx_dialect >= cxx11)
  9189. && TREE_CODE (type) == FUNCTION_TYPE)
  9190. type = apply_memfn_quals (type, memfn_quals, rqual);
  9191. else
  9192. error ("invalid qualifiers on non-member function type");
  9193. }
  9194. return type;
  9195. }
  9196. else if (unqualified_id == NULL_TREE && decl_context != PARM
  9197. && decl_context != CATCHPARM
  9198. && TREE_CODE (type) != UNION_TYPE
  9199. && ! bitfield)
  9200. {
  9201. error ("abstract declarator %qT used as declaration", type);
  9202. return error_mark_node;
  9203. }
  9204. /* Only functions may be declared using an operator-function-id. */
  9205. if (unqualified_id
  9206. && IDENTIFIER_OPNAME_P (unqualified_id)
  9207. && TREE_CODE (type) != FUNCTION_TYPE
  9208. && TREE_CODE (type) != METHOD_TYPE)
  9209. {
  9210. error ("declaration of %qD as non-function", unqualified_id);
  9211. return error_mark_node;
  9212. }
  9213. /* We don't check parameter types here because we can emit a better
  9214. error message later. */
  9215. if (decl_context != PARM)
  9216. {
  9217. type = check_var_type (unqualified_id, type);
  9218. if (type == error_mark_node)
  9219. return error_mark_node;
  9220. }
  9221. /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
  9222. or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
  9223. if (decl_context == PARM || decl_context == CATCHPARM)
  9224. {
  9225. if (ctype || in_namespace)
  9226. error ("cannot use %<::%> in parameter declaration");
  9227. if (type_uses_auto (type))
  9228. {
  9229. if (cxx_dialect >= cxx14)
  9230. error ("%<auto%> parameter not permitted in this context");
  9231. else
  9232. error ("parameter declared %<auto%>");
  9233. type = error_mark_node;
  9234. }
  9235. /* A parameter declared as an array of T is really a pointer to T.
  9236. One declared as a function is really a pointer to a function.
  9237. One declared as a member is really a pointer to member. */
  9238. if (TREE_CODE (type) == ARRAY_TYPE)
  9239. {
  9240. /* Transfer const-ness of array into that of type pointed to. */
  9241. type = build_pointer_type (TREE_TYPE (type));
  9242. type_quals = TYPE_UNQUALIFIED;
  9243. array_parameter_p = true;
  9244. }
  9245. else if (TREE_CODE (type) == FUNCTION_TYPE)
  9246. type = build_pointer_type (type);
  9247. }
  9248. if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
  9249. && !NEW_DELETE_OPNAME_P (unqualified_id))
  9250. {
  9251. cp_cv_quals real_quals = memfn_quals;
  9252. if (cxx_dialect < cxx14 && constexpr_p
  9253. && sfk != sfk_constructor && sfk != sfk_destructor)
  9254. real_quals |= TYPE_QUAL_CONST;
  9255. type = build_memfn_type (type, ctype, real_quals, rqual);
  9256. }
  9257. {
  9258. tree decl;
  9259. if (decl_context == PARM)
  9260. {
  9261. decl = cp_build_parm_decl (unqualified_id, type);
  9262. DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
  9263. bad_specifiers (decl, BSP_PARM, virtualp,
  9264. memfn_quals != TYPE_UNQUALIFIED,
  9265. inlinep, friendp, raises != NULL_TREE);
  9266. }
  9267. else if (decl_context == FIELD)
  9268. {
  9269. if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE
  9270. && type_uses_auto (type))
  9271. {
  9272. error ("non-static data member declared %<auto%>");
  9273. type = error_mark_node;
  9274. }
  9275. /* The C99 flexible array extension. */
  9276. if (!staticp && TREE_CODE (type) == ARRAY_TYPE
  9277. && TYPE_DOMAIN (type) == NULL_TREE)
  9278. {
  9279. tree itype = compute_array_index_type (dname, integer_zero_node,
  9280. tf_warning_or_error);
  9281. type = build_cplus_array_type (TREE_TYPE (type), itype);
  9282. }
  9283. if (type == error_mark_node)
  9284. {
  9285. /* Happens when declaring arrays of sizes which
  9286. are error_mark_node, for example. */
  9287. decl = NULL_TREE;
  9288. }
  9289. else if (in_namespace && !friendp)
  9290. {
  9291. /* Something like struct S { int N::j; }; */
  9292. error ("invalid use of %<::%>");
  9293. return error_mark_node;
  9294. }
  9295. else if (TREE_CODE (type) == FUNCTION_TYPE
  9296. || TREE_CODE (type) == METHOD_TYPE)
  9297. {
  9298. int publicp = 0;
  9299. tree function_context;
  9300. if (friendp == 0)
  9301. {
  9302. /* This should never happen in pure C++ (the check
  9303. could be an assert). It could happen in
  9304. Objective-C++ if someone writes invalid code that
  9305. uses a function declaration for an instance
  9306. variable or property (instance variables and
  9307. properties are parsed as FIELD_DECLs, but they are
  9308. part of an Objective-C class, not a C++ class).
  9309. That code is invalid and is caught by this
  9310. check. */
  9311. if (!ctype)
  9312. {
  9313. error ("declaration of function %qD in invalid context",
  9314. unqualified_id);
  9315. return error_mark_node;
  9316. }
  9317. /* ``A union may [ ... ] not [ have ] virtual functions.''
  9318. ARM 9.5 */
  9319. if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
  9320. {
  9321. error ("function %qD declared virtual inside a union",
  9322. unqualified_id);
  9323. return error_mark_node;
  9324. }
  9325. if (NEW_DELETE_OPNAME_P (unqualified_id))
  9326. {
  9327. if (virtualp)
  9328. {
  9329. error ("%qD cannot be declared virtual, since it "
  9330. "is always static",
  9331. unqualified_id);
  9332. virtualp = 0;
  9333. }
  9334. }
  9335. }
  9336. /* Check that the name used for a destructor makes sense. */
  9337. if (sfk == sfk_destructor)
  9338. {
  9339. tree uqname = id_declarator->u.id.unqualified_name;
  9340. if (!ctype)
  9341. {
  9342. gcc_assert (friendp);
  9343. error ("expected qualified name in friend declaration "
  9344. "for destructor %qD", uqname);
  9345. return error_mark_node;
  9346. }
  9347. if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
  9348. {
  9349. error ("declaration of %qD as member of %qT",
  9350. uqname, ctype);
  9351. return error_mark_node;
  9352. }
  9353. if (constexpr_p)
  9354. {
  9355. error ("a destructor cannot be %<constexpr%>");
  9356. return error_mark_node;
  9357. }
  9358. }
  9359. else if (sfk == sfk_constructor && friendp && !ctype)
  9360. {
  9361. error ("expected qualified name in friend declaration "
  9362. "for constructor %qD",
  9363. id_declarator->u.id.unqualified_name);
  9364. return error_mark_node;
  9365. }
  9366. if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
  9367. {
  9368. tree tmpl = TREE_OPERAND (unqualified_id, 0);
  9369. if (variable_template_p (tmpl))
  9370. {
  9371. error ("specialization of variable template %qD "
  9372. "declared as function", tmpl);
  9373. inform (DECL_SOURCE_LOCATION (tmpl),
  9374. "variable template declared here");
  9375. return error_mark_node;
  9376. }
  9377. }
  9378. /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
  9379. function_context = (ctype != NULL_TREE) ?
  9380. decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
  9381. publicp = (! friendp || ! staticp)
  9382. && function_context == NULL_TREE;
  9383. if (late_return_type_p)
  9384. TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
  9385. decl = grokfndecl (ctype, type,
  9386. TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
  9387. ? unqualified_id : dname,
  9388. parms,
  9389. unqualified_id,
  9390. virtualp, flags, memfn_quals, rqual, raises,
  9391. friendp ? -1 : 0, friendp, publicp,
  9392. inlinep | (2 * constexpr_p),
  9393. initialized == SD_DELETED, sfk,
  9394. funcdef_flag, template_count, in_namespace,
  9395. attrlist, declarator->id_loc);
  9396. decl = set_virt_specifiers (decl, virt_specifiers);
  9397. if (decl == NULL_TREE)
  9398. return error_mark_node;
  9399. #if 0
  9400. /* This clobbers the attrs stored in `decl' from `attrlist'. */
  9401. /* The decl and setting of decl_attr is also turned off. */
  9402. decl = build_decl_attribute_variant (decl, decl_attr);
  9403. #endif
  9404. /* [class.conv.ctor]
  9405. A constructor declared without the function-specifier
  9406. explicit that can be called with a single parameter
  9407. specifies a conversion from the type of its first
  9408. parameter to the type of its class. Such a constructor
  9409. is called a converting constructor. */
  9410. if (explicitp == 2)
  9411. DECL_NONCONVERTING_P (decl) = 1;
  9412. }
  9413. else if (!staticp && !dependent_type_p (type)
  9414. && !COMPLETE_TYPE_P (complete_type (type))
  9415. && (TREE_CODE (type) != ARRAY_TYPE
  9416. || !COMPLETE_TYPE_P (TREE_TYPE (type))
  9417. || initialized == 0))
  9418. {
  9419. if (unqualified_id)
  9420. {
  9421. error ("field %qD has incomplete type %qT",
  9422. unqualified_id, type);
  9423. cxx_incomplete_type_inform (strip_array_types (type));
  9424. }
  9425. else
  9426. error ("name %qT has incomplete type", type);
  9427. type = error_mark_node;
  9428. decl = NULL_TREE;
  9429. }
  9430. else
  9431. {
  9432. if (friendp)
  9433. {
  9434. error ("%qE is neither function nor member function; "
  9435. "cannot be declared friend", unqualified_id);
  9436. friendp = 0;
  9437. }
  9438. decl = NULL_TREE;
  9439. }
  9440. if (friendp)
  9441. {
  9442. /* Friends are treated specially. */
  9443. if (ctype == current_class_type)
  9444. ; /* We already issued a permerror. */
  9445. else if (decl && DECL_NAME (decl))
  9446. {
  9447. if (template_class_depth (current_class_type) == 0)
  9448. {
  9449. decl = check_explicit_specialization
  9450. (unqualified_id, decl, template_count,
  9451. 2 * funcdef_flag + 4);
  9452. if (decl == error_mark_node)
  9453. return error_mark_node;
  9454. }
  9455. decl = do_friend (ctype, unqualified_id, decl,
  9456. *attrlist, flags,
  9457. funcdef_flag);
  9458. return decl;
  9459. }
  9460. else
  9461. return error_mark_node;
  9462. }
  9463. /* Structure field. It may not be a function, except for C++. */
  9464. if (decl == NULL_TREE)
  9465. {
  9466. if (staticp)
  9467. {
  9468. /* C++ allows static class members. All other work
  9469. for this is done by grokfield. */
  9470. decl = build_lang_decl_loc (declarator
  9471. ? declarator->id_loc
  9472. : input_location,
  9473. VAR_DECL, unqualified_id, type);
  9474. set_linkage_for_static_data_member (decl);
  9475. /* Even if there is an in-class initialization, DECL
  9476. is considered undefined until an out-of-class
  9477. definition is provided. */
  9478. DECL_EXTERNAL (decl) = 1;
  9479. if (thread_p)
  9480. {
  9481. set_decl_tls_model (decl, decl_default_tls_model (decl));
  9482. if (declspecs->gnu_thread_keyword_p)
  9483. DECL_GNU_TLS_P (decl) = true;
  9484. }
  9485. if (constexpr_p && !initialized)
  9486. {
  9487. error ("constexpr static data member %qD must have an "
  9488. "initializer", decl);
  9489. constexpr_p = false;
  9490. }
  9491. }
  9492. else
  9493. {
  9494. if (constexpr_p)
  9495. {
  9496. error ("non-static data member %qE declared %<constexpr%>",
  9497. unqualified_id);
  9498. constexpr_p = false;
  9499. }
  9500. decl = build_decl (input_location,
  9501. FIELD_DECL, unqualified_id, type);
  9502. DECL_NONADDRESSABLE_P (decl) = bitfield;
  9503. if (bitfield && !unqualified_id)
  9504. TREE_NO_WARNING (decl) = 1;
  9505. if (storage_class == sc_mutable)
  9506. {
  9507. DECL_MUTABLE_P (decl) = 1;
  9508. storage_class = sc_none;
  9509. }
  9510. if (initialized)
  9511. {
  9512. /* An attempt is being made to initialize a non-static
  9513. member. This is new in C++11. */
  9514. maybe_warn_cpp0x (CPP0X_NSDMI);
  9515. /* If this has been parsed with static storage class, but
  9516. errors forced staticp to be cleared, ensure NSDMI is
  9517. not present. */
  9518. if (declspecs->storage_class == sc_static)
  9519. DECL_INITIAL (decl) = error_mark_node;
  9520. }
  9521. }
  9522. bad_specifiers (decl, BSP_FIELD, virtualp,
  9523. memfn_quals != TYPE_UNQUALIFIED,
  9524. inlinep, friendp, raises != NULL_TREE);
  9525. }
  9526. }
  9527. else if (TREE_CODE (type) == FUNCTION_TYPE
  9528. || TREE_CODE (type) == METHOD_TYPE)
  9529. {
  9530. tree original_name;
  9531. int publicp = 0;
  9532. if (!unqualified_id)
  9533. return error_mark_node;
  9534. if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
  9535. original_name = dname;
  9536. else
  9537. original_name = unqualified_id;
  9538. if (storage_class == sc_auto)
  9539. error ("storage class %<auto%> invalid for function %qs", name);
  9540. else if (storage_class == sc_register)
  9541. error ("storage class %<register%> invalid for function %qs", name);
  9542. else if (thread_p)
  9543. {
  9544. if (declspecs->gnu_thread_keyword_p)
  9545. error ("storage class %<__thread%> invalid for function %qs",
  9546. name);
  9547. else
  9548. error ("storage class %<thread_local%> invalid for function %qs",
  9549. name);
  9550. }
  9551. if (virt_specifiers)
  9552. error ("virt-specifiers in %qs not allowed outside a class definition", name);
  9553. /* Function declaration not at top level.
  9554. Storage classes other than `extern' are not allowed
  9555. and `extern' makes no difference. */
  9556. if (! toplevel_bindings_p ()
  9557. && (storage_class == sc_static
  9558. || decl_spec_seq_has_spec_p (declspecs, ds_inline))
  9559. && pedantic)
  9560. {
  9561. if (storage_class == sc_static)
  9562. pedwarn (input_location, OPT_Wpedantic,
  9563. "%<static%> specified invalid for function %qs "
  9564. "declared out of global scope", name);
  9565. else
  9566. pedwarn (input_location, OPT_Wpedantic,
  9567. "%<inline%> specifier invalid for function %qs "
  9568. "declared out of global scope", name);
  9569. }
  9570. if (ctype == NULL_TREE)
  9571. {
  9572. if (virtualp)
  9573. {
  9574. error ("virtual non-class function %qs", name);
  9575. virtualp = 0;
  9576. }
  9577. else if (sfk == sfk_constructor
  9578. || sfk == sfk_destructor)
  9579. {
  9580. error (funcdef_flag
  9581. ? G_("%qs defined in a non-class scope")
  9582. : G_("%qs declared in a non-class scope"), name);
  9583. sfk = sfk_none;
  9584. }
  9585. }
  9586. /* Record whether the function is public. */
  9587. publicp = (ctype != NULL_TREE
  9588. || storage_class != sc_static);
  9589. if (late_return_type_p)
  9590. TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
  9591. decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
  9592. virtualp, flags, memfn_quals, rqual, raises,
  9593. 1, friendp,
  9594. publicp, inlinep | (2 * constexpr_p),
  9595. initialized == SD_DELETED, sfk,
  9596. funcdef_flag,
  9597. template_count, in_namespace, attrlist,
  9598. declarator->id_loc);
  9599. if (decl == NULL_TREE)
  9600. return error_mark_node;
  9601. if (staticp == 1)
  9602. {
  9603. int invalid_static = 0;
  9604. /* Don't allow a static member function in a class, and forbid
  9605. declaring main to be static. */
  9606. if (TREE_CODE (type) == METHOD_TYPE)
  9607. {
  9608. permerror (input_location, "cannot declare member function %qD to have "
  9609. "static linkage", decl);
  9610. invalid_static = 1;
  9611. }
  9612. else if (current_function_decl)
  9613. {
  9614. /* FIXME need arm citation */
  9615. error ("cannot declare static function inside another function");
  9616. invalid_static = 1;
  9617. }
  9618. if (invalid_static)
  9619. {
  9620. staticp = 0;
  9621. storage_class = sc_none;
  9622. }
  9623. }
  9624. }
  9625. else
  9626. {
  9627. /* It's a variable. */
  9628. /* An uninitialized decl with `extern' is a reference. */
  9629. decl = grokvardecl (type, dname, unqualified_id,
  9630. declspecs,
  9631. initialized,
  9632. (type_quals & TYPE_QUAL_CONST) != 0,
  9633. template_count,
  9634. ctype ? ctype : in_namespace);
  9635. if (decl == NULL_TREE)
  9636. return error_mark_node;
  9637. bad_specifiers (decl, BSP_VAR, virtualp,
  9638. memfn_quals != TYPE_UNQUALIFIED,
  9639. inlinep, friendp, raises != NULL_TREE);
  9640. if (ctype)
  9641. {
  9642. DECL_CONTEXT (decl) = ctype;
  9643. if (staticp == 1)
  9644. {
  9645. permerror (input_location, "%<static%> may not be used when defining "
  9646. "(as opposed to declaring) a static data member");
  9647. staticp = 0;
  9648. storage_class = sc_none;
  9649. }
  9650. if (storage_class == sc_register && TREE_STATIC (decl))
  9651. {
  9652. error ("static member %qD declared %<register%>", decl);
  9653. storage_class = sc_none;
  9654. }
  9655. if (storage_class == sc_extern && pedantic)
  9656. {
  9657. pedwarn (input_location, OPT_Wpedantic,
  9658. "cannot explicitly declare member %q#D to have "
  9659. "extern linkage", decl);
  9660. storage_class = sc_none;
  9661. }
  9662. }
  9663. else if (constexpr_p && DECL_EXTERNAL (decl))
  9664. {
  9665. error ("declaration of constexpr variable %qD is not a definition",
  9666. decl);
  9667. constexpr_p = false;
  9668. }
  9669. }
  9670. if (storage_class == sc_extern && initialized && !funcdef_flag)
  9671. {
  9672. if (toplevel_bindings_p ())
  9673. {
  9674. /* It's common practice (and completely valid) to have a const
  9675. be initialized and declared extern. */
  9676. if (!(type_quals & TYPE_QUAL_CONST))
  9677. warning (0, "%qs initialized and declared %<extern%>", name);
  9678. }
  9679. else
  9680. {
  9681. error ("%qs has both %<extern%> and initializer", name);
  9682. return error_mark_node;
  9683. }
  9684. }
  9685. /* Record `register' declaration for warnings on &
  9686. and in case doing stupid register allocation. */
  9687. if (storage_class == sc_register)
  9688. DECL_REGISTER (decl) = 1;
  9689. else if (storage_class == sc_extern)
  9690. DECL_THIS_EXTERN (decl) = 1;
  9691. else if (storage_class == sc_static)
  9692. DECL_THIS_STATIC (decl) = 1;
  9693. /* Set constexpr flag on vars (functions got it in grokfndecl). */
  9694. if (constexpr_p && VAR_P (decl))
  9695. DECL_DECLARED_CONSTEXPR_P (decl) = true;
  9696. /* Record constancy and volatility on the DECL itself . There's
  9697. no need to do this when processing a template; we'll do this
  9698. for the instantiated declaration based on the type of DECL. */
  9699. if (!processing_template_decl)
  9700. cp_apply_type_quals_to_decl (type_quals, decl);
  9701. return decl;
  9702. }
  9703. }
  9704. /* Subroutine of start_function. Ensure that each of the parameter
  9705. types (as listed in PARMS) is complete, as is required for a
  9706. function definition. */
  9707. static void
  9708. require_complete_types_for_parms (tree parms)
  9709. {
  9710. for (; parms; parms = DECL_CHAIN (parms))
  9711. {
  9712. if (dependent_type_p (TREE_TYPE (parms)))
  9713. continue;
  9714. if (!VOID_TYPE_P (TREE_TYPE (parms))
  9715. && complete_type_or_else (TREE_TYPE (parms), parms))
  9716. {
  9717. relayout_decl (parms);
  9718. DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
  9719. }
  9720. else
  9721. /* grokparms or complete_type_or_else will have already issued
  9722. an error. */
  9723. TREE_TYPE (parms) = error_mark_node;
  9724. }
  9725. }
  9726. /* Returns nonzero if T is a local variable. */
  9727. int
  9728. local_variable_p (const_tree t)
  9729. {
  9730. if ((VAR_P (t)
  9731. /* A VAR_DECL with a context that is a _TYPE is a static data
  9732. member. */
  9733. && !TYPE_P (CP_DECL_CONTEXT (t))
  9734. /* Any other non-local variable must be at namespace scope. */
  9735. && !DECL_NAMESPACE_SCOPE_P (t))
  9736. || (TREE_CODE (t) == PARM_DECL))
  9737. return 1;
  9738. return 0;
  9739. }
  9740. /* Like local_variable_p, but suitable for use as a tree-walking
  9741. function. */
  9742. static tree
  9743. local_variable_p_walkfn (tree *tp, int *walk_subtrees,
  9744. void * /*data*/)
  9745. {
  9746. if (local_variable_p (*tp)
  9747. && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
  9748. return *tp;
  9749. else if (TYPE_P (*tp))
  9750. *walk_subtrees = 0;
  9751. return NULL_TREE;
  9752. }
  9753. /* Check that ARG, which is a default-argument expression for a
  9754. parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
  9755. something goes wrong. DECL may also be a _TYPE node, rather than a
  9756. DECL, if there is no DECL available. */
  9757. tree
  9758. check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
  9759. {
  9760. tree var;
  9761. tree decl_type;
  9762. if (TREE_CODE (arg) == DEFAULT_ARG)
  9763. /* We get a DEFAULT_ARG when looking at an in-class declaration
  9764. with a default argument. Ignore the argument for now; we'll
  9765. deal with it after the class is complete. */
  9766. return arg;
  9767. if (TYPE_P (decl))
  9768. {
  9769. decl_type = decl;
  9770. decl = NULL_TREE;
  9771. }
  9772. else
  9773. decl_type = TREE_TYPE (decl);
  9774. if (arg == error_mark_node
  9775. || decl == error_mark_node
  9776. || TREE_TYPE (arg) == error_mark_node
  9777. || decl_type == error_mark_node)
  9778. /* Something already went wrong. There's no need to check
  9779. further. */
  9780. return error_mark_node;
  9781. /* [dcl.fct.default]
  9782. A default argument expression is implicitly converted to the
  9783. parameter type. */
  9784. ++cp_unevaluated_operand;
  9785. perform_implicit_conversion_flags (decl_type, arg, complain,
  9786. LOOKUP_IMPLICIT);
  9787. --cp_unevaluated_operand;
  9788. /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
  9789. the call sites. */
  9790. if (TYPE_PTR_OR_PTRMEM_P (decl_type)
  9791. && null_ptr_cst_p (arg))
  9792. return nullptr_node;
  9793. /* [dcl.fct.default]
  9794. Local variables shall not be used in default argument
  9795. expressions.
  9796. The keyword `this' shall not be used in a default argument of a
  9797. member function. */
  9798. var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
  9799. if (var)
  9800. {
  9801. if (complain & tf_warning_or_error)
  9802. {
  9803. if (DECL_NAME (var) == this_identifier)
  9804. permerror (input_location, "default argument %qE uses %qD",
  9805. arg, var);
  9806. else
  9807. error ("default argument %qE uses local variable %qD", arg, var);
  9808. }
  9809. return error_mark_node;
  9810. }
  9811. /* All is well. */
  9812. return arg;
  9813. }
  9814. /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
  9815. static tree
  9816. type_is_deprecated (tree type)
  9817. {
  9818. enum tree_code code;
  9819. if (TREE_DEPRECATED (type))
  9820. return type;
  9821. if (TYPE_NAME (type)
  9822. && TREE_DEPRECATED (TYPE_NAME (type)))
  9823. return type;
  9824. /* Do warn about using typedefs to a deprecated class. */
  9825. if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
  9826. return type_is_deprecated (TYPE_MAIN_VARIANT (type));
  9827. code = TREE_CODE (type);
  9828. if (code == POINTER_TYPE || code == REFERENCE_TYPE
  9829. || code == OFFSET_TYPE || code == FUNCTION_TYPE
  9830. || code == METHOD_TYPE || code == ARRAY_TYPE)
  9831. return type_is_deprecated (TREE_TYPE (type));
  9832. if (TYPE_PTRMEMFUNC_P (type))
  9833. return type_is_deprecated
  9834. (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
  9835. return NULL_TREE;
  9836. }
  9837. /* Decode the list of parameter types for a function type.
  9838. Given the list of things declared inside the parens,
  9839. return a list of types.
  9840. If this parameter does not end with an ellipsis, we append
  9841. void_list_node.
  9842. *PARMS is set to the chain of PARM_DECLs created. */
  9843. static tree
  9844. grokparms (tree parmlist, tree *parms)
  9845. {
  9846. tree result = NULL_TREE;
  9847. tree decls = NULL_TREE;
  9848. tree parm;
  9849. int any_error = 0;
  9850. for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
  9851. {
  9852. tree type = NULL_TREE;
  9853. tree init = TREE_PURPOSE (parm);
  9854. tree decl = TREE_VALUE (parm);
  9855. const char *errmsg;
  9856. if (parm == void_list_node)
  9857. break;
  9858. if (! decl || TREE_TYPE (decl) == error_mark_node)
  9859. continue;
  9860. type = TREE_TYPE (decl);
  9861. if (VOID_TYPE_P (type))
  9862. {
  9863. if (same_type_p (type, void_type_node)
  9864. && !init
  9865. && !DECL_NAME (decl) && !result
  9866. && TREE_CHAIN (parm) == void_list_node)
  9867. /* DR 577: A parameter list consisting of a single
  9868. unnamed parameter of non-dependent type 'void'. */
  9869. break;
  9870. else if (cv_qualified_p (type))
  9871. error_at (DECL_SOURCE_LOCATION (decl),
  9872. "invalid use of cv-qualified type %qT in "
  9873. "parameter declaration", type);
  9874. else
  9875. error_at (DECL_SOURCE_LOCATION (decl),
  9876. "invalid use of type %<void%> in parameter "
  9877. "declaration");
  9878. /* It's not a good idea to actually create parameters of
  9879. type `void'; other parts of the compiler assume that a
  9880. void type terminates the parameter list. */
  9881. type = error_mark_node;
  9882. TREE_TYPE (decl) = error_mark_node;
  9883. }
  9884. if (type != error_mark_node
  9885. && TYPE_FOR_JAVA (type)
  9886. && MAYBE_CLASS_TYPE_P (type))
  9887. {
  9888. error ("parameter %qD has Java class type", decl);
  9889. type = error_mark_node;
  9890. TREE_TYPE (decl) = error_mark_node;
  9891. init = NULL_TREE;
  9892. }
  9893. if (type != error_mark_node
  9894. && (errmsg = targetm.invalid_parameter_type (type)))
  9895. {
  9896. error (errmsg);
  9897. type = error_mark_node;
  9898. TREE_TYPE (decl) = error_mark_node;
  9899. }
  9900. if (type != error_mark_node)
  9901. {
  9902. if (deprecated_state != DEPRECATED_SUPPRESS)
  9903. {
  9904. tree deptype = type_is_deprecated (type);
  9905. if (deptype)
  9906. warn_deprecated_use (deptype, NULL_TREE);
  9907. }
  9908. /* Top-level qualifiers on the parameters are
  9909. ignored for function types. */
  9910. type = cp_build_qualified_type (type, 0);
  9911. if (TREE_CODE (type) == METHOD_TYPE)
  9912. {
  9913. error ("parameter %qD invalidly declared method type", decl);
  9914. type = build_pointer_type (type);
  9915. TREE_TYPE (decl) = type;
  9916. }
  9917. else if (abstract_virtuals_error (decl, type))
  9918. any_error = 1; /* Seems like a good idea. */
  9919. else if (POINTER_TYPE_P (type))
  9920. {
  9921. /* [dcl.fct]/6, parameter types cannot contain pointers
  9922. (references) to arrays of unknown bound. */
  9923. tree t = TREE_TYPE (type);
  9924. int ptr = TYPE_PTR_P (type);
  9925. while (1)
  9926. {
  9927. if (TYPE_PTR_P (t))
  9928. ptr = 1;
  9929. else if (TREE_CODE (t) != ARRAY_TYPE)
  9930. break;
  9931. else if (!TYPE_DOMAIN (t))
  9932. break;
  9933. t = TREE_TYPE (t);
  9934. }
  9935. if (TREE_CODE (t) == ARRAY_TYPE)
  9936. error (ptr
  9937. ? G_("parameter %qD includes pointer to array of "
  9938. "unknown bound %qT")
  9939. : G_("parameter %qD includes reference to array of "
  9940. "unknown bound %qT"),
  9941. decl, t);
  9942. }
  9943. if (any_error)
  9944. init = NULL_TREE;
  9945. else if (init && !processing_template_decl)
  9946. init = check_default_argument (decl, init, tf_warning_or_error);
  9947. }
  9948. DECL_CHAIN (decl) = decls;
  9949. decls = decl;
  9950. result = tree_cons (init, type, result);
  9951. }
  9952. decls = nreverse (decls);
  9953. result = nreverse (result);
  9954. if (parm)
  9955. result = chainon (result, void_list_node);
  9956. *parms = decls;
  9957. return result;
  9958. }
  9959. /* D is a constructor or overloaded `operator='.
  9960. Let T be the class in which D is declared. Then, this function
  9961. returns:
  9962. -1 if D's is an ill-formed constructor or copy assignment operator
  9963. whose first parameter is of type `T'.
  9964. 0 if D is not a copy constructor or copy assignment
  9965. operator.
  9966. 1 if D is a copy constructor or copy assignment operator whose
  9967. first parameter is a reference to non-const qualified T.
  9968. 2 if D is a copy constructor or copy assignment operator whose
  9969. first parameter is a reference to const qualified T.
  9970. This function can be used as a predicate. Positive values indicate
  9971. a copy constructor and nonzero values indicate a copy assignment
  9972. operator. */
  9973. int
  9974. copy_fn_p (const_tree d)
  9975. {
  9976. tree args;
  9977. tree arg_type;
  9978. int result = 1;
  9979. gcc_assert (DECL_FUNCTION_MEMBER_P (d));
  9980. if (TREE_CODE (d) == TEMPLATE_DECL
  9981. || (DECL_TEMPLATE_INFO (d)
  9982. && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
  9983. /* Instantiations of template member functions are never copy
  9984. functions. Note that member functions of templated classes are
  9985. represented as template functions internally, and we must
  9986. accept those as copy functions. */
  9987. return 0;
  9988. args = FUNCTION_FIRST_USER_PARMTYPE (d);
  9989. if (!args)
  9990. return 0;
  9991. arg_type = TREE_VALUE (args);
  9992. if (arg_type == error_mark_node)
  9993. return 0;
  9994. if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
  9995. {
  9996. /* Pass by value copy assignment operator. */
  9997. result = -1;
  9998. }
  9999. else if (TREE_CODE (arg_type) == REFERENCE_TYPE
  10000. && !TYPE_REF_IS_RVALUE (arg_type)
  10001. && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
  10002. {
  10003. if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
  10004. result = 2;
  10005. }
  10006. else
  10007. return 0;
  10008. args = TREE_CHAIN (args);
  10009. if (args && args != void_list_node && !TREE_PURPOSE (args))
  10010. /* There are more non-optional args. */
  10011. return 0;
  10012. return result;
  10013. }
  10014. /* D is a constructor or overloaded `operator='.
  10015. Let T be the class in which D is declared. Then, this function
  10016. returns true when D is a move constructor or move assignment
  10017. operator, false otherwise. */
  10018. bool
  10019. move_fn_p (const_tree d)
  10020. {
  10021. gcc_assert (DECL_FUNCTION_MEMBER_P (d));
  10022. if (cxx_dialect == cxx98)
  10023. /* There are no move constructors if we are in C++98 mode. */
  10024. return false;
  10025. if (TREE_CODE (d) == TEMPLATE_DECL
  10026. || (DECL_TEMPLATE_INFO (d)
  10027. && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
  10028. /* Instantiations of template member functions are never move
  10029. functions. Note that member functions of templated classes are
  10030. represented as template functions internally, and we must
  10031. accept those as move functions. */
  10032. return 0;
  10033. return move_signature_fn_p (d);
  10034. }
  10035. /* D is a constructor or overloaded `operator='.
  10036. Then, this function returns true when D has the same signature as a move
  10037. constructor or move assignment operator (because either it is such a
  10038. ctor/op= or it is a template specialization with the same signature),
  10039. false otherwise. */
  10040. bool
  10041. move_signature_fn_p (const_tree d)
  10042. {
  10043. tree args;
  10044. tree arg_type;
  10045. bool result = false;
  10046. args = FUNCTION_FIRST_USER_PARMTYPE (d);
  10047. if (!args)
  10048. return 0;
  10049. arg_type = TREE_VALUE (args);
  10050. if (arg_type == error_mark_node)
  10051. return 0;
  10052. if (TREE_CODE (arg_type) == REFERENCE_TYPE
  10053. && TYPE_REF_IS_RVALUE (arg_type)
  10054. && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
  10055. DECL_CONTEXT (d)))
  10056. result = true;
  10057. args = TREE_CHAIN (args);
  10058. if (args && args != void_list_node && !TREE_PURPOSE (args))
  10059. /* There are more non-optional args. */
  10060. return false;
  10061. return result;
  10062. }
  10063. /* Remember any special properties of member function DECL. */
  10064. void
  10065. grok_special_member_properties (tree decl)
  10066. {
  10067. tree class_type;
  10068. if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
  10069. return;
  10070. class_type = DECL_CONTEXT (decl);
  10071. if (DECL_CONSTRUCTOR_P (decl))
  10072. {
  10073. int ctor = copy_fn_p (decl);
  10074. if (!DECL_ARTIFICIAL (decl))
  10075. TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
  10076. if (ctor > 0)
  10077. {
  10078. /* [class.copy]
  10079. A non-template constructor for class X is a copy
  10080. constructor if its first parameter is of type X&, const
  10081. X&, volatile X& or const volatile X&, and either there
  10082. are no other parameters or else all other parameters have
  10083. default arguments. */
  10084. TYPE_HAS_COPY_CTOR (class_type) = 1;
  10085. if (user_provided_p (decl))
  10086. TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
  10087. if (ctor > 1)
  10088. TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
  10089. }
  10090. else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
  10091. TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
  10092. else if (move_fn_p (decl) && user_provided_p (decl))
  10093. TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
  10094. else if (is_list_ctor (decl))
  10095. TYPE_HAS_LIST_CTOR (class_type) = 1;
  10096. if (DECL_DECLARED_CONSTEXPR_P (decl)
  10097. && !copy_fn_p (decl) && !move_fn_p (decl))
  10098. TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
  10099. }
  10100. else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
  10101. {
  10102. /* [class.copy]
  10103. A non-template assignment operator for class X is a copy
  10104. assignment operator if its parameter is of type X, X&, const
  10105. X&, volatile X& or const volatile X&. */
  10106. int assop = copy_fn_p (decl);
  10107. if (assop)
  10108. {
  10109. TYPE_HAS_COPY_ASSIGN (class_type) = 1;
  10110. if (user_provided_p (decl))
  10111. TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
  10112. if (assop != 1)
  10113. TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
  10114. }
  10115. else if (move_fn_p (decl) && user_provided_p (decl))
  10116. TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
  10117. }
  10118. /* Destructors are handled in check_methods. */
  10119. }
  10120. /* Check a constructor DECL has the correct form. Complains
  10121. if the class has a constructor of the form X(X). */
  10122. int
  10123. grok_ctor_properties (const_tree ctype, const_tree decl)
  10124. {
  10125. int ctor_parm = copy_fn_p (decl);
  10126. if (ctor_parm < 0)
  10127. {
  10128. /* [class.copy]
  10129. A declaration of a constructor for a class X is ill-formed if
  10130. its first parameter is of type (optionally cv-qualified) X
  10131. and either there are no other parameters or else all other
  10132. parameters have default arguments.
  10133. We *don't* complain about member template instantiations that
  10134. have this form, though; they can occur as we try to decide
  10135. what constructor to use during overload resolution. Since
  10136. overload resolution will never prefer such a constructor to
  10137. the non-template copy constructor (which is either explicitly
  10138. or implicitly defined), there's no need to worry about their
  10139. existence. Theoretically, they should never even be
  10140. instantiated, but that's hard to forestall. */
  10141. error ("invalid constructor; you probably meant %<%T (const %T&)%>",
  10142. ctype, ctype);
  10143. return 0;
  10144. }
  10145. return 1;
  10146. }
  10147. /* An operator with this code is unary, but can also be binary. */
  10148. static int
  10149. ambi_op_p (enum tree_code code)
  10150. {
  10151. return (code == INDIRECT_REF
  10152. || code == ADDR_EXPR
  10153. || code == UNARY_PLUS_EXPR
  10154. || code == NEGATE_EXPR
  10155. || code == PREINCREMENT_EXPR
  10156. || code == PREDECREMENT_EXPR);
  10157. }
  10158. /* An operator with this name can only be unary. */
  10159. static int
  10160. unary_op_p (enum tree_code code)
  10161. {
  10162. return (code == TRUTH_NOT_EXPR
  10163. || code == BIT_NOT_EXPR
  10164. || code == COMPONENT_REF
  10165. || code == TYPE_EXPR);
  10166. }
  10167. /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
  10168. errors are issued for invalid declarations. */
  10169. bool
  10170. grok_op_properties (tree decl, bool complain)
  10171. {
  10172. tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
  10173. tree argtype;
  10174. int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
  10175. tree name = DECL_NAME (decl);
  10176. enum tree_code operator_code;
  10177. int arity;
  10178. bool ellipsis_p;
  10179. tree class_type;
  10180. /* Count the number of arguments and check for ellipsis. */
  10181. for (argtype = argtypes, arity = 0;
  10182. argtype && argtype != void_list_node;
  10183. argtype = TREE_CHAIN (argtype))
  10184. ++arity;
  10185. ellipsis_p = !argtype;
  10186. class_type = DECL_CONTEXT (decl);
  10187. if (class_type && !CLASS_TYPE_P (class_type))
  10188. class_type = NULL_TREE;
  10189. if (DECL_CONV_FN_P (decl))
  10190. operator_code = TYPE_EXPR;
  10191. else
  10192. do
  10193. {
  10194. #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
  10195. if (ansi_opname (CODE) == name) \
  10196. { \
  10197. operator_code = (CODE); \
  10198. break; \
  10199. } \
  10200. else if (ansi_assopname (CODE) == name) \
  10201. { \
  10202. operator_code = (CODE); \
  10203. DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
  10204. break; \
  10205. }
  10206. #include "operators.def"
  10207. #undef DEF_OPERATOR
  10208. gcc_unreachable ();
  10209. }
  10210. while (0);
  10211. gcc_assert (operator_code != MAX_TREE_CODES);
  10212. SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
  10213. if (class_type)
  10214. switch (operator_code)
  10215. {
  10216. case NEW_EXPR:
  10217. TYPE_HAS_NEW_OPERATOR (class_type) = 1;
  10218. break;
  10219. case DELETE_EXPR:
  10220. TYPE_GETS_DELETE (class_type) |= 1;
  10221. break;
  10222. case VEC_NEW_EXPR:
  10223. TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
  10224. break;
  10225. case VEC_DELETE_EXPR:
  10226. TYPE_GETS_DELETE (class_type) |= 2;
  10227. break;
  10228. default:
  10229. break;
  10230. }
  10231. /* [basic.std.dynamic.allocation]/1:
  10232. A program is ill-formed if an allocation function is declared
  10233. in a namespace scope other than global scope or declared static
  10234. in global scope.
  10235. The same also holds true for deallocation functions. */
  10236. if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
  10237. || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
  10238. {
  10239. if (DECL_NAMESPACE_SCOPE_P (decl))
  10240. {
  10241. if (CP_DECL_CONTEXT (decl) != global_namespace)
  10242. {
  10243. error ("%qD may not be declared within a namespace", decl);
  10244. return false;
  10245. }
  10246. else if (!TREE_PUBLIC (decl))
  10247. {
  10248. error ("%qD may not be declared as static", decl);
  10249. return false;
  10250. }
  10251. if (!flag_sized_deallocation && warn_cxx14_compat)
  10252. {
  10253. tree parm = FUNCTION_ARG_CHAIN (decl);
  10254. if (parm && same_type_p (TREE_VALUE (parm), size_type_node)
  10255. && TREE_CHAIN (parm) == void_list_node)
  10256. warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_compat,
  10257. "%qD is a usual (non-placement) deallocation "
  10258. "function in C++14 (or with -fsized-deallocation)",
  10259. decl);
  10260. }
  10261. }
  10262. }
  10263. if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
  10264. {
  10265. TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
  10266. DECL_IS_OPERATOR_NEW (decl) = 1;
  10267. }
  10268. else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
  10269. TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
  10270. else
  10271. {
  10272. /* An operator function must either be a non-static member function
  10273. or have at least one parameter of a class, a reference to a class,
  10274. an enumeration, or a reference to an enumeration. 13.4.0.6 */
  10275. if (! methodp || DECL_STATIC_FUNCTION_P (decl))
  10276. {
  10277. if (operator_code == TYPE_EXPR
  10278. || operator_code == CALL_EXPR
  10279. || operator_code == COMPONENT_REF
  10280. || operator_code == ARRAY_REF
  10281. || operator_code == NOP_EXPR)
  10282. {
  10283. error ("%qD must be a nonstatic member function", decl);
  10284. return false;
  10285. }
  10286. else
  10287. {
  10288. tree p;
  10289. if (DECL_STATIC_FUNCTION_P (decl))
  10290. {
  10291. error ("%qD must be either a non-static member "
  10292. "function or a non-member function", decl);
  10293. return false;
  10294. }
  10295. for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
  10296. {
  10297. tree arg = non_reference (TREE_VALUE (p));
  10298. if (arg == error_mark_node)
  10299. return false;
  10300. /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
  10301. because these checks are performed even on
  10302. template functions. */
  10303. if (MAYBE_CLASS_TYPE_P (arg)
  10304. || TREE_CODE (arg) == ENUMERAL_TYPE)
  10305. break;
  10306. }
  10307. if (!p || p == void_list_node)
  10308. {
  10309. if (complain)
  10310. error ("%qD must have an argument of class or "
  10311. "enumerated type", decl);
  10312. return false;
  10313. }
  10314. }
  10315. }
  10316. /* There are no restrictions on the arguments to an overloaded
  10317. "operator ()". */
  10318. if (operator_code == CALL_EXPR)
  10319. return true;
  10320. /* Warn about conversion operators that will never be used. */
  10321. if (IDENTIFIER_TYPENAME_P (name)
  10322. && ! DECL_TEMPLATE_INFO (decl)
  10323. && warn_conversion
  10324. /* Warn only declaring the function; there is no need to
  10325. warn again about out-of-class definitions. */
  10326. && class_type == current_class_type)
  10327. {
  10328. tree t = TREE_TYPE (name);
  10329. int ref = (TREE_CODE (t) == REFERENCE_TYPE);
  10330. if (ref)
  10331. t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
  10332. if (VOID_TYPE_P (t))
  10333. warning (OPT_Wconversion,
  10334. ref
  10335. ? G_("conversion to a reference to void "
  10336. "will never use a type conversion operator")
  10337. : G_("conversion to void "
  10338. "will never use a type conversion operator"));
  10339. else if (class_type)
  10340. {
  10341. if (t == class_type)
  10342. warning (OPT_Wconversion,
  10343. ref
  10344. ? G_("conversion to a reference to the same type "
  10345. "will never use a type conversion operator")
  10346. : G_("conversion to the same type "
  10347. "will never use a type conversion operator"));
  10348. /* Don't force t to be complete here. */
  10349. else if (MAYBE_CLASS_TYPE_P (t)
  10350. && COMPLETE_TYPE_P (t)
  10351. && DERIVED_FROM_P (t, class_type))
  10352. warning (OPT_Wconversion,
  10353. ref
  10354. ? G_("conversion to a reference to a base class "
  10355. "will never use a type conversion operator")
  10356. : G_("conversion to a base class "
  10357. "will never use a type conversion operator"));
  10358. }
  10359. }
  10360. if (operator_code == COND_EXPR)
  10361. {
  10362. /* 13.4.0.3 */
  10363. error ("ISO C++ prohibits overloading operator ?:");
  10364. return false;
  10365. }
  10366. else if (ellipsis_p)
  10367. {
  10368. error ("%qD must not have variable number of arguments", decl);
  10369. return false;
  10370. }
  10371. else if (ambi_op_p (operator_code))
  10372. {
  10373. if (arity == 1)
  10374. /* We pick the one-argument operator codes by default, so
  10375. we don't have to change anything. */
  10376. ;
  10377. else if (arity == 2)
  10378. {
  10379. /* If we thought this was a unary operator, we now know
  10380. it to be a binary operator. */
  10381. switch (operator_code)
  10382. {
  10383. case INDIRECT_REF:
  10384. operator_code = MULT_EXPR;
  10385. break;
  10386. case ADDR_EXPR:
  10387. operator_code = BIT_AND_EXPR;
  10388. break;
  10389. case UNARY_PLUS_EXPR:
  10390. operator_code = PLUS_EXPR;
  10391. break;
  10392. case NEGATE_EXPR:
  10393. operator_code = MINUS_EXPR;
  10394. break;
  10395. case PREINCREMENT_EXPR:
  10396. operator_code = POSTINCREMENT_EXPR;
  10397. break;
  10398. case PREDECREMENT_EXPR:
  10399. operator_code = POSTDECREMENT_EXPR;
  10400. break;
  10401. default:
  10402. gcc_unreachable ();
  10403. }
  10404. SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
  10405. if ((operator_code == POSTINCREMENT_EXPR
  10406. || operator_code == POSTDECREMENT_EXPR)
  10407. && ! processing_template_decl
  10408. && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
  10409. {
  10410. if (methodp)
  10411. error ("postfix %qD must take %<int%> as its argument",
  10412. decl);
  10413. else
  10414. error ("postfix %qD must take %<int%> as its second "
  10415. "argument", decl);
  10416. return false;
  10417. }
  10418. }
  10419. else
  10420. {
  10421. if (methodp)
  10422. error ("%qD must take either zero or one argument", decl);
  10423. else
  10424. error ("%qD must take either one or two arguments", decl);
  10425. return false;
  10426. }
  10427. /* More Effective C++ rule 6. */
  10428. if (warn_ecpp
  10429. && (operator_code == POSTINCREMENT_EXPR
  10430. || operator_code == POSTDECREMENT_EXPR
  10431. || operator_code == PREINCREMENT_EXPR
  10432. || operator_code == PREDECREMENT_EXPR))
  10433. {
  10434. tree arg = TREE_VALUE (argtypes);
  10435. tree ret = TREE_TYPE (TREE_TYPE (decl));
  10436. if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
  10437. arg = TREE_TYPE (arg);
  10438. arg = TYPE_MAIN_VARIANT (arg);
  10439. if (operator_code == PREINCREMENT_EXPR
  10440. || operator_code == PREDECREMENT_EXPR)
  10441. {
  10442. if (TREE_CODE (ret) != REFERENCE_TYPE
  10443. || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
  10444. arg))
  10445. warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
  10446. build_reference_type (arg));
  10447. }
  10448. else
  10449. {
  10450. if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
  10451. warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
  10452. }
  10453. }
  10454. }
  10455. else if (unary_op_p (operator_code))
  10456. {
  10457. if (arity != 1)
  10458. {
  10459. if (methodp)
  10460. error ("%qD must take %<void%>", decl);
  10461. else
  10462. error ("%qD must take exactly one argument", decl);
  10463. return false;
  10464. }
  10465. }
  10466. else /* if (binary_op_p (operator_code)) */
  10467. {
  10468. if (arity != 2)
  10469. {
  10470. if (methodp)
  10471. error ("%qD must take exactly one argument", decl);
  10472. else
  10473. error ("%qD must take exactly two arguments", decl);
  10474. return false;
  10475. }
  10476. /* More Effective C++ rule 7. */
  10477. if (warn_ecpp
  10478. && (operator_code == TRUTH_ANDIF_EXPR
  10479. || operator_code == TRUTH_ORIF_EXPR
  10480. || operator_code == COMPOUND_EXPR))
  10481. warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
  10482. decl);
  10483. }
  10484. /* Effective C++ rule 23. */
  10485. if (warn_ecpp
  10486. && arity == 2
  10487. && !DECL_ASSIGNMENT_OPERATOR_P (decl)
  10488. && (operator_code == PLUS_EXPR
  10489. || operator_code == MINUS_EXPR
  10490. || operator_code == TRUNC_DIV_EXPR
  10491. || operator_code == MULT_EXPR
  10492. || operator_code == TRUNC_MOD_EXPR)
  10493. && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
  10494. warning (OPT_Weffc__, "%qD should return by value", decl);
  10495. /* [over.oper]/8 */
  10496. for (; argtypes && argtypes != void_list_node;
  10497. argtypes = TREE_CHAIN (argtypes))
  10498. if (TREE_PURPOSE (argtypes))
  10499. {
  10500. TREE_PURPOSE (argtypes) = NULL_TREE;
  10501. if (operator_code == POSTINCREMENT_EXPR
  10502. || operator_code == POSTDECREMENT_EXPR)
  10503. {
  10504. pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
  10505. decl);
  10506. }
  10507. else
  10508. {
  10509. error ("%qD cannot have default arguments", decl);
  10510. return false;
  10511. }
  10512. }
  10513. }
  10514. return true;
  10515. }
  10516. /* Return a string giving the keyword associate with CODE. */
  10517. static const char *
  10518. tag_name (enum tag_types code)
  10519. {
  10520. switch (code)
  10521. {
  10522. case record_type:
  10523. return "struct";
  10524. case class_type:
  10525. return "class";
  10526. case union_type:
  10527. return "union";
  10528. case enum_type:
  10529. return "enum";
  10530. case typename_type:
  10531. return "typename";
  10532. default:
  10533. gcc_unreachable ();
  10534. }
  10535. }
  10536. /* Name lookup in an elaborated-type-specifier (after the keyword
  10537. indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
  10538. elaborated-type-specifier is invalid, issue a diagnostic and return
  10539. error_mark_node; otherwise, return the *_TYPE to which it referred.
  10540. If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
  10541. tree
  10542. check_elaborated_type_specifier (enum tag_types tag_code,
  10543. tree decl,
  10544. bool allow_template_p)
  10545. {
  10546. tree type;
  10547. /* In the case of:
  10548. struct S { struct S *p; };
  10549. name lookup will find the TYPE_DECL for the implicit "S::S"
  10550. typedef. Adjust for that here. */
  10551. if (DECL_SELF_REFERENCE_P (decl))
  10552. decl = TYPE_NAME (TREE_TYPE (decl));
  10553. type = TREE_TYPE (decl);
  10554. /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
  10555. is false for this case as well. */
  10556. if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
  10557. {
  10558. error ("using template type parameter %qT after %qs",
  10559. type, tag_name (tag_code));
  10560. return error_mark_node;
  10561. }
  10562. /* Accept template template parameters. */
  10563. else if (allow_template_p
  10564. && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
  10565. || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
  10566. ;
  10567. /* [dcl.type.elab]
  10568. If the identifier resolves to a typedef-name or the
  10569. simple-template-id resolves to an alias template
  10570. specialization, the elaborated-type-specifier is ill-formed.
  10571. In other words, the only legitimate declaration to use in the
  10572. elaborated type specifier is the implicit typedef created when
  10573. the type is declared. */
  10574. else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
  10575. && !DECL_SELF_REFERENCE_P (decl)
  10576. && tag_code != typename_type)
  10577. {
  10578. if (alias_template_specialization_p (type))
  10579. error ("using alias template specialization %qT after %qs",
  10580. type, tag_name (tag_code));
  10581. else
  10582. error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
  10583. inform (DECL_SOURCE_LOCATION (decl),
  10584. "%qD has a previous declaration here", decl);
  10585. return error_mark_node;
  10586. }
  10587. else if (TREE_CODE (type) != RECORD_TYPE
  10588. && TREE_CODE (type) != UNION_TYPE
  10589. && tag_code != enum_type
  10590. && tag_code != typename_type)
  10591. {
  10592. error ("%qT referred to as %qs", type, tag_name (tag_code));
  10593. inform (input_location, "%q+T has a previous declaration here", type);
  10594. return error_mark_node;
  10595. }
  10596. else if (TREE_CODE (type) != ENUMERAL_TYPE
  10597. && tag_code == enum_type)
  10598. {
  10599. error ("%qT referred to as enum", type);
  10600. inform (input_location, "%q+T has a previous declaration here", type);
  10601. return error_mark_node;
  10602. }
  10603. else if (!allow_template_p
  10604. && TREE_CODE (type) == RECORD_TYPE
  10605. && CLASSTYPE_IS_TEMPLATE (type))
  10606. {
  10607. /* If a class template appears as elaborated type specifier
  10608. without a template header such as:
  10609. template <class T> class C {};
  10610. void f(class C); // No template header here
  10611. then the required template argument is missing. */
  10612. error ("template argument required for %<%s %T%>",
  10613. tag_name (tag_code),
  10614. DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
  10615. return error_mark_node;
  10616. }
  10617. return type;
  10618. }
  10619. /* Lookup NAME in elaborate type specifier in scope according to
  10620. SCOPE and issue diagnostics if necessary.
  10621. Return *_TYPE node upon success, NULL_TREE when the NAME is not
  10622. found, and ERROR_MARK_NODE for type error. */
  10623. static tree
  10624. lookup_and_check_tag (enum tag_types tag_code, tree name,
  10625. tag_scope scope, bool template_header_p)
  10626. {
  10627. tree t;
  10628. tree decl;
  10629. if (scope == ts_global)
  10630. {
  10631. /* First try ordinary name lookup, ignoring hidden class name
  10632. injected via friend declaration. */
  10633. decl = lookup_name_prefer_type (name, 2);
  10634. decl = strip_using_decl (decl);
  10635. /* If that fails, the name will be placed in the smallest
  10636. non-class, non-function-prototype scope according to 3.3.1/5.
  10637. We may already have a hidden name declared as friend in this
  10638. scope. So lookup again but not ignoring hidden names.
  10639. If we find one, that name will be made visible rather than
  10640. creating a new tag. */
  10641. if (!decl)
  10642. decl = lookup_type_scope (name, ts_within_enclosing_non_class);
  10643. }
  10644. else
  10645. decl = lookup_type_scope (name, scope);
  10646. if (decl
  10647. && (DECL_CLASS_TEMPLATE_P (decl)
  10648. /* If scope is ts_current we're defining a class, so ignore a
  10649. template template parameter. */
  10650. || (scope != ts_current
  10651. && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
  10652. decl = DECL_TEMPLATE_RESULT (decl);
  10653. if (decl && TREE_CODE (decl) == TYPE_DECL)
  10654. {
  10655. /* Look for invalid nested type:
  10656. class C {
  10657. class C {};
  10658. }; */
  10659. if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
  10660. {
  10661. error ("%qD has the same name as the class in which it is "
  10662. "declared",
  10663. decl);
  10664. return error_mark_node;
  10665. }
  10666. /* Two cases we need to consider when deciding if a class
  10667. template is allowed as an elaborated type specifier:
  10668. 1. It is a self reference to its own class.
  10669. 2. It comes with a template header.
  10670. For example:
  10671. template <class T> class C {
  10672. class C *c1; // DECL_SELF_REFERENCE_P is true
  10673. class D;
  10674. };
  10675. template <class U> class C; // template_header_p is true
  10676. template <class T> class C<T>::D {
  10677. class C *c2; // DECL_SELF_REFERENCE_P is true
  10678. }; */
  10679. t = check_elaborated_type_specifier (tag_code,
  10680. decl,
  10681. template_header_p
  10682. | DECL_SELF_REFERENCE_P (decl));
  10683. return t;
  10684. }
  10685. else if (decl && TREE_CODE (decl) == TREE_LIST)
  10686. {
  10687. error ("reference to %qD is ambiguous", name);
  10688. print_candidates (decl);
  10689. return error_mark_node;
  10690. }
  10691. else
  10692. return NULL_TREE;
  10693. }
  10694. /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
  10695. Define the tag as a forward-reference if it is not defined.
  10696. If a declaration is given, process it here, and report an error if
  10697. multiple declarations are not identical.
  10698. SCOPE is TS_CURRENT when this is also a definition. Only look in
  10699. the current frame for the name (since C++ allows new names in any
  10700. scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
  10701. declaration. Only look beginning from the current scope outward up
  10702. till the nearest non-class scope. Otherwise it is TS_GLOBAL.
  10703. TEMPLATE_HEADER_P is true when this declaration is preceded by
  10704. a set of template parameters. */
  10705. static tree
  10706. xref_tag_1 (enum tag_types tag_code, tree name,
  10707. tag_scope orig_scope, bool template_header_p)
  10708. {
  10709. enum tree_code code;
  10710. tree t;
  10711. tree context = NULL_TREE;
  10712. tag_scope scope;
  10713. gcc_assert (identifier_p (name));
  10714. switch (tag_code)
  10715. {
  10716. case record_type:
  10717. case class_type:
  10718. code = RECORD_TYPE;
  10719. break;
  10720. case union_type:
  10721. code = UNION_TYPE;
  10722. break;
  10723. case enum_type:
  10724. code = ENUMERAL_TYPE;
  10725. break;
  10726. default:
  10727. gcc_unreachable ();
  10728. }
  10729. if (orig_scope == ts_lambda)
  10730. scope = ts_current;
  10731. else
  10732. scope = orig_scope;
  10733. /* In case of anonymous name, xref_tag is only called to
  10734. make type node and push name. Name lookup is not required. */
  10735. if (ANON_AGGRNAME_P (name))
  10736. t = NULL_TREE;
  10737. else
  10738. t = lookup_and_check_tag (tag_code, name,
  10739. scope, template_header_p);
  10740. if (t == error_mark_node)
  10741. return error_mark_node;
  10742. if (scope != ts_current && t && current_class_type
  10743. && template_class_depth (current_class_type)
  10744. && template_header_p)
  10745. {
  10746. if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
  10747. return t;
  10748. /* Since SCOPE is not TS_CURRENT, we are not looking at a
  10749. definition of this tag. Since, in addition, we are currently
  10750. processing a (member) template declaration of a template
  10751. class, we must be very careful; consider:
  10752. template <class X>
  10753. struct S1
  10754. template <class U>
  10755. struct S2
  10756. { template <class V>
  10757. friend struct S1; };
  10758. Here, the S2::S1 declaration should not be confused with the
  10759. outer declaration. In particular, the inner version should
  10760. have a template parameter of level 2, not level 1. This
  10761. would be particularly important if the member declaration
  10762. were instead:
  10763. template <class V = U> friend struct S1;
  10764. say, when we should tsubst into `U' when instantiating
  10765. S2. On the other hand, when presented with:
  10766. template <class T>
  10767. struct S1 {
  10768. template <class U>
  10769. struct S2 {};
  10770. template <class U>
  10771. friend struct S2;
  10772. };
  10773. we must find the inner binding eventually. We
  10774. accomplish this by making sure that the new type we
  10775. create to represent this declaration has the right
  10776. TYPE_CONTEXT. */
  10777. context = TYPE_CONTEXT (t);
  10778. t = NULL_TREE;
  10779. }
  10780. if (! t)
  10781. {
  10782. /* If no such tag is yet defined, create a forward-reference node
  10783. and record it as the "definition".
  10784. When a real declaration of this type is found,
  10785. the forward-reference will be altered into a real type. */
  10786. if (code == ENUMERAL_TYPE)
  10787. {
  10788. error ("use of enum %q#D without previous declaration", name);
  10789. return error_mark_node;
  10790. }
  10791. else
  10792. {
  10793. t = make_class_type (code);
  10794. TYPE_CONTEXT (t) = context;
  10795. if (orig_scope == ts_lambda)
  10796. /* Remember that we're declaring a lambda to avoid bogus errors
  10797. in push_template_decl. */
  10798. CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
  10799. t = pushtag (name, t, scope);
  10800. }
  10801. }
  10802. else
  10803. {
  10804. if (template_header_p && MAYBE_CLASS_TYPE_P (t))
  10805. {
  10806. if (!redeclare_class_template (t, current_template_parms))
  10807. return error_mark_node;
  10808. }
  10809. else if (!processing_template_decl
  10810. && CLASS_TYPE_P (t)
  10811. && CLASSTYPE_IS_TEMPLATE (t))
  10812. {
  10813. error ("redeclaration of %qT as a non-template", t);
  10814. error ("previous declaration %q+D", t);
  10815. return error_mark_node;
  10816. }
  10817. /* Make injected friend class visible. */
  10818. if (scope != ts_within_enclosing_non_class
  10819. && hidden_name_p (TYPE_NAME (t)))
  10820. {
  10821. DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
  10822. DECL_FRIEND_P (TYPE_NAME (t)) = 0;
  10823. if (TYPE_TEMPLATE_INFO (t))
  10824. {
  10825. DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
  10826. DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
  10827. }
  10828. }
  10829. }
  10830. return t;
  10831. }
  10832. /* Wrapper for xref_tag_1. */
  10833. tree
  10834. xref_tag (enum tag_types tag_code, tree name,
  10835. tag_scope scope, bool template_header_p)
  10836. {
  10837. tree ret;
  10838. bool subtime;
  10839. subtime = timevar_cond_start (TV_NAME_LOOKUP);
  10840. ret = xref_tag_1 (tag_code, name, scope, template_header_p);
  10841. timevar_cond_stop (TV_NAME_LOOKUP, subtime);
  10842. return ret;
  10843. }
  10844. tree
  10845. xref_tag_from_type (tree old, tree id, tag_scope scope)
  10846. {
  10847. enum tag_types tag_kind;
  10848. if (TREE_CODE (old) == RECORD_TYPE)
  10849. tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
  10850. else
  10851. tag_kind = union_type;
  10852. if (id == NULL_TREE)
  10853. id = TYPE_IDENTIFIER (old);
  10854. return xref_tag (tag_kind, id, scope, false);
  10855. }
  10856. /* Create the binfo hierarchy for REF with (possibly NULL) base list
  10857. BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
  10858. access_* node, and the TREE_VALUE is the type of the base-class.
  10859. Non-NULL TREE_TYPE indicates virtual inheritance.
  10860. Returns true if the binfo hierarchy was successfully created,
  10861. false if an error was detected. */
  10862. bool
  10863. xref_basetypes (tree ref, tree base_list)
  10864. {
  10865. tree *basep;
  10866. tree binfo, base_binfo;
  10867. unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
  10868. unsigned max_bases = 0; /* Maximum direct bases. */
  10869. int i;
  10870. tree default_access;
  10871. tree igo_prev; /* Track Inheritance Graph Order. */
  10872. if (ref == error_mark_node)
  10873. return false;
  10874. /* The base of a derived class is private by default, all others are
  10875. public. */
  10876. default_access = (TREE_CODE (ref) == RECORD_TYPE
  10877. && CLASSTYPE_DECLARED_CLASS (ref)
  10878. ? access_private_node : access_public_node);
  10879. /* First, make sure that any templates in base-classes are
  10880. instantiated. This ensures that if we call ourselves recursively
  10881. we do not get confused about which classes are marked and which
  10882. are not. */
  10883. basep = &base_list;
  10884. while (*basep)
  10885. {
  10886. tree basetype = TREE_VALUE (*basep);
  10887. /* The dependent_type_p call below should really be dependent_scope_p
  10888. so that we give a hard error about using an incomplete type as a
  10889. base, but we allow it with a pedwarn for backward
  10890. compatibility. */
  10891. if (processing_template_decl
  10892. && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
  10893. cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
  10894. if (!dependent_type_p (basetype)
  10895. && !complete_type_or_else (basetype, NULL))
  10896. /* An incomplete type. Remove it from the list. */
  10897. *basep = TREE_CHAIN (*basep);
  10898. else
  10899. {
  10900. max_bases++;
  10901. if (TREE_TYPE (*basep))
  10902. max_vbases++;
  10903. if (CLASS_TYPE_P (basetype))
  10904. max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
  10905. basep = &TREE_CHAIN (*basep);
  10906. }
  10907. }
  10908. TYPE_MARKED_P (ref) = 1;
  10909. /* The binfo slot should be empty, unless this is an (ill-formed)
  10910. redefinition. */
  10911. if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
  10912. {
  10913. error ("redefinition of %q#T", ref);
  10914. return false;
  10915. }
  10916. gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
  10917. binfo = make_tree_binfo (max_bases);
  10918. TYPE_BINFO (ref) = binfo;
  10919. BINFO_OFFSET (binfo) = size_zero_node;
  10920. BINFO_TYPE (binfo) = ref;
  10921. /* Apply base-class info set up to the variants of this type. */
  10922. fixup_type_variants (ref);
  10923. if (max_bases)
  10924. {
  10925. vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
  10926. /* An aggregate cannot have baseclasses. */
  10927. CLASSTYPE_NON_AGGREGATE (ref) = 1;
  10928. if (TREE_CODE (ref) == UNION_TYPE)
  10929. {
  10930. error ("derived union %qT invalid", ref);
  10931. return false;
  10932. }
  10933. }
  10934. if (max_bases > 1)
  10935. {
  10936. if (TYPE_FOR_JAVA (ref))
  10937. {
  10938. error ("Java class %qT cannot have multiple bases", ref);
  10939. return false;
  10940. }
  10941. }
  10942. if (max_vbases)
  10943. {
  10944. vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
  10945. if (TYPE_FOR_JAVA (ref))
  10946. {
  10947. error ("Java class %qT cannot have virtual bases", ref);
  10948. return false;
  10949. }
  10950. }
  10951. for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
  10952. {
  10953. tree access = TREE_PURPOSE (base_list);
  10954. int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
  10955. tree basetype = TREE_VALUE (base_list);
  10956. if (access == access_default_node)
  10957. access = default_access;
  10958. if (PACK_EXPANSION_P (basetype))
  10959. basetype = PACK_EXPANSION_PATTERN (basetype);
  10960. if (TREE_CODE (basetype) == TYPE_DECL)
  10961. basetype = TREE_TYPE (basetype);
  10962. if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
  10963. {
  10964. error ("base type %qT fails to be a struct or class type",
  10965. basetype);
  10966. return false;
  10967. }
  10968. if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
  10969. TYPE_FOR_JAVA (ref) = 1;
  10970. base_binfo = NULL_TREE;
  10971. if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
  10972. {
  10973. base_binfo = TYPE_BINFO (basetype);
  10974. /* The original basetype could have been a typedef'd type. */
  10975. basetype = BINFO_TYPE (base_binfo);
  10976. /* Inherit flags from the base. */
  10977. TYPE_HAS_NEW_OPERATOR (ref)
  10978. |= TYPE_HAS_NEW_OPERATOR (basetype);
  10979. TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
  10980. |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
  10981. TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
  10982. TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
  10983. CLASSTYPE_DIAMOND_SHAPED_P (ref)
  10984. |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
  10985. CLASSTYPE_REPEATED_BASE_P (ref)
  10986. |= CLASSTYPE_REPEATED_BASE_P (basetype);
  10987. }
  10988. /* We must do this test after we've seen through a typedef
  10989. type. */
  10990. if (TYPE_MARKED_P (basetype))
  10991. {
  10992. if (basetype == ref)
  10993. error ("recursive type %qT undefined", basetype);
  10994. else
  10995. error ("duplicate base type %qT invalid", basetype);
  10996. return false;
  10997. }
  10998. if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
  10999. /* Regenerate the pack expansion for the bases. */
  11000. basetype = make_pack_expansion (basetype);
  11001. TYPE_MARKED_P (basetype) = 1;
  11002. base_binfo = copy_binfo (base_binfo, basetype, ref,
  11003. &igo_prev, via_virtual);
  11004. if (!BINFO_INHERITANCE_CHAIN (base_binfo))
  11005. BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
  11006. BINFO_BASE_APPEND (binfo, base_binfo);
  11007. BINFO_BASE_ACCESS_APPEND (binfo, access);
  11008. }
  11009. if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
  11010. /* If we didn't get max_vbases vbases, we must have shared at
  11011. least one of them, and are therefore diamond shaped. */
  11012. CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
  11013. /* Unmark all the types. */
  11014. for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  11015. TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
  11016. TYPE_MARKED_P (ref) = 0;
  11017. /* Now see if we have a repeated base type. */
  11018. if (!CLASSTYPE_REPEATED_BASE_P (ref))
  11019. {
  11020. for (base_binfo = binfo; base_binfo;
  11021. base_binfo = TREE_CHAIN (base_binfo))
  11022. {
  11023. if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
  11024. {
  11025. CLASSTYPE_REPEATED_BASE_P (ref) = 1;
  11026. break;
  11027. }
  11028. TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
  11029. }
  11030. for (base_binfo = binfo; base_binfo;
  11031. base_binfo = TREE_CHAIN (base_binfo))
  11032. if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
  11033. TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
  11034. else
  11035. break;
  11036. }
  11037. return true;
  11038. }
  11039. /* Copies the enum-related properties from type SRC to type DST.
  11040. Used with the underlying type of an enum and the enum itself. */
  11041. static void
  11042. copy_type_enum (tree dst, tree src)
  11043. {
  11044. tree t;
  11045. for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
  11046. {
  11047. TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
  11048. TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
  11049. TYPE_SIZE (t) = TYPE_SIZE (src);
  11050. TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
  11051. SET_TYPE_MODE (dst, TYPE_MODE (src));
  11052. TYPE_PRECISION (t) = TYPE_PRECISION (src);
  11053. TYPE_ALIGN (t) = TYPE_ALIGN (src);
  11054. TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
  11055. TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
  11056. }
  11057. }
  11058. /* Begin compiling the definition of an enumeration type.
  11059. NAME is its name,
  11060. if ENUMTYPE is not NULL_TREE then the type has alredy been found.
  11061. UNDERLYING_TYPE is the type that will be used as the storage for
  11062. the enumeration type. This should be NULL_TREE if no storage type
  11063. was specified.
  11064. SCOPED_ENUM_P is true if this is a scoped enumeration type.
  11065. if IS_NEW is not NULL, gets TRUE iff a new type is created.
  11066. Returns the type object, as yet incomplete.
  11067. Also records info about it so that build_enumerator
  11068. may be used to declare the individual values as they are read. */
  11069. tree
  11070. start_enum (tree name, tree enumtype, tree underlying_type,
  11071. bool scoped_enum_p, bool *is_new)
  11072. {
  11073. tree prevtype = NULL_TREE;
  11074. gcc_assert (identifier_p (name));
  11075. if (is_new)
  11076. *is_new = false;
  11077. /* [C++0x dcl.enum]p5:
  11078. If not explicitly specified, the underlying type of a scoped
  11079. enumeration type is int. */
  11080. if (!underlying_type && scoped_enum_p)
  11081. underlying_type = integer_type_node;
  11082. if (underlying_type)
  11083. underlying_type = cv_unqualified (underlying_type);
  11084. /* If this is the real definition for a previous forward reference,
  11085. fill in the contents in the same object that used to be the
  11086. forward reference. */
  11087. if (!enumtype)
  11088. enumtype = lookup_and_check_tag (enum_type, name,
  11089. /*tag_scope=*/ts_current,
  11090. /*template_header_p=*/false);
  11091. /* In case of a template_decl, the only check that should be deferred
  11092. to instantiation time is the comparison of underlying types. */
  11093. if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
  11094. {
  11095. if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
  11096. {
  11097. error_at (input_location, "scoped/unscoped mismatch "
  11098. "in enum %q#T", enumtype);
  11099. error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
  11100. "previous definition here");
  11101. enumtype = error_mark_node;
  11102. }
  11103. else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
  11104. {
  11105. error_at (input_location, "underlying type mismatch "
  11106. "in enum %q#T", enumtype);
  11107. error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
  11108. "previous definition here");
  11109. enumtype = error_mark_node;
  11110. }
  11111. else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
  11112. && !dependent_type_p (underlying_type)
  11113. && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
  11114. && !same_type_p (underlying_type,
  11115. ENUM_UNDERLYING_TYPE (enumtype)))
  11116. {
  11117. error_at (input_location, "different underlying type "
  11118. "in enum %q#T", enumtype);
  11119. error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
  11120. "previous definition here");
  11121. underlying_type = NULL_TREE;
  11122. }
  11123. }
  11124. if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
  11125. || processing_template_decl)
  11126. {
  11127. /* In case of error, make a dummy enum to allow parsing to
  11128. continue. */
  11129. if (enumtype == error_mark_node)
  11130. {
  11131. name = make_anon_name ();
  11132. enumtype = NULL_TREE;
  11133. }
  11134. /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
  11135. of an opaque enum, or an opaque enum of an already defined
  11136. enumeration (C++0x only).
  11137. In any other case, it'll be NULL_TREE. */
  11138. if (!enumtype)
  11139. {
  11140. if (is_new)
  11141. *is_new = true;
  11142. }
  11143. prevtype = enumtype;
  11144. /* Do not push the decl more than once, unless we need to
  11145. compare underlying types at instantiation time */
  11146. if (!enumtype
  11147. || TREE_CODE (enumtype) != ENUMERAL_TYPE
  11148. || (underlying_type
  11149. && dependent_type_p (underlying_type))
  11150. || (ENUM_UNDERLYING_TYPE (enumtype)
  11151. && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
  11152. {
  11153. enumtype = cxx_make_type (ENUMERAL_TYPE);
  11154. enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
  11155. }
  11156. else
  11157. enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
  11158. false);
  11159. if (enumtype == error_mark_node)
  11160. return error_mark_node;
  11161. /* The enum is considered opaque until the opening '{' of the
  11162. enumerator list. */
  11163. SET_OPAQUE_ENUM_P (enumtype, true);
  11164. ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
  11165. }
  11166. SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
  11167. if (underlying_type)
  11168. {
  11169. if (CP_INTEGRAL_TYPE_P (underlying_type))
  11170. {
  11171. copy_type_enum (enumtype, underlying_type);
  11172. ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
  11173. }
  11174. else if (dependent_type_p (underlying_type))
  11175. ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
  11176. else
  11177. error ("underlying type %<%T%> of %<%T%> must be an integral type",
  11178. underlying_type, enumtype);
  11179. }
  11180. /* If into a template class, the returned enum is always the first
  11181. declaration (opaque or not) seen. This way all the references to
  11182. this type will be to the same declaration. The following ones are used
  11183. only to check for definition errors. */
  11184. if (prevtype && processing_template_decl)
  11185. return prevtype;
  11186. else
  11187. return enumtype;
  11188. }
  11189. /* After processing and defining all the values of an enumeration type,
  11190. install their decls in the enumeration type.
  11191. ENUMTYPE is the type object. */
  11192. void
  11193. finish_enum_value_list (tree enumtype)
  11194. {
  11195. tree values;
  11196. tree underlying_type;
  11197. tree decl;
  11198. tree value;
  11199. tree minnode, maxnode;
  11200. tree t;
  11201. bool fixed_underlying_type_p
  11202. = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
  11203. /* We built up the VALUES in reverse order. */
  11204. TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
  11205. /* For an enum defined in a template, just set the type of the values;
  11206. all further processing is postponed until the template is
  11207. instantiated. We need to set the type so that tsubst of a CONST_DECL
  11208. works. */
  11209. if (processing_template_decl)
  11210. {
  11211. for (values = TYPE_VALUES (enumtype);
  11212. values;
  11213. values = TREE_CHAIN (values))
  11214. TREE_TYPE (TREE_VALUE (values)) = enumtype;
  11215. return;
  11216. }
  11217. /* Determine the minimum and maximum values of the enumerators. */
  11218. if (TYPE_VALUES (enumtype))
  11219. {
  11220. minnode = maxnode = NULL_TREE;
  11221. for (values = TYPE_VALUES (enumtype);
  11222. values;
  11223. values = TREE_CHAIN (values))
  11224. {
  11225. decl = TREE_VALUE (values);
  11226. /* [dcl.enum]: Following the closing brace of an enum-specifier,
  11227. each enumerator has the type of its enumeration. Prior to the
  11228. closing brace, the type of each enumerator is the type of its
  11229. initializing value. */
  11230. TREE_TYPE (decl) = enumtype;
  11231. /* Update the minimum and maximum values, if appropriate. */
  11232. value = DECL_INITIAL (decl);
  11233. if (value == error_mark_node)
  11234. value = integer_zero_node;
  11235. /* Figure out what the minimum and maximum values of the
  11236. enumerators are. */
  11237. if (!minnode)
  11238. minnode = maxnode = value;
  11239. else if (tree_int_cst_lt (maxnode, value))
  11240. maxnode = value;
  11241. else if (tree_int_cst_lt (value, minnode))
  11242. minnode = value;
  11243. }
  11244. }
  11245. else
  11246. /* [dcl.enum]
  11247. If the enumerator-list is empty, the underlying type is as if
  11248. the enumeration had a single enumerator with value 0. */
  11249. minnode = maxnode = integer_zero_node;
  11250. if (!fixed_underlying_type_p)
  11251. {
  11252. /* Compute the number of bits require to represent all values of the
  11253. enumeration. We must do this before the type of MINNODE and
  11254. MAXNODE are transformed, since tree_int_cst_min_precision relies
  11255. on the TREE_TYPE of the value it is passed. */
  11256. signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
  11257. int lowprec = tree_int_cst_min_precision (minnode, sgn);
  11258. int highprec = tree_int_cst_min_precision (maxnode, sgn);
  11259. int precision = MAX (lowprec, highprec);
  11260. unsigned int itk;
  11261. bool use_short_enum;
  11262. /* Determine the underlying type of the enumeration.
  11263. [dcl.enum]
  11264. The underlying type of an enumeration is an integral type that
  11265. can represent all the enumerator values defined in the
  11266. enumeration. It is implementation-defined which integral type is
  11267. used as the underlying type for an enumeration except that the
  11268. underlying type shall not be larger than int unless the value of
  11269. an enumerator cannot fit in an int or unsigned int.
  11270. We use "int" or an "unsigned int" as the underlying type, even if
  11271. a smaller integral type would work, unless the user has
  11272. explicitly requested that we use the smallest possible type. The
  11273. user can request that for all enumerations with a command line
  11274. flag, or for just one enumeration with an attribute. */
  11275. use_short_enum = flag_short_enums
  11276. || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
  11277. for (itk = (use_short_enum ? itk_char : itk_int);
  11278. itk != itk_none;
  11279. itk++)
  11280. {
  11281. underlying_type = integer_types[itk];
  11282. if (underlying_type != NULL_TREE
  11283. && TYPE_PRECISION (underlying_type) >= precision
  11284. && TYPE_SIGN (underlying_type) == sgn)
  11285. break;
  11286. }
  11287. if (itk == itk_none)
  11288. {
  11289. /* DR 377
  11290. IF no integral type can represent all the enumerator values, the
  11291. enumeration is ill-formed. */
  11292. error ("no integral type can represent all of the enumerator values "
  11293. "for %qT", enumtype);
  11294. precision = TYPE_PRECISION (long_long_integer_type_node);
  11295. underlying_type = integer_types[itk_unsigned_long_long];
  11296. }
  11297. /* [dcl.enum]
  11298. The value of sizeof() applied to an enumeration type, an object
  11299. of an enumeration type, or an enumerator, is the value of sizeof()
  11300. applied to the underlying type. */
  11301. copy_type_enum (enumtype, underlying_type);
  11302. /* Compute the minimum and maximum values for the type.
  11303. [dcl.enum]
  11304. For an enumeration where emin is the smallest enumerator and emax
  11305. is the largest, the values of the enumeration are the values of the
  11306. underlying type in the range bmin to bmax, where bmin and bmax are,
  11307. respectively, the smallest and largest values of the smallest bit-
  11308. field that can store emin and emax. */
  11309. /* The middle-end currently assumes that types with TYPE_PRECISION
  11310. narrower than their underlying type are suitably zero or sign
  11311. extended to fill their mode. Similarly, it assumes that the front
  11312. end assures that a value of a particular type must be within
  11313. TYPE_MIN_VALUE and TYPE_MAX_VALUE.
  11314. We used to set these fields based on bmin and bmax, but that led
  11315. to invalid assumptions like optimizing away bounds checking. So
  11316. now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
  11317. TYPE_MAX_VALUE to the values for the mode above and only restrict
  11318. the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
  11319. ENUM_UNDERLYING_TYPE (enumtype)
  11320. = build_distinct_type_copy (underlying_type);
  11321. TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
  11322. set_min_and_max_values_for_integral_type
  11323. (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
  11324. /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
  11325. if (flag_strict_enums)
  11326. set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
  11327. }
  11328. else
  11329. underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
  11330. /* Convert each of the enumerators to the type of the underlying
  11331. type of the enumeration. */
  11332. for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
  11333. {
  11334. location_t saved_location;
  11335. decl = TREE_VALUE (values);
  11336. saved_location = input_location;
  11337. input_location = DECL_SOURCE_LOCATION (decl);
  11338. if (fixed_underlying_type_p)
  11339. /* If the enumeration type has a fixed underlying type, we
  11340. already checked all of the enumerator values. */
  11341. value = DECL_INITIAL (decl);
  11342. else
  11343. value = perform_implicit_conversion (underlying_type,
  11344. DECL_INITIAL (decl),
  11345. tf_warning_or_error);
  11346. input_location = saved_location;
  11347. /* Do not clobber shared ints. */
  11348. value = copy_node (value);
  11349. TREE_TYPE (value) = enumtype;
  11350. DECL_INITIAL (decl) = value;
  11351. }
  11352. /* Fix up all variant types of this enum type. */
  11353. for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
  11354. TYPE_VALUES (t) = TYPE_VALUES (enumtype);
  11355. if (at_class_scope_p ()
  11356. && COMPLETE_TYPE_P (current_class_type)
  11357. && UNSCOPED_ENUM_P (enumtype))
  11358. insert_late_enum_def_into_classtype_sorted_fields (enumtype,
  11359. current_class_type);
  11360. /* Finish debugging output for this type. */
  11361. rest_of_type_compilation (enumtype, namespace_bindings_p ());
  11362. }
  11363. /* Finishes the enum type. This is called only the first time an
  11364. enumeration is seen, be it opaque or odinary.
  11365. ENUMTYPE is the type object. */
  11366. void
  11367. finish_enum (tree enumtype)
  11368. {
  11369. if (processing_template_decl)
  11370. {
  11371. if (at_function_scope_p ())
  11372. add_stmt (build_min (TAG_DEFN, enumtype));
  11373. return;
  11374. }
  11375. /* If this is a forward declaration, there should not be any variants,
  11376. though we can get a variant in the middle of an enum-specifier with
  11377. wacky code like 'enum E { e = sizeof(const E*) };' */
  11378. gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
  11379. && (TYPE_VALUES (enumtype)
  11380. || !TYPE_NEXT_VARIANT (enumtype)));
  11381. }
  11382. /* Build and install a CONST_DECL for an enumeration constant of the
  11383. enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
  11384. LOC is the location of NAME.
  11385. Assignment of sequential values by default is handled here. */
  11386. void
  11387. build_enumerator (tree name, tree value, tree enumtype, location_t loc)
  11388. {
  11389. tree decl;
  11390. tree context;
  11391. tree type;
  11392. /* scalar_constant_value will pull out this expression, so make sure
  11393. it's folded as appropriate. */
  11394. if (processing_template_decl)
  11395. value = fold_non_dependent_expr (value);
  11396. /* If the VALUE was erroneous, pretend it wasn't there; that will
  11397. result in the enum being assigned the next value in sequence. */
  11398. if (value == error_mark_node)
  11399. value = NULL_TREE;
  11400. /* Remove no-op casts from the value. */
  11401. if (value)
  11402. STRIP_TYPE_NOPS (value);
  11403. if (! processing_template_decl)
  11404. {
  11405. /* Validate and default VALUE. */
  11406. if (value != NULL_TREE)
  11407. {
  11408. if (!ENUM_UNDERLYING_TYPE (enumtype))
  11409. {
  11410. tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
  11411. value, true);
  11412. if (tmp_value)
  11413. value = tmp_value;
  11414. }
  11415. else if (! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
  11416. value = perform_implicit_conversion_flags
  11417. (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
  11418. LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
  11419. if (value == error_mark_node)
  11420. value = NULL_TREE;
  11421. if (value != NULL_TREE)
  11422. {
  11423. value = cxx_constant_value (value);
  11424. if (TREE_CODE (value) != INTEGER_CST
  11425. || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
  11426. {
  11427. error ("enumerator value for %qD is not an integer constant",
  11428. name);
  11429. value = NULL_TREE;
  11430. }
  11431. }
  11432. }
  11433. /* Default based on previous value. */
  11434. if (value == NULL_TREE)
  11435. {
  11436. if (TYPE_VALUES (enumtype))
  11437. {
  11438. tree prev_value;
  11439. bool overflowed;
  11440. /* C++03 7.2/4: If no initializer is specified for the first
  11441. enumerator, the type is an unspecified integral
  11442. type. Otherwise the type is the same as the type of the
  11443. initializing value of the preceding enumerator unless the
  11444. incremented value is not representable in that type, in
  11445. which case the type is an unspecified integral type
  11446. sufficient to contain the incremented value. */
  11447. prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
  11448. if (error_operand_p (prev_value))
  11449. value = error_mark_node;
  11450. else
  11451. {
  11452. tree type = TREE_TYPE (prev_value);
  11453. signop sgn = TYPE_SIGN (type);
  11454. widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
  11455. &overflowed);
  11456. if (!overflowed)
  11457. {
  11458. bool pos = !wi::neg_p (wi, sgn);
  11459. if (!wi::fits_to_tree_p (wi, type))
  11460. {
  11461. unsigned int itk;
  11462. for (itk = itk_int; itk != itk_none; itk++)
  11463. {
  11464. type = integer_types[itk];
  11465. if (type != NULL_TREE
  11466. && (pos || !TYPE_UNSIGNED (type))
  11467. && wi::fits_to_tree_p (wi, type))
  11468. break;
  11469. }
  11470. if (type && cxx_dialect < cxx11
  11471. && itk > itk_unsigned_long)
  11472. pedwarn (input_location, OPT_Wlong_long, pos ? "\
  11473. incremented enumerator value is too large for %<unsigned long%>" : "\
  11474. incremented enumerator value is too large for %<long%>");
  11475. }
  11476. if (type == NULL_TREE)
  11477. overflowed = true;
  11478. else
  11479. value = wide_int_to_tree (type, wi);
  11480. }
  11481. if (overflowed)
  11482. {
  11483. error ("overflow in enumeration values at %qD", name);
  11484. value = error_mark_node;
  11485. }
  11486. }
  11487. }
  11488. else
  11489. value = integer_zero_node;
  11490. }
  11491. /* Remove no-op casts from the value. */
  11492. STRIP_TYPE_NOPS (value);
  11493. /* If the underlying type of the enum is fixed, check whether
  11494. the enumerator values fits in the underlying type. If it
  11495. does not fit, the program is ill-formed [C++0x dcl.enum]. */
  11496. if (ENUM_UNDERLYING_TYPE (enumtype)
  11497. && value
  11498. && TREE_CODE (value) == INTEGER_CST)
  11499. {
  11500. if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
  11501. error ("enumerator value %E is outside the range of underlying "
  11502. "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
  11503. /* Convert the value to the appropriate type. */
  11504. value = convert (ENUM_UNDERLYING_TYPE (enumtype), value);
  11505. }
  11506. }
  11507. /* C++ associates enums with global, function, or class declarations. */
  11508. context = current_scope ();
  11509. /* Build the actual enumeration constant. Note that the enumeration
  11510. constants have the underlying type of the enum (if it is fixed)
  11511. or the type of their initializer (if the underlying type of the
  11512. enum is not fixed):
  11513. [ C++0x dcl.enum ]
  11514. If the underlying type is fixed, the type of each enumerator
  11515. prior to the closing brace is the underlying type; if the
  11516. initializing value of an enumerator cannot be represented by
  11517. the underlying type, the program is ill-formed. If the
  11518. underlying type is not fixed, the type of each enumerator is
  11519. the type of its initializing value.
  11520. If the underlying type is not fixed, it will be computed by
  11521. finish_enum and we will reset the type of this enumerator. Of
  11522. course, if we're processing a template, there may be no value. */
  11523. type = value ? TREE_TYPE (value) : NULL_TREE;
  11524. decl = build_decl (loc, CONST_DECL, name, type);
  11525. DECL_CONTEXT (decl) = enumtype;
  11526. TREE_CONSTANT (decl) = 1;
  11527. TREE_READONLY (decl) = 1;
  11528. DECL_INITIAL (decl) = value;
  11529. if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
  11530. /* In something like `struct S { enum E { i = 7 }; };' we put `i'
  11531. on the TYPE_FIELDS list for `S'. (That's so that you can say
  11532. things like `S::i' later.) */
  11533. finish_member_declaration (decl);
  11534. else
  11535. pushdecl (decl);
  11536. /* Add this enumeration constant to the list for this type. */
  11537. TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
  11538. }
  11539. /* Look for an enumerator with the given NAME within the enumeration
  11540. type ENUMTYPE. This routine is used primarily for qualified name
  11541. lookup into an enumerator in C++0x, e.g.,
  11542. enum class Color { Red, Green, Blue };
  11543. Color color = Color::Red;
  11544. Returns the value corresponding to the enumerator, or
  11545. NULL_TREE if no such enumerator was found. */
  11546. tree
  11547. lookup_enumerator (tree enumtype, tree name)
  11548. {
  11549. tree e;
  11550. gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
  11551. e = purpose_member (name, TYPE_VALUES (enumtype));
  11552. return e? TREE_VALUE (e) : NULL_TREE;
  11553. }
  11554. /* We're defining DECL. Make sure that its type is OK. */
  11555. static void
  11556. check_function_type (tree decl, tree current_function_parms)
  11557. {
  11558. tree fntype = TREE_TYPE (decl);
  11559. tree return_type = complete_type (TREE_TYPE (fntype));
  11560. /* In a function definition, arg types must be complete. */
  11561. require_complete_types_for_parms (current_function_parms);
  11562. if (dependent_type_p (return_type)
  11563. || type_uses_auto (return_type))
  11564. return;
  11565. if (!COMPLETE_OR_VOID_TYPE_P (return_type)
  11566. || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
  11567. {
  11568. tree args = TYPE_ARG_TYPES (fntype);
  11569. if (!COMPLETE_OR_VOID_TYPE_P (return_type))
  11570. error ("return type %q#T is incomplete", return_type);
  11571. else
  11572. error ("return type has Java class type %q#T", return_type);
  11573. /* Make it return void instead. */
  11574. if (TREE_CODE (fntype) == METHOD_TYPE)
  11575. fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
  11576. void_type_node,
  11577. TREE_CHAIN (args));
  11578. else
  11579. fntype = build_function_type (void_type_node, args);
  11580. fntype
  11581. = build_exception_variant (fntype,
  11582. TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
  11583. fntype = (cp_build_type_attribute_variant
  11584. (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
  11585. TREE_TYPE (decl) = fntype;
  11586. }
  11587. else
  11588. abstract_virtuals_error (decl, TREE_TYPE (fntype));
  11589. }
  11590. /* Create the FUNCTION_DECL for a function definition.
  11591. DECLSPECS and DECLARATOR are the parts of the declaration;
  11592. they describe the function's name and the type it returns,
  11593. but twisted together in a fashion that parallels the syntax of C.
  11594. FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
  11595. DECLARATOR is really the DECL for the function we are about to
  11596. process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
  11597. indicating that the function is an inline defined in-class.
  11598. This function creates a binding context for the function body
  11599. as well as setting up the FUNCTION_DECL in current_function_decl.
  11600. For C++, we must first check whether that datum makes any sense.
  11601. For example, "class A local_a(1,2);" means that variable local_a
  11602. is an aggregate of type A, which should have a constructor
  11603. applied to it with the argument list [1, 2].
  11604. On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
  11605. or may be a BLOCK if the function has been defined previously
  11606. in this translation unit. On exit, DECL_INITIAL (decl1) will be
  11607. error_mark_node if the function has never been defined, or
  11608. a BLOCK if the function has been defined somewhere. */
  11609. bool
  11610. start_preparsed_function (tree decl1, tree attrs, int flags)
  11611. {
  11612. tree ctype = NULL_TREE;
  11613. tree fntype;
  11614. tree restype;
  11615. int doing_friend = 0;
  11616. cp_binding_level *bl;
  11617. tree current_function_parms;
  11618. struct c_fileinfo *finfo
  11619. = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
  11620. bool honor_interface;
  11621. /* Sanity check. */
  11622. gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
  11623. gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
  11624. fntype = TREE_TYPE (decl1);
  11625. if (TREE_CODE (fntype) == METHOD_TYPE)
  11626. ctype = TYPE_METHOD_BASETYPE (fntype);
  11627. /* ISO C++ 11.4/5. A friend function defined in a class is in
  11628. the (lexical) scope of the class in which it is defined. */
  11629. if (!ctype && DECL_FRIEND_P (decl1))
  11630. {
  11631. ctype = DECL_FRIEND_CONTEXT (decl1);
  11632. /* CTYPE could be null here if we're dealing with a template;
  11633. for example, `inline friend float foo()' inside a template
  11634. will have no CTYPE set. */
  11635. if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
  11636. ctype = NULL_TREE;
  11637. else
  11638. doing_friend = 1;
  11639. }
  11640. if (DECL_DECLARED_INLINE_P (decl1)
  11641. && lookup_attribute ("noinline", attrs))
  11642. warning (0, "inline function %q+D given attribute noinline", decl1);
  11643. /* Handle gnu_inline attribute. */
  11644. if (GNU_INLINE_P (decl1))
  11645. {
  11646. DECL_EXTERNAL (decl1) = 1;
  11647. DECL_NOT_REALLY_EXTERN (decl1) = 0;
  11648. DECL_INTERFACE_KNOWN (decl1) = 1;
  11649. DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
  11650. }
  11651. if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
  11652. /* This is a constructor, we must ensure that any default args
  11653. introduced by this definition are propagated to the clones
  11654. now. The clones are used directly in overload resolution. */
  11655. adjust_clone_args (decl1);
  11656. /* Sometimes we don't notice that a function is a static member, and
  11657. build a METHOD_TYPE for it. Fix that up now. */
  11658. gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
  11659. && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
  11660. /* Set up current_class_type, and enter the scope of the class, if
  11661. appropriate. */
  11662. if (ctype)
  11663. push_nested_class (ctype);
  11664. else if (DECL_STATIC_FUNCTION_P (decl1))
  11665. push_nested_class (DECL_CONTEXT (decl1));
  11666. /* Now that we have entered the scope of the class, we must restore
  11667. the bindings for any template parameters surrounding DECL1, if it
  11668. is an inline member template. (Order is important; consider the
  11669. case where a template parameter has the same name as a field of
  11670. the class.) It is not until after this point that
  11671. PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
  11672. if (flags & SF_INCLASS_INLINE)
  11673. maybe_begin_member_template_processing (decl1);
  11674. /* Effective C++ rule 15. */
  11675. if (warn_ecpp
  11676. && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
  11677. && VOID_TYPE_P (TREE_TYPE (fntype)))
  11678. warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
  11679. /* Make the init_value nonzero so pushdecl knows this is not tentative.
  11680. error_mark_node is replaced below (in poplevel) with the BLOCK. */
  11681. if (!DECL_INITIAL (decl1))
  11682. DECL_INITIAL (decl1) = error_mark_node;
  11683. /* This function exists in static storage.
  11684. (This does not mean `static' in the C sense!) */
  11685. TREE_STATIC (decl1) = 1;
  11686. /* We must call push_template_decl after current_class_type is set
  11687. up. (If we are processing inline definitions after exiting a
  11688. class scope, current_class_type will be NULL_TREE until set above
  11689. by push_nested_class.) */
  11690. if (processing_template_decl)
  11691. {
  11692. tree newdecl1 = push_template_decl (decl1);
  11693. if (newdecl1 == error_mark_node)
  11694. {
  11695. if (ctype || DECL_STATIC_FUNCTION_P (decl1))
  11696. pop_nested_class ();
  11697. return false;
  11698. }
  11699. decl1 = newdecl1;
  11700. }
  11701. /* We are now in the scope of the function being defined. */
  11702. current_function_decl = decl1;
  11703. /* Save the parm names or decls from this function's declarator
  11704. where store_parm_decls will find them. */
  11705. current_function_parms = DECL_ARGUMENTS (decl1);
  11706. /* Make sure the parameter and return types are reasonable. When
  11707. you declare a function, these types can be incomplete, but they
  11708. must be complete when you define the function. */
  11709. check_function_type (decl1, current_function_parms);
  11710. /* Build the return declaration for the function. */
  11711. restype = TREE_TYPE (fntype);
  11712. if (DECL_RESULT (decl1) == NULL_TREE)
  11713. {
  11714. tree resdecl;
  11715. resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
  11716. DECL_ARTIFICIAL (resdecl) = 1;
  11717. DECL_IGNORED_P (resdecl) = 1;
  11718. DECL_RESULT (decl1) = resdecl;
  11719. cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
  11720. }
  11721. /* Let the user know we're compiling this function. */
  11722. announce_function (decl1);
  11723. /* Record the decl so that the function name is defined.
  11724. If we already have a decl for this name, and it is a FUNCTION_DECL,
  11725. use the old decl. */
  11726. if (!processing_template_decl && !(flags & SF_PRE_PARSED))
  11727. {
  11728. /* A specialization is not used to guide overload resolution. */
  11729. if (!DECL_FUNCTION_MEMBER_P (decl1)
  11730. && !(DECL_USE_TEMPLATE (decl1) &&
  11731. PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
  11732. {
  11733. tree olddecl = pushdecl (decl1);
  11734. if (olddecl == error_mark_node)
  11735. /* If something went wrong when registering the declaration,
  11736. use DECL1; we have to have a FUNCTION_DECL to use when
  11737. parsing the body of the function. */
  11738. ;
  11739. else
  11740. {
  11741. /* Otherwise, OLDDECL is either a previous declaration
  11742. of the same function or DECL1 itself. */
  11743. if (warn_missing_declarations
  11744. && olddecl == decl1
  11745. && !DECL_MAIN_P (decl1)
  11746. && TREE_PUBLIC (decl1)
  11747. && !DECL_DECLARED_INLINE_P (decl1))
  11748. {
  11749. tree context;
  11750. /* Check whether DECL1 is in an anonymous
  11751. namespace. */
  11752. for (context = DECL_CONTEXT (decl1);
  11753. context;
  11754. context = DECL_CONTEXT (context))
  11755. {
  11756. if (TREE_CODE (context) == NAMESPACE_DECL
  11757. && DECL_NAME (context) == NULL_TREE)
  11758. break;
  11759. }
  11760. if (context == NULL)
  11761. warning (OPT_Wmissing_declarations,
  11762. "no previous declaration for %q+D", decl1);
  11763. }
  11764. decl1 = olddecl;
  11765. }
  11766. }
  11767. else
  11768. {
  11769. /* We need to set the DECL_CONTEXT. */
  11770. if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
  11771. DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
  11772. }
  11773. fntype = TREE_TYPE (decl1);
  11774. restype = TREE_TYPE (fntype);
  11775. /* If #pragma weak applies, mark the decl appropriately now.
  11776. The pragma only applies to global functions. Because
  11777. determining whether or not the #pragma applies involves
  11778. computing the mangled name for the declaration, we cannot
  11779. apply the pragma until after we have merged this declaration
  11780. with any previous declarations; if the original declaration
  11781. has a linkage specification, that specification applies to
  11782. the definition as well, and may affect the mangled name. */
  11783. if (DECL_FILE_SCOPE_P (decl1))
  11784. maybe_apply_pragma_weak (decl1);
  11785. }
  11786. /* Reset this in case the call to pushdecl changed it. */
  11787. current_function_decl = decl1;
  11788. gcc_assert (DECL_INITIAL (decl1));
  11789. /* This function may already have been parsed, in which case just
  11790. return; our caller will skip over the body without parsing. */
  11791. if (DECL_INITIAL (decl1) != error_mark_node)
  11792. return true;
  11793. /* Initialize RTL machinery. We cannot do this until
  11794. CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
  11795. even when processing a template; this is how we get
  11796. CFUN set up, and our per-function variables initialized.
  11797. FIXME factor out the non-RTL stuff. */
  11798. bl = current_binding_level;
  11799. allocate_struct_function (decl1, processing_template_decl);
  11800. /* Initialize the language data structures. Whenever we start
  11801. a new function, we destroy temporaries in the usual way. */
  11802. cfun->language = ggc_cleared_alloc<language_function> ();
  11803. current_stmt_tree ()->stmts_are_full_exprs_p = 1;
  11804. current_binding_level = bl;
  11805. if (!processing_template_decl && type_uses_auto (restype))
  11806. {
  11807. FNDECL_USED_AUTO (decl1) = true;
  11808. current_function_auto_return_pattern = restype;
  11809. }
  11810. /* Start the statement-tree, start the tree now. */
  11811. DECL_SAVED_TREE (decl1) = push_stmt_list ();
  11812. /* If we are (erroneously) defining a function that we have already
  11813. defined before, wipe out what we knew before. */
  11814. if (!DECL_PENDING_INLINE_P (decl1))
  11815. DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
  11816. if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
  11817. {
  11818. /* We know that this was set up by `grokclassfn'. We do not
  11819. wait until `store_parm_decls', since evil parse errors may
  11820. never get us to that point. Here we keep the consistency
  11821. between `current_class_type' and `current_class_ptr'. */
  11822. tree t = DECL_ARGUMENTS (decl1);
  11823. gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
  11824. gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
  11825. cp_function_chain->x_current_class_ref
  11826. = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
  11827. /* Set this second to avoid shortcut in cp_build_indirect_ref. */
  11828. cp_function_chain->x_current_class_ptr = t;
  11829. /* Constructors and destructors need to know whether they're "in
  11830. charge" of initializing virtual base classes. */
  11831. t = DECL_CHAIN (t);
  11832. if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
  11833. {
  11834. current_in_charge_parm = t;
  11835. t = DECL_CHAIN (t);
  11836. }
  11837. if (DECL_HAS_VTT_PARM_P (decl1))
  11838. {
  11839. gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
  11840. current_vtt_parm = t;
  11841. }
  11842. }
  11843. honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
  11844. /* Implicitly-defined methods (like the
  11845. destructor for a class in which no destructor
  11846. is explicitly declared) must not be defined
  11847. until their definition is needed. So, we
  11848. ignore interface specifications for
  11849. compiler-generated functions. */
  11850. && !DECL_ARTIFICIAL (decl1));
  11851. if (processing_template_decl)
  11852. /* Don't mess with interface flags. */;
  11853. else if (DECL_INTERFACE_KNOWN (decl1))
  11854. {
  11855. tree ctx = decl_function_context (decl1);
  11856. if (DECL_NOT_REALLY_EXTERN (decl1))
  11857. DECL_EXTERNAL (decl1) = 0;
  11858. if (ctx != NULL_TREE && vague_linkage_p (ctx))
  11859. /* This is a function in a local class in an extern inline
  11860. or template function. */
  11861. comdat_linkage (decl1);
  11862. }
  11863. /* If this function belongs to an interface, it is public.
  11864. If it belongs to someone else's interface, it is also external.
  11865. This only affects inlines and template instantiations. */
  11866. else if (!finfo->interface_unknown && honor_interface)
  11867. {
  11868. if (DECL_DECLARED_INLINE_P (decl1)
  11869. || DECL_TEMPLATE_INSTANTIATION (decl1))
  11870. {
  11871. DECL_EXTERNAL (decl1)
  11872. = (finfo->interface_only
  11873. || (DECL_DECLARED_INLINE_P (decl1)
  11874. && ! flag_implement_inlines
  11875. && !DECL_VINDEX (decl1)));
  11876. /* For WIN32 we also want to put these in linkonce sections. */
  11877. maybe_make_one_only (decl1);
  11878. }
  11879. else
  11880. DECL_EXTERNAL (decl1) = 0;
  11881. DECL_INTERFACE_KNOWN (decl1) = 1;
  11882. /* If this function is in an interface implemented in this file,
  11883. make sure that the back end knows to emit this function
  11884. here. */
  11885. if (!DECL_EXTERNAL (decl1))
  11886. mark_needed (decl1);
  11887. }
  11888. else if (finfo->interface_unknown && finfo->interface_only
  11889. && honor_interface)
  11890. {
  11891. /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
  11892. interface, we will have both finfo->interface_unknown and
  11893. finfo->interface_only set. In that case, we don't want to
  11894. use the normal heuristics because someone will supply a
  11895. #pragma implementation elsewhere, and deducing it here would
  11896. produce a conflict. */
  11897. comdat_linkage (decl1);
  11898. DECL_EXTERNAL (decl1) = 0;
  11899. DECL_INTERFACE_KNOWN (decl1) = 1;
  11900. DECL_DEFER_OUTPUT (decl1) = 1;
  11901. }
  11902. else
  11903. {
  11904. /* This is a definition, not a reference.
  11905. So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
  11906. if (!GNU_INLINE_P (decl1))
  11907. DECL_EXTERNAL (decl1) = 0;
  11908. if ((DECL_DECLARED_INLINE_P (decl1)
  11909. || DECL_TEMPLATE_INSTANTIATION (decl1))
  11910. && ! DECL_INTERFACE_KNOWN (decl1))
  11911. DECL_DEFER_OUTPUT (decl1) = 1;
  11912. else
  11913. DECL_INTERFACE_KNOWN (decl1) = 1;
  11914. }
  11915. /* Determine the ELF visibility attribute for the function. We must not
  11916. do this before calling "pushdecl", as we must allow "duplicate_decls"
  11917. to merge any attributes appropriately. We also need to wait until
  11918. linkage is set. */
  11919. if (!DECL_CLONED_FUNCTION_P (decl1))
  11920. determine_visibility (decl1);
  11921. if (!processing_template_decl)
  11922. maybe_instantiate_noexcept (decl1);
  11923. begin_scope (sk_function_parms, decl1);
  11924. ++function_depth;
  11925. if (DECL_DESTRUCTOR_P (decl1)
  11926. || (DECL_CONSTRUCTOR_P (decl1)
  11927. && targetm.cxx.cdtor_returns_this ()))
  11928. {
  11929. cdtor_label = create_artificial_label (input_location);
  11930. }
  11931. start_fname_decls ();
  11932. store_parm_decls (current_function_parms);
  11933. return true;
  11934. }
  11935. /* Like start_preparsed_function, except that instead of a
  11936. FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
  11937. Returns true on success. If the DECLARATOR is not suitable
  11938. for a function, we return false, which tells the parser to
  11939. skip the entire function. */
  11940. bool
  11941. start_function (cp_decl_specifier_seq *declspecs,
  11942. const cp_declarator *declarator,
  11943. tree attrs)
  11944. {
  11945. tree decl1;
  11946. decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
  11947. if (decl1 == error_mark_node)
  11948. return false;
  11949. /* If the declarator is not suitable for a function definition,
  11950. cause a syntax error. */
  11951. if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
  11952. {
  11953. error ("invalid function declaration");
  11954. return false;
  11955. }
  11956. if (DECL_MAIN_P (decl1))
  11957. /* main must return int. grokfndecl should have corrected it
  11958. (and issued a diagnostic) if the user got it wrong. */
  11959. gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
  11960. integer_type_node));
  11961. return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
  11962. }
  11963. /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
  11964. FN. */
  11965. static bool
  11966. use_eh_spec_block (tree fn)
  11967. {
  11968. return (flag_exceptions && flag_enforce_eh_specs
  11969. && !processing_template_decl
  11970. && !type_throw_all_p (TREE_TYPE (fn))
  11971. /* We insert the EH_SPEC_BLOCK only in the original
  11972. function; then, it is copied automatically to the
  11973. clones. */
  11974. && !DECL_CLONED_FUNCTION_P (fn)
  11975. /* Implicitly-generated constructors and destructors have
  11976. exception specifications. However, those specifications
  11977. are the union of the possible exceptions specified by the
  11978. constructors/destructors for bases and members, so no
  11979. unallowed exception will ever reach this function. By
  11980. not creating the EH_SPEC_BLOCK we save a little memory,
  11981. and we avoid spurious warnings about unreachable
  11982. code. */
  11983. && !DECL_DEFAULTED_FN (fn));
  11984. }
  11985. /* Store the parameter declarations into the current function declaration.
  11986. This is called after parsing the parameter declarations, before
  11987. digesting the body of the function.
  11988. Also install to binding contour return value identifier, if any. */
  11989. static void
  11990. store_parm_decls (tree current_function_parms)
  11991. {
  11992. tree fndecl = current_function_decl;
  11993. tree parm;
  11994. /* This is a chain of any other decls that came in among the parm
  11995. declarations. If a parm is declared with enum {foo, bar} x;
  11996. then CONST_DECLs for foo and bar are put here. */
  11997. tree nonparms = NULL_TREE;
  11998. if (current_function_parms)
  11999. {
  12000. /* This case is when the function was defined with an ANSI prototype.
  12001. The parms already have decls, so we need not do anything here
  12002. except record them as in effect
  12003. and complain if any redundant old-style parm decls were written. */
  12004. tree specparms = current_function_parms;
  12005. tree next;
  12006. /* Must clear this because it might contain TYPE_DECLs declared
  12007. at class level. */
  12008. current_binding_level->names = NULL;
  12009. /* If we're doing semantic analysis, then we'll call pushdecl
  12010. for each of these. We must do them in reverse order so that
  12011. they end in the correct forward order. */
  12012. specparms = nreverse (specparms);
  12013. for (parm = specparms; parm; parm = next)
  12014. {
  12015. next = DECL_CHAIN (parm);
  12016. if (TREE_CODE (parm) == PARM_DECL)
  12017. {
  12018. if (DECL_NAME (parm) == NULL_TREE
  12019. || !VOID_TYPE_P (parm))
  12020. pushdecl (parm);
  12021. else
  12022. error ("parameter %qD declared void", parm);
  12023. }
  12024. else
  12025. {
  12026. /* If we find an enum constant or a type tag,
  12027. put it aside for the moment. */
  12028. TREE_CHAIN (parm) = NULL_TREE;
  12029. nonparms = chainon (nonparms, parm);
  12030. }
  12031. }
  12032. /* Get the decls in their original chain order and record in the
  12033. function. This is all and only the PARM_DECLs that were
  12034. pushed into scope by the loop above. */
  12035. DECL_ARGUMENTS (fndecl) = getdecls ();
  12036. }
  12037. else
  12038. DECL_ARGUMENTS (fndecl) = NULL_TREE;
  12039. /* Now store the final chain of decls for the arguments
  12040. as the decl-chain of the current lexical scope.
  12041. Put the enumerators in as well, at the front so that
  12042. DECL_ARGUMENTS is not modified. */
  12043. current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
  12044. if (use_eh_spec_block (current_function_decl))
  12045. current_eh_spec_block = begin_eh_spec_block ();
  12046. }
  12047. /* We have finished doing semantic analysis on DECL, but have not yet
  12048. generated RTL for its body. Save away our current state, so that
  12049. when we want to generate RTL later we know what to do. */
  12050. static void
  12051. save_function_data (tree decl)
  12052. {
  12053. struct language_function *f;
  12054. /* Save the language-specific per-function data so that we can
  12055. get it back when we really expand this function. */
  12056. gcc_assert (!DECL_PENDING_INLINE_P (decl));
  12057. /* Make a copy. */
  12058. f = ggc_alloc<language_function> ();
  12059. memcpy (f, cp_function_chain, sizeof (struct language_function));
  12060. DECL_SAVED_FUNCTION_DATA (decl) = f;
  12061. /* Clear out the bits we don't need. */
  12062. f->base.x_stmt_tree.x_cur_stmt_list = NULL;
  12063. f->bindings = NULL;
  12064. f->x_local_names = NULL;
  12065. f->base.local_typedefs = NULL;
  12066. }
  12067. /* Set the return value of the constructor (if present). */
  12068. static void
  12069. finish_constructor_body (void)
  12070. {
  12071. tree val;
  12072. tree exprstmt;
  12073. if (targetm.cxx.cdtor_returns_this ()
  12074. && (! TYPE_FOR_JAVA (current_class_type)))
  12075. {
  12076. /* Any return from a constructor will end up here. */
  12077. add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
  12078. val = DECL_ARGUMENTS (current_function_decl);
  12079. val = build2 (MODIFY_EXPR, TREE_TYPE (val),
  12080. DECL_RESULT (current_function_decl), val);
  12081. /* Return the address of the object. */
  12082. exprstmt = build_stmt (input_location, RETURN_EXPR, val);
  12083. add_stmt (exprstmt);
  12084. }
  12085. }
  12086. /* Do all the processing for the beginning of a destructor; set up the
  12087. vtable pointers and cleanups for bases and members. */
  12088. static void
  12089. begin_destructor_body (void)
  12090. {
  12091. tree compound_stmt;
  12092. /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
  12093. issued an error message. We still want to try to process the
  12094. body of the function, but initialize_vtbl_ptrs will crash if
  12095. TYPE_BINFO is NULL. */
  12096. if (COMPLETE_TYPE_P (current_class_type))
  12097. {
  12098. compound_stmt = begin_compound_stmt (0);
  12099. /* Make all virtual function table pointers in non-virtual base
  12100. classes point to CURRENT_CLASS_TYPE's virtual function
  12101. tables. */
  12102. initialize_vtbl_ptrs (current_class_ptr);
  12103. finish_compound_stmt (compound_stmt);
  12104. if (flag_lifetime_dse)
  12105. {
  12106. /* Insert a cleanup to let the back end know that the object is dead
  12107. when we exit the destructor, either normally or via exception. */
  12108. tree btype = CLASSTYPE_AS_BASE (current_class_type);
  12109. tree clobber = build_constructor (btype, NULL);
  12110. TREE_THIS_VOLATILE (clobber) = true;
  12111. tree bref = build_nop (build_reference_type (btype),
  12112. current_class_ptr);
  12113. bref = convert_from_reference (bref);
  12114. tree exprstmt = build2 (MODIFY_EXPR, btype, bref, clobber);
  12115. finish_decl_cleanup (NULL_TREE, exprstmt);
  12116. }
  12117. /* And insert cleanups for our bases and members so that they
  12118. will be properly destroyed if we throw. */
  12119. push_base_cleanups ();
  12120. }
  12121. }
  12122. /* At the end of every destructor we generate code to delete the object if
  12123. necessary. Do that now. */
  12124. static void
  12125. finish_destructor_body (void)
  12126. {
  12127. tree exprstmt;
  12128. /* Any return from a destructor will end up here; that way all base
  12129. and member cleanups will be run when the function returns. */
  12130. add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
  12131. /* In a virtual destructor, we must call delete. */
  12132. if (DECL_VIRTUAL_P (current_function_decl))
  12133. {
  12134. tree if_stmt;
  12135. tree virtual_size = cxx_sizeof (current_class_type);
  12136. /* [class.dtor]
  12137. At the point of definition of a virtual destructor (including
  12138. an implicit definition), non-placement operator delete shall
  12139. be looked up in the scope of the destructor's class and if
  12140. found shall be accessible and unambiguous. */
  12141. exprstmt = build_op_delete_call (DELETE_EXPR, current_class_ptr,
  12142. virtual_size,
  12143. /*global_p=*/false,
  12144. /*placement=*/NULL_TREE,
  12145. /*alloc_fn=*/NULL_TREE,
  12146. tf_warning_or_error);
  12147. if_stmt = begin_if_stmt ();
  12148. finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
  12149. current_in_charge_parm,
  12150. integer_one_node),
  12151. if_stmt);
  12152. finish_expr_stmt (exprstmt);
  12153. finish_then_clause (if_stmt);
  12154. finish_if_stmt (if_stmt);
  12155. }
  12156. if (targetm.cxx.cdtor_returns_this ())
  12157. {
  12158. tree val;
  12159. val = DECL_ARGUMENTS (current_function_decl);
  12160. val = build2 (MODIFY_EXPR, TREE_TYPE (val),
  12161. DECL_RESULT (current_function_decl), val);
  12162. /* Return the address of the object. */
  12163. exprstmt = build_stmt (input_location, RETURN_EXPR, val);
  12164. add_stmt (exprstmt);
  12165. }
  12166. }
  12167. /* Do the necessary processing for the beginning of a function body, which
  12168. in this case includes member-initializers, but not the catch clauses of
  12169. a function-try-block. Currently, this means opening a binding level
  12170. for the member-initializers (in a ctor), member cleanups (in a dtor),
  12171. and capture proxies (in a lambda operator()). */
  12172. tree
  12173. begin_function_body (void)
  12174. {
  12175. tree stmt;
  12176. if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
  12177. return NULL_TREE;
  12178. if (processing_template_decl)
  12179. /* Do nothing now. */;
  12180. else
  12181. /* Always keep the BLOCK node associated with the outermost pair of
  12182. curly braces of a function. These are needed for correct
  12183. operation of dwarfout.c. */
  12184. keep_next_level (true);
  12185. stmt = begin_compound_stmt (BCS_FN_BODY);
  12186. if (processing_template_decl)
  12187. /* Do nothing now. */;
  12188. else if (DECL_DESTRUCTOR_P (current_function_decl))
  12189. begin_destructor_body ();
  12190. return stmt;
  12191. }
  12192. /* Do the processing for the end of a function body. Currently, this means
  12193. closing out the cleanups for fully-constructed bases and members, and in
  12194. the case of the destructor, deleting the object if desired. Again, this
  12195. is only meaningful for [cd]tors, since they are the only functions where
  12196. there is a significant distinction between the main body and any
  12197. function catch clauses. Handling, say, main() return semantics here
  12198. would be wrong, as flowing off the end of a function catch clause for
  12199. main() would also need to return 0. */
  12200. void
  12201. finish_function_body (tree compstmt)
  12202. {
  12203. if (compstmt == NULL_TREE)
  12204. return;
  12205. /* Close the block. */
  12206. finish_compound_stmt (compstmt);
  12207. if (processing_template_decl)
  12208. /* Do nothing now. */;
  12209. else if (DECL_CONSTRUCTOR_P (current_function_decl))
  12210. finish_constructor_body ();
  12211. else if (DECL_DESTRUCTOR_P (current_function_decl))
  12212. finish_destructor_body ();
  12213. }
  12214. /* Given a function, returns the BLOCK corresponding to the outermost level
  12215. of curly braces, skipping the artificial block created for constructor
  12216. initializers. */
  12217. tree
  12218. outer_curly_brace_block (tree fndecl)
  12219. {
  12220. tree block = DECL_INITIAL (fndecl);
  12221. if (BLOCK_OUTER_CURLY_BRACE_P (block))
  12222. return block;
  12223. block = BLOCK_SUBBLOCKS (block);
  12224. if (BLOCK_OUTER_CURLY_BRACE_P (block))
  12225. return block;
  12226. block = BLOCK_SUBBLOCKS (block);
  12227. gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
  12228. return block;
  12229. }
  12230. /* If FNDECL is a class's key method, add the class to the list of
  12231. keyed classes that should be emitted. */
  12232. static void
  12233. record_key_method_defined (tree fndecl)
  12234. {
  12235. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
  12236. && DECL_VIRTUAL_P (fndecl)
  12237. && !processing_template_decl)
  12238. {
  12239. tree fnclass = DECL_CONTEXT (fndecl);
  12240. if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
  12241. keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
  12242. }
  12243. }
  12244. /* Subroutine of finish_function.
  12245. Save the body of constexpr functions for possible
  12246. future compile time evaluation. */
  12247. static void
  12248. maybe_save_function_definition (tree fun)
  12249. {
  12250. if (!processing_template_decl
  12251. && DECL_DECLARED_CONSTEXPR_P (fun)
  12252. && !cp_function_chain->invalid_constexpr
  12253. && !DECL_CLONED_FUNCTION_P (fun))
  12254. register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
  12255. }
  12256. /* Finish up a function declaration and compile that function
  12257. all the way to assembler language output. The free the storage
  12258. for the function definition.
  12259. FLAGS is a bitwise or of the following values:
  12260. 2 - INCLASS_INLINE
  12261. We just finished processing the body of an in-class inline
  12262. function definition. (This processing will have taken place
  12263. after the class definition is complete.) */
  12264. tree
  12265. finish_function (int flags)
  12266. {
  12267. tree fndecl = current_function_decl;
  12268. tree fntype, ctype = NULL_TREE;
  12269. int inclass_inline = (flags & 2) != 0;
  12270. /* When we get some parse errors, we can end up without a
  12271. current_function_decl, so cope. */
  12272. if (fndecl == NULL_TREE)
  12273. return error_mark_node;
  12274. if (c_dialect_objc ())
  12275. objc_finish_function ();
  12276. gcc_assert (!defer_mark_used_calls);
  12277. defer_mark_used_calls = true;
  12278. record_key_method_defined (fndecl);
  12279. fntype = TREE_TYPE (fndecl);
  12280. /* TREE_READONLY (fndecl) = 1;
  12281. This caused &foo to be of type ptr-to-const-function
  12282. which then got a warning when stored in a ptr-to-function variable. */
  12283. gcc_assert (building_stmt_list_p ());
  12284. /* The current function is being defined, so its DECL_INITIAL should
  12285. be set, and unless there's a multiple definition, it should be
  12286. error_mark_node. */
  12287. gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
  12288. /* For a cloned function, we've already got all the code we need;
  12289. there's no need to add any extra bits. */
  12290. if (!DECL_CLONED_FUNCTION_P (fndecl))
  12291. {
  12292. /* Make it so that `main' always returns 0 by default. */
  12293. if (DECL_MAIN_P (current_function_decl))
  12294. finish_return_stmt (integer_zero_node);
  12295. if (use_eh_spec_block (current_function_decl))
  12296. finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
  12297. (TREE_TYPE (current_function_decl)),
  12298. current_eh_spec_block);
  12299. }
  12300. /* If we're saving up tree structure, tie off the function now. */
  12301. DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
  12302. if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
  12303. cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
  12304. finish_fname_decls ();
  12305. /* If this function can't throw any exceptions, remember that. */
  12306. if (!processing_template_decl
  12307. && !cp_function_chain->can_throw
  12308. && !flag_non_call_exceptions
  12309. && !decl_replaceable_p (fndecl))
  12310. TREE_NOTHROW (fndecl) = 1;
  12311. /* This must come after expand_function_end because cleanups might
  12312. have declarations (from inline functions) that need to go into
  12313. this function's blocks. */
  12314. /* If the current binding level isn't the outermost binding level
  12315. for this function, either there is a bug, or we have experienced
  12316. syntax errors and the statement tree is malformed. */
  12317. if (current_binding_level->kind != sk_function_parms)
  12318. {
  12319. /* Make sure we have already experienced errors. */
  12320. gcc_assert (errorcount);
  12321. /* Throw away the broken statement tree and extra binding
  12322. levels. */
  12323. DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
  12324. while (current_binding_level->kind != sk_function_parms)
  12325. {
  12326. if (current_binding_level->kind == sk_class)
  12327. pop_nested_class ();
  12328. else
  12329. poplevel (0, 0, 0);
  12330. }
  12331. }
  12332. poplevel (1, 0, 1);
  12333. /* Statements should always be full-expressions at the outermost set
  12334. of curly braces for a function. */
  12335. gcc_assert (stmts_are_full_exprs_p ());
  12336. /* If there are no return statements in a function with auto return type,
  12337. the return type is void. But if the declared type is something like
  12338. auto*, this is an error. */
  12339. if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
  12340. && TREE_TYPE (fntype) == current_function_auto_return_pattern)
  12341. {
  12342. if (!is_auto (current_function_auto_return_pattern)
  12343. && !current_function_returns_value && !current_function_returns_null)
  12344. {
  12345. error ("no return statements in function returning %qT",
  12346. current_function_auto_return_pattern);
  12347. inform (input_location, "only plain %<auto%> return type can be "
  12348. "deduced to %<void%>");
  12349. }
  12350. apply_deduced_return_type (fndecl, void_type_node);
  12351. fntype = TREE_TYPE (fndecl);
  12352. }
  12353. /* Save constexpr function body before it gets munged by
  12354. the NRV transformation. */
  12355. maybe_save_function_definition (fndecl);
  12356. /* Set up the named return value optimization, if we can. Candidate
  12357. variables are selected in check_return_expr. */
  12358. if (current_function_return_value)
  12359. {
  12360. tree r = current_function_return_value;
  12361. tree outer;
  12362. if (r != error_mark_node
  12363. /* This is only worth doing for fns that return in memory--and
  12364. simpler, since we don't have to worry about promoted modes. */
  12365. && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
  12366. /* Only allow this for variables declared in the outer scope of
  12367. the function so we know that their lifetime always ends with a
  12368. return; see g++.dg/opt/nrv6.C. We could be more flexible if
  12369. we were to do this optimization in tree-ssa. */
  12370. && (outer = outer_curly_brace_block (fndecl))
  12371. && chain_member (r, BLOCK_VARS (outer)))
  12372. finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
  12373. current_function_return_value = NULL_TREE;
  12374. }
  12375. /* Remember that we were in class scope. */
  12376. if (current_class_name)
  12377. ctype = current_class_type;
  12378. /* Must mark the RESULT_DECL as being in this function. */
  12379. DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
  12380. /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
  12381. to the FUNCTION_DECL node itself. */
  12382. BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
  12383. /* Save away current state, if appropriate. */
  12384. if (!processing_template_decl)
  12385. save_function_data (fndecl);
  12386. /* Complain if there's just no return statement. */
  12387. if (warn_return_type
  12388. && !VOID_TYPE_P (TREE_TYPE (fntype))
  12389. && !dependent_type_p (TREE_TYPE (fntype))
  12390. && !current_function_returns_value && !current_function_returns_null
  12391. /* Don't complain if we abort or throw. */
  12392. && !current_function_returns_abnormally
  12393. /* Don't complain if there's an infinite loop. */
  12394. && !current_function_infinite_loop
  12395. /* Don't complain if we are declared noreturn. */
  12396. && !TREE_THIS_VOLATILE (fndecl)
  12397. && !DECL_NAME (DECL_RESULT (fndecl))
  12398. && !TREE_NO_WARNING (fndecl)
  12399. /* Structor return values (if any) are set by the compiler. */
  12400. && !DECL_CONSTRUCTOR_P (fndecl)
  12401. && !DECL_DESTRUCTOR_P (fndecl)
  12402. && targetm.warn_func_return (fndecl))
  12403. {
  12404. warning (OPT_Wreturn_type,
  12405. "no return statement in function returning non-void");
  12406. TREE_NO_WARNING (fndecl) = 1;
  12407. }
  12408. /* Store the end of the function, so that we get good line number
  12409. info for the epilogue. */
  12410. cfun->function_end_locus = input_location;
  12411. /* Complain about parameters that are only set, but never otherwise used. */
  12412. if (warn_unused_but_set_parameter
  12413. && !processing_template_decl
  12414. && errorcount == unused_but_set_errorcount
  12415. && !DECL_CLONED_FUNCTION_P (fndecl))
  12416. {
  12417. tree decl;
  12418. for (decl = DECL_ARGUMENTS (fndecl);
  12419. decl;
  12420. decl = DECL_CHAIN (decl))
  12421. if (TREE_USED (decl)
  12422. && TREE_CODE (decl) == PARM_DECL
  12423. && !DECL_READ_P (decl)
  12424. && DECL_NAME (decl)
  12425. && !DECL_ARTIFICIAL (decl)
  12426. && !TREE_NO_WARNING (decl)
  12427. && !DECL_IN_SYSTEM_HEADER (decl)
  12428. && TREE_TYPE (decl) != error_mark_node
  12429. && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
  12430. && (!CLASS_TYPE_P (TREE_TYPE (decl))
  12431. || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
  12432. warning (OPT_Wunused_but_set_parameter,
  12433. "parameter %q+D set but not used", decl);
  12434. unused_but_set_errorcount = errorcount;
  12435. }
  12436. /* Complain about locally defined typedefs that are not used in this
  12437. function. */
  12438. maybe_warn_unused_local_typedefs ();
  12439. /* Genericize before inlining. */
  12440. if (!processing_template_decl)
  12441. {
  12442. struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
  12443. invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
  12444. cp_genericize (fndecl);
  12445. /* Clear out the bits we don't need. */
  12446. f->x_current_class_ptr = NULL;
  12447. f->x_current_class_ref = NULL;
  12448. f->x_eh_spec_block = NULL;
  12449. f->x_in_charge_parm = NULL;
  12450. f->x_vtt_parm = NULL;
  12451. f->x_return_value = NULL;
  12452. f->bindings = NULL;
  12453. f->extern_decl_map = NULL;
  12454. f->infinite_loops = NULL;
  12455. }
  12456. /* Clear out the bits we don't need. */
  12457. local_names = NULL;
  12458. /* We're leaving the context of this function, so zap cfun. It's still in
  12459. DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
  12460. set_cfun (NULL);
  12461. current_function_decl = NULL;
  12462. /* If this is an in-class inline definition, we may have to pop the
  12463. bindings for the template parameters that we added in
  12464. maybe_begin_member_template_processing when start_function was
  12465. called. */
  12466. if (inclass_inline)
  12467. maybe_end_member_template_processing ();
  12468. /* Leave the scope of the class. */
  12469. if (ctype)
  12470. pop_nested_class ();
  12471. --function_depth;
  12472. /* Clean up. */
  12473. current_function_decl = NULL_TREE;
  12474. defer_mark_used_calls = false;
  12475. if (deferred_mark_used_calls)
  12476. {
  12477. unsigned int i;
  12478. tree decl;
  12479. FOR_EACH_VEC_SAFE_ELT (deferred_mark_used_calls, i, decl)
  12480. mark_used (decl);
  12481. vec_free (deferred_mark_used_calls);
  12482. }
  12483. return fndecl;
  12484. }
  12485. /* Create the FUNCTION_DECL for a function definition.
  12486. DECLSPECS and DECLARATOR are the parts of the declaration;
  12487. they describe the return type and the name of the function,
  12488. but twisted together in a fashion that parallels the syntax of C.
  12489. This function creates a binding context for the function body
  12490. as well as setting up the FUNCTION_DECL in current_function_decl.
  12491. Returns a FUNCTION_DECL on success.
  12492. If the DECLARATOR is not suitable for a function (it defines a datum
  12493. instead), we return 0, which tells yyparse to report a parse error.
  12494. May return void_type_node indicating that this method is actually
  12495. a friend. See grokfield for more details.
  12496. Came here with a `.pushlevel' .
  12497. DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
  12498. CHANGES TO CODE IN `grokfield'. */
  12499. tree
  12500. grokmethod (cp_decl_specifier_seq *declspecs,
  12501. const cp_declarator *declarator, tree attrlist)
  12502. {
  12503. tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
  12504. &attrlist);
  12505. if (fndecl == error_mark_node)
  12506. return error_mark_node;
  12507. if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
  12508. {
  12509. error ("invalid member function declaration");
  12510. return error_mark_node;
  12511. }
  12512. if (attrlist)
  12513. cplus_decl_attributes (&fndecl, attrlist, 0);
  12514. /* Pass friends other than inline friend functions back. */
  12515. if (fndecl == void_type_node)
  12516. return fndecl;
  12517. if (DECL_IN_AGGR_P (fndecl))
  12518. {
  12519. if (DECL_CLASS_SCOPE_P (fndecl))
  12520. error ("%qD is already defined in class %qT", fndecl,
  12521. DECL_CONTEXT (fndecl));
  12522. return error_mark_node;
  12523. }
  12524. check_template_shadow (fndecl);
  12525. DECL_COMDAT (fndecl) = 1;
  12526. DECL_DECLARED_INLINE_P (fndecl) = 1;
  12527. DECL_NO_INLINE_WARNING_P (fndecl) = 1;
  12528. /* We process method specializations in finish_struct_1. */
  12529. if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
  12530. {
  12531. fndecl = push_template_decl (fndecl);
  12532. if (fndecl == error_mark_node)
  12533. return fndecl;
  12534. }
  12535. if (! DECL_FRIEND_P (fndecl))
  12536. {
  12537. if (DECL_CHAIN (fndecl))
  12538. {
  12539. fndecl = copy_node (fndecl);
  12540. TREE_CHAIN (fndecl) = NULL_TREE;
  12541. }
  12542. }
  12543. cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
  12544. DECL_IN_AGGR_P (fndecl) = 1;
  12545. return fndecl;
  12546. }
  12547. /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
  12548. we can lay it out later, when and if its type becomes complete.
  12549. Also handle constexpr variables where the initializer involves
  12550. an unlowered PTRMEM_CST because the class isn't complete yet. */
  12551. void
  12552. maybe_register_incomplete_var (tree var)
  12553. {
  12554. gcc_assert (VAR_P (var));
  12555. /* Keep track of variables with incomplete types. */
  12556. if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
  12557. && DECL_EXTERNAL (var))
  12558. {
  12559. tree inner_type = TREE_TYPE (var);
  12560. while (TREE_CODE (inner_type) == ARRAY_TYPE)
  12561. inner_type = TREE_TYPE (inner_type);
  12562. inner_type = TYPE_MAIN_VARIANT (inner_type);
  12563. if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
  12564. /* RTTI TD entries are created while defining the type_info. */
  12565. || (TYPE_LANG_SPECIFIC (inner_type)
  12566. && TYPE_BEING_DEFINED (inner_type)))
  12567. {
  12568. incomplete_var iv = {var, inner_type};
  12569. vec_safe_push (incomplete_vars, iv);
  12570. }
  12571. else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
  12572. && decl_constant_var_p (var)
  12573. && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
  12574. {
  12575. /* When the outermost open class is complete we can resolve any
  12576. pointers-to-members. */
  12577. tree context = outermost_open_class ();
  12578. incomplete_var iv = {var, context};
  12579. vec_safe_push (incomplete_vars, iv);
  12580. }
  12581. }
  12582. }
  12583. /* Called when a class type (given by TYPE) is defined. If there are
  12584. any existing VAR_DECLs whose type has been completed by this
  12585. declaration, update them now. */
  12586. void
  12587. complete_vars (tree type)
  12588. {
  12589. unsigned ix;
  12590. incomplete_var *iv;
  12591. for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
  12592. {
  12593. if (same_type_p (type, iv->incomplete_type))
  12594. {
  12595. tree var = iv->decl;
  12596. tree type = TREE_TYPE (var);
  12597. if (TYPE_MAIN_VARIANT (strip_array_types (type))
  12598. == iv->incomplete_type)
  12599. {
  12600. /* Complete the type of the variable. The VAR_DECL itself
  12601. will be laid out in expand_expr. */
  12602. complete_type (type);
  12603. cp_apply_type_quals_to_decl (cp_type_quals (type), var);
  12604. }
  12605. if (DECL_INITIAL (var)
  12606. && decl_constant_var_p (var))
  12607. DECL_INITIAL (var) = cplus_expand_constant (DECL_INITIAL (var));
  12608. /* Remove this entry from the list. */
  12609. incomplete_vars->unordered_remove (ix);
  12610. }
  12611. else
  12612. ix++;
  12613. }
  12614. /* Check for pending declarations which may have abstract type. */
  12615. complete_type_check_abstract (type);
  12616. }
  12617. /* If DECL is of a type which needs a cleanup, build and return an
  12618. expression to perform that cleanup here. Return NULL_TREE if no
  12619. cleanup need be done. */
  12620. tree
  12621. cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
  12622. {
  12623. tree type;
  12624. tree attr;
  12625. tree cleanup;
  12626. /* Assume no cleanup is required. */
  12627. cleanup = NULL_TREE;
  12628. if (error_operand_p (decl))
  12629. return cleanup;
  12630. /* Handle "__attribute__((cleanup))". We run the cleanup function
  12631. before the destructor since the destructor is what actually
  12632. terminates the lifetime of the object. */
  12633. attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
  12634. if (attr)
  12635. {
  12636. tree id;
  12637. tree fn;
  12638. tree arg;
  12639. /* Get the name specified by the user for the cleanup function. */
  12640. id = TREE_VALUE (TREE_VALUE (attr));
  12641. /* Look up the name to find the cleanup function to call. It is
  12642. important to use lookup_name here because that is what is
  12643. used in c-common.c:handle_cleanup_attribute when performing
  12644. initial checks on the attribute. Note that those checks
  12645. include ensuring that the function found is not an overloaded
  12646. function, or an object with an overloaded call operator,
  12647. etc.; we can rely on the fact that the function found is an
  12648. ordinary FUNCTION_DECL. */
  12649. fn = lookup_name (id);
  12650. arg = build_address (decl);
  12651. mark_used (decl);
  12652. cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
  12653. if (cleanup == error_mark_node)
  12654. return error_mark_node;
  12655. }
  12656. /* Handle ordinary C++ destructors. */
  12657. type = TREE_TYPE (decl);
  12658. if (type_build_dtor_call (type))
  12659. {
  12660. int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
  12661. tree addr;
  12662. tree call;
  12663. if (TREE_CODE (type) == ARRAY_TYPE)
  12664. addr = decl;
  12665. else
  12666. addr = build_address (decl);
  12667. call = build_delete (TREE_TYPE (addr), addr,
  12668. sfk_complete_destructor, flags, 0, complain);
  12669. if (call == error_mark_node)
  12670. cleanup = error_mark_node;
  12671. else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  12672. /* Discard the call. */;
  12673. else if (cleanup)
  12674. cleanup = cp_build_compound_expr (cleanup, call, complain);
  12675. else
  12676. cleanup = call;
  12677. }
  12678. /* build_delete sets the location of the destructor call to the
  12679. current location, even though the destructor is going to be
  12680. called later, at the end of the current scope. This can lead to
  12681. a "jumpy" behaviour for users of debuggers when they step around
  12682. the end of the block. So let's unset the location of the
  12683. destructor call instead. */
  12684. if (cleanup != NULL && EXPR_P (cleanup))
  12685. SET_EXPR_LOCATION (cleanup, UNKNOWN_LOCATION);
  12686. if (cleanup
  12687. && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
  12688. /* Treat objects with destructors as used; the destructor may do
  12689. something substantive. */
  12690. mark_used (decl);
  12691. return cleanup;
  12692. }
  12693. /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
  12694. FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
  12695. METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
  12696. tree
  12697. static_fn_type (tree memfntype)
  12698. {
  12699. tree fntype;
  12700. tree args;
  12701. if (TYPE_PTRMEMFUNC_P (memfntype))
  12702. memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
  12703. if (POINTER_TYPE_P (memfntype)
  12704. || TREE_CODE (memfntype) == FUNCTION_DECL)
  12705. memfntype = TREE_TYPE (memfntype);
  12706. if (TREE_CODE (memfntype) == FUNCTION_TYPE)
  12707. return memfntype;
  12708. gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
  12709. args = TYPE_ARG_TYPES (memfntype);
  12710. cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
  12711. fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
  12712. fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
  12713. fntype = (cp_build_type_attribute_variant
  12714. (fntype, TYPE_ATTRIBUTES (memfntype)));
  12715. fntype = (build_exception_variant
  12716. (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
  12717. if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
  12718. TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
  12719. return fntype;
  12720. }
  12721. /* DECL was originally constructed as a non-static member function,
  12722. but turned out to be static. Update it accordingly. */
  12723. void
  12724. revert_static_member_fn (tree decl)
  12725. {
  12726. tree stype = static_fn_type (decl);
  12727. cp_cv_quals quals = type_memfn_quals (stype);
  12728. cp_ref_qualifier rqual = type_memfn_rqual (stype);
  12729. if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
  12730. stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
  12731. TREE_TYPE (decl) = stype;
  12732. if (DECL_ARGUMENTS (decl))
  12733. DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
  12734. DECL_STATIC_FUNCTION_P (decl) = 1;
  12735. }
  12736. /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
  12737. one of the language-independent trees. */
  12738. enum cp_tree_node_structure_enum
  12739. cp_tree_node_structure (union lang_tree_node * t)
  12740. {
  12741. switch (TREE_CODE (&t->generic))
  12742. {
  12743. case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
  12744. case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
  12745. case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
  12746. case OVERLOAD: return TS_CP_OVERLOAD;
  12747. case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
  12748. case PTRMEM_CST: return TS_CP_PTRMEM;
  12749. case BASELINK: return TS_CP_BASELINK;
  12750. case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
  12751. case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
  12752. case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
  12753. case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
  12754. case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
  12755. case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
  12756. case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
  12757. default: return TS_CP_GENERIC;
  12758. }
  12759. }
  12760. /* Build the void_list_node (void_type_node having been created). */
  12761. tree
  12762. build_void_list_node (void)
  12763. {
  12764. tree t = build_tree_list (NULL_TREE, void_type_node);
  12765. return t;
  12766. }
  12767. bool
  12768. cp_missing_noreturn_ok_p (tree decl)
  12769. {
  12770. /* A missing noreturn is ok for the `main' function. */
  12771. return DECL_MAIN_P (decl);
  12772. }
  12773. /* Return the decl used to identify the COMDAT group into which DECL should
  12774. be placed. */
  12775. tree
  12776. cxx_comdat_group (tree decl)
  12777. {
  12778. /* Virtual tables, construction virtual tables, and virtual table
  12779. tables all go in a single COMDAT group, named after the primary
  12780. virtual table. */
  12781. if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
  12782. decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
  12783. /* For all other DECLs, the COMDAT group is the mangled name of the
  12784. declaration itself. */
  12785. else
  12786. {
  12787. while (DECL_THUNK_P (decl))
  12788. {
  12789. /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
  12790. into the same section as the target function. In that case
  12791. we must return target's name. */
  12792. tree target = THUNK_TARGET (decl);
  12793. if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
  12794. && DECL_SECTION_NAME (target) != NULL
  12795. && DECL_ONE_ONLY (target))
  12796. decl = target;
  12797. else
  12798. break;
  12799. }
  12800. }
  12801. return decl;
  12802. }
  12803. /* Returns the return type for FN as written by the user, which may include
  12804. a placeholder for a deduced return type. */
  12805. tree
  12806. fndecl_declared_return_type (tree fn)
  12807. {
  12808. fn = STRIP_TEMPLATE (fn);
  12809. if (FNDECL_USED_AUTO (fn))
  12810. {
  12811. struct language_function *f = NULL;
  12812. if (DECL_STRUCT_FUNCTION (fn))
  12813. f = DECL_STRUCT_FUNCTION (fn)->language;
  12814. if (f == NULL)
  12815. f = DECL_SAVED_FUNCTION_DATA (fn);
  12816. return f->x_auto_return_pattern;
  12817. }
  12818. return TREE_TYPE (TREE_TYPE (fn));
  12819. }
  12820. /* Returns true iff DECL was declared with an auto return type and it has
  12821. not yet been deduced to a real type. */
  12822. bool
  12823. undeduced_auto_decl (tree decl)
  12824. {
  12825. if (cxx_dialect < cxx14)
  12826. return false;
  12827. return type_uses_auto (TREE_TYPE (decl));
  12828. }
  12829. /* Complain if DECL has an undeduced return type. */
  12830. void
  12831. require_deduced_type (tree decl)
  12832. {
  12833. if (undeduced_auto_decl (decl))
  12834. error ("use of %qD before deduction of %<auto%>", decl);
  12835. }
  12836. #include "gt-cp-decl.h"