init.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332
  1. /* Handle initialization things in C++.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann (tiemann@cygnus.com)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* High-level class interface. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "stringpool.h"
  32. #include "varasm.h"
  33. #include "cp-tree.h"
  34. #include "flags.h"
  35. #include "target.h"
  36. #include "gimplify.h"
  37. #include "wide-int.h"
  38. #include "c-family/c-ubsan.h"
  39. static bool begin_init_stmts (tree *, tree *);
  40. static tree finish_init_stmts (bool, tree, tree);
  41. static void construct_virtual_base (tree, tree);
  42. static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
  43. static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
  44. static void perform_member_init (tree, tree);
  45. static int member_init_ok_or_else (tree, tree, tree);
  46. static void expand_virtual_init (tree, tree);
  47. static tree sort_mem_initializers (tree, tree);
  48. static tree initializing_context (tree);
  49. static void expand_cleanup_for_base (tree, tree);
  50. static tree dfs_initialize_vtbl_ptrs (tree, void *);
  51. static tree build_field_list (tree, tree, int *);
  52. static int diagnose_uninitialized_cst_or_ref_member_1 (tree, tree, bool, bool);
  53. /* We are about to generate some complex initialization code.
  54. Conceptually, it is all a single expression. However, we may want
  55. to include conditionals, loops, and other such statement-level
  56. constructs. Therefore, we build the initialization code inside a
  57. statement-expression. This function starts such an expression.
  58. STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
  59. pass them back to finish_init_stmts when the expression is
  60. complete. */
  61. static bool
  62. begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
  63. {
  64. bool is_global = !building_stmt_list_p ();
  65. *stmt_expr_p = begin_stmt_expr ();
  66. *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
  67. return is_global;
  68. }
  69. /* Finish out the statement-expression begun by the previous call to
  70. begin_init_stmts. Returns the statement-expression itself. */
  71. static tree
  72. finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
  73. {
  74. finish_compound_stmt (compound_stmt);
  75. stmt_expr = finish_stmt_expr (stmt_expr, true);
  76. gcc_assert (!building_stmt_list_p () == is_global);
  77. return stmt_expr;
  78. }
  79. /* Constructors */
  80. /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
  81. which we want to initialize the vtable pointer for, DATA is
  82. TREE_LIST whose TREE_VALUE is the this ptr expression. */
  83. static tree
  84. dfs_initialize_vtbl_ptrs (tree binfo, void *data)
  85. {
  86. if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
  87. return dfs_skip_bases;
  88. if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
  89. {
  90. tree base_ptr = TREE_VALUE ((tree) data);
  91. base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1,
  92. tf_warning_or_error);
  93. expand_virtual_init (binfo, base_ptr);
  94. }
  95. return NULL_TREE;
  96. }
  97. /* Initialize all the vtable pointers in the object pointed to by
  98. ADDR. */
  99. void
  100. initialize_vtbl_ptrs (tree addr)
  101. {
  102. tree list;
  103. tree type;
  104. type = TREE_TYPE (TREE_TYPE (addr));
  105. list = build_tree_list (type, addr);
  106. /* Walk through the hierarchy, initializing the vptr in each base
  107. class. We do these in pre-order because we can't find the virtual
  108. bases for a class until we've initialized the vtbl for that
  109. class. */
  110. dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
  111. }
  112. /* Return an expression for the zero-initialization of an object with
  113. type T. This expression will either be a constant (in the case
  114. that T is a scalar), or a CONSTRUCTOR (in the case that T is an
  115. aggregate), or NULL (in the case that T does not require
  116. initialization). In either case, the value can be used as
  117. DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
  118. initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
  119. is the number of elements in the array. If STATIC_STORAGE_P is
  120. TRUE, initializers are only generated for entities for which
  121. zero-initialization does not simply mean filling the storage with
  122. zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
  123. subfields with bit positions at or above that bit size shouldn't
  124. be added. Note that this only works when the result is assigned
  125. to a base COMPONENT_REF; if we only have a pointer to the base subobject,
  126. expand_assignment will end up clearing the full size of TYPE. */
  127. static tree
  128. build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
  129. tree field_size)
  130. {
  131. tree init = NULL_TREE;
  132. /* [dcl.init]
  133. To zero-initialize an object of type T means:
  134. -- if T is a scalar type, the storage is set to the value of zero
  135. converted to T.
  136. -- if T is a non-union class type, the storage for each nonstatic
  137. data member and each base-class subobject is zero-initialized.
  138. -- if T is a union type, the storage for its first data member is
  139. zero-initialized.
  140. -- if T is an array type, the storage for each element is
  141. zero-initialized.
  142. -- if T is a reference type, no initialization is performed. */
  143. gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
  144. if (type == error_mark_node)
  145. ;
  146. else if (static_storage_p && zero_init_p (type))
  147. /* In order to save space, we do not explicitly build initializers
  148. for items that do not need them. GCC's semantics are that
  149. items with static storage duration that are not otherwise
  150. initialized are initialized to zero. */
  151. ;
  152. else if (TYPE_PTR_OR_PTRMEM_P (type))
  153. init = convert (type, nullptr_node);
  154. else if (SCALAR_TYPE_P (type))
  155. init = convert (type, integer_zero_node);
  156. else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
  157. {
  158. tree field;
  159. vec<constructor_elt, va_gc> *v = NULL;
  160. /* Iterate over the fields, building initializations. */
  161. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  162. {
  163. if (TREE_CODE (field) != FIELD_DECL)
  164. continue;
  165. if (TREE_TYPE (field) == error_mark_node)
  166. continue;
  167. /* Don't add virtual bases for base classes if they are beyond
  168. the size of the current field, that means it is present
  169. somewhere else in the object. */
  170. if (field_size)
  171. {
  172. tree bitpos = bit_position (field);
  173. if (TREE_CODE (bitpos) == INTEGER_CST
  174. && !tree_int_cst_lt (bitpos, field_size))
  175. continue;
  176. }
  177. /* Note that for class types there will be FIELD_DECLs
  178. corresponding to base classes as well. Thus, iterating
  179. over TYPE_FIELDs will result in correct initialization of
  180. all of the subobjects. */
  181. if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
  182. {
  183. tree new_field_size
  184. = (DECL_FIELD_IS_BASE (field)
  185. && DECL_SIZE (field)
  186. && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
  187. ? DECL_SIZE (field) : NULL_TREE;
  188. tree value = build_zero_init_1 (TREE_TYPE (field),
  189. /*nelts=*/NULL_TREE,
  190. static_storage_p,
  191. new_field_size);
  192. if (value)
  193. CONSTRUCTOR_APPEND_ELT(v, field, value);
  194. }
  195. /* For unions, only the first field is initialized. */
  196. if (TREE_CODE (type) == UNION_TYPE)
  197. break;
  198. }
  199. /* Build a constructor to contain the initializations. */
  200. init = build_constructor (type, v);
  201. }
  202. else if (TREE_CODE (type) == ARRAY_TYPE)
  203. {
  204. tree max_index;
  205. vec<constructor_elt, va_gc> *v = NULL;
  206. /* Iterate over the array elements, building initializations. */
  207. if (nelts)
  208. max_index = fold_build2_loc (input_location,
  209. MINUS_EXPR, TREE_TYPE (nelts),
  210. nelts, integer_one_node);
  211. else
  212. max_index = array_type_nelts (type);
  213. /* If we have an error_mark here, we should just return error mark
  214. as we don't know the size of the array yet. */
  215. if (max_index == error_mark_node)
  216. return error_mark_node;
  217. gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
  218. /* A zero-sized array, which is accepted as an extension, will
  219. have an upper bound of -1. */
  220. if (!tree_int_cst_equal (max_index, integer_minus_one_node))
  221. {
  222. constructor_elt ce;
  223. /* If this is a one element array, we just use a regular init. */
  224. if (tree_int_cst_equal (size_zero_node, max_index))
  225. ce.index = size_zero_node;
  226. else
  227. ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
  228. max_index);
  229. ce.value = build_zero_init_1 (TREE_TYPE (type),
  230. /*nelts=*/NULL_TREE,
  231. static_storage_p, NULL_TREE);
  232. if (ce.value)
  233. {
  234. vec_alloc (v, 1);
  235. v->quick_push (ce);
  236. }
  237. }
  238. /* Build a constructor to contain the initializations. */
  239. init = build_constructor (type, v);
  240. }
  241. else if (TREE_CODE (type) == VECTOR_TYPE)
  242. init = build_zero_cst (type);
  243. else
  244. gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
  245. /* In all cases, the initializer is a constant. */
  246. if (init)
  247. TREE_CONSTANT (init) = 1;
  248. return init;
  249. }
  250. /* Return an expression for the zero-initialization of an object with
  251. type T. This expression will either be a constant (in the case
  252. that T is a scalar), or a CONSTRUCTOR (in the case that T is an
  253. aggregate), or NULL (in the case that T does not require
  254. initialization). In either case, the value can be used as
  255. DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
  256. initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
  257. is the number of elements in the array. If STATIC_STORAGE_P is
  258. TRUE, initializers are only generated for entities for which
  259. zero-initialization does not simply mean filling the storage with
  260. zero bytes. */
  261. tree
  262. build_zero_init (tree type, tree nelts, bool static_storage_p)
  263. {
  264. return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
  265. }
  266. /* Return a suitable initializer for value-initializing an object of type
  267. TYPE, as described in [dcl.init]. */
  268. tree
  269. build_value_init (tree type, tsubst_flags_t complain)
  270. {
  271. /* [dcl.init]
  272. To value-initialize an object of type T means:
  273. - if T is a class type (clause 9) with either no default constructor
  274. (12.1) or a default constructor that is user-provided or deleted,
  275. then then the object is default-initialized;
  276. - if T is a (possibly cv-qualified) class type without a user-provided
  277. or deleted default constructor, then the object is zero-initialized
  278. and the semantic constraints for default-initialization are checked,
  279. and if T has a non-trivial default constructor, the object is
  280. default-initialized;
  281. - if T is an array type, then each element is value-initialized;
  282. - otherwise, the object is zero-initialized.
  283. A program that calls for default-initialization or
  284. value-initialization of an entity of reference type is ill-formed. */
  285. /* The AGGR_INIT_EXPR tweaking below breaks in templates. */
  286. gcc_assert (!processing_template_decl
  287. || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
  288. if (CLASS_TYPE_P (type)
  289. && type_build_ctor_call (type))
  290. {
  291. tree ctor =
  292. build_special_member_call (NULL_TREE, complete_ctor_identifier,
  293. NULL, type, LOOKUP_NORMAL,
  294. complain);
  295. if (ctor == error_mark_node)
  296. return ctor;
  297. tree fn = NULL_TREE;
  298. if (TREE_CODE (ctor) == CALL_EXPR)
  299. fn = get_callee_fndecl (ctor);
  300. ctor = build_aggr_init_expr (type, ctor);
  301. if (fn && user_provided_p (fn))
  302. return ctor;
  303. else if (TYPE_HAS_COMPLEX_DFLT (type))
  304. {
  305. /* This is a class that needs constructing, but doesn't have
  306. a user-provided constructor. So we need to zero-initialize
  307. the object and then call the implicitly defined ctor.
  308. This will be handled in simplify_aggr_init_expr. */
  309. AGGR_INIT_ZERO_FIRST (ctor) = 1;
  310. return ctor;
  311. }
  312. }
  313. /* Discard any access checking during subobject initialization;
  314. the checks are implied by the call to the ctor which we have
  315. verified is OK (cpp0x/defaulted46.C). */
  316. push_deferring_access_checks (dk_deferred);
  317. tree r = build_value_init_noctor (type, complain);
  318. pop_deferring_access_checks ();
  319. return r;
  320. }
  321. /* Like build_value_init, but don't call the constructor for TYPE. Used
  322. for base initializers. */
  323. tree
  324. build_value_init_noctor (tree type, tsubst_flags_t complain)
  325. {
  326. if (!COMPLETE_TYPE_P (type))
  327. {
  328. if (complain & tf_error)
  329. error ("value-initialization of incomplete type %qT", type);
  330. return error_mark_node;
  331. }
  332. /* FIXME the class and array cases should just use digest_init once it is
  333. SFINAE-enabled. */
  334. if (CLASS_TYPE_P (type))
  335. {
  336. gcc_assert (!TYPE_HAS_COMPLEX_DFLT (type)
  337. || errorcount != 0);
  338. if (TREE_CODE (type) != UNION_TYPE)
  339. {
  340. tree field;
  341. vec<constructor_elt, va_gc> *v = NULL;
  342. /* Iterate over the fields, building initializations. */
  343. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  344. {
  345. tree ftype, value;
  346. if (TREE_CODE (field) != FIELD_DECL)
  347. continue;
  348. ftype = TREE_TYPE (field);
  349. if (ftype == error_mark_node)
  350. continue;
  351. /* We could skip vfields and fields of types with
  352. user-defined constructors, but I think that won't improve
  353. performance at all; it should be simpler in general just
  354. to zero out the entire object than try to only zero the
  355. bits that actually need it. */
  356. /* Note that for class types there will be FIELD_DECLs
  357. corresponding to base classes as well. Thus, iterating
  358. over TYPE_FIELDs will result in correct initialization of
  359. all of the subobjects. */
  360. value = build_value_init (ftype, complain);
  361. value = maybe_constant_init (value);
  362. if (value == error_mark_node)
  363. return error_mark_node;
  364. CONSTRUCTOR_APPEND_ELT(v, field, value);
  365. /* We shouldn't have gotten here for anything that would need
  366. non-trivial initialization, and gimplify_init_ctor_preeval
  367. would need to be fixed to allow it. */
  368. gcc_assert (TREE_CODE (value) != TARGET_EXPR
  369. && TREE_CODE (value) != AGGR_INIT_EXPR);
  370. }
  371. /* Build a constructor to contain the zero- initializations. */
  372. return build_constructor (type, v);
  373. }
  374. }
  375. else if (TREE_CODE (type) == ARRAY_TYPE)
  376. {
  377. vec<constructor_elt, va_gc> *v = NULL;
  378. /* Iterate over the array elements, building initializations. */
  379. tree max_index = array_type_nelts (type);
  380. /* If we have an error_mark here, we should just return error mark
  381. as we don't know the size of the array yet. */
  382. if (max_index == error_mark_node)
  383. {
  384. if (complain & tf_error)
  385. error ("cannot value-initialize array of unknown bound %qT",
  386. type);
  387. return error_mark_node;
  388. }
  389. gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
  390. /* A zero-sized array, which is accepted as an extension, will
  391. have an upper bound of -1. */
  392. if (!tree_int_cst_equal (max_index, integer_minus_one_node))
  393. {
  394. constructor_elt ce;
  395. /* If this is a one element array, we just use a regular init. */
  396. if (tree_int_cst_equal (size_zero_node, max_index))
  397. ce.index = size_zero_node;
  398. else
  399. ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, max_index);
  400. ce.value = build_value_init (TREE_TYPE (type), complain);
  401. ce.value = maybe_constant_init (ce.value);
  402. if (ce.value == error_mark_node)
  403. return error_mark_node;
  404. vec_alloc (v, 1);
  405. v->quick_push (ce);
  406. /* We shouldn't have gotten here for anything that would need
  407. non-trivial initialization, and gimplify_init_ctor_preeval
  408. would need to be fixed to allow it. */
  409. gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
  410. && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
  411. }
  412. /* Build a constructor to contain the initializations. */
  413. return build_constructor (type, v);
  414. }
  415. else if (TREE_CODE (type) == FUNCTION_TYPE)
  416. {
  417. if (complain & tf_error)
  418. error ("value-initialization of function type %qT", type);
  419. return error_mark_node;
  420. }
  421. else if (TREE_CODE (type) == REFERENCE_TYPE)
  422. {
  423. if (complain & tf_error)
  424. error ("value-initialization of reference type %qT", type);
  425. return error_mark_node;
  426. }
  427. return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
  428. }
  429. /* Initialize current class with INIT, a TREE_LIST of
  430. arguments for a target constructor. If TREE_LIST is void_type_node,
  431. an empty initializer list was given. */
  432. static void
  433. perform_target_ctor (tree init)
  434. {
  435. tree decl = current_class_ref;
  436. tree type = current_class_type;
  437. finish_expr_stmt (build_aggr_init (decl, init,
  438. LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS,
  439. tf_warning_or_error));
  440. if (type_build_dtor_call (type))
  441. {
  442. tree expr = build_delete (type, decl, sfk_complete_destructor,
  443. LOOKUP_NORMAL
  444. |LOOKUP_NONVIRTUAL
  445. |LOOKUP_DESTRUCTOR,
  446. 0, tf_warning_or_error);
  447. if (expr != error_mark_node
  448. && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  449. finish_eh_cleanup (expr);
  450. }
  451. }
  452. /* Return the non-static data initializer for FIELD_DECL MEMBER. */
  453. tree
  454. get_nsdmi (tree member, bool in_ctor)
  455. {
  456. tree init;
  457. tree save_ccp = current_class_ptr;
  458. tree save_ccr = current_class_ref;
  459. if (!in_ctor)
  460. {
  461. /* Use a PLACEHOLDER_EXPR when we don't have a 'this' parameter to
  462. refer to; constexpr evaluation knows what to do with it. */
  463. current_class_ref = build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (member));
  464. current_class_ptr = build_address (current_class_ref);
  465. }
  466. if (DECL_LANG_SPECIFIC (member) && DECL_TEMPLATE_INFO (member))
  467. {
  468. /* Do deferred instantiation of the NSDMI. */
  469. init = (tsubst_copy_and_build
  470. (DECL_INITIAL (DECL_TI_TEMPLATE (member)),
  471. DECL_TI_ARGS (member),
  472. tf_warning_or_error, member, /*function_p=*/false,
  473. /*integral_constant_expression_p=*/false));
  474. init = digest_nsdmi_init (member, init);
  475. }
  476. else
  477. {
  478. init = DECL_INITIAL (member);
  479. if (init && TREE_CODE (init) == DEFAULT_ARG)
  480. {
  481. error ("constructor required before non-static data member "
  482. "for %qD has been parsed", member);
  483. DECL_INITIAL (member) = error_mark_node;
  484. init = error_mark_node;
  485. }
  486. /* Strip redundant TARGET_EXPR so we don't need to remap it, and
  487. so the aggregate init code below will see a CONSTRUCTOR. */
  488. if (init && TREE_CODE (init) == TARGET_EXPR
  489. && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
  490. init = TARGET_EXPR_INITIAL (init);
  491. init = break_out_target_exprs (init);
  492. }
  493. current_class_ptr = save_ccp;
  494. current_class_ref = save_ccr;
  495. return init;
  496. }
  497. /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
  498. arguments. If TREE_LIST is void_type_node, an empty initializer
  499. list was given; if NULL_TREE no initializer was given. */
  500. static void
  501. perform_member_init (tree member, tree init)
  502. {
  503. tree decl;
  504. tree type = TREE_TYPE (member);
  505. /* Use the non-static data member initializer if there was no
  506. mem-initializer for this field. */
  507. if (init == NULL_TREE)
  508. init = get_nsdmi (member, /*ctor*/true);
  509. if (init == error_mark_node)
  510. return;
  511. /* Effective C++ rule 12 requires that all data members be
  512. initialized. */
  513. if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
  514. warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
  515. "%qD should be initialized in the member initialization list",
  516. member);
  517. /* Get an lvalue for the data member. */
  518. decl = build_class_member_access_expr (current_class_ref, member,
  519. /*access_path=*/NULL_TREE,
  520. /*preserve_reference=*/true,
  521. tf_warning_or_error);
  522. if (decl == error_mark_node)
  523. return;
  524. if (warn_init_self && init && TREE_CODE (init) == TREE_LIST
  525. && TREE_CHAIN (init) == NULL_TREE)
  526. {
  527. tree val = TREE_VALUE (init);
  528. if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member
  529. && TREE_OPERAND (val, 0) == current_class_ref)
  530. warning_at (DECL_SOURCE_LOCATION (current_function_decl),
  531. OPT_Winit_self, "%qD is initialized with itself",
  532. member);
  533. }
  534. if (init == void_type_node)
  535. {
  536. /* mem() means value-initialization. */
  537. if (TREE_CODE (type) == ARRAY_TYPE)
  538. {
  539. init = build_vec_init_expr (type, init, tf_warning_or_error);
  540. init = build2 (INIT_EXPR, type, decl, init);
  541. finish_expr_stmt (init);
  542. }
  543. else
  544. {
  545. tree value = build_value_init (type, tf_warning_or_error);
  546. if (value == error_mark_node)
  547. return;
  548. init = build2 (INIT_EXPR, type, decl, value);
  549. finish_expr_stmt (init);
  550. }
  551. }
  552. /* Deal with this here, as we will get confused if we try to call the
  553. assignment op for an anonymous union. This can happen in a
  554. synthesized copy constructor. */
  555. else if (ANON_AGGR_TYPE_P (type))
  556. {
  557. if (init)
  558. {
  559. init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
  560. finish_expr_stmt (init);
  561. }
  562. }
  563. else if (init
  564. && (TREE_CODE (type) == REFERENCE_TYPE
  565. /* Pre-digested NSDMI. */
  566. || (((TREE_CODE (init) == CONSTRUCTOR
  567. && TREE_TYPE (init) == type)
  568. /* { } mem-initializer. */
  569. || (TREE_CODE (init) == TREE_LIST
  570. && DIRECT_LIST_INIT_P (TREE_VALUE (init))))
  571. && (CP_AGGREGATE_TYPE_P (type)
  572. || is_std_init_list (type)))))
  573. {
  574. /* With references and list-initialization, we need to deal with
  575. extending temporary lifetimes. 12.2p5: "A temporary bound to a
  576. reference member in a constructor’s ctor-initializer (12.6.2)
  577. persists until the constructor exits." */
  578. unsigned i; tree t;
  579. vec<tree, va_gc> *cleanups = make_tree_vector ();
  580. if (TREE_CODE (init) == TREE_LIST)
  581. init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
  582. tf_warning_or_error);
  583. if (TREE_TYPE (init) != type)
  584. {
  585. if (BRACE_ENCLOSED_INITIALIZER_P (init)
  586. && CP_AGGREGATE_TYPE_P (type))
  587. init = reshape_init (type, init, tf_warning_or_error);
  588. init = digest_init (type, init, tf_warning_or_error);
  589. }
  590. if (init == error_mark_node)
  591. return;
  592. /* A FIELD_DECL doesn't really have a suitable lifetime, but
  593. make_temporary_var_for_ref_to_temp will treat it as automatic and
  594. set_up_extended_ref_temp wants to use the decl in a warning. */
  595. init = extend_ref_init_temps (member, init, &cleanups);
  596. if (TREE_CODE (type) == ARRAY_TYPE
  597. && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type)))
  598. init = build_vec_init_expr (type, init, tf_warning_or_error);
  599. init = build2 (INIT_EXPR, type, decl, init);
  600. finish_expr_stmt (init);
  601. FOR_EACH_VEC_ELT (*cleanups, i, t)
  602. push_cleanup (decl, t, false);
  603. release_tree_vector (cleanups);
  604. }
  605. else if (type_build_ctor_call (type)
  606. || (init && CLASS_TYPE_P (strip_array_types (type))))
  607. {
  608. if (TREE_CODE (type) == ARRAY_TYPE)
  609. {
  610. if (init)
  611. {
  612. if (TREE_CHAIN (init))
  613. init = error_mark_node;
  614. else
  615. init = TREE_VALUE (init);
  616. if (BRACE_ENCLOSED_INITIALIZER_P (init))
  617. init = digest_init (type, init, tf_warning_or_error);
  618. }
  619. if (init == NULL_TREE
  620. || same_type_ignoring_top_level_qualifiers_p (type,
  621. TREE_TYPE (init)))
  622. {
  623. init = build_vec_init_expr (type, init, tf_warning_or_error);
  624. init = build2 (INIT_EXPR, type, decl, init);
  625. finish_expr_stmt (init);
  626. }
  627. else
  628. error ("invalid initializer for array member %q#D", member);
  629. }
  630. else
  631. {
  632. int flags = LOOKUP_NORMAL;
  633. if (DECL_DEFAULTED_FN (current_function_decl))
  634. flags |= LOOKUP_DEFAULTED;
  635. if (CP_TYPE_CONST_P (type)
  636. && init == NULL_TREE
  637. && default_init_uninitialized_part (type))
  638. {
  639. /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
  640. vtable; still give this diagnostic. */
  641. if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
  642. "uninitialized const member in %q#T", type))
  643. inform (DECL_SOURCE_LOCATION (member),
  644. "%q#D should be initialized", member );
  645. }
  646. finish_expr_stmt (build_aggr_init (decl, init, flags,
  647. tf_warning_or_error));
  648. }
  649. }
  650. else
  651. {
  652. if (init == NULL_TREE)
  653. {
  654. tree core_type;
  655. /* member traversal: note it leaves init NULL */
  656. if (TREE_CODE (type) == REFERENCE_TYPE)
  657. {
  658. if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
  659. "uninitialized reference member in %q#T", type))
  660. inform (DECL_SOURCE_LOCATION (member),
  661. "%q#D should be initialized", member);
  662. }
  663. else if (CP_TYPE_CONST_P (type))
  664. {
  665. if (permerror (DECL_SOURCE_LOCATION (current_function_decl),
  666. "uninitialized const member in %q#T", type))
  667. inform (DECL_SOURCE_LOCATION (member),
  668. "%q#D should be initialized", member );
  669. }
  670. core_type = strip_array_types (type);
  671. if (CLASS_TYPE_P (core_type)
  672. && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
  673. || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
  674. diagnose_uninitialized_cst_or_ref_member (core_type,
  675. /*using_new=*/false,
  676. /*complain=*/true);
  677. }
  678. else if (TREE_CODE (init) == TREE_LIST)
  679. /* There was an explicit member initialization. Do some work
  680. in that case. */
  681. init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
  682. tf_warning_or_error);
  683. if (init)
  684. finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
  685. tf_warning_or_error));
  686. }
  687. if (type_build_dtor_call (type))
  688. {
  689. tree expr;
  690. expr = build_class_member_access_expr (current_class_ref, member,
  691. /*access_path=*/NULL_TREE,
  692. /*preserve_reference=*/false,
  693. tf_warning_or_error);
  694. expr = build_delete (type, expr, sfk_complete_destructor,
  695. LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0,
  696. tf_warning_or_error);
  697. if (expr != error_mark_node
  698. && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  699. finish_eh_cleanup (expr);
  700. }
  701. }
  702. /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
  703. the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
  704. static tree
  705. build_field_list (tree t, tree list, int *uses_unions_p)
  706. {
  707. tree fields;
  708. /* Note whether or not T is a union. */
  709. if (TREE_CODE (t) == UNION_TYPE)
  710. *uses_unions_p = 1;
  711. for (fields = TYPE_FIELDS (t); fields; fields = DECL_CHAIN (fields))
  712. {
  713. tree fieldtype;
  714. /* Skip CONST_DECLs for enumeration constants and so forth. */
  715. if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
  716. continue;
  717. fieldtype = TREE_TYPE (fields);
  718. /* Keep track of whether or not any fields are unions. */
  719. if (TREE_CODE (fieldtype) == UNION_TYPE)
  720. *uses_unions_p = 1;
  721. /* For an anonymous struct or union, we must recursively
  722. consider the fields of the anonymous type. They can be
  723. directly initialized from the constructor. */
  724. if (ANON_AGGR_TYPE_P (fieldtype))
  725. {
  726. /* Add this field itself. Synthesized copy constructors
  727. initialize the entire aggregate. */
  728. list = tree_cons (fields, NULL_TREE, list);
  729. /* And now add the fields in the anonymous aggregate. */
  730. list = build_field_list (fieldtype, list, uses_unions_p);
  731. }
  732. /* Add this field. */
  733. else if (DECL_NAME (fields))
  734. list = tree_cons (fields, NULL_TREE, list);
  735. }
  736. return list;
  737. }
  738. /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
  739. a FIELD_DECL or BINFO in T that needs initialization. The
  740. TREE_VALUE gives the initializer, or list of initializer arguments.
  741. Return a TREE_LIST containing all of the initializations required
  742. for T, in the order in which they should be performed. The output
  743. list has the same format as the input. */
  744. static tree
  745. sort_mem_initializers (tree t, tree mem_inits)
  746. {
  747. tree init;
  748. tree base, binfo, base_binfo;
  749. tree sorted_inits;
  750. tree next_subobject;
  751. vec<tree, va_gc> *vbases;
  752. int i;
  753. int uses_unions_p = 0;
  754. /* Build up a list of initializations. The TREE_PURPOSE of entry
  755. will be the subobject (a FIELD_DECL or BINFO) to initialize. The
  756. TREE_VALUE will be the constructor arguments, or NULL if no
  757. explicit initialization was provided. */
  758. sorted_inits = NULL_TREE;
  759. /* Process the virtual bases. */
  760. for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
  761. vec_safe_iterate (vbases, i, &base); i++)
  762. sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
  763. /* Process the direct bases. */
  764. for (binfo = TYPE_BINFO (t), i = 0;
  765. BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
  766. if (!BINFO_VIRTUAL_P (base_binfo))
  767. sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
  768. /* Process the non-static data members. */
  769. sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
  770. /* Reverse the entire list of initializations, so that they are in
  771. the order that they will actually be performed. */
  772. sorted_inits = nreverse (sorted_inits);
  773. /* If the user presented the initializers in an order different from
  774. that in which they will actually occur, we issue a warning. Keep
  775. track of the next subobject which can be explicitly initialized
  776. without issuing a warning. */
  777. next_subobject = sorted_inits;
  778. /* Go through the explicit initializers, filling in TREE_PURPOSE in
  779. the SORTED_INITS. */
  780. for (init = mem_inits; init; init = TREE_CHAIN (init))
  781. {
  782. tree subobject;
  783. tree subobject_init;
  784. subobject = TREE_PURPOSE (init);
  785. /* If the explicit initializers are in sorted order, then
  786. SUBOBJECT will be NEXT_SUBOBJECT, or something following
  787. it. */
  788. for (subobject_init = next_subobject;
  789. subobject_init;
  790. subobject_init = TREE_CHAIN (subobject_init))
  791. if (TREE_PURPOSE (subobject_init) == subobject)
  792. break;
  793. /* Issue a warning if the explicit initializer order does not
  794. match that which will actually occur.
  795. ??? Are all these on the correct lines? */
  796. if (warn_reorder && !subobject_init)
  797. {
  798. if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
  799. warning (OPT_Wreorder, "%q+D will be initialized after",
  800. TREE_PURPOSE (next_subobject));
  801. else
  802. warning (OPT_Wreorder, "base %qT will be initialized after",
  803. TREE_PURPOSE (next_subobject));
  804. if (TREE_CODE (subobject) == FIELD_DECL)
  805. warning (OPT_Wreorder, " %q+#D", subobject);
  806. else
  807. warning (OPT_Wreorder, " base %qT", subobject);
  808. warning_at (DECL_SOURCE_LOCATION (current_function_decl),
  809. OPT_Wreorder, " when initialized here");
  810. }
  811. /* Look again, from the beginning of the list. */
  812. if (!subobject_init)
  813. {
  814. subobject_init = sorted_inits;
  815. while (TREE_PURPOSE (subobject_init) != subobject)
  816. subobject_init = TREE_CHAIN (subobject_init);
  817. }
  818. /* It is invalid to initialize the same subobject more than
  819. once. */
  820. if (TREE_VALUE (subobject_init))
  821. {
  822. if (TREE_CODE (subobject) == FIELD_DECL)
  823. error_at (DECL_SOURCE_LOCATION (current_function_decl),
  824. "multiple initializations given for %qD",
  825. subobject);
  826. else
  827. error_at (DECL_SOURCE_LOCATION (current_function_decl),
  828. "multiple initializations given for base %qT",
  829. subobject);
  830. }
  831. /* Record the initialization. */
  832. TREE_VALUE (subobject_init) = TREE_VALUE (init);
  833. next_subobject = subobject_init;
  834. }
  835. /* [class.base.init]
  836. If a ctor-initializer specifies more than one mem-initializer for
  837. multiple members of the same union (including members of
  838. anonymous unions), the ctor-initializer is ill-formed.
  839. Here we also splice out uninitialized union members. */
  840. if (uses_unions_p)
  841. {
  842. tree *last_p = NULL;
  843. tree *p;
  844. for (p = &sorted_inits; *p; )
  845. {
  846. tree field;
  847. tree ctx;
  848. init = *p;
  849. field = TREE_PURPOSE (init);
  850. /* Skip base classes. */
  851. if (TREE_CODE (field) != FIELD_DECL)
  852. goto next;
  853. /* If this is an anonymous union with no explicit initializer,
  854. splice it out. */
  855. if (!TREE_VALUE (init) && ANON_UNION_TYPE_P (TREE_TYPE (field)))
  856. goto splice;
  857. /* See if this field is a member of a union, or a member of a
  858. structure contained in a union, etc. */
  859. for (ctx = DECL_CONTEXT (field);
  860. !same_type_p (ctx, t);
  861. ctx = TYPE_CONTEXT (ctx))
  862. if (TREE_CODE (ctx) == UNION_TYPE
  863. || !ANON_AGGR_TYPE_P (ctx))
  864. break;
  865. /* If this field is not a member of a union, skip it. */
  866. if (TREE_CODE (ctx) != UNION_TYPE)
  867. goto next;
  868. /* If this union member has no explicit initializer and no NSDMI,
  869. splice it out. */
  870. if (TREE_VALUE (init) || DECL_INITIAL (field))
  871. /* OK. */;
  872. else
  873. goto splice;
  874. /* It's only an error if we have two initializers for the same
  875. union type. */
  876. if (!last_p)
  877. {
  878. last_p = p;
  879. goto next;
  880. }
  881. /* See if LAST_FIELD and the field initialized by INIT are
  882. members of the same union. If so, there's a problem,
  883. unless they're actually members of the same structure
  884. which is itself a member of a union. For example, given:
  885. union { struct { int i; int j; }; };
  886. initializing both `i' and `j' makes sense. */
  887. ctx = common_enclosing_class (DECL_CONTEXT (field),
  888. DECL_CONTEXT (TREE_PURPOSE (*last_p)));
  889. if (ctx && TREE_CODE (ctx) == UNION_TYPE)
  890. {
  891. /* A mem-initializer hides an NSDMI. */
  892. if (TREE_VALUE (init) && !TREE_VALUE (*last_p))
  893. *last_p = TREE_CHAIN (*last_p);
  894. else if (TREE_VALUE (*last_p) && !TREE_VALUE (init))
  895. goto splice;
  896. else
  897. {
  898. error_at (DECL_SOURCE_LOCATION (current_function_decl),
  899. "initializations for multiple members of %qT",
  900. ctx);
  901. goto splice;
  902. }
  903. }
  904. last_p = p;
  905. next:
  906. p = &TREE_CHAIN (*p);
  907. continue;
  908. splice:
  909. *p = TREE_CHAIN (*p);
  910. continue;
  911. }
  912. }
  913. return sorted_inits;
  914. }
  915. /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
  916. is a TREE_LIST giving the explicit mem-initializer-list for the
  917. constructor. The TREE_PURPOSE of each entry is a subobject (a
  918. FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
  919. is a TREE_LIST giving the arguments to the constructor or
  920. void_type_node for an empty list of arguments. */
  921. void
  922. emit_mem_initializers (tree mem_inits)
  923. {
  924. int flags = LOOKUP_NORMAL;
  925. /* We will already have issued an error message about the fact that
  926. the type is incomplete. */
  927. if (!COMPLETE_TYPE_P (current_class_type))
  928. return;
  929. if (mem_inits
  930. && TYPE_P (TREE_PURPOSE (mem_inits))
  931. && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
  932. {
  933. /* Delegating constructor. */
  934. gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
  935. perform_target_ctor (TREE_VALUE (mem_inits));
  936. return;
  937. }
  938. if (DECL_DEFAULTED_FN (current_function_decl)
  939. && ! DECL_INHERITED_CTOR_BASE (current_function_decl))
  940. flags |= LOOKUP_DEFAULTED;
  941. /* Sort the mem-initializers into the order in which the
  942. initializations should be performed. */
  943. mem_inits = sort_mem_initializers (current_class_type, mem_inits);
  944. in_base_initializer = 1;
  945. /* Initialize base classes. */
  946. for (; (mem_inits
  947. && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL);
  948. mem_inits = TREE_CHAIN (mem_inits))
  949. {
  950. tree subobject = TREE_PURPOSE (mem_inits);
  951. tree arguments = TREE_VALUE (mem_inits);
  952. /* We already have issued an error message. */
  953. if (arguments == error_mark_node)
  954. continue;
  955. if (arguments == NULL_TREE)
  956. {
  957. /* If these initializations are taking place in a copy constructor,
  958. the base class should probably be explicitly initialized if there
  959. is a user-defined constructor in the base class (other than the
  960. default constructor, which will be called anyway). */
  961. if (extra_warnings
  962. && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
  963. && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
  964. warning_at (DECL_SOURCE_LOCATION (current_function_decl),
  965. OPT_Wextra, "base class %q#T should be explicitly "
  966. "initialized in the copy constructor",
  967. BINFO_TYPE (subobject));
  968. }
  969. /* Initialize the base. */
  970. if (BINFO_VIRTUAL_P (subobject))
  971. construct_virtual_base (subobject, arguments);
  972. else
  973. {
  974. tree base_addr;
  975. base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
  976. subobject, 1, tf_warning_or_error);
  977. expand_aggr_init_1 (subobject, NULL_TREE,
  978. cp_build_indirect_ref (base_addr, RO_NULL,
  979. tf_warning_or_error),
  980. arguments,
  981. flags,
  982. tf_warning_or_error);
  983. expand_cleanup_for_base (subobject, NULL_TREE);
  984. }
  985. }
  986. in_base_initializer = 0;
  987. /* Initialize the vptrs. */
  988. initialize_vtbl_ptrs (current_class_ptr);
  989. /* Initialize the data members. */
  990. while (mem_inits)
  991. {
  992. perform_member_init (TREE_PURPOSE (mem_inits),
  993. TREE_VALUE (mem_inits));
  994. mem_inits = TREE_CHAIN (mem_inits);
  995. }
  996. }
  997. /* Returns the address of the vtable (i.e., the value that should be
  998. assigned to the vptr) for BINFO. */
  999. tree
  1000. build_vtbl_address (tree binfo)
  1001. {
  1002. tree binfo_for = binfo;
  1003. tree vtbl;
  1004. if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
  1005. /* If this is a virtual primary base, then the vtable we want to store
  1006. is that for the base this is being used as the primary base of. We
  1007. can't simply skip the initialization, because we may be expanding the
  1008. inits of a subobject constructor where the virtual base layout
  1009. can be different. */
  1010. while (BINFO_PRIMARY_P (binfo_for))
  1011. binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
  1012. /* Figure out what vtable BINFO's vtable is based on, and mark it as
  1013. used. */
  1014. vtbl = get_vtbl_decl_for_binfo (binfo_for);
  1015. TREE_USED (vtbl) = true;
  1016. /* Now compute the address to use when initializing the vptr. */
  1017. vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
  1018. if (VAR_P (vtbl))
  1019. vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
  1020. return vtbl;
  1021. }
  1022. /* This code sets up the virtual function tables appropriate for
  1023. the pointer DECL. It is a one-ply initialization.
  1024. BINFO is the exact type that DECL is supposed to be. In
  1025. multiple inheritance, this might mean "C's A" if C : A, B. */
  1026. static void
  1027. expand_virtual_init (tree binfo, tree decl)
  1028. {
  1029. tree vtbl, vtbl_ptr;
  1030. tree vtt_index;
  1031. /* Compute the initializer for vptr. */
  1032. vtbl = build_vtbl_address (binfo);
  1033. /* We may get this vptr from a VTT, if this is a subobject
  1034. constructor or subobject destructor. */
  1035. vtt_index = BINFO_VPTR_INDEX (binfo);
  1036. if (vtt_index)
  1037. {
  1038. tree vtbl2;
  1039. tree vtt_parm;
  1040. /* Compute the value to use, when there's a VTT. */
  1041. vtt_parm = current_vtt_parm;
  1042. vtbl2 = fold_build_pointer_plus (vtt_parm, vtt_index);
  1043. vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
  1044. vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
  1045. /* The actual initializer is the VTT value only in the subobject
  1046. constructor. In maybe_clone_body we'll substitute NULL for
  1047. the vtt_parm in the case of the non-subobject constructor. */
  1048. vtbl = build3 (COND_EXPR,
  1049. TREE_TYPE (vtbl),
  1050. build2 (EQ_EXPR, boolean_type_node,
  1051. current_in_charge_parm, integer_zero_node),
  1052. vtbl2,
  1053. vtbl);
  1054. }
  1055. /* Compute the location of the vtpr. */
  1056. vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
  1057. tf_warning_or_error),
  1058. TREE_TYPE (binfo));
  1059. gcc_assert (vtbl_ptr != error_mark_node);
  1060. /* Assign the vtable to the vptr. */
  1061. vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0, tf_warning_or_error);
  1062. finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
  1063. tf_warning_or_error));
  1064. }
  1065. /* If an exception is thrown in a constructor, those base classes already
  1066. constructed must be destroyed. This function creates the cleanup
  1067. for BINFO, which has just been constructed. If FLAG is non-NULL,
  1068. it is a DECL which is nonzero when this base needs to be
  1069. destroyed. */
  1070. static void
  1071. expand_cleanup_for_base (tree binfo, tree flag)
  1072. {
  1073. tree expr;
  1074. if (!type_build_dtor_call (BINFO_TYPE (binfo)))
  1075. return;
  1076. /* Call the destructor. */
  1077. expr = build_special_member_call (current_class_ref,
  1078. base_dtor_identifier,
  1079. NULL,
  1080. binfo,
  1081. LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
  1082. tf_warning_or_error);
  1083. if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
  1084. return;
  1085. if (flag)
  1086. expr = fold_build3_loc (input_location,
  1087. COND_EXPR, void_type_node,
  1088. c_common_truthvalue_conversion (input_location, flag),
  1089. expr, integer_zero_node);
  1090. finish_eh_cleanup (expr);
  1091. }
  1092. /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
  1093. constructor. */
  1094. static void
  1095. construct_virtual_base (tree vbase, tree arguments)
  1096. {
  1097. tree inner_if_stmt;
  1098. tree exp;
  1099. tree flag;
  1100. /* If there are virtual base classes with destructors, we need to
  1101. emit cleanups to destroy them if an exception is thrown during
  1102. the construction process. These exception regions (i.e., the
  1103. period during which the cleanups must occur) begin from the time
  1104. the construction is complete to the end of the function. If we
  1105. create a conditional block in which to initialize the
  1106. base-classes, then the cleanup region for the virtual base begins
  1107. inside a block, and ends outside of that block. This situation
  1108. confuses the sjlj exception-handling code. Therefore, we do not
  1109. create a single conditional block, but one for each
  1110. initialization. (That way the cleanup regions always begin
  1111. in the outer block.) We trust the back end to figure out
  1112. that the FLAG will not change across initializations, and
  1113. avoid doing multiple tests. */
  1114. flag = DECL_CHAIN (DECL_ARGUMENTS (current_function_decl));
  1115. inner_if_stmt = begin_if_stmt ();
  1116. finish_if_stmt_cond (flag, inner_if_stmt);
  1117. /* Compute the location of the virtual base. If we're
  1118. constructing virtual bases, then we must be the most derived
  1119. class. Therefore, we don't have to look up the virtual base;
  1120. we already know where it is. */
  1121. exp = convert_to_base_statically (current_class_ref, vbase);
  1122. expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
  1123. 0, tf_warning_or_error);
  1124. finish_then_clause (inner_if_stmt);
  1125. finish_if_stmt (inner_if_stmt);
  1126. expand_cleanup_for_base (vbase, flag);
  1127. }
  1128. /* Find the context in which this FIELD can be initialized. */
  1129. static tree
  1130. initializing_context (tree field)
  1131. {
  1132. tree t = DECL_CONTEXT (field);
  1133. /* Anonymous union members can be initialized in the first enclosing
  1134. non-anonymous union context. */
  1135. while (t && ANON_AGGR_TYPE_P (t))
  1136. t = TYPE_CONTEXT (t);
  1137. return t;
  1138. }
  1139. /* Function to give error message if member initialization specification
  1140. is erroneous. FIELD is the member we decided to initialize.
  1141. TYPE is the type for which the initialization is being performed.
  1142. FIELD must be a member of TYPE.
  1143. MEMBER_NAME is the name of the member. */
  1144. static int
  1145. member_init_ok_or_else (tree field, tree type, tree member_name)
  1146. {
  1147. if (field == error_mark_node)
  1148. return 0;
  1149. if (!field)
  1150. {
  1151. error ("class %qT does not have any field named %qD", type,
  1152. member_name);
  1153. return 0;
  1154. }
  1155. if (VAR_P (field))
  1156. {
  1157. error ("%q#D is a static data member; it can only be "
  1158. "initialized at its definition",
  1159. field);
  1160. return 0;
  1161. }
  1162. if (TREE_CODE (field) != FIELD_DECL)
  1163. {
  1164. error ("%q#D is not a non-static data member of %qT",
  1165. field, type);
  1166. return 0;
  1167. }
  1168. if (initializing_context (field) != type)
  1169. {
  1170. error ("class %qT does not have any field named %qD", type,
  1171. member_name);
  1172. return 0;
  1173. }
  1174. return 1;
  1175. }
  1176. /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
  1177. is a _TYPE node or TYPE_DECL which names a base for that type.
  1178. Check the validity of NAME, and return either the base _TYPE, base
  1179. binfo, or the FIELD_DECL of the member. If NAME is invalid, return
  1180. NULL_TREE and issue a diagnostic.
  1181. An old style unnamed direct single base construction is permitted,
  1182. where NAME is NULL. */
  1183. tree
  1184. expand_member_init (tree name)
  1185. {
  1186. tree basetype;
  1187. tree field;
  1188. if (!current_class_ref)
  1189. return NULL_TREE;
  1190. if (!name)
  1191. {
  1192. /* This is an obsolete unnamed base class initializer. The
  1193. parser will already have warned about its use. */
  1194. switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
  1195. {
  1196. case 0:
  1197. error ("unnamed initializer for %qT, which has no base classes",
  1198. current_class_type);
  1199. return NULL_TREE;
  1200. case 1:
  1201. basetype = BINFO_TYPE
  1202. (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
  1203. break;
  1204. default:
  1205. error ("unnamed initializer for %qT, which uses multiple inheritance",
  1206. current_class_type);
  1207. return NULL_TREE;
  1208. }
  1209. }
  1210. else if (TYPE_P (name))
  1211. {
  1212. basetype = TYPE_MAIN_VARIANT (name);
  1213. name = TYPE_NAME (name);
  1214. }
  1215. else if (TREE_CODE (name) == TYPE_DECL)
  1216. basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
  1217. else
  1218. basetype = NULL_TREE;
  1219. if (basetype)
  1220. {
  1221. tree class_binfo;
  1222. tree direct_binfo;
  1223. tree virtual_binfo;
  1224. int i;
  1225. if (current_template_parms
  1226. || same_type_p (basetype, current_class_type))
  1227. return basetype;
  1228. class_binfo = TYPE_BINFO (current_class_type);
  1229. direct_binfo = NULL_TREE;
  1230. virtual_binfo = NULL_TREE;
  1231. /* Look for a direct base. */
  1232. for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
  1233. if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
  1234. break;
  1235. /* Look for a virtual base -- unless the direct base is itself
  1236. virtual. */
  1237. if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
  1238. virtual_binfo = binfo_for_vbase (basetype, current_class_type);
  1239. /* [class.base.init]
  1240. If a mem-initializer-id is ambiguous because it designates
  1241. both a direct non-virtual base class and an inherited virtual
  1242. base class, the mem-initializer is ill-formed. */
  1243. if (direct_binfo && virtual_binfo)
  1244. {
  1245. error ("%qD is both a direct base and an indirect virtual base",
  1246. basetype);
  1247. return NULL_TREE;
  1248. }
  1249. if (!direct_binfo && !virtual_binfo)
  1250. {
  1251. if (CLASSTYPE_VBASECLASSES (current_class_type))
  1252. error ("type %qT is not a direct or virtual base of %qT",
  1253. basetype, current_class_type);
  1254. else
  1255. error ("type %qT is not a direct base of %qT",
  1256. basetype, current_class_type);
  1257. return NULL_TREE;
  1258. }
  1259. return direct_binfo ? direct_binfo : virtual_binfo;
  1260. }
  1261. else
  1262. {
  1263. if (identifier_p (name))
  1264. field = lookup_field (current_class_type, name, 1, false);
  1265. else
  1266. field = name;
  1267. if (member_init_ok_or_else (field, current_class_type, name))
  1268. return field;
  1269. }
  1270. return NULL_TREE;
  1271. }
  1272. /* This is like `expand_member_init', only it stores one aggregate
  1273. value into another.
  1274. INIT comes in two flavors: it is either a value which
  1275. is to be stored in EXP, or it is a parameter list
  1276. to go to a constructor, which will operate on EXP.
  1277. If INIT is not a parameter list for a constructor, then set
  1278. LOOKUP_ONLYCONVERTING.
  1279. If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
  1280. the initializer, if FLAGS is 0, then it is the (init) form.
  1281. If `init' is a CONSTRUCTOR, then we emit a warning message,
  1282. explaining that such initializations are invalid.
  1283. If INIT resolves to a CALL_EXPR which happens to return
  1284. something of the type we are looking for, then we know
  1285. that we can safely use that call to perform the
  1286. initialization.
  1287. The virtual function table pointer cannot be set up here, because
  1288. we do not really know its type.
  1289. This never calls operator=().
  1290. When initializing, nothing is CONST.
  1291. A default copy constructor may have to be used to perform the
  1292. initialization.
  1293. A constructor or a conversion operator may have to be used to
  1294. perform the initialization, but not both, as it would be ambiguous. */
  1295. tree
  1296. build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
  1297. {
  1298. tree stmt_expr;
  1299. tree compound_stmt;
  1300. int destroy_temps;
  1301. tree type = TREE_TYPE (exp);
  1302. int was_const = TREE_READONLY (exp);
  1303. int was_volatile = TREE_THIS_VOLATILE (exp);
  1304. int is_global;
  1305. if (init == error_mark_node)
  1306. return error_mark_node;
  1307. TREE_READONLY (exp) = 0;
  1308. TREE_THIS_VOLATILE (exp) = 0;
  1309. if (init && init != void_type_node
  1310. && TREE_CODE (init) != TREE_LIST
  1311. && !(TREE_CODE (init) == TARGET_EXPR
  1312. && TARGET_EXPR_DIRECT_INIT_P (init))
  1313. && !DIRECT_LIST_INIT_P (init))
  1314. flags |= LOOKUP_ONLYCONVERTING;
  1315. if (TREE_CODE (type) == ARRAY_TYPE)
  1316. {
  1317. tree itype;
  1318. /* An array may not be initialized use the parenthesized
  1319. initialization form -- unless the initializer is "()". */
  1320. if (init && TREE_CODE (init) == TREE_LIST)
  1321. {
  1322. if (complain & tf_error)
  1323. error ("bad array initializer");
  1324. return error_mark_node;
  1325. }
  1326. /* Must arrange to initialize each element of EXP
  1327. from elements of INIT. */
  1328. itype = init ? TREE_TYPE (init) : NULL_TREE;
  1329. if (cv_qualified_p (type))
  1330. TREE_TYPE (exp) = cv_unqualified (type);
  1331. if (itype && cv_qualified_p (itype))
  1332. TREE_TYPE (init) = cv_unqualified (itype);
  1333. stmt_expr = build_vec_init (exp, NULL_TREE, init,
  1334. /*explicit_value_init_p=*/false,
  1335. itype && same_type_p (TREE_TYPE (init),
  1336. TREE_TYPE (exp)),
  1337. complain);
  1338. TREE_READONLY (exp) = was_const;
  1339. TREE_THIS_VOLATILE (exp) = was_volatile;
  1340. TREE_TYPE (exp) = type;
  1341. /* Restore the type of init unless it was used directly. */
  1342. if (init && TREE_CODE (stmt_expr) != INIT_EXPR)
  1343. TREE_TYPE (init) = itype;
  1344. return stmt_expr;
  1345. }
  1346. if ((VAR_P (exp) || TREE_CODE (exp) == PARM_DECL)
  1347. && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (type)))
  1348. /* Just know that we've seen something for this node. */
  1349. TREE_USED (exp) = 1;
  1350. is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
  1351. destroy_temps = stmts_are_full_exprs_p ();
  1352. current_stmt_tree ()->stmts_are_full_exprs_p = 0;
  1353. expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
  1354. init, LOOKUP_NORMAL|flags, complain);
  1355. stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
  1356. current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
  1357. TREE_READONLY (exp) = was_const;
  1358. TREE_THIS_VOLATILE (exp) = was_volatile;
  1359. return stmt_expr;
  1360. }
  1361. static void
  1362. expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
  1363. tsubst_flags_t complain)
  1364. {
  1365. tree type = TREE_TYPE (exp);
  1366. tree ctor_name;
  1367. /* It fails because there may not be a constructor which takes
  1368. its own type as the first (or only parameter), but which does
  1369. take other types via a conversion. So, if the thing initializing
  1370. the expression is a unit element of type X, first try X(X&),
  1371. followed by initialization by X. If neither of these work
  1372. out, then look hard. */
  1373. tree rval;
  1374. vec<tree, va_gc> *parms;
  1375. /* If we have direct-initialization from an initializer list, pull
  1376. it out of the TREE_LIST so the code below can see it. */
  1377. if (init && TREE_CODE (init) == TREE_LIST
  1378. && DIRECT_LIST_INIT_P (TREE_VALUE (init)))
  1379. {
  1380. gcc_checking_assert ((flags & LOOKUP_ONLYCONVERTING) == 0
  1381. && TREE_CHAIN (init) == NULL_TREE);
  1382. init = TREE_VALUE (init);
  1383. }
  1384. if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
  1385. && CP_AGGREGATE_TYPE_P (type))
  1386. /* A brace-enclosed initializer for an aggregate. In C++0x this can
  1387. happen for direct-initialization, too. */
  1388. init = digest_init (type, init, complain);
  1389. /* A CONSTRUCTOR of the target's type is a previously digested
  1390. initializer, whether that happened just above or in
  1391. cp_parser_late_parsing_nsdmi.
  1392. A TARGET_EXPR with TARGET_EXPR_DIRECT_INIT_P or TARGET_EXPR_LIST_INIT_P
  1393. set represents the whole initialization, so we shouldn't build up
  1394. another ctor call. */
  1395. if (init
  1396. && (TREE_CODE (init) == CONSTRUCTOR
  1397. || (TREE_CODE (init) == TARGET_EXPR
  1398. && (TARGET_EXPR_DIRECT_INIT_P (init)
  1399. || TARGET_EXPR_LIST_INIT_P (init))))
  1400. && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), type))
  1401. {
  1402. /* Early initialization via a TARGET_EXPR only works for
  1403. complete objects. */
  1404. gcc_assert (TREE_CODE (init) == CONSTRUCTOR || true_exp == exp);
  1405. init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
  1406. TREE_SIDE_EFFECTS (init) = 1;
  1407. finish_expr_stmt (init);
  1408. return;
  1409. }
  1410. if (init && TREE_CODE (init) != TREE_LIST
  1411. && (flags & LOOKUP_ONLYCONVERTING))
  1412. {
  1413. /* Base subobjects should only get direct-initialization. */
  1414. gcc_assert (true_exp == exp);
  1415. if (flags & DIRECT_BIND)
  1416. /* Do nothing. We hit this in two cases: Reference initialization,
  1417. where we aren't initializing a real variable, so we don't want
  1418. to run a new constructor; and catching an exception, where we
  1419. have already built up the constructor call so we could wrap it
  1420. in an exception region. */;
  1421. else
  1422. init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP,
  1423. flags, complain);
  1424. if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
  1425. /* We need to protect the initialization of a catch parm with a
  1426. call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
  1427. around the TARGET_EXPR for the copy constructor. See
  1428. initialize_handler_parm. */
  1429. {
  1430. TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
  1431. TREE_OPERAND (init, 0));
  1432. TREE_TYPE (init) = void_type_node;
  1433. }
  1434. else
  1435. init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
  1436. TREE_SIDE_EFFECTS (init) = 1;
  1437. finish_expr_stmt (init);
  1438. return;
  1439. }
  1440. if (init == NULL_TREE)
  1441. parms = NULL;
  1442. else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
  1443. {
  1444. parms = make_tree_vector ();
  1445. for (; init != NULL_TREE; init = TREE_CHAIN (init))
  1446. vec_safe_push (parms, TREE_VALUE (init));
  1447. }
  1448. else
  1449. parms = make_tree_vector_single (init);
  1450. if (exp == current_class_ref && current_function_decl
  1451. && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
  1452. {
  1453. /* Delegating constructor. */
  1454. tree complete;
  1455. tree base;
  1456. tree elt; unsigned i;
  1457. /* Unshare the arguments for the second call. */
  1458. vec<tree, va_gc> *parms2 = make_tree_vector ();
  1459. FOR_EACH_VEC_SAFE_ELT (parms, i, elt)
  1460. {
  1461. elt = break_out_target_exprs (elt);
  1462. vec_safe_push (parms2, elt);
  1463. }
  1464. complete = build_special_member_call (exp, complete_ctor_identifier,
  1465. &parms2, binfo, flags,
  1466. complain);
  1467. complete = fold_build_cleanup_point_expr (void_type_node, complete);
  1468. release_tree_vector (parms2);
  1469. base = build_special_member_call (exp, base_ctor_identifier,
  1470. &parms, binfo, flags,
  1471. complain);
  1472. base = fold_build_cleanup_point_expr (void_type_node, base);
  1473. rval = build3 (COND_EXPR, void_type_node,
  1474. build2 (EQ_EXPR, boolean_type_node,
  1475. current_in_charge_parm, integer_zero_node),
  1476. base,
  1477. complete);
  1478. }
  1479. else
  1480. {
  1481. if (true_exp == exp)
  1482. ctor_name = complete_ctor_identifier;
  1483. else
  1484. ctor_name = base_ctor_identifier;
  1485. rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
  1486. complain);
  1487. }
  1488. if (parms != NULL)
  1489. release_tree_vector (parms);
  1490. if (exp == true_exp && TREE_CODE (rval) == CALL_EXPR)
  1491. {
  1492. tree fn = get_callee_fndecl (rval);
  1493. if (fn && DECL_DECLARED_CONSTEXPR_P (fn))
  1494. {
  1495. tree e = maybe_constant_init (rval, exp);
  1496. if (TREE_CONSTANT (e))
  1497. rval = build2 (INIT_EXPR, type, exp, e);
  1498. }
  1499. }
  1500. /* FIXME put back convert_to_void? */
  1501. if (TREE_SIDE_EFFECTS (rval))
  1502. finish_expr_stmt (rval);
  1503. }
  1504. /* This function is responsible for initializing EXP with INIT
  1505. (if any).
  1506. BINFO is the binfo of the type for who we are performing the
  1507. initialization. For example, if W is a virtual base class of A and B,
  1508. and C : A, B.
  1509. If we are initializing B, then W must contain B's W vtable, whereas
  1510. were we initializing C, W must contain C's W vtable.
  1511. TRUE_EXP is nonzero if it is the true expression being initialized.
  1512. In this case, it may be EXP, or may just contain EXP. The reason we
  1513. need this is because if EXP is a base element of TRUE_EXP, we
  1514. don't necessarily know by looking at EXP where its virtual
  1515. baseclass fields should really be pointing. But we do know
  1516. from TRUE_EXP. In constructors, we don't know anything about
  1517. the value being initialized.
  1518. FLAGS is just passed to `build_new_method_call'. See that function
  1519. for its description. */
  1520. static void
  1521. expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
  1522. tsubst_flags_t complain)
  1523. {
  1524. tree type = TREE_TYPE (exp);
  1525. gcc_assert (init != error_mark_node && type != error_mark_node);
  1526. gcc_assert (building_stmt_list_p ());
  1527. /* Use a function returning the desired type to initialize EXP for us.
  1528. If the function is a constructor, and its first argument is
  1529. NULL_TREE, know that it was meant for us--just slide exp on
  1530. in and expand the constructor. Constructors now come
  1531. as TARGET_EXPRs. */
  1532. if (init && VAR_P (exp)
  1533. && COMPOUND_LITERAL_P (init))
  1534. {
  1535. vec<tree, va_gc> *cleanups = NULL;
  1536. /* If store_init_value returns NULL_TREE, the INIT has been
  1537. recorded as the DECL_INITIAL for EXP. That means there's
  1538. nothing more we have to do. */
  1539. init = store_init_value (exp, init, &cleanups, flags);
  1540. if (init)
  1541. finish_expr_stmt (init);
  1542. gcc_assert (!cleanups);
  1543. return;
  1544. }
  1545. /* If an explicit -- but empty -- initializer list was present,
  1546. that's value-initialization. */
  1547. if (init == void_type_node)
  1548. {
  1549. /* If the type has data but no user-provided ctor, we need to zero
  1550. out the object. */
  1551. if (!type_has_user_provided_constructor (type)
  1552. && !is_really_empty_class (type))
  1553. {
  1554. tree field_size = NULL_TREE;
  1555. if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
  1556. /* Don't clobber already initialized virtual bases. */
  1557. field_size = TYPE_SIZE (CLASSTYPE_AS_BASE (type));
  1558. init = build_zero_init_1 (type, NULL_TREE, /*static_storage_p=*/false,
  1559. field_size);
  1560. init = build2 (INIT_EXPR, type, exp, init);
  1561. finish_expr_stmt (init);
  1562. }
  1563. /* If we don't need to mess with the constructor at all,
  1564. then we're done. */
  1565. if (! type_build_ctor_call (type))
  1566. return;
  1567. /* Otherwise fall through and call the constructor. */
  1568. init = NULL_TREE;
  1569. }
  1570. /* We know that expand_default_init can handle everything we want
  1571. at this point. */
  1572. expand_default_init (binfo, true_exp, exp, init, flags, complain);
  1573. }
  1574. /* Report an error if TYPE is not a user-defined, class type. If
  1575. OR_ELSE is nonzero, give an error message. */
  1576. int
  1577. is_class_type (tree type, int or_else)
  1578. {
  1579. if (type == error_mark_node)
  1580. return 0;
  1581. if (! CLASS_TYPE_P (type))
  1582. {
  1583. if (or_else)
  1584. error ("%qT is not a class type", type);
  1585. return 0;
  1586. }
  1587. return 1;
  1588. }
  1589. tree
  1590. get_type_value (tree name)
  1591. {
  1592. if (name == error_mark_node)
  1593. return NULL_TREE;
  1594. if (IDENTIFIER_HAS_TYPE_VALUE (name))
  1595. return IDENTIFIER_TYPE_VALUE (name);
  1596. else
  1597. return NULL_TREE;
  1598. }
  1599. /* Build a reference to a member of an aggregate. This is not a C++
  1600. `&', but really something which can have its address taken, and
  1601. then act as a pointer to member, for example TYPE :: FIELD can have
  1602. its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
  1603. this expression is the operand of "&".
  1604. @@ Prints out lousy diagnostics for operator <typename>
  1605. @@ fields.
  1606. @@ This function should be rewritten and placed in search.c. */
  1607. tree
  1608. build_offset_ref (tree type, tree member, bool address_p,
  1609. tsubst_flags_t complain)
  1610. {
  1611. tree decl;
  1612. tree basebinfo = NULL_TREE;
  1613. /* class templates can come in as TEMPLATE_DECLs here. */
  1614. if (TREE_CODE (member) == TEMPLATE_DECL)
  1615. return member;
  1616. if (dependent_scope_p (type) || type_dependent_expression_p (member))
  1617. return build_qualified_name (NULL_TREE, type, member,
  1618. /*template_p=*/false);
  1619. gcc_assert (TYPE_P (type));
  1620. if (! is_class_type (type, 1))
  1621. return error_mark_node;
  1622. gcc_assert (DECL_P (member) || BASELINK_P (member));
  1623. /* Callers should call mark_used before this point. */
  1624. gcc_assert (!DECL_P (member) || TREE_USED (member));
  1625. type = TYPE_MAIN_VARIANT (type);
  1626. if (!COMPLETE_OR_OPEN_TYPE_P (complete_type (type)))
  1627. {
  1628. if (complain & tf_error)
  1629. error ("incomplete type %qT does not have member %qD", type, member);
  1630. return error_mark_node;
  1631. }
  1632. /* Entities other than non-static members need no further
  1633. processing. */
  1634. if (TREE_CODE (member) == TYPE_DECL)
  1635. return member;
  1636. if (VAR_P (member) || TREE_CODE (member) == CONST_DECL)
  1637. return convert_from_reference (member);
  1638. if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
  1639. {
  1640. if (complain & tf_error)
  1641. error ("invalid pointer to bit-field %qD", member);
  1642. return error_mark_node;
  1643. }
  1644. /* Set up BASEBINFO for member lookup. */
  1645. decl = maybe_dummy_object (type, &basebinfo);
  1646. /* A lot of this logic is now handled in lookup_member. */
  1647. if (BASELINK_P (member))
  1648. {
  1649. /* Go from the TREE_BASELINK to the member function info. */
  1650. tree t = BASELINK_FUNCTIONS (member);
  1651. if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
  1652. {
  1653. /* Get rid of a potential OVERLOAD around it. */
  1654. t = OVL_CURRENT (t);
  1655. /* Unique functions are handled easily. */
  1656. /* For non-static member of base class, we need a special rule
  1657. for access checking [class.protected]:
  1658. If the access is to form a pointer to member, the
  1659. nested-name-specifier shall name the derived class
  1660. (or any class derived from that class). */
  1661. if (address_p && DECL_P (t)
  1662. && DECL_NONSTATIC_MEMBER_P (t))
  1663. perform_or_defer_access_check (TYPE_BINFO (type), t, t,
  1664. complain);
  1665. else
  1666. perform_or_defer_access_check (basebinfo, t, t,
  1667. complain);
  1668. if (DECL_STATIC_FUNCTION_P (t))
  1669. return t;
  1670. member = t;
  1671. }
  1672. else
  1673. TREE_TYPE (member) = unknown_type_node;
  1674. }
  1675. else if (address_p && TREE_CODE (member) == FIELD_DECL)
  1676. /* We need additional test besides the one in
  1677. check_accessibility_of_qualified_id in case it is
  1678. a pointer to non-static member. */
  1679. perform_or_defer_access_check (TYPE_BINFO (type), member, member,
  1680. complain);
  1681. if (!address_p)
  1682. {
  1683. /* If MEMBER is non-static, then the program has fallen afoul of
  1684. [expr.prim]:
  1685. An id-expression that denotes a nonstatic data member or
  1686. nonstatic member function of a class can only be used:
  1687. -- as part of a class member access (_expr.ref_) in which the
  1688. object-expression refers to the member's class or a class
  1689. derived from that class, or
  1690. -- to form a pointer to member (_expr.unary.op_), or
  1691. -- in the body of a nonstatic member function of that class or
  1692. of a class derived from that class (_class.mfct.nonstatic_), or
  1693. -- in a mem-initializer for a constructor for that class or for
  1694. a class derived from that class (_class.base.init_). */
  1695. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
  1696. {
  1697. /* Build a representation of the qualified name suitable
  1698. for use as the operand to "&" -- even though the "&" is
  1699. not actually present. */
  1700. member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
  1701. /* In Microsoft mode, treat a non-static member function as if
  1702. it were a pointer-to-member. */
  1703. if (flag_ms_extensions)
  1704. {
  1705. PTRMEM_OK_P (member) = 1;
  1706. return cp_build_addr_expr (member, complain);
  1707. }
  1708. if (complain & tf_error)
  1709. error ("invalid use of non-static member function %qD",
  1710. TREE_OPERAND (member, 1));
  1711. return error_mark_node;
  1712. }
  1713. else if (TREE_CODE (member) == FIELD_DECL)
  1714. {
  1715. if (complain & tf_error)
  1716. error ("invalid use of non-static data member %qD", member);
  1717. return error_mark_node;
  1718. }
  1719. return member;
  1720. }
  1721. member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
  1722. PTRMEM_OK_P (member) = 1;
  1723. return member;
  1724. }
  1725. /* If DECL is a scalar enumeration constant or variable with a
  1726. constant initializer, return the initializer (or, its initializers,
  1727. recursively); otherwise, return DECL. If STRICT_P, the
  1728. initializer is only returned if DECL is a
  1729. constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
  1730. return an aggregate constant. */
  1731. static tree
  1732. constant_value_1 (tree decl, bool strict_p, bool return_aggregate_cst_ok_p)
  1733. {
  1734. while (TREE_CODE (decl) == CONST_DECL
  1735. || (strict_p
  1736. ? decl_constant_var_p (decl)
  1737. : (VAR_P (decl)
  1738. && CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
  1739. {
  1740. tree init;
  1741. /* If DECL is a static data member in a template
  1742. specialization, we must instantiate it here. The
  1743. initializer for the static data member is not processed
  1744. until needed; we need it now. */
  1745. mark_used (decl);
  1746. mark_rvalue_use (decl);
  1747. init = DECL_INITIAL (decl);
  1748. if (init == error_mark_node)
  1749. {
  1750. if (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
  1751. /* Treat the error as a constant to avoid cascading errors on
  1752. excessively recursive template instantiation (c++/9335). */
  1753. return init;
  1754. else
  1755. return decl;
  1756. }
  1757. /* Initializers in templates are generally expanded during
  1758. instantiation, so before that for const int i(2)
  1759. INIT is a TREE_LIST with the actual initializer as
  1760. TREE_VALUE. */
  1761. if (processing_template_decl
  1762. && init
  1763. && TREE_CODE (init) == TREE_LIST
  1764. && TREE_CHAIN (init) == NULL_TREE)
  1765. init = TREE_VALUE (init);
  1766. if (!init
  1767. || !TREE_TYPE (init)
  1768. || !TREE_CONSTANT (init)
  1769. || (!return_aggregate_cst_ok_p
  1770. /* Unless RETURN_AGGREGATE_CST_OK_P is true, do not
  1771. return an aggregate constant (of which string
  1772. literals are a special case), as we do not want
  1773. to make inadvertent copies of such entities, and
  1774. we must be sure that their addresses are the
  1775. same everywhere. */
  1776. && (TREE_CODE (init) == CONSTRUCTOR
  1777. || TREE_CODE (init) == STRING_CST)))
  1778. break;
  1779. decl = unshare_expr (init);
  1780. }
  1781. return decl;
  1782. }
  1783. /* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by constant
  1784. of integral or enumeration type, or a constexpr variable of scalar type,
  1785. then return that value. These are those variables permitted in constant
  1786. expressions by [5.19/1]. */
  1787. tree
  1788. scalar_constant_value (tree decl)
  1789. {
  1790. return constant_value_1 (decl, /*strict_p=*/true,
  1791. /*return_aggregate_cst_ok_p=*/false);
  1792. }
  1793. /* Like scalar_constant_value, but can also return aggregate initializers. */
  1794. tree
  1795. decl_really_constant_value (tree decl)
  1796. {
  1797. return constant_value_1 (decl, /*strict_p=*/true,
  1798. /*return_aggregate_cst_ok_p=*/true);
  1799. }
  1800. /* A more relaxed version of scalar_constant_value, used by the
  1801. common C/C++ code. */
  1802. tree
  1803. decl_constant_value (tree decl)
  1804. {
  1805. return constant_value_1 (decl, /*strict_p=*/processing_template_decl,
  1806. /*return_aggregate_cst_ok_p=*/true);
  1807. }
  1808. /* Common subroutines of build_new and build_vec_delete. */
  1809. /* Build and return a NEW_EXPR. If NELTS is non-NULL, TYPE[NELTS] is
  1810. the type of the object being allocated; otherwise, it's just TYPE.
  1811. INIT is the initializer, if any. USE_GLOBAL_NEW is true if the
  1812. user explicitly wrote "::operator new". PLACEMENT, if non-NULL, is
  1813. a vector of arguments to be provided as arguments to a placement
  1814. new operator. This routine performs no semantic checks; it just
  1815. creates and returns a NEW_EXPR. */
  1816. static tree
  1817. build_raw_new_expr (vec<tree, va_gc> *placement, tree type, tree nelts,
  1818. vec<tree, va_gc> *init, int use_global_new)
  1819. {
  1820. tree init_list;
  1821. tree new_expr;
  1822. /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
  1823. If INIT is not NULL, then we want to store VOID_ZERO_NODE. This
  1824. permits us to distinguish the case of a missing initializer "new
  1825. int" from an empty initializer "new int()". */
  1826. if (init == NULL)
  1827. init_list = NULL_TREE;
  1828. else if (init->is_empty ())
  1829. init_list = void_node;
  1830. else
  1831. init_list = build_tree_list_vec (init);
  1832. new_expr = build4 (NEW_EXPR, build_pointer_type (type),
  1833. build_tree_list_vec (placement), type, nelts,
  1834. init_list);
  1835. NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
  1836. TREE_SIDE_EFFECTS (new_expr) = 1;
  1837. return new_expr;
  1838. }
  1839. /* Diagnose uninitialized const members or reference members of type
  1840. TYPE. USING_NEW is used to disambiguate the diagnostic between a
  1841. new expression without a new-initializer and a declaration. Returns
  1842. the error count. */
  1843. static int
  1844. diagnose_uninitialized_cst_or_ref_member_1 (tree type, tree origin,
  1845. bool using_new, bool complain)
  1846. {
  1847. tree field;
  1848. int error_count = 0;
  1849. if (type_has_user_provided_constructor (type))
  1850. return 0;
  1851. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  1852. {
  1853. tree field_type;
  1854. if (TREE_CODE (field) != FIELD_DECL)
  1855. continue;
  1856. field_type = strip_array_types (TREE_TYPE (field));
  1857. if (type_has_user_provided_constructor (field_type))
  1858. continue;
  1859. if (TREE_CODE (field_type) == REFERENCE_TYPE)
  1860. {
  1861. ++ error_count;
  1862. if (complain)
  1863. {
  1864. if (DECL_CONTEXT (field) == origin)
  1865. {
  1866. if (using_new)
  1867. error ("uninitialized reference member in %q#T "
  1868. "using %<new%> without new-initializer", origin);
  1869. else
  1870. error ("uninitialized reference member in %q#T", origin);
  1871. }
  1872. else
  1873. {
  1874. if (using_new)
  1875. error ("uninitialized reference member in base %q#T "
  1876. "of %q#T using %<new%> without new-initializer",
  1877. DECL_CONTEXT (field), origin);
  1878. else
  1879. error ("uninitialized reference member in base %q#T "
  1880. "of %q#T", DECL_CONTEXT (field), origin);
  1881. }
  1882. inform (DECL_SOURCE_LOCATION (field),
  1883. "%q#D should be initialized", field);
  1884. }
  1885. }
  1886. if (CP_TYPE_CONST_P (field_type))
  1887. {
  1888. ++ error_count;
  1889. if (complain)
  1890. {
  1891. if (DECL_CONTEXT (field) == origin)
  1892. {
  1893. if (using_new)
  1894. error ("uninitialized const member in %q#T "
  1895. "using %<new%> without new-initializer", origin);
  1896. else
  1897. error ("uninitialized const member in %q#T", origin);
  1898. }
  1899. else
  1900. {
  1901. if (using_new)
  1902. error ("uninitialized const member in base %q#T "
  1903. "of %q#T using %<new%> without new-initializer",
  1904. DECL_CONTEXT (field), origin);
  1905. else
  1906. error ("uninitialized const member in base %q#T "
  1907. "of %q#T", DECL_CONTEXT (field), origin);
  1908. }
  1909. inform (DECL_SOURCE_LOCATION (field),
  1910. "%q#D should be initialized", field);
  1911. }
  1912. }
  1913. if (CLASS_TYPE_P (field_type))
  1914. error_count
  1915. += diagnose_uninitialized_cst_or_ref_member_1 (field_type, origin,
  1916. using_new, complain);
  1917. }
  1918. return error_count;
  1919. }
  1920. int
  1921. diagnose_uninitialized_cst_or_ref_member (tree type, bool using_new, bool complain)
  1922. {
  1923. return diagnose_uninitialized_cst_or_ref_member_1 (type, type, using_new, complain);
  1924. }
  1925. /* Call __cxa_bad_array_new_length to indicate that the size calculation
  1926. overflowed. Pretend it returns sizetype so that it plays nicely in the
  1927. COND_EXPR. */
  1928. tree
  1929. throw_bad_array_new_length (void)
  1930. {
  1931. tree fn = get_identifier ("__cxa_throw_bad_array_new_length");
  1932. if (!get_global_value_if_present (fn, &fn))
  1933. fn = push_throw_library_fn (fn, build_function_type_list (sizetype,
  1934. NULL_TREE));
  1935. return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
  1936. }
  1937. /* Generate code for a new-expression, including calling the "operator
  1938. new" function, initializing the object, and, if an exception occurs
  1939. during construction, cleaning up. The arguments are as for
  1940. build_raw_new_expr. This may change PLACEMENT and INIT. */
  1941. static tree
  1942. build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
  1943. vec<tree, va_gc> **init, bool globally_qualified_p,
  1944. tsubst_flags_t complain)
  1945. {
  1946. tree size, rval;
  1947. /* True iff this is a call to "operator new[]" instead of just
  1948. "operator new". */
  1949. bool array_p = false;
  1950. /* If ARRAY_P is true, the element type of the array. This is never
  1951. an ARRAY_TYPE; for something like "new int[3][4]", the
  1952. ELT_TYPE is "int". If ARRAY_P is false, this is the same type as
  1953. TYPE. */
  1954. tree elt_type;
  1955. /* The type of the new-expression. (This type is always a pointer
  1956. type.) */
  1957. tree pointer_type;
  1958. tree non_const_pointer_type;
  1959. tree outer_nelts = NULL_TREE;
  1960. /* For arrays, a bounds checks on the NELTS parameter. */
  1961. tree outer_nelts_check = NULL_TREE;
  1962. bool outer_nelts_from_type = false;
  1963. offset_int inner_nelts_count = 1;
  1964. tree alloc_call, alloc_expr;
  1965. /* Size of the inner array elements. */
  1966. offset_int inner_size;
  1967. /* The address returned by the call to "operator new". This node is
  1968. a VAR_DECL and is therefore reusable. */
  1969. tree alloc_node;
  1970. tree alloc_fn;
  1971. tree cookie_expr, init_expr;
  1972. int nothrow, check_new;
  1973. int use_java_new = 0;
  1974. /* If non-NULL, the number of extra bytes to allocate at the
  1975. beginning of the storage allocated for an array-new expression in
  1976. order to store the number of elements. */
  1977. tree cookie_size = NULL_TREE;
  1978. tree placement_first;
  1979. tree placement_expr = NULL_TREE;
  1980. /* True if the function we are calling is a placement allocation
  1981. function. */
  1982. bool placement_allocation_fn_p;
  1983. /* True if the storage must be initialized, either by a constructor
  1984. or due to an explicit new-initializer. */
  1985. bool is_initialized;
  1986. /* The address of the thing allocated, not including any cookie. In
  1987. particular, if an array cookie is in use, DATA_ADDR is the
  1988. address of the first array element. This node is a VAR_DECL, and
  1989. is therefore reusable. */
  1990. tree data_addr;
  1991. tree init_preeval_expr = NULL_TREE;
  1992. tree orig_type = type;
  1993. if (nelts)
  1994. {
  1995. outer_nelts = nelts;
  1996. array_p = true;
  1997. }
  1998. else if (TREE_CODE (type) == ARRAY_TYPE)
  1999. {
  2000. /* Transforms new (T[N]) to new T[N]. The former is a GNU
  2001. extension for variable N. (This also covers new T where T is
  2002. a VLA typedef.) */
  2003. array_p = true;
  2004. nelts = array_type_nelts_top (type);
  2005. outer_nelts = nelts;
  2006. type = TREE_TYPE (type);
  2007. outer_nelts_from_type = true;
  2008. }
  2009. /* If our base type is an array, then make sure we know how many elements
  2010. it has. */
  2011. for (elt_type = type;
  2012. TREE_CODE (elt_type) == ARRAY_TYPE;
  2013. elt_type = TREE_TYPE (elt_type))
  2014. {
  2015. tree inner_nelts = array_type_nelts_top (elt_type);
  2016. tree inner_nelts_cst = maybe_constant_value (inner_nelts);
  2017. if (TREE_CODE (inner_nelts_cst) == INTEGER_CST)
  2018. {
  2019. bool overflow;
  2020. offset_int result = wi::mul (wi::to_offset (inner_nelts_cst),
  2021. inner_nelts_count, SIGNED, &overflow);
  2022. if (overflow)
  2023. {
  2024. if (complain & tf_error)
  2025. error ("integer overflow in array size");
  2026. nelts = error_mark_node;
  2027. }
  2028. inner_nelts_count = result;
  2029. }
  2030. else
  2031. {
  2032. if (complain & tf_error)
  2033. {
  2034. error_at (EXPR_LOC_OR_LOC (inner_nelts, input_location),
  2035. "array size in new-expression must be constant");
  2036. cxx_constant_value(inner_nelts);
  2037. }
  2038. nelts = error_mark_node;
  2039. }
  2040. if (nelts != error_mark_node)
  2041. nelts = cp_build_binary_op (input_location,
  2042. MULT_EXPR, nelts,
  2043. inner_nelts_cst,
  2044. complain);
  2045. }
  2046. if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error))
  2047. {
  2048. error ("variably modified type not allowed in new-expression");
  2049. return error_mark_node;
  2050. }
  2051. if (nelts == error_mark_node)
  2052. return error_mark_node;
  2053. /* Warn if we performed the (T[N]) to T[N] transformation and N is
  2054. variable. */
  2055. if (outer_nelts_from_type
  2056. && !TREE_CONSTANT (maybe_constant_value (outer_nelts)))
  2057. {
  2058. if (complain & tf_warning_or_error)
  2059. {
  2060. const char *msg;
  2061. if (typedef_variant_p (orig_type))
  2062. msg = ("non-constant array new length must be specified "
  2063. "directly, not by typedef");
  2064. else
  2065. msg = ("non-constant array new length must be specified "
  2066. "without parentheses around the type-id");
  2067. pedwarn (EXPR_LOC_OR_LOC (outer_nelts, input_location),
  2068. OPT_Wvla, msg);
  2069. }
  2070. else
  2071. return error_mark_node;
  2072. }
  2073. if (VOID_TYPE_P (elt_type))
  2074. {
  2075. if (complain & tf_error)
  2076. error ("invalid type %<void%> for new");
  2077. return error_mark_node;
  2078. }
  2079. if (abstract_virtuals_error_sfinae (ACU_NEW, elt_type, complain))
  2080. return error_mark_node;
  2081. is_initialized = (type_build_ctor_call (elt_type) || *init != NULL);
  2082. if (*init == NULL && cxx_dialect < cxx11)
  2083. {
  2084. bool maybe_uninitialized_error = false;
  2085. /* A program that calls for default-initialization [...] of an
  2086. entity of reference type is ill-formed. */
  2087. if (CLASSTYPE_REF_FIELDS_NEED_INIT (elt_type))
  2088. maybe_uninitialized_error = true;
  2089. /* A new-expression that creates an object of type T initializes
  2090. that object as follows:
  2091. - If the new-initializer is omitted:
  2092. -- If T is a (possibly cv-qualified) non-POD class type
  2093. (or array thereof), the object is default-initialized (8.5).
  2094. [...]
  2095. -- Otherwise, the object created has indeterminate
  2096. value. If T is a const-qualified type, or a (possibly
  2097. cv-qualified) POD class type (or array thereof)
  2098. containing (directly or indirectly) a member of
  2099. const-qualified type, the program is ill-formed; */
  2100. if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (elt_type))
  2101. maybe_uninitialized_error = true;
  2102. if (maybe_uninitialized_error
  2103. && diagnose_uninitialized_cst_or_ref_member (elt_type,
  2104. /*using_new=*/true,
  2105. complain & tf_error))
  2106. return error_mark_node;
  2107. }
  2108. if (CP_TYPE_CONST_P (elt_type) && *init == NULL
  2109. && default_init_uninitialized_part (elt_type))
  2110. {
  2111. if (complain & tf_error)
  2112. error ("uninitialized const in %<new%> of %q#T", elt_type);
  2113. return error_mark_node;
  2114. }
  2115. size = size_in_bytes (elt_type);
  2116. if (array_p)
  2117. {
  2118. /* Maximum available size in bytes. Half of the address space
  2119. minus the cookie size. */
  2120. offset_int max_size
  2121. = wi::set_bit_in_zero <offset_int> (TYPE_PRECISION (sizetype) - 1);
  2122. /* Maximum number of outer elements which can be allocated. */
  2123. offset_int max_outer_nelts;
  2124. tree max_outer_nelts_tree;
  2125. gcc_assert (TREE_CODE (size) == INTEGER_CST);
  2126. cookie_size = targetm.cxx.get_cookie_size (elt_type);
  2127. gcc_assert (TREE_CODE (cookie_size) == INTEGER_CST);
  2128. gcc_checking_assert (wi::ltu_p (wi::to_offset (cookie_size), max_size));
  2129. /* Unconditionally subtract the cookie size. This decreases the
  2130. maximum object size and is safe even if we choose not to use
  2131. a cookie after all. */
  2132. max_size -= wi::to_offset (cookie_size);
  2133. bool overflow;
  2134. inner_size = wi::mul (wi::to_offset (size), inner_nelts_count, SIGNED,
  2135. &overflow);
  2136. if (overflow || wi::gtu_p (inner_size, max_size))
  2137. {
  2138. if (complain & tf_error)
  2139. error ("size of array is too large");
  2140. return error_mark_node;
  2141. }
  2142. max_outer_nelts = wi::udiv_trunc (max_size, inner_size);
  2143. /* Only keep the top-most seven bits, to simplify encoding the
  2144. constant in the instruction stream. */
  2145. {
  2146. unsigned shift = (max_outer_nelts.get_precision ()) - 7
  2147. - wi::clz (max_outer_nelts);
  2148. max_outer_nelts = wi::lshift (wi::lrshift (max_outer_nelts, shift),
  2149. shift);
  2150. }
  2151. max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts);
  2152. size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
  2153. outer_nelts_check = fold_build2 (LE_EXPR, boolean_type_node,
  2154. outer_nelts,
  2155. max_outer_nelts_tree);
  2156. }
  2157. alloc_fn = NULL_TREE;
  2158. /* If PLACEMENT is a single simple pointer type not passed by
  2159. reference, prepare to capture it in a temporary variable. Do
  2160. this now, since PLACEMENT will change in the calls below. */
  2161. placement_first = NULL_TREE;
  2162. if (vec_safe_length (*placement) == 1
  2163. && (TYPE_PTR_P (TREE_TYPE ((**placement)[0]))))
  2164. placement_first = (**placement)[0];
  2165. /* Allocate the object. */
  2166. if (vec_safe_is_empty (*placement) && TYPE_FOR_JAVA (elt_type))
  2167. {
  2168. tree class_addr;
  2169. tree class_decl;
  2170. static const char alloc_name[] = "_Jv_AllocObject";
  2171. if (!MAYBE_CLASS_TYPE_P (elt_type))
  2172. {
  2173. error ("%qT isn%'t a valid Java class type", elt_type);
  2174. return error_mark_node;
  2175. }
  2176. class_decl = build_java_class_ref (elt_type);
  2177. if (class_decl == error_mark_node)
  2178. return error_mark_node;
  2179. use_java_new = 1;
  2180. if (!get_global_value_if_present (get_identifier (alloc_name),
  2181. &alloc_fn))
  2182. {
  2183. if (complain & tf_error)
  2184. error ("call to Java constructor with %qs undefined", alloc_name);
  2185. return error_mark_node;
  2186. }
  2187. else if (really_overloaded_fn (alloc_fn))
  2188. {
  2189. if (complain & tf_error)
  2190. error ("%qD should never be overloaded", alloc_fn);
  2191. return error_mark_node;
  2192. }
  2193. alloc_fn = OVL_CURRENT (alloc_fn);
  2194. class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
  2195. alloc_call = cp_build_function_call_nary (alloc_fn, complain,
  2196. class_addr, NULL_TREE);
  2197. }
  2198. else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
  2199. {
  2200. error ("Java class %q#T object allocated using placement new", elt_type);
  2201. return error_mark_node;
  2202. }
  2203. else
  2204. {
  2205. tree fnname;
  2206. tree fns;
  2207. fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
  2208. if (!globally_qualified_p
  2209. && CLASS_TYPE_P (elt_type)
  2210. && (array_p
  2211. ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
  2212. : TYPE_HAS_NEW_OPERATOR (elt_type)))
  2213. {
  2214. /* Use a class-specific operator new. */
  2215. /* If a cookie is required, add some extra space. */
  2216. if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
  2217. size = size_binop (PLUS_EXPR, size, cookie_size);
  2218. else
  2219. {
  2220. cookie_size = NULL_TREE;
  2221. /* No size arithmetic necessary, so the size check is
  2222. not needed. */
  2223. if (outer_nelts_check != NULL && inner_size == 1)
  2224. outer_nelts_check = NULL_TREE;
  2225. }
  2226. /* Perform the overflow check. */
  2227. tree errval = TYPE_MAX_VALUE (sizetype);
  2228. if (cxx_dialect >= cxx11 && flag_exceptions)
  2229. errval = throw_bad_array_new_length ();
  2230. if (outer_nelts_check != NULL_TREE)
  2231. size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check,
  2232. size, errval);
  2233. /* Create the argument list. */
  2234. vec_safe_insert (*placement, 0, size);
  2235. /* Do name-lookup to find the appropriate operator. */
  2236. fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
  2237. if (fns == NULL_TREE)
  2238. {
  2239. if (complain & tf_error)
  2240. error ("no suitable %qD found in class %qT", fnname, elt_type);
  2241. return error_mark_node;
  2242. }
  2243. if (TREE_CODE (fns) == TREE_LIST)
  2244. {
  2245. if (complain & tf_error)
  2246. {
  2247. error ("request for member %qD is ambiguous", fnname);
  2248. print_candidates (fns);
  2249. }
  2250. return error_mark_node;
  2251. }
  2252. alloc_call = build_new_method_call (build_dummy_object (elt_type),
  2253. fns, placement,
  2254. /*conversion_path=*/NULL_TREE,
  2255. LOOKUP_NORMAL,
  2256. &alloc_fn,
  2257. complain);
  2258. }
  2259. else
  2260. {
  2261. /* Use a global operator new. */
  2262. /* See if a cookie might be required. */
  2263. if (!(array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)))
  2264. {
  2265. cookie_size = NULL_TREE;
  2266. /* No size arithmetic necessary, so the size check is
  2267. not needed. */
  2268. if (outer_nelts_check != NULL && inner_size == 1)
  2269. outer_nelts_check = NULL_TREE;
  2270. }
  2271. alloc_call = build_operator_new_call (fnname, placement,
  2272. &size, &cookie_size,
  2273. outer_nelts_check,
  2274. &alloc_fn, complain);
  2275. }
  2276. }
  2277. if (alloc_call == error_mark_node)
  2278. return error_mark_node;
  2279. gcc_assert (alloc_fn != NULL_TREE);
  2280. /* If we found a simple case of PLACEMENT_EXPR above, then copy it
  2281. into a temporary variable. */
  2282. if (!processing_template_decl
  2283. && placement_first != NULL_TREE
  2284. && TREE_CODE (alloc_call) == CALL_EXPR
  2285. && call_expr_nargs (alloc_call) == 2
  2286. && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
  2287. && TYPE_PTR_P (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))))
  2288. {
  2289. tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
  2290. if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
  2291. || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
  2292. {
  2293. placement_expr = get_target_expr (placement_first);
  2294. CALL_EXPR_ARG (alloc_call, 1)
  2295. = convert (TREE_TYPE (placement_arg), placement_expr);
  2296. }
  2297. }
  2298. /* In the simple case, we can stop now. */
  2299. pointer_type = build_pointer_type (type);
  2300. if (!cookie_size && !is_initialized)
  2301. return build_nop (pointer_type, alloc_call);
  2302. /* Store the result of the allocation call in a variable so that we can
  2303. use it more than once. */
  2304. alloc_expr = get_target_expr (alloc_call);
  2305. alloc_node = TARGET_EXPR_SLOT (alloc_expr);
  2306. /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
  2307. while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
  2308. alloc_call = TREE_OPERAND (alloc_call, 1);
  2309. /* Now, check to see if this function is actually a placement
  2310. allocation function. This can happen even when PLACEMENT is NULL
  2311. because we might have something like:
  2312. struct S { void* operator new (size_t, int i = 0); };
  2313. A call to `new S' will get this allocation function, even though
  2314. there is no explicit placement argument. If there is more than
  2315. one argument, or there are variable arguments, then this is a
  2316. placement allocation function. */
  2317. placement_allocation_fn_p
  2318. = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
  2319. || varargs_function_p (alloc_fn));
  2320. /* Preevaluate the placement args so that we don't reevaluate them for a
  2321. placement delete. */
  2322. if (placement_allocation_fn_p)
  2323. {
  2324. tree inits;
  2325. stabilize_call (alloc_call, &inits);
  2326. if (inits)
  2327. alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
  2328. alloc_expr);
  2329. }
  2330. /* unless an allocation function is declared with an empty excep-
  2331. tion-specification (_except.spec_), throw(), it indicates failure to
  2332. allocate storage by throwing a bad_alloc exception (clause _except_,
  2333. _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
  2334. cation function is declared with an empty exception-specification,
  2335. throw(), it returns null to indicate failure to allocate storage and a
  2336. non-null pointer otherwise.
  2337. So check for a null exception spec on the op new we just called. */
  2338. nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
  2339. check_new = (flag_check_new || nothrow) && ! use_java_new;
  2340. if (cookie_size)
  2341. {
  2342. tree cookie;
  2343. tree cookie_ptr;
  2344. tree size_ptr_type;
  2345. /* Adjust so we're pointing to the start of the object. */
  2346. data_addr = fold_build_pointer_plus (alloc_node, cookie_size);
  2347. /* Store the number of bytes allocated so that we can know how
  2348. many elements to destroy later. We use the last sizeof
  2349. (size_t) bytes to store the number of elements. */
  2350. cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
  2351. cookie_ptr = fold_build_pointer_plus_loc (input_location,
  2352. alloc_node, cookie_ptr);
  2353. size_ptr_type = build_pointer_type (sizetype);
  2354. cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
  2355. cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
  2356. cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
  2357. if (targetm.cxx.cookie_has_size ())
  2358. {
  2359. /* Also store the element size. */
  2360. cookie_ptr = fold_build_pointer_plus (cookie_ptr,
  2361. fold_build1_loc (input_location,
  2362. NEGATE_EXPR, sizetype,
  2363. size_in_bytes (sizetype)));
  2364. cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
  2365. cookie = build2 (MODIFY_EXPR, sizetype, cookie,
  2366. size_in_bytes (elt_type));
  2367. cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
  2368. cookie, cookie_expr);
  2369. }
  2370. }
  2371. else
  2372. {
  2373. cookie_expr = NULL_TREE;
  2374. data_addr = alloc_node;
  2375. }
  2376. /* Now use a pointer to the type we've actually allocated. */
  2377. /* But we want to operate on a non-const version to start with,
  2378. since we'll be modifying the elements. */
  2379. non_const_pointer_type = build_pointer_type
  2380. (cp_build_qualified_type (type, cp_type_quals (type) & ~TYPE_QUAL_CONST));
  2381. data_addr = fold_convert (non_const_pointer_type, data_addr);
  2382. /* Any further uses of alloc_node will want this type, too. */
  2383. alloc_node = fold_convert (non_const_pointer_type, alloc_node);
  2384. /* Now initialize the allocated object. Note that we preevaluate the
  2385. initialization expression, apart from the actual constructor call or
  2386. assignment--we do this because we want to delay the allocation as long
  2387. as possible in order to minimize the size of the exception region for
  2388. placement delete. */
  2389. if (is_initialized)
  2390. {
  2391. bool stable;
  2392. bool explicit_value_init_p = false;
  2393. if (*init != NULL && (*init)->is_empty ())
  2394. {
  2395. *init = NULL;
  2396. explicit_value_init_p = true;
  2397. }
  2398. if (processing_template_decl && explicit_value_init_p)
  2399. {
  2400. /* build_value_init doesn't work in templates, and we don't need
  2401. the initializer anyway since we're going to throw it away and
  2402. rebuild it at instantiation time, so just build up a single
  2403. constructor call to get any appropriate diagnostics. */
  2404. init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
  2405. if (type_build_ctor_call (elt_type))
  2406. init_expr = build_special_member_call (init_expr,
  2407. complete_ctor_identifier,
  2408. init, elt_type,
  2409. LOOKUP_NORMAL,
  2410. complain);
  2411. stable = stabilize_init (init_expr, &init_preeval_expr);
  2412. }
  2413. else if (array_p)
  2414. {
  2415. tree vecinit = NULL_TREE;
  2416. if (vec_safe_length (*init) == 1
  2417. && DIRECT_LIST_INIT_P ((**init)[0]))
  2418. {
  2419. vecinit = (**init)[0];
  2420. if (CONSTRUCTOR_NELTS (vecinit) == 0)
  2421. /* List-value-initialization, leave it alone. */;
  2422. else
  2423. {
  2424. tree arraytype, domain;
  2425. if (TREE_CONSTANT (nelts))
  2426. domain = compute_array_index_type (NULL_TREE, nelts,
  2427. complain);
  2428. else
  2429. /* We'll check the length at runtime. */
  2430. domain = NULL_TREE;
  2431. arraytype = build_cplus_array_type (type, domain);
  2432. vecinit = digest_init (arraytype, vecinit, complain);
  2433. }
  2434. }
  2435. else if (*init)
  2436. {
  2437. if (complain & tf_error)
  2438. permerror (input_location,
  2439. "parenthesized initializer in array new");
  2440. else
  2441. return error_mark_node;
  2442. vecinit = build_tree_list_vec (*init);
  2443. }
  2444. init_expr
  2445. = build_vec_init (data_addr,
  2446. cp_build_binary_op (input_location,
  2447. MINUS_EXPR, outer_nelts,
  2448. integer_one_node,
  2449. complain),
  2450. vecinit,
  2451. explicit_value_init_p,
  2452. /*from_array=*/0,
  2453. complain);
  2454. /* An array initialization is stable because the initialization
  2455. of each element is a full-expression, so the temporaries don't
  2456. leak out. */
  2457. stable = true;
  2458. }
  2459. else
  2460. {
  2461. init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
  2462. if (type_build_ctor_call (type) && !explicit_value_init_p)
  2463. {
  2464. init_expr = build_special_member_call (init_expr,
  2465. complete_ctor_identifier,
  2466. init, elt_type,
  2467. LOOKUP_NORMAL,
  2468. complain);
  2469. }
  2470. else if (explicit_value_init_p)
  2471. {
  2472. /* Something like `new int()'. */
  2473. tree val = build_value_init (type, complain);
  2474. if (val == error_mark_node)
  2475. return error_mark_node;
  2476. init_expr = build2 (INIT_EXPR, type, init_expr, val);
  2477. }
  2478. else
  2479. {
  2480. tree ie;
  2481. /* We are processing something like `new int (10)', which
  2482. means allocate an int, and initialize it with 10. */
  2483. ie = build_x_compound_expr_from_vec (*init, "new initializer",
  2484. complain);
  2485. init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
  2486. complain);
  2487. }
  2488. stable = stabilize_init (init_expr, &init_preeval_expr);
  2489. }
  2490. if (init_expr == error_mark_node)
  2491. return error_mark_node;
  2492. /* If any part of the object initialization terminates by throwing an
  2493. exception and a suitable deallocation function can be found, the
  2494. deallocation function is called to free the memory in which the
  2495. object was being constructed, after which the exception continues
  2496. to propagate in the context of the new-expression. If no
  2497. unambiguous matching deallocation function can be found,
  2498. propagating the exception does not cause the object's memory to be
  2499. freed. */
  2500. if (flag_exceptions && ! use_java_new)
  2501. {
  2502. enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
  2503. tree cleanup;
  2504. /* The Standard is unclear here, but the right thing to do
  2505. is to use the same method for finding deallocation
  2506. functions that we use for finding allocation functions. */
  2507. cleanup = (build_op_delete_call
  2508. (dcode,
  2509. alloc_node,
  2510. size,
  2511. globally_qualified_p,
  2512. placement_allocation_fn_p ? alloc_call : NULL_TREE,
  2513. alloc_fn,
  2514. complain));
  2515. if (!cleanup)
  2516. /* We're done. */;
  2517. else if (stable)
  2518. /* This is much simpler if we were able to preevaluate all of
  2519. the arguments to the constructor call. */
  2520. {
  2521. /* CLEANUP is compiler-generated, so no diagnostics. */
  2522. TREE_NO_WARNING (cleanup) = true;
  2523. init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
  2524. init_expr, cleanup);
  2525. /* Likewise, this try-catch is compiler-generated. */
  2526. TREE_NO_WARNING (init_expr) = true;
  2527. }
  2528. else
  2529. /* Ack! First we allocate the memory. Then we set our sentry
  2530. variable to true, and expand a cleanup that deletes the
  2531. memory if sentry is true. Then we run the constructor, and
  2532. finally clear the sentry.
  2533. We need to do this because we allocate the space first, so
  2534. if there are any temporaries with cleanups in the
  2535. constructor args and we weren't able to preevaluate them, we
  2536. need this EH region to extend until end of full-expression
  2537. to preserve nesting. */
  2538. {
  2539. tree end, sentry, begin;
  2540. begin = get_target_expr (boolean_true_node);
  2541. CLEANUP_EH_ONLY (begin) = 1;
  2542. sentry = TARGET_EXPR_SLOT (begin);
  2543. /* CLEANUP is compiler-generated, so no diagnostics. */
  2544. TREE_NO_WARNING (cleanup) = true;
  2545. TARGET_EXPR_CLEANUP (begin)
  2546. = build3 (COND_EXPR, void_type_node, sentry,
  2547. cleanup, void_node);
  2548. end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
  2549. sentry, boolean_false_node);
  2550. init_expr
  2551. = build2 (COMPOUND_EXPR, void_type_node, begin,
  2552. build2 (COMPOUND_EXPR, void_type_node, init_expr,
  2553. end));
  2554. /* Likewise, this is compiler-generated. */
  2555. TREE_NO_WARNING (init_expr) = true;
  2556. }
  2557. }
  2558. }
  2559. else
  2560. init_expr = NULL_TREE;
  2561. /* Now build up the return value in reverse order. */
  2562. rval = data_addr;
  2563. if (init_expr)
  2564. rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
  2565. if (cookie_expr)
  2566. rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
  2567. if (rval == data_addr)
  2568. /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
  2569. and return the call (which doesn't need to be adjusted). */
  2570. rval = TARGET_EXPR_INITIAL (alloc_expr);
  2571. else
  2572. {
  2573. if (check_new)
  2574. {
  2575. tree ifexp = cp_build_binary_op (input_location,
  2576. NE_EXPR, alloc_node,
  2577. nullptr_node,
  2578. complain);
  2579. rval = build_conditional_expr (input_location, ifexp, rval,
  2580. alloc_node, complain);
  2581. }
  2582. /* Perform the allocation before anything else, so that ALLOC_NODE
  2583. has been initialized before we start using it. */
  2584. rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
  2585. }
  2586. if (init_preeval_expr)
  2587. rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
  2588. /* A new-expression is never an lvalue. */
  2589. gcc_assert (!lvalue_p (rval));
  2590. return convert (pointer_type, rval);
  2591. }
  2592. /* Generate a representation for a C++ "new" expression. *PLACEMENT
  2593. is a vector of placement-new arguments (or NULL if none). If NELTS
  2594. is NULL, TYPE is the type of the storage to be allocated. If NELTS
  2595. is not NULL, then this is an array-new allocation; TYPE is the type
  2596. of the elements in the array and NELTS is the number of elements in
  2597. the array. *INIT, if non-NULL, is the initializer for the new
  2598. object, or an empty vector to indicate an initializer of "()". If
  2599. USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
  2600. rather than just "new". This may change PLACEMENT and INIT. */
  2601. tree
  2602. build_new (vec<tree, va_gc> **placement, tree type, tree nelts,
  2603. vec<tree, va_gc> **init, int use_global_new, tsubst_flags_t complain)
  2604. {
  2605. tree rval;
  2606. vec<tree, va_gc> *orig_placement = NULL;
  2607. tree orig_nelts = NULL_TREE;
  2608. vec<tree, va_gc> *orig_init = NULL;
  2609. if (type == error_mark_node)
  2610. return error_mark_node;
  2611. if (nelts == NULL_TREE && vec_safe_length (*init) == 1
  2612. /* Don't do auto deduction where it might affect mangling. */
  2613. && (!processing_template_decl || at_function_scope_p ()))
  2614. {
  2615. tree auto_node = type_uses_auto (type);
  2616. if (auto_node)
  2617. {
  2618. tree d_init = (**init)[0];
  2619. d_init = resolve_nondeduced_context (d_init);
  2620. type = do_auto_deduction (type, d_init, auto_node);
  2621. }
  2622. }
  2623. if (processing_template_decl)
  2624. {
  2625. if (dependent_type_p (type)
  2626. || any_type_dependent_arguments_p (*placement)
  2627. || (nelts && type_dependent_expression_p (nelts))
  2628. || (nelts && *init)
  2629. || any_type_dependent_arguments_p (*init))
  2630. return build_raw_new_expr (*placement, type, nelts, *init,
  2631. use_global_new);
  2632. orig_placement = make_tree_vector_copy (*placement);
  2633. orig_nelts = nelts;
  2634. if (*init)
  2635. orig_init = make_tree_vector_copy (*init);
  2636. make_args_non_dependent (*placement);
  2637. if (nelts)
  2638. nelts = build_non_dependent_expr (nelts);
  2639. make_args_non_dependent (*init);
  2640. }
  2641. if (nelts)
  2642. {
  2643. if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
  2644. {
  2645. if (complain & tf_error)
  2646. permerror (input_location, "size in array new must have integral type");
  2647. else
  2648. return error_mark_node;
  2649. }
  2650. nelts = mark_rvalue_use (nelts);
  2651. nelts = cp_save_expr (cp_convert (sizetype, nelts, complain));
  2652. }
  2653. /* ``A reference cannot be created by the new operator. A reference
  2654. is not an object (8.2.2, 8.4.3), so a pointer to it could not be
  2655. returned by new.'' ARM 5.3.3 */
  2656. if (TREE_CODE (type) == REFERENCE_TYPE)
  2657. {
  2658. if (complain & tf_error)
  2659. error ("new cannot be applied to a reference type");
  2660. else
  2661. return error_mark_node;
  2662. type = TREE_TYPE (type);
  2663. }
  2664. if (TREE_CODE (type) == FUNCTION_TYPE)
  2665. {
  2666. if (complain & tf_error)
  2667. error ("new cannot be applied to a function type");
  2668. return error_mark_node;
  2669. }
  2670. /* The type allocated must be complete. If the new-type-id was
  2671. "T[N]" then we are just checking that "T" is complete here, but
  2672. that is equivalent, since the value of "N" doesn't matter. */
  2673. if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
  2674. return error_mark_node;
  2675. rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
  2676. if (rval == error_mark_node)
  2677. return error_mark_node;
  2678. if (processing_template_decl)
  2679. {
  2680. tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
  2681. orig_init, use_global_new);
  2682. release_tree_vector (orig_placement);
  2683. release_tree_vector (orig_init);
  2684. return ret;
  2685. }
  2686. /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
  2687. rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
  2688. TREE_NO_WARNING (rval) = 1;
  2689. return rval;
  2690. }
  2691. /* Given a Java class, return a decl for the corresponding java.lang.Class. */
  2692. tree
  2693. build_java_class_ref (tree type)
  2694. {
  2695. tree name = NULL_TREE, class_decl;
  2696. static tree CL_suffix = NULL_TREE;
  2697. if (CL_suffix == NULL_TREE)
  2698. CL_suffix = get_identifier("class$");
  2699. if (jclass_node == NULL_TREE)
  2700. {
  2701. jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
  2702. if (jclass_node == NULL_TREE)
  2703. {
  2704. error ("call to Java constructor, while %<jclass%> undefined");
  2705. return error_mark_node;
  2706. }
  2707. jclass_node = TREE_TYPE (jclass_node);
  2708. }
  2709. /* Mangle the class$ field. */
  2710. {
  2711. tree field;
  2712. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  2713. if (DECL_NAME (field) == CL_suffix)
  2714. {
  2715. mangle_decl (field);
  2716. name = DECL_ASSEMBLER_NAME (field);
  2717. break;
  2718. }
  2719. if (!field)
  2720. {
  2721. error ("can%'t find %<class$%> in %qT", type);
  2722. return error_mark_node;
  2723. }
  2724. }
  2725. class_decl = IDENTIFIER_GLOBAL_VALUE (name);
  2726. if (class_decl == NULL_TREE)
  2727. {
  2728. class_decl = build_decl (input_location,
  2729. VAR_DECL, name, TREE_TYPE (jclass_node));
  2730. TREE_STATIC (class_decl) = 1;
  2731. DECL_EXTERNAL (class_decl) = 1;
  2732. TREE_PUBLIC (class_decl) = 1;
  2733. DECL_ARTIFICIAL (class_decl) = 1;
  2734. DECL_IGNORED_P (class_decl) = 1;
  2735. pushdecl_top_level (class_decl);
  2736. make_decl_rtl (class_decl);
  2737. }
  2738. return class_decl;
  2739. }
  2740. static tree
  2741. build_vec_delete_1 (tree base, tree maxindex, tree type,
  2742. special_function_kind auto_delete_vec,
  2743. int use_global_delete, tsubst_flags_t complain)
  2744. {
  2745. tree virtual_size;
  2746. tree ptype = build_pointer_type (type = complete_type (type));
  2747. tree size_exp;
  2748. /* Temporary variables used by the loop. */
  2749. tree tbase, tbase_init;
  2750. /* This is the body of the loop that implements the deletion of a
  2751. single element, and moves temp variables to next elements. */
  2752. tree body;
  2753. /* This is the LOOP_EXPR that governs the deletion of the elements. */
  2754. tree loop = 0;
  2755. /* This is the thing that governs what to do after the loop has run. */
  2756. tree deallocate_expr = 0;
  2757. /* This is the BIND_EXPR which holds the outermost iterator of the
  2758. loop. It is convenient to set this variable up and test it before
  2759. executing any other code in the loop.
  2760. This is also the containing expression returned by this function. */
  2761. tree controller = NULL_TREE;
  2762. tree tmp;
  2763. /* We should only have 1-D arrays here. */
  2764. gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
  2765. if (base == error_mark_node || maxindex == error_mark_node)
  2766. return error_mark_node;
  2767. if (!COMPLETE_TYPE_P (type))
  2768. {
  2769. if ((complain & tf_warning)
  2770. && warning (OPT_Wdelete_incomplete,
  2771. "possible problem detected in invocation of "
  2772. "delete [] operator:"))
  2773. {
  2774. cxx_incomplete_type_diagnostic (base, type, DK_WARNING);
  2775. inform (input_location, "neither the destructor nor the "
  2776. "class-specific operator delete [] will be called, "
  2777. "even if they are declared when the class is defined");
  2778. }
  2779. /* This size won't actually be used. */
  2780. size_exp = size_one_node;
  2781. goto no_destructor;
  2782. }
  2783. size_exp = size_in_bytes (type);
  2784. if (! MAYBE_CLASS_TYPE_P (type))
  2785. goto no_destructor;
  2786. else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  2787. {
  2788. /* Make sure the destructor is callable. */
  2789. if (type_build_dtor_call (type))
  2790. {
  2791. tmp = build_delete (ptype, base, sfk_complete_destructor,
  2792. LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
  2793. complain);
  2794. if (tmp == error_mark_node)
  2795. return error_mark_node;
  2796. }
  2797. goto no_destructor;
  2798. }
  2799. /* The below is short by the cookie size. */
  2800. virtual_size = size_binop (MULT_EXPR, size_exp,
  2801. convert (sizetype, maxindex));
  2802. tbase = create_temporary_var (ptype);
  2803. tbase_init
  2804. = cp_build_modify_expr (tbase, NOP_EXPR,
  2805. fold_build_pointer_plus_loc (input_location,
  2806. fold_convert (ptype,
  2807. base),
  2808. virtual_size),
  2809. complain);
  2810. if (tbase_init == error_mark_node)
  2811. return error_mark_node;
  2812. controller = build3 (BIND_EXPR, void_type_node, tbase,
  2813. NULL_TREE, NULL_TREE);
  2814. TREE_SIDE_EFFECTS (controller) = 1;
  2815. body = build1 (EXIT_EXPR, void_type_node,
  2816. build2 (EQ_EXPR, boolean_type_node, tbase,
  2817. fold_convert (ptype, base)));
  2818. tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
  2819. tmp = fold_build_pointer_plus (tbase, tmp);
  2820. tmp = cp_build_modify_expr (tbase, NOP_EXPR, tmp, complain);
  2821. if (tmp == error_mark_node)
  2822. return error_mark_node;
  2823. body = build_compound_expr (input_location, body, tmp);
  2824. tmp = build_delete (ptype, tbase, sfk_complete_destructor,
  2825. LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
  2826. complain);
  2827. if (tmp == error_mark_node)
  2828. return error_mark_node;
  2829. body = build_compound_expr (input_location, body, tmp);
  2830. loop = build1 (LOOP_EXPR, void_type_node, body);
  2831. loop = build_compound_expr (input_location, tbase_init, loop);
  2832. no_destructor:
  2833. /* Delete the storage if appropriate. */
  2834. if (auto_delete_vec == sfk_deleting_destructor)
  2835. {
  2836. tree base_tbd;
  2837. /* The below is short by the cookie size. */
  2838. virtual_size = size_binop (MULT_EXPR, size_exp,
  2839. convert (sizetype, maxindex));
  2840. if (! TYPE_VEC_NEW_USES_COOKIE (type))
  2841. /* no header */
  2842. base_tbd = base;
  2843. else
  2844. {
  2845. tree cookie_size;
  2846. cookie_size = targetm.cxx.get_cookie_size (type);
  2847. base_tbd = cp_build_binary_op (input_location,
  2848. MINUS_EXPR,
  2849. cp_convert (string_type_node,
  2850. base, complain),
  2851. cookie_size,
  2852. complain);
  2853. if (base_tbd == error_mark_node)
  2854. return error_mark_node;
  2855. base_tbd = cp_convert (ptype, base_tbd, complain);
  2856. /* True size with header. */
  2857. virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
  2858. }
  2859. deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
  2860. base_tbd, virtual_size,
  2861. use_global_delete & 1,
  2862. /*placement=*/NULL_TREE,
  2863. /*alloc_fn=*/NULL_TREE,
  2864. complain);
  2865. }
  2866. body = loop;
  2867. if (!deallocate_expr)
  2868. ;
  2869. else if (!body)
  2870. body = deallocate_expr;
  2871. else
  2872. body = build_compound_expr (input_location, body, deallocate_expr);
  2873. if (!body)
  2874. body = integer_zero_node;
  2875. /* Outermost wrapper: If pointer is null, punt. */
  2876. body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
  2877. fold_build2_loc (input_location,
  2878. NE_EXPR, boolean_type_node, base,
  2879. convert (TREE_TYPE (base),
  2880. nullptr_node)),
  2881. body, integer_zero_node);
  2882. body = build1 (NOP_EXPR, void_type_node, body);
  2883. if (controller)
  2884. {
  2885. TREE_OPERAND (controller, 1) = body;
  2886. body = controller;
  2887. }
  2888. if (TREE_CODE (base) == SAVE_EXPR)
  2889. /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
  2890. body = build2 (COMPOUND_EXPR, void_type_node, base, body);
  2891. return convert_to_void (body, ICV_CAST, complain);
  2892. }
  2893. /* Create an unnamed variable of the indicated TYPE. */
  2894. tree
  2895. create_temporary_var (tree type)
  2896. {
  2897. tree decl;
  2898. decl = build_decl (input_location,
  2899. VAR_DECL, NULL_TREE, type);
  2900. TREE_USED (decl) = 1;
  2901. DECL_ARTIFICIAL (decl) = 1;
  2902. DECL_IGNORED_P (decl) = 1;
  2903. DECL_CONTEXT (decl) = current_function_decl;
  2904. return decl;
  2905. }
  2906. /* Create a new temporary variable of the indicated TYPE, initialized
  2907. to INIT.
  2908. It is not entered into current_binding_level, because that breaks
  2909. things when it comes time to do final cleanups (which take place
  2910. "outside" the binding contour of the function). */
  2911. tree
  2912. get_temp_regvar (tree type, tree init)
  2913. {
  2914. tree decl;
  2915. decl = create_temporary_var (type);
  2916. add_decl_expr (decl);
  2917. finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
  2918. tf_warning_or_error));
  2919. return decl;
  2920. }
  2921. /* Subroutine of build_vec_init. Returns true if assigning to an array of
  2922. INNER_ELT_TYPE from INIT is trivial. */
  2923. static bool
  2924. vec_copy_assign_is_trivial (tree inner_elt_type, tree init)
  2925. {
  2926. tree fromtype = inner_elt_type;
  2927. if (real_lvalue_p (init))
  2928. fromtype = cp_build_reference_type (fromtype, /*rval*/false);
  2929. return is_trivially_xible (MODIFY_EXPR, inner_elt_type, fromtype);
  2930. }
  2931. /* `build_vec_init' returns tree structure that performs
  2932. initialization of a vector of aggregate types.
  2933. BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
  2934. to the first element, of POINTER_TYPE.
  2935. MAXINDEX is the maximum index of the array (one less than the
  2936. number of elements). It is only used if BASE is a pointer or
  2937. TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
  2938. INIT is the (possibly NULL) initializer.
  2939. If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL. All
  2940. elements in the array are value-initialized.
  2941. FROM_ARRAY is 0 if we should init everything with INIT
  2942. (i.e., every element initialized from INIT).
  2943. FROM_ARRAY is 1 if we should index into INIT in parallel
  2944. with initialization of DECL.
  2945. FROM_ARRAY is 2 if we should index into INIT in parallel,
  2946. but use assignment instead of initialization. */
  2947. tree
  2948. build_vec_init (tree base, tree maxindex, tree init,
  2949. bool explicit_value_init_p,
  2950. int from_array, tsubst_flags_t complain)
  2951. {
  2952. tree rval;
  2953. tree base2 = NULL_TREE;
  2954. tree itype = NULL_TREE;
  2955. tree iterator;
  2956. /* The type of BASE. */
  2957. tree atype = TREE_TYPE (base);
  2958. /* The type of an element in the array. */
  2959. tree type = TREE_TYPE (atype);
  2960. /* The element type reached after removing all outer array
  2961. types. */
  2962. tree inner_elt_type;
  2963. /* The type of a pointer to an element in the array. */
  2964. tree ptype;
  2965. tree stmt_expr;
  2966. tree compound_stmt;
  2967. int destroy_temps;
  2968. tree try_block = NULL_TREE;
  2969. int num_initialized_elts = 0;
  2970. bool is_global;
  2971. tree obase = base;
  2972. bool xvalue = false;
  2973. bool errors = false;
  2974. if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
  2975. maxindex = array_type_nelts (atype);
  2976. if (maxindex == NULL_TREE || maxindex == error_mark_node)
  2977. return error_mark_node;
  2978. if (explicit_value_init_p)
  2979. gcc_assert (!init);
  2980. inner_elt_type = strip_array_types (type);
  2981. /* Look through the TARGET_EXPR around a compound literal. */
  2982. if (init && TREE_CODE (init) == TARGET_EXPR
  2983. && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
  2984. && from_array != 2)
  2985. init = TARGET_EXPR_INITIAL (init);
  2986. /* If we have a braced-init-list, make sure that the array
  2987. is big enough for all the initializers. */
  2988. bool length_check = (init && TREE_CODE (init) == CONSTRUCTOR
  2989. && CONSTRUCTOR_NELTS (init) > 0
  2990. && !TREE_CONSTANT (maxindex));
  2991. if (init
  2992. && TREE_CODE (atype) == ARRAY_TYPE
  2993. && TREE_CONSTANT (maxindex)
  2994. && (from_array == 2
  2995. ? vec_copy_assign_is_trivial (inner_elt_type, init)
  2996. : !TYPE_NEEDS_CONSTRUCTING (type))
  2997. && ((TREE_CODE (init) == CONSTRUCTOR
  2998. /* Don't do this if the CONSTRUCTOR might contain something
  2999. that might throw and require us to clean up. */
  3000. && (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))
  3001. || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
  3002. || from_array))
  3003. {
  3004. /* Do non-default initialization of trivial arrays resulting from
  3005. brace-enclosed initializers. In this case, digest_init and
  3006. store_constructor will handle the semantics for us. */
  3007. if (BRACE_ENCLOSED_INITIALIZER_P (init))
  3008. init = digest_init (atype, init, complain);
  3009. stmt_expr = build2 (INIT_EXPR, atype, base, init);
  3010. return stmt_expr;
  3011. }
  3012. maxindex = cp_convert (ptrdiff_type_node, maxindex, complain);
  3013. if (TREE_CODE (atype) == ARRAY_TYPE)
  3014. {
  3015. ptype = build_pointer_type (type);
  3016. base = decay_conversion (base, complain);
  3017. if (base == error_mark_node)
  3018. return error_mark_node;
  3019. base = cp_convert (ptype, base, complain);
  3020. }
  3021. else
  3022. ptype = atype;
  3023. /* The code we are generating looks like:
  3024. ({
  3025. T* t1 = (T*) base;
  3026. T* rval = t1;
  3027. ptrdiff_t iterator = maxindex;
  3028. try {
  3029. for (; iterator != -1; --iterator) {
  3030. ... initialize *t1 ...
  3031. ++t1;
  3032. }
  3033. } catch (...) {
  3034. ... destroy elements that were constructed ...
  3035. }
  3036. rval;
  3037. })
  3038. We can omit the try and catch blocks if we know that the
  3039. initialization will never throw an exception, or if the array
  3040. elements do not have destructors. We can omit the loop completely if
  3041. the elements of the array do not have constructors.
  3042. We actually wrap the entire body of the above in a STMT_EXPR, for
  3043. tidiness.
  3044. When copying from array to another, when the array elements have
  3045. only trivial copy constructors, we should use __builtin_memcpy
  3046. rather than generating a loop. That way, we could take advantage
  3047. of whatever cleverness the back end has for dealing with copies
  3048. of blocks of memory. */
  3049. is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
  3050. destroy_temps = stmts_are_full_exprs_p ();
  3051. current_stmt_tree ()->stmts_are_full_exprs_p = 0;
  3052. rval = get_temp_regvar (ptype, base);
  3053. base = get_temp_regvar (ptype, rval);
  3054. iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
  3055. /* If initializing one array from another, initialize element by
  3056. element. We rely upon the below calls to do the argument
  3057. checking. Evaluate the initializer before entering the try block. */
  3058. if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
  3059. {
  3060. if (lvalue_kind (init) & clk_rvalueref)
  3061. xvalue = true;
  3062. base2 = decay_conversion (init, complain);
  3063. if (base2 == error_mark_node)
  3064. return error_mark_node;
  3065. itype = TREE_TYPE (base2);
  3066. base2 = get_temp_regvar (itype, base2);
  3067. itype = TREE_TYPE (itype);
  3068. }
  3069. /* Protect the entire array initialization so that we can destroy
  3070. the partially constructed array if an exception is thrown.
  3071. But don't do this if we're assigning. */
  3072. if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
  3073. && from_array != 2)
  3074. {
  3075. try_block = begin_try_block ();
  3076. }
  3077. /* Should we try to create a constant initializer? */
  3078. bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
  3079. && TREE_CONSTANT (maxindex)
  3080. && (init ? TREE_CODE (init) == CONSTRUCTOR
  3081. : (type_has_constexpr_default_constructor
  3082. (inner_elt_type)))
  3083. && (literal_type_p (inner_elt_type)
  3084. || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
  3085. vec<constructor_elt, va_gc> *const_vec = NULL;
  3086. bool saw_non_const = false;
  3087. /* If we're initializing a static array, we want to do static
  3088. initialization of any elements with constant initializers even if
  3089. some are non-constant. */
  3090. bool do_static_init = (DECL_P (obase) && TREE_STATIC (obase));
  3091. bool empty_list = false;
  3092. if (init && BRACE_ENCLOSED_INITIALIZER_P (init)
  3093. && CONSTRUCTOR_NELTS (init) == 0)
  3094. /* Skip over the handling of non-empty init lists. */
  3095. empty_list = true;
  3096. /* Maybe pull out constant value when from_array? */
  3097. else if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
  3098. {
  3099. /* Do non-default initialization of non-trivial arrays resulting from
  3100. brace-enclosed initializers. */
  3101. unsigned HOST_WIDE_INT idx;
  3102. tree field, elt;
  3103. /* If the constructor already has the array type, it's been through
  3104. digest_init, so we shouldn't try to do anything more. */
  3105. bool digested = same_type_p (atype, TREE_TYPE (init));
  3106. from_array = 0;
  3107. if (length_check)
  3108. {
  3109. tree nelts = size_int (CONSTRUCTOR_NELTS (init) - 1);
  3110. if (TREE_CODE (atype) != ARRAY_TYPE)
  3111. {
  3112. if (flag_exceptions)
  3113. {
  3114. tree c = fold_build2 (LT_EXPR, boolean_type_node, iterator,
  3115. nelts);
  3116. c = build3 (COND_EXPR, void_type_node, c,
  3117. throw_bad_array_new_length (), void_node);
  3118. finish_expr_stmt (c);
  3119. }
  3120. /* Don't check an array new when -fno-exceptions. */
  3121. }
  3122. else if (flag_sanitize & SANITIZE_BOUNDS
  3123. && do_ubsan_in_current_function ())
  3124. {
  3125. /* Make sure the last element of the initializer is in bounds. */
  3126. finish_expr_stmt
  3127. (ubsan_instrument_bounds
  3128. (input_location, obase, &nelts, /*ignore_off_by_one*/false));
  3129. }
  3130. }
  3131. if (try_const)
  3132. vec_alloc (const_vec, CONSTRUCTOR_NELTS (init));
  3133. FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, field, elt)
  3134. {
  3135. tree baseref = build1 (INDIRECT_REF, type, base);
  3136. tree one_init;
  3137. num_initialized_elts++;
  3138. current_stmt_tree ()->stmts_are_full_exprs_p = 1;
  3139. if (digested)
  3140. one_init = build2 (INIT_EXPR, type, baseref, elt);
  3141. else if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
  3142. one_init = build_aggr_init (baseref, elt, 0, complain);
  3143. else
  3144. one_init = cp_build_modify_expr (baseref, NOP_EXPR,
  3145. elt, complain);
  3146. if (one_init == error_mark_node)
  3147. errors = true;
  3148. if (try_const)
  3149. {
  3150. tree e = maybe_constant_init (one_init);
  3151. if (reduced_constant_expression_p (e))
  3152. {
  3153. CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
  3154. if (do_static_init)
  3155. one_init = NULL_TREE;
  3156. else
  3157. one_init = build2 (INIT_EXPR, type, baseref, e);
  3158. }
  3159. else
  3160. {
  3161. if (do_static_init)
  3162. {
  3163. tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
  3164. true);
  3165. if (value)
  3166. CONSTRUCTOR_APPEND_ELT (const_vec, field, value);
  3167. }
  3168. saw_non_const = true;
  3169. }
  3170. }
  3171. if (one_init)
  3172. finish_expr_stmt (one_init);
  3173. current_stmt_tree ()->stmts_are_full_exprs_p = 0;
  3174. one_init = cp_build_unary_op (PREINCREMENT_EXPR, base, 0, complain);
  3175. if (one_init == error_mark_node)
  3176. errors = true;
  3177. else
  3178. finish_expr_stmt (one_init);
  3179. one_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
  3180. complain);
  3181. if (one_init == error_mark_node)
  3182. errors = true;
  3183. else
  3184. finish_expr_stmt (one_init);
  3185. }
  3186. /* Any elements without explicit initializers get T{}. */
  3187. empty_list = true;
  3188. }
  3189. else if (from_array)
  3190. {
  3191. if (init)
  3192. /* OK, we set base2 above. */;
  3193. else if (CLASS_TYPE_P (type)
  3194. && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
  3195. {
  3196. if (complain & tf_error)
  3197. error ("initializer ends prematurely");
  3198. errors = true;
  3199. }
  3200. }
  3201. /* Now, default-initialize any remaining elements. We don't need to
  3202. do that if a) the type does not need constructing, or b) we've
  3203. already initialized all the elements.
  3204. We do need to keep going if we're copying an array. */
  3205. if (try_const && !init)
  3206. /* With a constexpr default constructor, which we checked for when
  3207. setting try_const above, default-initialization is equivalent to
  3208. value-initialization, and build_value_init gives us something more
  3209. friendly to maybe_constant_init. */
  3210. explicit_value_init_p = true;
  3211. if (from_array
  3212. || ((type_build_ctor_call (type) || init || explicit_value_init_p)
  3213. && ! (tree_fits_shwi_p (maxindex)
  3214. && (num_initialized_elts
  3215. == tree_to_shwi (maxindex) + 1))))
  3216. {
  3217. /* If the ITERATOR is equal to -1, then we don't have to loop;
  3218. we've already initialized all the elements. */
  3219. tree for_stmt;
  3220. tree elt_init;
  3221. tree to;
  3222. for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
  3223. finish_for_init_stmt (for_stmt);
  3224. finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
  3225. build_int_cst (TREE_TYPE (iterator), -1)),
  3226. for_stmt, false);
  3227. elt_init = cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
  3228. complain);
  3229. if (elt_init == error_mark_node)
  3230. errors = true;
  3231. finish_for_expr (elt_init, for_stmt);
  3232. to = build1 (INDIRECT_REF, type, base);
  3233. /* If the initializer is {}, then all elements are initialized from T{}.
  3234. But for non-classes, that's the same as value-initialization. */
  3235. if (empty_list)
  3236. {
  3237. if (cxx_dialect >= cxx11 && AGGREGATE_TYPE_P (type))
  3238. {
  3239. init = build_constructor (init_list_type_node, NULL);
  3240. CONSTRUCTOR_IS_DIRECT_INIT (init) = true;
  3241. }
  3242. else
  3243. {
  3244. init = NULL_TREE;
  3245. explicit_value_init_p = true;
  3246. }
  3247. }
  3248. if (from_array)
  3249. {
  3250. tree from;
  3251. if (base2)
  3252. {
  3253. from = build1 (INDIRECT_REF, itype, base2);
  3254. if (xvalue)
  3255. from = move (from);
  3256. }
  3257. else
  3258. from = NULL_TREE;
  3259. if (from_array == 2)
  3260. elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
  3261. complain);
  3262. else if (type_build_ctor_call (type))
  3263. elt_init = build_aggr_init (to, from, 0, complain);
  3264. else if (from)
  3265. elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
  3266. complain);
  3267. else
  3268. gcc_unreachable ();
  3269. }
  3270. else if (TREE_CODE (type) == ARRAY_TYPE)
  3271. {
  3272. if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
  3273. sorry
  3274. ("cannot initialize multi-dimensional array with initializer");
  3275. elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
  3276. 0, init,
  3277. explicit_value_init_p,
  3278. 0, complain);
  3279. }
  3280. else if (explicit_value_init_p)
  3281. {
  3282. elt_init = build_value_init (type, complain);
  3283. if (elt_init != error_mark_node)
  3284. elt_init = build2 (INIT_EXPR, type, to, elt_init);
  3285. }
  3286. else
  3287. {
  3288. gcc_assert (type_build_ctor_call (type) || init);
  3289. if (CLASS_TYPE_P (type))
  3290. elt_init = build_aggr_init (to, init, 0, complain);
  3291. else
  3292. {
  3293. if (TREE_CODE (init) == TREE_LIST)
  3294. init = build_x_compound_expr_from_list (init, ELK_INIT,
  3295. complain);
  3296. elt_init = build2 (INIT_EXPR, type, to, init);
  3297. }
  3298. }
  3299. if (elt_init == error_mark_node)
  3300. errors = true;
  3301. if (try_const)
  3302. {
  3303. /* FIXME refs to earlier elts */
  3304. tree e = maybe_constant_init (elt_init);
  3305. if (reduced_constant_expression_p (e))
  3306. {
  3307. if (initializer_zerop (e))
  3308. /* Don't fill the CONSTRUCTOR with zeros. */
  3309. e = NULL_TREE;
  3310. if (do_static_init)
  3311. elt_init = NULL_TREE;
  3312. }
  3313. else
  3314. {
  3315. saw_non_const = true;
  3316. if (do_static_init)
  3317. e = build_zero_init (TREE_TYPE (e), NULL_TREE, true);
  3318. else
  3319. e = NULL_TREE;
  3320. }
  3321. if (e)
  3322. {
  3323. int max = tree_to_shwi (maxindex)+1;
  3324. for (; num_initialized_elts < max; ++num_initialized_elts)
  3325. {
  3326. tree field = size_int (num_initialized_elts);
  3327. CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
  3328. }
  3329. }
  3330. }
  3331. current_stmt_tree ()->stmts_are_full_exprs_p = 1;
  3332. if (elt_init)
  3333. finish_expr_stmt (elt_init);
  3334. current_stmt_tree ()->stmts_are_full_exprs_p = 0;
  3335. finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
  3336. complain));
  3337. if (base2)
  3338. finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
  3339. complain));
  3340. finish_for_stmt (for_stmt);
  3341. }
  3342. /* Make sure to cleanup any partially constructed elements. */
  3343. if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
  3344. && from_array != 2)
  3345. {
  3346. tree e;
  3347. tree m = cp_build_binary_op (input_location,
  3348. MINUS_EXPR, maxindex, iterator,
  3349. complain);
  3350. /* Flatten multi-dimensional array since build_vec_delete only
  3351. expects one-dimensional array. */
  3352. if (TREE_CODE (type) == ARRAY_TYPE)
  3353. m = cp_build_binary_op (input_location,
  3354. MULT_EXPR, m,
  3355. /* Avoid mixing signed and unsigned. */
  3356. convert (TREE_TYPE (m),
  3357. array_type_nelts_total (type)),
  3358. complain);
  3359. finish_cleanup_try_block (try_block);
  3360. e = build_vec_delete_1 (rval, m,
  3361. inner_elt_type, sfk_complete_destructor,
  3362. /*use_global_delete=*/0, complain);
  3363. if (e == error_mark_node)
  3364. errors = true;
  3365. finish_cleanup (e, try_block);
  3366. }
  3367. /* The value of the array initialization is the array itself, RVAL
  3368. is a pointer to the first element. */
  3369. finish_stmt_expr_expr (rval, stmt_expr);
  3370. stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
  3371. current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
  3372. if (errors)
  3373. return error_mark_node;
  3374. if (try_const)
  3375. {
  3376. if (!saw_non_const)
  3377. {
  3378. tree const_init = build_constructor (atype, const_vec);
  3379. return build2 (INIT_EXPR, atype, obase, const_init);
  3380. }
  3381. else if (do_static_init && !vec_safe_is_empty (const_vec))
  3382. DECL_INITIAL (obase) = build_constructor (atype, const_vec);
  3383. else
  3384. vec_free (const_vec);
  3385. }
  3386. /* Now make the result have the correct type. */
  3387. if (TREE_CODE (atype) == ARRAY_TYPE)
  3388. {
  3389. atype = build_pointer_type (atype);
  3390. stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
  3391. stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
  3392. TREE_NO_WARNING (stmt_expr) = 1;
  3393. }
  3394. return stmt_expr;
  3395. }
  3396. /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
  3397. build_delete. */
  3398. static tree
  3399. build_dtor_call (tree exp, special_function_kind dtor_kind, int flags,
  3400. tsubst_flags_t complain)
  3401. {
  3402. tree name;
  3403. tree fn;
  3404. switch (dtor_kind)
  3405. {
  3406. case sfk_complete_destructor:
  3407. name = complete_dtor_identifier;
  3408. break;
  3409. case sfk_base_destructor:
  3410. name = base_dtor_identifier;
  3411. break;
  3412. case sfk_deleting_destructor:
  3413. name = deleting_dtor_identifier;
  3414. break;
  3415. default:
  3416. gcc_unreachable ();
  3417. }
  3418. fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
  3419. return build_new_method_call (exp, fn,
  3420. /*args=*/NULL,
  3421. /*conversion_path=*/NULL_TREE,
  3422. flags,
  3423. /*fn_p=*/NULL,
  3424. complain);
  3425. }
  3426. /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
  3427. ADDR is an expression which yields the store to be destroyed.
  3428. AUTO_DELETE is the name of the destructor to call, i.e., either
  3429. sfk_complete_destructor, sfk_base_destructor, or
  3430. sfk_deleting_destructor.
  3431. FLAGS is the logical disjunction of zero or more LOOKUP_
  3432. flags. See cp-tree.h for more info. */
  3433. tree
  3434. build_delete (tree otype, tree addr, special_function_kind auto_delete,
  3435. int flags, int use_global_delete, tsubst_flags_t complain)
  3436. {
  3437. tree expr;
  3438. if (addr == error_mark_node)
  3439. return error_mark_node;
  3440. tree type = TYPE_MAIN_VARIANT (otype);
  3441. /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
  3442. set to `error_mark_node' before it gets properly cleaned up. */
  3443. if (type == error_mark_node)
  3444. return error_mark_node;
  3445. if (TREE_CODE (type) == POINTER_TYPE)
  3446. type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  3447. if (TREE_CODE (type) == ARRAY_TYPE)
  3448. {
  3449. if (TYPE_DOMAIN (type) == NULL_TREE)
  3450. {
  3451. if (complain & tf_error)
  3452. error ("unknown array size in delete");
  3453. return error_mark_node;
  3454. }
  3455. return build_vec_delete (addr, array_type_nelts (type),
  3456. auto_delete, use_global_delete, complain);
  3457. }
  3458. if (TYPE_PTR_P (otype))
  3459. {
  3460. addr = mark_rvalue_use (addr);
  3461. /* We don't want to warn about delete of void*, only other
  3462. incomplete types. Deleting other incomplete types
  3463. invokes undefined behavior, but it is not ill-formed, so
  3464. compile to something that would even do The Right Thing
  3465. (TM) should the type have a trivial dtor and no delete
  3466. operator. */
  3467. if (!VOID_TYPE_P (type))
  3468. {
  3469. complete_type (type);
  3470. if (!COMPLETE_TYPE_P (type))
  3471. {
  3472. if ((complain & tf_warning)
  3473. && warning (OPT_Wdelete_incomplete,
  3474. "possible problem detected in invocation of "
  3475. "delete operator:"))
  3476. {
  3477. cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
  3478. inform (input_location,
  3479. "neither the destructor nor the class-specific "
  3480. "operator delete will be called, even if they are "
  3481. "declared when the class is defined");
  3482. }
  3483. }
  3484. else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
  3485. && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
  3486. && TYPE_POLYMORPHIC_P (type))
  3487. {
  3488. tree dtor;
  3489. dtor = CLASSTYPE_DESTRUCTORS (type);
  3490. if (!dtor || !DECL_VINDEX (dtor))
  3491. {
  3492. if (CLASSTYPE_PURE_VIRTUALS (type))
  3493. warning (OPT_Wdelete_non_virtual_dtor,
  3494. "deleting object of abstract class type %qT"
  3495. " which has non-virtual destructor"
  3496. " will cause undefined behaviour", type);
  3497. else
  3498. warning (OPT_Wdelete_non_virtual_dtor,
  3499. "deleting object of polymorphic class type %qT"
  3500. " which has non-virtual destructor"
  3501. " might cause undefined behaviour", type);
  3502. }
  3503. }
  3504. }
  3505. if (TREE_SIDE_EFFECTS (addr))
  3506. addr = save_expr (addr);
  3507. /* Throw away const and volatile on target type of addr. */
  3508. addr = convert_force (build_pointer_type (type), addr, 0, complain);
  3509. }
  3510. else
  3511. {
  3512. /* Don't check PROTECT here; leave that decision to the
  3513. destructor. If the destructor is accessible, call it,
  3514. else report error. */
  3515. addr = cp_build_addr_expr (addr, complain);
  3516. if (addr == error_mark_node)
  3517. return error_mark_node;
  3518. if (TREE_SIDE_EFFECTS (addr))
  3519. addr = save_expr (addr);
  3520. addr = convert_force (build_pointer_type (type), addr, 0, complain);
  3521. }
  3522. if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  3523. {
  3524. /* Make sure the destructor is callable. */
  3525. if (type_build_dtor_call (type))
  3526. {
  3527. expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL,
  3528. complain),
  3529. sfk_complete_destructor, flags, complain);
  3530. if (expr == error_mark_node)
  3531. return error_mark_node;
  3532. }
  3533. if (auto_delete != sfk_deleting_destructor)
  3534. return void_node;
  3535. return build_op_delete_call (DELETE_EXPR, addr,
  3536. cxx_sizeof_nowarn (type),
  3537. use_global_delete,
  3538. /*placement=*/NULL_TREE,
  3539. /*alloc_fn=*/NULL_TREE,
  3540. complain);
  3541. }
  3542. else
  3543. {
  3544. tree head = NULL_TREE;
  3545. tree do_delete = NULL_TREE;
  3546. tree ifexp;
  3547. if (CLASSTYPE_LAZY_DESTRUCTOR (type))
  3548. lazily_declare_fn (sfk_destructor, type);
  3549. /* For `::delete x', we must not use the deleting destructor
  3550. since then we would not be sure to get the global `operator
  3551. delete'. */
  3552. if (use_global_delete && auto_delete == sfk_deleting_destructor)
  3553. {
  3554. /* We will use ADDR multiple times so we must save it. */
  3555. addr = save_expr (addr);
  3556. head = get_target_expr (build_headof (addr));
  3557. /* Delete the object. */
  3558. do_delete = build_op_delete_call (DELETE_EXPR,
  3559. head,
  3560. cxx_sizeof_nowarn (type),
  3561. /*global_p=*/true,
  3562. /*placement=*/NULL_TREE,
  3563. /*alloc_fn=*/NULL_TREE,
  3564. complain);
  3565. /* Otherwise, treat this like a complete object destructor
  3566. call. */
  3567. auto_delete = sfk_complete_destructor;
  3568. }
  3569. /* If the destructor is non-virtual, there is no deleting
  3570. variant. Instead, we must explicitly call the appropriate
  3571. `operator delete' here. */
  3572. else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
  3573. && auto_delete == sfk_deleting_destructor)
  3574. {
  3575. /* We will use ADDR multiple times so we must save it. */
  3576. addr = save_expr (addr);
  3577. /* Build the call. */
  3578. do_delete = build_op_delete_call (DELETE_EXPR,
  3579. addr,
  3580. cxx_sizeof_nowarn (type),
  3581. /*global_p=*/false,
  3582. /*placement=*/NULL_TREE,
  3583. /*alloc_fn=*/NULL_TREE,
  3584. complain);
  3585. /* Call the complete object destructor. */
  3586. auto_delete = sfk_complete_destructor;
  3587. }
  3588. else if (auto_delete == sfk_deleting_destructor
  3589. && TYPE_GETS_REG_DELETE (type))
  3590. {
  3591. /* Make sure we have access to the member op delete, even though
  3592. we'll actually be calling it from the destructor. */
  3593. build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
  3594. /*global_p=*/false,
  3595. /*placement=*/NULL_TREE,
  3596. /*alloc_fn=*/NULL_TREE,
  3597. complain);
  3598. }
  3599. expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL, complain),
  3600. auto_delete, flags, complain);
  3601. if (expr == error_mark_node)
  3602. return error_mark_node;
  3603. if (do_delete)
  3604. expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
  3605. /* We need to calculate this before the dtor changes the vptr. */
  3606. if (head)
  3607. expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
  3608. if (flags & LOOKUP_DESTRUCTOR)
  3609. /* Explicit destructor call; don't check for null pointer. */
  3610. ifexp = integer_one_node;
  3611. else
  3612. {
  3613. /* Handle deleting a null pointer. */
  3614. ifexp = fold (cp_build_binary_op (input_location,
  3615. NE_EXPR, addr, nullptr_node,
  3616. complain));
  3617. if (ifexp == error_mark_node)
  3618. return error_mark_node;
  3619. }
  3620. if (ifexp != integer_one_node)
  3621. expr = build3 (COND_EXPR, void_type_node, ifexp, expr, void_node);
  3622. return expr;
  3623. }
  3624. }
  3625. /* At the beginning of a destructor, push cleanups that will call the
  3626. destructors for our base classes and members.
  3627. Called from begin_destructor_body. */
  3628. void
  3629. push_base_cleanups (void)
  3630. {
  3631. tree binfo, base_binfo;
  3632. int i;
  3633. tree member;
  3634. tree expr;
  3635. vec<tree, va_gc> *vbases;
  3636. /* Run destructors for all virtual baseclasses. */
  3637. if (CLASSTYPE_VBASECLASSES (current_class_type))
  3638. {
  3639. tree cond = (condition_conversion
  3640. (build2 (BIT_AND_EXPR, integer_type_node,
  3641. current_in_charge_parm,
  3642. integer_two_node)));
  3643. /* The CLASSTYPE_VBASECLASSES vector is in initialization
  3644. order, which is also the right order for pushing cleanups. */
  3645. for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
  3646. vec_safe_iterate (vbases, i, &base_binfo); i++)
  3647. {
  3648. if (type_build_dtor_call (BINFO_TYPE (base_binfo)))
  3649. {
  3650. expr = build_special_member_call (current_class_ref,
  3651. base_dtor_identifier,
  3652. NULL,
  3653. base_binfo,
  3654. (LOOKUP_NORMAL
  3655. | LOOKUP_NONVIRTUAL),
  3656. tf_warning_or_error);
  3657. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
  3658. {
  3659. expr = build3 (COND_EXPR, void_type_node, cond,
  3660. expr, void_node);
  3661. finish_decl_cleanup (NULL_TREE, expr);
  3662. }
  3663. }
  3664. }
  3665. }
  3666. /* Take care of the remaining baseclasses. */
  3667. for (binfo = TYPE_BINFO (current_class_type), i = 0;
  3668. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  3669. {
  3670. if (BINFO_VIRTUAL_P (base_binfo)
  3671. || !type_build_dtor_call (BINFO_TYPE (base_binfo)))
  3672. continue;
  3673. expr = build_special_member_call (current_class_ref,
  3674. base_dtor_identifier,
  3675. NULL, base_binfo,
  3676. LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
  3677. tf_warning_or_error);
  3678. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
  3679. finish_decl_cleanup (NULL_TREE, expr);
  3680. }
  3681. /* Don't automatically destroy union members. */
  3682. if (TREE_CODE (current_class_type) == UNION_TYPE)
  3683. return;
  3684. for (member = TYPE_FIELDS (current_class_type); member;
  3685. member = DECL_CHAIN (member))
  3686. {
  3687. tree this_type = TREE_TYPE (member);
  3688. if (this_type == error_mark_node
  3689. || TREE_CODE (member) != FIELD_DECL
  3690. || DECL_ARTIFICIAL (member))
  3691. continue;
  3692. if (ANON_AGGR_TYPE_P (this_type))
  3693. continue;
  3694. if (type_build_dtor_call (this_type))
  3695. {
  3696. tree this_member = (build_class_member_access_expr
  3697. (current_class_ref, member,
  3698. /*access_path=*/NULL_TREE,
  3699. /*preserve_reference=*/false,
  3700. tf_warning_or_error));
  3701. expr = build_delete (this_type, this_member,
  3702. sfk_complete_destructor,
  3703. LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
  3704. 0, tf_warning_or_error);
  3705. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (this_type))
  3706. finish_decl_cleanup (NULL_TREE, expr);
  3707. }
  3708. }
  3709. }
  3710. /* Build a C++ vector delete expression.
  3711. MAXINDEX is the number of elements to be deleted.
  3712. ELT_SIZE is the nominal size of each element in the vector.
  3713. BASE is the expression that should yield the store to be deleted.
  3714. This function expands (or synthesizes) these calls itself.
  3715. AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
  3716. This also calls delete for virtual baseclasses of elements of the vector.
  3717. Update: MAXINDEX is no longer needed. The size can be extracted from the
  3718. start of the vector for pointers, and from the type for arrays. We still
  3719. use MAXINDEX for arrays because it happens to already have one of the
  3720. values we'd have to extract. (We could use MAXINDEX with pointers to
  3721. confirm the size, and trap if the numbers differ; not clear that it'd
  3722. be worth bothering.) */
  3723. tree
  3724. build_vec_delete (tree base, tree maxindex,
  3725. special_function_kind auto_delete_vec,
  3726. int use_global_delete, tsubst_flags_t complain)
  3727. {
  3728. tree type;
  3729. tree rval;
  3730. tree base_init = NULL_TREE;
  3731. type = TREE_TYPE (base);
  3732. if (TYPE_PTR_P (type))
  3733. {
  3734. /* Step back one from start of vector, and read dimension. */
  3735. tree cookie_addr;
  3736. tree size_ptr_type = build_pointer_type (sizetype);
  3737. base = mark_rvalue_use (base);
  3738. if (TREE_SIDE_EFFECTS (base))
  3739. {
  3740. base_init = get_target_expr (base);
  3741. base = TARGET_EXPR_SLOT (base_init);
  3742. }
  3743. type = strip_array_types (TREE_TYPE (type));
  3744. cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
  3745. sizetype, TYPE_SIZE_UNIT (sizetype));
  3746. cookie_addr = fold_build_pointer_plus (fold_convert (size_ptr_type, base),
  3747. cookie_addr);
  3748. maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, complain);
  3749. }
  3750. else if (TREE_CODE (type) == ARRAY_TYPE)
  3751. {
  3752. /* Get the total number of things in the array, maxindex is a
  3753. bad name. */
  3754. maxindex = array_type_nelts_total (type);
  3755. type = strip_array_types (type);
  3756. base = decay_conversion (base, complain);
  3757. if (base == error_mark_node)
  3758. return error_mark_node;
  3759. if (TREE_SIDE_EFFECTS (base))
  3760. {
  3761. base_init = get_target_expr (base);
  3762. base = TARGET_EXPR_SLOT (base_init);
  3763. }
  3764. }
  3765. else
  3766. {
  3767. if (base != error_mark_node && !(complain & tf_error))
  3768. error ("type to vector delete is neither pointer or array type");
  3769. return error_mark_node;
  3770. }
  3771. rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
  3772. use_global_delete, complain);
  3773. if (base_init && rval != error_mark_node)
  3774. rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
  3775. return rval;
  3776. }