decl2.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169
  1. /* Process declarations and variables for C++ compiler.
  2. Copyright (C) 1988-2015 Free Software Foundation, Inc.
  3. Hacked by Michael Tiemann (tiemann@cygnus.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* 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 "stringpool.h"
  36. #include "varasm.h"
  37. #include "attribs.h"
  38. #include "stor-layout.h"
  39. #include "calls.h"
  40. #include "flags.h"
  41. #include "cp-tree.h"
  42. #include "decl.h"
  43. #include "toplev.h"
  44. #include "timevar.h"
  45. #include "cpplib.h"
  46. #include "target.h"
  47. #include "c-family/c-common.h"
  48. #include "c-family/c-objc.h"
  49. #include "hash-map.h"
  50. #include "is-a.h"
  51. #include "plugin-api.h"
  52. #include "hard-reg-set.h"
  53. #include "input.h"
  54. #include "function.h"
  55. #include "ipa-ref.h"
  56. #include "cgraph.h"
  57. #include "tree-inline.h"
  58. #include "c-family/c-pragma.h"
  59. #include "dumpfile.h"
  60. #include "intl.h"
  61. #include "splay-tree.h"
  62. #include "langhooks.h"
  63. #include "c-family/c-ada-spec.h"
  64. #include "asan.h"
  65. extern cpp_reader *parse_in;
  66. /* This structure contains information about the initializations
  67. and/or destructions required for a particular priority level. */
  68. typedef struct priority_info_s {
  69. /* Nonzero if there have been any initializations at this priority
  70. throughout the translation unit. */
  71. int initializations_p;
  72. /* Nonzero if there have been any destructions at this priority
  73. throughout the translation unit. */
  74. int destructions_p;
  75. } *priority_info;
  76. static void mark_vtable_entries (tree);
  77. static bool maybe_emit_vtables (tree);
  78. static bool acceptable_java_type (tree);
  79. static tree start_objects (int, int);
  80. static void finish_objects (int, int, tree);
  81. static tree start_static_storage_duration_function (unsigned);
  82. static void finish_static_storage_duration_function (tree);
  83. static priority_info get_priority_info (int);
  84. static void do_static_initialization_or_destruction (tree, bool);
  85. static void one_static_initialization_or_destruction (tree, tree, bool);
  86. static void generate_ctor_or_dtor_function (bool, int, location_t *);
  87. static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node,
  88. void *);
  89. static tree prune_vars_needing_no_initialization (tree *);
  90. static void write_out_vars (tree);
  91. static void import_export_class (tree);
  92. static tree get_guard_bits (tree);
  93. static void determine_visibility_from_class (tree, tree);
  94. static bool determine_hidden_inline (tree);
  95. static bool decl_defined_p (tree);
  96. /* A list of static class variables. This is needed, because a
  97. static class variable can be declared inside the class without
  98. an initializer, and then initialized, statically, outside the class. */
  99. static GTY(()) vec<tree, va_gc> *pending_statics;
  100. /* A list of functions which were declared inline, but which we
  101. may need to emit outline anyway. */
  102. static GTY(()) vec<tree, va_gc> *deferred_fns;
  103. /* A list of decls that use types with no linkage, which we need to make
  104. sure are defined. */
  105. static GTY(()) vec<tree, va_gc> *no_linkage_decls;
  106. /* Nonzero if we're done parsing and into end-of-file activities. */
  107. int at_eof;
  108. /* Return a member function type (a METHOD_TYPE), given FNTYPE (a
  109. FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers
  110. that apply to the function). */
  111. tree
  112. build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals,
  113. cp_ref_qualifier rqual)
  114. {
  115. tree raises;
  116. tree attrs;
  117. int type_quals;
  118. bool late_return_type_p;
  119. if (fntype == error_mark_node || ctype == error_mark_node)
  120. return error_mark_node;
  121. gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE
  122. || TREE_CODE (fntype) == METHOD_TYPE);
  123. type_quals = quals & ~TYPE_QUAL_RESTRICT;
  124. ctype = cp_build_qualified_type (ctype, type_quals);
  125. raises = TYPE_RAISES_EXCEPTIONS (fntype);
  126. attrs = TYPE_ATTRIBUTES (fntype);
  127. late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
  128. fntype = build_method_type_directly (ctype, TREE_TYPE (fntype),
  129. (TREE_CODE (fntype) == METHOD_TYPE
  130. ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
  131. : TYPE_ARG_TYPES (fntype)));
  132. if (attrs)
  133. fntype = cp_build_type_attribute_variant (fntype, attrs);
  134. if (rqual)
  135. fntype = build_ref_qualified_type (fntype, rqual);
  136. if (raises)
  137. fntype = build_exception_variant (fntype, raises);
  138. if (late_return_type_p)
  139. TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
  140. return fntype;
  141. }
  142. /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its
  143. return type changed to NEW_RET. */
  144. tree
  145. change_return_type (tree new_ret, tree fntype)
  146. {
  147. tree newtype;
  148. tree args = TYPE_ARG_TYPES (fntype);
  149. tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
  150. tree attrs = TYPE_ATTRIBUTES (fntype);
  151. bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype);
  152. if (new_ret == error_mark_node)
  153. return fntype;
  154. if (same_type_p (new_ret, TREE_TYPE (fntype)))
  155. return fntype;
  156. if (TREE_CODE (fntype) == FUNCTION_TYPE)
  157. {
  158. newtype = build_function_type (new_ret, args);
  159. newtype = apply_memfn_quals (newtype,
  160. type_memfn_quals (fntype),
  161. type_memfn_rqual (fntype));
  162. }
  163. else
  164. newtype = build_method_type_directly
  165. (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args));
  166. if (raises)
  167. newtype = build_exception_variant (newtype, raises);
  168. if (attrs)
  169. newtype = cp_build_type_attribute_variant (newtype, attrs);
  170. if (late_return_type_p)
  171. TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1;
  172. return newtype;
  173. }
  174. /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE
  175. appropriately. */
  176. tree
  177. cp_build_parm_decl (tree name, tree type)
  178. {
  179. tree parm = build_decl (input_location,
  180. PARM_DECL, name, type);
  181. /* DECL_ARG_TYPE is only used by the back end and the back end never
  182. sees templates. */
  183. if (!processing_template_decl)
  184. DECL_ARG_TYPE (parm) = type_passed_as (type);
  185. return parm;
  186. }
  187. /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
  188. indicated NAME. */
  189. tree
  190. build_artificial_parm (tree name, tree type)
  191. {
  192. tree parm = cp_build_parm_decl (name, type);
  193. DECL_ARTIFICIAL (parm) = 1;
  194. /* All our artificial parms are implicitly `const'; they cannot be
  195. assigned to. */
  196. TREE_READONLY (parm) = 1;
  197. return parm;
  198. }
  199. /* Constructors for types with virtual baseclasses need an "in-charge" flag
  200. saying whether this constructor is responsible for initialization of
  201. virtual baseclasses or not. All destructors also need this "in-charge"
  202. flag, which additionally determines whether or not the destructor should
  203. free the memory for the object.
  204. This function adds the "in-charge" flag to member function FN if
  205. appropriate. It is called from grokclassfn and tsubst.
  206. FN must be either a constructor or destructor.
  207. The in-charge flag follows the 'this' parameter, and is followed by the
  208. VTT parm (if any), then the user-written parms. */
  209. void
  210. maybe_retrofit_in_chrg (tree fn)
  211. {
  212. tree basetype, arg_types, parms, parm, fntype;
  213. /* If we've already add the in-charge parameter don't do it again. */
  214. if (DECL_HAS_IN_CHARGE_PARM_P (fn))
  215. return;
  216. /* When processing templates we can't know, in general, whether or
  217. not we're going to have virtual baseclasses. */
  218. if (processing_template_decl)
  219. return;
  220. /* We don't need an in-charge parameter for constructors that don't
  221. have virtual bases. */
  222. if (DECL_CONSTRUCTOR_P (fn)
  223. && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
  224. return;
  225. arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
  226. basetype = TREE_TYPE (TREE_VALUE (arg_types));
  227. arg_types = TREE_CHAIN (arg_types);
  228. parms = DECL_CHAIN (DECL_ARGUMENTS (fn));
  229. /* If this is a subobject constructor or destructor, our caller will
  230. pass us a pointer to our VTT. */
  231. if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
  232. {
  233. parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
  234. /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
  235. DECL_CHAIN (parm) = parms;
  236. parms = parm;
  237. /* ...and then to TYPE_ARG_TYPES. */
  238. arg_types = hash_tree_chain (vtt_parm_type, arg_types);
  239. DECL_HAS_VTT_PARM_P (fn) = 1;
  240. }
  241. /* Then add the in-charge parm (before the VTT parm). */
  242. parm = build_artificial_parm (in_charge_identifier, integer_type_node);
  243. DECL_CHAIN (parm) = parms;
  244. parms = parm;
  245. arg_types = hash_tree_chain (integer_type_node, arg_types);
  246. /* Insert our new parameter(s) into the list. */
  247. DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms;
  248. /* And rebuild the function type. */
  249. fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)),
  250. arg_types);
  251. if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
  252. fntype = build_exception_variant (fntype,
  253. TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
  254. if (TYPE_ATTRIBUTES (TREE_TYPE (fn)))
  255. fntype = (cp_build_type_attribute_variant
  256. (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn))));
  257. TREE_TYPE (fn) = fntype;
  258. /* Now we've got the in-charge parameter. */
  259. DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
  260. }
  261. /* Classes overload their constituent function names automatically.
  262. When a function name is declared in a record structure,
  263. its name is changed to it overloaded name. Since names for
  264. constructors and destructors can conflict, we place a leading
  265. '$' for destructors.
  266. CNAME is the name of the class we are grokking for.
  267. FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
  268. FLAGS contains bits saying what's special about today's
  269. arguments. DTOR_FLAG == DESTRUCTOR.
  270. If FUNCTION is a destructor, then we must add the `auto-delete' field
  271. as a second parameter. There is some hair associated with the fact
  272. that we must "declare" this variable in the manner consistent with the
  273. way the rest of the arguments were declared.
  274. QUALS are the qualifiers for the this pointer. */
  275. void
  276. grokclassfn (tree ctype, tree function, enum overload_flags flags)
  277. {
  278. tree fn_name = DECL_NAME (function);
  279. /* Even within an `extern "C"' block, members get C++ linkage. See
  280. [dcl.link] for details. */
  281. SET_DECL_LANGUAGE (function, lang_cplusplus);
  282. if (fn_name == NULL_TREE)
  283. {
  284. error ("name missing for member function");
  285. fn_name = get_identifier ("<anonymous>");
  286. DECL_NAME (function) = fn_name;
  287. }
  288. DECL_CONTEXT (function) = ctype;
  289. if (flags == DTOR_FLAG)
  290. DECL_DESTRUCTOR_P (function) = 1;
  291. if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
  292. maybe_retrofit_in_chrg (function);
  293. }
  294. /* Create an ARRAY_REF, checking for the user doing things backwards
  295. along the way. DECLTYPE_P is for N3276, as in the parser. */
  296. tree
  297. grok_array_decl (location_t loc, tree array_expr, tree index_exp,
  298. bool decltype_p)
  299. {
  300. tree type;
  301. tree expr;
  302. tree orig_array_expr = array_expr;
  303. tree orig_index_exp = index_exp;
  304. if (error_operand_p (array_expr) || error_operand_p (index_exp))
  305. return error_mark_node;
  306. if (processing_template_decl)
  307. {
  308. if (type_dependent_expression_p (array_expr)
  309. || type_dependent_expression_p (index_exp))
  310. return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp,
  311. NULL_TREE, NULL_TREE);
  312. array_expr = build_non_dependent_expr (array_expr);
  313. index_exp = build_non_dependent_expr (index_exp);
  314. }
  315. type = TREE_TYPE (array_expr);
  316. gcc_assert (type);
  317. type = non_reference (type);
  318. /* If they have an `operator[]', use that. */
  319. if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp)))
  320. {
  321. tsubst_flags_t complain = tf_warning_or_error;
  322. if (decltype_p)
  323. complain |= tf_decltype;
  324. expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr,
  325. index_exp, NULL_TREE, /*overload=*/NULL, complain);
  326. }
  327. else
  328. {
  329. tree p1, p2, i1, i2;
  330. /* Otherwise, create an ARRAY_REF for a pointer or array type.
  331. It is a little-known fact that, if `a' is an array and `i' is
  332. an int, you can write `i[a]', which means the same thing as
  333. `a[i]'. */
  334. if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
  335. p1 = array_expr;
  336. else
  337. p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false);
  338. if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
  339. p2 = index_exp;
  340. else
  341. p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false);
  342. i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr,
  343. false);
  344. i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp,
  345. false);
  346. if ((p1 && i2) && (i1 && p2))
  347. error ("ambiguous conversion for array subscript");
  348. if (p1 && i2)
  349. array_expr = p1, index_exp = i2;
  350. else if (i1 && p2)
  351. array_expr = p2, index_exp = i1;
  352. else
  353. {
  354. error ("invalid types %<%T[%T]%> for array subscript",
  355. type, TREE_TYPE (index_exp));
  356. return error_mark_node;
  357. }
  358. if (array_expr == error_mark_node || index_exp == error_mark_node)
  359. error ("ambiguous conversion for array subscript");
  360. expr = build_array_ref (input_location, array_expr, index_exp);
  361. }
  362. if (processing_template_decl && expr != error_mark_node)
  363. return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp,
  364. NULL_TREE, NULL_TREE);
  365. return expr;
  366. }
  367. /* Given the cast expression EXP, checking out its validity. Either return
  368. an error_mark_node if there was an unavoidable error, return a cast to
  369. void for trying to delete a pointer w/ the value 0, or return the
  370. call to delete. If DOING_VEC is true, we handle things differently
  371. for doing an array delete.
  372. Implements ARM $5.3.4. This is called from the parser. */
  373. tree
  374. delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete,
  375. tsubst_flags_t complain)
  376. {
  377. tree t, type;
  378. if (exp == error_mark_node)
  379. return exp;
  380. if (processing_template_decl)
  381. {
  382. t = build_min (DELETE_EXPR, void_type_node, exp, size);
  383. DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
  384. DELETE_EXPR_USE_VEC (t) = doing_vec;
  385. TREE_SIDE_EFFECTS (t) = 1;
  386. return t;
  387. }
  388. /* An array can't have been allocated by new, so complain. */
  389. if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
  390. warning (0, "deleting array %q#E", exp);
  391. t = build_expr_type_conversion (WANT_POINTER, exp, true);
  392. if (t == NULL_TREE || t == error_mark_node)
  393. {
  394. error ("type %q#T argument given to %<delete%>, expected pointer",
  395. TREE_TYPE (exp));
  396. return error_mark_node;
  397. }
  398. type = TREE_TYPE (t);
  399. /* As of Valley Forge, you can delete a pointer to const. */
  400. /* You can't delete functions. */
  401. if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
  402. {
  403. error ("cannot delete a function. Only pointer-to-objects are "
  404. "valid arguments to %<delete%>");
  405. return error_mark_node;
  406. }
  407. /* Deleting ptr to void is undefined behavior [expr.delete/3]. */
  408. if (VOID_TYPE_P (TREE_TYPE (type)))
  409. {
  410. warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type);
  411. doing_vec = 0;
  412. }
  413. /* Deleting a pointer with the value zero is valid and has no effect. */
  414. if (integer_zerop (t))
  415. return build1 (NOP_EXPR, void_type_node, t);
  416. if (doing_vec)
  417. return build_vec_delete (t, /*maxindex=*/NULL_TREE,
  418. sfk_deleting_destructor,
  419. use_global_delete, complain);
  420. else
  421. return build_delete (type, t, sfk_deleting_destructor,
  422. LOOKUP_NORMAL, use_global_delete,
  423. complain);
  424. }
  425. /* Report an error if the indicated template declaration is not the
  426. sort of thing that should be a member template. */
  427. void
  428. check_member_template (tree tmpl)
  429. {
  430. tree decl;
  431. gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
  432. decl = DECL_TEMPLATE_RESULT (tmpl);
  433. if (TREE_CODE (decl) == FUNCTION_DECL
  434. || DECL_ALIAS_TEMPLATE_P (tmpl)
  435. || (TREE_CODE (decl) == TYPE_DECL
  436. && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))))
  437. {
  438. /* The parser rejects template declarations in local classes
  439. (with the exception of generic lambdas). */
  440. gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl));
  441. /* The parser rejects any use of virtual in a function template. */
  442. gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL
  443. && DECL_VIRTUAL_P (decl)));
  444. /* The debug-information generating code doesn't know what to do
  445. with member templates. */
  446. DECL_IGNORED_P (tmpl) = 1;
  447. }
  448. else if (variable_template_p (tmpl))
  449. /* OK */;
  450. else
  451. error ("template declaration of %q#D", decl);
  452. }
  453. /* Return true iff TYPE is a valid Java parameter or return type. */
  454. static bool
  455. acceptable_java_type (tree type)
  456. {
  457. if (type == error_mark_node)
  458. return false;
  459. if (VOID_TYPE_P (type) || TYPE_FOR_JAVA (type))
  460. return true;
  461. if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
  462. {
  463. type = TREE_TYPE (type);
  464. if (TREE_CODE (type) == RECORD_TYPE)
  465. {
  466. tree args; int i;
  467. if (! TYPE_FOR_JAVA (type))
  468. return false;
  469. if (! CLASSTYPE_TEMPLATE_INFO (type))
  470. return true;
  471. args = CLASSTYPE_TI_ARGS (type);
  472. i = TREE_VEC_LENGTH (args);
  473. while (--i >= 0)
  474. {
  475. type = TREE_VEC_ELT (args, i);
  476. if (TYPE_PTR_P (type))
  477. type = TREE_TYPE (type);
  478. if (! TYPE_FOR_JAVA (type))
  479. return false;
  480. }
  481. return true;
  482. }
  483. }
  484. return false;
  485. }
  486. /* For a METHOD in a Java class CTYPE, return true if
  487. the parameter and return types are valid Java types.
  488. Otherwise, print appropriate error messages, and return false. */
  489. bool
  490. check_java_method (tree method)
  491. {
  492. bool jerr = false;
  493. tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
  494. tree ret_type = TREE_TYPE (TREE_TYPE (method));
  495. if (!acceptable_java_type (ret_type))
  496. {
  497. error ("Java method %qD has non-Java return type %qT",
  498. method, ret_type);
  499. jerr = true;
  500. }
  501. arg_types = TREE_CHAIN (arg_types);
  502. if (DECL_HAS_IN_CHARGE_PARM_P (method))
  503. arg_types = TREE_CHAIN (arg_types);
  504. if (DECL_HAS_VTT_PARM_P (method))
  505. arg_types = TREE_CHAIN (arg_types);
  506. for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
  507. {
  508. tree type = TREE_VALUE (arg_types);
  509. if (!acceptable_java_type (type))
  510. {
  511. if (type != error_mark_node)
  512. error ("Java method %qD has non-Java parameter type %qT",
  513. method, type);
  514. jerr = true;
  515. }
  516. }
  517. return !jerr;
  518. }
  519. /* Sanity check: report error if this function FUNCTION is not
  520. really a member of the class (CTYPE) it is supposed to belong to.
  521. TEMPLATE_PARMS is used to specify the template parameters of a member
  522. template passed as FUNCTION_DECL. If the member template is passed as a
  523. TEMPLATE_DECL, it can be NULL since the parameters can be extracted
  524. from the declaration. If the function is not a function template, it
  525. must be NULL.
  526. It returns the original declaration for the function, NULL_TREE if
  527. no declaration was found, error_mark_node if an error was emitted. */
  528. tree
  529. check_classfn (tree ctype, tree function, tree template_parms)
  530. {
  531. int ix;
  532. bool is_template;
  533. tree pushed_scope;
  534. if (DECL_USE_TEMPLATE (function)
  535. && !(TREE_CODE (function) == TEMPLATE_DECL
  536. && DECL_TEMPLATE_SPECIALIZATION (function))
  537. && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function)))
  538. /* Since this is a specialization of a member template,
  539. we're not going to find the declaration in the class.
  540. For example, in:
  541. struct S { template <typename T> void f(T); };
  542. template <> void S::f(int);
  543. we're not going to find `S::f(int)', but there's no
  544. reason we should, either. We let our callers know we didn't
  545. find the method, but we don't complain. */
  546. return NULL_TREE;
  547. /* Basic sanity check: for a template function, the template parameters
  548. either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */
  549. if (TREE_CODE (function) == TEMPLATE_DECL)
  550. {
  551. if (template_parms
  552. && !comp_template_parms (template_parms,
  553. DECL_TEMPLATE_PARMS (function)))
  554. {
  555. error ("template parameter lists provided don%'t match the "
  556. "template parameters of %qD", function);
  557. return error_mark_node;
  558. }
  559. template_parms = DECL_TEMPLATE_PARMS (function);
  560. }
  561. /* OK, is this a definition of a member template? */
  562. is_template = (template_parms != NULL_TREE);
  563. /* [temp.mem]
  564. A destructor shall not be a member template. */
  565. if (DECL_DESTRUCTOR_P (function) && is_template)
  566. {
  567. error ("destructor %qD declared as member template", function);
  568. return error_mark_node;
  569. }
  570. /* We must enter the scope here, because conversion operators are
  571. named by target type, and type equivalence relies on typenames
  572. resolving within the scope of CTYPE. */
  573. pushed_scope = push_scope (ctype);
  574. ix = class_method_index_for_fn (complete_type (ctype), function);
  575. if (ix >= 0)
  576. {
  577. vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype);
  578. tree fndecls, fndecl = 0;
  579. bool is_conv_op;
  580. const char *format = NULL;
  581. for (fndecls = (*methods)[ix];
  582. fndecls; fndecls = OVL_NEXT (fndecls))
  583. {
  584. tree p1, p2;
  585. fndecl = OVL_CURRENT (fndecls);
  586. p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
  587. p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  588. /* We cannot simply call decls_match because this doesn't
  589. work for static member functions that are pretending to
  590. be methods, and because the name may have been changed by
  591. asm("new_name"). */
  592. /* Get rid of the this parameter on functions that become
  593. static. */
  594. if (DECL_STATIC_FUNCTION_P (fndecl)
  595. && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
  596. p1 = TREE_CHAIN (p1);
  597. /* A member template definition only matches a member template
  598. declaration. */
  599. if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
  600. continue;
  601. /* ref-qualifier or absence of same must match. */
  602. if (type_memfn_rqual (TREE_TYPE (function))
  603. != type_memfn_rqual (TREE_TYPE (fndecl)))
  604. continue;
  605. /* While finding a match, same types and params are not enough
  606. if the function is versioned. Also check version ("target")
  607. attributes. */
  608. if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
  609. TREE_TYPE (TREE_TYPE (fndecl)))
  610. && compparms (p1, p2)
  611. && !targetm.target_option.function_versions (function, fndecl)
  612. && (!is_template
  613. || comp_template_parms (template_parms,
  614. DECL_TEMPLATE_PARMS (fndecl)))
  615. && (DECL_TEMPLATE_SPECIALIZATION (function)
  616. == DECL_TEMPLATE_SPECIALIZATION (fndecl))
  617. && (!DECL_TEMPLATE_SPECIALIZATION (function)
  618. || (DECL_TI_TEMPLATE (function)
  619. == DECL_TI_TEMPLATE (fndecl))))
  620. break;
  621. }
  622. if (fndecls)
  623. {
  624. if (pushed_scope)
  625. pop_scope (pushed_scope);
  626. return OVL_CURRENT (fndecls);
  627. }
  628. error_at (DECL_SOURCE_LOCATION (function),
  629. "prototype for %q#D does not match any in class %qT",
  630. function, ctype);
  631. is_conv_op = DECL_CONV_FN_P (fndecl);
  632. if (is_conv_op)
  633. ix = CLASSTYPE_FIRST_CONVERSION_SLOT;
  634. fndecls = (*methods)[ix];
  635. while (fndecls)
  636. {
  637. fndecl = OVL_CURRENT (fndecls);
  638. fndecls = OVL_NEXT (fndecls);
  639. if (!fndecls && is_conv_op)
  640. {
  641. if (methods->length () > (size_t) ++ix)
  642. {
  643. fndecls = (*methods)[ix];
  644. if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls)))
  645. {
  646. fndecls = NULL_TREE;
  647. is_conv_op = false;
  648. }
  649. }
  650. else
  651. is_conv_op = false;
  652. }
  653. if (format)
  654. format = " %+#D";
  655. else if (fndecls)
  656. format = N_("candidates are: %+#D");
  657. else
  658. format = N_("candidate is: %+#D");
  659. error (format, fndecl);
  660. }
  661. }
  662. else if (!COMPLETE_TYPE_P (ctype))
  663. cxx_incomplete_type_error (function, ctype);
  664. else
  665. error ("no %q#D member function declared in class %qT",
  666. function, ctype);
  667. if (pushed_scope)
  668. pop_scope (pushed_scope);
  669. return error_mark_node;
  670. }
  671. /* DECL is a function with vague linkage. Remember it so that at the
  672. end of the translation unit we can decide whether or not to emit
  673. it. */
  674. void
  675. note_vague_linkage_fn (tree decl)
  676. {
  677. DECL_DEFER_OUTPUT (decl) = 1;
  678. vec_safe_push (deferred_fns, decl);
  679. }
  680. /* As above, but for variable template instantiations. */
  681. void
  682. note_variable_template_instantiation (tree decl)
  683. {
  684. vec_safe_push (pending_statics, decl);
  685. }
  686. /* We have just processed the DECL, which is a static data member.
  687. The other parameters are as for cp_finish_decl. */
  688. void
  689. finish_static_data_member_decl (tree decl,
  690. tree init, bool init_const_expr_p,
  691. tree asmspec_tree,
  692. int flags)
  693. {
  694. DECL_CONTEXT (decl) = current_class_type;
  695. /* We cannot call pushdecl here, because that would fill in the
  696. TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
  697. the right thing, namely, to put this decl out straight away. */
  698. if (! processing_template_decl)
  699. vec_safe_push (pending_statics, decl);
  700. if (LOCAL_CLASS_P (current_class_type)
  701. /* We already complained about the template definition. */
  702. && !DECL_TEMPLATE_INSTANTIATION (decl))
  703. permerror (input_location, "local class %q#T shall not have static data member %q#D",
  704. current_class_type, decl);
  705. else
  706. for (tree t = current_class_type; TYPE_P (t);
  707. t = CP_TYPE_CONTEXT (t))
  708. if (TYPE_ANONYMOUS_P (t))
  709. {
  710. if (permerror (DECL_SOURCE_LOCATION (decl),
  711. "static data member %qD in unnamed class", decl))
  712. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
  713. "unnamed class defined here");
  714. break;
  715. }
  716. DECL_IN_AGGR_P (decl) = 1;
  717. if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
  718. && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE)
  719. SET_VAR_HAD_UNKNOWN_BOUND (decl);
  720. cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags);
  721. }
  722. /* DECLARATOR and DECLSPECS correspond to a class member. The other
  723. parameters are as for cp_finish_decl. Return the DECL for the
  724. class member declared. */
  725. tree
  726. grokfield (const cp_declarator *declarator,
  727. cp_decl_specifier_seq *declspecs,
  728. tree init, bool init_const_expr_p,
  729. tree asmspec_tree,
  730. tree attrlist)
  731. {
  732. tree value;
  733. const char *asmspec = 0;
  734. int flags;
  735. tree name;
  736. if (init
  737. && TREE_CODE (init) == TREE_LIST
  738. && TREE_VALUE (init) == error_mark_node
  739. && TREE_CHAIN (init) == NULL_TREE)
  740. init = NULL_TREE;
  741. value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
  742. if (! value || value == error_mark_node)
  743. /* friend or constructor went bad. */
  744. return error_mark_node;
  745. if (TREE_TYPE (value) == error_mark_node)
  746. return value;
  747. if (TREE_CODE (value) == TYPE_DECL && init)
  748. {
  749. error ("typedef %qD is initialized (use decltype instead)", value);
  750. init = NULL_TREE;
  751. }
  752. /* Pass friendly classes back. */
  753. if (value == void_type_node)
  754. return value;
  755. name = DECL_NAME (value);
  756. if (name != NULL_TREE)
  757. {
  758. if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
  759. {
  760. error ("explicit template argument list not allowed");
  761. return error_mark_node;
  762. }
  763. if (IDENTIFIER_POINTER (name)[0] == '_'
  764. && ! strcmp (IDENTIFIER_POINTER (name), "_vptr"))
  765. error ("member %qD conflicts with virtual function table field name",
  766. value);
  767. }
  768. /* Stash away type declarations. */
  769. if (TREE_CODE (value) == TYPE_DECL)
  770. {
  771. DECL_NONLOCAL (value) = 1;
  772. DECL_CONTEXT (value) = current_class_type;
  773. if (attrlist)
  774. {
  775. int attrflags = 0;
  776. /* If this is a typedef that names the class for linkage purposes
  777. (7.1.3p8), apply any attributes directly to the type. */
  778. if (OVERLOAD_TYPE_P (TREE_TYPE (value))
  779. && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
  780. attrflags = ATTR_FLAG_TYPE_IN_PLACE;
  781. cplus_decl_attributes (&value, attrlist, attrflags);
  782. }
  783. if (decl_spec_seq_has_spec_p (declspecs, ds_typedef)
  784. && TREE_TYPE (value) != error_mark_node
  785. && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value)
  786. set_underlying_type (value);
  787. /* It's important that push_template_decl below follows
  788. set_underlying_type above so that the created template
  789. carries the properly set type of VALUE. */
  790. if (processing_template_decl)
  791. value = push_template_decl (value);
  792. record_locally_defined_typedef (value);
  793. return value;
  794. }
  795. int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
  796. if (!friendp && DECL_IN_AGGR_P (value))
  797. {
  798. error ("%qD is already defined in %qT", value, DECL_CONTEXT (value));
  799. return void_type_node;
  800. }
  801. if (asmspec_tree && asmspec_tree != error_mark_node)
  802. asmspec = TREE_STRING_POINTER (asmspec_tree);
  803. if (init)
  804. {
  805. if (TREE_CODE (value) == FUNCTION_DECL)
  806. {
  807. if (init == ridpointers[(int)RID_DELETE])
  808. {
  809. DECL_DELETED_FN (value) = 1;
  810. DECL_DECLARED_INLINE_P (value) = 1;
  811. DECL_INITIAL (value) = error_mark_node;
  812. }
  813. else if (init == ridpointers[(int)RID_DEFAULT])
  814. {
  815. if (defaultable_fn_check (value))
  816. {
  817. DECL_DEFAULTED_FN (value) = 1;
  818. DECL_INITIALIZED_IN_CLASS_P (value) = 1;
  819. DECL_DECLARED_INLINE_P (value) = 1;
  820. }
  821. }
  822. else if (TREE_CODE (init) == DEFAULT_ARG)
  823. error ("invalid initializer for member function %qD", value);
  824. else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE)
  825. {
  826. if (integer_zerop (init))
  827. DECL_PURE_VIRTUAL_P (value) = 1;
  828. else if (error_operand_p (init))
  829. ; /* An error has already been reported. */
  830. else
  831. error ("invalid initializer for member function %qD",
  832. value);
  833. }
  834. else
  835. {
  836. gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE);
  837. if (friendp)
  838. error ("initializer specified for friend function %qD",
  839. value);
  840. else
  841. error ("initializer specified for static member function %qD",
  842. value);
  843. }
  844. }
  845. else if (TREE_CODE (value) == FIELD_DECL)
  846. /* C++11 NSDMI, keep going. */;
  847. else if (!VAR_P (value))
  848. gcc_unreachable ();
  849. }
  850. /* Pass friend decls back. */
  851. if ((TREE_CODE (value) == FUNCTION_DECL
  852. || TREE_CODE (value) == TEMPLATE_DECL)
  853. && DECL_CONTEXT (value) != current_class_type)
  854. return value;
  855. /* Need to set this before push_template_decl. */
  856. if (TREE_CODE (value) == VAR_DECL)
  857. DECL_CONTEXT (value) = current_class_type;
  858. if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value))
  859. {
  860. value = push_template_decl (value);
  861. if (error_operand_p (value))
  862. return error_mark_node;
  863. }
  864. if (attrlist)
  865. cplus_decl_attributes (&value, attrlist, 0);
  866. if (init && DIRECT_LIST_INIT_P (init))
  867. flags = LOOKUP_NORMAL;
  868. else
  869. flags = LOOKUP_IMPLICIT;
  870. switch (TREE_CODE (value))
  871. {
  872. case VAR_DECL:
  873. finish_static_data_member_decl (value, init, init_const_expr_p,
  874. asmspec_tree, flags);
  875. return value;
  876. case FIELD_DECL:
  877. if (asmspec)
  878. error ("%<asm%> specifiers are not permitted on non-static data members");
  879. if (DECL_INITIAL (value) == error_mark_node)
  880. init = error_mark_node;
  881. cp_finish_decl (value, init, /*init_const_expr_p=*/false,
  882. NULL_TREE, flags);
  883. DECL_IN_AGGR_P (value) = 1;
  884. return value;
  885. case FUNCTION_DECL:
  886. if (asmspec)
  887. set_user_assembler_name (value, asmspec);
  888. cp_finish_decl (value,
  889. /*init=*/NULL_TREE,
  890. /*init_const_expr_p=*/false,
  891. asmspec_tree, flags);
  892. /* Pass friends back this way. */
  893. if (DECL_FRIEND_P (value))
  894. return void_type_node;
  895. DECL_IN_AGGR_P (value) = 1;
  896. return value;
  897. default:
  898. gcc_unreachable ();
  899. }
  900. return NULL_TREE;
  901. }
  902. /* Like `grokfield', but for bitfields.
  903. WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
  904. tree
  905. grokbitfield (const cp_declarator *declarator,
  906. cp_decl_specifier_seq *declspecs, tree width,
  907. tree attrlist)
  908. {
  909. tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist);
  910. if (value == error_mark_node)
  911. return NULL_TREE; /* friends went bad. */
  912. if (TREE_TYPE (value) == error_mark_node)
  913. return value;
  914. /* Pass friendly classes back. */
  915. if (VOID_TYPE_P (value))
  916. return void_type_node;
  917. if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value))
  918. && (POINTER_TYPE_P (value)
  919. || !dependent_type_p (TREE_TYPE (value))))
  920. {
  921. error ("bit-field %qD with non-integral type", value);
  922. return error_mark_node;
  923. }
  924. if (TREE_CODE (value) == TYPE_DECL)
  925. {
  926. error ("cannot declare %qD to be a bit-field type", value);
  927. return NULL_TREE;
  928. }
  929. /* Usually, finish_struct_1 catches bitfields with invalid types.
  930. But, in the case of bitfields with function type, we confuse
  931. ourselves into thinking they are member functions, so we must
  932. check here. */
  933. if (TREE_CODE (value) == FUNCTION_DECL)
  934. {
  935. error ("cannot declare bit-field %qD with function type",
  936. DECL_NAME (value));
  937. return NULL_TREE;
  938. }
  939. if (DECL_IN_AGGR_P (value))
  940. {
  941. error ("%qD is already defined in the class %qT", value,
  942. DECL_CONTEXT (value));
  943. return void_type_node;
  944. }
  945. if (TREE_STATIC (value))
  946. {
  947. error ("static member %qD cannot be a bit-field", value);
  948. return NULL_TREE;
  949. }
  950. cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0);
  951. if (width != error_mark_node)
  952. {
  953. /* The width must be an integer type. */
  954. if (!type_dependent_expression_p (width)
  955. && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
  956. error ("width of bit-field %qD has non-integral type %qT", value,
  957. TREE_TYPE (width));
  958. DECL_INITIAL (value) = width;
  959. SET_DECL_C_BIT_FIELD (value);
  960. }
  961. DECL_IN_AGGR_P (value) = 1;
  962. if (attrlist)
  963. cplus_decl_attributes (&value, attrlist, /*flags=*/0);
  964. return value;
  965. }
  966. /* Returns true iff ATTR is an attribute which needs to be applied at
  967. instantiation time rather than template definition time. */
  968. static bool
  969. is_late_template_attribute (tree attr, tree decl)
  970. {
  971. tree name = get_attribute_name (attr);
  972. tree args = TREE_VALUE (attr);
  973. const struct attribute_spec *spec = lookup_attribute_spec (name);
  974. tree arg;
  975. if (!spec)
  976. /* Unknown attribute. */
  977. return false;
  978. /* Attribute weak handling wants to write out assembly right away. */
  979. if (is_attribute_p ("weak", name))
  980. return true;
  981. /* Attribute unused is applied directly, as it appertains to
  982. decls. */
  983. if (is_attribute_p ("unused", name))
  984. return false;
  985. /* #pragma omp declare simd attribute needs to be always deferred. */
  986. if (flag_openmp
  987. && is_attribute_p ("omp declare simd", name))
  988. return true;
  989. /* An attribute pack is clearly dependent. */
  990. if (args && PACK_EXPANSION_P (args))
  991. return true;
  992. /* If any of the arguments are dependent expressions, we can't evaluate
  993. the attribute until instantiation time. */
  994. for (arg = args; arg; arg = TREE_CHAIN (arg))
  995. {
  996. tree t = TREE_VALUE (arg);
  997. /* If the first attribute argument is an identifier, only consider
  998. second and following arguments. Attributes like mode, format,
  999. cleanup and several target specific attributes aren't late
  1000. just because they have an IDENTIFIER_NODE as first argument. */
  1001. if (arg == args && identifier_p (t))
  1002. continue;
  1003. if (value_dependent_expression_p (t)
  1004. || type_dependent_expression_p (t))
  1005. return true;
  1006. }
  1007. if (TREE_CODE (decl) == TYPE_DECL
  1008. || TYPE_P (decl)
  1009. || spec->type_required)
  1010. {
  1011. tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl);
  1012. /* We can't apply any attributes to a completely unknown type until
  1013. instantiation time. */
  1014. enum tree_code code = TREE_CODE (type);
  1015. if (code == TEMPLATE_TYPE_PARM
  1016. || code == BOUND_TEMPLATE_TEMPLATE_PARM
  1017. || code == TYPENAME_TYPE)
  1018. return true;
  1019. /* Also defer most attributes on dependent types. This is not
  1020. necessary in all cases, but is the better default. */
  1021. else if (dependent_type_p (type)
  1022. /* But some attributes specifically apply to templates. */
  1023. && !is_attribute_p ("abi_tag", name)
  1024. && !is_attribute_p ("deprecated", name)
  1025. && !is_attribute_p ("visibility", name))
  1026. return true;
  1027. else
  1028. return false;
  1029. }
  1030. else
  1031. return false;
  1032. }
  1033. /* ATTR_P is a list of attributes. Remove any attributes which need to be
  1034. applied at instantiation time and return them. If IS_DEPENDENT is true,
  1035. the declaration itself is dependent, so all attributes should be applied
  1036. at instantiation time. */
  1037. static tree
  1038. splice_template_attributes (tree *attr_p, tree decl)
  1039. {
  1040. tree *p = attr_p;
  1041. tree late_attrs = NULL_TREE;
  1042. tree *q = &late_attrs;
  1043. if (!p)
  1044. return NULL_TREE;
  1045. for (; *p; )
  1046. {
  1047. if (is_late_template_attribute (*p, decl))
  1048. {
  1049. ATTR_IS_DEPENDENT (*p) = 1;
  1050. *q = *p;
  1051. *p = TREE_CHAIN (*p);
  1052. q = &TREE_CHAIN (*q);
  1053. *q = NULL_TREE;
  1054. }
  1055. else
  1056. p = &TREE_CHAIN (*p);
  1057. }
  1058. return late_attrs;
  1059. }
  1060. /* Remove any late attributes from the list in ATTR_P and attach them to
  1061. DECL_P. */
  1062. static void
  1063. save_template_attributes (tree *attr_p, tree *decl_p)
  1064. {
  1065. tree *q;
  1066. if (attr_p && *attr_p == error_mark_node)
  1067. return;
  1068. tree late_attrs = splice_template_attributes (attr_p, *decl_p);
  1069. if (!late_attrs)
  1070. return;
  1071. if (DECL_P (*decl_p))
  1072. q = &DECL_ATTRIBUTES (*decl_p);
  1073. else
  1074. q = &TYPE_ATTRIBUTES (*decl_p);
  1075. tree old_attrs = *q;
  1076. /* Merge the late attributes at the beginning with the attribute
  1077. list. */
  1078. late_attrs = merge_attributes (late_attrs, *q);
  1079. *q = late_attrs;
  1080. if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p))
  1081. {
  1082. /* We've added new attributes directly to the main variant, so
  1083. now we need to update all of the other variants to include
  1084. these new attributes. */
  1085. tree variant;
  1086. for (variant = TYPE_NEXT_VARIANT (*decl_p); variant;
  1087. variant = TYPE_NEXT_VARIANT (variant))
  1088. {
  1089. gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs);
  1090. TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p);
  1091. }
  1092. }
  1093. }
  1094. /* Return true iff ATTRS are acceptable attributes to be applied in-place
  1095. to a typedef which gives a previously anonymous class or enum a name for
  1096. linkage purposes. */
  1097. bool
  1098. attributes_naming_typedef_ok (tree attrs)
  1099. {
  1100. for (; attrs; attrs = TREE_CHAIN (attrs))
  1101. {
  1102. tree name = get_attribute_name (attrs);
  1103. if (is_attribute_p ("vector_size", name))
  1104. return false;
  1105. }
  1106. return true;
  1107. }
  1108. /* Like reconstruct_complex_type, but handle also template trees. */
  1109. tree
  1110. cp_reconstruct_complex_type (tree type, tree bottom)
  1111. {
  1112. tree inner, outer;
  1113. bool late_return_type_p = false;
  1114. if (TYPE_PTR_P (type))
  1115. {
  1116. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1117. outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
  1118. TYPE_REF_CAN_ALIAS_ALL (type));
  1119. }
  1120. else if (TREE_CODE (type) == REFERENCE_TYPE)
  1121. {
  1122. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1123. outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
  1124. TYPE_REF_CAN_ALIAS_ALL (type));
  1125. }
  1126. else if (TREE_CODE (type) == ARRAY_TYPE)
  1127. {
  1128. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1129. outer = build_cplus_array_type (inner, TYPE_DOMAIN (type));
  1130. /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the
  1131. element type qualification will be handled by the recursive
  1132. cp_reconstruct_complex_type call and cp_build_qualified_type
  1133. for ARRAY_TYPEs changes the element type. */
  1134. return outer;
  1135. }
  1136. else if (TREE_CODE (type) == FUNCTION_TYPE)
  1137. {
  1138. late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
  1139. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1140. outer = build_function_type (inner, TYPE_ARG_TYPES (type));
  1141. outer = apply_memfn_quals (outer,
  1142. type_memfn_quals (type),
  1143. type_memfn_rqual (type));
  1144. }
  1145. else if (TREE_CODE (type) == METHOD_TYPE)
  1146. {
  1147. late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type);
  1148. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1149. /* The build_method_type_directly() routine prepends 'this' to argument list,
  1150. so we must compensate by getting rid of it. */
  1151. outer
  1152. = build_method_type_directly
  1153. (class_of_this_parm (type), inner,
  1154. TREE_CHAIN (TYPE_ARG_TYPES (type)));
  1155. }
  1156. else if (TREE_CODE (type) == OFFSET_TYPE)
  1157. {
  1158. inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom);
  1159. outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
  1160. }
  1161. else
  1162. return bottom;
  1163. if (TYPE_ATTRIBUTES (type))
  1164. outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type));
  1165. outer = cp_build_qualified_type (outer, cp_type_quals (type));
  1166. if (late_return_type_p)
  1167. TYPE_HAS_LATE_RETURN_TYPE (outer) = 1;
  1168. return outer;
  1169. }
  1170. /* Replaces any constexpr expression that may be into the attributes
  1171. arguments with their reduced value. */
  1172. static void
  1173. cp_check_const_attributes (tree attributes)
  1174. {
  1175. if (attributes == error_mark_node)
  1176. return;
  1177. tree attr;
  1178. for (attr = attributes; attr; attr = TREE_CHAIN (attr))
  1179. {
  1180. tree arg;
  1181. for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg))
  1182. {
  1183. tree expr = TREE_VALUE (arg);
  1184. if (EXPR_P (expr))
  1185. TREE_VALUE (arg) = maybe_constant_value (expr);
  1186. }
  1187. }
  1188. }
  1189. /* Return true if TYPE is an OpenMP mappable type. */
  1190. bool
  1191. cp_omp_mappable_type (tree type)
  1192. {
  1193. /* Mappable type has to be complete. */
  1194. if (type == error_mark_node || !COMPLETE_TYPE_P (type))
  1195. return false;
  1196. /* Arrays have mappable type if the elements have mappable type. */
  1197. while (TREE_CODE (type) == ARRAY_TYPE)
  1198. type = TREE_TYPE (type);
  1199. /* A mappable type cannot contain virtual members. */
  1200. if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type))
  1201. return false;
  1202. /* All data members must be non-static. */
  1203. if (CLASS_TYPE_P (type))
  1204. {
  1205. tree field;
  1206. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  1207. if (TREE_CODE (field) == VAR_DECL)
  1208. return false;
  1209. /* All fields must have mappable types. */
  1210. else if (TREE_CODE (field) == FIELD_DECL
  1211. && !cp_omp_mappable_type (TREE_TYPE (field)))
  1212. return false;
  1213. }
  1214. return true;
  1215. }
  1216. /* Like decl_attributes, but handle C++ complexity. */
  1217. void
  1218. cplus_decl_attributes (tree *decl, tree attributes, int flags)
  1219. {
  1220. if (*decl == NULL_TREE || *decl == void_type_node
  1221. || *decl == error_mark_node)
  1222. return;
  1223. /* Add implicit "omp declare target" attribute if requested. */
  1224. if (scope_chain->omp_declare_target_attribute
  1225. && ((TREE_CODE (*decl) == VAR_DECL
  1226. && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl)))
  1227. || TREE_CODE (*decl) == FUNCTION_DECL))
  1228. {
  1229. if (TREE_CODE (*decl) == VAR_DECL
  1230. && DECL_CLASS_SCOPE_P (*decl))
  1231. error ("%q+D static data member inside of declare target directive",
  1232. *decl);
  1233. else if (TREE_CODE (*decl) == VAR_DECL
  1234. && (DECL_FUNCTION_SCOPE_P (*decl)
  1235. || (current_function_decl && !DECL_EXTERNAL (*decl))))
  1236. error ("%q+D in block scope inside of declare target directive",
  1237. *decl);
  1238. else if (!processing_template_decl
  1239. && TREE_CODE (*decl) == VAR_DECL
  1240. && !cp_omp_mappable_type (TREE_TYPE (*decl)))
  1241. error ("%q+D in declare target directive does not have mappable type",
  1242. *decl);
  1243. else
  1244. attributes = tree_cons (get_identifier ("omp declare target"),
  1245. NULL_TREE, attributes);
  1246. }
  1247. if (processing_template_decl)
  1248. {
  1249. if (check_for_bare_parameter_packs (attributes))
  1250. return;
  1251. save_template_attributes (&attributes, decl);
  1252. }
  1253. cp_check_const_attributes (attributes);
  1254. if (TREE_CODE (*decl) == TEMPLATE_DECL)
  1255. decl = &DECL_TEMPLATE_RESULT (*decl);
  1256. if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl)))
  1257. {
  1258. attributes
  1259. = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT);
  1260. decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)),
  1261. attributes, flags);
  1262. }
  1263. else
  1264. decl_attributes (decl, attributes, flags);
  1265. if (TREE_CODE (*decl) == TYPE_DECL)
  1266. SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
  1267. /* Propagate deprecation out to the template. */
  1268. if (TREE_DEPRECATED (*decl))
  1269. if (tree ti = get_template_info (*decl))
  1270. {
  1271. tree tmpl = TI_TEMPLATE (ti);
  1272. tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl)
  1273. : DECL_TEMPLATE_RESULT (tmpl));
  1274. if (*decl == pattern)
  1275. TREE_DEPRECATED (tmpl) = true;
  1276. }
  1277. }
  1278. /* Walks through the namespace- or function-scope anonymous union
  1279. OBJECT, with the indicated TYPE, building appropriate VAR_DECLs.
  1280. Returns one of the fields for use in the mangled name. */
  1281. static tree
  1282. build_anon_union_vars (tree type, tree object)
  1283. {
  1284. tree main_decl = NULL_TREE;
  1285. tree field;
  1286. /* Rather than write the code to handle the non-union case,
  1287. just give an error. */
  1288. if (TREE_CODE (type) != UNION_TYPE)
  1289. {
  1290. error ("anonymous struct not inside named type");
  1291. return error_mark_node;
  1292. }
  1293. for (field = TYPE_FIELDS (type);
  1294. field != NULL_TREE;
  1295. field = DECL_CHAIN (field))
  1296. {
  1297. tree decl;
  1298. tree ref;
  1299. if (DECL_ARTIFICIAL (field))
  1300. continue;
  1301. if (TREE_CODE (field) != FIELD_DECL)
  1302. {
  1303. permerror (input_location, "%q+#D invalid; an anonymous union can only "
  1304. "have non-static data members", field);
  1305. continue;
  1306. }
  1307. if (TREE_PRIVATE (field))
  1308. permerror (input_location, "private member %q+#D in anonymous union", field);
  1309. else if (TREE_PROTECTED (field))
  1310. permerror (input_location, "protected member %q+#D in anonymous union", field);
  1311. if (processing_template_decl)
  1312. ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object,
  1313. DECL_NAME (field), NULL_TREE);
  1314. else
  1315. ref = build_class_member_access_expr (object, field, NULL_TREE,
  1316. false, tf_warning_or_error);
  1317. if (DECL_NAME (field))
  1318. {
  1319. tree base;
  1320. decl = build_decl (input_location,
  1321. VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
  1322. DECL_ANON_UNION_VAR_P (decl) = 1;
  1323. DECL_ARTIFICIAL (decl) = 1;
  1324. base = get_base_address (object);
  1325. TREE_PUBLIC (decl) = TREE_PUBLIC (base);
  1326. TREE_STATIC (decl) = TREE_STATIC (base);
  1327. DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);
  1328. SET_DECL_VALUE_EXPR (decl, ref);
  1329. DECL_HAS_VALUE_EXPR_P (decl) = 1;
  1330. decl = pushdecl (decl);
  1331. }
  1332. else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
  1333. decl = build_anon_union_vars (TREE_TYPE (field), ref);
  1334. else
  1335. decl = 0;
  1336. if (main_decl == NULL_TREE)
  1337. main_decl = decl;
  1338. }
  1339. return main_decl;
  1340. }
  1341. /* Finish off the processing of a UNION_TYPE structure. If the union is an
  1342. anonymous union, then all members must be laid out together. PUBLIC_P
  1343. is nonzero if this union is not declared static. */
  1344. void
  1345. finish_anon_union (tree anon_union_decl)
  1346. {
  1347. tree type;
  1348. tree main_decl;
  1349. bool public_p;
  1350. if (anon_union_decl == error_mark_node)
  1351. return;
  1352. type = TREE_TYPE (anon_union_decl);
  1353. public_p = TREE_PUBLIC (anon_union_decl);
  1354. /* The VAR_DECL's context is the same as the TYPE's context. */
  1355. DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
  1356. if (TYPE_FIELDS (type) == NULL_TREE)
  1357. return;
  1358. if (public_p)
  1359. {
  1360. error ("namespace-scope anonymous aggregates must be static");
  1361. return;
  1362. }
  1363. main_decl = build_anon_union_vars (type, anon_union_decl);
  1364. if (main_decl == error_mark_node)
  1365. return;
  1366. if (main_decl == NULL_TREE)
  1367. {
  1368. warning (0, "anonymous union with no members");
  1369. return;
  1370. }
  1371. if (!processing_template_decl)
  1372. {
  1373. /* Use main_decl to set the mangled name. */
  1374. DECL_NAME (anon_union_decl) = DECL_NAME (main_decl);
  1375. maybe_commonize_var (anon_union_decl);
  1376. if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl))
  1377. mangle_decl (anon_union_decl);
  1378. DECL_NAME (anon_union_decl) = NULL_TREE;
  1379. }
  1380. pushdecl (anon_union_decl);
  1381. cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
  1382. }
  1383. /* Auxiliary functions to make type signatures for
  1384. `operator new' and `operator delete' correspond to
  1385. what compiler will be expecting. */
  1386. tree
  1387. coerce_new_type (tree type)
  1388. {
  1389. int e = 0;
  1390. tree args = TYPE_ARG_TYPES (type);
  1391. gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
  1392. if (!same_type_p (TREE_TYPE (type), ptr_type_node))
  1393. {
  1394. e = 1;
  1395. error ("%<operator new%> must return type %qT", ptr_type_node);
  1396. }
  1397. if (args && args != void_list_node)
  1398. {
  1399. if (TREE_PURPOSE (args))
  1400. {
  1401. /* [basic.stc.dynamic.allocation]
  1402. The first parameter shall not have an associated default
  1403. argument. */
  1404. error ("the first parameter of %<operator new%> cannot "
  1405. "have a default argument");
  1406. /* Throw away the default argument. */
  1407. TREE_PURPOSE (args) = NULL_TREE;
  1408. }
  1409. if (!same_type_p (TREE_VALUE (args), size_type_node))
  1410. {
  1411. e = 2;
  1412. args = TREE_CHAIN (args);
  1413. }
  1414. }
  1415. else
  1416. e = 2;
  1417. if (e == 2)
  1418. permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) "
  1419. "as first parameter", size_type_node);
  1420. switch (e)
  1421. {
  1422. case 2:
  1423. args = tree_cons (NULL_TREE, size_type_node, args);
  1424. /* Fall through. */
  1425. case 1:
  1426. type = build_exception_variant
  1427. (build_function_type (ptr_type_node, args),
  1428. TYPE_RAISES_EXCEPTIONS (type));
  1429. /* Fall through. */
  1430. default:;
  1431. }
  1432. return type;
  1433. }
  1434. tree
  1435. coerce_delete_type (tree type)
  1436. {
  1437. int e = 0;
  1438. tree args = TYPE_ARG_TYPES (type);
  1439. gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
  1440. if (!same_type_p (TREE_TYPE (type), void_type_node))
  1441. {
  1442. e = 1;
  1443. error ("%<operator delete%> must return type %qT", void_type_node);
  1444. }
  1445. if (!args || args == void_list_node
  1446. || !same_type_p (TREE_VALUE (args), ptr_type_node))
  1447. {
  1448. e = 2;
  1449. if (args && args != void_list_node)
  1450. args = TREE_CHAIN (args);
  1451. error ("%<operator delete%> takes type %qT as first parameter",
  1452. ptr_type_node);
  1453. }
  1454. switch (e)
  1455. {
  1456. case 2:
  1457. args = tree_cons (NULL_TREE, ptr_type_node, args);
  1458. /* Fall through. */
  1459. case 1:
  1460. type = build_exception_variant
  1461. (build_function_type (void_type_node, args),
  1462. TYPE_RAISES_EXCEPTIONS (type));
  1463. /* Fall through. */
  1464. default:;
  1465. }
  1466. return type;
  1467. }
  1468. /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable
  1469. and mark them as needed. */
  1470. static void
  1471. mark_vtable_entries (tree decl)
  1472. {
  1473. tree fnaddr;
  1474. unsigned HOST_WIDE_INT idx;
  1475. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
  1476. idx, fnaddr)
  1477. {
  1478. tree fn;
  1479. STRIP_NOPS (fnaddr);
  1480. if (TREE_CODE (fnaddr) != ADDR_EXPR
  1481. && TREE_CODE (fnaddr) != FDESC_EXPR)
  1482. /* This entry is an offset: a virtual base class offset, a
  1483. virtual call offset, an RTTI offset, etc. */
  1484. continue;
  1485. fn = TREE_OPERAND (fnaddr, 0);
  1486. TREE_ADDRESSABLE (fn) = 1;
  1487. /* When we don't have vcall offsets, we output thunks whenever
  1488. we output the vtables that contain them. With vcall offsets,
  1489. we know all the thunks we'll need when we emit a virtual
  1490. function, so we emit the thunks there instead. */
  1491. if (DECL_THUNK_P (fn))
  1492. use_thunk (fn, /*emit_p=*/0);
  1493. mark_used (fn);
  1494. }
  1495. }
  1496. /* Set DECL up to have the closest approximation of "initialized common"
  1497. linkage available. */
  1498. void
  1499. comdat_linkage (tree decl)
  1500. {
  1501. if (flag_weak)
  1502. make_decl_one_only (decl, cxx_comdat_group (decl));
  1503. else if (TREE_CODE (decl) == FUNCTION_DECL
  1504. || (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
  1505. /* We can just emit function and compiler-generated variables
  1506. statically; having multiple copies is (for the most part) only
  1507. a waste of space.
  1508. There are two correctness issues, however: the address of a
  1509. template instantiation with external linkage should be the
  1510. same, independent of what translation unit asks for the
  1511. address, and this will not hold when we emit multiple copies of
  1512. the function. However, there's little else we can do.
  1513. Also, by default, the typeinfo implementation assumes that
  1514. there will be only one copy of the string used as the name for
  1515. each type. Therefore, if weak symbols are unavailable, the
  1516. run-time library should perform a more conservative check; it
  1517. should perform a string comparison, rather than an address
  1518. comparison. */
  1519. TREE_PUBLIC (decl) = 0;
  1520. else
  1521. {
  1522. /* Static data member template instantiations, however, cannot
  1523. have multiple copies. */
  1524. if (DECL_INITIAL (decl) == 0
  1525. || DECL_INITIAL (decl) == error_mark_node)
  1526. DECL_COMMON (decl) = 1;
  1527. else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
  1528. {
  1529. DECL_COMMON (decl) = 1;
  1530. DECL_INITIAL (decl) = error_mark_node;
  1531. }
  1532. else if (!DECL_EXPLICIT_INSTANTIATION (decl))
  1533. {
  1534. /* We can't do anything useful; leave vars for explicit
  1535. instantiation. */
  1536. DECL_EXTERNAL (decl) = 1;
  1537. DECL_NOT_REALLY_EXTERN (decl) = 0;
  1538. }
  1539. }
  1540. DECL_COMDAT (decl) = 1;
  1541. }
  1542. /* For win32 we also want to put explicit instantiations in
  1543. linkonce sections, so that they will be merged with implicit
  1544. instantiations; otherwise we get duplicate symbol errors.
  1545. For Darwin we do not want explicit instantiations to be
  1546. linkonce. */
  1547. void
  1548. maybe_make_one_only (tree decl)
  1549. {
  1550. /* We used to say that this was not necessary on targets that support weak
  1551. symbols, because the implicit instantiations will defer to the explicit
  1552. one. However, that's not actually the case in SVR4; a strong definition
  1553. after a weak one is an error. Also, not making explicit
  1554. instantiations one_only means that we can end up with two copies of
  1555. some template instantiations. */
  1556. if (! flag_weak)
  1557. return;
  1558. /* We can't set DECL_COMDAT on functions, or cp_finish_file will think
  1559. we can get away with not emitting them if they aren't used. We need
  1560. to for variables so that cp_finish_decl will update their linkage,
  1561. because their DECL_INITIAL may not have been set properly yet. */
  1562. if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC
  1563. || (! DECL_EXPLICIT_INSTANTIATION (decl)
  1564. && ! DECL_TEMPLATE_SPECIALIZATION (decl)))
  1565. {
  1566. make_decl_one_only (decl, cxx_comdat_group (decl));
  1567. if (VAR_P (decl))
  1568. {
  1569. varpool_node *node = varpool_node::get_create (decl);
  1570. DECL_COMDAT (decl) = 1;
  1571. /* Mark it needed so we don't forget to emit it. */
  1572. node->forced_by_abi = true;
  1573. TREE_USED (decl) = 1;
  1574. }
  1575. }
  1576. }
  1577. /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage.
  1578. This predicate will give the right answer during parsing of the
  1579. function, which other tests may not. */
  1580. bool
  1581. vague_linkage_p (tree decl)
  1582. {
  1583. /* Unfortunately, import_export_decl has not always been called
  1584. before the function is processed, so we cannot simply check
  1585. DECL_COMDAT. */
  1586. if (DECL_COMDAT (decl)
  1587. || (((TREE_CODE (decl) == FUNCTION_DECL
  1588. && DECL_DECLARED_INLINE_P (decl))
  1589. || (DECL_LANG_SPECIFIC (decl)
  1590. && DECL_TEMPLATE_INSTANTIATION (decl)))
  1591. && TREE_PUBLIC (decl)))
  1592. return true;
  1593. else if (DECL_FUNCTION_SCOPE_P (decl))
  1594. /* A local static in an inline effectively has vague linkage. */
  1595. return (TREE_STATIC (decl)
  1596. && vague_linkage_p (DECL_CONTEXT (decl)));
  1597. else
  1598. return false;
  1599. }
  1600. /* Determine whether or not we want to specifically import or export CTYPE,
  1601. using various heuristics. */
  1602. static void
  1603. import_export_class (tree ctype)
  1604. {
  1605. /* -1 for imported, 1 for exported. */
  1606. int import_export = 0;
  1607. /* It only makes sense to call this function at EOF. The reason is
  1608. that this function looks at whether or not the first non-inline
  1609. non-abstract virtual member function has been defined in this
  1610. translation unit. But, we can't possibly know that until we've
  1611. seen the entire translation unit. */
  1612. gcc_assert (at_eof);
  1613. if (CLASSTYPE_INTERFACE_KNOWN (ctype))
  1614. return;
  1615. /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface,
  1616. we will have CLASSTYPE_INTERFACE_ONLY set but not
  1617. CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
  1618. heuristic because someone will supply a #pragma implementation
  1619. elsewhere, and deducing it here would produce a conflict. */
  1620. if (CLASSTYPE_INTERFACE_ONLY (ctype))
  1621. return;
  1622. if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
  1623. import_export = -1;
  1624. else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
  1625. import_export = 1;
  1626. else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
  1627. && !flag_implicit_templates)
  1628. /* For a template class, without -fimplicit-templates, check the
  1629. repository. If the virtual table is assigned to this
  1630. translation unit, then export the class; otherwise, import
  1631. it. */
  1632. import_export = repo_export_class_p (ctype) ? 1 : -1;
  1633. else if (TYPE_POLYMORPHIC_P (ctype))
  1634. {
  1635. /* The ABI specifies that the virtual table and associated
  1636. information are emitted with the key method, if any. */
  1637. tree method = CLASSTYPE_KEY_METHOD (ctype);
  1638. /* If weak symbol support is not available, then we must be
  1639. careful not to emit the vtable when the key function is
  1640. inline. An inline function can be defined in multiple
  1641. translation units. If we were to emit the vtable in each
  1642. translation unit containing a definition, we would get
  1643. multiple definition errors at link-time. */
  1644. if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method)))
  1645. import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
  1646. }
  1647. /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing
  1648. a definition anywhere else. */
  1649. if (MULTIPLE_SYMBOL_SPACES && import_export == -1)
  1650. import_export = 0;
  1651. /* Allow back ends the chance to overrule the decision. */
  1652. if (targetm.cxx.import_export_class)
  1653. import_export = targetm.cxx.import_export_class (ctype, import_export);
  1654. if (import_export)
  1655. {
  1656. SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
  1657. CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
  1658. }
  1659. }
  1660. /* Return true if VAR has already been provided to the back end; in that
  1661. case VAR should not be modified further by the front end. */
  1662. static bool
  1663. var_finalized_p (tree var)
  1664. {
  1665. return varpool_node::get_create (var)->definition;
  1666. }
  1667. /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason,
  1668. must be emitted in this translation unit. Mark it as such. */
  1669. void
  1670. mark_needed (tree decl)
  1671. {
  1672. TREE_USED (decl) = 1;
  1673. if (TREE_CODE (decl) == FUNCTION_DECL)
  1674. {
  1675. /* Extern inline functions don't become needed when referenced.
  1676. If we know a method will be emitted in other TU and no new
  1677. functions can be marked reachable, just use the external
  1678. definition. */
  1679. struct cgraph_node *node = cgraph_node::get_create (decl);
  1680. node->forced_by_abi = true;
  1681. /* #pragma interface and -frepo code can call mark_needed for
  1682. maybe-in-charge 'tors; mark the clones as well. */
  1683. tree clone;
  1684. FOR_EACH_CLONE (clone, decl)
  1685. mark_needed (clone);
  1686. }
  1687. else if (TREE_CODE (decl) == VAR_DECL)
  1688. {
  1689. varpool_node *node = varpool_node::get_create (decl);
  1690. /* C++ frontend use mark_decl_references to force COMDAT variables
  1691. to be output that might appear dead otherwise. */
  1692. node->forced_by_abi = true;
  1693. }
  1694. }
  1695. /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function
  1696. returns true if a definition of this entity should be provided in
  1697. this object file. Callers use this function to determine whether
  1698. or not to let the back end know that a definition of DECL is
  1699. available in this translation unit. */
  1700. bool
  1701. decl_needed_p (tree decl)
  1702. {
  1703. gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
  1704. /* This function should only be called at the end of the translation
  1705. unit. We cannot be sure of whether or not something will be
  1706. COMDAT until that point. */
  1707. gcc_assert (at_eof);
  1708. /* All entities with external linkage that are not COMDAT/EXTERN should be
  1709. emitted; they may be referred to from other object files. */
  1710. if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl))
  1711. return true;
  1712. /* Functions marked "dllexport" must be emitted so that they are
  1713. visible to other DLLs. */
  1714. if (flag_keep_inline_dllexport
  1715. && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
  1716. return true;
  1717. /* When not optimizing, do not bother to produce definitions for extern
  1718. symbols. */
  1719. if (DECL_REALLY_EXTERN (decl)
  1720. && ((TREE_CODE (decl) != FUNCTION_DECL
  1721. && !optimize)
  1722. || (TREE_CODE (decl) == FUNCTION_DECL
  1723. && !opt_for_fn (decl, optimize)))
  1724. && !lookup_attribute ("always_inline", decl))
  1725. return false;
  1726. /* If this entity was used, let the back end see it; it will decide
  1727. whether or not to emit it into the object file. */
  1728. if (TREE_USED (decl))
  1729. return true;
  1730. /* Virtual functions might be needed for devirtualization. */
  1731. if (flag_devirtualize
  1732. && TREE_CODE (decl) == FUNCTION_DECL
  1733. && DECL_VIRTUAL_P (decl))
  1734. return true;
  1735. /* Otherwise, DECL does not need to be emitted -- yet. A subsequent
  1736. reference to DECL might cause it to be emitted later. */
  1737. return false;
  1738. }
  1739. /* If necessary, write out the vtables for the dynamic class CTYPE.
  1740. Returns true if any vtables were emitted. */
  1741. static bool
  1742. maybe_emit_vtables (tree ctype)
  1743. {
  1744. tree vtbl;
  1745. tree primary_vtbl;
  1746. int needed = 0;
  1747. varpool_node *current = NULL, *last = NULL;
  1748. /* If the vtables for this class have already been emitted there is
  1749. nothing more to do. */
  1750. primary_vtbl = CLASSTYPE_VTABLES (ctype);
  1751. if (var_finalized_p (primary_vtbl))
  1752. return false;
  1753. /* Ignore dummy vtables made by get_vtable_decl. */
  1754. if (TREE_TYPE (primary_vtbl) == void_type_node)
  1755. return false;
  1756. /* On some targets, we cannot determine the key method until the end
  1757. of the translation unit -- which is when this function is
  1758. called. */
  1759. if (!targetm.cxx.key_method_may_be_inline ())
  1760. determine_key_method (ctype);
  1761. /* See if any of the vtables are needed. */
  1762. for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
  1763. {
  1764. import_export_decl (vtbl);
  1765. if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl))
  1766. needed = 1;
  1767. }
  1768. if (!needed)
  1769. {
  1770. /* If the references to this class' vtables are optimized away,
  1771. still emit the appropriate debugging information. See
  1772. dfs_debug_mark. */
  1773. if (DECL_COMDAT (primary_vtbl)
  1774. && CLASSTYPE_DEBUG_REQUESTED (ctype))
  1775. note_debug_info_needed (ctype);
  1776. return false;
  1777. }
  1778. /* The ABI requires that we emit all of the vtables if we emit any
  1779. of them. */
  1780. for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl))
  1781. {
  1782. /* Mark entities references from the virtual table as used. */
  1783. mark_vtable_entries (vtbl);
  1784. if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0)
  1785. {
  1786. vec<tree, va_gc> *cleanups = NULL;
  1787. tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups,
  1788. LOOKUP_NORMAL);
  1789. /* It had better be all done at compile-time. */
  1790. gcc_assert (!expr && !cleanups);
  1791. }
  1792. /* Write it out. */
  1793. DECL_EXTERNAL (vtbl) = 0;
  1794. rest_of_decl_compilation (vtbl, 1, 1);
  1795. /* Because we're only doing syntax-checking, we'll never end up
  1796. actually marking the variable as written. */
  1797. if (flag_syntax_only)
  1798. TREE_ASM_WRITTEN (vtbl) = 1;
  1799. else if (DECL_ONE_ONLY (vtbl))
  1800. {
  1801. current = varpool_node::get_create (vtbl);
  1802. if (last)
  1803. current->add_to_same_comdat_group (last);
  1804. last = current;
  1805. }
  1806. }
  1807. /* Since we're writing out the vtable here, also write the debug
  1808. info. */
  1809. note_debug_info_needed (ctype);
  1810. return true;
  1811. }
  1812. /* A special return value from type_visibility meaning internal
  1813. linkage. */
  1814. enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 };
  1815. /* walk_tree helper function for type_visibility. */
  1816. static tree
  1817. min_vis_r (tree *tp, int *walk_subtrees, void *data)
  1818. {
  1819. int *vis_p = (int *)data;
  1820. if (! TYPE_P (*tp))
  1821. {
  1822. *walk_subtrees = 0;
  1823. }
  1824. else if (OVERLOAD_TYPE_P (*tp)
  1825. && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp)))
  1826. {
  1827. *vis_p = VISIBILITY_ANON;
  1828. return *tp;
  1829. }
  1830. else if (CLASS_TYPE_P (*tp)
  1831. && CLASSTYPE_VISIBILITY (*tp) > *vis_p)
  1832. *vis_p = CLASSTYPE_VISIBILITY (*tp);
  1833. return NULL;
  1834. }
  1835. /* Returns the visibility of TYPE, which is the minimum visibility of its
  1836. component types. */
  1837. static int
  1838. type_visibility (tree type)
  1839. {
  1840. int vis = VISIBILITY_DEFAULT;
  1841. cp_walk_tree_without_duplicates (&type, min_vis_r, &vis);
  1842. return vis;
  1843. }
  1844. /* Limit the visibility of DECL to VISIBILITY, if not explicitly
  1845. specified (or if VISIBILITY is static). If TMPL is true, this
  1846. constraint is for a template argument, and takes precedence
  1847. over explicitly-specified visibility on the template. */
  1848. static void
  1849. constrain_visibility (tree decl, int visibility, bool tmpl)
  1850. {
  1851. if (visibility == VISIBILITY_ANON)
  1852. {
  1853. /* extern "C" declarations aren't affected by the anonymous
  1854. namespace. */
  1855. if (!DECL_EXTERN_C_P (decl))
  1856. {
  1857. TREE_PUBLIC (decl) = 0;
  1858. DECL_WEAK (decl) = 0;
  1859. DECL_COMMON (decl) = 0;
  1860. DECL_COMDAT (decl) = false;
  1861. if (TREE_CODE (decl) == FUNCTION_DECL
  1862. || TREE_CODE (decl) == VAR_DECL)
  1863. {
  1864. struct symtab_node *snode = symtab_node::get (decl);
  1865. if (snode)
  1866. snode->set_comdat_group (NULL);
  1867. }
  1868. DECL_INTERFACE_KNOWN (decl) = 1;
  1869. if (DECL_LANG_SPECIFIC (decl))
  1870. DECL_NOT_REALLY_EXTERN (decl) = 1;
  1871. }
  1872. }
  1873. else if (visibility > DECL_VISIBILITY (decl)
  1874. && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl)))
  1875. {
  1876. DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility;
  1877. /* This visibility was not specified. */
  1878. DECL_VISIBILITY_SPECIFIED (decl) = false;
  1879. }
  1880. }
  1881. /* Constrain the visibility of DECL based on the visibility of its template
  1882. arguments. */
  1883. static void
  1884. constrain_visibility_for_template (tree decl, tree targs)
  1885. {
  1886. /* If this is a template instantiation, check the innermost
  1887. template args for visibility constraints. The outer template
  1888. args are covered by the class check. */
  1889. tree args = INNERMOST_TEMPLATE_ARGS (targs);
  1890. int i;
  1891. for (i = TREE_VEC_LENGTH (args); i > 0; --i)
  1892. {
  1893. int vis = 0;
  1894. tree arg = TREE_VEC_ELT (args, i-1);
  1895. if (TYPE_P (arg))
  1896. vis = type_visibility (arg);
  1897. else
  1898. {
  1899. if (REFERENCE_REF_P (arg))
  1900. arg = TREE_OPERAND (arg, 0);
  1901. if (TREE_TYPE (arg))
  1902. STRIP_NOPS (arg);
  1903. if (TREE_CODE (arg) == ADDR_EXPR)
  1904. arg = TREE_OPERAND (arg, 0);
  1905. if (VAR_OR_FUNCTION_DECL_P (arg))
  1906. {
  1907. if (! TREE_PUBLIC (arg))
  1908. vis = VISIBILITY_ANON;
  1909. else
  1910. vis = DECL_VISIBILITY (arg);
  1911. }
  1912. }
  1913. if (vis)
  1914. constrain_visibility (decl, vis, true);
  1915. }
  1916. }
  1917. /* Like c_determine_visibility, but with additional C++-specific
  1918. behavior.
  1919. Function-scope entities can rely on the function's visibility because
  1920. it is set in start_preparsed_function.
  1921. Class-scope entities cannot rely on the class's visibility until the end
  1922. of the enclosing class definition.
  1923. Note that because namespaces have multiple independent definitions,
  1924. namespace visibility is handled elsewhere using the #pragma visibility
  1925. machinery rather than by decorating the namespace declaration.
  1926. The goal is for constraints from the type to give a diagnostic, and
  1927. other constraints to be applied silently. */
  1928. void
  1929. determine_visibility (tree decl)
  1930. {
  1931. tree class_type = NULL_TREE;
  1932. bool use_template;
  1933. bool orig_visibility_specified;
  1934. enum symbol_visibility orig_visibility;
  1935. /* Remember that all decls get VISIBILITY_DEFAULT when built. */
  1936. /* Only relevant for names with external linkage. */
  1937. if (!TREE_PUBLIC (decl))
  1938. return;
  1939. /* Cloned constructors and destructors get the same visibility as
  1940. the underlying function. That should be set up in
  1941. maybe_clone_body. */
  1942. gcc_assert (!DECL_CLONED_FUNCTION_P (decl));
  1943. orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl);
  1944. orig_visibility = DECL_VISIBILITY (decl);
  1945. if (TREE_CODE (decl) == TYPE_DECL)
  1946. {
  1947. if (CLASS_TYPE_P (TREE_TYPE (decl)))
  1948. use_template = CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl));
  1949. else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
  1950. use_template = 1;
  1951. else
  1952. use_template = 0;
  1953. }
  1954. else if (DECL_LANG_SPECIFIC (decl))
  1955. use_template = DECL_USE_TEMPLATE (decl);
  1956. else
  1957. use_template = 0;
  1958. /* If DECL is a member of a class, visibility specifiers on the
  1959. class can influence the visibility of the DECL. */
  1960. if (DECL_CLASS_SCOPE_P (decl))
  1961. class_type = DECL_CONTEXT (decl);
  1962. else
  1963. {
  1964. /* Not a class member. */
  1965. /* Virtual tables have DECL_CONTEXT set to their associated class,
  1966. so they are automatically handled above. */
  1967. gcc_assert (!VAR_P (decl)
  1968. || !DECL_VTABLE_OR_VTT_P (decl));
  1969. if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl))
  1970. {
  1971. /* Local statics and classes get the visibility of their
  1972. containing function by default, except that
  1973. -fvisibility-inlines-hidden doesn't affect them. */
  1974. tree fn = DECL_CONTEXT (decl);
  1975. if (DECL_VISIBILITY_SPECIFIED (fn))
  1976. {
  1977. DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
  1978. DECL_VISIBILITY_SPECIFIED (decl) =
  1979. DECL_VISIBILITY_SPECIFIED (fn);
  1980. }
  1981. else
  1982. {
  1983. if (DECL_CLASS_SCOPE_P (fn))
  1984. determine_visibility_from_class (decl, DECL_CONTEXT (fn));
  1985. else if (determine_hidden_inline (fn))
  1986. {
  1987. DECL_VISIBILITY (decl) = default_visibility;
  1988. DECL_VISIBILITY_SPECIFIED (decl) =
  1989. visibility_options.inpragma;
  1990. }
  1991. else
  1992. {
  1993. DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn);
  1994. DECL_VISIBILITY_SPECIFIED (decl) =
  1995. DECL_VISIBILITY_SPECIFIED (fn);
  1996. }
  1997. }
  1998. /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set,
  1999. but have no TEMPLATE_INFO, so don't try to check it. */
  2000. use_template = 0;
  2001. }
  2002. else if (VAR_P (decl) && DECL_TINFO_P (decl)
  2003. && flag_visibility_ms_compat)
  2004. {
  2005. /* Under -fvisibility-ms-compat, types are visible by default,
  2006. even though their contents aren't. */
  2007. tree underlying_type = TREE_TYPE (DECL_NAME (decl));
  2008. int underlying_vis = type_visibility (underlying_type);
  2009. if (underlying_vis == VISIBILITY_ANON
  2010. || (CLASS_TYPE_P (underlying_type)
  2011. && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type)))
  2012. constrain_visibility (decl, underlying_vis, false);
  2013. else
  2014. DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
  2015. }
  2016. else if (VAR_P (decl) && DECL_TINFO_P (decl))
  2017. {
  2018. /* tinfo visibility is based on the type it's for. */
  2019. constrain_visibility
  2020. (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false);
  2021. /* Give the target a chance to override the visibility associated
  2022. with DECL. */
  2023. if (TREE_PUBLIC (decl)
  2024. && !DECL_REALLY_EXTERN (decl)
  2025. && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl)))
  2026. && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl))))
  2027. targetm.cxx.determine_class_data_visibility (decl);
  2028. }
  2029. else if (use_template)
  2030. /* Template instantiations and specializations get visibility based
  2031. on their template unless they override it with an attribute. */;
  2032. else if (! DECL_VISIBILITY_SPECIFIED (decl))
  2033. {
  2034. if (determine_hidden_inline (decl))
  2035. DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  2036. else
  2037. {
  2038. /* Set default visibility to whatever the user supplied with
  2039. #pragma GCC visibility or a namespace visibility attribute. */
  2040. DECL_VISIBILITY (decl) = default_visibility;
  2041. DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
  2042. }
  2043. }
  2044. }
  2045. if (use_template)
  2046. {
  2047. /* If the specialization doesn't specify visibility, use the
  2048. visibility from the template. */
  2049. tree tinfo = (TREE_CODE (decl) == TYPE_DECL
  2050. ? TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
  2051. : DECL_TEMPLATE_INFO (decl));
  2052. tree args = TI_ARGS (tinfo);
  2053. tree attribs = (TREE_CODE (decl) == TYPE_DECL
  2054. ? TYPE_ATTRIBUTES (TREE_TYPE (decl))
  2055. : DECL_ATTRIBUTES (decl));
  2056. if (args != error_mark_node)
  2057. {
  2058. tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo));
  2059. if (!DECL_VISIBILITY_SPECIFIED (decl))
  2060. {
  2061. if (!DECL_VISIBILITY_SPECIFIED (pattern)
  2062. && determine_hidden_inline (decl))
  2063. DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  2064. else
  2065. {
  2066. DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern);
  2067. DECL_VISIBILITY_SPECIFIED (decl)
  2068. = DECL_VISIBILITY_SPECIFIED (pattern);
  2069. }
  2070. }
  2071. if (args
  2072. /* Template argument visibility outweighs #pragma or namespace
  2073. visibility, but not an explicit attribute. */
  2074. && !lookup_attribute ("visibility", attribs))
  2075. {
  2076. int depth = TMPL_ARGS_DEPTH (args);
  2077. if (DECL_VISIBILITY_SPECIFIED (decl))
  2078. {
  2079. /* A class template member with explicit visibility
  2080. overrides the class visibility, so we need to apply
  2081. all the levels of template args directly. */
  2082. int i;
  2083. for (i = 1; i <= depth; ++i)
  2084. {
  2085. tree lev = TMPL_ARGS_LEVEL (args, i);
  2086. constrain_visibility_for_template (decl, lev);
  2087. }
  2088. }
  2089. else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
  2090. /* Limit visibility based on its template arguments. */
  2091. constrain_visibility_for_template (decl, args);
  2092. }
  2093. }
  2094. }
  2095. if (class_type)
  2096. determine_visibility_from_class (decl, class_type);
  2097. if (decl_anon_ns_mem_p (decl))
  2098. /* Names in an anonymous namespace get internal linkage.
  2099. This might change once we implement export. */
  2100. constrain_visibility (decl, VISIBILITY_ANON, false);
  2101. else if (TREE_CODE (decl) != TYPE_DECL)
  2102. {
  2103. /* Propagate anonymity from type to decl. */
  2104. int tvis = type_visibility (TREE_TYPE (decl));
  2105. if (tvis == VISIBILITY_ANON
  2106. || ! DECL_VISIBILITY_SPECIFIED (decl))
  2107. constrain_visibility (decl, tvis, false);
  2108. }
  2109. else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true))
  2110. /* DR 757: A type without linkage shall not be used as the type of a
  2111. variable or function with linkage, unless
  2112. o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
  2113. o the variable or function is not used (3.2 [basic.def.odr]) or is
  2114. defined in the same translation unit.
  2115. Since non-extern "C" decls need to be defined in the same
  2116. translation unit, we can make the type internal. */
  2117. constrain_visibility (decl, VISIBILITY_ANON, false);
  2118. /* If visibility changed and DECL already has DECL_RTL, ensure
  2119. symbol flags are updated. */
  2120. if ((DECL_VISIBILITY (decl) != orig_visibility
  2121. || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified)
  2122. && ((VAR_P (decl) && TREE_STATIC (decl))
  2123. || TREE_CODE (decl) == FUNCTION_DECL)
  2124. && DECL_RTL_SET_P (decl))
  2125. make_decl_rtl (decl);
  2126. }
  2127. /* By default, static data members and function members receive
  2128. the visibility of their containing class. */
  2129. static void
  2130. determine_visibility_from_class (tree decl, tree class_type)
  2131. {
  2132. if (DECL_VISIBILITY_SPECIFIED (decl))
  2133. return;
  2134. if (determine_hidden_inline (decl))
  2135. DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  2136. else
  2137. {
  2138. /* Default to the class visibility. */
  2139. DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type);
  2140. DECL_VISIBILITY_SPECIFIED (decl)
  2141. = CLASSTYPE_VISIBILITY_SPECIFIED (class_type);
  2142. }
  2143. /* Give the target a chance to override the visibility associated
  2144. with DECL. */
  2145. if (VAR_P (decl)
  2146. && (DECL_TINFO_P (decl)
  2147. || (DECL_VTABLE_OR_VTT_P (decl)
  2148. /* Construction virtual tables are not exported because
  2149. they cannot be referred to from other object files;
  2150. their name is not standardized by the ABI. */
  2151. && !DECL_CONSTRUCTION_VTABLE_P (decl)))
  2152. && TREE_PUBLIC (decl)
  2153. && !DECL_REALLY_EXTERN (decl)
  2154. && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type))
  2155. targetm.cxx.determine_class_data_visibility (decl);
  2156. }
  2157. /* Returns true iff DECL is an inline that should get hidden visibility
  2158. because of -fvisibility-inlines-hidden. */
  2159. static bool
  2160. determine_hidden_inline (tree decl)
  2161. {
  2162. return (visibility_options.inlines_hidden
  2163. /* Don't do this for inline templates; specializations might not be
  2164. inline, and we don't want them to inherit the hidden
  2165. visibility. We'll set it here for all inline instantiations. */
  2166. && !processing_template_decl
  2167. && TREE_CODE (decl) == FUNCTION_DECL
  2168. && DECL_DECLARED_INLINE_P (decl)
  2169. && (! DECL_LANG_SPECIFIC (decl)
  2170. || ! DECL_EXPLICIT_INSTANTIATION (decl)));
  2171. }
  2172. /* Constrain the visibility of a class TYPE based on the visibility of its
  2173. field types. Warn if any fields require lesser visibility. */
  2174. void
  2175. constrain_class_visibility (tree type)
  2176. {
  2177. tree binfo;
  2178. tree t;
  2179. int i;
  2180. int vis = type_visibility (type);
  2181. if (vis == VISIBILITY_ANON
  2182. || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type)))
  2183. return;
  2184. /* Don't warn about visibility if the class has explicit visibility. */
  2185. if (CLASSTYPE_VISIBILITY_SPECIFIED (type))
  2186. vis = VISIBILITY_INTERNAL;
  2187. for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
  2188. if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node)
  2189. {
  2190. tree ftype = strip_pointer_or_array_types (TREE_TYPE (t));
  2191. int subvis = type_visibility (ftype);
  2192. if (subvis == VISIBILITY_ANON)
  2193. {
  2194. if (!in_main_input_context ())
  2195. warning (0, "\
  2196. %qT has a field %qD whose type uses the anonymous namespace",
  2197. type, t);
  2198. }
  2199. else if (MAYBE_CLASS_TYPE_P (ftype)
  2200. && vis < VISIBILITY_HIDDEN
  2201. && subvis >= VISIBILITY_HIDDEN)
  2202. warning (OPT_Wattributes, "\
  2203. %qT declared with greater visibility than the type of its field %qD",
  2204. type, t);
  2205. }
  2206. binfo = TYPE_BINFO (type);
  2207. for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i)
  2208. {
  2209. int subvis = type_visibility (TREE_TYPE (t));
  2210. if (subvis == VISIBILITY_ANON)
  2211. {
  2212. if (!in_main_input_context())
  2213. warning (0, "\
  2214. %qT has a base %qT whose type uses the anonymous namespace",
  2215. type, TREE_TYPE (t));
  2216. }
  2217. else if (vis < VISIBILITY_HIDDEN
  2218. && subvis >= VISIBILITY_HIDDEN)
  2219. warning (OPT_Wattributes, "\
  2220. %qT declared with greater visibility than its base %qT",
  2221. type, TREE_TYPE (t));
  2222. }
  2223. }
  2224. /* Functions for adjusting the visibility of a tagged type and its nested
  2225. types and declarations when it gets a name for linkage purposes from a
  2226. typedef. */
  2227. static void bt_reset_linkage_1 (binding_entry, void *);
  2228. static void bt_reset_linkage_2 (binding_entry, void *);
  2229. /* First reset the visibility of all the types. */
  2230. static void
  2231. reset_type_linkage_1 (tree type)
  2232. {
  2233. set_linkage_according_to_type (type, TYPE_MAIN_DECL (type));
  2234. if (CLASS_TYPE_P (type))
  2235. binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
  2236. bt_reset_linkage_1, NULL);
  2237. }
  2238. static void
  2239. bt_reset_linkage_1 (binding_entry b, void */*data*/)
  2240. {
  2241. reset_type_linkage_1 (b->type);
  2242. }
  2243. /* Then reset the visibility of any static data members or member
  2244. functions that use those types. */
  2245. static void
  2246. reset_decl_linkage (tree decl)
  2247. {
  2248. if (TREE_PUBLIC (decl))
  2249. return;
  2250. if (DECL_CLONED_FUNCTION_P (decl))
  2251. return;
  2252. TREE_PUBLIC (decl) = true;
  2253. DECL_INTERFACE_KNOWN (decl) = false;
  2254. determine_visibility (decl);
  2255. tentative_decl_linkage (decl);
  2256. }
  2257. static void
  2258. reset_type_linkage_2 (tree type)
  2259. {
  2260. if (CLASS_TYPE_P (type))
  2261. {
  2262. if (tree vt = CLASSTYPE_VTABLES (type))
  2263. {
  2264. tree name = mangle_vtbl_for_type (type);
  2265. DECL_NAME (vt) = name;
  2266. SET_DECL_ASSEMBLER_NAME (vt, name);
  2267. reset_decl_linkage (vt);
  2268. }
  2269. if (tree ti = CLASSTYPE_TYPEINFO_VAR (type))
  2270. {
  2271. tree name = mangle_typeinfo_for_type (type);
  2272. DECL_NAME (ti) = name;
  2273. SET_DECL_ASSEMBLER_NAME (ti, name);
  2274. TREE_TYPE (name) = type;
  2275. reset_decl_linkage (ti);
  2276. }
  2277. for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m))
  2278. if (TREE_CODE (m) == VAR_DECL)
  2279. reset_decl_linkage (m);
  2280. for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m))
  2281. reset_decl_linkage (m);
  2282. binding_table_foreach (CLASSTYPE_NESTED_UTDS (type),
  2283. bt_reset_linkage_2, NULL);
  2284. }
  2285. }
  2286. static void
  2287. bt_reset_linkage_2 (binding_entry b, void */*data*/)
  2288. {
  2289. reset_type_linkage_2 (b->type);
  2290. }
  2291. void
  2292. reset_type_linkage (tree type)
  2293. {
  2294. reset_type_linkage_1 (type);
  2295. reset_type_linkage_2 (type);
  2296. }
  2297. /* Set up our initial idea of what the linkage of DECL should be. */
  2298. void
  2299. tentative_decl_linkage (tree decl)
  2300. {
  2301. if (DECL_INTERFACE_KNOWN (decl))
  2302. /* We've already made a decision as to how this function will
  2303. be handled. */;
  2304. else if (vague_linkage_p (decl))
  2305. {
  2306. if (TREE_CODE (decl) == FUNCTION_DECL
  2307. && decl_defined_p (decl))
  2308. {
  2309. DECL_EXTERNAL (decl) = 1;
  2310. DECL_NOT_REALLY_EXTERN (decl) = 1;
  2311. note_vague_linkage_fn (decl);
  2312. /* A non-template inline function with external linkage will
  2313. always be COMDAT. As we must eventually determine the
  2314. linkage of all functions, and as that causes writes to
  2315. the data mapped in from the PCH file, it's advantageous
  2316. to mark the functions at this point. */
  2317. if (DECL_DECLARED_INLINE_P (decl)
  2318. && (!DECL_IMPLICIT_INSTANTIATION (decl)
  2319. || DECL_DEFAULTED_FN (decl)))
  2320. {
  2321. /* This function must have external linkage, as
  2322. otherwise DECL_INTERFACE_KNOWN would have been
  2323. set. */
  2324. gcc_assert (TREE_PUBLIC (decl));
  2325. comdat_linkage (decl);
  2326. DECL_INTERFACE_KNOWN (decl) = 1;
  2327. }
  2328. }
  2329. else if (TREE_CODE (decl) == VAR_DECL)
  2330. maybe_commonize_var (decl);
  2331. }
  2332. }
  2333. /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage
  2334. for DECL has not already been determined, do so now by setting
  2335. DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this
  2336. function is called entities with vague linkage whose definitions
  2337. are available must have TREE_PUBLIC set.
  2338. If this function decides to place DECL in COMDAT, it will set
  2339. appropriate flags -- but will not clear DECL_EXTERNAL. It is up to
  2340. the caller to decide whether or not to clear DECL_EXTERNAL. Some
  2341. callers defer that decision until it is clear that DECL is actually
  2342. required. */
  2343. void
  2344. import_export_decl (tree decl)
  2345. {
  2346. int emit_p;
  2347. bool comdat_p;
  2348. bool import_p;
  2349. tree class_type = NULL_TREE;
  2350. if (DECL_INTERFACE_KNOWN (decl))
  2351. return;
  2352. /* We cannot determine what linkage to give to an entity with vague
  2353. linkage until the end of the file. For example, a virtual table
  2354. for a class will be defined if and only if the key method is
  2355. defined in this translation unit. As a further example, consider
  2356. that when compiling a translation unit that uses PCH file with
  2357. "-frepo" it would be incorrect to make decisions about what
  2358. entities to emit when building the PCH; those decisions must be
  2359. delayed until the repository information has been processed. */
  2360. gcc_assert (at_eof);
  2361. /* Object file linkage for explicit instantiations is handled in
  2362. mark_decl_instantiated. For static variables in functions with
  2363. vague linkage, maybe_commonize_var is used.
  2364. Therefore, the only declarations that should be provided to this
  2365. function are those with external linkage that are:
  2366. * implicit instantiations of function templates
  2367. * inline function
  2368. * implicit instantiations of static data members of class
  2369. templates
  2370. * virtual tables
  2371. * typeinfo objects
  2372. Furthermore, all entities that reach this point must have a
  2373. definition available in this translation unit.
  2374. The following assertions check these conditions. */
  2375. gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
  2376. /* Any code that creates entities with TREE_PUBLIC cleared should
  2377. also set DECL_INTERFACE_KNOWN. */
  2378. gcc_assert (TREE_PUBLIC (decl));
  2379. if (TREE_CODE (decl) == FUNCTION_DECL)
  2380. gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
  2381. || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
  2382. || DECL_DECLARED_INLINE_P (decl));
  2383. else
  2384. gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
  2385. || DECL_VTABLE_OR_VTT_P (decl)
  2386. || DECL_TINFO_P (decl));
  2387. /* Check that a definition of DECL is available in this translation
  2388. unit. */
  2389. gcc_assert (!DECL_REALLY_EXTERN (decl));
  2390. /* Assume that DECL will not have COMDAT linkage. */
  2391. comdat_p = false;
  2392. /* Assume that DECL will not be imported into this translation
  2393. unit. */
  2394. import_p = false;
  2395. /* See if the repository tells us whether or not to emit DECL in
  2396. this translation unit. */
  2397. emit_p = repo_emit_p (decl);
  2398. if (emit_p == 0)
  2399. import_p = true;
  2400. else if (emit_p == 1)
  2401. {
  2402. /* The repository indicates that this entity should be defined
  2403. here. Make sure the back end honors that request. */
  2404. mark_needed (decl);
  2405. /* Output the definition as an ordinary strong definition. */
  2406. DECL_EXTERNAL (decl) = 0;
  2407. DECL_INTERFACE_KNOWN (decl) = 1;
  2408. return;
  2409. }
  2410. if (import_p)
  2411. /* We have already decided what to do with this DECL; there is no
  2412. need to check anything further. */
  2413. ;
  2414. else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
  2415. {
  2416. class_type = DECL_CONTEXT (decl);
  2417. import_export_class (class_type);
  2418. if (TYPE_FOR_JAVA (class_type))
  2419. import_p = true;
  2420. else if (CLASSTYPE_INTERFACE_KNOWN (class_type)
  2421. && CLASSTYPE_INTERFACE_ONLY (class_type))
  2422. import_p = true;
  2423. else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC)
  2424. && !CLASSTYPE_USE_TEMPLATE (class_type)
  2425. && CLASSTYPE_KEY_METHOD (class_type)
  2426. && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)))
  2427. /* The ABI requires that all virtual tables be emitted with
  2428. COMDAT linkage. However, on systems where COMDAT symbols
  2429. don't show up in the table of contents for a static
  2430. archive, or on systems without weak symbols (where we
  2431. approximate COMDAT linkage by using internal linkage), the
  2432. linker will report errors about undefined symbols because
  2433. it will not see the virtual table definition. Therefore,
  2434. in the case that we know that the virtual table will be
  2435. emitted in only one translation unit, we make the virtual
  2436. table an ordinary definition with external linkage. */
  2437. DECL_EXTERNAL (decl) = 0;
  2438. else if (CLASSTYPE_INTERFACE_KNOWN (class_type))
  2439. {
  2440. /* CLASS_TYPE is being exported from this translation unit,
  2441. so DECL should be defined here. */
  2442. if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type))
  2443. /* If a class is declared in a header with the "extern
  2444. template" extension, then it will not be instantiated,
  2445. even in translation units that would normally require
  2446. it. Often such classes are explicitly instantiated in
  2447. one translation unit. Therefore, the explicit
  2448. instantiation must be made visible to other translation
  2449. units. */
  2450. DECL_EXTERNAL (decl) = 0;
  2451. else
  2452. {
  2453. /* The generic C++ ABI says that class data is always
  2454. COMDAT, even if there is a key function. Some
  2455. variants (e.g., the ARM EABI) says that class data
  2456. only has COMDAT linkage if the class data might be
  2457. emitted in more than one translation unit. When the
  2458. key method can be inline and is inline, we still have
  2459. to arrange for comdat even though
  2460. class_data_always_comdat is false. */
  2461. if (!CLASSTYPE_KEY_METHOD (class_type)
  2462. || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))
  2463. || targetm.cxx.class_data_always_comdat ())
  2464. {
  2465. /* The ABI requires COMDAT linkage. Normally, we
  2466. only emit COMDAT things when they are needed;
  2467. make sure that we realize that this entity is
  2468. indeed needed. */
  2469. comdat_p = true;
  2470. mark_needed (decl);
  2471. }
  2472. }
  2473. }
  2474. else if (!flag_implicit_templates
  2475. && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type))
  2476. import_p = true;
  2477. else
  2478. comdat_p = true;
  2479. }
  2480. else if (VAR_P (decl) && DECL_TINFO_P (decl))
  2481. {
  2482. tree type = TREE_TYPE (DECL_NAME (decl));
  2483. if (CLASS_TYPE_P (type))
  2484. {
  2485. class_type = type;
  2486. import_export_class (type);
  2487. if (CLASSTYPE_INTERFACE_KNOWN (type)
  2488. && TYPE_POLYMORPHIC_P (type)
  2489. && CLASSTYPE_INTERFACE_ONLY (type)
  2490. /* If -fno-rtti was specified, then we cannot be sure
  2491. that RTTI information will be emitted with the
  2492. virtual table of the class, so we must emit it
  2493. wherever it is used. */
  2494. && flag_rtti)
  2495. import_p = true;
  2496. else
  2497. {
  2498. if (CLASSTYPE_INTERFACE_KNOWN (type)
  2499. && !CLASSTYPE_INTERFACE_ONLY (type))
  2500. {
  2501. comdat_p = (targetm.cxx.class_data_always_comdat ()
  2502. || (CLASSTYPE_KEY_METHOD (type)
  2503. && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type))));
  2504. mark_needed (decl);
  2505. if (!flag_weak)
  2506. {
  2507. comdat_p = false;
  2508. DECL_EXTERNAL (decl) = 0;
  2509. }
  2510. }
  2511. else
  2512. comdat_p = true;
  2513. }
  2514. }
  2515. else
  2516. comdat_p = true;
  2517. }
  2518. else if (DECL_TEMPLOID_INSTANTIATION (decl))
  2519. {
  2520. /* DECL is an implicit instantiation of a function or static
  2521. data member. */
  2522. if ((flag_implicit_templates
  2523. && !flag_use_repository)
  2524. || (flag_implicit_inline_templates
  2525. && TREE_CODE (decl) == FUNCTION_DECL
  2526. && DECL_DECLARED_INLINE_P (decl)))
  2527. comdat_p = true;
  2528. else
  2529. /* If we are not implicitly generating templates, then mark
  2530. this entity as undefined in this translation unit. */
  2531. import_p = true;
  2532. }
  2533. else if (DECL_FUNCTION_MEMBER_P (decl))
  2534. {
  2535. if (!DECL_DECLARED_INLINE_P (decl))
  2536. {
  2537. tree ctype = DECL_CONTEXT (decl);
  2538. import_export_class (ctype);
  2539. if (CLASSTYPE_INTERFACE_KNOWN (ctype))
  2540. {
  2541. DECL_NOT_REALLY_EXTERN (decl)
  2542. = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
  2543. || (DECL_DECLARED_INLINE_P (decl)
  2544. && ! flag_implement_inlines
  2545. && !DECL_VINDEX (decl)));
  2546. if (!DECL_NOT_REALLY_EXTERN (decl))
  2547. DECL_EXTERNAL (decl) = 1;
  2548. /* Always make artificials weak. */
  2549. if (DECL_ARTIFICIAL (decl) && flag_weak)
  2550. comdat_p = true;
  2551. else
  2552. maybe_make_one_only (decl);
  2553. }
  2554. }
  2555. else
  2556. comdat_p = true;
  2557. }
  2558. else
  2559. comdat_p = true;
  2560. if (import_p)
  2561. {
  2562. /* If we are importing DECL into this translation unit, mark is
  2563. an undefined here. */
  2564. DECL_EXTERNAL (decl) = 1;
  2565. DECL_NOT_REALLY_EXTERN (decl) = 0;
  2566. }
  2567. else if (comdat_p)
  2568. {
  2569. /* If we decided to put DECL in COMDAT, mark it accordingly at
  2570. this point. */
  2571. comdat_linkage (decl);
  2572. }
  2573. DECL_INTERFACE_KNOWN (decl) = 1;
  2574. }
  2575. /* Return an expression that performs the destruction of DECL, which
  2576. must be a VAR_DECL whose type has a non-trivial destructor, or is
  2577. an array whose (innermost) elements have a non-trivial destructor. */
  2578. tree
  2579. build_cleanup (tree decl)
  2580. {
  2581. tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
  2582. gcc_assert (clean != NULL_TREE);
  2583. return clean;
  2584. }
  2585. /* Returns the initialization guard variable for the variable DECL,
  2586. which has static storage duration. */
  2587. tree
  2588. get_guard (tree decl)
  2589. {
  2590. tree sname;
  2591. tree guard;
  2592. sname = mangle_guard_variable (decl);
  2593. guard = IDENTIFIER_GLOBAL_VALUE (sname);
  2594. if (! guard)
  2595. {
  2596. tree guard_type;
  2597. /* We use a type that is big enough to contain a mutex as well
  2598. as an integer counter. */
  2599. guard_type = targetm.cxx.guard_type ();
  2600. guard = build_decl (DECL_SOURCE_LOCATION (decl),
  2601. VAR_DECL, sname, guard_type);
  2602. /* The guard should have the same linkage as what it guards. */
  2603. TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
  2604. TREE_STATIC (guard) = TREE_STATIC (decl);
  2605. DECL_COMMON (guard) = DECL_COMMON (decl);
  2606. DECL_COMDAT (guard) = DECL_COMDAT (decl);
  2607. set_decl_tls_model (guard, DECL_TLS_MODEL (decl));
  2608. if (DECL_ONE_ONLY (decl))
  2609. make_decl_one_only (guard, cxx_comdat_group (guard));
  2610. if (TREE_PUBLIC (decl))
  2611. DECL_WEAK (guard) = DECL_WEAK (decl);
  2612. DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl);
  2613. DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl);
  2614. DECL_ARTIFICIAL (guard) = 1;
  2615. DECL_IGNORED_P (guard) = 1;
  2616. TREE_USED (guard) = 1;
  2617. pushdecl_top_level_and_finish (guard, NULL_TREE);
  2618. }
  2619. return guard;
  2620. }
  2621. /* Return those bits of the GUARD variable that should be set when the
  2622. guarded entity is actually initialized. */
  2623. static tree
  2624. get_guard_bits (tree guard)
  2625. {
  2626. if (!targetm.cxx.guard_mask_bit ())
  2627. {
  2628. /* We only set the first byte of the guard, in order to leave room
  2629. for a mutex in the high-order bits. */
  2630. guard = build1 (ADDR_EXPR,
  2631. build_pointer_type (TREE_TYPE (guard)),
  2632. guard);
  2633. guard = build1 (NOP_EXPR,
  2634. build_pointer_type (char_type_node),
  2635. guard);
  2636. guard = build1 (INDIRECT_REF, char_type_node, guard);
  2637. }
  2638. return guard;
  2639. }
  2640. /* Return an expression which determines whether or not the GUARD
  2641. variable has already been initialized. */
  2642. tree
  2643. get_guard_cond (tree guard)
  2644. {
  2645. tree guard_value;
  2646. /* Check to see if the GUARD is zero. */
  2647. guard = get_guard_bits (guard);
  2648. /* Mask off all but the low bit. */
  2649. if (targetm.cxx.guard_mask_bit ())
  2650. {
  2651. guard_value = integer_one_node;
  2652. if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
  2653. guard_value = convert (TREE_TYPE (guard), guard_value);
  2654. guard = cp_build_binary_op (input_location,
  2655. BIT_AND_EXPR, guard, guard_value,
  2656. tf_warning_or_error);
  2657. }
  2658. guard_value = integer_zero_node;
  2659. if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
  2660. guard_value = convert (TREE_TYPE (guard), guard_value);
  2661. return cp_build_binary_op (input_location,
  2662. EQ_EXPR, guard, guard_value,
  2663. tf_warning_or_error);
  2664. }
  2665. /* Return an expression which sets the GUARD variable, indicating that
  2666. the variable being guarded has been initialized. */
  2667. tree
  2668. set_guard (tree guard)
  2669. {
  2670. tree guard_init;
  2671. /* Set the GUARD to one. */
  2672. guard = get_guard_bits (guard);
  2673. guard_init = integer_one_node;
  2674. if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
  2675. guard_init = convert (TREE_TYPE (guard), guard_init);
  2676. return cp_build_modify_expr (guard, NOP_EXPR, guard_init,
  2677. tf_warning_or_error);
  2678. }
  2679. /* Returns true iff we can tell that VAR does not have a dynamic
  2680. initializer. */
  2681. static bool
  2682. var_defined_without_dynamic_init (tree var)
  2683. {
  2684. /* If it's defined in another TU, we can't tell. */
  2685. if (DECL_EXTERNAL (var))
  2686. return false;
  2687. /* If it has a non-trivial destructor, registering the destructor
  2688. counts as dynamic initialization. */
  2689. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)))
  2690. return false;
  2691. /* If it's in this TU, its initializer has been processed, unless
  2692. it's a case of self-initialization, then DECL_INITIALIZED_P is
  2693. false while the initializer is handled by finish_id_expression. */
  2694. if (!DECL_INITIALIZED_P (var))
  2695. return false;
  2696. /* If it has no initializer or a constant one, it's not dynamic. */
  2697. return (!DECL_NONTRIVIALLY_INITIALIZED_P (var)
  2698. || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var));
  2699. }
  2700. /* Returns true iff VAR is a variable that needs uses to be
  2701. wrapped for possible dynamic initialization. */
  2702. static bool
  2703. var_needs_tls_wrapper (tree var)
  2704. {
  2705. return (!error_operand_p (var)
  2706. && DECL_THREAD_LOCAL_P (var)
  2707. && !DECL_GNU_TLS_P (var)
  2708. && !DECL_FUNCTION_SCOPE_P (var)
  2709. && !var_defined_without_dynamic_init (var));
  2710. }
  2711. /* Get the FUNCTION_DECL for the shared TLS init function for this
  2712. translation unit. */
  2713. static tree
  2714. get_local_tls_init_fn (void)
  2715. {
  2716. tree sname = get_identifier ("__tls_init");
  2717. tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
  2718. if (!fn)
  2719. {
  2720. fn = build_lang_decl (FUNCTION_DECL, sname,
  2721. build_function_type (void_type_node,
  2722. void_list_node));
  2723. SET_DECL_LANGUAGE (fn, lang_c);
  2724. TREE_PUBLIC (fn) = false;
  2725. DECL_ARTIFICIAL (fn) = true;
  2726. mark_used (fn);
  2727. SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
  2728. }
  2729. return fn;
  2730. }
  2731. /* Get a FUNCTION_DECL for the init function for the thread_local
  2732. variable VAR. The init function will be an alias to the function
  2733. that initializes all the non-local TLS variables in the translation
  2734. unit. The init function is only used by the wrapper function. */
  2735. static tree
  2736. get_tls_init_fn (tree var)
  2737. {
  2738. /* Only C++11 TLS vars need this init fn. */
  2739. if (!var_needs_tls_wrapper (var))
  2740. return NULL_TREE;
  2741. /* If -fno-extern-tls-init, assume that we don't need to call
  2742. a tls init function for a variable defined in another TU. */
  2743. if (!flag_extern_tls_init && DECL_EXTERNAL (var))
  2744. return NULL_TREE;
  2745. #ifdef ASM_OUTPUT_DEF
  2746. /* If the variable is internal, or if we can't generate aliases,
  2747. call the local init function directly. */
  2748. if (!TREE_PUBLIC (var))
  2749. #endif
  2750. return get_local_tls_init_fn ();
  2751. tree sname = mangle_tls_init_fn (var);
  2752. tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
  2753. if (!fn)
  2754. {
  2755. fn = build_lang_decl (FUNCTION_DECL, sname,
  2756. build_function_type (void_type_node,
  2757. void_list_node));
  2758. SET_DECL_LANGUAGE (fn, lang_c);
  2759. TREE_PUBLIC (fn) = TREE_PUBLIC (var);
  2760. DECL_ARTIFICIAL (fn) = true;
  2761. DECL_COMDAT (fn) = DECL_COMDAT (var);
  2762. DECL_EXTERNAL (fn) = DECL_EXTERNAL (var);
  2763. if (DECL_ONE_ONLY (var))
  2764. make_decl_one_only (fn, cxx_comdat_group (fn));
  2765. if (TREE_PUBLIC (var))
  2766. {
  2767. tree obtype = strip_array_types (non_reference (TREE_TYPE (var)));
  2768. /* If the variable is defined somewhere else and might have static
  2769. initialization, make the init function a weak reference. */
  2770. if ((!TYPE_NEEDS_CONSTRUCTING (obtype)
  2771. || TYPE_HAS_CONSTEXPR_CTOR (obtype))
  2772. && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype)
  2773. && DECL_EXTERNAL (var))
  2774. declare_weak (fn);
  2775. else
  2776. DECL_WEAK (fn) = DECL_WEAK (var);
  2777. }
  2778. DECL_VISIBILITY (fn) = DECL_VISIBILITY (var);
  2779. DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var);
  2780. DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var);
  2781. DECL_IGNORED_P (fn) = 1;
  2782. mark_used (fn);
  2783. DECL_BEFRIENDING_CLASSES (fn) = var;
  2784. SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
  2785. }
  2786. return fn;
  2787. }
  2788. /* Get a FUNCTION_DECL for the init wrapper function for the thread_local
  2789. variable VAR. The wrapper function calls the init function (if any) for
  2790. VAR and then returns a reference to VAR. The wrapper function is used
  2791. in place of VAR everywhere VAR is mentioned. */
  2792. tree
  2793. get_tls_wrapper_fn (tree var)
  2794. {
  2795. /* Only C++11 TLS vars need this wrapper fn. */
  2796. if (!var_needs_tls_wrapper (var))
  2797. return NULL_TREE;
  2798. tree sname = mangle_tls_wrapper_fn (var);
  2799. tree fn = IDENTIFIER_GLOBAL_VALUE (sname);
  2800. if (!fn)
  2801. {
  2802. /* A named rvalue reference is an lvalue, so the wrapper should
  2803. always return an lvalue reference. */
  2804. tree type = non_reference (TREE_TYPE (var));
  2805. type = build_reference_type (type);
  2806. tree fntype = build_function_type (type, void_list_node);
  2807. fn = build_lang_decl (FUNCTION_DECL, sname, fntype);
  2808. SET_DECL_LANGUAGE (fn, lang_c);
  2809. TREE_PUBLIC (fn) = TREE_PUBLIC (var);
  2810. DECL_ARTIFICIAL (fn) = true;
  2811. DECL_IGNORED_P (fn) = 1;
  2812. /* The wrapper is inline and emitted everywhere var is used. */
  2813. DECL_DECLARED_INLINE_P (fn) = true;
  2814. if (TREE_PUBLIC (var))
  2815. {
  2816. comdat_linkage (fn);
  2817. #ifdef HAVE_GAS_HIDDEN
  2818. /* Make the wrapper bind locally; there's no reason to share
  2819. the wrapper between multiple shared objects. */
  2820. DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL;
  2821. DECL_VISIBILITY_SPECIFIED (fn) = true;
  2822. #endif
  2823. }
  2824. if (!TREE_PUBLIC (fn))
  2825. DECL_INTERFACE_KNOWN (fn) = true;
  2826. mark_used (fn);
  2827. note_vague_linkage_fn (fn);
  2828. #if 0
  2829. /* We want CSE to commonize calls to the wrapper, but marking it as
  2830. pure is unsafe since it has side-effects. I guess we need a new
  2831. ECF flag even weaker than ECF_PURE. FIXME! */
  2832. DECL_PURE_P (fn) = true;
  2833. #endif
  2834. DECL_BEFRIENDING_CLASSES (fn) = var;
  2835. SET_IDENTIFIER_GLOBAL_VALUE (sname, fn);
  2836. }
  2837. return fn;
  2838. }
  2839. /* At EOF, generate the definition for the TLS wrapper function FN:
  2840. T& var_wrapper() {
  2841. if (init_fn) init_fn();
  2842. return var;
  2843. } */
  2844. static void
  2845. generate_tls_wrapper (tree fn)
  2846. {
  2847. tree var = DECL_BEFRIENDING_CLASSES (fn);
  2848. start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
  2849. tree body = begin_function_body ();
  2850. /* Only call the init fn if there might be one. */
  2851. if (tree init_fn = get_tls_init_fn (var))
  2852. {
  2853. tree if_stmt = NULL_TREE;
  2854. /* If init_fn is a weakref, make sure it exists before calling. */
  2855. if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn)))
  2856. {
  2857. if_stmt = begin_if_stmt ();
  2858. tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error);
  2859. tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var),
  2860. NE_EXPR, addr, nullptr_node,
  2861. tf_warning_or_error);
  2862. finish_if_stmt_cond (cond, if_stmt);
  2863. }
  2864. finish_expr_stmt (build_cxx_call
  2865. (init_fn, 0, NULL, tf_warning_or_error));
  2866. if (if_stmt)
  2867. {
  2868. finish_then_clause (if_stmt);
  2869. finish_if_stmt (if_stmt);
  2870. }
  2871. }
  2872. else
  2873. /* If there's no initialization, the wrapper is a constant function. */
  2874. TREE_READONLY (fn) = true;
  2875. finish_return_stmt (convert_from_reference (var));
  2876. finish_function_body (body);
  2877. expand_or_defer_fn (finish_function (0));
  2878. }
  2879. /* Start the process of running a particular set of global constructors
  2880. or destructors. Subroutine of do_[cd]tors. Also called from
  2881. vtv_start_verification_constructor_init_function. */
  2882. static tree
  2883. start_objects (int method_type, int initp)
  2884. {
  2885. tree body;
  2886. tree fndecl;
  2887. char type[14];
  2888. /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
  2889. if (initp != DEFAULT_INIT_PRIORITY)
  2890. {
  2891. char joiner;
  2892. #ifdef JOINER
  2893. joiner = JOINER;
  2894. #else
  2895. joiner = '_';
  2896. #endif
  2897. sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp);
  2898. }
  2899. else
  2900. sprintf (type, "sub_%c", method_type);
  2901. fndecl = build_lang_decl (FUNCTION_DECL,
  2902. get_file_function_name (type),
  2903. build_function_type_list (void_type_node,
  2904. NULL_TREE));
  2905. start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED);
  2906. TREE_PUBLIC (current_function_decl) = 0;
  2907. /* Mark as artificial because it's not explicitly in the user's
  2908. source code. */
  2909. DECL_ARTIFICIAL (current_function_decl) = 1;
  2910. /* Mark this declaration as used to avoid spurious warnings. */
  2911. TREE_USED (current_function_decl) = 1;
  2912. /* Mark this function as a global constructor or destructor. */
  2913. if (method_type == 'I')
  2914. DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
  2915. else
  2916. DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
  2917. body = begin_compound_stmt (BCS_FN_BODY);
  2918. return body;
  2919. }
  2920. /* Finish the process of running a particular set of global constructors
  2921. or destructors. Subroutine of do_[cd]tors. */
  2922. static void
  2923. finish_objects (int method_type, int initp, tree body)
  2924. {
  2925. tree fn;
  2926. /* Finish up. */
  2927. finish_compound_stmt (body);
  2928. fn = finish_function (0);
  2929. if (method_type == 'I')
  2930. {
  2931. DECL_STATIC_CONSTRUCTOR (fn) = 1;
  2932. decl_init_priority_insert (fn, initp);
  2933. }
  2934. else
  2935. {
  2936. DECL_STATIC_DESTRUCTOR (fn) = 1;
  2937. decl_fini_priority_insert (fn, initp);
  2938. }
  2939. expand_or_defer_fn (fn);
  2940. }
  2941. /* The names of the parameters to the function created to handle
  2942. initializations and destructions for objects with static storage
  2943. duration. */
  2944. #define INITIALIZE_P_IDENTIFIER "__initialize_p"
  2945. #define PRIORITY_IDENTIFIER "__priority"
  2946. /* The name of the function we create to handle initializations and
  2947. destructions for objects with static storage duration. */
  2948. #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
  2949. /* The declaration for the __INITIALIZE_P argument. */
  2950. static GTY(()) tree initialize_p_decl;
  2951. /* The declaration for the __PRIORITY argument. */
  2952. static GTY(()) tree priority_decl;
  2953. /* The declaration for the static storage duration function. */
  2954. static GTY(()) tree ssdf_decl;
  2955. /* All the static storage duration functions created in this
  2956. translation unit. */
  2957. static GTY(()) vec<tree, va_gc> *ssdf_decls;
  2958. /* A map from priority levels to information about that priority
  2959. level. There may be many such levels, so efficient lookup is
  2960. important. */
  2961. static splay_tree priority_info_map;
  2962. /* Begins the generation of the function that will handle all
  2963. initialization and destruction of objects with static storage
  2964. duration. The function generated takes two parameters of type
  2965. `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
  2966. nonzero, it performs initializations. Otherwise, it performs
  2967. destructions. It only performs those initializations or
  2968. destructions with the indicated __PRIORITY. The generated function
  2969. returns no value.
  2970. It is assumed that this function will only be called once per
  2971. translation unit. */
  2972. static tree
  2973. start_static_storage_duration_function (unsigned count)
  2974. {
  2975. tree type;
  2976. tree body;
  2977. char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
  2978. /* Create the identifier for this function. It will be of the form
  2979. SSDF_IDENTIFIER_<number>. */
  2980. sprintf (id, "%s_%u", SSDF_IDENTIFIER, count);
  2981. type = build_function_type_list (void_type_node,
  2982. integer_type_node, integer_type_node,
  2983. NULL_TREE);
  2984. /* Create the FUNCTION_DECL itself. */
  2985. ssdf_decl = build_lang_decl (FUNCTION_DECL,
  2986. get_identifier (id),
  2987. type);
  2988. TREE_PUBLIC (ssdf_decl) = 0;
  2989. DECL_ARTIFICIAL (ssdf_decl) = 1;
  2990. /* Put this function in the list of functions to be called from the
  2991. static constructors and destructors. */
  2992. if (!ssdf_decls)
  2993. {
  2994. vec_alloc (ssdf_decls, 32);
  2995. /* Take this opportunity to initialize the map from priority
  2996. numbers to information about that priority level. */
  2997. priority_info_map = splay_tree_new (splay_tree_compare_ints,
  2998. /*delete_key_fn=*/0,
  2999. /*delete_value_fn=*/
  3000. (splay_tree_delete_value_fn) &free);
  3001. /* We always need to generate functions for the
  3002. DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
  3003. priorities later, we'll be sure to find the
  3004. DEFAULT_INIT_PRIORITY. */
  3005. get_priority_info (DEFAULT_INIT_PRIORITY);
  3006. }
  3007. vec_safe_push (ssdf_decls, ssdf_decl);
  3008. /* Create the argument list. */
  3009. initialize_p_decl = cp_build_parm_decl
  3010. (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node);
  3011. DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
  3012. TREE_USED (initialize_p_decl) = 1;
  3013. priority_decl = cp_build_parm_decl
  3014. (get_identifier (PRIORITY_IDENTIFIER), integer_type_node);
  3015. DECL_CONTEXT (priority_decl) = ssdf_decl;
  3016. TREE_USED (priority_decl) = 1;
  3017. DECL_CHAIN (initialize_p_decl) = priority_decl;
  3018. DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
  3019. /* Put the function in the global scope. */
  3020. pushdecl (ssdf_decl);
  3021. /* Start the function itself. This is equivalent to declaring the
  3022. function as:
  3023. static void __ssdf (int __initialize_p, init __priority_p);
  3024. It is static because we only need to call this function from the
  3025. various constructor and destructor functions for this module. */
  3026. start_preparsed_function (ssdf_decl,
  3027. /*attrs=*/NULL_TREE,
  3028. SF_PRE_PARSED);
  3029. /* Set up the scope of the outermost block in the function. */
  3030. body = begin_compound_stmt (BCS_FN_BODY);
  3031. return body;
  3032. }
  3033. /* Finish the generation of the function which performs initialization
  3034. and destruction of objects with static storage duration. After
  3035. this point, no more such objects can be created. */
  3036. static void
  3037. finish_static_storage_duration_function (tree body)
  3038. {
  3039. /* Close out the function. */
  3040. finish_compound_stmt (body);
  3041. expand_or_defer_fn (finish_function (0));
  3042. }
  3043. /* Return the information about the indicated PRIORITY level. If no
  3044. code to handle this level has yet been generated, generate the
  3045. appropriate prologue. */
  3046. static priority_info
  3047. get_priority_info (int priority)
  3048. {
  3049. priority_info pi;
  3050. splay_tree_node n;
  3051. n = splay_tree_lookup (priority_info_map,
  3052. (splay_tree_key) priority);
  3053. if (!n)
  3054. {
  3055. /* Create a new priority information structure, and insert it
  3056. into the map. */
  3057. pi = XNEW (struct priority_info_s);
  3058. pi->initializations_p = 0;
  3059. pi->destructions_p = 0;
  3060. splay_tree_insert (priority_info_map,
  3061. (splay_tree_key) priority,
  3062. (splay_tree_value) pi);
  3063. }
  3064. else
  3065. pi = (priority_info) n->value;
  3066. return pi;
  3067. }
  3068. /* The effective initialization priority of a DECL. */
  3069. #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \
  3070. ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \
  3071. ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl))
  3072. /* Whether a DECL needs a guard to protect it against multiple
  3073. initialization. */
  3074. #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \
  3075. || DECL_ONE_ONLY (decl) \
  3076. || DECL_WEAK (decl)))
  3077. /* Called from one_static_initialization_or_destruction(),
  3078. via walk_tree.
  3079. Walks the initializer list of a global variable and looks for
  3080. temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0)
  3081. and that have their DECL_CONTEXT() == NULL.
  3082. For each such temporary variable, set their DECL_CONTEXT() to
  3083. the current function. This is necessary because otherwise
  3084. some optimizers (enabled by -O2 -fprofile-arcs) might crash
  3085. when trying to refer to a temporary variable that does not have
  3086. it's DECL_CONTECT() properly set. */
  3087. static tree
  3088. fix_temporary_vars_context_r (tree *node,
  3089. int * /*unused*/,
  3090. void * /*unused1*/)
  3091. {
  3092. gcc_assert (current_function_decl);
  3093. if (TREE_CODE (*node) == BIND_EXPR)
  3094. {
  3095. tree var;
  3096. for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var))
  3097. if (VAR_P (var)
  3098. && !DECL_NAME (var)
  3099. && DECL_ARTIFICIAL (var)
  3100. && !DECL_CONTEXT (var))
  3101. DECL_CONTEXT (var) = current_function_decl;
  3102. }
  3103. return NULL_TREE;
  3104. }
  3105. /* Set up to handle the initialization or destruction of DECL. If
  3106. INITP is nonzero, we are initializing the variable. Otherwise, we
  3107. are destroying it. */
  3108. static void
  3109. one_static_initialization_or_destruction (tree decl, tree init, bool initp)
  3110. {
  3111. tree guard_if_stmt = NULL_TREE;
  3112. tree guard;
  3113. /* If we are supposed to destruct and there's a trivial destructor,
  3114. nothing has to be done. */
  3115. if (!initp
  3116. && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
  3117. return;
  3118. /* Trick the compiler into thinking we are at the file and line
  3119. where DECL was declared so that error-messages make sense, and so
  3120. that the debugger will show somewhat sensible file and line
  3121. information. */
  3122. input_location = DECL_SOURCE_LOCATION (decl);
  3123. /* Make sure temporary variables in the initialiser all have
  3124. their DECL_CONTEXT() set to a value different from NULL_TREE.
  3125. This can happen when global variables initialisers are built.
  3126. In that case, the DECL_CONTEXT() of the global variables _AND_ of all
  3127. the temporary variables that might have been generated in the
  3128. accompagning initialisers is NULL_TREE, meaning the variables have been
  3129. declared in the global namespace.
  3130. What we want to do here is to fix that and make sure the DECL_CONTEXT()
  3131. of the temporaries are set to the current function decl. */
  3132. cp_walk_tree_without_duplicates (&init,
  3133. fix_temporary_vars_context_r,
  3134. NULL);
  3135. /* Because of:
  3136. [class.access.spec]
  3137. Access control for implicit calls to the constructors,
  3138. the conversion functions, or the destructor called to
  3139. create and destroy a static data member is performed as
  3140. if these calls appeared in the scope of the member's
  3141. class.
  3142. we pretend we are in a static member function of the class of
  3143. which the DECL is a member. */
  3144. if (member_p (decl))
  3145. {
  3146. DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
  3147. DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
  3148. }
  3149. /* Assume we don't need a guard. */
  3150. guard = NULL_TREE;
  3151. /* We need a guard if this is an object with external linkage that
  3152. might be initialized in more than one place. (For example, a
  3153. static data member of a template, when the data member requires
  3154. construction.) */
  3155. if (NEEDS_GUARD_P (decl))
  3156. {
  3157. tree guard_cond;
  3158. guard = get_guard (decl);
  3159. /* When using __cxa_atexit, we just check the GUARD as we would
  3160. for a local static. */
  3161. if (flag_use_cxa_atexit)
  3162. {
  3163. /* When using __cxa_atexit, we never try to destroy
  3164. anything from a static destructor. */
  3165. gcc_assert (initp);
  3166. guard_cond = get_guard_cond (guard);
  3167. }
  3168. /* If we don't have __cxa_atexit, then we will be running
  3169. destructors from .fini sections, or their equivalents. So,
  3170. we need to know how many times we've tried to initialize this
  3171. object. We do initializations only if the GUARD is zero,
  3172. i.e., if we are the first to initialize the variable. We do
  3173. destructions only if the GUARD is one, i.e., if we are the
  3174. last to destroy the variable. */
  3175. else if (initp)
  3176. guard_cond
  3177. = cp_build_binary_op (input_location,
  3178. EQ_EXPR,
  3179. cp_build_unary_op (PREINCREMENT_EXPR,
  3180. guard,
  3181. /*noconvert=*/1,
  3182. tf_warning_or_error),
  3183. integer_one_node,
  3184. tf_warning_or_error);
  3185. else
  3186. guard_cond
  3187. = cp_build_binary_op (input_location,
  3188. EQ_EXPR,
  3189. cp_build_unary_op (PREDECREMENT_EXPR,
  3190. guard,
  3191. /*noconvert=*/1,
  3192. tf_warning_or_error),
  3193. integer_zero_node,
  3194. tf_warning_or_error);
  3195. guard_if_stmt = begin_if_stmt ();
  3196. finish_if_stmt_cond (guard_cond, guard_if_stmt);
  3197. }
  3198. /* If we're using __cxa_atexit, we have not already set the GUARD,
  3199. so we must do so now. */
  3200. if (guard && initp && flag_use_cxa_atexit)
  3201. finish_expr_stmt (set_guard (guard));
  3202. /* Perform the initialization or destruction. */
  3203. if (initp)
  3204. {
  3205. if (init)
  3206. {
  3207. finish_expr_stmt (init);
  3208. if (flag_sanitize & SANITIZE_ADDRESS)
  3209. {
  3210. varpool_node *vnode = varpool_node::get (decl);
  3211. if (vnode)
  3212. vnode->dynamically_initialized = 1;
  3213. }
  3214. }
  3215. /* If we're using __cxa_atexit, register a function that calls the
  3216. destructor for the object. */
  3217. if (flag_use_cxa_atexit)
  3218. finish_expr_stmt (register_dtor_fn (decl));
  3219. }
  3220. else
  3221. finish_expr_stmt (build_cleanup (decl));
  3222. /* Finish the guard if-stmt, if necessary. */
  3223. if (guard)
  3224. {
  3225. finish_then_clause (guard_if_stmt);
  3226. finish_if_stmt (guard_if_stmt);
  3227. }
  3228. /* Now that we're done with DECL we don't need to pretend to be a
  3229. member of its class any longer. */
  3230. DECL_CONTEXT (current_function_decl) = NULL_TREE;
  3231. DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
  3232. }
  3233. /* Generate code to do the initialization or destruction of the decls in VARS,
  3234. a TREE_LIST of VAR_DECL with static storage duration.
  3235. Whether initialization or destruction is performed is specified by INITP. */
  3236. static void
  3237. do_static_initialization_or_destruction (tree vars, bool initp)
  3238. {
  3239. tree node, init_if_stmt, cond;
  3240. /* Build the outer if-stmt to check for initialization or destruction. */
  3241. init_if_stmt = begin_if_stmt ();
  3242. cond = initp ? integer_one_node : integer_zero_node;
  3243. cond = cp_build_binary_op (input_location,
  3244. EQ_EXPR,
  3245. initialize_p_decl,
  3246. cond,
  3247. tf_warning_or_error);
  3248. finish_if_stmt_cond (cond, init_if_stmt);
  3249. /* To make sure dynamic construction doesn't access globals from other
  3250. compilation units where they might not be yet constructed, for
  3251. -fsanitize=address insert __asan_before_dynamic_init call that
  3252. prevents access to either all global variables that need construction
  3253. in other compilation units, or at least those that haven't been
  3254. initialized yet. Variables that need dynamic construction in
  3255. the current compilation unit are kept accessible. */
  3256. if (flag_sanitize & SANITIZE_ADDRESS)
  3257. finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false));
  3258. node = vars;
  3259. do {
  3260. tree decl = TREE_VALUE (node);
  3261. tree priority_if_stmt;
  3262. int priority;
  3263. priority_info pi;
  3264. /* If we don't need a destructor, there's nothing to do. Avoid
  3265. creating a possibly empty if-stmt. */
  3266. if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
  3267. {
  3268. node = TREE_CHAIN (node);
  3269. continue;
  3270. }
  3271. /* Remember that we had an initialization or finalization at this
  3272. priority. */
  3273. priority = DECL_EFFECTIVE_INIT_PRIORITY (decl);
  3274. pi = get_priority_info (priority);
  3275. if (initp)
  3276. pi->initializations_p = 1;
  3277. else
  3278. pi->destructions_p = 1;
  3279. /* Conditionalize this initialization on being in the right priority
  3280. and being initializing/finalizing appropriately. */
  3281. priority_if_stmt = begin_if_stmt ();
  3282. cond = cp_build_binary_op (input_location,
  3283. EQ_EXPR,
  3284. priority_decl,
  3285. build_int_cst (NULL_TREE, priority),
  3286. tf_warning_or_error);
  3287. finish_if_stmt_cond (cond, priority_if_stmt);
  3288. /* Process initializers with same priority. */
  3289. for (; node
  3290. && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority;
  3291. node = TREE_CHAIN (node))
  3292. /* Do one initialization or destruction. */
  3293. one_static_initialization_or_destruction (TREE_VALUE (node),
  3294. TREE_PURPOSE (node), initp);
  3295. /* Finish up the priority if-stmt body. */
  3296. finish_then_clause (priority_if_stmt);
  3297. finish_if_stmt (priority_if_stmt);
  3298. } while (node);
  3299. /* Revert what __asan_before_dynamic_init did by calling
  3300. __asan_after_dynamic_init. */
  3301. if (flag_sanitize & SANITIZE_ADDRESS)
  3302. finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true));
  3303. /* Finish up the init/destruct if-stmt body. */
  3304. finish_then_clause (init_if_stmt);
  3305. finish_if_stmt (init_if_stmt);
  3306. }
  3307. /* VARS is a list of variables with static storage duration which may
  3308. need initialization and/or finalization. Remove those variables
  3309. that don't really need to be initialized or finalized, and return
  3310. the resulting list. The order in which the variables appear in
  3311. VARS is in reverse order of the order in which they should actually
  3312. be initialized. The list we return is in the unreversed order;
  3313. i.e., the first variable should be initialized first. */
  3314. static tree
  3315. prune_vars_needing_no_initialization (tree *vars)
  3316. {
  3317. tree *var = vars;
  3318. tree result = NULL_TREE;
  3319. while (*var)
  3320. {
  3321. tree t = *var;
  3322. tree decl = TREE_VALUE (t);
  3323. tree init = TREE_PURPOSE (t);
  3324. /* Deal gracefully with error. */
  3325. if (decl == error_mark_node)
  3326. {
  3327. var = &TREE_CHAIN (t);
  3328. continue;
  3329. }
  3330. /* The only things that can be initialized are variables. */
  3331. gcc_assert (VAR_P (decl));
  3332. /* If this object is not defined, we don't need to do anything
  3333. here. */
  3334. if (DECL_EXTERNAL (decl))
  3335. {
  3336. var = &TREE_CHAIN (t);
  3337. continue;
  3338. }
  3339. /* Also, if the initializer already contains errors, we can bail
  3340. out now. */
  3341. if (init && TREE_CODE (init) == TREE_LIST
  3342. && value_member (error_mark_node, init))
  3343. {
  3344. var = &TREE_CHAIN (t);
  3345. continue;
  3346. }
  3347. /* This variable is going to need initialization and/or
  3348. finalization, so we add it to the list. */
  3349. *var = TREE_CHAIN (t);
  3350. TREE_CHAIN (t) = result;
  3351. result = t;
  3352. }
  3353. return result;
  3354. }
  3355. /* Make sure we have told the back end about all the variables in
  3356. VARS. */
  3357. static void
  3358. write_out_vars (tree vars)
  3359. {
  3360. tree v;
  3361. for (v = vars; v; v = TREE_CHAIN (v))
  3362. {
  3363. tree var = TREE_VALUE (v);
  3364. if (!var_finalized_p (var))
  3365. {
  3366. import_export_decl (var);
  3367. rest_of_decl_compilation (var, 1, 1);
  3368. }
  3369. }
  3370. }
  3371. /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
  3372. (otherwise) that will initialize all global objects with static
  3373. storage duration having the indicated PRIORITY. */
  3374. static void
  3375. generate_ctor_or_dtor_function (bool constructor_p, int priority,
  3376. location_t *locus)
  3377. {
  3378. char function_key;
  3379. tree fndecl;
  3380. tree body;
  3381. size_t i;
  3382. input_location = *locus;
  3383. /* ??? */
  3384. /* Was: locus->line++; */
  3385. /* We use `I' to indicate initialization and `D' to indicate
  3386. destruction. */
  3387. function_key = constructor_p ? 'I' : 'D';
  3388. /* We emit the function lazily, to avoid generating empty
  3389. global constructors and destructors. */
  3390. body = NULL_TREE;
  3391. /* For Objective-C++, we may need to initialize metadata found in this module.
  3392. This must be done _before_ any other static initializations. */
  3393. if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY)
  3394. && constructor_p && objc_static_init_needed_p ())
  3395. {
  3396. body = start_objects (function_key, priority);
  3397. objc_generate_static_init_call (NULL_TREE);
  3398. }
  3399. /* Call the static storage duration function with appropriate
  3400. arguments. */
  3401. FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl)
  3402. {
  3403. /* Calls to pure or const functions will expand to nothing. */
  3404. if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE)))
  3405. {
  3406. tree call;
  3407. if (! body)
  3408. body = start_objects (function_key, priority);
  3409. call = cp_build_function_call_nary (fndecl, tf_warning_or_error,
  3410. build_int_cst (NULL_TREE,
  3411. constructor_p),
  3412. build_int_cst (NULL_TREE,
  3413. priority),
  3414. NULL_TREE);
  3415. finish_expr_stmt (call);
  3416. }
  3417. }
  3418. /* Close out the function. */
  3419. if (body)
  3420. finish_objects (function_key, priority, body);
  3421. }
  3422. /* Generate constructor and destructor functions for the priority
  3423. indicated by N. */
  3424. static int
  3425. generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data)
  3426. {
  3427. location_t *locus = (location_t *) data;
  3428. int priority = (int) n->key;
  3429. priority_info pi = (priority_info) n->value;
  3430. /* Generate the functions themselves, but only if they are really
  3431. needed. */
  3432. if (pi->initializations_p)
  3433. generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus);
  3434. if (pi->destructions_p)
  3435. generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus);
  3436. /* Keep iterating. */
  3437. return 0;
  3438. }
  3439. /* Java requires that we be able to reference a local address for a
  3440. method, and not be confused by PLT entries. If hidden aliases are
  3441. supported, collect and return all the functions for which we should
  3442. emit a hidden alias. */
  3443. static hash_set<tree> *
  3444. collect_candidates_for_java_method_aliases (void)
  3445. {
  3446. struct cgraph_node *node;
  3447. hash_set<tree> *candidates = NULL;
  3448. #ifndef HAVE_GAS_HIDDEN
  3449. return candidates;
  3450. #endif
  3451. FOR_EACH_FUNCTION (node)
  3452. {
  3453. tree fndecl = node->decl;
  3454. if (DECL_CLASS_SCOPE_P (fndecl)
  3455. && TYPE_FOR_JAVA (DECL_CONTEXT (fndecl))
  3456. && TARGET_USE_LOCAL_THUNK_ALIAS_P (fndecl))
  3457. {
  3458. if (candidates == NULL)
  3459. candidates = new hash_set<tree>;
  3460. candidates->add (fndecl);
  3461. }
  3462. }
  3463. return candidates;
  3464. }
  3465. /* Java requires that we be able to reference a local address for a
  3466. method, and not be confused by PLT entries. If hidden aliases are
  3467. supported, emit one for each java function that we've emitted.
  3468. CANDIDATES is the set of FUNCTION_DECLs that were gathered
  3469. by collect_candidates_for_java_method_aliases. */
  3470. static void
  3471. build_java_method_aliases (hash_set<tree> *candidates)
  3472. {
  3473. struct cgraph_node *node;
  3474. #ifndef HAVE_GAS_HIDDEN
  3475. return;
  3476. #endif
  3477. FOR_EACH_FUNCTION (node)
  3478. {
  3479. tree fndecl = node->decl;
  3480. if (TREE_ASM_WRITTEN (fndecl)
  3481. && candidates->contains (fndecl))
  3482. {
  3483. /* Mangle the name in a predictable way; we need to reference
  3484. this from a java compiled object file. */
  3485. tree oid, nid, alias;
  3486. const char *oname;
  3487. char *nname;
  3488. oid = DECL_ASSEMBLER_NAME (fndecl);
  3489. oname = IDENTIFIER_POINTER (oid);
  3490. gcc_assert (oname[0] == '_' && oname[1] == 'Z');
  3491. nname = ACONCAT (("_ZGA", oname+2, NULL));
  3492. nid = get_identifier (nname);
  3493. alias = make_alias_for (fndecl, nid);
  3494. TREE_PUBLIC (alias) = 1;
  3495. DECL_VISIBILITY (alias) = VISIBILITY_HIDDEN;
  3496. assemble_alias (alias, oid);
  3497. }
  3498. }
  3499. }
  3500. /* Return C++ property of T, based on given operation OP. */
  3501. static int
  3502. cpp_check (tree t, cpp_operation op)
  3503. {
  3504. switch (op)
  3505. {
  3506. case IS_ABSTRACT:
  3507. return DECL_PURE_VIRTUAL_P (t);
  3508. case IS_CONSTRUCTOR:
  3509. return DECL_CONSTRUCTOR_P (t);
  3510. case IS_DESTRUCTOR:
  3511. return DECL_DESTRUCTOR_P (t);
  3512. case IS_COPY_CONSTRUCTOR:
  3513. return DECL_COPY_CONSTRUCTOR_P (t);
  3514. case IS_TEMPLATE:
  3515. return TREE_CODE (t) == TEMPLATE_DECL;
  3516. case IS_TRIVIAL:
  3517. return trivial_type_p (t);
  3518. default:
  3519. return 0;
  3520. }
  3521. }
  3522. /* Collect source file references recursively, starting from NAMESPC. */
  3523. static void
  3524. collect_source_refs (tree namespc)
  3525. {
  3526. tree t;
  3527. if (!namespc)
  3528. return;
  3529. /* Iterate over names in this name space. */
  3530. for (t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t))
  3531. if (!DECL_IS_BUILTIN (t) )
  3532. collect_source_ref (DECL_SOURCE_FILE (t));
  3533. /* Dump siblings, if any */
  3534. collect_source_refs (TREE_CHAIN (namespc));
  3535. /* Dump children, if any */
  3536. collect_source_refs (NAMESPACE_LEVEL (namespc)->namespaces);
  3537. }
  3538. /* Collect decls relevant to SOURCE_FILE from all namespaces recursively,
  3539. starting from NAMESPC. */
  3540. static void
  3541. collect_ada_namespace (tree namespc, const char *source_file)
  3542. {
  3543. if (!namespc)
  3544. return;
  3545. /* Collect decls from this namespace */
  3546. collect_ada_nodes (NAMESPACE_LEVEL (namespc)->names, source_file);
  3547. /* Collect siblings, if any */
  3548. collect_ada_namespace (TREE_CHAIN (namespc), source_file);
  3549. /* Collect children, if any */
  3550. collect_ada_namespace (NAMESPACE_LEVEL (namespc)->namespaces, source_file);
  3551. }
  3552. /* Returns true iff there is a definition available for variable or
  3553. function DECL. */
  3554. static bool
  3555. decl_defined_p (tree decl)
  3556. {
  3557. if (TREE_CODE (decl) == FUNCTION_DECL)
  3558. return (DECL_INITIAL (decl) != NULL_TREE);
  3559. else
  3560. {
  3561. gcc_assert (VAR_P (decl));
  3562. return !DECL_EXTERNAL (decl);
  3563. }
  3564. }
  3565. /* Nonzero for a VAR_DECL whose value can be used in a constant expression.
  3566. [expr.const]
  3567. An integral constant-expression can only involve ... const
  3568. variables of integral or enumeration types initialized with
  3569. constant expressions ...
  3570. C++0x also allows constexpr variables and temporaries initialized
  3571. with constant expressions. We handle the former here, but the latter
  3572. are just folded away in cxx_eval_constant_expression.
  3573. The standard does not require that the expression be non-volatile.
  3574. G++ implements the proposed correction in DR 457. */
  3575. bool
  3576. decl_constant_var_p (tree decl)
  3577. {
  3578. if (!decl_maybe_constant_var_p (decl))
  3579. return false;
  3580. /* We don't know if a template static data member is initialized with
  3581. a constant expression until we instantiate its initializer. Even
  3582. in the case of a constexpr variable, we can't treat it as a
  3583. constant until its initializer is complete in case it's used in
  3584. its own initializer. */
  3585. mark_used (decl);
  3586. return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl);
  3587. }
  3588. /* Returns true if DECL could be a symbolic constant variable, depending on
  3589. its initializer. */
  3590. bool
  3591. decl_maybe_constant_var_p (tree decl)
  3592. {
  3593. tree type = TREE_TYPE (decl);
  3594. if (!VAR_P (decl))
  3595. return false;
  3596. if (DECL_DECLARED_CONSTEXPR_P (decl))
  3597. return true;
  3598. return (CP_TYPE_CONST_NON_VOLATILE_P (type)
  3599. && INTEGRAL_OR_ENUMERATION_TYPE_P (type));
  3600. }
  3601. /* Complain that DECL uses a type with no linkage. In C++98 mode this is
  3602. called from grokfndecl and grokvardecl; in all modes it is called from
  3603. cp_write_global_declarations. */
  3604. void
  3605. no_linkage_error (tree decl)
  3606. {
  3607. if (cxx_dialect >= cxx11 && decl_defined_p (decl))
  3608. /* In C++11 it's ok if the decl is defined. */
  3609. return;
  3610. tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
  3611. if (t == NULL_TREE)
  3612. /* The type that got us on no_linkage_decls must have gotten a name for
  3613. linkage purposes. */;
  3614. else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t))
  3615. /* The type might end up having a typedef name for linkage purposes. */
  3616. vec_safe_push (no_linkage_decls, decl);
  3617. else if (TYPE_ANONYMOUS_P (t))
  3618. {
  3619. bool d = false;
  3620. if (cxx_dialect >= cxx11)
  3621. d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
  3622. "anonymous type, is used but never defined", decl);
  3623. else if (DECL_EXTERN_C_P (decl))
  3624. /* Allow this; it's pretty common in C. */;
  3625. else if (TREE_CODE (decl) == VAR_DECL)
  3626. /* DRs 132, 319 and 389 seem to indicate types with
  3627. no linkage can only be used to declare extern "C"
  3628. entities. Since it's not always an error in the
  3629. ISO C++ 90 Standard, we only issue a warning. */
  3630. d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "anonymous type "
  3631. "with no linkage used to declare variable %q#D with "
  3632. "linkage", decl);
  3633. else
  3634. d = permerror (DECL_SOURCE_LOCATION (decl), "anonymous type with no "
  3635. "linkage used to declare function %q#D with linkage",
  3636. decl);
  3637. if (d && is_typedef_decl (TYPE_NAME (t)))
  3638. inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
  3639. "to the unqualified type, so it is not used for linkage",
  3640. TYPE_NAME (t));
  3641. }
  3642. else if (cxx_dialect >= cxx11)
  3643. permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using local type "
  3644. "%qT, is used but never defined", decl, t);
  3645. else if (TREE_CODE (decl) == VAR_DECL)
  3646. warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
  3647. "used to declare variable %q#D with linkage", t, decl);
  3648. else
  3649. permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used "
  3650. "to declare function %q#D with linkage", t, decl);
  3651. }
  3652. /* Collect declarations from all namespaces relevant to SOURCE_FILE. */
  3653. static void
  3654. collect_all_refs (const char *source_file)
  3655. {
  3656. collect_ada_namespace (global_namespace, source_file);
  3657. }
  3658. /* Clear DECL_EXTERNAL for NODE. */
  3659. static bool
  3660. clear_decl_external (struct cgraph_node *node, void * /*data*/)
  3661. {
  3662. DECL_EXTERNAL (node->decl) = 0;
  3663. return false;
  3664. }
  3665. /* Build up the function to run dynamic initializers for thread_local
  3666. variables in this translation unit and alias the init functions for the
  3667. individual variables to it. */
  3668. static void
  3669. handle_tls_init (void)
  3670. {
  3671. tree vars = prune_vars_needing_no_initialization (&tls_aggregates);
  3672. if (vars == NULL_TREE)
  3673. return;
  3674. location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars));
  3675. write_out_vars (vars);
  3676. tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"),
  3677. boolean_type_node);
  3678. TREE_PUBLIC (guard) = false;
  3679. TREE_STATIC (guard) = true;
  3680. DECL_ARTIFICIAL (guard) = true;
  3681. DECL_IGNORED_P (guard) = true;
  3682. TREE_USED (guard) = true;
  3683. set_decl_tls_model (guard, decl_default_tls_model (guard));
  3684. pushdecl_top_level_and_finish (guard, NULL_TREE);
  3685. tree fn = get_local_tls_init_fn ();
  3686. start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED);
  3687. tree body = begin_function_body ();
  3688. tree if_stmt = begin_if_stmt ();
  3689. tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false,
  3690. tf_warning_or_error);
  3691. finish_if_stmt_cond (cond, if_stmt);
  3692. finish_expr_stmt (cp_build_modify_expr (guard, NOP_EXPR, boolean_true_node,
  3693. tf_warning_or_error));
  3694. for (; vars; vars = TREE_CHAIN (vars))
  3695. {
  3696. tree var = TREE_VALUE (vars);
  3697. tree init = TREE_PURPOSE (vars);
  3698. one_static_initialization_or_destruction (var, init, true);
  3699. #ifdef ASM_OUTPUT_DEF
  3700. /* Output init aliases even with -fno-extern-tls-init. */
  3701. if (TREE_PUBLIC (var))
  3702. {
  3703. tree single_init_fn = get_tls_init_fn (var);
  3704. if (single_init_fn == NULL_TREE)
  3705. continue;
  3706. cgraph_node *alias
  3707. = cgraph_node::get_create (fn)->create_same_body_alias
  3708. (single_init_fn, fn);
  3709. gcc_assert (alias != NULL);
  3710. }
  3711. #endif
  3712. }
  3713. finish_then_clause (if_stmt);
  3714. finish_if_stmt (if_stmt);
  3715. finish_function_body (body);
  3716. expand_or_defer_fn (finish_function (0));
  3717. }
  3718. /* The entire file is now complete. If requested, dump everything
  3719. to a file. */
  3720. static void
  3721. dump_tu (void)
  3722. {
  3723. int flags;
  3724. FILE *stream = dump_begin (TDI_tu, &flags);
  3725. if (stream)
  3726. {
  3727. dump_node (global_namespace, flags & ~TDF_SLIM, stream);
  3728. dump_end (TDI_tu, stream);
  3729. }
  3730. }
  3731. /* Check the deallocation functions for CODE to see if we want to warn that
  3732. only one was defined. */
  3733. static void
  3734. maybe_warn_sized_delete (enum tree_code code)
  3735. {
  3736. tree sized = NULL_TREE;
  3737. tree unsized = NULL_TREE;
  3738. for (tree ovl = IDENTIFIER_GLOBAL_VALUE (ansi_opname (code));
  3739. ovl; ovl = OVL_NEXT (ovl))
  3740. {
  3741. tree fn = OVL_CURRENT (ovl);
  3742. /* We're only interested in usual deallocation functions. */
  3743. if (!non_placement_deallocation_fn_p (fn))
  3744. continue;
  3745. if (FUNCTION_ARG_CHAIN (fn) == void_list_node)
  3746. unsized = fn;
  3747. else
  3748. sized = fn;
  3749. }
  3750. if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized))
  3751. warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation,
  3752. "the program should also define %qD", sized);
  3753. else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized))
  3754. warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation,
  3755. "the program should also define %qD", unsized);
  3756. }
  3757. /* Check the global deallocation functions to see if we want to warn about
  3758. defining unsized without sized (or vice versa). */
  3759. static void
  3760. maybe_warn_sized_delete ()
  3761. {
  3762. if (!flag_sized_deallocation || !warn_sized_deallocation)
  3763. return;
  3764. maybe_warn_sized_delete (DELETE_EXPR);
  3765. maybe_warn_sized_delete (VEC_DELETE_EXPR);
  3766. }
  3767. /* This routine is called at the end of compilation.
  3768. Its job is to create all the code needed to initialize and
  3769. destroy the global aggregates. We do the destruction
  3770. first, since that way we only need to reverse the decls once. */
  3771. void
  3772. cp_write_global_declarations (void)
  3773. {
  3774. tree vars;
  3775. bool reconsider;
  3776. size_t i;
  3777. location_t locus;
  3778. unsigned ssdf_count = 0;
  3779. int retries = 0;
  3780. tree decl;
  3781. hash_set<tree> *candidates;
  3782. locus = input_location;
  3783. at_eof = 1;
  3784. /* Bad parse errors. Just forget about it. */
  3785. if (! global_bindings_p () || current_class_type
  3786. || !vec_safe_is_empty (decl_namespace_list))
  3787. return;
  3788. /* This is the point to write out a PCH if we're doing that.
  3789. In that case we do not want to do anything else. */
  3790. if (pch_file)
  3791. {
  3792. c_common_write_pch ();
  3793. dump_tu ();
  3794. return;
  3795. }
  3796. symtab->process_same_body_aliases ();
  3797. /* Handle -fdump-ada-spec[-slim] */
  3798. if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
  3799. {
  3800. if (flag_dump_ada_spec_slim)
  3801. collect_source_ref (main_input_filename);
  3802. else
  3803. collect_source_refs (global_namespace);
  3804. dump_ada_specs (collect_all_refs, cpp_check);
  3805. }
  3806. /* FIXME - huh? was input_line -= 1;*/
  3807. timevar_start (TV_PHASE_DEFERRED);
  3808. /* We now have to write out all the stuff we put off writing out.
  3809. These include:
  3810. o Template specializations that we have not yet instantiated,
  3811. but which are needed.
  3812. o Initialization and destruction for non-local objects with
  3813. static storage duration. (Local objects with static storage
  3814. duration are initialized when their scope is first entered,
  3815. and are cleaned up via atexit.)
  3816. o Virtual function tables.
  3817. All of these may cause others to be needed. For example,
  3818. instantiating one function may cause another to be needed, and
  3819. generating the initializer for an object may cause templates to be
  3820. instantiated, etc., etc. */
  3821. emit_support_tinfos ();
  3822. do
  3823. {
  3824. tree t;
  3825. tree decl;
  3826. reconsider = false;
  3827. /* If there are templates that we've put off instantiating, do
  3828. them now. */
  3829. instantiate_pending_templates (retries);
  3830. ggc_collect ();
  3831. /* Write out virtual tables as required. Note that writing out
  3832. the virtual table for a template class may cause the
  3833. instantiation of members of that class. If we write out
  3834. vtables then we remove the class from our list so we don't
  3835. have to look at it again. */
  3836. while (keyed_classes != NULL_TREE
  3837. && maybe_emit_vtables (TREE_VALUE (keyed_classes)))
  3838. {
  3839. reconsider = true;
  3840. keyed_classes = TREE_CHAIN (keyed_classes);
  3841. }
  3842. t = keyed_classes;
  3843. if (t != NULL_TREE)
  3844. {
  3845. tree next = TREE_CHAIN (t);
  3846. while (next)
  3847. {
  3848. if (maybe_emit_vtables (TREE_VALUE (next)))
  3849. {
  3850. reconsider = true;
  3851. TREE_CHAIN (t) = TREE_CHAIN (next);
  3852. }
  3853. else
  3854. t = next;
  3855. next = TREE_CHAIN (t);
  3856. }
  3857. }
  3858. /* Write out needed type info variables. We have to be careful
  3859. looping through unemitted decls, because emit_tinfo_decl may
  3860. cause other variables to be needed. New elements will be
  3861. appended, and we remove from the vector those that actually
  3862. get emitted. */
  3863. for (i = unemitted_tinfo_decls->length ();
  3864. unemitted_tinfo_decls->iterate (--i, &t);)
  3865. if (emit_tinfo_decl (t))
  3866. {
  3867. reconsider = true;
  3868. unemitted_tinfo_decls->unordered_remove (i);
  3869. }
  3870. /* The list of objects with static storage duration is built up
  3871. in reverse order. We clear STATIC_AGGREGATES so that any new
  3872. aggregates added during the initialization of these will be
  3873. initialized in the correct order when we next come around the
  3874. loop. */
  3875. vars = prune_vars_needing_no_initialization (&static_aggregates);
  3876. if (vars)
  3877. {
  3878. /* We need to start a new initialization function each time
  3879. through the loop. That's because we need to know which
  3880. vtables have been referenced, and TREE_SYMBOL_REFERENCED
  3881. isn't computed until a function is finished, and written
  3882. out. That's a deficiency in the back end. When this is
  3883. fixed, these initialization functions could all become
  3884. inline, with resulting performance improvements. */
  3885. tree ssdf_body;
  3886. /* Set the line and file, so that it is obviously not from
  3887. the source file. */
  3888. input_location = locus;
  3889. ssdf_body = start_static_storage_duration_function (ssdf_count);
  3890. /* Make sure the back end knows about all the variables. */
  3891. write_out_vars (vars);
  3892. /* First generate code to do all the initializations. */
  3893. if (vars)
  3894. do_static_initialization_or_destruction (vars, /*initp=*/true);
  3895. /* Then, generate code to do all the destructions. Do these
  3896. in reverse order so that the most recently constructed
  3897. variable is the first destroyed. If we're using
  3898. __cxa_atexit, then we don't need to do this; functions
  3899. were registered at initialization time to destroy the
  3900. local statics. */
  3901. if (!flag_use_cxa_atexit && vars)
  3902. {
  3903. vars = nreverse (vars);
  3904. do_static_initialization_or_destruction (vars, /*initp=*/false);
  3905. }
  3906. else
  3907. vars = NULL_TREE;
  3908. /* Finish up the static storage duration function for this
  3909. round. */
  3910. input_location = locus;
  3911. finish_static_storage_duration_function (ssdf_body);
  3912. /* All those initializations and finalizations might cause
  3913. us to need more inline functions, more template
  3914. instantiations, etc. */
  3915. reconsider = true;
  3916. ssdf_count++;
  3917. /* ??? was: locus.line++; */
  3918. }
  3919. /* Now do the same for thread_local variables. */
  3920. handle_tls_init ();
  3921. /* Go through the set of inline functions whose bodies have not
  3922. been emitted yet. If out-of-line copies of these functions
  3923. are required, emit them. */
  3924. FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
  3925. {
  3926. /* Does it need synthesizing? */
  3927. if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl)
  3928. && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl)))
  3929. {
  3930. /* Even though we're already at the top-level, we push
  3931. there again. That way, when we pop back a few lines
  3932. hence, all of our state is restored. Otherwise,
  3933. finish_function doesn't clean things up, and we end
  3934. up with CURRENT_FUNCTION_DECL set. */
  3935. push_to_top_level ();
  3936. /* The decl's location will mark where it was first
  3937. needed. Save that so synthesize method can indicate
  3938. where it was needed from, in case of error */
  3939. input_location = DECL_SOURCE_LOCATION (decl);
  3940. synthesize_method (decl);
  3941. pop_from_top_level ();
  3942. reconsider = true;
  3943. }
  3944. if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl))
  3945. generate_tls_wrapper (decl);
  3946. if (!DECL_SAVED_TREE (decl))
  3947. continue;
  3948. /* We lie to the back end, pretending that some functions
  3949. are not defined when they really are. This keeps these
  3950. functions from being put out unnecessarily. But, we must
  3951. stop lying when the functions are referenced, or if they
  3952. are not comdat since they need to be put out now. If
  3953. DECL_INTERFACE_KNOWN, then we have already set
  3954. DECL_EXTERNAL appropriately, so there's no need to check
  3955. again, and we do not want to clear DECL_EXTERNAL if a
  3956. previous call to import_export_decl set it.
  3957. This is done in a separate for cycle, because if some
  3958. deferred function is contained in another deferred
  3959. function later in deferred_fns varray,
  3960. rest_of_compilation would skip this function and we
  3961. really cannot expand the same function twice. */
  3962. import_export_decl (decl);
  3963. if (DECL_NOT_REALLY_EXTERN (decl)
  3964. && DECL_INITIAL (decl)
  3965. && decl_needed_p (decl))
  3966. {
  3967. struct cgraph_node *node, *next;
  3968. node = cgraph_node::get (decl);
  3969. if (node->cpp_implicit_alias)
  3970. node = node->get_alias_target ();
  3971. node->call_for_symbol_thunks_and_aliases (clear_decl_external,
  3972. NULL, true);
  3973. /* If we mark !DECL_EXTERNAL one of the symbols in some comdat
  3974. group, we need to mark all symbols in the same comdat group
  3975. that way. */
  3976. if (node->same_comdat_group)
  3977. for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
  3978. next != node;
  3979. next = dyn_cast<cgraph_node *> (next->same_comdat_group))
  3980. next->call_for_symbol_thunks_and_aliases (clear_decl_external,
  3981. NULL, true);
  3982. }
  3983. /* If we're going to need to write this function out, and
  3984. there's already a body for it, create RTL for it now.
  3985. (There might be no body if this is a method we haven't
  3986. gotten around to synthesizing yet.) */
  3987. if (!DECL_EXTERNAL (decl)
  3988. && decl_needed_p (decl)
  3989. && !TREE_ASM_WRITTEN (decl)
  3990. && !cgraph_node::get (decl)->definition)
  3991. {
  3992. /* We will output the function; no longer consider it in this
  3993. loop. */
  3994. DECL_DEFER_OUTPUT (decl) = 0;
  3995. /* Generate RTL for this function now that we know we
  3996. need it. */
  3997. expand_or_defer_fn (decl);
  3998. /* If we're compiling -fsyntax-only pretend that this
  3999. function has been written out so that we don't try to
  4000. expand it again. */
  4001. if (flag_syntax_only)
  4002. TREE_ASM_WRITTEN (decl) = 1;
  4003. reconsider = true;
  4004. }
  4005. }
  4006. if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
  4007. reconsider = true;
  4008. /* Static data members are just like namespace-scope globals. */
  4009. FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl)
  4010. {
  4011. if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl)
  4012. /* Don't write it out if we haven't seen a definition. */
  4013. || DECL_IN_AGGR_P (decl))
  4014. continue;
  4015. import_export_decl (decl);
  4016. /* If this static data member is needed, provide it to the
  4017. back end. */
  4018. if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
  4019. DECL_EXTERNAL (decl) = 0;
  4020. }
  4021. if (vec_safe_length (pending_statics) != 0
  4022. && wrapup_global_declarations (pending_statics->address (),
  4023. pending_statics->length ()))
  4024. reconsider = true;
  4025. retries++;
  4026. }
  4027. while (reconsider);
  4028. /* All used inline functions must have a definition at this point. */
  4029. FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl)
  4030. {
  4031. if (/* Check online inline functions that were actually used. */
  4032. DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl)
  4033. /* If the definition actually was available here, then the
  4034. fact that the function was not defined merely represents
  4035. that for some reason (use of a template repository,
  4036. #pragma interface, etc.) we decided not to emit the
  4037. definition here. */
  4038. && !DECL_INITIAL (decl)
  4039. /* Don't complain if the template was defined. */
  4040. && !(DECL_TEMPLATE_INSTANTIATION (decl)
  4041. && DECL_INITIAL (DECL_TEMPLATE_RESULT
  4042. (template_for_substitution (decl)))))
  4043. {
  4044. warning (0, "inline function %q+D used but never defined", decl);
  4045. /* Avoid a duplicate warning from check_global_declaration_1. */
  4046. TREE_NO_WARNING (decl) = 1;
  4047. }
  4048. }
  4049. /* So must decls that use a type with no linkage. */
  4050. FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl)
  4051. no_linkage_error (decl);
  4052. maybe_warn_sized_delete ();
  4053. /* Then, do the Objective-C stuff. This is where all the
  4054. Objective-C module stuff gets generated (symtab,
  4055. class/protocol/selector lists etc). This must be done after C++
  4056. templates, destructors etc. so that selectors used in C++
  4057. templates are properly allocated. */
  4058. if (c_dialect_objc ())
  4059. objc_write_global_declarations ();
  4060. /* We give C linkage to static constructors and destructors. */
  4061. push_lang_context (lang_name_c);
  4062. /* Generate initialization and destruction functions for all
  4063. priorities for which they are required. */
  4064. if (priority_info_map)
  4065. splay_tree_foreach (priority_info_map,
  4066. generate_ctor_and_dtor_functions_for_priority,
  4067. /*data=*/&locus);
  4068. else if (c_dialect_objc () && objc_static_init_needed_p ())
  4069. /* If this is obj-c++ and we need a static init, call
  4070. generate_ctor_or_dtor_function. */
  4071. generate_ctor_or_dtor_function (/*constructor_p=*/true,
  4072. DEFAULT_INIT_PRIORITY, &locus);
  4073. /* We're done with the splay-tree now. */
  4074. if (priority_info_map)
  4075. splay_tree_delete (priority_info_map);
  4076. /* Generate any missing aliases. */
  4077. maybe_apply_pending_pragma_weaks ();
  4078. /* We're done with static constructors, so we can go back to "C++"
  4079. linkage now. */
  4080. pop_lang_context ();
  4081. /* Collect candidates for Java hidden aliases. */
  4082. candidates = collect_candidates_for_java_method_aliases ();
  4083. timevar_stop (TV_PHASE_DEFERRED);
  4084. timevar_start (TV_PHASE_OPT_GEN);
  4085. if (flag_vtable_verify)
  4086. {
  4087. vtv_recover_class_info ();
  4088. vtv_compute_class_hierarchy_transitive_closure ();
  4089. vtv_build_vtable_verify_fndecl ();
  4090. }
  4091. symtab->finalize_compilation_unit ();
  4092. if (flag_vtable_verify)
  4093. {
  4094. /* Generate the special constructor initialization function that
  4095. calls __VLTRegisterPairs, and give it a very high
  4096. initialization priority. This must be done after
  4097. finalize_compilation_unit so that we have accurate
  4098. information about which vtable will actually be emitted. */
  4099. vtv_generate_init_routine ();
  4100. }
  4101. timevar_stop (TV_PHASE_OPT_GEN);
  4102. timevar_start (TV_PHASE_CHECK_DBGINFO);
  4103. /* Now, issue warnings about static, but not defined, functions,
  4104. etc., and emit debugging information. */
  4105. walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
  4106. if (vec_safe_length (pending_statics) != 0)
  4107. {
  4108. check_global_declarations (pending_statics->address (),
  4109. pending_statics->length ());
  4110. emit_debug_global_declarations (pending_statics->address (),
  4111. pending_statics->length ());
  4112. }
  4113. perform_deferred_noexcept_checks ();
  4114. /* Generate hidden aliases for Java. */
  4115. if (candidates)
  4116. {
  4117. build_java_method_aliases (candidates);
  4118. delete candidates;
  4119. }
  4120. finish_repo ();
  4121. /* The entire file is now complete. If requested, dump everything
  4122. to a file. */
  4123. dump_tu ();
  4124. if (flag_detailed_statistics)
  4125. {
  4126. dump_tree_statistics ();
  4127. dump_time_statistics ();
  4128. }
  4129. input_location = locus;
  4130. #ifdef ENABLE_CHECKING
  4131. validate_conversion_obstack ();
  4132. #endif /* ENABLE_CHECKING */
  4133. timevar_stop (TV_PHASE_CHECK_DBGINFO);
  4134. }
  4135. /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the
  4136. function to call in parse-tree form; it has not yet been
  4137. semantically analyzed. ARGS are the arguments to the function.
  4138. They have already been semantically analyzed. This may change
  4139. ARGS. */
  4140. tree
  4141. build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args,
  4142. tsubst_flags_t complain)
  4143. {
  4144. tree orig_fn;
  4145. vec<tree, va_gc> *orig_args = NULL;
  4146. tree expr;
  4147. tree object;
  4148. orig_fn = fn;
  4149. object = TREE_OPERAND (fn, 0);
  4150. if (processing_template_decl)
  4151. {
  4152. gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR
  4153. || TREE_CODE (fn) == MEMBER_REF);
  4154. if (type_dependent_expression_p (fn)
  4155. || any_type_dependent_arguments_p (*args))
  4156. return build_nt_call_vec (fn, *args);
  4157. orig_args = make_tree_vector_copy (*args);
  4158. /* Transform the arguments and add the implicit "this"
  4159. parameter. That must be done before the FN is transformed
  4160. because we depend on the form of FN. */
  4161. make_args_non_dependent (*args);
  4162. object = build_non_dependent_expr (object);
  4163. if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
  4164. {
  4165. if (TREE_CODE (fn) == DOTSTAR_EXPR)
  4166. object = cp_build_addr_expr (object, complain);
  4167. vec_safe_insert (*args, 0, object);
  4168. }
  4169. /* Now that the arguments are done, transform FN. */
  4170. fn = build_non_dependent_expr (fn);
  4171. }
  4172. /* A qualified name corresponding to a bound pointer-to-member is
  4173. represented as an OFFSET_REF:
  4174. struct B { void g(); };
  4175. void (B::*p)();
  4176. void B::g() { (this->*p)(); } */
  4177. if (TREE_CODE (fn) == OFFSET_REF)
  4178. {
  4179. tree object_addr = cp_build_addr_expr (object, complain);
  4180. fn = TREE_OPERAND (fn, 1);
  4181. fn = get_member_function_from_ptrfunc (&object_addr, fn,
  4182. complain);
  4183. vec_safe_insert (*args, 0, object_addr);
  4184. }
  4185. if (CLASS_TYPE_P (TREE_TYPE (fn)))
  4186. expr = build_op_call (fn, args, complain);
  4187. else
  4188. expr = cp_build_function_call_vec (fn, args, complain);
  4189. if (processing_template_decl && expr != error_mark_node)
  4190. expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args);
  4191. if (orig_args != NULL)
  4192. release_tree_vector (orig_args);
  4193. return expr;
  4194. }
  4195. void
  4196. check_default_args (tree x)
  4197. {
  4198. tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
  4199. bool saw_def = false;
  4200. int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
  4201. for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
  4202. {
  4203. if (TREE_PURPOSE (arg))
  4204. saw_def = true;
  4205. else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg)))
  4206. {
  4207. error ("default argument missing for parameter %P of %q+#D", i, x);
  4208. TREE_PURPOSE (arg) = error_mark_node;
  4209. }
  4210. }
  4211. }
  4212. /* Return true if function DECL can be inlined. This is used to force
  4213. instantiation of methods that might be interesting for inlining. */
  4214. bool
  4215. possibly_inlined_p (tree decl)
  4216. {
  4217. gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
  4218. if (DECL_UNINLINABLE (decl))
  4219. return false;
  4220. if (!optimize || pragma_java_exceptions)
  4221. return DECL_DECLARED_INLINE_P (decl);
  4222. /* When optimizing, we might inline everything when flatten
  4223. attribute or heuristics inlining for size or autoinlining
  4224. is used. */
  4225. return true;
  4226. }
  4227. /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program.
  4228. If DECL is a specialization or implicitly declared class member,
  4229. generate the actual definition. Return false if something goes
  4230. wrong, true otherwise. */
  4231. bool
  4232. mark_used (tree decl, tsubst_flags_t complain)
  4233. {
  4234. /* If DECL is a BASELINK for a single function, then treat it just
  4235. like the DECL for the function. Otherwise, if the BASELINK is
  4236. for an overloaded function, we don't know which function was
  4237. actually used until after overload resolution. */
  4238. if (BASELINK_P (decl))
  4239. {
  4240. decl = BASELINK_FUNCTIONS (decl);
  4241. if (really_overloaded_fn (decl))
  4242. return true;
  4243. decl = OVL_CURRENT (decl);
  4244. }
  4245. /* Set TREE_USED for the benefit of -Wunused. */
  4246. TREE_USED (decl) = 1;
  4247. if (DECL_CLONED_FUNCTION_P (decl))
  4248. TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1;
  4249. /* Mark enumeration types as used. */
  4250. if (TREE_CODE (decl) == CONST_DECL)
  4251. used_types_insert (DECL_CONTEXT (decl));
  4252. if (TREE_CODE (decl) == FUNCTION_DECL)
  4253. maybe_instantiate_noexcept (decl);
  4254. if (TREE_CODE (decl) == FUNCTION_DECL
  4255. && DECL_DELETED_FN (decl))
  4256. {
  4257. if (DECL_ARTIFICIAL (decl))
  4258. {
  4259. if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR
  4260. && LAMBDA_TYPE_P (DECL_CONTEXT (decl)))
  4261. {
  4262. /* We mark a lambda conversion op as deleted if we can't
  4263. generate it properly; see maybe_add_lambda_conv_op. */
  4264. sorry ("converting lambda which uses %<...%> to "
  4265. "function pointer");
  4266. return false;
  4267. }
  4268. }
  4269. if (complain & tf_error)
  4270. {
  4271. error ("use of deleted function %qD", decl);
  4272. if (!maybe_explain_implicit_delete (decl))
  4273. inform (DECL_SOURCE_LOCATION (decl), "declared here");
  4274. }
  4275. return false;
  4276. }
  4277. if (TREE_DEPRECATED (decl) && (complain & tf_warning)
  4278. && deprecated_state != DEPRECATED_SUPPRESS)
  4279. warn_deprecated_use (decl, NULL_TREE);
  4280. /* We can only check DECL_ODR_USED on variables or functions with
  4281. DECL_LANG_SPECIFIC set, and these are also the only decls that we
  4282. might need special handling for. */
  4283. if (!VAR_OR_FUNCTION_DECL_P (decl)
  4284. || DECL_LANG_SPECIFIC (decl) == NULL
  4285. || DECL_THUNK_P (decl))
  4286. {
  4287. if (!processing_template_decl && type_uses_auto (TREE_TYPE (decl)))
  4288. {
  4289. if (complain & tf_error)
  4290. error ("use of %qD before deduction of %<auto%>", decl);
  4291. return false;
  4292. }
  4293. return true;
  4294. }
  4295. /* We only want to do this processing once. We don't need to keep trying
  4296. to instantiate inline templates, because unit-at-a-time will make sure
  4297. we get them compiled before functions that want to inline them. */
  4298. if (DECL_ODR_USED (decl))
  4299. return true;
  4300. /* If within finish_function, defer the rest until that function
  4301. finishes, otherwise it might recurse. */
  4302. if (defer_mark_used_calls)
  4303. {
  4304. vec_safe_push (deferred_mark_used_calls, decl);
  4305. return true;
  4306. }
  4307. /* Normally, we can wait until instantiation-time to synthesize DECL.
  4308. However, if DECL is a static data member initialized with a constant
  4309. or a constexpr function, we need it right now because a reference to
  4310. such a data member or a call to such function is not value-dependent.
  4311. For a function that uses auto in the return type, we need to instantiate
  4312. it to find out its type. For OpenMP user defined reductions, we need
  4313. them instantiated for reduction clauses which inline them by hand
  4314. directly. */
  4315. if (DECL_LANG_SPECIFIC (decl)
  4316. && DECL_TEMPLATE_INFO (decl)
  4317. && (decl_maybe_constant_var_p (decl)
  4318. || (TREE_CODE (decl) == FUNCTION_DECL
  4319. && DECL_OMP_DECLARE_REDUCTION_P (decl))
  4320. || undeduced_auto_decl (decl))
  4321. && !uses_template_parms (DECL_TI_ARGS (decl)))
  4322. {
  4323. /* Instantiating a function will result in garbage collection. We
  4324. must treat this situation as if we were within the body of a
  4325. function so as to avoid collecting live data only referenced from
  4326. the stack (such as overload resolution candidates). */
  4327. ++function_depth;
  4328. instantiate_decl (decl, /*defer_ok=*/false,
  4329. /*expl_inst_class_mem_p=*/false);
  4330. --function_depth;
  4331. }
  4332. if (processing_template_decl || in_template_function ())
  4333. return true;
  4334. /* Check this too in case we're within instantiate_non_dependent_expr. */
  4335. if (DECL_TEMPLATE_INFO (decl)
  4336. && uses_template_parms (DECL_TI_ARGS (decl)))
  4337. return true;
  4338. if (undeduced_auto_decl (decl))
  4339. {
  4340. if (complain & tf_error)
  4341. error ("use of %qD before deduction of %<auto%>", decl);
  4342. return false;
  4343. }
  4344. /* If we don't need a value, then we don't need to synthesize DECL. */
  4345. if (cp_unevaluated_operand != 0)
  4346. return true;
  4347. DECL_ODR_USED (decl) = 1;
  4348. if (DECL_CLONED_FUNCTION_P (decl))
  4349. DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1;
  4350. /* DR 757: A type without linkage shall not be used as the type of a
  4351. variable or function with linkage, unless
  4352. o the variable or function has extern "C" linkage (7.5 [dcl.link]), or
  4353. o the variable or function is not used (3.2 [basic.def.odr]) or is
  4354. defined in the same translation unit. */
  4355. if (cxx_dialect > cxx98
  4356. && decl_linkage (decl) != lk_none
  4357. && !DECL_EXTERN_C_P (decl)
  4358. && !DECL_ARTIFICIAL (decl)
  4359. && !decl_defined_p (decl)
  4360. && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
  4361. {
  4362. if (is_local_extern (decl))
  4363. /* There's no way to define a local extern, and adding it to
  4364. the vector interferes with GC, so give an error now. */
  4365. no_linkage_error (decl);
  4366. else
  4367. vec_safe_push (no_linkage_decls, decl);
  4368. }
  4369. if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)
  4370. && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl))
  4371. /* Remember it, so we can check it was defined. */
  4372. note_vague_linkage_fn (decl);
  4373. /* Is it a synthesized method that needs to be synthesized? */
  4374. if (TREE_CODE (decl) == FUNCTION_DECL
  4375. && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
  4376. && DECL_DEFAULTED_FN (decl)
  4377. /* A function defaulted outside the class is synthesized either by
  4378. cp_finish_decl or instantiate_decl. */
  4379. && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)
  4380. && ! DECL_INITIAL (decl))
  4381. {
  4382. /* Defer virtual destructors so that thunks get the right
  4383. linkage. */
  4384. if (DECL_VIRTUAL_P (decl) && !at_eof)
  4385. {
  4386. note_vague_linkage_fn (decl);
  4387. return true;
  4388. }
  4389. /* Remember the current location for a function we will end up
  4390. synthesizing. Then we can inform the user where it was
  4391. required in the case of error. */
  4392. DECL_SOURCE_LOCATION (decl) = input_location;
  4393. /* Synthesizing an implicitly defined member function will result in
  4394. garbage collection. We must treat this situation as if we were
  4395. within the body of a function so as to avoid collecting live data
  4396. on the stack (such as overload resolution candidates).
  4397. We could just let cp_write_global_declarations handle synthesizing
  4398. this function by adding it to deferred_fns, but doing
  4399. it at the use site produces better error messages. */
  4400. ++function_depth;
  4401. synthesize_method (decl);
  4402. --function_depth;
  4403. /* If this is a synthesized method we don't need to
  4404. do the instantiation test below. */
  4405. }
  4406. else if (VAR_OR_FUNCTION_DECL_P (decl)
  4407. && DECL_TEMPLATE_INFO (decl)
  4408. && (!DECL_EXPLICIT_INSTANTIATION (decl)
  4409. || always_instantiate_p (decl)))
  4410. /* If this is a function or variable that is an instance of some
  4411. template, we now know that we will need to actually do the
  4412. instantiation. We check that DECL is not an explicit
  4413. instantiation because that is not checked in instantiate_decl.
  4414. We put off instantiating functions in order to improve compile
  4415. times. Maintaining a stack of active functions is expensive,
  4416. and the inliner knows to instantiate any functions it might
  4417. need. Therefore, we always try to defer instantiation. */
  4418. {
  4419. ++function_depth;
  4420. instantiate_decl (decl, /*defer_ok=*/true,
  4421. /*expl_inst_class_mem_p=*/false);
  4422. --function_depth;
  4423. }
  4424. return true;
  4425. }
  4426. bool
  4427. mark_used (tree decl)
  4428. {
  4429. return mark_used (decl, tf_warning_or_error);
  4430. }
  4431. tree
  4432. vtv_start_verification_constructor_init_function (void)
  4433. {
  4434. return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1);
  4435. }
  4436. tree
  4437. vtv_finish_verification_constructor_init_function (tree function_body)
  4438. {
  4439. tree fn;
  4440. finish_compound_stmt (function_body);
  4441. fn = finish_function (0);
  4442. DECL_STATIC_CONSTRUCTOR (fn) = 1;
  4443. decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1);
  4444. return fn;
  4445. }
  4446. #include "gt-cp-decl2.h"