constexpr.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612
  1. /* Perform -*- C++ -*- constant expression evaluation, including calls to
  2. constexpr functions. These routines are used both during actual parsing
  3. and during the instantiation of template functions.
  4. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "varasm.h"
  32. #include "cp-tree.h"
  33. #include "c-family/c-objc.h"
  34. #include "tree-iterator.h"
  35. #include "gimplify.h"
  36. #include "builtins.h"
  37. #include "tree-inline.h"
  38. #include "ubsan.h"
  39. static bool verify_constant (tree, bool, bool *, bool *);
  40. #define VERIFY_CONSTANT(X) \
  41. do { \
  42. if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
  43. return t; \
  44. } while (0)
  45. /* Returns true iff FUN is an instantiation of a constexpr function
  46. template or a defaulted constexpr function. */
  47. bool
  48. is_instantiation_of_constexpr (tree fun)
  49. {
  50. return ((DECL_TEMPLOID_INSTANTIATION (fun)
  51. && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
  52. || (DECL_DEFAULTED_FN (fun)
  53. && DECL_DECLARED_CONSTEXPR_P (fun)));
  54. }
  55. /* Return true if T is a literal type. */
  56. bool
  57. literal_type_p (tree t)
  58. {
  59. if (SCALAR_TYPE_P (t)
  60. || TREE_CODE (t) == VECTOR_TYPE
  61. || TREE_CODE (t) == REFERENCE_TYPE
  62. || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
  63. return true;
  64. if (CLASS_TYPE_P (t))
  65. {
  66. t = complete_type (t);
  67. gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
  68. return CLASSTYPE_LITERAL_P (t);
  69. }
  70. if (TREE_CODE (t) == ARRAY_TYPE)
  71. return literal_type_p (strip_array_types (t));
  72. return false;
  73. }
  74. /* If DECL is a variable declared `constexpr', require its type
  75. be literal. Return the DECL if OK, otherwise NULL. */
  76. tree
  77. ensure_literal_type_for_constexpr_object (tree decl)
  78. {
  79. tree type = TREE_TYPE (decl);
  80. if (VAR_P (decl)
  81. && (DECL_DECLARED_CONSTEXPR_P (decl)
  82. || var_in_constexpr_fn (decl))
  83. && !processing_template_decl)
  84. {
  85. tree stype = strip_array_types (type);
  86. if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
  87. /* Don't complain here, we'll complain about incompleteness
  88. when we try to initialize the variable. */;
  89. else if (!literal_type_p (type))
  90. {
  91. if (DECL_DECLARED_CONSTEXPR_P (decl))
  92. {
  93. error ("the type %qT of constexpr variable %qD is not literal",
  94. type, decl);
  95. explain_non_literal_class (type);
  96. }
  97. else
  98. {
  99. if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
  100. {
  101. error ("variable %qD of non-literal type %qT in %<constexpr%> "
  102. "function", decl, type);
  103. explain_non_literal_class (type);
  104. }
  105. cp_function_chain->invalid_constexpr = true;
  106. }
  107. return NULL;
  108. }
  109. }
  110. return decl;
  111. }
  112. /* Representation of entries in the constexpr function definition table. */
  113. struct GTY((for_user)) constexpr_fundef {
  114. tree decl;
  115. tree body;
  116. };
  117. struct constexpr_fundef_hasher : ggc_hasher<constexpr_fundef *>
  118. {
  119. static hashval_t hash (constexpr_fundef *);
  120. static bool equal (constexpr_fundef *, constexpr_fundef *);
  121. };
  122. /* This table holds all constexpr function definitions seen in
  123. the current translation unit. */
  124. static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
  125. /* Utility function used for managing the constexpr function table.
  126. Return true if the entries pointed to by P and Q are for the
  127. same constexpr function. */
  128. inline bool
  129. constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
  130. {
  131. return lhs->decl == rhs->decl;
  132. }
  133. /* Utility function used for managing the constexpr function table.
  134. Return a hash value for the entry pointed to by Q. */
  135. inline hashval_t
  136. constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
  137. {
  138. return DECL_UID (fundef->decl);
  139. }
  140. /* Return a previously saved definition of function FUN. */
  141. static constexpr_fundef *
  142. retrieve_constexpr_fundef (tree fun)
  143. {
  144. constexpr_fundef fundef = { NULL, NULL };
  145. if (constexpr_fundef_table == NULL)
  146. return NULL;
  147. fundef.decl = fun;
  148. return constexpr_fundef_table->find (&fundef);
  149. }
  150. /* Check whether the parameter and return types of FUN are valid for a
  151. constexpr function, and complain if COMPLAIN. */
  152. static bool
  153. is_valid_constexpr_fn (tree fun, bool complain)
  154. {
  155. bool ret = true;
  156. if (DECL_INHERITED_CTOR_BASE (fun)
  157. && TREE_CODE (fun) == TEMPLATE_DECL)
  158. {
  159. ret = false;
  160. if (complain)
  161. error ("inherited constructor %qD is not constexpr",
  162. get_inherited_ctor (fun));
  163. }
  164. else
  165. {
  166. for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
  167. parm != NULL_TREE; parm = TREE_CHAIN (parm))
  168. if (!literal_type_p (TREE_TYPE (parm)))
  169. {
  170. ret = false;
  171. if (complain)
  172. {
  173. error ("invalid type for parameter %d of constexpr "
  174. "function %q+#D", DECL_PARM_INDEX (parm), fun);
  175. explain_non_literal_class (TREE_TYPE (parm));
  176. }
  177. }
  178. }
  179. if (!DECL_CONSTRUCTOR_P (fun))
  180. {
  181. tree rettype = TREE_TYPE (TREE_TYPE (fun));
  182. if (!literal_type_p (rettype))
  183. {
  184. ret = false;
  185. if (complain)
  186. {
  187. error ("invalid return type %qT of constexpr function %q+D",
  188. rettype, fun);
  189. explain_non_literal_class (rettype);
  190. }
  191. }
  192. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
  193. && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
  194. {
  195. ret = false;
  196. if (complain)
  197. {
  198. error ("enclosing class of constexpr non-static member "
  199. "function %q+#D is not a literal type", fun);
  200. explain_non_literal_class (DECL_CONTEXT (fun));
  201. }
  202. }
  203. }
  204. else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
  205. {
  206. ret = false;
  207. if (complain)
  208. error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
  209. }
  210. return ret;
  211. }
  212. /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
  213. for a member of an anonymous aggregate, INIT is the initializer for that
  214. member, and VEC_OUTER is the vector of constructor elements for the class
  215. whose constructor we are processing. Add the initializer to the vector
  216. and return true to indicate success. */
  217. static bool
  218. build_anon_member_initialization (tree member, tree init,
  219. vec<constructor_elt, va_gc> **vec_outer)
  220. {
  221. /* MEMBER presents the relevant fields from the inside out, but we need
  222. to build up the initializer from the outside in so that we can reuse
  223. previously built CONSTRUCTORs if this is, say, the second field in an
  224. anonymous struct. So we use a vec as a stack. */
  225. auto_vec<tree, 2> fields;
  226. do
  227. {
  228. fields.safe_push (TREE_OPERAND (member, 1));
  229. member = TREE_OPERAND (member, 0);
  230. }
  231. while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
  232. && TREE_CODE (member) == COMPONENT_REF);
  233. /* VEC has the constructor elements vector for the context of FIELD.
  234. If FIELD is an anonymous aggregate, we will push inside it. */
  235. vec<constructor_elt, va_gc> **vec = vec_outer;
  236. tree field;
  237. while (field = fields.pop(),
  238. ANON_AGGR_TYPE_P (TREE_TYPE (field)))
  239. {
  240. tree ctor;
  241. /* If there is already an outer constructor entry for the anonymous
  242. aggregate FIELD, use it; otherwise, insert one. */
  243. if (vec_safe_is_empty (*vec)
  244. || (*vec)->last().index != field)
  245. {
  246. ctor = build_constructor (TREE_TYPE (field), NULL);
  247. CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
  248. }
  249. else
  250. ctor = (*vec)->last().value;
  251. vec = &CONSTRUCTOR_ELTS (ctor);
  252. }
  253. /* Now we're at the innermost field, the one that isn't an anonymous
  254. aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
  255. gcc_assert (fields.is_empty());
  256. CONSTRUCTOR_APPEND_ELT (*vec, field, init);
  257. return true;
  258. }
  259. /* Subroutine of build_constexpr_constructor_member_initializers.
  260. The expression tree T represents a data member initialization
  261. in a (constexpr) constructor definition. Build a pairing of
  262. the data member with its initializer, and prepend that pair
  263. to the existing initialization pair INITS. */
  264. static bool
  265. build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
  266. {
  267. tree member, init;
  268. if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
  269. t = TREE_OPERAND (t, 0);
  270. if (TREE_CODE (t) == EXPR_STMT)
  271. t = TREE_OPERAND (t, 0);
  272. if (t == error_mark_node)
  273. return false;
  274. if (TREE_CODE (t) == STATEMENT_LIST)
  275. {
  276. tree_stmt_iterator i;
  277. for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
  278. {
  279. if (! build_data_member_initialization (tsi_stmt (i), vec))
  280. return false;
  281. }
  282. return true;
  283. }
  284. if (TREE_CODE (t) == CLEANUP_STMT)
  285. {
  286. /* We can't see a CLEANUP_STMT in a constructor for a literal class,
  287. but we can in a constexpr constructor for a non-literal class. Just
  288. ignore it; either all the initialization will be constant, in which
  289. case the cleanup can't run, or it can't be constexpr.
  290. Still recurse into CLEANUP_BODY. */
  291. return build_data_member_initialization (CLEANUP_BODY (t), vec);
  292. }
  293. if (TREE_CODE (t) == CONVERT_EXPR)
  294. t = TREE_OPERAND (t, 0);
  295. if (TREE_CODE (t) == INIT_EXPR
  296. /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
  297. use what this function builds for cx_check_missing_mem_inits, and
  298. assignment in the ctor body doesn't count. */
  299. || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
  300. {
  301. member = TREE_OPERAND (t, 0);
  302. init = break_out_target_exprs (TREE_OPERAND (t, 1));
  303. }
  304. else if (TREE_CODE (t) == CALL_EXPR)
  305. {
  306. tree fn = get_callee_fndecl (t);
  307. if (!fn || !DECL_CONSTRUCTOR_P (fn))
  308. /* We're only interested in calls to subobject constructors. */
  309. return true;
  310. member = CALL_EXPR_ARG (t, 0);
  311. /* We don't use build_cplus_new here because it complains about
  312. abstract bases. Leaving the call unwrapped means that it has the
  313. wrong type, but cxx_eval_constant_expression doesn't care. */
  314. init = break_out_target_exprs (t);
  315. }
  316. else if (TREE_CODE (t) == BIND_EXPR)
  317. return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
  318. else
  319. /* Don't add anything else to the CONSTRUCTOR. */
  320. return true;
  321. if (INDIRECT_REF_P (member))
  322. member = TREE_OPERAND (member, 0);
  323. if (TREE_CODE (member) == NOP_EXPR)
  324. {
  325. tree op = member;
  326. STRIP_NOPS (op);
  327. if (TREE_CODE (op) == ADDR_EXPR)
  328. {
  329. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  330. (TREE_TYPE (TREE_TYPE (op)),
  331. TREE_TYPE (TREE_TYPE (member))));
  332. /* Initializing a cv-qualified member; we need to look through
  333. the const_cast. */
  334. member = op;
  335. }
  336. else if (op == current_class_ptr
  337. && (same_type_ignoring_top_level_qualifiers_p
  338. (TREE_TYPE (TREE_TYPE (member)),
  339. current_class_type)))
  340. /* Delegating constructor. */
  341. member = op;
  342. else
  343. {
  344. /* This is an initializer for an empty base; keep it for now so
  345. we can check it in cxx_eval_bare_aggregate. */
  346. gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
  347. }
  348. }
  349. if (TREE_CODE (member) == ADDR_EXPR)
  350. member = TREE_OPERAND (member, 0);
  351. if (TREE_CODE (member) == COMPONENT_REF)
  352. {
  353. tree aggr = TREE_OPERAND (member, 0);
  354. if (TREE_CODE (aggr) != COMPONENT_REF)
  355. /* Normal member initialization. */
  356. member = TREE_OPERAND (member, 1);
  357. else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
  358. /* Initializing a member of an anonymous union. */
  359. return build_anon_member_initialization (member, init, vec);
  360. else
  361. /* We're initializing a vtable pointer in a base. Leave it as
  362. COMPONENT_REF so we remember the path to get to the vfield. */
  363. gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
  364. }
  365. CONSTRUCTOR_APPEND_ELT (*vec, member, init);
  366. return true;
  367. }
  368. /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
  369. In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
  370. BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
  371. static bool
  372. check_constexpr_bind_expr_vars (tree t)
  373. {
  374. gcc_assert (TREE_CODE (t) == BIND_EXPR);
  375. for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
  376. if (TREE_CODE (var) == TYPE_DECL
  377. && DECL_IMPLICIT_TYPEDEF_P (var)
  378. && !LAMBDA_TYPE_P (TREE_TYPE (var)))
  379. return false;
  380. return true;
  381. }
  382. /* Subroutine of check_constexpr_ctor_body. */
  383. static bool
  384. check_constexpr_ctor_body_1 (tree last, tree list)
  385. {
  386. switch (TREE_CODE (list))
  387. {
  388. case DECL_EXPR:
  389. if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
  390. return true;
  391. return false;
  392. case CLEANUP_POINT_EXPR:
  393. return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
  394. /*complain=*/false);
  395. case BIND_EXPR:
  396. if (!check_constexpr_bind_expr_vars (list)
  397. || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
  398. /*complain=*/false))
  399. return false;
  400. return true;
  401. case USING_STMT:
  402. case STATIC_ASSERT:
  403. return true;
  404. default:
  405. return false;
  406. }
  407. }
  408. /* Make sure that there are no statements after LAST in the constructor
  409. body represented by LIST. */
  410. bool
  411. check_constexpr_ctor_body (tree last, tree list, bool complain)
  412. {
  413. /* C++14 doesn't require a constexpr ctor to have an empty body. */
  414. if (cxx_dialect >= cxx14)
  415. return true;
  416. bool ok = true;
  417. if (TREE_CODE (list) == STATEMENT_LIST)
  418. {
  419. tree_stmt_iterator i = tsi_last (list);
  420. for (; !tsi_end_p (i); tsi_prev (&i))
  421. {
  422. tree t = tsi_stmt (i);
  423. if (t == last)
  424. break;
  425. if (!check_constexpr_ctor_body_1 (last, t))
  426. {
  427. ok = false;
  428. break;
  429. }
  430. }
  431. }
  432. else if (list != last
  433. && !check_constexpr_ctor_body_1 (last, list))
  434. ok = false;
  435. if (!ok)
  436. {
  437. if (complain)
  438. error ("constexpr constructor does not have empty body");
  439. DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
  440. }
  441. return ok;
  442. }
  443. /* V is a vector of constructor elements built up for the base and member
  444. initializers of a constructor for TYPE. They need to be in increasing
  445. offset order, which they might not be yet if TYPE has a primary base
  446. which is not first in the base-clause or a vptr and at least one base
  447. all of which are non-primary. */
  448. static vec<constructor_elt, va_gc> *
  449. sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
  450. {
  451. tree pri = CLASSTYPE_PRIMARY_BINFO (type);
  452. tree field_type;
  453. unsigned i;
  454. constructor_elt *ce;
  455. if (pri)
  456. field_type = BINFO_TYPE (pri);
  457. else if (TYPE_CONTAINS_VPTR_P (type))
  458. field_type = vtbl_ptr_type_node;
  459. else
  460. return v;
  461. /* Find the element for the primary base or vptr and move it to the
  462. beginning of the vec. */
  463. for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
  464. if (TREE_TYPE (ce->index) == field_type)
  465. break;
  466. if (i > 0 && i < vec_safe_length (v))
  467. {
  468. vec<constructor_elt, va_gc> &vref = *v;
  469. constructor_elt elt = vref[i];
  470. for (; i > 0; --i)
  471. vref[i] = vref[i-1];
  472. vref[0] = elt;
  473. }
  474. return v;
  475. }
  476. /* Build compile-time evalable representations of member-initializer list
  477. for a constexpr constructor. */
  478. static tree
  479. build_constexpr_constructor_member_initializers (tree type, tree body)
  480. {
  481. vec<constructor_elt, va_gc> *vec = NULL;
  482. bool ok = true;
  483. if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
  484. || TREE_CODE (body) == EH_SPEC_BLOCK)
  485. body = TREE_OPERAND (body, 0);
  486. if (TREE_CODE (body) == STATEMENT_LIST)
  487. {
  488. for (tree_stmt_iterator i = tsi_start (body);
  489. !tsi_end_p (i); tsi_next (&i))
  490. {
  491. body = tsi_stmt (i);
  492. if (TREE_CODE (body) == BIND_EXPR)
  493. break;
  494. }
  495. }
  496. if (TREE_CODE (body) == BIND_EXPR)
  497. body = BIND_EXPR_BODY (body);
  498. if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
  499. {
  500. body = TREE_OPERAND (body, 0);
  501. if (TREE_CODE (body) == EXPR_STMT)
  502. body = TREE_OPERAND (body, 0);
  503. if (TREE_CODE (body) == INIT_EXPR
  504. && (same_type_ignoring_top_level_qualifiers_p
  505. (TREE_TYPE (TREE_OPERAND (body, 0)),
  506. current_class_type)))
  507. {
  508. /* Trivial copy. */
  509. return TREE_OPERAND (body, 1);
  510. }
  511. ok = build_data_member_initialization (body, &vec);
  512. }
  513. else if (TREE_CODE (body) == STATEMENT_LIST)
  514. {
  515. tree_stmt_iterator i;
  516. for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
  517. {
  518. ok = build_data_member_initialization (tsi_stmt (i), &vec);
  519. if (!ok)
  520. break;
  521. }
  522. }
  523. else if (TREE_CODE (body) == TRY_BLOCK)
  524. {
  525. error ("body of %<constexpr%> constructor cannot be "
  526. "a function-try-block");
  527. return error_mark_node;
  528. }
  529. else if (EXPR_P (body))
  530. ok = build_data_member_initialization (body, &vec);
  531. else
  532. gcc_assert (errorcount > 0);
  533. if (ok)
  534. {
  535. if (vec_safe_length (vec) > 0)
  536. {
  537. /* In a delegating constructor, return the target. */
  538. constructor_elt *ce = &(*vec)[0];
  539. if (ce->index == current_class_ptr)
  540. {
  541. body = ce->value;
  542. vec_free (vec);
  543. return body;
  544. }
  545. }
  546. vec = sort_constexpr_mem_initializers (type, vec);
  547. return build_constructor (type, vec);
  548. }
  549. else
  550. return error_mark_node;
  551. }
  552. /* Subroutine of register_constexpr_fundef. BODY is the body of a function
  553. declared to be constexpr, or a sub-statement thereof. Returns the
  554. return value if suitable, error_mark_node for a statement not allowed in
  555. a constexpr function, or NULL_TREE if no return value was found. */
  556. static tree
  557. constexpr_fn_retval (tree body)
  558. {
  559. switch (TREE_CODE (body))
  560. {
  561. case STATEMENT_LIST:
  562. {
  563. tree_stmt_iterator i;
  564. tree expr = NULL_TREE;
  565. for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
  566. {
  567. tree s = constexpr_fn_retval (tsi_stmt (i));
  568. if (s == error_mark_node)
  569. return error_mark_node;
  570. else if (s == NULL_TREE)
  571. /* Keep iterating. */;
  572. else if (expr)
  573. /* Multiple return statements. */
  574. return error_mark_node;
  575. else
  576. expr = s;
  577. }
  578. return expr;
  579. }
  580. case RETURN_EXPR:
  581. return break_out_target_exprs (TREE_OPERAND (body, 0));
  582. case DECL_EXPR:
  583. {
  584. tree decl = DECL_EXPR_DECL (body);
  585. if (TREE_CODE (decl) == USING_DECL
  586. /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
  587. || DECL_ARTIFICIAL (decl))
  588. return NULL_TREE;
  589. return error_mark_node;
  590. }
  591. case CLEANUP_POINT_EXPR:
  592. return constexpr_fn_retval (TREE_OPERAND (body, 0));
  593. case BIND_EXPR:
  594. if (!check_constexpr_bind_expr_vars (body))
  595. return error_mark_node;
  596. return constexpr_fn_retval (BIND_EXPR_BODY (body));
  597. case USING_STMT:
  598. return NULL_TREE;
  599. default:
  600. return error_mark_node;
  601. }
  602. }
  603. /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
  604. FUN; do the necessary transformations to turn it into a single expression
  605. that we can store in the hash table. */
  606. static tree
  607. massage_constexpr_body (tree fun, tree body)
  608. {
  609. if (DECL_CONSTRUCTOR_P (fun))
  610. body = build_constexpr_constructor_member_initializers
  611. (DECL_CONTEXT (fun), body);
  612. else if (cxx_dialect < cxx14)
  613. {
  614. if (TREE_CODE (body) == EH_SPEC_BLOCK)
  615. body = EH_SPEC_STMTS (body);
  616. if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
  617. body = TREE_OPERAND (body, 0);
  618. body = constexpr_fn_retval (body);
  619. }
  620. return body;
  621. }
  622. /* FUN is a constexpr constructor with massaged body BODY. Return true
  623. if some bases/fields are uninitialized, and complain if COMPLAIN. */
  624. static bool
  625. cx_check_missing_mem_inits (tree fun, tree body, bool complain)
  626. {
  627. bool bad;
  628. tree field;
  629. unsigned i, nelts;
  630. tree ctype;
  631. if (TREE_CODE (body) != CONSTRUCTOR)
  632. return false;
  633. nelts = CONSTRUCTOR_NELTS (body);
  634. ctype = DECL_CONTEXT (fun);
  635. field = TYPE_FIELDS (ctype);
  636. if (TREE_CODE (ctype) == UNION_TYPE)
  637. {
  638. if (nelts == 0 && next_initializable_field (field))
  639. {
  640. if (complain)
  641. error ("%<constexpr%> constructor for union %qT must "
  642. "initialize exactly one non-static data member", ctype);
  643. return true;
  644. }
  645. return false;
  646. }
  647. bad = false;
  648. for (i = 0; i <= nelts; ++i)
  649. {
  650. tree index;
  651. if (i == nelts)
  652. index = NULL_TREE;
  653. else
  654. {
  655. index = CONSTRUCTOR_ELT (body, i)->index;
  656. /* Skip base and vtable inits. */
  657. if (TREE_CODE (index) != FIELD_DECL
  658. || DECL_ARTIFICIAL (index))
  659. continue;
  660. }
  661. for (; field != index; field = DECL_CHAIN (field))
  662. {
  663. tree ftype;
  664. if (TREE_CODE (field) != FIELD_DECL
  665. || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
  666. || DECL_ARTIFICIAL (field))
  667. continue;
  668. ftype = strip_array_types (TREE_TYPE (field));
  669. if (type_has_constexpr_default_constructor (ftype))
  670. {
  671. /* It's OK to skip a member with a trivial constexpr ctor.
  672. A constexpr ctor that isn't trivial should have been
  673. added in by now. */
  674. gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
  675. || errorcount != 0);
  676. continue;
  677. }
  678. if (!complain)
  679. return true;
  680. error ("member %qD must be initialized by mem-initializer "
  681. "in %<constexpr%> constructor", field);
  682. inform (DECL_SOURCE_LOCATION (field), "declared here");
  683. bad = true;
  684. }
  685. if (field == NULL_TREE)
  686. break;
  687. field = DECL_CHAIN (field);
  688. }
  689. return bad;
  690. }
  691. /* We are processing the definition of the constexpr function FUN.
  692. Check that its BODY fulfills the propriate requirements and
  693. enter it in the constexpr function definition table.
  694. For constructor BODY is actually the TREE_LIST of the
  695. member-initializer list. */
  696. tree
  697. register_constexpr_fundef (tree fun, tree body)
  698. {
  699. constexpr_fundef entry;
  700. constexpr_fundef **slot;
  701. if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
  702. return NULL;
  703. body = massage_constexpr_body (fun, body);
  704. if (body == NULL_TREE || body == error_mark_node)
  705. {
  706. if (!DECL_CONSTRUCTOR_P (fun))
  707. error ("body of constexpr function %qD not a return-statement", fun);
  708. return NULL;
  709. }
  710. if (!potential_rvalue_constant_expression (body))
  711. {
  712. if (!DECL_GENERATED_P (fun))
  713. require_potential_rvalue_constant_expression (body);
  714. return NULL;
  715. }
  716. if (DECL_CONSTRUCTOR_P (fun)
  717. && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
  718. return NULL;
  719. /* Create the constexpr function table if necessary. */
  720. if (constexpr_fundef_table == NULL)
  721. constexpr_fundef_table
  722. = hash_table<constexpr_fundef_hasher>::create_ggc (101);
  723. entry.decl = fun;
  724. entry.body = body;
  725. slot = constexpr_fundef_table->find_slot (&entry, INSERT);
  726. gcc_assert (*slot == NULL);
  727. *slot = ggc_alloc<constexpr_fundef> ();
  728. **slot = entry;
  729. return fun;
  730. }
  731. /* FUN is a non-constexpr function called in a context that requires a
  732. constant expression. If it comes from a constexpr template, explain why
  733. the instantiation isn't constexpr. */
  734. void
  735. explain_invalid_constexpr_fn (tree fun)
  736. {
  737. static hash_set<tree> *diagnosed;
  738. tree body;
  739. location_t save_loc;
  740. /* Only diagnose defaulted functions or instantiations. */
  741. if (!DECL_DEFAULTED_FN (fun)
  742. && !is_instantiation_of_constexpr (fun))
  743. return;
  744. if (diagnosed == NULL)
  745. diagnosed = new hash_set<tree>;
  746. if (diagnosed->add (fun))
  747. /* Already explained. */
  748. return;
  749. save_loc = input_location;
  750. input_location = DECL_SOURCE_LOCATION (fun);
  751. inform (0, "%q+D is not usable as a constexpr function because:", fun);
  752. /* First check the declaration. */
  753. if (is_valid_constexpr_fn (fun, true))
  754. {
  755. /* Then if it's OK, the body. */
  756. if (!DECL_DECLARED_CONSTEXPR_P (fun))
  757. explain_implicit_non_constexpr (fun);
  758. else
  759. {
  760. body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
  761. require_potential_rvalue_constant_expression (body);
  762. if (DECL_CONSTRUCTOR_P (fun))
  763. cx_check_missing_mem_inits (fun, body, true);
  764. }
  765. }
  766. input_location = save_loc;
  767. }
  768. /* Objects of this type represent calls to constexpr functions
  769. along with the bindings of parameters to their arguments, for
  770. the purpose of compile time evaluation. */
  771. struct GTY((for_user)) constexpr_call {
  772. /* Description of the constexpr function definition. */
  773. constexpr_fundef *fundef;
  774. /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
  775. is a parameter _DECL and the TREE_VALUE is the value of the parameter.
  776. Note: This arrangement is made to accommodate the use of
  777. iterative_hash_template_arg (see pt.c). If you change this
  778. representation, also change the hash calculation in
  779. cxx_eval_call_expression. */
  780. tree bindings;
  781. /* Result of the call.
  782. NULL means the call is being evaluated.
  783. error_mark_node means that the evaluation was erroneous;
  784. otherwise, the actuall value of the call. */
  785. tree result;
  786. /* The hash of this call; we remember it here to avoid having to
  787. recalculate it when expanding the hash table. */
  788. hashval_t hash;
  789. };
  790. struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
  791. {
  792. static hashval_t hash (constexpr_call *);
  793. static bool equal (constexpr_call *, constexpr_call *);
  794. };
  795. /* The constexpr expansion context. CALL is the current function
  796. expansion, CTOR is the current aggregate initializer, OBJECT is the
  797. object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
  798. is a map of values of variables initialized within the expression. */
  799. struct constexpr_ctx {
  800. /* The innermost call we're evaluating. */
  801. constexpr_call *call;
  802. /* Values for any temporaries or local variables within the
  803. constant-expression. */
  804. hash_map<tree,tree> *values;
  805. /* The CONSTRUCTOR we're currently building up for an aggregate
  806. initializer. */
  807. tree ctor;
  808. /* The object we're building the CONSTRUCTOR for. */
  809. tree object;
  810. /* Whether we should error on a non-constant expression or fail quietly. */
  811. bool quiet;
  812. /* Whether we are strictly conforming to constant expression rules or
  813. trying harder to get a constant value. */
  814. bool strict;
  815. };
  816. /* A table of all constexpr calls that have been evaluated by the
  817. compiler in this translation unit. */
  818. static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
  819. static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
  820. bool, bool *, bool *, tree * = NULL);
  821. /* Compute a hash value for a constexpr call representation. */
  822. inline hashval_t
  823. constexpr_call_hasher::hash (constexpr_call *info)
  824. {
  825. return info->hash;
  826. }
  827. /* Return true if the objects pointed to by P and Q represent calls
  828. to the same constexpr function with the same arguments.
  829. Otherwise, return false. */
  830. bool
  831. constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
  832. {
  833. tree lhs_bindings;
  834. tree rhs_bindings;
  835. if (lhs == rhs)
  836. return 1;
  837. if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
  838. return 0;
  839. lhs_bindings = lhs->bindings;
  840. rhs_bindings = rhs->bindings;
  841. while (lhs_bindings != NULL && rhs_bindings != NULL)
  842. {
  843. tree lhs_arg = TREE_VALUE (lhs_bindings);
  844. tree rhs_arg = TREE_VALUE (rhs_bindings);
  845. gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
  846. if (!cp_tree_equal (lhs_arg, rhs_arg))
  847. return 0;
  848. lhs_bindings = TREE_CHAIN (lhs_bindings);
  849. rhs_bindings = TREE_CHAIN (rhs_bindings);
  850. }
  851. return lhs_bindings == rhs_bindings;
  852. }
  853. /* Initialize the constexpr call table, if needed. */
  854. static void
  855. maybe_initialize_constexpr_call_table (void)
  856. {
  857. if (constexpr_call_table == NULL)
  858. constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
  859. }
  860. /* We have an expression tree T that represents a call, either CALL_EXPR
  861. or AGGR_INIT_EXPR. If the call is lexically to a named function,
  862. retrun the _DECL for that function. */
  863. static tree
  864. get_function_named_in_call (tree t)
  865. {
  866. tree fun = NULL;
  867. switch (TREE_CODE (t))
  868. {
  869. case CALL_EXPR:
  870. fun = CALL_EXPR_FN (t);
  871. break;
  872. case AGGR_INIT_EXPR:
  873. fun = AGGR_INIT_EXPR_FN (t);
  874. break;
  875. default:
  876. gcc_unreachable();
  877. break;
  878. }
  879. if (fun && TREE_CODE (fun) == ADDR_EXPR
  880. && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
  881. fun = TREE_OPERAND (fun, 0);
  882. return fun;
  883. }
  884. /* We have an expression tree T that represents a call, either CALL_EXPR
  885. or AGGR_INIT_EXPR. Return the Nth argument. */
  886. static inline tree
  887. get_nth_callarg (tree t, int n)
  888. {
  889. switch (TREE_CODE (t))
  890. {
  891. case CALL_EXPR:
  892. return CALL_EXPR_ARG (t, n);
  893. case AGGR_INIT_EXPR:
  894. return AGGR_INIT_EXPR_ARG (t, n);
  895. default:
  896. gcc_unreachable ();
  897. return NULL;
  898. }
  899. }
  900. /* Look up the binding of the function parameter T in a constexpr
  901. function call context CALL. */
  902. static tree
  903. lookup_parameter_binding (const constexpr_call *call, tree t)
  904. {
  905. tree b = purpose_member (t, call->bindings);
  906. return TREE_VALUE (b);
  907. }
  908. /* Attempt to evaluate T which represents a call to a builtin function.
  909. We assume here that all builtin functions evaluate to scalar types
  910. represented by _CST nodes. */
  911. static tree
  912. cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t,
  913. bool lval,
  914. bool *non_constant_p, bool *overflow_p)
  915. {
  916. const int nargs = call_expr_nargs (t);
  917. tree *args = (tree *) alloca (nargs * sizeof (tree));
  918. tree new_call;
  919. int i;
  920. for (i = 0; i < nargs; ++i)
  921. {
  922. args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
  923. lval,
  924. non_constant_p, overflow_p);
  925. if (ctx->quiet && *non_constant_p)
  926. return t;
  927. }
  928. if (*non_constant_p)
  929. return t;
  930. new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
  931. CALL_EXPR_FN (t), nargs, args);
  932. VERIFY_CONSTANT (new_call);
  933. return new_call;
  934. }
  935. /* TEMP is the constant value of a temporary object of type TYPE. Adjust
  936. the type of the value to match. */
  937. static tree
  938. adjust_temp_type (tree type, tree temp)
  939. {
  940. if (TREE_TYPE (temp) == type)
  941. return temp;
  942. /* Avoid wrapping an aggregate value in a NOP_EXPR. */
  943. if (TREE_CODE (temp) == CONSTRUCTOR)
  944. return build_constructor (type, CONSTRUCTOR_ELTS (temp));
  945. gcc_assert (scalarish_type_p (type));
  946. return cp_fold_convert (type, temp);
  947. }
  948. /* True if we want to use the new handling of constexpr calls based on
  949. DECL_SAVED_TREE. */
  950. #define use_new_call true
  951. /* Subroutine of cxx_eval_call_expression.
  952. We are processing a call expression (either CALL_EXPR or
  953. AGGR_INIT_EXPR) in the context of CTX. Evaluate
  954. all arguments and bind their values to correspondings
  955. parameters, making up the NEW_CALL context. */
  956. static void
  957. cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
  958. constexpr_call *new_call,
  959. bool *non_constant_p, bool *overflow_p,
  960. bool *non_constant_args)
  961. {
  962. const int nargs = call_expr_nargs (t);
  963. tree fun = new_call->fundef->decl;
  964. tree parms = DECL_ARGUMENTS (fun);
  965. int i;
  966. tree *p = &new_call->bindings;
  967. for (i = 0; i < nargs; ++i)
  968. {
  969. tree x, arg;
  970. tree type = parms ? TREE_TYPE (parms) : void_type_node;
  971. x = get_nth_callarg (t, i);
  972. /* For member function, the first argument is a pointer to the implied
  973. object. For a constructor, it might still be a dummy object, in
  974. which case we get the real argument from ctx. */
  975. if (i == 0 && DECL_CONSTRUCTOR_P (fun)
  976. && is_dummy_object (x))
  977. {
  978. x = ctx->object;
  979. x = cp_build_addr_expr (x, tf_warning_or_error);
  980. }
  981. bool lval = false;
  982. if (parms && DECL_BY_REFERENCE (parms) && !use_new_call)
  983. {
  984. /* cp_genericize made this a reference for argument passing, but
  985. we don't want to treat it like one for C++11 constexpr
  986. evaluation. C++14 constexpr evaluation uses the genericized
  987. DECL_SAVED_TREE. */
  988. gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
  989. gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
  990. type = TREE_TYPE (type);
  991. x = convert_from_reference (x);
  992. lval = true;
  993. }
  994. arg = cxx_eval_constant_expression (ctx, x, lval,
  995. non_constant_p, overflow_p);
  996. /* Don't VERIFY_CONSTANT here. */
  997. if (*non_constant_p && ctx->quiet)
  998. return;
  999. /* Just discard ellipsis args after checking their constantitude. */
  1000. if (!parms)
  1001. continue;
  1002. if (*non_constant_p)
  1003. /* Don't try to adjust the type of non-constant args. */
  1004. goto next;
  1005. /* Make sure the binding has the same type as the parm. */
  1006. if (TREE_CODE (type) != REFERENCE_TYPE)
  1007. arg = adjust_temp_type (type, arg);
  1008. if (!TREE_CONSTANT (arg))
  1009. *non_constant_args = true;
  1010. *p = build_tree_list (parms, arg);
  1011. p = &TREE_CHAIN (*p);
  1012. next:
  1013. parms = TREE_CHAIN (parms);
  1014. }
  1015. }
  1016. /* Variables and functions to manage constexpr call expansion context.
  1017. These do not need to be marked for PCH or GC. */
  1018. /* FIXME remember and print actual constant arguments. */
  1019. static vec<tree> call_stack = vNULL;
  1020. static int call_stack_tick;
  1021. static int last_cx_error_tick;
  1022. static bool
  1023. push_cx_call_context (tree call)
  1024. {
  1025. ++call_stack_tick;
  1026. if (!EXPR_HAS_LOCATION (call))
  1027. SET_EXPR_LOCATION (call, input_location);
  1028. call_stack.safe_push (call);
  1029. if (call_stack.length () > (unsigned) max_constexpr_depth)
  1030. return false;
  1031. return true;
  1032. }
  1033. static void
  1034. pop_cx_call_context (void)
  1035. {
  1036. ++call_stack_tick;
  1037. call_stack.pop ();
  1038. }
  1039. vec<tree>
  1040. cx_error_context (void)
  1041. {
  1042. vec<tree> r = vNULL;
  1043. if (call_stack_tick != last_cx_error_tick
  1044. && !call_stack.is_empty ())
  1045. r = call_stack;
  1046. last_cx_error_tick = call_stack_tick;
  1047. return r;
  1048. }
  1049. /* Subroutine of cxx_eval_constant_expression.
  1050. Evaluate the call expression tree T in the context of OLD_CALL expression
  1051. evaluation. */
  1052. static tree
  1053. cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
  1054. bool lval,
  1055. bool *non_constant_p, bool *overflow_p)
  1056. {
  1057. location_t loc = EXPR_LOC_OR_LOC (t, input_location);
  1058. tree fun = get_function_named_in_call (t);
  1059. constexpr_call new_call = { NULL, NULL, NULL, 0 };
  1060. bool depth_ok;
  1061. if (fun == NULL_TREE)
  1062. switch (CALL_EXPR_IFN (t))
  1063. {
  1064. case IFN_UBSAN_NULL:
  1065. case IFN_UBSAN_BOUNDS:
  1066. case IFN_UBSAN_VPTR:
  1067. return void_node;
  1068. default:
  1069. if (!ctx->quiet)
  1070. error_at (loc, "call to internal function");
  1071. *non_constant_p = true;
  1072. return t;
  1073. }
  1074. if (TREE_CODE (fun) != FUNCTION_DECL)
  1075. {
  1076. /* Might be a constexpr function pointer. */
  1077. fun = cxx_eval_constant_expression (ctx, fun,
  1078. /*lval*/false, non_constant_p,
  1079. overflow_p);
  1080. STRIP_NOPS (fun);
  1081. if (TREE_CODE (fun) == ADDR_EXPR)
  1082. fun = TREE_OPERAND (fun, 0);
  1083. }
  1084. if (TREE_CODE (fun) != FUNCTION_DECL)
  1085. {
  1086. if (!ctx->quiet && !*non_constant_p)
  1087. error_at (loc, "expression %qE does not designate a constexpr "
  1088. "function", fun);
  1089. *non_constant_p = true;
  1090. return t;
  1091. }
  1092. if (DECL_CLONED_FUNCTION_P (fun))
  1093. fun = DECL_CLONED_FUNCTION (fun);
  1094. if (is_ubsan_builtin_p (fun))
  1095. return void_node;
  1096. if (is_builtin_fn (fun))
  1097. return cxx_eval_builtin_function_call (ctx, t,
  1098. lval, non_constant_p, overflow_p);
  1099. if (!DECL_DECLARED_CONSTEXPR_P (fun))
  1100. {
  1101. if (!ctx->quiet)
  1102. {
  1103. error_at (loc, "call to non-constexpr function %qD", fun);
  1104. explain_invalid_constexpr_fn (fun);
  1105. }
  1106. *non_constant_p = true;
  1107. return t;
  1108. }
  1109. /* Shortcut trivial constructor/op=. */
  1110. if (trivial_fn_p (fun))
  1111. {
  1112. if (call_expr_nargs (t) == 2)
  1113. {
  1114. tree arg = convert_from_reference (get_nth_callarg (t, 1));
  1115. return cxx_eval_constant_expression (ctx, arg,
  1116. lval, non_constant_p,
  1117. overflow_p);
  1118. }
  1119. else if (TREE_CODE (t) == AGGR_INIT_EXPR
  1120. && AGGR_INIT_ZERO_FIRST (t))
  1121. return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
  1122. }
  1123. /* We can't defer instantiating the function any longer. */
  1124. if (!DECL_INITIAL (fun)
  1125. && DECL_TEMPLOID_INSTANTIATION (fun))
  1126. {
  1127. ++function_depth;
  1128. instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
  1129. --function_depth;
  1130. }
  1131. /* If in direct recursive call, optimize definition search. */
  1132. if (ctx && ctx->call && ctx->call->fundef->decl == fun)
  1133. new_call.fundef = ctx->call->fundef;
  1134. else
  1135. {
  1136. new_call.fundef = retrieve_constexpr_fundef (fun);
  1137. if (new_call.fundef == NULL || new_call.fundef->body == NULL)
  1138. {
  1139. if (!ctx->quiet)
  1140. {
  1141. if (DECL_INITIAL (fun))
  1142. {
  1143. /* The definition of fun was somehow unsuitable. */
  1144. error_at (loc, "%qD called in a constant expression", fun);
  1145. explain_invalid_constexpr_fn (fun);
  1146. }
  1147. else
  1148. error_at (loc, "%qD used before its definition", fun);
  1149. }
  1150. *non_constant_p = true;
  1151. return t;
  1152. }
  1153. }
  1154. constexpr_ctx new_ctx = *ctx;
  1155. if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
  1156. && TREE_CODE (t) == AGGR_INIT_EXPR)
  1157. {
  1158. /* We want to have an initialization target for an AGGR_INIT_EXPR.
  1159. If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
  1160. new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
  1161. tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
  1162. CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
  1163. ctx->values->put (new_ctx.object, ctor);
  1164. ctx = &new_ctx;
  1165. }
  1166. bool non_constant_args = false;
  1167. cxx_bind_parameters_in_call (ctx, t, &new_call,
  1168. non_constant_p, overflow_p, &non_constant_args);
  1169. if (*non_constant_p)
  1170. return t;
  1171. depth_ok = push_cx_call_context (t);
  1172. tree result = NULL_TREE;
  1173. constexpr_call *entry = NULL;
  1174. if (!non_constant_args)
  1175. {
  1176. new_call.hash = iterative_hash_template_arg
  1177. (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
  1178. /* If we have seen this call before, we are done. */
  1179. maybe_initialize_constexpr_call_table ();
  1180. constexpr_call **slot
  1181. = constexpr_call_table->find_slot (&new_call, INSERT);
  1182. entry = *slot;
  1183. if (entry == NULL)
  1184. {
  1185. /* We need to keep a pointer to the entry, not just the slot, as the
  1186. slot can move in the call to cxx_eval_builtin_function_call. */
  1187. *slot = entry = ggc_alloc<constexpr_call> ();
  1188. *entry = new_call;
  1189. }
  1190. /* Calls which are in progress have their result set to NULL
  1191. so that we can detect circular dependencies. */
  1192. else if (entry->result == NULL)
  1193. {
  1194. if (!ctx->quiet)
  1195. error ("call has circular dependency");
  1196. *non_constant_p = true;
  1197. entry->result = result = error_mark_node;
  1198. }
  1199. else
  1200. result = entry->result;
  1201. }
  1202. if (!depth_ok)
  1203. {
  1204. if (!ctx->quiet)
  1205. error ("constexpr evaluation depth exceeds maximum of %d (use "
  1206. "-fconstexpr-depth= to increase the maximum)",
  1207. max_constexpr_depth);
  1208. *non_constant_p = true;
  1209. result = error_mark_node;
  1210. }
  1211. else
  1212. {
  1213. if (!result || result == error_mark_node)
  1214. {
  1215. if (!use_new_call)
  1216. {
  1217. new_ctx.call = &new_call;
  1218. result = (cxx_eval_constant_expression
  1219. (&new_ctx, new_call.fundef->body,
  1220. lval,
  1221. non_constant_p, overflow_p));
  1222. }
  1223. else
  1224. {
  1225. if (DECL_SAVED_TREE (fun) == NULL_TREE
  1226. && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
  1227. /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
  1228. cleared, try a clone. */
  1229. for (fun = DECL_CHAIN (fun);
  1230. fun && DECL_CLONED_FUNCTION_P (fun);
  1231. fun = DECL_CHAIN (fun))
  1232. if (DECL_SAVED_TREE (fun))
  1233. break;
  1234. if (!DECL_SAVED_TREE (fun))
  1235. {
  1236. /* cgraph/gimplification have released the DECL_SAVED_TREE
  1237. for this function. Fail gracefully. */
  1238. gcc_assert (ctx->quiet);
  1239. *non_constant_p = true;
  1240. return t;
  1241. }
  1242. tree parms, res;
  1243. /* Unshare the whole function body. */
  1244. tree body = copy_fn (fun, parms, res);
  1245. /* Associate the bindings with the remapped parms. */
  1246. tree bound = new_call.bindings;
  1247. tree remapped = parms;
  1248. while (bound)
  1249. {
  1250. tree oparm = TREE_PURPOSE (bound);
  1251. tree arg = TREE_VALUE (bound);
  1252. gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
  1253. ctx->values->put (remapped, arg);
  1254. bound = TREE_CHAIN (bound);
  1255. remapped = DECL_CHAIN (remapped);
  1256. }
  1257. /* Add the RESULT_DECL to the values map, too. */
  1258. tree slot = NULL_TREE;
  1259. if (DECL_BY_REFERENCE (res))
  1260. {
  1261. slot = AGGR_INIT_EXPR_SLOT (t);
  1262. tree addr = build_address (slot);
  1263. addr = build_nop (TREE_TYPE (res), addr);
  1264. ctx->values->put (res, addr);
  1265. ctx->values->put (slot, NULL_TREE);
  1266. }
  1267. else
  1268. ctx->values->put (res, NULL_TREE);
  1269. tree jump_target = NULL_TREE;
  1270. cxx_eval_constant_expression (ctx, body,
  1271. lval, non_constant_p, overflow_p,
  1272. &jump_target);
  1273. if (DECL_CONSTRUCTOR_P (fun))
  1274. /* This can be null for a subobject constructor call, in
  1275. which case what we care about is the initialization
  1276. side-effects rather than the value. We could get at the
  1277. value by evaluating *this, but we don't bother; there's
  1278. no need to put such a call in the hash table. */
  1279. result = lval ? ctx->object : ctx->ctor;
  1280. else if (VOID_TYPE_P (TREE_TYPE (res)))
  1281. result = void_node;
  1282. else
  1283. {
  1284. result = *ctx->values->get (slot ? slot : res);
  1285. if (result == NULL_TREE && !*non_constant_p)
  1286. {
  1287. if (!ctx->quiet)
  1288. error ("constexpr call flows off the end "
  1289. "of the function");
  1290. *non_constant_p = true;
  1291. }
  1292. }
  1293. /* Remove the parms/result from the values map. Is it worth
  1294. bothering to do this when the map itself is only live for
  1295. one constexpr evaluation? If so, maybe also clear out
  1296. other vars from call, maybe in BIND_EXPR handling? */
  1297. ctx->values->remove (res);
  1298. if (slot)
  1299. ctx->values->remove (slot);
  1300. for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
  1301. ctx->values->remove (parm);
  1302. }
  1303. }
  1304. if (result == error_mark_node)
  1305. *non_constant_p = true;
  1306. if (*non_constant_p)
  1307. result = error_mark_node;
  1308. else if (result)
  1309. {
  1310. /* If this was a call to initialize an object, set the type of
  1311. the CONSTRUCTOR to the type of that object. */
  1312. if (DECL_CONSTRUCTOR_P (fun) && !use_new_call)
  1313. {
  1314. tree ob_arg = get_nth_callarg (t, 0);
  1315. STRIP_NOPS (ob_arg);
  1316. gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
  1317. && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
  1318. result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
  1319. result);
  1320. }
  1321. }
  1322. else
  1323. result = void_node;
  1324. if (entry)
  1325. entry->result = result;
  1326. }
  1327. pop_cx_call_context ();
  1328. return unshare_expr (result);
  1329. }
  1330. /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
  1331. bool
  1332. reduced_constant_expression_p (tree t)
  1333. {
  1334. switch (TREE_CODE (t))
  1335. {
  1336. case PTRMEM_CST:
  1337. /* Even if we can't lower this yet, it's constant. */
  1338. return true;
  1339. case CONSTRUCTOR:
  1340. /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
  1341. tree elt; unsigned HOST_WIDE_INT idx;
  1342. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
  1343. if (!reduced_constant_expression_p (elt))
  1344. return false;
  1345. return true;
  1346. default:
  1347. /* FIXME are we calling this too much? */
  1348. return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
  1349. }
  1350. }
  1351. /* Some expressions may have constant operands but are not constant
  1352. themselves, such as 1/0. Call this function (or rather, the macro
  1353. following it) to check for that condition.
  1354. We only call this in places that require an arithmetic constant, not in
  1355. places where we might have a non-constant expression that can be a
  1356. component of a constant expression, such as the address of a constexpr
  1357. variable that might be dereferenced later. */
  1358. static bool
  1359. verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
  1360. bool *overflow_p)
  1361. {
  1362. if (!*non_constant_p && !reduced_constant_expression_p (t))
  1363. {
  1364. if (!allow_non_constant)
  1365. error ("%q+E is not a constant expression", t);
  1366. *non_constant_p = true;
  1367. }
  1368. if (TREE_OVERFLOW_P (t))
  1369. {
  1370. if (!allow_non_constant)
  1371. {
  1372. permerror (input_location, "overflow in constant expression");
  1373. /* If we're being permissive (and are in an enforcing
  1374. context), ignore the overflow. */
  1375. if (flag_permissive)
  1376. return *non_constant_p;
  1377. }
  1378. *overflow_p = true;
  1379. }
  1380. return *non_constant_p;
  1381. }
  1382. /* Check whether the shift operation with code CODE and type TYPE on LHS
  1383. and RHS is undefined. If it is, give an error with an explanation,
  1384. and return true; return false otherwise. */
  1385. static bool
  1386. cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
  1387. enum tree_code code, tree type, tree lhs, tree rhs)
  1388. {
  1389. if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
  1390. || TREE_CODE (lhs) != INTEGER_CST
  1391. || TREE_CODE (rhs) != INTEGER_CST)
  1392. return false;
  1393. tree lhstype = TREE_TYPE (lhs);
  1394. unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
  1395. /* [expr.shift] The behavior is undefined if the right operand
  1396. is negative, or greater than or equal to the length in bits
  1397. of the promoted left operand. */
  1398. if (tree_int_cst_sgn (rhs) == -1)
  1399. {
  1400. if (!ctx->quiet)
  1401. error_at (loc, "right operand of shift expression %q+E is negative",
  1402. build2_loc (loc, code, type, lhs, rhs));
  1403. return true;
  1404. }
  1405. if (compare_tree_int (rhs, uprec) >= 0)
  1406. {
  1407. if (!ctx->quiet)
  1408. error_at (loc, "right operand of shift expression %q+E is >= than "
  1409. "the precision of the left operand",
  1410. build2_loc (loc, code, type, lhs, rhs));
  1411. return true;
  1412. }
  1413. /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
  1414. if E1 has a signed type and non-negative value, and E1x2^E2 is
  1415. representable in the corresponding unsigned type of the result type,
  1416. then that value, converted to the result type, is the resulting value;
  1417. otherwise, the behavior is undefined. */
  1418. if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
  1419. && (cxx_dialect >= cxx11))
  1420. {
  1421. if (tree_int_cst_sgn (lhs) == -1)
  1422. {
  1423. if (!ctx->quiet)
  1424. error_at (loc, "left operand of shift expression %q+E is negative",
  1425. build2_loc (loc, code, type, lhs, rhs));
  1426. return true;
  1427. }
  1428. /* For signed x << y the following:
  1429. (unsigned) x >> ((prec (lhs) - 1) - y)
  1430. if > 1, is undefined. The right-hand side of this formula
  1431. is the highest bit of the LHS that can be set (starting from 0),
  1432. so that the shift doesn't overflow. We then right-shift the LHS
  1433. to see whether any other bit is set making the original shift
  1434. undefined -- the result is not representable in the corresponding
  1435. unsigned type. */
  1436. tree t = build_int_cst (unsigned_type_node, uprec - 1);
  1437. t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
  1438. tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
  1439. t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
  1440. if (tree_int_cst_lt (integer_one_node, t))
  1441. {
  1442. if (!ctx->quiet)
  1443. error_at (loc, "shift expression %q+E overflows",
  1444. build2_loc (loc, code, type, lhs, rhs));
  1445. return true;
  1446. }
  1447. }
  1448. return false;
  1449. }
  1450. /* Subroutine of cxx_eval_constant_expression.
  1451. Attempt to reduce the unary expression tree T to a compile time value.
  1452. If successful, return the value. Otherwise issue a diagnostic
  1453. and return error_mark_node. */
  1454. static tree
  1455. cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
  1456. bool /*lval*/,
  1457. bool *non_constant_p, bool *overflow_p)
  1458. {
  1459. tree r;
  1460. tree orig_arg = TREE_OPERAND (t, 0);
  1461. tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
  1462. non_constant_p, overflow_p);
  1463. VERIFY_CONSTANT (arg);
  1464. location_t loc = EXPR_LOCATION (t);
  1465. enum tree_code code = TREE_CODE (t);
  1466. tree type = TREE_TYPE (t);
  1467. r = fold_unary_loc (loc, code, type, arg);
  1468. if (r == NULL_TREE)
  1469. {
  1470. if (arg == orig_arg)
  1471. r = t;
  1472. else
  1473. r = build1_loc (loc, code, type, arg);
  1474. }
  1475. VERIFY_CONSTANT (r);
  1476. return r;
  1477. }
  1478. /* Subroutine of cxx_eval_constant_expression.
  1479. Like cxx_eval_unary_expression, except for binary expressions. */
  1480. static tree
  1481. cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
  1482. bool /*lval*/,
  1483. bool *non_constant_p, bool *overflow_p)
  1484. {
  1485. tree r;
  1486. tree orig_lhs = TREE_OPERAND (t, 0);
  1487. tree orig_rhs = TREE_OPERAND (t, 1);
  1488. tree lhs, rhs;
  1489. lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
  1490. non_constant_p, overflow_p);
  1491. /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
  1492. a local array in a constexpr function. */
  1493. bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
  1494. if (!ptr)
  1495. VERIFY_CONSTANT (lhs);
  1496. rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
  1497. non_constant_p, overflow_p);
  1498. if (!ptr)
  1499. VERIFY_CONSTANT (rhs);
  1500. location_t loc = EXPR_LOCATION (t);
  1501. enum tree_code code = TREE_CODE (t);
  1502. tree type = TREE_TYPE (t);
  1503. r = fold_binary_loc (loc, code, type, lhs, rhs);
  1504. if (r == NULL_TREE)
  1505. {
  1506. if (lhs == orig_lhs && rhs == orig_rhs)
  1507. r = t;
  1508. else
  1509. r = build2_loc (loc, code, type, lhs, rhs);
  1510. }
  1511. else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
  1512. *non_constant_p = true;
  1513. if (!ptr)
  1514. VERIFY_CONSTANT (r);
  1515. return r;
  1516. }
  1517. /* Subroutine of cxx_eval_constant_expression.
  1518. Attempt to evaluate condition expressions. Dead branches are not
  1519. looked into. */
  1520. static tree
  1521. cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
  1522. bool lval,
  1523. bool *non_constant_p, bool *overflow_p,
  1524. tree *jump_target)
  1525. {
  1526. tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
  1527. /*lval*/false,
  1528. non_constant_p, overflow_p);
  1529. VERIFY_CONSTANT (val);
  1530. /* Don't VERIFY_CONSTANT the other operands. */
  1531. if (integer_zerop (val))
  1532. return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
  1533. lval,
  1534. non_constant_p, overflow_p,
  1535. jump_target);
  1536. return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
  1537. lval,
  1538. non_constant_p, overflow_p,
  1539. jump_target);
  1540. }
  1541. /* Subroutine of cxx_eval_constant_expression.
  1542. Attempt to reduce a reference to an array slot. */
  1543. static tree
  1544. cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
  1545. bool lval,
  1546. bool *non_constant_p, bool *overflow_p)
  1547. {
  1548. tree oldary = TREE_OPERAND (t, 0);
  1549. tree ary = cxx_eval_constant_expression (ctx, oldary,
  1550. lval,
  1551. non_constant_p, overflow_p);
  1552. tree index, oldidx;
  1553. HOST_WIDE_INT i;
  1554. tree elem_type;
  1555. unsigned len, elem_nchars = 1;
  1556. if (*non_constant_p)
  1557. return t;
  1558. oldidx = TREE_OPERAND (t, 1);
  1559. index = cxx_eval_constant_expression (ctx, oldidx,
  1560. false,
  1561. non_constant_p, overflow_p);
  1562. VERIFY_CONSTANT (index);
  1563. if (lval && ary == oldary && index == oldidx)
  1564. return t;
  1565. else if (lval)
  1566. return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
  1567. elem_type = TREE_TYPE (TREE_TYPE (ary));
  1568. if (TREE_CODE (ary) == CONSTRUCTOR)
  1569. len = CONSTRUCTOR_NELTS (ary);
  1570. else if (TREE_CODE (ary) == STRING_CST)
  1571. {
  1572. elem_nchars = (TYPE_PRECISION (elem_type)
  1573. / TYPE_PRECISION (char_type_node));
  1574. len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
  1575. }
  1576. else
  1577. {
  1578. /* We can't do anything with other tree codes, so use
  1579. VERIFY_CONSTANT to complain and fail. */
  1580. VERIFY_CONSTANT (ary);
  1581. gcc_unreachable ();
  1582. }
  1583. if (compare_tree_int (index, len) >= 0)
  1584. {
  1585. if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
  1586. {
  1587. /* If it's within the array bounds but doesn't have an explicit
  1588. initializer, it's value-initialized. */
  1589. tree val = build_value_init (elem_type, tf_warning_or_error);
  1590. return cxx_eval_constant_expression (ctx, val,
  1591. lval,
  1592. non_constant_p, overflow_p);
  1593. }
  1594. if (!ctx->quiet)
  1595. error ("array subscript out of bound");
  1596. *non_constant_p = true;
  1597. return t;
  1598. }
  1599. else if (tree_int_cst_lt (index, integer_zero_node))
  1600. {
  1601. if (!ctx->quiet)
  1602. error ("negative array subscript");
  1603. *non_constant_p = true;
  1604. return t;
  1605. }
  1606. i = tree_to_shwi (index);
  1607. if (TREE_CODE (ary) == CONSTRUCTOR)
  1608. return (*CONSTRUCTOR_ELTS (ary))[i].value;
  1609. else if (elem_nchars == 1)
  1610. return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
  1611. TREE_STRING_POINTER (ary)[i]);
  1612. else
  1613. {
  1614. tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
  1615. return native_interpret_expr (type, (const unsigned char *)
  1616. TREE_STRING_POINTER (ary)
  1617. + i * elem_nchars, elem_nchars);
  1618. }
  1619. /* Don't VERIFY_CONSTANT here. */
  1620. }
  1621. /* Subroutine of cxx_eval_constant_expression.
  1622. Attempt to reduce a field access of a value of class type. */
  1623. static tree
  1624. cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
  1625. bool lval,
  1626. bool *non_constant_p, bool *overflow_p)
  1627. {
  1628. unsigned HOST_WIDE_INT i;
  1629. tree field;
  1630. tree value;
  1631. tree part = TREE_OPERAND (t, 1);
  1632. tree orig_whole = TREE_OPERAND (t, 0);
  1633. tree whole = cxx_eval_constant_expression (ctx, orig_whole,
  1634. lval,
  1635. non_constant_p, overflow_p);
  1636. if (whole == orig_whole)
  1637. return t;
  1638. if (lval)
  1639. return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
  1640. whole, part, NULL_TREE);
  1641. /* Don't VERIFY_CONSTANT here; we only want to check that we got a
  1642. CONSTRUCTOR. */
  1643. if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
  1644. {
  1645. if (!ctx->quiet)
  1646. error ("%qE is not a constant expression", orig_whole);
  1647. *non_constant_p = true;
  1648. }
  1649. if (DECL_MUTABLE_P (part))
  1650. {
  1651. if (!ctx->quiet)
  1652. error ("mutable %qD is not usable in a constant expression", part);
  1653. *non_constant_p = true;
  1654. }
  1655. if (*non_constant_p)
  1656. return t;
  1657. FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
  1658. {
  1659. if (field == part)
  1660. {
  1661. if (value)
  1662. return value;
  1663. else
  1664. /* We're in the middle of initializing it. */
  1665. break;
  1666. }
  1667. }
  1668. if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
  1669. && CONSTRUCTOR_NELTS (whole) > 0)
  1670. {
  1671. /* DR 1188 says we don't have to deal with this. */
  1672. if (!ctx->quiet)
  1673. error ("accessing %qD member instead of initialized %qD member in "
  1674. "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
  1675. *non_constant_p = true;
  1676. return t;
  1677. }
  1678. if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
  1679. {
  1680. /* 'whole' is part of the aggregate initializer we're currently
  1681. building; if there's no initializer for this member yet, that's an
  1682. error. */
  1683. if (!ctx->quiet)
  1684. error ("accessing uninitialized member %qD", part);
  1685. *non_constant_p = true;
  1686. return t;
  1687. }
  1688. /* If there's no explicit init for this field, it's value-initialized. */
  1689. value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
  1690. return cxx_eval_constant_expression (ctx, value,
  1691. lval,
  1692. non_constant_p, overflow_p);
  1693. }
  1694. /* Subroutine of cxx_eval_constant_expression.
  1695. Attempt to reduce a field access of a value of class type that is
  1696. expressed as a BIT_FIELD_REF. */
  1697. static tree
  1698. cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
  1699. bool lval,
  1700. bool *non_constant_p, bool *overflow_p)
  1701. {
  1702. tree orig_whole = TREE_OPERAND (t, 0);
  1703. tree retval, fldval, utype, mask;
  1704. bool fld_seen = false;
  1705. HOST_WIDE_INT istart, isize;
  1706. tree whole = cxx_eval_constant_expression (ctx, orig_whole,
  1707. lval,
  1708. non_constant_p, overflow_p);
  1709. tree start, field, value;
  1710. unsigned HOST_WIDE_INT i;
  1711. if (whole == orig_whole)
  1712. return t;
  1713. /* Don't VERIFY_CONSTANT here; we only want to check that we got a
  1714. CONSTRUCTOR. */
  1715. if (!*non_constant_p
  1716. && TREE_CODE (whole) != VECTOR_CST
  1717. && TREE_CODE (whole) != CONSTRUCTOR)
  1718. {
  1719. if (!ctx->quiet)
  1720. error ("%qE is not a constant expression", orig_whole);
  1721. *non_constant_p = true;
  1722. }
  1723. if (*non_constant_p)
  1724. return t;
  1725. if (TREE_CODE (whole) == VECTOR_CST)
  1726. return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
  1727. TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
  1728. start = TREE_OPERAND (t, 2);
  1729. istart = tree_to_shwi (start);
  1730. isize = tree_to_shwi (TREE_OPERAND (t, 1));
  1731. utype = TREE_TYPE (t);
  1732. if (!TYPE_UNSIGNED (utype))
  1733. utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
  1734. retval = build_int_cst (utype, 0);
  1735. FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
  1736. {
  1737. tree bitpos = bit_position (field);
  1738. if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
  1739. return value;
  1740. if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
  1741. && TREE_CODE (value) == INTEGER_CST
  1742. && tree_fits_shwi_p (bitpos)
  1743. && tree_fits_shwi_p (DECL_SIZE (field)))
  1744. {
  1745. HOST_WIDE_INT bit = tree_to_shwi (bitpos);
  1746. HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
  1747. HOST_WIDE_INT shift;
  1748. if (bit >= istart && bit + sz <= istart + isize)
  1749. {
  1750. fldval = fold_convert (utype, value);
  1751. mask = build_int_cst_type (utype, -1);
  1752. mask = fold_build2 (LSHIFT_EXPR, utype, mask,
  1753. size_int (TYPE_PRECISION (utype) - sz));
  1754. mask = fold_build2 (RSHIFT_EXPR, utype, mask,
  1755. size_int (TYPE_PRECISION (utype) - sz));
  1756. fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
  1757. shift = bit - istart;
  1758. if (BYTES_BIG_ENDIAN)
  1759. shift = TYPE_PRECISION (utype) - shift - sz;
  1760. fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
  1761. size_int (shift));
  1762. retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
  1763. fld_seen = true;
  1764. }
  1765. }
  1766. }
  1767. if (fld_seen)
  1768. return fold_convert (TREE_TYPE (t), retval);
  1769. gcc_unreachable ();
  1770. return error_mark_node;
  1771. }
  1772. /* Subroutine of cxx_eval_constant_expression.
  1773. Evaluate a short-circuited logical expression T in the context
  1774. of a given constexpr CALL. BAILOUT_VALUE is the value for
  1775. early return. CONTINUE_VALUE is used here purely for
  1776. sanity check purposes. */
  1777. static tree
  1778. cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
  1779. tree bailout_value, tree continue_value,
  1780. bool lval,
  1781. bool *non_constant_p, bool *overflow_p)
  1782. {
  1783. tree r;
  1784. tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
  1785. lval,
  1786. non_constant_p, overflow_p);
  1787. VERIFY_CONSTANT (lhs);
  1788. if (tree_int_cst_equal (lhs, bailout_value))
  1789. return lhs;
  1790. gcc_assert (tree_int_cst_equal (lhs, continue_value));
  1791. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
  1792. lval, non_constant_p,
  1793. overflow_p);
  1794. VERIFY_CONSTANT (r);
  1795. return r;
  1796. }
  1797. /* REF is a COMPONENT_REF designating a particular field. V is a vector of
  1798. CONSTRUCTOR elements to initialize (part of) an object containing that
  1799. field. Return a pointer to the constructor_elt corresponding to the
  1800. initialization of the field. */
  1801. static constructor_elt *
  1802. base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
  1803. {
  1804. tree aggr = TREE_OPERAND (ref, 0);
  1805. tree field = TREE_OPERAND (ref, 1);
  1806. HOST_WIDE_INT i;
  1807. constructor_elt *ce;
  1808. gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
  1809. if (TREE_CODE (aggr) == COMPONENT_REF)
  1810. {
  1811. constructor_elt *base_ce
  1812. = base_field_constructor_elt (v, aggr);
  1813. v = CONSTRUCTOR_ELTS (base_ce->value);
  1814. }
  1815. for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
  1816. if (ce->index == field)
  1817. return ce;
  1818. gcc_unreachable ();
  1819. return NULL;
  1820. }
  1821. /* Some of the expressions fed to the constexpr mechanism are calls to
  1822. constructors, which have type void. In that case, return the type being
  1823. initialized by the constructor. */
  1824. static tree
  1825. initialized_type (tree t)
  1826. {
  1827. if (TYPE_P (t))
  1828. return t;
  1829. tree type = cv_unqualified (TREE_TYPE (t));
  1830. if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
  1831. {
  1832. /* A constructor call has void type, so we need to look deeper. */
  1833. tree fn = get_function_named_in_call (t);
  1834. if (fn && TREE_CODE (fn) == FUNCTION_DECL
  1835. && DECL_CXX_CONSTRUCTOR_P (fn))
  1836. type = DECL_CONTEXT (fn);
  1837. }
  1838. return type;
  1839. }
  1840. /* We're about to initialize element INDEX of an array or class from VALUE.
  1841. Set up NEW_CTX appropriately by adjusting .object to refer to the
  1842. subobject and creating a new CONSTRUCTOR if the element is itself
  1843. a class or array. */
  1844. static void
  1845. init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
  1846. tree index, tree &value)
  1847. {
  1848. new_ctx = *ctx;
  1849. if (index && TREE_CODE (index) != INTEGER_CST
  1850. && TREE_CODE (index) != FIELD_DECL)
  1851. /* This won't have an element in the new CONSTRUCTOR. */
  1852. return;
  1853. tree type = initialized_type (value);
  1854. if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
  1855. /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
  1856. return;
  1857. /* The sub-aggregate initializer might contain a placeholder;
  1858. update object to refer to the subobject and ctor to refer to
  1859. the (newly created) sub-initializer. */
  1860. if (ctx->object)
  1861. new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
  1862. tree elt = build_constructor (type, NULL);
  1863. CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
  1864. new_ctx.ctor = elt;
  1865. if (TREE_CODE (value) == TARGET_EXPR)
  1866. /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
  1867. value = TARGET_EXPR_INITIAL (value);
  1868. }
  1869. /* We're about to process an initializer for a class or array TYPE. Make
  1870. sure that CTX is set up appropriately. */
  1871. static void
  1872. verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
  1873. {
  1874. /* We don't bother building a ctor for an empty base subobject. */
  1875. if (is_empty_class (type))
  1876. return;
  1877. /* We're in the middle of an initializer that might involve placeholders;
  1878. our caller should have created a CONSTRUCTOR for us to put the
  1879. initializer into. We will either return that constructor or T. */
  1880. gcc_assert (ctx->ctor);
  1881. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  1882. (type, TREE_TYPE (ctx->ctor)));
  1883. gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
  1884. if (ctx->object)
  1885. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  1886. (type, TREE_TYPE (ctx->object)));
  1887. gcc_assert (!ctx->object || !DECL_P (ctx->object)
  1888. || *(ctx->values->get (ctx->object)) == ctx->ctor);
  1889. }
  1890. /* Subroutine of cxx_eval_constant_expression.
  1891. The expression tree T denotes a C-style array or a C-style
  1892. aggregate. Reduce it to a constant expression. */
  1893. static tree
  1894. cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
  1895. bool lval,
  1896. bool *non_constant_p, bool *overflow_p)
  1897. {
  1898. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
  1899. bool changed = false;
  1900. gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
  1901. verify_ctor_sanity (ctx, TREE_TYPE (t));
  1902. vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
  1903. vec_alloc (*p, vec_safe_length (v));
  1904. unsigned i; tree index, value;
  1905. FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
  1906. {
  1907. constexpr_ctx new_ctx;
  1908. init_subob_ctx (ctx, new_ctx, index, value);
  1909. if (new_ctx.ctor != ctx->ctor)
  1910. /* If we built a new CONSTRUCTOR, attach it now so that other
  1911. initializers can refer to it. */
  1912. CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
  1913. tree elt = cxx_eval_constant_expression (&new_ctx, value,
  1914. lval,
  1915. non_constant_p, overflow_p);
  1916. /* Don't VERIFY_CONSTANT here. */
  1917. if (ctx->quiet && *non_constant_p)
  1918. break;
  1919. if (elt != value)
  1920. changed = true;
  1921. if (index && TREE_CODE (index) == COMPONENT_REF)
  1922. {
  1923. /* This is an initialization of a vfield inside a base
  1924. subaggregate that we already initialized; push this
  1925. initialization into the previous initialization. */
  1926. constructor_elt *inner = base_field_constructor_elt (*p, index);
  1927. inner->value = elt;
  1928. changed = true;
  1929. }
  1930. else if (index
  1931. && (TREE_CODE (index) == NOP_EXPR
  1932. || TREE_CODE (index) == POINTER_PLUS_EXPR))
  1933. {
  1934. /* This is an initializer for an empty base; now that we've
  1935. checked that it's constant, we can ignore it. */
  1936. gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
  1937. changed = true;
  1938. }
  1939. else if (new_ctx.ctor != ctx->ctor)
  1940. {
  1941. /* We appended this element above; update the value. */
  1942. gcc_assert ((*p)->last().index == index);
  1943. (*p)->last().value = elt;
  1944. }
  1945. else
  1946. CONSTRUCTOR_APPEND_ELT (*p, index, elt);
  1947. }
  1948. if (*non_constant_p || !changed)
  1949. return t;
  1950. t = ctx->ctor;
  1951. /* We're done building this CONSTRUCTOR, so now we can interpret an
  1952. element without an explicit initializer as value-initialized. */
  1953. CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
  1954. if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
  1955. t = fold (t);
  1956. return t;
  1957. }
  1958. /* Subroutine of cxx_eval_constant_expression.
  1959. The expression tree T is a VEC_INIT_EXPR which denotes the desired
  1960. initialization of a non-static data member of array type. Reduce it to a
  1961. CONSTRUCTOR.
  1962. Note that apart from value-initialization (when VALUE_INIT is true),
  1963. this is only intended to support value-initialization and the
  1964. initializations done by defaulted constructors for classes with
  1965. non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
  1966. will either be NULL_TREE for the default constructor, or a COMPONENT_REF
  1967. for the copy/move constructor. */
  1968. static tree
  1969. cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
  1970. bool value_init, bool lval,
  1971. bool *non_constant_p, bool *overflow_p)
  1972. {
  1973. tree elttype = TREE_TYPE (atype);
  1974. unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
  1975. verify_ctor_sanity (ctx, atype);
  1976. vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
  1977. vec_alloc (*p, max + 1);
  1978. bool pre_init = false;
  1979. unsigned HOST_WIDE_INT i;
  1980. /* For the default constructor, build up a call to the default
  1981. constructor of the element type. We only need to handle class types
  1982. here, as for a constructor to be constexpr, all members must be
  1983. initialized, which for a defaulted default constructor means they must
  1984. be of a class type with a constexpr default constructor. */
  1985. if (TREE_CODE (elttype) == ARRAY_TYPE)
  1986. /* We only do this at the lowest level. */;
  1987. else if (value_init)
  1988. {
  1989. init = build_value_init (elttype, tf_warning_or_error);
  1990. pre_init = true;
  1991. }
  1992. else if (!init)
  1993. {
  1994. vec<tree, va_gc> *argvec = make_tree_vector ();
  1995. init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
  1996. &argvec, elttype, LOOKUP_NORMAL,
  1997. tf_warning_or_error);
  1998. release_tree_vector (argvec);
  1999. init = build_aggr_init_expr (TREE_TYPE (init), init);
  2000. pre_init = true;
  2001. }
  2002. for (i = 0; i < max; ++i)
  2003. {
  2004. tree idx = build_int_cst (size_type_node, i);
  2005. tree eltinit;
  2006. constexpr_ctx new_ctx;
  2007. init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
  2008. if (new_ctx.ctor != ctx->ctor)
  2009. CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
  2010. if (TREE_CODE (elttype) == ARRAY_TYPE)
  2011. {
  2012. /* A multidimensional array; recurse. */
  2013. if (value_init || init == NULL_TREE)
  2014. eltinit = NULL_TREE;
  2015. else
  2016. eltinit = cp_build_array_ref (input_location, init, idx,
  2017. tf_warning_or_error);
  2018. eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
  2019. lval,
  2020. non_constant_p, overflow_p);
  2021. }
  2022. else if (pre_init)
  2023. {
  2024. /* Initializing an element using value or default initialization
  2025. we just pre-built above. */
  2026. eltinit = (cxx_eval_constant_expression
  2027. (&new_ctx, init,
  2028. lval, non_constant_p, overflow_p));
  2029. }
  2030. else
  2031. {
  2032. /* Copying an element. */
  2033. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  2034. (atype, TREE_TYPE (init)));
  2035. eltinit = cp_build_array_ref (input_location, init, idx,
  2036. tf_warning_or_error);
  2037. if (!real_lvalue_p (init))
  2038. eltinit = move (eltinit);
  2039. eltinit = force_rvalue (eltinit, tf_warning_or_error);
  2040. eltinit = (cxx_eval_constant_expression
  2041. (&new_ctx, eltinit, lval,
  2042. non_constant_p, overflow_p));
  2043. }
  2044. if (*non_constant_p && !ctx->quiet)
  2045. break;
  2046. if (new_ctx.ctor != ctx->ctor)
  2047. {
  2048. /* We appended this element above; update the value. */
  2049. gcc_assert ((*p)->last().index == idx);
  2050. (*p)->last().value = eltinit;
  2051. }
  2052. else
  2053. CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
  2054. }
  2055. if (!*non_constant_p)
  2056. {
  2057. init = ctx->ctor;
  2058. CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
  2059. }
  2060. return init;
  2061. }
  2062. static tree
  2063. cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
  2064. bool lval,
  2065. bool *non_constant_p, bool *overflow_p)
  2066. {
  2067. tree atype = TREE_TYPE (t);
  2068. tree init = VEC_INIT_EXPR_INIT (t);
  2069. tree r = cxx_eval_vec_init_1 (ctx, atype, init,
  2070. VEC_INIT_EXPR_VALUE_INIT (t),
  2071. lval, non_constant_p, overflow_p);
  2072. if (*non_constant_p)
  2073. return t;
  2074. else
  2075. return r;
  2076. }
  2077. /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
  2078. match. We want to be less strict for simple *& folding; if we have a
  2079. non-const temporary that we access through a const pointer, that should
  2080. work. We handle this here rather than change fold_indirect_ref_1
  2081. because we're dealing with things like ADDR_EXPR of INTEGER_CST which
  2082. don't really make sense outside of constant expression evaluation. Also
  2083. we want to allow folding to COMPONENT_REF, which could cause trouble
  2084. with TBAA in fold_indirect_ref_1.
  2085. Try to keep this function synced with fold_indirect_ref_1. */
  2086. static tree
  2087. cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
  2088. {
  2089. tree sub, subtype;
  2090. sub = op0;
  2091. STRIP_NOPS (sub);
  2092. subtype = TREE_TYPE (sub);
  2093. if (!POINTER_TYPE_P (subtype))
  2094. return NULL_TREE;
  2095. if (TREE_CODE (sub) == ADDR_EXPR)
  2096. {
  2097. tree op = TREE_OPERAND (sub, 0);
  2098. tree optype = TREE_TYPE (op);
  2099. /* *&CONST_DECL -> to the value of the const decl. */
  2100. if (TREE_CODE (op) == CONST_DECL)
  2101. return DECL_INITIAL (op);
  2102. /* *&p => p; make sure to handle *&"str"[cst] here. */
  2103. if (same_type_ignoring_top_level_qualifiers_p (optype, type))
  2104. {
  2105. tree fop = fold_read_from_constant_string (op);
  2106. if (fop)
  2107. return fop;
  2108. else
  2109. return op;
  2110. }
  2111. /* *(foo *)&fooarray => fooarray[0] */
  2112. else if (TREE_CODE (optype) == ARRAY_TYPE
  2113. && (same_type_ignoring_top_level_qualifiers_p
  2114. (type, TREE_TYPE (optype))))
  2115. {
  2116. tree type_domain = TYPE_DOMAIN (optype);
  2117. tree min_val = size_zero_node;
  2118. if (type_domain && TYPE_MIN_VALUE (type_domain))
  2119. min_val = TYPE_MIN_VALUE (type_domain);
  2120. return build4_loc (loc, ARRAY_REF, type, op, min_val,
  2121. NULL_TREE, NULL_TREE);
  2122. }
  2123. /* *(foo *)&complexfoo => __real__ complexfoo */
  2124. else if (TREE_CODE (optype) == COMPLEX_TYPE
  2125. && (same_type_ignoring_top_level_qualifiers_p
  2126. (type, TREE_TYPE (optype))))
  2127. return fold_build1_loc (loc, REALPART_EXPR, type, op);
  2128. /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
  2129. else if (TREE_CODE (optype) == VECTOR_TYPE
  2130. && (same_type_ignoring_top_level_qualifiers_p
  2131. (type, TREE_TYPE (optype))))
  2132. {
  2133. tree part_width = TYPE_SIZE (type);
  2134. tree index = bitsize_int (0);
  2135. return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
  2136. }
  2137. /* Also handle conversion to an empty base class, which
  2138. is represented with a NOP_EXPR. */
  2139. else if (is_empty_class (type)
  2140. && CLASS_TYPE_P (optype)
  2141. && DERIVED_FROM_P (type, optype))
  2142. {
  2143. *empty_base = true;
  2144. return op;
  2145. }
  2146. /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
  2147. else if (RECORD_OR_UNION_TYPE_P (optype))
  2148. {
  2149. tree field = TYPE_FIELDS (optype);
  2150. for (; field; field = DECL_CHAIN (field))
  2151. if (TREE_CODE (field) == FIELD_DECL
  2152. && integer_zerop (byte_position (field))
  2153. && (same_type_ignoring_top_level_qualifiers_p
  2154. (TREE_TYPE (field), type)))
  2155. {
  2156. return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
  2157. break;
  2158. }
  2159. }
  2160. }
  2161. else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
  2162. && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
  2163. {
  2164. tree op00 = TREE_OPERAND (sub, 0);
  2165. tree op01 = TREE_OPERAND (sub, 1);
  2166. STRIP_NOPS (op00);
  2167. if (TREE_CODE (op00) == ADDR_EXPR)
  2168. {
  2169. tree op00type;
  2170. op00 = TREE_OPERAND (op00, 0);
  2171. op00type = TREE_TYPE (op00);
  2172. /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
  2173. if (TREE_CODE (op00type) == VECTOR_TYPE
  2174. && (same_type_ignoring_top_level_qualifiers_p
  2175. (type, TREE_TYPE (op00type))))
  2176. {
  2177. HOST_WIDE_INT offset = tree_to_shwi (op01);
  2178. tree part_width = TYPE_SIZE (type);
  2179. unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
  2180. unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
  2181. tree index = bitsize_int (indexi);
  2182. if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
  2183. return fold_build3_loc (loc,
  2184. BIT_FIELD_REF, type, op00,
  2185. part_width, index);
  2186. }
  2187. /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
  2188. else if (TREE_CODE (op00type) == COMPLEX_TYPE
  2189. && (same_type_ignoring_top_level_qualifiers_p
  2190. (type, TREE_TYPE (op00type))))
  2191. {
  2192. tree size = TYPE_SIZE_UNIT (type);
  2193. if (tree_int_cst_equal (size, op01))
  2194. return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
  2195. }
  2196. /* ((foo *)&fooarray)[1] => fooarray[1] */
  2197. else if (TREE_CODE (op00type) == ARRAY_TYPE
  2198. && (same_type_ignoring_top_level_qualifiers_p
  2199. (type, TREE_TYPE (op00type))))
  2200. {
  2201. tree type_domain = TYPE_DOMAIN (op00type);
  2202. tree min_val = size_zero_node;
  2203. if (type_domain && TYPE_MIN_VALUE (type_domain))
  2204. min_val = TYPE_MIN_VALUE (type_domain);
  2205. op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
  2206. TYPE_SIZE_UNIT (type));
  2207. op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
  2208. return build4_loc (loc, ARRAY_REF, type, op00, op01,
  2209. NULL_TREE, NULL_TREE);
  2210. }
  2211. /* Also handle conversion to an empty base class, which
  2212. is represented with a NOP_EXPR. */
  2213. else if (is_empty_class (type)
  2214. && CLASS_TYPE_P (op00type)
  2215. && DERIVED_FROM_P (type, op00type))
  2216. {
  2217. *empty_base = true;
  2218. return op00;
  2219. }
  2220. /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
  2221. else if (RECORD_OR_UNION_TYPE_P (op00type))
  2222. {
  2223. tree field = TYPE_FIELDS (op00type);
  2224. for (; field; field = DECL_CHAIN (field))
  2225. if (TREE_CODE (field) == FIELD_DECL
  2226. && tree_int_cst_equal (byte_position (field), op01)
  2227. && (same_type_ignoring_top_level_qualifiers_p
  2228. (TREE_TYPE (field), type)))
  2229. {
  2230. return fold_build3 (COMPONENT_REF, type, op00,
  2231. field, NULL_TREE);
  2232. break;
  2233. }
  2234. }
  2235. }
  2236. }
  2237. /* *(foo *)fooarrptr => (*fooarrptr)[0] */
  2238. else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
  2239. && (same_type_ignoring_top_level_qualifiers_p
  2240. (type, TREE_TYPE (TREE_TYPE (subtype)))))
  2241. {
  2242. tree type_domain;
  2243. tree min_val = size_zero_node;
  2244. tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
  2245. if (newsub)
  2246. sub = newsub;
  2247. else
  2248. sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
  2249. type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
  2250. if (type_domain && TYPE_MIN_VALUE (type_domain))
  2251. min_val = TYPE_MIN_VALUE (type_domain);
  2252. return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
  2253. NULL_TREE);
  2254. }
  2255. return NULL_TREE;
  2256. }
  2257. static tree
  2258. cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
  2259. bool lval,
  2260. bool *non_constant_p, bool *overflow_p)
  2261. {
  2262. tree orig_op0 = TREE_OPERAND (t, 0);
  2263. tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
  2264. /*lval*/false, non_constant_p,
  2265. overflow_p);
  2266. bool empty_base = false;
  2267. tree r;
  2268. /* Don't VERIFY_CONSTANT here. */
  2269. if (*non_constant_p)
  2270. return t;
  2271. r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
  2272. &empty_base);
  2273. if (r)
  2274. r = cxx_eval_constant_expression (ctx, r,
  2275. lval, non_constant_p, overflow_p);
  2276. else
  2277. {
  2278. tree sub = op0;
  2279. STRIP_NOPS (sub);
  2280. if (TREE_CODE (sub) == ADDR_EXPR)
  2281. {
  2282. /* We couldn't fold to a constant value. Make sure it's not
  2283. something we should have been able to fold. */
  2284. gcc_assert (!same_type_ignoring_top_level_qualifiers_p
  2285. (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
  2286. /* DR 1188 says we don't have to deal with this. */
  2287. if (!ctx->quiet)
  2288. error ("accessing value of %qE through a %qT glvalue in a "
  2289. "constant expression", build_fold_indirect_ref (sub),
  2290. TREE_TYPE (t));
  2291. *non_constant_p = true;
  2292. return t;
  2293. }
  2294. }
  2295. /* If we're pulling out the value of an empty base, make sure
  2296. that the whole object is constant and then return an empty
  2297. CONSTRUCTOR. */
  2298. if (empty_base && !lval)
  2299. {
  2300. VERIFY_CONSTANT (r);
  2301. r = build_constructor (TREE_TYPE (t), NULL);
  2302. TREE_CONSTANT (r) = true;
  2303. }
  2304. if (r == NULL_TREE)
  2305. {
  2306. if (lval && op0 != orig_op0)
  2307. return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
  2308. if (!lval)
  2309. VERIFY_CONSTANT (t);
  2310. return t;
  2311. }
  2312. return r;
  2313. }
  2314. /* Complain about R, a VAR_DECL, not being usable in a constant expression.
  2315. Shared between potential_constant_expression and
  2316. cxx_eval_constant_expression. */
  2317. static void
  2318. non_const_var_error (tree r)
  2319. {
  2320. tree type = TREE_TYPE (r);
  2321. error ("the value of %qD is not usable in a constant "
  2322. "expression", r);
  2323. /* Avoid error cascade. */
  2324. if (DECL_INITIAL (r) == error_mark_node)
  2325. return;
  2326. if (DECL_DECLARED_CONSTEXPR_P (r))
  2327. inform (DECL_SOURCE_LOCATION (r),
  2328. "%qD used in its own initializer", r);
  2329. else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
  2330. {
  2331. if (!CP_TYPE_CONST_P (type))
  2332. inform (DECL_SOURCE_LOCATION (r),
  2333. "%q#D is not const", r);
  2334. else if (CP_TYPE_VOLATILE_P (type))
  2335. inform (DECL_SOURCE_LOCATION (r),
  2336. "%q#D is volatile", r);
  2337. else if (!DECL_INITIAL (r)
  2338. || !TREE_CONSTANT (DECL_INITIAL (r)))
  2339. inform (DECL_SOURCE_LOCATION (r),
  2340. "%qD was not initialized with a constant "
  2341. "expression", r);
  2342. else
  2343. gcc_unreachable ();
  2344. }
  2345. else
  2346. {
  2347. if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
  2348. inform (DECL_SOURCE_LOCATION (r),
  2349. "%qD was not declared %<constexpr%>", r);
  2350. else
  2351. inform (DECL_SOURCE_LOCATION (r),
  2352. "%qD does not have integral or enumeration type",
  2353. r);
  2354. }
  2355. }
  2356. /* Subroutine of cxx_eval_constant_expression.
  2357. Like cxx_eval_unary_expression, except for trinary expressions. */
  2358. static tree
  2359. cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
  2360. bool lval,
  2361. bool *non_constant_p, bool *overflow_p)
  2362. {
  2363. int i;
  2364. tree args[3];
  2365. tree val;
  2366. for (i = 0; i < 3; i++)
  2367. {
  2368. args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
  2369. lval,
  2370. non_constant_p, overflow_p);
  2371. VERIFY_CONSTANT (args[i]);
  2372. }
  2373. val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
  2374. args[0], args[1], args[2]);
  2375. if (val == NULL_TREE)
  2376. return t;
  2377. VERIFY_CONSTANT (val);
  2378. return val;
  2379. }
  2380. bool
  2381. var_in_constexpr_fn (tree t)
  2382. {
  2383. tree ctx = DECL_CONTEXT (t);
  2384. return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
  2385. && DECL_DECLARED_CONSTEXPR_P (ctx));
  2386. }
  2387. /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
  2388. static tree
  2389. cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
  2390. bool lval,
  2391. bool *non_constant_p, bool *overflow_p)
  2392. {
  2393. constexpr_ctx new_ctx = *ctx;
  2394. tree init = TREE_OPERAND (t, 1);
  2395. /* First we figure out where we're storing to. */
  2396. tree target = TREE_OPERAND (t, 0);
  2397. tree type = TREE_TYPE (target);
  2398. target = cxx_eval_constant_expression (ctx, target,
  2399. true,
  2400. non_constant_p, overflow_p);
  2401. if (*non_constant_p)
  2402. return t;
  2403. if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type)
  2404. && is_empty_class (type))
  2405. {
  2406. /* For initialization of an empty base, the original target will be
  2407. *(base*)this, which the above evaluation resolves to the object
  2408. argument, which has the derived type rather than the base type. In
  2409. this situation, just evaluate the initializer and return, since
  2410. there's no actual data to store. */
  2411. return cxx_eval_constant_expression (ctx, init, false,
  2412. non_constant_p, overflow_p);
  2413. }
  2414. /* And then find the underlying variable. */
  2415. vec<tree,va_gc> *refs = make_tree_vector();
  2416. tree object = NULL_TREE;
  2417. for (tree probe = target; object == NULL_TREE; )
  2418. {
  2419. switch (TREE_CODE (probe))
  2420. {
  2421. case BIT_FIELD_REF:
  2422. case COMPONENT_REF:
  2423. case ARRAY_REF:
  2424. vec_safe_push (refs, TREE_OPERAND (probe, 1));
  2425. vec_safe_push (refs, TREE_TYPE (probe));
  2426. probe = TREE_OPERAND (probe, 0);
  2427. break;
  2428. default:
  2429. object = probe;
  2430. }
  2431. }
  2432. /* And then find/build up our initializer for the path to the subobject
  2433. we're initializing. */
  2434. tree *valp;
  2435. if (DECL_P (object))
  2436. valp = ctx->values->get (object);
  2437. else
  2438. valp = NULL;
  2439. if (!valp)
  2440. {
  2441. /* A constant-expression cannot modify objects from outside the
  2442. constant-expression. */
  2443. if (!ctx->quiet)
  2444. error ("modification of %qE is not a constant-expression", object);
  2445. *non_constant_p = true;
  2446. return t;
  2447. }
  2448. type = TREE_TYPE (object);
  2449. while (!refs->is_empty())
  2450. {
  2451. if (*valp == NULL_TREE)
  2452. {
  2453. *valp = build_constructor (type, NULL);
  2454. CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
  2455. }
  2456. constructor_elt ce;
  2457. type = refs->pop();
  2458. ce.index = refs->pop();
  2459. ce.value = NULL_TREE;
  2460. unsigned HOST_WIDE_INT idx = 0;
  2461. constructor_elt *cep = NULL;
  2462. for (idx = 0;
  2463. vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
  2464. idx++)
  2465. /* ??? slow */
  2466. if (cp_tree_equal (ce.index, cep->index))
  2467. break;
  2468. if (!cep)
  2469. cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
  2470. valp = &cep->value;
  2471. }
  2472. release_tree_vector (refs);
  2473. if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
  2474. {
  2475. /* Create a new CONSTRUCTOR in case evaluation of the initializer
  2476. wants to modify it. */
  2477. new_ctx.ctor = build_constructor (type, NULL);
  2478. if (*valp == NULL_TREE)
  2479. *valp = new_ctx.ctor;
  2480. CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
  2481. new_ctx.object = target;
  2482. }
  2483. init = cxx_eval_constant_expression (&new_ctx, init, false,
  2484. non_constant_p, overflow_p);
  2485. if (target == object)
  2486. {
  2487. /* The hash table might have moved since the get earlier. */
  2488. valp = ctx->values->get (object);
  2489. if (TREE_CODE (init) == CONSTRUCTOR)
  2490. /* An outer ctx->ctor might be pointing to *valp, so just replace
  2491. its contents. */
  2492. CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
  2493. else
  2494. *valp = init;
  2495. }
  2496. else
  2497. *valp = init;
  2498. if (*non_constant_p)
  2499. return t;
  2500. else if (lval)
  2501. return target;
  2502. else
  2503. return init;
  2504. }
  2505. /* Evaluate a ++ or -- expression. */
  2506. static tree
  2507. cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
  2508. bool lval,
  2509. bool *non_constant_p, bool *overflow_p)
  2510. {
  2511. enum tree_code code = TREE_CODE (t);
  2512. tree type = TREE_TYPE (t);
  2513. tree op = TREE_OPERAND (t, 0);
  2514. tree offset = TREE_OPERAND (t, 1);
  2515. gcc_assert (TREE_CONSTANT (offset));
  2516. /* The operand as an lvalue. */
  2517. op = cxx_eval_constant_expression (ctx, op, true,
  2518. non_constant_p, overflow_p);
  2519. /* The operand as an rvalue. */
  2520. tree val = rvalue (op);
  2521. val = cxx_eval_constant_expression (ctx, val, false,
  2522. non_constant_p, overflow_p);
  2523. /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
  2524. a local array in a constexpr function. */
  2525. bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
  2526. if (!ptr)
  2527. VERIFY_CONSTANT (val);
  2528. /* The modified value. */
  2529. bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
  2530. tree mod;
  2531. if (POINTER_TYPE_P (type))
  2532. {
  2533. /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
  2534. offset = convert_to_ptrofftype (offset);
  2535. if (!inc)
  2536. offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
  2537. mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
  2538. }
  2539. else
  2540. mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
  2541. if (!ptr)
  2542. VERIFY_CONSTANT (mod);
  2543. /* Storing the modified value. */
  2544. tree store = build2 (MODIFY_EXPR, type, op, mod);
  2545. cxx_eval_constant_expression (ctx, store,
  2546. true, non_constant_p, overflow_p);
  2547. /* And the value of the expression. */
  2548. if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
  2549. {
  2550. /* Prefix ops are lvalues. */
  2551. if (lval)
  2552. return op;
  2553. else
  2554. /* But we optimize when the caller wants an rvalue. */
  2555. return mod;
  2556. }
  2557. else
  2558. /* Postfix ops are rvalues. */
  2559. return val;
  2560. }
  2561. /* Predicates for the meaning of *jump_target. */
  2562. static bool
  2563. returns (tree *jump_target)
  2564. {
  2565. return *jump_target
  2566. && TREE_CODE (*jump_target) == RETURN_EXPR;
  2567. }
  2568. static bool
  2569. breaks (tree *jump_target)
  2570. {
  2571. return *jump_target
  2572. && TREE_CODE (*jump_target) == LABEL_DECL
  2573. && LABEL_DECL_BREAK (*jump_target);
  2574. }
  2575. static bool
  2576. continues (tree *jump_target)
  2577. {
  2578. return *jump_target
  2579. && TREE_CODE (*jump_target) == LABEL_DECL
  2580. && LABEL_DECL_CONTINUE (*jump_target);
  2581. }
  2582. static bool
  2583. switches (tree *jump_target)
  2584. {
  2585. return *jump_target
  2586. && TREE_CODE (*jump_target) == INTEGER_CST;
  2587. }
  2588. /* Subroutine of cxx_eval_statement_list. Determine whether the statement
  2589. at I matches *jump_target. If we're looking for a case label and we see
  2590. the default label, copy I into DEFAULT_LABEL. */
  2591. static bool
  2592. label_matches (tree *jump_target, tree_stmt_iterator i,
  2593. tree_stmt_iterator& default_label)
  2594. {
  2595. tree stmt = tsi_stmt (i);
  2596. switch (TREE_CODE (*jump_target))
  2597. {
  2598. case LABEL_DECL:
  2599. if (TREE_CODE (stmt) == LABEL_EXPR
  2600. && LABEL_EXPR_LABEL (stmt) == *jump_target)
  2601. return true;
  2602. break;
  2603. case INTEGER_CST:
  2604. if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
  2605. {
  2606. if (!CASE_LOW (stmt))
  2607. default_label = i;
  2608. else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
  2609. return true;
  2610. }
  2611. break;
  2612. default:
  2613. gcc_unreachable ();
  2614. }
  2615. return false;
  2616. }
  2617. /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
  2618. semantics, for switch, break, continue, and return. */
  2619. static tree
  2620. cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
  2621. bool *non_constant_p, bool *overflow_p,
  2622. tree *jump_target)
  2623. {
  2624. tree_stmt_iterator i;
  2625. tree_stmt_iterator default_label = tree_stmt_iterator();
  2626. tree local_target;
  2627. /* In a statement-expression we want to return the last value. */
  2628. tree r = NULL_TREE;
  2629. if (!jump_target)
  2630. {
  2631. local_target = NULL_TREE;
  2632. jump_target = &local_target;
  2633. }
  2634. for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
  2635. {
  2636. reenter:
  2637. tree stmt = tsi_stmt (i);
  2638. if (*jump_target)
  2639. {
  2640. if (TREE_CODE (stmt) == STATEMENT_LIST)
  2641. /* The label we want might be inside. */;
  2642. else if (label_matches (jump_target, i, default_label))
  2643. /* Found it. */
  2644. *jump_target = NULL_TREE;
  2645. else
  2646. continue;
  2647. }
  2648. r = cxx_eval_constant_expression (ctx, stmt, false,
  2649. non_constant_p, overflow_p,
  2650. jump_target);
  2651. if (*non_constant_p)
  2652. break;
  2653. if (returns (jump_target) || breaks (jump_target))
  2654. break;
  2655. }
  2656. if (switches (jump_target) && !tsi_end_p (default_label))
  2657. {
  2658. i = default_label;
  2659. *jump_target = NULL_TREE;
  2660. goto reenter;
  2661. }
  2662. return r;
  2663. }
  2664. /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
  2665. semantics; continue semantics are covered by cxx_eval_statement_list. */
  2666. static tree
  2667. cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
  2668. bool *non_constant_p, bool *overflow_p,
  2669. tree *jump_target)
  2670. {
  2671. tree body = TREE_OPERAND (t, 0);
  2672. while (true)
  2673. {
  2674. cxx_eval_statement_list (ctx, body,
  2675. non_constant_p, overflow_p, jump_target);
  2676. if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
  2677. break;
  2678. }
  2679. if (breaks (jump_target))
  2680. *jump_target = NULL_TREE;
  2681. return NULL_TREE;
  2682. }
  2683. /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
  2684. semantics. */
  2685. static tree
  2686. cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
  2687. bool *non_constant_p, bool *overflow_p,
  2688. tree *jump_target)
  2689. {
  2690. tree cond = TREE_OPERAND (t, 0);
  2691. cond = cxx_eval_constant_expression (ctx, cond, false,
  2692. non_constant_p, overflow_p);
  2693. VERIFY_CONSTANT (cond);
  2694. *jump_target = cond;
  2695. tree body = TREE_OPERAND (t, 1);
  2696. cxx_eval_statement_list (ctx, body,
  2697. non_constant_p, overflow_p, jump_target);
  2698. if (breaks (jump_target) || switches (jump_target))
  2699. *jump_target = NULL_TREE;
  2700. return NULL_TREE;
  2701. }
  2702. /* Subroutine of cxx_eval_constant_expression.
  2703. Attempt to reduce a POINTER_PLUS_EXPR expression T. */
  2704. static tree
  2705. cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
  2706. bool lval, bool *non_constant_p,
  2707. bool *overflow_p)
  2708. {
  2709. tree orig_type = TREE_TYPE (t);
  2710. tree op00 = TREE_OPERAND (t, 0);
  2711. tree op01 = TREE_OPERAND (t, 1);
  2712. location_t loc = EXPR_LOCATION (t);
  2713. op00 = cxx_eval_constant_expression (ctx, op00, lval,
  2714. non_constant_p, overflow_p);
  2715. STRIP_NOPS (op00);
  2716. if (TREE_CODE (op00) != ADDR_EXPR)
  2717. return NULL_TREE;
  2718. op00 = TREE_OPERAND (op00, 0);
  2719. /* &A[i] p+ j => &A[i + j] */
  2720. if (TREE_CODE (op00) == ARRAY_REF
  2721. && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
  2722. && TREE_CODE (op01) == INTEGER_CST
  2723. && TYPE_SIZE_UNIT (TREE_TYPE (op00))
  2724. && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
  2725. {
  2726. tree type = TREE_TYPE (op00);
  2727. t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
  2728. tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
  2729. /* Don't fold an out-of-bound access. */
  2730. if (!tree_int_cst_le (t, nelts))
  2731. return NULL_TREE;
  2732. op01 = cp_fold_convert (ssizetype, op01);
  2733. /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
  2734. constexpr int A[1]; ... (char *)&A[0] + 1 */
  2735. if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
  2736. op01, TYPE_SIZE_UNIT (type))))
  2737. return NULL_TREE;
  2738. /* Make sure to treat the second operand of POINTER_PLUS_EXPR
  2739. as signed. */
  2740. op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
  2741. TYPE_SIZE_UNIT (type));
  2742. t = size_binop_loc (loc, PLUS_EXPR, op01, t);
  2743. t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
  2744. t, NULL_TREE, NULL_TREE);
  2745. t = cp_build_addr_expr (t, tf_warning_or_error);
  2746. t = cp_fold_convert (orig_type, t);
  2747. return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
  2748. overflow_p);
  2749. }
  2750. return NULL_TREE;
  2751. }
  2752. /* Attempt to reduce the expression T to a constant value.
  2753. On failure, issue diagnostic and return error_mark_node. */
  2754. /* FIXME unify with c_fully_fold */
  2755. /* FIXME overflow_p is too global */
  2756. static tree
  2757. cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
  2758. bool lval,
  2759. bool *non_constant_p, bool *overflow_p,
  2760. tree *jump_target)
  2761. {
  2762. constexpr_ctx new_ctx;
  2763. tree r = t;
  2764. if (t == error_mark_node)
  2765. {
  2766. *non_constant_p = true;
  2767. return t;
  2768. }
  2769. if (CONSTANT_CLASS_P (t))
  2770. {
  2771. if (TREE_CODE (t) == PTRMEM_CST)
  2772. t = cplus_expand_constant (t);
  2773. else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
  2774. *overflow_p = true;
  2775. return t;
  2776. }
  2777. switch (TREE_CODE (t))
  2778. {
  2779. case RESULT_DECL:
  2780. if (lval)
  2781. return t;
  2782. /* We ask for an rvalue for the RESULT_DECL when indirecting
  2783. through an invisible reference, or in named return value
  2784. optimization. */
  2785. return (*ctx->values->get (t));
  2786. case VAR_DECL:
  2787. case CONST_DECL:
  2788. /* We used to not check lval for CONST_DECL, but darwin.c uses
  2789. CONST_DECL for aggregate constants. */
  2790. if (lval)
  2791. return t;
  2792. if (ctx->strict)
  2793. r = decl_really_constant_value (t);
  2794. else
  2795. r = decl_constant_value (t);
  2796. if (TREE_CODE (r) == TARGET_EXPR
  2797. && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
  2798. r = TARGET_EXPR_INITIAL (r);
  2799. if (TREE_CODE (r) == VAR_DECL)
  2800. if (tree *p = ctx->values->get (r))
  2801. r = *p;
  2802. if (DECL_P (r))
  2803. {
  2804. if (!ctx->quiet)
  2805. non_const_var_error (r);
  2806. *non_constant_p = true;
  2807. }
  2808. break;
  2809. case FUNCTION_DECL:
  2810. case TEMPLATE_DECL:
  2811. case LABEL_DECL:
  2812. case LABEL_EXPR:
  2813. case CASE_LABEL_EXPR:
  2814. return t;
  2815. case PARM_DECL:
  2816. if (!use_new_call && ctx
  2817. && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
  2818. r = lookup_parameter_binding (ctx->call, t);
  2819. else if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
  2820. /* glvalue use. */;
  2821. else if (tree *p = ctx->values->get (r))
  2822. r = *p;
  2823. else if (lval)
  2824. /* Defer in case this is only used for its type. */;
  2825. else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
  2826. /* Defer, there's no lvalue->rvalue conversion. */;
  2827. else if (is_empty_class (TREE_TYPE (t)))
  2828. {
  2829. /* If the class is empty, we aren't actually loading anything. */
  2830. r = build_constructor (TREE_TYPE (t), NULL);
  2831. TREE_CONSTANT (r) = true;
  2832. }
  2833. else
  2834. {
  2835. if (!ctx->quiet)
  2836. error ("%qE is not a constant expression", t);
  2837. *non_constant_p = true;
  2838. }
  2839. break;
  2840. case CALL_EXPR:
  2841. case AGGR_INIT_EXPR:
  2842. r = cxx_eval_call_expression (ctx, t, lval,
  2843. non_constant_p, overflow_p);
  2844. break;
  2845. case DECL_EXPR:
  2846. {
  2847. r = DECL_EXPR_DECL (t);
  2848. if (AGGREGATE_TYPE_P (TREE_TYPE (r))
  2849. || VECTOR_TYPE_P (TREE_TYPE (r)))
  2850. {
  2851. new_ctx = *ctx;
  2852. new_ctx.object = r;
  2853. new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
  2854. CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
  2855. new_ctx.values->put (r, new_ctx.ctor);
  2856. ctx = &new_ctx;
  2857. }
  2858. if (tree init = DECL_INITIAL (r))
  2859. {
  2860. init = cxx_eval_constant_expression (ctx, init,
  2861. false,
  2862. non_constant_p, overflow_p);
  2863. ctx->values->put (r, init);
  2864. }
  2865. else if (ctx == &new_ctx)
  2866. /* We gave it a CONSTRUCTOR above. */;
  2867. else
  2868. ctx->values->put (r, NULL_TREE);
  2869. }
  2870. break;
  2871. case TARGET_EXPR:
  2872. if (!literal_type_p (TREE_TYPE (t)))
  2873. {
  2874. if (!ctx->quiet)
  2875. {
  2876. error ("temporary of non-literal type %qT in a "
  2877. "constant expression", TREE_TYPE (t));
  2878. explain_non_literal_class (TREE_TYPE (t));
  2879. }
  2880. *non_constant_p = true;
  2881. break;
  2882. }
  2883. if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
  2884. {
  2885. /* We're being expanded without an explicit target, so start
  2886. initializing a new object; expansion with an explicit target
  2887. strips the TARGET_EXPR before we get here. */
  2888. new_ctx = *ctx;
  2889. new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
  2890. CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
  2891. new_ctx.object = TARGET_EXPR_SLOT (t);
  2892. ctx->values->put (new_ctx.object, new_ctx.ctor);
  2893. ctx = &new_ctx;
  2894. }
  2895. /* Pass false for 'lval' because this indicates
  2896. initialization of a temporary. */
  2897. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
  2898. false,
  2899. non_constant_p, overflow_p);
  2900. if (!*non_constant_p)
  2901. /* Adjust the type of the result to the type of the temporary. */
  2902. r = adjust_temp_type (TREE_TYPE (t), r);
  2903. if (lval)
  2904. {
  2905. tree slot = TARGET_EXPR_SLOT (t);
  2906. ctx->values->put (slot, r);
  2907. return slot;
  2908. }
  2909. break;
  2910. case INIT_EXPR:
  2911. if (!use_new_call)
  2912. {
  2913. /* In C++11 constexpr evaluation we are looking for the value,
  2914. not the side-effect of the initialization. */
  2915. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
  2916. false,
  2917. non_constant_p, overflow_p);
  2918. break;
  2919. }
  2920. /* else fall through */
  2921. case MODIFY_EXPR:
  2922. r = cxx_eval_store_expression (ctx, t, lval,
  2923. non_constant_p, overflow_p);
  2924. break;
  2925. case SCOPE_REF:
  2926. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
  2927. lval,
  2928. non_constant_p, overflow_p);
  2929. break;
  2930. case RETURN_EXPR:
  2931. if (TREE_OPERAND (t, 0) != NULL_TREE)
  2932. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
  2933. lval,
  2934. non_constant_p, overflow_p);
  2935. *jump_target = t;
  2936. break;
  2937. case SAVE_EXPR:
  2938. /* Avoid evaluating a SAVE_EXPR more than once. */
  2939. if (tree *p = ctx->values->get (t))
  2940. r = *p;
  2941. else
  2942. {
  2943. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
  2944. non_constant_p, overflow_p);
  2945. ctx->values->put (t, r);
  2946. }
  2947. break;
  2948. case NON_LVALUE_EXPR:
  2949. case TRY_CATCH_EXPR:
  2950. case TRY_BLOCK:
  2951. case CLEANUP_POINT_EXPR:
  2952. case MUST_NOT_THROW_EXPR:
  2953. case EXPR_STMT:
  2954. case EH_SPEC_BLOCK:
  2955. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
  2956. lval,
  2957. non_constant_p, overflow_p,
  2958. jump_target);
  2959. break;
  2960. case TRY_FINALLY_EXPR:
  2961. r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
  2962. non_constant_p, overflow_p,
  2963. jump_target);
  2964. if (!*non_constant_p)
  2965. /* Also evaluate the cleanup. */
  2966. cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
  2967. non_constant_p, overflow_p,
  2968. jump_target);
  2969. break;
  2970. /* These differ from cxx_eval_unary_expression in that this doesn't
  2971. check for a constant operand or result; an address can be
  2972. constant without its operand being, and vice versa. */
  2973. case INDIRECT_REF:
  2974. r = cxx_eval_indirect_ref (ctx, t, lval,
  2975. non_constant_p, overflow_p);
  2976. break;
  2977. case ADDR_EXPR:
  2978. {
  2979. tree oldop = TREE_OPERAND (t, 0);
  2980. tree op = cxx_eval_constant_expression (ctx, oldop,
  2981. /*lval*/true,
  2982. non_constant_p, overflow_p);
  2983. /* Don't VERIFY_CONSTANT here. */
  2984. if (*non_constant_p)
  2985. return t;
  2986. gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
  2987. /* This function does more aggressive folding than fold itself. */
  2988. r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
  2989. if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
  2990. return t;
  2991. break;
  2992. }
  2993. case REALPART_EXPR:
  2994. case IMAGPART_EXPR:
  2995. case CONJ_EXPR:
  2996. case FIX_TRUNC_EXPR:
  2997. case FLOAT_EXPR:
  2998. case NEGATE_EXPR:
  2999. case ABS_EXPR:
  3000. case BIT_NOT_EXPR:
  3001. case TRUTH_NOT_EXPR:
  3002. case FIXED_CONVERT_EXPR:
  3003. r = cxx_eval_unary_expression (ctx, t, lval,
  3004. non_constant_p, overflow_p);
  3005. break;
  3006. case SIZEOF_EXPR:
  3007. if (SIZEOF_EXPR_TYPE_P (t))
  3008. r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
  3009. SIZEOF_EXPR, false);
  3010. else if (TYPE_P (TREE_OPERAND (t, 0)))
  3011. r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
  3012. false);
  3013. else
  3014. r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
  3015. false);
  3016. if (r == error_mark_node)
  3017. r = size_one_node;
  3018. VERIFY_CONSTANT (r);
  3019. break;
  3020. case COMPOUND_EXPR:
  3021. {
  3022. /* check_return_expr sometimes wraps a TARGET_EXPR in a
  3023. COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
  3024. introduced by build_call_a. */
  3025. tree op0 = TREE_OPERAND (t, 0);
  3026. tree op1 = TREE_OPERAND (t, 1);
  3027. STRIP_NOPS (op1);
  3028. if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
  3029. || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
  3030. r = cxx_eval_constant_expression (ctx, op0,
  3031. lval, non_constant_p, overflow_p,
  3032. jump_target);
  3033. else
  3034. {
  3035. /* Check that the LHS is constant and then discard it. */
  3036. cxx_eval_constant_expression (ctx, op0,
  3037. true, non_constant_p, overflow_p,
  3038. jump_target);
  3039. op1 = TREE_OPERAND (t, 1);
  3040. r = cxx_eval_constant_expression (ctx, op1,
  3041. lval, non_constant_p, overflow_p,
  3042. jump_target);
  3043. }
  3044. }
  3045. break;
  3046. case POINTER_PLUS_EXPR:
  3047. r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
  3048. overflow_p);
  3049. if (r)
  3050. break;
  3051. /* else fall through */
  3052. case PLUS_EXPR:
  3053. case MINUS_EXPR:
  3054. case MULT_EXPR:
  3055. case TRUNC_DIV_EXPR:
  3056. case CEIL_DIV_EXPR:
  3057. case FLOOR_DIV_EXPR:
  3058. case ROUND_DIV_EXPR:
  3059. case TRUNC_MOD_EXPR:
  3060. case CEIL_MOD_EXPR:
  3061. case ROUND_MOD_EXPR:
  3062. case RDIV_EXPR:
  3063. case EXACT_DIV_EXPR:
  3064. case MIN_EXPR:
  3065. case MAX_EXPR:
  3066. case LSHIFT_EXPR:
  3067. case RSHIFT_EXPR:
  3068. case LROTATE_EXPR:
  3069. case RROTATE_EXPR:
  3070. case BIT_IOR_EXPR:
  3071. case BIT_XOR_EXPR:
  3072. case BIT_AND_EXPR:
  3073. case TRUTH_XOR_EXPR:
  3074. case LT_EXPR:
  3075. case LE_EXPR:
  3076. case GT_EXPR:
  3077. case GE_EXPR:
  3078. case EQ_EXPR:
  3079. case NE_EXPR:
  3080. case UNORDERED_EXPR:
  3081. case ORDERED_EXPR:
  3082. case UNLT_EXPR:
  3083. case UNLE_EXPR:
  3084. case UNGT_EXPR:
  3085. case UNGE_EXPR:
  3086. case UNEQ_EXPR:
  3087. case LTGT_EXPR:
  3088. case RANGE_EXPR:
  3089. case COMPLEX_EXPR:
  3090. r = cxx_eval_binary_expression (ctx, t, lval,
  3091. non_constant_p, overflow_p);
  3092. break;
  3093. /* fold can introduce non-IF versions of these; still treat them as
  3094. short-circuiting. */
  3095. case TRUTH_AND_EXPR:
  3096. case TRUTH_ANDIF_EXPR:
  3097. r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
  3098. boolean_true_node,
  3099. lval,
  3100. non_constant_p, overflow_p);
  3101. break;
  3102. case TRUTH_OR_EXPR:
  3103. case TRUTH_ORIF_EXPR:
  3104. r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
  3105. boolean_false_node,
  3106. lval,
  3107. non_constant_p, overflow_p);
  3108. break;
  3109. case ARRAY_REF:
  3110. r = cxx_eval_array_reference (ctx, t, lval,
  3111. non_constant_p, overflow_p);
  3112. break;
  3113. case COMPONENT_REF:
  3114. if (is_overloaded_fn (t))
  3115. {
  3116. /* We can only get here in checking mode via
  3117. build_non_dependent_expr, because any expression that
  3118. calls or takes the address of the function will have
  3119. pulled a FUNCTION_DECL out of the COMPONENT_REF. */
  3120. gcc_checking_assert (ctx->quiet || errorcount);
  3121. *non_constant_p = true;
  3122. return t;
  3123. }
  3124. r = cxx_eval_component_reference (ctx, t, lval,
  3125. non_constant_p, overflow_p);
  3126. break;
  3127. case BIT_FIELD_REF:
  3128. r = cxx_eval_bit_field_ref (ctx, t, lval,
  3129. non_constant_p, overflow_p);
  3130. break;
  3131. case COND_EXPR:
  3132. case VEC_COND_EXPR:
  3133. r = cxx_eval_conditional_expression (ctx, t, lval,
  3134. non_constant_p, overflow_p,
  3135. jump_target);
  3136. break;
  3137. case CONSTRUCTOR:
  3138. if (TREE_CONSTANT (t))
  3139. /* Don't re-process a constant CONSTRUCTOR, but do fold it to
  3140. VECTOR_CST if applicable. */
  3141. return fold (t);
  3142. r = cxx_eval_bare_aggregate (ctx, t, lval,
  3143. non_constant_p, overflow_p);
  3144. break;
  3145. case VEC_INIT_EXPR:
  3146. /* We can get this in a defaulted constructor for a class with a
  3147. non-static data member of array type. Either the initializer will
  3148. be NULL, meaning default-initialization, or it will be an lvalue
  3149. or xvalue of the same type, meaning direct-initialization from the
  3150. corresponding member. */
  3151. r = cxx_eval_vec_init (ctx, t, lval,
  3152. non_constant_p, overflow_p);
  3153. break;
  3154. case FMA_EXPR:
  3155. case VEC_PERM_EXPR:
  3156. r = cxx_eval_trinary_expression (ctx, t, lval,
  3157. non_constant_p, overflow_p);
  3158. break;
  3159. case CONVERT_EXPR:
  3160. case VIEW_CONVERT_EXPR:
  3161. case NOP_EXPR:
  3162. {
  3163. tree oldop = TREE_OPERAND (t, 0);
  3164. tree op = cxx_eval_constant_expression (ctx, oldop,
  3165. lval,
  3166. non_constant_p, overflow_p);
  3167. if (*non_constant_p)
  3168. return t;
  3169. if (POINTER_TYPE_P (TREE_TYPE (t))
  3170. && TREE_CODE (op) == INTEGER_CST
  3171. && !integer_zerop (op))
  3172. {
  3173. if (!ctx->quiet)
  3174. error_at (EXPR_LOC_OR_LOC (t, input_location),
  3175. "reinterpret_cast from integer to pointer");
  3176. *non_constant_p = true;
  3177. return t;
  3178. }
  3179. if (op == oldop)
  3180. /* We didn't fold at the top so we could check for ptr-int
  3181. conversion. */
  3182. return fold (t);
  3183. r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
  3184. /* Conversion of an out-of-range value has implementation-defined
  3185. behavior; the language considers it different from arithmetic
  3186. overflow, which is undefined. */
  3187. if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
  3188. TREE_OVERFLOW (r) = false;
  3189. }
  3190. break;
  3191. case EMPTY_CLASS_EXPR:
  3192. /* This is good enough for a function argument that might not get
  3193. used, and they can't do anything with it, so just return it. */
  3194. return t;
  3195. case STATEMENT_LIST:
  3196. new_ctx = *ctx;
  3197. new_ctx.ctor = new_ctx.object = NULL_TREE;
  3198. return cxx_eval_statement_list (&new_ctx, t,
  3199. non_constant_p, overflow_p, jump_target);
  3200. case BIND_EXPR:
  3201. return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
  3202. lval,
  3203. non_constant_p, overflow_p,
  3204. jump_target);
  3205. case PREINCREMENT_EXPR:
  3206. case POSTINCREMENT_EXPR:
  3207. case PREDECREMENT_EXPR:
  3208. case POSTDECREMENT_EXPR:
  3209. return cxx_eval_increment_expression (ctx, t,
  3210. lval, non_constant_p, overflow_p);
  3211. case LAMBDA_EXPR:
  3212. case NEW_EXPR:
  3213. case VEC_NEW_EXPR:
  3214. case DELETE_EXPR:
  3215. case VEC_DELETE_EXPR:
  3216. case THROW_EXPR:
  3217. case MODOP_EXPR:
  3218. /* GCC internal stuff. */
  3219. case VA_ARG_EXPR:
  3220. case OBJ_TYPE_REF:
  3221. case WITH_CLEANUP_EXPR:
  3222. case NON_DEPENDENT_EXPR:
  3223. case BASELINK:
  3224. case OFFSET_REF:
  3225. if (!ctx->quiet)
  3226. error_at (EXPR_LOC_OR_LOC (t, input_location),
  3227. "expression %qE is not a constant-expression", t);
  3228. *non_constant_p = true;
  3229. break;
  3230. case PLACEHOLDER_EXPR:
  3231. if (!ctx || !ctx->ctor || (lval && !ctx->object)
  3232. || !(same_type_ignoring_top_level_qualifiers_p
  3233. (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
  3234. {
  3235. /* A placeholder without a referent. We can get here when
  3236. checking whether NSDMIs are noexcept, or in massage_init_elt;
  3237. just say it's non-constant for now. */
  3238. gcc_assert (ctx->quiet);
  3239. *non_constant_p = true;
  3240. break;
  3241. }
  3242. else
  3243. {
  3244. /* Use of the value or address of the current object. We could
  3245. use ctx->object unconditionally, but using ctx->ctor when we
  3246. can is a minor optimization. */
  3247. tree ctor = lval ? ctx->object : ctx->ctor;
  3248. return cxx_eval_constant_expression
  3249. (ctx, ctor, lval,
  3250. non_constant_p, overflow_p);
  3251. }
  3252. break;
  3253. case GOTO_EXPR:
  3254. *jump_target = TREE_OPERAND (t, 0);
  3255. gcc_assert (breaks (jump_target) || continues (jump_target));
  3256. break;
  3257. case LOOP_EXPR:
  3258. cxx_eval_loop_expr (ctx, t,
  3259. non_constant_p, overflow_p, jump_target);
  3260. break;
  3261. case SWITCH_EXPR:
  3262. cxx_eval_switch_expr (ctx, t,
  3263. non_constant_p, overflow_p, jump_target);
  3264. break;
  3265. default:
  3266. if (STATEMENT_CODE_P (TREE_CODE (t)))
  3267. {
  3268. /* This function doesn't know how to deal with pre-genericize
  3269. statements; this can only happen with statement-expressions,
  3270. so for now just fail. */
  3271. if (!ctx->quiet)
  3272. error_at (EXPR_LOCATION (t),
  3273. "statement is not a constant-expression");
  3274. }
  3275. else
  3276. internal_error ("unexpected expression %qE of kind %s", t,
  3277. get_tree_code_name (TREE_CODE (t)));
  3278. *non_constant_p = true;
  3279. break;
  3280. }
  3281. if (r == error_mark_node)
  3282. *non_constant_p = true;
  3283. if (*non_constant_p)
  3284. return t;
  3285. else
  3286. return r;
  3287. }
  3288. static tree
  3289. cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
  3290. bool strict = true, tree object = NULL_TREE)
  3291. {
  3292. bool non_constant_p = false;
  3293. bool overflow_p = false;
  3294. hash_map<tree,tree> map;
  3295. constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
  3296. tree type = initialized_type (t);
  3297. tree r = t;
  3298. if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
  3299. {
  3300. /* In C++14 an NSDMI can participate in aggregate initialization,
  3301. and can refer to the address of the object being initialized, so
  3302. we need to pass in the relevant VAR_DECL if we want to do the
  3303. evaluation in a single pass. The evaluation will dynamically
  3304. update ctx.values for the VAR_DECL. We use the same strategy
  3305. for C++11 constexpr constructors that refer to the object being
  3306. initialized. */
  3307. ctx.ctor = build_constructor (type, NULL);
  3308. CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
  3309. if (!object)
  3310. {
  3311. if (TREE_CODE (t) == TARGET_EXPR)
  3312. object = TARGET_EXPR_SLOT (t);
  3313. else if (TREE_CODE (t) == AGGR_INIT_EXPR)
  3314. object = AGGR_INIT_EXPR_SLOT (t);
  3315. }
  3316. ctx.object = object;
  3317. if (object)
  3318. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  3319. (type, TREE_TYPE (object)));
  3320. if (object && DECL_P (object))
  3321. map.put (object, ctx.ctor);
  3322. if (TREE_CODE (r) == TARGET_EXPR)
  3323. /* Avoid creating another CONSTRUCTOR when we expand the
  3324. TARGET_EXPR. */
  3325. r = TARGET_EXPR_INITIAL (r);
  3326. }
  3327. r = cxx_eval_constant_expression (&ctx, r,
  3328. false, &non_constant_p, &overflow_p);
  3329. verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
  3330. /* Mutable logic is a bit tricky: we want to allow initialization of
  3331. constexpr variables with mutable members, but we can't copy those
  3332. members to another constexpr variable. */
  3333. if (TREE_CODE (r) == CONSTRUCTOR
  3334. && CONSTRUCTOR_MUTABLE_POISON (r))
  3335. {
  3336. if (!allow_non_constant)
  3337. error ("%qE is not a constant expression because it refers to "
  3338. "mutable subobjects of %qT", t, type);
  3339. non_constant_p = true;
  3340. }
  3341. /* Technically we should check this for all subexpressions, but that
  3342. runs into problems with our internal representation of pointer
  3343. subtraction and the 5.19 rules are still in flux. */
  3344. if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
  3345. && ARITHMETIC_TYPE_P (TREE_TYPE (r))
  3346. && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
  3347. {
  3348. if (!allow_non_constant)
  3349. error ("conversion from pointer type %qT "
  3350. "to arithmetic type %qT in a constant-expression",
  3351. TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
  3352. non_constant_p = true;
  3353. }
  3354. if (!non_constant_p && overflow_p)
  3355. non_constant_p = true;
  3356. if (non_constant_p && !allow_non_constant)
  3357. return error_mark_node;
  3358. else if (non_constant_p && TREE_CONSTANT (r))
  3359. {
  3360. /* This isn't actually constant, so unset TREE_CONSTANT. */
  3361. if (EXPR_P (r))
  3362. r = copy_node (r);
  3363. else if (TREE_CODE (r) == CONSTRUCTOR)
  3364. r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
  3365. else
  3366. r = build_nop (TREE_TYPE (r), r);
  3367. TREE_CONSTANT (r) = false;
  3368. }
  3369. else if (non_constant_p || r == t)
  3370. return t;
  3371. if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
  3372. {
  3373. if (TREE_CODE (t) == TARGET_EXPR
  3374. && TARGET_EXPR_INITIAL (t) == r)
  3375. return t;
  3376. else
  3377. {
  3378. r = get_target_expr (r);
  3379. TREE_CONSTANT (r) = true;
  3380. return r;
  3381. }
  3382. }
  3383. else
  3384. return r;
  3385. }
  3386. /* Returns true if T is a valid subexpression of a constant expression,
  3387. even if it isn't itself a constant expression. */
  3388. bool
  3389. is_sub_constant_expr (tree t)
  3390. {
  3391. bool non_constant_p = false;
  3392. bool overflow_p = false;
  3393. hash_map <tree, tree> map;
  3394. constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
  3395. cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
  3396. &overflow_p);
  3397. return !non_constant_p && !overflow_p;
  3398. }
  3399. /* If T represents a constant expression returns its reduced value.
  3400. Otherwise return error_mark_node. If T is dependent, then
  3401. return NULL. */
  3402. tree
  3403. cxx_constant_value (tree t, tree decl)
  3404. {
  3405. return cxx_eval_outermost_constant_expr (t, false, true, decl);
  3406. }
  3407. /* If T is a constant expression, returns its reduced value.
  3408. Otherwise, if T does not have TREE_CONSTANT set, returns T.
  3409. Otherwise, returns a version of T without TREE_CONSTANT. */
  3410. tree
  3411. maybe_constant_value (tree t, tree decl)
  3412. {
  3413. tree r;
  3414. if (instantiation_dependent_expression_p (t)
  3415. || type_unknown_p (t)
  3416. || BRACE_ENCLOSED_INITIALIZER_P (t)
  3417. || !potential_constant_expression (t))
  3418. {
  3419. if (TREE_OVERFLOW_P (t))
  3420. {
  3421. t = build_nop (TREE_TYPE (t), t);
  3422. TREE_CONSTANT (t) = false;
  3423. }
  3424. return t;
  3425. }
  3426. r = cxx_eval_outermost_constant_expr (t, true, true, decl);
  3427. #ifdef ENABLE_CHECKING
  3428. gcc_assert (r == t
  3429. || CONVERT_EXPR_P (t)
  3430. || TREE_CODE (t) == VIEW_CONVERT_EXPR
  3431. || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
  3432. || !cp_tree_equal (r, t));
  3433. #endif
  3434. return r;
  3435. }
  3436. /* Like maybe_constant_value but first fully instantiate the argument.
  3437. Note: this is equivalent to instantiate_non_dependent_expr_sfinae
  3438. (t, tf_none) followed by maybe_constant_value but is more efficient,
  3439. because calls instantiation_dependent_expression_p and
  3440. potential_constant_expression at most once. */
  3441. tree
  3442. fold_non_dependent_expr (tree t)
  3443. {
  3444. if (t == NULL_TREE)
  3445. return NULL_TREE;
  3446. /* If we're in a template, but T isn't value dependent, simplify
  3447. it. We're supposed to treat:
  3448. template <typename T> void f(T[1 + 1]);
  3449. template <typename T> void f(T[2]);
  3450. as two declarations of the same function, for example. */
  3451. if (processing_template_decl)
  3452. {
  3453. if (!instantiation_dependent_expression_p (t)
  3454. && potential_constant_expression (t))
  3455. {
  3456. processing_template_decl_sentinel s;
  3457. t = instantiate_non_dependent_expr_internal (t, tf_none);
  3458. if (type_unknown_p (t)
  3459. || BRACE_ENCLOSED_INITIALIZER_P (t))
  3460. {
  3461. if (TREE_OVERFLOW_P (t))
  3462. {
  3463. t = build_nop (TREE_TYPE (t), t);
  3464. TREE_CONSTANT (t) = false;
  3465. }
  3466. return t;
  3467. }
  3468. tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
  3469. #ifdef ENABLE_CHECKING
  3470. /* cp_tree_equal looks through NOPs, so allow them. */
  3471. gcc_assert (r == t
  3472. || CONVERT_EXPR_P (t)
  3473. || TREE_CODE (t) == VIEW_CONVERT_EXPR
  3474. || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
  3475. || !cp_tree_equal (r, t));
  3476. #endif
  3477. return r;
  3478. }
  3479. else if (TREE_OVERFLOW_P (t))
  3480. {
  3481. t = build_nop (TREE_TYPE (t), t);
  3482. TREE_CONSTANT (t) = false;
  3483. }
  3484. return t;
  3485. }
  3486. return maybe_constant_value (t);
  3487. }
  3488. /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
  3489. than wrapped in a TARGET_EXPR. */
  3490. tree
  3491. maybe_constant_init (tree t, tree decl)
  3492. {
  3493. if (TREE_CODE (t) == EXPR_STMT)
  3494. t = TREE_OPERAND (t, 0);
  3495. if (TREE_CODE (t) == CONVERT_EXPR
  3496. && VOID_TYPE_P (TREE_TYPE (t)))
  3497. t = TREE_OPERAND (t, 0);
  3498. if (TREE_CODE (t) == INIT_EXPR)
  3499. t = TREE_OPERAND (t, 1);
  3500. if (instantiation_dependent_expression_p (t)
  3501. || type_unknown_p (t)
  3502. || BRACE_ENCLOSED_INITIALIZER_P (t)
  3503. || !potential_static_init_expression (t))
  3504. /* Don't try to evaluate it. */;
  3505. else
  3506. t = cxx_eval_outermost_constant_expr (t, true, false, decl);
  3507. if (TREE_CODE (t) == TARGET_EXPR)
  3508. {
  3509. tree init = TARGET_EXPR_INITIAL (t);
  3510. if (TREE_CODE (init) == CONSTRUCTOR)
  3511. t = init;
  3512. }
  3513. return t;
  3514. }
  3515. #if 0
  3516. /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
  3517. /* Return true if the object referred to by REF has automatic or thread
  3518. local storage. */
  3519. enum { ck_ok, ck_bad, ck_unknown };
  3520. static int
  3521. check_automatic_or_tls (tree ref)
  3522. {
  3523. machine_mode mode;
  3524. HOST_WIDE_INT bitsize, bitpos;
  3525. tree offset;
  3526. int volatilep = 0, unsignedp = 0;
  3527. tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
  3528. &mode, &unsignedp, &volatilep, false);
  3529. duration_kind dk;
  3530. /* If there isn't a decl in the middle, we don't know the linkage here,
  3531. and this isn't a constant expression anyway. */
  3532. if (!DECL_P (decl))
  3533. return ck_unknown;
  3534. dk = decl_storage_duration (decl);
  3535. return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
  3536. }
  3537. #endif
  3538. /* Return true if T denotes a potentially constant expression. Issue
  3539. diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
  3540. an lvalue-rvalue conversion is implied.
  3541. C++0x [expr.const] used to say
  3542. 6 An expression is a potential constant expression if it is
  3543. a constant expression where all occurrences of function
  3544. parameters are replaced by arbitrary constant expressions
  3545. of the appropriate type.
  3546. 2 A conditional expression is a constant expression unless it
  3547. involves one of the following as a potentially evaluated
  3548. subexpression (3.2), but subexpressions of logical AND (5.14),
  3549. logical OR (5.15), and conditional (5.16) operations that are
  3550. not evaluated are not considered. */
  3551. static bool
  3552. potential_constant_expression_1 (tree t, bool want_rval, bool strict,
  3553. tsubst_flags_t flags)
  3554. {
  3555. #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
  3556. enum { any = false, rval = true };
  3557. int i;
  3558. tree tmp;
  3559. if (t == error_mark_node)
  3560. return false;
  3561. if (t == NULL_TREE)
  3562. return true;
  3563. if (TREE_THIS_VOLATILE (t))
  3564. {
  3565. if (flags & tf_error)
  3566. error ("expression %qE has side-effects", t);
  3567. return false;
  3568. }
  3569. if (CONSTANT_CLASS_P (t))
  3570. return true;
  3571. switch (TREE_CODE (t))
  3572. {
  3573. case FUNCTION_DECL:
  3574. case BASELINK:
  3575. case TEMPLATE_DECL:
  3576. case OVERLOAD:
  3577. case TEMPLATE_ID_EXPR:
  3578. case LABEL_DECL:
  3579. case LABEL_EXPR:
  3580. case CASE_LABEL_EXPR:
  3581. case CONST_DECL:
  3582. case SIZEOF_EXPR:
  3583. case ALIGNOF_EXPR:
  3584. case OFFSETOF_EXPR:
  3585. case NOEXCEPT_EXPR:
  3586. case TEMPLATE_PARM_INDEX:
  3587. case TRAIT_EXPR:
  3588. case IDENTIFIER_NODE:
  3589. case USERDEF_LITERAL:
  3590. /* We can see a FIELD_DECL in a pointer-to-member expression. */
  3591. case FIELD_DECL:
  3592. case PARM_DECL:
  3593. case USING_DECL:
  3594. case USING_STMT:
  3595. case PLACEHOLDER_EXPR:
  3596. case BREAK_STMT:
  3597. case CONTINUE_STMT:
  3598. return true;
  3599. case AGGR_INIT_EXPR:
  3600. case CALL_EXPR:
  3601. /* -- an invocation of a function other than a constexpr function
  3602. or a constexpr constructor. */
  3603. {
  3604. tree fun = get_function_named_in_call (t);
  3605. const int nargs = call_expr_nargs (t);
  3606. i = 0;
  3607. if (fun == NULL_TREE)
  3608. {
  3609. if (TREE_CODE (t) == CALL_EXPR
  3610. && CALL_EXPR_FN (t) == NULL_TREE)
  3611. switch (CALL_EXPR_IFN (t))
  3612. {
  3613. /* These should be ignored, they are optimized away from
  3614. constexpr functions. */
  3615. case IFN_UBSAN_NULL:
  3616. case IFN_UBSAN_BOUNDS:
  3617. case IFN_UBSAN_VPTR:
  3618. return true;
  3619. default:
  3620. break;
  3621. }
  3622. /* fold_call_expr can't do anything with IFN calls. */
  3623. if (flags & tf_error)
  3624. error_at (EXPR_LOC_OR_LOC (t, input_location),
  3625. "call to internal function");
  3626. return false;
  3627. }
  3628. if (is_overloaded_fn (fun))
  3629. {
  3630. if (TREE_CODE (fun) == FUNCTION_DECL)
  3631. {
  3632. if (builtin_valid_in_constant_expr_p (fun))
  3633. return true;
  3634. if (!DECL_DECLARED_CONSTEXPR_P (fun)
  3635. /* Allow any built-in function; if the expansion
  3636. isn't constant, we'll deal with that then. */
  3637. && !is_builtin_fn (fun))
  3638. {
  3639. if (flags & tf_error)
  3640. {
  3641. error_at (EXPR_LOC_OR_LOC (t, input_location),
  3642. "call to non-constexpr function %qD", fun);
  3643. explain_invalid_constexpr_fn (fun);
  3644. }
  3645. return false;
  3646. }
  3647. /* A call to a non-static member function takes the address
  3648. of the object as the first argument. But in a constant
  3649. expression the address will be folded away, so look
  3650. through it now. */
  3651. if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
  3652. && !DECL_CONSTRUCTOR_P (fun))
  3653. {
  3654. tree x = get_nth_callarg (t, 0);
  3655. if (is_this_parameter (x))
  3656. return true;
  3657. else if (!RECUR (x, rval))
  3658. return false;
  3659. i = 1;
  3660. }
  3661. }
  3662. else
  3663. {
  3664. if (!RECUR (fun, true))
  3665. return false;
  3666. fun = get_first_fn (fun);
  3667. }
  3668. /* Skip initial arguments to base constructors. */
  3669. if (DECL_BASE_CONSTRUCTOR_P (fun))
  3670. i = num_artificial_parms_for (fun);
  3671. fun = DECL_ORIGIN (fun);
  3672. }
  3673. else
  3674. {
  3675. if (RECUR (fun, rval))
  3676. /* Might end up being a constant function pointer. */;
  3677. else
  3678. return false;
  3679. }
  3680. for (; i < nargs; ++i)
  3681. {
  3682. tree x = get_nth_callarg (t, i);
  3683. /* In a template, reference arguments haven't been converted to
  3684. REFERENCE_TYPE and we might not even know if the parameter
  3685. is a reference, so accept lvalue constants too. */
  3686. bool rv = processing_template_decl ? any : rval;
  3687. if (!RECUR (x, rv))
  3688. return false;
  3689. }
  3690. return true;
  3691. }
  3692. case NON_LVALUE_EXPR:
  3693. /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
  3694. -- an lvalue of integral type that refers to a non-volatile
  3695. const variable or static data member initialized with
  3696. constant expressions, or
  3697. -- an lvalue of literal type that refers to non-volatile
  3698. object defined with constexpr, or that refers to a
  3699. sub-object of such an object; */
  3700. return RECUR (TREE_OPERAND (t, 0), rval);
  3701. case VAR_DECL:
  3702. if (want_rval
  3703. && !decl_constant_var_p (t)
  3704. && (strict
  3705. || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
  3706. || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
  3707. && !var_in_constexpr_fn (t)
  3708. && !type_dependent_expression_p (t))
  3709. {
  3710. if (flags & tf_error)
  3711. non_const_var_error (t);
  3712. return false;
  3713. }
  3714. return true;
  3715. case NOP_EXPR:
  3716. case CONVERT_EXPR:
  3717. case VIEW_CONVERT_EXPR:
  3718. /* -- a reinterpret_cast. FIXME not implemented, and this rule
  3719. may change to something more specific to type-punning (DR 1312). */
  3720. {
  3721. tree from = TREE_OPERAND (t, 0);
  3722. if (POINTER_TYPE_P (TREE_TYPE (t))
  3723. && TREE_CODE (from) == INTEGER_CST
  3724. && !integer_zerop (from))
  3725. {
  3726. if (flags & tf_error)
  3727. error_at (EXPR_LOC_OR_LOC (t, input_location),
  3728. "reinterpret_cast from integer to pointer");
  3729. return false;
  3730. }
  3731. return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
  3732. }
  3733. case ADDR_EXPR:
  3734. /* -- a unary operator & that is applied to an lvalue that
  3735. designates an object with thread or automatic storage
  3736. duration; */
  3737. t = TREE_OPERAND (t, 0);
  3738. if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
  3739. /* A pointer-to-member constant. */
  3740. return true;
  3741. #if 0
  3742. /* FIXME adjust when issue 1197 is fully resolved. For now don't do
  3743. any checking here, as we might dereference the pointer later. If
  3744. we remove this code, also remove check_automatic_or_tls. */
  3745. i = check_automatic_or_tls (t);
  3746. if (i == ck_ok)
  3747. return true;
  3748. if (i == ck_bad)
  3749. {
  3750. if (flags & tf_error)
  3751. error ("address-of an object %qE with thread local or "
  3752. "automatic storage is not a constant expression", t);
  3753. return false;
  3754. }
  3755. #endif
  3756. return RECUR (t, any);
  3757. case COMPONENT_REF:
  3758. case BIT_FIELD_REF:
  3759. case ARROW_EXPR:
  3760. case OFFSET_REF:
  3761. /* -- a class member access unless its postfix-expression is
  3762. of literal type or of pointer to literal type. */
  3763. /* This test would be redundant, as it follows from the
  3764. postfix-expression being a potential constant expression. */
  3765. return RECUR (TREE_OPERAND (t, 0), want_rval);
  3766. case EXPR_PACK_EXPANSION:
  3767. return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
  3768. case INDIRECT_REF:
  3769. {
  3770. tree x = TREE_OPERAND (t, 0);
  3771. STRIP_NOPS (x);
  3772. if (is_this_parameter (x))
  3773. {
  3774. if (DECL_CONTEXT (x)
  3775. && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
  3776. {
  3777. if (flags & tf_error)
  3778. error ("use of %<this%> in a constant expression");
  3779. return false;
  3780. }
  3781. return true;
  3782. }
  3783. return RECUR (x, rval);
  3784. }
  3785. case STATEMENT_LIST:
  3786. {
  3787. tree_stmt_iterator i;
  3788. for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
  3789. {
  3790. if (!RECUR (tsi_stmt (i), any))
  3791. return false;
  3792. }
  3793. return true;
  3794. }
  3795. break;
  3796. case MODIFY_EXPR:
  3797. if (cxx_dialect < cxx14)
  3798. goto fail;
  3799. if (!RECUR (TREE_OPERAND (t, 0), any))
  3800. return false;
  3801. if (!RECUR (TREE_OPERAND (t, 1), rval))
  3802. return false;
  3803. return true;
  3804. case MODOP_EXPR:
  3805. if (cxx_dialect < cxx14)
  3806. goto fail;
  3807. if (!RECUR (TREE_OPERAND (t, 0), rval))
  3808. return false;
  3809. if (!RECUR (TREE_OPERAND (t, 2), rval))
  3810. return false;
  3811. return true;
  3812. case IF_STMT:
  3813. if (!RECUR (IF_COND (t), rval))
  3814. return false;
  3815. if (!RECUR (THEN_CLAUSE (t), any))
  3816. return false;
  3817. if (!RECUR (ELSE_CLAUSE (t), any))
  3818. return false;
  3819. return true;
  3820. case DO_STMT:
  3821. if (!RECUR (DO_COND (t), rval))
  3822. return false;
  3823. if (!RECUR (DO_BODY (t), any))
  3824. return false;
  3825. return true;
  3826. case FOR_STMT:
  3827. if (!RECUR (FOR_INIT_STMT (t), any))
  3828. return false;
  3829. if (!RECUR (FOR_COND (t), rval))
  3830. return false;
  3831. if (!RECUR (FOR_EXPR (t), any))
  3832. return false;
  3833. if (!RECUR (FOR_BODY (t), any))
  3834. return false;
  3835. return true;
  3836. case WHILE_STMT:
  3837. if (!RECUR (WHILE_COND (t), rval))
  3838. return false;
  3839. if (!RECUR (WHILE_BODY (t), any))
  3840. return false;
  3841. return true;
  3842. case SWITCH_STMT:
  3843. if (!RECUR (SWITCH_STMT_COND (t), rval))
  3844. return false;
  3845. if (!RECUR (SWITCH_STMT_BODY (t), any))
  3846. return false;
  3847. return true;
  3848. case STMT_EXPR:
  3849. return RECUR (STMT_EXPR_STMT (t), rval);
  3850. case LAMBDA_EXPR:
  3851. case DYNAMIC_CAST_EXPR:
  3852. case PSEUDO_DTOR_EXPR:
  3853. case NEW_EXPR:
  3854. case VEC_NEW_EXPR:
  3855. case DELETE_EXPR:
  3856. case VEC_DELETE_EXPR:
  3857. case THROW_EXPR:
  3858. case OMP_ATOMIC:
  3859. case OMP_ATOMIC_READ:
  3860. case OMP_ATOMIC_CAPTURE_OLD:
  3861. case OMP_ATOMIC_CAPTURE_NEW:
  3862. /* GCC internal stuff. */
  3863. case VA_ARG_EXPR:
  3864. case OBJ_TYPE_REF:
  3865. case TRANSACTION_EXPR:
  3866. case ASM_EXPR:
  3867. fail:
  3868. if (flags & tf_error)
  3869. error ("expression %qE is not a constant-expression", t);
  3870. return false;
  3871. case TYPEID_EXPR:
  3872. /* -- a typeid expression whose operand is of polymorphic
  3873. class type; */
  3874. {
  3875. tree e = TREE_OPERAND (t, 0);
  3876. if (!TYPE_P (e) && !type_dependent_expression_p (e)
  3877. && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
  3878. {
  3879. if (flags & tf_error)
  3880. error ("typeid-expression is not a constant expression "
  3881. "because %qE is of polymorphic type", e);
  3882. return false;
  3883. }
  3884. return true;
  3885. }
  3886. case MINUS_EXPR:
  3887. /* -- a subtraction where both operands are pointers. */
  3888. if (TYPE_PTR_P (TREE_OPERAND (t, 0))
  3889. && TYPE_PTR_P (TREE_OPERAND (t, 1)))
  3890. {
  3891. if (flags & tf_error)
  3892. error ("difference of two pointer expressions is not "
  3893. "a constant expression");
  3894. return false;
  3895. }
  3896. want_rval = true;
  3897. goto binary;
  3898. case LT_EXPR:
  3899. case LE_EXPR:
  3900. case GT_EXPR:
  3901. case GE_EXPR:
  3902. case EQ_EXPR:
  3903. case NE_EXPR:
  3904. /* -- a relational or equality operator where at least
  3905. one of the operands is a pointer. */
  3906. if (TYPE_PTR_P (TREE_OPERAND (t, 0))
  3907. || TYPE_PTR_P (TREE_OPERAND (t, 1)))
  3908. {
  3909. if (flags & tf_error)
  3910. error ("pointer comparison expression is not a "
  3911. "constant expression");
  3912. return false;
  3913. }
  3914. want_rval = true;
  3915. goto binary;
  3916. case PREINCREMENT_EXPR:
  3917. case POSTINCREMENT_EXPR:
  3918. case PREDECREMENT_EXPR:
  3919. case POSTDECREMENT_EXPR:
  3920. if (cxx_dialect < cxx14)
  3921. goto fail;
  3922. goto unary;
  3923. case BIT_NOT_EXPR:
  3924. /* A destructor. */
  3925. if (TYPE_P (TREE_OPERAND (t, 0)))
  3926. return true;
  3927. /* else fall through. */
  3928. case REALPART_EXPR:
  3929. case IMAGPART_EXPR:
  3930. case CONJ_EXPR:
  3931. case SAVE_EXPR:
  3932. case FIX_TRUNC_EXPR:
  3933. case FLOAT_EXPR:
  3934. case NEGATE_EXPR:
  3935. case ABS_EXPR:
  3936. case TRUTH_NOT_EXPR:
  3937. case FIXED_CONVERT_EXPR:
  3938. case UNARY_PLUS_EXPR:
  3939. unary:
  3940. return RECUR (TREE_OPERAND (t, 0), rval);
  3941. case CAST_EXPR:
  3942. case CONST_CAST_EXPR:
  3943. case STATIC_CAST_EXPR:
  3944. case REINTERPRET_CAST_EXPR:
  3945. case IMPLICIT_CONV_EXPR:
  3946. if (cxx_dialect < cxx11
  3947. && !dependent_type_p (TREE_TYPE (t))
  3948. && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
  3949. /* In C++98, a conversion to non-integral type can't be part of a
  3950. constant expression. */
  3951. {
  3952. if (flags & tf_error)
  3953. error ("cast to non-integral type %qT in a constant expression",
  3954. TREE_TYPE (t));
  3955. return false;
  3956. }
  3957. return (RECUR (TREE_OPERAND (t, 0),
  3958. TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
  3959. case BIND_EXPR:
  3960. return RECUR (BIND_EXPR_BODY (t), want_rval);
  3961. case WITH_CLEANUP_EXPR:
  3962. case CLEANUP_POINT_EXPR:
  3963. case MUST_NOT_THROW_EXPR:
  3964. case TRY_CATCH_EXPR:
  3965. case TRY_BLOCK:
  3966. case EH_SPEC_BLOCK:
  3967. case EXPR_STMT:
  3968. case PAREN_EXPR:
  3969. case DECL_EXPR:
  3970. case NON_DEPENDENT_EXPR:
  3971. /* For convenience. */
  3972. case RETURN_EXPR:
  3973. return RECUR (TREE_OPERAND (t, 0), want_rval);
  3974. case TRY_FINALLY_EXPR:
  3975. return (RECUR (TREE_OPERAND (t, 0), want_rval)
  3976. && RECUR (TREE_OPERAND (t, 1), any));
  3977. case SCOPE_REF:
  3978. return RECUR (TREE_OPERAND (t, 1), want_rval);
  3979. case TARGET_EXPR:
  3980. if (!literal_type_p (TREE_TYPE (t)))
  3981. {
  3982. if (flags & tf_error)
  3983. {
  3984. error ("temporary of non-literal type %qT in a "
  3985. "constant expression", TREE_TYPE (t));
  3986. explain_non_literal_class (TREE_TYPE (t));
  3987. }
  3988. return false;
  3989. }
  3990. case INIT_EXPR:
  3991. return RECUR (TREE_OPERAND (t, 1), rval);
  3992. case CONSTRUCTOR:
  3993. {
  3994. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
  3995. constructor_elt *ce;
  3996. for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
  3997. if (!RECUR (ce->value, want_rval))
  3998. return false;
  3999. return true;
  4000. }
  4001. case TREE_LIST:
  4002. {
  4003. gcc_assert (TREE_PURPOSE (t) == NULL_TREE
  4004. || DECL_P (TREE_PURPOSE (t)));
  4005. if (!RECUR (TREE_VALUE (t), want_rval))
  4006. return false;
  4007. if (TREE_CHAIN (t) == NULL_TREE)
  4008. return true;
  4009. return RECUR (TREE_CHAIN (t), want_rval);
  4010. }
  4011. case TRUNC_DIV_EXPR:
  4012. case CEIL_DIV_EXPR:
  4013. case FLOOR_DIV_EXPR:
  4014. case ROUND_DIV_EXPR:
  4015. case TRUNC_MOD_EXPR:
  4016. case CEIL_MOD_EXPR:
  4017. case ROUND_MOD_EXPR:
  4018. {
  4019. tree denom = TREE_OPERAND (t, 1);
  4020. if (!RECUR (denom, rval))
  4021. return false;
  4022. /* We can't call cxx_eval_outermost_constant_expr on an expression
  4023. that hasn't been through instantiate_non_dependent_expr yet. */
  4024. if (!processing_template_decl)
  4025. denom = cxx_eval_outermost_constant_expr (denom, true);
  4026. if (integer_zerop (denom))
  4027. {
  4028. if (flags & tf_error)
  4029. error ("division by zero is not a constant-expression");
  4030. return false;
  4031. }
  4032. else
  4033. {
  4034. want_rval = true;
  4035. return RECUR (TREE_OPERAND (t, 0), want_rval);
  4036. }
  4037. }
  4038. case COMPOUND_EXPR:
  4039. {
  4040. /* check_return_expr sometimes wraps a TARGET_EXPR in a
  4041. COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
  4042. introduced by build_call_a. */
  4043. tree op0 = TREE_OPERAND (t, 0);
  4044. tree op1 = TREE_OPERAND (t, 1);
  4045. STRIP_NOPS (op1);
  4046. if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
  4047. || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
  4048. return RECUR (op0, want_rval);
  4049. else
  4050. goto binary;
  4051. }
  4052. /* If the first operand is the non-short-circuit constant, look at
  4053. the second operand; otherwise we only care about the first one for
  4054. potentiality. */
  4055. case TRUTH_AND_EXPR:
  4056. case TRUTH_ANDIF_EXPR:
  4057. tmp = boolean_true_node;
  4058. goto truth;
  4059. case TRUTH_OR_EXPR:
  4060. case TRUTH_ORIF_EXPR:
  4061. tmp = boolean_false_node;
  4062. truth:
  4063. {
  4064. tree op = TREE_OPERAND (t, 0);
  4065. if (!RECUR (op, rval))
  4066. return false;
  4067. if (!processing_template_decl)
  4068. op = cxx_eval_outermost_constant_expr (op, true);
  4069. if (tree_int_cst_equal (op, tmp))
  4070. return RECUR (TREE_OPERAND (t, 1), rval);
  4071. else
  4072. return true;
  4073. }
  4074. case PLUS_EXPR:
  4075. case MULT_EXPR:
  4076. case POINTER_PLUS_EXPR:
  4077. case RDIV_EXPR:
  4078. case EXACT_DIV_EXPR:
  4079. case MIN_EXPR:
  4080. case MAX_EXPR:
  4081. case LSHIFT_EXPR:
  4082. case RSHIFT_EXPR:
  4083. case LROTATE_EXPR:
  4084. case RROTATE_EXPR:
  4085. case BIT_IOR_EXPR:
  4086. case BIT_XOR_EXPR:
  4087. case BIT_AND_EXPR:
  4088. case TRUTH_XOR_EXPR:
  4089. case UNORDERED_EXPR:
  4090. case ORDERED_EXPR:
  4091. case UNLT_EXPR:
  4092. case UNLE_EXPR:
  4093. case UNGT_EXPR:
  4094. case UNGE_EXPR:
  4095. case UNEQ_EXPR:
  4096. case LTGT_EXPR:
  4097. case RANGE_EXPR:
  4098. case COMPLEX_EXPR:
  4099. want_rval = true;
  4100. /* Fall through. */
  4101. case ARRAY_REF:
  4102. case ARRAY_RANGE_REF:
  4103. case MEMBER_REF:
  4104. case DOTSTAR_EXPR:
  4105. case MEM_REF:
  4106. binary:
  4107. for (i = 0; i < 2; ++i)
  4108. if (!RECUR (TREE_OPERAND (t, i), want_rval))
  4109. return false;
  4110. return true;
  4111. case CILK_SYNC_STMT:
  4112. case CILK_SPAWN_STMT:
  4113. case ARRAY_NOTATION_REF:
  4114. return false;
  4115. case FMA_EXPR:
  4116. case VEC_PERM_EXPR:
  4117. for (i = 0; i < 3; ++i)
  4118. if (!RECUR (TREE_OPERAND (t, i), true))
  4119. return false;
  4120. return true;
  4121. case COND_EXPR:
  4122. case VEC_COND_EXPR:
  4123. /* If the condition is a known constant, we know which of the legs we
  4124. care about; otherwise we only require that the condition and
  4125. either of the legs be potentially constant. */
  4126. tmp = TREE_OPERAND (t, 0);
  4127. if (!RECUR (tmp, rval))
  4128. return false;
  4129. if (!processing_template_decl)
  4130. tmp = cxx_eval_outermost_constant_expr (tmp, true);
  4131. if (integer_zerop (tmp))
  4132. return RECUR (TREE_OPERAND (t, 2), want_rval);
  4133. else if (TREE_CODE (tmp) == INTEGER_CST)
  4134. return RECUR (TREE_OPERAND (t, 1), want_rval);
  4135. for (i = 1; i < 3; ++i)
  4136. if (potential_constant_expression_1 (TREE_OPERAND (t, i),
  4137. want_rval, strict, tf_none))
  4138. return true;
  4139. if (flags & tf_error)
  4140. error ("expression %qE is not a constant-expression", t);
  4141. return false;
  4142. case VEC_INIT_EXPR:
  4143. if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
  4144. return true;
  4145. if (flags & tf_error)
  4146. {
  4147. error ("non-constant array initialization");
  4148. diagnose_non_constexpr_vec_init (t);
  4149. }
  4150. return false;
  4151. case TYPE_DECL:
  4152. case TAG_DEFN:
  4153. /* We can see these in statement-expressions. */
  4154. return true;
  4155. default:
  4156. if (objc_is_property_ref (t))
  4157. return false;
  4158. sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
  4159. gcc_unreachable();
  4160. return false;
  4161. }
  4162. #undef RECUR
  4163. }
  4164. /* The main entry point to the above. */
  4165. bool
  4166. potential_constant_expression (tree t)
  4167. {
  4168. return potential_constant_expression_1 (t, false, true, tf_none);
  4169. }
  4170. bool
  4171. potential_static_init_expression (tree t)
  4172. {
  4173. return potential_constant_expression_1 (t, false, false, tf_none);
  4174. }
  4175. /* As above, but require a constant rvalue. */
  4176. bool
  4177. potential_rvalue_constant_expression (tree t)
  4178. {
  4179. return potential_constant_expression_1 (t, true, true, tf_none);
  4180. }
  4181. /* Like above, but complain about non-constant expressions. */
  4182. bool
  4183. require_potential_constant_expression (tree t)
  4184. {
  4185. return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
  4186. }
  4187. /* Cross product of the above. */
  4188. bool
  4189. require_potential_rvalue_constant_expression (tree t)
  4190. {
  4191. return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
  4192. }
  4193. #include "gt-cp-constexpr.h"