typeck2.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  1. /* Report error messages, build initializers, and perform
  2. some front-end optimizations for C++ compiler.
  3. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  4. Hacked by Michael Tiemann (tiemann@cygnus.com)
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* This file is part of the C++ front end.
  18. It contains routines to build C++ expressions given their operands,
  19. including computing the types of the result, C and C++ specific error
  20. checks, and some optimization. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "tm.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "stor-layout.h"
  36. #include "varasm.h"
  37. #include "intl.h"
  38. #include "cp-tree.h"
  39. #include "flags.h"
  40. #include "diagnostic-core.h"
  41. #include "wide-int.h"
  42. static tree
  43. process_init_constructor (tree type, tree init, tsubst_flags_t complain);
  44. /* Print an error message stemming from an attempt to use
  45. BASETYPE as a base class for TYPE. */
  46. tree
  47. error_not_base_type (tree basetype, tree type)
  48. {
  49. if (TREE_CODE (basetype) == FUNCTION_DECL)
  50. basetype = DECL_CONTEXT (basetype);
  51. error ("type %qT is not a base type for type %qT", basetype, type);
  52. return error_mark_node;
  53. }
  54. tree
  55. binfo_or_else (tree base, tree type)
  56. {
  57. tree binfo = lookup_base (type, base, ba_unique,
  58. NULL, tf_warning_or_error);
  59. if (binfo == error_mark_node)
  60. return NULL_TREE;
  61. else if (!binfo)
  62. error_not_base_type (base, type);
  63. return binfo;
  64. }
  65. /* According to ARM $7.1.6, "A `const' object may be initialized, but its
  66. value may not be changed thereafter. */
  67. void
  68. cxx_readonly_error (tree arg, enum lvalue_use errstring)
  69. {
  70. /* This macro is used to emit diagnostics to ensure that all format
  71. strings are complete sentences, visible to gettext and checked at
  72. compile time. */
  73. #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
  74. do { \
  75. switch (errstring) \
  76. { \
  77. case lv_assign: \
  78. error(AS, ARG); \
  79. break; \
  80. case lv_asm: \
  81. error(ASM, ARG); \
  82. break; \
  83. case lv_increment: \
  84. error (IN, ARG); \
  85. break; \
  86. case lv_decrement: \
  87. error (DE, ARG); \
  88. break; \
  89. default: \
  90. gcc_unreachable (); \
  91. } \
  92. } while (0)
  93. /* Handle C++-specific things first. */
  94. if (VAR_P (arg)
  95. && DECL_LANG_SPECIFIC (arg)
  96. && DECL_IN_AGGR_P (arg)
  97. && !TREE_STATIC (arg))
  98. ERROR_FOR_ASSIGNMENT (G_("assignment of "
  99. "constant field %qD"),
  100. G_("constant field %qD "
  101. "used as %<asm%> output"),
  102. G_("increment of "
  103. "constant field %qD"),
  104. G_("decrement of "
  105. "constant field %qD"),
  106. arg);
  107. else if (INDIRECT_REF_P (arg)
  108. && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
  109. && (VAR_P (TREE_OPERAND (arg, 0))
  110. || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
  111. ERROR_FOR_ASSIGNMENT (G_("assignment of "
  112. "read-only reference %qD"),
  113. G_("read-only reference %qD "
  114. "used as %<asm%> output"),
  115. G_("increment of "
  116. "read-only reference %qD"),
  117. G_("decrement of "
  118. "read-only reference %qD"),
  119. TREE_OPERAND (arg, 0));
  120. else
  121. readonly_error (input_location, arg, errstring);
  122. }
  123. /* Structure that holds information about declarations whose type was
  124. incomplete and we could not check whether it was abstract or not. */
  125. struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
  126. /* Declaration which we are checking for abstractness. It is either
  127. a DECL node, or an IDENTIFIER_NODE if we do not have a full
  128. declaration available. */
  129. tree decl;
  130. /* Type which will be checked for abstractness. */
  131. tree type;
  132. /* Kind of use in an unnamed declarator. */
  133. abstract_class_use use;
  134. /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
  135. because DECLs already carry locus information. */
  136. location_t locus;
  137. /* Link to the next element in list. */
  138. struct pending_abstract_type* next;
  139. };
  140. struct abstract_type_hasher : ggc_hasher<pending_abstract_type *>
  141. {
  142. typedef tree compare_type;
  143. static hashval_t hash (pending_abstract_type *);
  144. static bool equal (pending_abstract_type *, tree);
  145. };
  146. /* Compute the hash value of the node VAL. This function is used by the
  147. hash table abstract_pending_vars. */
  148. hashval_t
  149. abstract_type_hasher::hash (pending_abstract_type *pat)
  150. {
  151. return (hashval_t) TYPE_UID (pat->type);
  152. }
  153. /* Compare node VAL1 with the type VAL2. This function is used by the
  154. hash table abstract_pending_vars. */
  155. bool
  156. abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
  157. {
  158. return (pat1->type == type2);
  159. }
  160. /* Hash table that maintains pending_abstract_type nodes, for which we still
  161. need to check for type abstractness. The key of the table is the type
  162. of the declaration. */
  163. static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
  164. static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
  165. /* This function is called after TYPE is completed, and will check if there
  166. are pending declarations for which we still need to verify the abstractness
  167. of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
  168. turned out to be incomplete. */
  169. void
  170. complete_type_check_abstract (tree type)
  171. {
  172. struct pending_abstract_type *pat;
  173. location_t cur_loc = input_location;
  174. gcc_assert (COMPLETE_TYPE_P (type));
  175. if (!abstract_pending_vars)
  176. return;
  177. /* Retrieve the list of pending declarations for this type. */
  178. pending_abstract_type **slot
  179. = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
  180. NO_INSERT);
  181. if (!slot)
  182. return;
  183. pat = *slot;
  184. gcc_assert (pat);
  185. /* If the type is not abstract, do not do anything. */
  186. if (CLASSTYPE_PURE_VIRTUALS (type))
  187. {
  188. struct pending_abstract_type *prev = 0, *next;
  189. /* Reverse the list to emit the errors in top-down order. */
  190. for (; pat; pat = next)
  191. {
  192. next = pat->next;
  193. pat->next = prev;
  194. prev = pat;
  195. }
  196. pat = prev;
  197. /* Go through the list, and call abstract_virtuals_error for each
  198. element: it will issue a diagnostic if the type is abstract. */
  199. while (pat)
  200. {
  201. gcc_assert (type == pat->type);
  202. /* Tweak input_location so that the diagnostic appears at the correct
  203. location. Notice that this is only needed if the decl is an
  204. IDENTIFIER_NODE. */
  205. input_location = pat->locus;
  206. abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
  207. tf_warning_or_error);
  208. pat = pat->next;
  209. }
  210. }
  211. abstract_pending_vars->clear_slot (slot);
  212. input_location = cur_loc;
  213. }
  214. /* If TYPE has abstract virtual functions, issue an error about trying
  215. to create an object of that type. DECL is the object declared, or
  216. NULL_TREE if the declaration is unavailable, in which case USE specifies
  217. the kind of invalid use. Returns 1 if an error occurred; zero if
  218. all was well. */
  219. static int
  220. abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
  221. tsubst_flags_t complain)
  222. {
  223. vec<tree, va_gc> *pure;
  224. /* This function applies only to classes. Any other entity can never
  225. be abstract. */
  226. if (!CLASS_TYPE_P (type))
  227. return 0;
  228. type = TYPE_MAIN_VARIANT (type);
  229. #if 0
  230. /* Instantiation here seems to be required by the standard,
  231. but breaks e.g. boost::bind. FIXME! */
  232. /* In SFINAE, non-N3276 context, force instantiation. */
  233. if (!(complain & (tf_error|tf_decltype)))
  234. complete_type (type);
  235. #endif
  236. /* If the type is incomplete, we register it within a hash table,
  237. so that we can check again once it is completed. This makes sense
  238. only for objects for which we have a declaration or at least a
  239. name. */
  240. if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
  241. {
  242. struct pending_abstract_type *pat;
  243. gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
  244. if (!abstract_pending_vars)
  245. abstract_pending_vars
  246. = hash_table<abstract_type_hasher>::create_ggc (31);
  247. pending_abstract_type **slot
  248. = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
  249. INSERT);
  250. pat = ggc_alloc<pending_abstract_type> ();
  251. pat->type = type;
  252. pat->decl = decl;
  253. pat->use = use;
  254. pat->locus = ((decl && DECL_P (decl))
  255. ? DECL_SOURCE_LOCATION (decl)
  256. : input_location);
  257. pat->next = *slot;
  258. *slot = pat;
  259. return 0;
  260. }
  261. if (!TYPE_SIZE (type))
  262. /* TYPE is being defined, and during that time
  263. CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
  264. return 0;
  265. pure = CLASSTYPE_PURE_VIRTUALS (type);
  266. if (!pure)
  267. return 0;
  268. if (!(complain & tf_error))
  269. return 1;
  270. if (decl)
  271. {
  272. if (VAR_P (decl))
  273. error ("cannot declare variable %q+D to be of abstract "
  274. "type %qT", decl, type);
  275. else if (TREE_CODE (decl) == PARM_DECL)
  276. {
  277. if (DECL_NAME (decl))
  278. error ("cannot declare parameter %q+D to be of abstract type %qT",
  279. decl, type);
  280. else
  281. error ("cannot declare parameter to be of abstract type %qT",
  282. type);
  283. }
  284. else if (TREE_CODE (decl) == FIELD_DECL)
  285. error ("cannot declare field %q+D to be of abstract type %qT",
  286. decl, type);
  287. else if (TREE_CODE (decl) == FUNCTION_DECL
  288. && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
  289. error ("invalid abstract return type for member function %q+#D", decl);
  290. else if (TREE_CODE (decl) == FUNCTION_DECL)
  291. error ("invalid abstract return type for function %q+#D", decl);
  292. else if (identifier_p (decl))
  293. /* Here we do not have location information. */
  294. error ("invalid abstract type %qT for %qE", type, decl);
  295. else
  296. error ("invalid abstract type for %q+D", decl);
  297. }
  298. else switch (use)
  299. {
  300. case ACU_ARRAY:
  301. error ("creating array of %qT, which is an abstract class type", type);
  302. break;
  303. case ACU_CAST:
  304. error ("invalid cast to abstract class type %qT", type);
  305. break;
  306. case ACU_NEW:
  307. error ("invalid new-expression of abstract class type %qT", type);
  308. break;
  309. case ACU_RETURN:
  310. error ("invalid abstract return type %qT", type);
  311. break;
  312. case ACU_PARM:
  313. error ("invalid abstract parameter type %qT", type);
  314. break;
  315. case ACU_THROW:
  316. error ("expression of abstract class type %qT cannot "
  317. "be used in throw-expression", type);
  318. break;
  319. case ACU_CATCH:
  320. error ("cannot declare catch parameter to be of abstract "
  321. "class type %qT", type);
  322. break;
  323. default:
  324. error ("cannot allocate an object of abstract type %qT", type);
  325. }
  326. /* Only go through this once. */
  327. if (pure->length ())
  328. {
  329. unsigned ix;
  330. tree fn;
  331. inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
  332. " because the following virtual functions are pure within %qT:",
  333. type);
  334. FOR_EACH_VEC_ELT (*pure, ix, fn)
  335. if (! DECL_CLONED_FUNCTION_P (fn)
  336. || DECL_COMPLETE_DESTRUCTOR_P (fn))
  337. inform (input_location, "\t%+#D", fn);
  338. /* Now truncate the vector. This leaves it non-null, so we know
  339. there are pure virtuals, but empty so we don't list them out
  340. again. */
  341. pure->truncate (0);
  342. }
  343. return 1;
  344. }
  345. int
  346. abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
  347. {
  348. return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
  349. }
  350. int
  351. abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
  352. tsubst_flags_t complain)
  353. {
  354. return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
  355. }
  356. /* Wrapper for the above function in the common case of wanting errors. */
  357. int
  358. abstract_virtuals_error (tree decl, tree type)
  359. {
  360. return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
  361. }
  362. int
  363. abstract_virtuals_error (abstract_class_use use, tree type)
  364. {
  365. return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
  366. }
  367. /* Print an inform about the declaration of the incomplete type TYPE. */
  368. void
  369. cxx_incomplete_type_inform (const_tree type)
  370. {
  371. if (!TYPE_MAIN_DECL (type))
  372. return;
  373. location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
  374. tree ptype = strip_top_quals (CONST_CAST_TREE (type));
  375. if (current_class_type
  376. && TYPE_BEING_DEFINED (current_class_type)
  377. && same_type_p (ptype, current_class_type))
  378. inform (loc, "definition of %q#T is not complete until "
  379. "the closing brace", ptype);
  380. else if (!TYPE_TEMPLATE_INFO (ptype))
  381. inform (loc, "forward declaration of %q#T", ptype);
  382. else
  383. inform (loc, "declaration of %q#T", ptype);
  384. }
  385. /* Print an error message for invalid use of an incomplete type.
  386. VALUE is the expression that was used (or 0 if that isn't known)
  387. and TYPE is the type that was invalid. DIAG_KIND indicates the
  388. type of diagnostic (see diagnostic.def). */
  389. void
  390. cxx_incomplete_type_diagnostic (const_tree value, const_tree type,
  391. diagnostic_t diag_kind)
  392. {
  393. bool is_decl = false, complained = false;
  394. gcc_assert (diag_kind == DK_WARNING
  395. || diag_kind == DK_PEDWARN
  396. || diag_kind == DK_ERROR);
  397. /* Avoid duplicate error message. */
  398. if (TREE_CODE (type) == ERROR_MARK)
  399. return;
  400. if (value != 0 && (VAR_P (value)
  401. || TREE_CODE (value) == PARM_DECL
  402. || TREE_CODE (value) == FIELD_DECL))
  403. {
  404. complained = emit_diagnostic (diag_kind, input_location, 0,
  405. "%q+D has incomplete type", value);
  406. is_decl = true;
  407. }
  408. retry:
  409. /* We must print an error message. Be clever about what it says. */
  410. switch (TREE_CODE (type))
  411. {
  412. case RECORD_TYPE:
  413. case UNION_TYPE:
  414. case ENUMERAL_TYPE:
  415. if (!is_decl)
  416. complained = emit_diagnostic (diag_kind, input_location, 0,
  417. "invalid use of incomplete type %q#T",
  418. type);
  419. if (complained)
  420. cxx_incomplete_type_inform (type);
  421. break;
  422. case VOID_TYPE:
  423. emit_diagnostic (diag_kind, input_location, 0,
  424. "invalid use of %qT", type);
  425. break;
  426. case ARRAY_TYPE:
  427. if (TYPE_DOMAIN (type))
  428. {
  429. type = TREE_TYPE (type);
  430. goto retry;
  431. }
  432. emit_diagnostic (diag_kind, input_location, 0,
  433. "invalid use of array with unspecified bounds");
  434. break;
  435. case OFFSET_TYPE:
  436. bad_member:
  437. {
  438. tree member = TREE_OPERAND (value, 1);
  439. if (is_overloaded_fn (member))
  440. member = get_first_fn (member);
  441. if (DECL_FUNCTION_MEMBER_P (member)
  442. && ! flag_ms_extensions)
  443. emit_diagnostic (diag_kind, input_location, 0,
  444. "invalid use of member function "
  445. "(did you forget the %<()%> ?)");
  446. else
  447. emit_diagnostic (diag_kind, input_location, 0,
  448. "invalid use of member "
  449. "(did you forget the %<&%> ?)");
  450. }
  451. break;
  452. case TEMPLATE_TYPE_PARM:
  453. if (is_auto (type))
  454. emit_diagnostic (diag_kind, input_location, 0,
  455. "invalid use of %<auto%>");
  456. else
  457. emit_diagnostic (diag_kind, input_location, 0,
  458. "invalid use of template type parameter %qT", type);
  459. break;
  460. case BOUND_TEMPLATE_TEMPLATE_PARM:
  461. emit_diagnostic (diag_kind, input_location, 0,
  462. "invalid use of template template parameter %qT",
  463. TYPE_NAME (type));
  464. break;
  465. case TYPENAME_TYPE:
  466. emit_diagnostic (diag_kind, input_location, 0,
  467. "invalid use of dependent type %qT", type);
  468. break;
  469. case LANG_TYPE:
  470. if (type == init_list_type_node)
  471. {
  472. emit_diagnostic (diag_kind, input_location, 0,
  473. "invalid use of brace-enclosed initializer list");
  474. break;
  475. }
  476. gcc_assert (type == unknown_type_node);
  477. if (value && TREE_CODE (value) == COMPONENT_REF)
  478. goto bad_member;
  479. else if (value && TREE_CODE (value) == ADDR_EXPR)
  480. emit_diagnostic (diag_kind, input_location, 0,
  481. "address of overloaded function with no contextual "
  482. "type information");
  483. else if (value && TREE_CODE (value) == OVERLOAD)
  484. emit_diagnostic (diag_kind, input_location, 0,
  485. "overloaded function with no contextual type information");
  486. else
  487. emit_diagnostic (diag_kind, input_location, 0,
  488. "insufficient contextual information to determine type");
  489. break;
  490. default:
  491. gcc_unreachable ();
  492. }
  493. }
  494. /* Backward-compatibility interface to incomplete_type_diagnostic;
  495. required by ../tree.c. */
  496. #undef cxx_incomplete_type_error
  497. void
  498. cxx_incomplete_type_error (const_tree value, const_tree type)
  499. {
  500. cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
  501. }
  502. /* The recursive part of split_nonconstant_init. DEST is an lvalue
  503. expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
  504. Return true if the whole of the value was initialized by the
  505. generated statements. */
  506. static bool
  507. split_nonconstant_init_1 (tree dest, tree init)
  508. {
  509. unsigned HOST_WIDE_INT idx;
  510. tree field_index, value;
  511. tree type = TREE_TYPE (dest);
  512. tree inner_type = NULL;
  513. bool array_type_p = false;
  514. bool complete_p = true;
  515. HOST_WIDE_INT num_split_elts = 0;
  516. switch (TREE_CODE (type))
  517. {
  518. case ARRAY_TYPE:
  519. inner_type = TREE_TYPE (type);
  520. array_type_p = true;
  521. if ((TREE_SIDE_EFFECTS (init)
  522. && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
  523. || array_of_runtime_bound_p (type))
  524. {
  525. /* For an array, we only need/want a single cleanup region rather
  526. than one per element. */
  527. tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
  528. tf_warning_or_error);
  529. add_stmt (code);
  530. return true;
  531. }
  532. /* FALLTHRU */
  533. case RECORD_TYPE:
  534. case UNION_TYPE:
  535. case QUAL_UNION_TYPE:
  536. FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
  537. field_index, value)
  538. {
  539. /* The current implementation of this algorithm assumes that
  540. the field was set for all the elements. This is usually done
  541. by process_init_constructor. */
  542. gcc_assert (field_index);
  543. if (!array_type_p)
  544. inner_type = TREE_TYPE (field_index);
  545. if (TREE_CODE (value) == CONSTRUCTOR)
  546. {
  547. tree sub;
  548. if (array_type_p)
  549. sub = build4 (ARRAY_REF, inner_type, dest, field_index,
  550. NULL_TREE, NULL_TREE);
  551. else
  552. sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
  553. NULL_TREE);
  554. if (!split_nonconstant_init_1 (sub, value))
  555. complete_p = false;
  556. else
  557. CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
  558. num_split_elts++;
  559. }
  560. else if (!initializer_constant_valid_p (value, inner_type))
  561. {
  562. tree code;
  563. tree sub;
  564. /* FIXME: Ordered removal is O(1) so the whole function is
  565. worst-case quadratic. This could be fixed using an aside
  566. bitmap to record which elements must be removed and remove
  567. them all at the same time. Or by merging
  568. split_non_constant_init into process_init_constructor_array,
  569. that is separating constants from non-constants while building
  570. the vector. */
  571. CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
  572. --idx;
  573. if (TREE_CODE (field_index) == RANGE_EXPR)
  574. {
  575. /* Use build_vec_init to initialize a range. */
  576. tree low = TREE_OPERAND (field_index, 0);
  577. tree hi = TREE_OPERAND (field_index, 1);
  578. sub = build4 (ARRAY_REF, inner_type, dest, low,
  579. NULL_TREE, NULL_TREE);
  580. sub = cp_build_addr_expr (sub, tf_warning_or_error);
  581. tree max = size_binop (MINUS_EXPR, hi, low);
  582. code = build_vec_init (sub, max, value, false, 0,
  583. tf_warning_or_error);
  584. add_stmt (code);
  585. if (tree_fits_shwi_p (max))
  586. num_split_elts += tree_to_shwi (max);
  587. }
  588. else
  589. {
  590. if (array_type_p)
  591. sub = build4 (ARRAY_REF, inner_type, dest, field_index,
  592. NULL_TREE, NULL_TREE);
  593. else
  594. sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
  595. NULL_TREE);
  596. code = build2 (INIT_EXPR, inner_type, sub, value);
  597. code = build_stmt (input_location, EXPR_STMT, code);
  598. code = maybe_cleanup_point_expr_void (code);
  599. add_stmt (code);
  600. if (type_build_dtor_call (inner_type))
  601. {
  602. code = (build_special_member_call
  603. (sub, complete_dtor_identifier, NULL, inner_type,
  604. LOOKUP_NORMAL, tf_warning_or_error));
  605. if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type))
  606. finish_eh_cleanup (code);
  607. }
  608. }
  609. num_split_elts++;
  610. }
  611. }
  612. break;
  613. case VECTOR_TYPE:
  614. if (!initializer_constant_valid_p (init, type))
  615. {
  616. tree code;
  617. tree cons = copy_node (init);
  618. CONSTRUCTOR_ELTS (init) = NULL;
  619. code = build2 (MODIFY_EXPR, type, dest, cons);
  620. code = build_stmt (input_location, EXPR_STMT, code);
  621. add_stmt (code);
  622. num_split_elts += CONSTRUCTOR_NELTS (init);
  623. }
  624. break;
  625. default:
  626. gcc_unreachable ();
  627. }
  628. /* The rest of the initializer is now a constant. */
  629. TREE_CONSTANT (init) = 1;
  630. return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
  631. num_split_elts, inner_type);
  632. }
  633. /* A subroutine of store_init_value. Splits non-constant static
  634. initializer INIT into a constant part and generates code to
  635. perform the non-constant part of the initialization to DEST.
  636. Returns the code for the runtime init. */
  637. tree
  638. split_nonconstant_init (tree dest, tree init)
  639. {
  640. tree code;
  641. if (TREE_CODE (init) == TARGET_EXPR)
  642. init = TARGET_EXPR_INITIAL (init);
  643. if (TREE_CODE (init) == CONSTRUCTOR)
  644. {
  645. code = push_stmt_list ();
  646. if (split_nonconstant_init_1 (dest, init))
  647. init = NULL_TREE;
  648. code = pop_stmt_list (code);
  649. DECL_INITIAL (dest) = init;
  650. TREE_READONLY (dest) = 0;
  651. }
  652. else
  653. code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
  654. return code;
  655. }
  656. /* Perform appropriate conversions on the initial value of a variable,
  657. store it in the declaration DECL,
  658. and print any error messages that are appropriate.
  659. If the init is invalid, store an ERROR_MARK.
  660. C++: Note that INIT might be a TREE_LIST, which would mean that it is
  661. a base class initializer for some aggregate type, hopefully compatible
  662. with DECL. If INIT is a single element, and DECL is an aggregate
  663. type, we silently convert INIT into a TREE_LIST, allowing a constructor
  664. to be called.
  665. If INIT is a TREE_LIST and there is no constructor, turn INIT
  666. into a CONSTRUCTOR and use standard initialization techniques.
  667. Perhaps a warning should be generated?
  668. Returns code to be executed if initialization could not be performed
  669. for static variable. In that case, caller must emit the code. */
  670. tree
  671. store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
  672. {
  673. tree value, type;
  674. /* If variable's type was invalidly declared, just ignore it. */
  675. type = TREE_TYPE (decl);
  676. if (TREE_CODE (type) == ERROR_MARK)
  677. return NULL_TREE;
  678. if (MAYBE_CLASS_TYPE_P (type))
  679. {
  680. if (TREE_CODE (init) == TREE_LIST)
  681. {
  682. error ("constructor syntax used, but no constructor declared "
  683. "for type %qT", type);
  684. init = build_constructor_from_list (init_list_type_node, nreverse (init));
  685. }
  686. }
  687. /* End of special C++ code. */
  688. if (flags & LOOKUP_ALREADY_DIGESTED)
  689. value = init;
  690. else
  691. /* Digest the specified initializer into an expression. */
  692. value = digest_init_flags (type, init, flags);
  693. value = extend_ref_init_temps (decl, value, cleanups);
  694. /* In C++11 constant expression is a semantic, not syntactic, property.
  695. In C++98, make sure that what we thought was a constant expression at
  696. template definition time is still constant and otherwise perform this
  697. as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
  698. if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
  699. {
  700. bool const_init;
  701. value = instantiate_non_dependent_expr (value);
  702. if (DECL_DECLARED_CONSTEXPR_P (decl)
  703. || DECL_IN_AGGR_P (decl))
  704. {
  705. /* Diagnose a non-constant initializer for constexpr. */
  706. if (processing_template_decl
  707. && !require_potential_constant_expression (value))
  708. value = error_mark_node;
  709. else
  710. value = cxx_constant_value (value, decl);
  711. }
  712. value = maybe_constant_init (value, decl);
  713. if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
  714. /* Poison this CONSTRUCTOR so it can't be copied to another
  715. constexpr variable. */
  716. CONSTRUCTOR_MUTABLE_POISON (value) = true;
  717. const_init = (reduced_constant_expression_p (value)
  718. || error_operand_p (value));
  719. DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
  720. TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
  721. }
  722. if (cxx_dialect >= cxx14 && CLASS_TYPE_P (strip_array_types (type)))
  723. /* Handle aggregate NSDMI in non-constant initializers, too. */
  724. value = replace_placeholders (value, decl);
  725. /* If the initializer is not a constant, fill in DECL_INITIAL with
  726. the bits that are constant, and then return an expression that
  727. will perform the dynamic initialization. */
  728. if (value != error_mark_node
  729. && (TREE_SIDE_EFFECTS (value)
  730. || array_of_runtime_bound_p (type)
  731. || ! reduced_constant_expression_p (value)))
  732. return split_nonconstant_init (decl, value);
  733. /* If the value is a constant, just put it in DECL_INITIAL. If DECL
  734. is an automatic variable, the middle end will turn this into a
  735. dynamic initialization later. */
  736. DECL_INITIAL (decl) = value;
  737. return NULL_TREE;
  738. }
  739. /* Give diagnostic about narrowing conversions within { }. */
  740. bool
  741. check_narrowing (tree type, tree init, tsubst_flags_t complain)
  742. {
  743. tree ftype = unlowered_expr_type (init);
  744. bool ok = true;
  745. REAL_VALUE_TYPE d;
  746. if (((!warn_narrowing || !(complain & tf_warning))
  747. && cxx_dialect == cxx98)
  748. || !ARITHMETIC_TYPE_P (type))
  749. return ok;
  750. if (BRACE_ENCLOSED_INITIALIZER_P (init)
  751. && TREE_CODE (type) == COMPLEX_TYPE)
  752. {
  753. tree elttype = TREE_TYPE (type);
  754. if (CONSTRUCTOR_NELTS (init) > 0)
  755. ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
  756. complain);
  757. if (CONSTRUCTOR_NELTS (init) > 1)
  758. ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
  759. complain);
  760. return ok;
  761. }
  762. init = fold_non_dependent_expr (init);
  763. if (TREE_CODE (type) == INTEGER_TYPE
  764. && TREE_CODE (ftype) == REAL_TYPE)
  765. ok = false;
  766. else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
  767. && CP_INTEGRAL_TYPE_P (type))
  768. {
  769. if (TREE_CODE (ftype) == ENUMERAL_TYPE)
  770. /* Check for narrowing based on the values of the enumeration. */
  771. ftype = ENUM_UNDERLYING_TYPE (ftype);
  772. if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
  773. TYPE_MAX_VALUE (ftype))
  774. || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
  775. TYPE_MIN_VALUE (type)))
  776. && (TREE_CODE (init) != INTEGER_CST
  777. || !int_fits_type_p (init, type)))
  778. ok = false;
  779. }
  780. else if (TREE_CODE (ftype) == REAL_TYPE
  781. && TREE_CODE (type) == REAL_TYPE)
  782. {
  783. if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
  784. {
  785. if (TREE_CODE (init) == REAL_CST)
  786. {
  787. /* Issue 703: Loss of precision is OK as long as the value is
  788. within the representable range of the new type. */
  789. REAL_VALUE_TYPE r;
  790. d = TREE_REAL_CST (init);
  791. real_convert (&r, TYPE_MODE (type), &d);
  792. if (real_isinf (&r))
  793. ok = false;
  794. }
  795. else
  796. ok = false;
  797. }
  798. }
  799. else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
  800. && TREE_CODE (type) == REAL_TYPE)
  801. {
  802. ok = false;
  803. if (TREE_CODE (init) == INTEGER_CST)
  804. {
  805. d = real_value_from_int_cst (0, init);
  806. if (exact_real_truncate (TYPE_MODE (type), &d))
  807. ok = true;
  808. }
  809. }
  810. if (!ok)
  811. {
  812. if (cxx_dialect == cxx98)
  813. warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
  814. "narrowing conversion of %qE from %qT to %qT inside { } "
  815. "is ill-formed in C++11", init, ftype, type);
  816. else if (!TREE_CONSTANT (init))
  817. {
  818. if (complain & tf_warning_or_error)
  819. {
  820. pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
  821. "narrowing conversion of %qE from %qT to %qT inside { }",
  822. init, ftype, type);
  823. ok = true;
  824. }
  825. }
  826. else if (complain & tf_error)
  827. {
  828. int savederrorcount = errorcount;
  829. global_dc->pedantic_errors = 1;
  830. pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
  831. "narrowing conversion of %qE from %qT to %qT "
  832. "inside { }", init, ftype, type);
  833. if (errorcount == savederrorcount)
  834. ok = true;
  835. global_dc->pedantic_errors = flag_pedantic_errors;
  836. }
  837. }
  838. return cxx_dialect == cxx98 || ok;
  839. }
  840. /* Process the initializer INIT for a variable of type TYPE, emitting
  841. diagnostics for invalid initializers and converting the initializer as
  842. appropriate.
  843. For aggregate types, it assumes that reshape_init has already run, thus the
  844. initializer will have the right shape (brace elision has been undone).
  845. NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
  846. static tree
  847. digest_init_r (tree type, tree init, bool nested, int flags,
  848. tsubst_flags_t complain)
  849. {
  850. enum tree_code code = TREE_CODE (type);
  851. if (error_operand_p (init))
  852. return error_mark_node;
  853. gcc_assert (init);
  854. /* We must strip the outermost array type when completing the type,
  855. because the its bounds might be incomplete at the moment. */
  856. if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
  857. ? TREE_TYPE (type) : type, NULL_TREE,
  858. complain))
  859. return error_mark_node;
  860. /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
  861. (g++.old-deja/g++.law/casts2.C). */
  862. if (TREE_CODE (init) == NON_LVALUE_EXPR)
  863. init = TREE_OPERAND (init, 0);
  864. /* Initialization of an array of chars from a string constant. The initializer
  865. can be optionally enclosed in braces, but reshape_init has already removed
  866. them if they were present. */
  867. if (code == ARRAY_TYPE)
  868. {
  869. tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  870. if (char_type_p (typ1)
  871. /*&& init */
  872. && TREE_CODE (init) == STRING_CST)
  873. {
  874. tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
  875. if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
  876. {
  877. if (char_type != char_type_node)
  878. {
  879. if (complain & tf_error)
  880. error ("char-array initialized from wide string");
  881. return error_mark_node;
  882. }
  883. }
  884. else
  885. {
  886. if (char_type == char_type_node)
  887. {
  888. if (complain & tf_error)
  889. error ("int-array initialized from non-wide string");
  890. return error_mark_node;
  891. }
  892. else if (char_type != typ1)
  893. {
  894. if (complain & tf_error)
  895. error ("int-array initialized from incompatible "
  896. "wide string");
  897. return error_mark_node;
  898. }
  899. }
  900. if (type != TREE_TYPE (init))
  901. {
  902. init = copy_node (init);
  903. TREE_TYPE (init) = type;
  904. }
  905. if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
  906. {
  907. int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
  908. size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  909. /* In C it is ok to subtract 1 from the length of the string
  910. because it's ok to ignore the terminating null char that is
  911. counted in the length of the constant, but in C++ this would
  912. be invalid. */
  913. if (size < TREE_STRING_LENGTH (init))
  914. permerror (input_location, "initializer-string for array "
  915. "of chars is too long");
  916. }
  917. return init;
  918. }
  919. }
  920. /* Handle scalar types (including conversions) and references. */
  921. if ((TREE_CODE (type) != COMPLEX_TYPE
  922. || BRACE_ENCLOSED_INITIALIZER_P (init))
  923. && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
  924. {
  925. tree *exp;
  926. if (nested)
  927. flags |= LOOKUP_NO_NARROWING;
  928. init = convert_for_initialization (0, type, init, flags,
  929. ICR_INIT, NULL_TREE, 0,
  930. complain);
  931. exp = &init;
  932. /* Skip any conversions since we'll be outputting the underlying
  933. constant. */
  934. while (CONVERT_EXPR_P (*exp)
  935. || TREE_CODE (*exp) == NON_LVALUE_EXPR)
  936. exp = &TREE_OPERAND (*exp, 0);
  937. *exp = cplus_expand_constant (*exp);
  938. return init;
  939. }
  940. /* Come here only for aggregates: records, arrays, unions, complex numbers
  941. and vectors. */
  942. gcc_assert (TREE_CODE (type) == ARRAY_TYPE
  943. || TREE_CODE (type) == VECTOR_TYPE
  944. || TREE_CODE (type) == RECORD_TYPE
  945. || TREE_CODE (type) == UNION_TYPE
  946. || TREE_CODE (type) == COMPLEX_TYPE);
  947. /* "If T is a class type and the initializer list has a single
  948. element of type cv U, where U is T or a class derived from T,
  949. the object is initialized from that element." */
  950. if (cxx_dialect >= cxx11
  951. && BRACE_ENCLOSED_INITIALIZER_P (init)
  952. && CONSTRUCTOR_NELTS (init) == 1
  953. && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)))
  954. {
  955. tree elt = CONSTRUCTOR_ELT (init, 0)->value;
  956. if (reference_related_p (type, TREE_TYPE (elt)))
  957. init = elt;
  958. }
  959. if (BRACE_ENCLOSED_INITIALIZER_P (init)
  960. && !TYPE_NON_AGGREGATE_CLASS (type))
  961. return process_init_constructor (type, init, complain);
  962. else
  963. {
  964. if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
  965. {
  966. if (complain & tf_error)
  967. error ("cannot initialize aggregate of type %qT with "
  968. "a compound literal", type);
  969. return error_mark_node;
  970. }
  971. if (TREE_CODE (type) == ARRAY_TYPE
  972. && !BRACE_ENCLOSED_INITIALIZER_P (init))
  973. {
  974. /* Allow the result of build_array_copy and of
  975. build_value_init_noctor. */
  976. if ((TREE_CODE (init) == VEC_INIT_EXPR
  977. || TREE_CODE (init) == CONSTRUCTOR)
  978. && (same_type_ignoring_top_level_qualifiers_p
  979. (type, TREE_TYPE (init))))
  980. return init;
  981. if (complain & tf_error)
  982. error ("array must be initialized with a brace-enclosed"
  983. " initializer");
  984. return error_mark_node;
  985. }
  986. return convert_for_initialization (NULL_TREE, type, init,
  987. flags,
  988. ICR_INIT, NULL_TREE, 0,
  989. complain);
  990. }
  991. }
  992. tree
  993. digest_init (tree type, tree init, tsubst_flags_t complain)
  994. {
  995. return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
  996. }
  997. tree
  998. digest_init_flags (tree type, tree init, int flags)
  999. {
  1000. return digest_init_r (type, init, false, flags, tf_warning_or_error);
  1001. }
  1002. /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
  1003. tree
  1004. digest_nsdmi_init (tree decl, tree init)
  1005. {
  1006. gcc_assert (TREE_CODE (decl) == FIELD_DECL);
  1007. int flags = LOOKUP_IMPLICIT;
  1008. if (DIRECT_LIST_INIT_P (init))
  1009. flags = LOOKUP_NORMAL;
  1010. init = digest_init_flags (TREE_TYPE (decl), init, flags);
  1011. if (TREE_CODE (init) == TARGET_EXPR)
  1012. /* This represents the whole initialization. */
  1013. TARGET_EXPR_DIRECT_INIT_P (init) = true;
  1014. return init;
  1015. }
  1016. /* Set of flags used within process_init_constructor to describe the
  1017. initializers. */
  1018. #define PICFLAG_ERRONEOUS 1
  1019. #define PICFLAG_NOT_ALL_CONSTANT 2
  1020. #define PICFLAG_NOT_ALL_SIMPLE 4
  1021. #define PICFLAG_SIDE_EFFECTS 8
  1022. /* Given an initializer INIT, return the flag (PICFLAG_*) which better
  1023. describe it. */
  1024. static int
  1025. picflag_from_initializer (tree init)
  1026. {
  1027. if (init == error_mark_node)
  1028. return PICFLAG_ERRONEOUS;
  1029. else if (!TREE_CONSTANT (init))
  1030. {
  1031. if (TREE_SIDE_EFFECTS (init))
  1032. return PICFLAG_SIDE_EFFECTS;
  1033. else
  1034. return PICFLAG_NOT_ALL_CONSTANT;
  1035. }
  1036. else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
  1037. return PICFLAG_NOT_ALL_SIMPLE;
  1038. return 0;
  1039. }
  1040. /* Adjust INIT for going into a CONSTRUCTOR. */
  1041. static tree
  1042. massage_init_elt (tree type, tree init, tsubst_flags_t complain)
  1043. {
  1044. init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
  1045. /* Strip a simple TARGET_EXPR when we know this is an initializer. */
  1046. if (TREE_CODE (init) == TARGET_EXPR
  1047. && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
  1048. init = TARGET_EXPR_INITIAL (init);
  1049. /* When we defer constant folding within a statement, we may want to
  1050. defer this folding as well. */
  1051. tree t = fold_non_dependent_expr (init);
  1052. t = maybe_constant_init (t);
  1053. if (TREE_CONSTANT (t))
  1054. init = t;
  1055. return init;
  1056. }
  1057. /* Subroutine of process_init_constructor, which will process an initializer
  1058. INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
  1059. which describe the initializers. */
  1060. static int
  1061. process_init_constructor_array (tree type, tree init,
  1062. tsubst_flags_t complain)
  1063. {
  1064. unsigned HOST_WIDE_INT i, len = 0;
  1065. int flags = 0;
  1066. bool unbounded = false;
  1067. constructor_elt *ce;
  1068. vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
  1069. gcc_assert (TREE_CODE (type) == ARRAY_TYPE
  1070. || TREE_CODE (type) == VECTOR_TYPE);
  1071. if (TREE_CODE (type) == ARRAY_TYPE)
  1072. {
  1073. tree domain = TYPE_DOMAIN (type);
  1074. if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
  1075. len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
  1076. - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
  1077. TYPE_PRECISION (TREE_TYPE (domain)),
  1078. TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
  1079. else
  1080. unbounded = true; /* Take as many as there are. */
  1081. }
  1082. else
  1083. /* Vectors are like simple fixed-size arrays. */
  1084. len = TYPE_VECTOR_SUBPARTS (type);
  1085. /* There must not be more initializers than needed. */
  1086. if (!unbounded && vec_safe_length (v) > len)
  1087. {
  1088. if (complain & tf_error)
  1089. error ("too many initializers for %qT", type);
  1090. else
  1091. return PICFLAG_ERRONEOUS;
  1092. }
  1093. FOR_EACH_VEC_SAFE_ELT (v, i, ce)
  1094. {
  1095. if (ce->index)
  1096. {
  1097. gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
  1098. if (compare_tree_int (ce->index, i) != 0)
  1099. {
  1100. ce->value = error_mark_node;
  1101. sorry ("non-trivial designated initializers not supported");
  1102. }
  1103. }
  1104. else
  1105. ce->index = size_int (i);
  1106. gcc_assert (ce->value);
  1107. ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
  1108. if (ce->value != error_mark_node)
  1109. gcc_assert (same_type_ignoring_top_level_qualifiers_p
  1110. (TREE_TYPE (type), TREE_TYPE (ce->value)));
  1111. flags |= picflag_from_initializer (ce->value);
  1112. }
  1113. /* No more initializers. If the array is unbounded, we are done. Otherwise,
  1114. we must add initializers ourselves. */
  1115. if (!unbounded)
  1116. for (; i < len; ++i)
  1117. {
  1118. tree next;
  1119. if (type_build_ctor_call (TREE_TYPE (type)))
  1120. {
  1121. /* If this type needs constructors run for default-initialization,
  1122. we can't rely on the back end to do it for us, so make the
  1123. initialization explicit by list-initializing from T{}. */
  1124. next = build_constructor (init_list_type_node, NULL);
  1125. CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
  1126. next = massage_init_elt (TREE_TYPE (type), next, complain);
  1127. if (initializer_zerop (next))
  1128. /* The default zero-initialization is fine for us; don't
  1129. add anything to the CONSTRUCTOR. */
  1130. next = NULL_TREE;
  1131. }
  1132. else if (!zero_init_p (TREE_TYPE (type)))
  1133. next = build_zero_init (TREE_TYPE (type),
  1134. /*nelts=*/NULL_TREE,
  1135. /*static_storage_p=*/false);
  1136. else
  1137. /* The default zero-initialization is fine for us; don't
  1138. add anything to the CONSTRUCTOR. */
  1139. next = NULL_TREE;
  1140. if (next)
  1141. {
  1142. flags |= picflag_from_initializer (next);
  1143. CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
  1144. }
  1145. }
  1146. CONSTRUCTOR_ELTS (init) = v;
  1147. return flags;
  1148. }
  1149. /* Subroutine of process_init_constructor, which will process an initializer
  1150. INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
  1151. the initializers. */
  1152. static int
  1153. process_init_constructor_record (tree type, tree init,
  1154. tsubst_flags_t complain)
  1155. {
  1156. vec<constructor_elt, va_gc> *v = NULL;
  1157. tree field;
  1158. int skipped = 0;
  1159. gcc_assert (TREE_CODE (type) == RECORD_TYPE);
  1160. gcc_assert (!CLASSTYPE_VBASECLASSES (type));
  1161. gcc_assert (!TYPE_BINFO (type)
  1162. || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
  1163. gcc_assert (!TYPE_POLYMORPHIC_P (type));
  1164. restart:
  1165. int flags = 0;
  1166. unsigned HOST_WIDE_INT idx = 0;
  1167. /* Generally, we will always have an index for each initializer (which is
  1168. a FIELD_DECL, put by reshape_init), but compound literals don't go trough
  1169. reshape_init. So we need to handle both cases. */
  1170. for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
  1171. {
  1172. tree next;
  1173. tree type;
  1174. if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
  1175. continue;
  1176. if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
  1177. continue;
  1178. /* If this is a bitfield, first convert to the declared type. */
  1179. type = TREE_TYPE (field);
  1180. if (DECL_BIT_FIELD_TYPE (field))
  1181. type = DECL_BIT_FIELD_TYPE (field);
  1182. if (type == error_mark_node)
  1183. return PICFLAG_ERRONEOUS;
  1184. if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
  1185. {
  1186. constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
  1187. if (ce->index)
  1188. {
  1189. /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
  1190. latter case can happen in templates where lookup has to be
  1191. deferred. */
  1192. gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
  1193. || identifier_p (ce->index));
  1194. if (ce->index != field
  1195. && ce->index != DECL_NAME (field))
  1196. {
  1197. ce->value = error_mark_node;
  1198. sorry ("non-trivial designated initializers not supported");
  1199. }
  1200. }
  1201. gcc_assert (ce->value);
  1202. next = massage_init_elt (type, ce->value, complain);
  1203. ++idx;
  1204. }
  1205. else if (DECL_INITIAL (field))
  1206. {
  1207. if (skipped > 0)
  1208. {
  1209. /* We're using an NSDMI past a field with implicit
  1210. zero-init. Go back and make it explicit. */
  1211. skipped = -1;
  1212. vec_safe_truncate (v, 0);
  1213. goto restart;
  1214. }
  1215. /* C++14 aggregate NSDMI. */
  1216. next = get_nsdmi (field, /*ctor*/false);
  1217. }
  1218. else if (type_build_ctor_call (TREE_TYPE (field)))
  1219. {
  1220. /* If this type needs constructors run for
  1221. default-initialization, we can't rely on the back end to do it
  1222. for us, so build up TARGET_EXPRs. If the type in question is
  1223. a class, just build one up; if it's an array, recurse. */
  1224. next = build_constructor (init_list_type_node, NULL);
  1225. /* Call this direct-initialization pending DR 1518 resolution so
  1226. that explicit default ctors don't break valid C++03 code. */
  1227. CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
  1228. next = massage_init_elt (TREE_TYPE (field), next, complain);
  1229. /* Warn when some struct elements are implicitly initialized. */
  1230. if ((complain & tf_warning)
  1231. && !EMPTY_CONSTRUCTOR_P (init))
  1232. warning (OPT_Wmissing_field_initializers,
  1233. "missing initializer for member %qD", field);
  1234. }
  1235. else
  1236. {
  1237. if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
  1238. {
  1239. if (complain & tf_error)
  1240. error ("member %qD is uninitialized reference", field);
  1241. else
  1242. return PICFLAG_ERRONEOUS;
  1243. }
  1244. else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
  1245. {
  1246. if (complain & tf_error)
  1247. error ("member %qD with uninitialized reference fields", field);
  1248. else
  1249. return PICFLAG_ERRONEOUS;
  1250. }
  1251. /* Warn when some struct elements are implicitly initialized
  1252. to zero. */
  1253. if ((complain & tf_warning)
  1254. && !EMPTY_CONSTRUCTOR_P (init))
  1255. warning (OPT_Wmissing_field_initializers,
  1256. "missing initializer for member %qD", field);
  1257. if (!zero_init_p (TREE_TYPE (field))
  1258. || skipped < 0)
  1259. next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
  1260. /*static_storage_p=*/false);
  1261. else
  1262. {
  1263. /* The default zero-initialization is fine for us; don't
  1264. add anything to the CONSTRUCTOR. */
  1265. skipped = 1;
  1266. continue;
  1267. }
  1268. }
  1269. /* If this is a bitfield, now convert to the lowered type. */
  1270. if (type != TREE_TYPE (field))
  1271. next = cp_convert_and_check (TREE_TYPE (field), next, complain);
  1272. flags |= picflag_from_initializer (next);
  1273. CONSTRUCTOR_APPEND_ELT (v, field, next);
  1274. }
  1275. if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
  1276. {
  1277. if (complain & tf_error)
  1278. error ("too many initializers for %qT", type);
  1279. else
  1280. return PICFLAG_ERRONEOUS;
  1281. }
  1282. CONSTRUCTOR_ELTS (init) = v;
  1283. return flags;
  1284. }
  1285. /* Subroutine of process_init_constructor, which will process a single
  1286. initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
  1287. which describe the initializer. */
  1288. static int
  1289. process_init_constructor_union (tree type, tree init,
  1290. tsubst_flags_t complain)
  1291. {
  1292. constructor_elt *ce;
  1293. int len;
  1294. /* If the initializer was empty, use default zero initialization. */
  1295. if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
  1296. return 0;
  1297. len = CONSTRUCTOR_ELTS (init)->length ();
  1298. if (len > 1)
  1299. {
  1300. if (!(complain & tf_error))
  1301. return PICFLAG_ERRONEOUS;
  1302. error ("too many initializers for %qT", type);
  1303. CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
  1304. }
  1305. ce = &(*CONSTRUCTOR_ELTS (init))[0];
  1306. /* If this element specifies a field, initialize via that field. */
  1307. if (ce->index)
  1308. {
  1309. if (TREE_CODE (ce->index) == FIELD_DECL)
  1310. ;
  1311. else if (identifier_p (ce->index))
  1312. {
  1313. /* This can happen within a cast, see g++.dg/opt/cse2.C. */
  1314. tree name = ce->index;
  1315. tree field;
  1316. for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  1317. if (DECL_NAME (field) == name)
  1318. break;
  1319. if (!field)
  1320. {
  1321. if (complain & tf_error)
  1322. error ("no field %qD found in union being initialized",
  1323. field);
  1324. ce->value = error_mark_node;
  1325. }
  1326. ce->index = field;
  1327. }
  1328. else
  1329. {
  1330. gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
  1331. || TREE_CODE (ce->index) == RANGE_EXPR);
  1332. if (complain & tf_error)
  1333. error ("index value instead of field name in union initializer");
  1334. ce->value = error_mark_node;
  1335. }
  1336. }
  1337. else
  1338. {
  1339. /* Find the first named field. ANSI decided in September 1990
  1340. that only named fields count here. */
  1341. tree field = TYPE_FIELDS (type);
  1342. while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
  1343. field = TREE_CHAIN (field);
  1344. if (field == NULL_TREE)
  1345. {
  1346. if (complain & tf_error)
  1347. error ("too many initializers for %qT", type);
  1348. ce->value = error_mark_node;
  1349. }
  1350. ce->index = field;
  1351. }
  1352. if (ce->value && ce->value != error_mark_node)
  1353. ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
  1354. return picflag_from_initializer (ce->value);
  1355. }
  1356. /* Process INIT, a constructor for a variable of aggregate type TYPE. The
  1357. constructor is a brace-enclosed initializer, and will be modified in-place.
  1358. Each element is converted to the right type through digest_init, and
  1359. missing initializers are added following the language rules (zero-padding,
  1360. etc.).
  1361. After the execution, the initializer will have TREE_CONSTANT if all elts are
  1362. constant, and TREE_STATIC set if, in addition, all elts are simple enough
  1363. constants that the assembler and linker can compute them.
  1364. The function returns the initializer itself, or error_mark_node in case
  1365. of error. */
  1366. static tree
  1367. process_init_constructor (tree type, tree init, tsubst_flags_t complain)
  1368. {
  1369. int flags;
  1370. gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
  1371. if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
  1372. flags = process_init_constructor_array (type, init, complain);
  1373. else if (TREE_CODE (type) == RECORD_TYPE)
  1374. flags = process_init_constructor_record (type, init, complain);
  1375. else if (TREE_CODE (type) == UNION_TYPE)
  1376. flags = process_init_constructor_union (type, init, complain);
  1377. else
  1378. gcc_unreachable ();
  1379. if (flags & PICFLAG_ERRONEOUS)
  1380. return error_mark_node;
  1381. TREE_TYPE (init) = type;
  1382. if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
  1383. cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
  1384. if (flags & PICFLAG_SIDE_EFFECTS)
  1385. {
  1386. TREE_CONSTANT (init) = false;
  1387. TREE_SIDE_EFFECTS (init) = true;
  1388. }
  1389. else if (flags & PICFLAG_NOT_ALL_CONSTANT)
  1390. /* Make sure TREE_CONSTANT isn't set from build_constructor. */
  1391. TREE_CONSTANT (init) = false;
  1392. else
  1393. {
  1394. TREE_CONSTANT (init) = 1;
  1395. if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
  1396. TREE_STATIC (init) = 1;
  1397. }
  1398. return init;
  1399. }
  1400. /* Given a structure or union value DATUM, construct and return
  1401. the structure or union component which results from narrowing
  1402. that value to the base specified in BASETYPE. For example, given the
  1403. hierarchy
  1404. class L { int ii; };
  1405. class A : L { ... };
  1406. class B : L { ... };
  1407. class C : A, B { ... };
  1408. and the declaration
  1409. C x;
  1410. then the expression
  1411. x.A::ii refers to the ii member of the L part of
  1412. the A part of the C object named by X. In this case,
  1413. DATUM would be x, and BASETYPE would be A.
  1414. I used to think that this was nonconformant, that the standard specified
  1415. that first we look up ii in A, then convert x to an L& and pull out the
  1416. ii part. But in fact, it does say that we convert x to an A&; A here
  1417. is known as the "naming class". (jason 2000-12-19)
  1418. BINFO_P points to a variable initialized either to NULL_TREE or to the
  1419. binfo for the specific base subobject we want to convert to. */
  1420. tree
  1421. build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
  1422. {
  1423. tree binfo;
  1424. if (datum == error_mark_node)
  1425. return error_mark_node;
  1426. if (*binfo_p)
  1427. binfo = *binfo_p;
  1428. else
  1429. binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
  1430. NULL, tf_warning_or_error);
  1431. if (!binfo || binfo == error_mark_node)
  1432. {
  1433. *binfo_p = NULL_TREE;
  1434. if (!binfo)
  1435. error_not_base_type (basetype, TREE_TYPE (datum));
  1436. return error_mark_node;
  1437. }
  1438. *binfo_p = binfo;
  1439. return build_base_path (PLUS_EXPR, datum, binfo, 1,
  1440. tf_warning_or_error);
  1441. }
  1442. /* Build a reference to an object specified by the C++ `->' operator.
  1443. Usually this just involves dereferencing the object, but if the
  1444. `->' operator is overloaded, then such overloads must be
  1445. performed until an object which does not have the `->' operator
  1446. overloaded is found. An error is reported when circular pointer
  1447. delegation is detected. */
  1448. tree
  1449. build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
  1450. {
  1451. tree orig_expr = expr;
  1452. tree type = TREE_TYPE (expr);
  1453. tree last_rval = NULL_TREE;
  1454. vec<tree, va_gc> *types_memoized = NULL;
  1455. if (type == error_mark_node)
  1456. return error_mark_node;
  1457. if (processing_template_decl)
  1458. {
  1459. if (type_dependent_expression_p (expr))
  1460. return build_min_nt_loc (loc, ARROW_EXPR, expr);
  1461. expr = build_non_dependent_expr (expr);
  1462. }
  1463. if (MAYBE_CLASS_TYPE_P (type))
  1464. {
  1465. struct tinst_level *actual_inst = current_instantiation ();
  1466. tree fn = NULL;
  1467. while ((expr = build_new_op (loc, COMPONENT_REF,
  1468. LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
  1469. &fn, complain)))
  1470. {
  1471. if (expr == error_mark_node)
  1472. return error_mark_node;
  1473. /* This provides a better instantiation backtrace in case of
  1474. error. */
  1475. if (fn && DECL_USE_TEMPLATE (fn))
  1476. push_tinst_level_loc (fn,
  1477. (current_instantiation () != actual_inst)
  1478. ? DECL_SOURCE_LOCATION (fn)
  1479. : input_location);
  1480. fn = NULL;
  1481. if (vec_member (TREE_TYPE (expr), types_memoized))
  1482. {
  1483. if (complain & tf_error)
  1484. error ("circular pointer delegation detected");
  1485. return error_mark_node;
  1486. }
  1487. vec_safe_push (types_memoized, TREE_TYPE (expr));
  1488. last_rval = expr;
  1489. }
  1490. while (current_instantiation () != actual_inst)
  1491. pop_tinst_level ();
  1492. if (last_rval == NULL_TREE)
  1493. {
  1494. if (complain & tf_error)
  1495. error ("base operand of %<->%> has non-pointer type %qT", type);
  1496. return error_mark_node;
  1497. }
  1498. if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
  1499. last_rval = convert_from_reference (last_rval);
  1500. }
  1501. else
  1502. last_rval = decay_conversion (expr, complain);
  1503. if (TYPE_PTR_P (TREE_TYPE (last_rval)))
  1504. {
  1505. if (processing_template_decl)
  1506. {
  1507. expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
  1508. orig_expr);
  1509. TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
  1510. return expr;
  1511. }
  1512. return cp_build_indirect_ref (last_rval, RO_NULL, complain);
  1513. }
  1514. if (complain & tf_error)
  1515. {
  1516. if (types_memoized)
  1517. error ("result of %<operator->()%> yields non-pointer result");
  1518. else
  1519. error ("base operand of %<->%> is not a pointer");
  1520. }
  1521. return error_mark_node;
  1522. }
  1523. /* Return an expression for "DATUM .* COMPONENT". DATUM has not
  1524. already been checked out to be of aggregate type. */
  1525. tree
  1526. build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
  1527. {
  1528. tree ptrmem_type;
  1529. tree objtype;
  1530. tree type;
  1531. tree binfo;
  1532. tree ctype;
  1533. if (error_operand_p (datum) || error_operand_p (component))
  1534. return error_mark_node;
  1535. datum = mark_lvalue_use (datum);
  1536. component = mark_rvalue_use (component);
  1537. ptrmem_type = TREE_TYPE (component);
  1538. if (!TYPE_PTRMEM_P (ptrmem_type))
  1539. {
  1540. if (complain & tf_error)
  1541. error ("%qE cannot be used as a member pointer, since it is of "
  1542. "type %qT", component, ptrmem_type);
  1543. return error_mark_node;
  1544. }
  1545. objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
  1546. if (! MAYBE_CLASS_TYPE_P (objtype))
  1547. {
  1548. if (complain & tf_error)
  1549. error ("cannot apply member pointer %qE to %qE, which is of "
  1550. "non-class type %qT", component, datum, objtype);
  1551. return error_mark_node;
  1552. }
  1553. type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
  1554. ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
  1555. if (!COMPLETE_TYPE_P (ctype))
  1556. {
  1557. if (!same_type_p (ctype, objtype))
  1558. goto mismatch;
  1559. binfo = NULL;
  1560. }
  1561. else
  1562. {
  1563. binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
  1564. if (!binfo)
  1565. {
  1566. mismatch:
  1567. if (complain & tf_error)
  1568. error ("pointer to member type %qT incompatible with object "
  1569. "type %qT", type, objtype);
  1570. return error_mark_node;
  1571. }
  1572. else if (binfo == error_mark_node)
  1573. return error_mark_node;
  1574. }
  1575. if (TYPE_PTRDATAMEM_P (ptrmem_type))
  1576. {
  1577. cp_lvalue_kind kind = lvalue_kind (datum);
  1578. tree ptype;
  1579. /* Compute the type of the field, as described in [expr.ref].
  1580. There's no such thing as a mutable pointer-to-member, so
  1581. things are not as complex as they are for references to
  1582. non-static data members. */
  1583. type = cp_build_qualified_type (type,
  1584. (cp_type_quals (type)
  1585. | cp_type_quals (TREE_TYPE (datum))));
  1586. datum = build_address (datum);
  1587. /* Convert object to the correct base. */
  1588. if (binfo)
  1589. {
  1590. datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
  1591. if (datum == error_mark_node)
  1592. return error_mark_node;
  1593. }
  1594. /* Build an expression for "object + offset" where offset is the
  1595. value stored in the pointer-to-data-member. */
  1596. ptype = build_pointer_type (type);
  1597. datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
  1598. datum = cp_build_indirect_ref (datum, RO_NULL, complain);
  1599. if (datum == error_mark_node)
  1600. return error_mark_node;
  1601. /* If the object expression was an rvalue, return an rvalue. */
  1602. if (kind & clk_class)
  1603. datum = rvalue (datum);
  1604. else if (kind & clk_rvalueref)
  1605. datum = move (datum);
  1606. return datum;
  1607. }
  1608. else
  1609. {
  1610. /* 5.5/6: In a .* expression whose object expression is an rvalue, the
  1611. program is ill-formed if the second operand is a pointer to member
  1612. function with ref-qualifier &. In a .* expression whose object
  1613. expression is an lvalue, the program is ill-formed if the second
  1614. operand is a pointer to member function with ref-qualifier &&. */
  1615. if (FUNCTION_REF_QUALIFIED (type))
  1616. {
  1617. bool lval = real_lvalue_p (datum);
  1618. if (lval && FUNCTION_RVALUE_QUALIFIED (type))
  1619. {
  1620. if (complain & tf_error)
  1621. error ("pointer-to-member-function type %qT requires an rvalue",
  1622. ptrmem_type);
  1623. return error_mark_node;
  1624. }
  1625. else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
  1626. {
  1627. if (complain & tf_error)
  1628. error ("pointer-to-member-function type %qT requires an lvalue",
  1629. ptrmem_type);
  1630. return error_mark_node;
  1631. }
  1632. }
  1633. return build2 (OFFSET_REF, type, datum, component);
  1634. }
  1635. }
  1636. /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
  1637. tree
  1638. build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
  1639. {
  1640. /* This is either a call to a constructor,
  1641. or a C cast in C++'s `functional' notation. */
  1642. /* The type to which we are casting. */
  1643. tree type;
  1644. vec<tree, va_gc> *parmvec;
  1645. if (error_operand_p (exp) || parms == error_mark_node)
  1646. return error_mark_node;
  1647. if (TREE_CODE (exp) == TYPE_DECL)
  1648. {
  1649. type = TREE_TYPE (exp);
  1650. if (complain & tf_warning
  1651. && TREE_DEPRECATED (type)
  1652. && DECL_ARTIFICIAL (exp))
  1653. warn_deprecated_use (type, NULL_TREE);
  1654. }
  1655. else
  1656. type = exp;
  1657. /* We need to check this explicitly, since value-initialization of
  1658. arrays is allowed in other situations. */
  1659. if (TREE_CODE (type) == ARRAY_TYPE)
  1660. {
  1661. if (complain & tf_error)
  1662. error ("functional cast to array type %qT", type);
  1663. return error_mark_node;
  1664. }
  1665. if (type_uses_auto (type))
  1666. {
  1667. if (complain & tf_error)
  1668. error ("invalid use of %<auto%>");
  1669. return error_mark_node;
  1670. }
  1671. if (processing_template_decl)
  1672. {
  1673. tree t;
  1674. /* Diagnose this even in a template. We could also try harder
  1675. to give all the usual errors when the type and args are
  1676. non-dependent... */
  1677. if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
  1678. {
  1679. if (complain & tf_error)
  1680. error ("invalid value-initialization of reference type");
  1681. return error_mark_node;
  1682. }
  1683. t = build_min (CAST_EXPR, type, parms);
  1684. /* We don't know if it will or will not have side effects. */
  1685. TREE_SIDE_EFFECTS (t) = 1;
  1686. return t;
  1687. }
  1688. if (! MAYBE_CLASS_TYPE_P (type))
  1689. {
  1690. if (parms == NULL_TREE)
  1691. {
  1692. if (VOID_TYPE_P (type))
  1693. return void_node;
  1694. return build_value_init (cv_unqualified (type), complain);
  1695. }
  1696. /* This must build a C cast. */
  1697. parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
  1698. return cp_build_c_cast (type, parms, complain);
  1699. }
  1700. /* Prepare to evaluate as a call to a constructor. If this expression
  1701. is actually used, for example,
  1702. return X (arg1, arg2, ...);
  1703. then the slot being initialized will be filled in. */
  1704. if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
  1705. return error_mark_node;
  1706. if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
  1707. return error_mark_node;
  1708. /* [expr.type.conv]
  1709. If the expression list is a single-expression, the type
  1710. conversion is equivalent (in definedness, and if defined in
  1711. meaning) to the corresponding cast expression. */
  1712. if (parms && TREE_CHAIN (parms) == NULL_TREE)
  1713. return cp_build_c_cast (type, TREE_VALUE (parms), complain);
  1714. /* [expr.type.conv]
  1715. The expression T(), where T is a simple-type-specifier for a
  1716. non-array complete object type or the (possibly cv-qualified)
  1717. void type, creates an rvalue of the specified type, which is
  1718. value-initialized. */
  1719. if (parms == NULL_TREE)
  1720. {
  1721. exp = build_value_init (type, complain);
  1722. exp = get_target_expr_sfinae (exp, complain);
  1723. return exp;
  1724. }
  1725. /* Call the constructor. */
  1726. parmvec = make_tree_vector ();
  1727. for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
  1728. vec_safe_push (parmvec, TREE_VALUE (parms));
  1729. exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
  1730. &parmvec, type, LOOKUP_NORMAL, complain);
  1731. release_tree_vector (parmvec);
  1732. if (exp == error_mark_node)
  1733. return error_mark_node;
  1734. return build_cplus_new (type, exp, complain);
  1735. }
  1736. /* Add new exception specifier SPEC, to the LIST we currently have.
  1737. If it's already in LIST then do nothing.
  1738. Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
  1739. know what we're doing. */
  1740. tree
  1741. add_exception_specifier (tree list, tree spec, int complain)
  1742. {
  1743. bool ok;
  1744. tree core = spec;
  1745. bool is_ptr;
  1746. diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
  1747. if (spec == error_mark_node)
  1748. return list;
  1749. gcc_assert (spec && (!list || TREE_VALUE (list)));
  1750. /* [except.spec] 1, type in an exception specifier shall not be
  1751. incomplete, or pointer or ref to incomplete other than pointer
  1752. to cv void. */
  1753. is_ptr = TYPE_PTR_P (core);
  1754. if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
  1755. core = TREE_TYPE (core);
  1756. if (complain < 0)
  1757. ok = true;
  1758. else if (VOID_TYPE_P (core))
  1759. ok = is_ptr;
  1760. else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
  1761. ok = true;
  1762. else if (processing_template_decl)
  1763. ok = true;
  1764. else
  1765. {
  1766. ok = true;
  1767. /* 15.4/1 says that types in an exception specifier must be complete,
  1768. but it seems more reasonable to only require this on definitions
  1769. and calls. So just give a pedwarn at this point; we will give an
  1770. error later if we hit one of those two cases. */
  1771. if (!COMPLETE_TYPE_P (complete_type (core)))
  1772. diag_type = DK_PEDWARN; /* pedwarn */
  1773. }
  1774. if (ok)
  1775. {
  1776. tree probe;
  1777. for (probe = list; probe; probe = TREE_CHAIN (probe))
  1778. if (same_type_p (TREE_VALUE (probe), spec))
  1779. break;
  1780. if (!probe)
  1781. list = tree_cons (NULL_TREE, spec, list);
  1782. }
  1783. else
  1784. diag_type = DK_ERROR; /* error */
  1785. if (diag_type != DK_UNSPECIFIED
  1786. && (complain & tf_warning_or_error))
  1787. cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
  1788. return list;
  1789. }
  1790. /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
  1791. static bool
  1792. nothrow_spec_p_uninst (const_tree spec)
  1793. {
  1794. if (DEFERRED_NOEXCEPT_SPEC_P (spec))
  1795. return false;
  1796. return nothrow_spec_p (spec);
  1797. }
  1798. /* Combine the two exceptions specifier lists LIST and ADD, and return
  1799. their union. */
  1800. tree
  1801. merge_exception_specifiers (tree list, tree add)
  1802. {
  1803. tree noex, orig_list;
  1804. /* No exception-specifier or noexcept(false) are less strict than
  1805. anything else. Prefer the newer variant (LIST). */
  1806. if (!list || list == noexcept_false_spec)
  1807. return list;
  1808. else if (!add || add == noexcept_false_spec)
  1809. return add;
  1810. /* noexcept(true) and throw() are stricter than anything else.
  1811. As above, prefer the more recent one (LIST). */
  1812. if (nothrow_spec_p_uninst (add))
  1813. return list;
  1814. /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
  1815. if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
  1816. && UNEVALUATED_NOEXCEPT_SPEC_P (list))
  1817. return list;
  1818. /* We should have instantiated other deferred noexcept specs by now. */
  1819. gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
  1820. if (nothrow_spec_p_uninst (list))
  1821. return add;
  1822. noex = TREE_PURPOSE (list);
  1823. gcc_checking_assert (!TREE_PURPOSE (add)
  1824. || cp_tree_equal (noex, TREE_PURPOSE (add)));
  1825. /* Combine the dynamic-exception-specifiers, if any. */
  1826. orig_list = list;
  1827. for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
  1828. {
  1829. tree spec = TREE_VALUE (add);
  1830. tree probe;
  1831. for (probe = orig_list; probe && TREE_VALUE (probe);
  1832. probe = TREE_CHAIN (probe))
  1833. if (same_type_p (TREE_VALUE (probe), spec))
  1834. break;
  1835. if (!probe)
  1836. {
  1837. spec = build_tree_list (NULL_TREE, spec);
  1838. TREE_CHAIN (spec) = list;
  1839. list = spec;
  1840. }
  1841. }
  1842. /* Keep the noexcept-specifier at the beginning of the list. */
  1843. if (noex != TREE_PURPOSE (list))
  1844. list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
  1845. return list;
  1846. }
  1847. /* Subroutine of build_call. Ensure that each of the types in the
  1848. exception specification is complete. Technically, 15.4/1 says that
  1849. they need to be complete when we see a declaration of the function,
  1850. but we should be able to get away with only requiring this when the
  1851. function is defined or called. See also add_exception_specifier. */
  1852. void
  1853. require_complete_eh_spec_types (tree fntype, tree decl)
  1854. {
  1855. tree raises;
  1856. /* Don't complain about calls to op new. */
  1857. if (decl && DECL_ARTIFICIAL (decl))
  1858. return;
  1859. for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
  1860. raises = TREE_CHAIN (raises))
  1861. {
  1862. tree type = TREE_VALUE (raises);
  1863. if (type && !COMPLETE_TYPE_P (type))
  1864. {
  1865. if (decl)
  1866. error
  1867. ("call to function %qD which throws incomplete type %q#T",
  1868. decl, type);
  1869. else
  1870. error ("call to function which throws incomplete type %q#T",
  1871. decl);
  1872. }
  1873. }
  1874. }
  1875. #include "gt-cp-typeck2.h"